]> Gentwo Git Trees - linux/.git/commitdiff
tools/rtla: Add fatal() and replace error handling pattern
authorCosta Shulyupin <costa.shul@redhat.com>
Sat, 11 Oct 2025 08:27:34 +0000 (11:27 +0300)
committerTomas Glozar <tglozar@redhat.com>
Fri, 21 Nov 2025 09:30:27 +0000 (10:30 +0100)
The code contains some technical debt in error handling,
which complicates the consolidation of duplicated code.

Introduce an fatal() function to replace the common pattern of
err_msg() followed by exit(EXIT_FAILURE), reducing the length of an
already long function.

Further patches using fatal() follow.

Signed-off-by: Costa Shulyupin <costa.shul@redhat.com>
Reviewed-by: Tomas Glozar <tglozar@redhat.com>
Link: https://lore.kernel.org/r/20251011082738.173670-2-costa.shul@redhat.com
Signed-off-by: Tomas Glozar <tglozar@redhat.com>
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
tools/tracing/rtla/src/timerlat_u.c
tools/tracing/rtla/src/utils.c
tools/tracing/rtla/src/utils.h

index df0657b789804e54ddd4b3da57af4b1099f17542..d30a9a03b76473cb4d7428e17d8eb266fb8f5652 100644 (file)
@@ -575,10 +575,8 @@ static struct common_params
                        break;
                case 'e':
                        tevent = trace_event_alloc(optarg);
-                       if (!tevent) {
-                               err_msg("Error alloc trace event");
-                               exit(EXIT_FAILURE);
-                       }
+                       if (!tevent)
+                               fatal("Error alloc trace event");
 
                        if (params->common.events)
                                tevent->next = params->common.events;
@@ -598,10 +596,8 @@ static struct common_params
                case 'H':
                        params->common.hk_cpus = 1;
                        retval = parse_cpu_set(optarg, &params->common.hk_cpu_set);
-                       if (retval) {
-                               err_msg("Error parsing house keeping CPUs\n");
-                               exit(EXIT_FAILURE);
-                       }
+                       if (retval)
+                               fatal("Error parsing house keeping CPUs");
                        break;
                case 'p':
                        params->period = get_llong_from_str(optarg);
@@ -654,10 +650,8 @@ static struct common_params
                case '4': /* trigger */
                        if (params->common.events) {
                                retval = trace_event_add_trigger(params->common.events, optarg);
-                               if (retval) {
-                                       err_msg("Error adding trigger %s\n", optarg);
-                                       exit(EXIT_FAILURE);
-                               }
+                               if (retval)
+                                       fatal("Error adding trigger %s", optarg);
                        } else {
                                osnoise_hist_usage("--trigger requires a previous -e\n");
                        }
@@ -665,10 +659,8 @@ static struct common_params
                case '5': /* filter */
                        if (params->common.events) {
                                retval = trace_event_add_filter(params->common.events, optarg);
-                               if (retval) {
-                                       err_msg("Error adding filter %s\n", optarg);
-                                       exit(EXIT_FAILURE);
-                               }
+                               if (retval)
+                                       fatal("Error adding filter %s", optarg);
                        } else {
                                osnoise_hist_usage("--filter requires a previous -e\n");
                        }
@@ -682,18 +674,14 @@ static struct common_params
                case '8':
                        retval = actions_parse(&params->common.threshold_actions, optarg,
                                               "osnoise_trace.txt");
-                       if (retval) {
-                               err_msg("Invalid action %s\n", optarg);
-                               exit(EXIT_FAILURE);
-                       }
+                       if (retval)
+                               fatal("Invalid action %s", optarg);
                        break;
                case '9':
                        retval = actions_parse(&params->common.end_actions, optarg,
                                               "osnoise_trace.txt");
-                       if (retval) {
-                               err_msg("Invalid action %s\n", optarg);
-                               exit(EXIT_FAILURE);
-                       }
+                       if (retval)
+                               fatal("Invalid action %s", optarg);
                        break;
                default:
                        osnoise_hist_usage("Invalid option");
@@ -703,10 +691,8 @@ static struct common_params
        if (trace_output)
                actions_add_trace_output(&params->common.threshold_actions, trace_output);
 
-       if (geteuid()) {
-               err_msg("rtla needs root permission\n");
-               exit(EXIT_FAILURE);
-       }
+       if (geteuid())
+               fatal("rtla needs root permission");
 
        if (params->common.hist.no_index && !params->common.hist.with_zeros)
                osnoise_hist_usage("no-index set and with-zeros not set - it does not make sense");
index 1b5181e66b175486c9e869ada0b045c1a06c0b07..487daac8908c07e674502e3bc66969d076860166 100644 (file)
@@ -421,10 +421,8 @@ struct common_params *osnoise_top_parse_args(int argc, char **argv)
                        break;
                case 'e':
                        tevent = trace_event_alloc(optarg);
-                       if (!tevent) {
-                               err_msg("Error alloc trace event");
-                               exit(EXIT_FAILURE);
-                       }
+                       if (!tevent)
+                               fatal("Error alloc trace event");
 
                        if (params->common.events)
                                tevent->next = params->common.events;
@@ -438,10 +436,8 @@ struct common_params *osnoise_top_parse_args(int argc, char **argv)
                case 'H':
                        params->common.hk_cpus = 1;
                        retval = parse_cpu_set(optarg, &params->common.hk_cpu_set);
-                       if (retval) {
-                               err_msg("Error parsing house keeping CPUs\n");
-                               exit(EXIT_FAILURE);
-                       }
+                       if (retval)
+                               fatal("Error parsing house keeping CPUs");
                        break;
                case 'p':
                        params->period = get_llong_from_str(optarg);
@@ -485,10 +481,8 @@ struct common_params *osnoise_top_parse_args(int argc, char **argv)
                case '0': /* trigger */
                        if (params->common.events) {
                                retval = trace_event_add_trigger(params->common.events, optarg);
-                               if (retval) {
-                                       err_msg("Error adding trigger %s\n", optarg);
-                                       exit(EXIT_FAILURE);
-                               }
+                               if (retval)
+                                       fatal("Error adding trigger %s", optarg);
                        } else {
                                osnoise_top_usage(params, "--trigger requires a previous -e\n");
                        }
@@ -496,10 +490,8 @@ struct common_params *osnoise_top_parse_args(int argc, char **argv)
                case '1': /* filter */
                        if (params->common.events) {
                                retval = trace_event_add_filter(params->common.events, optarg);
-                               if (retval) {
-                                       err_msg("Error adding filter %s\n", optarg);
-                                       exit(EXIT_FAILURE);
-                               }
+                               if (retval)
+                                       fatal("Error adding filter %s", optarg);
                        } else {
                                osnoise_top_usage(params, "--filter requires a previous -e\n");
                        }
@@ -513,18 +505,14 @@ struct common_params *osnoise_top_parse_args(int argc, char **argv)
                case '4':
                        retval = actions_parse(&params->common.threshold_actions, optarg,
                                               "osnoise_trace.txt");
-                       if (retval) {
-                               err_msg("Invalid action %s\n", optarg);
-                               exit(EXIT_FAILURE);
-                       }
+                       if (retval)
+                               fatal("Invalid action %s", optarg);
                        break;
                case '5':
                        retval = actions_parse(&params->common.end_actions, optarg,
                                               "osnoise_trace.txt");
-                       if (retval) {
-                               err_msg("Invalid action %s\n", optarg);
-                               exit(EXIT_FAILURE);
-                       }
+                       if (retval)
+                               fatal("Invalid action %s", optarg);
                        break;
                default:
                        osnoise_top_usage(params, "Invalid option");
@@ -534,10 +522,8 @@ struct common_params *osnoise_top_parse_args(int argc, char **argv)
        if (trace_output)
                actions_add_trace_output(&params->common.threshold_actions, trace_output);
 
-       if (geteuid()) {
-               err_msg("osnoise needs root permission\n");
-               exit(EXIT_FAILURE);
-       }
+       if (geteuid())
+               fatal("osnoise needs root permission");
 
        return &params->common;
 }
index c432ef5f59e7feeeea1859ed10c3670f95023f3e..f98deb16b452b2d847a4a71c9303c84f00f548a8 100644 (file)
@@ -894,10 +894,8 @@ static struct common_params
                        break;
                case 'e':
                        tevent = trace_event_alloc(optarg);
-                       if (!tevent) {
-                               err_msg("Error alloc trace event");
-                               exit(EXIT_FAILURE);
-                       }
+                       if (!tevent)
+                               fatal("Error alloc trace event");
 
                        if (params->common.events)
                                tevent->next = params->common.events;
@@ -917,10 +915,8 @@ static struct common_params
                case 'H':
                        params->common.hk_cpus = 1;
                        retval = parse_cpu_set(optarg, &params->common.hk_cpu_set);
-                       if (retval) {
-                               err_msg("Error parsing house keeping CPUs\n");
-                               exit(EXIT_FAILURE);
-                       }
+                       if (retval)
+                               fatal("Error parsing house keeping CPUs");
                        break;
                case 'i':
                        params->common.stop_us = get_llong_from_str(optarg);
@@ -986,10 +982,8 @@ static struct common_params
                case '6': /* trigger */
                        if (params->common.events) {
                                retval = trace_event_add_trigger(params->common.events, optarg);
-                               if (retval) {
-                                       err_msg("Error adding trigger %s\n", optarg);
-                                       exit(EXIT_FAILURE);
-                               }
+                               if (retval)
+                                       fatal("Error adding trigger %s", optarg);
                        } else {
                                timerlat_hist_usage("--trigger requires a previous -e\n");
                        }
@@ -997,20 +991,16 @@ static struct common_params
                case '7': /* filter */
                        if (params->common.events) {
                                retval = trace_event_add_filter(params->common.events, optarg);
-                               if (retval) {
-                                       err_msg("Error adding filter %s\n", optarg);
-                                       exit(EXIT_FAILURE);
-                               }
+                               if (retval)
+                                       fatal("Error adding filter %s", optarg);
                        } else {
                                timerlat_hist_usage("--filter requires a previous -e\n");
                        }
                        break;
                case '8':
                        params->dma_latency = get_llong_from_str(optarg);
-                       if (params->dma_latency < 0 || params->dma_latency > 10000) {
-                               err_msg("--dma-latency needs to be >= 0 and < 10000");
-                               exit(EXIT_FAILURE);
-                       }
+                       if (params->dma_latency < 0 || params->dma_latency > 10000)
+                               fatal("--dma-latency needs to be >= 0 and < 10000");
                        break;
                case '9':
                        params->no_aa = 1;
@@ -1030,31 +1020,25 @@ static struct common_params
                case '\5':
                        retval = actions_parse(&params->common.threshold_actions, optarg,
                                               "timerlat_trace.txt");
-                       if (retval) {
-                               err_msg("Invalid action %s\n", optarg);
-                               exit(EXIT_FAILURE);
-                       }
+                       if (retval)
+                               fatal("Invalid action %s", optarg);
                        break;
                case '\6':
                        retval = actions_parse(&params->common.end_actions, optarg,
                                               "timerlat_trace.txt");
-                       if (retval) {
-                               err_msg("Invalid action %s\n", optarg);
-                               exit(EXIT_FAILURE);
-                       }
+                       if (retval)
+                               fatal("Invalid action %s", optarg);
                        break;
                default:
-                       timerlat_hist_usage("Invalid option");
+                       fatal("Invalid option");
                }
        }
 
        if (trace_output)
                actions_add_trace_output(&params->common.threshold_actions, trace_output);
 
-       if (geteuid()) {
-               err_msg("rtla needs root permission\n");
-               exit(EXIT_FAILURE);
-       }
+       if (geteuid())
+               fatal("rtla needs root permission");
 
        if (params->common.hist.no_irq && params->common.hist.no_thread)
                timerlat_hist_usage("no-irq and no-thread set, there is nothing to do here");
index 82e227d27af7a847f39018d1102dbcb11b39fbf2..9664b8af727e6d828b73515d7e3f732c6754e3d0 100644 (file)
@@ -666,10 +666,8 @@ static struct common_params
                        break;
                case 'e':
                        tevent = trace_event_alloc(optarg);
-                       if (!tevent) {
-                               err_msg("Error alloc trace event");
-                               exit(EXIT_FAILURE);
-                       }
+                       if (!tevent)
+                               fatal("Error alloc trace event");
 
                        if (params->common.events)
                                tevent->next = params->common.events;
@@ -682,10 +680,8 @@ static struct common_params
                case 'H':
                        params->common.hk_cpus = 1;
                        retval = parse_cpu_set(optarg, &params->common.hk_cpu_set);
-                       if (retval) {
-                               err_msg("Error parsing house keeping CPUs\n");
-                               exit(EXIT_FAILURE);
-                       }
+                       if (retval)
+                               fatal("Error parsing house keeping CPUs");
                        break;
                case 'i':
                        params->common.stop_us = get_llong_from_str(optarg);
@@ -736,10 +732,8 @@ static struct common_params
                case '0': /* trigger */
                        if (params->common.events) {
                                retval = trace_event_add_trigger(params->common.events, optarg);
-                               if (retval) {
-                                       err_msg("Error adding trigger %s\n", optarg);
-                                       exit(EXIT_FAILURE);
-                               }
+                               if (retval)
+                                       fatal("Error adding trigger %s", optarg);
                        } else {
                                timerlat_top_usage("--trigger requires a previous -e\n");
                        }
@@ -747,20 +741,16 @@ static struct common_params
                case '1': /* filter */
                        if (params->common.events) {
                                retval = trace_event_add_filter(params->common.events, optarg);
-                               if (retval) {
-                                       err_msg("Error adding filter %s\n", optarg);
-                                       exit(EXIT_FAILURE);
-                               }
+                               if (retval)
+                                       fatal("Error adding filter %s", optarg);
                        } else {
                                timerlat_top_usage("--filter requires a previous -e\n");
                        }
                        break;
                case '2': /* dma-latency */
                        params->dma_latency = get_llong_from_str(optarg);
-                       if (params->dma_latency < 0 || params->dma_latency > 10000) {
-                               err_msg("--dma-latency needs to be >= 0 and < 10000");
-                               exit(EXIT_FAILURE);
-                       }
+                       if (params->dma_latency < 0 || params->dma_latency > 10000)
+                               fatal("--dma-latency needs to be >= 0 and < 10000");
                        break;
                case '3': /* no-aa */
                        params->no_aa = 1;
@@ -780,18 +770,14 @@ static struct common_params
                case '9':
                        retval = actions_parse(&params->common.threshold_actions, optarg,
                                               "timerlat_trace.txt");
-                       if (retval) {
-                               err_msg("Invalid action %s\n", optarg);
-                               exit(EXIT_FAILURE);
-                       }
+                       if (retval)
+                               fatal("Invalid action %s", optarg);
                        break;
                case '\1':
                        retval = actions_parse(&params->common.end_actions, optarg,
                                               "timerlat_trace.txt");
-                       if (retval) {
-                               err_msg("Invalid action %s\n", optarg);
-                               exit(EXIT_FAILURE);
-                       }
+                       if (retval)
+                               fatal("Invalid action %s", optarg);
                        break;
                default:
                        timerlat_top_usage("Invalid option");
@@ -801,10 +787,8 @@ static struct common_params
        if (trace_output)
                actions_add_trace_output(&params->common.threshold_actions, trace_output);
 
-       if (geteuid()) {
-               err_msg("rtla needs root permission\n");
-               exit(EXIT_FAILURE);
-       }
+       if (geteuid())
+               fatal("rtla needs root permission");
 
        /*
         * Auto analysis only happens if stop tracing, thus:
index 01dbf9a6b5a51ed7592e3bed5a6d8d2d26342625..ce68e39d25fdeef892852e036606638096015685 100644 (file)
@@ -51,10 +51,8 @@ static int timerlat_u_main(int cpu, struct timerlat_u_params *params)
 
        if (!params->sched_param) {
                retval = sched_setscheduler(0, SCHED_FIFO, &sp);
-               if (retval < 0) {
-                       err_msg("Error setting timerlat u default priority: %s\n", strerror(errno));
-                       exit(1);
-               }
+               if (retval < 0)
+                       fatal("Error setting timerlat u default priority: %s", strerror(errno));
        } else {
                retval = __set_sched_attr(getpid(), params->sched_param);
                if (retval) {
@@ -78,10 +76,8 @@ static int timerlat_u_main(int cpu, struct timerlat_u_params *params)
        snprintf(buffer, sizeof(buffer), "osnoise/per_cpu/cpu%d/timerlat_fd", cpu);
 
        timerlat_fd = tracefs_instance_file_open(NULL, buffer, O_RDONLY);
-       if (timerlat_fd < 0) {
-               err_msg("Error opening %s:%s\n", buffer, strerror(errno));
-               exit(1);
-       }
+       if (timerlat_fd < 0)
+               fatal("Error opening %s:%s", buffer, strerror(errno));
 
        debug_msg("User-space timerlat pid %d on cpu %d\n", gettid(), cpu);
 
index d6ab15dcb4907e09bcec1c1207adfa5a2655397d..54334c676a2290788dc552f0ca19c64b3a7f470b 100644 (file)
@@ -56,6 +56,21 @@ void debug_msg(const char *fmt, ...)
        fprintf(stderr, "%s", message);
 }
 
+/*
+ * fatal - print an error message and EOL to stderr and exit with ERROR
+ */
+void fatal(const char *fmt, ...)
+{
+       va_list ap;
+
+       va_start(ap, fmt);
+       vfprintf(stderr, fmt, ap);
+       va_end(ap);
+       fprintf(stderr, "\n");
+
+       exit(ERROR);
+}
+
 /*
  * get_llong_from_str - get a long long int from a string
  */
index a2a6f89f342d09d0c5fcad9d09ea485375f5d5c2..1be095f9a7e6287f057403607707f9e1bb8e9c31 100644 (file)
@@ -19,6 +19,7 @@
 extern int config_debug;
 void debug_msg(const char *fmt, ...);
 void err_msg(const char *fmt, ...);
+void fatal(const char *fmt, ...);
 
 long parse_seconds_duration(char *val);
 void get_duration(time_t start_time, char *output, int output_size);