usage: a first start pairs the history once for the page and Codex's effort (#2278)

A first start, with no sums on disk, paired the whole history a few
days at a time (coldSpans) for the warm-up's page of All (pageCold),
then again for Codex's effort of the same period (effortCold), or the
other way round when the Sessions overview asked first. Both read the
same blocks and name the settled days by the same heatSettledKey, so
whichever runs first now counts both from its one pairing and writes
both files: pageCold counts the effort when its filter is empty and
effort-v1.json lacks it, effortCold counts the cells of the period's
unfiltered page when page-v1.json lacks them.

Run on mote (M5 Pro), at this tree (rebased on 7e0fe83d4):
- go test -count=1 ./internal/usage/: ok (25.5s).
- TestFirstBuildPairsOnceForPageAndEffort asks the page then the
  effort, and the effort then the page, each after a cold restart:
  the second pairs no span, and both answers equal a build alone.
  Broken by hand: pageCold given no effort key, red ("page first:
  effort … after N spans"); effortCold counting no cells, red
  ("effort first: page …").
- Real cold start on the 11,925-file fixture (8.5GB .zst + 17GB
  jsonl), `magpie web`, two runs each, alternated:
  before (v0.1.2368's tree) CPU 3:11.45 / 3:04.84, footprint peak
  332 / 335 MB; after CPU 2:54.02 / 2:57.12, peak 330 / 325 MB. The
  heatmap, requests, overview (0/7/30) and stats (0/30) answers are
  byte for byte the same. Load on mote 22-28 from other sessions.

Not changed: the footprint's peak barely moves; the two builds ran one
after the other already (coldTurns), so this saves their CPU, not a
peak.
This commit is contained in:
yetone
2026-10-31 18:57:39 +08:00
parent a9154b170a
commit 213d59f3b3
3 changed files with 102 additions and 9 deletions
+49
View File
@@ -106,6 +106,55 @@ func TestFirstBuildPairsSpans(t *testing.T) {
}
}
// A first start pairs the history once for the warm-up's page and
// Codex's effort, whichever is asked first, and tells what each pairing
// of its own does (#2278).
func TestFirstBuildPairsOnceForPageAndEffort(t *testing.T) {
yesterday, _ := advanceHistory(t)
writeLongCodexThread(t, yesterday.AddDate(0, 0, -20), yesterday.AddDate(0, 0, -1))
coldSpan = 4
restart := func() {
requestCache.Lock()
requestCache.parts, requestCache.pageOld, requestCache.heatParts, requestCache.heatOld, requestCache.heats, requestCache.pairs, requestCache.pages = nil, nil, nil, nil, nil, nil, map[pageKey]RequestPage{}
requestCache.Unlock()
codexEffort.Lock()
codexEffort.periods = map[Period]*effortEntry{}
codexEffort.Unlock()
codexEffortBuilding.Lock()
effortOlds = map[Period]*effortOld{}
codexEffortBuilding.Unlock()
for _, path := range []string{pageDiskPath(), heatDiskPath(), effortDiskPath()} {
os.Remove(path)
}
}
restart()
t.Cleanup(restart)
spans := 0
coldSpanning = func(int) { spans++ }
t.Cleanup(func() { coldSpanning = nil })
page := QueryPage(All, Filter{}, 0, 50)
restart()
effort := CodexModelEffort(All)
restart()
spans = 0
if got := QueryPage(All, Filter{}, 0, 50); !reflect.DeepEqual(got, page) {
t.Fatalf("page first: %+v, alone %+v", got, page)
}
first := spans
if got := CodexModelEffort(All); !reflect.DeepEqual(got, effort) || spans != first || first == 0 {
t.Fatalf("page first: effort %+v after %d spans of %d, alone %+v", got, spans, first, effort)
}
restart()
spans = 0
if got := CodexModelEffort(All); !reflect.DeepEqual(got, effort) {
t.Fatalf("effort first: %+v, alone %+v", got, effort)
}
first = spans
if got := QueryPage(All, Filter{}, 0, 50); !reflect.DeepEqual(got, page) || spans != first || first == 0 {
t.Fatalf("effort first: page %+v after %d spans of %d, alone %+v", got, spans, first, page)
}
}
// writeLongCodexThread writes one Codex rollout with a call at each of
// ats, and the gateway's row of each.
func writeLongCodexThread(t *testing.T, ats ...time.Time) {
+21 -5
View File
@@ -144,7 +144,8 @@ func effortSplit(sums effortSums, p Period, now time.Time, gateways, chunks, oth
if !named {
return false
}
key = fmt.Sprintf("%x", sha256.Sum256([]byte("effort-v1\x00"+key)))
settledKey := key
key = effortKeyOf(Filter{}, settledKey)
switch {
case same(old) && old.key == key:
fresh := *old
@@ -155,7 +156,7 @@ func effortSplit(sums effortSums, p Period, now time.Time, gateways, chunks, oth
old, kept = &effortOld{root: disk.root, cut: cut, since: since, settled: serials, before: before, at: time.Now(), key: key, sums: s}, true
} else if s, found := effortAdvance(disk, cut, since, until, identities, sets, key); found {
old, kept = &effortOld{root: disk.root, cut: cut, since: since, settled: serials, before: before, at: time.Now(), key: key, sums: s}, true
} else if s, found := effortCold(cut, since, identities, sets, key); found {
} else if s, found := effortCold(cut, since, until, identities, sets, key, newCellPass(p, Filter{}, now, true), settledKey); found {
old, kept = &effortOld{root: disk.root, cut: cut, since: since, settled: serials, before: before, at: time.Now(), key: key, sums: s}, true
}
}
@@ -191,16 +192,31 @@ func effortSplit(sums effortSums, p Period, now time.Time, gateways, chunks, oth
}
// effortCold is the effort from since to cut, of a pairing a few days
// at a time (coldSpans), written under key; or false.
func effortCold(cut, since time.Time, identities map[string]sessions.CodexIdentity, sets [3][]*rowChunk, key string) (map[string][]sessions.ModelEffort, bool) {
// at a time (coldSpans), written under key; or false. The cells before
// cut of the period's unfiltered page, read through pass, are counted of
// the same pairing and kept too, when the file doesn't have them: the
// warm-up's page of All paired the whole history again after (pageCold).
func effortCold(cut, since, until time.Time, identities map[string]sessions.CodexIdentity, sets [3][]*rowChunk, key string, pass *cellPass, settledKey string) (map[string][]sessions.ModelEffort, bool) {
part := effortSums{}
pageKey := pageKeyOf(until, pass, settledKey)
var cells *chunkPart
if _, _, found := pageDiskRead(pageKey); !found {
cells = &chunkPart{}
}
if !coldSpans(since, cut, sets, func(from, to time.Time, span [3][]*rowChunk) {
pairRowsIn(from, to, span[0], span[1], span[2], identities).visitActors(part.add)
ps := pairRowsIn(from, to, span[0], span[1], span[2], identities)
ps.visitActors(part.add)
if cells != nil {
cells = addCells(cells, ps.cellsBefore(pass, to))
}
}) {
return nil, false
}
sums := part.values()
_ = effortDiskWrite(key, cut, since, sums)
if cells != nil {
_ = pageDiskWrite(pageKey, cut, since, cells)
}
return sums, true
}
+32 -4
View File
@@ -84,7 +84,7 @@ func pageSplit(p Period, f Filter, now time.Time, take int, pass *cellPass, gate
old, kept = &pageOld{cut: cut, since: since, settled: serials, before: before, at: time.Now(), key: key, cells: c}, true
} else if c, found := pageAdvance(cache.disk, cut, since, until, f, pass, identities, sets, key); found {
old, kept = &pageOld{cut: cut, since: since, settled: serials, before: before, at: time.Now(), key: key, cells: c}, true
} else if c, found := pageCold(cut, since, pass, identities, sets, key); found {
} else if c, found := pageCold(cut, since, pass, identities, sets, key, effortKeyOf(f, k)); found {
old, kept = &pageOld{cut: cut, since: since, settled: serials, before: before, at: time.Now(), key: key, cells: c}, true
}
}
@@ -175,18 +175,46 @@ func pageAdvance(d heatDiskRef, cut, since, until time.Time, f Filter, pass *cel
}
// pageCold is a page's cells before cut from since, of a pairing a few
// days at a time (coldSpans), written under key; or false.
func pageCold(cut, since time.Time, pass *cellPass, identities map[string]sessions.CodexIdentity, sets [3][]*rowChunk, key string) (*chunkPart, bool) {
// days at a time (coldSpans), written under key; or false. With
// effortKey, Codex's effort before cut is counted of the same pairing and
// kept under it, when the file doesn't have it: a first start paired the
// whole history for the warm-up's page and again for the effort of the
// same period, ~65 and ~105 MB of its live heap one after the other, and
// seconds of CPU each (#2278).
func pageCold(cut, since time.Time, pass *cellPass, identities map[string]sessions.CodexIdentity, sets [3][]*rowChunk, key, effortKey string) (*chunkPart, bool) {
cells := &chunkPart{}
var effort effortSums
if effortKey != "" {
if _, found := effortDiskRead(effortKey); !found {
effort = effortSums{}
}
}
if !coldSpans(since, cut, sets, func(from, to time.Time, span [3][]*rowChunk) {
cells = addCells(cells, pairRowsIn(from, to, span[0], span[1], span[2], identities).cellsBefore(pass, to))
ps := pairRowsIn(from, to, span[0], span[1], span[2], identities)
cells = addCells(cells, ps.cellsBefore(pass, to))
if effort != nil {
ps.visitActors(effort.add)
}
}) {
return nil, false
}
_ = pageDiskWrite(key, cut, since, cells)
if effort != nil {
_ = effortDiskWrite(effortKey, cut, since, effort.values())
}
return cells, true
}
// effortKeyOf is the key Codex's effort before the cut is kept under, of
// the settled chunks heatSettledKey names k for f; "" when f filters
// anything, as the effort is of every row.
func effortKeyOf(f Filter, k string) string {
if f != (Filter{}) {
return ""
}
return fmt.Sprintf("%x", sha256.Sum256([]byte("effort-v1\x00"+k)))
}
// addCells is the cells of a and of b, the later, as one part: a cell of
// the same names, bits and bucket added up, in the order of their last.
func addCells(a, b *chunkPart) *chunkPart {