D3D12-REAPER-BOUNDED-QUEUE: bound the reaper's line (LEONIDA_REAPER_LIMIT) #2826
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2826"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from ubiquitarianism/leonida. Originally posted by enlight3d on 2026-12-01T03:48:12Z.
Claimed in the coordination issue as: D3D12-REAPER-BOUNDED-QUEUE (https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7293629499)
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 54dc3209f, 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
main023043bfe; 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), branch200a55e29againstmain023043bfe:--features diagnosticsmain
023043bfedoes not build its leonida-gpu tests (indirect/asbench.rs missing fromd87ada391); 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 onmaind920d850dduring this work (unrelated to the reaper); it passes on90c514437and023043bfe.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 54dc3209f) and the confirmation run of this branch's exe (cc577f26e).
AI-assisted: yes (Claude Code, Claude Opus 5.5)
Checklist
main(023043bfe) and merges cleanly.docs/design.md(n/a).🤖 Generated with Claude Code
Imported from ubiquitarianism/leonida. Originally posted by enlight3d on 2026-12-01T04:27:32Z.
Update (head
247e27d91):caab7fe29moves thereaper_testsmodule, unchanged, intod3d12/crates/leonida-gpu/src/host/reaper_tests.rs, so this pull request adds less tohost.rs, which is already over the policy's size limit onmain(it now adds about 10 KB instead of 25 KB).247e27d91mergesmain86c30fcea(with #2825 and #2827);d3d12/docs/switches.mdregenerated withtools/d3d12_switches.py,--checkclean.main86c30fcea, no stand-in file any more:cargo test -p leonida-gpu --locked601 passed / 0 failed against 590 / 0 onmain; with--features diagnostics682 / 0 against 670 / 0.cargo clippy -p leonida-gpu --all-targets, with and withoutdiagnostics: the same warnings asmain, none new.Since this was opened,
mainturned lists' scratch in the card's memory back on for cards with 9 GiB or more (e22a5ea7a,9d231d373: a budget-sized spare that lets the oldest go, and a guard that sends new chunks to the PC's memory after 1,000 made in 10 s). The bound stays the safety net it was meant to be. A run of that new default on the RTX 3090 (24 GB) is under way; I will add what it shows for the reaper's line.Imported from ubiquitarianism/leonida. Originally posted by enlight3d on 2026-12-01T04:57:02Z.
Measured on the RTX 3090 (24 GB) with
main9d231d373's new default (lists' scratch in the card's memory again, with the budget-sized spare), without this pull request: 601 s standing, the churn guard never tripped, card chunks made 0 a second after the first 30 s, the reaper's line at most 229, private memory flat at most 30.5 GB. So by default the bound (4096) does not take hold on this card; it stays the safety net for a backlog (a card where the spare is not enough before the guard trips,LEONIDA_SPARE_CARD_GROW=0, or any other cause). Details: the RUN in #2570.Imported from ubiquitarianism/leonida. Originally posted by KelboLive on 2026-12-01T05:01:02Z.
Review: COMMENTED
The bound is sound and the hazards I came for are closed.
hand_overdoes the check and the count in onefetch_updateand never blocks (host.rs:869-871); on a full line the batch comes back to the caller andreap_through(:791-803) frees it with the samerecycle/drop the reaper would use, sowhatis 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=0andLEONIDA_ABall read as documented, and shutdown is unchanged from main (nothing drains). Mutation check:held < limit->held <= limitfails botha_full_reaper_line_sends_the_rest_back_to_be_let_go_in_place("holds 105, over 98 and a batch of 7") andthreads_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.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 aneprintln(:140,:199), so withLEONIDA_REAPER=0the run says "11 passed" while nothing of the bound was tested — an#[ignore]or a "SKIP:" line would be honest.Imported from ubiquitarianism/leonida. Originally posted by Kal0s-design on 2026-12-01T06:10:37Z.
Independent Windows check of
247e27d91on currentmain640b0d0df(no conflict, mergecb931f16d). Native Windows 11, MSVC x64, Rust 1.98.1, unit tests only (no title run).cargo test -p leonida-gpu --lib, three runs: 610 passed, 0 failed, 54 ignored each time.mainlists 653 tests, the merge 664, so the 11host::reaper_testsare the only addition.reapertests on their own in the release build 300 times with 16 test threads, includingthreads_together_never_overfill_the_reaper_line,a_full_reaper_line_sends_the_rest_back_to_be_let_go_in_placeanda_batch_comes_off_the_line_even_if_letting_it_go_panics. 0 failures in 300.REAPINGcount the description measures.GPUs tested: n/a for the run itself (unit tests). AI-assisted: yes.
Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-12-01T12:58:39Z.
Verified on Windows, RTX 4070 Laptop 8 GB (not the author). AI-assisted (Claude Code agent
oreofr-agent, for OreoFR). NVIDIA 621.40, Core Ultra 7 155H, Windows 11 Pro, Rust 1.98.1 pinned.Head
247e27d91merged intomain640b0d0df(clean at the time):cargo test --locked -p leonida-gpu610 passed, 0 failed;-p leonida-hle242/0. Re-run with the head's code on the latermainc2dd42174: 638/0 and 243/0 (see the conflict note below).hand_over'sroomnever refusing (Some(held.wrapping_add(handed))), fourhost::reaper_testsfail (a_full_reaper_line_sends_the_rest_back_to_be_let_go_in_place,lists_fronts_go_to_the_reaper_or_here,with_the_reaper_thread_held_the_host_lets_go_in_place,threads_together_never_overfill_the_reaper_line). Reverted, tree clean.-SeriesS -NoDlss -NoFrameGen, walking (keys file), cold shaders: 330 s in the world, 0 device loss, 0 ERROR lines, clean close. Note: the bound did take hold once on this 8 GB card (scratch in the PC's memory) during the warm-up:the reaper thread's line holds 4493 things, LEONIDA_REAPER_LIMIT=4096 or more.LEONIDA_AB=10,LEONIDA_REAPER_LIMIT, standing 600 s, warm pipelines:d3d12_ab_compare.py phases: 28 pairs, no clear difference on the median frame time. So the bound costs nothing measurable here.Blocking for a merge now: GitHub shows the head as CONFLICTING with current
main(ed867d7b2):d3d12/docs/switches.md(generated). The code files merge cleanly. Could you mergemainand regenerateswitches.mdwithtools/d3d12_switches.py? I will re-run the tests on the new head and currentmainbefore asking OreoFR for the merge.Imported from ubiquitarianism/leonida. Originally posted by enlight3d on 2026-12-01T14:04:54Z.
Head
a6f835ef0:maine04721c7fmerged in (merge commit, no rebase),d3d12/docs/switches.mdregenerated withtools/d3d12_switches.py(--checkclean;main's page was behind, so the regenerated page also adds the rowsLEONIDA_CONSTANTS_ONCE,LEONIDA_CONSTANT_SLOT_INLINEandLEONIDA_SUBMIT_SENDERand two L marks, besides this pull request's row).Fresh run against
maine04721c7fon the same machine:cargo test --workspace --exclude xtask --locked --no-fail-fast -- --skip depth_bounds_pipeline_loads_and_filters_draws(the CI's command): 1,622 passed / 1 failed / 74 ignored, against 1,611 / 1 / 74 onmain; the only difference isleonida-gpu641 against 630 (the 11 new tests). The one failure is the same on both sides,leonida-loadertests/load_probe.rs(its probe DLL is not found on this machine).cargo test -p leonida-gpu --features diagnostics: 723 passed / 0 failed against 711 / 0.cargo clippy -p leonida-gpu --all-targets, with and withoutdiagnostics: the same warnings asmain, none new.Verified independently on an RTX 4070 Laptop by OreoFR (tests, mutations, 330 s walking, an in-run A/B of
LEONIDA_REAPER_LIMITwithout cost; the bound took hold once there during warm-up, at a line of 4,493).Imported from ubiquitarianism/leonida. Originally posted by merzleckeier on 2026-12-01T14:27:55Z.
I checked which thread releases actual D3D12 buffers at
a6f835ef0merged ontob3910e5e. I held the reaper with the existing test lock, then handed the productionreapfunction two separate one-object batches. Each owned a real 64 KiB D3D12 buffer.LEONIDA_REAPER_LIMIT=0: both releases waited for the lock and ran onleonida-reaperin order.LEONIDA_REAPER_LIMIT=1: the first release waited for the reaper. The second buffer was released by the caller while the reaper was still held, and the in-place counter increased by one.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
--checkat the merged tree. This verifies release and accounting at the queue boundary, without game runs or frame-time measurements. MEASURED, AI-assisted (Codex).Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-12-01T15:28:34Z.
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
oreofr-agent, for OreoFR).Head
a6f835ef0merged intomainedf7d01f8(tree73be2a8bf, merge commit, no conflict at the time). Main has since moved to0ff8a532f, and the generatedd3d12/docs/switches.mdconflicts again (the only conflicting file).cargo test --locked -p leonida-gpu642 passed / 0 failed;-p leonida-hle242 / 0.0e9750943= head + maincd0c52fcf;host.rsis the same onedf7d01f8):hand_over's room made to never refuse (|held| Some(held + handed)) → 4 reaper tests FAIL (a_full_reaper_line_sends_the_rest_back_to_be_let_go_in_place,lists_fronts_go_to_the_reaper_or_here,with_the_reaper_thread_held_the_host_lets_go_in_place,threads_together_never_overfill_the_reaper_line); restored, tree clean.247e27d91, which I verified earlier, except for the regeneratedswitches.md.-SeriesS, no DLSS, BAR 0, texture budget 1024): 0 device lost, 0 ERROR; the bound took hold (87 let go in place).LEONIDA_AB=10,LEONIDA_REAPER_LIMIT, 600 s walking: median frame −0.016 ± 0.209 ms (25 pairs), "no clear difference"; card busy/idle and submitting-thread time unchanged per arm.target\diagfolder was just as slow (38.7 ms against 35.6 ms from its usual folder), with the card idle 3.5 ms instead of 1.0. So the gap follows the exe's folder, most likely the driver's per-path shader cache, and not this PR. Compare exes from the same folder, or useLEONIDA_AB.CI is green on the head. Ready to merge once
switches.mdis regenerated on the current main; I will re-run the tests and the 300 s gate on the new head before merging.Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-12-01T18:37:10Z.
Merging. Head
597e2d922merges cleanly with mainea6caf88d.Claim: D3D12-REAPER-BOUNDED-QUEUE, enlight3d's own (comment in #2570).
Red/green: by oreofr-agent (not the author), RTX 4070 Laptop, Windows 11: red by mutation (the bound made to never refuse: 4 reaper tests fail), green
cargo test --locked -p leonida-gpu644/0 on this head merged with main27613ab6b. Also checked by TheGeogeo's agent (RTX 3080, ABBA) and Kal0s-design.In-game run: RTX 4070 Laptop 8 GB, NVIDIA 621.40, this head merged with
27613ab6b: 330 s in the world walking, 0 device loss, 0 ERROR lines. A/B of the bound in one binary (600 s walking): -0.016 +- 0.209 ms, no clear difference. Since27613ab6b, main gained #952, #2913 (CI config) and #726 (an audio export tool): nothing that runs in the game.CI: green on the head (6/6); main's CI green on
7fbdf8511. Merged while main's CI onea6caf88d(#726, tools only) is still queued, on OreoFR's explicit instruction: no conflict, and the two touch separate areas.AI-assisted: yes, Claude Code agent
oreofr-agent, merged on OreoFR's (maintainer) written OK.