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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
//! A NIC's interrupts (T2) and NUMA node (T6), its `carrier_changes`, and
//! the host's busy-polling sysctls.

use std::fmt;

use crate::cpu::{housekeeping, isolated_cpus};
use crate::cpuset::CpuSet;
use crate::finding::{Finding, HotSet, Severity};
use crate::host::{Host, ReadError};
use crate::mem::nodes_online;

/// A NIC, by kernel interface name or PCI address.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum Nic {
    /// A kernel network interface (`ens5`, `eth1`). The interfaces below
    /// it (`lower_*`) are included: on Azure its accelerated-networking VF,
    /// under a VLAN or bond the NICs it runs on.
    Interface(String),
    /// A PCI device by address (`0000:00:06.0`): a DPDK port, bound to
    /// `igb_uio` or `vfio-pci`.
    Pci(String),
}

impl fmt::Display for Nic {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Interface(name) => f.write_str(name),
            Self::Pci(bdf) => write!(f, "PCI device {bdf}"),
        }
    }
}

/// One interrupt of a NIC.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct NicIrq {
    /// The IRQ number.
    pub irq: u32,
    /// The PCI device it belongs to.
    pub device: String,
    /// Its handler's name (`ens5-Tx-Rx-0`), from `/sys/kernel/irq/<n>/actions`.
    pub action: Option<String>,
}

impl NicIrq {
    pub(crate) fn label(&self) -> String {
        self.action.as_ref().map_or_else(
            || format!("IRQ {} of {}", self.irq, self.device),
            |a| format!("IRQ {} ({a}) of {}", self.irq, self.device),
        )
    }
}

/// An interface name the kernel could have made: 1 to 15 bytes, not `.` or
/// `..`, no `/`, `:` or whitespace (`dev_valid_name`). Keeps a name from
/// reaching outside `/sys/class/net`.
pub fn check_interface(name: &str) -> Result<(), ReadError> {
    let ok = !name.is_empty()
        && name.len() < 16
        && name != "."
        && name != ".."
        && !name
            .bytes()
            .any(|b| b == b'/' || b == b':' || b.is_ascii_whitespace());
    if ok {
        Ok(())
    } else {
        Err(ReadError::Malformed {
            path: name.to_owned(),
            detail: "not an interface name",
        })
    }
}

fn check_bdf(bdf: &str) -> Result<(), ReadError> {
    let ok = !bdf.is_empty()
        && bdf.len() <= 16
        && bdf
            .bytes()
            .all(|b| b.is_ascii_hexdigit() || b == b':' || b == b'.');
    if ok {
        Ok(())
    } else {
        Err(ReadError::Malformed {
            path: bdf.to_owned(),
            detail: "not a PCI address",
        })
    }
}

/// An interface's device: its name on its bus, the bus, and its driver.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Device {
    pub name: String,
    pub bus: Option<String>,
    pub driver: Option<String>,
}

/// `interface`'s device, or `None` when it has none (a virtual interface).
///
/// # Errors
/// No such interface, or its links cannot be read.
pub fn interface_device(host: &Host, interface: &str) -> Result<Option<Device>, ReadError> {
    check_interface(interface)?;
    let base = format!("/sys/class/net/{interface}");
    // `bonding_masters` sits beside the interfaces, a file.
    if !host.is_dir(&base) {
        return Err(host.list(&base).err().unwrap_or_else(|| ReadError::Io {
            path: base.clone(),
            kind: std::io::ErrorKind::NotFound,
        }));
    }
    let Some(name) = host.link_name(&format!("{base}/device"))? else {
        return Ok(None);
    };
    Ok(Some(Device {
        name,
        bus: host.link_name(&format!("{base}/device/subsystem"))?,
        driver: host.link_name(&format!("{base}/device/driver"))?,
    }))
}

/// The interfaces below `interface` (`lower_<name>` links: Azure's
/// accelerated-networking VF under its `hv_netvsc` interface).
pub fn lower_interfaces(host: &Host, interface: &str) -> Result<Vec<String>, ReadError> {
    check_interface(interface)?;
    Ok(host
        .list(&format!("/sys/class/net/{interface}"))?
        .iter()
        .filter_map(|n| n.strip_prefix("lower_"))
        .map(str::to_owned)
        .collect())
}

/// The error for an interface whose device is not visible.
pub fn no_device(interface: &str) -> ReadError {
    ReadError::NoDevice {
        interface: interface.to_owned(),
        detail: "no device (a virtual interface, such as a pod's veth): its NIC is not visible here",
    }
}

/// How deep [`pci_devices`] follows `lower_*` links: a VLAN on a bond of
/// Azure `hv_netvsc` interfaces over their VFs is three levels.
const LOWER_DEPTH: u32 = 4;

/// The PCI devices of `interface` and of the interfaces below it
/// (`lower_*`: an Azure VF, a VLAN's or bond's NICs), into `out`; the first
/// device seen that is not PCI, for the reason when there is none.
fn collect_pci(
    host: &Host,
    interface: &str,
    depth: u32,
    out: &mut Vec<String>,
    other: &mut Option<Device>,
) -> Result<(), ReadError> {
    match interface_device(host, interface)? {
        Some(d) if d.bus.as_deref() == Some("pci") => {
            if !out.contains(&d.name) {
                out.push(d.name);
            }
        }
        Some(d) => {
            other.get_or_insert(d);
        }
        None => {}
    }
    if depth == 0 {
        return Ok(());
    }
    for lower in lower_interfaces(host, interface)? {
        collect_pci(host, &lower, depth - 1, out, other)?;
    }
    Ok(())
}

/// The PCI devices behind `nic`: an interface's own device if it is PCI,
/// and those of the interfaces below it (`lower_*`: its Azure VF, the NICs
/// under a VLAN or bond); a PCI address as given.
pub fn pci_devices(host: &Host, nic: &Nic) -> Result<Vec<String>, ReadError> {
    match nic {
        Nic::Pci(bdf) => {
            check_bdf(bdf)?;
            let dir = format!("/sys/bus/pci/devices/{bdf}");
            if !host.exists(&dir) {
                return Err(ReadError::Io {
                    path: dir,
                    kind: std::io::ErrorKind::NotFound,
                });
            }
            Ok(vec![bdf.clone()])
        }
        Nic::Interface(name) => {
            let mut out = Vec::new();
            let mut other = None;
            collect_pci(host, name, LOWER_DEPTH, &mut out, &mut other)?;
            if !out.is_empty() {
                return Ok(out);
            }
            let Some(dev) = other else {
                return Err(no_device(name));
            };
            let detail = match dev.bus.as_deref() {
                Some("vmbus") => {
                    "no PCI device (a VMBus device with no VF): its interrupts are not in /proc/irq"
                }
                Some("virtio") => {
                    "no PCI device of its own (a virtio device: its interrupts are its parent \
                     PCI device's, which this check does not follow)"
                }
                _ => "no PCI device: its interrupts cannot be mapped",
            };
            Err(ReadError::NoDevice {
                interface: name.clone(),
                detail,
            })
        }
    }
}

/// The interrupts of `nic`'s PCI devices (an interface's VFs included), in
/// IRQ order: each device's `msi_irqs`, or its legacy `irq` when that has a
/// handler.
///
/// # Errors
/// The interface or device is not there, has no PCI device, or its files
/// cannot be read.
pub fn nic_irqs(host: &Host, nic: &Nic) -> Result<Vec<NicIrq>, ReadError> {
    let mut out = Vec::new();
    for bdf in pci_devices(host, nic)? {
        let dir = format!("/sys/bus/pci/devices/{bdf}");
        let numbers: Vec<u32> = match host.list(&format!("{dir}/msi_irqs")) {
            Ok(names) => names
                .iter()
                .map(|n| {
                    n.parse().map_err(|_| ReadError::Malformed {
                        path: format!("{dir}/msi_irqs/{n}"),
                        detail: "not an IRQ number",
                    })
                })
                .collect::<Result<_, _>>()?,
            Err(ReadError::Io {
                kind: std::io::ErrorKind::NotFound,
                ..
            }) => {
                // A legacy IRQ counts when a handler is on it. Only a
                // number with no descriptor proves there is none: an
                // unreadable `irq`, or no /sys/kernel/irq to look in, is
                // unknown, never "no interrupts".
                let legacy = host.read_i64(&format!("{dir}/irq"))?;
                match u32::try_from(legacy) {
                    Ok(n) if n > 0 => {
                        if !host.is_dir("/sys/kernel/irq") {
                            return Err(ReadError::Io {
                                path: "/sys/kernel/irq".to_owned(),
                                kind: std::io::ErrorKind::NotFound,
                            });
                        }
                        // Read, not `exists`: that is false for any error,
                        // EACCES included.
                        match host.read_line(&format!("/sys/kernel/irq/{n}/actions")) {
                            Ok(a) if !a.is_empty() => vec![n],
                            Ok(_)
                            | Err(ReadError::Io {
                                kind: std::io::ErrorKind::NotFound,
                                ..
                            }) => Vec::new(),
                            Err(e) => return Err(e),
                        }
                    }
                    _ => Vec::new(),
                }
            }
            Err(e) => return Err(e),
        };
        for irq in numbers {
            let action = host
                .read_line(&format!("/sys/kernel/irq/{irq}/actions"))
                .ok()
                .filter(|a| !a.is_empty());
            out.push(NicIrq {
                irq,
                device: bdf.clone(),
                action,
            });
        }
    }
    out.sort_by_key(|i| i.irq);
    Ok(out)
}

const T2_EVIDENCE: &str = "Linux \
     drivers/net/ethernet/amazon/ena/ena_netdev.c (ena_setup_io_intr: queue i's IRQ on CPU i \
     modulo the online CPUs, set again at every ena_up)";

/// T2 for one interrupt: a warning when its `smp_affinity_list` names a
/// busy-spinning CPU, information when only its effective affinity still
/// does, unknown when unreadable, nothing when it is clear.
fn t2_irq(
    host: &Host,
    irq: &NicIrq,
    cpus: &HotSet,
    spinning: &CpuSet,
    hk: &str,
) -> Option<Finding> {
    let path = format!("/proc/irq/{}/smp_affinity_list", irq.irq);
    let affinity = match host.read_cpu_list(&path) {
        Ok(a) => a,
        Err(e) => {
            return Some(Finding::unknown(
                "T2",
                Severity::Warn,
                T2_EVIDENCE,
                format!("cannot tell where {} may fire", irq.label()),
                e,
            ));
        }
    };
    let hits = affinity.intersection(spinning);
    if !hits.is_empty() {
        let ena = host
            .link_name(&format!("/sys/bus/pci/devices/{}/driver", irq.device))
            .ok()
            .flatten()
            .is_some_and(|d| d == "ena");
        let (reset, repin) = if ena {
            (
                " ENA re-places its queue IRQs at every interface reset (queue i on CPU i modulo \
                 the number of online CPUs, isolated ones included).",
                ", and re-pin at every link event (when carrier_changes moves)",
            )
        } else {
            ("", "")
        };
        return Some(Finding::found(
            "T2",
            Severity::Warn,
            T2_EVIDENCE,
            format!(
                "{} may fire on {}: its smp_affinity_list is {affinity}. On the loop's CPU each \
                 interrupt pauses the loop while its handler runs.{reset} Fix: `echo {hk} > \
                 /proc/irq/{}/smp_affinity_list`{repin}",
                irq.label(),
                cpus.describe_all(&hits),
                irq.irq
            ),
        ));
    }
    let effective = host
        .read_cpu_list(&format!("/proc/irq/{}/effective_affinity_list", irq.irq))
        .unwrap_or_default();
    let still = effective.intersection(spinning);
    (!still.is_empty()).then(|| {
        Finding::found(
            "T2",
            Severity::Info,
            T2_EVIDENCE,
            format!(
                "{} is still delivered to {} (effective_affinity_list {effective}) although its \
                 smp_affinity_list ({affinity}) excludes it: it moves at its next interrupt",
                irq.label(),
                cpus.describe_all(&still)
            ),
        )
    })
}

/// T2: no interrupt of `nic` may fire on a busy-spinning CPU of `cpus`.
///
/// Judged by each IRQ's `smp_affinity_list` (a warning per IRQ). An IRQ
/// whose list excludes the hot CPUs but whose `effective_affinity_list`
/// still names one is information: a VM moves an IRQ only at its next
/// interrupt.
#[must_use]
pub fn t2_nic_irqs(host: &Host, nic: &Nic, cpus: &HotSet) -> Vec<Finding> {
    let id = "T2";
    let sev = Severity::Warn;
    let spinning = cpus.spinning();
    if spinning.is_empty() {
        return vec![Finding::ok(
            id,
            sev,
            T2_EVIDENCE,
            "no busy-spinning CPU to check".to_owned(),
        )];
    }
    let irqs = match nic_irqs(host, nic) {
        Ok(v) => v,
        Err(e) => {
            return vec![Finding::unknown(
                id,
                sev,
                T2_EVIDENCE,
                format!("cannot map {nic}'s interrupts"),
                e,
            )];
        }
    };
    let isolated = isolated_cpus(host).unwrap_or_default();
    let hk = housekeeping(host, &spinning.union(&isolated));
    let mut out: Vec<Finding> = irqs
        .iter()
        .filter_map(|irq| t2_irq(host, irq, cpus, &spinning, &hk))
        .collect();
    if irqs.is_empty() {
        return vec![Finding::ok(
            id,
            sev,
            T2_EVIDENCE,
            format!(
                "{nic} has no interrupts (no msi_irqs, no legacy IRQ with a handler): nothing \
                 can fire on {}",
                cpus.describe_all(&spinning)
            ),
        )];
    }
    if out.is_empty() {
        let devices: Vec<&str> =
            irqs.iter()
                .map(|i| i.device.as_str())
                .fold(Vec::new(), |mut v, d| {
                    if !v.contains(&d) {
                        v.push(d);
                    }
                    v
                });
        out.push(Finding::ok(
            id,
            sev,
            T2_EVIDENCE,
            format!(
                "{} interrupts of {nic} ({}) are off {}",
                irqs.len(),
                devices.join(", "),
                cpus.describe_all(&spinning)
            ),
        ));
    }
    out
}

const T6_EVIDENCE: &str = "DPDK's documentation (NUMA placement of ports, cores and memory); \
     not measured: every lab host had one node";

/// T6: on a host with more than one NUMA node, `nic` is on the node of the
/// busy-spinning CPUs of `cpus`. The media driver's check (a network I/O
/// layer need not call it).
#[must_use]
pub fn t6_nic_node(host: &Host, nic: &Nic, cpus: &HotSet) -> Vec<Finding> {
    let id = "T6";
    let sev = Severity::Warn;
    let unknown =
        |what: String, e: &dyn fmt::Display| vec![Finding::unknown(id, sev, T6_EVIDENCE, what, e)];
    let nodes = match nodes_online(host) {
        Ok(Some(n)) if n.len() > 1 => n,
        Ok(_) => {
            return vec![Finding::ok(
                id,
                sev,
                T6_EVIDENCE,
                "one NUMA node: the NIC's node does not apply".to_owned(),
            )];
        }
        Err(e) => {
            return unknown(
                "cannot tell how many NUMA nodes the host has".to_owned(),
                &e,
            );
        }
    };
    let mut cpus_of: Vec<(u32, CpuSet)> = Vec::new();
    for n in nodes.iter() {
        match host.read_cpu_list(&format!("/sys/devices/system/node/node{n}/cpulist")) {
            Ok(c) => cpus_of.push((n, c)),
            Err(e) => return unknown(format!("cannot tell node {n}'s CPUs"), &e),
        }
    }
    let node_of = |cpu: u32| {
        cpus_of
            .iter()
            .find(|(_, c)| c.contains(cpu))
            .map(|(n, _)| *n)
    };
    let devices = match pci_devices(host, nic) {
        Ok(d) => d,
        Err(e) => return unknown(format!("cannot tell {nic}'s NUMA node"), &e),
    };
    let spinning = cpus.spinning();
    let mut out = Vec::new();
    for bdf in devices {
        let node = match host.read_i64(&format!("/sys/bus/pci/devices/{bdf}/numa_node")) {
            Ok(n) => n,
            Err(e) => return unknown(format!("cannot tell {bdf}'s NUMA node"), &e),
        };
        let Ok(node) = u32::try_from(node) else {
            out.push(Finding::unknown(
                id,
                sev,
                T6_EVIDENCE,
                format!("cannot tell {bdf}'s NUMA node"),
                "its numa_node is -1: the firmware gave the kernel none",
            ));
            continue;
        };
        let remote: CpuSet = spinning
            .iter()
            .filter(|c| node_of(*c) != Some(node))
            .collect();
        if remote.is_empty() {
            out.push(Finding::ok(
                id,
                sev,
                T6_EVIDENCE,
                format!(
                    "{bdf} is on node {node}, with {}",
                    cpus.describe_all(&spinning)
                ),
            ));
            continue;
        }
        let local = cpus_of
            .iter()
            .find(|(n, _)| *n == node)
            .map_or_else(String::new, |(_, c)| c.to_string());
        let theirs: Vec<String> = remote
            .iter()
            .map(|c| {
                node_of(c).map_or_else(
                    || format!("{} on no node", cpus.describe(c)),
                    |n| format!("{} on node {n}", cpus.describe(c)),
                )
            })
            .collect();
        out.push(Finding::found(
            id,
            sev,
            T6_EVIDENCE,
            format!(
                "port {bdf} is on node {node} and {}: every packet crosses the interconnect between \
                 the nodes. Fix: pin them to node {node}'s CPUs ({local}), or use a NIC on their node",
                theirs.join(", ")
            ),
        ));
    }
    out
}

/// How many times `interface`'s carrier has changed
/// (`/sys/class/net/<if>/carrier_changes`): a re-check is due when it
/// moves (an ENA reset brings the interface down and up).
///
/// # Errors
/// The file cannot be read or is not a number.
pub fn carrier_changes(host: &Host, interface: &str) -> Result<u64, ReadError> {
    check_interface(interface)?;
    let path = format!("/sys/class/net/{interface}/carrier_changes");
    let n = host.read_i64(&path)?;
    u64::try_from(n).map_err(|_| ReadError::Malformed {
        path,
        detail: "a negative count",
    })
}

/// The host-wide kernel busy-polling settings.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct BusyPoll {
    /// `net.core.busy_read` (µs): blocking reads busy-poll this long.
    pub busy_read: u32,
    /// `net.core.busy_poll` (µs): `poll`/`select` busy-poll this long.
    pub busy_poll: u32,
}

/// Reads `net.core.busy_read` and `busy_poll`. They exist only in the
/// host's network namespace: in a pod they are absent, which is an error
/// (unknown), never zero.
///
/// # Errors
/// Either file cannot be read or is not a number.
pub fn busy_poll_sysctls(host: &Host) -> Result<BusyPoll, ReadError> {
    let read = |name: &str| {
        let path = format!("/proc/sys/net/core/{name}");
        let v = host.read_i64(&path)?;
        u32::try_from(v).map_err(|_| ReadError::Malformed {
            path,
            detail: "out of range",
        })
    };
    Ok(BusyPoll {
        busy_read: read("busy_read")?,
        busy_poll: read("busy_poll")?,
    })
}