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 D8271A62481; Tue, 26 Mar 2024 19:09:46 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org D8271A62481 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1711469387; bh=yvn2fMjH/J+fjmeiImnMAvK8L3uRxHxPLzDVDInb960=; h=To:Date:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=Z38FpFzkHWdIzO1QjFKcjMPgUdYTk5pl2hc8fTdgbCp1jM4NlMun19JyFnDC1/l0t NkNp+xwo12r+VTdcTEHS6zJIT9qtSmwZ2ONLasqkWdsm6QIXe4yt1DH+HLvHbtwLAA aUndaW5RSnwOcUyws3ZLMqyDk8Yc/Vu3+lbbv5nw= Received: from mail-lf1-f46.google.com (mail-lf1-f46.google.com [209.85.167.46]) (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 84EE8A6248C for ; Tue, 26 Mar 2024 19:09:45 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 84EE8A6248C Received: by mail-lf1-f46.google.com with SMTP id 2adb3069b0e04-5101cd91017so6751458e87.2 for ; Tue, 26 Mar 2024 09:09:45 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711469384; x=1712074184; 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=59Yd8XuaKEsatfKN1DOfrVm9jG9oyadl9E+3/rsVxoo=; b=Q+sHw+y5x/XU20M9jyRFB9d/XANpSicA9G5zFGhUecfkccNwbl4of5FD+mMgDoXnzC iw1eBaTSLe1bgxTvPIWwOueTmtd/CrKS3yO7qAZdloEOkPi+f8TMwUMFlNR6C1I4TblP sWeq0gs+GOi0JIzyhOQ/pv6jZjin2buxj4+BKudgJhnZH7JBfxpsSdakiksSb6Ik6J7Q xnk0cBOxA+8qzMNwG0uSLZksXHU+h6B4TeGnSq+A+7Gk7hOr/nsSb5x7yOBs0d6/jzK5 VL6AW7CSBaFz4PrWe7v2HOZdBgNqryEfDcJVLVdW1kxhliQH48qtK+Wc7FwG7JaNvbQy 4Yxw== X-Gm-Message-State: AOJu0Yx5+ihZyBXBAf3EN3qxhgNb0XxUVzAvHv858JA+MXLlxdOzfsdt 1JONJjQmkj5jv13hVupK1mOjJnlIDVbereAUC9DgV5WIRoyJU5OMogMgapcutye5zg== X-Google-Smtp-Source: AGHT+IHhjmEjmoQvk9S9cqrRdBDQQC0sQc1GKO2v6llX4fpC/Q5FecS1fEFgONdZ9/PFD/+GdAbJnw== X-Received: by 2002:a05:6512:532:b0:513:ccda:bc86 with SMTP id o18-20020a056512053200b00513ccdabc86mr1397026lfc.4.1711469383980; Tue, 26 Mar 2024 09:09:43 -0700 (PDT) Received: from localhost.localdomain (95-24-11-149.broadband.corbina.ru. [95.24.11.149]) by smtp.gmail.com with ESMTPSA id e2-20020ac25462000000b00514b4c93b2dsm1536493lfn.178.2024.03.26.09.09.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Mar 2024 09:09:43 -0700 (PDT) X-Google-Original-From: Maxim Kokryashkin To: tarantool-patches@dev.tarantool.org, skaplun@tarantool.org, sergeyb@tarantool.org Date: Tue, 26 Mar 2024 19:09:40 +0300 Message-ID: <20240326160940.339283-1-m.kokryashkin@tarantool.org> X-Mailer: git-send-email 2.44.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [Tarantool-patches] [PATCH luajit v1] profilers: print user-friendly errors 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" Prior to this patch, there was no error-checking in profilers, which resulted in raw Lua errors being reported in cases of non-existing paths or corrupt file structures. This patch makes use of the event reader module introduced in commit df5a07218dba8cefa370499a1a54ba75fb73b99b ("profilers: introduce event reader module") to add error handling, so all parsing errors are now reported in a user-friendly manner. Tool CLI flag tests are adapted correspondingly to error message changes. Resolves tarantool/tarantool#9217 Part of tarantool/tarantool#5994 --- Branch: https://github.com/tarantool/luajit/tree/mkokryashkin/profile-parsers-refactoring-p2 .../gh-5688-tool-cli-flag.test.lua | 4 +- ...17-profile-parsers-error-handling.test.lua | 90 +++++++++++++++++++ tools/memprof.lua | 11 +-- tools/memprof/parse.lua | 5 +- tools/sysprof.lua | 10 +-- tools/sysprof/parse.lua | 5 +- tools/utils/symtab.lua | 6 +- 7 files changed, 112 insertions(+), 19 deletions(-) create mode 100644 test/tarantool-tests/gh-9217-profile-parsers-error-handling.test.lua diff --git a/test/tarantool-tests/gh-5688-tool-cli-flag.test.lua b/test/tarantool-tests/gh-5688-tool-cli-flag.test.lua index 75293f11..8ead83b5 100644 --- a/test/tarantool-tests/gh-5688-tool-cli-flag.test.lua +++ b/test/tarantool-tests/gh-5688-tool-cli-flag.test.lua @@ -42,7 +42,7 @@ local SMOKE_CMD_SET = { local MEMPROF_CMD_SET = { { cmd = MEMPROF_PARSER .. BAD_PATH, - like = 'fopen, errno: 2', + like = 'Failed to open.*fopen, errno: 2', }, { cmd = MEMPROF_PARSER .. TMP_BINFILE_MEMPROF, @@ -61,7 +61,7 @@ local MEMPROF_CMD_SET = { local SYSPROF_CMD_SET = { { cmd = SYSPROF_PARSER .. BAD_PATH, - like = 'fopen, errno: 2', + like = 'Failed to open.*fopen, errno: 2', }, { cmd = SYSPROF_PARSER .. TMP_BINFILE_SYSPROF, diff --git a/test/tarantool-tests/gh-9217-profile-parsers-error-handling.test.lua b/test/tarantool-tests/gh-9217-profile-parsers-error-handling.test.lua new file mode 100644 index 00000000..c2e0f0a6 --- /dev/null +++ b/test/tarantool-tests/gh-9217-profile-parsers-error-handling.test.lua @@ -0,0 +1,90 @@ +local tap = require('tap') +local test = tap.test('gh-9217-profile-parsers-error-handling'):skipcond({ + ['Profile tools are implemented for x86_64 only'] = jit.arch ~= 'x86' and + jit.arch ~= 'x64', + ['Profile tools are implemented for Linux only'] = jit.os ~= 'Linux', + -- XXX: Tarantool integration is required to run this test properly. + -- luacheck: no global + ['No profile tools CLI option integration'] = _TARANTOOL, +}) + +jit.off() +jit.flush() + +local table_new = require('table.new') +local utils = require('utils') + +local BAD_PATH = utils.tools.profilename('bad-path-tmp.bin') +local NON_PROFILE_DATA = utils.tools.profilename('not-profile-data.tmp.bin') +local CORRUPT_PROFILE = utils.tools.profilename('profdata.tmp.bin') + +local EXECUTABLE = utils.exec.luacmd(arg) +local PARSERS = { + memprof = EXECUTABLE .. ' -tm ', + sysprof = EXECUTABLE .. ' -ts ', +} +local REDIRECT_OUTPUT = ' 2>&1' + +local TABLE_SIZE = 20 + +local TEST_CASES = { + { + path = BAD_PATH, + err_msg = 'Failed to open' + }, + { + path = NON_PROFILE_DATA, + err_msg = 'Failed to parse symtab from' + }, + { + path = CORRUPT_PROFILE, + err_msg = 'Failed to parse profile data from' + }, +} + +test:plan(2 * #TEST_CASES) + +local function generate_non_profile_data(path) + local file = io.open(path, 'w') + file:write('data') + file:close() +end + +local function generate_corrupt_profile(path) + local res, err = misc.memprof.start(path) + -- Should start successfully. + assert(res, err) + + local _ = table_new(TABLE_SIZE, 0) + _ = nil + collectgarbage() + + res, err = misc.memprof.stop() + -- Should stop successfully. + assert(res, err) + + local file = io.open(path, 'r') + local content = file:read('*all') + file:close() + local index = string.find(content, 'ljm') + + file = io.open(path, 'w') + file:write(string.sub(content, 1, index - 1)) + file:close() +end + +generate_non_profile_data(NON_PROFILE_DATA) +generate_corrupt_profile(CORRUPT_PROFILE) + +for _, case in ipairs(TEST_CASES) do + for profiler, parser in pairs(PARSERS) do + local path = case.path + local err_msg = case.err_msg + local output = io.popen(parser .. path .. REDIRECT_OUTPUT):read('*all') + test:like(output, err_msg, string.format('%s: %s', profiler, err_msg)) + end +end + +os.remove(NON_PROFILE_DATA) +os.remove(CORRUPT_PROFILE) +test:done(true) diff --git a/tools/memprof.lua b/tools/memprof.lua index 537cb869..7d7e8e05 100644 --- a/tools/memprof.lua +++ b/tools/memprof.lua @@ -10,10 +10,9 @@ -- Major portions taken verbatim or adapted from the LuaVela. -- Copyright (C) 2015-2019 IPONWEB Ltd. -local bufread = require "utils.bufread" local memprof = require "memprof.parse" local process = require "memprof.process" -local symtab = require "utils.symtab" +local evread = require "utils.evread" local view = require "memprof.humanize" local stdout, stderr = io.stdout, io.stderr @@ -108,9 +107,11 @@ local function parseargs(args) end local function dump(inputfile) - local reader = bufread.new(inputfile) - local symbols = symtab.parse(reader) - local events = memprof.parse(reader, symbols) + -- XXX: This function exits with a non-zero exit code and + -- prints an error message if it encounters any failure during + -- the process of parsing. + local events, symbols = evread(memprof.parse, inputfile) + if not config.leak_only then view.profile_info(events, config) end diff --git a/tools/memprof/parse.lua b/tools/memprof/parse.lua index cc66a23e..11a79a1d 100644 --- a/tools/memprof/parse.lua +++ b/tools/memprof/parse.lua @@ -206,12 +206,13 @@ function M.parse(reader, symbols) local _ = reader:read_octets(3) if magic ~= LJM_MAGIC then - error("Bad LJM format prologue: "..magic) + error("Bad memprof event format prologue: "..magic) end if string.byte(version) ~= LJM_CURRENT_VERSION then error(string_format( - "LJM format version mismatch: the tool expects %d, but your data is %d", + "Memprof event format version mismatch:".. + " the tool expects %d, but your data is %d", LJM_CURRENT_VERSION, string.byte(version) )) diff --git a/tools/sysprof.lua b/tools/sysprof.lua index 8449b23f..420ca41c 100644 --- a/tools/sysprof.lua +++ b/tools/sysprof.lua @@ -1,6 +1,5 @@ -local bufread = require "utils.bufread" +local evread = require "utils.evread" local sysprof = require "sysprof.parse" -local symtab = require "utils.symtab" local stdout, stderr = io.stdout, io.stderr local match, gmatch = string.match, string.gmatch @@ -78,9 +77,10 @@ local function parseargs(args) end local function dump(inputfile) - local reader = bufread.new(inputfile) - local symbols = symtab.parse(reader) - local events = sysprof.parse(reader, symbols) + -- XXX: This function exits with a non-zero exit code and + -- prints an error message if it encounters any failure during + -- the process of parsing. + local events = evread(sysprof.parse, inputfile) for stack, count in pairs(events) do print(stack, count) diff --git a/tools/sysprof/parse.lua b/tools/sysprof/parse.lua index 64c4a455..0e416d37 100755 --- a/tools/sysprof/parse.lua +++ b/tools/sysprof/parse.lua @@ -237,12 +237,13 @@ function M.parse(reader, symbols) local _ = reader:read_octets(3) if magic ~= LJP_MAGIC then - error("Bad LJP format prologue: "..magic) + error("Bad sysprof event format prologue: " .. tostring(magic)) end if string.byte(version) ~= LJP_CURRENT_VERSION then error(string_format( - "LJP format version mismatch: the tool expects %d, but your data is %d", + "Sysprof event format version mismatch:".. + " the tool expects %d, but your data is %d", LJP_CURRENT_VERSION, string.byte(version) )) diff --git a/tools/utils/symtab.lua b/tools/utils/symtab.lua index 0c878e7a..e80b9f33 100644 --- a/tools/utils/symtab.lua +++ b/tools/utils/symtab.lua @@ -95,13 +95,13 @@ function M.parse(reader) local _ = reader:read_octets(3) if magic ~= LJS_MAGIC then - error("Bad LJS format prologue: "..magic) + error("Bad LuaJIT symbol table format prologue: " .. tostring(magic)) end if string.byte(version) ~= LJS_CURRENT_VERSION then error(string_format( - "LJS format version mismatch:".. - "the tool expects %d, but your data is %d", + "LuaJIT symbol table format version mismatch:".. + " the tool expects %d, but your data is %d", LJS_CURRENT_VERSION, string.byte(version) )) -- 2.44.0