]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
daemon: more aggressive Lua GC, forced GC steps
authorMarek Vavruša <marek.vavrusa@nic.cz>
Sat, 1 Aug 2015 17:51:13 +0000 (19:51 +0200)
committerMarek Vavruša <marek.vavrusa@nic.cz>
Sat, 1 Aug 2015 17:52:47 +0000 (19:52 +0200)
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
daemon/engine.c
daemon/ffimodule.c
daemon/worker.c

index 29af9e62d3d18b0119e40436cacd4e5ab33c5010..bef846f63351e690181996d2d650aa3a71d5f2fe 100644 (file)
@@ -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);
index db0c5c39eb10ada83e0d0ecedc5cfef8ebc288fe..f482f2e6d3da225d5a65203a149d21369cbb9a0e 100644 (file)
@@ -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();
 }
 
index 046521a5f051d5907aea244b29e7f7941b4a96bf..920b125072b603f0c40e687377d9a763c46dfe45 100644 (file)
@@ -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;
 }
 
index 3cf883d1e3801690a10e0cfc1bb65b973b8a6ca7..cd61e2aa4f64a1f4d6c9c6a5aab1d4e44c0f7084 100644 (file)
@@ -15,6 +15,7 @@
  */
 
 #include <uv.h>
+#include <lua.h>
 #include <libknot/packet/pkt.h>
 #include <libknot/internal/net.h>
 #include <contrib/ucw/lib.h>
@@ -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;
 }