Callgrind 使用 2

       Callgrind是一款和gprof类似的性能分析工具,与gprof不同的是它不需要在编译源码时附加特殊选项,但推荐加上调试选项。Callgrind使用cachegrind的统计信息Ir(I cache reads,即一条指令执行的次数)来统计程序中函数的调用情况,建立函数调用关系图,还可以有选择地进行cache模拟。在运行结束时,它会把分析数据写入一个文件,callgrind_annotate可以把这个文件的内容转化成可读的形式。
一、基本语法
        Callgrind语法格式如下:
        valgrind --tool=callgrind [callgrind options] your-program [program options]
        可以如下来使用valgrind启动后台服务进程:
        #valgrind --tool=callgrind --separate-threads=yes ./haproxy -f haproxy.cfg
        其中,--tool=callgrind表示使用valgrind提供的性能分析功能,--separate-threads=yes表示要查看多线程相关数据。
        在程序运行过程中可以用callgrind_control将性能数据dump到指定目录下(可以使用callgrind_control -h查看帮助信息),比如:
        #callgrind_control -d -w /usr/local/haproxy
        上面命令会将所有线程的性能数据dump到目录/usr/local/haproxy下生成的callgrind.out.pid文件中。
        我们可以使用callgrind_annotate [options] callgrind.out.pid查看性能数据,其中,options选项参数详细信息如下:
        (1)--inclusive=yes:不但分别统计每个语句的执行次数,还把调用关系计算进入,比如函数foo调用了bar,那么foo的代价中会加入bar的代价。
        (2)--tree=both:显示调用关系。
        (3)--auto=yes:会自动将统计信息和源码关联。
        单纯查看性能数据,可以如下:
        #callgrind_annotate --inclusive=yes callgrind.out.pid
        既要查看性能数据,又要查看调用关系,可以如下:
        #callgrind_annotate --inclusive=yes --tree=both callgrind.out.pid 

二、使用实例
        下面将提供一段简单的代码来演示如何使用callgrind来分析性能,具体代码如下:
            
        编译代码并使用callgrind进行性能统计:
            
        此时,在当前目录下能看到生成了callgrind.out.23588的性能数据文件。当然如果在编译代码时想生成汇编代码,那么可以如下:
        #g++ -S -g -o test test.cpp
        不错如此一来,则就会在当前目录下生成一大批callgrind.out.xxxxx的性能数据文件,在此不表汇编的事情。
        接下来分别用前文提到三种方式来查看统计信息:
        (1)--auto=yes
        #callgrind_annotate --auto=yes callgrind.out.23588 > log
        #vi log
             
        由于这种模拟统计需要消耗大量的资源,所以callgrind只统计次数而不统计时间,上面只是截取了统计分析结果的一部分,左侧数字表示该条代码执行所用的指令数。   
        (2)--tree=both
        #callgrind_annotate --tree=both callgrind.out.23588 > log
        #vi log
        
        上图中左侧数字表示调用带来的代价。
        (3)--inclusive=yes
        #callgrind_annotate --inclusive=yes callgrind.out.23588 > log
        #vi log
        
        像上图这般分析比较常见。
        

posted @ 2016-07-05 14:14  zengkefu  阅读(3996)  评论(0编辑  收藏  举报