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 E1CCA6FC8F; Sun, 12 Sep 2021 17:51:49 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org E1CCA6FC8F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1631458310; bh=VWC4+3FVykDqSI+9cGIRIncQiO7+3EGhtzr4S4komEQ=; h=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=E8oxplcFAzQVKGODBE41Usgj3h+TJUB+xqXwV0jXpmEq3B6Uym1WjN1rBZqUFalCw Ln6LvgMRiPT7vL10tJFcjy51lrmZB7rut6m06xZ0UCCml631UchiL9CQ/kNNF8+Otu jKq4yGlHaLT2EwlAgOAsHFNpjt+LEgwhwJvJiN8I= Received: from mail-lj1-f169.google.com (mail-lj1-f169.google.com [209.85.208.169]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id B393A6FC8F for ; Sun, 12 Sep 2021 17:51:47 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org B393A6FC8F Received: by mail-lj1-f169.google.com with SMTP id s12so12382425ljg.0 for ; Sun, 12 Sep 2021 07:51:47 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=aUuanaxb10uRJ45Oz61w0d1f+h9vRXnFeS6gV4IAvUU=; b=VivnpIeZB+m8iqdGzZtZ03x6h/mi/7Y4oVeF1yGA17S39mJLrSbAc8lw5ZfM81TFuK hSxgevFZuAnbgeY83W5rAnU9ZzxWt8/G5RDU5FNI0wk6IRWxx90FE7vMM9HOsZOSDwZY n6OBIkRe3ktbQz6Lg0RUCIEH3KeidAgNtGEMrpRqhevtfrOJT/sJr3fLVRcFXgGJJltR KQEhosrrJhQUTJ55VMsqFyJyqx7h3k7TznOg13LbQiP4BeKBXEafpzVeDucPTX300wdE Cmn7U66IaZJz5YFu9iHNAlY345++VdQLuls9ZY73HDdpO/mihTyuz5VbS24PT0UhoTpz u0yQ== X-Gm-Message-State: AOAM533j5OpODbYzQpzdOJjmcVvdm3cje+7Qetjy30juH0Ted8ds+n6j DiChFzq1a5Nvb2X5jW+Hqzbw/U5jF9M6fw== X-Google-Smtp-Source: ABdhPJzQfvDSZDCLrIu3QF5HDfH2Mm1CnD/1WHo2bIJtFMlW8dUROsKqjdB0FYw8FD2QbUEDYsvJ0g== X-Received: by 2002:a2e:808a:: with SMTP id i10mr6414331ljg.396.1631458306763; Sun, 12 Sep 2021 07:51:46 -0700 (PDT) Received: from localhost.localdomain ([2a00:1fa0:a66:4655:79c7:43af:35f4:cdbb]) by smtp.gmail.com with ESMTPSA id b4sm538021lfo.91.2021.09.12.07.51.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 12 Sep 2021 07:51:46 -0700 (PDT) X-Google-Original-From: Maxim Kokryashkin To: tarantool-patches@dev.tarantool.org, imun@tarantool.org, skaplun@tarantool.org Date: Sun, 12 Sep 2021 17:51:18 +0300 Message-Id: <20210912145118.392561-1-m.kokryashkin@tarantool.org> X-Mailer: git-send-email 2.33.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [Tarantool-patches] [PATCH luajit v2] memprof: introduce cli flag to run dump parser 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: Maxim Kokryashkin via Tarantool-patches Reply-To: Maxim Kokryashkin Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" It is really unconvinient to use a standalone shell script to parse memprof dump. That is why this commit intoroduces a CLI flag for tools to the LuaJIT, so now it is possible to parse memprof dump as simple as: ``` luajit -tm memprof.bin ``` Closes tarantool/tarantool#5688 --- GitHub branch: https://github.com/tarantool/luajit/tree/fckxorg/gh-5688-cli-for-memprof-parse Issue: https://github.com/tarantool/tarantool/issues/5688 Also, I am not sure whether it is ok to do that[1] trick with `argn` or not. It seems reasonable to run tools using the `runargs` method, but then the `argn` should be adjusted in a such way, that the `createargtable` will save parameters for the tool and 'runargs' will execute the script itself. [1]: https://github.com/tarantool/luajit/blob/8c074ebe7336a27f15484f1bb7eff757fa92c2f4/src/luajit.c#L565 CMakeLists.txt | 9 ++++---- src/CMakeLists.txt | 5 +++++ src/lj_tools_conf.h.in | 6 ++++++ src/luajit.c | 49 ++++++++++++++++++++++++++++++++---------- tools/CMakeLists.txt | 2 ++ 5 files changed, 56 insertions(+), 15 deletions(-) create mode 100644 src/lj_tools_conf.h.in diff --git a/CMakeLists.txt b/CMakeLists.txt index 5348e043..74826095 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -250,6 +250,11 @@ endif() # related compiler and linker flags passed. This should be done # the right way later. +# --- Tools -------------------------------------------------------------------- + +add_subdirectory(tools) +set(LUAJIT_TOOLS_DIR "${LUAJIT_TOOLS_DIR}") + # --- Main source tree --------------------------------------------------------- add_subdirectory(src) @@ -258,10 +263,6 @@ add_subdirectory(src) add_subdirectory(etc) -# --- Tools -------------------------------------------------------------------- - -add_subdirectory(tools) - # --- Testing source tree ------------------------------------------------------ # Auxiliary options for testing. diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 809aac68..d9debccf 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -142,6 +142,8 @@ make_source_list(SOURCES_CORE_NO_JIT_FFI ${SOURCES_UTILS} ) +configure_file(${CMAKE_CURRENT_SOURCE_DIR}/lj_tools_conf.h.in ${CMAKE_CURRENT_SOURCE_DIR}/lj_tools_conf.h) + set(SOURCES_CORE ${SOURCES_CORE_NO_JIT_FFI}) # Build JIT sources if JIT support is enabled. @@ -248,6 +250,9 @@ add_custom_target( jit/vmdef.lua ) +# --- Generate luajit tools config header ------------------------------------- +configure_file(${CMAKE_CURRENT_SOURCE_DIR}/lj_tools_conf.h.in ${CMAKE_CURRENT_SOURCE_DIR}/lj_tools_conf.h) + # --- Generate core and VM object files --------------------------------------- # Virtual machine. diff --git a/src/lj_tools_conf.h.in b/src/lj_tools_conf.h.in new file mode 100644 index 00000000..366c3ec4 --- /dev/null +++ b/src/lj_tools_conf.h.in @@ -0,0 +1,6 @@ +#ifndef LJ_TOOLS_CONF_H +#define LJ_TOOLS_CONF_H + +#define PARSER_PATH "@LUAJIT_TOOLS_DIR@/memprof.lua" + +#endif diff --git a/src/luajit.c b/src/luajit.c index 1ca24301..525797a5 100644 --- a/src/luajit.c +++ b/src/luajit.c @@ -19,6 +19,8 @@ #include "lj_arch.h" +#include "lj_tools_conf.h" + #if LJ_TARGET_POSIX #include #define lua_stdin_is_tty() isatty(0) @@ -72,6 +74,7 @@ static void print_usage(void) " -O[opt] Control LuaJIT optimizations.\n" " -i Enter interactive mode after executing " LUA_QL("script") ".\n" " -v Show version information.\n" + " -t[cmd] Execute tool.\n" " -E Ignore environment variables.\n" " -- Stop handling options.\n" " - Execute stdin and stop handling options.\n", stderr); @@ -266,21 +269,17 @@ static void dotty(lua_State *L) progname = oldprogname; } -static int handle_script(lua_State *L, char **argx) +static int call_script(lua_State *L, const char *fname) { - int status; - const char *fname = argx[0]; - if (strcmp(fname, "-") == 0 && strcmp(argx[-1], "--") != 0) - fname = NULL; /* stdin */ - status = luaL_loadfile(L, fname); + int status = luaL_loadfile(L, fname); if (status == LUA_OK) { /* Fetch args from arg table. LUA_INIT or -e might have changed them. */ int narg = 0; lua_getglobal(L, "arg"); if (lua_istable(L, -1)) { do { - narg++; - lua_rawgeti(L, -narg, narg); + narg++; + lua_rawgeti(L, -narg, narg); } while (!lua_isnil(L, -1)); lua_pop(L, 1); lua_remove(L, -narg); @@ -290,6 +289,16 @@ static int handle_script(lua_State *L, char **argx) } status = docall(L, narg, 0); } + return status; +} + +static int handle_script(lua_State *L, char **argx) +{ + int status; + const char *fname = argx[0]; + if (strcmp(fname, "-") == 0 && strcmp(argx[-1], "--") != 0) + fname = NULL; /* stdin */ + status = call_script(L, fname); return report(L, status); } @@ -361,6 +370,15 @@ static int dojitcmd(lua_State *L, const char *cmd) return runcmdopt(L, opt ? opt+1 : opt); } +static int dotoolcmd(lua_State *L, const char *cmd) +{ + if(strcmp(cmd, "m") == 0) { + const int status = call_script(L, PARSER_PATH); + return status == LUA_OK; + } + return -1; +} + /* Optimization flags. */ static int dojitopt(lua_State *L, const char *opt) { @@ -398,6 +416,7 @@ static int dobytecode(lua_State *L, char **argv) #define FLAGS_EXEC 4 #define FLAGS_OPTION 8 #define FLAGS_NOENV 16 +#define FLAGS_TOOL 32 static int collectargs(char **argv, int *flags) { @@ -419,14 +438,19 @@ static int collectargs(char **argv, int *flags) notail(argv[i]); *flags |= FLAGS_VERSION; break; + case 't': + *flags |= FLAGS_TOOL; + if (argv[i][2] == '\0') return -1; + if (argv[i + 1] == NULL) return -1; + return i + 1; case 'e': *flags |= FLAGS_EXEC; case 'j': /* LuaJIT extension */ case 'l': *flags |= FLAGS_OPTION; if (argv[i][2] == '\0') { - i++; - if (argv[i] == NULL) return -1; + i++; + if (argv[i] == NULL) return -1; } break; case 'O': break; /* LuaJIT extension */ @@ -474,6 +498,9 @@ static int runargs(lua_State *L, char **argv, int argn) return 1; break; } + case 't' : + const char *cmd = argv[i] + 2; + return dotoolcmd(L, cmd) == LUA_OK; case 'O': /* LuaJIT extension. */ if (dojitopt(L, argv[i] + 2)) return 1; @@ -535,7 +562,7 @@ static int pmain(lua_State *L) luaL_openlibs(L); lua_gc(L, LUA_GCRESTART, -1); - createargtable(L, argv, s->argc, argn); + createargtable(L, argv, s->argc, (flags & FLAGS_TOOL) ? argn - 1 : argn); if (!(flags & FLAGS_NOENV)) { s->status = handle_luainit(L); diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index 61830e44..84129153 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -16,6 +16,7 @@ else() # path where LuaJIT binary is located. set(LUAJIT_TOOLS_BIN ${LUAJIT_BINARY_DIR}/${LUAJIT_CLI_NAME}) set(LUAJIT_TOOLS_DIR ${CMAKE_CURRENT_SOURCE_DIR}) + set(LUAJIT_TOOLS_DIR ${LUAJIT_TOOLS_DIR} PARENT_SCOPE) # XXX: Unfortunately, there is no convenient way to set # particular permissions to the output file via CMake. # Furthermore, I even failed to copy the given file to the same @@ -75,6 +76,7 @@ else() " set(LUAJIT_TOOLS_BIN ${CMAKE_INSTALL_PREFIX}/bin/${LUAJIT_CLI_NAME}) set(LUAJIT_TOOLS_DIR ${CMAKE_INSTALL_PREFIX}/${LUAJIT_DATAROOTDIR}) + set(LUAJIT_TOOLS_DIR ${LUAJIT_TOOLS_DIR} PARENT_SCOPE) configure_file(${CMAKE_CURRENT_SOURCE_DIR}/luajit-parse-memprof.in ${PROJECT_BINARY_DIR}/luajit-parse-memprof @ONLY ESCAPE_QUOTES) file(INSTALL ${PROJECT_BINARY_DIR}/luajit-parse-memprof -- 2.33.0