Go block profiler — screenshot of github.com

Go block profiler

I use the Go block profiler to analyze how much time my Go program spends waiting on blocking operations like channel sends/receives and mutex acquisitions. It helps me identify contention points.

Visit github.com →

Questions & Answers

What is the Go block profiler?
The Go block profiler is a tool that allows developers to analyze the amount of time a Go program spends waiting on specific blocking operations. These operations include channel sends/receives, semaphore acquisitions (like sync.Mutex.Lock), and condition variable waits (sync.Cond.Wait). It helps identify bottlenecks caused by goroutine suspension.
Who should use the Go block profiler?
The Go block profiler is intended for developers and performance engineers who need to diagnose and optimize the performance of Go applications. It is particularly useful when a program experiences high CPU usage without clear computation, indicating contention or frequent waiting states.
How does Go block profiling compare to goroutine profiling?
Block profiling specifically tracks time spent waiting on a subset of blocking operations where the goroutine is parked. In contrast, goroutine profiling (with debug=2) covers all waiting states, including I/O, GC, and runtime internal locks, and can show ongoing blocking events that haven't completed.
When should I enable the Go block profiler?
You should enable the Go block profiler when your application exhibits unexpected performance issues or high latency that might be due to contention or goroutines frequently waiting on shared resources. It helps pinpoint exact code locations causing significant blocking.
How do you enable and configure the Go block profiler?
The block profiler is disabled by default and can be enabled by calling runtime.SetBlockProfileRate(rate). A rate of 1 tracks every blocking event, while a rate greater than 1 samples events; for example, rate=10000 (10µs) typically has negligible overhead in production. The collected profile can be accessed via pprof.Lookup("block").WriteTo(...).