usage: heat-digests.bin is read mapped, not copied into the heap
The heatmap's key names each session file's chunk by a digest kept on disk by serial (#2278). The file holds up to 2*sourceDigestsKept digests at 40 bytes each, 5MB on a long history, and every start read it whole into the heap and kept it there for the process's life. After a save the encoded bytes stayed instead, the same 5MB. Where the system maps files (unix), the file is now mapped read-only, as requests-v1.bin already is (mapDiskFile), and kept is read from the mapping: its pages are clean file pages, not footprint, and a lookup is a binary search that touches a few of them. After a save the file just written is mapped in place of the encoded bytes, when it is still those bytes. The file is only ever replaced by a rename, so a mapping stays as it was read. Windows reads it into memory as before (mapsFiles). Run (mica, go1.26.8): - go test ./internal/usage/: ok (54s) - go vet ./internal/usage/, GOOS=windows go vet ./internal/usage/: clean - Broken by hand (no mapping on load, no keepMapped after save): TestSourceDigestsKeptAcrossStarts red, "after a save the digests are kept as encoded"; green again with the change. Real account (mote, the owner's history in a sandbox HOME, magpie web with the page open, 90s idle, footprint + Go MemStats): - before (w63 = v0.1.4074's code): footprint 109MB, live heap 44.9MB - after: footprint 101MB, live heap 39.6MB - wsl-run and win-run: go test -run 'SourceDigest|Heat' ./internal/usage/ ok on Linux (the mapped path) and Windows (read into memory). Not run: the full ./... suite (only internal/usage is changed).
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user