| 1 | //! Based on [heim's implementation](https://github.com/heim-rs/heim/blob/master/heim-disk/src/sys/linux/counters.rs). |
| 2 | |
| 3 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{ |
| 4 | fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#, |
| 5 | iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::{selfrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/, BufReadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/BufRead#, BufReaderrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/buffered/bufreader/BufReader#}, |
| 6 | numrust-analyzer cargo std https://github.com/rust-lang/rust/library/std num/::ParseIntErrorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/error/ParseIntError#, |
| 7 | strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/::FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#, |
| 8 | }; |
| 9 | |
| 10 | use craterust-analyzer cargo bottom 0.14.9 crate/::collectionrust-analyzer cargo bottom 0.14.9 collection/::disksrust-analyzer cargo bottom 0.14.9 collection/disks/::IoCountersrust-analyzer cargo bottom 0.14.9 collection/disks/io_counters/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_SIZErust-analyzer cargo bottom 0.14.9 collection/disks/unix/linux/counters/DISK_SECTOR_SIZE.: u64 = 512; |
| 25 | |
| 26 | impl FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr# for IoCountersrust-analyzer cargo bottom 0.14.9 collection/disks/io_counters/IoCounters# { |
| 27 | type Errrust-analyzer cargo bottom 0.14.9 collection/disks/unix/linux/counters/impl#[IoCounters][FromStr]Err# = anyhowrust-analyzer cargo anyhow 1.0.104 crate/::Errorrust-analyzer cargo anyhow 1.0.104 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_strrust-analyzer cargo bottom 0.14.9 collection/disks/unix/linux/counters/impl#[IoCounters][FromStr]from_str().(s: &str) -> anyhowrust-analyzer cargo anyhow 1.0.104 crate/::Resultrust-analyzer cargo anyhow 1.0.104 Result#<IoCountersrust-analyzer cargo bottom 0.14.9 collection/disks/io_counters/IoCounters#> { |
| 39 | fn next_partrust-analyzer cargo bottom 0.14.9 collection/disks/unix/linux/counters/next_part().<'a>(iter: &mut impl Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#<Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item# = &'a str>) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<&'a str, iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#> { |
| 40 | iter.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#next().() |
| 41 | .ok_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or_else().(|| iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::fromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error][`From<ErrorKind>`]from().(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::InvalidDatarust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#InvalidData#)) |
| 42 | } |
| 43 | |
| 44 | fn next_part_to_u64rust-analyzer cargo bottom 0.14.9 collection/disks/unix/linux/counters/next_part_to_u64().<'a>(iter: &mut impl Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#<Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item# = &'a str>) -> anyhowrust-analyzer cargo anyhow 1.0.104 crate/::Resultrust-analyzer cargo anyhow 1.0.104 Result#<u64> { |
| 45 | next_partrust-analyzer cargo bottom 0.14.9 collection/disks/unix/linux/counters/next_part().(iter)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). |
| 46 | .parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().() |
| 47 | .map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|err: ParseIntErrorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/error/ParseIntError#| err.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()) |
| 48 | } |
| 49 | |
| 50 | // Skip the major and minor numbers. |
| 51 | let mut parts = s.split_whitespacerust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]split_whitespace().().skiprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#skip().(2); |
| 52 | |
| 53 | let name = next_partrust-analyzer cargo bottom 0.14.9 collection/disks/unix/linux/counters/next_part().(&mut parts)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()..to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(); |
| 54 | |
| 55 | // Skip read count, read merged count. |
| 56 | let mut parts = parts.skiprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#skip().(2); |
| 57 | let read_bytes = next_part_to_u64rust-analyzer cargo bottom 0.14.9 collection/disks/unix/linux/counters/next_part_to_u64().(&mut parts)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Mul<Self>`]mul().DISK_SECTOR_SIZErust-analyzer cargo bottom 0.14.9 collection/disks/unix/linux/counters/DISK_SECTOR_SIZE.; |
| 58 | |
| 59 | // Skip read time seconds, write count, and write merged count. |
| 60 | let mut parts = parts.skiprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#skip().(3); |
| 61 | let write_bytes = next_part_to_u64rust-analyzer cargo bottom 0.14.9 collection/disks/unix/linux/counters/next_part_to_u64().(&mut parts)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Mul<Self>`]mul().DISK_SECTOR_SIZErust-analyzer cargo bottom 0.14.9 collection/disks/unix/linux/counters/DISK_SECTOR_SIZE.; |
| 62 | |
| 63 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(IoCountersrust-analyzer cargo bottom 0.14.9 collection/disks/io_counters/IoCounters#::newrust-analyzer cargo bottom 0.14.9 collection/disks/io_counters/impl#[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_statsrust-analyzer cargo bottom 0.14.9 collection/disks/unix/linux/counters/io_stats().() -> anyhowrust-analyzer cargo anyhow 1.0.104 crate/::Resultrust-analyzer cargo anyhow 1.0.104 Result#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<IoCountersrust-analyzer cargo bottom 0.14.9 collection/disks/io_counters/IoCounters#>> { |
| 69 | const PROC_DISKSTATSrust-analyzer cargo bottom 0.14.9 collection/disks/unix/linux/counters/PROC_DISKSTATS.: &str = "/proc/diskstats"; |
| 70 | |
| 71 | let mut results = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![]; |
| 72 | let mut reader = BufReaderrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/buffered/bufreader/BufReader#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/buffered/bufreader/impl#[`BufReader<R>`]new().(Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#::openrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]open().(PROC_DISKSTATSrust-analyzer cargo bottom 0.14.9 collection/disks/unix/linux/counters/PROC_DISKSTATS.)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().); |
| 73 | let mut line = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]new().(); |
| 74 | |
| 75 | // This saves us from doing a string allocation on each iteration compared to |
| 76 | // `lines()`. |
| 77 | while let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(bytes) = reader.read_linerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/BufRead#read_line().(&mut line) { |
| 78 | if bytes rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().0 { |
| 79 | if let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(counters) = IoCountersrust-analyzer cargo bottom 0.14.9 collection/disks/io_counters/IoCounters#::from_strrust-analyzer cargo bottom 0.14.9 collection/disks/unix/linux/counters/impl#[IoCounters][FromStr]from_str().(&line) { |
| 80 | results.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(counters); |
| 81 | } |
| 82 | line.clearrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]clear().(); |
| 83 | } else { |
| 84 | break; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | #[cfg(feature = "zfs")] |
| 89 | { |
| 90 | use craterust-analyzer cargo bottom 0.14.9 crate/::collectionrust-analyzer cargo bottom 0.14.9 collection/::disksrust-analyzer cargo bottom 0.14.9 collection/disks/::zfs_io_countersrust-analyzer cargo bottom 0.14.9 collection/disks/zfs_io_counters/; |
| 91 | if let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(mut zfs_io) = zfs_io_countersrust-analyzer cargo bottom 0.14.9 collection/disks/zfs_io_counters/::zfs_io_statsrust-analyzer cargo bottom 0.14.9 collection/disks/zfs_io_counters/zfs_io_stats().() { |
| 92 | results.appendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]append().(&mut zfs_io); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(results) |
| 97 | } |