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.
79 lines
2.3 KiB
Go
79 lines
2.3 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 the memory Go holds is limited to what it held when the read
|
|
// began, and the limit set before is put back; a lower limit the user set
|
|
// is kept, and GOGC off gets none.
|
|
func TestUnderTightGCLimitsMemory(t *testing.T) {
|
|
defer debug.SetGCPercent(debug.SetGCPercent(100))
|
|
defer debug.SetMemoryLimit(debug.SetMemoryLimit(math.MaxInt64))
|
|
for _, c := range []struct {
|
|
name string
|
|
gogc int
|
|
before int64
|
|
low bool
|
|
}{
|
|
{"default", 100, math.MaxInt64, true},
|
|
{"user limit lower", 100, 1 << 20, false},
|
|
{"GOGC off", -1, math.MaxInt64, false},
|
|
} {
|
|
debug.SetGCPercent(c.gogc)
|
|
debug.SetMemoryLimit(c.before)
|
|
var during int64
|
|
underTightGC(func() { during = debug.SetMemoryLimit(-1) })
|
|
if c.low && during >= c.before {
|
|
t.Errorf("%s: read under limit %d, want what Go held", c.name, during)
|
|
}
|
|
if !c.low && during != c.before {
|
|
t.Errorf("%s: read under limit %d, want %d kept", c.name, during, c.before)
|
|
}
|
|
if after := debug.SetMemoryLimit(-1); after != c.before {
|
|
t.Errorf("%s: after the read the limit is %d, want %d", c.name, after, c.before)
|
|
}
|
|
}
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
}
|