Diagnose Linux CPU and I/O Bottlenecks with perf tools

Overview: When to use perf for Linux bottlenecks

This guide is for Linux system administrators and SREs who need a pragmatic playbook to diagnose CPU and disk I/O bottlenecks on production servers. It focuses on using perf alongside traditional tools such as iostat and pidstat, and on escalation to bpftrace and flamegraphs when deeper insight is required.

You will get actionable collection commands, interpretation tips, and concrete mitigation steps to apply to real systems. The examples are oriented to modern kernels and common server workloads, and assume root or equivalent privileges for tracing tools.

Quick diagnostic checklist and tools

Start with low impact, high signal tools to triage the problem, then escalate as needed. Common first responders are: top or htop for CPU usage, iostat for device throughput and latency, pidstat for process level I/O and CPU accounting, and perf for sampling stacks and hotspots.

Use this checklist to prioritize work, it helps avoid time wasted on noisy traces or irrelevant metrics:

  • Confirm overall CPU saturation with top or mpstat.
  • Check per device I/O and await times with iostat -x 1 5.
  • Identify offending PIDs with pidstat -d -u 1.
  • Use perf to sample stacks only after you know which process or CPU is hot.

Collecting system metrics: top, iostat, pidstat

Run top or htop to see load, per CPU usage, and runaway processes. top reveals runnable queue size and gives quick visibility into context switches and processes in D state, which often point to I/O waits.

See also  Speed Up Linux Boot: Diagnose and Fix systemd Delays

Use iostat -x 1 10 to capture device utilization, await and svctm. If %util is near 100 and await is high, the device is a bottleneck. pidstat -d -u 1 will attribute I/O and CPU to processes, useful to pinpoint the application responsible for heavy IO or CPU consumption.

Using perf top and perf record

perf top gives live sampling of kernel and user space hotspots, which is useful for short investigations. Run perf top -g to see calling context. For reproducible traces use perf record then perf report. Example: perf record -F 99 -g — sleep 60, followed by perf report to see sampled call stacks.

When sampling production workloads, keep frequency reasonable to limit overhead, for example -F 49 or 99. Focus on the process or CPU range, using perf record -p or -C to isolate the source of contention.

Flamegraphs and bpftrace: when to escalate

If perf report shows many samples but unclear stack aggregation, generate a flamegraph. Use perf script | stackcollapse-perf.pl | flamegraph.pl to produce an SVG, which makes hotspots and deep stacks immediately obvious, especially in multithreaded applications.

Use bpftrace when you need dynamic tracing of syscalls, block events, or tail latency at the kernel boundary. Simple bpftrace one liners such as sudo bpftrace -e ‘tracepoint:block:block_rq_issue { @[comm] = count(); }’ help reveal which processes issue the most block requests without heavy overhead.

Linux perf bottlenecks

Interpreting CPU hotspots and syscall waits

Differentiate between user CPU, kernel CPU, and time spent blocked in syscalls. perf annotate and perf report show which functions consume cycles. If kernel functions appear frequently, investigate spinlocks, scheduling, or heavy network stack usage.

See also  Build an Immutable Linux Root with dm-verity and systemd

If stacks show repeated futex or io_wait patterns, the issue may be lock contention or synchronous I/O blocking threads. Use pidstat -w and perf to correlate context switch counts with hotspots, and examine application code paths identified in the flamegraph for expensive locks.

Investigating disk I/O bottlenecks

When iostat reveals high await and high utilization, drill down with iostat -p and blktrace if supported. blktrace provides detailed per-request traces that can reveal request sizes, merges, and scheduling latencies at the block layer.

Compare application patterns: many small synchronous writes cause high IOPS with poor latency, while large sequential transfers stress throughput. Use perf and bpftrace to see which process issues block requests, and whether requests originate from kernel writeback, direct I/O, or user threads calling fsync.

Remediation steps and mitigation actions

Once you identify the root cause, apply targeted mitigations. If CPU hot spots are in application code, optimize algorithms or add worker threads. If kernel spin or syscall costs are high, consider kernel tuning, updating drivers, or isolating interrupts to specific CPUs.

For disk issues, options include tuning IO schedulers, enabling writeback batching, switching to faster devices, adjusting filesystem mount options, or changing application I/O patterns. Use this short checklist to act quickly:

  • Throttle or batch synchronous writes, introduce buffering.
  • Move heavy I/O to dedicated disks or nodes, use tiered storage.
  • Adjust kernel vm and block settings such as vm.dirty_ratio or elevator choice, test changes progressively.
  • Profile and optimize application locks or hot code paths shown in flamegraphs.

FAQ

Q: Can I run perf in production without crashing the server? A: Yes, but limit sampling frequency, scope by PID or CPU, and test overhead in staging first. Use perf top for low overhead quick checks and perf record with short durations for deeper analysis.

See also  Linux kpatch Live Kernel Patching: Deploy and Automate

Q: How do I correlate perf output with iostat or pidstat data? A: Align timestamps, collect short windows with each tool simultaneously, and focus perf on the PID reported by pidstat. Correlate high CPU stack samples with high I/O activity in iostat to see whether CPU waits or active processing dominate.

Q: When should I use bpftrace instead of perf? A: Use bpftrace for targeted kernel events and dynamic probes such as tracepoints, kprobes or syscall tracing with low overhead. Use perf for aggregated CPU sampling and user space stack traces, then escalate to bpftrace for specific kernel or syscall questions.

Q: How do I distinguish kernel stalls from user space CPU usage? A: In perf report look at kernel symbols versus user symbols. High percentages in functions like schedule, softirq, or block layer point to kernel level activity. User symbols indicate application CPU time. Flamegraphs colored by kernel or user help make this distinction obvious.

Conclusion

Diagnosing Linux CPU and disk I/O bottlenecks requires a structured approach: triage with low overhead tools, attribute load to processes, sample stacks with perf, and escalate to flamegraphs and bpftrace for hard to find issues. This playbook emphasizes correlation, not isolation in time, so collect metrics and traces that overlap in the same incident window to avoid chasing ghosts.

Apply remediation steps that match the root cause, for example tune kernel parameters for block layer pressure, move heavy I/O off the hot device, or refactor hot application code revealed by flamegraphs. Always test changes incrementally, measure the effect, and revert if a tuning harms other workloads. With the methods and commands above, sysadmins can move from symptoms to informed fixes quickly, reducing mean time to resolution and improving predictability of production Linux services.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top