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
//! What kind of host this is, for the wording that differs per platform
//! (Graviton has no SMT; Intel and AMD idle differently).

use crate::host::{Host, first_line};

/// The CPU architecture, from `/proc/cpuinfo`.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Arch {
    /// x86-64 (`vendor_id` lines).
    X86_64,
    /// 64-bit Arm (`CPU implementer` lines).
    Aarch64,
    /// Neither, or not readable.
    Unknown,
}

/// The CPU vendor, from `/proc/cpuinfo`'s `vendor_id` (x86) or
/// `CPU implementer` (Arm).
///
/// Checks branch on it where the vendors differ: the instance family T26
/// matches, and the idle driver T20's fix names (Intel's `intel_idle`,
/// AMD's usual `acpi_idle`).
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Vendor {
    /// `GenuineIntel`.
    Intel,
    /// `AuthenticAMD`.
    Amd,
    /// 64-bit Arm (`CPU implementer`, Graviton `0x41` included).
    Arm,
    /// Another vendor, or not readable.
    Unknown,
}

impl Vendor {
    /// `Intel`, `AMD`, `Arm` or `unknown`.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Intel => "Intel",
            Self::Amd => "AMD",
            Self::Arm => "Arm",
            Self::Unknown => "unknown",
        }
    }
}

/// The cloud, from the DMI system vendor.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Cloud {
    /// `Amazon EC2`.
    Aws,
    /// `Microsoft Corporation` (Azure, or Hyper-V elsewhere).
    Azure,
    /// Another vendor, or none readable.
    Other,
}

/// The host's architecture, vendor, cloud and instance type.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Platform {
    /// The CPU architecture.
    pub arch: Arch,
    /// The CPU vendor.
    pub vendor: Vendor,
    /// The cloud.
    pub cloud: Cloud,
    /// The DMI product name: the instance type on AWS Nitro
    /// (`c9g.xlarge`), `Virtual Machine` on Azure.
    pub product: Option<String>,
}

impl Platform {
    /// Reads `/proc/cpuinfo` and `/sys/class/dmi/id/{sys_vendor,product_name}`.
    /// What cannot be read is [`Arch::Unknown`], [`Vendor::Unknown`] or
    /// [`Cloud::Other`]. The platform changes wording and severity; only T26
    /// fires on it (a vendor that does not match the declared family), and
    /// T26 reports [`Vendor::Unknown`] as unknown, never as a match.
    #[must_use]
    pub fn detect(host: &Host) -> Self {
        let cpuinfo = host.read("/proc/cpuinfo").ok();
        let arch = cpuinfo.as_deref().map_or(Arch::Unknown, arch_of);
        let vendor = cpuinfo.as_deref().map_or(Vendor::Unknown, vendor_of);
        let dmi = host
            .read("/sys/class/dmi/id/sys_vendor")
            .map(|t| first_line(&t))
            .unwrap_or_default();
        let cloud = match dmi.as_str() {
            "Amazon EC2" => Cloud::Aws,
            "Microsoft Corporation" => Cloud::Azure,
            _ => Cloud::Other,
        };
        let product = host
            .read("/sys/class/dmi/id/product_name")
            .map(|t| first_line(&t))
            .ok()
            .filter(|p| !p.is_empty());
        Self {
            arch,
            vendor,
            cloud,
            product,
        }
    }

    /// An AWS Graviton instance: 64-bit Arm on EC2. One thread per core
    /// (no SMT).
    #[must_use]
    pub fn is_graviton(&self) -> bool {
        self.arch == Arch::Aarch64 && self.cloud == Cloud::Aws
    }
}

pub fn arch_of(cpuinfo: &str) -> Arch {
    for line in cpuinfo.lines() {
        let key = line.split(':').next().unwrap_or_default().trim();
        match key {
            "vendor_id" => return Arch::X86_64,
            "CPU implementer" => return Arch::Aarch64,
            _ => {}
        }
    }
    Arch::Unknown
}

/// The CPU vendor from `/proc/cpuinfo`: `GenuineIntel`, `AuthenticAMD`, or
/// any `CPU implementer` (Arm, Graviton `0x41` included).
pub fn vendor_of(cpuinfo: &str) -> Vendor {
    for line in cpuinfo.lines() {
        let mut parts = line.splitn(2, ':');
        let key = parts.next().unwrap_or_default().trim();
        let value = parts.next().unwrap_or_default().trim();
        match key {
            "vendor_id" => {
                return match value {
                    "GenuineIntel" => Vendor::Intel,
                    "AuthenticAMD" => Vendor::Amd,
                    _ => Vendor::Unknown,
                };
            }
            "CPU implementer" => return Vendor::Arm,
            _ => {}
        }
    }
    Vendor::Unknown
}