Skip to content

Commit a50c538

Browse files
walaclenticularis39
authored andcommitted
rtla: Introduce common_threshold_handler() helper
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>
1 parent 989e5b8 commit a50c538

4 files changed

Lines changed: 86 additions & 44 deletions

File tree

tools/tracing/rtla/src/common.c

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,38 @@ common_apply_config(struct osnoise_tool *tool, struct common_params *params)
207207
}
208208

209209

210+
/**
211+
* common_threshold_handler - handle latency threshold overflow
212+
* @tool: pointer to the osnoise_tool instance containing trace contexts
213+
*
214+
* Executes the configured threshold actions (e.g., saving trace, printing,
215+
* sending signals). If the continue flag is set (--on-threshold continue),
216+
* restarts the auxiliary trace instances to continue monitoring.
217+
*
218+
* Return: 0 for success, -1 for error.
219+
*/
220+
int
221+
common_threshold_handler(const struct osnoise_tool *tool)
222+
{
223+
actions_perform(&tool->params->threshold_actions);
224+
225+
if (!should_continue_tracing(tool->params))
226+
/* continue flag not set, break */
227+
return 0;
228+
229+
/* continue action reached, re-enable tracing */
230+
if (tool->record && trace_instance_start(&tool->record->trace))
231+
goto err;
232+
if (tool->aa && trace_instance_start(&tool->aa->trace))
233+
goto err;
234+
235+
return 0;
236+
237+
err:
238+
err_msg("Error restarting trace\n");
239+
return -1;
240+
}
241+
210242
int run_tool(struct tool_ops *ops, int argc, char *argv[])
211243
{
212244
struct common_params *params;
@@ -385,17 +417,14 @@ int top_main_loop(struct osnoise_tool *tool)
385417
/* stop tracing requested, do not perform actions */
386418
return 0;
387419

388-
actions_perform(&params->threshold_actions);
420+
retval = common_threshold_handler(tool);
421+
if (retval)
422+
return retval;
423+
389424

390-
if (!params->threshold_actions.continue_flag)
391-
/* continue flag not set, break */
425+
if (!should_continue_tracing(params))
392426
return 0;
393427

394-
/* continue action reached, re-enable tracing */
395-
if (record)
396-
trace_instance_start(&record->trace);
397-
if (tool->aa)
398-
trace_instance_start(&tool->aa->trace);
399428
trace_instance_start(trace);
400429
}
401430

@@ -436,18 +465,14 @@ int hist_main_loop(struct osnoise_tool *tool)
436465
/* stop tracing requested, do not perform actions */
437466
break;
438467

439-
actions_perform(&params->threshold_actions);
468+
retval = common_threshold_handler(tool);
469+
if (retval)
470+
return retval;
440471

441-
if (!params->threshold_actions.continue_flag)
442-
/* continue flag not set, break */
443-
break;
472+
if (!should_continue_tracing(params))
473+
return 0;
444474

445-
/* continue action reached, re-enable tracing */
446-
if (tool->record)
447-
trace_instance_start(&tool->record->trace);
448-
if (tool->aa)
449-
trace_instance_start(&tool->aa->trace);
450-
trace_instance_start(&tool->trace);
475+
trace_instance_start(trace);
451476
}
452477

453478
/* is there still any user-threads ? */

tools/tracing/rtla/src/common.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,24 @@ struct tool_ops {
146146
void (*free)(struct osnoise_tool *tool);
147147
};
148148

149+
/**
150+
* should_continue_tracing - check if tracing should continue after threshold
151+
* @params: pointer to the common parameters structure
152+
*
153+
* Returns true if the continue action was configured (--on-threshold continue),
154+
* indicating that tracing should be restarted after handling the threshold event.
155+
*
156+
* Return: 1 if tracing should continue, 0 otherwise.
157+
*/
158+
static inline int
159+
should_continue_tracing(const struct common_params *params)
160+
{
161+
return params->threshold_actions.continue_flag;
162+
}
163+
164+
int
165+
common_threshold_handler(const struct osnoise_tool *tool);
166+
149167
int osnoise_set_cpus(struct osnoise_context *context, char *cpus);
150168
void osnoise_restore_cpus(struct osnoise_context *context);
151169

tools/tracing/rtla/src/timerlat_hist.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "timerlat.h"
1818
#include "timerlat_aa.h"
1919
#include "timerlat_bpf.h"
20+
#include "common.h"
2021

2122
struct timerlat_hist_cpu {
2223
int *irq;
@@ -1047,26 +1048,24 @@ static struct osnoise_tool
10471048

10481049
static int timerlat_hist_bpf_main_loop(struct osnoise_tool *tool)
10491050
{
1050-
struct timerlat_params *params = to_timerlat_params(tool->params);
10511051
int retval;
10521052

10531053
while (!stop_tracing) {
10541054
timerlat_bpf_wait(-1);
10551055

10561056
if (!stop_tracing) {
10571057
/* Threshold overflow, perform actions on threshold */
1058-
actions_perform(&params->common.threshold_actions);
1058+
retval = common_threshold_handler(tool);
1059+
if (retval)
1060+
return retval;
10591061

1060-
if (!params->common.threshold_actions.continue_flag)
1061-
/* continue flag not set, break */
1062+
if (!should_continue_tracing(tool->params))
10621063
break;
10631064

1064-
/* continue action reached, re-enable tracing */
1065-
if (tool->record)
1066-
trace_instance_start(&tool->record->trace);
1067-
if (tool->aa)
1068-
trace_instance_start(&tool->aa->trace);
1069-
timerlat_bpf_restart_tracing();
1065+
if (timerlat_bpf_restart_tracing()) {
1066+
err_msg("Error restarting BPF trace\n");
1067+
return -1;
1068+
}
10701069
}
10711070
}
10721071
timerlat_bpf_detach();

tools/tracing/rtla/src/timerlat_top.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "timerlat.h"
1818
#include "timerlat_aa.h"
1919
#include "timerlat_bpf.h"
20+
#include "common.h"
2021

2122
struct timerlat_top_cpu {
2223
unsigned long long irq_count;
@@ -795,48 +796,47 @@ static struct osnoise_tool
795796
static int
796797
timerlat_top_bpf_main_loop(struct osnoise_tool *tool)
797798
{
798-
struct timerlat_params *params = to_timerlat_params(tool->params);
799+
const struct common_params *params = tool->params;
799800
int retval, wait_retval;
800801

801-
if (params->common.aa_only) {
802+
if (params->aa_only) {
802803
/* Auto-analysis only, just wait for stop tracing */
803804
timerlat_bpf_wait(-1);
804805
return 0;
805806
}
806807

807808
/* Pull and display data in a loop */
808809
while (!stop_tracing) {
809-
wait_retval = timerlat_bpf_wait(params->common.quiet ? -1 :
810-
params->common.sleep_time);
810+
wait_retval = timerlat_bpf_wait(params->quiet ? -1 :
811+
params->sleep_time);
811812

812813
retval = timerlat_top_bpf_pull_data(tool);
813814
if (retval) {
814815
err_msg("Error pulling BPF data\n");
815816
return retval;
816817
}
817818

818-
if (!params->common.quiet)
819+
if (!params->quiet)
819820
timerlat_print_stats(tool);
820821

821822
if (wait_retval != 0) {
822823
/* Stopping requested by tracer */
823-
actions_perform(&params->common.threshold_actions);
824+
retval = common_threshold_handler(tool);
825+
if (retval)
826+
return retval;
824827

825-
if (!params->common.threshold_actions.continue_flag)
826-
/* continue flag not set, break */
828+
if (!should_continue_tracing(tool->params))
827829
break;
828830

829-
/* continue action reached, re-enable tracing */
830-
if (tool->record)
831-
trace_instance_start(&tool->record->trace);
832-
if (tool->aa)
833-
trace_instance_start(&tool->aa->trace);
834-
timerlat_bpf_restart_tracing();
831+
if (timerlat_bpf_restart_tracing()) {
832+
err_msg("Error restarting BPF trace\n");
833+
return -1;
834+
}
835835
}
836836

837837
/* is there still any user-threads ? */
838-
if (params->common.user_workload) {
839-
if (params->common.user.stopped_running) {
838+
if (params->user_workload) {
839+
if (params->user.stopped_running) {
840840
debug_msg("timerlat user space threads stopped!\n");
841841
break;
842842
}

0 commit comments

Comments
 (0)