From 43c6c0d8daa2c2701efd2d228d240d446a894dde Mon Sep 17 00:00:00 2001 From: Kit Chan Date: Tue, 18 Jan 2022 22:43:32 -0800 Subject: [PATCH 1/3] Add option to disable JIT mode for luajit --- plugins/lua/ts_lua.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/plugins/lua/ts_lua.c b/plugins/lua/ts_lua.c index d1634d86d94..b233962f05b 100644 --- a/plugins/lua/ts_lua.c +++ b/plugins/lua/ts_lua.c @@ -24,6 +24,7 @@ #include #include "ts_lua_util.h" +#include "luajit.h" #define TS_LUA_MAX_STATE_COUNT 256 @@ -773,9 +774,11 @@ TSPluginInit(int argc, const char *argv[]) int states = ts_lua_max_state_count; + int jit = 1; int reload = 0; static const struct option longopt[] = { {"states", required_argument, 0, 's'}, + {"jit", required_argument, 0, 'j'}, {"enable-reload", no_argument, 0, 'r'}, {0, 0, 0, 0}, }; @@ -789,6 +792,19 @@ TSPluginInit(int argc, const char *argv[]) states = atoi(optarg); // set state break; + case 'j': + jit = atoi(optarg); + if (jit == 0) { + TSDebug(TS_LUA_DEBUG_TAG, "[%s] disable JIT mode", __FUNCTION__); + for (int index = 0; index < ts_lua_max_state_count; ++index) { + ts_lua_main_ctx *const main_ctx = (ts_lua_g_main_ctx_array + index); + lua_State *const lstate = main_ctx->lua; + if (luaJIT_setmode(lstate, 0, LUAJIT_MODE_ENGINE|LUAJIT_MODE_OFF) == 0) { + TSError("[ts_lua][%s] Failed to disable JIT mode", __FUNCTION__); + } + } + } + break; case 'r': reload = 1; TSDebug(TS_LUA_DEBUG_TAG, "[%s] enable global plugin reload [%d]", __FUNCTION__, reload); From 1eabcd0d57dfdae668f3412b67813e04f60cfae5 Mon Sep 17 00:00:00 2001 From: Kit Chan Date: Tue, 18 Jan 2022 23:06:20 -0800 Subject: [PATCH 2/3] Update lua plugin documentation --- doc/admin-guide/plugins/lua.en.rst | 53 +++++++++++++++++++++++------- 1 file changed, 41 insertions(+), 12 deletions(-) diff --git a/doc/admin-guide/plugins/lua.en.rst b/doc/admin-guide/plugins/lua.en.rst index 8da1cdea98e..81a305a1243 100644 --- a/doc/admin-guide/plugins/lua.en.rst +++ b/doc/admin-guide/plugins/lua.en.rst @@ -22,13 +22,24 @@ Lua Plugin ********** -This module embeds Lua, via the standard Lua 5.1 interpreter, into |ATS|. With +This module embeds Lua, via the LuaJIT engine (>2.0.4), into |ATS|. With this module, we can implement ATS plugin by writing Lua script instead of C code. Lua code executed using this module can be 100% non-blocking because the powerful Lua coroutines have been integrated into the ATS event model. -Synopsis -======== +Installation +============ + +This plugin is only built if LuaJIT (>2.0.4) is installed. The configure option + +:: + + --with-luajit= + +can be used to specify a LuaJIT install. Otherwise, configure will use pkg-config to find a viable installation. + +Example Scripts +=============== **test_hdr.lua** @@ -64,20 +75,24 @@ Synopsis return 0 end - -Installation -============ - -This plugin is only built if LuaJIT (>2.0.4) is installed. The configure option +**test_global_hdr.lua** :: - --with-luajit= + function send_response() + ts.client_response.header['Rhost'] = ts.ctx['rhost'] + return 0 + end -can be used to specify a LuaJIT install. Otherwise, configure will use pkg-config to find a viable installation. + function do_global_read_request() + local req_host = ts.client_request.header.Host + ts.ctx['rhost'] = string.reverse(req_host) + ts.hook(TS_LUA_HOOK_SEND_RESPONSE_HDR, send_response) + end -Configuration -============= + +Usage with Example Scripts +========================== This module acts as remap plugin of Traffic Server, so we should realize 'do_remap' or 'do_os_response' function in each lua script. The path referencing a file with the lua script can be relative to the configuration directory or an absolute @@ -115,6 +130,10 @@ We can write this in plugin.config: tslua.so /etc/trafficserver/script/test_global_hdr.lua + +Configuration for number of Lua states +====================================== + We can also define the number of Lua states to be used for the plugin. If it is used as global plugin, we can write the following in plugin.config @@ -138,6 +157,16 @@ adding a configuration option to records.config. Any per plugin --states value overrides this default value but must be less than or equal to this value. This setting is not reloadable since it must be applied when all the lua states are first initialized. +Configuration for JIT mode +========================== + +We can also turn off JIT mode for LuaJIT when it is acting as global plugin for Traffic Server. The default is on (1). We can write this in plugin.config to turn off JIT + +:: + + tslua.so --jit=0 /etc/trafficserver/script/test_global_hdr.lua + + Profiling ========= From a26fdf6e66b9cd2d66a902765dd115ac6b92a496 Mon Sep 17 00:00:00 2001 From: Kit Chan Date: Tue, 18 Jan 2022 23:30:55 -0800 Subject: [PATCH 3/3] Fix clang format --- plugins/lua/ts_lua.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/lua/ts_lua.c b/plugins/lua/ts_lua.c index b233962f05b..059d545116f 100644 --- a/plugins/lua/ts_lua.c +++ b/plugins/lua/ts_lua.c @@ -799,7 +799,7 @@ TSPluginInit(int argc, const char *argv[]) for (int index = 0; index < ts_lua_max_state_count; ++index) { ts_lua_main_ctx *const main_ctx = (ts_lua_g_main_ctx_array + index); lua_State *const lstate = main_ctx->lua; - if (luaJIT_setmode(lstate, 0, LUAJIT_MODE_ENGINE|LUAJIT_MODE_OFF) == 0) { + if (luaJIT_setmode(lstate, 0, LUAJIT_MODE_ENGINE | LUAJIT_MODE_OFF) == 0) { TSError("[ts_lua][%s] Failed to disable JIT mode", __FUNCTION__); } }