From b65d29d86b698dde880ca9c0124e5edfde9b5635 Mon Sep 17 00:00:00 2001 From: Christoffer Martinsson Date: Sun, 12 Oct 2025 18:30:33 +0200 Subject: [PATCH] Testing --- agent/src/collectors/service.rs | 65 ++++++++++++++++----------------- 1 file changed, 31 insertions(+), 34 deletions(-) diff --git a/agent/src/collectors/service.rs b/agent/src/collectors/service.rs index d448041..df04d62 100644 --- a/agent/src/collectors/service.rs +++ b/agent/src/collectors/service.rs @@ -389,44 +389,41 @@ impl ServiceCollector { Ok((parse(parts[0])?, parse(parts[1])?, parse(parts[2])?)) } - 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); - } - } + async fn get_cpu_cstate_info(&self) -> Option { + // Read C-state information to show actual sleep state distribution + let mut cstate_times: Vec<(String, u64)> = Vec::new(); + let mut total_time = 0u64; + + // Check if C-state information is available + if let Ok(mut entries) = fs::read_dir("/sys/devices/system/cpu/cpu0/cpuidle").await { + while let Ok(Some(entry)) = entries.next_entry().await { + let state_path = entry.path(); + let name_path = state_path.join("name"); + let time_path = state_path.join("time"); + + if let (Ok(name), Ok(time_str)) = ( + fs::read_to_string(&name_path).await, + fs::read_to_string(&time_path).await + ) { + let name = name.trim().to_string(); + if let Ok(time) = time_str.trim().parse::() { + total_time += time; + cstate_times.push((name, time)); } } } - if !frequencies.is_empty() { - let avg_freq = frequencies.iter().sum::() / frequencies.len() as f32; - return Some(avg_freq); + if total_time > 0 && !cstate_times.is_empty() { + // Find the dominant C-state (highest time) + cstate_times.sort_by(|a, b| b.1.cmp(&a.1)); + let dominant_state = &cstate_times[0]; + let dominant_percent = (dominant_state.1 as f32 / total_time as f32) * 100.0; + + // Return dominant state info + return Some(format!("{}({:.1}%)", dominant_state.0, dominant_percent)); } } - - // 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", - ]; - - for path in candidates { - if let Ok(content) = fs::read_to_string(path).await { - if let Ok(khz) = content.trim().parse::() { - if khz > 0.0 { - return Some(khz / 1000.0); - } - } - } - } - + None } @@ -988,7 +985,7 @@ impl Collector for ServiceCollector { let (cpu_load_1, cpu_load_5, cpu_load_15) = self.get_cpu_load().await.unwrap_or((0.0, 0.0, 0.0)); - let cpu_freq_mhz = self.get_cpu_frequency_mhz().await; + let cpu_cstate_info = self.get_cpu_cstate_info().await; let cpu_temp_c = self.get_cpu_temperature_c().await; let (gpu_load_percent, gpu_temp_c) = self.get_gpu_metrics().await; @@ -1011,7 +1008,7 @@ impl Collector for ServiceCollector { "cpu_load_1": cpu_load_1, "cpu_load_5": cpu_load_5, "cpu_load_15": cpu_load_15, - "cpu_freq_mhz": cpu_freq_mhz, + "cpu_cstate": cpu_cstate_info, "cpu_temp_c": cpu_temp_c, "gpu_load_percent": gpu_load_percent, "gpu_temp_c": gpu_temp_c,