diff --git a/internal/usage/request_page.go b/internal/usage/request_page.go index da1007be8..2d740016e 100644 --- a/internal/usage/request_page.go +++ b/internal/usage/request_page.go @@ -1394,34 +1394,103 @@ func visibleLocal(chunks []*rowChunk) refSet { // made to size, as many as the rows that can have an id (a Codex call // has none): grown an id at a time, it was ~250 MB of a minute and a // third of polls (#2278) + keys := msgKeysOf(chunks) n := 0 - for _, c := range chunks { - if c.Source.Agent != "codex" { - n += c.Count - } + for _, k := range keys { + n += len(k.keys) } seen := make(map[[2]uint64]struct{}, n) - rows := lentRows() - defer rows.release() - for _, c := range chunks { - open := rows.of(c) - for i := range open.Rows { - p := &open.Rows[i] - msg := open.Strings[open.text(p, rowMsg)] - if msg == "" { - continue - } - k := [2]uint64{maphash.String(msgSeeds[0], msg), maphash.String(msgSeeds[1], msg)} - if _, ok := seen[k]; ok { - duplicates.add(rowRef{c, i}) + for i, c := range chunks { + for _, k := range keys[i].keys { + if _, ok := seen[k.k]; ok { + duplicates.add(rowRef{c, int(k.at)}) } else { - seen[k] = struct{}{} + seen[k.k] = struct{}{} } } } return duplicates } +// msgKeys are a chunk's message ids as visibleLocal hashes them, each +// with its row, kept by the chunk's serial as matchHints are: a pairing +// decoded every session file of the period to read them, ~840 MB of the +// 1.1 GB visibleLocal allocated in 90s while an agent worked (#2278). +var msgKeys struct { + sync.Mutex + by map[uint64]*chunkMsgKeys + turn uint64 +} + +type chunkMsgKeys struct { + keys []msgKey + turn uint64 +} + +type msgKey struct { + k [2]uint64 + at int32 +} + +// msgKeysOpening, in tests, is told each chunk msgKeysOf decodes. +var msgKeysOpening func(*rowChunk) + +// msgKeysOf are chunks' message keys, read from a chunk only when none +// are kept for its serial. +func msgKeysOf(chunks []*rowChunk) []*chunkMsgKeys { + msgKeys.Lock() + msgKeys.turn++ + turn := msgKeys.turn + out := make([]*chunkMsgKeys, len(chunks)) + for i, c := range chunks { + if k := msgKeys.by[c.serial]; c.serial != 0 && k != nil { + k.turn = turn + out[i] = k + } + } + msgKeys.Unlock() + rows := lentRows() + defer rows.release() + made := map[uint64]*chunkMsgKeys{} + for i, c := range chunks { + if out[i] != nil { + continue + } + if msgKeysOpening != nil { + msgKeysOpening(c) + } + k := &chunkMsgKeys{turn: turn} + open := rows.of(c) + for j := range open.Rows { + p := &open.Rows[j] + msg := open.Strings[open.text(p, rowMsg)] + if msg == "" { + continue + } + k.keys = append(k.keys, msgKey{[2]uint64{maphash.String(msgSeeds[0], msg), maphash.String(msgSeeds[1], msg)}, int32(j)}) + } + k.keys = slices.Clip(k.keys) + out[i] = k + if c.serial != 0 { + made[c.serial] = k + } + } + msgKeys.Lock() + if msgKeys.by == nil { + msgKeys.by = map[uint64]*chunkMsgKeys{} + } + for serial, k := range made { + msgKeys.by[serial] = k + } + for serial, k := range msgKeys.by { + if k.turn+hintsKept < msgKeys.turn { + delete(msgKeys.by, serial) + } + } + msgKeys.Unlock() + return out +} + var msgSeeds = [2]maphash.Seed{maphash.MakeSeed(), maphash.MakeSeed()} // idCalls are the gateway's calls of one request id, in the gateway's diff --git a/internal/usage/request_page_test.go b/internal/usage/request_page_test.go index 35ae9562f..32747be0c 100644 --- a/internal/usage/request_page_test.go +++ b/internal/usage/request_page_test.go @@ -3,6 +3,7 @@ package usage import ( "encoding/json" "fmt" + "maps" "math" "os" "path/filepath" @@ -330,6 +331,41 @@ func TestVisibleLocalLeavesOutRepeatedMessages(t *testing.T) { } } +// A chunk's message ids are read once while its serial stands: a pairing +// decoded every session file of the period for them on every poll +// (#2278), and what is left out is the same either way. +func TestVisibleLocalKeptBySerial(t *testing.T) { + a, b, c := &rowChunk{}, &rowChunk{}, &rowChunk{} + a.add(Row{}, "msg_1", 0, false) + a.add(Row{}, "msg_2", 1, false) + b.add(Row{}, "msg_2", 0, false) + b.add(Row{}, "msg_3", 1, false) + c.add(Row{}, "msg_3", 0, false) + c.add(Row{}, "msg_1", 1, false) + a, b, c = a.pack(), b.pack(), c.pack() + a.serial, b.serial = nextChunkSerial(), nextChunkSerial() + opened := 0 + msgKeysOpening = func(*rowChunk) { opened++ } + t.Cleanup(func() { msgKeysOpening = nil }) + want := map[rowRef]bool{{b, 0}: true, {c, 0}: true, {c, 1}: true} + for i, open := range []int{3, 1, 1} { + opened = 0 + got := refMap(visibleLocal([]*rowChunk{a, b, c})) + if !maps.Equal(got, want) { + t.Fatalf("call %d left out %v, want %v", i, got, want) + } + if opened != open { + t.Fatalf("call %d decoded %d chunks, want %d (c has no serial)", i, opened, open) + } + } + // in another order, the kept keys are read in that order + opened = 0 + got := refMap(visibleLocal([]*rowChunk{c, b, a})) + if want := map[rowRef]bool{{b, 1}: true, {a, 0}: true, {a, 1}: true}; !maps.Equal(got, want) || opened != 1 { + t.Fatalf("reversed: left out %v (decoded %d), want %v", got, opened, want) + } +} + // 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 taken as it is, its use counted on it: a copy