| 1 | package main |
| 2 | |
| 3 | // What each check of a version cost the host to build, against what the |
| 4 | // same check recently cost, and whether the host's newest build of it |
| 5 | // failed, for valley changes --checks, valley pending --checks and valley |
| 6 | // review. |
| 7 | // |
| 8 | // The numbers are the host's check worker's own: each attest run a pass |
| 9 | // starts records a check.result for each check it builds, in the worker's |
| 10 | // telemetry stream (workertelemetry.go), naming the project, change, |
| 11 | // version and tree. Its result is passed, failed, or error when attest |
| 12 | // could not decide the check, as when it did not evaluate. Its build_ms is |
| 13 | // how long the check's nix build took, and its eval_ms how long the run's |
| 14 | // shared evaluation took. A check that did not evaluate records no |
| 15 | // build_ms, and no cost counts it. The worker records the end of each batch |
| 16 | // too, with its result and its attest run. These are diagnostics, never |
| 17 | // evidence: nothing here is signed, and neither approve, the integrator, |
| 18 | // nor a check's or a version's state reads them. |
| 19 | // |
| 20 | // A listing reads the worker source once for each host it lists, with the |
| 21 | // same command valley telemetry read runs (telemetry.go). The newest page |
| 22 | // is read first, and then the older pages up to costRecords records, at |
| 23 | // once, each from a cursor made from the newest page's. valley pending |
| 24 | // starts the reads before it reads origin, and the other listings while |
| 25 | // they read the policy, so they add little to a listing's time. A |
| 26 | // host that does not answer, or refuses, leaves every version it serves |
| 27 | // without build times, and stderr says why, once. No read outlives the |
| 28 | // command that started it: once the listing has what it needs, it closes |
| 29 | // the reads, which cancels each one nobody waited for and returns once |
| 30 | // every ssh they started has ended. A command that ends any other way, |
| 31 | // by a signal or SIGPIPE, takes its reads' ssh with it (sshCommand). |
| 32 | // |
| 33 | // For each required nix check of a version: |
| 34 | // |
| 35 | // - build_ms is the newest build of the check for this version, and |
| 36 | // cached says whether its output was already in the host's store; |
| 37 | // - baseline_ms is the median build time of the check's newest |
| 38 | // baselineBuilds uncached, passing builds in the same project and on |
| 39 | // the same capacity as the version's own build, the |
| 40 | // builds of every version of this change left out; |
| 41 | // - new says no such build is among the records read, and |
| 42 | // other_capacity says there are builds, but only on other capacity: |
| 43 | // the host was resized, so the check starts afresh and is not grown; |
| 44 | // - grown says the build took longer than its baseline by more than |
| 45 | // both grownShare of it and grownBy (grew). |
| 46 | // |
| 47 | // A build's capacity is the line the worker gave attest (capacity.go). |
| 48 | // Records with none compare only with each other. |
| 49 | // |
| 50 | // A record from an attest that does not say whether the output was |
| 51 | // cached counts as cached when its build wrote no log: nix writes the |
| 52 | // builder's output to the log, and building nothing writes none. |
| 53 | // |
| 54 | // attest publishes nothing when any check of its batch fails, so a check |
| 55 | // the worker built and that failed has no note, and its state is pending. |
| 56 | // The worker's report tells it from a check not built yet: |
| 57 | // |
| 58 | // - a check whose state is not passed carries worker, the result, time |
| 59 | // and attest run of the newest build of the check for the version's |
| 60 | // tree, when that build did not pass. The build may be any change's |
| 61 | // or version's with the same tree, since a check is about a tree. A |
| 62 | // record from an attest that names no tree counts only for its own |
| 63 | // change and version; |
| 64 | // - when a check is pending, the version carries worker, the time and |
| 65 | // attest run of the newest batch for its tree, when that batch failed |
| 66 | // and no check.result names its attest run: it failed before any |
| 67 | // check ran, as when the evaluation or attest itself failed. |
| 68 | |
| 69 | import ( |
| 70 | "bufio" |
| 71 | "context" |
| 72 | "encoding/json" |
| 73 | "fmt" |
| 74 | "io" |
| 75 | "os" |
| 76 | "sort" |
| 77 | "strings" |
| 78 | "sync" |
| 79 | "time" |
| 80 | |
| 81 | "the-valley/telemetry" |
| 82 | ) |
| 83 | |
| 84 | // workerSource is the telemetry source the host module gives the check |
| 85 | // worker. |
| 86 | const workerSource = "worker" |
| 87 | |
| 88 | // costRecords bounds how many of the worker's newest records a listing |
| 89 | // reads from one host: five of the collector's largest pages. Each batch |
| 90 | // of checks writes about five records a check, so this holds the last |
| 91 | // few hundred check builds. |
| 92 | const costRecords = 5 * telemetry.MaxReadLimit |
| 93 | |
| 94 | // baselineBuilds is how many of a check's recent builds its baseline is |
| 95 | // the median of. |
| 96 | const baselineBuilds = 20 |
| 97 | |
| 98 | // A check has grown when its build took longer than its baseline by more |
| 99 | // than both of these. Batches build side by side on one host, so a build |
| 100 | // can take a few seconds longer, or a few percent, for reasons of the |
| 101 | // host's and not the change's. Only growth past both is shown. |
| 102 | const ( |
| 103 | grownShare = 0.20 |
| 104 | grownBy = 30 * time.Second |
| 105 | ) |
| 106 | |
| 107 | // grew reports whether a build of build ms has grown against a baseline |
| 108 | // of baseline ms. |
| 109 | func grew(build, baseline int64) bool { |
| 110 | d := build - baseline |
| 111 | return d > grownBy.Milliseconds() && float64(d) > grownShare*float64(baseline) |
| 112 | } |
| 113 | |
| 114 | // median is the median of ms, which must not be empty. |
| 115 | func median(ms []int64) int64 { |
| 116 | s := append([]int64(nil), ms...) |
| 117 | sort.Slice(s, func(i, j int) bool { return s[i] < s[j] }) |
| 118 | n := len(s) |
| 119 | if n%2 == 1 { |
| 120 | return s[n/2] |
| 121 | } |
| 122 | return (s[n/2-1] + s[n/2]) / 2 |
| 123 | } |
| 124 | |
| 125 | // checkBuild is one check.result record of the worker's: one check built |
| 126 | // for one version or main's tip, or one that did not evaluate. |
| 127 | type checkBuild struct { |
| 128 | time, run string |
| 129 | project, change string |
| 130 | version int |
| 131 | tree, check string |
| 132 | result string |
| 133 | unevaluated bool // the check did not evaluate, so nothing was built |
| 134 | buildMS, evalMS int64 |
| 135 | cached bool |
| 136 | capacity string |
| 137 | } |
| 138 | |
| 139 | // forTree reports whether b built a check for tree, or, when its record |
| 140 | // names no tree, for version n of the change changeID. |
| 141 | func (b *checkBuild) forTree(tree, changeID string, n int) bool { |
| 142 | return b.tree == tree || b.tree == "" && b.change == changeID && b.version == n |
| 143 | } |
| 144 | |
| 145 | // batchEnd is the end of a batch the worker ran for a change's version: |
| 146 | // when it ended, its attest run, the project and tree it was for, and its |
| 147 | // result, passed, failed or cancelled. |
| 148 | type batchEnd struct { |
| 149 | time, run string |
| 150 | project, tree string |
| 151 | result string |
| 152 | } |
| 153 | |
| 154 | // costs reads the worker's telemetry of each host a listing serves, once |
| 155 | // a host, in the background, until close. |
| 156 | type costs struct { |
| 157 | mu sync.Mutex |
| 158 | reads map[string]*costRead // by destination identity |
| 159 | said map[string]bool // the reasons already said on stderr |
| 160 | ctx context.Context // every read's, done once closed |
| 161 | cancel context.CancelFunc |
| 162 | closed bool // no read starts any more |
| 163 | running sync.WaitGroup // the reads not yet ended |
| 164 | } |
| 165 | |
| 166 | type costRead struct { |
| 167 | done chan struct{} |
| 168 | builds []checkBuild // newest first |
| 169 | mains []mainRun // the batches run on main tips (mainstate.go) |
| 170 | batches []batchEnd // the ends of the batches run for changes, newest first |
| 171 | why string // why there are none, when the read failed |
| 172 | } |
| 173 | |
| 174 | func newCosts() *costs { |
| 175 | ctx, cancel := context.WithCancel(context.Background()) |
| 176 | return &costs{reads: map[string]*costRead{}, said: map[string]bool{}, ctx: ctx, cancel: cancel} |
| 177 | } |
| 178 | |
| 179 | // close cancels every read still running, whose answer nobody waits for |
| 180 | // any more, and returns once each has ended with every ssh it started. |
| 181 | func (c *costs) close() { |
| 182 | c.mu.Lock() |
| 183 | c.closed = true |
| 184 | c.mu.Unlock() |
| 185 | c.cancel() |
| 186 | c.running.Wait() |
| 187 | } |
| 188 | |
| 189 | // start begins the read of the host g's telemetry remote names |
| 190 | // (telemetryRemote), unless it has begun. |
| 191 | func (c *costs) start(g git) *costRead { |
| 192 | dest, err := telemetryRemote(g, "") |
| 193 | c.mu.Lock() |
| 194 | defer c.mu.Unlock() |
| 195 | if err != nil { |
| 196 | r := &costRead{done: make(chan struct{}), why: err.Error()} |
| 197 | close(r.done) |
| 198 | return r |
| 199 | } |
| 200 | if r := c.reads[dest.identity()]; r != nil { |
| 201 | return r |
| 202 | } |
| 203 | if c.closed { |
| 204 | r := &costRead{done: make(chan struct{}), why: "the command is ending"} |
| 205 | close(r.done) |
| 206 | return r |
| 207 | } |
| 208 | r := &costRead{done: make(chan struct{})} |
| 209 | c.reads[dest.identity()] = r |
| 210 | c.running.Add(1) |
| 211 | go func() { |
| 212 | defer c.running.Done() |
| 213 | defer close(r.done) |
| 214 | r.builds, r.mains, r.batches, r.why = readBuilds(c.ctx, dest) |
| 215 | }() |
| 216 | return r |
| 217 | } |
| 218 | |
| 219 | // wait waits for a read to end, and reports whether it read. The reason a |
| 220 | // read failed is said on stderr the first time it is met. |
| 221 | func (c *costs) wait(r *costRead, warn func(format string, args ...any)) bool { |
| 222 | <-r.done |
| 223 | if r.why == "" { |
| 224 | return true |
| 225 | } |
| 226 | c.mu.Lock() |
| 227 | said := c.said[r.why] |
| 228 | c.said[r.why] = true |
| 229 | c.mu.Unlock() |
| 230 | if !said { |
| 231 | warn("no check lists its build times: %s", r.why) |
| 232 | } |
| 233 | return false |
| 234 | } |
| 235 | |
| 236 | // readBuilds reads up to costRecords of the newest records of the worker |
| 237 | // source at dest, and returns the check builds among them, newest first, |
| 238 | // the starts and ends of the batches run on main tips, and the ends of the |
| 239 | // batches run for changes, newest first. Each page's ssh ends with ctx. |
| 240 | func readBuilds(ctx context.Context, dest sshDest) ([]checkBuild, []mainRun, []batchEnd, string) { |
| 241 | first, err := readPage(ctx, dest, "") |
| 242 | if err != nil { |
| 243 | return nil, nil, nil, err.Error() |
| 244 | } |
| 245 | pages := []workerPage{first} |
| 246 | var mu sync.Mutex |
| 247 | var wg sync.WaitGroup |
| 248 | for p := 1; p*telemetry.MaxReadLimit < costRecords; p++ { |
| 249 | // Page p holds, for each stream, the records up to p pages |
| 250 | // before the newest one the first page read. |
| 251 | var parts []string |
| 252 | for name, pos := range first.cursor { |
| 253 | if after := pos.Seq - int64((p+1)*telemetry.MaxReadLimit); after+telemetry.MaxReadLimit > 0 { |
| 254 | parts = append(parts, fmt.Sprintf("%s=%s:%d", name, pos.Epoch, max(after, 0))) |
| 255 | } |
| 256 | } |
| 257 | if len(parts) == 0 { |
| 258 | break |
| 259 | } |
| 260 | sort.Strings(parts) |
| 261 | wg.Add(1) |
| 262 | go func() { |
| 263 | defer wg.Done() |
| 264 | // An older page that cannot be read leaves the baselines |
| 265 | // shorter, and nothing else. |
| 266 | if page, err := readPage(ctx, dest, strings.Join(parts, ",")); err == nil { |
| 267 | mu.Lock() |
| 268 | pages = append(pages, page) |
| 269 | mu.Unlock() |
| 270 | } |
| 271 | }() |
| 272 | } |
| 273 | wg.Wait() |
| 274 | seen := map[string]bool{} |
| 275 | var builds []checkBuild |
| 276 | var mains []mainRun |
| 277 | var batches []batchEnd |
| 278 | for _, page := range pages { |
| 279 | mains = append(mains, page.mains...) |
| 280 | for _, b := range page.builds { |
| 281 | if key := b.key; !seen[key] { |
| 282 | seen[key] = true |
| 283 | builds = append(builds, b.checkBuild) |
| 284 | } |
| 285 | } |
| 286 | for _, e := range page.batches { |
| 287 | if key := e.key; !seen[key] { |
| 288 | seen[key] = true |
| 289 | batches = append(batches, e.batchEnd) |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | sort.SliceStable(builds, func(i, j int) bool { return builds[i].time > builds[j].time }) |
| 294 | sort.SliceStable(batches, func(i, j int) bool { return batches[i].time > batches[j].time }) |
| 295 | return builds, mains, batches, "" |
| 296 | } |
| 297 | |
| 298 | // workerPage is one answer to a read of the worker source. |
| 299 | type workerPage struct { |
| 300 | cursor telemetry.Cursor |
| 301 | builds []keyedBuild |
| 302 | mains []mainRun |
| 303 | batches []keyedBatch |
| 304 | } |
| 305 | |
| 306 | type keyedBatch struct { |
| 307 | batchEnd |
| 308 | key string // the record's stream and number |
| 309 | } |
| 310 | |
| 311 | type keyedBuild struct { |
| 312 | checkBuild |
| 313 | key string // the record's stream and number |
| 314 | } |
| 315 | |
| 316 | // readPage reads one page of the worker source: the records after the |
| 317 | // cursor, or the newest without one. When ctx ends, the ssh is killed and |
| 318 | // the read of its answer stops, even if something it started, such as a |
| 319 | // ProxyCommand, still holds the answer open. What ssh says comes through |
| 320 | // a pipe the read owns rather than one os/exec copies, so the wait for |
| 321 | // ssh ends when ssh does, never after the wait for a pipe something else |
| 322 | // holds (sshCommand's WaitDelay), and its last sshSaidLimit bytes are |
| 323 | // kept. |
| 324 | func readPage(ctx context.Context, dest sshDest, cursor string) (workerPage, error) { |
| 325 | req := telemetry.Request{V: 1, Op: telemetry.OpRead, Source: workerSource, Cursor: cursor, Limit: telemetry.MaxReadLimit} |
| 326 | ctx, cancel := context.WithTimeout(ctx, 30*time.Second) |
| 327 | defer cancel() |
| 328 | ssh := sshCommand(ctx, dest, req.Command()) |
| 329 | out, err := ssh.StdoutPipe() |
| 330 | if err != nil { |
| 331 | return workerPage{}, err |
| 332 | } |
| 333 | defer context.AfterFunc(ctx, func() { out.Close() })() |
| 334 | r, w, err := os.Pipe() |
| 335 | if err != nil { |
| 336 | return workerPage{}, fmt.Errorf("could not run ssh: %v", err) |
| 337 | } |
| 338 | ssh.Stderr = w |
| 339 | err = ssh.Start() |
| 340 | w.Close() |
| 341 | if err != nil { |
| 342 | r.Close() |
| 343 | return workerPage{}, fmt.Errorf("could not run ssh: %v", err) |
| 344 | } |
| 345 | said := &lastBytes{max: sshSaidLimit} |
| 346 | heard := make(chan struct{}) |
| 347 | go func() { |
| 348 | io.Copy(said, r) |
| 349 | close(heard) |
| 350 | }() |
| 351 | stopHearing := func() { |
| 352 | r.Close() |
| 353 | <-heard |
| 354 | } |
| 355 | defer stopHearing() |
| 356 | page, refused, err := parsePage(out) |
| 357 | io.Copy(io.Discard, out) |
| 358 | waitErr := ssh.Wait() |
| 359 | switch { |
| 360 | case refused != "": |
| 361 | return workerPage{}, fmt.Errorf("%s refused to read the worker's telemetry: %s", dest.host, refused) |
| 362 | case err == nil: |
| 363 | return page, nil |
| 364 | } |
| 365 | // ssh has ended, but a process it started may hold its stderr open, |
| 366 | // and no event says it will never write again: the rest of what ssh |
| 367 | // said is waited for up to sshWaitDelay, as os/exec waits, and not at |
| 368 | // all once the read is cancelled. |
| 369 | select { |
| 370 | case <-heard: |
| 371 | case <-ctx.Done(): |
| 372 | case <-time.After(sshWaitDelay): |
| 373 | } |
| 374 | stopHearing() |
| 375 | if why := strings.TrimSpace(telemetry.Sanitize(string(said.b))); why != "" { |
| 376 | err = fmt.Errorf("%v: %s", err, why) |
| 377 | } else if waitErr != nil { |
| 378 | err = fmt.Errorf("%v: %v", err, waitErr) |
| 379 | } |
| 380 | return workerPage{}, fmt.Errorf("the worker's telemetry on %s did not read: %v", dest.host, err) |
| 381 | } |
| 382 | |
| 383 | // sshSaidLimit is how much of what ssh says on stderr a read keeps: its |
| 384 | // last bytes, which say why it failed. A remote or a proxy that says more |
| 385 | // fills neither memory nor storage. |
| 386 | const sshSaidLimit = 4 << 10 |
| 387 | |
| 388 | // lastBytes keeps the last max bytes written through it. |
| 389 | type lastBytes struct { |
| 390 | b []byte |
| 391 | max int |
| 392 | } |
| 393 | |
| 394 | func (l *lastBytes) Write(p []byte) (int, error) { |
| 395 | if len(p) >= l.max { |
| 396 | l.b = append(l.b[:0], p[len(p)-l.max:]...) |
| 397 | return len(p), nil |
| 398 | } |
| 399 | if extra := len(l.b) + len(p) - l.max; extra > 0 { |
| 400 | l.b = append(l.b[:0], l.b[extra:]...) |
| 401 | } |
| 402 | l.b = append(l.b, p...) |
| 403 | return len(p), nil |
| 404 | } |
| 405 | |
| 406 | // parsePage reads one answer, which must end, or says why the host |
| 407 | // refused it. |
| 408 | func parsePage(r io.Reader) (page workerPage, refused string, err error) { |
| 409 | sc := bufio.NewScanner(r) |
| 410 | sc.Buffer(make([]byte, 64<<10), telemetry.MaxResponseBytes) |
| 411 | for sc.Scan() { |
| 412 | var m struct { |
| 413 | Kind string `json:"kind"` |
| 414 | Stream string `json:"stream"` |
| 415 | Seq int64 `json:"seq"` |
| 416 | Type string `json:"type"` |
| 417 | Time string `json:"time"` |
| 418 | Run string `json:"run"` |
| 419 | Project string `json:"project"` |
| 420 | Change string `json:"change"` |
| 421 | Version int `json:"version"` |
| 422 | Tree string `json:"tree"` |
| 423 | Check string `json:"check"` |
| 424 | Data map[string]any `json:"data"` |
| 425 | Error string `json:"error"` |
| 426 | What string `json:"what"` |
| 427 | Cursor string `json:"cursor"` |
| 428 | } |
| 429 | if json.Unmarshal(sc.Bytes(), &m) != nil { |
| 430 | return workerPage{}, "", fmt.Errorf("an answer line is not JSON") |
| 431 | } |
| 432 | switch m.Kind { |
| 433 | case "error": |
| 434 | return workerPage{}, telemetry.Sanitize(m.Error), nil |
| 435 | case "unavailable": |
| 436 | // The journal says nothing a build time needs. A stream the |
| 437 | // host could not read may be missing records, so the page is |
| 438 | // not complete. |
| 439 | if m.What == "journal" { |
| 440 | continue |
| 441 | } |
| 442 | return workerPage{}, "", fmt.Errorf("the host could not read %s: %s", telemetry.Sanitize(m.What), telemetry.Sanitize(m.Error)) |
| 443 | case "end": |
| 444 | cur, err := telemetry.ParseCursor(m.Cursor) |
| 445 | if err != nil { |
| 446 | return workerPage{}, "", err |
| 447 | } |
| 448 | page.cursor = cur |
| 449 | return page, "", nil |
| 450 | case "record": |
| 451 | if m.Change == "" && (m.Type == "batch.start" || m.Type == "batch.end") { |
| 452 | // A follow run or a request; its class tells which |
| 453 | // (mainstate.go). |
| 454 | run := mainRun{project: m.Project, tree: m.Tree, ended: m.Type == "batch.end"} |
| 455 | run.class, _ = m.Data["class"].(string) |
| 456 | run.run, _ = m.Data["attest_run"].(string) |
| 457 | checks, _ := m.Data["checks"].([]any) |
| 458 | for _, c := range checks { |
| 459 | if name, ok := c.(string); ok { |
| 460 | run.checks = append(run.checks, name) |
| 461 | } |
| 462 | } |
| 463 | page.mains = append(page.mains, run) |
| 464 | continue |
| 465 | } |
| 466 | if m.Type == "batch.end" { |
| 467 | e := batchEnd{time: m.Time, project: m.Project, tree: m.Tree} |
| 468 | e.run, _ = m.Data["attest_run"].(string) |
| 469 | e.result, _ = m.Data["result"].(string) |
| 470 | page.batches = append(page.batches, keyedBatch{e, fmt.Sprintf("%s#%d", m.Stream, m.Seq)}) |
| 471 | continue |
| 472 | } |
| 473 | if m.Type != "check.result" { |
| 474 | continue |
| 475 | } |
| 476 | // A check that did not evaluate is kept as a run that failed, |
| 477 | // which main's state and a version's worker report read. It |
| 478 | // built nothing, so no cost counts it. |
| 479 | build, built := m.Data["build_ms"].(float64) |
| 480 | b := checkBuild{time: m.Time, run: m.Run, project: m.Project, change: m.Change, version: m.Version, |
| 481 | tree: m.Tree, check: m.Check, unevaluated: !built, buildMS: int64(build)} |
| 482 | b.result, _ = m.Data["result"].(string) |
| 483 | if eval, ok := m.Data["eval_ms"].(float64); ok { |
| 484 | b.evalMS = int64(eval) |
| 485 | } |
| 486 | b.capacity, _ = m.Data["capacity"].(string) |
| 487 | if cached, ok := m.Data["cached"].(bool); ok { |
| 488 | b.cached = cached |
| 489 | } else { |
| 490 | logged, _ := m.Data["log_bytes"].(float64) |
| 491 | b.cached = logged == 0 |
| 492 | } |
| 493 | page.builds = append(page.builds, keyedBuild{b, fmt.Sprintf("%s#%d", m.Stream, m.Seq)}) |
| 494 | } |
| 495 | } |
| 496 | return workerPage{}, "", fmt.Errorf("the answer did not end") |
| 497 | } |
| 498 | |
| 499 | // addCosts gives a version's nix checks what they cost to build, from |
| 500 | // builds, newest first: the version's own, and the recent builds of each |
| 501 | // check in its project. |
| 502 | func addCosts(out *jsonVersionChecks, required []jsonRequired, project, changeID string, n int, builds []checkBuild) { |
| 503 | runner := map[string]string{} |
| 504 | for _, q := range required { |
| 505 | runner[q.Name] = q.Runner |
| 506 | } |
| 507 | var total int64 |
| 508 | evals := map[string]int64{} // each attest run's evaluation |
| 509 | found := false |
| 510 | for i := range out.Required { |
| 511 | s := &out.Required[i] |
| 512 | if runner[s.Name] != "nix" { |
| 513 | continue |
| 514 | } |
| 515 | var own *checkBuild |
| 516 | for j := range builds { |
| 517 | b := &builds[j] |
| 518 | if !b.unevaluated && b.project == project && b.check == s.Name && b.change == changeID && b.version == n && (b.tree == "" || b.tree == out.Tree) { |
| 519 | own = b |
| 520 | break |
| 521 | } |
| 522 | } |
| 523 | var recent []int64 |
| 524 | other := false |
| 525 | for j := range builds { |
| 526 | b := &builds[j] |
| 527 | if b.unevaluated || b.project != project || b.check != s.Name { |
| 528 | continue |
| 529 | } |
| 530 | if b.change == changeID || b.cached || b.result != checkPassed { |
| 531 | continue |
| 532 | } |
| 533 | if own != nil && b.capacity != own.capacity { |
| 534 | other = true |
| 535 | } else if len(recent) < baselineBuilds { |
| 536 | recent = append(recent, b.buildMS) |
| 537 | } |
| 538 | } |
| 539 | if own != nil { |
| 540 | found = true |
| 541 | build, cached := own.buildMS, own.cached |
| 542 | s.BuildMS, s.Cached = &build, &cached |
| 543 | total += build |
| 544 | evals[own.run] = max(evals[own.run], own.evalMS) |
| 545 | } |
| 546 | if len(recent) == 0 { |
| 547 | s.New, s.OtherCapacity = true, other |
| 548 | continue |
| 549 | } |
| 550 | base := median(recent) |
| 551 | s.BaselineMS = &base |
| 552 | s.Grown = own != nil && !own.cached && grew(own.buildMS, base) |
| 553 | } |
| 554 | if !found { |
| 555 | return |
| 556 | } |
| 557 | var eval int64 |
| 558 | for _, ms := range evals { |
| 559 | eval += ms |
| 560 | } |
| 561 | out.BuildMS, out.EvalMS = &total, &eval |
| 562 | } |
| 563 | |
| 564 | // addWorkerReports gives each nix check of a version that no note makes |
| 565 | // passed the worker's report of its newest build for the version's tree, |
| 566 | // when that build failed, whatever change or version it was built for. |
| 567 | // When a nix check is pending, the version also gets the report of the |
| 568 | // worker's newest batch for its tree, when that batch failed and recorded |
| 569 | // no check's result: it failed before any check ran. builds and batches |
| 570 | // are newest first. |
| 571 | func addWorkerReports(out *jsonVersionChecks, required []jsonRequired, project, changeID string, n int, builds []checkBuild, batches []batchEnd) { |
| 572 | runner := map[string]string{} |
| 573 | for _, q := range required { |
| 574 | runner[q.Name] = q.Runner |
| 575 | } |
| 576 | ran := map[string]bool{} // the attest runs that recorded a check's result |
| 577 | for i := range builds { |
| 578 | ran[builds[i].run] = true |
| 579 | } |
| 580 | pending := false |
| 581 | for i := range out.Required { |
| 582 | s := &out.Required[i] |
| 583 | if runner[s.Name] != "nix" || s.State == checkPassed { |
| 584 | continue |
| 585 | } |
| 586 | pending = pending || s.State == checkPending |
| 587 | for j := range builds { |
| 588 | b := &builds[j] |
| 589 | if b.project != project || b.check != s.Name || !b.forTree(out.Tree, changeID, n) { |
| 590 | continue |
| 591 | } |
| 592 | if b.result != checkPassed { |
| 593 | s.Worker = &jsonWorkerReport{Result: b.result, Time: b.time, Run: b.run} |
| 594 | } |
| 595 | break |
| 596 | } |
| 597 | } |
| 598 | if !pending { |
| 599 | return |
| 600 | } |
| 601 | for _, e := range batches { |
| 602 | if e.project != project || e.tree != out.Tree { |
| 603 | continue |
| 604 | } |
| 605 | if e.result == checkFailed && e.run != "" && !ran[e.run] { |
| 606 | out.Worker = &jsonWorkerReport{Result: e.result, Time: e.time, Run: e.run} |
| 607 | } |
| 608 | break |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | // costLine is what checksLine adds about a version's build times: its |
| 613 | // total and its evaluation, then each check that grew against its |
| 614 | // baseline, most first: " in 9m10s (eval 24s); worker-e2e 431s, +68s |
| 615 | // against its recent 363s". A check with no baseline is no news, and is |
| 616 | // left out. |
| 617 | func costLine(c *jsonVersionChecks) string { |
| 618 | if c.BuildMS == nil { |
| 619 | return "" |
| 620 | } |
| 621 | line := fmt.Sprintf(" in %s (eval %s)", duration(*c.BuildMS), duration(*c.EvalMS)) |
| 622 | var grown []jsonCheckState |
| 623 | for _, q := range c.Required { |
| 624 | if q.Grown { |
| 625 | grown = append(grown, q) |
| 626 | } |
| 627 | } |
| 628 | sort.SliceStable(grown, func(i, j int) bool { |
| 629 | return *grown[i].BuildMS-*grown[i].BaselineMS > *grown[j].BuildMS-*grown[j].BaselineMS |
| 630 | }) |
| 631 | for _, q := range grown { |
| 632 | line += fmt.Sprintf("; %s %s, +%s against its recent %s", q.Name, seconds(*q.BuildMS), seconds(*q.BuildMS-*q.BaselineMS), seconds(*q.BaselineMS)) |
| 633 | } |
| 634 | return line |
| 635 | } |
| 636 | |
| 637 | // duration is ms as 9m10s, to the second. |
| 638 | func duration(ms int64) string { |
| 639 | return (time.Duration(ms) * time.Millisecond).Round(time.Second).String() |
| 640 | } |
| 641 | |
| 642 | // seconds is ms as 431s, to the second. |
| 643 | func seconds(ms int64) string { |
| 644 | return fmt.Sprintf("%ds", (ms+500)/1000) |
| 645 | } |