第一句子网 - 唯美句子、句子迷、好句子大全
第一句子网 > 【程序性能分析利器】Google Perf Tool 和 Valgrind 工具包简介

【程序性能分析利器】Google Perf Tool 和 Valgrind 工具包简介

时间:2022-09-22 04:32:41

相关推荐

【程序性能分析利器】Google Perf Tool 和 Valgrind 工具包简介

Google Perf Tools 的安装和使用

Gperf 工具包包含如下几个工具: 一个优化的内存管理算法—tcmalloc性能优于malloc。一个用于CPU profile的工具,用于检测程序的性能热点,这个功能和gprof类似。一个用于堆检查工具,用于检测程序在是够有内存泄露,这个功能和valgrind类似。一个用于Heap profile的工具,用于监控程序在执行过程的内存使用情况。其使用方法如下:

1.使用其提供的内存管理函数---TC Malloc:gcc [...] -ltcmalloc 2.使用其堆内存检查工具---Heap Checker:gcc [...] -o myprogram -ltcmallocHEAPCHECK=normal ./myprogram 3.使用Heap Profiler:gcc [...] -o myprogram -ltcmallocHEAPPROFILE=/tmp/netheap ./myprogram 4.使用Cpu Profiler:gcc [...] -o myprogram -lprofilerCPUPROFILE=/tmp/profile ./myprogram

官方代码:/gperftools/gperftools

官方文档:/p/google-perftools/wiki/GooglePerformanceTools

官方 github 简要文档:https://gperftools.github.io/gperftools

1.安装依赖 libunwind

(1)源码安装libunwind

wget /libunwind/libunwind/archive/v0.99.tar.gz tar -xvf v0.99.tar.gz cd libunwind-0.99 autoreconf --force -v --install ./configure make make install

其安装版本可自行选择。在 ./configure 步如果不加生成选项,--prefix=path 不指定安装的目录, lib 会默认产生在系统/usr/local/lib,头文件在 /usr/local/include

(2)yum安装

yum search libunwind # 查找,然后选择需要的安装 yum install libunwind-devel.x86_64

2.安装gperftools

源码安装google-perf-tools/gperftools

wget /gperftools/gperftools/releases/download/gperftools-2.6.1/gperftools-2.6.1.tar.gz tar -xvf gperftools-2.6.1.tar.gz # 解压 ./configure --prefix=gperftools-tutorial/output make make install

2.1 问题

问题一:安装时可能出现configure: WARNING: No frame pointers and no libunwind. Using experimental backtrace capturing via libgcc.Expect crashy cpu profiler。

是因为没有安装libunwind。在gperftools工具的INSTLL例有说明,64位系统需要安装。使用yum search libunwind查找,然后选择需要的安装。

问题二:编译时打开了./configure –enable-frame-pointers ,这要求被测试的程序在编译时要加上gcc编译选项,否则某些多线程程序可能会 core:CCFLAGS=-fno-omit-frame-pointer

3.gperftools使用

我们在Google使用的CPU分析器。使用它的过程分为三个部分:将库链接到应用程序,运行代码以及分析输出。

3.1 Linking in the Library

要将CPU profiler安装到可执行文件中,请添加-lprofiler链接到可执行文件。(也可能在运行时使用LD_PRELOAD,例如 在添加 % env LD_PRELOAD="/usr/lib/libprofiler.so"

(1)如果是默认的prefix安装在/usr/local/lib/libprofiler.so

g++ src/test-normal/test-normal.cpp -lprofiler -o test-normal

(2)按顺序执行下列shell

export LD_PRELOAD="/usr/local/lib/libprofiler.so" g++ src/test-normal/test-normal.cpp -o test-normal env CPUPROFILE=test-normal.prof ./test-normal

可以看到profiler的文件test-normal.prof

(3)

env LD_PRELOAD=/usr/local/lib/libprofiler.so CPUPROFILE=test-normal.prof ./test-normal

可以看到profiler的文件test-normal.prof

3.2 Running the Code

此外,还可以将其嵌入到代码中运行。此处可参考官方文档。

3.3 Analyzing the Output

需要dot为任何图形输出,yum install graphviz, 这里有一些调用pprof的方法。这些将在下面更详细地描述。

pprof /bin/ls ls.profEnters "interactive" modepprof --text /bin/ls ls.profOutputs one line per procedurepprof --gv /bin/ls ls.profDisplays annotated call-graph via 'gv'pprof --gv --focus=Mutex /bin/ls ls.profRestricts to code paths including a .*Mutex.* entrypprof --gv --focus=Mutex --ignore=string /bin/ls ls.profCode paths including Mutex but not stringpprof --list=getdir /bin/ls ls.prof(Per-line) annotated source listing for getdir()pprof --disasm=getdir /bin/ls ls.prof(Per-PC) annotated disassembly for getdir()pprof --text localhost:1234Outputs one line per procedure for localhost:1234pprof --callgrind /bin/ls ls.profOutputs the call information in callgrind format

pprof --pdf test-server test-server.prof > test-server.pdf

(1)节点信息

查看 test-server.pdf,在pprof的各种图形模式中,输出是带有时序信息注释的调用图每个节点代表一个过程。有向边指示调用者与被调用者的关系。每个节点的格式如下表所示。

最后一两行包含时序信息。(分析是通过采样方法完成的,默认情况下,我们每秒进行100次采样。因此,输出中的一个时间单位对应于大约10毫秒的执行时间。)"local"时间是执行指令所花费的时间。直接包含在过程中(以及该过程中内联的任何其他过程中)。“cumulative”时间是“local”时间与在任何被调者身上花费的时间之和。如果累计时间与当地时间相同,则不会打印。

(2)边信息

从一个节点到另一个节点的边表示调用者与被调用者的关系。每个边都标有被调用者代表调用者花费的时间。

有向边:调用者指向被调用者,有向边上的时间表示被调用者所消耗的CPU时间

valgrind的callgrind工具进行多线程性能分析

安装

1./downloads/old.html2.yum install valgrind3. 上述安装完 gperf 后便会默认安装 valgrind 工具包。

Valgrind 的主要作者 Julian Seward 刚获得了今年的Google-O'Reilly开源大奖之一──Best Tool Maker。让我们一起来看一下他的作品。Valgrind 是运行在 Linux 上一套基于仿真技术的程序调试和分析工具,它包含一个内核──一个软件合成的CPU,和一系列的小工具,每个工具都可以完成一项任务──调试,分析,或测试等。Valgrind可以检测内存泄漏和内存违例,还可以分析cache的使用等,灵活轻巧而又强大,能直穿程序错误的心脏,真可谓是程序员的瑞士军刀。

一. Valgrind概观

Valgrind的最新版是3.2.0,它一般包含下列工具:

1.Memcheck

最常用的工具,用来检测程序中出现的内存问题,所有对内存的读写都会被检测到,一切对malloc()/free()/new/delete的调用都会被捕获。所以,它能检测以下问题:

1.对未初始化内存的使用;

2.读/写释放后的内存块;

3.读/写超出malloc分配的内存块;

4.读/写不适当的栈中内存块;

5.内存泄漏,指向一块内存的指针永远丢失;

6.不正确的malloc/free或new/delete匹配;

7,memcpy()相关函数中的dst和src指针重叠。

这些问题往往是C/C++程序员最头疼的问题,Memcheck在这里帮上了大忙。

2.Callgrind

和gprof类似的分析工具,但它对程序的运行观察更是入微,能给我们提供更多的信息。和gprof不同,它不需要在编译源代码时附加特殊选项,但加上调试选项是推荐的。Callgrind收集程序运行时的一些数据,建立函数调用关系图,还可以有选择地进行cache模拟。在运行结束时,它会把分析数据写入一个文件。callgrind_annotate可以把这个文件的内容转化成可读的形式。

3.Cachegrind

Cache分析器,它模拟CPU中的一级缓存I1,Dl和二级缓存,能够精确地指出程序中cache的丢失和命中。如果需要,它还能够为我们提供cache丢失次数,内存引用次数,以及每行代码,每个函数,每个模块,整个程序产生的指令数。这对优化程序有很大的帮助。

4.Helgrind

它主要用来检查多线程程序中出现的竞争问题。Helgrind寻找内存中被多个线程访问,而又没有一贯加锁的区域,这些区域往往是线程之间失去同步的地方,而且会导致难以发掘的错误。Helgrind实现了名为“Eraser”的竞争检测算法,并做了进一步改进,减少了报告错误的次数。不过,Helgrind仍然处于实验阶段。

5. Massif

堆栈分析器,它能测量程序在堆栈中使用了多少内存,告诉我们堆块,堆管理块和栈的大小。Massif能帮助我们减少内存的使用,在带有虚拟内存的现代系统中,它还能够加速我们程序的运行,减少程序停留在交换区中的几率。

此外,lackey和nulgrind也会提供。Lackey是小型工具,很少用到;Nulgrind只是为开发者展示如何创建一个工具。我们就不做介绍了。

二. 使用Valgrind

Valgrind的使用非常简单,valgrind命令的格式如下:

valgrind [valgrind-options] your-prog [your-prog options]

一些常用的选项如下:

选项 :作用

-h --help 显示帮助信息。

--version 显示valgrind内核的版本,每个工具都有各自的版本。

-q --quiet 安静地运行,只打印错误信息。

-v --verbose 打印更详细的信息。

--tool= [default: memcheck] 最常用的选项。运行valgrind中名为toolname的工具。如果省略工具名,默认运行memcheck。

--db-attach= [default: no] 绑定到调试器上,便于调试错误。

我们通过例子看一下它的具体使用。我们构造一个存在内存泄漏的C程序,如下:

#include #include void f(void) { int* x = malloc(10 * sizeof(int)); x[10] = 0; // problem 1: heap block overrun } // problem 2: memory leak -- x not freed int main(void) { int i; f(); printf("i=%d/n",i); //problem 3: use uninitialised value. return 0; }

保存为memleak.c并编译,然后用valgrind检测。

$ gcc -Wall -o memleak memleak.c

$ valgrind --tool=memcheck ./memleak

我们得到如下错误信息: ==3649== Invalid write of size 4 ==3649== at 0x80483CF: f (in /home/wangcong/memleak) ==3649== by 0x80483EC: main (in /home/wangcong/memleak) ==3649== Address 0x4024050 is 0 bytes after a block of size 40 alloc'd ==3649== at 0x40051F9: malloc (vg_replace_malloc.c:149) ==3649== by 0x80483C5: f (in /home/wangcong/memleak) ==3649== by 0x80483EC: main (in /home/wangcong/memleak) 前面的3649是程序运行时的进程号。第一行是告诉我们错误类型,这里是非法写入。下面的是告诉我们错误发生的位置,在main()调用的f()函数中。 ==3649== Use of uninitialised value of size 4 ==3649== at 0xC3A264: _itoa_word (in /lib/libc-2.4.so) ==3649== by 0xC3E25C: vfprintf (in /lib/libc-2.4.so) ==3649== by 0xC442B6: printf (in /lib/libc-2.4.so) ==3649== by 0x80483FF: main (in /home/wangcong/memleak) 这个错误是使用未初始化的值,在main()调用的printf()函数中。这里的函数调用关系是通过堆栈跟踪的,所以有时会非常多,尤其是当你使用C++的STL时。其它一些错误都是由于把未初始化的值传递给libc函数而被检测到。在程序运行结束后,valgrind还给出了一个小的总结: ==3649== ERROR SUMMARY: 20 errors from 6 contexts (suppressed: 12 from 1) ==3649== malloc/free: in use at exit: 40 bytes in 1 blocks. ==3649== malloc/free: 1 allocs, 0 frees, 40 bytes allocated. ==3649== For counts of detected errors, rerun with: -v ==3649== searching for pointers to 1 not-freed blocks. ==3649== checked 47,256 bytes. ==3649== ==3649== LEAK SUMMARY: ==3649== definitely lost: 40 bytes in 1 blocks. ==3649== possibly lost: 0 bytes in 0 blocks. ==3649== still reachable: 0 bytes in 0 blocks. ==3649== suppressed: 0 bytes in 0 blocks. ==3649== Use --leak-check=full to see details of leaked memory.

我们可以很清楚地看出,分配和释放了多少内存,有多少内存泄漏。这对我们查找内存泄漏十分方便。然后我们重新编译程序并绑定调试器:

$ gcc -Wall -ggdb -o memleak memleak.c

$ valgrind --db-attach=yes --tool=memcheck ./memleak

一出现错误,valgrind会自动启动调试器(一般是gdb):

==3893== ---- Attach to debugger ? --- [Return/N/n/Y/y/C/c] ---- ystarting debugger==3893== starting debugger with cmd: /usr/bin/gdb -nw /proc/3895/fd/1014 3895

退出gdb后我们又能回到valgrind继续执行程序。

还是用上面的程序,我们使用callgrind来分析一下它的效率:

$ valgrind --tool=callgrind ./memleak

Callgrind会输出很多,而且最后在当前目录下生成一个文件: callgrind.out.pid。用callgrind_annotate来查看它:

$ callgrind_annotate callgrind.out.3949

详细的信息就列出来了。而且,当callgrind运行你的程序时,你还可以使用callgrind_control来观察程序的执行,而且不会干扰它的运行。

再来看一下cachegrind的表现:

$ valgrind --tool=cachegrind ./memleak

得到如下信息:

==4073== I refs: 147,500 ==4073== I1 misses: 1,189 ==4073== L2i misses: 679 ==4073== I1 miss rate: 0.80% ==4073== L2i miss rate: 0.46% ==4073== ==4073== D refs: 61,920 (46,126 rd + 15,794 wr) ==4073== D1 misses: 1,759 ( 1,545 rd + 214 wr) ==4073== L2d misses: 1,241 ( 1,062 rd + 179 wr) ==4073== D1 miss rate: 2.8% ( 3.3% + 1.3% ) ==4073== L2d miss rate: 2.0% ( 2.3% + 1.1% ) ==4073== ==4073== L2 refs: 2,948 ( 2,734 rd + 214 wr) ==4073== L2 misses: 1,920 ( 1,741 rd + 179 wr) ==4073== L2 miss rate: 0.9% ( 0.8% + 1.1% )

上面的是指令缓存,I1和L2i缓存,的访问信息,包括总的访问次数,丢失次数,丢失率。

中间的是数据缓存,D1和L2d缓存,的访问的相关信息,下面的L2缓存单独的信息。Cachegrind也生成一个文件,名为cachegrind.out.pid,可以通过cg_annotate来读取。输出是一个更详细的列表。Massif的使用和cachegrind类似,不过它也会生成一个名为massif.pid.ps的PostScript文件,里面只有一幅描述堆栈使用状况的彩图。

以上只是简单的演示了valgrind的使用,更多的信息可以在它附带的文档中得到,也可以访问valgrind的主页:。学会正确合理地使用valgrind对于调试程序会有很大的帮助。

三、使用valgrind的callgrind工具进行多线程性能分析

valgrind是开源的性能分析利器。 根据它的文档,可以用它来检查内存泄漏等问题,还可以用来生成函数的调用图,就这两个功能就足够有吸引力了。

本文主要是介绍如何使用valgrind的callgrind工具进行性能分析。

分析过程

使用callgrind工具生成性能分析数据

命令格式如下:

valgrind --tool=callgrind ./exproxy

其中 ./exproxy就是我们要分析的程序。执行完毕后,就会在当前目录下生成一个文件。文件名为“callgrind.out.进程号”。如,callgrind.out.31113。注意,对于daemon进程的调试,不要通过kill -9方式停止。

如果你调试的程序是多线程,你也可以在命令行中加一个参数-separate-threads=yes。这样就会为每个线程单独生成一个性能分析文件。如下:

valgrind --tool=callgrind --separate-threads=yes ./exproxy

生成的文件除了callgrind.out.31113外,还会多出一些子线程的文件。文件名如下:

callgrind.out.31113-01 callgrind.out.31113-02 callgrind.out.31113-03

把callgrind生成的性能数据转换成dot格式数据

可以使用gprof2dot.py脚本,把callgrind生成的性能分析数据转换成dot格式的数据。方便使用dot把分析数据图形化。

脚本可以点此下载。脚本使用方式如下:

python gprof2dot.py -f callgrind -n10 -s callgrind.out.31113 > valgrind.dot

使用dot把数据生成图片

命令格式如下:

dot -Tpng valgrind.dot -o valgrind.png

生成的图片示例

通过图形,我们可以很直观的知道那段程序执行慢,并且了解相关调用关系。

[root@localhost blake]# cat kk.c#include <stdio.h>#include <stdlib.h>void f1() {int i;int *p;for (i = 0; i < 10; i++) {p = malloc(sizeof(int));*p = 10;free(p);}}void f2() {int i;int *p;for (i = 0; i < 20; i++) {p = malloc(sizeof(int));*p = 10;free(p);}}void f3() {int i;int *p;for (i = 0; i < 30; i++) {p = malloc(sizeof(int));*p = 10;free(p);}}int main() {int i;for (i = 0; i < 1000; i++) {f1();f2();f3();}return 0;}[root@localhost blake]# gcc -g kk.c -okk

[root@localhost uu]# valgrind --tool=callgrind ./kk --dump-instr=yes --trace-jump=yes==4798== Callgrind, a call-graph generating cache profiler==4798== Copyright (C) 2002-, and GNU GPL'd, by Josef Weidendorfer et al.==4798== Using Valgrind-3.5.0 and LibVEX; rerun with -h for copyright info==4798== Command: ./kk --dump-instr=yes --trace-jump=yes==4798== ==4798== For interactive control, run 'callgrind_control -h'.==4798== ==4798== Events : Ir==4798== Collected : 11648419==4798== ==4798== I refs:11,648,419

[root@localhost uu]# ll总用量 44-rw------- 1 root root 26499 Jul 4 21:01 callgrind.out.4798-rwxr-xr-x 1 root root 9289 Jul 4 20:48 kk-rw-r--r-- 1 root root 516 Jul 4 20:48 kk.c

把callgrind.out.4798传到 window 7 ,

如果要时行源代码显示,KK.C也传到windows 7 。

用kcachegrind 打开callgrind.out.4798 ,即显示下面图

LINUX: /html/Home.html

windows: /projects/precompiledbin/files/?source=navbar

其他相关使用介绍的文章可参见:

/swartz_lubel/category_6968911.html

/yutianzuijin/article/details/109731052

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。