usage: requests-v1.bin is of the code that makes rows, not of the binary
The file of the session files' chunks was named by the binary's own
stat key, so every upgrade, and magpie ships several a day, read every
session file again on its first Requests page: on a 15.6k-shard history
warm-up step 2 went from ~0.8-0.9s (read from the file) to 1.6-1.9s and
~1.3GB more allocation. It is named now by the call shards' version
(sessions.CallCacheVersion, which already decides whether a shard of the
calls is read) and a SHA-256 of this package's non-test source, which
the binary carries (go:embed *.go, ~750KB with the tests, which the name
leaves out). Of the last 99 releases, 17 changed that source; the other
82 keep the file. Prices, names and settings are in the file's meta as
before. When the embedded source can't be read the name is the binary's,
as it was.
Measured (real history, sbpairs): one test binary wrote the file, a
second binary at another path (the old key differs) read it: step 2
0.79s and 1001MB alloc, against 1.63s and 2285MB cold.
Run at this tree (89dc16476 + this change):
- go test -race -count=1 ./internal/usage: ok
- go test -count=1 ./internal/sessions ./internal/gui: ok
- GOOS=linux and GOOS=windows go vet -tags nogui ./internal/usage
./internal/sessions, and go build -tags nogui .: ok; gofmt -l clean
- TestRequestDiskNamesItsSource hashes the package's files from disk
and wants the same name. Broken by hand (embed request_*.go only):
red; restored: green.
Not tried: an upgrade of the real app from one release to the next.
This commit is contained in:
@@ -44,6 +44,10 @@ import (
|
||||
// the version is part of the directory's name, so a cache written by an older
|
||||
// one is never read (pruneCalls).
|
||||
const callCacheVersion = "calls-v15"
|
||||
|
||||
// CallCacheVersion is callCacheVersion, for a cache of what is made of the
|
||||
// calls (usage's requests-v1.bin).
|
||||
const CallCacheVersion = callCacheVersion
|
||||
const maxKeptCalls = 131072
|
||||
const maxKeptFiles = 64
|
||||
|
||||
|
||||
@@ -2,12 +2,18 @@ package usage
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"crypto/sha256"
|
||||
"embed"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"hash/crc32"
|
||||
"io"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/yetone/magpie/internal/sessions"
|
||||
@@ -19,7 +25,7 @@ import (
|
||||
// could show, and so did a page asked for once two idle minutes had trimmed
|
||||
// the chunks (#3493). An entry stands for its session file while the file's
|
||||
// size and mtime are the same, as a call shard does, and the whole file
|
||||
// while the binary that wrote it and what prices and names a call (the
|
||||
// while the code that wrote it and what prices and names a call (the
|
||||
// index's root and meta) are the same.
|
||||
//
|
||||
// The file is a header, then entries one after another, each its length,
|
||||
@@ -32,15 +38,44 @@ func requestDiskPath() string {
|
||||
return filepath.Join(filepath.Dir(sessions.CachePath()), "requests-v1.bin")
|
||||
}
|
||||
|
||||
// requestDiskBinary names the binary a file was written by: a new build may
|
||||
// make a row of a call differently.
|
||||
var requestDiskBinary = func() string {
|
||||
exe, err := os.Executable()
|
||||
if err != nil {
|
||||
return ""
|
||||
// requestDiskBinary names the code a file was written by: another may make
|
||||
// a row of a call differently. It was the binary itself, so every upgrade
|
||||
// read every session file again (~15.6k call shards, a cold start's
|
||||
// longest step); it is the call shards' version and this package's own
|
||||
// source, which 17 of 99 releases changed.
|
||||
var requestDiskBinary = sync.OnceValue(func() string {
|
||||
h := sha256.New()
|
||||
io.WriteString(h, sessions.CallCacheVersion)
|
||||
// unread, it is the binary's, as it was
|
||||
binary := func() string {
|
||||
exe, err := os.Executable()
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return statKey(exe)
|
||||
}
|
||||
return statKey(exe)
|
||||
}
|
||||
names, err := fs.Glob(ownSource, "*.go")
|
||||
if err != nil || len(names) == 0 {
|
||||
return binary()
|
||||
}
|
||||
for _, name := range names { // in lexical order
|
||||
if strings.HasSuffix(name, "_test.go") {
|
||||
continue
|
||||
}
|
||||
b, err := ownSource.ReadFile(name)
|
||||
if err != nil {
|
||||
return binary()
|
||||
}
|
||||
fmt.Fprintf(h, "\x00%s\x00%d\x00", name, len(b))
|
||||
h.Write(b)
|
||||
}
|
||||
return fmt.Sprintf("source %x", h.Sum(nil))
|
||||
})
|
||||
|
||||
// ownSource is this package's source, for requestDiskBinary.
|
||||
//
|
||||
//go:embed *.go
|
||||
var ownSource embed.FS
|
||||
|
||||
// requestDiskLeast is how many session files must want reading before the
|
||||
// file is read for them: a page after a call reads one or two again.
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
package usage
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -111,3 +114,32 @@ func TestRequestChunksFromDisk(t *testing.T) {
|
||||
t.Fatalf("a bad file: %d reads, want %d", reads.Load(), all)
|
||||
}
|
||||
}
|
||||
|
||||
// The file is of the code that makes a call's rows, not of the binary: an
|
||||
// upgrade that changed no source of this package reads it as the last run
|
||||
// left it. The name is of every source file here but the tests, and of
|
||||
// the call shards' version.
|
||||
func TestRequestDiskNamesItsSource(t *testing.T) {
|
||||
names, err := filepath.Glob("*.go")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
h := sha256.New()
|
||||
io.WriteString(h, sessions.CallCacheVersion)
|
||||
sources := 0
|
||||
for _, name := range names {
|
||||
if strings.HasSuffix(name, "_test.go") {
|
||||
continue
|
||||
}
|
||||
b, err := os.ReadFile(name)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
fmt.Fprintf(h, "\x00%s\x00%d\x00", name, len(b))
|
||||
h.Write(b)
|
||||
sources++
|
||||
}
|
||||
if want := fmt.Sprintf("source %x", h.Sum(nil)); requestDiskBinary() != want || sources < 10 {
|
||||
t.Fatalf("named %q, want %q of %d source files", requestDiskBinary(), want, sources)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user