#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.
71 lines
2.0 KiB
Go
71 lines
2.0 KiB
Go
package sessions
|
|
|
|
import (
|
|
"runtime"
|
|
"sync"
|
|
"sync/atomic"
|
|
"time"
|
|
)
|
|
|
|
// The warm-up at start (Warm) reads every changed session file before any
|
|
// page asks for them. After an update that changes how a file is read, that
|
|
// is every file again: 8.9 GB of Claude Code and Codex sessions took ~40 s
|
|
// of CPU, and with twice as many readers as cores it held every core for
|
|
// the first ~15 s of magpie's start (#1665, laowangba233: the CPU full on
|
|
// each start). Nobody waits on the warm-up, so it reads with a quarter of
|
|
// the cores; a page that asks for sessions meanwhile (Ask) has the reading
|
|
// go at full speed again, as it then waits on it.
|
|
|
|
var (
|
|
warming, asking atomic.Int32
|
|
quietSlots = make(chan struct{}, quietReaders())
|
|
)
|
|
|
|
// quietReaders is how many files the warm-up reads at once.
|
|
func quietReaders() int { return max(1, runtime.NumCPU()/4) }
|
|
|
|
// Warm runs fn, the reads nobody waits on yet, with few readers.
|
|
func Warm(fn func()) {
|
|
warming.Add(1)
|
|
defer warming.Add(-1)
|
|
fn()
|
|
}
|
|
|
|
// Ask marks a page waiting on sessions until done is called: reading goes
|
|
// at full speed while it waits.
|
|
func Ask() (done func()) {
|
|
asking.Add(1)
|
|
return sync.OnceFunc(func() { asking.Add(-1) })
|
|
}
|
|
|
|
func quiet() bool { return warming.Load() > 0 && asking.Load() == 0 }
|
|
|
|
// readSlot waits, while only the warm-up reads, for one of the warm-up's
|
|
// few reading slots; release gives it back. A page asking meanwhile lets
|
|
// the reader go at once.
|
|
func readSlot() (release func()) {
|
|
if !quiet() {
|
|
return func() {}
|
|
}
|
|
t := time.NewTicker(50 * time.Millisecond)
|
|
defer t.Stop()
|
|
for quiet() {
|
|
select {
|
|
case quietSlots <- struct{}{}:
|
|
return sync.OnceFunc(func() { <-quietSlots })
|
|
case <-t.C:
|
|
}
|
|
}
|
|
return func() {}
|
|
}
|
|
|
|
// reading and readPeak count files being read at once, for a test.
|
|
var reading, readPeak atomic.Int32
|
|
|
|
func readBegin() (end func()) {
|
|
n := reading.Add(1)
|
|
for p := readPeak.Load(); n > p && !readPeak.CompareAndSwap(p, n); p = readPeak.Load() {
|
|
}
|
|
return func() { reading.Add(-1) }
|
|
}
|