perf Suite
The perf tool suite is a powerful set of command-line utilities for profiling Linux systems, enabling detailed analysis of CPU usage, function call stacks, and kernel behavior. It leverages the Linux kernel's Performance Events subsystem to collect data from hardware and software events, making it indispensable for diagnosing performance bottlenecks and optimizing system behavior. This section covers how to use perf to profile CPU usage, track function calls, and analyze kernel stack traces.
Profiling CPU Usage¶
To profile CPU usage, perf captures samples of CPU activity over time, revealing which processes or functions consume the most resources.
Example: Recording CPU usage
-g: Enables call graph recording (for function call tracking).-
sleep 10: Runs a dummy workload for 10 seconds.
Analyzing results
- Use
--sort comm,dso to sort by process name or shared object (library).
Statistical overview
Tracking Function Calls¶
The -g option enables call graph recording, allowing you to trace how functions invoke each other.
Example: Recording function call stacks
--call-graph fp: Uses frame pointers for call stack recording (requires kernel support).
Analyzing call graphs
- Use
--sort comm,dt to sort by process and duration.
Filtering by process
<PID> with a specific process ID to focus on its behavior.
Analyzing Kernel Stack Traces¶
To debug kernel-level performance issues, perf can capture stack traces of kernel functions.
Example: Recording kernel events
-k: Specifies the kernel debug symbol file (ensure it matches your kernel version).
Analyzing kernel stack traces
- Look for patterns in kernel functions like
do_syscall_64 or schedule.
Troubleshooting tips
- If no kernel symbols are found, rebuild your kernel with CONFIG_DEBUG_INFO enabled.
- Use perf list to check available events and ensure perf is properly installed.
Key takeaways¶
- Use
perf record -gto capture detailed CPU and function call data. - Analyze results with
perf reportto identify performance bottlenecks. - Kernel stack traces require debug symbols and proper configuration.
- Combine
perf statfor statistical insights andperf recordfor granular tracing. - Always verify kernel version and debug symbol compatibility for kernel analysis.