diff --git a/internal/usage/request_page.go b/internal/usage/request_page.go index 4a8bfdecf..2c3455dc0 100644 --- a/internal/usage/request_page.go +++ b/internal/usage/request_page.go @@ -964,14 +964,15 @@ func readRequestBlocks(idx *requestIndex, st requestState, p Period, now time.Ti c.serial = nextChunkSerial() built++ builtRows += c.Count + c.Used = idx.tick put = spill.put(c, i) + } else { + c = idx.use(c) } - kept, next := *c, *c - kept.Used, next.Used = idx.tick, idx.tick - idx.chunks[s.Path] = &kept - chunks = append(chunks, &next) + idx.chunks[s.Path] = c + chunks = append(chunks, c) if put { - spill.hand(&kept, &next) + spill.hand(c) } } spill.done(func(i int) *rowChunk { return makeChunk()(sources[i]) }) @@ -1001,11 +1002,12 @@ func readRequestBlocks(idx *requestIndex, st requestState, p Period, now time.Ti c.dict, c.shapeIDs = nil, nil c = c.pack() c.serial = nextChunkSerial() + c.Used = idx.tick + } else { + c = idx.use(c) } - kept, next := *c, *c - kept.Used, next.Used = idx.tick, idx.tick - idx.chunks[s.path] = &kept - others = append(others, &next) + idx.chunks[s.path] = c + others = append(others, c) } for path := range idx.chunks { if !on[path] { @@ -1046,6 +1048,20 @@ func (idx *requestIndex) adopt(st requestState) { } } +// use is c, a chunk of idx's, as used by this read. A chunk another read +// published is copied, since that read's page may still hold it; one this +// read made (fromDisk) is its own. Both the map and the page take the one +// chunk: a copy for each was 13.5 MB of the live heap after 34k sessions' +// start (#2278). +func (idx *requestIndex) use(c *rowChunk) *rowChunk { + if c.Used == idx.tick { + return c + } + next := *c + next.Used = idx.tick + return &next +} + // private is a copy of idx's metadata and chunk map to read into without // its lock: published chunks are immutable, and reading, decoding, pricing // and aggregation use the copy. It counts a use of idx (tick). idx is diff --git a/internal/usage/request_page_test.go b/internal/usage/request_page_test.go index 33fde1011..db7eb6553 100644 --- a/internal/usage/request_page_test.go +++ b/internal/usage/request_page_test.go @@ -329,3 +329,36 @@ func TestVisibleLocalLeavesOutRepeatedMessages(t *testing.T) { t.Fatalf("left out %v, want b's rows 0 and 3 only", got) } } + +// A read keeps one chunk a source, the map's and the page's both: a copy +// for each was 13.5 MB after 34k sessions' start (#2278). A chunk an +// earlier read published is copied once, never changed under its page. +func TestRequestBlocksKeepOneChunkASource(t *testing.T) { + pageHome(t) + now := holdClock(t, time.Date(2026, 9, 30, 12, 0, 0, 0, time.Local)) + path := filepath.Join(sessions.CodexDir(), "sessions", "rollout-2026-09-30T00-00-00-one.jsonl") + os.MkdirAll(filepath.Dir(path), 0700) + os.WriteFile(path, []byte(`{"type":"session_meta","payload":{"id":"one","model_provider":"custom"}}`+"\n"+`{"type":"turn_context","payload":{"model":"m"}}`+"\n"+ + fmt.Sprintf(`{"timestamp":%q,"type":"event_msg","payload":{"type":"token_count","info":{"total_token_usage":{"input_tokens":10,"output_tokens":1},"last_token_usage":{"input_tokens":10,"output_tokens":1}}}}`+"\n", now.Format(time.RFC3339Nano))), 0600) + st := readRequestState(now) + shared := &requestIndex{} + shared.adopt(st) + read := func() (*rowChunk, *requestIndex) { + idx := shared.private() + b := readRequestBlocks(idx, st, All, now, sessions.ReadCallSource) + if len(b.chunks) != 1 { + t.Fatalf("%d chunks, want 1", len(b.chunks)) + } + return b.chunks[0], idx + } + built, idx := read() + if idx.chunks[path] != built || built.Used != idx.tick { + t.Fatalf("a built chunk is copied: map %p, page %p", idx.chunks[path], built) + } + shared.keep(idx, map[string]bool{path: true}) + used := built.Used + again, idx := read() + if again == built || idx.chunks[path] != again || again.Used != idx.tick || built.Used != used { + t.Fatalf("a published chunk read again: %p (was %p), map %p, used %d (was %d)", again, built, idx.chunks[path], built.Used, used) + } +}