Skip to main content

koth_core/
mem.rs

1/// Read current process RSS (resident set size) from /proc/self/status.
2/// Returns MB on Linux, 0.0 on other platforms.
3pub fn rss_mb() -> f64 {
4    #[cfg(target_os = "linux")]
5    {
6        if let Ok(s) = std::fs::read_to_string("/proc/self/status") {
7            for line in s.lines() {
8                if let Some(rest) = line.strip_prefix("VmRSS:") {
9                    if let Some(kb_str) = rest.split_whitespace().next() {
10                        if let Ok(kb) = kb_str.parse::<u64>() {
11                            return kb as f64 / 1024.0;
12                        }
13                    }
14                }
15            }
16        }
17        0.0
18    }
19    #[cfg(not(target_os = "linux"))]
20    {
21        0.0
22    }
23}
24
25/// Log RSS at a named checkpoint.
26pub fn log_mem(label: &str) {
27    let mb = rss_mb();
28    if mb > 0.0 {
29        log::info!("[mem] {}: {:.0} MB RSS", label, mb);
30    }
31}