Refactor the logic for writing trace data in the file
cover Refactor the logic for writing trace data in the file
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_info() tracecmd_write_buffer_info()
Changed APIs:
tracecmd_append_buffer_cpu_data()
Removed APIs:
tracecmd_add_buffer_option()
Refactored the internal logic of tracecmd_write_cpu_data() API to be suitable for upcoming trace file format changes and data compression. The size and the offset of the trace data is saved in the file right after the data is written. The old logic calculates the size and offset in advance, but when the trace data is compressed it is hard to use that approach.
This patch-set depends on "[PATCH 00/10] Refactor APIs for creating output handler", should be applied on top of it:
https://lore.kernel.org/linux-trace-devel/[email protected]/
Tzvetomir Stoyanov (VMware) (4):
trace-cmd library: Add internal database for trace buffers
trace-cmd: Use the new trace buffers API
trace-cmd library: Helper logic to update the trace buffer offset trace-cmd library: Refactor the logic for writing trace data in the
file
.../include/private/trace-cmd-private.h | 10 +- lib/trace-cmd/include/trace-cmd-local.h | 19 ++ lib/trace-cmd/trace-output.c | 319 ++++++++++++------ tracecmd/trace-listen.c | 2 +- tracecmd/trace-record.c | 18 +- 5 files changed, 250 insertions(+), 118 deletions(-)
Signature · 1 line
2.31.1
PATCH 1/4 trace-cmd library: Add internal database for trace buffers
The new list with trace buffers information is added in the output handler, and internal APIs to work with that list. Storing various trace buffers information in the handler hides the complexity of this logic inside the library and simplifies possible extensions in the future.
--- .../include/private/trace-cmd-private.h | 2 + lib/trace-cmd/trace-output.c | 58 ++++++++++++++++++- 2 files changed, 58 insertions(+), 2 deletions(-)
lib/trace-cmd/include/private/trace-cmd-private.h +2 -0
@@ -292,6 +292,8 @@ tracecmd_add_option_v(struct tracecmd_output *handle,struct tracecmd_option *tracecmd_add_buffer_option (struct tracecmd_output *handle,const char *name,int cpus); +int tracecmd_add_buffer_info (struct tracecmd_output *handle,const char *name,int cpus); +int tracecmd_write_buffer_info (struct tracecmd_output *handle);int tracecmd_write_cpus (struct tracecmd_output *handle,int cpus);int tracecmd_write_cmdlines (struct tracecmd_output *handle);
lib/trace-cmd/trace-output.c +56 -2
118 lines not on this page · raw message
Signature · 1 line
2.31.1
PATCH 2/4 trace-cmd: Use the new trace buffers API
In reply to Tzvetomir Stoyanov (VMware)When recording trace buffers, use the newly introduced library API for describing the buffers.
--- .../include/private/trace-cmd-private.h | 2 -- lib/trace-cmd/trace-output.c | 7 +++---- tracecmd/trace-record.c | 16 +++++----------- 3 files changed, 8 insertions(+), 17 deletions(-)
lib/trace-cmd/include/private/trace-cmd-private.h +0 -2
@@ -290,8 +290,6 @@ 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_info (struct tracecmd_output *handle,const char *name,int cpus);int tracecmd_write_buffer_info (struct tracecmd_output *handle);
lib/trace-cmd/trace-output.c +3 -4
@@ -1334,9 +1334,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;
@@ -1387,7 +1386,7 @@ int tracecmd_write_buffer_info(struct tracecmd_output *handle)struct tracecmd_buffer *buf;list_for_each_entry (buf, &handle->buffers, list) { - option =tracecmd_add_buffer_option (handle, buf->name, buf->cpus); + option =add_buffer_option (handle, buf->name, buf->cpus);if (!option)return -1 ; buf->option = option;
tracecmd/trace-record.c +5 -11
@@ -4169,7 +4169,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) {
@@ -4197,7 +4196,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,NULL , cpu_count, temp_files);for (i =0 ; i < instance->cpu_count; i++) {
@@ -4482,7 +4481,6 @@ error: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;
@@ -4551,9 +4549,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 ?
@@ -4561,10 +4556,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_info (handle, +tracefs_instance_get_name (instance->tracefs), + cpus);add_buffer_stat (handle, instance); } }
@@ -4599,7 +4593,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
PATCH 3/4 trace-cmd library: Helper logic to update the trace buffer offset
In reply to Tzvetomir Stoyanov (VMware)The new helper functions can be used the update the trace buffer metadata with the real offset within the trace file, where the buffer data are stored.
--- lib/trace-cmd/trace-output.c | 49 ++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+)
lib/trace-cmd/trace-output.c +49 -0
@@ -1395,6 +1395,20 @@ int tracecmd_write_buffer_info(struct tracecmd_output *handle)return 0 ; } +static tsize_t get_buffer_file_offset (struct tracecmd_output *handle,const char *name) +{ +struct tracecmd_buffer *buf; + +list_for_each_entry (buf, &handle->buffers, list) { +if (!strcmp (name, buf->name)) { +if (!buf->option) +break ; +return buf->option->offset; + } + } +return 0 ; +} +int tracecmd_write_cmdlines (struct tracecmd_output *handle) {int ret;
@@ -1487,6 +1501,41 @@ out:return ret; } +static int update_buffer_cpu_offset (struct tracecmd_output *handle, +const char *name,tsize_t offset) +{ +tsize_t b_offset; +tsize_t current; + +if (!name) + name ="" ; + + b_offset =get_buffer_file_offset (handle, name); +if (!b_offset) { +tracecmd_warning ("Cannot find description for buffer %s\n" , name); +return -1 ; + } + + current =lseek64 (handle->fd,0 ,SEEK_CUR ); + +/* Go to the option data, where will write the offest */ +if (lseek64 (handle->fd, b_offset,SEEK_SET ) == (off64_t)-1 ) { +tracecmd_warning ("could not seek to %lld\n" , b_offset); +return -1 ; + } + +if (do_write_check (handle, &offset,8 )) +return -1 ; + +/* Go back to end of file */ +if (lseek64 (handle->fd, current,SEEK_SET ) == (off64_t)-1 ) { +tracecmd_warning ("could not seek to %lld\n" , offset); +return -1 ; + } +return 0 ; +} + +static char *get_clock (struct tracecmd_output *handle) {struct tracefs_instance *inst;
Signature · 1 line
2.31.1
PATCH 4/4 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 upcoming trace file format changes and data compression. The size and the offset of the trace data is saved in the file right after the data is written. The old logic calculates the size and offset in advance, but when the trace data is compressed it is hard to use that approach.
--- .../include/private/trace-cmd-private.h | 6 +- lib/trace-cmd/include/trace-cmd-local.h | 19 ++ lib/trace-cmd/trace-output.c | 207 ++++++++++-------- tracecmd/trace-listen.c | 2 +- tracecmd/trace-record.c | 4 +- 5 files changed, 137 insertions(+), 101 deletions(-)
lib/trace-cmd/include/private/trace-cmd-private.h +2 -4
@@ -303,13 +303,11 @@ struct tracecmd_output *tracecmd_copy(struct tracecmd_input *ihandle, const char *file);int tracecmd_write_cpu_data (struct tracecmd_output *handle, -int cpus,char *const *cpu_data_files); +int cpus,char *const *cpu_data_files,const char *buff_name);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/include/trace-cmd-local.h +19 -0
@@ -31,7 +31,26 @@ void tracecmd_info(const char *fmt, ...);#endif #endif +struct data_file_write { +unsigned long long file_size; +unsigned long long write_size; +/* offset in the trace file, where write_size is stored */ +unsigned long long file_write_size; +unsigned long long data_offset; +/* offset in the trace file, where data_offset is stored */ +unsigned long long file_data_offset; +}; +bool check_file_state (unsigned long file_version,int current_state,int new_state);bool check_out_state (struct tracecmd_output *handle,int new_state); +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,const char *buff_name); +#endif /* _TRACE_CMD_LOCAL_H */
lib/trace-cmd/trace-output.c +113 -94
263 lines not on this page · raw message
tracecmd/trace-listen.c +1 -1
@@ -610,7 +610,7 @@ static int put_together_file(int cpus, int ofd, const char *node,if (ret) goto out; } - ret =tracecmd_write_cpu_data (handle, cpus, temp_files); + ret =tracecmd_write_cpu_data (handle, cpus, temp_files,NULL ); out:tracecmd_output_close (handle);
tracecmd/trace-record.c +2 -2
@@ -4196,7 +4196,7 @@ static void append_buffer(struct tracecmd_output *handle,touch_file (temp_files[i]); } -tracecmd_append_buffer_cpu_data (handle,NULL , +tracecmd_append_buffer_cpu_data (handle,tracefs_instance_get_name (instance->tracefs), cpu_count, temp_files);for (i =0 ; i < instance->cpu_count; i++) {
@@ -4446,7 +4446,7 @@ static void write_guest_file(struct buffer_instance *instance)die ("failed to allocate memory" ); } -if (tracecmd_write_cpu_data (handle, cpu_count, temp_files) <0 ) +if (tracecmd_write_cpu_data (handle, cpu_count, temp_files,NULL ) <0 )die ("failed to write CPU data" );tracecmd_output_close (handle);
Signature · 1 line
2.31.1