Skip to content

D3D12.X: instrument submitting-thread call breakdown under XCL_LOOK_STATS=2 - #2729

Open
Decodere wants to merge 1 commit into
xcl-dev:nvidia-devfrom
Decodere:ws8-part1-submitter-api-cost-breakdown
Open

Decodere wants to merge 1 commit into
xcl-dev:nvidia-devfrom
Decodere:ws8-part1-submitter-api-cost-breakdown

Conversation

@Decodere

@Decodere Decodere commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Targets nvidia-dev (eae01502). Adds XCL_LOOK_STATS=2-only submitting-thread and recording-path instrumentation to api_cost so CPU-bound frames on 8 GB GPUs can be broken down into non-overlapping completed top-level call durations per 2 s sample window. Zero behavior change and zero timer/atomic overhead when XCL_LOOK_STATS != 2.

Part of #2688

What is in it

  • crates/xcl/src/hle/api_cost.rs: accumulate durations internally in nanoseconds (exposing microseconds from totals() and submitter_totals()) so high-frequency sub-microsecond calls do not truncate to 0 us; protect ON_SUBMITTER entries with a single-writer seqlock (CounterPair) so submitter_totals() takes coherent (calls, us) snapshots; add #[inline] time_if(on, call) (no clock read, TLS lookup, or atomic write when disabled), 8 XCL_LOOK_STATS=2-only Call variants (DrawRecord, DispatchRecord, BindState, ViewDescriptor, ViewWant, ListBarrier, ListCopy, ListLifecycle), is_sub_call (excluding nested PresentCopyWait, PipelineWait, and ViewWant from top-level completed totals), all_threads_summary_parts, submitter_summary_line(now, before, window_ms) (accounting any nonzero count or duration delta and reporting ; completed X ms (window Y ms) for calls completed since the prior sample without claiming an in-window untimed remainder), and 6 new/updated unit tests (sub_microsecond_calls_accumulate_without_truncation, time_if_disabled_adds_zero_counts_and_zero_time, submitter_summary_excludes_sub_calls_and_reports_completed_window, summary_retains_nonzero_duration_delta_when_count_delta_is_zero, submitter_counter_pair_snapshots_are_coherent_under_concurrent_writes, submitter_summary_describes_straddling_completed_call_without_untimed_claim).
  • crates/xcl/src/host.rs: format calls (2 s, all threads) via api_cost::all_threads_summary_parts (retaining nonzero duration deltas even when count delta is 0), track the actual elapsed window_ms on each periodic activity tick, and log api_cost::submitter_summary_line(&spent, &own_cost, window_ms) when XCL_LOOK_STATS=2 is active.
  • crates/xcl/src/hle/d3d12x/bind.rs: wire time_if(deep(), ...) for DrawRecord (draw_instanced, draw_indexed_instanced, dispatch_mesh), DispatchRecord (dispatch_flags, covering both Dispatch and DispatchX), lazy deep().then(...) in execute_indirect (so signature_dispatches(sig) is never evaluated when disabled), and BindState (root-parameter, descriptor-table, and PSO bindings), preserving capture::op / capture_work and deferred two-pass pipeline queueing on eae01502.
  • crates/xcl/src/hle/d3d12x/list_x.rs: wire time_if(deep(), ...) for DrawRecord, BindState (including set_descriptor_heaps_x), ListBarrier, ListCopy, and ListLifecycle; split out emit_uav_barrier_pc, reset_pc_inner, and copy_buffer_region_inner so internal delegation paths (flush_cache_range_x/flush_pipeline_x, reset_stand_in, copy_resource_x) never double-count while preserving capture::op instrumentation.
  • crates/xcl/src/hle/d3d12x/real.rs: wire Call::FenceWait on blocking SetEventOnCompletion(_, NULL) scoped to drop before settle_readbacks_try() so FenceSettle never nests inside FenceWait; wire ViewDescriptor (splitting dev_create_srv_inner and dev_create_uav_checked_inner so dev_create_srv_at_x and depth_uav twins never double-count, and resolving resource_at before starting ViewDescriptor in dev_create_srv_at_x/dev_create_uav_at_x so CreatePlacedResource never nests inside ViewDescriptor), BindState, ListCopy, and ListLifecycle; guard all unconditional PC-vtable hooks (cl_close, cl_reset, alloc_reset, dev_create_rtv, cl_clear_rtv, cl_om_set_render_targets, fence_set_event_on_completion) with !title_textures::is_own_work() && !present::presenting() while preserving flush_two_pass, gpu_times::before_close, and eb784bc3 submit/sparse lock-holder timing on eae01502.
  • crates/xcl/src/hle/d3d12x/view_want.rs: wire time_if(deep(), Call::ViewWant) in want after the is_view(va) guard.

Tested

  • Hardware and toolchain: Windows 11 Home (build 26200), NVIDIA GeForce RTX 4060 8 GB (32-lane waves, driver 621.14), Intel Core i5-11400F (6C/12T), 32 GB RAM, rustc 1.100.0, DXC 1.9.2609.5, debug (WARP) and release (RTX 4060), rebased cleanly onto origin/nvidia-dev (eae01502).
  • cargo test -p xcl --lib hle::api_cost::tests:: -- --test-threads=4 --nocapture (debug XCL_ADAPTER=warp): 8 passed, 0 failed, 0 ignored (0 skipped, 0.00 s).
  • cargo test --release -p xcl --lib hle::api_cost::tests:: -- --test-threads=4 --nocapture (release RTX 4060): 8 passed, 0 failed, 0 ignored (0 skipped, 0.00 s).
  • Affected module suites on release RTX 4060 (--test-threads=4): 200 passed, 0 failed, 0 ignored across hle::d3d12x::bind:: (53/0/0, 0.35 s, 1 opt-in skip with XCL_HEAP_WINDOWS unset), hle::d3d12x::list_x:: (50/0/0, 0.93 s, 0 skipped), and hle::d3d12x::real:: (97/0/0, 0 skipped).
  • cargo clippy --workspace --all-targets: 0 new warnings and 0 new errors across all added/changed lines in all 6 modified files (pre-existing nvidia-dev clippy errors remain in untouched lower_valu.rs, lower_wide.rs, recomp/lower.rs, and sparse.rs).
  • Live XCL_LOOK_STATS=2 XCL_ACTIVITY=1 25 s RTX 4060 smoke on 4922e39e (title: 29640639): 470 frames (PresentX 470), 1 60 60 presents/2 s, 0 device removals, 0 crashes, 15788 MB peak commit, exit: 0x0.

Regression run
Quiet 60 s scripts/smoke.ps1 runs on RTX 4060 with default flags (XCL_LOOK_STATS unset). Includes main merge-base (b49be183, same-day run on this hardware) for pr_check.py, nvidia-dev target base (eae01502), and rebased PR HEAD (4922e39e):

main (b49be183, merge-base):

xcl-smoke v1
commit: b49be1836844fe0b0cdb3211dce3cb7fb4e21ac7
gpu: NVIDIA GeForce RTX 4060
title: 29640639
seconds: 60
exit: 0x0
frames shown: 1571 (PresentX 1571)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 13975 MB

nvidia-dev (eae01502, target base):

xcl-smoke v1
commit: eae015021c639866ab061cdff2e9daf8ba9daac5
gpu: NVIDIA GeForce RTX 4060
title: 29640639
seconds: 60
exit: 0x0
frames shown: 1585 (PresentX 1585)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15900 MB

PR HEAD (4922e39e, default flags):

xcl-smoke v1
commit: 4922e39e2c56c1de53ecbef4675a4dfa7908ade6
gpu: NVIDIA GeForce RTX 4060
title: 29640639
seconds: 60
exit: 0x0
frames shown: 1559 (PresentX 1559)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 16099 MB

Not tested

  • Full --workspace test suite on the rebased eae01502 commit (targeted api_cost::tests:: and affected bind::/list_x::/real:: suites plus clippy and smokes ran on 4922e39e).
  • AMD and Intel GPUs, 64-lane wave GPUs, and Linux / Wine / vkd3d-proton.
  • Live retail title hits for Call::FenceWait (blocking SetEventOnCompletion(_, NULL)) and Call::ViewWant (view_want::want), which did not fire during the short menu/intro smoke (covered by deterministic unit tests).
  • Optional-env/sample-pack tests (XCL_HEAP_WINDOWS, XCL_SAMPLE_RPF, d3d12SDKLayers.dll unset/unavailable).

AI use

  • Google Antigravity (Gemini 4 Neon): implemented the api_cost nanosecond accumulation, CounterPair seqlock, XCL_LOOK_STATS=2 submitting-thread instrumentation, and completed-call summary wording, rebased onto origin/nvidia-dev (eae01502), ran the unit/module tests, clippy, and retail smoke runs on the hardware above, and updated this description.

@luckyteagirl luckyteagirl 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.

On 5465094, note publishes a submitter call count and its duration in separate atomic writes, while submitter_totals reads them separately. An activity tick can snapshot (1 call, 0 µs) between the writes. The next tick then sees (1 call, 400000 µs) but submitter_summary_line skips that category because the count delta is zero; the host still advances its baseline. That completed 400 ms call disappears from every new accounted window, even though it remains in cumulative totals. Relaxed ordering makes this interleaving possible; stronger ordering on the two independent atomics alone would not make their snapshot coherent.

Could the formatter account a nonzero duration delta independently of count, or take a coherent count/duration snapshot? A deterministic formatter/host fixture with snapshots (0,0) → (1,0) → (1,400000) should retain the 400 ms in the second report. This is exact-head source review; I did not reproduce the race at runtime.

@luckyteagirl luckyteagirl 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.

On 5465094, the new accounted … of … (… untimed) line treats cumulative completed-call durations as time inside the last activity window. A call is added in full at Timer::drop. For example, a 2.5-second blocking fence wait spanning two 2-second samples adds zero to the first window and all 2500 ms to the second. That second report can say accounted 2500 ms of 2000 ms (0 ms untimed) even when no calls overlap, so the stated wall-clock remainder is misleading. This is separate from the count/duration snapshot race I reported earlier.

Could the durations be split by overlap with the measurement window, or the line describe durations of calls completed since the prior sample without claiming an in-window untimed remainder? A synthetic call crossing a tick would make the chosen semantics testable. This is exact-head source review; I did not run a timing fixture.

@emirduran707

Copy link
Copy Markdown
Contributor

Independent AMD RDNA4 (native wave64) verification of exact PR head 546509487103c94e40d423b4e1d778cc54076741 (base: nvidia-dev) on Windows 11 Pro 26300 (AMD Radeon RX 9060 XT 16 GB, driver 32.0.31044.16, Ryzen 5 5600, Rust 1.100.0 MSVC, DXC 1.9.2602.17).

Tested

  • cargo build --workspace (debug): passed (0 errors).
  • cargo test --workspace --no-fail-fast -- --test-threads=4: 1746 passed, 8 failed, 48 ignored (9 SKIPPED: 6 per-lane DXIL forms, 2 no heap windows, 1 pso_measurement_compiles_an_exact_synthetic_record Windows Developer Mode off).
    • All 3 new unit tests passed:
      • hle::api_cost::tests::sub_microsecond_calls_accumulate_without_truncation
      • hle::api_cost::tests::submitter_summary_excludes_sub_calls_from_accounted_and_computes_untimed
      • hle::api_cost::tests::time_if_disabled_adds_zero_counts_and_zero_time
    • The 8 failures are outside api_cost (fuzz_validate_and_gpu_{1,3}, texture_upload x2, console_ms_in_as_slot_and_ms_with_as_are_refused_cleanly, async_compute_pso_registers_real_pso_and_not_standin, two_pass_driver_cache_reuses_both_passes_and_recovers_corrupt_blobs).
  • cargo build --release -p xcl: passed (0 errors).

Not tested

  • 60 s retail-title smoke via scripts/smoke.ps1 (the nvidia-dev branch started inline shader precompilation before printing the log path, so smoke.ps1 aborted early).
  • Runtime XCL_LOOK_STATS=2 output, NVIDIA / Intel GPUs, Linux / Wine.

AI use

  • Verification run and report prepared with Antigravity (Gemini 4 Neon); all build and test results are from live runs on this machine.

@MarcosMDev

Copy link
Copy Markdown

Test report: 5465094 against its base af2cecc on an RTX 4060 (Windows): full suite, no new failure. No title was run.

cargo build --workspace --locked, then cargo test --workspace --locked --no-fail-fast -- --test-threads=4, debug, each commit in its own worktree and target folder:

  • base af2cecc: 1745 passed, 6 failed, 48 ignored
  • head: 1748 passed, 6 failed, 48 ignored. The 3 new api_cost tests pass.
  • The same 30 SKIPPED lines on both. The failures are the same 5 tests on both (one is counted twice), already listed in this PR's description.

I read the hook changes for the "no behavior change" claim. Each new guard only decides whether a timer starts; the hooks' own calls are unchanged. deep() reads a OnceLock, so with XCL_LOOK_STATS unset each hook adds one branch. The two _inner splits keep the callers' arguments, and the depth-UAV twin now calls the inner function, so it isn't counted twice.

RTX 4060 8 GB (driver 621.01), Ryzen 7 5700X, 24 GB, Windows 11 26200, rustc 1.100.0, DXC 1.9.2609.5.

Not tested: any title, so no XCL_LOOK_STATS=2 lines from a real run; overhead with it off; AMD.

AI use: Claude Code (Claude Opus 5.5) ran the tests, read the diff and drafted this; I read it before posting.

@Decodere
Decodere force-pushed the ws8-part1-submitter-api-cost-breakdown branch from 5465094 to 93e66c1 Compare November 26, 2026 11:29
@Decodere

Copy link
Copy Markdown
Contributor Author

Thanks for both reviews, @luckyteagirl! Updated in 93e66c19:

  1. Count/duration snapshot coherence: Protected ON_SUBMITTER entries with a single-writer seqlock (CounterPair, since SUBMITTER latches at most one thread ID) so submitter_totals() returns coherent (calls, us) pairs, and updated both submitter_summary_line and all_threads_summary_parts (host.rs) to retain any category with a nonzero count or duration delta (dc > 0 || dus > 0). Added deterministic tests for both the (0, 0) -> (1, 0) -> (1, 400_000) fixture (summary_retains_nonzero_duration_delta_when_count_delta_is_zero) and concurrent seqlock coherence (submitter_counter_pair_snapshots_are_coherent_under_concurrent_writes).
  2. Completed-call window semantics: Removed the in-window untimed remainder claim in favor of ; completed {ms} ms (window {window_ms} ms) so durations of calls completed since the prior sample are reported honestly even when a long call straddles a 2 s tick, with a deterministic 2.5 s straddling-call test (submitter_summary_describes_straddling_completed_call_without_untimed_claim).

Re-verified hle::api_cost::tests:: (8/8 debug WARP and release RTX 4060), full release workspace (1753 passed, 3 pre-existing nvidia-dev failures), and 60 s RTX 4060 smokes (default + XCL_LOOK_STATS=2).

@MarcosMDev

Copy link
Copy Markdown

Follow-up to my report, on the new head 93e66c1 (same base af2cecc, same RTX 4060 and setup):

  • Full suite, same command: 1751 passed, 6 failed, 48 ignored. The same 5 base failures and the same 30 SKIPPED lines as on the base; the 6 api_cost tests now in the PR pass, the concurrent seqlock one included.

Not tested: any title, so no XCL_LOOK_STATS=2 lines from a real run; AMD.

AI use: Claude Code (Claude Opus 5.5) ran the tests and drafted this; I read it before posting.

@Prime1997 Prime1997 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Good within the submitting-thread accounting scope. The current head keeps count/duration snapshots coherent and labels durations as completed-call time rather than claiming an untimed wall-clock remainder. The focused tests cover both earlier review cases, sub-microsecond accumulation, disabled instrumentation and thread separation.

Tested clean PR head 93e66c197c2737c2fa7cbb97645c367cf01b33eb, targeting current nvidia-dev af2cecc40886a8682ecf3267f5328c6dc4b66ad7, in its own worktree and build directory. 8 passed, 0 failed, 0 ignored, 0 skipped. Each filter ran in a fresh process:

  • hle::api_cost::tests::a_call_is_counted_with_its_time
  • hle::api_cost::tests::the_submitting_thread_has_its_own_count
  • hle::api_cost::tests::sub_microsecond_calls_accumulate_without_truncation
  • hle::api_cost::tests::time_if_disabled_adds_zero_counts_and_zero_time
  • hle::api_cost::tests::submitter_summary_excludes_sub_calls_and_reports_completed_window
  • hle::api_cost::tests::summary_retains_nonzero_duration_delta_when_count_delta_is_zero
  • hle::api_cost::tests::submitter_counter_pair_snapshots_are_coherent_under_concurrent_writes
  • hle::api_cost::tests::submitter_summary_describes_straddling_completed_call_without_untimed_claim

Build: cargo test --locked --offline -p xcl --lib --no-run --message-format=json --target-dir <isolated-target>. Execution: <test-exe> <each-exact-name-above> --exact --test-threads=1 --nocapture. Only private executable/target paths are placeholders. Windows 11 Pro 26200, i5-10400, 32 GiB RAM, Rust 1.100.0 MSVC debug tests; no GPU calls in these selected fixtures.

Not tested: No live XCL_LOOK_STATS=2 title output, instrumentation overhead, GPU behavior or full-suite run was measured. This supports retaining the PR; it is not whole-branch or merge certification.

AI use: Codex (GPT-6) reviewed source, ran the exact tests and checked the raw results. Optional temporary Discord for coordinating contributor work, testing and comparing results.

@Prime1997 Prime1997 mentioned this pull request Nov 26, 2026
@throny

throny commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator

Test report: XCL_LOOK_STATS=2 in the world on a 16 GB card. Here the submitting thread's time is in PresentX, not in ExecuteCommandLists.

Tested

What it shows (the calls of the submitting thread lines from 200 s on, 211 s of windows):

call count time share of the window
PresentX 69 195.9 s (2.84 s each; 4.0 s each in the last minute) 93 %
ExecuteCommandLists 1,251 14.5 s (11.6 ms each) 7 %
WaitFrameEventX 139 0.0 s 0 %
wait of a present for the last frame's copy per present 0 ms 0 %

Not tested

  • The suite and this PR's tests (others' reports cover them), AMD, a card under its budget. One run.

AI use: Claude Code (Claude Fable 5.2) ran this on the owner's machine and wrote the report.

…TATS=2

With `XCL_LOOK_STATS=2`, `api_cost` previously truncated each call via `d.as_micros() as u64` (rounding sub-microsecond calls to 0), left `Call::FenceWait` unwired, and did not time command-list recording, state/root binding, view/descriptor calls, barriers/flushes, copies/clears, list lifecycle, or GPU-view tile asks on the submitting thread.

- Accumulate call durations in nanoseconds inside `api_cost` and convert to microseconds in `totals()` / `submitter_totals()` so high-frequency sub-microsecond calls sum accurately without changing the public `(calls, us)` API.
- Protect `ON_SUBMITTER` entries with a single-writer seqlock (`CounterPair`) so `submitter_totals()` snapshots coherent `(calls, us)` pairs, and retain nonzero duration deltas even when `count` delta is 0 in both `all_threads_summary_parts` and `submitter_summary_line`.
- Add `#[inline] api_cost::time_if(on, call)` (zero clock, TLS, or atomic overhead when disabled), 8 `XCL_LOOK_STATS=2`-only `Call` variants (`DrawRecord`, `DispatchRecord`, `BindState`, `ViewDescriptor`, `ViewWant`, `ListBarrier`, `ListCopy`, `ListLifecycle`), `is_sub_call`, and `submitter_summary_line` (reporting completed top-level call durations alongside the elapsed `window_ms` without claiming an in-window untimed remainder or double-counting nested sub-timers `PresentCopyWait`, `PipelineWait`, and `ViewWant`).
- Update `host.rs` to format `calls (2 s, all threads)` via `all_threads_summary_parts` and log `submitter_summary_line` using the actual elapsed tick duration.
- Wire `XCL_LOOK_STATS=2`-gated timers across `bind.rs`, `list_x.rs`, `real.rs` (including `Call::FenceWait` on blocking `SetEventOnCompletion(_, NULL)` dropped before `settle_readbacks_try()`), and `view_want.rs`, splitting internal helpers (`emit_uav_barrier_pc`, `reset_pc_inner`, `copy_buffer_region_inner`, `dev_create_srv_inner`, `dev_create_uav_checked_inner`), resolving `resource_at` before starting `ViewDescriptor` in `dev_create_srv_at_x`/`dev_create_uav_at_x`, and guarding all unconditional PC-vtable hooks (`cl_close`, `cl_reset`, `alloc_reset`, `dev_create_rtv`, `cl_clear_rtv`, `cl_om_set_render_targets`, `fence_set_event_on_completion`) with `!is_own_work() && !presenting()` so no top-level call double-counts.
- Add unit tests in `api_cost::tests` covering sub-microsecond accumulation, zero-count disabled `time_if`, `submitter_summary_line` sub-call exclusion, nonzero-duration delta retention on zero count delta (`(0,0) -> (1,0) -> (1,400_000)`), concurrent `CounterPair` snapshot coherence, and a 2.5 s call straddling two 2 s activity ticks.

Part of xcl-dev#2688
Assisted-by: Google Antigravity (Gemini 4 Neon)
@Decodere
Decodere force-pushed the ws8-part1-submitter-api-cost-breakdown branch from 93e66c1 to 4922e39 Compare November 26, 2026 18:02
@Decodere

Copy link
Copy Markdown
Contributor Author

Rebased cleanly onto origin/nvidia-dev eae01502 in 4922e39e (mergeable, 0 conflicts).

  • Preserved eb784bc3's submit & sparse lock-holder timing (XCL_LOOK_STATS=2) alongside the submitting-thread api_cost breakdown (CounterPair, dc > 0 || dus > 0, ; completed X ms (window Y ms)), eae01502 capture::op/capture_work, deferred two-pass queueing, and gpu_times::before_close.
  • Re-verified on RTX 4060 (Win11, release): debug WARP & release api_cost::tests:: 8/0/0, bind:: 53/0/0 (1 skip), list_x:: 50/0/0, real:: 97/0/0, 0 new clippy warnings, paired 60 s default smoke vs eae01502 (1559 vs 1585 frames, 60 60 60, 16099 vs 15900 MB, 0 rem/crash), and 25 s XCL_LOOK_STATS=2 smoke (470 frames, exit: 0x0).

@MarcosMDev

Copy link
Copy Markdown

Follow-up on the rebased head 4922e39 against its new base eae0150 (same RTX 4060 and setup as my report): no new failure.

  • Full suite, cargo test --workspace --locked --no-fail-fast -- --test-threads=4: base 1816 passed, 9 failed, 48 ignored; head 1822 passed, 9 failed, 48 ignored. The same 7 tests fail on both (split_compute_prefers_the_offline_whole_pipeline and the two-pass one count twice), and the same 30 SKIPPED lines. All 8 api_cost tests pass.

Not tested: any title, so no XCL_LOOK_STATS=2 lines next to eb784bc's lock-holder times from a real run; AMD.

AI use: Claude Code (Claude Opus 5.5) ran the tests and drafted this; I read it before posting.

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.

6 participants