diff --git a/README.md b/README.md index 4488386e58..848ad03dc0 100644 --- a/README.md +++ b/README.md @@ -383,9 +383,10 @@ xcl rpf D:\data\shaders.rpf --extract NAME --out folder line with what the title did in that time (presents, submits, draws, dispatches, frame-event waits, DirectStorage requests), a `memory:` line with the process's commit and video memory in use, a `memory by owner` line splitting the known XCL allocations from `other`, and sampled - texture resident / SRV-addressable / virtual texel totals. A negative `other` exposes an - overlapping or stale sample; unavailable adapter segments say `unknown`. Where the lines stop - changing is where the title stopped. + texture resident / SRV-addressable / virtual texel totals, and the foreground CPU time spent + looking for CPU writes before submits (write-watch bytes/ranges/pages, sparse copy phases, and + other refresh work). A negative `other` exposes an overlapping or stale sample; unavailable + adapter segments say `unknown`. Where the lines stop changing is where the title stopped. - `XCL_THREAD_DUMP_AT=s1,s2,…`: at each of those seconds after the start, the state of every thread of the title goes into `xcl.log`, for a title that stalls. - `XCL_TRACE_FAULTS=1` (any value): the first 32 access violations are logged as they are raised diff --git a/crates/xcl/src/hle/d3d12x/sparse.rs b/crates/xcl/src/hle/d3d12x/sparse.rs index 106bd0713d..79fcb2c963 100644 --- a/crates/xcl/src/hle/d3d12x/sparse.rs +++ b/crates/xcl/src/hle/d3d12x/sparse.rs @@ -1888,14 +1888,62 @@ 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)]; -/// [lock wait, chunk scan, upload] in µs, runs uploaded -static PHASES: [AtomicU64; 4] = [const { AtomicU64::new(0) }; 4]; +/// `XCL_ACTIVITY` look counters: whole µs, write-watch µs, dirty pages, looks, watched bytes, +/// watched ranges, sparse lock/scan/upload µs, and sparse runs. Callers touch them only while +/// activity reporting is enabled, so normal title submission pays no clocks or atomic updates. +static LOOK: [AtomicU64; 10] = [const { AtomicU64::new(0) }; 10]; + +/// One write-watch scan inside a look before title submission. +pub fn note_watch(t: std::time::Duration, pages: usize, watched: usize, ranges: usize) { + LOOK[1].fetch_add(t.as_micros() as u64, Ordering::Relaxed); + LOOK[2].fetch_add(pages as u64, Ordering::Relaxed); + LOOK[4].fetch_add(watched as u64, Ordering::Relaxed); + LOOK[5].fetch_add(ranges as u64, Ordering::Relaxed); +} + +/// The whole serialized look: write-watch, sparse propagation, and title-texture refresh. +pub fn note_look(t: std::time::Duration) { + LOOK[0].fetch_add(t.as_micros() as u64, Ordering::Relaxed); + LOOK[3].fetch_add(1, Ordering::Relaxed); +} + +fn note_sparse_phases( + lock: std::time::Duration, + scan: std::time::Duration, + upload: std::time::Duration, + runs: usize, +) { + LOOK[6].fetch_add(lock.as_micros() as u64, Ordering::Relaxed); + LOOK[7].fetch_add(scan.as_micros() as u64, Ordering::Relaxed); + LOOK[8].fetch_add(upload.as_micros() as u64, Ordering::Relaxed); + LOOK[9].fetch_add(runs as u64, Ordering::Relaxed); +} -pub fn note_watch(t: std::time::Duration, pages: usize) { - WATCH[0].fetch_add(t.as_micros() as u64, Ordering::Relaxed); - WATCH[1].fetch_add(pages as u64, Ordering::Relaxed); +pub fn look_counters() -> [u64; 10] { + std::array::from_fn(|i| LOOK[i].load(Ordering::Relaxed)) +} + +pub fn look_delta(now: [u64; 10], before: [u64; 10]) -> [u64; 10] { + std::array::from_fn(|i| now[i].wrapping_sub(before[i])) +} + +/// Foreground CPU cost of looks before title submits over one `XCL_ACTIVITY` interval. +pub fn look_line(d: [u64; 10], secs: u64) -> String { + let [all, watch, pages, looks, watched, ranges, lock, scan, upload, runs] = d; + let ms = |us: u64| us / 1000; + let copies = lock.saturating_add(scan).saturating_add(upload); + format!( + "looks before submits ({secs} s): {looks} looks took {} ms ({:.1} % of the time): write-watch {} ms ({:.1} MB in {:.1} ranges/look, {pages} pages written); sparse copies lock/scan/upload {}/{}/{} ms ({runs} runs); 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, + ranges as f64 / looks.max(1) as f64, + ms(lock), + ms(scan), + ms(upload), + ms(all.saturating_sub(watch.saturating_add(copies))) + ) } fn poll_unique(mut fences: Vec, key: impl Fn(&F) -> usize, mut completed: impl FnMut(&F) -> u64) -> Vec<(F, u64)> { @@ -1946,11 +1994,11 @@ unsafe fn poll_retirement_fences() -> Vec<(ID3D12Fence, u64)> { /// Before the title's ExecuteCommandLists on `queue`: the pages the CPU wrote (`dirty`, 4 KiB /// pages from the write-watch, sorted) and the reused tiles are copied asynchronously into video /// memory, and `queue` GPU-waits for that and for new tile mappings. -pub unsafe fn before_execute(queue: &ID3D12CommandQueue, dirty: &[usize], page: usize) { +pub unsafe fn before_execute(queue: &ID3D12CommandQueue, dirty: &[usize], page: usize, measure: bool) { let q = queue.as_raw() as usize; + let t0 = measure.then(std::time::Instant::now); let queue_gate = WAIT_GATES.lock().unwrap().entry(q).or_default().clone(); let _queue_serial = queue_gate.lock().unwrap(); - let t0 = std::time::Instant::now(); let observed = poll_retirement_fences(); let mut g = lock_state(); let Some(s) = g.as_mut() else { return }; @@ -1963,8 +2011,7 @@ pub unsafe fn before_execute(queue: &ID3D12CommandQueue, dirty: &[usize], page: let sparse_done = observed_completion(&observed, &s.fence, |known, current| known.as_raw() == current.as_raw()); s.retire_free_pools_with(sparse_done); } - let t_locked = std::time::Instant::now(); - PHASES[0].fetch_add((t_locked - t0).as_micros() as u64, Ordering::Relaxed); + let t_locked = t0.map(|_| std::time::Instant::now()); let zero_runs = std::mem::take(&mut s.to_zero); for &(a, n) in &zero_runs { s.clear_sent_range(a, n); @@ -1999,9 +2046,7 @@ pub unsafe fn before_execute(queue: &ID3D12CommandQueue, dirty: &[usize], page: x = end; } } - let t_scanned = std::time::Instant::now(); - PHASES[1].fetch_add((t_scanned - t_locked).as_micros() as u64, Ordering::Relaxed); - PHASES[3].fetch_add((zero_runs.len() + dirty_runs.len()) as u64, Ordering::Relaxed); + let t_scanned = t_locked.map(|_| std::time::Instant::now()); // Submit tile clears before CPU writes so overlapping CopyBufferRegion destinations have no WAW hazard. if !zero_runs.is_empty() { if !s.upload_zeros(&zero_runs) { @@ -2011,28 +2056,14 @@ pub unsafe fn before_execute(queue: &ID3D12CommandQueue, dirty: &[usize], page: if !dirty_runs.is_empty() { let _ = s.upload_dirty(&dirty_runs); } - PHASES[2].fetch_add(t_scanned.elapsed().as_micros() as u64, Ordering::Relaxed); - { - static CALLS: AtomicU64 = AtomicU64::new(0); - static BYTES: AtomicU64 = AtomicU64::new(0); - static MICROS: AtomicU64 = AtomicU64::new(0); - let bytes: usize = zero_runs.iter().chain(dirty_runs.iter()).map(|r| r.1).sum(); - let n = CALLS.fetch_add(1, Ordering::Relaxed) + 1; - let b = BYTES.fetch_add(bytes as u64, Ordering::Relaxed) + bytes as u64; - let us = MICROS.fetch_add(t0.elapsed().as_micros() as u64, Ordering::Relaxed) + t0.elapsed().as_micros() as u64; - if n.is_power_of_two() && n >= 16 { - crate::xlog_info!("sparse windows: {n} submits, {} MB of CPU writes uploaded, {} ms spent, {} tiles mapped", b >> 20, us / 1000, s.mapped.len()); - let ms = |x: &AtomicU64| x.load(Ordering::Relaxed) / 1000; - crate::xlog_info!( - "sparse windows timing: write-watch {} ms ({} dirty pages), lock wait {} ms, chunk scan {} ms, upload {} ms ({} runs)", - ms(&WATCH[0]), - WATCH[1].load(Ordering::Relaxed), - ms(&PHASES[0]), - ms(&PHASES[1]), - ms(&PHASES[2]), - PHASES[3].load(Ordering::Relaxed) - ); - } + if let (Some(t0), Some(t_locked), Some(t_scanned)) = (t0, t_locked, t_scanned) { + let t_uploaded = std::time::Instant::now(); + note_sparse_phases( + t_locked.duration_since(t0), + t_scanned.duration_since(t_locked), + t_uploaded.duration_since(t_scanned), + zero_runs.len() + dirty_runs.len(), + ); } dirty_runs.clear(); s.scratch_runs = dirty_runs; @@ -2196,6 +2227,33 @@ fn forget_in(s: &mut State, res: usize) { mod tests { use super::*; + #[test] + fn activity_look_line_splits_foreground_costs() { + let line = look_line([ + 830_000, + 610_000, + 4_800, + 240, + 240 * 1_641 << 20, + 240 * 69, + 40_000, + 30_000, + 50_000, + 90, + ], 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 in 69.0 ranges/look, 4800 pages written)"), "{line}"); + assert!(line.contains("sparse copies lock/scan/upload 40/30/50 ms (90 runs); the rest 100 ms"), "{line}"); + let idle = look_line([0; 10], 2); + assert!(idle.contains("0 looks took 0 ms (0.0 % of the time)"), "{idle}"); + assert!(idle.contains("0.0 MB in 0.0 ranges/look"), "{idle}"); + } + + #[test] + fn activity_look_counter_delta_survives_wrap() { + assert_eq!(look_delta([0; 10], [u64::MAX; 10]), [1; 10]); + } + #[test] fn writable_record_requires_every_touched_page_and_tracks_protection() { let mut mapped = FxHashMap::default(); diff --git a/crates/xcl/src/hle/d3d12x/title_textures.rs b/crates/xcl/src/hle/d3d12x/title_textures.rs index 0c6320851a..590c4fdfa5 100644 --- a/crates/xcl/src/hle/d3d12x/title_textures.rs +++ b/crates/xcl/src/hle/d3d12x/title_textures.rs @@ -879,8 +879,13 @@ pub(super) unsafe fn refresh(queue: *mut c_void) { if BUSY.with(|b| b.get()) { return; } + let measure = super::memory_report::enabled(); + let t0 = measure.then(std::time::Instant::now); let _refresh = REFRESH.lock().unwrap(); - own_work(|| refresh_once(queue)); + own_work(|| refresh_once(queue, measure)); + if let Some(t0) = t0 { + super::sparse::note_look(t0.elapsed()); + } } /// The pages of `[base, base + size)` written since the last look, added to `dirty` and reset @@ -903,16 +908,16 @@ unsafe fn take_written(base: usize, size: usize, buf: &mut [*mut c_void], dirty: } } -unsafe fn refresh_once(queue: *mut c_void) { +unsafe fn refresh_once(queue: *mut c_void, measure: bool) { let Some(u) = crate::gpu::uma::get() else { return }; let sparse = u.has_sparse(); if !sparse && CPU_TEXTURES_LEN.load(Ordering::Relaxed) == 0 { return; } recheck_gpu_view_sources(); - let watch_t0 = std::time::Instant::now(); let mut page = 0x1000u32; let ranges = cached_watch_ranges(u); + let watch_t0 = measure.then(std::time::Instant::now); let dirty_len = DIRTY_BUF.with(|dcell| { let mut dirty = dcell.borrow_mut(); dirty.clear(); @@ -928,7 +933,14 @@ unsafe fn refresh_once(queue: *mut c_void) { dirty.sort_unstable(); dirty.len() }); - super::sparse::note_watch(watch_t0.elapsed(), dirty_len); + if let Some(watch_t0) = watch_t0 { + super::sparse::note_watch( + watch_t0.elapsed(), + dirty_len, + ranges.iter().fold(0usize, |bytes, range| bytes.saturating_add(range.1)), + ranges.len(), + ); + } 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?) @@ -961,7 +973,7 @@ unsafe fn refresh_once(queue: *mut c_void) { if let Some(q) = ID3D12CommandQueue::from_raw_borrowed(&queue) { DIRTY_BUF.with(|dcell| { let dirty = dcell.borrow(); - super::sparse::before_execute(q, &dirty, page as usize); + super::sparse::before_execute(q, &dirty, page as usize, measure); super::vram::before_execute(q); }); } diff --git a/crates/xcl/src/host.rs b/crates/xcl/src/host.rs index c4730abac6..5dbcfbce30 100644 --- a/crates/xcl/src/host.rs +++ b/crates/xcl/src/host.rs @@ -107,6 +107,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(); @@ -115,6 +116,11 @@ 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] ); + let looks = crate::hle::d3d12x::sparse::look_counters(); + crate::xlog_info!("{}", crate::hle::d3d12x::sparse::look_line( + crate::hle::d3d12x::sparse::look_delta(looks, last_looks), 2, + )); + last_looks = looks; // where the memory is: the process's commit, video memory, and XCL's parts of it let owner = crate::hle::d3d12x::memory_report::snapshot(); let private = owner.commit.unwrap_or(0);