diff mbox series

[v3,3/4] trace-cmd library: Helper logic to update the trace buffer offset

Message ID 20211111150805.86419-4-tz.stoyanov@gmail.com (mailing list archive)
State Accepted
Headers show
Series Refactor the logic for writing trace data in the file | expand

Commit Message

Tzvetomir Stoyanov (VMware) Nov. 11, 2021, 3:08 p.m. UTC
The new helper functions can be used the update the trace buffer
metadata with the real offset within the trace file, where the buffer
data are stored.

Signed-off-by: Tzvetomir Stoyanov (VMware) <tz.stoyanov@gmail.com>
---
 lib/trace-cmd/trace-output.c | 49 ++++++++++++++++++++++++++++++++++++
 1 file changed, 49 insertions(+)

Comments

Steven Rostedt Nov. 24, 2021, 5:22 p.m. UTC | #1
On Thu, 11 Nov 2021 17:08:04 +0200
"Tzvetomir Stoyanov (VMware)" <tz.stoyanov@gmail.com> wrote:
> --- a/lib/trace-cmd/trace-output.c
> +++ b/lib/trace-cmd/trace-output.c
> @@ -1395,6 +1395,20 @@ int tracecmd_write_buffer_info(struct tracecmd_output *handle)
>  	return 0;
>  }
>  
> +static tsize_t get_buffer_file_offset(struct tracecmd_output *handle, const char *name)
> +{


>  
> +static int update_buffer_cpu_offset(struct tracecmd_output *handle,
> +				    const char *name, tsize_t offset)
> +{

Let's not have patches that add static functions that are not used.

They should be added when they are used. I may just merge the patches.

-- Steve
diff mbox series

Patch

diff --git a/lib/trace-cmd/trace-output.c b/lib/trace-cmd/trace-output.c
index 1f144d28..17607b93 100644
--- a/lib/trace-cmd/trace-output.c
+++ b/lib/trace-cmd/trace-output.c
@@ -1395,6 +1395,20 @@  int tracecmd_write_buffer_info(struct tracecmd_output *handle)
 	return 0;
 }
 
+static tsize_t get_buffer_file_offset(struct tracecmd_output *handle, const char *name)
+{
+	struct tracecmd_buffer *buf;
+
+	list_for_each_entry(buf, &handle->buffers, list) {
+		if (!strcmp(name, buf->name)) {
+			if (!buf->option)
+				break;
+			return buf->option->offset;
+		}
+	}
+	return 0;
+}
+
 int tracecmd_write_cmdlines(struct tracecmd_output *handle)
 {
 	int ret;
@@ -1487,6 +1501,41 @@  out:
 	return ret;
 }
 
+static int update_buffer_cpu_offset(struct tracecmd_output *handle,
+				    const char *name, tsize_t offset)
+{
+	tsize_t b_offset;
+	tsize_t current;
+
+	if (!name)
+		name = "";
+
+	b_offset = get_buffer_file_offset(handle, name);
+	if (!b_offset) {
+		tracecmd_warning("Cannot find description for buffer %s\n", name);
+		return -1;
+	}
+
+	current = lseek64(handle->fd, 0, SEEK_CUR);
+
+	/* Go to the option data, where will write the offest */
+	if (lseek64(handle->fd, b_offset, SEEK_SET) == (off64_t)-1) {
+		tracecmd_warning("could not seek to %lld\n", b_offset);
+		return -1;
+	}
+
+	if (do_write_check(handle, &offset, 8))
+		return -1;
+
+	/* Go back to end of file */
+	if (lseek64(handle->fd, current, SEEK_SET) == (off64_t)-1) {
+		tracecmd_warning("could not seek to %lld\n", offset);
+		return -1;
+	}
+	return 0;
+}
+
+
 static char *get_clock(struct tracecmd_output *handle)
 {
 	struct tracefs_instance *inst;