如何使用 perf 收集一些可读的堆栈跟踪?

How to collect some readable stack traces with perf?

我想使用 this answer 中描述的随机抽样在 Linux 上分析 C++ 程序:

However, if you're in a hurry and you can manually interrupt your program under the debugger while it's being subjectively slow, there's a simple way to find performance problems.

问题是我无法使用 gdb 调试器,因为我想在重负载下对生产进行分析,而调试器过于侵入并大大减慢了程序速度。但是我可以使用 perf recordperf report 来查找瓶颈而不影响程序性能。有没有一种方法可以使用 perf 而不是 gdb 来收集一些可读的(类似 gdb 的)堆栈跟踪?

perf 确实提供了三种不同技术的调用堆栈记录

  • 默认情况下使用帧指针 (fp)。这通常得到支持并且表现良好,但不适用于某些优化。使用 -fno-omit-frame-pointer 等编译您的应用程序以确保其正常运行。
  • dwarf 为 post-processing 的每个样本使用袋子的转储。这会显着降低性能
  • 现代系统可以使用hardware-supported最后一个分支记录,lbr

堆栈可在 perf 分析工具中访问,例如 perf reportperf script

有关详细信息,请查看 man perf-record