Skip to main content

ergon_tune/
lib.rs

1//! # ergon-tune
2//!
3//! Checks of a Linux host's low-latency settings, for start-up and
4//! housekeeping. A busy-spinning thread loses latency to whatever takes its
5//! CPU: a timer tick, an interrupt, `khugepaged`, a busy hyperthread, a
6//! container's CPU quota. Each check reads the host and returns a
7//! [`Finding`]: what is wrong, the exact fix and the evidence. Nothing is
8//! changed, and no check needs more than the standard library and
9//! [rustix](https://crates.io/crates/rustix).
10//!
11//! At start-up, name the CPUs that busy-spin and log what the checks find:
12//!
13//! ```
14//! use ergon_tune::{Component, Host, HotSet, Role, t1_isolation, t3_thp};
15//!
16//! let host = Host::system();
17//! let hot = HotSet::new().with(3, Role::Loop); // the loop runs on CPU 3
18//! for f in t1_isolation(&host, &hot).iter().chain(&t3_thp(&host, Component::Rt)) {
19//!     // One line each: `T1 warn: <what, its measured effect, the fix> (evidence: <where>)`.
20//!     if f.is_finding() {
21//!         eprintln!("{f}");
22//!     }
23//! }
24//! ```
25//!
26//! Before deploying, run the same checks on a node with the `ergon-tune`
27//! command (`cargo install ergon-tune`). Feature `nic` adds N1-N5.
28//!
29//! # Documentation
30//!
31//! - [The ergon-tune book chapter](https://mimran1980.github.io/ergon/tune/overview.html):
32//!   each setting in plain English, the evidence per check and a
33//!   symptom-to-fix table.
34//! - [Crate README](https://github.com/mimran1980/ergon/blob/main/tune/README.md):
35//!   the `ERGON_HOST_CHECKS` switch, the guarantees and limits and the command's
36//!   options.
37//!
38//! ## The checks
39//!
40//! | Check | Function | Meant for |
41//! |---|---|---|
42//! | T1 | [`t1_isolation`] | io, md, rt |
43//! | T2 | [`t2_nic_irqs`] | io (egress NIC), md (data NICs, DPDK ports) |
44//! | T3 | [`t3_thp`] | md, rt (warn); io (info) |
45//! | T4 | [`t4_aeron_dir`] | md, rt |
46//! | T5 | [`t5_numa_balancing`] | md, rt |
47//! | T6 | [`t6_nic_node`] | md |
48//! | T7 | [`t7_sched_policy`] | io, md, rt |
49//! | T8 | [`t8_smt_siblings`] | io, md, rt |
50//! | T9 | [`t9_cpu_quota`] at start; [`ThrottleWatch`] while running | io, md (start); `ThrottleWatch`: a runtime's housekeeping |
51//! | T10 | [`t10_smt_on`] | io, md |
52//! | T11 | [`t11_time_sync`] at start; [`ClockWatch`] while running | rt (start, housekeeping) |
53//! | T12 | [`t12_clocksource`] at start; [`ClockWatch`] while running | rt (start, housekeeping) |
54//! | T13 | [`t13_workqueue_cpumask`] | io, md |
55//! | T14 | [`t14_compaction`] (info) | io, md |
56//! | T15 | [`t15_host_tasks`] | io, md |
57//! | T16 | [`t16_instrumentation`] (info) | io, md |
58//! | T17 | [`t17_ksm`] | io, md |
59//! | T18 | [`t18_steal`] (info) | io, md |
60//! | T19 | [`FaultWatch`] while running | io (the polling thread, every few seconds from housekeeping) |
61//! | T20 | [`t20_irq_idle`] | a node check (the CPU taking NIC interrupts) |
62//! | T21 | [`t21_tcp_autocorking`] | io (a warning: its connections send latency-sensitive TCP) |
63//! | T22 | [`t22_aeron_affinity_names`] | md |
64//! | T23 | [`t23_aeron_hugepage_env`] | md |
65//! | T24 | [`t24_dpdk_nic_pair`] | io (on DPDK), md (on a DPDK port) |
66//! | T25 | [`t25_frequency`] | a node check (the CPU frequency governor) |
67//! | T26 | [`t26_family_layout`] | a caller that declares its instance family (`c7i`, `c7a`, `c8g`, `c9g`) |
68//! | T27 | [`t27_ephemeral_overlap`] | a caller whose app listens on ports that may fall in `net.ipv4.ip_local_port_range` |
69//! | K1-K6 | [`k8s_checks`] (with T7 and T9), inside a pod only | io |
70//! | K7 | [`k8s_checks`] with [`Needs::with_nic`] | a caller that names its NIC |
71//! | N1 (`nic`) | `n1_adaptive_rx` | io, md (kernel sockets) |
72//! | N2 (`nic`) | `n2_deferred_irqs` | io, md |
73//! | N3 (`nic`) | `n3_azure_vf` | io |
74//! | N4 (`nic`) | `n4_rx_ring` | md (socket transport with busy polling) |
75//! | N5 (`nic`) | `n5_irq_headroom` | io, md (kernel sockets, with N1's setting) |
76//!
77//! - **Callers** ([`Component`]): io is a busy-spinning network I/O layer,
78//!   md an Aeron media driver, rt an application's busy-spinning runtime.
79//!   None of them is in this repository; each calls what it chooses.
80//! - **Results**: each is a [`Finding`]. Its [`Status`] says whether the check
81//!   fired; its [`Severity`] says how to log it.
82//! - **Cost**: file reads and allocations. T7 reads `/proc/thread-self` (no
83//!   system call); T2 reads one directory and two files per interrupt; N1 and
84//!   N4 make one netlink exchange per interface per reset (plus one family
85//!   lookup on first use), each with 100 ms send and receive timeouts.
86//!   [`ThrottleWatch::check`] reads one open file (`cpu.stat` too once
87//!   throttling shows) and allocates only when it reports. T11 scans
88//!   `/proc` and waits at most 50 ms for chronyd; [`ClockWatch::check`]
89//!   never waits and allocates only when a finding changes. T15 reads two
90//!   files per task on the host; [`FaultWatch::check`] reads one open file
91//!   and allocates only when it reports.
92//! - **Evidence**: each finding's `evidence` names its source: the
93//!   kernel's, Kubernetes' or a vendor's documentation, marked "not
94//!   measured here".
95//! - **The switch**: every host-check warning line ends with how to silence
96//!   it. [`HOST_CHECKS_ENV`] is `off` (no warning line, the results are
97//!   still returned), `on` (the default) or `require` (a warning is an error
98//!   at start). See [`HostCheckPolicy`] and [`host_check_line`].
99
100mod aeron;
101mod cpu;
102mod cpuset;
103mod dpdk;
104mod family;
105mod faults;
106mod finding;
107mod freq;
108mod host;
109mod idle;
110mod k8s;
111mod mem;
112mod net;
113#[cfg(feature = "nic")]
114mod nic;
115mod noise;
116mod platform;
117mod ports;
118mod quota;
119mod report;
120mod tcp;
121mod time;
122
123pub use aeron::{AERON_HUGEPAGE_BYTES, t22_aeron_affinity_names, t23_aeron_hugepage_env};
124pub use cpu::{
125    SchedPolicy, Thread, isolated_cpus, t1_isolation, t7_sched_policy, t8_smt_siblings, t10_smt_on,
126    thread_affinity, thread_policy,
127};
128pub use cpuset::{CPU_LIMIT, CpuSet, CpuSetError};
129pub use dpdk::{DpdkNicPair, t24_dpdk_nic_pair};
130pub use family::t26_family_layout;
131pub use faults::{FaultWatch, Faults};
132pub use finding::{Component, Finding, HotCpu, HotSet, Role, Severity, Status};
133pub use freq::t25_frequency;
134pub use host::{Host, ReadError};
135pub use idle::{
136    IdleState, ResumeLimit, T20_MAX_EXIT_LATENCY_US, cpu_idle_states, cpuidle_driver, resume_limit,
137    t20_irq_idle,
138};
139pub use k8s::{Needs, in_pod, k8s_checks};
140pub use mem::{FsMagic, fs_magic, t3_thp, t4_aeron_dir, t4_from, t5_numa_balancing};
141pub use net::{
142    BusyPoll, Nic, NicIrq, busy_poll_sysctls, carrier_changes, nic_irqs, t2_nic_irqs, t6_nic_node,
143};
144#[cfg(feature = "nic")]
145pub use nic::{
146    Coalesce, Ethtool, EthtoolCache, EthtoolError, Netlink, Rings, n1_adaptive_rx,
147    n2_deferred_irqs, n3_azure_vf, n4_rx_ring, n5_irq_headroom,
148};
149pub use noise::{
150    CpuSteal, cpu_steal, t13_workqueue_cpumask, t14_compaction, t15_host_tasks,
151    t16_instrumentation, t17_ksm, t18_steal,
152};
153pub use platform::{Arch, Cloud, Platform, Vendor};
154pub use ports::{ip_local_port_range, ip_local_reserved_ports, t27_ephemeral_overlap};
155pub use quota::{
156    CgroupVersion, CpuCgroup, CpuQuota, ThrottleWatch, Throttling, cpu_cgroup, t9_cpu_quota,
157};
158pub use report::{
159    HOST_CHECKS_ENV, HostCheckError, HostCheckPolicy, SILENCE, host_check_line,
160    unknown_stops_require, warn_line,
161};
162pub use tcp::t21_tcp_autocorking;
163pub use time::{
164    Bound, BoundSource, ChronyAnswer, ChronyError, ChronyReference, ChronyTracking,
165    ClockBoundAnswer, ClockBoundError, ClockBoundReading, ClockBoundStatus, ClockSample,
166    ClockWatch, Clocksource, ClocksourceKind, ClocksourceName, KernelSync, Observed, PtpClock,
167    RefclockId, SyncVerdict, TimeDaemon, TimeSync, TimeSyncLimits, TimeSyncProbe, t11_time_sync,
168    t12_clocksource, time_sync,
169};