Skip to content
Closed
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
5 changes: 5 additions & 0 deletions crates/xcl/src/gpu/uma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -987,6 +987,11 @@ impl Arena {

static GLOBAL: OnceLock<Option<Uma>> = 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`.
Expand Down
47 changes: 45 additions & 2 deletions crates/xcl/src/hle/d3d12x/memory_report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,16 @@ pub fn local_segment() -> Option<Segment> {
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<Segment>, non_local: Option<Segment>, 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;
Expand All @@ -114,11 +124,12 @@ 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 },
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) },
Expand All @@ -134,6 +145,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;
Expand Down Expand Up @@ -165,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 {
Expand Down Expand Up @@ -195,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());
}
}
Expand Down
11 changes: 11 additions & 0 deletions crates/xcl/src/hle/d3d12x/real.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,17 @@ static UMA_BUFFER: Mutex<Vec<(usize, u64)>> = Mutex::new(Vec::new());
/// `XCL_UMA_DRIVER=mixed:...`).
static UMA_HEAPS: Mutex<Vec<Vec<(usize, usize, usize)>>> = 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)?;
Expand Down
Loading