<HTML><BODY><div>Yikes! And the GitHub branch too:</div><div>https://github.com/tarantool/luajit/tree/fckxorg/gh-5688-cli-for-memprof-parse</div><div> </div><blockquote style="border-left:1px solid #0857A6; margin:10px; padding:0 0 0 10px;"><div> <blockquote style="border-left:1px solid #0857A6; margin:10px; padding:0 0 0 10px;"><div id=""><div class="js-helper js-readmsg-msg"><div><div id="style_16275596160441448238_BODY"><div class="cl_578529"><div>Sorry, I forgot to add the ‘LuaJIT’ tag to the subject</div><div> <div> </div> <blockquote style="border-left:1px solid #0857A6;margin:10px;padding:0 0 0 10px;"><div id=""><div class="js-helper_mr_css_attr js-readmsg-msg_mr_css_attr"><div><div id="style_16275594951902263950_BODY_mr_css_attr">It is inconvenient to use a standalone shell script to parse a memprof<br>dump. That is why this commit adds a CLI flag to call the parser into<br>the LuaJIT, so now it is possible to parse a dump as simple as:<br>```<br>luajit -m memprof.bin<br>```<br><br>Closes tarantool/tarantool#5688<br>---<br> CMakeLists.txt | 8 +++++---<br> src/CMakeLists.txt | 5 +++++<br> src/lj_tools_conf.h.in | 6 ++++++<br> src/luajit.c | 36 ++++++++++++++++++++++++++++--------<br> tools/CMakeLists.txt | 2 ++<br> 5 files changed, 46 insertions(+), 11 deletions(-)<br> create mode 100644 src/lj_tools_conf.h.in<br><br>diff --git a/CMakeLists.txt b/CMakeLists.txt<br>index 5348e043..619e9441 100644<br>--- a/CMakeLists.txt<br>+++ b/CMakeLists.txt<br>@@ -250,6 +250,11 @@ endif()<br> # related compiler and linker flags passed. This should be done<br> # the right way later.<br> <br>+# --- Tools --------------------------------------------------------------------<br>+<br>+add_subdirectory(tools)<br>+set(LUAJIT_TOOLS_DIR "${LUAJIT_TOOLS_DIR}")<br>+<br> # --- Main source tree ---------------------------------------------------------<br> <br> add_subdirectory(src)<br>@@ -258,9 +263,6 @@ add_subdirectory(src)<br> <br> add_subdirectory(etc)<br> <br>-# --- Tools --------------------------------------------------------------------<br>-<br>-add_subdirectory(tools)<br> <br> # --- Testing source tree ------------------------------------------------------<br> <br>diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt<br>index 809aac68..d9debccf 100644<br>--- a/src/CMakeLists.txt<br>+++ b/src/CMakeLists.txt<br>@@ -142,6 +142,8 @@ make_source_list(SOURCES_CORE_NO_JIT_FFI<br> ${SOURCES_UTILS}<br> )<br> <br>+configure_file(${CMAKE_CURRENT_SOURCE_DIR}/lj_tools_conf.h.in ${CMAKE_CURRENT_SOURCE_DIR}/lj_tools_conf.h)<br>+<br> set(SOURCES_CORE ${SOURCES_CORE_NO_JIT_FFI})<br> <br> # Build JIT sources if JIT support is enabled.<br>@@ -248,6 +250,9 @@ add_custom_target(<br> jit/vmdef.lua<br> )<br> <br>+# --- Generate luajit tools config header -------------------------------------<br>+configure_file(${CMAKE_CURRENT_SOURCE_DIR}/lj_tools_conf.h.in ${CMAKE_CURRENT_SOURCE_DIR}/lj_tools_conf.h)<br>+<br> # --- Generate core and VM object files ---------------------------------------<br> <br> # Virtual machine.<br>diff --git a/src/lj_tools_conf.h.in b/src/lj_tools_conf.h.in<br>new file mode 100644<br>index 00000000..366c3ec4<br>--- /dev/null<br>+++ b/src/lj_tools_conf.h.in<br>@@ -0,0 +1,6 @@<br>+#ifndef LJ_TOOLS_CONF_H<br>+#define LJ_TOOLS_CONF_H<br>+<br>+#define PARSER_PATH "@LUAJIT_TOOLS_DIR@/memprof.lua"<br>+<br>+#endif<br>diff --git a/src/luajit.c b/src/luajit.c<br>index 1ca24301..6697d00f 100644<br>--- a/src/luajit.c<br>+++ b/src/luajit.c<br>@@ -19,6 +19,8 @@<br> <br> #include "lj_arch.h"<br> <br>+#include "lj_tools_conf.h"<br>+<br> #if LJ_TARGET_POSIX<br> #include <unistd.h><br> #define lua_stdin_is_tty() isatty(0)<br>@@ -72,6 +74,7 @@ static void print_usage(void)<br> " -O[opt] Control LuaJIT optimizations.\n"<br> " -i Enter interactive mode after executing " LUA_QL("script") ".\n"<br> " -v Show version information.\n"<br>+ " -m Parse memprof profile data.\n"<br> " -E Ignore environment variables.\n"<br> " -- Stop handling options.\n"<br> " - Execute stdin and stop handling options.\n", stderr);<br>@@ -266,21 +269,17 @@ static void dotty(lua_State *L)<br> progname = oldprogname;<br> }<br> <br>-static int handle_script(lua_State *L, char **argx)<br>+static int call_script(lua_State *L, const char *fname)<br> {<br>- int status;<br>- const char *fname = argx[0];<br>- if (strcmp(fname, "-") == 0 && strcmp(argx[-1], "--") != 0)<br>- fname = NULL; /* stdin */<br>- status = luaL_loadfile(L, fname);<br>+ int status = luaL_loadfile(L, fname);<br> if (status == LUA_OK) {<br> /* Fetch args from arg table. LUA_INIT or -e might have changed them. */<br> int narg = 0;<br> lua_getglobal(L, "arg");<br> if (lua_istable(L, -1)) {<br> do {<br>- narg++;<br>- lua_rawgeti(L, -narg, narg);<br>+ narg++;<br>+ lua_rawgeti(L, -narg, narg);<br> } while (!lua_isnil(L, -1));<br> lua_pop(L, 1);<br> lua_remove(L, -narg);<br>@@ -290,6 +289,16 @@ static int handle_script(lua_State *L, char **argx)<br> }<br> status = docall(L, narg, 0);<br> }<br>+ return status;<br>+}<br>+<br>+static int handle_script(lua_State *L, char **argx)<br>+{<br>+ int status;<br>+ const char *fname = argx[0];<br>+ if (strcmp(fname, "-") == 0 && strcmp(argx[-1], "--") != 0)<br>+ fname = NULL; /* stdin */<br>+ call_script(L, fname);<br> return report(L, status);<br> }<br> <br>@@ -398,6 +407,7 @@ static int dobytecode(lua_State *L, char **argv)<br> #define FLAGS_EXEC 4<br> #define FLAGS_OPTION 8<br> #define FLAGS_NOENV 16<br>+#define FLAGS_MEMPROF_PARSE 32<br> <br> static int collectargs(char **argv, int *flags)<br> {<br>@@ -411,6 +421,10 @@ static int collectargs(char **argv, int *flags)<br> return i+1;<br> case '\0':<br> return i;<br>+ case 'm':<br>+ notail(argv[i]);<br>+ *flags |= FLAGS_MEMPROF_PARSE;<br>+ return i;<br> case 'i':<br> notail(argv[i]);<br> *flags |= FLAGS_INTERACTIVE;<br>@@ -547,6 +561,12 @@ static int pmain(lua_State *L)<br> s->status = runargs(L, argv, argn);<br> if (s->status != LUA_OK) return 0;<br> <br>+ if((flags & FLAGS_MEMPROF_PARSE)) {<br>+ //char* parser_path = "/usr/local/share/luajit-2.1.0-beta3/memprof.lua";<br>+ s->status = report(L, call_script(L, PARSER_PATH));<br>+ return s->status == LUA_OK;<br>+ }<br>+<br> if (s->argc > argn) {<br> s->status = handle_script(L, argv + argn);<br> if (s->status != LUA_OK) return 0;<br>diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt<br>index 61830e44..84129153 100644<br>--- a/tools/CMakeLists.txt<br>+++ b/tools/CMakeLists.txt<br>@@ -16,6 +16,7 @@ else()<br> # path where LuaJIT binary is located.<br> set(LUAJIT_TOOLS_BIN ${LUAJIT_BINARY_DIR}/${LUAJIT_CLI_NAME})<br> set(LUAJIT_TOOLS_DIR ${CMAKE_CURRENT_SOURCE_DIR})<br>+ set(LUAJIT_TOOLS_DIR ${LUAJIT_TOOLS_DIR} PARENT_SCOPE)<br> # XXX: Unfortunately, there is no convenient way to set<br> # particular permissions to the output file via CMake.<br> # Furthermore, I even failed to copy the given file to the same<br>@@ -75,6 +76,7 @@ else()<br> "<br> set(LUAJIT_TOOLS_BIN ${CMAKE_INSTALL_PREFIX}/bin/${LUAJIT_CLI_NAME})<br> set(LUAJIT_TOOLS_DIR ${CMAKE_INSTALL_PREFIX}/${LUAJIT_DATAROOTDIR})<br>+ set(LUAJIT_TOOLS_DIR ${LUAJIT_TOOLS_DIR} PARENT_SCOPE)<br> configure_file(${CMAKE_CURRENT_SOURCE_DIR}/luajit-parse-memprof.in<br> ${PROJECT_BINARY_DIR}/luajit-parse-memprof @ONLY ESCAPE_QUOTES)<br> file(INSTALL ${PROJECT_BINARY_DIR}/luajit-parse-memprof<br>--<br>2.32.0</div></div></div></div></blockquote><div> </div></div></div></div></div></div></div></blockquote><div> </div></div></blockquote></BODY></HTML>