perf record can drop samples under high CPU load, leading to incomplete performance profiles.
Common Causes and Fixes
-
Sample Rate Too High:
- Diagnosis: Check the sample rate. If it’s very high (e.g.,
perf record -e cycles:Pwhich defaults to a high frequency), it can overwhelm the kernel’s sampling mechanism. - Fix: Reduce the sample frequency. For example,
perf record -e cycles:50000samples every 50,000 CPU cycles. This reduces the overhead and the likelihood of dropped samples. - Why it works: Lowering the sampling frequency means
perfinterrupts the CPU less often, reducing the chance that an interrupt handler (whichperfuses) will be preempted or that the system will be too busy to record the sample.
- Diagnosis: Check the sample rate. If it’s very high (e.g.,
-
Insufficient Buffer Size:
- Diagnosis: The default buffer size for
perfevents might be too small for the amount of data being generated. Checkperf’s buffer settings. - Fix: Increase the buffer size. Use
perf record -c 1000000to set the buffer size to 1,000,000 events. This givesperfmore room to store samples before they need to be written to disk. - Why it works: A larger buffer allows
perfto accumulate more samples in memory before needing to flush them, reducing the chance of overflow if the system experiences short bursts of high activity.
- Diagnosis: The default buffer size for
-
Kernel Module Interference:
- Diagnosis: Certain kernel modules, especially those that heavily instrument the kernel or interact with tracing mechanisms, can conflict with
perf. Look for messages indmesgrelated to tracing orperf. - Fix: Temporarily unload suspect kernel modules. For example, if
tracepointsare causing issues, tryrmmod tracepoints(if applicable and safe). Testperf recordagain. - Why it works: Some kernel tracing mechanisms can consume significant kernel resources or lock contention, interfering with
perf’s ability to reliably capture samples.
- Diagnosis: Certain kernel modules, especially those that heavily instrument the kernel or interact with tracing mechanisms, can conflict with
-
System-Wide vs. Per-Thread/Process Sampling:
- Diagnosis: If you’re trying to profile a specific process but
perf recordis configured for system-wide sampling without filtering, it might be swamped by other processes. - Fix: Target your profiling. Use
perf record -p <PID> -e cycles:Pto profile a specific process ID (<PID>). This focusesperf’s efforts on the target. - Why it works: By specifying a process ID,
perfonly collects samples related to that process’s execution, drastically reducing the volume of data and the system load it imposes.
- Diagnosis: If you’re trying to profile a specific process but
-
CPU Frequency Scaling:
- Diagnosis: Aggressive CPU frequency scaling (e.g., the
ondemandorpowersavegovernors) can lead to unpredictable sampling intervals if the CPU speed changes rapidly during sampling. - Fix: Set the CPU governor to
performance. You can do this temporarily for all CPUs withecho performance | sudo tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor. This keeps CPUs running at their maximum frequency. - Why it works: A constant CPU frequency means the timing of
cyclesevents is more predictable, leading to more consistent sampling and fewer dropped samples.
- Diagnosis: Aggressive CPU frequency scaling (e.g., the
-
I/O Bottlenecks:
- Diagnosis: If the disk where
perf.datais being written is slow or heavily utilized, it can become a bottleneck, causingperfto drop samples because it can’t write them out fast enough. - Fix: Write
perf.datato a faster storage device (e.g., an NVMe SSD instead of an HDD) or to a different disk that is less busy. You can specify the output file withperf record -o /path/to/faster/disk/perf.data .... - Why it works: By offloading the write operations to a faster or less contended I/O subsystem,
perfcan flush its internal buffers more quickly, preventing them from overflowing.
- Diagnosis: If the disk where
-
Insufficient Permissions/Security Modules:
- Diagnosis: Security modules like SELinux or AppArmor might restrict
perf’s access to certain kernel features or memory regions necessary for accurate sampling, especially for kernel-level events. Check/var/log/audit/audit.logordmesgfor denials. - Fix: Adjust SELinux/AppArmor policies to allow
perfnecessary permissions. For SELinux, this might involve setting specific booleans or contexts. For example,sudo setsebool -P allow_perf_trace_events on(though this depends on your specific SELinux configuration and kernel version). - Why it works:
perfrelies on specific kernel interfaces and memory access for sampling. Security modules can block these, leading to incomplete data or errors. Granting the necessary permissions allowsperfto function correctly.
- Diagnosis: Security modules like SELinux or AppArmor might restrict
The next error you’ll likely encounter after fixing dropped samples is related to insufficient symbols for accurate function-level analysis, requiring perf to have access to debug information.
perf is designed to give you a window into your system’s execution, but it’s not a passive observer; it actively influences the very behavior it’s trying to measure.
Let’s look at how perf record captures stack traces, and why it’s more than just a simple function call list. When perf samples, it doesn’t just record the instruction pointer at the moment of interruption. For stack traces, it walks the call stack at that instant. This involves traversing the stack frame pointers in memory to reconstruct the sequence of function calls that led to the current execution point.
Consider a scenario where you’re profiling a web server under load. You run perf record -g -p <PID> -- sleep 10. The -g flag tells perf to record call graphs (stack traces).
# Example output snippet from perf script after recording
# (This is a simulated representation, actual output is more verbose)
# Event: cycles:P