From 5604c411a90896c5aa3e2df2807df4254ca64fd7 Mon Sep 17 00:00:00 2001 From: yetone Date: Mon, 5 Oct 2026 20:18:22 +0800 Subject: [PATCH] =?UTF-8?q?usage,=20gui:=20Usage=20=E2=80=BA=20Requests'?= =?UTF-8?q?=20All=20opens=20at=20once=20after=20magpie=20has=20been=20up?= =?UTF-8?q?=20a=20few=20seconds,=20and=20in=20half=20the=20time=20before?= =?UTF-8?q?=20(the=20owner)=20=E2=80=94=20Requests=20on=20All=20stayed=20a?= =?UTF-8?q?=20skeleton=20for=2039=20s=20on=20the=20owner's=20magpie=20righ?= =?UTF-8?q?t=20after=20it=20started:=20the=20first=20All=20parses=20every?= =?UTF-8?q?=20session=20file=20(12.3k=20here:=2011k=20Codex=20sessions,=20?= =?UTF-8?q?1.2k=20Claude=20Code=20projects'=20files),=20one=20after=20the?= =?UTF-8?q?=20other,=20each=20a=20wait=20on=20the=20disk.=20Those=20a=20qu?= =?UTF-8?q?ery=20has=20to=20parse=20are=20now=20read=208=20at=20a=20time?= =?UTF-8?q?=20ahead=20of=20the=20loop=20that=20takes=20them=20in=20order?= =?UTF-8?q?=20(prefetchSources),=20at=20most=2032=20waiting,=20so=20the=20?= =?UTF-8?q?history=20is=20never=20all=20held=20at=20once;=20and=20magpie?= =?UTF-8?q?=20reads=20the=20whole=20history=20in=20the=20background=20once?= =?UTF-8?q?=20it=20is=20up,=20right=20after=20the=20Sessions=20page's=20ow?= =?UTF-8?q?n=20warm-up=20and=20not=20beside=20it=20on=20the=20disk,=20so?= =?UTF-8?q?=20the=20page=20opens=20on=20the=20kept=20index.=20Tested=20in?= =?UTF-8?q?=20a=20sandbox=20HOME=20on=20a=20copy=20of=20the=20owner's=20re?= =?UTF-8?q?al=20~/.codex=20and=20~/.claude=20history=20(magpie=20app,=20de?= =?UTF-8?q?v=20UI):=20the=20first=20All=20right=20at=20startup=20took=2010?= =?UTF-8?q?.6=20s=20before=20and=204.6=E2=80=934.9=20s=20now;=20opened=202?= =?UTF-8?q?0=20s=20after=20startup=20it=20took=2010.0=20s=20before=20and?= =?UTF-8?q?=200.01=20s=20now;=20later=20Alls=200.3=E2=80=930.4=20s=20as=20?= =?UTF-8?q?before.=20TestPrefetchSources=20(each=20source=20handed=20back?= =?UTF-8?q?=20as=20its=20own,=20read=20in=20parallel,=20no=20more=20than?= =?UTF-8?q?=20the=20window=20ahead,=20every=20one=20read=20once,=20an=20un?= =?UTF-8?q?wanted=20one=20read=20when=20asked)=20is=20new.=20go=20vet,=20g?= =?UTF-8?q?o=20test=20-race=20./internal/usage,=20go=20build=20(darwin,=20?= =?UTF-8?q?GOOS=3Dlinux=20nogui,=20GOOS=3Dwindows)=20and=20go=20test=20-ta?= =?UTF-8?q?gs=20nogui=20./...=20under=20a=20temp=20HOME=20pass.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/gui/sessions.go | 5 ++- internal/usage/prefetch_test.go | 75 +++++++++++++++++++++++++++++++++ internal/usage/request_page.go | 66 ++++++++++++++++++++++++++++- 3 files changed, 143 insertions(+), 3 deletions(-) create mode 100644 internal/usage/prefetch_test.go diff --git a/internal/gui/sessions.go b/internal/gui/sessions.go index aa62995b6..0b2c6c32f 100644 --- a/internal/gui/sessions.go +++ b/internal/gui/sessions.go @@ -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) }() } diff --git a/internal/usage/prefetch_test.go b/internal/usage/prefetch_test.go new file mode 100644 index 000000000..363e362ec --- /dev/null +++ b/internal/usage/prefetch_test.go @@ -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) + } +} diff --git a/internal/usage/request_page.go b/internal/usage/request_page.go index 01d72c6e6..a61431ac2 100644 --- a/internal/usage/request_page.go +++ b/internal/usage/request_page.go @@ -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 + } +}