Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions HANDOFF.md
Original file line number Diff line number Diff line change
Expand Up @@ -1810,6 +1810,10 @@ 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 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#.
Expand Down
82 changes: 79 additions & 3 deletions crates/xcl/src/hle/d3d12x/sparse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1444,14 +1444,72 @@ 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);
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,
/// 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. `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, 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(the_rest),
ms(recheck),
ms(vram),
ms(streamed),
ms(select),
bytes >> 20,
ms(hash),
ms(detile),
ms(submit),
ms(the_rest.saturating_sub(named))
)
}

/// Before the title's ExecuteCommandLists on `queue`: the pages the CPU wrote (`dirty`, 4 KiB
Expand Down Expand Up @@ -1636,6 +1694,24 @@ 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 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}");
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
/// 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`).
Expand Down
67 changes: 64 additions & 3 deletions crates/xcl/src/hle/d3d12x/title_textures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -715,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<bool> = 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<std::time::Instant> {
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<std::time::Instant>) {
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);
Expand All @@ -740,7 +785,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?)
Expand Down Expand Up @@ -775,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();
Expand All @@ -814,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);
}

Expand Down Expand Up @@ -1097,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) {
Expand All @@ -1113,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() {
Expand All @@ -1130,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);
Expand Down
12 changes: 12 additions & 0 deletions crates/xcl/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ 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();
let mut last_parts = crate::hle::d3d12x::title_textures::look_parts();
loop {
std::thread::sleep(std::time::Duration::from_secs(2));
let now = snap();
Expand All @@ -58,6 +60,16 @@ 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();
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};
Expand Down
Loading