Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

rtla: Fix -t\--trace[=file]

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>

authored by

John Kacur and committed by
Daniel Bristot de Oliveira
842fc5b8 01b05fc0

+36 -20
+9 -5
tools/tracing/rtla/src/osnoise_hist.c
··· 437 437 static const char * const msg[] = { 438 438 "", 439 439 " usage: rtla osnoise hist [-h] [-D] [-d s] [-a us] [-p us] [-r us] [-s us] [-S us] \\", 440 - " [-T us] [-t[=file]] [-e sys[:event]] [--filter <filter>] [--trigger <trigger>] \\", 440 + " [-T us] [-t[file]] [-e sys[:event]] [--filter <filter>] [--trigger <trigger>] \\", 441 441 " [-c cpu-list] [-H cpu-list] [-P priority] [-b N] [-E N] [--no-header] [--no-summary] \\", 442 442 " [--no-index] [--with-zeros] [-C[=cgroup_name]] [--warm-up]", 443 443 "", ··· 453 453 " -C/--cgroup[=cgroup_name]: set cgroup, if no cgroup_name is passed, the rtla's cgroup will be inherited", 454 454 " -d/--duration time[s|m|h|d]: duration of the session", 455 455 " -D/--debug: print debug info", 456 - " -t/--trace[=file]: save the stopped trace to [file|osnoise_trace.txt]", 456 + " -t/--trace[file]: save the stopped trace to [file|osnoise_trace.txt]", 457 457 " -e/--event <sys:event>: enable the <sys:event> in the trace instance, multiple -e are allowed", 458 458 " --filter <filter>: enable a trace event filter to the previous -e event", 459 459 " --trigger <trigger>: enable a trace event trigger to the previous -e event", ··· 645 645 params->threshold = get_llong_from_str(optarg); 646 646 break; 647 647 case 't': 648 - if (optarg) 649 - /* skip = */ 650 - params->trace_output = &optarg[1]; 648 + if (optarg) { 649 + if (optarg[0] == '=') 650 + params->trace_output = &optarg[1]; 651 + else 652 + params->trace_output = &optarg[0]; 653 + } else if (optind < argc && argv[optind][0] != '0') 654 + params->trace_output = argv[optind]; 651 655 else 652 656 params->trace_output = "osnoise_trace.txt"; 653 657 break;
+9 -5
tools/tracing/rtla/src/osnoise_top.c
··· 283 283 284 284 static const char * const msg[] = { 285 285 " [-h] [-q] [-D] [-d s] [-a us] [-p us] [-r us] [-s us] [-S us] \\", 286 - " [-T us] [-t[=file]] [-e sys[:event]] [--filter <filter>] [--trigger <trigger>] \\", 286 + " [-T us] [-t[file]] [-e sys[:event]] [--filter <filter>] [--trigger <trigger>] \\", 287 287 " [-c cpu-list] [-H cpu-list] [-P priority] [-C[=cgroup_name]] [--warm-up s]", 288 288 "", 289 289 " -h/--help: print this menu", ··· 298 298 " -C/--cgroup[=cgroup_name]: set cgroup, if no cgroup_name is passed, the rtla's cgroup will be inherited", 299 299 " -d/--duration time[s|m|h|d]: duration of the session", 300 300 " -D/--debug: print debug info", 301 - " -t/--trace[=file]: save the stopped trace to [file|osnoise_trace.txt]", 301 + " -t/--trace[file]: save the stopped trace to [file|osnoise_trace.txt]", 302 302 " -e/--event <sys:event>: enable the <sys:event> in the trace instance, multiple -e are allowed", 303 303 " --filter <filter>: enable a trace event filter to the previous -e event", 304 304 " --trigger <trigger>: enable a trace event trigger to the previous -e event", ··· 486 486 params->stop_total_us = get_llong_from_str(optarg); 487 487 break; 488 488 case 't': 489 - if (optarg) 490 - /* skip = */ 491 - params->trace_output = &optarg[1]; 489 + if (optarg) { 490 + if (optarg[0] == '=') 491 + params->trace_output = &optarg[1]; 492 + else 493 + params->trace_output = &optarg[0]; 494 + } else if (optind < argc && argv[optind][0] != '-') 495 + params->trace_output = argv[optind]; 492 496 else 493 497 params->trace_output = "osnoise_trace.txt"; 494 498 break;
+9 -5
tools/tracing/rtla/src/timerlat_hist.c
··· 652 652 char *msg[] = { 653 653 "", 654 654 " usage: [rtla] timerlat hist [-h] [-q] [-d s] [-D] [-n] [-a us] [-p us] [-i us] [-T us] [-s us] \\", 655 - " [-t[=file]] [-e sys[:event]] [--filter <filter>] [--trigger <trigger>] [-c cpu-list] [-H cpu-list]\\", 655 + " [-t[file]] [-e sys[:event]] [--filter <filter>] [--trigger <trigger>] [-c cpu-list] [-H cpu-list]\\", 656 656 " [-P priority] [-E N] [-b N] [--no-irq] [--no-thread] [--no-header] [--no-summary] \\", 657 657 " [--no-index] [--with-zeros] [--dma-latency us] [-C[=cgroup_name]] [--no-aa] [--dump-task] [-u|-k]", 658 658 " [--warm-up s]", ··· 669 669 " -d/--duration time[m|h|d]: duration of the session in seconds", 670 670 " --dump-tasks: prints the task running on all CPUs if stop conditions are met (depends on !--no-aa)", 671 671 " -D/--debug: print debug info", 672 - " -t/--trace[=file]: save the stopped trace to [file|timerlat_trace.txt]", 672 + " -t/--trace[file]: save the stopped trace to [file|timerlat_trace.txt]", 673 673 " -e/--event <sys:event>: enable the <sys:event> in the trace instance, multiple -e are allowed", 674 674 " --filter <filter>: enable a trace event filter to the previous -e event", 675 675 " --trigger <trigger>: enable a trace event trigger to the previous -e event", ··· 885 885 params->stop_total_us = get_llong_from_str(optarg); 886 886 break; 887 887 case 't': 888 - if (optarg) 889 - /* skip = */ 890 - params->trace_output = &optarg[1]; 888 + if (optarg) { 889 + if (optarg[0] == '=') 890 + params->trace_output = &optarg[1]; 891 + else 892 + params->trace_output = &optarg[0]; 893 + } else if (optind < argc && argv[optind][0] != '-') 894 + params->trace_output = argv[optind]; 891 895 else 892 896 params->trace_output = "timerlat_trace.txt"; 893 897 break;
+9 -5
tools/tracing/rtla/src/timerlat_top.c
··· 446 446 static const char *const msg[] = { 447 447 "", 448 448 " usage: rtla timerlat [top] [-h] [-q] [-a us] [-d s] [-D] [-n] [-p us] [-i us] [-T us] [-s us] \\", 449 - " [[-t[=file]] [-e sys[:event]] [--filter <filter>] [--trigger <trigger>] [-c cpu-list] [-H cpu-list]\\", 449 + " [[-t[file]] [-e sys[:event]] [--filter <filter>] [--trigger <trigger>] [-c cpu-list] [-H cpu-list]\\", 450 450 " [-P priority] [--dma-latency us] [--aa-only us] [-C[=cgroup_name]] [-u|-k] [--warm-up s]", 451 451 "", 452 452 " -h/--help: print this menu", ··· 462 462 " -d/--duration time[m|h|d]: duration of the session in seconds", 463 463 " -D/--debug: print debug info", 464 464 " --dump-tasks: prints the task running on all CPUs if stop conditions are met (depends on !--no-aa)", 465 - " -t/--trace[=file]: save the stopped trace to [file|timerlat_trace.txt]", 465 + " -t/--trace[file]: save the stopped trace to [file|timerlat_trace.txt]", 466 466 " -e/--event <sys:event>: enable the <sys:event> in the trace instance, multiple -e are allowed", 467 467 " --filter <command>: enable a trace event filter to the previous -e event", 468 468 " --trigger <command>: enable a trace event trigger to the previous -e event", ··· 668 668 params->stop_total_us = get_llong_from_str(optarg); 669 669 break; 670 670 case 't': 671 - if (optarg) 672 - /* skip = */ 673 - params->trace_output = &optarg[1]; 671 + if (optarg) { 672 + if (optarg[0] == '=') 673 + params->trace_output = &optarg[1]; 674 + else 675 + params->trace_output = &optarg[0]; 676 + } else if (optind < argc && argv[optind][0] != '-') 677 + params->trace_output = argv[optind]; 674 678 else 675 679 params->trace_output = "timerlat_trace.txt"; 676 680