1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
use std::ffi::OsStr;
use std::fmt;

use heim_common::prelude::*;
use heim_common::units::Information;

use crate::sys;

/// Disk I/O counters.
///
/// ## Compatibility
///
/// See [os]-specific extension traits also.
///
/// On some systems such a Linux the numbers returned may overflow and wrap.
/// Contrary to `psutil` behavior, at the moment `heim` will not automatically
/// handle these cases and returned values might wrap.
///
/// [os]: ./os/index.html
pub struct IoCounters(sys::IoCounters);

wrap!(IoCounters, sys::IoCounters);

impl IoCounters {
    /// Returns disk device name.
    pub fn device_name(&self) -> &OsStr {
        self.as_ref().device_name()
    }

    /// Returns number of reads.
    pub fn read_count(&self) -> u64 {
        self.as_ref().read_count()
    }

    /// Returns number of writes.
    pub fn write_count(&self) -> u64 {
        self.as_ref().write_count()
    }

    /// Returns number of bytes read.
    pub fn read_bytes(&self) -> Information {
        self.as_ref().read_bytes()
    }

    /// Returns number of bytes written.
    pub fn write_bytes(&self) -> Information {
        self.as_ref().write_bytes()
    }
}

impl fmt::Debug for IoCounters {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("IoCounters")
            .field("device_name", &self.device_name())
            .field("read_count", &self.read_count())
            .field("write_count", &self.write_count())
            .field("read_bytes", &self.read_bytes())
            .field("write_bytes", &self.write_bytes())
            .finish()
    }
}

/// Returns a stream over [IO counters] for all disks available in system.
///
/// ## Compatibility
///
/// Same to similar tools, on Windows it may be necessary to issue `diskperf -y` command
/// from `cmd.exe` first in order to enable IO counters.
///
/// [IO counters]: struct.IoCounters.html
pub async fn io_counters() -> Result<impl Stream<Item = Result<IoCounters>>> {
    let inner = sys::io_counters().await?;

    Ok(inner.map_ok(Into::into))
}

/// Returns a stream over [IO counters] for each physical disk installed on the system.
///
/// [IO counters]: struct.IoCounters.html
pub async fn io_counters_physical() -> Result<impl Stream<Item = Result<IoCounters>>> {
    let inner = sys::io_counters_physical().await?;

    Ok(inner.map_ok(Into::into))
}