Skip to content

Commit a6934d3

Browse files
committed
various: add log messages to 'critical' functions
Most of those are not critical, I just couldn't think of a better word. Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: I8689bed0470604c75dbac08b7dbd514b6a6a6964
1 parent 2a9560d commit a6934d3

File tree

5 files changed

+100
-0
lines changed

5 files changed

+100
-0
lines changed

watt/config.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ impl CpusDelta {
7676
&self,
7777
state: &EvalState<'_, '_>,
7878
) -> anyhow::Result<(HashMap<Arc<cpu::Cpu>, cpu::Delta>, Option<bool>)> {
79+
log::debug!("evaluating CPU deltas...");
80+
7981
let cpus = match &self.for_ {
8082
Some(numbers) => {
8183
let numbers = numbers
@@ -111,6 +113,8 @@ impl CpusDelta {
111113

112114
let mut deltas = HashMap::with_capacity(cpus.len());
113115

116+
log::trace!("filtering CPUs by number: {cpus:?}");
117+
114118
for cpu in cpus {
115119
let state = state.in_context(EvalContext::Cpu(&cpu));
116120
let mut delta = cpu::Delta::default();
@@ -251,6 +255,8 @@ impl PowersDelta {
251255
HashMap<Arc<power_supply::PowerSupply>, power_supply::Delta>,
252256
Option<String>,
253257
)> {
258+
log::debug!("evaluating power supply deltas...");
259+
254260
let power_supplies = match &self.for_ {
255261
Some(names) => {
256262
let names = names
@@ -282,6 +288,8 @@ impl PowersDelta {
282288

283289
let mut deltas = HashMap::with_capacity(power_supplies.len());
284290

291+
log::trace!("filtering power supplies by name: {power_supplies:?}");
292+
285293
for power_supply in power_supplies {
286294
let state = state.in_context(EvalContext::PowerSupply(&power_supply));
287295
let mut delta = power_supply::Delta::default();
@@ -638,6 +646,8 @@ impl Expression {
638646
) -> anyhow::Result<Option<Expression>> {
639647
use Expression::*;
640648

649+
log::trace!("evaluating expression: {self:?}");
650+
641651
macro_rules! try_ok {
642652
($expression:expr) => {
643653
match $expression {
@@ -945,6 +955,8 @@ impl DaemonConfig {
945955
{
946956
let mut priorities = Vec::with_capacity(config.rules.len());
947957

958+
log::debug!("validating rule priorities...");
959+
948960
for rule in &config.rules {
949961
if priorities.contains(&rule.priority) {
950962
bail!("each config rule must have a different priority")
@@ -967,6 +979,8 @@ impl DaemonConfig {
967979

968980
config.rules.sort_by_key(|rule| rule.priority);
969981

982+
log::debug!("sorted {} rules by priority", config.rules.len());
983+
970984
log::debug!("loaded config: {config:#?}");
971985

972986
Ok(config)

watt/cpu.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,13 @@ impl Cpu {
116116

117117
const PATH: &str = "/sys/devices/system/cpu";
118118

119+
log::info!("detecting CPUs...");
120+
119121
let mut cpus = vec![];
120122
let cache = CpuScanCache::default();
121123

124+
log::debug!("scanning CPU entries in {PATH}");
125+
122126
for entry in fs::read_dir(PATH)
123127
.context("failed to read CPU entries")?
124128
.with_context(|| format!("'{PATH}' doesn't exist, are you on linux?"))?
@@ -146,16 +150,21 @@ impl Cpu {
146150

147151
// Fall back if sysfs iteration above fails to find any cpufreq CPUs.
148152
if cpus.is_empty() {
153+
log::warn!("no CPUs found in sysfs, using logical CPU count fallback");
149154
for number in 0..num_cpus::get() as u32 {
150155
cpus.push(from_number(number, &cache)?);
151156
}
152157
}
153158

159+
log::info!("detected {} CPUs", cpus.len());
160+
154161
Ok(cpus)
155162
}
156163

157164
/// Scan CPU, tuning local copy of settings.
158165
fn scan(&mut self, cache: &CpuScanCache) -> anyhow::Result<()> {
166+
log::debug!("scanning CPU {}", self.number);
167+
159168
let Self { number, .. } = self;
160169

161170
if !fs::exists(format!("/sys/devices/system/cpu/cpu{number}")) {
@@ -165,6 +174,8 @@ impl Cpu {
165174
self.has_cpufreq =
166175
fs::exists(format!("/sys/devices/system/cpu/cpu{number}/cpufreq"));
167176

177+
log::trace!("CPU {} has cpufreq: {}", self.number, self.has_cpufreq);
178+
168179
if self.has_cpufreq {
169180
self.scan_governor()?;
170181
self.scan_frequency()?;
@@ -179,6 +190,8 @@ impl Cpu {
179190
}
180191

181192
fn scan_governor(&mut self) -> anyhow::Result<()> {
193+
log::trace!("scanning governor for CPU {}", self.number);
194+
182195
let Self { number, .. } = *self;
183196

184197
self.governor = fs::read(format!(
@@ -210,6 +223,8 @@ impl Cpu {
210223
}
211224

212225
fn scan_frequency(&mut self) -> anyhow::Result<()> {
226+
log::trace!("scanning frequency for CPU {}", self.number);
227+
213228
let Self { number, .. } = *self;
214229

215230
let frequency_khz = fs::read_n::<u64>(format!(
@@ -233,6 +248,8 @@ impl Cpu {
233248
}
234249

235250
fn scan_epp(&mut self) -> anyhow::Result<()> {
251+
log::trace!("scanning EPP for CPU {}", self.number);
252+
236253
let Self { number, .. } = *self;
237254

238255
self.epp = fs::read(format!(
@@ -263,6 +280,8 @@ impl Cpu {
263280
}
264281

265282
fn scan_epb(&mut self) -> anyhow::Result<()> {
283+
log::trace!("scanning EPB for CPU {}", self.number);
284+
266285
let Self { number, .. } = self;
267286

268287
self.epb = fs::read(format!(
@@ -300,6 +319,8 @@ impl Cpu {
300319
}
301320

302321
fn scan_stat(&mut self, cache: &CpuScanCache) -> anyhow::Result<()> {
322+
log::trace!("scanning stat for CPU {}", self.number);
323+
303324
// OnceCell::get_or_try_init is unstable. Cope:
304325
let stat = match cache.stat.get() {
305326
Some(stat) => stat,
@@ -349,6 +370,8 @@ impl Cpu {
349370
}
350371

351372
fn scan_info(&mut self, cache: &CpuScanCache) -> anyhow::Result<()> {
373+
log::trace!("scanning info for CPU {}", self.number);
374+
352375
// OnceCell::get_or_try_init is unstable. Cope:
353376
let info = match cache.info.get() {
354377
Some(stat) => stat,
@@ -436,6 +459,8 @@ impl Cpu {
436459

437460
self.governor = Some(governor.to_owned());
438461

462+
log::info!("CPU {} governor set to {}", self.number, governor);
463+
439464
Ok(())
440465
}
441466

@@ -470,6 +495,8 @@ impl Cpu {
470495

471496
self.epp = Some(epp.to_owned());
472497

498+
log::info!("CPU {} EPP set to {}", self.number, epp);
499+
473500
Ok(())
474501
}
475502

@@ -503,6 +530,8 @@ impl Cpu {
503530

504531
self.epb = Some(epb.to_owned());
505532

533+
log::info!("CPU {} EPB set to {}", self.number, epb);
534+
506535
Ok(())
507536
}
508537

@@ -529,6 +558,12 @@ impl Cpu {
529558
)
530559
})?;
531560

561+
log::info!(
562+
"CPU {} min frequency set to {} MHz",
563+
self.number,
564+
frequency_mhz
565+
);
566+
532567
Ok(())
533568
}
534569

@@ -581,6 +616,12 @@ impl Cpu {
581616
)
582617
})?;
583618

619+
log::info!(
620+
"CPU {} max frequency set to {} MHz",
621+
self.number,
622+
frequency_mhz
623+
);
624+
584625
Ok(())
585626
}
586627

@@ -616,6 +657,8 @@ impl Cpu {
616657
on: bool,
617658
mut cpus: impl Iterator<Item = &'a Self>,
618659
) -> anyhow::Result<()> {
660+
log::info!("setting CPU turbo boost to {on}");
661+
619662
let value_boost = match on {
620663
true => "1", // boost = 1 means turbo is enabled.
621664
false => "0", // boost = 0 means turbo is disabled.
@@ -667,6 +710,8 @@ impl Cpu {
667710
}
668711

669712
pub fn hardware_frequency_mhz_maximum() -> anyhow::Result<Option<u64>> {
713+
log::trace!("reading hardware frequency limits");
714+
670715
fs::read_n::<u64>("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq")
671716
.context("failed to read CPU hardware maximum frequency")
672717
.map(|x| x.map(|freq| freq / 1000))
@@ -679,6 +724,8 @@ impl Cpu {
679724
}
680725

681726
pub fn turbo() -> anyhow::Result<Option<bool>> {
727+
log::trace!("reading turbo boost status");
728+
682729
if let Some(content) =
683730
fs::read_n::<u64>("/sys/devices/system/cpu/intel_pstate/no_turbo")
684731
.context("failed to read CPU turbo boost status")?

watt/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,7 @@ pub fn main() -> anyhow::Result<()> {
3636
let config = config::DaemonConfig::load_from(cli.config.as_deref())
3737
.context("failed to load daemon config")?;
3838

39+
log::info!("starting watt daemon");
40+
3941
system::run_daemon(config)
4042
}

watt/power_supply.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,12 @@ const POWER_SUPPLY_PATH: &str = "/sys/class/power_supply";
115115

116116
impl PowerSupply {
117117
pub fn all() -> anyhow::Result<Vec<PowerSupply>> {
118+
log::info!("detecting power supplies...");
119+
118120
let mut power_supplies = Vec::new();
119121

122+
log::debug!("scanning power supplies in {POWER_SUPPLY_PATH}");
123+
120124
for entry in fs::read_dir(POWER_SUPPLY_PATH)
121125
.context("failed to read power supply entries")?
122126
.with_context(|| {
@@ -166,10 +170,14 @@ impl PowerSupply {
166170
power_supplies.push(power_supply);
167171
}
168172

173+
log::info!("detected {} power supplies", power_supplies.len());
174+
169175
Ok(power_supplies)
170176
}
171177

172178
fn scan(&mut self) -> anyhow::Result<()> {
179+
log::trace!("scanning power supply '{}'", self.name);
180+
173181
if !self.path.exists() {
174182
bail!("{self} does not exist");
175183
}
@@ -189,6 +197,8 @@ impl PowerSupply {
189197
self.is_from_peripheral = 'is_from_peripheral: {
190198
let name_lower = self.name.to_lowercase();
191199

200+
log::trace!("power supply '{}' type: {}", self.name, self.type_);
201+
192202
// Common peripheral battery names.
193203
if name_lower.contains("mouse")
194204
|| name_lower.contains("keyboard")
@@ -281,6 +291,12 @@ impl PowerSupply {
281291
&& self.path.join(config.path_end).exists()
282292
})
283293
.copied();
294+
295+
log::debug!(
296+
"power supply '{}' threshold config: {:?}",
297+
self.name,
298+
self.threshold_config
299+
);
284300
}
285301

286302
Ok(())
@@ -353,6 +369,8 @@ impl PowerSupply {
353369
}
354370

355371
pub fn get_available_platform_profiles() -> anyhow::Result<Vec<String>> {
372+
log::trace!("reading available platform profiles");
373+
356374
let path = "/sys/firmware/acpi/platform_profile_choices";
357375

358376
let Some(content) = fs::read(path)
@@ -377,6 +395,8 @@ impl PowerSupply {
377395
///
378396
/// [`The Kernel docs`]: <https://docs.kernel.org/userspace-api/sysfs-platform_profile.html>
379397
pub fn set_platform_profile(profile: &str) -> anyhow::Result<()> {
398+
log::info!("setting platform profile to '{profile}'");
399+
380400
let profiles = Self::get_available_platform_profiles()?;
381401

382402
if !profiles
@@ -397,6 +417,8 @@ impl PowerSupply {
397417
}
398418

399419
pub fn platform_profile() -> anyhow::Result<String> {
420+
log::trace!("reading current platform profile");
421+
400422
fs::read("/sys/firmware/acpi/platform_profile")
401423
.context("failed to read platform profile")?
402424
.context("failed to find platform profile")

watt/system.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ impl System {
207207
}
208208

209209
fn scan_temperatures(&mut self) -> anyhow::Result<()> {
210+
log::debug!("scanning CPU temperatures...");
211+
210212
const PATH: &str = "/sys/class/hwmon";
211213

212214
let mut temperatures = HashMap::new();
@@ -414,6 +416,8 @@ impl System {
414416
}
415417

416418
fn scan_load_average(&mut self) -> anyhow::Result<()> {
419+
log::trace!("scanning load average");
420+
417421
let content = fs::read("/proc/loadavg")
418422
.context("failed to read load average from '/proc/loadavg'")?
419423
.context("'/proc/loadavg' doesn't exist, are you on linux?")?;
@@ -582,6 +586,8 @@ impl System {
582586
/// So a return value of Some(0.3) means the battery has been
583587
/// discharging 30% per hour.
584588
fn power_supply_discharge_rate(&self) -> Option<f64> {
589+
log::trace!("calculating power supply discharge rate");
590+
585591
let mut last_charge = None;
586592

587593
// A list of increasing charge percentages.
@@ -667,6 +673,8 @@ pub fn run_daemon(config: config::DaemonConfig) -> anyhow::Result<()> {
667673
let last_user_activity = Instant::now();
668674

669675
while !cancelled.load(Ordering::SeqCst) {
676+
log::debug!("starting main polling loop iteration");
677+
670678
system.scan()?;
671679

672680
let delay = {
@@ -875,11 +883,18 @@ pub fn run_daemon(config: config::DaemonConfig) -> anyhow::Result<()> {
875883
.with_context(|| format!("failed to apply delta to {cpu}"))?;
876884
}
877885

886+
log::info!("applying CPU deltas to {} CPUs", cpu_deltas.len());
887+
878888
if let Some(turbo) = cpu_turbo {
879889
cpu::Cpu::set_turbo(turbo, cpu_deltas.keys().map(|arc| &**arc))
880890
.context("failed to set CPU turbo")?;
881891
}
882892

893+
log::info!(
894+
"applying power supply deltas to {} devices",
895+
power_deltas.len()
896+
);
897+
883898
for (power, delta) in power_deltas {
884899
delta
885900
.apply(&mut (*power).clone())

0 commit comments

Comments
 (0)