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
use std::io::{self, Read, Write};
use std::fmt::Display;
use std::fs::{File, OpenOptions};
use std::path::{Path, PathBuf};
use std::str::FromStr;
pub struct PState {
path: PathBuf,
}
impl PState {
pub fn new() -> io::Result<PState> {
let path = PathBuf::from("/sys/devices/system/cpu/intel_pstate");
if path.is_dir() {
Ok(PState { path })
} else {
Err(io::Error::new(io::ErrorKind::NotFound, "intel_pstate directory not found"))
}
}
pub fn min_perf_pct(&self) -> io::Result<u64> {
parse_file(self.path.join("min_perf_pct"))
}
pub fn set_min_perf_pct(&self, value: u64) -> io::Result<()> {
write_file(self.path.join("min_perf_pct"), format!("{}", value))
}
pub fn max_perf_pct(&self) -> io::Result<u64> {
parse_file(self.path.join("max_perf_pct"))
}
pub fn set_max_perf_pct(&self, value: u64) -> io::Result<()> {
write_file(self.path.join("max_perf_pct"), format!("{}", value))
}
pub fn no_turbo(&self) -> io::Result<bool> {
let value: u64 = parse_file(self.path.join("no_turbo"))?;
Ok(value > 0)
}
pub fn set_no_turbo(&self, value: bool) -> io::Result<()> {
write_file(self.path.join("no_turbo"), if value { "1" } else { "0" })
}
}
fn read_file<P: AsRef<Path>>(path: P) -> io::Result<String> {
let mut data = String::new();
{
let mut file = File::open(path.as_ref())?;
file.read_to_string(&mut data)?;
}
Ok(data)
}
fn write_file<P: AsRef<Path>, S: AsRef<[u8]>>(path: P, data: S) -> io::Result<()> {
{
let mut file = OpenOptions::new().write(true).open(path)?;
file.write_all(data.as_ref())?
}
Ok(())
}
fn parse_file<F: FromStr, P: AsRef<Path>>(path: P) -> io::Result<F> where F::Err: Display {
read_file(path)?.trim().parse().map_err(|err| {
io::Error::new(
io::ErrorKind::InvalidData,
format!("{}", err)
)
})
}