1.使用callgrind工具生成性能分析数据
命令格式如下:
valgrind --tool=callgrind ./vs
其中 ./vs就是我们要分析的程序。执行完毕后,就会在当前目录下生成一个文件。文件名为“callgrind.out.进程号”。如,callgrind.out.31113。注意,对于daemon进程的调试,不要通过kill -9方式停止。
如果你调试的程序是多线程,你也可以在命令行中加一个参数 -separate-threads=yes。这样就会为每个线程单独生成一个性能分析文件。如下:
valgrind --tool=callgrind --separate-threads=yes ./vs
生成的文件除了callgrind.out.31113外,还会多出一些子线程的文件。文件名如下:
callgrind.out.31113-01 callgrind.out.31113-02 callgrind.out.31113-03
2.把callgrind生成的性能数据转换成dot格式数据
可以使用gprof2dot.py脚本,把callgrind生成的性能分析数据转换成dot格式的数据。方便使用dot把分析数据图形化。
脚本使用方式如下:
python gprof2dot.py -f callgrind -n10 -s callgrind.out.31113 > valgrind.dot3.使用dot把数据生成图片
从源代码安装Graphviz
[root@jfht setup]# wget "http://www.graphviz.org/pub/graphviz/stable/SOURCES/graphviz-2.26.3.tar.gz"
--2010-12-12 09:10:26-- http://www.graphviz.org/pub/graphviz/stable/SOURCES/graphviz-2.26.3.tar.gz
正在解析主机 www.graphviz.org... 192.20.225.20
Connecting to www.graphviz.org|192.20.225.20|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:17092429 (16M) [application/x-gzip]
Saving to: `graphviz-2.26.3.tar.gz'
100%[==========================================================================================>] 17,092,429 174K/s in 1m 44s
2010-12-12 09:12:10 (161 KB/s) - `graphviz-2.26.3.tar.gz' saved [17092429/17092429]
[root@jfht setup]# tar zxf graphviz-2.26.3.tar.gz
[root@jfht setup]# cd graphviz-2.26.3
[root@jfht graphviz-2.26.3]# ls
aclocal.m4 builddate.h config.h.in configure.old dot.demo graphviz.sln INSTALL makearch plugin
ast_common.h ChangeLog config.h.old contrib Doxyfile graphviz.spec INSTALL.old Makeargs README
ast_common.h.in cmd config.iffe COPYING Doxyfile.in graphviz.spec.in lib Makefile.am rtest
AUTHORS compat_getopt.h Config.mk.old cpl1.0.txt features graphviz.vcproj libltdl Makefile.in share
autogen.sh compat.h configure debian graphs graphviz_version.h m4 Makefile.old tclpkg
awk config configure.ac doc graphviz.7 iffe macosx NEWS windows
[root@jfht graphviz-2.26.3]# ./configure --prefix=/usr
checking build system type... i686-pc-linux-gnu
checking host system type... i686-pc-linux-gnu
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... /bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking for style of include used by make... GNU
checking for gcc... gcc
checking for C compiler default output file name... a.out
checking whether the C compiler works... yes省略输出
----------------------------------------------------------------
graphviz-2.26.3 will be compiled with the following:
options:
cgraph: No (disabled by default - experimental)
digcola: Yes
expat: No (missing library)
fontconfig: No (missing fontconfig-config)
freetype: No (missing freetype-config)
glut: No (missing GL/glut.h)
gts: No (gts library not available)
ipsepcola: No (disabled by default - C++ portability issues)
ltdl: Yes
ortho: No (disabled by default - experimental)
png: No (missing png.h)
jpeg: No (missing header)
sfdp: Yes
shared: Yes
static: No (disabled by default)
commands:
dot: Yes (always enabled)
neato: Yes (always enabled)
fdp: Yes (always enabled)
circo: Yes (always enabled)
twopi: Yes (always enabled)
gvpr: Yes (always enabled)
lefty: No (missing Xaw headers)
smyrna: No (disabled by default - experimental)
plugin libraries:
dot_layout: Yes (always enabled)
neato_layout: Yes (always enabled)
core: Yes (always enabled)
devil: No (missing library)
gd: Yes (internal)
gdiplus: No (disabled by default - Windows only)
gdk_pixbuf: No (gdk_pixbuf library not available)
ghostscript: No (missing headers)
glitz: No (disabled by default - incomplete)
gtk: No (gtk library not available)
lasi: No (lasi library not available)
ming: No (disabled by default - incomplete)
pangocairo: No (pangocairo library not available)
quartz: No (disabled by default - Mac only)
rsvg: No (rsvg library not available)
visio: No (disabled by default - experimental)
xlib: No (disabled or unavailable)
language extensions:
gv_sharp: No (swig not available)
gv_guile: No (swig not available)
gv_io: No (disabled by default - no swig support yet)
gv_java: No (swig not available)
gv_lua: No (swig not available)
gv_ocaml: No (swig not available)
gv_perl: No (swig not available)
gv_php: No (swig not available)
gv_python: No (swig not available)
gv_python23: No (disabled by default - for multiversion installs)
gv_python24: No (disabled by default - for multiversion installs)
gv_python25: No (disabled by default - for multiversion installs)
gv_python26: No (disabled by default - for multiversion installs)
gv_R: No (swig not available)
gv_ruby: No (swig not available)
gv_tcl: No (tcl not available)
tcldot: No (tcl not available)
tclpathplan: No (tcl not available)
gdtclft: No (tcl not available)
tkspline: No (tk not available)
[root@jfht graphviz-2.26.3]#
[root@jfht graphviz-2.26.3]# make && make install
test -z "/usr/share/graphviz/doc/html" || /bin/mkdir -p "/usr/share/graphviz/doc/html"
test -z "/usr/share/man/man7" || /bin/mkdir -p "/usr/share/man/man7"
/usr/bin/install -c -m 644 'graphviz.7' '/usr/share/man/man7/graphviz.7'
test -z "/usr/include/graphviz" || /bin/mkdir -p "/usr/include/graphviz"
/usr/bin/install -c -m 644 'graphviz_version.h' '/usr/include/graphviz/graphviz_version.h'
test -z "/usr/share/graphviz/doc" || /bin/mkdir -p "/usr/share/graphviz/doc"
/usr/bin/install -c -m 644 'AUTHORS' '/usr/share/graphviz/doc/AUTHORS'
/usr/bin/install -c -m 644 'COPYING' '/usr/share/graphviz/doc/COPYING'
/usr/bin/install -c -m 644 'ChangeLog' '/usr/share/graphviz/doc/ChangeLog'
/usr/bin/install -c -m 644 'NEWS' '/usr/share/graphviz/doc/NEWS'
/usr/bin/install -c -m 644 'cpl1.0.txt' '/usr/share/graphviz/doc/cpl1.0.txt'
make[2]: Leaving directory `/root/setup/graphviz-2.26.3'
make[1]: Leaving directory `/root/setup/graphviz-2.26.3'
You have new mail in /var/spool/mail/root
[root@jfht graphviz-2.26.3]#
命令格式如下:
dot -Tpng valgrind.dot -o valgrind.png生成的图片示例
通过图形,我们可以很直观的知道那段程序执行慢,并且了解相关调用关系