Skip to content

Commit

Permalink
refactor: 使用file_handler保存File减少开销
Browse files Browse the repository at this point in the history
  • Loading branch information
shadow3aaa committed Jun 29, 2024
1 parent 76eee9b commit eae7d69
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 28 deletions.
14 changes: 7 additions & 7 deletions src/cpu_common/cpu_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{fs, path::PathBuf};

use anyhow::Result;

use super::unlock_write;
use super::file_handler::FileHandler;

#[derive(Debug)]
pub struct Info {
Expand Down Expand Up @@ -36,25 +36,25 @@ impl Info {
})
}

pub fn write_freq(&self, freq: isize) -> Result<()> {
pub fn write_freq(&self, freq: isize, file_handler: &mut FileHandler) -> Result<()> {
let freq = freq.to_string();
let max_freq_path = self.max_freq_path();
unlock_write(max_freq_path, &freq)?;
file_handler.write(max_freq_path, &freq)?;

if self.policy != 0 {
let min_freq_path = self.min_freq_path();
unlock_write(min_freq_path, &freq)?;
file_handler.write(min_freq_path, &freq)?;
}

Ok(())
}

pub fn reset_freq(&self) -> Result<()> {
pub fn reset_freq(&self, file_handler: &mut FileHandler) -> Result<()> {
let max_freq_path = self.max_freq_path();
let min_freq_path = self.min_freq_path();

unlock_write(max_freq_path, self.freqs.last().unwrap().to_string())?;
unlock_write(min_freq_path, self.freqs.first().unwrap().to_string())?;
file_handler.write(max_freq_path, self.freqs.last().unwrap().to_string())?;
file_handler.write(min_freq_path, self.freqs.first().unwrap().to_string())?;

Ok(())
}
Expand Down
53 changes: 53 additions & 0 deletions src/cpu_common/file_handler.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use std::{
collections::{hash_map::Entry, HashMap},
fs::File,
io::prelude::*,
path::{Path, PathBuf},
};

use anyhow::Result;

#[derive(Debug)]
pub struct FileHandler {
files: HashMap<PathBuf, File>,
}

impl FileHandler {
pub fn new() -> Self {
Self {
files: HashMap::new(),
}
}

pub fn read_to_string(&mut self, path: impl AsRef<Path>) -> Result<String> {
let mut string = String::new();
match self.files.entry(path.as_ref().to_path_buf()) {
Entry::Occupied(entry) => {
let mut string = String::new();
entry.get().read_to_string(&mut string)?;
}
Entry::Vacant(entry) => {
let mut file = File::open(path.as_ref())?;
file.read_to_string(&mut string)?;
entry.insert(file);
}
}

Ok(string)
}

pub fn write(&mut self, path: impl AsRef<Path>, content: impl AsRef<[u8]>) -> Result<()> {
match self.files.entry(path.as_ref().to_path_buf()) {
Entry::Occupied(mut entry) => {
entry.get_mut().write_all(content.as_ref())?;
}
Entry::Vacant(entry) => {
let mut file = File::create(path)?;
file.write_all(content.as_ref())?;
entry.insert(file);
}
}

Ok(())
}
}
43 changes: 22 additions & 21 deletions src/cpu_common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
mod cpu_info;
mod file_handler;

use std::{fs, os::unix::fs::PermissionsExt, path::Path, time::Duration};
use std::{fs, path::Path, time::Duration};

use anyhow::Result;

use cpu_info::Info;
use file_handler::FileHandler;
#[cfg(debug_assertions)]
use log::debug;
use log::error;
Expand All @@ -19,6 +21,7 @@ pub struct Controller {
min_freq: isize,
policy_freq: isize,
cpu_infos: Vec<Info>,
file_handler: FileHandler,
}

impl Controller {
Expand Down Expand Up @@ -59,6 +62,7 @@ impl Controller {
min_freq,
policy_freq: max_freq,
cpu_infos,
file_handler: FileHandler::new(),
})
}

Expand All @@ -68,11 +72,13 @@ impl Controller {

let walt_extra = Path::new("/proc/sys/walt/sched_fmax_cap");
if walt_extra.exists() {
let _ = unlock_write(walt_extra, self.max_freq.to_string());
let _ = self
.file_handler
.write(walt_extra, self.max_freq.to_string());
}

for cpu in &self.cpu_infos {
cpu.write_freq(self.max_freq)
cpu.write_freq(self.max_freq, &mut self.file_handler)
.unwrap_or_else(|e| error!("{e:?}"));
}
}
Expand All @@ -83,11 +89,14 @@ impl Controller {

let walt_extra = Path::new("/proc/sys/walt/sched_fmax_cap");
if walt_extra.exists() {
let _ = unlock_write(walt_extra, self.max_freq.to_string());
let _ = self
.file_handler
.write(walt_extra, self.max_freq.to_string());
}

for cpu in &self.cpu_infos {
cpu.reset_freq().unwrap_or_else(|e| error!("{e:?}"));
cpu.reset_freq(&mut self.file_handler)
.unwrap_or_else(|e| error!("{e:?}"));
}
}

Expand All @@ -99,34 +108,26 @@ impl Controller {

let walt_extra = Path::new("/proc/sys/walt/sched_fmax_cap");
if walt_extra.exists() {
let _ = unlock_write(walt_extra, self.policy_freq.to_string());
let _ = self
.file_handler
.write(walt_extra, self.policy_freq.to_string());
}

for cpu in &self.cpu_infos {
cpu.write_freq(self.policy_freq)
cpu.write_freq(self.policy_freq, &mut self.file_handler)
.unwrap_or_else(|e| error!("{e:?}"));
}
}

pub fn scale_factor(target_fps: u32, frame: Duration, target: Duration) -> f64 {
if frame > target {
let factor_a = (frame - target).as_nanos() as f64 / target.as_nanos() as f64;
let factor_b = 120.0 / target_fps as f64;
factor_a * f64::from(factor_b)
let factor_b = 120.0 / f64::from(target_fps);
factor_a * factor_b
} else {
let factor_a = (target - frame).as_nanos() as f64 / target.as_nanos() as f64;
let factor_b = 120.0 / target_fps as f64;
factor_a * f64::from(factor_b) * -1.0
let factor_b = 120.0 / f64::from(target_fps);
factor_a * factor_b * -1.0
}
}
}

fn unlock_write<P, C>(path: P, contents: C) -> Result<()>
where
P: AsRef<Path>,
C: AsRef<[u8]>,
{
let _ = fs::set_permissions(path.as_ref(), PermissionsExt::from_mode(0o644));
fs::write(path, contents)?;
Ok(())
}

0 comments on commit eae7d69

Please sign in to comment.