gunk-dev/the-valley

capacity.go

source78684ec35c8emain's published tip history
1.4 KiB
1package main
2
3// The capacity a pass builds on: one line of key=value pairs that the
4// pass hands to each attest run (attest run --capacity), so that every
5// check build the run records says what capacity it had. A check's build
6// is compared only with builds on the same capacity (checkcost.go).
7//
8// The line holds what the pass is given and what nix reports to it: slots
9// (--slots, or one per CPU), batches (--batches), nix's max-jobs and
10// cores as nix config show reads them, and the CPU count. A setting nix
11// does not report is left out.
12
13import (
14 "context"
15 "fmt"
16 "os/exec"
17 "runtime"
18 "strconv"
19 "strings"
20 "time"
21)
22
23// measureCapacity is the capacity of a pass that builds slots checks at
24// once in each batch (0 for attest's default) and batches batches:
25// "slots=2 batches=2 max_jobs=2 cores=2 cpus=6".
26func measureCapacity(slots, batches int) string {
27 if slots == 0 {
28 slots = runtime.NumCPU()
29 }
30 line := fmt.Sprintf("slots=%d batches=%d", slots, batches)
31 for _, name := range []string{"max-jobs", "cores"} {
32 ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
33 out, err := exec.CommandContext(ctx, "nix", "--extra-experimental-features", "nix-command", "config", "show", name).Output()
34 cancel()
35 if n, aerr := strconv.Atoi(strings.TrimSpace(string(out))); err == nil && aerr == nil {
36 line += fmt.Sprintf(" %s=%d", strings.ReplaceAll(name, "-", "_"), n)
37 }
38 }
39 return line + fmt.Sprintf(" cpus=%d", runtime.NumCPU())
40}