Files
magpie/internal/gui/page_memory.go
T
yetone 936bdccfab gui: the page reads hand memory back only after 30s at rest, past every poll (#2278)
An open Usage or Sessions window polls every 10s and 15s. With a 3s rest,
every poll's pages were handed back (ReleaseScratch + FreeOSMemory) and
taken back by the next poll: in the CPU profile, 1.88s of 1.95s madvise
was runtime.sysUsed re-committing them, and the matcher's scratch was
made again on every poll. 30s outlasts both polls, so the pages go back
once a window stops polling, not between an open window's polls.

Measured on mote (~/r2278 run2278d: 10 endpoints every 5s, gateway feed
from 60s, real ~/.codex and ~/.claude copy), 60s CPU profile, 3s vs 30s:
  GC mark workers  6.92s -> 4.76s
  madvise          1.81s -> 1.18s
  avg RSS after 60s  893 -> 925 MiB (+32 MiB while polling)
Latency was not comparable (mote load 40-73 during the run).
The binary measured was this constant at f193c3865. Rebased onto
91a2f603c (a masking commit and a .cjs GUI test only), and the vet and
test below were run again after it.

Run at the pushed head on mote: GOOS=linux/windows/darwin go vet -tags
nogui ./internal/gui/ ok. go test ./internal/gui/ red only in
TestMainWindowCorners/macOS_26_SDK, the open #3949.
Not tried: an open window left polling for an hour (RSS stays at the
poll's working set by design).
2026-10-29 07:58:26 +08:00

86 lines
2.6 KiB
Go

package gui
import (
"runtime/debug"
"sync"
"time"
"github.com/yetone/magpie/internal/usage"
)
// A Sessions or Usage page's poll reads every session file's listing and
// the request ledger again: ~330 MB allocated every 15 s to keep ~105 MB.
// Go keeps the pages that freed, and its scavenger hands them back only
// slowly, never while the next collection's goal covers them, so an open
// window sat at ~340 MB in Activity Monitor between polls. The gateway
// hands its requests' pages back once it rests (#3472), but the window's
// reads aren't its requests. So once those reads have rested for
// pageRest, what they left goes back at once, as the warm-up's does.
// pageRest is how long the page reads rest before that. It outlasts the
// pages' polls (Usage every 10 s, Sessions every 15 s), so the pages go
// back once a window stops polling, not between an open window's polls:
// at 3 s every poll took back the pages the last one handed back, and
// madvise and the collections it drove were ~40% of the CPU (#2278).
const pageRest = 30 * time.Second
var pageReads struct {
sync.Mutex
n int // reads in flight
last time.Time // when the latest ended
due *time.Timer // the release to come, or nil
rest time.Duration
// debug.FreeOSMemory, or a test's count
release func()
}
// readingPage counts a page read until done is called, and has what it
// left handed back once no read has been in flight for pageRest.
func readingPage() (done func()) {
pageReads.Lock()
pageReads.n++
pageReads.Unlock()
return sync.OnceFunc(func() {
pageReads.Lock()
defer pageReads.Unlock()
pageReads.last = time.Now()
if pageReads.n--; pageReads.n == 0 && pageReads.due == nil {
pageReads.due = time.AfterFunc(pageRestFor(), releaseIfPagesRest)
}
})
}
// pageRestFor is pageRest, or a test's. Called under pageReads' lock.
func pageRestFor() time.Duration {
if pageReads.rest > 0 {
return pageReads.rest
}
return pageRest
}
// releaseIfPagesRest hands memory back when no page read has been in
// flight for pageRest. A read in flight arms it again when it ends, and a
// rest not long enough yet waits out the rest of it.
func releaseIfPagesRest() {
pageReads.Lock()
pageReads.due = nil
if pageReads.n != 0 {
pageReads.Unlock()
return
}
if wait := pageRestFor() - time.Since(pageReads.last); wait > 0 {
pageReads.due = time.AfterFunc(wait, releaseIfPagesRest)
pageReads.Unlock()
return
}
release := pageReads.release
pageReads.Unlock()
// what the request matching keeps for a poll's next read goes too
usage.ReleaseScratch()
if release != nil {
release()
return
}
debug.FreeOSMemory()
}