gperftools的安装使用说明

2024-03-08 22:52

本文主要是介绍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 samples433  77.6%  77.6%      433  77.6% __write80  14.3%  91.9%       80  14.3% t243   7.7%  99.6%       43   7.7% t11   0.2%  99.8%        1   0.2% __nss_database_lookup1   0.2% 100.0%        1   0.2% fflush0   0.0% 100.0%      433  77.6% _IO_do_write0   0.0% 100.0%      433  77.6% _IO_file_overflow0   0.0% 100.0%      433  77.6% _IO_file_write0   0.0% 100.0%      558 100.0% __libc_start_main0   0.0% 100.0%      558 100.0% _start0   0.0% 100.0%      558 100.0% main0   0.0% 100.0%      433  77.6% std::endl0   0.0% 100.0%        1   0.2% std::operator<<0   0.0% 100.0%        1   0.2% std::ostream::flush0   0.0% 100.0%      433  77.6% std::ostream::put0   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=FocusFunctionignore:
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:可以不使用,此为过滤某个函数的条件

九、总结

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

这篇关于gperftools的安装使用说明的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/788671

相关文章

使用Python实现可恢复式多线程下载器

《使用Python实现可恢复式多线程下载器》在数字时代,大文件下载已成为日常操作,本文将手把手教你用Python打造专业级下载器,实现断点续传,多线程加速,速度限制等功能,感兴趣的小伙伴可以了解下... 目录一、智能续传:从崩溃边缘抢救进度二、多线程加速:榨干网络带宽三、速度控制:做网络的好邻居四、终端交互

Python中注释使用方法举例详解

《Python中注释使用方法举例详解》在Python编程语言中注释是必不可少的一部分,它有助于提高代码的可读性和维护性,:本文主要介绍Python中注释使用方法的相关资料,需要的朋友可以参考下... 目录一、前言二、什么是注释?示例:三、单行注释语法:以 China编程# 开头,后面的内容为注释内容示例:示例:四

Python中win32包的安装及常见用途介绍

《Python中win32包的安装及常见用途介绍》在Windows环境下,PythonWin32模块通常随Python安装包一起安装,:本文主要介绍Python中win32包的安装及常见用途的相关... 目录前言主要组件安装方法常见用途1. 操作Windows注册表2. 操作Windows服务3. 窗口操作

Go语言数据库编程GORM 的基本使用详解

《Go语言数据库编程GORM的基本使用详解》GORM是Go语言流行的ORM框架,封装database/sql,支持自动迁移、关联、事务等,提供CRUD、条件查询、钩子函数、日志等功能,简化数据库操作... 目录一、安装与初始化1. 安装 GORM 及数据库驱动2. 建立数据库连接二、定义模型结构体三、自动迁

MySQL之InnoDB存储引擎中的索引用法及说明

《MySQL之InnoDB存储引擎中的索引用法及说明》:本文主要介绍MySQL之InnoDB存储引擎中的索引用法及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录1、背景2、准备3、正篇【1】存储用户记录的数据页【2】存储目录项记录的数据页【3】聚簇索引【4】二

mysql中的数据目录用法及说明

《mysql中的数据目录用法及说明》:本文主要介绍mysql中的数据目录用法及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、背景2、版本3、数据目录4、总结1、背景安装mysql之后,在安装目录下会有一个data目录,我们创建的数据库、创建的表、插入的

ModelMapper基本使用和常见场景示例详解

《ModelMapper基本使用和常见场景示例详解》ModelMapper是Java对象映射库,支持自动映射、自定义规则、集合转换及高级配置(如匹配策略、转换器),可集成SpringBoot,减少样板... 目录1. 添加依赖2. 基本用法示例:简单对象映射3. 自定义映射规则4. 集合映射5. 高级配置匹

Spring 框架之Springfox使用详解

《Spring框架之Springfox使用详解》Springfox是Spring框架的API文档工具,集成Swagger规范,自动生成文档并支持多语言/版本,模块化设计便于扩展,但存在版本兼容性、性... 目录核心功能工作原理模块化设计使用示例注意事项优缺点优点缺点总结适用场景建议总结Springfox 是

嵌入式数据库SQLite 3配置使用讲解

《嵌入式数据库SQLite3配置使用讲解》本文强调嵌入式项目中SQLite3数据库的重要性,因其零配置、轻量级、跨平台及事务处理特性,可保障数据溯源与责任明确,详细讲解安装配置、基础语法及SQLit... 目录0、惨痛教训1、SQLite3环境配置(1)、下载安装SQLite库(2)、解压下载的文件(3)、

使用Python绘制3D堆叠条形图全解析

《使用Python绘制3D堆叠条形图全解析》在数据可视化的工具箱里,3D图表总能带来眼前一亮的效果,本文就来和大家聊聊如何使用Python实现绘制3D堆叠条形图,感兴趣的小伙伴可以了解下... 目录为什么选择 3D 堆叠条形图代码实现:从数据到 3D 世界的搭建核心代码逐行解析细节优化应用场景:3D 堆叠图