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).
This commit is contained in:
+4
-4
@@ -2571,10 +2571,10 @@ func held(h http.Handler) http.Handler {
|
||||
// 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)()
|
||||
// in Activity Monitor during a poll. No memory limit: held as
|
||||
// close as what Go held, every poll collected over and over,
|
||||
// 5x the CPU.
|
||||
defer tightenGC()()
|
||||
}
|
||||
if !(api && r.Method == http.MethodGet) {
|
||||
// anything that may have written drops what was held, once it
|
||||
|
||||
+11
-39
@@ -9,7 +9,6 @@ import (
|
||||
"os"
|
||||
"runtime"
|
||||
"runtime/debug"
|
||||
"runtime/metrics"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -640,16 +639,14 @@ func warmUp(steps ...func()) {
|
||||
// still holds its rows, run under it: the warm-up as a whole did too for
|
||||
// ~11 s more CPU.
|
||||
//
|
||||
// It also holds the memory Go asks of the system to what it held when fn
|
||||
// began, as a soft limit (a lower one the user set is kept, and GOGC off
|
||||
// has none). A build of the request index after an update reads every
|
||||
// session again and allocates ~1.2 GB to keep ~40 MB more, and the heap
|
||||
// overshot its goal into pages it then kept: 308–323 MB in Activity
|
||||
// Monitor for 15.7k sessions, where a start that reads the index peaked at
|
||||
// 247–255 MB. Under the limit, 241–251 MB and 211–228 MB, for 1–7 s more
|
||||
// CPU in that build.
|
||||
// It sets no memory limit. It held the memory Go asked of the system to
|
||||
// what it held when fn began, and a cold start then reached the limit
|
||||
// with the heap all live: from ~11 s to ~24 s it collected every ~160 ms,
|
||||
// a core's worth, 3:39 of CPU in its first 60 s for 390k calls and 1166
|
||||
// collections. Without it, 2:03–2:06 and 618, and the footprint's peak
|
||||
// the same, 835–863 MB against 855 (#2278).
|
||||
func underTightGC(fn func()) {
|
||||
defer tightenGC(true)()
|
||||
defer tightenGC()()
|
||||
fn()
|
||||
}
|
||||
|
||||
@@ -659,15 +656,12 @@ func underTightGC(fn func()) {
|
||||
// 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
|
||||
n int // reads under it
|
||||
was int // GOGC before the first
|
||||
}
|
||||
|
||||
// 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()) {
|
||||
// tightenGC begins a read under a tight GC; undo ends it.
|
||||
func tightenGC() (undo func()) {
|
||||
tightGC.Lock()
|
||||
defer tightGC.Unlock()
|
||||
if tightGC.n == 0 {
|
||||
@@ -678,37 +672,15 @@ func tightenGC(limit bool) (undo func()) {
|
||||
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 {
|
||||
s := []metrics.Sample{{Name: "/memory/classes/total:bytes"}, {Name: "/memory/classes/heap/released:bytes"}}
|
||||
metrics.Read(s)
|
||||
return int64(s[0].Value.Uint64() - s[1].Value.Uint64())
|
||||
}
|
||||
|
||||
const tightGCPercent = 25
|
||||
|
||||
// openTerminal runs a command in the chosen Mac terminal through a .command
|
||||
|
||||
@@ -34,35 +34,14 @@ func TestUnderTightGC(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// 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))
|
||||
// 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))
|
||||
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)
|
||||
}
|
||||
var during int64
|
||||
underTightGC(func() { during = debug.SetMemoryLimit(-1) })
|
||||
if during != math.MaxInt64 {
|
||||
t.Errorf("read under limit %d, want none", during)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,8 +52,8 @@ 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)
|
||||
first := tightenGC()
|
||||
second := tightenGC()
|
||||
if p := gcPercent(); p != tightGCPercent {
|
||||
t.Fatalf("two reads under GOGC=%d, want %d", p, tightGCPercent)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user