use std::collections::HashMap;
use std::path::Path;
use crate::error::{Error, Result};
use crate::util::parse;
pub fn vmstat() -> Result<HashMap<String, u64>> {
let path = Path::new("/proc/vmstat");
let bytes = parse::read_file(path)?;
let mut out: HashMap<String, u64> = HashMap::new();
for (line_num, line) in bytes
.split(|&b| b == b'\n')
.filter(|l| !l.is_empty())
.enumerate()
{
let fields = parse::SplitFields::<2>::new(line);
if fields.len() != 2 {
return Err(Error::Parse {
path: path.to_path_buf(),
line: line_num + 1,
msg: "expected '<key> <value>'",
});
}
let key = String::from(std::str::from_utf8(fields[0]).map_err(|_| Error::Parse {
path: path.to_path_buf(),
line: line_num + 1,
msg: "invalid key",
})?);
let value = parse::parse_dec_u64_fast(fields[1]).map_err(|_| Error::Parse {
path: path.to_path_buf(),
line: line_num + 1,
msg: "invalid value",
})?;
out.insert(key, value);
}
Ok(out)
}