From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id 4A5C670C99; Thu, 2 Sep 2021 16:57:35 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 4A5C670C99 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1630591055; bh=gLTNuupyIBQSOrgLCDEwIUf5b0fUkCT+mmTQz8yWyeE=; h=Date:To:References:In-Reply-To:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=DIBxOt3x+j+d1eAKsOHS56ueDDTGxQ15hpAT7g0EqBDbk3LpakqPWTpDnIfRHTbN4 Ybuv80zjZHSbHr39qjWACW03WTirVAT/pELq6ZBXyLwUZx05q3QTuOLpB5QjBVNfH+ n2FZotAhz2iMDyVFIV8E4P84wtq3sEV5BhYS52UE= Received: from smtp52.i.mail.ru (smtp52.i.mail.ru [94.100.177.112]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id 4A06270C99 for ; Thu, 2 Sep 2021 16:57:34 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 4A06270C99 Received: by smtp52.i.mail.ru with esmtpa (envelope-from ) id 1mLnDh-0004tP-5j; Thu, 02 Sep 2021 16:57:33 +0300 Date: Thu, 2 Sep 2021 16:56:11 +0300 To: Maxim Kokryashkin Message-ID: References: <20210812175351.443616-1-m.kokryashkin@tarantool.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210812175351.443616-1-m.kokryashkin@tarantool.org> X-4EC0790: 10 X-7564579A: B8F34718100C35BD X-77F55803: 4F1203BC0FB41BD9D96C1EA41D18F4D5409838B14BB7DC4567FB6F44CFDC9DC8182A05F53808504007B6A89C4A5B1449562EFDBA02EF073CC272780802433CDA1ACF7766C6CC52C4 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7D77100FFB2844417EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F790063791F3A0CD9A153DC48638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D879FEE276AE4E82776C6B6A34E15E0D3A117882F4460429724CE54428C33FAD305F5C1EE8F4F765FCAA867293B0326636D2E47CDBA5A96583BD4B6F7A4D31EC0BC014FD901B82EE079FA2833FD35BB23D27C277FBC8AE2E8BAA867293B0326636D2E47CDBA5A96583BA9C0B312567BB231DD303D21008E29813377AFFFEAFD269A417C69337E82CC2E827F84554CEF50127C277FBC8AE2E8BA83251EDC214901ED5E8D9A59859A8B6AC294AFEFA671E80089D37D7C0E48F6C5571747095F342E88FB05168BE4CE3AF X-C1DE0DAB: 0D63561A33F958A5B0B7BA98A592E44C4FE95FDD4B2B9B76AB0933689511EB4AD59269BC5F550898D99A6476B3ADF6B47008B74DF8BB9EF7333BD3B22AA88B938A852937E12ACA752546FE575EB473F1410CA545F18667F91A7EA1CDA0B5A7A0 X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34B5900AD87B4159A4734916AF5E6F4F666141E045DDBE36372FC40382A546FF7D52A5A38F922B2B471D7E09C32AA3244C56B7E7E9BB53F171C273BA7353FEE0BC1E098CBE561D6343927AC6DF5659F194 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojK/h3ZnmlPjEmKQyzTtGiFw== X-Mailru-Sender: 3B9A0136629DC91206CBC582EFEF4CB4AB65BA8B4ECAFBC441E2AD09919D1F43FE933C2FEF60FCDEF2400F607609286E924004A7DEC283833C7120B22964430C52B393F8C72A41A89437F6177E88F7363CDA0F3B3F5B9367 X-Mras: Ok Subject: Re: [Tarantool-patches] [PATCH] luajit: proxy -j and -b flags X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Sergey Kaplun via Tarantool-patches Reply-To: Sergey Kaplun Cc: tarantool-patches@dev.tarantool.org Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" Hi, Maxim! Thanks for the patch! Please, consider my comments below. On 12.08.21, Maxim Kokryashkin wrote: > There are two flags in the LuaJIT useful for debugging purposes: `-j` > and `-b`. However, if you want to check the same Lua code from the > Tarantool, you will need to make some adjustments in the script itself, > as those flags are not present in the Tarantool's CLI. > > This patch introduces those flags to the Tarantool, so debugging is > much more convenient now. Flags are working the same as they do in > LuaJIT. > > Closes tarantool/tarantool#5541 Minor: As far as it is tarantool repo there is no need to use the full path. #5541 is enough. > --- > Github branch: > https://github.com/tarantool/tarantool/tree/fckxorg/gh-5541-proxy-luajit-flags > > Issue: > https://github.com/tarantool/tarantool/issues/5541 > > src/lua/init.c | 160 +++++++++++++++++++++++++++++++++++++++++++++ > src/lua/init.h | 3 + > src/main.cc | 123 +++++++++++++++++++++------------- > third_party/luajit | 2 +- I don't get from the commit message why do we need to bump LuaJIT version here (I strongly doubt that we need it)? > 4 files changed, 241 insertions(+), 47 deletions(-) Sorry, the patch is not well formated, so it is hard to read the part. Some general thoughts: Please rewrite your code considering our code style guide [1][2]. This means: 1) In C use tabs, not whitespaces. Tab width should be 8 symbols. In Lua use whitespaces. Indentation step is 4 whitespaces; 2) In C comments out of functions and inside of functions should be different in how they are started. Everything else is wrong. Below are correct examples. `/**` comes for documentation comments, `/*` for local not documented comments. However the difference is vague already, so the rule is simple - out of function = `/**`, inside = `/*`. 3) Remove trailing whitespaces. 4) Use separate line for comments. Also, it would be nice to see some tests for new functionality (even the dummy one). Please fix the following comments and resend patch in v2. > > diff --git a/src/lua/init.c b/src/lua/init.c > index f9738025d..16c8bc4b5 100644 > --- a/src/lua/init.c > +++ b/src/lua/init.c > @@ -313,6 +313,127 @@ skip: base = (base == -1 ? 10 : base); > > /* }}} */ > > +static void l_message(const char *msg) > +{ > + fputs(msg, stderr); fputc('\n', stderr); Minor: Use separate line here. XXX: Also, I don't sure about stderr usage: For example, Tarantool report error in stdout, when the script name is invalid. > + fflush(stderr); > +} > + > +static int report(lua_State *L, int status) > +{ > + if (status && !lua_isnil(L, -1)) { > + const char *msg = lua_tostring(L, -1); > + if (msg == NULL) msg = "(error object is not a string)"; Minor: use separate line here. Feel free to ignore. > + l_message(msg); > + lua_pop(L, 1); > + } > + return status; > +} > + > +/* Load add-on module. */ Please, describe values on Lua stack before and after this call. It should be mentioned for each function, which works with lua_State. > +static int loadjitmodule(lua_State* L) > +{ > + lua_getglobal(L, "require"); > + lua_pushliteral(L, "jit."); > + lua_pushvalue(L, -3); > + lua_concat(L, 2); > + if (lua_pcall(L, 1, 1, 0)) { > + const char *msg = lua_tostring(L, -1); > + if (msg && !strncmp(msg, "module ", 7)) > + goto nomodule; > + return report(L, 1); > + } > + lua_getfield(L, -1, "start"); > + if (lua_isnil(L, -1)) { > + nomodule: > + l_message("unknown luaJIT command or jit.* modules not installed"); Side note: I've thought it should be LuaJIT here, but it's the same in the LuaJIT sources. > + return 1; > + } > + lua_remove(L, -2); /* Drop module table. */ > + return 0; > +} > + > +int dobytecode(va_list ap) Why can't we call this function on tarantool_L state, as it is done for -j flag? > +{ > + struct lua_State *L = va_arg(ap, struct lua_State *); > + char **argv = va_arg(ap, char **); > + struct diag *diag = va_arg(ap, struct diag *); > + > + int narg = 0; > + bool aux_loop_is_run = false; > + > + lua_pushliteral(L, "bcsave"); > + if (loadjitmodule(L)) > + goto error; > + if (argv[0][2]) { > + narg++; > + argv[0][1] = '-'; > + lua_pushstring(L, argv[0]+1); Typo: s/argv[0]+1/argv[0] + 1/ > + } > + > + fiber_sleep(0.0); Why do we need fiber_sleep here? Please, drop a comment. > + aux_loop_is_run = true; > + for (argv++; *argv != NULL; narg++, argv++) > + lua_pushstring(L, *argv); > + int res = lua_pcall(L, narg, 0, 0); > + if(res) Typo: s/if(/if (/ Minor: use LUA_OK, to check result of lua_pcall. > + goto error; > +end: > + if (!aux_loop_is_run) > + fiber_sleep(0.0); Why do we need fiber_sleep here? Please, drop a comment. > + ev_break(loop(), EVBREAK_ALL); > + return 0; > + > +error: > + diag_move(diag_get(), diag); > + goto end; > +} > + > +/* Run command with options. */ > +static int runcmdopt(lua_State *L, const char *opt) > +{ > + int narg = 0; > + if (opt && *opt) { > + for (;;) { /* Split arguments. */ > + const char *p = strchr(opt, ','); > + narg++; > + if (!p) break; > + if (p == opt) > + lua_pushnil(L); > + else > + lua_pushlstring(L, opt, (size_t)(p - opt)); > + opt = p + 1; > + } > + if (*opt) > + lua_pushstring(L, opt); > + else > + lua_pushnil(L); > + } > + return report(L, lua_pcall(L, narg, 0, 0)); > +} > + > +/* JIT engine control command: try jit library first or load add-on module. */ > +int dojitcmd(const char *cmd) > +{ > + const char *opt = strchr(cmd, '='); > + lua_pushlstring(tarantool_L, cmd, opt ? (size_t)(opt - cmd) : strlen(cmd)); > + lua_getfield(tarantool_L, LUA_REGISTRYINDEX, "_LOADED"); > + lua_getfield(tarantool_L, -1, "jit"); /* Get jit.* module table. */ > + lua_remove(tarantool_L, -2); > + lua_pushvalue(tarantool_L, -2); > + lua_gettable(tarantool_L, -2); /* Lookup library function. */ > + if (!lua_isfunction(tarantool_L, -1)) { > + lua_pop(tarantool_L, 2); /* Drop non-function and jit.* table, keep module name. */ > + if (loadjitmodule(tarantool_L)) > + return 1; > + } else { > + lua_remove(tarantool_L, -2); /* Drop jit.* table. */ > + } > + lua_remove(tarantool_L, -2); /* Drop module name. */ > + return runcmdopt(tarantool_L, opt ? opt+1 : opt); Typo: s/opt+1/opt + 1/ > +} > + > + Typo: extra empty line. > /** > * Original LuaJIT/Lua logic: > * > @@ -608,6 +729,10 @@ run_script_f(va_list ap) > lua_setglobal(L, optv[i + 1]); > lua_settop(L, 0); > break; > + case 'j': > + if (dojitcmd(optv[i + 1]) != 0) > + goto error; > + break; > case 'e': > /* > * Execute chunk > @@ -747,6 +872,39 @@ tarantool_lua_run_script(char *path, bool interactive, > return diag_is_empty(diag_get()) ? 0 : -1; > } > > +int > +tarantool_lua_dump_bytecode(char **argv) { > + script_fiber = fiber_new("bcsave", dobytecode); IINM, bcsave module don't run a user's code, just precompile it to the bytecode. Do we need a separate fiber for it? > + if (script_fiber == NULL) > + panic("%s", diag_last_error(diag_get())->errmsg); > + script_fiber->storage.lua.stack = tarantool_L; > + /* > + * Create a new diag on the stack. Don't pass fiber's diag, because it > + * might be overwritten by libev callbacks invoked in the scheduler > + * fiber (which is this), and therefore can't be used as a sign of fail > + * in the script itself. > + */ > + struct diag bc_diag; > + diag_create(&bc_diag); > + fiber_start(script_fiber, tarantool_L, argv, &bc_diag); > + /* > + * Run an auxiliary event loop to re-schedule run_script fiber. > + * When this fiber finishes, it will call ev_break to stop the loop. > + */ > + if (start_loop) > + ev_run(loop(), 0); > + /* The fiber running the startup script has ended. */ > + script_fiber = NULL; > + diag_move(&bc_diag, diag_get()); > + diag_destroy(&bc_diag); > + /* > + * Result can't be obtained via fiber_join - script fiber > + * never dies if os.exit() was called. This is why diag > + * is checked explicitly. > + */ > + return diag_is_empty(diag_get()) ? 0 : -1; > +} > + > void > tarantool_lua_free() > { > @@ -783,3 +941,5 @@ tarantool_lua_free() > } > #endif > } > + > + Looks like these changes are unnecessary. > diff --git a/src/lua/init.h b/src/lua/init.h > index 7fc0b1a31..4d049ecc8 100644 > --- a/src/lua/init.h > +++ b/src/lua/init.h > @@ -74,6 +74,9 @@ int > tarantool_lua_run_script(char *path, bool force_interactive, > int optc, const char **optv, > int argc, char **argv); > +int > +tarantool_lua_dump_bytecode(char **argv); > +int dojitcmd(const char *cmd); Why this function should be declared as public for this module if it is used only inside it? > > extern char *history; > > diff --git a/src/main.cc b/src/main.cc > index de082c17f..bd0314852 100644 > --- a/src/main.cc > +++ b/src/main.cc > } > > +enum { > + ARG_OK = 1, > + ARG_LJ = 2 > +}; > + > +enum { > + O_INTERACTIVE = 1, > + O_BYTECODE = 2 I suppose it will be better to declare this flags like the following: | #define OPT_INTERACTIVE 0x1 | #define OPT_BYTECODE 0x2 To hightlight that they are flags to be set. > +}; > + > +static int collect_tarantool_arg(int ch, uint32_t *opt_mask, int argc, char **argv, int *optc, const char ***optv) { Why do we need a new function with 6 arguments? > + switch (ch) { > + case 'V': > + case 'v': > + print_version(); > + return 0; > + case 'h': > + print_help(basename(argv[0])); > + return 0; > + case 'i': > + /* Force interactive mode */ > + *opt_mask |= O_INTERACTIVE; > + break; > + case 'b': > + *opt_mask |= O_BYTECODE; > + return ARG_LJ; > + case 'j': > + case 'l': > + case 'e': > + /* Save Lua interepter options to optv as is */ > + if (*optc == 0) { > + *optv = (const char **) calloc(argc, > + sizeof((*optv)[0])); > + if (*optv == NULL) > + panic_syserror("No enough memory for arguments"); > + } > + if(ch == 'l') (*optv)[(*optc)++] = "-l"; > + else if(ch == 'j') (*optv)[(*optc)++] = "-j"; > + else (*optv)[(*optc)++] = "-e"; > + (*optv)[(*optc)++] = optarg; > + break; > + default: > + /* "invalid option" is printed by getopt */ > + return EX_USAGE; > + } > + return ARG_OK; > +} > + > extern "C" void ** > export_syms(void); > > @@ -598,7 +648,7 @@ main(int argc, char **argv) > fpconv_check(); > > /* Enter interactive mode after executing 'script' */ > - bool interactive = false; > + uint32_t opt_mask = 0; > /* Lua interpeter options, e.g. -e and -l */ > int optc = 0; > const char **optv = NULL; > @@ -609,58 +659,35 @@ main(int argc, char **argv) > {"version", no_argument, 0, 'v'}, > {NULL, 0, 0, 0}, > }; > - static const char *opts = "+hVvie:l:"; > + static const char *opts = "+hVvbij:e:l:"; > > int ch; > while ((ch = getopt_long(argc, argv, opts, longopts, NULL)) != -1) { > - switch (ch) { > - case 'V': > - case 'v': > - print_version(); > - return 0; > - case 'h': > - print_help(basename(argv[0])); > - return 0; > - case 'i': > - /* Force interactive mode */ > - interactive = true; > - break; > - case 'l': > - case 'e': > - /* Save Lua interepter options to optv as is */ > - if (optc == 0) { > - optv = (const char **) calloc(argc, > - sizeof(optv[0])); > - if (optv == NULL) > - panic_syserror("No enough memory for arguments"); > - } > - optv[optc++] = ch == 'l' ? "-l" : "-e"; > - optv[optc++] = optarg; > - break; > - default: > - /* "invalid option" is printed by getopt */ > - return EX_USAGE; > - } > + int res = collect_tarantool_arg(ch, &opt_mask, argc, argv, &optc, &optv); > + if(res == 0 || res == EX_USAGE) return res; > + if(res == ARG_LJ) break; > } > > /* Shift arguments */ > argc = 1 + (argc - optind); > for (int i = 1; i < argc; i++) > argv[i] = argv[optind + i - 1]; > - > - if (argc > 1 && strcmp(argv[1], "-") && access(argv[1], R_OK) != 0) { > - /* > - * Somebody made a mistake in the file > - * name. Be nice: open the file to set > - * errno. > - */ > - int fd = open(argv[1], O_RDONLY); > - int save_errno = errno; > - if (fd >= 0) > - close(fd); > - printf("Can't open script %s: %s\n", argv[1], strerror(save_errno)); > - return save_errno; > - } > + > + if(!(opt_mask & O_BYTECODE)) { > + if (argc > 1 && strcmp(argv[1], "-") && access(argv[1], R_OK) != 0) { > + /* > + * Somebody made a mistake in the file > + * name. Be nice: open the file to set > + * errno. > + */ > + int fd = open(argv[1], O_RDONLY); > + int save_errno = errno; > + if (fd >= 0) > + close(fd); > + printf("Can't open script %s: %s\n", argv[1], strerror(save_errno)); > + return save_errno; > + } > + } > > argv = title_init(argc, argv); > /* > @@ -751,13 +778,17 @@ main(int argc, char **argv) > panic("%s", "can't init event loop"); > > int events = ev_activecnt(loop()); > - /* > + > + if(opt_mask & O_BYTECODE) { > + return tarantool_lua_dump_bytecode(argv); > + } > + /* > * Load user init script. The script should have access > * to Tarantool Lua API (box.cfg, box.fiber, etc...) that > * is why script must run only after the server was fully > * initialized. > */ > - if (tarantool_lua_run_script(script, interactive, optc, optv, > + if (tarantool_lua_run_script(script, opt_mask & O_INTERACTIVE, optc, optv, > main_argc, main_argv) != 0) > diag_raise(); > /* > diff --git a/third_party/luajit b/third_party/luajit > index e7f701639..436898f4e 160000 > --- a/third_party/luajit > +++ b/third_party/luajit > @@ -1 +1 @@ > -Subproject commit e7f701639cc6900edeef4cec22776ea04e33895d > +Subproject commit 436898f4eb69902c1adf82a302717f47b48ffe2c > -- > 2.32.0 > [1]: https://www.tarantool.io/en/doc/latest/dev_guide/c_style_guide/ [2]: https://github.com/tarantool/tarantool/wiki/Code-review-procedure -- Best regards, Sergey Kaplun