diff --git a/internal/gui/sessions.go b/internal/gui/sessions.go index bfbb09992..30d66cd35 100644 --- a/internal/gui/sessions.go +++ b/internal/gui/sessions.go @@ -666,6 +666,7 @@ func warmSessions() { var warmOnce sync.Once func warmStart() { + steadyGCStart() go func() { // a fresh install reads no agent's sessions until asked; one // that read them before keeps reading them (decided once) @@ -795,6 +796,7 @@ var tightGC struct { n int // reads under it was int // GOGC before the first hooked bool // a retightenGC is waiting for the next collection + steady bool // steadyGCStart has run: retightenGC sets steadyPercent between reads } // tightenGC begins a read under a tight GC; undo ends it. @@ -863,18 +865,64 @@ func tightPercent(live int64, was int) int { func retightenGC() { tightGC.Lock() defer tightGC.Unlock() - if tightGC.n == 0 { + if tightGC.n == 0 && !tightGC.steady { 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)) + live := int64(s[0].Value.Uint64()) + if tightGC.n > 0 { + debug.SetGCPercent(tightPercent(live, tightGC.was)) + } else { + debug.SetGCPercent(steadyPercent(live)) + } } afterNextGC(retightenGC) } +// steadyGCStart has the heap grow by half of what it holds before a +// collection, or by steadyGCRoom when that is more, not Go's default of +// all of it, for the life of the process; a GOGC the user set is kept. +// A window resting on ~60 MB live polls /api/state, /api/drift and +// /api/providers, ~10 MB thrown away a round, so at the default its +// footprint peaked at 138 MB; under half, 107-108 MB, for ~9% more CPU in +// the polls. Reads under a tight GC still set their tenth while they run. +func steadyGCStart() { + if os.Getenv("GOGC") != "" { + return + } + tightGC.Lock() + defer tightGC.Unlock() + tightGC.steady = true + if tightGC.n == 0 { + debug.SetGCPercent(steadyPercent(0)) + } + if !tightGC.hooked { + tightGC.hooked = true + afterNextGC(retightenGC) + } +} + +// steadyGCPercent is the half steadyGCStart lets the heap grow by. +const steadyGCPercent = 50 + +// steadyGCRoom is the least it grows by: half of a small heap is a few +// MB, and a heap under 64 MB grows by 32 MB, never by more than Go's +// default. +const steadyGCRoom = 32 << 20 + +// steadyPercent is the GOGC steadyGCStart sets on a live heap of live +// bytes (0 before the first collection: steadyGCPercent). +func steadyPercent(live int64) int { + p := steadyGCPercent + if live > 0 { + p = max(p, int(min(steadyGCRoom*100/live, 100))) + } + return p +} + // afterNextGC runs fn on the finalizer goroutine once the next collection // has finished. func afterNextGC(fn func()) { diff --git a/internal/gui/warm_gc_test.go b/internal/gui/warm_gc_test.go index 75f5b4184..082d58362 100644 --- a/internal/gui/warm_gc_test.go +++ b/internal/gui/warm_gc_test.go @@ -143,3 +143,90 @@ func TestTightGCRetunesAfterACollection(t *testing.T) { t.Fatalf("GOGC after the read = %d, want 100", got) } } + +// TestSteadyPercent: between reads the heap grows by half of what it +// holds, by 32 MB when that is more, and never by more than Go's default. +func TestSteadyPercent(t *testing.T) { + for _, c := range []struct { + live int64 + want int + }{ + {0, 50}, + {60 << 20, 53}, + {64 << 20, 50}, + {16 << 20, 100}, + {32 << 20, 100}, + {500 << 20, 50}, + } { + if got := steadyPercent(c.live); got != c.want { + t.Errorf("steadyPercent(%d MB) = %d, want %d", c.live>>20, got, c.want) + } + } +} + +// steadyOff puts back what steadyGCStart set, for the tests after. +func steadyOff(t *testing.T) { + before := gcPercent() + t.Cleanup(func() { + tightGC.Lock() + tightGC.steady = false + tightGC.Unlock() + debug.SetGCPercent(before) + }) +} + +// TestSteadyGCKeepsTheUsersGOGC: a GOGC the user set is what the process +// runs under. +func TestSteadyGCKeepsTheUsersGOGC(t *testing.T) { + steadyOff(t) + t.Setenv("GOGC", "200") + debug.SetGCPercent(200) + steadyGCStart() + if got := gcPercent(); got != 200 { + t.Fatalf("GOGC=200 set by the user, magpie runs under %d", got) + } + tightGC.Lock() + steady := tightGC.steady + tightGC.Unlock() + if steady { + t.Fatal("a GOGC the user set is retuned after each collection") + } +} + +// TestSteadyGCRetunesAfterACollection: from the start GOGC is half, each +// collection sets it for the heap it left (a test's heap is under the +// room: Go's 100), a read under a tight GC still sets its tenth, and after +// the read the next collection sets it for the heap again. +func TestSteadyGCRetunesAfterACollection(t *testing.T) { + steadyOff(t) + t.Setenv("GOGC", "") + debug.SetGCPercent(100) + steadyGCStart() + if got := gcPercent(); got != steadyGCPercent { + t.Fatalf("GOGC before a collection = %d, want %d", got, steadyGCPercent) + } + collectUntil := func(want int) { + t.Helper() + deadline := time.Now().Add(10 * time.Second) + for gcPercent() != want { + if time.Now().After(deadline) { + t.Fatalf("GOGC after collections = %d, want %d", gcPercent(), want) + } + runtime.GC() + time.Sleep(time.Millisecond) + } + } + collectUntil(100) + debug.SetGCPercent(steadyGCPercent) + undo := tightenGC() + if got := gcPercent(); got != tightGCPercent { + t.Fatalf("a read under a tight GC runs under %d, want %d", got, tightGCPercent) + } + runtime.GC() + runtime.GC() + if got := gcPercent(); got != tightGCPercent { + t.Fatalf("a read under a tight GC runs under %d after collections, want %d", got, tightGCPercent) + } + undo() + collectUntil(100) +}