Skip to contentSkip to conversation

Add trace file compression

PATCH In review Tzvetomir Stoyanov (VMware) 2 participants 64 messages Jun 14, 2021 – Jun 22, 2021 Documentation

lore
Share & tools
lore
#0 lore raw

v6 cover Add trace file compression

New implementation, adding a compression to trace file. A basic infrastructure for compression support is added into the trace-cmd library. The zlib is used for compression, but more libraries and algorithms can be added. Trace data is commpressed and part of the trace file metadata:
- ftrace events format
- format of recorded events
- information about the mapping of function addresses to the function names
- trace_printk() format strings
- information of the mapping a PID to a process name
- options
Note: not all trace-cmd commands are tested with these changes. These are verified to work with compressed trace files:
trace-cmd record
trace-cmd report
trace-cmd dump
trace-cmd agent
trace-cmd profile
trace-cmd split
trace-cmd convert

Todo list, in order to have complete trace file compression:
- Add support for more compression libraries.
- Test all trace-cmd commands with trace file v7.

v6 changes:
- Documentation upadate.
- Bug fixes.
- Coding style fixes.
v5 changes:
- Comress the latency trace data.
- Add new "trace-cmd convert" subcommand.
- Fixed bug with empty CPUs in the trace file.
- A lot of minor bug fixes.
v4 changes:
- Tested and fixed profile and split subcommands with compression files.
- Bug fixes.
v3 changes:
- Compress the trace data.
- Added documentation to all compression APIs.
- A few minor fixes.
v2 changes:
- Refactored compression APIs.
- Moved the trace buffers description out of the trace options section.
- Added compression of "options" section of the trace file.
- Updated "trace-cmd list" to show available compression algorithms.
- Tested with host-guest tracing.
- Merged with the patchset that bumps the trace file version.

Tzvetomir Stoyanov (VMware) (45):
trace-cmd library: Remove unused private APIs for creating trace files trace-cmd library: Remove unused API tracecmd_update_option
trace-cmd: Check if file version is supported
trace-cmd library: Add new API to get file version of input handler trace-cmd library: Select the file version when writing trace file
trace-cmd: Add APIs for library initialization and free trace-cmd library: Add support for compression algorithms trace-cmd list: Show supported compression algorithms trace-cmd library: Bump the trace file version to 7 trace-cmd library: Compress part of the trace file trace-cmd library: Read compressed trace file
trace-cmd library: Add new API to get compression of input handler trace-cmd library: Inherit compression algorithm from input file trace-cmd library: Extend the create file APIs to support different
compression
trace-cmd record: Add new parameter --compression trace-cmd dump: Add support for trace files version 7 trace-cmd library: Add support for zlib compression library trace-cmd library: Hide the logic for updating buffer offset
trace-cmd: Move buffers description outside of options trace-cmd library: Track the offset in the option section in the trace
file
trace-cmd library: Add compression of the option section of the trace
file
trace-cmd library: Refactor the logic for writing trace data in the
file
trace-cmd library: Add APIs for read and write compressed data in
chunks
trace-cmd: Compress trace data
trace-cmd: Read compressed trace data
trace-cmd library: Compress latency trace data
trace-cmd: Read compressed latency trace data
trace-cmd library: Reuse within the library the function that checks
file state.
trace-cmd library: Make tracecmd_copy_headers() to work with output
handler
trace-cmd: Do not use trace file compression with streams trace-cmd library: Add new API to get file version of output handler
trace-cmd: Add file state parameter to tracecmd_copy
trace-cmd: Copy CPU count in tracecmd_copy
trace-cmd: Copy buffers description in tracecmd_copy
trace-cmd: Copy options in tracecmd_copy
trace-cmd library: Refactor the logic for writing CPU trace data trace-cmd library: Refactor the logic for writing CPU instance trace
data
trace-cmd: Copy trace data in tracecmd_copy
trace-cmd: Add compression parameter to tracecmd_copy
trace-cmd: Add new command "trace-cmd convert"
trace-cmd record: Update man page
trace-cmd: Add convert man page
trace-cmd: Update bash completion
trace-cmd list: Update the man page
trace-cmd: Update trace.dat man page

 .../trace-cmd/trace-cmd-convert.1.txt         |  60 ++
 Documentation/trace-cmd/trace-cmd-list.1.txt  |   3 +
 .../trace-cmd/trace-cmd-record.1.txt          |  10 +
 Documentation/trace-cmd/trace-cmd.1.txt       |   4 +-
 Documentation/trace-cmd/trace-cmd.dat.5.txt   |  56 +-
 lib/trace-cmd/Makefile                        |  11 +
 .../include/private/trace-cmd-private.h       |  92 +-
 lib/trace-cmd/include/trace-cmd-local.h       |  44 +-
 lib/trace-cmd/trace-compress-zlib.c           | 172 +++
 lib/trace-cmd/trace-compress.c                | 797 ++++++++++++++
 lib/trace-cmd/trace-input.c                   | 999 ++++++++++++++----
 lib/trace-cmd/trace-output.c                  | 877 ++++++++++-----
 lib/trace-cmd/trace-util.c                    |  60 ++
 tracecmd/Makefile                             |   1 +
 tracecmd/include/trace-local.h                |   2 +
 tracecmd/trace-cmd.bash                       |  37 +
 tracecmd/trace-cmd.c                          |  12 +-
 tracecmd/trace-convert.c                      |  94 ++
 tracecmd/trace-dump.c                         | 162 ++-
 tracecmd/trace-list.c                         |  26 +
 tracecmd/trace-listen.c                       |   3 +
 tracecmd/trace-read.c                         |  14 +-
 tracecmd/trace-record.c                       |  48 +-
 tracecmd/trace-restore.c                      |   6 +-
 tracecmd/trace-split.c                        |   2 +-
 tracecmd/trace-stream.c                       |   2 +-
 tracecmd/trace-usage.c                        |  18 +
 27 files changed, 3076 insertions(+), 536 deletions(-)
 create mode 100644 Documentation/trace-cmd/trace-cmd-convert.1.txt
 create mode 100644 lib/trace-cmd/trace-compress-zlib.c
 create mode 100644 lib/trace-cmd/trace-compress.c
 create mode 100644 tracecmd/trace-convert.c
Signature · 1 line
2.31.1
#1 lore raw

PATCH v6 1/45 trace-cmd library: Remove unused private APIs for creating trace files

Remove these unused private trace-cmd library APIs:
tracecmd_create_file() tracecmd_create_file_glob()

---
 .../include/private/trace-cmd-private.h       |  6 ----
 lib/trace-cmd/trace-output.c                  | 29 -------------------
 2 files changed, 35 deletions(-)
lib/trace-cmd/include/private/trace-cmd-private.h +0 -6
index 5c2ab4ca..4bd36baa 100644
@@ -263,12 +263,6 @@ struct tracecmd_option;
 struct tracecmd_msg_handle;
 
 struct tracecmd_output *tracecmd_create_file_latency(const char *output_file, int cpus);
-struct tracecmd_output *tracecmd_create_file(const char *output_file,
-					     int cpus, char * const *cpu_data_files);
-struct tracecmd_output *
-tracecmd_create_file_glob(const char *output_file,
-			  int cpus, char * const *cpu_data_files,
-			  struct tracecmd_event_list *event_globs);
 struct tracecmd_output *
 tracecmd_create_init_file_glob(const char *output_file,
 			       struct tracecmd_event_list *list);
lib/trace-cmd/trace-output.c +0 -29
index a4a1eecc..9231c319 100644
@@ -1614,35 +1614,6 @@ struct tracecmd_output *tracecmd_get_output_handle_fd(int fd)
 	return NULL;
 }
 
-struct tracecmd_output *
-tracecmd_create_file_glob(const char *output_file,
-			  int cpus, char * const *cpu_data_files,
-			  struct tracecmd_event_list *list)
-{
-	struct tracecmd_output *handle;
-
-	handle = create_file(output_file, NULL, NULL, NULL, list);
-	if (!handle)
-		return NULL;
-
-	if (tracecmd_write_cmdlines(handle))
-		return NULL;
-
-	if (tracecmd_append_cpu_data(handle, cpus, cpu_data_files) < 0) {
-		tracecmd_output_close(handle);
-		return NULL;
-	}
-
-	return handle;
-}
-
-struct tracecmd_output *tracecmd_create_file(const char *output_file,
-					     int cpus, char * const *cpu_data_files)
-{
-	return tracecmd_create_file_glob(output_file, cpus,
-					 cpu_data_files, &all_event_list);
-}
-
 struct tracecmd_output *tracecmd_create_init_fd(int fd)
 {
 	return create_file_fd(fd, NULL, NULL, NULL, &all_event_list, NULL);
Signature · 1 line
2.31.1
#2 lore raw

PATCH v6 2/45 trace-cmd library: Remove unused API tracecmd_update_option

In reply to Tzvetomir Stoyanov (VMware)

Removed not used trace-cmd library API for updating the options section of already written trace file:
tracecmd_update_option()

---
 .../include/private/trace-cmd-private.h       |  3 --
 lib/trace-cmd/trace-output.c                  | 40 -------------------
 2 files changed, 43 deletions(-)
lib/trace-cmd/include/private/trace-cmd-private.h +0 -3
index 4bd36baa..49539432 100644
@@ -290,9 +290,6 @@ int tracecmd_write_cpus(struct tracecmd_output *handle, int cpus);
 int tracecmd_write_cmdlines(struct tracecmd_output *handle);
 int tracecmd_write_options(struct tracecmd_output *handle);
 int tracecmd_append_options(struct tracecmd_output *handle);
-int tracecmd_update_option(struct tracecmd_output *handle,
-			   struct tracecmd_option *option, int size,
-			   const void *data);
 void tracecmd_output_close(struct tracecmd_output *handle);
 void tracecmd_output_free(struct tracecmd_output *handle);
 struct tracecmd_output *tracecmd_copy(struct tracecmd_input *ihandle,
lib/trace-cmd/trace-output.c +0 -40
index 9231c319..c0aa58cf 100644
@@ -1229,46 +1229,6 @@ int tracecmd_append_options(struct tracecmd_output *handle)
 	return 0;
 }
 
-int tracecmd_update_option(struct tracecmd_output *handle,
-			   struct tracecmd_option *option, int size,
-			   const void *data)
-{
-	tsize_t offset;
-	stsize_t ret;
-
-	if (size > option->size) {
-		tracecmd_warning("Can't update option with more data than allocated");
-		return -1;
-	}
-
-	if (handle->file_state < TRACECMD_FILE_OPTIONS) {
-		/* Hasn't been written yet. Just update current pointer */
-		option->size = size;
-		memcpy(option->data, data, size);
-		return 0;
-	}
-
-	/* Save current offset */
-	offset = lseek64(handle->fd, 0, SEEK_CUR);
-
-	ret = lseek64(handle->fd, option->offset, SEEK_SET);
-	if (ret == (off64_t)-1) {
-		tracecmd_warning("could not seek to %lld\n", option->offset);
-		return -1;
-	}
-
-	if (do_write_check(handle, data, size))
-		return -1;
-
-	ret = lseek64(handle->fd, offset, SEEK_SET);
-	if (ret == (off64_t)-1) {
-		tracecmd_warning("could not seek to %lld\n", offset);
-		return -1;
-	}
-
-	return 0;
-}
-
 struct tracecmd_option *
 tracecmd_add_buffer_option(struct tracecmd_output *handle, const char *name,
 			   int cpus)
Signature · 1 line
2.31.1
#3 lore raw

PATCH v6 3/45 trace-cmd: Check if file version is supported

In reply to Tzvetomir Stoyanov (VMware)

When reading a trace file, version of the file is ignored. This could case problems when bumping the version number because of changes in in the structure of the file. The old code should detect unsupported file version and should not try to read it.
A new trace-cmd library API is added to check if version is supported:
tracecmd_is_version_supported()
Checks are added in the code to ensure not trying to read trace file from unsupported version.

---
 lib/trace-cmd/include/private/trace-cmd-private.h |  2 ++
 lib/trace-cmd/trace-input.c                       | 10 ++++++++++
 lib/trace-cmd/trace-util.c                        |  7 +++++++
 tracecmd/trace-dump.c                             |  7 +++++++
 4 files changed, 26 insertions(+)
lib/trace-cmd/include/private/trace-cmd-private.h +2 -0
index 49539432..6fc18938 100644
@@ -42,6 +42,8 @@ void tracecmd_record_ref(struct tep_record *record);
 void tracecmd_set_debug(bool set_debug);
 bool tracecmd_get_debug(void);
 
+bool tracecmd_is_version_supported(unsigned int version);
+
 struct tracecmd_output;
 struct tracecmd_recorder;
 struct hook_list;
lib/trace-cmd/trace-input.c +10 -0
index 5ee69b14..97ad0a5d 100644
@@ -131,6 +131,7 @@ struct tracecmd_input {
 	bool			use_trace_clock;
 	bool			read_page;
 	bool			use_pipe;
+	int			file_version;
 	struct cpu_data 	*cpu_data;
 	long long		ts_offset;
 	struct tsc2nsec		tsc_calc;
@@ -3269,6 +3270,7 @@ struct tracecmd_input *tracecmd_alloc_fd(int fd, int flags)
 	unsigned int page_size;
 	char *version;
 	char buf[BUFSIZ];
+	unsigned long ver;
 
 	handle = malloc(sizeof(*handle));
 	if (!handle)
@@ -3293,6 +3295,14 @@ struct tracecmd_input *tracecmd_alloc_fd(int fd, int flags)
 	if (!version)
 		goto failed_read;
 	tracecmd_info("version = %s\n", version);
+	ver = strtol(version, NULL, 10);
+	if (!ver && errno)
+		goto failed_read;
+	if (!tracecmd_is_version_supported(ver)) {
+		tracecmd_warning("Unsupported file version %lu", ver);
+		goto failed_read;
+	}
+	handle->file_version = ver;
 	free(version);
 
 	if (do_read_check(handle, buf, 1))
lib/trace-cmd/trace-util.c +7 -0
index b65f9dec..b0c98c72 100644
@@ -617,3 +617,10 @@ unsigned long long tracecmd_generate_traceid(void)
 	free(str);
 	return hash;
 }
+
+bool tracecmd_is_version_supported(unsigned int version)
+{
+	if (version <= FILE_VERSION)
+		return true;
+	return false;
+}
tracecmd/trace-dump.c +7 -0
index 98425b98..03cc82b4 100644
@@ -10,6 +10,7 @@
 #include <getopt.h>
 #include <sys/stat.h>
 #include <fcntl.h>
+#include <errno.h>
 
 #include "trace-local.h"
 
@@ -145,6 +146,7 @@ static void dump_initial_format(int fd)
 	char magic[] = TRACECMD_MAGIC;
 	char buf[DUMP_SIZE];
 	int val4;
+	unsigned long ver;
 
 	do_print(SUMMARY, "\t[Initial format]\n");
 
@@ -166,6 +168,11 @@ static void dump_initial_format(int fd)
 		die("no version string");
 
 	do_print(SUMMARY, "\t\t%s\t[Version]\n", buf);
+	ver = strtol(buf, NULL, 10);
+	if (!ver && errno)
+		die("Invalid file version string %s", buf);
+	if (!tracecmd_is_version_supported(ver))
+		die("Unsupported file version %lu", ver);
 
 	/* get file endianness*/
 	if (read_file_bytes(fd, buf, 1))
Signature · 1 line
2.31.1
#4 lore raw

Re: [PATCH v6 03/45] trace-cmd: Check if file version is supported

Quoted 8 lines · On Mon, 14 Jun 2021 10:49:47 +0300 "Tzvetomir Stoyanov (VMware)"…

When reading a trace file, version of the file is ignored. This could case problems when bumping the version number because of changes in in the structure of the file. The old code should detect unsupported file version and should not try to read it.
A new trace-cmd library API is added to check if version is supported:
tracecmd_is_version_supported()
Checks are added in the code to ensure not trying to read trace file from unsupported version.

After applying this patch, I get:

$ ./tracecmd/trace-cmd report 
  error reading header for trace.dat

-- Steve

#5 lore raw

Re: [PATCH v6 03/45] trace-cmd: Check if file version is supported

Quoted 4 lines · On Mon, 21 Jun 2021 18:27:46 -0400 Steven Rostedt <[email protected]> wrote:

After applying this patch, I get:

$ ./tracecmd/trace-cmd report 
  error reading header for trace.dat

Never mind, I tested on a leftover trace.dat that had version 7 already set! So the patch did work :-)

-- Steve

#6 lore raw

PATCH v6 4/45 trace-cmd library: Add new API to get file version of input handler

In reply to Tzvetomir Stoyanov (VMware)

Added an API to get the version of the trace file, associated with given input file handler.
tracecmd_get_in_file_version()

---
 lib/trace-cmd/include/private/trace-cmd-private.h | 2 ++
 lib/trace-cmd/trace-input.c                       | 9 +++++++++
 2 files changed, 11 insertions(+)
lib/trace-cmd/include/private/trace-cmd-private.h +2 -0
index 6fc18938..01b12c47 100644
@@ -232,6 +232,8 @@ int tracecmd_set_cursor(struct tracecmd_input *handle,
 unsigned long long
 tracecmd_get_cursor(struct tracecmd_input *handle, int cpu);
 
+unsigned long tracecmd_get_in_file_version(struct tracecmd_input *handle);
+
 int tracecmd_ftrace_overrides(struct tracecmd_input *handle, struct tracecmd_ftrace *finfo);
 bool tracecmd_get_use_trace_clock(struct tracecmd_input *handle);
 tracecmd_show_data_func
lib/trace-cmd/trace-input.c +9 -0
index 97ad0a5d..46619ac7 100644
@@ -4046,6 +4046,15 @@ struct tep_handle *tracecmd_get_tep(struct tracecmd_input *handle)
 	return handle->pevent;
 }
 
+/**
+ * tracecmd_get_in_file_version - return the trace.dat file version
+ * @handle: input handle for the trace.dat file
+ */
+unsigned long tracecmd_get_in_file_version(struct tracecmd_input *handle)
+{
+	return handle->file_version;
+}
+
 /**
  * tracecmd_get_use_trace_clock - return use_trace_clock
  * @handle: input handle for the trace.dat file
Signature · 1 line
2.31.1
#7 lore raw

PATCH v6 5/45 trace-cmd library: Select the file version when writing trace file

In reply to Tzvetomir Stoyanov (VMware)

When a new output handler to trace file is allocated, select the proper file version. If this output handler is based on an existing input trace file handler, inherit the trace file version.

---
 lib/trace-cmd/trace-output.c | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)
lib/trace-cmd/trace-output.c +18 -1
index c0aa58cf..bacda23b 100644
@@ -57,6 +57,7 @@ struct tracecmd_output {
 	int			nr_options;
 	bool			quiet;
 	unsigned long		file_state;
+	unsigned long		file_version;
 	struct list_head	options;
 	struct tracecmd_msg_handle *msg_handle;
 	char			*trace_clock;
@@ -907,6 +908,17 @@ out_free:
 	return ret;
 }
 
+static int select_file_version(struct tracecmd_output *handle,
+				struct tracecmd_input *ihandle)
+{
+	if (ihandle)
+		handle->file_version = tracecmd_get_in_file_version(ihandle);
+	else
+		handle->file_version = FILE_VERSION;
+
+	return 0;
+}
+
 static struct tracecmd_output *
 create_file_fd(int fd, struct tracecmd_input *ihandle,
 	       const char *tracing_dir,
@@ -933,6 +945,9 @@ create_file_fd(int fd, struct tracecmd_input *ihandle,
 
 	handle->msg_handle = msg_handle;
 
+	if (select_file_version(handle, ihandle))
+		goto out_free;
+
 	list_head_init(&handle->options);
 
 	buf[0] = 23;
@@ -943,7 +958,8 @@ create_file_fd(int fd, struct tracecmd_input *ihandle,
 	if (do_write_check(handle, buf, 10))
 		goto out_free;
 
-	if (do_write_check(handle, FILE_VERSION_STRING, strlen(FILE_VERSION_STRING) + 1))
+	sprintf(buf, "%lu", handle->file_version);
+	if (do_write_check(handle, buf, strlen(buf) + 1))
 		goto out_free;
 
 	/* get endian and page size */
@@ -1562,6 +1578,7 @@ struct tracecmd_output *tracecmd_get_output_handle_fd(int fd)
 	handle->pevent = tracecmd_get_tep(ihandle);
 	tep_ref(handle->pevent);
 	handle->page_size = tracecmd_page_size(ihandle);
+	handle->file_version = tracecmd_get_in_file_version(ihandle);
 	list_head_init(&handle->options);
 
 	tracecmd_close(ihandle);
Signature · 1 line
2.31.1
#8 lore raw

PATCH v6 6/45 trace-cmd: Add APIs for library initialization and free

In reply to Tzvetomir Stoyanov (VMware)

The trace-cmd library has no APIs for initialization and free of the whole library. Added these new APIs:
tracecmd_lib_init()
tracecmd_lib_free()
and call them in trace-cmd main function.

---
 lib/trace-cmd/include/private/trace-cmd-private.h |  3 +++
 lib/trace-cmd/trace-util.c                        |  9 +++++++++
 tracecmd/trace-cmd.c                              | 11 ++++++++---
 3 files changed, 20 insertions(+), 3 deletions(-)
lib/trace-cmd/include/private/trace-cmd-private.h +3 -0
index 01b12c47..be8b3c48 100644
@@ -29,6 +29,9 @@
 
 struct tep_plugin_list *trace_load_plugins(struct tep_handle *tep, int flags);
 
+int tracecmd_lib_init(void);
+void tracecmd_lib_free(void);
+
 int *tracecmd_add_id(int *list, int id, int len);
 
 enum {
lib/trace-cmd/trace-util.c +9 -0
index b0c98c72..61054ad2 100644
@@ -624,3 +624,12 @@ bool tracecmd_is_version_supported(unsigned int version)
 		return true;
 	return false;
 }
+
+int tracecmd_lib_init(void)
+{
+	return 0;
+}
+
+void tracecmd_lib_free(void)
+{
+}
tracecmd/trace-cmd.c +8 -3
index 00cdaa37..71c8f6d6 100644
@@ -142,15 +142,20 @@ int main (int argc, char **argv)
 	if (argc < 2)
 		trace_usage(argc, argv);
 
+	tracecmd_lib_init();
+
 	for (i = 0; i < ARRAY_SIZE(commands); ++i) {
 		if (strcmp(argv[1], commands[i].name) == 0 ){
 			commands[i].run(argc, argv);
-			goto out;
+			break;
 		}
 	}
 
+	tracecmd_lib_free();
+
 	/* No valid command found, show help */
-	trace_usage(argc, argv);
-out:
+	if (i == ARRAY_SIZE(commands))
+		trace_usage(argc, argv);
+
 	exit(0);
 }
Signature · 1 line
2.31.1
#9 lore raw

PATCH v6 7/45 trace-cmd library: Add support for compression algorithms

In reply to Tzvetomir Stoyanov (VMware)

Added infrastructure to trace-cmd library for compression. Introduced various new APIs to work with this new functionality:
struct tracecmd_compression
tracecmd_compress_init()
tracecmd_compress_free()
tracecmd_compress_alloc()
tracecmd_compress_destroy()
tracecmd_compress_block()
tracecmd_uncompress_block()
tracecmd_compress_reset()
tracecmd_compress_read()
tracecmd_compress_pread()
tracecmd_compress_write()
tracecmd_compress_lseek() tracecmd_compress_proto_get_name() tracecmd_compress_is_supported() tracecmd_compress_protos_get() tracecmd_compress_proto_register()
The compression algorithms are not part of this patch.

---
 lib/trace-cmd/Makefile                        |   1 +
 .../include/private/trace-cmd-private.h       |  26 +
 lib/trace-cmd/include/trace-cmd-local.h       |   2 +
 lib/trace-cmd/trace-compress.c                | 589 ++++++++++++++++++
 lib/trace-cmd/trace-util.c                    |   3 +
 5 files changed, 621 insertions(+)
 create mode 100644 lib/trace-cmd/trace-compress.c
lib/trace-cmd/Makefile +1 -0
index 17600318..bab4322d 100644
@@ -25,6 +25,7 @@ ifeq ($(VSOCK_DEFINED), 1)
 OBJS += trace-timesync-ptp.o
 OBJS += trace-timesync-kvm.o
 endif
+OBJS += trace-compress.o
 
 # Additional util objects
 OBJS += trace-blk-hack.o
lib/trace-cmd/include/private/trace-cmd-private.h +26 -0
index be8b3c48..ec534d51 100644
@@ -464,6 +464,32 @@ void tracecmd_tsync_free(struct tracecmd_time_sync *tsync);
 int tracecmd_write_guest_time_shift(struct tracecmd_output *handle,
 				    struct tracecmd_time_sync *tsync);
 
+/* --- Compression --- */
+struct tracecmd_compression;
+struct tracecmd_compression *tracecmd_compress_alloc(const char *name, const char *version,
+						     int fd, struct tep_handle *tep,
+						     struct tracecmd_msg_handle *msg_handle);
+void tracecmd_compress_destroy(struct tracecmd_compression *handle);
+int tracecmd_compress_block(struct tracecmd_compression *handle);
+int tracecmd_uncompress_block(struct tracecmd_compression *handle);
+void tracecmd_compress_reset(struct tracecmd_compression *handle);
+int tracecmd_compress_read(struct tracecmd_compression *handle, char *dst, int len);
+int tracecmd_compress_pread(struct tracecmd_compression *handle, char *dst, int len, off_t offset);
+int tracecmd_compress_write(struct tracecmd_compression *handle,
+			    const void *data, unsigned long long size);
+off_t tracecmd_compress_lseek(struct tracecmd_compression *handle, off_t offset, int whence);
+int tracecmd_compress_proto_get_name(struct tracecmd_compression *compress,
+				     const char **name, const char **version);
+bool tracecmd_compress_is_supported(const char *name, const char *version);
+int tracecmd_compress_protos_get(char ***names, char ***versions);
+int tracecmd_compress_proto_register(const char *name, const char *version, int weight,
+				     int (*compress)(char *in, unsigned int in_bytes,
+						     char *out, unsigned int *out_bytes),
+				     int (*uncompress)(char *in, unsigned int in_bytes,
+						       char *out, unsigned int *out_bytes),
+				     unsigned int (*comress_size)(unsigned int bytes),
+				     bool (*is_supported)(const char *name, const char *version));
+
 /* --- Plugin handling --- */
 extern struct tep_plugin_option trace_ftrace_options[];
 
lib/trace-cmd/include/trace-cmd-local.h +2 -0
index 821b5cdb..c6591e56 100644
@@ -31,5 +31,7 @@ void tracecmd_info(const char *fmt, ...);
 #endif
 #endif
 
+void tracecmd_compress_init(void);
+void tracecmd_compress_free(void);
 
 #endif /* _TRACE_CMD_LOCAL_H */
lib/trace-cmd/trace-compress.c new file +589 -0

589 lines not on this page · raw message

lib/trace-cmd/trace-util.c +3 -0
index 61054ad2..136260c6 100644
@@ -627,9 +627,12 @@ bool tracecmd_is_version_supported(unsigned int version)
 
 int tracecmd_lib_init(void)
 {
+
+	tracecmd_compress_init();
 	return 0;
 }
 
 void tracecmd_lib_free(void)
 {
+	tracecmd_compress_free();
 }
Signature · 1 line
2.31.1
#10 lore raw

PATCH v6 8/45 trace-cmd list: Show supported compression algorithms

In reply to Tzvetomir Stoyanov (VMware)

Add new parameter "trace-cmd list -c" to show supported compression algorithms.

---
 tracecmd/trace-list.c  | 26 ++++++++++++++++++++++++++
 tracecmd/trace-usage.c |  1 +
 2 files changed, 27 insertions(+)
tracecmd/trace-list.c +26 -0
index d060c810..900da73b 100644
@@ -549,6 +549,24 @@ static void show_plugins(void)
 	tep_free(pevent);
 }
 
+static void show_compression(void)
+{
+	char **versions, **names;
+	int c, i;
+
+	c = tracecmd_compress_protos_get(&names, &versions);
+	if (c <= 0) {
+		printf("No compression algorithms are supported\n");
+		return;
+	}
+	printf("Supported compression algorithms:\n");
+	for (i = 0; i < c; i++)
+		printf("\t%s, %s\n", names[i], versions[i]);
+
+	free(names);
+	free(versions);
+}
+
 void trace_list(int argc, char **argv)
 {
 	int events = 0;
@@ -562,6 +580,7 @@ void trace_list(int argc, char **argv)
 	int flags = 0;
 	int systems = 0;
 	int show_all = 1;
+	int compression = 0;
 	int i;
 	const char *arg;
 	const char *funcre = NULL;
@@ -626,6 +645,10 @@ void trace_list(int argc, char **argv)
 				systems = 1;
 				show_all = 0;
 				break;
+			case 'c':
+				compression = 1;
+				show_all = 0;
+				break;
 			case '-':
 				if (strcmp(argv[i], "--debug") == 0) {
 					tracecmd_set_debug(true);
@@ -670,6 +693,8 @@ void trace_list(int argc, char **argv)
 		show_clocks();
 	if (systems)
 		show_systems();
+	if (compression)
+		show_compression();
 	if (show_all) {
 		printf("event systems:\n");
 		show_systems();
@@ -679,6 +704,7 @@ void trace_list(int argc, char **argv)
 		show_tracers();
 		printf("\noptions:\n");
 		show_options();
+		show_compression();
 	}
 
 	return;
tracecmd/trace-usage.c +1 -0
index c0033ce8..cd8ca391 100644
@@ -331,6 +331,7 @@ static struct usage_help usage_help[] = {
 		"          -O list plugin options\n"
 		"          -B list defined buffer instances\n"
 		"          -C list the defined clocks (and active one)\n"
+		"          -c list the supported trace file compression algorithms\n"
 	},
 	{
 		"restore",
Signature · 1 line
2.31.1
#11 lore raw

PATCH v6 9/45 trace-cmd library: Bump the trace file version to 7

In reply to Tzvetomir Stoyanov (VMware)

Adding a compression of the trace.dat file will change its structure. These changes are not backward compatible, the old trace-cmd binaries will not be able to read compressed trace files. Bumping the version to 7 will prevent old trace-cmd to read such files.

---
 lib/trace-cmd/include/trace-cmd-local.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/trace-cmd/include/trace-cmd-local.h +1 -1
index c6591e56..4d5d1455 100644
@@ -15,7 +15,7 @@ void tracecmd_critical(const char *fmt, ...);
 void tracecmd_info(const char *fmt, ...);
 
 /* trace.dat file format version */
-#define FILE_VERSION 6
+#define FILE_VERSION 7
 
 #define _STR(x)	#x
 #define STR(x)	_STR(x)
Signature · 1 line
2.31.1
#12 lore raw

PATCH v6 10/45 trace-cmd library: Compress part of the trace file

In reply to Tzvetomir Stoyanov (VMware)

Compress part of the trace.dat file metadata. If there is compression support, chose file version 7 and compress these parts of the file:
- ftrace events format
- format of recorded events
- information of the mapping of function addresses to the function names
- trace_printk() format strings
- information of the mapping a PID to a process name

A new compression header is added in the file, right after the page size information.

---
 lib/trace-cmd/include/trace-cmd-local.h |   4 +
 lib/trace-cmd/trace-output.c            | 145 ++++++++++++++++++++----
 2 files changed, 127 insertions(+), 22 deletions(-)
lib/trace-cmd/include/trace-cmd-local.h +4 -0
index 4d5d1455..93613cd7 100644
@@ -34,4 +34,8 @@ void tracecmd_info(const char *fmt, ...);
 void tracecmd_compress_init(void);
 void tracecmd_compress_free(void);
 
+int out_compression_start(struct tracecmd_output *handle);
+int out_compression_end(struct tracecmd_output *handle);
+void out_compression_reset(struct tracecmd_output *handle);
+
 #endif /* _TRACE_CMD_LOCAL_H */
lib/trace-cmd/trace-output.c +123 -22

263 lines not on this page · raw message

Signature · 1 line
2.31.1
#13 lore raw

PATCH v6 11/45 trace-cmd library: Read compressed trace file

In reply to Tzvetomir Stoyanov (VMware)

If a trace file version 7 is detected, read compressed sections of it. Read the compression header, check if the compression algorithm and version, used to compress the file, are supported and use it to uncompress these sections of the file:
- ftrace events format
- format of recorded events
- information of the mapping of function addresses to the function names
- trace_printk() format strings
- information of the mapping a PID to a process name

---
 lib/trace-cmd/include/trace-cmd-local.h |   3 +
 lib/trace-cmd/trace-input.c             | 257 ++++++++++++++++--------
 2 files changed, 178 insertions(+), 82 deletions(-)
lib/trace-cmd/include/trace-cmd-local.h +3 -0
index 93613cd7..419bfee0 100644
@@ -38,4 +38,7 @@ int out_compression_start(struct tracecmd_output *handle);
 int out_compression_end(struct tracecmd_output *handle);
 void out_compression_reset(struct tracecmd_output *handle);
 
+void in_uncompress_reset(struct tracecmd_input *handle);
+int in_uncompress_block(struct tracecmd_input *handle);
+
 #endif /* _TRACE_CMD_LOCAL_H */
lib/trace-cmd/trace-input.c +175 -82

438 lines not on this page · raw message

Signature · 1 line
2.31.1
#14 lore raw

PATCH v6 12/45 trace-cmd library: Add new API to get compression of input handler

In reply to Tzvetomir Stoyanov (VMware)

The new API returns name and version of the compression algorithm, used to compress the trace file associated with given input file handler:
tracecmd_get_file_compress_proto()

---
 .../include/private/trace-cmd-private.h          |  2 ++
 lib/trace-cmd/trace-input.c                      | 16 ++++++++++++++++
 2 files changed, 18 insertions(+)
lib/trace-cmd/include/private/trace-cmd-private.h +2 -0
index ec534d51..624d0afb 100644
@@ -236,6 +236,8 @@ unsigned long long
 tracecmd_get_cursor(struct tracecmd_input *handle, int cpu);
 
 unsigned long tracecmd_get_in_file_version(struct tracecmd_input *handle);
+int tracecmd_get_file_compress_proto(struct tracecmd_input *handle,
+				     const char **name, const char **version);
 
 int tracecmd_ftrace_overrides(struct tracecmd_input *handle, struct tracecmd_ftrace *finfo);
 bool tracecmd_get_use_trace_clock(struct tracecmd_input *handle);
lib/trace-cmd/trace-input.c +16 -0
index 43d731e0..0b17b58d 100644
@@ -4148,6 +4148,22 @@ unsigned long tracecmd_get_in_file_version(struct tracecmd_input *handle)
 	return handle->file_version;
 }
 
+/**
+ * tracecmd_get_file_compress_proto - get name and version of compression algorithm,
+ *				      used to compress the trace file
+ * @handle: input handle for the trace.dat file
+ * @name: return, name of the compression algorithm.
+ * @version: return, version of the compression algorithm.
+ *
+ * Returns 0 on success, or -1 in case of an error. If 0 is returned, the name and version of the
+ * algorithm are stored in @name and @version. The returned strings must *not* be freed.
+ */
+int tracecmd_get_file_compress_proto(struct tracecmd_input *handle,
+				     const char **name, const char **version)
+{
+	return tracecmd_compress_proto_get_name(handle->compress, name, version);
+}
+
 /**
  * tracecmd_get_use_trace_clock - return use_trace_clock
  * @handle: input handle for the trace.dat file
Signature · 1 line
2.31.1
#15 lore raw

PATCH v6 13/45 trace-cmd library: Inherit compression algorithm from input file

In reply to Tzvetomir Stoyanov (VMware)

When a new trace file output handler is allocated, based on given trace file input handler - use the same compression algorithm.

---
 lib/trace-cmd/trace-output.c | 32 +++++++++++++++++++++++++-------
 1 file changed, 25 insertions(+), 7 deletions(-)
lib/trace-cmd/trace-output.c +25 -7
index 87816b37..82b3e5df 100644
@@ -980,17 +980,27 @@ out_free:
 static int select_file_version(struct tracecmd_output *handle,
 				struct tracecmd_input *ihandle)
 {
+	const char *cname = NULL;
+	const char *cver = NULL;
+
 	if (ihandle) {
 		handle->file_version = tracecmd_get_in_file_version(ihandle);
-	} else {
-		handle->compress = tracecmd_compress_alloc(NULL, NULL, handle->fd,
-							   handle->pevent, handle->msg_handle);
-		if (handle->compress)
-			handle->file_version = 7;
-		else
-			handle->file_version = 6;
+		if (!tracecmd_get_file_compress_proto(ihandle, &cname, &cver)) {
+			handle->compress = tracecmd_compress_alloc(cname, cver, handle->fd,
+								   handle->pevent, handle->msg_handle);
+			if (!handle->compress)
+				return -1;
+		}
+		return 0;
 	}
 
+	handle->compress = tracecmd_compress_alloc(NULL, NULL, handle->fd,
+						   handle->pevent, handle->msg_handle);
+	if (handle->compress)
+		handle->file_version = 7;
+	else
+		handle->file_version = 6;
+
 	return 0;
 }
 
@@ -1645,6 +1655,8 @@ struct tracecmd_output *tracecmd_get_output_handle_fd(int fd)
 {
 	struct tracecmd_output *handle = NULL;
 	struct tracecmd_input *ihandle;
+	const char *cname = NULL;
+	const char *cver = NULL;
 	int fd2;
 
 	/* Move the file descriptor to the beginning */
@@ -1682,6 +1694,12 @@ struct tracecmd_output *tracecmd_get_output_handle_fd(int fd)
 	handle->file_version = tracecmd_get_in_file_version(ihandle);
 	list_head_init(&handle->options);
 
+	if (!tracecmd_get_file_compress_proto(ihandle, &cname, &cver)) {
+		handle->compress = tracecmd_compress_alloc(cname, cver, handle->fd,
+							   handle->pevent, handle->msg_handle);
+		if (!handle->compress)
+			goto out_free;
+	}
 	tracecmd_close(ihandle);
 
 	return handle;
Signature · 1 line
2.31.1
#16 lore raw

PATCH v6 14/45 trace-cmd library: Extend the create file APIs to support different compression

In reply to Tzvetomir Stoyanov (VMware)

Added additional parameter for file compression to all trace-cmd library APIs for creating a new trace file. The caller could specify what compression algorithm should be used when creating a new trace file:
tracecmd_create_file_latency
tracecmd_create_init_file_glob
tracecmd_create_init_fd_glob
tracecmd_create_init_fd_msg
tracecmd_create_init_file tracecmd_create_init_file_override
tracecmd_create_init_fd

The new parameter is of type string and can be one of:
"any" - auto select the best available compression algorithm "none" - do not compress the trace file
name - the name of the desired compression algorithms

If a compression is used, trace file version 7 is selected, otherwise trace file version 6 is used.

---
 .../include/private/trace-cmd-private.h       | 19 +++--
 lib/trace-cmd/trace-output.c                  | 79 ++++++++++++-------
 tracecmd/trace-record.c                       | 12 ++-
 tracecmd/trace-restore.c                      |  4 +-
 tracecmd/trace-stream.c                       |  2 +-
 5 files changed, 70 insertions(+), 46 deletions(-)
lib/trace-cmd/include/private/trace-cmd-private.h +11 -8
index 624d0afb..ee73325c 100644
@@ -271,20 +271,23 @@ struct tracecmd_event_list {
 struct tracecmd_option;
 struct tracecmd_msg_handle;
 
-struct tracecmd_output *tracecmd_create_file_latency(const char *output_file, int cpus);
+struct tracecmd_output *tracecmd_create_file_latency(const char *output_file, int cpus,
+						     const char *compression);
 struct tracecmd_output *
-tracecmd_create_init_file_glob(const char *output_file,
-			       struct tracecmd_event_list *list);
-struct tracecmd_output *tracecmd_create_init_fd(int fd);
+tracecmd_create_init_file_glob(const char *output_file, struct tracecmd_event_list *list,
+			       const char *compression);
+struct tracecmd_output *tracecmd_create_init_fd(int fd, const char *compression);
 struct tracecmd_output *
-tracecmd_create_init_fd_glob(int fd, struct tracecmd_event_list *list);
+tracecmd_create_init_fd_glob(int fd, struct tracecmd_event_list *list, const char *compression);
 struct tracecmd_output *
 tracecmd_create_init_fd_msg(struct tracecmd_msg_handle *msg_handle,
-			    struct tracecmd_event_list *list);
-struct tracecmd_output *tracecmd_create_init_file(const char *output_file);
+			    struct tracecmd_event_list *list,
+			    const char *compression);
+struct tracecmd_output *tracecmd_create_init_file(const char *output_file, const char *compression);
 struct tracecmd_output *tracecmd_create_init_file_override(const char *output_file,
 							   const char *tracing_dir,
-							   const char *kallsyms);
+							   const char *kallsyms,
+							   const char *compression);
 struct tracecmd_option *tracecmd_add_option(struct tracecmd_output *handle,
 					    unsigned short id, int size,
 					    const void *data);
lib/trace-cmd/trace-output.c +51 -28

169 lines not on this page · raw message

tracecmd/trace-record.c +5 -7
index 60ee5fb3..29f84b7c 100644
@@ -3700,7 +3700,7 @@ setup_connection(struct buffer_instance *instance, struct common_record_context
 
 	/* Now create the handle through this socket */
 	if (msg_handle->version == V3_PROTOCOL) {
-		network_handle = tracecmd_create_init_fd_msg(msg_handle, listed_events);
+		network_handle = tracecmd_create_init_fd_msg(msg_handle, listed_events, NULL);
 		if (!network_handle)
 			goto error;
 		tracecmd_set_quiet(network_handle, quiet);
@@ -3718,8 +3718,7 @@ setup_connection(struct buffer_instance *instance, struct common_record_context
 		if (ret)
 			goto error;
 	} else {
-		network_handle = tracecmd_create_init_fd_glob(msg_handle->fd,
-							      listed_events);
+		network_handle = tracecmd_create_init_fd_glob(msg_handle->fd, listed_events, NULL);
 		if (!network_handle)
 			goto error;
 		tracecmd_set_quiet(network_handle, quiet);
@@ -4067,8 +4066,7 @@ static void setup_agent(struct buffer_instance *instance,
 {
 	struct tracecmd_output *network_handle;
 
-	network_handle = tracecmd_create_init_fd_msg(instance->msg_handle,
-						     listed_events);
+	network_handle = tracecmd_create_init_fd_msg(instance->msg_handle, listed_events, NULL);
 	add_options(network_handle, ctx);
 	tracecmd_write_cmdlines(network_handle);
 	tracecmd_write_cpus(network_handle, instance->cpu_count);
@@ -4460,7 +4458,7 @@ static void record_data(struct common_record_context *ctx)
 		return;
 
 	if (latency) {
-		handle = tracecmd_create_file_latency(ctx->output, local_cpu_count);
+		handle = tracecmd_create_file_latency(ctx->output, local_cpu_count, NULL);
 		tracecmd_set_quiet(handle, quiet);
 	} else {
 		if (!local_cpu_count)
@@ -4491,7 +4489,7 @@ static void record_data(struct common_record_context *ctx)
 				touch_file(temp_files[i]);
 		}
 
-		handle = tracecmd_create_init_file_glob(ctx->output, listed_events);
+		handle = tracecmd_create_init_file_glob(ctx->output, listed_events, NULL);
 		if (!handle)
 			die("Error creating output file");
 		tracecmd_set_quiet(handle, quiet);
tracecmd/trace-restore.c +2 -2
index 280a37f0..96b4fb5d 100644
@@ -91,7 +91,7 @@ void trace_restore (int argc, char **argv)
 		}
 
 		handle = tracecmd_create_init_file_override(output, tracing_dir,
-							    kallsyms);
+							    kallsyms, NULL);
 		if (!handle)
 			die("Unabled to create output file %s", output);
 		if (tracecmd_write_cmdlines(handle) < 0)
@@ -128,7 +128,7 @@ void trace_restore (int argc, char **argv)
 		handle = tracecmd_copy(ihandle, output);
 		tracecmd_close(ihandle);
 	} else
-		handle = tracecmd_create_init_file(output);
+		handle = tracecmd_create_init_file(output, NULL);
 
 	if (!handle)
 		die("error writing to %s", output);
tracecmd/trace-stream.c +1 -1
index b47b208c..317de1b2 100644
@@ -43,7 +43,7 @@ trace_stream_init(struct buffer_instance *instance, int cpu, int fd, int cpus,
 		tfd = fileno(fp);
 
 		ofd = dup(tfd);
-		trace_output = tracecmd_create_init_fd(ofd);
+		trace_output = tracecmd_create_init_fd(ofd, NULL);
 		if (!trace_output) {
 			fclose(fp);
 			return NULL;
Signature · 1 line
2.31.1
#17 lore raw

PATCH v6 15/45 trace-cmd record: Add new parameter --compression

In reply to Tzvetomir Stoyanov (VMware)

Added a new parameter "trace-cmd record --compression", can be used to select the desired compression algorithm for the trace output file. One of these strings can be passed:
"any" - auto select the best available compression algorithm "none" - do not compress the trace file
name - the name of the desired compression algorithms, available
algorithms can be listed with "trace-cmd list -c"

If a compression is used, trace file version 7 is selected, otherwise trace file version 6 is used.

---
 tracecmd/trace-record.c | 30 +++++++++++++++++++++++++-----
 tracecmd/trace-usage.c  |  5 +++++
 2 files changed, 30 insertions(+), 5 deletions(-)
tracecmd/trace-record.c +25 -5
index 29f84b7c..d3362e5b 100644
@@ -199,6 +199,7 @@ struct common_record_context {
 	char *date2ts;
 	char *user;
 	const char *clock;
+	const char *compression;
 	struct tsc_nsec tsc2nsec;
 	int data_flags;
 	int tsync_loop_interval;
@@ -3700,7 +3701,8 @@ setup_connection(struct buffer_instance *instance, struct common_record_context
 
 	/* Now create the handle through this socket */
 	if (msg_handle->version == V3_PROTOCOL) {
-		network_handle = tracecmd_create_init_fd_msg(msg_handle, listed_events, NULL);
+		network_handle = tracecmd_create_init_fd_msg(msg_handle,
+							     listed_events, ctx->compression);
 		if (!network_handle)
 			goto error;
 		tracecmd_set_quiet(network_handle, quiet);
@@ -3718,7 +3720,8 @@ setup_connection(struct buffer_instance *instance, struct common_record_context
 		if (ret)
 			goto error;
 	} else {
-		network_handle = tracecmd_create_init_fd_glob(msg_handle->fd, listed_events, NULL);
+		network_handle = tracecmd_create_init_fd_glob(msg_handle->fd,
+							      listed_events, ctx->compression);
 		if (!network_handle)
 			goto error;
 		tracecmd_set_quiet(network_handle, quiet);
@@ -4066,7 +4069,8 @@ static void setup_agent(struct buffer_instance *instance,
 {
 	struct tracecmd_output *network_handle;
 
-	network_handle = tracecmd_create_init_fd_msg(instance->msg_handle, listed_events, NULL);
+	network_handle = tracecmd_create_init_fd_msg(instance->msg_handle,
+						     listed_events, ctx->compression);
 	add_options(network_handle, ctx);
 	tracecmd_write_cmdlines(network_handle);
 	tracecmd_write_cpus(network_handle, instance->cpu_count);
@@ -4458,7 +4462,8 @@ static void record_data(struct common_record_context *ctx)
 		return;
 
 	if (latency) {
-		handle = tracecmd_create_file_latency(ctx->output, local_cpu_count, NULL);
+		handle = tracecmd_create_file_latency(ctx->output,
+						      local_cpu_count, ctx->compression);
 		tracecmd_set_quiet(handle, quiet);
 	} else {
 		if (!local_cpu_count)
@@ -4489,7 +4494,8 @@ static void record_data(struct common_record_context *ctx)
 				touch_file(temp_files[i]);
 		}
 
-		handle = tracecmd_create_init_file_glob(ctx->output, listed_events, NULL);
+		handle = tracecmd_create_init_file_glob(ctx->output,
+							listed_events, ctx->compression);
 		if (!handle)
 			die("Error creating output file");
 		tracecmd_set_quiet(handle, quiet);
@@ -5736,6 +5742,7 @@ void init_top_instance(void)
 }
 
 enum {
+	OPT_comporession	= 239,
 	OPT_tsc2nsec		= 240,
 	OPT_fork		= 241,
 	OPT_tsyncinterval	= 242,
@@ -6172,6 +6179,7 @@ static void parse_record_options(int argc,
 			{"fork", no_argument, NULL, OPT_fork},
 			{"tsc2nsec", no_argument, NULL, OPT_tsc2nsec},
 			{"poll", no_argument, NULL, OPT_poll},
+			{"compression", required_argument, NULL, OPT_comporession},
 			{NULL, 0, NULL, 0}
 		};
 
@@ -6597,6 +6605,18 @@ static void parse_record_options(int argc,
 			cmd_check_die(ctx, CMD_set, *(argv+1), "--poll");
 			recorder_flags |= TRACECMD_RECORD_POLL;
 			break;
+		case OPT_comporession:
+			cmd_check_die(ctx, CMD_start, *(argv+1), "--compression");
+			cmd_check_die(ctx, CMD_set, *(argv+1), "--compression");
+			cmd_check_die(ctx, CMD_extract, *(argv+1), "--compression");
+			cmd_check_die(ctx, CMD_stream, *(argv+1), "--compression");
+			cmd_check_die(ctx, CMD_profile, *(argv+1), "--compression");
+			cmd_check_die(ctx, CMD_record_agent, *(argv+1), "--compression");
+			if (strcmp(optarg, "any") && strcmp(optarg, "none") &&
+			    !tracecmd_compress_is_supported(optarg, NULL))
+				die("Compression algorithm  %s is not supported", optarg);
+			ctx->compression = strdup(optarg);
+			break;
 		case OPT_quiet:
 		case 'q':
 			quiet = true;
tracecmd/trace-usage.c +5 -0
index cd8ca391..c70f9919 100644
@@ -69,6 +69,11 @@ static struct usage_help usage_help[] = {
 		"               If 0 is specified, no loop is performed - timestamps offset is calculated only twice,"
 		"                                                         at the beginnig and at the end of the trace\n"
 		"          --poll don't block while reading from the trace buffer\n"
+		"          --compression compress the trace output file, one of these strings can be passed:\n"
+		"                            any  - auto select the best available compression algorithm\n"
+		"                            none - do not compress the trace file\n"
+		"                            name - the name of the desired compression algorithms\n"
+		"                        available algorithms can be listed with trace-cmd list -c\n"
 	},
 	{
 		"set",
Signature · 1 line
2.31.1
#18 lore raw

PATCH v6 16/45 trace-cmd dump: Add support for trace files version 7

In reply to Tzvetomir Stoyanov (VMware)

Added support to read and dump information of trace files version 7.

---
 tracecmd/trace-dump.c | 125 +++++++++++++++++++++++++++++++++++-------
 1 file changed, 105 insertions(+), 20 deletions(-)
tracecmd/trace-dump.c +105 -20
index 03cc82b4..ed82104b 100644
@@ -26,6 +26,9 @@
 static struct tep_handle *tep;
 static unsigned int trace_cpus;
 static int has_clock;
+static unsigned long file_version;
+static bool	read_compress;
+static struct tracecmd_compression *compress;
 
 enum dump_items {
 	SUMMARY		= (1 << 0),
@@ -52,46 +55,62 @@ enum dump_items verbosity;
 			tracecmd_plog(fmt, ##__VA_ARGS__);	\
 	} while (0)
 
-static int read_file_string(int fd, char *dst, int len)
+static int read_fd(int fd, char *dst, int len)
 {
 	size_t size = 0;
 	int r;
 
 	do {
-		r = read(fd, dst+size, 1);
+		r = read(fd, dst+size, len);
 		if (r > 0) {
-			size++;
-			len--;
+			size += r;
+			len -= r;
 		} else
 			break;
-		if (!dst[size - 1])
-			break;
-	} while (r > 0 && len);
+	} while (r > 0);
 
-	if (!size || dst[size - 1])
+	if (len)
 		return -1;
-	return 0;
+	return size;
 }
 
-static int read_file_bytes(int fd, char *dst, int len)
+static int read_compressed(int fd, char *dst, int len)
+{
+
+	if (read_compress)
+		return tracecmd_compress_read(compress, dst, len);
+	return read_fd(fd, dst, len);
+}
+
+static int read_file_string(int fd, char *dst, int len)
 {
 	size_t size = 0;
 	int r;
 
 	do {
-		r = read(fd, dst+size, len);
+		r = read_compressed(fd, dst+size, 1);
 		if (r > 0) {
-			size += r;
-			len -= r;
+			size++;
+			len--;
 		} else
 			break;
-	} while (r > 0);
+		if (!dst[size - 1])
+			break;
+	} while (r > 0 && len);
 
-	if (len)
+	if (!size || dst[size - 1])
 		return -1;
 	return 0;
 }
 
+static int read_file_bytes(int fd, char *dst, int len)
+{
+	int ret;
+
+	ret = read_compressed(fd, dst, len);
+	return ret < 0 ? ret : 0;
+}
+
 static void read_dump_string(int fd, int size, enum dump_items id)
 {
 	char buf[DUMP_SIZE];
@@ -146,7 +165,6 @@ static void dump_initial_format(int fd)
 	char magic[] = TRACECMD_MAGIC;
 	char buf[DUMP_SIZE];
 	int val4;
-	unsigned long ver;
 
 	do_print(SUMMARY, "\t[Initial format]\n");
 
@@ -168,11 +186,11 @@ static void dump_initial_format(int fd)
 		die("no version string");
 
 	do_print(SUMMARY, "\t\t%s\t[Version]\n", buf);
-	ver = strtol(buf, NULL, 10);
-	if (!ver && errno)
+	file_version = strtol(buf, NULL, 10);
+	if (!file_version && errno)
 		die("Invalid file version string %s", buf);
-	if (!tracecmd_is_version_supported(ver))
-		die("Unsupported file version %lu", ver);
+	if (!tracecmd_is_version_supported(file_version))
+		die("Unsupported file version %lu", file_version);
 
 	/* get file endianness*/
 	if (read_file_bytes(fd, buf, 1))
@@ -192,6 +210,29 @@ static void dump_initial_format(int fd)
 	do_print(SUMMARY, "\t\t%d\t[Page size, bytes]\n", val4);
 }
 
+static void dump_compress(int fd)
+{
+	char compr[DUMP_SIZE];
+	char *ver = NULL;
+
+	if (file_version < 7)
+		return;
+
+	/* get compression header */
+	if (read_file_string(fd, compr, DUMP_SIZE))
+		die("no compression header");
+	ver = strchr(compr, ' ');
+	if (!ver)
+		die("no compression version");
+	*ver = '\0';
+	do_print((SUMMARY), "\t\t%s\t[Compression algorithm]\n", compr);
+	do_print((SUMMARY), "\t\t%s\t[Compression version]\n", ver + 1);
+
+	compress = tracecmd_compress_alloc(compr, ver + 1, fd, tep, NULL);
+	if (!compress)
+		die("cannot uncomress the file");
+}
+
 static void dump_header_page(int fd)
 {
 	unsigned long long size;
@@ -234,11 +275,34 @@ static void dump_header_event(int fd)
 	read_dump_string(fd, size, HEAD_EVENT);
 }
 
+static void uncompress_reset(void)
+{
+	if (compress && file_version >= 7) {
+		read_compress = false;
+		tracecmd_compress_reset(compress);
+	}
+}
+
+static int uncompress_block(void)
+{
+	int ret = 0;
+
+	if (compress && file_version >= 7) {
+		ret = tracecmd_uncompress_block(compress);
+		if (!ret)
+			read_compress = true;
+
+	}
+	return ret;
+}
+
 static void dump_ftrace_events_format(int fd)
 {
 	unsigned long long size;
 	unsigned int count;
 
+	if (uncompress_block())
+		die("cannot uncompress file block");
 	do_print((SUMMARY | FTRACE_FORMAT), "\t[Ftrace format, ");
 	if (read_file_number(fd, &count, 4))
 		die("cannot read the count of the ftrace events");
@@ -251,6 +315,7 @@ static void dump_ftrace_events_format(int fd)
 		read_dump_string(fd, size, FTRACE_FORMAT);
 		count--;
 	}
+	uncompress_reset();
 }
 
 static void dump_events_format(int fd)
@@ -262,6 +327,9 @@ static void dump_events_format(int fd)
 
 	do_print((SUMMARY | EVENT_FORMAT | EVENT_SYSTEMS), "\t[Events format, ");
 
+	if (uncompress_block())
+		die("cannot uncompress file block");
+
 	if (read_file_number(fd, &systems, 4))
 		die("cannot read the count of the event systems");
 
@@ -284,6 +352,7 @@ static void dump_events_format(int fd)
 		}
 		systems--;
 	}
+	uncompress_reset();
 }
 
 static void dump_kallsyms(int fd)
@@ -292,12 +361,17 @@ static void dump_kallsyms(int fd)
 
 	do_print((SUMMARY | KALLSYMS), "\t[Kallsyms, ");
 
+	if (uncompress_block())
+		die("cannot uncompress file block");
+
 	if (read_file_number(fd, &size, 4))
 		die("cannot read the size of the kallsyms");
 
 	do_print((SUMMARY | KALLSYMS), "%d bytes]\n", size);
 
 	read_dump_string(fd, size, KALLSYMS);
+
+	uncompress_reset();
 }
 
 static void dump_printk(int fd)
@@ -306,12 +380,17 @@ static void dump_printk(int fd)
 
 	do_print((SUMMARY | TRACE_PRINTK), "\t[Trace printk, ");
 
+	if (uncompress_block())
+		die("cannot uncompress file block");
+
 	if (read_file_number(fd, &size, 4))
 		die("cannot read the size of the trace printk");
 
 	do_print((SUMMARY | TRACE_PRINTK), "%d bytes]\n", size);
 
 	read_dump_string(fd, size, TRACE_PRINTK);
+
+	uncompress_reset();
 }
 
 static void dump_cmdlines(int fd)
@@ -320,12 +399,17 @@ static void dump_cmdlines(int fd)
 
 	do_print((SUMMARY | CMDLINES), "\t[Saved command lines, ");
 
+	if (uncompress_block())
+		die("cannot uncompress file block");
+
 	if (read_file_number(fd, &size, 8))
 		die("cannot read the size of the saved command lines");
 
 	do_print((SUMMARY | CMDLINES), "%d bytes]\n", size);
 
 	read_dump_string(fd, size, CMDLINES);
+
+	uncompress_reset();
 }
 
 static void dump_cpus_count(int fd)
@@ -669,6 +753,7 @@ static void dump_file(const char *file)
 	do_print(SUMMARY, "\n Tracing meta data in file %s:\n", file);
 
 	dump_initial_format(fd);
+	dump_compress(fd);
 	dump_header_page(fd);
 	dump_header_event(fd);
 	dump_ftrace_events_format(fd);
Signature · 1 line
2.31.1
#19 lore raw

PATCH v6 17/45 trace-cmd library: Add support for zlib compression library

In reply to Tzvetomir Stoyanov (VMware)

If libz is available, use that library to provide trace file compression support. The library is detected runtime.

---
 lib/trace-cmd/Makefile                  |  10 ++
 lib/trace-cmd/include/trace-cmd-local.h |   5 +
 lib/trace-cmd/trace-compress-zlib.c     | 172 ++++++++++++++++++++++++
 lib/trace-cmd/trace-compress.c          |   8 ++
 4 files changed, 195 insertions(+)
 create mode 100644 lib/trace-cmd/trace-compress-zlib.c
lib/trace-cmd/Makefile +10 -0
index bab4322d..83ba7016 100644
@@ -7,6 +7,13 @@ ldir:=$(src)/lib/trace-cmd
 
 DEFAULT_TARGET = $(LIBTRACECMD_STATIC)
 
+pound := \#
+ZLIB_INSTALLED := $(shell if (printf "$(pound)include <zlib.h>\n void main(){deflateInit(NULL, Z_BEST_COMPRESSION);}" | $(CC) -o /dev/null -x c - -lz >/dev/null 2>&1) ; then echo 1; else echo 0 ; fi)
+ifeq ($(ZLIB_INSTALLED), 1)
+CFLAGS += -DHAVE_ZLIB
+$(info    Have zlib compression support)
+endif
+
 OBJS =
 OBJS += trace-hash.o
 OBJS += trace-hooks.o
@@ -26,6 +33,9 @@ OBJS += trace-timesync-ptp.o
 OBJS += trace-timesync-kvm.o
 endif
 OBJS += trace-compress.o
+ifeq ($(ZLIB_INSTALLED), 1)
+OBJS += trace-compress-zlib.o
+endif
 
 # Additional util objects
 OBJS += trace-blk-hack.o
lib/trace-cmd/include/trace-cmd-local.h +5 -0
index 419bfee0..40a6a40d 100644
@@ -31,6 +31,11 @@ void tracecmd_info(const char *fmt, ...);
 #endif
 #endif
 
+#ifdef HAVE_ZLIB
+int tracecmd_zlib_init(void);
+void tracecmd_zlib_free(void);
+#endif
+
 void tracecmd_compress_init(void);
 void tracecmd_compress_free(void);
 
lib/trace-cmd/trace-compress-zlib.c new file +172 -0

172 lines not on this page · raw message

lib/trace-cmd/trace-compress.c +8 -0
index 378a3e57..039f60c5 100644
@@ -355,6 +355,10 @@ void tracecmd_compress_init(void)
 
 	gettimeofday(&time, NULL);
 	srand((time.tv_sec * 1000) + (time.tv_usec / 1000));
+
+#ifdef HAVE_ZLIB
+	tracecmd_zlib_init();
+#endif
 }
 
 static struct compress_proto *compress_proto_select(void)
@@ -531,6 +535,10 @@ void tracecmd_compress_free(void)
 	struct compress_proto *proto = proto_list;
 	struct compress_proto *del;
 
+#ifdef HAVE_ZLIB
+	tracecmd_zlib_free();
+#endif
+
 	while (proto) {
 		del = proto;
 		proto = proto->next;
Signature · 1 line
2.31.1
#20 lore raw

Re: [PATCH v6 17/45] trace-cmd library: Add support for zlib compression library

Quoted 2 lines · On Mon, 14 Jun 2021 10:50:01 +0300 "Tzvetomir Stoyanov (VMware)"…

If libz is available, use that library to provide trace file compression support. The library is detected runtime.

Why have the library detected at runtime?

If it is detected, we can then have the library flags include -lz.

Why use dlopen to load zlib? And not just include it?

This seems rather fragile to try to get right.

-- Steve

Quoted 3 lines

---

#21 lore raw

Re: [PATCH v6 17/45] trace-cmd library: Add support for zlib compression library

Quoted 7 lines · Steven Rostedt
Quoted 2 lines · On Mon, 14 Jun 2021 10:50:01 +0300 "Tzvetomir Stoyanov (VMware)"…

If libz is available, use that library to provide trace file compression support. The library is detected runtime.

Quoted 7 lines

Why have the library detected at runtime?

If it is detected, we can then have the library flags include -lz.

Why use dlopen to load zlib? And not just include it?

This seems rather fragile to try to get right.

The idea of this design is not to bring additional mandatory dependencies. I do not know if libz is available by default, but even if we assume it is - each additional compression library that is added will be a mandatory dependency. The compression is not a mandatory functionality, trace-cmd can work without it. Why do you think it is fragile, I think dlopen() uses the same core linker logic to find and load the library. The only difference I see is that using "-lz" leads to a mandatory dependency, trace-cmd will not run without it.

Quoted 7 lines

-- Steve

Quoted 3 lines

---

--
Tzvetomir (Ceco) Stoyanov
VMware Open Source Technology Center

#22 lore raw

Re: [PATCH v6 17/45] trace-cmd library: Add support for zlib compression library

Quoted 16 lines · On Tue, 22 Jun 2021 13:29:36 +0300 Tzvetomir Stoyanov <[email protected]>…
Quoted 7 lines · Steven Rostedt
Quoted 2 lines · On Mon, 14 Jun 2021 10:50:01 +0300 "Tzvetomir Stoyanov (VMware)"…

If libz is available, use that library to provide trace file compression support. The library is detected runtime.

Quoted 7 lines

Why have the library detected at runtime?

If it is detected, we can then have the library flags include -lz.

Why use dlopen to load zlib? And not just include it?

This seems rather fragile to try to get right.

Quoted 9 lines

The idea of this design is not to bring additional mandatory dependencies. I do not know if libz is available by default, but even if we assume it is - each additional compression library that is added will be a mandatory dependency. The compression is not a mandatory functionality, trace-cmd can work without it. Why do you think it is fragile, I think dlopen() uses the same core linker logic to find and load the library. The only difference I see is that using "-lz" leads to a mandatory dependency, trace-cmd will not run without it.

The dependency is determined at compile time, just like we do for the python libraries, and dwarf, and anything else that might add a new feature.

Let me explain a scenario of why I called it fragile. Let's say that trace-cmd has no package dependency on zlib, but the system has some other package that does have a dependency. Thus the package manager pulls in zlib to satisfy this other package.

The user does a recording, and trace-cmd detects zlib, and compresses the data.

Later, the user decides they do not need this other package and uninstalls it. The package manager sees there's nothing that depends on zlib anymore, and uninstalls zlib as well.

Then the user goes to read their trace.dat file, and suddenly trace-cmd can't read it!

THAT is what I call fragile. And if something like that ever happened to me, I would stop using whatever did that to me.

Manually pulling in system dynamic libraries with dlopen is something I never heard of. The way to do this is make it a build time dependency. If zlib exists, then define HAVE_ZLIB and allow compressions and everything else. If it does not, then we don't support compression. Simple as that.

Making it a runtime dependency has a lot of issues, *especially* since one run of trace-cmd (the reporting) depends on a previous run of trace-cmd (the recording), and if the environment changes between the two, the user will rightfully say WTF!

-- Steve

#23 lore raw

PATCH v6 18/45 trace-cmd library: Hide the logic for updating buffer offset

In reply to Tzvetomir Stoyanov (VMware)

When a trace buffer data are written in the trace file, the buffer option in the file metadata is updated with the file offset of the tracing data. Hide this logic into the trace-cmd library. Added new APIs:
tracecmd_add_buffer_description() tracecmd_write_buffers_description()
Changed APIs:
tracecmd_append_buffer_cpu_data()
Removed APIs:
tracecmd_add_buffer_option()

This internal refactoring is needed for changes, related to compression of the options sections of the trace file.

---
 .../include/private/trace-cmd-private.h       |  8 +-
 lib/trace-cmd/trace-output.c                  | 84 +++++++++++++++++--
 tracecmd/trace-record.c                       | 16 ++--
 3 files changed, 85 insertions(+), 23 deletions(-)
lib/trace-cmd/include/private/trace-cmd-private.h +3 -5
index ee73325c..cbb578ec 100644
@@ -295,8 +295,8 @@ struct tracecmd_option *
 tracecmd_add_option_v(struct tracecmd_output *handle,
 		      unsigned short id, const struct iovec *vector, int count);
 
-struct tracecmd_option *tracecmd_add_buffer_option(struct tracecmd_output *handle,
-						   const char *name, int cpus);
+int tracecmd_add_buffer_description(struct tracecmd_output *handle, const char *name, int cpus);
+int tracecmd_write_buffers_description(struct tracecmd_output *handle);
 
 int tracecmd_write_cpus(struct tracecmd_output *handle, int cpus);
 int tracecmd_write_cmdlines(struct tracecmd_output *handle);
@@ -312,9 +312,7 @@ int tracecmd_write_cpu_data(struct tracecmd_output *handle,
 int tracecmd_append_cpu_data(struct tracecmd_output *handle,
 			     int cpus, char * const *cpu_data_files);
 int tracecmd_append_buffer_cpu_data(struct tracecmd_output *handle,
-				    struct tracecmd_option *option,
-				    int cpus, char * const *cpu_data_files);
-
+				    const char *name, int cpus, char * const *cpu_data_files);
 struct tracecmd_output *tracecmd_get_output_handle_fd(int fd);
 
 /* --- Reading the Fly Recorder Trace --- */
lib/trace-cmd/trace-output.c +77 -7

146 lines not on this page · raw message

tracecmd/trace-record.c +5 -11
index d3362e5b..eff6f2f0 100644
@@ -4152,7 +4152,6 @@ static void touch_file(const char *file)
 }
 
 static void append_buffer(struct tracecmd_output *handle,
-			  struct tracecmd_option *buffer_option,
 			  struct buffer_instance *instance,
 			  char **temp_files)
 {
@@ -4180,7 +4179,7 @@ static void append_buffer(struct tracecmd_output *handle,
 			touch_file(temp_files[i]);
 	}
 
-	tracecmd_append_buffer_cpu_data(handle, buffer_option,
+	tracecmd_append_buffer_cpu_data(handle, tracefs_instance_get_name(instance->tracefs),
 					cpu_count, temp_files);
 
 	for (i = 0; i < instance->cpu_count; i++) {
@@ -4441,7 +4440,6 @@ static void write_guest_file(struct buffer_instance *instance)
 
 static void record_data(struct common_record_context *ctx)
 {
-	struct tracecmd_option **buffer_options;
 	struct tracecmd_output *handle;
 	struct buffer_instance *instance;
 	bool local = false;
@@ -4512,9 +4510,6 @@ static void record_data(struct common_record_context *ctx)
 		}
 
 		if (buffers) {
-			buffer_options = malloc(sizeof(*buffer_options) * buffers);
-			if (!buffer_options)
-				die("Failed to allocate buffer options");
 			i = 0;
 			for_each_instance(instance) {
 				int cpus = instance->cpu_count != local_cpu_count ?
@@ -4522,10 +4517,9 @@ static void record_data(struct common_record_context *ctx)
 
 				if (instance->msg_handle)
 					continue;
-
-				buffer_options[i++] = tracecmd_add_buffer_option(handle,
-										 tracefs_instance_get_name(instance->tracefs),
-										 cpus);
+				tracecmd_add_buffer_description(handle,
+								tracefs_instance_get_name(instance->tracefs),
+								cpus);
 				add_buffer_stat(handle, instance);
 			}
 		}
@@ -4560,7 +4554,7 @@ static void record_data(struct common_record_context *ctx)
 				if (instance->msg_handle)
 					continue;
 				print_stat(instance);
-				append_buffer(handle, buffer_options[i++], instance, temp_files);
+				append_buffer(handle, instance, temp_files);
 			}
 		}
 
Signature · 1 line
2.31.1
#24 lore raw

Re: [PATCH v6 18/45] trace-cmd library: Hide the logic for updating buffer offset

Quoted 10 lines · On Mon, 14 Jun 2021 10:50:02 +0300 "Tzvetomir Stoyanov (VMware)"…

When a trace buffer data are written in the trace file, the buffer option in the file metadata is updated with the file offset of the tracing data. Hide this logic into the trace-cmd library. Added new APIs:
tracecmd_add_buffer_description() tracecmd_write_buffers_description()
Changed APIs:
tracecmd_append_buffer_cpu_data()
Removed APIs:
tracecmd_add_buffer_option()

So I was going to add this patch with some other ones from this series, but I'm holding off just due to the names.

We are not writing a description, we are only adding name and cpus. We could change that to tracecmd_add_buffer_info() and tracecmd_write_buffer_info() ? As "info" is just information about the buffer, and name / cpu is that. But "description" usually means a bit more in depth information.

Even though the write_buffer_info() could be doing more than one buffer, I think it still sounds best keeping it singular, as it is writing the buffer_info section.

Quoted 42 lines

This internal refactoring is needed for changes, related to compression of the options sections of the trace file.

---
 .../include/private/trace-cmd-private.h       |  8 +-
 lib/trace-cmd/trace-output.c                  | 84 +++++++++++++++++--
 tracecmd/trace-record.c                       | 16 ++--
 3 files changed, 85 insertions(+), 23 deletions(-)
lib/trace-cmd/include/private/trace-cmd-private.h +3 -5
index ee73325c..cbb578ec 100644
@@ -295,8 +295,8 @@ struct tracecmd_option *
 tracecmd_add_option_v(struct tracecmd_output *handle,
 		      unsigned short id, const struct iovec *vector, int count);
 
-struct tracecmd_option *tracecmd_add_buffer_option(struct tracecmd_output *handle,
-						   const char *name, int cpus);
+int tracecmd_add_buffer_description(struct tracecmd_output *handle, const char *name, int cpus);
+int tracecmd_write_buffers_description(struct tracecmd_output *handle);
 
 int tracecmd_write_cpus(struct tracecmd_output *handle, int cpus);
 int tracecmd_write_cmdlines(struct tracecmd_output *handle);
@@ -312,9 +312,7 @@ int tracecmd_write_cpu_data(struct tracecmd_output *handle,
 int tracecmd_append_cpu_data(struct tracecmd_output *handle,
 			     int cpus, char * const *cpu_data_files);
 int tracecmd_append_buffer_cpu_data(struct tracecmd_output *handle,
-				    struct tracecmd_option *option,
-				    int cpus, char * const *cpu_data_files);
-
+				    const char *name, int cpus, char * const *cpu_data_files);
 struct tracecmd_output *tracecmd_get_output_handle_fd(int fd);
 
 /* --- Reading the Fly Recorder Trace --- */
lib/trace-cmd/trace-output.c +0 -0
index 7c7d3d76..8f8ca164 100644

lib/trace-cmd/trace-output.c struct tracecmd_option { · line 53

Quoted 10 lines
lib/trace-cmd/trace-output.c +7 -0
@@ -44,6 +44,14 @@ struct tracecmd_option {
 	struct list_head list;
 };
 
+struct tracecmd_buffer {
+	int		cpus;
+	void		*name;
+	tsize_t		offset;
+	struct tracecmd_option *option;
+	struct list_head list;
+};

You can add another tab to the above structure to make it look better:

struct tracecmd_buffer {
	int			cpus;
	void			*name;
	tsize_t			offset;
	struct tracecmd_option  *option;
	struct list_head 	list;
};

-- Steve

Quoted 206 lines
lib/trace-cmd/trace-output.c +70 -7
+
 enum {
 	OUTPUT_FL_SEND_META	= (1 << 0),
 };
@@ -63,6 +71,7 @@ struct tracecmd_output {
 	struct tracecmd_compression *compress;
 
 	struct list_head	options;
+	struct list_head	buffers;
 	struct tracecmd_msg_handle *msg_handle;
 	char			*trace_clock;
 };
@@ -189,6 +198,7 @@ bool tracecmd_get_quiet(struct tracecmd_output *handle)
 void tracecmd_output_free(struct tracecmd_output *handle)
 {
 	struct tracecmd_option *option;
+	struct tracecmd_buffer *buffer;
 
 	if (!handle)
 		return;
@@ -199,6 +209,13 @@ void tracecmd_output_free(struct tracecmd_output *handle)
 	if (handle->pevent)
 		tep_unref(handle->pevent);
 
+	while (!list_empty(&handle->buffers)) {
+		buffer = container_of(handle->buffers.next,
+				      struct tracecmd_buffer, list);
+		list_del(&buffer->list);
+		free(buffer->name);
+		free(buffer);
+	}
 	while (!list_empty(&handle->options)) {
 		option = container_of(handle->options.next,
 				      struct tracecmd_option, list);
@@ -1071,6 +1088,7 @@ create_file_fd(int fd, struct tracecmd_input *ihandle,
 		goto out_free;
 
 	list_head_init(&handle->options);
+	list_head_init(&handle->buffers);
 
 	buf[0] = 23;
 	buf[1] = 8;
@@ -1369,9 +1387,8 @@ int tracecmd_append_options(struct tracecmd_output *handle)
 	return 0;
 }
 
-struct tracecmd_option *
-tracecmd_add_buffer_option(struct tracecmd_output *handle, const char *name,
-			   int cpus)
+static struct tracecmd_option *
+add_buffer_option(struct tracecmd_output *handle, const char *name, int cpus)
 {
 	struct tracecmd_option *option;
 	char *buf;
@@ -1399,6 +1416,53 @@ tracecmd_add_buffer_option(struct tracecmd_output *handle, const char *name,
 	return option;
 }
 
+int tracecmd_add_buffer_description(struct tracecmd_output *handle, const char *name, int cpus)
+{
+	struct tracecmd_buffer *buf;
+
+	buf = calloc(1, sizeof(struct tracecmd_buffer));
+	if (!buf)
+		return -1;
+	buf->name = strdup(name);
+	buf->cpus = cpus;
+	if (!buf->name) {
+		free(buf);
+		return -1;
+	}
+	list_add_tail(&buf->list, &handle->buffers);
+	return 0;
+}
+
+int tracecmd_write_buffers_description(struct tracecmd_output *handle)
+{
+	struct tracecmd_option *option;
+	struct tracecmd_buffer *buf;
+
+	list_for_each_entry(buf, &handle->buffers, list) {
+		option = add_buffer_option(handle, buf->name, buf->cpus);
+		if (!option)
+			return -1;
+		buf->option = option;
+	}
+	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 (strlen(name) == strlen(buf->name) && !strcmp(name, buf->name)) {
+			if (handle->file_version >= 7)
+				return buf->offset;
+			if (!buf->option)
+				break;
+			return buf->option->offset;
+		}
+	}
+	return 0;
+}
+
 int tracecmd_write_cmdlines(struct tracecmd_output *handle)
 {
 	int ret;
@@ -1643,18 +1707,23 @@ int tracecmd_append_cpu_data(struct tracecmd_output *handle,
 }
 
 int tracecmd_append_buffer_cpu_data(struct tracecmd_output *handle,
-				    struct tracecmd_option *option,
-				    int cpus, char * const *cpu_data_files)
+				    const char *name, int cpus, char * const *cpu_data_files)
 {
+	tsize_t b_offset;
 	tsize_t offset;
 	stsize_t ret;
 
+	b_offset = get_buffer_file_offset(handle, name);
+	if (!b_offset) {
+		tracecmd_warning("Cannot find description for buffer %s\n", name);
+		return -1;
+	}
 	offset = lseek64(handle->fd, 0, SEEK_CUR);
 
 	/* Go to the option data, where will write the offest */
-	ret = lseek64(handle->fd, option->offset, SEEK_SET);
+	ret = lseek64(handle->fd, b_offset, SEEK_SET);
 	if (ret == (off64_t)-1) {
-		tracecmd_warning("could not seek to %lld\n", option->offset);
+		tracecmd_warning("could not seek to %lld\n", b_offset);
 		return -1;
 	}
 
@@ -1713,6 +1782,7 @@ struct tracecmd_output *tracecmd_get_output_handle_fd(int fd)
 	handle->page_size = tracecmd_page_size(ihandle);
 	handle->file_version = tracecmd_get_in_file_version(ihandle);
 	list_head_init(&handle->options);
+	list_head_init(&handle->buffers);
 
 	if (!tracecmd_get_file_compress_proto(ihandle, &cname, &cver)) {
 		handle->compress = tracecmd_compress_alloc(cname, cver, handle->fd,
tracecmd/trace-record.c +5 -11
index d3362e5b..eff6f2f0 100644
@@ -4152,7 +4152,6 @@ static void touch_file(const char *file)
 }
 
 static void append_buffer(struct tracecmd_output *handle,
-			  struct tracecmd_option *buffer_option,
 			  struct buffer_instance *instance,
 			  char **temp_files)
 {
@@ -4180,7 +4179,7 @@ static void append_buffer(struct tracecmd_output *handle,
 			touch_file(temp_files[i]);
 	}
 
-	tracecmd_append_buffer_cpu_data(handle, buffer_option,
+	tracecmd_append_buffer_cpu_data(handle, tracefs_instance_get_name(instance->tracefs),
 					cpu_count, temp_files);
 
 	for (i = 0; i < instance->cpu_count; i++) {
@@ -4441,7 +4440,6 @@ static void write_guest_file(struct buffer_instance *instance)
 
 static void record_data(struct common_record_context *ctx)
 {
-	struct tracecmd_option **buffer_options;
 	struct tracecmd_output *handle;
 	struct buffer_instance *instance;
 	bool local = false;
@@ -4512,9 +4510,6 @@ static void record_data(struct common_record_context *ctx)
 		}
 
 		if (buffers) {
-			buffer_options = malloc(sizeof(*buffer_options) * buffers);
-			if (!buffer_options)
-				die("Failed to allocate buffer options");
 			i = 0;
 			for_each_instance(instance) {
 				int cpus = instance->cpu_count != local_cpu_count ?
@@ -4522,10 +4517,9 @@ static void record_data(struct common_record_context *ctx)
 
 				if (instance->msg_handle)
 					continue;
-
-				buffer_options[i++] = tracecmd_add_buffer_option(handle,
-										 tracefs_instance_get_name(instance->tracefs),
-										 cpus);
+				tracecmd_add_buffer_description(handle,
+								tracefs_instance_get_name(instance->tracefs),
+								cpus);
 				add_buffer_stat(handle, instance);
 			}
 		}
@@ -4560,7 +4554,7 @@ static void record_data(struct common_record_context *ctx)
 				if (instance->msg_handle)
 					continue;
 				print_stat(instance);
-				append_buffer(handle, buffer_options[i++], instance, temp_files);
+				append_buffer(handle, instance, temp_files);
 			}
 		}
 
#25 lore raw

PATCH v6 19/45 trace-cmd: Move buffers description outside of options

In reply to Tzvetomir Stoyanov (VMware)

When a new buffer data are written in the trace file, the file offset is stored in the buffer description. As the buffer description is part of the trace options, which are already written in the file, the offset field in this option is updated with the new value. This causes a serious problem when option section of the file is compressed. Updating a value in already compressed section may change the size of this section, which can make the calculated offset of the buffer trace data invalid. To solve that problem and to enable the compression of the option section of the file, the structure of the trace file is changed:
- In trace file v7, the description of the buffers is moved to a separate section in the trace file medatata. It is stored after the CPU count section and before the option section. The format of this new buffers description is:
4 bytes - count of the buffers, stored in the file. The top buffer
is not counted.
For each buffer:
8 bytes, offset of the trace data for this buffer in the file. null-terminated string, name of the buffer.
The new section is mandatory, in case of no buffers - the count 0 is stored in the first 4 bytes.
- In trace file v6, the description of the buffers is not changed.

These changes are needed for compression of the options sections from the trace file.

---
 .../include/private/trace-cmd-private.h       |  1 +
 lib/trace-cmd/include/trace-cmd-local.h       |  3 +
 lib/trace-cmd/trace-input.c                   | 40 +++++++++
 lib/trace-cmd/trace-output.c                  | 85 ++++++++++++++++++-
 tracecmd/trace-dump.c                         | 24 ++++++
 tracecmd/trace-listen.c                       |  3 +
 tracecmd/trace-record.c                       |  4 +
 7 files changed, 156 insertions(+), 4 deletions(-)
lib/trace-cmd/include/private/trace-cmd-private.h +1 -0
index cbb578ec..faa8c533 100644
@@ -112,6 +112,7 @@ enum tracecmd_file_states {
 	TRACECMD_FILE_PRINTK,
 	TRACECMD_FILE_CMD_LINES,
 	TRACECMD_FILE_CPU_COUNT,
+	TRACECMD_FILE_BUFERS,
 	TRACECMD_FILE_OPTIONS,
 	TRACECMD_FILE_CPU_LATENCY,
 	TRACECMD_FILE_CPU_FLYRECORD,
lib/trace-cmd/include/trace-cmd-local.h +3 -0
index 40a6a40d..2f421103 100644
@@ -46,4 +46,7 @@ void out_compression_reset(struct tracecmd_output *handle);
 void in_uncompress_reset(struct tracecmd_input *handle);
 int in_uncompress_block(struct tracecmd_input *handle);
 
+int write_buffers_description_v7(struct tracecmd_output *handle);
+int write_buffers_description_v6(struct tracecmd_output *handle);
+
 #endif /* _TRACE_CMD_LOCAL_H */
lib/trace-cmd/trace-input.c +40 -0
index 0b17b58d..e953909f 100644
@@ -881,6 +881,39 @@ static int read_cpus(struct tracecmd_input *handle)
 	return 0;
 }
 
+static int read_buffers(struct tracecmd_input *handle)
+{
+	struct input_buffer_instance *buffer;
+	unsigned long long offset;
+	unsigned int count;
+	int i;
+
+	if (handle->file_version < 7 || handle->file_state >= TRACECMD_FILE_BUFERS)
+		return 0;
+
+	if (read4(handle, &count) < 0)
+		return -1;
+	i = handle->nr_buffers;
+	handle->nr_buffers += count;
+	handle->buffers = realloc(handle->buffers,
+				  sizeof(*handle->buffers) * handle->nr_buffers);
+	if (!handle->buffers)
+		return -1;
+
+	for (; i < handle->nr_buffers; i++) {
+		buffer = &handle->buffers[i];
+		if (read8(handle, &offset) < 0)
+			return -1;
+		buffer->offset = offset;
+		buffer->name = read_string(handle);
+		if (!buffer->name)
+			return -1;
+	}
+
+	handle->file_state = TRACECMD_FILE_BUFERS;
+	return 0;
+}
+
 /**
  * tracecmd_read_headers - read the header information from trace.dat
  * @handle: input handle for the trace.dat file
@@ -955,6 +988,13 @@ int tracecmd_read_headers(struct tracecmd_input *handle,
 	if (state <= handle->file_state)
 		return 0;
 
+	if (read_buffers(handle) < 0)
+		return -1;
+
+	if (state <= handle->file_state)
+		return 0;
+
+
 	if (read_options_type(handle) < 0)
 		return -1;
 
lib/trace-cmd/trace-output.c +81 -4

124 lines not on this page · raw message

tracecmd/trace-dump.c +24 -0
index ed82104b..56acb01d 100644
@@ -420,6 +420,29 @@ static void dump_cpus_count(int fd)
 	do_print(SUMMARY, "\t%d [CPUs with tracing data]\n", trace_cpus);
 }
 
+static void dump_buffers(int fd)
+{
+	unsigned long long offset;
+	char name[DUMP_SIZE];
+	int count;
+
+	if (file_version < 7)
+		return;
+
+	if (read_file_number(fd, &count, 4))
+		die("cannot read the buffers count");
+	do_print(SUMMARY, "\t%d [buffers with tracing data]\n", count);
+
+	while (count) {
+		if (read_file_number(fd, &offset, 8))
+			die("cannot read the buffer offset");
+		if (read_file_string(fd, name, DUMP_SIZE))
+			die("cannot read the buffer name");
+		do_print(SUMMARY, "\t\t 0x%llx %s[data offset, buffer name]\n", offset, name);
+		count--;
+	}
+}
+
 static void dump_option_string(int fd, int size, char *desc)
 {
 	do_print(OPTIONS, "\t\t[Option %s, %d bytes]\n", desc, size);
@@ -762,6 +785,7 @@ static void dump_file(const char *file)
 	dump_printk(fd);
 	dump_cmdlines(fd);
 	dump_cpus_count(fd);
+	dump_buffers(fd);
 	dump_therest(fd);
 
 	tep_free(tep);
tracecmd/trace-listen.c +3 -0
index 0ae1c948..62debdb6 100644
@@ -604,6 +604,9 @@ static int put_together_file(int cpus, int ofd, const char *node,
 
 	if (write_options) {
 		ret = tracecmd_write_cpus(handle, cpus);
+		if (ret)
+			goto out;
+		ret = tracecmd_write_buffers_description(handle);
 		if (ret)
 			goto out;
 		ret = tracecmd_write_options(handle);
tracecmd/trace-record.c +4 -0
index eff6f2f0..2a379d09 100644
@@ -3711,6 +3711,9 @@ setup_connection(struct buffer_instance *instance, struct common_record_context
 		if (ret)
 			goto error;
 		ret = tracecmd_write_cpus(network_handle, instance->cpu_count);
+		if (ret)
+			goto error;
+		ret = tracecmd_write_buffers_description(network_handle);
 		if (ret)
 			goto error;
 		ret = tracecmd_write_options(network_handle);
@@ -4074,6 +4077,7 @@ static void setup_agent(struct buffer_instance *instance,
 	add_options(network_handle, ctx);
 	tracecmd_write_cmdlines(network_handle);
 	tracecmd_write_cpus(network_handle, instance->cpu_count);
+	tracecmd_write_buffers_description(network_handle);
 	tracecmd_write_options(network_handle);
 	tracecmd_msg_finish_sending_data(instance->msg_handle);
 	instance->network_handle = network_handle;
Signature · 1 line
2.31.1
#26 lore raw

Re: [PATCH v6 19/45] trace-cmd: Move buffers description outside of options

lib/trace-cmd/include/private/trace-cmd-private.h enum tracecmd_file_states { · line 115

Quoted 7 lines · On Mon, 14 Jun 2021 10:50:03 +0300 "Tzvetomir Stoyanov (VMware)"…
lib/trace-cmd/include/private/trace-cmd-private.h +1 -0
@@ -112,6 +112,7 @@ enum tracecmd_file_states {
 	TRACECMD_FILE_PRINTK,
 	TRACECMD_FILE_CMD_LINES,
 	TRACECMD_FILE_CPU_COUNT,
+	TRACECMD_FILE_BUFERS,

Use the proper spelling of BUFFERS.

Thanks,

-- Steve

Quoted 3 lines
lib/trace-cmd/include/private/trace-cmd-private.h +0 -0
 	TRACECMD_FILE_OPTIONS,
 	TRACECMD_FILE_CPU_LATENCY,
 	TRACECMD_FILE_CPU_FLYRECORD,
#27 lore raw

PATCH v6 20/45 trace-cmd library: Track the offset in the option section in the trace file

In reply to Tzvetomir Stoyanov (VMware)

When reading / writing trace file, store the offset of the option section in the file. A new internal API is added, to get the offset of the option section from an input file handler:
tracecmd_get_options_offset()

These changes are needed for compression of the option section from the trace file.

---
 lib/trace-cmd/include/private/trace-cmd-private.h |  1 +
 lib/trace-cmd/trace-input.c                       | 11 +++++++++++
 lib/trace-cmd/trace-output.c                      |  2 ++
 3 files changed, 14 insertions(+)
lib/trace-cmd/include/private/trace-cmd-private.h +1 -0
index faa8c533..10089389 100644
@@ -239,6 +239,7 @@ tracecmd_get_cursor(struct tracecmd_input *handle, int cpu);
 unsigned long tracecmd_get_in_file_version(struct tracecmd_input *handle);
 int tracecmd_get_file_compress_proto(struct tracecmd_input *handle,
 				     const char **name, const char **version);
+size_t tracecmd_get_options_offset(struct tracecmd_input *handle);
 
 int tracecmd_ftrace_overrides(struct tracecmd_input *handle, struct tracecmd_ftrace *finfo);
 bool tracecmd_get_use_trace_clock(struct tracecmd_input *handle);
lib/trace-cmd/trace-input.c +11 -0
index e953909f..b76f2b47 100644
@@ -157,6 +157,7 @@ struct tracecmd_input {
 	size_t			header_files_start;
 	size_t			ftrace_files_start;
 	size_t			event_files_start;
+	size_t			options_start;
 	size_t			total_file_size;
 
 	/* For custom profilers. */
@@ -2759,6 +2760,7 @@ static int handle_options(struct tracecmd_input *handle)
 
 	/* By default, use usecs, unless told otherwise */
 	handle->flags |= TRACECMD_FL_IN_USECS;
+	handle->options_start = lseek64(handle->fd, 0, SEEK_CUR);
 
 	for (;;) {
 		if (do_read_check(handle, &option, 2))
@@ -4213,6 +4215,15 @@ bool tracecmd_get_use_trace_clock(struct tracecmd_input *handle)
 	return handle->use_trace_clock;
 }
 
+/**
+ * tracecmd_get_options_offset - get offset of the options sections in the file
+ * @handle: input handle for the trace.dat file
+ */
+size_t tracecmd_get_options_offset(struct tracecmd_input *handle)
+{
+	return handle->options_start;
+}
+
 /**
  * tracecmd_get_show_data_func - return the show data func
  * @handle: input handle for the trace.dat file
lib/trace-cmd/trace-output.c +2 -0
index 7830f643..bad234c4 100644
@@ -66,6 +66,7 @@ struct tracecmd_output {
 	bool			quiet;
 	unsigned long		file_state;
 	unsigned long		file_version;
+	size_t			options_start;
 
 	bool			do_compress;
 	struct tracecmd_compression *compress;
@@ -1858,6 +1859,7 @@ struct tracecmd_output *tracecmd_get_output_handle_fd(int fd)
 	tep_ref(handle->pevent);
 	handle->page_size = tracecmd_page_size(ihandle);
 	handle->file_version = tracecmd_get_in_file_version(ihandle);
+	handle->options_start = tracecmd_get_options_offset(ihandle);
 	list_head_init(&handle->options);
 	list_head_init(&handle->buffers);
 
Signature · 1 line
2.31.1
#28 lore raw

PATCH v6 21/45 trace-cmd library: Add compression of the option section of the trace file

In reply to Tzvetomir Stoyanov (VMware)

Comperss the option section of the trace file. This section is not big currently and compressing it does not reduce significantly the size of the file. This could be useful in the future as new options can be added, storing a potentially huge amount of data.

---
 lib/trace-cmd/include/trace-cmd-local.h |  1 +
 lib/trace-cmd/trace-input.c             | 46 +++++++++++------
 lib/trace-cmd/trace-output.c            | 69 +++++++++++++++++--------
 tracecmd/trace-dump.c                   | 16 ++++--
 4 files changed, 92 insertions(+), 40 deletions(-)
lib/trace-cmd/include/trace-cmd-local.h +1 -0
index 2f421103..03d2a9a3 100644
@@ -39,6 +39,7 @@ void tracecmd_zlib_free(void);
 void tracecmd_compress_init(void);
 void tracecmd_compress_free(void);
 
+int out_uncompress_block(struct tracecmd_output *handle);
 int out_compression_start(struct tracecmd_output *handle);
 int out_compression_end(struct tracecmd_output *handle);
 void out_compression_reset(struct tracecmd_output *handle);
lib/trace-cmd/trace-input.c +30 -16

89 lines not on this page · raw message

lib/trace-cmd/trace-output.c +48 -21

128 lines not on this page · raw message

tracecmd/trace-dump.c +13 -3
index 56acb01d..8d0f2251 100644
@@ -82,6 +82,13 @@ static int read_compressed(int fd, char *dst, int len)
 	return read_fd(fd, dst, len);
 }
 
+static int do_lseek(int fd, int offset, int whence)
+{
+	if (read_compress)
+		return tracecmd_compress_lseek(compress, offset, whence);
+	return lseek64(fd, offset, whence);
+}
+
 static int read_file_string(int fd, char *dst, int len)
 {
 	size_t size = 0;
@@ -625,6 +632,9 @@ static void dump_options(int fd)
 	unsigned int size;
 	int count = 0;
 
+	if (uncompress_block())
+		die("cannot uncompress file block");
+
 	for (;;) {
 		if (read_file_number(fd, &option, 2))
 			die("cannot read the option id");
@@ -635,7 +645,7 @@ static void dump_options(int fd)
 
 		count++;
 		if (!DUMP_CHECK(OPTIONS) && !DUMP_CHECK(CLOCK) && !DUMP_CHECK(SUMMARY)) {
-			lseek64(fd, size, SEEK_CUR);
+			do_lseek(fd, size, SEEK_CUR);
 			continue;
 		}
 		switch (option) {
@@ -685,12 +695,12 @@ static void dump_options(int fd)
 		default:
 			do_print(OPTIONS, " %d %d\t[Unknown option, size - skipping]\n",
 				 option, size);
-			lseek64(fd, size, SEEK_CUR);
+			do_lseek(fd, size, SEEK_CUR);
 			break;
 		}
 	}
 	do_print(SUMMARY, "\t[%d options]\n", count);
-
+	uncompress_reset();
 }
 
 static void dump_latency(int fd)
Signature · 1 line
2.31.1
#29 lore raw

Re: [PATCH v6 21/45] trace-cmd library: Add compression of the option section of the trace file

Quoted 4 lines · On Mon, 14 Jun 2021 10:50:05 +0300 "Tzvetomir Stoyanov (VMware)"…

Comperss the option section of the trace file. This section is not big currently and compressing it does not reduce significantly the size of the file. This could be useful in the future as new options can be added, storing a potentially huge amount of data.

I'm not sure we want to bother doing this. New options should never be large, and if they are, then the option itself can be compressed when created.

What good reason is there to compress the options?

I believe this is just adding more complexity than needed.

-- Steve

#30 lore raw

Re: [PATCH v6 21/45] trace-cmd library: Add compression of the option section of the trace file

Quoted 9 lines · Steven Rostedt
Quoted 4 lines · On Mon, 14 Jun 2021 10:50:05 +0300 "Tzvetomir Stoyanov (VMware)"…

Comperss the option section of the trace file. This section is not big currently and compressing it does not reduce significantly the size of the file. This could be useful in the future as new options can be added, storing a potentially huge amount of data.

Quoted 5 lines

I'm not sure we want to bother doing this. New options should never be large, and if they are, then the option itself can be compressed when created.

What good reason is there to compress the options?

I think adding any new metadata should be done only by adding new options - this will not break the format. For example kernel debug information for parsing function's arguments - it will be huge. Having the entire option section compressed will simplify that. And it is consistent with the other metadata sections.

Quoted 4 lines

I believe this is just adding more complexity than needed.

-- Steve

Signature · 2 lines
Tzvetomir (Ceco) Stoyanov
VMware Open Source Technology Center
#31 lore raw

PATCH v6 22/45 trace-cmd library: Refactor the logic for writing trace data in the file

In reply to Tzvetomir Stoyanov (VMware)

Refactored the internal logic of tracecmd_write_cpu_data() API to be suitable for adding trace data compression.

---
 lib/trace-cmd/trace-output.c | 94 +++++++++++++++++-------------------
 1 file changed, 44 insertions(+), 50 deletions(-)
lib/trace-cmd/trace-output.c +44 -50
index f7e6c3dd..ed4ef638 100644
@@ -1679,11 +1679,17 @@ static char *get_clock(struct tracecmd_output *handle)
 	return handle->trace_clock;
 }
 
+struct data_file_write {
+	int file_size;
+	off64_t soffset;
+	off64_t data_offset;
+	off64_t doffset;
+};
+
 int tracecmd_write_cpu_data(struct tracecmd_output *handle,
 			    int cpus, char * const *cpu_data_files)
 {
-	off64_t *offsets = NULL;
-	unsigned long long *sizes = NULL;
+	struct data_file_write *data_files = NULL;
 	off64_t offset;
 	unsigned long long endian8;
 	char *clock = NULL;
@@ -1705,37 +1711,11 @@ int tracecmd_write_cpu_data(struct tracecmd_output *handle,
 	if (do_write_check(handle, "flyrecord", 10))
 		goto out_free;
 
-	offsets = malloc(sizeof(*offsets) * cpus);
-	if (!offsets)
-		goto out_free;
-	sizes = malloc(sizeof(*sizes) * cpus);
-	if (!sizes)
-		goto out_free;
-
-	offset = lseek64(handle->fd, 0, SEEK_CUR);
-
-	/* hold any extra data for data */
-	offset += cpus * (16);
-
-	/*
-	 * Unfortunately, the trace_clock data was placed after the
-	 * cpu data, and wasn't accounted for with the offsets.
-	 * We need to save room for the trace_clock file. This means
-	 * we need to find the size of it before we define the final
-	 * offsets.
-	 */
-	clock = get_clock(handle);
-	if (!clock)
+	data_files = calloc(cpus, sizeof(struct data_file_write));
+	if (!data_files)
 		goto out_free;
-	/* Save room for storing the size */
-	offset += 8;
-	offset += strlen(clock);
-	/* 2 bytes for [] around the clock */
-	offset += 2;
-
-	/* Page align offset */
-	offset = (offset + (handle->page_size - 1)) & ~(handle->page_size - 1);
 
+	/* Write 0 for trace data offset and size and store offsets of these fields */
 	for (i = 0; i < cpus; i++) {
 		file = cpu_data_files[i];
 		ret = stat(file, &st);
@@ -1743,53 +1723,67 @@ int tracecmd_write_cpu_data(struct tracecmd_output *handle,
 			tracecmd_warning("can not stat '%s'", file);
 			goto out_free;
 		}
-		offsets[i] = offset;
-		sizes[i] = st.st_size;
-		offset += st.st_size;
-		offset = (offset + (handle->page_size - 1)) & ~(handle->page_size - 1);
+		data_files[i].file_size = st.st_size;
 
-		endian8 = convert_endian_8(handle, offsets[i]);
+		endian8 = 0;
+		data_files[i].doffset = lseek64(handle->fd, 0, SEEK_CUR);
 		if (do_write_check(handle, &endian8, 8))
 			goto out_free;
-		endian8 = convert_endian_8(handle, sizes[i]);
+		data_files[i].soffset = lseek64(handle->fd, 0, SEEK_CUR);
 		if (do_write_check(handle, &endian8, 8))
 			goto out_free;
 	}
 
-	if (save_clock(handle, clock))
+	clock = get_clock(handle);
+	if (clock && save_clock(handle, clock))
 		goto out_free;
 
 	for (i = 0; i < cpus; i++) {
+		data_files[i].data_offset = lseek64(handle->fd, 0, SEEK_CUR);
+		/* Page align offset */
+		data_files[i].data_offset = (data_files[i].data_offset + (handle->page_size - 1)) & ~(handle->page_size - 1);
+		data_files[i].data_offset = lseek64(handle->fd, data_files[i].data_offset, SEEK_SET);
+		if (data_files[i].data_offset == (off64_t)-1)
+			goto out_free;
 		if (!tracecmd_get_quiet(handle))
 			fprintf(stderr, "CPU%d data recorded at offset=0x%llx\n",
-				i, (unsigned long long) offsets[i]);
-		offset = lseek64(handle->fd, offsets[i], SEEK_SET);
-		if (offset == (off64_t)-1) {
-			tracecmd_warning("could not seek to %lld\n", offsets[i]);
-			goto out_free;
-		}
+				i, (unsigned long long) data_files[i].data_offset);
+
 		check_size = copy_file(handle, cpu_data_files[i]);
-		if (check_size != sizes[i]) {
+		if (check_size != data_files[i].file_size) {
 			errno = EINVAL;
 			tracecmd_warning("did not match size of %lld to %lld",
-					 check_size, sizes[i]);
+					 check_size, data_files[i].file_size);
 			goto out_free;
 		}
+		/* Write the real CPU data offset inthe file */
+		offset = lseek64(handle->fd, data_files[i].doffset, SEEK_SET);
+		endian8 = convert_endian_8(handle, data_files[i].data_offset);
+		if (do_write_check(handle, &endian8, 8))
+			goto out_free;
+		/* Write the real CPU data size in the file */
+		offset = lseek64(handle->fd, data_files[i].soffset, SEEK_SET);
+		endian8 = convert_endian_8(handle, check_size);
+		if (do_write_check(handle, &endian8, 8))
+			goto out_free;
+		offset = data_files[i].data_offset + check_size;
+		offset = lseek64(handle->fd, offset, SEEK_SET);
 		if (!tracecmd_get_quiet(handle))
 			fprintf(stderr, "    %llu bytes in size\n",
 				(unsigned long long)check_size);
 	}
 
-	free(offsets);
-	free(sizes);
+	if (lseek64(handle->fd, 0, SEEK_END) == (off64_t)-1)
+		goto out_free;
 
+	free(data_files);
 	handle->file_state = TRACECMD_FILE_CPU_FLYRECORD;
 
 	return 0;
 
  out_free:
-	free(offsets);
-	free(sizes);
+	lseek64(handle->fd, 0, SEEK_END);
+	free(data_files);
 	return -1;
 }
 
Signature · 1 line
2.31.1
#32 lore raw

Re: [PATCH v6 22/45] trace-cmd library: Refactor the logic for writing trace data in the file

Quoted 2 lines · On Mon, 14 Jun 2021 10:50:06 +0300 "Tzvetomir Stoyanov (VMware)"…

Refactored the internal logic of tracecmd_write_cpu_data() API to be suitable for adding trace data compression.

The change log should be more descriptive of what is being refactored.

-- Steve

#33 lore raw

PATCH v6 23/45 trace-cmd library: Add APIs for read and write compressed data in chunks

In reply to Tzvetomir Stoyanov (VMware)

New compression APIs are added for writing and reading compressed data from / to files in chunks:
tracecmd_compress_copy_from() tracecmd_uncompress_copy_to()
Format of the compressed data, used by these APIs, is:
- 4 bytes, chunks count
- for each chunk:
- 4 bytes, size of compressed data in this chunk
- 4 bytes, uncompressed size of the data in this chunk
- data, bytes of <size of compressed data in this chunk>

---
 .../include/private/trace-cmd-private.h       |   5 +-
 lib/trace-cmd/trace-compress.c                | 200 ++++++++++++++++++
 2 files changed, 204 insertions(+), 1 deletion(-)
lib/trace-cmd/include/private/trace-cmd-private.h +4 -1
index 10089389..067ba34d 100644
@@ -494,7 +494,10 @@ int tracecmd_compress_proto_register(const char *name, const char *version, int
 						       char *out, unsigned int *out_bytes),
 				     unsigned int (*comress_size)(unsigned int bytes),
 				     bool (*is_supported)(const char *name, const char *version));
-
+int tracecmd_compress_copy_from(struct tracecmd_compression *handle, int fd, int chunk_size,
+				unsigned long long *read_size, unsigned long long *write_size);
+int tracecmd_uncompress_copy_to(struct tracecmd_compression *handle, int fd,
+				unsigned long long *read_size, unsigned long long *write_size);
 /* --- Plugin handling --- */
 extern struct tep_plugin_option trace_ftrace_options[];
 
lib/trace-cmd/trace-compress.c +200 -0

203 lines not on this page · raw message

Signature · 1 line
2.31.1
#34 lore raw

PATCH v6 24/45 trace-cmd: Compress trace data

In reply to Tzvetomir Stoyanov (VMware)

When creating a trace.dat file of version 7, compress the trace data. The data is compressed in chunks. The chunk size is a compile time parameter, set by default to 1MB.

---
 lib/trace-cmd/trace-output.c | 50 +++++++++++++++++++++++++++++++-----
 1 file changed, 43 insertions(+), 7 deletions(-)
lib/trace-cmd/trace-output.c +43 -7
index ed4ef638..20a646af 100644
@@ -321,6 +321,41 @@ static tsize_t copy_file(struct tracecmd_output *handle,
 	return size;
 }
 
+#define COMPRESS_CHUNK_SIZE (1*1024*1024)
+static tsize_t copy_file_compress(struct tracecmd_output *handle,
+				  const char *file, unsigned long long *write_size)
+{
+	unsigned long long rsize = 0;
+	unsigned long long wsize = 0;
+	tsize_t size;
+	int ret;
+	int fd;
+
+	fd = open(file, O_RDONLY);
+	if (fd < 0) {
+		tracecmd_warning("Can't read '%s'", file);
+		return 0;
+	}
+
+	if (handle->file_version >= 7) {
+		ret = tracecmd_compress_copy_from(handle->compress, fd,
+						  COMPRESS_CHUNK_SIZE, &rsize, &wsize);
+		if (ret < 0) {
+			tracecmd_warning("Can't compress '%s'", file);
+			close(fd);
+			return 0;
+		}
+		size = rsize;
+		*write_size = wsize;
+	} else {
+		size = copy_file_fd(handle, fd);
+		*write_size = size;
+	}
+
+	close(fd);
+	return size;
+}
+
 /*
  * Finds the path to the debugfs/tracing
  * Allocates the string and stores it.
@@ -1692,8 +1727,9 @@ int tracecmd_write_cpu_data(struct tracecmd_output *handle,
 	struct data_file_write *data_files = NULL;
 	off64_t offset;
 	unsigned long long endian8;
+	unsigned long long read_size;
+	unsigned long long write_size;
 	char *clock = NULL;
-	off64_t check_size;
 	char *file;
 	struct stat st;
 	int ret;
@@ -1749,11 +1785,11 @@ int tracecmd_write_cpu_data(struct tracecmd_output *handle,
 			fprintf(stderr, "CPU%d data recorded at offset=0x%llx\n",
 				i, (unsigned long long) data_files[i].data_offset);
 
-		check_size = copy_file(handle, cpu_data_files[i]);
-		if (check_size != data_files[i].file_size) {
+		read_size = copy_file_compress(handle, cpu_data_files[i], &write_size);
+		if (read_size != data_files[i].file_size) {
 			errno = EINVAL;
 			tracecmd_warning("did not match size of %lld to %lld",
-					 check_size, data_files[i].file_size);
+					 read_size, data_files[i].file_size);
 			goto out_free;
 		}
 		/* Write the real CPU data offset inthe file */
@@ -1763,14 +1799,14 @@ int tracecmd_write_cpu_data(struct tracecmd_output *handle,
 			goto out_free;
 		/* Write the real CPU data size in the file */
 		offset = lseek64(handle->fd, data_files[i].soffset, SEEK_SET);
-		endian8 = convert_endian_8(handle, check_size);
+		endian8 = convert_endian_8(handle, write_size);
 		if (do_write_check(handle, &endian8, 8))
 			goto out_free;
-		offset = data_files[i].data_offset + check_size;
+		offset = data_files[i].data_offset + write_size;
 		offset = lseek64(handle->fd, offset, SEEK_SET);
 		if (!tracecmd_get_quiet(handle))
 			fprintf(stderr, "    %llu bytes in size\n",
-				(unsigned long long)check_size);
+				(unsigned long long)write_size);
 	}
 
 	if (lseek64(handle->fd, 0, SEEK_END) == (off64_t)-1)
Signature · 1 line
2.31.1
#35 lore raw

PATCH v6 25/45 trace-cmd: Read compressed trace data

In reply to Tzvetomir Stoyanov (VMware)

When reading a trace.dat file of version 7, uncompress the trace data. The trace data for each CPU is uncompressed in a temporary file, located in /tmp directory with prefix "trace_cpu_data".

---
 lib/trace-cmd/trace-input.c | 74 +++++++++++++++++++++++++++++--------
 tracecmd/trace-read.c       |  8 ++++
 2 files changed, 67 insertions(+), 15 deletions(-)
lib/trace-cmd/trace-input.c +59 -15
index 8fff003e..327082a2 100644
@@ -54,6 +54,7 @@ struct page {
 #endif
 };
 
+#define COMPR_TEMP_FILE "/tmp/trace_cpu_dataXXXXXX"
 struct cpu_data {
 	/* the first two never change */
 	unsigned long long	file_offset;
@@ -72,6 +73,10 @@ struct cpu_data {
 	int			page_cnt;
 	int			cpu;
 	int			pipe_fd;
+
+	/* temporary file for uncompressed cpu data */
+	int			cfd;
+	char			cfile[26]; /* strlen(COMPR_TEMP_FILE) */
 };
 
 struct input_buffer_instance {
@@ -1080,6 +1085,7 @@ static void *allocate_page_map(struct tracecmd_input *handle,
 	off64_t map_offset;
 	void *map;
 	int ret;
+	int fd;
 
 	if (handle->read_page) {
 		map = malloc(handle->page_size);
@@ -1119,12 +1125,15 @@ static void *allocate_page_map(struct tracecmd_input *handle,
 		map_size -= map_offset + map_size -
 			(cpu_data->file_offset + cpu_data->file_size);
 
+	if (cpu_data->cfd >= 0)
+		fd = cpu_data->cfd;
+	else
+		fd = handle->fd;
  again:
 	page_map->size = map_size;
 	page_map->offset = map_offset;
 
-	page_map->map = mmap(NULL, map_size, PROT_READ, MAP_PRIVATE,
-			 handle->fd, map_offset);
+	page_map->map = mmap(NULL, map_size, PROT_READ, MAP_PRIVATE, fd, map_offset);
 
 	if (page_map->map == MAP_FAILED) {
 		/* Try a smaller map */
@@ -2316,13 +2325,41 @@ tracecmd_read_prev(struct tracecmd_input *handle, struct tep_record *record)
 	/* Not reached */
 }
 
+static int cpu_data_uncompress(struct tracecmd_input *handle, int cpu, unsigned long long *size)
+{
+	struct cpu_data *cpu_data;
+
+	cpu_data = &handle->cpu_data[cpu];
+	strcpy(cpu_data->cfile, COMPR_TEMP_FILE);
+	cpu_data->cfd = mkstemp(cpu_data->cfile);
+	if (cpu_data->cfd < 0)
+		return -1;
+	return tracecmd_uncompress_copy_to(handle->compress, cpu_data->cfd, NULL, size);
+}
+
 static int init_cpu(struct tracecmd_input *handle, int cpu)
 {
 	struct cpu_data *cpu_data = &handle->cpu_data[cpu];
+	unsigned long long size;
+	off64_t offset;
 	int i;
 
-	cpu_data->offset = cpu_data->file_offset;
-	cpu_data->size = cpu_data->file_size;
+	if (handle->file_version >= 7 && cpu_data->file_size > 0) {
+		offset = lseek64(handle->fd, 0, SEEK_CUR);
+		if (lseek64(handle->fd, cpu_data->file_offset, SEEK_SET) == (off_t)-1)
+			return -1;
+		if (cpu_data_uncompress(handle, cpu, &size) < 0)
+			return -1;
+		cpu_data->offset = 0;
+		cpu_data->file_offset = 0;
+		cpu_data->file_size = size;
+		cpu_data->size = size;
+		if (lseek64(handle->fd, offset, SEEK_SET) == (off_t)-1)
+			return -1;
+	} else {
+		cpu_data->offset = cpu_data->file_offset;
+		cpu_data->size = cpu_data->file_size;
+	}
 	cpu_data->timestamp = 0;
 
 	list_head_init(&cpu_data->page_maps);
@@ -3015,6 +3052,7 @@ static int read_cpu_data(struct tracecmd_input *handle)
 
 		handle->cpu_data[cpu].file_offset = offset;
 		handle->cpu_data[cpu].file_size = size;
+		handle->cpu_data[cpu].cfd = -1;
 		if (size > max_size)
 			max_size = size;
 
@@ -3635,17 +3673,23 @@ void tracecmd_close(struct tracecmd_input *handle)
 		/* The tracecmd_peek_data may have cached a record */
 		free_next(handle, cpu);
 		free_page(handle, cpu);
-		if (handle->cpu_data && handle->cpu_data[cpu].kbuf) {
-			kbuffer_free(handle->cpu_data[cpu].kbuf);
-			if (handle->cpu_data[cpu].page_map)
-				free_page_map(handle->cpu_data[cpu].page_map);
-
-			if (handle->cpu_data[cpu].page_cnt)
-				tracecmd_warning("%d pages still allocated on cpu %d%s",
-						 handle->cpu_data[cpu].page_cnt, cpu,
-						 show_records(handle->cpu_data[cpu].pages,
-							      handle->cpu_data[cpu].nr_pages));
-			free(handle->cpu_data[cpu].pages);
+		if (handle->cpu_data) {
+			if (handle->cpu_data[cpu].kbuf) {
+				kbuffer_free(handle->cpu_data[cpu].kbuf);
+				if (handle->cpu_data[cpu].page_map)
+					free_page_map(handle->cpu_data[cpu].page_map);
+
+				if (handle->cpu_data[cpu].page_cnt)
+					tracecmd_warning("%d pages still allocated on cpu %d%s",
+							 handle->cpu_data[cpu].page_cnt, cpu,
+							 show_records(handle->cpu_data[cpu].pages,
+								      handle->cpu_data[cpu].nr_pages));
+				free(handle->cpu_data[cpu].pages);
+			}
+			if (handle->cpu_data[cpu].cfd >= 0) {
+				close(handle->cpu_data[cpu].cfd);
+				unlink(handle->cpu_data[cpu].cfile);
+			}
 		}
 	}
 
tracecmd/trace-read.c +8 -0
index 0cf6e773..d605d05a 100644
@@ -1363,7 +1363,14 @@ struct tracecmd_input *read_trace_header(const char *file, int flags)
 
 static void sig_end(int sig)
 {
+	struct handle_list *handles;
+
 	fprintf(stderr, "trace-cmd: Received SIGINT\n");
+
+	list_for_each_entry(handles, &handle_list, list) {
+		tracecmd_close(handles->handle);
+	}
+
 	exit(0);
 }
 
@@ -1924,6 +1931,7 @@ void trace_report (int argc, char **argv)
 	/* and version overrides uname! */
 	if (show_version)
 		otype = OUTPUT_VERSION_ONLY;
+
 	read_data_info(&handle_list, otype, global, align_ts);
 
 	list_for_each_entry(handles, &handle_list, list) {
Signature · 1 line
2.31.1
#36 lore raw

Re: [PATCH v6 25/45] trace-cmd: Read compressed trace data

Quoted 3 lines · On Mon, 14 Jun 2021 10:50:09 +0300 "Tzvetomir Stoyanov (VMware)"…

When reading a trace.dat file of version 7, uncompress the trace data. The trace data for each CPU is uncompressed in a temporary file, located in /tmp directory with prefix "trace_cpu_data".

With large trace files, this will be an issue. Several systems setup the /tmp directory as a ramfs file system (that is, it is locate in ram, and not backed up on disk). If you have very large trace files, which you would if you are going to bother compressing them, by uncompressing them into /tmp, it could take up all the memory of the machine, or easily fill the /tmp limit.

Simply uncompressing the entire trace data is not an option. The best we can do is to uncompress on a as needed basis. That would require having meta data that is stored to know what pages are compressed.

-- Steve

#37 lore raw

Re: [PATCH v6 25/45] trace-cmd: Read compressed trace data

Quoted 8 lines · Steven Rostedt
Quoted 3 lines · On Mon, 14 Jun 2021 10:50:09 +0300 "Tzvetomir Stoyanov (VMware)"…

When reading a trace.dat file of version 7, uncompress the trace data. The trace data for each CPU is uncompressed in a temporary file, located in /tmp directory with prefix "trace_cpu_data".

Quoted 6 lines

With large trace files, this will be an issue. Several systems setup the /tmp directory as a ramfs file system (that is, it is locate in ram, and not backed up on disk). If you have very large trace files, which you would if you are going to bother compressing them, by uncompressing them into /tmp, it could take up all the memory of the machine, or easily fill the /tmp limit.

There are a few possible approaches for solving that:
- use the same directory where the input trace file is located
- use an environment variable for user specified temp directory for these files
- check if there is enough free space on the FS before uncompressing

Quoted 5 lines

Simply uncompressing the entire trace data is not an option. The best we can do is to uncompress on a as needed basis. That would require having meta data that is stored to know what pages are compressed.

I can modify that logic to compress page by page, as the data is loaded by pages. Or use some of the above approaches ?

Quoted 1 line

-- Steve

Signature · 2 lines
Tzvetomir (Ceco) Stoyanov
VMware Open Source Technology Center
#38 lore raw

Re: [PATCH v6 25/45] trace-cmd: Read compressed trace data

Quoted 9 lines · On Tue, 22 Jun 2021 13:50:44 +0300 Tzvetomir Stoyanov <[email protected]>…
Quoted 6 lines

With large trace files, this will be an issue. Several systems setup the /tmp directory as a ramfs file system (that is, it is locate in ram, and not backed up on disk). If you have very large trace files, which you would if you are going to bother compressing them, by uncompressing them into /tmp, it could take up all the memory of the machine, or easily fill the /tmp limit.

There are a few possible approaches for solving that:
- use the same directory where the input trace file is located

I thought about that, but then decided against it, because there's a reason people compress it. If we have to uncompress it to read it, I can see people saying "why is it compressed in the first place?" When data is compressed to save disk space (which I consider this a case), then the reading has to uncompress it on a as-needed basis.

Quoted 10 lines

- use an environment variable for user specified temp directory for these files
- check if there is enough free space on the FS before uncompressing

Quoted 5 lines

Simply uncompressing the entire trace data is not an option. The best we can do is to uncompress on a as needed basis. That would require having meta data that is stored to know what pages are compressed.

I can modify that logic to compress page by page, as the data is loaded by pages. Or use some of the above approaches ?

Doing it page by page is probably the most logical solution. It will make it easier to manage without needing to create separate temporary files.

I'm guessing we need an index of each page and where they start. We need a way to map the record offset to the page that contains it in such a way that tracecmd_read_at() still works.

We could keep this in the file, or create it from the data. I'm thinking saving this as a section in the file would be good as it would be quicker for loading.

Have a section for each CPU, that maps each page with their compressed offset in the file, and then just consider the page to be page size.

Oh, which reminds me, we need to make sure that we don't use "getpagesize()" to determine the size of the page buffers, because I may be making the buffers more than a single page. It must use the header_page file in the events directory, because it that might change in the future!

Anyway, we can have this:

	buffer_page_size:	4096

/* lets say the compressed data starts at 10,000 just to make this easier to explain. */

	u64 cpu_array[0]	10000 <- page 1 (compress to 100 bytes)
				10100 <- page 2 (compressed to 150 bytes)
				10250 <- page 3
				[...]

But the record->offset should contain the offset of the uncompressed data. That is, if the record is on page 2 at offset 400 (uncompressed) then offset should be:

	record->offset = 14496 (10000 + 4096 + 400)

Which would be calculated as:

	record->offset = cpu_data_start[cpu] + page * buffer_page_size + offset;

This also means that cpu_array[1] has to save its uncompressed start. That is, even though it may start at 20,000 in the trace data file (10,000 more than the cpu_array[0] start). It's uncompressed location needs to account for all the cpu_array[0] pages, such that no two record's offsets will overlap if they are on different CPUs.

	cpu_data_start[0] = 10000 (but has 1000 pages, where 1000 * 4096 = 4,096,000)

But even if cpu_array[1] starts at 20000, it has to account for the uncompressed cpu_array[0] data, thus we have:

	cpu_data_start[1] = 4106000 (4096000 + 10000)

-- Steve

#39 lore raw

PATCH v6 26/45 trace-cmd library: Compress latency trace data

In reply to Tzvetomir Stoyanov (VMware)

When creating latency trace file of version 7, compress the latency trace data.

---
 lib/trace-cmd/trace-output.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/trace-cmd/trace-output.c +1 -1
index 20a646af..017b3d7d 100644
@@ -1661,7 +1661,7 @@ struct tracecmd_output *tracecmd_create_file_latency(const char *output_file, in
 	if (!path)
 		goto out_free;
 
-	copy_file(handle, path);
+	copy_file_compress(handle, path, NULL);
 
 	put_tracing_file(path);
 
Signature · 1 line
2.31.1
#40 lore raw

PATCH v6 27/45 trace-cmd: Read compressed latency trace data

In reply to Tzvetomir Stoyanov (VMware)

When reading trace file version 7 with latency trace data, uncompress the data. A new trace-cmd API is introduced to handle that case:
tracecmd_latency_data_read()

---
 .../include/private/trace-cmd-private.h       |  2 +
 lib/trace-cmd/trace-input.c                   | 42 ++++++++++++++++++-
 tracecmd/trace-read.c                         |  6 +--
 3 files changed, 46 insertions(+), 4 deletions(-)
lib/trace-cmd/include/private/trace-cmd-private.h +2 -0
index 067ba34d..4a3eb1bc 100644
@@ -208,6 +208,8 @@ tracecmd_peek_data_ref(struct tracecmd_input *handle, int cpu)
 	return rec;
 }
 
+int tracecmd_latency_data_read(struct tracecmd_input *handle, void *buf, size_t size);
+
 struct tep_record *
 tracecmd_read_prev(struct tracecmd_input *handle, struct tep_record *record);
 
lib/trace-cmd/trace-input.c +41 -1

73 lines not on this page · raw message

tracecmd/trace-read.c +3 -3
index d605d05a..dfb814b7 100644
@@ -948,13 +948,13 @@ void trace_show_data(struct tracecmd_input *handle, struct tep_record *record)
 	printf("\n");
 }
 
-static void read_rest(void)
+static void read_latency(struct tracecmd_input *handle)
 {
 	char buf[BUFSIZ + 1];
 	int r;
 
 	do {
-		r = read(input_fd, buf, BUFSIZ);
+		r = tracecmd_latency_data_read(handle, buf, BUFSIZ);
 		if (r > 0) {
 			buf[r] = 0;
 			printf("%s", buf);
@@ -1241,7 +1241,7 @@ static void read_data_info(struct list_head *handle_list, enum output_type otype
 		if (ret > 0) {
 			if (multi_inputs)
 				die("latency traces do not work with multiple inputs");
-			read_rest();
+			read_latency(handles->handle);
 			return;
 		}
 
Signature · 1 line
2.31.1
#41 lore raw

PATCH v6 28/45 trace-cmd library: Reuse within the library the function that checks file state.

In reply to Tzvetomir Stoyanov (VMware)

Make the function, that checks if the next file state is valid, global for the tracmd-library, so it can be reused. It is important the same check logic to be used in the whole library.

---
 lib/trace-cmd/include/trace-cmd-local.h |  4 ++
 lib/trace-cmd/trace-input.c             |  5 ++
 lib/trace-cmd/trace-output.c            | 83 ++++++-------------------
 lib/trace-cmd/trace-util.c              | 41 ++++++++++++
 4 files changed, 70 insertions(+), 63 deletions(-)
lib/trace-cmd/include/trace-cmd-local.h +4 -0
index 03d2a9a3..d44b58a9 100644
@@ -39,6 +39,10 @@ void tracecmd_zlib_free(void);
 void tracecmd_compress_init(void);
 void tracecmd_compress_free(void);
 
+bool check_file_state(unsigned long file_version, int current_state, int new_state);
+bool check_in_state(struct tracecmd_input *handle, int new_state);
+bool check_out_state(struct tracecmd_output *handle, int new_state);
+
 int out_uncompress_block(struct tracecmd_output *handle);
 int out_compression_start(struct tracecmd_output *handle);
 int out_compression_end(struct tracecmd_output *handle);
lib/trace-cmd/trace-input.c +5 -0
index 12876838..d3c5ad18 100644
@@ -4436,3 +4436,8 @@ int tracecmd_enable_tsync(struct tracecmd_input *handle, bool enable)
 
 	return 0;
 }
+
+__hidden bool check_in_state(struct tracecmd_input *handle, int new_state)
+{
+	return check_file_state(handle->file_version, handle->file_state, new_state);
+}
lib/trace-cmd/trace-output.c +20 -63

168 lines not on this page · raw message

lib/trace-cmd/trace-util.c +41 -0

44 lines not on this page · raw message

Signature · 1 line
2.31.1
#42 lore raw

PATCH v6 29/45 trace-cmd library: Make tracecmd_copy_headers() to work with output handler

In reply to Tzvetomir Stoyanov (VMware)

When copying headers between two trace files, use output handler context instead of just a fd. Using output handler will allow to use file compression, if needed.
This change fixes "trace-cmd split" command to work with trace files v7.

---
 .../include/private/trace-cmd-private.h       |   3 +-
 lib/trace-cmd/include/trace-cmd-local.h       |   4 +
 lib/trace-cmd/trace-input.c                   | 244 ++++++++++++------
 lib/trace-cmd/trace-output.c                  |  13 +-
 4 files changed, 174 insertions(+), 90 deletions(-)
lib/trace-cmd/include/private/trace-cmd-private.h +2 -1
index 4a3eb1bc..65e80cec 100644
@@ -170,7 +170,8 @@ int tracecmd_get_parsing_failures(struct tracecmd_input *handle);
 int tracecmd_long_size(struct tracecmd_input *handle);
 int tracecmd_page_size(struct tracecmd_input *handle);
 int tracecmd_cpus(struct tracecmd_input *handle);
-int tracecmd_copy_headers(struct tracecmd_input *handle, int fd,
+int tracecmd_copy_headers(struct tracecmd_input *in_handle,
+			  struct tracecmd_output *out_handle,
 			  enum tracecmd_file_states start_state,
 			  enum tracecmd_file_states end_state);
 void tracecmd_set_flag(struct tracecmd_input *handle, int flag);
lib/trace-cmd/include/trace-cmd-local.h +4 -0
index d44b58a9..8fb391fc 100644
@@ -51,7 +51,11 @@ void out_compression_reset(struct tracecmd_output *handle);
 void in_uncompress_reset(struct tracecmd_input *handle);
 int in_uncompress_block(struct tracecmd_input *handle);
 
+void out_set_file_state(struct tracecmd_output *handle, int new_state);
+
 int write_buffers_description_v7(struct tracecmd_output *handle);
 int write_buffers_description_v6(struct tracecmd_output *handle);
 
+long long do_write_check(struct tracecmd_output *handle, const void *data, long long size);
+
 #endif /* _TRACE_CMD_LOCAL_H */
lib/trace-cmd/trace-input.c +160 -84

447 lines not on this page · raw message

lib/trace-cmd/trace-output.c +8 -5
index c41dfb8f..06d0738e 100644
@@ -89,8 +89,8 @@ struct list_event_system {
 	char				*name;
 };
 
-static stsize_t
-do_write_check(struct tracecmd_output *handle, const void *data, tsize_t size)
+__hidden long long
+do_write_check(struct tracecmd_output *handle, const void *data, long long size)
 {
 	if (handle->do_compress)
 		return tracecmd_compress_write(handle->compress, data, size);
@@ -1946,11 +1946,9 @@ struct tracecmd_output *tracecmd_copy(struct tracecmd_input *ihandle, const char
 	if (!handle)
 		return NULL;
 
-	if (tracecmd_copy_headers(ihandle, handle->fd, 0, 0) < 0)
+	if (tracecmd_copy_headers(ihandle, handle, 0, 0) < 0)
 		goto out_free;
 
-	handle->file_state = tracecmd_get_file_state(ihandle);
-
 	/* The file is all ready to have cpu data attached */
 	return handle;
 
@@ -1959,6 +1957,11 @@ out_free:
 	return NULL;
 }
 
+__hidden void out_set_file_state(struct tracecmd_output *handle, int new_state)
+{
+	handle->file_state = new_state;
+}
+
 __hidden bool check_out_state(struct tracecmd_output *handle, int new_state)
 {
 	return check_file_state(handle->file_version, handle->file_state, new_state);
Signature · 1 line
2.31.1
#43 lore raw

PATCH v6 30/45 trace-cmd: Do not use trace file compression with streams

In reply to Tzvetomir Stoyanov (VMware)

When creating a temporary output handler for use with a stream, force it not not use trace file compression. As no data are saved in this file, there is no need to have a compression overhead.

---
 tracecmd/trace-stream.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tracecmd/trace-stream.c +1 -1
index 317de1b2..1ca3a79d 100644
@@ -43,7 +43,7 @@ trace_stream_init(struct buffer_instance *instance, int cpu, int fd, int cpus,
 		tfd = fileno(fp);
 
 		ofd = dup(tfd);
-		trace_output = tracecmd_create_init_fd(ofd, NULL);
+		trace_output = tracecmd_create_init_fd(ofd, "none");
 		if (!trace_output) {
 			fclose(fp);
 			return NULL;
Signature · 1 line
2.31.1
#44 lore raw

PATCH v6 31/45 trace-cmd library: Add new API to get file version of output handler

In reply to Tzvetomir Stoyanov (VMware)

Added an API to get the version of the trace file, associated with given output file handler.
tracecmd_get_out_file_version()

---
 lib/trace-cmd/include/private/trace-cmd-private.h | 1 +
 lib/trace-cmd/trace-output.c                      | 9 +++++++++
 2 files changed, 10 insertions(+)
lib/trace-cmd/include/private/trace-cmd-private.h +1 -0
index 65e80cec..9a08a960 100644
@@ -319,6 +319,7 @@ int tracecmd_append_cpu_data(struct tracecmd_output *handle,
 int tracecmd_append_buffer_cpu_data(struct tracecmd_output *handle,
 				    const char *name, int cpus, char * const *cpu_data_files);
 struct tracecmd_output *tracecmd_get_output_handle_fd(int fd);
+unsigned long tracecmd_get_out_file_version(struct tracecmd_output *handle);
 
 /* --- Reading the Fly Recorder Trace --- */
 
lib/trace-cmd/trace-output.c +9 -0
index 06d0738e..9b83eb76 100644
@@ -1966,3 +1966,12 @@ __hidden bool check_out_state(struct tracecmd_output *handle, int new_state)
 {
 	return check_file_state(handle->file_version, handle->file_state, new_state);
 }
+
+/**
+ * tracecmd_get_out_file_version - return the trace.dat file version
+ * @handle: output handle for the trace.dat file
+ */
+unsigned long tracecmd_get_out_file_version(struct tracecmd_output *handle)
+{
+	return handle->file_version;
+}
Signature · 1 line
2.31.1
#45 lore raw

PATCH v6 32/45 trace-cmd: Add file state parameter to tracecmd_copy

In reply to Tzvetomir Stoyanov (VMware)

The tracecmd_copy() API is used to create output trace handler from given input trace handler and to copy data from it. Currently it is used by trace-cmd split and restore commands, that's why it is hardcoded what data to be copied from input to output handler. Addig desired output handler file state makes the API more generic and allows it to be used in more use cases.

---
 lib/trace-cmd/include/private/trace-cmd-private.h | 4 ++--
 lib/trace-cmd/trace-output.c                      | 8 ++++++--
 tracecmd/trace-restore.c                          | 2 +-
 tracecmd/trace-split.c                            | 2 +-
 4 files changed, 10 insertions(+), 6 deletions(-)
lib/trace-cmd/include/private/trace-cmd-private.h +2 -2
index 9a08a960..2c31be5f 100644
@@ -309,8 +309,8 @@ int tracecmd_write_options(struct tracecmd_output *handle);
 int tracecmd_append_options(struct tracecmd_output *handle);
 void tracecmd_output_close(struct tracecmd_output *handle);
 void tracecmd_output_free(struct tracecmd_output *handle);
-struct tracecmd_output *tracecmd_copy(struct tracecmd_input *ihandle,
-				      const char *file);
+struct tracecmd_output *tracecmd_copy(struct tracecmd_input *ihandle, const char *file,
+				      enum tracecmd_file_states state);
 
 int tracecmd_write_cpu_data(struct tracecmd_output *handle,
 			    int cpus, char * const *cpu_data_files);
lib/trace-cmd/trace-output.c +6 -2
index 9b83eb76..806c833c 100644
@@ -1931,13 +1931,16 @@ struct tracecmd_output *tracecmd_create_init_file_override(const char *output_fi
  * tracecmd_copy - copy the headers of one trace.dat file for another
  * @ihandle: input handle of the trace.dat file to copy
  * @file: the trace.dat file to create
+ * @state: what data will be copied from the source handle
  *
  * Reads the header information and creates a new trace data file
  * with the same characteristics (events and all) and returns
  * tracecmd_output handle to this new file.
  */
-struct tracecmd_output *tracecmd_copy(struct tracecmd_input *ihandle, const char *file)
+struct tracecmd_output *tracecmd_copy(struct tracecmd_input *ihandle, const char *file,
+				      enum tracecmd_file_states state)
 {
+	enum tracecmd_file_states fstate;
 	struct tracecmd_output *handle;
 	const char *compr_name = NULL;
 
@@ -1946,7 +1949,8 @@ struct tracecmd_output *tracecmd_copy(struct tracecmd_input *ihandle, const char
 	if (!handle)
 		return NULL;
 
-	if (tracecmd_copy_headers(ihandle, handle, 0, 0) < 0)
+	fstate = state > TRACECMD_FILE_CMD_LINES ? TRACECMD_FILE_CMD_LINES : state;
+	if (tracecmd_copy_headers(ihandle, handle, 0, fstate) < 0)
 		goto out_free;
 
 	/* The file is all ready to have cpu data attached */
tracecmd/trace-restore.c +1 -1
index 96b4fb5d..7d439fd2 100644
@@ -125,7 +125,7 @@ void trace_restore (int argc, char **argv)
 		if (tracecmd_read_headers(ihandle, TRACECMD_FILE_CMD_LINES) < 0)
 			die("error reading file %s headers", input);
 
-		handle = tracecmd_copy(ihandle, output);
+		handle = tracecmd_copy(ihandle, output, TRACECMD_FILE_CMD_LINES);
 		tracecmd_close(ihandle);
 	} else
 		handle = tracecmd_create_init_file(output, NULL);
tracecmd/trace-split.c +1 -1
index 8366d128..401e1d29 100644
@@ -345,7 +345,7 @@ static double parse_file(struct tracecmd_input *handle,
 	dir = dirname(output);
 	base = basename(output);
 
-	ohandle = tracecmd_copy(handle, output_file);
+	ohandle = tracecmd_copy(handle, output_file, TRACECMD_FILE_CMD_LINES);
 
 	cpus = tracecmd_cpus(handle);
 	cpu_data = malloc(sizeof(*cpu_data) * cpus);
Signature · 1 line
2.31.1
#46 lore raw

PATCH v6 33/45 trace-cmd: Copy CPU count in tracecmd_copy

In reply to Tzvetomir Stoyanov (VMware)

Extend the tracecmd_copy() API to support copying of CPU count from input to output trace hanlder.

---
 lib/trace-cmd/trace-input.c  | 30 ++++++++++++++++++++++++++++++
 lib/trace-cmd/trace-output.c |  2 +-
 2 files changed, 31 insertions(+), 1 deletion(-)
lib/trace-cmd/trace-input.c +30 -0
index 74d1ebf7..289102bf 100644
@@ -4043,6 +4043,23 @@ error:
 	return -1;
 }
 
+static int copy_cpu_count(struct tracecmd_input *in_handle, struct tracecmd_output *out_handle)
+{
+	unsigned int size;
+
+	if (!check_in_state(in_handle, TRACECMD_FILE_CPU_COUNT) ||
+	    !check_out_state(out_handle, TRACECMD_FILE_CPU_COUNT))
+		return -1;
+
+	if (read_copy_size4(in_handle, out_handle, &size) < 0)
+		return -1;
+
+	in_handle->file_state = TRACECMD_FILE_CPU_COUNT;
+	out_set_file_state(out_handle, in_handle->file_state);
+
+	return 0;
+}
+
 /**
  * tracecmd_copy_headers - Copy headers from a tracecmd_input handle to a file descriptor
  * @in_handle: input handle for the trace.dat file to copy from.
@@ -4135,6 +4152,19 @@ int tracecmd_copy_headers(struct tracecmd_input *in_handle,
 			return 0;
 
 		ret = copy_command_lines(in_handle, out_handle);
+		if (ret < 0)
+			goto out;
+
+		/* fallthrough */
+	case TRACECMD_FILE_CPU_COUNT:
+		if (end_state <= in_handle->file_state)
+			return 0;
+
+		ret = copy_cpu_count(in_handle, out_handle);
+		if (ret < 0)
+			goto out;
+
+		/* fallthrough */
 	default:
 		break;
 	}
lib/trace-cmd/trace-output.c +1 -1
index 806c833c..b4bdaeeb 100644
@@ -1949,7 +1949,7 @@ struct tracecmd_output *tracecmd_copy(struct tracecmd_input *ihandle, const char
 	if (!handle)
 		return NULL;
 
-	fstate = state > TRACECMD_FILE_CMD_LINES ? TRACECMD_FILE_CMD_LINES : state;
+	fstate = state > TRACECMD_FILE_CPU_COUNT ? TRACECMD_FILE_CPU_COUNT : state;
 	if (tracecmd_copy_headers(ihandle, handle, 0, fstate) < 0)
 		goto out_free;
 
Signature · 1 line
2.31.1
#47 lore raw

Re: [PATCH v6 33/45] trace-cmd: Copy CPU count in tracecmd_copy

Quoted 2 lines · On Mon, 14 Jun 2021 10:50:17 +0300 "Tzvetomir Stoyanov (VMware)"…

Extend the tracecmd_copy() API to support copying of CPU count from input to output trace hanlder.

typo above.

But there's no rationale for the change in the change log. It's simply saying what is being done, but not why it is being done.

-- Steve

Quoted 4 lines

---

#48 lore raw

PATCH v6 34/45 trace-cmd: Copy buffers description in tracecmd_copy

In reply to Tzvetomir Stoyanov (VMware)

Extend the tracecmd_copy() API to support copying of trace buffers description from input to output trace handler.

---
 .../include/private/trace-cmd-private.h       |  2 +
 lib/trace-cmd/trace-input.c                   | 38 +++++++++++++++++++
 lib/trace-cmd/trace-output.c                  |  3 ++
 3 files changed, 43 insertions(+)
lib/trace-cmd/include/private/trace-cmd-private.h +2 -0
index 2c31be5f..3735586d 100644
@@ -174,6 +174,8 @@ int tracecmd_copy_headers(struct tracecmd_input *in_handle,
 			  struct tracecmd_output *out_handle,
 			  enum tracecmd_file_states start_state,
 			  enum tracecmd_file_states end_state);
+int tracecmd_copy_buffer_descr(struct tracecmd_input *in_handle,
+			       struct tracecmd_output *out_handle);
 void tracecmd_set_flag(struct tracecmd_input *handle, int flag);
 void tracecmd_clear_flag(struct tracecmd_input *handle, int flag);
 unsigned long tracecmd_get_flags(struct tracecmd_input *handle);
lib/trace-cmd/trace-input.c +38 -0
index 289102bf..7d0a41c2 100644
@@ -4173,6 +4173,44 @@ int tracecmd_copy_headers(struct tracecmd_input *in_handle,
 	return ret < 0 ? -1 : 0;
 }
 
+static int skip_buffers(struct tracecmd_input *handle)
+{
+	unsigned long long offset;
+	unsigned int count;
+	char *bname;
+	int i;
+
+	if (read4(handle, &count) < 0)
+		return -1;
+
+	for (i = 0; i < count; i++) {
+		if (read8(handle, &offset) < 0)
+			return -1;
+		bname = read_string(handle);
+		free(bname);
+	}
+
+	handle->file_state = TRACECMD_FILE_BUFERS;
+	return 0;
+}
+
+int tracecmd_copy_buffer_descr(struct tracecmd_input *in_handle,
+			       struct tracecmd_output *out_handle)
+{
+	int i;
+
+	if (in_handle->file_version >= 7) {
+		if (!check_in_state(in_handle, TRACECMD_FILE_BUFERS))
+			return -1;
+		skip_buffers(in_handle);
+	}
+	for (i = 0; i < in_handle->nr_buffers; i++)
+		tracecmd_add_buffer_description(out_handle,
+						in_handle->buffers[i].name, 0);
+
+	return tracecmd_write_buffers_description(out_handle);
+}
+
 /**
  * tracecmd_record_at_buffer_start - return true if record is first on subbuffer
  * @handle: input handle for the trace.dat file
lib/trace-cmd/trace-output.c +3 -0
index b4bdaeeb..ca9024bf 100644
@@ -1952,6 +1952,9 @@ struct tracecmd_output *tracecmd_copy(struct tracecmd_input *ihandle, const char
 	fstate = state > TRACECMD_FILE_CPU_COUNT ? TRACECMD_FILE_CPU_COUNT : state;
 	if (tracecmd_copy_headers(ihandle, handle, 0, fstate) < 0)
 		goto out_free;
+	if (state >= TRACECMD_FILE_BUFERS &&
+	    tracecmd_copy_buffer_descr(ihandle, handle) < 0)
+		goto out_free;
 
 	/* The file is all ready to have cpu data attached */
 	return handle;
Signature · 1 line
2.31.1
#49 lore raw

PATCH v6 35/45 trace-cmd: Copy options in tracecmd_copy

In reply to Tzvetomir Stoyanov (VMware)

Extend the tracecmd_copy() API to support copying of trace options section from input to output trace handler.

---
 .../include/private/trace-cmd-private.h       |  2 +
 lib/trace-cmd/include/trace-cmd-local.h       |  1 +
 lib/trace-cmd/trace-input.c                   | 80 +++++++++++++++++++
 lib/trace-cmd/trace-output.c                  |  8 ++
 4 files changed, 91 insertions(+)
lib/trace-cmd/include/private/trace-cmd-private.h +2 -0
index 3735586d..595ba688 100644
@@ -176,6 +176,8 @@ int tracecmd_copy_headers(struct tracecmd_input *in_handle,
 			  enum tracecmd_file_states end_state);
 int tracecmd_copy_buffer_descr(struct tracecmd_input *in_handle,
 			       struct tracecmd_output *out_handle);
+int tracecmd_copy_options(struct tracecmd_input *in_handle,
+			  struct tracecmd_output *out_handle);
 void tracecmd_set_flag(struct tracecmd_input *handle, int flag);
 void tracecmd_clear_flag(struct tracecmd_input *handle, int flag);
 unsigned long tracecmd_get_flags(struct tracecmd_input *handle);
lib/trace-cmd/include/trace-cmd-local.h +1 -0
index 8fb391fc..2e5682c0 100644
@@ -52,6 +52,7 @@ void in_uncompress_reset(struct tracecmd_input *handle);
 int in_uncompress_block(struct tracecmd_input *handle);
 
 void out_set_file_state(struct tracecmd_output *handle, int new_state);
+void out_save_options_offset(struct tracecmd_output *handle);
 
 int write_buffers_description_v7(struct tracecmd_output *handle);
 int write_buffers_description_v6(struct tracecmd_output *handle);
lib/trace-cmd/trace-input.c +80 -0

86 lines not on this page · raw message

lib/trace-cmd/trace-output.c +8 -0
index ca9024bf..95ca483c 100644
@@ -1955,6 +1955,9 @@ struct tracecmd_output *tracecmd_copy(struct tracecmd_input *ihandle, const char
 	if (state >= TRACECMD_FILE_BUFERS &&
 	    tracecmd_copy_buffer_descr(ihandle, handle) < 0)
 		goto out_free;
+	if (state >= TRACECMD_FILE_OPTIONS &&
+	    tracecmd_copy_options(ihandle, handle) < 0)
+		goto out_free;
 
 	/* The file is all ready to have cpu data attached */
 	return handle;
@@ -1974,6 +1977,11 @@ __hidden bool check_out_state(struct tracecmd_output *handle, int new_state)
 	return check_file_state(handle->file_version, handle->file_state, new_state);
 }
 
+__hidden void out_save_options_offset(struct tracecmd_output *handle)
+{
+	handle->options_start = lseek64(handle->fd, 0, SEEK_CUR);
+}
+
 /**
  * tracecmd_get_out_file_version - return the trace.dat file version
  * @handle: output handle for the trace.dat file
Signature · 1 line
2.31.1
#50 lore raw

PATCH v6 36/45 trace-cmd library: Refactor the logic for writing CPU trace data

In reply to Tzvetomir Stoyanov (VMware)

Refactored the internal library logic for writing trace CPU data in the trace file. The existing logic copies trace data from a temporary file into the trace file. In order to reuse the code, modify it to support copying the trace data not only from temporary file, but from given fd at given offset. These changes will be used in extending the tracecmd_copy API.

---
 lib/trace-cmd/include/trace-cmd-local.h |  13 +++
 lib/trace-cmd/trace-output.c            | 142 +++++++++++++++++-------
 2 files changed, 114 insertions(+), 41 deletions(-)
lib/trace-cmd/include/trace-cmd-local.h +13 -0
index 2e5682c0..970087c7 100644
@@ -53,10 +53,23 @@ int in_uncompress_block(struct tracecmd_input *handle);
 
 void out_set_file_state(struct tracecmd_output *handle, int new_state);
 void out_save_options_offset(struct tracecmd_output *handle);
+unsigned long long out_copy_fd_compress(struct tracecmd_output *handle,
+					int fd, unsigned long long max,
+					unsigned long long *write_size);
+
 
 int write_buffers_description_v7(struct tracecmd_output *handle);
 int write_buffers_description_v6(struct tracecmd_output *handle);
 
 long long do_write_check(struct tracecmd_output *handle, const void *data, long long size);
 
+struct cpu_data_source {
+	int fd;
+	int size;
+	off64_t offset;
+};
+
+int out_write_cpu_data(struct tracecmd_output *handle, int cpus,
+		       struct cpu_data_source *data);
+
 #endif /* _TRACE_CMD_LOCAL_H */
lib/trace-cmd/trace-output.c +101 -41

229 lines not on this page · raw message

Signature · 1 line
2.31.1
#51 lore raw

PATCH v6 37/45 trace-cmd library: Refactor the logic for writing CPU instance trace data

In reply to Tzvetomir Stoyanov (VMware)

Added a helper function, internal to the library, for updating CPU instance offset in the trace file. This allows to reuse the logic inside the library, will be used in the tracecmd_copy API.

---
 lib/trace-cmd/include/trace-cmd-local.h |  2 ++
 lib/trace-cmd/trace-output.c            | 11 +++++++++--
 2 files changed, 11 insertions(+), 2 deletions(-)
lib/trace-cmd/include/trace-cmd-local.h +2 -0
index 970087c7..3cb171ea 100644
@@ -72,4 +72,6 @@ struct cpu_data_source {
 int out_write_cpu_data(struct tracecmd_output *handle, int cpus,
 		       struct cpu_data_source *data);
 
+int out_update_buffer_cpu_offset(struct tracecmd_output *handle, const char *name);
+
 #endif /* _TRACE_CMD_LOCAL_H */
lib/trace-cmd/trace-output.c +9 -2
index e5da4cdb..54134d03 100644
@@ -1853,8 +1853,7 @@ int tracecmd_append_cpu_data(struct tracecmd_output *handle,
 	return tracecmd_write_cpu_data(handle, cpus, cpu_data_files);
 }
 
-int tracecmd_append_buffer_cpu_data(struct tracecmd_output *handle,
-				    const char *name, int cpus, char * const *cpu_data_files)
+__hidden int out_update_buffer_cpu_offset(struct tracecmd_output *handle, const char *name)
 {
 	tsize_t b_offset;
 	tsize_t offset;
@@ -1883,6 +1882,14 @@ int tracecmd_append_buffer_cpu_data(struct tracecmd_output *handle,
 		tracecmd_warning("could not seek to %lld\n", offset);
 		return -1;
 	}
+	return 0;
+}
+
+int tracecmd_append_buffer_cpu_data(struct tracecmd_output *handle,
+				    const char *name, int cpus, char * const *cpu_data_files)
+{
+	if (out_update_buffer_cpu_offset(handle, name))
+		return -1;
 
 	return tracecmd_write_cpu_data(handle, cpus, cpu_data_files);
 }
Signature · 1 line
2.31.1
#52 lore raw

PATCH v6 38/45 trace-cmd: Copy trace data in tracecmd_copy

In reply to Tzvetomir Stoyanov (VMware)

Extend the tracecmd_copy() API to support copying of trace data from input to output trace handler.

---
 .../include/private/trace-cmd-private.h       |   2 +
 lib/trace-cmd/trace-input.c                   | 101 ++++++++++++++++++
 lib/trace-cmd/trace-output.c                  |   3 +
 3 files changed, 106 insertions(+)
lib/trace-cmd/include/private/trace-cmd-private.h +2 -0
index 595ba688..79fef884 100644
@@ -178,6 +178,8 @@ int tracecmd_copy_buffer_descr(struct tracecmd_input *in_handle,
 			       struct tracecmd_output *out_handle);
 int tracecmd_copy_options(struct tracecmd_input *in_handle,
 			  struct tracecmd_output *out_handle);
+int tracecmd_copy_trace_data(struct tracecmd_input *in_handle,
+			     struct tracecmd_output *out_handle);
 void tracecmd_set_flag(struct tracecmd_input *handle, int flag);
 void tracecmd_clear_flag(struct tracecmd_input *handle, int flag);
 unsigned long tracecmd_get_flags(struct tracecmd_input *handle);
lib/trace-cmd/trace-input.c +101 -0

107 lines not on this page · raw message

lib/trace-cmd/trace-output.c +3 -0
index 54134d03..377c5863 100644
@@ -2025,6 +2025,9 @@ struct tracecmd_output *tracecmd_copy(struct tracecmd_input *ihandle, const char
 	if (state >= TRACECMD_FILE_OPTIONS &&
 	    tracecmd_copy_options(ihandle, handle) < 0)
 		goto out_free;
+	if (state >= TRACECMD_FILE_CPU_LATENCY &&
+	    tracecmd_copy_trace_data(ihandle, handle) < 0)
+		goto out_free;
 
 	/* The file is all ready to have cpu data attached */
 	return handle;
Signature · 1 line
2.31.1
#53 lore raw

PATCH v6 39/45 trace-cmd: Add compression parameter to tracecmd_copy

In reply to Tzvetomir Stoyanov (VMware)

Added compression parameter to the tracecmd_copy() API, which allows to select the compression of the created output file.

---
 lib/trace-cmd/include/private/trace-cmd-private.h |  2 +-
 lib/trace-cmd/trace-output.c                      | 11 +++++++----
 tracecmd/trace-restore.c                          |  2 +-
 tracecmd/trace-split.c                            |  2 +-
 4 files changed, 10 insertions(+), 7 deletions(-)
lib/trace-cmd/include/private/trace-cmd-private.h +1 -1
index 79fef884..c8f0ee93 100644
@@ -316,7 +316,7 @@ int tracecmd_append_options(struct tracecmd_output *handle);
 void tracecmd_output_close(struct tracecmd_output *handle);
 void tracecmd_output_free(struct tracecmd_output *handle);
 struct tracecmd_output *tracecmd_copy(struct tracecmd_input *ihandle, const char *file,
-				      enum tracecmd_file_states state);
+				      enum tracecmd_file_states state, const char *compression);
 
 int tracecmd_write_cpu_data(struct tracecmd_output *handle,
 			    int cpus, char * const *cpu_data_files);
lib/trace-cmd/trace-output.c +7 -4
index 377c5863..dae11ed3 100644
@@ -1999,20 +1999,23 @@ struct tracecmd_output *tracecmd_create_init_file_override(const char *output_fi
  * @ihandle: input handle of the trace.dat file to copy
  * @file: the trace.dat file to create
  * @state: what data will be copied from the source handle
+ * @compression: compression of the output file, can be one of:
+ *		 NULL - inherit compression from the input file
+ *		 "any" - compress the output file with the best available algorithm
+ *		 "none" - do not compress the output file
+ *		 algorithm_name - compress the output file with specified algorithm
  *
  * Reads the header information and creates a new trace data file
  * with the same characteristics (events and all) and returns
  * tracecmd_output handle to this new file.
  */
 struct tracecmd_output *tracecmd_copy(struct tracecmd_input *ihandle, const char *file,
-				      enum tracecmd_file_states state)
+				      enum tracecmd_file_states state, const char *compression)
 {
 	enum tracecmd_file_states fstate;
 	struct tracecmd_output *handle;
-	const char *compr_name = NULL;
 
-	tracecmd_get_file_compress_proto(ihandle, &compr_name, NULL);
-	handle = create_file(file, ihandle, NULL, NULL, &all_event_list, compr_name);
+	handle = create_file(file, ihandle, NULL, NULL, &all_event_list, compression);
 	if (!handle)
 		return NULL;
 
tracecmd/trace-restore.c +1 -1
index 7d439fd2..58913fa7 100644
@@ -125,7 +125,7 @@ void trace_restore (int argc, char **argv)
 		if (tracecmd_read_headers(ihandle, TRACECMD_FILE_CMD_LINES) < 0)
 			die("error reading file %s headers", input);
 
-		handle = tracecmd_copy(ihandle, output, TRACECMD_FILE_CMD_LINES);
+		handle = tracecmd_copy(ihandle, output, TRACECMD_FILE_CMD_LINES, NULL);
 		tracecmd_close(ihandle);
 	} else
 		handle = tracecmd_create_init_file(output, NULL);
tracecmd/trace-split.c +1 -1
index 401e1d29..b98e6fc9 100644
@@ -345,7 +345,7 @@ static double parse_file(struct tracecmd_input *handle,
 	dir = dirname(output);
 	base = basename(output);
 
-	ohandle = tracecmd_copy(handle, output_file, TRACECMD_FILE_CMD_LINES);
+	ohandle = tracecmd_copy(handle, output_file, TRACECMD_FILE_CMD_LINES, NULL);
 
 	cpus = tracecmd_cpus(handle);
 	cpu_data = malloc(sizeof(*cpu_data) * cpus);
Signature · 1 line
2.31.1
#54 lore raw

PATCH v6 40/45 trace-cmd: Add new command "trace-cmd convert"

In reply to Tzvetomir Stoyanov (VMware)

A new trace-cmd subcommand is added, used to convert trace files:

trace-cmd convert -i <input file> -o <output file> --compression <out file compression>

The command reads the given input trace file and creates output trace file with given compression. The --compression parameter is optional:
- no --compression parameter: the output file inherits the compression from the input file.
--compression none - the output file is not compressed. --compression any - use the best available compression algorithm to
compress the output file
--compression <algorithm> - use the specified compression algorithm, if
available, to compress the output file.

---
 tracecmd/Makefile              |  1 +
 tracecmd/include/trace-local.h |  2 +
 tracecmd/trace-cmd.c           |  1 +
 tracecmd/trace-convert.c       | 94 ++++++++++++++++++++++++++++++++++
 tracecmd/trace-usage.c         | 12 +++++
 5 files changed, 110 insertions(+)
 create mode 100644 tracecmd/trace-convert.c
tracecmd/Makefile +1 -0
index 80c69bbb..35086b71 100644
@@ -36,6 +36,7 @@ TRACE_CMD_OBJS += trace-usage.o
 TRACE_CMD_OBJS += trace-dump.o
 TRACE_CMD_OBJS += trace-clear.o
 TRACE_CMD_OBJS += trace-vm.o
+TRACE_CMD_OBJS += trace-convert.o
 
 ifeq ($(VSOCK_DEFINED), 1)
 TRACE_CMD_OBJS += trace-agent.o
tracecmd/include/trace-local.h +2 -0
index e9a0aea8..13dab44c 100644
@@ -110,6 +110,8 @@ void trace_usage(int argc, char **argv);
 
 void trace_dump(int argc, char **argv);
 
+void trace_convert(int argc, char **argv);
+
 int trace_record_agent(struct tracecmd_msg_handle *msg_handle,
 		       int cpus, int *fds,
 		       int argc, char **argv, bool use_fifos,
tracecmd/trace-cmd.c +1 -0
index 71c8f6d6..e6274c35 100644
@@ -130,6 +130,7 @@ struct command commands[] = {
 	{"list", trace_list},
 	{"help", trace_usage},
 	{"dump", trace_dump},
+	{"convert", trace_convert},
 	{"-h", trace_usage},
 };
 
tracecmd/trace-convert.c new file +94 -0

94 lines not on this page · raw message

tracecmd/trace-usage.c +12 -0
index c70f9919..d0d12e87 100644
@@ -392,6 +392,18 @@ static struct usage_help usage_help[] = {
 		"          --clock trace clock, saved in the file\n"
 		"          -h, --help show usage information\n"
 	},
+	{
+		"convert",
+		"convert trace file to different version",
+		" %s convert [options]\n"
+		"          -i input file, default is trace.dat\n"
+		"          -o output file, mandatory parameter.\n"
+		"             The output file can be specified also as last argument of the command\n"
+		"          --compression compress the trace output file, one of these strings can be passed:\n"
+		"                            any  - auto select the best available compression algorithm\n"
+		"                            none - do not compress the trace file\n"
+		"                            name - the name of the desired compression algorithms\n"
+		"                        available algorithms can be listed with trace-cmd list -c\n"	},
 	{
 		NULL, NULL, NULL
 	}
Signature · 1 line
2.31.1
#55 lore raw

PATCH v6 41/45 trace-cmd record: Update man page

In reply to Tzvetomir Stoyanov (VMware)

Added documentation of the 'trace-cmd record --compression' parameter in the man page.

---
 Documentation/trace-cmd/trace-cmd-record.1.txt | 10 ++++++++++
 1 file changed, 10 insertions(+)
Documentation/trace-cmd/trace-cmd-record.1.txt +10 -0
index 5d063cc2..c5a7fb75 100644
@@ -367,6 +367,16 @@ OPTIONS
     Traces are extracted by busy waiting, which will hog the CPUs, so only use
     when really needed.
 
+*--compression*::
+    Compression of the trace output file, one of these strings can be passed:
+
+    'any'  - auto select the best available compression algorithm
+
+    'none' - do not compress the trace file
+
+    'name' - the name of the desired compression algorithms. Available algorithms can be listed with
+    trace-cmd list -c
+
 EXAMPLES
 --------
 
Signature · 1 line
2.31.1
#56 lore raw

PATCH v6 42/45 trace-cmd: Add convert man page

In reply to Tzvetomir Stoyanov (VMware)

Added new man page, documenting the "trace-cmd convert" command.

---
 .../trace-cmd/trace-cmd-convert.1.txt         | 60 +++++++++++++++++++
 Documentation/trace-cmd/trace-cmd.1.txt       |  4 +-
 2 files changed, 63 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/trace-cmd/trace-cmd-convert.1.txt
Documentation/trace-cmd/trace-cmd-convert.1.txt new file +60 -0
new file mode 100644
index 00000000..e8d65843
@@ -0,0 +1,60 @@
+TRACE-CMD-CONVERT(1)
+===================
+
+NAME
+----
+trace-cmd-convert - convert trace files
+
+SYNOPSIS
+--------
+*trace-cmd convert* ['OPTIONS'] ['output-file']
+
+DESCRIPTION
+-----------
+The trace-cmd(1) convert command converts trace file. It reads the input file and copies the data
+into an output file. The output file may be in different format, depending on the command line
+arguments.
+
+OPTIONS
+-------
+*-i* 'input-file'::
+    By default, trace-cmd convert will read the file 'trace.dat'. But the *-i*
+    option open up the given 'input-file' instead.
+*-o* 'out-file'::
+    The name of the output file, this parameter is mandatory. Note, the output file may also be
+    specified as the last item on the command line.
+*--compression*::
+    Compression of the trace output file, one of these strings can be passed:
+
+    'any'  - auto select the best available compression algorithm
+
+    'none' - do not compress the trace file
+
+    'name' - the name of the desired compression algorithms. Available algorithms can be listed with
+    trace-cmd list -c
+
+*--help*::
+    Print usage information.
+
+EXAMPLES
+--------
+
+# trace-cmd convert --compression any trace_compress.dat
+
+SEE ALSO
+--------
+trace-cmd(1), trace-cmd.dat(1)
+
+AUTHOR
+------
+*Steven Rostedt* <[email protected]>, author of *trace-cmd*.
+*Tzvetomir Stoyanov* <[email protected]>, author of this man page.
+
+RESOURCES
+---------
+https://git.kernel.org/pub/scm/utils/trace-cmd/trace-cmd.git/
+
+COPYING
+-------
+Copyright \(C) 2021 VMware. Free use of this software is granted under
+the terms of the GNU Public License (GPL).
Documentation/trace-cmd/trace-cmd.1.txt +3 -1
index b330b4fe..27c6e973 100644
@@ -58,6 +58,8 @@ COMMANDS
   check-events - parse format strings for all trace events and return
                  whether all formats are parseable
 
+  convert   - convert trace files
+
 OPTIONS
 -------
 
@@ -70,7 +72,7 @@ SEE ALSO
 --------
 trace-cmd-record(1), trace-cmd-report(1), trace-cmd-hist(1), trace-cmd-start(1),
 trace-cmd-stop(1), trace-cmd-extract(1), trace-cmd-reset(1),
-trace-cmd-restore(1), trace-cmd-stack(1),
+trace-cmd-restore(1), trace-cmd-stack(1), trace-cmd-convert(1),
 trace-cmd-split(1), trace-cmd-list(1), trace-cmd-listen(1),
 trace-cmd.dat(5), trace-cmd-check-events(1) trace-cmd-stat(1)
 
Signature · 1 line
2.31.1
#57 lore raw

PATCH v6 43/45 trace-cmd: Update bash completion

In reply to Tzvetomir Stoyanov (VMware)

Added support for trace-cmd convert and compression parameters in bash completion.

---
 tracecmd/trace-cmd.bash | 37 +++++++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)
tracecmd/trace-cmd.bash +37 -0
index b01c7a07..6639c143 100644
@@ -64,6 +64,13 @@ plugin_options()
     COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
 }
 
+compression_param()
+{
+    local opts=$(trace-cmd list -c | grep -v 'Supported' | cut -d "," -f1)
+    opts+=" any none "
+    COMPREPLY=( $(compgen -W "${opts}") )
+}
+
 __trace_cmd_list_complete()
 {
     local prev=$1
@@ -181,6 +188,9 @@ __trace_cmd_record_complete()
 		cmd_options record "$cur"
 	    fi
 	    ;;
+	--compression)
+	    compression_param
+	    ;;
         *)
 	    # stream start and profile do not show all options
 	    cmd_options record "$cur"
@@ -222,6 +232,29 @@ __trace_cmd_dump_complete()
     esac
 }
 
+__trace_cmd_convert_complete()
+{
+    local prev=$1
+    local cur=$2
+    shift 2
+    local words=("$@")
+
+    case "$prev" in
+	-i)
+	    __show_files
+	    ;;
+	-o)
+	    __show_files
+	    ;;
+	--compression)
+	    compression_param
+	    ;;
+	*)
+	    cmd_options convert "$cur"
+	    ;;
+    esac
+}
+
 __show_command_options()
 {
     local command="$1"
@@ -298,6 +331,10 @@ _trace_cmd_complete()
 	    __trace_cmd_dump_complete "${prev}" "${cur}" ${words[@]}
 	    return 0
 	    ;;
+	convert)
+	    __trace_cmd_convert_complete "${prev}" "${cur}" ${words[@]}
+	    return 0
+	    ;;
         *)
 	    __show_command_options "$w" "${prev}" "${cur}"
             ;;
Signature · 1 line
2.31.1
#58 lore raw

PATCH v6 44/45 trace-cmd list: Update the man page

In reply to Tzvetomir Stoyanov (VMware)

Added documentation of the 'trace-cmd list -c' parameter in the man page.

---
 Documentation/trace-cmd/trace-cmd-list.1.txt | 3 +++
 1 file changed, 3 insertions(+)
Documentation/trace-cmd/trace-cmd-list.1.txt +3 -0
index a5c6b16c..b77e3460 100644
@@ -71,6 +71,9 @@ OPTIONS
     List defined clocks that can be used with trace-cmd record -C.
     The one in brackets ([]) is the active clock.
 
+*-c*::
+    List the available trace file compression algorithms.
+
 SEE ALSO
 --------
 trace-cmd(1), trace-cmd-record(1), trace-cmd-report(1), trace-cmd-start(1),
Signature · 1 line
2.31.1
#59 lore raw

PATCH v6 45/45 trace-cmd: Update trace.dat man page

In reply to Tzvetomir Stoyanov (VMware)

Updated the trace.dat man page with the changes related to file version 7 and compression.

---
 Documentation/trace-cmd/trace-cmd.dat.5.txt | 56 ++++++++++++++++++---
 1 file changed, 50 insertions(+), 6 deletions(-)
Documentation/trace-cmd/trace-cmd.dat.5.txt +50 -6
index 8d285353..e80d460e 100644
@@ -52,6 +52,23 @@ INITIAL FORMAT
   The next 4 bytes are a 32-bit word that defines what the traced
   host machine page size was.
 
+  If the file version is 7 or greater, the compression header is
+  written next:
+     "name version\0"
+  where "name" and "version" are strings, name and version of the
+  compression algorithm used to compress the trace file.
+
+COMPRESSION FORMAT OF THE HEADER SECTIONS
+-----------------------------------------
+  If the file version is 7 or greater, some header sections are compressed
+  with the compression algorithm, specified in the compression header.
+  The format of these compressed sections is:
+     <4 bytes> unsigned int, size of compressed data in the next block.
+     <4 bytes> unsigned int, size of uncompressed data.
+     <data> binary compressed data, with the specified size.
+  These sections must be uncompressed on reading. The described format of
+  the sections refers to the uncomperssed data.
+
 HEADER INFO FORMAT
 ------------------
 
@@ -93,7 +110,8 @@ FTRACE EVENT FORMATS
 
   Directly after the header information comes the information about
   the Ftrace specific events. These are the events used by the Ftrace plugins
-  and are not enabled by the event tracing.
+  and are not enabled by the event tracing. If the file version is 7 or
+  greater, this section is compressed.
 
   The next 4 bytes contain a 32-bit word of the number of Ftrace event
   format files that are stored in the file.
@@ -110,7 +128,8 @@ EVENT FORMATS
 -------------
 
   Directly after the Ftrace formats comes the information about
-  the event layout.
+  the event layout. If the file version is 7 or greater, this section
+  is compressed.
 
   The next 4 bytes are a 32-bit word containing the number of
   event systems that are stored in the file. These are the
@@ -137,7 +156,8 @@ KALLSYMS INFORMATION
 --------------------
 
   Directly after the event formats comes the information of the mapping
-  of function addresses to the function names.
+  of function addresses to the function names. If the file version is 7
+  or greater, this section is compressed.
 
   The next 4 bytes are a 32-bit word containing the size of the
   data holding the function mappings.
@@ -154,6 +174,7 @@ TRACE_PRINTK INFORMATION
   store the format string outside the ring buffer.
   This information can be found in:
   debugfs/tracing/printk_formats
+  If the file version is 7 or greater, this section is compressed.
 
   The next 4 bytes are a 32-bit word containing the size of the
   data holding the printk formats.
@@ -166,7 +187,8 @@ PROCESS INFORMATION
 -------------------
 
   Directly after the trace_printk formats comes the information mapping
-  a PID to a process name.
+  a PID to a process name. If the file version is 7 or greater, this
+  section is compressed.
 
   The next 8 bytes contain a 64-bit word that holds the size of the
   data mapping the PID to a process name.
@@ -193,10 +215,11 @@ REST OF TRACE-CMD HEADER
 
     "flyrecord\0"
 
-  If it is "options  \0" then:
+  If it is "options  \0" then follows a section with trace options.
+  If the file version is 7 or greater, this section is compressed.
 
   The next 2 bytes are a 16-bit word defining the current option.
-  If the the value is zero then there are no more options.
+  If the value is zero then there are no more options.
 
   Otherwise, the next 4 bytes contain a 32-bit word containing the
   option size. If the reader does not know how to handle the option
@@ -206,6 +229,25 @@ REST OF TRACE-CMD HEADER
   The next option will be directly after the previous option, and
   the options ends with a zero in the option type field.
 
+COMPRESSION FORMAT OF THE TRACE DATA
+------------------------------------
+
+  If the file version is 7 or greater, the tarce data is compressed
+  with the compression algorithm, specified in the compression header.
+  The data is compressed in chunks. The size of one compression chunk
+  is defined when the file is written. The format of compressed trace
+  data is:
+     <4 bytes> unsigned int, count of chunks.
+     Follows the compressed chunks of givent count. For each chunk:
+        <4 bytes> unsigned int, size of compressed data in this chunk.
+        <4 bytes> unsigned int, size of uncompressed data.
+        <data> binary compressed data, with the specified size.
+  These chunks must be uncompressed on reading. The described format of
+  trace data refers to the uncomperssed data.
+
+TRACE DATA
+----------
+
   The next 10 bytes after the options are one of the following:
 
   "latency  \0"
@@ -217,6 +259,7 @@ REST OF TRACE-CMD HEADER
   If the value is "latency  \0", then the rest of the file is
   simply ASCII text that was taken from the target's:
   debugfs/tracing/trace
+  If the file version is 7 or greater, the latency data is compressed.
 
   If the value is "flyrecord\0", the following is present:
 
@@ -232,6 +275,7 @@ REST OF TRACE-CMD HEADER
 CPU DATA
 --------
 
+  If the file version is 7 or greater, the CPU data is compressed.
   The CPU data is located in the part of the file that is specified
   in the end of the header. Padding is placed between the header and
   the CPU data, placing the CPU data at a page aligned (target page) position
Signature · 1 line
2.31.1
#60 lore raw

Re: [PATCH v6 45/45] trace-cmd: Update trace.dat man page

Quoted 13 lines · On Mon, 14 Jun 2021 10:50:29 +0300 "Tzvetomir Stoyanov (VMware)"…

Updated the trace.dat man page with the changes related to file version 7 and compression.

---
 Documentation/trace-cmd/trace-cmd.dat.5.txt | 56 ++++++++++++++++++---
 1 file changed, 50 insertions(+), 6 deletions(-)
Documentation/trace-cmd/trace-cmd.dat.5.txt +0 -0
index 8d285353..e80d460e 100644

Documentation/trace-cmd/trace-cmd.dat.5.txt INITIAL FORMAT · line 57

Quoted 6 lines
Documentation/trace-cmd/trace-cmd.dat.5.txt +3 -0
@@ -52,6 +52,23 @@ INITIAL FORMAT
   The next 4 bytes are a 32-bit word that defines what the traced
   host machine page size was.
 
+  If the file version is 7 or greater, the compression header is
+  written next:
+     "name version\0"

I wonder if we should make it: "name\0version\0"

Also, I think "none" is acceptable, where none of the sections are compressed. If we add something special for a version 7 but don't want to compress, we need to support that.

Documentation/trace-cmd/trace-cmd.dat.5.txt INITIAL FORMAT · line 70

Quoted 13 lines
Documentation/trace-cmd/trace-cmd.dat.5.txt +13 -0
+  where "name" and "version" are strings, name and version of the
+  compression algorithm used to compress the trace file.
+
+COMPRESSION FORMAT OF THE HEADER SECTIONS
+-----------------------------------------
+  If the file version is 7 or greater, some header sections are compressed
+  with the compression algorithm, specified in the compression header.
+  The format of these compressed sections is:
+     <4 bytes> unsigned int, size of compressed data in the next block.
+     <4 bytes> unsigned int, size of uncompressed data.
+     <data> binary compressed data, with the specified size.
+  These sections must be uncompressed on reading. The described format of
+  the sections refers to the uncomperssed data.

I think each section should have a flag that states that it is compressed or not. That way we could have options that determine "what" gets compressed, and not have it be all or none.

Documentation/trace-cmd/trace-cmd.dat.5.txt FTRACE EVENT FORMATS · line 114

Quoted 11 lines
Documentation/trace-cmd/trace-cmd.dat.5.txt +3 -1
+
 HEADER INFO FORMAT
 ------------------
 
@@ -93,7 +110,8 @@ FTRACE EVENT FORMATS
 
   Directly after the header information comes the information about
   the Ftrace specific events. These are the events used by the Ftrace plugins
-  and are not enabled by the event tracing.
+  and are not enabled by the event tracing. If the file version is 7 or
+  greater, this section is compressed.

Perhaps add a single byte ahead of each section, where "0" is not compressed, and "1" is compressed?

Quoted 56 lines
Documentation/trace-cmd/trace-cmd.dat.5.txt +10 -5
   The next 4 bytes contain a 32-bit word of the number of Ftrace event
   format files that are stored in the file.
@@ -110,7 +128,8 @@ EVENT FORMATS
 -------------
 
   Directly after the Ftrace formats comes the information about
-  the event layout.
+  the event layout. If the file version is 7 or greater, this section
+  is compressed.
 
   The next 4 bytes are a 32-bit word containing the number of
   event systems that are stored in the file. These are the
@@ -137,7 +156,8 @@ KALLSYMS INFORMATION
 --------------------
 
   Directly after the event formats comes the information of the mapping
-  of function addresses to the function names.
+  of function addresses to the function names. If the file version is 7
+  or greater, this section is compressed.
 
   The next 4 bytes are a 32-bit word containing the size of the
   data holding the function mappings.
@@ -154,6 +174,7 @@ TRACE_PRINTK INFORMATION
   store the format string outside the ring buffer.
   This information can be found in:
   debugfs/tracing/printk_formats
+  If the file version is 7 or greater, this section is compressed.
 
   The next 4 bytes are a 32-bit word containing the size of the
   data holding the printk formats.
@@ -166,7 +187,8 @@ PROCESS INFORMATION
 -------------------
 
   Directly after the trace_printk formats comes the information mapping
-  a PID to a process name.
+  a PID to a process name. If the file version is 7 or greater, this
+  section is compressed.
 
   The next 8 bytes contain a 64-bit word that holds the size of the
   data mapping the PID to a process name.
@@ -193,10 +215,11 @@ REST OF TRACE-CMD HEADER
 
     "flyrecord\0"
 
-  If it is "options  \0" then:
+  If it is "options  \0" then follows a section with trace options.
+  If the file version is 7 or greater, this section is compressed.
 
   The next 2 bytes are a 16-bit word defining the current option.
-  If the the value is zero then there are no more options.
+  If the value is zero then there are no more options.
 
   Otherwise, the next 4 bytes contain a 32-bit word containing the
   option size. If the reader does not know how to handle the option

Documentation/trace-cmd/trace-cmd.dat.5.txt REST OF TRACE-CMD HEADER · line 235

Quoted 7 lines
Documentation/trace-cmd/trace-cmd.dat.5.txt +4 -0
@@ -206,6 +229,25 @@ REST OF TRACE-CMD HEADER
   The next option will be directly after the previous option, and
   the options ends with a zero in the option type field.
 
+COMPRESSION FORMAT OF THE TRACE DATA
+------------------------------------
+
+  If the file version is 7 or greater, the tarce data is compressed

Typo "trace data"

And this is where we definitely need to make it optional. We currently do not have a safe way to read this file. The "uncompress to /tmp" is not a reliable way to do this. And again, people can likely want to have the header compressed but not the data, due to speed in reading.

-- Steve

Quoted 34 lines
Documentation/trace-cmd/trace-cmd.dat.5.txt +17 -0
+  with the compression algorithm, specified in the compression header.
+  The data is compressed in chunks. The size of one compression chunk
+  is defined when the file is written. The format of compressed trace
+  data is:
+     <4 bytes> unsigned int, count of chunks.
+     Follows the compressed chunks of givent count. For each chunk:
+        <4 bytes> unsigned int, size of compressed data in this chunk.
+        <4 bytes> unsigned int, size of uncompressed data.
+        <data> binary compressed data, with the specified size.
+  These chunks must be uncompressed on reading. The described format of
+  trace data refers to the uncomperssed data.
+
+TRACE DATA
+----------
+
   The next 10 bytes after the options are one of the following:
 
   "latency  \0"
@@ -217,6 +259,7 @@ REST OF TRACE-CMD HEADER
   If the value is "latency  \0", then the rest of the file is
   simply ASCII text that was taken from the target's:
   debugfs/tracing/trace
+  If the file version is 7 or greater, the latency data is compressed.
 
   If the value is "flyrecord\0", the following is present:
 
@@ -232,6 +275,7 @@ REST OF TRACE-CMD HEADER
 CPU DATA
 --------
 
+  If the file version is 7 or greater, the CPU data is compressed.
   The CPU data is located in the part of the file that is specified
   in the end of the header. Padding is placed between the header and
   the CPU data, placing the CPU data at a page aligned (target page) position
#61 lore raw

Re: [PATCH v6 45/45] trace-cmd: Update trace.dat man page

Quoted 39 lines · Steven Rostedt
Quoted 13 lines · On Mon, 14 Jun 2021 10:50:29 +0300 "Tzvetomir Stoyanov (VMware)"…

Updated the trace.dat man page with the changes related to file version 7 and compression.

---
 Documentation/trace-cmd/trace-cmd.dat.5.txt | 56 ++++++++++++++++++---
 1 file changed, 50 insertions(+), 6 deletions(-)
Documentation/trace-cmd/trace-cmd.dat.5.txt +0 -0
index 8d285353..e80d460e 100644

Documentation/trace-cmd/trace-cmd.dat.5.txt INITIAL FORMAT · line 57

Quoted 6 lines
Documentation/trace-cmd/trace-cmd.dat.5.txt +3 -0
@@ -52,6 +52,23 @@ INITIAL FORMAT
   The next 4 bytes are a 32-bit word that defines what the traced
   host machine page size was.
 
+  If the file version is 7 or greater, the compression header is
+  written next:
+     "name version\0"

I wonder if we should make it: "name\0version\0"

Also, I think "none" is acceptable, where none of the sections are compressed. If we add something special for a version 7 but don't want to compress, we need to support that.

Quoted 9 lines
Documentation/trace-cmd/trace-cmd.dat.5.txt +9 -0
+  where "name" and "version" are strings, name and version of the
+  compression algorithm used to compress the trace file.
+
+COMPRESSION FORMAT OF THE HEADER SECTIONS
+-----------------------------------------
+  If the file version is 7 or greater, some header sections are compressed
+  with the compression algorithm, specified in the compression header.
+  The format of these compressed sections is:
+     <4 bytes> unsigned int, size of compressed data in the next block.
Quoted 8 lines

Documentation/trace-cmd/trace-cmd.dat.5.txt INITIAL FORMAT · line 70

Quoted 4 lines
Documentation/trace-cmd/trace-cmd.dat.5.txt +4 -0
+     <4 bytes> unsigned int, size of uncompressed data.
+     <data> binary compressed data, with the specified size.
+  These sections must be uncompressed on reading. The described format of
+  the sections refers to the uncomperssed data.

I think each section should have a flag that states that it is compressed or not. That way we could have options that determine "what" gets compressed, and not have it be all or none.

I was thinking the same, but could not find a use case. That means to give control to the user to decide what parts should be compressed. This will complicate the implementation, new trace-cmd parameters should be added. As I couldn't thought of a use case, decided to go with the simpler approach. May be it makes sense only for the trace data, but the metadata should be always compressed if possible.

Quoted 124 lines

Documentation/trace-cmd/trace-cmd.dat.5.txt FTRACE EVENT FORMATS · line 114

Quoted 12 lines
Documentation/trace-cmd/trace-cmd.dat.5.txt +3 -1
+
 HEADER INFO FORMAT
 ------------------
 
@@ -93,7 +110,8 @@ FTRACE EVENT FORMATS
 
   Directly after the header information comes the information about
   the Ftrace specific events. These are the events used by the Ftrace plugins
-  and are not enabled by the event tracing.
+  and are not enabled by the event tracing. If the file version is 7 or
+  greater, this section is compressed.

Perhaps add a single byte ahead of each section, where "0" is not compressed, and "1" is compressed?

Quoted 56 lines
Documentation/trace-cmd/trace-cmd.dat.5.txt +10 -5
   The next 4 bytes contain a 32-bit word of the number of Ftrace event
   format files that are stored in the file.
@@ -110,7 +128,8 @@ EVENT FORMATS
 -------------
 
   Directly after the Ftrace formats comes the information about
-  the event layout.
+  the event layout. If the file version is 7 or greater, this section
+  is compressed.
 
   The next 4 bytes are a 32-bit word containing the number of
   event systems that are stored in the file. These are the
@@ -137,7 +156,8 @@ KALLSYMS INFORMATION
 --------------------
 
   Directly after the event formats comes the information of the mapping
-  of function addresses to the function names.
+  of function addresses to the function names. If the file version is 7
+  or greater, this section is compressed.
 
   The next 4 bytes are a 32-bit word containing the size of the
   data holding the function mappings.
@@ -154,6 +174,7 @@ TRACE_PRINTK INFORMATION
   store the format string outside the ring buffer.
   This information can be found in:
   debugfs/tracing/printk_formats
+  If the file version is 7 or greater, this section is compressed.
 
   The next 4 bytes are a 32-bit word containing the size of the
   data holding the printk formats.
@@ -166,7 +187,8 @@ PROCESS INFORMATION
 -------------------
 
   Directly after the trace_printk formats comes the information mapping
-  a PID to a process name.
+  a PID to a process name. If the file version is 7 or greater, this
+  section is compressed.
 
   The next 8 bytes contain a 64-bit word that holds the size of the
   data mapping the PID to a process name.
@@ -193,10 +215,11 @@ REST OF TRACE-CMD HEADER
 
     "flyrecord\0"
 
-  If it is "options  \0" then:
+  If it is "options  \0" then follows a section with trace options.
+  If the file version is 7 or greater, this section is compressed.
 
   The next 2 bytes are a 16-bit word defining the current option.
-  If the the value is zero then there are no more options.
+  If the value is zero then there are no more options.
 
   Otherwise, the next 4 bytes contain a 32-bit word containing the
   option size. If the reader does not know how to handle the option

Documentation/trace-cmd/trace-cmd.dat.5.txt REST OF TRACE-CMD HEADER · line 235

Quoted 7 lines
Documentation/trace-cmd/trace-cmd.dat.5.txt +4 -0
@@ -206,6 +229,25 @@ REST OF TRACE-CMD HEADER
   The next option will be directly after the previous option, and
   the options ends with a zero in the option type field.
 
+COMPRESSION FORMAT OF THE TRACE DATA
+------------------------------------
+
+  If the file version is 7 or greater, the tarce data is compressed

Typo "trace data"

And this is where we definitely need to make it optional. We currently do not have a safe way to read this file. The "uncompress to /tmp" is not a reliable way to do this. And again, people can likely want to have the header compressed but not the data, due to speed in reading.

-- Steve

Quoted 34 lines
Documentation/trace-cmd/trace-cmd.dat.5.txt +17 -0
+  with the compression algorithm, specified in the compression header.
+  The data is compressed in chunks. The size of one compression chunk
+  is defined when the file is written. The format of compressed trace
+  data is:
+     <4 bytes> unsigned int, count of chunks.
+     Follows the compressed chunks of givent count. For each chunk:
+        <4 bytes> unsigned int, size of compressed data in this chunk.
+        <4 bytes> unsigned int, size of uncompressed data.
+        <data> binary compressed data, with the specified size.
+  These chunks must be uncompressed on reading. The described format of
+  trace data refers to the uncomperssed data.
+
+TRACE DATA
+----------
+
   The next 10 bytes after the options are one of the following:
 
   "latency  \0"
@@ -217,6 +259,7 @@ REST OF TRACE-CMD HEADER
   If the value is "latency  \0", then the rest of the file is
   simply ASCII text that was taken from the target's:
   debugfs/tracing/trace
+  If the file version is 7 or greater, the latency data is compressed.
 
   If the value is "flyrecord\0", the following is present:
 
@@ -232,6 +275,7 @@ REST OF TRACE-CMD HEADER
 CPU DATA
 --------
 
+  If the file version is 7 or greater, the CPU data is compressed.
   The CPU data is located in the part of the file that is specified
   in the end of the header. Padding is placed between the header and
   the CPU data, placing the CPU data at a page aligned (target page) position
Signature · 2 lines
Tzvetomir (Ceco) Stoyanov
VMware Open Source Technology Center
#62 lore raw

Re: [PATCH v6 45/45] trace-cmd: Update trace.dat man page

Quoted 6 lines · On Tue, 22 Jun 2021 14:05:25 +0300 Tzvetomir Stoyanov <[email protected]>…

I was thinking the same, but could not find a use case. That means to give control to the user to decide what parts should be compressed. This will complicate the implementation, new trace-cmd parameters should be added. As I couldn't thought of a use case, decided to go with the simpler approach. May be it makes sense only for the trace data, but the metadata should be always compressed if possible.

I haven't though too hard for a use case, but the problem about going with the simple approach is that if we come up with a use case, we can never implement it.

I like to error on the side of flexibility at the cost of some complexity at the beginning. You may not need that flexibility for years to come, but when it happens, you'll be really glad you did it that way.

When going with the simple approach, and putting yourself in a corner, you are setting yourself up for easy work now, but could potentially have a real hard time, including complete rewrites, or just giving up on features, in the future.

The use case I had was not to allow users to have a lot of options, but it lets us know if we want to bother compressing everything or not. We can perhaps find that compression takes up more time, and doesn't save us anything. If we find that's the case, we can chose not to compress later. By adding a flag that tells trace-cmd if a section is compressed or not, we don't need to worry about that change. We could uncompress a section, and everything will still "just work"!

-- Steve

#63 lore raw
In reply to Tzvetomir Stoyanov (VMware)

Please rebase the series on top of upstream. I pushed up 6 of your patches from this series. And one bug fix that will affect the series as well.

Also, we need to come up with a better option name than "--compression". One that also can handle an optional parameter. Perhaps just have it be "-z". We can have that be default zlib?

Maybe, zlib is enough, and we don't need to have different compression types.

-- Steve