#1665: magpie pegs the CPU on every start (Windows Task Manager showed 95.8%). Three seconds after start, warmSessions indexes every Claude/Codex session file and reads every file's calls for usage. refresh() ran min(max(8, 2*NumCPU), 32) parsers at once and usage's prefetch 8 more, so the background warm-up took every core while nobody was looking at the result. Two goals, in one commit because they touch the same loop in refresh(): 1. Quiet warm-up (quiet.go). gui's warmSessions runs inside sessions.Warm. While a warm-up runs and no request waits, every parse in refresh() and every readCalls takes one of max(1, NumCPU/4) slots. A request to /api/sessions* or /api/usage* (gui's held middleware) calls sessions.Ask(), which lifts the limit for as long as it runs, so a page the user opened isn't slowed down. A waiting reader re-checks every 50ms, so Ask takes effect mid-read. 2. Checkpoints. refresh() saved sessions.json only once all files were parsed. Quitting magpie before that (a slow machine, or the user closing it because the CPU is pegged) kept nothing, and the next start parsed every file again. Now every 5s (checkpointEvery) the files done so far are published and saved, if the cache generation still matches. Measured on a Mac (14 cores) with a sandbox HOME holding 8.9 GB of real sessions (copied read-only), sessions.json and calls-v11 removed, sampling ps every 2s: - cold start before: 41.2 CPU-s, peak 580-600%, done ~14s - cold start after: 34.7 CPU-s, peak ~300-360%, done ~16s - quit mid-index, before: quit at 6s (13.5 CPU-s spent) left no sessions.json and no calls files; the next start spent 41 CPU-s again. - quit mid-index, after: quit at 12s left a 1.3 MB sessions.json (of 3.0 MB); the next start spent 26.6 CPU-s instead of 34.7. Tests (each red with its part undone, script-checked: refresh's slot replaced by a no-op, readCalls' slot removed, the checkpoint block removed): - TestWarmReadsOnlyInItsSlots: with every slot held, neither the index nor the calls read anything for 300ms; Ask() lets both finish. - TestReadSlotOnlyWhileWarming: outside Warm, or while asked, reads don't wait. - TestLongReadKeepsWhatItRead: a read held after its 3rd file has saved sessions.json with fewer files than it has to do. Run: go test -tags nogui ./internal/sessions ./internal/gui ./internal/library ./internal/usage; gofmt -l; GOOS=linux and GOOS=windows go vet of sessions, gui, library; full go test ./... before push. Not tried: the reporter's machine or data volume; a Windows re-measure.
127 lines
3.3 KiB
Go
127 lines
3.3 KiB
Go
package sessions
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"sync"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// moreSessions adds n Claude Code sessions to setup's.
|
|
func moreSessions(t *testing.T, claude string, n int) {
|
|
t.Helper()
|
|
dir := filepath.Join(claude, "projects", "-work-many")
|
|
os.MkdirAll(dir, 0o755)
|
|
for i := range n {
|
|
p := filepath.Join(dir, fmt.Sprintf("33333333-2222-3333-4444-%012d.jsonl", i))
|
|
os.WriteFile(p, []byte(ccMsg(fmt.Sprintf("m%d", i), "2026-09-21T10:00:00.000Z", 100, 10)), 0o644)
|
|
}
|
|
}
|
|
|
|
// holdQuietSlots takes every one of the warm-up's reading slots, and gives
|
|
// them back when the test ends.
|
|
func holdQuietSlots(t *testing.T) {
|
|
for range cap(quietSlots) {
|
|
quietSlots <- struct{}{}
|
|
}
|
|
t.Cleanup(func() {
|
|
for len(quietSlots) > 0 {
|
|
<-quietSlots
|
|
}
|
|
})
|
|
}
|
|
|
|
// The warm-up at start reads every changed file before anyone asks for
|
|
// them; after an update that reads them all again it held every core
|
|
// (#1665). It reads only with the warm-up's few slots: with all of them
|
|
// taken, neither the index (Stats) nor the calls (ReadCallSource) read a
|
|
// file, and a page asking has them read at once.
|
|
func TestWarmReadsOnlyInItsSlots(t *testing.T) {
|
|
inZone(t, 0)
|
|
claude, _ := setup(t)
|
|
moreSessions(t, claude, 12)
|
|
holdQuietSlots(t)
|
|
warm := func(what string, read func()) {
|
|
t.Helper()
|
|
readPeak.Store(0)
|
|
done := make(chan struct{})
|
|
go Warm(func() { read(); close(done) })
|
|
time.Sleep(300 * time.Millisecond)
|
|
if n := readPeak.Load(); n != 0 {
|
|
t.Fatalf("%s: the warm-up read %d files at once with no slot free", what, n)
|
|
}
|
|
// a page asks: read at full speed, slots or not
|
|
asked := Ask()
|
|
defer asked()
|
|
select {
|
|
case <-done:
|
|
case <-time.After(20 * time.Second):
|
|
t.Fatalf("%s: a page asking didn't have the warm-up's reading go on", what)
|
|
}
|
|
if readPeak.Load() == 0 {
|
|
t.Fatalf("%s: nothing read", what)
|
|
}
|
|
}
|
|
warm("the index", func() { statsAt(0, statsNow) })
|
|
if got := len(List(0)); got < 12 {
|
|
t.Fatalf("%d sessions listed", got)
|
|
}
|
|
warm("the calls", func() {
|
|
for _, s := range CallSources() {
|
|
ReadCallSource(s)
|
|
}
|
|
})
|
|
}
|
|
|
|
// Outside the warm-up the slots don't hold anything up.
|
|
func TestReadSlotOnlyWhileWarming(t *testing.T) {
|
|
holdQuietSlots(t)
|
|
got := make(chan struct{})
|
|
go func() { readSlot()(); close(got) }()
|
|
select {
|
|
case <-got:
|
|
case <-time.After(10 * time.Second):
|
|
t.Fatal("a read outside the warm-up waited for a slot")
|
|
}
|
|
}
|
|
|
|
// A long first read keeps what it has read on disk as it goes: a magpie
|
|
// quit before the end (its CPU full on a slow disk, #1665) kept nothing of
|
|
// a minutes-long read, so the next start read it all again. The files after
|
|
// the first three wait until a save is written with files still to read.
|
|
func TestLongReadKeepsWhatItRead(t *testing.T) {
|
|
inZone(t, 0)
|
|
claude, _ := setup(t)
|
|
moreSessions(t, claude, 12)
|
|
old := checkpointEvery
|
|
checkpointEvery = 5 * time.Millisecond
|
|
early := make(chan struct{})
|
|
var once sync.Once
|
|
saveHook = func() {
|
|
if progress.files.Load() < progress.todo.Load() {
|
|
once.Do(func() { close(early) })
|
|
}
|
|
}
|
|
var parsed atomic.Int32
|
|
parseHook = func() {
|
|
if parsed.Add(1) > 3 {
|
|
select {
|
|
case <-early:
|
|
case <-time.After(5 * time.Second):
|
|
}
|
|
}
|
|
}
|
|
t.Cleanup(func() { checkpointEvery, saveHook, parseHook = old, nil, nil })
|
|
|
|
statsAt(0, statsNow)
|
|
Saved()
|
|
select {
|
|
case <-early:
|
|
default:
|
|
t.Fatal("nothing kept on disk until every file was read")
|
|
}
|
|
}
|