gperftools工具

11 篇文章 0 订阅

1.gperftools工具是什么

性能调优工具。可分析代码段内CPU,内存使用,or追踪某线程性能。
相对于 gprof, 它可以由程序员指定任意起始和终止位置,侵入式分析该区间性能。
github

2.安装/针对demo程序

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

3.嵌入至project

3.1 加入编译选项和头文件

在项目.pro中加入
LIBS += -lprofilerLIBS += -ltcmalloc
在插入代码之前,应加入头文件
#include <gperftools/profiler.h> // CPU Profiler 头文件#include <gperftools/heap-profiler.h> // Heap Profiler 头文件

3.2常用api

在起始位置和结束位置分别加入这两段
起始位置
// 启动 CPU Profiler
ProfilerStart(“/home/kenexs/xxx/profFile/profile_data.prof”); // 指定输出文件名
// 启动 Heap Profiler
HeapProfilerStart(“/home/kenexs/xxx/profFile/heap_profile”);
结束位置
//停止 CPU Profiler
ProfilerStop();
// 停止 Heap ProfilrHeap
HeapProfilerStop();

3.3 线程相关

可自行探索
/* Returns nonzero if profile is currently enabled, zero if it’s not. */
PERFTOOLS_DLL_DECL int ProfilingIsEnabledForAllThreads(void);

/* Routine for registering new threads with the profiler.
*/
PERFTOOLS_DLL_DECL void ProfilerRegisterThread(void);

4. 查看

4.1 .prof文件分析

在获取.prof文件后,转为文本or图片格式查看

4.1.1 文本形式

// 形式
pprof [options]

// 以server为例
pprof --text …path/V500R001/software_codes/build/AMD64/kn_server …path/profile_data.prof > profile_data.txt

4.1.2图片形式

火焰图工具FlameGraph
pprof [options]
// 以server为例 生成堆栈信息
pprof --collapsed …path/V500R001/software_codes/build/AMD64/kn_server …path/profile_data.prof > profile_data.cbt
// 绘制
…path/flamegraph.pl --invert --color aqua --title=“Flame Graph: malloc()” profile_data.cbt > graph.svg

4.2 .heap文件分析

堆栈使用情况
类似于上, a.out代指该程序的可执行文件。
// pprof文本分析
pprof --text --stack a.out profile.0001.heap

// 结合graphviz 和 gv进行图片分析,需要安装
sudo apt install graphviz gv
pprof --gv a.out profile.0001.heap

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值