xref: /spdk/lib/trace/trace.c (revision 488570ebd418ba07c9e69e65106dcc964f3bb41b)
1 /*   SPDX-License-Identifier: BSD-3-Clause
2  *   Copyright (c) Intel Corporation.
3  *   All rights reserved.
4  */
5 
6 #include "spdk/stdinc.h"
7 
8 #include "spdk/env.h"
9 #include "spdk/string.h"
10 #include "spdk/trace.h"
11 #include "spdk/util.h"
12 #include "spdk/barrier.h"
13 #include "spdk/log.h"
14 
15 static int g_trace_fd = -1;
16 static char g_shm_name[64];
17 
18 struct spdk_trace_histories *g_trace_histories;
19 
20 static inline struct spdk_trace_entry *
21 get_trace_entry(struct spdk_trace_history *history, uint64_t offset)
22 {
23 	return &history->entries[offset & (history->num_entries - 1)];
24 }
25 
26 void
27 _spdk_trace_record(uint64_t tsc, uint16_t tpoint_id, uint16_t poller_id, uint32_t size,
28 		   uint64_t object_id, int num_args, ...)
29 {
30 	struct spdk_trace_history *lcore_history;
31 	struct spdk_trace_entry *next_entry;
32 	struct spdk_trace_entry_buffer *buffer;
33 	struct spdk_trace_tpoint *tpoint;
34 	struct spdk_trace_argument *argument;
35 	unsigned lcore, i, offset, num_entries, arglen, argoff, curlen;
36 	uint64_t intval;
37 	void *argval;
38 	va_list vl;
39 
40 	lcore = spdk_env_get_current_core();
41 	if (lcore >= SPDK_TRACE_MAX_LCORE) {
42 		return;
43 	}
44 
45 	lcore_history = spdk_get_per_lcore_history(g_trace_histories, lcore);
46 	if (tsc == 0) {
47 		tsc = spdk_get_ticks();
48 	}
49 
50 	lcore_history->tpoint_count[tpoint_id]++;
51 
52 	tpoint = &g_trace_flags->tpoint[tpoint_id];
53 	/* Make sure that the number of arguments passed matches tracepoint definition */
54 	if (tpoint->num_args != num_args) {
55 		assert(0 && "Unexpected number of tracepoint arguments");
56 		return;
57 	}
58 
59 	/* Get next entry index in the circular buffer */
60 	next_entry = get_trace_entry(lcore_history, lcore_history->next_entry);
61 	next_entry->tsc = tsc;
62 	next_entry->tpoint_id = tpoint_id;
63 	next_entry->poller_id = poller_id;
64 	next_entry->size = size;
65 	next_entry->object_id = object_id;
66 
67 	num_entries = 1;
68 	buffer = (struct spdk_trace_entry_buffer *)next_entry;
69 	/* The initial offset needs to be adjusted by the fields present in the first entry
70 	 * (poller_id, size, etc.).
71 	 */
72 	offset = offsetof(struct spdk_trace_entry, args) -
73 		 offsetof(struct spdk_trace_entry_buffer, data);
74 
75 	va_start(vl, num_args);
76 	for (i = 0; i < tpoint->num_args; ++i) {
77 		argument = &tpoint->args[i];
78 		switch (argument->type) {
79 		case SPDK_TRACE_ARG_TYPE_STR:
80 			argval = va_arg(vl, void *);
81 			arglen = strnlen((const char *)argval, argument->size - 1) + 1;
82 			break;
83 		case SPDK_TRACE_ARG_TYPE_INT:
84 		case SPDK_TRACE_ARG_TYPE_PTR:
85 			intval = va_arg(vl, uint64_t);
86 			argval = &intval;
87 			arglen = sizeof(uint64_t);
88 			break;
89 		default:
90 			assert(0 && "Invalid trace argument type");
91 			return;
92 		}
93 
94 		/* Copy argument's data. For some argument types (strings) user is allowed to pass a
95 		 * value that is either larger or smaller than what's defined in the tracepoint's
96 		 * description. If the value is larger, we'll truncate it, while if it's smaller,
97 		 * we'll only fill portion of the buffer, without touching the rest. For instance,
98 		 * if the definition marks an argument as 40B and user passes 12B string, we'll only
99 		 * copy 13B (accounting for the NULL terminator).
100 		 */
101 		argoff = 0;
102 		while (argoff < argument->size) {
103 			/* Current buffer is full, we need to acquire another one */
104 			if (offset == sizeof(buffer->data)) {
105 				buffer = (struct spdk_trace_entry_buffer *) get_trace_entry(
106 						 lcore_history,
107 						 lcore_history->next_entry + num_entries);
108 				buffer->tpoint_id = SPDK_TRACE_MAX_TPOINT_ID;
109 				buffer->tsc = tsc;
110 				num_entries++;
111 				offset = 0;
112 			}
113 
114 			curlen = spdk_min(sizeof(buffer->data) - offset, argument->size - argoff);
115 			if (argoff < arglen) {
116 				assert(argval != NULL);
117 				memcpy(&buffer->data[offset], (uint8_t *)argval + argoff,
118 				       spdk_min(curlen, arglen - argoff));
119 			}
120 
121 			offset += curlen;
122 			argoff += curlen;
123 		}
124 
125 		/* Make sure that truncated strings are NULL-terminated */
126 		if (argument->type == SPDK_TRACE_ARG_TYPE_STR) {
127 			assert(offset > 0);
128 			buffer->data[offset - 1] = '\0';
129 		}
130 	}
131 	va_end(vl);
132 
133 	/* Ensure all elements of the trace entry are visible to outside trace tools */
134 	spdk_smp_wmb();
135 	lcore_history->next_entry += num_entries;
136 }
137 
138 int
139 spdk_trace_init(const char *shm_name, uint64_t num_entries)
140 {
141 	int i = 0;
142 	int histories_size;
143 	uint64_t lcore_offsets[SPDK_TRACE_MAX_LCORE + 1];
144 
145 	/* 0 entries requested - skip trace initialization */
146 	if (num_entries == 0) {
147 		return 0;
148 	}
149 
150 	lcore_offsets[0] = sizeof(struct spdk_trace_flags);
151 	for (i = 1; i < (int)SPDK_COUNTOF(lcore_offsets); i++) {
152 		lcore_offsets[i] = spdk_get_trace_history_size(num_entries) + lcore_offsets[i - 1];
153 	}
154 	histories_size = lcore_offsets[SPDK_TRACE_MAX_LCORE];
155 
156 	snprintf(g_shm_name, sizeof(g_shm_name), "%s", shm_name);
157 
158 	g_trace_fd = shm_open(shm_name, O_RDWR | O_CREAT, 0600);
159 	if (g_trace_fd == -1) {
160 		SPDK_ERRLOG("could not shm_open spdk_trace\n");
161 		SPDK_ERRLOG("errno=%d %s\n", errno, spdk_strerror(errno));
162 		return 1;
163 	}
164 
165 	if (ftruncate(g_trace_fd, histories_size) != 0) {
166 		SPDK_ERRLOG("could not truncate shm\n");
167 		goto trace_init_err;
168 	}
169 
170 	g_trace_histories = mmap(NULL, histories_size, PROT_READ | PROT_WRITE,
171 				 MAP_SHARED, g_trace_fd, 0);
172 	if (g_trace_histories == MAP_FAILED) {
173 		SPDK_ERRLOG("could not mmap shm\n");
174 		goto trace_init_err;
175 	}
176 
177 	/* TODO: On FreeBSD, mlock on shm_open'd memory doesn't seem to work.  Docs say that kern.ipc.shm_use_phys=1
178 	 * should allow it, but forcing that doesn't seem to work either.  So for now just skip mlock on FreeBSD
179 	 * altogether.
180 	 */
181 #if defined(__linux__)
182 	if (mlock(g_trace_histories, histories_size) != 0) {
183 		SPDK_ERRLOG("Could not mlock shm for tracing - %s.\n", spdk_strerror(errno));
184 		if (errno == ENOMEM) {
185 			SPDK_ERRLOG("Check /dev/shm for old tracing files that can be deleted.\n");
186 		}
187 		goto trace_init_err;
188 	}
189 #endif
190 
191 	memset(g_trace_histories, 0, histories_size);
192 
193 	g_trace_flags = &g_trace_histories->flags;
194 
195 	g_trace_flags->tsc_rate = spdk_get_ticks_hz();
196 
197 	for (i = 0; i < SPDK_TRACE_MAX_LCORE; i++) {
198 		struct spdk_trace_history *lcore_history;
199 
200 		g_trace_flags->lcore_history_offsets[i] = lcore_offsets[i];
201 		lcore_history = spdk_get_per_lcore_history(g_trace_histories, i);
202 		lcore_history->lcore = i;
203 		lcore_history->num_entries = num_entries;
204 	}
205 	g_trace_flags->lcore_history_offsets[SPDK_TRACE_MAX_LCORE] = lcore_offsets[SPDK_TRACE_MAX_LCORE];
206 
207 	spdk_trace_flags_init();
208 
209 	return 0;
210 
211 trace_init_err:
212 	if (g_trace_histories != MAP_FAILED) {
213 		munmap(g_trace_histories, histories_size);
214 	}
215 	close(g_trace_fd);
216 	g_trace_fd = -1;
217 	shm_unlink(shm_name);
218 	g_trace_histories = NULL;
219 
220 	return 1;
221 
222 }
223 
224 void
225 spdk_trace_cleanup(void)
226 {
227 	bool unlink;
228 	int i;
229 	struct spdk_trace_history *lcore_history;
230 
231 	if (g_trace_histories == NULL) {
232 		return;
233 	}
234 
235 	/*
236 	 * Only unlink the shm if there were no trace_entry recorded. This ensures the file
237 	 * can be used after this process exits/crashes for debugging.
238 	 * Note that we have to calculate this value before g_trace_histories gets unmapped.
239 	 */
240 	for (i = 0; i < SPDK_TRACE_MAX_LCORE; i++) {
241 		lcore_history = spdk_get_per_lcore_history(g_trace_histories, i);
242 		unlink = lcore_history->entries[0].tsc == 0;
243 		if (!unlink) {
244 			break;
245 		}
246 	}
247 
248 	munmap(g_trace_histories, sizeof(struct spdk_trace_histories));
249 	g_trace_histories = NULL;
250 	close(g_trace_fd);
251 
252 	if (unlink) {
253 		shm_unlink(g_shm_name);
254 	}
255 }
256