usage: a read of the gateway log decodes its blocks into one buffer (#2278)
logSnapshot.visit unpacked each block of usage.jsonl's snapshot into rows of its own. Every Sessions, Usage and Routing poll reads the log through it (buildGatewaySessions, topSummary, buildVias), so while calls came in it allocated ~800 MB of rows a minute: 1.3 GB of the 5.3 GB a 24s heap profile of a fed sandbox allocated was unpackReusing's rows, strings and text under visit. It now decodes every block into one chunkRows buffer, each over the one before, as the page's other readers do. A record's strings are the block's own text, not the buffer's, so fn may keep them. Run at this head (b69f8e96a+ this change), on mote: - go test ./internal/usage -timeout 45m: ok (24s). - TestLogVisitReusesItsRows (new): visit's records equal a per-block unpack's, and one visit of 20000 rows allocates at most a quarter of their rows. Red on the old visit (4506304 > 4320000 bytes), green now. - go test ./internal/gui -timeout 45m: ok but TestMainWindowCorners, red the same as before this change (#3949). - go vet -tags nogui ./... for darwin, linux and windows: clean. A/B on mote, a sandbox HOME with #2278's synthetic history (34k sessions, 3000 with gateway records), 10 page endpoints polled every 5s and 4 gateway appends a second, 230s each, old = v0.1.1923: old new all polls mean 1.69s 1.09s heatmap mean 4.54s 2.94s footprint peak 648.9M 615.5M avg CPU after feed 70.9% 70.3% Another session's GUI suite ran on mote through both arms, so CPU is noisy; the poll times are the point. Rebased ontoc5cdb043e(no internal/usage change in between), then run again there: go test ./internal/usage ok (27.7s); go vet -tags nogui of internal/usage and internal/gui for darwin, linux and windows clean. Then rebased onto8273325b0, which changes site/ only. Not tried: the user's own history; the A/B's history is made up.
This commit is contained in:
@@ -260,15 +260,19 @@ func (o routeRows) handedOn(c *rowChunk, r *Record) {
|
||||
r.Tries = max(r.Tries, 2)
|
||||
}
|
||||
|
||||
// visit hands fn each record of the log since since. The blocks are
|
||||
// decoded into one buffer, each over the one before: each decoded into
|
||||
// rows of its own, the Sessions, Usage and Routing pages' reads of the
|
||||
// log allocated ~800 MB of rows a minute while calls came in (#2278). A
|
||||
// record's strings are the block's own, not the buffer's.
|
||||
func (s *logSnapshot) visit(since time.Time, fn func(Record)) {
|
||||
rows := chunkRows{}
|
||||
defer rows.release()
|
||||
for _, packed := range s.blocks {
|
||||
if !since.IsZero() && packed.Latest.Before(since) {
|
||||
continue
|
||||
}
|
||||
c := packed.unpack()
|
||||
if c == nil {
|
||||
continue
|
||||
}
|
||||
c := rows.of(packed)
|
||||
for i := range c.Rows {
|
||||
p := &c.Rows[i]
|
||||
if !p.Time.Before(since) {
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
package usage
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"runtime"
|
||||
"testing"
|
||||
"time"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// A read of the log decodes its blocks into one buffer, not each into rows
|
||||
// of its own: the pages' reads allocated ~800 MB of rows a minute while
|
||||
// calls came in (#2278). It reads every record as decoding each block did.
|
||||
func TestLogVisitReusesItsRows(t *testing.T) {
|
||||
pageHome(t)
|
||||
holdClock(t, time.Now())
|
||||
const n = 20000
|
||||
historyLog(t, n)
|
||||
s := readLogSnapshot()
|
||||
if len(s.blocks) < 10 {
|
||||
t.Fatalf("%d blocks, want the log in many", len(s.blocks))
|
||||
}
|
||||
var want []Record
|
||||
for _, packed := range s.blocks {
|
||||
c := packed.unpack()
|
||||
for i := range c.Rows {
|
||||
want = append(want, c.row(i).Record)
|
||||
}
|
||||
}
|
||||
var got []Record
|
||||
s.visit(time.Time{}, func(r Record) { got = append(got, r) })
|
||||
if len(got) != n || !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("visit read %d records, not the %d of the blocks decoded one by one", len(got), len(want))
|
||||
}
|
||||
var before, after runtime.MemStats
|
||||
runtime.ReadMemStats(&before)
|
||||
s.visit(time.Time{}, func(Record) {})
|
||||
runtime.ReadMemStats(&after)
|
||||
rows := int64(n) * int64(unsafe.Sizeof(packedRow{}))
|
||||
if allocated := int64(after.TotalAlloc - before.TotalAlloc); allocated > rows/4 {
|
||||
t.Fatalf("a visit of %d records allocated %d bytes; their rows alone are %d", n, allocated, rows)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user