Files
magpie/internal/usage/request_disk_map_unix.go
T
yetone 9dc7d6eb84 usage: a large build's archives are held in a spill file as they are made (#3493)
A cold build (first start, or after an update that changes internal/usage)
kept every chunk's archive on the heap until save had written
requests-v1.bin and swapped them for the file's pages: 31MB for 15.7k
sessions, the largest thing a cold peak held over a warm start's.

Now a build that wants requestDiskLeast or more files written writes each
archive to requests-*.spill as the chunk is made, lets go of the heap copy,
and maps the file once the build is done (unlinked at once). save then
swaps the spill's pages for requests-v1.bin's as before: pagesRank orders
heap < spill < file, and keep and save take the later. A spill that can't
be written or mapped has each of its chunks made again from its source, so
none is left without an archive. Windows maps no file, so nothing changes
there.

Run, real data (15.7k Claude Code + Codex sessions, sandbox HOME, copies of
the call shards, requests-v1.bin removed before each run), the gui warm-up
harness alternating old (cbbf3163c) and new, 4 rounds each:
  phys_footprint_peak old 396 353 346 352 MB, new 319 322 329 329 MB
  peak live (gctrace) old 209-259 MB, new 188-205 MB
  at rest unchanged (184-205 MB), warm-up time in the same range.
  gctrace: cold max live after mark is now 187MB against warm's 173MB; what
  is left of the gap is allocation churn (1.2GB allocated in the cold
  build against 280MB warm), not held data.
go test ./internal/usage ./internal/gui ok; -race -count=3 of the disk and
spill tests ok; GOOS=linux and windows go vet -tags nogui ok; gofmt clean.
Broken by hand: no spill put -> TestRequestDiskBuildSpills red; done not
making a chunk again -> TestArchiveSpillFailedMadeAgain red; the spill
ranked as the file -> TestRequestDiskBuildSpills red after the save.
Not tried: Windows (no mapping, path unchanged), a disk that fills while
spilling (the made-again path is covered by the unit test only).
2026-10-25 16:56:02 +08:00

48 lines
1.2 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.
// mapsFiles: the system maps a file, and archiveSpill holds a build's
// archives in one.
const mapsFiles = true
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)
}