gprof這個常用的性能工具,用來性能調優很方便。但是!!有個致命的缺點,不能處理動態鏈接庫(dlopen()加載的)。sigh…
那遇到動態鏈接庫怎么調優呢,用這個工具callgrind。
同樣是valgrind工具集中的一個,使用也是同樣方便。gcc帶上-g參數,然后用callgrind運行!
同樣取上一個程序:
#include <stdio.h> #include <stdlib.h> void f1() { int i; int *p; for (i = 0; i < 10; i++) { p = malloc(sizeof(int)); *p = 10; free(p); } } void f2() { int i; int *p; for (i = 0; i < 20; i++) { p = malloc(sizeof(int)); *p = 10; free(p); } } void f3() { int i; int *p; for (i = 0; i < 30; i++) { p = malloc(sizeof(int)); *p = 10; free(p); } } int main() { int i; for (i = 0; i < 1000000; i++) { f1(); f2(); f3(); } return 0; }
編譯運行:
gcc -o test -g test.c valgrind --tool=callgrind ./test
要有耐心,上了callgrind運行的程序會非常的慢(理解萬歲)。
運行完之后文件夾下出現了callgrind.out.***便是日志文件。更massif一樣,需要用工具轉化:
callgrind_annotate callgrind.out.438
結果如下:
-------------------------------------------------------------------------------- Profile data file 'callgrind.out.438' (creator: callgrind-3.5.0) -------------------------------------------------------------------------------- I1 cache: D1 cache: L2 cache: Timerange: Basic block 0 - 977300000 Trigger: Program termination Profiled target: ./test (PID 438, part 1) Events recorded: Ir Events shown: Ir Event sort order: Ir Thresholds: 99 Include dirs: User annotated: Auto-annotation: off -------------------------------------------------------------------------------- Ir -------------------------------------------------------------------------------- 4,136,177,734 PROGRAM TOTALS -------------------------------------------------------------------------------- Ir file:function -------------------------------------------------------------------------------- 1,353,303,232 ???:_int_free [/lib64/libc-2.12.90.so] 1,162,995,238 ???:_int_malloc [/lib64/libc-2.12.90.so] 972,686,762 ???:malloc [/lib64/libc-2.12.90.so] 401,761,897 ???:free [/lib64/libc-2.12.90.so] 119,471,210 test.c:f3 [/home/dccmx/projects/console/test] 80,704,867 test.c:f2 [/home/dccmx/projects/console/test] 41,938,337 test.c:f1 [/home/dccmx/projects/console/test]
有沒有更給力的圖形前端呢?有,將callgrind.out.***用kcachegrind打開看看:

