]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
rtla: Introduce common_threshold_handler() helper
authorWander Lairson Costa <wander@redhat.com>
Mon, 9 Mar 2026 19:46:17 +0000 (16:46 -0300)
committerTomas Glozar <tglozar@redhat.com>
Tue, 10 Mar 2026 09:32:37 +0000 (10:32 +0100)
Several functions duplicate the logic for handling threshold actions.
When a threshold is reached, these functions stop the trace, perform
configured actions, and restart the trace if --on-threshold continue
is set.

Create common_threshold_handler() to centralize this shared logic and
avoid code duplication. The function executes the configured threshold
actions and restarts the necessary trace instances when appropriate.

Also add should_continue_tracing() helper to encapsulate the check
for whether tracing should continue after a threshold event, improving
code readability at call sites.

In timerlat_top_bpf_main_loop(), use common_params directly instead
of casting through timerlat_params when only common fields are needed.

Signed-off-by: Wander Lairson Costa <wander@redhat.com>
Link: https://lore.kernel.org/r/20260309195040.1019085-5-wander@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/timerlat_hist.c
tools/tracing/rtla/src/timerlat_top.c

index a31fbaea5da6b972d78aa09e6066a82fd6aab3b1..a00b5e55354583771873e21a9e8fb2022f0891c6 100644 (file)
@@ -207,6 +207,38 @@ out_err:
 }
 
 
+/**
+ * common_threshold_handler - handle latency threshold overflow
+ * @tool: pointer to the osnoise_tool instance containing trace contexts
+ *
+ * Executes the configured threshold actions (e.g., saving trace, printing,
+ * sending signals). If the continue flag is set (--on-threshold continue),
+ * restarts the auxiliary trace instances to continue monitoring.
+ *
+ * Return: 0 for success, -1 for error.
+ */
+int
+common_threshold_handler(const struct osnoise_tool *tool)
+{
+       actions_perform(&tool->params->threshold_actions);
+
+       if (!should_continue_tracing(tool->params))
+               /* continue flag not set, break */
+               return 0;
+
+       /* continue action reached, re-enable tracing */
+       if (tool->record && trace_instance_start(&tool->record->trace))
+               goto err;
+       if (tool->aa && trace_instance_start(&tool->aa->trace))
+               goto err;
+
+       return 0;
+
+err:
+       err_msg("Error restarting trace\n");
+       return -1;
+}
+
 int run_tool(struct tool_ops *ops, int argc, char *argv[])
 {
        struct common_params *params;
@@ -385,17 +417,14 @@ int top_main_loop(struct osnoise_tool *tool)
                                /* stop tracing requested, do not perform actions */
                                return 0;
 
-                       actions_perform(&params->threshold_actions);
+                       retval = common_threshold_handler(tool);
+                       if (retval)
+                               return retval;
+
 
-                       if (!params->threshold_actions.continue_flag)
-                               /* continue flag not set, break */
+                       if (!should_continue_tracing(params))
                                return 0;
 
-                       /* continue action reached, re-enable tracing */
-                       if (record)
-                               trace_instance_start(&record->trace);
-                       if (tool->aa)
-                               trace_instance_start(&tool->aa->trace);
                        trace_instance_start(trace);
                }
 
@@ -436,18 +465,14 @@ int hist_main_loop(struct osnoise_tool *tool)
                                /* stop tracing requested, do not perform actions */
                                break;
 
-                       actions_perform(&params->threshold_actions);
+                       retval = common_threshold_handler(tool);
+                       if (retval)
+                               return retval;
 
-                       if (!params->threshold_actions.continue_flag)
-                               /* continue flag not set, break */
-                               break;
+                       if (!should_continue_tracing(params))
+                               return 0;
 
-                       /* continue action reached, re-enable tracing */
-                       if (tool->record)
-                               trace_instance_start(&tool->record->trace);
-                       if (tool->aa)
-                               trace_instance_start(&tool->aa->trace);
-                       trace_instance_start(&tool->trace);
+                       trace_instance_start(trace);
                }
 
                /* is there still any user-threads ? */
index 22ec436a93cc1ba239795a8d28dd37609bc846eb..51665db4ffce807758ff30e196d7aa1f07dd790b 100644 (file)
@@ -146,6 +146,24 @@ struct tool_ops {
        void (*free)(struct osnoise_tool *tool);
 };
 
+/**
+ * should_continue_tracing - check if tracing should continue after threshold
+ * @params: pointer to the common parameters structure
+ *
+ * Returns true if the continue action was configured (--on-threshold continue),
+ * indicating that tracing should be restarted after handling the threshold event.
+ *
+ * Return: 1 if tracing should continue, 0 otherwise.
+ */
+static inline int
+should_continue_tracing(const struct common_params *params)
+{
+       return params->threshold_actions.continue_flag;
+}
+
+int
+common_threshold_handler(const struct osnoise_tool *tool);
+
 int osnoise_set_cpus(struct osnoise_context *context, char *cpus);
 void osnoise_restore_cpus(struct osnoise_context *context);
 
index 6524be4e2bf794c49f3c3be37bba42fe306f5f26..79142af4f5662e6c6bbc57f5045a3ee4f0d7ccb0 100644 (file)
@@ -17,6 +17,7 @@
 #include "timerlat.h"
 #include "timerlat_aa.h"
 #include "timerlat_bpf.h"
+#include "common.h"
 
 struct timerlat_hist_cpu {
        int                     *irq;
@@ -1047,7 +1048,6 @@ out_err:
 
 static int timerlat_hist_bpf_main_loop(struct osnoise_tool *tool)
 {
-       struct timerlat_params *params = to_timerlat_params(tool->params);
        int retval;
 
        while (!stop_tracing) {
@@ -1055,18 +1055,17 @@ static int timerlat_hist_bpf_main_loop(struct osnoise_tool *tool)
 
                if (!stop_tracing) {
                        /* Threshold overflow, perform actions on threshold */
-                       actions_perform(&params->common.threshold_actions);
+                       retval = common_threshold_handler(tool);
+                       if (retval)
+                               return retval;
 
-                       if (!params->common.threshold_actions.continue_flag)
-                               /* continue flag not set, break */
+                       if (!should_continue_tracing(tool->params))
                                break;
 
-                       /* continue action reached, re-enable tracing */
-                       if (tool->record)
-                               trace_instance_start(&tool->record->trace);
-                       if (tool->aa)
-                               trace_instance_start(&tool->aa->trace);
-                       timerlat_bpf_restart_tracing();
+                       if (timerlat_bpf_restart_tracing()) {
+                               err_msg("Error restarting BPF trace\n");
+                               return -1;
+                       }
                }
        }
        timerlat_bpf_detach();
index 8dd04c5ffb686c47e39f5a0d063b0dbed2d4daa5..64cbdcc878b0d080cf7f3b5c13a37ffac56dd912 100644 (file)
@@ -17,6 +17,7 @@
 #include "timerlat.h"
 #include "timerlat_aa.h"
 #include "timerlat_bpf.h"
+#include "common.h"
 
 struct timerlat_top_cpu {
        unsigned long long      irq_count;
@@ -795,10 +796,10 @@ out_err:
 static int
 timerlat_top_bpf_main_loop(struct osnoise_tool *tool)
 {
-       struct timerlat_params *params = to_timerlat_params(tool->params);
+       const struct common_params *params = tool->params;
        int retval, wait_retval;
 
-       if (params->common.aa_only) {
+       if (params->aa_only) {
                /* Auto-analysis only, just wait for stop tracing */
                timerlat_bpf_wait(-1);
                return 0;
@@ -806,8 +807,8 @@ timerlat_top_bpf_main_loop(struct osnoise_tool *tool)
 
        /* Pull and display data in a loop */
        while (!stop_tracing) {
-               wait_retval = timerlat_bpf_wait(params->common.quiet ? -1 :
-                                               params->common.sleep_time);
+               wait_retval = timerlat_bpf_wait(params->quiet ? -1 :
+                                               params->sleep_time);
 
                retval = timerlat_top_bpf_pull_data(tool);
                if (retval) {
@@ -815,28 +816,27 @@ timerlat_top_bpf_main_loop(struct osnoise_tool *tool)
                        return retval;
                }
 
-               if (!params->common.quiet)
+               if (!params->quiet)
                        timerlat_print_stats(tool);
 
                if (wait_retval != 0) {
                        /* Stopping requested by tracer */
-                       actions_perform(&params->common.threshold_actions);
+                       retval = common_threshold_handler(tool);
+                       if (retval)
+                               return retval;
 
-                       if (!params->common.threshold_actions.continue_flag)
-                               /* continue flag not set, break */
+                       if (!should_continue_tracing(tool->params))
                                break;
 
-                       /* continue action reached, re-enable tracing */
-                       if (tool->record)
-                               trace_instance_start(&tool->record->trace);
-                       if (tool->aa)
-                               trace_instance_start(&tool->aa->trace);
-                       timerlat_bpf_restart_tracing();
+                       if (timerlat_bpf_restart_tracing()) {
+                               err_msg("Error restarting BPF trace\n");
+                               return -1;
+                       }
                }
 
                /* is there still any user-threads ? */
-               if (params->common.user_workload) {
-                       if (params->common.user.stopped_running) {
+               if (params->user_workload) {
+                       if (params->user.stopped_running) {
                                debug_msg("timerlat user space threads stopped!\n");
                                break;
                        }