Skip to main content

resource_sampler/
disk_io.rs

1/// Return (read_bytes, write_bytes) from `/proc/<pid>/io`.
2///
3/// This mirrors the behaviour previously implemented in
4/// `peek-core::proc::resources::read_io`.
5pub fn read_io(pid: i32) -> anyhow::Result<(u64, u64)> {
6    let raw = std::fs::read_to_string(format!("/proc/{}/io", pid))?;
7    let mut read_bytes = 0u64;
8    let mut write_bytes = 0u64;
9    for line in raw.lines() {
10        if let Some(val) = line.strip_prefix("read_bytes: ") {
11            read_bytes = val.trim().parse().unwrap_or(0);
12        } else if let Some(val) = line.strip_prefix("write_bytes: ") {
13            write_bytes = val.trim().parse().unwrap_or(0);
14        }
15    }
16    Ok((read_bytes, write_bytes))
17}