2022-06-03 19:15:11 +00:00
|
|
|
/* SPDX-License-Identifier: BSD-3-Clause
|
2022-11-01 20:26:26 +00:00
|
|
|
* Copyright (C) 2016 Intel Corporation.
|
2016-05-10 23:17:51 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*/
|
|
|
|
|
2017-05-02 18:18:25 +00:00
|
|
|
#include "spdk/stdinc.h"
|
|
|
|
|
2016-08-18 19:52:48 +00:00
|
|
|
#include "spdk/env.h"
|
2017-08-11 23:27:04 +00:00
|
|
|
#include "spdk/string.h"
|
2016-05-10 23:17:51 +00:00
|
|
|
#include "spdk/trace.h"
|
2018-11-15 17:07:01 +00:00
|
|
|
#include "spdk/util.h"
|
2018-11-15 03:01:51 +00:00
|
|
|
#include "spdk/barrier.h"
|
2020-04-18 12:12:43 +00:00
|
|
|
#include "spdk/log.h"
|
2022-08-12 05:56:58 +00:00
|
|
|
#include "spdk/cpuset.h"
|
2022-10-20 20:38:46 +00:00
|
|
|
#include "spdk/likely.h"
|
2022-11-14 02:47:03 +00:00
|
|
|
#include "trace_internal.h"
|
2016-05-10 23:17:51 +00:00
|
|
|
|
2018-03-09 17:08:57 +00:00
|
|
|
static int g_trace_fd = -1;
|
2016-05-10 23:17:51 +00:00
|
|
|
static char g_shm_name[64];
|
|
|
|
|
2018-08-31 21:02:11 +00:00
|
|
|
struct spdk_trace_histories *g_trace_histories;
|
2016-05-10 23:17:51 +00:00
|
|
|
|
2021-06-14 12:04:01 +00:00
|
|
|
static inline struct spdk_trace_entry *
|
|
|
|
get_trace_entry(struct spdk_trace_history *history, uint64_t offset)
|
|
|
|
{
|
|
|
|
return &history->entries[offset & (history->num_entries - 1)];
|
|
|
|
}
|
|
|
|
|
2016-05-10 23:17:51 +00:00
|
|
|
void
|
2018-08-31 20:34:33 +00:00
|
|
|
_spdk_trace_record(uint64_t tsc, uint16_t tpoint_id, uint16_t poller_id, uint32_t size,
|
2021-05-17 13:10:39 +00:00
|
|
|
uint64_t object_id, int num_args, ...)
|
2016-05-10 23:17:51 +00:00
|
|
|
{
|
|
|
|
struct spdk_trace_history *lcore_history;
|
|
|
|
struct spdk_trace_entry *next_entry;
|
2021-06-16 12:14:50 +00:00
|
|
|
struct spdk_trace_entry_buffer *buffer;
|
2021-05-17 13:10:39 +00:00
|
|
|
struct spdk_trace_tpoint *tpoint;
|
2021-06-16 11:27:49 +00:00
|
|
|
struct spdk_trace_argument *argument;
|
2021-06-16 12:14:50 +00:00
|
|
|
unsigned lcore, i, offset, num_entries, arglen, argoff, curlen;
|
2021-06-14 12:04:01 +00:00
|
|
|
uint64_t intval;
|
2021-06-16 12:14:50 +00:00
|
|
|
void *argval;
|
2021-05-17 13:10:39 +00:00
|
|
|
va_list vl;
|
2016-05-10 23:17:51 +00:00
|
|
|
|
2017-12-11 22:47:00 +00:00
|
|
|
lcore = spdk_env_get_current_core();
|
2022-10-20 20:38:46 +00:00
|
|
|
if (spdk_unlikely(lcore >= SPDK_TRACE_MAX_LCORE)) {
|
2016-07-22 00:11:15 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-15 17:07:01 +00:00
|
|
|
lcore_history = spdk_get_per_lcore_history(g_trace_histories, lcore);
|
2022-10-20 20:38:46 +00:00
|
|
|
|
2018-08-31 20:34:33 +00:00
|
|
|
if (tsc == 0) {
|
|
|
|
tsc = spdk_get_ticks();
|
|
|
|
}
|
2016-05-10 23:17:51 +00:00
|
|
|
|
|
|
|
lcore_history->tpoint_count[tpoint_id]++;
|
|
|
|
|
2021-06-16 11:27:49 +00:00
|
|
|
tpoint = &g_trace_flags->tpoint[tpoint_id];
|
|
|
|
/* Make sure that the number of arguments passed matches tracepoint definition */
|
2022-10-20 20:38:46 +00:00
|
|
|
if (spdk_unlikely(tpoint->num_args != num_args)) {
|
2021-06-16 11:27:49 +00:00
|
|
|
assert(0 && "Unexpected number of tracepoint arguments");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-15 03:01:51 +00:00
|
|
|
/* Get next entry index in the circular buffer */
|
2021-06-14 12:04:01 +00:00
|
|
|
next_entry = get_trace_entry(lcore_history, lcore_history->next_entry);
|
2016-05-10 23:17:51 +00:00
|
|
|
next_entry->tsc = tsc;
|
|
|
|
next_entry->tpoint_id = tpoint_id;
|
|
|
|
next_entry->poller_id = poller_id;
|
|
|
|
next_entry->size = size;
|
|
|
|
next_entry->object_id = object_id;
|
2021-05-17 13:10:39 +00:00
|
|
|
|
2021-06-16 12:14:50 +00:00
|
|
|
num_entries = 1;
|
|
|
|
buffer = (struct spdk_trace_entry_buffer *)next_entry;
|
|
|
|
/* The initial offset needs to be adjusted by the fields present in the first entry
|
|
|
|
* (poller_id, size, etc.).
|
|
|
|
*/
|
|
|
|
offset = offsetof(struct spdk_trace_entry, args) -
|
|
|
|
offsetof(struct spdk_trace_entry_buffer, data);
|
|
|
|
|
2021-05-17 13:10:39 +00:00
|
|
|
va_start(vl, num_args);
|
2021-06-16 12:14:50 +00:00
|
|
|
for (i = 0; i < tpoint->num_args; ++i) {
|
2021-06-16 11:27:49 +00:00
|
|
|
argument = &tpoint->args[i];
|
|
|
|
switch (argument->type) {
|
2021-05-17 13:16:10 +00:00
|
|
|
case SPDK_TRACE_ARG_TYPE_STR:
|
2021-06-16 12:14:50 +00:00
|
|
|
argval = va_arg(vl, void *);
|
|
|
|
arglen = strnlen((const char *)argval, argument->size - 1) + 1;
|
2021-05-17 13:16:10 +00:00
|
|
|
break;
|
|
|
|
case SPDK_TRACE_ARG_TYPE_INT:
|
|
|
|
case SPDK_TRACE_ARG_TYPE_PTR:
|
2022-08-11 05:34:47 +00:00
|
|
|
if (argument->size == 8) {
|
|
|
|
intval = va_arg(vl, uint64_t);
|
|
|
|
} else {
|
|
|
|
intval = va_arg(vl, uint32_t);
|
|
|
|
}
|
2021-06-16 12:14:50 +00:00
|
|
|
argval = &intval;
|
2022-08-11 05:34:47 +00:00
|
|
|
arglen = argument->size;
|
2021-05-17 13:16:10 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0 && "Invalid trace argument type");
|
2021-07-08 13:22:49 +00:00
|
|
|
return;
|
2021-05-17 13:16:10 +00:00
|
|
|
}
|
|
|
|
|
2021-06-16 12:14:50 +00:00
|
|
|
/* Copy argument's data. For some argument types (strings) user is allowed to pass a
|
|
|
|
* value that is either larger or smaller than what's defined in the tracepoint's
|
|
|
|
* description. If the value is larger, we'll truncate it, while if it's smaller,
|
|
|
|
* we'll only fill portion of the buffer, without touching the rest. For instance,
|
|
|
|
* if the definition marks an argument as 40B and user passes 12B string, we'll only
|
|
|
|
* copy 13B (accounting for the NULL terminator).
|
|
|
|
*/
|
|
|
|
argoff = 0;
|
|
|
|
while (argoff < argument->size) {
|
|
|
|
/* Current buffer is full, we need to acquire another one */
|
2022-10-20 20:38:46 +00:00
|
|
|
if (spdk_unlikely(offset == sizeof(buffer->data))) {
|
2021-06-16 12:14:50 +00:00
|
|
|
buffer = (struct spdk_trace_entry_buffer *) get_trace_entry(
|
|
|
|
lcore_history,
|
|
|
|
lcore_history->next_entry + num_entries);
|
|
|
|
buffer->tpoint_id = SPDK_TRACE_MAX_TPOINT_ID;
|
|
|
|
buffer->tsc = tsc;
|
|
|
|
num_entries++;
|
|
|
|
offset = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
curlen = spdk_min(sizeof(buffer->data) - offset, argument->size - argoff);
|
2022-10-20 20:38:46 +00:00
|
|
|
if (spdk_likely(argoff < arglen)) {
|
2021-07-08 13:22:49 +00:00
|
|
|
assert(argval != NULL);
|
2021-06-16 12:14:50 +00:00
|
|
|
memcpy(&buffer->data[offset], (uint8_t *)argval + argoff,
|
|
|
|
spdk_min(curlen, arglen - argoff));
|
|
|
|
}
|
|
|
|
|
|
|
|
offset += curlen;
|
|
|
|
argoff += curlen;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure that truncated strings are NULL-terminated */
|
2022-10-20 20:38:46 +00:00
|
|
|
if (spdk_unlikely(argument->type == SPDK_TRACE_ARG_TYPE_STR)) {
|
2021-06-16 12:14:50 +00:00
|
|
|
assert(offset > 0);
|
|
|
|
buffer->data[offset - 1] = '\0';
|
|
|
|
}
|
2021-05-17 13:10:39 +00:00
|
|
|
}
|
|
|
|
va_end(vl);
|
2016-05-10 23:17:51 +00:00
|
|
|
|
2018-11-15 03:01:51 +00:00
|
|
|
/* Ensure all elements of the trace entry are visible to outside trace tools */
|
|
|
|
spdk_smp_wmb();
|
2021-06-16 12:14:50 +00:00
|
|
|
lcore_history->next_entry += num_entries;
|
2016-05-10 23:17:51 +00:00
|
|
|
}
|
|
|
|
|
2018-03-09 17:08:57 +00:00
|
|
|
int
|
2018-11-13 17:23:16 +00:00
|
|
|
spdk_trace_init(const char *shm_name, uint64_t num_entries)
|
2016-05-10 23:17:51 +00:00
|
|
|
{
|
2022-08-12 05:56:58 +00:00
|
|
|
uint32_t i = 0;
|
2018-11-15 17:07:01 +00:00
|
|
|
int histories_size;
|
2022-08-12 05:56:58 +00:00
|
|
|
uint64_t lcore_offsets[SPDK_TRACE_MAX_LCORE + 1] = { 0 };
|
|
|
|
struct spdk_cpuset cpuset = {};
|
2018-11-15 17:07:01 +00:00
|
|
|
|
2020-02-05 14:17:05 +00:00
|
|
|
/* 0 entries requested - skip trace initialization */
|
|
|
|
if (num_entries == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-08-12 05:56:58 +00:00
|
|
|
spdk_cpuset_zero(&cpuset);
|
|
|
|
histories_size = sizeof(struct spdk_trace_flags);
|
|
|
|
SPDK_ENV_FOREACH_CORE(i) {
|
|
|
|
spdk_cpuset_set_cpu(&cpuset, i, true);
|
|
|
|
lcore_offsets[i] = histories_size;
|
|
|
|
histories_size += spdk_get_trace_history_size(num_entries);
|
2018-11-15 17:07:01 +00:00
|
|
|
}
|
2022-08-12 05:56:58 +00:00
|
|
|
lcore_offsets[SPDK_TRACE_MAX_LCORE] = histories_size;
|
2016-05-10 23:17:51 +00:00
|
|
|
|
2017-02-10 17:39:12 +00:00
|
|
|
snprintf(g_shm_name, sizeof(g_shm_name), "%s", shm_name);
|
2016-05-10 23:17:51 +00:00
|
|
|
|
2018-03-09 17:08:57 +00:00
|
|
|
g_trace_fd = shm_open(shm_name, O_RDWR | O_CREAT, 0600);
|
|
|
|
if (g_trace_fd == -1) {
|
2020-04-18 12:12:43 +00:00
|
|
|
SPDK_ERRLOG("could not shm_open spdk_trace\n");
|
|
|
|
SPDK_ERRLOG("errno=%d %s\n", errno, spdk_strerror(errno));
|
2018-03-09 17:08:57 +00:00
|
|
|
return 1;
|
2016-05-10 23:17:51 +00:00
|
|
|
}
|
|
|
|
|
2018-11-13 17:23:16 +00:00
|
|
|
if (ftruncate(g_trace_fd, histories_size) != 0) {
|
2020-04-18 12:12:43 +00:00
|
|
|
SPDK_ERRLOG("could not truncate shm\n");
|
2018-03-09 17:08:57 +00:00
|
|
|
goto trace_init_err;
|
2016-05-10 23:17:51 +00:00
|
|
|
}
|
|
|
|
|
2018-11-13 17:23:16 +00:00
|
|
|
g_trace_histories = mmap(NULL, histories_size, PROT_READ | PROT_WRITE,
|
2018-03-09 17:08:57 +00:00
|
|
|
MAP_SHARED, g_trace_fd, 0);
|
2018-03-28 18:07:54 +00:00
|
|
|
if (g_trace_histories == MAP_FAILED) {
|
2020-04-18 12:12:43 +00:00
|
|
|
SPDK_ERRLOG("could not mmap shm\n");
|
2018-03-09 17:08:57 +00:00
|
|
|
goto trace_init_err;
|
2016-05-10 23:17:51 +00:00
|
|
|
}
|
|
|
|
|
2018-09-03 00:20:02 +00:00
|
|
|
/* TODO: On FreeBSD, mlock on shm_open'd memory doesn't seem to work. Docs say that kern.ipc.shm_use_phys=1
|
|
|
|
* should allow it, but forcing that doesn't seem to work either. So for now just skip mlock on FreeBSD
|
|
|
|
* altogether.
|
|
|
|
*/
|
|
|
|
#if defined(__linux__)
|
2018-11-13 17:23:16 +00:00
|
|
|
if (mlock(g_trace_histories, histories_size) != 0) {
|
2020-04-18 12:12:43 +00:00
|
|
|
SPDK_ERRLOG("Could not mlock shm for tracing - %s.\n", spdk_strerror(errno));
|
2018-09-03 00:20:02 +00:00
|
|
|
if (errno == ENOMEM) {
|
2020-04-18 12:12:43 +00:00
|
|
|
SPDK_ERRLOG("Check /dev/shm for old tracing files that can be deleted.\n");
|
2018-09-03 00:20:02 +00:00
|
|
|
}
|
|
|
|
goto trace_init_err;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-11-13 17:23:16 +00:00
|
|
|
memset(g_trace_histories, 0, histories_size);
|
2016-05-10 23:17:51 +00:00
|
|
|
|
2017-09-12 14:50:24 +00:00
|
|
|
g_trace_flags = &g_trace_histories->flags;
|
|
|
|
|
|
|
|
g_trace_flags->tsc_rate = spdk_get_ticks_hz();
|
2016-05-10 23:17:51 +00:00
|
|
|
|
2016-07-22 00:11:15 +00:00
|
|
|
for (i = 0; i < SPDK_TRACE_MAX_LCORE; i++) {
|
2018-11-13 17:23:16 +00:00
|
|
|
struct spdk_trace_history *lcore_history;
|
|
|
|
|
2018-11-15 17:07:01 +00:00
|
|
|
g_trace_flags->lcore_history_offsets[i] = lcore_offsets[i];
|
2022-08-12 05:56:58 +00:00
|
|
|
if (lcore_offsets[i] == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
assert(spdk_cpuset_get_cpu(&cpuset, i));
|
2018-11-15 17:07:01 +00:00
|
|
|
lcore_history = spdk_get_per_lcore_history(g_trace_histories, i);
|
2018-11-13 17:23:16 +00:00
|
|
|
lcore_history->lcore = i;
|
2018-11-15 17:07:01 +00:00
|
|
|
lcore_history->num_entries = num_entries;
|
2016-05-10 23:17:51 +00:00
|
|
|
}
|
2018-11-15 17:07:01 +00:00
|
|
|
g_trace_flags->lcore_history_offsets[SPDK_TRACE_MAX_LCORE] = lcore_offsets[SPDK_TRACE_MAX_LCORE];
|
2016-05-10 23:17:51 +00:00
|
|
|
|
2017-09-12 14:50:24 +00:00
|
|
|
spdk_trace_flags_init();
|
2018-03-09 17:08:57 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
trace_init_err:
|
2018-09-03 00:20:02 +00:00
|
|
|
if (g_trace_histories != MAP_FAILED) {
|
2018-11-13 17:23:16 +00:00
|
|
|
munmap(g_trace_histories, histories_size);
|
2018-09-03 00:20:02 +00:00
|
|
|
}
|
2018-03-09 17:08:57 +00:00
|
|
|
close(g_trace_fd);
|
|
|
|
g_trace_fd = -1;
|
|
|
|
shm_unlink(shm_name);
|
2018-09-03 00:20:02 +00:00
|
|
|
g_trace_histories = NULL;
|
2018-03-09 17:08:57 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
2016-05-10 23:17:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
spdk_trace_cleanup(void)
|
|
|
|
{
|
2022-08-12 05:56:58 +00:00
|
|
|
bool unlink = true;
|
2018-11-29 11:12:26 +00:00
|
|
|
int i;
|
|
|
|
struct spdk_trace_history *lcore_history;
|
2018-08-31 22:16:39 +00:00
|
|
|
|
|
|
|
if (g_trace_histories == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2018-11-29 11:12:26 +00:00
|
|
|
* Only unlink the shm if there were no trace_entry recorded. This ensures the file
|
2018-08-31 22:16:39 +00:00
|
|
|
* can be used after this process exits/crashes for debugging.
|
|
|
|
* Note that we have to calculate this value before g_trace_histories gets unmapped.
|
|
|
|
*/
|
2018-11-29 11:12:26 +00:00
|
|
|
for (i = 0; i < SPDK_TRACE_MAX_LCORE; i++) {
|
|
|
|
lcore_history = spdk_get_per_lcore_history(g_trace_histories, i);
|
2022-08-12 05:56:58 +00:00
|
|
|
if (lcore_history == NULL) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-11-29 11:12:26 +00:00
|
|
|
unlink = lcore_history->entries[0].tsc == 0;
|
|
|
|
if (!unlink) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-31 22:16:39 +00:00
|
|
|
munmap(g_trace_histories, sizeof(struct spdk_trace_histories));
|
|
|
|
g_trace_histories = NULL;
|
|
|
|
close(g_trace_fd);
|
|
|
|
|
|
|
|
if (unlink) {
|
|
|
|
shm_unlink(g_shm_name);
|
2018-02-22 23:50:51 +00:00
|
|
|
}
|
2016-05-10 23:17:51 +00:00
|
|
|
}
|
2022-11-14 02:47:03 +00:00
|
|
|
|
|
|
|
const char *
|
|
|
|
trace_get_shm_name(void)
|
|
|
|
{
|
|
|
|
return g_shm_name;
|
|
|
|
}
|