Skip to content

Commit ff07b73

Browse files
committed
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 <[email protected]> Reviewed-by: Michael Pratt <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent c785be4 commit ff07b73

File tree

3 files changed

+20
-17
lines changed

3 files changed

+20
-17
lines changed

src/cmd/trace/main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ func main() {
139139
log.Printf("Opening browser. Trace viewer is listening on %s", addr)
140140
browser.Open(addr)
141141

142-
// Install MMU handlers.
143-
traceviewer.InstallMMUHandlers(http.DefaultServeMux, ranges, mutatorUtil)
142+
// Install MMU handler.
143+
http.HandleFunc("/mmu", traceviewer.MMUHandlerFunc(ranges, mutatorUtil))
144144

145145
// Install main handler.
146146
http.Handle("/", traceviewer.MainHandler(ranges))

src/cmd/trace/v2/main.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,18 @@ func Main(traceFile, httpAddr, pprof string, debug int) error {
6262
log.Printf("Opening browser. Trace viewer is listening on %s", addr)
6363
browser.Open(addr)
6464

65+
mutatorUtil := func(flags trace.UtilFlags) ([][]trace.MutatorUtil, error) {
66+
return trace.MutatorUtilizationV2(parsed.events, flags), nil
67+
}
68+
6569
mux := http.NewServeMux()
6670
mux.Handle("/", traceviewer.MainHandler(ranges))
6771
mux.Handle("/trace", traceviewer.TraceHandler())
6872
mux.Handle("/jsontrace", JSONTraceHandler(parsed))
6973
mux.Handle("/static/", traceviewer.StaticHandler())
7074
mux.HandleFunc("/goroutines", GoroutinesHandlerFunc(gSummaries))
7175
mux.HandleFunc("/goroutine", GoroutineHandler(gSummaries))
72-
73-
// Install MMU handlers.
74-
mutatorUtil := func(flags trace.UtilFlags) ([][]trace.MutatorUtil, error) {
75-
return trace.MutatorUtilizationV2(parsed.events, flags), nil
76-
}
77-
traceviewer.InstallMMUHandlers(mux, ranges, mutatorUtil)
76+
mux.HandleFunc("/mmu", traceviewer.MMUHandlerFunc(ranges, mutatorUtil))
7877

7978
err = http.Serve(ln, mux)
8079
return fmt.Errorf("failed to start http server: %w", err)

src/internal/trace/traceviewer/mmu.go

+13-9
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,23 @@ import (
4040

4141
type MutatorUtilFunc func(trace.UtilFlags) ([][]trace.MutatorUtil, error)
4242

43-
func InstallMMUHandlers(mux *http.ServeMux, ranges []Range, f MutatorUtilFunc) {
43+
func MMUHandlerFunc(ranges []Range, f MutatorUtilFunc) http.HandlerFunc {
4444
mmu := &mmu{
4545
cache: make(map[trace.UtilFlags]*mmuCacheEntry),
4646
f: f,
4747
ranges: ranges,
4848
}
49-
mux.HandleFunc("/mmu", func(w http.ResponseWriter, r *http.Request) {
50-
// N.B. templMMU has Javascript that implicitly relies upon the existence
51-
// of /mmuPlot and /mmuDetails on the same server.
49+
return func(w http.ResponseWriter, r *http.Request) {
50+
switch r.FormValue("mode") {
51+
case "plot":
52+
mmu.HandlePlot(w, r)
53+
return
54+
case "details":
55+
mmu.HandleDetails(w, r)
56+
return
57+
}
5258
http.ServeContent(w, r, "", time.Time{}, strings.NewReader(templMMU))
53-
})
54-
mux.HandleFunc("/mmuPlot", mmu.HandlePlot)
55-
mux.HandleFunc("/mmuDetails", mmu.HandleDetails)
59+
}
5660
}
5761

5862
var utilFlagNames = map[string]trace.UtilFlags{
@@ -209,7 +213,7 @@ var templMMU = `<!doctype html>
209213
container.css('opacity', '.5');
210214
refreshChart.count++;
211215
var seq = refreshChart.count;
212-
$.getJSON('/mmuPlot?flags=' + mmuFlags())
216+
$.getJSON('?mode=plot&flags=' + mmuFlags())
213217
.fail(function(xhr, status, error) {
214218
alert('failed to load plot: ' + status);
215219
})
@@ -282,7 +286,7 @@ var templMMU = `<!doctype html>
282286
var details = $('#details');
283287
details.empty();
284288
var windowNS = curve[items[0].row][0];
285-
var url = '/mmuDetails?window=' + windowNS + '&flags=' + mmuFlags();
289+
var url = '?mode=details&window=' + windowNS + '&flags=' + mmuFlags();
286290
$.getJSON(url)
287291
.fail(function(xhr, status, error) {
288292
details.text(status + ': ' + url + ' could not be loaded');

0 commit comments

Comments
 (0)