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