Diagnose Scheduler and Lock Contention With go tool trace and Mutex Profiles
Capture -mutexprofile and -trace outputs from Go benchmarks to pinpoint lock contention and scheduler stalls. After this, you spot hidden blocking waits that CPU profiles miss.
Your API's p99 latency just jumped from 15 ms to 280 ms under load, yet CPU usage is sitting at 18% and memory is flat. When you capture a standard -cpuprofile, nothing stands out. Why?
Because CPU profiles only sample goroutines that are actively running instructions on a CPU core. If 80 goroutines are asleep waiting in line behind a single contended sync.Mutex, they burn 0% CPU while your request latency explodes. That is what -mutexprofile and the execution tracer (-trace) are built to uncover.
Skip ahead
Jump straight to capturing -mutexprofile and -trace or comparing diagnostic tools.
1. Capture a mutex contention profile and execution trace
Mutex profiling is off by default (0). Enable it with runtime.SetMutexProfileFraction(1) in a parallel benchmark (or a sampled fraction like 100 in live services) and pass -mutexprofile and -trace:
package traceprof
import (
"runtime"
"sync"
"testing"
"time"
)
func BenchmarkContendedMutex(b *testing.B) {
runtime.SetMutexProfileFraction(1)
defer runtime.SetMutexProfileFraction(0)
var mu sync.Mutex
var n int
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
mu.Lock()
n++
time.Sleep(1 * time.Microsecond)
mu.Unlock()
}
})
}
$ go test -bench=BenchmarkContendedMutex -mutexprofile=mutex.out -trace=trace.out -benchtime=50ms .
$ go tool pprof -top -nodecount=4 mutex.out
File: traceprof.test
Type: delay
Showing nodes accounting for 81.65ms, 100% of 81.65ms total
flat flat% sum% cum cum%
81.65ms 100% 100% 81.65ms 100% sync.(*Mutex).Unlock (inline)
0 0% 100% 81.65ms 100% example.com/traceprof.BenchmarkContendedMutex.func1
2. Why sync.(*Mutex).Unlock appears at the top
That output confuses almost everyone the first time they see it: why does Unlock() show 81.65ms of delay? Because Go records contention against the goroutine that held the lock and finally released it in Unlock()—telling you which critical section caused other goroutines to wait! Look one caller up in cum (or run go tool pprof -list=BenchmarkContendedMutex mutex.out) to see the exact lock site.
Next, open go tool trace trace.out in your browser to inspect per-goroutine timelines: you can see every moment a goroutine sits in Runnable (waiting for a CPU core) vs. Waiting (blocked on a mutex, channel, or GC pause).
Which diagnostic captures which bottleneck?
| Tool / Flag | Shows | Misses |
|---|---|---|
-cpuprofile |
Hot computation loops | Sleeping goroutines blocked on locks, channels, or I/O |
-mutexprofile |
Wait time caused by contended sync.Mutex / RWMutex |
Channel waits and non-mutex syscalls |
-blockprofile |
Time blocked on channels, mutexes, and select |
Requires runtime.SetBlockProfileRate; can add overhead if rate is 1 |
-trace |
Microsecond timeline of scheduler stalls and GC pauses | Large output files if left running longer than a few seconds |
💡 Tip
Once -mutexprofile fingers a hot lock, fix it by moving network/disk I/O outside the lock, sharding a single global map into N buckets (buckets[hash(k)%N]), or replacing a single counter lock with atomic.Int64.
Common mistakes
- Forgetting
runtime.SetMutexProfileFraction: Leaving it at default0records an emptymutex.out. - Holding a
sync.Mutexduring an HTTP call or SQL query: One slow downstream call stalls every other goroutine waiting on that lock. - Capturing 5-minute traces under heavy traffic: Keep
go tool tracecaptures to 1–5 seconds so the browser UI loads quickly.
⚠ Warning
Never assume low CPU usage means your Go service is healthy. Whenever tail latency rises while CPU stays low, check -mutexprofile and go tool trace immediately.
What happens if you skip this
Teams relying solely on CPU profiles chase non-existent computation bottlenecks while requests silently queue behind one contended lock or scheduler stall.
Related course: Go Advanced.
About
Capture -mutexprofile and -trace outputs from Go benchmarks to pinpoint lock contention and scheduler stalls. After this, you spot hidden blocking waits that CPU profiles miss.
- Responsible
- Jay P
- Last Update
- 11/27/2026
- Reading Time
- 4 min
Reviews
Sign in to write a reviewComments
Sign in to commentNo comments yet.