Read scheduler trace and runtime execution traces
Read scheduler trace and runtime execution traces
Use GODEBUG=schedtrace=1000 to print a scheduler snapshot to stderr about every 1000 ms, and scheddetail=1 for more detail. These implementation-specific snapshots include runnable queue information but are not an event history. runtime/trace records scheduling transitions, blocking, syscalls and other events over a bounded interval. Decode a trace with the matching go tool trace; inspect runnable delay separately from waiting on an I/O or synchronization event.
package main
import ( "bytes" "fmt" "runtime/trace")
func main() { var b bytes.Buffer if err := trace.Start(&b); err != nil { panic(err) } c := make(chan int) go func() { c <- 42 }() v := <-c trace.Stop() fmt.Println(v, b.Len() > 0)}Stop flushes trace data before reading the buffer. This checks that data was emitted, not that any particular scheduling event occurred. For a small offline lab save b.Bytes() to a private trace.out file after Stop (check errors), then use go tool trace trace.out or go tool trace -d=1 trace.out for a textual event view on Go 1.23. Traces can contain operational metadata and incur overhead; keep collection bounded and access restricted.
Expected output:
42 trueInspect the runnable latency histogram
package main
import ( "fmt" "runtime/metrics")
func main() { s := []metrics.Sample{{Name: "/sched/latencies:seconds"}} metrics.Read(s) h := s[0].Value.Float64Histogram() fmt.Println(len(h.Buckets) == len(h.Counts)+1)}Bucket counts are cumulative across observations; compare successive snapshots to estimate a window, and avoid treating counts as durations. The metric describes time spent runnable before execution, not total request latency. Correlate with CPU limits, queue buildup and trace evidence before changing settings.
Expected output:
trueTry it locally
Capture a trace in memory with a named region called compute that sums 1 through 4. Stop the trace and print the sum and whether the buffer is nonempty.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Runtime scheduler 07: Read scheduler trace and runtime execution traces
Check the lesson objective before moving on.
Which signal measures runnable time before execution?
Why call trace.Stop before decoding?