use std::collections::BTreeMap;
use crate::cpuset::CpuSet;
use crate::finding::{Finding, Severity};
use crate::host::{Host, ReadError};
use crate::platform::{Arch, Platform};
const T25_EVIDENCE: &str = "Linux Documentation/admin-guide/pm/cpufreq.rst (scaling_governor, \
cpufreq.default_governor); not measured here";
const REQUIRED: &str = "performance";
#[must_use]
pub fn t25_frequency(host: &Host) -> Finding {
let id = "T25";
let sev = Severity::Warn;
let cannot = || "cannot tell the CPU frequency governor".to_owned();
let online = match host.read_cpu_list("/sys/devices/system/cpu/online") {
Ok(c) => c,
Err(e) => return Finding::unknown(id, sev, T25_EVIDENCE, cannot(), e),
};
let mut by_governor: BTreeMap<String, CpuSet> = BTreeMap::new();
let mut checked = 0usize;
for cpu in online.iter() {
match host.read_line(&format!(
"/sys/devices/system/cpu/cpu{cpu}/cpufreq/scaling_governor"
)) {
Ok(g) => {
checked += 1;
by_governor.entry(g).or_default().insert(cpu);
}
Err(ReadError::Io {
kind: std::io::ErrorKind::NotFound,
..
}) => {}
Err(e) => return Finding::unknown(id, sev, T25_EVIDENCE, cannot(), e),
}
}
if checked == 0 {
if Platform::detect(host).arch == Arch::Aarch64 && !online.is_empty() {
return Finding::ok(
id,
sev,
T25_EVIDENCE,
"arm64: fixed frequency (no cpufreq governor on any CPU, as on Graviton)"
.to_owned(),
);
}
let reason = if online.is_empty() {
"no online CPU"
} else {
"no cpufreq scaling_governor on any online CPU"
};
return Finding::unknown(id, sev, T25_EVIDENCE, cannot(), reason);
}
by_governor.remove(REQUIRED);
if by_governor.is_empty() {
return Finding::ok(
id,
sev,
T25_EVIDENCE,
format!("{checked} online CPU(s) in {REQUIRED}"),
);
}
let bad: Vec<String> = by_governor
.iter()
.map(|(g, cpus)| format!("{g} on CPU {cpus}"))
.collect();
Finding::found(
id,
sev,
T25_EVIDENCE,
format!(
"{}: a governor other than {REQUIRED} drops a CPU's frequency between bursts, so a \
hot core's next busy-spin pays the ramp (not measured here). Fix (node, root): \
`cpupower frequency-set -g {REQUIRED}` (until reboot), or boot with \
`cpufreq.default_governor={REQUIRED}` (Intel, AMD and Arm cpufreq drivers alike); \
on Kubernetes a node setting (a privileged DaemonSet or the node image), never the \
pod's",
bad.join(", ")
),
)
}