pub fn process_footprint() -> Option<u64> {
imp()
}
pub fn process_peak_footprint() -> Option<u64> {
peak()
}
#[cfg(target_os = "macos")]
fn peak() -> Option<u64> {
unsafe {
let mut info: libc::rusage_info_v4 = std::mem::zeroed();
let r = libc::proc_pid_rusage(
libc::getpid(),
libc::RUSAGE_INFO_V4,
(&mut info as *mut libc::rusage_info_v4).cast(),
);
(r == 0).then_some(info.ri_lifetime_max_phys_footprint)
}
}
#[cfg(target_os = "linux")]
fn peak() -> Option<u64> {
let s = std::fs::read_to_string("/proc/self/status").ok()?;
let line = s.lines().find(|l| l.starts_with("VmHWM:"))?;
let kib: u64 = line.split_whitespace().nth(1)?.parse().ok()?;
Some(kib * 1024)
}
#[cfg(windows)]
fn peak() -> Option<u64> {
counters().map(|c| c.PeakWorkingSetSize as u64)
}
#[cfg(not(any(target_os = "macos", target_os = "linux", windows)))]
fn peak() -> Option<u64> {
None
}
#[cfg(target_os = "macos")]
fn imp() -> Option<u64> {
unsafe {
let mut info: libc::rusage_info_v2 = std::mem::zeroed();
let r = libc::proc_pid_rusage(
libc::getpid(),
libc::RUSAGE_INFO_V2,
(&mut info as *mut libc::rusage_info_v2).cast(),
);
(r == 0).then_some(info.ri_phys_footprint)
}
}
#[cfg(target_os = "linux")]
fn imp() -> Option<u64> {
let s = std::fs::read_to_string("/proc/self/statm").ok()?;
let pages: u64 = s.split_whitespace().nth(1)?.parse().ok()?;
Some(pages * super::page_size() as u64)
}
#[cfg(windows)]
fn imp() -> Option<u64> {
counters().map(|c| c.WorkingSetSize as u64)
}
#[cfg(windows)]
fn counters() -> Option<windows_sys::Win32::System::ProcessStatus::PROCESS_MEMORY_COUNTERS> {
use windows_sys::Win32::System::ProcessStatus::{
K32GetProcessMemoryInfo, PROCESS_MEMORY_COUNTERS,
};
use windows_sys::Win32::System::Threading::GetCurrentProcess;
let mut c = PROCESS_MEMORY_COUNTERS {
cb: std::mem::size_of::<PROCESS_MEMORY_COUNTERS>() as u32,
..Default::default()
};
let ok = unsafe { K32GetProcessMemoryInfo(GetCurrentProcess(), &mut c, c.cb) };
(ok != 0).then_some(c)
}
#[cfg(not(any(target_os = "macos", target_os = "linux", windows)))]
fn imp() -> Option<u64> {
None
}
#[cfg(test)]
mod tests {
#[test]
#[cfg(any(target_os = "macos", target_os = "linux", windows))]
#[ignore = "measures the whole process: run with --ignored --test-threads=1"]
fn footprint_grows_with_touched_memory() {
let before = super::process_footprint().unwrap();
let v = vec![1u8; 64 << 20];
let after = super::process_footprint().unwrap();
assert!(after >= before + (48 << 20), "{before} -> {after}");
assert!(super::process_peak_footprint().unwrap() >= after);
drop(v);
}
}