sinf 0.1.0

Library for obtaining in-depth system information at both the hardware and software levels
Documentation
use std::collections::HashMap;
use std::ffi::OsStr;

use super::System;
use crate::{CGroupLimits, Cpu, Pid, Process};

impl System {
    /// Returns the process list.
    pub fn processes(&self) -> &HashMap<Pid, Process> {
        self.inner.processes()
    }

    /// Returns the process with the given `pid`, or `None` if no such process exists.
    pub fn process(&self, pid: Pid) -> Option<&Process> {
        self.inner.process(pid)
    }

    /// Returns an iterator over the processes whose name contains `name`.
    pub fn processes_by_name<'a: 'b, 'b>(
        &'a self,
        name: &'b OsStr,
    ) -> impl Iterator<Item = &'a Process> + 'b {
        let needle = name.as_encoded_bytes();
        self.processes()
            .values()
            .filter(move |process| contains_bytes(process.name().as_encoded_bytes(), needle))
    }

    /// Returns an iterator over the processes whose name matches `name` exactly.
    pub fn processes_by_exact_name<'a: 'b, 'b>(
        &'a self,
        name: &'b OsStr,
    ) -> impl Iterator<Item = &'a Process> + 'b {
        self.processes()
            .values()
            .filter(move |val: &&Process| val.name() == name)
    }

    /// Returns global CPU usage, the sum of all CPUs.
    pub fn global_cpu_usage(&self) -> f32 {
        self.inner.global_cpu_usage()
    }

    /// Returns the CPU list.
    pub fn cpus(&self) -> &[Cpu] {
        self.inner.cpus()
    }

    /// Returns the RAM size in bytes.
    pub fn total_memory(&self) -> u64 {
        self.inner.total_memory()
    }

    /// Returns the amount of free RAM in bytes.
    pub fn free_memory(&self) -> u64 {
        self.inner.free_memory()
    }

    /// Returns the amount of available RAM in bytes.
    pub fn available_memory(&self) -> u64 {
        self.inner.available_memory()
    }

    /// Returns the amount of used RAM in bytes.
    pub fn used_memory(&self) -> u64 {
        self.inner.used_memory()
    }

    /// Returns the SWAP size in bytes.
    pub fn total_swap(&self) -> u64 {
        self.inner.total_swap()
    }

    /// Returns the amount of free SWAP in bytes.
    pub fn free_swap(&self) -> u64 {
        self.inner.free_swap()
    }

    /// Returns the amount of used SWAP in bytes.
    pub fn used_swap(&self) -> u64 {
        self.inner.used_swap()
    }

    /// Returns the limits of the root cgroup, or `None` when none exist.
    pub fn cgroup_limits(&self) -> Option<CGroupLimits> {
        self.inner.cgroup_limits()
    }
}

fn contains_bytes(haystack: &[u8], needle: &[u8]) -> bool {
    let Some((&first, rest)) = needle.split_first() else {
        return true;
    };

    haystack
        .windows(needle.len())
        .any(|window| window[0] == first && &window[1..] == rest)
}

#[cfg(test)]
mod tests {
    use super::contains_bytes;

    #[test]
    fn process_name_search_matches_byte_substrings() {
        assert!(contains_bytes(b"worker", b""));
        assert!(contains_bytes(b"worker", b"ork"));
        assert!(!contains_bytes(b"worker", b"missing"));
        assert!(contains_bytes(b"worker\xff", b"r\xff"));
        assert!(!contains_bytes(b"worker\xff", b"r\xfe"));
    }
}