From e6d8f8c2dad6a4fab1c02b98e604e56e724b11c6 Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Sun, 18 May 2025 02:25:35 +0300 Subject: [PATCH] engine: allow configuring initial turbo state --- src/config/types.rs | 7 +++++++ src/engine.rs | 45 +++++++++++++++++++++++++++++++++++++-------- 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/src/config/types.rs b/src/config/types.rs index 83d268e..b64064e 100644 --- a/src/config/types.rs +++ b/src/config/types.rs @@ -171,12 +171,15 @@ pub struct TurboAutoSettings { pub load_threshold_low: f32, #[serde(default = "default_temp_threshold_high")] pub temp_threshold_high: f32, + #[serde(default = "default_initial_turbo_state")] + pub initial_turbo_state: bool, } // Default thresholds for Auto turbo mode pub const DEFAULT_LOAD_THRESHOLD_HIGH: f32 = 70.0; // enable turbo if load is above this pub const DEFAULT_LOAD_THRESHOLD_LOW: f32 = 30.0; // disable turbo if load is below this pub const DEFAULT_TEMP_THRESHOLD_HIGH: f32 = 75.0; // disable turbo if temperature is above this +pub const DEFAULT_INITIAL_TURBO_STATE: bool = false; // by default, start with turbo disabled const fn default_load_threshold_high() -> f32 { DEFAULT_LOAD_THRESHOLD_HIGH @@ -187,6 +190,9 @@ const fn default_load_threshold_low() -> f32 { const fn default_temp_threshold_high() -> f32 { DEFAULT_TEMP_THRESHOLD_HIGH } +const fn default_initial_turbo_state() -> bool { + DEFAULT_INITIAL_TURBO_STATE +} impl Default for TurboAutoSettings { fn default() -> Self { @@ -194,6 +200,7 @@ impl Default for TurboAutoSettings { load_threshold_high: DEFAULT_LOAD_THRESHOLD_HIGH, load_threshold_low: DEFAULT_LOAD_THRESHOLD_LOW, temp_threshold_high: DEFAULT_TEMP_THRESHOLD_HIGH, + initial_turbo_state: DEFAULT_INITIAL_TURBO_STATE, } } } diff --git a/src/engine.rs b/src/engine.rs index 91bc5aa..c545970 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -33,6 +33,16 @@ impl TurboHysteresis { } } + /// Initialize the state with a specific value if not already initialized + fn initialize_with(&self, initial_state: bool) -> bool { + if !self.initialized.load(Ordering::Acquire) { + self.previous_state.store(initial_state, Ordering::Release); + self.initialized.store(true, Ordering::Release); + return initial_state; + } + self.previous_state.load(Ordering::Acquire) + } + /// Update the turbo state for hysteresis fn update_state(&self, new_state: bool) { self.previous_state.store(new_state, Ordering::Release); @@ -257,15 +267,27 @@ fn manage_auto_turbo(report: &SystemReport, config: &ProfileConfig) -> Result<() }; // Use the appropriate hysteresis object based on whether we're on battery or AC power - // We don't have direct access to the AppConfig here, so we need to make a determination - // based on other factors like whether turbo_auto_settings are more conservative (typically battery profile) - // or by checking if temperature thresholds are lower (typically battery profile) let is_on_ac = report.batteries.iter().any(|b| b.ac_connected); + // Get the previous state or initialize with the configured initial state let previous_turbo_enabled = if is_on_ac { - CHARGER_TURBO_HYSTERESIS.with(TurboHysteresis::get_previous_state) + CHARGER_TURBO_HYSTERESIS.with(|h| { + if let Some(state) = h.get_previous_state() { + Some(state) + } else { + // Initialize with the configured initial state and return it + Some(h.initialize_with(turbo_settings.initial_turbo_state)) + } + }) } else { - BATTERY_TURBO_HYSTERESIS.with(TurboHysteresis::get_previous_state) + BATTERY_TURBO_HYSTERESIS.with(|h| { + if let Some(state) = h.get_previous_state() { + Some(state) + } else { + // Initialize with the configured initial state and return it + Some(h.initialize_with(turbo_settings.initial_turbo_state)) + } + }) }; // Decision logic for enabling/disabling turbo with hysteresis @@ -306,10 +328,17 @@ fn manage_auto_turbo(report: &SystemReport, config: &ProfileConfig) -> Result<() ); prev_state } - // In indeterminate states or unknown previous state, default to disabled + // In indeterminate states or unknown previous state, use the configured initial state _ => { - info!("Auto Turbo: Disabled (default for indeterminate state)"); - false + info!( + "Auto Turbo: Using configured initial state ({})", + if turbo_settings.initial_turbo_state { + "enabled" + } else { + "disabled" + } + ); + turbo_settings.initial_turbo_state } };