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:
parent
4e7b2d405b
commit
15cdf22557
2 changed files with 17 additions and 33 deletions
40
src/cpu.rs
40
src/cpu.rs
|
@ -1,6 +1,7 @@
|
|||
use crate::core::{GovernorOverrideMode, TurboSetting};
|
||||
use crate::util::error::ControlError;
|
||||
use core::str;
|
||||
use log::debug;
|
||||
use std::{fs, io, path::Path, string::ToString};
|
||||
|
||||
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<()> {
|
||||
let value_pstate = match setting {
|
||||
TurboSetting::Always => "0", // no_turbo = 0 means turbo is enabled
|
||||
TurboSetting::Never => "1", // no_turbo = 1 means turbo is disabled
|
||||
// For Auto, we need to enable the hardware default (which is turbo enabled)
|
||||
// and we reset to the system default when explicitly set to Auto
|
||||
TurboSetting::Auto => "0", // set to enabled (default hardware state) when Auto is requested
|
||||
// Auto mode is handled at the engine level, not directly at the sysfs level
|
||||
TurboSetting::Auto => {
|
||||
debug!("Turbo Auto mode is managed by engine logic based on system conditions");
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
let value_boost = match setting {
|
||||
TurboSetting::Always => "1", // boost = 1 means turbo is enabled
|
||||
TurboSetting::Never => "0", // boost = 0 means turbo is disabled
|
||||
// For Auto, we need to enable the hardware default (which is turbo enabled)
|
||||
// and we reset to the system default when explicitly set to Auto
|
||||
TurboSetting::Auto => "1", // set to enabled (default hardware state) when Auto is requested
|
||||
TurboSetting::Auto => {
|
||||
debug!("Turbo Auto mode is managed by engine logic based on system conditions");
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
|
||||
// AMD specific paths
|
||||
|
|
|
@ -434,10 +434,14 @@ fn manage_auto_turbo(
|
|||
}
|
||||
|
||||
fn validate_turbo_auto_settings(settings: &TurboAutoSettings) -> Result<(), EngineError> {
|
||||
// Validate load thresholds
|
||||
if settings.load_threshold_high <= settings.load_threshold_low {
|
||||
// Validate load thresholds (0-100 % and high > 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(
|
||||
"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(),
|
||||
));
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue