tools/rtla: Add common_usage()

The rtla tools have significant code quadruplication in their usage
functions. Each tool implements its own version of the same help text
formatting and option descriptions, leading to maintenance overhead and
inconsistencies.  Documentation/tools/rtla/common_options.rst lists 14
common options.

Add common_usage() infrastructure to consolidate help formatting.
Subsequent patches will extend this to handle other common options.

The refactored output is almost identical to the original, with the
following changes:
- add square brackets to specify optionality: `usage: [rtla] ...`
- remove `-q` from timerlat hist because hist tools don't support it
- minor spacing

Signed-off-by: Costa Shulyupin <costa.shul@redhat.com>
Link: https://lore.kernel.org/r/20251124063204.845425-1-costa.shul@redhat.com
Signed-off-by: Tomas Glozar <tglozar@redhat.com>
This commit is contained in:
Costa Shulyupin 2025-11-24 08:31:46 +02:00 committed by Tomas Glozar
parent c219d4ee1d
commit a08e012e81
6 changed files with 81 additions and 64 deletions

View file

@ -4,6 +4,7 @@
#include <pthread.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "common.h"
@ -368,3 +369,41 @@ int osn_set_stop(struct osnoise_tool *tool)
return 0;
}
static void print_msg_array(const char * const *msgs)
{
if (!msgs)
return;
for (int i = 0; msgs[i]; i++)
fprintf(stderr, "%s\n", msgs[i]);
}
/*
* common_usage - print complete usage information
*/
void common_usage(const char *tool, const char *mode,
const char *desc, const char * const *start_msgs, const char * const *opt_msgs)
{
static const char * const common_options[] = {
" -h/--help: print this menu",
NULL
};
fprintf(stderr, "rtla %s", tool);
if (strcmp(mode, ""))
fprintf(stderr, " %s", mode);
fprintf(stderr, ": %s (version %s)\n\n", desc, VERSION);
fprintf(stderr, " usage: [rtla] %s ", tool);
if (strcmp(mode, "top") == 0)
fprintf(stderr, "[top] [-h] ");
else
fprintf(stderr, "%s [-h] ", mode);
print_msg_array(start_msgs);
fprintf(stderr, "\n");
print_msg_array(common_options);
print_msg_array(opt_msgs);
exit(EXIT_SUCCESS);
}

View file

@ -160,3 +160,6 @@ 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);
int osn_set_stop(struct osnoise_tool *tool);
void common_usage(const char *tool, const char *mode,
const char *desc, const char * const *start_msgs, const char * const *opt_msgs);

View file

@ -409,16 +409,15 @@ osnoise_print_stats(struct osnoise_tool *tool)
*/
static void osnoise_hist_usage(void)
{
int i;
static const char * const msg[] = {
"",
" usage: rtla osnoise hist [-h] [-D] [-d s] [-a us] [-p us] [-r us] [-s us] [-S us] \\",
static const char * const msg_start[] = {
"[-D] [-d s] [-a us] [-p us] [-r us] [-s us] [-S us] \\",
" [-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]",
"",
" -h/--help: print this menu",
NULL,
};
static const char * const msg_opts[] = {
" -a/--auto: set automatic trace mode, stopping the session if argument in us sample is hit",
" -p/--period us: osnoise period in us",
" -r/--runtime us: osnoise runtime in us",
@ -453,13 +452,8 @@ static void osnoise_hist_usage(void)
NULL,
};
fprintf(stderr, "rtla osnoise hist: a per-cpu histogram of the OS noise (version %s)\n",
VERSION);
for (i = 0; msg[i]; i++)
fprintf(stderr, "%s\n", msg[i]);
exit(EXIT_SUCCESS);
common_usage("osnoise", "hist", "a per-cpu histogram of the OS noise",
msg_start, msg_opts);
}
/*

View file

@ -257,14 +257,16 @@ osnoise_print_stats(struct osnoise_tool *top)
*/
static void osnoise_top_usage(struct osnoise_params *params)
{
int i;
const char *tool, *mode, *desc;
static const char * const msg[] = {
" [-h] [-q] [-D] [-d s] [-a us] [-p us] [-r us] [-s us] [-S us] \\",
static const char * const msg_start[] = {
"[-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>] \\",
" [-c cpu-list] [-H cpu-list] [-P priority] [-C [cgroup_name]] [--warm-up s]",
"",
" -h/--help: print this menu",
NULL,
};
static const char * const msg_opts[] = {
" -a/--auto: set automatic trace mode, stopping the session if argument in us sample is hit",
" -p/--period us: osnoise period in us",
" -r/--runtime us: osnoise runtime in us",
@ -295,25 +297,16 @@ static void osnoise_top_usage(struct osnoise_params *params)
};
if (params->mode == MODE_OSNOISE) {
fprintf(stderr,
"rtla osnoise top: a per-cpu summary of the OS noise (version %s)\n",
VERSION);
fprintf(stderr, " usage: rtla osnoise [top]");
tool = "osnoise";
mode = "top";
desc = "a per-cpu summary of the OS noise";
} else {
tool = "hwnoise";
mode = "";
desc = "a summary of hardware-related noise";
}
if (params->mode == MODE_HWNOISE) {
fprintf(stderr,
"rtla hwnoise: a summary of hardware-related noise (version %s)\n",
VERSION);
fprintf(stderr, " usage: rtla hwnoise");
}
for (i = 0; msg[i]; i++)
fprintf(stderr, "%s\n", msg[i]);
exit(EXIT_SUCCESS);
common_usage(tool, mode, desc, msg_start, msg_opts);
}
/*

View file

@ -696,17 +696,16 @@ timerlat_print_stats(struct osnoise_tool *tool)
*/
static void timerlat_hist_usage(void)
{
int i;
char *msg[] = {
"",
" usage: [rtla] timerlat hist [-h] [-q] [-d s] [-D] [-n] [-a us] [-p us] [-i us] [-T us] [-s us] \\",
static const char * const msg_start[] = {
"[-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]\\",
" [-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] [--deepest-idle-state n]",
"",
" -h/--help: print this menu",
NULL,
};
static const char * const msg_opts[] = {
" -a/--auto: set automatic trace mode, stopping the session if argument in us latency is hit",
" -p/--period us: timerlat period in us",
" -i/--irq us: stop trace if the irq latency is higher than the argument in us",
@ -750,13 +749,8 @@ static void timerlat_hist_usage(void)
NULL,
};
fprintf(stderr, "rtla timerlat hist: a per-cpu histogram of the timer latency (version %s)\n",
VERSION);
for (i = 0; msg[i]; i++)
fprintf(stderr, "%s\n", msg[i]);
exit(EXIT_SUCCESS);
common_usage("timerlat", "hist", "a per-cpu histogram of the timer latency",
msg_start, msg_opts);
}
/*

View file

@ -476,15 +476,14 @@ timerlat_print_stats(struct osnoise_tool *top)
*/
static void timerlat_top_usage(void)
{
int i;
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] \\",
static const char *const msg_start[] = {
"[-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]\\",
" [-P priority] [--dma-latency us] [--aa-only us] [-C [cgroup_name]] [-u|-k] [--warm-up s] [--deepest-idle-state n]",
"",
" -h/--help: print this menu",
NULL,
};
static const char *const msg_opts[] = {
" -a/--auto: set automatic trace mode, stopping the session if argument in us latency is hit",
" --aa-only us: stop if <us> latency is hit, only printing the auto analysis (reduces CPU usage)",
" -p/--period us: timerlat period in us",
@ -522,13 +521,8 @@ static void timerlat_top_usage(void)
NULL,
};
fprintf(stderr, "rtla timerlat top: a per-cpu summary of the timer latency (version %s)\n",
VERSION);
for (i = 0; msg[i]; i++)
fprintf(stderr, "%s\n", msg[i]);
exit(EXIT_SUCCESS);
common_usage("timerlat", "top", "a per-cpu summary of the timer latency",
msg_start, msg_opts);
}
/*