Go: Discovery of the Trace Package
ℹ️ This article is based on Go 1.13.
Go provides us a tool to enable tracing during the runtime and get a detailed view of the execution of our program. This tool can be enabled by flag -trace
with the tests, frompprof
to get live tracing, or anywhere in our code thanks to the trace
package. This tool can be even more powerful since you can enhance it with your own traces. Let’s review how it works.
Flow
The flow of the tool is quite straightforward. Each event, such as memory allocation; all the phases of garbage collector; goroutines when they run, pause, etc. is statically recorded by the Go standard library and formatted to be displayed later. However, before the recording starts, Go first “stops the world” and takes a snapshot of the current goroutines and their states.
You can find more details about this phase in my article “Go: How Does Go Stop the World?”
This will later allow Go to construct the cycle of life of each goroutine properly. Here is the flow:
Then, collected events are pushed to a buffer that is later flushed to a list of full buffers when the max capacity is…