diff --git a/internal/gui/sessions.go b/internal/gui/sessions.go index 4cacae71e..6a82ec51a 100644 --- a/internal/gui/sessions.go +++ b/internal/gui/sessions.go @@ -82,7 +82,7 @@ func gatewaySession(g usage.GatewaySession) sessions.Session { func sessionRoutes(mux *http.ServeMux, w Windows) { warmSessions() - mux.HandleFunc("GET /api/sessions", func(rw http.ResponseWriter, r *http.Request) { + mux.HandleFunc("GET /api/sessions", coldTurn(func(rw http.ResponseWriter, r *http.Request) { // the list and the Codex threads' effort each list every session file defer sessions.OneListing()() n, _ := strconv.Atoi(r.URL.Query().Get("limit")) @@ -158,11 +158,11 @@ func sessionRoutes(mux *http.ServeMux, w Windows) { Terminal bool `json:"terminal"` Dirs []string `json:"dirs"` }{out.Terminal, out.Dirs}) - }) + })) // stats is every session's usage by day over the last ?days=N days // (every day when 0 or none), cut by agent, folder and model for the // page to filter; names are the agents' as the page shows them. - mux.HandleFunc("GET /api/sessions/stats", func(rw http.ResponseWriter, r *http.Request) { + mux.HandleFunc("GET /api/sessions/stats", coldTurn(func(rw http.ResponseWriter, r *http.Request) { defer sessions.ShareListing()() n, _ := strconv.Atoi(r.URL.Query().Get("days")) st, gen := statsForGen(n) @@ -199,11 +199,11 @@ func sessionRoutes(mux *http.ServeMux, w Windows) { statsTags.m[n] = statsTag{gen, agents, tag} statsTags.Unlock() } - }) + })) // overview sums up the range's sessions under the page's filters: // how many, what the middle one spent, how many each day, and the ones // that spent the most. - mux.HandleFunc("GET /api/sessions/overview", func(rw http.ResponseWriter, r *http.Request) { + mux.HandleFunc("GET /api/sessions/overview", coldTurn(func(rw http.ResponseWriter, r *http.Request) { defer sessions.ShareListing()() q := r.URL.Query() n, _ := strconv.Atoi(q.Get("days")) @@ -234,7 +234,7 @@ func sessionRoutes(mux *http.ServeMux, w Windows) { } } writeJSON(rw, out) - }) + })) // progress is how far the reading of the session files has got, for // the page to show while its first read takes a while. mux.HandleFunc("GET /api/sessions/progress", func(rw http.ResponseWriter, r *http.Request) { @@ -705,12 +705,54 @@ func warmUp(steps ...func()) { // that asks: an empty sandbox's warm-up looked at settings.json ~300 // times on Windows (#1665); a write still reads the disk (edit.Read) defer filememo.Hold()() + defer coldTurns.over.Store(true) for _, step := range steps { - step() + func() { + coldTurns.turn <- struct{}{} + defer func() { <-coldTurns.turn }() + step() + }() } debug.FreeOSMemory() } +// coldTurns has the warm-up's reads and the pages' first asks take turns +// until the warm-up is over. A first start's window asks the Sessions +// page's stats and the Usage page's requests and heatmap while the +// warm-up reads the same history, each a first read of it held beside the +// others: ~150 MB of the asks' and ~40 MB of the warm-up's at once, a +// ~300 MB live heap and a 420–480 MB footprint (#2278). Each reads after +// the one before it has kept what it read, and most then find it kept. +var coldTurns = struct { + turn chan struct{} + over atomic.Bool +}{turn: make(chan struct{}, 1)} + +// coldTurnMost is how long a page waits for its turn: a warm-up stuck on +// one read never keeps the pages from answering. +const coldTurnMost = 2 * time.Minute + +// coldTurnsTaken is false in tests, whose pages and warm-ups are their own. +var coldTurnsTaken = !testing.Testing() + +// coldTurn is h taking its turn while the warm-up isn't over; the +// warm-up's reading goes at full speed while it waits (held's +// sessions.Ask). +func coldTurn(h http.HandlerFunc) http.HandlerFunc { + return func(rw http.ResponseWriter, r *http.Request) { + if coldTurnsTaken && !coldTurns.over.Load() { + t := time.NewTimer(coldTurnMost) + select { + case coldTurns.turn <- struct{}{}: + defer func() { <-coldTurns.turn }() + case <-t.C: + } + t.Stop() + } + h(rw, r) + } +} + // underTightGC runs fn letting the heap grow by a quarter of what it holds // before a collection, not Go's default of all of it, and then puts back // what was set; a GOGC the user set lower, or off, is kept. The usage diff --git a/internal/gui/usage.go b/internal/gui/usage.go index 57d5e08c5..905db8bad 100644 --- a/internal/gui/usage.go +++ b/internal/gui/usage.go @@ -98,10 +98,10 @@ func requestContent(agent, session string, from, to, at time.Time) contentJSON { } func usageRoutes(mux *http.ServeMux, w Windows) { - mux.HandleFunc("GET /api/usage", func(rw http.ResponseWriter, r *http.Request) { + mux.HandleFunc("GET /api/usage", coldTurn(func(rw http.ResponseWriter, r *http.Request) { // the days and hours of the reader's zone, which the page names writeJSON(rw, usageState(periodOf(r.URL.Query().Get("period")), usageapi.ZoneOf(r))) - }) + })) // the providers a deleted one's calls can be merged into: those there // are now and shown, by name mux.HandleFunc("GET /api/usage/deleted/into", func(rw http.ResponseWriter, r *http.Request) { @@ -176,17 +176,17 @@ func usageRoutes(mux *http.ServeMux, w Windows) { writeJSON(rw, map[string]bool{"ok": true}) }) // the ledger: the period's calls, newest first, a page at a time - mux.HandleFunc("GET /api/usage/requests", func(rw http.ResponseWriter, r *http.Request) { + mux.HandleFunc("GET /api/usage/requests", coldTurn(func(rw http.ResponseWriter, r *http.Request) { q := r.URL.Query() offset, _ := strconv.Atoi(q.Get("offset")) limit, _ := strconv.Atoi(q.Get("limit")) writeJSON(rw, ledgerPage(periodOf(q.Get("period")), ledgerFilter(r), offset, limit)) - }) + })) // the heatmap (#1369): the last 53 weeks a day each, of the requests the // page's filters keep, read from the same index as the page - mux.HandleFunc("GET /api/usage/heatmap", func(rw http.ResponseWriter, r *http.Request) { + mux.HandleFunc("GET /api/usage/heatmap", coldTurn(func(rw http.ResponseWriter, r *http.Request) { writeJSON(rw, usage.HeatmapOf(ledgerFilter(r))) - }) + })) // what was said in one request, read from the agent's session file when the // row is opened, between two times (the call's own, or a gateway request's // span): magpie keeps no copy diff --git a/internal/gui/warm_turns_test.go b/internal/gui/warm_turns_test.go new file mode 100644 index 000000000..fb6e81f65 --- /dev/null +++ b/internal/gui/warm_turns_test.go @@ -0,0 +1,73 @@ +package gui + +import ( + "net/http" + "net/http/httptest" + "sync/atomic" + "testing" + "time" +) + +// Until the warm-up is over, a page's first ask and each of its reads +// take turns: none holds a first read of the history beside another +// (#2278). Once it is over, a page asks at once. +func TestColdTurnsTaken(t *testing.T) { + coldTurnsTaken = true + coldTurns.over.Store(false) + t.Cleanup(func() { coldTurnsTaken = false; coldTurns.over.Store(false) }) + var inStep, inPage, both atomic.Int32 + asked, release := make(chan struct{}), make(chan struct{}) + page := coldTurn(func(http.ResponseWriter, *http.Request) { + inPage.Add(1) + if inStep.Load() > 0 { + both.Add(1) + } + inPage.Add(-1) + }) + ask := func() { + page(httptest.NewRecorder(), httptest.NewRequest("GET", "/api/usage/heatmap", nil)) + } + warmed := make(chan struct{}) + go func() { + defer close(warmed) + warmUp(func() { + inStep.Add(1) + defer inStep.Add(-1) + <-asked + <-release + }, func() { + inStep.Add(1) + defer inStep.Add(-1) + if inPage.Load() > 0 { + both.Add(1) + } + }) + }() + answered := make(chan struct{}) + go func() { + close(asked) + ask() + close(answered) + }() + select { + case <-answered: + t.Fatal("the page was answered while the warm-up's step read") + case <-time.After(50 * time.Millisecond): + } + close(release) + <-answered + <-warmed + if both.Load() != 0 { + t.Fatal("a page and a warm-up step read at once") + } + // once the warm-up is over, nothing waits + coldTurns.turn <- struct{}{} + defer func() { <-coldTurns.turn }() + done := make(chan struct{}) + go func() { ask(); close(done) }() + select { + case <-done: + case <-time.After(10 * time.Second): + t.Fatal("a page after the warm-up waited for a turn") + } +}