Gperftools

导言

相对于perf命令,能低侵入的profile具体某个函数。

  • 提供更多的具体有效信息
  • 可惜暂时不能像viztracer和 tray profiler一样调用栈可视化。

Google Performance Tools (gperftools)

  • 用途:gperftools 是 Google 开源的性能分析工具,包含多种子工具,包括 tcmalloc、cpu profiler、heap profiler 等,专门用于分析 CPU 和内存的使用情况。

  • 优点:

    • 针对多线程应用进行了优化,可以对多线程程序进行有效分析。
    • 提供内存分配、释放等详细的分析信息。
  • 缺点:

    • 与其他工具相比,使用门槛稍高,且需要嵌入到程序代码中进行配置。

使用

代码修改

启动 CPU Profiling:

1
2
3
4
5
6
7
#include <gperftools/profiler.h>

int main() {
ProfilerStart("your_program.prof");
// your code here
ProfilerStop();
}

编译

链接程序到 gperftools 库:

1
g++ your_program.cpp -lprofiler -o your_program

解析

使用 pprof 分析生成的性能数据:

1
pprof --text your_program your_program.prof

参考文献

Author

Shaojie Tan

Posted on

2024-10-18

Updated on

2025-01-30

Licensed under