From 327771b218047295efb0942e6a5f62f866b5631c Mon Sep 17 00:00:00 2001 From: Camerooooon Date: Fri, 27 May 2022 11:48:34 -0700 Subject: [PATCH 1/6] Add rule function to trait --- src/daemon.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/daemon.rs b/src/daemon.rs index 2cf77031..839886a0 100644 --- a/src/daemon.rs +++ b/src/daemon.rs @@ -49,6 +49,10 @@ pub trait Checker { // High Temperature Rule fn start_high_temperature_rule(&mut self) -> Result<(), Error>; fn end_high_temperature_rule(&mut self) -> Result<(), Error>; + + // High CPU Usage Rule + fn start_cpu_usage_rule(&mut self) -> Result<(), Error>; + fn end_cpu_usage_rule(&mut self) -> Result<(), Error>; // Other methods fn run(&mut self) -> Result<(), Error>; From 65f6061f8b8027f5ad00d94f63edc8cabca31b8b Mon Sep 17 00:00:00 2001 From: Camerooooon Date: Sat, 28 May 2022 23:15:05 -0700 Subject: [PATCH 2/6] Calculate average usage inside of daemon --- src/daemon.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/daemon.rs b/src/daemon.rs index 839886a0..f0a89d9c 100644 --- a/src/daemon.rs +++ b/src/daemon.rs @@ -1,5 +1,6 @@ use std::convert::TryInto; use std::{thread, time}; +use std::time::{SystemTime}; use colored::*; use nix::unistd::Uid; @@ -86,6 +87,7 @@ pub struct Daemon { pub already_closed: bool, pub already_under_powersave_under_percent: bool, pub already_high_temp: bool, + pub last_below_cpu_usage_percent: SystemTime, pub graph: String, pub grapher: Graph, pub temp_max: i8, @@ -147,6 +149,14 @@ fn print_turbo_status(cores: usize, no_animation: bool, term_width: usize, delay Err(e) => eprintln!("Could not check turbo\n{:?}", e), } } + +fn calculate_average_usage(cpus: &Vec) -> Result { + let mut sum = 0.0; + for cpu in cpus { + sum += cpu.cur_usage; + } + Ok((sum / (cpus.len() as f32)) as f32) +} impl Checker for Daemon { /// Apply a function to every cpu @@ -202,6 +212,7 @@ impl Checker for Daemon { } fn end_charging_rule(&mut self) -> Result<(), Error> { + if !self.charging && self.already_charging { self.logger.log( "Governor set to powersave because battery is not charging", @@ -297,6 +308,16 @@ impl Checker for Daemon { } Ok(()) } + + + fn start_cpu_usage_rule(&mut self) -> Result<(), Error> { + println!("{:?}", calculate_average_usage(&self.cpus)? * 100.0); + Ok(()) + } + + fn end_cpu_usage_rule(&mut self) -> Result<(), Error> { + Ok(()) + } fn init(&mut self) { // Get the commit hash from the compile time env variable @@ -351,6 +372,8 @@ impl Checker for Daemon { self.lid_close_rule()?; self.lid_open_rule()?; self.under_powersave_under_rule()?; + self.start_cpu_usage_rule()?; + self.end_cpu_usage_rule()?; self.end_loop(); Ok(()) @@ -601,6 +624,7 @@ pub fn daemon_init(settings: Settings, config: Config) -> Result already_closed: false, already_under_powersave_under_percent: false, already_high_temp: false, + last_below_cpu_usage_percent: SystemTime::now(), graph: String::new(), grapher: Graph { freqs: vec![0.0] }, temp_max: 0, From cfd990401b32fafdb288cd5d3d9e3b4bb4a74c58 Mon Sep 17 00:00:00 2001 From: Camerooooon Date: Sun, 29 May 2022 12:03:45 -0700 Subject: [PATCH 3/6] Set timestamp when cpu is above threshold --- src/daemon.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/daemon.rs b/src/daemon.rs index f0a89d9c..d254570d 100644 --- a/src/daemon.rs +++ b/src/daemon.rs @@ -81,13 +81,14 @@ pub struct Daemon { pub lid_state: LidState, pub charging: bool, pub charge: i8, + pub usage: f32, pub logger: logger::Logger, pub config: Config, pub already_charging: bool, pub already_closed: bool, pub already_under_powersave_under_percent: bool, pub already_high_temp: bool, - pub last_below_cpu_usage_percent: SystemTime, + pub last_below_cpu_usage_percent: Option, pub graph: String, pub grapher: Graph, pub temp_max: i8, @@ -311,7 +312,10 @@ impl Checker for Daemon { fn start_cpu_usage_rule(&mut self) -> Result<(), Error> { - println!("{:?}", calculate_average_usage(&self.cpus)? * 100.0); + if self.usage > 70.0 && self.last_below_cpu_usage_percent == None { + self.last_below_cpu_usage_percent = Some(SystemTime::now()); + } + self.logger.log(&format!("{:?}", self.last_below_cpu_usage_percent), logger::Severity::Log); Ok(()) } @@ -344,6 +348,7 @@ impl Checker for Daemon { self.charging = read_power_source()?; self.charge = read_battery_charge()?; self.lid_state = read_lid_state()?; + self.usage = calculate_average_usage(&self.cpus)? * 100.0; Ok(()) } @@ -616,6 +621,7 @@ pub fn daemon_init(settings: Settings, config: Config) -> Result false }, charge: 100, + usage: 0.0, logger: logger::Logger { logs: Vec::::new(), }, @@ -624,7 +630,7 @@ pub fn daemon_init(settings: Settings, config: Config) -> Result already_closed: false, already_under_powersave_under_percent: false, already_high_temp: false, - last_below_cpu_usage_percent: SystemTime::now(), + last_below_cpu_usage_percent: None, graph: String::new(), grapher: Graph { freqs: vec![0.0] }, temp_max: 0, From 44a89195828822af0e0bf0fb365b4e4db4ec8bba Mon Sep 17 00:00:00 2001 From: Camerooooon Date: Sun, 29 May 2022 13:34:24 -0700 Subject: [PATCH 4/6] Log when cpu usage is above 70% for longer than 15 seconds --- src/daemon.rs | 24 +++++++++++++++++++++--- src/error.rs | 7 +++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/daemon.rs b/src/daemon.rs index d254570d..0054f2e8 100644 --- a/src/daemon.rs +++ b/src/daemon.rs @@ -88,6 +88,7 @@ pub struct Daemon { pub already_closed: bool, pub already_under_powersave_under_percent: bool, pub already_high_temp: bool, + pub already_high_cpu: bool, pub last_below_cpu_usage_percent: Option, pub graph: String, pub grapher: Graph, @@ -226,7 +227,7 @@ impl Checker for Daemon { } fn start_high_temperature_rule(&mut self) -> Result<(), Error> { - if !self.already_high_temp && self.temp_max > self.config.overheat_threshold { + if !self.already_high_temp && !self.already_high_usage && self.temp_max > self.config.overheat_threshold { self.logger.log( "Governor set to powersave because CPU temperature is high", logger::Severity::Log, @@ -312,14 +313,30 @@ impl Checker for Daemon { fn start_cpu_usage_rule(&mut self) -> Result<(), Error> { - if self.usage > 70.0 && self.last_below_cpu_usage_percent == None { + if self.usage > 70.0 && self.last_below_cpu_usage_percent.is_none() { self.last_below_cpu_usage_percent = Some(SystemTime::now()); } - self.logger.log(&format!("{:?}", self.last_below_cpu_usage_percent), logger::Severity::Log); + + match self.last_below_cpu_usage_percent { + Some(last) => { + if SystemTime::now().duration_since(last)?.as_secs() >= 15 { + self.logger.log( + &format!( + "Governor set to performance because cpu was over 70% overall usage for longer than 15 seconds", + ), + logger::Severity::Log, + ); + } + }, + None => {}, + } Ok(()) } fn end_cpu_usage_rule(&mut self) -> Result<(), Error> { + if self.usage < 70.0 && self.last_below_cpu_usage_percent.is_some() { + self.last_below_cpu_usage_percent = None; + } Ok(()) } @@ -630,6 +647,7 @@ pub fn daemon_init(settings: Settings, config: Config) -> Result already_closed: false, already_under_powersave_under_percent: false, already_high_temp: false, + already_high_cpu: false, last_below_cpu_usage_percent: None, graph: String::new(), grapher: Graph { freqs: vec![0.0] }, diff --git a/src/error.rs b/src/error.rs index dbb737f2..6c9353c2 100644 --- a/src/error.rs +++ b/src/error.rs @@ -5,6 +5,7 @@ https://github.com/JakeRoggenbuck/auto-clock-speed/issues/new/choose"; pub enum Error { IO(std::io::Error), + TimeError(std::time::SystemTimeError), Unknown, DivisionByZero, } @@ -15,6 +16,12 @@ impl From for Error { } } +impl From for Error { + fn from(e: std::time::SystemTimeError) -> Error { + Error::TimeError(e) + } +} + impl fmt::Debug for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "Something went wrong. Try running with sudo.\n") From 4124eda23318e32ca1ab04f1dfb737c72d12f3e7 Mon Sep 17 00:00:00 2001 From: Camerooooon Date: Sun, 29 May 2022 13:43:46 -0700 Subject: [PATCH 5/6] Do not set governor multiple times and also tell the user when governor returns to normal --- src/daemon.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/daemon.rs b/src/daemon.rs index 0054f2e8..72d1aa6a 100644 --- a/src/daemon.rs +++ b/src/daemon.rs @@ -88,7 +88,7 @@ pub struct Daemon { pub already_closed: bool, pub already_under_powersave_under_percent: bool, pub already_high_temp: bool, - pub already_high_cpu: bool, + pub already_high_usage: bool, pub last_below_cpu_usage_percent: Option, pub graph: String, pub grapher: Graph, @@ -319,13 +319,14 @@ impl Checker for Daemon { match self.last_below_cpu_usage_percent { Some(last) => { - if SystemTime::now().duration_since(last)?.as_secs() >= 15 { + if SystemTime::now().duration_since(last)?.as_secs() >= 15 && !self.already_high_usage { self.logger.log( &format!( "Governor set to performance because cpu was over 70% overall usage for longer than 15 seconds", ), logger::Severity::Log, ); + self.already_high_usage = true; } }, None => {}, @@ -336,6 +337,13 @@ impl Checker for Daemon { fn end_cpu_usage_rule(&mut self) -> Result<(), Error> { if self.usage < 70.0 && self.last_below_cpu_usage_percent.is_some() { self.last_below_cpu_usage_percent = None; + self.already_high_usage = false; + self.logger.log( + &format!( + "Governor returning to default because usage dropped below 70%", + ), + logger::Severity::Log, + ); } Ok(()) } @@ -647,7 +655,7 @@ pub fn daemon_init(settings: Settings, config: Config) -> Result already_closed: false, already_under_powersave_under_percent: false, already_high_temp: false, - already_high_cpu: false, + already_high_usage: false, last_below_cpu_usage_percent: None, graph: String::new(), grapher: Graph { freqs: vec![0.0] }, From b3d4eeb4a6df62de3ff5e817bb8c794f774bbc8c Mon Sep 17 00:00:00 2001 From: Camerooooon Date: Sun, 29 May 2022 13:46:45 -0700 Subject: [PATCH 6/6] Cargo fomat --- src/daemon.rs | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/daemon.rs b/src/daemon.rs index 5989c458..b13708ee 100644 --- a/src/daemon.rs +++ b/src/daemon.rs @@ -1,6 +1,6 @@ use std::convert::TryInto; +use std::time::SystemTime; use std::{thread, time}; -use std::time::{SystemTime}; use colored::*; use nix::unistd::Uid; @@ -51,7 +51,7 @@ pub trait Checker { // High Temperature Rule fn start_high_temperature_rule(&mut self) -> Result<(), Error>; fn end_high_temperature_rule(&mut self) -> Result<(), Error>; - + // High CPU Usage Rule fn start_cpu_usage_rule(&mut self) -> Result<(), Error>; fn end_cpu_usage_rule(&mut self) -> Result<(), Error>; @@ -163,7 +163,7 @@ fn print_turbo_status(cores: usize, no_animation: bool, term_width: usize, delay Err(e) => eprintln!("Could not check turbo\n{:?}", e), } } - + fn calculate_average_usage(cpus: &Vec) -> Result { let mut sum = 0.0; for cpu in cpus { @@ -215,7 +215,6 @@ impl Checker for Daemon { } fn end_charging_rule(&mut self) -> Result<(), Error> { - if !self.charging && self.already_charging { self.logger.log( "Governor set to powersave because battery is not charging", @@ -228,7 +227,10 @@ impl Checker for Daemon { } fn start_high_temperature_rule(&mut self) -> Result<(), Error> { - if !self.already_high_temp && !self.already_high_usage && self.temp_max > self.config.overheat_threshold { + if !self.already_high_temp + && !self.already_high_usage + && self.temp_max > self.config.overheat_threshold + { self.logger.log( "Governor set to powersave because CPU temperature is high", logger::Severity::Log, @@ -300,7 +302,6 @@ impl Checker for Daemon { } Ok(()) } - fn start_cpu_usage_rule(&mut self) -> Result<(), Error> { if self.usage > 70.0 && self.last_below_cpu_usage_percent.is_none() { @@ -309,7 +310,9 @@ impl Checker for Daemon { match self.last_below_cpu_usage_percent { Some(last) => { - if SystemTime::now().duration_since(last)?.as_secs() >= 15 && !self.already_high_usage { + if SystemTime::now().duration_since(last)?.as_secs() >= 15 + && !self.already_high_usage + { self.logger.log( &format!( "Governor set to performance because cpu was over 70% overall usage for longer than 15 seconds", @@ -318,20 +321,18 @@ impl Checker for Daemon { ); self.already_high_usage = true; } - }, - None => {}, + } + None => {} } Ok(()) } - + fn end_cpu_usage_rule(&mut self) -> Result<(), Error> { if self.usage < 70.0 && self.last_below_cpu_usage_percent.is_some() { self.last_below_cpu_usage_percent = None; self.already_high_usage = false; self.logger.log( - &format!( - "Governor returning to default because usage dropped below 70%", - ), + &format!("Governor returning to default because usage dropped below 70%",), logger::Severity::Log, ); }