GPU: large heap allocations name their caller; list upload memory in the memory line (diagnostics)

The counting allocator logs the stack of the first allocation of each
power-of-two size class from 32 MiB (who holds our 1.8 GB of large
blocks, run textures6), and the video-memory line counts the live list
upload blocks (list 0x4a0) and their bytes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
MarvisandClaude Opus 5.5 committed 2026-11-25 06:22:45 +01:00
1 parent 29221419b5
commit ac7ffc0ae1
3 files changed
+48 -1

No files matched your search

+36
View File
@@ -345,6 +345,33 @@ pub struct CountingAlloc;
/// Bytes [`CountingAlloc`] has handed out and not taken back.
pub(crate) static HEAP_BYTES: AtomicU64 = AtomicU64::new(0);
/// Allocations of at least this many bytes name their caller, once per
/// power-of-two size class (who holds the big blocks of our heap).
const LARGE: usize = 32 << 20;
/// Logs the stack of a large allocation of `size` bytes, the first of its
/// size class.
fn note_large(size: usize) {
static SEEN: std::sync::Mutex<u64> = std::sync::Mutex::new(0);
std::thread_local!(static INSIDE: std::cell::Cell<bool> = const { std::cell::Cell::new(false) });
let class = 1u64 << size.ilog2().min(63);
if INSIDE.with(|i| i.replace(true)) {
return;
}
let first = {
let mut seen = SEEN.lock().unwrap_or_else(|e| e.into_inner());
let first = *seen & class == 0;
*seen |= class;
first
};
if first {
let trace = std::backtrace::Backtrace::force_capture().to_string();
let frames: Vec<&str> = trace.lines().filter(|l| l.contains("victory")).take(24).collect();
tracing::info!(mb = size >> 20, "a large heap allocation (first of its size class): {}", frames.join(" | "));
}
INSIDE.with(|i| i.set(false));
}
// SAFETY: every call goes to the system allocator unchanged; only counted.
unsafe impl std::alloc::GlobalAlloc for CountingAlloc {
unsafe fn alloc(&self, layout: std::alloc::Layout) -> *mut u8 {
@@ -352,6 +379,9 @@ unsafe impl std::alloc::GlobalAlloc for CountingAlloc {
let p = unsafe { std::alloc::System.alloc(layout) };
if !p.is_null() {
HEAP_BYTES.fetch_add(layout.size() as u64, Relaxed);
if layout.size() >= LARGE {
note_large(layout.size());
}
}
p
}
@@ -361,6 +391,9 @@ unsafe impl std::alloc::GlobalAlloc for CountingAlloc {
let p = unsafe { std::alloc::System.alloc_zeroed(layout) };
if !p.is_null() {
HEAP_BYTES.fetch_add(layout.size() as u64, Relaxed);
if layout.size() >= LARGE {
note_large(layout.size());
}
}
p
}
@@ -377,6 +410,9 @@ unsafe impl std::alloc::GlobalAlloc for CountingAlloc {
if !q.is_null() {
HEAP_BYTES.fetch_sub(layout.size() as u64, Relaxed);
HEAP_BYTES.fetch_add(size as u64, Relaxed);
if size >= LARGE {
note_large(size);
}
}
q
}
+3 -1
View File
@@ -2526,13 +2526,15 @@ impl Meter {
tracing::info!(
"video memory: {local:.0} MB of a {budget:.0} MB budget on the card, \
{system:.0} MB of {system_budget:.0} MB in system memory; process commit {:.0} MB, \
working set {:.0} MB; process heaps {:.0} MB (Rust {:.0} MB); texture staging {:.0} MB; placed textures made {:.0} MB",
working set {:.0} MB; process heaps {:.0} MB (Rust {:.0} MB); texture staging {:.0} MB; placed textures made {:.0} MB; list upload memory {} blocks {:.0} MB",
commit as f64 / 1e6,
working_set as f64 / 1e6,
crate::copystat::heaps_committed() as f64 / 1e6,
crate::copystat::HEAP_BYTES.load(Ordering::Relaxed) as f64 / 1e6,
crate::texture::STAGING_BYTES.load(Ordering::Relaxed) as f64 / 1e6,
crate::resource::PLACED_BYTES.load(Ordering::Relaxed) as f64 / 1e6,
crate::list::UPLOADS[0].load(Ordering::Relaxed),
crate::list::UPLOADS[1].load(Ordering::Relaxed) as f64 / 1e6,
);
}
let [gpu, title] = crate::resource::texture_census().map(|r| r.map(|b| b as f64 / 1e6));
+9
View File
@@ -1084,6 +1084,8 @@ impl Upload {
return None;
}
crate::shadow::register(at, at as u64, len as u64);
UPLOADS[0].fetch_add(1, std::sync::atomic::Ordering::Relaxed);
UPLOADS[1].fetch_add(len as u64, std::sync::atomic::Ordering::Relaxed);
Some(Upload { at, len })
}
@@ -1107,6 +1109,8 @@ impl Drop for Upload {
fn drop(&mut self) {
use windows_sys::Win32::System::Memory::{MEM_RELEASE, VirtualFree};
crate::shadow::unregister(self.at, self.at as u64);
UPLOADS[0].fetch_sub(1, std::sync::atomic::Ordering::Relaxed);
UPLOADS[1].fetch_sub(self.len as u64, std::sync::atomic::Ordering::Relaxed);
// Not guarded any more: memory allocated here next is new.
crate::watch::title_memory_remapped(self.at, self.len);
// SAFETY: our allocation; lists that still use its copy hold that.
@@ -1118,6 +1122,11 @@ impl Drop for Upload {
#[cfg(windows)]
unsafe impl Send for Upload {}
/// Live list upload blocks and their bytes (the statistics).
#[cfg(windows)]
pub(crate) static UPLOADS: [std::sync::atomic::AtomicU64; 2] =
[std::sync::atomic::AtomicU64::new(0), std::sync::atomic::AtomicU64::new(0)];
/// Draw state the host has no use for (root signatures: ours come with
/// the pipeline; depth bounds, depth order and shading rate hints): any
/// arguments, nothing returned.