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