Every query of All priced and unpacked every gateway block of the history
before it looked at what it needed. With the page's cells, the heatmap's
days and Codex's effort kept from earlier days (page-v1, heat-v1,
effort-v1), a cache hit reads only the last days' rows. Pricing the rest
was still 53 MB of the 219 MB heap at 18s on the real history
(blockPricer.priceBlock: 38 MB of pooled rows, 14 MB of unpacking).
readRequestBlocks now leaves a packed, named block whose newest call is
before settledBefore(now) (the splits' edge) as a stub: its serial, count
and latest time, the raw block and the pricer. priceStubs prices stubs in
place, and every pairing calls it first (pairRowsIn, pairRowsMemo,
heatBefore). So a miss, a page of older rows, or a period the splits don't
cover prices them as before. rowsIn counts a stub by its raw block.
heatSettledKey names a gateway block by its raw rows, which requestState's
meta prices, so a priced block and a stub get one key. That changes the
keys once; this binary's own keys already change.
Run at 0414bbedd plus this change (origin/main fetched, no newer commits), on mote:
- go test -count=1 ./internal/usage ./internal/usageapi: ok
- go test ./internal/gui: the AppKit tests time out at 30s, the same set
as #4125 (TestDockJoinLeadsCommandTab, TestPanelCloseHidesIt,
TestMainWindowCorners and 7 more); nothing else red
- GOOS=linux/windows/darwin go vet -tags nogui on the three packages: ok
- Broken by hand: with the stub turned off,
TestKeptPageLeavesSettledBlocksUnpriced fails with "started again, 2
blocks priced"
Real history on mote (~/r2278, a copy of the real ~/.codex, ~/.claude and
gateway log), 120s with the GUI's polls:
- v0.1.2183 at a cache hit: fp_peak 368 MB (356-374 before), peaklive
262 MB
- this change at a hit: fp_peak 301 and 308 MB, peaklive 226 and 219 MB
- the first start of a new binary misses as before: 510-512 MB
- the five pages (all, 7d, all at offset 0 limit 100, all for codex, 30d)
are the same as v0.1.2183's apart from float rounding in the sums
(largest relative difference 5e-14)
Not tried: Windows and Linux runs. The change is in the usage package,
which has no OS-specific code.
368 lines
12 KiB
Go
368 lines
12 KiB
Go
package usage
|
|
|
|
// The heatmap (#1369): the last 53 weeks a day each, as a calendar of
|
|
// contributions lays them out, Monday on top. It is the request page's own
|
|
// index read over that span — QueryPage's cached chunks, priced once — and
|
|
// not another pass over the logs, so a page asking for it again reads only
|
|
// what changed. It counts what the Requests tab's chart counts: the rows its
|
|
// filters keep, but for the day picked, and not the calls the gateway turned
|
|
// away itself.
|
|
|
|
import (
|
|
"slices"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/sessions"
|
|
)
|
|
|
|
// HeatmapWeeks is how many weeks the heatmap shows, this one the last.
|
|
const HeatmapWeeks = 53
|
|
|
|
// heatmapPeriod is the span the heatmap is read over: QueryPage's period for
|
|
// it, from the Monday HeatmapWeeks-1 weeks before this week's. Not one the
|
|
// page's period switch offers.
|
|
const heatmapPeriod Period = "heatmap"
|
|
|
|
// heatmapSince is the Monday the heatmap starts on, as of now.
|
|
func heatmapSince(now time.Time) time.Time {
|
|
day := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
|
|
return day.AddDate(0, 0, -(int(day.Weekday())+6)%7-7*(HeatmapWeeks-1))
|
|
}
|
|
|
|
// Day is one day's calls, by the local calendar day they fell on.
|
|
type Day struct {
|
|
Date string `json:"date"` // YYYY-MM-DD
|
|
Calls int `json:"calls"`
|
|
Tokens int `json:"tokens"` // in, out and through the cache, as the chart counts them
|
|
Cost float64 `json:"cost"` // of the priced calls, USD
|
|
// Unpriced: calls with tokens but no known price, not in Cost
|
|
Unpriced int `json:"unpriced,omitempty"`
|
|
}
|
|
|
|
// Heatmap is the days from From to To (today), both YYYY-MM-DD in the
|
|
// reader's time zone (Filter.Zone): only those with calls are listed, oldest first.
|
|
type Heatmap struct {
|
|
From string `json:"from"`
|
|
To string `json:"to"`
|
|
Days []Day `json:"days"`
|
|
}
|
|
|
|
// HeatmapOf is the last HeatmapWeeks weeks of the calls the filter keeps, a
|
|
// day each. The filter's Day is not one of its dimensions here: the heatmap
|
|
// is every day's.
|
|
func HeatmapOf(f Filter) Heatmap {
|
|
f.Day = ""
|
|
if h := QueryPage(heatmapPeriod, f, 0, 1).Heat; h != nil {
|
|
return *h
|
|
}
|
|
return Heatmap{Days: []Day{}}
|
|
}
|
|
|
|
// heatmapOf sums the rows visit gives it by their day in now's zone (the
|
|
// reader's), from since to now's day; a row outside those days is left out.
|
|
func heatmapOf(since, now time.Time, visit func(add func(Row))) *Heatmap {
|
|
days := heatDays{}
|
|
visit(func(r Row) { days.add(&r, since, now.Location()) })
|
|
return heatmapFrom(since, now, days.list())
|
|
}
|
|
|
|
// heatDays adds rows up a day each, by the day's name.
|
|
type heatDays struct {
|
|
at map[string]*Day
|
|
// a day's name is made once for each day, not for each of its calls
|
|
names map[[2]int]string
|
|
}
|
|
|
|
// add adds r to its day in loc, unless the heatmap doesn't count it.
|
|
func (h *heatDays) add(r *Row, since time.Time, loc *time.Location) {
|
|
if r.IsRejected() || r.IsProbe() || r.Time.Before(since) {
|
|
return
|
|
}
|
|
if h.at == nil {
|
|
h.at, h.names = map[string]*Day{}, map[[2]int]string{}
|
|
}
|
|
t := r.Time.In(loc)
|
|
k := [2]int{t.Year(), t.YearDay()}
|
|
d, ok := h.names[k]
|
|
if !ok {
|
|
d = t.Format(time.DateOnly)
|
|
h.names[k] = d
|
|
}
|
|
x := h.at[d]
|
|
if x == nil {
|
|
x = &Day{Date: d}
|
|
h.at[d] = x
|
|
}
|
|
if !r.HandedOn {
|
|
x.Calls++ // a request, not each try at it
|
|
}
|
|
x.Tokens += r.Input + r.Output + r.CacheRead + r.CacheWrite
|
|
switch {
|
|
case r.Priced:
|
|
x.Cost += r.Cost
|
|
case r.Input+r.Output > 0:
|
|
x.Unpriced++
|
|
}
|
|
}
|
|
|
|
// addDay adds a day's sums to its day.
|
|
func (h *heatDays) addDay(d Day) {
|
|
if h.at == nil {
|
|
h.at = map[string]*Day{}
|
|
}
|
|
x := h.at[d.Date]
|
|
if x == nil {
|
|
x = &Day{Date: d.Date}
|
|
h.at[d.Date] = x
|
|
}
|
|
x.Calls += d.Calls
|
|
x.Tokens += d.Tokens
|
|
x.Cost += d.Cost
|
|
x.Unpriced += d.Unpriced
|
|
}
|
|
|
|
// list is the days, oldest first.
|
|
func (h *heatDays) list() []Day {
|
|
out := make([]Day, 0, len(h.at))
|
|
for _, x := range h.at {
|
|
out = append(out, *x)
|
|
}
|
|
slices.SortFunc(out, func(a, b Day) int { return strings.Compare(a.Date, b.Date) })
|
|
return out
|
|
}
|
|
|
|
// heatmapFrom is days from since to now's day, a day after it left out.
|
|
func heatmapFrom(since, now time.Time, days []Day) *Heatmap {
|
|
out := &Heatmap{From: since.Format(time.DateOnly), To: now.Format(time.DateOnly), Days: []Day{}}
|
|
for _, d := range days {
|
|
if d.Date <= out.To {
|
|
out.Days = append(out.Days, d)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// heatOf is heatmapOf of ps's rows f keeps, added up a chunk at a time:
|
|
// cache's sums of a chunk paired as before are taken as they are. A call
|
|
// through magpie changes the gateway's log, and the Requests tab's refresh
|
|
// read every row of the year again for it, and paired them: ~4 GB a
|
|
// minute and seconds of CPU a refresh while an agent worked (#2278).
|
|
func (ps *pairedRows) heatOf(since, now time.Time, f Filter, cache *partCache, identities map[string]sessions.CodexIdentity) *Heatmap {
|
|
return heatmapFrom(since, now, ps.heatDaysOf(since, now, f, cache, identities, nil))
|
|
}
|
|
|
|
// heatDaysOf is heatOf's days, oldest first, each with a call, of the
|
|
// chunks of ps that only keeps (nil: all).
|
|
func (ps *pairedRows) heatDaysOf(since, now time.Time, f Filter, cache *partCache, identities map[string]sessions.CodexIdentity, only func(*rowChunk) bool) []Day {
|
|
sigs := ps.sigs(identities)
|
|
var rows chunkRows
|
|
defer rows.release()
|
|
var r Row
|
|
all := heatDays{}
|
|
for _, c := range ps.all {
|
|
if only != nil && !only(c) {
|
|
continue
|
|
}
|
|
sig := sigs[c]
|
|
var part *chunkPart
|
|
if cache != nil && c.serial != 0 {
|
|
if old := cache.old[c.serial]; old != nil && old.sig == sig {
|
|
part = old
|
|
}
|
|
}
|
|
if part == nil {
|
|
if heatReading != nil {
|
|
heatReading(c)
|
|
}
|
|
one := heatDays{}
|
|
ps.visitChunk(&rows, c, &r, func(_ timedRef, r *Row) {
|
|
if f.keepsRow(r) {
|
|
one.add(r, since, now.Location())
|
|
}
|
|
})
|
|
part = &chunkPart{sig: sig, days: one.list()}
|
|
}
|
|
if cache != nil && c.serial != 0 {
|
|
cache.next[c.serial] = part
|
|
}
|
|
for _, d := range part.days {
|
|
all.addDay(d)
|
|
}
|
|
}
|
|
return all.list()
|
|
}
|
|
|
|
// heatRecent is how many days before today the heatmap pairs again for a
|
|
// call (heatSplit), and heatReach how far before them a chunk's newest
|
|
// call can be and still be paired with one of theirs: the gateway's calls
|
|
// are read from a day before a period (gatewaySince), the other
|
|
// computers' days from two.
|
|
const (
|
|
heatRecent = 2
|
|
heatReach = 48 * time.Hour
|
|
)
|
|
|
|
// heatOldFresh is how long heatSplit keeps the days before its last few
|
|
// while only the chunks of those days change. A session file a call is
|
|
// written to now can hold older calls too, and those are taken to stay as
|
|
// they were: an agent appends to its file. What changes them anyway, a
|
|
// file written again or a thread's identity learnt, shows within this:
|
|
// the days are named again (heatSettledKey), and counted again only when
|
|
// the name changed.
|
|
var heatOldFresh = 10 * time.Minute
|
|
|
|
// heatOld is the heatmap's days before cut, from a pairing of every
|
|
// chunk, while settled, the serials of the chunks with no call since
|
|
// heatReach before cut, are the same. parts are the other chunks' sums
|
|
// from cut on, as the last pairing of them paired them.
|
|
type heatOld struct {
|
|
cut time.Time
|
|
settled []uint64
|
|
at time.Time
|
|
days []Day
|
|
parts map[uint64]*chunkPart
|
|
// before is the recent chunks' rows before cut, which days counts
|
|
before heatSum
|
|
// key names what days were counted of (heatSettledKey); "" unnamed
|
|
key string
|
|
}
|
|
|
|
// heatSum is a sum of rows as read, to tell a change in them.
|
|
type heatSum struct {
|
|
n int
|
|
at, tokens int64
|
|
}
|
|
|
|
// heatBefore is the sum of the recent chunks' rows before cut: a recent
|
|
// chunk can hold calls of the days before (a session file copied in, a
|
|
// rollout forked from an old one), and they change the days the last
|
|
// pairing of every chunk counted.
|
|
func heatBefore(cut time.Time, recent [3][]*rowChunk) heatSum {
|
|
priceStubs(recent[0])
|
|
rows := lentRows()
|
|
defer rows.release()
|
|
var s heatSum
|
|
for _, set := range recent {
|
|
for _, c := range set {
|
|
open := rows.of(c)
|
|
for i := range open.Rows {
|
|
if pr := &open.Rows[i]; pr.Time.Before(cut) {
|
|
s.n++
|
|
s.at += pr.Time.UnixNano()
|
|
for _, t := range pr.Tokens {
|
|
s.tokens += t
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return s
|
|
}
|
|
|
|
// heatSplit is heatOf of the rows of every chunk, paired. A call through
|
|
// magpie changes the gateway's newest block and a session file, and the
|
|
// heatmap paired a year of calls again for it: ~1.8 s and most of the
|
|
// GC of each refresh of the Usage page while an agent worked (#2278). The
|
|
// days before the last heatRecent are taken from the last pairing of
|
|
// every chunk while the chunks that could change them are the same, and
|
|
// only the chunks of the days since are paired again, as a page of those
|
|
// days pairs them.
|
|
func heatSplit(since, now time.Time, gateways, chunks, others []*rowChunk, f Filter, identities map[string]sessions.CodexIdentity, cache *partCache) *Heatmap {
|
|
cut := time.Date(now.Year(), now.Month(), now.Day()-heatRecent, 0, 0, 0, 0, now.Location())
|
|
edge := cut.Add(-heatReach)
|
|
isRecent := func(c *rowChunk) bool { return c.Latest.IsZero() || !c.Latest.Before(edge) }
|
|
if cache == nil || !cut.After(since) {
|
|
return heatmapFrom(since, now, pairRows(heatmapPeriod, now, gateways, chunks, others, identities).heatDaysOf(since, now, f, cache, identities, nil))
|
|
}
|
|
var recent, settled [3][]*rowChunk
|
|
for k, set := range [3][]*rowChunk{gateways, chunks, others} {
|
|
for _, c := range set {
|
|
if isRecent(c) {
|
|
recent[k] = append(recent[k], c)
|
|
} else {
|
|
settled[k] = append(settled[k], c)
|
|
}
|
|
}
|
|
}
|
|
// recentDays adds up the recent chunks' rows from cut on, paired as
|
|
// the next call pairs them, into parts
|
|
recentDays := func(old, parts map[uint64]*chunkPart) []Day {
|
|
ps := pairRowsIn(cut, heatmapPeriod.Until(now), recent[0], recent[1], recent[2], identities)
|
|
return ps.heatDaysOf(cut, now, f, &partCache{old: old, next: parts}, identities, nil)
|
|
}
|
|
serials, ok := pairSerials(settled[0], settled[1], settled[2])
|
|
old := cache.heatOld
|
|
var before heatSum
|
|
if ok {
|
|
before = heatBefore(cut, recent)
|
|
}
|
|
var key string
|
|
named := false
|
|
kept := ok && old != nil && old.cut.Equal(cut) && slices.Equal(old.settled, serials) && old.before == before && time.Since(old.at) < heatOldFresh
|
|
if ok && !kept {
|
|
// the days are counted again only when what they were counted of
|
|
// changed: kept, or on disk from the last run
|
|
if key, named = heatSettledKey(cache.disk, cut, since, f, identities, settled, before); named {
|
|
if old != nil && old.key == key && old.cut.Equal(cut) && old.before == before {
|
|
fresh := *old
|
|
fresh.settled, fresh.at = serials, time.Now()
|
|
old, kept = &fresh, true
|
|
} else if days, found := heatDiskRead(key); found {
|
|
old, kept = &heatOld{cut: cut, settled: serials, at: time.Now(), days: days, before: before, key: key}, true
|
|
}
|
|
}
|
|
}
|
|
if !kept {
|
|
if heatPairing != nil {
|
|
heatPairing(len(gateways) + len(chunks) + len(others))
|
|
}
|
|
days := pairRows(heatmapPeriod, now, gateways, chunks, others, identities).heatDaysOf(since, now, f, cache, identities, nil)
|
|
if ok {
|
|
n, _ := slices.BinarySearchFunc(days, cut.Format(time.DateOnly), func(d Day, day string) int { return strings.Compare(d.Date, day) })
|
|
parts := map[uint64]*chunkPart{}
|
|
var kept map[uint64]*chunkPart
|
|
if old != nil {
|
|
kept = old.parts
|
|
}
|
|
recentDays(kept, parts)
|
|
next := &heatOld{cut: cut, settled: serials, at: time.Now(), days: slices.Clip(days[:n]), parts: parts, before: before}
|
|
if named {
|
|
next.key = key
|
|
// not written, the days are counted again on the next start
|
|
_ = heatDiskWrite(key, next.days)
|
|
}
|
|
cache.heatOldNext = next
|
|
}
|
|
return heatmapFrom(since, now, days)
|
|
}
|
|
next := *old
|
|
next.parts = map[uint64]*chunkPart{}
|
|
cache.heatOldNext = &next
|
|
// the settled chunks' sums stay kept for the next pairing of them all
|
|
for _, set := range settled {
|
|
for _, c := range set {
|
|
if part := cache.old[c.serial]; part != nil {
|
|
cache.next[c.serial] = part
|
|
}
|
|
}
|
|
}
|
|
all := heatDays{}
|
|
for _, d := range old.days {
|
|
all.addDay(d)
|
|
}
|
|
if heatPairing != nil {
|
|
heatPairing(len(recent[0]) + len(recent[1]) + len(recent[2]))
|
|
}
|
|
for _, d := range recentDays(old.parts, next.parts) {
|
|
all.addDay(d)
|
|
}
|
|
return heatmapFrom(since, now, all.list())
|
|
}
|
|
|
|
// heatPairing, in tests, is told how many chunks heatSplit pairs.
|
|
var heatPairing func(chunks int)
|
|
|
|
// heatReading, in tests, is told each chunk heatOf adds up again.
|
|
var heatReading func(*rowChunk)
|