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 + } +}