xref: /spdk/lib/trace/trace.c (revision 59970a89be936e68272f889ea856bd60c987cc39)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) Intel Corporation.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include "spdk/stdinc.h"
35 
36 #include "spdk/env.h"
37 #include "spdk/string.h"
38 #include "spdk/trace.h"
39 
40 #include <rte_config.h>
41 #include <rte_lcore.h>
42 
43 static char g_shm_name[64];
44 
45 static struct spdk_trace_histories *g_trace_histories;
46 
47 void
48 spdk_trace_record(uint16_t tpoint_id, uint16_t poller_id, uint32_t size,
49 		  uint64_t object_id, uint64_t arg1)
50 {
51 	struct spdk_trace_history *lcore_history;
52 	struct spdk_trace_entry *next_entry;
53 	uint64_t tsc;
54 	unsigned lcore;
55 
56 	/*
57 	 * Tracepoint group ID is encoded in the tpoint_id.  Lower 6 bits determine the tracepoint
58 	 *  within the group, the remaining upper bits determine the tracepoint group.  Each
59 	 *  tracepoint group has its own tracepoint mask.
60 	 */
61 	if (g_trace_histories == NULL ||
62 	    !((1ULL << (tpoint_id & 0x3F)) & g_trace_histories->flags.tpoint_mask[tpoint_id >> 6])) {
63 		return;
64 	}
65 
66 	lcore = rte_lcore_id();
67 	if (lcore >= SPDK_TRACE_MAX_LCORE) {
68 		return;
69 	}
70 
71 	lcore_history = &g_trace_histories->per_lcore_history[lcore];
72 	tsc = spdk_get_ticks();
73 
74 	lcore_history->tpoint_count[tpoint_id]++;
75 
76 	next_entry = &lcore_history->entries[lcore_history->next_entry];
77 	next_entry->tsc = tsc;
78 	next_entry->tpoint_id = tpoint_id;
79 	next_entry->poller_id = poller_id;
80 	next_entry->size = size;
81 	next_entry->object_id = object_id;
82 	next_entry->arg1 = arg1;
83 
84 	lcore_history->next_entry++;
85 	if (lcore_history->next_entry == SPDK_TRACE_SIZE) {
86 		lcore_history->next_entry = 0;
87 	}
88 }
89 
90 void
91 spdk_trace_init(const char *shm_name)
92 {
93 	int trace_fd;
94 	int i = 0;
95 	char buf[64];
96 
97 	snprintf(g_shm_name, sizeof(g_shm_name), "%s", shm_name);
98 
99 	trace_fd = shm_open(shm_name, O_RDWR | O_CREAT, 0600);
100 	if (trace_fd == -1) {
101 		spdk_strerror_r(errno, buf, sizeof(buf));
102 		fprintf(stderr, "could not shm_open spdk_trace\n");
103 		fprintf(stderr, "errno=%d %s\n", errno, buf);
104 		exit(EXIT_FAILURE);
105 	}
106 
107 	if (ftruncate(trace_fd, sizeof(*g_trace_histories)) != 0) {
108 		fprintf(stderr, "could not truncate shm\n");
109 		exit(EXIT_FAILURE);
110 	}
111 
112 	g_trace_histories = mmap(NULL, sizeof(*g_trace_histories), PROT_READ | PROT_WRITE,
113 				 MAP_SHARED, trace_fd, 0);
114 	if (g_trace_histories == NULL) {
115 		fprintf(stderr, "could not mmap shm\n");
116 		exit(EXIT_FAILURE);
117 	}
118 
119 	memset(g_trace_histories, 0, sizeof(*g_trace_histories));
120 
121 	g_trace_flags = &g_trace_histories->flags;
122 
123 	g_trace_flags->tsc_rate = spdk_get_ticks_hz();
124 
125 	for (i = 0; i < SPDK_TRACE_MAX_LCORE; i++) {
126 		g_trace_histories->per_lcore_history[i].lcore = i;
127 	}
128 
129 	spdk_trace_flags_init();
130 }
131 
132 void
133 spdk_trace_cleanup(void)
134 {
135 	munmap(g_trace_histories, sizeof(struct spdk_trace_histories));
136 	shm_unlink(g_shm_name);
137 }
138