Commit Graph
11 Commits
Author SHA1 Message Date
yetone e94fb18b8f usage: a page's cells and the heatmap's days read strings where they were decoded (#2278)
partsOf, cellsBefore and heatDaysOf read each chunk through a chunkRows
that copied the chunk's whole string section first. What they keep is
unique's handles (a cell), a date made by Format and numbers (a day), so
the copy was thrown away chunk by chunk. They now read through
lentRows(), as newest and the Codex readers already do.

Run on mote:
- go test ./internal/usage/ -count=1 -timeout 45m: ok (40s).
- -race -count=3 on TestPartsReadStringsWhereDecoded,
  TestRowBufferKeptPastCollections, TestRequestCells*, *Heat*, *PageSplit*: ok.
- TestPartsReadStringsWhereDecoded (new): 8 packed chunks, 1.1MB of
  strings. With the three sites put back to `var rows chunkRows` by sed
  it is red: partsOf 1,188,672 bytes, cellsBefore 1,197,568, heatDaysOf
  1,187,616. Green with the change; the cells' names and the days' counts
  are right after the buffer was decoded over again.
- Real history copy (66GB, ~12k session files), magpie web in a sandbox
  HOME, heap profile 40s after start, two rounds each:
  unpackReusing 183MB / 174MB before, 106MB / 113MB after;
  partsOf 76MB / 78MB before, 55MB / 56MB after.
  A warm poll of every page reads cached parts and is unchanged.

Not tried: the Windows and Linux boxes (no OS-specific code).
2026-10-31 20:48:05 +08:00
yetone 4023a2e999 usage: a page's newest rows are kept on each chunk's part, not decoded again for every page (#2278)
A Usage page read again after a call picked its newest rows by decoding
the few newest chunks again, then decoded them once more for the rows
themselves (rowsOf). Under an agent at work that is the long session's
own file among them (57k calls): in the writer bench (a call every 3s,
the GUI's 10 endpoints polled) pairedRows.newest was 392 MB of 90s.

newest now keeps what a chunk gave on the chunk's part: its newest rows
the filter keeps, up to take, each with its row (strings copied, as the
chunk is decoded lent), and whether that was all of them. The part is
the cell cache's: its chunk serial and pairing sig, under the page's
period and filter, dropped with the rest when the clears, names or
prices change (meta). A page that wants more rows than the chunk gave
reads it again. The page's rows come from newest, and rowsOf is not
called for them.

Run at the pushed head, on mote:
- TestNewestKeptOnPart: pages of take 2, 2, 1, 4, 4, 6 decode 1, 0, 0,
  2, 0, 0 chunks with the same refs and rows; new parts decode again;
  rows read stay as they were after the chunks are decoded again over
  the lent block. Broken by hand, red each way: the kept rows never
  used; the take check dropped; the lent strings not copied.
- go test ./internal/usage/ ./internal/usageapi/: ok
- go vet -tags nogui, GOOS linux, windows, darwin: ok
- Writer bench, second start of each binary, 90s, load ~25:
  newest 392 MB and rowsOf 88 MB (v0.1.2190) -> 6 MB and none.
  Everything allocated: 3698 MB with only the refs kept -> 2742 MB.
  fp_peak 312 -> 333 MB, late GCs 46 -> 26.
  Not changed: the decode buffers are a sync.Pool, emptied at each GC,
  so the big chunk's buffer is now grown by matchedBlocks (18 -> 553 MB)
  where newest and rowsOf grew it before. That is the next change.
2026-10-30 18:17:09 +08:00
yetone c7171a2561 usage: a page's cells before its last days are kept in page-v1.json, so a start pairs only the recent chunks for All (#2278)
A start's first page of All (the warm-up's, and the Usage page's own)
paired the whole history for the page's cells, every gateway call with
its session row: ~85 MB of a ~316 MB heap at 18s after the heatmap's days
and Codex's effort were kept (v0.1.2172). The cells of the rows before
cut (local midnight two days back) are now kept as heatSplit keeps its
days, in memory and in page-v1.json beside the sessions cache, under a
key from heatSettledKey plus the page's until, names and hours. A start
pairs only the chunks since the edge. A page whose rows are older than
cut (an offset past the recent rows) falls back to the full pairing.

Real history on mote (~/r2278, a copy of the owner's), 120s each,
three starts in a row:
  v0.1.2165       fp_peak 451-471 MB, peaklive 326-364 MB
  this, miss      fp_peak 587 MB, peaklive 408 MB (a new binary's first start)
  this, hit       fp_peak 361/366 MB, peaklive 265 MB, gcs 150 (was ~150)
page-v1.json is ~240 KB.
The output is the same: /api/usage/requests for period all, 7d, 30d,
all with offset/limit and all filtered to codex, from a start that hit
page-v1.json and from a cold one (file moved away): 0 differences, each
float within 1e-6, on two hit runs.

Test: TestPageSettledFromDisk (first page pairs once and writes the file;
after a restart fewer chunks are paired and the page is equal; an older
offset falls back and equals the cold page; a changed settled file pairs
everything, then hits again). Broken by hand (pageSplit always false):
it goes red at "first page: pairings []".

Run at this head (rebased on ace8ee1e1), on mote:
  go test -count=1 ./internal/usage ./internal/usageapi: ok, ok
  GOOS=linux/windows/darwin go vet -tags nogui ./internal/usage ./internal/usageapi: ok
Not run: the full suite. TestCoveredWindowStaysUp (internal/gui) is red
on mote even alone, "signal: killed", the AppKit hang under #4125; this
change touches internal/usage only.
2026-10-30 15:44:59 +08:00
yetone 8d4c07ffed usage: the heatmap's settled days are kept on disk, so a start pairs only the last days (#2278)
The heatmap's days before the cut (two days ago) came from heatOld, a
full pairing of the whole history kept in memory. A started magpie made
it again on its first heatmap, and again every heatOldFresh (10 min):
on the 52G Codex + 13G Claude bench history that was heatSplit's 88 MB
of the cold start's ~325 MB in-use heap at 18 s.

The days are now kept in heat-v1.json beside the sessions cache, under
a key (heatSettledKey) naming what they were counted of: the request
code and the index's root and meta (prices, names), the cut, since,
zone, filter, the recent chunks' rows before the cut, the Codex thread
identities, and each settled chunk (a session file's by path, size and
mtime, like requests-v1.bin; any other chunk by its rows). The file is
written through a temp file renamed over the last. A key that can't be
named (a chunk with no serial) pairs as before; after heatOldFresh a
same-named key keeps the days without pairing again.

Measured on mote, ~/r2278/full, cold start + the page's 10 endpoints
polled every 5 s:
- heap at 18 s: v0.1.2149 324.6 MB in use, heatSplit 88.3 MB,
  HeatmapOf 135.8 MB; this 310.1 MB, no heatSplit, HeatmapOf 44.1 MB.
- /api/usage/heatmap at 40 s: byte-for-byte the same as v0.1.2149's.
- gcpeak 120 s, 3 rounds each, alternated (mote under other load):
  live 360/393/414 vs 378/404/387 MB, gcs 148/174/168 vs 201/209/160.
  The peak moves to CodexModelEffort's own full pairing (91 MB), next.
- First start, no file yet: pairs as before and writes 7.9 KB.

Tests: TestHeatmapAfterACallPairsOnlyTheLastDays now removes the file
for its cold oracle, and past heatOldFresh with nothing changed pairs
less than all, then a new settled file pairs all and equals cold.
TestHeatmapSettledDaysFromDisk: restarted with memory cleared pairs
less than all and gives the same heatmap; another filter, a rewritten
settled file, and no file each pair all.

Run at this tree (mote): go test ./internal/usage ok (71s);
go test -tags nogui ./internal/gui -run 'Heat|Usage' ok, usageapi ok;
go vet -tags nogui ./internal/usage/... ./internal/gui/... on linux,
windows, darwin ok; gofmt -l clean. Rebased onto e8d42c02d, then cae785a37 (no
internal/usage change in either); at e8d42c02d go test ./internal/usage ok
(100s) and linux vet ok again. Not run: the full suite.
2026-10-30 14:19:41 +08:00
yetone fddd7ff08f usage: pairing keeps its rows as bits and its groups by sums, and lets its pairs go (#2278)
A cold start pairs the whole history, and pairing held its row sets as
map[rowRef]bool, ~40 bytes a row, several sets of most rows at once.
They are now refSet: a bitset a chunk, a bit a row. matchScratch's
groupOf was keyed by the whole matchKey (a session string and its
tokens); it is keyed by a 128-bit maphash sum of them now, and the
sessions map is filled when a group is made. pairedRows' pairs and
compactions maps were filled and never read; they are gone.

Measured on mote, a copy of the real full history (52G Codex, 13G
Claude), GODEBUG=gctrace=1, 120s of /api polls, 3 rounds each,
alternating, v0.1.2143 (807048470) against this change:
  peak live heap  459 -> 439 MB (512/426/440 vs 470/433/413)
  heap at GC      543 -> 517 MB
  footprint peak  677 -> 641 MB
The inuse profile at the peak shows groupOf 28.7 -> 11.8 MB and the
matched set gone from matchedBlocks. This is a small part of the peak;
the rest is the full re-pair itself, which is next.

Run at this tree (rebased onto dca3e2d71 from 807048470), on mote:
go vet ./internal/usage; GOOS=linux|windows|darwin go vet -tags nogui
./... all ok; go test -count=1 ./internal/usage ok (30.1s), with the
new TestRefSet and TestMatchKeySum (no allocation).
Not run: the full suite, the GUI suite.
2026-10-30 13:42:11 +08:00
yetone d7e9c37454 usage: a call pairs again only the heatmap's last days, not its year (#2278)
Each call through magpie changes the gateway's newest block, so the
heatmap's 30 s reuse missed and HeatmapOf paired a year of gateway rows
against every session file again: 15% of magpie's CPU on real history
(HeatmapOf -> pairRows 5.2 s of 32.7 s sampled), ~2.3 s a refresh, and a
large share of the allocation the GC marks.

heatSplit keeps the days before cut (00:00 two days ago) from the last
full pairing, with the serials of the chunks that can't reach past cut
(newest row more than heatReach = 48 h before it). A call then pairs only
the recent chunks, from cut on (pairRowsIn), and adds their days to the
kept ones. Every chunk is paired again when:
- a settled chunk changed or went (its serials differ), or
- the recent chunks' rows before cut changed: a session file copied in or
  a rollout forked from an old one holds calls of then (heatBefore, a
  count, time and token sum of those rows), or
- cut moved (a new day), or the kept pairing is heatOldFresh (10 min) old.
heatReach covers a call paired across cut: a full gateway block whose
newest call is before cut is still paired with the session row after it.

TestHeatmapAfterACallAddsUpOnlyWhatChanged's gateway now has a call of
the last days before its first build, as a gateway in use does: with
only a call of 40 days ago its block went from settled to recent at the
call, which is a full pairing by design.

Run on mote, heat2278 at this head (based on fb5959553, rebased on
b2d005bf3, which changes internal/gateway and nothing under usage):
- GOOS=linux/windows/darwin go vet -tags nogui ./internal/usage/...
  ./internal/usageapi/... ./internal/gui/...: ok
- go test -count=1 -timeout 45m ./internal/usage/... ./internal/usageapi/...: ok,
  and again with vet after the rebase
- ./internal/gui/...: TestMainWindowCorners/macOS_26_SDK red, #3949
  (mote's macOS 27; nothing here reaches it); every other test ok
- broken by hand, TestHeatmapAfterACallPairsOnlyTheLastDays goes red:
  recent days after cut+1 dropped; heatReach 0 (the cross-cut gateway
  block); the heatBefore check removed (both the rewritten mixed file and
  the forked file, each alone)

Real history on mote (the owner's usage.jsonl and session files copied
into a sandbox HOME, GUI polled every 5 s, the gateway fed 4 calls/s,
load ~30 from other sessions), v0.1.1956 build vs this one:
- /api/usage/heatmap mean 2.35 s -> 1.53 s; all polls 0.93 s -> 0.74 s
- HeatmapOf 0.89 s of 28.35 s sampled (3.1%), heatSplit itself 0.25 s,
  from 15%
- average CPU after the feed starts 61.2% -> 59.0%; peak RSS 1103 -> 1065 MiB
What is left of the heatmap's wait is readRequestState and the pages it
shares a lock with, not its pairing.
2026-10-29 05:04:50 +08:00
yetone b69f8e96af usage: a call through magpie adds up the heatmap only for the chunks it changed (#2278)
A call through magpie changes the gateway's log, so the heatmap's kept
page (heatFresh) is no use and the Requests tab's refresh read every
row of the year again: unpacked each session file's chunk and summed its
days. While an agent worked through magpie that was every refresh.

The heatmap now keeps each chunk's day sums (chunkPart.days, in
requestIndex.heatParts, apart from the pages' cells so neither pushes
the other's out), under the same pairing signature the cells use, and
adds up again only a chunk whose rows or pairing changed. A call is
still counted at once. pairedRows.visit had no other caller and is gone.

Measured on mote at #2278's scale (synthetic HOME: 15 GB of Codex
rollouts, 1.9 GB of Claude sessions, 240k-row usage.jsonl), the GUI's
polls every 5s and 4 gateway records appended a second, against
v0.1.1920:
- CPU of a 60s profile while appending: 57.9s -> 42.1s;
  HeatmapOf 11.5s -> 6.7s, of it the visit 4.8s -> ~0
- /api/usage/heatmap mean 2.79s -> 1.69s; process CPU average
  107% -> 83%
- peak footprint unchanged (579 MB, 607 MB this run: noise; what is
  left is pairRows' pairing of the year, next)

Test: TestHeatmapAfterACallAddsUpOnlyWhatChanged (8 session files and
a gateway call: after one more call, at most 1 chunk is added up again,
the call is counted, and the sums equal a cold build). Broken by hand
(old sums never taken): red, "a call added up 9 chunks again".

Run at the pushed head (rebased on fc6f1d5e8, no internal/usage change
in between, rerun after): go test ./internal/usage ok; GOOS=linux,
windows, darwin go vet -tags nogui ./internal/usage ./internal/gui ok;
go test ./internal/gui before the rebase: ok except
TestMainWindowCorners, #3949 (macOS 27 on mote). Not run: the whole
suite.
2026-10-28 23:44:37 +08:00
yetone 0dc4cec85d usage: a row's Codex thread is kept as a 4-byte place, not a string (#2278)
Every row of a chunk had a 16-byte string header for its Codex
thread. A day's rows are a few hundred threads, one after another. At
the scale of #2278 (34k session files, 1M Codex rows), these headers
were the largest single holder in the heap, 24.5 MB of 184 MB live.
codexActors now keeps a []uint32 into the chunk's thread names. The
row before's thread is checked first, then the names. A clone shares
the names clipped, so adding a name to one never writes into the
other. The disk format is unchanged: each row's thread is still
written as a string.

Measured on mote, at 7e58591dc's parent with this change. The history
is synthetic: 25,000 Codex rollouts and 9,000 Claude Code sessions, 40
turns each (13 GB), plus a 115 MB usage.jsonl of 240k gateway calls on
the newest 3,000 of each. Cold `magpie web --no-open` in a sandbox
HOME, with `magpie heap` taken at 130 s:
- main: live heap 195.2 MB (both runs), physical footprint peak 389 to
  417 MB.
- this change: live heap 175.6 MB, physical footprint peak 366 MB.
The reporter's 0.1.1318 peaked at 2.85 GB RSS on such a history.

Run at the pushed head (rebased onto 7e58591dc, rerun after):
- GOOS=linux, windows and darwin go vet -tags nogui ./internal/usage/...
  ./internal/sessions/... ./internal/gateway ./internal/cli: ok.
- go test ./internal/usage/... ./internal/sessions/...: ok.
TestCodexActorsTakeFourBytesARow: 100k rows of 200 threads hold at
most 5 bytes a row, read back, and a clone keeps its own rows. Broken
by hand (no lookup of a name already held) it goes red.
Not run before the push: the full suite. It runs next.
2026-10-28 20:43:10 +08:00
yetone ba301e98e2 usage: a page's passes over the chunks decode into buffers kept between them (#3493)
Each pass of a Requests page over every chunk (foldCodexRows, the
response pairs, visibleLocal, matchedBlocks, visit, partsOf, newest,
rowsOf) started a chunkRows with no buffers and grew its rows, strings,
shapes and decode buffer from nothing. A warm start's All page and
Codex's effort after it read the same rows pass after pass, ~30 MB a
pass. chunkRows now takes a rowBuffer from a pool, unpacks into it
(unpackReusing gets a dst chunk too, so the open chunk isn't allocated
each block), and gives it back on release, its strings cleared so no
row text is held.

Measured on the real data copy (sbpairs: 12k Codex rollouts and the
Claude projects), warm-up harness TestZZWarm, each binary run 3 times
in a row and the first run dropped (the chunk file is named by this
package's source, so alternating binaries rebuilds it):
  step 2 (QueryPage All) allocated   835 MB -> 666 MB
  allocated through step 3 (effort)  994 MB -> 820 MB
  unpackReusing in step 2            163 MB -> 34 MB
  peak footprint                     ~240 MB, unchanged

Run at this head (base defbb1a15): go test ./internal/usage
./internal/sessions ./internal/gui ok; -race -count=3 of the chunkRows,
Pack and Archive tests ok; GOOS=linux and windows go vet -tags nogui
./internal/usage ok; gofmt clean. TestChunkRowsReuseReadsEachBlock now
runs 4 passes, lent and not, in both orders, through one pool; broken
by hand (a reused rows slice cut to its cap, not the block's count) it
fails "pass 0 block 1: 0 rows, want 3".

Not tried: Linux and Windows real-data runs.
2026-10-25 15:10:47 +08:00
yetone 4982ded8a6 usage: a turned page reuses the last pairing while its chunks are the same
A Requests page paired every Codex session file's calls with the
gateway's again on each query: clone, fold forks, match response ids.
That unpacked every session chunk three times and was about half of a
warm page (#3493). pairRowsMemo names the chunks of a pairing by their
serials (the probe chunk, which has none, by its rows' log offsets and
times) and keeps what the pairing found: the rows left out (skip,
matched), the actors it changed and each chunk's compactions. The next
query over the same chunks and window applies that to fresh clones
instead of pairing again. A changed file gets a new serial, a new
window or period misses, and adopt drops the memos with the parts.

Run at a6399dd49 (= origin/main, fetched):
- TestRequestPairsReusedAcrossPages (new): a gateway call paired with
  its rollout, a fork copying another file's call and a Claude call,
  every page of All/Today/Week at limit 1 and the whole page against
  pageFromLedger. Broken by hand three ways, each red on the memo hit:
  apply without matched ("all page 1: 4 rows, 157 input"), without skip
  ("4 rows, 127 input"), without compactions (page differs).
- Real data, same process (scratch test, not committed), fresh pairRows
  vs the memo applied over a second read: sandbox with the live
  rollouts (symlinked, read only) and a copy of usage.jsonl: All 15607
  chunks, 4801 skip, 1469 matched, 2 actor sets; Today 58 matched; 7d
  1075 matched. Actors, compactions, skip, matched equal, and pages
  equal for 3 filters per period. Without the gateway log: All 15600
  chunks, 4801 skip, all periods equal.
- Timing on that sandbox (load 110-143), page turns after the first:
  v0.1.1449 mean 407 / 460 ms, this change 154 / 145 ms.
- go test -count=1 -timeout 45m ./internal/usage/... ./internal/usageapi/...
  ./internal/sessions/...: usageapi and sessions ok; usage red only on
  TestUsageReadAfterAppendHashesNothingOnAFineClock/whole-second_stamp,
  which is #3511 (taken by another session). At -count=20, load 154,
  it fails 3/20 here and 2/20 at the base 1313b047f; it builds no page.
- gofmt -l clean; GOOS=linux and GOOS=windows go vet -tags nogui ok.

Not tried: the GUI against a live gateway; the page reads the same
QueryPage the tests and the real-data run used.
2026-10-25 05:18:41 +08:00
yetone a6399dd494 usage: a Requests page sums each chunk once and reuses it while the chunk is the same
Turning a page, or the 15s refresh of the open page, built every sum,
chart and share from every row of history again. On the owner's real
history that was 1.15-1.36s a page (load 170) after the chunks were
already cached.

Each chunk's rows are now folded into cells: one per provider, agent,
model, kind, caller, account, computer, source, filter bits and chart
bucket, with its totals. A page merges the cells. The cells of a chunk
are kept per (period, filter, since, until, zone), up to 4 such sets,
keyed by the chunk's serial (a new one every time a chunk is read or
frozen) and a signature of what pairing gave it: rows skipped or
matched, Codex actors and their identities (with the parent's role), and
compactions. A changed file, a gateway tail block or an identity change
from another file sums that chunk again. The newest rows are taken from
chunks in Latest order, stopping once the heap holds rows newer than
the next chunk. Cell strings are unique handles, so the cache holds no
chunk's string block. Totals and Part sums are linear, so merged cells
equal the row-by-row sums; date-mode buckets store the day number and
are placed against the chart's first day at merge time.

What was run (macOS, arm64):
- Real history (sbmem sandbox HOME, read-only links to the owner's
  data), zzbench dump of 9 cases x 2 pages (offset 0/50 after a warm
  7/50 call, and 120/30) with the base 1313b047f and this change: 0
  differences over 18 pages (floats at rel 1e-9).
- zzbench timing at load 170: warm pages 1.15s / 1.36s mean -> 0.45s /
  0.41s; first page 3.1s / 2.8s -> 2.4s / 2.6s.
- New TestRequestCellsFollowIdentityAndFiles: a child chunk whose
  parent's file changes (its root moves to top-z) without the child
  being read again, a changed child file, a gateway append, compared
  to pageFromLedger for All and Week. Broken by hand: reusing a part
  whatever its signature turns it red ("a cached part kept the old
  root: 0 rows, 0 input"). Passes in TZ=UTC too.
- go test -count=1 -timeout 45m ./internal/usage/... ./internal/usageapi/...
  ./internal/sessions/...: usageapi and sessions ok; usage red only on
  TestUsageReadAfterAppendHashesNothingOnAFineClock/whole-second_stamp,
  #3511 (taken by another session). -count=10 of it: 2 FAIL on this
  change and 2 FAIL on the base 1313b047f, at load ~100-145. Every
  other usage test passes.
- gofmt -l clean; GOOS=linux and GOOS=windows go vet -tags nogui
  ./internal/usage/.

Not done here: the pairing setup (unpacking unchanged chunks for
visibleLocal, matchedBlocks and foldCodexRows) is now most of a warm
page, ~0.18s each; it is next.
2026-10-25 05:02:18 +08:00