| 1 | /* This file is part of the Linux Trace Toolkit viewer |
| 2 | * Copyright (C) 2005 Mathieu Desnoyers |
| 3 | * |
| 4 | * Complete rewrite from the original version made by XangXiu Yang. |
| 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
| 8 | * License Version 2.1 as published by the Free Software Foundation. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Lesser General Public |
| 16 | * License along with this library; if not, write to the |
| 17 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 18 | * Boston, MA 02111-1307, USA. |
| 19 | */ |
| 20 | |
| 21 | #ifdef HAVE_CONFIG_H |
| 22 | #include <config.h> |
| 23 | #endif |
| 24 | |
| 25 | #include <stdio.h> |
| 26 | #include <fcntl.h> |
| 27 | #include <string.h> |
| 28 | #include <dirent.h> |
| 29 | #include <sys/stat.h> |
| 30 | #include <sys/types.h> |
| 31 | #include <errno.h> |
| 32 | #include <unistd.h> |
| 33 | #include <math.h> |
| 34 | #include <glib.h> |
| 35 | #include <glib/gprintf.h> |
| 36 | #include <malloc.h> |
| 37 | #include <sys/mman.h> |
| 38 | #include <string.h> |
| 39 | #include <ctype.h> |
| 40 | #include <inttypes.h> |
| 41 | |
| 42 | // For realpath |
| 43 | #include <limits.h> |
| 44 | #include <stdlib.h> |
| 45 | |
| 46 | |
| 47 | #include <ltt/ltt.h> |
| 48 | #include "ltt-private.h" |
| 49 | #include <ltt/trace.h> |
| 50 | #include <ltt/event.h> |
| 51 | #include <ltt/ltt-types.h> |
| 52 | #include <ltt/marker.h> |
| 53 | |
| 54 | #define DEFAULT_N_BLOCKS 32 |
| 55 | |
| 56 | /* from marker.c */ |
| 57 | extern long marker_update_fields_offsets(struct marker_info *info, const char *data); |
| 58 | extern void marker_update_event_fields_offsets(GArray *fields_offsets, |
| 59 | struct marker_info *info); |
| 60 | |
| 61 | /* Tracefile names used in this file */ |
| 62 | |
| 63 | GQuark LTT_TRACEFILE_NAME_METADATA; |
| 64 | |
| 65 | #ifndef g_open |
| 66 | #define g_open open |
| 67 | #endif |
| 68 | |
| 69 | |
| 70 | #define __UNUSED__ __attribute__((__unused__)) |
| 71 | |
| 72 | #define g_info(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, format) |
| 73 | |
| 74 | #ifndef g_debug |
| 75 | #define g_debug(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format) |
| 76 | #endif |
| 77 | |
| 78 | #define g_close close |
| 79 | |
| 80 | /* Those macros must be called from within a function where page_size is a known |
| 81 | * variable */ |
| 82 | #define PAGE_MASK (~(page_size-1)) |
| 83 | #define PAGE_ALIGN(addr) (((addr)+page_size-1)&PAGE_MASK) |
| 84 | |
| 85 | /* set the offset of the fields belonging to the event, |
| 86 | need the information of the archecture */ |
| 87 | //void set_fields_offsets(LttTracefile *tf, LttEventType *event_type); |
| 88 | //size_t get_fields_offsets(LttTracefile *tf, LttEventType *event_type, void *data); |
| 89 | |
| 90 | /* map a fixed size or a block information from the file (fd) */ |
| 91 | static gint map_block(LttTracefile * tf, guint block_num); |
| 92 | |
| 93 | /* calculate nsec per cycles for current block */ |
| 94 | #if 0 |
| 95 | static guint32 calc_nsecs_per_cycle(LttTracefile * t); |
| 96 | static guint64 cycles_2_ns(LttTracefile *tf, guint64 cycles); |
| 97 | #endif //0 |
| 98 | |
| 99 | /* go to the next event */ |
| 100 | static int ltt_seek_next_event(LttTracefile *tf); |
| 101 | |
| 102 | static int open_tracefiles(LttTrace *trace, gchar *root_path, |
| 103 | gchar *relative_path); |
| 104 | static int ltt_process_metadata_tracefile(LttTracefile *tf); |
| 105 | static void ltt_tracefile_time_span_get(LttTracefile *tf, |
| 106 | LttTime *start, LttTime *end); |
| 107 | static void group_time_span_get(GQuark name, gpointer data, gpointer user_data); |
| 108 | static gint map_block(LttTracefile * tf, guint block_num); |
| 109 | static void ltt_update_event_size(LttTracefile *tf); |
| 110 | static LttTrace *_ltt_trace_open(const gchar *pathname, gboolean is_live); |
| 111 | static int ltt_tracefile_update(LttTracefile *tf); |
| 112 | static void update_tracefile_group(GQuark name, gpointer data, gpointer user_data); |
| 113 | |
| 114 | /* Enable event debugging */ |
| 115 | static int a_event_debug = 0; |
| 116 | |
| 117 | void ltt_event_debug(int state) |
| 118 | { |
| 119 | a_event_debug = state; |
| 120 | } |
| 121 | |
| 122 | /* trace can be NULL |
| 123 | * |
| 124 | * Return value : 0 success, 1 bad tracefile |
| 125 | */ |
| 126 | static int parse_trace_header(ltt_subbuffer_header_t *header, |
| 127 | LttTracefile *tf, LttTrace *t) |
| 128 | { |
| 129 | if (header->magic_number == LTT_MAGIC_NUMBER) |
| 130 | tf->reverse_bo = 0; |
| 131 | else if(header->magic_number == LTT_REV_MAGIC_NUMBER) |
| 132 | tf->reverse_bo = 1; |
| 133 | else /* invalid magic number, bad tracefile ! */ |
| 134 | return 1; |
| 135 | |
| 136 | if(t) { |
| 137 | t->ltt_major_version = header->major_version; |
| 138 | t->ltt_minor_version = header->minor_version; |
| 139 | t->arch_size = header->arch_size; |
| 140 | } |
| 141 | tf->alignment = header->alignment; |
| 142 | |
| 143 | /* Get float byte order : might be different from int byte order |
| 144 | * (or is set to 0 if the trace has no float (kernel trace)) */ |
| 145 | tf->float_word_order = 0; |
| 146 | |
| 147 | switch(header->major_version) { |
| 148 | case 0: |
| 149 | case 1: |
| 150 | g_warning("Unsupported trace version : %hhu.%hhu", |
| 151 | header->major_version, header->minor_version); |
| 152 | return 1; |
| 153 | break; |
| 154 | case 2: |
| 155 | switch(header->minor_version) { |
| 156 | case 6: |
| 157 | { |
| 158 | struct ltt_subbuffer_header_2_6 *vheader = header; |
| 159 | tf->buffer_header_size = ltt_subbuffer_header_size(); |
| 160 | tf->tscbits = 27; |
| 161 | tf->eventbits = 5; |
| 162 | tf->tsc_mask = ((1ULL << tf->tscbits) - 1); |
| 163 | tf->tsc_mask_next_bit = (1ULL << tf->tscbits); |
| 164 | |
| 165 | if(t) { |
| 166 | t->start_freq = ltt_get_uint64(LTT_GET_BO(tf), |
| 167 | &vheader->start_freq); |
| 168 | t->freq_scale = ltt_get_uint32(LTT_GET_BO(tf), |
| 169 | &vheader->freq_scale); |
| 170 | t->start_tsc = ltt_get_uint64(LTT_GET_BO(tf), |
| 171 | &vheader->cycle_count_begin); |
| 172 | t->start_monotonic = 0; |
| 173 | t->start_time.tv_sec = ltt_get_uint64(LTT_GET_BO(tf), |
| 174 | &vheader->start_time_sec); |
| 175 | t->start_time.tv_nsec = ltt_get_uint64(LTT_GET_BO(tf), |
| 176 | &vheader->start_time_usec); |
| 177 | t->start_time.tv_nsec *= 1000; /* microsec to nanosec */ |
| 178 | |
| 179 | t->start_time_from_tsc = |
| 180 | ltt_time_from_uint64(tsc_to_uint64(t->freq_scale, |
| 181 | t->start_freq, t->start_tsc)); |
| 182 | } |
| 183 | } |
| 184 | break; |
| 185 | default: |
| 186 | g_warning("Unsupported trace version : %hhu.%hhu", |
| 187 | header->major_version, header->minor_version); |
| 188 | return 1; |
| 189 | } |
| 190 | break; |
| 191 | default: |
| 192 | g_warning("Unsupported trace version : %hhu.%hhu", |
| 193 | header->major_version, header->minor_version); |
| 194 | return 1; |
| 195 | } |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | int get_block_offset_size(LttTracefile *tf, guint block_num, |
| 200 | uint64_t *offset, uint32_t *size) |
| 201 | { |
| 202 | uint64_t offa, offb; |
| 203 | |
| 204 | if (unlikely(block_num >= tf->num_blocks)) |
| 205 | return -1; |
| 206 | |
| 207 | offa = g_array_index(tf->buf_index, uint64_t, block_num); |
| 208 | if (likely(block_num < tf->num_blocks - 1)) |
| 209 | offb = g_array_index(tf->buf_index, uint64_t, block_num + 1); |
| 210 | else |
| 211 | offb = tf->file_size; |
| 212 | *offset = offa; |
| 213 | *size = offb - offa; |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | static int ltt_trace_update_block_index(LttTracefile *tf, uint64_t offset, |
| 218 | unsigned long firstBlock) |
| 219 | { |
| 220 | int i = firstBlock; |
| 221 | int page_size = getpagesize(); |
| 222 | unsigned int header_map_size = PAGE_ALIGN(ltt_subbuffer_header_size()); |
| 223 | |
| 224 | g_assert(tf->buf_index->len == i); |
| 225 | while (offset < tf->file_size) { |
| 226 | ltt_subbuffer_header_t *header; |
| 227 | uint64_t *off; |
| 228 | uint64_t size; |
| 229 | |
| 230 | /* map block header */ |
| 231 | header = mmap(0, header_map_size, PROT_READ, |
| 232 | MAP_PRIVATE, tf->fd, (off_t)offset); |
| 233 | if(header == MAP_FAILED) { |
| 234 | perror("Error in allocating memory for buffer of tracefile"); |
| 235 | return -1; |
| 236 | } |
| 237 | |
| 238 | /* read len, offset += len */ |
| 239 | size = ltt_get_uint32(LTT_GET_BO(tf), &header->sb_size); |
| 240 | |
| 241 | /* Only index completly writen blocks */ |
| 242 | if (offset + size <= tf->file_size) { |
| 243 | |
| 244 | tf->buf_index = g_array_set_size(tf->buf_index, i + 1); |
| 245 | off = &g_array_index(tf->buf_index, uint64_t, i); |
| 246 | *off = offset; |
| 247 | |
| 248 | /* Store current buffer end cycle as the last file timestamp */ |
| 249 | /* TODO ybrosseau 2010-11-04: Might want to convert it to a LttTime */ |
| 250 | tf->end_timestamp = ltt_get_uint64(LTT_GET_BO(tf), |
| 251 | &header->cycle_count_end); |
| 252 | |
| 253 | ++i; |
| 254 | } |
| 255 | offset += size; |
| 256 | /* unmap block header */ |
| 257 | if(munmap(header, header_map_size)) { |
| 258 | g_warning("unmap size : %u\n", header_map_size); |
| 259 | perror("munmap error"); |
| 260 | return -1; |
| 261 | } |
| 262 | } |
| 263 | tf->num_blocks = i; |
| 264 | |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | /* parse the new information from the file and reajust the number of blocks. |
| 269 | * |
| 270 | * Return value : 0 success, -1 error |
| 271 | */ |
| 272 | int ltt_trace_continue_block_index(LttTracefile *tf) |
| 273 | { |
| 274 | int ret; |
| 275 | uint64_t offset; |
| 276 | uint32_t last_block_size; |
| 277 | unsigned long i = tf->num_blocks; |
| 278 | int page_size = getpagesize(); |
| 279 | unsigned int header_map_size = PAGE_ALIGN(ltt_subbuffer_header_size()); |
| 280 | |
| 281 | get_block_offset_size(tf, tf->num_blocks-1, &offset, &last_block_size); |
| 282 | |
| 283 | ltt_subbuffer_header_t *header_tmp = mmap(0, header_map_size, PROT_READ, |
| 284 | MAP_PRIVATE, tf->fd, (off_t)offset); |
| 285 | if(header_tmp == MAP_FAILED) { |
| 286 | perror("Error in allocating memory for buffer of tracefile"); |
| 287 | return -1; |
| 288 | } |
| 289 | |
| 290 | /* read len, offset += len */ |
| 291 | offset += ltt_get_uint32(LTT_GET_BO(tf), &header_tmp->sb_size); |
| 292 | |
| 293 | ret = ltt_trace_update_block_index(tf, offset, i); |
| 294 | |
| 295 | return ret; |
| 296 | } |
| 297 | |
| 298 | int ltt_trace_create_block_index(LttTracefile *tf) |
| 299 | { |
| 300 | int ret; |
| 301 | uint64_t offset = 0; |
| 302 | unsigned long i = 0; |
| 303 | |
| 304 | tf->buf_index = g_array_sized_new(FALSE, TRUE, sizeof(uint64_t), |
| 305 | DEFAULT_N_BLOCKS); |
| 306 | if(!tf->buf_index) |
| 307 | return -1; |
| 308 | ret = ltt_trace_update_block_index(tf, offset, i); |
| 309 | return ret; |
| 310 | } |
| 311 | |
| 312 | /* |
| 313 | Read file header and initialise buffer indexes |
| 314 | |
| 315 | Return value : 0 for success, -1 otherwise. |
| 316 | */ |
| 317 | static int ltt_tracefile_init(LttTracefile *tf) |
| 318 | { |
| 319 | ltt_subbuffer_header_t *header; |
| 320 | int page_size = getpagesize(); |
| 321 | |
| 322 | /* Temporarily map the buffer start header to get trace information */ |
| 323 | /* Multiple of pages aligned head */ |
| 324 | tf->buffer.head = mmap(0, |
| 325 | PAGE_ALIGN(ltt_subbuffer_header_size()), PROT_READ, |
| 326 | MAP_PRIVATE, tf->fd, 0); |
| 327 | if(tf->buffer.head == MAP_FAILED) { |
| 328 | perror("Error in allocating memory for buffer of tracefile"); |
| 329 | goto unmap_file; |
| 330 | } |
| 331 | g_assert( ( (gulong)tf->buffer.head&(8-1) ) == 0); // make sure it's aligned. |
| 332 | |
| 333 | header = (ltt_subbuffer_header_t *)tf->buffer.head; |
| 334 | |
| 335 | if(parse_trace_header(header, tf, tf->trace)) |
| 336 | { |
| 337 | g_warning("parse_trace_header error"); |
| 338 | goto unmap_file; |
| 339 | } |
| 340 | |
| 341 | if(munmap(tf->buffer.head, |
| 342 | PAGE_ALIGN(ltt_subbuffer_header_size()))) |
| 343 | { |
| 344 | g_warning("unmap size : %zu\n", |
| 345 | PAGE_ALIGN(ltt_subbuffer_header_size())); |
| 346 | perror("munmap error"); |
| 347 | g_assert(0); |
| 348 | } |
| 349 | tf->buffer.head = NULL; |
| 350 | |
| 351 | /* Create fields offset table */ |
| 352 | tf->event.fields_offsets = g_array_sized_new(FALSE, FALSE, |
| 353 | sizeof(struct LttField), 1); |
| 354 | if (!tf->event.fields_offsets) { |
| 355 | g_warning("Cannot create fields offset table"); |
| 356 | goto unmap_file; |
| 357 | } |
| 358 | |
| 359 | /* Create block index */ |
| 360 | ltt_trace_create_block_index(tf); |
| 361 | |
| 362 | if(map_block(tf,0)) |
| 363 | { |
| 364 | perror("Cannot map block for tracefile"); |
| 365 | goto unmap_file; |
| 366 | } |
| 367 | return 0; |
| 368 | /* Error */ |
| 369 | unmap_file: |
| 370 | if(munmap(tf->buffer.head, |
| 371 | PAGE_ALIGN(ltt_subbuffer_header_size()))) { |
| 372 | g_warning("unmap size : %zu\n", |
| 373 | PAGE_ALIGN(ltt_subbuffer_header_size())); |
| 374 | perror("munmap error"); |
| 375 | g_assert(0); |
| 376 | } |
| 377 | return -1; |
| 378 | } |
| 379 | |
| 380 | /***************************************************************************** |
| 381 | *Function name |
| 382 | * ltt_tracefile_open : open a trace file, construct a LttTracefile |
| 383 | *Input params |
| 384 | * t : the trace containing the tracefile |
| 385 | * fileName : path name of the trace file |
| 386 | * tf : the tracefile structure |
| 387 | *Return value |
| 388 | * : 0 for success, -1 otherwise. |
| 389 | ****************************************************************************/ |
| 390 | static gint ltt_tracefile_open(LttTrace *t, gchar * fileName, LttTracefile *tf) |
| 391 | { |
| 392 | struct stat lTDFStat; /* Trace data file status */ |
| 393 | |
| 394 | //open the file |
| 395 | tf->long_name = g_quark_from_string(fileName); |
| 396 | tf->trace = t; |
| 397 | tf->fd = open(fileName, O_RDONLY); |
| 398 | tf->buf_index = NULL; |
| 399 | tf->num_blocks = 0; |
| 400 | if(tf->fd < 0){ |
| 401 | g_warning("Unable to open input data file %s\n", fileName); |
| 402 | goto end; |
| 403 | } |
| 404 | |
| 405 | // Get the file's status |
| 406 | if(fstat(tf->fd, &lTDFStat) < 0){ |
| 407 | g_warning("Unable to get the status of the input data file %s\n", fileName); |
| 408 | goto close_file; |
| 409 | } |
| 410 | //store the size of the file |
| 411 | tf->file_size = lTDFStat.st_size; |
| 412 | tf->events_lost = 0; |
| 413 | tf->subbuf_corrupt = 0; |
| 414 | tf->buffer.head = NULL; |
| 415 | tf->event.fields_offsets = NULL; |
| 416 | |
| 417 | /* Is the file large enough to contain a trace */ |
| 418 | if(lTDFStat.st_size < (off_t)(ltt_subbuffer_header_size())){ |
| 419 | if (t->is_live) { |
| 420 | /* It a live trace so the file can be empty at the start of the analysis */ |
| 421 | goto end; |
| 422 | } else { |
| 423 | g_print("The input data file %s does not contain a trace\n", fileName); |
| 424 | goto close_file; |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | if(ltt_tracefile_init(tf) < 0) { |
| 429 | goto close_file; |
| 430 | } |
| 431 | |
| 432 | return 0; |
| 433 | |
| 434 | /* Error */ |
| 435 | close_file: |
| 436 | close(tf->fd); |
| 437 | end: |
| 438 | if (tf->buf_index) |
| 439 | g_array_free(tf->buf_index, TRUE); |
| 440 | return -1; |
| 441 | } |
| 442 | |
| 443 | /***************************************************************************** |
| 444 | *Function name |
| 445 | * ltt_tracefile_close: close a trace file, |
| 446 | *Input params |
| 447 | * t : tracefile which will be closed |
| 448 | ****************************************************************************/ |
| 449 | |
| 450 | static void ltt_tracefile_close(LttTracefile *t) |
| 451 | { |
| 452 | int page_size = getpagesize(); |
| 453 | |
| 454 | if(t->buffer.head != NULL) |
| 455 | if(munmap(t->buffer.head, PAGE_ALIGN(t->buffer.size))) { |
| 456 | g_warning("unmap size : %u\n", |
| 457 | PAGE_ALIGN(t->buffer.size)); |
| 458 | perror("munmap error"); |
| 459 | g_assert(0); |
| 460 | } |
| 461 | |
| 462 | close(t->fd); |
| 463 | if (t->buf_index) |
| 464 | g_array_free(t->buf_index, TRUE); |
| 465 | if (t->event.fields_offsets) { |
| 466 | g_array_free(t->event.fields_offsets, TRUE); |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | /**************************************************************************** |
| 471 | * get_absolute_pathname |
| 472 | * |
| 473 | * return the unique pathname in the system |
| 474 | * |
| 475 | * MD : Fixed this function so it uses realpath, dealing well with |
| 476 | * forgotten cases (.. were not used correctly before). |
| 477 | * |
| 478 | ****************************************************************************/ |
| 479 | void get_absolute_pathname(const gchar *pathname, gchar * abs_pathname) |
| 480 | { |
| 481 | abs_pathname[0] = '\0'; |
| 482 | |
| 483 | if (realpath(pathname, abs_pathname) != NULL) |
| 484 | return; |
| 485 | else |
| 486 | { |
| 487 | /* error, return the original path unmodified */ |
| 488 | strcpy(abs_pathname, pathname); |
| 489 | return; |
| 490 | } |
| 491 | return; |
| 492 | } |
| 493 | |
| 494 | /* Search for something like : .*_.* |
| 495 | * |
| 496 | * The left side is the name, the right side is the number. |
| 497 | * Exclude leading /. |
| 498 | * Exclude flight- prefix. |
| 499 | */ |
| 500 | |
| 501 | static int get_tracefile_name_number(gchar *raw_name, |
| 502 | GQuark *name, |
| 503 | guint *num, |
| 504 | gulong *tid, |
| 505 | gulong *pgid, |
| 506 | guint64 *creation) |
| 507 | { |
| 508 | guint raw_name_len = strlen(raw_name); |
| 509 | gchar char_name[PATH_MAX]; |
| 510 | int i; |
| 511 | int underscore_pos; |
| 512 | long int cpu_num; |
| 513 | gchar *endptr; |
| 514 | gchar *tmpptr; |
| 515 | |
| 516 | /* skip leading / */ |
| 517 | for(i = 0; i < raw_name_len-1;i++) { |
| 518 | if(raw_name[i] != '/') |
| 519 | break; |
| 520 | } |
| 521 | raw_name = &raw_name[i]; |
| 522 | raw_name_len = strlen(raw_name); |
| 523 | |
| 524 | for(i=raw_name_len-1;i>=0;i--) { |
| 525 | if(raw_name[i] == '_') break; |
| 526 | } |
| 527 | if(i==-1) { /* Either not found or name length is 0 */ |
| 528 | /* This is a userspace tracefile */ |
| 529 | strncpy(char_name, raw_name, raw_name_len); |
| 530 | char_name[raw_name_len] = '\0'; |
| 531 | *name = g_quark_from_string(char_name); |
| 532 | *num = 0; /* unknown cpu */ |
| 533 | for(i=0;i<raw_name_len;i++) { |
| 534 | if(raw_name[i] == '/') { |
| 535 | break; |
| 536 | } |
| 537 | } |
| 538 | i++; |
| 539 | for(;i<raw_name_len;i++) { |
| 540 | if(raw_name[i] == '/') { |
| 541 | break; |
| 542 | } |
| 543 | } |
| 544 | i++; |
| 545 | for(;i<raw_name_len;i++) { |
| 546 | if(raw_name[i] == '-') { |
| 547 | break; |
| 548 | } |
| 549 | } |
| 550 | if(i == raw_name_len) return -1; |
| 551 | i++; |
| 552 | tmpptr = &raw_name[i]; |
| 553 | for(;i<raw_name_len;i++) { |
| 554 | if(raw_name[i] == '.') { |
| 555 | raw_name[i] = ' '; |
| 556 | break; |
| 557 | } |
| 558 | } |
| 559 | *tid = strtoul(tmpptr, &endptr, 10); |
| 560 | if(endptr == tmpptr) |
| 561 | return -1; /* No digit */ |
| 562 | if(*tid == ULONG_MAX) |
| 563 | return -1; /* underflow / overflow */ |
| 564 | i++; |
| 565 | tmpptr = &raw_name[i]; |
| 566 | for(;i<raw_name_len;i++) { |
| 567 | if(raw_name[i] == '.') { |
| 568 | raw_name[i] = ' '; |
| 569 | break; |
| 570 | } |
| 571 | } |
| 572 | *pgid = strtoul(tmpptr, &endptr, 10); |
| 573 | if(endptr == tmpptr) |
| 574 | return -1; /* No digit */ |
| 575 | if(*pgid == ULONG_MAX) |
| 576 | return -1; /* underflow / overflow */ |
| 577 | i++; |
| 578 | tmpptr = &raw_name[i]; |
| 579 | *creation = strtoull(tmpptr, &endptr, 10); |
| 580 | if(endptr == tmpptr) |
| 581 | return -1; /* No digit */ |
| 582 | if(*creation == G_MAXUINT64) |
| 583 | return -1; /* underflow / overflow */ |
| 584 | } else { |
| 585 | underscore_pos = i; |
| 586 | |
| 587 | cpu_num = strtol(raw_name+underscore_pos+1, &endptr, 10); |
| 588 | |
| 589 | if(endptr == raw_name+underscore_pos+1) |
| 590 | return -1; /* No digit */ |
| 591 | if(cpu_num == LONG_MIN || cpu_num == LONG_MAX) |
| 592 | return -1; /* underflow / overflow */ |
| 593 | |
| 594 | if (!strncmp(raw_name, "flight-", sizeof("flight-") - 1)) { |
| 595 | raw_name += sizeof("flight-") - 1; |
| 596 | underscore_pos -= sizeof("flight-") - 1; |
| 597 | } |
| 598 | strncpy(char_name, raw_name, underscore_pos); |
| 599 | char_name[underscore_pos] = '\0'; |
| 600 | *name = g_quark_from_string(char_name); |
| 601 | *num = cpu_num; |
| 602 | } |
| 603 | |
| 604 | |
| 605 | return 0; |
| 606 | } |
| 607 | |
| 608 | |
| 609 | GData **ltt_trace_get_tracefiles_groups(LttTrace *trace) |
| 610 | { |
| 611 | return &trace->tracefiles; |
| 612 | } |
| 613 | |
| 614 | |
| 615 | void compute_tracefile_group(GQuark key_id, |
| 616 | GArray *group, |
| 617 | struct compute_tracefile_group_args *args) |
| 618 | { |
| 619 | unsigned int i; |
| 620 | LttTracefile *tf; |
| 621 | |
| 622 | for(i=0; i<group->len; i++) { |
| 623 | tf = &g_array_index (group, LttTracefile, i); |
| 624 | if(tf->cpu_online) |
| 625 | args->func(tf, args->func_args); |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | |
| 630 | static void ltt_tracefile_group_destroy(gpointer data) |
| 631 | { |
| 632 | GArray *group = (GArray *)data; |
| 633 | unsigned int i; |
| 634 | LttTracefile *tf; |
| 635 | |
| 636 | if (group->len > 0) |
| 637 | destroy_marker_data(g_array_index (group, LttTracefile, 0).mdata); |
| 638 | for(i=0; i<group->len; i++) { |
| 639 | tf = &g_array_index (group, LttTracefile, i); |
| 640 | if(tf->cpu_online) |
| 641 | ltt_tracefile_close(tf); |
| 642 | } |
| 643 | g_array_free(group, TRUE); |
| 644 | } |
| 645 | |
| 646 | static __attribute__ ((__unused__)) gboolean ltt_tracefile_group_has_cpu_online(gpointer data) |
| 647 | { |
| 648 | GArray *group = (GArray *)data; |
| 649 | unsigned int i; |
| 650 | LttTracefile *tf; |
| 651 | |
| 652 | for(i=0; i<group->len; i++) { |
| 653 | tf = &g_array_index (group, LttTracefile, i); |
| 654 | if(tf->cpu_online) |
| 655 | return 1; |
| 656 | } |
| 657 | return 0; |
| 658 | } |
| 659 | |
| 660 | |
| 661 | /* Open each tracefile under a specific directory. Put them in a |
| 662 | * GData : permits to access them using their tracefile group pathname. |
| 663 | * i.e. access control/modules tracefile group by index : |
| 664 | * "control/module". |
| 665 | * |
| 666 | * relative path is the path relative to the trace root |
| 667 | * root path is the full path |
| 668 | * |
| 669 | * A tracefile group is simply an array where all the per cpu tracefiles sit. |
| 670 | */ |
| 671 | |
| 672 | static int open_tracefiles(LttTrace *trace, gchar *root_path, gchar *relative_path) |
| 673 | { |
| 674 | DIR *dir = opendir(root_path); |
| 675 | struct dirent *entry; |
| 676 | struct stat stat_buf; |
| 677 | int ret, i; |
| 678 | struct marker_data *mdata; |
| 679 | |
| 680 | gchar path[PATH_MAX]; |
| 681 | int path_len; |
| 682 | gchar *path_ptr; |
| 683 | |
| 684 | int rel_path_len; |
| 685 | gchar rel_path[PATH_MAX]; |
| 686 | gchar *rel_path_ptr; |
| 687 | LttTracefile tmp_tf; |
| 688 | |
| 689 | if(dir == NULL) { |
| 690 | perror(root_path); |
| 691 | return ENOENT; |
| 692 | } |
| 693 | |
| 694 | strncpy(path, root_path, PATH_MAX-1); |
| 695 | path_len = strlen(path); |
| 696 | path[path_len] = '/'; |
| 697 | path_len++; |
| 698 | path_ptr = path + path_len; |
| 699 | |
| 700 | strncpy(rel_path, relative_path, PATH_MAX-1); |
| 701 | rel_path_len = strlen(rel_path); |
| 702 | rel_path[rel_path_len] = '/'; |
| 703 | rel_path_len++; |
| 704 | rel_path_ptr = rel_path + rel_path_len; |
| 705 | |
| 706 | while((entry = readdir(dir)) != NULL) { |
| 707 | if(entry->d_name[0] == '.') continue; |
| 708 | |
| 709 | strncpy(path_ptr, entry->d_name, PATH_MAX - path_len); |
| 710 | strncpy(rel_path_ptr, entry->d_name, PATH_MAX - rel_path_len); |
| 711 | |
| 712 | ret = stat(path, &stat_buf); |
| 713 | if(ret == -1) { |
| 714 | perror(path); |
| 715 | continue; |
| 716 | } |
| 717 | |
| 718 | g_debug("Tracefile file or directory : %s\n", path); |
| 719 | |
| 720 | // if(strcmp(rel_path, "/eventdefs") == 0) continue; |
| 721 | |
| 722 | if(S_ISDIR(stat_buf.st_mode)) { |
| 723 | |
| 724 | g_debug("Entering subdirectory...\n"); |
| 725 | ret = open_tracefiles(trace, path, rel_path); |
| 726 | if(ret < 0) continue; |
| 727 | } else if(S_ISREG(stat_buf.st_mode)) { |
| 728 | GQuark name; |
| 729 | guint num; |
| 730 | gulong tid, pgid; |
| 731 | guint64 creation; |
| 732 | GArray *group; |
| 733 | num = 0; |
| 734 | tid = pgid = 0; |
| 735 | creation = 0; |
| 736 | if(get_tracefile_name_number(rel_path, &name, &num, &tid, &pgid, &creation)) |
| 737 | continue; /* invalid name */ |
| 738 | |
| 739 | g_debug("Opening file.\n"); |
| 740 | if(ltt_tracefile_open(trace, path, &tmp_tf)) { |
| 741 | /* Only consider the error for non live trace */ |
| 742 | if (!trace->is_live) { |
| 743 | |
| 744 | g_info("Error opening tracefile %s", path); |
| 745 | continue; /* error opening the tracefile : bad magic number ? */ |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | g_debug("Tracefile name is %s and number is %u", |
| 750 | g_quark_to_string(name), num); |
| 751 | |
| 752 | mdata = NULL; |
| 753 | tmp_tf.cpu_online = 1; |
| 754 | tmp_tf.cpu_num = num; |
| 755 | tmp_tf.name = name; |
| 756 | tmp_tf.tid = tid; |
| 757 | tmp_tf.pgid = pgid; |
| 758 | tmp_tf.creation = creation; |
| 759 | group = g_datalist_id_get_data(&trace->tracefiles, name); |
| 760 | if(group == NULL) { |
| 761 | /* Elements are automatically cleared when the array is allocated. |
| 762 | * It makes the cpu_online variable set to 0 : cpu offline, by default. |
| 763 | */ |
| 764 | group = g_array_sized_new (FALSE, TRUE, sizeof(LttTracefile), 10); |
| 765 | g_datalist_id_set_data_full(&trace->tracefiles, name, |
| 766 | group, ltt_tracefile_group_destroy); |
| 767 | mdata = allocate_marker_data(); |
| 768 | if (!mdata) |
| 769 | g_error("Error in allocating marker data"); |
| 770 | } |
| 771 | |
| 772 | /* Add the per cpu tracefile to the named group */ |
| 773 | unsigned int old_len = group->len; |
| 774 | if(num+1 > old_len) |
| 775 | group = g_array_set_size(group, num+1); |
| 776 | |
| 777 | g_assert(group->len > 0); |
| 778 | if (!mdata) |
| 779 | mdata = g_array_index (group, LttTracefile, 0).mdata; |
| 780 | |
| 781 | g_array_index (group, LttTracefile, num) = tmp_tf; |
| 782 | g_array_index (group, LttTracefile, num).event.tracefile = |
| 783 | &g_array_index (group, LttTracefile, num); |
| 784 | for (i = 0; i < group->len; i++) |
| 785 | g_array_index (group, LttTracefile, i).mdata = mdata; |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | closedir(dir); |
| 790 | |
| 791 | return 0; |
| 792 | } |
| 793 | |
| 794 | |
| 795 | /* Presumes the tracefile is already seeked at the beginning. It makes sense, |
| 796 | * because it must be done just after the opening */ |
| 797 | static int ltt_process_metadata_tracefile(LttTracefile *tf) |
| 798 | { |
| 799 | int err; |
| 800 | |
| 801 | while(1) { |
| 802 | err = ltt_tracefile_read_seek(tf); |
| 803 | if(err == EPERM) goto seek_error; |
| 804 | else if(err == ERANGE) break; /* End of tracefile */ |
| 805 | |
| 806 | err = ltt_tracefile_read_update_event(tf); |
| 807 | if(err) goto update_error; |
| 808 | |
| 809 | /* The rules are : |
| 810 | * It contains only core events : |
| 811 | * 0 : set_marker_id |
| 812 | * 1 : set_marker_format |
| 813 | */ |
| 814 | if(tf->event.event_id >= MARKER_CORE_IDS) { |
| 815 | /* Should only contain core events */ |
| 816 | g_warning("Error in processing metadata file %s, " |
| 817 | "should not contain event id %u.", g_quark_to_string(tf->name), |
| 818 | tf->event.event_id); |
| 819 | err = EPERM; |
| 820 | goto event_id_error; |
| 821 | } else { |
| 822 | char *pos; |
| 823 | const char *channel_name, *marker_name, *format; |
| 824 | uint16_t id; |
| 825 | guint8 int_size, long_size, pointer_size, size_t_size, alignment; |
| 826 | |
| 827 | switch((enum marker_id)tf->event.event_id) { |
| 828 | case MARKER_ID_SET_MARKER_ID: |
| 829 | channel_name = pos = tf->event.data; |
| 830 | pos += strlen(channel_name) + 1; |
| 831 | marker_name = pos; |
| 832 | g_debug("Doing MARKER_ID_SET_MARKER_ID of marker %s.%s", |
| 833 | channel_name, marker_name); |
| 834 | pos += strlen(marker_name) + 1; |
| 835 | pos += ltt_align((size_t)pos, sizeof(guint16), tf->alignment); |
| 836 | id = ltt_get_uint16(LTT_GET_BO(tf), pos); |
| 837 | g_debug("In MARKER_ID_SET_MARKER_ID of marker %s.%s id %hu", |
| 838 | channel_name, marker_name, id); |
| 839 | pos += sizeof(guint16); |
| 840 | int_size = *(guint8*)pos; |
| 841 | pos += sizeof(guint8); |
| 842 | long_size = *(guint8*)pos; |
| 843 | pos += sizeof(guint8); |
| 844 | pointer_size = *(guint8*)pos; |
| 845 | pos += sizeof(guint8); |
| 846 | size_t_size = *(guint8*)pos; |
| 847 | pos += sizeof(guint8); |
| 848 | alignment = *(guint8*)pos; |
| 849 | pos += sizeof(guint8); |
| 850 | marker_id_event(tf->trace, |
| 851 | g_quark_from_string(channel_name), |
| 852 | g_quark_from_string(marker_name), |
| 853 | id, int_size, long_size, |
| 854 | pointer_size, size_t_size, alignment); |
| 855 | break; |
| 856 | case MARKER_ID_SET_MARKER_FORMAT: |
| 857 | channel_name = pos = tf->event.data; |
| 858 | pos += strlen(channel_name) + 1; |
| 859 | marker_name = pos; |
| 860 | g_debug("Doing MARKER_ID_SET_MARKER_FORMAT of marker %s.%s", |
| 861 | channel_name, marker_name); |
| 862 | pos += strlen(marker_name) + 1; |
| 863 | format = pos; |
| 864 | pos += strlen(format) + 1; |
| 865 | marker_format_event(tf->trace, |
| 866 | g_quark_from_string(channel_name), |
| 867 | g_quark_from_string(marker_name), |
| 868 | format); |
| 869 | /* get information from dictionary TODO */ |
| 870 | break; |
| 871 | default: |
| 872 | g_warning("Error in processing metadata file %s, " |
| 873 | "unknown event id %hhu.", |
| 874 | g_quark_to_string(tf->name), |
| 875 | tf->event.event_id); |
| 876 | err = EPERM; |
| 877 | goto event_id_error; |
| 878 | } |
| 879 | } |
| 880 | } |
| 881 | return 0; |
| 882 | |
| 883 | /* Error handling */ |
| 884 | event_id_error: |
| 885 | update_error: |
| 886 | seek_error: |
| 887 | g_warning("An error occured in metadata tracefile parsing"); |
| 888 | return err; |
| 889 | } |
| 890 | |
| 891 | |
| 892 | LttTrace *ltt_trace_open(const gchar *pathname) |
| 893 | { |
| 894 | return _ltt_trace_open(pathname, FALSE); |
| 895 | } |
| 896 | |
| 897 | LttTrace *ltt_trace_open_live(const gchar *pathname) |
| 898 | { |
| 899 | return _ltt_trace_open(pathname, TRUE); |
| 900 | } |
| 901 | |
| 902 | /* |
| 903 | * Open a trace and return its LttTrace handle. |
| 904 | * |
| 905 | * pathname must be the directory of the trace |
| 906 | */ |
| 907 | |
| 908 | static LttTrace *_ltt_trace_open(const gchar *pathname, gboolean is_live) |
| 909 | { |
| 910 | gchar abs_path[PATH_MAX]; |
| 911 | LttTrace * t; |
| 912 | LttTracefile *tf; |
| 913 | GArray *group; |
| 914 | unsigned int i; |
| 915 | int ret; |
| 916 | ltt_subbuffer_header_t *header; |
| 917 | DIR *dir; |
| 918 | struct dirent *entry; |
| 919 | struct stat stat_buf; |
| 920 | gchar path[PATH_MAX]; |
| 921 | |
| 922 | t = g_new(LttTrace, 1); |
| 923 | if(!t) goto alloc_error; |
| 924 | |
| 925 | get_absolute_pathname(pathname, abs_path); |
| 926 | t->pathname = g_quark_from_string(abs_path); |
| 927 | |
| 928 | g_datalist_init(&t->tracefiles); |
| 929 | |
| 930 | /* Test to see if it looks like a trace */ |
| 931 | dir = opendir(abs_path); |
| 932 | if(dir == NULL) { |
| 933 | perror(abs_path); |
| 934 | goto open_error; |
| 935 | } |
| 936 | while((entry = readdir(dir)) != NULL) { |
| 937 | strcpy(path, abs_path); |
| 938 | strcat(path, "/"); |
| 939 | strcat(path, entry->d_name); |
| 940 | ret = stat(path, &stat_buf); |
| 941 | if(ret == -1) { |
| 942 | perror(path); |
| 943 | continue; |
| 944 | } |
| 945 | } |
| 946 | closedir(dir); |
| 947 | |
| 948 | t->is_live = is_live; |
| 949 | t->live_safe_timestamp = ltt_time_zero; |
| 950 | |
| 951 | /* Open all the tracefiles */ |
| 952 | t->start_freq= 0; |
| 953 | if(open_tracefiles(t, abs_path, "")) { |
| 954 | g_warning("Error opening tracefile %s", abs_path); |
| 955 | goto find_error; |
| 956 | } |
| 957 | |
| 958 | /* Parse each trace metadata_N files : get runtime fac. info */ |
| 959 | group = g_datalist_id_get_data(&t->tracefiles, LTT_TRACEFILE_NAME_METADATA); |
| 960 | if(group == NULL) { |
| 961 | g_warning("Trace %s has no metadata tracefile", abs_path); |
| 962 | goto find_error; |
| 963 | } |
| 964 | |
| 965 | /* |
| 966 | * Get the trace information for the first valid metadata tracefile. |
| 967 | * In live trace mode, the metadata_0 might be empty |
| 968 | * Getting a correct trace start_time and start_tsc is insured by the fact |
| 969 | * that no subbuffers are supposed to be lost in the metadata channel. |
| 970 | * Therefore, the first subbuffer contains the start_tsc timestamp in its |
| 971 | * buffer header. |
| 972 | */ |
| 973 | g_assert(group->len > 0); |
| 974 | header = NULL; |
| 975 | for(i=0; i<group->len; i++) { |
| 976 | tf = &g_array_index (group, LttTracefile, i); |
| 977 | header = (ltt_subbuffer_header_t *)tf->buffer.head; |
| 978 | if (header) { |
| 979 | break; |
| 980 | } |
| 981 | } |
| 982 | if (header == NULL) { |
| 983 | g_warning("Trace %s has not one valid metadata tracefile", abs_path); |
| 984 | goto find_error; |
| 985 | } |
| 986 | |
| 987 | ret = parse_trace_header(header, tf, t); |
| 988 | g_assert(!ret); |
| 989 | |
| 990 | t->num_cpu = group->len; |
| 991 | t->drift = 1.; |
| 992 | t->offset = 0.; |
| 993 | |
| 994 | //ret = allocate_marker_data(t); |
| 995 | //if (ret) |
| 996 | // g_error("Error in allocating marker data"); |
| 997 | |
| 998 | for(i=0; i<group->len; i++) { |
| 999 | tf = &g_array_index (group, LttTracefile, i); |
| 1000 | if (tf->cpu_online && tf->buffer.head ) |
| 1001 | if(ltt_process_metadata_tracefile(tf)) |
| 1002 | goto find_error; |
| 1003 | // goto metadata_error; |
| 1004 | } |
| 1005 | |
| 1006 | return t; |
| 1007 | |
| 1008 | /* Error handling */ |
| 1009 | //metadata_error: |
| 1010 | // destroy_marker_data(t); |
| 1011 | find_error: |
| 1012 | g_datalist_clear(&t->tracefiles); |
| 1013 | open_error: |
| 1014 | g_free(t); |
| 1015 | alloc_error: |
| 1016 | return NULL; |
| 1017 | |
| 1018 | } |
| 1019 | |
| 1020 | |
| 1021 | /***************************************************************************** |
| 1022 | Update the informations concerning the tracefile |
| 1023 | |
| 1024 | Must be called periodically to update trace file and file size |
| 1025 | information. |
| 1026 | |
| 1027 | Input params |
| 1028 | tf : the tracefile |
| 1029 | Return value |
| 1030 | : Number of tracefile with available events |
| 1031 | -1 on error. |
| 1032 | ****************************************************************************/ |
| 1033 | int ltt_trace_update(LttTrace *trace) |
| 1034 | { |
| 1035 | int trace_updated_count = 0; |
| 1036 | |
| 1037 | /* Only update live traces */ |
| 1038 | if(trace->is_live) { |
| 1039 | |
| 1040 | /* Iterate on all tracefiles */ |
| 1041 | g_datalist_foreach(&trace->tracefiles, |
| 1042 | &update_tracefile_group, |
| 1043 | &trace_updated_count); |
| 1044 | |
| 1045 | return trace_updated_count; |
| 1046 | } |
| 1047 | return 0; |
| 1048 | |
| 1049 | } |
| 1050 | |
| 1051 | static void update_tracefile_group(GQuark name, gpointer data, gpointer user_data) |
| 1052 | { |
| 1053 | int *trace_updated_count = (int *)user_data; |
| 1054 | unsigned int i; |
| 1055 | LttTracefile *tf; |
| 1056 | GArray *group = (GArray *)data; |
| 1057 | |
| 1058 | g_debug("Updating tracefile group %s", g_quark_to_string(name)); |
| 1059 | for(i=0; i<group->len; i++) { |
| 1060 | tf = &g_array_index (group, LttTracefile, i); |
| 1061 | |
| 1062 | /* Update safe timestamp */ |
| 1063 | tf->trace->live_safe_timestamp = |
| 1064 | LTT_TIME_MAX(tf->trace->live_safe_timestamp, |
| 1065 | ltt_interpolate_time_from_tsc(tf, |
| 1066 | tf->end_timestamp)); |
| 1067 | |
| 1068 | /* Update tracefile */ |
| 1069 | int ret = ltt_tracefile_update(tf); |
| 1070 | if(ret < 0) { |
| 1071 | g_warning("LIVE: Cannot update tracefile %s", |
| 1072 | g_quark_to_string(ltt_tracefile_long_name(tf))); |
| 1073 | *trace_updated_count = -1; |
| 1074 | break; |
| 1075 | } else { |
| 1076 | *trace_updated_count += 1; |
| 1077 | } |
| 1078 | } |
| 1079 | } |
| 1080 | |
| 1081 | |
| 1082 | /***************************************************************************** |
| 1083 | *Function name |
| 1084 | * ltt_tracefile_update : Update the informations concerning a tracefile |
| 1085 | * Must be called periodically to update trace file and file size |
| 1086 | information. |
| 1087 | *Input params |
| 1088 | * tf : the tracefile |
| 1089 | *Return value |
| 1090 | * : 1 for success and an event is available |
| 1091 | 0 for success but no event available, |
| 1092 | * -1 on error. |
| 1093 | ****************************************************************************/ |
| 1094 | static int ltt_tracefile_update(LttTracefile *tf) |
| 1095 | { |
| 1096 | struct stat lTDFStat; /* Trace data file status */ |
| 1097 | if(fstat(tf->fd, &lTDFStat) < 0){ |
| 1098 | perror("error in getting the tracefile informations."); |
| 1099 | } |
| 1100 | |
| 1101 | |
| 1102 | /* Process the file only on size change */ |
| 1103 | if(tf->file_size < lTDFStat.st_size) { |
| 1104 | /* Update the size of the file */ |
| 1105 | tf->file_size = lTDFStat.st_size; |
| 1106 | g_debug("Updating tracefile %s", g_quark_to_string(tf->long_name)); |
| 1107 | |
| 1108 | if( tf->file_size >= (off_t)(ltt_subbuffer_header_size()) ) { |
| 1109 | |
| 1110 | if(tf->buf_index == NULL) { |
| 1111 | if(ltt_tracefile_init(tf) < 0) { |
| 1112 | return -1; |
| 1113 | } |
| 1114 | if(tf->name == LTT_TRACEFILE_NAME_METADATA) { |
| 1115 | |
| 1116 | LttTime start; |
| 1117 | start.tv_sec = 0; |
| 1118 | start.tv_nsec = 0; |
| 1119 | ltt_process_metadata_tracefile(tf); |
| 1120 | |
| 1121 | ltt_tracefile_seek_time(tf, start); |
| 1122 | tf->event.offset = 0; |
| 1123 | |
| 1124 | } |
| 1125 | } |
| 1126 | else |
| 1127 | { |
| 1128 | /* Retrieve the new subbuffers and index them */ |
| 1129 | ltt_trace_continue_block_index(tf); |
| 1130 | |
| 1131 | if(tf->name == LTT_TRACEFILE_NAME_METADATA) { |
| 1132 | LttEventPosition *pos = ltt_event_position_new(); |
| 1133 | ltt_tracefile_get_current_position(tf, pos); |
| 1134 | ltt_process_metadata_tracefile(tf); |
| 1135 | ltt_tracefile_seek_position(tf, pos); |
| 1136 | g_free(pos); |
| 1137 | |
| 1138 | } |
| 1139 | } |
| 1140 | |
| 1141 | return 1; |
| 1142 | } |
| 1143 | } |
| 1144 | |
| 1145 | return 0; |
| 1146 | |
| 1147 | } |
| 1148 | |
| 1149 | /* Open another, completely independant, instance of a trace. |
| 1150 | * |
| 1151 | * A read on this new instance will read the first event of the trace. |
| 1152 | * |
| 1153 | * When we copy a trace, we want all the opening actions to happen again : |
| 1154 | * the trace will be reopened and totally independant from the original. |
| 1155 | * That's why we call ltt_trace_open. |
| 1156 | */ |
| 1157 | LttTrace *ltt_trace_copy(LttTrace *self) |
| 1158 | { |
| 1159 | return _ltt_trace_open(g_quark_to_string(self->pathname), self->is_live); |
| 1160 | } |
| 1161 | |
| 1162 | /* |
| 1163 | * Close a trace |
| 1164 | */ |
| 1165 | |
| 1166 | void ltt_trace_close(LttTrace *t) |
| 1167 | { |
| 1168 | g_datalist_clear(&t->tracefiles); |
| 1169 | g_free(t); |
| 1170 | } |
| 1171 | |
| 1172 | |
| 1173 | /***************************************************************************** |
| 1174 | * Get the start time and end time of the trace |
| 1175 | ****************************************************************************/ |
| 1176 | |
| 1177 | void ltt_tracefile_time_span_get(LttTracefile *tf, |
| 1178 | LttTime *start, LttTime *end) |
| 1179 | { |
| 1180 | int err; |
| 1181 | |
| 1182 | |
| 1183 | err = map_block(tf, 0); |
| 1184 | /* Only empty live traces will return ERANGE */ |
| 1185 | if(err == ERANGE) { |
| 1186 | *start = ltt_time_infinite; |
| 1187 | *end = ltt_time_zero; |
| 1188 | return; |
| 1189 | } else if(unlikely(err)) { |
| 1190 | g_error("Can not map block"); |
| 1191 | *start = ltt_time_infinite; |
| 1192 | } else |
| 1193 | *start = tf->buffer.begin.timestamp; |
| 1194 | |
| 1195 | err = map_block(tf, tf->num_blocks - 1); /* Last block */ |
| 1196 | if(unlikely(err)) { |
| 1197 | g_error("Can not map block"); |
| 1198 | *end = ltt_time_zero; |
| 1199 | } else |
| 1200 | *end = tf->buffer.end.timestamp; |
| 1201 | |
| 1202 | g_assert(end->tv_sec <= G_MAXUINT); |
| 1203 | } |
| 1204 | |
| 1205 | struct tracefile_time_span_get_args { |
| 1206 | LttTrace *t; |
| 1207 | LttTime *start; |
| 1208 | LttTime *end; |
| 1209 | }; |
| 1210 | |
| 1211 | static void group_time_span_get(GQuark name, gpointer data, gpointer user_data) |
| 1212 | { |
| 1213 | struct tracefile_time_span_get_args *args = |
| 1214 | (struct tracefile_time_span_get_args*)user_data; |
| 1215 | |
| 1216 | GArray *group = (GArray *)data; |
| 1217 | unsigned int i; |
| 1218 | LttTracefile *tf; |
| 1219 | LttTime tmp_start; |
| 1220 | LttTime tmp_end; |
| 1221 | |
| 1222 | for(i=0; i<group->len; i++) { |
| 1223 | tf = &g_array_index (group, LttTracefile, i); |
| 1224 | if(tf->cpu_online) { |
| 1225 | ltt_tracefile_time_span_get(tf, &tmp_start, &tmp_end); |
| 1226 | if(ltt_time_compare(*args->start, tmp_start)>0) *args->start = tmp_start; |
| 1227 | if(ltt_time_compare(*args->end, tmp_end)<0) *args->end = tmp_end; |
| 1228 | } |
| 1229 | } |
| 1230 | } |
| 1231 | |
| 1232 | /* return the start and end time of a trace */ |
| 1233 | |
| 1234 | void ltt_trace_time_span_get(LttTrace *t, LttTime *start, LttTime *end) |
| 1235 | { |
| 1236 | LttTime min_start = ltt_time_infinite; |
| 1237 | LttTime max_end = ltt_time_zero; |
| 1238 | struct tracefile_time_span_get_args args = { t, &min_start, &max_end }; |
| 1239 | |
| 1240 | g_datalist_foreach(&t->tracefiles, &group_time_span_get, &args); |
| 1241 | |
| 1242 | if(start != NULL) *start = min_start; |
| 1243 | if(end != NULL) *end = max_end; |
| 1244 | |
| 1245 | } |
| 1246 | |
| 1247 | |
| 1248 | /* Seek to the first event in a tracefile that has a time equal or greater than |
| 1249 | * the time passed in parameter. |
| 1250 | * |
| 1251 | * If the time parameter is outside the tracefile time span, seek to the first |
| 1252 | * event or if after, return ERANGE. |
| 1253 | * |
| 1254 | * If the time parameter is before the first event, we have to seek specially to |
| 1255 | * there. |
| 1256 | * |
| 1257 | * If the time is after the end of the trace, return ERANGE. |
| 1258 | * |
| 1259 | * Do a binary search to find the right block, then a sequential search in the |
| 1260 | * block to find the event. |
| 1261 | * |
| 1262 | * In the special case where the time requested fits inside a block that has no |
| 1263 | * event corresponding to the requested time, the first event of the next block |
| 1264 | * will be seeked. |
| 1265 | * |
| 1266 | * IMPORTANT NOTE : // FIXME everywhere... |
| 1267 | * |
| 1268 | * You MUST NOT do a ltt_tracefile_read right after a ltt_tracefile_seek_time : |
| 1269 | * you will jump over an event if you do. |
| 1270 | * |
| 1271 | * Return value : 0 : no error, the tf->event can be used |
| 1272 | * ERANGE : time if after the last event of the trace |
| 1273 | * otherwise : this is an error. |
| 1274 | * |
| 1275 | * */ |
| 1276 | |
| 1277 | int ltt_tracefile_seek_time(LttTracefile *tf, LttTime time) |
| 1278 | { |
| 1279 | int ret = 0; |
| 1280 | int err; |
| 1281 | unsigned int block_num, high, low; |
| 1282 | |
| 1283 | /* seek at the beginning of trace */ |
| 1284 | err = map_block(tf, 0); /* First block */ |
| 1285 | if(unlikely(err == ERANGE)) { |
| 1286 | goto range; |
| 1287 | } else if(unlikely(err)) { |
| 1288 | g_error("Can not map block"); |
| 1289 | goto fail; |
| 1290 | } |
| 1291 | |
| 1292 | /* If the time is lower or equal the beginning of the trace, |
| 1293 | * go to the first event. */ |
| 1294 | if(ltt_time_compare(time, tf->buffer.begin.timestamp) <= 0) { |
| 1295 | ret = ltt_tracefile_read(tf); |
| 1296 | if(ret == ERANGE) goto range; |
| 1297 | else if (ret) goto fail; |
| 1298 | goto found; /* There is either no event in the trace or the event points |
| 1299 | to the first event in the trace */ |
| 1300 | } |
| 1301 | |
| 1302 | err = map_block(tf, tf->num_blocks - 1); /* Last block */ |
| 1303 | if(unlikely(err)) { |
| 1304 | g_error("Can not map block"); |
| 1305 | goto fail; |
| 1306 | } |
| 1307 | |
| 1308 | /* If the time is after the end of the trace, return ERANGE. */ |
| 1309 | if(ltt_time_compare(time, tf->buffer.end.timestamp) > 0) { |
| 1310 | goto range; |
| 1311 | } |
| 1312 | |
| 1313 | /* Binary search the block */ |
| 1314 | high = tf->num_blocks - 1; |
| 1315 | low = 0; |
| 1316 | |
| 1317 | while(1) { |
| 1318 | block_num = ((high-low) / 2) + low; |
| 1319 | |
| 1320 | err = map_block(tf, block_num); |
| 1321 | if(unlikely(err)) { |
| 1322 | g_error("Can not map block"); |
| 1323 | goto fail; |
| 1324 | } |
| 1325 | if(high == low) { |
| 1326 | /* We cannot divide anymore : this is what would happen if the time |
| 1327 | * requested was exactly between two consecutive buffers'end and start |
| 1328 | * timestamps. This is also what would happend if we didn't deal with out |
| 1329 | * of span cases prior in this function. */ |
| 1330 | /* The event is right in the buffer! |
| 1331 | * (or in the next buffer first event) */ |
| 1332 | while(1) { |
| 1333 | ret = ltt_tracefile_read(tf); |
| 1334 | if(ret == ERANGE) goto range; /* ERANGE or EPERM */ |
| 1335 | else if(ret) goto fail; |
| 1336 | |
| 1337 | if(ltt_time_compare(time, tf->event.event_time) <= 0) |
| 1338 | goto found; |
| 1339 | } |
| 1340 | |
| 1341 | } else if(ltt_time_compare(time, tf->buffer.begin.timestamp) < 0) { |
| 1342 | /* |
| 1343 | * Go to lower part. We don't want block_num - 1 since block_num |
| 1344 | * can equal low , in which case high < low. |
| 1345 | */ |
| 1346 | high = block_num; |
| 1347 | } else if(ltt_time_compare(time, tf->buffer.end.timestamp) > 0) { |
| 1348 | /* go to higher part */ |
| 1349 | low = block_num + 1; |
| 1350 | } else {/* The event is right in the buffer! |
| 1351 | (or in the next buffer first event) */ |
| 1352 | while(1) { |
| 1353 | ret = ltt_tracefile_read(tf); |
| 1354 | if(ret == ERANGE) goto range; /* ERANGE or EPERM */ |
| 1355 | else if(ret) goto fail; |
| 1356 | |
| 1357 | if(ltt_time_compare(time, tf->event.event_time) <= 0) |
| 1358 | break; |
| 1359 | } |
| 1360 | goto found; |
| 1361 | } |
| 1362 | } |
| 1363 | |
| 1364 | found: |
| 1365 | return 0; |
| 1366 | range: |
| 1367 | return ERANGE; |
| 1368 | |
| 1369 | /* Error handling */ |
| 1370 | fail: |
| 1371 | g_error("ltt_tracefile_seek_time failed on tracefile %s", |
| 1372 | g_quark_to_string(tf->name)); |
| 1373 | return EPERM; |
| 1374 | } |
| 1375 | |
| 1376 | |
| 1377 | /* Save the current tracefile position in the passed position pointer */ |
| 1378 | int ltt_tracefile_get_current_position(const LttTracefile *tf, LttEventPosition *ep) |
| 1379 | { |
| 1380 | /* TODO ybrosseau 2011-06-07: Maybe add some error checking |
| 1381 | (ex: check the validity of the arguments pointer) */ |
| 1382 | ltt_event_position(&(tf->event), ep); |
| 1383 | return 0; |
| 1384 | } |
| 1385 | |
| 1386 | |
| 1387 | /* Seek to a position indicated by an LttEventPosition |
| 1388 | */ |
| 1389 | |
| 1390 | int ltt_tracefile_seek_position(LttTracefile *tf, const LttEventPosition *ep) |
| 1391 | { |
| 1392 | int err; |
| 1393 | |
| 1394 | if(ep->tracefile != tf) { |
| 1395 | goto fail; |
| 1396 | } |
| 1397 | |
| 1398 | err = map_block(tf, ep->block); |
| 1399 | if(unlikely(err)) { |
| 1400 | g_error("Can not map block"); |
| 1401 | goto fail; |
| 1402 | } |
| 1403 | |
| 1404 | tf->event.offset = ep->offset; |
| 1405 | |
| 1406 | /* Put back the event real tsc */ |
| 1407 | tf->event.tsc = ep->tsc; |
| 1408 | tf->buffer.tsc = ep->tsc; |
| 1409 | |
| 1410 | err = ltt_tracefile_read_update_event(tf); |
| 1411 | if(err) goto fail; |
| 1412 | |
| 1413 | /* deactivate this, as it does nothing for now |
| 1414 | err = ltt_tracefile_read_op(tf); |
| 1415 | if(err) goto fail; |
| 1416 | */ |
| 1417 | |
| 1418 | return 0; |
| 1419 | |
| 1420 | fail: |
| 1421 | g_error("ltt_tracefile_seek_time failed on tracefile %s", |
| 1422 | g_quark_to_string(tf->name)); |
| 1423 | return 1; |
| 1424 | } |
| 1425 | |
| 1426 | /* |
| 1427 | * Convert a value in "TSC scale" to a value in nanoseconds |
| 1428 | */ |
| 1429 | guint64 tsc_to_uint64(guint32 freq_scale, uint64_t start_freq, guint64 tsc) |
| 1430 | { |
| 1431 | return (double) tsc * NANOSECONDS_PER_SECOND * freq_scale / start_freq; |
| 1432 | } |
| 1433 | |
| 1434 | /* Given a TSC value, return the LttTime (seconds,nanoseconds) it |
| 1435 | * corresponds to. |
| 1436 | */ |
| 1437 | LttTime ltt_interpolate_time_from_tsc(LttTracefile *tf, guint64 tsc) |
| 1438 | { |
| 1439 | return ltt_time_from_uint64(tsc_to_uint64(tf->trace->freq_scale, |
| 1440 | tf->trace->start_freq, tf->trace->drift * tsc + |
| 1441 | tf->trace->offset)); |
| 1442 | } |
| 1443 | |
| 1444 | /* Calculate the real event time based on the buffer boundaries */ |
| 1445 | LttTime ltt_interpolate_time(LttTracefile *tf, LttEvent *event) |
| 1446 | { |
| 1447 | return ltt_interpolate_time_from_tsc(tf, tf->buffer.tsc); |
| 1448 | } |
| 1449 | |
| 1450 | |
| 1451 | /* Get the current event of the tracefile : valid until the next read */ |
| 1452 | LttEvent *ltt_tracefile_get_event(LttTracefile *tf) |
| 1453 | { |
| 1454 | return &tf->event; |
| 1455 | } |
| 1456 | |
| 1457 | /***************************************************************************** |
| 1458 | *Function name |
| 1459 | * ltt_tracefile_read : Read the next event in the tracefile |
| 1460 | *Input params |
| 1461 | * t : tracefile |
| 1462 | *Return value |
| 1463 | * |
| 1464 | * Returns 0 if an event can be used in tf->event. |
| 1465 | * Returns ERANGE on end of trace. The event in tf->event still can be used |
| 1466 | * (if the last block was not empty). |
| 1467 | * Returns EPERM on error. |
| 1468 | * |
| 1469 | * This function does make the tracefile event structure point to the event |
| 1470 | * currently pointed to by the tf->event. |
| 1471 | * |
| 1472 | * Note : you must call a ltt_tracefile_seek to the beginning of the trace to |
| 1473 | * reinitialize it after an error if you want results to be coherent. |
| 1474 | * It would be the case if a end of trace last buffer has no event : the end |
| 1475 | * of trace wouldn't be returned, but an error. |
| 1476 | * We make the assumption there is at least one event per buffer. |
| 1477 | ****************************************************************************/ |
| 1478 | |
| 1479 | int ltt_tracefile_read(LttTracefile *tf) |
| 1480 | { |
| 1481 | int err; |
| 1482 | |
| 1483 | err = ltt_tracefile_read_seek(tf); |
| 1484 | if(err) return err; |
| 1485 | err = ltt_tracefile_read_update_event(tf); |
| 1486 | if(err) return err; |
| 1487 | |
| 1488 | /* deactivate this, as it does nothing for now |
| 1489 | err = ltt_tracefile_read_op(tf); |
| 1490 | if(err) return err; |
| 1491 | */ |
| 1492 | |
| 1493 | return 0; |
| 1494 | } |
| 1495 | |
| 1496 | int ltt_tracefile_read_seek(LttTracefile *tf) |
| 1497 | { |
| 1498 | int err; |
| 1499 | |
| 1500 | /* Get next buffer until we finally have an event, or end of trace */ |
| 1501 | while(1) { |
| 1502 | err = ltt_seek_next_event(tf); |
| 1503 | if(unlikely(err == ENOPROTOOPT)) { |
| 1504 | return EPERM; |
| 1505 | } |
| 1506 | |
| 1507 | /* Are we at the end of the buffer ? */ |
| 1508 | if(err == ERANGE) { |
| 1509 | if(unlikely(tf->buffer.index == tf->num_blocks-1)){ /* end of trace ? */ |
| 1510 | return ERANGE; |
| 1511 | } else { |
| 1512 | /* get next block */ |
| 1513 | err = map_block(tf, tf->buffer.index + 1); |
| 1514 | if(unlikely(err)) { |
| 1515 | g_error("Can not map block"); |
| 1516 | return EPERM; |
| 1517 | } |
| 1518 | } |
| 1519 | } else break; /* We found an event ! */ |
| 1520 | } |
| 1521 | |
| 1522 | return 0; |
| 1523 | } |
| 1524 | |
| 1525 | /* do an operation when reading a new event */ |
| 1526 | |
| 1527 | /* This function does nothing for now */ |
| 1528 | #if 0 |
| 1529 | int ltt_tracefile_read_op(LttTracefile *tf) |
| 1530 | { |
| 1531 | LttEvent *event; |
| 1532 | |
| 1533 | event = &tf->event; |
| 1534 | |
| 1535 | /* do event specific operation */ |
| 1536 | |
| 1537 | /* nothing */ |
| 1538 | |
| 1539 | return 0; |
| 1540 | } |
| 1541 | #endif |
| 1542 | |
| 1543 | static void print_debug_event_header(LttEvent *ev, void *start_pos, void *end_pos) |
| 1544 | { |
| 1545 | unsigned int offset = 0; |
| 1546 | int i, j; |
| 1547 | |
| 1548 | g_printf("Event header (tracefile %s offset %" PRIx64 "):\n", |
| 1549 | g_quark_to_string(ev->tracefile->long_name), |
| 1550 | (uint64_t)ev->tracefile->buffer.offset + |
| 1551 | (long)start_pos - (long)ev->tracefile->buffer.head); |
| 1552 | |
| 1553 | while (offset < (long)end_pos - (long)start_pos) { |
| 1554 | g_printf("%8lx", (long)start_pos - (long)ev->tracefile->buffer.head + offset); |
| 1555 | g_printf(" "); |
| 1556 | |
| 1557 | for (i = 0; i < 4 ; i++) { |
| 1558 | for (j = 0; j < 4; j++) { |
| 1559 | if (offset + ((i * 4) + j) < |
| 1560 | (long)end_pos - (long)start_pos) |
| 1561 | g_printf("%02hhX", |
| 1562 | ((char*)start_pos)[offset + ((i * 4) + j)]); |
| 1563 | else |
| 1564 | g_printf(" "); |
| 1565 | g_printf(" "); |
| 1566 | } |
| 1567 | if (i < 4) |
| 1568 | g_printf(" "); |
| 1569 | } |
| 1570 | offset+=16; |
| 1571 | g_printf("\n"); |
| 1572 | } |
| 1573 | } |
| 1574 | |
| 1575 | |
| 1576 | /* same as ltt_tracefile_read, but does not seek to the next event nor call |
| 1577 | * event specific operation. */ |
| 1578 | int ltt_tracefile_read_update_event(LttTracefile *tf) |
| 1579 | { |
| 1580 | void * pos; |
| 1581 | LttEvent *event; |
| 1582 | void *pos_aligned; |
| 1583 | guint16 packed_evid; /* event id reader from the 5 bits in header */ |
| 1584 | |
| 1585 | event = &tf->event; |
| 1586 | pos = tf->buffer.head + event->offset; |
| 1587 | |
| 1588 | /* Read event header */ |
| 1589 | |
| 1590 | /* Align the head */ |
| 1591 | pos += ltt_align((size_t)pos, sizeof(guint32), tf->alignment); |
| 1592 | pos_aligned = pos; |
| 1593 | |
| 1594 | event->timestamp = ltt_get_uint32(LTT_GET_BO(tf), pos); |
| 1595 | event->event_id = packed_evid = event->timestamp >> tf->tscbits; |
| 1596 | event->timestamp = event->timestamp & tf->tsc_mask; |
| 1597 | pos += sizeof(guint32); |
| 1598 | |
| 1599 | switch (packed_evid) { |
| 1600 | case 29: /* LTT_RFLAG_ID_SIZE_TSC */ |
| 1601 | event->event_id = ltt_get_uint16(LTT_GET_BO(tf), pos); |
| 1602 | pos += sizeof(guint16); |
| 1603 | event->event_size = ltt_get_uint16(LTT_GET_BO(tf), pos); |
| 1604 | pos += sizeof(guint16); |
| 1605 | if (event->event_size == 0xFFFF) { |
| 1606 | event->event_size = ltt_get_uint32(LTT_GET_BO(tf), pos); |
| 1607 | pos += sizeof(guint32); |
| 1608 | } |
| 1609 | pos += ltt_align((size_t)pos, sizeof(guint64), tf->alignment); |
| 1610 | tf->buffer.tsc = ltt_get_uint64(LTT_GET_BO(tf), pos); |
| 1611 | pos += sizeof(guint64); |
| 1612 | break; |
| 1613 | case 30: /* LTT_RFLAG_ID_SIZE */ |
| 1614 | event->event_id = ltt_get_uint16(LTT_GET_BO(tf), pos); |
| 1615 | pos += sizeof(guint16); |
| 1616 | event->event_size = ltt_get_uint16(LTT_GET_BO(tf), pos); |
| 1617 | pos += sizeof(guint16); |
| 1618 | if (event->event_size == 0xFFFF) { |
| 1619 | event->event_size = ltt_get_uint32(LTT_GET_BO(tf), pos); |
| 1620 | pos += sizeof(guint32); |
| 1621 | } |
| 1622 | break; |
| 1623 | case 31: /* LTT_RFLAG_ID */ |
| 1624 | event->event_id = ltt_get_uint16(LTT_GET_BO(tf), pos); |
| 1625 | pos += sizeof(guint16); |
| 1626 | event->event_size = G_MAXUINT; |
| 1627 | break; |
| 1628 | default: |
| 1629 | event->event_size = G_MAXUINT; |
| 1630 | break; |
| 1631 | } |
| 1632 | |
| 1633 | if (likely(packed_evid != 29)) { |
| 1634 | /* No extended timestamp */ |
| 1635 | if (event->timestamp < (tf->buffer.tsc & tf->tsc_mask)) |
| 1636 | tf->buffer.tsc = ((tf->buffer.tsc & ~tf->tsc_mask) /* overflow */ |
| 1637 | + tf->tsc_mask_next_bit) |
| 1638 | | (guint64)event->timestamp; |
| 1639 | else |
| 1640 | tf->buffer.tsc = (tf->buffer.tsc & ~tf->tsc_mask) /* no overflow */ |
| 1641 | | (guint64)event->timestamp; |
| 1642 | } |
| 1643 | event->tsc = tf->buffer.tsc; |
| 1644 | |
| 1645 | event->event_time = ltt_interpolate_time(tf, event); |
| 1646 | |
| 1647 | if (a_event_debug) |
| 1648 | print_debug_event_header(event, pos_aligned, pos); |
| 1649 | |
| 1650 | event->data = pos; |
| 1651 | |
| 1652 | /* |
| 1653 | * Let ltt_update_event_size update event->data according to the largest |
| 1654 | * alignment within the payload. |
| 1655 | * Get the data size and update the event fields with the current |
| 1656 | * information. */ |
| 1657 | ltt_update_event_size(tf); |
| 1658 | |
| 1659 | return 0; |
| 1660 | } |
| 1661 | |
| 1662 | |
| 1663 | /**************************************************************************** |
| 1664 | *Function name |
| 1665 | * map_block : map a block from the file |
| 1666 | *Input Params |
| 1667 | * lttdes : ltt trace file |
| 1668 | * whichBlock : the block which will be read |
| 1669 | *return value |
| 1670 | * 0 : success |
| 1671 | * EINVAL : lseek fail |
| 1672 | * EIO : can not read from the file |
| 1673 | ****************************************************************************/ |
| 1674 | |
| 1675 | static gint map_block(LttTracefile * tf, guint block_num) |
| 1676 | { |
| 1677 | int page_size = getpagesize(); |
| 1678 | ltt_subbuffer_header_t *header; |
| 1679 | uint64_t offset; |
| 1680 | uint32_t size; |
| 1681 | int ret; |
| 1682 | |
| 1683 | if(tf->num_blocks == 0) { |
| 1684 | errno = ERANGE; |
| 1685 | return ERANGE; |
| 1686 | } |
| 1687 | |
| 1688 | g_assert(block_num < tf->num_blocks); |
| 1689 | |
| 1690 | if(tf->buffer.head != NULL) { |
| 1691 | if(munmap(tf->buffer.head, PAGE_ALIGN(tf->buffer.size))) { |
| 1692 | g_warning("unmap size : %u\n", |
| 1693 | PAGE_ALIGN(tf->buffer.size)); |
| 1694 | perror("munmap error"); |
| 1695 | g_assert(0); |
| 1696 | } |
| 1697 | } |
| 1698 | |
| 1699 | ret = get_block_offset_size(tf, block_num, &offset, &size); |
| 1700 | g_assert(!ret); |
| 1701 | |
| 1702 | g_debug("Map block %u, offset %llu, size %u\n", block_num, |
| 1703 | (unsigned long long)offset, (unsigned int)size); |
| 1704 | |
| 1705 | /* Multiple of pages aligned head */ |
| 1706 | tf->buffer.head = mmap(0, (size_t)size, PROT_READ, MAP_PRIVATE, |
| 1707 | tf->fd, (off_t)offset); |
| 1708 | |
| 1709 | if(tf->buffer.head == MAP_FAILED) { |
| 1710 | perror("Error in allocating memory for buffer of tracefile"); |
| 1711 | g_assert(0); |
| 1712 | goto map_error; |
| 1713 | } |
| 1714 | g_assert( ( (gulong)tf->buffer.head&(8-1) ) == 0); // make sure it's aligned. |
| 1715 | |
| 1716 | tf->buffer.index = block_num; |
| 1717 | |
| 1718 | header = (ltt_subbuffer_header_t *)tf->buffer.head; |
| 1719 | |
| 1720 | tf->buffer.begin.cycle_count = ltt_get_uint64(LTT_GET_BO(tf), |
| 1721 | &header->cycle_count_begin); |
| 1722 | tf->buffer.end.cycle_count = ltt_get_uint64(LTT_GET_BO(tf), |
| 1723 | &header->cycle_count_end); |
| 1724 | tf->buffer.offset = offset; |
| 1725 | tf->buffer.size = ltt_get_uint32(LTT_GET_BO(tf), |
| 1726 | &header->sb_size); |
| 1727 | tf->buffer.data_size = ltt_get_uint32(LTT_GET_BO(tf), |
| 1728 | &header->data_size); |
| 1729 | tf->buffer.tsc = tf->buffer.begin.cycle_count; |
| 1730 | tf->event.tsc = tf->buffer.tsc; |
| 1731 | tf->buffer.freq = tf->buffer.begin.freq; |
| 1732 | |
| 1733 | g_assert(size == tf->buffer.size); |
| 1734 | g_assert(tf->buffer.data_size <= tf->buffer.size); |
| 1735 | |
| 1736 | if (tf->trace->start_freq) |
| 1737 | { |
| 1738 | tf->buffer.begin.freq = tf->trace->start_freq; |
| 1739 | tf->buffer.begin.timestamp = ltt_interpolate_time_from_tsc(tf, |
| 1740 | tf->buffer.begin.cycle_count); |
| 1741 | tf->buffer.end.freq = tf->trace->start_freq; |
| 1742 | tf->buffer.end.timestamp = ltt_interpolate_time_from_tsc(tf, |
| 1743 | tf->buffer.end.cycle_count); |
| 1744 | } |
| 1745 | |
| 1746 | /* Make the current event point to the beginning of the buffer : |
| 1747 | * it means that the event read must get the first event. */ |
| 1748 | tf->event.tracefile = tf; |
| 1749 | tf->event.block = block_num; |
| 1750 | tf->event.offset = 0; |
| 1751 | |
| 1752 | if (header->events_lost) { |
| 1753 | g_warning("%d events lost so far in tracefile %s at block %u", |
| 1754 | (guint)header->events_lost, |
| 1755 | g_quark_to_string(tf->long_name), |
| 1756 | block_num); |
| 1757 | tf->events_lost = header->events_lost; |
| 1758 | } |
| 1759 | if (header->subbuf_corrupt) { |
| 1760 | g_warning("%d subbuffer(s) corrupted so far in tracefile %s at block %u", |
| 1761 | (guint)header->subbuf_corrupt, |
| 1762 | g_quark_to_string(tf->long_name), |
| 1763 | block_num); |
| 1764 | tf->subbuf_corrupt = header->subbuf_corrupt; |
| 1765 | } |
| 1766 | |
| 1767 | return 0; |
| 1768 | |
| 1769 | map_error: |
| 1770 | return -errno; |
| 1771 | } |
| 1772 | |
| 1773 | static void print_debug_event_data(LttEvent *ev) |
| 1774 | { |
| 1775 | unsigned int offset = 0; |
| 1776 | int i, j; |
| 1777 | |
| 1778 | if (!max(ev->event_size, ev->data_size)) |
| 1779 | return; |
| 1780 | |
| 1781 | g_printf("Event data (tracefile %s offset %" PRIx64 "):\n", |
| 1782 | g_quark_to_string(ev->tracefile->long_name), |
| 1783 | (uint64_t)ev->tracefile->buffer.offset |
| 1784 | + (long)ev->data - (long)ev->tracefile->buffer.head); |
| 1785 | |
| 1786 | while (offset < max(ev->event_size, ev->data_size)) { |
| 1787 | g_printf("%8lx", (long)ev->data + offset |
| 1788 | - (long)ev->tracefile->buffer.head); |
| 1789 | g_printf(" "); |
| 1790 | |
| 1791 | for (i = 0; i < 4 ; i++) { |
| 1792 | for (j = 0; j < 4; j++) { |
| 1793 | if (offset + ((i * 4) + j) < max(ev->event_size, ev->data_size)) |
| 1794 | g_printf("%02hhX", ((char*)ev->data)[offset + ((i * 4) + j)]); |
| 1795 | else |
| 1796 | g_printf(" "); |
| 1797 | g_printf(" "); |
| 1798 | } |
| 1799 | if (i < 4) |
| 1800 | g_printf(" "); |
| 1801 | } |
| 1802 | |
| 1803 | g_printf(" "); |
| 1804 | |
| 1805 | for (i = 0; i < 4; i++) { |
| 1806 | for (j = 0; j < 4; j++) { |
| 1807 | if (offset + ((i * 4) + j) < max(ev->event_size, ev->data_size)) { |
| 1808 | if (isprint(((char*)ev->data)[offset + ((i * 4) + j)])) |
| 1809 | g_printf("%c", ((char*)ev->data)[offset + ((i * 4) + j)]); |
| 1810 | else |
| 1811 | g_printf("."); |
| 1812 | } else |
| 1813 | g_printf(" "); |
| 1814 | } |
| 1815 | } |
| 1816 | offset+=16; |
| 1817 | g_printf("\n"); |
| 1818 | } |
| 1819 | } |
| 1820 | |
| 1821 | /* It will update the fields offsets too */ |
| 1822 | void ltt_update_event_size(LttTracefile *tf) |
| 1823 | { |
| 1824 | off_t size = 0; |
| 1825 | struct marker_info *info; |
| 1826 | |
| 1827 | if (tf->name == LTT_TRACEFILE_NAME_METADATA) { |
| 1828 | switch((enum marker_id)tf->event.event_id) { |
| 1829 | case MARKER_ID_SET_MARKER_ID: |
| 1830 | size = strlen((char*)tf->event.data) + 1; |
| 1831 | g_debug("marker %s id set", (char*)tf->event.data + size); |
| 1832 | size += strlen((char*)tf->event.data + size) + 1; |
| 1833 | size += ltt_align(size, sizeof(guint16), tf->alignment); |
| 1834 | size += sizeof(guint16); |
| 1835 | size += sizeof(guint8); |
| 1836 | size += sizeof(guint8); |
| 1837 | size += sizeof(guint8); |
| 1838 | size += sizeof(guint8); |
| 1839 | size += sizeof(guint8); |
| 1840 | break; |
| 1841 | case MARKER_ID_SET_MARKER_FORMAT: |
| 1842 | size = strlen((char*)tf->event.data) + 1; |
| 1843 | g_debug("marker %s format set", (char*)tf->event.data); |
| 1844 | size += strlen((char*)tf->event.data + size) + 1; |
| 1845 | size += strlen((char*)tf->event.data + size) + 1; |
| 1846 | break; |
| 1847 | } |
| 1848 | } |
| 1849 | |
| 1850 | info = marker_get_info_from_id(tf->mdata, tf->event.event_id); |
| 1851 | |
| 1852 | if (tf->event.event_id >= MARKER_CORE_IDS) |
| 1853 | g_assert(info != NULL); |
| 1854 | |
| 1855 | /* Do not update field offsets of core markers when initially reading the |
| 1856 | * metadata tracefile when the infos about these markers do not exist yet. |
| 1857 | */ |
| 1858 | if (likely(info && info->fields)) { |
| 1859 | /* alignment */ |
| 1860 | tf->event.data += ltt_align((off_t)(unsigned long)tf->event.data, |
| 1861 | info->largest_align, |
| 1862 | info->alignment); |
| 1863 | /* size, dynamically computed */ |
| 1864 | if (info->size != -1) |
| 1865 | size = info->size; |
| 1866 | else |
| 1867 | size = marker_update_fields_offsets(info, tf->event.data); |
| 1868 | /* Update per-tracefile offsets */ |
| 1869 | marker_update_event_fields_offsets(tf->event.fields_offsets, info); |
| 1870 | } |
| 1871 | |
| 1872 | tf->event.data_size = size; |
| 1873 | |
| 1874 | /* Check consistency between kernel and LTTV structure sizes */ |
| 1875 | if(tf->event.event_size == G_MAXUINT) { |
| 1876 | /* Event size too big to fit in the event size field */ |
| 1877 | tf->event.event_size = tf->event.data_size; |
| 1878 | } |
| 1879 | |
| 1880 | if (a_event_debug) |
| 1881 | print_debug_event_data(&tf->event); |
| 1882 | |
| 1883 | if (tf->event.data_size != tf->event.event_size) { |
| 1884 | struct marker_info *info = marker_get_info_from_id(tf->mdata, |
| 1885 | tf->event.event_id); |
| 1886 | if (!info) |
| 1887 | g_error("Undescribed event %hhu in channel %s", tf->event.event_id, |
| 1888 | g_quark_to_string(tf->name)); |
| 1889 | g_error("Kernel/LTTV event size differs for event %s: kernel %u, LTTV %u", |
| 1890 | g_quark_to_string(info->name), |
| 1891 | tf->event.event_size, tf->event.data_size); |
| 1892 | exit(-1); |
| 1893 | } |
| 1894 | } |
| 1895 | |
| 1896 | |
| 1897 | /* Take the tf current event offset and use the event id to figure out where is |
| 1898 | * the next event offset. |
| 1899 | * |
| 1900 | * This is an internal function not aiming at being used elsewhere : it will |
| 1901 | * not jump over the current block limits. Please consider using |
| 1902 | * ltt_tracefile_read to do this. |
| 1903 | * |
| 1904 | * Returns 0 on success |
| 1905 | * ERANGE if we are at the end of the buffer. |
| 1906 | * ENOPROTOOPT if an error occured when getting the current event size. |
| 1907 | */ |
| 1908 | static int ltt_seek_next_event(LttTracefile *tf) |
| 1909 | { |
| 1910 | int ret = 0; |
| 1911 | void *pos; |
| 1912 | |
| 1913 | /* seek over the buffer header if we are at the buffer start */ |
| 1914 | if(tf->event.offset == 0) { |
| 1915 | tf->event.offset += tf->buffer_header_size; |
| 1916 | |
| 1917 | if(tf->event.offset == tf->buffer.data_size) { |
| 1918 | ret = ERANGE; |
| 1919 | } |
| 1920 | goto found; |
| 1921 | } |
| 1922 | |
| 1923 | pos = tf->event.data; |
| 1924 | |
| 1925 | if(tf->event.data_size < 0) goto error; |
| 1926 | |
| 1927 | pos += (size_t)tf->event.data_size; |
| 1928 | |
| 1929 | tf->event.offset = pos - tf->buffer.head; |
| 1930 | |
| 1931 | if(tf->event.offset >= tf->buffer.data_size) { |
| 1932 | ret = ERANGE; |
| 1933 | goto found; |
| 1934 | } |
| 1935 | g_assert(tf->event.offset < tf->buffer.data_size); |
| 1936 | |
| 1937 | found: |
| 1938 | return ret; |
| 1939 | |
| 1940 | error: |
| 1941 | g_error("Error in ltt_seek_next_event for tracefile %s", |
| 1942 | g_quark_to_string(tf->name)); |
| 1943 | return ENOPROTOOPT; |
| 1944 | } |
| 1945 | |
| 1946 | |
| 1947 | /***************************************************************************** |
| 1948 | *Function name |
| 1949 | * ltt_get_int : get an integer number |
| 1950 | *Input params |
| 1951 | * reverse_byte_order: must we reverse the byte order ? |
| 1952 | * size : the size of the integer |
| 1953 | * ptr : the data pointer |
| 1954 | *Return value |
| 1955 | * gint64 : a 64 bits integer |
| 1956 | ****************************************************************************/ |
| 1957 | |
| 1958 | gint64 ltt_get_int(gboolean reverse_byte_order, gint size, void *data) |
| 1959 | { |
| 1960 | gint64 val; |
| 1961 | |
| 1962 | switch(size) { |
| 1963 | case 1: val = *((gint8*)data); break; |
| 1964 | case 2: val = ltt_get_int16(reverse_byte_order, data); break; |
| 1965 | case 4: val = ltt_get_int32(reverse_byte_order, data); break; |
| 1966 | case 8: val = ltt_get_int64(reverse_byte_order, data); break; |
| 1967 | default: val = ltt_get_int64(reverse_byte_order, data); |
| 1968 | g_critical("get_int : integer size %d unknown", size); |
| 1969 | break; |
| 1970 | } |
| 1971 | |
| 1972 | return val; |
| 1973 | } |
| 1974 | |
| 1975 | /***************************************************************************** |
| 1976 | *Function name |
| 1977 | * ltt_get_uint : get an unsigned integer number |
| 1978 | *Input params |
| 1979 | * reverse_byte_order: must we reverse the byte order ? |
| 1980 | * size : the size of the integer |
| 1981 | * ptr : the data pointer |
| 1982 | *Return value |
| 1983 | * guint64 : a 64 bits unsigned integer |
| 1984 | ****************************************************************************/ |
| 1985 | |
| 1986 | guint64 ltt_get_uint(gboolean reverse_byte_order, gint size, void *data) |
| 1987 | { |
| 1988 | guint64 val; |
| 1989 | |
| 1990 | switch(size) { |
| 1991 | case 1: val = *((gint8*)data); break; |
| 1992 | case 2: val = ltt_get_uint16(reverse_byte_order, data); break; |
| 1993 | case 4: val = ltt_get_uint32(reverse_byte_order, data); break; |
| 1994 | case 8: val = ltt_get_uint64(reverse_byte_order, data); break; |
| 1995 | default: val = ltt_get_uint64(reverse_byte_order, data); |
| 1996 | g_critical("get_uint : unsigned integer size %d unknown", |
| 1997 | size); |
| 1998 | break; |
| 1999 | } |
| 2000 | |
| 2001 | return val; |
| 2002 | } |
| 2003 | |
| 2004 | |
| 2005 | /* get the node name of the system */ |
| 2006 | |
| 2007 | char * ltt_trace_system_description_node_name (LttSystemDescription * s) |
| 2008 | { |
| 2009 | return s->node_name; |
| 2010 | } |
| 2011 | |
| 2012 | |
| 2013 | /* get the domain name of the system */ |
| 2014 | |
| 2015 | char * ltt_trace_system_description_domain_name (LttSystemDescription * s) |
| 2016 | { |
| 2017 | return s->domain_name; |
| 2018 | } |
| 2019 | |
| 2020 | |
| 2021 | /* get the description of the system */ |
| 2022 | |
| 2023 | char * ltt_trace_system_description_description (LttSystemDescription * s) |
| 2024 | { |
| 2025 | return s->description; |
| 2026 | } |
| 2027 | |
| 2028 | |
| 2029 | /* get the NTP corrected start time of the trace */ |
| 2030 | LttTime ltt_trace_start_time(LttTrace *t) |
| 2031 | { |
| 2032 | return t->start_time; |
| 2033 | } |
| 2034 | |
| 2035 | /* get the monotonic start time of the trace */ |
| 2036 | LttTime ltt_trace_start_time_monotonic(LttTrace *t) |
| 2037 | { |
| 2038 | return t->start_time_from_tsc; |
| 2039 | } |
| 2040 | |
| 2041 | static __attribute__ ((__unused__)) LttTracefile *ltt_tracefile_new() |
| 2042 | { |
| 2043 | LttTracefile *tf; |
| 2044 | tf = g_new(LttTracefile, 1); |
| 2045 | tf->event.tracefile = tf; |
| 2046 | return tf; |
| 2047 | } |
| 2048 | |
| 2049 | static __attribute__ ((__unused__)) void ltt_tracefile_destroy(LttTracefile *tf) |
| 2050 | { |
| 2051 | g_free(tf); |
| 2052 | } |
| 2053 | |
| 2054 | static __attribute__ ((__unused__)) void ltt_tracefile_copy(LttTracefile *dest, const LttTracefile *src) |
| 2055 | { |
| 2056 | *dest = *src; |
| 2057 | } |
| 2058 | |
| 2059 | /* Before library loading... */ |
| 2060 | |
| 2061 | static __attribute__((constructor)) void init(void) |
| 2062 | { |
| 2063 | LTT_TRACEFILE_NAME_METADATA = g_quark_from_string("metadata"); |
| 2064 | } |
| 2065 | |
| 2066 | /***************************************************************************** |
| 2067 | *Function name |
| 2068 | * ltt_tracefile_open_header : based on ltt_tracefile_open but it stops |
| 2069 | * when it gets the header |
| 2070 | *Input params |
| 2071 | * fileName : path to the tracefile |
| 2072 | * tf : the tracefile (metadata_0) where the header will be read |
| 2073 | *Return value |
| 2074 | * ltt_subbuffer_header_t : the header containing the version number |
| 2075 | ****************************************************************************/ |
| 2076 | static ltt_subbuffer_header_t * ltt_tracefile_open_header(gchar *fileName, LttTracefile *tf) |
| 2077 | { |
| 2078 | struct stat lTDFStat; /* Trace data file status */ |
| 2079 | ltt_subbuffer_header_t *header; |
| 2080 | int page_size = getpagesize(); |
| 2081 | |
| 2082 | /* open the file */ |
| 2083 | tf->long_name = g_quark_from_string(fileName); |
| 2084 | tf->fd = open(fileName, O_RDONLY); |
| 2085 | if(tf->fd < 0){ |
| 2086 | g_warning("Unable to open input data file %s\n", fileName); |
| 2087 | goto end; |
| 2088 | } |
| 2089 | |
| 2090 | /* Get the file's status */ |
| 2091 | if(fstat(tf->fd, &lTDFStat) < 0){ |
| 2092 | g_warning("Unable to get the status of the input data file %s\n", fileName); |
| 2093 | goto close_file; |
| 2094 | } |
| 2095 | |
| 2096 | /* Is the file large enough to contain a trace */ |
| 2097 | if(lTDFStat.st_size < (off_t)(ltt_subbuffer_header_size())) { |
| 2098 | g_print("The input data file %s does not contain a trace\n", fileName); |
| 2099 | goto close_file; |
| 2100 | } |
| 2101 | |
| 2102 | /* Temporarily map the buffer start header to get trace information */ |
| 2103 | /* Multiple of pages aligned head */ |
| 2104 | tf->buffer.head = mmap(0,PAGE_ALIGN(ltt_subbuffer_header_size()), PROT_READ, MAP_PRIVATE, tf->fd, 0); |
| 2105 | |
| 2106 | if(tf->buffer.head == MAP_FAILED) { |
| 2107 | perror("Error in allocating memory for buffer of tracefile"); |
| 2108 | goto close_file; |
| 2109 | } |
| 2110 | g_assert( ( (gulong)tf->buffer.head&(8-1) ) == 0); // make sure it's aligned. |
| 2111 | |
| 2112 | header = (ltt_subbuffer_header_t *)tf->buffer.head; |
| 2113 | |
| 2114 | return header; |
| 2115 | |
| 2116 | close_file: |
| 2117 | close(tf->fd); |
| 2118 | end: |
| 2119 | return 0; |
| 2120 | } |
| 2121 | |
| 2122 | |
| 2123 | /***************************************************************************** |
| 2124 | *Function name |
| 2125 | * get_version : get the trace version from a metadata_0 trace file |
| 2126 | *Input params |
| 2127 | * pathname : path to the trace |
| 2128 | * version_number : the struct that will get the version number |
| 2129 | *Return value |
| 2130 | * int : 1 if succeed, -1 if error |
| 2131 | ****************************************************************************/ |
| 2132 | int ltt_get_trace_version(const gchar *pathname, struct LttTraceVersion *version_number) |
| 2133 | { |
| 2134 | gchar abs_path[PATH_MAX]; |
| 2135 | int ret = 0; |
| 2136 | DIR *dir; |
| 2137 | struct dirent *entry; |
| 2138 | struct stat stat_buf; |
| 2139 | gchar path[PATH_MAX]; |
| 2140 | |
| 2141 | LttTracefile tmp_tf; |
| 2142 | LttTrace * t; |
| 2143 | ltt_subbuffer_header_t *header; |
| 2144 | |
| 2145 | t = g_new(LttTrace, 1); |
| 2146 | |
| 2147 | get_absolute_pathname(pathname, abs_path); |
| 2148 | |
| 2149 | /* Test to see if it looks like a trace */ |
| 2150 | dir = opendir(abs_path); |
| 2151 | |
| 2152 | if(dir == NULL) { |
| 2153 | perror(abs_path); |
| 2154 | goto open_error; |
| 2155 | } |
| 2156 | |
| 2157 | while((entry = readdir(dir)) != NULL) { |
| 2158 | strcpy(path, abs_path); |
| 2159 | strcat(path, "/"); |
| 2160 | strcat(path, entry->d_name); |
| 2161 | ret = stat(path, &stat_buf); |
| 2162 | if(ret == -1) { |
| 2163 | perror(path); |
| 2164 | continue; |
| 2165 | } |
| 2166 | } |
| 2167 | |
| 2168 | closedir(dir); |
| 2169 | dir = opendir(abs_path); |
| 2170 | |
| 2171 | while((entry = readdir(dir)) != NULL) { |
| 2172 | if(entry->d_name[0] == '.') continue; |
| 2173 | if(g_str_has_prefix(entry->d_name, "metadata_") != 0) continue; |
| 2174 | |
| 2175 | strcpy(path, abs_path); |
| 2176 | strcat(path, "/"); |
| 2177 | strcat(path, entry->d_name); |
| 2178 | if(ret == -1) { |
| 2179 | perror(path); |
| 2180 | continue; |
| 2181 | } |
| 2182 | |
| 2183 | header = ltt_tracefile_open_header(path, &tmp_tf); |
| 2184 | |
| 2185 | if(header == NULL) { |
| 2186 | g_info("Error getting the header %s", path); |
| 2187 | continue; /* error opening the tracefile : bad magic number ? */ |
| 2188 | } |
| 2189 | |
| 2190 | version_number->ltt_major_version = header->major_version; |
| 2191 | version_number->ltt_minor_version = header->minor_version; |
| 2192 | |
| 2193 | return 1; |
| 2194 | } |
| 2195 | |
| 2196 | return -1; |
| 2197 | |
| 2198 | open_error: |
| 2199 | g_free(t); |
| 2200 | return -1; |
| 2201 | } |