summaryrefslogtreecommitdiffstats
path: root/src/spdk/app/trace
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 18:45:59 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 18:45:59 +0000
commit19fcec84d8d7d21e796c7624e521b60d28ee21ed (patch)
tree42d26aa27d1e3f7c0b8bd3fd14e7d7082f5008dc /src/spdk/app/trace
parentInitial commit. (diff)
downloadceph-upstream/16.2.11+ds.tar.xz
ceph-upstream/16.2.11+ds.zip
Adding upstream version 16.2.11+ds.upstream/16.2.11+dsupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r--src/spdk/app/trace/.gitignore1
-rw-r--r--src/spdk/app/trace/Makefile42
-rw-r--r--src/spdk/app/trace/trace.cpp462
-rw-r--r--src/spdk/app/trace_record/.gitignore1
-rw-r--r--src/spdk/app/trace_record/Makefile43
-rw-r--r--src/spdk/app/trace_record/trace_record.c704
6 files changed, 1253 insertions, 0 deletions
diff --git a/src/spdk/app/trace/.gitignore b/src/spdk/app/trace/.gitignore
new file mode 100644
index 000000000..bbb611b88
--- /dev/null
+++ b/src/spdk/app/trace/.gitignore
@@ -0,0 +1 @@
+spdk_trace
diff --git a/src/spdk/app/trace/Makefile b/src/spdk/app/trace/Makefile
new file mode 100644
index 000000000..92df0857f
--- /dev/null
+++ b/src/spdk/app/trace/Makefile
@@ -0,0 +1,42 @@
+#
+# BSD LICENSE
+#
+# Copyright (c) Intel Corporation.
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#
+# * Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+# * Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in
+# the documentation and/or other materials provided with the
+# distribution.
+# * Neither the name of Intel Corporation nor the names of its
+# contributors may be used to endorse or promote products derived
+# from this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+#
+
+SPDK_ROOT_DIR := $(abspath $(CURDIR)/../..)
+include $(SPDK_ROOT_DIR)/mk/spdk.common.mk
+include $(SPDK_ROOT_DIR)/mk/spdk.modules.mk
+
+APP = spdk_trace
+
+CXX_SRCS := trace.cpp
+
+include $(SPDK_ROOT_DIR)/mk/spdk.app_cxx.mk
diff --git a/src/spdk/app/trace/trace.cpp b/src/spdk/app/trace/trace.cpp
new file mode 100644
index 000000000..71350e85c
--- /dev/null
+++ b/src/spdk/app/trace/trace.cpp
@@ -0,0 +1,462 @@
+/*-
+ * BSD LICENSE
+ *
+ * Copyright (c) Intel Corporation.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Intel Corporation nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "spdk/stdinc.h"
+
+#include <map>
+
+extern "C" {
+#include "spdk/trace.h"
+#include "spdk/util.h"
+}
+
+static struct spdk_trace_histories *g_histories;
+static bool g_print_tsc = false;
+
+static void usage(void);
+
+struct entry_key {
+ entry_key(uint16_t _lcore, uint64_t _tsc) : lcore(_lcore), tsc(_tsc) {}
+ uint16_t lcore;
+ uint64_t tsc;
+};
+
+class compare_entry_key
+{
+public:
+ bool operator()(const entry_key &first, const entry_key &second) const
+ {
+ if (first.tsc == second.tsc) {
+ return first.lcore < second.lcore;
+ } else {
+ return first.tsc < second.tsc;
+ }
+ }
+};
+
+typedef std::map<entry_key, spdk_trace_entry *, compare_entry_key> entry_map;
+
+entry_map g_entry_map;
+
+struct object_stats {
+
+ std::map<uint64_t, uint64_t> start;
+ std::map<uint64_t, uint64_t> index;
+ std::map<uint64_t, uint64_t> size;
+ std::map<uint64_t, uint64_t> tpoint_id;
+ uint64_t counter;
+
+ object_stats() : start(), index(), size(), tpoint_id(), counter(0) {}
+};
+
+struct object_stats g_stats[SPDK_TRACE_MAX_OBJECT];
+
+static char *g_exe_name;
+static int g_verbose = 1;
+
+static uint64_t g_tsc_rate;
+static uint64_t g_first_tsc = 0x0;
+
+static float
+get_us_from_tsc(uint64_t tsc, uint64_t tsc_rate)
+{
+ return ((float)tsc) * 1000 * 1000 / tsc_rate;
+}
+
+static void
+print_ptr(const char *arg_string, uint64_t arg)
+{
+ printf("%-7.7s0x%-14jx ", arg_string, arg);
+}
+
+static void
+print_uint64(const char *arg_string, uint64_t arg)
+{
+ /*
+ * Print arg as signed, since -1 is a common value especially
+ * for FLUSH WRITEBUF when writev() returns -1 due to full
+ * socket buffer.
+ */
+ printf("%-7.7s%-16jd ", arg_string, arg);
+}
+
+static void
+print_string(const char *arg_string, uint64_t arg)
+{
+ char *str = (char *)&arg;
+ printf("%-7.7s%.8s ", arg_string, str);
+}
+
+static void
+print_size(uint32_t size)
+{
+ if (size > 0) {
+ printf("size: %6u ", size);
+ } else {
+ printf("%13s", " ");
+ }
+}
+
+static void
+print_object_id(uint8_t type, uint64_t id)
+{
+ printf("id: %c%-15jd ", g_histories->flags.object[type].id_prefix, id);
+}
+
+static void
+print_float(const char *arg_string, float arg)
+{
+ printf("%-7s%-16.3f ", arg_string, arg);
+}
+
+static void
+print_arg(uint8_t arg_type, const char *arg_string, uint64_t arg)
+{
+ if (arg_string[0] == 0) {
+ printf("%24s", "");
+ return;
+ }
+
+ switch (arg_type) {
+ case SPDK_TRACE_ARG_TYPE_PTR:
+ print_ptr(arg_string, arg);
+ break;
+ case SPDK_TRACE_ARG_TYPE_INT:
+ print_uint64(arg_string, arg);
+ break;
+ case SPDK_TRACE_ARG_TYPE_STR:
+ print_string(arg_string, arg);
+ break;
+ }
+}
+
+static void
+print_event(struct spdk_trace_entry *e, uint64_t tsc_rate,
+ uint64_t tsc_offset, uint16_t lcore)
+{
+ struct spdk_trace_tpoint *d;
+ struct object_stats *stats;
+ float us;
+
+ d = &g_histories->flags.tpoint[e->tpoint_id];
+ stats = &g_stats[d->object_type];
+
+ if (d->new_object) {
+ stats->index[e->object_id] = stats->counter++;
+ stats->tpoint_id[e->object_id] = e->tpoint_id;
+ stats->start[e->object_id] = e->tsc;
+ stats->size[e->object_id] = e->size;
+ }
+
+ us = get_us_from_tsc(e->tsc - tsc_offset, tsc_rate);
+
+ printf("%2d: %10.3f ", lcore, us);
+ if (g_print_tsc) {
+ printf("(%9ju) ", e->tsc - tsc_offset);
+ }
+ if (g_histories->flags.owner[d->owner_type].id_prefix) {
+ printf("%c%02d ", g_histories->flags.owner[d->owner_type].id_prefix, e->poller_id);
+ } else {
+ printf("%4s", " ");
+ }
+
+ printf("%-*s ", (int)sizeof(d->name), d->name);
+ print_size(e->size);
+
+ print_arg(d->arg1_type, d->arg1_name, e->arg1);
+ if (d->new_object) {
+ print_object_id(d->object_type, stats->index[e->object_id]);
+ } else if (d->object_type != OBJECT_NONE) {
+ if (stats->start.find(e->object_id) != stats->start.end()) {
+ us = get_us_from_tsc(e->tsc - stats->start[e->object_id],
+ tsc_rate);
+ print_object_id(d->object_type, stats->index[e->object_id]);
+ print_float("time:", us);
+ } else {
+ printf("id: N/A");
+ }
+ } else if (e->object_id != 0) {
+ print_arg(SPDK_TRACE_ARG_TYPE_PTR, "object: ", e->object_id);
+ }
+ printf("\n");
+}
+
+static void
+process_event(struct spdk_trace_entry *e, uint64_t tsc_rate,
+ uint64_t tsc_offset, uint16_t lcore)
+{
+ if (g_verbose) {
+ print_event(e, tsc_rate, tsc_offset, lcore);
+ }
+}
+
+static int
+populate_events(struct spdk_trace_history *history, int num_entries)
+{
+ int i, num_entries_filled;
+ struct spdk_trace_entry *e;
+ int first, last, lcore;
+
+ lcore = history->lcore;
+
+ e = history->entries;
+
+ num_entries_filled = num_entries;
+ while (e[num_entries_filled - 1].tsc == 0) {
+ num_entries_filled--;
+ }
+
+ if (num_entries == num_entries_filled) {
+ first = last = 0;
+ for (i = 1; i < num_entries; i++) {
+ if (e[i].tsc < e[first].tsc) {
+ first = i;
+ }
+ if (e[i].tsc > e[last].tsc) {
+ last = i;
+ }
+ }
+ } else {
+ first = 0;
+ last = num_entries_filled - 1;
+ }
+
+ /*
+ * We keep track of the highest first TSC out of all reactors.
+ * We will ignore any events that occured before this TSC on any
+ * other reactors. This will ensure we only print data for the
+ * subset of time where we have data across all reactors.
+ */
+ if (e[first].tsc > g_first_tsc) {
+ g_first_tsc = e[first].tsc;
+ }
+
+ i = first;
+ while (1) {
+ g_entry_map[entry_key(lcore, e[i].tsc)] = &e[i];
+ if (i == last) {
+ break;
+ }
+ i++;
+ if (i == num_entries_filled) {
+ i = 0;
+ }
+ }
+
+ return (0);
+}
+
+static void usage(void)
+{
+ fprintf(stderr, "usage:\n");
+ fprintf(stderr, " %s <option> <lcore#>\n", g_exe_name);
+ fprintf(stderr, " option = '-q' to disable verbose mode\n");
+ fprintf(stderr, " '-c' to display single lcore history\n");
+ fprintf(stderr, " '-t' to display TSC offset for each event\n");
+ fprintf(stderr, " '-s' to specify spdk_trace shm name for a\n");
+ fprintf(stderr, " currently running process\n");
+ fprintf(stderr, " '-i' to specify the shared memory ID\n");
+ fprintf(stderr, " '-p' to specify the trace PID\n");
+ fprintf(stderr, " (If -s is specified, then one of\n");
+ fprintf(stderr, " -i or -p must be specified)\n");
+ fprintf(stderr, " '-f' to specify a tracepoint file name\n");
+ fprintf(stderr, " (-s and -f are mutually exclusive)\n");
+}
+
+int main(int argc, char **argv)
+{
+ void *history_ptr;
+ struct spdk_trace_history *history;
+ int fd, i, rc;
+ int lcore = SPDK_TRACE_MAX_LCORE;
+ uint64_t tsc_offset;
+ const char *app_name = NULL;
+ const char *file_name = NULL;
+ int op;
+ char shm_name[64];
+ int shm_id = -1, shm_pid = -1;
+ uint64_t trace_histories_size;
+ struct stat _stat;
+
+ g_exe_name = argv[0];
+ while ((op = getopt(argc, argv, "c:f:i:p:qs:t")) != -1) {
+ switch (op) {
+ case 'c':
+ lcore = atoi(optarg);
+ if (lcore > SPDK_TRACE_MAX_LCORE) {
+ fprintf(stderr, "Selected lcore: %d "
+ "exceeds maximum %d\n", lcore,
+ SPDK_TRACE_MAX_LCORE);
+ exit(1);
+ }
+ break;
+ case 'i':
+ shm_id = atoi(optarg);
+ break;
+ case 'p':
+ shm_pid = atoi(optarg);
+ break;
+ case 'q':
+ g_verbose = 0;
+ break;
+ case 's':
+ app_name = optarg;
+ break;
+ case 'f':
+ file_name = optarg;
+ break;
+ case 't':
+ g_print_tsc = true;
+ break;
+ default:
+ usage();
+ exit(1);
+ }
+ }
+
+ if (file_name != NULL && app_name != NULL) {
+ fprintf(stderr, "-f and -s are mutually exclusive\n");
+ usage();
+ exit(1);
+ }
+
+ if (file_name == NULL && app_name == NULL) {
+ fprintf(stderr, "One of -f and -s must be specified\n");
+ usage();
+ exit(1);
+ }
+
+ if (file_name) {
+ fd = open(file_name, O_RDONLY);
+ } else {
+ if (shm_id >= 0) {
+ snprintf(shm_name, sizeof(shm_name), "/%s_trace.%d", app_name, shm_id);
+ } else {
+ snprintf(shm_name, sizeof(shm_name), "/%s_trace.pid%d", app_name, shm_pid);
+ }
+ fd = shm_open(shm_name, O_RDONLY, 0600);
+ file_name = shm_name;
+ }
+ if (fd < 0) {
+ fprintf(stderr, "Could not open %s.\n", file_name);
+ usage();
+ exit(-1);
+ }
+
+ rc = fstat(fd, &_stat);
+ if (rc < 0) {
+ fprintf(stderr, "Could not get size of %s.\n", file_name);
+ usage();
+ exit(-1);
+ }
+ if ((size_t)_stat.st_size < sizeof(*g_histories)) {
+ fprintf(stderr, "%s is not a valid trace file\n", file_name);
+ usage();
+ exit(-1);
+ }
+
+ /* Map the header of trace file */
+ history_ptr = mmap(NULL, sizeof(*g_histories), PROT_READ, MAP_SHARED, fd, 0);
+ if (history_ptr == MAP_FAILED) {
+ fprintf(stderr, "Could not mmap %s.\n", file_name);
+ usage();
+ exit(-1);
+ }
+
+ g_histories = (struct spdk_trace_histories *)history_ptr;
+
+ g_tsc_rate = g_histories->flags.tsc_rate;
+ if (g_tsc_rate == 0) {
+ fprintf(stderr, "Invalid tsc_rate %ju\n", g_tsc_rate);
+ usage();
+ exit(-1);
+ }
+
+ if (g_verbose) {
+ printf("TSC Rate: %ju\n", g_tsc_rate);
+ }
+
+ /* Remap the entire trace file */
+ trace_histories_size = spdk_get_trace_histories_size(g_histories);
+ munmap(history_ptr, sizeof(*g_histories));
+ if ((size_t)_stat.st_size < trace_histories_size) {
+ fprintf(stderr, "%s is not a valid trace file\n", file_name);
+ usage();
+ exit(-1);
+ }
+ history_ptr = mmap(NULL, trace_histories_size, PROT_READ, MAP_SHARED, fd, 0);
+ if (history_ptr == MAP_FAILED) {
+ fprintf(stderr, "Could not mmap %s.\n", file_name);
+ usage();
+ exit(-1);
+ }
+
+ g_histories = (struct spdk_trace_histories *)history_ptr;
+
+ if (lcore == SPDK_TRACE_MAX_LCORE) {
+ for (i = 0; i < SPDK_TRACE_MAX_LCORE; i++) {
+ history = spdk_get_per_lcore_history(g_histories, i);
+ if (history->num_entries == 0 || history->entries[0].tsc == 0) {
+ continue;
+ }
+
+ if (g_verbose && history->num_entries) {
+ printf("Trace Size of lcore (%d): %ju\n", i, history->num_entries);
+ }
+
+ populate_events(history, history->num_entries);
+ }
+ } else {
+ history = spdk_get_per_lcore_history(g_histories, lcore);
+ if (history->num_entries > 0 && history->entries[0].tsc != 0) {
+ if (g_verbose && history->num_entries) {
+ printf("Trace Size of lcore (%d): %ju\n", lcore, history->num_entries);
+ }
+
+ populate_events(history, history->num_entries);
+ }
+ }
+
+ tsc_offset = g_first_tsc;
+ for (entry_map::iterator it = g_entry_map.begin(); it != g_entry_map.end(); it++) {
+ if (it->first.tsc < g_first_tsc) {
+ continue;
+ }
+ process_event(it->second, g_tsc_rate, tsc_offset, it->first.lcore);
+ }
+
+ munmap(history_ptr, trace_histories_size);
+ close(fd);
+
+ return (0);
+}
diff --git a/src/spdk/app/trace_record/.gitignore b/src/spdk/app/trace_record/.gitignore
new file mode 100644
index 000000000..a0fd6b1e0
--- /dev/null
+++ b/src/spdk/app/trace_record/.gitignore
@@ -0,0 +1 @@
+spdk_trace_record
diff --git a/src/spdk/app/trace_record/Makefile b/src/spdk/app/trace_record/Makefile
new file mode 100644
index 000000000..a198b0e72
--- /dev/null
+++ b/src/spdk/app/trace_record/Makefile
@@ -0,0 +1,43 @@
+#
+# BSD LICENSE
+#
+# Copyright (c) Intel Corporation.
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#
+# * Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+# * Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in
+# the documentation and/or other materials provided with the
+# distribution.
+# * Neither the name of Intel Corporation nor the names of its
+# contributors may be used to endorse or promote products derived
+# from this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+#
+
+SPDK_ROOT_DIR := $(abspath $(CURDIR)/../..)
+include $(SPDK_ROOT_DIR)/mk/spdk.common.mk
+
+SPDK_LIB_LIST = util log
+
+APP = spdk_trace_record
+
+C_SRCS := trace_record.c
+
+include $(SPDK_ROOT_DIR)/mk/spdk.app.mk
diff --git a/src/spdk/app/trace_record/trace_record.c b/src/spdk/app/trace_record/trace_record.c
new file mode 100644
index 000000000..ac815b020
--- /dev/null
+++ b/src/spdk/app/trace_record/trace_record.c
@@ -0,0 +1,704 @@
+/*-
+ * BSD LICENSE
+ *
+ * Copyright (c) Intel Corporation.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Intel Corporation nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "spdk/stdinc.h"
+
+#include "spdk/env.h"
+#include "spdk/string.h"
+#include "spdk/trace.h"
+#include "spdk/util.h"
+#include "spdk/barrier.h"
+
+#define TRACE_FILE_COPY_SIZE (32 * 1024)
+#define TRACE_PATH_MAX 2048
+
+static char *g_exe_name;
+static int g_verbose = 1;
+static uint64_t g_tsc_rate;
+static uint64_t g_utsc_rate;
+static bool g_shutdown = false;
+static uint64_t g_histories_size;
+
+struct lcore_trace_record_ctx {
+ char lcore_file[TRACE_PATH_MAX];
+ int fd;
+ struct spdk_trace_history *in_history;
+ struct spdk_trace_history *out_history;
+
+ /* Recorded next entry index in record */
+ uint64_t rec_next_entry;
+
+ /* Record tsc for report */
+ uint64_t first_entry_tsc;
+ uint64_t last_entry_tsc;
+
+ /* Total number of entries in lcore trace file */
+ uint64_t num_entries;
+};
+
+struct aggr_trace_record_ctx {
+ const char *out_file;
+ int out_fd;
+ int shm_fd;
+ struct lcore_trace_record_ctx lcore_ports[SPDK_TRACE_MAX_LCORE];
+ struct spdk_trace_histories *trace_histories;
+};
+
+static int
+input_trace_file_mmap(struct aggr_trace_record_ctx *ctx, const char *shm_name)
+{
+ void *history_ptr;
+ int i;
+
+ ctx->shm_fd = shm_open(shm_name, O_RDONLY, 0);
+ if (ctx->shm_fd < 0) {
+ fprintf(stderr, "Could not open %s.\n", shm_name);
+ return -1;
+ }
+
+ /* Map the header of trace file */
+ history_ptr = mmap(NULL, sizeof(struct spdk_trace_histories), PROT_READ, MAP_SHARED, ctx->shm_fd,
+ 0);
+ if (history_ptr == MAP_FAILED) {
+ fprintf(stderr, "Could not mmap shm %s.\n", shm_name);
+ close(ctx->shm_fd);
+ return -1;
+ }
+
+ ctx->trace_histories = (struct spdk_trace_histories *)history_ptr;
+
+ g_tsc_rate = ctx->trace_histories->flags.tsc_rate;
+ g_utsc_rate = g_tsc_rate / 1000;
+ if (g_tsc_rate == 0) {
+ fprintf(stderr, "Invalid tsc_rate %ju\n", g_tsc_rate);
+ munmap(history_ptr, sizeof(struct spdk_trace_histories));
+ close(ctx->shm_fd);
+ return -1;
+ }
+
+ if (g_verbose) {
+ printf("TSC Rate: %ju\n", g_tsc_rate);
+ }
+
+ /* Remap the entire trace file */
+ g_histories_size = spdk_get_trace_histories_size(ctx->trace_histories);
+ munmap(history_ptr, sizeof(struct spdk_trace_histories));
+ history_ptr = mmap(NULL, g_histories_size, PROT_READ, MAP_SHARED, ctx->shm_fd, 0);
+ if (history_ptr == MAP_FAILED) {
+ fprintf(stderr, "Could not remmap shm %s.\n", shm_name);
+ close(ctx->shm_fd);
+ return -1;
+ }
+
+ ctx->trace_histories = (struct spdk_trace_histories *)history_ptr;
+ for (i = 0; i < SPDK_TRACE_MAX_LCORE; i++) {
+ ctx->lcore_ports[i].in_history = spdk_get_per_lcore_history(ctx->trace_histories, i);
+
+ if (g_verbose) {
+ printf("Number of trace entries for lcore (%d): %ju\n", i,
+ ctx->lcore_ports[i].in_history->num_entries);
+ }
+ }
+
+ return 0;
+}
+
+static int
+output_trace_files_prepare(struct aggr_trace_record_ctx *ctx, const char *aggr_path)
+{
+ int flags = O_CREAT | O_EXCL | O_RDWR;
+ struct lcore_trace_record_ctx *port_ctx;
+ int name_len;
+ int i, rc;
+
+ /* Assign file names for related trace files */
+ ctx->out_file = aggr_path;
+ for (i = 0; i < SPDK_TRACE_MAX_LCORE; i++) {
+ port_ctx = &ctx->lcore_ports[i];
+
+ /* Get the length of trace file name for each lcore with format "%s-%d" */
+ name_len = snprintf(port_ctx->lcore_file, TRACE_PATH_MAX, "%s-%d", ctx->out_file, i);
+ if (name_len >= TRACE_PATH_MAX) {
+ fprintf(stderr, "Length of file path (%s) exceeds limitation for lcore file.\n",
+ aggr_path);
+ goto err;
+ }
+ }
+
+ /* If output trace file already exists, try to unlink it together with its temporary files */
+ if (access(ctx->out_file, F_OK) == 0) {
+ rc = unlink(ctx->out_file);
+ if (rc) {
+ goto err;
+ }
+
+ for (i = 0; i < SPDK_TRACE_MAX_LCORE; i++) {
+ port_ctx = &ctx->lcore_ports[i];
+ if (access(port_ctx->lcore_file, F_OK) == 0) {
+ rc = unlink(port_ctx->lcore_file);
+ if (rc) {
+ goto err;
+ }
+ }
+ }
+
+ }
+
+ for (i = 0; i < SPDK_TRACE_MAX_LCORE; i++) {
+ port_ctx = &ctx->lcore_ports[i];
+
+ port_ctx->fd = open(port_ctx->lcore_file, flags, 0600);
+ if (port_ctx->fd < 0) {
+ fprintf(stderr, "Could not open lcore file %s.\n", port_ctx->lcore_file);
+ goto err;
+ }
+
+ if (g_verbose) {
+ printf("Create tmp lcore trace file %s for lcore %d\n", port_ctx->lcore_file, i);
+ }
+
+ port_ctx->out_history = calloc(1, sizeof(struct spdk_trace_history));
+ if (port_ctx->out_history == NULL) {
+ fprintf(stderr, "Failed to allocate memory for out_history.\n");
+ goto err;
+ }
+ }
+
+ return 0;
+
+err:
+ for (i = 0; i < SPDK_TRACE_MAX_LCORE; i++) {
+ port_ctx = &ctx->lcore_ports[i];
+ free(port_ctx->out_history);
+
+ if (port_ctx->fd > 0) {
+ close(port_ctx->fd);
+ }
+ }
+
+ return -1;
+}
+
+static void
+output_trace_files_finish(struct aggr_trace_record_ctx *ctx)
+{
+ struct lcore_trace_record_ctx *port_ctx;
+ int i;
+
+ for (i = 0; i < SPDK_TRACE_MAX_LCORE; i++) {
+ port_ctx = &ctx->lcore_ports[i];
+
+ free(port_ctx->out_history);
+ close(port_ctx->fd);
+ unlink(port_ctx->lcore_file);
+
+ if (g_verbose) {
+ printf("Remove tmp lcore trace file %s for lcore %d\n", port_ctx->lcore_file, i);
+ }
+ }
+}
+
+static int
+cont_write(int fildes, const void *buf, size_t nbyte)
+{
+ int rc;
+ int _nbyte = nbyte;
+
+ while (_nbyte) {
+ rc = write(fildes, buf, _nbyte);
+ if (rc < 0) {
+ if (errno != EINTR) {
+ return -1;
+ }
+
+ continue;
+ }
+
+ _nbyte -= rc;
+ }
+
+ return nbyte;
+}
+
+static int
+cont_read(int fildes, void *buf, size_t nbyte)
+{
+ int rc;
+ int _nbyte = nbyte;
+
+ while (_nbyte) {
+ rc = read(fildes, buf, _nbyte);
+ if (rc == 0) {
+ return nbyte - _nbyte;
+ } else if (rc < 0) {
+ if (errno != EINTR) {
+ return -1;
+ }
+
+ continue;
+ }
+
+ _nbyte -= rc;
+ }
+
+ return nbyte;
+}
+
+static int
+lcore_trace_last_entry_idx(struct spdk_trace_history *in_history, int cir_next_idx)
+{
+ int last_idx;
+
+ if (cir_next_idx == 0) {
+ last_idx = in_history->num_entries - 1;
+ } else {
+ last_idx = cir_next_idx - 1;
+ }
+
+ return last_idx;
+}
+
+static int
+circular_buffer_padding_backward(int fd, struct spdk_trace_history *in_history,
+ int cir_start, int cir_end)
+{
+ int rc;
+
+ if (cir_end <= cir_start) {
+ fprintf(stderr, "Wrong using of circular_buffer_padding_back\n");
+ return -1;
+ }
+
+ rc = cont_write(fd, &in_history->entries[cir_start],
+ sizeof(struct spdk_trace_entry) * (cir_end - cir_start));
+ if (rc < 0) {
+ fprintf(stderr, "Failed to append entries into lcore file\n");
+ return rc;
+ }
+
+ return 0;
+}
+
+static int
+circular_buffer_padding_across(int fd, struct spdk_trace_history *in_history,
+ int cir_start, int cir_end)
+{
+ int rc;
+ int num_entries = in_history->num_entries;
+
+ if (cir_end > cir_start) {
+ fprintf(stderr, "Wrong using of circular_buffer_padding_across\n");
+ return -1;
+ }
+
+ rc = cont_write(fd, &in_history->entries[cir_start],
+ sizeof(struct spdk_trace_entry) * (num_entries - cir_start));
+ if (rc < 0) {
+ fprintf(stderr, "Failed to append entries into lcore file backward\n");
+ return rc;
+ }
+
+ if (cir_end == 0) {
+ return 0;
+ }
+
+ rc = cont_write(fd, &in_history->entries[0], sizeof(struct spdk_trace_entry) * cir_end);
+ if (rc < 0) {
+ fprintf(stderr, "Failed to append entries into lcore file forward\n");
+ return rc;
+ }
+
+ return 0;
+}
+
+static int
+circular_buffer_padding_all(int fd, struct spdk_trace_history *in_history,
+ int cir_end)
+{
+ return circular_buffer_padding_across(fd, in_history, cir_end, cir_end);
+}
+
+static int
+lcore_trace_record(struct lcore_trace_record_ctx *lcore_port)
+{
+ struct spdk_trace_history *in_history = lcore_port->in_history;
+ uint64_t rec_next_entry = lcore_port->rec_next_entry;
+ uint64_t rec_num_entries = lcore_port->num_entries;
+ int fd = lcore_port->fd;
+ uint64_t shm_next_entry;
+ uint64_t num_cir_entries;
+ uint64_t shm_cir_next;
+ uint64_t rec_cir_next;
+ int rc;
+ int last_idx;
+
+ shm_next_entry = in_history->next_entry;
+
+ /* Ensure all entries of spdk_trace_history are latest to next_entry */
+ spdk_smp_rmb();
+
+ if (shm_next_entry == rec_next_entry) {
+ /* There is no update */
+ return 0;
+ } else if (shm_next_entry < rec_next_entry) {
+ /* Error branch */
+ fprintf(stderr, "Trace porting error in lcore %d, trace rollback occurs.\n", in_history->lcore);
+ fprintf(stderr, "shm_next_entry is %ju, record_next_entry is %ju.\n", shm_next_entry,
+ rec_next_entry);
+ return -1;
+ }
+
+ num_cir_entries = in_history->num_entries;
+ shm_cir_next = shm_next_entry & (num_cir_entries - 1);
+
+ /* Record first entry's tsc and corresponding entries when recording first time. */
+ if (lcore_port->first_entry_tsc == 0) {
+ if (shm_next_entry < num_cir_entries) {
+ /* Updates haven't been across circular buffer yet.
+ * The first entry in shared memory is the eldest one.
+ */
+ lcore_port->first_entry_tsc = in_history->entries[0].tsc;
+
+ lcore_port->num_entries += shm_cir_next;
+ rc = circular_buffer_padding_backward(fd, in_history, 0, shm_cir_next);
+ } else {
+ /* Updates have already been across circular buffer.
+ * The eldest entry in shared memory is pointed by shm_cir_next.
+ */
+ lcore_port->first_entry_tsc = in_history->entries[shm_cir_next].tsc;
+
+ lcore_port->num_entries += num_cir_entries;
+ rc = circular_buffer_padding_all(fd, in_history, shm_cir_next);
+ }
+
+ goto out;
+ }
+
+ if (shm_next_entry - rec_next_entry > num_cir_entries) {
+ /* There must be missed updates */
+ fprintf(stderr, "Trace-record missed %ju trace entries\n",
+ shm_next_entry - rec_next_entry - num_cir_entries);
+
+ lcore_port->num_entries += num_cir_entries;
+ rc = circular_buffer_padding_all(fd, in_history, shm_cir_next);
+ } else if (shm_next_entry - rec_next_entry == num_cir_entries) {
+ /* All circular buffer is updated */
+ lcore_port->num_entries += num_cir_entries;
+ rc = circular_buffer_padding_all(fd, in_history, shm_cir_next);
+ } else {
+ /* Part of circular buffer is updated */
+ rec_cir_next = rec_next_entry & (num_cir_entries - 1);
+
+ if (shm_cir_next > rec_cir_next) {
+ /* Updates are not across circular buffer */
+ lcore_port->num_entries += shm_cir_next - rec_cir_next;
+ rc = circular_buffer_padding_backward(fd, in_history, rec_cir_next, shm_cir_next);
+ } else {
+ /* Updates are across circular buffer */
+ lcore_port->num_entries += num_cir_entries - rec_cir_next + shm_cir_next;
+ rc = circular_buffer_padding_across(fd, in_history, rec_cir_next, shm_cir_next);
+ }
+ }
+
+out:
+ if (rc) {
+ return rc;
+ }
+
+ if (g_verbose) {
+ printf("Append %ju trace_entry for lcore %d\n", lcore_port->num_entries - rec_num_entries,
+ in_history->lcore);
+ }
+
+ /* Update tpoint_count info */
+ memcpy(lcore_port->out_history, lcore_port->in_history, sizeof(struct spdk_trace_history));
+
+ /* Update last_entry_tsc to align with appended entries */
+ last_idx = lcore_trace_last_entry_idx(in_history, shm_cir_next);
+ lcore_port->last_entry_tsc = in_history->entries[last_idx].tsc;
+ lcore_port->rec_next_entry = shm_next_entry;
+
+ return rc;
+}
+
+static int
+trace_files_aggregate(struct aggr_trace_record_ctx *ctx)
+{
+ int flags = O_CREAT | O_EXCL | O_RDWR;
+ struct lcore_trace_record_ctx *lcore_port;
+ char copy_buff[TRACE_FILE_COPY_SIZE];
+ uint64_t lcore_offsets[SPDK_TRACE_MAX_LCORE + 1];
+ int rc, i;
+ ssize_t len = 0;
+ uint64_t len_sum;
+
+ ctx->out_fd = open(ctx->out_file, flags, 0600);
+ if (ctx->out_fd < 0) {
+ fprintf(stderr, "Could not open aggregation file %s.\n", ctx->out_file);
+ return -1;
+ }
+
+ if (g_verbose) {
+ printf("Create trace file %s for output\n", ctx->out_file);
+ }
+
+ /* Write flags of histories into head of converged trace file, except num_entriess */
+ rc = cont_write(ctx->out_fd, ctx->trace_histories,
+ sizeof(struct spdk_trace_histories) - sizeof(lcore_offsets));
+ if (rc < 0) {
+ fprintf(stderr, "Failed to write trace header into trace file\n");
+ goto out;
+ }
+
+ /* Update and append lcore offsets converged trace file */
+ lcore_offsets[0] = sizeof(struct spdk_trace_flags);
+ for (i = 1; i < (int)SPDK_COUNTOF(lcore_offsets); i++) {
+ lcore_offsets[i] = spdk_get_trace_history_size(ctx->lcore_ports[i - 1].num_entries) +
+ lcore_offsets[i - 1];
+ }
+
+ rc = cont_write(ctx->out_fd, lcore_offsets, sizeof(lcore_offsets));
+ if (rc < 0) {
+ fprintf(stderr, "Failed to write lcore offsets into trace file\n");
+ goto out;
+ }
+
+ /* Append each lcore trace file into converged trace file */
+ for (i = 0; i < SPDK_TRACE_MAX_LCORE; i++) {
+ lcore_port = &ctx->lcore_ports[i];
+
+ lcore_port->out_history->num_entries = lcore_port->num_entries;
+ rc = cont_write(ctx->out_fd, lcore_port->out_history, sizeof(struct spdk_trace_history));
+ if (rc < 0) {
+ fprintf(stderr, "Failed to write lcore trace header into trace file\n");
+ goto out;
+ }
+
+ /* Move file offset to the start of trace_entries */
+ rc = lseek(lcore_port->fd, 0, SEEK_SET);
+ if (rc != 0) {
+ fprintf(stderr, "Failed to lseek lcore trace file\n");
+ goto out;
+ }
+
+ len_sum = 0;
+ while ((len = cont_read(lcore_port->fd, copy_buff, TRACE_FILE_COPY_SIZE)) > 0) {
+ len_sum += len;
+ rc = cont_write(ctx->out_fd, copy_buff, len);
+ if (rc != len) {
+ fprintf(stderr, "Failed to write lcore trace entries into trace file\n");
+ goto out;
+ }
+ }
+
+ if (len_sum != lcore_port->num_entries * sizeof(struct spdk_trace_entry)) {
+ fprintf(stderr, "Len of lcore trace file doesn't match number of entries for lcore\n");
+ }
+ }
+
+ printf("All lcores trace entries are aggregated into trace file %s\n", ctx->out_file);
+
+out:
+ close(ctx->out_fd);
+
+ return rc;
+}
+
+static void
+__shutdown_signal(int signo)
+{
+ g_shutdown = true;
+}
+
+static int
+setup_exit_signal_handler(void)
+{
+ struct sigaction sigact;
+ int rc;
+
+ memset(&sigact, 0, sizeof(sigact));
+ sigemptyset(&sigact.sa_mask);
+ /* Install the same handler for SIGINT and SIGTERM */
+ sigact.sa_handler = __shutdown_signal;
+
+ rc = sigaction(SIGINT, &sigact, NULL);
+ if (rc < 0) {
+ fprintf(stderr, "sigaction(SIGINT) failed\n");
+
+ return rc;
+ }
+
+ rc = sigaction(SIGTERM, &sigact, NULL);
+ if (rc < 0) {
+ fprintf(stderr, "sigaction(SIGTERM) failed\n");
+ }
+
+ return rc;
+}
+
+static void usage(void)
+{
+ printf("\n%s is used to record all SPDK generated trace entries\n", g_exe_name);
+ printf("from SPDK trace shared-memory to specified file.\n\n");
+ printf("usage:\n");
+ printf(" %s <option>\n", g_exe_name);
+ printf(" option = '-q' to disable verbose mode\n");
+ printf(" '-s' to specify spdk_trace shm name for a\n");
+ printf(" currently running process\n");
+ printf(" '-i' to specify the shared memory ID\n");
+ printf(" '-p' to specify the trace PID\n");
+ printf(" (one of -i or -p must be specified)\n");
+ printf(" '-f' to specify output trace file name\n");
+ printf(" '-h' to print usage information\n");
+}
+
+int main(int argc, char **argv)
+{
+ const char *app_name = NULL;
+ const char *file_name = NULL;
+ int op;
+ char shm_name[64];
+ int shm_id = -1, shm_pid = -1;
+ int rc = 0;
+ int i;
+ struct aggr_trace_record_ctx ctx = {};
+ struct lcore_trace_record_ctx *lcore_port;
+
+ g_exe_name = argv[0];
+ while ((op = getopt(argc, argv, "f:i:p:qs:h")) != -1) {
+ switch (op) {
+ case 'i':
+ shm_id = spdk_strtol(optarg, 10);
+ break;
+ case 'p':
+ shm_pid = spdk_strtol(optarg, 10);
+ break;
+ case 'q':
+ g_verbose = 0;
+ break;
+ case 's':
+ app_name = optarg;
+ break;
+ case 'f':
+ file_name = optarg;
+ break;
+ case 'h':
+ default:
+ usage();
+ exit(1);
+ }
+ }
+
+ if (file_name == NULL) {
+ fprintf(stderr, "-f must be specified\n");
+ usage();
+ exit(1);
+ }
+
+ if (app_name == NULL) {
+ fprintf(stderr, "-s must be specified\n");
+ usage();
+ exit(1);
+ }
+
+ if (shm_id == -1 && shm_pid == -1) {
+ fprintf(stderr, "-i or -p must be specified\n");
+ usage();
+ exit(1);
+ }
+
+ if (shm_id >= 0) {
+ snprintf(shm_name, sizeof(shm_name), "/%s_trace.%d", app_name, shm_id);
+ } else {
+ snprintf(shm_name, sizeof(shm_name), "/%s_trace.pid%d", app_name, shm_pid);
+ }
+
+ rc = setup_exit_signal_handler();
+ if (rc) {
+ exit(1);
+ }
+
+ rc = input_trace_file_mmap(&ctx, shm_name);
+ if (rc) {
+ exit(1);
+ }
+
+ rc = output_trace_files_prepare(&ctx, file_name);
+ if (rc) {
+ exit(1);
+ }
+
+ printf("Start to poll trace shm file %s\n", shm_name);
+ while (!g_shutdown && rc == 0) {
+ for (i = 0; i < SPDK_TRACE_MAX_LCORE; i++) {
+ lcore_port = &ctx.lcore_ports[i];
+
+ rc = lcore_trace_record(lcore_port);
+ if (rc) {
+ break;
+ }
+ }
+ }
+
+ if (rc) {
+ exit(1);
+ }
+
+ printf("Start to aggregate lcore trace files\n");
+ rc = trace_files_aggregate(&ctx);
+ if (rc) {
+ exit(1);
+ }
+
+ /* Summary report */
+ printf("TSC Rate: %ju\n", g_tsc_rate);
+ for (i = 0; i < SPDK_TRACE_MAX_LCORE; i++) {
+ lcore_port = &ctx.lcore_ports[i];
+
+ if (lcore_port->num_entries == 0) {
+ continue;
+ }
+
+ printf("Port %ju trace entries for lcore (%d) in %ju usec\n",
+ lcore_port->num_entries, i,
+ (lcore_port->last_entry_tsc - lcore_port->first_entry_tsc) / g_utsc_rate);
+
+ }
+
+ munmap(ctx.trace_histories, g_histories_size);
+ close(ctx.shm_fd);
+
+ output_trace_files_finish(&ctx);
+
+ return 0;
+}