d3d12: host lists record into pooled command allocators (D3D12-COMMAND-ALLOCATOR-RING) #3495

Code

Clone this repository

Checkout

From your project repository, check out a new branch and test the changes.

git fetch -u  "d3d12-command-allocator-ring:Krashgamer-d3d12-command-allocator-ring"
git switch "Krashgamer-d3d12-command-allocator-ring"
Open
leonida/mandy
Krashgamer wants to merge 6 commits from Krashgamer/mandy:d3d12-command-allocator-ring into main

This branch is out-of-date with the base branch

Conversation
Reviewers 0 approvals
  • No reviewers
Details
This pull request is blocked because it's outdated.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u 'd3d12-command-allocator-ring:Krashgamer-d3d12-command-allocator-ring'
git switch 'Krashgamer-d3d12-command-allocator-ring'
First-time contributor

Replaces #3031, which came over from GitHub under the name Ghost, so I can no longer push to it. Same code as #3031 (head 0edc09ac2, the commit the test reports there name). The discussion and test results so far are on #3031.

Claimed in the coordination issue as: D3D12-COMMAND-ALLOCATOR-RING (https://git.leonida.now/leonida/mandy/issues/2570)

What and why

Every host list and every list prelude had a command allocator of its own: some 2,500 in the world (1,474 host lists alive, plus their preludes). On NVIDIA an allocator keeps the memory of its largest recording through Reset, and what an allocator lets go stays in the driver's pool (Windows does not get it back). So the process held the sum of every allocator's peak. That is also why LEONIDA_ALLOCATOR_TRIM=1 measured nothing (findings.md): a replaced allocator's memory went back to the driver, and the new one grew again.

Device test on an RTX 3080 (no game):

Case Private bytes
1,000 allocators, nothing recorded +3.6 MB
1,000 lists created on one allocator +223 MB (0.22 MB each)
999 lists × 20,000 CopyBufferRegion, each on its own allocator +2,629 MB
the same after Reset of every allocator unchanged
releasing every allocator and list −10 MB
one list recorded 999 times on 999 allocators +2,311 MB
999 lists recorded once each on one allocator, reset between +321 MB

The allocators hold the memory.

This change:

  • New lists. Host lists and their preludes are made closed with no allocator (CreateCommandList1).
  • Pooled allocators. Each recording takes an empty allocator from a pool (host/recorders.rs) and has it to itself. When the recording is over (its list was reset after its run, or let go behind the submissions running, host::retire), the allocator is reset outside the pool's lock and goes back to the pool.
  • No list records after another in one allocator before its reset. D3D12 allows that, and the first version of this pull request did it (up to 16 recordings per allocator). With -RayTracing that hung the card (DXGI_ERROR_DEVICE_HUNG) in 5 of 15 runs, against 0 of 10 with an allocator per list. Of those 10, two ran this branch with LEONIDA_ALLOCATOR_RING=0, and eight ran a build of another branch on main whose own switch was unset. With one recording per allocator: 0 of 12 (see Tested). The cause inside the driver was not found; the pool keeps to the simplest contract instead.
  • Idle pools. Pooled idle lists stay closed. An open idle list would keep an allocator to itself.
  • Lists with no recording. A list that a submission names with no live recording of its own gets an empty one first. Without this, a list handed out closed for the title's next reset and submitted again with no reset ran with none, and the debug layer stopped the device: "A Command List created via ID3D12Device::CreateCommandList1 can not be executed before it has been reset … and closed".
  • LEONIDA_ALLOCATOR_RING=0 gives each list and each prelude an allocator of its own again ("home" slots), as before.
  • LEONIDA_ALLOCATOR_TRIM keeps only its idle-list part; the allocator replacement is gone (it did nothing).
  • New log line. The frame-rate line now prints, every twelfth time, how many allocators there are and how they are used.

Tested

Game gate, Series X, 300 s in the world, the same save, RTX 3080 10 GB, 32 GB RAM. Main 5b1fff821 against this change on it, alternating, two runs each. This change was measured at 9903ba336, the commit before the last review fixes listed below; those touch only list retirement, where a released allocator is let go, comments and tests. Columns (private = private bytes at most):

main: private / fps median this change: private / fps median gate
ray tracing off 27,574 and 27,569 MB / 24.5 and 24.1 26,187 and 25,965 MB / 24.7 and 24.2 all PASS
-HardwareRT (one complete run each) 27,926 MB / 21.1 26,967 MB / 21.3 all PASS*

* With -HardwareRT both main and this change stay over the card's memory budget for about 190 of the 300 s; the gate's over-budget stop was turned off for these runs (a first round with it on stopped main and this change alike at ~175 s).

-RayTracing (the title's own structure builder, where the first version hung): 4 gate runs of this head, all PASS, 0 device lost (fps 11.6-15.1, private 30.8-31.4 GB); with 8 runs of an equivalent earlier build, 0 of 12 hung.

Measured by others: OtavMacedo, RTX 3070 8 GB, 16 GB RAM, Series X, 4 pairs of two builds (4e59d46e8 on the same base against the base alone, 12 gate runs, all PASS): private bytes at most -752 MB (-3.0 %, 95 % interval -1,232 to -271), least left to commit +718 MB, frame time no clear difference. They saw longer gaps between frames in the last two runs with the change (up to 4.6 s), not read as caused by it; worth watching near arrival (https://git.leonida.now/leonida/mandy/issues/3031).

Other results:

  • Allocators: about 1,200 pooled; some 400 of them held over 60 s by lists the title keeps recorded and unreset.
  • Releasing a prelude's recording early (tried): no measurable change (31,175 against 31,262 MB with -RayTracing, 4 pairs), so not part of this change.

Without the game:

  • cargo test -p leonida-gpu --features diagnostics --lib on WARP with the debug layer (LEONIDA_D3D12_WARP=1 LEONIDA_D3D12_DEBUG=1): 789 passed.
  • Clippy: no new warnings.
  • check_no_game_data.py: passed.
  • Docs tests: passed.
  • d3d12_switches.py --check: passed.

Red/green for host::batch_scratch_tests::lists_recorded_in_turn_share_allocators. The test records, runs and retires 200 lists in turn, then counts the allocators they took:

  • This change: at most a few allocators, and the test passes.
  • LEONIDA_ALLOCATOR_RING=0, with the test's switch guard taken out: "200 lists recorded in turn took 200 allocators", and the test fails.

lists_record_in_pooled_allocators_and_run checks that recordings alive at once have allocators of their own. The recorder module has unit tests for the bookkeeping:

  • a recording has its allocator to itself until it is over, then the allocator is reset and back in the pool
  • no reset while a recording in it is still open
  • a slot being reset is taken by nobody
  • home slots: two recordings in a row, and going with their owner

The change was reviewed by independent reviewer agents before this pull request and before this update. Findings fixed in the update:

  • HostList::retire closes a list still recording before its allocator can reset (it only asserted in debug builds).
  • An allocator whose owner went is released outside the pool's lock.
  • Tests asserted too little (an always-true check; an order production never takes).

GPUs tested: NVIDIA GeForce RTX 3080 10 GB, driver 32.0.16.1088 (610.88), Windows 11 Pro 26200; Intel i7-10700K, 32 GB RAM

AI-assisted: yes, Claude Code (Claude Opus 5.5)

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a: no translator change)

🤖 Generated with Claude Code

Replaces #3031, which came over from GitHub under the name Ghost, so I can no longer push to it. Same code as #3031 (head `0edc09ac2`, the commit the test reports there name). The discussion and test results so far are on #3031. Claimed in the coordination issue as: D3D12-COMMAND-ALLOCATOR-RING (https://git.leonida.now/leonida/mandy/issues/2570) ### What and why Every host list and every list prelude had a command allocator of its own: some 2,500 in the world (1,474 host lists alive, plus their preludes). On NVIDIA an allocator keeps the memory of its largest recording through `Reset`, and what an allocator lets go stays in the driver's pool (Windows does not get it back). So the process held the sum of every allocator's peak. That is also why `LEONIDA_ALLOCATOR_TRIM=1` measured nothing (findings.md): a replaced allocator's memory went back to the driver, and the new one grew again. Device test on an RTX 3080 (no game): | Case | Private bytes | |---|---| | 1,000 allocators, nothing recorded | +3.6 MB | | 1,000 lists created on one allocator | +223 MB (0.22 MB each) | | 999 lists × 20,000 `CopyBufferRegion`, each on its own allocator | +2,629 MB | | the same after `Reset` of every allocator | unchanged | | releasing every allocator and list | −10 MB | | one list recorded 999 times on 999 allocators | +2,311 MB | | 999 lists recorded once each on one allocator, reset between | +321 MB | The allocators hold the memory. This change: - **New lists.** Host lists and their preludes are made closed with no allocator (`CreateCommandList1`). - **Pooled allocators.** Each recording takes an empty allocator from a pool (`host/recorders.rs`) and has it to itself. When the recording is over (its list was reset after its run, or let go behind the submissions running, `host::retire`), the allocator is reset outside the pool's lock and goes back to the pool. - **No list records after another in one allocator before its reset.** D3D12 allows that, and the first version of this pull request did it (up to 16 recordings per allocator). With `-RayTracing` that hung the card (`DXGI_ERROR_DEVICE_HUNG`) in 5 of 15 runs, against 0 of 10 with an allocator per list. Of those 10, two ran this branch with `LEONIDA_ALLOCATOR_RING=0`, and eight ran a build of another branch on main whose own switch was unset. With one recording per allocator: 0 of 12 (see Tested). The cause inside the driver was not found; the pool keeps to the simplest contract instead. - **Idle pools.** Pooled idle lists stay closed. An open idle list would keep an allocator to itself. - **Lists with no recording.** A list that a submission names with no live recording of its own gets an empty one first. Without this, a list handed out closed for the title's next reset and submitted again with no reset ran with none, and the debug layer stopped the device: "A Command List created via ID3D12Device::CreateCommandList1 can not be executed before it has been reset … and closed". - **`LEONIDA_ALLOCATOR_RING=0`** gives each list and each prelude an allocator of its own again ("home" slots), as before. - **`LEONIDA_ALLOCATOR_TRIM`** keeps only its idle-list part; the allocator replacement is gone (it did nothing). - **New log line.** The frame-rate line now prints, every twelfth time, how many allocators there are and how they are used. ### Tested Game gate, Series X, 300 s in the world, the same save, RTX 3080 10 GB, 32 GB RAM. Main `5b1fff821` against this change on it, alternating, two runs each. This change was measured at `9903ba336`, the commit before the last review fixes listed below; those touch only list retirement, where a released allocator is let go, comments and tests. Columns (`private` = private bytes at most): | | main: private / fps median | this change: private / fps median | gate | |---|---|---|---| | ray tracing off | 27,574 and 27,569 MB / 24.5 and 24.1 | 26,187 and 25,965 MB / 24.7 and 24.2 | all PASS | | `-HardwareRT` (one complete run each) | 27,926 MB / 21.1 | 26,967 MB / 21.3 | all PASS* | \* With `-HardwareRT` both main and this change stay over the card's memory budget for about 190 of the 300 s; the gate's over-budget stop was turned off for these runs (a first round with it on stopped main and this change alike at ~175 s). `-RayTracing` (the title's own structure builder, where the first version hung): 4 gate runs of this head, all PASS, 0 device lost (fps 11.6-15.1, private 30.8-31.4 GB); with 8 runs of an equivalent earlier build, 0 of 12 hung. Measured by others: OtavMacedo, RTX 3070 8 GB, 16 GB RAM, Series X, 4 pairs of two builds (`4e59d46e8` on the same base against the base alone, 12 gate runs, all PASS): private bytes at most -752 MB (-3.0 %, 95 % interval -1,232 to -271), least left to commit +718 MB, frame time no clear difference. They saw longer gaps between frames in the last two runs with the change (up to 4.6 s), not read as caused by it; worth watching near arrival (https://git.leonida.now/leonida/mandy/issues/3031). Other results: - **Allocators:** about 1,200 pooled; some 400 of them held over 60 s by lists the title keeps recorded and unreset. - **Releasing a prelude's recording early** (tried): no measurable change (31,175 against 31,262 MB with `-RayTracing`, 4 pairs), so not part of this change. Without the game: - `cargo test -p leonida-gpu --features diagnostics --lib` on WARP with the debug layer (`LEONIDA_D3D12_WARP=1 LEONIDA_D3D12_DEBUG=1`): 789 passed. - Clippy: no new warnings. - `check_no_game_data.py`: passed. - Docs tests: passed. - `d3d12_switches.py --check`: passed. Red/green for `host::batch_scratch_tests::lists_recorded_in_turn_share_allocators`. The test records, runs and retires 200 lists in turn, then counts the allocators they took: - **This change:** at most a few allocators, and the test passes. - **`LEONIDA_ALLOCATOR_RING=0`, with the test's switch guard taken out:** "200 lists recorded in turn took 200 allocators", and the test fails. `lists_record_in_pooled_allocators_and_run` checks that recordings alive at once have allocators of their own. The recorder module has unit tests for the bookkeeping: - a recording has its allocator to itself until it is over, then the allocator is reset and back in the pool - no reset while a recording in it is still open - a slot being reset is taken by nobody - home slots: two recordings in a row, and going with their owner The change was reviewed by independent reviewer agents before this pull request and before this update. Findings fixed in the update: - `HostList::retire` closes a list still recording before its allocator can reset (it only asserted in debug builds). - An allocator whose owner went is released outside the pool's lock. - Tests asserted too little (an always-true check; an order production never takes). GPUs tested: NVIDIA GeForce RTX 3080 10 GB, driver 32.0.16.1088 (610.88), Windows 11 Pro 26200; Intel i7-10700K, 32 GB RAM AI-assisted: yes, Claude Code (Claude Opus 5.5) ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [x] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. (n/a: no translator change) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Every host list and every prelude had a command allocator of its own,
some 2,500 in the world. On this driver an allocator keeps the memory of
its largest recording through Reset, and what an allocator lets go stays
with the driver, so the process held the sum of every allocator's peak
(and LEONIDA_ALLOCATOR_TRIM, which replaced old allocators, moved nothing).

Lists are now made closed with no allocator (CreateCommandList1) and each
recording takes one from a shared set (host/recorders.rs): one list records
into an allocator at a time, closed recordings are appended (up to 16), and
an allocator is reset once every recording in it is over, i.e. its list was
reset after its run or let go. Idle pooled lists stay closed. A list named
by a submission with no recording of its own gets an empty one first.
LEONIDA_ALLOCATOR_RING=0 gives each list and prelude an allocator of its own
again. LEONIDA_ALLOCATOR_TRIM keeps only its idle-list part.

RTX 3080 10 GB, Series X, game gate 300 s in the world: private bytes at
most 27,665 MB -> 25,472 / 25,549 MB, lowest left to commit +2 GB, 134
shared allocators; frame times unchanged; D3D12 debug layer silent over
120 s in the world.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
lists_recorded_in_turn_share_allocators copied a->b and b->a in one list. A
buffer first read in a list is promoted to the copy-source state for the rest
of it, so the copy back was an error under the debug layer, and its message
went to the device's queue that other tests read: with LEONIDA_D3D12_DEBUG=1
and the whole lib in parallel, seven indirect:: and host::card_steps tests
failed. Copies now go one way; the allocator count is what the test checks.

WARP + debug layer, whole lib in parallel: 745 passed twice (7 failed before);
card + debug layer: 745 passed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With ray tracing on (-RayTracing, RTX 3080), lists recording one after
another into one allocator before its reset left the card hung
(DXGI_ERROR_DEVICE_HUNG) in about one run in three (5 of 15). The
allocators stay pooled, but each recording now has one to itself until it
is over and reset: 0 of 8 runs hung.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
First-time contributor

Independent local verification of this change (I changed no code).

  • Head 52c83b4b (Merge main), merge base 0036195a. All five new tests are absent on the base: git grep -F lists_recorded_in_turn_share_allocators 0036195a -- d3d12/crates/leonida-gpu/src -> exit 1 (same for the other four).
  • On the PR head, private CARGO_TARGET_DIR, -- --exact, one at a time:
    • host::batch_scratch_tests::the_idle_lists_trim_takes_its_seconds -> 1 passed
    • host::batch_scratch_tests::lists_record_in_pooled_allocators_and_run -> 1 passed (0.52 s)
    • host::batch_scratch_tests::lists_recorded_in_turn_share_allocators -> 1 passed (2.74 s)
    • host::recorders::tests::a_recording_has_its_allocator_to_itself_until_it_is_over -> 1 passed
    • host::recorders::tests::a_recording_still_open_keeps_its_allocator_from_a_reset -> 1 passed
      (854 filtered each). No is_none() device gate in the added lines.
  • git diff --check 0036195a 52c83b4b -> clean. python -I scripts/check_no_game_data.py on host.rs, host/recorders.rs, list.rs, docs/findings.md and docs/switches.md -> clean.

GPUs tested: none (the tests drive the allocator ring and the trim clock on synthetic values). Driver: n/a. No game data, assets or extracted shaders were used or attached.

AI-assisted: yes, Hy4 flash (verification only). No approval implied; this is a local test report.

Independent local verification of this change (I changed no code). - Head `52c83b4b` (`Merge main`), merge base `0036195a`. All five new tests are absent on the base: `git grep -F lists_recorded_in_turn_share_allocators 0036195a -- d3d12/crates/leonida-gpu/src` -> exit 1 (same for the other four). - On the PR head, private `CARGO_TARGET_DIR`, `-- --exact`, one at a time: - `host::batch_scratch_tests::the_idle_lists_trim_takes_its_seconds` -> 1 passed - `host::batch_scratch_tests::lists_record_in_pooled_allocators_and_run` -> 1 passed (0.52 s) - `host::batch_scratch_tests::lists_recorded_in_turn_share_allocators` -> 1 passed (2.74 s) - `host::recorders::tests::a_recording_has_its_allocator_to_itself_until_it_is_over` -> 1 passed - `host::recorders::tests::a_recording_still_open_keeps_its_allocator_from_a_reset` -> 1 passed (854 filtered each). No `is_none()` device gate in the added lines. - `git diff --check 0036195a 52c83b4b` -> clean. `python -I scripts/check_no_game_data.py` on host.rs, host/recorders.rs, list.rs, docs/findings.md and docs/switches.md -> clean. GPUs tested: none (the tests drive the allocator ring and the trim clock on synthetic values). Driver: n/a. No game data, assets or extracted shaders were used or attached. AI-assisted: yes, Hy4 flash (verification only). No approval implied; this is a local test report.
First-time contributor

Test report from another machine (I changed no code). Not a review of the code.

  • Head 52c83b4b9 merged onto main 93b9d90a2: clean merge.
  • RTX 3060 Ti 8 GB, driver 610.88, Intel i7-13700KF, Windows 11, 32 GB RAM.
  • cargo test -p leonida-gpu --lib --locked -- --test-threads=1, 4 runs: 3 passed (795 passed, 0 failed), 1 failed.
  • The new tests (the four in host::recorders::tests and lists_recorded_in_turn_share_allocators) passed in all 4 runs.
  • The failing run: texture::ahead::tests::a_placed_texture_is_filled_ahead_of_the_lists (ahead.rs:403, "not taken") and texture::ahead::tests::drain_waits_until_then_callbacks_have_run (ahead.rs:588, "not taken") failed; 793 passed. I could not reproduce it in the 3 runs after.
  • About the machine: every run was inside a Windows job object capped at 6 GB of committed memory, and the PC had 6 to 7 GB of free commit. main 93b9d90a2 passed the same command 4 of 4 times (790 passed), 3 of them under the same cap.
  • I cannot say whether the one failure is from this change or from the memory limit. I have not seen these two tests fail on main, in 4 runs.
  • Not run: the real-card (--ignored) tests, clippy, the clean-room check, any in-game run.

GPUs tested: RTX 3060 Ti 8 GB, driver 610.88. No game files were used.

AI-assisted: yes (Claude Code, Claude Fable 5.2; verification only).

Test report from another machine (I changed no code). Not a review of the code. - Head `52c83b4b9` merged onto `main` `93b9d90a2`: clean merge. - RTX 3060 Ti 8 GB, driver 610.88, Intel i7-13700KF, Windows 11, 32 GB RAM. - `cargo test -p leonida-gpu --lib --locked -- --test-threads=1`, 4 runs: **3 passed (795 passed, 0 failed), 1 failed**. - The new tests (the four in `host::recorders::tests` and `lists_recorded_in_turn_share_allocators`) passed in all 4 runs. - The failing run: `texture::ahead::tests::a_placed_texture_is_filled_ahead_of_the_lists` (`ahead.rs:403`, "not taken") and `texture::ahead::tests::drain_waits_until_then_callbacks_have_run` (`ahead.rs:588`, "not taken") failed; 793 passed. I could not reproduce it in the 3 runs after. - About the machine: every run was inside a Windows job object capped at 6 GB of committed memory, and the PC had 6 to 7 GB of free commit. `main` `93b9d90a2` passed the same command 4 of 4 times (790 passed), 3 of them under the same cap. - I cannot say whether the one failure is from this change or from the memory limit. I have not seen these two tests fail on `main`, in 4 runs. - Not run: the real-card (`--ignored`) tests, clippy, the clean-room check, any in-game run. GPUs tested: RTX 3060 Ti 8 GB, driver 610.88. No game files were used. AI-assisted: yes (Claude Code, Claude Fable 5.2; verification only).
Sign in to join this conversation.
No description provided.