diff --git a/agent/src/collector.rs b/agent/src/collector.rs index 1b7240b..974388c 100644 --- a/agent/src/collector.rs +++ b/agent/src/collector.rs @@ -1,13 +1,13 @@ use common::metrics::{CPU, Disk, Memory, Metrics, Network, SystemInfo}; use sysinfo::{Disks, Networks, System}; -use crate::cpu::CPUStatReader; +use crate::cpu::CpuStatReader; pub struct Collector { sys: System, disks: Disks, networks: Networks, - cpu_reader: CPUStatReader, + cpu_reader: CpuStatReader, } impl Collector { @@ -16,7 +16,7 @@ impl Collector { sys: System::new_all(), disks: Disks::new_with_refreshed_list(), networks: Networks::new_with_refreshed_list(), - cpu_reader: CPUStatReader::new(), + cpu_reader: CpuStatReader::new(), } } diff --git a/agent/src/cpu.rs b/agent/src/cpu.rs index 3fdf810..dc1c35b 100644 --- a/agent/src/cpu.rs +++ b/agent/src/cpu.rs @@ -1,9 +1,9 @@ use anyhow::anyhow; -use common::metrics::CPUBreakdown; +use common::metrics::CpuBreakdown; use std::collections::HashMap; #[derive(Default, Debug, Clone, Copy)] -struct CPUValues { +struct CpuValues { pub user: u64, pub nice: u64, pub system: u64, @@ -16,7 +16,7 @@ struct CPUValues { pub guest_nice: u64, } -impl CPUValues { +impl CpuValues { pub fn total(&self) -> u64 { self.user + self.nice @@ -31,18 +31,18 @@ impl CPUValues { } } -pub struct CPUStatReader { - previous_stats: HashMap, +pub struct CpuStatReader { + previous_stats: HashMap, } -impl CPUStatReader { +impl CpuStatReader { pub fn new() -> Self { Self { previous_stats: HashMap::new(), } } - pub fn read_global_cpu_stats(&mut self) -> anyhow::Result { + pub fn read_global_cpu_stats(&mut self) -> anyhow::Result { let content = std::fs::read_to_string("/proc/stat").unwrap(); let cpu_line = content @@ -53,7 +53,7 @@ impl CPUStatReader { self.parse_cpu_line(cpu_line) } - fn parse_cpu_line(&mut self, line: &str) -> anyhow::Result { + fn parse_cpu_line(&mut self, line: &str) -> anyhow::Result { let mut parts = line.split_whitespace(); let cpu_name = parts @@ -61,7 +61,7 @@ impl CPUStatReader { .ok_or_else(|| anyhow!("missing cpu name"))? .to_string(); - let mut values = CPUValues::default(); + let mut values = CpuValues::default(); let mut fields = [ &mut values.user, &mut values.nice, @@ -93,22 +93,25 @@ impl CPUStatReader { fn calculate_percentages( &self, - current: &CPUValues, - previous: Option, - ) -> CPUBreakdown { + current: &CpuValues, + previous: Option, + ) -> CpuBreakdown { let Some(prev) = previous else { - return CPUBreakdown::default(); + return CpuBreakdown::default(); }; let total_delta = current.total().saturating_sub(prev.total()) as f32; if total_delta <= 0.0 { - return CPUBreakdown::default(); + return CpuBreakdown::default(); } - let calculate_pct = - |current: u64, prev: u64| (current.saturating_sub(prev) as f32 / total_delta) * 100.0; + let idle = current.idle.saturating_sub(prev.idle) as f32 / total_delta; + let total = 1.0 - idle as f32; - CPUBreakdown { + let calculate_pct = + |curr: u64, prev: u64| total * (curr.saturating_sub(prev) as f32 / total_delta) * 100.0; + + CpuBreakdown { system: calculate_pct(current.system, prev.system), user: calculate_pct(current.user, prev.user), idle: calculate_pct(current.idle, prev.idle), diff --git a/common/src/metrics.rs b/common/src/metrics.rs index 4ff4c00..b1107d2 100644 --- a/common/src/metrics.rs +++ b/common/src/metrics.rs @@ -22,11 +22,11 @@ pub struct SystemInfo { pub struct CPU { pub usage: f32, pub threads: usize, - pub breakdown: CPUBreakdown, + pub breakdown: CpuBreakdown, } #[derive(Debug, Clone, Serialize, Deserialize, Default)] -pub struct CPUBreakdown { +pub struct CpuBreakdown { pub system: f32, pub user: f32, pub idle: f32,