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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
//! Binary layout of the shared-memory metrics registry.
//!
//! Defines the header and slot in-memory representation. All multi-byte fields
//! that participate in cross-process reads/writes are accessed as atomics; the
//! seqlock pattern (see [`registry`](super::registry)) guards the non-atomic
//! per-sample bytes against torn reads.
//!
//! # Memory layout
//!
//! The mapped region is `HEADER_SIZE + capacity * SLOT_SIZE` bytes
//! (`256 + capacity * 512`). The header lives at offset 0, slot N at offset
//! `256 + N * 512`. Field offsets below include the implicit alignment
//! padding the compiler inserts for `#[repr(C)]` — the `const _ = assert!`
//! checks at the bottom of this file guard the total struct sizes against
//! drift.
//!
//! ```text
//! +-----------------------------------------------------------------+ 0x000
//! | HEADER (256 bytes) |
//! | 0x00 magic u64 "MSBMET01" tag |
//! | 0x08 version u32 layout version (3) |
//! | 0x0c header_len u32 = 256 |
//! | 0x10 slot_len u32 = 512 |
//! | 0x14 capacity u32 number of slots |
//! | 0x18 state AU32 uninit | initializing | ready |
//! | 0x1c -- padding -- |
//! | 0x20 created_at_ms i64 first-create wall clock |
//! | 0x28 global_generation AU64 monotonically increasing |
//! | 0x30 _reserved [u8; 208] (room for future fields) |
//! +-----------------------------------------------------------------+ 0x100
//! | SLOT 0 (512 bytes) |
//! | 0x00 state AU32 free | reserved | active | stale |
//! | 0x04 -- padding -- |
//! | 0x08 generation AU64 ABA stamp, bumped on every reuse |
//! | 0x10 seq AU64 seqlock (odd = writer mid-write) |
//! | 0x18 sandbox_id AI32 catalog sandbox id |
//! | 0x1c run_id AI32 catalog run id (0 until activate) |
//! | 0x20 pid AI32 host pid of the writer |
//! | 0x24 _pad0 u32 |
//! | 0x28 started_at_ms AI64 |
//! | 0x30 sampled_at_ms AI64 0 until first sample |
//! | 0x38 sample_flags AU32 source bits for optional fields |
//! | 0x3c -- padding -- |
//! | 0x40 memory_limit AU64 bytes |
//! | 0x48 vcpu_time_ns AU64 cumulative guest vCPU time |
//! | 0x50 cpu_percent_bits AU32 f32 bits, 0 until CPU rate exists |
//! | 0x54 -- padding -- |
//! | 0x58 memory_bytes AU64 0 until guest-used memory exists |
//! | 0x60 memory_available AU64 |
//! | 0x68 memory_host_rss AU64 |
//! | 0x70 disk_read_bytes AU64 |
//! | 0x78 disk_write_bytes AU64 |
//! | 0x80 net_rx_bytes AU64 |
//! | 0x88 net_tx_bytes AU64 |
//! | 0x90 upper_used AU64 guest-visible upper used bytes |
//! | 0x98 upper_free AU64 guest-visible upper free bytes |
//! | 0xa0 upper_host_alloc AU64 allocated bytes on host upper |
//! | 0xa8 name_len AU16 |
//! | 0xaa _pad1 [AU8; 6] |
//! | 0xb0 name_bytes [AU8; 128] |
//! | 0x130 _tail [u8; 208] |
//! +-----------------------------------------------------------------+ 0x300
//! | SLOT 1 ... SLOT capacity-1 |
//! ~ ~
//! +-----------------------------------------------------------------+
//! ```
//!
//! Prefix `A` denotes `Atomic` (e.g. `AU64` is [`AtomicU64`]). The seqlock
//! contract: a writer increments `seq` to odd, stores the per-sample fields
//! with `Relaxed` ordering, then increments `seq` to even. A reader observes
//! the slot's `state` and `generation`, loads `seq` twice around the field
//! reads, and accepts the snapshot only when both seqs are equal-even,
//! the generation has not advanced, and the state stayed in
//! `{Active, Stale}` throughout.
use ;
//--------------------------------------------------------------------------------------------------
// Constants
//--------------------------------------------------------------------------------------------------
/// Magic value identifying a microsandbox metrics registry.
///
/// ASCII "MSBMET01" (8 bytes).
pub const REGISTRY_MAGIC: u64 = 0x3130_5445_4d42_534d;
/// On-disk layout version. Bump on any incompatible layout change.
pub const REGISTRY_VERSION: u32 = 3;
/// Current shared-memory registry ABI version.
///
/// External registry names and diagnostic filenames must be derived from this
/// value so incompatible slot layouts never share the same POSIX shm object.
pub const REGISTRY_ABI_VERSION: u32 = REGISTRY_VERSION;
/// Default slot capacity used by the host process when global config does
/// not override it. At 512 bytes per slot, 1024 slots = ~512 KiB plus the
/// 256-byte header — enough headroom for the documented 20–560 sandbox
/// range without paying an 8 MiB always-mapped tax for capacity nobody uses.
pub const DEFAULT_CAPACITY: u32 = 1024;
/// Maximum bytes reserved for the sandbox name inside a slot.
///
/// This matches the public sandbox-name byte limit enforced by the SDK crate.
pub const NAME_BYTES: usize = 128;
/// Size of one slot in bytes. Sized at 512 bytes so a slot occupies a single
/// CPU cache-line group on common platforms while leaving headroom for future
/// fields without breaking compatibility within the current version.
pub const SLOT_SIZE: usize = 512;
/// Size of the registry header in bytes.
pub const HEADER_SIZE: usize = 256;
//--------------------------------------------------------------------------------------------------
// Constants: Header state values
//--------------------------------------------------------------------------------------------------
/// Header state: uninitialized memory after `ftruncate`.
pub const HEADER_STATE_UNINIT: u32 = 0;
/// Header state: the creating process is still writing the header.
pub const HEADER_STATE_INITIALIZING: u32 = 1;
/// Header state: header is valid and slots are usable.
pub const HEADER_STATE_READY: u32 = 2;
//--------------------------------------------------------------------------------------------------
// Constants: Slot state values
//--------------------------------------------------------------------------------------------------
/// Slot state: free for allocation.
pub const SLOT_FREE: u32 = 0;
/// Slot state: reserved by the launcher, runtime has not attached yet.
pub const SLOT_RESERVED: u32 = 1;
/// Slot state: runtime attached and writing samples.
pub const SLOT_ACTIVE: u32 = 2;
/// Slot state: writer exited cleanly; last sample preserved for readers and
/// the slot may be reused by the allocator.
pub const SLOT_STALE: u32 = 3;
//--------------------------------------------------------------------------------------------------
// Constants: Sample flags
//--------------------------------------------------------------------------------------------------
/// Sample flag: `cpu_percent` and `vcpu_time_ns` contain guest-sourced values.
pub const SAMPLE_FLAG_CPU: u32 = 1 << 0;
/// Sample flag: `memory_bytes` contains a guest-sourced used-memory value.
pub const SAMPLE_FLAG_MEMORY_USED: u32 = 1 << 1;
/// Sample flag: `memory_available_bytes` contains a valid guest value.
pub const SAMPLE_FLAG_MEMORY_AVAILABLE: u32 = 1 << 2;
/// Sample flag: `memory_host_resident_bytes` contains a valid host diagnostic.
pub const SAMPLE_FLAG_MEMORY_HOST_RESIDENT: u32 = 1 << 3;
/// Sample flag: `upper_used_bytes` contains a valid protected guest filesystem value.
pub const SAMPLE_FLAG_UPPER_USED: u32 = 1 << 4;
/// Sample flag: `upper_free_bytes` contains a valid protected guest filesystem value.
pub const SAMPLE_FLAG_UPPER_FREE: u32 = 1 << 5;
/// Sample flag: `upper_host_allocated_bytes` contains a valid host diagnostic.
pub const SAMPLE_FLAG_UPPER_HOST_ALLOCATED: u32 = 1 << 6;
//--------------------------------------------------------------------------------------------------
// Types
//--------------------------------------------------------------------------------------------------
/// Registry header. Lives at offset `0` of the mapped region.
const _: = assert!;
/// One metrics slot. Lives at offset `HEADER_SIZE + slot_index * SLOT_SIZE`.
// Layout accounting (with `#[repr(C)]` alignment), in offset order:
// state(4) + pad(4) + generation(8) + seq(8) = 24
// sandbox_id(4) + run_id(4) + pid(4) + _pad0(4) = +16 -> 40
// started_at(8) + sampled_at(8) = +16 -> 56
// sample_flags(4) + pad(4) + memory_limit_bytes(8) + vcpu_time_ns(8) = +24 -> 80
// cpu_percent_bits(4) + pad(4) + memory fields(24) = +32 -> 112
// disk_read(8) + disk_write(8) + net_rx(8) + net_tx(8) = +32 -> 144
// upper_used_bytes(8) + upper_free_bytes(8) + upper_host_allocated(8) = +24 -> 168
// name_len(2) + _pad1(6) + name_bytes(NAME_BYTES) = +8 + NAME_BYTES
const SLOT_BASE_BYTES: usize = 176 + NAME_BYTES;
const SLOT_TAIL_PAD: usize = SLOT_SIZE - SLOT_BASE_BYTES;
const _: = assert!;
//--------------------------------------------------------------------------------------------------
// Functions
//--------------------------------------------------------------------------------------------------
/// Total bytes required to map a registry with `capacity` slots.
pub const