diff mbox series

[RFC] libtracefs: Add tracefs_iterate_stop()

Message ID 20210705122706.146428-1-y.karadz@gmail.com (mailing list archive)
State Accepted
Commit ffcc062c6352d247c9fa4a51d39d270f13061b29
Headers show
Series [RFC] libtracefs: Add tracefs_iterate_stop() | expand

Commit Message

Yordan Karadzhov July 5, 2021, 12:27 p.m. UTC
In the API for data streaming we implemented a special
function (tracefs_trace_pipe_stop()) that can be called from
the user in order to terminate the continuous streaming of the
tracing data. Here we add similar functionality that can be
used to terminate the continuous iteration over the raw events.

Signed-off-by: Yordan Karadzhov (VMware) <y.karadz@gmail.com>
---
 include/tracefs-local.h |  1 +
 include/tracefs.h       |  1 +
 src/tracefs-events.c    | 27 ++++++++++++++++++++++++---
 3 files changed, 26 insertions(+), 3 deletions(-)
diff mbox series

Patch

diff --git a/include/tracefs-local.h b/include/tracefs-local.h
index 73698e8..5a469f9 100644
--- a/include/tracefs-local.h
+++ b/include/tracefs-local.h
@@ -31,6 +31,7 @@  struct tracefs_instance {
 	int				ftrace_marker_fd;
 	int				ftrace_marker_raw_fd;
 	bool				pipe_keep_going;
+	bool				iterate_keep_going;
 };
 
 extern pthread_mutex_t toplevel_lock;
diff --git a/include/tracefs.h b/include/tracefs.h
index 1c8703a..07124ef 100644
--- a/include/tracefs.h
+++ b/include/tracefs.h
@@ -97,6 +97,7 @@  int tracefs_iterate_raw_events(struct tep_handle *tep,
 						struct tep_record *,
 						int, void *),
 				void *callback_context);
+void tracefs_iterate_stop(struct tracefs_instance *instance);
 
 char **tracefs_tracers(const char *tracing_dir);
 
diff --git a/src/tracefs-events.c b/src/tracefs-events.c
index 568bfe6..3093bdd 100644
--- a/src/tracefs-events.c
+++ b/src/tracefs-events.c
@@ -109,7 +109,8 @@  static int read_cpu_pages(struct tep_handle *tep, struct cpu_iterate *cpus, int
 			  int (*callback)(struct tep_event *,
 					  struct tep_record *,
 					  int, void *),
-			  void *callback_context)
+			  void *callback_context,
+			  bool *keep_going)
 {
 	bool has_data = false;
 	int ret;
@@ -121,7 +122,7 @@  static int read_cpu_pages(struct tep_handle *tep, struct cpu_iterate *cpus, int
 			has_data = true;
 	}
 
-	while (has_data) {
+	while (has_data && *(volatile bool *)keep_going) {
 		j = count;
 		for (i = 0; i < count; i++) {
 			if (!cpus[i].event)
@@ -205,6 +206,8 @@  out:
 	return ret;
 }
 
+static bool top_iterate_keep_going;
+
 /*
  * tracefs_iterate_raw_events - Iterate through events in trace_pipe_raw,
  *				per CPU trace buffers
@@ -230,18 +233,24 @@  int tracefs_iterate_raw_events(struct tep_handle *tep,
 						int, void *),
 				void *callback_context)
 {
+	bool *keep_going = instance ? &instance->pipe_keep_going :
+				      &top_iterate_keep_going;
 	struct cpu_iterate *all_cpus = NULL;
 	int count = 0;
 	int ret;
 	int i;
 
+	(*(volatile bool *)keep_going) = true;
+
 	if (!tep || !callback)
 		return -1;
 
 	ret = open_cpu_files(instance, cpus, cpu_size, &all_cpus, &count);
 	if (ret < 0)
 		goto out;
-	ret = read_cpu_pages(tep, all_cpus, count, callback, callback_context);
+	ret = read_cpu_pages(tep, all_cpus, count,
+			     callback, callback_context,
+			     keep_going);
 
 out:
 	if (all_cpus) {
@@ -256,6 +265,18 @@  out:
 	return ret;
 }
 
+/**
+ * tracefs_iterate_stop - stop the iteration over the raw events.
+ * @instance: ftrace instance, can be NULL for top tracing instance.
+ */
+void tracefs_iterate_stop(struct tracefs_instance *instance)
+{
+	if (instance)
+		instance->iterate_keep_going = false;
+	else
+		top_iterate_keep_going = false;
+}
+
 static char **add_list_string(char **list, const char *name, int len)
 {
 	if (!list)