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 48D334B0335; Wed, 5 Jul 2023 15:25:49 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 48D334B0335 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1688559949; bh=BEIw6uvDCaf548JhgAbrgkuWgcXsAw4hvixsgPrPSt8=; h=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=YlrVf9bHQGPuSeoUdRG1y/gqtVsOHz6AznbbaNLQ0+XBseQxh87JBFRpWh98KqHCj FgCvCZ8JUO7kp/QzRLZXK0Tuz0ANy2T69aPjbKm4l1osW9MHshOLvvLFhT2UEicaSe SMuG+tME1Ay2SL9TdeUdi15PoLahPAIy2vrB6BmE= Received: from mail-lf1-f47.google.com (mail-lf1-f47.google.com [209.85.167.47]) (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 837684B0335 for ; Wed, 5 Jul 2023 15:25:47 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 837684B0335 Received: by mail-lf1-f47.google.com with SMTP id 2adb3069b0e04-4fb9ae4cef6so10567457e87.3 for ; Wed, 05 Jul 2023 05:25:47 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688559946; x=1691151946; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=z6K+Bk9RdOT24s0nZFjLhOjnBYhrgPKjwXnfXzBC3ek=; b=T9baKJtrwi91sRjDBQSZESpfzyWEO1u/N0RzANYeEYLu7WMdAeN54XN3YYMQpVlDu3 uAaoJSVic3sYE+h0HMHS22Rvs2KT7UxbBfgEYEg+d3O4Q5/JtgYOXgHlgh+mFejpIiGr ffKH084E419Q67TvVzu9O43XPko5A9xvpsqcG0Cfy/QcZiHnF4Tnlzgjv9PrMnFsJUe1 zVPF3KD0OMivt71GN2+Q0cKCYAa+fdytA4blahKH0Up29zURTvGH7zh77vnKXyFNc156 9B5I4GpeLnF61RIne/kb8HHum3dWbqy+HH7ehFkwZIkPkOMDv3Qq66nNE2aYyzwt+v3W UZaQ== X-Gm-Message-State: ABy/qLYyjtdfmmgRp1hCXoaI7CnwYZT+CZKaRfLr8HENofLPChTYY0ka hC1SG6PArXlwWtqT9WNtD58LmKkJwMp0iw== X-Google-Smtp-Source: APBJJlGeXyaGoxMFuYy1zT6BnuZYEwL48ku/zWrPJm4bTeomA2+H1t5l6EbmJibYP17PCFRrzAtrwg== X-Received: by 2002:ac2:5b44:0:b0:4f8:57b8:8efe with SMTP id i4-20020ac25b44000000b004f857b88efemr11191433lfp.55.1688559945992; Wed, 05 Jul 2023 05:25:45 -0700 (PDT) Received: from localhost.localdomain (95-24-4-177.broadband.corbina.ru. [95.24.4.177]) by smtp.gmail.com with ESMTPSA id t4-20020ac24c04000000b004fb7584b7absm4840418lfq.5.2023.07.05.05.25.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Jul 2023 05:25:45 -0700 (PDT) X-Google-Original-From: Maxim Kokryashkin To: tarantool-patches@dev.tarantool.org, skaplun@tarantool.org, sergeyb@tarantool.org Date: Wed, 5 Jul 2023 15:25:41 +0300 Message-Id: <20230705122541.405023-1-m.kokryashkin@tarantool.org> X-Mailer: git-send-email 2.40.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [Tarantool-patches] [PATCH luajit v6] 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 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 v6: - Simplified the patch. 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 | 31 ++++++++++- .../gh-5688-memprof-cli-flag.test.lua | 55 +++++++++++++++++++ 2 files changed, 85 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..81c0f8af 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,24 @@ 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, + "unknown luaJIT command or tools not installed"); + return 1; + } + lua_getglobal(L, "arg"); + return report(L, lua_pcall(L, 1, 1, 0)); + } + return -1; +} + /* Optimization flags. */ static int dojitopt(lua_State *L, const char *opt) { @@ -398,6 +417,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 +439,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 +499,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 +564,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..ba0cad68 --- /dev/null +++ b/test/tarantool-tests/gh-5688-memprof-cli-flag.test.lua @@ -0,0 +1,55 @@ +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(2) + +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 .. ' -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.40.1