usage: a session file two queries read at once is made once (#2278)
At a cold start with the app open, the warm-up's request page and the GUI's heatmap each read every session file at the same time. Each one decoded and priced its own chunk of each file. That is twice the CPU, and the GUI's requests queued behind it. sharedBuild keys a build by the request state (root and meta, hashed) and by the file's path, size and time. A query that finds the same file already being made waits for that build, then takes its own shallow copy, because a query writes its chunk's serial, use and archive place. The rows and the archive are shared and only read. If the build panics, the waiter builds its own. Only QueryPage and the effort index share builds; queryPage's other callers build as before. blockPricer.share now takes the hashed state directly. Measured on mote with synthetic history at #2278's scale (25k Codex rollouts, 9k Claude sessions, a 115 MB usage.jsonl), with a cold `magpie web` and 10 GUI endpoints polled every 5 s for 150 s. Built on v0.1.1903 before the rebase: - slowest GUI response: 61 s on v0.1.1903, 15.3 s with this change - footprint peak: 591 MB on v0.1.1903, 609 MB with this change, so no memory gain; the peak is a transient spread over the page, heatmap, effort index and session stats (heap profile at 15 s: QueryPage 145 MB, HeatmapOf 87 MB, buildEffort 51 MB, StatsAndNative 43 MB) TestSharedBuildMakesAFileOnce checks several things: - a second query waits for the first one's build - each query gets its own copy, and the copies share one archive - a change to one copy doesn't show in the other - the map is empty afterwards - after a panicked build, the waiter builds its own With the join broken by hand (the lookup disabled), it failed with "a file made again while another query was making it". Run at the pushed tree (rebased ontoca499953e, afterb7b817116and84a6a2c42changed request_page.go), on mote: - go test -race -tags nogui ./internal/usage ./internal/sessions: ok - go vet -tags nogui ./internal/usage for linux, windows and darwin: ok Not run: the full suite, and the footprint run at the rebased tree.
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package usage
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"slices"
|
||||
"sync"
|
||||
|
||||
@@ -70,9 +69,10 @@ var sharedPriced = struct {
|
||||
blocks map[pricedKey]*pricedBlock
|
||||
}{blocks: map[pricedKey]*pricedBlock{}}
|
||||
|
||||
// share has b's blocks shared with the other queries of meta.
|
||||
func (b *blockPricer) share(meta string) {
|
||||
b.meta, b.shared = sha256.Sum256([]byte(meta)), true
|
||||
// share has b's blocks shared with the other queries of meta, a hash of
|
||||
// requestState's root and meta.
|
||||
func (b *blockPricer) share(meta [32]byte) {
|
||||
b.meta, b.shared = meta, true
|
||||
}
|
||||
|
||||
// release hands the rows of the blocks it priced back, once the query no
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package usage
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -24,7 +25,7 @@ func TestPricedBlocksSharedWhileHeld(t *testing.T) {
|
||||
}
|
||||
pricer := func(meta string) *blockPricer {
|
||||
b := newBlockPricer(provider.Renames{}, nil, func(Record) *catalog.Price { return nil })
|
||||
b.share(meta)
|
||||
b.share(sha256.Sum256([]byte(meta)))
|
||||
return b
|
||||
}
|
||||
held := func() int {
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
package usage
|
||||
|
||||
import "sync"
|
||||
|
||||
// chunkBuildKey is a session file as one query reads it: what prices and
|
||||
// names a call (requestState's root and meta, hashed), and the file's
|
||||
// path, size and time.
|
||||
type chunkBuildKey struct {
|
||||
meta [32]byte
|
||||
path string
|
||||
size int64
|
||||
modified int64
|
||||
}
|
||||
|
||||
type chunkBuild struct {
|
||||
done chan struct{}
|
||||
c *rowChunk // as made, never changed; nil when the make panicked
|
||||
}
|
||||
|
||||
// chunkBuilds are the session files' chunks being made now, by the
|
||||
// queries reading at once. At a cold start with the app open the
|
||||
// warm-up's page and the GUI's heatmap each read every session file
|
||||
// beside the other, and each held a chunk of each: twice the CPU, and
|
||||
// 76 MB of chunks in the making at the peak (#2278).
|
||||
var chunkBuilds = struct {
|
||||
sync.Mutex
|
||||
m map[chunkBuildKey]*chunkBuild
|
||||
}{m: map[chunkBuildKey]*chunkBuild{}}
|
||||
|
||||
// buildJoined, in tests, is told each time a query waits for another's
|
||||
// chunk.
|
||||
var buildJoined func()
|
||||
|
||||
// sharedBuild is the chunk of k that build makes, or one another query is
|
||||
// making of k now, once it is made. Each caller is handed a copy of its
|
||||
// own, since a query writes its chunk's serial, its use and where its
|
||||
// archive is; the rows and the archive are shared, and only read.
|
||||
func sharedBuild(k chunkBuildKey, build func() *rowChunk) *rowChunk {
|
||||
chunkBuilds.Lock()
|
||||
if b := chunkBuilds.m[k]; b != nil {
|
||||
chunkBuilds.Unlock()
|
||||
if buildJoined != nil {
|
||||
buildJoined()
|
||||
}
|
||||
<-b.done
|
||||
if b.c == nil {
|
||||
return build()
|
||||
}
|
||||
c := *b.c
|
||||
return &c
|
||||
}
|
||||
b := &chunkBuild{done: make(chan struct{})}
|
||||
chunkBuilds.m[k] = b
|
||||
chunkBuilds.Unlock()
|
||||
defer func() {
|
||||
chunkBuilds.Lock()
|
||||
delete(chunkBuilds.m, k)
|
||||
chunkBuilds.Unlock()
|
||||
close(b.done)
|
||||
}()
|
||||
made := build()
|
||||
if made == nil {
|
||||
return nil
|
||||
}
|
||||
b.c = made
|
||||
c := *made
|
||||
return &c
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package usage
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Queries reading one session file at once make one chunk of it, each
|
||||
// handed a copy of its own: at a cold start with the app open, the
|
||||
// warm-up's page and the heatmap each read every file (#2278). A make
|
||||
// that panics leaves the waiter to make its own.
|
||||
func TestSharedBuildMakesAFileOnce(t *testing.T) {
|
||||
t.Cleanup(func() { buildJoined = nil })
|
||||
joined := make(chan struct{}, 1)
|
||||
buildJoined = func() { joined <- struct{}{} }
|
||||
at := time.Date(2026, 10, 28, 12, 0, 0, 0, time.UTC)
|
||||
made := func() *rowChunk {
|
||||
var c rowChunk
|
||||
for i := 0; i < logBlockRows; i++ {
|
||||
c.add(Row{Record: Record{Time: at, Provider: "openai", Model: "gpt-5", Input: i}}, "", int64(i), false)
|
||||
}
|
||||
return c.pack()
|
||||
}
|
||||
k := chunkBuildKey{path: "/s/rollout.jsonl", size: 10, modified: 1}
|
||||
builds := 0
|
||||
inside, release := make(chan struct{}), make(chan struct{})
|
||||
first := make(chan *rowChunk)
|
||||
go func() {
|
||||
first <- sharedBuild(k, func() *rowChunk {
|
||||
builds++
|
||||
close(inside)
|
||||
<-release
|
||||
return made()
|
||||
})
|
||||
}()
|
||||
<-inside
|
||||
second := make(chan *rowChunk)
|
||||
go func() {
|
||||
second <- sharedBuild(k, func() *rowChunk { builds++; return made() })
|
||||
}()
|
||||
select { // the second waits for the first's chunk
|
||||
case <-joined:
|
||||
case <-second:
|
||||
t.Fatal("a file made again while another query was making it")
|
||||
}
|
||||
close(release)
|
||||
a, b := <-first, <-second
|
||||
if builds != 1 {
|
||||
t.Fatalf("one file made %d times", builds)
|
||||
}
|
||||
if a == b || len(a.Archive) == 0 || &a.Archive[0] != &b.Archive[0] {
|
||||
t.Fatalf("copies %p and %p of one archive (%d bytes)", a, b, len(a.Archive))
|
||||
}
|
||||
a.serial, a.Archive = 7, nil
|
||||
if b.serial == 7 || len(b.Archive) == 0 || b.unpack().row(3).Input != 3 {
|
||||
t.Fatal("one query's copy changed under another's")
|
||||
}
|
||||
chunkBuilds.Lock()
|
||||
n := len(chunkBuilds.m)
|
||||
chunkBuilds.Unlock()
|
||||
if n != 0 {
|
||||
t.Fatalf("%d builds left after both were done", n)
|
||||
}
|
||||
|
||||
// a make that panics: the one waiting makes its own
|
||||
inside, release = make(chan struct{}), make(chan struct{})
|
||||
go func() {
|
||||
defer func() { recover() }()
|
||||
sharedBuild(k, func() *rowChunk { close(inside); <-release; panic("read") })
|
||||
}()
|
||||
<-inside
|
||||
go func() { second <- sharedBuild(k, made) }()
|
||||
<-joined
|
||||
close(release)
|
||||
if c := <-second; c == nil || c.unpack().row(5).Input != 5 {
|
||||
t.Fatalf("after a panicked make, read %v", c)
|
||||
}
|
||||
}
|
||||
@@ -703,9 +703,19 @@ func hashCodexIdentities(h hash.Hash, ids map[string]sessions.CodexIdentity) {
|
||||
func requestRoot() string { return Path() + "|" + catalog.CachePath() }
|
||||
|
||||
func QueryPage(p Period, f Filter, offset, limit int) RequestPage {
|
||||
return queryPage(p, f, offset, limit, sessions.ReadCallSource)
|
||||
return queryPageOf(p, f, offset, limit, sessions.ReadCallSource, true)
|
||||
}
|
||||
func queryPage(p Period, f Filter, offset, limit int, readSource func(sessions.CallSource) []sessions.Call) (page RequestPage) {
|
||||
|
||||
// queryPage is QueryPage of the session files as readSource reads them,
|
||||
// its chunks its own.
|
||||
func queryPage(p Period, f Filter, offset, limit int, readSource func(sessions.CallSource) []sessions.Call) RequestPage {
|
||||
return queryPageOf(p, f, offset, limit, readSource, false)
|
||||
}
|
||||
|
||||
// queryPageOf is queryPage; share says readSource is the files' own
|
||||
// reader, so a chunk another query is making of a file is waited for
|
||||
// (sharedBuild).
|
||||
func queryPageOf(p Period, f Filter, offset, limit int, readSource func(sessions.CallSource) []sessions.Call, share bool) (page RequestPage) {
|
||||
if limit <= 0 {
|
||||
limit = 100
|
||||
}
|
||||
@@ -766,7 +776,7 @@ func queryPage(p Period, f Filter, offset, limit int, readSource func(sessions.C
|
||||
shared := idx
|
||||
idx = shared.private()
|
||||
shared.Unlock()
|
||||
b := readRequestBlocks(idx, st, p, now, readSource)
|
||||
b := readRequestBlocks(idx, st, p, now, readSource, share)
|
||||
defer b.priced.release() // on a panic too: a shared block is held until let go
|
||||
var names map[string]string
|
||||
if someShared {
|
||||
@@ -819,7 +829,7 @@ func codexEffortOfIndex(st requestState, p Period, now time.Time) map[string][]s
|
||||
memo := shared.pairs[p]
|
||||
idx := shared.private()
|
||||
shared.Unlock()
|
||||
b := readRequestBlocks(idx, st, p, now, sessions.ReadCallSource)
|
||||
b := readRequestBlocks(idx, st, p, now, sessions.ReadCallSource, true)
|
||||
defer b.priced.release()
|
||||
sums := effortSums{}
|
||||
ps, next := pairRowsMemo(p, now, b.gateways, b.chunks, b.others, st.codexIdentities, memo)
|
||||
@@ -849,8 +859,10 @@ type requestBlocks struct {
|
||||
|
||||
// readRequestBlocks reads the blocks of p at now into idx, a private copy
|
||||
// of the request index (private): a session file or another computer's
|
||||
// day is read again only when it changed since its chunk was made.
|
||||
func readRequestBlocks(idx *requestIndex, st requestState, p Period, now time.Time, readSource func(sessions.CallSource) []sessions.Call) requestBlocks {
|
||||
// day is read again only when it changed since its chunk was made. share
|
||||
// says readSource is sessions.ReadCallSource: a file another query is
|
||||
// making a chunk of now is waited for, not read beside it (sharedBuild).
|
||||
func readRequestBlocks(idx *requestIndex, st requestState, p Period, now time.Time, readSource func(sessions.CallSource) []sessions.Call, share bool) requestBlocks {
|
||||
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,
|
||||
@@ -895,7 +907,8 @@ func readRequestBlocks(idx *requestIndex, st requestState, p Period, now time.Ti
|
||||
var gateways []*rowChunk
|
||||
renames := provider.UsageRenames()
|
||||
priced := newBlockPricer(renames, cfg.ModelWires, price)
|
||||
priced.share(st.root + "|" + st.meta)
|
||||
meta := sha256.Sum256([]byte(st.root + "|" + st.meta))
|
||||
priced.share(meta)
|
||||
for _, packed := range snapshot.blocks {
|
||||
if !gatewaySince.IsZero() && packed.Latest.Before(gatewaySince) {
|
||||
continue
|
||||
@@ -935,7 +948,7 @@ func readRequestBlocks(idx *requestIndex, st requestState, p Period, now time.Ti
|
||||
// another, they were most of a cold build's wait (#3493).
|
||||
makeChunk := func() func(sessions.CallSource) *rowChunk {
|
||||
priceRow := pricers()
|
||||
return func(s sessions.CallSource) *rowChunk {
|
||||
build := func(s sessions.CallSource) *rowChunk {
|
||||
cs := readSource(s)
|
||||
c := &rowChunk{Source: s, Rows: make([]packedRow, 0, len(cs))}
|
||||
for _, call := range cs {
|
||||
@@ -951,6 +964,12 @@ func readRequestBlocks(idx *requestIndex, st requestState, p Period, now time.Ti
|
||||
c.dict, c.shapeIDs = nil, nil // local chunks are immutable until the source changes
|
||||
return c.pack()
|
||||
}
|
||||
if !share {
|
||||
return build
|
||||
}
|
||||
return func(s sessions.CallSource) *rowChunk {
|
||||
return sharedBuild(chunkBuildKey{meta, s.Path, s.Size, s.Modified.UnixNano()}, func() *rowChunk { return build(s) })
|
||||
}
|
||||
}
|
||||
read := prefetchEach(sources, stale, makeChunk)
|
||||
for i, s := range sources {
|
||||
|
||||
@@ -345,7 +345,7 @@ func TestRequestBlocksKeepOneChunkASource(t *testing.T) {
|
||||
shared.adopt(st)
|
||||
read := func() (*rowChunk, *requestIndex) {
|
||||
idx := shared.private()
|
||||
b := readRequestBlocks(idx, st, All, now, sessions.ReadCallSource)
|
||||
b := readRequestBlocks(idx, st, All, now, sessions.ReadCallSource, true)
|
||||
defer b.priced.release()
|
||||
if len(b.chunks) != 1 {
|
||||
t.Fatalf("%d chunks, want 1", len(b.chunks))
|
||||
|
||||
Reference in New Issue
Block a user