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
use std::path::Path;
use crate::error::{Error, Result};
use crate::util::parse;
/// Per-CPU software interrupt (`softirq`) counters from `/proc/softirqs`.
///
/// Values are cumulative since boot; sample twice and subtract to get
/// rates.
#[derive(Debug)]
pub struct Softirqs {
/// CPU ids from the header line, ascending.
///
/// Each row's counts align with this vector by position, not by id.
pub cpus: Vec<u32>,
/// One entry per softirq type, in file order.
pub rows: Vec<SoftirqRow>,
}
/// Counts for one softirq type across all CPUs.
#[derive(Debug)]
pub struct SoftirqRow {
/// Softirq name, without the trailing `:`.
///
/// The kernel fixes the set (`HI`, `TIMER`, `NET_TX`, `NET_RX`,
/// `BLOCK`, `IRQ_POLL`, `TASKLET`, `SCHED`, `HRTIMER`, `RCU`);
/// names from future kernels are kept as-is.
pub name: Box<str>,
/// Times this softirq has run on each CPU, aligned with
/// [`Softirqs::cpus`] by position.
pub per_cpu: Vec<u64>,
}
/// Reads `/proc/softirqs` and returns per-CPU softirq counters.
///
/// The first line names the online CPUs (`CPU0`, `CPU1`, ...); every
/// following line holds one softirq type and its per-CPU counts.
/// Counts are read column by column, so a row with fewer columns than
/// the header (a CPU going offline mid-read) yields a shorter
/// [`SoftirqRow::per_cpu`] instead of an error.
pub fn softirqs() -> Result<Softirqs> {
let path = Path::new("/proc/softirqs");
let bytes = parse::read_file(path)?;
let mut cpus: Vec<u32> = Vec::new();
let mut rows: Vec<SoftirqRow> = Vec::new();
for (line_num, line) in bytes
.split(|&b| b == b'\n')
.filter(|l| !l.is_empty())
.enumerate()
{
let fields = parse::split_spaces(line);
if fields.is_empty() {
continue;
}
if fields[0].starts_with(b"CPU") {
cpus.clear();
for &field in &fields {
let id = field
.strip_prefix(&b"CPU"[..])
.ok_or_else(|| Error::Parse {
path: path.to_path_buf(),
line: line_num + 1,
msg: "invalid CPU column",
})?;
cpus.push(parse::parse_dec_u32(id).map_err(|_| Error::Parse {
path: path.to_path_buf(),
line: line_num + 1,
msg: "invalid CPU id",
})?);
}
} else {
if fields.len() < 2 {
return Err(Error::Parse {
path: path.to_path_buf(),
line: line_num + 1,
msg: "expected '<name>: <per-cpu counts>'",
});
}
let (name, _) = parse::split_at_byte(fields[0], b':');
let name = std::str::from_utf8(name).map_err(|_| Error::Parse {
path: path.to_path_buf(),
line: line_num + 1,
msg: "invalid softirq name",
})?;
let mut per_cpu = Vec::with_capacity(fields.len() - 1);
for &field in &fields[1..] {
per_cpu.push(parse::parse_dec_u64(field).map_err(|_| Error::Parse {
path: path.to_path_buf(),
line: line_num + 1,
msg: "invalid counter",
})?);
}
rows.push(SoftirqRow {
name: name.into(),
per_cpu,
});
}
}
Ok(Softirqs { cpus, rows })
}