d3d12: time each begin and end of a pipeline wait under its count's lock (D3D12-COLD-START-SKIPS follow-up) #3032

Merged
enlight3d merged 1 commit from upstream-pr/3032 into main 2026-12-02 03:26:36 +00:00
enlight3d commented 2026-12-02 02:46:03 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by enlight3d on 2026-12-02T02:46:03Z.


Claimed in the coordination issue as: D3D12-COLD-START-SKIPS (https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7318581855). A follow-up of #3000, for the counting defect its two reviewers found.

What and why

#3000 counts the pipeline waits of a frame's recording threads once (what passed while at least one of them waited) and charges a pipeline's own budget (LEONIDA_PIPELINE_WAIT_ONCE) the same way: Waited in d3d12/crates/leonida-gpu/src/pipeline/make.rs. andrew-ai and merzleckeier showed, in review, that the count could come out wrong. After a wait, wait_in read the clock once, before it took the lock of the frame's count and that of the pipeline's, and gave both counts that time. A thread stopped between the reading and the lock had its end recorded after events with later times, and Waited took the order it was given the events in as the order of their times:

  • andrew-ai: A waits 0-20 ms and B 10-85 ms, with a budget of 100 ms. With A's end recorded after B's, the count closed at A's 20 ms and gave back B's time: a wait that began at 86 ms was left 80 ms instead of 15, and a pipeline both waited for was charged 20 ms instead of 85.
  • merzleckeier: A waits 0-10 ms and B 40-50 ms. With A's end, read at 10 ms, recorded after B began, A was taken to be still waiting: B was left 60 ms instead of 90, the frame had 50 ms left after both instead of 80, and the pipeline was charged 50 ms instead of 20 (the 30 ms between the two waits were counted).

This change (d3d12/crates/leonida-gpu/src/pipeline/make.rs only):

  • reads the time of each begin and end of a wait under the lock of the count it goes into: the frame's for take and give_back, the pipeline's for begin and end. A count then gets its events in the order of their times. The clock and the wait reach the code of wait_in through a small seam (trait Waiting; in the title, Real is Instant::now and until_made), so that tests can script both;
  • makes Waited keep the latest time it has seen and take an earlier one as that time: an end gives back nothing already counted, and a begin counts nothing twice. With the times read under the lock and a monotonic clock this never happens, so it is a guard. andrew-ai's case is fixed by either part, merzleckeier's only by reading under the lock;
  • times the frame's end right after the pipeline's, before the two log lines (one reading, taken before the logs, used to serve both).

What it does not change: a wait counts until its end is timed. A thread stopped after its wait and before it reads the time still counts as waiting meanwhile. In merzleckeier's case with A stopped there, B is still left 60 ms and the frame is charged 50 (test a_wait_counts_until_its_end_is_timed). The count cannot know an end before the thread gives it, and B's 90 ms would need that. The reading of what is left of a pipeline's own budget (billed) still happens before its lock. It changes no count; a thread stopped there can be left more than its pipeline's remaining budget, by the length of the stop.

The switches are those of #3000, unchanged (LEONIDA_PIPELINE_WAIT_DEADLINE, LEONIDA_PIPELINE_WAIT_SHARED, LEONIDA_PIPELINE_WAIT_ONCE, LEONIDA_AB). d3d12/docs/switches.md needs no change: python -I tools/d3d12_switches.py --check d3d12/docs/switches.md passes, and --out writes the same page (no comment that names a switch changed).

Tested

The reviewers' cases, red then green. The tests were first run on a seam with no change of behaviour (the clock and the wait passed in, the times read where they were, on the head of #3000), where each failed for the reason it states, then with the change. On this commit, bringing the original counting back fails the same tests with the same numbers (first mutation below).

Test Case Before With the change
an_older_end_gives_back_nothing_to_the_frame andrew-ai, the frame's count given B's end, then A's next wait at 86 ms left 80 ms 15 ms
an_older_end_gives_back_nothing_to_the_pipeline andrew-ai, a pipeline's own count charged 20 ms 85 ms
an_older_end_gives_back_nothing andrew-ai, Waited itself 20 ms counted 85 ms
an_end_told_late_gives_back_nothing_to_the_frame andrew-ai through the code of wait_in, two threads 80 ms left 15 ms
an_end_told_late_gives_back_nothing_to_the_pipeline the same, one pipeline for both charged 20 ms 85 ms
an_end_timed_before_a_begin_is_counted_before_it_in_the_frame merzleckeier through the code of wait_in B left 60 ms, 50 ms left after both 90 ms, 80 ms
an_end_timed_before_a_begin_is_counted_before_it_in_the_pipeline the same, one pipeline for both B left 60 ms, charged 50 ms 90 ms, 20 ms
an_older_begin_counts_no_time_twice the guard on begins 130 ms counted 95 ms

The two-thread tests run the real code of the wait with a scripted clock. Thread A is stopped once, just after it reads the time of the end under test (as the system may stop a thread anywhere), until the test lets it go; if A holds the lock B waits for, it goes on alone after half a second. With the time read before the lock, as before, the stop let B in between the reading and the count. With the time read under the lock, B waits behind A. With only the guard on Waited (the times still read before the locks), andrew-ai's tests pass and merzleckeier's two still fail with (60, 50).

Two tests were added after review: an_end_older_than_a_begin_gives_back_nothing (it catches a guard that does not remember the times of begins) and a_wait_counts_until_its_end_is_timed (the limit above, pinned).

Rust, on 4b735e555 (this branch, on main d741a9f8f, which has #3000), from d3d12/, all with --target x86_64-pc-windows-msvc:

this branch
cargo test -p leonida-gpu --lib --locked -- pipeline::make::tests 31 passed, 3 runs in a row
cargo test -p leonida-gpu --locked 666 passed, 0 failed, 59 ignored
cargo test -p leonida-gpu --features diagnostics --locked 748 passed, 0 failed, 60 ignored
cargo clippy -p leonida-gpu --all-targets --locked 1 warning, in host.rs; none in make.rs
cargo clippy -p leonida-gpu --all-targets --features diagnostics --locked 10 warnings, none in make.rs

The change adds 10 tests, all in pipeline::make::tests (21 on main).

Mutations, each applied alone to this commit with the make tests then run; each is caught:

  • the original counting brought back (the ends' time read once, before both locks, and no guard): 9 tests fail, with the reviewers' numbers (80 ms left instead of 15; charged 20 ms instead of 85; B left 60 ms instead of 90, with 50 ms left instead of 80, or charged 50 instead of 20);
  • no guard on ends: 5 tests fail;
  • no guard on begins: 2;
  • the ends' time read before both locks, the guard kept: merzleckeier's 2 tests fail, with (60, 50);
  • only the frame's end read before its lock: 1 (the frame's);
  • only the pipeline's end read before its lock: 2;
  • a begin that does not remember its time: 1 (an_end_older_than_a_begin_gives_back_nothing).

Not caught, and the same counts either way: the time of take or of the pipeline's begin read before its lock (the guard on begins keeps the count right; a stop there can only leave the wait more time), and billed read under its lock.

python -I scripts/check_no_game_data.py passes on the changed file and on this text.

GPUs tested: NVIDIA GeForce RTX 3090, driver 621.40, Windows 11 Pro (build 26300): the Rust suites. No game run of this change yet.

AI-assisted: yes (Claude Code, Claude Opus 5.5; reviews by two independent Claude Opus 5.5 agents)

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. (No translator change.)

🤖 Generated with Claude Code

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5598788112 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3032). Originally posted by [enlight3d](https://github.com/enlight3d) on 2026-12-02T02:46:03Z. --- Claimed in the coordination issue as: D3D12-COLD-START-SKIPS (https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7318581855). A follow-up of #3000, for the counting defect its two reviewers found. ### What and why #3000 counts the pipeline waits of a frame's recording threads once (what passed while at least one of them waited) and charges a pipeline's own budget (`LEONIDA_PIPELINE_WAIT_ONCE`) the same way: `Waited` in `d3d12/crates/leonida-gpu/src/pipeline/make.rs`. andrew-ai and merzleckeier showed, in review, that the count could come out wrong. After a wait, `wait_in` read the clock once, before it took the lock of the frame's count and that of the pipeline's, and gave both counts that time. A thread stopped between the reading and the lock had its end recorded after events with later times, and `Waited` took the order it was given the events in as the order of their times: - **andrew-ai**: A waits 0-20 ms and B 10-85 ms, with a budget of 100 ms. With A's end recorded after B's, the count closed at A's 20 ms and gave back B's time: a wait that began at 86 ms was left 80 ms instead of 15, and a pipeline both waited for was charged 20 ms instead of 85. - **merzleckeier**: A waits 0-10 ms and B 40-50 ms. With A's end, read at 10 ms, recorded after B began, A was taken to be still waiting: B was left 60 ms instead of 90, the frame had 50 ms left after both instead of 80, and the pipeline was charged 50 ms instead of 20 (the 30 ms between the two waits were counted). This change (`d3d12/crates/leonida-gpu/src/pipeline/make.rs` only): - reads the time of each begin and end of a wait under the lock of the count it goes into: the frame's for `take` and `give_back`, the pipeline's for `begin` and `end`. A count then gets its events in the order of their times. The clock and the wait reach the code of `wait_in` through a small seam (`trait Waiting`; in the title, `Real` is `Instant::now` and `until_made`), so that tests can script both; - makes `Waited` keep the latest time it has seen and take an earlier one as that time: an end gives back nothing already counted, and a begin counts nothing twice. With the times read under the lock and a monotonic clock this never happens, so it is a guard. andrew-ai's case is fixed by either part, merzleckeier's only by reading under the lock; - times the frame's end right after the pipeline's, before the two log lines (one reading, taken before the logs, used to serve both). What it does not change: a wait counts until its end is timed. A thread stopped after its wait and before it reads the time still counts as waiting meanwhile. In merzleckeier's case with A stopped there, B is still left 60 ms and the frame is charged 50 (test `a_wait_counts_until_its_end_is_timed`). The count cannot know an end before the thread gives it, and B's 90 ms would need that. The reading of what is left of a pipeline's own budget (`billed`) still happens before its lock. It changes no count; a thread stopped there can be left more than its pipeline's remaining budget, by the length of the stop. The switches are those of #3000, unchanged (`LEONIDA_PIPELINE_WAIT_DEADLINE`, `LEONIDA_PIPELINE_WAIT_SHARED`, `LEONIDA_PIPELINE_WAIT_ONCE`, `LEONIDA_AB`). `d3d12/docs/switches.md` needs no change: `python -I tools/d3d12_switches.py --check d3d12/docs/switches.md` passes, and `--out` writes the same page (no comment that names a switch changed). ### Tested **The reviewers' cases, red then green.** The tests were first run on a seam with no change of behaviour (the clock and the wait passed in, the times read where they were, on the head of #3000), where each failed for the reason it states, then with the change. On this commit, bringing the original counting back fails the same tests with the same numbers (first mutation below). | Test | Case | Before | With the change | |---|---|---|---| | `an_older_end_gives_back_nothing_to_the_frame` | andrew-ai, the frame's count given B's end, then A's | next wait at 86 ms left 80 ms | 15 ms | | `an_older_end_gives_back_nothing_to_the_pipeline` | andrew-ai, a pipeline's own count | charged 20 ms | 85 ms | | `an_older_end_gives_back_nothing` | andrew-ai, `Waited` itself | 20 ms counted | 85 ms | | `an_end_told_late_gives_back_nothing_to_the_frame` | andrew-ai through the code of `wait_in`, two threads | 80 ms left | 15 ms | | `an_end_told_late_gives_back_nothing_to_the_pipeline` | the same, one pipeline for both | charged 20 ms | 85 ms | | `an_end_timed_before_a_begin_is_counted_before_it_in_the_frame` | merzleckeier through the code of `wait_in` | B left 60 ms, 50 ms left after both | 90 ms, 80 ms | | `an_end_timed_before_a_begin_is_counted_before_it_in_the_pipeline` | the same, one pipeline for both | B left 60 ms, charged 50 ms | 90 ms, 20 ms | | `an_older_begin_counts_no_time_twice` | the guard on begins | 130 ms counted | 95 ms | The two-thread tests run the real code of the wait with a scripted clock. Thread A is stopped once, just after it reads the time of the end under test (as the system may stop a thread anywhere), until the test lets it go; if A holds the lock B waits for, it goes on alone after half a second. With the time read before the lock, as before, the stop let B in between the reading and the count. With the time read under the lock, B waits behind A. With only the guard on `Waited` (the times still read before the locks), andrew-ai's tests pass and merzleckeier's two still fail with (60, 50). Two tests were added after review: `an_end_older_than_a_begin_gives_back_nothing` (it catches a guard that does not remember the times of begins) and `a_wait_counts_until_its_end_is_timed` (the limit above, pinned). **Rust**, on `4b735e555` (this branch, on `main` `d741a9f8f`, which has #3000), from `d3d12/`, all with `--target x86_64-pc-windows-msvc`: | | this branch | |---|---| | `cargo test -p leonida-gpu --lib --locked -- pipeline::make::tests` | 31 passed, 3 runs in a row | | `cargo test -p leonida-gpu --locked` | 666 passed, 0 failed, 59 ignored | | `cargo test -p leonida-gpu --features diagnostics --locked` | 748 passed, 0 failed, 60 ignored | | `cargo clippy -p leonida-gpu --all-targets --locked` | 1 warning, in `host.rs`; none in `make.rs` | | `cargo clippy -p leonida-gpu --all-targets --features diagnostics --locked` | 10 warnings, none in `make.rs` | The change adds 10 tests, all in `pipeline::make::tests` (21 on `main`). Mutations, each applied alone to this commit with the make tests then run; each is caught: - the original counting brought back (the ends' time read once, before both locks, and no guard): 9 tests fail, with the reviewers' numbers (80 ms left instead of 15; charged 20 ms instead of 85; B left 60 ms instead of 90, with 50 ms left instead of 80, or charged 50 instead of 20); - no guard on ends: 5 tests fail; - no guard on begins: 2; - the ends' time read before both locks, the guard kept: merzleckeier's 2 tests fail, with (60, 50); - only the frame's end read before its lock: 1 (the frame's); - only the pipeline's end read before its lock: 2; - a begin that does not remember its time: 1 (`an_end_older_than_a_begin_gives_back_nothing`). Not caught, and the same counts either way: the time of `take` or of the pipeline's `begin` read before its lock (the guard on begins keeps the count right; a stop there can only leave the wait more time), and `billed` read under its lock. `python -I scripts/check_no_game_data.py` passes on the changed file and on this text. GPUs tested: NVIDIA GeForce RTX 3090, driver 621.40, Windows 11 Pro (build 26300): the Rust suites. No game run of this change yet. AI-assisted: yes (Claude Code, Claude Opus 5.5; reviews by two independent Claude Opus 5.5 agents) ### 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. - [ ] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. (No translator change.) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
OreoFR commented 2026-12-02 03:15:50 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-12-02T03:15:50Z.


RUN - #3032 head 4b735e555 merged into main 393901a5b, RTX 4070 Laptop 8 GB, NVIDIA 621.40, Windows 11 (Verified by OreoFR)

  • Tests: cargo test -p leonida-gpu --features diagnostics -- pipeline::make on the merged tree: 31 passed, 0 failed (the 10 new wait-accounting tests included). PR CI 6/6 green.
  • In game, first start with no pipelines (fresh data folder, -SeriesS -NoDlss -NoFrameGen, BAR 0, texture budget 1024, standing, 330 s): the head is drawn at arrival (checked on local captures at +60/+150/+280/+320 s, not posted). The first-start frames are long while pipelines compile (0 fps at +60/+150 s), then 25-27 fps from +280 s. 0 device lost, 0 panics.
  • Red side: the defect is in the counting of the shared wait budget; it has no visible symptom of its own in game. The red is the author's tests (red on #3000, green here), and #3000's in-game red/green on this card still holds (head missing 2/2 with LEONIDA_PIPELINE_WAIT_DEADLINE=0).

Safe to merge into main? Yes.

  1. git merge-tree --write-tree onto current main 2172c1502: clean (main moved only in docs since 393901a5b; no d3d12/ code change).
  2. PR CI on 4b735e555: all green. Main's last CI / D3D12 runtime / No Game Data: green.
  3. Default path: it only changes how wait times are counted; no new switch, LEONIDA_PIPELINE_WAIT_DEADLINE=0 still restores the old behaviour.
  4. Repo checks: No Game Data green; crate tests green on the merged tree.
  5. Scope: one file, d3d12/crates/leonida-gpu/src/pipeline/make.rs (117 lines of code, 372 of tests).

AI-assisted (Claude Code, Claude Opus 5.5).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7333706341 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3032#issuecomment-7333706341). Originally posted by [OreoFR](https://github.com/OreoFR) on 2026-12-02T03:15:50Z. --- RUN - #3032 head `4b735e555` merged into `main` 393901a5b, RTX 4070 Laptop 8 GB, NVIDIA 621.40, Windows 11 (Verified by OreoFR) - **Tests:** `cargo test -p leonida-gpu --features diagnostics -- pipeline::make` on the merged tree: 31 passed, 0 failed (the 10 new wait-accounting tests included). PR CI 6/6 green. - **In game, first start with no pipelines** (fresh data folder, `-SeriesS -NoDlss -NoFrameGen`, BAR 0, texture budget 1024, standing, 330 s): the head is drawn at arrival (checked on local captures at +60/+150/+280/+320 s, not posted). The first-start frames are long while pipelines compile (0 fps at +60/+150 s), then 25-27 fps from +280 s. 0 device lost, 0 panics. - **Red side:** the defect is in the counting of the shared wait budget; it has no visible symptom of its own in game. The red is the author's tests (red on #3000, green here), and #3000's in-game red/green on this card still holds (head missing 2/2 with `LEONIDA_PIPELINE_WAIT_DEADLINE=0`). **Safe to merge into main?** Yes. 1. `git merge-tree --write-tree` onto current `main` 2172c1502: clean (main moved only in docs since 393901a5b; no `d3d12/` code change). 2. PR CI on `4b735e555`: all green. Main's last CI / D3D12 runtime / No Game Data: green. 3. Default path: it only changes how wait times are counted; no new switch, `LEONIDA_PIPELINE_WAIT_DEADLINE=0` still restores the old behaviour. 4. Repo checks: No Game Data green; crate tests green on the merged tree. 5. Scope: one file, `d3d12/crates/leonida-gpu/src/pipeline/make.rs` (117 lines of code, 372 of tests). AI-assisted (Claude Code, Claude Opus 5.5).
OreoFR commented 2026-12-02 03:16:12 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-12-02T03:16:12Z.


Merging. Head 4b735e555, main 2172c1502, through the merge queue, with OreoFR's OK (maintainer). Claim D3D12-COLD-START-SKIPS (#2570). Verified on an RTX 4070 Laptop 8 GB / 621.40: tests 31/31 on the merged tree, first start with no pipelines draws the head, 330 s, 0 device lost: https://github.com/ProjectLeonida/mandy/pull/3032#issuecomment-7333706341

AI-assisted (Claude Code, Claude Opus 5.5).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7333712600 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3032#issuecomment-7333712600). Originally posted by [OreoFR](https://github.com/OreoFR) on 2026-12-02T03:16:12Z. --- Merging. Head `4b735e555`, main `2172c1502`, through the merge queue, with OreoFR's OK (maintainer). Claim D3D12-COLD-START-SKIPS (#2570). Verified on an RTX 4070 Laptop 8 GB / 621.40: tests 31/31 on the merged tree, first start with no pipelines draws the head, 330 s, 0 device lost: https://github.com/ProjectLeonida/mandy/pull/3032#issuecomment-7333706341 AI-assisted (Claude Code, Claude Opus 5.5).
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
ProjectLeonida/mandy!3032
No description provided.