Show top 3 C-states with usage percentages
All checks were successful
Build and Release / build-and-release (push) Successful in 1m21s

- Changed CpuData.cstate from String to Vec<CStateInfo>
- Added CStateInfo struct with name and percent fields
- Collector calculates percentage for each C-state based on accumulated time
- Sorts and returns top 3 C-states by usage
- Dashboard displays: "C10:79% C8:10% C6:8%"

Provides better visibility into CPU idle state distribution.

Bump version to v0.1.209

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-11-28 23:45:46 +01:00
parent c6817537a8
commit 7c030b33d6
7 changed files with 58 additions and 29 deletions

View File

@@ -119,30 +119,27 @@ impl CpuCollector {
utils::parse_u64(content.trim())
}
/// Collect CPU C-state (idle depth) and populate AgentData
/// Collect CPU C-state (idle depth) and populate AgentData with top 3 C-states by usage
async fn collect_cstate(&self, agent_data: &mut AgentData) -> Result<(), CollectorError> {
// Read C-state usage from first CPU (representative of overall system)
// C-states indicate CPU idle depth: C1=light sleep, C6=deep sleep, C10=deepest
let mut deepest_state = String::from("C0"); // Default to active
let mut max_time: u64 = 0;
let mut cstate_times: Vec<(String, u64)> = Vec::new();
let mut total_time: u64 = 0;
// Check C-states from CPU0
// Collect all C-state times from CPU0
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;
}
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" && time > 0 {
cstate_times.push((state_name, time));
total_time += time;
}
}
}
@@ -152,7 +149,26 @@ impl CpuCollector {
}
}
agent_data.system.cpu.cstate = deepest_state;
// Sort by time descending to get top 3
cstate_times.sort_by(|a, b| b.1.cmp(&a.1));
// Calculate percentages for top 3 and populate AgentData
agent_data.system.cpu.cstates = cstate_times
.iter()
.take(3)
.map(|(name, time)| {
let percent = if total_time > 0 {
(*time as f32 / total_time as f32) * 100.0
} else {
0.0
};
cm_dashboard_shared::CStateInfo {
name: name.clone(),
percent,
}
})
.collect();
Ok(())
}
}