Files
magpie/internal/gui/page_memory.go
T
yetone 94a70f1a0d gui: what a Sessions or Usage poll threw away goes back once the page rests
An open window's Sessions and Usage polls allocate ~330 MB every 15 s to
keep ~105 MB, and Go's scavenger hands the freed pages back only slowly,
never while the next collection's goal covers them: between polls the
footprint sat at 336-392 MB. The gateway already hands its requests'
pages back once it rests (#3472), but the window's reads go through the
GUI's handler, not the gateway's, so nothing ever did that for them.

held now counts the /api/sessions and /api/usage reads (readingPage),
and once none has been in flight for 3 s (a poll's reads come within a
second of one another) it calls debug.FreeOSMemory, as the warm-up does
after its reads.

Measured on a copy of the owner's real history (rss-home, 3581/3582,
8 GUI-like poll cycles 15 s apart, footprint 10 s into each rest), at
c2c08850f vs this change:
- footprint at rest: 336-392 MB -> 164-193 MB
- footprint right after a poll: 336-392 MB -> 181-276 MB (358 on the
  first)
- process CPU over the 8 cycles: 6.55 s -> 6.27 s
- /api/sessions mean 256 -> 305 ms, /api/usage/requests 232 -> 154 ms
  (one run each, the live history changing under them)

Tests: TestPageReadsReleasedAtRest (no release while a read is in
flight, one when the last ends, a cut-short rest waited out again) and
TestHeldCountsPageReads (sessions and usage counted, /api/state not).
Broken by hand: without the in-flight check the first fails ("released
while a read was in flight"); without readingPage in held the second
fails for both paths.

Run at this head (rebased on 8fba65d0b): go test ./internal/gui/ ok (94s); the two new tests
-count=20 ok; gofmt clean; GOOS=linux and GOOS=windows go vet -tags
nogui ./internal/gui/ ok.
Not run: the GUI JS suite (no script changed), the full ./... suite.
2026-10-27 08:29:11 +08:00

79 lines
2.2 KiB
Go

package gui
import (
"runtime/debug"
"sync"
"time"
)
// 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: a poll's few
// reads come within a second of one another.
const pageRest = 3 * 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()
if release != nil {
release()
return
}
debug.FreeOSMemory()
}