Add on_load and on_unload traits, reduce impact of #123

This commit is contained in:
NGnius (Graham) 2024-02-05 17:45:35 -05:00
parent c8b8cd1571
commit 48ec9f518f
19 changed files with 1416 additions and 198 deletions

File diff suppressed because it is too large Load diff

View file

@ -3,7 +3,8 @@ use std::sync::mpsc::{self, Receiver, Sender};
use crate::persist::SettingsJson;
use crate::settings::{
MinMax, OnPowerEvent, OnResume, OnSet, PowerMode, Settings, TBattery, TCpus, TGeneral, TGpu,
MinMax, OnLoad, OnPowerEvent, OnResume, OnSet, OnUnload, PowerMode, Settings, TBattery, TCpus,
TGeneral, TGpu,
};
type Callback<T> = Box<dyn FnOnce(T) + Send>;
@ -401,13 +402,22 @@ impl ApiMessageHandler {
}
ApiMessage::LoadSettings(id, name, variant_id, variant_name) => {
let path = format!("{}.ron", id);
if let Err(e) = settings.on_unload() {
print_errors("LoadSettings on_unload()", e);
}
match settings.load_file(path.into(), id, name, variant_id, variant_name, false) {
Ok(success) => log::info!("Loaded settings file? {}", success),
Err(e) => log::warn!("Load file err: {}", e),
}
if let Err(e) = settings.on_load() {
print_errors("LoadSettings on_load()", e);
}
true
}
ApiMessage::LoadVariant(variant_id, variant_name) => {
if let Err(e) = settings.on_unload() {
print_errors("LoadVariant on_unload()", e);
}
let path = settings.general.get_path();
let app_id = settings.general.get_app_id();
match settings.load_file(
@ -421,9 +431,15 @@ impl ApiMessageHandler {
Ok(success) => log::info!("Loaded variant settings file? {}", success),
Err(e) => log::warn!("Load file err: {}", e),
}
if let Err(e) = settings.on_load() {
print_errors("LoadVariant on_load()", e);
}
true
}
ApiMessage::LoadMainSettings => {
if let Err(e) = settings.on_unload() {
print_errors("LoadMainSettings on_unload()", e);
}
match settings.load_file(
crate::consts::DEFAULT_SETTINGS_FILE.into(),
0,
@ -435,14 +451,23 @@ impl ApiMessageHandler {
Ok(success) => log::info!("Loaded main settings file? {}", success),
Err(e) => log::warn!("Load file err: {}", e),
}
if let Err(e) = settings.on_load() {
print_errors("LoadMainSettings on_load()", e);
}
true
}
ApiMessage::LoadSystemSettings => {
if let Err(e) = settings.on_unload() {
print_errors("LoadSystemSettings on_unload()", e);
}
settings.load_system_default(
settings.general.get_name().to_owned(),
settings.general.get_variant_id(),
settings.general.get_variant_info().name,
);
if let Err(e) = settings.on_load() {
print_errors("LoadSystemSettings on_load()", e);
}
true
}
ApiMessage::GetLimits(cb) => {
@ -464,7 +489,6 @@ impl ApiMessageHandler {
false
}
ApiMessage::UploadCurrentVariant(steam_id, steam_username) => {
//TODO
let steam_app_id = settings.general.get_app_id();
super::web::upload_settings(
steam_app_id,

View file

@ -76,6 +76,20 @@ impl OnResume for Battery {
impl crate::settings::OnPowerEvent for Battery {}
impl crate::settings::OnLoad for Battery {
fn on_load(&mut self) -> Result<(), Vec<SettingError>> {
log::debug!("dev_mode_Battery::on_load(self)");
Ok(())
}
}
impl crate::settings::OnUnload for Battery {
fn on_unload(&mut self) -> Result<(), Vec<SettingError>> {
log::debug!("dev_mode_Battery::on_unload(self)");
Ok(())
}
}
impl TBattery for Battery {
fn limits(&self) -> crate::api::BatteryLimits {
log::debug!("dev_mode_Battery::limits(self) -> {{...}}");

View file

@ -39,6 +39,20 @@ impl OnResume for Cpus {
impl crate::settings::OnPowerEvent for Cpus {}
impl crate::settings::OnLoad for Cpus {
fn on_load(&mut self) -> Result<(), Vec<SettingError>> {
log::debug!("dev_mode_Cpus::on_load(self)");
Ok(())
}
}
impl crate::settings::OnUnload for Cpus {
fn on_unload(&mut self) -> Result<(), Vec<SettingError>> {
log::debug!("dev_mode_Cpus::on_unload(self)");
Ok(())
}
}
impl ProviderBuilder<Vec<CpuJson>, GenericCpusLimit> for Cpus {
fn from_json_and_limits(
persistent: Vec<CpuJson>,

View file

@ -80,6 +80,20 @@ impl OnResume for Gpu {
impl crate::settings::OnPowerEvent for Gpu {}
impl crate::settings::OnLoad for Gpu {
fn on_load(&mut self) -> Result<(), Vec<SettingError>> {
log::debug!("dev_mode_Gpu::on_load(self)");
Ok(())
}
}
impl crate::settings::OnUnload for Gpu {
fn on_unload(&mut self) -> Result<(), Vec<SettingError>> {
log::debug!("dev_mode_Gpu::on_unload(self)");
Ok(())
}
}
impl TGpu for Gpu {
fn limits(&self) -> crate::api::GpuLimits {
log::debug!("dev_mode_Gpu::limits(self) -> {{...}}");

View file

@ -2,7 +2,7 @@ use std::path::PathBuf;
//use std::sync::{Arc, Mutex};
//use super::{Battery, Cpus, Gpu};
use super::{OnResume, OnSet, SettingError};
use super::{OnLoad, OnPowerEvent, OnResume, OnSet, OnUnload, SettingError};
use super::{TBattery, TCpus, TGeneral, TGpu};
use crate::persist::{FileJson, SettingsJson};
//use crate::utility::unwrap_lock;
@ -51,7 +51,19 @@ impl OnResume for General {
}
}
impl crate::settings::OnPowerEvent for General {}
impl OnPowerEvent for General {}
impl OnLoad for General {
fn on_load(&mut self) -> Result<(), Vec<SettingError>> {
Ok(())
}
}
impl OnUnload for General {
fn on_unload(&mut self) -> Result<(), Vec<SettingError>> {
Ok(())
}
}
impl TGeneral for General {
fn limits(&self) -> crate::api::GeneralLimits {
@ -461,7 +473,7 @@ impl OnResume for Settings {
}
}
impl crate::settings::OnPowerEvent for Settings {
impl OnPowerEvent for Settings {
fn on_power_event(&mut self, new_mode: super::PowerMode) -> Result<(), Vec<SettingError>> {
let mut errors = Vec::new();
@ -486,6 +498,56 @@ impl crate::settings::OnPowerEvent for Settings {
}
}
impl OnLoad for Settings {
fn on_load(&mut self) -> Result<(), Vec<SettingError>> {
let mut errors = Vec::new();
self.general
.on_load()
.unwrap_or_else(|mut e| errors.append(&mut e));
self.battery
.on_load()
.unwrap_or_else(|mut e| errors.append(&mut e));
self.cpus
.on_load()
.unwrap_or_else(|mut e| errors.append(&mut e));
self.gpu
.on_load()
.unwrap_or_else(|mut e| errors.append(&mut e));
if errors.is_empty() {
Ok(())
} else {
Err(errors)
}
}
}
impl OnUnload for Settings {
fn on_unload(&mut self) -> Result<(), Vec<SettingError>> {
let mut errors = Vec::new();
self.general
.on_unload()
.unwrap_or_else(|mut e| errors.append(&mut e));
self.battery
.on_unload()
.unwrap_or_else(|mut e| errors.append(&mut e));
self.cpus
.on_unload()
.unwrap_or_else(|mut e| errors.append(&mut e));
self.gpu
.on_unload()
.unwrap_or_else(|mut e| errors.append(&mut e));
if errors.is_empty() {
Ok(())
} else {
Err(errors)
}
}
}
/*impl Into<SettingsJson> for Settings {
#[inline]
fn into(self) -> SettingsJson {

View file

@ -132,6 +132,18 @@ impl OnResume for Battery {
impl crate::settings::OnPowerEvent for Battery {}
impl crate::settings::OnLoad for Battery {
fn on_load(&mut self) -> Result<(), Vec<SettingError>> {
Ok(())
}
}
impl crate::settings::OnUnload for Battery {
fn on_unload(&mut self) -> Result<(), Vec<SettingError>> {
Ok(())
}
}
impl TBattery for Battery {
fn limits(&self) -> crate::api::BatteryLimits {
crate::api::BatteryLimits {

View file

@ -176,6 +176,22 @@ impl<C: AsMut<Cpu> + AsRef<Cpu> + TCpu + crate::settings::OnPowerEvent>
}
}
impl<C: AsMut<Cpu> + AsRef<Cpu> + TCpu + OnResume + OnSet + crate::settings::OnPowerEvent>
crate::settings::OnLoad for Cpus<C>
{
fn on_load(&mut self) -> Result<(), Vec<SettingError>> {
Ok(())
}
}
impl<C: AsMut<Cpu> + AsRef<Cpu> + TCpu + OnResume + OnSet + crate::settings::OnPowerEvent>
crate::settings::OnUnload for Cpus<C>
{
fn on_unload(&mut self) -> Result<(), Vec<SettingError>> {
Ok(())
}
}
impl<C: AsMut<Cpu> + AsRef<Cpu> + TCpu + OnResume + OnSet + crate::settings::OnPowerEvent> TCpus
for Cpus<C>
{

View file

@ -138,6 +138,18 @@ impl OnResume for Gpu {
impl crate::settings::OnPowerEvent for Gpu {}
impl crate::settings::OnLoad for Gpu {
fn on_load(&mut self) -> Result<(), Vec<SettingError>> {
Ok(())
}
}
impl crate::settings::OnUnload for Gpu {
fn on_unload(&mut self) -> Result<(), Vec<SettingError>> {
Ok(())
}
}
impl TGpu for Gpu {
fn limits(&self) -> crate::api::GpuLimits {
crate::api::GpuLimits {

View file

@ -43,6 +43,18 @@ impl OnSet for Cpus {
impl crate::settings::OnPowerEvent for Cpus {}
impl crate::settings::OnLoad for Cpus {
fn on_load(&mut self) -> Result<(), Vec<SettingError>> {
Ok(())
}
}
impl crate::settings::OnUnload for Cpus {
fn on_unload(&mut self) -> Result<(), Vec<SettingError>> {
Ok(())
}
}
impl TCpus for Cpus {
fn limits(&self) -> crate::api::CpusLimits {
self.generic.limits()

View file

@ -312,6 +312,18 @@ impl OnSet for Gpu {
impl crate::settings::OnPowerEvent for Gpu {}
impl crate::settings::OnLoad for Gpu {
fn on_load(&mut self) -> Result<(), Vec<SettingError>> {
Ok(())
}
}
impl crate::settings::OnUnload for Gpu {
fn on_unload(&mut self) -> Result<(), Vec<SettingError>> {
Ok(())
}
}
fn bad_gpu_limits() -> crate::api::GpuLimits {
crate::api::GpuLimits {
fast_ppt_limits: None,

View file

@ -22,8 +22,8 @@ pub use min_max::{min_max_from_json, MinMax};
pub use error::SettingError;
pub use traits::{
OnPowerEvent, OnResume, OnSet, PowerMode, ProviderBuilder, TBattery, TCpu, TCpus, TGeneral,
TGpu,
OnLoad, OnPowerEvent, OnResume, OnSet, OnUnload, PowerMode, ProviderBuilder, TBattery, TCpu,
TCpus, TGeneral, TGpu,
};
#[cfg(test)]

View file

@ -743,6 +743,18 @@ impl OnPowerEvent for Battery {
}
}
impl crate::settings::OnLoad for Battery {
fn on_load(&mut self) -> Result<(), Vec<SettingError>> {
Ok(())
}
}
impl crate::settings::OnUnload for Battery {
fn on_unload(&mut self) -> Result<(), Vec<SettingError>> {
Ok(())
}
}
impl TBattery for Battery {
fn limits(&self) -> crate::api::BatteryLimits {
crate::api::BatteryLimits {

View file

@ -181,6 +181,35 @@ impl ProviderBuilder<Vec<CpuJson>, GenericCpusLimit> for Cpus {
impl crate::settings::OnPowerEvent for Cpus {}
impl crate::settings::OnLoad for Cpus {
fn on_load(&mut self) -> Result<(), Vec<SettingError>> {
let mut errors = Vec::new();
for cpu in &mut self.cpus {
cpu.on_load().unwrap_or_else(|mut e| errors.append(&mut e));
}
if errors.is_empty() {
Ok(())
} else {
Err(errors)
}
}
}
impl crate::settings::OnUnload for Cpus {
fn on_unload(&mut self) -> Result<(), Vec<SettingError>> {
let mut errors = Vec::new();
for cpu in &mut self.cpus {
cpu.on_unload()
.unwrap_or_else(|mut e| errors.append(&mut e));
}
if errors.is_empty() {
Ok(())
} else {
Err(errors)
}
}
}
impl TCpus for Cpus {
fn limits(&self) -> crate::api::CpusLimits {
crate::api::CpusLimits {
@ -404,9 +433,6 @@ impl Cpu {
)
.unwrap_or_else(|e| errors.push(e));
}
// TODO remove this when it's no longer needed
self.clock_unset_workaround()
.unwrap_or_else(|mut e| errors.append(&mut e));
if errors.is_empty() {
Ok(())
} else {
@ -418,14 +444,9 @@ impl Cpu {
}
// https://github.com/NGnius/PowerTools/issues/107
fn clock_unset_workaround(&self) -> Result<(), Vec<SettingError>> {
if !self.state.is_resuming {
let mut errors = Vec::new();
POWER_DPM_FORCE_PERFORMANCE_LEVEL_MGMT.set_cpu(true, self.index);
// always set clock speeds, since it doesn't reset correctly (kernel/hardware bug)
POWER_DPM_FORCE_PERFORMANCE_LEVEL_MGMT.enforce_level(&self.sysfs)?;
// disable manual clock limits
log::debug!("Setting CPU {} to default clockspeed", self.index);
fn clock_unset(&self) -> Result<(), Vec<SettingError>> {
let mut errors = Vec::new();
if self.state.clock_limits_set && POWER_DPM_FORCE_PERFORMANCE_LEVEL_MGMT.needs_manual() {
// max clock
self.set_clock_limit(
self.index,
@ -445,15 +466,15 @@ impl Cpu {
self.reset_clock_limits().unwrap_or_else(|e| errors.push(e));
self.set_confirm().unwrap_or_else(|e| errors.push(e));
POWER_DPM_FORCE_PERFORMANCE_LEVEL_MGMT.set_cpu(false, self.index);
if errors.is_empty() {
Ok(())
} else {
Err(errors)
}
} else {
}
POWER_DPM_FORCE_PERFORMANCE_LEVEL_MGMT.set_cpu(false, self.index);
POWER_DPM_FORCE_PERFORMANCE_LEVEL_MGMT
.enforce_level(&self.sysfs)
.unwrap_or_else(|mut e| errors.append(&mut e));
if errors.is_empty() {
Ok(())
} else {
Err(errors)
}
}
@ -639,6 +660,18 @@ impl OnResume for Cpu {
}
}
impl crate::settings::OnLoad for Cpu {
fn on_load(&mut self) -> Result<(), Vec<SettingError>> {
Ok(())
}
}
impl crate::settings::OnUnload for Cpu {
fn on_unload(&mut self) -> Result<(), Vec<SettingError>> {
self.clock_unset()
}
}
impl TCpu for Cpu {
fn online(&mut self) -> &mut bool {
&mut self.online

View file

@ -597,6 +597,18 @@ impl OnResume for Gpu {
impl crate::settings::OnPowerEvent for Gpu {}
impl crate::settings::OnLoad for Gpu {
fn on_load(&mut self) -> Result<(), Vec<SettingError>> {
Ok(())
}
}
impl crate::settings::OnUnload for Gpu {
fn on_unload(&mut self) -> Result<(), Vec<SettingError>> {
Ok(())
}
}
impl TGpu for Gpu {
fn limits(&self) -> crate::api::GpuLimits {
crate::api::GpuLimits {

View file

@ -10,6 +10,14 @@ pub trait OnResume {
fn on_resume(&self) -> Result<(), Vec<SettingError>>;
}
pub trait OnLoad {
fn on_load(&mut self) -> Result<(), Vec<SettingError>>;
}
pub trait OnUnload {
fn on_unload(&mut self) -> Result<(), Vec<SettingError>>;
}
#[repr(u8)]
#[derive(Clone, Copy, Debug)]
pub enum PowerMode {
@ -46,7 +54,7 @@ pub trait ProviderBuilder<J, L> {
fn from_limits(limits: L) -> Self;
}
pub trait TGpu: OnSet + OnResume + OnPowerEvent + Debug + Send {
pub trait TGpu: OnSet + OnResume + OnPowerEvent + OnLoad + OnUnload + Debug + Send {
fn limits(&self) -> crate::api::GpuLimits;
fn json(&self) -> crate::persist::GpuJson;
@ -68,7 +76,7 @@ pub trait TGpu: OnSet + OnResume + OnPowerEvent + Debug + Send {
}
}
pub trait TCpus: OnSet + OnResume + OnPowerEvent + Debug + Send {
pub trait TCpus: OnSet + OnResume + OnPowerEvent + OnLoad + OnUnload + Debug + Send {
fn limits(&self) -> crate::api::CpusLimits;
fn json(&self) -> Vec<crate::persist::CpuJson>;
@ -96,7 +104,7 @@ pub trait TCpu: Debug + Send {
fn get_clock_limits(&self) -> Option<&MinMax<u64>>;
}
pub trait TGeneral: OnSet + OnResume + OnPowerEvent + Debug + Send {
pub trait TGeneral: OnSet + OnResume + OnPowerEvent + OnLoad + OnUnload + Debug + Send {
fn limits(&self) -> crate::api::GeneralLimits;
fn get_persistent(&self) -> bool;
@ -133,7 +141,7 @@ pub trait TGeneral: OnSet + OnResume + OnPowerEvent + Debug + Send {
fn provider(&self) -> crate::persist::DriverJson;
}
pub trait TBattery: OnSet + OnResume + OnPowerEvent + Debug + Send {
pub trait TBattery: OnSet + OnResume + OnPowerEvent + OnLoad + OnUnload + Debug + Send {
fn limits(&self) -> crate::api::BatteryLimits;
fn json(&self) -> crate::persist::BatteryJson;

View file

@ -56,6 +56,18 @@ impl OnResume for Battery {
impl crate::settings::OnPowerEvent for Battery {}
impl crate::settings::OnLoad for Battery {
fn on_load(&mut self) -> Result<(), Vec<SettingError>> {
Ok(())
}
}
impl crate::settings::OnUnload for Battery {
fn on_unload(&mut self) -> Result<(), Vec<SettingError>> {
Ok(())
}
}
impl TBattery for Battery {
fn limits(&self) -> crate::api::BatteryLimits {
crate::api::BatteryLimits {

View file

@ -71,6 +71,18 @@ impl OnResume for Cpus {
impl crate::settings::OnPowerEvent for Cpus {}
impl crate::settings::OnLoad for Cpus {
fn on_load(&mut self) -> Result<(), Vec<SettingError>> {
Ok(())
}
}
impl crate::settings::OnUnload for Cpus {
fn on_unload(&mut self) -> Result<(), Vec<SettingError>> {
Ok(())
}
}
impl Cpus {
pub fn cpu_count() -> Option<usize> {
let mut data: String = usdpl_back::api::files::read_single(CPU_PRESENT_PATH)

View file

@ -55,6 +55,18 @@ impl OnResume for Gpu {
impl crate::settings::OnPowerEvent for Gpu {}
impl crate::settings::OnLoad for Gpu {
fn on_load(&mut self) -> Result<(), Vec<SettingError>> {
Ok(())
}
}
impl crate::settings::OnUnload for Gpu {
fn on_unload(&mut self) -> Result<(), Vec<SettingError>> {
Ok(())
}
}
impl TGpu for Gpu {
fn limits(&self) -> crate::api::GpuLimits {
crate::api::GpuLimits {