Nginx 性能分析之 gpreftools
之前介绍了多种nginx调试工具,今天介绍 nginx 性能分析工具——gperftools
gperftools 是 google_perftools,google 开发的一款非常使用的工具集,主要包括性能优异的 malloc free 内存分配器 tcmalloc、基于 tcmalloc 的堆内存检测和内存泄露分析工具 heap-profiler、heap-checker、基于 tcmalloc 实现的程序 CPU 性能检测工具 cpu-profiler
github 上提供了非常详尽的文档https://github.com/gperftools/gperftools
回到主题,在 nginx 中,可以通过--with-google_perftools_module 编译参数启用这个性能分析模块

如图,添加编译参数,重新编译 nginx
该模块依赖于 gpreftools 库,所以在编译之前,需要先编译安装 gperftools,在 64 位系统下,需要先安装 libunwind
libunwind 安装:
从https://github.com/libunwind/libunwind/releases或http://download.savannah.gnu.org/releases/libunwind/下载最新版本安装包
解压,编译安装

当然,你也可以--prefix 指定位置编译安装
安装完成后,安装 gperftools

安装完成后,重新编译安装 nginx

编译安装完后,如果遇到找不到 lib 共享库,可以和上图一样,做软链处理,libunwind 和 gperftools 编译完成后,共享库都是存储在/usr/local/lib 的,而 64 位的系统中通常是找/lib64 下面的,或者可以通过添加 ldconfig 来解决
解决上诉问题后,可以看到 nginx 已经开启了 google_perftools 模块
在 nginx 中配置 gperftools 很简单,就一个指令

配置在主配置文件中,指定分析信息存储的文件位置及名称即可

这样就会生成一个分析结果文件,后面的数字是 nginx 工作进程的 PID 号
接着用 ab 压测一下 nginx,接着再重载一下 nginx,让分析结果落盘写入分析信息文件

生成的文件不方便查看,所以我们通过 svg 来查看,首先安装图像分析生成工具 graphviz,直接通过 yum 安装就可以
安装完成后,通过 pprof 来生成 svg

生成 nginx.svg,我们查看结果,为了方便完整,我转换成了 png

这样我们就能很清晰的看到 nginx 的调用和性能消耗,从而找到性能瓶颈点,进行相应优化
版权声明: 本文为 InfoQ 作者【运维研习社】的原创文章。
原文链接:【http://xie.infoq.cn/article/977414e8a6a01dac35e0dfb29】。文章转载请联系作者。
评论