An idle magpie's heap (real-account copy, 600s idle on mote, t58) held two copies of models.dev's catalog for the life of the process: - the shipped copy, unpacked by Seed at start into a sync.OnceValue: 5.7 MB of io.ReadAll, though Seed has nothing to write once a newer catalog file is there, and nothing else reads it then; - the parsed catalog (loadGen, ~6.7 MB with its votes and indexes), loaded at start and by dsh's five-minutely rebuild. Seed now looks at the file before unpacking, and the copy is unpacked for each call and kept by no one. The loaded catalog, with what is built and indexed of it, is let go once nothing has read it for two minutes (load_idle.go, like the sessions' cacheIdle). The next read parses the same file again. Forget is not told: the provider package's lists stay right, since a changed file is Reset by whoever wrote it. TestIdleCatalogLetGoAndReadAgain and TestSeedUnpacksNothingOverANewerCatalog are red with dropIdleLoad returning false and with Seed unpacking first (broken by hand in a copy). TestListedByReadsAnIndex is red under -race at the base too (#6023).
92 lines
2.6 KiB
Go
92 lines
2.6 KiB
Go
package catalog
|
|
|
|
import (
|
|
"runtime/debug"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// loadIdle is how long the loaded catalog stays in memory after the last
|
|
// read of it. models.dev's api.json is ~6 MB, and parsed, with what is
|
|
// voted and indexed of it, it was the largest thing an idle magpie held
|
|
// (~7 MB of a ~40 MB heap) for the life of the process. Read again, it
|
|
// is parsed again from the same file (~0.1s), as after a Reset.
|
|
const loadIdle = 2 * time.Minute
|
|
|
|
var loadKept struct {
|
|
last atomic.Int64
|
|
armed atomic.Bool
|
|
off bool
|
|
}
|
|
|
|
func init() { loadKept.off = testing.Testing() }
|
|
|
|
// loadDrops counts the times the loaded catalog was let go, for the tests.
|
|
var loadDrops atomic.Int64
|
|
|
|
// loadUsed notes a read of the loaded catalog and arms the idle check.
|
|
func loadUsed() {
|
|
if loadKept.off {
|
|
return
|
|
}
|
|
loadKept.last.Store(time.Now().UnixNano())
|
|
if loadKept.armed.CompareAndSwap(false, true) {
|
|
time.AfterFunc(loadIdle, loadIdleCheck)
|
|
}
|
|
}
|
|
|
|
// loadIdleCheck lets the loaded catalog go once nothing has read it for
|
|
// loadIdle, or looks again when that will be.
|
|
func loadIdleCheck() {
|
|
if left := loadIdle - time.Since(time.Unix(0, loadKept.last.Load())); left > 0 {
|
|
time.AfterFunc(left, loadIdleCheck)
|
|
return
|
|
}
|
|
at := time.Now().UnixNano()
|
|
dropped := dropIdleLoad()
|
|
loadKept.armed.Store(false)
|
|
if loadKept.last.Load() > at && loadKept.armed.CompareAndSwap(false, true) {
|
|
time.AfterFunc(loadIdle, loadIdleCheck) // read again meanwhile
|
|
}
|
|
if dropped {
|
|
debug.FreeOSMemory()
|
|
}
|
|
}
|
|
|
|
// dropIdleLoad lets go of the loaded catalog and what was built of it,
|
|
// as Reset does, when nothing has read it for loadIdle. What the
|
|
// provider package keeps of it is left alone (Forget is not told): the
|
|
// next load reads the same file, and a changed one is Reset by whoever
|
|
// wrote it. It says whether there was anything to let go.
|
|
func dropIdleLoad() bool {
|
|
loadMu.Lock()
|
|
if !loaded || time.Since(time.Unix(0, loadKept.last.Load())) < loadIdle {
|
|
loadMu.Unlock()
|
|
return false
|
|
}
|
|
loaded = false
|
|
built = nil
|
|
gen++
|
|
mdev, images, windows, outputs, efforts, thinks, names, listed, canonical, labs, normal = nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil
|
|
loadMu.Unlock()
|
|
// what was indexed of the old load is made again for the next
|
|
priceIndexes.Lock()
|
|
priceIndexes.by = nil
|
|
priceIndexes.Unlock()
|
|
makerOwners.Lock()
|
|
makerOwners.by = nil
|
|
makerOwners.Unlock()
|
|
levelIndexes.Lock()
|
|
levelIndexes.by = nil
|
|
levelIndexes.Unlock()
|
|
endpoints.Lock()
|
|
endpoints.by = nil
|
|
endpoints.Unlock()
|
|
pricedMemo.Lock()
|
|
pricedMemo.by = nil
|
|
pricedMemo.Unlock()
|
|
loadDrops.Add(1)
|
|
return true
|
|
}
|