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 752E3518636; Mon, 10 Jul 2023 14:24:08 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 752E3518636 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1688988248; bh=cJjpAE4UX4xCTGqEnZbFC8uts29u+FKtmJ+lAHT2YDs=; h=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=jAemkVygN5c9P+TtVoLRkLG8U5y+YHilXkzuMEA+7nnLv1zmMBsGNeY/MPNgNN/ep 4Vs+oxwhv+ghjgwKSyAy9zfHt8kOjNE2jEd8+mJnSPyZmEvlUAFroZVfIyeRVxSv3Y LqXTt0gplG4mLiUju+QVEman7K9X8dg4lw1r5ZMA= Received: from mail-lf1-f42.google.com (mail-lf1-f42.google.com [209.85.167.42]) (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 27FBA518636 for ; Mon, 10 Jul 2023 14:24:07 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 27FBA518636 Received: by mail-lf1-f42.google.com with SMTP id 2adb3069b0e04-4fbaef9871cso6403408e87.0 for ; Mon, 10 Jul 2023 04:24:07 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688988246; x=1691580246; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Pb78hi3S7UmXE4K/LrPok9ik4COtkQ9yx/ltYgQZMQo=; b=kfi2nwZ9iSdG63N73FZZQKJAy8XjVZi20hQZDO2uXaVoIifKlAvNlE0wTpunOGDQwc 74+L0bUvgKDca7ieA23tYeEQDKlAxTp+77QycDlLZDw9Vy0cRbPyOVt+nHM479sr8IG+ OnhQqkc+Gi/2OxtllaW2A66bsBR6ygpGfeqXUOeTo9Fhz4H13mqb06Ear+l8oh9/tvB2 8K/8xPhtbBkqTQbhI9B91qQ9OWtLiwoUE3WU9dyQ8Z+Liu0JEdV6BdJ2bZ24ErcqZgo5 5E2AURSAzKJUQ4j8cX927GfVZl2a+kTMHsdh8LUKINDK9EBUmAp+HdJFagzNFy4z+9Pt 5XyA== X-Gm-Message-State: ABy/qLa+21gF2jRBOLUKMtToSQfNXFpNnsKVMsA6GEhItvvKYp6EsMYY n7XDfY0uAIc2kypvWwOm7S+p2riwXEODXg== X-Google-Smtp-Source: APBJJlFB2kf2AJTuNSMw1iM/zKjPq/IAWB3DJ3fjdbTvFNuaJVtSRUuj3rrXN6GlIY0nDFitebMujg== X-Received: by 2002:a05:6512:2005:b0:4f9:b032:5361 with SMTP id a5-20020a056512200500b004f9b0325361mr8648457lfb.10.1688988245792; Mon, 10 Jul 2023 04:24:05 -0700 (PDT) Received: from localhost.localdomain ([185.205.79.32]) by smtp.gmail.com with ESMTPSA id b25-20020ac25639000000b004fbe2172f7csm195436lff.49.2023.07.10.04.24.04 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 10 Jul 2023 04:24:05 -0700 (PDT) To: tarantool-patches@dev.tarantool.org, sergeyb@tarantool.org, skaplun@tarantool.org, m.kokryashkin@tarantool.org Date: Mon, 10 Jul 2023 14:24:00 +0300 Message-Id: <20230710112400.1802-1-max.kokryashkin@gmail.com> X-Mailer: git-send-email 2.39.2 (Apple Git-143) MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [Tarantool-patches] [PATCH luajit v7] 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: Maksim Kokryashkin via Tarantool-patches Reply-To: Maksim Kokryashkin Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" From: Maxim Kokryashkin It is really inconvenient to use a standalone shell script to parse memprof dump. That is why this commit introduces 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 --- Changes in v7: - Fixed comments as per review by Sergey Branch: https://github.com/tarantool/luajit/tree/fckxorg/gh-5688-cli-for-memprof-parse PR: https://github.com/tarantool/tarantool/pull/8002 src/luajit.c | 32 +++++++++- .../gh-5688-memprof-cli-flag.test.lua | 58 +++++++++++++++++++ 2 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 test/tarantool-tests/gh-5688-memprof-cli-flag.test.lua diff --git a/src/luajit.c b/src/luajit.c index 1ca24301..d335f32b 100644 --- a/src/luajit.c +++ b/src/luajit.c @@ -72,6 +72,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); @@ -361,6 +362,25 @@ 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) { + lua_getglobal(L, "require"); + lua_pushliteral(L, "memprof"); + if (lua_pcall(L, 1, 1, 0)) { + const char *msg = lua_tostring(L, -1); + if (msg && !strncmp(msg, "module ", 7)) + l_message(progname, + "tools.* modules not installed"); + return 1; + } + lua_getglobal(L, "arg"); + return report(L, lua_pcall(L, 1, 1, 0)); + } + l_message(progname, "invalid tool command"); + return -1; +} + /* Optimization flags. */ static int dojitopt(lua_State *L, const char *opt) { @@ -398,6 +418,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,6 +440,11 @@ 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 */ @@ -474,6 +500,10 @@ static int runargs(lua_State *L, char **argv, int argn) return 1; break; } + case 't': { /* Tarantool's fork extension. */ + 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 +565,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/test/tarantool-tests/gh-5688-memprof-cli-flag.test.lua b/test/tarantool-tests/gh-5688-memprof-cli-flag.test.lua new file mode 100644 index 00000000..8361781f --- /dev/null +++ b/test/tarantool-tests/gh-5688-memprof-cli-flag.test.lua @@ -0,0 +1,58 @@ +local tap = require('tap') +local test = tap.test('gh-5688-cli-for-memprof-parse'):skipcond({ + ['Memprof is implemented for x86_64 only'] = jit.arch ~= 'x86' and + jit.arch ~= 'x64', + ['Memprof is implemented for Linux only'] = jit.os ~= 'Linux', +-- XXX: The patch is for LuaJIT only, and it doesn't +-- work on Tarantool. +-- luacheck: no global + ['No memprof CLI option'] = _TARANTOOL, +}) + +test:plan(3) + +jit.off() +jit.flush() + +local table_new = require('table.new') +local utils = require('utils') + +local TMP_BINFILE = utils.tools.profilename('memprofdata.tmp.bin') +local BAD_PATH = utils.tools.profilename('bad-path-tmp.bin') +local EXECUTABLE = utils.exec.luacmd(arg) +local TABLE_SIZE = 20 + +local function default_payload() + local _ = table_new(TABLE_SIZE, 0) + _ = nil + collectgarbage() +end + +local function generate_output(filename, payload) + -- Clean up all garbage to avoid pollution of free. + collectgarbage() + + local res, err = misc.memprof.start(filename) + -- Should start succesfully. + assert(res, err) + + payload() + + res, err = misc.memprof.stop() + -- Should stop succesfully. + assert(res, err) +end + +generate_output(TMP_BINFILE, default_payload) + +local errcode = os.execute(EXECUTABLE .. ' -tinvalid ' .. TMP_BINFILE) +test:ok(errcode ~= 0, 'invalid tool flag') + +errcode = os.execute(EXECUTABLE .. ' -tm ' .. BAD_PATH) +test:ok(errcode ~= 0, 'binfile does not exist') + +errcode = os.execute(EXECUTABLE .. ' -tm ' .. TMP_BINFILE) +test:ok(errcode == 0, 'memprof binfile parsing') + +os.remove(TMP_BINFILE) +os.exit(test:check() and 0 or 1) -- 2.39.2 (Apple Git-143)