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

Cpu threshold config #319

Merged
merged 10 commits into from
Jun 12, 2022
3 changes: 1 addition & 2 deletions acs.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# acs.toml
powersave_under = 20
overheat_threshold = 80
ignore_power = false
ignore_lid = false
active_rules = [ "battery_percent_rule", "lid_open_rule", "ac_charging_rule", "cpu_usage_rule" ]
45 changes: 30 additions & 15 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use super::daemon::State;
use super::warn_user;
use serde::{Deserialize, Serialize};
use std::fmt;
Expand Down Expand Up @@ -26,25 +27,30 @@ pub fn default_config() -> Config {
Config {
powersave_under: 20,
overheat_threshold: 80,
ignore_power: false,
ignore_lid: false,
high_cpu_threshold: 50,
active_rules: vec![
State::BatteryLow,
State::LidClosed,
State::Charging,
State::CpuUsageHigh,
],
}
}

#[derive(Debug, Deserialize, Serialize)]
#[derive(Debug)]
pub struct Config {
pub powersave_under: i8,
pub overheat_threshold: i8,
pub ignore_power: bool,
pub ignore_lid: bool,
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 ignore_power: Option<bool>,
pub ignore_lid: Option<bool>,
pub high_cpu_threshold: Option<i8>,
pub active_rules: Option<Vec<String>>,
}

trait SafeFillConfig {
Expand Down Expand Up @@ -76,12 +82,21 @@ impl SafeFillConfig for SafeConfig {
base.overheat_threshold = self.overheat_threshold.unwrap();
}

if self.ignore_power.is_some() {
base.ignore_power = self.ignore_power.unwrap();
if self.high_cpu_threshold.is_some() {
base.high_cpu_threshold = self.high_cpu_threshold.unwrap();
}

if self.ignore_lid.is_some() {
base.ignore_lid = self.ignore_lid.unwrap();
if self.active_rules.is_some() {
base.active_rules.clear();
for rule in self.active_rules.clone().unwrap() {
base.active_rules.push(match rule.as_str() {
"battery_percent_rule" => State::BatteryLow,
"lid_open_rule" => State::LidClosed,
"ac_charging_rule" => State::Charging,
"cpu_usage_rule" => State::CpuUsageHigh,
_ => State::Unknown,
});
}
}

return base;
Expand All @@ -94,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 = {}\nignore_power = {}\nignore_lid = {} ",
self.powersave_under, self.overheat_threshold, self.ignore_power, self.ignore_lid,
"powersave_under = {}\noverheat_threshold = {}\nhigh_cpu_threshold = {}\nacive_rules = {:?}",
self.powersave_under, self.overheat_threshold, self.high_cpu_threshold, self.active_rules,
)
}
}
Expand All @@ -114,8 +129,8 @@ fn parse_as_toml(config: String) -> Config {
toml::from_str(config.as_str()).unwrap_or_else(|_| SafeConfig {
powersave_under: None,
overheat_threshold: None,
ignore_power: None,
ignore_lid: None,
high_cpu_threshold: None,
active_rules: None,
});

safe_config.safe_fill_config()
Expand Down
56 changes: 33 additions & 23 deletions src/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use super::Error;
use crate::display::print_turbo_animation;
use crate::warn_user;

#[derive(Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq)]
pub enum State {
Normal,
BatteryLow,
Expand Down Expand Up @@ -177,38 +177,48 @@ impl Checker for Daemon {
fn run_state_machine(&mut self) -> State {
let mut state = State::Normal;

if self.usage > 70.0 && self.last_below_cpu_usage_percent.is_none() {
self.last_below_cpu_usage_percent = Some(SystemTime::now());
}
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()
{
self.last_below_cpu_usage_percent = Some(SystemTime::now());
}

if self.usage <= 70.0 {
self.last_below_cpu_usage_percent = None;
}
if self.usage <= self.config.high_cpu_threshold.into() {
self.last_below_cpu_usage_percent = None;
}

match self.last_below_cpu_usage_percent {
Some(last) => {
if SystemTime::now()
.duration_since(last)
.expect("Could not compare times")
.as_secs()
>= 15
{
state = State::CpuUsageHigh;
match self.last_below_cpu_usage_percent {
Some(last) => {
if SystemTime::now()
.duration_since(last)
.expect("Could not compare times")
.as_secs()
>= 15
{
state = State::CpuUsageHigh;
}
}
None => {}
}
None => {}
}

if self.lid_state == LidState::Closed {
state = State::LidClosed;
if self.config.active_rules.contains(&State::LidClosed) {
if self.lid_state == LidState::Closed {
state = State::LidClosed;
}
}

if self.charging {
state = State::Charging;
if self.config.active_rules.contains(&State::Charging) {
if self.charging {
state = State::Charging;
}
}

if self.charge < self.config.powersave_under {
state = State::BatteryLow;
if self.config.active_rules.contains(&State::BatteryLow) {
if self.charge < self.config.powersave_under {
state = State::BatteryLow;
}
}

state
Expand Down