use super::governor::CpuGovernor;
use super::info::CpuFrequencyInfo;
#[derive(Debug, Clone)]
pub struct FrequencyReading {
pub cpus: Vec<CpuFrequencyInfo>,
pub timestamp_ns: u64,
}
impl FrequencyReading {
pub fn average_mhz(&self) -> f64 {
if self.cpus.is_empty() {
return 0.0;
}
let total: f64 = self.cpus.iter().map(|c| c.current_mhz()).sum();
total / self.cpus.len() as f64
}
pub fn min_mhz(&self) -> f64 {
self.extremum_mhz(f64::min)
}
pub fn max_mhz(&self) -> f64 {
self.extremum_mhz(f64::max)
}
pub fn variance_mhz(&self) -> f64 {
self.max_mhz() - self.min_mhz()
}
fn extremum_mhz(&self, cmp: fn(f64, f64) -> f64) -> f64 {
self.cpus
.iter()
.map(|c| c.current_mhz())
.reduce(cmp)
.unwrap_or(0.0)
}
pub fn uniform_governor(&self) -> bool {
if self.cpus.is_empty() {
return true;
}
let first = self.cpus[0].governor;
self.cpus.iter().all(|c| c.governor == first)
}
pub fn common_governor(&self) -> CpuGovernor {
self.cpus
.first()
.map(|c| c.governor)
.unwrap_or(CpuGovernor::Unknown)
}
}