summaryrefslogtreecommitdiff
path: root/src/freq.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/freq.rs')
-rw-r--r--src/freq.rs61
1 files changed, 44 insertions, 17 deletions
diff --git a/src/freq.rs b/src/freq.rs
index 2cfcf75..aa575f9 100644
--- a/src/freq.rs
+++ b/src/freq.rs
@@ -8,10 +8,33 @@ use std::time::Duration;
pub type FrequencyKHZ = u32;
+pub enum Governor {
+ Conservative,
+ Ondemand,
+ Userspace,
+ Powersave,
+ Performance,
+ Schedutil,
+}
+
+impl Governor {
+ fn to_sysfs_string(&self) -> &str {
+ match self {
+ Governor::Conservative => "conservative",
+ Governor::Performance => "performance",
+ Governor::Schedutil => "schedutil",
+ Governor::Powersave => "powersave",
+ Governor::Userspace => "userspace",
+ Governor::Ondemand => "ondemand",
+ }
+ }
+}
+
pub enum Request {
GetPossibleCPUFrequencyRange,
GetPolicyCPUFrequency,
GetCurrentFrequencies,
+ SetGovernorForCore(u32, Governor),
SetFrequencyRangeAllCores(RangeInclusive<FrequencyKHZ>),
SetFrequencyRangeForCore(u32, RangeInclusive<FrequencyKHZ>),
SetTargetFrequencyForCore(u32, FrequencyKHZ),
@@ -204,8 +227,10 @@ impl SysFSFrequencyService {
self.cpu_descriptors = CPUDescriptors::new_range(&self.cpus).unwrap();
let ranges = self.get_freq_limits().unwrap();
self.frequency_ranges = ranges;
- if self.switch_governor("conservative").is_err() {
- println!("failed to set governor to conservative");
+ for cpu in self.cpus.clone() {
+ if self.switch_governor(cpu, Governor::Conservative).is_err() {
+ println!("failed to set governor to conservative");
+ }
}
loop {
@@ -232,6 +257,9 @@ impl SysFSFrequencyService {
*self.cpu_current_frequencies.write().unwrap() =
self.get_current_frequencies()?;
}
+ Request::SetGovernorForCore(cpu, governor) => {
+ self.switch_governor(cpu, governor)?;
+ }
Request::SetFrequencyRangeAllCores(frequency) => {
self.set_frequency_range_all_cores(&frequency)?;
}
@@ -267,27 +295,26 @@ impl SysFSFrequencyService {
Ok(ranges)
}
- fn switch_governor(&self, governor: &str) -> io::Result<()> {
+ fn switch_governor(&self, cpu: u32, governor: Governor) -> io::Result<()> {
+ let governor = governor.to_sysfs_string();
let available_governor =
fs::read_to_string("/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors")?;
if !available_governor.contains(governor) {
io::Error::new(io::ErrorKind::NotFound, governor);
}
- for cpu in self.cpus.clone() {
- let current_governor = fs::read_to_string(format!(
- "/sys/devices/system/cpu/cpu{}/cpufreq/scaling_governor",
- cpu
- ))?;
- if current_governor != governor {
- fs::write(
- format!(
- "/sys/devices/system/cpu/cpu{}/cpufreq/scaling_governor",
- cpu
- ),
- governor,
- )?;
- }
+ let current_governor = fs::read_to_string(format!(
+ "/sys/devices/system/cpu/cpu{}/cpufreq/scaling_governor",
+ cpu
+ ))?;
+ if current_governor != governor {
+ fs::write(
+ format!(
+ "/sys/devices/system/cpu/cpu{}/cpufreq/scaling_governor",
+ cpu
+ ),
+ governor,
+ )?;
}
Ok(())
}