Consider the following program, which simply spins up a few goroutines and then waits for them to finish and signal over the channel that they are done.
package main
import (
"os"
"runtime/trace"
"time"
)
func doWork(c chan int) {
startTime := time.Now()
i := 0
for curTime := startTime; curTime.Sub(startTime) < 2; curTime = time.Now() {
i++
}
c <- i
}
func main() {
numGoRoutine := 10
traceOutFile, _ := os.OpenFile("/tmp/Trace.out", os.O_WRONLY|os.O_CREATE, os.ModeExclusive|os.ModePerm)
trace.Start(traceOutFile)
// Start goroutines
termChannel := make(chan int)
for i := 0; i < numGoRoutine; i++ {
go doWork(termChannel)
}
// Wait for completion
for i := 0; i < numGoRoutine; i++ {
<-termChannel
}
trace.Stop()
}
When this program terminates, the output is a binary file called /tmp/Trace.out
. Next, I tried to view the trace by using the trace tool as follows.
go tool trace -http=localhost:8080 ./Main /tmp/Trace.out
This produces launches a page with a link to View Trace
(together with a view other links that give only aggregate data), but clicking on that link results in a blank page. When I view the source of that page, I see the following source, which seems imply that JSON is expected rather than binary.
<html>
<head>
<link href="/trace_viewer_html" rel="import">
<script>
document.addEventListener("DOMContentLoaded", function(event) {
var viewer = new tr.TraceViewer('/jsontrace');
document.body.appendChild(viewer);
});
</script>
</head>
<body>
</body>
</html>
How can I view the event-by-event trace using Go tools?
I can confirm @widsvc's comment: you are probably using an incompatible browser like Firefox.
It works as expected with Chrome.
Looking under the hood, you can see in Firefox's console this error:
ReferenceError: tr is not defined trace:7:9
After a quick search, it appears this uses trace-viewer which is embedded in Chrome: https://www.chromium.org/developers/how-tos/trace-event-profiling-tool.
I tried using their stand-alone trace2html
tool on the content of http://localhost:8080/jsontrace, but the output still gives me errors with Firefox (first "document.registerElement is not a function", and after fixing that one others keep coming)
In case anyone finds themselves in my shoes, it turns out the trace viewer was broken in Chrome 49, too.
It has been fixed in a recent CL: https://go-review.googlesource.com/#/c/22013/