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

#[cfg(windows)]
extern crate winapi;
#[cfg(unix)]
extern crate linux_proc;

use std::time;
use std::ops;
use std::io;

mod imp;

/// Like `std::time::Instant`, but with information about the cpu usage stats.
#[derive(Debug, Copy, Clone)]
pub struct CpuInstant {
    instant: time::Instant,
    cpu_total: f64,
    cpu_idle: f64,
}

impl CpuInstant {
    /// Get the current instant.
    ///
    /// The main constructor method of the crate.
    pub fn now() -> io::Result<CpuInstant> {
        let (cpu_total, cpu_idle) = imp::get_cpu_totals()?;
        Ok(CpuInstant {
            instant: time::Instant::now(),
            cpu_total,
            cpu_idle,
        })
    }

    /// Get the wrapped `time::Instant`.
    pub fn instant(&self) -> time::Instant {
        self.instant
    }
}

impl ops::Sub for CpuInstant {
    type Output = CpuDuration;

    fn sub(self, rhs: Self) -> Self::Output {
        CpuDuration {
            duration: self.instant - rhs.instant,
            cpu_total: self.cpu_total - rhs.cpu_total,
            cpu_idle: self.cpu_idle - rhs.cpu_idle,
        }
    }
}

/// Like `std::time::Duration`, but with information about the cpu usage stats.
///
/// The way to get this is to subtract one `CpuInstant` from another.
#[derive(Debug, Copy, Clone)]
pub struct CpuDuration {
    duration: time::Duration,
    cpu_total: f64,
    cpu_idle: f64,
}

impl CpuDuration {
    /// The gap between samples.
    pub fn duration(&self) -> time::Duration {
        self.duration
    }

    /// The proportion of the time spent idle (between 0 and 1).
    pub fn idle(&self) -> f64 {
        self.cpu_idle / self.cpu_total
    }

    /// The proportion of the time spent not idle (between 0 and 1).
    pub fn non_idle(&self) -> f64 {
        1.0 - self.idle()
    }
}