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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
use std::{
collections::HashMap,
path::{Path, PathBuf},
};
use crate::sysfs::SysFS;
#[derive(Debug)]
pub struct HwMon {
path: PathBuf,
}
impl HwMon {
pub fn new_from_path(path: PathBuf) -> Result<Self, HwMonError> {
let hw_mon = Self { path };
match hw_mon.read_file("name") {
Some(_) => Ok(hw_mon),
None => Err(HwMonError::InvalidSysFS),
}
}
pub fn get_temps(&self) -> HashMap<String, Temperature> {
let mut temps = HashMap::new();
let mut i = 1;
while let Some(label) = self.read_file(&format!("temp{}_label", i)) {
temps.insert(
label,
Temperature {
current: self
.read_file(&format!("temp{}_input", i))
.unwrap_or_default()
.parse::<f32>()
.expect("Invalid temperature (driver bug?)")
/ 1000.0,
crit: self
.read_file(&format!("temp{}_crit", i))
.unwrap_or_default()
.parse::<f32>()
.expect("Invalid temperature (driver bug?)")
/ 1000.0,
crit_hyst: self
.read_file(&format!("temp{}_crit_hyst", i))
.unwrap_or_default()
.parse::<f32>()
.expect("Invalid temperature (driver bug?)")
/ 1000.0,
},
);
i += 1;
}
temps
}
pub fn get_gpu_clockspeed(&self) -> Option<u64> {
self.read_file("freq1_input").map(|f| {
f.parse::<u64>()
.expect("Unexpected GPU clockspeed (driver bug?)")
/ 1000000
})
}
pub fn get_vram_clockspeed(&self) -> Option<u64> {
self.read_file("freq2_input").map(|f| {
f.parse::<u64>()
.expect("Unexpected VRAM clockspeed (driver bug?)")
/ 1000000
})
}
pub fn get_power_average(&self) -> Option<f64> {
self.read_file("power1_average").map(|p| {
p.parse::<f64>()
.expect("Unexpected power usage (driver bug?)")
/ 1000000.0
})
}
pub fn get_power_cap(&self) -> Option<f64> {
self.read_file("power1_cap").map(|p| {
p.parse::<f64>()
.expect("Unexpected power usage (driver bug?)")
/ 1000000.0
})
}
pub fn get_power_cap_max(&self) -> Option<f64> {
self.read_file("power1_cap_max").map(|p| {
p.parse::<f64>()
.expect("Unexpected power usage (driver bug?)")
/ 1000000.0
})
}
pub fn get_power_cap_min(&self) -> Option<f64> {
self.read_file("power1_cap_min").map(|p| {
p.parse::<f64>()
.expect("Unexpected power usage (driver bug?)")
/ 1000000.0
})
}
pub fn get_fan_pwm(&self) -> Option<u8> {
self.read_file("pwm1")
.map(|pwm| pwm.parse().expect("Unexpected PWM (driver bug?)"))
}
pub fn get_fan_current(&self) -> Option<u32> {
self.read_file("fan1_input").map(|s| {
s
.parse()
.expect("Unexpected fan1_input (driver bug?)")
})
}
pub fn get_fan_max(&self) -> Option<u32> {
self.read_file("fan1_max").map(|s| {
s
.parse()
.expect("Unexpected fan1_max (driver bug?)")
})
}
pub fn get_fan_min(&self) -> Option<u32> {
self.read_file("fan1_min").map(|s| {
s
.parse()
.expect("Unexpected fan1_min (driver bug?)")
})
}
pub fn get_fan_target(&self) -> Option<u32> {
self.read_file("fan1_target").map(|s| {
s
.parse()
.expect("Unexpected fan1_target (driver bug?)")
})
}
pub fn get_fan_control_method(&self) -> Option<FanControlMethod> {
self.read_file("pwm1_enable").map(|pwm1_enable| {
FanControlMethod::from_enable(
pwm1_enable
.parse()
.expect("Unexpected pwm1_enable (driver bug?)"),
)
.expect("Unexpected pwm1_enable (driver bug or unsupported?)")
})
}
}
impl SysFS for HwMon {
fn get_path(&self) -> &Path {
&self.path
}
}
#[derive(Debug, Clone)]
pub struct Temperature {
pub current: f32,
pub crit: f32,
pub crit_hyst: f32,
}
#[derive(Debug)]
pub enum HwMonError {
InvalidSysFS,
InvalidValue,
}
pub enum FanControlMethod {
None,
Auto,
Manual,
}
impl FanControlMethod {
pub fn from_enable(enable_value: u8) -> Result<Self, HwMonError> {
match enable_value {
0 => Ok(Self::None),
1 => Ok(Self::Manual),
2 => Ok(Self::Auto),
_ => Err(HwMonError::InvalidValue),
}
}
}