M9 step 1: memory cheap wins (S9) #3

Closed
opened 2026-10-02 00:16:05 +00:00 by jhgaylor · 1 comment
Owner

Blocks: #10

Split out of M9: S9 found the trigger already holds, but these cheap fixes come before any parking. Rerun the S9 benchmark afterwards.

From PLAN.md, section "M9: parking (scale)".


  • S9: measured 2026-10-01; the trigger already holds, but parking isn't the
    first fix.
    See spikes/s9-memory.
    • Idle cost: an idle, empty pane costs 3.1–3.3 MB of daemon RSS (50
      panes: 172 MB; 500: 1.6 GB, with 5 threads per pane). Shims and bash add
      about 1.7 MB more, so 500 idle shells cost about 2.5 GB in all.
    • Scrollback costs about 1.7 KB per row at 200 columns: 10k lines is
      33 MB per pane, and the 64 MiB cap is about 160 MB.
    • Memory isn't given back after panes close. glibc keeps it: 500 panes
      closed down to 1 still hold 191 MB.
  • Step 1: cheap wins, before any parking. Then rerun the S9 benchmark.
    • Drop Zig's 256 KiB per-thread signal stack in libghostty, which glibc
      gives every thread: 1.3 MB per pane. It's a one-line patch
      (ghostty-no-signal-stack.patch) to carry and send upstream.
    • Avoid libghostty's ReleaseSafe page fill: 1.5 MB per screen.
      ReleaseFast plus the patch measured 0.48 MB per idle pane, which already
      meets M9's done bar. But ReleaseFast drops safety checks on untrusted
      program output, so it's a decision (open); an upstream fix that avoids
      the fill would remove the trade.
    • Fix the malloc mmap threshold (mallopt at startup, or another
      allocator) and call malloc_trim after a pane closes. That saves about
      13 MB per pane at 10k lines, and memory comes back.
    • The output ring holds 4 MiB, not 2 MiB, because Ring::push extends
      before draining. Set it to 1 MiB (replay never uses more than
      MAX_REPLAY_BYTES) and drain first.
    • Lower the in-memory scrollback cap from 64 to 16 MiB (about 28 MB per
      pane). Full history is on disk.
    • Fold the reaper thread into the wait thread, and use a tiny shim
      binary instead of re-running the 21 MB daemon (about 0.3 GB at 500
      panes).
  • Step 2: parking, only if panes with real history still blow the budget.
    S9 found nothing that argues for PTY or client-buffer parking. Parking
    can't save the shells and shims either (0.75–0.87 GB at 500 panes).
  • Stalled clients: a client that stops reading cost about 28 MB in one
    burst, and up to about 64 MB per client from the 1,024-frame queue. Cap the
    queue in bytes, not frames.
  • CI benchmark: S9's bench.py at 50 idle panes (3.07–3.11 MB per pane
    over four runs) is the regression test M9 wants. Leave headroom.
**Blocks:** #10 Split out of M9: S9 found the trigger already holds, but these cheap fixes come before any parking. Rerun the S9 benchmark afterwards. _From PLAN.md, section "M9: parking (scale)"._ --- - **S9: measured 2026-10-01; the trigger already holds, but parking isn't the first fix.** See [spikes/s9-memory](https://git.inevitable.fyi/jhgaylor/illogical/src/branch/main/spikes/s9-memory/README.md). - **Idle cost:** an idle, empty pane costs 3.1–3.3 MB of daemon RSS (50 panes: 172 MB; 500: 1.6 GB, with 5 threads per pane). Shims and bash add about 1.7 MB more, so 500 idle shells cost about 2.5 GB in all. - **Scrollback** costs about 1.7 KB per row at 200 columns: 10k lines is 33 MB per pane, and the 64 MiB cap is about 160 MB. - **Memory isn't given back after panes close.** glibc keeps it: 500 panes closed down to 1 still hold 191 MB. - **Step 1: cheap wins, before any parking.** Then rerun the S9 benchmark. - **Drop Zig's 256 KiB per-thread signal stack in libghostty**, which glibc gives every thread: 1.3 MB per pane. It's a one-line patch (`ghostty-no-signal-stack.patch`) to carry and send upstream. - **Avoid libghostty's ReleaseSafe page fill:** 1.5 MB per screen. ReleaseFast plus the patch measured 0.48 MB per idle pane, which already meets M9's done bar. But ReleaseFast drops safety checks on untrusted program output, so it's a decision (open); an upstream fix that avoids the fill would remove the trade. - **Fix the malloc mmap threshold** (`mallopt` at startup, or another allocator) and call `malloc_trim` after a pane closes. That saves about 13 MB per pane at 10k lines, and memory comes back. - **The output ring holds 4 MiB, not 2 MiB,** because `Ring::push` extends before draining. Set it to 1 MiB (replay never uses more than `MAX_REPLAY_BYTES`) and drain first. - **Lower the in-memory scrollback cap** from 64 to 16 MiB (about 28 MB per pane). Full history is on disk. - **Fold the reaper thread into the wait thread,** and use a tiny shim binary instead of re-running the 21 MB daemon (about 0.3 GB at 500 panes). - **Step 2: parking, only if panes with real history still blow the budget.** S9 found nothing that argues for PTY or client-buffer parking. Parking can't save the shells and shims either (0.75–0.87 GB at 500 panes). - **Stalled clients:** a client that stops reading cost about 28 MB in one burst, and up to about 64 MB per client from the 1,024-frame queue. Cap the queue in bytes, not frames. - **CI benchmark:** S9's `bench.py` at 50 idle panes (3.07–3.11 MB per pane over four runs) is the regression test M9 wants. Leave headroom.
Author
Owner

Done: merged as 7b91972 (Merge M9 step 1). PLAN.md "#### M9 step 1". Stayed ReleaseSafe as decided.

  • libghostty signal-stack patch: carried in vendor/libghostty-vt-sys/patches/.
  • glibc heap: mallopt at startup and malloc_trim after a pane closes.
  • Output ring: 1 MiB, drained before it grows.
  • Scrollback in memory: capped at 16 MiB.
  • Also taken: the reaper is folded into the wait thread, and the client queue is capped at 8 MiB of live output.
  • Not taken: the tiny shim binary. PLAN.md says why.

S9 benchmark rerun on geek, before (2440cc3) and after:

scenario before after
idle, per pane at 10 / 50 / 200 / 500 panes 3.48 / 3.33 / 3.30 / 3.30 MB 2.15 / 1.88 / 1.81 / 1.78 MB
500 idle panes, daemon total 1,638 MB 887 MB
500 panes closed down to 1 212 MB 35 MB
500 idle shells with shim and bash 2.61 GB 1.74 GB
10k lines of scrollback, per pane 31.7 MB 17.8 MB
200k lines (at the cap), per pane 149.3 MB 18.2 MB

Regression check: crates/daemon/tests/memory.rs runs in just check on Linux. It allows at most 2.6 MB per idle pane at 50 panes, and at most 8 MB kept after closing 49 of them.

Upstream drafts for Jake: #63.

#10's trigger: the per-idle-pane part (over 2 MB) no longer holds, at 1.78 MB. M9's done bar (under 1 MB) still isn't met; about 1.5 MB of what's left is libghostty's ReleaseSafe page fill, which the #63 upstream fix would remove and parking wouldn't. A pane with full scrollback now costs about 18 MB, so 500 of them would be about 9 GB; that is where step-2 parking would help. Batch 3 decides #10 on these numbers.

Done: merged as 7b91972 (Merge M9 step 1). PLAN.md "#### M9 step 1". Stayed ReleaseSafe as decided. - **libghostty signal-stack patch:** carried in `vendor/libghostty-vt-sys/patches/`. - **glibc heap:** `mallopt` at startup and `malloc_trim` after a pane closes. - **Output ring:** 1 MiB, drained before it grows. - **Scrollback in memory:** capped at 16 MiB. - **Also taken:** the reaper is folded into the wait thread, and the client queue is capped at 8 MiB of live output. - **Not taken:** the tiny shim binary. PLAN.md says why. S9 benchmark rerun on geek, before (2440cc3) and after: | scenario | before | after | |---|---|---| | idle, per pane at 10 / 50 / 200 / 500 panes | 3.48 / 3.33 / 3.30 / 3.30 MB | 2.15 / 1.88 / 1.81 / 1.78 MB | | 500 idle panes, daemon total | 1,638 MB | 887 MB | | 500 panes closed down to 1 | 212 MB | 35 MB | | 500 idle shells with shim and bash | 2.61 GB | 1.74 GB | | 10k lines of scrollback, per pane | 31.7 MB | 17.8 MB | | 200k lines (at the cap), per pane | 149.3 MB | 18.2 MB | **Regression check:** `crates/daemon/tests/memory.rs` runs in `just check` on Linux. It allows at most 2.6 MB per idle pane at 50 panes, and at most 8 MB kept after closing 49 of them. **Upstream drafts for Jake:** #63. **#10's trigger:** the per-idle-pane part (over 2 MB) no longer holds, at 1.78 MB. M9's done bar (under 1 MB) still isn't met; about 1.5 MB of what's left is libghostty's ReleaseSafe page fill, which the #63 upstream fix would remove and parking wouldn't. A pane with full scrollback now costs about 18 MB, so 500 of them would be about 9 GB; that is where step-2 parking would help. Batch 3 decides #10 on these numbers.
jhgaylor referenced this issue from a commit 2026-10-02 12:48:47 +00:00
Commenting is not possible because the repository is archived.
No milestone
No project
No assignees
1 participant
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Blocks
#10 M9: parking (scale)
jhgaylor/illogical
Reference
jhgaylor/illogical#3
No description provided.