From a22f0736d8c3c9a3afb0e97dcb9d860f90b5cd1d Mon Sep 17 00:00:00 2001 From: Paul Kulchenko Date: Fri, 13 Aug 2021 02:11:49 -0700 Subject: [PATCH] Improve Redbean Lua memory (#241) - Update redbean Lua to check there is enough stack to use - Add explicit Lua gc pass after each message is processed - Add Lua memory reporting to redbean --- tool/net/redbean.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tool/net/redbean.c b/tool/net/redbean.c index bd33d399d..237a636ba 100644 --- a/tool/net/redbean.c +++ b/tool/net/redbean.c @@ -545,6 +545,9 @@ static void CollectGarbage(void) { LOGIFNEG1(munmap(unmaplist.p[unmaplist.n].p, unmaplist.p[unmaplist.n].n)); LOGIFNEG1(close(unmaplist.p[unmaplist.n].f)); } +#ifndef STATIC + (void)lua_gc(L, LUA_GCCOLLECT); +#endif } static void UseOutput(void) { @@ -1080,6 +1083,8 @@ static int LuaCallWithTrace(lua_State *L, int nargs, int nres) { } else { // move results to the main stack lua_xmove(co, L, nresults); + // make sure the stack has enough space to grow + luaL_checkstack(L, nres - nresults, NULL); // grow the stack in case returned fewer results // than the caller expects, as lua_resume // doesn't adjust the stack for needed results @@ -3130,6 +3135,9 @@ static char *ServeStatusz(void) { AppendLong1("lastmeltdown", shared->lastmeltdown); AppendLong1("workers", shared->workers); AppendLong1("assets.n", assets.n); +#ifndef STATIC + AppendLong1("lua.memory", lua_gc(L, LUA_GCCOUNT)*1024 + lua_gc(L, LUA_GCCOUNTB)); +#endif ServeCounters(); AppendRusage("server", &shared->server); AppendRusage("children", &shared->children);