usage: a session file's rows are made by the reader that read it (#3493)
A cold Requests build (every upgrade) read the call shards eight at a time, but turned every call into a row on the one loop that takes them: logRecord, the account resolver, pricing, interning and packing, ~1 s of serial CPU over a real history, with the readers waiting on the window behind it. prefetchSources becomes prefetchEach: each reader has a make of its own from newMake, and the loop has its own for a source not wanted. Request rows are made there. Each make has its own Pricer and Swapped memo, so neither needs a lock; the session resolver is made once (sync.OnceValue) and only read after. The serial number is still given in the loop, in source order. The window now holds packed chunks, smaller than the calls it held. Measured on a copy of real data (15,656 shards, 3,451 Claude Code subagent meta files), GUI warm-up step 2 (QueryPage(All)), cold (requests-v1.bin written for the other binary), alternating runs: - before (432393217): 3.24 s / 448 MB maxrss, 2.18 s / 433 MB - after: 1.85 s / 417 MB, 1.84 s / 410 MB The readers are now mostly in syscalls (shard and meta.json reads). Run at this head (432393217+ this): - go test -count=1 -timeout 45m ./internal/usage: ok; with -race: ok - go test -count=1 -timeout 45m ./internal/gui: one red, TestRestartHandsTheGatewayOver (18 of 19 requests answered, EOF), the open #3451; alone -count=5 it passes. Load was ~45. - TestPrefetchEachMakeIsAReadersOwn: 9 makes (8 readers, the loop's own) and no race. Broken by hand: one make shared by every reader -> -race reports DATA RACE, red. - gofmt -l clean; GOOS=linux and GOOS=windows go vet -tags nogui ./internal/usage ok.
This commit is contained in:
@@ -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 ahead
|
||||
reads = 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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -770,30 +770,35 @@ func readRequestBlocks(idx *requestIndex, st requestState, p Period, now time.Ti
|
||||
sources, cfg, snapshot, days := st.sources, st.cfg, st.snapshot, st.days
|
||||
price := 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
|
||||
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
|
||||
}
|
||||
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
|
||||
}
|
||||
priceRow := pricers()
|
||||
since := p.Since(now)
|
||||
gatewaySince := since
|
||||
if !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,7 +843,27 @@ func readRequestBlocks(idx *requestIndex, st requestState, p Period, now time.Ti
|
||||
idx.fromDisk(want)
|
||||
}
|
||||
built := 0
|
||||
read := prefetchSources(sources, stale, readSource)
|
||||
// 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.SessionProvider = call.Upstream
|
||||
row := priceRow(r, "log")
|
||||
row.codexActor = codexActor(call)
|
||||
c.add(row, call.Msg, call.To, call.Error != "")
|
||||
}
|
||||
c.Bytes -= int64(32 * len(c.Strings))
|
||||
c.dict, c.shapeIDs = nil, nil // local chunks are immutable until the source changes
|
||||
return c.pack()
|
||||
}
|
||||
})
|
||||
for i, s := range sources {
|
||||
on[s.Path] = true
|
||||
if !since.IsZero() && s.Modified.Before(since) {
|
||||
@@ -846,23 +873,7 @@ func readRequestBlocks(idx *requestIndex, st requestState, p Period, now time.Ti
|
||||
// (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))}
|
||||
for _, call := range cs {
|
||||
r := logRecord(call)
|
||||
r.SessionAccount = resolver.resolve(call)
|
||||
r.SessionOfficialLogin = resolver.officialLogin(call, r.SessionAccount)
|
||||
r.SessionProvider = call.Upstream
|
||||
row := priceRow(r, "log")
|
||||
row.codexActor = codexActor(call)
|
||||
c.add(row, call.Msg, call.To, call.Error != "")
|
||||
}
|
||||
c.Bytes -= int64(32 * len(c.Strings))
|
||||
c.dict, c.shapeIDs = nil, nil // local chunks are immutable until the source changes
|
||||
c = c.pack()
|
||||
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.CallSourc
|
||||
order = 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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user