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.
This commit is contained in:
yetone
2026-10-27 14:31:17 +08:00
parent 6eb510f788
commit 353519f557
3 changed files with 87 additions and 9 deletions
+7
View File
@@ -2401,6 +2401,13 @@ func held(h http.Handler) http.Handler {
defer sessions.Ask()()
// and what the read threw away goes back once the page rests
defer readingPage()()
// and collected under GOGC 25 while it runs: a poll's reads
// allocated ~330 MB to keep ~105 MB, and at the default 100 the
// heap grew to twice what it kept before a collection, ~320 MB
// in Activity Monitor during a poll. The memory limit the
// warm-up's read takes too isn't taken here: held as close as
// heldMemory, every poll collected over and over, 5x the CPU.
defer tightenGC(false)()
}
if !(api && r.Method == http.MethodGet) {
// anything that may have written drops what was held, once it
+49 -9
View File
@@ -599,18 +599,58 @@ func warmUp(steps ...func()) {
// 247–255 MB. Under the limit, 241–251 MB and 211–228 MB, for 1–7 s more
// CPU in that build.
func underTightGC(fn func()) {
was := debug.SetGCPercent(tightGCPercent)
if was < tightGCPercent {
debug.SetGCPercent(was)
} else {
defer debug.SetGCPercent(was)
}
if limit := heldMemory(); was >= 0 && limit < debug.SetMemoryLimit(-1) {
defer debug.SetMemoryLimit(debug.SetMemoryLimit(limit))
}
defer tightenGC(true)()
fn()
}
// tightGC is what the reads under a tight GC hold: the first to begin sets
// it, and the last to end puts back what was set before it. Each read
// putting back what it found let two at once leave GOGC at 25 for good,
// when the first to begin ended before the second.
var tightGC struct {
sync.Mutex
n, limits int // reads under it, and those of them limited
was int // GOGC before the first
limit int64 // the memory limit before the first limited one
limited bool // whether that one set one
}
// tightenGC begins a read under a tight GC, with the memory limit too when
// limit is set; undo ends it.
func tightenGC(limit bool) (undo func()) {
tightGC.Lock()
defer tightGC.Unlock()
if tightGC.n == 0 {
was := debug.SetGCPercent(tightGCPercent)
if was < tightGCPercent {
debug.SetGCPercent(was)
}
tightGC.was = was
}
tightGC.n++
if limit {
if tightGC.limits == 0 {
tightGC.limited = false
if held := heldMemory(); tightGC.was >= 0 && held < debug.SetMemoryLimit(-1) {
tightGC.limit, tightGC.limited = debug.SetMemoryLimit(held), true
}
}
tightGC.limits++
}
return sync.OnceFunc(func() {
tightGC.Lock()
defer tightGC.Unlock()
if limit {
if tightGC.limits--; tightGC.limits == 0 && tightGC.limited {
debug.SetMemoryLimit(tightGC.limit)
}
}
if tightGC.n--; tightGC.n == 0 {
debug.SetGCPercent(tightGC.was)
}
})
}
// heldMemory is what Go holds of the system's memory: all it mapped, less
// the pages it gave back.
func heldMemory() int64 {
+31
View File
@@ -66,6 +66,37 @@ func TestUnderTightGCLimitsMemory(t *testing.T) {
}
}
// 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(true)
second := tightenGC(false)
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) {