diff --git a/internal/usage/cold_split.go b/internal/usage/cold_split.go index d68aa83e2..2945be40a 100644 --- a/internal/usage/cold_split.go +++ b/internal/usage/cold_split.go @@ -188,6 +188,7 @@ func (b *blockPricer) alone() *blockPricer { // none. A named chunk's is kept by its serial: the page, the heatmap and // Codex's effort each ask it of every chunk. func chunkFirsts(sets [3][]*rowChunk) func(*rowChunk) time.Time { + derivedUsed() firsts.Lock() firsts.turn++ turn := firsts.turn diff --git a/internal/usage/derived_idle.go b/internal/usage/derived_idle.go new file mode 100644 index 000000000..7930b5330 --- /dev/null +++ b/internal/usage/derived_idle.go @@ -0,0 +1,81 @@ +package usage + +import ( + "runtime/debug" + "sync/atomic" + "testing" + "time" +) + +// derivedIdle is how long what is kept of each chunk by its serial (its +// message keys, match hints, first time and heat before a cut) stays +// after the last pairing or heatmap that read it. Each is read again from +// its chunk on the next ask. They were kept by turns, and with no turn +// while nobody looked they stayed for the life of the process: ~4 MB of +// message keys alone in a real history's idle heap. +const derivedIdle = 3 * time.Minute + +var derivedKept struct { + last atomic.Int64 + armed atomic.Bool + off bool +} + +func init() { derivedKept.off = testing.Testing() } + +// derivedUsed notes a read of the per-chunk caches and arms the idle +// check. +func derivedUsed() { + if derivedKept.off { + return + } + derivedKept.last.Store(time.Now().UnixNano()) + if derivedKept.armed.CompareAndSwap(false, true) { + time.AfterFunc(derivedIdle, derivedIdleCheck) + } +} + +// derivedIdleCheck lets the per-chunk caches go once nothing has read +// them for derivedIdle, or looks again when that will be. +func derivedIdleCheck() { + if derivedKept.off { // a test's timer, after it ended + derivedKept.armed.Store(false) + return + } + if left := derivedIdle - time.Since(time.Unix(0, derivedKept.last.Load())); left > 0 { + time.AfterFunc(left, derivedIdleCheck) + return + } + at := time.Now().UnixNano() + n := dropDerived() + derivedKept.armed.Store(false) + if derivedKept.last.Load() > at && derivedKept.armed.CompareAndSwap(false, true) { + time.AfterFunc(derivedIdle, derivedIdleCheck) // read again meanwhile + } + if n > 0 { + debug.FreeOSMemory() + } +} + +// dropDerived lets go of what is kept of every chunk by its serial and +// says how many were kept. +func dropDerived() int { + n := 0 + msgKeys.Lock() + n += len(msgKeys.by) + msgKeys.by = nil + msgKeys.Unlock() + matchHints.Lock() + n += len(matchHints.by) + matchHints.by = nil + matchHints.Unlock() + firsts.Lock() + n += len(firsts.by) + firsts.by = map[uint64]chunkFirst{} // written below an unlock, never nil + firsts.Unlock() + heatBefores.Lock() + n += len(heatBefores.by) + heatBefores.by = nil + heatBefores.Unlock() + return n +} diff --git a/internal/usage/derived_idle_test.go b/internal/usage/derived_idle_test.go new file mode 100644 index 000000000..6c8e0c4c4 --- /dev/null +++ b/internal/usage/derived_idle_test.go @@ -0,0 +1,66 @@ +package usage + +import ( + "testing" + "time" +) + +// What is kept of each chunk by its serial stays while it was read within +// derivedIdle, is let go after, and is read again from the chunk with the +// same answer. +func TestDerivedLetGoWhenIdle(t *testing.T) { + now := time.Now().Truncate(time.Second) + cut := now.Add(-48 * time.Hour) + var set []*rowChunk + for k := range 3 { + c := &rowChunk{serial: nextChunkSerial()} + for i := range 30 { + r := Record{Time: now.Add(-time.Duration(k*15+i) * 3 * time.Hour), Agent: "claude", Provider: "relay", Input: 1 + i, Output: k} + c.add(Row{Record: r}, "", int64(i), false) + } + set = append(set, c) + } + sets := [3][]*rowChunk{set, nil, nil} + var opened int + heatBeforeOpening = func(*rowChunk) { opened++ } + t.Cleanup(func() { heatBeforeOpening = nil }) + derivedKept.off = false + t.Cleanup(func() { derivedKept.off = true }) + + before := heatBefore(cut, sets) + firstAt := chunkFirsts(sets)(set[0]) + if opened != 3 || before.n == 0 || firstAt.IsZero() { + t.Fatalf("first read unpacked %d, %+v, first %v", opened, before, firstAt) + } + derivedIdleCheck() // read just now: kept + opened = 0 + if heatBefore(cut, sets); opened != 0 { + t.Fatalf("read within derivedIdle unpacked %d chunks; want them kept", opened) + } + + derivedKept.last.Store(time.Now().Add(-2 * derivedIdle).UnixNano()) + derivedIdleCheck() + heatBefores.Lock() + hb := len(heatBefores.by) + heatBefores.Unlock() + firsts.Lock() + fb := len(firsts.by) + firsts.Unlock() + if hb != 0 || fb != 0 { + t.Fatalf("idle: %d heat sums and %d firsts kept", hb, fb) + } + if derivedKept.armed.Load() { + t.Fatal("still armed after letting go with no read since") + } + + opened = 0 + if got := heatBefore(cut, sets); got != before || opened != 3 { + t.Fatalf("after the let-go: %+v unpacking %d, want %+v unpacking 3", got, opened, before) + } + if got := chunkFirsts(sets)(set[0]); !got.Equal(firstAt) { + t.Fatalf("first after the let-go = %v, want %v", got, firstAt) + } + if !derivedKept.armed.Load() { + t.Fatal("a read after the let-go did not arm the check again") + } +} diff --git a/internal/usage/heatmap.go b/internal/usage/heatmap.go index dc4945f0b..4a190af46 100644 --- a/internal/usage/heatmap.go +++ b/internal/usage/heatmap.go @@ -243,6 +243,7 @@ type heatSum struct { // it of every recent chunk, unpacked whole, on each read, ~120 MB of a // start's first 18s's allocations on a year's history (#2278). func heatBefore(cut time.Time, recent [3][]*rowChunk) heatSum { + derivedUsed() priceStubs(recent[0]) heatBefores.Lock() heatBefores.turn++ diff --git a/internal/usage/match_chunks.go b/internal/usage/match_chunks.go index 7a69bb4d7..2bc9d9023 100644 --- a/internal/usage/match_chunks.go +++ b/internal/usage/match_chunks.go @@ -43,6 +43,7 @@ var matchOpening func(*rowChunk) // hintsOf are chunks' hints, read from a chunk only when none are kept for // its serial. func hintsOf(chunks []*rowChunk, rows *chunkRows) []*chunkHints { + derivedUsed() matchHints.Lock() matchHints.turn++ turn := matchHints.turn diff --git a/internal/usage/request_page.go b/internal/usage/request_page.go index ee57c8008..4d10b4ac1 100644 --- a/internal/usage/request_page.go +++ b/internal/usage/request_page.go @@ -1699,6 +1699,7 @@ var msgKeysOpening func(*rowChunk) // cold start's pairing of the whole history kept all of them, ~67 MB of // its ~460 MB live heap, beside the period's that polls read (#2278). func msgKeysOf(chunks []*rowChunk) []*chunkMsgKeys { + derivedUsed() settled := settledBefore(time.Now()) msgKeys.Lock() msgKeys.turn++