Each Codex listing (several a minute of the page's polls over a long history) made every rollout's path and key again, grew a new dirent.Listing an entry at a time for every day folder, and gave each rollout name a slice of its own in distinctCodexFiles: ~230 MB of a minute of polls, from 20k unchanged rollouts. - listedRollouts keeps each day folder's rollouts as its last listing found them; a rollout listed before keeps its path and key (made only from its folder and name) and takes its size and time now. A folder is kept again only when a rollout came or went. - dirent.Stats takes its listing from a pool and its callers (listedRollouts, statFiles) give it back with Done once read; Done clears it. getattrlistbulk's 64 KB buffer is pooled too. - distinctCodexFiles keeps a name's first file in a map of ints, and a slice only for names with more files. New tests: TestListedRolloutsKeepTheirNames (a grown, a removed and a new rollout; reused strings) and TestStatsAfterDone. Run on mote. Written on7037cd980(v0.1.2126), rebased on6db21d869(no sessions or dirent change between); at the pushed head again: go test -race ./internal/dirent/ ./internal/sessions/ ok, and the three vets below clean. At the tree before the rebase: - go vet -tags nogui ./internal/sessions/ ./internal/dirent/ for linux, windows and darwin: clean. - go test -race ./internal/dirent/ ./internal/sessions/: ok, at this tree. Before the dirent pool was added: also ./internal/usage/ ok and ./internal/gui/ ok but TestMainWindowCorners (#3949, known). - Broken by hand: listedRollouts not reading its last listing turns TestListedRolloutsKeepTheirNames red ("had its path and key made again"); Done not clearing turns TestStatsAfterDone red. - Full-history bench (10 endpoints every 5s, heap profiles at 50s and 110s, alloc_space diff), two rounds each, v0.1.2126 against this: listCodexFiles 229 / 238 MB -> 72 / 92 MB; dirent.Stats 115 / 130 MB -> 62 / 72 MB; distinctCodexFiles 40 / 51 MB -> 7 / 12 MB; readRequestState 318 / 390 MB -> 225 / 222 MB. The totals (1186 / 694 against 477 / 960 MB) swing with mote's load more than the change moves them. Not changed: dirent's names are still copied for each entry (~30 MB), and Linux and Windows list without dirent.Stats (walkLinked, statFiles' lstat), so only the distinctCodexFiles part reaches them.
242 lines
6.5 KiB
Go
242 lines
6.5 KiB
Go
package sessions
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
"slices"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
codexCopiesDiffer = iota
|
|
codexCopiesEqual
|
|
codexFirstPrefix
|
|
codexSecondPrefix
|
|
maxCodexCopyPairs = 4096
|
|
)
|
|
|
|
type codexCopyPair struct{ First, Second string }
|
|
type codexCopyResult struct {
|
|
First, Second os.FileInfo
|
|
Relation int
|
|
}
|
|
|
|
var codexCopyCache = struct {
|
|
sync.Mutex
|
|
Pairs map[codexCopyPair]codexCopyResult
|
|
}{Pairs: map[codexCopyPair]codexCopyResult{}}
|
|
|
|
// Windows replaces this with a same-handle stat carrying ChangeTime.
|
|
var codexDiscoveryStat = os.Stat
|
|
var compareCodexCopies = readCodexCopies
|
|
var codexDiscoveryNow = time.Now
|
|
|
|
// A file system stamps change times from the clock tick, not the precise
|
|
// clock: NTFS from Windows' system time (0.5 ms to 15.6 ms), Linux from the
|
|
// coarse kernel clock. Two writes within one tick get the same change time,
|
|
// so a comparison is cached only for files whose change time is older than
|
|
// this when it starts. A later rewrite is then in a later tick and changes
|
|
// the stamp; a file changed more recently is compared again.
|
|
const codexStampSettle = time.Second
|
|
|
|
type codexDiscoveryInfo struct {
|
|
os.FileInfo
|
|
ChangeTime time.Time
|
|
}
|
|
|
|
// codexChangeTime is the file's change time, zero when the file system
|
|
// doesn't expose one.
|
|
func codexChangeTime(info os.FileInfo) time.Time {
|
|
if wrapped, ok := info.(codexDiscoveryInfo); ok {
|
|
return wrapped.ChangeTime
|
|
}
|
|
// Darwin and Linux expose nanosecond ctime under different field names.
|
|
// An unsupported filesystem gets no cached content comparison: mtime
|
|
// alone cannot detect a same-size rewrite with a restored timestamp.
|
|
v := reflect.ValueOf(info.Sys())
|
|
if v.Kind() == reflect.Pointer && !v.IsNil() {
|
|
v = v.Elem()
|
|
}
|
|
if v.Kind() != reflect.Struct {
|
|
return time.Time{}
|
|
}
|
|
for _, name := range []string{"Ctimespec", "Ctim"} {
|
|
c := v.FieldByName(name)
|
|
if c.IsValid() && c.Kind() == reflect.Struct {
|
|
sec, nsec := c.FieldByName("Sec"), c.FieldByName("Nsec")
|
|
if sec.IsValid() && nsec.IsValid() && sec.CanInt() && nsec.CanInt() {
|
|
return time.Unix(sec.Int(), nsec.Int())
|
|
}
|
|
}
|
|
}
|
|
return time.Time{}
|
|
}
|
|
|
|
// settledCodexStamp reports whether a comparison of this file, started at
|
|
// start, can be cached: it has a change time from before the current tick.
|
|
func settledCodexStamp(info os.FileInfo, start time.Time) bool {
|
|
c := codexChangeTime(info)
|
|
return !c.IsZero() && c.Before(start.Add(-codexStampSettle))
|
|
}
|
|
|
|
func sameCodexSnapshot(a, b os.FileInfo) bool {
|
|
if a == nil || b == nil || a.Size() != b.Size() || !a.ModTime().Equal(b.ModTime()) || !codexChangeTime(a).Equal(codexChangeTime(b)) {
|
|
return false
|
|
}
|
|
if wrapped, ok := a.(codexDiscoveryInfo); ok {
|
|
a = wrapped.FileInfo
|
|
}
|
|
if wrapped, ok := b.(codexDiscoveryInfo); ok {
|
|
b = wrapped.FileInfo
|
|
}
|
|
return os.SameFile(a, b)
|
|
}
|
|
|
|
// Only a proven decoded prefix is a redundant copy. A shared thread/name
|
|
// alone cannot establish ownership of divergent logs or distinct segments.
|
|
func distinctCodexFiles(files []file) []file {
|
|
// a name's first file, and the rest of its files, which few names
|
|
// have: a slice made for every name was ~45 MB of a minute of the
|
|
// page's polls (#2278)
|
|
first := make(map[string]int, len(files))
|
|
var more map[string][]int
|
|
keep := make([]bool, len(files))
|
|
for i, f := range files {
|
|
keep[i] = true
|
|
name := strings.TrimSuffix(filepath.Base(f.path), zstSuffix)
|
|
j0, seen := first[name]
|
|
if !seen {
|
|
first[name] = i
|
|
continue
|
|
}
|
|
for _, j := range slices.Concat([]int{j0}, more[name]) {
|
|
if !keep[j] {
|
|
continue
|
|
}
|
|
switch codexCopyRelation(files[j].path, f.path) {
|
|
case codexCopiesEqual, codexSecondPrefix:
|
|
keep[i] = false
|
|
case codexFirstPrefix:
|
|
keep[j] = false
|
|
}
|
|
if !keep[i] {
|
|
break
|
|
}
|
|
}
|
|
if more == nil {
|
|
more = map[string][]int{}
|
|
}
|
|
more[name] = append(more[name], i)
|
|
}
|
|
out := files[:0]
|
|
for i, f := range files {
|
|
if keep[i] {
|
|
out = append(out, f)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
func codexCopyRelation(first, second string) int {
|
|
start := codexDiscoveryNow()
|
|
a, errA := codexDiscoveryStat(first)
|
|
b, errB := codexDiscoveryStat(second)
|
|
if errA != nil || errB != nil {
|
|
return codexCopiesDiffer
|
|
}
|
|
key := codexCopyPair{first, second}
|
|
cacheable := settledCodexStamp(a, start) && settledCodexStamp(b, start)
|
|
codexCopyCache.Lock()
|
|
saved, found := codexCopyCache.Pairs[key]
|
|
codexCopyCache.Unlock()
|
|
if cacheable && found && sameCodexSnapshot(a, saved.First) && sameCodexSnapshot(b, saved.Second) {
|
|
return saved.Relation
|
|
}
|
|
relation, err := compareCodexCopies(first, second)
|
|
if err != nil {
|
|
return codexCopiesDiffer
|
|
}
|
|
// A comparison spanning an append/replacement is not a stable proof.
|
|
afterA, errA := codexDiscoveryStat(first)
|
|
afterB, errB := codexDiscoveryStat(second)
|
|
if errA != nil || errB != nil || !sameCodexSnapshot(a, afterA) || !sameCodexSnapshot(b, afterB) {
|
|
return codexCopiesDiffer
|
|
}
|
|
if cacheable {
|
|
codexCopyCache.Lock()
|
|
if len(codexCopyCache.Pairs) >= maxCodexCopyPairs {
|
|
for old := range codexCopyCache.Pairs {
|
|
delete(codexCopyCache.Pairs, old)
|
|
break
|
|
}
|
|
}
|
|
codexCopyCache.Pairs[key] = codexCopyResult{a, b, relation}
|
|
codexCopyCache.Unlock()
|
|
}
|
|
return relation
|
|
}
|
|
|
|
// Stream comparisons use constant memory. When one stream ends, consume the
|
|
// other to validate its compression trailer before selecting the longer copy.
|
|
func readCodexCopies(first, second string) (int, error) {
|
|
a, err := openLines(first)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
defer a.Close()
|
|
b, err := openLines(second)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
defer b.Close()
|
|
x, y := make([]byte, 32<<10), make([]byte, 32<<10)
|
|
for {
|
|
nx, endA, ex := readCodexChunk(a, x)
|
|
ny, endB, ey := readCodexChunk(b, y)
|
|
if ex != nil {
|
|
return 0, ex
|
|
}
|
|
if ey != nil {
|
|
return 0, ey
|
|
}
|
|
if !bytes.Equal(x[:min(nx, ny)], y[:min(nx, ny)]) {
|
|
return codexCopiesDiffer, nil
|
|
}
|
|
if nx < ny {
|
|
_, err = io.Copy(io.Discard, b)
|
|
return codexFirstPrefix, err
|
|
}
|
|
if ny < nx {
|
|
_, err = io.Copy(io.Discard, a)
|
|
return codexSecondPrefix, err
|
|
}
|
|
if endA && endB {
|
|
return codexCopiesEqual, nil
|
|
}
|
|
}
|
|
}
|
|
|
|
// io.ReadFull turns a clean short EOF into ErrUnexpectedEOF, making it
|
|
// indistinguishable from a truncated zstd frame. Preserve that distinction.
|
|
func readCodexChunk(r io.Reader, b []byte) (n int, end bool, err error) {
|
|
for n < len(b) {
|
|
read, err := r.Read(b[n:])
|
|
n += read
|
|
if err == io.EOF {
|
|
return n, true, nil
|
|
}
|
|
if err != nil {
|
|
return n, false, err
|
|
}
|
|
if read == 0 {
|
|
return n, false, io.ErrNoProgress
|
|
}
|
|
}
|
|
return n, false, nil
|
|
}
|