diff --git a/internal/usage/match_chunks.go b/internal/usage/match_chunks.go new file mode 100644 index 000000000..906192fc1 --- /dev/null +++ b/internal/usage/match_chunks.go @@ -0,0 +1,119 @@ +package usage + +import ( + "hash/maphash" + "slices" + "sync" + "time" +) + +// matchHints are what matchedBlocks needs to know of a local chunk before +// decoding it, kept by the chunk's serial: the request ids its calls name, +// the sessions its calls can be matched in, and its last call's time. A +// poll matched the gateway's calls against every session file of the +// period, decoding each twice to find the few sessions the gateway had +// calls of: ~0.8 GB a minute and seconds of CPU while an agent worked +// (#2278). A chunk none of whose ids or sessions the gateway names can +// match nothing, and is not decoded. +var matchHints struct { + sync.Mutex + by map[uint64]*chunkHints + turn uint64 +} + +type chunkHints struct { + ids []uint32 // matchHash of each request id, sorted, once each + sessions []uint64 // matchHash of each session a call can match in + latest time.Time + turn uint64 +} + +// hintsKept is how many matchings a chunk's hints outlive the last that +// read them: the page, the Sessions pages and the ledger match chunks of +// their own. +const hintsKept = 8 + +var matchSeed = maphash.MakeSeed() + +func matchHash(s string) uint64 { return maphash.String(matchSeed, s) } + +// matchOpening, in tests, is told each local chunk matchedBlocks decodes. +var matchOpening func(*rowChunk) + +// hintsOf are chunks' hints, read from a chunk only when none are kept for +// its serial. +func hintsOf(chunks []*rowChunk, rows *chunkRows) []*chunkHints { + matchHints.Lock() + matchHints.turn++ + turn := matchHints.turn + out := make([]*chunkHints, len(chunks)) + for i, c := range chunks { + if h := matchHints.by[c.serial]; c.serial != 0 && h != nil { + h.turn = turn + out[i] = h + } + } + matchHints.Unlock() + made := map[uint64]*chunkHints{} + for i, c := range chunks { + if out[i] != nil { + continue + } + h := &chunkHints{turn: turn} + open := rows.of(c) + for j := range open.Rows { + p := &open.Rows[j] + if p.Time.After(h.latest) { + h.latest = p.Time + } + if id := open.Strings[open.text(p, 11)]; id != "" { + h.ids = append(h.ids, uint32(matchHash(id))) + } + if p.Tokens[0]+p.Tokens[1]+p.Tokens[2]+p.Tokens[3] == 0 && p.Flags&16 == 0 { + continue + } + session := open.Strings[open.text(p, 13)] + if open.Strings[open.text(p, 0)] == "agy" { + session = agyMatchSession + } + if s := matchHash(session); len(h.sessions) == 0 || h.sessions[len(h.sessions)-1] != s { + h.sessions = append(h.sessions, s) + } + } + slices.Sort(h.ids) + h.ids = slices.Clip(slices.Compact(h.ids)) + slices.Sort(h.sessions) + h.sessions = slices.Clip(slices.Compact(h.sessions)) + out[i] = h + if c.serial != 0 { + made[c.serial] = h + } + } + matchHints.Lock() + if matchHints.by == nil { + matchHints.by = map[uint64]*chunkHints{} + } + for serial, h := range made { + matchHints.by[serial] = h + } + for serial, h := range matchHints.by { + if h.turn+hintsKept < matchHints.turn { + delete(matchHints.by, serial) + } + } + matchHints.Unlock() + return out +} + +// namesAny says some id of ids is in set +func namesAny[T comparable](ids []T, set map[T]struct{}) bool { + if len(set) == 0 { + return false + } + for _, id := range ids { + if _, ok := set[id]; ok { + return true + } + } + return false +} diff --git a/internal/usage/match_chunks_test.go b/internal/usage/match_chunks_test.go new file mode 100644 index 000000000..562a6d096 --- /dev/null +++ b/internal/usage/match_chunks_test.go @@ -0,0 +1,93 @@ +package usage + +import ( + "fmt" + "math/rand" + "reflect" + "testing" + "time" + + "github.com/yetone/magpie/internal/sessions" +) + +// Matching the gateway's calls to the session files' decodes only the +// files whose sessions or request ids the gateway names, and matches as +// reading every file did (#2278). +func TestMatchOpensOnlyNamedChunks(t *testing.T) { + for seed := int64(0); seed < 20; seed++ { + rng := rand.New(rand.NewSource(seed)) + now := time.Now().Truncate(time.Second) + gateway := &rowChunk{} + var locals []*rowChunk + named := map[*rowChunk]bool{} + for s := range 12 { + session := fmt.Sprint("session-", s) + local := &rowChunk{Source: sessions.CallSource{Path: "/local/" + session}} + for i := range 60 { + r := Record{Time: now.Add(time.Duration(rng.Intn(20)) * time.Second), Agent: "claude", Provider: "relay", Session: session, Input: rng.Intn(3), Output: rng.Intn(2), Millis: 1000} + if i%4 == 0 { + r.RequestID = fmt.Sprint(session, "-", i) + } + if i%5 == 0 { + r.Status = 500 + } + if s < 4 { // the gateway has calls of four sessions + gateway.add(Row{Record: r}, "", int64(len(gateway.Rows)), false) + } + if s == 0 && i == 0 { // its call is named in another session's file + r.RequestID = "" + } + if s == 5 && i == 0 { + r.RequestID = "session-0-0" + } + r.Time = r.Time.Add(time.Duration(rng.Intn(5)-2) * time.Second) + local.add(Row{Record: r, Source: "log"}, "", int64(i), r.Failed()) + } + local.serial = nextChunkSerial() + locals = append(locals, local) + if s < 4 || s == 5 { + named[local] = true + } + } + for _, since := range []time.Time{{}, now.Add(5 * time.Second)} { + var opened []*rowChunk + matchOpening = func(c *rowChunk) { opened = append(opened, c) } + got := matchedBlocks([]*rowChunk{gateway}, locals, nil, nil, since, false) + matchOpening = nil + want := matchedLocalLegacy(gateway, locals, nil, since) + if !reflect.DeepEqual(got, want) { + t.Fatalf("seed %d: %d matches, want %d", seed, len(got), len(want)) + } + for _, c := range opened { + if !named[c] { + t.Fatalf("seed %d: matching decoded %s, whose sessions and ids the gateway never named", seed, c.Source.Path) + } + } + if since.IsZero() && len(opened) == 0 { + t.Fatalf("seed %d: matching decoded no chunk", seed) + } + } + } + t.Cleanup(func() { matchOpening = nil }) +} + +// agy's gateway calls name no session, and its transcript's steps are +// matched by tokens and time (#2097): its file is one the gateway names. +func TestMatchOpensAgyChunks(t *testing.T) { + now := time.Now().Truncate(time.Second) + gateway, agy, other := &rowChunk{}, &rowChunk{}, &rowChunk{} + gateway.add(Row{Record: Record{Time: now, Agent: "agy", Provider: "relay", Input: 7, Output: 3, Millis: 1000}}, "", 0, false) + agy.add(Row{Record: Record{Time: now.Add(-time.Minute), Agent: "agy", Session: "agy-transcript", Input: 7, Output: 3}, Source: "log"}, "", 0, false) + other.add(Row{Record: Record{Time: now, Agent: "claude", Session: "s", Input: 7, Output: 3}, Source: "log"}, "", 0, false) + agy.serial, other.serial = nextChunkSerial(), nextChunkSerial() + var opened []*rowChunk + matchOpening = func(c *rowChunk) { opened = append(opened, c) } + t.Cleanup(func() { matchOpening = nil }) + got := matchedBlocks([]*rowChunk{gateway}, []*rowChunk{agy, other}, nil, nil, time.Time{}, false) + if !got[rowRef{agy, 0}] || len(got) != 1 { + t.Fatalf("matched %v; want the agy step alone", got) + } + if len(opened) != 1 || opened[0] != agy { + t.Fatalf("decoded %d chunks; want the agy transcript alone", len(opened)) + } +} diff --git a/internal/usage/request_page.go b/internal/usage/request_page.go index 8aa28cc30..b9d899f53 100644 --- a/internal/usage/request_page.go +++ b/internal/usage/request_page.go @@ -1354,7 +1354,18 @@ func matchedBlocks(gateways []*rowChunk, chunks []*rowChunk, skip map[rowRef]boo byRequest := map[string][]timedRef{} rows := lentRows() // a kept request id is cloned; a key is looked up defer rows.release() - for _, c := range chunks { + hints := hintsOf(chunks, &rows) + ids := make(map[uint32]struct{}, len(byID)) + for id := range byID { + ids[uint32(matchHash(id))] = struct{}{} + } + for n, c := range chunks { + if hints[n].latest.Before(since) || !namesAny(hints[n].ids, ids) { + continue + } + if matchOpening != nil { + matchOpening(c) + } open := rows.of(c) for i := range open.Rows { p := &open.Rows[i] @@ -1424,8 +1435,18 @@ func matchedBlocks(gateways []*rowChunk, chunks []*rowChunk, skip map[rowRef]boo slices.SortFunc(g.ends, func(a, b matchEnd) int { return a.at.Compare(b.at) }) g.counts = make([]int, len(g.ends)+1) } + named := make(map[uint64]struct{}, len(groups)) + for key := range groups { + named[matchHash(key.session)] = struct{}{} + } candidates := map[rowRef]rowRef{} - for _, chunk := range chunks { + for n, chunk := range chunks { + if hints[n].latest.Before(since) || !namesAny(hints[n].sessions, named) { + continue + } + if matchOpening != nil { + matchOpening(chunk) + } c := rows.of(chunk) for j := range c.Rows { p := &c.Rows[j]