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