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.
79 lines
2.3 KiB
Go
79 lines
2.3 KiB
Go
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)
|
|
}
|
|
}
|