xref: /spdk/lib/trace_parser/trace.cpp (revision 189b0f092069b767d2f02e7e85e7c00939b46ba3)
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 #include "spdk/likely.h"
36 #include "spdk/log.h"
37 #include "spdk/trace_parser.h"
38 #include "spdk/util.h"
39 
40 #include <exception>
41 #include <map>
42 #include <new>
43 
44 struct entry_key {
45 	entry_key(uint16_t _lcore, uint64_t _tsc) : lcore(_lcore), tsc(_tsc) {}
46 	uint16_t lcore;
47 	uint64_t tsc;
48 };
49 
50 class compare_entry_key
51 {
52 public:
53 	bool operator()(const entry_key &first, const entry_key &second) const
54 	{
55 		if (first.tsc == second.tsc) {
56 			return first.lcore < second.lcore;
57 		} else {
58 			return first.tsc < second.tsc;
59 		}
60 	}
61 };
62 
63 typedef std::map<entry_key, spdk_trace_entry *, compare_entry_key> entry_map;
64 
65 struct argument_context {
66 	spdk_trace_entry	*entry;
67 	spdk_trace_entry_buffer	*buffer;
68 	uint16_t		lcore;
69 	size_t			offset;
70 
71 	argument_context(spdk_trace_entry *entry, uint16_t lcore) :
72 		entry(entry), lcore(lcore)
73 	{
74 		buffer = reinterpret_cast<spdk_trace_entry_buffer *>(entry);
75 
76 		/* The first argument resides within the spdk_trace_entry structure, so the initial
77 		 * offset needs to be adjusted to the start of the spdk_trace_entry.args array
78 		 */
79 		offset = offsetof(spdk_trace_entry, args) -
80 			 offsetof(spdk_trace_entry_buffer, data);
81 	}
82 };
83 
84 struct spdk_trace_parser {
85 	spdk_trace_parser(const spdk_trace_parser_opts *opts);
86 	~spdk_trace_parser();
87 	spdk_trace_parser(const spdk_trace_parser &) = delete;
88 	spdk_trace_parser &operator=(const spdk_trace_parser &) = delete;
89 	const spdk_trace_flags *flags() const { return &_histories->flags; }
90 	uint64_t tsc_offset() const { return _tsc_offset; }
91 	bool next_entry(spdk_trace_parser_entry *entry);
92 	uint64_t entry_count(uint16_t lcore) const;
93 private:
94 	spdk_trace_entry_buffer *get_next_buffer(spdk_trace_entry_buffer *buf, uint16_t lcore);
95 	bool build_arg(argument_context *argctx, const spdk_trace_argument *arg, int argid,
96 		       spdk_trace_parser_entry *pe);
97 	void populate_events(spdk_trace_history *history, int num_entries);
98 	bool init(const spdk_trace_parser_opts *opts);
99 	void cleanup();
100 
101 	spdk_trace_histories	*_histories;
102 	size_t			_map_size;
103 	int			_fd;
104 	uint64_t		_tsc_offset;
105 	entry_map		_entries;
106 	entry_map::iterator	_iter;
107 };
108 
109 uint64_t
110 spdk_trace_parser::entry_count(uint16_t lcore) const
111 {
112 	spdk_trace_history *history;
113 
114 	if (lcore >= SPDK_TRACE_MAX_LCORE) {
115 		return 0;
116 	}
117 
118 	history = spdk_get_per_lcore_history(_histories, lcore);
119 	assert(history);
120 
121 	return history->num_entries;
122 }
123 
124 spdk_trace_entry_buffer *
125 spdk_trace_parser::get_next_buffer(spdk_trace_entry_buffer *buf, uint16_t lcore)
126 {
127 	spdk_trace_history *history;
128 
129 	history = spdk_get_per_lcore_history(_histories, lcore);
130 	assert(history);
131 
132 	if (spdk_unlikely(static_cast<void *>(buf) ==
133 			  static_cast<void *>(&history->entries[history->num_entries - 1]))) {
134 		return reinterpret_cast<spdk_trace_entry_buffer *>(&history->entries[0]);
135 	} else {
136 		return buf + 1;
137 	}
138 }
139 
140 bool
141 spdk_trace_parser::build_arg(argument_context *argctx, const spdk_trace_argument *arg, int argid,
142 			     spdk_trace_parser_entry *pe)
143 {
144 	spdk_trace_entry *entry = argctx->entry;
145 	spdk_trace_entry_buffer *buffer = argctx->buffer;
146 	size_t curlen, argoff;
147 
148 	argoff = 0;
149 	while (argoff < arg->size) {
150 		if (argctx->offset == sizeof(buffer->data)) {
151 			buffer = get_next_buffer(buffer, argctx->lcore);
152 			if (spdk_unlikely(buffer->tpoint_id != SPDK_TRACE_MAX_TPOINT_ID ||
153 					  buffer->tsc != entry->tsc)) {
154 				return false;
155 			}
156 
157 			argctx->offset = 0;
158 			argctx->buffer = buffer;
159 		}
160 
161 		curlen = spdk_min(sizeof(buffer->data) - argctx->offset, arg->size - argoff);
162 		if (argoff < sizeof(pe->args[0])) {
163 			memcpy(&pe->args[argid].string[argoff], &buffer->data[argctx->offset],
164 			       spdk_min(curlen, sizeof(pe->args[0]) - argoff));
165 		}
166 
167 		argctx->offset += curlen;
168 		argoff += curlen;
169 	}
170 
171 	return true;
172 }
173 
174 bool
175 spdk_trace_parser::next_entry(spdk_trace_parser_entry *pe)
176 {
177 	spdk_trace_tpoint *tpoint;
178 	spdk_trace_entry *entry;
179 
180 	if (_iter == _entries.end()) {
181 		return false;
182 	}
183 
184 	pe->entry = entry = _iter->second;
185 	pe->lcore = _iter->first.lcore;
186 	tpoint = &_histories->flags.tpoint[entry->tpoint_id];
187 
188 	argument_context argctx(entry, pe->lcore);
189 	for (uint8_t i = 0; i < tpoint->num_args; ++i) {
190 		if (!build_arg(&argctx, &tpoint->args[i], i, pe)) {
191 			SPDK_ERRLOG("Failed to parse tracepoint argument\n");
192 			return false;
193 		}
194 	}
195 
196 	_iter++;
197 	return true;
198 }
199 
200 void
201 spdk_trace_parser::populate_events(spdk_trace_history *history, int num_entries)
202 {
203 	int i, num_entries_filled;
204 	spdk_trace_entry *e;
205 	int first, last, lcore;
206 
207 	lcore = history->lcore;
208 	e = history->entries;
209 
210 	num_entries_filled = num_entries;
211 	while (e[num_entries_filled - 1].tsc == 0) {
212 		num_entries_filled--;
213 	}
214 
215 	if (num_entries == num_entries_filled) {
216 		first = last = 0;
217 		for (i = 1; i < num_entries; i++) {
218 			if (e[i].tsc < e[first].tsc) {
219 				first = i;
220 			}
221 			if (e[i].tsc > e[last].tsc) {
222 				last = i;
223 			}
224 		}
225 	} else {
226 		first = 0;
227 		last = num_entries_filled - 1;
228 	}
229 
230 	/*
231 	 * We keep track of the highest first TSC out of all reactors.
232 	 *  We will ignore any events that occured before this TSC on any
233 	 *  other reactors.  This will ensure we only print data for the
234 	 *  subset of time where we have data across all reactors.
235 	 */
236 	if (e[first].tsc > _tsc_offset) {
237 		_tsc_offset = e[first].tsc;
238 	}
239 
240 	i = first;
241 	while (1) {
242 		if (e[i].tpoint_id != SPDK_TRACE_MAX_TPOINT_ID) {
243 			_entries[entry_key(lcore, e[i].tsc)] = &e[i];
244 		}
245 		if (i == last) {
246 			break;
247 		}
248 		i++;
249 		if (i == num_entries_filled) {
250 			i = 0;
251 		}
252 	}
253 }
254 
255 bool
256 spdk_trace_parser::init(const spdk_trace_parser_opts *opts)
257 {
258 	spdk_trace_history *history;
259 	struct stat st;
260 	int rc, i;
261 
262 	switch (opts->mode) {
263 	case SPDK_TRACE_PARSER_MODE_FILE:
264 		_fd = open(opts->filename, O_RDONLY);
265 		break;
266 	case SPDK_TRACE_PARSER_MODE_SHM:
267 		_fd = shm_open(opts->filename, O_RDONLY, 0600);
268 		break;
269 	default:
270 		SPDK_ERRLOG("Invalid mode: %d\n", opts->mode);
271 		return false;
272 	}
273 
274 	if (_fd < 0) {
275 		SPDK_ERRLOG("Could not open trace file: %s (%d)\n", opts->filename, errno);
276 		return false;
277 	}
278 
279 	rc = fstat(_fd, &st);
280 	if (rc < 0) {
281 		SPDK_ERRLOG("Could not get size of trace file: %s\n", opts->filename);
282 		return false;
283 	}
284 
285 	if ((size_t)st.st_size < sizeof(*_histories)) {
286 		SPDK_ERRLOG("Invalid trace file: %s\n", opts->filename);
287 		return false;
288 	}
289 
290 	/* Map the header of trace file */
291 	_map_size = sizeof(*_histories);
292 	_histories = static_cast<spdk_trace_histories *>(mmap(NULL, _map_size, PROT_READ,
293 			MAP_SHARED, _fd, 0));
294 	if (_histories == MAP_FAILED) {
295 		SPDK_ERRLOG("Could not mmap trace file: %s\n", opts->filename);
296 		_histories = NULL;
297 		return false;
298 	}
299 
300 	/* Remap the entire trace file */
301 	_map_size = spdk_get_trace_histories_size(_histories);
302 	munmap(_histories, sizeof(*_histories));
303 	if ((size_t)st.st_size < _map_size) {
304 		SPDK_ERRLOG("Trace file %s is not valid\n", opts->filename);
305 		_histories = NULL;
306 		return false;
307 	}
308 	_histories = static_cast<spdk_trace_histories *>(mmap(NULL, _map_size, PROT_READ,
309 			MAP_SHARED, _fd, 0));
310 	if (_histories == MAP_FAILED) {
311 		SPDK_ERRLOG("Could not mmap trace file: %s\n", opts->filename);
312 		_histories = NULL;
313 		return false;
314 	}
315 
316 	if (opts->lcore == SPDK_TRACE_MAX_LCORE) {
317 		for (i = 0; i < SPDK_TRACE_MAX_LCORE; i++) {
318 			history = spdk_get_per_lcore_history(_histories, i);
319 			if (history->num_entries == 0 || history->entries[0].tsc == 0) {
320 				continue;
321 			}
322 
323 			populate_events(history, history->num_entries);
324 		}
325 	} else {
326 		history = spdk_get_per_lcore_history(_histories, opts->lcore);
327 		if (history->num_entries > 0 && history->entries[0].tsc != 0) {
328 			populate_events(history, history->num_entries);
329 		}
330 	}
331 
332 	_iter = _entries.begin();
333 	return true;
334 }
335 
336 void
337 spdk_trace_parser::cleanup()
338 {
339 	if (_histories != NULL) {
340 		munmap(_histories, _map_size);
341 	}
342 
343 	if (_fd > 0) {
344 		close(_fd);
345 	}
346 }
347 
348 spdk_trace_parser::spdk_trace_parser(const spdk_trace_parser_opts *opts) :
349 	_histories(NULL),
350 	_map_size(0),
351 	_fd(-1),
352 	_tsc_offset(0)
353 {
354 	if (!init(opts)) {
355 		cleanup();
356 		throw std::exception();
357 	}
358 }
359 
360 spdk_trace_parser::~spdk_trace_parser()
361 {
362 	cleanup();
363 }
364 
365 struct spdk_trace_parser *
366 spdk_trace_parser_init(const struct spdk_trace_parser_opts *opts)
367 {
368 	try {
369 		return new spdk_trace_parser(opts);
370 	} catch (...) {
371 		return NULL;
372 	}
373 }
374 
375 void
376 spdk_trace_parser_cleanup(struct spdk_trace_parser *parser)
377 {
378 	delete parser;
379 }
380 
381 const struct spdk_trace_flags *
382 spdk_trace_parser_get_flags(const struct spdk_trace_parser *parser)
383 {
384 	return parser->flags();
385 }
386 
387 uint64_t
388 spdk_trace_parser_get_tsc_offset(const struct spdk_trace_parser *parser)
389 {
390 	return parser->tsc_offset();
391 }
392 
393 bool
394 spdk_trace_parser_next_entry(struct spdk_trace_parser *parser,
395 			     struct spdk_trace_parser_entry *entry)
396 {
397 	return parser->next_entry(entry);
398 }
399 
400 uint64_t
401 spdk_trace_parser_get_entry_count(const struct spdk_trace_parser *parser, uint16_t lcore)
402 {
403 	return parser->entry_count(lcore);
404 }
405