Skip to content

Commit

Permalink
internal/trace/traceviewer: make the mmu handler more self-contained
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
mknyszek committed Nov 21, 2023
1 parent c785be4 commit ff07b73
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/cmd/trace/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
11 changes: 5 additions & 6 deletions src/cmd/trace/v2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,18 @@ 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())
mux.Handle("/jsontrace", JSONTraceHandler(parsed))
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)
Expand Down
22 changes: 13 additions & 9 deletions src/internal/trace/traceviewer/mmu.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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);
})
Expand Down Expand Up @@ -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');
Expand Down

0 comments on commit ff07b73

Please sign in to comment.