gui: a tight GC lets a small heap grow by 32 MB, not a tenth (#2278)
The usage and sessions reads run under GOGC=10. On a first start they begin on a ~50 MB live heap and allocate ~400 MB a second, so a tenth was a collection every few MB: 2,102 to 2,239 collections in the first 75 s, about 70 s of the start's CPU, and each one emptied the zstd and bufio pools, which were then allocated again. While a read is under a tight GC, each collection now sets GOGC for the heap it left: tightPercent gives a tenth, or enough for 32 MB of growth, never above what was set before (a lower or off GOGC is kept). A heap over 320 MB is held to a tenth as before. The hook is a finalizer re-armed after each collection while a read is under it. Cold start on mote's copy of the real history (75 s, two runs each, v0.1.2330's lastIndex build vs this): - CPU 3:46.66 / 4:00.50 -> 3:08.69 / 3:10.04 (user 3:18.97 / 3:28.67 -> 2:44.23 / 2:45.85) - collections 2,239 / 2,102 -> 706 / 705 - phys_footprint_peak 318 / 312 -> 330 / 335 MB - heat, requests, overview 0/7/30 and stats 0/30 answers byte-identical A 16 MB room: 3:46.86 / 3:38.78 CPU, 1,554 / 1,390 collections, peak 331 / 323 MB, so it buys little. Tests (mote): go vet ./internal/gui; go test ./internal/gui -run 'TightGC|WarmUp|UnderTight|TightPercent|WarmCodex' ok. TestTightPercentLeavesRoom pins the bound; TestTightGCRetunesAfterACollection checks a collection retunes GOGC and the read's end restores it. Broken by hand: retightenGC not setting GOGC turns TestTightGCRetunesAfterACollection red. Not tried: the full gui package (TestCoveredWindowStaysUp is killed on mote, #4125); Windows and Linux runs.
This commit is contained in:
@@ -6,11 +6,13 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"maps"
|
||||
"math"
|
||||
"net/http"
|
||||
"os"
|
||||
"reflect"
|
||||
"runtime"
|
||||
"runtime/debug"
|
||||
"runtime/metrics"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -788,8 +790,9 @@ func underTightGC(fn func()) {
|
||||
// when the first to begin ended before the second.
|
||||
var tightGC struct {
|
||||
sync.Mutex
|
||||
n int // reads under it
|
||||
was int // GOGC before the first
|
||||
n int // reads under it
|
||||
was int // GOGC before the first
|
||||
hooked bool // a retightenGC is waiting for the next collection
|
||||
}
|
||||
|
||||
// tightenGC begins a read under a tight GC; undo ends it.
|
||||
@@ -802,6 +805,10 @@ func tightenGC() (undo func()) {
|
||||
debug.SetGCPercent(was)
|
||||
}
|
||||
tightGC.was = was
|
||||
if tightGCRoom > 0 && !tightGC.hooked {
|
||||
tightGC.hooked = true
|
||||
afterNextGC(retightenGC)
|
||||
}
|
||||
}
|
||||
tightGC.n++
|
||||
return sync.OnceFunc(func() {
|
||||
@@ -821,6 +828,58 @@ func tightenGC() (undo func()) {
|
||||
// ~2 s a minute of polls asking ten answers every 5 s (#2278).
|
||||
const tightGCPercent = 10
|
||||
|
||||
// tightGCRoom is the least a tight GC lets the heap grow by before a
|
||||
// collection. A tenth of a small heap is a few MB: a first start's reads
|
||||
// began on a ~50 MB heap and allocate ~400 MB a second, so it collected
|
||||
// ~28 times a second, 2,205 times in its first 78 s, and the collections
|
||||
// were 69–72 s of its ~4:10 of CPU (#2278). With this room a heap of
|
||||
// over 320 MB is held to a tenth still; one below grows by 32 MB. 0 in
|
||||
// tests, whose GOGC is read as tightGCPercent while a read is under it.
|
||||
var tightGCRoom = func() int64 {
|
||||
if testing.Testing() {
|
||||
return 0
|
||||
}
|
||||
return 32 << 20
|
||||
}()
|
||||
|
||||
// tightPercent is the GOGC a tight GC sets on a live heap of live bytes:
|
||||
// tightGCPercent, or enough for tightGCRoom, never above was, what was
|
||||
// set before (off stays off).
|
||||
func tightPercent(live int64, was int) int {
|
||||
p := tightGCPercent
|
||||
if live > 0 && tightGCRoom > 0 {
|
||||
p = max(p, int(min(tightGCRoom*100/live, math.MaxInt32)))
|
||||
}
|
||||
if was < p {
|
||||
return was
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
// retightenGC sets the GOGC for the heap the collection just left, while
|
||||
// a read is under a tight GC, and waits for the next one.
|
||||
func retightenGC() {
|
||||
tightGC.Lock()
|
||||
defer tightGC.Unlock()
|
||||
if tightGC.n == 0 {
|
||||
tightGC.hooked = false
|
||||
return
|
||||
}
|
||||
s := []metrics.Sample{{Name: "/gc/heap/live:bytes"}}
|
||||
metrics.Read(s)
|
||||
if s[0].Value.Kind() == metrics.KindUint64 {
|
||||
debug.SetGCPercent(tightPercent(int64(s[0].Value.Uint64()), tightGC.was))
|
||||
}
|
||||
afterNextGC(retightenGC)
|
||||
}
|
||||
|
||||
// afterNextGC runs fn on the finalizer goroutine once the next collection
|
||||
// has finished.
|
||||
func afterNextGC(fn func()) {
|
||||
type tick struct{ _ *byte } // a pointer keeps it off the tiny allocator
|
||||
runtime.SetFinalizer(&tick{}, func(*tick) { go fn() })
|
||||
}
|
||||
|
||||
// agentCommand is the shell line that starts a: its Launch where it has
|
||||
// one (agy, Claude Science, Cursor Private Inference), else its own
|
||||
// program, quoted; "" for one in a WSL distro or with no program here (a
|
||||
|
||||
@@ -2,8 +2,10 @@ package gui
|
||||
|
||||
import (
|
||||
"math"
|
||||
"runtime"
|
||||
"runtime/debug"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func gcPercent() int {
|
||||
@@ -86,3 +88,58 @@ func TestWarmUpTightensOnlyTheUsageRead(t *testing.T) {
|
||||
t.Fatalf("a warm-up step read under GOGC=%d, want 100", during)
|
||||
}
|
||||
}
|
||||
|
||||
// TestTightPercentLeavesRoom: a tight GC on a small heap lets it grow by
|
||||
// tightGCRoom, not a tenth, which on a first start collected ~28 times a
|
||||
// second (#2278); a large heap is held to a tenth, and a lower or off
|
||||
// GOGC is kept.
|
||||
func TestTightPercentLeavesRoom(t *testing.T) {
|
||||
room := tightGCRoom
|
||||
tightGCRoom = 32 << 20
|
||||
defer func() { tightGCRoom = room }()
|
||||
for _, c := range []struct {
|
||||
live int64
|
||||
was int
|
||||
want int
|
||||
}{
|
||||
{50 << 20, 100, 64},
|
||||
{8 << 20, 100, 100},
|
||||
{8 << 20, 300, 300},
|
||||
{320 << 20, 100, tightGCPercent},
|
||||
{2 << 30, 100, tightGCPercent},
|
||||
{50 << 20, 5, 5},
|
||||
{50 << 20, -1, -1},
|
||||
{0, 100, tightGCPercent},
|
||||
} {
|
||||
if got := tightPercent(c.live, c.was); got != c.want {
|
||||
t.Errorf("tightPercent(%d MB, %d) = %d, want %d", c.live>>20, c.was, got, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestTightGCRetunesAfterACollection: while a read is under a tight GC,
|
||||
// each collection sets GOGC for the heap it left, and once the read ends
|
||||
// GOGC is what it was before.
|
||||
func TestTightGCRetunesAfterACollection(t *testing.T) {
|
||||
room := tightGCRoom
|
||||
tightGCRoom = 1 << 40
|
||||
defer func() { tightGCRoom = room }()
|
||||
before := debug.SetGCPercent(100)
|
||||
defer debug.SetGCPercent(before)
|
||||
undo := tightenGC()
|
||||
if got := gcPercent(); got != tightGCPercent {
|
||||
t.Fatalf("GOGC before a collection = %d, want %d", got, tightGCPercent)
|
||||
}
|
||||
deadline := time.Now().Add(10 * time.Second)
|
||||
for gcPercent() != 100 {
|
||||
if time.Now().After(deadline) {
|
||||
t.Fatalf("GOGC after collections = %d, want 100 for a heap far under the room", gcPercent())
|
||||
}
|
||||
runtime.GC()
|
||||
time.Sleep(time.Millisecond)
|
||||
}
|
||||
undo()
|
||||
if got := gcPercent(); got != 100 {
|
||||
t.Fatalf("GOGC after the read = %d, want 100", got)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user