1. 程式人生 > >Linux下利用Valgrind工具進行記憶體洩露檢測和效能分析

Linux下利用Valgrind工具進行記憶體洩露檢測和效能分析

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能在這裡幫上大忙。
例如:

  1. #include <stdlib.h>  
  2. #include <malloc.h>  
  3. #include <string.h>  
  4. void test()  
  5. {  
  6.     int *ptr = malloc(sizeof(int)*10);  
  7.     ptr[10] = 7; // 記憶體越界  
  8.     memcpy(ptr +1, ptr, 5); // 踩記憶體  
  9.     free(ptr);   
  10.     free(ptr);// 重複釋放  
  11.     int *p1;  
  12.     *p1 = 1; // 非法指標  
  13. }  
  14. int main(void)  
  15. {  
  16.     test();  
  17.     return 0;  
  18. }  
將程式編譯生成可執行檔案後執行:valgrind --leak-check=full ./程式名

輸出結果如下:

  1. ==4832== Memcheck, a memory error detector  
  2. ==4832== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.  
  3. ==4832== Using Valgrind-3.6.1 and LibVEX; rerun with -h for copyright info  
  4. ==4832== Command: ./tmp  
  5. ==4832==   
  6. ==4832== Invalid write of size 4      // 記憶體越界  
  7. ==4832==    at 0x804843F: test (in /home/yanghao/Desktop/testC/testmem/tmp)  
  8. ==4832==    by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)  
  9. ==4832==  Address 0x41a6050 is 0 bytes after a block of size 40 alloc'd  
  10. ==4832==    at 0x4026864: malloc (vg_replace_malloc.c:236)  
  11. ==4832==    by 0x8048435: test (in /home/yanghao/Desktop/testC/testmem/tmp)  
  12. ==4832==    by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)  
  13. ==4832==   
  14. ==4832== Source and destination overlap in memcpy(0x41a602c, 0x41a6028, 5) // 踩記憶體  
  15. ==4832==    at 0x4027BD6: memcpy (mc_replace_strmem.c:635)  
  16. ==4832==    by 0x8048461: test (in /home/yanghao/Desktop/testC/testmem/tmp)  
  17. ==4832==    by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)  
  18. ==4832==   
  19. ==4832== Invalid free() / delete / delete[] // 重複釋放  
  20. ==4832==    at 0x4025BF0: free (vg_replace_malloc.c:366)  
  21. ==4832==    by 0x8048477: test (in /home/yanghao/Desktop/testC/testmem/tmp)  
  22. ==4832==    by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)  
  23. ==4832==  Address 0x41a6028 is 0 bytes inside a block of size 40 free'd  
  24. ==4832==    at 0x4025BF0: free (vg_replace_malloc.c:366)  
  25. ==4832==    by 0x804846C: test (in /home/yanghao/Desktop/testC/testmem/tmp)  
  26. ==4832==    by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)  
  27. ==4832==   
  28. ==4832== Use of uninitialised value of size 4 // 非法指標  
  29. ==4832==    at 0x804847B: test (in /home/yanghao/Desktop/testC/testmem/tmp)  
  30. ==4832==    by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)  
  31. ==4832==   
  32. ==4832==   
  33. ==4832== Process terminating with default action of signal 11 (SIGSEGV) //由於非法指標賦值導致的程式崩潰  
  34. ==4832==  Bad permissions for mapped region at address 0x419FFF4  
  35. ==4832==    at 0x804847B: test (in /home/yanghao/Desktop/testC/testmem/tmp)  
  36. ==4832==    by 0x804848D: main (in /home/yanghao/Desktop/testC/testmem/tmp)  
  37. ==4832==   
  38. ==4832== HEAP SUMMARY:  
  39. ==4832==     in use at exit: 0 bytes in 0 blocks  
  40. ==4832==   total heap usage: 1 allocs, 2 frees, 40 bytes allocated  
  41. ==4832==   
  42. ==4832== All heap blocks were freed -- no leaks are possible  
  43. ==4832==   
  44. ==4832== For counts of detected and suppressed errors, rerun with: -v  
  45. ==4832== Use --track-origins=yes to see where uninitialised values come from  
  46. ==4832== ERROR SUMMARY: 4 errors from 4 contexts (suppressed: 11 from 6)  
  47. Segmentation fault  

從valgrind的檢測輸出結果看,這幾個錯誤都找了出來。

2.Callgrind

和gprof類似的分析工具,但它對程式的執行觀察更是入微,能給我們提供更多的資訊。和gprof不同,它不需要在編譯原始碼時附加特殊選項,但加上除錯選項是推薦的。Callgrind收集程式執行時的一些資料���建立函式呼叫關係圖,還可以有選擇地進行cache模擬。在執行結束時,它會把分析資料寫入一個檔案。callgrind_annotate可以把這個檔案的內容轉化成可讀的形式。

這是個python指令碼,把它下載之後修改其許可權chmod +7 gprof2dot.py ,並把這個指令碼新增到$PATH路徑中的任一資料夾下,我是將它放到了/usr/bin目錄下,這樣就可以直接在終端下執行gprof2dot.py了。

Callgrind可以生成程式效能分析的圖形,首先來說說程式效能分析的工具吧,通常可以使用gnu自帶的gprof,它的使用方法是:在編譯程式時新增-pg引數,例如:

  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仍然處於實驗階段。

首先舉一個競態的例子吧:

  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: [email protected]@GLIBC_2.1 (createthread.c:256)
==4666==    by 0x4026E2D: pthread_create_WRK (hg_intercepts.c:257)
==4666==    by 0x4026F8B: [email protected]* (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: [email protected]@GLIBC_2.1 (createthread.c:256)
==4666==    by 0x4026E2D: pthread_create_WRK (hg_intercepts.c:257)
==4666==    by 0x4026F8B: [email protected]* (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]

一些常用的選項如下:

選項

作用

-h --help

顯示幫助資訊。

--version

顯示valgrind核心的版本,每個工具都有各自的版本。

-q --quiet

安靜地執行,只打印錯誤資訊。

-v --verbose

列印更詳細的資訊。

--tool=<toolname> [default: memcheck]

最常用的選項。執行valgrind中名為toolname的工具。如果省略工具名,預設執行memcheck。

--db-attach=<yes|no> [default: no]

繫結到偵錯程式上,便於除錯錯誤。

linux