gprof2dot分析代码

原文地址:http://blog.****.net/banana1006034246/article/details/68925078?ticket=ST-6307-y2fWRKCU96SbwodJ0SRi-passport.****.net

perf分析热点代码

perf是linux的一款性能分析工具

perf list            ;;列出平台中perf支持的事件命令

perf top -e cycles:pp        ;;采样数据,其中有四个级别  0--无精度保证  p--采样指令和触发性能事件之间的偏差为常数 pp--偏差尽量趋于零  ppp--偏差必须为零

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            ;;可多次回车选择不同的项观看,如图,第一个选项是对应热点的汇编代码
gprof2dot分析代码

perf stat ./test   ;;显示程序的cpu、预取、cache miss等

对于perf 后面接的文件,不必是二进制可执行文件,比如

perf record -e cpu-clock python cnn.py         ;;使用python执行卷积神经网络cnn.py

perf report    ;;显示出来,回车再回车

gprof2dot分析代码

gprof2dot分析代码

  • 作图显示热点图

请确保系统安装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

  1. <span style="font-size:14px;">void longa()   
  2.  {   
  3.    int i,j;   
  4.    for(i = 0; i < 1000000; i++)//循环1百万次  
  5.       j=i;    
  6.  }   
  7.  void foo2()   
  8.  {   
  9.    int i;   
  10.    for(i=0 ; i < 10; i++)   
  11.         longa();   
  12.  }   
  13.  void foo1()   
  14.  {   
  15.    int i;   
  16.    for(i = 0; i< 100; i++)   
  17.       longa();   
  18.  }   
  19.  int main(void)   
  20.  {   
  21.    foo1();   
  22.    foo2();   
  23.  }</span>  


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

查看图片,效果图如下:可以看到函数调用的百分比和次数,以及热点线路

gprof2dot分析代码

  • 参考文献

http://www.tuicool.com/articles/UrQvqa

http://www.51testing.com/html/97/13997-79952.html