wget http://download.savannah.gnu.org/releases/libunwind/libunwind-0.99-beta.tar.gz tar -zxvf libunwind-0.99-beta.tar.gz cd libunwind-0.99-beta/ ./configure make make install
git clone https://github.com/gperftools/gperftools.git cd gperftools/ ./autogen.sh ./configure make make install
遇到的问题1:
1 2 3 4 5
[root@10-8-152-53 gperftools]# ./autogen.sh configure.ac:174: error: possibly undefined macro: AC_PROG_LIBTOOL If this token and others are legitimate, please use m4_pattern_allow. See the Autoconf documentation. autoreconf: /usr/bin/autoconf failed with exit status:
#include<gperftools/profiler.h> #include<iostream> usingnamespace std; voidfunc1(){ int i = 0; while (i < 100000) { ++i; } } voidfunc2(){ int i = 0; while (i < 200000) { ++i; } } voidfunc3(){ for (int i = 0; i < 1000; ++i) { func1(); func2(); } } intmain(){ func3(); return0; }
#include<gperftools/profiler.h> #include<iostream> usingnamespace std; voidfunc1(){ int i = 0; while (i < 100000) { ++i; } } voidfunc2(){ int i = 0; while (i < 200000) { ++i; } } voidfunc3(){ for (int i = 0; i < 1000; ++i) { func1(); func2(); } } intmain(){ ProfilerStart("test.prof"); // 指定所生成的profile文件名 func3(); ProfilerStop(); // 结束profiling return0; }
voidfunc1(){ int i = 0; while (i < 100000) { ++i; } } voidfunc2(){ int i = 0; while (i < 200000) { ++i; } } voidfunc3(){ for (int i = 0; i < 1000; ++i) { func1(); func2(); } } intmain(){ signal(SIGUSR1, gprofStartAndStop);
voidf1() { int i; for (i=0; i<1024*1024; ++i) { int* p2 = newint; //delete[] p2; } }
voidf2() { int i; for (i=0; i<1024*1024; ++i) { int* p2 = newint; //delete[] p2; } }
intmain(){ f1(); f2(); return0; }
编译运行如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
[root@10-8-152-53 heapprofilertest]# g++ test.cpp -ltcmalloc [root@10-8-152-53 heapprofilertest]# env HEAPPROFILE=./test_heap.prof ./a.out Starting tracking the heap Dumping heap profile to ./test_heap.prof.0001.heap (Exiting, 8 MB in use) [root@10-8-152-53 heapprofilertest]# pprof --text ./a.out ./test_heap.prof.0001.heap Using local file ./a.out. Using local file ./test_heap.prof.0001.heap. Total: 8.0 MB 4.050.0% 50.0% 4.050.0% f1 4.050.0% 100.0% 4.050.0% f2 0.00.0% 100.0% 8.0100.0% __libc_start_main 0.00.0% 100.0% 8.0100.0% _start 0.00.0% 100.0% 8.0100.0% main [root@10-8-152-53 heapprofilertest]# pprof --pdf ./a.out ./test_heap.prof.0001.heap > ./test_heap.pdf Using local file ./a.out. Using local file ./test_heap.prof.0001.heap. Dropping nodes with <= 0.0 MB; edges with <= 0.0abs(MB) [root@10-8-152-53 heapprofilertest]# ls a.out test.cpp test_heap.pdf test_heap.prof.0001.heap
在生成heap的过程中,还有另外的一些属性可以设置[2]:
HEAP_PROFILE_ALLOCATION_INTERVAL
default: 1073741824 (1 Gb)
Dump heap profiling information each time the specified number of bytes has been allocated by the program.
HEAP_PROFILE_INUSE_INTERVAL
default: 104857600 (100 Mb)
Dump heap profiling information whenever the high-water memory usage mark increases by the specified number of bytes.
HEAP_PROFILE_TIME_INTERVAL
default: 0
Dump heap profiling information each time the specified number of seconds has elapsed.
HEAPPROFILESIGNAL
default: disabled
Dump heap profiling information whenever the specified signal is sent to the process.
HEAP_PROFILE_MMAP
default: false
Profile mmap, mremap and sbrk calls in addition to malloc, calloc, realloc, and new. NOTE: this causes the profiler to profile calls internal to tcmalloc, since tcmalloc and friends use mmap and sbrk internally for allocations. One partial solution is to filter these allocations out when running pprof, with something like `pprof –ignore=’DoAllocWithArena
HEAP_PROFILE_ONLY_MMAP
default: false
Only profile mmap, mremap, and sbrk calls; do not profile malloc, calloc, realloc, or new.