From nobody Sun May 24 20:34:47 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8412D3112DA for ; Thu, 21 May 2026 14:18:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779373131; cv=none; b=aqpfUV1sl49zbmMvczrKDOLcJBFO5A1KZeYZ2Tqt/ZJUX3RyVq08OLdlBh3u2FMlH1uHm8yuwVVN20l+bB0x/4viN9rkE1hNRc8fr0N8SCfFZEEm6gZm4BkBcBCv5PyMW1Xxb8n8aY2D2KEGvMYLFBRH5p0lTXrymXl4+742Cvw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779373131; c=relaxed/simple; bh=55YlRYChFr/1kv/e7yVZ5w04U8HdZ0Qv46fqHYbd8lc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=pyD3zt8tTt1lzoITKNaDOVeVIGWPN8lfsvNcZ1RpjM2OfOw334kQhbse5S4jcsKUa9twMakNYOrcfGTC5BzMO7nLVPtmUZVfe14F8c+b0UU981YRrvbRFPDx0MUo7lfEWjnQN/qVo6PXJIZL7OonfLWllEOwvgNAeFGu/4d7tl8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=hG8Bi92U; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="hG8Bi92U" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1779373128; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=fk3Xl9XlxIYip2ISIzinPX3J5ZgeotOzmja4RirWRCE=; b=hG8Bi92UU7ZOHDY8Pr9mpnF+OqRr7nPLq0WppLsag1kmv9rPS2IbgzQYF7IpRLg8rPrE5H 3z/R+Ywtha/rlGR5XHcc/x+sCY7W0yKKO63RYrbVgdFnMAYCJOep5iVBoAWmnVZKnnKm/s CAPH3vBfSbL1kVePIlhi/6nPK5QqrB4= Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-711-dXLOjquUMIOEdpX706_Fsg-1; Thu, 21 May 2026 10:18:45 -0400 X-MC-Unique: dXLOjquUMIOEdpX706_Fsg-1 X-Mimecast-MFC-AGG-ID: dXLOjquUMIOEdpX706_Fsg_1779373124 Received: from mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.95]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id DA2BE1956066; Thu, 21 May 2026 14:18:43 +0000 (UTC) Received: from fedora.brq.redhat.com (unknown [10.43.17.109]) by mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id AE56E1684; Thu, 21 May 2026 14:18:40 +0000 (UTC) From: Tomas Glozar To: Steven Rostedt , Tomas Glozar Cc: John Kacur , Luis Goncalves , Crystal Wood , Costa Shulyupin , Wander Lairson Costa , Ivan Pravdin , Namhyung Kim , Ian Rogers , Arnaldo Carvalho de Melo , LKML , linux-trace-kernel , linux-perf-users Subject: [PATCH v2 1/6] rtla: Add libsubcmd dependency Date: Thu, 21 May 2026 16:18:28 +0200 Message-ID: <20260521141833.2353025-2-tglozar@redhat.com> In-Reply-To: <20260521141833.2353025-1-tglozar@redhat.com> References: <20260521141833.2353025-1-tglozar@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.6 on 10.30.177.95 Content-Type: text/plain; charset="utf-8" In preparation for migrating RTLA to libsubcmd, build libsubcmd from the appropriate directory next to the RTLA build proper, and link the resulting object to RTLA. libsubcmd uses str_error_r() and strlcpy() at several places. To support these, also link the respective libraries from tools/lib. For completeness, also add tools/include to include path. This will allow other userspace functions and macros shipped with the kernel to be used in RTLA; perf and bpftool, two other users of libsubcmd, already do that. To prevent a name conflict, rename RTLA's run_command() function to run_tool_command(), and replace RTLA's own container_of implementation with the one in tools/include/linux/container_of.h. Assisted-by: Composer:composer-1 Signed-off-by: Tomas Glozar --- tools/tracing/rtla/.gitignore | 2 ++ tools/tracing/rtla/Makefile | 66 ++++++++++++++++++++++++++++++---- tools/tracing/rtla/src/rtla.c | 8 ++--- tools/tracing/rtla/src/utils.h | 6 ++-- 4 files changed, 67 insertions(+), 15 deletions(-) diff --git a/tools/tracing/rtla/.gitignore b/tools/tracing/rtla/.gitignore index 231fb8d67f97..c7b4bf1c8ba9 100644 --- a/tools/tracing/rtla/.gitignore +++ b/tools/tracing/rtla/.gitignore @@ -10,3 +10,5 @@ custom_filename.txt osnoise_irq_noise_hist.txt osnoise_trace.txt timerlat_trace.txt +libsubcmd/ +lib/ diff --git a/tools/tracing/rtla/Makefile b/tools/tracing/rtla/Makefile index 45690ee14544..19eb5581fa23 100644 --- a/tools/tracing/rtla/Makefile +++ b/tools/tracing/rtla/Makefile @@ -27,6 +27,30 @@ endif RTLA :=3D $(OUTPUT)rtla RTLA_IN :=3D $(RTLA)-in.o =20 +LIBSUBCMD_DIR =3D $(srctree)/tools/lib/subcmd/ +ifneq ($(OUTPUT),) + LIBSUBCMD_OUTPUT =3D $(abspath $(OUTPUT))/libsubcmd +else + LIBSUBCMD_OUTPUT =3D $(CURDIR)/libsubcmd +endif +LIBSUBCMD =3D $(LIBSUBCMD_OUTPUT)/libsubcmd.a +LIBSUBCMD_INCLUDES =3D $(LIBSUBCMD_OUTPUT)/include +LIBSUBCMD_MAKEFLAGS =3D O=3D$(LIBSUBCMD_OUTPUT) DESTDIR=3D$(LIBSUBCMD_OUTP= UT) prefix=3D subdir=3D + +TOOLS_INCLUDES =3D -I$(srctree)/tools/include + +ifneq ($(OUTPUT),) + LIB_OUTPUT =3D $(abspath $(OUTPUT))/lib +else + LIB_OUTPUT =3D $(CURDIR)/lib +endif + +LIB_STRING =3D $(LIB_OUTPUT)/string.o +LIB_STRING_SRC =3D $(srctree)/tools/lib/string.c + +LIB_STR_ERROR_R =3D $(LIB_OUTPUT)/str_error_r.o +LIB_STR_ERROR_R_SRC =3D $(srctree)/tools/lib/str_error_r.c + VERSION :=3D $(shell sh -c "make -sC ../../.. kernelversion | grep -v mak= e") DOCSRC :=3D ../../../Documentation/tools/rtla/ =20 @@ -66,7 +90,7 @@ ifeq ($(config),1) include Makefile.config endif =20 -CFLAGS +=3D $(INCLUDES) $(LIB_INCLUDES) +CFLAGS +=3D $(INCLUDES) $(LIB_INCLUDES) $(TOOLS_INCLUDES) -I$(LIBSUBCMD_I= NCLUDES) =20 export CFLAGS OUTPUT srctree =20 @@ -93,20 +117,48 @@ tests/bpf/bpf_action_map.o: tests/bpf/bpf_action_map.c $(Q)echo "BPF skeleton support is disabled, skipping tests/bpf/bpf_action= _map.o" endif =20 -$(RTLA): $(RTLA_IN) - $(QUIET_LINK)$(CC) $(LDFLAGS) -o $(RTLA) $(RTLA_IN) $(EXTLIBS) +$(RTLA): $(RTLA_IN) $(LIBSUBCMD) $(LIB_STRING) $(LIB_STR_ERROR_R) + $(QUIET_LINK)$(CC) $(LDFLAGS) -o $(RTLA) $(RTLA_IN) $(LIBSUBCMD) $(LIB_ST= RING) $(LIB_STR_ERROR_R) $(EXTLIBS) =20 -static: $(RTLA_IN) +static: $(RTLA_IN) $(LIBSUBCMD) $(LIB_STRING) $(LIB_STR_ERROR_R) $(eval LDFLAGS +=3D -static) - $(QUIET_LINK)$(CC) -static $(LDFLAGS) -o $(RTLA)-static $(RTLA_IN) $(EXT= LIBS) + $(QUIET_LINK)$(CC) -static $(LDFLAGS) -o $(RTLA)-static $(RTLA_IN) $(LIBS= UBCMD) $(LIB_STRING) $(LIB_STR_ERROR_R) $(EXTLIBS) =20 rtla.%: fixdep FORCE make -f $(srctree)/tools/build/Makefile.build dir=3D. $@ =20 -$(RTLA_IN): fixdep FORCE src/timerlat.skel.h +$(RTLA_IN): fixdep FORCE src/timerlat.skel.h $(LIBSUBCMD_INCLUDES) make $(build)=3Drtla =20 -clean: doc_clean fixdep-clean +$(LIBSUBCMD_OUTPUT): + $(Q)$(MKDIR) -p $@ + +$(LIBSUBCMD_INCLUDES): $(LIBSUBCMD_OUTPUT) + $(Q)$(MAKE) -C $(LIBSUBCMD_DIR) $(LIBSUBCMD_MAKEFLAGS) \ + install_headers + +$(LIBSUBCMD): fixdep $(LIBSUBCMD_OUTPUT) + $(Q)$(MAKE) -C $(LIBSUBCMD_DIR) $(LIBSUBCMD_MAKEFLAGS) \ + $@ + +$(LIB_OUTPUT): + $(Q)$(MKDIR) -p $@ + +$(LIB_STR_ERROR_R): $(LIB_STR_ERROR_R_SRC) $(LIB_OUTPUT) + $(QUIET_CC)$(CC) $(CFLAGS) -c -o $@ $< + +$(LIB_STRING): $(LIB_STRING_SRC) $(LIB_OUTPUT) + $(QUIET_CC)$(CC) $(CFLAGS) -c -o $@ $< + +libsubcmd-clean: + $(call QUIET_CLEAN, libsubcmd) + $(Q)$(RM) -r -- $(LIBSUBCMD_OUTPUT) + +lib-clean: + $(call QUIET_CLEAN, lib) + $(Q)$(RM) -r -- $(LIB_OUTPUT) + +clean: doc_clean fixdep-clean libsubcmd-clean lib-clean $(call QUIET_CLEAN, rtla) $(Q)find . -name '*.o' -delete -o -name '\.*.cmd' -delete -o -name '\.*.d= ' -delete $(Q)rm -f rtla rtla-static fixdep FEATURE-DUMP rtla-* diff --git a/tools/tracing/rtla/src/rtla.c b/tools/tracing/rtla/src/rtla.c index 3398250076ea..f4342ca684c3 100644 --- a/tools/tracing/rtla/src/rtla.c +++ b/tools/tracing/rtla/src/rtla.c @@ -38,12 +38,12 @@ static void rtla_usage(int err) } =20 /* - * run_command - try to run a rtla tool command + * run_tool_command - try to run a rtla tool command * * It returns 0 if it fails. The tool's main will generally not * return as they should call exit(). */ -int run_command(int argc, char **argv, int start_position) +int run_tool_command(int argc, char **argv, int start_position) { if (strcmp(argv[start_position], "osnoise") =3D=3D 0) { osnoise_main(argc-start_position, &argv[start_position]); @@ -69,7 +69,7 @@ int main(int argc, char *argv[]) int retval; =20 /* is it an alias? */ - retval =3D run_command(argc, argv, 0); + retval =3D run_tool_command(argc, argv, 0); if (retval) exit(0); =20 @@ -82,7 +82,7 @@ int main(int argc, char *argv[]) rtla_usage(0); } =20 - retval =3D run_command(argc, argv, 1); + retval =3D run_tool_command(argc, argv, 1); if (retval) exit(0); =20 diff --git a/tools/tracing/rtla/src/utils.h b/tools/tracing/rtla/src/utils.h index e794ede64b2c..96fd72042717 100644 --- a/tools/tracing/rtla/src/utils.h +++ b/tools/tracing/rtla/src/utils.h @@ -7,6 +7,8 @@ #include #include =20 +#include + /* * '18446744073709551615\0' */ @@ -37,10 +39,6 @@ static inline bool str_has_prefix(const char *str, const= char *prefix) return strncmp(str, prefix, strlen(prefix)) =3D=3D 0; } =20 -#define container_of(ptr, type, member)({ \ - const typeof(((type *)0)->member) *__mptr =3D (ptr); \ - (type *)((char *)__mptr - offsetof(type, member)) ; }) - extern int config_debug; void debug_msg(const char *fmt, ...); void err_msg(const char *fmt, ...); --=20 2.54.0 From nobody Sun May 24 20:34:47 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 86DCD306743 for ; Thu, 21 May 2026 14:18:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779373135; cv=none; b=eQ1oik2hioHjK+nRGDqITR3mPa5qaF/7uG+2XqgEzEoiIVlk+emqJIy3C41qZ+/aGkq/VwwsfLr+1QZwu1w9vCR6lw66PhfDcyBmfFvI8NJTQlDRpNGwOBf90/yobqEoC3aw/ao6t+YkvkE2n7CrLFs2GsZiODbXUIw39KBN1SA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779373135; c=relaxed/simple; bh=gzqgFNoB5ibYYcggtyU17s+jVSr4H2qmZTUcTXw6d1I=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=l182/CDAP4F5OeBR4d5WWSwG+/dZElKj6TX7GRQcEJAK/77tMqyCWU5oYL3dIm6XQb0nh6i9cvoFUzotoUnNpRIcSYUuUkko/7Jdw24gSdpQNEKDcdF3EGjqtqXeFPxmENmvh2N86dm+JVMDC/FI9zSC4y8AVWIVRpwsC2ubRc0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=YBjwpwXD; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="YBjwpwXD" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1779373132; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=uZYGTasRkRL1pH2vV9nYtelkEq8mhaLf0iEW2VMecWQ=; b=YBjwpwXDzLt//uEp/Eon680yTfYVveZy0X3cuBk0YnfgM76P5hSEMTDVovL3Tlblwthh4Q 5Zbb9esiMdZ1u04ELMJtRzuQANISsbAHu+2nG3yJAobQT2U8Aoa7iuVGE+NjkmyLgbkgfu tPiuumJWMpE4EnAIftqKp3y0UlhTRqQ= Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-658-NR4FxkqOOVSlPMf5bQQMsA-1; Thu, 21 May 2026 10:18:49 -0400 X-MC-Unique: NR4FxkqOOVSlPMf5bQQMsA-1 X-Mimecast-MFC-AGG-ID: NR4FxkqOOVSlPMf5bQQMsA_1779373127 Received: from mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.95]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 89A5A180062C; Thu, 21 May 2026 14:18:47 +0000 (UTC) Received: from fedora.brq.redhat.com (unknown [10.43.17.109]) by mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 55AC51684; Thu, 21 May 2026 14:18:44 +0000 (UTC) From: Tomas Glozar To: Steven Rostedt , Tomas Glozar Cc: John Kacur , Luis Goncalves , Crystal Wood , Costa Shulyupin , Wander Lairson Costa , Ivan Pravdin , Namhyung Kim , Ian Rogers , Arnaldo Carvalho de Melo , LKML , linux-trace-kernel , linux-perf-users Subject: [PATCH v2 2/6] tools subcmd: support optarg as separate argument Date: Thu, 21 May 2026 16:18:29 +0200 Message-ID: <20260521141833.2353025-3-tglozar@redhat.com> In-Reply-To: <20260521141833.2353025-1-tglozar@redhat.com> References: <20260521141833.2353025-1-tglozar@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.6 on 10.30.177.95 Content-Type: text/plain; charset="utf-8" In addition to "-ovalue" and "--opt=3Dvalue" syntax, allow also "-o value" and "--opt value" for options with optional argument when the newly added PARSE_OPT_OPTARG_ALLOW_NEXT flag is set. This behavior is turned off by default since it does not make sense for tools using non-option command line arguments. Consider the ambiguity of "cmd -d x", where "-d x" can mean either "-d with argument of x" or "-d without argument, followed by non-option argument x". This is not an issue in the case that the tool takes no non-option arguments. To implement this, a new local variable, force_defval, is created in get_value(), along with a comment explaining the logic. Signed-off-by: Tomas Glozar --- tools/lib/subcmd/parse-options.c | 53 +++++++++++++++++++++++++++----- tools/lib/subcmd/parse-options.h | 1 + 2 files changed, 46 insertions(+), 8 deletions(-) diff --git a/tools/lib/subcmd/parse-options.c b/tools/lib/subcmd/parse-opti= ons.c index 555d617c1f50..664b2053bb77 100644 --- a/tools/lib/subcmd/parse-options.c +++ b/tools/lib/subcmd/parse-options.c @@ -72,6 +72,7 @@ static int get_value(struct parse_opt_ctx_t *p, const char *s, *arg =3D NULL; const int unset =3D flags & OPT_UNSET; int err; + bool force_defval =3D false; =20 if (unset && p->opt) return opterror(opt, "takes no value", flags); @@ -123,6 +124,42 @@ static int get_value(struct parse_opt_ctx_t *p, } } =20 + if (opt->flags & PARSE_OPT_OPTARG && !p->opt) { + if (!(p->flags & PARSE_OPT_OPTARG_ALLOW_NEXT)) { + /* + * If the option has an optional argument, and the argument is not + * provided in the option itself, do not attempt to get it from + * the next argument, unless PARSE_OPT_OPTARG_ALLOW_NEXT is set. + * + * This prevents a non-option argument from being interpreted as an + * optional argument of a preceding option, for example: + * + * $ cmd --opt val + * -> is "val" argument of "--opt" or a separate non-option + * argument? + * + * With PARSE_OPT_OPTARG_ALLOW_NEXT, "val" is interpreted as + * the argument of "--opt", i.e. the same as "--opt=3Dval". + * Without PARSE_OPT_OPTARG_ALLOW_NEXT, --opt is interpreted + * as having the default value, and "val" as a separate non-option + * argument. + * + * PARSE_OPT_OPTARG_ALLOW_NEXT is useful for commands that take no + * non-option arguments and want to allow more flexibility in + * optional argument passing. + */ + force_defval =3D true; + } + + if (p->argc <=3D 1 || p->argv[1][0] =3D=3D '-') { + /* + * If next argument is an option or does not exist, + * use the default value. + */ + force_defval =3D true; + } + } + if (opt->flags & PARSE_OPT_NOBUILD) { char reason[128]; bool noarg =3D false; @@ -148,7 +185,7 @@ static int get_value(struct parse_opt_ctx_t *p, noarg =3D true; if (opt->flags & PARSE_OPT_NOARG) noarg =3D true; - if (opt->flags & PARSE_OPT_OPTARG && !p->opt) + if (force_defval) noarg =3D true; =20 switch (opt->type) { @@ -212,7 +249,7 @@ static int get_value(struct parse_opt_ctx_t *p, err =3D 0; if (unset) *(const char **)opt->value =3D NULL; - else if (opt->flags & PARSE_OPT_OPTARG && !p->opt) + else if (force_defval) *(const char **)opt->value =3D (const char *)opt->defval; else err =3D get_arg(p, opt, flags, (const char **)opt->value); @@ -244,7 +281,7 @@ static int get_value(struct parse_opt_ctx_t *p, return (*opt->callback)(opt, NULL, 1) ? (-1) : 0; if (opt->flags & PARSE_OPT_NOARG) return (*opt->callback)(opt, NULL, 0) ? (-1) : 0; - if (opt->flags & PARSE_OPT_OPTARG && !p->opt) + if (force_defval) return (*opt->callback)(opt, NULL, 0) ? (-1) : 0; if (get_arg(p, opt, flags, &arg)) return -1; @@ -255,7 +292,7 @@ static int get_value(struct parse_opt_ctx_t *p, *(int *)opt->value =3D 0; return 0; } - if (opt->flags & PARSE_OPT_OPTARG && !p->opt) { + if (force_defval) { *(int *)opt->value =3D opt->defval; return 0; } @@ -271,7 +308,7 @@ static int get_value(struct parse_opt_ctx_t *p, *(unsigned int *)opt->value =3D 0; return 0; } - if (opt->flags & PARSE_OPT_OPTARG && !p->opt) { + if (force_defval) { *(unsigned int *)opt->value =3D opt->defval; return 0; } @@ -289,7 +326,7 @@ static int get_value(struct parse_opt_ctx_t *p, *(long *)opt->value =3D 0; return 0; } - if (opt->flags & PARSE_OPT_OPTARG && !p->opt) { + if (force_defval) { *(long *)opt->value =3D opt->defval; return 0; } @@ -305,7 +342,7 @@ static int get_value(struct parse_opt_ctx_t *p, *(unsigned long *)opt->value =3D 0; return 0; } - if (opt->flags & PARSE_OPT_OPTARG && !p->opt) { + if (force_defval) { *(unsigned long *)opt->value =3D opt->defval; return 0; } @@ -321,7 +358,7 @@ static int get_value(struct parse_opt_ctx_t *p, *(u64 *)opt->value =3D 0; return 0; } - if (opt->flags & PARSE_OPT_OPTARG && !p->opt) { + if (force_defval) { *(u64 *)opt->value =3D opt->defval; return 0; } diff --git a/tools/lib/subcmd/parse-options.h b/tools/lib/subcmd/parse-opti= ons.h index 8e9147358a28..c573a0ca5ca6 100644 --- a/tools/lib/subcmd/parse-options.h +++ b/tools/lib/subcmd/parse-options.h @@ -33,6 +33,7 @@ enum parse_opt_flags { PARSE_OPT_KEEP_ARGV0 =3D 4, PARSE_OPT_KEEP_UNKNOWN =3D 8, PARSE_OPT_NO_INTERNAL_HELP =3D 16, + PARSE_OPT_OPTARG_ALLOW_NEXT =3D 32, }; =20 enum parse_opt_option_flags { --=20 2.54.0 From nobody Sun May 24 20:34:47 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2867C3D6CC7 for ; Thu, 21 May 2026 14:18:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779373138; cv=none; b=XNxGuhSmGbK2l6a4AX7SMEXOfGVm3soDlNmoRglSURayyclIndK4rFXy0u/SpxBCnBLPKW9BV6R1wmebd+7aX5+ag59qkG7d3UtHErx4CgzcXdQE/BEaklMVheNZBcgqq53PW2uAxsRl4GoZBR+TshwKTspCE7RYffmxMb2W+Es= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779373138; c=relaxed/simple; bh=pP9zymcXj1EdhFLVo2c4W4swf8P6e2WPmGdTahTiMxs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=nAnKSkJK3GfFsEAmht4+Htxg61tbS1sHvdEXgTXbvxPS1LKtfS3pA3jHAIyMP4tMIiLPVFcyi+qk60LgskkTdnon9P+fUz9egA8pUXCpvEuSwgOQ8c3LpLdoST+0STa5H1Cq0MV7rrhktx3SuXMDQGivrwDQORlJJVZeAjbVtos= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=eAnhDzJT; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="eAnhDzJT" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1779373136; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=A6jNa+GQSgA+jzK73QK9g09OBoehVz9w9jxoOFMsCm4=; b=eAnhDzJTaxGfMjVTsZ+/FNQTtD4xU2VpPkmkGAdwUYhS8InRbeRqWhZ5ZHYwv0q0DI1bwv Skq6TX2YRov2Ehkw034Insn3umG1JI+WVOQL3xExpPAm/XfWE1ChdHB1K7pwoxf4xiaJ/w kQa7Lwq1DEdIQ0SLyQp1no45AGp24CI= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-282-WlQ7H0M6OTqEd4TdNH7h-g-1; Thu, 21 May 2026 10:18:52 -0400 X-MC-Unique: WlQ7H0M6OTqEd4TdNH7h-g-1 X-Mimecast-MFC-AGG-ID: WlQ7H0M6OTqEd4TdNH7h-g_1779373131 Received: from mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.95]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 11DB21956062; Thu, 21 May 2026 14:18:51 +0000 (UTC) Received: from fedora.brq.redhat.com (unknown [10.43.17.109]) by mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id E4F4A1687; Thu, 21 May 2026 14:18:47 +0000 (UTC) From: Tomas Glozar To: Steven Rostedt , Tomas Glozar Cc: John Kacur , Luis Goncalves , Crystal Wood , Costa Shulyupin , Wander Lairson Costa , Ivan Pravdin , Namhyung Kim , Ian Rogers , Arnaldo Carvalho de Melo , LKML , linux-trace-kernel , linux-perf-users Subject: [PATCH v2 3/6] tools subcmd: allow parsing distinct --opt and --no-opt Date: Thu, 21 May 2026 16:18:30 +0200 Message-ID: <20260521141833.2353025-4-tglozar@redhat.com> In-Reply-To: <20260521141833.2353025-1-tglozar@redhat.com> References: <20260521141833.2353025-1-tglozar@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.6 on 10.30.177.95 Content-Type: text/plain; charset="utf-8" libsubcmd automatically generates for every option --opt an equivalent negated option, --no-opt, to unset the option. Vice versa, for every option declared as --no-opt, a shorthand --opt is declared for convenience. Add a flag, PARSE_OPT_NOAUTONEG, to disable this behavior. This new flag behaves similarly to the already existing PARSE_OPT_NONEG, only it does not reject the --no-opt variant, but leaves it undefined. That is useful when there is a conflicting distinct --no-opt option in the syntax of the tool. PARSE_OPT_NOAUTONEG is enabled per-option, allowing to unset other options that do not have this conflict. Signed-off-by: Tomas Glozar --- tools/lib/subcmd/parse-options.c | 10 ++++++---- tools/lib/subcmd/parse-options.h | 3 +++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/tools/lib/subcmd/parse-options.c b/tools/lib/subcmd/parse-opti= ons.c index 664b2053bb77..e83200e9f56a 100644 --- a/tools/lib/subcmd/parse-options.c +++ b/tools/lib/subcmd/parse-options.c @@ -427,7 +427,8 @@ static int parse_long_opt(struct parse_opt_ctx_t *p, co= nst char *arg, return 0; } if (!rest) { - if (strstarts(options->long_name, "no-")) { + if (strstarts(options->long_name, "no-") && + !(options->flags & PARSE_OPT_NOAUTONEG)) { /* * The long name itself starts with "no-", so * accept the option without "no-" so that users @@ -465,12 +466,12 @@ static int parse_long_opt(struct parse_opt_ctx_t *p, = const char *arg, continue; } /* negated and abbreviated very much? */ - if (strstarts("no-", arg)) { + if (strstarts("no-", arg) && !(options->flags & PARSE_OPT_NOAUTONEG)) { flags |=3D OPT_UNSET; goto is_abbreviated; } /* negated? */ - if (strncmp(arg, "no-", 3)) + if (strncmp(arg, "no-", 3) || (options->flags & PARSE_OPT_NOAUTONEG)) continue; flags |=3D OPT_UNSET; rest =3D skip_prefix(arg + 3, options->long_name); @@ -1019,7 +1020,8 @@ int parse_options_usage(const char * const *usagestr, if (strstarts(opts->long_name, optstr)) print_option_help(opts, 0); if (strstarts("no-", optstr) && - strstarts(opts->long_name, optstr + 3)) + strstarts(opts->long_name, optstr + 3) && + !(opts->flags & PARSE_OPT_NOAUTONEG)) print_option_help(opts, 0); } =20 diff --git a/tools/lib/subcmd/parse-options.h b/tools/lib/subcmd/parse-opti= ons.h index c573a0ca5ca6..38df5fd21963 100644 --- a/tools/lib/subcmd/parse-options.h +++ b/tools/lib/subcmd/parse-options.h @@ -47,6 +47,7 @@ enum parse_opt_option_flags { PARSE_OPT_NOEMPTY =3D 128, PARSE_OPT_NOBUILD =3D 256, PARSE_OPT_CANSKIP =3D 512, + PARSE_OPT_NOAUTONEG =3D 1024, }; =20 struct option; @@ -149,6 +150,8 @@ struct option { { .type =3D OPTION_CALLBACK, .short_name =3D (s), .long_name =3D (l), .va= lue =3D (v), .argh =3D "time", .help =3D (h), .callback =3D parse_opt_appro= xidate_cb } #define OPT_CALLBACK(s, l, v, a, h, f) \ { .type =3D OPTION_CALLBACK, .short_name =3D (s), .long_name =3D (l), .va= lue =3D (v), .argh =3D (a), .help =3D (h), .callback =3D (f) } +#define OPT_CALLBACK_FLAG(s, l, v, a, h, f, fl) \ + { .type =3D OPTION_CALLBACK, .short_name =3D (s), .long_name =3D (l), .va= lue =3D (v), .argh =3D (a), .help =3D (h), .callback =3D (f), .flags =3D (f= l) } #define OPT_CALLBACK_SET(s, l, v, os, a, h, f) \ { .type =3D OPTION_CALLBACK, .short_name =3D (s), .long_name =3D (l), .va= lue =3D (v), .argh =3D (a), .help =3D (h), .callback =3D (f), .set =3D chec= k_vtype(os, bool *)} #define OPT_CALLBACK_NOOPT(s, l, v, a, h, f) \ --=20 2.54.0 From nobody Sun May 24 20:34:47 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EFCBF3DB332 for ; Thu, 21 May 2026 14:19:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779373145; cv=none; b=At6tpDVKVQqdPQvneCPdO7i0B28bsA42J095sDFJ0t0c7WEffJCteC/bslGd21+TvJZB6nt7oazJsiqGKbtp0Z1oj+MbqKy1HtgSktqGWBIeoDVM6v63UqutCb/tDBKTivFAUWB9Fj7fnhOE7asxL1bk0/7hg5sDICokgwJRifc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779373145; c=relaxed/simple; bh=u94mWAcQH9VoBoBHBMNBqbzenxuSOnadHvjBa3SQRxg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=aWF4bWtWYbLOcdzdPUVhF6H3OQkepDPyLH9bGECoOYSV9jB0K8OWwWGPi0JMEJe96Zr/YwqXhy05zbXlUKMvYGFd7OLh8RA8vVHSvCumlUhcU88FoqIP6hT2Zn1cap7kBqlHWXT319JYnr7WcuvVi8LJnzc86gkK4mfJPeEi6gs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=fy+s2neV; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="fy+s2neV" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1779373140; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=aGoY94Fm6JxP8yLXk9zcaU6r6KchyIK0b/MHv8tGafY=; b=fy+s2neVMgoPPx5jxR6QFjKXoxdIkeIW0FPw33cQrxrsWrMrjA6sV2iRGKNaX0Rp4nGamc 7dZpWo7dZzsV9KnBQXtNom1Zrmd2SPWmU4BIkx8f3+TioUe5OjFkJ1bMTpwfXK0F0YdjmZ VVBEyRW8g32fb6FdY7YjDkyGEB7zvks= Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-504-VopOugzTNiyijjBJ8RVrGA-1; Thu, 21 May 2026 10:18:56 -0400 X-MC-Unique: VopOugzTNiyijjBJ8RVrGA-1 X-Mimecast-MFC-AGG-ID: VopOugzTNiyijjBJ8RVrGA_1779373135 Received: from mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.95]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id EE81A19560B4; Thu, 21 May 2026 14:18:54 +0000 (UTC) Received: from fedora.brq.redhat.com (unknown [10.43.17.109]) by mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 6D86A1684; Thu, 21 May 2026 14:18:51 +0000 (UTC) From: Tomas Glozar To: Steven Rostedt , Tomas Glozar Cc: John Kacur , Luis Goncalves , Crystal Wood , Costa Shulyupin , Wander Lairson Costa , Ivan Pravdin , Namhyung Kim , Ian Rogers , Arnaldo Carvalho de Melo , LKML , linux-trace-kernel , linux-perf-users Subject: [PATCH v2 4/6] rtla: Parse cmdline using libsubcmd Date: Thu, 21 May 2026 16:18:31 +0200 Message-ID: <20260521141833.2353025-5-tglozar@redhat.com> In-Reply-To: <20260521141833.2353025-1-tglozar@redhat.com> References: <20260521141833.2353025-1-tglozar@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.6 on 10.30.177.95 Content-Type: text/plain; charset="utf-8" Instead of using getopt_long() directly to parse the command line arguments given to an RTLA tool, use libsubcmd's parse_options(). Utilizing libsubcmd for parsing command line arguments has several benefits: - A help message is automatically generated by libsubcmd from the specification, removing the need of writing it by hand. - Options are sorted into groups based on which part of tracing (CPU, thread, auto-analysis, tuning, histogram) they relate to. - Common parsing patterns for numerical and boolean values now share code, with the target variable being stored in the option array. To avoid duplication of the option parsing logic, RTLA-specific macros defining struct option values are created: - RTLA_OPT_* for options common to all tools - OSNOISE_OPT_* and TIMERLAT_OPT_* for options specific to osnoise/timerlat tools - HIST_OPT_* macros for options specific to histogram-based tools. Individual *_parse_args() functions then construct an array out of these macros that is then passed to libsubcmd's parse_options(). All code specific to command line options parsing is moved out of the individual tool files into a new file, cli.c, which also contains the contents of the rtla.c file. A private header, cli_p.h, is added alongside the public header cli.h, so that unit tests are able to test statically declared option callbacks. Minor changes: - The return value of tool-level help option changes to 129, as this is the value set by libsubcmd; this is reflected in affected test cases. The implementation of help for command-level and tracer-level help is set to 129 as well for consistency, and the change is reflected in exit value documentation. - Related to the above, {rtla,osnoise,timerlat}_usage() are marked __noreturn and exit() is removed from after they are called for cleaner code. - The error messages for invalid argument for options --dma-latency and -E/--entries were corrected, fixing off-by-one in the limits. Note that unsetting options (using --no- syntax) is currently not implemented for options that use custom callbacks. For --irq and --thread, it will never be implemented, as they conflict with already existing --no-irq and --no-thread with a different meaning. Assisted-by: Composer:composer-1.5 Signed-off-by: Tomas Glozar --- Documentation/tools/rtla/common_appendix.txt | 7 +- tools/tracing/rtla/src/Build | 2 +- tools/tracing/rtla/src/cli.c | 537 +++++++++++++++ tools/tracing/rtla/src/cli.h | 7 + tools/tracing/rtla/src/cli_p.h | 670 +++++++++++++++++++ tools/tracing/rtla/src/common.c | 109 --- tools/tracing/rtla/src/common.h | 27 +- tools/tracing/rtla/src/osnoise.c | 9 +- tools/tracing/rtla/src/osnoise_hist.c | 221 +----- tools/tracing/rtla/src/osnoise_top.c | 200 +----- tools/tracing/rtla/src/rtla.c | 92 --- tools/tracing/rtla/src/timerlat.c | 9 +- tools/tracing/rtla/src/timerlat.h | 4 +- tools/tracing/rtla/src/timerlat_hist.c | 317 +-------- tools/tracing/rtla/src/timerlat_top.c | 286 +------- tools/tracing/rtla/src/utils.c | 28 +- tools/tracing/rtla/src/utils.h | 3 +- tools/tracing/rtla/tests/hwnoise.t | 2 +- tools/tracing/rtla/tests/osnoise.t | 6 +- tools/tracing/rtla/tests/timerlat.t | 6 +- 20 files changed, 1256 insertions(+), 1286 deletions(-) create mode 100644 tools/tracing/rtla/src/cli.c create mode 100644 tools/tracing/rtla/src/cli.h create mode 100644 tools/tracing/rtla/src/cli_p.h delete mode 100644 tools/tracing/rtla/src/rtla.c diff --git a/Documentation/tools/rtla/common_appendix.txt b/Documentation/t= ools/rtla/common_appendix.txt index 8c90a02588e7..68cb15840d3a 100644 --- a/Documentation/tools/rtla/common_appendix.txt +++ b/Documentation/tools/rtla/common_appendix.txt @@ -26,9 +26,10 @@ EXIT STATUS =20 :: =20 - 0 Passed: the test did not hit the stop tracing condition - 1 Error: invalid argument - 2 Failed: the test hit the stop tracing condition + 0 Passed: the test did not hit the stop tracing condition + 1 Error: invalid argument + 2 Failed: the test hit the stop tracing condition + 129 Help: either user requested help or incorrect option was specified =20 REPORTING BUGS =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D diff --git a/tools/tracing/rtla/src/Build b/tools/tracing/rtla/src/Build index 329e24a40cf7..a1f3ab927207 100644 --- a/tools/tracing/rtla/src/Build +++ b/tools/tracing/rtla/src/Build @@ -11,4 +11,4 @@ rtla-y +=3D timerlat_hist.o rtla-y +=3D timerlat_u.o rtla-y +=3D timerlat_aa.o rtla-y +=3D timerlat_bpf.o -rtla-y +=3D rtla.o +rtla-y +=3D cli.o diff --git a/tools/tracing/rtla/src/cli.c b/tools/tracing/rtla/src/cli.c new file mode 100644 index 000000000000..7f531519df44 --- /dev/null +++ b/tools/tracing/rtla/src/cli.c @@ -0,0 +1,537 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2021 Red Hat Inc, Daniel Bristot de Oliveira + */ + +#define _GNU_SOURCE +#include +#include +#include +#include +#include + +#include + +#define RTLA_ALLOW_CLI_P_H +#include "cli_p.h" + +static const char * const osnoise_top_usage[] =3D { + "rtla osnoise [top] [] [-h|--help]", + NULL, +}; + +static const char * const osnoise_hist_usage[] =3D { + "rtla osnoise hist [] [-h|--help]", + NULL, +}; + +static const char * const timerlat_top_usage[] =3D { + "rtla timerlat [top] [] [-h|--help]", + NULL, +}; + +static const char * const timerlat_hist_usage[] =3D { + "rtla timerlat hist [] [-h|--help]", + NULL, +}; + +static const char * const hwnoise_usage[] =3D { + "rtla hwnoise [] [-h|--help]", + NULL, +}; + +static const int common_parse_options_flags =3D PARSE_OPT_OPTARG_ALLOW_NEX= T; + +bool in_unit_test; + +/* + * osnoise_top_parse_args - allocs, parse and fill the cmd line parameters + */ +struct common_params *osnoise_top_parse_args(int argc, char **argv) +{ + struct osnoise_params *params; + struct osnoise_cb_data cb_data; + const char * const *usage; + + params =3D calloc_fatal(1, sizeof(*params)); + + cb_data.params =3D params; + cb_data.trace_output =3D NULL; + + if (strcmp(argv[0], "hwnoise") =3D=3D 0) { + params->mode =3D MODE_HWNOISE; + /* + * Reduce CPU usage for 75% to avoid killing the system. + */ + params->runtime =3D 750000; + params->period =3D 1000000; + usage =3D hwnoise_usage; + } else { + usage =3D osnoise_top_usage; + } + + const struct option osnoise_top_options[] =3D { + OPT_GROUP("Tracing Options:"), + OSNOISE_OPT_PERIOD, + OSNOISE_OPT_RUNTIME, + RTLA_OPT_STOP('s', "stop", "single sample"), + RTLA_OPT_STOP_TOTAL('S', "stop-total", "total sample"), + OSNOISE_OPT_THRESHOLD, + RTLA_OPT_TRACE_OUTPUT("osnoise", opt_osnoise_trace_output_cb), + + OPT_GROUP("Event Configuration:"), + RTLA_OPT_EVENT, + RTLA_OPT_FILTER, + RTLA_OPT_TRIGGER, + + OPT_GROUP("CPU Configuration:"), + RTLA_OPT_CPUS, + RTLA_OPT_HOUSEKEEPING, + + OPT_GROUP("Thread Configuration:"), + RTLA_OPT_PRIORITY, + RTLA_OPT_CGROUP, + + OPT_GROUP("Output:"), + RTLA_OPT_QUIET, + + OPT_GROUP("System Tuning:"), + RTLA_OPT_TRACE_BUFFER_SIZE, + RTLA_OPT_WARM_UP, + + OPT_GROUP("Auto Analysis and Actions:"), + RTLA_OPT_AUTO(opt_osnoise_auto_cb), + RTLA_OPT_ON_THRESHOLD("stop-total", opt_osnoise_on_threshold_cb), + RTLA_OPT_ON_END(opt_osnoise_on_end_cb), + + OPT_GROUP("General:"), + RTLA_OPT_DURATION, + RTLA_OPT_DEBUG, + + OPT_END(), + }; + + actions_init(¶ms->common.threshold_actions); + actions_init(¶ms->common.end_actions); + + argc =3D parse_options(argc, (const char **)argv, + osnoise_top_options, + usage, + common_parse_options_flags); + if (argc < 0) + return NULL; + + if (cb_data.trace_output) + actions_add_trace_output(¶ms->common.threshold_actions, cb_data.trac= e_output); + + if (geteuid()) + fatal("osnoise needs root permission"); + + return ¶ms->common; +} + +/* + * osnoise_hist_parse_args - allocs, parse and fill the cmd line parameters + */ +struct common_params *osnoise_hist_parse_args(int argc, char **argv) +{ + struct osnoise_params *params; + struct osnoise_cb_data cb_data; + + params =3D calloc_fatal(1, sizeof(*params)); + + cb_data.params =3D params; + cb_data.trace_output =3D NULL; + + const struct option osnoise_hist_options[] =3D { + OPT_GROUP("Tracing Options:"), + OSNOISE_OPT_PERIOD, + OSNOISE_OPT_RUNTIME, + RTLA_OPT_STOP('s', "stop", "single sample"), + RTLA_OPT_STOP_TOTAL('S', "stop-total", "total sample"), + OSNOISE_OPT_THRESHOLD, + RTLA_OPT_TRACE_OUTPUT("osnoise", opt_osnoise_trace_output_cb), + + OPT_GROUP("Event Configuration:"), + RTLA_OPT_EVENT, + RTLA_OPT_FILTER, + RTLA_OPT_TRIGGER, + + OPT_GROUP("CPU Configuration:"), + RTLA_OPT_CPUS, + RTLA_OPT_HOUSEKEEPING, + + OPT_GROUP("Thread Configuration:"), + RTLA_OPT_PRIORITY, + RTLA_OPT_CGROUP, + + OPT_GROUP("Histogram Options:"), + HIST_OPT_BUCKET_SIZE, + HIST_OPT_ENTRIES, + HIST_OPT_NO_HEADER, + HIST_OPT_NO_SUMMARY, + HIST_OPT_NO_INDEX, + HIST_OPT_WITH_ZEROS, + + OPT_GROUP("System Tuning:"), + RTLA_OPT_TRACE_BUFFER_SIZE, + RTLA_OPT_WARM_UP, + + OPT_GROUP("Auto Analysis and Actions:"), + RTLA_OPT_AUTO(opt_osnoise_auto_cb), + RTLA_OPT_ON_THRESHOLD("stop-total", opt_osnoise_on_threshold_cb), + RTLA_OPT_ON_END(opt_osnoise_on_end_cb), + + OPT_GROUP("General:"), + RTLA_OPT_DURATION, + RTLA_OPT_DEBUG, + + OPT_END(), + }; + + actions_init(¶ms->common.threshold_actions); + actions_init(¶ms->common.end_actions); + + /* display data in microseconds */ + params->common.output_divisor =3D 1000; + params->common.hist.bucket_size =3D 1; + params->common.hist.entries =3D 256; + + argc =3D parse_options(argc, (const char **)argv, + osnoise_hist_options, osnoise_hist_usage, + common_parse_options_flags); + if (argc < 0) + return NULL; + + if (cb_data.trace_output) + actions_add_trace_output(¶ms->common.threshold_actions, cb_data.trac= e_output); + + if (geteuid()) + fatal("rtla needs root permission"); + + if (params->common.hist.no_index && !params->common.hist.with_zeros) + fatal("no-index set and with-zeros not set - it does not make sense"); + + return ¶ms->common; +} + +struct common_params *timerlat_top_parse_args(int argc, char **argv) +{ + struct timerlat_params *params; + struct timerlat_cb_data cb_data; + + params =3D calloc_fatal(1, sizeof(*params)); + + cb_data.params =3D params; + cb_data.trace_output =3D NULL; + + const struct option timerlat_top_options[] =3D { + OPT_GROUP("Tracing Options:"), + TIMERLAT_OPT_PERIOD, + RTLA_OPT_STOP('i', "irq", "irq latency"), + RTLA_OPT_STOP_TOTAL('T', "thread", "thread latency"), + TIMERLAT_OPT_STACK, + RTLA_OPT_TRACE_OUTPUT("timerlat", opt_timerlat_trace_output_cb), + + OPT_GROUP("Event Configuration:"), + RTLA_OPT_EVENT, + RTLA_OPT_FILTER, + RTLA_OPT_TRIGGER, + + OPT_GROUP("CPU Configuration:"), + RTLA_OPT_CPUS, + RTLA_OPT_HOUSEKEEPING, + + OPT_GROUP("Thread Configuration:"), + RTLA_OPT_PRIORITY, + RTLA_OPT_CGROUP, + RTLA_OPT_USER_THREADS, + RTLA_OPT_KERNEL_THREADS, + RTLA_OPT_USER_LOAD, + + OPT_GROUP("Output:"), + TIMERLAT_OPT_NANO, + RTLA_OPT_QUIET, + + OPT_GROUP("System Tuning:"), + TIMERLAT_OPT_DMA_LATENCY, + TIMERLAT_OPT_DEEPEST_IDLE_STATE, + RTLA_OPT_TRACE_BUFFER_SIZE, + RTLA_OPT_WARM_UP, + + OPT_GROUP("Auto Analysis and Actions:"), + RTLA_OPT_AUTO(opt_timerlat_auto_cb), + TIMERLAT_OPT_AA_ONLY, + TIMERLAT_OPT_NO_AA, + TIMERLAT_OPT_DUMPS_TASKS, + RTLA_OPT_ON_THRESHOLD("latency", opt_timerlat_on_threshold_cb), + RTLA_OPT_ON_END(opt_timerlat_on_end_cb), + TIMERLAT_OPT_BPF_ACTION, + TIMERLAT_OPT_STACK_FORMAT, + + OPT_GROUP("General:"), + RTLA_OPT_DURATION, + RTLA_OPT_DEBUG, + + OPT_END(), + }; + + actions_init(¶ms->common.threshold_actions); + actions_init(¶ms->common.end_actions); + + /* disabled by default */ + params->dma_latency =3D -1; + params->deepest_idle_state =3D -2; + + /* display data in microseconds */ + params->common.output_divisor =3D 1000; + + /* default to BPF mode */ + params->mode =3D TRACING_MODE_BPF; + + /* default to truncate stack format */ + params->stack_format =3D STACK_FORMAT_TRUNCATE; + + argc =3D parse_options(argc, (const char **)argv, + timerlat_top_options, timerlat_top_usage, + common_parse_options_flags); + if (argc < 0) + return NULL; + + if (cb_data.trace_output) + actions_add_trace_output(¶ms->common.threshold_actions, cb_data.trac= e_output); + + if (geteuid()) + fatal("rtla needs root permission"); + + /* + * Auto analysis only happens if stop tracing, thus: + */ + if (!params->common.stop_us && !params->common.stop_total_us) + params->no_aa =3D 1; + + if (params->no_aa && params->common.aa_only) + fatal("--no-aa and --aa-only are mutually exclusive!"); + + if (params->common.kernel_workload && params->common.user_workload) + fatal("--kernel-threads and --user-threads are mutually exclusive!"); + + /* + * If auto-analysis or trace output is enabled, switch from BPF mode to + * mixed mode + */ + if (params->mode =3D=3D TRACING_MODE_BPF && + (params->common.threshold_actions.present[ACTION_TRACE_OUTPUT] || + params->common.end_actions.present[ACTION_TRACE_OUTPUT] || + !params->no_aa)) + params->mode =3D TRACING_MODE_MIXED; + + return ¶ms->common; +} + +struct common_params *timerlat_hist_parse_args(int argc, char **argv) +{ + struct timerlat_params *params; + struct timerlat_cb_data cb_data; + + params =3D calloc_fatal(1, sizeof(*params)); + + cb_data.params =3D params; + cb_data.trace_output =3D NULL; + + const struct option timerlat_hist_options[] =3D { + OPT_GROUP("Tracing Options:"), + TIMERLAT_OPT_PERIOD, + RTLA_OPT_STOP('i', "irq", "irq latency"), + RTLA_OPT_STOP_TOTAL('T', "thread", "thread latency"), + TIMERLAT_OPT_STACK, + RTLA_OPT_TRACE_OUTPUT("timerlat", opt_timerlat_trace_output_cb), + + OPT_GROUP("Event Configuration:"), + RTLA_OPT_EVENT, + RTLA_OPT_FILTER, + RTLA_OPT_TRIGGER, + + OPT_GROUP("CPU Configuration:"), + RTLA_OPT_CPUS, + RTLA_OPT_HOUSEKEEPING, + + OPT_GROUP("Thread Configuration:"), + RTLA_OPT_PRIORITY, + RTLA_OPT_CGROUP, + RTLA_OPT_USER_THREADS, + RTLA_OPT_KERNEL_THREADS, + RTLA_OPT_USER_LOAD, + + OPT_GROUP("Histogram Options:"), + HIST_OPT_BUCKET_SIZE, + HIST_OPT_ENTRIES, + HIST_OPT_NO_IRQ, + HIST_OPT_NO_THREAD, + HIST_OPT_NO_HEADER, + HIST_OPT_NO_SUMMARY, + HIST_OPT_NO_INDEX, + HIST_OPT_WITH_ZEROS, + + OPT_GROUP("Output:"), + TIMERLAT_OPT_NANO, + + OPT_GROUP("System Tuning:"), + TIMERLAT_OPT_DMA_LATENCY, + TIMERLAT_OPT_DEEPEST_IDLE_STATE, + RTLA_OPT_TRACE_BUFFER_SIZE, + RTLA_OPT_WARM_UP, + + OPT_GROUP("Auto Analysis and Actions:"), + RTLA_OPT_AUTO(opt_timerlat_auto_cb), + TIMERLAT_OPT_NO_AA, + TIMERLAT_OPT_DUMPS_TASKS, + RTLA_OPT_ON_THRESHOLD("latency", opt_timerlat_on_threshold_cb), + RTLA_OPT_ON_END(opt_timerlat_on_end_cb), + TIMERLAT_OPT_BPF_ACTION, + TIMERLAT_OPT_STACK_FORMAT, + + OPT_GROUP("General:"), + RTLA_OPT_DURATION, + RTLA_OPT_DEBUG, + + OPT_END(), + }; + + actions_init(¶ms->common.threshold_actions); + actions_init(¶ms->common.end_actions); + + /* disabled by default */ + params->dma_latency =3D -1; + + /* disabled by default */ + params->deepest_idle_state =3D -2; + + /* display data in microseconds */ + params->common.output_divisor =3D 1000; + params->common.hist.bucket_size =3D 1; + params->common.hist.entries =3D 256; + + /* default to BPF mode */ + params->mode =3D TRACING_MODE_BPF; + + /* default to truncate stack format */ + params->stack_format =3D STACK_FORMAT_TRUNCATE; + + argc =3D parse_options(argc, (const char **)argv, + timerlat_hist_options, timerlat_hist_usage, + common_parse_options_flags); + if (argc < 0) + return NULL; + + if (cb_data.trace_output) + actions_add_trace_output(¶ms->common.threshold_actions, cb_data.trac= e_output); + + if (geteuid()) + fatal("rtla needs root permission"); + + if (params->common.hist.no_irq && params->common.hist.no_thread) + fatal("no-irq and no-thread set, there is nothing to do here"); + + if (params->common.hist.no_index && !params->common.hist.with_zeros) + fatal("no-index set with with-zeros is not set - it does not make sense"= ); + + /* + * Auto analysis only happens if stop tracing, thus: + */ + if (!params->common.stop_us && !params->common.stop_total_us) + params->no_aa =3D 1; + + if (params->common.kernel_workload && params->common.user_workload) + fatal("--kernel-threads and --user-threads are mutually exclusive!"); + + /* + * If auto-analysis or trace output is enabled, switch from BPF mode to + * mixed mode + */ + if (params->mode =3D=3D TRACING_MODE_BPF && + (params->common.threshold_actions.present[ACTION_TRACE_OUTPUT] || + params->common.end_actions.present[ACTION_TRACE_OUTPUT] || + !params->no_aa)) + params->mode =3D TRACING_MODE_MIXED; + + return ¶ms->common; +} + +/* + * rtla_usage - print rtla usage + */ +__noreturn static void rtla_usage(int err) +{ + int i; + + static const char * const msg[] =3D { + "", + "rtla version " VERSION, + "", + " usage: rtla COMMAND ...", + "", + " commands:", + " osnoise - gives information about the operating system noise (osn= oise)", + " hwnoise - gives information about hardware-related noise", + " timerlat - measures the timer irq and thread latency", + "", + NULL, + }; + + for (i =3D 0; msg[i]; i++) + fprintf(stderr, "%s\n", msg[i]); + exit(err); +} + +/* + * run_tool_command - try to run a rtla tool command + * + * It returns 0 if it fails. The tool's main will generally not + * return as they should call exit(). + */ +int run_tool_command(int argc, char **argv, int start_position) +{ + if (strcmp(argv[start_position], "osnoise") =3D=3D 0) { + osnoise_main(argc-start_position, &argv[start_position]); + goto ran; + } else if (strcmp(argv[start_position], "hwnoise") =3D=3D 0) { + hwnoise_main(argc-start_position, &argv[start_position]); + goto ran; + } else if (strcmp(argv[start_position], "timerlat") =3D=3D 0) { + timerlat_main(argc-start_position, &argv[start_position]); + goto ran; + } + + return 0; +ran: + return 1; +} + +/* Set main as weak to allow overriding it for building unit test binary */ +#pragma weak main + +int main(int argc, char *argv[]) +{ + int retval; + + /* is it an alias? */ + retval =3D run_tool_command(argc, argv, 0); + if (retval) + exit(0); + + if (argc < 2) + goto usage; + + if (strcmp(argv[1], "-h") =3D=3D 0) + rtla_usage(129); + else if (strcmp(argv[1], "--help") =3D=3D 0) + rtla_usage(129); + + retval =3D run_tool_command(argc, argv, 1); + if (retval) + exit(0); + +usage: + rtla_usage(129); +} diff --git a/tools/tracing/rtla/src/cli.h b/tools/tracing/rtla/src/cli.h new file mode 100644 index 000000000000..c49ccb3e92f5 --- /dev/null +++ b/tools/tracing/rtla/src/cli.h @@ -0,0 +1,7 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#pragma once + +struct common_params *osnoise_top_parse_args(int argc, char **argv); +struct common_params *osnoise_hist_parse_args(int argc, char **argv); +struct common_params *timerlat_top_parse_args(int argc, char **argv); +struct common_params *timerlat_hist_parse_args(int argc, char **argv); diff --git a/tools/tracing/rtla/src/cli_p.h b/tools/tracing/rtla/src/cli_p.h new file mode 100644 index 000000000000..3cea4f6e976e --- /dev/null +++ b/tools/tracing/rtla/src/cli_p.h @@ -0,0 +1,670 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#pragma once + +#ifndef RTLA_ALLOW_CLI_P_H +#error "Private header file included outside of cli.c module" +#endif + +#include +#include + +#include "cli.h" +#include "osnoise.h" +#include "timerlat.h" + +struct osnoise_cb_data { + struct osnoise_params *params; + char *trace_output; +}; + +struct timerlat_cb_data { + struct timerlat_params *params; + char *trace_output; +}; + +/* + * Macros for command line options common to all tools + * + * Note: Some of the options are common to both timerlat and osnoise, but + * have a slightly different meaning. Such options take additional argumen= ts + * that have to be provided by the *_parse_args() function of the correspo= nding + * tool. + * + * All macros defined here assume the presence of a params variable of + * the corresponding tool type (i.e struct timerlat_params or struct osnoi= se_params) + * and a cb_data variable of the matching type. + */ + + #define RTLA_OPT_STOP(short, long, name) OPT_CALLBACK_FLAG(short, long, \ + ¶ms->common.stop_us, \ + "us", \ + "stop trace if " name " is higher than the argument in us", \ + opt_llong_callback, PARSE_OPT_NOAUTONEG) + +#define RTLA_OPT_STOP_TOTAL(short, long, name) OPT_CALLBACK_FLAG(short, lo= ng, \ + ¶ms->common.stop_total_us, \ + "us", \ + "stop trace if " name " is higher than the argument in us", \ + opt_llong_callback, PARSE_OPT_NOAUTONEG) + +#define RTLA_OPT_TRACE_OUTPUT(tracer, cb) OPT_CALLBACK_OPTARG('t', "trace"= , \ + (const char **)&cb_data.trace_output, \ + tracer "_trace.txt", \ + "[file]", \ + "save the stopped trace to [file|" tracer "_trace.txt]", \ + cb) + +#define RTLA_OPT_CPUS OPT_CALLBACK('c', "cpus", ¶ms->common, \ + "cpu-list", \ + "run the tracer only on the given cpus", \ + opt_cpus_cb) + +#define RTLA_OPT_CGROUP OPT_CALLBACK_OPTARG('C', "cgroup", ¶ms->common= , \ + "[cgroup_name]", NULL, \ + "set cgroup, no argument means rtla's cgroup will be inherited", \ + opt_cgroup_cb) + +#define RTLA_OPT_USER_THREADS OPT_CALLBACK_NOOPT('u', "user-threads", para= ms, NULL, \ + "use rtla user-space threads instead of kernel-space timerlat threads", \ + opt_user_threads_cb) + +#define RTLA_OPT_KERNEL_THREADS OPT_BOOLEAN('k', "kernel-threads", \ + ¶ms->common.kernel_workload, \ + "use timerlat kernel-space threads instead of rtla user-space threads") + +#define RTLA_OPT_USER_LOAD OPT_BOOLEAN('U', "user-load", ¶ms->common.u= ser_data, \ + "enable timerlat for user-defined user-space workload") + +#define RTLA_OPT_DURATION OPT_CALLBACK('d', "duration", ¶ms->common, \ + "time[s|m|h|d]", \ + "set the duration of the session", \ + opt_duration_cb) + +#define RTLA_OPT_EVENT OPT_CALLBACK('e', "event", ¶ms->common.events, \ + "sys:event", \ + "enable the in the trace instance, multiple -e are allowed", \ + opt_event_cb) + +#define RTLA_OPT_HOUSEKEEPING OPT_CALLBACK('H', "house-keeping", ¶ms->= common, \ + "cpu-list", \ + "run rtla control threads only on the given cpus", \ + opt_housekeeping_cb) + +#define RTLA_OPT_PRIORITY OPT_CALLBACK('P', "priority", ¶ms->common, \ + "o:prio|r:prio|f:prio|d:runtime:period", \ + "set scheduling parameters", \ + opt_priority_cb) + +#define RTLA_OPT_TRIGGER OPT_CALLBACK(0, "trigger", ¶ms->common.events= , \ + "trigger", \ + "enable a trace event trigger to the previous -e event", \ + opt_trigger_cb) + +#define RTLA_OPT_FILTER OPT_CALLBACK(0, "filter", ¶ms->common.events, \ + "filter", \ + "enable a trace event filter to the previous -e event", \ + opt_filter_cb) + +#define RTLA_OPT_QUIET OPT_BOOLEAN('q', "quiet", ¶ms->common.quiet, \ + "print only a summary at the end") + +#define RTLA_OPT_TRACE_BUFFER_SIZE OPT_CALLBACK(0, "trace-buffer-size", \ + ¶ms->common.buffer_size, "kB", \ + "set the per-cpu trace buffer size in kB", \ + opt_int_callback) + +#define RTLA_OPT_WARM_UP OPT_CALLBACK(0, "warm-up", ¶ms->common.warmup= , "s", \ + "let the workload run for s seconds before collecting data", \ + opt_int_callback) + +#define RTLA_OPT_AUTO(cb) OPT_CALLBACK('a', "auto", &cb_data, "us", \ + "set automatic trace mode, stopping the session if argument in us sample = is hit", \ + cb) + +#define RTLA_OPT_ON_THRESHOLD(threshold, cb) OPT_CALLBACK(0, "on-threshold= ", \ + ¶ms->common.threshold_actions, \ + "action", \ + "define action to be executed at " threshold " threshold, multiple are al= lowed", \ + cb) + +#define RTLA_OPT_ON_END(cb) OPT_CALLBACK(0, "on-end", ¶ms->common.end_= actions, \ + "action", \ + "define action to be executed at measurement end, multiple are allowed", \ + cb) + +#define RTLA_OPT_DEBUG OPT_BOOLEAN('D', "debug", &config_debug, \ + "print debug info") + +/* + * Common callback functions for command line options + */ + +static int opt_llong_callback(const struct option *opt, const char *arg, i= nt unset) +{ + long long *value =3D opt->value; + + if (unset || !arg) + return -1; + + *value =3D get_llong_from_str((char *)arg); + return 0; +} + +static int opt_int_callback(const struct option *opt, const char *arg, int= unset) +{ + int *value =3D opt->value; + + if (unset || !arg) + return -1; + + if (strtoi(arg, value)) + return -1; + + return 0; +} + +static int opt_cpus_cb(const struct option *opt, const char *arg, int unse= t) +{ + struct common_params *params =3D opt->value; + int retval; + + if (unset || !arg) + return -1; + + retval =3D parse_cpu_set((char *)arg, ¶ms->monitored_cpus); + if (retval) + fatal("Invalid -c cpu list"); + params->cpus =3D (char *)arg; + + return 0; +} + +static int opt_cgroup_cb(const struct option *opt, const char *arg, int un= set) +{ + struct common_params *params =3D opt->value; + + if (unset) + return -1; + + params->cgroup =3D 1; + params->cgroup_name =3D (char *)arg; + if (params->cgroup_name && params->cgroup_name[0] =3D=3D '=3D') + /* Allow -C=3D next to -C[ ] */ + ++params->cgroup_name; + + return 0; +} + +static int opt_duration_cb(const struct option *opt, const char *arg, int = unset) +{ + struct common_params *params =3D opt->value; + + if (unset || !arg) + return -1; + + params->duration =3D parse_seconds_duration((char *)arg); + if (!params->duration) + fatal("Invalid -d duration"); + + return 0; +} + +static int opt_event_cb(const struct option *opt, const char *arg, int uns= et) +{ + struct trace_events **events =3D opt->value; + struct trace_events *tevent; + + if (unset || !arg) + return -1; + + tevent =3D trace_event_alloc((char *)arg); + if (!tevent) + fatal("Error alloc trace event"); + + if (*events) + tevent->next =3D *events; + *events =3D tevent; + + return 0; +} + +static int opt_housekeeping_cb(const struct option *opt, const char *arg, = int unset) +{ + struct common_params *params =3D opt->value; + int retval; + + if (unset || !arg) + return -1; + + params->hk_cpus =3D 1; + retval =3D parse_cpu_set((char *)arg, ¶ms->hk_cpu_set); + if (retval) + fatal("Error parsing house keeping CPUs"); + + return 0; +} + +static int opt_priority_cb(const struct option *opt, const char *arg, int = unset) +{ + struct common_params *params =3D opt->value; + int retval; + + if (unset || !arg) + return -1; + + retval =3D parse_prio((char *)arg, ¶ms->sched_param); + if (retval =3D=3D -1) + fatal("Invalid -P priority"); + params->set_sched =3D 1; + + return 0; +} + +static int opt_trigger_cb(const struct option *opt, const char *arg, int u= nset) +{ + struct trace_events **events =3D opt->value; + + if (unset || !arg) + return -1; + + if (!*events) + fatal("--trigger requires a previous -e"); + + trace_event_add_trigger(*events, (char *)arg); + + return 0; +} + +static int opt_filter_cb(const struct option *opt, const char *arg, int un= set) +{ + struct trace_events **events =3D opt->value; + + if (unset || !arg) + return -1; + + if (!*events) + fatal("--filter requires a previous -e"); + + trace_event_add_filter(*events, (char *)arg); + + return 0; +} + +/* + * Macros for command line options specific to osnoise + */ +#define OSNOISE_OPT_PERIOD OPT_CALLBACK('p', "period", ¶ms->period, "u= s", \ + "osnoise period in us", \ + opt_osnoise_period_cb) + +#define OSNOISE_OPT_RUNTIME OPT_CALLBACK('r', "runtime", ¶ms->runtime,= "us", \ + "osnoise runtime in us", \ + opt_osnoise_runtime_cb) + +#define OSNOISE_OPT_THRESHOLD OPT_CALLBACK('T', "threshold", ¶ms->thre= shold, "us", \ + "the minimum delta to be considered a noise", \ + opt_llong_callback) + +/* + * Callback functions for command line options for osnoise tools + */ + +static int opt_osnoise_auto_cb(const struct option *opt, const char *arg, = int unset) +{ + struct osnoise_cb_data *cb_data =3D opt->value; + struct osnoise_params *params =3D cb_data->params; + long long auto_thresh; + + if (unset || !arg) + return -1; + + auto_thresh =3D get_llong_from_str((char *)arg); + params->common.stop_us =3D auto_thresh; + params->threshold =3D 1; + + if (!cb_data->trace_output) + cb_data->trace_output =3D "osnoise_trace.txt"; + + return 0; +} + +static int opt_osnoise_period_cb(const struct option *opt, const char *arg= , int unset) +{ + unsigned long long *period =3D opt->value; + + if (unset || !arg) + return -1; + + *period =3D get_llong_from_str((char *)arg); + if (*period > 10000000) + fatal("Period longer than 10 s"); + + return 0; +} + +static int opt_osnoise_runtime_cb(const struct option *opt, const char *ar= g, int unset) +{ + unsigned long long *runtime =3D opt->value; + + if (unset || !arg) + return -1; + + *runtime =3D get_llong_from_str((char *)arg); + if (*runtime < 100) + fatal("Runtime shorter than 100 us"); + + return 0; +} + +static int opt_osnoise_trace_output_cb(const struct option *opt, const cha= r *arg, int unset) +{ + const char **trace_output =3D opt->value; + + if (unset) + return -1; + + if (!arg) { + *trace_output =3D "osnoise_trace.txt"; + } else { + *trace_output =3D (char *)arg; + if (*trace_output && (*trace_output)[0] =3D=3D '=3D') + /* Allow -t=3D next to -t[ ] */ + ++*trace_output; + } + + return 0; +} + +static int opt_osnoise_on_threshold_cb(const struct option *opt, const cha= r *arg, int unset) +{ + struct actions *actions =3D opt->value; + int retval; + + if (unset || !arg) + return -1; + + retval =3D actions_parse(actions, (char *)arg, "osnoise_trace.txt"); + if (retval) + fatal("Invalid action %s", arg); + + return 0; +} + +static int opt_osnoise_on_end_cb(const struct option *opt, const char *arg= , int unset) +{ + struct actions *actions =3D opt->value; + int retval; + + if (unset || !arg) + return -1; + + retval =3D actions_parse(actions, (char *)arg, "osnoise_trace.txt"); + if (retval) + fatal("Invalid action %s", arg); + + return 0; +} + +/* + * Macros for command line options specific to timerlat + */ +#define TIMERLAT_OPT_PERIOD OPT_CALLBACK('p', "period", ¶ms->timerlat_= period_us, "us", \ + "timerlat period in us", \ + opt_timerlat_period_cb) + +#define TIMERLAT_OPT_STACK OPT_CALLBACK('s', "stack", ¶ms->print_stack= , "us", \ + "save the stack trace at the IRQ if a thread latency is higher than the a= rgument in us", \ + opt_llong_callback) + +#define TIMERLAT_OPT_NANO OPT_CALLBACK_NOOPT('n', "nano", params, NULL, \ + "display data in nanoseconds", \ + opt_nano_cb) + +#define TIMERLAT_OPT_DMA_LATENCY OPT_CALLBACK(0, "dma-latency", ¶ms->d= ma_latency, "us", \ + "set /dev/cpu_dma_latency latency to reduce exit from idle latency",= \ + opt_dma_latency_cb) + +#define TIMERLAT_OPT_DEEPEST_IDLE_STATE OPT_CALLBACK(0, "deepest-idle-stat= e", \ + ¶ms->deepest_idle_state, "n", \ + "only go down to idle state n on cpus used by timerlat to reduce exit fro= m idle latency", \ + opt_int_callback) + +#define TIMERLAT_OPT_AA_ONLY OPT_CALLBACK(0, "aa-only", params, "us", \ + "stop if latency is hit, only printing the auto analysis (reduces CP= U usage)", \ + opt_aa_only_cb) + +#define TIMERLAT_OPT_NO_AA OPT_BOOLEAN(0, "no-aa", ¶ms->no_aa, \ + "disable auto-analysis, reducing rtla timerlat cpu usage") + +#define TIMERLAT_OPT_DUMPS_TASKS OPT_BOOLEAN(0, "dump-tasks", ¶ms->dum= p_tasks, \ + "prints the task running on all CPUs if stop conditions are met (depends = on !--no-aa)") + +#define TIMERLAT_OPT_BPF_ACTION OPT_STRING(0, "bpf-action", ¶ms->bpf_a= ction_program, \ + "program", \ + "load and execute BPF program when latency threshold is exceeded") + +#define TIMERLAT_OPT_STACK_FORMAT OPT_CALLBACK(0, "stack-format", ¶ms-= >stack_format, "format", \ + "set the stack format (truncate, skip, full)", \ + opt_stack_format_cb) + +/* + * Callback functions for command line options for timerlat tools + */ + +static int opt_timerlat_period_cb(const struct option *opt, const char *ar= g, int unset) +{ + long long *period =3D opt->value; + + if (unset || !arg) + return -1; + + *period =3D get_llong_from_str((char *)arg); + if (*period > 1000000) + fatal("Period longer than 1 s"); + + return 0; +} + +static int opt_timerlat_auto_cb(const struct option *opt, const char *arg,= int unset) +{ + struct timerlat_cb_data *cb_data =3D opt->value; + struct timerlat_params *params =3D cb_data->params; + long long auto_thresh; + + if (unset || !arg) + return -1; + + auto_thresh =3D get_llong_from_str((char *)arg); + params->common.stop_total_us =3D auto_thresh; + params->common.stop_us =3D auto_thresh; + params->print_stack =3D auto_thresh; + + if (!cb_data->trace_output) + cb_data->trace_output =3D "timerlat_trace.txt"; + + return 0; +} + +static int opt_dma_latency_cb(const struct option *opt, const char *arg, i= nt unset) +{ + int *dma_latency =3D opt->value; + int retval; + + if (unset || !arg) + return -1; + + retval =3D strtoi((char *)arg, dma_latency); + if (retval) + fatal("Invalid -dma-latency %s", arg); + if (*dma_latency < 0 || *dma_latency > 10000) + fatal("--dma-latency needs to be >=3D 0 and <=3D 10000"); + + return 0; +} + +static int opt_aa_only_cb(const struct option *opt, const char *arg, int u= nset) +{ + struct timerlat_params *params =3D opt->value; + long long auto_thresh; + + if (unset || !arg) + return -1; + + auto_thresh =3D get_llong_from_str((char *)arg); + params->common.stop_total_us =3D auto_thresh; + params->common.stop_us =3D auto_thresh; + params->print_stack =3D auto_thresh; + params->common.aa_only =3D 1; + + return 0; +} + +static int opt_timerlat_trace_output_cb(const struct option *opt, const ch= ar *arg, int unset) +{ + const char **trace_output =3D opt->value; + + if (unset) + return -1; + + if (!arg) { + *trace_output =3D "timerlat_trace.txt"; + } else { + *trace_output =3D (char *)arg; + if (*trace_output && (*trace_output)[0] =3D=3D '=3D') + /* Allow -t=3D next to -t[ ] */ + ++*trace_output; + } + + return 0; +} + +static int opt_timerlat_on_threshold_cb(const struct option *opt, const ch= ar *arg, int unset) +{ + struct actions *actions =3D opt->value; + int retval; + + if (unset || !arg) + return -1; + + retval =3D actions_parse(actions, (char *)arg, "timerlat_trace.txt"); + if (retval) + fatal("Invalid action %s", arg); + + return 0; +} + +static int opt_timerlat_on_end_cb(const struct option *opt, const char *ar= g, int unset) +{ + struct actions *actions =3D opt->value; + int retval; + + if (unset || !arg) + return -1; + + retval =3D actions_parse(actions, (char *)arg, "timerlat_trace.txt"); + if (retval) + fatal("Invalid action %s", arg); + + return 0; +} + +static int opt_user_threads_cb(const struct option *opt, const char *arg, = int unset) +{ + struct timerlat_params *params =3D opt->value; + + if (unset) + return -1; + + params->common.user_workload =3D true; + params->common.user_data =3D true; + + return 0; +} + +static int opt_nano_cb(const struct option *opt, const char *arg, int unse= t) +{ + struct timerlat_params *params =3D opt->value; + + if (unset) + return -1; + + params->common.output_divisor =3D 1; + + return 0; +} + +static int opt_stack_format_cb(const struct option *opt, const char *arg, = int unset) +{ + int *format =3D opt->value; + + if (unset || !arg) + return -1; + + *format =3D parse_stack_format((char *)arg); + + if (*format =3D=3D -1) + fatal("Invalid --stack-format option"); + + return 0; +} + +/* + * Macros for command line options specific to histogram-based tools + */ + +#define HIST_OPT_BUCKET_SIZE OPT_CALLBACK('b', "bucket-size", \ + ¶ms->common.hist.bucket_size, "N", \ + "set the histogram bucket size (default 1)", \ + opt_bucket_size_cb) + +#define HIST_OPT_ENTRIES OPT_CALLBACK('E', "entries", ¶ms->common.hist= .entries, "N", \ + "set the number of entries of the histogram (default 256)", \ + opt_entries_cb) + +#define HIST_OPT_NO_IRQ OPT_BOOLEAN_FLAG(0, "no-irq", ¶ms->common.hist= .no_irq, \ + "ignore IRQ latencies", PARSE_OPT_NOAUTONEG) + +#define HIST_OPT_NO_THREAD OPT_BOOLEAN_FLAG(0, "no-thread", ¶ms->commo= n.hist.no_thread, \ + "ignore thread latencies", PARSE_OPT_NOAUTONEG) + +#define HIST_OPT_NO_HEADER OPT_BOOLEAN(0, "no-header", ¶ms->common.his= t.no_header, \ + "do not print header") + +#define HIST_OPT_NO_SUMMARY OPT_BOOLEAN(0, "no-summary", ¶ms->common.h= ist.no_summary, \ + "do not print summary") + +#define HIST_OPT_NO_INDEX OPT_BOOLEAN(0, "no-index", ¶ms->common.hist.= no_index, \ + "do not print index") + +#define HIST_OPT_WITH_ZEROS OPT_BOOLEAN(0, "with-zeros", ¶ms->common.h= ist.with_zeros, \ + "print zero only entries") + +/* Histogram-specific callbacks */ + +static int opt_bucket_size_cb(const struct option *opt, const char *arg, i= nt unset) +{ + int *bucket_size =3D opt->value; + + if (unset || !arg) + return -1; + + *bucket_size =3D get_llong_from_str((char *)arg); + if (*bucket_size =3D=3D 0 || *bucket_size >=3D 1000000) + fatal("Bucket size needs to be > 0 and <=3D 1000000"); + + return 0; +} + +static int opt_entries_cb(const struct option *opt, const char *arg, int u= nset) +{ + int *entries =3D opt->value; + + if (unset || !arg) + return -1; + + *entries =3D get_llong_from_str((char *)arg); + if (*entries < 10 || *entries > 9999999) + fatal("Entries must be > 10 and < 10000000"); + + return 0; +} diff --git a/tools/tracing/rtla/src/common.c b/tools/tracing/rtla/src/commo= n.c index effad523e8cf..d0a8a6edbf0c 100644 --- a/tools/tracing/rtla/src/common.c +++ b/tools/tracing/rtla/src/common.c @@ -5,7 +5,6 @@ #include #include #include -#include #include =20 #include "common.h" @@ -57,114 +56,6 @@ static void unset_signals(struct common_params *params) } } =20 -/* - * getopt_auto - auto-generates optstring from long_options - */ -int getopt_auto(int argc, char **argv, const struct option *long_opts) -{ - char opts[256]; - int n =3D 0; - - for (int i =3D 0; long_opts[i].name; i++) { - if (long_opts[i].val < 32 || long_opts[i].val > 127) - continue; - - if (n + 4 >=3D sizeof(opts)) - fatal("optstring buffer overflow"); - - opts[n++] =3D long_opts[i].val; - - if (long_opts[i].has_arg =3D=3D required_argument) - opts[n++] =3D ':'; - else if (long_opts[i].has_arg =3D=3D optional_argument) { - opts[n++] =3D ':'; - opts[n++] =3D ':'; - } - } - - opts[n] =3D '\0'; - - return getopt_long(argc, argv, opts, long_opts, NULL); -} - -/* - * common_parse_options - parse common command line options - * - * @argc: argument count - * @argv: argument vector - * @common: common parameters structure - * - * Parse command line options that are common to all rtla tools. - * - * Returns: non zero if a common option was parsed, or 0 - * if the option should be handled by tool-specific parsing. - */ -int common_parse_options(int argc, char **argv, struct common_params *comm= on) -{ - struct trace_events *tevent; - int saved_state =3D optind; - int c; - - static struct option long_options[] =3D { - {"cpus", required_argument, 0, 'c'}, - {"cgroup", optional_argument, 0, 'C'}, - {"debug", no_argument, 0, 'D'}, - {"duration", required_argument, 0, 'd'}, - {"event", required_argument, 0, 'e'}, - {"house-keeping", required_argument, 0, 'H'}, - {"priority", required_argument, 0, 'P'}, - {0, 0, 0, 0} - }; - - opterr =3D 0; - c =3D getopt_auto(argc, argv, long_options); - opterr =3D 1; - - switch (c) { - case 'c': - if (parse_cpu_set(optarg, &common->monitored_cpus)) - fatal("Invalid -c cpu list"); - common->cpus =3D optarg; - break; - case 'C': - common->cgroup =3D 1; - common->cgroup_name =3D parse_optional_arg(argc, argv); - break; - case 'D': - config_debug =3D 1; - break; - case 'd': - common->duration =3D parse_seconds_duration(optarg); - if (!common->duration) - fatal("Invalid -d duration"); - break; - case 'e': - tevent =3D trace_event_alloc(optarg); - if (!tevent) - fatal("Error alloc trace event"); - - if (common->events) - tevent->next =3D common->events; - common->events =3D tevent; - break; - case 'H': - common->hk_cpus =3D 1; - if (parse_cpu_set(optarg, &common->hk_cpu_set)) - fatal("Error parsing house keeping CPUs"); - break; - case 'P': - if (parse_prio(optarg, &common->sched_param) =3D=3D -1) - fatal("Invalid -P priority"); - common->set_sched =3D 1; - break; - default: - optind =3D saved_state; - return 0; - } - - return c; -} - /* * common_apply_config - apply common configs to the initialized tool */ diff --git a/tools/tracing/rtla/src/common.h b/tools/tracing/rtla/src/commo= n.h index eba40b6d9504..0dfca83bd726 100644 --- a/tools/tracing/rtla/src/common.h +++ b/tools/tracing/rtla/src/common.h @@ -1,7 +1,6 @@ /* SPDX-License-Identifier: GPL-2.0 */ #pragma once =20 -#include #include "actions.h" #include "timerlat_u.h" #include "trace.h" @@ -57,12 +56,12 @@ struct osnoise_context { extern volatile int stop_tracing; =20 struct hist_params { - char no_irq; - char no_thread; - char no_header; - char no_summary; - char no_index; - char with_zeros; + bool no_irq; + bool no_thread; + bool no_header; + bool no_summary; + bool no_index; + bool with_zeros; int bucket_size; int entries; }; @@ -95,12 +94,12 @@ struct common_params { /* Other parameters */ struct hist_params hist; int output_divisor; - int pretty_output; - int quiet; - int user_workload; - int kernel_workload; - int user_data; - int aa_only; + bool pretty_output; + bool quiet; + bool user_workload; + bool kernel_workload; + bool user_data; + bool aa_only; =20 struct actions threshold_actions; struct actions end_actions; @@ -176,8 +175,6 @@ int osnoise_set_stop_us(struct osnoise_context *context= , long long stop_us); int osnoise_set_stop_total_us(struct osnoise_context *context, long long stop_total_us); =20 -int getopt_auto(int argc, char **argv, const struct option *long_opts); -int common_parse_options(int argc, char **argv, struct common_params *comm= on); int common_apply_config(struct osnoise_tool *tool, struct common_params *p= arams); int top_main_loop(struct osnoise_tool *tool); int hist_main_loop(struct osnoise_tool *tool); diff --git a/tools/tracing/rtla/src/osnoise.c b/tools/tracing/rtla/src/osno= ise.c index 2db3db155c44..e1e32898af2d 100644 --- a/tools/tracing/rtla/src/osnoise.c +++ b/tools/tracing/rtla/src/osnoise.c @@ -15,6 +15,8 @@ #include #include =20 +#include + #include "osnoise.h" =20 #define DEFAULT_SAMPLE_PERIOD 1000000 /* 1s */ @@ -1171,7 +1173,7 @@ int osnoise_enable(struct osnoise_tool *tool) return 0; } =20 -static void osnoise_usage(int err) +__noreturn static void osnoise_usage(int err) { int i; =20 @@ -1209,7 +1211,7 @@ int osnoise_main(int argc, char *argv[]) } =20 if ((strcmp(argv[1], "-h") =3D=3D 0) || (strcmp(argv[1], "--help") =3D=3D= 0)) { - osnoise_usage(0); + osnoise_usage(129); } else if (str_has_prefix(argv[1], "-")) { /* the user skipped the tool, call the default one */ run_tool(&osnoise_top_ops, argc, argv); @@ -1223,8 +1225,7 @@ int osnoise_main(int argc, char *argv[]) } =20 usage: - osnoise_usage(1); - exit(1); + osnoise_usage(129); } =20 int hwnoise_main(int argc, char *argv[]) diff --git a/tools/tracing/rtla/src/osnoise_hist.c b/tools/tracing/rtla/src= /osnoise_hist.c index 8ad816b80265..dfa91d0681f8 100644 --- a/tools/tracing/rtla/src/osnoise_hist.c +++ b/tools/tracing/rtla/src/osnoise_hist.c @@ -4,7 +4,6 @@ */ =20 #define _GNU_SOURCE -#include #include #include #include @@ -13,6 +12,7 @@ #include =20 #include "osnoise.h" +#include "cli.h" =20 struct osnoise_hist_cpu { int *samples; @@ -400,225 +400,6 @@ osnoise_print_stats(struct osnoise_tool *tool) osnoise_report_missed_events(tool); } =20 -/* - * osnoise_hist_usage - prints osnoise hist usage message - */ -static void osnoise_hist_usage(void) -{ - static const char * const msg_start[] =3D { - "[-D] [-d s] [-a us] [-p us] [-r us] [-s us] [-S us] \\", - " [-T us] [-t [file]] [-e sys[:event]] [--filter ] [--trigger = ] \\", - " [-c cpu-list] [-H cpu-list] [-P priority] [-b N] [-E N] [--no-header= ] [--no-summary] \\", - " [--no-index] [--with-zeros] [-C [cgroup_name]] [--warm-up]", - NULL, - }; - - static const char * const msg_opts[] =3D { - " -a/--auto: set automatic trace mode, stopping the session if argumen= t in us sample is hit", - " -p/--period us: osnoise period in us", - " -r/--runtime us: osnoise runtime in us", - " -s/--stop us: stop trace if a single sample is higher than the argum= ent in us", - " -S/--stop-total us: stop trace if the total sample is higher than th= e argument in us", - " -T/--threshold us: the minimum delta to be considered a noise", - " -c/--cpus cpu-list: list of cpus to run osnoise threads", - " -H/--house-keeping cpus: run rtla control threads only on the given = cpus", - " -C/--cgroup [cgroup_name]: set cgroup, if no cgroup_name is passed, = the rtla's cgroup will be inherited", - " -d/--duration time[s|m|h|d]: duration of the session", - " -D/--debug: print debug info", - " -t/--trace [file]: save the stopped trace to [file|osnoise_trace.txt= ]", - " -e/--event : enable the in the trace instance= , multiple -e are allowed", - " --filter : enable a trace event filter to the previous -e= event", - " --trigger : enable a trace event trigger to the previous= -e event", - " -b/--bucket-size N: set the histogram bucket size (default 1)", - " -E/--entries N: set the number of entries of the histogram (default = 256)", - " --no-header: do not print header", - " --no-summary: do not print summary", - " --no-index: do not print index", - " --with-zeros: print zero only entries", - " -P/--priority o:prio|r:prio|f:prio|d:runtime:period: set scheduling = parameters", - " o:prio - use SCHED_OTHER with prio", - " r:prio - use SCHED_RR with prio", - " f:prio - use SCHED_FIFO with prio", - " d:runtime[us|ms|s]:period[us|ms|s] - use SCHED_DEADLINE with runtime = and period", - " in nanoseconds", - " --warm-up: let the workload run for s seconds before collecting d= ata", - " --trace-buffer-size kB: set the per-cpu trace buffer size in kB", - " --on-threshold : define action to be executed at stop-tot= al threshold, multiple are allowed", - " --on-end : define action to be executed at measurement en= d, multiple are allowed", - NULL, - }; - - common_usage("osnoise", "hist", "a per-cpu histogram of the OS noise", - msg_start, msg_opts); -} - -/* - * osnoise_hist_parse_args - allocs, parse and fill the cmd line parameters - */ -static struct common_params -*osnoise_hist_parse_args(int argc, char *argv[]) -{ - struct osnoise_params *params; - int retval; - int c; - char *trace_output =3D NULL; - - params =3D calloc_fatal(1, sizeof(*params)); - - actions_init(¶ms->common.threshold_actions); - actions_init(¶ms->common.end_actions); - - /* display data in microseconds */ - params->common.output_divisor =3D 1000; - params->common.hist.bucket_size =3D 1; - params->common.hist.entries =3D 256; - - while (1) { - static struct option long_options[] =3D { - {"auto", required_argument, 0, 'a'}, - {"bucket-size", required_argument, 0, 'b'}, - {"entries", required_argument, 0, 'E'}, - {"help", no_argument, 0, 'h'}, - {"period", required_argument, 0, 'p'}, - {"runtime", required_argument, 0, 'r'}, - {"stop", required_argument, 0, 's'}, - {"stop-total", required_argument, 0, 'S'}, - {"trace", optional_argument, 0, 't'}, - {"threshold", required_argument, 0, 'T'}, - {"no-header", no_argument, 0, '0'}, - {"no-summary", no_argument, 0, '1'}, - {"no-index", no_argument, 0, '2'}, - {"with-zeros", no_argument, 0, '3'}, - {"trigger", required_argument, 0, '4'}, - {"filter", required_argument, 0, '5'}, - {"warm-up", required_argument, 0, '6'}, - {"trace-buffer-size", required_argument, 0, '7'}, - {"on-threshold", required_argument, 0, '8'}, - {"on-end", required_argument, 0, '9'}, - {0, 0, 0, 0} - }; - - if (common_parse_options(argc, argv, ¶ms->common)) - continue; - - c =3D getopt_auto(argc, argv, long_options); - - /* detect the end of the options. */ - if (c =3D=3D -1) - break; - - switch (c) { - case 'a': - /* set sample stop to auto_thresh */ - params->common.stop_us =3D get_llong_from_str(optarg); - - /* set sample threshold to 1 */ - params->threshold =3D 1; - - /* set trace */ - if (!trace_output) - trace_output =3D "osnoise_trace.txt"; - - break; - case 'b': - params->common.hist.bucket_size =3D get_llong_from_str(optarg); - if (params->common.hist.bucket_size =3D=3D 0 || - params->common.hist.bucket_size >=3D 1000000) - fatal("Bucket size needs to be > 0 and <=3D 1000000"); - break; - case 'E': - params->common.hist.entries =3D get_llong_from_str(optarg); - if (params->common.hist.entries < 10 || - params->common.hist.entries > 9999999) - fatal("Entries must be > 10 and < 9999999"); - break; - case 'h': - case '?': - osnoise_hist_usage(); - break; - case 'p': - params->period =3D get_llong_from_str(optarg); - if (params->period > 10000000) - fatal("Period longer than 10 s"); - break; - case 'r': - params->runtime =3D get_llong_from_str(optarg); - if (params->runtime < 100) - fatal("Runtime shorter than 100 us"); - break; - case 's': - params->common.stop_us =3D get_llong_from_str(optarg); - break; - case 'S': - params->common.stop_total_us =3D get_llong_from_str(optarg); - break; - case 'T': - params->threshold =3D get_llong_from_str(optarg); - break; - case 't': - trace_output =3D parse_optional_arg(argc, argv); - if (!trace_output) - trace_output =3D "osnoise_trace.txt"; - break; - case '0': /* no header */ - params->common.hist.no_header =3D 1; - break; - case '1': /* no summary */ - params->common.hist.no_summary =3D 1; - break; - case '2': /* no index */ - params->common.hist.no_index =3D 1; - break; - case '3': /* with zeros */ - params->common.hist.with_zeros =3D 1; - break; - case '4': /* trigger */ - if (params->common.events) - trace_event_add_trigger(params->common.events, optarg); - else - fatal("--trigger requires a previous -e"); - break; - case '5': /* filter */ - if (params->common.events) - trace_event_add_filter(params->common.events, optarg); - else - fatal("--filter requires a previous -e"); - break; - case '6': - params->common.warmup =3D get_llong_from_str(optarg); - break; - case '7': - params->common.buffer_size =3D get_llong_from_str(optarg); - break; - case '8': - retval =3D actions_parse(¶ms->common.threshold_actions, optarg, - "osnoise_trace.txt"); - if (retval) - fatal("Invalid action %s", optarg); - break; - case '9': - retval =3D actions_parse(¶ms->common.end_actions, optarg, - "osnoise_trace.txt"); - if (retval) - fatal("Invalid action %s", optarg); - break; - default: - fatal("Invalid option"); - } - } - - if (trace_output) - actions_add_trace_output(¶ms->common.threshold_actions, trace_output= ); - - if (geteuid()) - fatal("rtla needs root permission"); - - if (params->common.hist.no_index && !params->common.hist.with_zeros) - fatal("no-index set and with-zeros not set - it does not make sense"); - - return ¶ms->common; -} - /* * osnoise_hist_apply_config - apply the hist configs to the initialized t= ool */ diff --git a/tools/tracing/rtla/src/osnoise_top.c b/tools/tracing/rtla/src/= osnoise_top.c index 244bdce022ad..512a6299cb01 100644 --- a/tools/tracing/rtla/src/osnoise_top.c +++ b/tools/tracing/rtla/src/osnoise_top.c @@ -4,7 +4,6 @@ */ =20 #define _GNU_SOURCE -#include #include #include #include @@ -13,6 +12,7 @@ #include =20 #include "osnoise.h" +#include "cli.h" =20 struct osnoise_top_cpu { unsigned long long sum_runtime; @@ -245,204 +245,6 @@ osnoise_print_stats(struct osnoise_tool *top) osnoise_report_missed_events(top); } =20 -/* - * osnoise_top_usage - prints osnoise top usage message - */ -static void osnoise_top_usage(struct osnoise_params *params) -{ - const char *tool, *mode, *desc; - - static const char * const msg_start[] =3D { - "[-q] [-D] [-d s] [-a us] [-p us] [-r us] [-s us] [-S us] \\", - " [-T us] [-t [file]] [-e sys[:event]] [--filter ] [--trigger = ] \\", - " [-c cpu-list] [-H cpu-list] [-P priority] [-C [cgroup_name]] [--warm= -up s]", - NULL, - }; - - static const char * const msg_opts[] =3D { - " -a/--auto: set automatic trace mode, stopping the session if argumen= t in us sample is hit", - " -p/--period us: osnoise period in us", - " -r/--runtime us: osnoise runtime in us", - " -s/--stop us: stop trace if a single sample is higher than the argum= ent in us", - " -S/--stop-total us: stop trace if the total sample is higher than th= e argument in us", - " -T/--threshold us: the minimum delta to be considered a noise", - " -c/--cpus cpu-list: list of cpus to run osnoise threads", - " -H/--house-keeping cpus: run rtla control threads only on the given = cpus", - " -C/--cgroup [cgroup_name]: set cgroup, if no cgroup_name is passed, = the rtla's cgroup will be inherited", - " -d/--duration time[s|m|h|d]: duration of the session", - " -D/--debug: print debug info", - " -t/--trace [file]: save the stopped trace to [file|osnoise_trace.txt= ]", - " -e/--event : enable the in the trace instance= , multiple -e are allowed", - " --filter : enable a trace event filter to the previous -e= event", - " --trigger : enable a trace event trigger to the previous= -e event", - " -q/--quiet print only a summary at the end", - " -P/--priority o:prio|r:prio|f:prio|d:runtime:period : set scheduling= parameters", - " o:prio - use SCHED_OTHER with prio", - " r:prio - use SCHED_RR with prio", - " f:prio - use SCHED_FIFO with prio", - " d:runtime[us|ms|s]:period[us|ms|s] - use SCHED_DEADLINE with runtime = and period", - " in nanoseconds", - " --warm-up s: let the workload run for s seconds before collecting= data", - " --trace-buffer-size kB: set the per-cpu trace buffer size in kB", - " --on-threshold : define action to be executed at stop-tot= al threshold, multiple are allowed", - " --on-end: define action to be executed at measurement end, multip= le are allowed", - NULL, - }; - - if (params->mode =3D=3D MODE_OSNOISE) { - tool =3D "osnoise"; - mode =3D "top"; - desc =3D "a per-cpu summary of the OS noise"; - } else { - tool =3D "hwnoise"; - mode =3D ""; - desc =3D "a summary of hardware-related noise"; - } - - common_usage(tool, mode, desc, msg_start, msg_opts); -} - -/* - * osnoise_top_parse_args - allocs, parse and fill the cmd line parameters - */ -struct common_params *osnoise_top_parse_args(int argc, char **argv) -{ - struct osnoise_params *params; - int retval; - int c; - char *trace_output =3D NULL; - - params =3D calloc_fatal(1, sizeof(*params)); - - actions_init(¶ms->common.threshold_actions); - actions_init(¶ms->common.end_actions); - - if (strcmp(argv[0], "hwnoise") =3D=3D 0) { - params->mode =3D MODE_HWNOISE; - /* - * Reduce CPU usage for 75% to avoid killing the system. - */ - params->runtime =3D 750000; - params->period =3D 1000000; - } - - while (1) { - static struct option long_options[] =3D { - {"auto", required_argument, 0, 'a'}, - {"help", no_argument, 0, 'h'}, - {"period", required_argument, 0, 'p'}, - {"quiet", no_argument, 0, 'q'}, - {"runtime", required_argument, 0, 'r'}, - {"stop", required_argument, 0, 's'}, - {"stop-total", required_argument, 0, 'S'}, - {"threshold", required_argument, 0, 'T'}, - {"trace", optional_argument, 0, 't'}, - {"trigger", required_argument, 0, '0'}, - {"filter", required_argument, 0, '1'}, - {"warm-up", required_argument, 0, '2'}, - {"trace-buffer-size", required_argument, 0, '3'}, - {"on-threshold", required_argument, 0, '4'}, - {"on-end", required_argument, 0, '5'}, - {0, 0, 0, 0} - }; - - if (common_parse_options(argc, argv, ¶ms->common)) - continue; - - c =3D getopt_auto(argc, argv, long_options); - - /* Detect the end of the options. */ - if (c =3D=3D -1) - break; - - switch (c) { - case 'a': - /* set sample stop to auto_thresh */ - params->common.stop_us =3D get_llong_from_str(optarg); - - /* set sample threshold to 1 */ - params->threshold =3D 1; - - /* set trace */ - if (!trace_output) - trace_output =3D "osnoise_trace.txt"; - - break; - case 'h': - case '?': - osnoise_top_usage(params); - break; - case 'p': - params->period =3D get_llong_from_str(optarg); - if (params->period > 10000000) - fatal("Period longer than 10 s"); - break; - case 'q': - params->common.quiet =3D 1; - break; - case 'r': - params->runtime =3D get_llong_from_str(optarg); - if (params->runtime < 100) - fatal("Runtime shorter than 100 us"); - break; - case 's': - params->common.stop_us =3D get_llong_from_str(optarg); - break; - case 'S': - params->common.stop_total_us =3D get_llong_from_str(optarg); - break; - case 't': - trace_output =3D parse_optional_arg(argc, argv); - if (!trace_output) - trace_output =3D "osnoise_trace.txt"; - break; - case 'T': - params->threshold =3D get_llong_from_str(optarg); - break; - case '0': /* trigger */ - if (params->common.events) - trace_event_add_trigger(params->common.events, optarg); - else - fatal("--trigger requires a previous -e"); - break; - case '1': /* filter */ - if (params->common.events) - trace_event_add_filter(params->common.events, optarg); - else - fatal("--filter requires a previous -e"); - break; - case '2': - params->common.warmup =3D get_llong_from_str(optarg); - break; - case '3': - params->common.buffer_size =3D get_llong_from_str(optarg); - break; - case '4': - retval =3D actions_parse(¶ms->common.threshold_actions, optarg, - "osnoise_trace.txt"); - if (retval) - fatal("Invalid action %s", optarg); - break; - case '5': - retval =3D actions_parse(¶ms->common.end_actions, optarg, - "osnoise_trace.txt"); - if (retval) - fatal("Invalid action %s", optarg); - break; - default: - fatal("Invalid option"); - } - } - - if (trace_output) - actions_add_trace_output(¶ms->common.threshold_actions, trace_output= ); - - if (geteuid()) - fatal("osnoise needs root permission"); - - return ¶ms->common; -} - /* * osnoise_top_apply_config - apply the top configs to the initialized tool */ diff --git a/tools/tracing/rtla/src/rtla.c b/tools/tracing/rtla/src/rtla.c deleted file mode 100644 index f4342ca684c3..000000000000 --- a/tools/tracing/rtla/src/rtla.c +++ /dev/null @@ -1,92 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0 -/* - * Copyright (C) 2021 Red Hat Inc, Daniel Bristot de Oliveira - */ - -#include -#include -#include -#include - -#include "osnoise.h" -#include "timerlat.h" - -/* - * rtla_usage - print rtla usage - */ -static void rtla_usage(int err) -{ - int i; - - static const char *msg[] =3D { - "", - "rtla version " VERSION, - "", - " usage: rtla COMMAND ...", - "", - " commands:", - " osnoise - gives information about the operating system noise (osn= oise)", - " hwnoise - gives information about hardware-related noise", - " timerlat - measures the timer irq and thread latency", - "", - NULL, - }; - - for (i =3D 0; msg[i]; i++) - fprintf(stderr, "%s\n", msg[i]); - exit(err); -} - -/* - * run_tool_command - try to run a rtla tool command - * - * It returns 0 if it fails. The tool's main will generally not - * return as they should call exit(). - */ -int run_tool_command(int argc, char **argv, int start_position) -{ - if (strcmp(argv[start_position], "osnoise") =3D=3D 0) { - osnoise_main(argc-start_position, &argv[start_position]); - goto ran; - } else if (strcmp(argv[start_position], "hwnoise") =3D=3D 0) { - hwnoise_main(argc-start_position, &argv[start_position]); - goto ran; - } else if (strcmp(argv[start_position], "timerlat") =3D=3D 0) { - timerlat_main(argc-start_position, &argv[start_position]); - goto ran; - } - - return 0; -ran: - return 1; -} - -/* Set main as weak to allow overriding it for building unit test binary */ -#pragma weak main - -int main(int argc, char *argv[]) -{ - int retval; - - /* is it an alias? */ - retval =3D run_tool_command(argc, argv, 0); - if (retval) - exit(0); - - if (argc < 2) - goto usage; - - if (strcmp(argv[1], "-h") =3D=3D 0) { - rtla_usage(0); - } else if (strcmp(argv[1], "--help") =3D=3D 0) { - rtla_usage(0); - } - - retval =3D run_tool_command(argc, argv, 1); - if (retval) - exit(0); - -usage: - rtla_usage(1); - exit(1); -} diff --git a/tools/tracing/rtla/src/timerlat.c b/tools/tracing/rtla/src/tim= erlat.c index 637f68d684f5..f990c8365776 100644 --- a/tools/tracing/rtla/src/timerlat.c +++ b/tools/tracing/rtla/src/timerlat.c @@ -13,6 +13,8 @@ #include #include =20 +#include + #include "timerlat.h" #include "timerlat_aa.h" #include "timerlat_bpf.h" @@ -231,7 +233,7 @@ void timerlat_free(struct osnoise_tool *tool) free_cpu_idle_disable_states(); } =20 -static void timerlat_usage(int err) +__noreturn static void timerlat_usage(int err) { int i; =20 @@ -269,7 +271,7 @@ int timerlat_main(int argc, char *argv[]) } =20 if ((strcmp(argv[1], "-h") =3D=3D 0) || (strcmp(argv[1], "--help") =3D=3D= 0)) { - timerlat_usage(0); + timerlat_usage(129); } else if (str_has_prefix(argv[1], "-")) { /* the user skipped the tool, call the default one */ run_tool(&timerlat_top_ops, argc, argv); @@ -283,6 +285,5 @@ int timerlat_main(int argc, char *argv[]) } =20 usage: - timerlat_usage(1); - exit(1); + timerlat_usage(129); } diff --git a/tools/tracing/rtla/src/timerlat.h b/tools/tracing/rtla/src/tim= erlat.h index 364203a29abd..37a808f1611e 100644 --- a/tools/tracing/rtla/src/timerlat.h +++ b/tools/tracing/rtla/src/timerlat.h @@ -23,8 +23,8 @@ struct timerlat_params { long long timerlat_period_us; long long print_stack; int dma_latency; - int no_aa; - int dump_tasks; + bool no_aa; + bool dump_tasks; int deepest_idle_state; enum timerlat_tracing_mode mode; const char *bpf_action_program; diff --git a/tools/tracing/rtla/src/timerlat_hist.c b/tools/tracing/rtla/sr= c/timerlat_hist.c index 3a15a85b7d72..df7b1398a966 100644 --- a/tools/tracing/rtla/src/timerlat_hist.c +++ b/tools/tracing/rtla/src/timerlat_hist.c @@ -4,7 +4,6 @@ */ =20 #define _GNU_SOURCE -#include #include #include #include @@ -17,6 +16,7 @@ #include "timerlat.h" #include "timerlat_aa.h" #include "timerlat_bpf.h" +#include "cli.h" #include "common.h" =20 struct timerlat_hist_cpu { @@ -685,321 +685,6 @@ timerlat_print_stats(struct osnoise_tool *tool) osnoise_report_missed_events(tool); } =20 -/* - * timerlat_hist_usage - prints timerlat top usage message - */ -static void timerlat_hist_usage(void) -{ - static const char * const msg_start[] =3D { - "[-d s] [-D] [-n] [-a us] [-p us] [-i us] [-T us] [-s us] \\", - " [-t [file]] [-e sys[:event]] [--filter ] [--trigger ] [-c cpu-list] [-H cpu-list]\\", - " [-P priority] [-E N] [-b N] [--no-irq] [--no-thread] [--no-header] [= --no-summary] \\", - " [--no-index] [--with-zeros] [--dma-latency us] [-C [cgroup_name]] [-= -no-aa] [--dump-tasks] [-u|-k]", - " [--warm-up s] [--deepest-idle-state n]", - NULL, - }; - - static const char * const msg_opts[] =3D { - " -a/--auto: set automatic trace mode, stopping the session if argumen= t in us latency is hit", - " -p/--period us: timerlat period in us", - " -i/--irq us: stop trace if the irq latency is higher than the argume= nt in us", - " -T/--thread us: stop trace if the thread latency is higher than the = argument in us", - " -s/--stack us: save the stack trace at the IRQ if a thread latency i= s higher than the argument in us", - " -c/--cpus cpus: run the tracer only on the given cpus", - " -H/--house-keeping cpus: run rtla control threads only on the given = cpus", - " -C/--cgroup [cgroup_name]: set cgroup, if no cgroup_name is passed, = the rtla's cgroup will be inherited", - " -d/--duration time[m|h|d]: duration of the session in seconds", - " --dump-tasks: prints the task running on all CPUs if stop conditi= ons are met (depends on !--no-aa)", - " -D/--debug: print debug info", - " -t/--trace [file]: save the stopped trace to [file|timerlat_trace.tx= t]", - " -e/--event : enable the in the trace instance= , multiple -e are allowed", - " --filter : enable a trace event filter to the previous -e= event", - " --trigger : enable a trace event trigger to the previous= -e event", - " -n/--nano: display data in nanoseconds", - " --no-aa: disable auto-analysis, reducing rtla timerlat cpu usage", - " -b/--bucket-size N: set the histogram bucket size (default 1)", - " -E/--entries N: set the number of entries of the histogram (default = 256)", - " --no-irq: ignore IRQ latencies", - " --no-thread: ignore thread latencies", - " --no-header: do not print header", - " --no-summary: do not print summary", - " --no-index: do not print index", - " --with-zeros: print zero only entries", - " --dma-latency us: set /dev/cpu_dma_latency latency to reduce= exit from idle latency", - " -P/--priority o:prio|r:prio|f:prio|d:runtime:period : set scheduling= parameters", - " o:prio - use SCHED_OTHER with prio", - " r:prio - use SCHED_RR with prio", - " f:prio - use SCHED_FIFO with prio", - " d:runtime[us|ms|s]:period[us|ms|s] - use SCHED_DEADLINE with runtime = and period", - " in nanoseconds", - " -u/--user-threads: use rtla user-space threads instead of kernel-spa= ce timerlat threads", - " -k/--kernel-threads: use timerlat kernel-space threads instead of rt= la user-space threads", - " -U/--user-load: enable timerlat for user-defined user-space workload= ", - " --warm-up s: let the workload run for s seconds before collecting= data", - " --trace-buffer-size kB: set the per-cpu trace buffer size in kB", - " --deepest-idle-state n: only go down to idle state n on cpus used= by timerlat to reduce exit from idle latency", - " --on-threshold : define action to be executed at latency = threshold, multiple are allowed", - " --on-end : define action to be executed at measurement en= d, multiple are allowed", - " --bpf-action : load and execute BPF program when latency= threshold is exceeded", - " --stack-format : set the stack format (truncate, skip, fu= ll)", - NULL, - }; - - common_usage("timerlat", "hist", "a per-cpu histogram of the timer latenc= y", - msg_start, msg_opts); -} - -/* - * timerlat_hist_parse_args - allocs, parse and fill the cmd line paramete= rs - */ -static struct common_params -*timerlat_hist_parse_args(int argc, char *argv[]) -{ - struct timerlat_params *params; - int auto_thresh; - int retval; - int c; - char *trace_output =3D NULL; - - params =3D calloc_fatal(1, sizeof(*params)); - - actions_init(¶ms->common.threshold_actions); - actions_init(¶ms->common.end_actions); - - /* disabled by default */ - params->dma_latency =3D -1; - - /* disabled by default */ - params->deepest_idle_state =3D -2; - - /* display data in microseconds */ - params->common.output_divisor =3D 1000; - params->common.hist.bucket_size =3D 1; - params->common.hist.entries =3D 256; - - /* default to BPF mode */ - params->mode =3D TRACING_MODE_BPF; - - /* default to truncate stack format */ - params->stack_format =3D STACK_FORMAT_TRUNCATE; - - while (1) { - static struct option long_options[] =3D { - {"auto", required_argument, 0, 'a'}, - {"bucket-size", required_argument, 0, 'b'}, - {"entries", required_argument, 0, 'E'}, - {"help", no_argument, 0, 'h'}, - {"irq", required_argument, 0, 'i'}, - {"nano", no_argument, 0, 'n'}, - {"period", required_argument, 0, 'p'}, - {"stack", required_argument, 0, 's'}, - {"thread", required_argument, 0, 'T'}, - {"trace", optional_argument, 0, 't'}, - {"user-threads", no_argument, 0, 'u'}, - {"kernel-threads", no_argument, 0, 'k'}, - {"user-load", no_argument, 0, 'U'}, - {"no-irq", no_argument, 0, '0'}, - {"no-thread", no_argument, 0, '1'}, - {"no-header", no_argument, 0, '2'}, - {"no-summary", no_argument, 0, '3'}, - {"no-index", no_argument, 0, '4'}, - {"with-zeros", no_argument, 0, '5'}, - {"trigger", required_argument, 0, '6'}, - {"filter", required_argument, 0, '7'}, - {"dma-latency", required_argument, 0, '8'}, - {"no-aa", no_argument, 0, '9'}, - {"dump-tasks", no_argument, 0, '\1'}, - {"warm-up", required_argument, 0, '\2'}, - {"trace-buffer-size", required_argument, 0, '\3'}, - {"deepest-idle-state", required_argument, 0, '\4'}, - {"on-threshold", required_argument, 0, '\5'}, - {"on-end", required_argument, 0, '\6'}, - {"bpf-action", required_argument, 0, '\7'}, - {"stack-format", required_argument, 0, '\10'}, - {0, 0, 0, 0} - }; - - if (common_parse_options(argc, argv, ¶ms->common)) - continue; - - c =3D getopt_auto(argc, argv, long_options); - - /* detect the end of the options. */ - if (c =3D=3D -1) - break; - - switch (c) { - case 'a': - auto_thresh =3D get_llong_from_str(optarg); - - /* set thread stop to auto_thresh */ - params->common.stop_total_us =3D auto_thresh; - params->common.stop_us =3D auto_thresh; - - /* get stack trace */ - params->print_stack =3D auto_thresh; - - /* set trace */ - if (!trace_output) - trace_output =3D "timerlat_trace.txt"; - - break; - case 'b': - params->common.hist.bucket_size =3D get_llong_from_str(optarg); - if (params->common.hist.bucket_size =3D=3D 0 || - params->common.hist.bucket_size >=3D 1000000) - fatal("Bucket size needs to be > 0 and <=3D 1000000"); - break; - case 'E': - params->common.hist.entries =3D get_llong_from_str(optarg); - if (params->common.hist.entries < 10 || - params->common.hist.entries > 9999999) - fatal("Entries must be > 10 and < 9999999"); - break; - case 'h': - case '?': - timerlat_hist_usage(); - break; - case 'i': - params->common.stop_us =3D get_llong_from_str(optarg); - break; - case 'k': - params->common.kernel_workload =3D 1; - break; - case 'n': - params->common.output_divisor =3D 1; - break; - case 'p': - params->timerlat_period_us =3D get_llong_from_str(optarg); - if (params->timerlat_period_us > 1000000) - fatal("Period longer than 1 s"); - break; - case 's': - params->print_stack =3D get_llong_from_str(optarg); - break; - case 'T': - params->common.stop_total_us =3D get_llong_from_str(optarg); - break; - case 't': - trace_output =3D parse_optional_arg(argc, argv); - if (!trace_output) - trace_output =3D "timerlat_trace.txt"; - break; - case 'u': - params->common.user_workload =3D 1; - /* fallback: -u implies in -U */ - case 'U': - params->common.user_data =3D 1; - break; - case '0': /* no irq */ - params->common.hist.no_irq =3D 1; - break; - case '1': /* no thread */ - params->common.hist.no_thread =3D 1; - break; - case '2': /* no header */ - params->common.hist.no_header =3D 1; - break; - case '3': /* no summary */ - params->common.hist.no_summary =3D 1; - break; - case '4': /* no index */ - params->common.hist.no_index =3D 1; - break; - case '5': /* with zeros */ - params->common.hist.with_zeros =3D 1; - break; - case '6': /* trigger */ - if (params->common.events) - trace_event_add_trigger(params->common.events, optarg); - else - fatal("--trigger requires a previous -e"); - break; - case '7': /* filter */ - if (params->common.events) - trace_event_add_filter(params->common.events, optarg); - else - fatal("--filter requires a previous -e"); - break; - case '8': - params->dma_latency =3D get_llong_from_str(optarg); - if (params->dma_latency < 0 || params->dma_latency > 10000) - fatal("--dma-latency needs to be >=3D 0 and < 10000"); - break; - case '9': - params->no_aa =3D 1; - break; - case '\1': - params->dump_tasks =3D 1; - break; - case '\2': - params->common.warmup =3D get_llong_from_str(optarg); - break; - case '\3': - params->common.buffer_size =3D get_llong_from_str(optarg); - break; - case '\4': - params->deepest_idle_state =3D get_llong_from_str(optarg); - break; - case '\5': - retval =3D actions_parse(¶ms->common.threshold_actions, optarg, - "timerlat_trace.txt"); - if (retval) - fatal("Invalid action %s", optarg); - break; - case '\6': - retval =3D actions_parse(¶ms->common.end_actions, optarg, - "timerlat_trace.txt"); - if (retval) - fatal("Invalid action %s", optarg); - break; - case '\7': - params->bpf_action_program =3D optarg; - break; - case '\10': - params->stack_format =3D parse_stack_format(optarg); - if (params->stack_format =3D=3D -1) - fatal("Invalid --stack-format option"); - break; - default: - fatal("Invalid option"); - } - } - - if (trace_output) - actions_add_trace_output(¶ms->common.threshold_actions, trace_output= ); - - if (geteuid()) - fatal("rtla needs root permission"); - - if (params->common.hist.no_irq && params->common.hist.no_thread) - fatal("no-irq and no-thread set, there is nothing to do here"); - - if (params->common.hist.no_index && !params->common.hist.with_zeros) - fatal("no-index set with with-zeros is not set - it does not make sense"= ); - - /* - * Auto analysis only happens if stop tracing, thus: - */ - if (!params->common.stop_us && !params->common.stop_total_us) - params->no_aa =3D 1; - - if (params->common.kernel_workload && params->common.user_workload) - fatal("--kernel-threads and --user-threads are mutually exclusive!"); - - /* - * If auto-analysis or trace output is enabled, switch from BPF mode to - * mixed mode - */ - if (params->mode =3D=3D TRACING_MODE_BPF && - (params->common.threshold_actions.present[ACTION_TRACE_OUTPUT] || - params->common.end_actions.present[ACTION_TRACE_OUTPUT] || - !params->no_aa)) - params->mode =3D TRACING_MODE_MIXED; - - return ¶ms->common; -} - /* * timerlat_hist_apply_config - apply the hist configs to the initialized = tool */ diff --git a/tools/tracing/rtla/src/timerlat_top.c b/tools/tracing/rtla/src= /timerlat_top.c index e3a374005690..18e1071a2e24 100644 --- a/tools/tracing/rtla/src/timerlat_top.c +++ b/tools/tracing/rtla/src/timerlat_top.c @@ -4,7 +4,6 @@ */ =20 #define _GNU_SOURCE -#include #include #include #include @@ -17,6 +16,7 @@ #include "timerlat.h" #include "timerlat_aa.h" #include "timerlat_bpf.h" +#include "cli.h" #include "common.h" =20 struct timerlat_top_cpu { @@ -459,290 +459,6 @@ timerlat_print_stats(struct osnoise_tool *top) osnoise_report_missed_events(top); } =20 -/* - * timerlat_top_usage - prints timerlat top usage message - */ -static void timerlat_top_usage(void) -{ - static const char *const msg_start[] =3D { - "[-q] [-a us] [-d s] [-D] [-n] [-p us] [-i us] [-T us] [-s us] \\", - " [[-t [file]] [-e sys[:event]] [--filter ] [--trigger ] [-c cpu-list] [-H cpu-list]\\", - " [-P priority] [--dma-latency us] [--aa-only us] [-C [cgroup_name]] [= --dump-tasks] [-u|-k] [--warm-up s]\\", - " [--deepest-idle-state n]", - NULL, - }; - - static const char *const msg_opts[] =3D { - " -a/--auto: set automatic trace mode, stopping the session if argumen= t in us latency is hit", - " --aa-only us: stop if latency is hit, only printing the auto= analysis (reduces CPU usage)", - " -p/--period us: timerlat period in us", - " -i/--irq us: stop trace if the irq latency is higher than the argume= nt in us", - " -T/--thread us: stop trace if the thread latency is higher than the = argument in us", - " -s/--stack us: save the stack trace at the IRQ if a thread latency i= s higher than the argument in us", - " -c/--cpus cpus: run the tracer only on the given cpus", - " -H/--house-keeping cpus: run rtla control threads only on the given = cpus", - " -C/--cgroup [cgroup_name]: set cgroup, if no cgroup_name is passed, = the rtla's cgroup will be inherited", - " -d/--duration time[s|m|h|d]: duration of the session", - " -D/--debug: print debug info", - " --dump-tasks: prints the task running on all CPUs if stop conditi= ons are met (depends on !--no-aa)", - " -t/--trace [file]: save the stopped trace to [file|timerlat_trace.tx= t]", - " -e/--event : enable the in the trace instance= , multiple -e are allowed", - " --filter : enable a trace event filter to the previous -= e event", - " --trigger : enable a trace event trigger to the previous= -e event", - " -n/--nano: display data in nanoseconds", - " --no-aa: disable auto-analysis, reducing rtla timerlat cpu usage", - " -q/--quiet print only a summary at the end", - " --dma-latency us: set /dev/cpu_dma_latency latency to reduce= exit from idle latency", - " -P/--priority o:prio|r:prio|f:prio|d:runtime:period : set scheduling= parameters", - " o:prio - use SCHED_OTHER with prio", - " r:prio - use SCHED_RR with prio", - " f:prio - use SCHED_FIFO with prio", - " d:runtime[us|ms|s]:period[us|ms|s] - use SCHED_DEADLINE with runtime = and period", - " in nanoseconds", - " -u/--user-threads: use rtla user-space threads instead of kernel-spa= ce timerlat threads", - " -k/--kernel-threads: use timerlat kernel-space threads instead of rt= la user-space threads", - " -U/--user-load: enable timerlat for user-defined user-space workload= ", - " --warm-up s: let the workload run for s seconds before collecting= data", - " --trace-buffer-size kB: set the per-cpu trace buffer size in kB", - " --deepest-idle-state n: only go down to idle state n on cpus used= by timerlat to reduce exit from idle latency", - " --on-threshold : define action to be executed at latency = threshold, multiple are allowed", - " --on-end: define action to be executed at measurement end, multip= le are allowed", - " --bpf-action : load and execute BPF program when latency= threshold is exceeded", - " --stack-format : set the stack format (truncate, skip, fu= ll)", - NULL, - }; - - common_usage("timerlat", "top", "a per-cpu summary of the timer latency", - msg_start, msg_opts); -} - -/* - * timerlat_top_parse_args - allocs, parse and fill the cmd line parameters - */ -static struct common_params -*timerlat_top_parse_args(int argc, char **argv) -{ - struct timerlat_params *params; - long long auto_thresh; - int retval; - int c; - char *trace_output =3D NULL; - - params =3D calloc_fatal(1, sizeof(*params)); - - actions_init(¶ms->common.threshold_actions); - actions_init(¶ms->common.end_actions); - - /* disabled by default */ - params->dma_latency =3D -1; - - /* disabled by default */ - params->deepest_idle_state =3D -2; - - /* display data in microseconds */ - params->common.output_divisor =3D 1000; - - /* default to BPF mode */ - params->mode =3D TRACING_MODE_BPF; - - /* default to truncate stack format */ - params->stack_format =3D STACK_FORMAT_TRUNCATE; - - while (1) { - static struct option long_options[] =3D { - {"auto", required_argument, 0, 'a'}, - {"help", no_argument, 0, 'h'}, - {"irq", required_argument, 0, 'i'}, - {"nano", no_argument, 0, 'n'}, - {"period", required_argument, 0, 'p'}, - {"quiet", no_argument, 0, 'q'}, - {"stack", required_argument, 0, 's'}, - {"thread", required_argument, 0, 'T'}, - {"trace", optional_argument, 0, 't'}, - {"user-threads", no_argument, 0, 'u'}, - {"kernel-threads", no_argument, 0, 'k'}, - {"user-load", no_argument, 0, 'U'}, - {"trigger", required_argument, 0, '0'}, - {"filter", required_argument, 0, '1'}, - {"dma-latency", required_argument, 0, '2'}, - {"no-aa", no_argument, 0, '3'}, - {"dump-tasks", no_argument, 0, '4'}, - {"aa-only", required_argument, 0, '5'}, - {"warm-up", required_argument, 0, '6'}, - {"trace-buffer-size", required_argument, 0, '7'}, - {"deepest-idle-state", required_argument, 0, '8'}, - {"on-threshold", required_argument, 0, '9'}, - {"on-end", required_argument, 0, '\1'}, - {"bpf-action", required_argument, 0, '\2'}, - {"stack-format", required_argument, 0, '\3'}, - {0, 0, 0, 0} - }; - - if (common_parse_options(argc, argv, ¶ms->common)) - continue; - - c =3D getopt_auto(argc, argv, long_options); - - /* detect the end of the options. */ - if (c =3D=3D -1) - break; - - switch (c) { - case 'a': - auto_thresh =3D get_llong_from_str(optarg); - - /* set thread stop to auto_thresh */ - params->common.stop_total_us =3D auto_thresh; - params->common.stop_us =3D auto_thresh; - - /* get stack trace */ - params->print_stack =3D auto_thresh; - - /* set trace */ - if (!trace_output) - trace_output =3D "timerlat_trace.txt"; - - break; - case '5': - /* it is here because it is similar to -a */ - auto_thresh =3D get_llong_from_str(optarg); - - /* set thread stop to auto_thresh */ - params->common.stop_total_us =3D auto_thresh; - params->common.stop_us =3D auto_thresh; - - /* get stack trace */ - params->print_stack =3D auto_thresh; - - /* set aa_only to avoid parsing the trace */ - params->common.aa_only =3D 1; - break; - case 'h': - case '?': - timerlat_top_usage(); - break; - case 'i': - params->common.stop_us =3D get_llong_from_str(optarg); - break; - case 'k': - params->common.kernel_workload =3D true; - break; - case 'n': - params->common.output_divisor =3D 1; - break; - case 'p': - params->timerlat_period_us =3D get_llong_from_str(optarg); - if (params->timerlat_period_us > 1000000) - fatal("Period longer than 1 s"); - break; - case 'q': - params->common.quiet =3D 1; - break; - case 's': - params->print_stack =3D get_llong_from_str(optarg); - break; - case 'T': - params->common.stop_total_us =3D get_llong_from_str(optarg); - break; - case 't': - trace_output =3D parse_optional_arg(argc, argv); - if (!trace_output) - trace_output =3D "timerlat_trace.txt"; - break; - case 'u': - params->common.user_workload =3D true; - /* fallback: -u implies -U */ - case 'U': - params->common.user_data =3D true; - break; - case '0': /* trigger */ - if (params->common.events) - trace_event_add_trigger(params->common.events, optarg); - else - fatal("--trigger requires a previous -e"); - break; - case '1': /* filter */ - if (params->common.events) - trace_event_add_filter(params->common.events, optarg); - else - fatal("--filter requires a previous -e"); - break; - case '2': /* dma-latency */ - params->dma_latency =3D get_llong_from_str(optarg); - if (params->dma_latency < 0 || params->dma_latency > 10000) - fatal("--dma-latency needs to be >=3D 0 and < 10000"); - break; - case '3': /* no-aa */ - params->no_aa =3D 1; - break; - case '4': - params->dump_tasks =3D 1; - break; - case '6': - params->common.warmup =3D get_llong_from_str(optarg); - break; - case '7': - params->common.buffer_size =3D get_llong_from_str(optarg); - break; - case '8': - params->deepest_idle_state =3D get_llong_from_str(optarg); - break; - case '9': - retval =3D actions_parse(¶ms->common.threshold_actions, optarg, - "timerlat_trace.txt"); - if (retval) - fatal("Invalid action %s", optarg); - break; - case '\1': - retval =3D actions_parse(¶ms->common.end_actions, optarg, - "timerlat_trace.txt"); - if (retval) - fatal("Invalid action %s", optarg); - break; - case '\2': - params->bpf_action_program =3D optarg; - break; - case '\3': - params->stack_format =3D parse_stack_format(optarg); - if (params->stack_format =3D=3D -1) - fatal("Invalid --stack-format option"); - break; - default: - fatal("Invalid option"); - } - } - - if (trace_output) - actions_add_trace_output(¶ms->common.threshold_actions, trace_output= ); - - if (geteuid()) - fatal("rtla needs root permission"); - - /* - * Auto analysis only happens if stop tracing, thus: - */ - if (!params->common.stop_us && !params->common.stop_total_us) - params->no_aa =3D 1; - - if (params->no_aa && params->common.aa_only) - fatal("--no-aa and --aa-only are mutually exclusive!"); - - if (params->common.kernel_workload && params->common.user_workload) - fatal("--kernel-threads and --user-threads are mutually exclusive!"); - - /* - * If auto-analysis or trace output is enabled, switch from BPF mode to - * mixed mode - */ - if (params->mode =3D=3D TRACING_MODE_BPF && - (params->common.threshold_actions.present[ACTION_TRACE_OUTPUT] || - params->common.end_actions.present[ACTION_TRACE_OUTPUT] || - !params->no_aa)) - params->mode =3D TRACING_MODE_MIXED; - - return ¶ms->common; -} - /* * timerlat_top_apply_config - apply the top configs to the initialized to= ol */ diff --git a/tools/tracing/rtla/src/utils.c b/tools/tracing/rtla/src/utils.c index 9cec5b3e02c8..cb187e7d48d1 100644 --- a/tools/tracing/rtla/src/utils.c +++ b/tools/tracing/rtla/src/utils.c @@ -22,7 +22,7 @@ #include "common.h" =20 #define MAX_MSG_LENGTH 1024 -int config_debug; +bool config_debug; =20 /* * err_msg - print an error message to the stderr @@ -1011,32 +1011,6 @@ int auto_house_keeping(cpu_set_t *monitored_cpus) return 1; } =20 -/** - * parse_optional_arg - Parse optional argument value - * - * Parse optional argument value, which can be in the form of: - * -sarg, -s/--long=3Darg, -s/--long arg - * - * Returns arg value if found, NULL otherwise. - */ -char *parse_optional_arg(int argc, char **argv) -{ - if (optarg) { - if (optarg[0] =3D=3D '=3D') { - /* skip the =3D */ - return &optarg[1]; - } else { - return optarg; - } - /* parse argument of form -s [arg] and --long [arg]*/ - } else if (optind < argc && argv[optind][0] !=3D '-') { - /* consume optind */ - return argv[optind++]; - } else { - return NULL; - } -} - /* * strtoi - convert string to integer with error checking * diff --git a/tools/tracing/rtla/src/utils.h b/tools/tracing/rtla/src/utils.h index 96fd72042717..2ba3333669bb 100644 --- a/tools/tracing/rtla/src/utils.h +++ b/tools/tracing/rtla/src/utils.h @@ -39,7 +39,7 @@ static inline bool str_has_prefix(const char *str, const = char *prefix) return strncmp(str, prefix, strlen(prefix)) =3D=3D 0; } =20 -extern int config_debug; +extern bool config_debug; void debug_msg(const char *fmt, ...); void err_msg(const char *fmt, ...); void fatal(const char *fmt, ...); @@ -47,7 +47,6 @@ void fatal(const char *fmt, ...); long parse_seconds_duration(char *val); void get_duration(time_t start_time, char *output, int output_size); =20 -char *parse_optional_arg(int argc, char **argv); long long get_llong_from_str(char *start); =20 static inline void diff --git a/tools/tracing/rtla/tests/hwnoise.t b/tools/tracing/rtla/tests/= hwnoise.t index 23ce250a6852..cfe687ff5ee1 100644 --- a/tools/tracing/rtla/tests/hwnoise.t +++ b/tools/tracing/rtla/tests/hwnoise.t @@ -6,7 +6,7 @@ test_begin set_timeout 2m =20 check "verify help page" \ - "hwnoise --help" 0 "summary of hardware-related noise" + "hwnoise --help" 129 "Usage: rtla hwnoise" check "detect noise higher than one microsecond" \ "hwnoise -c 0 -T 1 -d 5s -q" 0 check "set the automatic trace mode" \ diff --git a/tools/tracing/rtla/tests/osnoise.t b/tools/tracing/rtla/tests/= osnoise.t index 06787471d0e8..c3dce1c9d197 100644 --- a/tools/tracing/rtla/tests/osnoise.t +++ b/tools/tracing/rtla/tests/osnoise.t @@ -6,9 +6,9 @@ test_begin set_timeout 2m =20 check "verify help page" \ - "osnoise --help" 0 "osnoise version" + "osnoise --help" 129 "osnoise version" check_top_hist "verify help page" \ - "osnoise TOOL --help" 0 "rtla osnoise" + "osnoise TOOL --help" 129 "rtla osnoise" check_top_q_hist "verify the --stop/-s param" \ "osnoise TOOL -s 30 -T 1" 2 "osnoise hit stop tracing" check_top_q_hist "verify the --trace param" \ @@ -32,7 +32,7 @@ check "hist with -b/--bucket-size" \ check "hist with -E/--entries" \ "osnoise hist -E 10 -d 1s" check "hist with -E/--entries out of range" \ - "osnoise hist -E 1 -d 1s" 1 "^Entries must be > 10 and < 9999999$" + "osnoise hist -E 1 -d 1s" 1 "^Entries must be > 10 and < 10000000$" check "hist with --no-header" \ "osnoise hist --no-header -d 1s" 0 "" "RTLA osnoise histogram" check "hist with --with-zeros" \ diff --git a/tools/tracing/rtla/tests/timerlat.t b/tools/tracing/rtla/tests= /timerlat.t index 3ebfe316b39e..27fd3143b379 100644 --- a/tools/tracing/rtla/tests/timerlat.t +++ b/tools/tracing/rtla/tests/timerlat.t @@ -21,9 +21,9 @@ export RTLA_NO_BPF=3D$option =20 # Basic tests check "verify help page" \ - "timerlat --help" 0 "timerlat version" + "timerlat --help" 129 "timerlat version" check_top_hist "verify help page" \ - "timerlat TOOL --help" 0 "rtla timerlat" + "timerlat TOOL --help" 129 "rtla timerlat" check_top_hist "verify -s/--stack" \ "timerlat TOOL -s 3 -T 10 -t" 2 "Blocking thread stack trace" check_top_hist "test in nanoseconds" \ @@ -61,7 +61,7 @@ check "hist with -b/--bucket-size" \ check "hist with -E/--entries" \ "timerlat hist -E 10 -d 1s" check "hist with -E/--entries out of range" \ - "timerlat hist -E 1 -d 1s" 1 "^Entries must be > 10 and < 9999999$" + "timerlat hist -E 1 -d 1s" 1 "^Entries must be > 10 and < 10000000$" check "hist with --no-header" \ "timerlat hist --no-header -d 1s" 0 "" "RTLA timerlat histogram" check "hist with --with-zeros" \ --=20 2.54.0 From nobody Sun May 24 20:34:47 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2114F3BAD9D for ; Thu, 21 May 2026 14:19:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779373167; cv=none; b=jSz87Z5kp5D8x+OEMuaRu4QonGh2qdQNKt+qG1QM2QkYAudohw85IIqNO0EOlGuJJYoK15YzplBDfVCJsV7+sZ22rRAJPnk9hzpg6o65JsIPq6mW98Q1+VkjJqKwHzo6grUSykW4omitDuPgQkhCLcSgiGvnOFE2ZG69QNmT+yc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779373167; c=relaxed/simple; bh=Jvya3ZuBR7T7+GLQKjRyyv37cDSNBtI0Ejw1tNuKJCw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=cxryO80TcIfuuq8b9K+n9hr62lJNn9P11rOMqQcZ2uQFh2jkD3PGPzGxe8uwG3Y1UtA5zMqkFxEBr5IAFOXDu49knCiQAPtZZSU1IEGrZxc+BzvWsQwbg0T6NRqC0kESdrQFoFLziJTMAOV0MeKmn7UxFXc2PZSvxFMHTqy+A1c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=W2uIgULs; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="W2uIgULs" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1779373163; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Q0q5TN8aNLEUi6dYvfKTD8Pb49y0VBIbW3CT2wMjCCU=; b=W2uIgULsSigNIaJIiJRS7QFs7q1aJME3ncRN0OEFJfDC07fZsV2R5cFc9KYTjU6dkU20cF tQ9fK0hhtnfPEySP8sWkVNb67d34/mPI0RM6lnQvp0k86yoyS149RxuhbBhL+3Qx1CVqmw KyPNReGLs82QskP0daCI4vn6XHg1+pw= Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-199-7twkPLmsNH-F14r9UxDfRw-1; Thu, 21 May 2026 10:19:00 -0400 X-MC-Unique: 7twkPLmsNH-F14r9UxDfRw-1 X-Mimecast-MFC-AGG-ID: 7twkPLmsNH-F14r9UxDfRw_1779373139 Received: from mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.95]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id D588F1800633; Thu, 21 May 2026 14:18:58 +0000 (UTC) Received: from fedora.brq.redhat.com (unknown [10.43.17.109]) by mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 55A33176E; Thu, 21 May 2026 14:18:55 +0000 (UTC) From: Tomas Glozar To: Steven Rostedt , Tomas Glozar Cc: John Kacur , Luis Goncalves , Crystal Wood , Costa Shulyupin , Wander Lairson Costa , Ivan Pravdin , Namhyung Kim , Ian Rogers , Arnaldo Carvalho de Melo , LKML , linux-trace-kernel , linux-perf-users Subject: [PATCH v2 5/6] rtla/tests: Add unit tests for _parse_args() functions Date: Thu, 21 May 2026 16:18:32 +0200 Message-ID: <20260521141833.2353025-6-tglozar@redhat.com> In-Reply-To: <20260521141833.2353025-1-tglozar@redhat.com> References: <20260521141833.2353025-1-tglozar@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.6 on 10.30.177.95 Content-Type: text/plain; charset="utf-8" Add a test suite for the _parse_args() function of each tool that checks the params structures (struct common_params, struct osnoise_params, struct timerlat_params) returned by them for correctness. One test case is added per option, as well as a few special cases for tricky combinations of options. Test cases are ordered the same as the option arrays and help message to allow easy checking of whether all options are covered. This should help clarify what the proper command line behavior of RTLA is in case there are holes in the documentation and verify that the intended behavior is implemented correctly. A few necessary changes to the unit tests were done as part of this commit: - Unit tests now also link to libsubcmd and its dependencies. - A new global variable in_unit_test is added to RTLA's CLI interface, causing it to skip check for root if running in unit tests. This allows the CLI unit tests to run as non-root, like existing unit tests. There is quite a lot of duplication, some of it is mitigated with macros, but partially it is intentional so that future changes in behavior are tracked across tools. Signed-off-by: Tomas Glozar --- tools/tracing/rtla/src/cli.c | 8 +- tools/tracing/rtla/src/cli.h | 2 + tools/tracing/rtla/tests/unit/Build | 4 + tools/tracing/rtla/tests/unit/Makefile.unit | 2 +- .../rtla/tests/unit/cli_params_assert.h | 68 ++ .../rtla/tests/unit/osnoise_hist_cli.c | 557 ++++++++++++++ .../tracing/rtla/tests/unit/osnoise_top_cli.c | 503 +++++++++++++ .../rtla/tests/unit/timerlat_hist_cli.c | 702 ++++++++++++++++++ .../rtla/tests/unit/timerlat_top_cli.c | 634 ++++++++++++++++ tools/tracing/rtla/tests/unit/unit_tests.c | 11 + 10 files changed, 2486 insertions(+), 5 deletions(-) create mode 100644 tools/tracing/rtla/tests/unit/cli_params_assert.h create mode 100644 tools/tracing/rtla/tests/unit/osnoise_hist_cli.c create mode 100644 tools/tracing/rtla/tests/unit/osnoise_top_cli.c create mode 100644 tools/tracing/rtla/tests/unit/timerlat_hist_cli.c create mode 100644 tools/tracing/rtla/tests/unit/timerlat_top_cli.c diff --git a/tools/tracing/rtla/src/cli.c b/tools/tracing/rtla/src/cli.c index 7f531519df44..709219341a56 100644 --- a/tools/tracing/rtla/src/cli.c +++ b/tools/tracing/rtla/src/cli.c @@ -124,7 +124,7 @@ struct common_params *osnoise_top_parse_args(int argc, = char **argv) if (cb_data.trace_output) actions_add_trace_output(¶ms->common.threshold_actions, cb_data.trac= e_output); =20 - if (geteuid()) + if (geteuid() && !in_unit_test) fatal("osnoise needs root permission"); =20 return ¶ms->common; @@ -206,7 +206,7 @@ struct common_params *osnoise_hist_parse_args(int argc,= char **argv) if (cb_data.trace_output) actions_add_trace_output(¶ms->common.threshold_actions, cb_data.trac= e_output); =20 - if (geteuid()) + if (geteuid() && !in_unit_test) fatal("rtla needs root permission"); =20 if (params->common.hist.no_index && !params->common.hist.with_zeros) @@ -301,7 +301,7 @@ struct common_params *timerlat_top_parse_args(int argc,= char **argv) if (cb_data.trace_output) actions_add_trace_output(¶ms->common.threshold_actions, cb_data.trac= e_output); =20 - if (geteuid()) + if (geteuid() && !in_unit_test) fatal("rtla needs root permission"); =20 /* @@ -427,7 +427,7 @@ struct common_params *timerlat_hist_parse_args(int argc= , char **argv) if (cb_data.trace_output) actions_add_trace_output(¶ms->common.threshold_actions, cb_data.trac= e_output); =20 - if (geteuid()) + if (geteuid() && !in_unit_test) fatal("rtla needs root permission"); =20 if (params->common.hist.no_irq && params->common.hist.no_thread) diff --git a/tools/tracing/rtla/src/cli.h b/tools/tracing/rtla/src/cli.h index c49ccb3e92f5..633a2322cf89 100644 --- a/tools/tracing/rtla/src/cli.h +++ b/tools/tracing/rtla/src/cli.h @@ -5,3 +5,5 @@ struct common_params *osnoise_top_parse_args(int argc, char= **argv); struct common_params *osnoise_hist_parse_args(int argc, char **argv); struct common_params *timerlat_top_parse_args(int argc, char **argv); struct common_params *timerlat_hist_parse_args(int argc, char **argv); + +extern bool in_unit_test; diff --git a/tools/tracing/rtla/tests/unit/Build b/tools/tracing/rtla/tests= /unit/Build index 2749f4cf202a..16139f17ea1f 100644 --- a/tools/tracing/rtla/tests/unit/Build +++ b/tools/tracing/rtla/tests/unit/Build @@ -1,3 +1,7 @@ unit_tests-y +=3D utils.o unit_tests-y +=3D actions.o unit_tests-y +=3D unit_tests.o +unit_tests-y +=3D osnoise_top_cli.o +unit_tests-y +=3D osnoise_hist_cli.o +unit_tests-y +=3D timerlat_top_cli.o +unit_tests-y +=3D timerlat_hist_cli.o diff --git a/tools/tracing/rtla/tests/unit/Makefile.unit b/tools/tracing/rt= la/tests/unit/Makefile.unit index bacb00164e46..8c33a9583c30 100644 --- a/tools/tracing/rtla/tests/unit/Makefile.unit +++ b/tools/tracing/rtla/tests/unit/Makefile.unit @@ -3,7 +3,7 @@ UNIT_TESTS :=3D $(OUTPUT)unit_tests UNIT_TESTS_IN :=3D $(UNIT_TESTS)-in.o =20 -$(UNIT_TESTS): $(UNIT_TESTS_IN) $(RTLA_IN) +$(UNIT_TESTS): $(UNIT_TESTS_IN) $(RTLA_IN) $(LIBSUBCMD) $(LIB_STRING) $(LI= B_STR_ERROR_R) $(QUIET_LINK)$(CC) $(LDFLAGS) -o $@ $^ $(EXTLIBS) -lcheck =20 $(UNIT_TESTS_IN): fixdep diff --git a/tools/tracing/rtla/tests/unit/cli_params_assert.h b/tools/trac= ing/rtla/tests/unit/cli_params_assert.h new file mode 100644 index 000000000000..4bc7d582fcf4 --- /dev/null +++ b/tools/tracing/rtla/tests/unit/cli_params_assert.h @@ -0,0 +1,68 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#pragma once + +#include "../../src/timerlat.h" + +/* Tracing Options */ + +#define CLI_ASSERT_SINGLE_EVENT(_system, _event) do {\ + ck_assert_ptr_nonnull(params->events);\ + ck_assert_str_eq(params->events->system, _system);\ + ck_assert_str_eq(params->events->event, _event);\ + ck_assert_ptr_null(params->events->next);\ +} while (0) + +#define CLI_ASSERT_SINGLE_FILTER(_filter) do {\ + ck_assert_ptr_nonnull(params->events);\ + ck_assert_str_eq(params->events->filter, _filter);\ + ck_assert_ptr_null(params->events->next);\ +} while (0) + +#define CLI_ASSERT_SINGLE_TRIGGER(_trigger) do {\ + ck_assert_ptr_nonnull(params->events);\ + ck_assert_str_eq(params->events->trigger, _trigger);\ + ck_assert_ptr_null(params->events->next);\ +} while (0) + +/* CPU Configuration */ + +#define CLI_ASSERT_CPUSET(_field, ...) do {\ + int n;\ + int cpus[] =3D { __VA_ARGS__ };\ + for (n =3D 0; n < sizeof(cpus) / sizeof(int); n++)\ + ck_assert(CPU_ISSET(cpus[n], ¶ms->_field));\ + ck_assert_int_eq(CPU_COUNT(¶ms->_field), n);\ +} while (0) + +/* Auto Analysis and Actions */ + +#define CLI_OSNOISE_ASSERT_AUTO(_stop) do {\ + ck_assert_int_eq(params->stop_us, _stop);\ + ck_assert_int_eq(osn_params->threshold, 1);\ + ck_assert_int_eq(params->threshold_actions.len, 1);\ + ck_assert_int_eq(params->threshold_actions.list[0].type, ACTION_TRACE_OUT= PUT);\ + ck_assert_str_eq(params->threshold_actions.list[0].trace_output, "osnoise= _trace.txt");\ +} while (0) + +#define CLI_TIMERLAT_ASSERT_AUTO(_threshold) do {\ + ck_assert_int_eq(params->stop_us, _threshold);\ + ck_assert_int_eq(params->stop_total_us, _threshold);\ + ck_assert_int_eq(tlat_params->print_stack, _threshold);\ + ck_assert_int_eq(params->threshold_actions.len, 1);\ + ck_assert_int_eq(params->threshold_actions.list[0].type, ACTION_TRACE_OUT= PUT);\ + ck_assert_str_eq(params->threshold_actions.list[0].trace_output, "timerla= t_trace.txt");\ +} while (0) + +#define CLI_TIMERLAT_ASSERT_AA_ONLY(_threshold) do {\ + ck_assert_int_eq(params->stop_us, _threshold);\ + ck_assert_int_eq(params->stop_total_us, _threshold);\ + ck_assert_int_eq(tlat_params->print_stack, _threshold);\ + ck_assert_int_eq(params->threshold_actions.len, 0);\ + ck_assert(params->aa_only);\ +} while (0) + +#define CLI_ASSERT_SINGLE_ACTION(_actions, _type, _arg, _valtype, _value) = do {\ + ck_assert_int_eq(params->_actions.len, 1);\ + ck_assert_int_eq(params->_actions.list[0].type, _type);\ + ck_assert_##_valtype##_eq(params->_actions.list[0]._arg, _value);\ +} while (0) diff --git a/tools/tracing/rtla/tests/unit/osnoise_hist_cli.c b/tools/traci= ng/rtla/tests/unit/osnoise_hist_cli.c new file mode 100644 index 000000000000..3661529f93dc --- /dev/null +++ b/tools/tracing/rtla/tests/unit/osnoise_hist_cli.c @@ -0,0 +1,557 @@ +// SPDX-License-Identifier: GPL-2.0 + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include + +#include "cli_params_assert.h" +#include "../../src/cli.h" + +#define PARSE_ARGS(...) char *argv[] =3D { __VA_ARGS__, NULL };\ + int argc =3D sizeof(argv) / sizeof(char *) - 1;\ + struct common_params *params =3D\ + osnoise_hist_parse_args(argc, argv);\ + struct osnoise_params *osn_params __maybe_unused =3D\ + to_osnoise_params(params) + +/* Tracing Options */ + +START_TEST(test_period_short) +{ + PARSE_ARGS("osnoise", "hist", "-p", "100000"); + + ck_assert_int_eq(osn_params->period, 100000); +} +END_TEST + +START_TEST(test_period_long) +{ + PARSE_ARGS("osnoise", "hist", "--period", "100000"); + + ck_assert_int_eq(osn_params->period, 100000); +} +END_TEST + +START_TEST(test_runtime_short) +{ + PARSE_ARGS("osnoise", "hist", "-r", "95000"); + + ck_assert_int_eq(osn_params->runtime, 95000); +} +END_TEST + +START_TEST(test_runtime_long) +{ + PARSE_ARGS("osnoise", "hist", "--runtime", "95000"); + + ck_assert_int_eq(osn_params->runtime, 95000); +} +END_TEST + +START_TEST(test_stop_short) +{ + PARSE_ARGS("osnoise", "hist", "-s", "20"); + + ck_assert_int_eq(params->stop_us, 20); +} +END_TEST + +START_TEST(test_stop_long) +{ + PARSE_ARGS("osnoise", "hist", "--stop", "20"); + + ck_assert_int_eq(params->stop_us, 20); +} +END_TEST + +START_TEST(test_stop_total_short) +{ + PARSE_ARGS("osnoise", "hist", "-S", "20"); + + ck_assert_int_eq(params->stop_total_us, 20); +} +END_TEST + +START_TEST(test_stop_total_long) +{ + PARSE_ARGS("osnoise", "hist", "--stop-total", "20"); + + ck_assert_int_eq(params->stop_total_us, 20); +} +END_TEST + +START_TEST(test_threshold_short) +{ + PARSE_ARGS("osnoise", "hist", "-T", "5"); + + ck_assert_int_eq(osn_params->threshold, 5); +} +END_TEST + +START_TEST(test_threshold_long) +{ + PARSE_ARGS("osnoise", "hist", "--threshold", "5"); + + ck_assert_int_eq(osn_params->threshold, 5); +} +END_TEST + +START_TEST(test_trace_short_noarg) +{ + PARSE_ARGS("osnoise", "hist", "-t"); + + CLI_ASSERT_SINGLE_ACTION(threshold_actions, ACTION_TRACE_OUTPUT, trace_ou= tput, str, + "osnoise_trace.txt"); +} +END_TEST + +START_TEST(test_trace_short_followarg) +{ + PARSE_ARGS("osnoise", "hist", "-t", "-d", "20"); + + CLI_ASSERT_SINGLE_ACTION(threshold_actions, ACTION_TRACE_OUTPUT, trace_ou= tput, str, + "osnoise_trace.txt"); + ck_assert_int_eq(params->duration, 20); /* check if next argument is read= correctly */ +} +END_TEST + +START_TEST(test_trace_short_space) +{ + PARSE_ARGS("osnoise", "hist", "-t", "tracefile"); + + CLI_ASSERT_SINGLE_ACTION(threshold_actions, ACTION_TRACE_OUTPUT, trace_ou= tput, str, + "tracefile"); +} +END_TEST + +START_TEST(test_trace_short_equals) +{ + PARSE_ARGS("osnoise", "hist", "-t=3Dtracefile"); + + CLI_ASSERT_SINGLE_ACTION(threshold_actions, ACTION_TRACE_OUTPUT, trace_ou= tput, str, + "tracefile"); +} +END_TEST + +START_TEST(test_trace_long_noarg) +{ + PARSE_ARGS("osnoise", "hist", "--trace"); + + CLI_ASSERT_SINGLE_ACTION(threshold_actions, ACTION_TRACE_OUTPUT, trace_ou= tput, str, + "osnoise_trace.txt"); +} +END_TEST + +START_TEST(test_trace_long_followarg) +{ + PARSE_ARGS("osnoise", "hist", "--trace", "-d", "20"); + + CLI_ASSERT_SINGLE_ACTION(threshold_actions, ACTION_TRACE_OUTPUT, trace_ou= tput, str, + "osnoise_trace.txt"); + ck_assert_int_eq(params->duration, 20); /* check if next argument is read= correctly */ +} +END_TEST + +START_TEST(test_trace_long_space) +{ + PARSE_ARGS("osnoise", "hist", "--trace", "tracefile"); + + CLI_ASSERT_SINGLE_ACTION(threshold_actions, ACTION_TRACE_OUTPUT, trace_ou= tput, str, + "tracefile"); +} +END_TEST + +START_TEST(test_trace_long_equals) +{ + PARSE_ARGS("osnoise", "hist", "--trace=3Dtracefile"); + + CLI_ASSERT_SINGLE_ACTION(threshold_actions, ACTION_TRACE_OUTPUT, trace_ou= tput, str, + "tracefile"); +} +END_TEST + +/* Event Configuration */ + +START_TEST(test_event_short) +{ + PARSE_ARGS("osnoise", "hist", "-e", "system:event"); + + CLI_ASSERT_SINGLE_EVENT("system", "event"); +} +END_TEST + +START_TEST(test_event_long) +{ + PARSE_ARGS("osnoise", "hist", "--event", "system:event"); + + CLI_ASSERT_SINGLE_EVENT("system", "event"); +} +END_TEST + +START_TEST(test_filter) +{ + PARSE_ARGS("osnoise", "hist", "-e", "system:event", "--filter", "filter"); + + CLI_ASSERT_SINGLE_FILTER("filter"); +} +END_TEST + +START_TEST(test_trigger) +{ + PARSE_ARGS("osnoise", "hist", "-e", "system:event", "--trigger", "trigger= "); + + CLI_ASSERT_SINGLE_TRIGGER("trigger"); +} +END_TEST + +/* CPU Configuration */ + +START_TEST(test_cpus_short) +{ + nr_cpus =3D 4; + + PARSE_ARGS("osnoise", "hist", "-c", "0-1,3"); + + ck_assert_str_eq(params->cpus, "0-1,3"); + CLI_ASSERT_CPUSET(monitored_cpus, 0, 1, 3); +} +END_TEST + +START_TEST(test_cpus_long) +{ + nr_cpus =3D 4; + + PARSE_ARGS("osnoise", "hist", "--cpus", "0-1,3"); + + ck_assert_str_eq(params->cpus, "0-1,3"); + CLI_ASSERT_CPUSET(monitored_cpus, 0, 1, 3); +} +END_TEST + +START_TEST(test_housekeeping_short) +{ + nr_cpus =3D 4; + + PARSE_ARGS("osnoise", "hist", "-H", "0-1,3"); + + CLI_ASSERT_CPUSET(hk_cpu_set, 0, 1, 3); +} +END_TEST + +START_TEST(test_housekeeping_long) +{ + nr_cpus =3D 4; + + PARSE_ARGS("osnoise", "hist", "--house-keeping", "0-1,3"); + + CLI_ASSERT_CPUSET(hk_cpu_set, 0, 1, 3); +} +END_TEST + +/* Thread Configuration */ + +START_TEST(test_cgroup_short_noarg) +{ + PARSE_ARGS("osnoise", "hist", "-C"); + + ck_assert(params->cgroup); + ck_assert_ptr_null(params->cgroup_name); +} +END_TEST + +START_TEST(test_cgroup_short_space) +{ + PARSE_ARGS("osnoise", "hist", "-C", "cgroup"); + + ck_assert(params->cgroup); + ck_assert_str_eq(params->cgroup_name, "cgroup"); +} +END_TEST + +START_TEST(test_cgroup_short_equals) +{ + PARSE_ARGS("osnoise", "hist", "-C=3Dcgroup"); + + ck_assert(params->cgroup); + ck_assert_str_eq(params->cgroup_name, "cgroup"); +} +END_TEST + +START_TEST(test_cgroup_long_noarg) +{ + PARSE_ARGS("osnoise", "hist", "--cgroup"); + + ck_assert(params->cgroup); + ck_assert_ptr_null(params->cgroup_name); +} +END_TEST + +START_TEST(test_cgroup_long_space) +{ + PARSE_ARGS("osnoise", "hist", "--cgroup", "cgroup"); + + ck_assert(params->cgroup); + ck_assert_str_eq(params->cgroup_name, "cgroup"); +} +END_TEST + +START_TEST(test_cgroup_long_equals) +{ + PARSE_ARGS("osnoise", "hist", "--cgroup=3Dcgroup"); + + ck_assert(params->cgroup); + ck_assert_str_eq(params->cgroup_name, "cgroup"); +} +END_TEST + +START_TEST(test_priority_short) +{ + PARSE_ARGS("osnoise", "hist", "-P", "f:95"); + + ck_assert_int_eq(params->sched_param.sched_policy, SCHED_FIFO); + ck_assert_int_eq(params->sched_param.sched_priority, 95); +} +END_TEST + +START_TEST(test_priority_long) +{ + PARSE_ARGS("osnoise", "hist", "--priority", "f:95"); + + ck_assert_int_eq(params->sched_param.sched_policy, SCHED_FIFO); + ck_assert_int_eq(params->sched_param.sched_priority, 95); +} +END_TEST + +/* Histogram Options */ + +START_TEST(test_bucket_size_short) +{ + PARSE_ARGS("osnoise", "hist", "-b", "2"); + + ck_assert_int_eq(params->hist.bucket_size, 2); +} +END_TEST + +START_TEST(test_bucket_size_long) +{ + PARSE_ARGS("osnoise", "hist", "--bucket-size", "2"); + + ck_assert_int_eq(params->hist.bucket_size, 2); +} +END_TEST + +START_TEST(test_entries_short) +{ + PARSE_ARGS("osnoise", "hist", "-E", "512"); + + ck_assert_int_eq(params->hist.entries, 512); +} +END_TEST + +START_TEST(test_entries_long) +{ + PARSE_ARGS("osnoise", "hist", "--entries", "512"); + + ck_assert_int_eq(params->hist.entries, 512); +} +END_TEST + +START_TEST(test_no_header) +{ + PARSE_ARGS("osnoise", "hist", "--no-header"); + + ck_assert(params->hist.no_header); +} +END_TEST + +START_TEST(test_no_index) +{ + PARSE_ARGS("osnoise", "hist", "--with-zeros", "--no-index"); + + ck_assert(params->hist.no_index); +} +END_TEST + +START_TEST(test_no_summary) +{ + PARSE_ARGS("osnoise", "hist", "--no-summary"); + + ck_assert(params->hist.no_summary); +} +END_TEST + +START_TEST(test_with_zeros) +{ + PARSE_ARGS("osnoise", "hist", "--with-zeros"); + + ck_assert(params->hist.with_zeros); +} +END_TEST + +/* System Tuning */ + +START_TEST(test_trace_buffer_size) +{ + PARSE_ARGS("osnoise", "hist", "--trace-buffer-size", "200"); + + ck_assert_int_eq(params->buffer_size, 200); +} +END_TEST + +START_TEST(test_warm_up) +{ + PARSE_ARGS("osnoise", "hist", "--warm-up", "5"); + + ck_assert_int_eq(params->warmup, 5); +} +END_TEST + +/* Auto Analysis and Actions */ + +START_TEST(test_auto) +{ + PARSE_ARGS("osnoise", "hist", "-a", "20"); + + CLI_OSNOISE_ASSERT_AUTO(20); +} +END_TEST + +START_TEST(test_on_end) +{ + PARSE_ARGS("osnoise", "hist", "--on-end", "trace"); + + CLI_ASSERT_SINGLE_ACTION(end_actions, ACTION_TRACE_OUTPUT, trace_output, = str, + "osnoise_trace.txt"); +} +END_TEST + +START_TEST(test_on_threshold) +{ + PARSE_ARGS("osnoise", "hist", "--on-threshold", "trace"); + + CLI_ASSERT_SINGLE_ACTION(threshold_actions, ACTION_TRACE_OUTPUT, trace_ou= tput, str, + "osnoise_trace.txt"); +} +END_TEST + +/* General */ + +START_TEST(test_debug_short) +{ + PARSE_ARGS("osnoise", "hist", "-D"); + + ck_assert(config_debug); +} +END_TEST + +START_TEST(test_debug_long) +{ + PARSE_ARGS("osnoise", "hist", "--debug"); + + ck_assert(config_debug); +} +END_TEST + +START_TEST(test_duration_short) +{ + PARSE_ARGS("osnoise", "hist", "-d", "1m"); + + ck_assert_int_eq(params->duration, 60); +} +END_TEST + +START_TEST(test_duration_long) +{ + PARSE_ARGS("osnoise", "hist", "--duration", "1m"); + + ck_assert_int_eq(params->duration, 60); +} +END_TEST + +Suite *osnoise_hist_cli_suite(void) +{ + Suite *s =3D suite_create("osnoise_hist_cli"); + TCase *tc; + + tc =3D tcase_create("tracing_options"); + tcase_add_test(tc, test_period_short); + tcase_add_test(tc, test_period_long); + tcase_add_test(tc, test_runtime_short); + tcase_add_test(tc, test_runtime_long); + tcase_add_test(tc, test_stop_short); + tcase_add_test(tc, test_stop_long); + tcase_add_test(tc, test_stop_total_short); + tcase_add_test(tc, test_stop_total_long); + tcase_add_test(tc, test_threshold_short); + tcase_add_test(tc, test_threshold_long); + tcase_add_test(tc, test_trace_short_noarg); + tcase_add_test(tc, test_trace_short_followarg); + tcase_add_test(tc, test_trace_short_space); + tcase_add_test(tc, test_trace_short_equals); + tcase_add_test(tc, test_trace_long_noarg); + tcase_add_test(tc, test_trace_long_followarg); + tcase_add_test(tc, test_trace_long_space); + tcase_add_test(tc, test_trace_long_equals); + suite_add_tcase(s, tc); + + tc =3D tcase_create("event_configuration"); + tcase_add_test(tc, test_event_short); + tcase_add_test(tc, test_event_long); + tcase_add_test(tc, test_filter); + tcase_add_test(tc, test_trigger); + suite_add_tcase(s, tc); + + tc =3D tcase_create("cpu_configuration"); + tcase_add_test(tc, test_cpus_short); + tcase_add_test(tc, test_cpus_long); + tcase_add_test(tc, test_housekeeping_short); + tcase_add_test(tc, test_housekeeping_long); + suite_add_tcase(s, tc); + + tc =3D tcase_create("thread_configuration"); + tcase_add_test(tc, test_cgroup_short_noarg); + tcase_add_test(tc, test_cgroup_short_space); + tcase_add_test(tc, test_cgroup_short_equals); + tcase_add_test(tc, test_cgroup_long_noarg); + tcase_add_test(tc, test_cgroup_long_space); + tcase_add_test(tc, test_cgroup_long_equals); + tcase_add_test(tc, test_priority_short); + tcase_add_test(tc, test_priority_long); + suite_add_tcase(s, tc); + + tc =3D tcase_create("histogram_options"); + tcase_add_test(tc, test_bucket_size_short); + tcase_add_test(tc, test_bucket_size_long); + tcase_add_test(tc, test_entries_short); + tcase_add_test(tc, test_entries_long); + tcase_add_test(tc, test_no_header); + tcase_add_test(tc, test_no_index); + tcase_add_test(tc, test_no_summary); + tcase_add_test(tc, test_with_zeros); + suite_add_tcase(s, tc); + + tc =3D tcase_create("system_tuning"); + tcase_add_test(tc, test_trace_buffer_size); + tcase_add_test(tc, test_warm_up); + suite_add_tcase(s, tc); + + tc =3D tcase_create("aa_actions"); + tcase_add_test(tc, test_auto); + tcase_add_test(tc, test_on_end); + tcase_add_test(tc, test_on_threshold); + suite_add_tcase(s, tc); + + tc =3D tcase_create("general"); + tcase_add_test(tc, test_debug_short); + tcase_add_test(tc, test_debug_long); + tcase_add_test(tc, test_duration_short); + tcase_add_test(tc, test_duration_long); + suite_add_tcase(s, tc); + + return s; +} diff --git a/tools/tracing/rtla/tests/unit/osnoise_top_cli.c b/tools/tracin= g/rtla/tests/unit/osnoise_top_cli.c new file mode 100644 index 000000000000..f3a8633cc84e --- /dev/null +++ b/tools/tracing/rtla/tests/unit/osnoise_top_cli.c @@ -0,0 +1,503 @@ +// SPDX-License-Identifier: GPL-2.0 + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include + +#include "cli_params_assert.h" +#include "../../src/cli.h" + +#define PARSE_ARGS(...) char *argv[] =3D { __VA_ARGS__, NULL };\ + int argc =3D sizeof(argv) / sizeof(char *) - 1;\ + struct common_params *params =3D\ + osnoise_top_parse_args(argc, argv);\ + struct osnoise_params *osn_params __maybe_unused =3D\ + to_osnoise_params(params) + +/* Tracing Options */ + +START_TEST(test_period_short) +{ + PARSE_ARGS("osnoise", "top", "-p", "100000"); + + ck_assert_int_eq(osn_params->period, 100000); +} +END_TEST + +START_TEST(test_period_long) +{ + PARSE_ARGS("osnoise", "top", "--period", "100000"); + + ck_assert_int_eq(osn_params->period, 100000); +} +END_TEST + +START_TEST(test_runtime_short) +{ + PARSE_ARGS("osnoise", "top", "-r", "95000"); + + ck_assert_int_eq(osn_params->runtime, 95000); +} +END_TEST + +START_TEST(test_runtime_long) +{ + PARSE_ARGS("osnoise", "top", "--runtime", "95000"); + + ck_assert_int_eq(osn_params->runtime, 95000); +} +END_TEST + +START_TEST(test_stop_short) +{ + PARSE_ARGS("osnoise", "top", "-s", "20"); + + ck_assert_int_eq(params->stop_us, 20); +} +END_TEST + +START_TEST(test_stop_long) +{ + PARSE_ARGS("osnoise", "top", "--stop", "20"); + + ck_assert_int_eq(params->stop_us, 20); +} +END_TEST + +START_TEST(test_stop_total_short) +{ + PARSE_ARGS("osnoise", "top", "-S", "20"); + + ck_assert_int_eq(params->stop_total_us, 20); +} +END_TEST + +START_TEST(test_stop_total_long) +{ + PARSE_ARGS("osnoise", "top", "--stop-total", "20"); + + ck_assert_int_eq(params->stop_total_us, 20); +} +END_TEST + +START_TEST(test_threshold_short) +{ + PARSE_ARGS("osnoise", "top", "-T", "5"); + + ck_assert_int_eq(osn_params->threshold, 5); +} +END_TEST + +START_TEST(test_threshold_long) +{ + PARSE_ARGS("osnoise", "top", "--threshold", "5"); + + ck_assert_int_eq(osn_params->threshold, 5); +} +END_TEST + +START_TEST(test_trace_short_noarg) +{ + PARSE_ARGS("osnoise", "top", "-t"); + + CLI_ASSERT_SINGLE_ACTION(threshold_actions, ACTION_TRACE_OUTPUT, trace_ou= tput, str, + "osnoise_trace.txt"); +} +END_TEST + +START_TEST(test_trace_short_followarg) +{ + PARSE_ARGS("osnoise", "top", "-t", "-d", "20"); + + CLI_ASSERT_SINGLE_ACTION(threshold_actions, ACTION_TRACE_OUTPUT, trace_ou= tput, str, + "osnoise_trace.txt"); + ck_assert_int_eq(params->duration, 20); /* check if next argument is read= correctly */ +} +END_TEST + +START_TEST(test_trace_short_space) +{ + PARSE_ARGS("osnoise", "top", "-t", "tracefile"); + + CLI_ASSERT_SINGLE_ACTION(threshold_actions, ACTION_TRACE_OUTPUT, trace_ou= tput, str, + "tracefile"); +} +END_TEST + +START_TEST(test_trace_short_equals) +{ + PARSE_ARGS("osnoise", "top", "-t=3Dtracefile"); + + CLI_ASSERT_SINGLE_ACTION(threshold_actions, ACTION_TRACE_OUTPUT, trace_ou= tput, str, + "tracefile"); +} +END_TEST + +START_TEST(test_trace_long_noarg) +{ + PARSE_ARGS("osnoise", "top", "--trace"); + + CLI_ASSERT_SINGLE_ACTION(threshold_actions, ACTION_TRACE_OUTPUT, trace_ou= tput, str, + "osnoise_trace.txt"); +} +END_TEST + +START_TEST(test_trace_long_followarg) +{ + PARSE_ARGS("osnoise", "top", "--trace", "-d", "20"); + + CLI_ASSERT_SINGLE_ACTION(threshold_actions, ACTION_TRACE_OUTPUT, trace_ou= tput, str, + "osnoise_trace.txt"); + ck_assert_int_eq(params->duration, 20); /* check if next argument is read= correctly */ +} +END_TEST + +START_TEST(test_trace_long_space) +{ + PARSE_ARGS("osnoise", "top", "--trace", "tracefile"); + + CLI_ASSERT_SINGLE_ACTION(threshold_actions, ACTION_TRACE_OUTPUT, trace_ou= tput, str, + "tracefile"); +} +END_TEST + +START_TEST(test_trace_long_equals) +{ + PARSE_ARGS("osnoise", "top", "--trace=3Dtracefile"); + + CLI_ASSERT_SINGLE_ACTION(threshold_actions, ACTION_TRACE_OUTPUT, trace_ou= tput, str, + "tracefile"); +} +END_TEST + +/* Event Configuration */ + +START_TEST(test_event_short) +{ + PARSE_ARGS("osnoise", "top", "-e", "system:event"); + + CLI_ASSERT_SINGLE_EVENT("system", "event"); +} +END_TEST + +START_TEST(test_event_long) +{ + PARSE_ARGS("osnoise", "top", "--event", "system:event"); + + CLI_ASSERT_SINGLE_EVENT("system", "event"); +} +END_TEST + +START_TEST(test_filter) +{ + PARSE_ARGS("osnoise", "top", "-e", "system:event", "--filter", "filter"); + + CLI_ASSERT_SINGLE_FILTER("filter"); +} +END_TEST + +START_TEST(test_trigger) +{ + PARSE_ARGS("osnoise", "top", "-e", "system:event", "--trigger", "trigger"= ); + + CLI_ASSERT_SINGLE_TRIGGER("trigger"); +} +END_TEST + +/* CPU Configuration */ + +START_TEST(test_cpus_short) +{ + nr_cpus =3D 4; + + PARSE_ARGS("osnoise", "top", "-c", "0-1,3"); + + ck_assert_str_eq(params->cpus, "0-1,3"); + CLI_ASSERT_CPUSET(monitored_cpus, 0, 1, 3); +} +END_TEST + +START_TEST(test_cpus_long) +{ + nr_cpus =3D 4; + + PARSE_ARGS("osnoise", "top", "--cpus", "0-1,3"); + + ck_assert_str_eq(params->cpus, "0-1,3"); + CLI_ASSERT_CPUSET(monitored_cpus, 0, 1, 3); +} +END_TEST + +START_TEST(test_housekeeping_short) +{ + nr_cpus =3D 4; + + PARSE_ARGS("osnoise", "top", "-H", "0-1,3"); + + CLI_ASSERT_CPUSET(hk_cpu_set, 0, 1, 3); +} +END_TEST + +START_TEST(test_housekeeping_long) +{ + nr_cpus =3D 4; + + PARSE_ARGS("osnoise", "top", "--house-keeping", "0-1,3"); + + CLI_ASSERT_CPUSET(hk_cpu_set, 0, 1, 3); +} +END_TEST + +/* Thread Configuration */ + +START_TEST(test_cgroup_short_noarg) +{ + PARSE_ARGS("osnoise", "top", "-C"); + + ck_assert(params->cgroup); + ck_assert_ptr_null(params->cgroup_name); +} +END_TEST + +START_TEST(test_cgroup_short_space) +{ + PARSE_ARGS("osnoise", "top", "-C", "cgroup"); + + ck_assert(params->cgroup); + ck_assert_str_eq(params->cgroup_name, "cgroup"); +} +END_TEST + +START_TEST(test_cgroup_short_equals) +{ + PARSE_ARGS("osnoise", "top", "-C=3Dcgroup"); + + ck_assert(params->cgroup); + ck_assert_str_eq(params->cgroup_name, "cgroup"); +} +END_TEST + +START_TEST(test_cgroup_long_noarg) +{ + PARSE_ARGS("osnoise", "top", "--cgroup"); + + ck_assert(params->cgroup); + ck_assert_ptr_null(params->cgroup_name); +} +END_TEST + +START_TEST(test_cgroup_long_space) +{ + PARSE_ARGS("osnoise", "top", "--cgroup", "cgroup"); + + ck_assert(params->cgroup); + ck_assert_str_eq(params->cgroup_name, "cgroup"); +} +END_TEST + +START_TEST(test_cgroup_long_equals) +{ + PARSE_ARGS("osnoise", "top", "--cgroup=3Dcgroup"); + + ck_assert(params->cgroup); + ck_assert_str_eq(params->cgroup_name, "cgroup"); +} +END_TEST + +START_TEST(test_priority_short) +{ + PARSE_ARGS("osnoise", "top", "-P", "f:95"); + + ck_assert_int_eq(params->sched_param.sched_policy, SCHED_FIFO); + ck_assert_int_eq(params->sched_param.sched_priority, 95); +} +END_TEST + +START_TEST(test_priority_long) +{ + PARSE_ARGS("osnoise", "top", "--priority", "f:95"); + + ck_assert_int_eq(params->sched_param.sched_policy, SCHED_FIFO); + ck_assert_int_eq(params->sched_param.sched_priority, 95); +} +END_TEST + +/* Output */ + +START_TEST(test_quiet_short) +{ + PARSE_ARGS("osnoise", "top", "-q"); + + ck_assert(params->quiet); +} +END_TEST + +START_TEST(test_quiet_long) +{ + PARSE_ARGS("osnoise", "top", "--quiet"); + + ck_assert(params->quiet); +} +END_TEST + +/* Auto Analysis and Actions */ + +START_TEST(test_auto) +{ + PARSE_ARGS("osnoise", "top", "-a", "20"); + + CLI_OSNOISE_ASSERT_AUTO(20); +} +END_TEST + +START_TEST(test_on_end) +{ + PARSE_ARGS("osnoise", "top", "--on-end", "trace"); + + CLI_ASSERT_SINGLE_ACTION(end_actions, ACTION_TRACE_OUTPUT, trace_output, = str, + "osnoise_trace.txt"); +} +END_TEST + +START_TEST(test_on_threshold) +{ + PARSE_ARGS("osnoise", "top", "--on-threshold", "trace"); + + CLI_ASSERT_SINGLE_ACTION(threshold_actions, ACTION_TRACE_OUTPUT, trace_ou= tput, str, + "osnoise_trace.txt"); +} +END_TEST + +/* System Tuning */ + +START_TEST(test_trace_buffer_size) +{ + PARSE_ARGS("osnoise", "top", "--trace-buffer-size", "200"); + + ck_assert_int_eq(params->buffer_size, 200); +} +END_TEST + +START_TEST(test_warm_up) +{ + PARSE_ARGS("osnoise", "top", "--warm-up", "5"); + + ck_assert_int_eq(params->warmup, 5); +} +END_TEST + +/* General */ + +START_TEST(test_debug_short) +{ + PARSE_ARGS("osnoise", "top", "-D"); + + ck_assert(config_debug); +} +END_TEST + +START_TEST(test_debug_long) +{ + PARSE_ARGS("osnoise", "top", "--debug"); + + ck_assert(config_debug); +} +END_TEST + +START_TEST(test_duration_short) +{ + PARSE_ARGS("osnoise", "top", "-d", "1m"); + + ck_assert_int_eq(params->duration, 60); +} +END_TEST + +START_TEST(test_duration_long) +{ + PARSE_ARGS("osnoise", "top", "--duration", "1m"); + + ck_assert_int_eq(params->duration, 60); +} +END_TEST + +Suite *osnoise_top_cli_suite(void) +{ + Suite *s =3D suite_create("osnoise_top_cli"); + TCase *tc; + + tc =3D tcase_create("tracing_options"); + tcase_add_test(tc, test_period_short); + tcase_add_test(tc, test_period_long); + tcase_add_test(tc, test_runtime_short); + tcase_add_test(tc, test_runtime_long); + tcase_add_test(tc, test_stop_short); + tcase_add_test(tc, test_stop_long); + tcase_add_test(tc, test_stop_total_short); + tcase_add_test(tc, test_stop_total_long); + tcase_add_test(tc, test_threshold_short); + tcase_add_test(tc, test_threshold_long); + tcase_add_test(tc, test_trace_short_noarg); + tcase_add_test(tc, test_trace_short_followarg); + tcase_add_test(tc, test_trace_short_space); + tcase_add_test(tc, test_trace_short_equals); + tcase_add_test(tc, test_trace_long_noarg); + tcase_add_test(tc, test_trace_long_followarg); + tcase_add_test(tc, test_trace_long_space); + tcase_add_test(tc, test_trace_long_equals); + suite_add_tcase(s, tc); + + tc =3D tcase_create("event_configuration"); + tcase_add_test(tc, test_event_short); + tcase_add_test(tc, test_event_long); + tcase_add_test(tc, test_filter); + tcase_add_test(tc, test_trigger); + suite_add_tcase(s, tc); + + tc =3D tcase_create("cpu_configuration"); + tcase_add_test(tc, test_cpus_short); + tcase_add_test(tc, test_cpus_long); + tcase_add_test(tc, test_housekeeping_short); + tcase_add_test(tc, test_housekeeping_long); + suite_add_tcase(s, tc); + + tc =3D tcase_create("thread_configuration"); + tcase_add_test(tc, test_cgroup_short_noarg); + tcase_add_test(tc, test_cgroup_short_space); + tcase_add_test(tc, test_cgroup_short_equals); + tcase_add_test(tc, test_cgroup_long_noarg); + tcase_add_test(tc, test_cgroup_long_space); + tcase_add_test(tc, test_cgroup_long_equals); + tcase_add_test(tc, test_priority_short); + tcase_add_test(tc, test_priority_long); + suite_add_tcase(s, tc); + + tc =3D tcase_create("output"); + tcase_add_test(tc, test_quiet_short); + tcase_add_test(tc, test_quiet_long); + suite_add_tcase(s, tc); + + tc =3D tcase_create("system_tuning"); + tcase_add_test(tc, test_trace_buffer_size); + tcase_add_test(tc, test_warm_up); + suite_add_tcase(s, tc); + + tc =3D tcase_create("aa_actions"); + tcase_add_test(tc, test_auto); + tcase_add_test(tc, test_on_end); + tcase_add_test(tc, test_on_threshold); + suite_add_tcase(s, tc); + + tc =3D tcase_create("general"); + tcase_add_test(tc, test_debug_short); + tcase_add_test(tc, test_debug_long); + tcase_add_test(tc, test_duration_short); + tcase_add_test(tc, test_duration_long); + suite_add_tcase(s, tc); + + return s; +} diff --git a/tools/tracing/rtla/tests/unit/timerlat_hist_cli.c b/tools/trac= ing/rtla/tests/unit/timerlat_hist_cli.c new file mode 100644 index 000000000000..81dc04596cd1 --- /dev/null +++ b/tools/tracing/rtla/tests/unit/timerlat_hist_cli.c @@ -0,0 +1,702 @@ +// SPDX-License-Identifier: GPL-2.0 + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "cli_params_assert.h" +#include "../../src/cli.h" + +#define PARSE_ARGS(...) char *argv[] =3D { __VA_ARGS__, NULL };\ + int argc =3D sizeof(argv) / sizeof(char *) - 1;\ + struct common_params *params =3D\ + timerlat_hist_parse_args(argc, argv);\ + struct timerlat_params *tlat_params __maybe_unused =3D\ + to_timerlat_params(params) + +/* Tracing Options */ + +START_TEST(test_irq_short) +{ + PARSE_ARGS("timerlat", "hist", "-i", "20"); + + ck_assert_int_eq(params->stop_us, 20); +} +END_TEST + +START_TEST(test_irq_long) +{ + PARSE_ARGS("timerlat", "hist", "--irq", "20"); + + ck_assert_int_eq(params->stop_us, 20); +} +END_TEST + +START_TEST(test_period_short) +{ + PARSE_ARGS("timerlat", "hist", "-p", "200"); + + ck_assert_int_eq(tlat_params->timerlat_period_us, 200); +} +END_TEST + +START_TEST(test_period_long) +{ + PARSE_ARGS("timerlat", "hist", "--period", "200"); + + ck_assert_int_eq(tlat_params->timerlat_period_us, 200); +} +END_TEST + +START_TEST(test_stack_short) +{ + PARSE_ARGS("timerlat", "hist", "-s", "20"); + + ck_assert_int_eq(tlat_params->print_stack, 20); +} +END_TEST + +START_TEST(test_stack_long) +{ + PARSE_ARGS("timerlat", "hist", "--stack", "20"); + + ck_assert_int_eq(tlat_params->print_stack, 20); +} +END_TEST + +START_TEST(test_thread_short) +{ + PARSE_ARGS("timerlat", "hist", "-T", "20"); + + ck_assert_int_eq(params->stop_total_us, 20); +} +END_TEST + +START_TEST(test_thread_long) +{ + PARSE_ARGS("timerlat", "hist", "--thread", "20"); + + ck_assert_int_eq(params->stop_total_us, 20); +} +END_TEST + +START_TEST(test_trace_short_noarg) +{ + PARSE_ARGS("timerlat", "hist", "-t"); + + CLI_ASSERT_SINGLE_ACTION(threshold_actions, ACTION_TRACE_OUTPUT, trace_ou= tput, str, + "timerlat_trace.txt"); +} +END_TEST + +START_TEST(test_trace_short_followarg) +{ + PARSE_ARGS("timerlat", "hist", "-t", "-d", "20"); + + CLI_ASSERT_SINGLE_ACTION(threshold_actions, ACTION_TRACE_OUTPUT, trace_ou= tput, str, + "timerlat_trace.txt"); + ck_assert_int_eq(params->duration, 20); /* check if next argument is read= correctly */ +} +END_TEST + +START_TEST(test_trace_short_space) +{ + PARSE_ARGS("timerlat", "hist", "-t", "tracefile"); + + CLI_ASSERT_SINGLE_ACTION(threshold_actions, ACTION_TRACE_OUTPUT, trace_ou= tput, str, + "tracefile"); +} +END_TEST + +START_TEST(test_trace_short_equals) +{ + PARSE_ARGS("timerlat", "hist", "-t=3Dtracefile"); + + CLI_ASSERT_SINGLE_ACTION(threshold_actions, ACTION_TRACE_OUTPUT, trace_ou= tput, str, + "tracefile"); +} +END_TEST + +START_TEST(test_trace_long_noarg) +{ + PARSE_ARGS("timerlat", "hist", "--trace"); + + CLI_ASSERT_SINGLE_ACTION(threshold_actions, ACTION_TRACE_OUTPUT, trace_ou= tput, str, + "timerlat_trace.txt"); +} +END_TEST + +START_TEST(test_trace_long_followarg) +{ + PARSE_ARGS("timerlat", "hist", "--trace", "-d", "20"); + + CLI_ASSERT_SINGLE_ACTION(threshold_actions, ACTION_TRACE_OUTPUT, trace_ou= tput, str, + "timerlat_trace.txt"); + ck_assert_int_eq(params->duration, 20); /* check if next argument is read= correctly */ +} +END_TEST + +START_TEST(test_trace_long_space) +{ + PARSE_ARGS("timerlat", "hist", "--trace", "tracefile"); + + CLI_ASSERT_SINGLE_ACTION(threshold_actions, ACTION_TRACE_OUTPUT, trace_ou= tput, str, + "tracefile"); +} +END_TEST + +START_TEST(test_trace_long_equals) +{ + PARSE_ARGS("timerlat", "hist", "--trace=3Dtracefile"); + + CLI_ASSERT_SINGLE_ACTION(threshold_actions, ACTION_TRACE_OUTPUT, trace_ou= tput, str, + "tracefile"); +} +END_TEST + +/* Event Configuration */ + +START_TEST(test_event_short) +{ + PARSE_ARGS("timerlat", "hist", "-e", "system:event"); + + CLI_ASSERT_SINGLE_EVENT("system", "event"); +} +END_TEST + +START_TEST(test_event_long) +{ + PARSE_ARGS("timerlat", "hist", "--event", "system:event"); + + CLI_ASSERT_SINGLE_EVENT("system", "event"); +} +END_TEST + +START_TEST(test_filter) +{ + PARSE_ARGS("timerlat", "hist", "-e", "system:event", "--filter", "filter"= ); + + CLI_ASSERT_SINGLE_FILTER("filter"); +} +END_TEST + +START_TEST(test_trigger) +{ + PARSE_ARGS("timerlat", "hist", "-e", "system:event", "--trigger", "trigge= r"); + + CLI_ASSERT_SINGLE_TRIGGER("trigger"); +} +END_TEST + +/* CPU Configuration */ + +START_TEST(test_cpus_short) +{ + nr_cpus =3D 4; + + PARSE_ARGS("timerlat", "hist", "-c", "0-1,3"); + + ck_assert_str_eq(params->cpus, "0-1,3"); + CLI_ASSERT_CPUSET(monitored_cpus, 0, 1, 3); +} +END_TEST + +START_TEST(test_cpus_long) +{ + nr_cpus =3D 4; + + PARSE_ARGS("timerlat", "hist", "--cpus", "0-1,3"); + + ck_assert_str_eq(params->cpus, "0-1,3"); + CLI_ASSERT_CPUSET(monitored_cpus, 0, 1, 3); +} +END_TEST + +START_TEST(test_housekeeping_short) +{ + nr_cpus =3D 4; + + PARSE_ARGS("timerlat", "hist", "-H", "0-1,3"); + + CLI_ASSERT_CPUSET(hk_cpu_set, 0, 1, 3); +} +END_TEST + +START_TEST(test_housekeeping_long) +{ + nr_cpus =3D 4; + + PARSE_ARGS("timerlat", "hist", "--house-keeping", "0-1,3"); + + CLI_ASSERT_CPUSET(hk_cpu_set, 0, 1, 3); +} +END_TEST + +/* Thread Configuration */ + +START_TEST(test_cgroup_short_noarg) +{ + PARSE_ARGS("timerlat", "hist", "-C"); + + ck_assert(params->cgroup); + ck_assert_ptr_null(params->cgroup_name); +} +END_TEST + +START_TEST(test_cgroup_short_space) +{ + PARSE_ARGS("timerlat", "hist", "-C", "cgroup"); + + ck_assert(params->cgroup); + ck_assert_str_eq(params->cgroup_name, "cgroup"); +} +END_TEST + +START_TEST(test_cgroup_short_equals) +{ + PARSE_ARGS("timerlat", "hist", "-C=3Dcgroup"); + + ck_assert(params->cgroup); + ck_assert_str_eq(params->cgroup_name, "cgroup"); +} +END_TEST + +START_TEST(test_cgroup_long_noarg) +{ + PARSE_ARGS("timerlat", "hist", "--cgroup"); + + ck_assert(params->cgroup); + ck_assert_ptr_null(params->cgroup_name); +} +END_TEST + +START_TEST(test_cgroup_long_space) +{ + PARSE_ARGS("timerlat", "hist", "--cgroup", "cgroup"); + + ck_assert(params->cgroup); + ck_assert_str_eq(params->cgroup_name, "cgroup"); +} +END_TEST + +START_TEST(test_cgroup_long_equals) +{ + PARSE_ARGS("timerlat", "hist", "--cgroup=3Dcgroup"); + + ck_assert(params->cgroup); + ck_assert_str_eq(params->cgroup_name, "cgroup"); +} +END_TEST + +START_TEST(test_kernel_threads_short) +{ + PARSE_ARGS("timerlat", "hist", "-k"); + + ck_assert(params->kernel_workload); + ck_assert(!params->user_workload); + ck_assert(!params->user_data); +} +END_TEST + +START_TEST(test_kernel_threads_long) +{ + PARSE_ARGS("timerlat", "hist", "--kernel-threads"); + + ck_assert(params->kernel_workload); + ck_assert(!params->user_workload); + ck_assert(!params->user_data); +} +END_TEST + +START_TEST(test_priority_short) +{ + PARSE_ARGS("timerlat", "hist", "-P", "f:95"); + + ck_assert_int_eq(params->sched_param.sched_policy, SCHED_FIFO); + ck_assert_int_eq(params->sched_param.sched_priority, 95); +} +END_TEST + +START_TEST(test_priority_long) +{ + PARSE_ARGS("timerlat", "hist", "--priority", "f:95"); + + ck_assert_int_eq(params->sched_param.sched_policy, SCHED_FIFO); + ck_assert_int_eq(params->sched_param.sched_priority, 95); +} +END_TEST + +START_TEST(test_user_load_short) +{ + PARSE_ARGS("timerlat", "hist", "-U"); + + ck_assert(!params->kernel_workload); + ck_assert(!params->user_workload); + ck_assert(params->user_data); +} +END_TEST + +START_TEST(test_user_load_long) +{ + PARSE_ARGS("timerlat", "hist", "--user-load"); + + ck_assert(!params->kernel_workload); + ck_assert(!params->user_workload); + ck_assert(params->user_data); +} +END_TEST + +START_TEST(test_user_threads_short) +{ + PARSE_ARGS("timerlat", "hist", "-u"); + + ck_assert(!params->kernel_workload); + ck_assert(params->user_workload); + ck_assert(params->user_data); +} +END_TEST + +START_TEST(test_user_threads_long) +{ + PARSE_ARGS("timerlat", "hist", "--user-threads"); + + ck_assert(!params->kernel_workload); + ck_assert(params->user_workload); + ck_assert(params->user_data); +} +END_TEST + +/* Histogram Options */ + +START_TEST(test_bucket_size_short) +{ + PARSE_ARGS("timerlat", "hist", "-b", "2"); + + ck_assert_int_eq(params->hist.bucket_size, 2); +} +END_TEST + +START_TEST(test_bucket_size_long) +{ + PARSE_ARGS("timerlat", "hist", "--bucket-size", "2"); + + ck_assert_int_eq(params->hist.bucket_size, 2); +} +END_TEST + +START_TEST(test_entries_short) +{ + PARSE_ARGS("timerlat", "hist", "-E", "512"); + + ck_assert_int_eq(params->hist.entries, 512); +} +END_TEST + +START_TEST(test_entries_long) +{ + PARSE_ARGS("timerlat", "hist", "--entries", "512"); + + ck_assert_int_eq(params->hist.entries, 512); +} +END_TEST + +START_TEST(test_no_header) +{ + PARSE_ARGS("timerlat", "hist", "--no-header"); + + ck_assert(params->hist.no_header); +} +END_TEST + +START_TEST(test_no_index) +{ + PARSE_ARGS("timerlat", "hist", "--with-zeros", "--no-index"); + + ck_assert(params->hist.no_index); +} +END_TEST + +START_TEST(test_no_irq) +{ + PARSE_ARGS("timerlat", "hist", "--no-irq"); + + ck_assert(params->hist.no_irq); +} +END_TEST + +START_TEST(test_no_summary) +{ + PARSE_ARGS("timerlat", "hist", "--no-summary"); + + ck_assert(params->hist.no_summary); +} +END_TEST + +START_TEST(test_no_thread) +{ + PARSE_ARGS("timerlat", "hist", "--no-thread"); + + ck_assert(params->hist.no_thread); +} +END_TEST + +START_TEST(test_with_zeros) +{ + PARSE_ARGS("timerlat", "hist", "--with-zeros"); + + ck_assert(params->hist.with_zeros); +} +END_TEST + +/* Output */ + +START_TEST(test_nano_short) +{ + PARSE_ARGS("timerlat", "hist", "-n"); + + ck_assert_int_eq(params->output_divisor, 1); +} +END_TEST + +START_TEST(test_nano_long) +{ + PARSE_ARGS("timerlat", "hist", "--nano"); + + ck_assert_int_eq(params->output_divisor, 1); +} +END_TEST + +/* System Tuning */ + +START_TEST(test_deepest_idle_state) +{ + PARSE_ARGS("timerlat", "hist", "--deepest-idle-state", "1"); + + ck_assert_int_eq(tlat_params->deepest_idle_state, 1); +} +END_TEST + +START_TEST(test_dma_latency) +{ + PARSE_ARGS("timerlat", "hist", "--dma-latency", "10"); + + ck_assert_int_eq(tlat_params->dma_latency, 10); +} +END_TEST + +START_TEST(test_trace_buffer_size) +{ + PARSE_ARGS("timerlat", "hist", "--trace-buffer-size", "200"); + + ck_assert_int_eq(params->buffer_size, 200); +} +END_TEST + +START_TEST(test_warm_up) +{ + PARSE_ARGS("timerlat", "hist", "--warm-up", "5"); + + ck_assert_int_eq(params->warmup, 5); +} +END_TEST + +/* Auto Analysis and Actions */ + +START_TEST(test_auto) +{ + PARSE_ARGS("timerlat", "hist", "-a", "20"); + + CLI_TIMERLAT_ASSERT_AUTO(20); +} +END_TEST + +START_TEST(test_bpf_action) +{ + PARSE_ARGS("timerlat", "hist", "--bpf-action", "program"); + + ck_assert_str_eq(tlat_params->bpf_action_program, "program"); +} +END_TEST + +START_TEST(test_dump_tasks) +{ + PARSE_ARGS("timerlat", "hist", "--dump-tasks"); + + ck_assert(tlat_params->dump_tasks); +} +END_TEST + +START_TEST(test_no_aa) +{ + PARSE_ARGS("timerlat", "hist", "--no-aa"); + + ck_assert(tlat_params->no_aa); +} +END_TEST + +START_TEST(test_on_end) +{ + PARSE_ARGS("timerlat", "hist", "--on-end", "trace"); + + CLI_ASSERT_SINGLE_ACTION(end_actions, ACTION_TRACE_OUTPUT, trace_output, = str, + "timerlat_trace.txt"); +} +END_TEST + +START_TEST(test_on_threshold) +{ + PARSE_ARGS("timerlat", "hist", "--on-threshold", "trace"); + + CLI_ASSERT_SINGLE_ACTION(threshold_actions, ACTION_TRACE_OUTPUT, trace_ou= tput, str, + "timerlat_trace.txt"); +} +END_TEST + +START_TEST(test_stack_format) +{ + PARSE_ARGS("timerlat", "hist", "--stack-format", "truncate"); + + ck_assert_int_eq(tlat_params->stack_format, STACK_FORMAT_TRUNCATE); +} +END_TEST + +/* General */ + +START_TEST(test_debug_short) +{ + PARSE_ARGS("timerlat", "hist", "-D"); + + ck_assert(config_debug); +} +END_TEST + +START_TEST(test_debug_long) +{ + PARSE_ARGS("timerlat", "hist", "--debug"); + + ck_assert(config_debug); +} +END_TEST + +START_TEST(test_duration_short) +{ + PARSE_ARGS("timerlat", "hist", "-d", "1m"); + + ck_assert_int_eq(params->duration, 60); +} +END_TEST + +START_TEST(test_duration_long) +{ + PARSE_ARGS("timerlat", "hist", "--duration", "1m"); + + ck_assert_int_eq(params->duration, 60); +} +END_TEST + +Suite *timerlat_hist_cli_suite(void) +{ + Suite *s =3D suite_create("timerlat_hist_cli"); + TCase *tc; + + tc =3D tcase_create("tracing_options"); + tcase_add_test(tc, test_irq_short); + tcase_add_test(tc, test_irq_long); + tcase_add_test(tc, test_period_short); + tcase_add_test(tc, test_period_long); + tcase_add_test(tc, test_stack_short); + tcase_add_test(tc, test_stack_long); + tcase_add_test(tc, test_thread_short); + tcase_add_test(tc, test_thread_long); + tcase_add_test(tc, test_trace_short_noarg); + tcase_add_test(tc, test_trace_short_followarg); + tcase_add_test(tc, test_trace_short_space); + tcase_add_test(tc, test_trace_short_equals); + tcase_add_test(tc, test_trace_long_noarg); + tcase_add_test(tc, test_trace_long_followarg); + tcase_add_test(tc, test_trace_long_space); + tcase_add_test(tc, test_trace_long_equals); + suite_add_tcase(s, tc); + + tc =3D tcase_create("event_configuration"); + tcase_add_test(tc, test_event_short); + tcase_add_test(tc, test_event_long); + tcase_add_test(tc, test_filter); + tcase_add_test(tc, test_trigger); + suite_add_tcase(s, tc); + + tc =3D tcase_create("cpu_configuration"); + tcase_add_test(tc, test_cpus_short); + tcase_add_test(tc, test_cpus_long); + tcase_add_test(tc, test_housekeeping_short); + tcase_add_test(tc, test_housekeeping_long); + suite_add_tcase(s, tc); + + tc =3D tcase_create("thread_configuration"); + tcase_add_test(tc, test_cgroup_short_noarg); + tcase_add_test(tc, test_cgroup_short_space); + tcase_add_test(tc, test_cgroup_short_equals); + tcase_add_test(tc, test_cgroup_long_noarg); + tcase_add_test(tc, test_cgroup_long_space); + tcase_add_test(tc, test_cgroup_long_equals); + tcase_add_test(tc, test_kernel_threads_short); + tcase_add_test(tc, test_kernel_threads_long); + tcase_add_test(tc, test_priority_short); + tcase_add_test(tc, test_priority_long); + tcase_add_test(tc, test_user_load_short); + tcase_add_test(tc, test_user_load_long); + tcase_add_test(tc, test_user_threads_short); + tcase_add_test(tc, test_user_threads_long); + suite_add_tcase(s, tc); + + tc =3D tcase_create("histogram_options"); + tcase_add_test(tc, test_bucket_size_short); + tcase_add_test(tc, test_bucket_size_long); + tcase_add_test(tc, test_entries_short); + tcase_add_test(tc, test_entries_long); + tcase_add_test(tc, test_no_header); + tcase_add_test(tc, test_no_index); + tcase_add_test(tc, test_no_irq); + tcase_add_test(tc, test_no_summary); + tcase_add_test(tc, test_no_thread); + tcase_add_test(tc, test_with_zeros); + suite_add_tcase(s, tc); + + tc =3D tcase_create("output"); + tcase_add_test(tc, test_nano_short); + tcase_add_test(tc, test_nano_long); + suite_add_tcase(s, tc); + + tc =3D tcase_create("system_tuning"); + tcase_add_test(tc, test_deepest_idle_state); + tcase_add_test(tc, test_dma_latency); + tcase_add_test(tc, test_trace_buffer_size); + tcase_add_test(tc, test_warm_up); + suite_add_tcase(s, tc); + + tc =3D tcase_create("aa_actions"); + tcase_add_test(tc, test_auto); + tcase_add_test(tc, test_bpf_action); + tcase_add_test(tc, test_dump_tasks); + tcase_add_test(tc, test_no_aa); + tcase_add_test(tc, test_on_end); + tcase_add_test(tc, test_on_threshold); + tcase_add_test(tc, test_stack_format); + suite_add_tcase(s, tc); + + tc =3D tcase_create("general"); + tcase_add_test(tc, test_debug_short); + tcase_add_test(tc, test_debug_long); + tcase_add_test(tc, test_duration_short); + tcase_add_test(tc, test_duration_long); + suite_add_tcase(s, tc); + + return s; +} diff --git a/tools/tracing/rtla/tests/unit/timerlat_top_cli.c b/tools/traci= ng/rtla/tests/unit/timerlat_top_cli.c new file mode 100644 index 000000000000..1c39008564c5 --- /dev/null +++ b/tools/tracing/rtla/tests/unit/timerlat_top_cli.c @@ -0,0 +1,634 @@ +// SPDX-License-Identifier: GPL-2.0 + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "cli_params_assert.h" +#include "../../src/cli.h" + +#define PARSE_ARGS(...) char *argv[] =3D { __VA_ARGS__, NULL };\ + int argc =3D sizeof(argv) / sizeof(char *) - 1;\ + struct common_params *params =3D\ + timerlat_top_parse_args(argc, argv);\ + struct timerlat_params *tlat_params __maybe_unused =3D\ + to_timerlat_params(params) + +/* Tracing Options */ + +START_TEST(test_irq_short) +{ + PARSE_ARGS("timerlat", "top", "-i", "20"); + + ck_assert_int_eq(params->stop_us, 20); +} +END_TEST + +START_TEST(test_irq_long) +{ + PARSE_ARGS("timerlat", "top", "--irq", "20"); + + ck_assert_int_eq(params->stop_us, 20); +} +END_TEST + +START_TEST(test_period_short) +{ + PARSE_ARGS("timerlat", "top", "-p", "200"); + + ck_assert_int_eq(tlat_params->timerlat_period_us, 200); +} +END_TEST + +START_TEST(test_period_long) +{ + PARSE_ARGS("timerlat", "top", "--period", "200"); + + ck_assert_int_eq(tlat_params->timerlat_period_us, 200); +} +END_TEST + +START_TEST(test_stack_short) +{ + PARSE_ARGS("timerlat", "top", "-s", "20"); + + ck_assert_int_eq(tlat_params->print_stack, 20); +} +END_TEST + +START_TEST(test_stack_long) +{ + PARSE_ARGS("timerlat", "top", "--stack", "20"); + + ck_assert_int_eq(tlat_params->print_stack, 20); +} +END_TEST + +START_TEST(test_thread_short) +{ + PARSE_ARGS("timerlat", "top", "-T", "20"); + + ck_assert_int_eq(params->stop_total_us, 20); +} +END_TEST + +START_TEST(test_thread_long) +{ + PARSE_ARGS("timerlat", "top", "--thread", "20"); + + ck_assert_int_eq(params->stop_total_us, 20); +} +END_TEST + +/* Event Configuration */ + +START_TEST(test_event_short) +{ + PARSE_ARGS("timerlat", "top", "-e", "system:event"); + + CLI_ASSERT_SINGLE_EVENT("system", "event"); +} +END_TEST + +START_TEST(test_event_long) +{ + PARSE_ARGS("timerlat", "top", "--event", "system:event"); + + CLI_ASSERT_SINGLE_EVENT("system", "event"); +} +END_TEST + +START_TEST(test_filter) +{ + PARSE_ARGS("timerlat", "top", "-e", "system:event", "--filter", "filter"); + + CLI_ASSERT_SINGLE_FILTER("filter"); +} +END_TEST + +START_TEST(test_trigger) +{ + PARSE_ARGS("timerlat", "top", "-e", "system:event", "--trigger", "trigger= "); + + CLI_ASSERT_SINGLE_TRIGGER("trigger"); +} +END_TEST + +START_TEST(test_trace_short_noarg) +{ + PARSE_ARGS("timerlat", "top", "-t"); + + CLI_ASSERT_SINGLE_ACTION(threshold_actions, ACTION_TRACE_OUTPUT, trace_ou= tput, str, + "timerlat_trace.txt"); +} +END_TEST + +START_TEST(test_trace_short_followarg) +{ + PARSE_ARGS("timerlat", "top", "-t", "-d", "20"); + + CLI_ASSERT_SINGLE_ACTION(threshold_actions, ACTION_TRACE_OUTPUT, trace_ou= tput, str, + "timerlat_trace.txt"); + ck_assert_int_eq(params->duration, 20); /* check if next argument is read= correctly */ +} +END_TEST + +START_TEST(test_trace_short_space) +{ + PARSE_ARGS("timerlat", "top", "-t", "tracefile"); + + CLI_ASSERT_SINGLE_ACTION(threshold_actions, ACTION_TRACE_OUTPUT, trace_ou= tput, str, + "tracefile"); +} +END_TEST + +START_TEST(test_trace_short_equals) +{ + PARSE_ARGS("timerlat", "top", "-t=3Dtracefile"); + + CLI_ASSERT_SINGLE_ACTION(threshold_actions, ACTION_TRACE_OUTPUT, trace_ou= tput, str, + "tracefile"); +} +END_TEST + +START_TEST(test_trace_long_noarg) +{ + PARSE_ARGS("timerlat", "top", "--trace"); + + CLI_ASSERT_SINGLE_ACTION(threshold_actions, ACTION_TRACE_OUTPUT, trace_ou= tput, str, + "timerlat_trace.txt"); +} +END_TEST + +START_TEST(test_trace_long_followarg) +{ + PARSE_ARGS("timerlat", "top", "--trace", "-d", "20"); + + CLI_ASSERT_SINGLE_ACTION(threshold_actions, ACTION_TRACE_OUTPUT, trace_ou= tput, str, + "timerlat_trace.txt"); + ck_assert_int_eq(params->duration, 20); /* check if next argument is read= correctly */ +} +END_TEST + +START_TEST(test_trace_long_space) +{ + PARSE_ARGS("timerlat", "top", "--trace", "tracefile"); + + CLI_ASSERT_SINGLE_ACTION(threshold_actions, ACTION_TRACE_OUTPUT, trace_ou= tput, str, + "tracefile"); +} +END_TEST + +START_TEST(test_trace_long_equals) +{ + PARSE_ARGS("timerlat", "top", "--trace=3Dtracefile"); + + CLI_ASSERT_SINGLE_ACTION(threshold_actions, ACTION_TRACE_OUTPUT, trace_ou= tput, str, + "tracefile"); +} +END_TEST + +/* CPU Configuration */ + +START_TEST(test_cpus_short) +{ + nr_cpus =3D 4; + + PARSE_ARGS("timerlat", "top", "-c", "0-1,3"); + + ck_assert_str_eq(params->cpus, "0-1,3"); + CLI_ASSERT_CPUSET(monitored_cpus, 0, 1, 3); +} +END_TEST + +START_TEST(test_cpus_long) +{ + nr_cpus =3D 4; + + PARSE_ARGS("timerlat", "top", "--cpus", "0-1,3"); + + ck_assert_str_eq(params->cpus, "0-1,3"); + CLI_ASSERT_CPUSET(monitored_cpus, 0, 1, 3); +} +END_TEST + +START_TEST(test_housekeeping_short) +{ + nr_cpus =3D 4; + + PARSE_ARGS("timerlat", "top", "-H", "0-1,3"); + + CLI_ASSERT_CPUSET(hk_cpu_set, 0, 1, 3); +} +END_TEST + +START_TEST(test_housekeeping_long) +{ + nr_cpus =3D 4; + + PARSE_ARGS("timerlat", "top", "--house-keeping", "0-1,3"); + + CLI_ASSERT_CPUSET(hk_cpu_set, 0, 1, 3); +} +END_TEST + +/* Thread Configuration */ + +START_TEST(test_cgroup_short_noarg) +{ + PARSE_ARGS("timerlat", "top", "-C"); + + ck_assert(params->cgroup); + ck_assert_ptr_null(params->cgroup_name); +} +END_TEST + +START_TEST(test_cgroup_short_space) +{ + PARSE_ARGS("timerlat", "top", "-C", "cgroup"); + + ck_assert(params->cgroup); + ck_assert_str_eq(params->cgroup_name, "cgroup"); +} +END_TEST + +START_TEST(test_cgroup_short_equals) +{ + PARSE_ARGS("timerlat", "top", "-C=3Dcgroup"); + + ck_assert(params->cgroup); + ck_assert_str_eq(params->cgroup_name, "cgroup"); +} +END_TEST + +START_TEST(test_cgroup_long_noarg) +{ + PARSE_ARGS("timerlat", "top", "--cgroup"); + + ck_assert(params->cgroup); + ck_assert_ptr_null(params->cgroup_name); +} +END_TEST + +START_TEST(test_cgroup_long_space) +{ + PARSE_ARGS("timerlat", "top", "--cgroup", "cgroup"); + + ck_assert(params->cgroup); + ck_assert_str_eq(params->cgroup_name, "cgroup"); +} +END_TEST + +START_TEST(test_cgroup_long_equals) +{ + PARSE_ARGS("timerlat", "top", "--cgroup=3Dcgroup"); + + ck_assert(params->cgroup); + ck_assert_str_eq(params->cgroup_name, "cgroup"); +} +END_TEST + +START_TEST(test_kernel_threads_short) +{ + PARSE_ARGS("timerlat", "top", "-k"); + + ck_assert(params->kernel_workload); + ck_assert(!params->user_workload); + ck_assert(!params->user_data); +} +END_TEST + +START_TEST(test_kernel_threads_long) +{ + PARSE_ARGS("timerlat", "top", "--kernel-threads"); + + ck_assert(params->kernel_workload); + ck_assert(!params->user_workload); + ck_assert(!params->user_data); +} +END_TEST + +START_TEST(test_priority_short) +{ + PARSE_ARGS("timerlat", "top", "-P", "f:95"); + + ck_assert_int_eq(params->sched_param.sched_policy, SCHED_FIFO); + ck_assert_int_eq(params->sched_param.sched_priority, 95); +} +END_TEST + +START_TEST(test_priority_long) +{ + PARSE_ARGS("timerlat", "top", "--priority", "f:95"); + + ck_assert_int_eq(params->sched_param.sched_policy, SCHED_FIFO); + ck_assert_int_eq(params->sched_param.sched_priority, 95); +} +END_TEST + +START_TEST(test_user_load_short) +{ + PARSE_ARGS("timerlat", "top", "-U"); + + ck_assert(!params->kernel_workload); + ck_assert(!params->user_workload); + ck_assert(params->user_data); +} +END_TEST + +START_TEST(test_user_load_long) +{ + PARSE_ARGS("timerlat", "top", "--user-load"); + + ck_assert(!params->kernel_workload); + ck_assert(!params->user_workload); + ck_assert(params->user_data); +} +END_TEST + +START_TEST(test_user_threads_short) +{ + PARSE_ARGS("timerlat", "top", "-u"); + + ck_assert(!params->kernel_workload); + ck_assert(params->user_workload); + ck_assert(params->user_data); +} +END_TEST + +START_TEST(test_user_threads_long) +{ + PARSE_ARGS("timerlat", "top", "--user-threads"); + + ck_assert(!params->kernel_workload); + ck_assert(params->user_workload); + ck_assert(params->user_data); +} +END_TEST + +/* Output */ + +START_TEST(test_nano_short) +{ + PARSE_ARGS("timerlat", "top", "-n"); + + ck_assert_int_eq(params->output_divisor, 1); +} +END_TEST + +START_TEST(test_nano_long) +{ + PARSE_ARGS("timerlat", "top", "--nano"); + + ck_assert_int_eq(params->output_divisor, 1); +} +END_TEST + +START_TEST(test_quiet_short) +{ + PARSE_ARGS("timerlat", "top", "-q"); + + ck_assert(params->quiet); +} +END_TEST + +START_TEST(test_quiet_long) +{ + PARSE_ARGS("timerlat", "top", "--quiet"); + + ck_assert(params->quiet); +} +END_TEST + +/* System Tuning */ + +START_TEST(test_deepest_idle_state) +{ + PARSE_ARGS("timerlat", "top", "--deepest-idle-state", "1"); + + ck_assert_int_eq(tlat_params->deepest_idle_state, 1); +} +END_TEST + +START_TEST(test_dma_latency) +{ + PARSE_ARGS("timerlat", "top", "--dma-latency", "10"); + + ck_assert_int_eq(tlat_params->dma_latency, 10); +} +END_TEST + +START_TEST(test_trace_buffer_size) +{ + PARSE_ARGS("timerlat", "top", "--trace-buffer-size", "200"); + + ck_assert_int_eq(params->buffer_size, 200); +} +END_TEST + +START_TEST(test_warm_up) +{ + PARSE_ARGS("timerlat", "top", "--warm-up", "5"); + + ck_assert_int_eq(params->warmup, 5); +} +END_TEST + +/* Auto Analysis and Actions */ + +START_TEST(test_auto) +{ + PARSE_ARGS("timerlat", "top", "-a", "20"); + + CLI_TIMERLAT_ASSERT_AUTO(20); +} +END_TEST + +START_TEST(test_aa_only) +{ + PARSE_ARGS("timerlat", "top", "--aa-only", "20"); + + CLI_TIMERLAT_ASSERT_AA_ONLY(20); +} +END_TEST + +START_TEST(test_bpf_action) +{ + PARSE_ARGS("timerlat", "top", "--bpf-action", "program"); + + ck_assert_str_eq(tlat_params->bpf_action_program, "program"); +} +END_TEST + +START_TEST(test_dump_tasks) +{ + PARSE_ARGS("timerlat", "top", "--dump-tasks"); + + ck_assert(tlat_params->dump_tasks); +} +END_TEST + +START_TEST(test_no_aa) +{ + PARSE_ARGS("timerlat", "top", "--no-aa"); + + ck_assert(tlat_params->no_aa); +} +END_TEST + +START_TEST(test_on_end) +{ + PARSE_ARGS("timerlat", "top", "--on-end", "trace"); + + CLI_ASSERT_SINGLE_ACTION(end_actions, ACTION_TRACE_OUTPUT, trace_output, = str, + "timerlat_trace.txt"); +} +END_TEST + +START_TEST(test_on_threshold) +{ + PARSE_ARGS("timerlat", "top", "--on-threshold", "trace"); + + CLI_ASSERT_SINGLE_ACTION(threshold_actions, ACTION_TRACE_OUTPUT, trace_ou= tput, str, + "timerlat_trace.txt"); +} +END_TEST + +START_TEST(test_stack_format) +{ + PARSE_ARGS("timerlat", "top", "--stack-format", "truncate"); + + ck_assert_int_eq(tlat_params->stack_format, STACK_FORMAT_TRUNCATE); +} +END_TEST + +/* General */ + +START_TEST(test_debug_short) +{ + PARSE_ARGS("timerlat", "top", "-D"); + + ck_assert(config_debug); +} +END_TEST + +START_TEST(test_debug_long) +{ + PARSE_ARGS("timerlat", "top", "--debug"); + + ck_assert(config_debug); +} +END_TEST + +START_TEST(test_duration_short) +{ + PARSE_ARGS("timerlat", "top", "-d", "1m"); + + ck_assert_int_eq(params->duration, 60); +} +END_TEST + +START_TEST(test_duration_long) +{ + PARSE_ARGS("timerlat", "top", "--duration", "1m"); + + ck_assert_int_eq(params->duration, 60); +} +END_TEST + +Suite *timerlat_top_cli_suite(void) +{ + Suite *s =3D suite_create("timerlat_top_cli"); + TCase *tc; + + tc =3D tcase_create("tracing_options"); + tcase_add_test(tc, test_irq_short); + tcase_add_test(tc, test_irq_long); + tcase_add_test(tc, test_period_short); + tcase_add_test(tc, test_period_long); + tcase_add_test(tc, test_stack_short); + tcase_add_test(tc, test_stack_long); + tcase_add_test(tc, test_thread_short); + tcase_add_test(tc, test_thread_long); + tcase_add_test(tc, test_trace_short_noarg); + tcase_add_test(tc, test_trace_short_followarg); + tcase_add_test(tc, test_trace_short_space); + tcase_add_test(tc, test_trace_short_equals); + tcase_add_test(tc, test_trace_long_noarg); + tcase_add_test(tc, test_trace_long_followarg); + tcase_add_test(tc, test_trace_long_space); + tcase_add_test(tc, test_trace_long_equals); + suite_add_tcase(s, tc); + + tc =3D tcase_create("event_configuration"); + tcase_add_test(tc, test_event_short); + tcase_add_test(tc, test_event_long); + tcase_add_test(tc, test_filter); + tcase_add_test(tc, test_trigger); + suite_add_tcase(s, tc); + + tc =3D tcase_create("cpu_configuration"); + tcase_add_test(tc, test_cpus_short); + tcase_add_test(tc, test_cpus_long); + tcase_add_test(tc, test_housekeeping_short); + tcase_add_test(tc, test_housekeeping_long); + suite_add_tcase(s, tc); + + tc =3D tcase_create("thread_configuration"); + tcase_add_test(tc, test_cgroup_short_noarg); + tcase_add_test(tc, test_cgroup_short_space); + tcase_add_test(tc, test_cgroup_short_equals); + tcase_add_test(tc, test_cgroup_long_noarg); + tcase_add_test(tc, test_cgroup_long_space); + tcase_add_test(tc, test_cgroup_long_equals); + tcase_add_test(tc, test_kernel_threads_short); + tcase_add_test(tc, test_kernel_threads_long); + tcase_add_test(tc, test_priority_short); + tcase_add_test(tc, test_priority_long); + tcase_add_test(tc, test_user_load_short); + tcase_add_test(tc, test_user_load_long); + tcase_add_test(tc, test_user_threads_short); + tcase_add_test(tc, test_user_threads_long); + suite_add_tcase(s, tc); + + tc =3D tcase_create("output"); + tcase_add_test(tc, test_nano_short); + tcase_add_test(tc, test_nano_long); + tcase_add_test(tc, test_quiet_short); + tcase_add_test(tc, test_quiet_long); + suite_add_tcase(s, tc); + + tc =3D tcase_create("system_tuning"); + tcase_add_test(tc, test_deepest_idle_state); + tcase_add_test(tc, test_dma_latency); + tcase_add_test(tc, test_trace_buffer_size); + tcase_add_test(tc, test_warm_up); + suite_add_tcase(s, tc); + + tc =3D tcase_create("aa_actions"); + tcase_add_test(tc, test_auto); + tcase_add_test(tc, test_aa_only); + tcase_add_test(tc, test_bpf_action); + tcase_add_test(tc, test_dump_tasks); + tcase_add_test(tc, test_no_aa); + tcase_add_test(tc, test_on_end); + tcase_add_test(tc, test_on_threshold); + tcase_add_test(tc, test_stack_format); + suite_add_tcase(s, tc); + + tc =3D tcase_create("general"); + tcase_add_test(tc, test_debug_short); + tcase_add_test(tc, test_debug_long); + tcase_add_test(tc, test_duration_short); + tcase_add_test(tc, test_duration_long); + suite_add_tcase(s, tc); + + return s; +} diff --git a/tools/tracing/rtla/tests/unit/unit_tests.c b/tools/tracing/rtl= a/tests/unit/unit_tests.c index f87d761f9b12..64884f6cbdeb 100644 --- a/tools/tracing/rtla/tests/unit/unit_tests.c +++ b/tools/tracing/rtla/tests/unit/unit_tests.c @@ -5,17 +5,28 @@ #include =20 #include "../../src/utils.h" +#include "../../src/cli.h" =20 Suite *utils_suite(void); Suite *actions_suite(void); +Suite *osnoise_top_cli_suite(void); +Suite *osnoise_hist_cli_suite(void); +Suite *timerlat_top_cli_suite(void); +Suite *timerlat_hist_cli_suite(void); =20 int main(int argc, char *argv[]) { int num_failed; SRunner *sr; =20 + in_unit_test =3D true; + sr =3D srunner_create(utils_suite()); srunner_add_suite(sr, actions_suite()); + srunner_add_suite(sr, osnoise_top_cli_suite()); + srunner_add_suite(sr, osnoise_hist_cli_suite()); + srunner_add_suite(sr, timerlat_top_cli_suite()); + srunner_add_suite(sr, timerlat_hist_cli_suite()); =20 srunner_run_all(sr, CK_VERBOSE); num_failed =3D srunner_ntests_failed(sr); --=20 2.54.0 From nobody Sun May 24 20:34:47 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 71D503BD228 for ; Thu, 21 May 2026 14:19:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779373167; cv=none; b=URw5vEj2V5Ryrt2NAC6zeGjTomkOp80pMQZ8r/L0eq5Vj3vaopWeCCQB/YubVGqiRxZgTQoFj9QFyICY5sEFp7X4l+ZKTWYJM1S2wwUds8chYhWTeOBVyC3H0YihBZJXON2oUPhpo4jVZy3JgcVDeeq6dQMMPZak8Q1hTzvGHJo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779373167; c=relaxed/simple; bh=A7eEBdnZdPJwm3+eW6mcDxil11etOD4xCI25wUGwWno=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Rl5c4PhKCo5FDKmujEQzORxxAId63Ku98rw/l9OL47zyudVyWTgqMKccakHczdfZmdUNeQySHEBg/R3Fw7KyO//oBABIa6B5DYu2WNddgNM2l1HUHJGtzIx/r7i8bnN6IatJG1EbPsJvtU/pZsGGWw2sgEcxcW7iZLy33ZBzuTs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=MrH1vOx+; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="MrH1vOx+" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1779373164; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=gwePtgyHfCYA8SF/W9HMlOSyujDMUDE9mFcE6kUigBE=; b=MrH1vOx+Hbjj9A7dw/MkXJxOZDIpss6UJr8avpNQw/CbNg8rBo3vzxNBOVqqwKTdvw+dp3 OksV/PJODeV3cAB+PTs3NPGfLH7w1ZbR4ZOKLrHXUhX3Hwqf/RuSQTf+jUslnEYdAx7VSH 22J6HslxcyLtJ+DPujirMZgRvl1GxdA= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-530-NzMkp_tHO4CwzQNCV2aKJQ-1; Thu, 21 May 2026 10:19:06 -0400 X-MC-Unique: NzMkp_tHO4CwzQNCV2aKJQ-1 X-Mimecast-MFC-AGG-ID: NzMkp_tHO4CwzQNCV2aKJQ_1779373142 Received: from mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.95]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 5D44F19560B0; Thu, 21 May 2026 14:19:02 +0000 (UTC) Received: from fedora.brq.redhat.com (unknown [10.43.17.109]) by mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 3B8441681; Thu, 21 May 2026 14:18:59 +0000 (UTC) From: Tomas Glozar To: Steven Rostedt , Tomas Glozar Cc: John Kacur , Luis Goncalves , Crystal Wood , Costa Shulyupin , Wander Lairson Costa , Ivan Pravdin , Namhyung Kim , Ian Rogers , Arnaldo Carvalho de Melo , LKML , linux-trace-kernel , linux-perf-users Subject: [PATCH v2 6/6] rtla/tests: Add unit tests for CLI option callbacks Date: Thu, 21 May 2026 16:18:33 +0200 Message-ID: <20260521141833.2353025-7-tglozar@redhat.com> In-Reply-To: <20260521141833.2353025-1-tglozar@redhat.com> References: <20260521141833.2353025-1-tglozar@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.6 on 10.30.177.95 Content-Type: text/plain; charset="utf-8" In addition to testing all tool_parse_args() functions, test also all callbacks used for parsing custom option formats. The callbacks represent a middle layer between the parsing functions and utility functions dedicated to checking specific argument formats, for example, scheduling class and duration. Callback tests are run before parsing functions to make sure any issue in the former is reported before it is encountered through the latter. Tests verify both successful parsing and proper rejection of invalid inputs (via exit tests). To enable testing static callbacks, a pragma once guard is added to timerlat.h for safe inclusion by cli_p.h. Add dependency of UNIT_TESTS_IN on LIBSUBCMD_INCLUDES, as the new test file tests/unit/cli_opt_callback.c includes cli_p.h which includes subcmd/parse-options.h. Signed-off-by: Tomas Glozar --- tools/tracing/rtla/src/timerlat.h | 2 + tools/tracing/rtla/tests/unit/Build | 1 + tools/tracing/rtla/tests/unit/Makefile.unit | 2 +- .../rtla/tests/unit/cli_opt_callback.c | 704 ++++++++++++++++++ tools/tracing/rtla/tests/unit/unit_tests.c | 2 + 5 files changed, 710 insertions(+), 1 deletion(-) create mode 100644 tools/tracing/rtla/tests/unit/cli_opt_callback.c diff --git a/tools/tracing/rtla/src/timerlat.h b/tools/tracing/rtla/src/tim= erlat.h index 37a808f1611e..38ab6b41a15e 100644 --- a/tools/tracing/rtla/src/timerlat.h +++ b/tools/tracing/rtla/src/timerlat.h @@ -1,4 +1,6 @@ // SPDX-License-Identifier: GPL-2.0 +#pragma once + #include "osnoise.h" =20 /* diff --git a/tools/tracing/rtla/tests/unit/Build b/tools/tracing/rtla/tests= /unit/Build index 16139f17ea1f..d5a0f13922be 100644 --- a/tools/tracing/rtla/tests/unit/Build +++ b/tools/tracing/rtla/tests/unit/Build @@ -5,3 +5,4 @@ unit_tests-y +=3D osnoise_top_cli.o unit_tests-y +=3D osnoise_hist_cli.o unit_tests-y +=3D timerlat_top_cli.o unit_tests-y +=3D timerlat_hist_cli.o +unit_tests-y +=3D cli_opt_callback.o diff --git a/tools/tracing/rtla/tests/unit/Makefile.unit b/tools/tracing/rt= la/tests/unit/Makefile.unit index 8c33a9583c30..839abda64b76 100644 --- a/tools/tracing/rtla/tests/unit/Makefile.unit +++ b/tools/tracing/rtla/tests/unit/Makefile.unit @@ -6,7 +6,7 @@ UNIT_TESTS_IN :=3D $(UNIT_TESTS)-in.o $(UNIT_TESTS): $(UNIT_TESTS_IN) $(RTLA_IN) $(LIBSUBCMD) $(LIB_STRING) $(LI= B_STR_ERROR_R) $(QUIET_LINK)$(CC) $(LDFLAGS) -o $@ $^ $(EXTLIBS) -lcheck =20 -$(UNIT_TESTS_IN): fixdep +$(UNIT_TESTS_IN): fixdep $(LIBSUBCMD_INCLUDES) make $(build)=3Dunit_tests =20 unit-tests: FORCE diff --git a/tools/tracing/rtla/tests/unit/cli_opt_callback.c b/tools/traci= ng/rtla/tests/unit/cli_opt_callback.c new file mode 100644 index 000000000000..01647f4227d1 --- /dev/null +++ b/tools/tracing/rtla/tests/unit/cli_opt_callback.c @@ -0,0 +1,704 @@ +// SPDX-License-Identifier: GPL-2.0 + +#define _GNU_SOURCE +#include +#include + +#define RTLA_ALLOW_CLI_P_H +#include "../../src/cli_p.h" +#include "cli_params_assert.h" + +#define TEST_CALLBACK(value, cb) OPT_CALLBACK('t', "test", value, "test va= lue", "test help", cb) + +START_TEST(test_opt_llong_callback_simple) +{ + long long test_value =3D 0; + const struct option opt =3D TEST_CALLBACK(&test_value, opt_llong_callback= ); + + ck_assert_int_eq(opt_llong_callback(&opt, "1234567890", 0), 0); + ck_assert_int_eq(test_value, 1234567890); +} +END_TEST + +START_TEST(test_opt_llong_callback_max) +{ + long long test_value =3D 0; + const struct option opt =3D TEST_CALLBACK(&test_value, opt_llong_callback= ); + + ck_assert_int_eq(opt_llong_callback(&opt, "9223372036854775807", 0), 0); + ck_assert_int_eq(test_value, 9223372036854775807LL); +} +END_TEST + +START_TEST(test_opt_llong_callback_min) +{ + long long test_value =3D 0; + const struct option opt =3D TEST_CALLBACK(&test_value, opt_llong_callback= ); + + ck_assert_int_eq(opt_llong_callback(&opt, "-9223372036854775808", 0), 0); + ck_assert_int_eq(test_value, ~9223372036854775807LL); +} +END_TEST + +START_TEST(test_opt_int_callback_simple) +{ + int test_value =3D 0; + const struct option opt =3D TEST_CALLBACK(&test_value, opt_int_callback); + + ck_assert_int_eq(opt_int_callback(&opt, "1234567890", 0), 0); + ck_assert_int_eq(test_value, 1234567890); +} +END_TEST + +START_TEST(test_opt_int_callback_max) +{ + int test_value =3D 0; + const struct option opt =3D TEST_CALLBACK(&test_value, opt_int_callback); + + ck_assert_int_eq(opt_int_callback(&opt, "2147483647", 0), 0); + ck_assert_int_eq(test_value, 2147483647); +} +END_TEST + +START_TEST(test_opt_int_callback_min) +{ + int test_value =3D 0; + const struct option opt =3D TEST_CALLBACK(&test_value, opt_int_callback); + + ck_assert_int_eq(opt_int_callback(&opt, "-2147483648", 0), 0); + ck_assert_int_eq(test_value, -2147483648); +} +END_TEST + +START_TEST(test_opt_int_callback_non_numeric) +{ + int test_value =3D 0; + const struct option opt =3D TEST_CALLBACK(&test_value, opt_int_callback); + + ck_assert_int_eq(opt_int_callback(&opt, "abc", 0), -1); + ck_assert_int_eq(test_value, 0); +} +END_TEST + +START_TEST(test_opt_int_callback_non_numeric_suffix) +{ + int test_value =3D 0; + const struct option opt =3D TEST_CALLBACK(&test_value, opt_int_callback); + + ck_assert_int_eq(opt_int_callback(&opt, "1234567890abc", 0), -1); + ck_assert_int_eq(test_value, 0); +} +END_TEST + +START_TEST(test_opt_cpus_cb) +{ + struct common_params params =3D {0}; + const struct option opt =3D TEST_CALLBACK(¶ms, opt_cpus_cb); + + nr_cpus =3D 4; + ck_assert_int_eq(opt_cpus_cb(&opt, "0-3", 0), 0); + ck_assert_str_eq(params.cpus, "0-3"); +} +END_TEST + +START_TEST(test_opt_cpus_cb_invalid) +{ + struct common_params params =3D {0}; + const struct option opt =3D TEST_CALLBACK(¶ms, opt_cpus_cb); + + nr_cpus =3D 4; + assert(freopen("/dev/null", "w", stderr)); + opt_cpus_cb(&opt, "0-3,5", 0); +} +END_TEST + +START_TEST(test_opt_cgroup_cb) +{ + struct common_params params =3D {0}; + const struct option opt =3D TEST_CALLBACK(¶ms, opt_cgroup_cb); + + ck_assert_int_eq(opt_cgroup_cb(&opt, "cgroup", 0), 0); + ck_assert_int_eq(params.cgroup, 1); + ck_assert_str_eq(params.cgroup_name, "cgroup"); +} +END_TEST + +START_TEST(test_opt_cgroup_cb_equals) +{ + struct common_params params =3D {0}; + const struct option opt =3D TEST_CALLBACK(¶ms, opt_cgroup_cb); + + ck_assert_int_eq(opt_cgroup_cb(&opt, "=3Dcgroup", 0), 0); + ck_assert_int_eq(params.cgroup, 1); + ck_assert_str_eq(params.cgroup_name, "cgroup"); +} +END_TEST + +START_TEST(test_opt_duration_cb) +{ + struct common_params params =3D {0}; + const struct option opt =3D TEST_CALLBACK(¶ms, opt_duration_cb); + + ck_assert_int_eq(opt_duration_cb(&opt, "1m", 0), 0); + ck_assert_int_eq(params.duration, 60); +} +END_TEST + +START_TEST(test_opt_duration_cb_invalid) +{ + struct common_params params =3D {0}; + const struct option opt =3D TEST_CALLBACK(¶ms, opt_duration_cb); + + assert(freopen("/dev/null", "w", stderr)); + opt_duration_cb(&opt, "abc", 0); +} +END_TEST + +START_TEST(test_opt_event_cb) +{ + struct trace_events *events =3D NULL; + const struct option opt =3D TEST_CALLBACK(&events, opt_event_cb); + + ck_assert_int_eq(opt_event_cb(&opt, "sched:sched_switch", 0), 0); + ck_assert_str_eq(events->system, "sched"); + ck_assert_str_eq(events->event, "sched_switch"); + ck_assert_ptr_eq(events->next, NULL); +} +END_TEST + +START_TEST(test_opt_event_cb_multiple) +{ + struct trace_events *events =3D NULL; + const struct option opt =3D TEST_CALLBACK(&events, opt_event_cb); + + ck_assert_int_eq(opt_event_cb(&opt, "sched:sched_switch", 0), 0); + ck_assert_int_eq(opt_event_cb(&opt, "sched:sched_wakeup", 0), 0); + ck_assert_str_eq(events->system, "sched"); + ck_assert_str_eq(events->event, "sched_wakeup"); + ck_assert_str_eq(events->next->system, "sched"); + ck_assert_str_eq(events->next->event, "sched_switch"); + ck_assert_ptr_eq(events->next->next, NULL); +} +END_TEST + +START_TEST(test_opt_housekeeping_cb) +{ + struct common_params __params =3D {0}; + struct common_params *params =3D &__params; + const struct option opt =3D TEST_CALLBACK(params, opt_housekeeping_cb); + + nr_cpus =3D 4; + ck_assert_int_eq(opt_housekeeping_cb(&opt, "0-3", 0), 0); + ck_assert_int_eq(params->hk_cpus, 1); + CLI_ASSERT_CPUSET(hk_cpu_set, 0, 1, 2, 3); +} +END_TEST + +START_TEST(test_opt_housekeeping_cb_invalid) +{ + struct common_params params =3D {0}; + const struct option opt =3D TEST_CALLBACK(¶ms, opt_housekeeping_cb); + + nr_cpus =3D 4; + assert(freopen("/dev/null", "w", stderr)); + opt_housekeeping_cb(&opt, "0-3,5", 0); +} +END_TEST + +START_TEST(test_opt_priority_cb) +{ + struct common_params params =3D {0}; + const struct option opt =3D TEST_CALLBACK(¶ms, opt_priority_cb); + + ck_assert_int_eq(opt_priority_cb(&opt, "f:95", 0), 0); + ck_assert_int_eq(params.sched_param.sched_policy, SCHED_FIFO); + ck_assert_int_eq(params.sched_param.sched_priority, 95); +} +END_TEST + +START_TEST(test_opt_priority_cb_invalid) +{ + struct common_params params =3D {0}; + const struct option opt =3D TEST_CALLBACK(¶ms, opt_priority_cb); + + assert(freopen("/dev/null", "w", stderr)); + opt_priority_cb(&opt, "abc", 0); +} +END_TEST + +START_TEST(test_opt_trigger_cb) +{ + struct trace_events *events =3D trace_event_alloc("sched:sched_switch"); + const struct option opt =3D TEST_CALLBACK(&events, opt_trigger_cb); + + ck_assert_int_eq(opt_trigger_cb(&opt, "stacktrace", 0), 0); + ck_assert_str_eq(events->trigger, "stacktrace"); +} +END_TEST + +START_TEST(test_opt_trigger_cb_no_event) +{ + struct trace_events *events =3D NULL; + const struct option opt =3D TEST_CALLBACK(&events, opt_trigger_cb); + + assert(freopen("/dev/null", "w", stderr)); + opt_trigger_cb(&opt, "stacktrace", 0); +} +END_TEST + +START_TEST(test_opt_filter_cb) +{ + struct trace_events *events =3D trace_event_alloc("sched:sched_switch"); + const struct option opt =3D TEST_CALLBACK(&events, opt_filter_cb); + + ck_assert_int_eq(opt_filter_cb(&opt, "comm ~ \"rtla\"", 0), 0); + ck_assert_str_eq(events->filter, "comm ~ \"rtla\""); +} +END_TEST + +START_TEST(test_opt_filter_cb_no_event) +{ + struct trace_events *events =3D NULL; + const struct option opt =3D TEST_CALLBACK(&events, opt_filter_cb); + + assert(freopen("/dev/null", "w", stderr)); + opt_filter_cb(&opt, "comm ~ \"rtla\"", 0); +} +END_TEST + +START_TEST(test_opt_osnoise_auto_cb) +{ + struct osnoise_params params =3D {0}; + struct osnoise_cb_data cb_data =3D {¶ms}; + const struct option opt =3D TEST_CALLBACK(&cb_data, opt_osnoise_auto_cb); + + ck_assert_int_eq(opt_osnoise_auto_cb(&opt, "10", 0), 0); + ck_assert_int_eq(params.common.stop_us, 10); + ck_assert_int_eq(params.threshold, 1); + ck_assert_str_eq(cb_data.trace_output, "osnoise_trace.txt"); +} +END_TEST + +START_TEST(test_opt_osnoise_period_cb) +{ + unsigned long long period =3D 0; + const struct option opt =3D TEST_CALLBACK(&period, opt_osnoise_period_cb); + + ck_assert_int_eq(opt_osnoise_period_cb(&opt, "1000000", 0), 0); + ck_assert_int_eq(period, 1000000); +} +END_TEST + +START_TEST(test_opt_osnoise_period_cb_invalid) +{ + unsigned long long period =3D 0; + const struct option opt =3D TEST_CALLBACK(&period, opt_osnoise_period_cb); + + assert(freopen("/dev/null", "w", stderr)); + opt_osnoise_period_cb(&opt, "10000001", 0); +} +END_TEST + +START_TEST(test_opt_osnoise_runtime_cb) +{ + unsigned long long runtime =3D 0; + const struct option opt =3D TEST_CALLBACK(&runtime, opt_osnoise_runtime_c= b); + + ck_assert_int_eq(opt_osnoise_runtime_cb(&opt, "900000", 0), 0); + ck_assert_int_eq(runtime, 900000); +} +END_TEST + +START_TEST(test_opt_osnoise_runtime_cb_invalid) +{ + unsigned long long runtime =3D 0; + const struct option opt =3D TEST_CALLBACK(&runtime, opt_osnoise_runtime_c= b); + + assert(freopen("/dev/null", "w", stderr)); + opt_osnoise_runtime_cb(&opt, "99", 0); +} +END_TEST + +START_TEST(test_opt_osnoise_trace_output_cb) +{ + const char *trace_output =3D NULL; + const struct option opt =3D TEST_CALLBACK(&trace_output, opt_osnoise_trac= e_output_cb); + + ck_assert_int_eq(opt_osnoise_trace_output_cb(&opt, "trace.txt", 0), 0); + ck_assert_str_eq(trace_output, "trace.txt"); +} +END_TEST + +START_TEST(test_opt_osnoise_trace_output_cb_noarg) +{ + const char *trace_output =3D NULL; + const struct option opt =3D TEST_CALLBACK(&trace_output, opt_osnoise_trac= e_output_cb); + + ck_assert_int_eq(opt_osnoise_trace_output_cb(&opt, NULL, 0), 0); + ck_assert_str_eq(trace_output, "osnoise_trace.txt"); +} +END_TEST + +START_TEST(test_opt_osnoise_on_threshold_cb) +{ + struct actions actions =3D {0}; + const struct option opt =3D TEST_CALLBACK(&actions, opt_osnoise_on_thresh= old_cb); + + ck_assert_int_eq(opt_osnoise_on_threshold_cb(&opt, "trace", 0), 0); + ck_assert_int_eq(actions.len, 1); + ck_assert_int_eq(actions.list[0].type, ACTION_TRACE_OUTPUT); + ck_assert_str_eq(actions.list[0].trace_output, "osnoise_trace.txt"); +} +END_TEST + +START_TEST(test_opt_osnoise_on_threshold_cb_invalid) +{ + struct actions actions =3D {0}; + const struct option opt =3D TEST_CALLBACK(&actions, opt_osnoise_on_thresh= old_cb); + + assert(freopen("/dev/null", "w", stderr)); + opt_osnoise_on_threshold_cb(&opt, "abc", 0); +} +END_TEST + +START_TEST(test_opt_osnoise_on_end_cb) +{ + struct actions actions =3D {0}; + const struct option opt =3D TEST_CALLBACK(&actions, opt_osnoise_on_end_cb= ); + + ck_assert_int_eq(opt_osnoise_on_end_cb(&opt, "trace", 0), 0); + ck_assert_int_eq(actions.len, 1); + ck_assert_int_eq(actions.list[0].type, ACTION_TRACE_OUTPUT); + ck_assert_str_eq(actions.list[0].trace_output, "osnoise_trace.txt"); +} +END_TEST + +START_TEST(test_opt_osnoise_on_end_cb_invalid) +{ + struct actions actions =3D {0}; + const struct option opt =3D TEST_CALLBACK(&actions, opt_osnoise_on_end_cb= ); + + assert(freopen("/dev/null", "w", stderr)); + opt_osnoise_on_end_cb(&opt, "abc", 0); +} +END_TEST + +START_TEST(test_opt_timerlat_period_cb) +{ + long long period =3D 0; + const struct option opt =3D TEST_CALLBACK(&period, opt_timerlat_period_cb= ); + + ck_assert_int_eq(opt_timerlat_period_cb(&opt, "1000", 0), 0); + ck_assert_int_eq(period, 1000); +} +END_TEST + +START_TEST(test_opt_timerlat_period_cb_invalid) +{ + long long period =3D 0; + const struct option opt =3D TEST_CALLBACK(&period, opt_timerlat_period_cb= ); + + assert(freopen("/dev/null", "w", stderr)); + opt_timerlat_period_cb(&opt, "1000001", 0); +} +END_TEST + +START_TEST(test_opt_timerlat_auto_cb) +{ + struct timerlat_params params =3D {0}; + struct timerlat_cb_data cb_data =3D {¶ms}; + const struct option opt =3D TEST_CALLBACK(&cb_data, opt_timerlat_auto_cb); + + ck_assert_int_eq(opt_timerlat_auto_cb(&opt, "10", 0), 0); + ck_assert_int_eq(params.common.stop_us, 10); + ck_assert_int_eq(params.common.stop_total_us, 10); + ck_assert_int_eq(params.print_stack, 10); + ck_assert_str_eq(cb_data.trace_output, "timerlat_trace.txt"); +} +END_TEST + +START_TEST(test_opt_dma_latency_cb) +{ + int dma_latency =3D 0; + const struct option opt =3D TEST_CALLBACK(&dma_latency, opt_dma_latency_c= b); + + ck_assert_int_eq(opt_dma_latency_cb(&opt, "1000", 0), 0); + ck_assert_int_eq(dma_latency, 1000); +} +END_TEST + +START_TEST(test_opt_dma_latency_cb_min) +{ + int dma_latency =3D 0; + const struct option opt =3D TEST_CALLBACK(&dma_latency, opt_dma_latency_c= b); + + assert(freopen("/dev/null", "w", stderr)); + opt_dma_latency_cb(&opt, "-1", 0); +} +END_TEST + +START_TEST(test_opt_dma_latency_cb_max) +{ + int dma_latency =3D 0; + const struct option opt =3D TEST_CALLBACK(&dma_latency, opt_dma_latency_c= b); + + assert(freopen("/dev/null", "w", stderr)); + opt_dma_latency_cb(&opt, "10001", 0); +} +END_TEST + +START_TEST(test_opt_aa_only_cb) +{ + struct timerlat_params params =3D {0}; + const struct option opt =3D TEST_CALLBACK(¶ms, opt_aa_only_cb); + + ck_assert_int_eq(opt_aa_only_cb(&opt, "10", 0), 0); + ck_assert_int_eq(params.common.stop_us, 10); + ck_assert_int_eq(params.common.stop_total_us, 10); + ck_assert_int_eq(params.print_stack, 10); + ck_assert_int_eq(params.common.aa_only, 1); +} +END_TEST + +START_TEST(test_opt_timerlat_trace_output_cb) +{ + const char *trace_output =3D NULL; + const struct option opt =3D TEST_CALLBACK(&trace_output, opt_timerlat_tra= ce_output_cb); + + ck_assert_int_eq(opt_timerlat_trace_output_cb(&opt, "trace.txt", 0), 0); + ck_assert_str_eq(trace_output, "trace.txt"); +} +END_TEST + +START_TEST(test_opt_timerlat_trace_output_cb_noarg) +{ + const char *trace_output =3D NULL; + const struct option opt =3D TEST_CALLBACK(&trace_output, opt_timerlat_tra= ce_output_cb); + + ck_assert_int_eq(opt_timerlat_trace_output_cb(&opt, NULL, 0), 0); + ck_assert_str_eq(trace_output, "timerlat_trace.txt"); +} +END_TEST + +START_TEST(test_opt_timerlat_on_threshold_cb) +{ + struct actions actions =3D {0}; + const struct option opt =3D TEST_CALLBACK(&actions, opt_timerlat_on_thres= hold_cb); + + ck_assert_int_eq(opt_timerlat_on_threshold_cb(&opt, "trace", 0), 0); + ck_assert_int_eq(actions.len, 1); + ck_assert_int_eq(actions.list[0].type, ACTION_TRACE_OUTPUT); + ck_assert_str_eq(actions.list[0].trace_output, "timerlat_trace.txt"); +} +END_TEST + +START_TEST(test_opt_timerlat_on_threshold_cb_invalid) +{ + struct actions actions =3D {0}; + const struct option opt =3D TEST_CALLBACK(&actions, opt_timerlat_on_thres= hold_cb); + + assert(freopen("/dev/null", "w", stderr)); + opt_timerlat_on_threshold_cb(&opt, "abc", 0); +} +END_TEST + +START_TEST(test_opt_timerlat_on_end_cb) +{ + struct actions actions =3D {0}; + const struct option opt =3D TEST_CALLBACK(&actions, opt_timerlat_on_end_c= b); + + ck_assert_int_eq(opt_timerlat_on_end_cb(&opt, "trace", 0), 0); + ck_assert_int_eq(actions.len, 1); + ck_assert_int_eq(actions.list[0].type, ACTION_TRACE_OUTPUT); + ck_assert_str_eq(actions.list[0].trace_output, "timerlat_trace.txt"); +} +END_TEST + +START_TEST(test_opt_timerlat_on_end_cb_invalid) +{ + struct actions actions =3D {0}; + const struct option opt =3D TEST_CALLBACK(&actions, opt_timerlat_on_end_c= b); + + assert(freopen("/dev/null", "w", stderr)); + opt_timerlat_on_end_cb(&opt, "abc", 0); +} +END_TEST + +START_TEST(test_opt_user_threads_cb) +{ + struct timerlat_params params =3D {0}; + const struct option opt =3D TEST_CALLBACK(¶ms, opt_user_threads_cb); + + ck_assert_int_eq(opt_user_threads_cb(&opt, NULL, 0), 0); + ck_assert_int_eq(params.common.user_workload, 1); + ck_assert_int_eq(params.common.user_data, 1); +} +END_TEST + +START_TEST(test_opt_nano_cb) +{ + struct timerlat_params params =3D {0}; + const struct option opt =3D TEST_CALLBACK(¶ms, opt_nano_cb); + + ck_assert_int_eq(opt_nano_cb(&opt, NULL, 0), 0); + ck_assert_int_eq(params.common.output_divisor, 1); +} +END_TEST + +START_TEST(test_opt_stack_format_cb) +{ + int stack_format =3D 0; + const struct option opt =3D TEST_CALLBACK(&stack_format, opt_stack_format= _cb); + + ck_assert_int_eq(opt_stack_format_cb(&opt, "full", 0), 0); + ck_assert_int_eq(stack_format, STACK_FORMAT_FULL); +} +END_TEST + +START_TEST(test_opt_stack_format_cb_invalid) +{ + int stack_format =3D 0; + const struct option opt =3D TEST_CALLBACK(&stack_format, opt_stack_format= _cb); + + assert(freopen("/dev/null", "w", stderr)); + opt_stack_format_cb(&opt, "abc", 0); +} +END_TEST + +START_TEST(test_opt_bucket_size_cb) +{ + int bucket_size =3D 0; + const struct option opt =3D TEST_CALLBACK(&bucket_size, opt_bucket_size_c= b); + + ck_assert_int_eq(opt_bucket_size_cb(&opt, "100", 0), 0); + ck_assert_int_eq(bucket_size, 100); +} +END_TEST + +START_TEST(test_opt_bucket_size_min) +{ + int bucket_size =3D 0; + const struct option opt =3D TEST_CALLBACK(&bucket_size, opt_bucket_size_c= b); + + assert(freopen("/dev/null", "w", stderr)); + opt_bucket_size_cb(&opt, "0", 0); +} +END_TEST + +START_TEST(test_opt_bucket_size_max) +{ + int bucket_size =3D 0; + const struct option opt =3D TEST_CALLBACK(&bucket_size, opt_bucket_size_c= b); + + assert(freopen("/dev/null", "w", stderr)); + opt_bucket_size_cb(&opt, "1000001", 0); +} +END_TEST + +START_TEST(test_opt_entries_cb) +{ + int entries =3D 0; + const struct option opt =3D TEST_CALLBACK(&entries, opt_entries_cb); + + ck_assert_int_eq(opt_entries_cb(&opt, "100", 0), 0); + ck_assert_int_eq(entries, 100); +} +END_TEST + +START_TEST(test_opt_entries_min) +{ + int entries =3D 0; + const struct option opt =3D TEST_CALLBACK(&entries, opt_entries_cb); + + assert(freopen("/dev/null", "w", stderr)); + opt_entries_cb(&opt, "9", 0); +} +END_TEST + +START_TEST(test_opt_entries_max) +{ + int entries =3D 0; + const struct option opt =3D TEST_CALLBACK(&entries, opt_entries_cb); + + assert(freopen("/dev/null", "w", stderr)); + opt_entries_cb(&opt, "10000000", 0); +} +END_TEST + +Suite *cli_opt_callback_suite(void) +{ + Suite *s =3D suite_create("cli_opt_callback"); + TCase *tc; + + tc =3D tcase_create("common"); + tcase_add_test(tc, test_opt_llong_callback_simple); + tcase_add_test(tc, test_opt_llong_callback_max); + tcase_add_test(tc, test_opt_llong_callback_min); + tcase_add_test(tc, test_opt_int_callback_simple); + tcase_add_test(tc, test_opt_int_callback_max); + tcase_add_test(tc, test_opt_int_callback_min); + tcase_add_test(tc, test_opt_int_callback_non_numeric); + tcase_add_test(tc, test_opt_int_callback_non_numeric_suffix); + tcase_add_test(tc, test_opt_cpus_cb); + tcase_add_exit_test(tc, test_opt_cpus_cb_invalid, EXIT_FAILURE); + tcase_add_test(tc, test_opt_cgroup_cb); + tcase_add_test(tc, test_opt_cgroup_cb_equals); + tcase_add_test(tc, test_opt_duration_cb); + tcase_add_exit_test(tc, test_opt_duration_cb_invalid, EXIT_FAILURE); + tcase_add_test(tc, test_opt_event_cb); + tcase_add_test(tc, test_opt_event_cb_multiple); + tcase_add_test(tc, test_opt_housekeeping_cb); + tcase_add_exit_test(tc, test_opt_housekeeping_cb_invalid, EXIT_FAILURE); + tcase_add_test(tc, test_opt_priority_cb); + tcase_add_exit_test(tc, test_opt_priority_cb_invalid, EXIT_FAILURE); + tcase_add_test(tc, test_opt_trigger_cb); + tcase_add_exit_test(tc, test_opt_trigger_cb_no_event, EXIT_FAILURE); + tcase_add_test(tc, test_opt_filter_cb); + tcase_add_exit_test(tc, test_opt_filter_cb_no_event, EXIT_FAILURE); + suite_add_tcase(s, tc); + + tc =3D tcase_create("osnoise"); + tcase_add_test(tc, test_opt_osnoise_auto_cb); + tcase_add_test(tc, test_opt_osnoise_period_cb); + tcase_add_exit_test(tc, test_opt_osnoise_period_cb_invalid, EXIT_FAILURE); + tcase_add_test(tc, test_opt_osnoise_runtime_cb); + tcase_add_exit_test(tc, test_opt_osnoise_runtime_cb_invalid, EXIT_FAILURE= ); + tcase_add_test(tc, test_opt_osnoise_trace_output_cb); + tcase_add_test(tc, test_opt_osnoise_trace_output_cb_noarg); + tcase_add_test(tc, test_opt_osnoise_on_threshold_cb); + tcase_add_exit_test(tc, test_opt_osnoise_on_threshold_cb_invalid, EXIT_FA= ILURE); + tcase_add_test(tc, test_opt_osnoise_on_end_cb); + tcase_add_exit_test(tc, test_opt_osnoise_on_end_cb_invalid, EXIT_FAILURE); + suite_add_tcase(s, tc); + + tc =3D tcase_create("timerlat"); + tcase_add_test(tc, test_opt_timerlat_period_cb); + tcase_add_exit_test(tc, test_opt_timerlat_period_cb_invalid, EXIT_FAILURE= ); + tcase_add_test(tc, test_opt_timerlat_auto_cb); + tcase_add_test(tc, test_opt_dma_latency_cb); + tcase_add_exit_test(tc, test_opt_dma_latency_cb_min, EXIT_FAILURE); + tcase_add_exit_test(tc, test_opt_dma_latency_cb_max, EXIT_FAILURE); + tcase_add_test(tc, test_opt_aa_only_cb); + tcase_add_test(tc, test_opt_timerlat_trace_output_cb); + tcase_add_test(tc, test_opt_timerlat_trace_output_cb_noarg); + tcase_add_test(tc, test_opt_timerlat_on_threshold_cb); + tcase_add_exit_test(tc, test_opt_timerlat_on_threshold_cb_invalid, EXIT_F= AILURE); + tcase_add_test(tc, test_opt_timerlat_on_end_cb); + tcase_add_exit_test(tc, test_opt_timerlat_on_end_cb_invalid, EXIT_FAILURE= ); + tcase_add_test(tc, test_opt_user_threads_cb); + tcase_add_test(tc, test_opt_nano_cb); + tcase_add_test(tc, test_opt_stack_format_cb); + tcase_add_exit_test(tc, test_opt_stack_format_cb_invalid, EXIT_FAILURE); + suite_add_tcase(s, tc); + + tc =3D tcase_create("histogram"); + tcase_add_test(tc, test_opt_bucket_size_cb); + tcase_add_exit_test(tc, test_opt_bucket_size_min, EXIT_FAILURE); + tcase_add_exit_test(tc, test_opt_bucket_size_max, EXIT_FAILURE); + tcase_add_test(tc, test_opt_entries_cb); + tcase_add_exit_test(tc, test_opt_entries_min, EXIT_FAILURE); + tcase_add_exit_test(tc, test_opt_entries_max, EXIT_FAILURE); + suite_add_tcase(s, tc); + + return s; +} diff --git a/tools/tracing/rtla/tests/unit/unit_tests.c b/tools/tracing/rtl= a/tests/unit/unit_tests.c index 64884f6cbdeb..75ca813f81ca 100644 --- a/tools/tracing/rtla/tests/unit/unit_tests.c +++ b/tools/tracing/rtla/tests/unit/unit_tests.c @@ -13,6 +13,7 @@ Suite *osnoise_top_cli_suite(void); Suite *osnoise_hist_cli_suite(void); Suite *timerlat_top_cli_suite(void); Suite *timerlat_hist_cli_suite(void); +Suite *cli_opt_callback_suite(void); =20 int main(int argc, char *argv[]) { @@ -22,6 +23,7 @@ int main(int argc, char *argv[]) in_unit_test =3D true; =20 sr =3D srunner_create(utils_suite()); + srunner_add_suite(sr, cli_opt_callback_suite()); srunner_add_suite(sr, actions_suite()); srunner_add_suite(sr, osnoise_top_cli_suite()); srunner_add_suite(sr, osnoise_hist_cli_suite()); --=20 2.54.0