usage: a Codex thread's model and effort are read through the request index
CodexModelEffort, which the Sessions list asks for, read the ledger again on every build: sessions.AgentCalls(since, "codex") opened all of Codex's shards (12k rollouts, 15.6k shards on the reporter-sized sandbox) after any new call, beside the Requests page reading the same files into its own index (#3493). It now reads through the request index as a page does: readRequestBlocks (moved out of queryPage), the page's pairing and visit (pairedRows, moved out of buildRequestBlocksOf), and the sums added row by row as they are visited (effortSums), so a build after a call reads only the files that changed, and the first of the Sessions list and the Requests page reads them for both. The LogCalls path still builds from codexEffortRows. The visit's row is one per visit now: as a returned closure's local it was moved to the heap, one allocation a row, which TestRequestPageAppendDoesNotRepriceHistory caught (10890 allocations over 8000 records against 2000). Measured before the rebase ontodbbe97991(its parentffe493773adds rows for magpie's own probes, none in this sandbox), on a sandbox copy of real data (sbmem: 12,069 Codex rollouts, ~416k rows), base22a7f86c9against this, /api/sessions then the Requests page then /api/sessions, one CPU profile over each run: total CPU 16.75s -> 9.40s (first run 20.10s -> 8.32s) CodexModelEffort 4.21s -> 1.65s sessions.loadCalls 5.61s -> 1.22s (now only under the first build; the page that follows reads none) wall, first run: sessions 4.97s -> 2.84s, requests 7.72s -> 2.01s (load average 78-133 from other sessions' suites, so wall is noisy) /api/sessions of both binaries: 4,930 Codex sessions, 4,604 model/effort entries, every token count equal and every cost equal within 1e-9 (summation order). Tests, at this head after the rebase: go test ./internal/usage ./internal/sessions ./internal/usageapi; ./internal/gui -run 'Session|Codex|Effort'; ./internal/usage -race -run 'Codex|Effort|RequestPage'; gofmt; GOOS=linux and windows go vet -tags nogui ./internal/usage ./internal/gui. TestCodexEffortRowsAreTheWholeLedgers now also holds codexEffortOfIndex to the whole ledger's sums for All, Today and Week; it goes red with a gateway call's actor broken by hand. Not tried: a live Codex session writing while the list polls (the build after a call is the page's incremental read, measured in22a7f86c9).
This commit is contained in:
@@ -52,6 +52,12 @@ func TestCodexEffortRowsAreTheWholeLedgers(t *testing.T) {
|
||||
t.Fatalf("%s, %s:\n got %+v\nwant %+v", p, name, got, whole)
|
||||
}
|
||||
}
|
||||
// read through the request index, as CodexModelEffort builds it
|
||||
// (#3493): the same sums as the whole ledger's thread rows
|
||||
want := codexModelEffort(threadRows(ledgerOfAt(p, Filter{}, now, sessions.Calls)))
|
||||
if got := codexEffortOfIndex(readRequestState(now), p, now); !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("%s through the index:\n got %+v\nwant %+v", p, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -440,10 +440,7 @@ func CodexModelEffort(p Period) map[string][]sessions.ModelEffort {
|
||||
if effortBuilt != nil {
|
||||
effortBuilt()
|
||||
}
|
||||
// Only Codex's own calls carry a Codex thread: a gateway call is told
|
||||
// its thread by the Codex call that has its response id, so the other
|
||||
// agents' session files are not read for this.
|
||||
value = codexModelEffort(codexEffortRows(p, now, codexCalls))
|
||||
value = codexEffortOfIndex(st, p, now)
|
||||
return cloneEffort(value)
|
||||
}
|
||||
|
||||
@@ -537,37 +534,51 @@ func cloneEffort(values map[string][]sessions.ModelEffort) map[string][]sessions
|
||||
}
|
||||
|
||||
func codexModelEffort(rows []Row) map[string][]sessions.ModelEffort {
|
||||
type modelEffortKey struct{ model, effort string }
|
||||
type total struct {
|
||||
value sessions.ModelEffort
|
||||
priced bool
|
||||
sums := effortSums{}
|
||||
for i := range rows {
|
||||
sums.add(&rows[i])
|
||||
}
|
||||
groups := map[string]map[modelEffortKey]*total{}
|
||||
for _, row := range rows {
|
||||
if row.codexActor == "" || row.IsRejected() ||
|
||||
row.Input+row.Output+row.CacheRead+row.CacheWrite == 0 {
|
||||
continue
|
||||
}
|
||||
byModel := groups[row.codexActor]
|
||||
if byModel == nil {
|
||||
byModel = map[modelEffortKey]*total{}
|
||||
groups[row.codexActor] = byModel
|
||||
}
|
||||
key := modelEffortKey{row.Model, row.Effort}
|
||||
entry := byModel[key]
|
||||
if entry == nil {
|
||||
entry = &total{value: sessions.ModelEffort{Model: row.Model, Effort: row.Effort}, priced: true}
|
||||
byModel[key] = entry
|
||||
}
|
||||
entry.priced = entry.priced && row.Priced
|
||||
entry.value.Input += row.Input
|
||||
entry.value.Output += row.Output
|
||||
entry.value.CacheRead += row.CacheRead
|
||||
entry.value.CacheWrite += row.CacheWrite
|
||||
entry.value.CacheWrite1h += row.CacheWrite1h
|
||||
entry.value.Reasoning += row.Reasoning
|
||||
entry.value.Cost += row.Cost
|
||||
return sums.values()
|
||||
}
|
||||
|
||||
type modelEffortKey struct{ model, effort string }
|
||||
|
||||
type effortTotal struct {
|
||||
value sessions.ModelEffort
|
||||
priced bool
|
||||
}
|
||||
|
||||
// effortSums adds rows up by Codex thread, model and effort as they are
|
||||
// visited, so that no build holds the rows themselves.
|
||||
type effortSums map[string]map[modelEffortKey]*effortTotal
|
||||
|
||||
func (groups effortSums) add(row *Row) {
|
||||
if row.codexActor == "" || row.IsRejected() ||
|
||||
row.Input+row.Output+row.CacheRead+row.CacheWrite == 0 {
|
||||
return
|
||||
}
|
||||
byModel := groups[row.codexActor]
|
||||
if byModel == nil {
|
||||
byModel = map[modelEffortKey]*effortTotal{}
|
||||
groups[row.codexActor] = byModel
|
||||
}
|
||||
key := modelEffortKey{row.Model, row.Effort}
|
||||
entry := byModel[key]
|
||||
if entry == nil {
|
||||
entry = &effortTotal{value: sessions.ModelEffort{Model: row.Model, Effort: row.Effort}, priced: true}
|
||||
byModel[key] = entry
|
||||
}
|
||||
entry.priced = entry.priced && row.Priced
|
||||
entry.value.Input += row.Input
|
||||
entry.value.Output += row.Output
|
||||
entry.value.CacheRead += row.CacheRead
|
||||
entry.value.CacheWrite += row.CacheWrite
|
||||
entry.value.CacheWrite1h += row.CacheWrite1h
|
||||
entry.value.Reasoning += row.Reasoning
|
||||
entry.value.Cost += row.Cost
|
||||
}
|
||||
|
||||
func (groups effortSums) values() map[string][]sessions.ModelEffort {
|
||||
out := make(map[string][]sessions.ModelEffort, len(groups))
|
||||
for actor, byModel := range groups {
|
||||
values := make([]sessions.ModelEffort, 0, len(byModel))
|
||||
|
||||
+131
-52
@@ -621,23 +621,12 @@ func queryPage(p Period, f Filter, offset, limit int, readSource func(sessions.C
|
||||
return pageFromLedgerAt(p, f, offset, limit, LedgerOfAt(p, Filter{}, now), now)
|
||||
}
|
||||
st := readRequestState(now)
|
||||
sources, codexIdentities, cfg, snapshot, days, someShared := st.sources, st.codexIdentities, st.cfg, st.snapshot, st.days, st.someShared
|
||||
version, meta, key, root := snapshot.version, st.meta, st.key, st.root
|
||||
codexIdentities, snapshot, someShared := st.codexIdentities, st.snapshot, st.someShared
|
||||
version, key := snapshot.version, st.key
|
||||
idx := &requestCache
|
||||
idx.Lock()
|
||||
idx.keepWhileAsked()
|
||||
|
||||
if idx.root != root || idx.meta != meta {
|
||||
idx.root, idx.meta = root, meta
|
||||
idx.chunks = map[string]*rowChunk{}
|
||||
idx.pages = nil
|
||||
idx.heats = nil
|
||||
idx.key = ""
|
||||
}
|
||||
if idx.key != key {
|
||||
idx.pages = map[pageKey]RequestPage{}
|
||||
idx.key = key
|
||||
}
|
||||
idx.adopt(st)
|
||||
q := pageKey{p, f, offset, limit, now.Format(time.DateOnly)}
|
||||
if page, ok := idx.pages[q]; ok {
|
||||
idx.Unlock()
|
||||
@@ -668,13 +657,76 @@ func queryPage(p Period, f Filter, offset, limit int, readSource func(sessions.C
|
||||
fl.page = page
|
||||
close(fl.done)
|
||||
}()
|
||||
idx.tick++
|
||||
// Copy only metadata while holding the lock. Published chunks are immutable;
|
||||
// reading, decoding, pricing and aggregation use a private snapshot.
|
||||
shared := idx
|
||||
idx = &requestIndex{root: idx.root, meta: idx.meta, key: idx.key, tick: idx.tick,
|
||||
chunks: maps.Clone(idx.chunks)}
|
||||
idx = shared.private()
|
||||
shared.Unlock()
|
||||
b := readRequestBlocks(idx, st, p, now, readSource)
|
||||
var names map[string]string
|
||||
if someShared {
|
||||
names = sharedNames()
|
||||
}
|
||||
if building != nil {
|
||||
building()
|
||||
}
|
||||
page = buildRequestBlocksWithCodex(p, f, offset, limit, now, b.gateways, b.chunks, b.others, names, codexIdentities)
|
||||
b.priced.release()
|
||||
shared.Lock()
|
||||
defer shared.Unlock()
|
||||
if !shared.keep(idx, b.on) {
|
||||
return page
|
||||
}
|
||||
if len(shared.pages) >= 16 {
|
||||
shared.pages = map[pageKey]RequestPage{}
|
||||
}
|
||||
shared.pages[q] = page
|
||||
if p == heatmapPeriod {
|
||||
if shared.heats == nil || len(shared.heats) >= 16 {
|
||||
shared.heats = map[pageKey]heatPage{}
|
||||
}
|
||||
shared.heats[q] = heatPage{page: page, version: version, at: began}
|
||||
}
|
||||
shared.trim(requestCacheBytes)
|
||||
return page
|
||||
}
|
||||
|
||||
// codexEffortOfIndex is codexModelEffort of p's Codex rows, read through
|
||||
// the request index as a page is: a build after a call reads only the
|
||||
// session files that changed, where reading the ledger again opened every
|
||||
// Codex shard there is (#3493).
|
||||
func codexEffortOfIndex(st requestState, p Period, now time.Time) map[string][]sessions.ModelEffort {
|
||||
shared := &requestCache
|
||||
shared.Lock()
|
||||
shared.keepWhileAsked()
|
||||
shared.adopt(st)
|
||||
idx := shared.private()
|
||||
shared.Unlock()
|
||||
b := readRequestBlocks(idx, st, p, now, sessions.ReadCallSource)
|
||||
_, visit := pairedRows(p, now, b.gateways, b.chunks, b.others, st.codexIdentities)
|
||||
sums := effortSums{}
|
||||
visit(func(_ timedRef, r *Row) { sums.add(r) })
|
||||
b.priced.release()
|
||||
shared.Lock()
|
||||
if shared.keep(idx, b.on) {
|
||||
shared.trim(requestCacheBytes)
|
||||
}
|
||||
shared.Unlock()
|
||||
return sums.values()
|
||||
}
|
||||
|
||||
// requestBlocks are what a page of a period is worked out from: the
|
||||
// gateway's blocks of it, priced, the session files' chunks and the other
|
||||
// computers' days, and the paths the index holds now (on).
|
||||
type requestBlocks struct {
|
||||
gateways, chunks, others []*rowChunk
|
||||
on map[string]bool
|
||||
priced *blockPricer
|
||||
}
|
||||
|
||||
// readRequestBlocks reads the blocks of p at now into idx, a private copy
|
||||
// of the request index (private): a session file or another computer's
|
||||
// day is read again only when it changed since its chunk was made.
|
||||
func readRequestBlocks(idx *requestIndex, st requestState, p Period, now time.Time, readSource func(sessions.CallSource) []sessions.Call) requestBlocks {
|
||||
sources, cfg, snapshot, days := st.sources, st.cfg, st.snapshot, st.days
|
||||
price := Pricer()
|
||||
// Swapped's names are a few hundred pairs over thousands of files' calls,
|
||||
// and its regexps were a third of reading them again (#3493)
|
||||
@@ -807,43 +859,55 @@ func queryPage(p Period, f Filter, offset, limit int, readSource func(sessions.C
|
||||
delete(idx.chunks, path)
|
||||
}
|
||||
}
|
||||
var names map[string]string
|
||||
if someShared {
|
||||
names = sharedNames()
|
||||
return requestBlocks{gateways: gateways, chunks: chunks, others: others, on: on, priced: priced}
|
||||
}
|
||||
|
||||
// adopt drops what idx keeps of a state other than st: every chunk when
|
||||
// what prices or names a call changed (meta), the pages when anything
|
||||
// did (key). idx is locked.
|
||||
func (idx *requestIndex) adopt(st requestState) {
|
||||
if idx.root != st.root || idx.meta != st.meta {
|
||||
idx.root, idx.meta = st.root, st.meta
|
||||
idx.chunks = map[string]*rowChunk{}
|
||||
idx.pages = nil
|
||||
idx.heats = nil
|
||||
idx.key = ""
|
||||
}
|
||||
if building != nil {
|
||||
building()
|
||||
if idx.key != st.key {
|
||||
idx.pages = map[pageKey]RequestPage{}
|
||||
idx.key = st.key
|
||||
}
|
||||
page = buildRequestBlocksWithCodex(p, f, offset, limit, now, gateways, chunks, others, names, codexIdentities)
|
||||
priced.release()
|
||||
shared.Lock()
|
||||
defer shared.Unlock()
|
||||
// A query for an older filesystem snapshot may finish after a newer one.
|
||||
if shared.root != idx.root || shared.meta != idx.meta || shared.key != idx.key || snapshot.version != version {
|
||||
return page
|
||||
}
|
||||
|
||||
// private is a copy of idx's metadata and chunk map to read into without
|
||||
// its lock: published chunks are immutable, and reading, decoding, pricing
|
||||
// and aggregation use the copy. It counts a use of idx (tick). idx is
|
||||
// locked.
|
||||
func (idx *requestIndex) private() *requestIndex {
|
||||
idx.tick++
|
||||
return &requestIndex{root: idx.root, meta: idx.meta, key: idx.key, tick: idx.tick,
|
||||
chunks: maps.Clone(idx.chunks)}
|
||||
}
|
||||
|
||||
// keep takes into idx the chunks read into read, its private copy, when
|
||||
// they are of the state idx is in: a read of an older filesystem snapshot
|
||||
// may finish after a newer one. on are the paths read holds. idx is
|
||||
// locked.
|
||||
func (idx *requestIndex) keep(read *requestIndex, on map[string]bool) bool {
|
||||
if idx.root != read.root || idx.meta != read.meta || idx.key != read.key {
|
||||
return false
|
||||
}
|
||||
for path, c := range idx.chunks {
|
||||
if prev := shared.chunks[path]; prev == nil || prev.Used <= c.Used {
|
||||
shared.chunks[path] = c
|
||||
for path, c := range read.chunks {
|
||||
if prev := idx.chunks[path]; prev == nil || prev.Used <= c.Used {
|
||||
idx.chunks[path] = c
|
||||
}
|
||||
}
|
||||
for path := range shared.chunks {
|
||||
for path := range idx.chunks {
|
||||
if !on[path] {
|
||||
delete(shared.chunks, path)
|
||||
delete(idx.chunks, path)
|
||||
}
|
||||
}
|
||||
if len(shared.pages) >= 16 {
|
||||
shared.pages = map[pageKey]RequestPage{}
|
||||
}
|
||||
shared.pages[q] = page
|
||||
if p == heatmapPeriod {
|
||||
if shared.heats == nil || len(shared.heats) >= 16 {
|
||||
shared.heats = map[pageKey]heatPage{}
|
||||
}
|
||||
shared.heats[q] = heatPage{page: page, version: version, at: began}
|
||||
}
|
||||
shared.trim(requestCacheBytes)
|
||||
return page
|
||||
return true
|
||||
}
|
||||
|
||||
// keepWhileAsked holds the chunks to requestCacheBytes while the page is
|
||||
@@ -1200,7 +1264,12 @@ func ownStrings(v reflect.Value) {
|
||||
}
|
||||
}
|
||||
|
||||
func buildRequestBlocksOf(p Period, f Filter, offset, limit int, now time.Time, gateways, chunks, others []*rowChunk, names map[string]string, identities map[string]sessions.CodexIdentity) RequestPage {
|
||||
// pairedRows are the page's rows of p: each gateway call told the Codex
|
||||
// thread of the session file's call it is paired with, and the session
|
||||
// files' calls a gateway call matched, or another file's copy, left out.
|
||||
// visit hands each row on in all's order. The chunks are copies, so the
|
||||
// cached ones are left as they are.
|
||||
func pairedRows(p Period, now time.Time, gateways, chunks, others []*rowChunk, identities map[string]sessions.CodexIdentity) (all []*rowChunk, visit func(fn func(timedRef, *Row))) {
|
||||
gateways, chunks, others = cloneCodexChunks(gateways), cloneCodexChunks(chunks), cloneCodexChunks(others)
|
||||
duplicateSkip := foldCodexRows(chunks)
|
||||
pairs, compactions := codexChunkResponsePairs(gateways, chunks, duplicateSkip)
|
||||
@@ -1223,9 +1292,13 @@ func buildRequestBlocksOf(p Period, f Filter, offset, limit int, now time.Time,
|
||||
setCodexActor(gateway.Chunk, gateway.Index, actor)
|
||||
gateway.Chunk.setCodexCompaction(gateway.Index, compactions[local])
|
||||
}
|
||||
all := append(append(slices.Clone(gateways), chunks...), others...)
|
||||
visit := func(fn func(timedRef, *Row)) {
|
||||
all = append(append(slices.Clone(gateways), chunks...), others...)
|
||||
visit = func(fn func(timedRef, *Row)) {
|
||||
var rows chunkRows
|
||||
// one row for the visit, written over for each: fn is not known
|
||||
// here, so a row of each would be an allocation of its own, and fn
|
||||
// keeps none of them
|
||||
var r Row
|
||||
for _, c := range all {
|
||||
open := rows.of(c)
|
||||
for i := range open.Rows {
|
||||
@@ -1234,11 +1307,17 @@ func buildRequestBlocksOf(p Period, f Filter, offset, limit int, now time.Time,
|
||||
if pr.Time.Before(since) || after(until, pr.Time) || len(skip) > 0 && skip[ref] || len(matched) > 0 && matched[ref] {
|
||||
continue
|
||||
}
|
||||
r := open.row(i)
|
||||
r = open.row(i)
|
||||
fn(timedOf(ref, pr), &r)
|
||||
}
|
||||
}
|
||||
}
|
||||
return all, visit
|
||||
}
|
||||
|
||||
func buildRequestBlocksOf(p Period, f Filter, offset, limit int, now time.Time, gateways, chunks, others []*rowChunk, names map[string]string, identities map[string]sessions.CodexIdentity) RequestPage {
|
||||
since := p.Since(now)
|
||||
all, visit := pairedRows(p, now, gateways, chunks, others, identities)
|
||||
out := RequestPage{Rows: []Row{}, Agents: []string{}, Providers: []string{}, Purposes: []string{}, By: map[string][]Share{}}
|
||||
chartFilter := f
|
||||
chartFilter.Day = ""
|
||||
|
||||
Reference in New Issue
Block a user