Replace CPU frequency with C-state monitoring
All checks were successful
Build and Release / build-and-release (push) Successful in 1m20s

- Changed CpuData.frequency_mhz to CpuData.cstate (String)
- Implemented collect_cstate() to read CPU idle depth from sysfs
- Finds deepest C-state with most accumulated time (C0-C10)
- Updated dashboard to display C-state instead of frequency
- More accurate indicator of CPU activity vs power management

Bump version to v0.1.208

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Christoffer Martinsson 2025-11-28 23:30:14 +01:00
parent 2189d34b16
commit c6817537a8
7 changed files with 46 additions and 42 deletions

4
Cargo.lock generated
View File

@ -301,7 +301,7 @@ dependencies = [
[[package]] [[package]]
name = "cm-dashboard-agent" name = "cm-dashboard-agent"
version = "0.1.206" version = "0.1.207"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"async-trait", "async-trait",
@ -324,7 +324,7 @@ dependencies = [
[[package]] [[package]]
name = "cm-dashboard-shared" name = "cm-dashboard-shared"
version = "0.1.206" version = "0.1.207"
dependencies = [ dependencies = [
"chrono", "chrono",
"serde", "serde",

View File

@ -1,6 +1,6 @@
[package] [package]
name = "cm-dashboard-agent" name = "cm-dashboard-agent"
version = "0.1.207" version = "0.1.208"
edition = "2021" edition = "2021"
[dependencies] [dependencies]

View File

@ -119,36 +119,40 @@ impl CpuCollector {
utils::parse_u64(content.trim()) utils::parse_u64(content.trim())
} }
/// Collect CPU frequency and populate AgentData /// Collect CPU C-state (idle depth) and populate AgentData
async fn collect_frequency(&self, agent_data: &mut AgentData) -> Result<(), CollectorError> { async fn collect_cstate(&self, agent_data: &mut AgentData) -> Result<(), CollectorError> {
// Try scaling frequency first (more accurate for current frequency) // Read C-state usage from first CPU (representative of overall system)
if let Ok(freq) = // C-states indicate CPU idle depth: C1=light sleep, C6=deep sleep, C10=deepest
utils::read_proc_file("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq")
{ let mut deepest_state = String::from("C0"); // Default to active
if let Ok(freq_khz) = utils::parse_u64(freq.trim()) { let mut max_time: u64 = 0;
let freq_mhz = freq_khz as f32 / 1000.0;
agent_data.system.cpu.frequency_mhz = freq_mhz; // Check C-states from CPU0
return Ok(()); for state_num in 0..=10 {
let time_path = format!("/sys/devices/system/cpu/cpu0/cpuidle/state{}/time", state_num);
let name_path = format!("/sys/devices/system/cpu/cpu0/cpuidle/state{}/name", state_num);
if let Ok(time_str) = utils::read_proc_file(&time_path) {
if let Ok(time) = utils::parse_u64(time_str.trim()) {
if time > max_time {
// This state has most accumulated time
if let Ok(name) = utils::read_proc_file(&name_path) {
let state_name = name.trim().to_string();
// Skip POLL state (not real idle)
if state_name != "POLL" {
max_time = time;
deepest_state = state_name;
}
}
}
}
} else {
// No more states available
break;
} }
} }
// Fallback: parse /proc/cpuinfo for base frequency agent_data.system.cpu.cstate = deepest_state;
if let Ok(content) = utils::read_proc_file("/proc/cpuinfo") {
for line in content.lines() {
if line.starts_with("cpu MHz") {
if let Some(freq_str) = line.split(':').nth(1) {
if let Ok(freq_mhz) = utils::parse_f32(freq_str) {
agent_data.system.cpu.frequency_mhz = freq_mhz;
return Ok(());
}
}
break; // Only need first CPU entry
}
}
}
debug!("CPU frequency not available");
// Leave frequency as 0.0 if not available
Ok(()) Ok(())
} }
} }
@ -165,8 +169,8 @@ impl Collector for CpuCollector {
// Collect temperature (optional) // Collect temperature (optional)
self.collect_temperature(agent_data).await?; self.collect_temperature(agent_data).await?;
// Collect frequency (optional) // Collect C-state (CPU idle depth)
self.collect_frequency(agent_data).await?; self.collect_cstate(agent_data).await?;
let duration = start.elapsed(); let duration = start.elapsed();
debug!("CPU collection completed in {:?}", duration); debug!("CPU collection completed in {:?}", duration);

View File

@ -1,6 +1,6 @@
[package] [package]
name = "cm-dashboard" name = "cm-dashboard"
version = "0.1.206" version = "0.1.208"
edition = "2021" edition = "2021"
[dependencies] [dependencies]

View File

@ -26,7 +26,7 @@ pub struct SystemWidget {
cpu_load_1min: Option<f32>, cpu_load_1min: Option<f32>,
cpu_load_5min: Option<f32>, cpu_load_5min: Option<f32>,
cpu_load_15min: Option<f32>, cpu_load_15min: Option<f32>,
cpu_frequency: Option<f32>, cpu_cstate: Option<String>,
cpu_status: Status, cpu_status: Status,
// Memory metrics // Memory metrics
@ -102,7 +102,7 @@ impl SystemWidget {
cpu_load_1min: None, cpu_load_1min: None,
cpu_load_5min: None, cpu_load_5min: None,
cpu_load_15min: None, cpu_load_15min: None,
cpu_frequency: None, cpu_cstate: None,
cpu_status: Status::Unknown, cpu_status: Status::Unknown,
memory_usage_percent: None, memory_usage_percent: None,
memory_used_gb: None, memory_used_gb: None,
@ -137,11 +137,11 @@ impl SystemWidget {
} }
} }
/// Format CPU frequency /// Format CPU C-state (idle depth)
fn format_cpu_frequency(&self) -> String { fn format_cpu_cstate(&self) -> String {
match self.cpu_frequency { match &self.cpu_cstate {
Some(freq) => format!("{:.0} MHz", freq), Some(cstate) => cstate.clone(),
None => " MHz".to_string(), None => "".to_string(),
} }
} }
@ -188,7 +188,7 @@ impl Widget for SystemWidget {
self.cpu_load_1min = Some(cpu.load_1min); self.cpu_load_1min = Some(cpu.load_1min);
self.cpu_load_5min = Some(cpu.load_5min); self.cpu_load_5min = Some(cpu.load_5min);
self.cpu_load_15min = Some(cpu.load_15min); self.cpu_load_15min = Some(cpu.load_15min);
self.cpu_frequency = Some(cpu.frequency_mhz); self.cpu_cstate = Some(cpu.cstate.clone());
self.cpu_status = Status::Ok; self.cpu_status = Status::Ok;
// Extract memory data directly // Extract memory data directly
@ -832,10 +832,10 @@ impl SystemWidget {
); );
lines.push(Line::from(cpu_spans)); lines.push(Line::from(cpu_spans));
let freq_text = self.format_cpu_frequency(); let cstate_text = self.format_cpu_cstate();
lines.push(Line::from(vec![ lines.push(Line::from(vec![
Span::styled(" └─ ", Typography::tree()), Span::styled(" └─ ", Typography::tree()),
Span::styled(format!("Freq: {}", freq_text), Typography::secondary()) Span::styled(format!("C-state: {}", cstate_text), Typography::secondary())
])); ]));
// RAM section // RAM section

View File

@ -1,6 +1,6 @@
[package] [package]
name = "cm-dashboard-shared" name = "cm-dashboard-shared"
version = "0.1.207" version = "0.1.208"
edition = "2021" edition = "2021"
[dependencies] [dependencies]

View File

@ -46,7 +46,7 @@ pub struct CpuData {
pub load_1min: f32, pub load_1min: f32,
pub load_5min: f32, pub load_5min: f32,
pub load_15min: f32, pub load_15min: f32,
pub frequency_mhz: f32, pub cstate: String, // Deepest C-state in use (C1, C6, C10, etc.) - indicates CPU idle depth
pub temperature_celsius: Option<f32>, pub temperature_celsius: Option<f32>,
pub load_status: Status, pub load_status: Status,
pub temperature_status: Status, pub temperature_status: Status,
@ -204,7 +204,7 @@ impl AgentData {
load_1min: 0.0, load_1min: 0.0,
load_5min: 0.0, load_5min: 0.0,
load_15min: 0.0, load_15min: 0.0,
frequency_mhz: 0.0, cstate: String::from("C0"),
temperature_celsius: None, temperature_celsius: None,
load_status: Status::Unknown, load_status: Status::Unknown,
temperature_status: Status::Unknown, temperature_status: Status::Unknown,