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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
//! Honest process measurements for the Grafana runtime row.
//!
//! The upstream dashboard gets these from the Go Prometheus collector. Stream
//! measures the same process boundaries directly instead of publishing
//! synthetic `go_*` series. Linux memory/thread values come from procfs;
//! Darwin uses the kernel's proc_pidinfo task snapshot. CPU is cumulative
//! getrusage time on both platforms, and FD counts enumerate this process's
//! descriptor directory.
const std = @import("std");
const Io = std.Io;
pub const Snapshot = struct {
cpu_us: ?u64 = null,
resident_memory_bytes: ?u64 = null,
virtual_memory_bytes: ?u64 = null,
threads: ?u64 = null,
open_fds: ?u64 = null,
max_fds: ?u64 = null,
minor_page_faults: ?u64 = null,
major_page_faults: ?u64 = null,
voluntary_context_switches: ?u64 = null,
involuntary_context_switches: ?u64 = null,
malloc_system_bytes: ?u64 = null,
malloc_free_total_bytes: ?u64 = null,
malloc_arena_heaps: ?u64 = null,
};
pub fn collect(io: Io) Snapshot {
var result: Snapshot = .{};
if (readMallocInfo()) |info| {
result.malloc_system_bytes = info.system_bytes;
result.malloc_free_total_bytes = info.free_bytes;
result.malloc_arena_heaps = info.heap_count;
}
const usage = std.posix.getrusage(std.posix.rusage.SELF);
result.cpu_us = timevalMicros(usage.utime) +| timevalMicros(usage.stime);
result.minor_page_faults = nonNegative(usage.minflt);
result.major_page_faults = nonNegative(usage.majflt);
result.voluntary_context_switches = nonNegative(usage.nvcsw);
result.involuntary_context_switches = nonNegative(usage.nivcsw);
const limits = std.posix.getrlimit(.NOFILE) catch null;
if (limits) |value| {
// The soft limit is the process's effective maximum. The hard limit
// is merely the ceiling to which a privileged process may raise it.
if (value.cur != std.math.maxInt(std.posix.rlim_t))
result.max_fds = @intCast(value.cur);
}
result.open_fds = countOpenFds(io);
switch (@import("builtin").os.tag) {
.linux => {
var buf: [16 * 1024]u8 = undefined;
const status = Io.Dir.cwd().readFile(io, "/proc/self/status", &buf) catch return result;
if (status.len == buf.len) return result;
const parsed = parseLinuxStatus(status);
result.resident_memory_bytes = parsed.resident_memory_bytes;
result.virtual_memory_bytes = parsed.virtual_memory_bytes;
result.threads = parsed.threads;
},
.macos => {
if (darwinTaskInfo()) |task| {
result.resident_memory_bytes = task.resident_size;
result.virtual_memory_bytes = task.virtual_size;
if (task.thread_count >= 0) result.threads = @intCast(task.thread_count);
}
},
else => {},
}
return result;
}
fn timevalMicros(value: anytype) u64 {
if (value.sec < 0 or value.usec < 0) return 0;
return @as(u64, @intCast(value.sec)) *| std.time.us_per_s +| @as(u64, @intCast(value.usec));
}
fn nonNegative(value: anytype) ?u64 {
if (value < 0) return null;
return @intCast(value);
}
fn countOpenFds(io: Io) ?u64 {
const path = switch (@import("builtin").os.tag) {
.linux => "/proc/self/fd",
.macos => "/dev/fd",
else => return null,
};
var dir = Io.Dir.cwd().openDir(io, path, .{ .iterate = true }) catch return null;
defer dir.close(io);
var iterator = dir.iterate();
var count: u64 = 0;
while (iterator.next(io) catch return null) |_| count += 1;
// Opening the descriptor directory itself adds one descriptor to the
// listing. Remove that observation artifact from the process count.
return count -| 1;
}
const LinuxStatus = struct {
resident_memory_bytes: ?u64 = null,
virtual_memory_bytes: ?u64 = null,
threads: ?u64 = null,
};
fn parseLinuxStatus(bytes: []const u8) LinuxStatus {
var result: LinuxStatus = .{};
var lines = std.mem.splitScalar(u8, bytes, '\n');
while (lines.next()) |line| {
if (parseKbField(line, "VmRSS:")) |value| result.resident_memory_bytes = value;
if (parseKbField(line, "VmSize:")) |value| result.virtual_memory_bytes = value;
if (parseIntegerField(line, "Threads:")) |value| result.threads = value;
}
return result;
}
fn parseKbField(line: []const u8, prefix: []const u8) ?u64 {
if (!std.mem.startsWith(u8, line, prefix)) return null;
const rest = std.mem.trim(u8, line[prefix.len..], " \t");
if (!std.mem.endsWith(u8, rest, "kB")) return null;
const digits = std.mem.trim(u8, rest[0 .. rest.len - 2], " \t");
const kib = std.fmt.parseInt(u64, digits, 10) catch return null;
return std.math.mul(u64, kib, 1024) catch null;
}
fn parseIntegerField(line: []const u8, prefix: []const u8) ?u64 {
if (!std.mem.startsWith(u8, line, prefix)) return null;
return std.fmt.parseInt(u64, std.mem.trim(u8, line[prefix.len..], " \t"), 10) catch null;
}
const DarwinTaskInfo = extern struct {
virtual_size: u64,
resident_size: u64,
total_user: u64,
total_system: u64,
threads_user: u64,
threads_system: u64,
policy: i32,
faults: i32,
pageins: i32,
cow_faults: i32,
messages_sent: i32,
messages_received: i32,
syscalls_mach: i32,
syscalls_unix: i32,
context_switches: i32,
thread_count: i32,
running_thread_count: i32,
priority: i32,
};
const Darwin = struct {
extern "c" fn proc_pidinfo(pid: std.posix.pid_t, flavor: c_int, arg: u64, buffer: *anyopaque, size: c_int) c_int;
};
fn darwinTaskInfo() ?DarwinTaskInfo {
if (@import("builtin").os.tag != .macos) return null;
var info: DarwinTaskInfo = undefined;
const written = Darwin.proc_pidinfo(std.posix.system.getpid(), 4, 0, &info, @sizeOf(DarwinTaskInfo));
if (written != @sizeOf(DarwinTaskInfo)) return null;
return info;
}
test "linux status parsing preserves procfs units" {
const parsed = parseLinuxStatus("Name:\tstream\n" ++
"VmSize:\t 987654 kB\n" ++
"VmRSS:\t 12345 kB\n" ++
"Threads:\t17\n");
try std.testing.expectEqual(@as(?u64, 987654 * 1024), parsed.virtual_memory_bytes);
try std.testing.expectEqual(@as(?u64, 12345 * 1024), parsed.resident_memory_bytes);
try std.testing.expectEqual(@as(?u64, 17), parsed.threads);
}
test "malformed procfs values remain unavailable" {
const parsed = parseLinuxStatus("VmSize:\tnope kB\n" ++
"VmRSS:\t18446744073709551615 kB\n" ++
"Threads:\t-1\n");
try std.testing.expectEqual(@as(?u64, null), parsed.virtual_memory_bytes);
try std.testing.expectEqual(@as(?u64, null), parsed.resident_memory_bytes);
try std.testing.expectEqual(@as(?u64, null), parsed.threads);
}
test "live process snapshot has real runtime measurements" {
const snapshot = collect(std.testing.io);
try std.testing.expect(snapshot.cpu_us != null);
try std.testing.expect(snapshot.resident_memory_bytes != null);
try std.testing.expect(snapshot.virtual_memory_bytes != null);
try std.testing.expect(snapshot.threads != null and snapshot.threads.? > 0);
try std.testing.expect(snapshot.open_fds != null and snapshot.open_fds.? > 0);
try std.testing.expect(snapshot.max_fds != null and snapshot.max_fds.? >= snapshot.open_fds.?);
try std.testing.expect(snapshot.minor_page_faults != null);
try std.testing.expect(snapshot.major_page_faults != null);
try std.testing.expect(snapshot.voluntary_context_switches != null);
try std.testing.expect(snapshot.involuntary_context_switches != null);
}
// All-arena glibc heap stats, parsed from malloc_info() XML — mallinfo()
// covers the main arena only and its dashboard understated multi-GiB arena
// fragmentation until the gauges below existed. Ported from zlay
// (src/internal/broadcaster.zig, incident-2026-05-30-arena-bloat.md):
// "balanced allocs + growing RSS = glibc arena fragmentation, not a leak."
// heap_count counts <heap nr=...> sections: chained 64 MiB per-arena heaps,
// the unit that actually pins RSS (MALLOC_ARENA_MAX caps arenas, not heaps).
const MallocInfo = struct {
system_bytes: u64,
free_bytes: u64,
heap_count: u32,
};
const CFile = opaque {};
const open_memstream = @extern(*const fn (ptr: *[*c]u8, size: *usize) callconv(.c) ?*CFile, .{ .name = "open_memstream" });
const c_fclose = @extern(*const fn (*CFile) callconv(.c) c_int, .{ .name = "fclose" });
const c_free = @extern(*const fn (?*anyopaque) callconv(.c) void, .{ .name = "free" });
const malloc_info_fn = @extern(*const fn (options: c_int, stream: *CFile) callconv(.c) c_int, .{ .name = "malloc_info" });
fn readMallocInfo() ?MallocInfo {
// comptime prunes the glibc-only externs from analysis on other hosts.
return if (comptime @import("builtin").os.tag == .linux) readMallocInfoLinux() else null;
}
fn readMallocInfoLinux() ?MallocInfo {
var buf_ptr: [*c]u8 = null;
var buf_size: usize = 0;
const stream = open_memstream(&buf_ptr, &buf_size) orelse return null;
_ = malloc_info_fn(0, stream);
_ = c_fclose(stream); // flushes and populates buf_ptr/buf_size
const p = buf_ptr orelse return null;
defer c_free(p);
const xml = p[0..buf_size];
return MallocInfo{
// top-level totals are the LAST occurrence (each <heap> emits its own)
.system_bytes = lastSizeAttr(xml, "system type=\"current\"") orelse 0,
.free_bytes = (lastSizeAttr(xml, "total type=\"fast\"") orelse 0) +
(lastSizeAttr(xml, "total type=\"rest\"") orelse 0),
.heap_count = countOccurrences(xml, "<heap nr="),
};
}
// find the last occurrence of `marker`, then parse the first `size="N"` after it.
fn lastSizeAttr(xml: []const u8, marker: []const u8) ?u64 {
const at = std.mem.lastIndexOf(u8, xml, marker) orelse return null;
const rest = xml[at + marker.len ..];
const key = "size=\"";
const kpos = std.mem.indexOf(u8, rest, key) orelse return null;
const num = rest[kpos + key.len ..];
const end = std.mem.indexOfScalar(u8, num, '"') orelse return null;
return std.fmt.parseInt(u64, num[0..end], 10) catch null;
}
fn countOccurrences(haystack: []const u8, needle: []const u8) u32 {
var n: u32 = 0;
var i: usize = 0;
while (std.mem.indexOfPos(u8, haystack, i, needle)) |pos| {
n += 1;
i = pos + needle.len;
}
return n;
}
test "readMallocInfo parses all-arena totals" {
// mirrors zlay's readMallocInfo test; only meaningful against glibc
if (comptime @import("builtin").os.tag != .linux) return error.SkipZigTest;
const a = std.heap.c_allocator;
var kept: [64][]u8 = undefined;
for (&kept) |*slot| slot.* = try a.alloc(u8, 4096);
defer for (kept) |s| a.free(s);
const info = readMallocInfo() orelse return error.MallocInfoUnavailable;
try std.testing.expect(info.system_bytes > 0);
try std.testing.expect(info.heap_count >= 1);
}
test "lastSizeAttr / countOccurrences against sample XML" {
// mirrors zlay's fixture: per-heap totals precede the top-level totals,
// and the parser must take the LAST occurrence
const xml =
\\<malloc version="1">
\\<heap nr="0">
\\<total type="fast" count="1" size="100"/>
\\<total type="rest" count="2" size="200"/>
\\<system type="current" size="9999"/>
\\</heap>
\\<heap nr="1">
\\<total type="fast" count="3" size="111"/>
\\<total type="rest" count="4" size="222"/>
\\<system type="current" size="8888"/>
\\</heap>
\\<total type="fast" count="4" size="211"/>
\\<total type="rest" count="6" size="422"/>
\\<system type="current" size="18887"/>
\\</malloc>
;
try std.testing.expectEqual(@as(?u64, 18887), lastSizeAttr(xml, "system type=\"current\""));
try std.testing.expectEqual(@as(?u64, 211), lastSizeAttr(xml, "total type=\"fast\""));
try std.testing.expectEqual(@as(?u64, 422), lastSizeAttr(xml, "total type=\"rest\""));
try std.testing.expectEqual(@as(u32, 2), countOccurrences(xml, "<heap nr="));
}