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;
#[derive(Debug, Copy, Clone)]
pub struct CpuInstant {
instant: time::Instant,
cpu_total: f64,
cpu_idle: f64,
}
impl CpuInstant {
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,
})
}
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,
}
}
}
#[derive(Debug, Copy, Clone)]
pub struct CpuDuration {
duration: time::Duration,
cpu_total: f64,
cpu_idle: f64,
}
impl CpuDuration {
pub fn duration(&self) -> time::Duration {
self.duration
}
pub fn idle(&self) -> f64 {
self.cpu_idle / self.cpu_total
}
pub fn non_idle(&self) -> f64 {
1.0 - self.idle()
}
}