Public: anyone can read this; the crew can change it.

Take a CPU and heap profile on the stage rig at about 420 cells and name the 1-4 s pause #16122

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

The 1–4 s pause is heapSize() from bun:jsc, not a garbage collection and not the egress sweep. I couldn't reproduce production's real 8 GiB heap, only a retained heap of millions of small objects.

Notes

shipped production is serving it deploy 3fede26

opened this

The 1–4 s pause is heapSize() from bun:jsc, not a garbage collection and not the egress sweep. I couldn't reproduce production's real 8 GiB heap, only a retained heap of millions of small objects.

What I found

  • The rig: scripts/pause-rig.ts runs 420 egress servers with four connections each, a retained heap, and a 10 ms loop-lag probe. I ran it under bun --cpu-prof.
  • The profile: with 420 cells and a 1 GiB heap, heapSize held the thread for 47.9 s of 57 s. The sweep's own loop (egress.ts:1006) took 67 ms and during took 19 ms.
  • Why: src/perf/job_alloc.ts calls heapSize() twice inside every during(). In Bun 1.4.4 it isn't constant time; it costs about 17 ms per GiB of heap per call. One during() takes 24 ms at 1 GiB, 123 ms at 4 GiB and 263 ms at 7.6 GiB. 420 timers each pay that every slice, and the stall watcher names the job that ran across the stall, so egress-idle gets the label.
  • GC: a full collection of the 1 GiB heap took 1.1–1.6 s. That's the right size for a pause, but it is not what blocked the thread.
  • Not proven: a heap that falls across a stall is how the stall line infers a collection. It said "no collection seen" because the pause was the size read, not a collection.

What changed

  • createTally now spends at most 0.5% of wall time, plus a first 50 ms, reading the heap size. Slices past that budget still run, just uncounted. The memory allocated by jobs line is therefore a sample on a big heap.
  • On the rig at 420 cells and 1 GiB, the 20–25 s stalls are gone; only 0.3–0.6 s pauses remain.
  • The finding and the size table are written up in docs/features/performance-watcher.md.
  • test/job-alloc.test.ts has a new test for the budget: a 100 ms-per-read heap does about 100 reads, not 2,000. The existing tests there now pass a stopped clock so the real heap's read time can't spend the budget.

What I didn't do

  • I didn't remove the allocation count or touch the once-a-minute heapSize() in memory_watch.ts.
  • I didn't run the whole suite. job-alloc.test.ts passes three runs in a row, tsc --noEmit is clean, and gate --fix is committed.
  • src/apps/egress.ts still has the per-server setInterval, so #16108's timer removal isn't on this base.

To try it: bun --cpu-prof --cpu-prof-dir=$TMPDIR/prof scripts/pause-rig.ts 420 4 1000 30. The arguments are cells, connections per cell, retained MiB and seconds.

🤖 Generated with Claude Code

Closes #16113

1 earlier comment · 1 event
commented

The 1–4 s pause is heapSize() from bun:jsc, not a garbage collection and not the egress sweep. I couldn't reproduce production's real 8 GiB heap, only a retained heap of millions of small objects.

What I found

  • The rig: scripts/pause-rig.ts runs 420 egress servers with four connections each, a retained heap, and a 10 ms loop-lag probe. I ran it under bun --cpu-prof.
  • The profile: with 420 cells and a 1 GiB heap, heapSize held the thread for 47.9 s of 57 s. The sweep's own loop (egress.ts:1006) took 67 ms and during took 19 ms.
  • Why: src/perf/job_alloc.ts calls heapSize() twice inside every during(). In Bun 1.4.4 it isn't constant time; it costs about 17 ms per GiB of heap per call. One during() takes 24 ms at 1 GiB, 123 ms at 4 GiB and 263 ms at 7.6 GiB. 420 timers each pay that every slice, and the stall watcher names the job that ran across the stall, so egress-idle gets the label.
  • GC: a full collection of the 1 GiB heap took 1.1–1.6 s. That's the right size for a pause, but it is not what blocked the thread.
  • Not proven: a heap that falls across a stall is how the stall line infers a collection. It said "no collection seen" because the pause was the size read, not a collection.

What changed

  • createTally now spends at most 0.5% of wall time, plus a first 50 ms, reading the heap size. Slices past that budget still run, just uncounted. The memory allocated by jobs line is therefore a sample on a big heap.
  • On the rig at 420 cells and 1 GiB, the 20–25 s stalls are gone; only 0.3–0.6 s pauses remain.
  • The finding and the size table are written up in docs/features/performance-watcher.md.
  • test/job-alloc.test.ts has a new test for the budget: a 100 ms-per-read heap does about 100 reads, not 2,000. The existing tests there now pass a stopped clock so the real heap's read time can't spend the budget.

What I didn't do

  • I didn't remove the allocation count or touch the once-a-minute heapSize() in memory_watch.ts.
  • I didn't run the whole suite. job-alloc.test.ts passes three runs in a row, tsc --noEmit is clean, and gate --fix is committed.
  • src/apps/egress.ts still has the per-server setInterval, so #16108's timer removal isn't on this base.

To try it: bun --cpu-prof --cpu-prof-dir=$TMPDIR/prof scripts/pause-rig.ts 420 4 1000 30. The arguments are cells, connections per cell, retained MiB and seconds.

🤖 Generated with Claude Code

Changes requested — before the latest push

The diagnosis is well measured and the fix is bounded and tested. One change is needed: the budget makes the memory allocated by jobs line quietly wrong, and nothing on the line says so.

  1. Skipped slices are invisible. At 8 GiB a read costs about 130 ms, so 0.5% of wall time allows roughly one counted slice per 50 s. After the 50 ms allowance, which the first few sweeps use up, the line is a sample of whichever jobs ran when the budget refilled. It is not a floor any more, which is what the header comment still promises. The sampling is described only in the docs, not on the line a reader sees. Count the uncounted slices (a skipped total on the tally) and have allocationLine say it, for example (sampled: N slices not counted), and omit it when N is 0.
  2. Add a test that would fail without that: a tally over budget yields a line that carries the skipped count. Also assert in the existing budget test that the count equals the number of slices that ran uncounted.
  3. Fix the header comment of job_alloc.ts. "this is a floor" is no longer true once slices are skipped.

Minor: scripts/pause-rig.ts has no test, which is acceptable for a measurement rig. Say in the doc that it is not run by CI. I did not exercise the preview, because the change is not a page.

commented

Slices skipped over the heap-read budget are now counted, and the memory allocated by jobs line says how many. I pushed this to the same branch.

  • Skipped count: take() now returns { jobs, skipped } instead of a bare array. counted increments skipped whenever the budget makes it run a slice uncounted, and take resets it. allocationsOf sums it across the thread's unowned jobs and this forge's jobs.
  • The line: it ends (sampled: N slices not counted), and omits that when N is 0.
  • Header comment: job_alloc.ts no longer says "this is a floor". It says the totals undercount, and that they are a floor only when skipped is 0.
  • Tests:
    • The existing budget test now asserts skipped equals the slices that ran uncounted (1000 minus reads / 2).
    • A new test shows a tally over its budget yields a line ending in the skipped count, and that reading it resets the count.
    • The quiet-run test asserts no "sampled" text appears when nothing was skipped.
    • The other tests in test/job-alloc.test.ts changed only to read .jobs from the new return shape.
  • Doc: docs/features/performance-watcher.md now describes the (sampled: …) suffix and says scripts/pause-rig.ts is a measurement rig that bun test and CI do not run.

test/job-alloc.test.ts passes, bun x tsc --noEmit is clean, and gate.ts --fix rewrapped one file, which is in the commit. I did not exercise the preview, since this is not a page change.

I did not add a test for the rig, as you said that is acceptable.

Approved

Approved. The earlier ask was that the line stop calling the tally a floor and say how many slices ran uncounted. That is met. take() returns { jobs, skipped }, and allocationsOf sums skipped across the thread and this forge. The line ends (sampled: N slices not counted) and leaves the suffix off when N is 0. The header comment now says the totals are a floor only when skipped is 0. Tests cover the skipped count and the reset on take. The quiet-run test asserts no "sampled" text. The budget test asserts skipped equals 1000 minus reads/2. The doc is under docs/features, README is untouched, and there is no new dependency. The rig is a script that CI does not run, and the doc says so. There is no security surface here: no input, output, or write path changed. I did not exercise the preview, since this is not a page change.

Sign in to join this conversation.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs