openresty / lua-nginx-module

Embed the Power of Lua into NGINX HTTP servers
https://openresty.org/
11.33k stars 2.03k forks source link

privileged agent and malloc trim #2233

Closed bungle closed 1 year ago

bungle commented 1 year ago

@agentzh or anyone else, do you know if the privileged agent does malloc trims? I am asking this because we would like to utilise privileged agent more, but it seems to cause more memory usage (that is never or very rarely returned - not leaking, just that memory usage spikes seem to persist for a long time). And one reason could be that it does not do malloc trims?

This (https://github.com/openresty/lua-nginx-module#lua_malloc_trim) makes a lot of sense on normal request processing workers, but privileged agent doesn't process any requests (that is what lead me to ask malloc trim in specific, but it might be unrelated to it).

zhuizhuhaomeng commented 1 year ago

As long as ngx_http_lua_log_handler is called, the malloc_trim will take effect.

zhuizhuhaomeng commented 1 year ago

As long as ngx_http_lua_log_handler is called, the malloc_trim will take effect.

zhuizhuhaomeng commented 1 year ago

You can use gdb and add breakpoint on malloc_trim to see if it happends.

bungle commented 1 year ago

@zhuizhuhaomeng thanks, I believe that nothing except init_worker handler is called on privileged agent, so it still feels about right that some malloc trim needs to be added for privileged agent too.

I’ll investigate it a bit more.

chobits commented 1 year ago

@zhuizhuhaomeng thanks, I believe that nothing except init_worker handler is called on privileged agent, so it still feels about right that some malloc trim needs to be added for privileged agent too.

I’ll investigate it a bit more.

Perhaps we can set up a timer to actively call malloc_trim(1) using ffi in the privileged agent. This can help us see if the memory issue is due to free memory not being released.

oowl commented 1 year ago

I think privileged workers can not trigger lua_malloc_trim function, even if we have the openresty timer task (fake request), it still can not be trigger. So as @chobits said, we can call malloc_trim in luajit ffi function to do this.

bungle commented 1 year ago

Reporting back here. We added FFI calls and tested it on Linux, but didn't find malloc_trim helping the situation. We need to do further investigation on memory usage before we can conclude anything. Thus I am closing this. Thank you!

zhuizhuhaomeng commented 1 year ago

@bungle OpenResty XRay can help you to identify the memory issue. You can apply for a free trial https://xray.openresty.com.