Debian Codegraph @

src/collection/disks/unix/linux/counters.rs

raw symbols SCIP
1 //! Based on [heim's implementation](https://github.com/heim-rs/heim/blob/master/heim-disk/src/sys/linux/counters.rs).
2
3 use std::{
4 fs::File,
5 io::{self, BufRead, BufReader},
6 num::ParseIntError,
7 str::FromStr,
8 };
9
10 use crate::collection::disks::IoCounters;
11
12 /// Copied from the `psutil` sources:
13 ///
14 /// "man iostat" states that sectors are equivalent with blocks and have
15 /// a size of 512 bytes. Despite this value can be queried at runtime
16 /// via /sys/block/{DISK}/queue/hw_sector_size and results may vary
17 /// between 1k, 2k, or 4k... 512 appears to be a magic constant used
18 /// throughout Linux source code:
19 /// * <https://stackoverflow.com/a/38136179/376587>
20 /// * <https://lists.gt.net/linux/kernel/2241060>
21 /// * <https://github.com/giampaolo/psutil/issues/1305>
22 /// * <https://github.com/torvalds/linux/blob/4f671fe2f9523a1ea206f63fe60a7c7b3a56d5c7/include/linux/bio.h#L99>
23 /// * <https://lkml.org/lkml/2015/8/17/234>
24 const DISK_SECTOR_SIZE: u64 = 512;
25
26 impl FromStr for IoCounters {
27 type Err = anyhow::Error;
28
29 /// Converts a `&str` to an [`IoCounters`].
30 ///
31 /// Follows the format used in Linux 2.6+. Note that this completely ignores
32 /// the following stats:
33 /// - Discard stats from 4.18+
34 /// - Flush stats from 5.5+
35 ///
36 /// <https://www.kernel.org/doc/Documentation/iostats.txt>
37 /// <https://www.kernel.org/doc/Documentation/ABI/testing/procfs-diskstats>
38 fn from_str(s: &str) -> anyhow::Result<IoCounters> {
39 fn next_part<'a>(iter: &mut impl Iterator<Item = &'a str>) -> Result<&'a str, io::Error> {
40 iter.next()
41 .ok_or_else(|| io::Error::from(io::ErrorKind::InvalidData))
42 }
43
44 fn next_part_to_u64<'a>(iter: &mut impl Iterator<Item = &'a str>) -> anyhow::Result<u64> {
45 next_part(iter)?
46 .parse()
47 .map_err(|err: ParseIntError| err.into())
48 }
49
50 // Skip the major and minor numbers.
51 let mut parts = s.split_whitespace().skip(2);
52
53 let name = next_part(&mut parts)?.to_string();
54
55 // Skip read count, read merged count.
56 let mut parts = parts.skip(2);
57 let read_bytes = next_part_to_u64(&mut parts)? * DISK_SECTOR_SIZE;
58
59 // Skip read time seconds, write count, and write merged count.
60 let mut parts = parts.skip(3);
61 let write_bytes = next_part_to_u64(&mut parts)? * DISK_SECTOR_SIZE;
62
63 Ok(IoCounters::new(name, read_bytes, write_bytes))
64 }
65 }
66
67 /// Returns an iterator of disk I/O stats. Pulls data from `/proc/diskstats`.
68 pub fn io_stats() -> anyhow::Result<Vec<IoCounters>> {
69 const PROC_DISKSTATS: &str = "/proc/diskstats";
70
71 let mut results = vec![];
72 let mut reader = BufReader::new(File::open(PROC_DISKSTATS)?);
73 let mut line = String::new();
74
75 // This saves us from doing a string allocation on each iteration compared to
76 // `lines()`.
77 while let Ok(bytes) = reader.read_line(&mut line) {
78 if bytes > 0 {
79 if let Ok(counters) = IoCounters::from_str(&line) {
80 results.push(counters);
81 }
82 line.clear();
83 } else {
84 break;
85 }
86 }
87
88 #[cfg(feature = "zfs")]
89 {
90 use crate::collection::disks::zfs_io_counters;
91 if let Ok(mut zfs_io) = zfs_io_counters::zfs_io_stats() {
92 results.append(&mut zfs_io);
93 }
94 }
95
96 Ok(results)
97 }