likely/unlikely branch prediction
[lttv.git] / ltt / branches / poly / ltt / tracefile.c
CommitLineData
449cb9d7 1/* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2003-2004 Xiangxiu Yang
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License Version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
16 * MA 02111-1307, USA.
17 */
18
6cd62ccf 19#include <stdio.h>
20#include <fcntl.h>
8d1e6362 21#include <string.h>
22#include <dirent.h>
6cd62ccf 23#include <sys/stat.h>
24#include <sys/types.h>
8d1e6362 25#include <errno.h>
26#include <unistd.h>
6cd62ccf 27
ef35d837 28// For realpath
29#include <limits.h>
30#include <stdlib.h>
31
32
6cd62ccf 33#include "parser.h"
a5dcde2f 34#include <ltt/ltt.h>
35#include "ltt-private.h"
963b5f2d 36#include <ltt/trace.h>
a5dcde2f 37#include <ltt/facility.h>
c02ea99f 38#include <ltt/event.h>
8d1e6362 39#include <ltt/type.h>
6cd62ccf 40
963b5f2d 41#define DIR_NAME_SIZE 256
51b5991e 42#define __UNUSED__ __attribute__((__unused__))
6cd62ccf 43
8959a0c8 44
45/* obtain the time of an event */
46
47static inline LttTime getEventTime(LttTracefile * tf);
48
49
6cd62ccf 50/* set the offset of the fields belonging to the event,
51 need the information of the archecture */
40331ba8 52void setFieldsOffset(LttTracefile *tf,LttEventType *evT,void *evD,LttTrace *t);
6cd62ccf 53
54/* get the size of the field type according to the archtecture's
55 size and endian type(info of the archecture) */
40331ba8 56int getFieldtypeSize(LttTracefile * tf, LttEventType * evT, int offsetRoot,
57 int offsetParent, LttField *fld, void *evD, LttTrace* t);
6cd62ccf 58
59/* read a fixed size or a block information from the file (fd) */
60int readFile(int fd, void * buf, size_t size, char * mesg);
963b5f2d 61int readBlock(LttTracefile * tf, int whichBlock);
6cd62ccf 62
63/* calculate cycles per nsec for current block */
963b5f2d 64void getCyclePerNsec(LttTracefile * t);
6cd62ccf 65
66/* reinitialize the info of the block which is already in the buffer */
963b5f2d 67void updateTracefile(LttTracefile * tf);
6cd62ccf 68
69/* go to the next event */
963b5f2d 70int skipEvent(LttTracefile * t);
6cd62ccf 71
6cd62ccf 72
43da6a59 73/* Functions to parse system.xml file (using glib xml parser) */
51b5991e 74static void parser_start_element (GMarkupParseContext __UNUSED__ *context,
43da6a59 75 const gchar *element_name,
76 const gchar **attribute_names,
77 const gchar **attribute_values,
78 gpointer user_data,
79 GError **error)
80{
81 int i=0;
82 LttSystemDescription* des = (LttSystemDescription* )user_data;
83 if(strcmp("system", element_name)){
2a74fbf4 84 *error = g_error_new(G_MARKUP_ERROR,
85 G_LOG_LEVEL_WARNING,
86 "This is not system.xml file");
87 return;
43da6a59 88 }
89
90 while(attribute_names[i]){
91 if(strcmp("node_name", attribute_names[i])==0){
92 des->node_name = g_strdup(attribute_values[i]);
93 }else if(strcmp("domainname", attribute_names[i])==0){
94 des->domain_name = g_strdup(attribute_values[i]);
95 }else if(strcmp("cpu", attribute_names[i])==0){
96 des->nb_cpu = atoi(attribute_values[i]);
97 }else if(strcmp("arch_size", attribute_names[i])==0){
98 if(strcmp(attribute_values[i],"LP32") == 0) des->size = LTT_LP32;
99 else if(strcmp(attribute_values[i],"ILP32") == 0) des->size = LTT_ILP32;
100 else if(strcmp(attribute_values[i],"LP64") == 0) des->size = LTT_LP64;
101 else if(strcmp(attribute_values[i],"ILP64") == 0) des->size = LTT_ILP64;
102 else if(strcmp(attribute_values[i],"UNKNOWN") == 0) des->size = LTT_UNKNOWN;
103 }else if(strcmp("endian", attribute_names[i])==0){
104 if(strcmp(attribute_values[i],"LITTLE_ENDIAN") == 0)
105 des->endian = LTT_LITTLE_ENDIAN;
106 else if(strcmp(attribute_values[i],"BIG_ENDIAN") == 0)
107 des->endian = LTT_BIG_ENDIAN;
108 }else if(strcmp("kernel_name", attribute_names[i])==0){
109 des->kernel_name = g_strdup(attribute_values[i]);
110 }else if(strcmp("kernel_release", attribute_names[i])==0){
111 des->kernel_release = g_strdup(attribute_values[i]);
112 }else if(strcmp("kernel_version", attribute_names[i])==0){
113 des->kernel_version = g_strdup(attribute_values[i]);
114 }else if(strcmp("machine", attribute_names[i])==0){
115 des->machine = g_strdup(attribute_values[i]);
116 }else if(strcmp("processor", attribute_names[i])==0){
117 des->processor = g_strdup(attribute_values[i]);
118 }else if(strcmp("hardware_platform", attribute_names[i])==0){
119 des->hardware_platform = g_strdup(attribute_values[i]);
120 }else if(strcmp("operating_system", attribute_names[i])==0){
121 des->operating_system = g_strdup(attribute_values[i]);
122 }else if(strcmp("ltt_major_version", attribute_names[i])==0){
123 des->ltt_major_version = atoi(attribute_values[i]);
124 }else if(strcmp("ltt_minor_version", attribute_names[i])==0){
125 des->ltt_minor_version = atoi(attribute_values[i]);
126 }else if(strcmp("ltt_block_size", attribute_names[i])==0){
127 des->ltt_block_size = atoi(attribute_values[i]);
128 }else{
2a74fbf4 129 *error = g_error_new(G_MARKUP_ERROR,
130 G_LOG_LEVEL_WARNING,
131 "Not a valid attribute");
132 return;
43da6a59 133 }
134 i++;
135 }
136}
137
51b5991e 138static void parser_characters (GMarkupParseContext __UNUSED__ *context,
43da6a59 139 const gchar *text,
51b5991e 140 gsize __UNUSED__ text_len,
43da6a59 141 gpointer user_data,
51b5991e 142 GError __UNUSED__ **error)
43da6a59 143{
144 LttSystemDescription* des = (LttSystemDescription* )user_data;
145 des->description = g_strdup(text);
146}
147
148
6cd62ccf 149/*****************************************************************************
150 *Function name
963b5f2d 151 * ltt_tracefile_open : open a trace file, construct a LttTracefile
6cd62ccf 152 *Input params
963b5f2d 153 * t : the trace containing the tracefile
154 * fileName : path name of the trace file
6cd62ccf 155 *Return value
963b5f2d 156 * : a pointer to a tracefile
6cd62ccf 157 ****************************************************************************/
158
963b5f2d 159LttTracefile* ltt_tracefile_open(LttTrace * t, char * fileName)
6cd62ccf 160{
963b5f2d 161 LttTracefile * tf;
162 struct stat lTDFStat; /* Trace data file status */
6cd62ccf 163
963b5f2d 164 tf = g_new(LttTracefile, 1);
6cd62ccf 165
166 //open the file
963b5f2d 167 tf->name = g_strdup(fileName);
168 tf->trace = t;
6cd62ccf 169 tf->fd = open(fileName, O_RDONLY, 0);
170 if(tf->fd < 0){
2a74fbf4 171 g_warning("Unable to open input data file %s\n", fileName);
172 g_free(tf->name);
173 g_free(tf);
174 return NULL;
6cd62ccf 175 }
176
177 // Get the file's status
178 if(fstat(tf->fd, &lTDFStat) < 0){
2a74fbf4 179 g_warning("Unable to get the status of the input data file %s\n", fileName);
180 g_free(tf->name);
181 close(tf->fd);
182 g_free(tf);
183 return NULL;
6cd62ccf 184 }
185
186 // Is the file large enough to contain a trace
8d1e6362 187 if(lTDFStat.st_size < (off_t)(sizeof(BlockStart) + EVENT_HEADER_SIZE)){
8710c6c7 188 g_print("The input data file %s does not contain a trace\n", fileName);
663bd9ff 189 g_free(tf->name);
190 close(tf->fd);
191 g_free(tf);
8710c6c7 192 return NULL;
6cd62ccf 193 }
194
195 //store the size of the file
196 tf->file_size = lTDFStat.st_size;
963b5f2d 197 tf->block_size = t->system_description->ltt_block_size;
198 tf->block_number = tf->file_size / tf->block_size;
199 tf->which_block = 0;
6cd62ccf 200
201 //allocate memory to contain the info of a block
963b5f2d 202 tf->buffer = (void *) g_new(char, t->system_description->ltt_block_size);
6cd62ccf 203
963b5f2d 204 //read the first block
205 if(readBlock(tf,1)) exit(1);
6cd62ccf 206
207 return tf;
208}
209
6cd62ccf 210
211/*****************************************************************************
963b5f2d 212 *Open control and per cpu tracefiles
6cd62ccf 213 ****************************************************************************/
214
963b5f2d 215void ltt_tracefile_open_cpu(LttTrace *t, char * tracefile_name)
6cd62ccf 216{
963b5f2d 217 LttTracefile * tf;
218 tf = ltt_tracefile_open(t,tracefile_name);
4a6c8e36 219 if(!tf) return;
963b5f2d 220 t->per_cpu_tracefile_number++;
221 g_ptr_array_add(t->per_cpu_tracefiles, tf);
6cd62ccf 222}
223
2a74fbf4 224gint ltt_tracefile_open_control(LttTrace *t, char * control_name)
6cd62ccf 225{
963b5f2d 226 LttTracefile * tf;
c02ea99f 227 LttEvent ev;
963b5f2d 228 LttFacility * f;
963b5f2d 229 void * pos;
230 FacilityLoad fLoad;
8d1e6362 231 unsigned int i;
963b5f2d 232
233 tf = ltt_tracefile_open(t,control_name);
2a74fbf4 234 if(!tf) {
235 g_warning("ltt_tracefile_open_control : bad file descriptor");
236 return -1;
237 }
963b5f2d 238 t->control_tracefile_number++;
239 g_ptr_array_add(t->control_tracefiles,tf);
240
241 //parse facilities tracefile to get base_id
542ceddd 242 if(strcmp(&control_name[strlen(control_name)-10],"facilities") ==0){
963b5f2d 243 while(1){
c02ea99f 244 if(!ltt_tracefile_read(tf,&ev)) return 0; // end of file
40331ba8 245
c02ea99f 246 if(ev.event_id == TRACE_FACILITY_LOAD){
247 pos = ev.data;
963b5f2d 248 fLoad.name = (char*)pos;
47a166fc 249 fLoad.checksum = *(LttChecksum*)(pos + strlen(fLoad.name));
cbd41522 250 fLoad.base_code = *(guint32 *)(pos + strlen(fLoad.name) + sizeof(LttChecksum));
963b5f2d 251
252 for(i=0;i<t->facility_number;i++){
253 f = (LttFacility*)g_ptr_array_index(t->facilities,i);
254 if(strcmp(f->name,fLoad.name)==0 && fLoad.checksum==f->checksum){
255 f->base_id = fLoad.base_code;
256 break;
257 }
258 }
2a74fbf4 259 if(i==t->facility_number) {
8d1e6362 260 g_warning("Facility: %s, checksum: %u is not found",
261 fLoad.name,(unsigned int)fLoad.checksum);
2a74fbf4 262 return -1;
263 }
c02ea99f 264 }else if(ev.event_id == TRACE_BLOCK_START){
40331ba8 265 continue;
c02ea99f 266 }else if(ev.event_id == TRACE_BLOCK_END){
40331ba8 267 break;
2a74fbf4 268 }else {
8d1e6362 269 g_warning("Not valid facilities trace file");
2a74fbf4 270 return -1;
271 }
963b5f2d 272 }
273 }
2a74fbf4 274 return 0;
6cd62ccf 275}
276
277/*****************************************************************************
278 *Function name
963b5f2d 279 * ltt_tracefile_close: close a trace file,
6cd62ccf 280 *Input params
963b5f2d 281 * t : tracefile which will be closed
6cd62ccf 282 ****************************************************************************/
283
963b5f2d 284void ltt_tracefile_close(LttTracefile *t)
6cd62ccf 285{
963b5f2d 286 g_free(t->name);
287 g_free(t->buffer);
663bd9ff 288 close(t->fd);
963b5f2d 289 g_free(t);
290}
6cd62ccf 291
6cd62ccf 292
963b5f2d 293/*****************************************************************************
294 *Get system information
295 ****************************************************************************/
2a74fbf4 296gint getSystemInfo(LttSystemDescription* des, char * pathname)
963b5f2d 297{
298 FILE * fp;
963b5f2d 299 char buf[DIR_NAME_SIZE];
43da6a59 300
301 GMarkupParseContext * context;
2a74fbf4 302 GError * error = NULL;
43da6a59 303 GMarkupParser markup_parser =
304 {
305 parser_start_element,
8d1e6362 306 NULL,
43da6a59 307 parser_characters,
308 NULL, /* passthrough */
309 NULL /* error */
310 };
963b5f2d 311
312 fp = fopen(pathname,"r");
313 if(!fp){
2a74fbf4 314 g_warning("Can not open file : %s\n", pathname);
315 return -1;
6cd62ccf 316 }
963b5f2d 317
43da6a59 318 context = g_markup_parse_context_new(&markup_parser, 0, des,NULL);
6cd62ccf 319
43da6a59 320 while(fgets(buf,DIR_NAME_SIZE, fp) != NULL){
321 if(!g_markup_parse_context_parse(context, buf, DIR_NAME_SIZE, &error)){
2a74fbf4 322 if(error != NULL) {
323 g_warning("Can not parse xml file: \n%s\n", error->message);
324 g_error_free(error);
325 }
326 g_markup_parse_context_free(context);
327 fclose(fp);
328 return -1;
43da6a59 329 }
963b5f2d 330 }
2a74fbf4 331 g_markup_parse_context_free(context);
963b5f2d 332 fclose(fp);
2a74fbf4 333 return 0;
6cd62ccf 334}
335
336/*****************************************************************************
963b5f2d 337 *The following functions get facility/tracefile information
6cd62ccf 338 ****************************************************************************/
339
2a74fbf4 340gint getFacilityInfo(LttTrace *t, char* eventdefs)
6cd62ccf 341{
963b5f2d 342 DIR * dir;
343 struct dirent *entry;
344 char * ptr;
8d1e6362 345 unsigned int i,j;
963b5f2d 346 LttFacility * f;
347 LttEventType * et;
e8bb1a73 348 char name[DIR_NAME_SIZE];
963b5f2d 349
350 dir = opendir(eventdefs);
2a74fbf4 351 if(!dir) {
352 g_warning("Can not open directory: %s\n", eventdefs);
353 return -1;
354 }
963b5f2d 355
356 while((entry = readdir(dir)) != NULL){
357 ptr = &entry->d_name[strlen(entry->d_name)-4];
358 if(strcmp(ptr,".xml") != 0) continue;
e8bb1a73 359 strcpy(name,eventdefs);
360 strcat(name,entry->d_name);
361 ltt_facility_open(t,name);
963b5f2d 362 }
363 closedir(dir);
364
963b5f2d 365 for(j=0;j<t->facility_number;j++){
8710c6c7 366 f = (LttFacility*)g_ptr_array_index(t->facilities, j);
963b5f2d 367 for(i=0; i<f->event_number; i++){
368 et = f->events[i];
40331ba8 369 setFieldsOffset(NULL, et, NULL, t);
963b5f2d 370 }
371 }
2a74fbf4 372 return 0;
963b5f2d 373}
6cd62ccf 374
2a74fbf4 375gint getControlFileInfo(LttTrace *t, char* control)
6cd62ccf 376{
963b5f2d 377 DIR * dir;
378 struct dirent *entry;
e8bb1a73 379 char name[DIR_NAME_SIZE];
963b5f2d 380
381 dir = opendir(control);
2a74fbf4 382 if(!dir) {
383 g_warning("Can not open directory: %s\n", control);
384 return -1;
385 }
963b5f2d 386
387 while((entry = readdir(dir)) != NULL){
8710c6c7 388 if(strcmp(entry->d_name,"facilities") != 0 &&
389 strcmp(entry->d_name,"interrupts") != 0 &&
963b5f2d 390 strcmp(entry->d_name,"processes") != 0) continue;
391
e8bb1a73 392 strcpy(name,control);
393 strcat(name,entry->d_name);
2a74fbf4 394 if(ltt_tracefile_open_control(t,name))
395 return -1;
963b5f2d 396 }
397 closedir(dir);
2a74fbf4 398 return 0;
6cd62ccf 399}
400
2a74fbf4 401gint getCpuFileInfo(LttTrace *t, char* cpu)
6cd62ccf 402{
963b5f2d 403 DIR * dir;
404 struct dirent *entry;
e8bb1a73 405 char name[DIR_NAME_SIZE];
963b5f2d 406
407 dir = opendir(cpu);
2a74fbf4 408 if(!dir) {
409 g_warning("Can not open directory: %s\n", cpu);
410 return -1;
411 }
963b5f2d 412
413 while((entry = readdir(dir)) != NULL){
8710c6c7 414 if(strcmp(entry->d_name,".") != 0 &&
a614481a 415 strcmp(entry->d_name,"..") != 0 &&
416 strcmp(entry->d_name,".svn") != 0){
e8bb1a73 417 strcpy(name,cpu);
418 strcat(name,entry->d_name);
419 ltt_tracefile_open_cpu(t,name);
963b5f2d 420 }else continue;
421 }
422 closedir(dir);
2a74fbf4 423 return 0;
6cd62ccf 424}
425
963b5f2d 426/*****************************************************************************
427 *A trace is specified as a pathname to the directory containing all the
428 *associated data (control tracefiles, per cpu tracefiles, event
429 *descriptions...).
430 *
431 *When a trace is closed, all the associated facilities, types and fields
432 *are released as well.
8d1e6362 433 */
434
435
436/****************************************************************************
437 * get_absolute_pathname
803229fa 438 *
8d1e6362 439 * return the unique pathname in the system
440 *
ef35d837 441 * MD : Fixed this function so it uses realpath, dealing well with
442 * forgotten cases (.. were not used correctly before).
803229fa 443 *
963b5f2d 444 ****************************************************************************/
9f797243 445void get_absolute_pathname(const char *pathname, char * abs_pathname)
446{
9f797243 447 abs_pathname[0] = '\0';
803229fa 448
ef35d837 449 if ( realpath (pathname, abs_pathname) != NULL)
450 return;
451 else
452 {
8d1e6362 453 /* error, return the original path unmodified */
ef35d837 454 strcpy(abs_pathname, pathname);
9f797243 455 return;
456 }
ef35d837 457 return;
9f797243 458}
459
f7afe191 460LttTrace *ltt_trace_open(const char *pathname)
6cd62ccf 461{
963b5f2d 462 LttTrace * t;
463 LttSystemDescription * sys_description;
464 char eventdefs[DIR_NAME_SIZE];
465 char info[DIR_NAME_SIZE];
466 char control[DIR_NAME_SIZE];
467 char cpu[DIR_NAME_SIZE];
468 char tmp[DIR_NAME_SIZE];
9f797243 469 char abs_path[DIR_NAME_SIZE];
963b5f2d 470 gboolean has_slash = FALSE;
471
9f797243 472 get_absolute_pathname(pathname, abs_path);
963b5f2d 473 //establish the pathname to different directories
9f797243 474 if(abs_path[strlen(abs_path)-1] == '/')has_slash = TRUE;
475 strcpy(eventdefs,abs_path);
963b5f2d 476 if(!has_slash)strcat(eventdefs,"/");
477 strcat(eventdefs,"eventdefs/");
478
9f797243 479 strcpy(info,abs_path);
963b5f2d 480 if(!has_slash)strcat(info,"/");
481 strcat(info,"info/");
482
9f797243 483 strcpy(control,abs_path);
963b5f2d 484 if(!has_slash)strcat(control,"/");
485 strcat(control,"control/");
486
9f797243 487 strcpy(cpu,abs_path);
963b5f2d 488 if(!has_slash)strcat(cpu,"/");
489 strcat(cpu,"cpu/");
490
491 //new trace
963b5f2d 492 sys_description = g_new(LttSystemDescription, 1);
2a74fbf4 493 t = g_new(LttTrace, 1);
9f797243 494 t->pathname = g_strdup(abs_path);
963b5f2d 495 t->facility_number = 0;
496 t->control_tracefile_number = 0;
497 t->per_cpu_tracefile_number = 0;
498 t->system_description = sys_description;
499 t->control_tracefiles = g_ptr_array_new();
500 t->per_cpu_tracefiles = g_ptr_array_new();
501 t->facilities = g_ptr_array_new();
502 getDataEndianType(&(t->my_arch_size), &(t->my_arch_endian));
503
504 //get system description
505 strcpy(tmp,info);
506 strcat(tmp,"system.xml");
2a74fbf4 507 if(getSystemInfo(sys_description, tmp)) {
508 g_ptr_array_free(t->facilities, TRUE);
509 g_ptr_array_free(t->per_cpu_tracefiles, TRUE);
510 g_ptr_array_free(t->control_tracefiles, TRUE);
511 g_free(sys_description);
512 g_free(t->pathname);
513 g_free(t);
514 return NULL;
515 }
516
517
963b5f2d 518
b333a76b 519 //get facilities info
2a74fbf4 520 if(getFacilityInfo(t,eventdefs)) {
521 g_ptr_array_free(t->facilities, TRUE);
522 g_ptr_array_free(t->per_cpu_tracefiles, TRUE);
523 g_ptr_array_free(t->control_tracefiles, TRUE);
524 g_free(sys_description);
525 g_free(t->pathname);
526 g_free(t);
527 return NULL;
528 }
b333a76b 529
963b5f2d 530 //get control tracefile info
a00149f6 531 getControlFileInfo(t,control);
532 /*
2a74fbf4 533 if(getControlFileInfo(t,control)) {
534 g_ptr_array_free(t->facilities, TRUE);
535 g_ptr_array_free(t->per_cpu_tracefiles, TRUE);
536 g_ptr_array_free(t->control_tracefiles, TRUE);
537 g_free(sys_description);
538 g_free(t->pathname);
539 g_free(t);
540 return NULL;
a00149f6 541 }*/ // With fatal error
963b5f2d 542
543 //get cpu tracefile info
2a74fbf4 544 if(getCpuFileInfo(t,cpu)) {
545 g_ptr_array_free(t->facilities, TRUE);
546 g_ptr_array_free(t->per_cpu_tracefiles, TRUE);
547 g_ptr_array_free(t->control_tracefiles, TRUE);
548 g_free(sys_description);
549 g_free(t->pathname);
550 g_free(t);
551 return NULL;
552 }
963b5f2d 553
963b5f2d 554 return t;
6cd62ccf 555}
556
49bf71b5 557char * ltt_trace_name(LttTrace *t)
558{
559 return t->pathname;
560}
561
562
f7afe191 563/******************************************************************************
564 * When we copy a trace, we want all the opening actions to happen again :
565 * the trace will be reopened and totally independant from the original.
566 * That's why we call ltt_trace_open.
567 *****************************************************************************/
568LttTrace *ltt_trace_copy(LttTrace *self)
569{
570 return ltt_trace_open(self->pathname);
571}
572
963b5f2d 573void ltt_trace_close(LttTrace *t)
6cd62ccf 574{
8d1e6362 575 unsigned int i;
963b5f2d 576 LttTracefile * tf;
577 LttFacility * f;
578
579 g_free(t->pathname);
580
581 //free system_description
582 g_free(t->system_description->description);
583 g_free(t->system_description->node_name);
584 g_free(t->system_description->domain_name);
585 g_free(t->system_description->kernel_name);
586 g_free(t->system_description->kernel_release);
587 g_free(t->system_description->kernel_version);
588 g_free(t->system_description->machine);
589 g_free(t->system_description->processor);
590 g_free(t->system_description->hardware_platform);
591 g_free(t->system_description->operating_system);
592 g_free(t->system_description);
593
594 //free control_tracefiles
595 for(i=0;i<t->control_tracefile_number;i++){
596 tf = (LttTracefile*)g_ptr_array_index(t->control_tracefiles,i);
597 ltt_tracefile_close(tf);
598 }
09ad4797 599 g_ptr_array_free(t->control_tracefiles, TRUE);
963b5f2d 600
601 //free per_cpu_tracefiles
602 for(i=0;i<t->per_cpu_tracefile_number;i++){
603 tf = (LttTracefile*)g_ptr_array_index(t->per_cpu_tracefiles,i);
604 ltt_tracefile_close(tf);
605 }
09ad4797 606 g_ptr_array_free(t->per_cpu_tracefiles, TRUE);
963b5f2d 607
608 //free facilities
609 for(i=0;i<t->facility_number;i++){
610 f = (LttFacility*)g_ptr_array_index(t->facilities,i);
611 ltt_facility_close(f);
612 }
09ad4797 613 g_ptr_array_free(t->facilities, TRUE);
963b5f2d 614
615 g_free(t);
09ad4797 616
617 g_blow_chunks();
6cd62ccf 618}
619
963b5f2d 620
6cd62ccf 621/*****************************************************************************
963b5f2d 622 *Get the system description of the trace
6cd62ccf 623 ****************************************************************************/
624
963b5f2d 625LttSystemDescription *ltt_trace_system_description(LttTrace *t)
6cd62ccf 626{
963b5f2d 627 return t->system_description;
6cd62ccf 628}
629
630/*****************************************************************************
963b5f2d 631 * The following functions discover the facilities of the trace
6cd62ccf 632 ****************************************************************************/
633
963b5f2d 634unsigned ltt_trace_facility_number(LttTrace *t)
635{
636 return (unsigned)(t->facility_number);
637}
638
639LttFacility *ltt_trace_facility_get(LttTrace *t, unsigned i)
6cd62ccf 640{
963b5f2d 641 return (LttFacility*)g_ptr_array_index(t->facilities, i);
6cd62ccf 642}
643
644/*****************************************************************************
645 *Function name
963b5f2d 646 * ltt_trace_facility_find : find facilities in the trace
6cd62ccf 647 *Input params
963b5f2d 648 * t : the trace
649 * name : facility name
650 *Output params
651 * position : position of the facility in the trace
6cd62ccf 652 *Return value
963b5f2d 653 * : the number of facilities
6cd62ccf 654 ****************************************************************************/
655
963b5f2d 656unsigned ltt_trace_facility_find(LttTrace *t, char *name, unsigned *position)
6cd62ccf 657{
8d1e6362 658 unsigned int i, count=0;
963b5f2d 659 LttFacility * f;
8a3005f3 660 for(i=0;i<t->facility_number;i++){
963b5f2d 661 f = (LttFacility*)g_ptr_array_index(t->facilities, i);
662 if(strcmp(f->name,name)==0){
663 count++;
664 if(count==1) *position = i;
665 }else{
666 if(count) break;
667 }
668 }
669 return count;
6cd62ccf 670}
671
672/*****************************************************************************
963b5f2d 673 * Functions to discover all the event types in the trace
6cd62ccf 674 ****************************************************************************/
675
963b5f2d 676unsigned ltt_trace_eventtype_number(LttTrace *t)
6cd62ccf 677{
8d1e6362 678 unsigned int i;
963b5f2d 679 unsigned count = 0;
680 LttFacility * f;
b445142a 681 for(i=0;i<t->facility_number;i++){
963b5f2d 682 f = (LttFacility*)g_ptr_array_index(t->facilities, i);
683 count += f->event_number;
684 }
685 return count;
6cd62ccf 686}
687
963b5f2d 688LttFacility * ltt_trace_facility_by_id(LttTrace * trace, unsigned id)
6cd62ccf 689{
cf74a6f1 690 LttFacility * facility = NULL;
8d1e6362 691 unsigned int i;
cf74a6f1 692
963b5f2d 693 for(i=0;i<trace->facility_number;i++){
cf74a6f1 694 LttFacility *iter_facility =
695 (LttFacility*) g_ptr_array_index(trace->facilities,i);
696 if(id >= iter_facility->base_id &&
697 id < iter_facility->base_id + iter_facility->event_number) {
698 facility = iter_facility;
963b5f2d 699 break;
cf74a6f1 700 }
963b5f2d 701 }
cf74a6f1 702
703 return facility;
6cd62ccf 704}
705
963b5f2d 706LttEventType *ltt_trace_eventtype_get(LttTrace *t, unsigned evId)
6cd62ccf 707{
963b5f2d 708 LttFacility * f;
709 f = ltt_trace_facility_by_id(t,evId);
710 if(!f) return NULL;
711 return f->events[evId - f->base_id];
6cd62ccf 712}
713
714/*****************************************************************************
963b5f2d 715 *There is one "per cpu" tracefile for each CPU, numbered from 0 to
716 *the maximum number of CPU in the system. When the number of CPU installed
717 *is less than the maximum, some positions are unused. There are also a
718 *number of "control" tracefiles (facilities, interrupts...).
6cd62ccf 719 ****************************************************************************/
963b5f2d 720unsigned ltt_trace_control_tracefile_number(LttTrace *t)
6cd62ccf 721{
963b5f2d 722 return t->control_tracefile_number;
6cd62ccf 723}
724
963b5f2d 725unsigned ltt_trace_per_cpu_tracefile_number(LttTrace *t)
6cd62ccf 726{
963b5f2d 727 return t->per_cpu_tracefile_number;
6cd62ccf 728}
729
730/*****************************************************************************
963b5f2d 731 *It is possible to search for the tracefiles by name or by CPU position.
732 *The index within the tracefiles of the same type is returned if found
733 *and a negative value otherwise.
6cd62ccf 734 ****************************************************************************/
735
8d1e6362 736int ltt_trace_control_tracefile_find(LttTrace *t, const gchar *name)
6cd62ccf 737{
963b5f2d 738 LttTracefile * tracefile;
8d1e6362 739 unsigned int i;
963b5f2d 740 for(i=0;i<t->control_tracefile_number;i++){
741 tracefile = (LttTracefile*)g_ptr_array_index(t->control_tracefiles, i);
742 if(strcmp(tracefile->name, name)==0)break;
743 }
744 if(i == t->control_tracefile_number) return -1;
745 return i;
6cd62ccf 746}
747
8d1e6362 748/* not really useful. We just have to know that cpu tracefiles
749 * comes before control tracefiles.
750 */
751int ltt_trace_per_cpu_tracefile_find(LttTrace *t, const gchar *name)
6cd62ccf 752{
963b5f2d 753 LttTracefile * tracefile;
8d1e6362 754 unsigned int i;
755 for(i=0;i<t->per_cpu_tracefile_number;i++){
756 tracefile = (LttTracefile*)g_ptr_array_index(t->per_cpu_tracefiles, i);
757 if(strcmp(tracefile->name, name)==0)break;
963b5f2d 758 }
8d1e6362 759 if(i == t->per_cpu_tracefile_number) return -1;
760 return i;
6cd62ccf 761}
762
763/*****************************************************************************
963b5f2d 764 *Get a specific tracefile
6cd62ccf 765 ****************************************************************************/
766
963b5f2d 767LttTracefile *ltt_trace_control_tracefile_get(LttTrace *t, unsigned i)
6cd62ccf 768{
5598cfe3 769 return (LttTracefile*)g_ptr_array_index(t->control_tracefiles, i);
963b5f2d 770}
771
772LttTracefile *ltt_trace_per_cpu_tracefile_get(LttTrace *t, unsigned i)
773{
774 return (LttTracefile*)g_ptr_array_index(t->per_cpu_tracefiles, i);
6cd62ccf 775}
776
487ad181 777/*****************************************************************************
778 * Get the start time and end time of the trace
779 ****************************************************************************/
780
781void ltt_trace_time_span_get(LttTrace *t, LttTime *start, LttTime *end)
782{
783 LttTime startSmall, startTmp, endBig, endTmp;
8d1e6362 784 unsigned int i, j=0;
487ad181 785 LttTracefile * tf;
786
787 for(i=0;i<t->control_tracefile_number;i++){
788 tf = g_ptr_array_index(t->control_tracefiles, i);
789 readBlock(tf,1);
790 startTmp = tf->a_block_start->time;
791 readBlock(tf,tf->block_number);
792 endTmp = tf->a_block_end->time;
793 if(i==0){
794 startSmall = startTmp;
795 endBig = endTmp;
796 j = 1;
797 continue;
798 }
308711e5 799 if(ltt_time_compare(startSmall,startTmp) > 0) startSmall = startTmp;
800 if(ltt_time_compare(endBig,endTmp) < 0) endBig = endTmp;
487ad181 801 }
802
803 for(i=0;i<t->per_cpu_tracefile_number;i++){
804 tf = g_ptr_array_index(t->per_cpu_tracefiles, i);
805 readBlock(tf,1);
806 startTmp = tf->a_block_start->time;
807 readBlock(tf,tf->block_number);
808 endTmp = tf->a_block_end->time;
809 if(j == 0 && i==0){
810 startSmall = startTmp;
811 endBig = endTmp;
812 continue;
813 }
308711e5 814 if(ltt_time_compare(startSmall,startTmp) > 0) startSmall = startTmp;
815 if(ltt_time_compare(endBig,endTmp) < 0) endBig = endTmp;
487ad181 816 }
817
c02ea99f 818 if(start != NULL) *start = startSmall;
819 if(end != NULL) *end = endBig;
487ad181 820}
821
822
6cd62ccf 823/*****************************************************************************
963b5f2d 824 *Get the name of a tracefile
6cd62ccf 825 ****************************************************************************/
826
963b5f2d 827char *ltt_tracefile_name(LttTracefile *tf)
6cd62ccf 828{
963b5f2d 829 return tf->name;
6cd62ccf 830}
831
80da81ad 832/*****************************************************************************
833 * Get the number of blocks in the tracefile
834 ****************************************************************************/
835
836unsigned ltt_tracefile_block_number(LttTracefile *tf)
837{
838 return tf->block_number;
839}
840
6cd62ccf 841/*****************************************************************************
842 *Function name
843 * ltt_tracefile_seek_time: seek to the first event of the trace with time
844 * larger or equal to time
845 *Input params
846 * t : tracefile
847 * time : criteria of the time
6cd62ccf 848 ****************************************************************************/
caf7a67a 849void ltt_tracefile_find_time_block(LttTracefile *t, LttTime time,
850 int start_block, int end_block)
851{
852 int err, tmp_block, s, e;
853 int headTime;
854 int tailTime;
855
856 err=readBlock(t,start_block);
857 if(err) g_error("Can not read tracefile: %s\n", t->name);
858 if(start_block == end_block)return;
859
860 tailTime = ltt_time_compare(t->a_block_end->time, time);
861 if(tailTime >= 0) return;
862
863 err=readBlock(t,end_block);
864 if(err) g_error("Can not read tracefile: %s\n", t->name);
865 if(start_block+1 == end_block)return;
866
867 headTime = ltt_time_compare(t->a_block_start->time, time);
868 if(headTime <= 0 ) return;
869
870 tmp_block = (end_block + start_block)/2;
871 err=readBlock(t,tmp_block);
872 if(err) g_error("Can not read tracefile: %s\n", t->name);
873
874 headTime = ltt_time_compare(t->a_block_start->time, time);
875 tailTime = ltt_time_compare(t->a_block_end->time, time);
876 if(headTime <= 0 && tailTime >= 0) return;
877
878 if(headTime > 0){
879 s = start_block + 1;
880 e = tmp_block - 1;
881 if(s <= e)
882 ltt_tracefile_find_time_block(t, time, s, e);
883 else return;
884 }
885
886 if(tailTime < 0){
887 s = tmp_block + 1;
888 e = end_block - 1;
889 if(s <= e)
890 ltt_tracefile_find_time_block(t, time, s, e);
891 else return;
892 }
893}
894
895void ltt_tracefile_backward_find_time_block(LttTracefile *t, LttTime time)
896{
897 int t_time, h_time, err;
898 err=readBlock(t,t->which_block-1);
899 if(err) g_error("Can not read tracefile: %s\n", t->name);
900 h_time = ltt_time_compare(t->a_block_start->time, time);
901 t_time = ltt_time_compare(t->a_block_end->time, time);
902 if(h_time == 0){
903 int tmp;
904 if(t->which_block == 1) return;
905 err=readBlock(t,t->which_block-1);
906 if(err) g_error("Can not read tracefile: %s\n", t->name);
907 tmp = ltt_time_compare(t->a_block_end->time, time);
908 if(tmp == 0) return ltt_tracefile_seek_time(t, time);
909 err=readBlock(t,t->which_block+1);
910 if(err) g_error("Can not read tracefile: %s\n", t->name);
911 }else if(h_time > 0){
912 ltt_tracefile_find_time_block(t, time, 1, t->which_block);
913 return ltt_tracefile_seek_time(t, time) ;
914 }else{
915 if(t_time >= 0) return ltt_tracefile_seek_time(t, time);
916 err=readBlock(t,t->which_block+1);
917 if(err) g_error("Can not read tracefile: %s\n", t->name);
918 }
919}
6cd62ccf 920
963b5f2d 921void ltt_tracefile_seek_time(LttTracefile *t, LttTime time)
6cd62ccf 922{
923 int err;
963b5f2d 924 LttTime lttTime;
308711e5 925 int headTime = ltt_time_compare(t->a_block_start->time, time);
926 int tailTime = ltt_time_compare(t->a_block_end->time, time);
c02ea99f 927 LttEvent ev;
62e55dd6 928
6cd62ccf 929 if(headTime < 0 && tailTime > 0){
308711e5 930 if(ltt_time_compare(t->a_block_end->time, t->current_event_time) !=0) {
db55eaae 931 lttTime = getEventTime(t);
308711e5 932 err = ltt_time_compare(lttTime, time);
db55eaae 933 if(err > 0){
8d1e6362 934 if(t->which_event==2 || ltt_time_compare(t->prev_event_time,time)<0){
1a3b8cbd 935 return;
db55eaae 936 }else{
937 updateTracefile(t);
938 return ltt_tracefile_seek_time(t, time);
1a3b8cbd 939 }
db55eaae 940 }else if(err < 0){
941 while(1){
c02ea99f 942 if(ltt_tracefile_read(t,&ev) == NULL) {
db55eaae 943 g_print("End of file\n");
944 return;
945 }
946 lttTime = getEventTime(t);
308711e5 947 err = ltt_time_compare(lttTime, time);
db55eaae 948 if(err >= 0)return;
949 }
950 }else return;
951 }else{//we are at the end of the block
952 updateTracefile(t);
953 return ltt_tracefile_seek_time(t, time);
954 }
e37c1372 955 }else if(headTime >= 0){
6cd62ccf 956 if(t->which_block == 1){
957 updateTracefile(t);
958 }else{
1ee6a9af 959 if(ltt_time_compare(t->prev_block_end_time, time) >= 0 ||
960 (t->prev_block_end_time.tv_sec == 0 &&
961 t->prev_block_end_time.tv_nsec == 0 )){
caf7a67a 962 ltt_tracefile_backward_find_time_block(t, time);
6cd62ccf 963 }else{
964 updateTracefile(t);
965 }
966 }
40331ba8 967 }else if(tailTime < 0){
6cd62ccf 968 if(t->which_block != t->block_number){
caf7a67a 969 ltt_tracefile_find_time_block(t, time, t->which_block+1, t->block_number);
970 return ltt_tracefile_seek_time(t, time);
963b5f2d 971 }else {
a8c0f09d 972 t->cur_event_pos = t->buffer + t->block_size;
973 g_print("End of file\n");
963b5f2d 974 return;
975 }
40331ba8 976 }else if(tailTime == 0){
e37c1372 977 t->cur_event_pos = t->last_event_pos;
62e55dd6 978 t->current_event_time = time;
979 t->cur_heart_beat_number = 0;
980 t->prev_event_time.tv_sec = 0;
981 t->prev_event_time.tv_nsec = 0;
40331ba8 982 return;
6cd62ccf 983 }
6cd62ccf 984}
985
80da81ad 986/*****************************************************************************
987 * Seek to the first event with position equal or larger to ep
18206708 988 *
989 * Modified by Mathieu Desnoyers to used faster offset position instead of
990 * re-reading the whole buffer.
80da81ad 991 ****************************************************************************/
992
04b44e05 993void ltt_tracefile_seek_position(LttTracefile *t, const LttEventPosition *ep)
80da81ad 994{
995 //if we are at the right place, just return
996 if(t->which_block == ep->block_num && t->which_event == ep->event_num)
997 return;
998
999 if(t->which_block == ep->block_num) updateTracefile(t);
1000 else readBlock(t,ep->block_num);
18206708 1001 //event offset is available
80da81ad 1002 if(ep->old_position){
18206708 1003 int err;
1004
1005 t->which_event = ep->event_num;
80da81ad 1006 t->cur_event_pos = t->buffer + ep->event_offset;
18206708 1007 t->prev_event_time = ep->event_time;
1008 t->current_event_time = ep->event_time;
1009 t->cur_heart_beat_number = ep->heart_beat_number;
1010 t->cur_cycle_count = ep->event_cycle_count;
1011
1012 /* This is a workaround for fast position seek */
1013 t->last_event_pos = ep->last_event_pos;
1014 t->prev_block_end_time = ep->prev_block_end_time;
1015 t->prev_event_time = ep->prev_event_time;
1016 t->pre_cycle_count = ep->pre_cycle_count;
1017 t->count = ep->count;
1018 /* end of workaround */
1019
1020 //update the fields of the current event and go to the next event
1021 err = skipEvent(t);
1022 if(err == ERANGE) g_error("event id is out of range\n");
1023
80da81ad 1024 return;
1025 }
1026
18206708 1027 //only block number and event index are available
1028 //MD: warning : this is slow!
1029 g_warning("using slow O(n) tracefile seek position");
1030
c02ea99f 1031 LttEvent event;
1032 while(t->which_event < ep->event_num) ltt_tracefile_read(t, &event);
80da81ad 1033
1034 return;
1035}
1036
6cd62ccf 1037/*****************************************************************************
1038 *Function name
40331ba8 1039 * ltt_tracefile_read : read the current event, set the pointer to the next
6cd62ccf 1040 *Input params
1041 * t : tracefile
1042 *Return value
963b5f2d 1043 * LttEvent * : an event to be processed
6cd62ccf 1044 ****************************************************************************/
1045
c02ea99f 1046LttEvent *ltt_tracefile_read(LttTracefile *t, LttEvent *event)
6cd62ccf 1047{
963b5f2d 1048 int err;
6cd62ccf 1049
bdc36259 1050 if(t->cur_event_pos == t->buffer + t->block_size){
1051 if(t->which_block == t->block_number){
bdc36259 1052 return NULL;
1053 }
1054 err = readBlock(t, t->which_block + 1);
1055 if(err)g_error("Can not read tracefile");
1056 }
1057
c02ea99f 1058 event->event_id = (int)(*(guint16 *)(t->cur_event_pos));
1059 if(event->event_id == TRACE_TIME_HEARTBEAT)
963b5f2d 1060 t->cur_heart_beat_number++;
6cd62ccf 1061
40331ba8 1062 t->prev_event_time = t->current_event_time;
62e55dd6 1063 // t->current_event_time = getEventTime(t);
6cd62ccf 1064
c02ea99f 1065 event->time_delta = *(guint32 *)(t->cur_event_pos + EVENT_ID_SIZE);
1066 event->event_time = t->current_event_time;
1067 event->event_cycle_count = t->cur_cycle_count;
963b5f2d 1068
c02ea99f 1069 event->tracefile = t;
1070 event->data = t->cur_event_pos + EVENT_HEADER_SIZE;
1071 event->which_block = t->which_block;
1072 event->which_event = t->which_event;
6cd62ccf 1073
18206708 1074 /* This is a workaround for fast position seek */
c02ea99f 1075 event->last_event_pos = t->last_event_pos;
1076 event->prev_block_end_time = t->prev_block_end_time;
1077 event->prev_event_time = t->prev_event_time;
1078 event->pre_cycle_count = t->pre_cycle_count;
1079 event->count = t->count;
18206708 1080 /* end of workaround */
1081
1082
1083
40331ba8 1084 //update the fields of the current event and go to the next event
1085 err = skipEvent(t);
40331ba8 1086 if(err == ERANGE) g_error("event id is out of range\n");
40331ba8 1087
c02ea99f 1088 return event;
6cd62ccf 1089}
1090
1091/****************************************************************************
1092 *Function name
1093 * readFile : wrap function to read from a file
1094 *Input Params
1095 * fd : file descriptor
1096 * buf : buf to contain the content
1097 * size : number of bytes to be read
1098 * mesg : message to be printed if some thing goes wrong
1099 *return value
1100 * 0 : success
1101 * EIO : can not read from the file
1102 ****************************************************************************/
1103
1104int readFile(int fd, void * buf, size_t size, char * mesg)
1105{
8d1e6362 1106 ssize_t nbBytes = read(fd, buf, size);
1107
1108 if((size_t)nbBytes != size) {
1109 if(nbBytes < 0) {
1110 perror("Error in readFile : ");
1111 } else {
1112 g_warning("%s",mesg);
1113 }
6cd62ccf 1114 return EIO;
1115 }
1116 return 0;
1117}
1118
1119/****************************************************************************
1120 *Function name
1121 * readBlock : read a block from the file
1122 *Input Params
1123 * lttdes : ltt trace file
1124 * whichBlock : the block which will be read
1125 *return value
1126 * 0 : success
1127 * EINVAL : lseek fail
1128 * EIO : can not read from the file
1129 ****************************************************************************/
1130
963b5f2d 1131int readBlock(LttTracefile * tf, int whichBlock)
6cd62ccf 1132{
1133 off_t nbBytes;
cbd41522 1134 guint32 lostSize;
6cd62ccf 1135
1136 if(whichBlock - tf->which_block == 1 && tf->which_block != 0){
963b5f2d 1137 tf->prev_block_end_time = tf->a_block_end->time;
40331ba8 1138 tf->prev_event_time = tf->a_block_end->time;
6cd62ccf 1139 }else{
1140 tf->prev_block_end_time.tv_sec = 0;
1141 tf->prev_block_end_time.tv_nsec = 0;
40331ba8 1142 tf->prev_event_time.tv_sec = 0;
1143 tf->prev_event_time.tv_nsec = 0;
6cd62ccf 1144 }
6cd62ccf 1145
963b5f2d 1146 nbBytes=lseek(tf->fd,(off_t)((whichBlock-1)*tf->block_size), SEEK_SET);
6cd62ccf 1147 if(nbBytes == -1) return EINVAL;
1148
963b5f2d 1149 if(readFile(tf->fd,tf->buffer,tf->block_size,"Unable to read a block"))
1150 return EIO;
6cd62ccf 1151
963b5f2d 1152 tf->a_block_start=(BlockStart *) (tf->buffer + EVENT_HEADER_SIZE);
cbd41522 1153 lostSize = *(guint32 *)(tf->buffer + tf->block_size - sizeof(guint32));
963b5f2d 1154 tf->a_block_end=(BlockEnd *)(tf->buffer + tf->block_size -
1155 lostSize + EVENT_HEADER_SIZE);
e37c1372 1156 tf->last_event_pos = tf->buffer + tf->block_size - lostSize;
6cd62ccf 1157
6cd62ccf 1158 tf->which_block = whichBlock;
963b5f2d 1159 tf->which_event = 1;
40331ba8 1160 tf->cur_event_pos = tf->buffer;//the beginning of the block, block start ev
6cd62ccf 1161 tf->cur_heart_beat_number = 0;
963b5f2d 1162
6cd62ccf 1163 getCyclePerNsec(tf);
1164
62e55dd6 1165 tf->current_event_time = getEventTime(tf);
40331ba8 1166
6cd62ccf 1167 return 0;
1168}
1169
1170/*****************************************************************************
1171 *Function name
1172 * updateTracefile : reinitialize the info of the block which is already
1173 * in the buffer
1174 *Input params
1175 * tf : tracefile
1176 ****************************************************************************/
1177
963b5f2d 1178void updateTracefile(LttTracefile * tf)
6cd62ccf 1179{
963b5f2d 1180 tf->which_event = 1;
40331ba8 1181 tf->cur_event_pos = tf->buffer;
62e55dd6 1182 tf->current_event_time = getEventTime(tf);
6cd62ccf 1183 tf->cur_heart_beat_number = 0;
1184
1185 tf->prev_event_time.tv_sec = 0;
1186 tf->prev_event_time.tv_nsec = 0;
1187}
1188
1189/*****************************************************************************
1190 *Function name
1191 * skipEvent : go to the next event, update the fields of the current event
1192 *Input params
1193 * t : tracefile
1194 *return value
1195 * 0 : success
6cd62ccf 1196 * ERANGE : event id is out of range
1197 ****************************************************************************/
1198
963b5f2d 1199int skipEvent(LttTracefile * t)
6cd62ccf 1200{
8d1e6362 1201 int evId;
6cd62ccf 1202 void * evData;
963b5f2d 1203 LttEventType * evT;
1204 LttField * rootFld;
6cd62ccf 1205
cbd41522 1206 evId = (int)(*(guint16 *)(t->cur_event_pos));
6cd62ccf 1207 evData = t->cur_event_pos + EVENT_HEADER_SIZE;
6cd62ccf 1208
908f42fa 1209 evT = ltt_trace_eventtype_get(t->trace,(unsigned)evId);
47a166fc 1210
1d1df11d 1211 if(likely(evT)) rootFld = evT->root_field;
908f42fa 1212 else return ERANGE;
6cd62ccf 1213
1d1df11d 1214 if(likely(rootFld)){
908f42fa 1215 //event has string/sequence or the last event is not the same event
1d1df11d 1216 if(likely((evT->latest_block!=t->which_block || evT->latest_event!=t->which_event)
1217 && rootFld->field_fixed == 0)){
908f42fa 1218 setFieldsOffset(t, evT, evData, t->trace);
47a166fc 1219 }
908f42fa 1220 t->cur_event_pos += EVENT_HEADER_SIZE + rootFld->field_size;
1221 }else t->cur_event_pos += EVENT_HEADER_SIZE;
1222
1223 evT->latest_block = t->which_block;
1224 evT->latest_event = t->which_event;
1225
6cd62ccf 1226 //the next event is in the next block
1d1df11d 1227 if(unlikely(evId == TRACE_BLOCK_END)){
bdc36259 1228 t->cur_event_pos = t->buffer + t->block_size;
6cd62ccf 1229 }else{
1230 t->which_event++;
62e55dd6 1231 t->current_event_time = getEventTime(t);
6cd62ccf 1232 }
1233
1234 return 0;
1235}
1236
18206708 1237
6cd62ccf 1238/*****************************************************************************
1239 *Function name
1240 * getCyclePerNsec : calculate cycles per nsec for current block
1241 *Input Params
1242 * t : tracefile
1243 ****************************************************************************/
1244
963b5f2d 1245void getCyclePerNsec(LttTracefile * t)
6cd62ccf 1246{
963b5f2d 1247 LttTime lBufTotalTime; /* Total time for this buffer */
887208b7 1248 double lBufTotalNSec; /* Total time for this buffer in nsecs */
1249 double lBufTotalCycle;/* Total cycles for this buffer */
6cd62ccf 1250
1251 /* Calculate the total time for this buffer */
308711e5 1252 lBufTotalTime = ltt_time_sub(t->a_block_end->time, t->a_block_start->time);
6cd62ccf 1253
1254 /* Calculate the total cycles for this bufffer */
e4eced0f 1255 lBufTotalCycle = t->a_block_end->cycle_count;
1256 lBufTotalCycle -= t->a_block_start->cycle_count;
6cd62ccf 1257
1258 /* Convert the total time to nsecs */
887208b7 1259 lBufTotalNSec = ltt_time_to_double(lBufTotalTime);
6cd62ccf 1260
887208b7 1261 t->nsec_per_cycle = (double)lBufTotalNSec / (double)lBufTotalCycle;
1262 /* See : http://www.azillionmonkeys.com/qed/adiv.html */
1263 // precalculate the reciprocal, so divisions will be really fast.
1264 // 2^32-1 == 0xFFFFFFFFULL
1265 //{
1266 // double int_res = lBufTotalCycle/lBufTotalNSec;
1267 // t->cycles_per_nsec_reciprocal =
1268 // ((0xFFFF+int_res)/int_res);
1269 //}
1270
6cd62ccf 1271}
1272
1273/****************************************************************************
1274 *Function name
1275 * getEventTime : obtain the time of an event
887208b7 1276 * NOTE : this function _really_ is on critical path.
6cd62ccf 1277 *Input params
1278 * tf : tracefile
1279 *Return value
963b5f2d 1280 * LttTime : the time of the event
6cd62ccf 1281 ****************************************************************************/
1282
8959a0c8 1283static inline LttTime getEventTime(LttTracefile * tf)
6cd62ccf 1284{
963b5f2d 1285 LttTime time;
1286 LttCycleCount cycle_count; // cycle count for the current event
1287 LttCycleCount lEventTotalCycle; // Total cycles from start for event
887208b7 1288 LttCycleCount lEventNSec; // Total nsecs from start for event
963b5f2d 1289 LttTime lTimeOffset; // Time offset in struct LttTime
cbd41522 1290 guint16 evId;
e4eced0f 1291
cbd41522 1292 evId = *(guint16 *)tf->cur_event_pos;
1d1df11d 1293 if(unlikely(evId == TRACE_BLOCK_START)){
dd691a2e 1294 tf->count = 0;
1295 tf->pre_cycle_count = 0;
e4eced0f 1296 tf->cur_cycle_count = tf->a_block_start->cycle_count;
40331ba8 1297 return tf->a_block_start->time;
1d1df11d 1298 }else if(unlikely(evId == TRACE_BLOCK_END)){
dd691a2e 1299 tf->count = 0;
1300 tf->pre_cycle_count = 0;
e4eced0f 1301 tf->cur_cycle_count = tf->a_block_end->cycle_count;
40331ba8 1302 return tf->a_block_end->time;
e4eced0f 1303 }
40331ba8 1304
e4eced0f 1305 // Calculate total time in cycles from start of buffer for this event
cbd41522 1306 cycle_count = (LttCycleCount)*(guint32 *)(tf->cur_event_pos + EVENT_ID_SIZE);
e4eced0f 1307
1d1df11d 1308 if(unlikely(cycle_count < tf->pre_cycle_count)) tf->count++;
dd691a2e 1309 tf->pre_cycle_count = cycle_count;
8959a0c8 1310 cycle_count += (LttCycleCount)tf->count << 32;
e4eced0f 1311
e9b34357 1312 // if(tf->cur_heart_beat_number > tf->count)
1313 // cycle_count += tmpCycleCount * (tf->cur_heart_beat_number - tf->count);
e4eced0f 1314
1315 tf->cur_cycle_count = cycle_count;
1316
1317 lEventTotalCycle = cycle_count;
1318 lEventTotalCycle -= tf->a_block_start->cycle_count;
6cd62ccf 1319
963b5f2d 1320 // Convert it to nsecs
887208b7 1321 lEventNSec = (double)lEventTotalCycle * (double)tf->nsec_per_cycle;
1322 //lEventNSec = (tf->cycles_per_nsec_reciprocal * lEventTotalCycle) >> 16;
1323
963b5f2d 1324 // Determine offset in struct LttTime
8959a0c8 1325 lTimeOffset = ltt_time_from_double(lEventNSec);
6cd62ccf 1326
308711e5 1327 time = ltt_time_add(tf->a_block_start->time, lTimeOffset);
e4eced0f 1328
6cd62ccf 1329 return time;
1330}
1331
1332/*****************************************************************************
1333 *Function name
1334 * setFieldsOffset : set offset of the fields
1335 *Input params
1336 * tracefile : opened trace file
1337 * evT : the event type
1338 * evD : event data, it may be NULL
1339 ****************************************************************************/
1340
40331ba8 1341void setFieldsOffset(LttTracefile *tf,LttEventType *evT,void *evD,LttTrace* t)
6cd62ccf 1342{
963b5f2d 1343 LttField * rootFld = evT->root_field;
6cd62ccf 1344 // rootFld->base_address = evD;
1345
1d1df11d 1346 if(likely(rootFld))
8710c6c7 1347 rootFld->field_size = getFieldtypeSize(tf, evT, 0,0,rootFld, evD,t);
6cd62ccf 1348}
1349
1350/*****************************************************************************
1351 *Function name
1352 * getFieldtypeSize: get the size of the field type (primitive type)
1353 *Input params
1354 * tracefile : opened trace file
1355 * evT : event type
1356 * offsetRoot : offset from the root
1357 * offsetParent : offset from the parrent
1358 * fld : field
1359 * evD : event data, it may be NULL
1360 *Return value
1361 * int : size of the field
1362 ****************************************************************************/
1363
963b5f2d 1364int getFieldtypeSize(LttTracefile * t, LttEventType * evT, int offsetRoot,
40331ba8 1365 int offsetParent, LttField * fld, void *evD, LttTrace *trace)
6cd62ccf 1366{
1367 int size, size1, element_number, i, offset1, offset2;
963b5f2d 1368 LttType * type = fld->field_type;
6cd62ccf 1369
1d1df11d 1370 if(likely(t)){
1371 if(unlikely(evT->latest_block==t->which_block && evT->latest_event==t->which_event)){
963b5f2d 1372 return fld->field_size;
1373 }
1374 }
6cd62ccf 1375
1d1df11d 1376 if(likely(fld->field_fixed == 1)){
6cd62ccf 1377 if(fld == evT->root_field) return fld->field_size;
1378 }
1379
cf74a6f1 1380 switch(type->type_class) {
1381 case LTT_ARRAY:
1382 element_number = (int) type->element_number;
1383 if(fld->field_fixed == -1){
1384 size = getFieldtypeSize(t, evT, offsetRoot,
1385 0,fld->child[0], NULL, trace);
1386 if(size == 0){ //has string or sequence
1387 fld->field_fixed = 0;
1388 }else{
1389 fld->field_fixed = 1;
1390 size *= element_number;
1391 }
1392 }else if(fld->field_fixed == 0){// has string or sequence
1393 size = 0;
1394 for(i=0;i<element_number;i++){
1395 size += getFieldtypeSize(t, evT, offsetRoot+size,size,
1396 fld->child[0], evD+size, trace);
1397 }
1398 }else size = fld->field_size;
1399 break;
1400
1401 case LTT_SEQUENCE:
1402 size1 = (int) ltt_type_size(trace, type);
1403 if(fld->field_fixed == -1){
1404 fld->sequ_number_size = size1;
1405 fld->field_fixed = 0;
1406 size = getFieldtypeSize(t, evT, offsetRoot,
1407 0,fld->child[0], NULL, trace);
1408 fld->element_size = size;
1409 }else{//0: sequence
1410 element_number = getIntNumber(size1,evD);
1411 type->element_number = element_number;
1412 if(fld->element_size > 0){
1413 size = element_number * fld->element_size;
1414 }else{//sequence has string or sequence
1415 size = 0;
1416 for(i=0;i<element_number;i++){
1417 size += getFieldtypeSize(t, evT, offsetRoot+size+size1,size+size1,
1418 fld->child[0], evD+size+size1, trace);
1419 }
1420 }
1421 size += size1;
1422 }
1423 break;
1424
1425 case LTT_STRING:
1426 size = 0;
1427 if(fld->field_fixed == -1){
1428 fld->field_fixed = 0;
1429 }else{//0: string
1430 size = strlen((char*)evD) + 1; //include end : '\0'
1431 }
1432 break;
1433
1434 case LTT_STRUCT:
1435 element_number = (int) type->element_number;
1436 size = 0;
1d1df11d 1437 if(fld->field_fixed == -1){
cf74a6f1 1438 offset1 = offsetRoot;
1439 offset2 = 0;
1440 for(i=0;i<element_number;i++){
1441 size1=getFieldtypeSize(t, evT,offset1,offset2,
1442 fld->child[i], NULL, trace);
1443 if(size1 > 0 && size >= 0){
1444 size += size1;
1445 if(offset1 >= 0) offset1 += size1;
1446 offset2 += size1;
1447 }else{
1448 size = -1;
1449 offset1 = -1;
1450 offset2 = -1;
1451 }
1452 }
1453 if(size == -1){
1454 fld->field_fixed = 0;
1455 size = 0;
1456 }else fld->field_fixed = 1;
1457 }else if(fld->field_fixed == 0){
1458 offset1 = offsetRoot;
1459 offset2 = 0;
1460 for(i=0;i<element_number;i++){
1461 size=getFieldtypeSize(t,evT,offset1,offset2,
1462 fld->child[i],evD+offset2, trace);
1463 offset1 += size;
1464 offset2 += size;
1465 }
1466 size = offset2;
1467 }else size = fld->field_size;
1468 break;
1469
1470 default:
1471 if(fld->field_fixed == -1){
1472 size = (int) ltt_type_size(trace, type);
1473 fld->field_fixed = 1;
1474 }else size = fld->field_size;
1475 break;
1476 }
1477
1478
1479
1480#if 0
6cd62ccf 1481 if(type->type_class != LTT_STRUCT && type->type_class != LTT_ARRAY &&
1482 type->type_class != LTT_SEQUENCE && type->type_class != LTT_STRING){
1483 if(fld->field_fixed == -1){
40331ba8 1484 size = (int) ltt_type_size(trace, type);
6cd62ccf 1485 fld->field_fixed = 1;
1486 }else size = fld->field_size;
1487
1488 }else if(type->type_class == LTT_ARRAY){
1489 element_number = (int) type->element_number;
1490 if(fld->field_fixed == -1){
40331ba8 1491 size = getFieldtypeSize(t, evT, offsetRoot,0,fld->child[0], NULL, trace);
6cd62ccf 1492 if(size == 0){ //has string or sequence
1493 fld->field_fixed = 0;
1494 }else{
1495 fld->field_fixed = 1;
1496 size *= element_number;
1497 }
1498 }else if(fld->field_fixed == 0){// has string or sequence
1499 size = 0;
1500 for(i=0;i<element_number;i++){
1501 size += getFieldtypeSize(t, evT, offsetRoot+size,size,
40331ba8 1502 fld->child[0], evD+size, trace);
6cd62ccf 1503 }
1504 }else size = fld->field_size;
1505
1506 }else if(type->type_class == LTT_SEQUENCE){
40331ba8 1507 size1 = (int) ltt_type_size(trace, type);
6cd62ccf 1508 if(fld->field_fixed == -1){
908f42fa 1509 fld->sequ_number_size = size1;
6cd62ccf 1510 fld->field_fixed = 0;
40331ba8 1511 size = getFieldtypeSize(t, evT, offsetRoot,0,fld->child[0], NULL, trace);
6cd62ccf 1512 fld->element_size = size;
1513 }else{//0: sequence
1514 element_number = getIntNumber(size1,evD);
1515 type->element_number = element_number;
1516 if(fld->element_size > 0){
1517 size = element_number * fld->element_size;
1518 }else{//sequence has string or sequence
1519 size = 0;
1520 for(i=0;i<element_number;i++){
1521 size += getFieldtypeSize(t, evT, offsetRoot+size+size1,size+size1,
40331ba8 1522 fld->child[0], evD+size+size1, trace);
6cd62ccf 1523 }
1524 }
1525 size += size1;
1526 }
1527
1528 }else if(type->type_class == LTT_STRING){
1529 size = 0;
1530 if(fld->field_fixed == -1){
1531 fld->field_fixed = 0;
1532 }else{//0: string
47a166fc 1533 size = strlen((char*)evD) + 1; //include end : '\0'
6cd62ccf 1534 }
1535
1536 }else if(type->type_class == LTT_STRUCT){
1537 element_number = (int) type->element_number;
1538 size = 0;
1539 if(fld->field_fixed == -1){
1540 offset1 = offsetRoot;
1541 offset2 = 0;
1542 for(i=0;i<element_number;i++){
40331ba8 1543 size1=getFieldtypeSize(t, evT,offset1,offset2, fld->child[i], NULL, trace);
6cd62ccf 1544 if(size1 > 0 && size >= 0){
1545 size += size1;
1546 if(offset1 >= 0) offset1 += size1;
1547 offset2 += size1;
1548 }else{
1549 size = -1;
1550 offset1 = -1;
1551 offset2 = -1;
1552 }
1553 }
1554 if(size == -1){
1555 fld->field_fixed = 0;
1556 size = 0;
1557 }else fld->field_fixed = 1;
1558 }else if(fld->field_fixed == 0){
1559 offset1 = offsetRoot;
1560 offset2 = 0;
1561 for(i=0;i<element_number;i++){
40331ba8 1562 size=getFieldtypeSize(t,evT,offset1,offset2,fld->child[i],evD+offset2, trace);
6cd62ccf 1563 offset1 += size;
1564 offset2 += size;
1565 }
1566 size = offset2;
1567 }else size = fld->field_size;
1568 }
cf74a6f1 1569#endif //0
6cd62ccf 1570
1571 fld->offset_root = offsetRoot;
1572 fld->offset_parent = offsetParent;
1573 if(!evD){
1574 fld->fixed_root = (offsetRoot==-1) ? 0 : 1;
1575 fld->fixed_parent = (offsetParent==-1) ? 0 : 1;
1576 }
1577 fld->field_size = size;
1578
1579 return size;
1580}
1581
6cd62ccf 1582
1583/*****************************************************************************
1584 *Function name
1585 * getIntNumber : get an integer number
1586 *Input params
1587 * size : the size of the integer
1588 * evD : the event data
1589 *Return value
cf74a6f1 1590 * gint64 : a 64 bits integer
6cd62ccf 1591 ****************************************************************************/
1592
cf74a6f1 1593gint64 getIntNumber(int size, void *evD)
6cd62ccf 1594{
cbd41522 1595 gint64 i;
cf74a6f1 1596
1597 switch(size) {
1598 case 1: i = *(gint8 *)evD; break;
1599 case 2: i = *(gint16 *)evD; break;
1600 case 4: i = *(gint32 *)evD; break;
1601 case 8: i = *(gint64 *)evD; break;
1602 default: i = *(gint64 *)evD;
1603 g_critical("getIntNumber : integer size %d unknown", size);
1604 break;
1605 }
1606
1607#if 0
cbd41522 1608 if(size == 1) i = *(gint8 *)evD;
1609 else if(size == 2) i = *(gint16 *)evD;
1610 else if(size == 4) i = *(gint32 *)evD;
1611 else if(size == 8) i = *(gint64 *)evD;
cf74a6f1 1612#endif //0
1613
1614 return (gint64)i;
6cd62ccf 1615}
1616
1617/*****************************************************************************
1618 *Function name
1619 * getDataEndianType : get the data type size and endian type of the local
1620 * machine
1621 *Input params
1622 * size : size of data type
1623 * endian : endian type, little or big
1624 ****************************************************************************/
1625
963b5f2d 1626void getDataEndianType(LttArchSize * size, LttArchEndian * endian)
6cd62ccf 1627{
1628 int i = 1;
1629 char c = (char) i;
1630 int sizeInt=sizeof(int), sizeLong=sizeof(long), sizePointer=sizeof(void *);
1631
1632 if(c == 1) *endian = LTT_LITTLE_ENDIAN;
1633 else *endian = LTT_BIG_ENDIAN;
1634
1635 if(sizeInt == 2 && sizeLong == 4 && sizePointer == 4)
1636 *size = LTT_LP32;
1637 else if(sizeInt == 4 && sizeLong == 4 && sizePointer == 4)
1638 *size = LTT_ILP32;
1639 else if(sizeInt == 4 && sizeLong == 8 && sizePointer == 8)
1640 *size = LTT_LP64;
1641 else if(sizeInt == 8 && sizeLong == 8 && sizePointer == 8)
1642 *size = LTT_ILP64;
1643 else *size = LTT_UNKNOWN;
1644}
1645
a5dcde2f 1646/* get the node name of the system */
1647
1648char * ltt_trace_system_description_node_name (LttSystemDescription * s)
1649{
1650 return s->node_name;
1651}
1652
1653
1654/* get the domain name of the system */
1655
1656char * ltt_trace_system_description_domain_name (LttSystemDescription * s)
1657{
1658 return s->domain_name;
1659}
1660
1661
1662/* get the description of the system */
1663
1664char * ltt_trace_system_description_description (LttSystemDescription * s)
1665{
1666 return s->description;
1667}
1668
1669
1670/* get the start time of the trace */
1671
1672LttTime ltt_trace_system_description_trace_start_time(LttSystemDescription *s)
1673{
1674 return s->trace_start;
1675}
1676
18206708 1677
1678LttTracefile *ltt_tracefile_new()
1679{
1680 return g_new(LttTracefile, 1);
1681}
1682
1683void ltt_tracefile_destroy(LttTracefile *tf)
1684{
1685 g_free(tf);
1686}
1687
1688void ltt_tracefile_copy(LttTracefile *dest, const LttTracefile *src)
1689{
1690 *dest = *src;
1691}
1692
This page took 0.110181 seconds and 4 git commands to generate.