Skip to content

Commit 7576b13

Browse files
committed
cpu: some sanity fixes, make it be able to fail
1 parent 474ee6f commit 7576b13

File tree

1 file changed

+28
-34
lines changed

1 file changed

+28
-34
lines changed

watt/cpu.rs

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -194,29 +194,30 @@ impl Cpu {
194194
fn rescan_governor(&mut self) -> anyhow::Result<()> {
195195
let Self { number, .. } = *self;
196196

197-
self.available_governors = 'available_governors: {
198-
let Some(content) = fs::read(format!(
199-
"/sys/devices/system/cpu/cpu{number}/cpufreq/\
200-
scaling_available_governors"
201-
))
202-
.with_context(|| format!("failed to read {self} available governors"))?
203-
else {
204-
break 'available_governors Vec::new();
205-
};
197+
self.governor = fs::read(format!(
198+
"/sys/devices/system/cpu/cpu{number}/cpufreq/scaling_governor"
199+
))
200+
.with_context(|| format!("failed to read {self} scaling governor"))?;
206201

207-
content
208-
.split_whitespace()
209-
.map(ToString::to_string)
210-
.collect()
211-
};
202+
if self.governor.is_some() {
203+
self.available_governors = 'available_governors: {
204+
let Some(content) = fs::read(format!(
205+
"/sys/devices/system/cpu/cpu{number}/cpufreq/\
206+
scaling_available_governors"
207+
))
208+
.with_context(|| {
209+
format!("failed to read {self} available governors")
210+
})?
211+
else {
212+
break 'available_governors Vec::new();
213+
};
212214

213-
self.governor = Some(
214-
fs::read(format!(
215-
"/sys/devices/system/cpu/cpu{number}/cpufreq/scaling_governor"
216-
))
217-
.with_context(|| format!("failed to read {self} scaling governor"))?
218-
.with_context(|| format!("failed to find {self} scaling governor"))?,
219-
);
215+
content
216+
.split_whitespace()
217+
.map(ToString::to_string)
218+
.collect()
219+
};
220+
}
220221

221222
Ok(())
222223
}
@@ -227,22 +228,19 @@ impl Cpu {
227228
let frequency_khz = fs::read_n::<u64>(format!(
228229
"/sys/devices/system/cpu/cpu{number}/cpufreq/scaling_cur_freq"
229230
))
230-
.with_context(|| format!("failed to parse {self} frequency"))?
231-
.with_context(|| format!("failed to find {self} frequency"))?;
231+
.with_context(|| format!("failed to parse {self} frequency"))?;
232232
let frequency_khz_minimum = fs::read_n::<u64>(format!(
233233
"/sys/devices/system/cpu/cpu{number}/cpufreq/scaling_min_freq"
234234
))
235-
.with_context(|| format!("failed to parse {self} frequency minimum"))?
236-
.with_context(|| format!("failed to find {self} frequency"))?;
235+
.with_context(|| format!("failed to parse {self} frequency minimum"))?;
237236
let frequency_khz_maximum = fs::read_n::<u64>(format!(
238237
"/sys/devices/system/cpu/cpu{number}/cpufreq/scaling_max_freq"
239238
))
240-
.with_context(|| format!("failed to parse {self} frequency maximum"))?
241-
.with_context(|| format!("failed to find {self} frequency"))?;
239+
.with_context(|| format!("failed to parse {self} frequency maximum"))?;
242240

243-
self.frequency_mhz = Some(frequency_khz / 1000);
244-
self.frequency_mhz_minimum = Some(frequency_khz_minimum / 1000);
245-
self.frequency_mhz_maximum = Some(frequency_khz_maximum / 1000);
241+
self.frequency_mhz = frequency_khz.map(|x| x / 1000);
242+
self.frequency_mhz_minimum = frequency_khz_minimum.map(|x| x / 1000);
243+
self.frequency_mhz_maximum = frequency_khz_maximum.map(|x| x / 1000);
246244

247245
Ok(())
248246
}
@@ -280,10 +278,6 @@ impl Cpu {
280278
fn rescan_epb(&mut self) -> anyhow::Result<()> {
281279
let Self { number, .. } = self;
282280

283-
if !self.has_cpufreq {
284-
return Ok(());
285-
}
286-
287281
self.epb = fs::read(format!(
288282
"/sys/devices/system/cpu/cpu{number}/cpufreq/energy_performance_bias"
289283
))

0 commit comments

Comments
 (0)