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

相关文章

SpringBoot线程池配置使用示例详解

《SpringBoot线程池配置使用示例详解》SpringBoot集成@Async注解,支持线程池参数配置(核心数、队列容量、拒绝策略等)及生命周期管理,结合监控与任务装饰器,提升异步处理效率与系统... 目录一、核心特性二、添加依赖三、参数详解四、配置线程池五、应用实践代码说明拒绝策略(Rejected

C++ Log4cpp跨平台日志库的使用小结

《C++Log4cpp跨平台日志库的使用小结》Log4cpp是c++类库,本文详细介绍了C++日志库log4cpp的使用方法,及设置日志输出格式和优先级,具有一定的参考价值,感兴趣的可以了解一下... 目录一、介绍1. log4cpp的日志方式2.设置日志输出的格式3. 设置日志的输出优先级二、Window

Ubuntu如何分配​​未使用的空间

《Ubuntu如何分配​​未使用的空间》Ubuntu磁盘空间不足,实际未分配空间8.2G因LVM卷组名称格式差异(双破折号误写)导致无法扩展,确认正确卷组名后,使用lvextend和resize2fs... 目录1:原因2:操作3:报错5:解决问题:确认卷组名称​6:再次操作7:验证扩展是否成功8:问题已解

Qt使用QSqlDatabase连接MySQL实现增删改查功能

《Qt使用QSqlDatabase连接MySQL实现增删改查功能》这篇文章主要为大家详细介绍了Qt如何使用QSqlDatabase连接MySQL实现增删改查功能,文中的示例代码讲解详细,感兴趣的小伙伴... 目录一、创建数据表二、连接mysql数据库三、封装成一个完整的轻量级 ORM 风格类3.1 表结构

使用Docker构建Python Flask程序的详细教程

《使用Docker构建PythonFlask程序的详细教程》在当今的软件开发领域,容器化技术正变得越来越流行,而Docker无疑是其中的佼佼者,本文我们就来聊聊如何使用Docker构建一个简单的Py... 目录引言一、准备工作二、创建 Flask 应用程序三、创建 dockerfile四、构建 Docker

Python使用vllm处理多模态数据的预处理技巧

《Python使用vllm处理多模态数据的预处理技巧》本文深入探讨了在Python环境下使用vLLM处理多模态数据的预处理技巧,我们将从基础概念出发,详细讲解文本、图像、音频等多模态数据的预处理方法,... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核

Python使用pip工具实现包自动更新的多种方法

《Python使用pip工具实现包自动更新的多种方法》本文深入探讨了使用Python的pip工具实现包自动更新的各种方法和技术,我们将从基础概念开始,逐步介绍手动更新方法、自动化脚本编写、结合CI/C... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核

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. 从字符串创建