diff --git a/Cargo.lock b/Cargo.lock index 3ba7e231..a5a5b6f9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -334,18 +334,6 @@ dependencies = [ "windows-sys 0.52.0", ] -[[package]] -name = "fallible-iterator" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2acce4a10f12dc2fb14a218589d4f1f62ef011b2d0cc4b3cb1bba8e94da14649" - -[[package]] -name = "fallible-streaming-iterator" -version = "0.1.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7360491ce676a36bf9bb3c56c1aa791658183a54d2744120f27285738d90465a" - [[package]] name = "fas-rs" version = "3.7.0" @@ -364,7 +352,6 @@ dependencies = [ "parking_lot", "quick-xml", "rand", - "rusqlite", "serde", "serde_json", "sys-mount", @@ -449,15 +436,6 @@ version = "0.15.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3a9bfc1af68b1726ea47d3d5109de126281def866b33970e10fbab11b5dafab3" -[[package]] -name = "hashlink" -version = "0.9.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ba4ff7128dee98c7dc9794b6a411377e1404dba1c97deb8d1a55297bd25d8af" -dependencies = [ - "hashbrown 0.14.5", -] - [[package]] name = "hermit-abi" version = "0.3.9" @@ -588,17 +566,6 @@ dependencies = [ "libc", ] -[[package]] -name = "libsqlite3-sys" -version = "0.30.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2e99fb7a497b1e3339bc746195567ed8d3e24945ecd636e3619d20b9de9e9149" -dependencies = [ - "cc", - "pkg-config", - "vcpkg", -] - [[package]] name = "likely_stable" version = "0.1.2" @@ -964,20 +931,6 @@ version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" -[[package]] -name = "rusqlite" -version = "0.32.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7753b721174eb8ff87a9a0e799e2d7bc3749323e773db92e0984debb00019d6e" -dependencies = [ - "bitflags", - "fallible-iterator", - "fallible-streaming-iterator", - "hashlink", - "libsqlite3-sys", - "smallvec", -] - [[package]] name = "rustc-demangle" version = "0.1.24" @@ -1253,12 +1206,6 @@ version = "1.0.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" -[[package]] -name = "vcpkg" -version = "0.2.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" - [[package]] name = "version_check" version = "0.9.5" diff --git a/Cargo.toml b/Cargo.toml index 27f6dfba..fc00f86b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -41,7 +41,6 @@ dumpsys-rs = { git = "https://github.com/shadow3aaa/dumpsys-rs" } mimalloc = "0.1.43" num_cpus = "1.16.0" rand = "0.8.5" -rusqlite = { version = "0.32.1", features = ["bundled"]} [build-dependencies] anyhow = "1.0.93" diff --git a/src/cpu_temp_watcher.rs b/src/cpu_temp_watcher.rs deleted file mode 100644 index c4af62c0..00000000 --- a/src/cpu_temp_watcher.rs +++ /dev/null @@ -1,51 +0,0 @@ -// Copyright 2023 shadow3aaa@gitbub.com -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -use std::{fs, path::PathBuf}; - -use anyhow::Result; - -pub struct CpuTempWatcher { - nodes: Vec, -} - -impl CpuTempWatcher { - pub fn new() -> Result { - let mut nodes = Vec::new(); - for device in fs::read_dir("/sys/devices/virtual/thermal")? { - let device = device?; - let device_type = device.path().join("type"); - let Ok(device_type) = fs::read_to_string(device_type) else { - continue; - }; - if device_type.contains("cpu-") - || device_type.contains("soc_max") - || device_type.contains("mtktscpu") - { - nodes.push(device.path().join("temp")); - } - } - - Ok(Self { nodes }) - } - - pub fn temp(&self) -> u64 { - self.nodes - .iter() - .filter_map(|path| fs::read_to_string(path).ok()) - .map(|temp| temp.trim().parse::().unwrap_or_default()) - .max() - .unwrap() - } -} diff --git a/src/framework/scheduler/looper/mod.rs b/src/framework/scheduler/looper/mod.rs index 0565f754..0541808f 100644 --- a/src/framework/scheduler/looper/mod.rs +++ b/src/framework/scheduler/looper/mod.rs @@ -24,16 +24,10 @@ use likely_stable::{likely, unlikely}; #[cfg(debug_assertions)] use log::debug; use log::info; -use policy::{ - controll::calculate_control, - evolution::{evaluate_fitness, load_control_params, mutate_params, open_database, Fitness}, - ControllerParams, -}; -use rusqlite::Connection; +use policy::{controll::calculate_control, ControllerParams}; use super::{topapp::TimedWatcher, FasData}; use crate::{ - cpu_temp_watcher::CpuTempWatcher, framework::{ config::Config, error::Result, @@ -46,6 +40,8 @@ use crate::{ use buffer::{Buffer, BufferWorkingState}; use clean::Cleaner; +const CONTROLLER_PARAMS: ControllerParams = ControllerParams { kp: 0.0006 }; + #[derive(PartialEq)] enum State { NotWorking, @@ -53,44 +49,6 @@ enum State { Working, } -struct EvolutionState { - controller_params: ControllerParams, - mutated_controller_params: ControllerParams, - mutate_timer: Instant, - fitness: Fitness, -} - -impl EvolutionState { - pub fn reset(&mut self, database: &Connection, pkg: &str) { - self.controller_params = - load_control_params(database, pkg).unwrap_or_else(|_| ControllerParams::default()); - self.mutated_controller_params = self.controller_params; - self.fitness = Fitness::MIN; - } - - pub fn try_evolution( - &mut self, - buffer: &Buffer, - cpu_temp_watcher: &CpuTempWatcher, - config: &mut Config, - mode: Mode, - ) { - if unlikely(self.mutate_timer.elapsed() > Duration::from_millis(100)) { - self.mutate_timer = Instant::now(); - - if let Some(fitness) = evaluate_fitness(buffer, cpu_temp_watcher, config, mode) { - if fitness > self.fitness { - self.controller_params = self.mutated_controller_params; - } - - self.fitness = fitness; - } - - self.mutated_controller_params = mutate_params(self.controller_params); - } - } -} - struct FasState { mode: Mode, working_state: State, @@ -106,22 +64,18 @@ struct AnalyzerState { pub struct Looper { analyzer_state: AnalyzerState, - cpu_temp_watcher: CpuTempWatcher, config: Config, node: Node, extension: Extension, controller: Controller, windows_watcher: TimedWatcher, cleaner: Cleaner, - database: Connection, fas_state: FasState, - evolution_state: EvolutionState, } impl Looper { pub fn new( analyzer: Analyzer, - cpu_temp_watcher: CpuTempWatcher, config: Config, node: Node, extension: Extension, @@ -133,26 +87,18 @@ impl Looper { restart_counter: 0, restart_timer: Instant::now(), }, - cpu_temp_watcher, config, node, extension, controller, windows_watcher: TimedWatcher::new(), cleaner: Cleaner::new(), - database: open_database().unwrap(), fas_state: FasState { mode: Mode::Balance, buffer: None, working_state: State::NotWorking, delay_timer: Instant::now(), }, - evolution_state: EvolutionState { - controller_params: ControllerParams::default(), - mutated_controller_params: ControllerParams::default(), - mutate_timer: Instant::now(), - fitness: Fitness::MIN, - }, } } @@ -253,18 +199,11 @@ impl Looper { } let control = if let Some(buffer) = &self.fas_state.buffer { - self.evolution_state.try_evolution( - buffer, - &self.cpu_temp_watcher, - &mut self.config, - self.fas_state.mode, - ); - calculate_control( buffer, &mut self.config, self.fas_state.mode, - self.evolution_state.mutated_controller_params, + CONTROLLER_PARAMS, ) .unwrap_or_default() } else { diff --git a/src/framework/scheduler/looper/policy/controll.rs b/src/framework/scheduler/looper/policy/controll.rs index 7bec4cfb..d5913794 100644 --- a/src/framework/scheduler/looper/policy/controll.rs +++ b/src/framework/scheduler/looper/policy/controll.rs @@ -55,8 +55,8 @@ fn calculate_control_inner( current_frametime: Duration, target_frametime: Duration, ) -> isize { - let error_p = - (current_frametime.as_nanos() as f64 - target_frametime.as_nanos() as f64) * controller_params.kp; + let error_p = (current_frametime.as_nanos() as f64 - target_frametime.as_nanos() as f64) + * controller_params.kp; #[cfg(debug_assertions)] debug!("error_p {error_p}"); diff --git a/src/framework/scheduler/looper/policy/evolution.rs b/src/framework/scheduler/looper/policy/evolution.rs deleted file mode 100644 index 6a283239..00000000 --- a/src/framework/scheduler/looper/policy/evolution.rs +++ /dev/null @@ -1,129 +0,0 @@ -// Copyright 2023 shadow3aaa@gitbub.com -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -use std::{cmp, time::Duration}; - -use anyhow::Result; -use likely_stable::unlikely; -use rand::Rng; -use rusqlite::{params, Connection}; - -use crate::{ - cpu_temp_watcher::CpuTempWatcher, framework::scheduler::looper::buffer::Buffer, Config, Mode, -}; - -use super::ControllerParams; - -pub const DATABASE_PATH: &str = "/sdcard/Android/fas-rs/database.db"; - -#[derive(Debug, PartialEq)] -pub struct Fitness { - fitness_frametime: f64, - temp: u64, -} - -impl PartialOrd for Fitness { - fn partial_cmp(&self, other: &Self) -> Option { - if (other.fitness_frametime - self.fitness_frametime).abs() < 1_000_000_000_000_000.0 { - other.temp.partial_cmp(&self.temp) - } else { - self.fitness_frametime.partial_cmp(&other.fitness_frametime) - } - } -} - -impl Fitness { - pub const MIN: Self = Self { - fitness_frametime: f64::MIN, - temp: u64::MIN, - }; -} - -pub fn open_database() -> Result { - let conn = Connection::open(DATABASE_PATH)?; - conn.execute( - "CREATE TABLE IF NOT EXISTS control_params ( - id TEXT PRIMARY KEY, - kp REAL NOT NULL - )", - [], - )?; - Ok(conn) -} - -pub fn load_control_params(conn: &Connection, package_name: &str) -> Result { - let mut stmt = conn.prepare("SELECT kp FROM control_params WHERE id = ?1")?; - - let params = stmt.query_row(params![package_name], |row| { - Ok(ControllerParams { kp: row.get(0)? }) - })?; - - Ok(params) -} - -pub fn save_control_params( - conn: &Connection, - package_name: &str, - control_params: ControllerParams, -) -> Result<()> { - conn.execute( - "INSERT INTO control_params (id, kp) - VALUES (?1, ?2) - ON CONFLICT(id) DO UPDATE SET - kp = excluded.kp", - params![package_name, control_params.kp], - )?; - Ok(()) -} - -pub fn mutate_params(params: ControllerParams) -> ControllerParams { - let mut rng = rand::thread_rng(); - ControllerParams { - kp: (params.kp + rng.gen_range(-0.000_1..0.000_1)).clamp(0.000_1, 0.000_8), - } -} - -pub fn evaluate_fitness( - buffer: &Buffer, - cpu_temp_watcher: &CpuTempWatcher, - config: &mut Config, - mode: Mode, -) -> Option { - let target_fps = buffer.target_fps_state.target_fps?; - - if unlikely(buffer.frametime_state.frametimes.len() < target_fps.try_into().unwrap()) { - return None; - } - - let margin = config.mode_config(mode).margin; - let margin = Duration::from_millis(margin); - let target = Duration::from_secs(1) + margin; - - let fitness_frametime = buffer - .frametime_state - .frametimes - .iter() - .copied() - .map(|frametime| frametime * target_fps) - .map(|frametime| (frametime.as_nanos() as f64 - target.as_nanos() as f64).powi(2)) - .sum::() - / buffer.frametime_state.frametimes.len() as f64 - * -1.0; - let temp = cpu_temp_watcher.temp(); - - Some(Fitness { - fitness_frametime, - temp, - }) -} diff --git a/src/framework/scheduler/looper/policy/mod.rs b/src/framework/scheduler/looper/policy/mod.rs index 0804c04c..51e872be 100644 --- a/src/framework/scheduler/looper/policy/mod.rs +++ b/src/framework/scheduler/looper/policy/mod.rs @@ -13,7 +13,6 @@ // limitations under the License. pub mod controll; -pub mod evolution; #[derive(Debug, Copy, Clone)] pub struct ControllerParams { diff --git a/src/framework/scheduler/looper/utils.rs b/src/framework/scheduler/looper/utils.rs index 831d6755..9c276fb4 100644 --- a/src/framework/scheduler/looper/utils.rs +++ b/src/framework/scheduler/looper/utils.rs @@ -17,12 +17,7 @@ use std::time::{Duration, Instant}; use likely_stable::unlikely; use log::info; -use super::{ - super::FasData, - buffer::BufferWorkingState, - policy::evolution::{open_database, save_control_params}, - Buffer, Looper, State, -}; +use super::{super::FasData, buffer::BufferWorkingState, Buffer, Looper, State}; use crate::{ api::{v1::ApiV1, v2::ApiV2, v3::ApiV3}, framework::{api::ApiV0, utils::get_process_name}, @@ -43,11 +38,6 @@ impl Looper { .analyzer .detach_app(buffer.package_info.pid); let pkg = buffer.package_info.pkg.clone(); - if save_control_params(&self.database, &pkg, self.evolution_state.controller_params) - .is_err() - { - self.database = open_database().unwrap(); - } self.extension .trigger_extentions(ApiV0::UnloadFas(buffer.package_info.pid, pkg.clone())); self.extension @@ -125,8 +115,6 @@ impl Looper { info!("New fas buffer on: [{pkg}]"); - self.evolution_state.reset(&self.database, &pkg); - self.extension .trigger_extentions(ApiV0::LoadFas(pid, pkg.clone())); self.extension diff --git a/src/framework/scheduler/mod.rs b/src/framework/scheduler/mod.rs index 6e2e3bb5..a09c5d01 100644 --- a/src/framework/scheduler/mod.rs +++ b/src/framework/scheduler/mod.rs @@ -23,7 +23,7 @@ use super::{ node::Node, Extension, }; -use crate::{cpu_temp_watcher::CpuTempWatcher, Controller}; +use crate::Controller; use frame_analyzer::Analyzer; use looper::Looper; @@ -72,15 +72,7 @@ impl Scheduler { let node = Node::init()?; let analyzer = Analyzer::new()?; - let cpu_temp_watcher = CpuTempWatcher::new()?; - Looper::new( - analyzer, - cpu_temp_watcher, - config, - node, - extension, - controller, - ) - .enter_loop() + + Looper::new(analyzer, config, node, extension, controller).enter_loop() } } diff --git a/src/main.rs b/src/main.rs index b320c4ea..51e74dde 100755 --- a/src/main.rs +++ b/src/main.rs @@ -23,7 +23,6 @@ )] mod cpu_common; -mod cpu_temp_watcher; mod file_handler; mod framework; mod misc;