1. 下载 gperftools (wget http://code.google.com/p/gperftools/downloads/list/gperftools-2.0.tar.gz)
mkdir ../gperftools
./configure prefix=/home/tools/gperftools
make && make install
2. 下载libunwind (wget http://download.savannah.gnu.org/releases/libunwind/libunwind-1.1.tar.gz)
mkdir ../libunwind
./configure prefix=/home/tools/libunwind
make && make install
3. export LD_LIBRARY_PATH=/home/tools/gperftools/lib
export PATH=$PATH:/home/tools/gperftools/bin
4. mkdir ../test && cd ../test
vi test.cpp
#include <google/profiler.h>
#include <iostream>
using namespace std;
void test1()
{
int i = 0;
while (i < 1000)
{
i++;
}
}
void test2()
{
int i = 0;
while (i < 2000)
{
i++;
}
}
void test3()
{
for (int i = 0; i < 100000; ++i)
{
test1();
test2();
}
}
int main()
{
ProfilerStart("test.prof"); // test.prof is the name of profile file
test3();
printf("Finish!");
ProfilerStop();
return 0;
}
5. 编译
g++ -o test test.cpp -I /home/tools/gperftools/include -I /home/tools/libunwind/include -L/home/tools/gperftools/lib/ -lprofiler -L/home/tools/libunwind/lib/ -lunwind
6. ./test
生成test.prof文件
7. pprof --text test test.prof
输出:
Using local file test.
Using local file test.prof.
Total: 100 samples
59 59.0% 59.0% 59 59.0% test2
40 40.0% 99.0% 40 40.0% test1
1 1.0% 100.0% 1 1.0% test3
官方文档:http://code.google.com/p/google-perftools/wiki/GooglePerformanceTools