ergon-tune 0.1.45

Start-up and housekeeping checks of a host's low-latency settings (CPU isolation, NIC interrupts, transparent hugepages, the Aeron directory, NUMA, scheduling policy, SMT siblings, a CPU quota and its throttling, how CLOCK_REALTIME is kept and the kernel's clock source, background noise on the hot CPUs (unbound workqueues, compaction, other processes' tasks, kernel instrumentation, KSM, steal), a hot thread's page faults, the IRQ CPUs' idle depth and TCP autocorking, and with `nic` the NIC's moderation, deferral, VF and ring): pure functions returning findings, with no threads or statics, and one audited unsafe call (a read-only adjtimex)
Documentation
//! T25: the CPU frequency governor, so the hot cores are not left in
//! `powersave` or another governor that throttles them under load.
//!
//! Reads `/proc/cpuinfo` and `/sys/devices/system/cpu` only (unprivileged,
//! visible from a pod).

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";

/// The governor T25 requires: the cores run at their top frequency rather
/// than scaling down between packets.
const REQUIRED: &str = "performance";

/// T25: every online CPU with a cpufreq governor is in `performance`.
///
/// Another governor (`powersave`, `schedutil`, `ondemand`) drops a CPU's
/// frequency between bursts, so a hot core's next busy-spin pays the ramp
/// (not measured here). The governor is read on every architecture. With no
/// cpufreq on any CPU, arm64 is ok (a fixed frequency, as on Graviton) and
/// any other architecture is unknown: it cannot be judged.
#[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(", ")
        ),
    )
}