mirror of
https://github.com/RGBCube/superfreq
synced 2025-07-27 17:07:44 +00:00
main: move application to deltas, comment out broken modules for now
This commit is contained in:
parent
c073b640dc
commit
ca4b1dbc92
3 changed files with 96 additions and 99 deletions
|
@ -3,6 +3,8 @@ use std::{fs, path::Path};
|
|||
use anyhow::{Context, bail};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{cpu, power_supply};
|
||||
|
||||
fn is_default<T: Default + PartialEq>(value: &T) -> bool {
|
||||
*value == T::default()
|
||||
}
|
||||
|
@ -46,6 +48,51 @@ pub struct CpuDelta {
|
|||
pub turbo: Option<bool>,
|
||||
}
|
||||
|
||||
impl CpuDelta {
|
||||
pub fn apply(&self) -> anyhow::Result<()> {
|
||||
let cpus = match &self.for_ {
|
||||
Some(numbers) => {
|
||||
let mut cpus = Vec::with_capacity(numbers.len());
|
||||
|
||||
for &number in numbers {
|
||||
cpus.push(cpu::Cpu::new(number)?);
|
||||
}
|
||||
|
||||
cpus
|
||||
}
|
||||
None => cpu::Cpu::all()?,
|
||||
};
|
||||
|
||||
for cpu in cpus {
|
||||
if let Some(governor) = self.governor.as_ref() {
|
||||
cpu.set_governor(governor)?;
|
||||
}
|
||||
|
||||
if let Some(epp) = self.energy_performance_preference.as_ref() {
|
||||
cpu.set_epp(epp)?;
|
||||
}
|
||||
|
||||
if let Some(epb) = self.energy_performance_bias.as_ref() {
|
||||
cpu.set_epb(epb)?;
|
||||
}
|
||||
|
||||
if let Some(mhz_minimum) = self.frequency_mhz_minimum {
|
||||
cpu.set_frequency_minimum(mhz_minimum)?;
|
||||
}
|
||||
|
||||
if let Some(mhz_maximum) = self.frequency_mhz_maximum {
|
||||
cpu.set_frequency_maximum(mhz_maximum)?;
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(turbo) = self.turbo {
|
||||
cpu::Cpu::set_turbo(turbo)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, clap::Parser, Default, Debug, Clone, PartialEq, Eq)]
|
||||
#[serde(deny_unknown_fields, default, rename_all = "kebab-case")]
|
||||
pub struct PowerDelta {
|
||||
|
@ -70,6 +117,43 @@ pub struct PowerDelta {
|
|||
pub platform_profile: Option<String>,
|
||||
}
|
||||
|
||||
impl PowerDelta {
|
||||
pub fn apply(&self) -> anyhow::Result<()> {
|
||||
let power_supplies = match &self.for_ {
|
||||
Some(names) => {
|
||||
let mut power_supplies = Vec::with_capacity(names.len());
|
||||
|
||||
for name in names {
|
||||
power_supplies.push(power_supply::PowerSupply::from_name(name.clone())?);
|
||||
}
|
||||
|
||||
power_supplies
|
||||
}
|
||||
|
||||
None => power_supply::PowerSupply::all()?
|
||||
.into_iter()
|
||||
.filter(|power_supply| power_supply.threshold_config.is_some())
|
||||
.collect(),
|
||||
};
|
||||
|
||||
for power_supply in power_supplies {
|
||||
if let Some(threshold_start) = self.charge_threshold_start {
|
||||
power_supply.set_charge_threshold_start(threshold_start)?;
|
||||
}
|
||||
|
||||
if let Some(threshold_end) = self.charge_threshold_end {
|
||||
power_supply.set_charge_threshold_end(threshold_end)?;
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(platform_profile) = self.platform_profile.as_ref() {
|
||||
power_supply::PowerSupply::set_platform_profile(platform_profile)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq, Eq, Hash)]
|
||||
#[serde(untagged, rename_all = "kebab-case")]
|
||||
pub enum Condition {
|
||||
|
|
|
@ -236,7 +236,7 @@ impl Cpu {
|
|||
self.validate_frequency_minimum(frequency_mhz)?;
|
||||
|
||||
// We use u64 for the intermediate calculation to prevent overflow
|
||||
let frequency_khz = u64::from(frequency_mhz) * 1000;
|
||||
let frequency_khz = frequency_mhz * 1000;
|
||||
let frequency_khz = frequency_khz.to_string();
|
||||
|
||||
write(
|
||||
|
@ -258,7 +258,7 @@ impl Cpu {
|
|||
return Ok(());
|
||||
};
|
||||
|
||||
if new_frequency_mhz as u64 * 1000 < minimum_frequency_khz {
|
||||
if new_frequency_mhz * 1000 < minimum_frequency_khz {
|
||||
bail!(
|
||||
"new minimum frequency ({new_frequency_mhz} MHz) cannot be lower than the minimum frequency ({} MHz) for {self}",
|
||||
minimum_frequency_khz / 1000,
|
||||
|
@ -274,7 +274,7 @@ impl Cpu {
|
|||
self.validate_frequency_maximum(frequency_mhz)?;
|
||||
|
||||
// We use u64 for the intermediate calculation to prevent overflow
|
||||
let frequency_khz = u64::from(frequency_mhz) * 1000;
|
||||
let frequency_khz = frequency_mhz * 1000;
|
||||
let frequency_khz = frequency_khz.to_string();
|
||||
|
||||
write(
|
||||
|
@ -344,7 +344,7 @@ impl Cpu {
|
|||
let Cpu { number, .. } = cpu;
|
||||
|
||||
write(
|
||||
&format!("/sys/devices/system/cpu/cpu{number}/cpufreq/boost"),
|
||||
format!("/sys/devices/system/cpu/cpu{number}/cpufreq/boost"),
|
||||
value_boost,
|
||||
)
|
||||
.is_ok()
|
||||
|
|
103
src/main.rs
103
src/main.rs
|
@ -1,9 +1,9 @@
|
|||
mod config;
|
||||
mod core;
|
||||
// mod core;
|
||||
mod cpu;
|
||||
mod daemon;
|
||||
mod engine;
|
||||
mod monitor;
|
||||
// mod daemon;
|
||||
// mod engine;
|
||||
// mod monitor;
|
||||
mod power_supply;
|
||||
|
||||
use anyhow::Context;
|
||||
|
@ -56,102 +56,15 @@ fn real_main() -> anyhow::Result<()> {
|
|||
Command::Info => todo!(),
|
||||
|
||||
Command::Start { config } => {
|
||||
let config = config::DaemonConfig::load_from(&config)
|
||||
let _config = config::DaemonConfig::load_from(&config)
|
||||
.context("failed to load daemon config file")?;
|
||||
|
||||
daemon::run(config)
|
||||
}
|
||||
|
||||
Command::CpuSet(config::CpuDelta {
|
||||
for_,
|
||||
governor,
|
||||
energy_performance_preference,
|
||||
energy_performance_bias,
|
||||
frequency_mhz_minimum,
|
||||
frequency_mhz_maximum,
|
||||
turbo,
|
||||
}) => {
|
||||
let cpus = match for_ {
|
||||
Some(numbers) => {
|
||||
let mut cpus = Vec::with_capacity(numbers.len());
|
||||
|
||||
for number in numbers {
|
||||
cpus.push(cpu::Cpu::new(number)?);
|
||||
}
|
||||
|
||||
cpus
|
||||
}
|
||||
None => cpu::Cpu::all()?,
|
||||
};
|
||||
|
||||
for cpu in cpus {
|
||||
if let Some(governor) = governor.as_ref() {
|
||||
cpu.set_governor(governor)?;
|
||||
}
|
||||
|
||||
if let Some(epp) = energy_performance_preference.as_ref() {
|
||||
cpu.set_epp(epp)?;
|
||||
}
|
||||
|
||||
if let Some(epb) = energy_performance_bias.as_ref() {
|
||||
cpu.set_epb(epb)?;
|
||||
}
|
||||
|
||||
if let Some(mhz_minimum) = frequency_mhz_minimum {
|
||||
cpu.set_frequency_minimum(mhz_minimum)?;
|
||||
}
|
||||
|
||||
if let Some(mhz_maximum) = frequency_mhz_maximum {
|
||||
cpu.set_frequency_maximum(mhz_maximum)?;
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(turbo) = turbo {
|
||||
cpu::Cpu::set_turbo(turbo)?;
|
||||
}
|
||||
|
||||
// daemon::run(config)
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Command::PowerSet(config::PowerDelta {
|
||||
for_,
|
||||
charge_threshold_start,
|
||||
charge_threshold_end,
|
||||
platform_profile,
|
||||
}) => {
|
||||
let power_supplies = match for_ {
|
||||
Some(names) => {
|
||||
let mut power_supplies = Vec::with_capacity(names.len());
|
||||
|
||||
for name in names {
|
||||
power_supplies.push(power_supply::PowerSupply::from_name(name)?);
|
||||
}
|
||||
|
||||
power_supplies
|
||||
}
|
||||
|
||||
None => power_supply::PowerSupply::all()?
|
||||
.into_iter()
|
||||
.filter(|power_supply| power_supply.threshold_config.is_some())
|
||||
.collect(),
|
||||
};
|
||||
|
||||
for power_supply in power_supplies {
|
||||
if let Some(threshold_start) = charge_threshold_start {
|
||||
power_supply.set_charge_threshold_start(threshold_start)?;
|
||||
}
|
||||
|
||||
if let Some(threshold_end) = charge_threshold_end {
|
||||
power_supply.set_charge_threshold_end(threshold_end)?;
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(platform_profile) = platform_profile.as_ref() {
|
||||
power_supply::PowerSupply::set_platform_profile(platform_profile);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Command::CpuSet(delta) => delta.apply(),
|
||||
Command::PowerSet(delta) => delta.apply(),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue