Reading the Go scheduler trace

A service can sit at 15% CPU and still be slow. The usual explanation is a lock or a syscall, and the usual way to confirm it is the execution trace, not the CPU profile.

go test -trace trace.out ./...
go tool trace trace.out

The trace viewer separates goroutine time into running, runnable, and blocked. A goroutine that is runnable but not running is waiting for a P. A goroutine blocked on a syscall is not consuming a P, which is why a program can look idle while every request is stuck in read.

Three patterns worth recognizing

Runnable queues that never drain. The scheduler is saturated. The fix is usually fewer goroutines per request or a bounded worker pool, not a bigger machine.

Long syscall bars. The file descriptor or socket is the bottleneck. Check the dependency before tuning GOMAXPROCS.

Frequent GC assist. Allocation rate is high enough that goroutines are drafted into collection work. A heap profile usually explains it in a minute.

What the trace will not tell you

It will not tell you why a lock is contended, and it will not show time spent in code that never yields. Pair it with a mutex profile and, when in doubt, a few microseconds of perf.

The trace is a map, not a diagnosis. It tells you where to look, which is most of the work.