Vias kept the lists for one since. The Sessions list asks from its
oldest listed session's start, each agent's manage page from that
agent's oldest, and a session's detail from that session's start, so
an open page's polls almost never asked the kept since, and each ask
read and grouped the whole log again. Idle with an open Sessions page
on the 720k-call sandbox, buildVias allocated 9.1 GB in 8 minutes,
78% of everything allocated, which is most of why the idle process
churned its heap and grew.
The cache now keeps one index from the whole log and each session's
first call. A session whose calls all came at or after the since is
answered from it. A session with calls before its since is answered
from one kept window for that since, built only when such a session
is read. Len counts the sessions with a call since the index's time.
Both are kept per log version and agent list, as before.
TestViasAnswerEverySinceFromOneIndex writes 20,001 sessions and asks
from three sinces in turn, as the three pages do: 2,677 bytes an ask.
On the old cache: red, 11,580,100 bytes an ask. It also checks that a
session with a call on each side of a since counts only the one after
it, and what Len says for a window and for every session.
TestGatewaySessionsCachePricesWindowsAndRewrite's window check and
TestUsageViasSkipsOldHistory stay green.
720k-call sandbox, idle with an open page's polls every 15 s (the
Sessions list, stats, Claude's manage page, Requests, state), before
and after side by side for 12 minutes, load ~170-250. Allocated from
minute 4 to minute 12: buildVias 9,100 MB before, 0 after (built once,
before minute 4). The faster answers let the poll loop run about twice
as often, so the totals (11.6 GB and 10.5 GB) aren't per poll. The
live heap ran 352-641 MB before and 514-724 MB after, and RSS 730-962
and 555-1217 MB; both swing with where a poll is when the snapshot is
taken and the machine's load, and neither shows a trend in 12 minutes.
Run: go test -p 4 -count=1 ./internal/usage/ -run
'Vias|KeptStrings|GatewaySessionsCachePrices|Incremental|Review': ok.
Full suite on this series' final tree (aa678b482 before this note was
added), rebased on origin/main f76201a84: go test -p 4 ./... -timeout
45m, exit 0, 64 packages ok and none failed (gateway 1783 s, gui 361 s),
41 minutes at load ~190-200. gofmt -l: no files. go vet: darwin, linux
(-tags nogui) and windows exit 0. The GUI suite wasn't run: no file
under internal/gui/assets changed.
111 lines
3.2 KiB
Go
111 lines
3.2 KiB
Go
package usage
|
|
|
|
import (
|
|
"slices"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
// viaLists is what the gateway sent each session's calls to, and when each
|
|
// session's first call was: a list counted from any time at or before that
|
|
// first call is the same list.
|
|
type viaLists struct {
|
|
m map[string][]Via
|
|
first map[string]time.Time
|
|
}
|
|
|
|
// viasCache keeps one index of the whole log, which answers every since a
|
|
// session's calls all came after, and one window for a session that had
|
|
// calls before its since. The Sessions list, each agent's manage page and
|
|
// a session's detail each ask from a since of their own; one kept since
|
|
// was built again from the whole log at almost every ask, ~280 MB a time
|
|
// over ~720k calls, on every poll of an open page (#2776).
|
|
var viasCache struct {
|
|
sync.Mutex
|
|
version uint64
|
|
clients string // the clients counted as agents (#1958)
|
|
all *viaLists
|
|
window struct {
|
|
version uint64
|
|
clients string
|
|
since time.Time
|
|
m map[string][]Via
|
|
}
|
|
}
|
|
|
|
// ViaIndex is Vias' answer: the one copy every caller shares, handed out
|
|
// whole and read only through Of, which gives each session's list as a
|
|
// copy of its own. Each ask cloned every session's list, ~66 MB a time
|
|
// over ~720k calls, where a page reads the few sessions it lists (#2776).
|
|
type ViaIndex struct {
|
|
all *viaLists
|
|
since time.Time
|
|
snapshot *logSnapshot
|
|
clients string
|
|
}
|
|
|
|
// Of is what the gateway sent one session's calls to ("agent|id") since
|
|
// the index's time, the most calls first; nil for a session it sent none
|
|
// of.
|
|
func (x ViaIndex) Of(key string) []Via {
|
|
vs, ok := x.all.m[key]
|
|
if !ok {
|
|
return nil
|
|
}
|
|
if x.since.IsZero() || !x.all.first[key].Before(x.since) {
|
|
return slices.Clone(vs)
|
|
}
|
|
return slices.Clone(x.window()[key])
|
|
}
|
|
|
|
// Len is how many sessions have a list.
|
|
func (x ViaIndex) Len() int {
|
|
if x.since.IsZero() {
|
|
return len(x.all.m)
|
|
}
|
|
n := 0
|
|
for _, vs := range x.all.m {
|
|
if slices.ContainsFunc(vs, func(v Via) bool { return !v.Last.Before(x.since) }) {
|
|
n++
|
|
}
|
|
}
|
|
return n
|
|
}
|
|
|
|
// window is the lists counted from the index's since, for a session that
|
|
// had calls before it.
|
|
func (x ViaIndex) window() map[string][]Via {
|
|
viasCache.Lock()
|
|
defer viasCache.Unlock()
|
|
w := &viasCache.window
|
|
if w.m != nil && w.version == x.snapshot.version && w.clients == x.clients && w.since.Equal(x.since) {
|
|
return w.m
|
|
}
|
|
m := buildVias(x.snapshot, x.since).m
|
|
if x.snapshot.complete() {
|
|
w.version, w.clients, w.since, w.m = x.snapshot.version, x.clients, x.since, m
|
|
}
|
|
return m
|
|
}
|
|
|
|
func cachedVias(since time.Time) ViaIndex {
|
|
snapshot := readLogSnapshot()
|
|
clients := clientAgentsKey()
|
|
viasCache.Lock()
|
|
defer viasCache.Unlock()
|
|
all := viasCache.all
|
|
if all == nil || viasCache.version != snapshot.version || viasCache.clients != clients {
|
|
all = buildVias(snapshot, time.Time{})
|
|
if snapshot.complete() {
|
|
viasCache.version, viasCache.clients, viasCache.all = snapshot.version, clients, all
|
|
}
|
|
}
|
|
return ViaIndex{all: all, since: since, snapshot: snapshot, clients: clients}
|
|
}
|
|
|
|
// complete is whether the snapshot reached the end of the log, so what is
|
|
// built from it can be kept for its version.
|
|
func (s *logSnapshot) complete() bool {
|
|
return s.info != nil && s.off == s.info.Size()
|
|
}
|