Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
151 changes: 136 additions & 15 deletions crates/xcl/src/hle/d3d12x/list_x.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(|| {
Expand All @@ -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
}
Expand Down Expand Up @@ -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,
}

Expand Down Expand Up @@ -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;

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.

Build holds stay uncapped and global (HELD counts any bind or submit), and any build hold resets cap_from for 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()) keeps held_for_build() true forever. The same happens if any other title thread waits on a build at least once every 20 s: cap_from keeps moving and the stuck wait never expires.

}
let pc_call_pauses = pc_call_held && cap.map_or(true, |cap| now.saturating_duration_since(aw.cap_from) < cap);

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.

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 PcCall doc in pso_async.rs (~3457) records a retail title placing 16 384 textures in 29 s without a frame. A GPU wait armed near the start of that load stops pausing at 20 s and expires at about 25 s, and with XCL_WAIT_TIMEOUT_CONTINUE unset main calls stop_for_unresolved_wait. A load that used to finish now ends the title.

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)]
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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] });
Expand Down Expand Up @@ -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 }],
Expand Down Expand Up @@ -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 }],
Expand All @@ -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()
Expand Down Expand Up @@ -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 }],
Expand All @@ -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 }],
Expand All @@ -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()
Expand Down Expand Up @@ -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 }],
Expand All @@ -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 }],
Expand Down Expand Up @@ -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()
},
Expand Down Expand Up @@ -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]
Expand All @@ -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,
Expand All @@ -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};
Expand Down
10 changes: 8 additions & 2 deletions crates/xcl/src/hle/d3d12x/pso_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

Expand Down
Loading