Skip to contentSkip to conversation

rtla: Migrate to libsubcmd for command line option parsing

PATCH Superseded Tomas Glozar 4 participants 11 messages Mar 20 – Apr 25

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Tomas Glozar

author maintainer
#0 lore raw

cover rtla: Migrate to libsubcmd for command line option parsing

[ CC to linux-perf-users for the libsubcmd code change (second commit) ]

rtla currently uses its own implementation that uses getopt_long() to parse command-line arguments.

Migrate rtla to use libsubcmd for command line argument parsing, similarly to what is already done by other tools like perf, bpftool, and objtool. Among other benefits, this allows help messages to be generated automatically rather then having to by typed out manually for each tool.

libsubcmd is extended with an option to parse optarg from separate argument if a new flag is turned on. Without the flag, the old behavior is preserved. That keeps the parsing working for tools that use positional arguments, and allows RTLA to keep its flexible syntax for -C and -t options and their long variants, --cgroup and --trace-output.

The new implementation is moved into a separate file, cli.c, together with a tiny header counterpart, cli.h. This helps separate the parsing logic, which has little in common with the rest of RTLA, in a separate module.

Macros to generate struct option array fields for libsubcmd's parse_args() are used to preserve the consolidation of argument parsing code across different RTLA tools. Kernel and user threads are, as an exception, treated as common, although they are currently implemented for timerlat only, in line with earlier consolidation changes.

I expect more improvements to the code being possible in the future, like creating macros for option groups to further deduplicate the code, or reduce the amount of extra code in the _parse_args() functions.

Tomas Glozar (3):
rtla: Add libsubcmd dependency
tools subcmd: support optarg as separate argument
rtla: Parse cmdline using libsubcmd

 tools/lib/subcmd/parse-options.c       |   53 +-
 tools/lib/subcmd/parse-options.h       |    1 +
 tools/tracing/rtla/.gitignore          |    1 +
 tools/tracing/rtla/Makefile            |   53 +-
 tools/tracing/rtla/src/Build           |    2 +-
 tools/tracing/rtla/src/cli.c           | 1207 ++++++++++++++++++++++++
 tools/tracing/rtla/src/cli.h           |    7 +
 tools/tracing/rtla/src/common.c        |  109 ---
 tools/tracing/rtla/src/common.h        |   26 +-
 tools/tracing/rtla/src/osnoise_hist.c  |  221 +----
 tools/tracing/rtla/src/osnoise_top.c   |  200 +---
 tools/tracing/rtla/src/rtla.c          |   89 --
 tools/tracing/rtla/src/timerlat.h      |    4 +-
 tools/tracing/rtla/src/timerlat_hist.c |  317 +------
 tools/tracing/rtla/src/timerlat_top.c  |  285 +-----
 tools/tracing/rtla/src/utils.c         |   28 +-
 tools/tracing/rtla/src/utils.h         |    9 +-
 tools/tracing/rtla/tests/hwnoise.t     |    2 +-
 18 files changed, 1331 insertions(+), 1283 deletions(-)
 create mode 100644 tools/tracing/rtla/src/cli.c
 create mode 100644 tools/tracing/rtla/src/cli.h
 delete mode 100644 tools/tracing/rtla/src/rtla.c
Signature · 1 line
2.53.0
author maintainer
#1 lore raw

PATCH 1/3 rtla: Add libsubcmd dependency

In preparation to 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 function and macros shipped with the kernel to be used in RTLA; perf and bpftool, two other users of libsubcmd, already do that.

To prevent 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
---
 tools/tracing/rtla/.gitignore  |  1 +
 tools/tracing/rtla/Makefile    | 53 +++++++++++++++++++++++++++++-----
 tools/tracing/rtla/src/rtla.c  |  8 ++---
 tools/tracing/rtla/src/utils.h |  6 ++--
 4 files changed, 53 insertions(+), 15 deletions(-)
tools/tracing/rtla/.gitignore +1 -0
index 4d39d64ac08c..123c2d5ed7ac 100644
@@ -9,3 +9,4 @@ custom_filename.txt
 osnoise_irq_noise_hist.txt
 osnoise_trace.txt
 timerlat_trace.txt
+libsubcmd/
tools/tracing/rtla/Makefile +46 -7

79 lines not on this page · raw message

tools/tracing/rtla/src/rtla.c +4 -4
index 7635c70123ab..845932f902ef 100644
@@ -38,12 +38,12 @@ static void rtla_usage(int err)
 }
 
 /*
- * 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") == 0) {
 		osnoise_main(argc-start_position, &argv[start_position]);
@@ -66,7 +66,7 @@ int main(int argc, char *argv[])
 	int retval;
 
 	/* is it an alias? */
-	retval = run_command(argc, argv, 0);
+	retval = run_tool_command(argc, argv, 0);
 	if (retval)
 		exit(0);
 
@@ -79,7 +79,7 @@ int main(int argc, char *argv[])
 		rtla_usage(0);
 	}
 
-	retval = run_command(argc, argv, 1);
+	retval = run_tool_command(argc, argv, 1);
 	if (retval)
 		exit(0);
 
tools/tracing/rtla/src/utils.h +2 -4
index e794ede64b2c..96fd72042717 100644
@@ -7,6 +7,8 @@
 #include <stdbool.h>
 #include <stdlib.h>
 
+#include <linux/container_of.h>
+
 /*
  * '18446744073709551615\0'
  */
@@ -37,10 +39,6 @@ static inline bool str_has_prefix(const char *str, const char *prefix)
 	return strncmp(str, prefix, strlen(prefix)) == 0;
 }
 
-#define container_of(ptr, type, member)({			\
-	const typeof(((type *)0)->member) *__mptr = (ptr);	\
-	(type *)((char *)__mptr - offsetof(type, member)) ; })
-
 extern int config_debug;
 void debug_msg(const char *fmt, ...);
 void err_msg(const char *fmt, ...);
Signature · 1 line
2.53.0
author maintainer
#3 lore raw

PATCH 2/3 tools subcmd: support optarg as separate argument

In reply to Tomas Glozar

In addition to "-ovalue" and "--opt=value" 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.

---
 tools/lib/subcmd/parse-options.c | 53 +++++++++++++++++++++++++++-----
 tools/lib/subcmd/parse-options.h |  1 +
 2 files changed, 46 insertions(+), 8 deletions(-)
tools/lib/subcmd/parse-options.c +45 -8
index 555d617c1f50..664b2053bb77 100644
@@ -72,6 +72,7 @@ static int get_value(struct parse_opt_ctx_t *p,
 	const char *s, *arg = NULL;
 	const int unset = flags & OPT_UNSET;
 	int err;
+	bool force_defval = false;
 
 	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,
 		}
 	}
 
+	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=val".
+			 * 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 = true;
+		}
+
+		if (p->argc <= 1 || p->argv[1][0] == '-') {
+			/*
+			 * If next argument is an option or does not exist,
+			 * use the default value.
+			 */
+			force_defval = true;
+		}
+	}
+
 	if (opt->flags & PARSE_OPT_NOBUILD) {
 		char reason[128];
 		bool noarg = false;
@@ -148,7 +185,7 @@ static int get_value(struct parse_opt_ctx_t *p,
 			noarg = true;
 		if (opt->flags & PARSE_OPT_NOARG)
 			noarg = true;
-		if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
+		if (force_defval)
 			noarg = true;
 
 		switch (opt->type) {
@@ -212,7 +249,7 @@ static int get_value(struct parse_opt_ctx_t *p,
 		err = 0;
 		if (unset)
 			*(const char **)opt->value = NULL;
-		else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
+		else if (force_defval)
 			*(const char **)opt->value = (const char *)opt->defval;
 		else
 			err = 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 = 0;
 			return 0;
 		}
-		if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
+		if (force_defval) {
 			*(int *)opt->value = opt->defval;
 			return 0;
 		}
@@ -271,7 +308,7 @@ static int get_value(struct parse_opt_ctx_t *p,
 			*(unsigned int *)opt->value = 0;
 			return 0;
 		}
-		if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
+		if (force_defval) {
 			*(unsigned int *)opt->value = opt->defval;
 			return 0;
 		}
@@ -289,7 +326,7 @@ static int get_value(struct parse_opt_ctx_t *p,
 			*(long *)opt->value = 0;
 			return 0;
 		}
-		if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
+		if (force_defval) {
 			*(long *)opt->value = opt->defval;
 			return 0;
 		}
@@ -305,7 +342,7 @@ static int get_value(struct parse_opt_ctx_t *p,
 			*(unsigned long *)opt->value = 0;
 			return 0;
 		}
-		if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
+		if (force_defval) {
 			*(unsigned long *)opt->value = opt->defval;
 			return 0;
 		}
@@ -321,7 +358,7 @@ static int get_value(struct parse_opt_ctx_t *p,
 			*(u64 *)opt->value = 0;
 			return 0;
 		}
-		if (opt->flags & PARSE_OPT_OPTARG && !p->opt) {
+		if (force_defval) {
 			*(u64 *)opt->value = opt->defval;
 			return 0;
 		}
tools/lib/subcmd/parse-options.h +1 -0
index 8e9147358a28..c573a0ca5ca6 100644
@@ -33,6 +33,7 @@ enum parse_opt_flags {
 	PARSE_OPT_KEEP_ARGV0 = 4,
 	PARSE_OPT_KEEP_UNKNOWN = 8,
 	PARSE_OPT_NO_INTERNAL_HELP = 16,
+	PARSE_OPT_OPTARG_ALLOW_NEXT = 32,
 };
 
 enum parse_opt_option_flags {
Signature · 1 line
2.53.0
author maintainer
#4 lore raw

PATCH 3/3 rtla: Parse cmdline using libsubcmd

In reply to Tomas Glozar

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.

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 remains the same.

Assisted-by Composer:composer-1.5
---
 tools/tracing/rtla/src/Build           |    2 +-
 tools/tracing/rtla/src/cli.c           | 1207 ++++++++++++++++++++++++
 tools/tracing/rtla/src/cli.h           |    7 +
 tools/tracing/rtla/src/common.c        |  109 ---
 tools/tracing/rtla/src/common.h        |   26 +-
 tools/tracing/rtla/src/osnoise_hist.c  |  221 +----
 tools/tracing/rtla/src/osnoise_top.c   |  200 +---
 tools/tracing/rtla/src/rtla.c          |   89 --
 tools/tracing/rtla/src/timerlat.h      |    4 +-
 tools/tracing/rtla/src/timerlat_hist.c |  317 +------
 tools/tracing/rtla/src/timerlat_top.c  |  285 +-----
 tools/tracing/rtla/src/utils.c         |   28 +-
 tools/tracing/rtla/src/utils.h         |    3 +-
 tools/tracing/rtla/tests/hwnoise.t     |    2 +-
 14 files changed, 1236 insertions(+), 1264 deletions(-)
 create mode 100644 tools/tracing/rtla/src/cli.c
 create mode 100644 tools/tracing/rtla/src/cli.h
 delete mode 100644 tools/tracing/rtla/src/rtla.c
tools/tracing/rtla/src/Build +1 -1
index 329e24a40cf7..a1f3ab927207 100644
@@ -11,4 +11,4 @@ rtla-y += timerlat_hist.o
 rtla-y += timerlat_u.o
 rtla-y += timerlat_aa.o
 rtla-y += timerlat_bpf.o
-rtla-y += rtla.o
+rtla-y += cli.o
tools/tracing/rtla/src/cli.c new file +1207 -0

1,207 lines not on this page · raw message

tools/tracing/rtla/src/cli.h new file +7 -0
new file mode 100644
index 000000000000..c49ccb3e92f5
@@ -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);
tools/tracing/rtla/src/common.c +0 -109

121 lines not on this page · raw message

tools/tracing/rtla/src/common.h +12 -14
index 51665db4ffce..27439b10ffd5 100644
@@ -1,7 +1,6 @@
 /* SPDX-License-Identifier: GPL-2.0 */
 #pragma once
 
-#include <getopt.h>
 #include "actions.h"
 #include "timerlat_u.h"
 #include "trace.h"
@@ -58,12 +57,12 @@ extern struct trace_instance *trace_inst;
 extern volatile int stop_tracing;
 
 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;
 };
@@ -96,12 +95,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;
 
 	struct actions		threshold_actions;
 	struct actions		end_actions;
@@ -177,7 +176,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);
 
-int getopt_auto(int argc, char **argv, const struct option *long_opts);
 int common_parse_options(int argc, char **argv, struct common_params *common);
 int common_apply_config(struct osnoise_tool *tool, struct common_params *params);
 int top_main_loop(struct osnoise_tool *tool);
tools/tracing/rtla/src/osnoise_hist.c +1 -220

239 lines not on this page · raw message

tools/tracing/rtla/src/osnoise_top.c +1 -199

218 lines not on this page · raw message

tools/tracing/rtla/src/rtla.c deleted +0 -89

89 lines not on this page · raw message

tools/tracing/rtla/src/timerlat.h +2 -2
index 364203a29abd..37a808f1611e 100644
@@ -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;
tools/tracing/rtla/src/timerlat_hist.c +1 -316

335 lines not on this page · raw message

tools/tracing/rtla/src/timerlat_top.c +1 -284

303 lines not on this page · raw message

tools/tracing/rtla/src/utils.c +1 -27
index 9cec5b3e02c8..cb187e7d48d1 100644
@@ -22,7 +22,7 @@
 #include "common.h"
 
 #define MAX_MSG_LENGTH	1024
-int config_debug;
+bool config_debug;
 
 /*
  * err_msg - print an error message to the stderr
@@ -1011,32 +1011,6 @@ int auto_house_keeping(cpu_set_t *monitored_cpus)
 	return 1;
 }
 
-/**
- * parse_optional_arg - Parse optional argument value
- *
- * Parse optional argument value, which can be in the form of:
- * -sarg, -s/--long=arg, -s/--long arg
- *
- * Returns arg value if found, NULL otherwise.
- */
-char *parse_optional_arg(int argc, char **argv)
-{
-	if (optarg) {
-		if (optarg[0] == '=') {
-			/* skip the = */
-			return &optarg[1];
-		} else {
-			return optarg;
-		}
-	/* parse argument of form -s [arg] and --long [arg]*/
-	} else if (optind < argc && argv[optind][0] != '-') {
-		/* consume optind */
-		return argv[optind++];
-	} else {
-		return NULL;
-	}
-}
-
 /*
  * strtoi - convert string to integer with error checking
  *
tools/tracing/rtla/src/utils.h +1 -2
index 96fd72042717..2ba3333669bb 100644
@@ -39,7 +39,7 @@ static inline bool str_has_prefix(const char *str, const char *prefix)
 	return strncmp(str, prefix, strlen(prefix)) == 0;
 }
 
-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);
 
-char *parse_optional_arg(int argc, char **argv);
 long long get_llong_from_str(char *start);
 
 static inline void
tools/tracing/rtla/tests/hwnoise.t +1 -1
index 23ce250a6852..cfe687ff5ee1 100644
@@ -6,7 +6,7 @@ test_begin
 set_timeout 2m
 
 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" \
Signature · 1 line
2.53.0
#5 lore raw

Re: [PATCH 3/3] rtla: Parse cmdline using libsubcmd

Quoted 18 lines · Tomas Glozar

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

[snip]

Quoted 2 lines

-int getopt_auto(int argc, char **argv, const struct option *long_opts);
int common_parse_options(int argc, char **argv, struct common_params *common);

The function common_parse_options() body was removed, but the declaration remains.

Quoted 1 line

int common_apply_config(struct osnoise_tool *tool, struct common_params *params);

[snip]

#7 lore raw

Re: [PATCH 3/3] rtla: Parse cmdline using libsubcmd

In reply to Tomas Glozar

Review

Quoted 3 lines · Tomas Glozar
Quoted 1 line

+#define TIMERLAT_OPT_NANO OPT_CALLBACK('n', "nano", params, NULL, \

(quoted diff) +2 -0
+       "display data in nanoseconds", \
+       opt_nano_cb)

-n/--nano requires value incorrectly

File: src/cli.c:463
Cause: TIMERLAT_OPT_NANO used OPT_CALLBACK which expects an argument,
but -n is a flag.
Fix: Changed to OPT_CALLBACK_NOOPT:

Quoted 1 line

+ HIST_OPT_NO_IRQ,

--no-irq clashes with auto-negation of --irq

File: src/cli.c:1042
Cause: libsubcmd auto-generates --no-X negations for every option.
--no-irq (histogram boolean) collides with the auto-negation of --irq (stop threshold). The first match wins, so --irq was matched first and its negation intercepted the call.
Fix: Moved HIST_OPT_NO_IRQ before RTLA_OPT_STOP('i', "irq", ...) in
the options array so the explicit --no-irq boolean is found first.

Quoted 1 line

+ HIST_OPT_NO_THREAD,

--no-thread - same issue

Cause: Same collision between --no-thread boolean and auto-negation
of --thread.
Fix: Same reordering approach.

Costa and Claude

tools/tracing/rtla/src/cli.c +0 -0
index d029a698e8a7..c6b9ed920539 100644
@@ -460,7 +460,7 @@ static int opt_osnoise_on_end_cb(const struct

option *opt, const char *arg, int
"save the stack trace at the IRQ if a thread latency is higher than
the argument in us", \
opt_llong_callback)

-#define TIMERLAT_OPT_NANO OPT_CALLBACK('n', "nano", params, NULL, \
+#define TIMERLAT_OPT_NANO OPT_CALLBACK_NOOPT('n', "nano", params, NULL, \
"display data in nanoseconds", \
opt_nano_cb)

@@ -1011,6 +1011,12 @@ struct common_params
*timerlat_hist_parse_args(int argc, char **argv)
cb_data.trace_output = NULL;

const struct option timerlat_hist_options[] = {
+ OPT_GROUP("Histogram Options:"),
+ HIST_OPT_NO_IRQ,
+ HIST_OPT_NO_THREAD,
+ HIST_OPT_BUCKET_SIZE,
+ HIST_OPT_ENTRIES,
+
OPT_GROUP("Tracing Options:"),
TIMERLAT_OPT_PERIOD,
RTLA_OPT_STOP('i', "irq", "irq latency"),
@@ -1034,11 +1040,7 @@ struct common_params
*timerlat_hist_parse_args(int argc, char **argv)
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,
+ OPT_GROUP(""),
HIST_OPT_NO_HEADER,
HIST_OPT_NO_SUMMARY,
HIST_OPT_NO_INDEX,

author maintainer
#6 lore raw

Re: [PATCH 3/3] rtla: Parse cmdline using libsubcmd

In reply to Wander Lairson Costa

pá 20. 3. 2026 v 18:32 odesílatel Wander Lairson Costa <[email protected]> napsal:

Quoted 6 lines

[snip]

Quoted 2 lines

-int getopt_auto(int argc, char **argv, const struct option *long_opts);
int common_parse_options(int argc, char **argv, struct common_params *common);

Quoted 6 lines

The function common_parse_options() body was removed, but the declaration remains.

Quoted 1 line

int common_apply_config(struct osnoise_tool *tool, struct common_params *params);

[snip]

Thanks for noticing that, I will fix that in v2 that will be needed either way.

Tomas

author maintainer
#8 lore raw

Re: [PATCH 3/3] rtla: Parse cmdline using libsubcmd

In reply to Costa Shulyupin

so 21. 3. 2026 v 17:08 odesílatel Costa Shulyupin
<[email protected]> napsal:

Quoted 8 lines · Tomas Glozar

Review

Quoted 3 lines
Quoted 1 line

+#define TIMERLAT_OPT_NANO OPT_CALLBACK('n', "nano", params, NULL, \

(quoted diff) +2 -0
+       "display data in nanoseconds", \
+       opt_nano_cb)

-n/--nano requires value incorrectly

Yes, this is a mistake.

Quoted 8 lines

File: src/cli.c:463
Cause: TIMERLAT_OPT_NANO used OPT_CALLBACK which expects an argument,
but -n is a flag.
Fix: Changed to OPT_CALLBACK_NOOPT:

Quoted 1 line

+ HIST_OPT_NO_IRQ,

--no-irq clashes with auto-negation of --irq

Quoted 8 lines

File: src/cli.c:1042
Cause: libsubcmd auto-generates --no-X negations for every option.
--no-irq (histogram boolean) collides with the auto-negation of --irq (stop threshold). The first match wins, so --irq was matched first and its negation intercepted the call.
Fix: Moved HIST_OPT_NO_IRQ before RTLA_OPT_STOP('i', "irq", ...) in
the options array so the explicit --no-irq boolean is found first.

Thank you for reporting that, I had no idea libsubcmd does this, I haven't seen it documented anywhere, and I never used it in either perf or bpftool.

It is a very strange thing to do for non-boolean options in my opinion. If I understand the intention correctly, the option is intended to unset another option specified before it, i.e. this:

$ ./rtla timerlat hist --event=timer --no-event

is supposed to behave the same as:

$ ./rtla timerlat hist

But it doesn't work:

$ ./rtla timerlat hist --event=timer --no-event

Usage: rtla timerlat hist [<options>]

perf fails even harder on this:

$ ./perf version
perf version 7.0.rc2.g61637af4f771
$ ./perf record --event=cycles --no-event
Segmentation fault (core dumped)

I'm thinking about modifying libsubcmd so this can be at least disabled (not sure if the behavior is even intended in the non-boolean case). Reshuffling the RTLA option array could work, too, but is unintuitive from the developer's point of view, and will also force histogram options to be before threshold options in the help message, which is not what I wanted.

Tomas

author maintainer
#9 lore raw

Re: [PATCH 3/3] rtla: Parse cmdline using libsubcmd

po 23. 3. 2026 v 15:26 odesílatel Tomas Glozar <[email protected]> napsal:

Quoted 10 lines
Quoted 10 lines

--no-irq clashes with auto-negation of --irq

File: src/cli.c:1042
Cause: libsubcmd auto-generates --no-X negations for every option.
--no-irq (histogram boolean) collides with the auto-negation of --irq (stop threshold). The first match wins, so --irq was matched first and its negation intercepted the call.
Fix: Moved HIST_OPT_NO_IRQ before RTLA_OPT_STOP('i', "irq", ...) in
the options array so the explicit --no-irq boolean is found first.

Quoted 6 lines

...
Reshuffling the RTLA option array could work, too, but is unintuitive from the developer's point of view, and will also force histogram options to be before threshold options in the help message, which is not what I wanted.

Actually, it doesn't. Just as defining --irq implicitly defines --no-irq, defining --no-irq implicitly defines --irq, so the long option --irq stops working when putting HIST_OPT_NO_IRQ before RTLA_OPT_STOP('i', "irq", "irq latency").

So the only solution appears to be to add a flag to libsubcmd that can disable this behavior (auto-parsing the counterpart of --opt/--no-opt) in a new patch in v2. Since this patchset already has a change to libsubcmd and CC to linux-perf-users, that should work.

Tomas

#2 lore raw

Re: [PATCH 1/3] rtla: Add libsubcmd dependency

In reply to Tomas Glozar
Sashiko AI review found 2 potential issue(s): - [Low] The variable `LIBSUBCMD_INCLUDES` is incorrectly used as both a compiler flag and a Make target, causing unnecessary sub-make executions.…

Sashiko AI review found 2 potential issue(s):
- [Low] The variable LIBSUBCMD_INCLUDES is incorrectly used as both a compiler flag and a Make target, causing unnecessary sub-make executions.
- [Low] The manual compilation rules for string.o and str_error_r.o bypass the standard fixdep dependency tracking mechanism. --

commit 61af24c97cebeb1e051afb96f7fccb2a5c359a72
Author: Tomas Glozar <[email protected]>

rtla: Add libsubcmd dependency

This commit prepares RTLA to migrate to libsubcmd by building and linking the library alongside the RTLA build. It also links respective libraries from tools/lib and adds tools/include to the include path.

Quoted 16 lines
tools/tracing/rtla/Makefile +8 -0
index 45690ee14544d..289e44c9664b2 100644
@@ -27,6 +27,24 @@ endif
 RTLA		:= $(OUTPUT)rtla
 RTLA_IN		:= $(RTLA)-in.o
 
+LIBSUBCMD_DIR = $(srctree)/tools/lib/subcmd/
+ifneq ($(OUTPUT),)
+  LIBSUBCMD_OUTPUT = $(abspath $(OUTPUT))/libsubcmd
+else
+  LIBSUBCMD_OUTPUT = $(CURDIR)/libsubcmd
+endif
+LIBSUBCMD = $(LIBSUBCMD_OUTPUT)/libsubcmd.a
+LIBSUBCMD_INCLUDES = -I$(LIBSUBCMD_OUTPUT)/include

[ ... ]

tools/tracing/rtla/Makefile tests/bpf/bpf_action_map.o: tests/bpf/bpf_action_map.c · line 132

Quoted 11 lines

-$(RTLA_IN): fixdep FORCE src/timerlat.skel.h +$(RTLA_IN): fixdep FORCE src/timerlat.skel.h $(LIBSUBCMD_INCLUDES)

tools/tracing/rtla/Makefile +6 -1
 	make $(build)=rtla
 
-clean: doc_clean fixdep-clean
+$(LIBSUBCMD_OUTPUT):
+	$(Q)$(MKDIR) -p $@
+
+$(LIBSUBCMD_INCLUDES): $(LIBSUBCMD_OUTPUT)
+	$(Q)$(MAKE) -C $(LIBSUBCMD_DIR) $(LIBSUBCMD_MAKEFLAGS) \
+		install_headers

Does this unintentionally cause the install_headers sub-make to run on every build?

Since LIBSUBCMD_INCLUDES expands to the compiler flag string -I$(LIBSUBCMD_OUTPUT)/include, make interprets this literal string as a target. Because a file named "-I..." is never actually created by the install_headers step, make considers the target permanently out-of-date and executes this recipe unconditionally on every invocation.

tools/tracing/rtla/Makefile tests/bpf/bpf_action_map.o: tests/bpf/bpf_action_map.c · line 142

Quoted 9 lines

+$(LIBSUBCMD): fixdep $(LIBSUBCMD_OUTPUT)

tools/tracing/rtla/Makefile +8 -0
+	$(Q)$(MAKE) -C $(LIBSUBCMD_DIR) $(LIBSUBCMD_MAKEFLAGS) \
+		$@
+
+$(LIB_STR_ERROR_R): $(LIB_STR_ERROR_R_SRC)
+	$(QUIET_CC)$(CC) $(CFLAGS) -c -o $@ $<
+
+$(LIB_STRING): $(LIB_STRING_SRC)
+	$(QUIET_CC)$(CC) $(CFLAGS) -c -o $@ $<

Do these manual compilation rules bypass dependency tracking?

These targets invoke the compiler directly without using the standard tools/build/Makefile.build infrastructure. By omitting the necessary compiler flags to generate dependency files and skipping the fixdep post-processing, updates to any headers included by string.c or str_error_r.c will not automatically trigger a rebuild of these object files.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#10 lore raw

Re: [PATCH 3/3] rtla: Parse cmdline using libsubcmd

In reply to Tomas Glozar
Sashiko AI review found 2 potential issue(s): - [High] Segmentation fault when parsing abbreviated help options (`--he`, `--hel`) due to `RTLA_OPT_HELP` macro definition. - [High] Options…

Sashiko AI review found 2 potential issue(s):
- [High] Segmentation fault when parsing abbreviated help options (--he, --hel) due to RTLA_OPT_HELP macro definition.
- [High] Options -u/--user-threads and -n/--nano incorrectly swallow subsequent arguments or fail when placed at the end of the command line. --

commit 7b68f92828ddb67d26e43dca96fdb16d529c279d
Author: Tomas Glozar <[email protected]>

rtla: Parse cmdline using libsubcmd

This commit moves RTLA command line parsing from raw getopt_long() to libsubcmd's parse_options(). It defines reusable macros for common and tool-specific options.

Quoted 4 lines
tools/tracing/rtla/src/cli.c new file +0 -0
new file mode 100644

[ ... ]

tools/tracing/rtla/src/cli.c line 101

Quoted 8 lines
tools/tracing/rtla/src/cli.c new file +8 -0
+#define RTLA_OPT_CGROUP OPT_CALLBACK_OPTARG('C', "cgroup", &params->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('u', "user-threads", params, NULL, \
+	"use rtla user-space threads instead of kernel-space timerlat threads", \
+	opt_user_threads_cb)

Can this swallow subsequent command-line arguments or fail at the end of the argument list?

Since OPT_CALLBACK is used here without PARSE_OPT_NOARG, libsubcmd's get_arg() will strictly expect a value. If someone runs
"rtla timerlat top -u -p 100", the "-p" would be consumed as the argument to "-u". If "-u" is placed at the end of the command line, it fails expecting a value.

Could OPT_CALLBACK_NOOPT or OPT_BOOLEAN be used here instead?

[ ... ]

tools/tracing/rtla/src/cli.c line 171

Quoted 5 lines
tools/tracing/rtla/src/cli.c new file +5 -0
+#define RTLA_OPT_DEBUG OPT_BOOLEAN('D', "debug", &config_debug, \
+	"print debug info")
+
+#define RTLA_OPT_HELP OPT_BOOLEAN('h', "help", (bool *)NULL, \
+	"show help")

Can this cause a segmentation fault if an abbreviated help option is provided?

While exact matches for "-h" and "--help" are intercepted early by libsubcmd, partial matches like "--he", "--hel", or negated "--no-help" bypass the early check and perform standard prefix matching. This leads to get_value() being called for the boolean option, which attempts to dereference the NULL value pointer.

[ ... ]

tools/tracing/rtla/src/cli.c line 465

Quoted 7 lines
tools/tracing/rtla/src/cli.c new file +7 -0
+#define TIMERLAT_OPT_STACK OPT_CALLBACK('s', "stack", &params->print_stack, "us", \
+	"save the stack trace at the IRQ if a thread latency is higher than the argument in us", \
+	opt_llong_callback)
+
+#define TIMERLAT_OPT_NANO OPT_CALLBACK('n', "nano", params, NULL, \
+	"display data in nanoseconds", \
+	opt_nano_cb)

Does this option also unintentionally consume the following argument?

Similar to RTLA_OPT_USER_THREADS, using OPT_CALLBACK without a flag preventing argument consumption causes libsubcmd to require a value. Could OPT_CALLBACK_NOOPT be used to ensure the flag behaves correctly as a toggle?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3