diff --git a/crates/xcl/src/hle/d3d12x/memory_report.rs b/crates/xcl/src/hle/d3d12x/memory_report.rs new file mode 100644 index 0000000000..baa1e91f7b --- /dev/null +++ b/crates/xcl/src/hle/d3d12x/memory_report.rs @@ -0,0 +1,241 @@ +//! Where XCL's memory goes: the parts it knows of, against what Windows and DXGI report (#2688, +//! workstream 1). One snapshot holds the process's commit, the adapter's local (video) and +//! non-local (system) memory in use against their budgets, and XCL's owners of each. What the +//! owners don't cover is shown as its own part: the title's own resources, the driver, and +//! whatever XCL doesn't count yet. + +/// Which memory a part takes. +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +pub enum Kind { + /// the adapter's own memory (DXGI local segment) + Video, + /// system memory the GPU reads (DXGI non-local segment: upload and readback heaps) + NonLocal, + /// the process's commit + Commit, +} + +/// One owner's bytes. +#[derive(Clone, Debug, PartialEq)] +pub struct Part { + pub owner: &'static str, + pub kind: Kind, + pub bytes: u64, +} + +/// What DXGI reports for one segment group. +#[derive(Clone, Copy, Debug, Default, PartialEq)] +pub struct Segment { + pub usage: u64, + pub budget: u64, +} + +#[derive(Clone, Debug, Default)] +pub struct Snapshot { + /// the process's committed bytes (`PrivateUsage`; None where it reads 0: Wine leaves it so) + pub commit: Option, + /// None: no device or DXGI did not answer + pub local: Option, + pub non_local: Option, + pub parts: Vec, +} + +impl Snapshot { + /// The bytes of the parts of `kind`. + pub fn counted(&self, kind: Kind) -> u64 { + self.parts.iter().filter(|p| p.kind == kind).map(|p| p.bytes).sum() + } + + /// What the total of `kind` holds besides the parts XCL counts (negative when the parts are + /// more than the total: a part counted on the wrong side, or memory not made yet). + pub fn other(&self, kind: Kind) -> Option { + let total = match kind { + Kind::Video => self.local?.usage, + Kind::NonLocal => self.non_local?.usage, + Kind::Commit => self.commit?, + }; + Some(total as i64 - self.counted(kind) as i64) + } + + /// One log line: each total, its budget, its parts and what is left (MB). + pub fn line(&self) -> String { + let mb = |b: u64| b >> 20; + let smb = |b: i64| b >> 20; + let parts = |kind: Kind| { + let mut s: Vec = self.parts.iter().filter(|p| p.kind == kind).map(|p| format!("{} {}", p.owner, mb(p.bytes))).collect(); + if let Some(o) = self.other(kind) { + s.push(format!("other {}", smb(o))); + } + s.join(", ") + }; + let segment = |name: &str, seg: Option, kind: Kind| match seg { + Some(g) => format!("{name} {} of a {} budget ({})", mb(g.usage), mb(g.budget), parts(kind)), + None => format!("{name} unknown ({})", parts(kind)), + }; + format!( + // (not "memory: N MB committed": the line `scripts/smoke.ps1` reads stays as it is) + "memory by owner (MB): commit {} ({}); {}; {}", + self.commit.map_or("unknown".into(), |c| mb(c).to_string()), + parts(Kind::Commit), + segment("video memory", self.local, Kind::Video), + segment("non-local", self.non_local, Kind::NonLocal) + ) + } +} + +/// The process's committed bytes, or None where `PrivateUsage` reads 0 (Wine leaves it so; #1097 +/// and #2362 read the commit there another way). +fn process_commit() -> Option { + unsafe { + use windows_sys::Win32::System::ProcessStatus::{K32GetProcessMemoryInfo, PROCESS_MEMORY_COUNTERS_EX}; + let mut c: PROCESS_MEMORY_COUNTERS_EX = std::mem::zeroed(); + c.cb = std::mem::size_of_val(&c) as u32; + K32GetProcessMemoryInfo(windows_sys::Win32::System::Threading::GetCurrentProcess(), &mut c as *mut _ as *mut _, c.cb); + Some(c.PrivateUsage as u64).filter(|&n| n != 0) + } +} + +/// What DXGI reports for the device's adapter: (local, non-local), or None without a device. +fn video_memory() -> Option<(Segment, Segment)> { + use windows::core::Interface; + use windows::Win32::Graphics::Direct3D12::ID3D12Device; + use windows::Win32::Graphics::Dxgi::*; + let raw = super::real::device_raw(); + let dev = unsafe { ID3D12Device::from_raw_borrowed(&raw) }?; + unsafe { + let factory: IDXGIFactory4 = CreateDXGIFactory1().ok()?; + let adapter: IDXGIAdapter3 = factory.EnumAdapterByLuid(dev.GetAdapterLuid()).ok()?; + let query = |group| { + let mut i = DXGI_QUERY_VIDEO_MEMORY_INFO::default(); + adapter.QueryVideoMemoryInfo(0, group, &mut i).ok().map(|_| Segment { usage: i.CurrentUsage, budget: i.Budget }) + }; + Some((query(DXGI_MEMORY_SEGMENT_GROUP_LOCAL)?, query(DXGI_MEMORY_SEGMENT_GROUP_NON_LOCAL)?)) + } +} + +/// The commit Windows charges for the process's D3D12 heaps: what DXGI counts in both segments, +/// less the coherent window D3D12 imported (`imported`: CPU memory, already committed as such); +/// 0 when unknown. +fn heap_charge(local: Option, non_local: Option, imported: u64) -> u64 { + match (local, non_local) { + (Some(l), Some(n)) => (l.usage + n.usage).saturating_sub(imported), + _ => 0, + } +} + +/// Where DXGI counts memory the GPU reads from system memory: the non-local segment, or on an +/// adapter with one segment (no non-local budget: an integrated GPU) the local one. +fn uma_segment(non_local: Option) -> Kind { + if non_local.is_some_and(|n| n.budget > 0) { + Kind::NonLocal + } else { + Kind::Video + } +} + +/// Now. +pub fn snapshot() -> Snapshot { + let (pages, page_pools) = super::vram::page_bytes(); + let (arenas, _) = super::vram::arena_bytes(); + let (pools, mapped) = super::sparse::pool_bytes(); + let (_, committed_textures) = super::title_textures::committed_bytes(); + let (local, non_local) = video_memory().map_or((None, None), |(l, n)| (Some(l), Some(n))); + let uma = crate::gpu::uma::get().map_or(0, |u| u.size() as u64); + // (what of it D3D12 has imported, which DXGI counts: none if the import failed) + let imported = super::real::uma_imported_bytes(); + let b = |n: usize| n as u64; + let parts = vec![ + // the title's memory in the coherent window: committed once, the GPU reads it there + Part { owner: "UMA window", kind: Kind::Commit, bytes: uma }, + // a title page in a sparse window is committed on the CPU as well as mapped to a tile + Part { owner: "sparse CPU copy", kind: Kind::Commit, bytes: b(super::sparse::committed_bytes()) }, + // Windows charges the process's D3D12 heaps to its commit as well, local and non-local + // alike (1 GiB heap: +1 GiB of commit, on an RTX 4070 Ti and an RDNA2 iGPU) – all DXGI + // counts but the coherent window, which is CPU memory imported and committed once + Part { owner: "D3D12 heaps", kind: Kind::Commit, bytes: heap_charge(local, non_local, imported) }, + Part { owner: "placed texture pages", kind: Kind::Video, bytes: b(pages) }, + Part { owner: "placed texture pools free", kind: Kind::Video, bytes: b(page_pools.saturating_sub(pages)) }, + Part { owner: "arenas", kind: Kind::Video, bytes: b(arenas) }, + Part { owner: "sparse tiles", kind: Kind::Video, bytes: b(mapped) }, + Part { owner: "sparse pools free", kind: Kind::Video, bytes: b(pools.saturating_sub(mapped)) }, + Part { owner: "sparse pools retired", kind: Kind::Video, bytes: b(super::sparse::retired_bytes()) }, + Part { owner: "committed textures", kind: Kind::Video, bytes: committed_textures }, + // the GPU reads the coherent window (imported, `OpenExistingHeapFromAddress`) as system + // memory: DXGI counts it in the non-local segment of a card (an RTX 4070 Ti and an + // RX 7800 XT), and in the local one of an integrated GPU, whose only segment that is + // (no non-local budget) + Part { owner: "UMA window", kind: uma_segment(non_local), bytes: imported }, + ]; + Snapshot { commit: process_commit(), local, non_local, parts } +} + +#[cfg(test)] +mod tests { + use super::*; + + /// The rest of each total is its total less its parts, a part is counted on its side only, + /// and the line shows every part, each total against its budget, and the rest. + #[test] + fn the_rest_is_the_total_less_the_parts() { + const M: u64 = 1 << 20; + let s = Snapshot { + commit: Some(10_000 * M), + local: Some(Segment { usage: 9_000 * M, budget: 11_000 * M }), + non_local: Some(Segment { usage: 300 * M, budget: 16_000 * M }), + parts: vec![ + Part { owner: "UMA window", kind: Kind::Commit, bytes: 4_096 * M }, + Part { owner: "placed texture pages", kind: Kind::Video, bytes: 5_000 * M }, + Part { owner: "arenas", kind: Kind::Video, bytes: 1_000 * M }, + Part { owner: "upload staging", kind: Kind::NonLocal, bytes: 400 * M }, + ], + }; + assert_eq!(s.counted(Kind::Video), 6_000 * M); + assert_eq!(s.other(Kind::Video), Some((3_000 * M) as i64)); + assert_eq!(s.other(Kind::Commit), Some((5_904 * M) as i64)); + // more counted than DXGI reports: a negative rest, not a wrap + assert_eq!(s.other(Kind::NonLocal), Some(-((100 * M) as i64))); + assert_eq!( + s.line(), + "memory by owner (MB): commit 10000 (UMA window 4096, other 5904); video memory 9000 of a 11000 budget (placed texture pages 5000, arenas 1000, other 3000); non-local 300 of a 16000 budget (upload staging 400, other -100)" + ); + // the coherent window: non-local on a card, local on a GPU of one segment + assert_eq!(uma_segment(Some(Segment { usage: 0, budget: 16_000 * M })), Kind::NonLocal); + assert_eq!(uma_segment(Some(Segment { usage: 0, budget: 0 })), Kind::Video); + assert_eq!(uma_segment(None), Kind::Video); + // the heaps' charge to the commit: both segments less the coherent window + let (l, n) = (Some(Segment { usage: 9_000 * M, budget: 1 }), Some(Segment { usage: 4_500 * M, budget: 1 })); + assert_eq!(heap_charge(l, n, 4_096 * M), 9_404 * M); + assert_eq!(heap_charge(l, None, 4_096 * M), 0); + assert_eq!(heap_charge(Some(Segment { usage: 100 * M, budget: 1 }), n, 8_000 * M), 0, "never below 0"); + // a window whose import failed is not in DXGI's usage: nothing of it is taken off + assert_eq!(heap_charge(Some(Segment { usage: 3_000 * M, budget: 1 }), Some(Segment { usage: 0, budget: 1 }), 0), 3_000 * M); + let unknown = Snapshot { commit: None, local: None, non_local: None, ..s }; + assert_eq!(unknown.other(Kind::Video), None); + // a commit that reads 0 (Wine) is unknown, not 0 with a negative rest + assert_eq!(unknown.other(Kind::Commit), None); + assert!(unknown.line().starts_with("memory by owner (MB): commit unknown (UMA window 4096)"), "{}", unknown.line()); + assert!(unknown.line().contains("video memory unknown (placed texture pages 5000, arenas 1000)"), "{}", unknown.line()); + } + + /// On a device the snapshot has DXGI's totals with a budget, the process's commit, and every + /// owner. + #[test] + fn a_snapshot_on_the_device_has_its_budget_and_owners() { + if super::super::backend() != super::super::Backend::Real { + crate::gpu::recomp::runner::skip_test("no GPU"); + return; + } + let s = snapshot(); + // (an integrated GPU has one segment: its non-local budget is 0) + assert!(s.non_local.is_some(), "{}", s.line()); + assert!(s.local.expect("local video memory").budget > 0, "{}", s.line()); + // (`PrivateUsage` reads 0 under Wine: the commit is unknown there, and the line says so) + match s.commit { + Some(c) => assert!(c > 0), + None => assert!(s.line().contains("commit unknown"), "{}", s.line()), + } + for owner in ["UMA window", "sparse CPU copy", "D3D12 heaps", "placed texture pages", "placed texture pools free", "arenas", "sparse tiles", "sparse pools free", "sparse pools retired", "committed textures"] { + assert!(s.parts.iter().any(|p| p.owner == owner), "{owner} missing: {}", s.line()); + } + } +} diff --git a/crates/xcl/src/hle/d3d12x/mod.rs b/crates/xcl/src/hle/d3d12x/mod.rs index 8886624632..080c83ec75 100644 --- a/crates/xcl/src/hle/d3d12x/mod.rs +++ b/crates/xcl/src/hle/d3d12x/mod.rs @@ -23,6 +23,7 @@ pub mod guard; mod late_release; pub mod layout; pub mod list_x; +pub mod memory_report; pub mod null; pub mod present; pub mod pso_async; diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index 76f31d4daf..f05cf9928e 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -172,6 +172,13 @@ pub fn abi_root_signature() -> Option { static UMA_BUFFER: Mutex> = Mutex::new(Vec::new()); /// the committed UMA windows opened as D3D12 heaps (indexed by window `k`; `None` for sparse windows) static UMA_HEAPS: Mutex>> = Mutex::new(Vec::new()); + +/// Bytes of the coherent windows imported into D3D12 (`OpenExistingHeapFromAddress`): those DXGI +/// counts. 0 before, or when the import failed (the windows are then CPU memory only). +pub fn uma_imported_bytes() -> u64 { + let Some(u) = crate::gpu::uma::get() else { return 0 }; + UMA_HEAPS.lock().unwrap().iter().enumerate().filter(|(_, h)| h.is_some()).map(|(k, _)| u.window(k).1 as u64).sum() +} /// console buffers placed in unified memory: resource → (address, bytes) /// address, bytes, whether XCL allocated the memory (not: the title's own, CreatePlacedResourceX) type UmaBuffer = (usize, usize, bool); diff --git a/crates/xcl/src/hle/d3d12x/sparse.rs b/crates/xcl/src/hle/d3d12x/sparse.rs index b7560f861e..dc5930258e 100644 --- a/crates/xcl/src/hle/d3d12x/sparse.rs +++ b/crates/xcl/src/hle/d3d12x/sparse.rs @@ -422,6 +422,18 @@ pub fn pool_bytes() -> (usize, usize) { let pools = s.pools.iter().filter(|p| p.is_some()).count() * POOL_TILES as usize * TILE; (pools, pools.saturating_sub(s.free.len() * TILE)) } + +/// Bytes of the pool heaps given back but not released yet (titles' queues may still read +/// them): memory accounting, #2688. +pub fn retired_bytes() -> usize { + lock_state().as_ref().map_or(0, |s| s.retired.len() * POOL_TILES as usize * TILE) +} + +/// Bytes of the title's pages committed in the sparse windows (their CPU side): memory +/// accounting, #2688. +pub fn committed_bytes() -> usize { + lock_state().as_ref().map_or(0, |s| s.committed_bytes()) +} /// commits before the device existed: the pages committed, as disjoint page ranges (start → /// end) merged with their neighbours, so a commit or decommit costs O(log n) and not a scan of /// every page committed so far. Like `committed` / `decommitted` with a state, a range counts @@ -654,6 +666,13 @@ impl State { h } + /// Bytes of the title's pages committed in the windows: the pages of the mapped tiles that + /// are not holes. A tile the title decommitted whole is not mapped any more, though its pool + /// tile may still be parked (memory accounting, #2688). + fn committed_bytes(&self) -> usize { + self.mapped.values().map(|e| PAGES_PER_TILE - e.holes.count_ones() as usize).sum::() * PAGE + } + /// Pools whose every tile is free again go (64 MB of video memory each – and as much of the /// system's commit: a retail game's homes of physical pages left 0.75 GB of pools for /// 0.29 GB of tiles in use). Call after their tiles are unmapped and signalled. @@ -2177,10 +2196,12 @@ mod tests { let tile = s.mapped[&base].pool; let v = s.value; assert!(v > 0, "a new tile is mapped"); + assert_eq!(s.committed_bytes(), TILE, "its pages are committed"); // decommitted: parked, still mapped, nothing sent to the GPU s.decommit_range(base, TILE); assert!(!s.mapped.contains_key(&base)); + assert_eq!(s.committed_bytes(), 0, "a parked tile's pages are not committed any more"); assert_eq!(s.parked.get(&base), Some(&tile)); assert!(!s.free.contains(&tile)); assert_eq!(s.value, v, "no tile mapping changed"); diff --git a/crates/xcl/src/hle/d3d12x/title_textures.rs b/crates/xcl/src/hle/d3d12x/title_textures.rs index 6d97bc6e4e..8843906448 100644 --- a/crates/xcl/src/hle/d3d12x/title_textures.rs +++ b/crates/xcl/src/hle/d3d12x/title_textures.rs @@ -322,6 +322,14 @@ pub fn counts() -> (usize, u64) { (TEXTURES.lock().unwrap().by_res.len(), UPLOADS.load(Ordering::Relaxed)) } +/// The title textures kept in step that are committed resources of their own (not on shared +/// `vram::Pages` tiles): how many, and their console size in bytes (memory accounting, #2688; +/// the PC resource can take somewhat more). +pub fn committed_bytes() -> (usize, u64) { + let t = TEXTURES.lock().unwrap(); + t.by_res.values().chain(t.shadowed.values()).filter(|e| !e.placed).fold((0, 0), |(n, b), e| (n + 1, b + e.tex.size())) +} + /// Whether a non-depth render target / UAV texture is small enough to be a CPU-filled glyph or UI /// atlas (`<= 1M` pixels, up to `2048` on either axis, e.g. a retail title's `2048x128` R8 glyph cache). fn is_small_cpu_filled_target(w: u64, h: u32, samples: u32, flags: u32) -> bool { diff --git a/crates/xcl/src/host.rs b/crates/xcl/src/host.rs index 444333d891..34f8c17989 100644 --- a/crates/xcl/src/host.rs +++ b/crates/xcl/src/host.rs @@ -84,6 +84,8 @@ pub fn host_main(boot_path: &Path) -> i32 { mb(pools as u64), mb(pools_mapped as u64) ); + // the same against DXGI's budget, by owner, with what XCL does not count (#2688) + crate::xlog_info!("{}", crate::hle::d3d12x::memory_report::snapshot().line()); last = now; } });