Skip to content

scavenger: count the wakes of parks, the turns of the scavenger and its sweeps of parked threads - #56

Closed
robobun wants to merge 1 commit into
oven-sh:bun-dev3-v2from
robobun:robobun/12c6d455/park-wake-counters
Closed

robobun wants to merge 1 commit into
oven-sh:bun-dev3-v2from
robobun:robobun/12c6d455/park-wake-counters

Conversation

@robobun

@robobun robobun commented Sep 29, 2026 •

Copy link
Copy Markdown
Collaborator

Behaviour change: none

Problem

  • mi_on_thread_idle_start wakes 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.
  • 1000 parks of 1 ms issue 966 to 1000 wakes for 11 to 13 sweeps. theap: a park wakes the scavenger only while the thread is not on its schedule #57 changes that. Its tests need these counts and a way to hold the scavenger at a known point.

Fix

  • Add three counters to mi_subproc_t: park_wakes, scavenger_turns, parked_sweeps. mi_purge_holes_stats_get returns them.
  • _mi_scavenger_wake returns whether it issued the syscall, so only the wakes of parks are counted.
  • Add the debug hook mi_debug_stall_in_scavenger_wait. It holds the scavenger between its walk and its wait.
  • Verified: test-park-handoff (5 new checks). All 35 release and 36 debug tests pass, built as C and as C++.

Background

  • A park is the time between mi_on_thread_idle_start and mi_on_thread_idle_end. The thread does not allocate then, so the scavenger can sweep its heaps.
  • The scavenger sleeps on the futex word 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

  • A park that issues a wake does one more atomic increment. The scavenger does one for each turn and one for each sweep.
  • mi_purge_holes_stats_t grows by 24 bytes. A caller built with the old header passes a struct that is too small. bun has no caller.
  • The release library grows by 229 bytes of text and 24 bytes of bss.
Notes

Functions whose size changes in the release library (clang, x64, bytes): _mi_scavenger_wake 76 to 83, _mi_theap_sweep_parked 727 to 735, mi_on_thread_idle_start 170 to 188, mi_scavenger_thread_main 498 to 514, mi_purge_holes_stats_get 395 to 447, mi_subproc_stats_print_out 372 to 421. No other function changes. The hook compiles to nothing without MI_DEBUG.

The statistics. mi_stats_print prints 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_msecs first (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 tsan job 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):

parks wakes scavenger turns sweeps
1000 parks of 1 ms 966 to 1000 1000 to 1005 11 to 13
3000 parks of 100 us, 20 us of work between them 2805 to 2998 2805 to 2998 6
400000 parks around a poll that returns at once 155651 to 230008 155651 to 230008 3 to 4

…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.
@Jarred-Sumner

Copy link
Copy Markdown
Collaborator

Closing with #57: these counters were there to measure the wake-ups per park, which #59 takes away (about 1.0 scavenger wake-up per park went to 0.001 for a thread that parks all the time, and stays at 1.0 for a 2 Hz timer).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants