perf分析热点代码
perf是linux的一款性能分析工具
perf list ;;列出平台中perf支持的事件命令
sudo perf timechart record python conv2d.py ;;各种统计信息图形化
sudo perf timechart ;;再执行,就会产生csv图形文件
perf record -e cpu-clock ./test ;;结合report命令找出热点函数
perf record -F 50000 -e cpu-clock ./test ;;指定频率
perf stat -r 10 ./test ;;执行10次求均值
perf record -g -e cpu-clock ./test ;;调用关系显示
perf report ;;可多次回车选择不同的项观看,如图,第一个选项是对应热点的汇编代码
perf stat ./test ;;显示程序的cpu、预取、cache miss等
perf record -e cpu-clock python cnn.py ;;使用python执行卷积神经网络cnn.py
perf report ;;显示出来,回车再回车
作图显示热点图
请确保系统安装python、gprof2dot、graphviz软件包
Ubuntu:
$sudo apt-get install python graphviz
$sudo pip install gprof2dot
centos/redhat/fedora:
$yum install python graphviz gprof2dot
以程序test.c为例进行演示
1.创建C文件 test.c
void longa()
{
int i,j;
for(i = 0; i < 1000000; i++)//循环1百万次
j=i;
}
void foo2()
{
int i;
for(i=0 ; i < 10; i++)
longa();
}
void foo1()
{
int i;
for(i = 0; i< 100; i++)
longa();
}
int main(void)
{
foo1();
foo2();
}
2. 终端执行命令, -pg参数产生供gprof剖析用的可执行文件(会在每个function call 插入一个_mcount的routine 功能) -g参数产生供gdb调试用的可执行文件。更多GCC参数可见这篇博客http://blog.chinaunix.net/uid-21411227-id-1826747.html
$ gcc -pg -g -o test test.c
3.接着执行 ./test ,会在当前目录下默认生成 gmon.out
$./test
4.执行以下命令,生成图片output.png
$ gprof ./test | gprof2dot -n0 -e0 | dot -Tpng -o output.png
查看图片,效果图如下:可以看到函数调用的百分比和次数,以及热点线路
对于Python程序,若想要构图可以使用以下方法,需要安装graphviz,根据情况修改gprof2dot.py路径,以Ubuntu为列:
$sudo apt-get install graphviz
$python -m cProfile -o output.pstats simple_conv2d.py
$python /usr/local/lib/python2.7/dist-packages/gprof2dot.py -f pstats output.pstats | dot -Tpng -o profiling_results.png
参考文献
http://www.tuicool.com/articles/UrQvqa
http://www.51testing.com/html/97/13997-79952.html