gperftools的使用

yum install libtool (生成makefile用)

yum install graphviz (生成pdf用)

./configure && make -j8 && make install
./configure && make -j8 && make install
如果不安装libunwind库,就在安装gperftools的时候 enable-frame-pointers, 
并且在编译应用程序的时候加上 -fno-omit-frame-pointer
echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf
ldconfig
包含头文件:#include <gperftools/profiler.h>
开始检测:ProfilerStart("fileName.prof ");
线程入口:ProfilerRegisterThread();
停止检测:ProfilerStop();
依赖两个库:-lprofiler -lunwind
pprof --pdf ./test  fileName.prof > test.pdf

非侵入式使用方法:
pprof可以在不修改源码的情况下,通过在编译时链接profiler库,程序启动前设置特定的环境变量来开启CPU剖析功能。
在程序编译时链接 profiler
编写启动脚本:
#!/bin/sh
echo "start CPU profile"
env CPUPROFILE=example.prof ./example
exit 1
编写展示脚本:
echo "display  CPU profile in pdf"
pprof --pdf ./ example example.prof > example.pdf
exit 1

注意:
gperftools需要程序正常退出才能向prof文件打印数据,所以当程序无法退出时,得要发送信号给进程,
在接到信号后,调用ProfilerStop();函数,才能打印出数据。如下所示:
void signal_handler(int signo)
{
    signal(signo, signal_handler);
    INFO_LOG("recv signal[%d]", signo);
    switch(signo)
    {      
        case SIGTERM:
             //程序自己退出,或shell里调用kill缺省该进程。该信号可以被阻塞,或被处理
             //可以在这里做一些程序退出前的最后处理工作
             ProfilerStop();
             INFO_LOG("Process recieve SIGTERM");
             break;      
    }
    exit(0);
}
//主函数
signal(SIGTERM, &signal_handler);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值