diff --git a/internal/usage/heat_digest_disk.go b/internal/usage/heat_digest_disk.go index 24672fa4f..20276c28f 100644 --- a/internal/usage/heat_digest_disk.go +++ b/internal/usage/heat_digest_disk.go @@ -28,6 +28,11 @@ var sourceDigestsDisk struct { path string // where kept was read from kept digestList // as read: no map of 17k entries beside sourceDigests' dirty bool // a digest was made that isn't on disk + // mapped holds the file kept is read from, where the system maps + // files: its pages are the file's, not the heap's, and a lookup + // touches a few of them. The file is 40 bytes a digest, up to + // 2*sourceDigestsKept of them (5MB), each kept in the heap before. + mapped *diskMapping } // sourceDigestsHashed counts the chunks sourceDigest hashed, for tests. @@ -80,6 +85,13 @@ func sourceDigestsLoad() { return } sourceDigestsDisk.path, sourceDigestsDisk.kept, sourceDigestsDisk.dirty = p, nil, false + sourceDigestsDisk.mapped = nil + if m, ok := mapDiskFile(p); ok { + if l := decodeSourceDigests(m.data, requestDiskBinary()); l != nil { + sourceDigestsDisk.kept, sourceDigestsDisk.mapped = l, m + } + return + } b, err := os.ReadFile(p) if err != nil { return @@ -87,6 +99,21 @@ func sourceDigestsLoad() { sourceDigestsDisk.kept = decodeSourceDigests(b, requestDiskBinary()) } +// keepMapped reads kept again from p as just written, mapped: the bytes +// encoded are let go of. Kept as encoded when the file isn't b (another +// write since) or can't be mapped. Under sourceDigests' lock. +func keepMapped(p string, b []byte) { + if sourceDigestsDisk.path != p || len(sourceDigestsDisk.kept) == 0 || &sourceDigestsDisk.kept[0] != &b[len(b)-len(sourceDigestsDisk.kept)] { + return // kept is no longer what b holds + } + m, ok := mapDiskFile(p) + if !ok || !bytes.Equal(m.data, b) { + return + } + sourceDigestsDisk.kept = decodeSourceDigests(m.data, requestDiskBinary()) + sourceDigestsDisk.mapped = m +} + // decodeSourceDigests is b's digests when b was written for code, nil // when not, when b is cut short or out of order. func decodeSourceDigests(b []byte, code string) digestList { @@ -191,19 +218,25 @@ func sourceDigestsSave() { return } w.wrote = gen - writeFileRenamed(p, b) + if !writeFileRenamed(p, b) || !mapsFiles { + return + } + d.Lock() + keepMapped(p, b) + d.Unlock() } // writeFileRenamed writes b to p through a temp file renamed over it. -func writeFileRenamed(p string, b []byte) { +// It reports whether p is b now. +func writeFileRenamed(p string, b []byte) bool { saveAsks() // the asks its rows were made of first (catalogBase) dir := filepath.Dir(p) if os.MkdirAll(dir, 0o700) != nil { - return + return false } tmp, err := os.CreateTemp(dir, ".heat-digests-*") if err != nil { - return + return false } _, err = tmp.Write(b) if cerr := tmp.Close(); err == nil { @@ -211,5 +244,7 @@ func writeFileRenamed(p string, b []byte) { } if err != nil || os.Rename(tmp.Name(), p) != nil { os.Remove(tmp.Name()) + return false } + return true } diff --git a/internal/usage/heat_digest_disk_test.go b/internal/usage/heat_digest_disk_test.go index e9ec27b45..6dd015646 100644 --- a/internal/usage/heat_digest_disk_test.go +++ b/internal/usage/heat_digest_disk_test.go @@ -26,7 +26,7 @@ func TestSourceDigestsKeptAcrossStarts(t *testing.T) { restart := func() { sourceDigests.Lock() sourceDigests.by, sourceDigests.last, sourceDigests.made = nil, nil, nil - sourceDigestsDisk.path, sourceDigestsDisk.kept = "", nil + sourceDigestsDisk.path, sourceDigestsDisk.kept, sourceDigestsDisk.mapped = "", nil, nil sourceDigests.Unlock() } key := func(cs ...*rowChunk) (string, int) { @@ -54,11 +54,23 @@ func TestSourceDigestsKeptAcrossStarts(t *testing.T) { if len(sourceDigests.made) != 0 || sourceDigestsDisk.kept.len() != 2 { t.Fatalf("after a save: %d made, %d kept", len(sourceDigests.made), sourceDigestsDisk.kept.len()) } + if mapsFiles && sourceDigestsDisk.mapped == nil { + t.Fatal("after a save the digests are kept as encoded, not from the file written") + } sourceDigests.Unlock() restart() if again, n := key(chunk(1<<63|1, 10), chunk(1<<63|2, 20)); n != 0 || again != first { t.Fatalf("started again, hashed %d chunks, key %s, was %s", n, again, first) } + // what was read is the file mapped, not a copy in the heap + if mapsFiles { + sourceDigests.Lock() + m, kept := sourceDigestsDisk.mapped, sourceDigestsDisk.kept + sourceDigests.Unlock() + if m == nil || kept.len() != 2 || &kept[0] != &m.data[len(m.data)-len(kept)] { + t.Fatalf("kept %d digests, mapped %v: not read from the mapped file", kept.len(), m != nil) + } + } // a start that met one file keeps the other's for the next restart() if _, n := key(chunk(1<<63|1, 10)); n != 0 {