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?