Repository navigation
Conversation
…ts sweeps of parked threads `mi_on_thread_idle_start` wakes the scavenger with a syscall whenever no wake is pending, and the scavenger takes a turn of its loop for each. How often that happens could only be seen with a syscall tracer. Three counters in the sub-process, read through `mi_purge_holes_stats_get` and printed with the statistics: - `park_wakes`: wake syscalls that a park issued, on the thread that parked. - `scavenger_turns`: turns of the loop of the scavenger. - `parked_sweeps`: sweeps of a parked thread by the scavenger. `_mi_scavenger_wake` returns whether it issued the syscall, so that only the wakes of parks are counted. Nothing reads the counters to decide anything. A debug build has a hook that holds the scavenger between its walk of the parked threads and its wait (`mi_debug_stall_in_scavenger_wait`), and says for how long it is going to wait (`mi_debug_scavenger_wait_msecs`). test-park-handoff parks there: the scavenger clears its wake word before the walk, so the wake of that park is pending when the wait begins and the park is swept. With the clear moved behind the walk the test fails 3 times of 3.
robobun
added a commit
to oven-sh/bun
that referenced
this pull request
Sep 30, 2026
…its schedule The event loop hands the heaps of its thread to mimalloc's scavenger around every poll (us_loop_run_bun_tick). Each hand-off woke the scavenger: a futex wake on the loop thread and a turn of the scavenger, which found that the thread was not due. mimalloc paced the sweeps of a thread (100 ms) and not its wakes. Bump mimalloc to oven-sh/mimalloc#57 (which contains oven-sh/mimalloc#56). The scavenger puts a thread on its schedule when it passes a park of that thread over inside its window, and a park of a thread on the schedule issues no wake. The call in the event loop does not change. - packages/bun-usockets: the comment at the hand-off said that it costs a compare-and-swap per tick. It says what it costs now, and why a tick whose poll cannot block hands off too. - src/mimalloc_sys: the same for the doc comment of mi_on_thread_idle_start. - test/js/bun/jsc/heapStats-mimalloc.test.ts: 500 turns of a 1 ms timer put the scavenger thread to sleep 14 to 32 times. It was once for each turn. - bench/snippets/event-loop-turn.mjs: CPU time and sleeps of the threads for one turn of the loop, by the kind of turn. The pin also carries oven-sh/mimalloc#34, which is on bun-dev3-v2.
robobun
added a commit
to oven-sh/bun
that referenced
this pull request
Sep 30, 2026
…its schedule The event loop hands the heaps of its thread to mimalloc's scavenger around every poll (us_loop_run_bun_tick). Each hand-off woke the scavenger: a futex wake on the loop thread and a turn of the scavenger, which found that the thread was not due. mimalloc paced the sweeps of a thread (100 ms) and not its wakes. Bump mimalloc to oven-sh/mimalloc#57 (which contains oven-sh/mimalloc#56). The scavenger puts a thread on its schedule when it passes a park of that thread over inside its window, and a park of a thread on the schedule issues no wake. The call in the event loop does not change. - packages/bun-usockets: the comment at the hand-off said that it costs a compare-and-swap per tick. It says what it costs now, and why a tick whose poll cannot block hands off too. - src/mimalloc_sys: the same for the doc comment of mi_on_thread_idle_start. - test/js/bun/jsc/heapStats-mimalloc.test.ts: 500 turns of a 1 ms timer put the scavenger thread to sleep 15 to 26 times. It was once for each turn. - bench/snippets/event-loop-turn.mjs: CPU time and sleeps of the threads for one turn of the loop, by the kind of turn. The pin also carries oven-sh/mimalloc#34, which is on bun-dev3-v2.
robobun
added a commit
to oven-sh/bun
that referenced
this pull request
Oct 2, 2026
…its schedule The event loop hands the heaps of its thread to mimalloc's scavenger around every poll (us_loop_run_bun_tick). Each hand-off woke the scavenger: a futex wake on the loop thread and a turn of the scavenger, which found that the thread was not due. mimalloc paced the sweeps of a thread (100 ms) and not its wakes. Bump mimalloc to oven-sh/mimalloc#57 (which contains oven-sh/mimalloc#56). The scavenger puts a thread on its schedule when it passes a park of that thread over inside its window, and a park of a thread on the schedule issues no wake. The call in the event loop does not change. - packages/bun-usockets: the comment at the hand-off said that it costs a compare-and-swap per tick. It says what it costs now, and why a tick whose poll cannot block hands off too. - src/mimalloc_sys: the same for the doc comment of mi_on_thread_idle_start. - test/js/bun/jsc/heapStats-mimalloc.test.ts: 500 turns of a 1 ms timer put the scavenger thread to sleep 15 to 26 times. It was once for each turn. - bench/snippets/event-loop-turn.mjs: CPU time and sleeps of the threads for one turn of the loop, by the kind of turn. The pin also carries oven-sh/mimalloc#34, which is on bun-dev3-v2.
robobun
added a commit
to oven-sh/bun
that referenced
this pull request
Oct 2, 2026
…its schedule The event loop hands the heaps of its thread to mimalloc's scavenger around every poll (us_loop_run_bun_tick). Each hand-off woke the scavenger: a futex wake on the loop thread and a turn of the scavenger, which found that the thread was not due. mimalloc paced the sweeps of a thread (100 ms) and not its wakes. Bump mimalloc to oven-sh/mimalloc#57 (which contains oven-sh/mimalloc#56). The scavenger puts a thread on its schedule when it passes a park of that thread over inside its window, and a park of a thread on the schedule issues no wake. The call in the event loop does not change. - packages/bun-usockets: the comment at the hand-off said that it costs a compare-and-swap per tick. It says what it costs now, and why a tick whose poll cannot block hands off too. - test/js/bun/jsc/heapStats-mimalloc.test.ts: 500 turns of a 1 ms timer put the scavenger thread to sleep 15 to 26 times. It was once for each turn. - bench/snippets/event-loop-turn.mjs: CPU time and sleeps of the threads for one turn of the loop, by the kind of turn. The pin also carries oven-sh/mimalloc#34, which is on bun-dev3-v2.
Collaborator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Behaviour change: none
Problem
mi_on_thread_idle_startwakes the scavenger with a syscall, and the scavenger takes a turn of its loop for each wake. Nothing counts either. Only a syscall tracer shows them.Fix
mi_subproc_t:park_wakes,scavenger_turns,parked_sweeps.mi_purge_holes_stats_getreturns them._mi_scavenger_wakereturns whether it issued the syscall, so only the wakes of parks are counted.mi_debug_stall_in_scavenger_wait. It holds the scavenger between its walk and its wait.test-park-handoff(5 new checks). All 35 release and 36 debug tests pass, built as C and as C++.Background
mi_on_thread_idle_startandmi_on_thread_idle_end. The thread does not allocate then, so the scavenger can sweep its heaps.subproc->scavenger_wake. It clears that word before it walks the parked threads. So the wake of a later park is still pending when the wait starts.Downsides
mi_purge_holes_stats_tgrows by 24 bytes. A caller built with the old header passes a struct that is too small. bun has no caller.Notes
Functions whose size changes in the release library (clang, x64, bytes):
_mi_scavenger_wake76 to 83,_mi_theap_sweep_parked727 to 735,mi_on_thread_idle_start170 to 188,mi_scavenger_thread_main498 to 514,mi_purge_holes_stats_get395 to 447,mi_subproc_stats_print_out372 to 421. No other function changes. The hook compiles to nothing withoutMI_DEBUG.The statistics.
mi_stats_printprints the three counters on one line when the scavenger took a turn.The debug test and its mutant. The test holds the scavenger between its walk and its wait, parks, and releases the scavenger. The park must be swept before the deadline. With the clear of the wake word moved behind the walk, the test fails 3 times of 3. A lost wake shows only if nothing else ends the wait of the scavenger. So the test reads
mi_debug_scavenger_wait_msecsfirst (the longest time the wait can last). If a purge is scheduled, it lets it pass and goes again.Not run here. The thread sanitizer build does not start in the container that I used (it cannot turn address space randomization off). The
tsanjob of this repository runs it. The jobs of this repository also cover macOS, Windows and FreeBSD.The counts before the change (release, Linux x64, one thread, 5 runs each):