mprober_lib/load_average/
mod.rs

1use std::io::ErrorKind;
2
3use crate::scanner_rust::{generic_array::typenum::U24, ScannerAscii, ScannerError};
4
5#[derive(Default, Debug, Clone)]
6pub struct LoadAverage {
7    pub one:     f64,
8    pub five:    f64,
9    pub fifteen: f64,
10    // Not include the numbers of active/total scheduled entities and the last created PID.
11}
12
13/// Get the load average by reading the `/proc/loadavg` file.
14///
15/// ```rust
16/// use mprober_lib::load_average;
17///
18/// let load_average = load_average::get_load_average().unwrap();
19///
20/// println!("{load_average:#?}");
21/// ```
22#[inline]
23pub fn get_load_average() -> Result<LoadAverage, ScannerError> {
24    let mut sc: ScannerAscii<_, U24> = ScannerAscii::scan_path2("/proc/loadavg")?;
25
26    let one = sc.next_f64()?.ok_or(ErrorKind::UnexpectedEof)?;
27    let five = sc.next_f64()?.ok_or(ErrorKind::UnexpectedEof)?;
28    let fifteen = sc.next_f64()?.ok_or(ErrorKind::UnexpectedEof)?;
29
30    Ok(LoadAverage {
31        one,
32        five,
33        fifteen,
34    })
35}