~ knotless Search/
↑↓ move↵ open⇧↵ all results

zat.dev/zlay

atproto relay in zig · mirror of https://tangled.org/zat.dev/zlay

0
0
65 lines · 3.4 KB Raw History

memory floor after 18:40Z burst — engineer response 2026-05-30

Punchline: the post-burst RSS floor is glibc per-thread-arena fragmentation, not a leak. The scavenger-handoff doc's mechanism is right in spirit but wrong on two facts: zlay uses glibc c_allocator, not SmpAllocator, and we already call malloc_trim(0) every 10 min. So the fix isn't a custom madvise scavenger — it's (1) see all arenas, (2) cap them.

what was actually wrong with the diagnosis

  • allocator: src/main.zig:167 and broadcaster.zig:161 use std.heap.c_allocator (glibc malloc). The relay_malloc_* metrics are literally glibc mallinfo fields.
  • trim already runs: gcLoop (main.zig:477) calls malloc_trim(0) every 10 min and it is not recovering this memory — because malloc_trim only returns the contiguous free top of each arena. Free chunks stranded below a live chunk stay put. Classic sticky fragmentation, worst-cased by a burst interleaving short/long-lived allocs across thousands of per-thread arenas.
  • the dashboard was half-blind: mallinfo() reports the main arena only (broadcaster.zig:1243). With ~2670 worker threads glibc spreads allocations across many per-thread arenas, so the relay_malloc_free_bytes / relay_malloc_arena_bytes numbers in the handoff table understate the true heap. The 2.4 GiB delta likely lives in arenas the old metrics never counted.

change shipped (code)

Added all-arena gauges via malloc_info() (covers every arena, not just main):

  • relay_malloc_system_bytes — total bytes from the OS across all arenas
  • relay_malloc_free_total_bytes — free bytes held in all arenas (fast+rest), not returned
  • relay_malloc_arena_count — live arena count (the knob MALLOC_ARENA_MAX controls)

Validated end-to-end: parser unit-tested against sample + real glibc malloc_info output in a debian container (16 threads → 4 arenas, top-level totals parsed exactly). Existing relay_malloc_* (main-arena) gauges kept for dashboard continuity.

Watch relay_malloc_free_total_bytes and relay_malloc_arena_count across the next burst — that's the previously-invisible fragmentation.

operator action — reversible, no rebuild

Set MALLOC_ARENA_MAX in the zlay deployment env. It's read by glibc at libc init; fewer arenas = far less fragmentation surface (the single highest-leverage glibc RSS knob for thread-heavy services). Start at 4, watch relay_malloc_arena_count settle there and relay_malloc_free_total_bytes after a burst; if throughput holds, try 2.

        env:
          - name: MALLOC_ARENA_MAX
            value: "4"

Trade-off: fewer arenas means more malloc-lock contention. The new arena_count / free_total gauges + existing throughput metrics tell us if we went too tight — back off to a higher value if fr/s dips. Fully reversible, no binary change.

if glibc tuning isn't enough

Swap to jemalloc or mimalloc (LD_PRELOAD or link) — both ship background purger threads, the real analog to Go's scavenger the handoff wanted. Bigger change; only if ARENA_MAX + watching the new metrics shows the floor still ratchets.

still open

What caused the 5.7 GiB transient burst — no slow-consumer / queue-full / backpressure / decode-error detector fired. Separate puzzle, likely needs per-consumer queue-depth attribution. The arena cap shrinks the aftermath; it doesn't explain the spike.