Skip to contentSkip to conversation

tools/rtla: Add for_each_monitored_cpu() helper

PATCH In review Costa Shulyupin 2 participants 5 messages Oct 2, 2025 – Nov 24, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 tools/rtla: Add for_each_monitored_cpu() helper

The rtla tools have many instances of iterating over CPUs while checking if they are monitored.

Add a for_each_monitored_cpu() helper macro to make the code more readable and reduce code duplication.

---
 tools/tracing/rtla/src/common.h        |  4 ++++
 tools/tracing/rtla/src/osnoise_hist.c  | 28 ++++++----------------
 tools/tracing/rtla/src/osnoise_top.c   |  4 +---
 tools/tracing/rtla/src/timerlat.c      |  9 ++------
 tools/tracing/rtla/src/timerlat_hist.c | 32 +++++++-------------------
 tools/tracing/rtla/src/timerlat_top.c  |  4 +---
 6 files changed, 23 insertions(+), 58 deletions(-)
tools/tracing/rtla/src/common.h +4 -0
index 355f113a14a3..68cf1c20cb5c 100644
@@ -107,6 +107,10 @@ struct common_params {
 	struct timerlat_u_params user;
 };
 
+#define for_each_monitored_cpu(cpu, nr_cpus, common) \
+	for (cpu = 0; cpu < nr_cpus; cpu++) \
+		if (!(common)->cpus || CPU_ISSET(cpu, &(common)->monitored_cpus))
+
 struct tool_ops;
 
 /*
tools/tracing/rtla/src/osnoise_hist.c +7 -21
index dffb6d0a98d7..844f0468953c 100644
@@ -247,9 +247,7 @@ static void osnoise_hist_header(struct osnoise_tool *tool)
 	if (!params->common.hist.no_index)
 		trace_seq_printf(s, "Index");
 
-	for (cpu = 0; cpu < data->nr_cpus; cpu++) {
-		if (params->common.cpus && !CPU_ISSET(cpu, &params->common.monitored_cpus))
-			continue;
+	for_each_monitored_cpu(cpu, data->nr_cpus, &params->common) {
 
 		if (!data->hist[cpu].count)
 			continue;
@@ -278,9 +276,7 @@ osnoise_print_summary(struct osnoise_params *params,
 	if (!params->common.hist.no_index)
 		trace_seq_printf(trace->seq, "count:");
 
-	for (cpu = 0; cpu < data->nr_cpus; cpu++) {
-		if (params->common.cpus && !CPU_ISSET(cpu, &params->common.monitored_cpus))
-			continue;
+	for_each_monitored_cpu(cpu, data->nr_cpus, &params->common) {
 
 		if (!data->hist[cpu].count)
 			continue;
@@ -292,9 +288,7 @@ osnoise_print_summary(struct osnoise_params *params,
 	if (!params->common.hist.no_index)
 		trace_seq_printf(trace->seq, "min:  ");
 
-	for (cpu = 0; cpu < data->nr_cpus; cpu++) {
-		if (params->common.cpus && !CPU_ISSET(cpu, &params->common.monitored_cpus))
-			continue;
+	for_each_monitored_cpu(cpu, data->nr_cpus, &params->common) {
 
 		if (!data->hist[cpu].count)
 			continue;
@@ -307,9 +301,7 @@ osnoise_print_summary(struct osnoise_params *params,
 	if (!params->common.hist.no_index)
 		trace_seq_printf(trace->seq, "avg:  ");
 
-	for (cpu = 0; cpu < data->nr_cpus; cpu++) {
-		if (params->common.cpus && !CPU_ISSET(cpu, &params->common.monitored_cpus))
-			continue;
+	for_each_monitored_cpu(cpu, data->nr_cpus, &params->common) {
 
 		if (!data->hist[cpu].count)
 			continue;
@@ -325,9 +317,7 @@ osnoise_print_summary(struct osnoise_params *params,
 	if (!params->common.hist.no_index)
 		trace_seq_printf(trace->seq, "max:  ");
 
-	for (cpu = 0; cpu < data->nr_cpus; cpu++) {
-		if (params->common.cpus && !CPU_ISSET(cpu, &params->common.monitored_cpus))
-			continue;
+	for_each_monitored_cpu(cpu, data->nr_cpus, &params->common) {
 
 		if (!data->hist[cpu].count)
 			continue;
@@ -362,9 +352,7 @@ osnoise_print_stats(struct osnoise_tool *tool)
 			trace_seq_printf(trace->seq, "%-6d",
 					 bucket * data->bucket_size);
 
-		for (cpu = 0; cpu < data->nr_cpus; cpu++) {
-			if (params->common.cpus && !CPU_ISSET(cpu, &params->common.monitored_cpus))
-				continue;
+		for_each_monitored_cpu(cpu, data->nr_cpus, &params->common) {
 
 			if (!data->hist[cpu].count)
 				continue;
@@ -400,9 +388,7 @@ osnoise_print_stats(struct osnoise_tool *tool)
 	if (!params->common.hist.no_index)
 		trace_seq_printf(trace->seq, "over: ");
 
-	for (cpu = 0; cpu < data->nr_cpus; cpu++) {
-		if (params->common.cpus && !CPU_ISSET(cpu, &params->common.monitored_cpus))
-			continue;
+	for_each_monitored_cpu(cpu, data->nr_cpus, &params->common) {
 
 		if (!data->hist[cpu].count)
 			continue;
tools/tracing/rtla/src/osnoise_top.c +1 -3
index 95418f7ecc96..defa1eb63bee 100644
@@ -243,9 +243,7 @@ osnoise_print_stats(struct osnoise_tool *top)
 
 	osnoise_top_header(top);
 
-	for (i = 0; i < nr_cpus; i++) {
-		if (params->common.cpus && !CPU_ISSET(i, &params->common.monitored_cpus))
-			continue;
+	for_each_monitored_cpu(i, nr_cpus, &params->common) {
 		osnoise_top_print(top, i);
 	}
 
tools/tracing/rtla/src/timerlat.c +2 -7
index b69212874127..28ea4f6710c1 100644
@@ -126,9 +126,7 @@ int timerlat_enable(struct osnoise_tool *tool)
 
 		nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
 
-		for (i = 0; i < nr_cpus; i++) {
-			if (params->common.cpus && !CPU_ISSET(i, &params->common.monitored_cpus))
-				continue;
+		for_each_monitored_cpu(i, nr_cpus, &params->common) {
 			if (save_cpu_idle_disable_state(i) < 0) {
 				err_msg("Could not save cpu idle state.\n");
 				return -1;
@@ -221,10 +219,7 @@ void timerlat_free(struct osnoise_tool *tool)
 	if (dma_latency_fd >= 0)
 		close(dma_latency_fd);
 	if (params->deepest_idle_state >= -1) {
-		for (i = 0; i < nr_cpus; i++) {
-			if (params->common.cpus &&
-			    !CPU_ISSET(i, &params->common.monitored_cpus))
-				continue;
+		for_each_monitored_cpu(i, nr_cpus, &params->common) {
 			restore_cpu_idle_disable_state(i);
 		}
 	}
tools/tracing/rtla/src/timerlat_hist.c +8 -24
index 606c1688057b..02d3ffd2cf4b 100644
@@ -305,9 +305,7 @@ static void timerlat_hist_header(struct osnoise_tool *tool)
 	if (!params->common.hist.no_index)
 		trace_seq_printf(s, "Index");
 
-	for (cpu = 0; cpu < data->nr_cpus; cpu++) {
-		if (params->common.cpus && !CPU_ISSET(cpu, &params->common.monitored_cpus))
-			continue;
+	for_each_monitored_cpu(cpu, data->nr_cpus, &params->common) {
 
 		if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count)
 			continue;
@@ -359,9 +357,7 @@ timerlat_print_summary(struct timerlat_params *params,
 	if (!params->common.hist.no_index)
 		trace_seq_printf(trace->seq, "count:");
 
-	for (cpu = 0; cpu < data->nr_cpus; cpu++) {
-		if (params->common.cpus && !CPU_ISSET(cpu, &params->common.monitored_cpus))
-			continue;
+	for_each_monitored_cpu(cpu, data->nr_cpus, &params->common) {
 
 		if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count)
 			continue;
@@ -383,9 +379,7 @@ timerlat_print_summary(struct timerlat_params *params,
 	if (!params->common.hist.no_index)
 		trace_seq_printf(trace->seq, "min:  ");
 
-	for (cpu = 0; cpu < data->nr_cpus; cpu++) {
-		if (params->common.cpus && !CPU_ISSET(cpu, &params->common.monitored_cpus))
-			continue;
+	for_each_monitored_cpu(cpu, data->nr_cpus, &params->common) {
 
 		if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count)
 			continue;
@@ -413,9 +407,7 @@ timerlat_print_summary(struct timerlat_params *params,
 	if (!params->common.hist.no_index)
 		trace_seq_printf(trace->seq, "avg:  ");
 
-	for (cpu = 0; cpu < data->nr_cpus; cpu++) {
-		if (params->common.cpus && !CPU_ISSET(cpu, &params->common.monitored_cpus))
-			continue;
+	for_each_monitored_cpu(cpu, data->nr_cpus, &params->common) {
 
 		if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count)
 			continue;
@@ -443,9 +435,7 @@ timerlat_print_summary(struct timerlat_params *params,
 	if (!params->common.hist.no_index)
 		trace_seq_printf(trace->seq, "max:  ");
 
-	for (cpu = 0; cpu < data->nr_cpus; cpu++) {
-		if (params->common.cpus && !CPU_ISSET(cpu, &params->common.monitored_cpus))
-			continue;
+	for_each_monitored_cpu(cpu, data->nr_cpus, &params->common) {
 
 		if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count)
 			continue;
@@ -490,9 +480,7 @@ timerlat_print_stats_all(struct timerlat_params *params,
 	sum.min_thread = ~0;
 	sum.min_user = ~0;
 
-	for (cpu = 0; cpu < data->nr_cpus; cpu++) {
-		if (params->common.cpus && !CPU_ISSET(cpu, &params->common.monitored_cpus))
-			continue;
+	for_each_monitored_cpu(cpu, data->nr_cpus, &params->common) {
 
 		if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count)
 			continue;
@@ -639,9 +627,7 @@ timerlat_print_stats(struct osnoise_tool *tool)
 			trace_seq_printf(trace->seq, "%-6d",
 					 bucket * data->bucket_size);
 
-		for (cpu = 0; cpu < data->nr_cpus; cpu++) {
-			if (params->common.cpus && !CPU_ISSET(cpu, &params->common.monitored_cpus))
-				continue;
+		for_each_monitored_cpu(cpu, data->nr_cpus, &params->common) {
 
 			if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count)
 				continue;
@@ -679,9 +665,7 @@ timerlat_print_stats(struct osnoise_tool *tool)
 	if (!params->common.hist.no_index)
 		trace_seq_printf(trace->seq, "over: ");
 
-	for (cpu = 0; cpu < data->nr_cpus; cpu++) {
-		if (params->common.cpus && !CPU_ISSET(cpu, &params->common.monitored_cpus))
-			continue;
+	for_each_monitored_cpu(cpu, data->nr_cpus, &params->common) {
 
 		if (!data->hist[cpu].irq_count && !data->hist[cpu].thread_count)
 			continue;
tools/tracing/rtla/src/timerlat_top.c +1 -3
index fc479a0dcb59..607b57f2f231 100644
@@ -459,9 +459,7 @@ timerlat_print_stats(struct osnoise_tool *top)
 
 	timerlat_top_header(params, top);
 
-	for (i = 0; i < nr_cpus; i++) {
-		if (params->common.cpus && !CPU_ISSET(i, &params->common.monitored_cpus))
-			continue;
+	for_each_monitored_cpu(i, nr_cpus, &params->common) {
 		timerlat_top_print(top, i);
 		timerlat_top_update_sum(top, i, &summary);
 	}
Signature · 1 line
2.51.0
#1 lore raw

PATCH 2/2 tools/rtla: Remove unused optional option_index

The longindex argument of getopt_long() is optional and tied to the unused local variable option_index.

Remove it to shorten the four longest functions
and make the code neater.

---
 tools/tracing/rtla/src/osnoise_hist.c  | 5 +----
 tools/tracing/rtla/src/osnoise_top.c   | 5 +----
 tools/tracing/rtla/src/timerlat_hist.c | 5 +----
 tools/tracing/rtla/src/timerlat_top.c  | 5 +----
 4 files changed, 4 insertions(+), 16 deletions(-)
tools/tracing/rtla/src/osnoise_hist.c +1 -4
index 844f0468953c..df0657b78980 100644
@@ -524,11 +524,8 @@ static struct common_params
 			{0, 0, 0, 0}
 		};
 
-		/* getopt_long stores the option index here. */
-		int option_index = 0;
-
 		c = getopt_long(argc, argv, "a:c:C::b:d:e:E:DhH:p:P:r:s:S:t::T:01234:5:6:7:",
-				 long_options, &option_index);
+				 long_options, NULL);
 
 		/* detect the end of the options. */
 		if (c == -1)
tools/tracing/rtla/src/osnoise_top.c +1 -4
index defa1eb63bee..1b5181e66b17 100644
@@ -376,11 +376,8 @@ struct common_params *osnoise_top_parse_args(int argc, char **argv)
 			{0, 0, 0, 0}
 		};
 
-		/* getopt_long stores the option index here. */
-		int option_index = 0;
-
 		c = getopt_long(argc, argv, "a:c:C::d:De:hH:p:P:qr:s:S:t::T:0:1:2:3:",
-				 long_options, &option_index);
+				 long_options, NULL);
 
 		/* Detect the end of the options. */
 		if (c == -1)
tools/tracing/rtla/src/timerlat_hist.c +1 -4
index 02d3ffd2cf4b..c432ef5f59e7 100644
@@ -840,11 +840,8 @@ static struct common_params
 			{0, 0, 0, 0}
 		};
 
-		/* getopt_long stores the option index here. */
-		int option_index = 0;
-
 		c = getopt_long(argc, argv, "a:c:C::b:d:e:E:DhH:i:knp:P:s:t::T:uU0123456:7:8:9\1\2:\3:",
-				 long_options, &option_index);
+				 long_options, NULL);
 
 		/* detect the end of the options. */
 		if (c == -1)
tools/tracing/rtla/src/timerlat_top.c +1 -4
index 607b57f2f231..82e227d27af7 100644
@@ -604,11 +604,8 @@ static struct common_params
 			{0, 0, 0, 0}
 		};
 
-		/* getopt_long stores the option index here. */
-		int option_index = 0;
-
 		c = getopt_long(argc, argv, "a:c:C::d:De:hH:i:knp:P:qs:t::T:uU0:1:2:345:6:7:",
-				 long_options, &option_index);
+				 long_options, NULL);
 
 		/* detect the end of the options. */
 		if (c == -1)
Signature · 1 line
2.51.0
maintainer Reviewed-by on v1 2/2
#2 lore raw

Re: [PATCH v1 2/2] tools/rtla: Remove unused optional option_index

čt 2. 10. 2025 v 14:36 odesílatel Costa Shulyupin
<[email protected]> napsal:

Quoted 7 lines

The longindex argument of getopt_long() is optional and tied to the unused local variable option_index.

Remove it to shorten the four longest functions
and make the code neater.

Yes, this is documented in the manpage:

"If longindex is not NULL, it points to a variable which is set to the index of the long option relative to longopts."

Quoted 8 lines
---
 tools/tracing/rtla/src/osnoise_hist.c  | 5 +----
 tools/tracing/rtla/src/osnoise_top.c   | 5 +----
 tools/tracing/rtla/src/timerlat_hist.c | 5 +----
 tools/tracing/rtla/src/timerlat_top.c  | 5 +----
 4 files changed, 4 insertions(+), 16 deletions(-)

Tomas

maintainer Reviewed-by on v1 1/2
#3 lore raw
In reply to Costa Shulyupin

čt 2. 10. 2025 v 14:36 odesílatel Costa Shulyupin
<[email protected]> napsal:

Quoted 7 lines

The rtla tools have many instances of iterating over CPUs while checking if they are monitored.

Add a for_each_monitored_cpu() helper macro to make the code more readable and reduce code duplication.

Yeah this iteration is repeated many times.

Quoted 10 lines
---
 tools/tracing/rtla/src/common.h        |  4 ++++
 tools/tracing/rtla/src/osnoise_hist.c  | 28 ++++++----------------
 tools/tracing/rtla/src/osnoise_top.c   |  4 +---
 tools/tracing/rtla/src/timerlat.c      |  9 ++------
 tools/tracing/rtla/src/timerlat_hist.c | 32 +++++++-------------------
 tools/tracing/rtla/src/timerlat_top.c  |  4 +---
 6 files changed, 23 insertions(+), 58 deletions(-)

Tomas

maintainer
#4 lore raw
In reply to Costa Shulyupin

čt 2. 10. 2025 v 14:36 odesílatel Costa Shulyupin
<[email protected]> napsal:

tools/tracing/rtla/src/common.h struct common_params { · line 113

Quoted 4 lines
tools/tracing/rtla/src/common.h +4 -0
+#define for_each_monitored_cpu(cpu, nr_cpus, common) \
+       for (cpu = 0; cpu < nr_cpus; cpu++) \
+               if (!(common)->cpus || CPU_ISSET(cpu, &(common)->monitored_cpus))
+

There is one case where this form of macro is not ideal. Consider this code:

if (something)

    for_each_monitored_cpu(i, nr_cpus, &params->common) {
        do_something()
    }
else
    something_else();

Here, since the macro includes an "if" statement, it will eat the else block (see [1]), and something_else() will be executed for all non-monitored CPUs (which might be zero times, or many times, instead of just once, as one would expect). This would not happen for the other for_each_* macros used in the Linux kernel, since they do not include an if.

This can be fixed by moving the if to a function called from inside the for loop:

static inline int next_monitored_cpu(int cpu, int nr_cpus, struct common_params *common)
{
for (cpu += 1; cpu < nr_cpus; cpu++)

       if (!common->cpus || CPU_ISSET(cpu, &common->monitored_cpus))
           break;

return cpu;
}

and then defining for_each_monitored_cpu() macro like this:

#define for_each_monitored_cpu(cpu, nr_cpus, common) \
for (cpu = 0; cpu < nr_cpus; cpu = next_monitored_cpu(cpu, nr_cpus, common))

Nevertheless, I took the patch already, and it is still better than repeating the same code multiple times, so this is for a future improvement.

[1] - https://en.wikipedia.org/wiki/Dangling_else

Tomas