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
//! The one switch over every host-check warning: `ERGON_HOST_CHECKS`.
//!
//! The checks always run and their results are always returned. The switch
//! only says how they are reported: `off` drops the warning lines, `on`
//! (the default) prints them, `require` makes a failing check an error at
//! start. Every warning line that is printed ends with how to silence it.
//!
//! `require` is a readiness gate, not a blanket refusal. It refuses on a
//! warning (a check fired) and on an unproven check whose job is to prove a
//! property of this pod or process ([`unknown_stops_require`]): starting on an
//! unproven isolation claim is the false pass the check exists to stop. An
//! unknown from a check whose subsystem the node may lack (`memory.high`, a
//! NUMA node, a NIC knob) is printed and allowed through, because refusing
//! there would stop every start on such a host.

use std::fmt;

use crate::finding::Finding;

/// The one switch over every host-check warning.
pub const HOST_CHECKS_ENV: &str = "ERGON_HOST_CHECKS";

/// What every printed warning line ends with.
pub const SILENCE: &str = "(ERGON_HOST_CHECKS=off silences host-check warnings)";

/// The checks whose unknown result does not stop a
/// [`HostCheckPolicy::Require`] start. Each reads a subsystem the node may
/// not have (THP, tmpfs, NUMA, `memory.high`, a NIC knob, cpufreq, the time
/// daemon a pod cannot see, or node-wide noise such as debugfs, other PID
/// namespaces and steal accounting where the node lets a process see it), so
/// missing evidence means a host without it, not a gap in a proof.
const SUBSYSTEM_CHECKS: [&str; 23] = [
    "T3", "T4", "T5", "T6", "T11", "T13", "T14", "T15", "T16", "T17", "T18", "T19", "T20", "T21",
    "T25", "K6", "K7", "N1", "N2", "N3", "N4", "N5", "M4",
];

/// Whether an unknown result of check `id` stops a
/// [`HostCheckPolicy::Require`] start.
///
/// A check that proves a property of this pod or process (its CPUs are its
/// own and isolated, no CPU quota, its helpers have a core, its pinning map
/// matches the declared instance family) blocks when it cannot be answered,
/// because unproven is not fine. A check of a subsystem the node may not
/// have (T3, T4, T5, T6, T11, T13-T21, T25, K6, K7, N1-N5, M4) does not.
/// Every other id blocks, a caller's own included, so an unclassified id
/// fails closed.
#[must_use]
pub fn unknown_stops_require(id: &str) -> bool {
    !SUBSYSTEM_CHECKS.contains(&id)
}

/// What [`HOST_CHECKS_ENV`] is set to.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Default)]
pub enum HostCheckPolicy {
    /// Print no warning line. The results are still returned, and info and
    /// unknown lines are still printed.
    Off,
    /// Print the warning lines (the default).
    #[default]
    On,
    /// Print them, and make a warning an error at start. An unproven check is
    /// an error too, but only where the check must prove something: see
    /// [`unknown_stops_require`].
    Require,
}

/// What a caller does with a host-check policy or its report.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum HostCheckError {
    /// [`HOST_CHECKS_ENV`] is set to something other than `off`, `on`/`warn`
    /// or `require`.
    Invalid {
        /// The value it is set to.
        value: String,
    },
    /// [`HostCheckPolicy::Require`] and a check warned, or could not prove a
    /// property that check must prove: the start must fail.
    Warnings {
        /// How many findings warned or were left unproven.
        count: usize,
        /// Their check ids, joined with `, `.
        ids: String,
    },
}

impl fmt::Display for HostCheckError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Invalid { value } => write!(
                f,
                "{HOST_CHECKS_ENV}={value}: set {HOST_CHECKS_ENV} to off, on or require (off prints \
                 no host-check warning line; require refuses to start on a warning, or on a check \
                 that could not be proven)"
            ),
            Self::Warnings { count, ids } => write!(
                f,
                "{HOST_CHECKS_ENV}=require and {count} host check(s) warned or could not be \
                 proven ({ids}), printed above: fix them, or set {HOST_CHECKS_ENV}=off to start \
                 anyway"
            ),
        }
    }
}

impl std::error::Error for HostCheckError {}

impl HostCheckPolicy {
    /// `off`, `on` (or `warn`, the same as `on`) or `require`, any case.
    ///
    /// # Errors
    ///
    /// [`HostCheckError::Invalid`] when `value` is none of those.
    pub fn parse(value: &str) -> Result<Self, HostCheckError> {
        match value.trim().to_ascii_lowercase().as_str() {
            "off" => Ok(Self::Off),
            "on" | "warn" => Ok(Self::On),
            "require" => Ok(Self::Require),
            _ => Err(HostCheckError::Invalid {
                value: value.to_owned(),
            }),
        }
    }

    /// From [`HOST_CHECKS_ENV`]'s value: [`Self::On`] when it is unset
    /// (`None`), else [`Self::parse`].
    ///
    /// # Errors
    ///
    /// [`HostCheckError::Invalid`] when it is set to none of `off`, `on`,
    /// `warn` or `require`.
    pub fn from_var(value: Option<&str>) -> Result<Self, HostCheckError> {
        value.map_or(Ok(Self::On), Self::parse)
    }

    /// From the process environment: [`HOST_CHECKS_ENV`], [`Self::On`] when
    /// it is unset. No other variable is read.
    ///
    /// # Errors
    ///
    /// [`HostCheckError::Invalid`] when it is set to none of `off`, `on`,
    /// `warn` or `require`, or is not valid Unicode.
    pub fn from_env() -> Result<Self, HostCheckError> {
        match std::env::var(HOST_CHECKS_ENV) {
            Ok(v) => Self::from_var(Some(&v)),
            Err(std::env::VarError::NotPresent) => Self::from_var(None),
            Err(std::env::VarError::NotUnicode(_)) => Err(HostCheckError::Invalid {
                value: "not valid Unicode".to_owned(),
            }),
        }
    }

    /// The warning lines of `findings` under this policy, as the caller
    /// prints them (`<prefix> warning: …`), each ending with [`SILENCE`].
    /// [`Self::Off`] drops them; the findings themselves are untouched.
    #[must_use]
    pub fn warn_lines(&self, prefix: &str, findings: &[Finding]) -> Vec<String> {
        if *self == Self::Off {
            return Vec::new();
        }
        findings
            .iter()
            .filter_map(|f| warn_line(prefix, f))
            .collect()
    }

    /// Every line to print for `findings` under this policy: the info and
    /// unknown lines always, and the warning lines (each ending with
    /// [`SILENCE`]) only when this is not [`Self::Off`]. An ok finding
    /// prints nothing.
    #[must_use]
    pub fn lines(&self, prefix: &str, findings: &[Finding]) -> Vec<String> {
        findings
            .iter()
            .filter(|f| *self != Self::Off || !f.is_warning())
            .filter_map(|f| host_check_line(prefix, f))
            .collect()
    }

    /// Under [`Self::Require`], the error a start must fail with when any
    /// finding is a warning, or is an unknown of a check that must be proven
    /// ([`unknown_stops_require`]).
    /// An unknown of a check whose subsystem the node may not have is
    /// reported and allowed through: that is a real host, not a gap.
    #[must_use]
    pub fn refusal(&self, findings: &[Finding]) -> Option<HostCheckError> {
        if *self != Self::Require {
            return None;
        }
        let mut ids: Vec<&str> = Vec::new();
        let mut count = 0;
        for f in findings
            .iter()
            .filter(|f| f.is_warning() || (f.is_unknown() && unknown_stops_require(f.id)))
        {
            count += 1;
            let id = f.id;
            if !ids.contains(&id) {
                ids.push(id);
            }
        }
        if count == 0 {
            None
        } else {
            Some(HostCheckError::Warnings {
                count,
                ids: ids.join(", "),
            })
        }
    }
}

/// The one warning line of `f`, if it is a warning: `<prefix> warning: <the
/// finding> (ERGON_HOST_CHECKS=off silences host-check warnings)`.
#[must_use]
pub fn warn_line(prefix: &str, f: &Finding) -> Option<String> {
    if f.is_warning() {
        Some(format!("{prefix} warning: {f} {SILENCE}"))
    } else {
        None
    }
}

/// The one line of `f` as the caller prints it: `<prefix> warning: …` with
/// [`SILENCE`] on a warning, `<prefix> info: …` on an info finding or an
/// unknown, and nothing on an ok.
#[must_use]
pub fn host_check_line(prefix: &str, f: &Finding) -> Option<String> {
    if f.is_warning() {
        warn_line(prefix, f)
    } else if f.is_unknown() || f.is_finding() {
        Some(format!("{prefix} info: {f}"))
    } else {
        None
    }
}