From 49e8f5fcf754c178f5a95a0294fd4dd68cb6b2bf Mon Sep 17 00:00:00 2001 From: Chris Tomlin Date: Sun, 29 Nov 2026 09:07:17 +0000 Subject: [PATCH 1/2] D3D12.X: a memory sample no longer makes the UMA window memory_report::snapshot() read the UMA window's size through uma::get(), which reserves and commits the window on first use. A sample in a process that had no window yet (a fresh child, a test) committed it, and so changed the memory it was measuring. uma::existing() returns the window only if something else made it. The child-process regression and the accessor are @thehollowedone's from their review of #2697. The test fails without the fix ("a memory sample made the UMA window") and passes with it. Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/gpu/uma.rs | 5 +++++ crates/xcl/src/hle/d3d12x/memory_report.rs | 20 +++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/crates/xcl/src/gpu/uma.rs b/crates/xcl/src/gpu/uma.rs index e4e34f0166..fb005d157d 100644 --- a/crates/xcl/src/gpu/uma.rs +++ b/crates/xcl/src/gpu/uma.rs @@ -987,6 +987,11 @@ impl Arena { static GLOBAL: OnceLock> = OnceLock::new(); +/// The window, if a caller has already made it: for observers, which must not commit it. +pub fn existing() -> Option<&'static Uma> { + GLOBAL.get().and_then(Option::as_ref) +} + /// A process that holds no title memory (a shader preparation worker): `get` is None from here /// on, and nothing is reserved or committed for it (a run's region commits its coherent window). /// Before any `get`. diff --git a/crates/xcl/src/hle/d3d12x/memory_report.rs b/crates/xcl/src/hle/d3d12x/memory_report.rs index 79ff78e38a..912c38bac0 100644 --- a/crates/xcl/src/hle/d3d12x/memory_report.rs +++ b/crates/xcl/src/hle/d3d12x/memory_report.rs @@ -114,7 +114,7 @@ pub fn snapshot() -> Snapshot { let (pools, mapped, retired, sparse_commit) = super::sparse::memory_bytes_for(device); let (_, committed_textures) = super::title_textures::committed_bytes_for(device); let (local, non_local) = video_memory(raw); - let uma = crate::gpu::uma::get().map_or(0, |u| u64::try_from(u.size()).unwrap_or(u64::MAX)); + let uma = crate::gpu::uma::existing().map_or(0, |u| u64::try_from(u.size()).unwrap_or(u64::MAX)); let b = |n: usize| u64::try_from(n).unwrap_or(u64::MAX); let parts = vec![ Part { owner: "UMA window", kind: Kind::Commit, bytes: uma }, @@ -134,6 +134,24 @@ pub fn snapshot() -> Snapshot { mod tests { use super::*; + /// A sample in a process with no UMA window yet (a fresh child) leaves it unmade. + #[test] + fn snapshot_does_not_initialize_uma() { + const CHILD: &str = "XCL_TEST_MEMORY_SNAPSHOT_CHILD"; + if std::env::var_os(CHILD).is_none() { + let output = std::process::Command::new(std::env::current_exe().unwrap()) + .args(["--exact", "hle::d3d12x::memory_report::tests::snapshot_does_not_initialize_uma", "--nocapture"]) + .env(CHILD, "1") + .output() + .unwrap(); + assert!(output.status.success(), "{}{}", String::from_utf8_lossy(&output.stdout), String::from_utf8_lossy(&output.stderr)); + return; + } + assert!(crate::gpu::uma::existing().is_none()); + let _ = snapshot(); + assert!(crate::gpu::uma::existing().is_none(), "a memory sample made the UMA window"); + } + #[test] fn remainder_and_partial_query_failure_are_explicit() { const M: u64 = 1 << 20; From 0f0cc3429f04c5a57192a1e414de8aa50682dec6 Mon Sep 17 00:00:00 2001 From: Chris Tomlin Date: Sun, 29 Nov 2026 09:08:22 +0000 Subject: [PATCH 2/2] D3D12.X: memory by owner counts the D3D12 heaps' charge to commit Windows charges a process's D3D12 heaps, local and non-local, to its commit, so `memory by owner` left them in commit `other` and counted them again under video memory. The new `D3D12 heaps` part is both DXGI segments' usage less the committed windows the GPU reads in place (imported, or remade as a driver heap on NVIDIA), which the `UMA window` part already has; a shadowed window is a separate copy and is not subtracted. The part and uma_imported_bytes() are from #2697 (@joeypercia), adapted to main's per-window heap list. On an RTX 3080 (621.40), with a retail title in the world, a VirtualAlloc trace put 19,470 MB of commit on dxgmms2.sys against 19,468 MB of local + non-local, and VMMap's write-combined private regions matched local + non-local less the 4,095 MB window to ~150 MB (#3211). Co-Authored-By: Claude Opus 5.5 --- crates/xcl/src/hle/d3d12x/memory_report.rs | 27 +++++++++++++++++++++- crates/xcl/src/hle/d3d12x/real.rs | 11 +++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/crates/xcl/src/hle/d3d12x/memory_report.rs b/crates/xcl/src/hle/d3d12x/memory_report.rs index 912c38bac0..e57fa0f66b 100644 --- a/crates/xcl/src/hle/d3d12x/memory_report.rs +++ b/crates/xcl/src/hle/d3d12x/memory_report.rs @@ -106,6 +106,16 @@ pub fn local_segment() -> Option { video_memory(super::real::device_raw()).0 } +/// Windows charges a process's D3D12 heaps, local and non-local, to its commit (1 GiB heap: +/// +1 GiB of commit; #2697): all DXGI counts but the windows the GPU reads in place, which the +/// UMA window part has. +fn heap_charge(local: Option, non_local: Option, in_place: u64) -> u64 { + match (local, non_local) { + (Some(l), Some(n)) => l.usage.saturating_add(n.usage).saturating_sub(in_place), + _ => 0, + } +} + pub fn snapshot() -> Snapshot { let raw = super::real::device_raw(); let device = raw as usize; @@ -119,6 +129,7 @@ pub fn snapshot() -> Snapshot { let parts = vec![ Part { owner: "UMA window", kind: Kind::Commit, bytes: uma }, Part { owner: "sparse CPU copy", kind: Kind::Commit, bytes: b(sparse_commit) }, + Part { owner: "D3D12 heaps", kind: Kind::Commit, bytes: heap_charge(local, non_local, super::real::uma_imported_bytes()) }, Part { owner: "target pages", kind: Kind::Video, bytes: b(pages) }, Part { owner: "target pools free", kind: Kind::Video, bytes: b(page_pools.saturating_sub(pages)) }, Part { owner: "arenas", kind: Kind::Video, bytes: b(arenas) }, @@ -183,6 +194,20 @@ mod tests { assert!(line.contains("non-local 700 of a 2000 budget (upload staging 400, other 300)"), "{line}"); } + /// Both segments' usage less what the GPU reads in place; nothing if either query failed. + #[test] + fn heap_charge_is_both_segments_less_the_windows_read_in_place() { + const M: u64 = 1 << 20; + let seg = |usage| Some(Segment { usage, budget: 16_000 * M }); + // the RTX 3080 run of #3211: local 14,208 + non-local 5,260, the 4,095 MB window remade + assert_eq!(heap_charge(seg(14_208 * M), seg(5_260 * M), 4_095 * M), 15_373 * M); + assert_eq!(heap_charge(seg(14_208 * M), seg(5_260 * M), 0), 19_468 * M); + assert_eq!(heap_charge(seg(100 * M), seg(0), 4_095 * M), 0); + assert_eq!(heap_charge(seg(u64::MAX), seg(1), 0), u64::MAX); + assert_eq!(heap_charge(None, seg(5_260 * M), 0), 0); + assert_eq!(heap_charge(seg(14_208 * M), None, 0), 0); + } + #[test] fn signed_remainders_truncate_and_saturate() { let tiny = Snapshot { @@ -213,7 +238,7 @@ mod tests { let s = snapshot(); assert!(s.local.expect("local video memory").budget > 0, "{}", s.line()); assert!(s.commit.is_some()); - for owner in ["UMA window", "sparse CPU copy", "target pages", "target pools free", "arenas", "sparse tiles", "sparse pools free", "sparse pools retired", "tracked committed sampled textures"] { + for owner in ["UMA window", "sparse CPU copy", "D3D12 heaps", "target pages", "target pools free", "arenas", "sparse tiles", "sparse pools free", "sparse pools retired", "tracked committed sampled textures"] { assert!(s.parts.iter().any(|p| p.owner == owner), "{owner} missing: {}", s.line()); } } diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index eafeccd069..74616e0c4e 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -471,6 +471,17 @@ static UMA_BUFFER: Mutex> = Mutex::new(Vec::new()); /// `XCL_UMA_DRIVER=mixed:...`). static UMA_HEAPS: Mutex>> = Mutex::new(Vec::new()); +/// Bytes of the committed windows the GPU reads in place (imported, or remade as a driver heap): +/// those DXGI counts as well as commit. 0 before they are opened, and when they are shadowed +/// (the GPU then reads a separate copy in video memory). +pub fn uma_imported_bytes() -> u64 { + let Some(u) = crate::gpu::uma::existing() else { return 0 }; + if u.shadowed() { + return 0; + } + UMA_HEAPS.lock().unwrap().iter().flatten().map(|&(_, len, _)| len as u64).sum() +} + fn uma_heap_for(k: usize, offset: usize, len: usize) -> Option<(usize, usize)> { let g = UMA_HEAPS.lock().unwrap(); let segs = g.get(k)?;