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
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
use std::fmt;
use std::ffi::OsStr;

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
#[derive(heim_derive::ImplWrap)]
pub struct 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 stream which will yield [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.
///
/// ## Examples
///
/// ```rust
/// # #![feature(async_await, futures_api)]
/// # use heim_common::prelude::*;
/// # use heim_common::Result;
/// # use heim_disk::io_counters;
/// #
/// # #[runtime::main]
/// # async fn main() -> Result<()> {
/// let mut counters_stream = io_counters();
/// while let Some(counter) = counters_stream.next().await {
///    dbg!(counter?);
/// }
/// # Ok(())
/// # }
/// ```
///
/// [IO counters]: struct.IoCounters.html
pub fn io_counters() -> impl Stream<Item = Result<IoCounters>> {
    sys::io_counters().map_ok(Into::into)
}

/// Returns future which will resolve into [IO counters]
/// for each physical disk installed on the system.
///
/// ## Examples
///
/// ```rust
/// # #![feature(async_await, futures_api)]
/// # use heim_common::prelude::*;
/// # use heim_common::Result;
/// # use heim_disk::io_counters_physical;
/// #
/// # #[runtime::main]
/// # async fn main() -> Result<()> {
/// let mut counters_stream = io_counters_physical();
/// while let Some(counter) = counters_stream.next().await {
///    dbg!(counter?);
/// }
/// # Ok(())
/// # }
/// ```
///
/// [IO counters]: struct.IoCounters.html
pub fn io_counters_physical() -> impl Stream<Item = Result<IoCounters>> {
    sys::io_counters_physical().map_ok(Into::into)
}