xref: /spdk/lib/trace_parser/trace.cpp (revision 4265fc50d9ec1215e21c6960d82a224277169496)
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 
190 	if (_iter == _entries.end()) {
191 		return false;
192 	}
193 
194 	pe->entry = entry = _iter->second;
195 	pe->lcore = _iter->first.lcore;
196 	tpoint = &_histories->flags.tpoint[entry->tpoint_id];
197 	stats = &_stats[tpoint->object_type];
198 
199 	if (tpoint->new_object) {
200 		stats->index[entry->object_id] = stats->counter++;
201 		stats->start[entry->object_id] = entry->tsc;
202 	}
203 
204 	if (tpoint->object_type != OBJECT_NONE) {
205 		if (spdk_likely(stats->start.find(entry->object_id) != stats->start.end())) {
206 			pe->object_index = stats->index[entry->object_id];
207 			pe->object_start = stats->start[entry->object_id];
208 		} else {
209 			pe->object_index = UINT64_MAX;
210 			pe->object_start = UINT64_MAX;
211 		}
212 	}
213 
214 	argument_context argctx(entry, pe->lcore);
215 	for (uint8_t i = 0; i < tpoint->num_args; ++i) {
216 		if (!build_arg(&argctx, &tpoint->args[i], i, pe)) {
217 			SPDK_ERRLOG("Failed to parse tracepoint argument\n");
218 			return false;
219 		}
220 	}
221 
222 	_iter++;
223 	return true;
224 }
225 
226 void
227 spdk_trace_parser::populate_events(spdk_trace_history *history, int num_entries)
228 {
229 	int i, num_entries_filled;
230 	spdk_trace_entry *e;
231 	int first, last, lcore;
232 
233 	lcore = history->lcore;
234 	e = history->entries;
235 
236 	num_entries_filled = num_entries;
237 	while (e[num_entries_filled - 1].tsc == 0) {
238 		num_entries_filled--;
239 	}
240 
241 	if (num_entries == num_entries_filled) {
242 		first = last = 0;
243 		for (i = 1; i < num_entries; i++) {
244 			if (e[i].tsc < e[first].tsc) {
245 				first = i;
246 			}
247 			if (e[i].tsc > e[last].tsc) {
248 				last = i;
249 			}
250 		}
251 	} else {
252 		first = 0;
253 		last = num_entries_filled - 1;
254 	}
255 
256 	/*
257 	 * We keep track of the highest first TSC out of all reactors.
258 	 *  We will ignore any events that occured before this TSC on any
259 	 *  other reactors.  This will ensure we only print data for the
260 	 *  subset of time where we have data across all reactors.
261 	 */
262 	if (e[first].tsc > _tsc_offset) {
263 		_tsc_offset = e[first].tsc;
264 	}
265 
266 	i = first;
267 	while (1) {
268 		if (e[i].tpoint_id != SPDK_TRACE_MAX_TPOINT_ID) {
269 			_entries[entry_key(lcore, e[i].tsc)] = &e[i];
270 		}
271 		if (i == last) {
272 			break;
273 		}
274 		i++;
275 		if (i == num_entries_filled) {
276 			i = 0;
277 		}
278 	}
279 }
280 
281 bool
282 spdk_trace_parser::init(const spdk_trace_parser_opts *opts)
283 {
284 	spdk_trace_history *history;
285 	struct stat st;
286 	int rc, i;
287 
288 	switch (opts->mode) {
289 	case SPDK_TRACE_PARSER_MODE_FILE:
290 		_fd = open(opts->filename, O_RDONLY);
291 		break;
292 	case SPDK_TRACE_PARSER_MODE_SHM:
293 		_fd = shm_open(opts->filename, O_RDONLY, 0600);
294 		break;
295 	default:
296 		SPDK_ERRLOG("Invalid mode: %d\n", opts->mode);
297 		return false;
298 	}
299 
300 	if (_fd < 0) {
301 		SPDK_ERRLOG("Could not open trace file: %s (%d)\n", opts->filename, errno);
302 		return false;
303 	}
304 
305 	rc = fstat(_fd, &st);
306 	if (rc < 0) {
307 		SPDK_ERRLOG("Could not get size of trace file: %s\n", opts->filename);
308 		return false;
309 	}
310 
311 	if ((size_t)st.st_size < sizeof(*_histories)) {
312 		SPDK_ERRLOG("Invalid trace file: %s\n", opts->filename);
313 		return false;
314 	}
315 
316 	/* Map the header of trace file */
317 	_map_size = sizeof(*_histories);
318 	_histories = static_cast<spdk_trace_histories *>(mmap(NULL, _map_size, PROT_READ,
319 			MAP_SHARED, _fd, 0));
320 	if (_histories == MAP_FAILED) {
321 		SPDK_ERRLOG("Could not mmap trace file: %s\n", opts->filename);
322 		_histories = NULL;
323 		return false;
324 	}
325 
326 	/* Remap the entire trace file */
327 	_map_size = spdk_get_trace_histories_size(_histories);
328 	munmap(_histories, sizeof(*_histories));
329 	if ((size_t)st.st_size < _map_size) {
330 		SPDK_ERRLOG("Trace file %s is not valid\n", opts->filename);
331 		_histories = NULL;
332 		return false;
333 	}
334 	_histories = static_cast<spdk_trace_histories *>(mmap(NULL, _map_size, PROT_READ,
335 			MAP_SHARED, _fd, 0));
336 	if (_histories == MAP_FAILED) {
337 		SPDK_ERRLOG("Could not mmap trace file: %s\n", opts->filename);
338 		_histories = NULL;
339 		return false;
340 	}
341 
342 	if (opts->lcore == SPDK_TRACE_MAX_LCORE) {
343 		for (i = 0; i < SPDK_TRACE_MAX_LCORE; i++) {
344 			history = spdk_get_per_lcore_history(_histories, i);
345 			assert(history);
346 			if (history->num_entries == 0 || history->entries[0].tsc == 0) {
347 				continue;
348 			}
349 
350 			populate_events(history, history->num_entries);
351 		}
352 	} else {
353 		history = spdk_get_per_lcore_history(_histories, opts->lcore);
354 		assert(history);
355 		if (history->num_entries > 0 && history->entries[0].tsc != 0) {
356 			populate_events(history, history->num_entries);
357 		}
358 	}
359 
360 	_iter = _entries.begin();
361 	return true;
362 }
363 
364 void
365 spdk_trace_parser::cleanup()
366 {
367 	if (_histories != NULL) {
368 		munmap(_histories, _map_size);
369 	}
370 
371 	if (_fd > 0) {
372 		close(_fd);
373 	}
374 }
375 
376 spdk_trace_parser::spdk_trace_parser(const spdk_trace_parser_opts *opts) :
377 	_histories(NULL),
378 	_map_size(0),
379 	_fd(-1),
380 	_tsc_offset(0)
381 {
382 	if (!init(opts)) {
383 		cleanup();
384 		throw std::exception();
385 	}
386 }
387 
388 spdk_trace_parser::~spdk_trace_parser()
389 {
390 	cleanup();
391 }
392 
393 struct spdk_trace_parser *
394 spdk_trace_parser_init(const struct spdk_trace_parser_opts *opts)
395 {
396 	try {
397 		return new spdk_trace_parser(opts);
398 	} catch (...) {
399 		return NULL;
400 	}
401 }
402 
403 void
404 spdk_trace_parser_cleanup(struct spdk_trace_parser *parser)
405 {
406 	delete parser;
407 }
408 
409 const struct spdk_trace_flags *
410 spdk_trace_parser_get_flags(const struct spdk_trace_parser *parser)
411 {
412 	return parser->flags();
413 }
414 
415 uint64_t
416 spdk_trace_parser_get_tsc_offset(const struct spdk_trace_parser *parser)
417 {
418 	return parser->tsc_offset();
419 }
420 
421 bool
422 spdk_trace_parser_next_entry(struct spdk_trace_parser *parser,
423 			     struct spdk_trace_parser_entry *entry)
424 {
425 	return parser->next_entry(entry);
426 }
427 
428 uint64_t
429 spdk_trace_parser_get_entry_count(const struct spdk_trace_parser *parser, uint16_t lcore)
430 {
431 	return parser->entry_count(lcore);
432 }
433