usage: a source's chunk is kept once, the index's and the page's both (#2278)
readRequestBlocks copied every source's chunk twice, one for the index and one for the page, on every read: 13.5 MB of the 176 MB live heap after 34k sessions' cold start (synthetic history at #2278's scale on mote, 240k gateway records in usage.jsonl). Nothing writes a published chunk in place (pairRows and pairMemo.apply clone, keep copies, the spill gives an archive back before the read returns), so both now take one chunk. A chunk this read built or read from disk is its own and is not copied at all; one an earlier read published is copied once, for its Used, since that read's page may still hold it. The other computers' days are kept the same way. TestRequestBlocksKeepOneChunkASource: a built chunk is the map's and the page's one pointer, with this read's tick; read again after keep, it is one new copy and the published one is unchanged. Broken by hand (the index given its own copy again) it failed with "a built chunk is copied". Run at this change on0dc4cec85(mote): go vet -tags nogui of internal/usage and internal/sessions for linux, windows and darwin ok; go test -tags nogui ./internal/usage ./internal/sessions ok. Rebased onto60561129f(GUI and codexcat changes), then go test -tags nogui ./internal/usage ./internal/sessions ./internal/codexcat ok again at the pushed tree. Not measured apart: the cold-start footprint run on mote ran beside a full suite (load ~40) and isn't comparable; the heap line above is from the profile before this change.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user