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 D4EF36EC55; Thu, 29 Jul 2021 14:51:35 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org D4EF36EC55 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1627559495; bh=CSenPtRSdPLt7g8Y65VG7AtyoFFY8V2GQkhkqeAiQw0=; h=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=JAqDk27DpWBOWpiXVrGNNcA9M1fbAWF85McGQNQQFSZGNl6daDaNDi6uWlaEAtuwI 0gaB+vxdMnc9v27UWC3tZCKSDSCDf4r4caSI1oy/Mu0SsJVNKmW6o4tvcRWIoVhGGG UXMOyactTtMUZTyKtX0/+RRIFaHrdLr2F0yAv+Rk= Received: from mail-lf1-f48.google.com (mail-lf1-f48.google.com [209.85.167.48]) (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 8F9D56EC55 for ; Thu, 29 Jul 2021 14:51:34 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 8F9D56EC55 Received: by mail-lf1-f48.google.com with SMTP id r17so10505302lfe.2 for ; Thu, 29 Jul 2021 04:51:34 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=srShACkMaD7/Nq3jplTm3s6/FIycLVTV7e8oFPaM350=; b=ZjHP0l4jZjACoseKDMSejt49CYehOeV19vfsMuhhpwevbQov9XiYrLykTeHAbgpzyL Y6CO6HUOD5f0W4NQkloTeSAAG0qMo2Lk/JkDWkloHemUXk+uBKwPdaS6DQYEBEkqYeTd 66Cc0b4X7ZsuF+/z6/BPeEJK5WJdYbr4vLNIQjgqAfvQN5o8sun2huG52kPXTwL+o1kC ok5znqKKbMvxTi9fX9s2uzqy3v0nlMZvvdO0lmji7YgLJsxAPRTs0+55BUrd4FWXxEdY t3QAUxUSdcHEKtSvxSbHxcv+64sx+ZNWNRQH8vlXx/2XRYvAkKOp67R3LSmybF8yAbBo LaOQ== X-Gm-Message-State: AOAM530EGZhrYLUar81Z1Zmx/OLdv45loYLiKUr/AKbBqzjsN7XBB+25 iC6gSjydjYu/fU6Qo+OdLrUqM/HcsrGn3w== X-Google-Smtp-Source: ABdhPJyEmEwXluzovpCjR26KAulNJSeJC9XNkJYDoBThRqFSbmBWzVVIVQ0dwGK3ZsMiMsqmn84fpw== X-Received: by 2002:a05:6512:3619:: with SMTP id f25mr3530983lfs.260.1627559493725; Thu, 29 Jul 2021 04:51:33 -0700 (PDT) Received: from localhost.localdomain ([2a00:1370:8131:3d05:a2ac:147a:da9f:68e4]) by smtp.gmail.com with ESMTPSA id v10sm282492lfo.175.2021.07.29.04.51.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Jul 2021 04:51:33 -0700 (PDT) X-Google-Original-From: Maxim Kokryashkin To: tarantool-patches@dev.tarantool.org, imun@tarantool.org, skaplun@tarantool.org Date: Thu, 29 Jul 2021 14:51:26 +0300 Message-Id: <20210729115126.336490-1-m.kokryashkin@tarantool.org> X-Mailer: git-send-email 2.32.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [Tarantool-patches] [PATCH] memprof: introduce a 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 inconvenient to use a standalone shell script to parse a memprof dump. That is why this commit adds a CLI flag to call the parser into the LuaJIT, so now it is possible to parse a dump as simple as: ``` luajit -m memprof.bin ``` Closes tarantool/tarantool#5688 --- CMakeLists.txt | 8 +++++--- src/CMakeLists.txt | 5 +++++ src/lj_tools_conf.h.in | 6 ++++++ src/luajit.c | 36 ++++++++++++++++++++++++++++-------- tools/CMakeLists.txt | 2 ++ 5 files changed, 46 insertions(+), 11 deletions(-) create mode 100644 src/lj_tools_conf.h.in diff --git a/CMakeLists.txt b/CMakeLists.txt index 5348e043..619e9441 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,9 +263,6 @@ add_subdirectory(src) add_subdirectory(etc) -# --- Tools -------------------------------------------------------------------- - -add_subdirectory(tools) # --- Testing source tree ------------------------------------------------------ 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..6697d00f 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" + " -m Parse memprof profile data.\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 */ + call_script(L, fname); return report(L, status); } @@ -398,6 +407,7 @@ static int dobytecode(lua_State *L, char **argv) #define FLAGS_EXEC 4 #define FLAGS_OPTION 8 #define FLAGS_NOENV 16 +#define FLAGS_MEMPROF_PARSE 32 static int collectargs(char **argv, int *flags) { @@ -411,6 +421,10 @@ static int collectargs(char **argv, int *flags) return i+1; case '\0': return i; + case 'm': + notail(argv[i]); + *flags |= FLAGS_MEMPROF_PARSE; + return i; case 'i': notail(argv[i]); *flags |= FLAGS_INTERACTIVE; @@ -547,6 +561,12 @@ static int pmain(lua_State *L) s->status = runargs(L, argv, argn); if (s->status != LUA_OK) return 0; + if((flags & FLAGS_MEMPROF_PARSE)) { + //char* parser_path = "/usr/local/share/luajit-2.1.0-beta3/memprof.lua"; + s->status = report(L, call_script(L, PARSER_PATH)); + return s->status == LUA_OK; + } + if (s->argc > argn) { s->status = handle_script(L, argv + argn); if (s->status != LUA_OK) return 0; 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.32.0