autocore-std 3.3.21

Standard library for AutoCore control programs - shared memory, IPC, and logging utilities
Documentation
//! Lightweight process diagnostics via `/proc/self`.
//!
//! These helpers are designed for periodic logging inside real-time loops.
//! They perform a single read per call and are cheap on Linux.

/// Count open file descriptors by reading `/proc/self/fd`.
pub fn count_open_fds() -> usize {
    std::fs::read_dir("/proc/self/fd").map(|d| d.count()).unwrap_or(0)
}

/// Return resident set size in KiB by reading `/proc/self/statm`.
///
/// The second field of `statm` is RSS in pages. We multiply by the page
/// size (almost always 4 KiB on Linux).
pub fn get_rss_kb() -> u64 {
    let page_kb: u64 = 4; // 4096 / 1024
    std::fs::read_to_string("/proc/self/statm")
        .ok()
        .and_then(|s| {
            s.split_whitespace()
                .nth(1) // RSS field (in pages)
                .and_then(|v| v.parse::<u64>().ok())
        })
        .map(|pages| pages * page_kb)
        .unwrap_or(0)
}