Clone
10
v3_CN_GPERF
winlin edited this page 2022-01-06 11:57:15 +08:00
This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

HOME > CN > GPERF Performance

GPERF内存和性能分析

Note: 如果觉得Github的Wiki访问太慢可以访问 Gitee 镜像。

本文只描述了GPERF工具的用法关于完整的性能分析方法参考SRS性能(CPU)、内存优化工具用法

valgrind一个很好用的内存和CPU分析工具SRS 3.0已经支持。

gperf是google用作内存和CPU分析的工具基于tcmalloc也是google内存分配库替换glibc的malloc和free。好消息是gperf可以用作srs的内存和性能分析。

gperf主要有三个应用

gmc内存检查

使用gmc时需要将tcmalloc编译进去或者动态链接具体参考官方文档。除此之外必须设置环境变量gmc才被开启。

SRS开启gmc的方法是

  • 配置时加上gmc./configure --with-gperf --with-gmc
  • 编译srsmake
  • 启动时指定环境变量:env PPROF_PATH=./objs/pprof HEAPCHECK=normal ./objs/srs -c conf/srs.conf
  • 停止srs打印gmc结果CTRL+C 或者发送SIGINT信号给SRS

备注make编译SRS成功后会打印出这些操作命令。

注意必须导出pprof环境变量PPROF_PATH,否则函数的地址和符合对应不上

若能打印下面的信息说明gmc成功启动

[winlin@dev6 srs]$ env PPROF_PATH=./objs/pprof HEAPCHECK=normal ./objs/srs -c conf/srs.conf
WARNING: Perftools heap leak checker is active -- Performance may suffer

gmc的结果

Leak check _main_ detected leaks of 184 bytes in 4 objects
The 4 largest leaks:
Using local file ./objs/srs.
Leak of 56 bytes in 1 objects allocated from:
	@ 46fae8 _st_stack_new
	@ 46f6b1 st_thread_create
	@ 46ea65 st_init
	@ 433f41 SrsServer::initialize
	@ 46e4ca main
	@ 3855a1ec5d __libc_start_main

有的时候gmc显示符号有问题无法显示函数那么就直接运行pprofgmc会有提示譬如

pprof ./objs/srs "/tmp/srs.11469._main_-end.heap" --inuse_objects --lines --heapcheck  --edgefraction=1e-10 --nodefraction=1e-10 --gv

需要改动两个地方:

  1. pprof改成./objs/pprof
  2. 去掉--gv直接进入命令行然后输入top就可以看到。

结果如下:

[winlin@centos6 srs]$ ./objs/pprof ./objs/srs "/tmp/srs.11469._main_-end.heap" --inuse_objects --lines --heapcheck  --edgefraction=1e-10 --nodefraction=1e-10
Using local file ./objs/srs.
Using local file /tmp/srs.11469._main_-end.heap.
Welcome to pprof!  For help, type 'help'.
(pprof) top
Total: 9 objects
       3  33.3%  33.3%        3  33.3% _st_netfd_new /home/winlin/srs/objs/st-1.9/io.c:136
       3  33.3%  66.7%        3  33.3% _st_stack_new /home/winlin/srs/objs/st-1.9/stk.c:78
       2  22.2%  88.9%        2  22.2% st_cond_new /home/winlin/srs/objs/st-1.9/sync.c:158
       1  11.1% 100.0%        1  11.1% SrsPithyPrint::create_ingester /home/winlin/srs/src/app/srs_app_pithy_print.cpp:139
       0   0.0% 100.0%        4  44.4% SrsAsyncCallWorker::start /home/winlin/srs/src/app/srs_app_async_call.cpp:70
       0   0.0% 100.0%        4  44.4% SrsConnection::cycle /home/winlin/srs/src/app/srs_app_conn.cpp:88
       0   0.0% 100.0%        2  22.2% SrsDvr::initialize /home/winlin/srs/src/app/srs_app_dvr.cpp:980
       0   0.0% 100.0%        2  22.2% SrsDvrPlan::initialize /home/winlin/srs/src/app/srs_app_dvr.cpp:570
       0   0.0% 100.0%        2  22.2% SrsHls::initialize /home/winlin/srs/src/app/srs_app_hls.cpp:1214
       0   0.0% 100.0%        2  22.2% SrsHlsMuxer::initialize /home/winlin/srs/src/app/srs_app_hls.cpp:370

另外SRS有例子说明如何使用gmc参考research/gperftools/heap-checker

GMP内存性能

使用gmc时需要将tcmalloc编译进去或者动态链接具体参考官方文档。

SRS开启gmp的方法是

  • 配置时加上gmc./configure --with-gperf --with-gmp
  • 编译srsmake
  • 正常启动srs就开始内存性能分析rm -f gperf.srs.gmp*; ./objs/srs -c conf/srs.conf
  • 停止srs生成gmp分析文件CTRL+C 或者发送SIGINT信号给SRS
  • 分析gmp文件./objs/pprof --text objs/srs gperf.srs.gmp*

备注make编译SRS成功后会打印出这些操作命令。

若能打印下面的信息则表示成功启动gmp

[winlin@dev6 srs]$ ./objs/srs -c conf/srs.conf
Starting tracking the heap

内存性能分析的结果如下:

[winlin@dev6 srs]$ ./objs/pprof --text objs/srs gperf.srs.gmp*
Using local file objs/srs.
Using local file gperf.srs.gmp.0001.heap.
Total: 0.1 MB
     0.0  31.5%  31.5%      0.0  49.1% SrsConfDirective::parse_conf
     0.0  28.4%  59.9%      0.0  28.4% std::basic_string::_Rep::_S_create
     0.0  27.4%  87.3%      0.0  27.4% _st_epoll_init
     0.0  11.7%  99.0%      0.0  11.7% __gnu_cxx::new_allocator::allocate
     0.0   0.4%  99.5%      0.0  27.9% st_init

另外SRS有例子说明如何使用gmc参考research/gperftools/heap-profiler

GCP-CPU性能分析

使用gcp时需要将tcmalloc编译进去或者动态链接具体参考官方文档。

SRS开启gcp的方法是

  • 配置时加上gmc./configure --with-gperf --with-gcp
  • 编译srsmake
  • 正常启动srs就开始内存性能分析rm -f gperf.srs.gcp*; ./objs/srs -c conf/srs.conf
  • 停止srs生成gmc分析文件CTRL+C 或者发送SIGINT信号给SRS
  • 分析gcp文件./objs/pprof --text objs/srs gperf.srs.gcp*

备注make编译SRS成功后会打印出这些操作命令。

性能分析的结果如下:

[winlin@dev6 srs]$ ./objs/pprof --text objs/srs gperf.srs.gcp*
Using local file objs/srs.
Using local file gperf.srs.gcp.
Removing _L_unlock_16 from all stack traces.
Total: 20 samples
       8  40.0%  40.0%        8  40.0% 0x00007fff0ea35917
       4  20.0%  60.0%        4  20.0% __epoll_wait_nocancel
       2  10.0%  70.0%        2  10.0% __read_nocancel
       1   5.0%  95.0%        1   5.0% memset
       1   5.0% 100.0%        1   5.0% tc_delete
       0   0.0% 100.0%        5  25.0% 0x00007f9fad927c4f
       0   0.0% 100.0%        2  10.0% SrsBuffer::ensure_buffer_bytes
       0   0.0% 100.0%        5  25.0% SrsClient::do_cycle
       0   0.0% 100.0%        5  25.0% SrsClient::fmle_publish
       0   0.0% 100.0%        1   5.0% SrsClient::process_publish_message

另外SRS有例子说明如何使用gmc参考research/gperftools/cpu-profiler

同时使用

可以同时开启:

  • gmc和gmp不支持同时开启。它们使用同一个框架无法一起运行参考文档的说明。
  • gmc和gcp支持同时开启。检测内存泄漏和测试CPU性能瓶颈。
  • gmp和gcp支持同时开启。检测内存瓶颈和CPU性能瓶颈。

备注SRS的configure脚本会检查是否可以同时开启。

Winlin 2014.3