当前位置:文档之家› ValGrind的使用分享

ValGrind的使用分享

ValGrind的使用分享
淘宝搜索中心
yewang
1
目录
•Valgrind 介绍 •Valgrind 安装 •Valgrind 使用方法 •Memcheck 工具 •Cachegrind 工具 •Callgrind 工具 •Helgrind & Massif 工具简介
2
Valgrind介绍
• Valgrind是一个Linux下灵活的调试和剖析 工具。收集各种有用的运行时信息,可以 帮助找到程序中潜在的bug和性能瓶颈 • Valgrind提供多个工具,其中最重要的是:
使用方法
• 命令格式:
Valgrind [options] [your-program] [[program-options]]
• 样例:
valgrind ./txt2db
memcheck内存错误检查
• 命令:
Valgrind -v --leak-check=full --tool=memcheck ./txt2db -f 11.txt -d 22
统计信息查看
• 指令:
• • • • • • • • • • • • • I refs: 总操作数 I1 misses: 一级缓存指令未命中次数 L2i misses: 二级缓存指令未命中数 I1 miss rate: 一级缓存指令未命中率 L2i miss rate: 二级缓存指令未命中率 D refs: 总数据操作数(读+写) D1 misses :一级缓存数据未命中次数 L2d misses:二级缓存数据未命中数 D1 miss rate: 一级缓存数据未命中率 L2d miss rate:二级缓存数据未命中率 L2 refs : I1 misses + D1 misses L2 misses : L2i misses + L2d misses L2 miss rate: L2 misses / I refs+D refs
Cachegrind:
提供详尽的profiling信息。它不光对指令、内存访问进行计数,更能 针对每条指令、每行源代码、每个函数和每个程序统计cache的不命 中次数。cache对目前系统的性能有决定性的影响。这些信息可以指 导我们优化程序性能
产生2个输出:
1. 统计信息 2. 详细信息文件,文件名为cachegrind.out.PID
Memcheck Cachegrind Callgrind Helgrind Massif
安装
• 官方网站:/ • 下载链接:/downloads/valgrind3.6.0.tar.bz2
• 安装步骤:
bzip2 -d valgrind-3.6.0.tar.bz2 tar -xvf valgrind-3.6.0.tar cd valgrind-3.6.0 修改配置请参考备注 ./configure & make & make install,
192.168.133.131 -p 20001 &
• 2. 执行正常的服务请求
• 3. 打开新的终端,执行 pkill memcheck
• 4. 返回原有终端,查看daemon.log
Cachegrind缓存检查
• 命令:
valgrind --tool=cachegrind ./memcheckTest
Massif堆栈分析
• 命令: valgrind --tool=massif ./txt2db -f 11.txt -d aa.db • 堆栈分析器,它能测量程序在堆栈中使用了多少内存,告 诉我们堆块,堆管理块和栈的大小。Massif能帮助我们减 少内存的使用 • 输出文件:massif.<pid>.ps massif. <pid>.txt
• 数据:
• 二级缓存:
cachegrind.out 查看
• 命令:
cg_annotate --sort=Dr --threshold=100 cachegrind.out.6393
• • • • • • • • • • • • • • • • -------------------------------------------------------------------------------I1 cache: 32768 B, 64 B, 8-way associative 总缓存大小,行的大小,每组包含的行数 D1 cache: 32768 B, 64 B, 8-way associative L2 cache: 2097152 B, 64 B, 8-way associative ………………….. -------------------------------------------------------------------------------- I : 指令 r:读 1:一级缓存 m:未命中 Ir I1mr I2mr Dr D1mr D2mr Dw D1mw D2mw D : 数据 w:写 2:二级缓存 -------------------------------------------------------------------------------1,835,635 1,390 1,349 566,433 13,039 4,502 171,223 966 581 PROGRAM TOTALS -------------------------------------------------------------------------------Ir I1mr I2mr Dr D1mr D2mr Dw D1mw D2mw file:function -------------------------------------------------------------------------------596,785 9 9 94,112 1,143 589 31,455 3 2 ???:_dl_lookup_symbol_x 540,656 12 12 218,986 5,600 1,282 69,968 13 3 ???:do_lookup_x 235,653 2 2 85,261 2,457 1,128 0 0 0 ???:strcmp 227,933 55 54 86,496 2,292 903 32,893 520 240 ???:_dl_relocate_object
循环优化前
循环优化后
ห้องสมุดไป่ตู้allgrind 函数调用分析
• 命令:
valgrind --tool=callgrind ./txt2db -f 11.txt -d aa.db
• 和gprof类似的分析工具,它对程序的运行观察细 致入微,能帮我们建立函数调用关系图,发现很 多隐藏的函数调用及消耗,对我们优化程序有非 常好的指导作用 • 输出文件 callgrind.out.<pid> • 被分析的程序编译时要 加-g ,建议-o2
Memcheck:
可以用来寻找c/c++程序中内存管理的错误。 很多隐藏很深的bug是内存操作问题。在Memcheck面前都无处遁形 它可以检查出下列几种错误: 1. 使用已经释放的内存 2. 内存块越界 3. 使用未初始化的变量 4. 内存泄漏(申请了未释放) 5. 同一个内存块释放多次 6. 栈内错误无法检查
callgrind.out查看
• 命令:
callgrind_annotate --threshold=100 --auto=yes --tree=both callgrind.out.7009
Helgrind多线程分析器
• 命令: valgrind --tool=helgrind ./pthreadTest • 主要用来检查多线程程序中出现的竞争问题。Helgrind寻 找内存中被多个线程访问,而又没有一贯加锁的区域,这 些区域往往是线程之间失去同步的地方,而且会导致难以 发掘的错误。Helgrind实现了名为“Eraser”的竞争检测算 法,并做了进一步改进,减少了报告错误的次数。
检查daemon进程
• 1. valgrind -q --log-file=daemon.log --trace-children=yes --leak-check=full --tool=memcheck /home/liminch/ll2/DSserver/DSserver -c 5 -h
相关主题