memopro 0.1.4

Budgeted memory runtime: keep, losslessly compress, re-read from verified source files or recompute large buffers so work runs within a guaranteed memory ceiling (alpha).
Documentation
//! Memory this process occupies, for budgets over the whole process (0189, 0229).

/// Bytes of memory this process occupies: the physical footprint on macOS (what Activity Monitor
/// shows, including compressed memory and Apple GPU memory), the resident set on Linux, the
/// working set on Windows (0242). `None` where it cannot be measured.
pub fn process_footprint() -> Option<u64> {
    imp()
}

/// The most [`process_footprint`] has been since the process started, as the OS keeps it
/// (macOS lifetime maximum physical footprint, Linux `VmHWM`, Windows peak working set); a
/// sampler can miss short peaks (0265). Linux updates `VmHWM` lazily from batched counters, so
/// right after a large free it can read a little below the true peak.
pub fn process_peak_footprint() -> Option<u64> {
    peak()
}

#[cfg(target_os = "macos")]
fn peak() -> Option<u64> {
    // SAFETY: rusage_info_v4 is plain data; proc_pid_rusage fills it for our own pid.
    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> {
    // SAFETY: rusage_info_v2 is plain data; proc_pid_rusage fills it for our own pid.
    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()
    };
    // SAFETY: the counters are plain data of the size given in `cb`; the handle is our own
    // process (a pseudo-handle that needs no closing).
    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}");
        // while the memory is held (Linux folds batched RSS counts into VmHWM lazily, so a peak
        // read right after freeing can lag; CI, 0265)
        assert!(super::process_peak_footprint().unwrap() >= after);
        drop(v);
    }
}