From: Vsevolod Stakhov Date: Sat, 2 May 2026 14:26:58 +0000 (+0100) Subject: [Feature] lua_common: expose Lua heap usage helper X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8f3c44280f002d29f8c24bcb895d81cc8c57e8ea;p=thirdparty%2Frspamd.git [Feature] lua_common: expose Lua heap usage helper Add rspamd_lua_get_memory_used() that combines LUA_GCCOUNT and LUA_GCCOUNTB into a byte count. Used by the memstat control command; also a convenient single entry point for any future per-worker Lua heap diagnostics. --- diff --git a/src/lua/lua_common.c b/src/lua/lua_common.c index d22a53e9fd..86a8b0a487 100644 --- a/src/lua/lua_common.c +++ b/src/lua/lua_common.c @@ -1105,6 +1105,28 @@ void rspamd_lua_start_gc(struct rspamd_config *cfg) lua_gc(L, LUA_GCRESTART, 0); } +gsize rspamd_lua_get_memory_used(lua_State *L) +{ + int kb; + int rem; + + if (L == NULL) { + return 0; + } + + kb = lua_gc(L, LUA_GCCOUNT, 0); + rem = lua_gc(L, LUA_GCCOUNTB, 0); + + if (kb < 0) { + kb = 0; + } + if (rem < 0) { + rem = 0; + } + + return (gsize) kb * 1024 + (gsize) rem; +} + void rspamd_plugins_table_push_elt(lua_State *L, const char *field_name, const char *new_elt) diff --git a/src/lua/lua_common.h b/src/lua/lua_common.h index 4f8b213b1e..265d42e1b3 100644 --- a/src/lua/lua_common.h +++ b/src/lua/lua_common.h @@ -250,6 +250,12 @@ void rspamd_lua_close(lua_State *L); void rspamd_lua_start_gc(struct rspamd_config *cfg); +/** + * Returns the total amount of memory currently used by the Lua heap, in bytes. + * Combines LUA_GCCOUNT (kilobytes) and LUA_GCCOUNTB (bytes remainder). + */ +gsize rspamd_lua_get_memory_used(lua_State *L); + /** * Sets field in a global variable * @param L