sinf 0.1.0

Library for obtaining in-depth system information at both the hardware and software levels
Documentation
use super::*;

pub(super) fn old_get_memory(entry: &mut ProcessInner, parts: &Parts, info: &SystemInfo) {
    // RSS.
    entry.memory = parts.resident_set_size.saturating_mul(info.page_size_b);
    // VSZ corresponds to the virtual memory size in bytes.
    entry.virtual_memory = parts.virtual_size
}

pub(super) fn slice_to_nb(s: &[u8]) -> u64 {
    let mut nb: u64 = 0;

    for c in s {
        nb = nb * 10 + (c - b'0') as u64;
    }
    nb
}

pub(super) fn get_memory(
    proc_dir: &mut ProcDir<'_>,
    entry: &mut ProcessInner,
    info: &SystemInfo,
    buf: &mut Vec<u8>,
) -> bool {
    if !proc_dir.read_into(c"statm", buf) {
        return false;
    }
    let mut parts = buf.split(|c| *c == b' ');
    entry.virtual_memory = parts
        .next()
        .map(slice_to_nb)
        .unwrap_or(0)
        .saturating_mul(info.page_size_b);
    entry.memory = parts
        .next()
        .map(slice_to_nb)
        .unwrap_or(0)
        .saturating_mul(info.page_size_b);
    true
}

#[allow(clippy::too_many_arguments)]
pub(super) fn update_time_and_memory(
    proc_dir: &mut ProcDir<'_>,
    entry: &mut ProcessInner,
    parts: &Parts,
    uptime: u64,
    info: &SystemInfo,
    refresh_kind: ProcessRefreshKind,
    buf: &mut Vec<u8>,
) {
    {
        #[allow(clippy::collapsible_if)]
        if refresh_kind.memory() {
            // The extra nesting is kept for readability.
            if !get_memory(proc_dir, entry, info, buf) {
                old_get_memory(entry, parts, info);
            }
        }
        set_time(entry, parts.user_time, parts.system_time);
        entry.run_time = uptime.saturating_sub(entry.start_time_without_boot_time);
    }
}