forked from LuaJIT/LuaJIT
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
profilers: print user-friendly errors
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
1 parent
aac00cb
commit c9e7600
Showing
9 changed files
with
142 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
test/tarantool-tests/gh-9217-profile-parsers-error-handling.test.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
local bufread = require('utils.bufread') | ||
local symtab = require('utils.symtab') | ||
|
||
local function make_error_handler(fmt, inputfile) | ||
return function(err) | ||
io.stderr:write(string.format(fmt, inputfile)) | ||
io.stderr:write(string.format('\n\t%s\n', err)) | ||
os.exit(1, true) | ||
end | ||
end | ||
|
||
return function(parser, inputfile) | ||
local _, reader = xpcall( | ||
bufread.new, | ||
make_error_handler('Failed to open %s.', inputfile), | ||
inputfile | ||
) | ||
|
||
local _, symbols = xpcall( | ||
symtab.parse, | ||
make_error_handler('Failed to parse symtab from %s.', inputfile), | ||
reader | ||
) | ||
|
||
local _, events = xpcall( | ||
parser, | ||
make_error_handler('Failed to parse profile data from %s.', inputfile), | ||
reader, | ||
symbols | ||
) | ||
return events, symbols | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters