diff --git a/crates/xcl/src/alloc_trace.rs b/crates/xcl/src/alloc_trace.rs new file mode 100644 index 000000000..b79282756 --- /dev/null +++ b/crates/xcl/src/alloc_trace.rs @@ -0,0 +1,142 @@ +//! `XCL_TRACE_ALLOC=1`: who commits big blocks of memory. `ntdll!NtAllocateVirtualMemory` is +//! detoured; every commit of `XCL_TRACE_ALLOC_MIN` bytes or more (default 1 MiB) is counted under +//! its call stack, and the stacks holding the most are logged every 15 s – the title's, XCL's +//! and the driver's allocations alike (they all end in that call). +//! +//! The detour runs inside the heap's own allocations (with the heap lock held): it takes no +//! lock and allocates nothing – the sites are a fixed table filled with atomics. + +use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering}; + +static ORIG: AtomicUsize = AtomicUsize::new(0); +static MIN: AtomicUsize = AtomicUsize::new(1 << 20); + +const FRAMES: usize = 40; +const SLOTS: usize = 4096; + +struct Site { + /// stack hash (0: free) + key: AtomicU64, + /// frames written (by the thread that claimed the slot) + ready: AtomicUsize, + frames: [AtomicUsize; FRAMES], + count: AtomicU64, + bytes: AtomicU64, +} + +#[allow(clippy::declare_interior_mutable_const)] +const EMPTY: Site = Site { + key: AtomicU64::new(0), + ready: AtomicUsize::new(0), + frames: [const { AtomicUsize::new(0) }; FRAMES], + count: AtomicU64::new(0), + bytes: AtomicU64::new(0), +}; +static SITES: [Site; SLOTS] = [EMPTY; SLOTS]; +static LOST: AtomicU64 = AtomicU64::new(0); + +unsafe extern "system" fn hk_alloc(process: isize, base: *mut usize, zero_bits: usize, size: *mut usize, kind: u32, protect: u32) -> i32 { + let orig: unsafe extern "system" fn(isize, *mut usize, usize, *mut usize, u32, u32) -> i32 = std::mem::transmute(ORIG.load(Ordering::Acquire)); + let st = orig(process, base, zero_bits, size, kind, protect); + // the current process (-1), committing, succeeded + if st >= 0 && process == -1 && kind & 0x1000 != 0 && !size.is_null() && *size >= MIN.load(Ordering::Relaxed) { + record(*size as u64); + } + st +} + +unsafe fn record(n: u64) { + let mut frames = [0usize; FRAMES]; + windows_sys::Win32::System::Diagnostics::Debug::RtlCaptureStackBackTrace(2, FRAMES as u32, frames.as_mut_ptr() as *mut _, std::ptr::null_mut()); + let mut h: u64 = 0xcbf2_9ce4_8422_2325; + for f in frames { + h = (h ^ f as u64).wrapping_mul(0x100_0000_01b3); + } + let h = h | 1; + let mut i = h as usize % SLOTS; + for _ in 0..SLOTS { + let s = &SITES[i]; + let k = s.key.load(Ordering::Acquire); + if k == h { + s.count.fetch_add(1, Ordering::Relaxed); + s.bytes.fetch_add(n, Ordering::Relaxed); + return; + } + if k == 0 && s.key.compare_exchange(0, h, Ordering::AcqRel, Ordering::Acquire).is_ok() { + for (d, f) in s.frames.iter().zip(frames) { + d.store(f, Ordering::Relaxed); + } + s.ready.store(1, Ordering::Release); + s.count.fetch_add(1, Ordering::Relaxed); + s.bytes.fetch_add(n, Ordering::Relaxed); + return; + } + if s.key.load(Ordering::Acquire) == h { + continue; // claimed for this stack just now: counted on the next look + } + i = (i + 1) % SLOTS; + } + LOST.fetch_add(n, Ordering::Relaxed); +} + +fn report() { + let mut top: Vec<(usize, u64, u64)> = SITES + .iter() + .enumerate() + .filter(|(_, s)| s.ready.load(Ordering::Acquire) != 0) + .map(|(i, s)| (i, s.count.load(Ordering::Relaxed), s.bytes.load(Ordering::Relaxed))) + .collect(); + top.sort_by(|a, b| b.2.cmp(&a.2)); + let total: u64 = top.iter().map(|t| t.2).sum(); + let gb = |b: u64| b as f64 / (1u64 << 30) as f64; + crate::xlog_info!( + "alloc trace: {:.2} GB committed in blocks of {} KB or more, {} call stacks ({:.2} GB not placed)", + gb(total), + MIN.load(Ordering::Relaxed) >> 10, + top.len(), + gb(LOST.load(Ordering::Relaxed)) + ); + // by the first module that is not the heap / CRT / Windows' allocator + let mut by_module: std::collections::HashMap = std::collections::HashMap::new(); + for &(i, _, bytes) in &top { + let owner = SITES[i] + .frames + .iter() + .map(|f| f.load(Ordering::Relaxed)) + .take_while(|&f| f != 0) + .map(crate::modules::describe_address) + .find(|d| !["ntdll.dll", "ucrtbase.dll", "KERNELBASE.dll", "KERNEL32.DLL", "msvcrt.dll", "d3d12.dll"].iter().any(|m| d.starts_with(m))) + .map(|d| d.split('+').next().unwrap_or("?").to_string()) + .unwrap_or_else(|| "?".into()); + *by_module.entry(owner).or_default() += bytes; + } + let mut mods: Vec<_> = by_module.into_iter().collect(); + mods.sort_by(|a, b| b.1.cmp(&a.1)); + crate::xlog_info!("alloc trace: by module: {}", mods.iter().take(8).map(|(m, b)| format!("{m} {:.2} GB", gb(*b))).collect::>().join(", ")); + for &(i, count, bytes) in top.iter().take(8) { + let stack: Vec = SITES[i].frames.iter().map(|f| f.load(Ordering::Relaxed)).take_while(|&f| f != 0).map(crate::modules::describe_address).collect(); + let tail: Vec<&String> = stack.iter().filter(|s| !s.starts_with("amdxc64.dll") && !s.starts_with("ntdll.dll")).collect(); + crate::xlog_info!("alloc trace: {:.2} GB in {count} blocks: {} … callers: {:?}", gb(bytes), stack.iter().take(4).cloned().collect::>().join(" < "), tail); + } + crate::log::flush(); +} + +pub fn install() { + if std::env::var_os("XCL_TRACE_ALLOC").is_none() { + return; + } + if let Some(m) = std::env::var("XCL_TRACE_ALLOC_MIN").ok().and_then(|v| v.parse::().ok()) { + MIN.store(m, Ordering::Relaxed); + } + unsafe { + if let Err(e) = crate::hooks::hook("ntdll.dll", "NtAllocateVirtualMemory", hk_alloc as usize, &ORIG) { + crate::xlog_warn!("alloc trace: {e}"); + return; + } + } + crate::xlog_info!("alloc trace: on (commits of {} bytes or more)", MIN.load(Ordering::Relaxed)); + let _ = std::thread::Builder::new().name("xcl-alloc-trace".into()).spawn(|| loop { + std::thread::sleep(std::time::Duration::from_secs(15)); + report(); + }); +} diff --git a/crates/xcl/src/eh.rs b/crates/xcl/src/eh.rs index 0102a29d7..19961e153 100644 --- a/crates/xcl/src/eh.rs +++ b/crates/xcl/src/eh.rs @@ -102,6 +102,12 @@ unsafe extern "system" fn unhandled_filter(info: *const EXCEPTION_POINTERS) -> i ); crate::xlog_error!("{msg}"); crate::report::write_final(Some(msg)); + // the other threads now: a title crash handler (Breakpad) can keep the process for + // minutes before it ends (GTA VI's watchdog: what was its silent thread doing?) + static DUMPED: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false); + if !DUMPED.swap(true, Ordering::AcqRel) { + dump_threads(); + } crate::log::flush(); } match TITLE_FILTER.load(Ordering::Acquire) { diff --git a/crates/xcl/src/hle/d3d12x/real.rs b/crates/xcl/src/hle/d3d12x/real.rs index 3e7a44f19..56dd868c9 100644 --- a/crates/xcl/src/hle/d3d12x/real.rs +++ b/crates/xcl/src/hle/d3d12x/real.rs @@ -419,10 +419,22 @@ pub fn init() -> Result { } } -/// A debug layer message: errors and warnings into the log (the first 200). +/// A debug layer message into the log: the first 200 warnings, the first 20 errors of each +/// message id – the error before a device removal came after hundreds of others and was dropped. unsafe extern "system" fn debug_message(_category: D3D12_MESSAGE_CATEGORY, severity: D3D12_MESSAGE_SEVERITY, id: D3D12_MESSAGE_ID, text: windows::core::PCSTR, _context: *mut c_void) { static N: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0); - if severity.0 > D3D12_MESSAGE_SEVERITY_WARNING.0 || N.fetch_add(1, Ordering::Relaxed) >= 200 { + static PER_ID: Mutex>> = Mutex::new(None); + if severity.0 > D3D12_MESSAGE_SEVERITY_WARNING.0 { + return; + } + if severity.0 <= D3D12_MESSAGE_SEVERITY_ERROR.0 { + let mut g = PER_ID.lock().unwrap(); + let n = g.get_or_insert_with(HashMap::new).entry(id.0).or_insert(0); + *n += 1; + if *n > 20 { + return; + } + } else if N.fetch_add(1, Ordering::Relaxed) >= 200 { return; } let t = if text.is_null() { String::new() } else { String::from_utf8_lossy(text.as_bytes()).into_owned() }; diff --git a/crates/xcl/src/host.rs b/crates/xcl/src/host.rs index 15d4918ae..ad6db6a32 100644 --- a/crates/xcl/src/host.rs +++ b/crates/xcl/src/host.rs @@ -98,6 +98,7 @@ pub fn host_main(boot_path: &Path) -> i32 { crate::xlog_error!("hook RtlPcToFileHeader: {e} – C++ exceptions in the game may not work"); } crate::eh::install_crash_handler(); + crate::alloc_trace::install(); crate::eh::start_probes(); HOST_MODE.store(true, Ordering::Release); diff --git a/crates/xcl/src/lib.rs b/crates/xcl/src/lib.rs index c0290744d..eb5303922 100644 --- a/crates/xcl/src/lib.rs +++ b/crates/xcl/src/lib.rs @@ -8,6 +8,7 @@ #[macro_use] pub mod log; pub mod manifest; +pub mod alloc_trace; pub mod config; pub mod disasm; pub mod eh;