mothership 0.0.100

Process supervisor with HTTP exposure - wrap, monitor, and expose your fleet
Documentation
use super::{MemoryError, MemorySample};
use std::time::Instant;

pub fn sample_memory(pid: u32) -> Result<MemorySample, MemoryError> {
    let path = format!("/proc/{}/status", pid);
    let content = std::fs::read_to_string(&path).map_err(|e| match e.kind() {
        std::io::ErrorKind::NotFound => MemoryError::ProcessNotFound(pid),
        std::io::ErrorKind::PermissionDenied => MemoryError::PermissionDenied(pid),
        _ => MemoryError::Io(e),
    })?;

    let mut rss_kb = 0u64;
    let mut peak_kb = 0u64;
    let mut virtual_kb = 0u64;
    let mut swap_kb = 0u64;

    for line in content.lines() {
        let parts: Vec<&str> = line.split_whitespace().collect();
        if parts.len() < 2 {
            continue;
        }

        match parts[0] {
            "VmRSS:" => rss_kb = parts[1].parse().unwrap_or(0),
            "VmHWM:" => peak_kb = parts[1].parse().unwrap_or(0),
            "VmSize:" => virtual_kb = parts[1].parse().unwrap_or(0),
            "VmSwap:" => swap_kb = parts[1].parse().unwrap_or(0),
            _ => {}
        }
    }

    Ok(MemorySample {
        timestamp: Instant::now(),
        rss_bytes: rss_kb * 1024,
        peak_rss_bytes: peak_kb * 1024,
        virtual_bytes: virtual_kb * 1024,
        swap_bytes: swap_kb * 1024,
    })
}