gprof工具简介
gprof为gnu profiler,用于分析程序性能信息文件,从中得到程序中的函数调用栈及被调函数耗时(也支持统计语句级别的耗时),指出程序中耗时的部分所在,辅助进行程序性能优化
gprof用户手册
Profiling has several steps:
- You must compile and link your program with profiling enabled. See Compiling.
- You must execute your program to generate a profile data file. See Executing.
- You must run
gprofto analyze the profile data. See Invoking.
使用方式概述
-
使用gprof分析程序性能时,编译程序需要增加编译和链接选项(-pg),具体可以见man手册说明
-pg Generate extra code to write profile information suitable for the analysis program gprof. You must use this option when compiling the source files you want data about, and you must also use it when linking. -
运行添加过-pg选项后的程序,得到gmon.out文件
-
使用gprof工具分析gmon.out,得到结果:gprof -b a.out gmon.out > profile.txt #这边a.out为程序名
注意事项:gprof工具要求程序正常退出或者调用exit退出,才能正常生成gmon.out文件
数据图形化
如果想进一步得到图形化文件的话,需要安装gprof2dot工具,以CentOS为例说明安装gprof2dot:
- 安装gprof2dot.py:yum install python3 graphviz && pip install gprof2dot
- gprof2dot.py profile.txt > profile.dot 生成dot文件
- dot -Tsvg -o gprof.svg profile.dot 生成svg文件 我们就直接用浏览器就可以打开svg看那个函数是热点了。
安装和使用说明可参考:gprof2dot
参考资料
gprof 用户手册网站
gprof2dot
Linux性能优化gprof使用
性能测试工具GNU gprof
https://cloud.tencent.com/developer/article/1505626
gprof多线程
gprof工具不支持对多线程程序的性能分析,不过可以通过下面的方法加以解决:
使用gprof进行多线程程序性能分析
或
HOWTO: using gprof with multithreaded applications
484



被折叠的 条评论
为什么被折叠?



