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 B642BEBF9E5; Tue, 4 Feb 2025 13:08:50 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org B642BEBF9E5 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1738663730; bh=sdFI1CqGySO8ld57oQG/ywoKZDlburKbgaecur0xBfg=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=lkCiq8wrHkSyZK+ipSCJz46+bdg+7s3TOZk9LfZb5Ds3haY0HA+kt3mV5D8D5sRdV FV2c4bH1jDiPE8DiowLSQ9Ctv6Tzgpd+anb/TpXHcwoCAlgkxVspDA57BIJBiPDOit 24MJgyLJKDyv5rogAHPjiRHJfX5nzjozqJlbIPLk= Received: from mail-ed1-f43.google.com (mail-ed1-f43.google.com [209.85.208.43]) (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 CE989EBF9E5 for ; Tue, 4 Feb 2025 13:07:53 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org CE989EBF9E5 Received: by mail-ed1-f43.google.com with SMTP id 4fb4d7f45d1cf-5dca451f922so4035813a12.2 for ; Tue, 04 Feb 2025 02:07:53 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738663673; x=1739268473; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=bAhFgX3ijWkf1j6d0lgIbF+5UeeZtTlLzrmfVylO65U=; b=lIUxo2JtBiVzxuA5UuMRLkq5oGSBFHZGLhDrv7U0xmx5zBwKTWuHCRTqlxEabvE7B+ qOlhYFhp+4hcZi6eDoY7G19AfQ+eCBQTxQrcW3lH/JBjh9mo+Ih6H1hv/aYoVm0b1MiT oIdxkhO5lkBZc7LDdN5tqdIv4KUVIyelYnmoreKrm+EpKL1xG3KGLnXjkSGKLHivcKVO zTyLbJQvIBlknYniQWls8zXOkv9Y7KzRHlLAKHLCPt9/JZxXep4iSHhJzOcqA+bnlcYD EvIH/T3EpnIRz+9RnNSCtcPyE6g4uElQzW8yFk+3Ve+AajTM8qqM/7uKlvfYsJ4F+7dx Sg4w== X-Gm-Message-State: AOJu0Yy9bbRAYnOTRXVkQtD9UoxiZXYjJoHrpx/R4KijzprOr3+S+6RQ Nb06MXbagD9wB/gknua6d51dakMcmSFemj/gRbMD2z7pHyEst8YdWPv5Tfg8 X-Gm-Gg: ASbGnctREIdl+iGvHwRqJiRntknQiwPy7rV3MKvdMCAA+Gx5ewQUd/vFhuHeELVR/hS Iu8e49CD+D6A5vGizoGcQOF1vtNFfXyd14C01id4i4l2ascZtQ5WlUxq+TXJJAdFMAUO42rCGxw tttYuUOMyy/9XyYrSNPfflC1lMATr8tB2uqn+u3seZ0jMoLCOdAPx9A/NzCOpp+YpEfBygX5XzO rQpX6EZw9AiGOKuGnemO43qi2iUFuGzV2W1bWyy9/CVniW/12R78Nuo3bLqoZAayhXN6UTOrTPt d8BiHg== X-Google-Smtp-Source: AGHT+IGn06f5FeLZ9Spb09GH4pDwmL/cjnOkPeO83V90gJzPjiY1p3fo/G0Q6bfftezosyCCZakUjw== X-Received: by 2002:a17:906:f595:b0:ab7:479f:8aa7 with SMTP id a640c23a62f3a-ab7479f8f86mr346143566b.30.1738663672481; Tue, 04 Feb 2025 02:07:52 -0800 (PST) Received: from localhost ([5.181.62.98]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-5dccb1c4963sm483584a12.11.2025.02.04.02.07.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Feb 2025 02:07:52 -0800 (PST) X-Google-Original-From: Sergey Bronnikov To: tarantool-patches@dev.tarantool.org, Sergey Kaplun Date: Tue, 4 Feb 2025 13:03:37 +0300 Message-Id: <5dd3f9113e7bc53441c4c26c4cc6938562fb2f2c.1738663201.git.sergeyb@tarantool.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [Tarantool-patches] [PATCH luajit 3/4] sysprof: introduce specific errors and default mode 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: Sergey Bronnikov via Tarantool-patches Reply-To: Sergey Bronnikov Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" 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. --- src/lib_misc.c | 47 ++++++++++++++----- src/lj_errmsg.h | 4 ++ src/lmisclib.h | 5 ++ .../profilers/misclib-sysprof-lapi.test.lua | 45 ++++++++++++++++-- 4 files changed, 85 insertions(+), 16 deletions(-) diff --git a/src/lib_misc.c b/src/lib_misc.c index 5b7a4b62..a28e4a3c 100644 --- a/src/lib_misc.c +++ b/src/lib_misc.c @@ -163,6 +163,7 @@ static int on_stop_cb_default(void *opt, uint8_t *buf) /* The default profiling interval equals to 10 ms. */ #define SYSPROF_DEFAULT_INTERVAL 10 +#define SYSPROF_DEFAULT_MODE "C" #define SYSPROF_DEFAULT_OUTPUT "sysprof.bin" 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 const char *mode = NULL; cTValue *mode_opt = lj_tab_getstr(options, lj_str_newlit(L, "mode")); - if (!mode_opt || !tvisstr(mode_opt)) { - return PROFILE_ERRUSE; + if (mode_opt) { + if (!tvisstr(mode_opt)) + return PROFILE_ERRBADMODE; + mode = strVdata(mode_opt); + if (mode[1] != '\0') + return PROFILE_ERRBADMODE; } - mode = strVdata(mode_opt); - if (mode[1] != '\0') - return PROFILE_ERRUSE; + if (!mode) + mode = SYSPROF_DEFAULT_MODE; switch (*mode) { case 'D': @@ -204,7 +208,7 @@ static int parse_sysprof_opts(lua_State *L, struct luam_Sysprof_Options *opt, in opt->mode = LUAM_SYSPROF_CALLGRAPH; break; default: - return PROFILE_ERRUSE; + return PROFILE_ERRBADMODE; } } @@ -215,7 +219,7 @@ static int parse_sysprof_opts(lua_State *L, struct luam_Sysprof_Options *opt, in if (interval && tvisnumber(interval)) { int32_t signed_interval = numberVint(interval); if (signed_interval < 1) - return PROFILE_ERRUSE; + return PROFILE_ERRBADINTERVAL; opt->interval = signed_interval; } } @@ -231,7 +235,7 @@ static int parse_sysprof_opts(lua_State *L, struct luam_Sysprof_Options *opt, in if (!pathtv) path = SYSPROF_DEFAULT_OUTPUT; else if (!tvisstr(pathtv)) - return PROFILE_ERRUSE; + return PROFILE_ERRBADPATH; else path = strVdata(pathtv); @@ -253,11 +257,12 @@ static int parse_sysprof_opts(lua_State *L, struct luam_Sysprof_Options *opt, in static int parse_options(lua_State *L, struct luam_Sysprof_Options *opt) { - if (lua_gettop(L) != 1) - return PROFILE_ERRUSE; + if (lua_gettop(L) != 1) { + lua_createtable(L, 0, 0); + } if (!lua_istable(L, 1)) - return PROFILE_ERRUSE; + return PROFILE_ERRBADTABLE; return parse_sysprof_opts(L, opt, 1); } @@ -270,6 +275,26 @@ static int sysprof_error(lua_State *L, int status) lua_pushstring(L, err2msg(LJ_ERR_PROF_MISUSE)); lua_pushinteger(L, EINVAL); return 3; + case PROFILE_ERRBADMODE: + lua_pushnil(L); + lua_pushstring(L, err2msg(LJ_ERR_PROF_BADMODE)); + lua_pushinteger(L, EINVAL); + return 3; + case PROFILE_ERRBADINTERVAL: + lua_pushnil(L); + lua_pushstring(L, err2msg(LJ_ERR_PROF_BADINTERVAL)); + lua_pushinteger(L, EINVAL); + return 3; + case PROFILE_ERRBADPATH: + lua_pushnil(L); + lua_pushstring(L, err2msg(LJ_ERR_PROF_BADPATH)); + lua_pushinteger(L, EINVAL); + return 3; + case PROFILE_ERRBADTABLE: + lua_pushnil(L); + lua_pushstring(L, err2msg(LJ_ERR_PROF_BADTABLE)); + lua_pushinteger(L, EINVAL); + return 3; #if LJ_HASSYSPROF case PROFILE_ERRRUN: lua_pushnil(L); diff --git a/src/lj_errmsg.h b/src/lj_errmsg.h index 19c41f0b..9713d6c7 100644 --- a/src/lj_errmsg.h +++ b/src/lj_errmsg.h @@ -183,6 +183,10 @@ ERRDEF(FFI_NYICALL, "NYI: cannot call this C function (yet)") /* Profiler errors. */ ERRDEF(PROF_MISUSE, "profiler misuse") +ERRDEF(PROF_BADMODE, "profiler mode must be 'D', 'L' or 'C'") +ERRDEF(PROF_BADINTERVAL, "profiler interval must be greater than 1") +ERRDEF(PROF_BADPATH, "profiler path does not exist") +ERRDEF(PROF_BADTABLE, "profiler expects a table with parameters") #if LJ_HASMEMPROF || LJ_HASSYSPROF ERRDEF(PROF_ISRUNNING, "profiler is running already") ERRDEF(PROF_NOTRUNNING, "profiler is not running") diff --git a/src/lmisclib.h b/src/lmisclib.h index 9084319c..b4eb0b57 100644 --- a/src/lmisclib.h +++ b/src/lmisclib.h @@ -142,6 +142,11 @@ struct luam_Sysprof_Options { #define PROFILE_ERRMEM 3 #define PROFILE_ERRIO 4 +#define PROFILE_ERRBADMODE 5 +#define PROFILE_ERRBADINTERVAL 6 +#define PROFILE_ERRBADPATH 7 +#define PROFILE_ERRBADTABLE 8 + LUAMISC_API int luaM_sysprof_set_writer(luam_Sysprof_writer writer); LUAMISC_API int luaM_sysprof_set_on_stop(luam_Sysprof_on_stop on_stop); diff --git a/test/tarantool-tests/profilers/misclib-sysprof-lapi.test.lua b/test/tarantool-tests/profilers/misclib-sysprof-lapi.test.lua index 581fb7fd..68a4b72f 100644 --- a/test/tarantool-tests/profilers/misclib-sysprof-lapi.test.lua +++ b/test/tarantool-tests/profilers/misclib-sysprof-lapi.test.lua @@ -10,7 +10,7 @@ local test = tap.test("misc-sysprof-lapi"):skipcond({ ["Disabled due to #10803"] = os.getenv("LUAJIT_TEST_USE_VALGRIND"), }) -test:plan(19) +test:plan(33) jit.off() -- XXX: Run JIT tuning functions in a safe frame to avoid errors @@ -65,9 +65,25 @@ end -- Wrong profiling mode. local res, err, errno = misc.sysprof.start{ mode = "A" } -test:ok(res == nil and err:match("profiler misuse"), "res with no parameters") +test:ok(res == nil, "res with no parameters") +test:ok(err:match("profiler mode must be 'D', 'L' or 'C'"), + "error with no parameters") test:ok(type(errno) == "number", "errno with no parameters") +-- Missed profiling mode. +res, err, errno = misc.sysprof.start{} +test:is(res, true, "res with missed profiling mode") +test:is(err, nil, "error with missed profiling mode") +test:is(errno, nil, "errno with missed profiling mode") +assert(misc.sysprof.stop(), "sysprof is not stopped") + +-- Not a table. +res, err, errno = misc.sysprof.start("NOT A TABLE") +test:ok(res == nil, "res with not a table") +test:ok(err:match("profiler expects a table with parameters"), + "error with not a table") +test:ok(type(errno) == "number", "errno with not a table") + -- Already running. res, err = misc.sysprof.start{ mode = "D" } assert(res, err) @@ -90,10 +106,29 @@ res, err, errno = misc.sysprof.start({ mode = "C", path = BAD_PATH }) test:ok(res == nil and err:match("No such file or directory"), "res and error with bad path") test:ok(type(errno) == "number", "errno with bad path") --- Bad interval. +-- Bad interval (-1). res, err, errno = misc.sysprof.start{ mode = "C", interval = -1 } -test:ok(res == nil and err:match("profiler misuse"), "res and error with bad interval") -test:ok(type(errno) == "number", "errno with bad interval") +test:ok(res == nil, "res with bad interval -1") +test:ok(err:match("profiler interval must be greater than 1"), + "error with bad interval -1") +test:ok(type(errno) == "number", "errno with bad interval -1") + +-- Bad interval (0). +res, err, errno = misc.sysprof.start{ mode = "C", interval = 0 } +test:ok(res == nil, "res with bad interval 0") +test:ok(err:match("profiler interval must be greater than 1"), + "error with bad interval 0") +test:ok(type(errno) == "number", "errno with bad interval 0") + +-- Good interval (1). +res, err, errno = misc.sysprof.start{ + mode = "C", + interval = 1, +} +test:is(res, true, "res with good interval 1") +test:is(err, nil, "error with good interval 1") +test:is(errno, nil, "errno with good interval 1") +misc.sysprof.stop() -- DEFAULT MODE -- 2.34.1