| name | go-profiling |
| description | Profile Go applications with pprof: CPU, memory, goroutine, block, and mutex profiles. Trigger: when profiling Go applications, memory leaks, high CPU usage, goroutine leaks, pprof, performance analysis, flame graphs |
| version | 1 |
| argument-hint | [profile-type] (cpu|heap|goroutine|block|mutex|trace) |
| allowed-tools | ["bash","read","write","grep","glob"] |
Go Profiling (pprof)
You are now operating in Go profiling mode.
Enable pprof in Your Application
Add this import to your main package (net/http/pprof registers handlers automatically):
import _ "net/http/pprof"
go func() {
log.Println(http.ListenAndServe("localhost:6060", nil))
}()
Profile Types
| Profile | URL Path | What it Measures | When to Use |
|---|
heap | /debug/pprof/heap | Memory allocations | Memory leaks, high RSS |
profile | /debug/pprof/profile | CPU usage | Slow requests, high CPU |
goroutine | /debug/pprof/goroutine | Goroutine stacks | Goroutine leaks, deadlocks |
block | /debug/pprof/block | Blocking operations | Channel/mutex contention |
mutex | /debug/pprof/mutex | Mutex contention | Lock contention |
allocs | /debug/pprof/allocs | All memory allocations | Allocation churn |
trace | /debug/pprof/trace | Execution trace | Latency analysis |
Capture Profiles
go tool pprof http://localhost:6060/debug/pprof/heap
go tool pprof http://localhost:6060/debug/pprof/profile?seconds=30
go tool pprof http://localhost:6060/debug/pprof/goroutine
go tool pprof http://localhost:6060/debug/pprof/block
go tool pprof http://localhost:6060/debug/pprof/mutex
curl -s http://localhost:6060/debug/pprof/heap > heap.pprof
curl -s "http://localhost:6060/debug/pprof/profile?seconds=30" > cpu.pprof
Analyze Profiles
go tool pprof -top heap.pprof
go tool pprof -top -nodecount=20 cpu.pprof
go tool pprof -svg heap.pprof > heap.svg
go tool pprof -web heap.pprof
go tool pprof -http=:8081 heap.pprof
Enable Block and Mutex Profiling
Block and mutex profiles require runtime configuration before they collect data:
import "runtime"
runtime.SetBlockProfileRate(1)
runtime.SetMutexProfileFraction(1)
Goroutine Stack Dump (Text)
curl http://localhost:6060/debug/pprof/goroutine?debug=2
curl -s http://localhost:6060/debug/pprof/goroutine?debug=1 | grep "^goroutine"
Execution Trace
curl -s "http://localhost:6060/debug/pprof/trace?seconds=5" > trace.out
go tool trace trace.out
Benchmark Profiling
go test -bench=BenchmarkFoo -cpuprofile cpu.pprof -memprofile mem.pprof ./...
go tool pprof cpu.pprof
go tool pprof mem.pprof
Reading Top Output
(pprof) top10
Showing nodes accounting for 42.31s, 91.30% of 46.34s total
flat flat% sum% cum cum%
15.32s 33.06% 33.06% 15.32s 33.06% runtime.mallocgc
8.21s 17.72% 50.78% 8.21s 17.72% runtime.scanobject
flat: time spent in this function itself
cum: cumulative time including callees
- High
flat + low cum = the function itself is slow
- Low
flat + high cum = this function calls slow functions
Memory Leak Detection Pattern
curl -s http://localhost:6060/debug/pprof/heap > heap1.pprof
sleep 60
curl -s http://localhost:6060/debug/pprof/heap > heap2.pprof
go tool pprof -base heap1.pprof heap2.pprof