-
Notifications
You must be signed in to change notification settings - Fork 108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
set governor to "performance" if available #2438
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
use std::borrow::Cow; | ||
use std::collections::BTreeSet; | ||
|
||
use imbl::OrdMap; | ||
use tokio::process::Command; | ||
|
||
use crate::prelude::*; | ||
use crate::util::Invoke; | ||
|
||
pub const GOVERNOR_PERFORMANCE: Governor = Governor(Cow::Borrowed("performance")); | ||
|
||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] | ||
pub struct Governor(Cow<'static, str>); | ||
impl std::fmt::Display for Governor { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
self.0.fmt(f) | ||
} | ||
} | ||
impl std::ops::Deref for Governor { | ||
type Target = str; | ||
fn deref(&self) -> &Self::Target { | ||
&*self.0 | ||
} | ||
} | ||
impl std::borrow::Borrow<str> for Governor { | ||
fn borrow(&self) -> &str { | ||
&**self | ||
} | ||
} | ||
|
||
pub async fn get_available_governors() -> Result<BTreeSet<Governor>, Error> { | ||
let raw = String::from_utf8( | ||
Command::new("cpupower") | ||
.arg("frequency-info") | ||
.arg("-g") | ||
.invoke(ErrorKind::CpuSettings) | ||
.await?, | ||
)?; | ||
let mut for_cpu: OrdMap<u32, BTreeSet<Governor>> = OrdMap::new(); | ||
let mut current_cpu = None; | ||
for line in raw.lines() { | ||
if line.starts_with("analyzing") { | ||
current_cpu = Some( | ||
sscanf::sscanf!(line, "analyzing CPU {u32}:") | ||
.map_err(|e| eyre!("{e}")) | ||
.with_kind(ErrorKind::ParseSysInfo)?, | ||
); | ||
} else if let Some(rest) = line | ||
.trim() | ||
.strip_prefix("available cpufreq governors:") | ||
.map(|s| s.trim()) | ||
{ | ||
if rest != "Not Available" { | ||
for_cpu | ||
.entry(current_cpu.ok_or_else(|| { | ||
Error::new( | ||
eyre!("governors listed before cpu"), | ||
ErrorKind::ParseSysInfo, | ||
) | ||
})?) | ||
.or_default() | ||
.extend( | ||
rest.split_ascii_whitespace() | ||
.map(|g| Governor(Cow::Owned(g.to_owned()))), | ||
); | ||
} | ||
} | ||
} | ||
Ok(for_cpu | ||
.into_iter() | ||
.fold(None, |acc: Option<BTreeSet<Governor>>, (_, x)| { | ||
if let Some(acc) = acc { | ||
Some(acc.intersection(&x).cloned().collect()) | ||
} else { | ||
Some(x) | ||
} | ||
}) | ||
.unwrap_or_default()) // include only governors available for ALL cpus | ||
} | ||
|
||
pub async fn current_governor() -> Result<Option<Governor>, Error> { | ||
let Some(raw) = Command::new("cpupower") | ||
.arg("frequency-info") | ||
.arg("-p") | ||
.invoke(ErrorKind::CpuSettings) | ||
.await | ||
.and_then(|s| Ok(Some(String::from_utf8(s)?))) | ||
.or_else(|e| { | ||
if e.source | ||
.to_string() | ||
.contains("Unable to determine current policy") | ||
{ | ||
Ok(None) | ||
} else { | ||
Err(e) | ||
} | ||
})? | ||
else { | ||
return Ok(None); | ||
}; | ||
|
||
for line in raw.lines() { | ||
if let Some(governor) = line | ||
.trim() | ||
.strip_prefix("The governor \"") | ||
.and_then(|s| s.strip_suffix("\" may decide which speed to use")) | ||
{ | ||
return Ok(Some(Governor(Cow::Owned(governor.to_owned())))); | ||
} | ||
} | ||
Err(Error::new( | ||
eyre!("Failed to parse cpupower output:\n{raw}"), | ||
ErrorKind::ParseSysInfo, | ||
)) | ||
} | ||
|
||
pub async fn set_governor(governor: &Governor) -> Result<(), Error> { | ||
Command::new("cpupower") | ||
.arg("frequency-set") | ||
.arg("-g") | ||
.arg(&*governor.0) | ||
.invoke(ErrorKind::CpuSettings) | ||
.await?; | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ iw | |
jq | ||
libavahi-client3 | ||
libyajl2 | ||
linux-cpupower | ||
lm-sensors | ||
lshw | ||
lvm2 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't a is_performance method be better for what is returned on current_govenrnor()