Skip to contentSkip to conversation

rtla/timerlat: Add auto-analysis action and report spike timestamps

RFC PATCH New Tomas Glozar 2 participants 13 messages Nov 27

Subsystems (3)
lore
Share & tools
lore

Tomas Glozar

author maintainer
#0 lore raw

cover rtla/timerlat: Add auto-analysis action and report spike timestamps

=== Problem ===

rtla timerlat already supports reacting to multiple spikes in one run by resuming tracing using --on-threshold continue,

    $ rtla timerlat -q --thread 1000 --duration 10s \
        --on-threshold shell,command="echo Tracing stopped on latency spike" \
        --on-threshold trace \
        --on-threshold shell,command="echo Trace output:; tail -n5 timerlat_trace.txt" \
        --on-threshold continue

will stop tracing after each latency spike over 1000us and print the tail of its trace:

Tracing stopped on latency spike
Saving trace to timerlat_trace.txt
Trace output:
<idle>-0 [003] dn.h2.. 10507.972291: irq_noise: local_timer:236 start 10507.972288915 duration 2208 ns
<idle>-0 [003] d...3.. 10507.972293: thread_noise: swapper/3:0 start 10507.972289122 duration 1464 ns
timerlatu/3-23927 [003] .....11 10507.972293: timerlat_sample: timer_latency=2319269 seqnum=8683 context=1
timerlatu/3-23927 [003] d..h312 10507.972297: irq_noise: irq_work:246 start 10507.972295820 duration 1014 ns
timerlatu/3-23927 [003] .....11 10507.972297: #8683 context thread timer_latency 2319269 ns
Tracing stopped on latency spike
Saving trace to timerlat_trace.txt
Trace output:
<idle>-0 [000] dn.h2.. 10508.176124: irq_noise: local_timer:236 start 10508.176121606 duration 2369 ns
<idle>-0 [000] d...3.. 10508.176126: thread_noise: swapper/0:0 start 10508.176121766 duration 1973 ns
timerlatu/0-23924 [000] .....11 10508.176127: timerlat_sample: timer_latency=1138875 seqnum=8894 context=1
timerlatu/0-23924 [000] d..h312 10508.176131: irq_noise: irq_work:246 start 10508.176129579 duration 872 ns
timerlatu/0-23924 [000] .....11 10508.176131: #8894 context thread timer_latency 1138875 ns
Timer Latency
0 00:00:12 | IRQ Timer Latency (us) | Thread Timer Latency (us) | Ret user Timer Latency (us)
CPU COUNT | cur min avg max | cur min avg max | cur min avg max
0 #7185 | 0 0 1 1358 | 3 2 4 1367 | 4 3 5 1000
1 #7175 | 0 0 5 1361 | 3 2 8 1368 | 4 3 7 987
2 #7178 | 0 0 1 1468 | 4 2 4 1474 | 5 3 4 433
3 #7184 | 0 0 3 2315 | 2 2 7 2319 | 4 3 6 667
---------------|----------------------------------------|----------------------------------------|---------------------------------------
ALL #28722 e0 | 0 2 2315 | 2 6 2319 | 3 6 1000

However, one cannot use rtla timerlat's built-in auto-analysis to analyze multiple spikes, because auto-analysis is not an action - instead, it is done at the end of measurement and only if measurement stopped by the spike, i.e. not resumed --on-threshold continue and stopped by the user or duration timer.

=== Proposed solution ===

This patchset adds a new action, "aa", that allows auto-analysis to be used right after a spike is detected.

This makes analyzing multiple spikes in a single run possible by allowing the measurement to be resumed after the auto-analysis.

Example (output squished for better readability):

    # rtla timerlat top -q --thread 1200 --duration 10s --on-threshold aa --on-threshold continue
    [  0d 00:00:00.310] ## CPU 1 hit stop tracing, analyzing it ## (seqnum 703)
    IRQ handler delay:      1247.64 us (99.44 %)
    IRQ latency:            1248.12 us
    Timerlat IRQ duration:     2.55 us (0.20 %)
    Blocking thread:           1.26 us (0.10 %)
        kwin_wayland:5350      1.26 us
    IRQ interference           2.02 us (0.16 %)
        irq_work:246           2.02 us
    Softirq interference       0.62 us (0.05 %)
        SCHED:7                0.62 us
    -------------------------------------------
    Thread latency:         1254.68 us (100%)
    Max timerlat IRQ latency from idle: 167.32 us in cpu 2
    [  0d 00:00:00.360] ## CPU 1 hit stop tracing, analyzing it ## (seqnum 752)
    IRQ handler delay:      1259.94 us (99.40 %)
    IRQ latency:            1260.40 us
    Timerlat IRQ duration:     3.60 us (0.28 %)
    Blocking thread:           1.16 us (0.09 %)
        kwin_wayland:5350      1.16 us
    IRQ interference           1.68 us (0.13 %)
        irq_work:246           1.68 us
    Softirq interference       0.69 us (0.05 %)
        SCHED:7                0.60 us
        RCU:9                  0.09 us
    -------------------------------------------
    Thread latency:         1267.53 us (100 %)
    Max timerlat IRQ latency from idle: 11.55 us in cpu 12
                                            Timer Latency
    0 00:00:11    |  IRQ Timer Latency (us)  | Thread Timer Latency (us) | Ret user Timer Latency (us)
    CPU COUNT     |  cur   min   avg    max  |  cur   min   avg    max   |  cur   min   avg    max
    0 #9943       |    1     0     0   1085  |    4     1     2   1091   |    7     2     3   1092
    1 #9943       |    0     0     0   1260  |    2     1     2   1267   |    3     2     2    966
    2 #9945       |    0     0     0    167  |    1     1     1    169   |    2     2     2    170
    3 #9944       |    0     0     0     93  |    2     1     1     95   |    3     2     2     96
    4 #9946       |    1     0     0     35  |    3     2     2     40   |    5     3     3     43
    5 #9945       |    1     0     0     12  |    2     2     2     54   |    3     3     3     63
    6 #9944       |    1     0     0     15  |    3     2     2     21   |    3     3     3     32
    7 #9945       |    1     0     0    131  |    3     2     3    137   |    4     3     3    140
    8 #9945       |    1     0     0     15  |    2     2     2     60   |    3     3     3     62
    9 #9944       |    1     0     0     14  |    2     2     2     22   |    3     3     3     28
    10 #9945      |    1     0     0     12  |    2     2     2     23   |    3     3     3     31
    11 #9944      |    1     0     0     14  |    2     2     2     20   |    3     3     3     29
    12 #9945      |    1     0     1     11  |    5     4     5     32   |    6     5     7     41
    13 #9946      |    1     1     1     11  |    5     4     5     26   |    8     5     7     33
    --------------|--------------------------|---------------------------|----------------------------
    ALL #139224 e0|          0     0   1260  |          1     2   1267   |          2     3   1092

Timestamps are collected as offsets from the starting value of the tracefs clock per-CPU and displayed along the auto-analysis output, together with timerlat sequence numbers, to distinguish between individual spikes.

The feature also makes auto-analysis flexible in other ways, for example, it can be made conditional on the success of the preceding action, or clearing the terminal (via shell,command=clear) can be added before it.

When the aa action is not used, the default end-of-measurement analysis is unchanged. When it is used, rtla skips the default analysis, as the action already ran on the stopped trace.

=== Known functional limitations ===

- Specifying "--on-threshold aa" together with "--aa-only ..." option of rtla-timerlat-top skips the "Max latency was ..." message at the end, if no latency has been seen. This happens because the default end-of-measurement analysis is skipped, the message is a part of it in the current implementation.
- When aa action is triggered multiple times in a row to analyze the same spike, for example, when "--on-threshold aa" is passed twice, or when there is both "--on-threshold aa" and "--on-end aa", the first run consumes the trace data, so there is nothing left to analyze for the subsequent runs. (Likely minor.)
- The formatting of the auto-analysis timestamp is inconsistent with the top elapsed time format: the former prints days with "d" suffix, the later without it. A unified format should be chosen.

=== Known implementation limitations ===

- Commit "rtla: Support auto-analysis as an action" folds in multiple changes of both auto-analysis and actions necessary for the auto-analysis action itself (passing context through struct actions, resetting aa structures, ...). Ideally, it should be separated into multiple commits.
- struct actions takes a pointer to the whole struct osnoise_tool instead of only the required parts, creating surface for bugs, e.g. in initialization order.
- timerlat_aa_reset() resets each field manually, creating the risk that a newly added field is not reset there.

=== Further related feature ideas ===

- When "--on-threshold aa" is used, summarize all spikes at the end of tracing.
- Allow saving the auto-analysis output into a file, optionally with a suffix, so that latter results would not overwrite earlier ones.

The aforementioned limitations will, if everything goes well, be addressed in a follow-up version of the patchset.

Suggestions for further improvements, changes to the output or interface, or implementation comments are welcome.

Tomas Glozar (10):
rtla: Support auto-analysis as an action
rtla/tests: Add unit tests for aa action
rtla/tests: Add runtime tests for aa action Documentation/rtla: Document aa action
rtla/trace: Add helper to read "now ts" stats field rtla/trace: Add helper to gather "now ts" over a CPU set rtla/trace: Allow storing start clock value of tracefs instances rtla/timerlat: Save the trace clock at measurement start rtla/timerlat_aa: Print time offset and seqnum of the spike Documentation/rtla: Document auto-analysis timestamps

 Documentation/tools/rtla/common_options.txt   |  12 +
 .../tools/rtla/rtla-timerlat-top.rst          |   9 +-
 tools/tracing/rtla/src/actions.c              |  39 ++-
 tools/tracing/rtla/src/actions.h              |   8 +-
 tools/tracing/rtla/src/cli.c                  |   6 +
 tools/tracing/rtla/src/common.c               |  21 +-
 tools/tracing/rtla/src/common.h               |   7 +
 tools/tracing/rtla/src/timerlat.c             |  14 ++
 tools/tracing/rtla/src/timerlat_aa.c          | 109 ++++++++-
 tools/tracing/rtla/src/trace.c                | 226 ++++++++++++++++++
 tools/tracing/rtla/src/trace.h                |  10 +
 tools/tracing/rtla/tests/osnoise.t            |   4 +
 tools/tracing/rtla/tests/timerlat.t           |  13 +
 tools/tracing/rtla/tests/unit/actions.c       |  31 ++-
 14 files changed, 483 insertions(+), 26 deletions(-)
Signature · 1 line
2.56.1
author maintainer
#1 lore raw

RFC PATCH 1/10 rtla: Support auto-analysis as an action

rtla currently performs auto-analysis of a latency spike at the end of the measurement, after the results (top/hist) are printed, in case tracing was stopped with -i/--irq, -T/--thread, -s/--stop, -S/--stop-total, or -a/--auto, and was not resumed with --on-threshold continue.

This has multiple disadvantages:

- The user is not able to make auto-analysis run before other actions, e.g. trace output.
- The user is not able to make auto-analysis run conditionally depending on the result of the previous actions.
- Auto-analysis can only be used to analyze a latency that stops tracing without resuming, thus, only one latency spike can be analyzed per rtla run.

To provide more flexibility, add an option to run auto-analysis as an action (--on-threshold aa, --on-end aa). The default behavior of auto-analyzing at the end of the measurement stays unchanged.

If the auto-analysis action is present, the default auto-analysis at the end is skipped to avoid analyzing twice.

---
 tools/tracing/rtla/src/actions.c        | 39 +++++++++++---
 tools/tracing/rtla/src/actions.h        |  8 ++-
 tools/tracing/rtla/src/cli.c            |  6 +++
 tools/tracing/rtla/src/common.c         | 21 +++++---
 tools/tracing/rtla/src/common.h         |  7 +++
 tools/tracing/rtla/src/timerlat_aa.c    | 67 +++++++++++++++++++++++++
 tools/tracing/rtla/src/trace.c          |  8 +++
 tools/tracing/rtla/src/trace.h          |  1 +
 tools/tracing/rtla/tests/unit/actions.c | 10 ++--
 9 files changed, 147 insertions(+), 20 deletions(-)
tools/tracing/rtla/src/actions.c +33 -6
index bf13d9d68f169..a554539a522c6 100644
@@ -4,9 +4,7 @@
 #include <signal.h>
 #include <unistd.h>
 
-#include "actions.h"
-#include "trace.h"
-#include "utils.h"
+#include "common.h"
 
 /*
  * actions_init - initialize struct actions
@@ -20,7 +18,7 @@ actions_init(struct actions *self)
 	self->continue_flag = false;
 
 	/* This has to be set by the user */
-	self->trace_output_inst = NULL;
+	self->tool = NULL;
 }
 
 /*
@@ -111,6 +109,17 @@ actions_add_continue(struct actions *self)
 	action->type = ACTION_CONTINUE;
 }
 
+/*
+ * actions_add_aa - add an action to perform auto-analysis
+ */
+void actions_add_aa(struct actions *self)
+{
+	struct action *action = actions_new(self);
+
+	self->present[ACTION_AA] = true;
+	action->type = ACTION_AA;
+}
+
 static inline const char *__extract_arg(const char *token, const char *opt, size_t opt_len)
 {
 	const size_t tok_len = strlen(token);
@@ -164,6 +173,8 @@ actions_parse(struct actions *self, const char *trigger, const char *tracefn)
 		type = ACTION_SHELL;
 	else if (strcmp(token, "continue") == 0)
 		type = ACTION_CONTINUE;
+	else if (strcmp(token, "aa") == 0)
+		type = ACTION_AA;
 	else
 		/* Invalid trigger type */
 		return -1;
@@ -231,6 +242,12 @@ actions_parse(struct actions *self, const char *trigger, const char *tracefn)
 			return -1;
 		actions_add_continue(self);
 		break;
+	case ACTION_AA:
+		/* Takes no argument */
+		if (token != NULL)
+			return -1;
+		actions_add_aa(self);
+		break;
 	default:
 		return -1;
 	}
@@ -240,9 +257,12 @@ actions_parse(struct actions *self, const char *trigger, const char *tracefn)
 
 /*
  * actions_perform - perform all actions
+ * @stopped: Whether tracing was stopped on a latency threshold.
+ *
+ * Returns 0 if all actions succeeded, or an error code if an action failed.
  */
 int
-actions_perform(struct actions *self)
+actions_perform(struct actions *self, bool stopped)
 {
 	int pid, retval;
 	const struct action *action;
@@ -252,7 +272,8 @@ actions_perform(struct actions *self)
 	for_each_action(self, action) {
 		switch (action->type) {
 		case ACTION_TRACE_OUTPUT:
-			retval = save_trace_to_file(self->trace_output_inst, action->trace_output);
+			retval = save_trace_to_file(self->tool->record->trace.inst,
+						    action->trace_output);
 			if (retval) {
 				err_msg("Error saving trace\n");
 				return retval;
@@ -277,6 +298,12 @@ actions_perform(struct actions *self)
 		case ACTION_CONTINUE:
 			self->continue_flag = true;
 			return 0;
+		case ACTION_AA:
+			if (self->tool->ops->analyze)
+				self->tool->ops->analyze(self->tool, stopped);
+			/* Ensure correct ordering with respect to shell command output */
+			fflush(stdout);
+			break;
 		default:
 			break;
 		}
tools/tracing/rtla/src/actions.h +6 -2
index 034048682fefb..2688f22bae90d 100644
@@ -2,12 +2,15 @@
 #include <tracefs.h>
 #include <stdbool.h>
 
+struct osnoise_tool;
+
 enum action_type {
 	ACTION_NONE = 0,
 	ACTION_TRACE_OUTPUT,
 	ACTION_SIGNAL,
 	ACTION_SHELL,
 	ACTION_CONTINUE,
+	ACTION_AA,
 	ACTION_FIELD_N
 };
 
@@ -39,7 +42,7 @@ struct actions {
 	bool continue_flag;
 
 	/* External dependencies */
-	struct tracefs_instance *trace_output_inst;
+	struct osnoise_tool *tool;
 };
 
 #define for_each_action(actions, action)			\
@@ -53,5 +56,6 @@ void actions_add_trace_output(struct actions *self, const char *trace_output);
 void actions_add_signal(struct actions *self, int signal, int pid);
 void actions_add_shell(struct actions *self, const char *command);
 void actions_add_continue(struct actions *self);
+void actions_add_aa(struct actions *self);
 int actions_parse(struct actions *self, const char *trigger, const char *tracefn);
-int actions_perform(struct actions *self);
+int actions_perform(struct actions *self, bool stopped);
tools/tracing/rtla/src/cli.c +6 -0
index 8c02b94ce4321..9742c70485e5b 100644
@@ -128,6 +128,9 @@ struct common_params *osnoise_top_parse_args(int argc, char **argv)
 	if (geteuid() && !in_unit_test)
 		fatal("osnoise needs root permission");
 
+	if (aa_action_present(&params->common))
+		fatal("auto-analysis action is not supported with osnoise tools");
+
 	return &params->common;
 }
 
@@ -210,6 +213,9 @@ struct common_params *osnoise_hist_parse_args(int argc, char **argv)
 	if (geteuid() && !in_unit_test)
 		fatal("rtla needs root permission");
 
+	if (aa_action_present(&params->common))
+		fatal("auto-analysis action is not supported with osnoise tools");
+
 	if (params->common.hist.no_index && !params->common.hist.with_zeros)
 		fatal("no-index set and with-zeros not set - it does not make sense");
 
tools/tracing/rtla/src/common.c +14 -7
index ac4172bcc4444..e085eb6c8f13f 100644
@@ -138,7 +138,7 @@ common_apply_config(struct osnoise_tool *tool, struct common_params *params)
 int
 common_threshold_handler(const struct osnoise_tool *tool)
 {
-	actions_perform(&tool->params->threshold_actions);
+	actions_perform(&tool->params->threshold_actions, true);
 
 	if (!should_continue_tracing(tool->params))
 		/* continue flag not set, break */
@@ -147,7 +147,8 @@ common_threshold_handler(const struct osnoise_tool *tool)
 	/* 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))
+	if (tool->aa && (trace_instance_clear(&tool->aa->trace) ||
+	    trace_instance_start(&tool->aa->trace)))
 		goto err;
 
 	return 0;
@@ -197,6 +198,13 @@ int run_tool(struct tool_ops *ops, int argc, char *argv[])
 	 */
 	trace_tool = tool;
 
+	/*
+	 * Expose the tool to actions, so they can access the trace output
+	 * instance and auto-analysis callback.
+	 */
+	params->threshold_actions.tool = tool;
+	params->end_actions.tool = tool;
+
 	retval = ops->apply_config(tool);
 	if (retval) {
 		err_msg("Could not apply config\n");
@@ -233,8 +241,6 @@ int run_tool(struct tool_ops *ops, int argc, char *argv[])
 			err_msg("Failed to enable the trace instance\n");
 			goto out_free;
 		}
-		params->threshold_actions.trace_output_inst = tool->record->trace.inst;
-		params->end_actions.trace_output_inst = tool->record->trace.inst;
 
 		if (params->events) {
 			retval = trace_events_enable(&tool->record->trace, params->events);
@@ -290,17 +296,18 @@ int run_tool(struct tool_ops *ops, int argc, char *argv[])
 
 	ops->print_stats(tool);
 
-	actions_perform(&params->end_actions);
-
 	return_value = PASSED;
 
 	stopped = osnoise_trace_is_off(tool, tool->record) && !stop_tracing;
+
+	actions_perform(&params->end_actions, stopped);
+
 	if (stopped) {
 		printf("%s hit stop tracing\n", ops->tracer);
 		return_value = FAILED;
 	}
 
-	if (ops->analyze)
+	if (ops->analyze && !aa_action_present(params))
 		ops->analyze(tool, stopped);
 
 out_signals:
tools/tracing/rtla/src/common.h +7 -0
index ec2f6f86ec3a0..ed0e318d8e151 100644
@@ -166,6 +166,13 @@ should_continue_tracing(const struct common_params *params)
 	return params->threshold_actions.continue_flag;
 }
 
+static inline bool
+aa_action_present(const struct common_params *params)
+{
+	return params->threshold_actions.present[ACTION_AA] ||
+		params->end_actions.present[ACTION_AA];
+}
+
 int
 common_threshold_handler(const struct osnoise_tool *tool);
 
tools/tracing/rtla/src/timerlat_aa.c +67 -0

79 lines not on this page · raw message

tools/tracing/rtla/src/trace.c +8 -0
index 4b5f4d033d8b4..c608393533494 100644
@@ -267,6 +267,14 @@ int trace_instance_stop(struct trace_instance *trace)
 	return tracefs_trace_off(trace->inst);
 }
 
+/*
+ * trace_instance_clear - clear all samples of a given rtla instance
+ */
+int trace_instance_clear(struct trace_instance *trace)
+{
+	return tracefs_instance_file_write(trace->inst, "trace", "");
+}
+
 /*
  * trace_events_free - free a list of trace events
  */
tools/tracing/rtla/src/trace.h +1 -0
index 94e4510db2d19..b3a626b676b3d 100644
@@ -26,6 +26,7 @@ struct trace_instance {
 int trace_instance_init(struct trace_instance *trace, char *tool_name);
 int trace_instance_start(struct trace_instance *trace);
 int trace_instance_stop(struct trace_instance *trace);
+int trace_instance_clear(struct trace_instance *trace);
 void trace_instance_destroy(struct trace_instance *trace);
 
 struct trace_seq *get_trace_seq(void);
tools/tracing/rtla/tests/unit/actions.c +5 -5
index 94ad5ad42774b..6d91ad698a925 100644
@@ -27,7 +27,7 @@ START_TEST(test_actions_init)
 	ck_assert_int_eq(actions.len, 0);
 	ck_assert_int_eq(actions.size, action_default_size);
 	ck_assert(!actions.continue_flag);
-	ck_assert_ptr_eq(actions.trace_output_inst, NULL);
+	ck_assert_ptr_eq(actions.tool, NULL);
 }
 END_TEST
 
@@ -302,7 +302,7 @@ END_TEST
 START_TEST(test_actions_perform_continue)
 {
 	actions_add_continue(&actions_fixture);
-	ck_assert_int_eq(actions_perform(&actions_fixture), 0);
+	ck_assert_int_eq(actions_perform(&actions_fixture, true), 0);
 
 	ck_assert(actions_fixture.continue_flag);
 }
@@ -312,7 +312,7 @@ START_TEST(test_actions_perform_continue_after_successful_shell_command)
 {
 	actions_add_shell(&actions_fixture, "exit 0");
 	actions_add_continue(&actions_fixture);
-	ck_assert_int_eq(actions_perform(&actions_fixture), 0 << 8);
+	ck_assert_int_eq(actions_perform(&actions_fixture, true), 0 << 8);
 
 	ck_assert(actions_fixture.continue_flag);
 }
@@ -322,7 +322,7 @@ START_TEST(test_actions_perform_continue_after_failed_shell_command)
 {
 	actions_add_shell(&actions_fixture, "exit 1");
 	actions_add_continue(&actions_fixture);
-	ck_assert_int_eq(actions_perform(&actions_fixture), 1 << 8);
+	ck_assert_int_eq(actions_perform(&actions_fixture, true), 1 << 8);
 
 	ck_assert(!actions_fixture.continue_flag);
 }
@@ -334,7 +334,7 @@ START_TEST(test_actions_perform_continue_unset_flag)
 
 	actions_add_shell(&actions_fixture, "exit 1");
 	actions_add_continue(&actions_fixture);
-	ck_assert_int_eq(actions_perform(&actions_fixture), 1 << 8);
+	ck_assert_int_eq(actions_perform(&actions_fixture, true), 1 << 8);
 
 	ck_assert(!actions_fixture.continue_flag);
 }
Signature · 1 line
2.56.1
#2 lore raw

Re: [RFC PATCH 01/10] rtla: Support auto-analysis as an action

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] rtla: timerlat_top_bpf_main_loop() skips aa action when --aa-only is set - [Low] rtla: repeated aa…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] rtla: timerlat_top_bpf_main_loop() skips aa action when --aa-only is set
- [Low] rtla: repeated aa action on stopped trace wipes state in timerlat_auto_analysis()
--

commit 3b613e42d174f9f624d5fde84c78336d0d724233
Author: Tomas Glozar <[email protected]>

rtla: Support auto-analysis as an action

This commit adds an "aa" action for --on-threshold and --on-end so that timerlat auto-analysis can be ordered with other actions and run multiple times when tracing continues, while skipping the default end-of-run auto-analysis when an aa action is configured.

Quoted 4 lines
tools/tracing/rtla/src/common.c +0 -0
index ac4172bcc4444..e085eb6c8f13f 100644

[ ... ]

Quoted 10 lines
tools/tracing/rtla/src/common.c +1 -2
@@ -290,17 +296,18 @@ int run_tool(struct tool_ops *ops, int argc, char *argv[])
 
 	ops->print_stats(tool);
 
-	actions_perform(&params->end_actions);
-
 	return_value = PASSED;
 
 	stopped = osnoise_trace_is_off(tool, tool->record) && !stop_tracing;
+

tools/tracing/rtla/src/common.c int run_tool(struct tool_ops *ops, int argc, char *argv[]) · line 311

Quoted 10 lines
tools/tracing/rtla/src/common.c +3 -1
+	actions_perform(&params->end_actions, stopped);
+
 	if (stopped) {
 		printf("%s hit stop tracing\n", ops->tracer);
 		return_value = FAILED;
 	}
 
-	if (ops->analyze)
+	if (ops->analyze && !aa_action_present(params))
 		ops->analyze(tool, stopped);

[Severity: Medium]
Does skipping ops->analyze(tool, stopped) here break --aa-only when combined with --on-threshold aa?

In BPF/mixed mode (the default when BPF is available), timerlat_top_bpf_main_loop() returns immediately after timerlat_bpf_wait(-1) when params->aa_only is set, without calling common_threshold_handler():

tools/tracing/rtla/src/timerlat_top.c:timerlat_top_bpf_main_loop() {
	if (params->aa_only) {
		/* Auto-analysis only, just wait for stop tracing */
		timerlat_bpf_wait(-1);
		return 0;
	}
    ...
}

Because threshold_actions never run in that path and run_tool() now skips ops->analyze() when aa_action_present(params) is true, running rtla timerlat top --aa-only <us> --on-threshold aa [--on-threshold continue] performs no auto-analysis at all and ignores continue.

Also, in both BPF and tracefs modes, if --aa-only is used with --on-threshold aa and the run finishes without hitting the threshold (stopped is false), skipping ops->analyze(tool, false) here bypasses the tracing_max_latency fallback in timerlat_analyze().

Quoted 4 lines
tools/tracing/rtla/src/timerlat_aa.c +0 -0
index a121ff9d2c571..11ddceab208ac 100644

[ ... ]

tools/tracing/rtla/src/timerlat_aa.c void timerlat_auto_analysis(int irq_thresh, int thread_thresh) · line 822

Quoted 8 lines
tools/tracing/rtla/src/timerlat_aa.c +3 -0
@@ -752,6 +816,9 @@ void timerlat_auto_analysis(int irq_thresh, int thread_thresh)
 	struct tep_handle *tep;
 	int cpu;
 
+	/* reset any data left over from previous run */
+	timerlat_aa_reset(taa_ctx);
+
 	timerlat_auto_analysis_collect_trace(taa_ctx);

[Severity: Low]
What happens if the aa action runs more than once on the same stopped trace without an intervening continue (for example, when both --on-threshold aa and --on-end aa are specified and tracing stops on a threshold)?

The first call to timerlat_auto_analysis_collect_trace() drains the per-CPU trace_pipe_raw buffers via tracefs_iterate_raw_events(). A second timerlat_auto_analysis() call on the same stopped trace wipes taa_ctx in timerlat_aa_reset() and reads zero events from the already consumed pipe, producing empty analysis output (and printing empty comms with PID 0 for every CPU if --dump-tasks is enabled).

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author maintainer
#3 lore raw

RFC PATCH 2/10 rtla/tests: Add unit tests for aa action

In reply to Tomas Glozar

Add unit tests for the new auto-analysis action (aa), mirroring the existing tests for continue.

---
 tools/tracing/rtla/tests/unit/actions.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
tools/tracing/rtla/tests/unit/actions.c +21 -0
index 6d91ad698a925..6ab9638c2d256 100644
@@ -291,6 +291,25 @@ START_TEST(test_actions_parse_continue_arg_bad)
 }
 END_TEST
 
+START_TEST(test_actions_parse_aa)
+{
+	ck_assert_int_eq(actions_parse(&actions_fixture, "aa", NULL), 0);
+
+	ck_assert_int_eq(actions_fixture.len, 1);
+	ck_assert_int_eq(actions_fixture.list[0].type, ACTION_AA);
+	ck_assert(actions_fixture.present[ACTION_AA]);
+}
+END_TEST
+
+START_TEST(test_actions_parse_aa_arg_bad)
+{
+	ck_assert_int_eq(actions_parse(&actions_fixture, "aa,foo=bar", NULL), -1);
+
+	ck_assert_int_eq(actions_fixture.len, 0);
+	ck_assert(!actions_fixture.present[ACTION_AA]);
+}
+END_TEST
+
 START_TEST(test_actions_parse_invalid)
 {
 	ck_assert_int_eq(actions_parse(&actions_fixture, "foobar", NULL), -1);
@@ -378,6 +397,8 @@ Suite *actions_suite(void)
 	tcase_add_test(tc, test_actions_parse_shell_arg_bad);
 	tcase_add_test(tc, test_actions_parse_continue);
 	tcase_add_test(tc, test_actions_parse_continue_arg_bad);
+	tcase_add_test(tc, test_actions_parse_aa);
+	tcase_add_test(tc, test_actions_parse_aa_arg_bad);
 	tcase_add_test(tc, test_actions_parse_invalid);
 	suite_add_tcase(s, tc);
 
Signature · 1 line
2.56.1
author maintainer
#4 lore raw

RFC PATCH 3/10 rtla/tests: Add runtime tests for aa action

In reply to Tomas Glozar

Cover auto-analysis action with runtime tests: the base case, repeated auto-analysis with continue action, and combination of auto-analysis action with --aa-only.

Verify that the aa action is correctly rejected for osnoise tools, as osnoise does not currently support auto-analysis.

Also, separate the action test cases into blocks to make them more readable, as they have grown into a quite long list.

---
 tools/tracing/rtla/tests/osnoise.t  |  4 ++++
 tools/tracing/rtla/tests/timerlat.t | 13 +++++++++++++
 2 files changed, 17 insertions(+)
tools/tracing/rtla/tests/osnoise.t +4 -0
index 352b302372453..d0173fe20caa5 100644
@@ -100,5 +100,9 @@ check_top_hist "with trace output at end" \
 	"osnoise TOOL -d 1s --on-end trace" 0 "^  Saving trace to osnoise_trace.txt$"
 check_top_q_hist "action with no threshold" \
 	"osnoise TOOL -d 1s --on-threshold shell,command='echo BAD'" 0 "" "BAD"
+check_top_hist "reject aa action on threshold" \
+	"osnoise TOOL --on-threshold aa" 1 "auto-analysis action is not supported with osnoise tools"
+check_top_hist "reject aa action on end" \
+	"osnoise TOOL --on-end aa" 1 "auto-analysis action is not supported with osnoise tools"
 
 test_end
tools/tracing/rtla/tests/timerlat.t +13 -0
index aab026ead6c55..2ebd1858be0bb 100644
@@ -122,18 +122,31 @@ check_top_q_hist "trace output through --on-threshold trace" \
 	"timerlat TOOL -T 2 --on-threshold trace" 2 "^  Saving trace to timerlat_trace.txt$"
 check_top_q_hist "trace output through --on-threshold trace with custom filename" \
 	"timerlat TOOL -T 2 --on-threshold trace,file=custom_filename.txt" 2 "^  Saving trace to custom_filename.txt$"
+
 check_top_q_hist "exec command" \
 	"timerlat TOOL -T 2 --on-threshold shell,command='echo TestOutput'" 2 "^TestOutput$"
+
+check_top_hist "aa action" \
+	"timerlat TOOL -T 2 -d 3s --on-threshold aa" 2 "analyzing it"
+check_top_hist "aa action at end" \
+	"timerlat TOOL -T 2 -d 3s --on-end aa" 2 "analyzing it"
+check_top_hist "aa action with continue" \
+	"timerlat TOOL -T 2 -d 3s --on-threshold aa --on-threshold continue" 0 "analyzing it"
+check "aa action with --aa-only" \
+	"timerlat top -d 3s --aa-only 2 --on-threshold aa --on-threshold continue" 0 "analyzing it" "Timer Latency"
+
 check_top_q_hist "multiple actions" \
 	"timerlat TOOL -T 2 --on-threshold shell,command='echo -n 1' --on-threshold shell,command='echo 2'" 2 "^12$"
 check "hist stop at failed action" \
 	"timerlat hist -T 2 --on-threshold shell,command='echo -n 1; false' --on-threshold shell,command='echo -n 2'" 2 "^1# RTLA timerlat histogram$"
 check "top stop at failed action" \
 	"timerlat top -T 2 --on-threshold shell,command='echo -n abc; false' --on-threshold shell,command='echo -n defgh'" 2 "^abc" "defgh"
+
 check_top_q_hist "with continue" \
 	"timerlat TOOL -T 2 -d 5s --on-threshold shell,command='echo TestOutput' --on-threshold continue" 0 "^TestOutput$"
 check_top_q_hist "with conditional continue" \
 	"timerlat TOOL -T 2 --on-threshold shell,command='if [ -f a ]; then echo 2; exit 1; else echo -n 1; touch a; fi' --on-threshold continue" 2 "^12$" "^2$"
+
 check_top_hist "with trace output at end" \
 	"timerlat TOOL -d 1s --on-end trace" 0 "^  Saving trace to timerlat_trace.txt$"
 check_top_q_hist "action with no threshold" \
Signature · 1 line
2.56.1
#5 lore raw

Re: [RFC PATCH 03/10] rtla/tests: Add runtime tests for aa action

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] rtla/tests: --aa-only test fails in BPF mode in timerlat_top_bpf_main_loop() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] rtla/tests: --aa-only test fails in BPF mode in timerlat_top_bpf_main_loop()
--

commit 3e2a3aa570e971109ae2e849934141658e12c520
Author: Tomas Glozar <[email protected]>

rtla/tests: Add runtime tests for aa action

This commit adds runtime test cases covering the auto-analysis action for timerlat and verifying that osnoise rejects the action.

Quoted 10 lines
tools/tracing/rtla/tests/timerlat.t +1 -0
index 1d317c851a0fa..d4f660d32f882 100644
@@ -122,18 +122,31 @@ check_top_q_hist "trace output through --on-threshold trace" \
 	"timerlat TOOL -T 2 --on-threshold trace" 2 "^  Saving trace to timerlat_trace.txt$"
 check_top_q_hist "trace output through --on-threshold trace with custom filename" \
 	"timerlat TOOL -T 2 --on-threshold trace,file=custom_filename.txt" 2 "^  Saving trace to custom_filename.txt$"
+
 check_top_q_hist "exec command" \

tools/tracing/rtla/tests/timerlat.t check_top_q_hist "trace output through --on-threshold trace" \ · line 136

Quoted 10 lines
tools/tracing/rtla/tests/timerlat.t +9 -0
 	"timerlat TOOL -T 2 --on-threshold shell,command='echo TestOutput'" 2 "^TestOutput$"
+
+check_top_hist "aa action" \
+	"timerlat TOOL -T 2 -d 3s --on-threshold aa" 2 "analyzing it"
+check_top_hist "aa action at end" \
+	"timerlat TOOL -T 2 -d 3s --on-end aa" 2 "analyzing it"
+check_top_hist "aa action with continue" \
+	"timerlat TOOL -T 2 -d 3s --on-threshold aa --on-threshold continue" 0 "analyzing it"
+check "aa action with --aa-only" \
+	"timerlat top -d 3s --aa-only 2 --on-threshold aa --on-threshold continue" 0 "analyzing it" "Timer Latency"

[Severity: Medium]
Does the "aa action with --aa-only" test fail when run in BPF mode (RTLA_NO_BPF=0)?

In BPF/mixed mode, timerlat_top_bpf_main_loop() takes the early return for params->aa_only as soon as the first threshold event arrives, without running common_threshold_handler() or checking should_continue_tracing():

tools/tracing/rtla/src/timerlat_top.c:timerlat_top_bpf_main_loop() {
	if (params->aa_only) {
		/* Auto-analysis only, just wait for stop tracing */
		timerlat_bpf_wait(-1);
		return 0;
	}
    ...
}

Because it returns before SIGALRM sets stop_tracing, run_tool() sees stopped == true (setting return_value = FAILED, exit status 2) and skips the end-of-run ops->analyze() call because aa_action_present(params) is true:

tools/tracing/rtla/src/common.c:run_tool() {
    ...
	if (stopped) {
		printf("%s hit stop tracing\n", ops->tracer);
		return_value = FAILED;
	}

	if (ops->analyze && !aa_action_present(params))
		ops->analyze(tool, stopped);
    ...
}

As a result, neither the threshold actions nor end-of-run auto-analysis run, "analyzing it" is never printed, and rtla exits with status 2 instead of 0.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
author maintainer
#6 lore raw

RFC PATCH 4/10 Documentation/rtla: Document aa action

In reply to Tomas Glozar

Add the new "aa" action to the list of actions under the --on-threshold option and clarify that it replaces the default auto-analysis done at the end of the measurement.

---
 Documentation/tools/rtla/common_options.txt | 12 ++++++++++++
 1 file changed, 12 insertions(+)
Documentation/tools/rtla/common_options.txt +12 -0
index 6bf0f86393333..01e8a5bd3553a 100644
@@ -94,6 +94,15 @@
           the trace being saved multiple times, and --no-trace will not disable trace
           output when enabled through this option.
 
+        - *aa*
+
+          Triggers auto-analysis of the measured latency.
+
+          Selecting this action skips the auto-analysis done by default at the end
+          of measurement when a latency threshold is hit.
+
+          This action is only supported with timerlat tools.
+
         - *signal,num=<sig>,pid=<pid>*
 
           Sends signal to process. "parent" might be specified in place of pid to target
@@ -134,6 +143,9 @@
         See the documentation for **--on-threshold** for the list of supported actions, with
         the exception that *continue* has no effect.
 
+        If **--on-end aa** triggers without a latency spike having caused the
+        end of tracing, the action is ignored.
+
         Example:
 
         $ rtla |tool| -d 5s --on-end trace
Signature · 1 line
2.56.1
author maintainer
#7 lore raw

RFC PATCH 5/10 rtla/trace: Add helper to read "now ts" stats field

In reply to Tomas Glozar

Add trace_get_cpu_now_ts() to read the current timestamp of a given CPU from the per-CPU stats file.

The returned value is in nanoseconds in the instance clock, matching the trace event timestamps, so it can be used as a per-CPU reference to compute how long before a sample the measurement started.

The "now ts" field reports the clock of the CPU executing the read, not the clock of the CPU owning the stats file, so callers must be running on the CPU they are reading.

The stats file reports the timestamp as seconds.microseconds, so the value is reported with microsecond granularity. Clocks that use a non-nanosecond unit (in_ns == 0) are unsupported.

---
 tools/tracing/rtla/src/trace.c | 42 ++++++++++++++++++++++++++++++++++
 tools/tracing/rtla/src/trace.h |  2 ++
 2 files changed, 44 insertions(+)
tools/tracing/rtla/src/trace.c +42 -0
index c608393533494..e068f25759c00 100644
@@ -275,6 +275,48 @@ int trace_instance_clear(struct trace_instance *trace)
 	return tracefs_instance_file_write(trace->inst, "trace", "");
 }
 
+/*
+ * trace_get_cpu_now_ts - get the current timestamp of a CPU
+ * @inst: the trace instance, NULL for the top instance
+ * @cpu: the CPU to read
+ * @ts: output, timestamp in nanoseconds in the instance clock
+ *
+ * The "now ts" field reports the clock of the CPU executing the read, not
+ * the clock of the CPU owning the stats file, so the caller must be running
+ * on @cpu.
+ *
+ * The output has microsecond granularity, matching the format of per-cpu stats
+ * reported by tracefs. Clocks that operate in non-nanosecond time units
+ * (in_ns == 0) are not supported.
+ *
+ * Returns 0 on success, -1 otherwise.
+ */
+int trace_get_cpu_now_ts(struct tracefs_instance *inst, int cpu,
+			 unsigned long long *ts)
+{
+	unsigned long long sec, usec;
+	char path[MAX_PATH];
+	char *buf, *line;
+	int ret = -1;
+
+	snprintf(path, sizeof(path), "per_cpu/cpu%d/stats", cpu);
+
+	buf = tracefs_instance_file_read(inst, path, NULL);
+	if (!buf)
+		return -1;
+
+	line = strstr(buf, "now ts:");
+	if (!line || sscanf(line, "now ts: %llu.%llu", &sec, &usec) != 2)
+		goto out;
+
+	*ts = sec * 1000000000ULL + usec * 1000ULL;
+	ret = 0;
+
+out:
+	free(buf);
+	return ret;
+}
+
 /*
  * trace_events_free - free a list of trace events
  */
tools/tracing/rtla/src/trace.h +2 -0
index b3a626b676b3d..4ec8259ccdd71 100644
@@ -37,6 +37,8 @@ struct tracefs_instance *create_instance(char *instance_name);
 void destroy_instance(struct tracefs_instance *inst);
 
 int save_trace_to_file(struct tracefs_instance *inst, const char *filename);
+int trace_get_cpu_now_ts(struct tracefs_instance *inst, int cpu,
+			 unsigned long long *ts);
 int collect_registered_events(struct tep_event *tep, struct tep_record *record,
 			      int cpu, void *context);
 
Signature · 1 line
2.56.1
author maintainer
#8 lore raw

RFC PATCH 6/10 rtla/trace: Add helper to gather "now ts" over a CPU set

In reply to Tomas Glozar

Add trace_get_now_ts_cpus() to read the current timestamp of a set of CPUs. One thread is spawned per CPU; each thread pins itself to its CPU and reads the clock there, storing the value in its own per-CPU slot of the output array.

Reading the clock from a thread pinned to the target CPU avoids migrating the calling thread around, keeping its affinity untouched.

The threads are held behind a gate until all of them have been created, then released together by a barrier, so the clock reads happen at nearly the same time.

CPUs that cannot be pinned or read are left at 0, and the helper returns -1, while still filling the CPUs that succeeded.

---
 tools/tracing/rtla/src/trace.c | 140 +++++++++++++++++++++++++++++++++
 tools/tracing/rtla/src/trace.h |   3 +
 2 files changed, 143 insertions(+)
tools/tracing/rtla/src/trace.c +140 -0
index e068f25759c00..a007479768f99 100644
@@ -4,6 +4,7 @@
 #include <tracefs.h>
 #include <stdlib.h>
 #include <unistd.h>
+#include <pthread.h>
 #include <errno.h>
 
 #include "trace.h"
@@ -317,6 +318,145 @@ int trace_get_cpu_now_ts(struct tracefs_instance *inst, int cpu,
 	return ret;
 }
 
+struct trace_cpu_ts_ctx {
+	pthread_mutex_t mutex;
+	pthread_cond_t cond;
+	pthread_barrier_t barrier;
+	bool go;
+	bool abort;
+};
+
+struct trace_cpu_ts_arg {
+	struct tracefs_instance *inst;
+	unsigned long long *ts;
+	struct trace_cpu_ts_ctx *ctx;
+	pthread_t thread;
+	bool created;
+	int cpu;
+	int retval;
+};
+
+static void *trace_cpu_ts_thread(void *data)
+{
+	struct trace_cpu_ts_arg *arg = data;
+	struct trace_cpu_ts_ctx *ctx = arg->ctx;
+	cpu_set_t affinity;
+	bool pinned;
+
+	CPU_ZERO(&affinity);
+	CPU_SET(arg->cpu, &affinity);
+
+	/*
+	 * Never read the clock without being pinned: the timestamp would be
+	 * the one of whichever CPU this thread happens to be running on.
+	 * A failure to pin must still reach the barrier, or the other threads
+	 * would wait forever.
+	 */
+	pinned = !sched_setaffinity(0, sizeof(affinity), &affinity);
+
+	pthread_mutex_lock(&ctx->mutex);
+	while (!ctx->go)
+		pthread_cond_wait(&ctx->cond, &ctx->mutex);
+	pthread_mutex_unlock(&ctx->mutex);
+
+	if (ctx->abort)
+		return NULL;
+
+	pthread_barrier_wait(&ctx->barrier);
+
+	if (pinned)
+		arg->retval = trace_get_cpu_now_ts(arg->inst, arg->cpu,
+						   &arg->ts[arg->cpu]);
+	return NULL;
+}
+
+/*
+ * trace_get_now_ts_cpus - get the current timestamp of a set of CPUs
+ * @inst: the trace instance, NULL for the top instance
+ * @cpus: the set of CPUs to read
+ * @ts: output array with nr_cpus entries, indexed by CPU
+ *
+ * Spawn a thread for each CPU in @cpus. Each thread pins itself to its CPU
+ * and reads the clock there, writing the timestamp to its own ts[cpu] slot.
+ * CPUs that cannot be read are left at 0.
+ *
+ * The threads are held after pinning until all of them were created, then
+ * released together by a barrier, so that the per-CPU clock readings happen
+ * at (nearly) the same time.
+ *
+ * Returns 0 on success, -1 if any CPU in @cpus could not be read.
+ */
+int trace_get_now_ts_cpus(struct tracefs_instance *inst, const cpu_set_t *cpus,
+			  unsigned long long *ts)
+{
+	struct trace_cpu_ts_ctx ctx = {
+		.mutex = PTHREAD_MUTEX_INITIALIZER,
+		.cond = PTHREAD_COND_INITIALIZER,
+	};
+	struct trace_cpu_ts_arg *args;
+	int cpu, count = 0, ret = 0;
+
+	args = calloc_fatal(nr_cpus, sizeof(*args));
+
+	for (cpu = 0; cpu < nr_cpus; cpu++) {
+		if (!CPU_ISSET(cpu, cpus))
+			continue;
+
+		ts[cpu] = 0;
+
+		args[cpu].inst = inst;
+		args[cpu].ts = ts;
+		args[cpu].ctx = &ctx;
+		args[cpu].cpu = cpu;
+		args[cpu].retval = -1;
+
+		if (pthread_create(&args[cpu].thread, NULL, trace_cpu_ts_thread,
+				   &args[cpu])) {
+			debug_msg("Could not create thread for cpu %d\n", cpu);
+			ret = -1;
+			continue;
+		}
+
+		args[cpu].created = true;
+		count++;
+	}
+
+	if (count) {
+		/*
+		 * The barrier can only be sized now that the number of created
+		 * threads is known. If it cannot be set up, release the threads
+		 * without reading the clock.
+		 */
+		if (pthread_barrier_init(&ctx.barrier, NULL, count)) {
+			debug_msg("Could not initialize thread barrier\n");
+			ctx.abort = true;
+		}
+
+		pthread_mutex_lock(&ctx.mutex);
+		ctx.go = true;
+		pthread_cond_broadcast(&ctx.cond);
+		pthread_mutex_unlock(&ctx.mutex);
+	}
+
+	for (cpu = 0; cpu < nr_cpus; cpu++) {
+		if (!args[cpu].created)
+			continue;
+
+		pthread_join(args[cpu].thread, NULL);
+		if (args[cpu].retval)
+			ret = -1;
+	}
+
+	if (count && !ctx.abort)
+		pthread_barrier_destroy(&ctx.barrier);
+	pthread_cond_destroy(&ctx.cond);
+	pthread_mutex_destroy(&ctx.mutex);
+
+	free(args);
+
+	return ret;
+}
+
 /*
  * trace_events_free - free a list of trace events
  */
tools/tracing/rtla/src/trace.h +3 -0
index 4ec8259ccdd71..ad9eaaa61b77a 100644
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0
 #include <tracefs.h>
 #include <stddef.h>
+#include <sched.h>
 
 struct trace_events {
 	struct trace_events *next;
@@ -39,6 +40,8 @@ void destroy_instance(struct tracefs_instance *inst);
 int save_trace_to_file(struct tracefs_instance *inst, const char *filename);
 int trace_get_cpu_now_ts(struct tracefs_instance *inst, int cpu,
 			 unsigned long long *ts);
+int trace_get_now_ts_cpus(struct tracefs_instance *inst, const cpu_set_t *cpus,
+			  unsigned long long *ts);
 int collect_registered_events(struct tep_event *tep, struct tep_record *record,
 			      int cpu, void *context);
 
Signature · 1 line
2.56.1
author maintainer
#9 lore raw

RFC PATCH 7/10 rtla/trace: Allow storing start clock value of tracefs instances

In reply to Tomas Glozar

Add a per-CPU start_ts array to struct trace_instance, allocated and freed with the instance, and helpers to save the current clock into it and to copy it between instances.

---
 tools/tracing/rtla/src/trace.c | 36 ++++++++++++++++++++++++++++++++++
 tools/tracing/rtla/src/trace.h |  4 ++++
 2 files changed, 40 insertions(+)
tools/tracing/rtla/src/trace.c +36 -0
index a007479768f99..1885219cdb589 100644
@@ -200,6 +200,11 @@ void trace_instance_destroy(struct trace_instance *trace)
 		tep_free(trace->tep);
 		trace->tep = NULL;
 	}
+
+	if (trace->start_ts) {
+		free(trace->start_ts);
+		trace->start_ts = NULL;
+	}
 }
 
 /*
@@ -219,6 +224,8 @@ int trace_instance_init(struct trace_instance *trace, char *tool_name)
 
 	trace_seq_init(trace->seq);
 
+	trace->start_ts = calloc_fatal(nr_cpus, sizeof(*trace->start_ts));
+
 	trace->inst = create_instance(tool_name);
 	if (!trace->inst)
 		goto out_err;
@@ -276,6 +283,35 @@ int trace_instance_clear(struct trace_instance *trace)
 	return tracefs_instance_file_write(trace->inst, "trace", "");
 }
 
+/*
+ * trace_instance_save_start - save the start timestamp of a given instance
+ * @trace: the trace instance
+ * @cpus: the set of CPUs to read
+ *
+ * Read the current timestamp of each CPU in @cpus and store it in the
+ * instance's start_ts array. Must be called before tracing is enabled.
+ *
+ * Returns 0 on success, -1 if any CPU could not be read.
+ */
+int trace_instance_save_start(struct trace_instance *trace, const cpu_set_t *cpus)
+{
+	return trace_get_now_ts_cpus(trace->inst, cpus, trace->start_ts);
+}
+
+/*
+ * trace_instance_copy_start - copy the start timestamps between instances
+ * @dst: the destination trace instance
+ * @src: the source trace instance
+ *
+ * The per-CPU clock is not tied to a trace instance, so the timestamps
+ * captured for one instance are valid for every other instance as well.
+ */
+void trace_instance_copy_start(struct trace_instance *dst,
+			       const struct trace_instance *src)
+{
+	memcpy(dst->start_ts, src->start_ts, nr_cpus * sizeof(*src->start_ts));
+}
+
 /*
  * trace_get_cpu_now_ts - get the current timestamp of a CPU
  * @inst: the trace instance, NULL for the top instance
tools/tracing/rtla/src/trace.h +4 -0
index ad9eaaa61b77a..5a3cdb14f86a7 100644
@@ -19,6 +19,7 @@ struct trace_instance {
 	struct tracefs_instance		*inst;
 	struct tep_handle		*tep;
 	struct trace_seq		*seq;
+	unsigned long long		*start_ts;
 	unsigned long long		missed_events;
 	unsigned long long		invalid_events;
 	unsigned long long		processed_events;
@@ -28,6 +29,9 @@ int trace_instance_init(struct trace_instance *trace, char *tool_name);
 int trace_instance_start(struct trace_instance *trace);
 int trace_instance_stop(struct trace_instance *trace);
 int trace_instance_clear(struct trace_instance *trace);
+int trace_instance_save_start(struct trace_instance *trace, const cpu_set_t *cpus);
+void trace_instance_copy_start(struct trace_instance *dst,
+			       const struct trace_instance *src);
 void trace_instance_destroy(struct trace_instance *trace);
 
 struct trace_seq *get_trace_seq(void);
Signature · 1 line
2.56.1
author maintainer
#10 lore raw

RFC PATCH 8/10 rtla/timerlat: Save the trace clock at measurement start

In reply to Tomas Glozar

Save the measurement start reference after the warm-up and before any instance starts tracing. The per-CPU clock is shared by all instances, so the clock is read once and copied to the record and auto-analysis instances.

Reading the reference before tracing is enabled avoids perturbing an already running measurement, and keeping the read in the enable path avoids re-reading it when tracing is restarted with --on-threshold continue.

---
 tools/tracing/rtla/src/timerlat.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
tools/tracing/rtla/src/timerlat.c +14 -0
index 169aa9a6569d6..8ae8241978491 100644
@@ -173,6 +173,20 @@ int timerlat_enable(struct osnoise_tool *tool)
 			return -1;
 	}
 
+	/*
+	 * Save the measurement start reference before any instance starts
+	 * tracing: the per-CPU clock is shared by all instances, so a single
+	 * reading is enough for all of them.
+	 */
+	if (!params->no_aa) {
+		if (trace_instance_save_start(&tool->trace,
+					      &params->common.monitored_cpus))
+			debug_msg("Could not read all the start timestamps\n");
+		if (tool->record)
+			trace_instance_copy_start(&tool->record->trace, &tool->trace);
+		trace_instance_copy_start(&tool->aa->trace, &tool->trace);
+	}
+
 	/*
 	 * Start the tracers here, after having set all instances.
 	 *
Signature · 1 line
2.56.1
author maintainer
#11 lore raw

RFC PATCH 9/10 rtla/timerlat_aa: Print time offset and seqnum of the spike

In reply to Tomas Glozar

Print the time elapsed since the beginning of the measurement, taken from the triggering sample, and the sequence number of that sample in the latency spike header.

This makes it possible to tell multiple spikes apart, which is useful when tracing is not stopped on the first occurrence, for example with --on-threshold continue.

The time is printed as days, hours, minutes, seconds and milliseconds, prefixed to the existing header. If the start reference is not available, the header keeps its previous format, with the sequence number appended.

---
 tools/tracing/rtla/src/timerlat_aa.c | 42 ++++++++++++++++++++++++----
 1 file changed, 37 insertions(+), 5 deletions(-)
tools/tracing/rtla/src/timerlat_aa.c +37 -5
index 11ddceab208ac..3c9b86e2f40cc 100644
@@ -570,14 +570,30 @@ static int timerlat_aa_kworker_start_handler(struct trace_seq *s, struct tep_rec
 	return 0;
 }
 
+/*
+ * timerlat_aa_format_elapsed - format a duration as days/hours/min/sec
+ */
+static void timerlat_aa_format_elapsed(unsigned long long ns, char *buf,
+				       size_t len)
+{
+	unsigned long long sec = ns / 1000000000ULL;
+
+	snprintf(buf, len, "%3llud %02llu:%02llu:%02llu.%03llu",
+		 sec / 86400, sec / 3600 % 24, sec / 60 % 60, sec % 60,
+		 ns / 1000000 % 1000);
+}
+
 /*
  * timerlat_thread_analysis - Prints the analysis of a CPU that hit a stop tracing
  *
  * This is the core of the analysis.
  */
 static void timerlat_thread_analysis(struct timerlat_aa_data *taa_data, int cpu,
-				     int irq_thresh, int thread_thresh)
+				     int irq_thresh, int thread_thresh,
+				     unsigned long long start_ts)
 {
+	unsigned long long seqnum, tstamp;
+	char elapsed[32];
 	long long exp_irq_ts;
 	int total;
 	int irq;
@@ -588,9 +604,23 @@ static void timerlat_thread_analysis(struct timerlat_aa_data *taa_data, int cpu,
 	if (taa_data->tlat_irq_seqnum > taa_data->tlat_thread_seqnum) {
 		irq = 1;
 		total = taa_data->tlat_irq_latency;
+		tstamp = taa_data->tlat_irq_timstamp;
+		seqnum = taa_data->tlat_irq_seqnum;
 	} else {
 		irq = 0;
 		total = taa_data->tlat_thread_latency;
+		tstamp = taa_data->tlat_thread_timstamp;
+		seqnum = taa_data->tlat_thread_seqnum;
+	}
+
+	if (start_ts && tstamp >= start_ts) {
+		timerlat_aa_format_elapsed(tstamp - start_ts, elapsed,
+					   sizeof(elapsed));
+		printf("[%s] ## CPU %d hit stop tracing, analyzing it ## (seqnum %llu)\n",
+		       elapsed, cpu, seqnum);
+	} else {
+		printf("## CPU %d hit stop tracing, analyzing it ## (seqnum %llu)\n",
+		       cpu, seqnum);
 	}
 
 	/*
@@ -829,11 +859,13 @@ void timerlat_auto_analysis(int irq_thresh, int thread_thresh)
 		taa_data = timerlat_aa_get_data(taa_ctx, cpu);
 
 		if (irq_thresh && taa_data->tlat_irq_latency >= irq_thresh) {
-			printf("## CPU %d hit stop tracing, analyzing it ##\n", cpu);
-			timerlat_thread_analysis(taa_data, cpu, irq_thresh, thread_thresh);
+			timerlat_thread_analysis(taa_data, cpu, irq_thresh,
+						 thread_thresh,
+						 taa_ctx->tool->trace.start_ts[cpu]);
 		} else if (thread_thresh && (taa_data->tlat_thread_latency) >= thread_thresh) {
-			printf("## CPU %d hit stop tracing, analyzing it ##\n", cpu);
-			timerlat_thread_analysis(taa_data, cpu, irq_thresh, thread_thresh);
+			timerlat_thread_analysis(taa_data, cpu, irq_thresh,
+						 thread_thresh,
+						 taa_ctx->tool->trace.start_ts[cpu]);
 		}
 
 		if (taa_data->max_exit_idle_latency > max_exit_from_idle) {
Signature · 1 line
2.56.1
author maintainer
#12 lore raw

RFC PATCH 10/10 Documentation/rtla: Document auto-analysis timestamps

In reply to Tomas Glozar

Update the rtla-timerlat-top example to show the new auto-analysis header, with the time elapsed since the beginning of the measurement and the sequence number of the sample that hit the stop condition.

---
 Documentation/tools/rtla/rtla-timerlat-top.rst | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
Documentation/tools/rtla/rtla-timerlat-top.rst +8 -1
index 05959f1a4661b..58469a0b78dfa 100644
@@ -76,7 +76,7 @@ higher is found::
    22 #12319     |        0         0         1        11 |        8         3        10        22
    23 #12320     |       28         0         1        28 |       41         3        11        41
   rtla timerlat hit stop tracing
-  ## CPU 23 hit stop tracing, analyzing it ##
+  [  0d 00:00:03.482] ## CPU 23 hit stop tracing, analyzing it ## (seqnum 4211)
   IRQ handler delay:                                        27.49 us (65.52 %)
   IRQ latency:                                              28.13 us
   Timerlat IRQ duration:                                     9.59 us (22.85 %)
@@ -109,6 +109,13 @@ higher is found::
     Max timerlat IRQ latency from idle: 17.48 us in cpu 4
   Saving trace to timerlat_trace.txt
 
+The value in brackets is the time elapsed since the beginning of the
+measurement on that CPU, in days, hours, minutes, seconds and milliseconds,
+followed by the sequence number of the timerlat sample that hit the stop
+condition. Both identify the sample that caused the trace to stop, which is
+useful when tracing is not stopped on the first occurrence, for example with
+**--on-threshold continue**.
+
 In this case, the major factor was the delay suffered by the *IRQ handler*
 that handles **timerlat** wakeup: *65.52%*. This can be caused by the
 current thread masking interrupts, which can be seen in the blocking
Signature · 1 line
2.56.1