d3d12: timeline spans that name the step of a copy's refresh (#3105 part 2, D3D12-COPY-REFRESH-SPANS) #3499

Code

Clone this repository

Checkout

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

git fetch -u  "fix/copy-update-stall:Henriazz11-fix/copy-update-stall"
git switch "Henriazz11-fix/copy-update-stall"
Open
leonida/mandy
Henriazz11 wants to merge 1 commit from Henriazz11/mandy:fix/copy-update-stall 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 'fix/copy-update-stall:Henriazz11-fix/copy-update-stall'
git switch 'Henriazz11-fix/copy-update-stall'
First-time contributor

Claimed in the coordination issue #3312 as: D3D12-COPY-REFRESH-SPANS (#3105 part 2; the masked-twin part of #3105 is #3200 and KelboLive's D3D12-MASKED-TWIN-SHORT-WAIT, untouched here).

What and why

Follow-up to #3105 (part 2: the ~1.95 s buffer-copy update stall with ~1.89 s of kernel-mode CPU time). This PR adds timeline spans only, with no behaviour change.

Spans added (zero cost when the timeline is off)

When LEONIDA_TIMELINE is off, each span is a single relaxed atomic load of the timeline mode flag (crate::timeline::on(), or false at compile time without the diagnostics feature). When active, the spans name each step of a copy's refresh:

  • Shadow::update_free (d3d12/crates/leonida-gpu/src/shadow.rs): "a copy's refresh: watch", "a copy's refresh: copy stale", and "a copy's refresh: copy whole".
  • watch::guard (d3d12/crates/leonida-gpu/src/watch.rs): "watch: guard query" -> "watch: guard protect".
  • host::copy_mapped (d3d12/crates/leonida-gpu/src/host.rs):
    • Bulk read (read): "copy mapped: leonida_safe_copy <size>" or "copy mapped: ReadProcessMemory <size>" (with <64K, 64K-1M, 1M-16M, >=16M size buckets, guarded by crate::timeline::on() so neither safecopy::on() nor the size branches run when the timeline is off).
    • Sparse fallback (read_sparse / read_pieces): "copy mapped: sparse fallback".

What the spans measured

Measured in the title (build with the spans plus the chunked variant below, commit c544106de) on the city teleport (-SeriesX -Dlss quality, no frame generation, LEONIDA_PIPELINE_WAIT_MS=50, LEONIDA_SAMPLE_MASK=0, stall trace at 250 ms, last 120 s of each run, runs alternated):

Run LEONIDA_COPY_UPDATE_CHUNK fps median frame frames >100 ms / min frames >1 s longest frame
s-off-1 off 13.8 69.3 ms 63.7 0 203 ms
s-on-1 1 12.5 75.7 ms 82.0 1 2704 ms
s-off-2 off 13.9 71.9 ms 74.3 0 224 ms
s-on-2 1 12.5 75.6 ms 95.6 1 1459 ms

The stall trace of s-off-2 caught a 3,604 ms freeze with this chain:

  • leonida-ahead-textures: ahead of a frame: textures picked by index, 1,865.82 ms;
  • then leonida-wide-4: a copy's refresh: copy stale, 1,646.90 ms in one span.

Inside that span, copy mapped: sparse fallback was 0.19 ms in total, and kernel: GetWriteWatch and watch: guard protect were about 1-2 ms. The time was in the copy itself (leonida_safe_copy or ReadProcessMemory), not in write-watch or VirtualProtect. During these runs the machine had about 600 MB of free RAM and 3,000-4,500 hard page reads/s (33 GB private, 6.6 GB shared VRAM), so the stall is likely paged-out memory, not proven (the trace does not record page-fault counts).

Chunked variant tried and dropped

A 4 MiB chunked update_free variant (LEONIDA_COPY_UPDATE_CHUNK=1) was tested in the same A/B runs above and dropped: it reduced fps by ~10% (13.8-13.9 -> 12.5 fps), increased >100 ms hitches (63.7-74.3 -> 82.0-95.6 / min), and introduced a >1 s frame in each run (2704 ms and 1459 ms vs 203 ms and 224 ms). Documented here so nobody retries it.

Tested

  • cargo clippy -j 6 -p leonida-gpu --all-targets --features dlss -- -D warnings: passed (exit 0).
  • cargo check -j 6 -p leonida-gpu --all-targets --features diagnostics,dlss: passed (exit 0).
  • cargo test -j 6 -p leonida-gpu --lib -- shadow:: --test-threads=1: 66 passed, 0 failed, 2 ignored.
  • cargo test -j 6 -p leonida-gpu --lib -- watch:: --test-threads=1: 55 passed, 0 failed, 4 ignored.
  • cargo test -j 6 -p leonida-gpu --lib -- host:: --test-threads=1: 166 passed, 0 failed, 24 ignored.
  • cargo test -j 6 -p leonida-gpu --lib --features diagnostics -- timeline:: --test-threads=1: 20 passed, 0 failed, 0 ignored.
  • python tests/test_d3d12_switches.py: 22/22 passed (exit 0).
  • python scripts/check_no_game_data.py ... on all modified files: passed (exit 0).

Head: main 0036195a7 + one commit 92f966f94 (rebased from f22b9fa64, cargo check --features diagnostics,dlss clean again). Known failures that are the same on main: frame::tests::handles::a_failed_wait_still_takes_the_time (0xc0000008) and two clippy::collapsible_if in leonida-hle/src/modules/xmem.rs.

GPU tested: NVIDIA GeForce RTX 5080 16 GB, Windows 11 (build 26300), Ryzen 9 5950X, 32 GB RAM.

Checklist

  • Based on current main (0036195a7), one commit, no new switch, no behaviour change.
  • No game data, keys or decryption, including in logs or reports quoted here.
  • Spans cost nothing with the timeline off
  • The chunked variant is not in this pull request; d3d12/docs/switches.md is unchanged.
  • scripts/check_no_game_data.py and tests/test_d3d12_switches.py pass

AI-assisted. Gemini 4 Hydrogen (Antigravity CLI) wrote the change and ran the checks; Claude Code (Claude Opus 5.5) reviewed it and ran the measurements in the title.

🤖 Generated with Claude Code

Claimed in the coordination issue #3312 as: D3D12-COPY-REFRESH-SPANS (#3105 part 2; the masked-twin part of #3105 is #3200 and KelboLive's D3D12-MASKED-TWIN-SHORT-WAIT, untouched here). ## What and why Follow-up to #3105 (part 2: the ~1.95 s buffer-copy update stall with ~1.89 s of kernel-mode CPU time). This PR adds timeline spans only, with no behaviour change. ### Spans added (zero cost when the timeline is off) When `LEONIDA_TIMELINE` is off, each span is a single relaxed atomic load of the timeline mode flag (`crate::timeline::on()`, or `false` at compile time without the `diagnostics` feature). When active, the spans name each step of a copy's refresh: - `Shadow::update_free` (`d3d12/crates/leonida-gpu/src/shadow.rs`): `"a copy's refresh: watch"`, `"a copy's refresh: copy stale"`, and `"a copy's refresh: copy whole"`. - `watch::guard` (`d3d12/crates/leonida-gpu/src/watch.rs`): `"watch: guard query"` -> `"watch: guard protect"`. - `host::copy_mapped` (`d3d12/crates/leonida-gpu/src/host.rs`): - Bulk read (`read`): `"copy mapped: leonida_safe_copy <size>"` or `"copy mapped: ReadProcessMemory <size>"` (with `<64K`, `64K-1M`, `1M-16M`, `>=16M` size buckets, guarded by `crate::timeline::on()` so neither `safecopy::on()` nor the size branches run when the timeline is off). - Sparse fallback (`read_sparse` / `read_pieces`): `"copy mapped: sparse fallback"`. ### What the spans measured Measured in the title (build with the spans plus the chunked variant below, commit c544106de) on the city teleport (`-SeriesX -Dlss quality`, no frame generation, `LEONIDA_PIPELINE_WAIT_MS=50`, `LEONIDA_SAMPLE_MASK=0`, stall trace at 250 ms, last 120 s of each run, runs alternated): | Run | `LEONIDA_COPY_UPDATE_CHUNK` | fps | median frame | frames >100 ms / min | frames >1 s | longest frame | |---|---|---|---|---|---|---| | s-off-1 | off | 13.8 | 69.3 ms | 63.7 | 0 | 203 ms | | s-on-1 | 1 | 12.5 | 75.7 ms | 82.0 | 1 | 2704 ms | | s-off-2 | off | 13.9 | 71.9 ms | 74.3 | 0 | 224 ms | | s-on-2 | 1 | 12.5 | 75.6 ms | 95.6 | 1 | 1459 ms | The stall trace of `s-off-2` caught a 3,604 ms freeze with this chain: - `leonida-ahead-textures`: `ahead of a frame: textures picked by index`, 1,865.82 ms; - then `leonida-wide-4`: **`a copy's refresh: copy stale`, 1,646.90 ms in one span**. Inside that span, `copy mapped: sparse fallback` was 0.19 ms in total, and `kernel: GetWriteWatch` and `watch: guard protect` were about 1-2 ms. The time was in the copy itself (`leonida_safe_copy` or `ReadProcessMemory`), not in write-watch or `VirtualProtect`. During these runs the machine had about 600 MB of free RAM and 3,000-4,500 hard page reads/s (33 GB private, 6.6 GB shared VRAM), so the stall is **likely paged-out memory, not proven** (the trace does not record page-fault counts). ### Chunked variant tried and dropped A 4 MiB chunked `update_free` variant (`LEONIDA_COPY_UPDATE_CHUNK=1`) was tested in the same A/B runs above and dropped: it reduced fps by ~10% (13.8-13.9 -> 12.5 fps), increased >100 ms hitches (63.7-74.3 -> 82.0-95.6 / min), and introduced a >1 s frame in each run (2704 ms and 1459 ms vs 203 ms and 224 ms). Documented here so nobody retries it. ## Tested - `cargo clippy -j 6 -p leonida-gpu --all-targets --features dlss -- -D warnings`: passed (exit 0). - `cargo check -j 6 -p leonida-gpu --all-targets --features diagnostics,dlss`: passed (exit 0). - `cargo test -j 6 -p leonida-gpu --lib -- shadow:: --test-threads=1`: 66 passed, 0 failed, 2 ignored. - `cargo test -j 6 -p leonida-gpu --lib -- watch:: --test-threads=1`: 55 passed, 0 failed, 4 ignored. - `cargo test -j 6 -p leonida-gpu --lib -- host:: --test-threads=1`: 166 passed, 0 failed, 24 ignored. - `cargo test -j 6 -p leonida-gpu --lib --features diagnostics -- timeline:: --test-threads=1`: 20 passed, 0 failed, 0 ignored. - `python tests/test_d3d12_switches.py`: 22/22 passed (exit 0). - `python scripts/check_no_game_data.py ...` on all modified files: passed (exit 0). Head: `main` 0036195a7 + one commit 92f966f94 (rebased from f22b9fa64, `cargo check --features diagnostics,dlss` clean again). Known failures that are the same on `main`: `frame::tests::handles::a_failed_wait_still_takes_the_time` (0xc0000008) and two `clippy::collapsible_if` in `leonida-hle/src/modules/xmem.rs`. GPU tested: NVIDIA GeForce RTX 5080 16 GB, Windows 11 (build 26300), Ryzen 9 5950X, 32 GB RAM. ## Checklist - [x] Based on current `main` (0036195a7), one commit, no new switch, no behaviour change. - [x] No game data, keys or decryption, including in logs or reports quoted here. - [x] Spans cost nothing with the timeline off - [x] The chunked variant is not in this pull request; `d3d12/docs/switches.md` is unchanged. - [x] `scripts/check_no_game_data.py` and `tests/test_d3d12_switches.py` pass AI-assisted. Gemini 4 Hydrogen (Antigravity CLI) wrote the change and ran the checks; Claude Code (Claude Opus 5.5) reviewed it and ran the measurements in the title. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Adds zero-cost-when-off timeline spans inside Shadow::update_free, watch::guard, and host::copy_mapped (including bulk copy method and size bucket, plus sparse fallback) so stall traces show which step of a buffer-copy refresh took the time. No behaviour change.

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

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

  • Head 92f966f9, merge base 0036195a.
  • The diff adds no #[test] at all: it is a span-naming change in host.rs, shadow.rs, timeline.rs and watch.rs (copy_span_name, the on() gate, watch counters). So the evidence below is a regression run, not a new test.
  • On the PR head, default features, private CARGO_TARGET_DIR: cargo test -p leonida-gpu --locked --lib -> 790 passed, 0 failed, 60 ignored. The PR shows mergeable=true against current main and nothing device-gated is being counted.
  • git diff --check 0036195a 92f966f9 -> clean. python -I scripts/check_no_game_data.py on host.rs, shadow.rs, timeline.rs and watch.rs -> clean.

Suggest: a unit test for copy_span_name (the step names are pure string selection) would make this checkable without a card; I did not add it.

GPUs tested: none (unit tests only, no game launch). 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 `92f966f9`, merge base `0036195a`. - The diff adds no `#[test]` at all: it is a span-naming change in `host.rs`, `shadow.rs`, `timeline.rs` and `watch.rs` (`copy_span_name`, the `on()` gate, watch counters). So the evidence below is a regression run, not a new test. - On the PR head, default features, private `CARGO_TARGET_DIR`: `cargo test -p leonida-gpu --locked --lib` -> 790 passed, 0 failed, 60 ignored. The PR shows `mergeable=true` against current main and nothing device-gated is being counted. - `git diff --check 0036195a 92f966f9` -> clean. `python -I scripts/check_no_game_data.py` on host.rs, shadow.rs, timeline.rs and watch.rs -> clean. Suggest: a unit test for `copy_span_name` (the step names are pure string selection) would make this checkable without a card; I did not add it. GPUs tested: none (unit tests only, no game launch). 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.
Sign in to join this conversation.
No description provided.