From 115ca34343c69daccfc768f44e8b9928dcfd56ec Mon Sep 17 00:00:00 2001 From: yetone Date: Sun, 25 Oct 2026 08:29:06 +0800 Subject: [PATCH] usage: the chunks' file is mapped, not read into the heap (#3493) A start read requests-v1.bin's 44MB of chunk archives into the heap, where they stayed: a fifth of the heap at rest, and with GOGC 25 a larger heap goal through every warm-up step. They are pointer-free and only ever decoded, so on unix the file is now mapped read-only (MAP_SHARED, PROT_READ) and each chunk's Archive is a capped slice of the mapping. A cleanup unmaps it once no chunk refers to it. The file is only replaced by a rename (toDisk), never written in place; a read past its end, if another program cut it short, is caught by readMapped (SetPanicOnFault) as a failed read, not a crash: the chunk unpacks as nil and reads as no rows, as a corrupt archive did before, until its session file changes or magpie starts again. Windows can't rename over a mapped file, so it reads the file as before (request_disk_map_other.go). decodeMapped keeps the decode's closure out of unpackReusing: with the closure there, data escaped to the heap and every row's take() through it was slower, which made step 2 0.12-0.2s slower than before (found by profile: unpackReusing.func2 0.28s -> 0.64s over three runs). Run at this tree, on the owner's real data (sandbox HOME with .claude/projects and .codex/sessions linked read-only, ~421k rows), warm runs interleaved with v0.1.1471 (b1f44d099), load 26-32: - phys_footprint_peak: 285-298MB -> 232-259MB - phys_footprint at rest: 221-227MB -> 176-179MB - peak live heap 200MB -> 157MB, heap at rest 198MB -> 147MB - step 2 (QueryPage All): 771-952ms -> 754-814ms; warm-up 1.68-1.96s -> 1.54-1.89s - every one of the 15624 mapped chunks unpacks to the same rows (reflect.DeepEqual, 359553 rows) as its archive copied to the heap - mmap of the file alone and a CRC pass: 6.5ms, read+CRC 8-10ms Tests: go test -timeout 45m ./internal/usage ./internal/sessions ./internal/gui ok; -race on the usage request/chunk/page/disk tests ok; gofmt clean; go vet -tags nogui with GOOS=linux and GOOS=windows ok. TestRequestDiskChunksMapped checks every chunk read from the file is of the mapping and that it is unmapped once forgotten; it is red with the mapping turned off. TestRequestDiskMappingCutShort is red ("fatal error: fault SIGBUS") with SetPanicOnFault off. Not tried: a Linux or Windows real-data run (Linux vet only; Windows keeps the old path); a file truncated under a running magpie outside the test. --- internal/usage/request_disk.go | 85 ++++++++++++++-- internal/usage/request_disk_map_other.go | 9 ++ internal/usage/request_disk_map_unix.go | 43 ++++++++ internal/usage/request_disk_map_unix_test.go | 100 +++++++++++++++++++ internal/usage/request_page.go | 2 + internal/usage/request_snapshot.go | 21 +++- 6 files changed, 251 insertions(+), 9 deletions(-) create mode 100644 internal/usage/request_disk_map_other.go create mode 100644 internal/usage/request_disk_map_unix.go create mode 100644 internal/usage/request_disk_map_unix_test.go diff --git a/internal/usage/request_disk.go b/internal/usage/request_disk.go index 71c7d01f5..a73db352a 100644 --- a/internal/usage/request_disk.go +++ b/internal/usage/request_disk.go @@ -12,6 +12,8 @@ import ( "io/fs" "os" "path/filepath" + "runtime" + "runtime/debug" "strings" "sync" "time" @@ -133,7 +135,10 @@ func diskEntry(c *rowChunk) []byte { b = appendDiskString(b, actor) } put(uint64(len(archive))) - return append(b, archive...) + if !readMapped(c.mapped, func() { b = append(b, archive...) }) { + return nil + } + return b } var errDiskEntry = errors.New("bad requests entry") @@ -217,10 +222,61 @@ func readDiskEntry(b []byte) (*rowChunk, error) { return c, nil } +// diskMapping is the file mapped into memory, where the system can map +// it: the chunks read from it are of its pages, not of the heap. Their +// 41MB of a 421k-call history were a fifth of a warm magpie's heap, and +// the collector let the heap grow by as much again before collecting +// (#3493). It is unmapped once no chunk read from it is left. +type diskMapping struct { + data []byte +} + +// diskUnmapped, in tests, is told each time a mapping is let go of. +var diskUnmapped func() + +// readMapped runs fn, which reads m's pages (none, with m nil). A fault +// reading them, the file cut short under the mapping by another program, +// is false and not a crash: magpie only ever renames a new file over it. +func readMapped(m *diskMapping, fn func()) (ok bool) { + if m == nil { + fn() + return true + } + defer debug.SetPanicOnFault(debug.SetPanicOnFault(true)) + defer func() { + if r := recover(); r != nil { + if _, fault := r.(runtime.Error); !fault { + panic(r) + } + ok = false + } + }() + fn() + runtime.KeepAlive(m) + return true +} + // readDiskEntries hands each entry of the file at path to each in turn, // while the file is of binary, root and meta. raw is the entry's bytes, -// whole, to be written on as they are. -func readDiskEntries(path, binary, root, meta string, each func(raw []byte) bool) { +// whole, to be written on as they are; with m, they are of m's pages, +// read-only, and good while m is. +func readDiskEntries(path, binary, root, meta string, each func(raw []byte, m *diskMapping) bool) { + if m, ok := mapDiskFile(path); ok { + readMapped(m, func() { + header, b, ok := cutDiskEntry(m.data) + if !ok || string(header) != string(diskHeader(binary, root, meta)) { + return + } + for { + var raw []byte + if raw, b, ok = cutDiskEntry(b); !ok || !each(raw, m) { + return + } + } + }) + m.read() + return + } in, err := os.Open(path) if err != nil { return @@ -233,12 +289,28 @@ func readDiskEntries(path, binary, root, meta string, each func(raw []byte) bool } for { raw, ok := nextDiskEntry(r) - if !ok || !each(raw) { + if !ok || !each(raw, nil) { return } } } +// cutDiskEntry is nextDiskEntry of b, and what follows it. The entry can't +// be appended to in place: b may be a read-only mapping. +func cutDiskEntry(b []byte) (entry, rest []byte, ok bool) { + n, k := binary.Uvarint(b) + if k <= 0 || n == 0 || n > 256<<20 || uint64(len(b)-k) < 4+n { + return nil, nil, false + } + sum := binary.LittleEndian.Uint32(b[k:]) + end := k + 4 + int(n) + entry = b[k+4 : end : end] + if crc32.ChecksumIEEE(entry) != sum { + return nil, nil, false + } + return entry, b[end:], true +} + func nextDiskEntry(r *bufio.Reader) ([]byte, bool) { n, err := binary.ReadUvarint(r) if err != nil || n == 0 || n > 256<<20 { @@ -269,11 +341,12 @@ func writeDiskEntry(w *bufio.Writer, b []byte) error { // fromDisk puts in idx the chunks the file holds of the sources in want, // each still the size and mtime it was. idx is a private copy (private). func (idx *requestIndex) fromDisk(want map[string]sessions.CallSource) { - readDiskEntries(requestDiskPath(), requestDiskBinary(), idx.root, idx.meta, func(raw []byte) bool { + readDiskEntries(requestDiskPath(), requestDiskBinary(), idx.root, idx.meta, func(raw []byte, m *diskMapping) bool { c, err := readDiskEntry(raw) if err != nil { return false } + c.mapped = m s, ok := want[c.Source.Path] if !ok || s.Size != c.Source.Size || !s.Modified.Equal(c.Source.Modified) { return true @@ -309,7 +382,7 @@ func toDisk(root, meta string, chunks map[string]*rowChunk, on map[string]bool) } } if err == nil { - readDiskEntries(path, binary, root, meta, func(raw []byte) bool { + readDiskEntries(path, binary, root, meta, func(raw []byte, _ *diskMapping) bool { p, ok := diskEntryPath(raw) if !ok { return false diff --git a/internal/usage/request_disk_map_other.go b/internal/usage/request_disk_map_other.go new file mode 100644 index 000000000..09a45a945 --- /dev/null +++ b/internal/usage/request_disk_map_other.go @@ -0,0 +1,9 @@ +//go:build !unix + +package usage + +// mapDiskFile maps nothing: on Windows a file mapped can't be renamed over, +// and toDisk replaces the file so. The file is read into memory instead. +func mapDiskFile(string) (*diskMapping, bool) { return nil, false } + +func (m *diskMapping) read() {} diff --git a/internal/usage/request_disk_map_unix.go b/internal/usage/request_disk_map_unix.go new file mode 100644 index 000000000..8aeff620a --- /dev/null +++ b/internal/usage/request_disk_map_unix.go @@ -0,0 +1,43 @@ +//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 diskUnmapped != nil { + diskUnmapped() + } + }, 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) +} diff --git a/internal/usage/request_disk_map_unix_test.go b/internal/usage/request_disk_map_unix_test.go new file mode 100644 index 000000000..b83383a8e --- /dev/null +++ b/internal/usage/request_disk_map_unix_test.go @@ -0,0 +1,100 @@ +//go:build unix + +package usage + +import ( + "fmt" + "os" + "path/filepath" + "runtime" + "sync/atomic" + "testing" + "time" + "unsafe" + + "github.com/yetone/magpie/internal/sessions" +) + +// The chunks a start reads from the file are of the file's pages, not of +// the heap (#3493), and the pages are let go of once no chunk is of them. +func TestRequestDiskChunksMapped(t *testing.T) { + pageHome(t) + now := holdClock(t, time.Date(2026, 9, 30, 12, 0, 0, 0, time.Local)) + least := requestDiskLeast + requestDiskLeast = 2 + t.Cleanup(func() { requestDiskLeast = least }) + var unmapped atomic.Int32 + diskUnmapped = func() { unmapped.Add(1) } + t.Cleanup(func() { diskUnmapped = nil }) + dir := filepath.Join(sessions.CodexDir(), "sessions", "2026", "09", "30") + for i := range 4 { + writeCodexCall(t, filepath.Join(dir, fmt.Sprintf("rollout-2026-09-30T10-00-0%d-s%d.jsonl", i, i)), fmt.Sprintf("s%d", i), fmt.Sprintf("native-%d", i), `"cli"`, fmt.Sprintf("resp-%d", i), "m", "high", now.Add(-time.Duration(i+1)*time.Hour), 10*(i+1), i) + } + forget := func() { + requestDiskSaves.Wait() + requestCache.Lock() + requestCache.chunks = map[string]*rowChunk{} + requestCache.pages, requestCache.parts, requestCache.pairs, requestCache.heats = nil, nil, nil, nil + requestCache.key = "" + requestCache.Unlock() + sessions.Reset() + } + first := queryPage(All, Filter{}, 0, 100, sessions.ReadCallSource) + forget() + equalPage(t, queryPage(All, Filter{}, 0, 100, sessions.ReadCallSource), first) + + requestCache.Lock() + mapped := 0 + for _, c := range requestCache.chunks { + if len(c.Archive) == 0 { + continue + } + if c.mapped == nil { + t.Errorf("%s: read from the file into the heap", filepath.Base(c.Source.Path)) + continue + } + at, start := uintptr(unsafe.Pointer(unsafe.SliceData(c.Archive))), uintptr(unsafe.Pointer(unsafe.SliceData(c.mapped.data))) + if at < start || at+uintptr(len(c.Archive)) > start+uintptr(len(c.mapped.data)) { + t.Errorf("%s: archive outside the mapping", filepath.Base(c.Source.Path)) + } + mapped++ + } + requestCache.Unlock() + if mapped != 4 { + t.Fatalf("%d chunks of the file's pages, want 4", mapped) + } + + was := unmapped.Load() + forget() + for deadline := time.Now().Add(30 * time.Second); unmapped.Load() == was; { + if time.Now().After(deadline) { + t.Fatal("the mapping outlived every chunk of it") + } + runtime.GC() + runtime.Gosched() + } +} + +// A file cut short under its mapping by another program is a read that +// failed, not a crash. +func TestRequestDiskMappingCutShort(t *testing.T) { + path := filepath.Join(t.TempDir(), "requests-v1.bin") + if err := os.WriteFile(path, make([]byte, 1<<20), 0o600); err != nil { + t.Fatal(err) + } + m, ok := mapDiskFile(path) + if !ok { + t.Fatal("not mapped") + } + var last byte + if !readMapped(m, func() { last = m.data[len(m.data)-1] }) || last != 0 { + t.Fatal("a whole file's read failed") + } + if err := os.Truncate(path, 0); err != nil { + t.Fatal(err) + } + if readMapped(m, func() { last = m.data[len(m.data)-1] }) { + t.Fatal("a read past the cut passed") + } + runtime.KeepAlive(m) +} diff --git a/internal/usage/request_page.go b/internal/usage/request_page.go index afb007b42..1b3b16ba0 100644 --- a/internal/usage/request_page.go +++ b/internal/usage/request_page.go @@ -124,6 +124,8 @@ type rowChunk struct { // serial names the chunk as made, for the cells a page keeps of it // (chunkSerial) serial uint64 + // mapped is the file Archive was read from, when it is of its pages + mapped *diskMapping } // rowMsg is the Text of a row's Claude message id, after rowText's, and diff --git a/internal/usage/request_snapshot.go b/internal/usage/request_snapshot.go index 36c2290c1..8267a1829 100644 --- a/internal/usage/request_snapshot.go +++ b/internal/usage/request_snapshot.go @@ -131,6 +131,21 @@ func (k keptStrings) of(s string) string { return kept } +// decodeMapped is s2.Decode of src, of m's pages (readMapped). It is not +// unpackReusing's own closure: data caught by one is on the heap, and +// every read of a row's fields through it is slower. +func decodeMapped(m *diskMapping, dst, src []byte) (data []byte, ok bool) { + var err error + if m == nil { + data, err = s2.Decode(dst, src) + return data, err == nil + } + if !readMapped(m, func() { data, err = s2.Decode(dst, src) }) { + return nil, false + } + return data, err == nil +} + // unpackInto is unpack decoding the rows into rows when it is large enough: // every field of a row is written, so a reused slice needs no clearing. func (c *rowChunk) unpackInto(into []packedRow) *rowChunk { @@ -151,8 +166,8 @@ func (c *rowChunk) unpackReusing(into []packedRow, strs []string, shapes [][rowT buf = rowDecodes.Get().(*[]byte) defer rowDecodes.Put(buf) } - data, err := s2.Decode((*buf)[:cap(*buf)], c.Archive) - if err != nil { + data, ok := decodeMapped(c.mapped, (*buf)[:cap(*buf)], c.Archive) + if !ok { return nil } *buf = data @@ -171,7 +186,7 @@ func (c *rowChunk) unpackReusing(into []packedRow, strs []string, shapes [][rowT return nil } next := *c - next.Archive, next.Locations = nil, nil + next.Archive, next.Locations, next.mapped = nil, nil, nil if uint64(cap(into)) >= rows { next.Rows = into[:rows] } else {