`
splayx
  • 浏览: 82683 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

使用google cpu profiler做性能分析

阅读更多

pprof是分析google cpu profiler输出文件的工具,

但在使用之前需要做些准备工作

 

1、安装graphviz(提供dot工具)

 

2、安装gv(http://www.gnu.org/software/gv/)

下载gv源码后,需要做一下准备

安装Xaw3d库

yum install Xaw3d

libXaw3d*放在:/usr/X11R6/lib

安装头文件

yum install Xaw3d-devel.x86_64

然后就可以./configure然后安装了。

 

google cpu profiler只有gproftools中的一个工具

可以在这里下载源码安装(注意事项见INSTALL)

http://code.google.com/p/gperftools/

 

下来介绍下怎么使用它。

首先设置环境变量CPUPROFILE,指定输出文件

$ CPUPROFILE=/tmp/profile
$ env | grep CPUPROFILE
$ export CPUPROFILE
$ env | grep CPUPROFILE
CPUPROFILE=/tmp/profile

 

随便写个程序

double bfunc() {
    double sum = 0;
    for (int i = 0; i < 100; ++i) {
        sum = sum * i;
    }
    return sum;
}

int afunc() {
    int sum = 0;
    for (int i = 0; i < 100; ++i) {
        sum += i;
    }
    return sum;
}
int main() {
    for (int i = 0; i < 1000000; ++i) afunc();
    for (int i = 0; i < 1000000; ++i) bfunc();
    return 0;
}

编译  

gcc -lstdc++ -g  -finstrument-functions test.cpp -lprofiler

运行./a.out看到

$ ./a.out 
PROFILE: interrupts/evictions/bytes = 25/0/192

ll /tmp/profile 可以看到产生的输出文件

生成dot文件

$ pprof --dot a.out /tmp/profile     
Using local file a.out.
Using local file /tmp/profile.
Removing killpg from all stack traces.
digraph "a.out; 2 samples" {
node [width=0.375,height=0.25];
Legend [shape=box,fontsize=24,shape=plaintext,label="a.out\lTotal samples: 2\lFocusing on: 2\lDropped nodes with <= 0 abs(samples)\lDropped edges with <= 0 samples\l"];
N1 [label="__libc_start_main\n0 (0.0%)\rof 2 (100.0%)\r",shape=box,fontsize=8.0];
N2 [label="bfunc\n2 (100.0%)\r",shape=box,fontsize=58.0];
N3 [label="main\n0 (0.0%)\rof 2 (100.0%)\r",shape=box,fontsize=8.0];
N3 -> N2 [label=2, weight=1, style="setlinewidth(2.000000)"];
N1 -> N3 [label=2, weight=1, style="setlinewidth(2.000000)"];
N2 -> N2 [label=1, weight=1, style="setlinewidth(2.000000)"];
}

 把它重定向到b.dot,生成gif,可能会遇到

$ dot -Tgif b -o outfile.gif
Renderer type: "gif" not recognized. Use one of: canon cmap cmapx dia dot fig hpgl imap ismap mif mp pcl pic plain plain-ext ps ps2 svg svgz vtx xdot

据说是一个Bug,yum install graph-gd之后就可以了。

也可以在下载源码安装,但是安装的时候提示graphviz的版本也要一致。

没办法重新下个一致的graphviz编译。

需要安装ann-libs等依赖见

http://rpm.pbone.net/index.php3/stat/3/srodzaj/1/search/libANN.so.1

http://www.graphviz.org/Download_linux_rhel.php

需要啥依赖都在rpm search那里搞吧。用个东西真不容易。。

最后终于大功告成

dot -Tgif b.dot -o outfile.gif

可以生成gif图片了。

 

这说明整数的加法相对于浮点数的乘法就是毛毛雨。

调整一下代码

double bfunc() {
    double sum = 0;
    for (int i = 0; i < 100; ++i) {
        sum = sum * i;
    }
    return sum;
}

int afunc() {
    double sum = 0;
    for (int i = 0; i < 100; ++i) {
        sum = sum * i;
    }
    return sum;
}
int main() {
    for (int i = 0; i < 1000000; ++i) afunc();
    for (int i = 0; i < 1000000; ++i) bfunc();
    return 0;
}

 

 

 这是符合期望的,但是main函数呢?可见main函数体的cpu运行时间太少了。再调整一下

double bfunc() {
    double sum = 0;
    for (int i = 0; i < 2; ++i) {
        sum = sum * i;
    }
    return sum;
}

int afunc() {
    double sum = 0;
    for (int i = 0; i < 2; ++i) {
        sum = sum * i;
    }
    return sum;
}
int main() {
    for (int i = 0; i < 50000000; ++i) afunc();
    for (int i = 0; i < 50000000; ++i) bfunc();
    return 0;
}

 

看到main函数了,为啥没有边(edges)?发现把之前的-g改成-O2了。。

 

 

g++ -g -lstdc++  test.cpp -lprofiler
sh make.sh
./a.out
pprof --dot --edgefraction=0.0 a.out /tmp/profile > b.dot
dot -Tgif b.dot -o outfile.gif
 最后的结果,如果不关心调用关系,只关心每个函数的CPU占用,还是可以把-g换成-O2的。

 

(如果编译加上-finstrument-functions,可以看到内核调用了__cyg_profile_func_exit)



 

 

最后为了用浏览器看到结果,

用tornado(http://www.tornadoweb.org/en/stable/)搭个web服务。

这个还是很使用的,因为很多时候我们在上面的分析可能在远程linux服务器下。

 

  • 大小: 10.5 KB
  • 大小: 11.8 KB
  • 大小: 12.5 KB
  • 大小: 16.6 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics