gperftools —— cpu profiler

程序性能分析 - gperftools - Cpu Profiler 程序性能指标 程序性能认证 程序性能指标大部分情况下为运行时长。 工具使用 gperftools gperftools(google perftools)是一系列C/C++ 程序的性能工具,它是一个遵守 BSD 协议的开源项目。使用该工具可以对 CPU 时间片、内存等系统资源的分配和使用进行分析,或者使用其中的tcmalloc等库增加程序性能。 库docs目录下有相应帮助文档。 编译安装 * 国内可以从github/gperftools获取源码 wget https://github.com/gpe 阅读详情

安装

32 位系统

只要去https://code.google.com/p/gperftools/downloads/list下载gperftools安装就可以了,

./configure

make

make install


64位系统

需要先安装libunwind库 http://gperftools.googlecode.com/svn/trunk/INSTALL 里面说明了原因,如果不安装libunwind库也可以,就是在安装gperftools的时候 enable-frame-pointers, 并且在编译应用程序的时候加上 -fno-omit-frame-pointer

*** NOTE FOR 64-BIT LINUX SYSTEMS

The glibc built-in stack-unwinder on 64-bit systems has some problems
with the perftools libraries.  (In particular, the cpu/heap profiler
may be in the middle of malloc, holding some malloc-related locks when
they invoke the stack unwinder.  The built-in stack unwinder may call
malloc recursively, which may require the thread to acquire a lock it
already holds: deadlock.)

For that reason, if you use a 64-bit system, we strongly recommend you
install libunwind before trying to configure or install gperftools.
libunwind can be found at

   http://download.savannah.gnu.org/releases/libunwind/libunwind-0.99-beta.tar.gz

Even if you already have libunwind installed, you should check the
version.  Versions older than this will not work properly; too-new
versions introduce new code that does not work well with perftools
(because libunwind can call malloc, which will lead to deadlock).

There have been reports of crashes with libunwind 0.99 (see
http://code.google.com/p/gperftools/issues/detail?id=374).
Alternately, you can use a more recent libunwind (e.g. 1.0.1) at the
cost of adding a bit of boilerplate to your code.  For details, see
http://groups.google.com/group/google-perftools/msg/2686d9f24ac4365f

   CAUTION: if you install libunwind from the url above, be aware that
   you may have trouble if you try to statically link your binary with
   perftools: that is, if you link with 'gcc -static -lgcc_eh ...'.
   This is because both libunwind and libgcc implement the same C++
   exception handling APIs, but they implement them differently on
   some platforms.  This is not likely to be a problem on ia64, but
   may be on x86-64.

   Also, if you link binaries statically, make sure that you add
   -Wl,--eh-frame-hdr to your linker options. This is required so that
   libunwind can find the information generated by the compiler
   required for stack unwinding.

   Using -static is rare, though, so unless you know this will affect
   you it probably won't.

If you cannot or do not wish to install libunwind, you can still try
to use the built-in stack unwinder.  The built-in stack unwinder
requires that your application, the tcmalloc library, and system
libraries like libc, all be compiled with a frame pointer.  This is
*not* the default for x86-64.

If you are on x86-64 system, know that you have a set of system
libraries with frame-pointers enabled, and compile all your
applications with -fno-omit-frame-pointer, then you can enable the
built-in perftools stack unwinder by passing the
--enable-frame-pointers flag to configure.

Even with the use of libunwind, there are still known problems with
stack unwinding on 64-bit systems, particularly x86-64.  See the
"64-BIT ISSUES" section in README.

If you encounter problems, try compiling perftools with './configure
--enable-frame-pointers'.  Note you will need to compile your
application with frame pointers (via 'gcc -fno-omit-frame-pointer
...') in this case.

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++

使用

http://google-perftools.googlecode.com/svn/trunk/doc/cpuprofile.html 将了使用的方法

在代码link过程中添加参数 –lprofiler

1、直接调用提供的api:
这种方式比较适用于对于程序的某个局部来做分析的情况,直接在要做分析的局部调用相关的api即可。
方式:#include <gperftools/profiler.h>, 并调用函数:ProfilerStart() and ProfilerStop() 开始和结束


2、使用环境变量
运行程序:env CPUPROFILE=./helloworld.prof ./helloworld
指定要profile的程序为helloworld,并且指定产生的分析结果文件的路径为./helloworld.prof


如果是多线程,则在线程的开始处调用 ProfilerRegisterThread()

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++

结果分析

我们可以通过pprof生成 text,pdf等各种形式的输出

如 pprof --text ./program prog.prof  > prog.txt

pprof --pdf ./program  prog.prof > prog.pdf

其中 program 是应用程序

如果想生存pdf的格式必须要安装 ghostscript 和 graphviz 两个库


Text mode has lines of output that look like this:

       14   2.1%  17.2%       58   8.7% std::_Rb_tree::find

Here is how to interpret the columns:

  1. Number of profiling samples in this function  分析样本数量(不包含其他函数调用)
  2. Percentage of profiling samples in this function  分析样本百分比(不包含其他函数调用)
  3. Percentage of profiling samples in the functions printed so far  目前为止的分析样本百分比(不包含其他函数调用)
  4. Number of profiling samples in this function and its callees  分析样本数量(包含其他函数调用)
  5. Percentage of profiling samples in this function and its callees  分析样本百分比(包含其他函数调用)
  6. Function name  函数名

含义如下:
14:find函数花费了14个profiling samples
2.1%:find函数花费的profiling samples占总的profiling samples的比例
17.2%:到find函数为止,已经运行的函数占总的profiling samples的比例
58:find函数加上find函数里的被调用者总共花费的profiling samples
8.7%:find函数加上find函数里的被调用者总共花费的profiling samples占总的profiling samples的比例
std::_Rb_tree::find:表示profile的函数
注意: 默认是 100 samples a second,所以得出的结果除以100,得秒单位,即 find函数本身花费了0.14秒, 而包含内部的其他调用之后花费了 0.58秒



关于图形风格输出结果
1.节点
每个节点代表一个函数,节点数据格式:
Class Name
Method Name
local (percentage)
of cumulative (percentage)
  
local时间是函数直接执行的指令所消耗的CPU时间(包括内联函数);性能分析通过抽样方法完成,默认是1秒100个样本,一个样本是10毫秒,即时间单位是10毫秒;
cumulative时间是local时间与其他函数调用的总和;
如果cumulative时间与local时间相同,则不打印cumulative时间项。
2.有向边
调用者指向被调用者,有向边上的时间表示被调用者所消耗的CPU时间

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

注意:

要用google-perftool来分析程序,必须保证程序能正常退出。












实战解析:如何用gperftoolsCPU Profiler定位C++服务中的性能热点(附信号控制技巧) 本文详细解析了如何使用gperftoolsCPU Profiler定位C++服务中的性能热点,包括侵入式与非侵入式采样方法,并分享了生产环境中动态采样的信号控制技巧。通过实战案例和优化建议,帮助开发者高效识别和解决性能瓶颈,提升服务响应速度。 阅读详情

相关推荐

运算放大器电路仿真实践:从零实现完整示例

通过一个完整的运算放大器电路仿真示例,深入掌握电路仿真的关键步骤与技巧,帮助电子爱好者和工程师快速上手实际项目设计。

weixin_34725745的博客 1288

使用cpu profile调试服务CPU性能

服务端程序CPU性能优化工具

cj19920421的博客 2229

rk3399开发环境的介绍

由于Bluez的介绍文档有限,以及对Linux 系统/驱动概念、D-Bus 通信和蓝牙协议都有要求,加上网络上其实没有一个完整的介绍Bluez系列的文档,所以不管是蓝牙初学者还是蓝牙从业人员,都有不小的难度,学习曲线也相对较陡,所以我有了这个想法,专门对Bluez做一个系统性的介绍,尽可能的涵盖所有内容。

朝气蓬勃 669

使用 gperftools 分析程序cpu性能

gperftools 是 google 开源的一组套件,提供了高性能的、支持多线程的 malloc 实现,以及一组优秀的性能分析工具。从 gperftools github 官网上下载最新版本的源码包:命令结束执行后出现一个报错:这是因为没有安装 。这里直接使用 yum 的方式安装:再次执行 ,命令执行成功。执行如下两个命令,进行编译并安装:最后执行 更新动态库文件执行如下命令,确认 gperftools 安装成功 三、示例 3.1 demo 3.2 编译,并链接profiler库 3.3 运行 p

线上幽灵 1520

18 | 性能分析:找出程序的瓶颈

今天是“技能进阶”单元的最后一节课,也要兑现刚开始在“概论”里的承诺,讲一讲在运行阶段我们能做什么。

qq_37756660的博客 1468

使用 gprof 监测程序性能 (1)

代码如下 5.c: #include #include #include void foo(int i) { printf("%d\n", i); } void *thread( void *arg) { ProfilerRegisterThread(); int i = 0; while(1) { foo(i++); if(i == 100000) { b

小猪爱拱地 654

C++性能分析工具gperftools安装教程与使用案例分析

gperftools是由谷歌开发的一套性能分析工具。它主要用于帮助开发人员更好地了解其程序的性能瓶颈并进行有效的优化。本文介绍gperftools的安装流程,并给出实际案例加深理解

FRIGIDWINTER的博客 1万+

gperftools CPU Profiler 使用指南:性能分析利器详解

gperftools 中的 CPU Profiler 是一个强大的性能分析工具,它通过采样方式收集程序运行时的调用信息,帮助开发者识别性能瓶颈。本文将全面介绍如何使用这个工具,从基础配置到高级分析技巧。 ## 环境准备 ### 链接 profiler 库 要在程序中使用 CPU Profiler,需要在编译链接阶段添加 `-lprofiler` 选项。这种方式会将 profiler 代码嵌入...

gitblog_00527的博客 506

性能测试工具CPU profiler(gperftools)的使用心得

按照 http://www.cnblogs.com/persistentsnail/p/3294843.html,yum安装gperftools-devel 和 libunwind 安装gperftools-devel的时候,同时就安装了 gperftools。如何使用,主要参考了 https://wiki.geany.org/howtos/profiling/gperftools,才入的门,回头...

yupei881027的专栏 1224

动态执行流程分析和性能瓶颈分析的利器——gperftoolsCpu Profiler

在《动态执行流程分析和性能瓶颈分析的利器——valgraind的callgrind》中,我们领略了valgrind对流程和性能瓶颈分析的强大能力。本文将介绍拥有相似能力的gperftoolsCpu Profiler。(转载请指明出于breaksoftware的csdn博客) 我们依然以callgrind一文中的例子为例 #include &lt;thread...

方亮的专栏 4462

gperftools源码分析和项目应用 - CPU Profiler

gperftools源码分析和项目应用 - CPU Profiler 原文:https://blog.csdn.net/yubo112002/article/details/81076821 原文链接:http://www.tealcode.com/gperftool_source_analysis/ Google的gperftool是一款非常好用的服务器程序性能分析工具,能提供非常...

weixin_30329623的博客 151

CPU profiler(gperftools)在嵌入式系统上的应用示例

上一篇博文《性能测试工具CPU profiler(gperftools)的使用心得》记录了CPU profiler的基本使用方式。本文以halley2开发板为例记录用CPU profiler在嵌入系统下进行性能分析的方法. 按照上文方法,已经可以让自己的测试程序test_capture生成性能分析报告 test_capture.prof。然后将test_capture交叉编译生成mips的可执行文...

10km的专栏 2417

Google performance Tools (gperftools) 使用心得

http://www.cnblogs.com/my_life/articles/4683313.html http://www.cnblogs.com/persistentsnail/p/3294843.html   gperftools是google开发的一款非常实用的工具集,主要包括:性能优异的malloc free内存分配器tcmalloc;基于tcmalloc

freedom8531的专栏 2330

gperftools cpp wrapper

gperftools cpp wrapper // Compile command : ${CXX} -o test_profiler.elf -DUSE_GPERFTOOLS -DDEBUG -D_DEBUG ${CXXFLAGS} -O0 -g -Wall ${LIBS} -lprofiler ${BUILD_LDFLAGS} test_profiler.cpp// Bash Comm...

weixin_30332241的博客 134

Google performance Tools (gperftools) 使用心得 (zhuan)

http://blog.csdn.net/passers_b/article/details/9264151 Google performance Tools (gperftools) 使用心得 gperftools是google开发的一款非常实用的工具集,主要包括:性能优异的malloc free内存分配器tcmalloc;基于tcmalloc的堆内存检测和内存泄漏分析工具heap-prof...

weixin_30883271的博客 108

gperftools在微服务架构中的性能监控应用

你是否正面临微服务架构下的性能瓶颈定位难题?分布式系统中单个服务的内存泄漏或CPU过载可能导致整个调用链崩溃,而传统监控工具往往无法提供细粒度的性能数据。gperftools(Google Performance Tools)作为一套高性能的C++性能分析工具集,通过TCMalloc内存分配器、CPU ProfilerCPU剖析器)和Heap Profiler(堆剖析器)三大核心组件,为微服务性...

gitblog_00768的博客 972

gperftools性能分析工具实践指南

在当前的应用程序开发中,性能分析是一个不可或缺的环节。优秀的性能分析工具可以帮助开发和运维团队理解程序的行为,找出性能瓶颈,优化系统资源使用效率。gperftools是一个在Linux系统上广泛使用的性能分析工具集,它由Google开发,用于提供应用程序的CPU和内存使用情况分析。Heap Profiler还提供了多种高级堆分析技术,以便开发者能够更深入地理解应用程序的内存使用情况。这些技术包括:生成内存使用报告。

weixin_42590539的博客 1008

基于javaweb的图书管理系统源码+数据库.zip

基于javaweb的图书管理系统源码+数据库.zip基于javaweb的图书管理系统源码+数据库.zip基于javaweb的图书管理系统源码+数据库.zip基于javaweb的图书管理系统源码+数据库.zip基于javaweb的图书管理系统源码+数据库.zip基于javaweb的图书管理系统源码+数据库.zip基于javaweb的图书管理系统源码+数据库.zip基于javaweb的图书管理系统源码+数据库.zip基于javaweb的图书管理系统源码+数据库.zip基于javaweb的图书管理系统源码+数据库.zip基于javaweb的图书管理系统源码+数据库.zip基于javaweb的图书管理系统源码+数据库.zip基于javaweb的图书管理系统源码+数据库.zip基于javaweb的图书管理系统源码+数据库.zip基于javaweb的图书管理系统源码+数据库.zip基于javaweb的图书管理系统源码+数据库.zip基于javaweb的图书管理系统源码+数据库.zip基于javaweb的图书管理系统源码+数据库.zip基于javaweb的图书管理系统源码+数据库.zip基于java

上一篇: linux 简单配置samba 服务
下一篇: ZeroMQ学习笔记
wind19
博客等级 码龄23年 477粉丝 170原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值