Skip to contentSkip to conversation

Get trace buffer page size from kernel

PATCH Superseded Tzvetomir Stoyanov (VMware) 1 participant 3 messages Jan 19, 2022 Documentation

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v5 cover Get trace buffer page size from kernel

The trace buffer page size is equal to the system memory page size in the current ftrace implementation, but this may change in the future. The newly introduced traceevent library API should be used to get the real trace buffer page size, bases on the information from the "events/header_page" ftrace file.

This patch set depends on:
"[PATCH v7 0/4] trace-cmd documentation update for v7 files"
https://lore.kernel.org/linux-trace-devel/[email protected]/

v5 changes:
- Bug fixes.
- Code cleanups.
- Rebase.
v4 changes
- Rebased on top of the latest master.
v3 changes
- Rebased on top of the latest master.
v2 changes:
- libtraceevent API is renamed, use the new version.
- Rewrote get_trace_page_size() to use libtracefs APIs.
- Cleanups in "trace-cmd dump" output, related to buffer page size print.

Tzvetomir Stoyanov (VMware) (2):
trace-cmd library: Use the real trace buffer page size trace-cmd library: Introduce buffer page size per instance

 .../trace-cmd/trace-cmd.dat.v7.5.txt          |  1 +
 lib/trace-cmd/include/trace-cmd-local.h       |  4 +-
 lib/trace-cmd/trace-input.c                   | 11 ++-
 lib/trace-cmd/trace-output.c                  | 74 +++++++++++++++----
 tracecmd/trace-dump.c                         |  9 ++-
 5 files changed, 79 insertions(+), 20 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH v5 1/2 trace-cmd library: Use the real trace buffer page size

When new output handler is created, it assumes that the trace buffer page size is equal to the system memory page size. This assumption is valid for the current ftrace implementation, but it may change in the future. The newly introduced traceevent library API should be used to get the real trace buffer page size, bases on the information from the "events/header_page" ftrace file.

This commit depends on:
[PATCH] libtraceevent: A new API for trace page size
https://lore.kernel.org/linux-trace-devel/[email protected]/

---
 lib/trace-cmd/trace-output.c | 37 +++++++++++++++++++++++++++++++++++-
 1 file changed, 36 insertions(+), 1 deletion(-)
lib/trace-cmd/trace-output.c +36 -1
index 0323da75..d901c32e 100644
@@ -1279,6 +1279,41 @@ static int write_compression_header(struct tracecmd_output *handle)
 	return 0;
 }
 
+static int get_trace_page_size(struct tracecmd_output *handle)
+{
+	struct tracefs_instance *instance;
+	struct tep_handle *tep = NULL;
+	int psize, size;
+	char *buff = NULL;
+
+	/* In case of an error, return user space page size */
+	psize = getpagesize();
+
+	instance = tracefs_instance_alloc(find_tracing_dir(handle), NULL);
+	if (!instance)
+		goto out;
+
+	buff = tracefs_instance_file_read(instance, "events/header_page", &size);
+	if (!buff)
+		goto out;
+
+	tep = tep_alloc();
+	if (!tep)
+		goto out;
+
+	if (tep_parse_header_page(tep, buff, size, sizeof(long long)))
+		goto out;
+
+	psize = tep_get_sub_buffer_size(tep);
+
+out:
+	tracefs_instance_free(instance);
+	tep_free(tep);
+	free(buff);
+
+	return psize;
+}
+
 /**
  * tracecmd_output_create_fd - allocate new output handle to a trace file
  * @fd: File descriptor for the handle to write to.
@@ -1303,7 +1338,7 @@ struct tracecmd_output *tracecmd_output_create_fd(int fd)
 
 	handle->file_version = FILE_VERSION_DEFAULT;
 
-	handle->page_size = getpagesize();
+	handle->page_size = get_trace_page_size(handle);
 	handle->big_endian = tracecmd_host_bigendian();
 
 	list_head_init(&handle->options);
Signature · 1 line
2.34.1
#2 lore raw

PATCH v5 2/2 trace-cmd library: Introduce buffer page size per instance

In reply to Tzvetomir Stoyanov (VMware)

Currently all ftrace instances have the same page buffer size and trace-cmd logic is implemented according to this assumption. Future changes in the kernel may introduce trace buffer page size per instance. That change will affect the format of the trace file. Add support for per instance trace page buffer size in the trace file format and logic. Only the trace file version 7 is extended with this functionality.

---
 .../trace-cmd/trace-cmd.dat.v7.5.txt          |  1 +
 lib/trace-cmd/include/trace-cmd-local.h       |  4 +-
 lib/trace-cmd/trace-input.c                   | 11 ++++-
 lib/trace-cmd/trace-output.c                  | 43 ++++++++++++-------
 tracecmd/trace-dump.c                         |  9 +++-
 5 files changed, 46 insertions(+), 22 deletions(-)
Documentation/trace-cmd/trace-cmd.dat.v7.5.txt +1 -0
index 1b556bba..e5bcac76 100644
@@ -147,6 +147,7 @@ OPTIONS SECTION
       is saved as name of the top instance.
       <string> a null terminated ASCII string, trace clock used for events timestamps in
       this trace instance.
+      <4 bytes> unsigned integer, size of the trace buffer page.
       <4 bytes> unsigned integer, count of the CPUs with trace data.
       For each CPU of the above count:
          <4 bytes> unsigned integer, ID of the CPU.
lib/trace-cmd/include/trace-cmd-local.h +2 -2
index 48f179d6..5b25719b 100644
@@ -57,7 +57,7 @@ int out_save_options_offset(struct tracecmd_output *handle,
 			    unsigned long long start);
 unsigned long long out_copy_fd_compress(struct tracecmd_output *handle,
 					int fd, unsigned long long max,
-					unsigned long long *write_size);
+					unsigned long long *write_size, int page);
 void in_uncompress_reset(struct tracecmd_input *handle);
 int in_uncompress_block(struct tracecmd_input *handle);
 
@@ -71,7 +71,7 @@ long long do_write_check(struct tracecmd_output *handle, const void *data, long
 struct tracecmd_option *
 out_add_buffer_option(struct tracecmd_output *handle, const char *name,
 		      unsigned short id, unsigned long long data_offset,
-		      int cpus, struct data_file_write *cpu_data);
+		      int cpus, struct data_file_write *cpu_data, int page_size);
 
 struct cpu_data_source {
 	int fd;
lib/trace-cmd/trace-input.c +9 -2
index 5056702a..e8d46bc2 100644
@@ -107,6 +107,7 @@ struct input_buffer_instance {
 	size_t			offset;
 	char			*clock;
 	bool			latency;
+	int			page_size;
 	int			cpus;
 	struct cpu_file_data	*cpu_data;
 };
@@ -3214,6 +3215,9 @@ static int handle_buffer_option(struct tracecmd_input *handle,
 		handle->trace_clock = strdup(buff->clock);
 
 	if (id == TRACECMD_OPTION_BUFFER) {
+		if (save_read_number(handle->pevent, data, &size, &rsize, 4, &tmp))
+			return -1;
+		buff->page_size = tmp;
 		if (save_read_number(handle->pevent, data, &size, &rsize, 4, &tmp))
 			return -1;
 		buff->cpus = tmp;
@@ -5124,6 +5128,7 @@ int tracecmd_copy_options(struct tracecmd_input *in_handle,
 static int copy_trace_latency(struct tracecmd_input *in_handle,
 			      struct tracecmd_output *out_handle, const char *buf_name)
 {
+	int page_size = getpagesize();
 	unsigned long long wsize;
 	unsigned long long offset;
 	int fd;
@@ -5135,7 +5140,8 @@ static int copy_trace_latency(struct tracecmd_input *in_handle,
 	offset = tracecmd_get_out_file_offset(out_handle);
 
 	if (tracecmd_get_out_file_version(out_handle) >= FILE_VERSION_SECTIONS &&
-	    !out_add_buffer_option(out_handle, buf_name, TRACECMD_OPTION_BUFFER_TEXT, offset, 0, NULL))
+	    !out_add_buffer_option(out_handle, buf_name, TRACECMD_OPTION_BUFFER_TEXT,
+				   offset, 0, NULL, page_size))
 		return -1;
 
 	offset = out_write_section_header(out_handle, TRACECMD_OPTION_BUFFER_TEXT,
@@ -5146,7 +5152,7 @@ static int copy_trace_latency(struct tracecmd_input *in_handle,
 	else
 		fd = in_handle->fd;
 
-	if (!out_copy_fd_compress(out_handle, fd, 0, &wsize))
+	if (!out_copy_fd_compress(out_handle, fd, 0, &wsize, page_size))
 		return -1;
 	if (out_update_section_header(out_handle, offset))
 		return -1;
@@ -5462,6 +5468,7 @@ tracecmd_buffer_instance_handle(struct tracecmd_input *handle, int indx)
 			goto error;
 		}
 	} else {
+		new_handle->page_size = handle->buffers[indx].page_size;
 		if (init_buffer_cpu_data(new_handle, buffer) < 0)
 			goto error;
 	}
lib/trace-cmd/trace-output.c +27 -16

147 lines not on this page · raw message

tracecmd/trace-dump.c +7 -2
index 70adb816..f2d8b5e9 100644
@@ -469,6 +469,7 @@ static void dump_option_buffer(int fd, unsigned short option, int size)
 	unsigned short flags;
 	char clock[DUMP_SIZE];
 	char name[DUMP_SIZE];
+	int page_size;
 	int cpus = 0;
 	int id;
 	int i;
@@ -504,6 +505,10 @@ static void dump_option_buffer(int fd, unsigned short option, int size)
 		die("cannot read clock of the buffer option");
 	do_print(OPTIONS|FLYRECORD, "\"%s\" [clock]\n", clock);
 	if (option == TRACECMD_OPTION_BUFFER) {
+		if (read_file_number(fd, &page_size, 4))
+			die("cannot read the page size of the buffer option");
+		do_print(OPTIONS|FLYRECORD, "%d [Page size, bytes]\n", page_size);
+
 		if (read_file_number(fd, &cpus, 4))
 			die("cannot read the cpu count of the buffer option");
 
@@ -519,9 +524,9 @@ static void dump_option_buffer(int fd, unsigned short option, int size)
 			do_print(OPTIONS|FLYRECORD, "   %d %lld\t%lld\t[id, data offset and size]\n",
 				 id, offset, data_size);
 		}
-		do_print(SUMMARY, "\t\[buffer \"%s\", \"%s\" clock, "
+		do_print(SUMMARY, "\t\[buffer \"%s\", \"%s\" clock, %d page size, "
 			 "%d cpus, %lld bytes flyrecord data]\n",
-			 name, clock, cpus, total_size);
+			 name, clock, page_size, cpus, total_size);
 	} else {
 		do_print(SUMMARY, "\t\[buffer \"%s\", \"%s\" clock, latency data]\n", name, clock);
 	}
Signature · 1 line
2.34.1