Linux下利用Valgrind工具进行内存泄露检测和性能分析_第1页
Linux下利用Valgrind工具进行内存泄露检测和性能分析_第2页
Linux下利用Valgrind工具进行内存泄露检测和性能分析_第3页
Linux下利用Valgrind工具进行内存泄露检测和性能分析_第4页
Linux下利用Valgrind工具进行内存泄露检测和性能分析_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

LinuxLinux 下利用下利用 ValgrindValgrind 工具进行内存泄露检测和性能分析工具进行内存泄露检测和性能分析 日期 2012 06 25 来源 Linux 社区 作者 yanghao23 Valgrind 通常用来成分析程序性能及程序中的内存泄露错误 一 Valgrind 工具集简绍 Valgrind 包含下列工具 1 memcheck 检查程序中的内存问题 如泄漏 越界 非法指针等 2 callgrind 检测程序代码的运行时间和调用过程 以及分析程序性能 3 cachegrind 分析 CPU 的 cache 命中率 丢失率 用于进行代码优化 4 helgrind 用于检查多线程程序的竞态条件 5 massif 堆栈分析器 指示程序中使用了多少堆内存等信息 6 lackey 7 nulgrind 这几个工具的使用是通过命令 valgrand tool name 程序名来分别调用的 当不指定 tool 参数时默认是 tool memcheck 二 Valgrind 工具详解 1 Memcheck 最常用的工具 用来检测程序中出现的内存问题 所有对内存的读写都会被检测到 一切对 malloc free new delete 的调用都会被捕获 所以 它能检测以下问题 1 对未初始化内存的使用 2 读 写释放后的内存块 3 读 写超出 malloc 分配的内存块 4 读 写不适当的栈中内存块 5 内存泄漏 指向一块内存的指针永远丢失 6 不正确的 malloc free 或 new delete 匹配 7 memcpy 相关函数中的 dst 和 src 指针重叠 这些问题往往是 C C 程序员最头疼的问题 Memcheck 能在这里帮上大忙 例如 include include include void test int ptr malloc sizeof int 10 ptr 10 7 内存越界 memcpy ptr 1 ptr 5 踩内存 free ptr free ptr 重复释放 int p1 p1 1 非法指针 int main void test return 0 将程序编译生成可执行文件后执行 valgrind leak check full 程序名 输出结果如下 4832 Memcheck a memory error detector 4832 Copyright C 2002 2010 and GNU GPL d by Julian Seward et al 4832 Using Valgrind 3 6 1 and LibVEX rerun with h for copyright info 4832 Command tmp 4832 4832 Invalid write of size 4 内存越界 4832 at 0 x804843F test in home yanghao Desktop testC testmem tmp 4832 by 0 x804848D main in home yanghao Desktop testC testmem tmp 4832 Address 0 x41a6050 is 0 bytes after a block of size 40 alloc d 4832 at 0 x4026864 malloc vg replace malloc c 236 4832 by 0 x8048435 test in home yanghao Desktop testC testmem tmp 4832 by 0 x804848D main in home yanghao Desktop testC testmem tmp 4832 4832 Source and destination overlap in memcpy 0 x41a602c 0 x41a6028 5 踩内存 4832 at 0 x4027BD6 memcpy mc replace strmem c 635 4832 by 0 x8048461 test in home yanghao Desktop testC testmem tmp 4832 by 0 x804848D main in home yanghao Desktop testC testmem tmp 4832 4832 Invalid free delete delete 重复释放 4832 at 0 x4025BF0 free vg replace malloc c 366 4832 by 0 x8048477 test in home yanghao Desktop testC testmem tmp 4832 by 0 x804848D main in home yanghao Desktop testC testmem tmp 4832 Address 0 x41a6028 is 0 bytes inside a block of size 40 free d 4832 at 0 x4025BF0 free vg replace malloc c 366 4832 by 0 x804846C test in home yanghao Desktop testC testmem tmp 4832 by 0 x804848D main in home yanghao Desktop testC testmem tmp 4832 4832 Use of uninitialised value of size 4 非法指针 4832 at 0 x804847B test in home yanghao Desktop testC testmem tmp 4832 by 0 x804848D main in home yanghao Desktop testC testmem tmp 4832 4832 4832 Process terminating with default action of signal 11 SIGSEGV 由于 非法指针赋值导致的程序崩溃 4832 Bad permissions for mapped region at address 0 x419FFF4 4832 at 0 x804847B test in home yanghao Desktop testC testmem tmp 4832 by 0 x804848D main in home yanghao Desktop testC testmem tmp 4832 4832 HEAP SUMMARY 4832 in use at exit 0 bytes in 0 blocks 4832 total heap usage 1 allocs 2 frees 40 bytes allocated 4832 4832 All heap blocks were freed no leaks are possible 4832 4832 For counts of detected and suppressed errors rerun with v 4832 Use track origins yes to see where uninitialised values come from 4832 ERROR SUMMARY 4 errors from 4 contexts suppressed 11 from 6 Segmentation fault 从 valgrind 的检测输出结果看 这几个错误都找了出来 2 Callgrind 和 gprof 类似的分析工具 但它对程序的运行观察更是入微 能给我们提供更多的信息 和 gprof 不同 它不需要在编译源代码时附加特殊选项 但加上调试选项是推荐的 Callgrind 收集程序运行时的一些数据 建立函数调用关系图 还可以有选择地进行 cache 模拟 在运行结束时 它会把分析数据写入一个文件 callgrind annotate 可以把这个文 件的内容转化成可读的形式 生成可视化的图形需要下载 gprof2dot 这是个 python 脚本 把它下载之后修改其权限 chmod 7 gprof2dot py 并把这个脚本 添加到 PATH 路径中的任一文件夹下 我是将它放到了 usr bin 目录下 这样就可以直接 在终端下执行 gprof2dot py 了 Callgrind 可以生成程序性能分析的图形 首先来说说程序性能分析的工具吧 通常可以 使用 gnu 自带的 gprof 它的使用方法是 在编译程序时添加 pg 参数 例如 include include void test sleep 1 void f int i for i 0 i 5 i test int main f printf process is over n return 0 首先执行 gcc pg o tmp tmp c 然后运行该程序 tmp 程序运行完成后会在当前目录 下生成 gmon out 文件 这个文件 gprof 在分析程序时需要 再执行 gprof tmp gprof2dot py dot Tpng o report png 打开 report png 结果 显示 test 被调用了 5 次 程序中耗时所占百分比最多的是 test 函数 再来看 Callgrind 的生成调用图过程吧 执行 valgrind tool callgrind tmp 执 行完成后在目录下生成 callgrind out XXX 的文件这是分析文件 可以直接利用 callgrind annotate callgrind out XXX 打印结果 也可以使用 gprof2dot py f callgrind callgrind out XXX dot Tpng o report png 来生成图形化结果 它生成的结果非常详细 甚至连函数入口 及库函数调用都标识出来了 3 Cachegrind Cache 分析器 它模拟 CPU 中的一级缓存 I1 Dl 和二级缓存 能够精确地指出程 序中 cache 的丢失和命中 如果需要 它还能够为我们提供 cache 丢失次数 内存引用次 数 以及每行代码 每个函数 每个模块 整个程序产生的指令数 这对优化程序有很大 的帮助 作一下广告 valgrind 自身利用该工具在过去几个月内使性能提高了 25 30 据早 先报道 kde 的开发 team 也对 valgrind 在提高 kde 性能方面的帮助表示感谢 它的使用方法也是 valgrind tool cachegrind 程序名 4 Helgrind 它主要用来检查多线程程序中出现的竞争问题 Helgrind 寻找内存中被多个线程访问 而又没有一贯加锁的区域 这些区域往往是线程之间失去同步的地方 而且会导致难以发 掘的错误 Helgrind 实现了名为 Eraser 的竞争检测算法 并做了进一步改进 减少了 报告错误的次数 不过 Helgrind 仍然处于实验阶段 首先举一个竞态的例子吧 include include define NLOOP 50 int counter 0 incremented by threads void threadfn void int main int argc char argv pthread t tid1 tid2 tid3 pthread create pthread create pthread create wait for both threads to terminate pthread join tid1 NULL pthread join tid2 NULL pthread join tid3 NULL return 0 void threadfn void vptr int i val for i 0 i NLOOP i val counter printf x d n unsigned int pthread self val 1 counter val 1 return NULL 这段程序的竞态在 30 32 行 我们想要的效果是 3 个线程分别对全局变量累加 50 次 最后 全局变量的值为 150 由于这里没有加锁 很明显竞态使得程序不能达到我们的目标 我 们来看 Helgrind 是如何帮我们检测到竞态的 先编译程序 gcc o test thread c lpthread 然后执行 valgrind tool helgrind test 输出结果如下 49c0b70 1 49c0b70 2 4666 Thread 3 was created 4666 at 0 x412E9D8 clone clone S 111 4666 by 0 x40494B5 pthread create GLIBC 2 1 createthread c 256 4666 by 0 x4026E2D pthread create WRK hg intercepts c 257 4666 by 0 x4026F8B pthread create hg intercepts c 288 4666 by 0 x8048524 main in home yanghao Desktop testC testmem a out 4666 4666 Thread 2 was created 4666 at 0 x412E9D8 clone clone S 111 4666 by 0 x40494B5 pthread create GLIBC 2 1 createthread c 256 4666 by 0 x4026E2D pthread create WRK hg intercepts c 257 4666 by 0 x4026F8B pthread create hg intercepts c 288 4666 by 0 x8048500 main in home yanghao Desktop testC testmem a out 4666 4666 Possible data race during read of size 4 at 0 x804a028 by thread 3 4666 at 0 x804859C threadfn in home yanghao Desktop testC testmem a out 4666 by 0 x4026F60 mythread wrapper hg intercepts c 221 4666 by 0 x4048E98 start thread pthread create c 304 4666 by 0 x412E9ED clone clone S 130 4666 This conflicts with a previous write of size 4 by thread 2 4666 at 0 x80485CA threadfn in home yanghao Desktop testC testmem a out 4666 by 0 x4026F60 mythread wrapper hg intercepts c 221 4666 by 0 x4048E98 start thread pthread create c 304 4666 by 0 x412E9ED clone clone S 130 4666 4666 Possible data race during write of size 4 at 0 x804a028 by thread 2 4666 at 0 x80485CA threadfn in home yanghao Desktop testC testmem a out 4666 by 0 x4026F60 mythread wrapper hg intercepts c 221 4666 by 0 x4048E98 start thread pthread create c 304 4666 by 0 x412E9ED clone clone S 130 4666 This conflicts with a previous read of size 4 by thread 3 4666 at 0 x804859C threadfn in home yanghao Desktop testC testmem a out 4666 by 0 x4026F60 mythread wrapper hg intercepts c 221 4666 by 0 x4048E98 start thread pthread create c 304 4666 by 0 x412E9ED clone clone S 130 4666 49c0b70 3 55c1b70 51 4666 4666 For counts of detected and suppressed errors rerun with v 4666 Use history level approx or none to gain increased speed at 4666 the cost of reduced accuracy of conflicting access i

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论