usage: the request page's probes are made into a chunk once per change

readRequestBlocks made probeChunk again on every poll of the Requests
and Usage pages: a rowChunk holding every probe since the period's
start minus a day, interned again each time. On the real-account
sandbox it was ~45 MB of 952 MB allocated in two and a half minutes
(rowChunk.add under probeChunk, #2278).

probeChunk now keeps the last chunk it made, with the probe blocks,
since, renames and hosts it was made from. A frozen block is never
written again (a new probe reopens the tail as a copy), UsageRenames
hands back the same maps while providers.json is unchanged
(Renames.Same compares them by identity), and hosts are compared by
content, so the same inputs make the same chunk, which no reader
writes. Any of them changing makes it again.

TestProbeChunkMadeOncePerChange: the same inputs give the same chunk;
a later since, a changed host and a new probe each make a new one.


Measured on mote with the real-account sandbox (pageheap.sh, the
request page polled to the mark): probeChunk allocated 43.81 MB at the
base (w42) and 24.54 / 24.10 MB with this (w43, two runs). It is not
zero there because the sandbox's network is blocked, so every probe
fails and logs a new row, which makes the chunk again.
Run on mote: go test ./internal/usage ok (33.9s); the test is red with
the memo turned off by hand.
This commit is contained in:
yetone
2026-11-13 05:31:22 +08:00
parent e6184b368a
commit a8b0e80f65
3 changed files with 78 additions and 0 deletions
+11
View File
@@ -6,6 +6,7 @@ import (
"fmt"
"net"
"os"
"reflect"
"slices"
"strings"
"sync/atomic"
@@ -269,6 +270,16 @@ type Renames struct {
merged map[string][]string
}
// Same is whether o is r itself, the one read UsageRenames hands back
// while providers.json is unchanged: the same maps, not equal ones. A
// reader that keeps what it made from r asks it to know r is unchanged.
func (r Renames) Same(o Renames) bool {
at := func(m any) uintptr { return reflect.ValueOf(m).Pointer() }
return at(r.now) == at(o.now) && at(r.gave) == at(o.gave) && at(r.hosts) == at(o.hosts) &&
at(r.sites) == at(o.sites) && at(r.gone) == at(o.gone) && at(r.into) == at(o.into) &&
at(r.merged) == at(o.merged)
}
type gaveTo struct {
Until time.Time `json:"until"`
To string `json:"to"`
+35
View File
@@ -1,6 +1,8 @@
package usage
import (
"maps"
"slices"
"strings"
"sync"
"time"
@@ -169,6 +171,39 @@ func clipProbeError(s string) string {
// way: CyberCaius again, Leee's model list asked at leee.baby before it
// was saved on another host of that site.
func probeChunk(probes []*rowChunk, since time.Time, renames provider.Renames, hosts map[string]string) *rowChunk {
// the page polls every few seconds and the probes rarely change: the
// chunk was made again each time, ~45 MB of two and a half minutes'
// allocations with the page open (#2278). A frozen block is never
// written again, so the same blocks, since, renames and hosts make
// the same chunk, which no reader writes.
probeMemo.Lock()
m := probeMemo.last
probeMemo.Unlock()
if m != nil && m.since.Equal(since) && slices.Equal(m.probes, probes) && m.renames.Same(renames) && maps.Equal(m.hosts, hosts) {
return m.c
}
c := makeProbeChunk(probes, since, renames, hosts)
probeMemo.Lock()
probeMemo.last = &probeMade{probes: slices.Clone(probes), since: since, renames: renames, hosts: maps.Clone(hosts), c: c}
probeMemo.Unlock()
return c
}
// probeMemo is the last chunk probeChunk made, and what it was made from.
var probeMemo struct {
sync.Mutex
last *probeMade
}
type probeMade struct {
probes []*rowChunk
since time.Time
renames provider.Renames
hosts map[string]string
c *rowChunk
}
func makeProbeChunk(probes []*rowChunk, since time.Time, renames provider.Renames, hosts map[string]string) *rowChunk {
var c *rowChunk
sites := sync.OnceValue(func() map[string]string { return sitesOf(hosts) })
eachProbe(probes, since, func(r Record, off int64) {
+32
View File
@@ -9,6 +9,7 @@ import (
"time"
"github.com/yetone/magpie/internal/probelog"
"github.com/yetone/magpie/internal/provider"
)
// ledgerSpy is an Observer that only counts the rows Append hands it.
@@ -131,3 +132,34 @@ func TestAnsweredModelListIsNoRow(t *testing.T) {
t.Fatalf("failed filter: total %d", p.Total)
}
}
// The request page asks for the probes on every poll; while none was
// made and no host or rename changed, the chunk made last time is the
// answer, and any of those changing makes it again (#2278).
func TestProbeChunkMadeOncePerChange(t *testing.T) {
at := time.Date(2026, 8, 1, 9, 0, 0, 0, time.UTC)
rows := probeRows{}
for i := range 3 {
rows.add(Record{Time: at.Add(time.Duration(i) * time.Minute), Agent: "magpie", Provider: "p", Host: "api.example.com", Status: 500, Source: SourceProbe}, int64(i))
}
blocks := rows.done()
hosts := map[string]string{"api.example.com": "p"}
first := probeChunk(blocks, at, provider.Renames{}, hosts)
if first == nil || first.Count != 3 {
t.Fatalf("first: %+v", first)
}
if again := probeChunk(slices.Clone(blocks), at, provider.Renames{}, map[string]string{"api.example.com": "p"}); again != first {
t.Fatal("the same probes, since, renames and hosts made the chunk again")
}
if c := probeChunk(blocks, at.Add(time.Minute), provider.Renames{}, hosts); c == first || c.Count != 2 {
t.Fatalf("a later since kept the old chunk: %+v", c)
}
if c := probeChunk(blocks, at, provider.Renames{}, map[string]string{"api.example.com": "q"}); c == first {
t.Fatal("a changed host kept the old chunk")
}
more := probeRows{blocks: slices.Clone(blocks)}
more.add(Record{Time: at.Add(5 * time.Minute), Agent: "magpie", Provider: "p", Host: "api.example.com", Status: 500, Source: SourceProbe}, 3)
if c := probeChunk(more.done(), at, provider.Renames{}, hosts); c == first || c.Count != 4 {
t.Fatalf("a new probe kept the old chunk: %+v", c)
}
}