Skip to content

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

Merged
OreoFR merged 1 commit into
ProjectLeonida:mainfrom
enlight3d:cold-start-wait-clock
Dec 2, 2026
Merged

OreoFR merged 1 commit into
ProjectLeonida:mainfrom
enlight3d:cold-start-wait-clock

Conversation

@enlight3d

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: D3D12-COLD-START-SKIPS (#2570 (comment)). 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

…ock (D3D12-COLD-START-SKIPS follow-up)

After a wait, wait_in read the clock once, before taking the lock of the
frame's count and that of the pipeline's own count, and told both ends
that time. A thread stopped between the read and the lock had its end
told after events with later times, and Waited took the order it was
told as the order of the times (review of pull request 3000):

- andrew-ai: A waits 0-20 ms, B 10-85 ms; with A's end told after B's,
  a wait at 86 ms was left 80 ms of 100 instead of 15, and a pipeline
  both waited for was charged 20 ms instead of 85;
- merzleckeier: A waits 0-10 ms, B 40-50; with A's end, read at 10,
  told after B began, B was left 60 ms instead of 90, the frame had 50
  left instead of 80, and the pipeline was charged 50 instead of 20.

Each begin and end is now timed under the lock of the count it goes
into, through a seam (trait Waiting: the clock and the wait; Real in
the title), so a count is told them in the order of their times. Waited
keeps the latest time it has seen and takes an older one as that: an end
gives back nothing, a begin counts nothing twice. The frame's end is
timed right after the pipeline's, before the log lines.

A wait still counts until its end is timed: a thread stopped after its
wait and before it reads the time counts as waiting meanwhile (test
a_wait_counts_until_its_end_is_timed).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@OreoFR

OreoFR commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator

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

Safe to merge into main? Yes.

  1. git merge-tree --write-tree onto current main 2172c15: clean (main moved only in docs since 393901a; 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
OreoFR added this pull request to the merge queue Dec 2, 2026
@OreoFR

OreoFR commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator

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: #3032 (comment)

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

Merged via the queue into ProjectLeonida:main with commit 8422def Dec 2, 2026
6 checks passed
@OreoFR
OreoFR removed their request for review December 2, 2026 03:43
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