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