From c6562a0c87c0d4ad30886dacd397499b9a2b8c08 Mon Sep 17 00:00:00 2001 From: KOLESNiii <102358205+KOLESNiii@users.noreply.github.com> Date: Wed, 25 Nov 2026 22:07:59 +0000 Subject: [PATCH] D3D12.X: XCL_ACTIVITY counts and times the per-draw buffer mirror sync Once any buffer mirror exists, sync_title_buffers runs on every draw and nothing reported what it costs. With XCL_ACTIVITY=1 it now logs, every 2 s in which it ran: draws synced and those with a mirror, copies (whole buffers), bytes copied, indices scanned for the vertex range, readable() calls (those over 16 pages, which can take the VirtualQuery walk), the time in readable() and its slowest call, and the time in the function. A row is logged whenever any of these moved, also with no draw synced: the counters of a sync still running at the sample (or stalled for over 2 s) land before its draw is counted, and its slowest readable() would otherwise be dropped with the skipped row. The slowest readable() is not in #2391's list: measuring on #2389 found single calls of 16 pages or fewer taking 1-9 ms, which the other counters average away. The counters cost nothing without XCL_ACTIVITY: host_main turns them on (enable_sync_activity) before it starts the activity thread, and until then sync_title_buffers, sync_readable() and sync_count() check one relaxed bool and take no time stamps and touch no shared counter. No change in behaviour: the old body is sync_mirrors, unchanged apart from the counters, and sync_title_buffers still returns first when no mirror exists. Fixes #2391. Part of #2389. Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/hle/d3d12x/list_x.rs | 123 +++++++++++++++++++++++++++- crates/xcl/src/host.rs | 16 ++++ 2 files changed, 137 insertions(+), 2 deletions(-) diff --git a/crates/xcl/src/hle/d3d12x/list_x.rs b/crates/xcl/src/hle/d3d12x/list_x.rs index 0210c522f1..173187d0ed 100644 --- a/crates/xcl/src/hle/d3d12x/list_x.rs +++ b/crates/xcl/src/hle/d3d12x/list_x.rs @@ -5827,12 +5827,90 @@ unsafe fn mirror_of(va: u64, bytes: usize, what: &str) -> Option { Some(p + (va - start) as usize) } +/// `XCL_ACTIVITY`: what `sync_title_buffers` did so far (the `SYNC_*` indices) +static SYNC_COUNTS: [AtomicU64; 9] = [const { AtomicU64::new(0) }; 9]; +const SYNC_DRAWS: usize = 0; +const SYNC_MIRRORED: usize = 1; +const SYNC_COPIES: usize = 2; +const SYNC_WHOLE: usize = 3; +const SYNC_BYTES: usize = 4; +const SYNC_INDICES: usize = 5; +const SYNC_READABLE: usize = 6; +const SYNC_READABLE_LARGE: usize = 7; +const SYNC_READABLE_NS: usize = 8; +/// ns of the whole function so far, and of its slowest `readable()` since `take_sync_slowest` +static SYNC_NS: AtomicU64 = AtomicU64::new(0); +static SYNC_READABLE_MAX_NS: AtomicU64 = AtomicU64::new(0); +/// whether the mirror sync counts and times at all: only with `XCL_ACTIVITY` (`enable_sync_activity`) +static SYNC_ON: AtomicBool = AtomicBool::new(false); + +/// `XCL_ACTIVITY`: from now on the mirror sync counts and times what it does +pub fn enable_sync_activity() { + SYNC_ON.store(true, Ordering::Relaxed); +} + +/// `XCL_ACTIVITY`: draws through the mirror sync, of those with a mirror, copies, whole-buffer +/// copies, bytes copied, indices scanned, `readable()` calls, of those over 16 pages (the +/// `VirtualQuery` walk), ns in `readable()`, ns in the sync – all so far +pub fn sync_activity() -> [u64; 10] { + let mut out = [0; 10]; + for (o, c) in out.iter_mut().zip(&SYNC_COUNTS) { + *o = c.load(Ordering::Relaxed); + } + out[9] = SYNC_NS.load(Ordering::Relaxed); + out +} + +/// ns of the slowest `readable()` of the mirror sync since the last call +pub fn take_sync_slowest() -> u64 { + SYNC_READABLE_MAX_NS.swap(0, Ordering::Relaxed) +} + +/// Whether `XCL_ACTIVITY` logs a row for these `sync_activity` deltas and slowest `readable()`: +/// when anything moved, also with no draw synced (a sync still running at the sample, or stalled) +pub fn sync_row_due(delta: &[u64], slowest: u64) -> bool { + slowest > 0 || delta.iter().any(|&v| v > 0) +} + +fn sync_count(k: usize, n: u64) { + if SYNC_ON.load(Ordering::Relaxed) { + SYNC_COUNTS[k].fetch_add(n, Ordering::Relaxed); + } +} + +/// `readable()`, counted and timed for `XCL_ACTIVITY` +fn sync_readable(p: usize, len: usize) -> bool { + if !SYNC_ON.load(Ordering::Relaxed) { + return crate::gpu::shader::readable(p, len); + } + let t = Instant::now(); + let r = crate::gpu::shader::readable(p, len); + let ns = t.elapsed().as_nanos() as u64; + sync_count(SYNC_READABLE, 1); + if len > 0 && ((p + len - 1) >> 12) - (p >> 12) + 1 > 16 { + sync_count(SYNC_READABLE_LARGE, 1); + } + sync_count(SYNC_READABLE_NS, ns); + SYNC_READABLE_MAX_NS.fetch_max(ns, Ordering::Relaxed); + r +} + /// Before a draw on `list`: the parts of mirrored buffers it reads are copied from the title's /// memory. `indexed`: (first index, index count, base vertex); else `first_vertex`, `vertices`. pub(super) unsafe fn sync_title_buffers(list: *mut c_void, indexed: Option<(u32, u32, i32)>, first_vertex: u32, vertices: u32, instances: u32) { if MIRROR_BYTES.load(Ordering::Relaxed) == 0 { return; } + if !SYNC_ON.load(Ordering::Relaxed) { + return sync_mirrors(list, indexed, first_vertex, vertices, instances); + } + let t = Instant::now(); + sync_mirrors(list, indexed, first_vertex, vertices, instances); + sync_count(SYNC_DRAWS, 1); + SYNC_NS.fetch_add(t.elapsed().as_nanos() as u64, Ordering::Relaxed); +} + +unsafe fn sync_mirrors(list: *mut c_void, indexed: Option<(u32, u32, i32)>, first_vertex: u32, vertices: u32, instances: u32) { let (vbs, ib) = { let g = LISTS.lock().unwrap(); let Some(b) = g.as_ref().and_then(|m| m.get(&(list as usize))).map(|s| &s.bindings) else { return }; @@ -5846,11 +5924,14 @@ pub(super) unsafe fn sync_title_buffers(list: *mut c_void, indexed: Option<(u32, } v }; + sync_count(SYNC_MIRRORED, 1); let copy = |va: u64, mirror: usize, bytes: u32, from: u64, len: u64| { let from = from.min(bytes as u64); let len = len.min(bytes as u64 - from) as usize; - if len > 0 && crate::gpu::shader::readable(va as usize + from as usize, len) { + if len > 0 && sync_readable(va as usize + from as usize, len) { std::ptr::copy_nonoverlapping((va + from) as *const u8, (mirror + from as usize) as *mut u8, len); + sync_count(SYNC_COPIES, 1); + sync_count(SYNC_BYTES, len as u64); } }; // the vertices read: [lo, hi) @@ -5862,9 +5943,10 @@ pub(super) unsafe fn sync_title_buffers(list: *mut c_void, indexed: Option<(u32, copy(va, m, bytes, from, len); } let n = (len.min((bytes as u64).saturating_sub(from)) / size) as usize; - if n == 0 || !crate::gpu::shader::readable((va + from) as usize, n * size as usize) { + if n == 0 || !sync_readable((va + from) as usize, n * size as usize) { (0, u64::MAX) } else { + sync_count(SYNC_INDICES, n as u64); let at = (va + from) as *const u8; let (mut lo, mut hi) = (u32::MAX, 0u32); for k in 0..n { @@ -5888,6 +5970,7 @@ pub(super) unsafe fn sync_title_buffers(list: *mut c_void, indexed: Option<(u32, // a buffer stepped by instance is not told apart: with instances (callers pass more // than 1 for a first instance other than 0 too), all of it if instances > 1 || hi == u64::MAX || stride == 0 || hi.saturating_mul(stride as u64) > bytes as u64 { + sync_count(SYNC_WHOLE, 1); copy(va, m, bytes, 0, bytes as u64); } else { copy(va, m, bytes, lo * stride as u64, (hi - lo) * stride as u64); @@ -11729,4 +11812,40 @@ mod tests { assert!(got.iter().all(|&v| v == 0xDEAD_BEEF), "the UAV was not cleared: {:x?}", &got[..4]); } } + + /// Without `XCL_ACTIVITY` the mirror sync counts and times nothing; once it is enabled it does. + #[test] + fn the_mirror_sync_counts_only_once_xcl_activity_enables_it() { + let buf = vec![0u8; 17 << 12]; // over 16 pages + let (p, len) = (buf.as_ptr() as usize, buf.len()); + assert!(!SYNC_ON.load(Ordering::Relaxed), "only this test enables the counters"); + let before = sync_activity(); + assert!(sync_readable(p, len)); + sync_count(SYNC_COPIES, 1); + assert_eq!(sync_activity(), before); + assert_eq!(take_sync_slowest(), 0); + + enable_sync_activity(); + assert!(sync_readable(p, len)); + sync_count(SYNC_COPIES, 1); + let after = sync_activity(); + // (other tests' draws may count too once it is on: at least ours) + assert!(after[SYNC_READABLE] > before[SYNC_READABLE]); + assert!(after[SYNC_READABLE_LARGE] > before[SYNC_READABLE_LARGE]); + assert!(after[SYNC_COPIES] > before[SYNC_COPIES]); + } + + /// A 2 s row with `readable()` work but no draw synced yet is logged, not dropped with its slowest call. + #[test] + fn a_mirror_sync_row_with_no_finished_draw_is_still_logged() { + let mut d = [0u64; 10]; + assert!(!sync_row_due(&d, 0)); + d[SYNC_READABLE] = 1; + d[SYNC_READABLE_NS] = 5_000_000; + assert!(sync_row_due(&d, 5_000_000)); + assert!(sync_row_due(&[0; 10], 5_000_000)); + d = [0; 10]; + d[SYNC_DRAWS] = 1; + assert!(sync_row_due(&d, 0)); + } } diff --git a/crates/xcl/src/host.rs b/crates/xcl/src/host.rs index f76e9a7a8d..dfebd69da3 100644 --- a/crates/xcl/src/host.rs +++ b/crates/xcl/src/host.rs @@ -100,6 +100,9 @@ pub fn host_main(boot_path: &Path) -> i32 { // mode queries only adapter-local usage; it does not enable the activity hot-path probes. let activity = crate::hle::d3d12x::memory_report::enabled(); let pressure = crate::hle::d3d12x::pso_async::pressure_sampling_enabled(); + if activity { + crate::hle::d3d12x::list_x::enable_sync_activity(); + } if activity || pressure { let name = if activity { "xcl-activity" } else { "xcl-pso-pressure" }; let sampler = std::thread::Builder::new().name(name.into()).spawn(move || { @@ -118,6 +121,7 @@ pub fn host_main(boot_path: &Path) -> i32 { let mut views = crate::hle::d3d12x::view_want::totals(); let mut tile_calls = crate::hle::d3d12x::sparse::tile_calls(); let mut last_tick = std::time::Instant::now(); + let mut last_sync = crate::hle::d3d12x::list_x::sync_activity(); loop { std::thread::sleep(std::time::Duration::from_secs(2)); let window_ms = last_tick.elapsed().as_millis() as u64; @@ -171,6 +175,18 @@ pub fn host_main(boot_path: &Path) -> i32 { crate::xlog_info!("tile mappings (2 s): {line}"); } tile_calls = mappings; + // the per-draw sync of buffer mirrors (only once a mirror exists) + let sync = crate::hle::d3d12x::list_x::sync_activity(); + let s: Vec = sync.iter().zip(last_sync).map(|(a, b)| a - b).collect(); + let slowest = crate::hle::d3d12x::list_x::take_sync_slowest(); + if crate::hle::d3d12x::list_x::sync_row_due(&s, slowest) { + let ms = |ns: u64| ns as f64 / 1e6; + crate::xlog_info!( + "buffer mirrors (2 s): {} draws synced, {} with a mirror; {} copies ({} whole buffers), {} KB; {} indices scanned; {} readable() calls ({} over 16 pages), {:.2} ms, slowest {:.2} ms; {:.2} ms in all", + s[0], s[1], s[2], s[3], s[4] >> 10, s[5], s[6], s[7], ms(s[8]), ms(slowest), ms(s[9]) + ); + } + last_sync = sync; // where the memory is: the process's commit, video memory, and XCL's parts of it let owner = owner.unwrap(); let private = owner.commit.unwrap_or(0);