Repository navigation
D3D12.X: a long PC call pauses the GPU wait watchdog only up to a cap #3305
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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<Duration> = OnceLock::new(); | ||
| *T.get_or_init(|| { | ||
|
|
@@ -89,6 +93,17 @@ fn gpu_wait_watchdog_timeout() -> Duration { | |
| }) | ||
| } | ||
|
|
||
| fn held_wait_cap() -> Option<Duration> { | ||
| static T: OnceLock<Option<Duration>> = 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<Duration> { | ||
| v.and_then(|v| v.parse::<u64>().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<Duration>) -> 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); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The cap counts from when the wait was armed, so a known, legitimate long run of PC calls without a frame now stops the title by default. The |
||
| 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<ExpiredGpuWait> { | ||
| fn take_expired_gpu_wait(cell: &mut SyncCell, build_held: bool, pc_call_held: bool) -> Option<ExpiredGpuWait> { | ||
| 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}; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Build holds stay uncapped and global (
HELDcounts any bind or submit), and any build hold resetscap_fromfor every armed wait, so the targeted freeze can still be permanent. In the described deadlock (a driver call holding the D3D12Core lock while waiting on the GPU), a pipeline worker's Create*PipelineState can block on the same lock. The title bind waiting on it (build_foreground->Held::new()) keepsheld_for_build()true forever. The same happens if any other title thread waits on a build at least once every 20 s:cap_fromkeeps moving and the stuck wait never expires.