trace: add tracepoint library and app

Change-Id: I472fb7e7a82e1337c6c06b1d3bb4e8a2a13d884a
Signed-off-by: Daniel Verkamp <daniel.verkamp@intel.com>
This commit is contained in:
Daniel Verkamp 2016-05-10 16:17:51 -07:00
parent ed694026b8
commit bdece622f5
10 changed files with 995 additions and 3 deletions

View File

@ -36,13 +36,14 @@ S :=
SPDK_ROOT_DIR := $(CURDIR) SPDK_ROOT_DIR := $(CURDIR)
include $(SPDK_ROOT_DIR)/mk/spdk.common.mk include $(SPDK_ROOT_DIR)/mk/spdk.common.mk
DIRS-y += lib test examples DIRS-y += lib test examples app
.PHONY: all clean $(DIRS-y) .PHONY: all clean $(DIRS-y)
all: $(DIRS-y) all: $(DIRS-y)
clean: $(DIRS-y) clean: $(DIRS-y)
app: lib
test: lib test: lib
examples: lib examples: lib

44
app/Makefile Normal file
View File

@ -0,0 +1,44 @@
#
# 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
DIRS-y += trace
.PHONY: all clean $(DIRS-y)
all: $(DIRS-y)
clean: $(DIRS-y)
include $(SPDK_ROOT_DIR)/mk/spdk.subdirs.mk

1
app/trace/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
spdk_trace

51
app/trace/Makefile Normal file
View File

@ -0,0 +1,51 @@
#
# 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
LIBS += -lrt
CXXFLAGS += $(DPDK_INC)
CXX_SRCS := trace.cpp
APP = spdk_trace
all: $(APP)
$(APP): $(OBJS) $(SPDK_LIBS)
$(LINK_CXX)
clean:
$(CLEAN_C) $(APP)
include $(SPDK_ROOT_DIR)/mk/spdk.deps.mk

406
app/trace/trace.cpp Normal file
View File

@ -0,0 +1,406 @@
/*-
* 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 <sys/types.h>
#include <sys/uio.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>
#include <fcntl.h>
#include <map>
extern "C" {
#include "spdk/trace.h"
}
static struct spdk_trace_histories *g_histories;
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 *exe_name;
static int verbose = 1;
static int g_instance_id = 0;
static int g_fudge_factor = 20;
static uint64_t tsc_rate;
static uint64_t first_tsc = 0x0;
static uint64_t last_tsc = -1ULL;
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_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->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(bool arg_is_ptr, const char *arg_string, uint64_t arg)
{
if (arg_string[0] == 0)
return;
if (arg_is_ptr)
print_ptr(arg_string, arg);
else
print_uint64(arg_string, arg);
}
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->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;
}
if (d->arg1_is_alias) {
stats->index[e->arg1] = stats->index[e->object_id];
stats->start[e->arg1] = stats->start[e->object_id];
stats->size[e->arg1] = stats->size[e->object_id];
}
us = get_us_from_tsc(e->tsc - tsc_offset, tsc_rate);
printf("%2d: %10.3f (%9ju) ", lcore, us, e->tsc - tsc_offset);
if (g_histories->owner[d->owner_type].id_prefix) {
printf("%c%02d ", g_histories->owner[d->owner_type].id_prefix, e->poller_id);
} else {
printf("%4s", " ");
}
printf("%-*s ", (int)sizeof(d->name), d->name);
print_size(e->size);
if (d->new_object) {
print_arg(d->arg1_is_ptr, d->arg1_name, e->arg1);
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()) {
struct spdk_trace_tpoint *start_description;
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);
start_description = &g_histories->tpoint[stats->tpoint_id[e->object_id]];
if (start_description->short_name[0] != 0) {
printf(" (%.4s)", start_description->short_name);
}
} else {
printf("id: N/A");
}
} else {
print_arg(d->arg1_is_ptr, d->arg1_name, e->arg1);
}
printf("\n");
}
static void
process_event(struct spdk_trace_entry *e, uint64_t tsc_rate,
uint64_t tsc_offset, uint16_t lcore)
{
if (verbose) {
print_event(e, tsc_rate, tsc_offset, lcore);
}
}
static int
populate_events(struct spdk_trace_history *history)
{
int i, entry_size, history_size, num_entries, num_entries_filled;
struct spdk_trace_entry *e;
int first, last, lcore;
lcore = history->lcore;
entry_size = sizeof(history->entries[0]);
history_size = sizeof(history->entries);
num_entries = history_size / entry_size;
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;
}
first += g_fudge_factor;
if (first >= num_entries)
first -= num_entries;
last -= g_fudge_factor;
if (last < 0)
last += num_entries;
} else {
first = 0;
last = num_entries_filled - 1;
}
/*
* We keep track of the highest first TSC out of all reactors and
* the lowest last TSC out of all reactors. We will ignore any
* events outside the range of these two TSC values. This will
* ensure we only print data for the subset of time where we have
* data across all reactors.
*/
if (e[first].tsc > first_tsc) {
first_tsc = e[first].tsc;
}
if (e[last].tsc < last_tsc) {
last_tsc = e[last].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", exe_name);
fprintf(stderr, " option = '-q' to disable verbose mode\n");
fprintf(stderr, " '-s' to specify spdk_trace shm name\n");
fprintf(stderr, " '-c' to display single lcore history\n");
fprintf(stderr, " '-f' to specify number of events to ignore at\n");
fprintf(stderr, " beginning and end of trace (default: 20)\n");
fprintf(stderr, " '-i' to specify the instance ID, (default: 0)\n");
}
int main(int argc, char **argv)
{
void *history_ptr;
struct spdk_trace_history *history_entries, *history;
int fd, i;
int lcore = RTE_MAX_LCORE;
uint64_t tsc_offset;
const char *app_name = "ids";
int op;
char shm_name[64];
exe_name = argv[0];
while ((op = getopt(argc, argv, "c:f:i:qs:")) != -1) {
switch (op) {
case 'c':
lcore = atoi(optarg);
if (lcore > RTE_MAX_LCORE) {
fprintf(stderr, "Selected lcore: %d "
"exceeds maximum %d\n", lcore,
RTE_MAX_LCORE);
exit(1);
}
break;
case 'f':
g_fudge_factor = atoi(optarg);
break;
case 'i':
g_instance_id = atoi(optarg);
break;
case 'q':
verbose = 0;
break;
case 's':
app_name = optarg;
break;
default:
usage();
exit(1);
}
}
snprintf(shm_name, sizeof(shm_name), "%s_trace.%d", app_name, g_instance_id);
fd = shm_open(shm_name, O_RDONLY, 0600);
if (fd < 0) {
fprintf(stderr, "Could not open shm %s.\n", shm_name);
usage();
exit(-1);
}
history_ptr = mmap(NULL, sizeof(*g_histories), PROT_READ, MAP_SHARED, fd, 0);
g_histories = (struct spdk_trace_histories *)history_ptr;
tsc_rate = g_histories->tsc_rate;
if (verbose) {
printf("TSC Rate: %ju\n", tsc_rate);
}
history_entries = (struct spdk_trace_history *)malloc(sizeof(g_histories->per_lcore_history));
if (history_entries == NULL) {
goto cleanup;
}
memcpy(history_entries, g_histories->per_lcore_history,
sizeof(g_histories->per_lcore_history));
if (lcore == RTE_MAX_LCORE) {
for (i = 0; i < RTE_MAX_LCORE; i++) {
history = &history_entries[i];
if (history->entries[0].tsc == 0) {
continue;
}
populate_events(history);
}
} else {
history = &history_entries[lcore];
if (history->entries[0].tsc != 0) {
populate_events(history);
}
}
tsc_offset = first_tsc;
for (entry_map::iterator it = g_entry_map.begin(); it != g_entry_map.end(); it++) {
if (it->first.tsc < first_tsc || it->first.tsc > last_tsc) {
continue;
}
process_event(it->second, tsc_rate, tsc_offset, it->first.lcore);
}
free(history_entries);
cleanup:
munmap(history_ptr, sizeof(*g_histories));
close(fd);
return (0);
}

184
include/spdk/trace.h Normal file
View File

@ -0,0 +1,184 @@
/*-
* 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.
*/
/**
* \file
* Tracepoint library
*/
#ifndef _SPDK_TRACE_H_
#define _SPDK_TRACE_H_
#include <inttypes.h>
#include <limits.h>
#include <rte_config.h>
#include <rte_lcore.h>
#ifdef __cplusplus
extern "C" {
#endif
#define SPDK_TRACE_SIZE (32 * 1024)
struct spdk_trace_entry {
uint64_t tsc;
uint16_t tpoint_id;
uint16_t poller_id;
uint32_t size;
uint64_t object_id;
uint64_t arg1;
};
/* If type changes from a uint8_t, change this value. */
#define SPDK_TRACE_MAX_OWNER (UCHAR_MAX + 1)
struct spdk_trace_owner {
uint8_t type;
char id_prefix;
};
/* If type changes from a uint8_t, change this value. */
#define SPDK_TRACE_MAX_OBJECT (UCHAR_MAX + 1)
struct spdk_trace_object {
uint8_t type;
char id_prefix;
};
#define SPDK_TRACE_MAX_GROUP_ID 16
#define SPDK_TRACE_MAX_TPOINT_ID (SPDK_TRACE_MAX_GROUP_ID * 64)
#define SPDK_TPOINT_ID(group, tpoint) ((group * 64) + tpoint)
struct spdk_trace_tpoint {
char name[24];
char short_name[4];
uint16_t tpoint_id;
uint8_t owner_type;
uint8_t object_type;
uint8_t new_object;
uint8_t arg1_is_ptr;
uint8_t arg1_is_alias;
char arg1_name[8];
};
struct spdk_trace_history {
/** Logical core number associated with this structure instance. */
int lcore;
/**
* Circular buffer of spdk_trace_entry structures for tracing
* tpoints on this core. Debug tool spdk_trace reads this
* buffer from shared memory to post-process the tpoint entries and
* display in a human-readable format.
*/
struct spdk_trace_entry entries[SPDK_TRACE_SIZE];
/**
* Running count of number of occurrences of each tracepoint on this
* lcore. Debug tools can use this to easily count tracepoints such as
* number of SCSI tasks completed or PDUs read.
*/
uint64_t tpoint_count[SPDK_TRACE_MAX_TPOINT_ID];
/** Index to next spdk_trace_entry to fill in the circular buffer. */
uint32_t next_entry;
};
struct spdk_trace_histories {
uint64_t tsc_rate;
uint64_t tpoint_mask[SPDK_TRACE_MAX_GROUP_ID];
struct spdk_trace_history per_lcore_history[RTE_MAX_LCORE];
struct spdk_trace_owner owner[UCHAR_MAX + 1];
struct spdk_trace_object object[UCHAR_MAX + 1];
struct spdk_trace_tpoint tpoint[SPDK_TRACE_MAX_TPOINT_ID];
};
void spdk_trace_record(uint16_t tpoint_id, uint16_t poller_id, uint32_t size,
uint64_t object_id, uint64_t arg1);
/** Returns the current tpoint mask. */
uint64_t spdk_trace_get_tpoint_mask(uint32_t group_id);
/** Adds the specified tpoints to the current tpoint mask for the given tpoint group. */
void spdk_trace_set_tpoints(uint32_t group_id, uint64_t tpoint_mask);
/** Clears the specified tpoints from the current tpoint mask for the given tpoint group. */
void spdk_trace_clear_tpoints(uint32_t group_id, uint64_t tpoint_mask);
/** Returns a mask of all tracepoint groups which have at least one tracepoint enabled. */
uint64_t spdk_trace_get_tpoint_group_mask(void);
/** For each tpoint group specified in the group mask, enable all of its tpoints. */
void spdk_trace_set_tpoint_group_mask(uint64_t tpoint_group_mask);
void spdk_trace_init(const char *shm_name);
void spdk_trace_cleanup(void);
#define OWNER_NONE 0
#define OBJECT_NONE 0
void spdk_trace_register_owner(uint8_t type, char id_prefix);
void spdk_trace_register_object(uint8_t type, char id_prefix);
void spdk_trace_register_description(const char *name, const char *short_name,
uint16_t tpoint_id, uint8_t owner_type,
uint8_t object_type, uint8_t new_object,
uint8_t arg1_is_ptr, uint8_t arg1_is_alias,
const char *arg1_name);
struct spdk_trace_register_fn {
void (*reg_fn)(void);
struct spdk_trace_register_fn *next;
};
void spdk_trace_add_register_fn(struct spdk_trace_register_fn *reg_fn);
#define SPDK_TRACE_REGISTER_FN(fn) \
static void fn(void); \
struct spdk_trace_register_fn reg_ ## fn = { \
.reg_fn = fn, \
.next = NULL, \
}; \
__attribute__((constructor)) static void _ ## fn(void) \
{ \
spdk_trace_add_register_fn(&reg_ ## fn); \
} \
static void fn(void)
#ifdef __cplusplus
}
#endif
#endif

View File

@ -34,7 +34,7 @@
SPDK_ROOT_DIR := $(abspath $(CURDIR)/..) SPDK_ROOT_DIR := $(abspath $(CURDIR)/..)
include $(SPDK_ROOT_DIR)/mk/spdk.common.mk include $(SPDK_ROOT_DIR)/mk/spdk.common.mk
DIRS-y += conf json log memory util nvme ioat DIRS-y += conf json log memory trace util nvme ioat
.PHONY: all clean $(DIRS-y) .PHONY: all clean $(DIRS-y)

40
lib/trace/Makefile Normal file
View File

@ -0,0 +1,40 @@
#
# 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)/../..)
CFLAGS += $(DPDK_INC)
C_SRCS = trace.c
LIBNAME = trace
include $(SPDK_ROOT_DIR)/mk/spdk.lib.mk

253
lib/trace/trace.c Normal file
View File

@ -0,0 +1,253 @@
/*-
* 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/trace.h"
#include <stdint.h>
#include <string.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <errno.h>
#include <rte_config.h>
#include <rte_cycles.h>
#include <rte_lcore.h>
static char g_shm_name[64];
static struct spdk_trace_histories *g_trace_histories;
static struct spdk_trace_register_fn *g_reg_fn_head = NULL;
void
spdk_trace_record(uint16_t tpoint_id, uint16_t poller_id, uint32_t size,
uint64_t object_id, uint64_t arg1)
{
struct spdk_trace_history *lcore_history;
struct spdk_trace_entry *next_entry;
uint64_t tsc;
/*
* Tracepoint group ID is encoded in the tpoint_id. Lower 6 bits determine the tracepoint
* within the group, the remaining upper bits determine the tracepoint group. Each
* tracepoint group has its own tracepoint mask.
*/
if (g_trace_histories == NULL ||
!((1ULL << (tpoint_id & 0x3F)) & g_trace_histories->tpoint_mask[tpoint_id >> 6])) {
return;
}
lcore_history = &g_trace_histories->per_lcore_history[rte_lcore_id()];
tsc = rte_get_timer_cycles();
lcore_history->tpoint_count[tpoint_id]++;
next_entry = &lcore_history->entries[lcore_history->next_entry];
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;
next_entry->arg1 = arg1;
lcore_history->next_entry++;
if (lcore_history->next_entry == SPDK_TRACE_SIZE)
lcore_history->next_entry = 0;
}
uint64_t
spdk_trace_get_tpoint_mask(uint32_t group_id)
{
return g_trace_histories->tpoint_mask[group_id];
}
void
spdk_trace_set_tpoints(uint32_t group_id, uint64_t tpoint_mask)
{
g_trace_histories->tpoint_mask[group_id] |= tpoint_mask;
}
void
spdk_trace_clear_tpoints(uint32_t group_id, uint64_t tpoint_mask)
{
g_trace_histories->tpoint_mask[group_id] &= ~tpoint_mask;
}
uint64_t
spdk_trace_get_tpoint_group_mask(void)
{
uint64_t mask = 0x0;
int i;
for (i = 0; i < 64; i++) {
if (spdk_trace_get_tpoint_mask(i) != 0) {
mask |= (1ULL << i);
}
}
return mask;
}
void
spdk_trace_set_tpoint_group_mask(uint64_t tpoint_group_mask)
{
int i;
for (i = 0; i < 64; i++) {
if (tpoint_group_mask & (1ULL << i)) {
spdk_trace_set_tpoints(i, -1ULL);
}
}
}
void
spdk_trace_init(const char *shm_name)
{
struct spdk_trace_register_fn *reg_fn;
int trace_fd;
int i = 0;
strncpy(g_shm_name, shm_name, sizeof(g_shm_name));
trace_fd = shm_open(shm_name, O_RDWR | O_CREAT, 0600);
if (trace_fd == -1) {
fprintf(stderr, "could not shm_open spdk_trace\n");
fprintf(stderr, "errno=%d %s\n", errno, strerror(errno));
exit(EXIT_FAILURE);
}
if (ftruncate(trace_fd, sizeof(*g_trace_histories)) != 0) {
fprintf(stderr, "could not truncate shm\n");
exit(EXIT_FAILURE);
}
g_trace_histories = mmap(NULL, sizeof(*g_trace_histories), PROT_READ | PROT_WRITE,
MAP_SHARED, trace_fd, 0);
if (g_trace_histories == NULL) {
fprintf(stderr, "could not mmap shm\n");
exit(EXIT_FAILURE);
}
memset(g_trace_histories, 0, sizeof(*g_trace_histories));
g_trace_histories->tsc_rate = rte_get_timer_hz();
for (i = 0; i < RTE_MAX_LCORE; i++) {
g_trace_histories->per_lcore_history[i].lcore = i;
}
reg_fn = g_reg_fn_head;
while (reg_fn) {
reg_fn->reg_fn();
reg_fn = reg_fn->next;
}
}
void
spdk_trace_cleanup(void)
{
munmap(g_trace_histories, sizeof(struct spdk_trace_histories));
shm_unlink(g_shm_name);
}
void
spdk_trace_register_owner(uint8_t type, char id_prefix)
{
struct spdk_trace_owner *owner;
RTE_VERIFY(type != OWNER_NONE);
/* 'owner' has 256 entries and since 'type' is a uint8_t, it
* can't overrun the array.
*/
owner = &g_trace_histories->owner[type];
RTE_VERIFY(owner->type == 0);
owner->type = type;
owner->id_prefix = id_prefix;
}
void
spdk_trace_register_object(uint8_t type, char id_prefix)
{
struct spdk_trace_object *object;
RTE_VERIFY(type != OBJECT_NONE);
/* 'object' has 256 entries and since 'type' is a uint8_t, it
* can't overrun the array.
*/
object = &g_trace_histories->object[type];
RTE_VERIFY(object->type == 0);
object->type = type;
object->id_prefix = id_prefix;
}
void
spdk_trace_register_description(const char *name, const char *short_name,
uint16_t tpoint_id, uint8_t owner_type,
uint8_t object_type, uint8_t new_object,
uint8_t arg1_is_ptr, uint8_t arg1_is_alias,
const char *arg1_name)
{
struct spdk_trace_tpoint *tpoint;
RTE_VERIFY(tpoint_id != 0);
RTE_VERIFY(tpoint_id < SPDK_TRACE_MAX_TPOINT_ID);
tpoint = &g_trace_histories->tpoint[tpoint_id];
RTE_VERIFY(tpoint->tpoint_id == 0);
strncpy(tpoint->name, name, sizeof(tpoint->name));
strncpy(tpoint->short_name, short_name, sizeof(tpoint->short_name));
tpoint->tpoint_id = tpoint_id;
tpoint->object_type = object_type;
tpoint->owner_type = owner_type;
tpoint->new_object = new_object;
tpoint->arg1_is_ptr = arg1_is_ptr;
tpoint->arg1_is_alias = arg1_is_alias;
strncpy(tpoint->arg1_name, arg1_name, sizeof(tpoint->arg1_name));
}
void
spdk_trace_add_register_fn(struct spdk_trace_register_fn *reg_fn)
{
reg_fn->next = g_reg_fn_head;
g_reg_fn_head = reg_fn;
}

View File

@ -104,7 +104,7 @@ endif
MAKEFLAGS += --no-print-directory MAKEFLAGS += --no-print-directory
OBJS = $(C_SRCS:.c=.o) OBJS = $(C_SRCS:.c=.o) $(CXX_SRCS:.cpp=.o)
DEPFLAGS = -MMD -MP -MF $*.d.tmp DEPFLAGS = -MMD -MP -MF $*.d.tmp
@ -114,11 +114,20 @@ COMPILE_C=\
$(CC) -o $@ $(DEPFLAGS) $(CFLAGS) -c $< && \ $(CC) -o $@ $(DEPFLAGS) $(CFLAGS) -c $< && \
mv -f $*.d.tmp $*.d mv -f $*.d.tmp $*.d
COMPILE_CXX=\
$(Q)echo " CXX $S/$@"; \
$(CXX) -o $@ $(DEPFLAGS) $(CXXFLAGS) -c $< && \
mv -f $*.d.tmp $*.d
# Link $(OBJS) and $(LIBS) into $@ (app) # Link $(OBJS) and $(LIBS) into $@ (app)
LINK_C=\ LINK_C=\
$(Q)echo " LINK $S/$@"; \ $(Q)echo " LINK $S/$@"; \
$(CC) -o $@ $(CPPFLAGS) $(LDFLAGS) $(OBJS) $(LIBS) $(CC) -o $@ $(CPPFLAGS) $(LDFLAGS) $(OBJS) $(LIBS)
LINK_CXX=\
$(Q)echo " LINK $S/$@"; \
$(CXX) -o $@ $(CPPFLAGS) $(LDFLAGS) $(OBJS) $(LIBS)
# Archive $(OBJS) into $@ (.a) # Archive $(OBJS) into $@ (.a)
LIB_C=\ LIB_C=\
$(Q)echo " LIB $S/$@"; \ $(Q)echo " LIB $S/$@"; \
@ -131,6 +140,9 @@ CLEAN_C=\
%.o: %.c %.d $(MAKEFILE_LIST) %.o: %.c %.d $(MAKEFILE_LIST)
$(COMPILE_C) $(COMPILE_C)
%.o: %.cpp %.d $(MAKEFILE_LIST)
$(COMPILE_CXX)
%.d: ; %.d: ;
DPDK_DIR ?= $(CONFIG_DPDK_DIR) DPDK_DIR ?= $(CONFIG_DPDK_DIR)