Skip to content

D3D12.X: the fence and CPU-write threads run at high priority - #3321

Draft
Prime1997 wants to merge 1 commit into
xcl-dev:mainfrom
Prime1997:contrib/sync-threads-time-critical
Draft

Prime1997 wants to merge 1 commit into
xcl-dev:mainfrom
Prime1997:contrib/sync-threads-time-critical

Conversation

@Prime1997

@Prime1997 Prime1997 commented Nov 29, 2026 •

Copy link
Copy Markdown
Collaborator

Requested by Prime

xcl-fences writes the completed values that title threads spin on, and xcl-cpu-writes writes the values a GPU command list leaves for the CPU. At normal priority both can queue behind the very title threads that spin waiting for them. Both sleep nearly all the time, so THREAD_PRIORITY_HIGHEST costs the title little. Not time-critical: a fences wake can settle readbacks for up to 20 ms.

What is in it

  • crates/xcl/src/hle/d3d12x/fence_x.rs: xcl-fences sets THREAD_PRIORITY_HIGHEST when it starts
  • crates/xcl/src/hle/d3d12x/cpu_writes.rs: xcl-cpu-writes does the same

Tested

  • RTX 3060 Ti 8 GB (driver 32.0.16.1714), Intel Core i5-10400, 32 GB RAM, Windows 11 Pro build 26200, Rust 1.100.0; the suite in debug, the smoke on a release build of the head commit
  • cargo test --workspace --no-fail-fast -- --test-threads=4 with the three uma_driver tests below skipped, on 4250334: 2605 passed, 0 failed, 49 ignored (SKIPPED GPU tests: 28 lines: 21 no wave64 (this card runs wave32), 3 a wave32 per-lane form refused by the translator (gpu: direct RDNA2 -> DXIL backend (no HLSL / DXC), behind XCL_DIRECT_DXIL #1426), 2 no heap windows (opt-in XCL_HEAP_WINDOWS=1), 1 no dxc.exe, 1 cannot disable the driver cache (needs Windows Developer Mode))
  • the three skipped tests on their own (cargo test -p xcl --lib -- --test-threads=1 uma_driver::tests::a_refused_gpu_upload_segment_becomes_custom uma_driver::tests::mixed_window_keeps_its_bytes_and_the_gpu_sees_every_segment uma_driver::tests::failed_restore_stops_before_importing_an_unbacked_window): 3 passed, 0 failed. They are left out of the whole run because on this PC's driver they can end the library test process (exit 0x7c10003, "coherent window could not be restored"), on main too
  • the library harness twice more (cargo test -p xcl --lib -- --test-threads=4, same skips): 2466 passed, 0 failed; 2466 passed, 0 failed
  • the same whole run on main fa20459: 2605 passed, 0 failed, 49 ignored, 28 SKIPPED lines (own-prs-20261129 run, same command)
  • retail title (60 s scripts/smoke.ps1, no input, copied profile and shader cache): blocks below

Regression run

xcl-smoke v1
commit: fa204595a488c60c6c5f905244f0172072c9c2a9
gpu: NVIDIA GeForce RTX 3060 Ti
title: 419239bf
seconds: 60
exit: 0x0
frames shown: 1334 (PresentX 1334)
presents per 2 s (last 3): 60 60 61
device removals: 0
crashes: 0
peak commit: 15272 MB
xcl-smoke v1
commit: 4250334773d9061155523b97398926935e8728b4
gpu: NVIDIA GeForce RTX 3060 Ti
title: 419239bf
seconds: 60
exit: 0x0
frames shown: 1474 (PresentX 1474)
presents per 2 s (last 3): 60 61 60
device removals: 0
crashes: 0
peak commit: 15338 MB

Not tested

  • AMD GPUs (this is an NVIDIA wave32 card)
  • the retail title's world: the 60 s runs end on the title's start-up screens
  • No unit test: the change is two thread priorities. Whether title threads wait less for fence values in a retail world was not measured for this change alone.

AI use

  • Claude Code wrote the change, wrote this description, and ran the tests and the smoke runs on this PC. The model is not named here: this session cannot tell which model served each turn.

Optional temporary Discord for coordinating contributor work, testing and comparing results.

🤖 Generated with Claude Code

xcl-fences writes the completed values title threads spin on, and xcl-cpu-writes writes the
values a GPU command list leaves for the CPU. At normal priority both can queue behind the
very title threads that spin waiting for them. Both sleep nearly all the time, so
THREAD_PRIORITY_HIGHEST costs the title little; not time-critical, as a fences wake can
settle readbacks for up to 20 ms.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DaZV7vMq866ni7pGnJC2Ew
@Prime1997 Prime1997 self-assigned this Nov 29, 2026
@github-actions github-actions Bot added area:d3d12 D3D12.X runtime, command lists, resources, sparse memory gpu:both Code affects both AMD and NVIDIA GPUs (scope tag; see needs:*-test for remaining checks) labels Nov 29, 2026
@andrewDoing

Copy link
Copy Markdown
Contributor

Confirmed both production workers select Windows priority +2 (HIGHEST) on 42503347, using GetThreadPriority inside each worker. A matched fresh-process control using the same binary with only the two SetThreadPriority calls bypassed read 0 (NORMAL) for both. RTX 5070 / driver 616.92, Ryzen 7 5700X3D.

A bounded two-queue test distinguished the two publication paths. An ungated write on queue B first published 0xC3C3C3C3 and completed its receipt. Then I gated queue A, scheduled a deferred CPU write on A, scheduled a write to a different CPU cell on independent queue B, and signaled a B fence after B’s write-fence signal.

At both priorities, the native B fence and its XCL CPU-visible mirror reached 1 while A was gated. B's CPU-write receipt remained pending throughout a subsequent 100 ms observation, and the cells still held 0 and 0xC3C3C3C3. Releasing A completed both receipts; acquire loads then read 0xA1A1A1A1 and 0xB2B2B2B2.

This identifies a distinct limit when interpreting critical-path traces: a ready fence mirror can coexist with deferred CPU writes still queued behind another queue’s fence. Raising worker priority can affect runnable work, but this FIFO dependency survives the change. The 100 ms was an intentional observation window, not measured scheduler latency. The test used the production write scheduler and fence-mirror hook with native DIRECT queues. It did not exercise guest submit hooks, title CPU contention, or title frame time.

AI disclosure: OpenAI Codex built and ran the fixture and drafted this report.

@KOLESNiii KOLESNiii left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Static review of 42503347 for the stack in the #3041 comparison.

I found no actionable issue in the tested fence and CPU-write worker priority changes.

Review assisted by Codex (GPT-6, exact variant unavailable). No new build or GPU run for this review.

@bmjask bmjask left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Critical review of current head 42503347. I found no immediate correctness defect in setting THREAD_PRIORITY_HIGHEST on the two sleeping completion workers; the work they wake for unblocks title threads. The remaining risk is scheduling under sustained wakeups: poll_fence_downloads and table().refresh() share the boosted fence worker, and neither call's duration is bounded here. Please report wake frequency and worst-case active time in a retail world, plus a before/after frame-time distribution. This is a nonblocking performance check. No local Windows run.

@KOLESNiii

Copy link
Copy Markdown
Contributor

Test run data for the static review

  • On this PC, the prior cargo build --release -p xcl build 4465c886 included this PR in the Prime1997 stack. One warm-cache retail run (XCL_PREPARE_ALL=1, XCL_RUN_SECONDS=180) reached the first present in 12,326 s and showed 5,104 frames in the subsequent 180 s, with zero failed or fallback pipelines and no crash. This is combined-build coverage. It cannot isolate this PR's effect. Run report and comparison
  • On exact head 42503347, andrewDoing measured both production workers at Windows priority +2 and both matched controls at 0. Their two-queue fixture also showed that the deferred-write FIFO dependency persists at both priorities. Independent fixture run
  • Fresh local cargo build --workspace passed on trial merge d761fc59, applying the reviewed head to main at 5b0d5fa3. The test binaries built with cargo test --workspace --no-fail-fast --no-run and the GPU suite is queued.

Local system used for these runs was an RTX 2070 SUPER 8 GB (621.40, wave32), Ryzen 5 3600XT, 32 GB RAM, Windows 11 build 26300, and rustc 1.100.1. Claude Code (Claude Opus 5.5) ran the earlier tests. Codex (GPT-6, exact variant unavailable) checked the artifacts and ran the new build.

@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Review (head 4250334, diff read, not a merge-check; draft, so not built or run):

  • xcl-fences busy-waits at HIGHEST in one case. On each wake it runs refresh(), and every cell's refresh_one (fence_x.rs ~63) calls settle_readbacks_within(20), which loops on yield_now for up to 300 µs while copies back are pending (sparse.rs ~5034-5042) and while trying the STATE lock, for up to 20 ms per cell (~5607-5626). The thread wakes once per armed title Signal, so while readbacks are pending those loops now outrank the title's normal-priority threads, which matters most on 4-6 core CPUs.
  • The benefit isn't measured. The 1334 -> 1474 frames match D3D12.X: count Wait32/64BitValueX waits made on the CPU, by reason #3323's 1470 against the same main run on the start-up screens, so they don't come from this change. Wake rate, time active and frame times in a world would show it. The module doc also says the title sleeps on the cell (monitorx / mwaitx) rather than spinning on it.
  • There's no switch to turn it off; D3D12.X: pipeline and DXC workers run below normal, raised while a title waits #3336 has XCL_LOW_PRIORITY_WORKERS=0.
  • CI is red on 4 pipeline_library / warm_library tests (3 == 2 at pipeline_library.rs:414, then PoisonError), in files this PR doesn't touch; looks like the existing test race, so a rerun should do.

Fine: SetThreadPriority(GetCurrentThread(), ...) runs inside each spawned thread, as elsewhere in the crate, xcl-cpu-writes only waits on an event and stores (no spin), and nothing is shared with #3336's workers.

AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.

@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Test report (not a merge-check: draft)
base: b2b1f10
head: 4250334
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2902 passed, 0 failed, 51 ignored; SKIPPED: 28 (20 no wave64, 2 no d3d12SDKLayers.dll, 1 no D3D12 debug layer, 1 no heap windows, 1 cannot disable the driver cache, 3 others; the same set as on main); failing: none
review: not repeated here; my review of this same head is at #3321 (comment), and its findings stand. This run adds what reading couldn't: whether the change merges, builds and passes the whole suite on current main.
performance: no smoke run.
assessment: merges, builds and passes the whole suite on current main (0 failed); the findings in my review stand
hardware / tool and model: RTX 5070 Ti (driver 621.14), Ryzen 7 7800X3D, Windows 11 26300 / Claude Code with Claude Opus 5.5

AI-written and posted by Foniass without a human check of each point.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:d3d12 D3D12.X runtime, command lists, resources, sparse memory gpu:both Code affects both AMD and NVIDIA GPUs (scope tag; see needs:*-test for remaining checks)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants