heim_disk/
counters.rs

1use std::ffi::OsStr;
2use std::fmt;
3
4use heim_common::prelude::*;
5use heim_common::units::Information;
6
7use crate::sys;
8
9/// Disk I/O counters.
10///
11/// ## Compatibility
12///
13/// See [os]-specific extension traits also.
14///
15/// On some systems such a Linux the numbers returned may overflow and wrap.
16/// Contrary to `psutil` behavior, at the moment `heim` will not automatically
17/// handle these cases and returned values might wrap.
18///
19/// [os]: ./os/index.html
20pub struct IoCounters(sys::IoCounters);
21
22wrap!(IoCounters, sys::IoCounters);
23
24impl IoCounters {
25    /// Returns disk device name.
26    pub fn device_name(&self) -> &OsStr {
27        self.as_ref().device_name()
28    }
29
30    /// Returns number of reads.
31    pub fn read_count(&self) -> u64 {
32        self.as_ref().read_count()
33    }
34
35    /// Returns number of writes.
36    pub fn write_count(&self) -> u64 {
37        self.as_ref().write_count()
38    }
39
40    /// Returns number of bytes read.
41    pub fn read_bytes(&self) -> Information {
42        self.as_ref().read_bytes()
43    }
44
45    /// Returns number of bytes written.
46    pub fn write_bytes(&self) -> Information {
47        self.as_ref().write_bytes()
48    }
49}
50
51impl fmt::Debug for IoCounters {
52    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
53        f.debug_struct("IoCounters")
54            .field("device_name", &self.device_name())
55            .field("read_count", &self.read_count())
56            .field("write_count", &self.write_count())
57            .field("read_bytes", &self.read_bytes())
58            .field("write_bytes", &self.write_bytes())
59            .finish()
60    }
61}
62
63/// Returns stream which will yield [IO counters] for all disks available in system.
64///
65/// ## Compatibility
66///
67/// Same to similar tools, on Windows it may be necessary to issue `diskperf -y` command
68/// from `cmd.exe` first in order to enable IO counters.
69///
70/// [IO counters]: struct.IoCounters.html
71pub fn io_counters() -> impl Stream<Item = Result<IoCounters>> {
72    sys::io_counters().map_ok(Into::into)
73}
74
75/// Returns future which will resolve into [IO counters]
76/// for each physical disk installed on the system.
77///
78/// [IO counters]: struct.IoCounters.html
79pub fn io_counters_physical() -> impl Stream<Item = Result<IoCounters>> {
80    sys::io_counters_physical().map_ok(Into::into)
81}