docs/debug/pprof.md
A Profile is a collection of stack traces showing the call sequences that led to instances of a particular event, such as allocation.
go install github.com/google/pprof@latest
(num_samples * 10ms)/1000alloc_objects and alloc_space refer to allocations since the start of the profiling period.
inuse_objects and inuse_space refers to allocations that have not been freed.
inuse_objects = allocations - freesOpen a profile in the terminal:
go tool pprof profile.prof
go tool pprof heap.prof
go tool pprof goroutine.prof
Open a profile in a web browser:
go tool pprof -http=:8080 profile.prof
If the correct source isn't detected automatically, specify the location of the source associated with the profile:
go tool pprof -http=:9090 -source_path=/Users/freddy/go/src/github.com/hashicorp/consul-enterprise profile.prof
Useful in annotated Source view which shows time on a line-by-line basis.
Important: Ensure that the source code matches the version of the binary! The source view relies on line numbers for its annotations.
Compare two profiles:
go tool pprof -http=:8080 -base before/profile.prof after/profile.prof
This comparison will subtract the -base profile from the given profile. In this case, "before" is subtracted from "after".
Useful commands when profile is opened in terminal:
top lists the top 10 nodes by valuelist <function regex> lists the top matches to the patterntop command in terminal view.Example:
func foo(){
a() // step 1 takes 1s
do something directly. // step 2 takes 3s
b() // step 3 takes 1s
}
flat is the time spent on step 2 (3s), while cum is time spent on steps 1 to 3.
Path Value
Node Color:
Node Font Size:
Edge Weight:
Edge Color:
Dashed Edges: some locations between the two connected locations were removed.
Solid Edges: one location directly calls the other.
"(inline)" Edge Marker: the call has been inlined into the caller. More on inlining: Inlining optimisations in Go | Dave Cheney
Example graph:
(*Store).Services()(*Store).Services() has both large flat and cumulative values, so the font is large and the box is red.mapassign_faststr and (radixIterator).Next() are solid and red because these are direct calls with large positive values.A collection of stack traces, where each stack is a column of boxes, and each box represents a function.
Functions at the top of the flame graph are parents of functions below.
The width of each box is proportional to the number of times it was observed during sampling.
Mouse-over boxes shows cum value and percentage, while clicking on boxes lets you zoom into their stack traces.