Nginx利用打印内存使用信息

jemalloc

1. 首先编译时加入选项--with-jemalloc=libs/jemalloc-5.2.1。如果编译时遇到jemalloc的Makefile相关错误,就需要修改nginx中jemalloc对应makefile。错误原因应该和jemalloc的auto_gen.sh相关。

2. 在ngx_cycle.c中添加下面几句

#include <jemalloc/jemalloc.h>


ngx_init_cycle(ngx_cycle *old_cycle)
{
    ...
    malloc_stats_print(NULL, NULL, NULL);

    ...
}

然后在reload的时候在error.log中就能看到内存使用的信息。

输出的内容太多了,我也看不懂。。。

 

ngx_debug_pool

对于不熟悉malloc内存管理的人而言,以上信息对于定位问题没什么帮助。

一个有效的办法是利用ngx_debug_pool模块进行定位。

1. 编译ngx_debug_pool模块,

--add-module=./modules/ngx_debug_pool

2. 配置开启debug_pool

http {
   server {
       listen 80;

       location = /debug_pool {
           debug_pool;
       }
   }
}

3. 发起http请求,结果如下

curl http://localhost:80/debug_pool
pid:18821
size:      223784 num:           2 cnum:           1 lnum:          10 ngx_init_cycle
size:        1536 num:           4 cnum:           1 lnum:          10 ngx_event_accept
size:           0 num:           1 cnum:           0 lnum:           0 ngx_http_lua_create_fake_request
size:           0 num:           1 cnum:           0 lnum:           0 main
size:           0 num:           1 cnum:           0 lnum:           0 ngx_http_lua_create_fake_connection
size:           0 num:           1 cnum:           0 lnum:           6 ngx_http_server_names
size:        8192 num:           4 cnum:           1 lnum:           0 ngx_http_create_request
size:           0 num:           1 cnum:           0 lnum:           0 ngx_http_lua_init_worker
size:       228KB num:          15 cnum:           3 lnum:          26 [SUMMARY]

 

4. 查看master进程的pool分配情况

gdb -q -x /tmp/debug_pool.gdb -p 1468298

进入gdb后输入"debug_pool" 就能够看到pool的分配和使用情况

其中debug_pool.gdb是在ngx_debug_pool模块代码中的,-p 后指定的是master进程pid

结果如下

 

参考链接

https://www.codeleading.com/article/64221285655/

http://tengine.taobao.org/document/ngx_debug_pool.html

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值