Skip to content

Commit

Permalink
fix: 废弃OFFSET_MAP以修复offset和新策略不兼容的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
shadow3aaa committed Nov 27, 2024
1 parent 3bc3a75 commit ffaca27
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 35 deletions.
19 changes: 6 additions & 13 deletions src/cpu_common/cpu_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use std::{
use anyhow::{Context, Result};
use sysinfo::{Cpu, CpuRefreshKind, RefreshKind, System};

use super::{IGNORE_MAP, OFFSET_MAP};
use super::IGNORE_MAP;
use crate::file_handler::FileHandler;

#[derive(Debug)]
Expand Down Expand Up @@ -80,19 +80,12 @@ impl Info {
}

pub fn write_freq(&mut self, freq: isize, file_handler: &mut FileHandler) -> Result<()> {
let offset = OFFSET_MAP
.get()
.context("OFFSET_MAP not initialized")?
.get(&self.policy)
.context("Policy offset not found")?
.load(Ordering::Acquire);

let min_freq = *self.freqs.first().context("No frequencies available")?;
let max_freq = *self.freqs.last().context("No frequencies available")?;

let adjusted_freq = freq.saturating_add(offset).clamp(min_freq, max_freq);
self.cur_freq = adjusted_freq;
let adjusted_freq = adjusted_freq.to_string();
let freq = freq.clamp(min_freq, max_freq);
self.cur_freq = freq;
let freq = freq.to_string();

if !IGNORE_MAP
.get()
Expand All @@ -101,8 +94,8 @@ impl Info {
.context("Policy ignore flag not found")?
.load(Ordering::Acquire)
{
file_handler.write_with_workround(self.max_freq_path(), &adjusted_freq)?;
file_handler.write_with_workround(self.min_freq_path(), &adjusted_freq)?;
file_handler.write_with_workround(self.max_freq_path(), &freq)?;
file_handler.write_with_workround(self.min_freq_path(), &freq)?;
}
Ok(())
}
Expand Down
12 changes: 1 addition & 11 deletions src/cpu_common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ use std::{
collections::HashMap,
fs,
path::Path,
sync::{
atomic::{AtomicBool, AtomicIsize},
OnceLock,
},
sync::{atomic::AtomicBool, OnceLock},
thread,
time::Duration,
};
Expand All @@ -39,7 +36,6 @@ use crate::{
Extension,
};

pub static OFFSET_MAP: OnceLock<HashMap<i32, AtomicIsize>> = OnceLock::new();
pub static IGNORE_MAP: OnceLock<HashMap<i32, AtomicBool>> = OnceLock::new();

#[derive(Debug)]
Expand All @@ -54,12 +50,6 @@ impl Controller {
let mut cpu_infos = Self::load_cpu_infos()?;
cpu_infos.sort_by_key(|cpu| cpu.policy);

OFFSET_MAP.get_or_init(|| {
cpu_infos
.iter()
.map(|cpu| (cpu.policy, AtomicIsize::new(0)))
.collect()
});
IGNORE_MAP.get_or_init(|| {
cpu_infos
.iter()
Expand Down
21 changes: 11 additions & 10 deletions src/framework/extension/api/helper_funs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,19 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::sync::atomic::Ordering;
use std::sync::atomic::{AtomicBool, Ordering};

use crate::cpu_common::{IGNORE_MAP, OFFSET_MAP};
use log::error;

pub fn set_policy_freq_offset(policy: i32, offset: isize) -> mlua::Result<()> {
OFFSET_MAP
.get()
.unwrap()
.get(&policy)
.ok_or_else(|| mlua::Error::runtime("Policy Not Found!"))?
.store(offset, Ordering::Release);
Ok(())
use crate::cpu_common::IGNORE_MAP;

static OFFSET_DEPRECATED_MSG_PRINTED: AtomicBool = AtomicBool::new(false);

pub fn set_policy_freq_offset(_: i32, _: isize) {
if !OFFSET_DEPRECATED_MSG_PRINTED.load(Ordering::Acquire) {
OFFSET_DEPRECATED_MSG_PRINTED.store(true, Ordering::Release);
error!("'set_policy_freq_offset' is deprecated and does nothing since v4.0.0.");
}
}

pub fn set_ignore_policy(policy: i32, val: bool) -> mlua::Result<()> {
Expand Down
2 changes: 1 addition & 1 deletion src/framework/extension/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ fn load_extensions() -> Result<ExtensionMap> {
lua.globals().set(
"set_policy_freq_offset",
lua.create_function(|_, (policy, offset): (i32, isize)| {
helper_funs::set_policy_freq_offset(policy, offset)?;
helper_funs::set_policy_freq_offset(policy, offset);
Ok(())
})?,
)?;
Expand Down

0 comments on commit ffaca27

Please sign in to comment.