Debian Codegraph @

debian/rust-vendor/procfs/examples/diskstat.rs

raw
1 use procfs::{diskstats, process::Process, DiskStat};
2 use std::collections::HashMap;
3 use std::iter::FromIterator;
4
5 fn main() {
6 let me = Process::myself().unwrap();
7 let mounts = me.mountinfo().unwrap();
8
9 // Get a list of all disks that we have IO stat info on
10 let disk_stats: HashMap<(i32, i32), DiskStat> =
11 HashMap::from_iter(diskstats().unwrap().into_iter().map(|i| ((i.major, i.minor), i)));
12
13 for mount in mounts {
14 // parse the majmin string (something like "0:3") into an (i32, i32) tuple
15 let (maj, min): (i32, i32) = {
16 let mut s = mount.majmin.split(':');
17 (s.next().unwrap().parse().unwrap(), s.next().unwrap().parse().unwrap())
18 };
19
20 if let Some(stat) = disk_stats.get(&(maj, min)) {
21 println!("{} mounted on {}:", stat.name, mount.mount_point.display());
22 println!(" total reads: {} ({} ms)", stat.reads, stat.time_reading);
23 println!(" total writes: {} ({} ms)", stat.writes, stat.time_writing);
24 println!(
25 " total flushes: {} ({} ms)",
26 stat.flushes.unwrap_or(0),
27 stat.time_flushing.unwrap_or(0)
28 );
29 }
30 }
31 }