use std::path::Path;
use crate::error::{Error, Result};
use crate::util::parse;
#[derive(Debug)]
pub struct LoadAvg {
pub one: f64,
pub five: f64,
pub fifteen: f64,
pub runnable: u32,
pub total: u32,
}
pub fn loadavg() -> Result<LoadAvg> {
let path = Path::new("/proc/loadavg");
let bytes = parse::read_file(path)?;
let fields = parse::SplitFields::<5>::new(&bytes);
if fields.len() < 4 {
return Err(Error::Parse {
path: path.to_path_buf(),
line: 1,
msg: "expected at least 4 fields",
});
}
let one = parse::parse_dec_f64(fields[0])?;
let five = parse::parse_dec_f64(fields[1])?;
let fifteen = parse::parse_dec_f64(fields[2])?;
let sched = fields[3];
let slash_idx = parse::memchr(b'/', sched).ok_or_else(|| Error::Parse {
path: path.to_path_buf(),
line: 1,
msg: "missing slash in sched field",
})?;
let runnable = parse::parse_dec_u32(&sched[..slash_idx])?;
let total = parse::parse_dec_u32(&sched[slash_idx + 1..])?;
Ok(LoadAvg {
one,
five,
fifteen,
runnable,
total,
})
}