Skip to contentSkip to conversation

trace-cmd library: Fixes and add a reset

PATCH New Steven Rostedt 1 participant 4 messages Dec 29, 2023 Documentation

lore
Share & tools
lore
#0 lore raw

cover trace-cmd library: Fixes and add a reset

From: "Steven Rostedt (Google)" <[email protected]>

A couple of fixes, plus adding a way to reset the iterator.

Steven Rostedt (Google) (3):
trace-cmd library: Fix tracecmd_iterate_events_multi() CPU clean up trace-cmd library: Add back setting errno to zero in
tracecmd_stack_tracer_status()
trace-cmd library: Add tracecmd_iterate_reset()

 .../libtracecmd/libtracecmd-iterate.txt       | 10 +++++-
 Documentation/libtracecmd/libtracecmd.txt     |  1 +
 include/trace-cmd/trace-cmd.h                 |  2 ++
 lib/trace-cmd/trace-input.c                   | 34 ++++++++++++++++++-
 lib/trace-cmd/trace-util.c                    |  1 +
 5 files changed, 46 insertions(+), 2 deletions(-)
Signature · 1 line
2.42.0
#1 lore raw

PATCH 1/3 trace-cmd library: Fix tracecmd_iterate_events_multi() CPU clean up

From: "Steven Rostedt (Google)" <[email protected]>

In tracecmd_iterate_events_multi(), the clean up code at the end of the function that unlocks and frees the remaining records, iterated the CPUs via a for loop of 0 through all_cpus. But the kbuffer_read_at_offset() function, expects the CPU passed in to be that of the handle and not of all the handles.

That is, if there are two trace.dat files being read, and one has 4 CPUs and the other has 2, the "all_cpus" variable will be 6. When iterating at the clean up, when it gets passed 3 (4 CPUs is represented by 0-3) it will then go to 4 and then 5 for the 2 CPUs trace.dat file. Passing in 4 or 5 to kbuffer_read_at_offset() will fail. It needs to use the local CPU count of the trace.dat file.

Fixes 7f0a59aef63c1 ("trace-cmd library: Have tracecmd_iterate_events() start where it left off")
---
 lib/trace-cmd/trace-input.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/trace-cmd/trace-input.c +1 -1
index 42c8312f8395..bf070f057ed0 100644
@@ -3143,7 +3143,7 @@ int tracecmd_iterate_events_multi(struct tracecmd_input **handles,
 		offset = (int)(records[cpu].record->offset & (handle->page_size - 1));
 		free_next(handle, local_cpu);
 		/* Reset the buffer to read the cached record again */
-		kbuffer_read_at_offset(handle->cpu_data[cpu].kbuf, offset, NULL);
+		kbuffer_read_at_offset(handle->cpu_data[local_cpu].kbuf, offset, NULL);
 	}
 
 	free(records);
Signature · 1 line
2.42.0
#2 lore raw

PATCH 2/3 trace-cmd library: Add back setting errno to zero in tracecmd_stack_tracer_status()

In reply to Steven Rostedt

From: "Steven Rostedt (Google)" <[email protected]>

A clean up was made to remove all the settings of errno to zero in the library, but it was a bit too aggressive in doing so. It removed the errno setting to zero in tracecmd_stack_tracer_status() just before it called strtol() which it uses to test for the success of that function. By not clearing errno, it was causing trace-cmd stat to report:

"Error reading stack tracer status"

Fixes dcd5ee3142 ("libtracecmd: Do not set errno to zero")
---
 lib/trace-cmd/trace-util.c | 1 +
 1 file changed, 1 insertion(+)
lib/trace-cmd/trace-util.c +1 -0
index fc61f9d1111b..b5002f1daa07 100644
@@ -557,6 +557,7 @@ int tracecmd_stack_tracer_status(int *status)
 
 	buf[n] = 0;
 
+	errno = 0;
 	num = strtol(buf, NULL, 10);
 
 	/* Check for various possible errors */
Signature · 1 line
2.42.0
#3 lore raw

PATCH 3/3 trace-cmd library: Add tracecmd_iterate_reset()

In reply to Steven Rostedt

From: "Steven Rostedt (Google)" <[email protected]>

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()

---
 .../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(-)
Documentation/libtracecmd/libtracecmd-iterate.txt +9 -1
index 12c54b2ce1a0..dc053ccbbade 100644
@@ -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]
Documentation/libtracecmd/libtracecmd.txt +1 -0
index bbe6cc78b575..52d4e5994c14 100644
@@ -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_);
include/trace-cmd/trace-cmd.h +2 -0
index e6527b7116c1..55a0c51845b0 100644
@@ -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,
lib/trace-cmd/trace-input.c +32 -0
index bf070f057ed0..88bef83f4fe0 100644
@@ -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
Signature · 1 line
2.42.0