usage: what is kept of each chunk by its serial is let go after three idle minutes

Message keys, match hints, chunk first times and heat sums are kept by
chunk serial and pruned only by turns, so with no pairing or heatmap
while nobody looked they stayed for the life of the process (~4 MB of
message keys alone in a real history's idle heap). They are now let go
once none was read for three minutes, and read again from the chunk on
the next ask.

Test: TestDerivedLetGoWhenIdle (kept while read, let go when idle, same
answers after).

Run: go test ./internal/usage on mote at this head (ok, 36s) and on
the Linux box; go vet linux, windows, darwin. Broken by hand (the drop
skipped): red, "idle: 3 heat sums and 3 firsts kept". Real-account copy
on mote, 330s idle, both orders: hintsOf's 1.6 MB is gone from the end
heap; footprint moved with run order (+10-15 MB for whichever ran
second), so no footprint gain is claimed. Not tried: an idle after the
Requests page was paged, where message keys are kept.
This commit is contained in:
yetone
2026-11-10 16:45:02 +08:00
parent 77035a4429
commit 3b261932e1
6 changed files with 151 additions and 0 deletions
+1
View File
@@ -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
+81
View File
@@ -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
}
+66
View File
@@ -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")
}
}
+1
View File
@@ -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++
+1
View File
@@ -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
+1
View File
@@ -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++