Skip to content

Commit

Permalink
smooth temp / power values between updates #10
Browse files Browse the repository at this point in the history
  • Loading branch information
vladkens committed Nov 29, 2024
1 parent 7bfdaf4 commit 10f677c
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 39 deletions.
85 changes: 52 additions & 33 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ panic = "abort"
strip = false

[dependencies]
clap = { version = "4.5.20", features = ["derive"] }
clap = { version = "4.5.21", features = ["derive"] }
core-foundation = "0.10.0"
libc = "0.2.161"
libc = "0.2.166"
num-traits = "0.2.19"
ratatui = { version = "0.29.0", features = ["serde"] }
serde = { version = "1.0.213", features = ["derive"] }
serde_json = "1.0.132"
serde = { version = "1.0.215", features = ["derive"] }
serde_json = "1.0.133"
14 changes: 12 additions & 2 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@ struct PowerStore {

impl PowerStore {
fn push(&mut self, value: f64) {
let was_top = if self.items.len() > 0 { self.items[0] as f64 / 1000.0 } else { 0.0 };
items_add(&mut self.items, (value * 1000.0) as u64);
self.top_value = value;
self.top_value = avg2(was_top, value);
self.avg_value = self.items.iter().sum::<u64>() as f64 / self.items.len() as f64 / 1000.0;
self.max_value = self.items.iter().max().map_or(0, |v| *v) as f64 / 1000.0;
}
Expand Down Expand Up @@ -171,6 +172,12 @@ fn run_sampler_thread(tx: mpsc::Sender<Event>, interval: u64) {
});
}

// get avaerage of two values, used to smooth out metrics
// see: https://github.com/vladkens/macmon/issues/10
fn avg2<T: num_traits::Float>(a: T, b: T) -> T {
return if a == T::zero() { b } else { (a + b) / T::from(2.0).unwrap() };
}

// MARK: App

#[derive(Debug, Default)]
Expand Down Expand Up @@ -208,7 +215,10 @@ impl App {
self.ecpu_freq.push(data.ecpu_usage.0 as u64, data.ecpu_usage.1 as f64);
self.pcpu_freq.push(data.pcpu_usage.0 as u64, data.pcpu_usage.1 as f64);
self.igpu_freq.push(data.gpu_usage.0 as u64, data.gpu_usage.1 as f64);
self.temp = data.temp;

self.temp.cpu_temp_avg = avg2(self.temp.cpu_temp_avg, data.temp.cpu_temp_avg);
self.temp.gpu_temp_avg = avg2(self.temp.gpu_temp_avg, data.temp.gpu_temp_avg);

self.mem.push(data.memory);
}

Expand Down
2 changes: 2 additions & 0 deletions src/sources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,8 @@ pub fn libc_ram() -> WithError<(u64, u64)> {
let mut count: u32 = libc::HOST_VM_INFO64_COUNT as _;
let mut stats = std::mem::zeroed::<libc::vm_statistics64>();

// todo: https://github.com/JohnTitor/mach2/issues/34
#[allow(deprecated)]
let ret_code = libc::host_statistics64(
libc::mach_host_self(),
libc::HOST_VM_INFO64,
Expand Down

0 comments on commit 10f677c

Please sign in to comment.