Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.
usage: a session file's rows are made by the reader that read it (#3493)
yetone2a3d9f5aeba5
diff --git a/internal/usage/prefetch_test.go b/internal/usage/prefetch_test.goindex 363e362ec..7b64d4265 100644--- a/internal/usage/prefetch_test.go+++ b/internal/usage/prefetch_test.go@@ -37,7 +37,7 @@ func TestPrefetchSources(t *testing.T) {return []sessions.Call{{Model: s.Path}}}want := func(s sessions.CallSource) bool { return s.Path != "7" }- read := prefetchSources(sources, want, readSource)+ read := prefetchEach(sources, want, func() func(sessions.CallSource) []sessions.Call { return readSource })for i := range sources {if i == 50 {// nothing is taken for a while: the readers stop at the window@@ -65,7 +65,7 @@ func TestPrefetchSources(t *testing.T) {// one or none wanted: read as asked, nothing aheadreads = map[string]int{}- read = prefetchSources(sources[:3], func(s sessions.CallSource) bool { return s.Path == "1" }, readSource)+ read = prefetchEach(sources[:3], func(s sessions.CallSource) bool { return s.Path == "1" }, func() func(sessions.CallSource) []sessions.Call { return readSource })if len(reads) != 0 {t.Fatalf("read ahead: %v", reads)}@@ -73,3 +73,32 @@ func TestPrefetchSources(t *testing.T) {t.Fatalf("%+v", cs)}}++// Each reader makes what it makes of a source with a make of its own, and+// so does the loop for a source not wanted: what a make keeps between+// sources (a pricer's memos) is never written from two at once. -race+// fails when one is shared.+func TestPrefetchEachMakeIsAReadersOwn(t *testing.T) {+ var sources []sessions.CallSource+ for i := range 100 {+ sources = append(sources, sessions.CallSource{Path: fmt.Sprint(i)})+ }+ var makes atomic.Int32+ read := prefetchEach(sources, func(s sessions.CallSource) bool { return s.Path != "3" }, func() func(sessions.CallSource) string {+ makes.Add(1)+ seen := map[string]bool{} // unlocked, as a reader's memo is+ return func(s sessions.CallSource) string {+ seen[s.Path] = true+ time.Sleep(100 * time.Microsecond)+ return s.Path + fmt.Sprint(len(seen) > 0)+ }+ })+ for i, s := range sources {+ if got := read(i); got != s.Path+"true" {+ t.Fatalf("source %d: %q", i, got)+ }+ }+ if n := makes.Load(); n != 9 { // 8 readers and the loop's own+ t.Fatalf("%d makes, want 9", n)+ }+}diff --git a/internal/usage/request_page.go b/internal/usage/request_page.goindex f2f583beb..6aa8da943 100644--- a/internal/usage/request_page.go+++ b/internal/usage/request_page.go@@ -770,30 +770,35 @@ func readRequestBlocks(idx *requestIndex, st requestState, p Period, now time.Tisources, cfg, snapshot, days := st.sources, st.cfg, st.snapshot, st.daysprice := Pricer()// Swapped's names are a few hundred pairs over thousands of files' calls,- // and its regexps were a third of reading them again (#3493)- swapped := map[[2]string]bool{}- priceRow := func(r Record, source string) Row {- sent := r.Model- if source == "" {- sent = provider.SentNameIn(cfg.ModelWires, r.Provider, r.Model, r.Effort)- }- row := Row{Record: r, Source: source, Routed: GroupRouted(sent, r.Served)}- if r.Served != "" {- k := [2]string{sent, r.Served}- s, ok := swapped[k]- if !ok {- s = Swapped(sent, r.Served)- swapped[k] = s+ // and its regexps were a third of reading them again (#3493). Each+ // reader of the session files has a pricer of its own, with its memos.+ pricers := func() func(Record, string) Row {+ price := Pricer()+ swapped := map[[2]string]bool{}+ return func(r Record, source string) Row {+ sent := r.Model+ if source == "" {+ sent = provider.SentNameIn(cfg.ModelWires, r.Provider, r.Model, r.Effort)}- row.Swapped = s- }- if pr := price(r); pr != nil && r.Input+r.Output > 0 {- row.Priced = true- row.Cost = r.CostAt(*pr)+ row := Row{Record: r, Source: source, Routed: GroupRouted(sent, r.Served)}+ if r.Served != "" {+ k := [2]string{sent, r.Served}+ s, ok := swapped[k]+ if !ok {+ s = Swapped(sent, r.Served)+ swapped[k] = s+ }+ row.Swapped = s+ }+ if pr := price(r); pr != nil && r.Input+r.Output > 0 {+ row.Priced = true+ row.Cost = r.CostAt(*pr)+ }+ row.Agent, row.Client = reportedAs(r.Agent)+ return row}- row.Agent, row.Client = reportedAs(r.Agent)- return row}+ priceRow := pricers()since := p.Since(now)gatewaySince := sinceif !since.IsZero() {@@ -818,7 +823,9 @@ func readRequestBlocks(idx *requestIndex, st requestState, p Period, now time.Ti}on := map[string]bool{}var chunks []*rowChunk- var resolver *sessionResolver+ // made once a session file is to be read again, shared by the readers:+ // it is only read after+ resolver := sync.OnceValue(func() *sessionResolver { return newSessionResolver([]sessions.Call{{}}) })stale := func(s sessions.CallSource) bool {if !since.IsZero() && s.Modified.Before(since) {return false@@ -836,25 +843,17 @@ func readRequestBlocks(idx *requestIndex, st requestState, p Period, now time.Tiidx.fromDisk(want)}built := 0- read := prefetchSources(sources, stale, readSource)- for i, s := range sources {- on[s.Path] = true- if !since.IsZero() && s.Modified.Before(since) {- continue- }- // kept packed: the page reads a chunk's rows when it reaches them- // (chunkRows), not every file's at once- c := idx.chunks[s.Path]- if reused := c != nil && c.Source.Size == s.Size && c.Source.Modified.Equal(s.Modified); !reused {- if resolver == nil {- resolver = newSessionResolver([]sessions.Call{{}})- }- cs := read(i)- c = &rowChunk{Source: s, Rows: make([]packedRow, 0, len(cs))}+ // A file's rows are made by the reader that read it: made one after+ // another, they were most of a cold build's wait (#3493).+ read := prefetchEach(sources, stale, func() func(sessions.CallSource) *rowChunk {+ priceRow := pricers()+ return func(s sessions.CallSource) *rowChunk {+ cs := readSource(s)+ c := &rowChunk{Source: s, Rows: make([]packedRow, 0, len(cs))}for _, call := range cs {r := logRecord(call)- r.SessionAccount = resolver.resolve(call)- r.SessionOfficialLogin = resolver.officialLogin(call, r.SessionAccount)+ r.SessionAccount = resolver().resolve(call)+ r.SessionOfficialLogin = resolver().officialLogin(call, r.SessionAccount)r.SessionProvider = call.Upstreamrow := priceRow(r, "log")row.codexActor = codexActor(call)@@ -862,7 +861,19 @@ func readRequestBlocks(idx *requestIndex, st requestState, p Period, now time.Ti}c.Bytes -= int64(32 * len(c.Strings))c.dict, c.shapeIDs = nil, nil // local chunks are immutable until the source changes- c = c.pack()+ return c.pack()+ }+ })+ for i, s := range sources {+ on[s.Path] = true+ if !since.IsZero() && s.Modified.Before(since) {+ continue+ }+ // kept packed: the page reads a chunk's rows when it reaches them+ // (chunkRows), not every file's at once+ c := idx.chunks[s.Path]+ if reused := c != nil && c.Source.Size == s.Size && c.Source.Modified.Equal(s.Modified); !reused {+ c = read(i)c.serial = nextChunkSerial()built++}@@ -1823,17 +1834,19 @@ func callerGroups[K comparable](groups map[K]*Group) []Group {return out}-// prefetchSources reads the session files a query has to parse a few at a+// prefetchEach reads the session files a query has to parse a few at a// time, ahead of the loop that takes them in order. The first All after// magpie starts parses every one of them, and one by one that was a wait on// the disk for each: Requests stayed a skeleton for 40 s over 12k Codex-// sessions. read(i) gives source i's calls; at most a window of them waits-// to be taken, so the history is never all held at once. A source that-// wasn't wanted is read when it is asked for.-func prefetchSources(sources []sessions.CallSource, want func(sessions.CallSource) bool, readSource func(sessions.CallSource) []sessions.Call) func(int) []sessions.Call {+// sessions. read(i) gives what a make from newMake made of source i; at+// most a window of them waits to be taken, so the history is never all+// held at once. A source that wasn't wanted is read when it is asked for.+// Each reader, and the loop for a source not wanted, has a make of its+// own, so what a make keeps between sources needs no lock.+func prefetchEach[T any](sources []sessions.CallSource, want func(sessions.CallSource) bool, newMake func() func(sessions.CallSource) T) func(int) T {type slot struct {- done chan struct{}- calls []sessions.Call+ done chan struct{}+ v T}slots := map[int]*slot{}var order []int@@ -1843,8 +1856,15 @@ func prefetchSources(sources []sessions.CallSource, want func(sessions.CallSourcorder = append(order, i)}}+ var own func(sessions.CallSource) T+ alone := func(i int) T {+ if own == nil {+ own = newMake()+ }+ return own(sources[i])+ }if len(order) < 2 {- return func(i int) []sessions.Call { return readSource(sources[i]) }+ return alone}workers := min(8, len(order))window := make(chan struct{}, 4*workers)@@ -1858,22 +1878,24 @@ func prefetchSources(sources []sessions.CallSource, want func(sessions.CallSourc}()for range workers {go func() {+ build := newMake()for i := range next {s := slots[i]- s.calls = readSource(sources[i])+ s.v = build(sources[i])close(s.done)}}()}- return func(i int) []sessions.Call {+ return func(i int) T {s := slots[i]if s == nil {- return readSource(sources[i])+ return alone(i)}<-s.done<-window- cs := s.calls- s.calls = nil- return cs+ v := s.v+ var zero T+ s.v = zero+ return v}}