sourceDigests.by and .last, the serials met this turn and the one
before, stayed for the life of the process: 577 KB of a real history's
idle heap. They only order which digests heat-digests.bin keeps first
when there are more than it holds (2*65536, against ~17k session
files). dropDerived, the 3-minute idle drop, now writes what was made
and lets them go; sourceDigest notes a read (derivedUsed) as the other
per-chunk caches do.
TestDigestTurnsGoWhenIdle: the turn's serials are nil after the idle
drop and the next key hashes nothing again. Red with the nil-ing taken
out by hand ("kept 1 and 0 serials when idle").
Run at this commit on mote (load 23.7, over its 18 cores; mica's disk
was full): go vet for darwin, linux and windows ok; go test
./internal/usage ok (52s), ./internal/gui ok (132s). Real Codex history
in a sandbox HOME (~/r2278), page open 30s then 8.5 min idle: liveHeap
13.13 MB against 13.68 MB before; the heap diff shows sourceDigest's
577 KB gone. Not run: the full suite.
125 lines
3.4 KiB
Go
125 lines
3.4 KiB
Go
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()
|
|
n += dropEffortPeriods()
|
|
n += dropDigestTurns()
|
|
return n
|
|
}
|
|
|
|
// dropDigestTurns lets go of the serials sourceDigest met this turn and
|
|
// the one before (577 KB of a real history's idle heap). They only order
|
|
// which digests heat-digests.bin keeps first when there are more than it
|
|
// holds; what was made is written out before they go.
|
|
func dropDigestTurns() int {
|
|
sourceDigestsSave()
|
|
d := &sourceDigests
|
|
d.Lock()
|
|
defer d.Unlock()
|
|
if len(d.made) != 0 { // made meanwhile: written with them next time
|
|
return 0
|
|
}
|
|
n := len(d.by) + len(d.last)
|
|
d.by, d.last = nil, nil
|
|
return n
|
|
}
|
|
|
|
// dropEffortPeriods lets go of every period's kept CodexModelEffort answer
|
|
// but one being built: each thread's consumption by model, ~7 MB of a real
|
|
// history's idle heap after one visit to the Sessions page. The next ask
|
|
// builds it again, waiting for it as a first ask does. The effort before
|
|
// the last few days (effortOlds, ~2 MB more) goes too when no build is
|
|
// under way: it is in effort-v1.json, read back by the next build.
|
|
func dropEffortPeriods() int {
|
|
n := 0
|
|
if codexEffortBuilding.TryLock() {
|
|
n += len(effortOlds)
|
|
clear(effortOlds)
|
|
codexEffortBuilding.Unlock()
|
|
}
|
|
codexEffort.Lock()
|
|
defer codexEffort.Unlock()
|
|
for p, e := range codexEffort.periods {
|
|
if e.building == nil {
|
|
delete(codexEffort.periods, p)
|
|
n++
|
|
}
|
|
}
|
|
return n
|
|
}
|