Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

usage: pages of 131k rows or more pair one at a time (#2278)

yetonec8095d110191
diff --git a/internal/usage/request_big_build_test.go b/internal/usage/request_big_build_test.go
new file mode 100644
index 000000000..d46ed8662
--- /dev/null
+++ b/internal/usage/request_big_build_test.go
@@ -0,0 +1,40 @@
+package usage
+
+import (
+ "sync"
+ "testing"
+ "time"
+)
+
+// Two big pages pair their rows one after another, not side by side: on a
+// cold start the heatmap's year and the requests' All each held every
+// call paired at once, most of the footprint's peak (#2278).
+func TestBigPagesPairInTurn(t *testing.T) {
+ pageHome(t)
+ settleLogClock(t)
+ holdClock(t, time.Date(2026, 9, 30, 12, 0, 0, 0, time.Local))
+ historyLog(t, 1034)
+ rows := bigBuildRows
+ bigBuildRows = 1
+ t.Cleanup(func() { bigBuildRows = rows })
+ var mu sync.Mutex
+ in, most := 0, 0
+ pairing = func() {
+ mu.Lock()
+ in++
+ most = max(most, in)
+ mu.Unlock()
+ time.Sleep(200 * time.Millisecond) // the other page pairs now, if it can
+ mu.Lock()
+ in--
+ mu.Unlock()
+ }
+ t.Cleanup(func() { pairing = nil })
+ var wg sync.WaitGroup
+ wg.Go(func() { QueryPage(All, Filter{}, 0, 100) })
+ wg.Go(func() { HeatmapOf(Filter{}) })
+ wg.Wait()
+ if most != 1 {
+ t.Fatalf("%d big pages paired at once, want 1", most)
+ }
+}
diff --git a/internal/usage/request_page.go b/internal/usage/request_page.go
index cdc46f8b6..1acadec72 100644
--- a/internal/usage/request_page.go
+++ b/internal/usage/request_page.go
@@ -797,7 +797,10 @@ func queryPageOf(p Period, f Filter, offset, limit int, readSource func(sessions
if building != nil {
building()
}
- page = buildRequestBlocksIn(p, f, offset, limit, now, b.gateways, b.chunks, b.others, names, codexIdentities, cache)
+ func() {
+ defer holdBigBuild(b.gateways, b.chunks, b.others)() // on a panic too
+ page = buildRequestBlocksIn(p, f, offset, limit, now, b.gateways, b.chunks, b.others, names, codexIdentities, cache)
+ }()
ownStrings(reflect.ValueOf(&page).Elem())
b.priced.release()
shared.Lock()
@@ -853,8 +856,13 @@ func codexEffortOfIndex(st requestState, p Period, now time.Time) map[string][]s
b := readRequestBlocks(idx, st, p, now, sessions.ReadCallSource, true)
defer b.priced.release()
sums := effortSums{}
- ps, next := pairRowsMemo(p, now, b.gateways, b.chunks, b.others, st.codexIdentities, memo)
- ps.visitActors(func(r *Row) { sums.add(r) })
+ var next *pairMemo
+ func() {
+ defer holdBigBuild(b.gateways, b.chunks, b.others)()
+ var ps *pairedRows
+ ps, next = pairRowsMemo(p, now, b.gateways, b.chunks, b.others, st.codexIdentities, memo)
+ ps.visitActors(func(r *Row) { sums.add(r) })
+ }()
b.priced.release()
shared.Lock()
if shared.keep(idx, b.on) {
@@ -866,6 +874,30 @@ func codexEffortOfIndex(st requestState, p Period, now time.Time) map[string][]s
return sums.values()
}
+// bigBuilds is held while a page pairs and adds up bigBuildRows rows or
+// more. On a cold start the Usage page's heatmap and requests each paired
+// a year of calls side by side, ~350 MB held at once and most of the
+// footprint's peak (#2278). A smaller page, Today's, goes beside them.
+var bigBuilds sync.Mutex
+
+var bigBuildRows = 1 << 17
+
+// holdBigBuild holds bigBuilds when the chunks have bigBuildRows rows or
+// more, and is what lets it go.
+func holdBigBuild(sets ...[]*rowChunk) (let func()) {
+ n := 0
+ for _, set := range sets {
+ for _, c := range set {
+ n += rowsIn(c)
+ }
+ }
+ if n < bigBuildRows {
+ return func() {}
+ }
+ bigBuilds.Lock()
+ return bigBuilds.Unlock
+}
+
// requestBlocks are what a page of a period is worked out from: the
// gateway's blocks of it, priced, the session files' chunks and the other
// computers' days, and the paths the index holds now (on).