rtla: Fix -t\--trace[=file]
authorJohn Kacur <jkacur@redhat.com>
Wed, 15 May 2024 18:30:23 +0000 (14:30 -0400)
committerDaniel Bristot de Oliveira <bristot@kernel.org>
Thu, 16 May 2024 14:49:10 +0000 (16:49 +0200)
The -t option has an optional argument.
The usual case is for a short option to be specified without an '='
and for the long version to be specified with an '='

Various forms of this do not work as expected.
For example:
rtla timerlat hist -T50 -tfile.txt
will result in a truncated file name of "ile.txt"

Another example is that the long form without the '=' will result in the
default file name instead of the requested file name.

This patch properly parses the optional argument with and without '='
and with and without spaces for the short form.

This patch was also tested using -t and --trace without providing a file
name both as the last requested option and with a following long and
short option.

For example:

  rtla timerlat hist -T50 -t -u
  rtla timerlat hist -T50 --trace -u

This fix is applied to both timerlat top and hist
and to osnoise top and hist.

Here is the full testing for rtla timerlat hist.
Before applying the patch

  rtla timerlat hist -T50 -t=file.txt
    Works as expected, "file.txt"

  rtla timerlat hist -T50 -tfile.txt
    Truncated file name "ile.txt"

  rtla timerlat hist -T50 -t file.txt
    Default file name instead of file.txt

  rtla timerlat hist -T50 --trace=file.txt
    Truncated file name "ile.txt"

  rtla timerlat hist -T50 --trace file.txt
    Default file name "timerlat_trace.txt" instead of "file.txt"

After applying the patch:

  rtla timerlat hist -T50 -t=file.txt
    Works as expected, "file.txt"

  rtla timerlat hist -T50 -tfile.txt
    Works as expected, "file.txt"

  rtla timerlat hist -T50 -t file.txt
    Works as expected, "file.txt"

  rtla timerlat hist -T50 --trace=file.txt
    Works as expected, "file.txt"

  rtla timerlat hist -T50 --trace file.txt
    Works as expected, "file.txt"

In addition the following tests were performed to make sure that
the default file name worked as expected including with trailing
options.

  rtla timerlat hist -T50 -t
    Works as expected "timerlat_trace.txt"

  rtla timerlat hist -T50 --trace
    Works as expected "timerlat_trace.txt"

  rtla timerlat hist -T50 -t -u
    Works as expected "timerlat_trace.txt"

  rtla timerlat hist -T50 --trace -u
    Works as expected "timerlat_trace.txt"

Link: https://lkml.kernel.org/r/20240515183024.59985-1-jkacur@redhat.com
Cc: Daniel Bristot de Oliveria <bristot@kernel.org>
Signed-off-by: John Kacur <jkacur@redhat.com>
Signed-off-by: Daniel Bristot de Oliveira <bristot@kernel.org>
tools/tracing/rtla/src/osnoise_hist.c
tools/tracing/rtla/src/osnoise_top.c
tools/tracing/rtla/src/timerlat_hist.c
tools/tracing/rtla/src/timerlat_top.c

index 198a17a3ea2e667c963d474ab2e399f1a616a287..7be17d09f7e8567ef61c28c78c121cecd8bb2198 100644 (file)
@@ -437,7 +437,7 @@ static void osnoise_hist_usage(char *usage)
        static const char * const msg[] = {
                "",
                "  usage: rtla osnoise hist [-h] [-D] [-d s] [-a us] [-p us] [-r us] [-s us] [-S us] \\",
-               "         [-T us] [-t[=file]] [-e sys[:event]] [--filter <filter>] [--trigger <trigger>] \\",
+               "         [-T us] [-t[file]] [-e sys[:event]] [--filter <filter>] [--trigger <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]",
                "",
@@ -453,7 +453,7 @@ static void osnoise_hist_usage(char *usage)
                "         -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]",
+               "         -t/--trace[file]: save the stopped trace to [file|osnoise_trace.txt]",
                "         -e/--event <sys:event>: enable the <sys:event> in the trace instance, multiple -e are allowed",
                "            --filter <filter>: enable a trace event filter to the previous -e event",
                "            --trigger <trigger>: enable a trace event trigger to the previous -e event",
@@ -645,9 +645,13 @@ static struct osnoise_hist_params
                        params->threshold = get_llong_from_str(optarg);
                        break;
                case 't':
-                       if (optarg)
-                               /* skip = */
-                               params->trace_output = &optarg[1];
+                       if (optarg) {
+                               if (optarg[0] == '=')
+                                       params->trace_output = &optarg[1];
+                               else
+                                       params->trace_output = &optarg[0];
+                       } else if (optind < argc && argv[optind][0] != '0')
+                               params->trace_output = argv[optind];
                        else
                                params->trace_output = "osnoise_trace.txt";
                        break;
index 7e5aab22727dadf2bbd5a7ba6cd004b82e51a168..07ba55d4ec06f0d7c03bb6cda7b23e2fe9d8eb21 100644 (file)
@@ -283,7 +283,7 @@ static void osnoise_top_usage(struct osnoise_top_params *params, char *usage)
 
        static const char * const msg[] = {
                " [-h] [-q] [-D] [-d s] [-a us] [-p us] [-r us] [-s us] [-S us] \\",
-               "         [-T us] [-t[=file]] [-e sys[:event]] [--filter <filter>] [--trigger <trigger>] \\",
+               "         [-T us] [-t[file]] [-e sys[:event]] [--filter <filter>] [--trigger <trigger>] \\",
                "         [-c cpu-list] [-H cpu-list] [-P priority] [-C[=cgroup_name]] [--warm-up s]",
                "",
                "         -h/--help: print this menu",
@@ -298,7 +298,7 @@ static void osnoise_top_usage(struct osnoise_top_params *params, char *usage)
                "         -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]",
+               "         -t/--trace[file]: save the stopped trace to [file|osnoise_trace.txt]",
                "         -e/--event <sys:event>: enable the <sys:event> in the trace instance, multiple -e are allowed",
                "            --filter <filter>: enable a trace event filter to the previous -e event",
                "            --trigger <trigger>: enable a trace event trigger to the previous -e event",
@@ -486,9 +486,13 @@ struct osnoise_top_params *osnoise_top_parse_args(int argc, char **argv)
                        params->stop_total_us = get_llong_from_str(optarg);
                        break;
                case 't':
-                       if (optarg)
-                               /* skip = */
-                               params->trace_output = &optarg[1];
+                       if (optarg) {
+                               if (optarg[0] == '=')
+                                       params->trace_output = &optarg[1];
+                               else
+                                       params->trace_output = &optarg[0];
+                       } else if (optind < argc && argv[optind][0] != '-')
+                               params->trace_output = argv[optind];
                        else
                                params->trace_output = "osnoise_trace.txt";
                        break;
index fbe2c6549bf94e59de90749956fb3aff827da878..a3907c390d67a5d710682dda8209691669ca5e88 100644 (file)
@@ -652,7 +652,7 @@ static void timerlat_hist_usage(char *usage)
        char *msg[] = {
                "",
                "  usage: [rtla] timerlat hist [-h] [-q] [-d s] [-D] [-n] [-a us] [-p us] [-i us] [-T us] [-s us] \\",
-               "         [-t[=file]] [-e sys[:event]] [--filter <filter>] [--trigger <trigger>] [-c cpu-list] [-H cpu-list]\\",
+               "         [-t[file]] [-e sys[:event]] [--filter <filter>] [--trigger <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-task] [-u|-k]",
                "         [--warm-up s]",
@@ -669,7 +669,7 @@ static void timerlat_hist_usage(char *usage)
                "         -d/--duration time[m|h|d]: duration of the session in seconds",
                "            --dump-tasks: prints the task running on all CPUs if stop conditions are met (depends on !--no-aa)",
                "         -D/--debug: print debug info",
-               "         -t/--trace[=file]: save the stopped trace to [file|timerlat_trace.txt]",
+               "         -t/--trace[file]: save the stopped trace to [file|timerlat_trace.txt]",
                "         -e/--event <sys:event>: enable the <sys:event> in the trace instance, multiple -e are allowed",
                "            --filter <filter>: enable a trace event filter to the previous -e event",
                "            --trigger <trigger>: enable a trace event trigger to the previous -e event",
@@ -885,9 +885,13 @@ static struct timerlat_hist_params
                        params->stop_total_us = get_llong_from_str(optarg);
                        break;
                case 't':
-                       if (optarg)
-                               /* skip = */
-                               params->trace_output = &optarg[1];
+                       if (optarg) {
+                               if (optarg[0] == '=')
+                                       params->trace_output = &optarg[1];
+                               else
+                                       params->trace_output = &optarg[0];
+                       } else if (optind < argc && argv[optind][0] != '-')
+                               params->trace_output = argv[optind];
                        else
                                params->trace_output = "timerlat_trace.txt";
                        break;
index 3a23e8d481c66b570f3d510e33ad72d3b6abb81a..8c16419fe22aa7b7791fb144932ca4f79be97583 100644 (file)
@@ -446,7 +446,7 @@ static void timerlat_top_usage(char *usage)
        static const char *const msg[] = {
                "",
                "  usage: rtla timerlat [top] [-h] [-q] [-a us] [-d s] [-D] [-n] [-p us] [-i us] [-T us] [-s us] \\",
-               "         [[-t[=file]] [-e sys[:event]] [--filter <filter>] [--trigger <trigger>] [-c cpu-list] [-H cpu-list]\\",
+               "         [[-t[file]] [-e sys[:event]] [--filter <filter>] [--trigger <trigger>] [-c cpu-list] [-H cpu-list]\\",
                "         [-P priority] [--dma-latency us] [--aa-only us] [-C[=cgroup_name]] [-u|-k] [--warm-up s]",
                "",
                "         -h/--help: print this menu",
@@ -462,7 +462,7 @@ static void timerlat_top_usage(char *usage)
                "         -d/--duration time[m|h|d]: duration of the session in seconds",
                "         -D/--debug: print debug info",
                "            --dump-tasks: prints the task running on all CPUs if stop conditions are met (depends on !--no-aa)",
-               "         -t/--trace[=file]: save the stopped trace to [file|timerlat_trace.txt]",
+               "         -t/--trace[file]: save the stopped trace to [file|timerlat_trace.txt]",
                "         -e/--event <sys:event>: enable the <sys:event> in the trace instance, multiple -e are allowed",
                "            --filter <command>: enable a trace event filter to the previous -e event",
                "            --trigger <command>: enable a trace event trigger to the previous -e event",
@@ -668,9 +668,13 @@ static struct timerlat_top_params
                        params->stop_total_us = get_llong_from_str(optarg);
                        break;
                case 't':
-                       if (optarg)
-                               /* skip = */
-                               params->trace_output = &optarg[1];
+                       if (optarg) {
+                               if (optarg[0] == '=')
+                                       params->trace_output = &optarg[1];
+                               else
+                                       params->trace_output = &optarg[0];
+                       } else if (optind < argc && argv[optind][0] != '-')
+                               params->trace_output = argv[optind];
                        else
                                params->trace_output = "timerlat_trace.txt";