use std::collections::BTreeMap;
use std::fmt::Write as _;
use crate::cpuset::CpuSet;
use crate::finding::{Finding, HotSet, Severity};
use crate::host::{Host, ReadError, first_line};
use crate::net::{Nic, nic_irqs};
pub const T20_MAX_EXIT_LATENCY_US: u64 = 1;
const CPUIDLE: &str = "/sys/devices/system/cpu/cpuidle";
const T20_EVIDENCE: &str = "the IRQ CPU's idle depth; not measured here; Linux \
Documentation/admin-guide/pm/cpuidle.rst (state<N>/disable, pm_qos_resume_latency_us, \
/dev/cpu_dma_latency) and intel_idle.rst (max_cstate)";
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct IdleState {
pub index: u32,
pub name: String,
pub exit_latency_us: u64,
pub disabled: bool,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ResumeLimit {
None,
PollOnly,
Us(u64),
}
impl ResumeLimit {
#[must_use]
pub const fn allows(self, us: u64) -> bool {
match self {
Self::None => true,
Self::PollOnly => false,
Self::Us(limit) => us <= limit,
}
}
}
pub fn cpuidle_driver(host: &Host) -> Result<Option<String>, ReadError> {
match host.read_optional(&format!("{CPUIDLE}/current_driver")) {
Ok(text) => Ok(text
.map(|t| first_line(&t))
.filter(|d| !d.is_empty() && d != "none")),
Err(_) if host.is_dir("/sys/devices/system/cpu") && !host.exists(CPUIDLE) => Ok(None),
Err(e) => Err(e),
}
}
fn number(host: &Host, abs: &str) -> Result<u64, ReadError> {
host.read_line(abs)?
.parse()
.map_err(|_| ReadError::Malformed {
path: abs.to_owned(),
detail: "not a number",
})
}
pub fn cpu_idle_states(host: &Host, cpu: u32) -> Result<Vec<IdleState>, ReadError> {
let dir = format!("/sys/devices/system/cpu/cpu{cpu}/cpuidle");
if !host.is_dir(&dir) {
return Ok(Vec::new());
}
let mut out = Vec::new();
for entry in host.list(&dir)? {
let Some(index) = entry
.strip_prefix("state")
.and_then(|i| i.parse::<u32>().ok())
else {
continue;
};
let d = format!("{dir}/{entry}");
out.push(IdleState {
index,
name: host.read_line(&format!("{d}/name"))?,
exit_latency_us: number(host, &format!("{d}/latency"))?,
disabled: number(host, &format!("{d}/disable"))? != 0,
});
}
out.sort_by_key(|s| s.index);
Ok(out)
}
pub fn resume_limit(host: &Host, cpu: u32) -> Result<ResumeLimit, ReadError> {
let path = format!("/sys/devices/system/cpu/cpu{cpu}/power/pm_qos_resume_latency_us");
let text = match host.read_optional(&path) {
Ok(Some(t)) => first_line(&t),
Ok(None) | Err(ReadError::Io { .. }) => return Ok(ResumeLimit::None),
Err(e) => return Err(e),
};
if text == "n/a" {
return Ok(ResumeLimit::PollOnly);
}
match text.parse::<u64>() {
Ok(0) => Ok(ResumeLimit::None),
Ok(us) => Ok(ResumeLimit::Us(us)),
Err(_) => Err(ReadError::Malformed {
path,
detail: "not a number or n/a",
}),
}
}
fn irq_cpus(host: &Host, nics: &[Nic], out: &mut Vec<Finding>) -> BTreeMap<u32, Vec<String>> {
let mut cpus: BTreeMap<u32, Vec<String>> = BTreeMap::new();
for nic in nics {
let irqs = match nic_irqs(host, nic) {
Ok(v) => v,
Err(e) => {
out.push(Finding::unknown(
"T20",
Severity::Warn,
T20_EVIDENCE,
format!("cannot tell which CPUs take {nic}'s interrupts"),
e,
));
continue;
}
};
for irq in irqs {
let lands = host
.read_cpu_list(&format!("/proc/irq/{}/effective_affinity_list", irq.irq))
.ok()
.filter(|c| !c.is_empty())
.or_else(|| {
host.read_cpu_list(&format!("/proc/irq/{}/smp_affinity_list", irq.irq))
.ok()
})
.unwrap_or_default();
let name = irq.action.clone().map_or_else(
|| format!("IRQ {}", irq.irq),
|a| format!("{a} (IRQ {})", irq.irq),
);
for c in lands.iter() {
cpus.entry(c).or_default().push(name.clone());
}
}
}
cpus
}
fn states_text(states: &[&IdleState]) -> String {
states
.iter()
.map(|s| format!("{} (exit {} µs)", s.name, s.exit_latency_us))
.collect::<Vec<_>>()
.join(", ")
}
fn fixes(cpu: u32, deep: &[&IdleState], driver: &str) -> String {
let mut per_cpu = deep
.iter()
.map(|s| {
format!(
"echo 1 > /sys/devices/system/cpu/cpu{cpu}/cpuidle/state{}/disable",
s.index
)
})
.collect::<Vec<_>>()
.join("; ");
per_cpu = format!("`{per_cpu}`");
let boot = match driver {
"intel_idle" => " at boot `intel_idle.max_cstate=1` (every CPU);",
"acpi_idle" => {
" at boot `processor.max_cstate=1` (every CPU; AMD's acpi_idle, or \
Intel's when intel_idle is not in use);"
}
_ => "",
};
format!(
"Fix (node, root; one of): {per_cpu} (until reboot); `echo {T20_MAX_EXIT_LATENCY_US} > \
/sys/devices/system/cpu/cpu{cpu}/power/pm_qos_resume_latency_us` (this CPU, until \
reboot);{boot} or a process holding /dev/cpu_dma_latency open with \
{T20_MAX_EXIT_LATENCY_US} written to it (every CPU, while held). Kubernetes: a node \
setting, never the pod's (/sys is read-only in a pod): the boot line, or a privileged \
DaemonSet that writes it; a pod can hold /dev/cpu_dma_latency only privileged, with the \
device mounted. A process already holding /dev/cpu_dma_latency is not seen here \
(opening it registers a request, so it is not read)"
)
}
enum CpuVerdict {
Deep(Vec<IdleState>),
Shallow(Option<IdleState>, ResumeLimit),
}
fn judge(host: &Host, cpu: u32) -> Result<CpuVerdict, ReadError> {
let states = cpu_idle_states(host, cpu)?;
let limit = resume_limit(host, cpu)?;
let usable: Vec<&IdleState> = states
.iter()
.filter(|s| !s.disabled && limit.allows(s.exit_latency_us))
.collect();
let deep: Vec<IdleState> = usable
.iter()
.filter(|s| s.exit_latency_us > T20_MAX_EXIT_LATENCY_US)
.map(|s| (*s).clone())
.collect();
if deep.is_empty() {
Ok(CpuVerdict::Shallow(
usable.last().map(|s| (*s).clone()),
limit,
))
} else {
Ok(CpuVerdict::Deep(deep))
}
}
fn shallow_text(deepest: Option<&IdleState>, limit: ResumeLimit) -> String {
let mut s = deepest.map_or_else(
|| "polling only".to_owned(),
|d| {
if d.exit_latency_us == 0 {
"polling only".to_owned()
} else {
format!("at most {} (exit {} µs)", d.name, d.exit_latency_us)
}
},
);
match limit {
ResumeLimit::Us(us) => {
let _ = write!(s, ", pm_qos_resume_latency_us {us}");
}
ResumeLimit::PollOnly => s.push_str(", pm_qos_resume_latency_us n/a"),
ResumeLimit::None => {}
}
s
}
fn one_cpu(
host: &Host,
cpu: u32,
cpus: &HotSet,
takes: Option<&[String]>,
driver: &str,
) -> Result<Option<Finding>, String> {
let id = "T20";
let who = takes.map_or_else(
|| cpus.describe(cpu),
|names| {
let mut list = names.iter().take(3).cloned().collect::<Vec<_>>().join(", ");
if names.len() > 3 {
list.push_str(", …");
}
format!("{} takes {list} and", cpus.describe(cpu))
},
);
let verdict = match judge(host, cpu) {
Ok(v) => v,
Err(e) => {
return Ok(Some(Finding::unknown(
id,
Severity::Warn,
T20_EVIDENCE,
format!("cannot tell how deep {} may sleep", cpus.describe(cpu)),
e,
)));
}
};
let (deepest, limit) = match verdict {
CpuVerdict::Deep(deep) => {
let deep: Vec<&IdleState> = deep.iter().collect();
let fix = fixes(cpu, &deep, driver);
let states = states_text(&deep);
return Ok(Some(if takes.is_some() {
Finding::found(
id,
Severity::Warn,
T20_EVIDENCE,
format!(
"{who} may sleep in {states} ({driver}): an interrupt that finds it there \
waits for the wake, up to the state's exit latency, before its handler \
and softirq run. {fix}"
),
)
} else {
Finding::found(
id,
Severity::Info,
T20_EVIDENCE,
format!(
"{who} may sleep in {states} ({driver}): it matters only when the thread \
there sleeps (a backoff idle strategy, a blocking wait), which then \
wakes up to the exit latency late. {fix}"
),
)
}));
}
CpuVerdict::Shallow(deepest, limit) => (deepest, limit),
};
let halts = deepest.as_ref().is_some_and(|d| d.exit_latency_us > 0);
if takes.is_some() && halts && driver == "acpi_idle" {
let name = deepest.as_ref().map_or("C1", |d| d.name.as_str());
let index = deepest.as_ref().map_or(1, |d| d.index);
return Ok(Some(Finding::found(
id,
Severity::Info,
T20_EVIDENCE,
format!(
"{who} sleeps at most in {name} (acpi_idle): nothing deeper to cap. Under a \
hypervisor C1 is a halt the hypervisor wakes. Polling only removes the halt as well, at a \
whole core's power: `echo 1 > \
/sys/devices/system/cpu/cpu{cpu}/cpuidle/state{index}/disable`"
),
)));
}
Err(format!(
"{}: {}",
cpus.describe(cpu),
shallow_text(deepest.as_ref(), limit)
))
}
#[must_use]
pub fn t20_irq_idle(host: &Host, nics: &[Nic], cpus: &HotSet) -> Vec<Finding> {
let (id, sev) = ("T20", Severity::Warn);
let driver = match cpuidle_driver(host) {
Ok(Some(d)) => d,
Ok(None) => {
return vec![Finding::ok(
id,
sev,
T20_EVIDENCE,
"no cpuidle driver (cpuidle/current_driver none or absent): idle is the \
hypervisor's or the architecture's (Graviton: WFI), nothing the OS caps"
.to_owned(),
)];
}
Err(e) => {
return vec![Finding::unknown(
id,
sev,
T20_EVIDENCE,
"cannot tell the cpuidle driver".to_owned(),
e,
)];
}
};
let mut out = Vec::new();
let irq = irq_cpus(host, nics, &mut out);
let spinning = cpus.spinning();
let sleepers: CpuSet = cpus.all().difference(&spinning);
let checked: CpuSet = irq.keys().copied().collect::<CpuSet>().union(&sleepers);
let mut ok = Vec::new();
for cpu in checked.iter() {
match one_cpu(host, cpu, cpus, irq.get(&cpu).map(Vec::as_slice), &driver) {
Ok(Some(f)) => out.push(f),
Ok(None) => {}
Err(line) => ok.push(line),
}
}
if out.iter().all(|f| !f.is_finding() && !f.is_unknown()) {
let message = if ok.is_empty() {
format!(
"no CPU to check: no interrupt of the data NICs found, and every hot CPU \
busy-spins ({driver})"
)
} else {
format!("{} ({driver})", ok.join("; "))
};
out.push(Finding::ok(id, sev, T20_EVIDENCE, message));
}
out
}