`
灵动的水
  • 浏览: 190906 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

学习使用开源软件google-perftools(分析c/c++程序的占用cpu和内存性能))

 
阅读更多
安装:++++++++++++++

TCMalloc (google-perftools) 是用于优化C++写的多线程应用,比glibc 2.3的malloc快。这个模块可以用来让MySQL在高并发下内存占用更加稳定。

下载二个软件地址:
google-perftools:http://code.google.com/p/google-perftools/

libunwind:http://download.savannah.gnu.org/releases/libunwind/

安装步骤:

1.64 位操作系统请先安装 libunwind库,32位操作系统不要安装。libunwind库为基于64位CPU和操作系统的程序提供了基本的堆栈辗转开解功能,其中包括用于输出堆栈跟踪的API、用于以编程方式辗转开解堆栈的API以及支持C++异常处理机制的API。
#tar zxvf libunwind-0.9.tar.gz
#cd libunwind-0.99

#./configure
#make
#make install

2.安装google-perftools:
#tar zxvf google-perftools-1.6.tar.gz

#cd google-perftools-1.6

#./configure
#make
#make install

3.运行以下二行命令

echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf
/sbin/ldconfig

4.修改MySQL启动脚本(根据你的MySQL安装位置而定):

vi /usr/local/mysql/bin/mysqld_safe
在# executing mysqld_safe的下一行,加上:
export LD_PRELOAD=/usr/local/lib/libtcmalloc.so

保存后退出,然后重启MySQL服务器。


5.使用验证lsof命令查看tcmalloc是否起效:
如lsof没有装,可yum安装
/usr/sbin/lsof -n | grep tcmalloc
如果发现以下信息,说明tcmalloc已经起效:
  mysqld    10847   mysql  mem       REG        8,5  1203756   20484960 /usr/local/lib/libtcmalloc.so.0.0.0



===========================
64位机器安装方法:
tar -zxvf libunwind-0.99-beta.tar.gz
cd libunwind-0.99-beta
CFLAGS=-fPIC ./configure --prefix=/usr
make CFLAGS=-fPIC
make CFLAGS=-fPIC install

tar -zxvf google-perftools-1.8.3.tar.gz
cd google-perftools-1.8.3
./configure --prefix=/usr
make
make install

echo "/usr/lib" > /etc/ld.so.conf.d/usr_lib.conf
/sbin/ldconfig


使用方法:
主函数:

#include <google/profiler.h>
main
{
ProfilerStart("outfile.prof");
int enable= ProfilingIsEnabledForAllThreads();
WriteLog("[CollectAS] Profiling Enabled: %d", enable);

………………………………


ProfilerStop();

}


每个线程添加注册函数:ProfilerRegisterThread();


此第三方软件在/usr 下,程序的makefile.am文件要包含库: -lprofiler

调用:
pprof --dot collect outfile.prof

利用graphviz将outfile.do生成图纸


=====================================

假设你已经安装好了这个开源工具

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格式就可以查看函数调用关系了
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics