Hello!
On Wed, Aug 28, 2013 at 7:32 PM, Alvin Jiang wrote:
> 我使用ngx_lua的resty_mysql模块去查询数据库,运行一夜以后发现CPU占用100%始终无法降低,用gdb attach以后发现nginx
> worker处在下文描述的调用堆栈中,恢复运行后再次 中断基本上还是处在这样的调用堆栈中(偶尔会执行其它代码),能提示一下有哪些原因吗?谢谢
>
CPU 100% 这样的问题其实是最好诊断的了。最常见的情况是你的某条 Lua 代码路径热循环了。你可以使用 ngx-lua-bt
工具获取当前的 Lua 调用栈迹:
https://github.com/agentzh/nginx-systemtap-toolkit#ngx-lua-bt
当然,你也可以使用 gdb 扩展命令 lbt:
https://github.com/agentzh/nginx-gdb-utils/blob/master/lbt.py
使用方法是
(gdb) source lbt.py
(gdb) lbt 0xb0234ef8
这里的神秘的数 0xb0234ef8 是从你提供的 C backtrace 里得到的 L 参数的值。你应视当前的实际情况进行调整。
> #2 0xb765839b in lua_pushfstring (L=0xb0234ef8, fmt=0x81320bf "cannot
> resume %s coroutine") at lj_api.c:618
> #3 0x080e8f15 in ngx_http_lua_coroutine_resume (L=0xb0234ef8) at
> ../ngx_lua-0.8.2/src/ngx_http_lua_coroutine.c:165
> #4 0xb764c37f in lj_BC_FUNCC () from
> /usr/local/openresty/luajit/lib/libluajit-5.1.so.2
> #5 0xb765978e in lua_resume (L=0xb0234ef8, nargs=1) at lj_api.c:1134
> #6 0x080dc5dc in ngx_http_lua_run_thread (L=0xb71a91c0, r=0x9da5570,
> ctx=0x9db5024, nrets=1) at ../ngx_lua-0.8.2/src/ngx_http_lua_util.c:1032
从你这里的 C 调用栈轨迹来看,很可能是你有某个 Lua 循环不断地调用 coroutine.resume(),但你并没有对这个
coroutine.resume() 调用作恰当的错误处理并退出当前循环。当然,由于你提供的信息极为有限,我只能作如此推测。
更一般地,对于高 CPU 的问题,可以使用 C 级别的火焰图采样工具
https://github.com/agentzh/nginx-systemtap-toolkit#ngx-sample-bt
以及 Lua 级别的火焰图采样工具
https://github.com/agentzh/nginx-systemtap-toolkit#ngx-sample-lua-bt
进行代码热点定位与分析。
同时抄送给 openresty 中文邮件列表: https://groups.google.com/group/openresty
建议你也加入此列表并在那里讨论这样的问题。谢谢合作!
Best regards,
-agentzh