Go: Samples Collection with pprof

Vincent
A Journey With Go
Published in
3 min readMay 5, 2020

--

Illustration created for “A Journey With Go”, made from the original Go Gopher, created by Renee French.

ℹ️ This article is based on Go 1.13.

pprof is a tool for analysis profiling data such as CPU or memory allocation. Profiling an application requires the collection of data at the runtime to aggregate them later and generate graphs. Let’s dive now into the workflow of this data collection and see how to tune it.

Workflow

pprof collects data on a fixed interval basis, defined by the number of collections per second. The default parameter is 100, meaning pprof will collect one hundred times data per second, i.e., every 10 milliseconds.

Starting pprof can be done by calling StartCPUProfile:

func main() {
f, _ := os.Create(`cpu.prof`)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()

...
}

This process will automatically set a timer on the running thread (represented by M on the schema) that allows Go to collect profiling on a regular basis. Here is a first diagram:

For more information about the P,M,G representation, I suggest you read my article “Go: Goroutine, OS Thread and CPU

--

--