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 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 101 return history == NULL ? 0 : history->num_entries; 102 } 103 104 spdk_trace_entry_buffer * 105 spdk_trace_parser::get_next_buffer(spdk_trace_entry_buffer *buf, uint16_t lcore) 106 { 107 spdk_trace_history *history; 108 109 history = spdk_get_per_lcore_history(_histories, lcore); 110 assert(history); 111 112 if (spdk_unlikely(static_cast<void *>(buf) == 113 static_cast<void *>(&history->entries[history->num_entries - 1]))) { 114 return reinterpret_cast<spdk_trace_entry_buffer *>(&history->entries[0]); 115 } else { 116 return buf + 1; 117 } 118 } 119 120 bool 121 spdk_trace_parser::build_arg(argument_context *argctx, const spdk_trace_argument *arg, int argid, 122 spdk_trace_parser_entry *pe) 123 { 124 spdk_trace_entry *entry = argctx->entry; 125 spdk_trace_entry_buffer *buffer = argctx->buffer; 126 size_t curlen, argoff; 127 128 argoff = 0; 129 /* Make sure that if we only copy a 4-byte integer, that the upper bytes have already been 130 * zeroed. 131 */ 132 pe->args[argid].integer = 0; 133 while (argoff < arg->size) { 134 if (argctx->offset == sizeof(buffer->data)) { 135 buffer = get_next_buffer(buffer, argctx->lcore); 136 if (spdk_unlikely(buffer->tpoint_id != SPDK_TRACE_MAX_TPOINT_ID || 137 buffer->tsc != entry->tsc)) { 138 return false; 139 } 140 141 argctx->offset = 0; 142 argctx->buffer = buffer; 143 } 144 145 curlen = spdk_min(sizeof(buffer->data) - argctx->offset, arg->size - argoff); 146 if (argoff < sizeof(pe->args[0])) { 147 memcpy(&pe->args[argid].string[argoff], &buffer->data[argctx->offset], 148 spdk_min(curlen, sizeof(pe->args[0]) - argoff)); 149 } 150 151 argctx->offset += curlen; 152 argoff += curlen; 153 } 154 155 return true; 156 } 157 158 bool 159 spdk_trace_parser::next_entry(spdk_trace_parser_entry *pe) 160 { 161 spdk_trace_tpoint *tpoint; 162 spdk_trace_entry *entry; 163 object_stats *stats; 164 std::map<uint64_t, uint64_t>::iterator related_kv; 165 166 if (_iter == _entries.end()) { 167 return false; 168 } 169 170 pe->entry = entry = _iter->second; 171 pe->lcore = _iter->first.lcore; 172 /* Set related index to the max value to indicate "empty" state */ 173 pe->related_index = UINT64_MAX; 174 pe->related_type = OBJECT_NONE; 175 tpoint = &_histories->flags.tpoint[entry->tpoint_id]; 176 stats = &_stats[tpoint->object_type]; 177 178 if (tpoint->new_object) { 179 stats->index[entry->object_id] = stats->counter++; 180 stats->start[entry->object_id] = entry->tsc; 181 } 182 183 if (tpoint->object_type != OBJECT_NONE) { 184 if (spdk_likely(stats->start.find(entry->object_id) != stats->start.end())) { 185 pe->object_index = stats->index[entry->object_id]; 186 pe->object_start = stats->start[entry->object_id]; 187 } else { 188 pe->object_index = UINT64_MAX; 189 pe->object_start = UINT64_MAX; 190 } 191 } 192 193 argument_context argctx(entry, pe->lcore); 194 for (uint8_t i = 0; i < tpoint->num_args; ++i) { 195 if (!build_arg(&argctx, &tpoint->args[i], i, pe)) { 196 SPDK_ERRLOG("Failed to parse tracepoint argument\n"); 197 return false; 198 } 199 } 200 201 for (uint8_t i = 0; i < SPDK_TRACE_MAX_RELATIONS; ++i) { 202 /* The relations are stored inside a tpoint, which means there might be 203 * multiple objects bound to a single tpoint. */ 204 if (tpoint->related_objects[i].object_type == OBJECT_NONE) { 205 break; 206 } 207 stats = &_stats[tpoint->related_objects[i].object_type]; 208 related_kv = stats->index.find(reinterpret_cast<uint64_t> 209 (pe->args[tpoint->related_objects[i].arg_index].pointer)); 210 /* To avoid parsing the whole array, object index and type are stored 211 * directly inside spdk_trace_parser_entry. */ 212 if (related_kv != stats->index.end()) { 213 pe->related_index = related_kv->second; 214 pe->related_type = tpoint->related_objects[i].object_type; 215 break; 216 } 217 } 218 219 _iter++; 220 return true; 221 } 222 223 void 224 spdk_trace_parser::populate_events(spdk_trace_history *history, int num_entries) 225 { 226 int i, num_entries_filled; 227 spdk_trace_entry *e; 228 int first, last, lcore; 229 230 lcore = history->lcore; 231 e = history->entries; 232 233 num_entries_filled = num_entries; 234 while (e[num_entries_filled - 1].tsc == 0) { 235 num_entries_filled--; 236 } 237 238 if (num_entries == num_entries_filled) { 239 first = last = 0; 240 for (i = 1; i < num_entries; i++) { 241 if (e[i].tsc < e[first].tsc) { 242 first = i; 243 } 244 if (e[i].tsc > e[last].tsc) { 245 last = i; 246 } 247 } 248 } else { 249 first = 0; 250 last = num_entries_filled - 1; 251 } 252 253 /* 254 * We keep track of the highest first TSC out of all reactors. 255 * We will ignore any events that occurred before this TSC on any 256 * other reactors. This will ensure we only print data for the 257 * subset of time where we have data across all reactors. 258 */ 259 if (e[first].tsc > _tsc_offset) { 260 _tsc_offset = e[first].tsc; 261 } 262 263 i = first; 264 while (1) { 265 if (e[i].tpoint_id != SPDK_TRACE_MAX_TPOINT_ID) { 266 _entries[entry_key(lcore, e[i].tsc)] = &e[i]; 267 } 268 if (i == last) { 269 break; 270 } 271 i++; 272 if (i == num_entries_filled) { 273 i = 0; 274 } 275 } 276 } 277 278 bool 279 spdk_trace_parser::init(const spdk_trace_parser_opts *opts) 280 { 281 spdk_trace_history *history; 282 struct stat st; 283 int rc, i; 284 285 switch (opts->mode) { 286 case SPDK_TRACE_PARSER_MODE_FILE: 287 _fd = open(opts->filename, O_RDONLY); 288 break; 289 case SPDK_TRACE_PARSER_MODE_SHM: 290 _fd = shm_open(opts->filename, O_RDONLY, 0600); 291 break; 292 default: 293 SPDK_ERRLOG("Invalid mode: %d\n", opts->mode); 294 return false; 295 } 296 297 if (_fd < 0) { 298 SPDK_ERRLOG("Could not open trace file: %s (%d)\n", opts->filename, errno); 299 return false; 300 } 301 302 rc = fstat(_fd, &st); 303 if (rc < 0) { 304 SPDK_ERRLOG("Could not get size of trace file: %s\n", opts->filename); 305 return false; 306 } 307 308 if ((size_t)st.st_size < sizeof(*_histories)) { 309 SPDK_ERRLOG("Invalid trace file: %s\n", opts->filename); 310 return false; 311 } 312 313 /* Map the header of trace file */ 314 _map_size = sizeof(*_histories); 315 _histories = static_cast<spdk_trace_histories *>(mmap(NULL, _map_size, PROT_READ, 316 MAP_SHARED, _fd, 0)); 317 if (_histories == MAP_FAILED) { 318 SPDK_ERRLOG("Could not mmap trace file: %s\n", opts->filename); 319 _histories = NULL; 320 return false; 321 } 322 323 /* Remap the entire trace file */ 324 _map_size = spdk_get_trace_histories_size(_histories); 325 munmap(_histories, sizeof(*_histories)); 326 if ((size_t)st.st_size < _map_size) { 327 SPDK_ERRLOG("Trace file %s is not valid\n", opts->filename); 328 _histories = NULL; 329 return false; 330 } 331 _histories = static_cast<spdk_trace_histories *>(mmap(NULL, _map_size, PROT_READ, 332 MAP_SHARED, _fd, 0)); 333 if (_histories == MAP_FAILED) { 334 SPDK_ERRLOG("Could not mmap trace file: %s\n", opts->filename); 335 _histories = NULL; 336 return false; 337 } 338 339 if (opts->lcore == SPDK_TRACE_MAX_LCORE) { 340 for (i = 0; i < SPDK_TRACE_MAX_LCORE; i++) { 341 history = spdk_get_per_lcore_history(_histories, i); 342 if (history == NULL || history->num_entries == 0 || history->entries[0].tsc == 0) { 343 continue; 344 } 345 346 populate_events(history, history->num_entries); 347 } 348 } else { 349 history = spdk_get_per_lcore_history(_histories, opts->lcore); 350 if (history == NULL) { 351 SPDK_ERRLOG("Trace file %s has no trace history for lcore %d\n", 352 opts->filename, opts->lcore); 353 return false; 354 } 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