Every poll while an agent works matches a year's history again: matchedBlocks and codexChunkResponsePairs grew their maps (byID, reserved, used, groupOf, picks, picked, ...) and slices from nothing on each call. They were ~3.0 GB of the ~40 GB a poll-heavy 80 s allocated on the real-history harness (d1951 heap diff, 120 s to 200 s). A sync.Pool was tried first and kept nothing in real use: under tightenGC a poll collects 4-5 times a second (1029 GCs in 230 s), each collection empties the pool, and the matcher's flat allocation went only from 2241 to 1840 MB. So one scratch of each is kept in an atomic.Pointer, emptied after a call (clear() keeps the maps' buckets, and drops every chunk the call saw), and let go by usage.ReleaseScratch, which releaseIfPagesRest calls before FreeOSMemory: once the window's reads have rested 3 s, what the matching kept goes back with the rest. A call that finds the scratch taken makes its own, sized as before. matchedBlocks' result map is now made once the id matches are known, not grown from the response pairs alone. Measured on mote (Apple M5 Pro), ataeacc6358+ this change: - BenchmarkPairRows (2000 files x 100 calls), -count 3: 100.8 MB/op, 7,056 allocs/op at aeacc6358; 48.4 MB/op, 5,458 allocs/op after. - Real history (copy of the user's usage.jsonl, sandbox HOME, polls + appended calls, run2278d.sh 230 s), heap alloc 120 s to 200 s: matchedBlocks flat 2241 -> 1295 MB, codexChunkResponsePairs flat 806 -> 143 MB, of a total about the same (39.8 vs 39.3 GB). That run was before the result-map change. Its CPU and peak RSS (974 MiB vs 936) aren't comparable: mote's load went from ~25 to 50-88 from other sessions during it. Tests: - New TestPairScratchKeepsNothingBetweenCalls (pairRows on one history after another matches what a first call does; ReleaseScratch lets go) and TestMatchScratchForgetsTheLastCall (a gateway call one call used by id or reserved by a response pair is matched in the next; ids the last call looked up in another gateway aren't). - Broken by hand, each red: without clear of byID (index out of range), used ("the last call's id match held the gateway's call"), reserved ("the last call's response pair held"), groupOf and picks (index out of range). ids, named and picked only choose which chunks are opened, so a stale entry there costs a read, not a match. - go test -count=1 -timeout 45m ./internal/usage/ ./internal/usageapi/ ./internal/sessions/ ./internal/gui/ ataeacc6358+ this change: usage, usageapi, sessions ok; gui red in TestMainWindowCorners/macOS_26_SDK alone, mote's macOS 27 (#3949). An earlier gui run there had ten AppKit window tests killed at their 20-40 s deadlines at load ~40-88 (#2645). - GOOS=linux, windows, darwin go vet -tags nogui ./internal/usage/ ./internal/gui/ ./internal/usageapi/: ok. Rebased onto origin/mainfe05f0a72(library and Claude-account changes, nothing in internal/usage or page_memory.go); after the rebase ./internal/usage/ and ./internal/usageapi/ ran again at the pushed tree: ok, and go build . ok.
83 lines
2.4 KiB
Go
83 lines
2.4 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: 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()
|
|
// what the request matching keeps for a poll's next read goes too
|
|
usage.ReleaseScratch()
|
|
if release != nil {
|
|
release()
|
|
return
|
|
}
|
|
debug.FreeOSMemory()
|
|
}
|