usage, gui: Usage › Requests' All opens at once after magpie has been up a few seconds, and in half the time before (the owner) — Requests on All stayed a skeleton for 39 s on the owner's magpie right after it started: the first All parses every session file (12.3k here: 11k Codex sessions, 1.2k Claude Code projects' files), one after the other, each a wait on the disk. Those a query has to parse are now read 8 at a time ahead of the loop that takes them in order (prefetchSources), at most 32 waiting, so the history is never all held at once; and magpie reads the whole history in the background once it is up, right after the Sessions page's own warm-up and not beside it on the disk, so the page opens on the kept index. Tested in a sandbox HOME on a copy of the owner's real ~/.codex and ~/.claude history (magpie app, dev UI): the first All right at startup took 10.6 s before and 4.6–4.9 s now; opened 20 s after startup it took 10.0 s before and 0.01 s now; later Alls 0.3–0.4 s as before. TestPrefetchSources (each source handed back as its own, read in parallel, no more than the window ahead, every one read once, an unwanted one read when asked) is new. go vet, go test -race ./internal/usage, go build (darwin, GOOS=linux nogui, GOOS=windows) and go test -tags nogui ./... under a temp HOME pass.
This commit is contained in:
@@ -238,7 +238,9 @@ func statsFor(days int) sessions.Stats {
|
||||
}
|
||||
|
||||
// warmSessions reads every session file once magpie is up, so the Sessions
|
||||
// page opens on the kept index and not on a first read of them all.
|
||||
// page opens on the kept index and not on a first read of them all. Usage ›
|
||||
// Requests' whole history is read after it, not beside it on the disk: a
|
||||
// first All parsing 12k sessions kept that page a skeleton for 40 s.
|
||||
func warmSessions() {
|
||||
if testing.Testing() {
|
||||
return
|
||||
@@ -247,6 +249,7 @@ func warmSessions() {
|
||||
time.Sleep(3 * time.Second)
|
||||
statsFor(0)
|
||||
statsFor(30)
|
||||
usage.QueryPage(usage.All, usage.Filter{}, 0, 50)
|
||||
}()
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
package usage
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/yetone/magpie/internal/sessions"
|
||||
)
|
||||
|
||||
// The sources a query parses are read several at a time, each handed back
|
||||
// as its own, in any order asked; never more than a window of them waits to
|
||||
// be taken, and one not wanted is read when it is asked for.
|
||||
func TestPrefetchSources(t *testing.T) {
|
||||
var sources []sessions.CallSource
|
||||
for i := range 200 {
|
||||
sources = append(sources, sessions.CallSource{Path: fmt.Sprint(i)})
|
||||
}
|
||||
var mu sync.Mutex
|
||||
reads := map[string]int{}
|
||||
var busy, most atomic.Int32
|
||||
readSource := func(s sessions.CallSource) []sessions.Call {
|
||||
n := busy.Add(1)
|
||||
for {
|
||||
m := most.Load()
|
||||
if n <= m || most.CompareAndSwap(m, n) {
|
||||
break
|
||||
}
|
||||
}
|
||||
time.Sleep(time.Millisecond)
|
||||
busy.Add(-1)
|
||||
mu.Lock()
|
||||
reads[s.Path]++
|
||||
mu.Unlock()
|
||||
return []sessions.Call{{Model: s.Path}}
|
||||
}
|
||||
want := func(s sessions.CallSource) bool { return s.Path != "7" }
|
||||
read := prefetchSources(sources, want, readSource)
|
||||
for i := range sources {
|
||||
if i == 50 {
|
||||
// nothing is taken for a while: the readers stop at the window
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
mu.Lock()
|
||||
ahead := len(reads)
|
||||
mu.Unlock()
|
||||
if ahead > 50+32+8 {
|
||||
t.Fatalf("%d read with 50 taken", ahead)
|
||||
}
|
||||
}
|
||||
cs := read(i)
|
||||
if len(cs) != 1 || cs[0].Model != sources[i].Path {
|
||||
t.Fatalf("source %d: %+v", i, cs)
|
||||
}
|
||||
}
|
||||
if most.Load() < 2 {
|
||||
t.Fatalf("at most %d read at once", most.Load())
|
||||
}
|
||||
for _, s := range sources {
|
||||
if reads[s.Path] != 1 {
|
||||
t.Fatalf("%s read %d times", s.Path, reads[s.Path])
|
||||
}
|
||||
}
|
||||
|
||||
// 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)
|
||||
if len(reads) != 0 {
|
||||
t.Fatalf("read ahead: %v", reads)
|
||||
}
|
||||
if cs := read(2); cs[0].Model != "2" {
|
||||
t.Fatalf("%+v", cs)
|
||||
}
|
||||
}
|
||||
@@ -346,7 +346,14 @@ func queryPage(p Period, f Filter, offset, limit int, readSource func(sessions.C
|
||||
on := map[string]bool{}
|
||||
var chunks []*rowChunk
|
||||
var resolver *sessionResolver
|
||||
for _, s := range sources {
|
||||
read := prefetchSources(sources, func(s sessions.CallSource) bool {
|
||||
if !since.IsZero() && s.Modified.Before(since) {
|
||||
return false
|
||||
}
|
||||
c := idx.chunks[s.Path]
|
||||
return c == nil || c.Source.Size != s.Size || !c.Source.Modified.Equal(s.Modified)
|
||||
}, readSource)
|
||||
for i, s := range sources {
|
||||
on[s.Path] = true
|
||||
if !since.IsZero() && s.Modified.Before(since) {
|
||||
continue
|
||||
@@ -358,7 +365,7 @@ func queryPage(p Period, f Filter, offset, limit int, readSource func(sessions.C
|
||||
if resolver == nil {
|
||||
resolver = newSessionResolver([]sessions.Call{{}})
|
||||
}
|
||||
cs := readSource(s)
|
||||
cs := read(i)
|
||||
c = &rowChunk{Source: s, Rows: make([]packedRow, 0, len(cs))}
|
||||
for _, call := range cs {
|
||||
r := logRecord(call)
|
||||
@@ -1056,3 +1063,58 @@ func callerGroups(groups map[string]*Group) []Group {
|
||||
})
|
||||
return out
|
||||
}
|
||||
|
||||
// prefetchSources 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 {
|
||||
type slot struct {
|
||||
done chan struct{}
|
||||
calls []sessions.Call
|
||||
}
|
||||
slots := map[int]*slot{}
|
||||
var order []int
|
||||
for i, s := range sources {
|
||||
if want(s) {
|
||||
slots[i] = &slot{done: make(chan struct{})}
|
||||
order = append(order, i)
|
||||
}
|
||||
}
|
||||
if len(order) < 2 {
|
||||
return func(i int) []sessions.Call { return readSource(sources[i]) }
|
||||
}
|
||||
workers := min(8, len(order))
|
||||
window := make(chan struct{}, 4*workers)
|
||||
next := make(chan int)
|
||||
go func() {
|
||||
for _, i := range order {
|
||||
window <- struct{}{}
|
||||
next <- i
|
||||
}
|
||||
close(next)
|
||||
}()
|
||||
for range workers {
|
||||
go func() {
|
||||
for i := range next {
|
||||
s := slots[i]
|
||||
s.calls = readSource(sources[i])
|
||||
close(s.done)
|
||||
}
|
||||
}()
|
||||
}
|
||||
return func(i int) []sessions.Call {
|
||||
s := slots[i]
|
||||
if s == nil {
|
||||
return readSource(sources[i])
|
||||
}
|
||||
<-s.done
|
||||
<-window
|
||||
cs := s.calls
|
||||
s.calls = nil
|
||||
return cs
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user