update compat
[lttv.git] / ltt / branches / poly / 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>
3aee1200 35#include <malloc.h>
36#include <sys/mman.h>
dd3a6d39 37#include <string.h>
6cd62ccf 38
ef35d837 39// For realpath
40#include <limits.h>
41#include <stdlib.h>
42
43
a5dcde2f 44#include <ltt/ltt.h>
45#include "ltt-private.h"
963b5f2d 46#include <ltt/trace.h>
c02ea99f 47#include <ltt/event.h>
1a2ceb63 48#include <ltt/ltt-types.h>
bb38a290 49#include <ltt/marker.h>
3aee1200 50
51/* Facility names used in this file */
52
53GQuark LTT_FACILITY_NAME_HEARTBEAT,
d1bb700c 54 LTT_EVENT_NAME_HEARTBEAT,
55 LTT_EVENT_NAME_HEARTBEAT_FULL;
3aee1200 56GQuark LTT_TRACEFILE_NAME_FACILITIES;
57
86005ded 58#ifndef g_open
59#define g_open open
60#endif
61
62
51b5991e 63#define __UNUSED__ __attribute__((__unused__))
6cd62ccf 64
a1062ddd 65#define g_info(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, format)
3aee1200 66
67#ifndef g_debug
a1062ddd 68#define g_debug(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format)
3aee1200 69#endif
a1062ddd 70
45e14832 71#define g_close close
8959a0c8 72
b77d1b57 73/* Those macros must be called from within a function where page_size is a known
74 * variable */
75#define PAGE_MASK (~(page_size-1))
76#define PAGE_ALIGN(addr) (((addr)+page_size-1)&PAGE_MASK)
77
9c731a50 78LttTrace *father_trace = NULL;
79
6cd62ccf 80/* set the offset of the fields belonging to the event,
81 need the information of the archecture */
f104d082 82//void set_fields_offsets(LttTracefile *tf, LttEventType *event_type);
eed2ef37 83//size_t get_fields_offsets(LttTracefile *tf, LttEventType *event_type, void *data);
6cd62ccf 84
3aee1200 85/* get the size of the field type according to
86 * The facility size information. */
f104d082 87#if 0
3aee1200 88static inline void preset_field_type_size(LttTracefile *tf,
89 LttEventType *event_type,
90 off_t offset_root, off_t offset_parent,
91 enum field_status *fixed_root, enum field_status *fixed_parent,
92 LttField *field);
f104d082 93#endif //0
6cd62ccf 94
3aee1200 95/* map a fixed size or a block information from the file (fd) */
96static gint map_block(LttTracefile * tf, guint block_num);
97
98/* calculate nsec per cycles for current block */
791dffa6 99#if 0
100static guint32 calc_nsecs_per_cycle(LttTracefile * t);
101static guint64 cycles_2_ns(LttTracefile *tf, guint64 cycles);
102#endif //0
6cd62ccf 103
3aee1200 104/* go to the next event */
105static int ltt_seek_next_event(LttTracefile *tf);
6cd62ccf 106
3c165eaf 107static int open_tracefiles(LttTrace *trace, gchar *root_path,
108 gchar *relative_path);
109static int ltt_process_facility_tracefile(LttTracefile *tf);
110static void ltt_tracefile_time_span_get(LttTracefile *tf,
111 LttTime *start, LttTime *end);
112static void group_time_span_get(GQuark name, gpointer data, gpointer user_data);
113static gint map_block(LttTracefile * tf, guint block_num);
3c165eaf 114static void ltt_update_event_size(LttTracefile *tf);
91f8d488 115
116/* Enable event debugging */
117static int a_event_debug = 0;
118
119void ltt_event_debug(int state)
120{
121 a_event_debug = state;
122}
123
b7576a11 124/* trace can be NULL
125 *
126 * Return value : 0 success, 1 bad tracefile
127 */
8655430b 128static int parse_trace_header(void *header, LttTracefile *tf, LttTrace *t)
b7576a11 129{
130 guint32 *magic_number = (guint32*)header;
131 struct ltt_trace_header_any *any = (struct ltt_trace_header_any *)header;
132
133 if(*magic_number == LTT_MAGIC_NUMBER)
134 tf->reverse_bo = 0;
135 else if(*magic_number == LTT_REV_MAGIC_NUMBER)
136 tf->reverse_bo = 1;
137 else /* invalid magic number, bad tracefile ! */
138 return 1;
139
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)) */
142 tf->float_word_order = any->float_word_order;
256a5b3a 143 tf->alignment = any->alignment;
90440c06 144 tf->has_heartbeat = any->has_heartbeat;
b7576a11 145
146 if(t) {
147 t->arch_type = ltt_get_uint32(LTT_GET_BO(tf),
148 &any->arch_type);
149 t->arch_variant = ltt_get_uint32(LTT_GET_BO(tf),
150 &any->arch_variant);
151 t->arch_size = any->arch_size;
152 t->ltt_major_version = any->major_version;
153 t->ltt_minor_version = any->minor_version;
154 t->flight_recorder = any->flight_recorder;
3c165eaf 155 // t->compact_facilities = NULL;
b7576a11 156 }
157
b7576a11 158 switch(any->major_version) {
159
160 case 0:
3c165eaf 161 g_warning("Unsupported trace version : %hhu.%hhu",
162 any->major_version, any->minor_version);
163 return 1;
164 break;
165 case 1:
b7576a11 166 switch(any->minor_version) {
3c165eaf 167 case 0:
b7576a11 168 {
3c165eaf 169 struct ltt_trace_header_1_0 *vheader =
170 (struct ltt_trace_header_1_0 *)header;
823820eb 171 tf->buffer_header_size =
172 sizeof(struct ltt_block_start_header)
3c165eaf 173 + sizeof(struct ltt_trace_header_1_0);
62e4e7bf 174 tf->tsc_lsb_truncate = vheader->tsc_lsb_truncate;
175 tf->tscbits = vheader->tscbits;
176 tf->tsc_msb_cutoff = 32 - tf->tsc_lsb_truncate - tf->tscbits;
08a986f1 177 tf->compact_event_bits = 32 - vheader->compact_data_shift;
dd3a6d39 178 tf->tsc_mask = ((1ULL << (tf->tscbits))-1);
179 tf->tsc_mask = tf->tsc_mask << tf->tsc_lsb_truncate;
62e4e7bf 180 tf->tsc_mask_next_bit = (1ULL<<(tf->tscbits));
181 tf->tsc_mask_next_bit = tf->tsc_mask_next_bit << tf->tsc_lsb_truncate;
e939e5b2 182 if(t) {
183 t->start_freq = ltt_get_uint64(LTT_GET_BO(tf),
184 &vheader->start_freq);
a3999b49 185 t->freq_scale = ltt_get_uint32(LTT_GET_BO(tf),
186 &vheader->freq_scale);
9c731a50 187 if(father_trace) {
188 t->start_freq = father_trace->start_freq;
189 t->freq_scale = father_trace->freq_scale;
190 }
191 else {
192 father_trace = t;
193 }
e939e5b2 194 t->start_tsc = ltt_get_uint64(LTT_GET_BO(tf),
195 &vheader->start_tsc);
196 t->start_monotonic = ltt_get_uint64(LTT_GET_BO(tf),
197 &vheader->start_monotonic);
198 t->start_time.tv_sec = ltt_get_uint64(LTT_GET_BO(tf),
199 &vheader->start_time_sec);
200 t->start_time.tv_nsec = ltt_get_uint64(LTT_GET_BO(tf),
201 &vheader->start_time_usec);
202 t->start_time.tv_nsec *= 1000; /* microsec to nanosec */
203
204 t->start_time_from_tsc = ltt_time_from_uint64(
c7f86478 205 (double)t->start_tsc
206 * (1000000000.0 / tf->trace->freq_scale)
62e4e7bf 207 / (double)t->start_freq);
e939e5b2 208 }
209 }
210 break;
b7576a11 211 default:
986e2a7c 212 g_warning("Unsupported trace version : %hhu.%hhu",
3c165eaf 213 any->major_version, any->minor_version);
b7576a11 214 return 1;
215 }
216 break;
b7576a11 217 default:
218 g_warning("Unsupported trace version : %hhu.%hhu",
219 any->major_version, any->minor_version);
220 return 1;
221 }
222
223
224 return 0;
225}
226
227
228
6cd62ccf 229/*****************************************************************************
230 *Function name
963b5f2d 231 * ltt_tracefile_open : open a trace file, construct a LttTracefile
6cd62ccf 232 *Input params
963b5f2d 233 * t : the trace containing the tracefile
234 * fileName : path name of the trace file
3aee1200 235 * tf : the tracefile structure
6cd62ccf 236 *Return value
3aee1200 237 * : 0 for success, -1 otherwise.
6cd62ccf 238 ****************************************************************************/
239
8655430b 240static gint ltt_tracefile_open(LttTrace *t, gchar * fileName, LttTracefile *tf)
6cd62ccf 241{
963b5f2d 242 struct stat lTDFStat; /* Trace data file status */
3aee1200 243 struct ltt_block_start_header *header;
b77d1b57 244 int page_size = getpagesize();
6cd62ccf 245
246 //open the file
d3d34f49 247 tf->long_name = g_quark_from_string(fileName);
963b5f2d 248 tf->trace = t;
3865ea09 249 tf->fd = open(fileName, O_RDONLY);
6cd62ccf 250 if(tf->fd < 0){
2a74fbf4 251 g_warning("Unable to open input data file %s\n", fileName);
3aee1200 252 goto end;
6cd62ccf 253 }
254
255 // Get the file's status
256 if(fstat(tf->fd, &lTDFStat) < 0){
2a74fbf4 257 g_warning("Unable to get the status of the input data file %s\n", fileName);
3aee1200 258 goto close_file;
6cd62ccf 259 }
260
261 // Is the file large enough to contain a trace
823820eb 262 if(lTDFStat.st_size <
9ea7b94b 263 (off_t)(sizeof(struct ltt_block_start_header)
264 + sizeof(struct ltt_trace_header_any))){
8710c6c7 265 g_print("The input data file %s does not contain a trace\n", fileName);
3aee1200 266 goto close_file;
267 }
268
269 /* Temporarily map the buffer start header to get trace information */
270 /* Multiple of pages aligned head */
b77d1b57 271 tf->buffer.head = mmap(0,
9ea7b94b 272 PAGE_ALIGN(sizeof(struct ltt_block_start_header)
273 + sizeof(struct ltt_trace_header_any)), PROT_READ,
3aee1200 274 MAP_PRIVATE, tf->fd, 0);
3865ea09 275 if(tf->buffer.head == MAP_FAILED) {
3aee1200 276 perror("Error in allocating memory for buffer of tracefile");
277 goto close_file;
6cd62ccf 278 }
f64fedd7 279 g_assert( ( (gulong)tf->buffer.head&(8-1) ) == 0); // make sure it's aligned.
3aee1200 280
281 header = (struct ltt_block_start_header*)tf->buffer.head;
6cd62ccf 282
51551c6f 283 if(parse_trace_header(header->trace, tf, NULL)) {
284 g_warning("parse_trace_header error");
285 goto unmap_file;
286 }
3aee1200 287
6cd62ccf 288 //store the size of the file
289 tf->file_size = lTDFStat.st_size;
f628823c 290 tf->buf_size = ltt_get_uint32(LTT_GET_BO(tf), &header->buf_size);
291 tf->num_blocks = tf->file_size / tf->buf_size;
292
293 if(munmap(tf->buffer.head,
9ea7b94b 294 PAGE_ALIGN(sizeof(struct ltt_block_start_header)
295 + sizeof(struct ltt_trace_header_any)))) {
f628823c 296 g_warning("unmap size : %u\n",
9ea7b94b 297 PAGE_ALIGN(sizeof(struct ltt_block_start_header)
298 + sizeof(struct ltt_trace_header_any)));
f628823c 299 perror("munmap error");
300 g_assert(0);
301 }
3aee1200 302 tf->buffer.head = NULL;
6cd62ccf 303
963b5f2d 304 //read the first block
3aee1200 305 if(map_block(tf,0)) {
306 perror("Cannot map block for tracefile");
307 goto close_file;
308 }
309
310 return 0;
6cd62ccf 311
3aee1200 312 /* Error */
313unmap_file:
f628823c 314 if(munmap(tf->buffer.head,
9ea7b94b 315 PAGE_ALIGN(sizeof(struct ltt_block_start_header)
316 + sizeof(struct ltt_trace_header_any)))) {
f628823c 317 g_warning("unmap size : %u\n",
9ea7b94b 318 PAGE_ALIGN(sizeof(struct ltt_block_start_header)
319 + sizeof(struct ltt_trace_header_any)));
f628823c 320 perror("munmap error");
321 g_assert(0);
322 }
3aee1200 323close_file:
3865ea09 324 close(tf->fd);
3aee1200 325end:
326 return -1;
6cd62ccf 327}
328
3aee1200 329#if 0
6cd62ccf 330/*****************************************************************************
963b5f2d 331 *Open control and per cpu tracefiles
6cd62ccf 332 ****************************************************************************/
333
45e14832 334void ltt_tracefile_open_cpu(LttTrace *t, gchar * tracefile_name)
6cd62ccf 335{
963b5f2d 336 LttTracefile * tf;
337 tf = ltt_tracefile_open(t,tracefile_name);
4a6c8e36 338 if(!tf) return;
963b5f2d 339 t->per_cpu_tracefile_number++;
340 g_ptr_array_add(t->per_cpu_tracefiles, tf);
6cd62ccf 341}
342
45e14832 343gint ltt_tracefile_open_control(LttTrace *t, gchar * control_name)
6cd62ccf 344{
963b5f2d 345 LttTracefile * tf;
c02ea99f 346 LttEvent ev;
963b5f2d 347 LttFacility * f;
963b5f2d 348 void * pos;
349 FacilityLoad fLoad;
8d1e6362 350 unsigned int i;
963b5f2d 351
352 tf = ltt_tracefile_open(t,control_name);
2a74fbf4 353 if(!tf) {
354 g_warning("ltt_tracefile_open_control : bad file descriptor");
355 return -1;
356 }
963b5f2d 357 t->control_tracefile_number++;
358 g_ptr_array_add(t->control_tracefiles,tf);
359
360 //parse facilities tracefile to get base_id
542ceddd 361 if(strcmp(&control_name[strlen(control_name)-10],"facilities") ==0){
963b5f2d 362 while(1){
c02ea99f 363 if(!ltt_tracefile_read(tf,&ev)) return 0; // end of file
40331ba8 364
c02ea99f 365 if(ev.event_id == TRACE_FACILITY_LOAD){
366 pos = ev.data;
45e14832 367 fLoad.name = (gchar*)pos;
47a166fc 368 fLoad.checksum = *(LttChecksum*)(pos + strlen(fLoad.name));
cbd41522 369 fLoad.base_code = *(guint32 *)(pos + strlen(fLoad.name) + sizeof(LttChecksum));
963b5f2d 370
371 for(i=0;i<t->facility_number;i++){
372 f = (LttFacility*)g_ptr_array_index(t->facilities,i);
373 if(strcmp(f->name,fLoad.name)==0 && fLoad.checksum==f->checksum){
374 f->base_id = fLoad.base_code;
375 break;
376 }
377 }
2a74fbf4 378 if(i==t->facility_number) {
8d1e6362 379 g_warning("Facility: %s, checksum: %u is not found",
380 fLoad.name,(unsigned int)fLoad.checksum);
2a74fbf4 381 return -1;
382 }
c02ea99f 383 }else if(ev.event_id == TRACE_BLOCK_START){
40331ba8 384 continue;
c02ea99f 385 }else if(ev.event_id == TRACE_BLOCK_END){
40331ba8 386 break;
2a74fbf4 387 }else {
8d1e6362 388 g_warning("Not valid facilities trace file");
2a74fbf4 389 return -1;
390 }
963b5f2d 391 }
392 }
2a74fbf4 393 return 0;
6cd62ccf 394}
3aee1200 395#endif //0
6cd62ccf 396
397/*****************************************************************************
398 *Function name
963b5f2d 399 * ltt_tracefile_close: close a trace file,
6cd62ccf 400 *Input params
963b5f2d 401 * t : tracefile which will be closed
6cd62ccf 402 ****************************************************************************/
403
8655430b 404static void ltt_tracefile_close(LttTracefile *t)
6cd62ccf 405{
f628823c 406 int page_size = getpagesize();
407
3aee1200 408 if(t->buffer.head != NULL)
f628823c 409 if(munmap(t->buffer.head, PAGE_ALIGN(t->buf_size))) {
410 g_warning("unmap size : %u\n",
411 PAGE_ALIGN(t->buf_size));
412 perror("munmap error");
413 g_assert(0);
414 }
415
3865ea09 416 close(t->fd);
963b5f2d 417}
6cd62ccf 418
6cd62ccf 419
963b5f2d 420/*****************************************************************************
421 *Get system information
422 ****************************************************************************/
3aee1200 423#if 0
45e14832 424gint getSystemInfo(LttSystemDescription* des, gchar * pathname)
963b5f2d 425{
45e14832 426 int fd;
427 GIOChannel *iochan;
428 gchar *buf = NULL;
429 gsize length;
43da6a59 430
431 GMarkupParseContext * context;
2a74fbf4 432 GError * error = NULL;
43da6a59 433 GMarkupParser markup_parser =
434 {
435 parser_start_element,
8d1e6362 436 NULL,
43da6a59 437 parser_characters,
438 NULL, /* passthrough */
439 NULL /* error */
440 };
963b5f2d 441
45e14832 442 fd = g_open(pathname, O_RDONLY, 0);
443 if(fd == -1){
2a74fbf4 444 g_warning("Can not open file : %s\n", pathname);
445 return -1;
6cd62ccf 446 }
963b5f2d 447
45e14832 448 iochan = g_io_channel_unix_new(fd);
449
43da6a59 450 context = g_markup_parse_context_new(&markup_parser, 0, des,NULL);
6cd62ccf 451
45e14832 452 //while(fgets(buf,DIR_NAME_SIZE, fp) != NULL){
453 while(g_io_channel_read_line(iochan, &buf, &length, NULL, &error)
454 != G_IO_STATUS_EOF) {
455
456 if(error != NULL) {
457 g_warning("Can not read xml file: \n%s\n", error->message);
458 g_error_free(error);
459 }
460 if(!g_markup_parse_context_parse(context, buf, length, &error)){
2a74fbf4 461 if(error != NULL) {
462 g_warning("Can not parse xml file: \n%s\n", error->message);
463 g_error_free(error);
464 }
465 g_markup_parse_context_free(context);
45e14832 466
467 g_io_channel_shutdown(iochan, FALSE, &error); /* No flush */
468 if(error != NULL) {
469 g_warning("Can not close file: \n%s\n", error->message);
470 g_error_free(error);
471 }
472
473 close(fd);
2a74fbf4 474 return -1;
43da6a59 475 }
963b5f2d 476 }
2a74fbf4 477 g_markup_parse_context_free(context);
45e14832 478
479 g_io_channel_shutdown(iochan, FALSE, &error); /* No flush */
480 if(error != NULL) {
481 g_warning("Can not close file: \n%s\n", error->message);
482 g_error_free(error);
483 }
484
485 g_close(fd);
486
487 g_free(buf);
2a74fbf4 488 return 0;
6cd62ccf 489}
3aee1200 490#endif //0
6cd62ccf 491
492/*****************************************************************************
963b5f2d 493 *The following functions get facility/tracefile information
6cd62ccf 494 ****************************************************************************/
3aee1200 495#if 0
45e14832 496gint getFacilityInfo(LttTrace *t, gchar* eventdefs)
6cd62ccf 497{
45e14832 498 GDir * dir;
499 const gchar * name;
8d1e6362 500 unsigned int i,j;
963b5f2d 501 LttFacility * f;
502 LttEventType * et;
45e14832 503 gchar fullname[DIR_NAME_SIZE];
504 GError * error = NULL;
505
506 dir = g_dir_open(eventdefs, 0, &error);
963b5f2d 507
45e14832 508 if(error != NULL) {
509 g_warning("Can not open directory: %s, %s\n", eventdefs, error->message);
510 g_error_free(error);
2a74fbf4 511 return -1;
512 }
963b5f2d 513
45e14832 514 while((name = g_dir_read_name(dir)) != NULL){
515 if(!g_pattern_match_simple("*.xml", name)) continue;
516 strcpy(fullname,eventdefs);
517 strcat(fullname,name);
518 ltt_facility_open(t,fullname);
519 }
520 g_dir_close(dir);
963b5f2d 521
963b5f2d 522 for(j=0;j<t->facility_number;j++){
8710c6c7 523 f = (LttFacility*)g_ptr_array_index(t->facilities, j);
963b5f2d 524 for(i=0; i<f->event_number; i++){
525 et = f->events[i];
40331ba8 526 setFieldsOffset(NULL, et, NULL, t);
963b5f2d 527 }
528 }
2a74fbf4 529 return 0;
963b5f2d 530}
3aee1200 531#endif //0
6cd62ccf 532
963b5f2d 533/*****************************************************************************
534 *A trace is specified as a pathname to the directory containing all the
535 *associated data (control tracefiles, per cpu tracefiles, event
536 *descriptions...).
537 *
538 *When a trace is closed, all the associated facilities, types and fields
539 *are released as well.
8d1e6362 540 */
541
542
543/****************************************************************************
544 * get_absolute_pathname
803229fa 545 *
8d1e6362 546 * return the unique pathname in the system
547 *
ef35d837 548 * MD : Fixed this function so it uses realpath, dealing well with
549 * forgotten cases (.. were not used correctly before).
803229fa 550 *
963b5f2d 551 ****************************************************************************/
45e14832 552void get_absolute_pathname(const gchar *pathname, gchar * abs_pathname)
9f797243 553{
9f797243 554 abs_pathname[0] = '\0';
803229fa 555
ef35d837 556 if ( realpath (pathname, abs_pathname) != NULL)
557 return;
558 else
559 {
8d1e6362 560 /* error, return the original path unmodified */
ef35d837 561 strcpy(abs_pathname, pathname);
9f797243 562 return;
563 }
ef35d837 564 return;
9f797243 565}
566
3aee1200 567/* Search for something like : .*_.*
568 *
569 * The left side is the name, the right side is the number.
570 */
571
8655430b 572static int get_tracefile_name_number(gchar *raw_name,
3aee1200 573 GQuark *name,
ae3d0f50 574 guint *num,
f64fedd7 575 gulong *tid,
576 gulong *pgid,
62e4e7bf 577 guint64 *creation)
6cd62ccf 578{
3aee1200 579 guint raw_name_len = strlen(raw_name);
580 gchar char_name[PATH_MAX];
3aee1200 581 int i;
582 int underscore_pos;
583 long int cpu_num;
584 gchar *endptr;
62e4e7bf 585 gchar *tmpptr;
3aee1200 586
587 for(i=raw_name_len-1;i>=0;i--) {
588 if(raw_name[i] == '_') break;
589 }
ae3d0f50 590 if(i==-1) { /* Either not found or name length is 0 */
62e4e7bf 591 /* This is a userspace tracefile */
592 strncpy(char_name, raw_name, raw_name_len);
593 char_name[raw_name_len] = '\0';
594 *name = g_quark_from_string(char_name);
595 *num = 0; /* unknown cpu */
596 for(i=0;i<raw_name_len;i++) {
597 if(raw_name[i] == '/') {
598 break;
599 }
600 }
601 i++;
602 for(;i<raw_name_len;i++) {
603 if(raw_name[i] == '/') {
604 break;
605 }
606 }
607 i++;
608 for(;i<raw_name_len;i++) {
609 if(raw_name[i] == '-') {
610 break;
611 }
612 }
613 if(i == raw_name_len) return -1;
614 i++;
615 tmpptr = &raw_name[i];
616 for(;i<raw_name_len;i++) {
617 if(raw_name[i] == '.') {
618 raw_name[i] = ' ';
619 break;
620 }
621 }
622 *tid = strtoul(tmpptr, &endptr, 10);
623 if(endptr == tmpptr)
624 return -1; /* No digit */
625 if(*tid == ULONG_MAX)
626 return -1; /* underflow / overflow */
627 i++;
628 tmpptr = &raw_name[i];
629 for(;i<raw_name_len;i++) {
630 if(raw_name[i] == '.') {
631 raw_name[i] = ' ';
632 break;
633 }
634 }
635 *pgid = strtoul(tmpptr, &endptr, 10);
636 if(endptr == tmpptr)
637 return -1; /* No digit */
638 if(*pgid == ULONG_MAX)
639 return -1; /* underflow / overflow */
640 i++;
641 tmpptr = &raw_name[i];
642 *creation = strtoull(tmpptr, &endptr, 10);
643 if(endptr == tmpptr)
644 return -1; /* No digit */
645 if(*creation == G_MAXUINT64)
646 return -1; /* underflow / overflow */
647 } else {
648 underscore_pos = i;
649
650 cpu_num = strtol(raw_name+underscore_pos+1, &endptr, 10);
651
652 if(endptr == raw_name+underscore_pos+1)
653 return -1; /* No digit */
654 if(cpu_num == LONG_MIN || cpu_num == LONG_MAX)
655 return -1; /* underflow / overflow */
656
657 strncpy(char_name, raw_name, underscore_pos);
658 char_name[underscore_pos] = '\0';
659
660 *name = g_quark_from_string(char_name);
661 *num = cpu_num;
662 }
69bd59ed 663
b333a76b 664
3aee1200 665 return 0;
666}
963b5f2d 667
3aee1200 668
3865ea09 669GData **ltt_trace_get_tracefiles_groups(LttTrace *trace)
77175651 670{
3865ea09 671 return &trace->tracefiles;
77175651 672}
673
674
3865ea09 675void compute_tracefile_group(GQuark key_id,
676 GArray *group,
677 struct compute_tracefile_group_args *args)
77175651 678{
679 int i;
680 LttTracefile *tf;
681
682 for(i=0; i<group->len; i++) {
683 tf = &g_array_index (group, LttTracefile, i);
684 if(tf->cpu_online)
3865ea09 685 args->func(tf, args->func_args);
77175651 686 }
687}
688
689
8655430b 690static void ltt_tracefile_group_destroy(gpointer data)
3aee1200 691{
692 GArray *group = (GArray *)data;
693 int i;
694 LttTracefile *tf;
695
696 for(i=0; i<group->len; i++) {
697 tf = &g_array_index (group, LttTracefile, i);
698 if(tf->cpu_online)
699 ltt_tracefile_close(tf);
700 }
701 g_array_free(group, TRUE);
6cd62ccf 702}
703
8655430b 704static gboolean ltt_tracefile_group_has_cpu_online(gpointer data)
49bf71b5 705{
3aee1200 706 GArray *group = (GArray *)data;
707 int i;
708 LttTracefile *tf;
709
710 for(i=0; i<group->len; i++) {
711 tf = &g_array_index (group, LttTracefile, i);
3c165eaf 712 if(tf->cpu_online)
713 return 1;
3aee1200 714 }
715 return 0;
49bf71b5 716}
717
718
3aee1200 719/* Open each tracefile under a specific directory. Put them in a
720 * GData : permits to access them using their tracefile group pathname.
721 * i.e. access control/modules tracefile group by index :
722 * "control/module".
3865ea09 723 *
724 * relative path is the path relative to the trace root
725 * root path is the full path
3aee1200 726 *
4a55f63e 727 * A tracefile group is simply an array where all the per cpu tracefiles sit.
3aee1200 728 */
729
8655430b 730static int open_tracefiles(LttTrace *trace, gchar *root_path, gchar *relative_path)
f7afe191 731{
62e4e7bf 732 DIR *dir = opendir(root_path);
733 struct dirent *entry;
734 struct stat stat_buf;
735 int ret;
3865ea09 736
62e4e7bf 737 gchar path[PATH_MAX];
738 int path_len;
739 gchar *path_ptr;
3aee1200 740
3865ea09 741 int rel_path_len;
69bd59ed 742 gchar rel_path[PATH_MAX];
743 gchar *rel_path_ptr;
cb03932a 744 LttTracefile tmp_tf;
3865ea09 745
62e4e7bf 746 if(dir == NULL) {
747 perror(root_path);
748 return ENOENT;
749 }
3aee1200 750
62e4e7bf 751 strncpy(path, root_path, PATH_MAX-1);
752 path_len = strlen(path);
753 path[path_len] = '/';
754 path_len++;
755 path_ptr = path + path_len;
3aee1200 756
3865ea09 757 strncpy(rel_path, relative_path, PATH_MAX-1);
758 rel_path_len = strlen(rel_path);
759 rel_path[rel_path_len] = '/';
760 rel_path_len++;
761 rel_path_ptr = rel_path + rel_path_len;
762
62e4e7bf 763 while((entry = readdir(dir)) != NULL) {
764
765 if(entry->d_name[0] == '.') continue;
766
767 strncpy(path_ptr, entry->d_name, PATH_MAX - path_len);
768 strncpy(rel_path_ptr, entry->d_name, PATH_MAX - rel_path_len);
769
770 ret = stat(path, &stat_buf);
771 if(ret == -1) {
772 perror(path);
773 continue;
774 }
775
776 g_debug("Tracefile file or directory : %s\n", path);
777
fe452434 778 // if(strcmp(rel_path, "/eventdefs") == 0) continue;
74a588bb 779
62e4e7bf 780 if(S_ISDIR(stat_buf.st_mode)) {
3aee1200 781
62e4e7bf 782 g_debug("Entering subdirectory...\n");
783 ret = open_tracefiles(trace, path, rel_path);
784 if(ret < 0) continue;
785 } else if(S_ISREG(stat_buf.st_mode)) {
786 GQuark name;
f64fedd7 787 guint num;
788 gulong tid, pgid;
62e4e7bf 789 guint64 creation;
3aee1200 790 GArray *group;
f64fedd7 791 num = 0;
792 tid = pgid = 0;
62e4e7bf 793 creation = 0;
ae3d0f50 794 if(get_tracefile_name_number(rel_path, &name, &num, &tid, &pgid, &creation))
3aee1200 795 continue; /* invalid name */
3865ea09 796
62e4e7bf 797 g_debug("Opening file.\n");
cb03932a 798 if(ltt_tracefile_open(trace, path, &tmp_tf)) {
799 g_info("Error opening tracefile %s", path);
800
801 continue; /* error opening the tracefile : bad magic number ? */
802 }
803
3865ea09 804 g_debug("Tracefile name is %s and number is %u",
805 g_quark_to_string(name), num);
806
cb03932a 807 tmp_tf.cpu_online = 1;
808 tmp_tf.cpu_num = num;
d3d34f49 809 tmp_tf.name = name;
62e4e7bf 810 tmp_tf.tid = tid;
811 tmp_tf.pgid = pgid;
812 tmp_tf.creation = creation;
d1bb700c 813 if(tmp_tf.name == g_quark_from_string("/compact")
62e4e7bf 814 || tmp_tf.name == g_quark_from_string("/flight-compact"))
815 tmp_tf.compact = 1;
d1bb700c 816 else
62e4e7bf 817 tmp_tf.compact = 0;
3865ea09 818 group = g_datalist_id_get_data(&trace->tracefiles, name);
3aee1200 819 if(group == NULL) {
820 /* Elements are automatically cleared when the array is allocated.
821 * It makes the cpu_online variable set to 0 : cpu offline, by default.
822 */
823 group = g_array_sized_new (FALSE, TRUE, sizeof(LttTracefile), 10);
3865ea09 824 g_datalist_id_set_data_full(&trace->tracefiles, name,
3aee1200 825 group, ltt_tracefile_group_destroy);
826 }
cb03932a 827
3aee1200 828 /* Add the per cpu tracefile to the named group */
829 unsigned int old_len = group->len;
830 if(num+1 > old_len)
831 group = g_array_set_size(group, num+1);
cb03932a 832 g_array_index (group, LttTracefile, num) = tmp_tf;
afd57a3c 833 g_array_index (group, LttTracefile, num).event.tracefile =
834 &g_array_index (group, LttTracefile, num);
62e4e7bf 835 }
836 }
837
838 closedir(dir);
3aee1200 839
62e4e7bf 840 return 0;
f7afe191 841}
842
3aee1200 843
844/* Presumes the tracefile is already seeked at the beginning. It makes sense,
845 * because it must be done just after the opening */
8655430b 846static int ltt_process_facility_tracefile(LttTracefile *tf)
3aee1200 847{
848 int err;
29af7cfd 849 //LttFacility *fac;
850 //GArray *fac_ids;
cb03932a 851 guint i;
29af7cfd 852 //LttEventType *et;
3aee1200 853
854 while(1) {
855 err = ltt_tracefile_read_seek(tf);
856 if(err == EPERM) goto seek_error;
857 else if(err == ERANGE) break; /* End of tracefile */
858
859 err = ltt_tracefile_read_update_event(tf);
860 if(err) goto update_error;
861
862 /* We are on a facility load/or facility unload/ or heartbeat event */
863 /* The rules are :
864 * * facility 0 is hardcoded : this is the core facility. It will be shown
865 * in the facility array though, and is shown as "loaded builtin" in the
866 * trace.
867 * It contains event :
868 * 0 : facility load
869 * 1 : facility unload
870 * 2 : state dump facility load
d2083cab 871 * 3 : heartbeat
3aee1200 872 */
3c165eaf 873 if(tf->event.event_id >= MARKER_CORE_IDS) {
d2083cab 874 /* Should only contain core facility */
3aee1200 875 g_warning("Error in processing facility file %s, "
3c165eaf 876 "should not contain event id %u.", g_quark_to_string(tf->name),
877 tf->event.event_id);
3aee1200 878 err = EPERM;
3c165eaf 879 goto event_id_error;
d2083cab 880 } else {
3aee1200 881
2e13d6af 882 char *pos;
3c165eaf 883 const char *marker_name, *format;
884 uint16_t id;
885 guint8 int_size, long_size, pointer_size, size_t_size, alignment;
3aee1200 886
887 // FIXME align
3c165eaf 888 switch((enum marker_id)tf->event.event_id) {
889 case MARKER_ID_SET_MARKER_ID:
2e13d6af 890 marker_name = pos = tf->event.data;
3c165eaf 891 g_debug("Doing MARKER_ID_SET_MARKER_ID of marker %s", marker_name);
2e13d6af 892 pos += strlen(marker_name) + 1;
893 //remove genevent compatibility
256a5b3a 894 //pos += ltt_align((size_t)pos, tf->trace->arch_size, tf->alignment);
895 pos += ltt_align((size_t)pos, sizeof(uint16_t), tf->alignment);
3c165eaf 896 id = ltt_get_uint16(LTT_GET_BO(tf), pos);
c7146c5c 897 g_debug("In MARKER_ID_SET_MARKER_ID of marker %s id %hu",
898 marker_name, id);
3c165eaf 899 pos += sizeof(guint16);
900 int_size = *(guint8*)pos;
901 pos += sizeof(guint8);
902 long_size = *(guint8*)pos;
903 pos += sizeof(guint8);
904 pointer_size = *(guint8*)pos;
905 pos += sizeof(guint8);
906 size_t_size = *(guint8*)pos;
907 pos += sizeof(guint8);
908 alignment = *(guint8*)pos;
909 pos += sizeof(guint8);
910 marker_id_event(tf->trace, g_quark_from_string(marker_name),
911 id, int_size, long_size,
912 pointer_size, size_t_size, alignment);
3aee1200 913 break;
3c165eaf 914 case MARKER_ID_SET_MARKER_FORMAT:
2e13d6af 915 marker_name = pos = tf->event.data;
3c165eaf 916 g_debug("Doing MARKER_ID_SET_MARKER_FORMAT of marker %s",
917 marker_name);
2e13d6af 918 pos += strlen(marker_name) + 1;
919 //break genevent.
256a5b3a 920 //pos += ltt_align((size_t)pos, tf->trace->arch_size, tf->alignment);
2e13d6af 921 format = pos;
3c165eaf 922 pos += strlen(format) + 1;
2e13d6af 923 //break genevent
256a5b3a 924 //pos += ltt_align((size_t)pos, tf->trace->arch_size, tf->alignment);
3c165eaf 925 marker_format_event(tf->trace, g_quark_from_string(marker_name),
926 format);
927 /* get information from dictionnary TODO */
3aee1200 928 break;
3c165eaf 929 case MARKER_ID_HEARTBEAT_32:
930 case MARKER_ID_HEARTBEAT_64:
d1bb700c 931 break;
3aee1200 932 default:
933 g_warning("Error in processing facility file %s, "
3c165eaf 934 "unknown event id %hhu.",
3aee1200 935 g_quark_to_string(tf->name),
936 tf->event.event_id);
937 err = EPERM;
938 goto event_id_error;
939 }
940 }
963b5f2d 941 }
3aee1200 942 return 0;
963b5f2d 943
3aee1200 944 /* Error handling */
3aee1200 945event_id_error:
3aee1200 946update_error:
947seek_error:
d2083cab 948 g_warning("An error occured in facility tracefile parsing");
3aee1200 949 return err;
6cd62ccf 950}
951
e95fe8f7 952/*
953 * Open a trace and return its LttTrace handle.
954 *
955 * pathname must be the directory of the trace
956 */
963b5f2d 957
3aee1200 958LttTrace *ltt_trace_open(const gchar *pathname)
959{
960 gchar abs_path[PATH_MAX];
961 LttTrace * t;
962 LttTracefile *tf;
963 GArray *group;
b56dcdf2 964 int i, ret;
16fcbb80 965 struct ltt_block_start_header *header;
62e4e7bf 966 DIR *dir;
967 struct dirent *entry;
b56dcdf2 968 guint control_found = 0;
62e4e7bf 969 struct stat stat_buf;
b56dcdf2 970 gchar path[PATH_MAX];
3aee1200 971
972 t = g_new(LttTrace, 1);
973 if(!t) goto alloc_error;
974
975 get_absolute_pathname(pathname, abs_path);
976 t->pathname = g_quark_from_string(abs_path);
977
3aee1200 978 g_datalist_init(&t->tracefiles);
b56dcdf2 979
980 /* Test to see if it looks like a trace */
62e4e7bf 981 dir = opendir(abs_path);
982 if(dir == NULL) {
983 perror(abs_path);
984 goto open_error;
985 }
986 while((entry = readdir(dir)) != NULL) {
b56dcdf2 987 strcpy(path, abs_path);
988 strcat(path, "/");
989 strcat(path, entry->d_name);
62e4e7bf 990 ret = stat(path, &stat_buf);
991 if(ret == -1) {
992 perror(path);
993 continue;
994 }
995 if(S_ISDIR(stat_buf.st_mode)) {
b56dcdf2 996 if(strcmp(entry->d_name, "control") == 0) {
997 control_found = 1;
998 }
b56dcdf2 999 }
1000 }
1001 closedir(dir);
1002
fe452434 1003 if(!control_found) goto find_error;
b56dcdf2 1004
1005 /* Open all the tracefiles */
e45551ac 1006 if(open_tracefiles(t, abs_path, "")) {
1007 g_warning("Error opening tracefile %s", abs_path);
b56dcdf2 1008 goto find_error;
e45551ac 1009 }
3aee1200 1010
3aee1200 1011 /* Parse each trace control/facilitiesN files : get runtime fac. info */
1012 group = g_datalist_id_get_data(&t->tracefiles, LTT_TRACEFILE_NAME_FACILITIES);
1013 if(group == NULL) {
1014 g_error("Trace %s has no facility tracefile", abs_path);
3865ea09 1015 g_assert(0);
3aee1200 1016 goto facilities_error;
1017 }
1018
16fcbb80 1019 /* Get the trace information for the control/facility 0 tracefile */
1020 g_assert(group->len > 0);
1021 tf = &g_array_index (group, LttTracefile, 0);
1022 header = (struct ltt_block_start_header*)tf->buffer.head;
51551c6f 1023 g_assert(parse_trace_header(header->trace,
1024 tf, t) == 0);
b56dcdf2 1025
1026 t->num_cpu = group->len;
16fcbb80 1027
d79909d1 1028 ret = allocate_marker_data(t);
b44ba972 1029 if (ret)
d79909d1 1030 g_error("Error in allocating marker data");
1031
3aee1200 1032 for(i=0; i<group->len; i++) {
1033 tf = &g_array_index (group, LttTracefile, i);
1034 if(ltt_process_facility_tracefile(tf))
1035 goto facilities_error;
1036 }
dd3a6d39 1037
3aee1200 1038 return t;
1039
1040 /* Error handling */
1041facilities_error:
d79909d1 1042 destroy_marker_data(t);
b56dcdf2 1043find_error:
3aee1200 1044 g_datalist_clear(&t->tracefiles);
b56dcdf2 1045open_error:
3aee1200 1046 g_free(t);
1047alloc_error:
1048 return NULL;
1049
1050}
6cd62ccf 1051
e95fe8f7 1052/* Open another, completely independant, instance of a trace.
1053 *
1054 * A read on this new instance will read the first event of the trace.
1055 *
3aee1200 1056 * When we copy a trace, we want all the opening actions to happen again :
1057 * the trace will be reopened and totally independant from the original.
1058 * That's why we call ltt_trace_open.
e95fe8f7 1059 */
3aee1200 1060LttTrace *ltt_trace_copy(LttTrace *self)
963b5f2d 1061{
3aee1200 1062 return ltt_trace_open(g_quark_to_string(self->pathname));
963b5f2d 1063}
1064
e95fe8f7 1065/*
1066 * Close a trace
1067 */
1068
3aee1200 1069void ltt_trace_close(LttTrace *t)
6cd62ccf 1070{
3aee1200 1071 g_datalist_clear(&t->tracefiles);
1072 g_free(t);
6cd62ccf 1073}
1074
3aee1200 1075
6cd62ccf 1076/*****************************************************************************
3aee1200 1077 * Get the start time and end time of the trace
6cd62ccf 1078 ****************************************************************************/
1079
3c165eaf 1080void ltt_tracefile_time_span_get(LttTracefile *tf,
3aee1200 1081 LttTime *start, LttTime *end)
6cd62ccf 1082{
3aee1200 1083 int err;
6cd62ccf 1084
3aee1200 1085 err = map_block(tf, 0);
1086 if(unlikely(err)) {
1087 g_error("Can not map block");
1088 *start = ltt_time_infinite;
1089 } else
1090 *start = tf->buffer.begin.timestamp;
1091
1092 err = map_block(tf, tf->num_blocks - 1); /* Last block */
1093 if(unlikely(err)) {
1094 g_error("Can not map block");
1095 *end = ltt_time_zero;
1096 } else
1097 *end = tf->buffer.end.timestamp;
6cd62ccf 1098}
1099
3aee1200 1100struct tracefile_time_span_get_args {
1101 LttTrace *t;
1102 LttTime *start;
1103 LttTime *end;
1104};
963b5f2d 1105
8655430b 1106static void group_time_span_get(GQuark name, gpointer data, gpointer user_data)
963b5f2d 1107{
3aee1200 1108 struct tracefile_time_span_get_args *args =
1109 (struct tracefile_time_span_get_args*)user_data;
1110
1111 GArray *group = (GArray *)data;
1112 int i;
1113 LttTracefile *tf;
1114 LttTime tmp_start;
1115 LttTime tmp_end;
1116
1117 for(i=0; i<group->len; i++) {
1118 tf = &g_array_index (group, LttTracefile, i);
1119 if(tf->cpu_online) {
1120 ltt_tracefile_time_span_get(tf, &tmp_start, &tmp_end);
1121 if(ltt_time_compare(*args->start, tmp_start)>0) *args->start = tmp_start;
1122 if(ltt_time_compare(*args->end, tmp_end)<0) *args->end = tmp_end;
1123 }
1124 }
6cd62ccf 1125}
1126
8655430b 1127/* return the start and end time of a trace */
1128
487ad181 1129void ltt_trace_time_span_get(LttTrace *t, LttTime *start, LttTime *end)
1130{
3aee1200 1131 LttTime min_start = ltt_time_infinite;
1132 LttTime max_end = ltt_time_zero;
1133 struct tracefile_time_span_get_args args = { t, &min_start, &max_end };
487ad181 1134
3aee1200 1135 g_datalist_foreach(&t->tracefiles, &group_time_span_get, &args);
1136
1137 if(start != NULL) *start = min_start;
1138 if(end != NULL) *end = max_end;
1139
487ad181 1140}
1141
1142
3aee1200 1143/* Seek to the first event in a tracefile that has a time equal or greater than
1144 * the time passed in parameter.
1145 *
1146 * If the time parameter is outside the tracefile time span, seek to the first
27304273 1147 * event or if after, return ERANGE.
3aee1200 1148 *
1149 * If the time parameter is before the first event, we have to seek specially to
1150 * there.
1151 *
27304273 1152 * If the time is after the end of the trace, return ERANGE.
3aee1200 1153 *
1154 * Do a binary search to find the right block, then a sequential search in the
1155 * block to find the event.
1156 *
1157 * In the special case where the time requested fits inside a block that has no
1158 * event corresponding to the requested time, the first event of the next block
1159 * will be seeked.
1160 *
1161 * IMPORTANT NOTE : // FIXME everywhere...
1162 *
1163 * You MUST NOT do a ltt_tracefile_read right after a ltt_tracefile_seek_time :
1164 * you will jump over an event if you do.
1165 *
1166 * Return value : 0 : no error, the tf->event can be used
27304273 1167 * ERANGE : time if after the last event of the trace
3aee1200 1168 * otherwise : this is an error.
1169 *
1170 * */
1171
1172int ltt_tracefile_seek_time(LttTracefile *tf, LttTime time)
1173{
1174 int ret = 0;
1175 int err;
1176 unsigned int block_num, high, low;
1177
1178 /* seek at the beginning of trace */
1179 err = map_block(tf, 0); /* First block */
1180 if(unlikely(err)) {
1181 g_error("Can not map block");
1182 goto fail;
caf7a67a 1183 }
1184
3aee1200 1185 /* If the time is lower or equal the beginning of the trace,
1186 * go to the first event. */
1187 if(ltt_time_compare(time, tf->buffer.begin.timestamp) <= 0) {
1188 ret = ltt_tracefile_read(tf);
27304273 1189 if(ret == ERANGE) goto range;
1190 else if (ret) goto fail;
3aee1200 1191 goto found; /* There is either no event in the trace or the event points
1192 to the first event in the trace */
1193 }
caf7a67a 1194
3aee1200 1195 err = map_block(tf, tf->num_blocks - 1); /* Last block */
1196 if(unlikely(err)) {
1197 g_error("Can not map block");
1198 goto fail;
caf7a67a 1199 }
6cd62ccf 1200
27304273 1201 /* If the time is after the end of the trace, return ERANGE. */
1202 if(ltt_time_compare(time, tf->buffer.end.timestamp) > 0) {
1203 goto range;
3aee1200 1204 }
62e55dd6 1205
3aee1200 1206 /* Binary search the block */
1207 high = tf->num_blocks - 1;
1208 low = 0;
1209
1210 while(1) {
1211 block_num = ((high-low) / 2) + low;
1212
1213 err = map_block(tf, block_num);
1214 if(unlikely(err)) {
1215 g_error("Can not map block");
1216 goto fail;
db55eaae 1217 }
3aee1200 1218 if(high == low) {
1219 /* We cannot divide anymore : this is what would happen if the time
1220 * requested was exactly between two consecutive buffers'end and start
1221 * timestamps. This is also what would happend if we didn't deal with out
1222 * of span cases prior in this function. */
1223 /* The event is right in the buffer!
1224 * (or in the next buffer first event) */
1225 while(1) {
1226 ret = ltt_tracefile_read(tf);
27304273 1227 if(ret == ERANGE) goto range; /* ERANGE or EPERM */
3aee1200 1228 else if(ret) goto fail;
1229
d9e13a0f 1230 if(ltt_time_compare(time, tf->event.event_time) <= 0)
e45551ac 1231 goto found;
3aee1200 1232 }
1233
27304273 1234 } else if(ltt_time_compare(time, tf->buffer.begin.timestamp) < 0) {
3aee1200 1235 /* go to lower part */
b1369bef 1236 high = block_num - 1;
3aee1200 1237 } else if(ltt_time_compare(time, tf->buffer.end.timestamp) > 0) {
1238 /* go to higher part */
b1369bef 1239 low = block_num + 1;
3aee1200 1240 } else {/* The event is right in the buffer!
1241 (or in the next buffer first event) */
1242 while(1) {
27304273 1243 ret = ltt_tracefile_read(tf);
1244 if(ret == ERANGE) goto range; /* ERANGE or EPERM */
3aee1200 1245 else if(ret) goto fail;
1246
d9e13a0f 1247 if(ltt_time_compare(time, tf->event.event_time) <= 0)
3aee1200 1248 break;
6cd62ccf 1249 }
3aee1200 1250 goto found;
6cd62ccf 1251 }
6cd62ccf 1252 }
3aee1200 1253
1254found:
1255 return 0;
27304273 1256range:
1257 return ERANGE;
3aee1200 1258
1259 /* Error handling */
1260fail:
1261 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1262 g_quark_to_string(tf->name));
1263 return EPERM;
6cd62ccf 1264}
1265
e95fe8f7 1266/* Seek to a position indicated by an LttEventPosition
1267 */
80da81ad 1268
8655430b 1269int ltt_tracefile_seek_position(LttTracefile *tf, const LttEventPosition *ep)
1270{
3aee1200 1271 int err;
1272
1273 if(ep->tracefile != tf) {
1274 goto fail;
80da81ad 1275 }
1276
3aee1200 1277 err = map_block(tf, ep->block);
1278 if(unlikely(err)) {
1279 g_error("Can not map block");
1280 goto fail;
1281 }
1282
1283 tf->event.offset = ep->offset;
18206708 1284
62e4e7bf 1285 /* Put back the event real tsc */
1286 tf->event.tsc = ep->tsc;
1287 tf->buffer.tsc = ep->tsc;
78f79181 1288
3aee1200 1289 err = ltt_tracefile_read_update_event(tf);
1290 if(err) goto fail;
73faf25a 1291
1292 /* deactivate this, as it does nothing for now
3aee1200 1293 err = ltt_tracefile_read_op(tf);
1294 if(err) goto fail;
73faf25a 1295 */
80da81ad 1296
a0c1f622 1297 return 0;
3aee1200 1298
1299fail:
1300 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1301 g_quark_to_string(tf->name));
a0c1f622 1302 return 1;
3aee1200 1303}
1304
e95fe8f7 1305/* Given a TSC value, return the LttTime (seconds,nanoseconds) it
1306 * corresponds to.
1307 */
1308
ae3d0f50 1309LttTime ltt_interpolate_time_from_tsc(LttTracefile *tf, guint64 tsc)
3aee1200 1310{
1311 LttTime time;
62e4e7bf 1312
1313 if(tsc > tf->trace->start_tsc) {
1314 time = ltt_time_from_uint64(
1315 (double)(tsc - tf->trace->start_tsc)
1316 * (1000000000.0 / tf->trace->freq_scale)
1317 / (double)tf->trace->start_freq);
1318 time = ltt_time_add(tf->trace->start_time_from_tsc, time);
1319 } else {
1320 time = ltt_time_from_uint64(
1321 (double)(tf->trace->start_tsc - tsc)
1322 * (1000000000.0 / tf->trace->freq_scale)
1323 / (double)tf->trace->start_freq);
1324 time = ltt_time_sub(tf->trace->start_time_from_tsc, time);
1325 }
3aee1200 1326 return time;
80da81ad 1327}
1328
ae3d0f50 1329/* Calculate the real event time based on the buffer boundaries */
1330LttTime ltt_interpolate_time(LttTracefile *tf, LttEvent *event)
1331{
62e4e7bf 1332 return ltt_interpolate_time_from_tsc(tf, tf->buffer.tsc);
ae3d0f50 1333}
1334
eed2ef37 1335
1336/* Get the current event of the tracefile : valid until the next read */
1337LttEvent *ltt_tracefile_get_event(LttTracefile *tf)
1338{
1339 return &tf->event;
1340}
1341
1342
1343
6cd62ccf 1344/*****************************************************************************
1345 *Function name
3aee1200 1346 * ltt_tracefile_read : Read the next event in the tracefile
6cd62ccf 1347 *Input params
1348 * t : tracefile
1349 *Return value
3aee1200 1350 *
1351 * Returns 0 if an event can be used in tf->event.
d822520b 1352 * Returns ERANGE on end of trace. The event in tf->event still can be used
1353 * (if the last block was not empty).
3aee1200 1354 * Returns EPERM on error.
1355 *
1356 * This function does make the tracefile event structure point to the event
1357 * currently pointed to by the tf->event.
1358 *
1359 * Note : you must call a ltt_tracefile_seek to the beginning of the trace to
1360 * reinitialize it after an error if you want results to be coherent.
1361 * It would be the case if a end of trace last buffer has no event : the end
1362 * of trace wouldn't be returned, but an error.
1363 * We make the assumption there is at least one event per buffer.
6cd62ccf 1364 ****************************************************************************/
1365
3aee1200 1366int ltt_tracefile_read(LttTracefile *tf)
6cd62ccf 1367{
963b5f2d 1368 int err;
6cd62ccf 1369
3aee1200 1370 err = ltt_tracefile_read_seek(tf);
1371 if(err) return err;
1372 err = ltt_tracefile_read_update_event(tf);
1373 if(err) return err;
73faf25a 1374
1375 /* deactivate this, as it does nothing for now
3aee1200 1376 err = ltt_tracefile_read_op(tf);
1377 if(err) return err;
73faf25a 1378 */
3aee1200 1379
1380 return 0;
1381}
1382
1383int ltt_tracefile_read_seek(LttTracefile *tf)
1384{
1385 int err;
1386
1387 /* Get next buffer until we finally have an event, or end of trace */
1388 while(1) {
1389 err = ltt_seek_next_event(tf);
1390 if(unlikely(err == ENOPROTOOPT)) {
1391 return EPERM;
bdc36259 1392 }
bdc36259 1393
3aee1200 1394 /* Are we at the end of the buffer ? */
1395 if(err == ERANGE) {
1396 if(unlikely(tf->buffer.index == tf->num_blocks-1)){ /* end of trace ? */
1397 return ERANGE;
1398 } else {
1399 /* get next block */
1400 err = map_block(tf, tf->buffer.index + 1);
1401 if(unlikely(err)) {
1402 g_error("Can not map block");
1403 return EPERM;
1404 }
1405 }
1406 } else break; /* We found an event ! */
1407 }
2dee981d 1408
3aee1200 1409 return 0;
1410}
18206708 1411
e95fe8f7 1412/* do an operation when reading a new event */
18206708 1413
73faf25a 1414/* This function does nothing for now */
1415#if 0
3aee1200 1416int ltt_tracefile_read_op(LttTracefile *tf)
1417{
3aee1200 1418 LttEvent *event;
1419
1420 event = &tf->event;
18206708 1421
73faf25a 1422 /* do event specific operation */
1423
1424 /* nothing */
40331ba8 1425
3aee1200 1426 return 0;
6cd62ccf 1427}
73faf25a 1428#endif
6cd62ccf 1429
91f8d488 1430static void print_debug_event_header(LttEvent *ev, void *start_pos, void *end_pos)
1431{
1432 unsigned int offset = 0;
1433 int i, j;
1434
1435 g_printf("Event header (tracefile %s offset %llx):\n",
afd57a3c 1436 g_quark_to_string(ev->tracefile->long_name),
91f8d488 1437 ((uint64_t)ev->tracefile->buffer.index * ev->tracefile->buf_size)
1438 + (long)start_pos - (long)ev->tracefile->buffer.head);
1439
1440 while (offset < (long)end_pos - (long)start_pos) {
1441 g_printf("%8lx", (long)start_pos - (long)ev->tracefile->buffer.head + offset);
1442 g_printf(" ");
1443
1444 for (i = 0; i < 4 ; i++) {
1445 for (j = 0; j < 4; j++) {
1446 if (offset + ((i * 4) + j) <
1447 (long)end_pos - (long)start_pos)
1448 g_printf("%02hhX",
d3353231 1449 ((char*)start_pos)[offset + ((i * 4) + j)]);
91f8d488 1450 else
1451 g_printf(" ");
1452 g_printf(" ");
1453 }
1454 if (i < 4)
1455 g_printf(" ");
1456 }
1457 offset+=16;
1458 g_printf("\n");
1459 }
1460}
1461
6cd62ccf 1462
3aee1200 1463/* same as ltt_tracefile_read, but does not seek to the next event nor call
1464 * event specific operation. */
1465int ltt_tracefile_read_update_event(LttTracefile *tf)
6cd62ccf 1466{
3aee1200 1467 void * pos;
1468 LttEvent *event;
91f8d488 1469 void *pos_aligned;
3aee1200 1470
1471 event = &tf->event;
eed2ef37 1472 pos = tf->buffer.head + event->offset;
3aee1200 1473
1474 /* Read event header */
1475
62e4e7bf 1476 /* Align the head */
1477 if(!tf->compact)
256a5b3a 1478 pos += ltt_align((size_t)pos, tf->trace->arch_size, tf->alignment);
62e4e7bf 1479 else {
1480 g_assert(tf->has_heartbeat);
256a5b3a 1481 pos += ltt_align((size_t)pos, sizeof(uint32_t), tf->alignment);
62e4e7bf 1482 }
91f8d488 1483 pos_aligned = pos;
3aee1200 1484
62e4e7bf 1485 if(tf->has_heartbeat) {
1486 event->timestamp = ltt_get_uint32(LTT_GET_BO(tf),
1487 pos);
d1bb700c 1488 if(!tf->compact) {
1489 /* 32 bits -> 64 bits tsc */
1490 /* note : still works for seek and non seek cases. */
1491 if(event->timestamp < (0xFFFFFFFFULL&tf->buffer.tsc)) {
1492 tf->buffer.tsc = ((tf->buffer.tsc&0xFFFFFFFF00000000ULL)
1493 + 0x100000000ULL)
1494 | (guint64)event->timestamp;
1495 event->tsc = tf->buffer.tsc;
1496 } else {
1497 /* no overflow */
1498 tf->buffer.tsc = (tf->buffer.tsc&0xFFFFFFFF00000000ULL)
1499 | (guint64)event->timestamp;
1500 event->tsc = tf->buffer.tsc;
62e4e7bf 1501 event->compact_data = 0;
d1bb700c 1502 }
1503 } else {
1504 /* Compact header */
1505 /* We keep the LSB of the previous timestamp, to make sure
1506 * we never go back */
1507 event->event_id = event->timestamp >> tf->tscbits;
d3353231 1508 event->event_id = event->event_id & ((1 << tf->compact_event_bits) - 1);
dd3a6d39 1509 event->compact_data = event->timestamp >>
d3353231 1510 (tf->compact_event_bits + tf->tscbits);
dd3a6d39 1511 //printf("tsc bits %u, ev bits %u init data %u\n",
62e4e7bf 1512 // tf->tscbits, tf->trace->compact_event_bits, event->compact_data);
dd3a6d39 1513 /* Put the compact data back in original endianness */
1514 event->compact_data = ltt_get_uint32(LTT_GET_BO(tf), &event->compact_data);
d1bb700c 1515 event->event_size = 0xFFFF;
dd3a6d39 1516 //printf("Found compact event %d\n", event->event_id);
1517 //printf("Compact data %d\n", event->compact_data);
d1bb700c 1518 event->timestamp = event->timestamp << tf->tsc_lsb_truncate;
1519 event->timestamp = event->timestamp & tf->tsc_mask;
dd3a6d39 1520 //printf("timestamp 0x%lX\n", event->timestamp);
1521 //printf("mask 0x%llX\n", tf->tsc_mask);
1522 //printf("mask_next 0x%llX\n", tf->tsc_mask_next_bit);
1523 //printf("previous tsc 0x%llX\n", tf->buffer.tsc);
1524 //printf("previous tsc&mask 0x%llX\n", tf->tsc_mask&tf->buffer.tsc);
1525 //printf("previous tsc&(~mask) 0x%llX\n", tf->buffer.tsc&(~tf->tsc_mask));
d1bb700c 1526 if(event->timestamp < (tf->tsc_mask&tf->buffer.tsc)) {
dd3a6d39 1527 //printf("wrap\n");
d1bb700c 1528 tf->buffer.tsc = ((tf->buffer.tsc&(~tf->tsc_mask))
1529 + tf->tsc_mask_next_bit)
1530 | (guint64)event->timestamp;
1531 event->tsc = tf->buffer.tsc;
1532 } else {
dd3a6d39 1533 //printf("no wrap\n");
d1bb700c 1534 /* no overflow */
1535 tf->buffer.tsc = (tf->buffer.tsc&(~tf->tsc_mask))
1536 | (guint64)event->timestamp;
1537 event->tsc = tf->buffer.tsc;
1538 }
dd3a6d39 1539 //printf("current tsc 0x%llX\n", tf->buffer.tsc);
d1bb700c 1540 }
62e4e7bf 1541 pos += sizeof(guint32);
1542 } else {
1543 event->tsc = ltt_get_uint64(LTT_GET_BO(tf), pos);
1544 tf->buffer.tsc = event->tsc;
1545 event->compact_data = 0;
1546 pos += sizeof(guint64);
1547 }
1548 event->event_time = ltt_interpolate_time(tf, event);
3aee1200 1549
d1bb700c 1550 if(!tf->compact) {
f439de06 1551 event->event_id = ltt_get_uint16(LTT_GET_BO(tf), pos);
3c165eaf 1552 pos += sizeof(guint16);
d1bb700c 1553
1554 event->event_size = ltt_get_uint16(LTT_GET_BO(tf), pos);
1555 pos += sizeof(guint16);
1556 } else {
1557 /* Compact event */
dcf96842 1558 event->event_size = 0xFFFF;
d1bb700c 1559 }
91f8d488 1560
1561 if (a_event_debug)
1562 print_debug_event_header(event, pos_aligned, pos);
1563
62e4e7bf 1564 /* Align the head */
1565 if(!tf->compact)
256a5b3a 1566 pos += ltt_align((size_t)pos, tf->trace->arch_size, tf->alignment);
f104d082 1567
3aee1200 1568 event->data = pos;
1569
77175651 1570 /* get the data size and update the event fields with the current
d1bb700c 1571 * information. Also update the time if a heartbeat_full event is found. */
eed2ef37 1572 ltt_update_event_size(tf);
77175651 1573
3aee1200 1574 return 0;
6cd62ccf 1575}
1576
507915ee 1577
6cd62ccf 1578/****************************************************************************
1579 *Function name
3aee1200 1580 * map_block : map a block from the file
6cd62ccf 1581 *Input Params
1582 * lttdes : ltt trace file
1583 * whichBlock : the block which will be read
1584 *return value
1585 * 0 : success
1586 * EINVAL : lseek fail
1587 * EIO : can not read from the file
1588 ****************************************************************************/
1589
8655430b 1590static gint map_block(LttTracefile * tf, guint block_num)
6cd62ccf 1591{
b77d1b57 1592 int page_size = getpagesize();
3aee1200 1593 struct ltt_block_start_header *header;
1594
1595 g_assert(block_num < tf->num_blocks);
6cd62ccf 1596
f628823c 1597 if(tf->buffer.head != NULL) {
1598 if(munmap(tf->buffer.head, PAGE_ALIGN(tf->buf_size))) {
1599 g_warning("unmap size : %u\n",
1600 PAGE_ALIGN(tf->buf_size));
1601 perror("munmap error");
1602 g_assert(0);
1603 }
1604 }
1605
ac849774 1606
3aee1200 1607 /* Multiple of pages aligned head */
b77d1b57 1608 tf->buffer.head = mmap(0,
f628823c 1609 PAGE_ALIGN(tf->buf_size),
b77d1b57 1610 PROT_READ, MAP_PRIVATE, tf->fd,
f628823c 1611 PAGE_ALIGN((off_t)tf->buf_size * (off_t)block_num));
3aee1200 1612
3865ea09 1613 if(tf->buffer.head == MAP_FAILED) {
3aee1200 1614 perror("Error in allocating memory for buffer of tracefile");
3865ea09 1615 g_assert(0);
3aee1200 1616 goto map_error;
6cd62ccf 1617 }
f64fedd7 1618 g_assert( ( (gulong)tf->buffer.head&(8-1) ) == 0); // make sure it's aligned.
3aee1200 1619
6cd62ccf 1620
3aee1200 1621 tf->buffer.index = block_num;
1622
1623 header = (struct ltt_block_start_header*)tf->buffer.head;
1624
791dffa6 1625#if 0
986e2a7c 1626 tf->buffer.begin.timestamp = ltt_time_add(
1627 ltt_time_from_uint64(
1628 ltt_get_uint64(LTT_GET_BO(tf),
1629 &header->begin.timestamp)
1630 - tf->trace->start_monotonic),
1631 tf->trace->start_time);
791dffa6 1632#endif //0
348c6ba8 1633 //g_debug("block %u begin : %lu.%lu", block_num,
1634 // tf->buffer.begin.timestamp.tv_sec, tf->buffer.begin.timestamp.tv_nsec);
3aee1200 1635 tf->buffer.begin.cycle_count = ltt_get_uint64(LTT_GET_BO(tf),
1636 &header->begin.cycle_count);
986e2a7c 1637 tf->buffer.begin.freq = ltt_get_uint64(LTT_GET_BO(tf),
1638 &header->begin.freq);
62e4e7bf 1639 if(tf->buffer.begin.freq == 0)
1640 tf->buffer.begin.freq = tf->trace->start_freq;
ae3d0f50 1641
1642 tf->buffer.begin.timestamp = ltt_interpolate_time_from_tsc(tf,
62e4e7bf 1643 tf->buffer.begin.cycle_count);
ae3d0f50 1644#if 0
62e4e7bf 1645 ltt_time_add(
791dffa6 1646 ltt_time_from_uint64(
88612d17 1647 (double)(tf->buffer.begin.cycle_count
1648 - tf->trace->start_tsc) * 1000000.0
791dffa6 1649 / (double)tf->trace->start_freq),
62e4e7bf 1650 tf->trace->start_time_from_tsc);
ae3d0f50 1651#endif //0
791dffa6 1652#if 0
1653
986e2a7c 1654 tf->buffer.end.timestamp = ltt_time_add(
1655 ltt_time_from_uint64(
1656 ltt_get_uint64(LTT_GET_BO(tf),
1657 &header->end.timestamp)
1658 - tf->trace->start_monotonic),
1659 tf->trace->start_time);
791dffa6 1660#endif //0
348c6ba8 1661 //g_debug("block %u end : %lu.%lu", block_num,
1662 // tf->buffer.end.timestamp.tv_sec, tf->buffer.end.timestamp.tv_nsec);
3aee1200 1663 tf->buffer.end.cycle_count = ltt_get_uint64(LTT_GET_BO(tf),
1664 &header->end.cycle_count);
986e2a7c 1665 tf->buffer.end.freq = ltt_get_uint64(LTT_GET_BO(tf),
1666 &header->end.freq);
62e4e7bf 1667 if(tf->buffer.end.freq == 0)
1668 tf->buffer.end.freq = tf->trace->start_freq;
1669
3aee1200 1670 tf->buffer.lost_size = ltt_get_uint32(LTT_GET_BO(tf),
986e2a7c 1671 &header->lost_size);
ae3d0f50 1672 tf->buffer.end.timestamp = ltt_interpolate_time_from_tsc(tf,
62e4e7bf 1673 tf->buffer.end.cycle_count);
ae3d0f50 1674#if 0
62e4e7bf 1675 ltt_time_add(
791dffa6 1676 ltt_time_from_uint64(
88612d17 1677 (double)(tf->buffer.end.cycle_count
1678 - tf->trace->start_tsc) * 1000000.0
791dffa6 1679 / (double)tf->trace->start_freq),
88612d17 1680 tf->trace->start_time_from_tsc);
ae3d0f50 1681#endif //0
3aee1200 1682 tf->buffer.tsc = tf->buffer.begin.cycle_count;
1683 tf->event.tsc = tf->buffer.tsc;
986e2a7c 1684 tf->buffer.freq = tf->buffer.begin.freq;
3aee1200 1685
1686 /* FIXME
1687 * eventually support variable buffer size : will need a partial pre-read of
1688 * the headers to create an index when we open the trace... eventually. */
f628823c 1689 g_assert(tf->buf_size == ltt_get_uint32(LTT_GET_BO(tf),
3aee1200 1690 &header->buf_size));
507915ee 1691
3aee1200 1692 /* Now that the buffer is mapped, calculate the time interpolation for the
1693 * block. */
1694
791dffa6 1695// tf->buffer.nsecs_per_cycle = calc_nsecs_per_cycle(tf);
1696 //tf->buffer.cyc2ns_scale = calc_nsecs_per_cycle(tf);
3aee1200 1697
1698 /* Make the current event point to the beginning of the buffer :
1699 * it means that the event read must get the first event. */
1700 tf->event.tracefile = tf;
1701 tf->event.block = block_num;
eed2ef37 1702 tf->event.offset = 0;
3aee1200 1703
1704 return 0;
6cd62ccf 1705
3aee1200 1706map_error:
1707 return -errno;
40331ba8 1708
6cd62ccf 1709}
1710
91f8d488 1711static void print_debug_event_data(LttEvent *ev)
1712{
1713 unsigned int offset = 0;
1714 int i, j;
1715
1716 if (!max(ev->event_size, ev->data_size))
1717 return;
1718
1719 g_printf("Event data (tracefile %s offset %llx):\n",
afd57a3c 1720 g_quark_to_string(ev->tracefile->long_name),
91f8d488 1721 ((uint64_t)ev->tracefile->buffer.index * ev->tracefile->buf_size)
1722 + (long)ev->data - (long)ev->tracefile->buffer.head);
1723
1724 while (offset < max(ev->event_size, ev->data_size)) {
1725 g_printf("%8lx", (long)ev->data + offset
1726 - (long)ev->tracefile->buffer.head);
1727 g_printf(" ");
1728
1729 for (i = 0; i < 4 ; i++) {
1730 for (j = 0; j < 4; j++) {
1731 if (offset + ((i * 4) + j) < max(ev->event_size, ev->data_size))
1732 g_printf("%02hhX", ((char*)ev->data)[offset + ((i * 4) + j)]);
1733 else
1734 g_printf(" ");
1735 g_printf(" ");
1736 }
1737 if (i < 4)
1738 g_printf(" ");
1739 }
1740
1741 g_printf(" ");
1742
1743 for (i = 0; i < 4; i++) {
1744 for (j = 0; j < 4; j++) {
1745 if (offset + ((i * 4) + j) < max(ev->event_size, ev->data_size)) {
1746 if (isprint(((char*)ev->data)[offset + ((i * 4) + j)]))
1747 g_printf("%c", ((char*)ev->data)[offset + ((i * 4) + j)]);
1748 else
1749 g_printf(".");
1750 } else
1751 g_printf(" ");
1752 }
1753 }
1754 offset+=16;
1755 g_printf("\n");
1756 }
1757}
1758
77175651 1759/* It will update the fields offsets too */
1760void ltt_update_event_size(LttTracefile *tf)
6cd62ccf 1761{
2312de30 1762 off_t size = 0;
d1bb700c 1763 char *tscdata;
d2007fbd 1764 struct marker_info *info;
44f317b7 1765
d2007fbd 1766 switch((enum marker_id)tf->event.event_id) {
1767 case MARKER_ID_SET_MARKER_ID:
44f317b7 1768 size = strlen((char*)tf->event.data) + 1;
07effdbd 1769 g_debug("marker %s id set", (char*)tf->event.data);
256a5b3a 1770 size += ltt_align(size, sizeof(guint16), tf->alignment);
d2007fbd 1771 size += sizeof(guint16);
3c165eaf 1772 size += sizeof(guint8);
1773 size += sizeof(guint8);
1774 size += sizeof(guint8);
1775 size += sizeof(guint8);
1776 size += sizeof(guint8);
44f317b7 1777 break;
d2007fbd 1778 case MARKER_ID_SET_MARKER_FORMAT:
07effdbd 1779 g_debug("marker %s format set", (char*)tf->event.data);
44f317b7 1780 size = strlen((char*)tf->event.data) + 1;
2e13d6af 1781 size += strlen((char*)tf->event.data + size) + 1;
44f317b7 1782 break;
d2007fbd 1783 case MARKER_ID_HEARTBEAT_32:
07effdbd 1784 g_debug("Update Event heartbeat 32 bits");
44f317b7 1785 break;
d2007fbd 1786 case MARKER_ID_HEARTBEAT_64:
07effdbd 1787 g_debug("Update Event heartbeat 64 bits");
d1bb700c 1788 tscdata = (char*)(tf->event.data);
1789 tf->event.tsc = ltt_get_uint64(LTT_GET_BO(tf), tscdata);
1790 tf->buffer.tsc = tf->event.tsc;
1791 tf->event.event_time = ltt_interpolate_time(tf, &tf->event);
256a5b3a 1792 size = ltt_align(size, sizeof(guint64), tf->alignment);
d2007fbd 1793 size += sizeof(guint64);
d1bb700c 1794 break;
256a5b3a 1795 }
1796
1797 info = marker_get_info_from_id(tf->trace, tf->event.event_id);
dcf96842 1798
256a5b3a 1799 if (tf->event.event_id >= MARKER_CORE_IDS)
1800 g_assert(info != NULL);
1801
1802 /* Do not update field offsets of core markers when initially reading the
1803 * facility tracefile when the infos about these markers do not exist yet.
1804 */
1805 if (likely(info && info->fields)) {
1806 if (info->size != -1)
1807 size = info->size;
1808 else
1809 size = marker_update_fields_offsets(marker_get_info_from_id(tf->trace,
1810 tf->event.event_id), tf->event.data);
44f317b7 1811 }
c4afd5d8 1812
d2007fbd 1813 tf->event.data_size = size;
1814
1815 /* Check consistency between kernel and LTTV structure sizes */
1816 if(tf->event.event_size == 0xFFFF) {
1817 /* Event size too big to fit in the event size field */
1818 tf->event.event_size = tf->event.data_size;
1819 }
91f8d488 1820
1821 if (a_event_debug)
1822 print_debug_event_data(&tf->event);
1823
27c089c1 1824 /* Having a marker load or marker format event out of the facilities
1825 * tracefiles is a serious bug. */
1826 switch((enum marker_id)tf->event.event_id) {
1827 case MARKER_ID_SET_MARKER_ID:
1828 case MARKER_ID_SET_MARKER_FORMAT:
1829 if (tf->name != g_quark_from_string("/control/facilities"))
1830 g_error("Trace inconsistency : metadata event found in data "
1831 "tracefile %s", g_quark_to_string(tf->long_name));
1832 }
1833
d2007fbd 1834 if (tf->event.data_size != tf->event.event_size) {
3c165eaf 1835 struct marker_info *info = marker_get_info_from_id(tf->trace,
1836 tf->event.event_id);
1837 g_error("Kernel/LTTV event size differs for event %s: kernel %u, LTTV %u",
1838 g_quark_to_string(info->name),
1839 tf->event.event_size, tf->event.data_size);
d2007fbd 1840 exit(-1);
1841 }
6cd62ccf 1842}
1843
6cd62ccf 1844
3aee1200 1845/* Take the tf current event offset and use the event facility id and event id
1846 * to figure out where is the next event offset.
1847 *
1848 * This is an internal function not aiming at being used elsewhere : it will
1849 * not jump over the current block limits. Please consider using
1850 * ltt_tracefile_read to do this.
1851 *
1852 * Returns 0 on success
1853 * ERANGE if we are at the end of the buffer.
1854 * ENOPROTOOPT if an error occured when getting the current event size.
1855 */
8655430b 1856static int ltt_seek_next_event(LttTracefile *tf)
6cd62ccf 1857{
3aee1200 1858 int ret = 0;
1859 void *pos;
3aee1200 1860
1861 /* seek over the buffer header if we are at the buffer start */
eed2ef37 1862 if(tf->event.offset == 0) {
51551c6f 1863 tf->event.offset += tf->buffer_header_size;
b77d1b57 1864
f628823c 1865 if(tf->event.offset == tf->buf_size - tf->buffer.lost_size) {
b77d1b57 1866 ret = ERANGE;
1867 }
3aee1200 1868 goto found;
1869 }
6cd62ccf 1870
6cd62ccf 1871
3aee1200 1872 pos = tf->event.data;
1873
77175651 1874 if(tf->event.data_size < 0) goto error;
3aee1200 1875
77175651 1876 pos += (size_t)tf->event.data_size;
3aee1200 1877
eed2ef37 1878 tf->event.offset = pos - tf->buffer.head;
cb03932a 1879
f628823c 1880 if(tf->event.offset == tf->buf_size - tf->buffer.lost_size) {
cb03932a 1881 ret = ERANGE;
1882 goto found;
1883 }
36d36c9f 1884 g_assert(tf->event.offset < tf->buf_size - tf->buffer.lost_size);
3aee1200 1885
1886found:
1887 return ret;
1888
1889error:
1890 g_error("Error in ltt_seek_next_event for tracefile %s",
1891 g_quark_to_string(tf->name));
1892 return ENOPROTOOPT;
6cd62ccf 1893}
1894
791dffa6 1895#if 0
6cd62ccf 1896/*****************************************************************************
1897 *Function name
3aee1200 1898 * calc_nsecs_per_cycle : calculate nsecs per cycle for current block
986e2a7c 1899 *
1900 * 1.0 / (freq(khz) *1000) * 1000000000
6cd62ccf 1901 *Input Params
1902 * t : tracefile
1903 ****************************************************************************/
986e2a7c 1904/* from timer_tsc.c */
1905#define CYC2NS_SCALE_FACTOR 10
791dffa6 1906static guint32 calc_nsecs_per_cycle(LttTracefile * tf)
6cd62ccf 1907{
986e2a7c 1908 //return 1e6 / (double)tf->buffer.freq;
791dffa6 1909 guint32 cpu_mhz = tf->buffer.freq / 1000;
1910 guint32 cyc2ns_scale = (1000 << CYC2NS_SCALE_FACTOR)/cpu_mhz;
6cd62ccf 1911
791dffa6 1912 return cyc2ns_scale;
986e2a7c 1913 // return 1e6 / (double)tf->buffer.freq;
3aee1200 1914}
791dffa6 1915
1916static guint64 cycles_2_ns(LttTracefile *tf, guint64 cycles)
1917{
1918 return (cycles * tf->buffer.cyc2ns_scale) >> CYC2NS_SCALE_FACTOR;
1919}
1920#endif //0
1921
3aee1200 1922#if 0
1923void setFieldsOffset(LttTracefile *tf, LttEventType *evT,void *evD)
1924{
1925 LttField * rootFld = evT->root_field;
1926 // rootFld->base_address = evD;
2dee981d 1927
3aee1200 1928 if(likely(rootFld))
1929 rootFld->field_size = getFieldtypeSize(tf, evT->facility,
1930 evT, 0,0,rootFld, evD);
6cd62ccf 1931}
3aee1200 1932#endif //0
f104d082 1933#if 0
3aee1200 1934/*****************************************************************************
6cd62ccf 1935 *Function name
3aee1200 1936 * set_fields_offsets : set the precomputable offset of the fields
6cd62ccf 1937 *Input params
3aee1200 1938 * tracefile : opened trace file
1939 * event_type : the event type
6cd62ccf 1940 ****************************************************************************/
1941
3aee1200 1942void set_fields_offsets(LttTracefile *tf, LttEventType *event_type)
6cd62ccf 1943{
3aee1200 1944 LttField *field = event_type->root_field;
1945 enum field_status fixed_root = FIELD_FIXED, fixed_parent = FIELD_FIXED;
1946
1947 if(likely(field))
1948 preset_field_type_size(tf, event_type, 0, 0,
1949 &fixed_root, &fixed_parent,
1950 field);
1951
1952}
f104d082 1953#endif //0
1954
1955
1956/*****************************************************************************
1957 *Function name
1958 * get_alignment : Get the alignment needed for a field.
1959 *Input params
f104d082 1960 * field : field
1961 *
1962 * returns : The size on which it must be aligned.
1963 *
1964 ****************************************************************************/
3c165eaf 1965#if 0
743e50fd 1966off_t get_alignment(LttField *field)
f104d082 1967{
2312de30 1968 LttType *type = &field->field_type;
f104d082 1969
1970 switch(type->type_class) {
1971 case LTT_INT_FIXED:
1972 case LTT_UINT_FIXED:
1973 case LTT_POINTER:
1974 case LTT_CHAR:
1975 case LTT_UCHAR:
1976 case LTT_SHORT:
1977 case LTT_USHORT:
1978 case LTT_INT:
1979 case LTT_UINT:
1980 case LTT_LONG:
1981 case LTT_ULONG:
1982 case LTT_SIZE_T:
1983 case LTT_SSIZE_T:
1984 case LTT_OFF_T:
1985 case LTT_FLOAT:
1986 case LTT_ENUM:
1987 /* Align offset on type size */
83e160f2 1988 g_assert(field->field_size != 0);
f104d082 1989 return field->field_size;
1990 break;
1991 case LTT_STRING:
83e160f2 1992 return 1;
f104d082 1993 break;
1994 case LTT_ARRAY:
1995 g_assert(type->fields->len == 1);
1996 {
1997 LttField *child = &g_array_index(type->fields, LttField, 0);
743e50fd 1998 return get_alignment(child);
f104d082 1999 }
2000 break;
2001 case LTT_SEQUENCE:
2002 g_assert(type->fields->len == 2);
2003 {
83e160f2 2004 off_t localign = 1;
f104d082 2005 LttField *child = &g_array_index(type->fields, LttField, 0);
2006
743e50fd 2007 localign = max(localign, get_alignment(child));
f104d082 2008
2009 child = &g_array_index(type->fields, LttField, 1);
743e50fd 2010 localign = max(localign, get_alignment(child));
f104d082 2011
2012 return localign;
2013 }
2014 break;
2015 case LTT_STRUCT:
2016 case LTT_UNION:
2017 {
2018 guint i;
83e160f2 2019 off_t localign = 1;
f104d082 2020
2021 for(i=0; i<type->fields->len; i++) {
2022 LttField *child = &g_array_index(type->fields, LttField, i);
743e50fd 2023 localign = max(localign, get_alignment(child));
f104d082 2024 }
2025 return localign;
2026 }
2027 break;
2028 case LTT_NONE:
2029 default:
2030 g_error("get_alignment : unknown type");
83e160f2 2031 return -1;
f104d082 2032 }
f104d082 2033}
2034
3c165eaf 2035#endif //0
2036
f104d082 2037/*****************************************************************************
2038 *Function name
2039 * field_compute_static_size : Determine the size of fields known by their
2040 * sole definition. Unions, arrays and struct sizes might be known, but
2041 * the parser does not give that information.
2042 *Input params
2043 * tf : tracefile
2044 * field : field
2045 *
2046 ****************************************************************************/
3c165eaf 2047#if 0
743e50fd 2048void field_compute_static_size(LttFacility *fac, LttField *field)
f104d082 2049{
2312de30 2050 LttType *type = &field->field_type;
f104d082 2051
2052 switch(type->type_class) {
2053 case LTT_INT_FIXED:
2054 case LTT_UINT_FIXED:
2055 case LTT_POINTER:
2056 case LTT_CHAR:
2057 case LTT_UCHAR:
2058 case LTT_SHORT:
2059 case LTT_USHORT:
2060 case LTT_INT:
2061 case LTT_UINT:
2062 case LTT_LONG:
2063 case LTT_ULONG:
2064 case LTT_SIZE_T:
2065 case LTT_SSIZE_T:
2066 case LTT_OFF_T:
2067 case LTT_FLOAT:
2068 case LTT_ENUM:
2069 case LTT_STRING:
2070 /* nothing to do */
2071 break;
2072 case LTT_ARRAY:
2073 /* note this : array type size is the number of elements in the array,
2074 * while array field size of the length of the array in bytes */
2075 g_assert(type->fields->len == 1);
2076 {
2077 LttField *child = &g_array_index(type->fields, LttField, 0);
743e50fd 2078 field_compute_static_size(fac, child);
f104d082 2079
2080 if(child->field_size != 0) {
2081 field->field_size = type->size * child->field_size;
2082 field->dynamic_offsets = g_array_sized_new(FALSE, TRUE,
2083 sizeof(off_t), type->size);
2084 } else {
2085 field->field_size = 0;
2086 }
2087 }
2088 break;
2089 case LTT_SEQUENCE:
2090 g_assert(type->fields->len == 2);
2091 {
2312de30 2092 off_t local_offset = 0;
f104d082 2093 LttField *child = &g_array_index(type->fields, LttField, 1);
743e50fd 2094 field_compute_static_size(fac, child);
f104d082 2095 field->field_size = 0;
2096 type->size = 0;
2097 if(child->field_size != 0) {
2098 field->dynamic_offsets = g_array_sized_new(FALSE, TRUE,
2099 sizeof(off_t), SEQUENCE_AVG_ELEMENTS);
2100 }
2101 }
2102 break;
2103 case LTT_STRUCT:
2104 case LTT_UNION:
2105 {
2106 guint i;
2107 for(i=0;i<type->fields->len;i++) {
2108 LttField *child = &g_array_index(type->fields, LttField, i);
743e50fd 2109 field_compute_static_size(fac, child);
f104d082 2110 if(child->field_size != 0) {
743e50fd 2111 type->size += ltt_align(type->size, get_alignment(child),
2112 fac->alignment);
f104d082 2113 type->size += child->field_size;
2114 } else {
2115 /* As soon as we find a child with variable size, we have
2116 * a variable size */
2117 type->size = 0;
2118 break;
2119 }
2120 }
2121 field->field_size = type->size;
2122 }
2123 break;
2124 default:
2125 g_error("field_static_size : unknown type");
2312de30 2126 }
f104d082 2127
2128}
3c165eaf 2129#endif //0
f104d082 2130
2131
2132/*****************************************************************************
2133 *Function name
2134 * precompute_fields_offsets : set the precomputable offset of the fields
2135 *Input params
743e50fd 2136 * fac : facility
f104d082 2137 * field : the field
2138 * offset : pointer to the current offset, must be incremented
2139 *
2140 * return : 1 : found a variable length field, stop the processing.
2141 * 0 otherwise.
2142 ****************************************************************************/
2143
3c165eaf 2144#if 0
dd3a6d39 2145gint precompute_fields_offsets(LttFacility *fac, LttField *field, off_t *offset, gint is_compact)
f104d082 2146{
2312de30 2147 LttType *type = &field->field_type;
dd3a6d39 2148
2149 if(unlikely(is_compact)) {
2150 g_assert(field->field_size != 0);
2151 /* FIXME THIS IS A HUUUUUGE hack :
2152 * offset is between the compact_data field in struct LttEvent
2153 * and the address of the field root in the memory map.
2154 * ark. Both will stay at the same addresses while the event
2155 * is readable, so it's ok.
2156 */
2157 field->offset_root = 0;
2158 field->fixed_root = FIELD_FIXED;
2159 return 0;
2160 }
f104d082 2161
2162 switch(type->type_class) {
2163 case LTT_INT_FIXED:
2164 case LTT_UINT_FIXED:
2165 case LTT_POINTER:
2166 case LTT_CHAR:
2167 case LTT_UCHAR:
2168 case LTT_SHORT:
2169 case LTT_USHORT:
2170 case LTT_INT:
2171 case LTT_UINT:
2172 case LTT_LONG:
2173 case LTT_ULONG:
2174 case LTT_SIZE_T:
2175 case LTT_SSIZE_T:
2176 case LTT_OFF_T:
2177 case LTT_FLOAT:
2178 case LTT_ENUM:
743e50fd 2179 g_assert(field->field_size != 0);
f104d082 2180 /* Align offset on type size */
743e50fd 2181 *offset += ltt_align(*offset, get_alignment(field),
2182 fac->alignment);
f104d082 2183 /* remember offset */
2184 field->offset_root = *offset;
2185 field->fixed_root = FIELD_FIXED;
2186 /* Increment offset */
2187 *offset += field->field_size;
2188 return 0;
2189 break;
2190 case LTT_STRING:
2191 field->offset_root = *offset;
2192 field->fixed_root = FIELD_FIXED;
2193 return 1;
2194 break;
2195 case LTT_ARRAY:
2196 g_assert(type->fields->len == 1);
2197 {
2198 LttField *child = &g_array_index(type->fields, LttField, 0);
2199
743e50fd 2200 *offset += ltt_align(*offset, get_alignment(field),
2201 fac->alignment);
f104d082 2202
2203 /* remember offset */
2204 field->offset_root = *offset;
2205 field->array_offset = *offset;
2206 field->fixed_root = FIELD_FIXED;
2207
2208 /* Let the child be variable */
2209 //precompute_fields_offsets(tf, child, offset);
2210
2211 if(field->field_size != 0) {
2212 /* Increment offset */
2213 /* field_size is the array size in bytes */
2214 *offset += field->field_size;
2215 return 0;
2216 } else {
2217 return 1;
2218 }
2219 }
2220 break;
2221 case LTT_SEQUENCE:
2222 g_assert(type->fields->len == 2);
2223 {
2224 LttField *child;
2225 guint ret;
2226
743e50fd 2227 *offset += ltt_align(*offset, get_alignment(field),
2228 fac->alignment);
f104d082 2229
2230 /* remember offset */
2231 field->offset_root = *offset;
2232 field->fixed_root = FIELD_FIXED;
2233
2234 child = &g_array_index(type->fields, LttField, 0);
dd3a6d39 2235 ret = precompute_fields_offsets(fac, child, offset, is_compact);
f104d082 2236 g_assert(ret == 0); /* Seq len cannot have variable len */
2237
2238 child = &g_array_index(type->fields, LttField, 1);
743e50fd 2239 *offset += ltt_align(*offset, get_alignment(child),
2240 fac->alignment);
f104d082 2241 field->array_offset = *offset;
743e50fd 2242 /* Let the child be variable. */
2243 //ret = precompute_fields_offsets(fac, child, offset);
f104d082 2244
2245 /* Cannot precompute fields offsets of sequence members, and has
2246 * variable length. */
2247 return 1;
2248 }
2249 break;
2250 case LTT_STRUCT:
2251 {
2252 LttField *child;
2253 guint i;
2254 gint ret=0;
2255
743e50fd 2256 *offset += ltt_align(*offset, get_alignment(field),
2257 fac->alignment);
f104d082 2258 /* remember offset */
2259 field->offset_root = *offset;
2260 field->fixed_root = FIELD_FIXED;
2261
2262 for(i=0; i< type->fields->len; i++) {
2263 child = &g_array_index(type->fields, LttField, i);
dd3a6d39 2264 ret = precompute_fields_offsets(fac, child, offset, is_compact);
f104d082 2265
2266 if(ret) break;
2267 }
2268 return ret;
2269 }
2270 break;
2271 case LTT_UNION:
2272 {
2273 LttField *child;
2274 guint i;
2275 gint ret=0;
2276
743e50fd 2277 *offset += ltt_align(*offset, get_alignment(field),
2278 fac->alignment);
f104d082 2279 /* remember offset */
2280 field->offset_root = *offset;
2281 field->fixed_root = FIELD_FIXED;
2282
2283 for(i=0; i< type->fields->len; i++) {
2284 *offset = field->offset_root;
2285 child = &g_array_index(type->fields, LttField, i);
dd3a6d39 2286 ret = precompute_fields_offsets(fac, child, offset, is_compact);
f104d082 2287
2288 if(ret) break;
2289 }
2290 *offset = field->offset_root + field->field_size;
2291 return ret;
2292 }
2293
2294 break;
2295 case LTT_NONE:
2296 default:
2297 g_error("precompute_fields_offsets : unknown type");
2298 return 1;
2299 }
2300
2301}
2302
3c165eaf 2303#endif //0
f104d082 2304
3c165eaf 2305#if 0
f104d082 2306/*****************************************************************************
2307 *Function name
2308 * precompute_offsets : set the precomputable offset of an event type
2309 *Input params
2310 * tf : tracefile
2311 * event : event type
2312 *
2313 ****************************************************************************/
743e50fd 2314void precompute_offsets(LttFacility *fac, LttEventType *event)
f104d082 2315{
2316 guint i;
2317 off_t offset = 0;
2318 gint ret;
2319
2320 /* First, compute the size of fixed size fields. Will determine size for
2321 * arrays, struct and unions, which is not done by the parser */
2322 for(i=0; i<event->fields->len; i++) {
2323 LttField *field = &g_array_index(event->fields, LttField, i);
743e50fd 2324 field_compute_static_size(fac, field);
f104d082 2325 }
2326
2327 /* Precompute all known offsets */
2328 for(i=0; i<event->fields->len; i++) {
2329 LttField *field = &g_array_index(event->fields, LttField, i);
dd3a6d39 2330 if(event->has_compact_data && i == 0)
2331 ret = precompute_fields_offsets(fac, field, &offset, 1);
2332 else
2333 ret = precompute_fields_offsets(fac, field, &offset, 0);
f104d082 2334 if(ret) break;
2335 }
2336}
3c165eaf 2337#endif //0
f104d082 2338
e4eced0f 2339
bbf28e50 2340
3aee1200 2341/*****************************************************************************
2342 *Function name
2343 * preset_field_type_size : set the fixed sizes of the field type
2344 *Input params
2345 * tf : tracefile
2346 * event_type : event type
2347 * offset_root : offset from the root
2348 * offset_parent : offset from the parent
2349 * fixed_root : Do we know a fixed offset to the root ?
2350 * fixed_parent : Do we know a fixed offset to the parent ?
2351 * field : field
2352 ****************************************************************************/
f104d082 2353
2354
2355
2356// preset the fixed size offsets. Calculate them just like genevent-new : an
2357// increment of a *to value that represents the offset from the start of the
2358// event data.
2359// The preset information is : offsets up to (and including) the first element
2360// of variable size. All subsequent fields must be flagged "VARIABLE OFFSET".
2361#if 0
3aee1200 2362void preset_field_type_size(LttTracefile *tf, LttEventType *event_type,
2363 off_t offset_root, off_t offset_parent,
2364 enum field_status *fixed_root, enum field_status *fixed_parent,
2365 LttField *field)
2366{
2367 enum field_status local_fixed_root, local_fixed_parent;
2368 guint i;
2369 LttType *type;
dfb73233 2370
3aee1200 2371 g_assert(field->fixed_root == FIELD_UNKNOWN);
2372 g_assert(field->fixed_parent == FIELD_UNKNOWN);
2373 g_assert(field->fixed_size == FIELD_UNKNOWN);
dfb73233 2374
3aee1200 2375 type = field->field_type;
2376
2377 field->fixed_root = *fixed_root;
2378 if(field->fixed_root == FIELD_FIXED)
2379 field->offset_root = offset_root;
2380 else
2381 field->offset_root = 0;
2382
2383 field->fixed_parent = *fixed_parent;
2384 if(field->fixed_parent == FIELD_FIXED)
2385 field->offset_parent = offset_parent;
2386 else
2387 field->offset_parent = 0;
2388
2389 size_t current_root_offset;
2390 size_t current_offset;
2391 enum field_status current_child_status, final_child_status;
2392 size_t max_size;
2393
2394 switch(type->type_class) {
83e160f2 2395 case LTT_INT_FIXED:
2396 case LTT_UINT_FIXED:
2397 case LTT_CHAR:
2398 case LTT_UCHAR:
2399 case LTT_SHORT:
2400 case LTT_USHORT:
3aee1200 2401 case LTT_INT:
2402 case LTT_UINT:
2403 case LTT_FLOAT:
2404 case LTT_ENUM:
2405 field->field_size = ltt_type_size(tf->trace, type);
2406 field->fixed_size = FIELD_FIXED;
2407 break;
2408 case LTT_POINTER:
2409 field->field_size = (off_t)event_type->facility->pointer_size;
2410 field->fixed_size = FIELD_FIXED;
2411 break;
2412 case LTT_LONG:
2413 case LTT_ULONG:
cb03932a 2414 field->field_size = (off_t)event_type->facility->long_size;
3aee1200 2415 field->fixed_size = FIELD_FIXED;
2416 break;
2417 case LTT_SIZE_T:
2418 case LTT_SSIZE_T:
2419 case LTT_OFF_T:
2420 field->field_size = (off_t)event_type->facility->size_t_size;
2421 field->fixed_size = FIELD_FIXED;
2422 break;
2423 case LTT_SEQUENCE:
2424 local_fixed_root = FIELD_VARIABLE;
2425 local_fixed_parent = FIELD_VARIABLE;
2426 preset_field_type_size(tf, event_type,
2427 0, 0,
2428 &local_fixed_root, &local_fixed_parent,
2429 field->child[0]);
2430 field->fixed_size = FIELD_VARIABLE;
2431 field->field_size = 0;
27304273 2432 *fixed_root = FIELD_VARIABLE;
2433 *fixed_parent = FIELD_VARIABLE;
3aee1200 2434 break;
2435 case LTT_STRING:
2436 field->fixed_size = FIELD_VARIABLE;
2437 field->field_size = 0;
27304273 2438 *fixed_root = FIELD_VARIABLE;
2439 *fixed_parent = FIELD_VARIABLE;
3aee1200 2440 break;
2441 case LTT_ARRAY:
2442 local_fixed_root = FIELD_VARIABLE;
2443 local_fixed_parent = FIELD_VARIABLE;
2444 preset_field_type_size(tf, event_type,
2445 0, 0,
2446 &local_fixed_root, &local_fixed_parent,
2447 field->child[0]);
2448 field->fixed_size = field->child[0]->fixed_size;
27304273 2449 if(field->fixed_size == FIELD_FIXED) {
3aee1200 2450 field->field_size = type->element_number * field->child[0]->field_size;
27304273 2451 } else {
3aee1200 2452 field->field_size = 0;
27304273 2453 *fixed_root = FIELD_VARIABLE;
2454 *fixed_parent = FIELD_VARIABLE;
2455 }
3aee1200 2456 break;
2457 case LTT_STRUCT:
2458 current_root_offset = field->offset_root;
2459 current_offset = 0;
2460 current_child_status = FIELD_FIXED;
2461 for(i=0;i<type->element_number;i++) {
2462 preset_field_type_size(tf, event_type,
2463 current_root_offset, current_offset,
2464 fixed_root, &current_child_status,
2465 field->child[i]);
2466 if(current_child_status == FIELD_FIXED) {
2467 current_root_offset += field->child[i]->field_size;
2468 current_offset += field->child[i]->field_size;
2469 } else {
2470 current_root_offset = 0;
2471 current_offset = 0;
2472 }
2473 }
2474 if(current_child_status != FIELD_FIXED) {
2475 *fixed_parent = current_child_status;
2476 field->field_size = 0;
2477 field->fixed_size = current_child_status;
2478 } else {
2479 field->field_size = current_offset;
2480 field->fixed_size = FIELD_FIXED;
2481 }
2482 break;
2483 case LTT_UNION:
2484 current_root_offset = field->offset_root;
2485 current_offset = 0;
2486 max_size = 0;
2487 final_child_status = FIELD_FIXED;
2488 for(i=0;i<type->element_number;i++) {
2489 enum field_status current_root_child_status = FIELD_FIXED;
2490 enum field_status current_child_status = FIELD_FIXED;
2491 preset_field_type_size(tf, event_type,
2492 current_root_offset, current_offset,
2493 &current_root_child_status, &current_child_status,
2494 field->child[i]);
2495 if(current_child_status != FIELD_FIXED)
2496 final_child_status = current_child_status;
2497 else
2498 max_size = max(max_size, field->child[i]->field_size);
2499 }
2500 if(final_child_status != FIELD_FIXED) {
62e4e7bf 2501 g_error("LTTV does not support variable size fields in unions.");
2502 /* This will stop the application. */
3aee1200 2503 *fixed_root = final_child_status;
2504 *fixed_parent = final_child_status;
2505 field->field_size = 0;
2506 field->fixed_size = current_child_status;
2507 } else {
2508 field->field_size = max_size;
2509 field->fixed_size = FIELD_FIXED;
2510 }
2511 break;
83e160f2 2512 case LTT_NONE:
2513 g_error("unexpected type NONE");
2514 break;
dfb73233 2515 }
2516
6cd62ccf 2517}
f104d082 2518#endif //0
3aee1200 2519
77175651 2520/*****************************************************************************
2521 *Function name
2522 * check_fields_compatibility : Check for compatibility between two fields :
2523 * do they use the same inner structure ?
2524 *Input params
2525 * event_type1 : event type
2526 * event_type2 : event type
2527 * field1 : field
2528 * field2 : field
2529 *Returns : 0 if identical
2530 * 1 if not.
2531 ****************************************************************************/
f104d082 2532// this function checks for equality of field types. Therefore, it does not use
2533// per se offsets. For instance, an aligned version of a structure is
2534// compatible with an unaligned version of the same structure.
3c165eaf 2535#if 0
f104d082 2536gint check_fields_compatibility(LttEventType *event_type1,
2537 LttEventType *event_type2,
2538 LttField *field1, LttField *field2)
2539{
2540 guint different = 0;
2312de30 2541 LttType *type1;
2542 LttType *type2;
f104d082 2543
2544 if(field1 == NULL) {
2545 if(field2 == NULL) goto end;
2546 else {
2547 different = 1;
2548 goto end;
2549 }
2550 } else if(field2 == NULL) {
2551 different = 1;
2552 goto end;
2553 }
2554
2312de30 2555 type1 = &field1->field_type;
2556 type2 = &field2->field_type;
f104d082 2557
2558 if(type1->type_class != type2->type_class) {
2559 different = 1;
2560 goto end;
2561 }
62e4e7bf 2562 if(type1->network != type2->network) {
2563 different = 1;
2564 goto end;
2565 }
f104d082 2566
2567 switch(type1->type_class) {
2568 case LTT_INT_FIXED:
2569 case LTT_UINT_FIXED:
2570 case LTT_POINTER:
2571 case LTT_CHAR:
2572 case LTT_UCHAR:
2573 case LTT_SHORT:
2574 case LTT_USHORT:
2575 case LTT_INT:
2576 case LTT_UINT:
2577 case LTT_LONG:
2578 case LTT_ULONG:
2579 case LTT_SIZE_T:
2580 case LTT_SSIZE_T:
2581 case LTT_OFF_T:
2582 case LTT_FLOAT:
2583 case LTT_ENUM:
2584 if(field1->field_size != field2->field_size)
2585 different = 1;
2586 break;
2587 case LTT_STRING:
2588 break;
2589 case LTT_ARRAY:
2590 {
2591 LttField *child1 = &g_array_index(type1->fields, LttField, 0);
2592 LttField *child2 = &g_array_index(type2->fields, LttField, 0);
2593
2594 if(type1->size != type2->size)
2595 different = 1;
2596 if(check_fields_compatibility(event_type1, event_type2, child1, child2))
2597 different = 1;
2598 }
2599 break;
2600 case LTT_SEQUENCE:
2601 {
2602 LttField *child1 = &g_array_index(type1->fields, LttField, 1);
2603 LttField *child2 = &g_array_index(type2->fields, LttField, 1);
2604
2605 if(check_fields_compatibility(event_type1, event_type2, child1, child2))
2606 different = 1;
2607 }
2608 break;
2609 case LTT_STRUCT:
2610 case LTT_UNION:
2611 {
2612 LttField *child;
2613 guint i;
2614
2615 if(type1->fields->len != type2->fields->len) {
2616 different = 1;
2617 goto end;
2618 }
2619
2620 for(i=0; i< type1->fields->len; i++) {
2312de30 2621 LttField *child1;
2622 LttField *child2;
f104d082 2623 child1 = &g_array_index(type1->fields, LttField, i);
2624 child2 = &g_array_index(type2->fields, LttField, i);
2625 different = check_fields_compatibility(event_type1,
2626 event_type2, child1, child2);
2627
2628 if(different) break;
2629 }
2630 }
2631 break;
2632 case LTT_NONE:
2633 default:
dd3a6d39 2634 g_error("check_fields_compatibility : unknown type");
f104d082 2635 }
2636
2637end:
2638 return different;
2639}
3c165eaf 2640#endif //0
f104d082 2641
2642#if 0
77175651 2643gint check_fields_compatibility(LttEventType *event_type1,
2644 LttEventType *event_type2,
2645 LttField *field1, LttField *field2)
2646{
2647 guint different = 0;
77175651 2648 guint i;
2649 LttType *type1;
2650 LttType *type2;
2651
2652 if(field1 == NULL) {
2653 if(field2 == NULL) goto end;
2654 else {
2655 different = 1;
2656 goto end;
2657 }
2658 } else if(field2 == NULL) {
2659 different = 1;
2660 goto end;
2661 }
2662
2663 g_assert(field1->fixed_root != FIELD_UNKNOWN);
2664 g_assert(field2->fixed_root != FIELD_UNKNOWN);
2665 g_assert(field1->fixed_parent != FIELD_UNKNOWN);
2666 g_assert(field2->fixed_parent != FIELD_UNKNOWN);
2667 g_assert(field1->fixed_size != FIELD_UNKNOWN);
2668 g_assert(field2->fixed_size != FIELD_UNKNOWN);
2669
2670 type1 = field1->field_type;
2671 type2 = field2->field_type;
2672
77175651 2673 if(type1->type_class != type2->type_class) {
2674 different = 1;
2675 goto end;
2676 }
2677 if(type1->element_name != type2->element_name) {
2678 different = 1;
2679 goto end;
2680 }
2681
2682 switch(type1->type_class) {
83e160f2 2683 case LTT_INT_FIXED:
2684 case LTT_UINT_FIXED:
2685 case LTT_POINTER:
2686 case LTT_CHAR:
2687 case LTT_UCHAR:
2688 case LTT_SHORT:
2689 case LTT_USHORT:
77175651 2690 case LTT_INT:
2691 case LTT_UINT:
2692 case LTT_FLOAT:
2693 case LTT_POINTER:
2694 case LTT_LONG:
2695 case LTT_ULONG:
2696 case LTT_SIZE_T:
2697 case LTT_SSIZE_T:
2698 case LTT_OFF_T:
2699 if(field1->field_size != field2->field_size) {
2700 different = 1;
2701 goto end;
2702 }
2703 break;
2704 case LTT_ENUM:
2705 if(type1->element_number != type2->element_number) {
2706 different = 1;
2707 goto end;
2708 }
2709 for(i=0;i<type1->element_number;i++) {
2710 if(type1->enum_strings[i] != type2->enum_strings[i]) {
2711 different = 1;
2712 goto end;
2713 }
2714 }
2715 break;
2716 case LTT_SEQUENCE:
2717 /* Two elements : size and child */
2718 g_assert(type1->element_number != type2->element_number);
2719 for(i=0;i<type1->element_number;i++) {
2720 if(check_fields_compatibility(event_type1, event_type2,
2721 field1->child[0], field2->child[0])) {
2722 different = 1;
2723 goto end;
2724 }
2725 }
2726 break;
2727 case LTT_STRING:
2728 break;
2729 case LTT_ARRAY:
2730 if(field1->field_size != field2->field_size) {
2731 different = 1;
2732 goto end;
2733 }
2734 /* Two elements : size and child */
2735 g_assert(type1->element_number != type2->element_number);
2736 for(i=0;i<type1->element_number;i++) {
2737 if(check_fields_compatibility(event_type1, event_type2,
2738 field1->child[0], field2->child[0])) {
2739 different = 1;
2740 goto end;
2741 }
2742 }
2743 break;
2744 case LTT_STRUCT:
2745 case LTT_UNION:
2746 if(type1->element_number != type2->element_number) {
2747 different = 1;
2748 break;
2749 }
2750 for(i=0;i<type1->element_number;i++) {
2751 if(check_fields_compatibility(event_type1, event_type2,
2752 field1->child[0], field2->child[0])) {
2753 different = 1;
2754 goto end;
2755 }
2756 }
2757 break;
2758 }
2759end:
2760 return different;
2761}
3aee1200 2762#endif //0
6cd62ccf 2763
f104d082 2764
6cd62ccf 2765/*****************************************************************************
2766 *Function name
eed2ef37 2767 * ltt_get_int : get an integer number
6cd62ccf 2768 *Input params
3aee1200 2769 * reverse_byte_order: must we reverse the byte order ?
6cd62ccf 2770 * size : the size of the integer
3aee1200 2771 * ptr : the data pointer
6cd62ccf 2772 *Return value
cf74a6f1 2773 * gint64 : a 64 bits integer
6cd62ccf 2774 ****************************************************************************/
2775
eed2ef37 2776gint64 ltt_get_int(gboolean reverse_byte_order, gint size, void *data)
6cd62ccf 2777{
3aee1200 2778 gint64 val;
cf74a6f1 2779
2780 switch(size) {
3aee1200 2781 case 1: val = *((gint8*)data); break;
2782 case 2: val = ltt_get_int16(reverse_byte_order, data); break;
2783 case 4: val = ltt_get_int32(reverse_byte_order, data); break;
2784 case 8: val = ltt_get_int64(reverse_byte_order, data); break;
2785 default: val = ltt_get_int64(reverse_byte_order, data);
2786 g_critical("get_int : integer size %d unknown", size);
cf74a6f1 2787 break;
2788 }
2789
3aee1200 2790 return val;
6cd62ccf 2791}
3aee1200 2792
6cd62ccf 2793/*****************************************************************************
2794 *Function name
eed2ef37 2795 * ltt_get_uint : get an unsigned integer number
6cd62ccf 2796 *Input params
3aee1200 2797 * reverse_byte_order: must we reverse the byte order ?
2798 * size : the size of the integer
2799 * ptr : the data pointer
2800 *Return value
2801 * guint64 : a 64 bits unsigned integer
6cd62ccf 2802 ****************************************************************************/
2803
eed2ef37 2804guint64 ltt_get_uint(gboolean reverse_byte_order, gint size, void *data)
6cd62ccf 2805{
3aee1200 2806 guint64 val;
2807
2808 switch(size) {
2809 case 1: val = *((gint8*)data); break;
2810 case 2: val = ltt_get_uint16(reverse_byte_order, data); break;
2811 case 4: val = ltt_get_uint32(reverse_byte_order, data); break;
2812 case 8: val = ltt_get_uint64(reverse_byte_order, data); break;
2813 default: val = ltt_get_uint64(reverse_byte_order, data);
2814 g_critical("get_uint : unsigned integer size %d unknown",
2815 size);
2816 break;
2817 }
2818
2819 return val;
6cd62ccf 2820}
3aee1200 2821
2822
a5dcde2f 2823/* get the node name of the system */
2824
2825char * ltt_trace_system_description_node_name (LttSystemDescription * s)
2826{
2827 return s->node_name;
2828}
2829
2830
2831/* get the domain name of the system */
2832
2833char * ltt_trace_system_description_domain_name (LttSystemDescription * s)
2834{
2835 return s->domain_name;
2836}
2837
2838
2839/* get the description of the system */
2840
2841char * ltt_trace_system_description_description (LttSystemDescription * s)
2842{
2843 return s->description;
2844}
2845
2846
bf33dd50 2847/* get the NTP corrected start time of the trace */
7bd563ec 2848LttTime ltt_trace_start_time(LttTrace *t)
a5dcde2f 2849{
7bd563ec 2850 return t->start_time;
a5dcde2f 2851}
2852
bf33dd50 2853/* get the monotonic start time of the trace */
2854LttTime ltt_trace_start_time_monotonic(LttTrace *t)
2855{
2856 return t->start_time_from_tsc;
2857}
2858
8655430b 2859static LttTracefile *ltt_tracefile_new()
18206708 2860{
afd57a3c 2861 LttTracefile *tf;
2862 tf = g_new(LttTracefile, 1);
2863 tf->event.tracefile = tf;
2864 return tf;
18206708 2865}
2866
8655430b 2867static void ltt_tracefile_destroy(LttTracefile *tf)
18206708 2868{
2869 g_free(tf);
2870}
2871
8655430b 2872static void ltt_tracefile_copy(LttTracefile *dest, const LttTracefile *src)
18206708 2873{
2874 *dest = *src;
2875}
2876
3aee1200 2877/* Before library loading... */
2878
8655430b 2879static __attribute__((constructor)) void init(void)
3aee1200 2880{
2881 LTT_FACILITY_NAME_HEARTBEAT = g_quark_from_string("heartbeat");
2882 LTT_EVENT_NAME_HEARTBEAT = g_quark_from_string("heartbeat");
d1bb700c 2883 LTT_EVENT_NAME_HEARTBEAT_FULL = g_quark_from_string("heartbeat_full");
3aee1200 2884
3865ea09 2885 LTT_TRACEFILE_NAME_FACILITIES = g_quark_from_string("/control/facilities");
3aee1200 2886}
This page took 0.214006 seconds and 4 git commands to generate.