Commit Graph
3 Commits
Author SHA1 Message Date
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
yetone f8fa8147ba sessions: a page-asked first read uses half the cores, not twice them (#3238)
The index's cold scan with a page waiting started min(max(8, 2*NumCPU),
32) readers, as if reading waited on the disk. On an SSD decoding and
parsing is the work: on the Windows box (32 cores) a cold start over
6751 real Codex session files (15156 MB) with /api/sessions waiting
held 2154% CPU, the ~2000% the reporter saw, while other apps ran
beside it. Now max(2, NumCPU/2) readers; the quiet warm-up's NumCPU/4
slots are unchanged.

Run:
- TestAskedIndexReadsWithHalfTheCores: ok; with the old count put back
  by hand it fails ("the index read 28 files at once, want 2 to 7").
- go test -count=1 ./internal/sessions/: ok (64s).
- gofmt -l clean; go vet darwin, GOOS=linux and GOOS=windows -tags nogui
  clean for internal/sessions.

- On the Windows box (32 cores), a cold read of 14051 session files
  (~44 GB) with a page waiting, magpie built from this commit's parent
  against this one: peak CPU 1634% -> 1172% (about 900-1100% sustained),
  peak working set 1612 MB -> 1108 MB, ~22s -> ~27s to read them all.
2026-10-24 21:00:05 +08:00
yetone e044ea862f sessions: the warm-up at start reads with a quarter of the cores unless a page waits, and a long first read keeps what it read (#1665, laowangba233)
#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.
2026-10-12 02:56:01 +08:00