From 0414bbeddbfc2028508b610fd1e7c194143454a0 Mon Sep 17 00:00:00 2001 From: yetone Date: Fri, 30 Oct 2026 16:14:41 +0800 Subject: [PATCH] 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. --- internal/usage/page_split.go | 25 ++++++++++++++++--------- internal/usage/page_split_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/internal/usage/page_split.go b/internal/usage/page_split.go index 44004c16f..d6f71ee6b 100644 --- a/internal/usage/page_split.go +++ b/internal/usage/page_split.go @@ -85,7 +85,11 @@ func pageSplit(p Period, f Filter, now time.Time, take int, pass *cellPass, gate fresh.settled, fresh.at = serials, time.Now() old, kept = &fresh, true default: - if c, found := pageDiskRead(key); found { + if c, at, found := pageDiskRead(key); found { + if at >= pageDiskPages/2 { + // read again near the end: kept from being the next dropped + _ = pageDiskWrite(key, c) + } old, kept = &pageOld{cut: cut, since: since, settled: serials, before: before, at: time.Now(), key: key, cells: c}, true } } @@ -211,9 +215,11 @@ type pageDiskEntry struct { Cells []pageDiskCell `json:"cells"` } -// pageDiskPages bounds the pages the file holds: a page is open on one -// period and filter, and the warm-up and the Sessions page ask for All's. -const pageDiskPages = partSets +// pageDiskPages bounds the pages the file holds, the last written or read +// again first: the warm-up's All, the Usage page's period, the tray's +// periods and the filters a reader picks. Four let a reader's few filters +// drop the warm-up's, and every start after paired the whole history again. +const pageDiskPages = 8 func pageDiskEntries() []pageDiskEntry { b, err := os.ReadFile(pageDiskPath()) @@ -227,9 +233,10 @@ func pageDiskEntries() []pageDiskEntry { return all } -// pageDiskRead is the cells the file holds under key, or false. -func pageDiskRead(key string) (*chunkPart, bool) { - for _, e := range pageDiskEntries() { +// pageDiskRead is the cells the file holds under key and where in the file +// they are, or false. +func pageDiskRead(key string) (*chunkPart, int, bool) { + for at, e := range pageDiskEntries() { if e.Key != key { continue } @@ -244,9 +251,9 @@ func pageDiskRead(key string) (*chunkPart, bool) { source: c.Source, bits: c.Bits, bucket: c.Bucket, n: c.N, d: c.D, } } - return part, true + return part, at, true } - return nil, false + return nil, 0, false } // pageDiskWrite keeps part under key with the pages written last, through diff --git a/internal/usage/page_split_test.go b/internal/usage/page_split_test.go index ae408b530..463a97c9e 100644 --- a/internal/usage/page_split_test.go +++ b/internal/usage/page_split_test.go @@ -1,6 +1,7 @@ package usage import ( + "fmt" "os" "path/filepath" "reflect" @@ -104,3 +105,27 @@ func TestLocalZoneIsTheWarmUpsPage(t *testing.T) { t.Fatalf("the page in time.Local paired %v: %+v, the warm-up's %+v", *paired, page, warm) } } + +// A page read from near the end of page-v1.json is moved first: pages +// written after it don't drop it, and the next start reads it again. +func TestPageReadAgainStaysOnDisk(t *testing.T) { + _, _, paired, restart := pageHistory(t) + QueryPage(All, Filter{}, 0, 1) + all := (*paired)[0] + for i := range pageDiskPages - 1 { + if err := pageDiskWrite(fmt.Sprint("other", i), &chunkPart{}); err != nil { + t.Fatal(err) + } + } + restart() + if QueryPage(All, Filter{}, 0, 1); (*paired)[len(*paired)-1] >= all { + t.Fatalf("the last page in the file, paired %d of %d", (*paired)[len(*paired)-1], all) + } + if err := pageDiskWrite("one more", &chunkPart{}); err != nil { + t.Fatal(err) + } + restart() + if QueryPage(All, Filter{}, 0, 1); (*paired)[len(*paired)-1] >= all { + t.Fatalf("read again, then another page written: paired %d of %d", (*paired)[len(*paired)-1], all) + } +}