From 53cb6510d0ed3805491e69177e1c48754e3616bc Mon Sep 17 00:00:00 2001 From: Christoffer Martinsson Date: Sun, 12 Oct 2025 18:10:05 +0200 Subject: [PATCH] Testing --- agent/src/collectors/service.rs | 36 ++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/agent/src/collectors/service.rs b/agent/src/collectors/service.rs index b4ef16a..d448041 100644 --- a/agent/src/collectors/service.rs +++ b/agent/src/collectors/service.rs @@ -390,6 +390,28 @@ impl ServiceCollector { } async fn get_cpu_frequency_mhz(&self) -> Option { + // Prioritize /proc/cpuinfo for actual current frequencies, then calculate average + if let Ok(content) = fs::read_to_string("/proc/cpuinfo").await { + let mut frequencies = Vec::new(); + for line in content.lines() { + if let Some(rest) = line.strip_prefix("cpu MHz") { + if let Some(value) = rest.split(':').nth(1) { + if let Ok(mhz) = value.trim().parse::() { + if mhz > 0.0 { + frequencies.push(mhz); + } + } + } + } + } + + if !frequencies.is_empty() { + let avg_freq = frequencies.iter().sum::() / frequencies.len() as f32; + return Some(avg_freq); + } + } + + // Fallback to scaling frequency if cpuinfo is not available let candidates = [ "/sys/devices/system/cpu/cpufreq/policy0/scaling_cur_freq", "/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq", @@ -405,20 +427,6 @@ impl ServiceCollector { } } - if let Ok(content) = fs::read_to_string("/proc/cpuinfo").await { - for line in content.lines() { - if let Some(rest) = line.strip_prefix("cpu MHz") { - if let Some(value) = rest.split(':').nth(1) { - if let Ok(mhz) = value.trim().parse::() { - if mhz > 0.0 { - return Some(mhz); - } - } - } - } - } - } - None }