Skip to content

Commit

Permalink
profilers: print user-friendly errors
Browse files Browse the repository at this point in the history
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 adds
error handling, so all parsing errors are now reported in a
user-friendly manner.

Event parsing is now moved into a separate profiler-agnostic
module.

Tool CLI flag tests are adapted correspondingly to error message
changes.

Resolves tarantool/tarantool#9217
Part of tarantool/tarantool#5994
  • Loading branch information
mkokryashkin committed Nov 23, 2023
1 parent ac1f1f1 commit 40b4c39
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 18 deletions.
4 changes: 2 additions & 2 deletions test/tarantool-tests/gh-5688-tool-cli-flag.test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
{
cmd = MEMPROF_PARSER .. TMP_BINFILE_MEMPROF,
Expand All @@ -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',
},
{
cmd = SYSPROF_PARSER .. TMP_BINFILE_SYSPROF,
Expand Down
4 changes: 4 additions & 0 deletions tools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ else()
memprof.lua
utils/avl.lua
utils/bufread.lua
utils/profile_data_reader.lua
utils/symtab.lua
)
list(APPEND LUAJIT_TOOLS_DEPS tools-parse-memprof)
Expand All @@ -36,6 +37,7 @@ else()
install(FILES
${CMAKE_CURRENT_SOURCE_DIR}/utils/avl.lua
${CMAKE_CURRENT_SOURCE_DIR}/utils/bufread.lua
${CMAKE_CURRENT_SOURCE_DIR}/utils/profile_data_reader.lua
${CMAKE_CURRENT_SOURCE_DIR}/utils/symtab.lua
DESTINATION ${LUAJIT_DATAROOTDIR}/utils
PERMISSIONS
Expand Down Expand Up @@ -67,6 +69,7 @@ else()
sysprof/collapse.lua
sysprof.lua
utils/bufread.lua
utils/profile_data_reader.lua
utils/symtab.lua
)
list(APPEND LUAJIT_TOOLS_DEPS tools-parse-sysprof)
Expand All @@ -85,6 +88,7 @@ else()
)
install(FILES
${CMAKE_CURRENT_SOURCE_DIR}/utils/bufread.lua
${CMAKE_CURRENT_SOURCE_DIR}/utils/profile_data_reader.lua
${CMAKE_CURRENT_SOURCE_DIR}/utils/symtab.lua
DESTINATION ${LUAJIT_DATAROOTDIR}/utils
PERMISSIONS
Expand Down
17 changes: 9 additions & 8 deletions tools/memprof.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@
-- 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 view = require "memprof.humanize"
local memprof = require('memprof.parse')
local process = require('memprof.process')
local reader = require('utils.profile_data_reader')
local view = require('memprof.humanize')

local stdout, stderr = io.stdout, io.stderr
local match, gmatch = string.match, string.gmatch
Expand Down Expand Up @@ -107,9 +106,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 = reader(inputfile, memprof.parse)

if not config.leak_only then
view.profile_info(events, config)
end
Expand Down
13 changes: 7 additions & 6 deletions tools/sysprof.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
local bufread = require "utils.bufread"
local sysprof = require "sysprof.parse"
local symtab = require "utils.symtab"
local reader = require('utils.profile_data_reader')
local sysprof = require('sysprof.parse')

local stdout, stderr = io.stdout, io.stderr
local match, gmatch = string.match, string.gmatch
Expand Down Expand Up @@ -77,10 +76,12 @@ local function parseargs(args)
return args[args.argn]
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 = reader(inputfile, sysprof.parse)

for stack, count in pairs(events) do
print(stack, count)
Expand Down
2 changes: 1 addition & 1 deletion tools/sysprof/parse.lua
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ function M.parse(reader, symbols)
local _ = reader:read_octets(3)

if magic ~= LJP_MAGIC then
error("Bad LJP format prologue: "..magic)
error("Bad LJP format prologue: " .. tostring(magic))
end

if string.byte(version) ~= LJP_CURRENT_VERSION then
Expand Down
33 changes: 33 additions & 0 deletions tools/utils/profile_data_reader.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
local bufread = require('utils.bufread')
local symtab = require('utils.symtab')

local function make_xpcall_handler(inputfile, fmt)
return function()
io.stderr:write(string.format(fmt, inputfile))
os.exit(1, true)
end
end

local function safe_event_reader(inputfile, parser)
local _, reader = xpcall(
bufread.new,
make_xpcall_handler(inputfile, 'Failed to open %s.'),
inputfile
)

local _, symbols = xpcall(
symtab.parse,
make_xpcall_handler(inputfile, 'Failed to parse symtab from %s.'),
reader
)

local _, events = xpcall(
parser,
make_xpcall_handler(inputfile, 'Failed to parse profile data from %s.'),
reader,
symbols
)
return events, symbols
end

return safe_event_reader
2 changes: 1 addition & 1 deletion tools/utils/symtab.lua
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ function M.parse(reader)
local _ = reader:read_octets(3)

if magic ~= LJS_MAGIC then
error("Bad LJS format prologue: "..magic)
error("Bad LJS format prologue: " .. tostring(magic))
end

if string.byte(version) ~= LJS_CURRENT_VERSION then
Expand Down

0 comments on commit 40b4c39

Please sign in to comment.