From: "Tzvetomir Stoyanov (VMware)" <tz.stoyanov@gmail.com>
To: rostedt@goodmis.org
Cc: linux-trace-devel@vger.kernel.org
Subject: [PATCH 6/8] trace-cmd library: New API to copy trace data between trace files
Date: Mon, 13 Sep 2021 15:47:52 +0300 [thread overview]
Message-ID: <20210913124754.3679916-7-tz.stoyanov@gmail.com> (raw)
In-Reply-To: <20210913124754.3679916-1-tz.stoyanov@gmail.com>
A new library API is introduced, to copy trace data between trace files:
tracecmd_copy_trace_data()
Signed-off-by: Tzvetomir Stoyanov (VMware) <tz.stoyanov@gmail.com>
---
.../include/private/trace-cmd-private.h | 2 +
lib/trace-cmd/include/trace-cmd-local.h | 6 +
lib/trace-cmd/trace-input.c | 166 ++++++++++++++++++
lib/trace-cmd/trace-output.c | 38 ++++
4 files changed, 212 insertions(+)
diff --git a/lib/trace-cmd/include/private/trace-cmd-private.h b/lib/trace-cmd/include/private/trace-cmd-private.h
index e68f75b8..2c373030 100644
--- a/lib/trace-cmd/include/private/trace-cmd-private.h
+++ b/lib/trace-cmd/include/private/trace-cmd-private.h
@@ -193,6 +193,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);
diff --git a/lib/trace-cmd/include/trace-cmd-local.h b/lib/trace-cmd/include/trace-cmd-local.h
index 706ba250..6c4132f2 100644
--- a/lib/trace-cmd/include/trace-cmd-local.h
+++ b/lib/trace-cmd/include/trace-cmd-local.h
@@ -64,6 +64,11 @@ int out_update_section_header(struct tracecmd_output *handle, unsigned long long
long long do_write_check(struct tracecmd_output *handle, const void *data, long long size);
+struct tracecmd_option *
+out_add_buffer_option_v7(struct tracecmd_output *handle, const char *name,
+ unsigned short id, unsigned long long data_offset,
+ int cpus, struct data_file_write *cpu_data);
+
struct cpu_data_source {
int fd;
int size;
@@ -72,6 +77,7 @@ struct cpu_data_source {
int out_write_cpu_data(struct tracecmd_output *handle, int cpus,
struct cpu_data_source *data, const char *buff_name);
+int out_write_emty_cpu_data(struct tracecmd_output *handle, int cpus);
off64_t msg_lseek(struct tracecmd_msg_handle *msg_handle, off_t offset, int whence);
unsigned long long get_last_option_offset(struct tracecmd_input *handle);
diff --git a/lib/trace-cmd/trace-input.c b/lib/trace-cmd/trace-input.c
index 2e5e9bcc..7a0102fa 100644
--- a/lib/trace-cmd/trace-input.c
+++ b/lib/trace-cmd/trace-input.c
@@ -5051,6 +5051,172 @@ int tracecmd_copy_options(struct tracecmd_input *in_handle,
return 0;
}
+static int copy_trace_latency(struct tracecmd_input *in_handle,
+ struct tracecmd_output *out_handle, const char *buf_name)
+{
+ unsigned long long wsize;
+ unsigned long long offset;
+ int fd;
+
+ if (tracecmd_get_out_file_version(out_handle) < FILE_VERSION_SECTIONS &&
+ do_write_check(out_handle, "latency ", 10))
+ return -1;
+
+ offset = tracecmd_get_out_file_offset(out_handle);
+
+ if (tracecmd_get_out_file_version(out_handle) >= FILE_VERSION_SECTIONS &&
+ !out_add_buffer_option_v7(out_handle, buf_name, TRACECMD_OPTION_BUFFER_TEXT, offset, 0, NULL))
+ return -1;
+
+ offset = out_write_section_header(out_handle, TRACECMD_OPTION_BUFFER_TEXT,
+ "buffer latency", TRACECMD_SEC_FL_COMPRESS, false);
+
+ if (in_handle->latz.fd >= 0)
+ fd = in_handle->latz.fd;
+ else
+ fd = in_handle->fd;
+
+ if (!out_copy_fd_compress(out_handle, fd, 0, &wsize))
+ return -1;
+ if (out_update_section_header(out_handle, offset))
+ return -1;
+
+ out_set_file_state(out_handle, TRACECMD_FILE_CPU_LATENCY);
+ return 0;
+}
+
+static int copy_trace_flyrecord_data(struct tracecmd_input *in_handle,
+ struct tracecmd_output *out_handle, const char *buff_name)
+{
+ struct cpu_data_source *data;
+ int total_size = 0;
+ int ret;
+ int i;
+
+ data = calloc(in_handle->cpus, sizeof(struct cpu_data_source));
+ if (!data)
+ return -1;
+ for (i = 0; i < in_handle->cpus; i++) {
+ data[i].size = in_handle->cpu_data[i].file_size;
+ total_size += data[i].size;
+ if (in_handle->cpu_data[i].compress.fd >= 0) {
+ data[i].fd = in_handle->cpu_data[i].compress.fd;
+ data[i].offset = 0;
+ } else {
+ data[i].fd = in_handle->fd;
+ data[i].offset = in_handle->cpu_data[i].file_offset;
+ }
+ }
+ if (total_size || tracecmd_get_out_file_version(out_handle) < FILE_VERSION_SECTIONS)
+ ret = out_write_cpu_data(out_handle, in_handle->cpus, data, buff_name);
+ else
+ ret = 0;
+ free(data);
+
+ return ret;
+}
+
+static int copy_flyrecord_buffer(struct tracecmd_input *in_handle,
+ struct tracecmd_output *out_handle, int index)
+{
+ struct tracecmd_input *instance;
+ const char *name;
+ int ret;
+
+ name = tracecmd_buffer_instance_name(in_handle, index);
+ if (!name)
+ return -1;
+
+ instance = tracecmd_buffer_instance_handle(in_handle, index);
+ if (!instance)
+ return -1;
+ if (!tracecmd_get_quiet(out_handle) && *name)
+ fprintf(stderr, "\nBuffer: %s\n\n", name);
+
+ if (in_handle->buffers[index].latency)
+ ret = copy_trace_latency(in_handle, out_handle, name);
+ else
+ ret = copy_trace_flyrecord_data(instance, out_handle, name);
+ tracecmd_close(instance);
+
+ return ret;
+}
+
+static int copy_trace_data_from_v6(struct tracecmd_input *in_handle,
+ struct tracecmd_output *out_handle)
+{
+ char buf[10];
+ int ret;
+ int i;
+
+ if (do_read_check(in_handle, buf, 10))
+ return -1;
+
+ if (strncmp(buf, "latency", 7) == 0)
+ in_handle->file_state = TRACECMD_FILE_CPU_LATENCY;
+ else if (strncmp(buf, "flyrecord", 9) == 0)
+ in_handle->file_state = TRACECMD_FILE_CPU_FLYRECORD;
+
+ tracecmd_init_data(in_handle);
+
+ if (in_handle->file_state == TRACECMD_FILE_CPU_LATENCY)
+ return copy_trace_latency(in_handle, out_handle, "");
+
+ /* top instance */
+ ret = copy_trace_flyrecord_data(in_handle, out_handle, "");
+ if (ret)
+ return ret;
+
+ for (i = 0; i < in_handle->nr_buffers; i++)
+ copy_flyrecord_buffer(in_handle, out_handle, i);
+ return 0;
+}
+
+static int copy_trace_data_from_v7(struct tracecmd_input *in_handle,
+ struct tracecmd_output *out_handle)
+{
+ int ret;
+ int i;
+
+ /* Force using temporary files for trace data decompression */
+ in_handle->read_zpage = false;
+ tracecmd_init_data(in_handle);
+
+ if (tracecmd_get_out_file_version(out_handle) < FILE_VERSION_SECTIONS) {
+ /* copy top buffer in v6 files */
+ if (in_handle->top_buffer.latency)
+ ret = copy_trace_latency(in_handle, out_handle, in_handle->top_buffer.name);
+ else if (in_handle->top_buffer.cpus)
+ ret = copy_trace_flyrecord_data(in_handle, out_handle,
+ in_handle->top_buffer.name);
+ else
+ ret = out_write_emty_cpu_data(out_handle, in_handle->max_cpu);
+ if (ret)
+ return ret;
+ }
+
+ for (i = 0; i < in_handle->nr_buffers; i++)
+ copy_flyrecord_buffer(in_handle, out_handle, i);
+
+ return 0;
+}
+
+__hidden int tracecmd_copy_trace_data(struct tracecmd_input *in_handle,
+ struct tracecmd_output *out_handle)
+{
+ int ret;
+
+ if (!check_in_state(in_handle, TRACECMD_FILE_CPU_FLYRECORD) ||
+ !check_out_state(out_handle, TRACECMD_FILE_CPU_FLYRECORD))
+ return -1;
+ tracecmd_set_out_clock(out_handle, in_handle->trace_clock);
+ if (in_handle->file_version < FILE_VERSION_SECTIONS)
+ ret = copy_trace_data_from_v6(in_handle, out_handle);
+ else
+ ret = copy_trace_data_from_v7(in_handle, out_handle);
+ return ret;
+}
+
/**
* tracecmd_record_at_buffer_start - return true if record is first on subbuffer
* @handle: input handle for the trace.dat file
diff --git a/lib/trace-cmd/trace-output.c b/lib/trace-cmd/trace-output.c
index 8021ffb6..53bd0209 100644
--- a/lib/trace-cmd/trace-output.c
+++ b/lib/trace-cmd/trace-output.c
@@ -2165,6 +2165,44 @@ static int update_buffer_cpu_offset_v6(struct tracecmd_output *handle,
return 0;
}
+__hidden int out_write_emty_cpu_data(struct tracecmd_output *handle, int cpus)
+{
+ unsigned long long zero = 0;
+ char *clock;
+ int ret;
+ int i;
+
+ if (HAS_SECTIONS(handle))
+ return 0;
+
+ ret = handle->file_state == TRACECMD_FILE_CPU_FLYRECORD ? 0 :
+ check_file_state(handle->file_version,
+ handle->file_state,
+ TRACECMD_FILE_CPU_FLYRECORD);
+ if (ret < 0) {
+ tracecmd_warning("Cannot write trace data into the file, unexpected state 0x%X",
+ handle->file_state);
+ return ret;
+ }
+
+ if (do_write_check(handle, "flyrecord", 10))
+ return -1;
+
+ for (i = 0; i < cpus; i++) {
+ /* Write 0 for trace data offset and size */
+ if (do_write_check(handle, &zero, 8))
+ return -1;
+ if (do_write_check(handle, &zero, 8))
+ return -1;
+ }
+ clock = get_clock(handle);
+ if (clock && save_clock(handle, clock))
+ return -1;
+ handle->file_state = TRACECMD_FILE_CPU_FLYRECORD;
+ return 0;
+}
+
+
__hidden int out_write_cpu_data(struct tracecmd_output *handle,
int cpus, struct cpu_data_source *data, const char *buff_name)
{
--
2.31.1
next prev parent reply other threads:[~2021-09-13 12:48 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-09-13 12:47 [PATCH 0/8] trace-cmd convert Tzvetomir Stoyanov (VMware)
2021-09-13 12:47 ` [PATCH 1/8] trace-cmd library: Use output handler when copying data from input file Tzvetomir Stoyanov (VMware)
2021-09-13 12:47 ` [PATCH 2/8] trace-cmd library: Handle version 7 files when copying headers between files Tzvetomir Stoyanov (VMware)
2021-09-13 12:47 ` [PATCH 3/8] trace-cmd library: Copy CPU count between trace files Tzvetomir Stoyanov (VMware)
2021-09-13 12:47 ` [PATCH 4/8] trace-cmd library: New API to copy buffer description " Tzvetomir Stoyanov (VMware)
2021-09-13 12:47 ` [PATCH 5/8] trace-cmd library: New API to copy options " Tzvetomir Stoyanov (VMware)
2021-09-13 12:47 ` Tzvetomir Stoyanov (VMware) [this message]
2021-09-13 12:47 ` [PATCH 7/8] trace-cmd library: Extend tracecmd_copy() API Tzvetomir Stoyanov (VMware)
2021-09-13 12:47 ` [PATCH 8/8] trace-cmd: Add new subcommand "convert" Tzvetomir Stoyanov (VMware)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20210913124754.3679916-7-tz.stoyanov@gmail.com \
--to=tz.stoyanov@gmail.com \
--cc=linux-trace-devel@vger.kernel.org \
--cc=rostedt@goodmis.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).