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 spdk_trace_parser { 85 spdk_trace_parser(const spdk_trace_parser_opts *opts); 86 ~spdk_trace_parser(); 87 spdk_trace_parser(const spdk_trace_parser &) = delete; 88 spdk_trace_parser &operator=(const spdk_trace_parser &) = delete; 89 const spdk_trace_flags *flags() const { return &_histories->flags; } 90 uint64_t tsc_offset() const { return _tsc_offset; } 91 bool next_entry(spdk_trace_parser_entry *entry); 92 private: 93 spdk_trace_entry_buffer *get_next_buffer(spdk_trace_entry_buffer *buf, uint16_t lcore); 94 bool build_arg(argument_context *argctx, const spdk_trace_argument *arg, int argid, 95 spdk_trace_parser_entry *pe); 96 void populate_events(spdk_trace_history *history, int num_entries); 97 bool init(const spdk_trace_parser_opts *opts); 98 void cleanup(); 99 100 spdk_trace_histories *_histories; 101 size_t _map_size; 102 int _fd; 103 uint64_t _tsc_offset; 104 entry_map _entries; 105 entry_map::iterator _iter; 106 }; 107 108 spdk_trace_entry_buffer * 109 spdk_trace_parser::get_next_buffer(spdk_trace_entry_buffer *buf, uint16_t lcore) 110 { 111 spdk_trace_history *history; 112 113 history = spdk_get_per_lcore_history(_histories, lcore); 114 assert(history); 115 116 if (spdk_unlikely(static_cast<void *>(buf) == 117 static_cast<void *>(&history->entries[history->num_entries - 1]))) { 118 return reinterpret_cast<spdk_trace_entry_buffer *>(&history->entries[0]); 119 } else { 120 return buf + 1; 121 } 122 } 123 124 bool 125 spdk_trace_parser::build_arg(argument_context *argctx, const spdk_trace_argument *arg, int argid, 126 spdk_trace_parser_entry *pe) 127 { 128 spdk_trace_entry *entry = argctx->entry; 129 spdk_trace_entry_buffer *buffer = argctx->buffer; 130 size_t curlen, argoff; 131 132 argoff = 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 164 if (_iter == _entries.end()) { 165 return false; 166 } 167 168 pe->entry = entry = _iter->second; 169 pe->lcore = _iter->first.lcore; 170 tpoint = &_histories->flags.tpoint[entry->tpoint_id]; 171 172 argument_context argctx(entry, pe->lcore); 173 for (uint8_t i = 0; i < tpoint->num_args; ++i) { 174 if (!build_arg(&argctx, &tpoint->args[i], i, pe)) { 175 SPDK_ERRLOG("Failed to parse tracepoint argument\n"); 176 return false; 177 } 178 } 179 180 _iter++; 181 return true; 182 } 183 184 void 185 spdk_trace_parser::populate_events(spdk_trace_history *history, int num_entries) 186 { 187 int i, num_entries_filled; 188 spdk_trace_entry *e; 189 int first, last, lcore; 190 191 lcore = history->lcore; 192 e = history->entries; 193 194 num_entries_filled = num_entries; 195 while (e[num_entries_filled - 1].tsc == 0) { 196 num_entries_filled--; 197 } 198 199 if (num_entries == num_entries_filled) { 200 first = last = 0; 201 for (i = 1; i < num_entries; i++) { 202 if (e[i].tsc < e[first].tsc) { 203 first = i; 204 } 205 if (e[i].tsc > e[last].tsc) { 206 last = i; 207 } 208 } 209 } else { 210 first = 0; 211 last = num_entries_filled - 1; 212 } 213 214 /* 215 * We keep track of the highest first TSC out of all reactors. 216 * We will ignore any events that occured before this TSC on any 217 * other reactors. This will ensure we only print data for the 218 * subset of time where we have data across all reactors. 219 */ 220 if (e[first].tsc > _tsc_offset) { 221 _tsc_offset = e[first].tsc; 222 } 223 224 i = first; 225 while (1) { 226 if (e[i].tpoint_id != SPDK_TRACE_MAX_TPOINT_ID) { 227 _entries[entry_key(lcore, e[i].tsc)] = &e[i]; 228 } 229 if (i == last) { 230 break; 231 } 232 i++; 233 if (i == num_entries_filled) { 234 i = 0; 235 } 236 } 237 } 238 239 bool 240 spdk_trace_parser::init(const spdk_trace_parser_opts *opts) 241 { 242 spdk_trace_history *history; 243 struct stat st; 244 int rc, i; 245 246 switch (opts->mode) { 247 case SPDK_TRACE_PARSER_MODE_FILE: 248 _fd = open(opts->filename, O_RDONLY); 249 break; 250 case SPDK_TRACE_PARSER_MODE_SHM: 251 _fd = shm_open(opts->filename, O_RDONLY, 0600); 252 break; 253 default: 254 SPDK_ERRLOG("Invalid mode: %d\n", opts->mode); 255 return false; 256 } 257 258 if (_fd < 0) { 259 SPDK_ERRLOG("Could not open trace file: %s (%d)\n", opts->filename, errno); 260 return false; 261 } 262 263 rc = fstat(_fd, &st); 264 if (rc < 0) { 265 SPDK_ERRLOG("Could not get size of trace file: %s\n", opts->filename); 266 return false; 267 } 268 269 if ((size_t)st.st_size < sizeof(*_histories)) { 270 SPDK_ERRLOG("Invalid trace file: %s\n", opts->filename); 271 return false; 272 } 273 274 /* Map the header of trace file */ 275 _map_size = sizeof(*_histories); 276 _histories = static_cast<spdk_trace_histories *>(mmap(NULL, _map_size, PROT_READ, 277 MAP_SHARED, _fd, 0)); 278 if (_histories == MAP_FAILED) { 279 SPDK_ERRLOG("Could not mmap trace file: %s\n", opts->filename); 280 _histories = NULL; 281 return false; 282 } 283 284 /* Remap the entire trace file */ 285 _map_size = spdk_get_trace_histories_size(_histories); 286 munmap(_histories, sizeof(*_histories)); 287 if ((size_t)st.st_size < _map_size) { 288 SPDK_ERRLOG("Trace file %s is not valid\n", opts->filename); 289 _histories = NULL; 290 return false; 291 } 292 _histories = static_cast<spdk_trace_histories *>(mmap(NULL, _map_size, PROT_READ, 293 MAP_SHARED, _fd, 0)); 294 if (_histories == MAP_FAILED) { 295 SPDK_ERRLOG("Could not mmap trace file: %s\n", opts->filename); 296 _histories = NULL; 297 return false; 298 } 299 300 if (opts->lcore == SPDK_TRACE_MAX_LCORE) { 301 for (i = 0; i < SPDK_TRACE_MAX_LCORE; i++) { 302 history = spdk_get_per_lcore_history(_histories, i); 303 if (history->num_entries == 0 || history->entries[0].tsc == 0) { 304 continue; 305 } 306 307 populate_events(history, history->num_entries); 308 } 309 } else { 310 history = spdk_get_per_lcore_history(_histories, opts->lcore); 311 if (history->num_entries > 0 && history->entries[0].tsc != 0) { 312 populate_events(history, history->num_entries); 313 } 314 } 315 316 _iter = _entries.begin(); 317 return true; 318 } 319 320 void 321 spdk_trace_parser::cleanup() 322 { 323 if (_histories != NULL) { 324 munmap(_histories, _map_size); 325 } 326 327 if (_fd > 0) { 328 close(_fd); 329 } 330 } 331 332 spdk_trace_parser::spdk_trace_parser(const spdk_trace_parser_opts *opts) : 333 _histories(NULL), 334 _map_size(0), 335 _fd(-1), 336 _tsc_offset(0) 337 { 338 if (!init(opts)) { 339 cleanup(); 340 throw std::exception(); 341 } 342 } 343 344 spdk_trace_parser::~spdk_trace_parser() 345 { 346 cleanup(); 347 } 348 349 struct spdk_trace_parser * 350 spdk_trace_parser_init(const struct spdk_trace_parser_opts *opts) 351 { 352 try { 353 return new spdk_trace_parser(opts); 354 } catch (...) { 355 return NULL; 356 } 357 } 358 359 void 360 spdk_trace_parser_cleanup(struct spdk_trace_parser *parser) 361 { 362 delete parser; 363 } 364 365 const struct spdk_trace_flags * 366 spdk_trace_parser_get_flags(const struct spdk_trace_parser *parser) 367 { 368 return parser->flags(); 369 } 370 371 uint64_t 372 spdk_trace_parser_get_tsc_offset(const struct spdk_trace_parser *parser) 373 { 374 return parser->tsc_offset(); 375 } 376 377 bool 378 spdk_trace_parser_next_entry(struct spdk_trace_parser *parser, 379 struct spdk_trace_parser_entry *entry) 380 { 381 return parser->next_entry(entry); 382 } 383