Mar 8, 2019

[Go][sum up] profiling

Reference:
https://blog.golang.org/profiling-go-programs
https://golang.org/pkg/runtime/pprof/
https://golang.org/pkg/runtime/pprof/#Profile
https://rakyll.org/custom-profiles/
https://rakyll.org/mutexprofile/
https://github.com/google/pprof
https://github.com/golang/go/issues/13841


Definition:
Profiles are only as good as the kernel support used to generate them.

A Profile is a collection of stack traces showing the call sequences that led to instances of a particular event, such as allocation.

Packages can create and maintain their own profiles; the most common use is for tracking resources that must be explicitly closed, such as files or network connections.

A Profile's methods can be called from multiple goroutines simultaneously.

Each Profile has a unique name.

A few profiles are predefined:
  • cpu
    CPU profile shows where a program spends its time while actively consuming CPU cycles (opposed to sleep or wait for I/O).
    CPU profile is not enabled by default; use StartCPUProfile to enable it (StopCPUProfile to stop it in the program).
    If the sample turns out 0, means your program isn't run long enough, or you could set the CPU profile rate:
    https://golang.org/pkg/runtime/#SetCPUProfileRate
  • goroutine
    Goroutine profile reports stack traces of all current goroutines.
  • heap
    Heap profile reports the current live allocations; monitors current memory usage and check for memory leaks.
  • allocs
    Allocs profile samples all past memory allocations.
  • threadcreate
    Thread creation profile stack traces that led to the creation of new OS threads
  • block
    Block profile reports where goroutines block waiting on synchronization primitives (including timer channels).
    Block profile is not enabled by default; use runtime.SetBlockProfileRate to enable it.
  • mutex
    Mutex profile reports the lock contentions. Useful for debugging mutex contention. Mutex profile is not enabled by default, use runtime.SetMutexProfileFraction to enable it.

Main URL:
http://localhost:4242/debug/pprof/


Sub URL:
http://localhost:4242/debug/pprof/goroutine
http://localhost:4242/debug/pprof/heap
http://localhost:4242/debug/pprof/allocs
http://localhost:4242/debug/pprof/threadcreate
http://localhost:4242/debug/pprof/block
http://localhost:4242/debug/pprof/mutex
http://localhost:4242/debug/pprof/profile
http://localhost:4242/debug/pprof/trace?seconds=5

Use "go tool pprof" to analyze these profiles (lists of stack traces),
Use "go tool trace" to analyze trace endpoint (/debug/pprof/trace?seconds=5)


cmd:
$ go tool pprof --help 

$ go tool pprof http://localhost:4242/debug/pprof/heap
$ go tool pprof http://localhost:4242/debug/pprof/profile?seconds=30

Look at the goroutine blocking profile,
after calling runtime.SetBlockProfileRate in the program:
$ go tool pprof http://localhost:4242/debug/pprof/block

Look at the holders of contended mutexes,
after calling runtime.SetMutexProfileFraction in your program:
$ go tool pprof http://localhost:4242/debug/pprof/mutex

Read heap information
$ go tool pprof -top http://localhost:4242/debug/pprof/heap

Generate png
$ go tool pprof -png http://localhost:4242/debug/pprof/heap > out.png

Profile benchmarks and the contention on your mutexes.
$ go test bench=. -mutexprofile=mutex.out

analyzing the Mutex contention ropfile
$ go tool pprof runtime.test mutex.out

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.