diff mbox series

[v2] libtracefs: Set the number of CPUs in tracefs_local_events_system()

Message ID 20220723102705.420e7d41@rorschach.local.home (mailing list archive)
State Accepted
Commit 9aaa8b0da61d9bc1e9de6c64355333bdc490392a
Headers show
Series [v2] libtracefs: Set the number of CPUs in tracefs_local_events_system() | expand

Commit Message

Steven Rostedt July 23, 2022, 2:27 p.m. UTC
From: "Steven Rostedt (Google)" <rostedt@goodmis.org>

When tracefs_local_events() (which also uses tracefs_local_events_system())
is called, it does not set the CPU count for the tep handler. It should do
so, so make that happen.

Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
---
Changes since v1: https://lore.kernel.org/all/20220723102232.1ba19aa6@rorschach.local.home/
  - Add a comment explaining why I did the counting the way I did.

 src/tracefs-events.c | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)
diff mbox series

Patch

diff --git a/src/tracefs-events.c b/src/tracefs-events.c
index 86395101c96a..b6ab8da3c8ce 100644
--- a/src/tracefs-events.c
+++ b/src/tracefs-events.c
@@ -945,6 +945,40 @@  out:
 	return ret;
 }
 
+static void set_tep_cpus(const char *tracing_dir, struct tep_handle *tep)
+{
+	struct stat st;
+	char path[PATH_MAX];
+	int cpus = sysconf(_SC_NPROCESSORS_CONF);
+	int max_cpu = 0;
+	int ret;
+	int i;
+
+	if (!tracing_dir)
+		tracing_dir = tracefs_tracing_dir();
+
+	/*
+	 * Paranoid: in case sysconf() above does not work.
+	 * And we also only care about the number of tracing
+	 * buffers that exist. If cpus is 32, but the top half
+	 * is offline, there may only be 16 tracing buffers.
+	 * That's what we want to know.
+	 */
+	for (i = 0; !cpus || i < cpus; i++) {
+		snprintf(path, PATH_MAX, "%s/per_cpu/cpu%d", tracing_dir, i);
+		ret = stat(path, &st);
+		if (!ret && S_ISDIR(st.st_mode))
+			max_cpu = i + 1;
+		else if (i >= cpus)
+			break;
+	}
+
+	if (!max_cpu)
+		max_cpu = cpus;
+
+	tep_set_cpus(tep, max_cpu);
+}
+
 /**
  * tracefs_local_events_system - create a tep from the events of the specified subsystem.
  *
@@ -969,6 +1003,8 @@  struct tep_handle *tracefs_local_events_system(const char *tracing_dir,
 		tep = NULL;
 	}
 
+	set_tep_cpus(tracing_dir, tep);
+
 	/* Set the long size for this tep handle */
 	tep_set_long_size(tep, tep_get_header_page_size(tep));