diff mbox series

libtracecmd: Optimize what cpus to check in tracecmd_iterate_events()

Message ID 20240528145219.7cef53b0@rorschach.local.home (mailing list archive)
State Accepted
Commit 9233f735f7fcaf8e0bb6469c5ef9a540f58948f7
Headers show
Series libtracecmd: Optimize what cpus to check in tracecmd_iterate_events() | expand

Commit Message

Steven Rostedt May 28, 2024, 6:52 p.m. UTC
From: "Steven Rostedt (Google)" <rostedt@goodmis.org>

Currently, tracecmd_iterate_events() will look at all possible CPUs to
find the next event to print, even if the CPUs to look at are filtered.
Instead, create a list of all the CPUs to look at and iterate that, such
that only the CPUs that are needed are looked at instead of all of them,
including those that are filtered out.

Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
---
 lib/trace-cmd/trace-input.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/lib/trace-cmd/trace-input.c b/lib/trace-cmd/trace-input.c
index ce4ecf43..2d12ac33 100644
--- a/lib/trace-cmd/trace-input.c
+++ b/lib/trace-cmd/trace-input.c
@@ -2813,9 +2813,12 @@  int tracecmd_iterate_events(struct tracecmd_input *handle,
 	struct tep_record *record;
 	unsigned long long *timestamps;
 	unsigned long long ts, last_timestamp = 0;
+	int *cpu_list;
+	int cpu_count = 0;
 	int next_cpu;
 	int cpu;
 	int ret = 0;
+	int i;
 
 	if (!callback && !handle->nr_followers) {
 		errno = EINVAL;
@@ -2826,17 +2829,28 @@  int tracecmd_iterate_events(struct tracecmd_input *handle,
 	if (!timestamps)
 		return -1;
 
+	cpu_list = calloc(handle->cpus, sizeof(*cpu_list));
+	if (!cpu_list) {
+		free(timestamps);
+		return -1;
+	}
+
 	for (cpu = 0; cpu < handle->cpus; cpu++) {
 		if (cpus && !CPU_ISSET_S(cpu, cpu_size, cpus))
 			continue;
+		cpu_list[cpu_count++] = cpu;
+	}
 
+	for (i = 0; i < cpu_count; i++) {
+		cpu = cpu_list[i];
 		record = tracecmd_peek_data(handle, cpu);
 		timestamps[cpu] = record ? record->ts : -1ULL;
 	}
 
 	do {
 		next_cpu = -1;
-		for (cpu = 0; cpu < handle->cpus; cpu++) {
+		for (i = 0; i < cpu_count; i++) {
+			cpu = cpu_list[i];
 			ts = timestamps[cpu];
 			if (ts == -1ULL)
 				continue;
@@ -2869,6 +2883,7 @@  int tracecmd_iterate_events(struct tracecmd_input *handle,
 	} while (next_cpu >= 0 && ret == 0);
 
 	free(timestamps);
+	free(cpu_list);
 
 	return ret;
 }