From 06f9df8394dc34d5e8f55371525559bdb88399b9 Mon Sep 17 00:00:00 2001 From: Marco Date: Wed, 25 Nov 2026 11:51:15 +0100 Subject: [PATCH 1/2] xcl run: XCL_THREAD_DUMP_AT also logs the fences and queue waits behind A retail title's world load can stop with no GPU-wait time-out: presents, submits and DirectStorage requests go to 0 and every thread waits. The watchdog's summaries of pending title fences and of XCL's own unmet queue waits are only logged at a time-out, so such a stop did not say whether a fence or a queue was behind. At each XCL_THREAD_DUMP_AT dump XCL now logs the two summaries and the number of armed GPU waits, on a thread of its own so that a lock the stall holds cannot hold up the next dump. On #2057's tree this showed every fence and queue caught up at 135, 200 and 300 s after a stop at 110 s (https://github.com/xcl-dev/xcl/pull/2057#issuecomment-7160090884). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01XZrhsCAiLU1tTuEBwPLaRW --- crates/xcl/src/hle/d3d12x/list_x.rs | 12 ++++++++++++ crates/xcl/src/host.rs | 4 +++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/crates/xcl/src/hle/d3d12x/list_x.rs b/crates/xcl/src/hle/d3d12x/list_x.rs index 096620f6fa..f871a5bd54 100644 --- a/crates/xcl/src/hle/d3d12x/list_x.rs +++ b/crates/xcl/src/hle/d3d12x/list_x.rs @@ -1192,6 +1192,18 @@ fn internal_waits_summary() -> String { unmet.into_iter().map(|(q, s)| format!("q{}: {s}", queue_id(q))).collect::>().join(", ") } +/// What the GPU wait watchdog logs at a time-out, logged on request (`XCL_THREAD_DUMP_AT`), so a +/// title that stops with no time-out shows whether a fence or a queue is behind. On a thread of +/// its own: a lock the stall holds does not hold up the next thread dump. +pub fn log_sync_state(at: &str) { + let at = at.to_string(); + let _ = std::thread::Builder::new().name("xcl-sync-state".into()).spawn(move || { + crate::xlog_info!("D3D12.X: sync state at {at}: pending title ID3D12Fences: {}", super::fence_x::pending_summary()); + crate::xlog_info!("D3D12.X: sync state at {at}: XCL's own queue waits unmet: {}", internal_waits_summary()); + crate::xlog_info!("D3D12.X: sync state at {at}: {} GPU waits armed", ARMED_GPU_WAITS.load(Ordering::Acquire)); + }); +} + fn queue_id(q: usize) -> usize { let mut g = QUEUE_IDS.lock().unwrap(); if let Some(pos) = g.iter().position(|&x| x == q) { diff --git a/crates/xcl/src/host.rs b/crates/xcl/src/host.rs index 444333d891..6bd5fbe0cc 100644 --- a/crates/xcl/src/host.rs +++ b/crates/xcl/src/host.rs @@ -88,7 +88,8 @@ pub fn host_main(boot_path: &Path) -> i32 { } }); } - // `XCL_THREAD_DUMP_AT=s1,s2…`: where every thread is at those seconds (a stalled title) + // `XCL_THREAD_DUMP_AT=s1,s2…`: where every thread is at those seconds (a stalled title), and + // which title fences and XCL queue waits are behind then if let Ok(v) = std::env::var("XCL_THREAD_DUMP_AT") { let at: Vec = v.split(',').filter_map(|s| s.trim().parse().ok()).collect(); let _ = std::thread::Builder::new().name("xcl-thread-dump".into()).spawn(move || { @@ -99,6 +100,7 @@ pub fn host_main(boot_path: &Path) -> i32 { std::thread::sleep(std::time::Duration::from_secs_f64(left)); } crate::eh::dump_threads(); + crate::hle::d3d12x::list_x::log_sync_state(&format!("{s} s")); } }); } From 97e22f64949d02d4cbbcce2bc6f0e2a703a09166 Mon Sep 17 00:00:00 2001 From: Marco Date: Wed, 25 Nov 2026 20:32:56 +0100 Subject: [PATCH 2/2] xcl run: XCL_THREAD_DUMP_AT also logs the Wait64 cells behind The three lines of the first commit read "none / none / 0" in runs where the title had stopped after its first #101: no fence and no queue was behind, yet nothing was presented. What showed the stop was a SYNC_GRAPH cell whose recorded value is above what was submitted and completed, with no GPU wait armed: a list that writes the value was recorded and never submitted, so the submitting thread is held (GreemSC's runs, RX 7900 XT; the thread dump then named that thread). cells_behind_summary lists such cells, and those with armed waits: memory, completed, submitted (and on which queue), recorded and CPU values, and the armed waits with their age. It takes the graph with try_lock, since a dump can come while a submitting thread holds it. The cell is read with ReadProcessMemory (dstorage::read_own), since the title can decommit its page at any time; an unreadable cell shows "mem unreadable" (bmjask's review). The function is GreemSC's, from the review of this PR. Co-authored-by: GreemSC <57862153+GreemSC@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01XZrhsCAiLU1tTuEBwPLaRW --- crates/xcl/src/hle/d3d12x/list_x.rs | 82 ++++++++++++++++++++++++++++- crates/xcl/src/hle/dstorage.rs | 2 +- 2 files changed, 81 insertions(+), 3 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/list_x.rs b/crates/xcl/src/hle/d3d12x/list_x.rs index f871a5bd54..774d347b8c 100644 --- a/crates/xcl/src/hle/d3d12x/list_x.rs +++ b/crates/xcl/src/hle/d3d12x/list_x.rs @@ -1192,15 +1192,53 @@ fn internal_waits_summary() -> String { unmet.into_iter().map(|(q, s)| format!("q{}: {s}", queue_id(q))).collect::>().join(", ") } +/// The `Wait64BitValueX` cells not yet at their highest recorded value, or with armed GPU waits: +/// `rec` above `sub` with nothing armed is a list that writes the value, recorded and never +/// submitted (its submitting thread is held). `try_lock`: a dump can come while a submitting +/// thread holds the graph. (@GreemSC's diagnostic, from #2244's review.) +fn cells_behind_summary() -> String { + let mut parts = Vec::new(); + if let Ok(g) = SYNC_GRAPH.try_lock() { + for (&va, c) in g.iter().flat_map(|gr| gr.cells.iter()) { + let done = c.fence.as_ref().map_or(0, |f| unsafe { f.GetCompletedValue() }); + if c.armed_waits.is_empty() && done >= c.recorded_max { + continue; + } + // (the title can decommit the cell's page at any moment: a read that cannot fault) + let mut v = 0u64; + let mem = if unsafe { crate::hle::dstorage::read_own(va as usize, &mut v as *mut u64 as *mut u8, 8) } { format!("{v:#x}") } else { "unreadable".into() }; + let armed: Vec = c.armed_waits.iter().map(|a| format!("{:#x}@q{} {} ms", a.val, queue_id(a.waiting_queue), a.armed_at.elapsed().as_millis())).collect(); + parts.push(format!( + "{va:#x}: mem {mem} done {done:#x} sub {:#x}@q{:?} rec {:#x} cpu {:#x}{} armed [{}]", + c.submitted_max, + c.submitted_queue.map(queue_id), + c.recorded_max, + c.cpu_signaled_max, + if c.non_monotonic { " non-monotonic" } else { "" }, + armed.join(", ") + )); + } + } else { + parts.push("(sync graph locked)".into()); + } + if parts.is_empty() { + "none".into() + } else { + parts.join("; ") + } +} + /// What the GPU wait watchdog logs at a time-out, logged on request (`XCL_THREAD_DUMP_AT`), so a -/// title that stops with no time-out shows whether a fence or a queue is behind. On a thread of -/// its own: a lock the stall holds does not hold up the next thread dump. +/// title that stops with no time-out shows whether a fence or a queue is behind, and which +/// `Wait64BitValueX` cells are behind what was recorded for them. On a thread of its own: a lock +/// the stall holds does not hold up the next thread dump. pub fn log_sync_state(at: &str) { let at = at.to_string(); let _ = std::thread::Builder::new().name("xcl-sync-state".into()).spawn(move || { crate::xlog_info!("D3D12.X: sync state at {at}: pending title ID3D12Fences: {}", super::fence_x::pending_summary()); crate::xlog_info!("D3D12.X: sync state at {at}: XCL's own queue waits unmet: {}", internal_waits_summary()); crate::xlog_info!("D3D12.X: sync state at {at}: {} GPU waits armed", ARMED_GPU_WAITS.load(Ordering::Acquire)); + crate::xlog_info!("D3D12.X: sync state at {at}: Wait64 cells behind: {}", cells_behind_summary()); }); } @@ -4430,6 +4468,46 @@ mod tests { use super::*; use std::sync::Arc; + /// A recorded `Wait64` cell whose page the title decommitted, or made inaccessible, is listed + /// as unreadable by the sync-state dump, which does not fault on it. + #[test] + fn the_cells_behind_summary_reads_a_decommitted_or_protected_cell_without_faulting() { + use windows_sys::Win32::System::Memory::{VirtualAlloc, VirtualFree, VirtualProtect, MEM_COMMIT, MEM_DECOMMIT, MEM_RELEASE, MEM_RESERVE, PAGE_NOACCESS, PAGE_READWRITE}; + unsafe { + let mem = VirtualAlloc(std::ptr::null(), 0x2000, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE) as usize; + assert_ne!(mem, 0); + let (decommitted, protected) = (mem as u64, (mem + 0x1000) as u64); + *(decommitted as *mut u64) = 7; + *(protected as *mut u64) = 7; + { + let mut g = SYNC_GRAPH.lock().unwrap(); + let gr = g.get_or_insert_with(Default::default); + for va in [decommitted, protected] { + gr.cells.insert(va, SyncCell { recorded_max: 9, ..Default::default() }); + } + } + assert_ne!(VirtualFree(mem as *mut c_void, 0x1000, MEM_DECOMMIT), 0); + let mut old = 0; + assert_ne!(VirtualProtect((mem + 0x1000) as *const c_void, 0x1000, PAGE_NOACCESS, &mut old), 0); + // (other tests take the graph too: the summary only tries the lock) + let mut s = cells_behind_summary(); + for _ in 0..200 { + if !s.contains("(sync graph locked)") { + break; + } + std::thread::sleep(std::time::Duration::from_millis(5)); + s = cells_behind_summary(); + } + if let Some(gr) = SYNC_GRAPH.lock().unwrap().as_mut() { + gr.cells.remove(&decommitted); + gr.cells.remove(&protected); + } + VirtualFree(mem as *mut c_void, 0, MEM_RELEASE); + assert!(s.contains(&format!("{decommitted:#x}: mem unreadable done 0x0")), "{s}"); + assert!(s.contains(&format!("{protected:#x}: mem unreadable done 0x0")), "{s}"); + } + } + /// 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] diff --git a/crates/xcl/src/hle/dstorage.rs b/crates/xcl/src/hle/dstorage.rs index 2ae4fe1b66..1766aed386 100644 --- a/crates/xcl/src/hle/dstorage.rs +++ b/crates/xcl/src/hle/dstorage.rs @@ -803,7 +803,7 @@ unsafe fn snapshot_page_array(ptr: u64, offset: u16, byte_len: u32) -> Result bool { +pub(crate) unsafe fn read_own(at: usize, out: *mut u8, len: usize) -> bool { use windows_sys::Win32::System::Diagnostics::Debug::ReadProcessMemory; use windows_sys::Win32::System::Threading::GetCurrentProcess; let mut done = 0usize;