Files
magpie/internal/sessions/quiet.go
T
yetone 04439c8592 sessions: the warm-up lists the Codex rollouts once for all its reads (#3493)
The warm-up at start ran three reads within about two seconds: the
stats of every day, the stats of 30 days and the Requests page. Each
listed every Codex rollout again through codexFiles, about 12k lstat()s
and ~47 MB allocated a time. codexFiles now keeps its listing while
only the warm-up reads (quiet: inside Warm, no page asking) and for 10s
at most, the age statsFor's memo already answers with. Each caller gets
its own copy. A page asking meanwhile, and every read outside the
warm-up, lists fresh, and Warm drops the kept listing when it ends.

Measured on the owner's real sessions (sbpairs: Claude Code and Codex
history, warm index), the gui warm-up harness inside sessions.Warm, run
alternately with the parent b686a8dfe, three runs each:
  allocated through step 3: 990/826/811 MB -> 751/732/734 MB
  step 1 (stats of 30 days): 436/134/114 ms -> 65/57/57 ms
  peak live heap: 170/163/160 MB -> 157/153/153 MB
  phys_footprint_peak: 259/242/234 MB -> 233/250/250 MB (no change)
Step 2 and the whole warm-up time are within noise.

Run at this tree (b686a8dfe + this change):
  go test -count=1 -timeout 45m ./internal/sessions/  ok
  go test -count=1 -timeout 45m ./internal/usage/  one red,
    TestUsageReadAfterAppendHashesNothingOnAFineClock/whole-second_stamp,
    which is #3511/#3551; alone -count=5 it passes. That path never
    enters Warm, so it never takes the kept listing.
  go test -count=1 -run 'Warm|Session' ./internal/gui/  ok
  GOOS=linux and GOOS=windows go vet -tags nogui ./internal/sessions/, gofmt -l: clean
Broken by hand: codexFiles always listing fresh makes
TestWarmUpListsRolloutsOnce red ("warm-up listed again"); handing back the
kept slice itself instead of a copy makes it red ("a caller's change
reached the kept listing").
Not tried: Windows (the box is unreachable over ssh).
2026-10-25 15:29:58 +08:00

82 lines
2.5 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) }
// scanReaders is how many files the index reads at once with a page
// waiting. It was twice the cores, up to 32, as if the reading waited on
// the disk; it doesn't on an SSD, decoding and parsing is the work, and a
// first read of 15 GB of Codex sessions held 22 of 32 cores (2154%) while
// the page waited, the machine's other apps slowed beside it (#3238). Half
// the cores leaves the other half to them.
func scanReaders() int { return max(2, runtime.NumCPU()/2) }
// Warm runs fn, the reads nobody waits on yet, with few readers.
func Warm(fn func()) {
warming.Add(1)
defer func() {
warming.Add(-1)
forgetWarmListing()
}()
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) }
}