A start that reads requests-v1.bin keeps each chunk's archive in the
file's mapped pages, not on the heap. A start that builds the chunks
(a cold one: no file, or one of another build) kept the archives it
encoded on the heap until the next start, after writing the very same
bytes to the file: 32-40MB for 15.7k sessions, and a resting heap
~50MB over a warm start's.
Once save has written the file, onDisk maps it and makes, for each
chunk whose archive is of the heap, a copy whose archive is the same
bytes of the mapping (checked equal). save swaps it in for the chunk
of the same serial: a read since has copied the chunk with a use of
its own, so the pointer it wrote is gone by then. A read that copied
the chunks before the swap and finishes after it keeps the mapped
archive (keep), or it would put the heap one back. Nothing changes
where no file is mapped (Windows).
diskUnmapped, the tests' hook, is an atomic.Pointer: a mapping's
cleanup reads it on its own goroutine while the next test sets it,
which -race -count=4 caught once the new test let go of mappings.
Run at this tree (67a137744 + this change):
- go test -count=1 -timeout 45m ./internal/usage/ ./internal/sessions/:
ok. go test -run 'Warm|Session|Request|Usage' ./internal/gui/
./internal/gateway/: ok.
- go test -race -count=4 -run TestRequestDisk ./internal/usage/: ok,
six times in a row.
- GOOS=linux and GOOS=windows go vet -tags nogui ./internal/usage/: ok;
gofmt -l clean.
- TestRequestDiskBuiltChunksMapped holds the write while a second page
copies the chunks and a third copies them before the write and keeps
them after it. It went red (0 of 4 mapped) with the swap matched by
pointer instead of serial, and with keep not carrying the mapping.
- The real warm-up (gui warmSteps in sessions.Warm) on a copy of the
owner's ~/.claude and ~/.codex sessions, cold (requests-v1.bin
removed), parent and change alternating, 3 runs each, measured 5s
after the warm-up:
- heap at rest: 200/200/198MB before, 154/148/152MB after;
- phys_footprint at rest: 233/234/237MB before, 198/197/205MB after;
- phys_footprint_peak: 361/396/347MB before, 335/367/378MB after,
the same within noise (the build itself is unchanged).
Not tried: Windows (it maps no file, so it is unchanged there).
44 lines
1.0 KiB
Go
44 lines
1.0 KiB
Go
//go:build unix
|
|
|
|
package usage
|
|
|
|
import (
|
|
"os"
|
|
"runtime"
|
|
|
|
"golang.org/x/sys/unix"
|
|
)
|
|
|
|
// mapDiskFile maps the file at path read-only. The file is only ever
|
|
// replaced by a rename (toDisk), never written in place, so what is
|
|
// mapped stays as it was read while the mapping lives.
|
|
func mapDiskFile(path string) (*diskMapping, bool) {
|
|
f, err := os.Open(path)
|
|
if err != nil {
|
|
return nil, false
|
|
}
|
|
defer f.Close()
|
|
info, err := f.Stat()
|
|
if err != nil || info.Size() <= 0 || info.Size() != int64(int(info.Size())) {
|
|
return nil, false
|
|
}
|
|
data, err := unix.Mmap(int(f.Fd()), 0, int(info.Size()), unix.PROT_READ, unix.MAP_SHARED)
|
|
if err != nil {
|
|
return nil, false
|
|
}
|
|
m := &diskMapping{data: data}
|
|
runtime.AddCleanup(m, func(data []byte) {
|
|
unix.Munmap(data)
|
|
if f := diskUnmapped.Load(); f != nil {
|
|
(*f)()
|
|
}
|
|
}, data)
|
|
return m, true
|
|
}
|
|
|
|
// read lets the system drop the pages a read of every entry touched: they
|
|
// are the file's, and read again from it when a chunk is decoded.
|
|
func (m *diskMapping) read() {
|
|
unix.Madvise(m.data, unix.MADV_DONTNEED)
|
|
}
|