Go Tools — Trace
A tool helping you understand Go execution tracer
Have you ever used
go tool trace command ?
If you have not, this is the right place to get grounded.
But before going for it, I need to introduce a concept “Go execution tracer”
What the hell is the Go Execution Tracer?
As the name suggests, it’s a tracer that tracks down all activities about “Execution”.
What do you mean by “Execution” here ?
It’s a collection of every single execution happens in a program such as how many goroutines are executing, for how long, and when are they blocking for what ? and how does Garbage Collector affect execution of individual goroutine? You can literally have fine-grained granular information about all those things by leveraging “Go Execution Tracer”.
So, how can I use the Go Execution Tracer? and what does it have to do with the
go tool tracecommand you’re talking about?
Hang on bro, let me talk you through that now with an example.
Let’s take a look at this example code.
This is one way of generating trace data out of a Go application.
using “runtime/trace” package, you can start or stop tracing.
so, line 9 starts tracing and output to standard error.
and once the main function returns, the deferred function trace.Stop() will be called to stop tracing.
But to output the trace data to a file, you need to run this command
go run main.go 2> trace.out
then, you will get this output in your filesystem.
➜ tracer git:(master) ✗ go run main.go 2> trace.out
➜ tracer git:(master) ✗ tree .
└── trace.out0 directories, 2 files
➜ tracer git:(master) ✗
Okay, I’ve been sitting tight and waiting to use the
go tool tracebut you’re testing my patience…
Calm down bro, now you can go head and run the command. here you are.
➜ tracer git:(master) ✗ go tool trace trace.out
2022/09/15 11:26:13 Parsing trace...
2022/09/15 11:26:13 Splitting trace...
2022/09/15 11:26:13 Opening browser. Trace viewer is listening on http://127.0.0.1:49924
Cool! what’s happening here ? it’s opening browser ?
Yes, yes. Check out a popped-up window and see what it contains.
You may see something like this.
For the simplicity, let’s just briefly talk about “View trace” first today.
As the title says, it’s going to give you all the details about event timelines for all running goroutine. Clicking through it shows you this.
Because the the app ran very short, the duration of trace data is also short.
Now, you have all the trace data to understand what’s happend with your application, which means you know now what the “go tool trace” does.
About comprehending the data in the chart, I will continue on the next article.