xref: /spdk/lib/trace/trace.c (revision 9889ab2dc80e40dae92dcef361d53dcba722043d)
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 #include "spdk/util.h"
40 #include "spdk/barrier.h"
41 
42 static int g_trace_fd = -1;
43 static char g_shm_name[64];
44 
45 struct spdk_trace_histories *g_trace_histories;
46 
47 void
48 _spdk_trace_record(uint64_t tsc, 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 	unsigned lcore;
54 	uint64_t next_circular_entry;
55 
56 	lcore = spdk_env_get_current_core();
57 	if (lcore >= SPDK_TRACE_MAX_LCORE) {
58 		return;
59 	}
60 
61 	lcore_history = spdk_get_per_lcore_history(g_trace_histories, lcore);
62 	if (tsc == 0) {
63 		tsc = spdk_get_ticks();
64 	}
65 
66 	lcore_history->tpoint_count[tpoint_id]++;
67 
68 	/* Get next entry index in the circular buffer */
69 	next_circular_entry = lcore_history->next_entry & (lcore_history->num_entries - 1);
70 	next_entry = &lcore_history->entries[next_circular_entry];
71 	next_entry->tsc = tsc;
72 	next_entry->tpoint_id = tpoint_id;
73 	next_entry->poller_id = poller_id;
74 	next_entry->size = size;
75 	next_entry->object_id = object_id;
76 	next_entry->arg1 = arg1;
77 
78 	/* Ensure all elements of the trace entry are visible to outside trace tools */
79 	spdk_smp_wmb();
80 	lcore_history->next_entry++;
81 }
82 
83 int
84 spdk_trace_init(const char *shm_name, uint64_t num_entries)
85 {
86 	int i = 0;
87 	int histories_size;
88 	uint64_t lcore_offsets[SPDK_TRACE_MAX_LCORE + 1];
89 
90 	lcore_offsets[0] = sizeof(struct spdk_trace_flags);
91 	for (i = 1; i < (int)SPDK_COUNTOF(lcore_offsets); i++) {
92 		lcore_offsets[i] = spdk_get_trace_history_size(num_entries) + lcore_offsets[i - 1];
93 	}
94 	histories_size = sizeof(struct spdk_trace_flags) + lcore_offsets[SPDK_TRACE_MAX_LCORE];
95 
96 	snprintf(g_shm_name, sizeof(g_shm_name), "%s", shm_name);
97 
98 	g_trace_fd = shm_open(shm_name, O_RDWR | O_CREAT, 0600);
99 	if (g_trace_fd == -1) {
100 		fprintf(stderr, "could not shm_open spdk_trace\n");
101 		fprintf(stderr, "errno=%d %s\n", errno, spdk_strerror(errno));
102 		return 1;
103 	}
104 
105 	if (ftruncate(g_trace_fd, histories_size) != 0) {
106 		fprintf(stderr, "could not truncate shm\n");
107 		goto trace_init_err;
108 	}
109 
110 	g_trace_histories = mmap(NULL, histories_size, PROT_READ | PROT_WRITE,
111 				 MAP_SHARED, g_trace_fd, 0);
112 	if (g_trace_histories == MAP_FAILED) {
113 		fprintf(stderr, "could not mmap shm\n");
114 		goto trace_init_err;
115 	}
116 
117 	/* TODO: On FreeBSD, mlock on shm_open'd memory doesn't seem to work.  Docs say that kern.ipc.shm_use_phys=1
118 	 * should allow it, but forcing that doesn't seem to work either.  So for now just skip mlock on FreeBSD
119 	 * altogether.
120 	 */
121 #if defined(__linux__)
122 	if (mlock(g_trace_histories, histories_size) != 0) {
123 		fprintf(stderr, "Could not mlock shm for tracing - %s.\n", spdk_strerror(errno));
124 		if (errno == ENOMEM) {
125 			fprintf(stderr, "Check /dev/shm for old tracing files that can be deleted.\n");
126 		}
127 		goto trace_init_err;
128 	}
129 #endif
130 
131 	memset(g_trace_histories, 0, histories_size);
132 
133 	g_trace_flags = &g_trace_histories->flags;
134 
135 	g_trace_flags->tsc_rate = spdk_get_ticks_hz();
136 
137 	for (i = 0; i < SPDK_TRACE_MAX_LCORE; i++) {
138 		struct spdk_trace_history *lcore_history;
139 
140 		g_trace_flags->lcore_history_offsets[i] = lcore_offsets[i];
141 		lcore_history = spdk_get_per_lcore_history(g_trace_histories, i);
142 		lcore_history->lcore = i;
143 		lcore_history->num_entries = num_entries;
144 	}
145 	g_trace_flags->lcore_history_offsets[SPDK_TRACE_MAX_LCORE] = lcore_offsets[SPDK_TRACE_MAX_LCORE];
146 
147 	spdk_trace_flags_init();
148 
149 	return 0;
150 
151 trace_init_err:
152 	if (g_trace_histories != MAP_FAILED) {
153 		munmap(g_trace_histories, histories_size);
154 	}
155 	close(g_trace_fd);
156 	g_trace_fd = -1;
157 	shm_unlink(shm_name);
158 	g_trace_histories = NULL;
159 
160 	return 1;
161 
162 }
163 
164 void
165 spdk_trace_cleanup(void)
166 {
167 	bool unlink;
168 	int i;
169 	struct spdk_trace_history *lcore_history;
170 
171 	if (g_trace_histories == NULL) {
172 		return;
173 	}
174 
175 	/*
176 	 * Only unlink the shm if there were no trace_entry recorded. This ensures the file
177 	 * can be used after this process exits/crashes for debugging.
178 	 * Note that we have to calculate this value before g_trace_histories gets unmapped.
179 	 */
180 	for (i = 0; i < SPDK_TRACE_MAX_LCORE; i++) {
181 		lcore_history = spdk_get_per_lcore_history(g_trace_histories, i);
182 		unlink = lcore_history->entries[0].tsc == 0;
183 		if (!unlink) {
184 			break;
185 		}
186 	}
187 
188 	munmap(g_trace_histories, sizeof(struct spdk_trace_histories));
189 	g_trace_histories = NULL;
190 	close(g_trace_fd);
191 
192 	if (unlink) {
193 		shm_unlink(g_shm_name);
194 	}
195 }
196