Skip to content

Commit 08f5667

Browse files
committed
tests/lapi: add sysprof tests
The patch adds tests for sysprof built into LuaJIT and Tarantool's sysprof.
1 parent 51d08cf commit 08f5667

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

tests/lapi/jit_p_test.lua

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
--[[
2+
SPDX-License-Identifier: ISC
3+
Copyright (c) 2025, Sergey Bronnikov.
4+
5+
LuaJIT profiler,
6+
https://luajit.org/ext_profiler.html
7+
]]
8+
9+
local luzer = require("luzer")
10+
local test_lib = require("lib")
11+
12+
if test_lib.lua_version() ~= "LuaJIT" then
13+
print("Unsupported version.")
14+
os.exit(0)
15+
end
16+
17+
local sysprof = require("jit.profile")
18+
19+
local MAX_INT = test_lib.MAX_INT
20+
local MIN_INT = test_lib.MIN_INT
21+
22+
local SYSPROF_DEFAULT_INTERVAL = 1 -- ms
23+
24+
local SYSPROF_OPTIONS = {
25+
"f", -- Profile with precision down to the function level.
26+
"l", -- Profile with precision down to the line level.
27+
"i", -- Sampling interval in milliseconds (default 10ms).
28+
}
29+
30+
local DUMPSTACK_FMT = {
31+
"p", -- Preserve the full path for module names.
32+
"f", -- Dump the function name if it can be derived.
33+
"F", -- Ditto, but dump module:name.
34+
"l", -- Dump module:line.
35+
"Z", -- Zap the following characters for the last dumped frame.
36+
}
37+
38+
local function sysprof_dumpstack(fdp)
39+
local fmt = fdp:oneof(DUMPSTACK_FMT)
40+
local depth = fdp:consume_integer(MIN_INT, MAX_INT)
41+
local dump = sysprof.dumpstack(fmt, depth)
42+
assert(dump)
43+
end
44+
45+
local function TestOneInput(buf)
46+
local fdp = luzer.FuzzedDataProvider(buf)
47+
local chunk = fdp:consume_string(test_lib.MAX_STR_LEN)
48+
-- LuaJIT ASSERT lj_bcread.c:123: bcread_byte: buffer read
49+
-- overflow.
50+
local func = load(chunk, "luzer", "t")
51+
52+
local sysprof_option = fdp:oneof(SYSPROF_OPTIONS)
53+
if sysprof_option == "i" then
54+
sysprof_option = ("i%d"):format(SYSPROF_DEFAULT_INTERVAL)
55+
end
56+
local cb = function(_thread, _samples, _vmstate)
57+
-- Nope.
58+
end
59+
60+
sysprof.start(sysprof_option, cb)
61+
pcall(func)
62+
sysprof_dumpstack(fdp)
63+
sysprof.stop()
64+
end
65+
66+
local args = {
67+
artifact_prefix = "jit_p_test",
68+
}
69+
luzer.Fuzz(TestOneInput, nil, args)

tests/lapi/misc_sysprof_test.lua

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
--[[
2+
SPDX-License-Identifier: ISC
3+
Copyright (c) 2025, Sergey Bronnikov.
4+
5+
Tarantool's platform profiler,
6+
https://www.tarantool.io/en/doc/latest/tooling/luajit_sysprof/
7+
]]
8+
9+
local luzer = require("luzer")
10+
local test_lib = require("lib")
11+
12+
local has_sysprof, sysprof = pcall(require, "misc.sysprof")
13+
if not has_sysprof then
14+
print("Unsupported version.")
15+
os.exit(0)
16+
end
17+
18+
local SYSPROF_DEFAULT_INTERVAL = 1 -- ms
19+
20+
local function TestOneInput(buf)
21+
local fdp = luzer.FuzzedDataProvider(buf)
22+
local chunk = fdp:consume_string(test_lib.MAX_STR_LEN)
23+
-- LuaJIT ASSERT lj_bcread.c:123: bcread_byte: buffer read
24+
-- overflow.
25+
local func = load(chunk, "luzer", "t")
26+
local sysprof_mode = fdp:oneof({"D", "L", "C"})
27+
28+
assert(sysprof.start({
29+
interval = SYSPROF_DEFAULT_INTERVAL,
30+
mode = sysprof_mode,
31+
path = "/dev/null",
32+
}))
33+
pcall(func)
34+
sysprof.report()
35+
sysprof.stop()
36+
end
37+
38+
local args = {
39+
artifact_prefix = "misc_sysprof_",
40+
}
41+
luzer.Fuzz(TestOneInput, nil, args)

0 commit comments

Comments
 (0)