From c4396e234d38b3ece461a643ff43ff8156cbd135 Mon Sep 17 00:00:00 2001 From: Trey Bertram <113378171+treybertram06@users.noreply.github.com> Date: Tue, 24 Nov 2026 16:11:51 -0800 Subject: [PATCH] D3D12.X: the GPU wait watchdog lists the waits XCL put on queues itself When a bridged Wait64BitValueX times out, the watchdog logs the producer queue's state, but not the queue.Waits XCL adds on its own: texture page mappings (vram::before_execute), sparse uploads and tile mappings, and sparse readbacks. They are not in SYNC_GRAPH, so a producer queue that stands behind one of them looked like a queue with "no tracked wait". On an RTX 3080 10 GB (driver 621.01) with the retail title, every timeout came with the producer queue (and another title queue) waiting for the texture page-mapping fence, about 25 mappings behind after 7.7 s; skipping that one wait in a local experiment took the CPU releases from 4 to 0-1 per run. note_internal_wait records each such wait per queue, and each watchdog round logs the unmet ones next to the pending title fences. The watchdog timeout (5000 ms) can now be changed with XCL_GPU_WAIT_TIMEOUT_MS, like XCL_WAIT_TIMEOUT_MS for the CPU waits. No behaviour change otherwise. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_015JSG96tVb4ZSAZ5WH4v95v --- crates/xcl/src/hle/d3d12x/list_x.rs | 83 +++++++++++++++++++++++++++-- crates/xcl/src/hle/d3d12x/sparse.rs | 2 + crates/xcl/src/hle/d3d12x/vram.rs | 1 + 3 files changed, 83 insertions(+), 3 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/list_x.rs b/crates/xcl/src/hle/d3d12x/list_x.rs index a4bc5d223..c5974ac23 100644 --- a/crates/xcl/src/hle/d3d12x/list_x.rs +++ b/crates/xcl/src/hle/d3d12x/list_x.rs @@ -59,7 +59,8 @@ const MAX_FILL: u64 = 256 << 20; const WAIT_TIMEOUT: Duration = Duration::from_secs(2); /// Watchdog timeout for GPU `queue.Wait` so a genuinely hung GPU cycle never triggers a 30 s /// hang, while giving heavy initial 3D frames (>1 s of dispatches) plenty of room to finish -/// cleanly without prematurely breaking synchronization. +/// cleanly without prematurely breaking synchronization. Overridable via `XCL_GPU_WAIT_TIMEOUT_MS` +/// for diagnostics. const GPU_WAIT_WATCHDOG_TIMEOUT: Duration = Duration::from_millis(5000); /// Cap on tracked per-list written UMA ranges (for same-list write-then-wait detection). const MAX_WRITTEN_RANGES: usize = 256; @@ -76,6 +77,18 @@ fn wait_timeout() -> Duration { }) } +fn gpu_wait_watchdog_timeout() -> Duration { + static T: OnceLock = OnceLock::new(); + *T.get_or_init(|| { + std::env::var("XCL_GPU_WAIT_TIMEOUT_MS") + .ok() + .and_then(|v| v.parse::().ok()) + .filter(|&ms| ms > 0) + .map(Duration::from_millis) + .unwrap_or(GPU_WAIT_WATCHDOG_TIMEOUT) + }) +} + fn once(counter: &AtomicU64, n: u64) -> bool { counter.fetch_add(1, Ordering::Relaxed) < n } @@ -1096,6 +1109,45 @@ static ARMED_GPU_WAITS: AtomicUsize = AtomicUsize::new(0); static SYNC_WATCHDOG_WAKE: OnceLock<(Mutex<()>, Condvar)> = OnceLock::new(); static QUEUE_IDS: Mutex> = Mutex::new(Vec::new()); +/// A `queue.Wait` XCL itself put on a queue (sparse uploads and readbacks, texture page +/// mappings). These are not in `SYNC_GRAPH`; the GPU wait watchdog lists the unmet ones, so a +/// producer queue that stands behind one of them shows in the log. +struct InternalWait { + what: &'static str, + fence: ID3D12Fence, + value: u64, + at: Instant, +} + +static INTERNAL_WAITS: Mutex>>> = Mutex::new(None); + +/// `queue` was made to wait for `fence` >= `value` by XCL (`what` says for what). +pub(super) fn note_internal_wait(queue: usize, what: &'static str, fence: &ID3D12Fence, value: u64) { + let mut g = INTERNAL_WAITS.lock().unwrap(); + let waits = g.get_or_insert_with(HashMap::new).entry(queue).or_default(); + waits.retain(|w| unsafe { w.fence.GetCompletedValue() } < w.value); + waits.push(InternalWait { what, fence: fence.clone(), value, at: Instant::now() }); +} + +/// The internal waits not yet met: `q: >= (done , ms)`. +fn internal_waits_summary() -> String { + let unmet: Vec<(usize, String)> = { + let g = INTERNAL_WAITS.lock().unwrap(); + g.iter() + .flat_map(|m| m.iter()) + .flat_map(|(&q, waits)| waits.iter().map(move |w| (q, w))) + .filter_map(|(q, w)| { + let done = unsafe { w.fence.GetCompletedValue() }; + (done < w.value).then(|| (q, format!("{} >= {:#x} (done {done:#x}, {} ms)", w.what, w.value, w.at.elapsed().as_millis()))) + }) + .collect() + }; + if unmet.is_empty() { + return "none".into(); + } + unmet.into_iter().map(|(q, s)| format!("q{}: {s}", queue_id(q))).collect::>().join(", ") +} + fn queue_id(q: usize) -> usize { let mut g = QUEUE_IDS.lock().unwrap(); if let Some(pos) = g.iter().position(|&x| x == q) { @@ -1402,7 +1454,7 @@ fn wake_sync_watchdog() { let mut waiter_ty = -1i32; let before = cell.armed_waits.len(); cell.armed_waits.retain(|aw| { - if aw.armed_at.elapsed() >= GPU_WAIT_WATCHDOG_TIMEOUT { + if aw.armed_at.elapsed() >= gpu_wait_watchdog_timeout() { if aw.val >= max_exp { max_exp = aw.val; waiter_q = aw.waiting_queue; @@ -1452,11 +1504,12 @@ fn wake_sync_watchdog() { let pq = prod_q.map(queue_id); crate::xlog_warn!( "D3D12.X: GPU queue.Wait at {va:#x} >= {val:#x} did not complete within {} ms (waiter=q{wq}/ty{waiter_ty}, prod=q{pq:?}/ty{prod_ty:?}, recorded={rec:#x}, submitted={sub:#x}, completed={done:#x}) – released on CPU to prevent GPU hang", - GPU_WAIT_WATCHDOG_TIMEOUT.as_millis() + gpu_wait_watchdog_timeout().as_millis() ); } if had_expired { crate::xlog_warn!("D3D12.X: pending title ID3D12Fences during GPU wait timeout: {}", super::fence_x::pending_summary()); + crate::xlog_warn!("D3D12.X: XCL's own queue waits unmet during GPU wait timeout: {}", internal_waits_summary()); } }); pair @@ -4073,6 +4126,30 @@ mod tests { use super::*; use std::sync::Arc; + /// An internal wait shows in the watchdog's summary while its fence is short of the value, + /// and is gone from it once the fence gets there. + #[test] + fn an_internal_wait_is_listed_until_its_fence_reaches_the_value() { + use windows::Win32::Graphics::Direct3D::D3D_FEATURE_LEVEL_11_0; + + unsafe { + let mut dev: Option = None; + if D3D12CreateDevice(None, D3D_FEATURE_LEVEL_11_0, &mut dev).is_err() { + crate::gpu::recomp::runner::skip_test("no GPU"); + return; + } + let dev = dev.unwrap(); + let fence: ID3D12Fence = dev.CreateFence(0, D3D12_FENCE_FLAG_NONE).unwrap(); + let queue = fence.as_raw() as usize; // any address no real queue has + note_internal_wait(queue, "test wait 7d1c", &fence, 3); + let s = internal_waits_summary(); + assert!(s.contains("test wait 7d1c >= 0x3 (done 0x0"), "{s}"); + fence.Signal(3).unwrap(); + let s = internal_waits_summary(); + assert!(!s.contains("test wait 7d1c"), "{s}"); + } + } + #[test] fn copy_scratch_budget_counts_resources_until_gpu_completion() { use windows::Win32::Graphics::Direct3D::D3D_FEATURE_LEVEL_11_0; diff --git a/crates/xcl/src/hle/d3d12x/sparse.rs b/crates/xcl/src/hle/d3d12x/sparse.rs index 0731890bf..70af50285 100644 --- a/crates/xcl/src/hle/d3d12x/sparse.rs +++ b/crates/xcl/src/hle/d3d12x/sparse.rs @@ -1256,6 +1256,7 @@ pub unsafe fn readback_after_execute(queue: &ID3D12CommandQueue, ranges: &[(usiz let v = s.rb_value; if queue.Signal(&s.rb_fence, v).is_ok() { let _ = s.queue.Wait(&s.rb_fence, v); + super::list_x::note_internal_wait(s.queue.as_raw() as usize, "sparse readback behind a title queue", &s.rb_fence, v); } s.readback_ranges(&merged); } @@ -1538,6 +1539,7 @@ pub unsafe fn before_execute(queue: &ID3D12CommandQueue, dirty: &[usize], page: let q = queue.as_raw() as usize; if s.value > 0 && s.waited.get(&q).copied().unwrap_or(0) < s.value { let _ = queue.Wait(&s.fence, s.value); + super::list_x::note_internal_wait(q, "sparse uploads and tile mappings", &s.fence, s.value); s.waited.insert(q, s.value); } } diff --git a/crates/xcl/src/hle/d3d12x/vram.rs b/crates/xcl/src/hle/d3d12x/vram.rs index 740b69ef9..5a89e9088 100644 --- a/crates/xcl/src/hle/d3d12x/vram.rs +++ b/crates/xcl/src/hle/d3d12x/vram.rs @@ -840,6 +840,7 @@ pub(super) unsafe fn before_execute(queue: &ID3D12CommandQueue) { let key = queue.as_raw() as usize; if pages.waited.get(&key).copied().unwrap_or(0) < pages.value { let _ = queue.Wait(&pages.fence, pages.value); + super::list_x::note_internal_wait(key, "texture page mappings", &pages.fence, pages.value); pages.waited.insert(key, pages.value); } }