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

相关文章

使用animation.css库快速实现CSS3旋转动画效果

《使用animation.css库快速实现CSS3旋转动画效果》随着Web技术的不断发展,动画效果已经成为了网页设计中不可或缺的一部分,本文将深入探讨animation.css的工作原理,如何使用以及... 目录1. css3动画技术简介2. animation.css库介绍2.1 animation.cs

使用雪花算法产生id导致前端精度缺失问题解决方案

《使用雪花算法产生id导致前端精度缺失问题解决方案》雪花算法由Twitter提出,设计目的是生成唯一的、递增的ID,下面:本文主要介绍使用雪花算法产生id导致前端精度缺失问题的解决方案,文中通过代... 目录一、问题根源二、解决方案1. 全局配置Jackson序列化规则2. 实体类必须使用Long封装类3.

Python文件操作与IO流的使用方式

《Python文件操作与IO流的使用方式》:本文主要介绍Python文件操作与IO流的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、python文件操作基础1. 打开文件2. 关闭文件二、文件读写操作1.www.chinasem.cn 读取文件2. 写

PyQt6中QMainWindow组件的使用详解

《PyQt6中QMainWindow组件的使用详解》QMainWindow是PyQt6中用于构建桌面应用程序的基础组件,本文主要介绍了PyQt6中QMainWindow组件的使用,具有一定的参考价值,... 目录1. QMainWindow 组php件概述2. 使用 QMainWindow3. QMainW

使用Python自动化生成PPT并结合LLM生成内容的代码解析

《使用Python自动化生成PPT并结合LLM生成内容的代码解析》PowerPoint是常用的文档工具,但手动设计和排版耗时耗力,本文将展示如何通过Python自动化提取PPT样式并生成新PPT,同时... 目录核心代码解析1. 提取 PPT 样式到 jsON关键步骤:代码片段:2. 应用 JSON 样式到

java变量内存中存储的使用方式

《java变量内存中存储的使用方式》:本文主要介绍java变量内存中存储的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、介绍2、变量的定义3、 变量的类型4、 变量的作用域5、 内存中的存储方式总结1、介绍在 Java 中,变量是用于存储程序中数据

关于Mybatis和JDBC的使用及区别

《关于Mybatis和JDBC的使用及区别》:本文主要介绍关于Mybatis和JDBC的使用及区别,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、JDBC1.1、流程1.2、优缺点2、MyBATis2.1、执行流程2.2、使用2.3、实现方式1、XML配置文件

macOS Sequoia 15.5 发布: 改进邮件和屏幕使用时间功能

《macOSSequoia15.5发布:改进邮件和屏幕使用时间功能》经过常规Beta测试后,新的macOSSequoia15.5现已公开发布,但重要的新功能将被保留到WWDC和... MACOS Sequoia 15.5 正式发布!本次更新为 Mac 用户带来了一系列功能强化、错误修复和安全性提升,进一步增

ubuntu20.0.4系统中安装Anaconda的超详细图文教程

《ubuntu20.0.4系统中安装Anaconda的超详细图文教程》:本文主要介绍了在Ubuntu系统中如何下载和安装Anaconda,提供了两种方法,详细内容请阅读本文,希望能对你有所帮助... 本文介绍了在Ubuntu系统中如何下载和安装Anaconda。提供了两种方法,包括通过网页手动下载和使用wg

Java资源管理和引用体系的使用详解

《Java资源管理和引用体系的使用详解》:本文主要介绍Java资源管理和引用体系的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、Java的引用体系1、强引用 (Strong Reference)2、软引用 (Soft Reference)3、弱引用 (W