diff --git a/internal/usage/log_index.go b/internal/usage/log_index.go index 341e2f3af..3b5b2f2c6 100644 --- a/internal/usage/log_index.go +++ b/internal/usage/log_index.go @@ -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) { diff --git a/internal/usage/log_visit_test.go b/internal/usage/log_visit_test.go new file mode 100644 index 000000000..4d92c0b83 --- /dev/null +++ b/internal/usage/log_visit_test.go @@ -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) + } +}