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

相关文章

Conda与Python venv虚拟环境的区别与使用方法详解

《Conda与Pythonvenv虚拟环境的区别与使用方法详解》随着Python社区的成长,虚拟环境的概念和技术也在不断发展,:本文主要介绍Conda与Pythonvenv虚拟环境的区别与使用... 目录前言一、Conda 与 python venv 的核心区别1. Conda 的特点2. Python v

Spring Boot中WebSocket常用使用方法详解

《SpringBoot中WebSocket常用使用方法详解》本文从WebSocket的基础概念出发,详细介绍了SpringBoot集成WebSocket的步骤,并重点讲解了常用的使用方法,包括简单消... 目录一、WebSocket基础概念1.1 什么是WebSocket1.2 WebSocket与HTTP

C#中Guid类使用小结

《C#中Guid类使用小结》本文主要介绍了C#中Guid类用于生成和操作128位的唯一标识符,用于数据库主键及分布式系统,支持通过NewGuid、Parse等方法生成,感兴趣的可以了解一下... 目录前言一、什么是 Guid二、生成 Guid1. 使用 Guid.NewGuid() 方法2. 从字符串创建

Python使用python-can实现合并BLF文件

《Python使用python-can实现合并BLF文件》python-can库是Python生态中专注于CAN总线通信与数据处理的强大工具,本文将使用python-can为BLF文件合并提供高效灵活... 目录一、python-can 库:CAN 数据处理的利器二、BLF 文件合并核心代码解析1. 基础合

Python使用OpenCV实现获取视频时长的小工具

《Python使用OpenCV实现获取视频时长的小工具》在处理视频数据时,获取视频的时长是一项常见且基础的需求,本文将详细介绍如何使用Python和OpenCV获取视频时长,并对每一行代码进行深入解析... 目录一、代码实现二、代码解析1. 导入 OpenCV 库2. 定义获取视频时长的函数3. 打开视频文

Go语言中make和new的区别及说明

《Go语言中make和new的区别及说明》:本文主要介绍Go语言中make和new的区别及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1 概述2 new 函数2.1 功能2.2 语法2.3 初始化案例3 make 函数3.1 功能3.2 语法3.3 初始化

Spring IoC 容器的使用详解(最新整理)

《SpringIoC容器的使用详解(最新整理)》文章介绍了Spring框架中的应用分层思想与IoC容器原理,通过分层解耦业务逻辑、数据访问等模块,IoC容器利用@Component注解管理Bean... 目录1. 应用分层2. IoC 的介绍3. IoC 容器的使用3.1. bean 的存储3.2. 方法注

Python内置函数之classmethod函数使用详解

《Python内置函数之classmethod函数使用详解》:本文主要介绍Python内置函数之classmethod函数使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 类方法定义与基本语法2. 类方法 vs 实例方法 vs 静态方法3. 核心特性与用法(1编程客

Linux中压缩、网络传输与系统监控工具的使用完整指南

《Linux中压缩、网络传输与系统监控工具的使用完整指南》在Linux系统管理中,压缩与传输工具是数据备份和远程协作的桥梁,而系统监控工具则是保障服务器稳定运行的眼睛,下面小编就来和大家详细介绍一下它... 目录引言一、压缩与解压:数据存储与传输的优化核心1. zip/unzip:通用压缩格式的便捷操作2.

java中新生代和老生代的关系说明

《java中新生代和老生代的关系说明》:本文主要介绍java中新生代和老生代的关系说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、内存区域划分新生代老年代二、对象生命周期与晋升流程三、新生代与老年代的协作机制1. 跨代引用处理2. 动态年龄判定3. 空间分