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
//! BTF offsets and stable enum indices for per-CPU CPU-time, softirq,
//! and IRQ counters.
//!
//! Three structs participate in the failure-dump CPU-time capture:
//!
//! - `struct kernel_cpustat` (`include/linux/kernel_stat.h`):
//! `u64 cpustat[NR_STATS]` per CPU, indexed by `enum cpu_usage_stat`.
//! - `struct kernel_stat` (`include/linux/kernel_stat.h`):
//! `unsigned long irqs_sum` plus `unsigned int softirqs[NR_SOFTIRQS]`
//! per CPU.
//! - `struct tick_sched` (`kernel/time/tick-sched.h`): per-CPU
//! `iowait_sleeptime` accumulated under `CONFIG_NO_HZ_COMMON`.
//!
//! All three sit in `.data..percpu` symbols (`kernel_cpustat`, `kstat`,
//! `tick_cpu_sched`); the dump path resolves the symbols via
//! `super::symbols::KernelSymbols::cpu_time_symbols` and adds
//! `__per_cpu_offset[cpu]` per CPU.
use Result;
use Btf;
use ;
/// Stable indices of `kernel_cpustat::cpustat[NR_STATS]` from
/// `enum cpu_usage_stat` (`include/linux/kernel_stat.h`). The kernel
/// pins the order so external readers — `/proc/stat` formatting,
/// `account_user_time` / `account_system_index_time` accumulation,
/// every userspace tool that reads `kernel_cpustat` — depend on it.
/// Hard-code the indices the failure dump captures instead of
/// resolving them via BTF: BTF only encodes the array length, not the
/// enum-to-position mapping, so a BTF-driven read would have to
/// resolve the enum separately. The cited header values are the
/// authoritative source; mismatching kernels would be a UAPI break,
/// not a layout drift this code can adapt to.
pub const CPUTIME_USER: usize = 0;
/// Index of `cpustat[CPUTIME_NICE]` (CPU time spent on nice'd user
/// processes). See [`CPUTIME_USER`].
pub const CPUTIME_NICE: usize = 1;
/// Index of `cpustat[CPUTIME_SYSTEM]` (CPU time spent in kernel).
/// See [`CPUTIME_USER`].
pub const CPUTIME_SYSTEM: usize = 2;
/// Index of `cpustat[CPUTIME_SOFTIRQ]` (CPU time servicing softirqs).
/// See [`CPUTIME_USER`].
pub const CPUTIME_SOFTIRQ: usize = 3;
/// Index of `cpustat[CPUTIME_IRQ]` (CPU time servicing hardirqs).
/// See [`CPUTIME_USER`].
pub const CPUTIME_IRQ: usize = 4;
/// Index of `cpustat[CPUTIME_IDLE]` (CPU time spent idle).
/// See [`CPUTIME_USER`].
pub const CPUTIME_IDLE: usize = 5;
/// Index of `cpustat[CPUTIME_IOWAIT]` (CPU time waiting on
/// outstanding block IO). See [`CPUTIME_USER`].
pub const CPUTIME_IOWAIT: usize = 6;
/// Index of `cpustat[CPUTIME_STEAL]` (CPU time stolen by the
/// hypervisor — virt only). See [`CPUTIME_USER`].
pub const CPUTIME_STEAL: usize = 7;
/// Number of softirq vectors per `enum` in `include/linux/interrupt.h`
/// (HI/TIMER/NET_TX/NET_RX/BLOCK/IRQ_POLL/TASKLET/SCHED/HRTIMER/RCU,
/// in that order). The order is enum-stable, mirroring
/// [`CPUTIME_USER`]'s rationale: external consumers (`/proc/softirqs`
/// formatting, `softirq_to_name[]`) depend on the layout, so a
/// reordering would be a UAPI break and resolving each name via BTF
/// would buy nothing.
pub const NR_SOFTIRQS: usize = 10;
/// Names of every softirq vector, indexed by the enum order shared
/// with the kernel's `softirq_to_name[]` (kernel/softirq.c). Surfaced
/// in failure-dump JSON so a downstream consumer reading
/// `softirqs[i]` knows which vector each slot represents without
/// chasing the kernel header.
///
/// `dead_code` allow: forward-looking — referenced from
/// [`super::super::dump::PerCpuTimeStats`] doc comments but no
/// renderer consumes the names yet.
pub const SOFTIRQ_NAMES: = ;
/// Byte offsets used to read per-CPU CPU-time and softirq/IRQ
/// counters from guest memory.
///
/// Three structs participate:
/// - `struct kernel_cpustat` (`include/linux/kernel_stat.h`):
/// a per-CPU `u64 cpustat[NR_STATS]` table indexed by
/// `enum cpu_usage_stat`. Hand-rolled accumulators in the
/// kernel's CPU-time accounting (`account_idle_time`,
/// `account_user_time`, etc.) bump these in nanoseconds (or
/// jiffies pre-NO_HZ_FULL — the field is `u64 nsecs` regardless;
/// `cputime64_to_clock_t` does the conversion at `/proc/stat`
/// read).
/// - `struct kernel_stat` (`include/linux/kernel_stat.h`): a
/// per-CPU `unsigned long irqs_sum` plus
/// `unsigned int softirqs[NR_SOFTIRQS]` table (10 counters in
/// 2026-04-30 mainline). `kstat_incr_softirqs_this_cpu` and
/// `kstat_incr_irq_this_cpu` are the producers.
/// - `struct tick_sched` (`kernel/time/tick-sched.h`): per-CPU
/// `iowait_sleeptime` (`ktime_t` aka `s64` ns) accumulated only
/// under NO_HZ when the CPU enters idle with `nr_iowait > 0`.
///
/// All three structs sit in `.data..percpu` symbols
/// (`kernel_cpustat`, `kstat`, `tick_cpu_sched`). Per-CPU symbols
/// carry section-relative offsets in vmlinux's symtab; the per-CPU
/// KVA for CPU `n` is `<symbol> + __per_cpu_offset[n]` —
/// [`super::super::symbols::KernelSymbols::cpu_time_symbols`] resolves the
/// symbols and the dump path adds `__per_cpu_offset[cpu]` per CPU.
///
/// Field-presence semantics: a kernel without sched_ext omits no
/// field captured here, but a kernel built without
/// `CONFIG_NO_HZ_COMMON` drops `tick_sched`. The offset resolver
/// reports `tick_sched_iowait_sleeptime` as `Some` only when the
/// type is present. Callers that observe `None` skip the
/// `iowait_sleeptime` capture and surface `nr_iowait` (an atomic
/// counter on `struct rq` that the existing scx walker already
/// reads) instead.