use std::process::ExitCode;
use ergon_tune::{
Component, CpuSet, Finding, Host, HostCheckPolicy, HotSet, Needs, Nic, Role, Severity, Status,
Thread, isolated_cpus, k8s_checks, t1_isolation, t2_nic_irqs, t3_thp, t5_numa_balancing,
t6_nic_node, t8_smt_siblings, t9_cpu_quota, t10_smt_on, t12_clocksource, t13_workqueue_cpumask,
t14_compaction, t15_host_tasks, t16_instrumentation, t17_ksm, t18_steal, t20_irq_idle,
t21_tcp_autocorking, t25_frequency, t26_family_layout,
};
const USAGE: &str = "usage: ergon-tune [--hot CPUS] [--nic NAME|PCI]... [--family NAME] [--tcp] \
[--busy-poll] [--component io|md|rt] [--root DIR] [--all] [--require]";
#[allow(clippy::struct_excessive_bools)] struct Args {
hot: Option<CpuSet>,
nics: Vec<Nic>,
family: Option<String>,
tcp: bool,
busy_poll: bool,
component: Component,
root: Option<String>,
all: bool,
require: bool,
}
fn parse(mut it: impl Iterator<Item = String>) -> Result<Args, String> {
let mut a = Args {
hot: None,
nics: Vec::new(),
family: None,
tcp: false,
busy_poll: false,
component: Component::Md,
root: None,
all: false,
require: false,
};
while let Some(arg) = it.next() {
let mut value = |name: &str| it.next().ok_or_else(|| format!("{name} needs a value"));
match arg.as_str() {
"--hot" => {
let v = value("--hot")?;
a.hot = Some(CpuSet::parse_list(&v).map_err(|e| format!("--hot {v}: {e}"))?);
}
"--nic" => {
let v = value("--nic")?;
a.nics
.push(if v.matches(':').count() == 2 && v.contains('.') {
Nic::Pci(v)
} else {
Nic::Interface(v)
});
}
"--family" => a.family = Some(value("--family")?),
"--component" => {
a.component = match value("--component")?.as_str() {
"io" => Component::Io,
"md" => Component::Md,
"rt" => Component::Rt,
other => return Err(format!("--component {other}: io, md or rt")),
};
}
"--root" => a.root = Some(value("--root")?),
"--tcp" => a.tcp = true,
"--busy-poll" => a.busy_poll = true,
"--all" => a.all = true,
"--require" => a.require = true,
other => return Err(format!("unknown argument {other}")),
}
}
Ok(a)
}
fn t21_not_checked() -> Finding {
Finding {
id: "T21",
severity: Severity::Info,
status: Status::Unknown(
"/proc/sys/net there need not be the application's network namespace (a mounted \
procfs answers for ergon-tune's own, a captured tree for the one it was captured \
in): run it without --root in the namespace the application sends from"
.to_owned(),
),
message: "TCP autocorking not checked under --root".to_owned(),
evidence: "network_namespaces(7) (files under /proc/sys/net are per network namespace)",
}
}
fn t9_not_checked() -> Finding {
Finding {
id: "T9",
severity: Severity::Info,
status: Status::Finding,
message: "CPU quota not checked under --root: /proc/self is ergon-tune's own process, \
not an app's. The app checks its own quota at start (ERGON_HOST_CHECKS), or \
run ergon-tune in the app's container"
.to_owned(),
evidence: "Linux Documentation/admin-guide/cgroup-v2.rst (cgroup namespaces, /proc/<pid>/cgroup)",
}
}
fn findings(host: &Host, a: &Args, spin: &HotSet) -> Vec<Finding> {
let mut f = Vec::new();
f.extend(t1_isolation(host, spin));
f.extend(t8_smt_siblings(host, spin));
f.push(t10_smt_on(host));
f.extend(t3_thp(host, a.component));
f.push(t5_numa_balancing(host));
f.push(if host.is_live() {
t9_cpu_quota(host)
} else {
t9_not_checked()
});
f.push(t12_clocksource(host));
f.push(t13_workqueue_cpumask(host, spin));
f.push(t14_compaction(host));
f.push(t15_host_tasks(host, spin));
f.push(t16_instrumentation(host));
f.push(t17_ksm(host));
f.push(t18_steal(host, spin));
f.push(if host.is_live() {
t21_tcp_autocorking(host, a.tcp)
} else {
t21_not_checked()
});
f.push(t25_frequency(host));
if let Some(family) = &a.family {
f.extend(t26_family_layout(host, family, spin));
}
for nic in &a.nics {
f.extend(t2_nic_irqs(host, nic, spin));
f.extend(t6_nic_node(host, nic, spin));
}
if !a.nics.is_empty() {
f.extend(t20_irq_idle(host, &a.nics, spin));
}
let mut needs = Needs::new(Thread::Current, Role::Loop);
if a.busy_poll {
needs = needs.busy_polls();
}
if let Some(nic) = a.nics.first() {
needs = needs.with_nic(nic.clone());
}
f.extend(k8s_checks(host, spin, &needs));
f
}
fn line(f: &Finding) -> String {
let tag = match (&f.status, f.severity) {
(Status::Ok, _) => "ok",
(Status::Unknown(_), _) => "UNKNOWN",
(Status::Finding, Severity::Warn) => "WARN",
(Status::Finding, _) => "info",
};
format!("{tag} {f}")
}
fn main() -> ExitCode {
if std::env::args().skip(1).any(|a| a == "--help" || a == "-h") {
println!(
"{USAGE}\n\nRuns ergon-tune's host checks on this node and prints each finding with its fix.\nExit status: 0 reported, 1 refused (--require), 2 usage."
);
return ExitCode::SUCCESS;
}
let a = match parse(std::env::args().skip(1)) {
Ok(a) => a,
Err(e) => {
eprintln!("ergon-tune: {e}\n{USAGE}");
return ExitCode::from(2);
}
};
let host = a.root.as_deref().map_or_else(Host::system, Host::at);
let (cpus, from) = match (&a.hot, isolated_cpus(&host)) {
(Some(c), _) => (c.clone(), "--hot".to_owned()),
(None, Ok(c)) => (c, "isolated".to_owned()),
(None, Err(e)) => (CpuSet::new(), format!("isolated: cannot read {e}")),
};
let mut spin = HotSet::new();
for c in cpus.iter() {
spin.push(c, Role::Loop);
}
if cpus.is_empty() {
println!(
"hot CPUs: none ({from}): the per-CPU checks have nothing to check; name them with --hot"
);
} else {
println!("hot CPUs: {cpus} ({from})");
}
let all = findings(&host, &a, &spin);
let (mut warn, mut info, mut unknown, mut ok) = (0, 0, 0, 0);
for f in &all {
match (&f.status, f.severity) {
(Status::Ok, _) => ok += 1,
(Status::Unknown(_), _) => unknown += 1,
(Status::Finding, Severity::Warn) => warn += 1,
(Status::Finding, _) => info += 1,
}
if a.all || f.status != Status::Ok {
println!("{}", line(f));
}
}
println!("summary: {warn} warnings, {info} info, {unknown} unknown, {ok} ok");
if a.require && cpus.is_empty() {
eprintln!("refused: no hot CPUs to check ({from}); name them with --hot");
return ExitCode::from(1);
}
if a.require
&& let Some(refusal) = HostCheckPolicy::Require.refusal(&all)
{
eprintln!("refused: {refusal}");
return ExitCode::from(1);
}
ExitCode::SUCCESS
}