From a0519dfdc862c79d6921cb71dffa76a81c72f9b7 Mon Sep 17 00:00:00 2001 From: grahamster2 <128315603+grahamster2@users.noreply.github.com> Date: Sat, 28 Nov 2026 16:43:27 -0500 Subject: [PATCH] D3D12.X: a long PC call pauses the GPU wait watchdog only up to a cap wait_expired reset an armed bridged wait's clock on every watchdog tick while pso_async::title_held() was true, and a hold can depend on the very GPU wait it pauses. Seen frozen for good: a title thread in execute_indirect -> split -> CreateCommandList blocked in the driver (D3DKMTDestroyAllocation2) waiting on the GPU while holding a D3D12Core lock; the submit thread inside q_execute_command_lists (a pc_call, so the title stays held) blocked on that lock in alloc_pool::reset_list; the GPU stuck behind one armed wait the watchdog never released. The two kinds of hold are told apart now (pso_async::held_for_build, held_by_pc_call). A hold for a pipeline build still pauses the clock for as long as it lasts. A hold by a long PC call - the kind that can wait on the GPU, as in the freeze above - pauses it only until cap_from (new in ArmedGpuWait: when the wait was armed, moved on while it is held for a build) + the cap (XCL_HELD_WAIT_CAP_MS, default 20000, 0 = no cap); after that the wait expires on the normal timeout. Seen on an RTX 3080, stacks from an external sampler: on a branch from before "fail closed on unproven wait timeouts" the wait was released 25 s after it was armed and rendering resumed. On main an expired wait without a proven cycle stops the title (stop_for_unresolved_wait) unless XCL_WAIT_TIMEOUT_CONTINUE=1, so here the cap turns the permanent freeze into that stop, or with XCL_WAIT_TIMEOUT_CONTINUE=1 into the release seen in that run. A PC-call hold longer than the cap in a long load now ends the same way. README documents XCL_HELD_WAIT_CAP_MS next to the other wait timeouts. Assisted-by: Claude Code (Claude Opus 5.5) --- README.md | 6 + crates/xcl/src/hle/d3d12x/list_x.rs | 151 ++++++++++++++++++++++--- crates/xcl/src/hle/d3d12x/pso_async.rs | 10 +- 3 files changed, 150 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index d55abf4300..67060bfc60 100644 --- a/README.md +++ b/README.md @@ -350,6 +350,12 @@ Fill the reusable DXIL cache ahead of a run with the read-only package wrapper. watchdog for a GPU queue wait (5000 ms). A value that is not a number above 0 keeps the default. Expiry stops the title before later GPU work is submitted; it never treats elapsed time as proof that the waited-for work completed. + - `XCL_HELD_WAIT_CAP_MS`: while a long PC call of the title (a submission, a texture placement) + holds it, the GPU queue wait watchdog stands, but only until this long after the wait was + armed or last held for a pipeline build (default 20000 ms; `0` = no cap). Past that the wait + expires on the watchdog above (so about 25 s with the defaults), which stops the title + unless `XCL_WAIT_TIMEOUT_CONTINUE=1`. A wait for a pipeline still being built pauses the + watchdog without a cap. - Retired two-pass mesh buffers from tracked command lists can be reused once all GPU submissions that used them complete, even while the title still holds the command allocator. - Direct two-pass mesh draws can reuse one smaller record buffer across several slices when diff --git a/crates/xcl/src/hle/d3d12x/list_x.rs b/crates/xcl/src/hle/d3d12x/list_x.rs index 0210c522f1..32bf701aac 100644 --- a/crates/xcl/src/hle/d3d12x/list_x.rs +++ b/crates/xcl/src/hle/d3d12x/list_x.rs @@ -65,6 +65,10 @@ const WAIT_TIMEOUT: Duration = Duration::from_secs(2); /// cleanly without prematurely breaking synchronization. Overridable via `XCL_GPU_WAIT_TIMEOUT_MS` /// for diagnostics. const GPU_WAIT_WATCHDOG_TIMEOUT: Duration = Duration::from_millis(5000); +/// How long after it was first armed a title hold may keep pausing a GPU wait's watchdog clock. +/// Overridable via `XCL_HELD_WAIT_CAP_MS`; `0` = no cap. +const HELD_WAIT_CAP: Duration = Duration::from_millis(20000); + fn wait_timeout() -> Duration { static T: OnceLock = OnceLock::new(); *T.get_or_init(|| { @@ -89,6 +93,17 @@ fn gpu_wait_watchdog_timeout() -> Duration { }) } +fn held_wait_cap() -> Option { + static T: OnceLock> = OnceLock::new(); + *T.get_or_init(|| parse_held_wait_cap(std::env::var("XCL_HELD_WAIT_CAP_MS").ok().as_deref())) +} + +/// `XCL_HELD_WAIT_CAP_MS`: unset or not a number = `HELD_WAIT_CAP`, `0` = no cap. +fn parse_held_wait_cap(v: Option<&str>) -> Option { + v.and_then(|v| v.parse::().ok()) + .map_or(Some(HELD_WAIT_CAP), |ms| (ms > 0).then(|| Duration::from_millis(ms))) +} + fn once(counter: &AtomicU64, n: u64) -> bool { counter.fetch_add(1, Ordering::Relaxed) < n } @@ -2137,6 +2152,10 @@ struct ArmedGpuWait { /// Per-queue submission sequence number at which this `Wait` was issued on `waiting_queue`. wait_seq: u64, armed_at: Instant, + /// Where `held_wait_cap()` of a PC-call hold counts from: when the wait was armed, moved on + /// while the title is held for a pipeline build (`wait_expired_at`). `armed_at` is moved on + /// by either hold. + cap_from: Instant, after_write64: bool, } @@ -2855,12 +2874,39 @@ pub(super) unsafe fn break_cycles_before_title_signal(sig_q: usize, title_fence: } } -/// Whether an armed GPU wait has outlasted the watchdog; its clock stands while a submit or bind is held for a pipeline build. -fn wait_expired(aw: &mut ArmedGpuWait, held: bool) -> bool { - if held { - aw.armed_at = Instant::now(); +/// Whether an armed GPU wait has outlasted the watchdog. Its clock stands while the title is held. +/// A hold for a pipeline build (`build_held`: a submit or bind waiting for one) pauses it for as +/// long as it lasts. A hold by a long PC call (`pc_call_held`) pauses it only until +/// `held_wait_cap()` after the wait was armed or last held for a build (`cap_from`): such a call +/// can itself depend on this GPU wait (a driver call blocked on the GPU), and without the cap that +/// would freeze the title forever. Past the cap that hold no longer resets the clock, so the wait +/// expires on the normal timeout (cap + timeout after `cap_from` at the earliest). +fn wait_expired(aw: &mut ArmedGpuWait, build_held: bool, pc_call_held: bool) -> bool { + let now = Instant::now(); + let expired = wait_expired_at(aw, build_held, pc_call_held, now, gpu_wait_watchdog_timeout(), held_wait_cap()); + if expired && pc_call_held { + static HELD_CAP_WARN: AtomicU64 = AtomicU64::new(0); + if once(&HELD_CAP_WARN, 8) { + crate::xlog_warn!( + "D3D12.X: GPU wait for {:#x} expires although a PC call holds the title: {} ms since it was armed or last held for a build, past the hold cap", + aw.val, + now.duration_since(aw.cap_from).as_millis() + ); + } + } + expired +} + +fn wait_expired_at(aw: &mut ArmedGpuWait, build_held: bool, pc_call_held: bool, now: Instant, timeout: Duration, cap: Option) -> bool { + if build_held { + // (time held for a build does not count towards the PC-call cap) + aw.cap_from = now; + } + let pc_call_pauses = pc_call_held && cap.map_or(true, |cap| now.saturating_duration_since(aw.cap_from) < cap); + if build_held || pc_call_pauses { + aw.armed_at = now; } - aw.armed_at.elapsed() >= gpu_wait_watchdog_timeout() + now.saturating_duration_since(aw.armed_at) >= timeout } #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -2872,10 +2918,10 @@ struct ExpiredGpuWait { /// Remove elapsed waits for diagnostics without advancing any fence state. An elapsed wall-clock /// timeout does not prove that the producer's work completed and must never synthesize completion. -fn take_expired_gpu_wait(cell: &mut SyncCell, held: bool) -> Option { +fn take_expired_gpu_wait(cell: &mut SyncCell, build_held: bool, pc_call_held: bool) -> Option { let mut expired = None; cell.armed_waits.retain_mut(|aw| { - if !wait_expired(aw, held) { + if !wait_expired(aw, build_held, pc_call_held) { return true; } if expired.is_none_or(|old: ExpiredGpuWait| aw.val >= old.val) { @@ -2968,8 +3014,10 @@ fn wake_sync_watchdog() { continue; } let before = cell.armed_waits.len(); - let held = super::pso_async::title_held(); - let expired = take_expired_gpu_wait(cell, held); + let build_held = super::pso_async::held_for_build(); + // (a build hold pauses the clock anyway: no need to look at the PC calls) + let pc_call_held = !build_held && super::pso_async::held_by_pc_call(); + let expired = take_expired_gpu_wait(cell, build_held, pc_call_held); let removed = before - cell.armed_waits.len(); if removed > 0 { ARMED_GPU_WAITS.fetch_sub(removed, Ordering::AcqRel); @@ -3416,6 +3464,7 @@ unsafe fn try_gpu_wait(queue: *mut c_void, w: &PendingWait, list_writes: &[(u64, waiting_queue_ty: q_ty.0, wait_seq, armed_at: Instant::now(), + cap_from: Instant::now(), after_write64: w.after_write64, }); ARMED_GPU_WAITS.fetch_add(1, Ordering::AcqRel); @@ -8745,6 +8794,7 @@ mod tests { waiting_queue_ty: 0, wait_seq: 1, armed_at: Instant::now(), + cap_from: Instant::now(), after_write64: false, }); cell.armed_waits.push(ArmedGpuWait { waiting_queue: kept, ..cell.armed_waits[0] }); @@ -10300,6 +10350,7 @@ mod tests { waiting_queue_ty: D3D12_COMMAND_LIST_TYPE_DIRECT.0, wait_seq: 1, armed_at: Instant::now(), + cap_from: Instant::now(), after_write64: false, }], submitted_signals: vec![SubmittedSignal { val: 0x50, queue: qb, seq: 2 }], @@ -10374,6 +10425,7 @@ mod tests { waiting_queue_ty: D3D12_COMMAND_LIST_TYPE_DIRECT.0, wait_seq: 1, armed_at: Instant::now(), + cap_from: Instant::now(), after_write64: false, }], submitted_signals: vec![SubmittedSignal { val: 10, queue: 0xC001, seq: 2 }], @@ -10392,6 +10444,7 @@ mod tests { waiting_queue_ty: D3D12_COMMAND_LIST_TYPE_COMPUTE.0, wait_seq: 3, // AFTER q0's signal of A (seq=2)! armed_at: Instant::now(), + cap_from: Instant::now(), after_write64: false, }], ..Default::default() @@ -10435,6 +10488,7 @@ mod tests { waiting_queue_ty: D3D12_COMMAND_LIST_TYPE_DIRECT.0, wait_seq: 1, armed_at: Instant::now(), + cap_from: Instant::now(), after_write64: false, }], submitted_signals: vec![SubmittedSignal { val: 0x78d, queue: 0xC001, seq: 2 }], @@ -10455,6 +10509,7 @@ mod tests { waiting_queue_ty: D3D12_COMMAND_LIST_TYPE_COMPUTE.0, wait_seq: 1, armed_at: Instant::now(), + cap_from: Instant::now(), after_write64: false, }], submitted_signals: vec![SubmittedSignal { val: 0x78d, queue: 0xC002, seq: 2 }], @@ -10473,6 +10528,7 @@ mod tests { waiting_queue_ty: D3D12_COMMAND_LIST_TYPE_COMPUTE.0, wait_seq: 1, armed_at: Instant::now(), + cap_from: Instant::now(), after_write64: false, }], ..Default::default() @@ -10511,6 +10567,7 @@ mod tests { waiting_queue_ty: D3D12_COMMAND_LIST_TYPE_DIRECT.0, wait_seq: 3544, armed_at: Instant::now(), + cap_from: Instant::now(), after_write64: false, }], submitted_signals: vec![SubmittedSignal { val: 0x6db, queue: 0xC001, seq: 16 }], @@ -10531,6 +10588,7 @@ mod tests { waiting_queue_ty: D3D12_COMMAND_LIST_TYPE_COMPUTE.0, wait_seq: 34, armed_at: Instant::now(), + cap_from: Instant::now(), after_write64: false, }], submitted_signals: vec![SubmittedSignal { val: 0x6dc, queue: 0xD001, seq: 3572 }], @@ -10635,7 +10693,7 @@ mod tests { recorded_max: 9, submitted_max: 9, submitted_queue: Some(q0), - armed_waits: vec![ArmedGpuWait { val: 9, waiting_queue: q1, waiting_queue_ty: D3D12_COMMAND_LIST_TYPE_COMPUTE.0, wait_seq: 4, armed_at: Instant::now(), after_write64: false }], + armed_waits: vec![ArmedGpuWait { val: 9, waiting_queue: q1, waiting_queue_ty: D3D12_COMMAND_LIST_TYPE_COMPUTE.0, wait_seq: 4, armed_at: Instant::now(), cap_from: Instant::now(), after_write64: false }], submitted_signals: vec![SubmittedSignal { val: 9, queue: q0, seq: 7 }], ..Default::default() }, @@ -10920,11 +10978,11 @@ mod tests { #[test] fn a_gpu_wait_does_not_expire_while_the_title_is_held_for_a_build() { let old = Instant::now() - gpu_wait_watchdog_timeout() * 2; - let mut aw = ArmedGpuWait { val: 1, waiting_queue: 0, waiting_queue_ty: 0, wait_seq: 1, armed_at: old, after_write64: false }; - assert!(!wait_expired(&mut aw, true)); - assert!(!wait_expired(&mut aw, false), "its clock starts again when the hold ends"); + let mut aw = ArmedGpuWait { val: 1, waiting_queue: 0, waiting_queue_ty: 0, wait_seq: 1, armed_at: old, cap_from: Instant::now(), after_write64: false }; + assert!(!wait_expired(&mut aw, true, false)); + assert!(!wait_expired(&mut aw, false, false), "its clock starts again when the hold ends"); aw.armed_at = old; - assert!(wait_expired(&mut aw, false)); + assert!(wait_expired(&mut aw, false, false)); } #[test] @@ -10939,13 +10997,14 @@ mod tests { waiting_queue_ty: D3D12_COMMAND_LIST_TYPE_COMPUTE.0, wait_seq: 2, armed_at: old, + cap_from: old, after_write64: false, }], submitted_signals: vec![SubmittedSignal { val: 9, queue: 0x7100, seq: 1 }], ..Default::default() }; - let expired = take_expired_gpu_wait(&mut cell, false).expect("the elapsed wait"); + let expired = take_expired_gpu_wait(&mut cell, false, false).expect("the elapsed wait"); assert_eq!(expired, ExpiredGpuWait { val: 9, waiting_queue: 0x7000, @@ -10957,6 +11016,68 @@ mod tests { assert_eq!(cell.submitted_signals[0].val, 9); } + #[test] + fn wait_expired_a_pc_call_hold_pauses_the_gpu_wait_watchdog_only_up_to_the_cap() { + let timeout = Duration::from_secs(5); + let cap = Some(Duration::from_secs(20)); + // Times from a fixed base forward, so nothing is subtracted from `Instant::now()`. + let older = Instant::now(); + let old = older + Duration::from_secs(20); + let now = older + Duration::from_secs(30); + let wait = |armed_at, cap_from| ArmedGpuWait { val: 1, waiting_queue: 0, waiting_queue_ty: 0, wait_seq: 1, armed_at, cap_from, after_write64: false }; + + let mut aw = wait(old, old); + assert!(!wait_expired_at(&mut aw, false, true, now, timeout, cap), "PC-call hold under the cap: the clock stands"); + assert!(aw.armed_at == now, "and starts again from now"); + + let mut aw = wait(old, older); + assert!(wait_expired_at(&mut aw, false, true, now, timeout, cap), "PC-call hold past the cap: the hold no longer resets the clock"); + assert!(aw.armed_at == old); + + let mut aw = wait(old, old); + assert!(wait_expired_at(&mut aw, false, false, now, timeout, cap), "not held: expires on the timeout"); + + let mut aw = wait(old, older); + assert!(!wait_expired_at(&mut aw, false, true, now, timeout, None), "no cap: a PC-call hold pauses the clock for good"); + } + + #[test] + fn wait_expired_a_build_hold_pauses_the_gpu_wait_watchdog_past_the_cap() { + let timeout = Duration::from_secs(5); + let cap = Some(Duration::from_secs(20)); + let older = Instant::now(); + let old = older + Duration::from_secs(20); + let now = older + Duration::from_secs(300); + let wait = |armed_at, cap_from| ArmedGpuWait { val: 1, waiting_queue: 0, waiting_queue_ty: 0, wait_seq: 1, armed_at, cap_from, after_write64: false }; + + let mut aw = wait(old, older); + assert!(!wait_expired_at(&mut aw, true, false, now, timeout, cap), "a build hold never lets a wait expire early"); + assert!(aw.armed_at == now); + let mut aw = wait(old, older); + assert!(!wait_expired_at(&mut aw, true, true, now, timeout, cap), "a build hold with a PC-call hold past the cap: still paused"); + let mut aw = wait(old, older); + assert!(wait_expired_at(&mut aw, false, false, now, timeout, cap), "the build hold ended: expires on the timeout"); + + // A PC-call hold after a long build hold gets the whole cap from the build hold's end. + let mut aw = wait(old, older); + assert!(!wait_expired_at(&mut aw, true, false, now, timeout, cap)); + assert!(aw.cap_from == now, "the cap counts from the build hold"); + let s = Duration::from_secs; + assert!(!wait_expired_at(&mut aw, false, true, now + s(10), timeout, cap), "PC-call hold 10 s after the build hold: paused"); + assert!(!wait_expired_at(&mut aw, false, true, now + s(19), timeout, cap)); + assert!(!wait_expired_at(&mut aw, false, true, now + s(21), timeout, cap), "past the cap the clock runs again from its last reset"); + assert!(wait_expired_at(&mut aw, false, true, now + s(24), timeout, cap), "and the wait expires on the timeout"); + } + + #[test] + fn held_wait_cap_parses_unset_zero_junk_and_a_value() { + assert_eq!(parse_held_wait_cap(None), Some(HELD_WAIT_CAP)); + assert_eq!(parse_held_wait_cap(Some("0")), None, "0 = no cap"); + assert_eq!(parse_held_wait_cap(Some("junk")), Some(HELD_WAIT_CAP)); + assert_eq!(parse_held_wait_cap(Some("")), Some(HELD_WAIT_CAP)); + assert_eq!(parse_held_wait_cap(Some("1500")), Some(Duration::from_millis(1500))); + } + #[test] fn a_dispatch_with_a_pipeline_still_building_waits_at_the_submit() { use super::super::{bind, pso_async, title_list}; diff --git a/crates/xcl/src/hle/d3d12x/pso_async.rs b/crates/xcl/src/hle/d3d12x/pso_async.rs index df9fabd0da..0f6f6e0558 100644 --- a/crates/xcl/src/hle/d3d12x/pso_async.rs +++ b/crates/xcl/src/hle/d3d12x/pso_async.rs @@ -3438,10 +3438,16 @@ const PC_CALL_HOLDS_AFTER: std::time::Duration = std::time::Duration::from_milli /// waiting for a pipeline still being built, or a PC call is going on after such calls have /// kept the title's threads for long (`pc_call`). `kernelx::Sleep` does not count that time. pub fn title_held() -> bool { - HELD.load(Ordering::Acquire) != 0 || held_by_pc_call() + held_for_build() || held_by_pc_call() } -fn held_by_pc_call() -> bool { +/// Whether a bind or submit of the title is waiting right now for a pipeline still being built. +pub fn held_for_build() -> bool { + HELD.load(Ordering::Acquire) != 0 +} + +/// Whether a PC call going on holds the title (`pc_call`). Such a call can wait on the GPU itself. +pub fn held_by_pc_call() -> bool { PC_CALLS.lock().unwrap().holds(super::present::presents(), std::time::Instant::now()) }