diff --git a/internal/usage/archive_row_test.go b/internal/usage/archive_row_test.go index 64ede71b6..2e0f575b5 100644 --- a/internal/usage/archive_row_test.go +++ b/internal/usage/archive_row_test.go @@ -24,7 +24,7 @@ func TestRowKeepsArchive(t *testing.T) { if r := chunk.row(i); r.Archive != fmt.Sprintf("2026-10-02/1010%02d-00000000000000aa", i) || r.Input != i { t.Fatalf("row %d: archive %q", i, r.Archive) } - if msg := chunk.Strings[chunk.Rows[i].Text[rowMsg]]; msg != fmt.Sprint("msg", i) { + if msg := chunk.Strings[chunk.text(&chunk.Rows[i], rowMsg)]; msg != fmt.Sprint("msg", i) { t.Fatalf("row %d: message %q", i, msg) } } diff --git a/internal/usage/block_price.go b/internal/usage/block_price.go index 26449a27b..ca4e67d57 100644 --- a/internal/usage/block_price.go +++ b/internal/usage/block_price.go @@ -115,29 +115,40 @@ func (b *blockPricer) block(packed *rowChunk) *rowChunk { added[s] = id return id } + // a shape's names are priced once, and the row takes the shape they + // make: the agent, the client and the provider are all a shape's + c.Shapes, c.shapeIDs = slices.Clip(c.Shapes), nil type ids struct { - provider uint32 - flags uint16 - price *catalog.Price + id uint32 // the provider's string, or the row's shape + flags uint16 + price *catalog.Price } names := map[[4]uint32]ids{} agents := map[uint32][2]uint32{} + shapes := map[uint32]ids{} for i := range c.Rows { p := &c.Rows[i] - k := [4]uint32{p.Text[1], p.Text[5], p.Text[7], p.Text[8]} - got, ok := names[k] + got, ok := shapes[p.Shape] if !ok { - a := b.as(c.Strings[k[0]], c.Strings[k[1]], c.Strings[k[2]], c.Strings[k[3]]) - got = ids{intern(a.provider, k[0]), a.flags, a.price} - names[k] = got + t := c.Shapes[p.Shape] + k := [4]uint32{t[1], t[5], t[7], t[8]} + named, ok := names[k] + if !ok { + a := b.as(c.Strings[k[0]], c.Strings[k[1]], c.Strings[k[2]], c.Strings[k[3]]) + named = ids{intern(a.provider, k[0]), a.flags, a.price} + names[k] = named + } + agent, ok := agents[t[0]] + if !ok { + a, client := b.agent(c.Strings[t[0]]) + agent = [2]uint32{intern(a, t[0]), intern(client, t[rowClient])} + agents[t[0]] = agent + } + t[0], t[1], t[rowClient] = agent[0], named.id, agent[1] + got = ids{c.shape(t), named.flags, named.price} + shapes[p.Shape] = got } - agent, ok := agents[p.Text[0]] - if !ok { - a, client := b.agent(c.Strings[p.Text[0]]) - agent = [2]uint32{intern(a, p.Text[0]), intern(client, p.Text[rowClient])} - agents[p.Text[0]] = agent - } - p.Text[0], p.Text[1], p.Text[rowClient] = agent[0], got.provider, agent[1] + p.Shape = got.id p.Flags = p.Flags&^(1|2|32) | got.flags if p.Flags&rowOwnCost != 0 { // a reported call that said what it cost costs that (#1874) diff --git a/internal/usage/log_index.go b/internal/usage/log_index.go index ade3c3f2b..5782c3689 100644 --- a/internal/usage/log_index.go +++ b/internal/usage/log_index.go @@ -212,7 +212,7 @@ func (o routeRows) handedOn(c *rowChunk, r *Record) { return } p := &c.Rows[j] - if p.Flags&rowHandedOn != 0 || p.Flags&4 != 0 || !(p.Status >= 400 || p.Text[rowError] != 0) { + if p.Flags&rowHandedOn != 0 || p.Flags&4 != 0 || !(p.Status >= 400 || c.text(p, rowError) != 0) { return } p.Flags |= rowHandedOn @@ -243,7 +243,8 @@ func (c *rowChunk) writable() *rowChunk { return &rowChunk{} } next := *d - next.Rows, next.Strings = slices.Clone(d.Rows), slices.Clone(d.Strings) + next.Rows, next.Strings, next.Shapes = slices.Clone(d.Rows), slices.Clone(d.Strings), slices.Clone(d.Shapes) + next.shapeIDs = nil next.dict = make(map[string]uint32, len(next.Strings)) for i, s := range next.Strings { next.dict[s] = uint32(i) @@ -257,6 +258,7 @@ func (c *rowChunk) freeze() *rowChunk { c.dict = nil c.Bytes -= int64(32 * len(c.Strings)) } + c.shapeIDs = nil return c.pack() } diff --git a/internal/usage/request_index_review_test.go b/internal/usage/request_index_review_test.go index 1cc8b187d..a86c2ea3a 100644 --- a/internal/usage/request_index_review_test.go +++ b/internal/usage/request_index_review_test.go @@ -186,7 +186,7 @@ func matchedLocalLegacy(gateway *rowChunk, chunks []*rowChunk, skip map[rowRef]b if skip[ref] || p.Time.Before(since) { continue } - id := c.Strings[p.Text[11]] + id := c.Strings[c.text(&p, 11)] if len(byID[id]) > 0 { byRequest[id] = append(byRequest[id], ref) } @@ -219,7 +219,7 @@ func matchedLocalLegacy(gateway *rowChunk, chunks []*rowChunk, skip map[rowRef]b if skip[ref] || matched[ref] || p.Time.Before(since) { continue } - s := c.Strings[p.Text[13]] + s := c.Strings[c.text(&p, 13)] if len(bySession[s]) == 0 { continue } diff --git a/internal/usage/request_page.go b/internal/usage/request_page.go index d47d7f6ac..971c837dc 100644 --- a/internal/usage/request_page.go +++ b/internal/usage/request_page.go @@ -61,8 +61,11 @@ type RequestPage struct { } type packedRow struct { - Time time.Time - Text [35]uint32 + Time time.Time + // Varied are the strings of rowVaried, and Shape the chunk's Shapes + // entry that holds the row's other strings + Varied [len(rowVaried)]uint32 + Shape uint32 Tokens [6]int64 Millis, TTFT, FirstText, Order int64 Sent, Flow, FirstReasoning int64 @@ -94,10 +97,15 @@ const rowOwnCost = 256 const rowCredits = 4096 type rowChunk struct { - Archive []byte - Locations []*time.Location - Rows []packedRow - Strings []string + Archive []byte + Locations []*time.Location + Rows []packedRow + Strings []string + // Shapes are the rows' strings but rowVaried's, each set kept once: + // in every row they were 140 of its 328 bytes, and the page holds every + // row of the period at once. 364k calls had 7300 shapes. + Shapes [][rowTexts]uint32 + shapeIDs map[[rowTexts]uint32]uint32 codexActors []string codexCompactions map[int]*bool codexIdentities map[string]sessions.CodexIdentity @@ -119,8 +127,69 @@ const ( // rowClient is the Text of the client's own name, when the user counts // it as another agent (Row.Client, #1958) rowClient = 33 + // rowTexts is how many strings a row has: rowText's and the message id + rowTexts = 35 ) +// rowVaried are the strings that differ call by call: the error, the +// request and response ids, the archive and the message id. A row keeps +// them itself; every other string is the same for most of a chunk's rows. +var rowVaried = [...]int{rowError, 11, 22, 25, rowMsg} + +// rowVariedAt is each Text slot's place in Varied, or -1 +var rowVariedAt = func() (at [rowTexts]int8) { + for i := range at { + at[i] = -1 + } + for k, slot := range rowVaried { + at[slot] = int8(k) + } + return at +}() + +// text is the string id of p's Text slot +func (c *rowChunk) text(p *packedRow, slot int) uint32 { + if k := rowVariedAt[slot]; k >= 0 { + return p.Varied[k] + } + return c.Shapes[p.Shape][slot] +} + +// texts is every string id of p, by Text slot +func (c *rowChunk) texts(p *packedRow) [rowTexts]uint32 { + t := c.Shapes[p.Shape] + for k, slot := range rowVaried { + t[slot] = p.Varied[k] + } + return t +} + +// setTexts keeps t, every string id of p by Text slot, in p and Shapes +func (c *rowChunk) setTexts(p *packedRow, t [rowTexts]uint32) { + for k, slot := range rowVaried { + p.Varied[k], t[slot] = t[slot], 0 + } + p.Shape = c.shape(t) +} + +// shape is the Shapes entry of t, which is added when there is none +func (c *rowChunk) shape(t [rowTexts]uint32) uint32 { + if c.shapeIDs == nil { + c.shapeIDs = make(map[[rowTexts]uint32]uint32, len(c.Shapes)) + for i, s := range c.Shapes { + c.shapeIDs[s] = uint32(i) + } + } + id, ok := c.shapeIDs[t] + if !ok { + id = uint32(len(c.Shapes)) + c.Shapes = append(c.Shapes, t) + c.shapeIDs[t] = id + c.Bytes += int64(unsafe.Sizeof(t)) + } + return id +} + // rowText is every string of a row's record, the upstream's own stop // reason ("Upstream stop reason", #1927) and the service tiers (#1784) // among them: a string left out comes back empty from the cache, in the @@ -144,10 +213,12 @@ func (c *rowChunk) add(r Row, msg string, order int64, failed bool) { return id } p := packedRow{Time: r.Time, Tokens: [6]int64{int64(r.Input), int64(r.Output), int64(r.CacheRead), int64(r.CacheWrite), int64(r.Reasoning), int64(r.CacheWrite1h)}, Millis: r.Millis, TTFT: r.TTFT, FirstText: r.FirstText, FirstReasoning: r.FirstReasoning, Sent: r.Sent, Flow: r.Flow, Own: r.Own, Retried: r.Retried, Queued: r.Queued, Order: order, RouteID: r.RouteID, Cost: r.Cost, Status: int32(r.Status), Tries: int32(r.Tries)} + var t [rowTexts]uint32 for i, s := range rowText(&r) { - p.Text[i] = intern(*s) + t[i] = intern(*s) } - p.Text[rowMsg] = intern(msg) + t[rowMsg] = intern(msg) + c.setTexts(&p, t) if r.Priced { p.Flags |= 1 } @@ -222,8 +293,9 @@ func (c *rowChunk) row(i int) Row { r.Compaction = &value } } + t := c.texts(p) for i, s := range rowText(&r) { - *s = c.Strings[p.Text[i]] + *s = c.Strings[t[i]] } if r.Swapped && p.Flags&swapKnown == 0 && (SameSpelled(r.Model, r.Served) || ServingName(r.Model, r.Served) || RelayRenamed(r.Model, r.Served)) { r.Swapped = false // kept before a name spelled otherwise, a vendor's serving name or a relay's rename was the same @@ -528,7 +600,7 @@ func queryPage(p Period, f Filter, offset, limit int, readSource func(sessions.C c.add(row, call.Msg, call.To, call.Error != "") } c.Bytes -= int64(32 * len(c.Strings)) - c.dict = nil // local chunks are immutable until the source changes + c.dict, c.shapeIDs = nil, nil // local chunks are immutable until the source changes idx.chunks[s.Path] = c } next := *c @@ -574,7 +646,7 @@ func queryPage(p Period, f Filter, offset, limit int, readSource func(sessions.C c.add(priceRow(r, call.Source), "", int64(i), false) } c.Bytes -= int64(32 * len(c.Strings)) - c.dict = nil + c.dict, c.shapeIDs = nil, nil } next := *c c = &next @@ -661,7 +733,7 @@ func visibleLocal(chunks []*rowChunk) map[rowRef]bool { for _, c := range chunks { for i := range c.Rows { p := &c.Rows[i] - msg := c.Strings[p.Text[rowMsg]] + msg := c.Strings[c.text(p, rowMsg)] if msg == "" { continue } @@ -738,10 +810,10 @@ func matchedBlocks(gateways []*rowChunk, chunks []*rowChunk, skip map[rowRef]boo continue } p := &gateway.Rows[i] - if (p.Flags&4 != 0 || p.Text[1] == 0 && p.Status >= 400) || p.Time.Before(gatewaySince) { + if (p.Flags&4 != 0 || gateway.text(p, 1) == 0 && p.Status >= 400) || p.Time.Before(gatewaySince) { continue } - if id := gateway.Strings[p.Text[11]]; id != "" { + if id := gateway.Strings[gateway.text(p, 11)]; id != "" { byID[id] = append(byID[id], rowRef{gateway, i}) } } @@ -754,7 +826,7 @@ func matchedBlocks(gateways []*rowChunk, chunks []*rowChunk, skip map[rowRef]boo if skip[ref] || reserved[ref] || p.Time.Before(since) { continue } - if id := c.Strings[p.Text[11]]; len(byID[id]) > 0 { + if id := c.Strings[c.text(p, 11)]; len(byID[id]) > 0 { byRequest[id] = append(byRequest[id], ref) } } @@ -787,20 +859,20 @@ func matchedBlocks(gateways []*rowChunk, chunks []*rowChunk, skip map[rowRef]boo continue } p := &gateway.Rows[i] - if used[ref] || (p.Flags&4 != 0 || p.Text[1] == 0 && p.Status >= 400) || p.Time.Before(gatewaySince) { + if used[ref] || (p.Flags&4 != 0 || gateway.text(p, 1) == 0 && p.Status >= 400) || p.Time.Before(gatewaySince) { continue } - session := gateway.Strings[p.Text[14]] + session := gateway.Strings[gateway.text(p, 14)] if session == "" { - session = gateway.Strings[p.Text[13]] + session = gateway.Strings[gateway.text(p, 13)] } - if session == "" && gateway.Strings[p.Text[0]] == "agy" { + if session == "" && gateway.Strings[gateway.text(p, 0)] == "agy" { session = agyMatchSession } if session == "" { continue } - key := matchKey{session, matchTokens(p.Tokens), p.Status >= 400 || p.Text[9] != 0, p.Text[11] != 0} + key := matchKey{session, matchTokens(p.Tokens), p.Status >= 400 || gateway.text(p, 9) != 0, gateway.text(p, 11) != 0} g := groups[key] if g == nil { g = &matchGroup{} @@ -825,16 +897,16 @@ func matchedBlocks(gateways []*rowChunk, chunks []*rowChunk, skip map[rowRef]boo if p.Tokens[0]+p.Tokens[1]+p.Tokens[2]+p.Tokens[3] == 0 && !failed { continue } - key := matchKey{session: c.Strings[p.Text[13]], tokens: matchTokens(p.Tokens), failed: failed} + key := matchKey{session: c.Strings[c.text(p, 13)], tokens: matchTokens(p.Tokens), failed: failed} from, to := p.Time.Add(-2*time.Second), p.Time.Add(2*time.Second) - if c.Strings[p.Text[0]] == "agy" { + if c.Strings[c.text(p, 0)] == "agy" { key.session, to = agyMatchSession, p.Time.Add(agyStepLongest) } count, candidate := 0, (rowRef{}) // Without a local ID either gateway partition can match. With an ID, // only an unnamed gateway call can match (different IDs stay distinct). for _, hasID := range []bool{false, true} { - if hasID && p.Text[11] != 0 { + if hasID && c.text(p, 11) != 0 { continue } key.hasID = hasID @@ -929,7 +1001,7 @@ func buildRequestBlocksWithCodex(p Period, f Filter, offset, limit int, now time visit := func(fn func(rowRef, Row)) { for _, c := range all { for i := range c.Rows { - pr := &c.Rows[i] // not a copy: a row is 264 bytes + pr := &c.Rows[i] // not a copy: a row is 216 bytes ref := rowRef{c, i} if pr.Time.Before(since) || after(until, pr.Time) || len(skip) > 0 && skip[ref] || len(matched) > 0 && matched[ref] { continue diff --git a/internal/usage/request_snapshot.go b/internal/usage/request_snapshot.go index 4a1ac43cb..627476afb 100644 --- a/internal/usage/request_snapshot.go +++ b/internal/usage/request_snapshot.go @@ -47,6 +47,12 @@ func (c *rowChunk) pack() *rowChunk { put(uint64(len(s))) data = append(data, s...) } + put(uint64(len(c.Shapes))) + for _, shape := range c.Shapes { + for _, n := range shape { + put(uint64(n)) + } + } var locations []*time.Location locs := map[*time.Location]uint64{} for k := range c.Rows { @@ -61,7 +67,8 @@ func (c *rowChunk) pack() *rowChunk { put(uint64(p.Time.Unix())) put(uint64(p.Time.Nanosecond())) put(id) - for _, n := range p.Text { + put(uint64(p.Shape)) + for _, n := range p.Varied { put(uint64(n)) } for _, n := range p.Tokens { @@ -84,7 +91,7 @@ func (c *rowChunk) pack() *rowChunk { return c } next := *c - next.Rows, next.Strings, next.dict = nil, nil, nil + next.Rows, next.Strings, next.Shapes, next.dict, next.shapeIDs = nil, nil, nil, nil, nil next.Archive, next.Locations = slices.Clone(encoded), locations next.Bytes = int64(len(next.Archive)+len(locations)*8) + codexActorBytes(next.codexActors) return &next @@ -170,6 +177,21 @@ func (c *rowChunk) unpackInto(into []packedRow) *rowChunk { next.Strings[i] = text[from : from+n] next.Bytes += int64(n) + 16 } + shapes := take() + if shapes > uint64(len(data)) { + return nil + } + next.Shapes, next.shapeIDs = make([][rowTexts]uint32, int(shapes)), nil + for i := range next.Shapes { + for j := range next.Shapes[i] { + n := take() + if n >= strs { + return nil + } + next.Shapes[i][j] = uint32(n) + } + } + next.Bytes += int64(len(next.Shapes)) * int64(unsafe.Sizeof([rowTexts]uint32{})) for i := range next.Rows { p := &next.Rows[i] sec, nano, loc := int64(take()), int64(take()), take() @@ -177,12 +199,16 @@ func (c *rowChunk) unpackInto(into []packedRow) *rowChunk { return nil } p.Time = time.Unix(sec, nano).In(c.Locations[loc]) - for j := range p.Text { + p.Shape = uint32(take()) + if int(p.Shape) >= len(next.Shapes) { + return nil + } + for j := range p.Varied { n := take() if n >= strs { return nil } - p.Text[j] = uint32(n) + p.Varied[j] = uint32(n) } for j := range p.Tokens { p.Tokens[j] = int64(take()) diff --git a/internal/usage/response_id_storage_test.go b/internal/usage/response_id_storage_test.go index 9284e246d..34079f27e 100644 --- a/internal/usage/response_id_storage_test.go +++ b/internal/usage/response_id_storage_test.go @@ -31,7 +31,7 @@ func testResponseIdentityStorageRoundTrip(t *testing.T, location *time.Location) for _, c := range []*rowChunk{chunk, packed.unpack()} { for i := range c.Rows { r := c.row(i) - if r.ResponseID != fmt.Sprint("resp", i) || r.RequestID != fmt.Sprint("req", i) || c.Strings[c.Rows[i].Text[rowMsg]] != fmt.Sprint("msg", i) { + if r.ResponseID != fmt.Sprint("resp", i) || r.RequestID != fmt.Sprint("req", i) || c.Strings[c.text(&c.Rows[i], rowMsg)] != fmt.Sprint("msg", i) { t.Fatalf("packed identity changed %+v", r) } } diff --git a/internal/usage/row_shape_test.go b/internal/usage/row_shape_test.go new file mode 100644 index 000000000..851debb72 --- /dev/null +++ b/internal/usage/row_shape_test.go @@ -0,0 +1,55 @@ +package usage + +import ( + "fmt" + "reflect" + "testing" + "time" + "unsafe" +) + +// A chunk's rows keep the strings they share once, in Shapes, and only +// their own ids in the row: the page holds every row of the period at once, +// and 140 of a row's 328 bytes were string ids most rows of a chunk repeat. +// Packed and read back, every row is the row it was. +func TestRowsOfOneSessionShareAShape(t *testing.T) { + if n := unsafe.Sizeof(packedRow{}); n > 216 { + t.Fatalf("a packed row is %d bytes, want 216 at most", n) + } + at := time.Date(2026, 10, 21, 12, 0, 0, 0, time.Local) + c := &rowChunk{} + var want []Row + for i := range 200 { + r := Row{Record: Record{Time: at.Add(time.Duration(i) * time.Second), Agent: "claude", Provider: "anthropic", Model: "claude-opus-5-5", + Session: "s1", Endpoint: "/v1/messages", Input: i, Output: 2 * i, Status: 200, + RequestID: fmt.Sprint("req", i), ResponseID: fmt.Sprint("resp", i), Archive: fmt.Sprint("a", i)}, Source: "log"} + if i%7 == 0 { + r.Error, r.Status = fmt.Sprint("overloaded ", i), 529 + } + if i == 150 { + r.Model = "claude-sonnet-5-5" + } + c.add(r, fmt.Sprint("msg", i), int64(i), r.Error != "") + want = append(want, c.row(i)) + } + if len(c.Shapes) != 2 { + t.Fatalf("200 rows of two models kept %d shapes, want 2", len(c.Shapes)) + } + c.dict, c.shapeIDs = nil, nil + packed := c.pack() + if len(packed.Archive) == 0 || packed.Shapes != nil { + t.Fatalf("not packed: %d archive bytes, %d shapes", len(packed.Archive), len(packed.Shapes)) + } + back := packed.unpack() + if back == nil || len(back.Rows) != len(want) || len(back.Shapes) != 2 { + t.Fatalf("read back: %+v", back) + } + for i, w := range want { + if got := back.row(i); !reflect.DeepEqual(got, w) { + t.Fatalf("row %d read back as\n%+v\nwant\n%+v", i, got, w) + } + if msg := back.Strings[back.text(&back.Rows[i], rowMsg)]; msg != fmt.Sprint("msg", i) { + t.Fatalf("row %d's message id read back as %q", i, msg) + } + } +} diff --git a/internal/usage/stop_row_test.go b/internal/usage/stop_row_test.go index d0b61e7c3..cf712dbc4 100644 --- a/internal/usage/stop_row_test.go +++ b/internal/usage/stop_row_test.go @@ -50,8 +50,8 @@ func TestRequestPageKeepsUpstreamStop(t *testing.T) { c := &rowChunk{} c.add(Row{Record: Record{Time: now, Stop: "tool_calls", ParentAgent: "a0"}}, "msg", 1, false) for _, chunk := range []*rowChunk{c, c.pack().unpack()} { - if r := chunk.row(0); r.Stop != "tool_calls" || r.ParentAgent != "a0" || chunk.Strings[chunk.Rows[0].Text[rowMsg]] != "msg" { - t.Fatalf("packed and read back: stop %q, parent %q, msg %q", r.Stop, r.ParentAgent, chunk.Strings[chunk.Rows[0].Text[rowMsg]]) + if r := chunk.row(0); r.Stop != "tool_calls" || r.ParentAgent != "a0" || chunk.Strings[chunk.text(&chunk.Rows[0], rowMsg)] != "msg" { + t.Fatalf("packed and read back: stop %q, parent %q, msg %q", r.Stop, r.ParentAgent, chunk.Strings[chunk.text(&chunk.Rows[0], rowMsg)]) } } } diff --git a/internal/usage/subagent_row_test.go b/internal/usage/subagent_row_test.go index 52edefd1f..a6eeaad28 100644 --- a/internal/usage/subagent_row_test.go +++ b/internal/usage/subagent_row_test.go @@ -30,7 +30,7 @@ func TestRowKeepsSubagent(t *testing.T) { c := &rowChunk{} c.add(Row{Record: Record{Time: time.Now(), Agent: "claude", Session: "conv-1", Subagent: "a1", ParentAgent: "a0"}}, "msg", 1, false) for _, chunk := range []*rowChunk{c, c.pack().unpack()} { - if r := chunk.row(0); r.Subagent != "a1" || r.ParentAgent != "a0" || r.Session != "conv-1" || chunk.Strings[chunk.Rows[0].Text[rowMsg]] != "msg" { + if r := chunk.row(0); r.Subagent != "a1" || r.ParentAgent != "a0" || r.Session != "conv-1" || chunk.Strings[chunk.text(&chunk.Rows[0], rowMsg)] != "msg" { t.Fatalf("read back: %+v", r) } }