]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Feature] lua_common: expose Lua heap usage helper
authorVsevolod Stakhov <vsevolod@rspamd.com>
Sat, 2 May 2026 14:26:58 +0000 (15:26 +0100)
committerVsevolod Stakhov <vsevolod@rspamd.com>
Sat, 2 May 2026 14:26:58 +0000 (15:26 +0100)
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.

src/lua/lua_common.c
src/lua/lua_common.h

index d22a53e9fdd84b02c979f5d4ff1b613402a5af7a..86a8b0a487e1797aa5636fd9383d941bd43ffdf5 100644 (file)
@@ -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)
index 4f8b213b1e810ddc3d47ded320b43e461637df83..265d42e1b3d77a5d7b60f61a616f430d0d31dd5c 100644 (file)
@@ -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