/performant-golang:go-perf-godebug-and-metricsfrom performant-golang
Guides Go runtime observability — the GODEBUG trace knobs (gctrace, schedtrace/scheddetail, inittrace, allocfreetrace, madvdontneed) and reading a gctrace line, the low-overhead runtime/metrics package and its histogram metrics, preferred over runtime.ReadMemStats which stops the world, and exposing metrics via expvar/Prometheus. Fires on "GODEBUG", "gctrace", "read runtime metrics", "monitor GC in production", "ReadMemStats vs runtime/metrics". Routes GC to go-perf-gc-tuning, scheduler to go-perf-goroutines-scheduler.
Ships in performant-golang. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
This skill owns runtime observability for performance: the GODEBUG knobs that make the runtime narrate what it is doing, and the runtime/metrics package that lets a process read its own runtime stats cheaply. It is a reference skill — it tells you what to look at; the action you take routes to a sibling (go-perf-gc-tuning for GC knobs, go-perf-goroutines-scheduler for scheduler depth, go-perf-tail-latency for the histograms, go-perf-execution-tracer for the tracer, go-perf-os-tooling for OS profilers).
All Go below builds clean under gofmt, go vet, and go test on Go 1.25 / 1.26.
GODEBUG is a comma-separated list of key=value settings that "controls the execution of certain parts of a Go program" (godebug). It carries two unrelated kinds of setting, and conflating them is a common error:
go line in go.mod, a godebug directive, or a //go:debug line — e.g. panicnil=1, http2client=0, and the perf-relevant containermaxprocs ({Name: "containermaxprocs", Package: "runtime", Changed: 25, Old: "0"} — toggles the Go 1.25 container-aware GOMAXPROCS) (godebugs/table.go). Each compatibility setting also exports a counter /godebug/non-default-behavior/<name>:events so you can see in production whether old behavior is firing (godebug; metrics/description.go).GODEBUG=gctrace=1 ./server). "Unrecognized settings in the GODEBUG environment variable are ignored" (godebug) — so a typo silently does nothing.Each knob below is documented in runtime's package doc (runtime: Environment Variables, mirrored in src/runtime/extern.go). They emit lines to standard error; the formats are "subject to change."
GODEBUG= | What it emits | Use it to see |
|---|---|---|
gctrace=1 | one line per GC (§3) | GC frequency, pause, heap sizes, GC CPU% |
schedtrace=N | one line every N ms summarizing scheduler state | run-queue depth, idle Ps/Ms |
schedtrace=N,scheddetail=1 | detailed multiline per-P/M/G state every N ms | which goroutines are runnable/blocked |
inittrace=1 | one line per package with init work (§3) | slow / allocation-heavy package init |
scavtrace=1 | one line ~per GC cycle from the scavenger | memory returned to the OS, RSS behavior |
madvdontneed=0 | (behavior toggle) use MADV_FREE not MADV_DONTNEED on Linux | why RSS stays high until memory pressure |
Two cautions:
schedtrace/scheddetail and gctrace are diagnostics, not always-on. scheddetail=1 is verbose; combining several traces interleaves their lines on the same stderr and makes both unreadable. Set one at a time.allocfreetrace is gone. It historically emitted a stack trace on every allocation and free — astronomically heavy and only ever a last resort. It has been removed from the modern runtime (no longer present in src/runtime/extern.go or src/runtime/*.go as of the Go 1.26 tree). For per-allocation insight today, use a heap profile (go-perf-pprof-profiling) or the execution tracer (go-perf-execution-tracer), not a GODEBUG knob.gctrace Line Field-by-Fieldgctrace=1 "causes the garbage collector to emit a single line to standard error at each collection, summarizing the amount of memory collected and the length of the pause" (runtime). The format (verbatim, "subject to change"):
gc # @#s #%: #+#+# ms clock, #+#/#/#+# ms cpu, #->#-># MB, # MB goal, # MB stacks, #MB globals, # P
Field by field, with the runtime/metrics metric that supersedes it (the doc lists these mappings inline):
| Field | Meaning | Modern metric |
|---|---|---|
gc # | the GC number, incremented each GC | — |
@#s | seconds since program start | — |
#% | percentage of time spent in GC since program start | /cpu/classes/gc/total:cpu-seconds |
#+#+# ms clock | wall-clock time for the GC phases | — |
#+#/#/#+# ms cpu | CPU time across the phases | /cpu/classes/gc/* |
#->#-># MB | heap size at GC start, at GC end, and live heap | /gc/scan/heap:bytes |
# MB goal | goal heap size | /gc/heap/goal:bytes |
# MB stacks | estimated scannable stack size | /gc/scan/stack:bytes |
#MB globals | scannable global size | /gc/scan/globals:bytes |
# P | number of processors used | /sched/gomaxprocs:threads |
"The phases are stop-the-world (STW) sweep termination, concurrent mark and scan, and STW mark termination. The CPU times for mark/scan are broken down in to assist time (GC performed in line with allocation), background GC time, and idle GC time. If the line ends with (forced), this GC was forced by a runtime.GC() call" (runtime). The #% GC-CPU field and the #->#-># live-heap field are the two to read first: a rising #% means GC is eating throughput, and a live heap that climbs run-to-run means a leak or a GOMEMLIMIT worth setting (act → go-perf-gc-tuning).
inittrace=1 is read the same way — init # @#ms, # ms clock, # bytes, # allocs per package, surfacing a package whose init is slow or allocates heavily (runtime).
runtime/metrics — The Modern, Low-Overhead WayFor anything programmatic (dashboards, alerts, in-process budgets), read runtime/metrics, not runtime.ReadMemStats. The package "provides a stable interface to access implementation-defined metrics exported by the Go runtime … similar to existing functions like runtime.ReadMemStats … but significantly more general" (runtime/metrics). Metrics are keyed by string names with an embedded unit (/path:unit), discovered at runtime via metrics.All(), so the set grows across Go versions without breaking your code; the Kind of a given metric is guaranteed stable.
package main
import (
"fmt"
"runtime/metrics"
)
// sampleGC reads three runtime metrics in one call. Reuse the []metrics.Sample
// slice across calls; metrics.Read fills in each Value in place.
func sampleGC() {
samples := []metrics.Sample{
{Name: "/gc/heap/allocs:bytes"}, // cumulative bytes allocated
{Name: "/sched/goroutines:goroutines"}, // live goroutines
{Name: "/sched/latencies:seconds"}, // runnable→running wait (histogram)
}
metrics.Read(samples)
for _, s := range samples {
switch s.Value.Kind() {
case metrics.KindUint64:
fmt.Printf("%s = %d\n", s.Name, s.Value.Uint64())
case metrics.KindFloat64:
fmt.Printf("%s = %f\n", s.Name, s.Value.Float64())
case metrics.KindFloat64Histogram:
h := s.Value.Float64Histogram()
fmt.Printf("%s = histogram with %d buckets\n", s.Name, len(h.Counts))
case metrics.KindBad:
panic("unsupported metric " + s.Name) // name not in metrics.All()
}
}
}
Key API (runtime/metrics):
metrics.Read([]Sample) fills each Sample.Value in place; reuse the slice between calls — the read is cheap and does not stop the world.metrics.All() []Description lists every supported metric (Name, Description, Kind, Cumulative). Iterate it rather than hardcoding names, so you skip a KindBad on an older runtime.KindFloat64Histogram) carry Buckets []float64 and Counts []uint64 — the raw material for a p99 without coordinated omission (methodology → go-perf-tail-latency).metrics/description.go)| Metric | Kind | Reads |
|---|---|---|
/gc/heap/allocs:bytes | uint64, cumulative | total bytes ever allocated (allocation rate = its slope) |
/gc/heap/allocs:objects | uint64, cumulative | total heap objects allocated |
/gc/heap/live:bytes | uint64 | live bytes marked by the previous GC |
/gc/gogc:percent, /gc/gomemlimit:bytes | uint64 | the effective GOGC / GOMEMLIMIT |
/sched/latencies:seconds | histogram | goroutine runnable→running wait (scheduler saturation) |
/sched/goroutines:goroutines | uint64 | live goroutines (leak detection) |
/sync/mutex/wait/total:seconds | float64, cumulative | global lock-contention trend |
/memory/classes/total:bytes | uint64 | all memory mapped read-write by the runtime |
Deprecation to know: /gc/pauses:seconds is now documented as "Deprecated. Prefer the identical /sched/pauses/total/gc:seconds." (metrics/description.go). The newer GC STW pause histograms are /sched/pauses/total/gc:seconds (full pause) and /sched/pauses/stopping/gc:seconds (just the time to stop all Ps); non-GC STW events live under /sched/pauses/*/other:seconds. Read code emitting /gc/pauses:seconds and migrate it.
runtime/metrics Beats runtime.ReadMemStatsruntime.ReadMemStats(m *MemStats) stops the world to take its snapshot — its implementation begins stw := stopTheWorld(stwReadMemStats) (src/runtime/mstats.go). Calling it on a timer in a hot service injects a global pause on every scrape, and it returns one coarse, fixed MemStats struct that cannot express histograms or grow with the runtime. runtime/metrics is the general, forward-compatible, non-STW replacement (§4). Prefer it for all new code; reach for ReadMemStats only when interfacing with old code that already depends on a specific MemStats field.
A one-shot metrics.Read answers "now"; production wants a time series.
expvar is the zero-dependency option. It "provides a standardized interface to public variables … exposed via HTTP at /debug/vars in JSON format," and importing it for its side effect registers a handler plus cmdline and memstats automatically (expvar). Publish your own with expvar.NewInt("requests") or expvar.Publish(name, v) (v is any Var whose String() returns valid JSON). Bridge runtime/metrics into it with an expvar.Func:import (
"expvar"
"runtime/metrics"
)
func init() {
const goroutines = "/sched/goroutines:goroutines"
expvar.Publish("go_goroutines", expvar.Func(func() any {
s := []metrics.Sample{{Name: goroutines}}
metrics.Read(s)
return s[0].Value.Uint64()
}))
}
collectors.NewGoCollector already sources from runtime/metrics, so you get /gc/* and /sched/* on a scrape endpoint with no custom wiring. (Use it over hand-rolled gauges; correctness of the client API is out of scope here — this skill's claim is only which source to pull from: runtime/metrics, not ReadMemStats.)Note expvar's default /debug/vars handler — like net/http/pprof — should not be on a public port; gate it to an internal interface.
This plugin (go-perf-*):
go-perf-gc-tuning — acting on a bad gctrace line or /gc/* metric: GOGC, GOMEMLIMIT, the pacer, allocation rate as the primary lever.go-perf-goroutines-scheduler — interpreting schedtrace//sched/* depth: GMP model, work-stealing, container-aware GOMAXPROCS.go-perf-tail-latency — turning /sched/latencies:seconds and /sched/pauses/total/gc:seconds histograms into p99/p999 without coordinated omission.go-perf-execution-tracer — when you need a timeline, not a counter: runtime/trace, go tool trace, the flight recorder.go-perf-os-tooling — OS-level profilers (perf, flame graphs) when the cost is below the runtime.go-perf-methodology — the decision layer: which diagnostic answers which question.Parent / sibling marketplace (go-*):
go-slog-logging — structured logging, distinct from metric counters.go-version-feature-map — version floors: container-aware GOMAXPROCS (1.25), Green Tea GC default (1.26).runtime.ReadMemStats on a timer — it stops the world every call (mstats.go); use runtime/metrics (§5)./gc/pauses:seconds — it is deprecated in favor of the identical /sched/pauses/total/gc:seconds (description.go).allocfreetrace — it was removed; use a heap profile or the tracer (§2).scheddetail=1 (or several traces) running together — verbose, interleaved stderr that drowns the signal (§2)./godebug/non-default-behavior/* counter; the second is environment-only stderr instrumentation (§1)./debug/vars (or pprof) on a public port — gate it internally (§6).runtime/metrics and then guess at the fix here — act in go-perf-gc-tuning / go-perf-goroutines-scheduler; this skill only tells you what to look at.Wrong/right anti-patterns in LLM-generated runtime-observability code, with citations:
Source provenance for every claim in this skill:
What SKILL.md sets; muted values are Claude Code's defaults
Other plugins' skills for the same job
npx claudepluginhub ctoth/golang-skills-plugin --plugin performant-golang34 skills in this plugin
/go-perf-allocator-internalsWhat a Go heap allocation costs — the ~70 size classes and rounding waste (unsafe.Sizeof vs real slot), the tiny allocator packing sub-16B pointer-free objects, the lock-free per-P mcache → mcentral → mheap hierarchy, and scannable vs noscan spans (pointer-free objects cut GC cost; "1 alloc/op" hides different costs). Fires on "still slow after removing allocs", "reduce GC pressure", "size class", "how does Go allocate". Routes to go-perf-escape-analysis, go-perf-sync-pool, go-perf-gc-tuning, go-perf-struct-layout./go-perf-atomics-vs-locksGuides the atomics-vs-locks performance tradeoff in Go — typed sync/atomic ops (atomic.Int64/Pointer/Bool, 1.19) are a single CAS-style instruction, cheaper than a mutex uncontended but still cache-line-bouncing when contended; sync.Mutex's fast path is itself a CAS that only parks the goroutine when contended; RWMutex wins only for long read sections and is slower (scales worse) than a plain Mutex for short ones; the atomic.Pointer copy-on-write config swap; when lock-free isn't worth the correctness risk. Fires on "atomic vs mutex", "is RWMutex faster", "lock-free counter", "reduce locking overhead", "atomic.Pointer config". Routes copylock/atomic-API correctness to go-sync-primitives, memory ordering to go-race-and-memory-model, measuring to go-perf-block-mutex-profiles./go-perf-audience-and-tradeoffsGuides framing a Go performance decision by audience — the same change is right for one and wrong for another: the library author who can't profile callers and must not pessimize the common case (offer AppendXxx/[]byte forms, no unsafe in the API), the app developer who can profile and optimizes the proven hot path, the SRE who needs the code diagnosable (pprof labels, metrics, GOMEMLIMIT), the next reader who pays for clever micro-opts; the clarity-vs-speed through-line. Fires on "should a library do this", "is this worth optimizing for my users", "make this diagnosable", "premature optimization vs pessimization", "who am I optimizing for". Routes the policy root to go-idiomatic-discipline, measure-first to go-perf-methodology./go-perf-benchmarking-statisticsGuides trustworthy Go benchmark measurement at the command level — running each side with -count≥10 into old.txt/new.txt and feeding both to benchstat, reading its ± confidence interval, p-value (Mann–Whitney U), geomean, and the "~" no-significant-difference result, choosing the right unit (b.SetBytes for MB/s, b.ReportMetric for custom throughput), grouping a matrix with -col/-row/-filter, building a reproducible environment (perflock's 90% governor + serialization, frequency scaling. Auto-invokes when comparing benchmark runs, running benchstat, writing a perf A/B, or stabilizing a bench machine, and on "is this benchmark result real", "compare these benchmarks", "did this actually get faster", "my benchmark is noisy", "set up a perf regression test". Routes benchmark loop mechanics (for b.Loop, the sink/DCE trap, ResetTimer, RunParallel) to go-testing-advanced and the why-one-run-lies concept to go-perf-methodology./go-perf-block-mutex-profilesGuides Go contention diagnosis at depth — the block, mutex, and goroutine profiles, why block/mutex are OFF by default and read empty until runtime.SetBlockProfileRate (nanoseconds, 1=everything) / runtime.SetMutexProfileFraction (1/n sampled) are set, the overhead tradeoff, reading delay-vs-contention in pprof, and the goroutine profile for leak and pile-up detection. Auto-invokes on "why is this contended", "diagnose lock contention", "goroutine leak", "my mutex is slow", "profile blocking", "goroutines are piling up". Routes the fix to go-perf-contention-and-sharding / go-perf-atomics-vs-locks, sync correctness to go-sync-primitives, CPU/heap to go-perf-pprof-profiling./go-perf-bounds-check-eliminationGuides bounds-check elimination (BCE) in Go — the compiler checks every slice/array index and the SSA pass removes the ones it proves redundant. Covers seeing surviving checks with go build -gcflags="-d=ssa/check_bce/debug=1", prover-friendly idioms (the _ = b[n-1] / b = b[:n] hint, range loops, constant vs variable offsets), the inlining link, and why -B is a footgun not a fix. Fires on "eliminate bounds checks", "why is this loop slow", "bounds check in hot loop", "optimize this numeric loop". Routes should-I to go-perf-methodology, slices to go-perf-slices.