13

使用valgrind的callgrind工具进行多线程性能分析

 2 years ago
source link: https://blogread.cn/it/article/7168?f=hot1
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

   valgrind是开源的性能分析利器。 根据它的文档,可以用它来检查内存泄漏等问题,还可以用来生成函数的调用图,就这两个功能就足够有吸引力了。

   本文主要是介绍如何使用valgrind的callgrind工具进行性能分析。

使用callgrind工具生成性能分析数据

   命令格式如下:

valgrind --tool=callgrind ./exproxy

   其中 ./exproxy就是我们要分析的程序。执行完毕后,就会在当前目录下生成一个文件。文件名为“callgrind.out.进程号”。如,callgrind.out.31113。注意,对于daemon进程的调试,不要通过kill -9方式停止。

   如果你调试的程序是多线程,你也可以在命令行中加一个参数 -separate-threads=yes。这样就会为每个线程单独生成一个性能分析文件。如下:

valgrind --tool=callgrind --separate-threads=yes ./exproxy

   生成的文件除了callgrind.out.31113外,还会多出一些子线程的文件。文件名如下:

   callgrind.out.31113-01    callgrind.out.31113-02    callgrind.out.31113-03

把callgrind生成的性能数据转换成dot格式数据

   可以使用gprof2dot.py脚本,把callgrind生成的性能分析数据转换成dot格式的数据。方便使用dot把分析数据图形化。

   脚本可以点此下载。脚本使用方式如下:

python gprof2dot.py -f callgrind -n10 -s callgrind.out.31113 > valgrind.dot

使用dot把数据生成图片

   命令格式如下:

dot -Tpng valgrind.dot -o valgrind.png

生成的图片示例

   通过图形,我们可以很直观的知道那段程序执行慢,并且了解相关调用关系。

建议继续学习:

  1. Linux C/C++ 内存泄漏检测工具:Valgrind    (阅读:2082)
QQ技术交流群:445447336,欢迎加入!
扫一扫订阅我的微信号:IT技术博客大学习

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK