diff --git a/internal/usage/log_disk.go b/internal/usage/log_disk.go index 44bbb70ad..ac9ed9810 100644 --- a/internal/usage/log_disk.go +++ b/internal/usage/log_disk.go @@ -70,12 +70,29 @@ type logDiskProbe struct { } // logDiskRead is the file's snapshot of the log at path, when the log's -// first bytes are still the ones it was written of. +// first bytes are still the ones it was written of. Where the system maps +// the file, the blocks are of its pages, as requests-v1.bin's are +// (diskMapping): read into the heap, a 240k-call history's 7 MB of them +// stayed there for good, and the collector let the heap grow by as much +// again (#2278). func logDiskRead(path string, size int64) *logDisk { + if m, ok := mapDiskFile(logDiskPath()); ok { + var d *logDisk + if !readMapped(m, func() { d = logDiskOf(m.data, m, path, size) }) { + d = nil + } + m.read() + return d + } b, err := os.ReadFile(logDiskPath()) if err != nil { return nil } + return logDiskOf(b, nil, path, size) +} + +// logDiskOf is logDiskRead of the file's bytes b, of m's pages with m. +func logDiskOf(b []byte, m *diskMapping, path string, size int64) *logDisk { header, b, ok := cutDiskEntry(b) if !ok { return nil @@ -137,7 +154,7 @@ func logDiskRead(path string, size int64) *logDisk { if err != nil { return nil } - c.serial = nextChunkSerial() + c.serial, c.mapped = nextChunkSerial(), m d.blocks = append(d.blocks, c) } if len(b) != 0 { diff --git a/internal/usage/request_disk_map_unix_test.go b/internal/usage/request_disk_map_unix_test.go index 4eb490636..0cac12c80 100644 --- a/internal/usage/request_disk_map_unix_test.go +++ b/internal/usage/request_disk_map_unix_test.go @@ -251,3 +251,31 @@ func TestArchiveSpillFailedMadeAgain(t *testing.T) { t.Errorf("spill left: %v", err) } } + +// The log's full blocks a start reads from log-v1.bin are of the file's +// pages too, not of the heap (#2278). +func TestLogDiskBlocksMapped(t *testing.T) { + pageHome(t) + settleLogClock(t) + t.Cleanup(restartLog) + restartLog() + logLines(t, 0, 2*logBlockRows+300) + readLogSnapshot() + if _, err := os.Stat(logDiskPath()); err != nil { + t.Fatalf("the full blocks weren't kept: %v", err) + } + restartLog() + s := readLogSnapshot() + if len(s.blocks) < 2 { + t.Fatalf("%d blocks", len(s.blocks)) + } + for i, c := range s.blocks[:2] { + if c.mapped == nil { + t.Fatalf("block %d: read from the file into the heap", i) + } + 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.Fatalf("block %d: archive outside the mapping", i) + } + } +}