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).
This commit is contained in:
yetone
2026-10-21 19:12:19 +08:00
parent 1bd99194ea
commit 1414705159
2 changed files with 64 additions and 1 deletions
+19 -1
View File
@@ -506,7 +506,7 @@ func warmSteps() []func() {
return []func(){
func() { statsFor(0) },
func() { statsFor(30) },
func() { usage.QueryPage(usage.All, usage.Filter{}, 0, 50) },
func() { underTightGC(func() { usage.QueryPage(usage.All, usage.Filter{}, 0, 50) }) },
func() {
if slices.ContainsFunc(statsFor(0).Keys, func(k string) bool { return strings.HasPrefix(k, "codex:") }) {
warmCodexEffort()
@@ -531,6 +531,24 @@ func warmUp(steps ...func()) {
debug.FreeOSMemory()
}
// underTightGC runs fn letting the heap grow by a quarter of what it holds
// before a collection, not Go's default of all of it, and then puts back
// what was set; a GOGC the user set lower, or off, is kept. The usage
// page's read of the whole history holds every row of it at once (~270 MB
// live for 390k calls), and a cold start that rests at ~200 MB peaked at
// 555–587 MB in Activity Monitor there; under it, 410–438 MB. Only that
// read runs under it: the warm-up as a whole did too for ~11 s more CPU.
func underTightGC(fn func()) {
if was := debug.SetGCPercent(tightGCPercent); was < tightGCPercent {
debug.SetGCPercent(was)
} else {
defer debug.SetGCPercent(was)
}
fn()
}
const tightGCPercent = 25
// openTerminal runs a command in the chosen Mac terminal through a .command
// file, as it would open from Finder: no Automation consent.
// The shell is left open when the agent quits.
+45
View File
@@ -0,0 +1,45 @@
package gui
import (
"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)
}
}
}
// 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)
}
}