From 60a90b12fd3ba1b69c23f542b84030ed420c5ffe Mon Sep 17 00:00:00 2001 From: bmjask <> Date: Wed, 25 Nov 2026 11:51:15 +0100 Subject: [PATCH 1/5] 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 Assisted-by: Codex (GPT-5.6-sol) --- 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 c1046f0879..1d6cd785cd 100644 --- a/crates/xcl/src/hle/d3d12x/list_x.rs +++ b/crates/xcl/src/hle/d3d12x/list_x.rs @@ -1676,6 +1676,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 9986adaaf9..492bae8470 100644 --- a/crates/xcl/src/host.rs +++ b/crates/xcl/src/host.rs @@ -177,7 +177,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 || { @@ -188,6 +189,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 3caff9ef8c3910f9094f23540b5fe9e83bcb02f0 Mon Sep 17 00:00:00 2001 From: bmjask <> Date: Wed, 25 Nov 2026 20:32:56 +0100 Subject: [PATCH 2/5] 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 Assisted-by: Codex (GPT-5.6-sol) --- 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 1d6cd785cd..268a08be6b 100644 --- a/crates/xcl/src/hle/d3d12x/list_x.rs +++ b/crates/xcl/src/hle/d3d12x/list_x.rs @@ -1676,15 +1676,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()); }); } @@ -5409,6 +5447,46 @@ mod tests { assert_eq!(mirror_in(&cache.1, 550, 1), None, "ranges are half-open"); } + /// 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 3c48e6fedf..2a9d878708 100644 --- a/crates/xcl/src/hle/dstorage.rs +++ b/crates/xcl/src/hle/dstorage.rs @@ -805,7 +805,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; From bff80f9bb3eb3dfafd78dabba3c58a9a320905ef Mon Sep 17 00:00:00 2001 From: bmjask <> Date: Thu, 26 Nov 2026 01:43:28 -0500 Subject: [PATCH 3/5] xcl run: the sync-state dump names the list behind a Wait64 cell A cell recorded and never submitted (#1452: rec 0x619, sub 0x0@qNone) did not say which list recorded the value or what became of that list. Each cell now keeps the PC list and time of the recording that set its recorded_max, and each list that recorded a Write64 keeps its last execute, Reset and Release (signal_write64_fences, the KickoffX submit, reset_pc, clear_segment_state, on_release). The cell entry in cells_behind_summary prints them as their time after the recording, or "no": reset or released with no execute is a recording thrown away (#2691's address reuse shows as a release), executed is a miss on the submit side, and none of the three a list the title still holds. Part of #1452. Co-Authored-By: Claude Opus 5.5 Assisted-by: Codex (GPT-5.6-sol) --- crates/xcl/src/hle/d3d12x/list_x.rs | 92 ++++++++++++++++++++++++++++- 1 file changed, 91 insertions(+), 1 deletion(-) diff --git a/crates/xcl/src/hle/d3d12x/list_x.rs b/crates/xcl/src/hle/d3d12x/list_x.rs index 268a08be6b..e52ca5ba1d 100644 --- a/crates/xcl/src/hle/d3d12x/list_x.rs +++ b/crates/xcl/src/hle/d3d12x/list_x.rs @@ -1286,6 +1286,7 @@ pub(super) unsafe fn on_create_with_backing(list: *mut c_void, alloc: usize, bac /// Clean up all tracked state when a command list is released for good. pub(super) fn on_release(list: usize) { + trace_list(list, ListEvent::Released); super::alloc_pool::forget_list(list); release_copy_scratch(list); super::real::dispatch_chain::list_reset(list); @@ -1308,6 +1309,7 @@ pub(super) fn on_release(list: usize) { /// Clear per-recording sync/write tracking on a split segment moved into `title_list`'s spare pool. pub(super) fn clear_segment_state(list: usize) { + trace_list(list, ListEvent::Reset); if let Some(s) = LISTS.lock().unwrap().as_mut().and_then(|m| m.get_mut(&list)) { s.occlusion = Occlusion::default(); s.written_ranges.clear(); @@ -1568,6 +1570,8 @@ struct SyncCell { recorded_list_type: Option, /// Last queue the recording list was executed on (if known). recorded_last_queue: Option, + /// The PC list of the recording that set `recorded_max`, and when (the sync-state dump). + recorded_by: Option<(usize, Instant)>, /// Highest value submitted via `queue.Signal(&fence, v)` or released on CPU. submitted_max: u64, /// Queue that issued the latest GPU `Signal` (`None` if released on CPU). @@ -1634,6 +1638,46 @@ impl SyncGraph { static SYNC_GRAPH: Mutex> = Mutex::new(None); static ARMED_GPU_WAITS: AtomicUsize = AtomicUsize::new(0); + +/// What became of a PC list that recorded a `Write64BitValue*`: when it was last executed, reset +/// and released (the sync-state dump; a cell recorded and never submitted names its list). +#[derive(Default, Clone, Copy)] +struct ListTrace { + executed: Option, + reset: Option, + released: Option, +} + +#[derive(Clone, Copy)] +enum ListEvent { + Executed, + Reset, + Released, +} + +static WRITE64_LISTS: Mutex>> = Mutex::new(None); + +/// Notes `event` on `list` when it ever recorded a `Write64BitValue*`. +fn trace_list(list: usize, event: ListEvent) { + let mut g = WRITE64_LISTS.lock().unwrap(); + if let Some(t) = g.as_mut().and_then(|m| m.get_mut(&list)) { + let at = Some(Instant::now()); + match event { + ListEvent::Executed => t.executed = at, + ListEvent::Reset => t.reset = at, + ListEvent::Released => t.released = at, + } + } +} + +/// `by ms ago (executed …, reset …, released …)`: each as its time after the +/// recording, or `no` when it has not come since. +fn recording_trace(list: usize, at: Instant) -> String { + let t = WRITE64_LISTS.try_lock().ok().and_then(|g| g.as_ref().and_then(|m| m.get(&list).copied())); + let Some(t) = t else { return format!("by {list:#x} {} ms ago", at.elapsed().as_millis()) }; + let after = |e: Option| e.filter(|&e| e >= at).map_or("no".to_string(), |e| format!("+{} ms", (e - at).as_millis())); + format!("by {list:#x} {} ms ago (executed {}, reset {}, released {})", at.elapsed().as_millis(), after(t.executed), after(t.reset), after(t.released)) +} static SYNC_WATCHDOG_WAKE: OnceLock<(Mutex<()>, Condvar)> = OnceLock::new(); static QUEUE_IDS: Mutex> = Mutex::new(Vec::new()); @@ -1693,10 +1737,11 @@ fn cells_behind_summary() -> String { 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 [{}]", + "{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.recorded_by.map_or(String::new(), |(l, at)| format!(" {}", recording_trace(l, at))), c.cpu_signaled_max, if c.non_monotonic { " non-monotonic" } else { "" }, armed.join(", ") @@ -2202,6 +2247,7 @@ fn note_write64_recorded(list: *mut c_void, va: u64, value: u64) { s.push_write64_signal(va, value); (s.list_type, s.last_queue) }; + WRITE64_LISTS.lock().unwrap().get_or_insert_with(HashMap::new).entry(list as usize).or_default(); let rescue: Option<(ID3D12Fence, u64, u64)> = { let mut g = SYNC_GRAPH.lock().unwrap(); let cell = g.get_or_insert_with(SyncGraph::default).cells.entry(va).or_default(); @@ -2225,6 +2271,7 @@ fn note_write64_recorded(list: *mut c_void, va: u64, value: u64) { } else { if value > cell.recorded_max { cell.recorded_max = value; + cell.recorded_by = Some((list as usize, Instant::now())); if lty.is_some() { cell.recorded_list_type = lty; } @@ -2374,6 +2421,9 @@ pub(super) unsafe fn signal_write64_fences(queue: *mut c_void, lists: &[*mut c_v if WRITE64_SIGNAL_LISTS.load(Ordering::Acquire) == 0 || queue.is_null() || lists.is_empty() { return; } + for &l in lists { + trace_list(l as usize, ListEvent::Executed); + } let pairs: Vec<(u64, u64)> = { let g = LISTS.lock().unwrap(); let Some(m) = g.as_ref() else { return }; @@ -4101,6 +4151,7 @@ pub unsafe extern "system" fn kickoff_x(this: *mut c_void) { let current_submitted = super::queue_submit::with(q_raw as usize, || { let submitted = gate.submit(q_raw, &one, || {}, || exec(q_raw, 1, one.as_ptr())); if submitted { + trace_list(this as usize, ListEvent::Executed); copy_scratch_submitted(q_raw, &one); super::title_textures::publish_submission_effects(queue_identity(q_raw as usize), &texture_writes, &texture_transitions); signal_write64_pairs(q_raw, &write64_signals); @@ -4869,6 +4920,7 @@ pub(super) unsafe fn reset_pc(this: *mut c_void, alloc: usize, pso: usize) -> i3 if hr < 0 { return hr; } + trace_list(this as usize, ListEvent::Reset); reset_copy_scratch(this); reset_own_alloc(this); @@ -5487,6 +5539,44 @@ mod tests { } } + /// A cell recorded and never submitted names the list that recorded it, and what came to that + /// list after the recording: here a Reset and then a Release, and no execute. + #[test] + fn the_cells_behind_summary_names_the_recording_list_and_what_came_to_it() { + let cell = Box::new(AtomicU64::new(0)); + let va = &*cell as *const AtomicU64 as u64; + // (an address no real list has: the box's own) + let token = Box::new(0u64); + let list = &*token as *const u64 as usize; + let summary = || { + // (other tests take the graph too: the summary only tries the lock) + for _ in 0..200 { + let s = cells_behind_summary(); + if !s.contains("(sync graph locked)") { + return s; + } + std::thread::sleep(std::time::Duration::from_millis(5)); + } + panic!("the sync graph stayed locked"); + }; + let entry = |s: &str| s.split("; ").find(|p| p.starts_with(&format!("{va:#x}:"))).map(str::to_string).unwrap_or_else(|| panic!("{s}")); + + note_write64_recorded(list as *mut c_void, va, 0x619); + let s = entry(&summary()); + assert!(s.contains(&format!("rec 0x619 by {list:#x} ")), "{s}"); + assert!(s.contains("(executed no, reset no, released no)"), "{s}"); + trace_list(list, ListEvent::Reset); + on_release(list); + let s = entry(&summary()); + assert!(s.contains("(executed no, reset +"), "{s}"); + assert!(s.contains(", released +"), "{s}"); + + if let Some(gr) = SYNC_GRAPH.lock().unwrap().as_mut() { + gr.cells.remove(&va); + } + WRITE64_LISTS.lock().unwrap().as_mut().map(|m| m.remove(&list)); + } + /// 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] From 976faf729f68f4d47771351007db60aa72734732 Mon Sep 17 00:00:00 2001 From: bmjask <> Date: Thu, 26 Nov 2026 03:49:06 -0500 Subject: [PATCH 4/5] xcl run: a Reset or Release ends the recording the Write64 trace follows The trace was kept per list address: after a Reset, or a Release and a new list at the same address, a later execute of another recording showed on the old cell as "executed", though its write was thrown away. Each recording that records a Write64 now has a trace of its own, which the cells it recorded hold; a Reset or Release ends it, and what comes to the list after belongs to the next one. The test adds both cases: Reset, an unrelated recording and its execute leave the old cell at "executed no", and so do a Release and a list at the same address that records and is executed. Part of #1452. Co-Authored-By: Claude Opus 5.5 Assisted-by: Codex (GPT-5.6-sol) --- crates/xcl/src/hle/d3d12x/list_x.rs | 82 +++++++++++++++++++++-------- 1 file changed, 60 insertions(+), 22 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/list_x.rs b/crates/xcl/src/hle/d3d12x/list_x.rs index e52ca5ba1d..48b129ad79 100644 --- a/crates/xcl/src/hle/d3d12x/list_x.rs +++ b/crates/xcl/src/hle/d3d12x/list_x.rs @@ -21,7 +21,7 @@ use std::collections::{BTreeMap, HashMap}; use std::ffi::c_void; use std::mem::{transmute_copy, ManuallyDrop}; use std::sync::atomic::{AtomicBool, AtomicU32, AtomicU64, AtomicUsize, Ordering}; -use std::sync::{Condvar, LazyLock, Mutex, OnceLock}; +use std::sync::{Arc, Condvar, LazyLock, Mutex, OnceLock}; use std::time::{Duration, Instant}; use windows::core::Interface; @@ -1570,8 +1570,9 @@ struct SyncCell { recorded_list_type: Option, /// Last queue the recording list was executed on (if known). recorded_last_queue: Option, - /// The PC list of the recording that set `recorded_max`, and when (the sync-state dump). - recorded_by: Option<(usize, Instant)>, + /// The PC list of the recording that set `recorded_max`, when, and what came to that + /// recording (the sync-state dump). + recorded_by: Option<(usize, Instant, Arc>)>, /// Highest value submitted via `queue.Signal(&fence, v)` or released on CPU. submitted_max: u64, /// Queue that issued the latest GPU `Signal` (`None` if released on CPU). @@ -1639,8 +1640,9 @@ impl SyncGraph { static SYNC_GRAPH: Mutex> = Mutex::new(None); static ARMED_GPU_WAITS: AtomicUsize = AtomicUsize::new(0); -/// What became of a PC list that recorded a `Write64BitValue*`: when it was last executed, reset -/// and released (the sync-state dump; a cell recorded and never submitted names its list). +/// What became of a recording of a PC list that recorded a `Write64BitValue*`: when it was last +/// executed, and the Reset or Release that ended it (the sync-state dump; a cell recorded and +/// never submitted names its list). #[derive(Default, Clone, Copy)] struct ListTrace { executed: Option, @@ -1655,12 +1657,22 @@ enum ListEvent { Released, } -static WRITE64_LISTS: Mutex>> = Mutex::new(None); +/// the recording each PC list has open, when it recorded a `Write64BitValue*` +static WRITE64_LISTS: Mutex>>>> = Mutex::new(None); -/// Notes `event` on `list` when it ever recorded a `Write64BitValue*`. +/// Notes `event` on the recording `list` has open, when it recorded a `Write64BitValue*`. A Reset +/// or a Release ends the recording: what comes to the list (or its address) after is another's. fn trace_list(list: usize, event: ListEvent) { - let mut g = WRITE64_LISTS.lock().unwrap(); - if let Some(t) = g.as_mut().and_then(|m| m.get_mut(&list)) { + let t = { + let mut g = WRITE64_LISTS.lock().unwrap(); + let Some(m) = g.as_mut() else { return }; + match event { + ListEvent::Executed => m.get(&list).cloned(), + ListEvent::Reset | ListEvent::Released => m.remove(&list), + } + }; + if let Some(t) = t { + let mut t = t.lock().unwrap(); let at = Some(Instant::now()); match event { ListEvent::Executed => t.executed = at, @@ -1671,11 +1683,10 @@ fn trace_list(list: usize, event: ListEvent) { } /// `by ms ago (executed …, reset …, released …)`: each as its time after the -/// recording, or `no` when it has not come since. -fn recording_trace(list: usize, at: Instant) -> String { - let t = WRITE64_LISTS.try_lock().ok().and_then(|g| g.as_ref().and_then(|m| m.get(&list).copied())); - let Some(t) = t else { return format!("by {list:#x} {} ms ago", at.elapsed().as_millis()) }; - let after = |e: Option| e.filter(|&e| e >= at).map_or("no".to_string(), |e| format!("+{} ms", (e - at).as_millis())); +/// recording `t`, or `no` when it has not come to it. +fn recording_trace(list: usize, at: Instant, t: &Mutex) -> String { + let Ok(t) = t.try_lock().map(|t| *t) else { return format!("by {list:#x} {} ms ago", at.elapsed().as_millis()) }; + let after = |e: Option| e.map_or("no".to_string(), |e| format!("+{} ms", e.saturating_duration_since(at).as_millis())); format!("by {list:#x} {} ms ago (executed {}, reset {}, released {})", at.elapsed().as_millis(), after(t.executed), after(t.reset), after(t.released)) } static SYNC_WATCHDOG_WAKE: OnceLock<(Mutex<()>, Condvar)> = OnceLock::new(); @@ -1741,7 +1752,7 @@ fn cells_behind_summary() -> String { c.submitted_max, c.submitted_queue.map(queue_id), c.recorded_max, - c.recorded_by.map_or(String::new(), |(l, at)| format!(" {}", recording_trace(l, at))), + c.recorded_by.as_ref().map_or(String::new(), |(l, at, t)| format!(" {}", recording_trace(*l, *at, t))), c.cpu_signaled_max, if c.non_monotonic { " non-monotonic" } else { "" }, armed.join(", ") @@ -2247,7 +2258,7 @@ fn note_write64_recorded(list: *mut c_void, va: u64, value: u64) { s.push_write64_signal(va, value); (s.list_type, s.last_queue) }; - WRITE64_LISTS.lock().unwrap().get_or_insert_with(HashMap::new).entry(list as usize).or_default(); + let recording = WRITE64_LISTS.lock().unwrap().get_or_insert_with(HashMap::new).entry(list as usize).or_default().clone(); let rescue: Option<(ID3D12Fence, u64, u64)> = { let mut g = SYNC_GRAPH.lock().unwrap(); let cell = g.get_or_insert_with(SyncGraph::default).cells.entry(va).or_default(); @@ -2271,7 +2282,7 @@ fn note_write64_recorded(list: *mut c_void, va: u64, value: u64) { } else { if value > cell.recorded_max { cell.recorded_max = value; - cell.recorded_by = Some((list as usize, Instant::now())); + cell.recorded_by = Some((list as usize, Instant::now(), recording)); if lty.is_some() { cell.recorded_list_type = lty; } @@ -5540,11 +5551,14 @@ mod tests { } /// A cell recorded and never submitted names the list that recorded it, and what came to that - /// list after the recording: here a Reset and then a Release, and no execute. + /// recording: here a Reset (which ends it: the Release after comes to none), and no execute. + /// A recording on the list after a Reset, or on a list made at its address after a Release, + /// is another one: its execute does not come to the cell. #[test] fn the_cells_behind_summary_names_the_recording_list_and_what_came_to_it() { - let cell = Box::new(AtomicU64::new(0)); - let va = &*cell as *const AtomicU64 as u64; + let cell = Box::new([AtomicU64::new(0), AtomicU64::new(0)]); + let va = &cell[0] as *const AtomicU64 as u64; + let other = &cell[1] as *const AtomicU64 as u64; // (an address no real list has: the box's own) let token = Box::new(0u64); let list = &*token as *const u64 as usize; @@ -5559,7 +5573,8 @@ mod tests { } panic!("the sync graph stayed locked"); }; - let entry = |s: &str| s.split("; ").find(|p| p.starts_with(&format!("{va:#x}:"))).map(str::to_string).unwrap_or_else(|| panic!("{s}")); + let entry_of = |va: u64, s: &str| s.split("; ").find(|p| p.starts_with(&format!("{va:#x}:"))).map(str::to_string).unwrap_or_else(|| panic!("{s}")); + let entry = |s: &str| entry_of(va, s); note_write64_recorded(list as *mut c_void, va, 0x619); let s = entry(&summary()); @@ -5569,10 +5584,33 @@ mod tests { on_release(list); let s = entry(&summary()); assert!(s.contains("(executed no, reset +"), "{s}"); - assert!(s.contains(", released +"), "{s}"); + assert!(s.contains(", released no)"), "{s}"); + + // a Reset, an unrelated recording on the list, and its execute + note_write64_recorded(list as *mut c_void, va, 0x61a); + trace_list(list, ListEvent::Reset); + note_write64_recorded(list as *mut c_void, other, 1); + trace_list(list, ListEvent::Executed); + let s = summary(); + assert!(entry(&s).contains("rec 0x61a by "), "{s}"); + assert!(entry(&s).contains("(executed no, reset +"), "{s}"); + assert!(entry_of(other, &s).contains("(executed +"), "{s}"); + + // a new recording (after the Reset that ends the executed one), a Release, and a list at + // the same address that records and is executed + trace_list(list, ListEvent::Reset); + note_write64_recorded(list as *mut c_void, va, 0x61b); + on_release(list); + note_write64_recorded(list as *mut c_void, other, 2); + trace_list(list, ListEvent::Executed); + let s = summary(); + assert!(entry(&s).contains("rec 0x61b by "), "{s}"); + assert!(entry(&s).contains("(executed no, reset no, released +"), "{s}"); + assert!(entry_of(other, &s).contains("(executed +"), "{s}"); if let Some(gr) = SYNC_GRAPH.lock().unwrap().as_mut() { gr.cells.remove(&va); + gr.cells.remove(&other); } WRITE64_LISTS.lock().unwrap().as_mut().map(|m| m.remove(&list)); } From 9d8af31c22805f8ad082a2621c8db245812fe5ec Mon Sep 17 00:00:00 2001 From: bmjask <> Date: Thu, 26 Nov 2026 02:05:03 -0800 Subject: [PATCH 5/5] xcl run: trace immutable submission generations The integrated execution path publishes captured SubmissionChunk effects instead of calling the legacy post-submit helpers. Capture each recording token with that immutable plan and mark it only after the exact chunk reaches the driver, so Reset, Release, address reuse, and split waits cannot redirect execution to another generation. Kickoff ends the actual reset target only after Reset succeeds. Recording-level tracing remains disabled unless XCL_THREAD_DUMP_AT schedules a dump. Assisted-by: Codex (GPT-5.6-sol) --- crates/xcl/src/hle/d3d12x/list_x.rs | 115 +++++++++++++++++++++------- crates/xcl/src/host.rs | 3 + 2 files changed, 92 insertions(+), 26 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/list_x.rs b/crates/xcl/src/hle/d3d12x/list_x.rs index 48b129ad79..b18edb7c84 100644 --- a/crates/xcl/src/hle/d3d12x/list_x.rs +++ b/crates/xcl/src/hle/d3d12x/list_x.rs @@ -756,6 +756,8 @@ struct ListState { /// so `ExecuteCommandLists` / `KickoffX` can signal per-address GPU fences for cross-queue /// `Wait64BitValueX` without blocking the submitter thread on the CPU. write64_signals: Vec<(u64, u64)>, + /// Causal dump token for this exact recording; absent unless scheduled dumps enabled tracing. + write64_trace: Option>>, /// External memory waits recorded on this list pending_waits: Vec, /// bottom-of-pipe writes into memory the GPU does not see (`cpu_writes`) @@ -1042,6 +1044,8 @@ impl SegmentEffects { pub(super) struct SubmissionPlan { lists: Vec<*mut c_void>, effects: Vec, + /// Exact recording tokens captured with `effects`; empty when causal dumps are disabled. + traces: Vec>>>, /// Per-segment dependency on an earlier CPU-after-segment producer; empty on the no-wait fast path. waits_after_cpu_write: Vec, has_waits: bool, @@ -1050,7 +1054,7 @@ pub(super) struct SubmissionPlan { impl SubmissionPlan { fn capture(lists: &[*mut c_void]) -> Self { if lists.is_empty() { - return Self { lists: Vec::new(), effects: Vec::new(), waits_after_cpu_write: Vec::new(), has_waits: false }; + return Self { lists: Vec::new(), effects: Vec::new(), traces: Vec::new(), waits_after_cpu_write: Vec::new(), has_waits: false }; } let g = LISTS.lock().unwrap(); let effects: Vec = lists @@ -1070,6 +1074,14 @@ impl SubmissionPlan { .unwrap_or_default() }) .collect(); + let traces = if sync_state_tracing() { + lists + .iter() + .map(|&list| g.as_ref().and_then(|m| m.get(&(list as usize))).and_then(|s| s.write64_trace.clone())) + .collect() + } else { + Vec::new() + }; drop(g); let has_waits = effects.iter().any(|e| !e.waits.is_empty()); let waits_after_cpu_write = if has_waits { @@ -1085,7 +1097,7 @@ impl SubmissionPlan { } else { Vec::new() }; - Self { lists: lists.to_vec(), effects, waits_after_cpu_write, has_waits } + Self { lists: lists.to_vec(), effects, traces, waits_after_cpu_write, has_waits } } fn cpu_producer_overlaps(&self, waits: &[PendingWait], before: usize) -> bool { @@ -1109,6 +1121,7 @@ impl SubmissionPlan { pub(super) struct SubmissionChunk<'a> { lists: &'a [*mut c_void], effects: &'a [SegmentEffects], + traces: &'a [Option>>], needs_cpu_completion: bool, } @@ -1122,6 +1135,13 @@ impl<'a> SubmissionChunk<'a> { } pub(super) unsafe fn submit_effects(self, queue: *mut c_void) -> Option { + // The immutable submission plan is now the production path, so record execution here + // after its caller has successfully submitted this exact chunk. The legacy + // `signal_write64_fences` path below remains useful to tests but is not reached by the + // title-facing submit path. + for trace in self.traces.iter().flatten() { + mark_trace(trace, ListEvent::Executed); + } submit_segment_effects(queue, self.effects, self.needs_cpu_completion) } } @@ -1639,6 +1659,17 @@ impl SyncGraph { static SYNC_GRAPH: Mutex> = Mutex::new(None); static ARMED_GPU_WAITS: AtomicUsize = AtomicUsize::new(0); +static SYNC_STATE_TRACING: AtomicBool = AtomicBool::new(false); + +/// Enable recording-level causal detail only when at least one scheduled sync dump will use it. +pub fn enable_sync_state_tracing() { + SYNC_STATE_TRACING.store(true, Ordering::Release); +} + +#[inline] +fn sync_state_tracing() -> bool { + SYNC_STATE_TRACING.load(Ordering::Acquire) +} /// What became of a recording of a PC list that recorded a `Write64BitValue*`: when it was last /// executed, and the Reset or Release that ended it (the sync-state dump; a cell recorded and @@ -1657,28 +1688,32 @@ enum ListEvent { Released, } -/// the recording each PC list has open, when it recorded a `Write64BitValue*` -static WRITE64_LISTS: Mutex>>>> = Mutex::new(None); - /// Notes `event` on the recording `list` has open, when it recorded a `Write64BitValue*`. A Reset /// or a Release ends the recording: what comes to the list (or its address) after is another's. fn trace_list(list: usize, event: ListEvent) { + if !sync_state_tracing() { + return; + } let t = { - let mut g = WRITE64_LISTS.lock().unwrap(); - let Some(m) = g.as_mut() else { return }; + let mut g = LISTS.lock().unwrap(); + let Some(state) = g.as_mut().and_then(|m| m.get_mut(&list)) else { return }; match event { - ListEvent::Executed => m.get(&list).cloned(), - ListEvent::Reset | ListEvent::Released => m.remove(&list), + ListEvent::Executed => state.write64_trace.clone(), + ListEvent::Reset | ListEvent::Released => state.write64_trace.take(), } }; if let Some(t) = t { - let mut t = t.lock().unwrap(); - let at = Some(Instant::now()); - match event { - ListEvent::Executed => t.executed = at, - ListEvent::Reset => t.reset = at, - ListEvent::Released => t.released = at, - } + mark_trace(&t, event); + } +} + +fn mark_trace(trace: &Mutex, event: ListEvent) { + let mut trace = trace.lock().unwrap(); + let at = Some(Instant::now()); + match event { + ListEvent::Executed => trace.executed = at, + ListEvent::Reset => trace.reset = at, + ListEvent::Released => trace.released = at, } } @@ -2252,13 +2287,17 @@ fn note_write64_recorded(list: *mut c_void, va: u64, value: u64) { if value == 0 || !va.is_multiple_of(8) { return; } - let (lty, last_q) = { + let (lty, last_q, recording) = { let mut g = LISTS.lock().unwrap(); let s = g.get_or_insert_with(HashMap::new).entry(list as usize).or_default(); s.push_write64_signal(va, value); - (s.list_type, s.last_queue) + let recording = if sync_state_tracing() { + Some(s.write64_trace.get_or_insert_with(Default::default).clone()) + } else { + None + }; + (s.list_type, s.last_queue, recording) }; - let recording = WRITE64_LISTS.lock().unwrap().get_or_insert_with(HashMap::new).entry(list as usize).or_default().clone(); let rescue: Option<(ID3D12Fence, u64, u64)> = { let mut g = SYNC_GRAPH.lock().unwrap(); let cell = g.get_or_insert_with(SyncGraph::default).cells.entry(va).or_default(); @@ -2282,7 +2321,7 @@ fn note_write64_recorded(list: *mut c_void, va: u64, value: u64) { } else { if value > cell.recorded_max { cell.recorded_max = value; - cell.recorded_by = Some((list as usize, Instant::now(), recording)); + cell.recorded_by = recording.map(|recording| (list as usize, Instant::now(), recording)); if lty.is_some() { cell.recorded_list_type = lty; } @@ -2710,6 +2749,7 @@ unsafe fn execute_submission_plan_with_completions( let chunk = |start, end| SubmissionChunk { lists: &plan.lists[start..end], effects: &plan.effects[start..end], + traces: if plan.traces.is_empty() { &[] } else { &plan.traces[start..end] }, needs_cpu_completion: plan.chunk_needs_cpu_completion(start, end, external_waits), }; if !plan.has_waits { @@ -4069,7 +4109,7 @@ pub unsafe extern "system" fn kickoff_x(this: *mut c_void) { } else { None }; - let (bound_pso, depth_bias, index_buffer, depth_bounds, predication, pred_buf, pred_in_state, active_query, preclose_effects) = { + let (bound_pso, depth_bias, index_buffer, depth_bounds, predication, pred_buf, pred_in_state, active_query, preclose_effects, current_trace) = { let mut g = LISTS.lock().unwrap(); let Some(s) = g.as_mut().and_then(|m| m.get_mut(&(this as usize))) else { if let Some((wrapper, standby)) = standby { @@ -4081,7 +4121,8 @@ pub unsafe extern "system" fn kickoff_x(this: *mut c_void) { let preclose_effects = take_kickoff_effects_before_close(s, standby.is_some()); let pred_in_state = std::mem::replace(&mut s.pred_in_state, false); let pred_buf = s.pred_buf.as_ref().map(|r| r.as_raw() as usize); - (s.bound_pso, s.depth_bias, s.index_buffer, s.depth_bounds, s.predication, pred_buf, pred_in_state, active_query, preclose_effects) + let current_trace = sync_state_tracing().then(|| s.write64_trace.clone()).flatten(); + (s.bound_pso, s.depth_bias, s.index_buffer, s.depth_bounds, s.predication, pred_buf, pred_in_state, active_query, preclose_effects, current_trace) }; // End any active occlusion query before Close (PC forbids closing with an open query). @@ -4162,7 +4203,9 @@ pub unsafe extern "system" fn kickoff_x(this: *mut c_void) { let current_submitted = super::queue_submit::with(q_raw as usize, || { let submitted = gate.submit(q_raw, &one, || {}, || exec(q_raw, 1, one.as_ptr())); if submitted { - trace_list(this as usize, ListEvent::Executed); + if let Some(trace) = ¤t_trace { + mark_trace(trace, ListEvent::Executed); + } copy_scratch_submitted(q_raw, &one); super::title_textures::publish_submission_effects(queue_identity(q_raw as usize), &texture_writes, &texture_transitions); signal_write64_pairs(q_raw, &write64_signals); @@ -4236,6 +4279,7 @@ pub unsafe extern "system" fn kickoff_x(this: *mut c_void) { if reset_hr < 0 { return warn(&ERR, "KickoffX", format_args!("Reset after kickoff failed ({reset_hr:#x})")); } + trace_list(target as usize, ListEvent::Reset); reset_copy_scratch(target); let bindings = LISTS.lock().unwrap().as_ref().and_then(|m| m.get(&(target as usize))).map(|s| s.bindings.clone()); if let Some(b) = bindings { @@ -5556,6 +5600,7 @@ mod tests { /// is another one: its execute does not come to the cell. #[test] fn the_cells_behind_summary_names_the_recording_list_and_what_came_to_it() { + enable_sync_state_tracing(); let cell = Box::new([AtomicU64::new(0), AtomicU64::new(0)]); let va = &cell[0] as *const AtomicU64 as u64; let other = &cell[1] as *const AtomicU64 as u64; @@ -5590,7 +5635,9 @@ mod tests { note_write64_recorded(list as *mut c_void, va, 0x61a); trace_list(list, ListEvent::Reset); note_write64_recorded(list as *mut c_void, other, 1); - trace_list(list, ListEvent::Executed); + let lists = [list as *mut c_void]; + let plan = submission_plan(&lists); + unsafe { execute_submission_plan_on(std::ptr::null_mut(), &plan, |chunk| drop(chunk.submit_effects(std::ptr::null_mut()))) }; let s = summary(); assert!(entry(&s).contains("rec 0x61a by "), "{s}"); assert!(entry(&s).contains("(executed no, reset +"), "{s}"); @@ -5602,17 +5649,32 @@ mod tests { note_write64_recorded(list as *mut c_void, va, 0x61b); on_release(list); note_write64_recorded(list as *mut c_void, other, 2); - trace_list(list, ListEvent::Executed); + let plan = submission_plan(&lists); + unsafe { execute_submission_plan_on(std::ptr::null_mut(), &plan, |chunk| drop(chunk.submit_effects(std::ptr::null_mut()))) }; let s = summary(); assert!(entry(&s).contains("rec 0x61b by "), "{s}"); assert!(entry(&s).contains("(executed no, reset no, released +"), "{s}"); assert!(entry_of(other, &s).contains("(executed +"), "{s}"); + // Planning snapshots the exact generation. A Reset and new recording before publication + // must not redirect the old submission's Executed event to the new recording. + trace_list(list, ListEvent::Reset); + note_write64_recorded(list as *mut c_void, va, 0x61c); + let captured = submission_plan(&lists); + trace_list(list, ListEvent::Reset); + note_write64_recorded(list as *mut c_void, other, 3); + unsafe { execute_submission_plan_on(std::ptr::null_mut(), &captured, |chunk| drop(chunk.submit_effects(std::ptr::null_mut()))) }; + let s = summary(); + assert!(entry(&s).contains("rec 0x61c by "), "{s}"); + assert!(entry(&s).contains("(executed +") && entry(&s).contains(", reset +"), "{s}"); + assert!(entry_of(other, &s).contains("rec 0x3 by "), "{s}"); + assert!(entry_of(other, &s).contains("(executed no, reset no, released no)"), "{s}"); + if let Some(gr) = SYNC_GRAPH.lock().unwrap().as_mut() { gr.cells.remove(&va); gr.cells.remove(&other); } - WRITE64_LISTS.lock().unwrap().as_mut().map(|m| m.remove(&list)); + on_release(list); } /// An internal wait shows in the watchdog's summary while its fence is short of the value, @@ -6326,6 +6388,7 @@ mod tests { assert!(!SubmissionPlan { lists: vec![producer], effects: vec![plan.effects[0].clone()], + traces: Vec::new(), waits_after_cpu_write: Vec::new(), has_waits: false, } diff --git a/crates/xcl/src/host.rs b/crates/xcl/src/host.rs index 492bae8470..970cf6c7b4 100644 --- a/crates/xcl/src/host.rs +++ b/crates/xcl/src/host.rs @@ -181,6 +181,9 @@ pub fn host_main(boot_path: &Path) -> i32 { // 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(); + if !at.is_empty() { + crate::hle::d3d12x::list_x::enable_sync_state_tracing(); + } let _ = std::thread::Builder::new().name("xcl-thread-dump".into()).spawn(move || { let t0 = std::time::Instant::now(); for s in at {