]> Cypherpunks repositories - gostls13.git/commitdiff
internal/trace/traceviewer: make the mmu handler more self-contained
authorMichael Anthony Knyszek <mknyszek@google.com>
Sun, 12 Nov 2023 23:27:31 +0000 (23:27 +0000)
committerMichael Knyszek <mknyszek@google.com>
Tue, 21 Nov 2023 21:27:53 +0000 (21:27 +0000)
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 <mknyszek@google.com>
Reviewed-by: Michael Pratt <mpratt@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>

src/cmd/trace/main.go
src/cmd/trace/v2/main.go
src/internal/trace/traceviewer/mmu.go

index cb39c08aa7c8d86d1d54630203d83eadbcef901b..b66980e9ac9f3bc8a2eceb94ed4f8fa1f4a3231e 100644 (file)
@@ -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))
index 38be4d2fe291a4b052faff72e3578c6963473fd5..51b5ebf6cf4f4899ea3deea84d92f3ccc82cbd13 100644 (file)
@@ -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)
index 42bf82774dbe421562c9f71c9d71233b462f7e4e..0cb2b42657b53a99e97e8a3f8ce1860c51eb805e 100644 (file)
@@ -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 = `<!doctype html>
         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 = `<!doctype html>
         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');