An unpacked block's strings are cut from one allocation (unpackInto). The gateway-sessions projection (gatewayCache) and the vias cache kept a session id, model, agent, provider or effort from nearly every block, and each such substring held the block's whole decoded text: request and response ids and errors included. keptStrings clones each kept value once and interns it, so a cache holds only what it uses. TestSessionCachesHoldNoDecodedBlocks writes 16 blocks x 512 rows with a 1 KB error each, reads GatewaySessions and Vias, drops the snapshot and compares the live heap with and without the caches. Without the fix (both call sites reverted) it is red: "the caches hold 16651 KB for 16 sessions: the decoded text of their blocks (8192 KB)". With it, green -count=3. Measured (macOS, sandbox HOME seeded with 600k gateway calls, the fixed build vsccb2eea15, the same Usage/Sessions/Routing views opened by Playwright, after GC via /v1/magpie/heap; footprint from `footprint`): - after the views: live heap 618 MB -> 466 MB, footprint 801 -> 670 MB - after warm-up only: live heap 467 -> 390 MB - CPU over the same 9m40s: ~22.4 s both Go is the process that holds memory: on a live v0.1.1208 app the Go process's footprint was 1045 MB, the Bun plugin host 51 MB, the two WebKit WebContent processes 70 and 83 MB. No single commit in v0.1.1225..HEAD is the cause: the shared-slab decode (99fcddeeb) and both caches (c9b826337,60f896031) predate v0.1.1225. Idle with no page open, HEAD used 1.04 s CPU per 5 min against v0.1.1225's 0.56 s. Not fixed here: the gateway projection is kept once per window (up to 4), the startup heap peaks ~1.5 GB from GOGC doubling the warm-up's live heap, and a synthetic 11k-session history did not reproduce the 1 GB reported. Run at this change onccb2eea15(load average ~275-500 from other sessions): - go test ./... -timeout 45m, sandbox HOME: 61 packages ok; internal/agent timed out at 45m; 4 reds in gateway (TestClaudeAccountsSpentFallBackToCodex, TestCodexServedNamesTheAccountThatSigned) and gui (TestPluginPageReadsWhatItDeclared, TestSetAnswersWhileDevinAndProvidersHang). - Those 4 alone: gateway ok 40.8s, gui ok 13.7s. - internal/agent alone (-timeout 45m): timed out again at 2732s under load ~500; the stacks left include a netproxy.Bridge accept loop. internal/agent never calls GatewaySessions or Vias, the only code changed. Not filed from here: the run kept only its last 25 lines, so the test that hung is not named. - internal/usage: ok 123s. - gofmt -l: clean. go vet ./internal/usage/ for darwin, linux (nogui) and windows: clean.
77 lines
2.2 KiB
Go
77 lines
2.2 KiB
Go
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)
|
|
}
|
|
}
|