Skip to content

Commit

Permalink
refactor: 移除基于cycles计算的线程跟踪,改为基于cpu时间比例
Browse files Browse the repository at this point in the history
  • Loading branch information
shadow3aaa committed Jan 24, 2025
1 parent da357fe commit de0b92e
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 345 deletions.
60 changes: 0 additions & 60 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ mlua = { version = "0.10.2", features = ["luajit", "vendored", "error-send"] }
frame-analyzer = "0.3.3"
dumpsys-rs = { git = "https://github.com/shadow3aaa/dumpsys-rs" }
mimalloc = "0.1.43"
perf-event2 = "0.7.4"
num_cpus = "1.16.0"

[build-dependencies]
Expand Down
13 changes: 0 additions & 13 deletions src/cpu_common/cpu_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ pub struct Info {
path: PathBuf,
pub cur_freq: isize,
pub freqs: Vec<isize>,
pub cpus: Vec<i32>,
}

impl Info {
Expand All @@ -47,17 +46,6 @@ impl Info {
.parse::<i32>()
.context("Failed to parse policy")?;

let cpus = fs::read_to_string(path.join("affected_cpus"))
.context("Failed to read affected_cpus")
.unwrap()
.split_whitespace()
.map(|c| {
c.parse::<i32>()
.context("Failed to parse affected_cpus")
.unwrap()
})
.collect();

let freqs_content = fs::read_to_string(path.join("scaling_available_frequencies"))
.context("Failed to read frequencies")?;
let mut freqs: Vec<isize> = freqs_content
Expand All @@ -71,7 +59,6 @@ impl Info {
path,
cur_freq: *freqs.last().context("No frequencies available")?,
freqs,
cpus,
})
}

Expand Down
63 changes: 11 additions & 52 deletions src/cpu_common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,13 @@

mod cpu_info;
pub mod extra_policy;
mod thread_tracker;
mod process_monitor;

use std::{
cmp,
collections::HashMap,
fs,
path::Path,
sync::{
atomic::{AtomicBool, AtomicI32, Ordering},
mpsc::{self, SyncSender},
Arc, OnceLock,
},
sync::{atomic::AtomicBool, OnceLock},
thread,
time::Duration,
};
Expand All @@ -38,7 +33,7 @@ use anyhow::{Context, Result};
use log::debug;
use log::warn;
use parking_lot::Mutex;
use thread_tracker::thread_tracker;
use process_monitor::ProcessMonitor;

use crate::{
api::{trigger_init_cpu_freq, trigger_reset_cpu_freq},
Expand All @@ -56,8 +51,7 @@ pub struct Controller {
max_freq: isize,
cpu_infos: Vec<Info>,
file_handler: FileHandler,
thread_map: Arc<HashMap<Vec<i32>, AtomicI32>>,
target_pid_sender: SyncSender<Option<i32>>,
process_monitor: ProcessMonitor,
}

impl Controller {
Expand Down Expand Up @@ -88,30 +82,11 @@ impl Controller {
.copied()
.unwrap_or(0);

let thread_map: Arc<HashMap<_, _>> = Arc::new(
cpu_infos
.iter()
.map(|info| (info.cpus.clone(), AtomicI32::new(0)))
.collect(),
);
let (target_pid_sender, target_pid_receiver) = mpsc::sync_channel(0);
{
let thread_map = thread_map.clone();

thread::Builder::new()
.name("UtilTracker".to_string())
.spawn(move || {
thread_tracker(&thread_map, &target_pid_receiver);
})
.unwrap();
}

Ok(Self {
max_freq,
cpu_infos,
file_handler: FileHandler::new(),
thread_map,
target_pid_sender,
process_monitor: ProcessMonitor::new(),
})
}

Expand Down Expand Up @@ -161,13 +136,13 @@ impl Controller {
pub fn init_game(&mut self, pid: i32, extension: &Extension) {
trigger_init_cpu_freq(extension);
self.set_all_cpu_freq(self.max_freq);
self.target_pid_sender.send(Some(pid)).unwrap();
self.process_monitor.set_pid(Some(pid));
}

pub fn init_default(&mut self, extension: &Extension) {
trigger_reset_cpu_freq(extension);
self.reset_all_cpu_freq();
self.target_pid_sender.send(None).unwrap();
self.process_monitor.set_pid(None);
}

pub fn fas_update_freq(&mut self, control: isize, is_janked: bool) {
Expand Down Expand Up @@ -210,13 +185,8 @@ impl Controller {
self.cpu_infos
.iter()
.map(|cpu| {
let util = f64::from(
self.thread_map
.get(&cpu.cpus)
.unwrap()
.load(Ordering::Acquire),
) / (cpu.read_freq() * 1000) as f64;
let util_tracking_sugg_freq = (cpu.read_freq() as f64 * util / 0.2) as isize; // min_util: 20%
let util = self.process_monitor.util_max();
let util_tracking_sugg_freq = (cpu.read_freq() as f64 * util / 0.50) as isize; // min_util: 50%

#[cfg(debug_assertions)]
debug!(
Expand Down Expand Up @@ -372,19 +342,8 @@ impl Controller {
}
}

pub fn util_max(&mut self) -> f64 {
self.cpu_infos
.iter_mut()
.map(|cpu| {
f64::from(
self.thread_map
.get(&cpu.cpus)
.unwrap()
.load(Ordering::Acquire),
) / (cpu.read_freq() * 1000) as f64
})
.max_by(|a, b| a.partial_cmp(b).unwrap_or(cmp::Ordering::Equal))
.unwrap_or_default()
pub fn util_max(&self) -> f64 {
self.process_monitor.util_max()
}
}

Expand Down
Loading

0 comments on commit de0b92e

Please sign in to comment.