use crate::{expect, from_str, ProcResult};
#[cfg(feature = "serde1")]
use serde::{Deserialize, Serialize};
use std::io::BufRead;
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
pub struct DiskStat {
pub major: i32,
pub minor: i32,
pub name: String,
pub reads: u64,
pub merged: u64,
pub sectors_read: u64,
pub time_reading: u64,
pub writes: u64,
pub writes_merged: u64,
pub sectors_written: u64,
pub time_writing: u64,
pub in_progress: u64,
pub time_in_progress: u64,
pub weighted_time_in_progress: u64,
pub discards: Option<u64>,
pub discards_merged: Option<u64>,
pub sectors_discarded: Option<u64>,
pub time_discarding: Option<u64>,
pub flushes: Option<u64>,
pub time_flushing: Option<u64>,
}
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
pub struct DiskStats(pub Vec<DiskStat>);
impl crate::FromBufRead for DiskStats {
fn from_buf_read<R: BufRead>(r: R) -> ProcResult<Self> {
let mut v = Vec::new();
for line in r.lines() {
let line = line?;
v.push(DiskStat::from_line(&line)?);
}
Ok(DiskStats(v))
}
}
impl DiskStat {
pub fn from_line(line: &str) -> ProcResult<DiskStat> {
let mut s = line.split_whitespace();
let major = from_str!(i32, expect!(s.next()));
let minor = from_str!(i32, expect!(s.next()));
let name = expect!(s.next()).to_string();
let reads = from_str!(u64, expect!(s.next()));
let merged = from_str!(u64, expect!(s.next()));
let sectors_read = from_str!(u64, expect!(s.next()));
let time_reading = from_str!(u64, expect!(s.next()));
let writes = from_str!(u64, expect!(s.next()));
let writes_merged = from_str!(u64, expect!(s.next()));
let sectors_written = from_str!(u64, expect!(s.next()));
let time_writing = from_str!(u64, expect!(s.next()));
let in_progress = from_str!(u64, expect!(s.next()));
let time_in_progress = from_str!(u64, expect!(s.next()));
let weighted_time_in_progress = from_str!(u64, expect!(s.next()));
let discards = s.next().and_then(|s| u64::from_str_radix(s, 10).ok());
let discards_merged = s.next().and_then(|s| u64::from_str_radix(s, 10).ok());
let sectors_discarded = s.next().and_then(|s| u64::from_str_radix(s, 10).ok());
let time_discarding = s.next().and_then(|s| u64::from_str_radix(s, 10).ok());
let flushes = s.next().and_then(|s| u64::from_str_radix(s, 10).ok());
let time_flushing = s.next().and_then(|s| u64::from_str_radix(s, 10).ok());
Ok(DiskStat {
major,
minor,
name,
reads,
merged,
sectors_read,
time_reading,
writes,
writes_merged,
sectors_written,
time_writing,
in_progress,
time_in_progress,
weighted_time_in_progress,
discards,
discards_merged,
sectors_discarded,
time_discarding,
flushes,
time_flushing,
})
}
}