How to view trace information generated by `runtime / trace`?

Consider the following program, which simply twists a few goroutines, and then waits for them to complete and transmit the signal through the channel they made.

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 exits, the output is a binary file called /tmp/Trace.out . Then I tried to view the trace using the trace facility as follows.

 go tool trace -http=localhost:8080 ./Main /tmp/Trace.out 

This leads to the launch of a page with a link to View Trace (along with viewing other links that give only aggregate data), but when you click on this link a blank page appears. When I look at the source of this page, I see the following source, which seems to imply that JSON is expected, not 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 to view event tracking with Go tools?

+5
source share
2 answers

I can confirm @widsvc comment: you are probably using an incompatible browser like Firefox.

It works as expected with Chrome.

Looking under the hood, you can see this error in the Firefox console:

ReferenceError: tr undefined trace: 7: 9

After a quick search, the following appears: the tracer embedded in Chrome: https://www.chromium.org/developers/how-tos/trace-event-profiling-tool .

I tried using my standalone trace2html tool in the contents of http: // localhost: 8080 / jsontrace , but the result still gives me errors with Firefox (the first โ€œdocument.registerElement is not a functionโ€, and after fixing that others keep coming)

+2
source

In case someone is on my feet, it turns out that a trace finder has also been found in the Chrome browser.

It has been fixed in a recent CL: https://go-review.googlesource.com/#/c/22013/

+2
source

Source: https://habr.com/ru/post/1235154/


All Articles