From 5a7094112c12da6350397b313e8c4b07328e4e01 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Marek=20Vavru=C5=A1a?= Date: Sat, 1 Aug 2015 19:51:13 +0200 Subject: [PATCH] daemon: more aggressive Lua GC, forced GC steps MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit the memory could go through the roof with sufficiently high pps, GC now runs at 4x the speed of allocations and doesn’t wait for increase, some callbacks also perform full collection cycle on completion --- daemon/bindings.c | 1 + daemon/engine.c | 7 +++++++ daemon/ffimodule.c | 14 +++++--------- daemon/worker.c | 9 ++++++--- 4 files changed, 19 insertions(+), 12 deletions(-) diff --git a/daemon/bindings.c b/daemon/bindings.c index 29af9e62d..bef846f63 100644 --- a/daemon/bindings.c +++ b/daemon/bindings.c @@ -449,6 +449,7 @@ static void event_callback(uv_timer_t *timer) } /* Clear the stack, there may be event a/o enything returned */ lua_settop(L, top); + lua_gc(L, LUA_GCCOLLECT, 0); /* Free callback if not recurrent or an error */ if (ret != 0 || uv_timer_get_repeat(timer) == 0) { uv_close((uv_handle_t *)timer, (uv_close_cb) event_free); diff --git a/daemon/engine.c b/daemon/engine.c index db0c5c39e..f482f2e6d 100644 --- a/daemon/engine.c +++ b/daemon/engine.c @@ -244,6 +244,7 @@ static int init_state(struct engine *engine) return kr_error(ENOMEM); } /* Initialize used libraries. */ + lua_gc(engine->L, LUA_GCSTOP, 0); luaL_openlibs(engine->L); /* Global functions */ lua_pushcfunction(engine->L, l_help); @@ -397,6 +398,12 @@ int engine_start(struct engine *engine) return ret; } + /* Clean up stack and restart GC */ + lua_settop(engine->L, 0); + lua_gc(engine->L, LUA_GCCOLLECT, 0); + lua_gc(engine->L, LUA_GCSETSTEPMUL, 99); + lua_gc(engine->L, LUA_GCSETPAUSE, 400); + lua_gc(engine->L, LUA_GCRESTART, 0); return kr_ok(); } diff --git a/daemon/ffimodule.c b/daemon/ffimodule.c index 046521a5f..920b12507 100644 --- a/daemon/ffimodule.c +++ b/daemon/ffimodule.c @@ -95,16 +95,12 @@ static inline int l_ffi_call(lua_State *L, int argc) lua_pop(L, 1); return kr_error(EIO); } - - int n = lua_gettop(L); - if (n > 0) { - if (lua_isthread(L, -1)) { /* Continuations */ - status = l_ffi_defer(lua_tothread(L, -1)); - } else if (lua_isnumber(L, -1)) { /* Return code */ - status = lua_tonumber(L, -1); - } - lua_pop(L, 1); + if (lua_isthread(L, -1)) { /* Continuations */ + status = l_ffi_defer(lua_tothread(L, -1)); + } else if (lua_isnumber(L, -1)) { /* Return code */ + status = lua_tonumber(L, -1); } + lua_pop(L, 1); return status; } diff --git a/daemon/worker.c b/daemon/worker.c index 3cf883d1e..cd61e2aa4 100644 --- a/daemon/worker.c +++ b/daemon/worker.c @@ -15,6 +15,7 @@ */ #include +#include #include #include #include @@ -168,14 +169,16 @@ static void qr_task_free(uv_handle_t *handle) } else { mp_delete(mp_context); } -#if defined(__GLIBC__) && defined(_GNU_SOURCE) /* Decommit memory every once in a while */ static int mp_delete_count = 0; - if (++mp_delete_count == 100 * MP_FREELIST_SIZE) { + if (++mp_delete_count == 100000) { + lua_gc(worker->engine->L, LUA_GCCOLLECT, 0); +#if defined(__GLIBC__) && defined(_GNU_SOURCE) malloc_trim(0); +#endif mp_delete_count = 0; } -#endif + /* Update stats */ worker->stats.concurrent -= 1; } -- 2.47.2