Skip to content
This repository has been archived by the owner on Aug 23, 2023. It is now read-only.

Commit

Permalink
Merge branch 'dev' for v1.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
NGnius committed Mar 4, 2023
2 parents 4bf5481 + 5c8ea50 commit 33cd064
Show file tree
Hide file tree
Showing 37 changed files with 867 additions and 493 deletions.
199 changes: 135 additions & 64 deletions backend/Cargo.lock

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions backend/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "powertools-rs"
version = "1.1.1"
name = "powertools"
version = "1.2.0"
edition = "2021"
authors = ["NGnius (Graham) <ngniusness@gmail.com>"]
description = "Backend (superuser) functionality for PowerTools"
Expand All @@ -12,7 +12,7 @@ readme = "../README.md"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
usdpl-back = { version = "0.9.1", features = ["blocking"] }#, path = "../../usdpl-rs/usdpl-back"}
usdpl-back = { version = "0.10.1", features = ["blocking"] }#, path = "../../usdpl-rs/usdpl-back"}
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

Expand All @@ -32,11 +32,12 @@ ryzenadj-rs = { version = "0.1" }
ureq = { version = "2.5", features = ["json", "gzip", "brotli", "charset"], default-features = false, optional = true }

[features]
default = ["online"]
default = ["online", "decky"]
decky = ["usdpl-back/decky"]
crankshaft = ["usdpl-back/crankshaft"]
encrypt = ["usdpl-back/encrypt"]
online = ["ureq"]
dev_stuff = []

[profile.release]
debug = false
Expand Down
2 changes: 1 addition & 1 deletion backend/build-docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ cargo --version
echo "--- Building plugin backend ---"
cargo build --profile docker
mkdir -p out
cp target/release/powertools-rs out/backend
cp target/release/powertools out/backend

echo " --- Cleaning up ---"
# remove root-owned target folder
Expand Down
10 changes: 5 additions & 5 deletions backend/build.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/bin/bash

cargo build --release --target x86_64-unknown-linux-musl
#cargo build --target x86_64-unknown-linux-musl
#cargo build --release --target x86_64-unknown-linux-musl
cargo build --target x86_64-unknown-linux-musl
#cross build --release

mkdir -p ../bin
cp ./target/x86_64-unknown-linux-musl/release/powertools-rs ../bin/backend
#cp ./target/x86_64-unknown-linux-musl/debug/powertools-rs ../bin/backend
#cp ./target/release/powertools-rs ../bin/backend
#cp ./target/x86_64-unknown-linux-musl/release/powertools ../bin/backend
cp ./target/x86_64-unknown-linux-musl/debug/powertools ../bin/backend
#cp ./target/release/powertools ../bin/backend
14 changes: 12 additions & 2 deletions backend/src/api/handler.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::sync::mpsc::{self, Receiver, Sender};
use std::fmt::Write;

use crate::settings::{Settings, TCpus, TGpu, TBattery, TGeneral, OnSet, OnResume, MinMax};
use crate::persist::SettingsJson;
Expand Down Expand Up @@ -216,6 +217,12 @@ pub struct ApiMessageHandler {
on_empty: Vec<Callback<()>>,
}

fn print_errors(call_name: &str, errors: Vec<crate::settings::SettingError>) {
let mut err_list = String::new();
errors.iter().for_each(|e| write!(err_list, "\t{},\n", e).unwrap_or(()));
log::error!("Settings {}() err:\n{}", call_name, err_list);
}

impl ApiMessageHandler {
pub fn process_forever(&mut self, settings: &mut Settings) {
let mut dirty_echo = true; // set everything twice, to make sure PowerTools wins on race conditions
Expand All @@ -228,7 +235,7 @@ impl ApiMessageHandler {
dirty_echo = dirty; // echo only once
// run on_set
if let Err(e) = settings.on_set() {
log::error!("Settings on_set() err: {}", e);
print_errors("on_set", e);
}
// do callbacks
for func in self.on_empty.drain(..) {
Expand All @@ -244,6 +251,9 @@ impl ApiMessageHandler {
let save_json: SettingsJson = settings_clone.into();
unwrap_maybe_fatal(save_json.save(&save_path), "Failed to save settings");
log::debug!("Saved settings to {}", save_path.display());
if let Err(e) = crate::utility::chown_settings_dir() {
log::error!("Failed to change config dir permissions: {}", e);
}
} else {
if save_path.exists() {
if let Err(e) = std::fs::remove_file(&save_path) {
Expand All @@ -269,7 +279,7 @@ impl ApiMessageHandler {
ApiMessage::General(x) => x.process(settings.general.as_mut()),
ApiMessage::OnResume => {
if let Err(e) = settings.on_resume() {
log::error!("Settings on_resume() err: {}", e);
print_errors("on_resume", e);
}
false
}
Expand Down
18 changes: 15 additions & 3 deletions backend/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use usdpl_back::core::serdes::Primitive;
use usdpl_back::Instance;

fn main() -> Result<(), ()> {

#[cfg(debug_assertions)]
let log_filepath = usdpl_back::api::dirs::home()
.unwrap_or_else(|| "/tmp/".into())
Expand Down Expand Up @@ -46,6 +45,7 @@ fn main() -> Result<(), ()> {
},
Default::default(),
std::fs::File::create(&log_filepath).unwrap(),
//std::fs::File::create("/home/deck/powertools-rs.log").unwrap(),
)
.unwrap();
log::debug!("Logging to: {:?}.", log_filepath);
Expand All @@ -55,6 +55,8 @@ fn main() -> Result<(), ()> {
log::info!("Current dir `{}`", std::env::current_dir().unwrap().display());
println!("Current dir `{}`", std::env::current_dir().unwrap().display());

log::info!("home dir: {:?}", usdpl_back::api::dirs::home());

let _limits_handle = crate::settings::limits_worker_spawn();
log::info!("Detected device automatically, starting with driver: {:?} (This can be overriden)", crate::settings::auto_detect_provider());

Expand All @@ -71,7 +73,13 @@ fn main() -> Result<(), ()> {

let instance = Instance::new(PORT)
.register("V_INFO", |_: Vec<Primitive>| {
vec![format!("{} v{}", PACKAGE_NAME, PACKAGE_VERSION).into()]
#[cfg(debug_assertions)]
{vec![format!("v{}-dbg", PACKAGE_VERSION).into()]}
#[cfg(not(debug_assertions))]
{vec![format!("v{}-rls", PACKAGE_VERSION).into()]}
})
.register("NAME", |_: Vec<Primitive>| {
vec![PACKAGE_NAME.into()]
})
.register("LOG", api::general::log_it())
// battery API functions
Expand Down Expand Up @@ -222,11 +230,15 @@ fn main() -> Result<(), ()> {
.register("GENERAL_idk", api::general::gunter);

if let Err(e) = loaded_settings.on_set() {
log::error!("Startup Settings.on_set() error: {}", e);
e.iter().for_each(|e| log::error!("Startup Settings.on_set() error: {}", e));
} else {
log::info!("Startup Settings.on_set() success");
}

if let Err(e) = utility::chown_settings_dir() {
log::warn!("Failed to change config dir permissions: {}", e);
}

api_worker::spawn(loaded_settings, api_handler);

instance
Expand Down
29 changes: 0 additions & 29 deletions backend/src/save_worker.rs

This file was deleted.

8 changes: 4 additions & 4 deletions backend/src/settings/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ pub struct General {
}

impl OnSet for General {
fn on_set(&mut self) -> Result<(), SettingError> {
fn on_set(&mut self) -> Result<(), Vec<SettingError>> {
Ok(())
}
}

impl OnResume for General {
fn on_resume(&self) -> Result<(), SettingError> {
fn on_resume(&self) -> Result<(), Vec<SettingError>> {
Ok(())
}
}
Expand Down Expand Up @@ -91,7 +91,7 @@ pub struct Settings {
}

impl OnSet for Settings {
fn on_set(&mut self) -> Result<(), SettingError> {
fn on_set(&mut self) -> Result<(), Vec<SettingError>> {
self.battery.on_set()?;
self.cpus.on_set()?;
self.gpu.on_set()?;
Expand Down Expand Up @@ -224,7 +224,7 @@ impl Settings {
}

impl OnResume for Settings {
fn on_resume(&self) -> Result<(), SettingError> {
fn on_resume(&self) -> Result<(), Vec<SettingError>> {
log::debug!("Applying settings for on_resume");
self.battery.on_resume()?;
log::debug!("Resumed battery");
Expand Down
6 changes: 4 additions & 2 deletions backend/src/settings/generic/battery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,15 @@ impl Battery {
}

impl OnSet for Battery {
fn on_set(&mut self) -> Result<(), SettingError> {
fn on_set(&mut self) -> Result<(), Vec<SettingError>> {
// TODO
Ok(())
}
}

impl OnResume for Battery {
fn on_resume(&self) -> Result<(), SettingError> {
fn on_resume(&self) -> Result<(), Vec<SettingError>> {
// TODO
Ok(())
}
}
Expand Down
43 changes: 29 additions & 14 deletions backend/src/settings/generic/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ pub struct Cpus<C: AsMut<Cpu> + AsRef<Cpu> + TCpu> {
}

impl<C: AsMut<Cpu> + AsRef<Cpu> + TCpu + OnSet> OnSet for Cpus<C> {
fn on_set(&mut self) -> Result<(), SettingError> {
fn on_set(&mut self) -> Result<(), Vec<SettingError>> {
let mut errors = Vec::new();
if self.smt_capable {
// toggle SMT
if self.smt {
Expand All @@ -31,7 +32,7 @@ impl<C: AsMut<Cpu> + AsRef<Cpu> + TCpu + OnSet> OnSet for Cpus<C> {
),
setting: crate::settings::SettingVariant::Cpu,
}
})?;
}).unwrap_or_else(|e| errors.push(e));
} else {
usdpl_back::api::files::write_single(CPU_SMT_PATH, "off").map_err(|e| {
SettingError {
Expand All @@ -41,23 +42,32 @@ impl<C: AsMut<Cpu> + AsRef<Cpu> + TCpu + OnSet> OnSet for Cpus<C> {
),
setting: crate::settings::SettingVariant::Cpu,
}
})?;
}).unwrap_or_else(|e| errors.push(e));
}
}
for (i, cpu) in self.cpus.as_mut_slice().iter_mut().enumerate() {
cpu.as_mut().state.do_set_online = self.smt || i % 2 == 0 || !self.smt_capable;
cpu.on_set()?;
cpu.on_set().unwrap_or_else(|mut e| errors.append(&mut e));
}
if errors.is_empty() {
Ok(())
} else {
Err(errors)
}
Ok(())
}
}

impl<C: AsMut<Cpu> + AsRef<Cpu> + TCpu + OnResume> OnResume for Cpus<C> {
fn on_resume(&self) -> Result<(), SettingError> {
fn on_resume(&self) -> Result<(), Vec<SettingError>> {
let mut errors = Vec::new();
for cpu in &self.cpus {
cpu.on_resume()?;
cpu.on_resume().unwrap_or_else(|mut e| errors.append(&mut e));
}
if errors.is_empty() {
Ok(())
} else {
Err(errors)
}
Ok(())
}
}

Expand Down Expand Up @@ -232,7 +242,8 @@ impl FromGenericCpuInfo for Cpu {
}

impl Cpu {
fn set_all(&mut self) -> Result<(), SettingError> {
fn set_all(&mut self) -> Result<(), Vec<SettingError>> {
let mut errors = Vec::new();
// set cpu online/offline
if self.index != 0 && self.state.do_set_online { // cpu0 cannot be disabled
let online_path = cpu_online_path(self.index);
Expand All @@ -241,7 +252,7 @@ impl Cpu {
msg: format!("Failed to write to `{}`: {}", &online_path, e),
setting: crate::settings::SettingVariant::Cpu,
}
})?;
}).unwrap_or_else(|e| errors.push(e));
}

// set governor
Expand All @@ -255,9 +266,13 @@ impl Cpu {
),
setting: crate::settings::SettingVariant::Cpu,
}
})?;
}).unwrap_or_else(|e| errors.push(e));
}
if errors.is_empty() {
Ok(())
} else {
Err(errors)
}
Ok(())
}

/*fn from_sys(cpu_index: usize) -> Self {
Expand Down Expand Up @@ -304,14 +319,14 @@ impl Into<CpuJson> for Cpu {
}

impl OnSet for Cpu {
fn on_set(&mut self) -> Result<(), SettingError> {
fn on_set(&mut self) -> Result<(), Vec<SettingError>> {
//self.clamp_all();
self.set_all()
}
}

impl OnResume for Cpu {
fn on_resume(&self) -> Result<(), SettingError> {
fn on_resume(&self) -> Result<(), Vec<SettingError>> {
let mut copy = self.clone();
copy.state.is_resuming = true;
copy.set_all()
Expand Down
4 changes: 2 additions & 2 deletions backend/src/settings/generic/gpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ impl Into<GpuJson> for Gpu {
}

impl OnSet for Gpu {
fn on_set(&mut self) -> Result<(), SettingError> {
fn on_set(&mut self) -> Result<(), Vec<SettingError>> {
Ok(())
}
}

impl OnResume for Gpu {
fn on_resume(&self) -> Result<(), SettingError> {
fn on_resume(&self) -> Result<(), Vec<SettingError>> {
Ok(())
}
}
Expand Down
Loading

0 comments on commit 33cd064

Please sign in to comment.