Commit Graph
6 Commits
Author SHA1 Message Date
yetone c76bcaea7a gui: between reads the heap grows by half of what it holds, not all of it
The window and the app's page, resting on ~60 MB live, poll /api/state,
/api/drift and /api/providers and throw ~10 MB away a round. At Go's
default GOGC of 100 the heap grew to twice what it kept before each
collection, so the footprint stood at 121-135 MB and peaked at 136-138
MB for a ~60 MB heap.

steadyGCStart, run once with the warm-up, sets GOGC to half, or to what
lets the heap grow by 32 MB when that is more (never above 100), and
sets it again for the live heap after each collection, through the
retightenGC hook the tight reads already use. The usage and sessions
reads under a tight GC still set their tenth while they run, and the
steady value comes back after them. A GOGC the user set in the
environment is kept, and nothing is retuned. No memory limit (#2278).

Run (mote, at this tree):
- go test -count=1 ./internal/gui: ok except TestSettingsSaveKeepsWhatItDoesNotSend
  (#4514) and TestMainWindowCorners (#3949), the known reds.
- -run 'GC|Steady|Tight' 3 times and once under -race: ok.
- Broken by hand: ignoring GOGC in the environment, TestSteadyGCKeepsTheUsersGOGC
  is red ("GOGC=200 set by the user, magpie runs under 50"); with no
  retune after a collection, TestSteadyGCRetunesAfterACollection is red
  ("GOGC after collections = 50, want 100").
- gofmt -l, go vet, GOOS=windows go vet -tags nogui ./internal/gui: clean.
- Real accounts, magpie web in a sandbox HOME, 40 rounds of the three
  polls, twice each, alternating, v0.1.2507 against this change:
    phys_footprint_peak  136 / 137 MB  ->  115 / 116 MB
    phys_footprint end   135 / 121 MB  ->   96 /  94 MB
    CPU of the polls     3.84 / 3.98s  ->  4.05 / 4.24s
  /api/state and /api/drift byte-identical.
  GOGC=50 in the environment gave 107-108 MB at +9% CPU; the 32 MB room
  keeps a small heap from collecting that often.

Not run: the desktop window (Wails) and Windows/Linux machines; the
tray app takes the same Handler path. A large usage history between
reads (~280 MB live) now grows by ~140 MB, not ~280, before a
collection; not measured here.

Picked onto 4179cf2d8 (pii: pf-serve-base for CPUs without AVX2; no file in common); not run again there.
2026-11-01 08:49:18 +08:00
yetone 0bf5adb118 gui: a tight GC lets a small heap grow by 32 MB, not a tenth (#2278)
The usage and sessions reads run under GOGC=10. On a first start they
begin on a ~50 MB live heap and allocate ~400 MB a second, so a tenth
was a collection every few MB: 2,102 to 2,239 collections in the first
75 s, about 70 s of the start's CPU, and each one emptied the zstd and
bufio pools, which were then allocated again.

While a read is under a tight GC, each collection now sets GOGC for
the heap it left: tightPercent gives a tenth, or enough for 32 MB of
growth, never above what was set before (a lower or off GOGC is kept).
A heap over 320 MB is held to a tenth as before. The hook is a
finalizer re-armed after each collection while a read is under it.

Cold start on mote's copy of the real history (75 s, two runs each,
v0.1.2330's lastIndex build vs this):
- CPU 3:46.66 / 4:00.50 -> 3:08.69 / 3:10.04 (user 3:18.97 / 3:28.67 ->
  2:44.23 / 2:45.85)
- collections 2,239 / 2,102 -> 706 / 705
- phys_footprint_peak 318 / 312 -> 330 / 335 MB
- heat, requests, overview 0/7/30 and stats 0/30 answers byte-identical
A 16 MB room: 3:46.86 / 3:38.78 CPU, 1,554 / 1,390 collections, peak
331 / 323 MB, so it buys little.

Tests (mote): go vet ./internal/gui; go test ./internal/gui -run
'TightGC|WarmUp|UnderTight|TightPercent|WarmCodex' ok.
TestTightPercentLeavesRoom pins the bound; TestTightGCRetunesAfterACollection
checks a collection retunes GOGC and the read's end restores it.
Broken by hand: retightenGC not setting GOGC turns
TestTightGCRetunesAfterACollection red.
Not tried: the full gui package (TestCoveredWindowStaysUp is killed on
mote, #4125); Windows and Linux runs.
2026-10-31 13:03:34 +08:00
yetone c7ae24f0a6 gui: the warm-up's usage read sets no memory limit, a cold start stops collecting nonstop (#2278)
The warm-up's QueryPage(All) and Codex effort ran under tightenGC(true):
GOGC 25 and a soft memory limit at what Go held when the read began. On
a cold start the heap reached that limit all live, and gctrace showed a
collection every ~160 ms from ~11 s to ~24 s, heap = live = limit, about
one core the whole time.

Now the read takes GOGC 25 only, as a page's request already did, and
tightenGC lost its limit parameter and heldMemory.

Measured on mote (M5 Pro), cold start with no caches, 390k calls in
usage.jsonl plus a growing Codex rollout, 10 endpoints polled every 5 s,
60 s each (coldx.sh: footprint each second, gctrace, ps cpu time):
- before (v0.1.2111 head): CPU 3:39.33, 1166 GCs, phys_footprint_peak 855 MB
- after: CPU 2:05.86 / 2:04.60 / 2:02.78, 618 GCs, peak 860 / 835 / 863 MB
The cold start reads no request index, so it builds one from every
session, the case the limit was added for; its peak is unchanged.

Tests at this head, through mote-run:
- go vet -tags nogui ./... for GOOS linux, windows and darwin: ok
- go test -timeout 45m ./internal/gui/ ./internal/usage/ ./internal/sessions/:
  usage, sessions ok; gui only TestMainWindowCorners/macOS_26_SDK, #3949.
TestUnderTightGCLimitsMemory became TestUnderTightGCSetsNoMemoryLimit;
the old code set a limit below MaxInt64, so it would be red there (not
run on the old code: tightenGC's signature changed).
2026-10-30 06:34:53 +08:00
yetone 353519f557 gui: a Sessions or Usage poll's reads are collected under GOGC 25, and two reads under it at once put back what was set before
An open Sessions or Usage window polls every 15 s, and a poll's reads
allocate ~330 MB to keep ~105 MB. At GOGC 100 the heap grew to twice
what it kept before a collection, so Activity Monitor showed ~280-360 MB
during each poll. Each /api/sessions* and /api/usage* GET now runs under
GOGC 25, the warm-up's usage read's setting, without the memory limit
that read also takes: held at heldMemory, every poll collected over and
over and the window spent 5x the CPU.

underTightGC put back what it found, so two reads at once ended with the
second's 25 for good when the first ended first. A count now holds it:
the first read sets GOGC (and the limit, for the warm-up), the last puts
back what was there before the first.

Measured in a sandbox HOME over the real session and usage data (6 GUI
polls 15 s apart after a warm start, footprint right after each poll /
10 s later; CPU over the 6 polls):
- origin/main: 326 326 326 290 364 281 MB / 188-217 MB; 5.2 s
- GOGC 25 + memory limit: 190 176 172 161 216 186 / 149-172; 20.7 s
- this (GOGC 25 only): 278 217 220 197 248 214 / 186-209; 5.6 s
/api/sessions 495 ms vs 653 ms on main, the other routes the same.

TestTightGCOverlappingReads ends two overlapping reads in either order;
with the count taken out (each read putting back what it found) it is red,
"one read still in flight under GOGC=100, want 25".

Run at this head (ea7c0067e + this): gofmt -l clean; GOOS=linux, windows,
darwin go vet -tags nogui ./internal/gui/ clean; go test -timeout 45m
./internal/gui/ ok (203.3s; a run before it had three
reds that were all "no space left on device", the disk filled by
another build of mine). Then rebased onto 6eb510f78, whose only
internal/gui change is web.go's home-screen manifest: gofmt and the
three vets clean again, and -run 'TightGC|Warm|Held|PageRead|Sessions'
./internal/gui/ ok.
Not tried: the Linux and Windows apps' own footprint.
2026-10-27 14:31:17 +08:00
yetone 99de8e1ad1 gui: the warm-up's whole-history reads hold Go to the memory it held when they began (#3493)
A cold build of the request index (after any update that changes
internal/usage) reads every session again and allocates ~1.2 GB to keep
~40 MB more. Even under GOGC 25 the heap overshot its goal into pages it
kept, so a cold start peaked ~70 MB over a warm one. underTightGC now also
sets a soft memory limit at what Go holds of the system's memory when its
read begins (total mapped less released), and puts the old limit back
after. A lower limit the user set is kept, and GOGC off gets none.

Real data, 15.7k sessions (sbpairs: copies of the real cache/config, the
real Claude Code and Codex session folders linked read-only), harness
TestZZWarm running warmSteps under sessions.Warm, phys_footprint_peak,
old = 9dc7d6eb8, alternated:
  cold (requests-v1.bin removed): old 313/308/320/323 MB, new 251/241/247/249
  warm: old 247/255/250, new 211/225/228
  at rest after: unchanged (old 186-200, new 174-190)
  user CPU, cold run: old 4.8-5.9 s, new 6.0-12.8 s (load 120-200 on the
  box, wall time the same: 2.8-4.9 s vs 3.0-5.9 s)
Fixed 220/200 MB limits were tried first: 200 doubled CPU; a limit relative
to what is held scales to other histories.

Run: go test -count=1 ./internal/gui ok (132s); the first full run had one
red, TestRestartHandsTheGatewayOver (handover, #3451, commented), green 3 of
3 alone. GOOS=linux and GOOS=windows go vet -tags nogui ./internal/gui ok,
gofmt clean.
Broken by hand, TestUnderTightGCLimitsMemory red each time: no limit set;
a limit under GOGC off; the user's lower limit replaced.
Not tried: Windows with real sessions (the box has none); a history much
larger than 15.7k sessions.
2026-10-25 17:14:38 +08:00
yetone 1414705159 gui: the warm-up's read of the whole usage history runs under GOGC 25, so a cold start peaks ~130 MB lower
The warm-up's last big read, the Usage page's QueryPage over all time,
holds every row of the history unpacked at once (129 MB of rows, 271 MB
live at that moment for this history), and at Go's default GOGC the heap
grows to twice what is live before collecting: Activity Monitor's
footprint peaked at 555-587 MB there, on a magpie that rests at ~200 MB
once it is done. underTightGC lets the heap grow by a quarter during that
read and puts back what was set; a GOGC the user set lower, or off, is
kept. Holding fewer rows at once needs the page built in passes over
packed chunks (rowRef points into unpacked ones), which is the next step.

The whole warm-up under GOGC 25 gave the same peak for ~11 s more CPU, so
only that read is.

Measured with startheap-runs/fp.sh: cold start (calls and sessions caches
deleted) of a 6,437-file history, phys_footprint sampled every 2 s and
footprint's own phys_footprint_peak at the end, run until the caches stop
growing for 30 s; every run wrote the same 6,437 files / 70,069,451 bytes.
Machine at load ~85-140 from other sessions.

  before (e68a0b1db)        peak 555, 587 MB   cpu 2:01 (one run)
  whole warm-up at 25       peak 438, 410 MB   cpu 2:12, 2:14
  this commit               peak 421, 456 MB   cpu 2:05, 2:01
  at rest after, all        183-215 MB

(ps RSS reads 500-720 MB at rest in every build: macOS counts the pages Go
already gave back with MADV_FREE until it takes them; heapReleased was
438-678 MB of that.)

Run:
- TestUnderTightGC (new): the read runs under 25 from GOGC 100 and 200,
  under 10 from 10, and with GC off from off; each is put back after.
  TestWarmUpTightensOnlyTheUsageRead: other warm-up steps run at 100.
- go test -run 'TestWarmUp|TestUnderTightGC' ./internal/gui ok.
- gofmt -l clean.
- go test -tags nogui -count=1 ./internal/gui (the whole package) ok
  77.2s; GOOS=linux and GOOS=windows go vet -tags nogui ./internal/gui
  ok. No other test sets or reads GOGC (grep SetGCPercent/FreeOSMemory
  in *_test.go). The full suite's last run (e58108c3b, the commit before)
  had every package ok but internal/agent at the 45m deadline under load
  ~120; alone it passed in 2213s (noted on #2544).
2026-10-21 19:12:19 +08:00