Skip to content
Merged
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
83 changes: 80 additions & 3 deletions crates/xcl/src/hle/d3d12x/list_x.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -76,6 +77,18 @@ fn wait_timeout() -> Duration {
})
}

fn gpu_wait_watchdog_timeout() -> Duration {
static T: OnceLock<Duration> = OnceLock::new();
*T.get_or_init(|| {
std::env::var("XCL_GPU_WAIT_TIMEOUT_MS")
.ok()
.and_then(|v| v.parse::<u64>().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
}
Expand Down Expand Up @@ -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<Vec<usize>> = 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<Option<HashMap<usize, Vec<InternalWait>>>> = 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<id>: <what> >= <value> (done <completed>, <age> 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::<Vec<_>>().join(", ")
}

fn queue_id(q: usize) -> usize {
let mut g = QUEUE_IDS.lock().unwrap();
if let Some(pos) = g.iter().position(|&x| x == q) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<ID3D12Device> = 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;
Expand Down
2 changes: 2 additions & 0 deletions crates/xcl/src/hle/d3d12x/sparse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/xcl/src/hle/d3d12x/vram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
Loading