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