All of lore.kernel.org
 help / color / mirror / Atom feed
From: Steven Rostedt <rostedt@goodmis.org>
To: linux-trace-devel@vger.kernel.org
Cc: "Steven Rostedt (Google)" <rostedt@goodmis.org>
Subject: [PATCH 3/3] trace-cmd library: Add tracecmd_iterate_reset()
Date: Thu, 28 Dec 2023 22:10:15 -0500	[thread overview]
Message-ID: <20231229031138.68313-4-rostedt@goodmis.org> (raw)
In-Reply-To: <20231229031138.68313-1-rostedt@goodmis.org>

From: "Steven Rostedt (Google)" <rostedt@goodmis.org>

Currently there's no easy way to reset iterating over a trace.dat file if a
tracecmd_iterate_events() was called. Add a helper function to do that:

 tracecmd_iterate_reset()

Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
---
 .../libtracecmd/libtracecmd-iterate.txt       | 10 +++++-
 Documentation/libtracecmd/libtracecmd.txt     |  1 +
 include/trace-cmd/trace-cmd.h                 |  2 ++
 lib/trace-cmd/trace-input.c                   | 32 +++++++++++++++++++
 4 files changed, 44 insertions(+), 1 deletion(-)

diff --git a/Documentation/libtracecmd/libtracecmd-iterate.txt b/Documentation/libtracecmd/libtracecmd-iterate.txt
index 12c54b2ce1a0..dc053ccbbade 100644
--- a/Documentation/libtracecmd/libtracecmd-iterate.txt
+++ b/Documentation/libtracecmd/libtracecmd-iterate.txt
@@ -4,7 +4,7 @@ libtracecmd(3)
 NAME
 ----
 tracecmd_iterate_events, tracecmd_iterate_events_multi, tracecmd_follow_event,
-tracecmd_follow_missed_events, tracecmd_filter_add - Read events from a trace file
+tracecmd_follow_missed_events, tracecmd_filter_add, tracecmd_iterate_reset - Read events from a trace file
 
 SYNOPSIS
 --------
@@ -45,6 +45,7 @@ int *tracecmd_follow_missed_events*(struct tracecmd_input pass:[*]_handle_,
 				   void pass:[*]_callback_data_);
 struct tracecmd_filter pass:[*]*tracecmd_filter_add*(struct tracecmd_input *_handle_,
 					    const char pass:[*]_filter_str_, bool _neg_);
+int *tracecmd_iterate_reset*(struct tracecmd_input pass:[*]_handle_);
 --
 
 DESCRIPTION
@@ -140,6 +141,10 @@ is defined by *tep_filter_add_filter_str(3)*. If _neg_ is true, then the events
 that match the filter will be skipped, otherwise the events that match will execute
 the _callback()_ function in the iterators.
 
+The *tracecmd_iterate_reset()* sets the _handle_ back to start at the beginning, so that
+the next call to *tracecmd_iterate_events()* starts back at the first event again, instead
+of continuing where it left off.
+
 RETURN VALUE
 ------------
 Both *tracecmd_iterate_events()*, *tracecmd_iterate_events_reverse()* and
@@ -147,6 +152,9 @@ Both *tracecmd_iterate_events()*, *tracecmd_iterate_events_reverse()* and
 (handling the follow and filters appropriately). Or an error value, which can include
 returning a non-zero result from the _callback()_ function.
 
+*tracecmd_iterate_reset()* returns 0 on success and -1 if an error occurred. Note,
+if -1 is returned, a partial reset may have also happened.
+
 EXAMPLE
 -------
 [source,c]
diff --git a/Documentation/libtracecmd/libtracecmd.txt b/Documentation/libtracecmd/libtracecmd.txt
index bbe6cc78b575..52d4e5994c14 100644
--- a/Documentation/libtracecmd/libtracecmd.txt
+++ b/Documentation/libtracecmd/libtracecmd.txt
@@ -61,6 +61,7 @@ Iterating over events in a trace file:
 					   void pass:[*]_callback_data_);
 	struct tracecmd_filter pass:[*]*tracecmd_filter_add*(struct tracecmd_input *_handle_,
 						    const char pass:[*]_filter_str_, bool _neg_);
+	int *tracecmd_iterate_reset*(struct tracecmd_input pass:[*]_handle_);
 
 Read tracing instances from a trace file:
 	int *tracecmd_buffer_instances*(struct tracecmd_input pass:[*]_handle_);
diff --git a/include/trace-cmd/trace-cmd.h b/include/trace-cmd/trace-cmd.h
index e6527b7116c1..55a0c51845b0 100644
--- a/include/trace-cmd/trace-cmd.h
+++ b/include/trace-cmd/trace-cmd.h
@@ -72,6 +72,8 @@ int tracecmd_follow_missed_events(struct tracecmd_input *handle,
 						  int, void *),
 				  void *callback_data);
 
+int tracecmd_iterate_reset(struct tracecmd_input *handle);
+
 int tracecmd_iterate_events(struct tracecmd_input *handle,
 			    cpu_set_t *cpus, int cpu_size,
 			    int (*callback)(struct tracecmd_input *handle,
diff --git a/lib/trace-cmd/trace-input.c b/lib/trace-cmd/trace-input.c
index bf070f057ed0..88bef83f4fe0 100644
--- a/lib/trace-cmd/trace-input.c
+++ b/lib/trace-cmd/trace-input.c
@@ -2113,6 +2113,38 @@ tracecmd_read_cpu_first(struct tracecmd_input *handle, int cpu)
 	return tracecmd_read_data(handle, cpu);
 }
 
+/**
+ * tracecmd_iterate_reset - Set the handle to iterate from the beginning
+ * @handle: input handle for the trace.dat file
+ *
+ * This causes tracecmd_iterate_events*() to start from the beginning
+ * of the trace.dat file.
+ */
+int tracecmd_iterate_reset(struct tracecmd_input *handle)
+{
+	unsigned long long page_offset;
+	int cpu;
+	int ret = 0;
+	int r;
+
+	for (cpu = 0; cpu < handle->cpus; cpu++) {
+		page_offset = calc_page_offset(handle, handle->cpu_data[cpu].file_offset);
+
+		r = get_page(handle, cpu, page_offset);
+		if (r < 0) {
+			ret = -1;
+			continue; /* ?? */
+		}
+
+		/* If the page was already mapped, we need to reset it */
+		if (r)
+			update_page_info(handle, cpu);
+
+		free_next(handle, cpu);
+	}
+	return ret;
+}
+
 /**
  * tracecmd_read_cpu_last - get the last record in a CPU
  * @handle: input handle for the trace.dat file
-- 
2.42.0


      parent reply	other threads:[~2023-12-29  3:10 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-29  3:10 [PATCH 0/3] trace-cmd library: Fixes and add a reset Steven Rostedt
2023-12-29  3:10 ` [PATCH 1/3] trace-cmd library: Fix tracecmd_iterate_events_multi() CPU clean up Steven Rostedt
2023-12-29  3:10 ` [PATCH 2/3] trace-cmd library: Add back setting errno to zero in tracecmd_stack_tracer_status() Steven Rostedt
2023-12-29  3:10 ` Steven Rostedt [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231229031138.68313-4-rostedt@goodmis.org \
    --to=rostedt@goodmis.org \
    --cc=linux-trace-devel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.