使用gperftools來動態(tài)追蹤Nginx

環(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
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容