rtla: Migrate to libsubcmd for command line option parsing
Subsystems (3)
Tomas Glozar
maintainerv3 cover rtla: Migrate to libsubcmd for command line option parsing
[ CC to linux-perf-users for the libsubcmd code changes ]
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 than having to be typed out manually for each tool.
libsubcmd is extended with a flag 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. Another flag is added to disable automatic definition of --no-xy for every option --xy and vice versa, which overlaps for RTLA's --irq and --thread options.
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. Another new file, cli_p.h, is used as a private header to contain macros and static function declarations that are also used by unit tests next to cli.c, but should not be imported from elsewhere.
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.
The test suite is expanded to include two levels of unit tests, one testing the already existing tool_parse_args() functions, one tests option callbacks, which are a new level of the CLI parser added in this patchset. This helps to verify that no regressions are caused by this refactoring.
I expect more improvements to the code being possible in the future, like creating macros for option groups to further deduplicate the code, reducing the amount of extra code in the _parse_args() functions, or implementing support for unsetting options (which is currently only supported for those that do not use a custom callback).
Dependencies:
- https://lore.kernel.org/linux-trace-kernel/[email protected]/T/
- https://lore.kernel.org/linux-trace-kernel/[email protected]/
- https://lore.kernel.org/linux-trace-kernel/[email protected]/
(apply in reverse order, alternatively, use base commit above)
v3 changes (all in cover letter or first commit):
- Add FORCE to all targets that feature a make subcommand to ensure changes in the dependency will trigger rebuild correctly.
- Convert all dependencies on directory targets LIBSUBCMD_OUTPUT and LIB_OUTPUT into order-only prerequisites to prevent modifications of the directory content triggering rebuild of targets inside it.
- Properly mention depedencies of the patchset in the cover letter, as well as the base commit.
v2: https://lore.kernel.org/linux-trace-kernel/[email protected]/T/
v2 changes:
- Two unit test suites are added to cover regressions, after several options were broken by the v1. The test suites cover all parsing issues reported in the v1 as well as those found during additional testing.
- Return value of all paths that print help, including those that are handled in RTLA, are set to libsubcmd's help exit code of 129. Previously, only the tool help returned 129. While some other tools (e.g. bpftool) do that, RTLA unifies those for consistency. The return value is also added to the corresponding section in documentation.
- Incorrect parsing of --no-irq and --no-thread is fixed using a newly added libsubcmd option flag.
- Incorrect parsing of -n and -u timerlat options (which erroneously required an argument in v1) is fixed.
- A now stale declaration of removed function common_parse_options() is removed from common.h.
- Segmentation fault on abbreviated --help (e.g. --he) is fixed.
- Incorrect formatting of OPT_END macro (spurious tab) is fixed.
- All opt_* callbacks now reject unimplemented unset (--no-) correctly.
- opt_trigger_cb() and opt_filter_cb() now take only the required events field, not the whole params structure.
- Spurious opt_osnoise_threshold_cb() which is actually just a wrapper for opt_llong_callback() is removed.
- Old off-by-one typo is fixed in --dma-latency and -E/--entries error messages, to make it consistent with the newly added unit tests.
- Fix a bug in Makefile that defined LIBSUBCMD_INCLUDES as -I... and then used it as a target name: define it as the path only, and then add -I$(...) to CFLAGS, as this is the only include path that is generated during the build itself.
v1: https://lore.kernel.org/linux-trace-kernel/[email protected]/T
Tomas Glozar (6):
rtla: Add libsubcmd dependency
tools subcmd: support optarg as separate argument tools subcmd: allow parsing distinct --opt and --no-opt
rtla: Parse cmdline using libsubcmd rtla/tests: Add unit tests for _parse_args() functions rtla/tests: Add unit tests for CLI option callbacks
Documentation/tools/rtla/common_appendix.txt | 7 +- tools/lib/subcmd/parse-options.c | 63 +- tools/lib/subcmd/parse-options.h | 4 + tools/tracing/rtla/.gitignore | 2 + tools/tracing/rtla/Makefile | 66 +- tools/tracing/rtla/src/Build | 2 +- tools/tracing/rtla/src/cli.c | 537 +++++++++++++ tools/tracing/rtla/src/cli.h | 9 + 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 | 6 +- 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 | 9 +- tools/tracing/rtla/tests/hwnoise.t | 2 +- tools/tracing/rtla/tests/osnoise.t | 6 +- tools/tracing/rtla/tests/timerlat.t | 6 +- tools/tracing/rtla/tests/unit/Build | 5 + tools/tracing/rtla/tests/unit/Makefile.unit | 4 +- .../rtla/tests/unit/cli_opt_callback.c | 704 ++++++++++++++++++ .../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 | 13 + 33 files changed, 4566 insertions(+), 1311 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 create mode 100644 tools/tracing/rtla/tests/unit/cli_opt_callback.c 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
Signature · 1 line
2.54.0
Tomas Glozar
maintainerPATCH v3 1/6 rtla: Add libsubcmd dependency
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.
--- 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(-)
tools/tracing/rtla/.gitignore +2 -0
@@ -10,3 +10,5 @@ custom_filename.txt osnoise_irq_noise_hist.txt osnoise_trace.txt timerlat_trace.txt +libsubcmd/ +lib/
tools/tracing/rtla/Makefile +59 -7
92 lines not on this page · raw message
tools/tracing/rtla/src/rtla.c +4 -4
@@ -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]);
@@ -69,7 +69,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 );
@@ -82,7 +82,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
@@ -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.54.0
Tomas Glozar
maintainerPATCH v3 2/6 tools subcmd: support optarg as separate argument
In reply to Tomas GlozarIn 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
@@ -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
@@ -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.54.0
Tomas Glozar
maintainerPATCH v3 3/6 tools subcmd: allow parsing distinct --opt and --no-opt
In reply to Tomas Glozarlibsubcmd 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.
--- tools/lib/subcmd/parse-options.c | 10 ++++++---- tools/lib/subcmd/parse-options.h | 3 +++ 2 files changed, 9 insertions(+), 4 deletions(-)
tools/lib/subcmd/parse-options.c +6 -4
@@ -427,7 +427,8 @@ static int parse_long_opt(struct parse_opt_ctx_t *p, const 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 |=OPT_UNSET ; goto is_abbreviated; }/* negated? */ -if (strncmp (arg,"no-" ,3 )) +if (strncmp (arg,"no-" ,3 ) || (options->flags &PARSE_OPT_NOAUTONEG ))continue ; flags |=OPT_UNSET ; rest =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 ); }
tools/lib/subcmd/parse-options.h +3 -0
@@ -47,6 +47,7 @@ enum parse_opt_option_flags {PARSE_OPT_NOEMPTY =128 ,PARSE_OPT_NOBUILD =256 ,PARSE_OPT_CANSKIP =512 , + PARSE_OPT_NOAUTONEG = 1024, };struct option ;
@@ -149,6 +150,8 @@ struct option { { .type = OPTION_CALLBACK, .short_name = (s), .long_name = (l), .value = (v), .argh = "time", .help = (h), .callback = parse_opt_approxidate_cb }#define OPT_CALLBACK (s, l, v, a, h, f) \ { .type = OPTION_CALLBACK, .short_name = (s), .long_name = (l), .value = (v), .argh = (a), .help = (h), .callback = (f) } +#define OPT_CALLBACK_FLAG (s, l, v, a, h, f, fl) \ + { .type = OPTION_CALLBACK, .short_name = (s), .long_name = (l), .value = (v), .argh = (a), .help = (h), .callback = (f), .flags = (fl) }#define OPT_CALLBACK_SET (s, l, v, os, a, h, f) \ { .type = OPTION_CALLBACK, .short_name = (s), .long_name = (l), .value = (v), .argh = (a), .help = (h), .callback = (f), .set = check_vtype(os, bool *)}#define OPT_CALLBACK_NOOPT (s, l, v, a, h, f) \
Signature · 1 line
2.54.0
Tomas Glozar
maintainerPATCH v3 4/6 rtla: Parse cmdline using libsubcmd
In reply to Tomas GlozarInstead 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-<opt> 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.
--- 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
Documentation/tools/rtla/common_appendix.txt +4 -3
@@ -26,9 +26,10 @@ EXIT STATUS :: - 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 REPORTING BUGS ==============
tools/tracing/rtla/src/Build +1 -1
@@ -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 +537 -0
537 lines not on this page · raw message
tools/tracing/rtla/src/cli.h new file +7 -0
@@ -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/cli_p.h new file +670 -0
670 lines not on this page · raw message
tools/tracing/rtla/src/common.c +0 -109
121 lines not on this page · raw message
tools/tracing/rtla/src/common.h +12 -15
@@ -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"
@@ -57,12 +56,12 @@ struct osnoise_context {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; };
@@ -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;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); -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);int hist_main_loop (struct osnoise_tool *tool);
tools/tracing/rtla/src/osnoise.c +5 -4
@@ -15,6 +15,8 @@#include <stdio.h> #include <sched.h> +#include <linux/compiler.h> +#include "osnoise.h" #define DEFAULT_SAMPLE_PERIOD 1000000/* 1s */
@@ -1171,7 +1173,7 @@ int osnoise_enable(struct osnoise_tool *tool)return 0 ; } -static void osnoise_usage (int err) +__noreturn static void osnoise_usage (int err) {int i;
@@ -1209,7 +1211,7 @@ int osnoise_main(int argc, char *argv[]) }if ((strcmp (argv[1 ],"-h" ) ==0 ) || (strcmp (argv[1 ],"--help" ) ==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[]) } usage: -osnoise_usage (1 ); -exit (1 ); +osnoise_usage (129 ); }int hwnoise_main (int argc,char *argv[])
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 -92
92 lines not on this page · raw message
tools/tracing/rtla/src/timerlat.c +5 -4
@@ -13,6 +13,8 @@#include <stdio.h> #include <sched.h> +#include <linux/compiler.h> +#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 (); } -static void timerlat_usage (int err) +__noreturn static void timerlat_usage (int err) {int i;
@@ -269,7 +271,7 @@ int timerlat_main(int argc, char *argv[]) }if ((strcmp (argv[1 ],"-h" ) ==0 ) || (strcmp (argv[1 ],"--help" ) ==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[]) } usage: -timerlat_usage (1 ); -exit (1 ); +timerlat_usage (129 ); }
tools/tracing/rtla/src/timerlat.h +2 -2
@@ -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 -285
304 lines not on this page · raw message
tools/tracing/rtla/src/utils.c +1 -27
@@ -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
@@ -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
@@ -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" \
tools/tracing/rtla/tests/osnoise.t +3 -3
@@ -6,9 +6,9 @@ test_begin set_timeout 2m 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" \
tools/tracing/rtla/tests/timerlat.t +3 -3
@@ -21,9 +21,9 @@ export RTLA_NO_BPF=$option # 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" \
Signature · 1 line
2.54.0
Tomas Glozar
maintainerPATCH v3 5/6 rtla/tests: Add unit tests for _parse_args() functions
In reply to Tomas GlozarAdd 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.
--- 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
tools/tracing/rtla/src/cli.c +4 -4
@@ -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.trace_output); -if (geteuid ()) +if (geteuid () && !in_unit_test)fatal ("osnoise needs root permission" );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.trace_output); -if (geteuid ()) +if (geteuid () && !in_unit_test)fatal ("rtla needs root permission" );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.trace_output); -if (geteuid ()) +if (geteuid () && !in_unit_test)fatal ("rtla needs root permission" );/*
@@ -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.trace_output); -if (geteuid ()) +if (geteuid () && !in_unit_test)fatal ("rtla needs root permission" );if (params->common.hist.no_irq && params->common.hist.no_thread)
tools/tracing/rtla/src/cli.h +2 -0
@@ -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;
tools/tracing/rtla/tests/unit/Build +4 -0
@@ -1,3 +1,7 @@ unit_tests-y += utils.o unit_tests-y += actions.o unit_tests-y += unit_tests.o +unit_tests-y += osnoise_top_cli.o +unit_tests-y += osnoise_hist_cli.o +unit_tests-y += timerlat_top_cli.o +unit_tests-y += timerlat_hist_cli.o
tools/tracing/rtla/tests/unit/Makefile.unit +1 -1
@@ -3,7 +3,7 @@ UNIT_TESTS := $(OUTPUT)unit_tests UNIT_TESTS_IN := $(UNIT_TESTS)-in.o -$(UNIT_TESTS): $(UNIT_TESTS_IN) $(RTLA_IN) +$(UNIT_TESTS): $(UNIT_TESTS_IN) $(RTLA_IN) $(LIBSUBCMD) $(LIB_STRING) $(LIB_STR_ERROR_R) $(QUIET_LINK)$(CC) $(LDFLAGS) -o $@ $^ $(EXTLIBS) -lcheck $(UNIT_TESTS_IN): fixdep
tools/tracing/rtla/tests/unit/cli_params_assert.h new file +68 -0
68 lines not on this page · raw message
tools/tracing/rtla/tests/unit/osnoise_hist_cli.c new file +557 -0
557 lines not on this page · raw message
tools/tracing/rtla/tests/unit/osnoise_top_cli.c new file +503 -0
503 lines not on this page · raw message
tools/tracing/rtla/tests/unit/timerlat_hist_cli.c new file +702 -0
702 lines not on this page · raw message
tools/tracing/rtla/tests/unit/timerlat_top_cli.c new file +634 -0
634 lines not on this page · raw message
tools/tracing/rtla/tests/unit/unit_tests.c +11 -0
@@ -5,17 +5,28 @@#include <stdbool.h> #include "../../src/utils.h" +#include "../../src/cli.h" 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 );int main (int argc,char *argv[]) {int num_failed;SRunner *sr; + in_unit_test = true; + sr =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 ());srunner_run_all (sr,CK_VERBOSE ); num_failed =srunner_ntests_failed (sr);
Signature · 1 line
2.54.0
Tomas Glozar
maintainerPATCH v3 6/6 rtla/tests: Add unit tests for CLI option callbacks
In reply to Tomas GlozarIn 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.
--- 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
tools/tracing/rtla/src/timerlat.h +2 -0
@@ -1,4 +1,6 @@// SPDX-License-Identifier: GPL-2.0 +#pragma once +#include "osnoise.h" /*
tools/tracing/rtla/tests/unit/Build +1 -0
@@ -5,3 +5,4 @@ unit_tests-y += osnoise_top_cli.o unit_tests-y += osnoise_hist_cli.o unit_tests-y += timerlat_top_cli.o unit_tests-y += timerlat_hist_cli.o +unit_tests-y += cli_opt_callback.o
tools/tracing/rtla/tests/unit/Makefile.unit +1 -1
@@ -6,7 +6,7 @@ UNIT_TESTS_IN := $(UNIT_TESTS)-in.o $(UNIT_TESTS): $(UNIT_TESTS_IN) $(RTLA_IN) $(LIBSUBCMD) $(LIB_STRING) $(LIB_STR_ERROR_R) $(QUIET_LINK)$(CC) $(LDFLAGS) -o $@ $^ $(EXTLIBS) -lcheck -$(UNIT_TESTS_IN): fixdep +$(UNIT_TESTS_IN): fixdep $(LIBSUBCMD_INCLUDES) make $(build)=unit_tests unit-tests: FORCE
tools/tracing/rtla/tests/unit/cli_opt_callback.c new file +704 -0
704 lines not on this page · raw message
tools/tracing/rtla/tests/unit/unit_tests.c +2 -0
@@ -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 );int main (int argc,char *argv[]) {
@@ -22,6 +23,7 @@ int main(int argc, char *argv[]) in_unit_test = true; sr =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 ());
Signature · 1 line
2.54.0
Quoted 62 lines · Tomas Glozar
[ CC to linux-perf-users for the libsubcmd code changes ]
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 than having to be typed out manually for each tool.
libsubcmd is extended with a flag 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. Another flag is added to disable automatic definition of --no-xy for every option --xy and vice versa, which overlaps for RTLA's --irq and --thread options.
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. Another new file,cli_p.h, is used as a private header to contain macros and static function declarations that are also used by unit tests next tocli.c, but should not be imported from elsewhere.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.The test suite is expanded to include two levels of unit tests, one testing the already existing
tool_parse_args()functions, one tests option callbacks, which are a new level of the CLI parser added in this patchset. This helps to verify that no regressions are caused by this refactoring.I expect more improvements to the code being possible in the future, like creating macros for option groups to further deduplicate the code, reducing the amount of extra code in the
_parse_args()functions, or implementing support for unsetting options (which is currently only supported for those that do not use a custom callback).Dependencies:
- https://lore.kernel.org/linux-trace-kernel/[email protected]/T/
- https://lore.kernel.org/linux-trace-kernel/[email protected]/
- https://lore.kernel.org/linux-trace-kernel/[email protected]/
(apply in reverse order, alternatively, use base commit above)v3 changes (all in cover letter or first commit):
- Add FORCE to all targets that feature a make subcommand to ensure changes in the dependency will trigger rebuild correctly.
- Convert all dependencies on directory targetsLIBSUBCMD_OUTPUTandLIB_OUTPUTinto order-only prerequisites to prevent modifications of the directory content triggering rebuild of targets inside it.
- Properly mention depedencies of the patchset in the cover letter, as well as the base commit.
Quoted 2 lines
v2: https://lore.kernel.org/linux-trace-kernel/[email protected]/T/
Oops. I mistakenly added the reviewed-by tag to the v2 patch series.