Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright (C) 2016 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 : : #include "spdk/cpuset.h"
15 : : #include "spdk/likely.h"
16 : : #include "spdk/bit_array.h"
17 : : #include "trace_internal.h"
18 : :
19 : : static int g_trace_fd = -1;
20 : : static char g_shm_name[64];
21 : :
22 : : static __thread uint32_t t_ut_array_index;
23 : : static __thread struct spdk_trace_history *t_ut_lcore_history;
24 : :
25 : : uint32_t g_user_thread_index_start;
26 : : struct spdk_trace_file *g_trace_file;
27 : : struct spdk_bit_array *g_ut_array;
28 : : pthread_mutex_t g_ut_array_mutex;
29 : :
30 : : #define TRACE_NUM_OWNERS (16 * 1024)
31 : : #define TRACE_OWNER_DESCRIPTION_SIZE (119)
32 : : SPDK_STATIC_ASSERT(sizeof(struct spdk_trace_owner) == 9, "incorrect size");
33 : : SPDK_STATIC_ASSERT(sizeof(struct spdk_trace_owner) + TRACE_OWNER_DESCRIPTION_SIZE == 128,
34 : : "incorrect size");
35 : :
36 : : static inline struct spdk_trace_entry *
37 : 343643013 : get_trace_entry(struct spdk_trace_history *history, uint64_t offset)
38 : : {
39 : 343643013 : return &history->entries[offset & (history->num_entries - 1)];
40 : : }
41 : :
42 : : void
43 : 302234730 : _spdk_trace_record(uint64_t tsc, uint16_t tpoint_id, uint16_t owner_id, uint32_t size,
44 : : uint64_t object_id, int num_args, ...)
45 : : {
46 : : struct spdk_trace_history *lcore_history;
47 : : struct spdk_trace_entry *next_entry;
48 : : struct spdk_trace_entry_buffer *buffer;
49 : : struct spdk_trace_tpoint *tpoint;
50 : : struct spdk_trace_argument *argument;
51 : : unsigned lcore, i, offset, num_entries, arglen, argoff, curlen;
52 : 806397 : uint64_t intval;
53 : : void *argval;
54 : 806397 : va_list vl;
55 : :
56 : 302234730 : lcore = spdk_env_get_current_core();
57 [ + - ]: 302234730 : if (spdk_likely(lcore != SPDK_ENV_LCORE_ID_ANY)) {
58 : 302234730 : lcore_history = spdk_get_per_lcore_history(g_trace_file, lcore);
59 [ # # ]: 0 : } else if (t_ut_lcore_history != NULL) {
60 : 0 : lcore_history = t_ut_lcore_history;
61 : : } else {
62 : 0 : return;
63 : : }
64 : :
65 [ + + ]: 302234730 : if (tsc == 0) {
66 : 265822829 : tsc = spdk_get_ticks();
67 : : }
68 : :
69 : 302234730 : lcore_history->tpoint_count[tpoint_id]++;
70 : :
71 : 302234730 : tpoint = &g_trace_file->tpoint[tpoint_id];
72 : : /* Make sure that the number of arguments passed matches tracepoint definition */
73 [ - + ]: 302234730 : if (spdk_unlikely(tpoint->num_args != num_args)) {
74 : 0 : assert(0 && "Unexpected number of tracepoint arguments");
75 : : return;
76 : : }
77 : :
78 : : /* Get next entry index in the circular buffer */
79 : 302234730 : next_entry = get_trace_entry(lcore_history, lcore_history->next_entry);
80 : 302234730 : next_entry->tsc = tsc;
81 : 302234730 : next_entry->tpoint_id = tpoint_id;
82 : 302234730 : next_entry->owner_id = owner_id;
83 : 302234730 : next_entry->size = size;
84 : 302234730 : next_entry->object_id = object_id;
85 : :
86 : 302234730 : num_entries = 1;
87 : 302234730 : buffer = (struct spdk_trace_entry_buffer *)next_entry;
88 : : /* The initial offset needs to be adjusted by the fields present in the first entry
89 : : * (owner_id, size, etc.).
90 : : */
91 : 302234730 : offset = offsetof(struct spdk_trace_entry, args) -
92 : : offsetof(struct spdk_trace_entry_buffer, data);
93 : :
94 : 302234730 : va_start(vl, num_args);
95 [ + + ]: 578259081 : for (i = 0; i < tpoint->num_args; ++i) {
96 : 276024351 : argument = &tpoint->args[i];
97 [ + + - ]: 276024351 : switch (argument->type) {
98 : 40913 : case SPDK_TRACE_ARG_TYPE_STR:
99 : 40913 : argval = va_arg(vl, void *);
100 [ - + ]: 40913 : arglen = strnlen((const char *)argval, argument->size - 1) + 1;
101 : 40913 : break;
102 : 275983438 : case SPDK_TRACE_ARG_TYPE_INT:
103 : : case SPDK_TRACE_ARG_TYPE_PTR:
104 [ + + ]: 275983438 : if (argument->size == 8) {
105 : 233073263 : intval = va_arg(vl, uint64_t);
106 : : } else {
107 : 42910175 : intval = va_arg(vl, uint32_t);
108 : : }
109 : 275983438 : argval = &intval;
110 : 275983438 : arglen = argument->size;
111 : 275983438 : break;
112 : 0 : default:
113 : 0 : assert(0 && "Invalid trace argument type");
114 : : return;
115 : : }
116 : :
117 : : /* Copy argument's data. For some argument types (strings) user is allowed to pass a
118 : : * value that is either larger or smaller than what's defined in the tracepoint's
119 : : * description. If the value is larger, we'll truncate it, while if it's smaller,
120 : : * we'll only fill portion of the buffer, without touching the rest. For instance,
121 : : * if the definition marks an argument as 40B and user passes 12B string, we'll only
122 : : * copy 13B (accounting for the NULL terminator).
123 : : */
124 : 276024351 : argoff = 0;
125 [ + + ]: 552364042 : while (argoff < argument->size) {
126 : : /* Current buffer is full, we need to acquire another one */
127 [ + + ]: 276339691 : if (spdk_unlikely(offset == sizeof(buffer->data))) {
128 : 41408283 : buffer = (struct spdk_trace_entry_buffer *) get_trace_entry(
129 : : lcore_history,
130 : 41408283 : lcore_history->next_entry + num_entries);
131 : 41408283 : buffer->tpoint_id = SPDK_TRACE_MAX_TPOINT_ID;
132 : 41408283 : buffer->tsc = tsc;
133 : 41408283 : num_entries++;
134 : 41408283 : offset = 0;
135 : : }
136 : :
137 : 276339691 : curlen = spdk_min(sizeof(buffer->data) - offset, argument->size - argoff);
138 [ + + ]: 276339691 : if (spdk_likely(argoff < arglen)) {
139 [ - + ]: 276298753 : assert(argval != NULL);
140 [ - + - + ]: 276298753 : memcpy(&buffer->data[offset], (uint8_t *)argval + argoff,
141 : 276298753 : spdk_min(curlen, arglen - argoff));
142 : : }
143 : :
144 : 276339691 : offset += curlen;
145 : 276339691 : argoff += curlen;
146 : : }
147 : :
148 : : /* Make sure that truncated strings are NULL-terminated */
149 [ + + ]: 276024351 : if (spdk_unlikely(argument->type == SPDK_TRACE_ARG_TYPE_STR)) {
150 [ - + ]: 40913 : assert(offset > 0);
151 : 40913 : buffer->data[offset - 1] = '\0';
152 : : }
153 : : }
154 : 302234730 : va_end(vl);
155 : :
156 : : /* Ensure all elements of the trace entry are visible to outside trace tools */
157 : 302234730 : spdk_smp_wmb();
158 : 302234730 : lcore_history->next_entry += num_entries;
159 : : }
160 : :
161 : : int
162 : 0 : spdk_trace_register_user_thread(void)
163 : : {
164 : : int ret;
165 : : uint32_t ut_index;
166 : : pthread_t tid;
167 : :
168 [ # # ]: 0 : if (!g_ut_array) {
169 : 0 : SPDK_ERRLOG("user thread array not created\n");
170 : 0 : return -ENOMEM;
171 : : }
172 : :
173 [ # # ]: 0 : if (spdk_env_get_current_core() != SPDK_ENV_LCORE_ID_ANY) {
174 : 0 : SPDK_ERRLOG("cannot register an user thread from a dedicated cpu %d\n",
175 : : spdk_env_get_current_core());
176 : 0 : return -EINVAL;
177 : : }
178 : :
179 : 0 : pthread_mutex_lock(&g_ut_array_mutex);
180 : :
181 : 0 : t_ut_array_index = spdk_bit_array_find_first_clear(g_ut_array, 0);
182 [ # # ]: 0 : if (t_ut_array_index == UINT32_MAX) {
183 : 0 : SPDK_ERRLOG("could not find an entry in the user thread array\n");
184 : 0 : pthread_mutex_unlock(&g_ut_array_mutex);
185 : 0 : return -ENOENT;
186 : : }
187 : :
188 : 0 : ut_index = t_ut_array_index + g_user_thread_index_start;
189 : :
190 : 0 : t_ut_lcore_history = spdk_get_per_lcore_history(g_trace_file, ut_index);
191 : :
192 [ # # ]: 0 : assert(t_ut_lcore_history != NULL);
193 : :
194 [ # # ]: 0 : memset(g_trace_file->tname[ut_index], 0, SPDK_TRACE_THREAD_NAME_LEN);
195 : :
196 : 0 : tid = pthread_self();
197 [ # # ]: 0 : ret = pthread_getname_np(tid, g_trace_file->tname[ut_index], SPDK_TRACE_THREAD_NAME_LEN);
198 [ # # ]: 0 : if (ret) {
199 : 0 : SPDK_ERRLOG("cannot get thread name\n");
200 : 0 : pthread_mutex_unlock(&g_ut_array_mutex);
201 : 0 : return ret;
202 : : }
203 : :
204 : 0 : spdk_bit_array_set(g_ut_array, t_ut_array_index);
205 : :
206 : 0 : pthread_mutex_unlock(&g_ut_array_mutex);
207 : :
208 : 0 : return 0;
209 : : }
210 : :
211 : : int
212 : 0 : spdk_trace_unregister_user_thread(void)
213 : : {
214 [ # # ]: 0 : if (!g_ut_array) {
215 : 0 : SPDK_ERRLOG("user thread array not created\n");
216 : 0 : return -ENOMEM;
217 : : }
218 : :
219 [ # # ]: 0 : if (spdk_env_get_current_core() != SPDK_ENV_LCORE_ID_ANY) {
220 : 0 : SPDK_ERRLOG("cannot unregister an user thread from a dedicated cpu %d\n",
221 : : spdk_env_get_current_core());
222 : 0 : return -EINVAL;
223 : : }
224 : :
225 [ # # ]: 0 : pthread_mutex_lock(&g_ut_array_mutex);
226 : :
227 : 0 : spdk_bit_array_clear(g_ut_array, t_ut_array_index);
228 : :
229 [ # # ]: 0 : pthread_mutex_unlock(&g_ut_array_mutex);
230 : :
231 : 0 : return 0;
232 : : }
233 : :
234 : : int
235 : 2717 : spdk_trace_init(const char *shm_name, uint64_t num_entries, uint32_t num_threads)
236 : : {
237 : 2717 : uint32_t i = 0, max_dedicated_cpu = 0;
238 : : uint64_t file_size;
239 : 2717 : uint64_t lcore_offsets[SPDK_TRACE_MAX_LCORE] = { 0 };
240 : : uint64_t owner_offset;
241 : 2717 : struct spdk_cpuset cpuset = {};
242 : :
243 : : /* 0 entries requested - skip trace initialization */
244 [ - + ]: 2717 : if (num_entries == 0) {
245 : 0 : return 0;
246 : : }
247 : :
248 [ - + ]: 2717 : if (num_threads >= SPDK_TRACE_MAX_LCORE) {
249 : 0 : SPDK_ERRLOG("cannot alloc trace entries for %d user threads\n", num_threads);
250 : 0 : SPDK_ERRLOG("supported maximum %d threads\n", SPDK_TRACE_MAX_LCORE - 1);
251 : 0 : return 1;
252 : : }
253 : :
254 : 2717 : spdk_cpuset_zero(&cpuset);
255 : 2717 : file_size = sizeof(struct spdk_trace_file);
256 [ + + ]: 6489 : SPDK_ENV_FOREACH_CORE(i) {
257 : 3772 : spdk_cpuset_set_cpu(&cpuset, i, true);
258 : 3772 : lcore_offsets[i] = file_size;
259 : 3772 : file_size += spdk_get_trace_history_size(num_entries);
260 : 3772 : max_dedicated_cpu = i;
261 : : }
262 : :
263 : 2717 : g_user_thread_index_start = max_dedicated_cpu + 1;
264 : :
265 [ - + ]: 2717 : if (g_user_thread_index_start + num_threads > SPDK_TRACE_MAX_LCORE) {
266 : 0 : SPDK_ERRLOG("user threads overlap with the threads on dedicated cpus\n");
267 : 0 : return 1;
268 : : }
269 : :
270 : 2717 : g_ut_array = spdk_bit_array_create(num_threads);
271 [ - + ]: 2717 : if (!g_ut_array) {
272 : 0 : SPDK_ERRLOG("could not create bit array for threads\n");
273 : 0 : return 1;
274 : : }
275 : :
276 [ - + ]: 2717 : for (i = g_user_thread_index_start; i < g_user_thread_index_start + num_threads; i++) {
277 : 0 : lcore_offsets[i] = file_size;
278 : 0 : file_size += spdk_get_trace_history_size(num_entries);
279 : : }
280 : 2717 : owner_offset = file_size;
281 : 2717 : file_size += TRACE_NUM_OWNERS *
282 : : (sizeof(struct spdk_trace_owner) + TRACE_OWNER_DESCRIPTION_SIZE);
283 : :
284 : 2717 : snprintf(g_shm_name, sizeof(g_shm_name), "%s", shm_name);
285 : :
286 : 2717 : g_trace_fd = shm_open(shm_name, O_RDWR | O_CREAT, 0600);
287 [ - + ]: 2717 : if (g_trace_fd == -1) {
288 : 0 : SPDK_ERRLOG("could not shm_open spdk_trace\n");
289 : 0 : SPDK_ERRLOG("errno=%d %s\n", errno, spdk_strerror(errno));
290 : 0 : spdk_bit_array_free(&g_ut_array);
291 : 0 : return 1;
292 : : }
293 : :
294 [ - + ]: 2717 : if (ftruncate(g_trace_fd, file_size) != 0) {
295 : 0 : SPDK_ERRLOG("could not truncate shm\n");
296 : 0 : goto trace_init_err;
297 : : }
298 : :
299 : 2717 : g_trace_file = mmap(NULL, file_size, PROT_READ | PROT_WRITE,
300 : : MAP_SHARED, g_trace_fd, 0);
301 [ - + ]: 2717 : if (g_trace_file == MAP_FAILED) {
302 : 0 : SPDK_ERRLOG("could not mmap shm\n");
303 : 0 : goto trace_init_err;
304 : : }
305 : :
306 : : /* TODO: On FreeBSD, mlock on shm_open'd memory doesn't seem to work. Docs say that kern.ipc.shm_use_phys=1
307 : : * should allow it, but forcing that doesn't seem to work either. So for now just skip mlock on FreeBSD
308 : : * altogether.
309 : : */
310 : : #if defined(__linux__)
311 [ - + ]: 2717 : if (mlock(g_trace_file, file_size) != 0) {
312 : 0 : SPDK_ERRLOG("Could not mlock shm for tracing - %s.\n", spdk_strerror(errno));
313 [ # # ]: 0 : if (errno == ENOMEM) {
314 : 0 : SPDK_ERRLOG("Check /dev/shm for old tracing files that can be deleted.\n");
315 : : }
316 : 0 : goto trace_init_err;
317 : : }
318 : : #endif
319 : :
320 [ - + ]: 2717 : memset(g_trace_file, 0, file_size);
321 : :
322 : 2717 : g_trace_file->tsc_rate = spdk_get_ticks_hz();
323 : :
324 [ + + ]: 2784925 : for (i = 0; i < SPDK_TRACE_MAX_LCORE; i++) {
325 : : struct spdk_trace_history *lcore_history;
326 : :
327 : 2782208 : g_trace_file->lcore_history_offsets[i] = lcore_offsets[i];
328 [ + + ]: 2782208 : if (lcore_offsets[i] == 0) {
329 : 2778436 : continue;
330 : : }
331 : :
332 [ + - ]: 3772 : if (i <= max_dedicated_cpu) {
333 [ - + ]: 3772 : assert(spdk_cpuset_get_cpu(&cpuset, i));
334 : : }
335 : :
336 : 3772 : lcore_history = spdk_get_per_lcore_history(g_trace_file, i);
337 : 3772 : lcore_history->lcore = i;
338 : 3772 : lcore_history->num_entries = num_entries;
339 : : }
340 : 2717 : g_trace_file->file_size = file_size;
341 : 2717 : g_trace_file->num_owners = TRACE_NUM_OWNERS;
342 : 2717 : g_trace_file->owner_description_size = TRACE_OWNER_DESCRIPTION_SIZE;
343 : 2717 : g_trace_file->owner_offset = owner_offset;
344 : :
345 [ - + ]: 2717 : if (trace_flags_init()) {
346 : 0 : goto trace_init_err;
347 : : }
348 : :
349 : 2717 : return 0;
350 : :
351 : 0 : trace_init_err:
352 [ # # ]: 0 : if (g_trace_file != MAP_FAILED) {
353 : 0 : munmap(g_trace_file, file_size);
354 : : }
355 : 0 : close(g_trace_fd);
356 : 0 : g_trace_fd = -1;
357 : 0 : shm_unlink(shm_name);
358 : 0 : spdk_bit_array_free(&g_ut_array);
359 : 0 : g_trace_file = NULL;
360 : :
361 : 0 : return 1;
362 : :
363 : : }
364 : :
365 : : void
366 : 2789 : spdk_trace_cleanup(void)
367 : : {
368 : 2789 : bool unlink = true;
369 : : int i;
370 : : struct spdk_trace_history *lcore_history;
371 : :
372 [ + + ]: 2789 : if (g_trace_file == NULL) {
373 : 72 : return;
374 : : }
375 : :
376 : 2717 : trace_flags_fini();
377 : :
378 : : /*
379 : : * Only unlink the shm if there were no trace_entry recorded. This ensures the file
380 : : * can be used after this process exits/crashes for debugging.
381 : : * Note that we have to calculate this value before g_trace_file gets unmapped.
382 : : */
383 [ + + ]: 2555643 : for (i = 0; i < SPDK_TRACE_MAX_LCORE; i++) {
384 : 2553150 : lcore_history = spdk_get_per_lcore_history(g_trace_file, i);
385 [ + + ]: 2553150 : if (lcore_history == NULL) {
386 : 2549692 : continue;
387 : : }
388 : 3458 : unlink = lcore_history->entries[0].tsc == 0;
389 [ + + ]: 3458 : if (!unlink) {
390 : 224 : break;
391 : : }
392 : : }
393 : :
394 : 2717 : munmap(g_trace_file, sizeof(struct spdk_trace_file));
395 : 2717 : g_trace_file = NULL;
396 : 2717 : close(g_trace_fd);
397 : 2717 : spdk_bit_array_free(&g_ut_array);
398 : :
399 [ + + ]: 2717 : if (unlink) {
400 : 2493 : shm_unlink(g_shm_name);
401 : : }
402 : : }
403 : :
404 : : const char *
405 : 19 : trace_get_shm_name(void)
406 : : {
407 : 19 : return g_shm_name;
408 : : }
|