Skip to content
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

Add powersave on overheat rules #221

Merged
merged 8 commits into from
Feb 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions acs.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# acs.toml
powersave_under = 20
overheat_threshold = 80
4 changes: 3 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ pub fn config_dir_exists() -> bool {
#[derive(Debug, Deserialize, Serialize)]
pub struct Config {
pub powersave_under: i8,
pub overheat_threshold: i8,
// Future variables
// pub charging_powersave_under: i32,
}

pub fn default_config() -> Config {
Config {
powersave_under: 20,
overheat_threshold: 80,
}
}

Expand All @@ -52,7 +54,7 @@ fn parse_as_toml(config: String) -> Config {

pub fn open_config() -> Result<Config, std::io::Error> {
let conf_path = config_path();
let mut config_file: File = File::open(&conf_path).unwrap();
let mut config_file: File = File::open(&conf_path)?;
let config_string = read_as_string(&mut config_file);
let config_toml = parse_as_toml(config_string);

Expand Down
45 changes: 45 additions & 0 deletions src/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ pub trait Checker {
// Under Powersave Under Rule
fn under_powersave_under_rule(&mut self) -> Result<(), Error>;

// High Temperature Rule
fn start_high_temperature_rule(&mut self) -> Result<(), Error>;
fn end_high_temperature_rule(&mut self) -> Result<(), Error>;

// Other methods
fn run(&mut self) -> Result<(), Error>;
fn init(&mut self);
Expand Down Expand Up @@ -72,8 +76,10 @@ pub struct Daemon {
pub already_charging: bool,
pub already_closed: bool,
pub already_under_powersave_under_percent: bool,
pub already_high_temp: bool,
pub graph: String,
pub grapher: Graph,
pub temp_max: i8,
pub commit_hash: String,
pub timeout: time::Duration,
pub settings: Settings,
Expand All @@ -89,6 +95,16 @@ fn make_gov_performance(cpu: &mut CPU) -> Result<(), Error> {
Ok(())
}

fn get_highest_temp(cpus: &Vec<CPU>) -> i32 {
let mut temp_max: i32 = 0;
for cpu in cpus {
if cpu.cur_temp > temp_max {
temp_max = cpu.cur_temp;
}
}
temp_max
}

fn green_or_red(boolean: bool) -> String {
if boolean {
color::Fg(color::Green).to_string()
Expand Down Expand Up @@ -208,6 +224,29 @@ impl Checker for Daemon {
Ok(())
}

fn start_high_temperature_rule(&mut self) -> Result<(), Error> {
if !self.already_high_temp && self.temp_max > self.config.overheat_threshold {
self.logger.log(
"Governor set to powersave because CPU temperature is high",
logger::Severity::Log,
);
self.apply_to_cpus(&make_gov_powersave)?;
self.already_high_temp = true;
}
Ok(())
}

fn end_high_temperature_rule(&mut self) -> Result<(), Error> {
if self.already_high_temp && self.temp_max < self.config.overheat_threshold {
self.logger.log(
"Governor set to powesave because CPU temperature is high",
logger::Severity::Log,
);
self.already_high_temp = false;
}
Ok(())
}

fn lid_close_rule(&mut self) -> Result<(), Error> {
if self.lid_state == LidState::Closed && !self.already_closed {
self.logger.log(
Expand Down Expand Up @@ -313,6 +352,8 @@ impl Checker for Daemon {
self.start_loop()?;

// Call all rules
self.start_high_temperature_rule()?;
self.end_high_temperature_rule()?;
self.start_charging_rule()?;
self.end_charging_rule()?;
self.lid_close_rule()?;
Expand Down Expand Up @@ -350,6 +391,8 @@ impl Checker for Daemon {
cpu.update()?;
}

self.temp_max = (get_highest_temp(&self.cpus) / 1000) as i8;

// Update the data in the graph and render it
if self.settings.should_graph {
self.grapher.freqs.push(check_cpu_freq()? as f64);
Expand Down Expand Up @@ -511,8 +554,10 @@ pub fn daemon_init(settings: Settings, config: Config) -> Result<Daemon, Error>
already_charging: false,
already_closed: false,
already_under_powersave_under_percent: false,
already_high_temp: false,
graph: String::new(),
grapher: Graph { freqs: vec![0.0] },
temp_max: 0,
commit_hash: String::new(),
timeout: time::Duration::from_millis(1),
settings,
Expand Down