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.
69 lines
1.8 KiB
Go
69 lines
1.8 KiB
Go
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
|
|
}
|