Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
the Mac hosts' memory peak is a high-water mark reset by the health q…
…uery

A review of the GLM host's memory counts pointed out that the peak field
carried the load limit, and the repair put the kernel's lifetime peak there —
which never resets, while /health?reset_peak=1 and the metrics' help text say
the peak is the most device memory held since start or the last reset. The Mac
hosts now keep that mark themselves: the session high-water mark of the
process footprint, cleared to the current footprint when the reset is asked
for, climbing again from there. /health's memory object carries the limit.
  • Loading branch information
heitke committed Oct 14, 2026
commit 6e8b3b5bfb052d0547f98e28d76df770807aa145
30 changes: 18 additions & 12 deletions zig/src/native/cache_fit.zig
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,14 @@ pub fn footprint() ?u64 {
return info[9];
}

/// This process's peak physical footprint over its lifetime (rusage_info_v4 slot 30; null elsewhere).
pub fn peakFootprint() ?u64 {
if (builtin.os.tag != .macos) return null;
var info: [43]u64 = undefined;
if (proc_pid_rusage(getpid(), 4, &info) != 0) return null;
return info[30];
}
/// The Mac hosts' session peak: the footprint's high-water mark since the last `reset_peak`.
var host_peak: std.atomic.Value(u64) = .init(0);

/// A Mac family host's memory counts for `/stats`, `/memory` and `/metrics`, from the process footprint.
pub fn hostMemory(limit_bytes: u64, reset_peak: bool) ?api.Memory {
_ = reset_peak;
const active = footprint() orelse return null;
const peak = peakFootprint() orelse active;
_ = host_peak.fetchMax(active, .monotonic);
const peak = if (reset_peak) active else host_peak.load(.monotonic);
return .{ .active = active, .cache = 0, .peak = peak, .pinned = 0, .limit = limit_bytes };
}

Expand Down Expand Up @@ -140,12 +135,23 @@ test "a host's memory counts keep the footprint, its measured peak and the limit
const limit = 235 * GiB;
const m = hostMemory(limit, false) orelse return error.SkipZigTest;
try std.testing.expect(m.active > 0); // this test process's own footprint
try std.testing.expect(m.peak >= m.active); // the kernel's lifetime peak covers now
try std.testing.expect(m.peak >= m.active); // the high-water mark covers now
try std.testing.expectEqual(limit, m.limit.?);
try std.testing.expectEqual(@as(u64, 0), m.cache);
try std.testing.expectEqual(@as(u64, 0), m.pinned);
try std.testing.expectEqual(m.active, hostMemory(limit, true).?.active); // reset_peak changes nothing here
try std.testing.expectEqual(m.peak, hostMemory(limit, true).?.peak);
const after_reset = hostMemory(limit, true).?; // a reset starts a new peak at the footprint now
try std.testing.expectEqual(m.active, after_reset.peak);
try std.testing.expect(after_reset.active <= m.active); // the footprint only shrank since the first read
const again = hostMemory(limit, false).?;
try std.testing.expect(again.peak >= again.active); // the mark climbs again from the reset
}

test "the high-water mark rises and resets" {
if (builtin.os.tag != .macos) return error.SkipZigTest;
_ = hostMemory(1 << 30, true) orelse return error.SkipZigTest; // start from a fresh mark
const low = hostMemory(1 << 30, false).?; // the footprint now is the mark
const high = hostMemory(1 << 30, false).?; // a second read cannot lower it
try std.testing.expect(high.peak >= low.active);
}

test "a host without a footprint reading reports no memory rather than zeros" {
Expand Down
1 change: 1 addition & 0 deletions zig/src/server/status_routes.zig
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub fn health(srv: *Server, conn: *Conn, a: Allocator) !void {
try memory.put(a, "active", try json.intValue(a, mem.active));
try memory.put(a, "cache", try json.intValue(a, mem.cache));
try memory.put(a, "peak", try json.intValue(a, mem.peak));
if (mem.limit) |limit| try memory.put(a, "limit", try json.intValue(a, limit));
}
try o.put(a, "memory", .{ .object = memory });
try o.put(a, "live", try live.snapshot(a, srv.engine));
Expand Down
Loading