1
Fork 0
mirror of https://github.com/RGBCube/superfreq synced 2025-07-27 17:07:44 +00:00

engine: allow configuring initial turbo state

This commit is contained in:
NotAShelf 2025-05-18 02:25:35 +03:00
parent a084f4e7b1
commit e6d8f8c2da
No known key found for this signature in database
GPG key ID: 29D95B64378DB4BF
2 changed files with 44 additions and 8 deletions

View file

@ -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,
}
}
}

View file

@ -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 {
BATTERY_TURBO_HYSTERESIS.with(TurboHysteresis::get_previous_state)
// Initialize with the configured initial state and return it
Some(h.initialize_with(turbo_settings.initial_turbo_state))
}
})
} else {
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
}
};