Commit Graph
7 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 213d59f3b3 usage: a first start pairs the history once for the page and Codex's effort (#2278)
A first start, with no sums on disk, paired the whole history a few
days at a time (coldSpans) for the warm-up's page of All (pageCold),
then again for Codex's effort of the same period (effortCold), or the
other way round when the Sessions overview asked first. Both read the
same blocks and name the settled days by the same heatSettledKey, so
whichever runs first now counts both from its one pairing and writes
both files: pageCold counts the effort when its filter is empty and
effort-v1.json lacks it, effortCold counts the cells of the period's
unfiltered page when page-v1.json lacks them.

Run on mote (M5 Pro), at this tree (rebased on 7e0fe83d4):
- go test -count=1 ./internal/usage/: ok (25.5s).
- TestFirstBuildPairsOnceForPageAndEffort asks the page then the
  effort, and the effort then the page, each after a cold restart:
  the second pairs no span, and both answers equal a build alone.
  Broken by hand: pageCold given no effort key, red ("page first:
  effort … after N spans"); effortCold counting no cells, red
  ("effort first: page …").
- Real cold start on the 11,925-file fixture (8.5GB .zst + 17GB
  jsonl), `magpie web`, two runs each, alternated:
  before (v0.1.2368's tree) CPU 3:11.45 / 3:04.84, footprint peak
  332 / 335 MB; after CPU 2:54.02 / 2:57.12, peak 330 / 325 MB. The
  heatmap, requests, overview (0/7/30) and stats (0/30) answers are
  byte for byte the same. Load on mote 22-28 from other sessions.

Not changed: the footprint's peak barely moves; the two builds ran one
after the other already (coldTurns), so this saves their CPU, not a
peak.
2026-10-31 18:57:39 +08:00
yetone a377ffde9e usage: a first build pairs four days of history at a time, not two weeks (#2278)
coldSpan was 14: each span of a first start's pairing held ~1000 to
4700 chunks' matched blocks, priced stubs and Codex pairs at once,
the largest part of the cold-start peak. Four days holds less of it
at the same CPU. The comments that said "a few weeks" say days, and
TestFirstBuildPairsSpans checks 1, 4 and 14 days a span against a
pairing of every chunk.

Run (mote, through mote-run): go build, go vet ./internal/usage,
go test ./internal/usage ./internal/usageapi: ok.

Real history copy on mote, cold starts:
  coldSpan 4   peak 319/322/328 MB (avg 323)  cpu 3:55/4:23/4:05
  coldSpan 14  peak 354/356/342 MB (avg 351)  cpu 4:20/4:08/4:05
  (an earlier pass: 14 → 353/372, 4 → 332/326, 2 → 325/323 with
  cpu 4:26/4:32; 2 days bought little more, so 4.)
heatmap, overview 0/7/30 and stats 0/30 JSON are byte-identical; the
request page differs in 10 floats by summation order only (<1e-9
relative), counts and every other value the same.

Rebased onto b09349d17 (probes.go, search_calls_test.go in usage); go vet
and go test ./internal/usage ./internal/usageapi again there: ok.
2026-10-31 10:35:47 +08:00
yetone f2eb56c6bb usage: a first start pairs the history a few weeks at a time, not at once (#2278)
With no sums on disk (a first install, or a version that counts rows
otherwise), the page's cells, the heatmap's days and Codex's effort
before the cut were built from one pairing of the whole history. Every
gateway block was priced, every session row's message id went into one
set, and every call into one match map. On the owner's history that
was ~212 MB of the warm-up's 341 MB in use and the cold start's peak.

coldSpans builds them as a day's start advances them (pageAdvance),
14 days at a time. A span pairs the chunks with a row from heatReach
before it to heatReach after. Each chunk's oldest row is kept by its
serial. A gateway block not priced yet is priced by a pricer of the
span alone, and its rows go back to the pool after the span. The
recent days are then paired as on any kept start.

The page's cells come out in another order (by span, then chunk), so
TestSettledCountedAsVersioned now sorts them before it hashes. The
sorted hash is the same for a pairing at once and in spans, so
settledCountVersion stays. Tests that count a pairing of every chunk
use pairAtOnce (coldSpan 0).

Run on mote at the pushed head (rebased on b62fa332f): gofmt -l
clean; go vet ./internal/usage for darwin, linux and windows ok;
go test -count=1 ./internal/usage ok (20.0s); go build . ok.
Broken by hand: TestFirstBuildPairsSpans goes red when a span pairs
only its own days (lo, hi := from, to): the effort differs.
Bench, real history (a year), cold first start, against v0.1.2256,
two rounds each, on mote at load ~22:
  no GUI polls:  footprint peak 451, 407 MB (2256: 493, 484);
                 live heap max 318, 289 MB (2256: 367, 362)
  GUI polls:     footprint peak 483, 410 MB (2256: 534, 532);
                 live heap max 316, 290 MB (2256: 395, 406)
The heatmap's answers are byte-equal to 2256's. The request page's
differ only in float digits from the summation order (232 floats,
largest relative difference 6.5e-14); every other value is equal.
Not tried: a start on Windows or Linux.
2026-10-31 03:13:28 +08:00
yetone f45c3c96b6 usage: a day's first start advances the kept sums from the last cut, not a pairing of all history (#2278)
The page's cells, the heatmap's days and Codex's effort before the last
days are kept on disk (page-v1.json, heat-v1.json, effort-v1.json) under
a key that names the cut, today minus two days at midnight. The cut moves
each midnight, so each user's first start of a day found none of them,
and paired the whole history again three times. On the 240k-row bench
history that peak was 506-538 MB, against 235-325 MB for a start of the
same day.

Each file now also holds the cut and since its sums were counted at.
When the current key misses, the file's sums still stand if the key at
their own cut is the same: the chunks are split again at that cut, named
as heatSettledKey names them, and compared with the file's key. Then
only the chunks of the days since that cut are paired. The rows between
the two cuts are added to the kept sums, and the sum is written under
the current key. The heatmap also drops days before a since that moved
forward a week. A full pairing is left for a first install, a raised
version, or a settled chunk that changed.

settled_version_test blanks a page's cut and since, as it blanks its key
and at. They say when the cells were counted, not how, so
settledCountVersion stays at 1. Files written before this commit have no
cut, so the first day change after the upgrade still pairs everything
once.

Run at 9cd9cfed0 (rebased on cfb06e11c), on mote:
- go test -count=1 ./internal/usage: ok (16.8s)
- GOOS=linux and GOOS=windows go vet ./internal/usage: ok
- gofmt -l internal/usage: clean

New tests (cut_advance_test.go): TestHeatmapDaysAdvanceACut,
TestPageCellsAdvanceACut and TestCodexEffortAdvancesACut. Each holds the
clock at Sunday 2026-10-04, then at Monday, and checks that every
pairing on Monday is below all chunks and that the result equals a cold
pairing. The heatmap test includes a call 368 days back that leaves the
window when the week turns.

Broken by hand:
- With the days between the cuts left out (all three), all three tests
  are red.
- With the advance turned off (all three), all three tests are red on
  the pairing count.

Real history (mote, ~/r2278: 240k gateway rows and the full session
history, window endpoints polled every 5s, clock moved one day by a
temporary build-only env that is not in this commit, caches restored
with cp -Rp each run):
- day change, advance off: peak 506, 518 MB
- day change, this commit: peak 418, 398 MB
- the same day: peak 325 MB
- /api/usage/heatmap and /api/usage/requests were byte-identical with
  the advance on and off.

Not tried: a real overnight day change on a user's machine. The cold
first-install peak (~480-538 MB) is unchanged by this commit.
2026-10-31 01:48:43 +08:00
yetone 0414bbeddb usage: page-v1.json holds eight pages and moves one read near its end first, so a reader's filters don't drop the warm-up's (#2278)
page-v1.json held four pages, newest written first, and a page read
from it was never moved. The warm-up's All, the Usage page's period,
the tray's periods and a few filters a reader picks are more than four,
so the warm-up's was dropped and every start after paired the whole
history again (the 587 MB start). Seen on the real history: after five
pg.sh pages the next start missed. It holds eight now, and a page read
from the second half is written again first.

Real history on mote (~/r2278), with the previous commit: a start
after the five pages hit (fp_peak 356 MB, peaklive 266 MB).

Test: TestPageReadAgainStaysOnDisk (the warm-up's page last of eight,
read, then one more page written: the next start still reads it).
Broken by hand (no move): red, "read again, then another page written".

Run at this head, on mote:
  go test -count=1 ./internal/usage ./internal/usageapi: ok, ok
  GOOS=linux/windows/darwin go vet -tags nogui ./internal/usage: ok
Not run: the full suite. internal/gui's AppKit tests time out on mote
(#4125); this change is internal/usage only.

Rebased on 6d154ef1e (with the commit before this); go test
./internal/usage ./internal/usageapi and the three vets ran again at this
head: ok.
2026-10-30 16:15:28 +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