Go

How can you observe the scheduler? (schedtrace, go tool trace, pprof)

Question 172HardGo 1.22 to 1.25
  • GODEBUG=schedtrace=1000 prints a line every second with gomaxprocs, idleprocs, threads, spinningthreads, runqueue (global) and the per-P local queue lengths [..]. Adding scheddetail=1 dumps every G, M and P.
  • Execution tracer (runtime/trace, go test -trace, or /debug/pprof/trace?seconds=5) opened with go tool trace. It shows per-P timelines, when goroutines were created, blocked and unblocked, syscalls, GC, and the scheduler latency profile. Tracing overhead fell to about 1–2% in Go 1.21. The flight recorder (Go 1.25) keeps a rolling buffer you can snapshot.
  • pprof: the goroutine profile shows counts and stacks. The block profile (enable with runtime.SetBlockProfileRate) and the mutex profile (SetMutexProfileFraction) show where goroutines wait.
  • runtime/metrics: /sched/latencies:seconds is a histogram of how long goroutines sit runnable before they run. /sched/goroutines:goroutines is the goroutine count.
f, _ := os.Create("trace.out")
defer f.Close()
trace.Start(f)
defer trace.Stop()

What the interviewer wants to hear: high scheduler latency together with CPU below saturation points to lock contention, GOMAXPROCS throttling or GC assists. The tools let you tell these causes apart.

More on Goroutines & the Scheduler

All 35 Goroutines & the Scheduler questions