SRS提供了一系列工具来定位性能瓶颈和内存泄漏,这些在./configure && make
后的summary
中是有给出来用法的,不过不是很方便,所以特地把用法写到这个文章中。
文中所有的工具,对于其他的linux程序也是有用的。
备注:所有工具用起来都会导致SRS性能低下,所以除非是排查问题,否则不要开启这些选项。
PERF是Linux性能分析工具,参考[PERF](perf record -e block:block_rq_issue -ag)。
可以实时看到当前的SRS热点函数:
perf top -p `ps aux|grep srs|grep conf|awk '{print $2}'`
或者记录一定时间的数据:
perf record -p `ps aux|grep srs|grep conf|awk '{print $2}'`
# 需要按CTRL+C取消record,然后执行下面的
perf report
Remark: 由于ST的堆栈是不正常的,perf开启
-g
后记录的堆栈都是错乱的,所以perf只能看SRS的热点,不能看堆栈信息。
GPROF是个GNU的CPU性能分析工具。参考SRS GPROF,以及GNU GPROF。
Usage:
# Build SRS with GPROF
./configure --with-gprof && make
# Start SRS with GPROF
./objs/srs -c conf/console.conf
# Or CTRL+C to stop GPROF
killall -2 srs
# To analysis result.
gprof -b ./objs/srs gmon.out
GPERF是google tcmalloc提供的cpu和内存工具,参考GPERF。
GCP是CPU性能分析工具,就是一般讲的性能瓶颈,看哪个函数调用占用过多的CPU。参考GCP。
Usage:
# Build SRS with GCP
./configure --with-gperf --with-gcp && make
# Start SRS with GCP
./objs/srs -c conf/console.conf
# Or CTRL+C to stop GCP
killall -2 srs
# To analysis cpu profile
./objs/pprof --text objs/srs gperf.srs.gcp*
GMD是GPERF提供的内存Defense工具,检测内存越界和野指针。一般在越界写入时,可能不会立刻导致破坏,而是在切换到其他线程使用被破坏的对象时才会发现破坏了,所以这种内存问题很难排查;GMD能在越界和野指针使用时直接core dump,定位在那个出问题的地方。参考GMD。
Usage:
# Build SRS with GMD.
./configure --with-gperf --with-gmd && make
# Start SRS with GMD.
env TCMALLOC_PAGE_FENCE=1 ./objs/srs -c conf/console.conf
GMC是内存泄漏检测工具,参考GMC。
Usage:
# Build SRS with GMC
./configure --with-gperf --with-gmc && make
# Start SRS with GMC
env PPROF_PATH=./objs/pprof HEAPCHECK=normal ./objs/srs -c conf/console.conf 2>gmc.log
# Or CTRL+C to stop gmc
killall -2 srs
# To analysis memory leak
cat gmc.log
GMP是内存性能分析工具,譬如检测是否有频繁的申请和释放堆内存导致的性能问题。参考GMP。
Usage:
# Build SRS with GMP
./configure --with-gperf --with-gmp && make
# Start SRS with GMP
./objs/srs -c conf/console.conf
# Or CTRL+C to stop gmp
killall -2 srs
# To analysis memory profile
./objs/pprof --text objs/srs gperf.srs.gmp*
VALGRIND是大名鼎鼎的C分析工具,SRS3之后支持了。SRS3之前,因为使用了ST,需要给ST打PATCH才能用。
valgrind --leak-check=full ./objs/srs -c conf/console.conf
Remark: SRS3之前的版本,可以手动给ST打PATCH支持VALGRIND,参考state-threads,详细的信息可以参考ST#2。
系统调用的性能排查,参考centos6的性能分析工具集合
在OSX/Darwin/Mac系统,可以用Instruments,在xcode中选择Open Develop Tools,就可以看到Instruments,也可以直接找这个程序,参考Profiling c++ on mac os x
instruments -l 30000 -t Time\ Profiler -p 72030
Remark: 也可以在Active Monitor中选择进程,然后选择Sample采样。
还有DTrace可以用,参考动态追踪技术(中) - Dtrace、SystemTap、火焰图或者浅谈动态跟踪技术之DTrace。
RTC是UDP的协议,下面命令是UDP分析常用的:
# 查看UDP缓冲区长度,默认只有200KB左右。
sysctl net.core.rmem_max
sysctl net.core.rmem_default
sysctl net.core.wmem_max
sysctl net.core.wmem_default
# 修改缓冲区长度为2MB,或改配置文件 /etc/sysctl.conf
sysctl net.core.rmem_max=2097152
sysctl net.core.rmem_default=2097152
sysctl net.core.wmem_max=2097152
sysctl net.core.wmem_default=2097152
# 查看接收和发送的丢包信息
netstat -s --udp
# 查看接收和发送的长度
netstat -lunp