xref: /spdk/lib/trace_parser/trace.cpp (revision 927f1fd57bd004df581518466ec4c1b8083e5d23)
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 object_stats {
85 	std::map<uint64_t, uint64_t>	index;
86 	std::map<uint64_t, uint64_t>	start;
87 	uint64_t			counter;
88 
89 	object_stats() : counter(0) {}
90 };
91 
92 struct spdk_trace_parser {
93 	spdk_trace_parser(const spdk_trace_parser_opts *opts);
94 	~spdk_trace_parser();
95 	spdk_trace_parser(const spdk_trace_parser &) = delete;
96 	spdk_trace_parser &operator=(const spdk_trace_parser &) = delete;
97 	const spdk_trace_flags *flags() const { return &_histories->flags; }
98 	uint64_t tsc_offset() const { return _tsc_offset; }
99 	bool next_entry(spdk_trace_parser_entry *entry);
100 	uint64_t entry_count(uint16_t lcore) const;
101 private:
102 	spdk_trace_entry_buffer *get_next_buffer(spdk_trace_entry_buffer *buf, uint16_t lcore);
103 	bool build_arg(argument_context *argctx, const spdk_trace_argument *arg, int argid,
104 		       spdk_trace_parser_entry *pe);
105 	void populate_events(spdk_trace_history *history, int num_entries);
106 	bool init(const spdk_trace_parser_opts *opts);
107 	void cleanup();
108 
109 	spdk_trace_histories	*_histories;
110 	size_t			_map_size;
111 	int			_fd;
112 	uint64_t		_tsc_offset;
113 	entry_map		_entries;
114 	entry_map::iterator	_iter;
115 	object_stats		_stats[SPDK_TRACE_MAX_OBJECT];
116 };
117 
118 uint64_t
119 spdk_trace_parser::entry_count(uint16_t lcore) const
120 {
121 	spdk_trace_history *history;
122 
123 	if (lcore >= SPDK_TRACE_MAX_LCORE) {
124 		return 0;
125 	}
126 
127 	history = spdk_get_per_lcore_history(_histories, lcore);
128 	assert(history);
129 
130 	return history->num_entries;
131 }
132 
133 spdk_trace_entry_buffer *
134 spdk_trace_parser::get_next_buffer(spdk_trace_entry_buffer *buf, uint16_t lcore)
135 {
136 	spdk_trace_history *history;
137 
138 	history = spdk_get_per_lcore_history(_histories, lcore);
139 	assert(history);
140 
141 	if (spdk_unlikely(static_cast<void *>(buf) ==
142 			  static_cast<void *>(&history->entries[history->num_entries - 1]))) {
143 		return reinterpret_cast<spdk_trace_entry_buffer *>(&history->entries[0]);
144 	} else {
145 		return buf + 1;
146 	}
147 }
148 
149 bool
150 spdk_trace_parser::build_arg(argument_context *argctx, const spdk_trace_argument *arg, int argid,
151 			     spdk_trace_parser_entry *pe)
152 {
153 	spdk_trace_entry *entry = argctx->entry;
154 	spdk_trace_entry_buffer *buffer = argctx->buffer;
155 	size_t curlen, argoff;
156 
157 	argoff = 0;
158 	while (argoff < arg->size) {
159 		if (argctx->offset == sizeof(buffer->data)) {
160 			buffer = get_next_buffer(buffer, argctx->lcore);
161 			if (spdk_unlikely(buffer->tpoint_id != SPDK_TRACE_MAX_TPOINT_ID ||
162 					  buffer->tsc != entry->tsc)) {
163 				return false;
164 			}
165 
166 			argctx->offset = 0;
167 			argctx->buffer = buffer;
168 		}
169 
170 		curlen = spdk_min(sizeof(buffer->data) - argctx->offset, arg->size - argoff);
171 		if (argoff < sizeof(pe->args[0])) {
172 			memcpy(&pe->args[argid].string[argoff], &buffer->data[argctx->offset],
173 			       spdk_min(curlen, sizeof(pe->args[0]) - argoff));
174 		}
175 
176 		argctx->offset += curlen;
177 		argoff += curlen;
178 	}
179 
180 	return true;
181 }
182 
183 bool
184 spdk_trace_parser::next_entry(spdk_trace_parser_entry *pe)
185 {
186 	spdk_trace_tpoint *tpoint;
187 	spdk_trace_entry *entry;
188 	object_stats *stats;
189 	std::map<uint64_t, uint64_t>::iterator related_kv;
190 
191 	if (_iter == _entries.end()) {
192 		return false;
193 	}
194 
195 	pe->entry = entry = _iter->second;
196 	pe->lcore = _iter->first.lcore;
197 	/* Set related index to the max value to indicate "empty" state */
198 	pe->related_index = UINT64_MAX;
199 	pe->related_type = OBJECT_NONE;
200 	tpoint = &_histories->flags.tpoint[entry->tpoint_id];
201 	stats = &_stats[tpoint->object_type];
202 
203 	if (tpoint->new_object) {
204 		stats->index[entry->object_id] = stats->counter++;
205 		stats->start[entry->object_id] = entry->tsc;
206 	}
207 
208 	if (tpoint->object_type != OBJECT_NONE) {
209 		if (spdk_likely(stats->start.find(entry->object_id) != stats->start.end())) {
210 			pe->object_index = stats->index[entry->object_id];
211 			pe->object_start = stats->start[entry->object_id];
212 		} else {
213 			pe->object_index = UINT64_MAX;
214 			pe->object_start = UINT64_MAX;
215 		}
216 	}
217 
218 	argument_context argctx(entry, pe->lcore);
219 	for (uint8_t i = 0; i < tpoint->num_args; ++i) {
220 		if (!build_arg(&argctx, &tpoint->args[i], i, pe)) {
221 			SPDK_ERRLOG("Failed to parse tracepoint argument\n");
222 			return false;
223 		}
224 	}
225 
226 	for (uint8_t i = 0; i < SPDK_TRACE_MAX_RELATIONS; ++i) {
227 		/* The relations are stored inside a tpoint, which means there might be
228 		 * multiple objects bound to a single tpoint. */
229 		if (tpoint->related_objects[i].object_type == OBJECT_NONE) {
230 			break;
231 		}
232 		stats = &_stats[tpoint->related_objects[i].object_type];
233 		related_kv = stats->index.find(reinterpret_cast<uint64_t>
234 					       (pe->args[tpoint->related_objects[i].arg_index].pointer));
235 		/* To avoid parsing the whole array, object index and type are stored
236 		 * directly inside spdk_trace_parser_entry. */
237 		if (related_kv != stats->index.end()) {
238 			pe->related_index = related_kv->second;
239 			pe->related_type = tpoint->related_objects[i].object_type;
240 			break;
241 		}
242 	}
243 
244 	_iter++;
245 	return true;
246 }
247 
248 void
249 spdk_trace_parser::populate_events(spdk_trace_history *history, int num_entries)
250 {
251 	int i, num_entries_filled;
252 	spdk_trace_entry *e;
253 	int first, last, lcore;
254 
255 	lcore = history->lcore;
256 	e = history->entries;
257 
258 	num_entries_filled = num_entries;
259 	while (e[num_entries_filled - 1].tsc == 0) {
260 		num_entries_filled--;
261 	}
262 
263 	if (num_entries == num_entries_filled) {
264 		first = last = 0;
265 		for (i = 1; i < num_entries; i++) {
266 			if (e[i].tsc < e[first].tsc) {
267 				first = i;
268 			}
269 			if (e[i].tsc > e[last].tsc) {
270 				last = i;
271 			}
272 		}
273 	} else {
274 		first = 0;
275 		last = num_entries_filled - 1;
276 	}
277 
278 	/*
279 	 * We keep track of the highest first TSC out of all reactors.
280 	 *  We will ignore any events that occurred before this TSC on any
281 	 *  other reactors.  This will ensure we only print data for the
282 	 *  subset of time where we have data across all reactors.
283 	 */
284 	if (e[first].tsc > _tsc_offset) {
285 		_tsc_offset = e[first].tsc;
286 	}
287 
288 	i = first;
289 	while (1) {
290 		if (e[i].tpoint_id != SPDK_TRACE_MAX_TPOINT_ID) {
291 			_entries[entry_key(lcore, e[i].tsc)] = &e[i];
292 		}
293 		if (i == last) {
294 			break;
295 		}
296 		i++;
297 		if (i == num_entries_filled) {
298 			i = 0;
299 		}
300 	}
301 }
302 
303 bool
304 spdk_trace_parser::init(const spdk_trace_parser_opts *opts)
305 {
306 	spdk_trace_history *history;
307 	struct stat st;
308 	int rc, i;
309 
310 	switch (opts->mode) {
311 	case SPDK_TRACE_PARSER_MODE_FILE:
312 		_fd = open(opts->filename, O_RDONLY);
313 		break;
314 	case SPDK_TRACE_PARSER_MODE_SHM:
315 		_fd = shm_open(opts->filename, O_RDONLY, 0600);
316 		break;
317 	default:
318 		SPDK_ERRLOG("Invalid mode: %d\n", opts->mode);
319 		return false;
320 	}
321 
322 	if (_fd < 0) {
323 		SPDK_ERRLOG("Could not open trace file: %s (%d)\n", opts->filename, errno);
324 		return false;
325 	}
326 
327 	rc = fstat(_fd, &st);
328 	if (rc < 0) {
329 		SPDK_ERRLOG("Could not get size of trace file: %s\n", opts->filename);
330 		return false;
331 	}
332 
333 	if ((size_t)st.st_size < sizeof(*_histories)) {
334 		SPDK_ERRLOG("Invalid trace file: %s\n", opts->filename);
335 		return false;
336 	}
337 
338 	/* Map the header of trace file */
339 	_map_size = sizeof(*_histories);
340 	_histories = static_cast<spdk_trace_histories *>(mmap(NULL, _map_size, PROT_READ,
341 			MAP_SHARED, _fd, 0));
342 	if (_histories == MAP_FAILED) {
343 		SPDK_ERRLOG("Could not mmap trace file: %s\n", opts->filename);
344 		_histories = NULL;
345 		return false;
346 	}
347 
348 	/* Remap the entire trace file */
349 	_map_size = spdk_get_trace_histories_size(_histories);
350 	munmap(_histories, sizeof(*_histories));
351 	if ((size_t)st.st_size < _map_size) {
352 		SPDK_ERRLOG("Trace file %s is not valid\n", opts->filename);
353 		_histories = NULL;
354 		return false;
355 	}
356 	_histories = static_cast<spdk_trace_histories *>(mmap(NULL, _map_size, PROT_READ,
357 			MAP_SHARED, _fd, 0));
358 	if (_histories == MAP_FAILED) {
359 		SPDK_ERRLOG("Could not mmap trace file: %s\n", opts->filename);
360 		_histories = NULL;
361 		return false;
362 	}
363 
364 	if (opts->lcore == SPDK_TRACE_MAX_LCORE) {
365 		for (i = 0; i < SPDK_TRACE_MAX_LCORE; i++) {
366 			history = spdk_get_per_lcore_history(_histories, i);
367 			assert(history);
368 			if (history->num_entries == 0 || history->entries[0].tsc == 0) {
369 				continue;
370 			}
371 
372 			populate_events(history, history->num_entries);
373 		}
374 	} else {
375 		history = spdk_get_per_lcore_history(_histories, opts->lcore);
376 		assert(history);
377 		if (history->num_entries > 0 && history->entries[0].tsc != 0) {
378 			populate_events(history, history->num_entries);
379 		}
380 	}
381 
382 	_iter = _entries.begin();
383 	return true;
384 }
385 
386 void
387 spdk_trace_parser::cleanup()
388 {
389 	if (_histories != NULL) {
390 		munmap(_histories, _map_size);
391 	}
392 
393 	if (_fd > 0) {
394 		close(_fd);
395 	}
396 }
397 
398 spdk_trace_parser::spdk_trace_parser(const spdk_trace_parser_opts *opts) :
399 	_histories(NULL),
400 	_map_size(0),
401 	_fd(-1),
402 	_tsc_offset(0)
403 {
404 	if (!init(opts)) {
405 		cleanup();
406 		throw std::exception();
407 	}
408 }
409 
410 spdk_trace_parser::~spdk_trace_parser()
411 {
412 	cleanup();
413 }
414 
415 struct spdk_trace_parser *
416 spdk_trace_parser_init(const struct spdk_trace_parser_opts *opts)
417 {
418 	try {
419 		return new spdk_trace_parser(opts);
420 	} catch (...) {
421 		return NULL;
422 	}
423 }
424 
425 void
426 spdk_trace_parser_cleanup(struct spdk_trace_parser *parser)
427 {
428 	delete parser;
429 }
430 
431 const struct spdk_trace_flags *
432 spdk_trace_parser_get_flags(const struct spdk_trace_parser *parser)
433 {
434 	return parser->flags();
435 }
436 
437 uint64_t
438 spdk_trace_parser_get_tsc_offset(const struct spdk_trace_parser *parser)
439 {
440 	return parser->tsc_offset();
441 }
442 
443 bool
444 spdk_trace_parser_next_entry(struct spdk_trace_parser *parser,
445 			     struct spdk_trace_parser_entry *entry)
446 {
447 	return parser->next_entry(entry);
448 }
449 
450 uint64_t
451 spdk_trace_parser_get_entry_count(const struct spdk_trace_parser *parser, uint16_t lcore)
452 {
453 	return parser->entry_count(lcore);
454 }
455