Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

usage: the Sessions and Routing caches copy out the strings they keep, so they no longer pin every decoded block

yetonedb61143fe7e3
diff --git a/internal/usage/gateway_sessions.go b/internal/usage/gateway_sessions.go
index dd5cf04e4..888afc7ab 100644
--- a/internal/usage/gateway_sessions.go
+++ b/internal/usage/gateway_sessions.go
@@ -178,11 +178,13 @@ func buildGatewaySessions(snapshot *logSnapshot, since time.Time, nativeKeys map
type dayKey struct{ date, agent, model string }
daily := map[dayKey]*GatewayDayUsage{}
perSession := map[key]map[dayKey]*GatewayDayUsage{}
+ keep := keptStrings{}
snapshot.visit(since, func(r Record) {
if r.IsRejected() || r.IsReported() || r.Session == "" {
return
}
- a := ReportedAs(r.Agent)
+ a := keep.of(ReportedAs(r.Agent))
+ r.Session, r.Model = keep.of(r.Session), keep.of(r.Model)
k := key{a, r.Session}
if nativeKeys != nil && nativeKeys[a+"|"+r.Session] {
return
diff --git a/internal/usage/kept_strings_test.go b/internal/usage/kept_strings_test.go
new file mode 100644
index 000000000..0e03230de
--- /dev/null
+++ b/internal/usage/kept_strings_test.go
@@ -0,0 +1,76 @@
+package usage
+
+import (
+ "bufio"
+ "crypto/rand"
+ "encoding/hex"
+ "encoding/json"
+ "os"
+ "path/filepath"
+ "runtime"
+ "testing"
+ "time"
+)
+
+// An unpacked block's strings share one allocation, so a cache that keeps a
+// session id or a model name read from a block must copy it out, or it keeps
+// every string of that block: over a long history, the decoded text of all
+// of it. This test fills each block with 1 KB of unique error text and gives
+// each block its own session, then measures what the Sessions and Routing
+// caches hold once the log index itself is let go.
+func TestSessionCachesHoldNoDecodedBlocks(t *testing.T) {
+ resetUsageTest(t)
+ const blocks, text = 16, 1024
+ path := Path()
+ if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
+ t.Fatal(err)
+ }
+ f, err := os.Create(path)
+ if err != nil {
+ t.Fatal(err)
+ }
+ w := bufio.NewWriter(f)
+ at := time.Now().Add(-time.Hour)
+ noise := make([]byte, text/2)
+ for b := range blocks {
+ for range logBlockRows {
+ rand.Read(noise)
+ line, _ := json.Marshal(Record{Time: at, Agent: "codex", Session: "s" + string(rune('a'+b)), Provider: "p", Model: "m", Effort: "high", Input: 10, Output: 2, Status: 200, Error: hex.EncodeToString(noise)})
+ w.Write(append(line, '\n'))
+ }
+ }
+ if err := w.Flush(); err != nil {
+ t.Fatal(err)
+ }
+ f.Close()
+
+ held := func() uint64 {
+ var m runtime.MemStats
+ runtime.GC()
+ runtime.GC()
+ runtime.ReadMemStats(&m)
+ return m.HeapAlloc
+ }
+ if got := GatewaySessions(time.Time{}, nil); len(got) != blocks {
+ t.Fatalf("sessions: %d, want %d", len(got), blocks)
+ }
+ if got := Vias(time.Time{}); len(got) != blocks {
+ t.Fatalf("vias: %d, want %d", len(got), blocks)
+ }
+ // the index is kept by design; only what the caches hold is measured
+ logIndex.Lock()
+ logIndex.snapshot = nil
+ logIndex.Unlock()
+ withCaches := held()
+ gatewayCache.Lock()
+ gatewayCache.entries = nil
+ gatewayCache.Unlock()
+ viasCache.Lock()
+ viasCache.value = nil
+ viasCache.Unlock()
+ without := held()
+ decoded := uint64(blocks * logBlockRows * text)
+ if withCaches > without && withCaches-without > decoded/8 {
+ t.Fatalf("the caches hold %d KB for %d sessions: the decoded text of their blocks (%d KB)", (withCaches-without)>>10, blocks, decoded>>10)
+ }
+}
diff --git a/internal/usage/request_snapshot.go b/internal/usage/request_snapshot.go
index 29ab2430c..c73300b31 100644
--- a/internal/usage/request_snapshot.go
+++ b/internal/usage/request_snapshot.go
@@ -12,6 +12,7 @@ import (
"encoding/binary"
"math"
"slices"
+ "strings"
"sync"
"time"
"unsafe"
@@ -79,6 +80,27 @@ func (c *rowChunk) pack() *rowChunk {
func (c *rowChunk) unpack() *rowChunk { return c.unpackInto(nil) }
+// keptStrings copies out the strings that a result kept after a read takes
+// from the rows it visited. An unpacked block's strings are cut from one
+// allocation (unpackInto), so a cache holding one session id from a block
+// holds every string of that block: its request and response ids and its
+// errors. The Sessions and Routing caches kept a session id from nearly
+// every block, and so the decoded text of the whole history: ~200 MB at
+// rest for 600k calls.
+type keptStrings map[string]string
+
+func (k keptStrings) of(s string) string {
+ if s == "" {
+ return ""
+ }
+ if kept, ok := k[s]; ok {
+ return kept
+ }
+ kept := strings.Clone(s)
+ k[kept] = kept
+ return kept
+}
+
// unpackInto is unpack decoding the rows into rows when it is large enough:
// every field of a row is written, so a reused slice needs no clearing.
func (c *rowChunk) unpackInto(into []packedRow) *rowChunk {
diff --git a/internal/usage/usage.go b/internal/usage/usage.go
index 7bfc5b609..684267961 100644
--- a/internal/usage/usage.go
+++ b/internal/usage/usage.go
@@ -1005,10 +1005,12 @@ func Vias(since time.Time) map[string][]Via {
func buildVias(snapshot *logSnapshot, since time.Time) map[string][]Via {
out := map[string][]Via{}
+ keep := keptStrings{}
snapshot.visit(since, func(r Record) {
if r.IsRejected() || r.IsReported() || r.Session == "" || r.Model == "" {
return
}
+ r.Provider, r.Model, r.Effort = keep.of(r.Provider), keep.of(r.Model), keep.of(r.Effort)
k := ReportedAs(r.Agent) + "|" + r.Session
vs := out[k]
i := slices.IndexFunc(vs, func(v Via) bool { return v.Provider == r.Provider && v.Model == r.Model && v.Effort == r.Effort })