Skip to content
Snippets Groups Projects
Commit 5a709411 authored by Marek Vavruša's avatar Marek Vavruša
Browse files

daemon: more aggressive Lua GC, forced GC steps

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
parent c0bf727a
No related branches found
No related tags found
No related merge requests found
......@@ -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);
......
......@@ -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();
}
......
......@@ -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;
}
......
......@@ -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;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment