usage: chunk digests in use are named by serial, read from the kept list (#2278)
sourceDigests held every chunk's 32-byte digest in a map (by, and last
for the turn before), beside heat-digests.bin's sorted list that already
holds them: ~2-3 MB of maps on a 17k-file history. by and last now hold
only the serials used, which decide what the kept file keeps first; a
digest is read from the kept list, or from made, the few hashed since
the last save. A save encodes the list and keeps what it wrote as the
list, so made is empty again after each heat key.
Measured on mote, real-account sandbox (17k chunks), pageheap.sh, page
open, w52v (v0.1.3983) against w53v (this), 3 runs each:
liveHeap 79.34 / 79.57 / 79.63 MB -> 78.77 / 78.64 / 77.99 MB
Footprint 133 / 134 / 133 MB -> 130 / 132 / 126 MB
heap profile, sourceDigest cum: 2.99 MB -> 0.90 MB (the list read)
Run: go vet ./internal/usage; go test -count=1 ./internal/usage ok (mote).
Broken by hand: not putting a hashed digest in made and not keeping the
written list made TestSourceDigestsKeptAcrossStarts red ("same run,
hashed 2 chunks"). New in the test: a second key in the same run hashes
nothing and leaves made empty; a used serial with no digest is left out.
Not tried: a history past 2*65536 chunks (the cap is unchanged).
This commit is contained in:
@@ -52,17 +52,23 @@ func (l digestList) at(i int) (uint64, []byte) {
|
||||
return binary.LittleEndian.Uint64(e), e[8:]
|
||||
}
|
||||
|
||||
func (l digestList) get(serial uint64) (sum [sha256.Size]byte, ok bool) {
|
||||
// find is serial's index in l.
|
||||
func (l digestList) find(serial uint64) (int, bool) {
|
||||
i := sort.Search(l.len(), func(i int) bool { s, _ := l.at(i); return s >= serial })
|
||||
if i == l.len() {
|
||||
return sum, false
|
||||
return i, false
|
||||
}
|
||||
s, b := l.at(i)
|
||||
if s != serial {
|
||||
return sum, false
|
||||
s, _ := l.at(i)
|
||||
return i, s == serial
|
||||
}
|
||||
|
||||
func (l digestList) get(serial uint64) (sum [sha256.Size]byte, ok bool) {
|
||||
i, ok := l.find(serial)
|
||||
if ok {
|
||||
_, b := l.at(i)
|
||||
copy(sum[:], b)
|
||||
}
|
||||
copy(sum[:], b)
|
||||
return sum, true
|
||||
return sum, ok
|
||||
}
|
||||
|
||||
// sourceDigestsLoad reads the kept digests, once for each place they are
|
||||
@@ -104,9 +110,11 @@ func decodeSourceDigests(b []byte, code string) digestList {
|
||||
return l
|
||||
}
|
||||
|
||||
// encodeSourceDigests writes the sets' digests and kept's for code, the
|
||||
// first set's first, at most limit of them.
|
||||
func encodeSourceDigests(code string, limit int, kept digestList, sets ...map[uint64][sha256.Size]byte) []byte {
|
||||
// encodeSourceDigests writes, for code, the digests of the serials the
|
||||
// sets name, the first set's first, then the rest of kept's, at most limit
|
||||
// of them. A serial's digest is made's, or kept's; one in neither is left
|
||||
// out.
|
||||
func encodeSourceDigests(code string, limit int, kept digestList, made map[uint64][sha256.Size]byte, sets ...map[uint64]struct{}) []byte {
|
||||
type one struct {
|
||||
serial uint64
|
||||
sum []byte
|
||||
@@ -114,10 +122,17 @@ func encodeSourceDigests(code string, limit int, kept digestList, sets ...map[ui
|
||||
var all []one
|
||||
seen := map[uint64]bool{}
|
||||
for _, s := range sets {
|
||||
for serial, sum := range s {
|
||||
if !seen[serial] && len(all) < limit {
|
||||
for serial := range s {
|
||||
if seen[serial] || len(all) >= limit {
|
||||
continue
|
||||
}
|
||||
if sum, ok := made[serial]; ok {
|
||||
seen[serial] = true
|
||||
all = append(all, one{serial, append([]byte(nil), sum[:]...)})
|
||||
} else if i, ok := kept.find(serial); ok {
|
||||
seen[serial] = true
|
||||
_, sum := kept.at(i)
|
||||
all = append(all, one{serial, sum})
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -160,7 +175,11 @@ func sourceDigestsSave() {
|
||||
// this run's first, then those kept from before it it hasn't met (a
|
||||
// query of a few files keeps the rest): a file gone falls out once
|
||||
// two turns' worth are made
|
||||
b := encodeSourceDigests(requestDiskBinary(), 2*sourceDigestsKept, sourceDigestsDisk.kept, d.by, d.last)
|
||||
code := requestDiskBinary()
|
||||
b := encodeSourceDigests(code, 2*sourceDigestsKept, sourceDigestsDisk.kept, d.made, d.by, d.last)
|
||||
// what is written is what is kept: the digests made are in it
|
||||
sourceDigestsDisk.kept = decodeSourceDigests(b, code)
|
||||
d.made = nil
|
||||
p := sourceDigestsDisk.path
|
||||
w := &sourceDigestWrites
|
||||
w.made++
|
||||
|
||||
@@ -25,7 +25,7 @@ func TestSourceDigestsKeptAcrossStarts(t *testing.T) {
|
||||
}
|
||||
restart := func() {
|
||||
sourceDigests.Lock()
|
||||
sourceDigests.by, sourceDigests.last = nil, nil
|
||||
sourceDigests.by, sourceDigests.last, sourceDigests.made = nil, nil, nil
|
||||
sourceDigestsDisk.path, sourceDigestsDisk.kept = "", nil
|
||||
sourceDigests.Unlock()
|
||||
}
|
||||
@@ -46,6 +46,15 @@ func TestSourceDigestsKeptAcrossStarts(t *testing.T) {
|
||||
if n != 2 {
|
||||
t.Fatalf("a first start hashed %d chunks, want 2", n)
|
||||
}
|
||||
// the same run again: what was made is kept, not hashed again
|
||||
if again, n := key(chunk(1<<63|1, 10), chunk(1<<63|2, 20)); n != 0 || again != first {
|
||||
t.Fatalf("same run, hashed %d chunks, key %s, was %s", n, again, first)
|
||||
}
|
||||
sourceDigests.Lock()
|
||||
if len(sourceDigests.made) != 0 || sourceDigestsDisk.kept.len() != 2 {
|
||||
t.Fatalf("after a save: %d made, %d kept", len(sourceDigests.made), sourceDigestsDisk.kept.len())
|
||||
}
|
||||
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)
|
||||
@@ -69,7 +78,7 @@ func TestSourceDigestsKeptAcrossStarts(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
other := encodeSourceDigests(requestDiskBinary()+"x", 10, decodeSourceDigests(b, requestDiskBinary()))
|
||||
other := encodeSourceDigests(requestDiskBinary()+"x", 10, decodeSourceDigests(b, requestDiskBinary()), nil)
|
||||
if decodeSourceDigests(b, requestDiskBinary()).len() != 3 {
|
||||
t.Fatalf("kept %d, want 3", decodeSourceDigests(b, requestDiskBinary()).len())
|
||||
}
|
||||
@@ -91,7 +100,12 @@ func TestSourceDigestsKeptAcrossStarts(t *testing.T) {
|
||||
t.Fatalf("no code: %v", got)
|
||||
}
|
||||
var sum [sha256.Size]byte
|
||||
if got := decodeSourceDigests(encodeSourceDigests("c", 1, nil, map[uint64][sha256.Size]byte{1: sum, 2: sum}), "c"); got.len() != 1 {
|
||||
t.Fatalf("limit 1 kept %d", len(got))
|
||||
made := map[uint64][sha256.Size]byte{1: sum, 2: sum}
|
||||
if got := decodeSourceDigests(encodeSourceDigests("c", 1, nil, made, map[uint64]struct{}{1: {}, 2: {}}), "c"); got.len() != 1 {
|
||||
t.Fatalf("limit 1 kept %d", got.len())
|
||||
}
|
||||
// a serial used with no digest made or kept is left out
|
||||
if got := decodeSourceDigests(encodeSourceDigests("c", 9, nil, map[uint64][sha256.Size]byte{1: sum}, map[uint64]struct{}{1: {}, 2: {}}), "c"); got.len() != 1 {
|
||||
t.Fatalf("kept %d, want 1", got.len())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -151,7 +151,13 @@ func heatSettledKey(d heatDiskRef, cut, since time.Time, f Filter, identities ma
|
||||
// serial: those of the keys made since the last turn, and before it.
|
||||
var sourceDigests struct {
|
||||
sync.Mutex
|
||||
by, last map[uint64][sha256.Size]byte
|
||||
// by and last are the serials used since the last turn, and before it,
|
||||
// which the kept file holds first. Their digests are in made, or in
|
||||
// what was kept on disk: not a map of 17k digests beside the list read
|
||||
// (#2278).
|
||||
by, last map[uint64]struct{}
|
||||
// made are the digests hashed that aren't kept on disk yet
|
||||
made map[uint64][sha256.Size]byte
|
||||
}
|
||||
|
||||
// sourceDigestsKept is how many digests a turn holds, more than the
|
||||
@@ -169,10 +175,7 @@ const sourceDigestsKept = 1 << 16
|
||||
func sourceDigest(c *rowChunk, rows *chunkRows) [sha256.Size]byte {
|
||||
d := &sourceDigests
|
||||
d.Lock()
|
||||
sum, ok := d.by[c.serial]
|
||||
if !ok {
|
||||
sum, ok = d.last[c.serial]
|
||||
}
|
||||
sum, ok := d.made[c.serial]
|
||||
if !ok {
|
||||
sourceDigestsLoad()
|
||||
sum, ok = sourceDigestsDisk.kept.get(c.serial)
|
||||
@@ -189,11 +192,15 @@ func sourceDigest(c *rowChunk, rows *chunkRows) [sha256.Size]byte {
|
||||
if made {
|
||||
sourceDigestsHashed++
|
||||
sourceDigestsDisk.dirty = true
|
||||
if d.made == nil {
|
||||
d.made = map[uint64][sha256.Size]byte{}
|
||||
}
|
||||
d.made[c.serial] = sum
|
||||
}
|
||||
if d.by == nil || len(d.by) >= sourceDigestsKept {
|
||||
d.last, d.by = d.by, map[uint64][sha256.Size]byte{}
|
||||
d.last, d.by = d.by, map[uint64]struct{}{}
|
||||
}
|
||||
d.by[c.serial] = sum
|
||||
d.by[c.serial] = struct{}{}
|
||||
d.Unlock()
|
||||
return sum
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user