google-perftools的用法
假设你已经安装好了这个开源工具
1) vim example.cpp
#include<iostream>
#include<stdlib.h>
//#include<google/profiler.h>
#include<stdio.h>
using namespace std;
void consumeSomeCPUTime1(int input){
int i = 0;
input++;
while(i++ < 10000){
i--; i++; i--; i++;
}
};
void consumeSomeCPUTime2(int input){
input++;
consumeSomeCPUTime1(input);
int i = 0;
while(i++ < 10000){
i--; i++; i--; i++;
}
};
int stupidComputing(int a, int b){
int i = 0;
while( i++ < 10000){
consumeSomeCPUTime1(i);
}
int j = 0;
while(j++ < 5000){
consumeSomeCPUTime2(j);
}
return a+b;
};
int smartComputing(int a, int b){
return a+b;
};
int main(){
int i = 0;
printf("reached the start point of performance bottle neck\n");
sleep(1);
// ProfilerStart("CPUProfile.prof");
while( i++ < 10){
printf("Stupid computing return : %d\n",stupidComputing(i, i+1));
printf("Smart computing return %d\n",smartComputing(i+1, i+2));
}
printf("should teminate profiling now.\n");
sleep(1);
// ProfilerStop();
return 0;
}
2)设置 LD_LIBRARY_PATH就是你的pprof的安装路径下的lib
3)设置CPUPROFILE的值如:CPUPROFILE=cpu;
这里还可以再源文件里面加入被注释掉的语句来设置cpuprofile输出的结果
如果是多线程的话就需要使用ProfilerRegisterThread();这样在每个线程执行的时候就注册了一次
4)编译g++ -g example.cpp -L/usr/local/lib/ -lprofiler -o example
5)执行example就会生成cpu文件
6)查看结果 pprof --text profile cpu>>t1.txt 这里是以text的格式查看函数调用关系 和耗时;也可以用--dot的格式查看;然后再将其转换成gif格式就可以看调用关系图:pprof --dot profile cpu>t1.dot
7)将dot文件下载到本地然后用Graphviz软件转换为gif格式就可以查看函数调用关系了
| /* This demo case is write by yulong.yhj@taobao.com |
| * If you have any question, please don't hesitate to contact me. |
| * You also can contact me with ÍúÍú: ÓñÁú |
| * Compile the Demo: |
| I find if we modify the code and call ProfilerRegisterThread(); for each thread. It can capture profiling samples for each thread. |
| LD_LIBRARY_PATH=/home/admin/google-perftools/lib g++ testvar.cpp -o testvar -lpthread -lprofiler -L/home/admin/google-perftools/lib -I/home/admin/google-perftools/include |
| * "/home/admin/google-perftools/lib" is the install folder of google perftools and libunwind libary. |
| * Run the demo with google CPU Profile |
| LD_LIBRARY_PATH=/home/admin/google-perftools/lib ./testvar |
| * |
| * |
| * |
| */ |
| #include <stdio.h> |
| #include <signal.h> |
| #include <unistd.h> |
| #include <iostream.h> |
| #include <pthread.h> |
| #include<sys/types.h> |
| #include <time.h> |
| #include <sys/time.h> |
| #include <google/profiler.h> |
| int gvar = 1000; |
| static void sig_hook(int sig) { |
| if( sig == SIGINT || sig == SIGTERM ) { |
| cout<<"---------------STOPPING----------------"<<endl; |
| //kill(getpid(), SIGKILL); |
| exit(1); |
| } |
| } |
| void calcA(){ |
| long i=1; |
| while(i<1000000){ |
| int a =333; |
| int b=777; |
| int c =0; |
| c =a * b; |
| i++; |
| } |
| } |
| void calcB(){ |
| static int a =0; |
| long i=1; |
| while(i<1000000){ |
| int a =333; |
| int b=777; |
| int c =0; |
| c =a * b; |
| i++; |
| } |
| if(a==100)sleep(3); |
| a++; |
| } |
| void calcC(){ |
| //static int a = 0; |
| long i=1; |
| while(i<1000000){ |
| int a =333; |
| int b=777; |
| int c =0; |
| c =a * b; |
| i++; |
| |
| } |
| //if(a==100)sleep(3); |
| //a++; |
| } |
| void calcA2(){ |
| //static int a = 0; |
| long i=1; |
| while(i<1000000){ |
| int a =333; |
| int b=777; |
| int c =0; |
| c =a * b; |
| i++; |
| } |
| //if(a==100)sleep(3); |
| //a++; |
| } |
| void * threadA(void *){ |
| ProfilerRegisterThread(); |
| while(true){ |
| calcA(); |
| } |
| } |
| void * threadA2(void *){ |
| ProfilerRegisterThread(); |
| while(true){ |
| calcA2(); |
| } |
| } |
| void * threadB(void *){ |
| ProfilerRegisterThread(); |
| while(true){ |
| calcB(); |
| } |
| } |
| int main(int argc, char * argv[]) { |
| ProfilerStart("gvar.prof") ; |
| int enable= ProfilingIsEnabledForAllThreads(); |
| cout<<"Profiling Enabled:"<<enable<<endl; |
| cout<<"GVar value: "<< gvar<<endl; |
| long start = time(0); |
| struct timeval tvstart; |
| struct timeval tvend; |
| gettimeofday(&tvstart, NULL); |
| signal(SIGINT, sig_hook); |
| signal(SIGTERM, sig_hook); |
| pthread_t id1; |
| pthread_t id2; |
| pthread_t id3; |
| pthread_create(&id1,NULL, threadA,NULL); |
| pthread_create(&id2,NULL, threadB,NULL); |
| pthread_create(&id3,NULL, threadA2, NULL); |
| long i=0; |
| while(i<2000){ |
| calcC(); |
| i++; |
| } |
| long end = time(0); |
| gettimeofday(&tvend, NULL); |
| long rt = (tvend.tv_usec - tvstart.tv_usec)/1000+(tvend.tv_sec-tvstart.tv_sec)*1000; |
| cout<<"Total Time: "<<rt<<" ms"<<endl; |
| cout<<"Finished Main Thread"<<endl; |
| ProfilerStop(); |
| } |
浙公网安备 33010602011771号