heim_process/os/linux/
io_counters.rs

1use std::fmt;
2
3use heim_common::units::{information, Information};
4
5/// Process IO statistics.
6///
7/// For additional information of data provided, see [proc.txt] documentation,
8/// section 3.3 "/proc/<pid>/io - Display the IO accounting fields".
9///
10/// [proc.txt]: https://www.kernel.org/doc/Documentation/filesystems/proc.txt
11#[derive(Default)]
12pub struct IoCounters {
13    pub(crate) rchar: u64,
14    pub(crate) wchar: u64,
15    pub(crate) syscr: u64,
16    pub(crate) syscw: u64,
17    pub(crate) read_bytes: u64,
18    pub(crate) write_bytes: u64,
19    pub(crate) cancelled_write_bytes: u64,
20}
21
22impl IoCounters {
23    /// The number of bytes which this task has caused to be read from storage.
24    pub fn chars_read(&self) -> Information {
25        Information::new::<information::byte>(self.rchar)
26    }
27
28    /// The number of bytes which this task has caused, or shall cause to be written to disk.
29    pub fn chars_written(&self) -> Information {
30        Information::new::<information::byte>(self.wchar)
31    }
32
33    /// Attempt to count the number of read I/O operations,
34    /// i.e. syscalls like `read()` and `pread()`.
35    pub fn read_syscalls(&self) -> u64 {
36        self.syscr
37    }
38
39    /// Attempt to count the number of write I/O operations,
40    /// i.e. syscalls like `write()` and `pwrite()`.
41    pub fn write_syscalls(&self) -> u64 {
42        self.syscw
43    }
44
45    /// Attempt to count the number of bytes which this process really did cause to
46    /// be fetched from the storage layer.
47    pub fn bytes_read(&self) -> Information {
48        Information::new::<information::byte>(self.read_bytes)
49    }
50
51    /// Attempt to count the number of bytes which this process caused to be sent to
52    /// the storage layer.
53    pub fn bytes_written(&self) -> Information {
54        Information::new::<information::byte>(self.write_bytes)
55    }
56
57    /// The number of bytes which this process caused to not happen,
58    /// by truncating pagecache.
59    pub fn cancelled_write_bytes(&self) -> Information {
60        Information::new::<information::byte>(self.cancelled_write_bytes)
61    }
62}
63
64impl fmt::Debug for IoCounters {
65    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
66        f.debug_struct("IoCounters")
67            .field("chars_read", &self.chars_read())
68            .field("chars_written", &self.chars_written())
69            .field("read_syscalls", &self.read_syscalls())
70            .field("write_syscalls", &self.write_syscalls())
71            .field("bytes_read", &self.bytes_read())
72            .field("bytes_written", &self.bytes_written())
73            .field("cancelled_write_bytes", &self.cancelled_write_bytes())
74            .finish()
75    }
76}