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
//! BTF offsets + decode for the system-wide PSI-irq host memory walk.
//!
//! Captures the guest's IRQ Pressure-Stall-Information observer-free —
//! by walking the global `struct psi_group psi_system` (`kernel/sched/psi.c`,
//! external linkage → KASLR-locatable like `jiffies_64`) from host-side guest
//! memory, instead of a guest `/proc/pressure/irq` read. Two fields are read:
//!
//! - `total[NR_PSI_AGGREGATORS][NR_PSI_STATES-1]` (`u64`, cumulative stall ns):
//! `total[PSI_AVGS][PSI_IRQ_FULL]` is the monotonic IRQ-full stall accumulator
//! (`collect_percpu_times` folds per-CPU `times[PSI_IRQ_FULL]` into it,
//! psi.c:362-415). `psi_show` renders it as µs via `div_u64(.., NSEC_PER_USEC)`
//! (psi.c:1280-1281), so [`decode_total_us`] divides the raw ns by 1000.
//! - `avg[NR_PSI_STATES-1][3]` (`unsigned long`, the EWMA): `avg[PSI_IRQ_FULL][0]`
//! is the 10s window. `calc_avgs` (psi.c:356-360) scales the percent by
//! `FIXED_1` (`pct *= FIXED_1`) and `calc_load` is scale-preserving, so the
//! stored value is `percent * FIXED_1`; [`decode_avg10_percent`] divides by
//! `FIXED_1` to recover percent `[0,100]` (matches `psi_show`'s
//! `LOAD_INT/LOAD_FRAC` = `raw/FIXED_1`, psi.c:1286-1288).
//!
//! GATING (loud-absent): `PSI_IRQ_FULL` is `#ifdef CONFIG_IRQ_TIME_ACCOUNTING`
//! in `enum psi_states` (`include/linux/psi_types.h`), so on a kernel built
//! without it the enumerator is absent from BTF → [`super::enum_value`] returns
//! `None` → [`PsiGroupOffsets::psi_irq_full_idx`] is `None` → the metrics read
//! loud-absent, the `avg_irq_util` BTF-gated pattern. ktstr.kconfig enables it,
//! so the index resolves to 6 on ktstr VMs. A config-ON kernel with
//! IRQ-time accounting disabled at runtime (`irqtime_enabled()` static key off)
//! leaves the accumulators at a real 0 → a measured `Some(0.0)`, NOT absent —
//! the `total_steal_time_ns` precedent (a present-but-accounting-off field reads
//! a constant 0).
use Result;
use Btf;
use ;
/// `PSI_AVGS` aggregator index into `psi_group.total`/`.avg`'s outer dimension
/// (`enum psi_aggregators`, `include/linux/psi_types.h`: `PSI_AVGS=0`,
/// `PSI_POLL=1`). The running-average aggregator — the one `/proc` reads.
pub const PSI_AVGS_AGG: usize = 0;
/// [`PsiGroupOffsets::total_irq_full_off`] reads the `PSI_AVGS` aggregator row
/// and relies on it being row 0 — so the row begins at the array base and the
/// element offset reduces to `idx * elem_size` (no aggregator-stride term).
/// Pin that here: a kernel that ever renumbered `enum psi_aggregators` so
/// `PSI_AVGS != 0` fails to compile rather than silently reading the wrong
/// aggregator's totals.
const _: = assert!;
/// Number of EWMA windows in `psi_group.avg[state][N]` — avg10/avg60/avg300
/// (`unsigned long avg[NR_PSI_STATES-1][3]`, `psi_types.h`). Fixed at 3,
/// config-independent (unlike the state dimension, which is `#ifdef`-gated).
pub const PSI_NR_WINDOWS: usize = 3;
/// Byte width of one `psi_group.total`/`.avg` element on a 64-bit guest:
/// `total` is `u64`, `avg` is `unsigned long` (both 8B on the LP64 targets ktstr
/// supports — x86_64 / aarch64). `read_u64` is byte-correct for both. ktstr is
/// 64-bit-only (the LE/64-bit host assumption documented for the VMM walks).
pub const PSI_ELEM_SIZE: usize = 8;
/// `FIXED_1 = 1<<FSHIFT`, `FSHIFT=11` (`include/linux/sched/loadavg.h`) — the
/// fixed-point scale of `psi_group.avg[]`. The stored EWMA is `percent*FIXED_1`
/// (`calc_avgs` psi.c:356-360), so `raw/FIXED_1` recovers percent `[0,100]`.
pub const PSI_FIXED_1: f64 = 2048.0;
/// `NSEC_PER_USEC` — `psi_show` (psi.c:1280-1281) emits `total` as
/// `div_u64(total_ns, NSEC_PER_USEC)`, so the stored `total[]` ns ÷ this = µs.
pub const PSI_NSEC_PER_USEC: f64 = 1000.0;
/// Decode a raw `psi_group.avg[PSI_IRQ_FULL][0]` (fixed-point) to percent,
/// clamped to `[0,100]` — `(raw / FIXED_1).min(100.0)`. The kernel bounds the
/// steady-state value (`update_averages` caps `sample = period` before
/// `calc_avgs`, `kernel/sched/psi.c:525-576`), so `raw <= 100*FIXED_1`; the `.min(100.0)`
/// hardens against a TORN lockless host-read — `collect_percpu_times` samples
/// per-CPU buckets locklessly and a delta can transiently slip past the period
/// before the clamp settles, so a host-walk mid-update could observe a raw a hair
/// above `100*FIXED_1`. The clamped value otherwise matches `/proc/pressure/irq`'s
/// `avg10=NN.MM` (the text floors to 2 decimals).
/// Decode a raw `psi_group.total[PSI_AVGS][PSI_IRQ_FULL]` (cumulative stall ns)
/// to microseconds — `raw_ns / NSEC_PER_USEC` (the `psi_show` total= unit).
/// Byte offsets within `struct psi_group` (`include/linux/psi_types.h`) for the
/// system-wide PSI-irq host-walk, plus the config-gated `PSI_IRQ_FULL` state
/// index. The two array bases are BTF-resolved (a future leading-field addition
/// surfaces here, not as a silent miscalculation); the element size / window
/// count / `PSI_AVGS` index are layout constants (hardcoded with the cited
/// header values, like the `cpu_time` enum indices).