use std::{
fs::File,
io::{self, BufRead, BufReader},
num::ParseIntError,
str::FromStr,
};
use crate::collection::disks::IoCounters;
const DISK_SECTOR_SIZE: u64 = 512;
impl FromStr for IoCounters {
type Err = anyhow::Error;
fn from_str(s: &str) -> anyhow::Result<IoCounters> {
fn next_part<'a>(iter: &mut impl Iterator<Item = &'a str>) -> Result<&'a str, io::Error> {
iter.next()
.ok_or_else(|| io::Error::from(io::ErrorKind::InvalidData))
}
fn next_part_to_u64<'a>(iter: &mut impl Iterator<Item = &'a str>) -> anyhow::Result<u64> {
next_part(iter)?
.parse()
.map_err(|err: ParseIntError| err.into())
}
let mut parts = s.split_whitespace().skip(2);
let name = next_part(&mut parts)?.to_string();
let mut parts = parts.skip(2);
let read_bytes = next_part_to_u64(&mut parts)? * DISK_SECTOR_SIZE;
let mut parts = parts.skip(3);
let write_bytes = next_part_to_u64(&mut parts)? * DISK_SECTOR_SIZE;
Ok(IoCounters::new(name, read_bytes, write_bytes))
}
}
pub fn io_stats() -> anyhow::Result<Vec<IoCounters>> {
const PROC_DISKSTATS: &str = "/proc/diskstats";
let mut results = vec![];
let mut reader = BufReader::new(File::open(PROC_DISKSTATS)?);
let mut line = String::new();
while let Ok(bytes) = reader.read_line(&mut line) {
if bytes > 0 {
if let Ok(counters) = IoCounters::from_str(&line) {
results.push(counters);
}
line.clear();
} else {
break;
}
}
#[cfg(feature = "zfs")]
{
use crate::collection::disks::zfs_io_counters;
if let Ok(mut zfs_io) = zfs_io_counters::zfs_io_stats() {
results.append(&mut zfs_io);
}
}
Ok(results)
}