Skip to content

Commit 917342b

Browse files
committed
fix(gc): return memory in long-running stdin servers (bb-mcp)
A bb-mcp head on cljw only grew with traffic. Four causes, each reproduced: - The stdin readers (text_io *in*, host_stream System/in) appended every chunk to their buffer and only advanced the cursor, so every byte a server ever read stayed resident (400 MB through read-line: 470 MB RSS). stdin.dropConsumed compacts the consumed prefix before each fill (71 MB, flat). - Nothing collected while the main thread blocked on stdin. Only the main thread may collect, and bb-mcp runs its calls on worker threads while main waits for the next request, so a burst of calls left all its garbage behind (20 calls of 4 MB JSON: 955 MB, held indefinitely). The stdin fill now waits with poll and, after each idle second, calls gc.collectBeforeIdle: a System/gc-style collect that keeps 1 MiB of empty pages, when at least 1 MiB was allocated since the last collect, followed by malloc_trim on glibc (129 MB, one second after the work ends). - A cljw.net socket's 128 KiB buffers and a StringBuilder's growing buffer were invisible to the collect trigger. Both are now charged. The three copies of the socket construction become one wrapStream. Six bursts of five such calls now hold at 72 MB, against 274 MB before.
1 parent 06abc57 commit 917342b

7 files changed

Lines changed: 126 additions & 36 deletions

File tree

‎src/runtime/cljw/net/socket.zig‎

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,13 @@ fn connectFn(rt: *Runtime, env: *Env, args: []const Value, loc: SourceLocation)
109109
return error_catalog.raise(.net_connect_failed, loc, .{ .host = host, .port = port_i });
110110
};
111111

112+
return wrapStream(rt, stream);
113+
}
114+
115+
/// A socket Value over the connected `stream`, in a fresh `SocketBox`. The
116+
/// box's buffers are charged to the collect trigger, since the only cell
117+
/// standing for them is a few words. Closes `stream` if the box cannot be made.
118+
fn wrapStream(rt: *Runtime, stream: std.Io.net.Stream) !Value {
112119
const box = rt.gpa.create(SocketBox) catch |e| {
113120
stream.close(rt.io);
114121
return e;
@@ -124,6 +131,7 @@ fn connectFn(rt: *Runtime, env: *Env, args: []const Value, loc: SourceLocation)
124131
};
125132
box.reader = box.stream.reader(box.io, &box.rbuf);
126133
box.writer = box.stream.writer(box.io, &box.wbuf);
134+
rt.gc.noteOwnedAlloc(@sizeOf(SocketBox));
127135

128136
return host_instance.alloc(rt, &descriptor, .{ @intFromPtr(box), 0, 0, 0 });
129137
}
@@ -313,23 +321,7 @@ fn acceptMethod(rt: *Runtime, env: *Env, args: []const Value, loc: SourceLocatio
313321
const stream = safepoint.blocking(std.Io.net.Server.accept, .{ &box.server, box.io }) catch
314322
return error_catalog.raise(.net_io_failed, loc, .{ .op = "accept" });
315323

316-
const sock = rt.gpa.create(SocketBox) catch |e| {
317-
stream.close(rt.io);
318-
return e;
319-
};
320-
sock.* = .{
321-
.io = rt.io,
322-
.stream = stream,
323-
.reader = undefined,
324-
.writer = undefined,
325-
.closed = false,
326-
.rbuf = undefined,
327-
.wbuf = undefined,
328-
};
329-
sock.reader = sock.stream.reader(sock.io, &sock.rbuf);
330-
sock.writer = sock.stream.writer(sock.io, &sock.wbuf);
331-
332-
return host_instance.alloc(rt, &descriptor, .{ @intFromPtr(sock), 0, 0, 0 });
324+
return wrapStream(rt, stream);
333325
}
334326

335327
/// `(.port srv)` — the BOUND port. `listen` with 0 resolves an ephemeral port
@@ -433,23 +425,7 @@ fn connectUnixFn(rt: *Runtime, env: *Env, args: []const Value, loc: SourceLocati
433425
const stream = safepoint.blocking(std.Io.net.UnixAddress.connect, .{ &ua, rt.io }) catch
434426
return error_catalog.raise(.net_unix_connect_failed, loc, .{ .path = path });
435427

436-
const box = rt.gpa.create(SocketBox) catch |e| {
437-
stream.close(rt.io);
438-
return e;
439-
};
440-
box.* = .{
441-
.io = rt.io,
442-
.stream = stream,
443-
.reader = undefined,
444-
.writer = undefined,
445-
.closed = false,
446-
.rbuf = undefined,
447-
.wbuf = undefined,
448-
};
449-
box.reader = box.stream.reader(box.io, &box.rbuf);
450-
box.writer = box.stream.writer(box.io, &box.wbuf);
451-
452-
return host_instance.alloc(rt, &descriptor, .{ @intFromPtr(box), 0, 0, 0 });
428+
return wrapStream(rt, stream);
453429
}
454430

455431
/// `(cljw.net/listen-unix path)` — bind and listen on a UNIX domain socket.

‎src/runtime/gc/gc_heap.zig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ pub const GcHeap = struct {
150150
pub const retainBytesAfterCollect = trigger.retainBytesAfterCollect;
151151
pub const maybeAutoCollect = trigger.maybeAutoCollect;
152152
pub const requestCollect = trigger.requestCollect;
153+
pub const collectBeforeIdle = trigger.collectBeforeIdle;
153154

154155
pub fn init(infra: std.mem.Allocator) GcHeap {
155156
const owner = root_set_mod.currentThreadId();

‎src/runtime/gc/gc_trigger.zig‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
//! so callers write `rt.gc.noteOwnedAlloc(n)`.
1818

1919
const std = @import("std");
20+
const builtin = @import("builtin");
2021
const gc_heap_mod = @import("gc_heap.zig");
2122
const root_set_mod = @import("root_set.zig");
2223

@@ -132,6 +133,28 @@ pub fn requestCollect(gc: *GcHeap) void {
132133
collectIfSafe(gc);
133134
}
134135

136+
/// Allocation pressure below which `collectBeforeIdle` does nothing, so an
137+
/// idle loop that allocates little pays no mark.
138+
pub const idle_collect_min_bytes: usize = 1024 * 1024;
139+
140+
/// Collect while the mutator waits for input (a server between requests),
141+
/// then hand freed malloc memory back to the OS. A no-op below
142+
/// `idle_collect_min_bytes` of pressure. Same safety gates and page
143+
/// release as `requestCollect`.
144+
pub fn collectBeforeIdle(gc: *GcHeap) void {
145+
if (pressureBytes(gc) < idle_collect_min_bytes) return;
146+
requestCollect(gc);
147+
trimProcessHeap();
148+
}
149+
150+
extern "c" fn malloc_trim(pad: usize) c_int;
151+
152+
/// Return glibc's free arena memory to the OS. Other allocators unmap large
153+
/// frees themselves, so this is a no-op off glibc.
154+
fn trimProcessHeap() void {
155+
if (comptime builtin.link_libc and builtin.target.abi.isGnu()) _ = malloc_trim(0);
156+
}
157+
135158
/// True iff a stop-the-world collect may run on this thread right now: no
136159
/// multi-alloc builder is mid-bracket (its intermediate nodes are
137160
/// live-but-unrooted), this is not a registered worker, the collector is not

‎src/runtime/io/host_stream.zig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ fn readByte(rt: *Runtime, env: *Env, args: []const Value, loc: SourceLocation) a
115115
/// stdin mode: append one blocking chunk from process stdin to `data`.
116116
/// A zero-byte read (or error) latches EOF. Mirrors text_io's `*in*` fill.
117117
fn stdinFill(rt: *Runtime, st: *StreamState) !void {
118+
stdin.beforeFill(rt.gc.infra, &st.data, &st.pos, &rt.gc);
118119
var chunk: [4096]u8 = undefined;
119120
const n = stdin.readChunk(rt.io, &chunk) catch {
120121
st.stdin_eof = true;

‎src/runtime/io/stdin.zig‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,89 @@
33
//! `*in*` readers (text_io, host_stream), via `std.Io.File`.
44

55
const std = @import("std");
6+
const builtin = @import("builtin");
67

78
/// Read up to `buf.len` bytes from stdin into `buf`. Returns bytes read
89
/// (0 = EOF).
910
pub fn readChunk(io: std.Io, buf: []u8) std.Io.File.ReadStreamingError!usize {
1011
return std.Io.File.stdin().readStreaming(io, &.{buf});
1112
}
13+
14+
/// How long a stdin fill waits for input before running an idle collect.
15+
pub const idle_poll_ms: i32 = 1000;
16+
17+
/// Wait up to `timeout_ms` for stdin to become readable (data, EOF or
18+
/// hang-up). True when a read will not block. Answers true at once where
19+
/// there is no `poll`, so callers never spin.
20+
pub fn waitReadable(timeout_ms: i32) bool {
21+
if (comptime builtin.os.tag == .wasi or builtin.os.tag == .windows) return true;
22+
var fds = [_]std.posix.pollfd{.{
23+
.fd = std.posix.STDIN_FILENO,
24+
.events = std.posix.POLL.IN,
25+
.revents = 0,
26+
}};
27+
const n = std.posix.poll(&fds, timeout_ms) catch return true;
28+
return n > 0;
29+
}
30+
31+
/// Ready a demand-filled stdin buffer for its next fill: drop its consumed
32+
/// prefix, then wait for input, calling `gc.collectBeforeIdle()` each time
33+
/// `idle_poll_ms` passes with none, so a process blocked on stdin still
34+
/// collects and returns memory.
35+
pub fn beforeFill(infra: std.mem.Allocator, data: *std.ArrayList(u8), pos: *usize, gc: anytype) void {
36+
dropConsumed(infra, data, pos);
37+
while (!waitReadable(idle_poll_ms)) gc.collectBeforeIdle();
38+
}
39+
40+
/// Capacity up to which `dropConsumed` never frees a stdin buffer's excess, so
41+
/// a steady stream of small lines does not realloc per fill.
42+
pub const retained_capacity: usize = 64 * 1024;
43+
44+
/// Drop the consumed prefix `data[0..pos]` of a demand-filled stdin buffer
45+
/// and reset `pos` to 0, so a long-running reader holds only its unread tail.
46+
/// Frees the excess allocation once it is over 4x the tail and
47+
/// `retained_capacity`.
48+
/// Call before appending a fill: the append may move `data` anyway, so no
49+
/// caller holds a slice or offset across it.
50+
pub fn dropConsumed(infra: std.mem.Allocator, data: *std.ArrayList(u8), pos: *usize) void {
51+
const consumed = pos.*;
52+
if (consumed == 0) return;
53+
const rest = data.items.len - consumed;
54+
std.mem.copyForwards(u8, data.items[0..rest], data.items[consumed..]);
55+
data.shrinkRetainingCapacity(rest);
56+
pos.* = 0;
57+
if (data.capacity > retained_capacity and data.capacity > rest * 4)
58+
data.shrinkAndFree(infra, rest);
59+
}
60+
61+
test "dropConsumed keeps the unread tail and resets pos" {
62+
const a = std.testing.allocator;
63+
var data: std.ArrayList(u8) = .empty;
64+
defer data.deinit(a);
65+
try data.appendSlice(a, "line one\nline two");
66+
var pos: usize = 9;
67+
dropConsumed(a, &data, &pos);
68+
try std.testing.expectEqualStrings("line two", data.items);
69+
try std.testing.expectEqual(@as(usize, 0), pos);
70+
}
71+
72+
test "dropConsumed releases a buffer that grew far past its tail" {
73+
const a = std.testing.allocator;
74+
var data: std.ArrayList(u8) = .empty;
75+
defer data.deinit(a);
76+
try data.appendNTimes(a, 'x', 1024 * 1024);
77+
var pos: usize = data.items.len - 3;
78+
dropConsumed(a, &data, &pos);
79+
try std.testing.expectEqual(@as(usize, 3), data.items.len);
80+
try std.testing.expect(data.capacity <= retained_capacity);
81+
}
82+
83+
test "dropConsumed is a no-op at pos 0" {
84+
const a = std.testing.allocator;
85+
var data: std.ArrayList(u8) = .empty;
86+
defer data.deinit(a);
87+
try data.appendSlice(a, "abc");
88+
var pos: usize = 0;
89+
dropConsumed(a, &data, &pos);
90+
try std.testing.expectEqualStrings("abc", data.items);
91+
}

‎src/runtime/io/text_io.zig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ const ReaderState = struct {
174174
/// TTY is the correct clj-parity behaviour (read-line waits for input).
175175
fn fillFromStdin(rt: *Runtime, st: *ReaderState) bool {
176176
if (st.stdin_eof) return false;
177+
stdin.beforeFill(rt.gc.infra, &st.data, &st.pos, &rt.gc);
177178
var chunk: [4096]u8 = undefined;
178179
const n = stdin.readChunk(rt.io, &chunk) catch {
179180
st.stdin_eof = true;

‎src/runtime/java/lang/StringBuilder.zig‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,15 @@ fn appendStr(rt: *Runtime, env: *Env, lp: *ByteList, v: Value) !void {
5050
var aw: std.Io.Writer.Allocating = .init(rt.gpa);
5151
defer aw.deinit();
5252
try print_mod.writeStrValue(rt, env, &aw.writer, v);
53-
try lp.appendSlice(rt.gc.infra, aw.writer.buffered());
53+
try appendCharged(rt, lp, aw.writer.buffered());
54+
}
55+
56+
/// Append `bytes` to `lp`, charging any buffer growth to the collect trigger:
57+
/// the builder's cell is a few words however large the buffer gets.
58+
fn appendCharged(rt: *Runtime, lp: *ByteList, bytes: []const u8) !void {
59+
const before = lp.capacity;
60+
try lp.appendSlice(rt.gc.infra, bytes);
61+
if (lp.capacity > before) rt.gc.noteOwnedAlloc(lp.capacity - before);
5462
}
5563

5664
/// `(StringBuilder.)` / `(StringBuilder. "seed")` / `(StringBuilder. n)` —
@@ -90,7 +98,7 @@ fn append(rt: *Runtime, env: *Env, args: []const Value, loc: SourceLocation) any
9098
const to = byteOffsetOfCodepoint(s, to_i);
9199
if (from_i < 0 or to_i < from_i or from == null or to == null)
92100
return error_catalog.raise(.index_out_of_range, loc, .{ .fn_name = "java.lang.StringBuilder/append" });
93-
try listOf(args[0]).appendSlice(rt.gc.infra, s[from.?..to.?]);
101+
try appendCharged(rt, listOf(args[0]), s[from.?..to.?]);
94102
return args[0];
95103
}
96104

0 commit comments

Comments
 (0)