Skip to content

Lesson 13 of 13 · 15m

Profiling in Production Without Breaking It

Sampling at 99 Hz and continuous profilers cost single-digit percent; tracing costs per event — pick the mechanism, not the fear.

The fear of profiling production comes from the tools that deserve it: strace -f on a hot process, full syscall tracing, a JVM agent at maximum detail. Sampling profilers are a different category. At 99 Hz, perf wakes ninety-nine times a second per CPU, walks a stack, writes a record — low single-digit percent of one core. Google's warehouse-fleet profiling (the 2010 GWP paper) ran continuously across entire datacenters at a fraction of a percent. The observer effect is real. It is also budgeted.

The overhead taxonomy

  • Sampling (perf, async-profiler, Go's CPU profile): roughly 1–5%, safe continuously.
  • In-kernel aggregation (bcc and bpftrace counters, histograms): ~1–2%, safe continuously, emits summaries.
  • Instrumentation (compile-time counters, metrics libraries): fixed per-call cost — fine for counters, deadly for per-request payloads.
  • Tracing (strace, perf trace, per-event printing): cost proportional to event rate — safe only on cold paths or short windows.

The JVM safepoint trap

Java earns its own note because the default mechanism is wrong. JVMTI GetStackTrace only unwinds at safepoints; hot code that does not reach one — tight loops, long native calls — gets its samples attributed to the next safepoint instead of where the CPU actually was. That is safepoint bias, and it systematically misreports hot loops. async-profiler (Andrei Pangin) uses AsyncGetCallTrace, which unwinds without requiring a safepoint, plus perf_events for the native side — which is why it is the standard answer for JVM profiling.

Always-on infrastructure

Continuous profilers collect fleet-wide at low rate: Grafana Pyroscope, Parca, Google Cloud Profiler, Datadog's profiler. The value is not one capture — it is diffing this week against last week at the same traffic and catching the regression nobody timed. Go makes the endpoint trivial: import _ "net/http/pprof" registers /debug/pprof/ on the default mux. Do not expose that mux publicly — it serves goroutine dumps and arbitrary CPU profiles. Bind it to a localhost listener or authenticate it.

The incident-time discipline

Capture before you restart. A wedged process killed under pressure loses every thread state that explained it — eu-stack -p costs seconds, gcore costs a pause, perf record costs a percent of one core. All three are cheaper than "we will never know." Then restart; the evidence survives the box.

A working incident loop: top and vmstat for the resource, perf stat -p for density, perf record -F 99 -g for stacks, offcputime when it is blocked, strace -c for five seconds when syscalls are suspect. Each tool answers one question at a known cost. For scheduler-level questions — run-queue delay, migrations — perf sched record plus perf sched map and timehist show who waited for whom.

Symbols and releases

The other half of profiling prod is being able to: keep per-release symbol files or a debuginfod server, verify frame pointers survived the build, and record which binary produced which perf.data — the build-id in perf's header pairs them. A profile of a stripped binary is a pile of addresses. Build-id lives in the ELF .note.gnu.build-id section; perf buildid-list -i perf.data lists what a capture actually referenced, which is how you confirm the symbol files you kept are the ones the profile needs.

The practice

  • Keep symbols shipped and kernel.perf_event_paranoid sane on prod hosts so profiling works when the incident is live — not the morning after.
  • During the spike: perf record -F 99 -p <pid> -- sleep 30. A 30-second capture at 99 Hz changes nothing but your certainty.
  • Run a continuous profiler for the fleet baseline; use on-demand captures for the incident.
  • /debug/pprof on a private listener only — it is a debugging API with no auth.
  • For Java, async-profiler specifically. Safepoint-biased stacks are worse than no stacks.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.