From 488e20da4d996d9d35e878af7794d5e458a04ccf Mon Sep 17 00:00:00 2001 From: Camerooooon Date: Thu, 9 Jun 2022 08:42:23 -0700 Subject: [PATCH 1/3] Load high cpu threshold from config --- src/config.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/config.rs b/src/config.rs index 9ee6ecbc..a8e21f98 100644 --- a/src/config.rs +++ b/src/config.rs @@ -27,6 +27,7 @@ pub fn default_config() -> Config { Config { powersave_under: 20, overheat_threshold: 80, + high_cpu_threshold: 50, active_rules: vec![ State::BatteryLow, State::LidClosed, @@ -40,6 +41,7 @@ pub fn default_config() -> Config { pub struct Config { pub powersave_under: i8, pub overheat_threshold: i8, + pub high_cpu_threshold: i8, pub active_rules: Vec, } @@ -47,6 +49,7 @@ pub struct Config { pub struct SafeConfig { pub powersave_under: Option, pub overheat_threshold: Option, + pub high_cpu_threshold: Option, pub active_rules: Option>, } @@ -78,6 +81,10 @@ impl SafeFillConfig for SafeConfig { if self.overheat_threshold.is_some() { base.overheat_threshold = self.overheat_threshold.unwrap(); } + + if self.high_cpu_threshold.is_some() { + base.high_cpu_threshold = self.high_cpu_threshold.unwrap(); + } if self.active_rules.is_some() { base.active_rules.clear(); @@ -102,8 +109,8 @@ impl fmt::Display for Config { // config iterable. This would also make safe_fill_config a lot easier as well. write!( f, - "powersave_under = {}\noverheat_threshold = {}\nacive_rules = {:?}", - self.powersave_under, self.overheat_threshold, self.active_rules, + "powersave_under = {}\noverheat_threshold = {}\nhigh_cpu_threshold = {}\nacive_rules = {:?}", + self.powersave_under, self.overheat_threshold, self.high_cpu_threshold, self.active_rules, ) } } @@ -122,6 +129,7 @@ fn parse_as_toml(config: String) -> Config { toml::from_str(config.as_str()).unwrap_or_else(|_| SafeConfig { powersave_under: None, overheat_threshold: None, + high_cpu_threshold: None, active_rules: None, }); From abd9fb6580a761ec4fea49407fb84895ada35094 Mon Sep 17 00:00:00 2001 From: Camerooooon Date: Thu, 9 Jun 2022 08:53:36 -0700 Subject: [PATCH 2/3] Usage cpu threshold for determining daemon state --- src/daemon.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/daemon.rs b/src/daemon.rs index 22d75ac6..c5af1df6 100644 --- a/src/daemon.rs +++ b/src/daemon.rs @@ -178,11 +178,11 @@ impl Checker for Daemon { let mut state = State::Normal; if self.config.active_rules.contains(&State::CpuUsageHigh) { - if self.usage > 70.0 && self.last_below_cpu_usage_percent.is_none() { + if self.usage > self.config.high_cpu_threshold.into() && self.last_below_cpu_usage_percent.is_none() { self.last_below_cpu_usage_percent = Some(SystemTime::now()); } - if self.usage <= 70.0 { + if self.usage <= self.config.high_cpu_threshold.into() { self.last_below_cpu_usage_percent = None; } From 8bfa549fa83f4c22e4c7fd5db0c64face1b60c01 Mon Sep 17 00:00:00 2001 From: Camerooooon Date: Thu, 9 Jun 2022 08:53:49 -0700 Subject: [PATCH 3/3] Cargo format --- src/config.rs | 2 +- src/daemon.rs | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/config.rs b/src/config.rs index a8e21f98..538cff9b 100644 --- a/src/config.rs +++ b/src/config.rs @@ -81,7 +81,7 @@ impl SafeFillConfig for SafeConfig { if self.overheat_threshold.is_some() { base.overheat_threshold = self.overheat_threshold.unwrap(); } - + if self.high_cpu_threshold.is_some() { base.high_cpu_threshold = self.high_cpu_threshold.unwrap(); } diff --git a/src/daemon.rs b/src/daemon.rs index c5af1df6..dea263da 100644 --- a/src/daemon.rs +++ b/src/daemon.rs @@ -178,7 +178,9 @@ impl Checker for Daemon { let mut state = State::Normal; if self.config.active_rules.contains(&State::CpuUsageHigh) { - if self.usage > self.config.high_cpu_threshold.into() && self.last_below_cpu_usage_percent.is_none() { + if self.usage > self.config.high_cpu_threshold.into() + && self.last_below_cpu_usage_percent.is_none() + { self.last_below_cpu_usage_percent = Some(SystemTime::now()); }