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.
67 lines
1.9 KiB
Go
67 lines
1.9 KiB
Go
package usage
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/catalog"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// Queries reading at once hold one priced copy of a gateway block between
|
|
// them, kept until the last is done: the GUI's requests at a cold start
|
|
// each held the whole history's, and the app doubled (#2278). A query of
|
|
// other prices or names prices its own.
|
|
func TestPricedBlocksSharedWhileHeld(t *testing.T) {
|
|
var raw rowChunk
|
|
at := time.Date(2026, 10, 28, 12, 0, 0, 0, time.UTC)
|
|
for i := 0; i < logBlockRows; i++ {
|
|
raw.add(Row{Record: Record{Time: at, Provider: "openai", Model: "gpt-5", Input: 100 + i, Output: 10}}, "", int64(i), false)
|
|
}
|
|
packed := raw.pack()
|
|
if packed == &raw {
|
|
t.Fatal("the block was not packed")
|
|
}
|
|
pricer := func(meta string) *blockPricer {
|
|
b := newBlockPricer(provider.Renames{}, nil, func(Record) *catalog.Price { return nil })
|
|
b.share(sha256.Sum256([]byte(meta)))
|
|
return b
|
|
}
|
|
held := func() int {
|
|
sharedPriced.Lock()
|
|
defer sharedPriced.Unlock()
|
|
n := 0
|
|
for k := range sharedPriced.blocks {
|
|
if k.block == packed {
|
|
n++
|
|
}
|
|
}
|
|
return n
|
|
}
|
|
a, b, other := pricer("m"), pricer("m"), pricer("n")
|
|
ca, cb, co := a.block(packed), b.block(packed), other.block(packed)
|
|
if ca == nil || ca != cb {
|
|
t.Fatalf("two queries of one meta priced %p and %p", ca, cb)
|
|
}
|
|
if co == ca {
|
|
t.Fatal("a query of other prices read another's block")
|
|
}
|
|
if ca.row(7).Input != 107 || co.row(7).Input != 107 {
|
|
t.Fatalf("rows read back %d, %d", ca.row(7).Input, co.row(7).Input)
|
|
}
|
|
a.release()
|
|
if held() != 2 || b.block(packed) != cb {
|
|
t.Fatalf("a block one query still holds was let go: %d held", held())
|
|
}
|
|
b.release()
|
|
b.release() // a second release lets nothing go twice
|
|
other.release()
|
|
if n := held(); n != 0 {
|
|
t.Fatalf("%d blocks held after every query was done", n)
|
|
}
|
|
if c := pricer("m").block(packed); c == ca {
|
|
t.Fatal("a block let go was handed out again")
|
|
}
|
|
}
|