]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
tools/rtla: Generate optstring from long options
authorCosta Shulyupin <costa.shul@redhat.com>
Thu, 8 Jan 2026 09:49:55 +0000 (11:49 +0200)
committerTomas Glozar <tglozar@redhat.com>
Mon, 2 Mar 2026 13:53:41 +0000 (14:53 +0100)
getopt_long() processes short and long options independently.
RTLA, like the majority of applications, uses both short and long
variants for each logical option.

Since the val member of struct option holds the letter of the short
variant, the string of short options can be reconstructed from the
array of long options.

Add getopt_auto() to generate optstring from an array of long options,
eliminating the need to maintain separate short option strings.

Signed-off-by: Costa Shulyupin <costa.shul@redhat.com>
Reviewed-by: Wander Lairson Costa <wander@redhat.com>
Link: https://lore.kernel.org/r/20260108095011.2115719-1-costa.shul@redhat.com
Signed-off-by: Tomas Glozar <tglozar@redhat.com>
tools/tracing/rtla/src/common.c
tools/tracing/rtla/src/common.h
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 ceff76a62a30b836ee14fdfc2c26b5bb0dc38b4a..f310b0d59ad3e3ae9a168a5a8180a072ec1c74ff 100644 (file)
@@ -39,6 +39,36 @@ static void set_signals(struct common_params *params)
        }
 }
 
+/*
+ * getopt_auto - auto-generates optstring from long_options
+ */
+int getopt_auto(int argc, char **argv, const struct option *long_opts)
+{
+       char opts[256];
+       int n = 0;
+
+       for (int i = 0; long_opts[i].name; i++) {
+               if (long_opts[i].val < 32 || long_opts[i].val > 127)
+                       continue;
+
+               if (n + 4 >= sizeof(opts))
+                       fatal("optstring buffer overflow");
+
+               opts[n++] = long_opts[i].val;
+
+               if (long_opts[i].has_arg == required_argument)
+                       opts[n++] = ':';
+               else if (long_opts[i].has_arg == optional_argument) {
+                       opts[n++] = ':';
+                       opts[n++] = ':';
+               }
+       }
+
+       opts[n] = '\0';
+
+       return getopt_long(argc, argv, opts, long_opts, NULL);
+}
+
 /*
  * common_parse_options - parse common command line options
  *
@@ -69,7 +99,7 @@ int common_parse_options(int argc, char **argv, struct common_params *common)
        };
 
        opterr = 0;
-       c = getopt_long(argc, argv, "c:C::Dd:e:H:P:", long_options, NULL);
+       c = getopt_auto(argc, argv, long_options);
        opterr = 1;
 
        switch (c) {
index 7602c5593ef5d978e7d2788db358e68cfec9a8d3..d4b3715700be73952bc0d1210a2adafbf0457add 100644 (file)
@@ -1,6 +1,7 @@
 /* SPDX-License-Identifier: GPL-2.0 */
 #pragma once
 
+#include <getopt.h>
 #include "actions.h"
 #include "timerlat_u.h"
 #include "trace.h"
@@ -156,6 +157,7 @@ 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);
index 9d70ea34807ffbc6daec1ec8db6bde3d96d12799..5c863e7aad28b4cfedaedf49e2938cbae49d398b 100644 (file)
@@ -506,8 +506,7 @@ static struct common_params
                if (common_parse_options(argc, argv, &params->common))
                        continue;
 
-               c = getopt_long(argc, argv, "a:b:E:hp:r:s:S:t::T:01234:5:6:7:",
-                                long_options, NULL);
+               c = getopt_auto(argc, argv, long_options);
 
                /* detect the end of the options. */
                if (c == -1)
index d54d47947fb4404c04b82d396cb06091e05fefec..b7aed40fd2164f1e5390ddc3b1db6fd16ad28085 100644 (file)
@@ -358,8 +358,7 @@ struct common_params *osnoise_top_parse_args(int argc, char **argv)
                if (common_parse_options(argc, argv, &params->common))
                        continue;
 
-               c = getopt_long(argc, argv, "a:hp:qr:s:S:t::T:0:1:2:3:",
-                                long_options, NULL);
+               c = getopt_auto(argc, argv, long_options);
 
                /* Detect the end of the options. */
                if (c == -1)
index 4e8c38a61197c396ab9d19e599ceffe57573a153..096de8ba3efbb6914884cb9bd2a3737b6eff084a 100644 (file)
@@ -825,8 +825,7 @@ static struct common_params
                if (common_parse_options(argc, argv, &params->common))
                        continue;
 
-               c = getopt_long(argc, argv, "a:b:E:hi:knp:s:t::T:uU0123456:7:8:9\1\2:\3:",
-                                long_options, NULL);
+               c = getopt_auto(argc, argv, long_options);
 
                /* detect the end of the options. */
                if (c == -1)
index 284b74773c2b5b228e578093f83791eca2631e95..27c14aa71a8bc85ef30aa3719779dd6fa82f1f9e 100644 (file)
@@ -588,8 +588,7 @@ static struct common_params
                if (common_parse_options(argc, argv, &params->common))
                        continue;
 
-               c = getopt_long(argc, argv, "a:hi:knp:qs:t::T:uU0:1:2:345:6:7:",
-                                long_options, NULL);
+               c = getopt_auto(argc, argv, long_options);
 
                /* detect the end of the options. */
                if (c == -1)