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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
//! T20: how deep the CPUs that take the data NICs' interrupts may sleep
//! (cpuidle), and the hot CPUs that may sleep at all.
//!
//! Reads `/sys/devices/system/cpu` only (unprivileged, visible from a pod).
//! `/dev/cpu_dma_latency` is never opened: opening it registers a
//! `pm_qos` request, which a read-only check must not do.

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

/// The deepest exit latency (µs) T20 accepts on a CPU that takes a data
/// NIC's interrupts: C1's. On `intel_idle` C1E (2 µs) is the first state
/// past it.
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)";

/// One cpuidle state of a CPU (`/sys/devices/system/cpu/cpu<N>/cpuidle/state<i>`).
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct IdleState {
    /// `i` in `state<i>`: 0 is the shallowest (`POLL` on x86).
    pub index: u32,
    /// Its name (`POLL`, `C1`, `C1E`, `C6`).
    pub name: String,
    /// Its exit latency (µs): the longest a wake from it takes.
    pub exit_latency_us: u64,
    /// Whether it is disabled (`disable` 1): the governor never picks it.
    pub disabled: bool,
}

/// A CPU's resume latency limit (`pm_qos`)
/// (`/sys/devices/system/cpu/cpu<N>/power/pm_qos_resume_latency_us`).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ResumeLimit {
    /// `0`, or the file absent: no limit.
    None,
    /// `n/a`: no latency tolerated, polling only.
    PollOnly,
    /// The governor keeps to states whose exit latency is at most this (µs).
    Us(u64),
}

impl ResumeLimit {
    /// Whether a state with exit latency `us` may be entered under it.
    #[must_use]
    pub const fn allows(self, us: u64) -> bool {
        match self {
            Self::None => true,
            Self::PollOnly => false,
            Self::Us(limit) => us <= limit,
        }
    }
}

/// The cpuidle driver (`intel_idle`, `acpi_idle`, `haltpoll`), or `None`
/// when there is none (`none`, or a kernel without cpuidle: the hypervisor's
/// or the architecture's idle, such as Graviton's WFI).
///
/// # Errors
/// `/sys/devices/system/cpu` cannot be read, or the driver file cannot.
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",
        })
}

/// CPU `cpu`'s cpuidle states, shallowest first; none when it has no
/// `cpuidle` directory.
///
/// # Errors
/// A state's files cannot be read or do not parse.
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)
}

/// CPU `cpu`'s resume latency limit (`pm_qos`).
///
/// # Errors
/// The file is there but cannot be read or does not parse.
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",
        }),
    }
}

/// The CPUs the interrupts of `nics` land on (each IRQ's effective
/// affinity, else its `smp_affinity_list`), each with its IRQs' names; one
/// unknown finding a NIC that cannot be mapped.
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
}

/// `C1E (exit 2 µs), C6 (exit 190 µs)`.
fn states_text(states: &[&IdleState]) -> String {
    states
        .iter()
        .map(|s| format!("{} (exit {} µs)", s.name, s.exit_latency_us))
        .collect::<Vec<_>>()
        .join(", ")
}

/// The node-side fixes for CPU `cpu` whose states `deep` are too deep.
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 is AMD's usual driver (and Intel's when intel_idle is not
        // built in): the same exit-latency cap, a different module name.
        "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)"
    )
}

/// What T20 found on one CPU.
enum CpuVerdict {
    /// States deeper than C1 it may enter.
    Deep(Vec<IdleState>),
    /// None: its deepest usable state, and the `pm_qos` limit if that is what
    /// keeps it there.
    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))
    }
}

/// `C1 (exit 1 µs)`, `polling only`, with the `pm_qos` limit when set.
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
}

/// T20 on one CPU: `Ok(Some)` its finding (a warning, information, or
/// unknown); `Err(line)` its ok line (a CPU that sleeps no deeper than C1),
/// kept for the summary.
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)
    ))
}

/// T20: the CPUs that take the data NICs' interrupts may not sleep deeper
/// than C1 ([`T20_MAX_EXIT_LATENCY_US`]), and a hot CPU that is not
/// busy-spinning is reported (information) when it may.
///
/// An interrupt that finds its CPU in a deep idle state waits for the wake
/// before its handler and softirq run: interrupt-driven receive (no busy
/// polling, or busy polling that misses) pays that on every packet that
/// arrives while the CPU sleeps. A CPU's usable states are the enabled ones
/// within its `pm_qos_resume_latency_us`. Per CPU: a warning for an IRQ CPU
/// with a deeper usable state; information for an IRQ CPU on `acpi_idle`
/// whose deepest is C1 (nothing to cap) and for a hot CPU of a role that
/// sleeps ([`Role::spins`](crate::Role::spins) false: the conductor, an EAL
/// lcore) with a deeper state; busy-spinning CPUs are skipped, since they
/// never idle while they spin. No cpuidle driver (Graviton, a hypervisor
/// that owns idle) is ok.
#[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
}