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

engine: stronger validation for turbo auto settings thresholds

This commit is contained in:
NotAShelf 2025-05-18 04:35:12 +03:00
parent 4e7b2d405b
commit 15cdf22557
No known key found for this signature in database
GPG key ID: 29D95B64378DB4BF
2 changed files with 17 additions and 33 deletions

View file

@ -1,6 +1,7 @@
use crate::core::{GovernorOverrideMode, TurboSetting}; use crate::core::{GovernorOverrideMode, TurboSetting};
use crate::util::error::ControlError; use crate::util::error::ControlError;
use core::str; use core::str;
use log::debug;
use std::{fs, io, path::Path, string::ToString}; use std::{fs, io, path::Path, string::ToString};
pub type Result<T, E = ControlError> = std::result::Result<T, E>; pub type Result<T, E = ControlError> = std::result::Result<T, E>;
@ -212,44 +213,23 @@ fn get_available_governors() -> Result<Vec<String>> {
)) ))
} }
/// Controls CPU turbo boost behavior by writing to appropriate sysfs files.
///
/// # Parameters
///
/// * `setting` - The desired turbo boost setting:
/// - `TurboSetting::Always`: Forces turbo boost to be always enabled
/// - `TurboSetting::Never`: Forces turbo boost to be always disabled
/// - `TurboSetting::Auto`: Has two distinct behaviors depending on the context:
/// 1. When called directly from user commands: Resets turbo to system default (enabled)
/// 2. When used with `enable_auto_turbo=true` in config: Managed dynamically by the engine
///
/// # Turbo Auto Mode Explained
///
/// When `TurboSetting::Auto` is used:
/// - This function writes the same values as `TurboSetting::Always` to reset the hardware
/// to its default state (turbo enabled)
/// - The actual dynamic management happens at a higher level in the engine module
/// when `enable_auto_turbo=true`
/// - With `enable_auto_turbo=false`, the system's native turbo management takes over
///
///
/// # Returns
///
/// * `Result<()>` - Success or an error if the operation failed
pub fn set_turbo(setting: TurboSetting) -> Result<()> { pub fn set_turbo(setting: TurboSetting) -> Result<()> {
let value_pstate = match setting { let value_pstate = match setting {
TurboSetting::Always => "0", // no_turbo = 0 means turbo is enabled TurboSetting::Always => "0", // no_turbo = 0 means turbo is enabled
TurboSetting::Never => "1", // no_turbo = 1 means turbo is disabled TurboSetting::Never => "1", // no_turbo = 1 means turbo is disabled
// For Auto, we need to enable the hardware default (which is turbo enabled) // Auto mode is handled at the engine level, not directly at the sysfs level
// and we reset to the system default when explicitly set to Auto TurboSetting::Auto => {
TurboSetting::Auto => "0", // set to enabled (default hardware state) when Auto is requested debug!("Turbo Auto mode is managed by engine logic based on system conditions");
return Ok(());
}
}; };
let value_boost = match setting { let value_boost = match setting {
TurboSetting::Always => "1", // boost = 1 means turbo is enabled TurboSetting::Always => "1", // boost = 1 means turbo is enabled
TurboSetting::Never => "0", // boost = 0 means turbo is disabled TurboSetting::Never => "0", // boost = 0 means turbo is disabled
// For Auto, we need to enable the hardware default (which is turbo enabled) TurboSetting::Auto => {
// and we reset to the system default when explicitly set to Auto debug!("Turbo Auto mode is managed by engine logic based on system conditions");
TurboSetting::Auto => "1", // set to enabled (default hardware state) when Auto is requested return Ok(());
}
}; };
// AMD specific paths // AMD specific paths

View file

@ -434,10 +434,14 @@ fn manage_auto_turbo(
} }
fn validate_turbo_auto_settings(settings: &TurboAutoSettings) -> Result<(), EngineError> { fn validate_turbo_auto_settings(settings: &TurboAutoSettings) -> Result<(), EngineError> {
// Validate load thresholds // Validate load thresholds (0-100 % and high > low)
if settings.load_threshold_high <= settings.load_threshold_low { if settings.load_threshold_high <= settings.load_threshold_low
|| settings.load_threshold_high > 100.0
|| settings.load_threshold_low < 0.0
|| settings.load_threshold_low > 100.0
{
return Err(EngineError::ConfigurationError( return Err(EngineError::ConfigurationError(
"Invalid turbo auto settings: high threshold must be greater than low threshold" "Invalid turbo auto settings: load thresholds must be in 0-100% and high > low"
.to_string(), .to_string(),
)); ));
} }