Repository navigation
[JSC] Sampling profiler: record ticks without a JavaScript frame - #1091
dylan-conway wants to merge 3 commits into
Conversation
…ipt frame It dropped every tick outside the VM and every tick whose stack it could not walk, so an embedder had to bill that time to a neighbouring function. On request such ticks are now kept, as idle, program, garbage collector or JavaScript. The timer thread also parks while paused, wakes up on start, on a new interval and on shutdown, is detached, never spins, and keeps its interval on Windows. The depth of a stack trace can be limited. Co-authored-by: dylanc <[email protected]>
| // The sampler is a thread like any other, so nothing here counts on how often it runs. | ||
| function failIfOutOfTime(describe) { | ||
| failIfOutOfTime.startTime ??= Date.now(); | ||
| if (Date.now() - failIfOutOfTime.startTime > 50000) |
There was a problem hiding this comment.
🟡 nit (optional): the new sampling-profiler tests are open-ended timing loops, so the stress suite's wall time grows by seconds per configuration where JSTests/README.md requires every new test to finish in under 200ms in all configurations. Each test repeats sleep/spin/allocate steps until the sampler has delivered enough samples, bounded only by the 50000 ms budget in samplesWithoutFrames.js:9, and most have no //@ directive so they run in every default configuration. Fix: keep each run under the README limit in every configuration (check with --report-execution-time), e.g. by bounding the step counts with testLoopCount and marking the long ones //@ slow!, which covers all eight new test files. [also at: JSTests/stress/sampling-profiler-samples-without-frames-heap-access.js:37 - nit: Maintainers running the stress suite get a new test that runs well past the 200 ms budget JSTests/README.md sets for new tests.]
Why this was flagged
JSTests/AGENTS.md imports JSTests/README.md, whose rule 1 says new tests must run in under 200ms in all configurations and rule 2 says to use testLoopCount for iteration counts. The new tests instead loop until a sampled condition holds: sampling-profiler-samples-without-frames.js:9-91 runs about ten phases each needing 10-50 samples from 2 ms setTimeout waits; sampling-profiler-samples-without-frames-heap-access.js:32-56 builds lists up to 3200000 nodes. The only bound is failIfOutOfTime at samplesWithoutFrames.js:9, 50 s. Five of the eight files carry no //@ line, so run-jsc-stress-tests runs them under defaultRun in every configuration. Only sampling-profiler-samples-without-frames-short-calls.js:9 uses testLoopCount. On the base branch none of these files exist; the existing upstream sampling-profiler tests have the same shape, so this is consistent with precedent but not with the README rule the fork's AGENTS.md applies.
Verification: Triggering condition: the tests run in every default configuration on a platform with ENABLE(SAMPLING_PROFILER). JSTests/README.md:19-20 states "1. Tests must run in under 200ms in all configurations". The new tests instead loop until a sampled condition holds, bounded only by the 50 s budget at JSTests/stress/sampling-profiler/samplesWithoutFrames.js:7-11.
There was a problem hiding this comment.
They end as soon as the sampler has delivered, the 50 s are only the limit for failing. With --report-execution-time five of the seven take under 150 ms in every mode. The GC test takes about 250 ms and the main one about 200 ms (400 in bytecode-cache, which runs it twice). The two that were slowest in the eager modes no longer run in those.
Generated by Claude Code
|
Preview build of 6867f14: |
JavaScriptCore now keeps the ticks that have no JavaScript frame, so the event loop only says when it waits. That replaces the recorder and the synthesized samples, and adds (garbage collector) and (program) between calls. WebKit is pinned to the preview build of oven-sh/WebKit#1091. Co-authored-by: dylanc <[email protected]>
…ick is kept Whoever only takes stack traces gains nothing from it, and is now suspended no more than before. Work for the collector also goes before the idle flag, and the new tests are listed as the fork's own. Co-authored-by: dylanc <[email protected]>
There was a problem hiding this comment.
I re-reviewed the follow-up commit and found no new bugs; since this reworks the profiler's timer thread, adds collector-tracking to Heap hot paths, and the Windows and macOS paths were not run, a look from a JSC reviewer would still be worthwhile.
What was reviewed: the follow-up's reordered executionThreadState() (collector before idle) and the restored base-branch wait charging for the stack-trace-only path; the park/wake protocol of timerLoop() against shutdown(), startWithLock() and setTimingInterval() (each notifies under the lock, and the Windows manual-reset timer is re-armed before every wait); the ServingCollectorScope placements in waitForCollector, acquireAccessSlow, sweepSynchronously and performIncrement; and the RELEASE_ASSERT(depth) in setMaximumStackDepth(), ruled out as an explicit contract since the jsc shell maps 0 to unlimitedStackDepth itself. Not done here: the per-test execution-time check that JSTests/README.md asks for, as no test runner is available in this session.
Extended reasoning...
The change adds a samples-without-frames mode to SamplingProfiler (new ThreadState, timer loop rewritten to park on a Condition / Windows high-resolution timer, detached thread, setMaximumStackDepth and a new option), adds an Atomic serving-collector depth to Heap read from the sampling thread, extends the jsc shell with three test hooks, and adds seven stress tests plus a helper and BUN-TEST-DIFFERENCES rows. It touches no auth, injection or data-exposure surface. The latest commit addressed most of my prior inline comments (collector-before-idle ordering, base timing restored for the stack-trace-only path, fork-test registry rows, lock annotation on setTimingInterval). Deferring rather than approving because the paths are CODEOWNER-owned (Source/JavaScriptCore and JSTests), the change is large and concurrency-heavy in Heap.cpp and the profiler thread, and the author states the Windows timer code was type-checked but not run.
Still open from earlier reviews (1):
- Unresolved: 1 minor or pre-existing.
Co-authored-by: dylanc <[email protected]>
Co-authored-by: dylanc <[email protected]>
That build contains the WebKit commit that main pins. Co-authored-by: dylanc <[email protected]>
The sampling profiler dropped every tick outside the VM or with an unwalkable stack, so Bun billed idle, native and GC time to JavaScript functions (oven-sh/bun#44077). On request it now keeps those ticks as
Idle,Program,GarbageCollectororJavaScript. oven-sh/bun#49461 uses it.Bun.serve, ~1,050 in the handlerFor every user of the profiler
setMaximumStackDepth()and--samplingProfilerMaxStackDepth(bun:jsc profile(): no way to cap the sampled stack depth, so a deep stack grows the engine's frames without limit bun#48102).Downsides
Independent of #804. Conflicts with #579.
Testing
Linux x64.
JSTests/stress, release: 93,140 runs, one failure that is as frequent onmain. New tests: 5,850 runs, partly under load, none failed. All but two of 38 mutants die; the subtraction is only measured. Bunmainpassed its profiler tests unchanged.Windows and macOS (x64, arm64): Bun's profiler tests pass in its CI on the preview build.
Generated by Claude Code