Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.
usage: the chunks' file is mapped, not read into the heap (#3493)
yetone115ca34343c6
diff --git a/internal/usage/request_disk.go b/internal/usage/request_disk.goindex 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 = ms, 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 falsediff --git a/internal/usage/request_disk_map_other.go b/internal/usage/request_disk_map_other.gonew file mode 100644index 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.gonew file mode 100644index 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.gonew file mode 100644index 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.goindex 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, anddiff --git a/internal/usage/request_snapshot.go b/internal/usage/request_snapshot.goindex 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 [][rowTbuf = 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 [][rowTreturn nil}next := *c- next.Archive, next.Locations = nil, nil+ next.Archive, next.Locations, next.mapped = nil, nil, nilif uint64(cap(into)) >= rows {next.Rows = into[:rows]} else {