gperftools的安装使用说明

一、介绍

gperftools是Google推出一个非常强大的性能分析工具集,在以前使用时进行过文档的整理,但时过境迁,这里又有了一些不同,正好实际工程用到它,对其的安装和使用说明进行一次完整的文档化。
gperftools主要包含三个功能:
1、分析 CPU 性能
能够通过统计一定时间内各个功能单元(线程、函数等)的执行时间并给出其占用比例,从而确定CPU瓶颈
2、分析内存占用
统计单位时间内各单元对内存的占用数量并查找是否有内存泄露
3、自动检查内存泄露

二、安装前准备

安装gperftools之前,需要准备一些基本的运行库,否则将无法执行相关性能分析工具:
1、安装libtool

sudo apt update
sudo apt install libtool -y

2、安装PKG(如果已安装可省略此步)

sudo apt install pkg-config

3、安装libunwind

sudo apt -y install libunwind-12(Debian:libunwind-dev)

如果有遇到其它情况需要安装相关软件的,可以根据实际情况安装即可。

三、安装

1、源码安装
下载代码

git clone https://github.com/gperftools/gperftools.git
编译
cd gperftools
./autogen.sh
./configure
make -j
安装
sudo make install
刷新动态库
sudo ldconfig

注意:如果没进行安装前准备,可能会报各种错误,可根据错误安装相关依赖库即可

2、安装相关工具

sudo apt install google-perftools

测试:
终端内输入pprof命令,回车,如果出现命令选项即正确安装成功

四、说明

上述的安装只针对Ubuntu和Debian ,其它系统仅供参考。

五、基本应用

gperftools的应用有两种方式,一种是侵入式的,一种是非侵入式的,可以根据实际情况来进行应用。而其使用又可分为三类:

To install the CPU profiler into your executable, add -lprofiler to the link-time step for your executable. (It's also probably possible to add in the profiler at run-time using LD_PRELOAD, e.g. % env LD_PRELOAD="/usr/lib/libprofiler.so" <binary>, but this isn't necessarily recommended.)

This does not turn on CPU profiling; it just inserts the code. For that reason, it's practical to just always link -lprofiler into a binary while developing; that's what we do at Google. (However, since any user can turn on the profiler by setting an environment variable, it's not necessarily recommended to install profiler-linked binaries into a production, running system.)
There are several alternatives to actually turn on CPU profiling for a given run of an executable:

Define the environment variable CPUPROFILE to the filename to dump the profile to. For instance, if you had a version of /bin/ls that had been linked against libprofiler, you could run:

1、% env CPUPROFILE=ls.prof /bin/ls
In addition to defining the environment variable CPUPROFILE you can also define CPUPROFILESIGNAL. This allows profiling to be controlled via the signal number that you specify. The signal number must be unused by the program under normal operation. Internally it acts as a switch, triggered by the signal, which is off by default. For instance, if you had a copy of /bin/chrome that had been been linked against libprofiler, you could run:

2、% env CPUPROFILE=chrome.prof CPUPROFILESIGNAL=12 /bin/chrome &
You can then trigger profiling to start:

% killall -12 chrome
Then after a period of time you can tell it to stop which will generate the profile:

% killall -12 chrome
3、In your code, bracket the code you want profiled in calls to ProfilerStart() and ProfilerStop(). (These functions are declared in <gperftools/profiler.h>.) ProfilerStart() will take the profile-filename as an argument.

如果将两种方式总结一下即为:
1、使用定义LD_PRELOAD和CPUPROFILE的非侵入方式,用于可可执行完成的程序。
2、使用ProfilerStart等函数的侵入式,针对具体细节单元进行分析或者非可停服务器系统进行测试等。
可根据实际情况进行选择。

六、非侵入例程

#include <iostream>
void test(){
	long long  sum = 0;
for (int num = 0;num < 1000000000;num++){
sum += num;
	//std::cout<<"cur num id:"<<num<<std::endl;
}
std::cout<<"cur sum:"<<sum<<std::endl;
}
int main(){
	test();
	std::cout<<"this is test!"<<std::endl;
return 0;
}

编译:

g++ -o main main.cpp -lprofiler

应用:

env LD_PRELOAD=/usr/local/lib/libprofiler.so.0 CPUPROFILE=./main.prof ./main

使用命令分析:

pprof --pdf  ./main main.prof >tmp2.pdf

注意:在编译时,官方文档说可以使用-lprofiler代替LD_PRELOAD,但实际测试没有成功。

七、侵入例程

#include <google/profiler.h>
#include <iostream>
#include <unistd.h>
#include <signal.h>
using namespace std;

void t1()
{
    int i = 0;
    while (i < 1000)
    {
        i++;
    }
}

void t2()
{
    int i = 0;
    while (i < 2000)
    {
        i++;
    }
}


void t3()
{
    for (int i = 0; i < 100000000; ++i)
    {
        t1();
        t2();
        std::cout<<"once t3"<<std::endl;
        //sleep(1);
        }
}


int bQuit = false;
static void setGperfSig(int num)
{
    static bool open = false;
    if (num != SIGUSR1) {
        return;
    }

    if (!open)
    {
        open = true;
        ProfilerStart("sigtest.prof");
        std::cout << "ProfilerStart OK" << std::endl;
    }
    else
    {
        open = false;
        std::cout << "ProfilrerStop OK" << std::endl;
        ProfilerStop();
        bQuit = true;
  }
}

int main(int argc, const char* argv[])
{
  signal(SIGUSR1, setGperfSig);
  int click = 0;

  while (!bQuit)
  {
      t3();
      std::cout << "second data is:" << ++click << std::endl;
      sleep(1);
  }

   return 0;
}


编译:

$ g++ -o sigtest sigtest.cpp -lprofiler -lunwind
//如果需要,设置一下库的路径,请根据实际情况设置
export LD_LIBRARY_PATH=/usr/local/lib
export PATH=$PATH:/usr/local/bin

启动测试:

//启动程序
$ ./sigtest
//显示进程ID
$ ps -ef|grep sigtest
fpc         4838    3202 80 10:44 pts/0    00:00:04 ./sigtest
fpc         4843    2213  0 10:44 pts/1    00:00:00 grep --color=auto sigtest
//发送启动测试信号
$ kill -s SIGUSR1 4838
//间隔一段时间再次发送
f:~/testtool$ kill -s SIGUSR1 4838

使用gperftools命令分析:

$ pprof --text sigtest sigtest.prof
Using local file sigtest.
Using local file sigtest.prof.
Total: 558 samples
     433  77.6%  77.6%      433  77.6% __write
      80  14.3%  91.9%       80  14.3% t2
      43   7.7%  99.6%       43   7.7% t1
       1   0.2%  99.8%        1   0.2% __nss_database_lookup
       1   0.2% 100.0%        1   0.2% fflush
       0   0.0% 100.0%      433  77.6% _IO_do_write
       0   0.0% 100.0%      433  77.6% _IO_file_overflow
       0   0.0% 100.0%      433  77.6% _IO_file_write
       0   0.0% 100.0%      558 100.0% __libc_start_main
       0   0.0% 100.0%      558 100.0% _start
       0   0.0% 100.0%      558 100.0% main
       0   0.0% 100.0%      433  77.6% std::endl
       0   0.0% 100.0%        1   0.2% std::operator<<
       0   0.0% 100.0%        1   0.2% std::ostream::flush
       0   0.0% 100.0%      433  77.6% std::ostream::put
       0   0.0% 100.0%      558 100.0% t3


八、命令分析和说明

下面将对本次测试使用的几个主要分析命令进行说明,更多的命令细节可参看官方网站(见第五节)。
1、pprof命令

pprof [option]  bin bin.prof > [file type](abc.pdf)

这个命令用于分析生成的prof文件,主要有几个参数,–text,–pdf,–web

2、显示调用图
常用的是过滤和忽略(Focus and Ignore)

focus:
pprof --text  ./runexe my.prof_4854 --focus=FocusFunction

ignore:
pprof --text  ./runexe my.prof_4854 --ignore=FocusFunction

其它还有:- -nodecount(显示节点数控制),–nodefraction(丢弃节点),–edgefraction(控制边缘的显示)

3、生成报告的粒度
一般常用是- -functions,其它还有- -lines,- -files,- -addresses主要是用来成节点。

4、Callgrind的使用
这个用法很简单,但一直没有安装成功相关的库,暂时忽略。

5、举例:
以实际工程测试为例 ,生成PROF文件后:

pprof --text  ./runexe my.prof_4854 --focus=FocusFunction
pprof --pdf  ./runexe my.prof_4854 --focus=FocusFunction>tmp2.pdf

–focus=FocusFunction:可以不使用,此为过滤某个函数的条件

九、总结

具体的图就不贴了,基本和原来的差不多。还是要多看一些相关的文档,这才是使用工具的标准打法。工具用好了,能起到事半功倍的效果,俗话说的好:“工欲善其事,必先利其器”。

  • 24
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值