Every start parsed the whole of usage.jsonl with json.Unmarshal a line at a time. On the owner's real history (109 MB) that was 622 MB of the first 18s's 1765 MB allocated, and most of the GC work of a cold start. The full blocks the log index freezes are now kept in log-v1.bin beside the sessions cache, with where they end in the log (offset, line count), the SHA-256 of the log up to there and the hasher's state, first/firstOwn, the provider keys and hosts seen, and the probe rows before that offset. A start reads them and parses only the lines after. They stand only while the log's bytes up to the offset hash the same and the code that reads them is the same (requestDiskBinary); otherwise the whole log is parsed as before. The file is written through a temp file and a rename, at most every 10 minutes and only when it gains blocks. Run on mote at this tree, rebased on origin/main: - go test ./internal/usage ./internal/usageapi: ok - GOOS=linux|windows|darwin go vet -tags nogui ./internal/usage: ok - TestStartReadsKeptLogBlocks: a second start loads the kept blocks, and rows, probes, keys, hosts and hash equal a whole parse; a log edited before the offset is parsed whole. - Broken by hand: accepting the kept blocks without resumeLogHash turns TestStartReadsKeptLogBlocks red (hash differs); restored, green. - Bench on a copy of the real history (~/r2278, 109 MB log), cold start polled 120s, second start of each binary (caches hit): v0.1.2184: peaklive 217MB fp_peak 307MB, 156 GCs this: peaklive 210MB fp_peak 315MB, 98 GCs readLogSnapshot alloc_space 622MB -> 8.6MB; total 1765MB -> 1095MB. The first start writes log-v1.bin (7.4 MB). Not tried: the GUI suite (no GUI file changed); Windows and Linux runs of the package (vet only).
248 lines
6.1 KiB
Go
248 lines
6.1 KiB
Go
package usage
|
|
|
|
import (
|
|
"bufio"
|
|
"encoding/binary"
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/sessions"
|
|
)
|
|
|
|
// The log's full blocks are kept on disk between runs (log-v1.bin). A
|
|
// started magpie parsed the whole log again, a record at a time, before
|
|
// its first page: a long history's 109 MB took ~620 MB of allocations in
|
|
// the first seconds, the start's largest part, and its collections (#2278).
|
|
// The file stands for the log's first bytes while their SHA-256 is the
|
|
// one it was written with, under the same code (requestDiskBinary); a
|
|
// start reads the blocks from it and parses only the lines after them, as
|
|
// a parse continues the snapshot before it.
|
|
|
|
const logDiskMagic = "magpie-log-v1\n"
|
|
|
|
func logDiskPath() string {
|
|
return filepath.Join(filepath.Dir(sessions.CachePath()), "log-v1.bin")
|
|
}
|
|
|
|
// logFull is a snapshot as it was where its full blocks end: how many,
|
|
// the log's bytes up to the last of them, their hash and its running
|
|
// state, and what the lines up to there said.
|
|
type logFull struct {
|
|
n int
|
|
off int64
|
|
hash string
|
|
digest []byte
|
|
first, firstOwn time.Time
|
|
keyProviders map[string]bool
|
|
hosts map[string]string
|
|
}
|
|
|
|
// logDiskEvery bounds how often the file is written again: a block fills
|
|
// every logBlockRows calls, and a start parses what came after the last
|
|
// write.
|
|
const logDiskEvery = 10 * time.Minute
|
|
|
|
// logDiskLoaded, in tests, is told how many blocks a start read from the
|
|
// file.
|
|
var logDiskLoaded func(n int)
|
|
|
|
var logDiskWrote struct {
|
|
sync.Mutex
|
|
path string
|
|
n int
|
|
at time.Time
|
|
}
|
|
|
|
// logDisk is the snapshot a start continues from: the file's blocks and
|
|
// what was read with them, or nil.
|
|
type logDisk struct {
|
|
full logFull
|
|
probes []probeRow
|
|
blocks []*rowChunk
|
|
}
|
|
|
|
type logDiskProbe struct {
|
|
R Record `json:"r"`
|
|
Off int64 `json:"o"`
|
|
}
|
|
|
|
// logDiskRead is the file's snapshot of the log at path, when the log's
|
|
// first bytes are still the ones it was written of.
|
|
func logDiskRead(path string, size int64) *logDisk {
|
|
b, err := os.ReadFile(logDiskPath())
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
header, b, ok := cutDiskEntry(b)
|
|
if !ok {
|
|
return nil
|
|
}
|
|
take := func() uint64 {
|
|
n, k := binary.Uvarint(header)
|
|
if k <= 0 {
|
|
header = nil
|
|
return 0
|
|
}
|
|
header = header[k:]
|
|
return n
|
|
}
|
|
text := func() string {
|
|
n := take()
|
|
if header == nil || n > uint64(len(header)) {
|
|
header = nil
|
|
return ""
|
|
}
|
|
s := string(header[:n])
|
|
header = header[n:]
|
|
return s
|
|
}
|
|
if text() != logDiskMagic || text() != requestDiskBinary() || text() != path || header == nil {
|
|
return nil
|
|
}
|
|
d := &logDisk{full: logFull{keyProviders: map[string]bool{}, hosts: map[string]string{}}}
|
|
d.full.hash, d.full.digest = text(), []byte(text())
|
|
d.full.off, d.full.n = int64(take()), int(take())
|
|
for _, t := range []*time.Time{&d.full.first, &d.full.firstOwn} {
|
|
if s := text(); s != "" && t.UnmarshalText([]byte(s)) != nil {
|
|
return nil
|
|
}
|
|
}
|
|
for range take() {
|
|
d.full.keyProviders[text()] = true
|
|
}
|
|
for range take() {
|
|
k := text()
|
|
d.full.hosts[k] = text()
|
|
}
|
|
if header == nil || len(header) != 0 || d.full.off > size || d.full.hash == "" || logRecordHash(path, d.full.off) != d.full.hash {
|
|
return nil
|
|
}
|
|
raw, b, ok := cutDiskEntry(b)
|
|
var probes []logDiskProbe
|
|
if !ok || json.Unmarshal(raw, &probes) != nil {
|
|
return nil
|
|
}
|
|
for _, p := range probes {
|
|
d.probes = append(d.probes, probeRow{p.R, p.Off})
|
|
}
|
|
for range d.full.n {
|
|
raw, b, ok = cutDiskEntry(b)
|
|
if !ok {
|
|
return nil
|
|
}
|
|
c, err := readDiskEntry(raw)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
c.serial = nextChunkSerial()
|
|
d.blocks = append(d.blocks, c)
|
|
}
|
|
if len(b) != 0 {
|
|
return nil
|
|
}
|
|
return d
|
|
}
|
|
|
|
// logDiskKept notes that the file holds n full blocks of the log at path.
|
|
func logDiskKept(path string, n int) {
|
|
logDiskWrote.Lock()
|
|
logDiskWrote.path, logDiskWrote.n, logDiskWrote.at = path, n, time.Now()
|
|
logDiskWrote.Unlock()
|
|
}
|
|
|
|
// logDiskWrite keeps s's full blocks, when it has more than the file was
|
|
// last written with and logDiskEvery has passed since.
|
|
func logDiskWrite(s *logSnapshot) {
|
|
if s.full.n == 0 || s.full.hash == "" || s.full.digest == nil {
|
|
return
|
|
}
|
|
logDiskWrote.Lock()
|
|
defer logDiskWrote.Unlock()
|
|
w := &logDiskWrote
|
|
if w.path == s.path && (s.full.n <= w.n || time.Since(w.at) < logDiskEvery) {
|
|
return
|
|
}
|
|
if logDiskSave(s) == nil {
|
|
w.path, w.n, w.at = s.path, s.full.n, time.Now()
|
|
}
|
|
}
|
|
|
|
func logDiskSave(s *logSnapshot) error {
|
|
var h []byte
|
|
for _, t := range []string{logDiskMagic, requestDiskBinary(), s.path, s.full.hash, string(s.full.digest)} {
|
|
h = appendDiskString(h, t)
|
|
}
|
|
h = binary.AppendUvarint(h, uint64(s.full.off))
|
|
h = binary.AppendUvarint(h, uint64(s.full.n))
|
|
for _, t := range []time.Time{s.full.first, s.full.firstOwn} {
|
|
text := ""
|
|
if !t.IsZero() {
|
|
b, err := t.MarshalText()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
text = string(b)
|
|
}
|
|
h = appendDiskString(h, text)
|
|
}
|
|
h = binary.AppendUvarint(h, uint64(len(s.full.keyProviders)))
|
|
for k := range s.full.keyProviders {
|
|
h = appendDiskString(h, k)
|
|
}
|
|
h = binary.AppendUvarint(h, uint64(len(s.full.hosts)))
|
|
for k, v := range s.full.hosts {
|
|
h = appendDiskString(appendDiskString(h, k), v)
|
|
}
|
|
// the probes of the full blocks' lines: the lines after are parsed again
|
|
probes := []logDiskProbe{}
|
|
for _, p := range s.probes {
|
|
if p.off <= s.full.off {
|
|
probes = append(probes, logDiskProbe{p.Record, p.off})
|
|
}
|
|
}
|
|
pb, err := json.Marshal(probes)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
path := logDiskPath()
|
|
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
|
|
return err
|
|
}
|
|
out, err := os.CreateTemp(filepath.Dir(path), "log-*.tmp")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer os.Remove(out.Name())
|
|
bw := bufio.NewWriterSize(out, 1<<20)
|
|
err = writeDiskEntry(bw, h)
|
|
if err == nil {
|
|
err = writeDiskEntry(bw, pb)
|
|
}
|
|
var buf []byte
|
|
for _, c := range s.blocks[:s.full.n] {
|
|
if err != nil {
|
|
break
|
|
}
|
|
b := diskEntry(buf, c)
|
|
if b == nil {
|
|
err = errDiskEntry
|
|
break
|
|
}
|
|
err = writeDiskEntry(bw, b)
|
|
buf = b
|
|
}
|
|
if err == nil {
|
|
err = bw.Flush()
|
|
}
|
|
if closeErr := out.Close(); err == nil {
|
|
err = closeErr
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return os.Rename(out.Name(), path)
|
|
}
|