From fa2dca569f3dfdb83c1646ac66b069e94f80fb85 Mon Sep 17 00:00:00 2001 From: marquezini Date: Tue, 24 Nov 2026 18:34:48 -0300 Subject: [PATCH 1/2] D3D12.X: XCL_ACTIVITY logs the CPU time of the looks before each submit Before every title submit XCL looks for the CPU's writes (write-watch over the watched ranges), copies them into sparse video memory and uploads changed title textures, on the title's own thread. On slower CPUs that look is most of the frame (2-6 fps on an i9-9900K in #582), but the only record of it is the cumulative timing line logged at power-of-two submit counts, so a report at a few fps cannot say how much of each second went there. With XCL_ACTIVITY=1 every 2 s now also logs the looks of the last 2 s: how many, their whole time and its share of the 2 s, the write-watch scan with the megabytes it watched a look and the pages it found, the copies into sparse video memory, and the rest. Without XCL_ACTIVITY only counters are added: two Instant::now() calls per look and a sum over the cached ranges. --- HANDOFF.md | 2 + crates/xcl/src/hle/d3d12x/sparse.rs | 52 +++++++++++++++++++-- crates/xcl/src/hle/d3d12x/title_textures.rs | 4 +- crates/xcl/src/host.rs | 5 ++ 4 files changed, 59 insertions(+), 4 deletions(-) diff --git a/HANDOFF.md b/HANDOFF.md index 984a45360..cfebef3a8 100644 --- a/HANDOFF.md +++ b/HANDOFF.md @@ -1810,6 +1810,8 @@ to an outside server at every crash (`hle/netoff.rs`: the title's WinHTTP / Wins translated as the mesh shader they are, the title rendered nothing from then on (black every run). Open: what it expects of them. - `XCL_ACTIVITY` logs the process's commit, local / non-local video memory, target pages, arenas and sparse pools. + And the CPU time of XCL's looks before the title's submits (`looks before submits`): in all and as a share of + the 2 s, the write-watch scan with the MB it watched a look, the copies into sparse video memory, the rest. - Open: arenas for D32S8 targets still take 2.8 GB (one per 256 MB of addresses; a D32S8 → R32 + separate stencil split, or exact-size heaps, would cut it); the debug layer's resource-state errors (538) and sparse simultaneous-access errors (1047); buffer views outside unified memory still get an empty V#. diff --git a/crates/xcl/src/hle/d3d12x/sparse.rs b/crates/xcl/src/hle/d3d12x/sparse.rs index 91eb2ba35..12b332e95 100644 --- a/crates/xcl/src/hle/d3d12x/sparse.rs +++ b/crates/xcl/src/hle/d3d12x/sparse.rs @@ -1444,14 +1444,48 @@ pub fn mapped_ranges() -> Vec<(usize, usize)> { s.runs.clone().unwrap_or_default() } -/// time spent and pages found by the write-watch scans (`refresh_once`), for the timing line -static WATCH: [AtomicU64; 2] = [AtomicU64::new(0), AtomicU64::new(0)]; +/// time spent and pages found by the write-watch scans (`refresh_once`), for the timing line; +/// then the looks, the bytes they watched and the µs of the whole looks (`XCL_ACTIVITY`) +static WATCH: [AtomicU64; 5] = [const { AtomicU64::new(0) }; 5]; /// [lock wait, chunk scan, upload] in µs, runs uploaded static PHASES: [AtomicU64; 4] = [const { AtomicU64::new(0) }; 4]; -pub fn note_watch(t: std::time::Duration, pages: usize) { +/// A look's write-watch scan: `t` long, `pages` found written in `watched` bytes. +pub fn note_watch(t: std::time::Duration, pages: usize, watched: usize) { WATCH[0].fetch_add(t.as_micros() as u64, Ordering::Relaxed); WATCH[1].fetch_add(pages as u64, Ordering::Relaxed); + WATCH[2].fetch_add(1, Ordering::Relaxed); + WATCH[3].fetch_add(watched as u64, Ordering::Relaxed); +} + +/// A whole look before a title's submit (`title_textures::refresh`: the scan, the copies of +/// CPU writes, the textures uploaded again), `t` long. +pub fn note_look(t: std::time::Duration) { + WATCH[4].fetch_add(t.as_micros() as u64, Ordering::Relaxed); +} + +/// What the looks before the title's submits cost so far: [whole looks µs, write-watch µs, +/// pages written, looks, bytes watched, sparse copies µs (lock wait, chunk scan, upload)]. +pub fn look_counters() -> [u64; 6] { + let w = |i: usize| WATCH[i].load(Ordering::Relaxed); + let copies = PHASES[..3].iter().map(|x| x.load(Ordering::Relaxed)).sum(); + [w(4), w(0), w(1), w(2), w(3), copies] +} + +/// The `XCL_ACTIVITY` line for `d`, two `look_counters()` subtracted, `secs` apart: how much +/// of the title's submitting threads' time XCL's looks took, and where it went. +pub fn look_line(d: [u64; 6], secs: u64) -> String { + let [all, watch, pages, looks, watched, copies] = d; + let ms = |us: u64| us / 1000; + format!( + "looks before submits ({secs} s): {looks} looks took {} ms ({:.1} % of the time): write-watch {} ms ({:.1} MB watched a look, {pages} pages written), copies into sparse video memory {} ms, the rest {} ms", + ms(all), + all as f64 / (secs.max(1) * 10_000) as f64, + ms(watch), + watched as f64 / looks.max(1) as f64 / (1 << 20) as f64, + ms(copies), + ms(all.saturating_sub(watch + copies)) + ) } /// Before the title's ExecuteCommandLists on `queue`: the pages the CPU wrote (`dirty`, 4 KiB @@ -1636,6 +1670,18 @@ fn forget_in(s: &mut State, res: usize) { mod tests { use super::*; + /// The `XCL_ACTIVITY` line: the parts of the looks, the share of the 2 s, and no division by + /// zero for a period without a look. + #[test] + fn the_look_line_splits_the_time() { + let line = look_line([830_000, 610_000, 4_800, 240, 240 * 1_641 << 20, 120_000], 2); + assert!(line.contains("240 looks took 830 ms (41.5 % of the time)"), "{line}"); + assert!(line.contains("write-watch 610 ms (1641.0 MB watched a look, 4800 pages written)"), "{line}"); + assert!(line.contains("copies into sparse video memory 120 ms, the rest 100 ms"), "{line}"); + let idle = look_line([0; 6], 2); + assert!(idle.contains("0 looks took 0 ms (0.0 % of the time)") && idle.contains("0.0 MB watched a look"), "{idle}"); + } + /// `queue()` answers while the state's lock is held: a resource's last `Release` asks for /// it (`late_release::drain_known`) from inside a D3D12 call that the lock's holder can be /// waiting for (a list's `Reset` against an upload's allocator `Reset`, with `XCL_DRED=1`). diff --git a/crates/xcl/src/hle/d3d12x/title_textures.rs b/crates/xcl/src/hle/d3d12x/title_textures.rs index 683b05593..48607de4f 100644 --- a/crates/xcl/src/hle/d3d12x/title_textures.rs +++ b/crates/xcl/src/hle/d3d12x/title_textures.rs @@ -691,8 +691,10 @@ pub(super) unsafe fn refresh(queue: *mut c_void) { if BUSY.with(|b| b.get()) { return; } + let t0 = std::time::Instant::now(); let _refresh = REFRESH.lock().unwrap(); own_work(|| refresh_once(queue)); + super::sparse::note_look(t0.elapsed()); } /// The pages of `[base, base + size)` written since the last look, added to `dirty` and reset @@ -740,7 +742,7 @@ unsafe fn refresh_once(queue: *mut c_void) { dirty.sort_unstable(); dirty.len() }); - super::sparse::note_watch(watch_t0.elapsed(), dirty_len); + super::sparse::note_watch(watch_t0.elapsed(), dirty_len, ranges.iter().map(|r| r.1).sum()); if super::present::trace_frame() { // every 600th look: what the CPU's view of a few textures holds now (does the title's // data reach the address at all, and is the texture still kept in step?) diff --git a/crates/xcl/src/host.rs b/crates/xcl/src/host.rs index 71a25c7c1..8e7e54797 100644 --- a/crates/xcl/src/host.rs +++ b/crates/xcl/src/host.rs @@ -50,6 +50,7 @@ pub fn host_main(boot_path: &Path) -> i32 { [p, e, d, x, f[0], f[1], f[2], f[3], r, b >> 20, fail] }; let mut last = snap(); + let mut last_looks = crate::hle::d3d12x::sparse::look_counters(); loop { std::thread::sleep(std::time::Duration::from_secs(2)); let now = snap(); @@ -58,6 +59,10 @@ pub fn host_main(boot_path: &Path) -> i32 { "activity (2 s): {} presents, {} submits, {} draws, {} dispatches, {} frame-event waits ({} timed out), {} frame statistics ({} not flipped); DirectStorage {} requests, {} MB, {} failed", d[0], d[1], d[2], d[3], d[4], d[5], d[6], d[7], d[8], d[9], d[10] ); + // XCL's own CPU time before the title's submits (on the title's threads) + let looks = crate::hle::d3d12x::sparse::look_counters(); + crate::xlog_info!("{}", crate::hle::d3d12x::sparse::look_line(std::array::from_fn(|i| looks[i] - last_looks[i]), 2)); + last_looks = looks; // where the memory is: the process's commit, video memory, and XCL's parts of it let private = unsafe { use windows_sys::Win32::System::ProcessStatus::{K32GetProcessMemoryInfo, PROCESS_MEMORY_COUNTERS_EX}; From f8eeef4de89d38c4d321c7f0d80a8191f89e5692 Mon Sep 17 00:00:00 2001 From: Ivan John Ozimchuk <3846306+ozimchuk@users.noreply.github.com> Date: Wed, 25 Nov 2026 16:52:56 +0200 Subject: [PATCH 2/2] D3D12.X: the looks line splits "the rest" and shows the longest look The `looks before submits` line (#1832) leaves "the rest" of a look as one number. Its parts are timed now: the view-source recheck, the VRAM pages' sync, the recheck of streamed textures outside the write-watch, choosing the textures to upload, and the uploads (MB; the content hash with its read, the detile, the submit with its wait), with what is left as "other". The line also gives the longest look of the period: one slow look is a stall an average hides. Co-Authored-By: Claude Opus 5.5 --- HANDOFF.md | 4 +- crates/xcl/src/hle/d3d12x/sparse.rs | 46 ++++++++++++--- crates/xcl/src/hle/d3d12x/title_textures.rs | 63 ++++++++++++++++++++- crates/xcl/src/host.rs | 9 ++- 4 files changed, 110 insertions(+), 12 deletions(-) diff --git a/HANDOFF.md b/HANDOFF.md index cfebef3a8..6b0ee63d1 100644 --- a/HANDOFF.md +++ b/HANDOFF.md @@ -1811,7 +1811,9 @@ to an outside server at every crash (`hle/netoff.rs`: the title's WinHTTP / Wins it expects of them. - `XCL_ACTIVITY` logs the process's commit, local / non-local video memory, target pages, arenas and sparse pools. And the CPU time of XCL's looks before the title's submits (`looks before submits`): in all and as a share of - the 2 s, the write-watch scan with the MB it watched a look, the copies into sparse video memory, the rest. + the 2 s, the longest look, the write-watch scan with the MB it watched a look, the copies into sparse video + memory, and the rest by its parts: the view-source recheck, the VRAM pages' sync, the streamed-texture recheck, + choosing the uploads, and the uploads (MB; content hash, detile, submit). - Open: arenas for D32S8 targets still take 2.8 GB (one per 256 MB of addresses; a D32S8 → R32 + separate stencil split, or exact-size heaps, would cut it); the debug layer's resource-state errors (538) and sparse simultaneous-access errors (1047); buffer views outside unified memory still get an empty V#. diff --git a/crates/xcl/src/hle/d3d12x/sparse.rs b/crates/xcl/src/hle/d3d12x/sparse.rs index 12b332e95..cd66ef35c 100644 --- a/crates/xcl/src/hle/d3d12x/sparse.rs +++ b/crates/xcl/src/hle/d3d12x/sparse.rs @@ -1462,6 +1462,15 @@ pub fn note_watch(t: std::time::Duration, pages: usize, watched: usize) { /// CPU writes, the textures uploaded again), `t` long. pub fn note_look(t: std::time::Duration) { WATCH[4].fetch_add(t.as_micros() as u64, Ordering::Relaxed); + LONGEST_LOOK.fetch_max(t.as_micros() as u64, Ordering::Relaxed); +} + +/// The longest look since the last `take_longest_look`, in µs. +static LONGEST_LOOK: AtomicU64 = AtomicU64::new(0); + +/// The longest look since the last call (µs; the line's one reader resets it). +pub fn take_longest_look() -> u64 { + LONGEST_LOOK.swap(0, Ordering::Relaxed) } /// What the looks before the title's submits cost so far: [whole looks µs, write-watch µs, @@ -1473,18 +1482,33 @@ pub fn look_counters() -> [u64; 6] { } /// The `XCL_ACTIVITY` line for `d`, two `look_counters()` subtracted, `secs` apart: how much -/// of the title's submitting threads' time XCL's looks took, and where it went. -pub fn look_line(d: [u64; 6], secs: u64) -> String { +/// of the title's submitting threads' time XCL's looks took, and where it went. `rest`: the +/// same for `title_textures::look_parts()` (what the rest was spent on), `longest`: the +/// longest look in the period (µs). +pub fn look_line(d: [u64; 6], rest: [u64; 8], longest: u64, secs: u64) -> String { let [all, watch, pages, looks, watched, copies] = d; + let [recheck, vram, streamed, select, hash, detile, submit, bytes] = rest; let ms = |us: u64| us / 1000; + let the_rest = all.saturating_sub(watch + copies); + let named = recheck + vram + streamed + select + hash + detile + submit; format!( - "looks before submits ({secs} s): {looks} looks took {} ms ({:.1} % of the time): write-watch {} ms ({:.1} MB watched a look, {pages} pages written), copies into sparse video memory {} ms, the rest {} ms", + "looks before submits ({secs} s): {looks} looks took {} ms ({:.1} % of the time, longest {} ms): write-watch {} ms ({:.1} MB watched a look, {pages} pages written), copies into sparse video memory {} ms, the rest {} ms (view-source recheck {}, VRAM pages {}, streamed-texture recheck {}, choosing uploads {}, uploads {} MB: hash {}, detile {}, submit {}; other {})", ms(all), all as f64 / (secs.max(1) * 10_000) as f64, + ms(longest), ms(watch), watched as f64 / looks.max(1) as f64 / (1 << 20) as f64, ms(copies), - ms(all.saturating_sub(watch + copies)) + ms(the_rest), + ms(recheck), + ms(vram), + ms(streamed), + ms(select), + bytes >> 20, + ms(hash), + ms(detile), + ms(submit), + ms(the_rest.saturating_sub(named)) ) } @@ -1674,12 +1698,18 @@ mod tests { /// zero for a period without a look. #[test] fn the_look_line_splits_the_time() { - let line = look_line([830_000, 610_000, 4_800, 240, 240 * 1_641 << 20, 120_000], 2); - assert!(line.contains("240 looks took 830 ms (41.5 % of the time)"), "{line}"); + let rest = [10_000, 5_000, 2_000, 8_000, 20_000, 30_000, 15_000, 48 << 20]; + let line = look_line([830_000, 610_000, 4_800, 240, 240 * 1_641 << 20, 120_000], rest, 41_000, 2); + assert!(line.contains("240 looks took 830 ms (41.5 % of the time, longest 41 ms)"), "{line}"); assert!(line.contains("write-watch 610 ms (1641.0 MB watched a look, 4800 pages written)"), "{line}"); assert!(line.contains("copies into sparse video memory 120 ms, the rest 100 ms"), "{line}"); - let idle = look_line([0; 6], 2); - assert!(idle.contains("0 looks took 0 ms (0.0 % of the time)") && idle.contains("0.0 MB watched a look"), "{idle}"); + assert!( + line.contains("(view-source recheck 10, VRAM pages 5, streamed-texture recheck 2, choosing uploads 8, uploads 48 MB: hash 20, detile 30, submit 15; other 10)"), + "{line}" + ); + let idle = look_line([0; 6], [0; 8], 0, 2); + assert!(idle.contains("0 looks took 0 ms (0.0 % of the time, longest 0 ms)") && idle.contains("0.0 MB watched a look"), "{idle}"); + assert!(idle.contains("; other 0)"), "{idle}"); } /// `queue()` answers while the state's lock is held: a resource's last `Release` asks for diff --git a/crates/xcl/src/hle/d3d12x/title_textures.rs b/crates/xcl/src/hle/d3d12x/title_textures.rs index 48607de4f..4747e2e1b 100644 --- a/crates/xcl/src/hle/d3d12x/title_textures.rs +++ b/crates/xcl/src/hle/d3d12x/title_textures.rs @@ -717,13 +717,56 @@ unsafe fn take_written(base: usize, size: usize, buf: &mut [*mut c_void], dirty: } } +/// `XCL_ACTIVITY`: what "the rest" of a look (`sparse::look_line`) was spent on, in µs – the +/// view-source recheck, the VRAM pages' sync, the recheck of streamed textures outside the +/// write-watch, choosing the textures to upload, and of their uploads the content hash (with +/// its read), the detile and the submit (with its wait) – and the bytes uploaded. +#[derive(Clone, Copy)] +#[repr(usize)] +enum Part { + Recheck, + Vram, + Streamed, + Select, + Hash, + Detile, + Submit, + Bytes, +} +static PARTS: [AtomicU64; 8] = [const { AtomicU64::new(0) }; 8]; + +/// Whether the looks are timed (`XCL_ACTIVITY`, as `host` reads it). +fn timed() -> bool { + static ON: std::sync::OnceLock = std::sync::OnceLock::new(); + *ON.get_or_init(|| std::env::var_os("XCL_ACTIVITY").is_some()) +} + +/// `Instant::now()` when the looks are timed. +fn part_start() -> Option { + timed().then(std::time::Instant::now) +} + +/// Add the time since `t0` to `p` (nothing when the looks are not timed: `t0` is `None`). +fn add_part(p: Part, t0: Option) { + if let Some(t0) = t0 { + PARTS[p as usize].fetch_add(t0.elapsed().as_micros() as u64, Ordering::Relaxed); + } +} + +/// The parts of the looks so far, in `Part` order (µs; the last: bytes uploaded). +pub fn look_parts() -> [u64; 8] { + std::array::from_fn(|i| PARTS[i].load(Ordering::Relaxed)) +} + unsafe fn refresh_once(queue: *mut c_void) { let Some(u) = crate::gpu::uma::get() else { return }; let sparse = u.has_sparse(); if !sparse && TEXTURES_LEN.load(Ordering::Relaxed) == 0 { return; } + let t0 = part_start(); recheck_gpu_view_sources(); + add_part(Part::Recheck, t0); let watch_t0 = std::time::Instant::now(); let mut page = 0x1000u32; let ranges = cached_watch_ranges(u); @@ -777,19 +820,24 @@ unsafe fn refresh_once(queue: *mut c_void) { DIRTY_BUF.with(|dcell| { let dirty = dcell.borrow(); super::sparse::before_execute(q, &dirty, page as usize); + let t0 = part_start(); super::vram::before_execute(q); + add_part(Part::Vram, t0); }); } let recheck_frame = { let (p, b) = super::present::counters(); p.max(b) }; + let t0 = part_start(); recheck_streamed_unwatched(&ranges, recheck_frame); + add_part(Part::Streamed, t0); if dirty_len == 0 && PENDING.load(Ordering::Relaxed) == 0 { return; } // textures made since the last submit and those with a written page; a reference each // while they are uploaded + let t0 = part_start(); let todo: Vec<(ID3D12Resource, Upload)> = DIRTY_BUF.with(|dcell| { let dirty = dcell.borrow(); let mut t = TEXTURES.lock().unwrap(); @@ -816,6 +864,7 @@ unsafe fn refresh_once(queue: *mut c_void) { .collect() }); seed_stream_recheck(&todo, &ranges, recheck_frame); + add_part(Part::Select, t0); upload_all(todo); } @@ -1099,7 +1148,9 @@ unsafe fn upload_all(todo: Vec<(ID3D12Resource, Upload)>) { // retail title's video planes and UI targets went up again for every submit, ~25 MB // a presented frame where ~3 MB had changed, and the submits waited here. if unchanged_skip() { + let t0 = part_start(); let h = content_hash(&title_bytes(va, tex.size() as usize)); + add_part(Part::Hash, t0); if same_as_last_upload(r.as_raw() as usize, h) { let n = UNCHANGED.fetch_add(1, Ordering::Relaxed) + 1; if n == 1 || (n.is_power_of_two() && n >= 1024) { @@ -1115,7 +1166,12 @@ unsafe fn upload_all(todo: Vec<(ID3D12Resource, Upload)>) { if picked.is_empty() { continue; } + let t0 = part_start(); let subs = subresources_of_all(&picked, bytes); + add_part(Part::Detile, t0); + if t0.is_some() { + PARTS[Part::Bytes as usize].fetch_add(bytes as u64, Ordering::Relaxed); + } let mut batch = Vec::with_capacity(picked.len()); for ((r, va, _, states), subs) in picked.into_iter().zip(subs) { if super::present::trace_frame() { @@ -1132,10 +1188,13 @@ unsafe fn upload_all(todo: Vec<(ID3D12Resource, Upload)>) { continue; } }; - match own_work(|| { + let t0 = part_start(); + let result = own_work(|| { super::vram::before_execute(&queue); texture::upload_batch(dev, &queue, &batch) - }) { + }); + add_part(Part::Submit, t0); + match result { Ok(failed) => { for (_, e) in failed { warn(&e); diff --git a/crates/xcl/src/host.rs b/crates/xcl/src/host.rs index 8e7e54797..1ca57fa38 100644 --- a/crates/xcl/src/host.rs +++ b/crates/xcl/src/host.rs @@ -51,6 +51,7 @@ pub fn host_main(boot_path: &Path) -> i32 { }; let mut last = snap(); let mut last_looks = crate::hle::d3d12x::sparse::look_counters(); + let mut last_parts = crate::hle::d3d12x::title_textures::look_parts(); loop { std::thread::sleep(std::time::Duration::from_secs(2)); let now = snap(); @@ -61,8 +62,14 @@ pub fn host_main(boot_path: &Path) -> i32 { ); // XCL's own CPU time before the title's submits (on the title's threads) let looks = crate::hle::d3d12x::sparse::look_counters(); - crate::xlog_info!("{}", crate::hle::d3d12x::sparse::look_line(std::array::from_fn(|i| looks[i] - last_looks[i]), 2)); + let parts = crate::hle::d3d12x::title_textures::look_parts(); + let longest = crate::hle::d3d12x::sparse::take_longest_look(); + crate::xlog_info!( + "{}", + crate::hle::d3d12x::sparse::look_line(std::array::from_fn(|i| looks[i] - last_looks[i]), std::array::from_fn(|i| parts[i] - last_parts[i]), longest, 2) + ); last_looks = looks; + last_parts = parts; // where the memory is: the process's commit, video memory, and XCL's parts of it let private = unsafe { use windows_sys::Win32::System::ProcessStatus::{K32GetProcessMemoryInfo, PROCESS_MEMORY_COUNTERS_EX};