Files
magpie/internal/gui/warm_gc_test.go
T
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

89 lines
2.7 KiB
Go

package gui
import (
"math"
"runtime/debug"
"testing"
)
func gcPercent() int {
p := debug.SetGCPercent(100)
debug.SetGCPercent(p)
return p
}
// The usage page's read of the whole history runs under a tighter GOGC, so
// its heap (every row at once) isn't let grow to twice that, and puts back
// what was set when it ends; a lower GOGC, or off, is kept as it is.
func TestUnderTightGC(t *testing.T) {
defer debug.SetGCPercent(debug.SetGCPercent(100))
for _, before := range []int{100, 200, 10, -1} {
debug.SetGCPercent(before)
var during int
underTightGC(func() { during = gcPercent() })
want := min(before, tightGCPercent)
if before < 0 {
want = before
}
if during != want {
t.Errorf("GOGC=%d: read under %d, want %d", before, during, want)
}
if after := gcPercent(); after != before {
t.Errorf("GOGC=%d: after the read it is %d", before, after)
}
}
}
// Under it no memory limit is set: the warm-up's read held at what Go
// held when it began collected every ~160 ms for 13 s of a cold start.
func TestUnderTightGCSetsNoMemoryLimit(t *testing.T) {
defer debug.SetMemoryLimit(debug.SetMemoryLimit(math.MaxInt64))
var during int64
underTightGC(func() { during = debug.SetMemoryLimit(-1) })
if during != math.MaxInt64 {
t.Errorf("read under limit %d, want none", during)
}
}
// Two reads under it at once put back what was set before the first, in
// either order they end: each putting back what it found left the second's
// 25 when the first ended before it.
func TestTightGCOverlappingReads(t *testing.T) {
defer debug.SetGCPercent(debug.SetGCPercent(100))
defer debug.SetMemoryLimit(debug.SetMemoryLimit(math.MaxInt64))
for _, firstEndsFirst := range []bool{true, false} {
first := tightenGC()
second := tightenGC()
if p := gcPercent(); p != tightGCPercent {
t.Fatalf("two reads under GOGC=%d, want %d", p, tightGCPercent)
}
if firstEndsFirst {
first()
} else {
second()
}
if p := gcPercent(); p != tightGCPercent {
t.Errorf("one read still in flight under GOGC=%d, want %d", p, tightGCPercent)
}
first()
second()
if p := gcPercent(); p != 100 {
t.Errorf("first ends first %v: after both GOGC=%d, want 100", firstEndsFirst, p)
}
if l := debug.SetMemoryLimit(-1); l != math.MaxInt64 {
t.Errorf("first ends first %v: after both the limit is %d", firstEndsFirst, l)
}
}
}
// The warm-up's usage read is the one under it, and the warm-up's other
// reads are not.
func TestWarmUpTightensOnlyTheUsageRead(t *testing.T) {
defer debug.SetGCPercent(debug.SetGCPercent(100))
var during int
warmUp(func() { during = gcPercent() })
if during != 100 {
t.Fatalf("a warm-up step read under GOGC=%d, want 100", during)
}
}