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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
use std::ops;
use std::path::{Path, PathBuf};
use heim_common::prelude::*;
use crate::units::Frequency;
#[derive(Debug, Default, heim_derive::Getter)]
pub struct CpuFrequency {
current: Frequency,
min: Option<Frequency>,
max: Option<Frequency>,
}
impl ops::Add<CpuFrequency> for CpuFrequency {
type Output = CpuFrequency;
fn add(self, rhs: CpuFrequency) -> CpuFrequency {
let current = self.current + rhs.current;
let min = match (self.min, rhs.min) {
(Some(left), Some(right)) => Some(left + right),
(Some(left), None) => Some(left),
(None, Some(right)) => Some(right),
(None, None) => None,
};
let max = match (self.max, rhs.max) {
(Some(left), Some(right)) => Some(left + right),
(Some(left), None) => Some(left),
(None, Some(right)) => Some(right),
(None, None) => None,
};
CpuFrequency {
current,
max,
min,
}
}
}
pub fn frequency() -> impl Future<Output = Result<CpuFrequency>> {
let init = CpuFrequency::default();
frequencies()
.try_fold((init, 0u64), |(acc, amount), freq| future::ok((acc + freq, amount + 1)))
.then(|result| {
match result {
// Will panic here if `frequencies()` stream returns nothing,
// which is either a bug in implementation or we are in container
// and should fetch information from the another place.
//
// Also, `bind_by_move_pattern_guards` feature
// would simplify the following code a little,
// `freq` can be modified and returned in place
Ok((ref freq, amount)) if amount > 0 => future::ok(CpuFrequency {
current: freq.current / amount,
min: freq.min.map(|value| value / amount),
max: freq.max.map(|value| value / amount),
}),
// Unable to determine CPU frequencies for some reasons.
// Might happen for containerized environments, such as Microsoft Azure, for example.
Ok(_) => future::err(Error::incompatible("No CPU frequencies was found, running in VM?")),
Err(e) => future::err(e),
}
})
}
pub fn frequencies() -> impl Stream<Item = Result<CpuFrequency>> {
// TODO: psutil looks into `/sys/devices/system/cpu/cpufreq/policy*` at first
// But at my machine with Linux 5.0 `./cpu/cpu*/cpufreq` are symlinks to the `policy*`,
// so at least we will cover most cases in first iteration and will fix weird values
// later with the thoughts and patches
// TODO: https://github.com/giampaolo/psutil/issues/1269
// TODO: `glob::glob` is synchronous, should replace it with some async dir reader
let walker = glob::glob("/sys/devices/system/cpu/cpu[0-9]*/cpufreq").expect("Invalid glob pattern");
stream::iter(walker)
.map_err(|e| Error::from(Box::new(e)))
.and_then(|path| {
let current = current_freq(&path);
let max = max_freq(&path);
let min = min_freq(&path);
future::try_join3(current, max, min)
})
.and_then(|(current, max, min)| {
future::ok(CpuFrequency {
current,
max,
min,
})
})
}
#[allow(clippy::redundant_closure)]
fn read_freq(path: PathBuf) -> impl Future<Output = Result<Frequency>> {
utils::fs::read_to_string(path)
.and_then(|value| future::ready(value.trim_end().parse::<u64>().map_err(Error::from)))
.map_ok(Frequency::new)
}
fn current_freq(path: &Path) -> impl Future<Output = Result<Frequency>> {
// TODO: Wait for Future' `try_select_all` and uncomment the block below
// Ref: https://github.com/rust-lang-nursery/futures-rs/pull/1557
read_freq(path.join("scaling_cur_freq"))
// let one = read_freq(path.join("scaling_cur_freq"))
// .into_future().fuse();
// let two = read_freq(path.join("cpuinfo_cur_freq"))
// .into_future().fuse();
//
// let result = futures::select! {
// Ok(freq) = one => Ok(freq),
// Ok(freq) = two => Ok(freq),
// };
//
// future::ready(result)
}
fn max_freq(path: &Path) -> impl Future<Output = Result<Option<Frequency>>> {
read_freq(path.join("scaling_max_freq"))
.into_future()
.map(|value| Ok(value.ok()))
}
fn min_freq(path: &Path) -> impl Future<Output = Result<Option<Frequency>>> {
read_freq(path.join("scaling_min_freq"))
.into_future()
.map(|value| Ok(value.ok()))
}