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