ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

valgrind内存泄露和线程竞态检测,Android岗面试12家大厂成功跳槽

2021-12-26 17:06:16  阅读:209  来源: 互联网

标签:clone 12 create 4832 valgrind 线程 pthread 4666


==4832==    at 0x804847B: test (in /home/yanghao/Desktop/testC/testmem/tmp)

==4832==    by 0x804848D: 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:http://jrfonseca.googlecode.com/svn/trunk/gprof2dot/gprof2dot.py

这是个Python脚本,把它下载之后修改其权限chmod +7 gprof2dot.py ,并把这个脚本添加到$PATH路径中的任一文件夹下,我是将它放到了/usr/bin目录下,这样就可以直接在终端下执行gprof2dot.py了。

Callgrind可以生成程序性能分析的图形,首先来说说程序性能分析的工具吧,通常可以使用gnu自带的gprof,它的使用方法是

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

:在编译程序时添加-pg参数,例如:

[plain]  view plain copy

  1. #include <stdio.h>

  2. #include <malloc.h>

  3. void test()

  4. {

  5. sleep(1);

  6. }

  7. void f()

  8. {

  9. int i;

  10. for( i = 0; i < 5; i ++)

  11. test();

  12. }

  13. int main()

  14. {

  15. f();

  16. printf(“process is over!\n”);

  17. return 0;

  18. }

首先执行 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仍然处于实验阶段。

首先举一个竞态的例子吧:

[plain]  view plain copy

  1. #include <stdio.h>

  2. #include <pthread.h>

  3. #define NLOOP 50

  4. int counter = 0; /* incremented by threads */

  5. void *threadfn(void *);

  6. int main(int argc, char **argv)

  7. {

  8. pthread_t tid1, tid2,tid3;

  9. pthread_create(&tid1, NULL, &threadfn, NULL);

  10. pthread_create(&tid2, NULL, &threadfn, NULL);

  11. pthread_create(&tid3, NULL, &threadfn, NULL);

  12. /* wait for both threads to terminate */

  13. pthread_join(tid1, NULL);

  14. pthread_join(tid2, NULL);

  15. pthread_join(tid3, NULL);

  16. return 0;

  17. }

  18. void *threadfn(void *vptr)

  19. {

  20. int i, val;

  21. for (i = 0; i < NLOOP; i++) {

  22. val = counter;

  23. printf("%x: %d \n", (unsigned int)pthread_self(),  val+1);

  24. counter = val+1;

  25. }

  26. return NULL;

  27. }

这段程序的 竞态在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 0x412E9D8: clone (clone.S:111)

==4666==    by 0x40494B5: pthread_create@@GLIBC_2.1 (createthread.c:256)

==4666==    by 0x4026E2D: pthread_create_WRK (hg_intercepts.c:257)

==4666==    by 0x4026F8B: pthread_create@* (hg_intercepts.c:288)

==4666==    by 0x8048524: main (in /home/yanghao/Desktop/testC/testmem/a.out)

==4666==

==4666== Thread #2 was created

==4666==    at 0x412E9D8: clone (clone.S:111)

==4666==    by 0x40494B5: pthread_create@@GLIBC_2.1 (createthread.c:256)

==4666==    by 0x4026E2D: pthread_create_WRK (hg_intercepts.c:257)

==4666==    by 0x4026F8B: pthread_create@* (hg_intercepts.c:288)

==4666==    by 0x8048500: main (in /home/yanghao/Desktop/testC/testmem/a.out)

==4666==

==4666== Possible data race during read of size 4 at 0x804a028 by thread #3

==4666==    at 0x804859C: threadfn (in /home/yanghao/Desktop/testC/testmem/a.out)

==4666==    by 0x4026F60: mythread_wrapper (hg_intercepts.c:221)

==4666==    by 0x4048E98: start_thread (pthread_create.c:304)

==4666==    by 0x412E9ED: clone (clone.S:130)

==4666==  This conflicts with a previous write of size 4 by thread #2

==4666==    at 0x80485CA: threadfn (in /home/yanghao/Desktop/testC/testmem/a.out)

==4666==    by 0x4026F60: mythread_wrapper (hg_intercepts.c:221)

==4666==    by 0x4048E98: start_thread (pthread_create.c:304)

==4666==    by 0x412E9ED: clone (clone.S:130)

==4666==

==4666== Possible data race during write of size 4 at 0x804a028 by thread #2

==4666==    at 0x80485CA: threadfn (in /home/yanghao/Desktop/testC/testmem/a.out)

==4666==    by 0x4026F60: mythread_wrapper (hg_intercepts.c:221)

==4666==    by 0x4048E98: start_thread (pthread_create.c:304)

==4666==    by 0x412E9ED: clone (clone.S:130)

==4666==  This conflicts with a previous read of size 4 by thread #3

==4666==    at 0x804859C: threadfn (in /home/yanghao/Desktop/testC/testmem/a.out)

==4666==    by 0x4026F60: mythread_wrapper (hg_intercepts.c:221)

==4666==    by 0x4048E98: start_thread (pthread_create.c:304)

==4666==    by 0x412E9ED: 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 information

==4666== ERROR SUMMARY: 8 errors from 2 contexts (suppressed: 99 from 31)

helgrind成功的找到了 竞态的所在位置,标红所示。

5. Massif

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

Massif对内存的分配和释放做profile。程序开发者通过它可以深入了解程序的内存使用行为,从而对内存使用进行优化。这个功能对C++尤其有用,因为C++有很多隐藏的内存分配和释放。

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

三 使用Valgrind

Valgrind使用起来非常简单,你甚至不需要重新编译你的程序就可以用它。当然如果要达到最好的效果,获得最准确的信息,还是需要按要求重新编译一下的。比如在使用memcheck的时候,最好关闭优化选项。

valgrind命令的格式如下:

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

valgrind --tool=massif --stacks=yes ./test

(这个工具有个bug, 只有程序中出现new或者malloc之类的堆操作,才会统计栈的使用,否则只统计堆的使用)

一些常用的选项如下:

|

选项

|

作用

|

| — | — |

|

-h --help

|

显示帮助信息。

|

|

–version

|

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

|

|

标签:clone,12,create,4832,valgrind,线程,pthread,4666
来源: https://blog.csdn.net/m0_65512512/article/details/122157440

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有