Skip to content

Commit

Permalink
Merge pull request #319 from Camerooooon/cpu-threshold-config
Browse files Browse the repository at this point in the history
Cpu threshold config
  • Loading branch information
JakeRoggenbuck authored Jun 12, 2022
2 parents 32bba25 + 8bfa549 commit b902af2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
12 changes: 10 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -40,13 +41,15 @@ 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<State>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct SafeConfig {
pub powersave_under: Option<i8>,
pub overheat_threshold: Option<i8>,
pub high_cpu_threshold: Option<i8>,
pub active_rules: Option<Vec<String>>,
}

Expand Down Expand Up @@ -79,6 +82,10 @@ impl SafeFillConfig for SafeConfig {
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();
for rule in self.active_rules.clone().unwrap() {
Expand All @@ -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,
)
}
}
Expand All @@ -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,
});

Expand Down
6 changes: 4 additions & 2 deletions src/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,13 @@ 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;
}

Expand Down

0 comments on commit b902af2

Please sign in to comment.