Skip to content

Commit 5dd3f91

Browse files
committed
sysprof: introduce specific errors and default mode
sysprof has a number of options and with any incorrect option it returns `false` and error message "profiler misuse". This message does not descriptive for sysprof users and make using sysprof more complicated. The patch splits error `PROFILE_ERRUSE` to four specific errors: `PROFILE_ERRBADMODE`, `PROFILE_ERRBADINTERVAL`, `PROFILE_ERRBADPATH` and `PROFILE_ERRBADTABLE`, and use default profiling mode ("C", callgraph) if it was not passed.
1 parent 6ff0d57 commit 5dd3f91

File tree

4 files changed

+85
-16
lines changed

4 files changed

+85
-16
lines changed

src/lib_misc.c

+36-11
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ static int on_stop_cb_default(void *opt, uint8_t *buf)
163163

164164
/* The default profiling interval equals to 10 ms. */
165165
#define SYSPROF_DEFAULT_INTERVAL 10
166+
#define SYSPROF_DEFAULT_MODE "C"
166167
#define SYSPROF_DEFAULT_OUTPUT "sysprof.bin"
167168

168169
static int set_output_path(const char *path, struct luam_Sysprof_Options *opt) {
@@ -185,13 +186,16 @@ static int parse_sysprof_opts(lua_State *L, struct luam_Sysprof_Options *opt, in
185186
const char *mode = NULL;
186187

187188
cTValue *mode_opt = lj_tab_getstr(options, lj_str_newlit(L, "mode"));
188-
if (!mode_opt || !tvisstr(mode_opt)) {
189-
return PROFILE_ERRUSE;
189+
if (mode_opt) {
190+
if (!tvisstr(mode_opt))
191+
return PROFILE_ERRBADMODE;
192+
mode = strVdata(mode_opt);
193+
if (mode[1] != '\0')
194+
return PROFILE_ERRBADMODE;
190195
}
191196

192-
mode = strVdata(mode_opt);
193-
if (mode[1] != '\0')
194-
return PROFILE_ERRUSE;
197+
if (!mode)
198+
mode = SYSPROF_DEFAULT_MODE;
195199

196200
switch (*mode) {
197201
case 'D':
@@ -204,7 +208,7 @@ static int parse_sysprof_opts(lua_State *L, struct luam_Sysprof_Options *opt, in
204208
opt->mode = LUAM_SYSPROF_CALLGRAPH;
205209
break;
206210
default:
207-
return PROFILE_ERRUSE;
211+
return PROFILE_ERRBADMODE;
208212
}
209213
}
210214

@@ -215,7 +219,7 @@ static int parse_sysprof_opts(lua_State *L, struct luam_Sysprof_Options *opt, in
215219
if (interval && tvisnumber(interval)) {
216220
int32_t signed_interval = numberVint(interval);
217221
if (signed_interval < 1)
218-
return PROFILE_ERRUSE;
222+
return PROFILE_ERRBADINTERVAL;
219223
opt->interval = signed_interval;
220224
}
221225
}
@@ -231,7 +235,7 @@ static int parse_sysprof_opts(lua_State *L, struct luam_Sysprof_Options *opt, in
231235
if (!pathtv)
232236
path = SYSPROF_DEFAULT_OUTPUT;
233237
else if (!tvisstr(pathtv))
234-
return PROFILE_ERRUSE;
238+
return PROFILE_ERRBADPATH;
235239
else
236240
path = strVdata(pathtv);
237241

@@ -253,11 +257,12 @@ static int parse_sysprof_opts(lua_State *L, struct luam_Sysprof_Options *opt, in
253257

254258
static int parse_options(lua_State *L, struct luam_Sysprof_Options *opt)
255259
{
256-
if (lua_gettop(L) != 1)
257-
return PROFILE_ERRUSE;
260+
if (lua_gettop(L) != 1) {
261+
lua_createtable(L, 0, 0);
262+
}
258263

259264
if (!lua_istable(L, 1))
260-
return PROFILE_ERRUSE;
265+
return PROFILE_ERRBADTABLE;
261266

262267
return parse_sysprof_opts(L, opt, 1);
263268
}
@@ -270,6 +275,26 @@ static int sysprof_error(lua_State *L, int status)
270275
lua_pushstring(L, err2msg(LJ_ERR_PROF_MISUSE));
271276
lua_pushinteger(L, EINVAL);
272277
return 3;
278+
case PROFILE_ERRBADMODE:
279+
lua_pushnil(L);
280+
lua_pushstring(L, err2msg(LJ_ERR_PROF_BADMODE));
281+
lua_pushinteger(L, EINVAL);
282+
return 3;
283+
case PROFILE_ERRBADINTERVAL:
284+
lua_pushnil(L);
285+
lua_pushstring(L, err2msg(LJ_ERR_PROF_BADINTERVAL));
286+
lua_pushinteger(L, EINVAL);
287+
return 3;
288+
case PROFILE_ERRBADPATH:
289+
lua_pushnil(L);
290+
lua_pushstring(L, err2msg(LJ_ERR_PROF_BADPATH));
291+
lua_pushinteger(L, EINVAL);
292+
return 3;
293+
case PROFILE_ERRBADTABLE:
294+
lua_pushnil(L);
295+
lua_pushstring(L, err2msg(LJ_ERR_PROF_BADTABLE));
296+
lua_pushinteger(L, EINVAL);
297+
return 3;
273298
#if LJ_HASSYSPROF
274299
case PROFILE_ERRRUN:
275300
lua_pushnil(L);

src/lj_errmsg.h

+4
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,10 @@ ERRDEF(FFI_NYICALL, "NYI: cannot call this C function (yet)")
183183

184184
/* Profiler errors. */
185185
ERRDEF(PROF_MISUSE, "profiler misuse")
186+
ERRDEF(PROF_BADMODE, "profiler mode must be 'D', 'L' or 'C'")
187+
ERRDEF(PROF_BADINTERVAL, "profiler interval must be greater than 1")
188+
ERRDEF(PROF_BADPATH, "profiler path does not exist")
189+
ERRDEF(PROF_BADTABLE, "profiler expects a table with parameters")
186190
#if LJ_HASMEMPROF || LJ_HASSYSPROF
187191
ERRDEF(PROF_ISRUNNING, "profiler is running already")
188192
ERRDEF(PROF_NOTRUNNING, "profiler is not running")

src/lmisclib.h

+5
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,11 @@ struct luam_Sysprof_Options {
142142
#define PROFILE_ERRMEM 3
143143
#define PROFILE_ERRIO 4
144144

145+
#define PROFILE_ERRBADMODE 5
146+
#define PROFILE_ERRBADINTERVAL 6
147+
#define PROFILE_ERRBADPATH 7
148+
#define PROFILE_ERRBADTABLE 8
149+
145150
LUAMISC_API int luaM_sysprof_set_writer(luam_Sysprof_writer writer);
146151

147152
LUAMISC_API int luaM_sysprof_set_on_stop(luam_Sysprof_on_stop on_stop);

test/tarantool-tests/profilers/misclib-sysprof-lapi.test.lua

+40-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ local test = tap.test("misc-sysprof-lapi"):skipcond({
1010
["Disabled due to #10803"] = os.getenv("LUAJIT_TEST_USE_VALGRIND"),
1111
})
1212

13-
test:plan(19)
13+
test:plan(33)
1414

1515
jit.off()
1616
-- XXX: Run JIT tuning functions in a safe frame to avoid errors
@@ -65,9 +65,25 @@ end
6565

6666
-- Wrong profiling mode.
6767
local res, err, errno = misc.sysprof.start{ mode = "A" }
68-
test:ok(res == nil and err:match("profiler misuse"), "res with no parameters")
68+
test:ok(res == nil, "res with no parameters")
69+
test:ok(err:match("profiler mode must be 'D', 'L' or 'C'"),
70+
"error with no parameters")
6971
test:ok(type(errno) == "number", "errno with no parameters")
7072

73+
-- Missed profiling mode.
74+
res, err, errno = misc.sysprof.start{}
75+
test:is(res, true, "res with missed profiling mode")
76+
test:is(err, nil, "error with missed profiling mode")
77+
test:is(errno, nil, "errno with missed profiling mode")
78+
assert(misc.sysprof.stop(), "sysprof is not stopped")
79+
80+
-- Not a table.
81+
res, err, errno = misc.sysprof.start("NOT A TABLE")
82+
test:ok(res == nil, "res with not a table")
83+
test:ok(err:match("profiler expects a table with parameters"),
84+
"error with not a table")
85+
test:ok(type(errno) == "number", "errno with not a table")
86+
7187
-- Already running.
7288
res, err = misc.sysprof.start{ mode = "D" }
7389
assert(res, err)
@@ -90,10 +106,29 @@ res, err, errno = misc.sysprof.start({ mode = "C", path = BAD_PATH })
90106
test:ok(res == nil and err:match("No such file or directory"), "res and error with bad path")
91107
test:ok(type(errno) == "number", "errno with bad path")
92108

93-
-- Bad interval.
109+
-- Bad interval (-1).
94110
res, err, errno = misc.sysprof.start{ mode = "C", interval = -1 }
95-
test:ok(res == nil and err:match("profiler misuse"), "res and error with bad interval")
96-
test:ok(type(errno) == "number", "errno with bad interval")
111+
test:ok(res == nil, "res with bad interval -1")
112+
test:ok(err:match("profiler interval must be greater than 1"),
113+
"error with bad interval -1")
114+
test:ok(type(errno) == "number", "errno with bad interval -1")
115+
116+
-- Bad interval (0).
117+
res, err, errno = misc.sysprof.start{ mode = "C", interval = 0 }
118+
test:ok(res == nil, "res with bad interval 0")
119+
test:ok(err:match("profiler interval must be greater than 1"),
120+
"error with bad interval 0")
121+
test:ok(type(errno) == "number", "errno with bad interval 0")
122+
123+
-- Good interval (1).
124+
res, err, errno = misc.sysprof.start{
125+
mode = "C",
126+
interval = 1,
127+
}
128+
test:is(res, true, "res with good interval 1")
129+
test:is(err, nil, "error with good interval 1")
130+
test:is(errno, nil, "errno with good interval 1")
131+
misc.sysprof.stop()
97132

98133
-- DEFAULT MODE
99134

0 commit comments

Comments
 (0)