From ff07b73faced25076548b8d916ff19de1844650a Mon Sep 17 00:00:00 2001 From: Michael Anthony Knyszek Date: Sun, 12 Nov 2023 23:27:31 +0000 Subject: [PATCH] internal/trace/traceviewer: make the mmu handler more self-contained The last change made the MMU rendering code common and introduced a new API, but it was kind of messy. Part of the problem was that some of the Javascript in the template for the main page referred to specific endpoints on the server. Fix this by having the Javascript access the same endpoint but with a different query variable. Now the Javascript code doesn't depend on specific endpoints, just on query variables for the current endpoint. For #60773. For #63960. Change-Id: I1c559d9859c3a0d62e2094c9d4ab117890b63b31 Reviewed-on: https://go-review.googlesource.com/c/go/+/541259 Auto-Submit: Michael Knyszek Reviewed-by: Michael Pratt LUCI-TryBot-Result: Go LUCI --- src/cmd/trace/main.go | 4 ++-- src/cmd/trace/v2/main.go | 11 +++++------ src/internal/trace/traceviewer/mmu.go | 22 +++++++++++++--------- 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/src/cmd/trace/main.go b/src/cmd/trace/main.go index cb39c08aa7c8d8..b66980e9ac9f3b 100644 --- a/src/cmd/trace/main.go +++ b/src/cmd/trace/main.go @@ -139,8 +139,8 @@ func main() { log.Printf("Opening browser. Trace viewer is listening on %s", addr) browser.Open(addr) - // Install MMU handlers. - traceviewer.InstallMMUHandlers(http.DefaultServeMux, ranges, mutatorUtil) + // Install MMU handler. + http.HandleFunc("/mmu", traceviewer.MMUHandlerFunc(ranges, mutatorUtil)) // Install main handler. http.Handle("/", traceviewer.MainHandler(ranges)) diff --git a/src/cmd/trace/v2/main.go b/src/cmd/trace/v2/main.go index 38be4d2fe291a4..51b5ebf6cf4f48 100644 --- a/src/cmd/trace/v2/main.go +++ b/src/cmd/trace/v2/main.go @@ -62,6 +62,10 @@ func Main(traceFile, httpAddr, pprof string, debug int) error { log.Printf("Opening browser. Trace viewer is listening on %s", addr) browser.Open(addr) + mutatorUtil := func(flags trace.UtilFlags) ([][]trace.MutatorUtil, error) { + return trace.MutatorUtilizationV2(parsed.events, flags), nil + } + mux := http.NewServeMux() mux.Handle("/", traceviewer.MainHandler(ranges)) mux.Handle("/trace", traceviewer.TraceHandler()) @@ -69,12 +73,7 @@ func Main(traceFile, httpAddr, pprof string, debug int) error { mux.Handle("/static/", traceviewer.StaticHandler()) mux.HandleFunc("/goroutines", GoroutinesHandlerFunc(gSummaries)) mux.HandleFunc("/goroutine", GoroutineHandler(gSummaries)) - - // Install MMU handlers. - mutatorUtil := func(flags trace.UtilFlags) ([][]trace.MutatorUtil, error) { - return trace.MutatorUtilizationV2(parsed.events, flags), nil - } - traceviewer.InstallMMUHandlers(mux, ranges, mutatorUtil) + mux.HandleFunc("/mmu", traceviewer.MMUHandlerFunc(ranges, mutatorUtil)) err = http.Serve(ln, mux) return fmt.Errorf("failed to start http server: %w", err) diff --git a/src/internal/trace/traceviewer/mmu.go b/src/internal/trace/traceviewer/mmu.go index 42bf82774dbe42..0cb2b42657b53a 100644 --- a/src/internal/trace/traceviewer/mmu.go +++ b/src/internal/trace/traceviewer/mmu.go @@ -40,19 +40,23 @@ import ( type MutatorUtilFunc func(trace.UtilFlags) ([][]trace.MutatorUtil, error) -func InstallMMUHandlers(mux *http.ServeMux, ranges []Range, f MutatorUtilFunc) { +func MMUHandlerFunc(ranges []Range, f MutatorUtilFunc) http.HandlerFunc { mmu := &mmu{ cache: make(map[trace.UtilFlags]*mmuCacheEntry), f: f, ranges: ranges, } - mux.HandleFunc("/mmu", func(w http.ResponseWriter, r *http.Request) { - // N.B. templMMU has Javascript that implicitly relies upon the existence - // of /mmuPlot and /mmuDetails on the same server. + return func(w http.ResponseWriter, r *http.Request) { + switch r.FormValue("mode") { + case "plot": + mmu.HandlePlot(w, r) + return + case "details": + mmu.HandleDetails(w, r) + return + } http.ServeContent(w, r, "", time.Time{}, strings.NewReader(templMMU)) - }) - mux.HandleFunc("/mmuPlot", mmu.HandlePlot) - mux.HandleFunc("/mmuDetails", mmu.HandleDetails) + } } var utilFlagNames = map[string]trace.UtilFlags{ @@ -209,7 +213,7 @@ var templMMU = ` container.css('opacity', '.5'); refreshChart.count++; var seq = refreshChart.count; - $.getJSON('/mmuPlot?flags=' + mmuFlags()) + $.getJSON('?mode=plot&flags=' + mmuFlags()) .fail(function(xhr, status, error) { alert('failed to load plot: ' + status); }) @@ -282,7 +286,7 @@ var templMMU = ` var details = $('#details'); details.empty(); var windowNS = curve[items[0].row][0]; - var url = '/mmuDetails?window=' + windowNS + '&flags=' + mmuFlags(); + var url = '?mode=details&window=' + windowNS + '&flags=' + mmuFlags(); $.getJSON(url) .fail(function(xhr, status, error) { details.text(status + ': ' + url + ' could not be loaded');