Repository navigation
D3D12-REAPER-BOUNDED-QUEUE: bound the reaper's line (LEONIDA_REAPER_LIMIT) - #2826
Conversation
The reaper thread's channel had no bound and nothing slowed its senders. On an RTX 3090 its line grew to hundreds of thousands of things in most runs; the landed host lists waiting there kept their scratch chunks, and the runs that were not stopped ended in a device loss as private memory neared Windows' commit limit, while LEONIDA_REAPER=0 kept memory flat. The lists' scratch was in card memory; it is in PC memory by default since ProjectLeonida#2819, where the line should stay short. This bound is the safety net for LEONIDA_SCRATCH_CARD=1 and any other cause of a backlog. Once the line (REAPING) holds LEONIDA_REAPER_LIMIT things (4096 by default; 0: no bound, as before; LEONIDA_AB can turn it on and off), what the host is done with is let go of by the thread done with it, as LEONIDA_REAPER=0 does. land(), bury() and fronts_let_go() go through one helper (reap), which counts each batch as the reaper thread takes it off; the check and the count are one atomic step (hand_over), so the line holds fewer than the bound and one batch however many threads send. A batch comes off the line also if letting it go panics. The first time the bound takes hold is logged once; the census memory line counts what was let go in place (REAPED_IN_PLACE) and says the bound. Diagnostics only: host lists and preludes made per frame by the title's threads, the submitting thread and the others, with their time; a SPLIT slot for preludes made and recorded; HostList::reset split into what the list held let go and the allocator and list resets. Claim: D3D12-REAPER-BOUNDED-QUEUE Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
host.rs is past the size the policy allows (cargo test -p xtask), and the reaper's tests were some 15 KB of it. They move as they are into host/reaper_tests.rs, a child module of host like its other files; they reach host's private items through super, as before. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
d3d12/docs/switches.md regenerated with tools/d3d12_switches.py. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Update (head 247e27d):
Since this was opened, |
|
Measured on the RTX 3090 (24 GB) with |
KelboLive
left a comment
There was a problem hiding this comment.
The bound is sound and the hazards I came for are closed. hand_over does the check and the count in one fetch_update and never blocks (host.rs:869-871); on a full line the batch comes back to the caller and reap_through (:791-803) frees it with the same recycle/drop the reaper would use, so what is owned by exactly one side — no leak, no double free, no producer blocking inside a lock the reaper needs. Off (:833-845) takes a batch off the line even while unwinding, so a panicking reaper cannot leave the count stuck. limit = 0, LEONIDA_REAPER=0 and LEONIDA_AB all read as documented, and shutdown is unchanged from main (nothing drains). Mutation check: held < limit -> held <= limit fails both a_full_reaper_line_sends_the_rest_back_to_be_let_go_in_place ("holds 105, over 98 and a batch of 7") and threads_together_never_overfill_the_reaper_line, so the bound is genuinely pinned.
Two test problems worth fixing before merge:
LEONIDA_REAPER_LIMIT=1turns the suite red.lists_fronts_go_to_the_reaper_or_herewaits forREAPING + 5 < limit(host/reaper_tests.rs:206), impossible below 6: alone with the switch at 1 it spins the 30 s timeout and fails ("the reaper's line stayed at 0 of 1",:211). The whole-module run passed only becausethe_reaper_bound_comes_from_its_variablehad removed the variable first — with--test-threads=1the same command fails (10 passed, 1 failed, 30 s). Make it skip, or use a batch smaller than the limit, whenlimit < 5.- That same test mutates the process environment (
set_var/remove_varonLEONIDA_REAPER_LIMIT,:292-294) while other tests read it andreaper_limit()caches in aOnceLock;set_varis unsafe with concurrent readers, and it deletes a value the developer or CI may have set.the_reaper_line_is_bounded_unless_asked_not_to_bealready pinsreaper_limit_from, so drop it or restore the old value under a serial guard.
Minor: the two real-thread tests skip by returning after an eprintln (:140, :199), so with LEONIDA_REAPER=0 the run says "11 passed" while nothing of the bound was tested — an #[ignore] or a "SKIP:" line would be honest.
|
Independent Windows check of
GPUs tested: n/a for the run itself (unit tests). AI-assisted: yes. |
|
Verified on Windows, RTX 4070 Laptop 8 GB (not the author). AI-assisted (Claude Code agent Head
Blocking for a merge now: GitHub shows the head as CONFLICTING with current |
d3d12/docs/switches.md regenerated with tools/d3d12_switches.py. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Head a6f835e: Fresh run against
Verified independently on an RTX 4070 Laptop by OreoFR (tests, mutations, 330 s walking, an in-run A/B of |
|
I checked which thread releases actual D3D12 buffers at
The queued counter returned to zero in all four runs. I verified the actual DXGI vendor IDs on AMD Raphael (RADV 26.3.1) and RTX 4070 SUPER (620.73.04), using Wine 11.22 and VKD3D-Proton 3.1.0. All 12 stock reaper tests also passed on each card. The regenerated switch catalog passes |
|
Verified by OreoFR on Windows 11, RTX 4070 Laptop 8 GB, NVIDIA 621.40, Core Ultra 7 155H. Not the author. AI-assisted (Claude Code agent Head
CI is green on the head. Ready to merge once |
d3d12/docs/switches.md regenerated with tools/d3d12_switches.py. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
d3d12/docs/switches.md regenerated with tools/d3d12_switches.py. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
d3d12/docs/switches.md regenerated with tools/d3d12_switches.py. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Merging. Head |
Claimed in the coordination issue as: D3D12-REAPER-BOUNDED-QUEUE (#2570 (comment))
What and why
The reaper thread (
d3d12/crates/leonida-gpu/src/host.rs,reaper) lets go of what the host is done with: landed host lists (reset and pooled), retired objects and lists' fronts. Its channel had no bound and nothing slowed its senders.On an RTX 3090 24 GB (NVIDIA 621.40, i9-13900K, 600-900 s runs into the world, 2026-11-30) its line (
REAPING, the census "waiting for the dropping thread" count since #2751) passed 100,000 in 18 runs of 20 and grew to about 250,000-600,000. The landed lists waiting there kept their scratch chunks (16.1-16.6 GB mapped at the device losses), the pool of reset lists stayed empty so new host lists were made (2.0-2.2 ms per host list), and the runs that were not stopped ended in a device loss as private memory neared Windows' commit limit. The cause was the lists' scratch in card memory: since #2819 it is in PC memory by default, and D3D12-SCRATCH-SPARE-PER-SIZE works on the scratch pool. With scratch in PC memory (run SC-1) the line stayed at 0 and nothing was let go in place. This pull request is the safety net: by default the bound should not take hold; it protectsLEONIDA_SCRATCH_CARD=1and any other cause of a backlog.LEONIDA_REAPER_LIMIT=<things>, 4096 by default;0: no bound, the mpsc channel as onmain;LEONIDA_ABcan turn it on and off): once the line holds that many things, nothing more is handed to the reaper thread. The check and the count are one atomic step (hand_over), so the line holds fewer than the bound plus one batch, however many threads send.land(),bury()andfronts_let_go()go through one helper (reap), which counts each batch as the reaper thread takes it off (Reaped::count).LEONIDA_REAPER=0: landed lists are recycled in place by the thread that callsland()(the submitting thread or the late-wait helper), retired objects and lists' fronts are dropped in place. Cost measured in run TL-1: 20.3 resets per frame in place on the submitting thread, 8.3 ms.Off; it covers the batch under way only).diagnosticsfeature only, added lines behindcfg): host lists and preludes made per frame by the title's threads, the submitting thread and the others, with their time; aSPLITslot for preludes made and recorded;HostList::resetsplit in two parts (what the list held let go; allocator and list reset). The census memory line counts what was let go in place (REAPED_IN_PLACE, which mixes objects, lists and fronts from every thread) and states the bound. The first time the bound takes hold is logged once (all builds).Below the bound nothing changes, and no lifetime changes beyond moving a release to the calling thread, which
LEONIDA_REAPER=0already does. Thread rules checked against Microsoft Learn (via context7): a command allocator is reset only when the GPU no longer runs its lists, and is not free-threaded ("Recording command lists and bundles"); a command list is not free-threaded ("Design philosophy of command queues and command lists"); a resource is freed only after the lists that reference it have run ("Differences in the binding model from Direct3D 11"). Lists land only after their flight has run plusbury_lag, each landed list is owned by oneVecat a time, and retired objects are due before they are handed over, as withLEONIDA_REAPER=0.A/B (MEASURED, RTX 3090, before #2819 and before the last rebase)
Run on exe 54dc320, which has the same
hand_overand the same in-place paths as this branch (the later commits add tests, the panic guard and diagnostics):The solid result is memory, all or nothing. No claim on frame time: the unbounded arm stopped early and there are two pairs only (run RL-1, with the bound: mean +10.8 ms, p90 +8.5 ms, median equal). With
LEONIDA_REAPER=0memory stayed flat in 4 runs of 4, but one of them (B-1) ended in a title crash (0xC0000005), the same seen onmainwithout a backlog (PA-2). A Windows stop 0x10E was seen during a backlog run, not shown to be caused by it.The wiring in
land()andbury()in the real submission flow is checked by the in-game run below, not by the unit tests (fronts_let_go(), theLEONIDA_REAPER=0path ofreapand the reaper thread's own loop are covered by unit tests).Checked by the in-game run of this branch's exe
Confirmation run (MEASURED, n=1, not a timed run: CPU busy with other jobs) of exe cc577f26e (this branch before its rebase on
main023043b; the rebase changed only a comment and the generated switches page in this pull request's files) on the RTX 3090 withLEONIDA_SCRATCH_CARD=1to force the backlog, 900 s, matching runs B-1 and B-2 of the A/B:REAPING) at most 5,811 (the bound plus 1,715, one batch), above 4,096 in 42 of 172 census lines; no value above 1e15, so no underflow. The one-time log line came at 78.7 s, with the line at 5,197.land()works in the real flow.Resetcalls: the cost is in what the lists hold (their scratch), not in the D3D12 resets.No frame-rate figure is offered as a gain.
Related work
retire_resourcecuts retired resources per frame (~53k to ~100) and the reaper's drop time; complementary, no pull request found.Out of scope, possible follow-ups
bury_lagis 64 submissions with more than 32 GiB of RAM and 16 otherwise. The fence alone already makes an object safe at lag 0; the lag is a margin added after the device losses of 2026-11-25 (page faults in memory nothing named any more) and for lists recorded and not yet sent. Lowering it is a safety question to measure, not a fence rule. The bound is independent of it: lagged items are not yet due and are not inREAPING.LEONIDA_REAPER_FAST_CORES) was built as a separate A/B arm.Tested
All on the RTX 3090 machine,
cargo test -p leonida-gpu --lockedforx86_64-pc-windows-msvc(Rust 1.98.1,CARGO_INCREMENTAL=0), branch 200a55e againstmain023043b:--features diagnosticsmain 023043b does not build its leonida-gpu tests (indirect/asbench.rs missing from d87ada3); both sides were tested with an empty untracked asbench.rs; its CI failure in test_tool_stdout_encoding (tools/make_amplify_dxil.py) is on main too.
New tests in
host::reaper_tests(11, and 12 withdiagnostics):a_full_reaper_line_sends_the_rest_back_to_be_let_go_in_place(bounds 98 and 100, batches of 7),a_reaper_that_keeps_up_gets_every_batch,threads_together_never_overfill_the_reaper_line(8 threads, 2,000 rounds),with_the_reaper_thread_held_the_host_lets_go_in_place(the host's ownreap, with a test-only hold on the reaper thread),lists_fronts_go_to_the_reaper_or_here,without_a_reaper_thread_the_host_lets_go_in_place,the_reaper_thread_lets_go_and_takes_off_its_line,a_batch_comes_off_the_line_even_if_letting_it_go_panics,the_reaper_line_is_bounded_unless_asked_not_to_be,the_reaper_bound_comes_from_its_variable,the_reaper_bound_in_an_ab_phase, and withdiagnosticshost_objects_are_counted_by_who_made_them. The two tests that use the real reaper thread skip themselves whenLEONIDA_REAPER=0orLEONIDA_REAPER_LIMIT=0.hand_overthat copiesmain's behaviour ("the reaper's line holds 112, over 100 and a batch of 7"; "the reaper's line has a bound by default"); onmainitself they do not compile (nohand_over). The panic guard's tests fail against a guard that does nothing (line 10 instead of 6; the line still over the bound after the thread let go of everything), and the per-thread count against a count that is not kept.<=for<; in-place count dropped; check then add instead of one atomic step (3 runs of 3, killed by the 8-thread test); A/B "off" keeping the bound; variable ignored; default 0;reappassing no bound;LEONIDA_REAPER=0batch not given back;fronts_let_goskippingreap; panic guard doing nothing; per-thread count not kept; reaper loop counting 0;reaphanding 0.cargo clippy -p leonida-gpu --all-targets(with and withoutdiagnostics): the same warnings asmain, none new.d3d12/docs/switches.md: regenerated withtools/d3d12_switches.py --out;--checkis clean. The only change againstmainis the new row and the count.python -I scripts/check_no_game_data.pyon every changed file and on this text: clean.host::refresh_tests::only_what_differs_goes_upfailed onmaind920d85 during this work (unrelated to the reaper); it passes on 90c5144 and 023043b.GPUs tested: NVIDIA GeForce RTX 3090 24 GB (desktop), driver 621.40, Windows 11 Pro 26300 (unit tests; profile L). In-game (both before the last rebase): the A/B above (exe 54dc320) and the confirmation run of this branch's exe (cc577f26e).
AI-assisted: yes (Claude Code, Claude Opus 5.5)
Checklist
main(023043b) and merges cleanly.docs/design.md(n/a).🤖 Generated with Claude Code