環(huán)境 Centos 7.4
官網(wǎng)文檔
http://nginx.org/en/docs/ngx_google_perftools_module.html
安裝 gperftools library 及 相關(guān)的工具。
這里可以直接使用 yum 安裝
$ yum install gperftools.x86_64 gperftools-libs.x86_64 gperftools-devel.x86_64
這里用到的是openresty(nginx,tengine都可)
編譯
./configure --prefix=/usr/local/openresty/ --with-google_perftools_module --with-http_realip_module --with-luajit --with-http_gzip_static_module --with-http_stub_status_module
gmake
gmake install
平滑升級nginx
首先進入 nginx 的可執(zhí)行文件的所在目錄。
1. 讓nginx把nginx.pid文件修改成nginx.pid.oldbin,隨即啟動nginx,實現(xiàn)不間斷服務(wù)運行
kill -USR2 `cat /usr/local/openresty/nginx/logs/nginx.pid`
可以看到有兩組nginx在運行
$ ps aux|grep nginx
root 9423 0.0 0.0 37256 1628 ? Ss 7月15 0:00 nginx: master process /usr/local/openresty/nginx/sbin/nginx -c /usr/local/openresty/nginx/conf/nginx.conf
nobody 9424 0.0 0.0 37664 2224 ? S 7月15 0:00 nginx: worker process
nobody 9426 0.0 0.0 37664 2224 ? S 7月15 0:00 nginx: worker process
root 29292 0.0 0.0 59908 4516 ? S 18:55 0:00 nginx: master process /usr/local/openresty/nginx/sbin/nginx -c /usr/local/openresty/nginx/conf/nginx.conf
nobody 29293 0.0 0.0 60328 2212 ? S 18:55 0:00 nginx: worker process
nobody 29294 0.0 0.0 60328 2220 ? S 18:55 0:00 nginx: worker process
2. 平滑停止舊的work進程
kill -WINCH `cat /usr/local/openresty/nginx/logs/nginx.pid.oldbin`
$ ps aux|grep nginx
root 9423 0.0 0.0 37256 1628 ? Ss 7月15 0:00 nginx: master process /usr/local/openresty/nginx/sbin/nginx -c /usr/local/openresty/nginx/conf/nginx.conf
root 29292 0.0 0.0 59908 4516 ? S 18:55 0:00 nginx: master process /usr/local/openresty/nginx/sbin/nginx -c /usr/local/openresty/nginx/conf/nginx.conf
nobody 29293 0.0 0.0 60328 2212 ? S 18:55 0:00 nginx: worker process
nobody 29294 0.0 0.0 60328 2220 ? S 18:55 0:00 nginx: worker process
3. 退出舊的Nginx master進程
# kill -QUIT `cat /usr/local/openresty/nginx/logs/nginx.pid.oldbin`
# ps aux|grep nginx
root 29292 0.0 0.0 59908 4516 ? S 18:55 0:00 nginx: master process /usr/local/openresty/nginx/sbin/nginx -c /usr/local/openresty/nginx/conf/nginx.conf
nobody 29293 0.0 0.0 60328 2212 ? S 18:55 0:00 nginx: worker process
nobody 29294 0.0 0.0 60328 2220 ? S 18:55 0:00 nginx: worker process
配置動態(tài)追蹤我的nginx。在 nginx.conf 中的main 里添加
pid logs/nginx.pid;
google_perftools_profiles tmp/gperf;
reload nginx
可以在 /tmp/ 下發(fā)現(xiàn)很多以gperf開頭的文件
壓力測試一下。
wrk -t6 -c30000 -d30s http://127.0.0.1:4000
再次重載 nginx (reload nginx)
將pref文件導入成 svg 文件。
pprof --svg /usr/local/openresty/nginx/sbin/nginx gperf.61642 > nginx.svg

perf