6 #include <linux/errno.h>
8 #include <ltt/LTTTypes.h>
10 #include <ltt/trace.h>
12 #define DIR_NAME_SIZE 256
14 /* set the offset of the fields belonging to the event,
15 need the information of the archecture */
16 void setFieldsOffset(LttTracefile
*tf
,LttEventType
*evT
,void *evD
,LttTrace
*t
);
18 /* get the size of the field type according to the archtecture's
19 size and endian type(info of the archecture) */
20 int getFieldtypeSize(LttTracefile
* tf
, LttEventType
* evT
, int offsetRoot
,
21 int offsetParent
, LttField
*fld
, void *evD
, LttTrace
* t
);
23 /* read a fixed size or a block information from the file (fd) */
24 int readFile(int fd
, void * buf
, size_t size
, char * mesg
);
25 int readBlock(LttTracefile
* tf
, int whichBlock
);
27 /* calculate cycles per nsec for current block */
28 void getCyclePerNsec(LttTracefile
* t
);
30 /* reinitialize the info of the block which is already in the buffer */
31 void updateTracefile(LttTracefile
* tf
);
33 /* go to the next event */
34 int skipEvent(LttTracefile
* t
);
36 /* compare two time (LttTime), 0:t1=t2, -1:t1<t2, 1:t1>t2 */
37 int timecmp(LttTime
* t1
, LttTime
* t2
);
39 /* get an integer number */
40 int getIntNumber(int size1
, void *evD
);
43 /* Time operation macros for LttTime (struct timespec) */
45 #define TimeSub(T3, T2, T1) \
48 (T3).tv_sec = (T2).tv_sec - (T1).tv_sec; \
49 (T3).tv_nsec = (T2).tv_nsec - (T1).tv_nsec; \
53 (T3).tv_nsec += 1000000000;\
58 #define TimeAdd(T3, T2, T1) \
61 (T3).tv_sec = (T2).tv_sec + (T1).tv_sec; \
62 (T3).tv_nsec = (T2).tv_nsec + (T1).tv_nsec; \
63 if((T3).tv_nsec >= 1000000000)\
65 (T3).tv_sec += (T3).tv_nsec / 1000000000;\
66 (T3).tv_nsec = (T3).tv_nsec % 1000000000;\
72 /*****************************************************************************
74 * ltt_tracefile_open : open a trace file, construct a LttTracefile
76 * t : the trace containing the tracefile
77 * fileName : path name of the trace file
79 * : a pointer to a tracefile
80 ****************************************************************************/
82 LttTracefile
* ltt_tracefile_open(LttTrace
* t
, char * fileName
)
85 struct stat lTDFStat
; /* Trace data file status */
86 BlockStart a_block_start
;
88 tf
= g_new(LttTracefile
, 1);
91 tf
->name
= g_strdup(fileName
);
93 tf
->fd
= open(fileName
, O_RDONLY
, 0);
95 g_error("Unable to open input data file %s\n", fileName
);
98 // Get the file's status
99 if(fstat(tf
->fd
, &lTDFStat
) < 0){
100 g_error("Unable to get the status of the input data file %s\n", fileName
);
103 // Is the file large enough to contain a trace
104 if(lTDFStat
.st_size
< sizeof(BlockStart
) + EVENT_HEADER_SIZE
){
105 g_print("The input data file %s does not contain a trace\n", fileName
);
109 //store the size of the file
110 tf
->file_size
= lTDFStat
.st_size
;
111 tf
->block_size
= t
->system_description
->ltt_block_size
;
112 tf
->block_number
= tf
->file_size
/ tf
->block_size
;
115 //allocate memory to contain the info of a block
116 tf
->buffer
= (void *) g_new(char, t
->system_description
->ltt_block_size
);
118 //read the first block
119 if(readBlock(tf
,1)) exit(1);
125 /*****************************************************************************
126 *Open control and per cpu tracefiles
127 ****************************************************************************/
129 void ltt_tracefile_open_cpu(LttTrace
*t
, char * tracefile_name
)
132 tf
= ltt_tracefile_open(t
,tracefile_name
);
133 t
->per_cpu_tracefile_number
++;
134 g_ptr_array_add(t
->per_cpu_tracefiles
, tf
);
137 void ltt_tracefile_open_control(LttTrace
*t
, char * control_name
)
147 tf
= ltt_tracefile_open(t
,control_name
);
149 t
->control_tracefile_number
++;
150 g_ptr_array_add(t
->control_tracefiles
,tf
);
152 //parse facilities tracefile to get base_id
153 if(strcmp(control_name
,"facilities") ==0){
155 ev
= ltt_tracefile_read(tf
);
156 if(!ev
)return; // end of file
158 if(ev
->event_id
== TRACE_FACILITY_LOAD
){
160 fLoad
.name
= (char*)pos
;
161 fLoad
.checksum
= *(LttChecksum
*)(pos
+ sizeof(pos
));
162 fLoad
.base_code
= *(uint32_t*)(pos
+ sizeof(pos
) + sizeof(LttChecksum
));
164 for(i
=0;i
<t
->facility_number
;i
++){
165 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
,i
);
166 if(strcmp(f
->name
,fLoad
.name
)==0 && fLoad
.checksum
==f
->checksum
){
167 f
->base_id
= fLoad
.base_code
;
171 if(i
==t
->facility_number
)
172 g_error("Facility: %s, checksum: %d is not founded\n",
173 fLoad
.name
,fLoad
.checksum
);
174 }else if(ev
->event_id
== TRACE_BLOCK_START
){
176 }else if(ev
->event_id
== TRACE_BLOCK_END
){
178 }else g_error("Not valid facilities trace file\n");
183 /*****************************************************************************
185 * ltt_tracefile_close: close a trace file,
187 * t : tracefile which will be closed
188 ****************************************************************************/
190 void ltt_tracefile_close(LttTracefile
*t
)
198 /*****************************************************************************
199 *Get system information
200 ****************************************************************************/
201 void getSystemInfo(LttSystemDescription
* des
, char * pathname
)
205 int entry_number
= 15;
206 char buf
[DIR_NAME_SIZE
];
207 char description
[4*DIR_NAME_SIZE
];
210 fp
= fopen(pathname
,"r");
212 g_error("Can not open file : %s\n", pathname
);
215 while(fgets(buf
,DIR_NAME_SIZE
, fp
)!= NULL
){
217 while(isspace(*ptr
)) ptr
++;
218 if(strlen(ptr
) == 0) continue;
222 if(strlen(ptr
) == 0) g_error("Not a valid file: %s\n", pathname
);
223 if(strncmp("<system",ptr
,7) !=0)g_error("Not a valid file: %s\n", pathname
);
225 for(i
=0;i
<entry_number
;i
++){
226 if(fgets(buf
,DIR_NAME_SIZE
, fp
)== NULL
)
227 g_error("Not a valid file: %s\n", pathname
);
228 buf
[strlen(buf
)-1] = '\0';
230 while(isspace(*ptr
)) ptr
++;
233 if(strncmp("node_name=",ptr
,10)!=0)
234 g_error("Not a valid file: %s\n", pathname
);
235 des
->node_name
= g_strdup(ptr
+10);
238 if(strncmp("domainname=",ptr
,11)!=0)
239 g_error("Not a valid file: %s\n", pathname
);
240 des
->domain_name
= g_strdup(ptr
+11);
243 if(strncmp("cpu=",ptr
,4)!=0)
244 g_error("Not a valid file: %s\n", pathname
);
245 des
->nb_cpu
= (unsigned)atoi(ptr
+4);
248 if(strncmp("arch_size=",ptr
,10)!=0)
249 g_error("Not a valid file: %s\n", pathname
);
250 if(strcmp(ptr
+10,"\"LP32\"") == 0) des
->size
= LTT_LP32
;
251 else if(strcmp(ptr
+10,"\"ILP32\"") == 0) des
->size
= LTT_ILP32
;
252 else if(strcmp(ptr
+10,"\"LP64\"") == 0) des
->size
= LTT_LP64
;
253 else if(strcmp(ptr
+10,"\"ILP64\"") == 0) des
->size
= LTT_ILP64
;
254 else if(strcmp(ptr
+10,"\"UNKNOWN\"") == 0) des
->size
= LTT_UNKNOWN
;
257 if(strncmp("endian=",ptr
,7)!=0)
258 g_error("Not a valid file: %s\n", pathname
);
259 if(strcmp(ptr
+7,"\"LITTLE_ENDIAN\"") == 0)
260 des
->endian
= LTT_LITTLE_ENDIAN
;
261 else if(strcmp(ptr
+7,"\"BIG_ENDIAN\"") == 0)
262 des
->endian
= LTT_BIG_ENDIAN
;
265 if(strncmp("kernel_name=",ptr
,12)!=0)
266 g_error("Not a valid file: %s\n", pathname
);
267 des
->kernel_name
= g_strdup(ptr
+12);
270 if(strncmp("kernel_release=",ptr
,15)!=0)
271 g_error("Not a valid file: %s\n", pathname
);
272 des
->kernel_release
= g_strdup(ptr
+15);
275 if(strncmp("kernel_version=",ptr
,15)!=0)
276 g_error("Not a valid file: %s\n", pathname
);
277 des
->kernel_version
= g_strdup(ptr
+15);
280 if(strncmp("machine=",ptr
,8)!=0)
281 g_error("Not a valid file: %s\n", pathname
);
282 des
->machine
= g_strdup(ptr
+8);
285 if(strncmp("processor=",ptr
,10)!=0)
286 g_error("Not a valid file: %s\n", pathname
);
287 des
->processor
= g_strdup(ptr
+10);
290 if(strncmp("hardware_platform=",ptr
,18)!=0)
291 g_error("Not a valid file: %s\n", pathname
);
292 des
->hardware_platform
= g_strdup(ptr
+18);
295 if(strncmp("operating_system=",ptr
,17)!=0)
296 g_error("Not a valid file: %s\n", pathname
);
297 des
->operating_system
= g_strdup(ptr
+17);
300 if(strncmp("ltt_major_version=",ptr
,18)!=0)
301 g_error("Not a valid file: %s\n", pathname
);
303 // ptr++;//skip begining "
304 // ptr[strlen(ptr)-1] = '\0'; //get rid of the ending "
305 des
->ltt_major_version
= (unsigned)atoi(ptr
);
308 if(strncmp("ltt_minor_version=",ptr
,18)!=0)
309 g_error("Not a valid file: %s\n", pathname
);
311 // ptr++;//skip begining "
312 // ptr[strlen(ptr)-1] = '\0'; //get rid of the ending "
313 des
->ltt_minor_version
= (unsigned)atoi(ptr
);
316 if(strncmp("ltt_block_size=",ptr
,15)!=0)
317 g_error("Not a valid file: %s\n", pathname
);
319 // ptr++;//skip begining "
320 // ptr[strlen(ptr)-1] = '\0'; //get rid of the ending "
321 des
->ltt_block_size
= (unsigned)atoi(ptr
);
324 g_error("Not a valid file: %s\n", pathname
);
329 description
[0] = '\0';
330 if(fgets(buf
,DIR_NAME_SIZE
, fp
)== NULL
)
331 g_error("Not a valid file: %s\n", pathname
);
333 while(isspace(*ptr
)) ptr
++;
334 if(*ptr
!= '>') g_error("Not a valid file: %s\n", pathname
);
335 while((ptr
=fgets(buf
,DIR_NAME_SIZE
, fp
))!= NULL
){
337 while(isspace(*ptr
)) ptr
++;
338 if(strncmp("</system>",ptr
,9) == 0 )break;
339 strcat(description
, buf
);
341 if(!ptr
)g_error("Not a valid file: %s\n", pathname
);
342 if(description
[0] = '\0')des
->description
= NULL
;
343 des
->description
= g_strdup(description
);
348 /*****************************************************************************
349 *The following functions get facility/tracefile information
350 ****************************************************************************/
352 void getFacilityInfo(LttTrace
*t
, char* eventdefs
)
355 struct dirent
*entry
;
360 char name
[DIR_NAME_SIZE
];
362 dir
= opendir(eventdefs
);
363 if(!dir
) g_error("Can not open directory: %s\n", eventdefs
);
365 while((entry
= readdir(dir
)) != NULL
){
366 ptr
= &entry
->d_name
[strlen(entry
->d_name
)-4];
367 if(strcmp(ptr
,".xml") != 0) continue;
368 strcpy(name
,eventdefs
);
369 strcat(name
,entry
->d_name
);
370 ltt_facility_open(t
,name
);
374 for(j
=0;j
<t
->facility_number
;j
++){
375 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, j
);
376 for(i
=0; i
<f
->event_number
; i
++){
378 setFieldsOffset(NULL
, et
, NULL
, t
);
383 void getControlFileInfo(LttTrace
*t
, char* control
)
386 struct dirent
*entry
;
387 char name
[DIR_NAME_SIZE
];
389 dir
= opendir(control
);
390 if(!dir
) g_error("Can not open directory: %s\n", control
);
392 while((entry
= readdir(dir
)) != NULL
){
393 if(strcmp(entry
->d_name
,"facilities") != 0 &&
394 strcmp(entry
->d_name
,"interrupts") != 0 &&
395 strcmp(entry
->d_name
,"processes") != 0) continue;
397 strcpy(name
,control
);
398 strcat(name
,entry
->d_name
);
399 ltt_tracefile_open_control(t
,name
);
404 void getCpuFileInfo(LttTrace
*t
, char* cpu
)
407 struct dirent
*entry
;
408 char name
[DIR_NAME_SIZE
];
411 if(!dir
) g_error("Can not open directory: %s\n", cpu
);
413 while((entry
= readdir(dir
)) != NULL
){
414 if(strcmp(entry
->d_name
,".") != 0 &&
415 strcmp(entry
->d_name
,"..") != 0 ){
417 strcat(name
,entry
->d_name
);
418 ltt_tracefile_open_cpu(t
,name
);
424 /*****************************************************************************
425 *A trace is specified as a pathname to the directory containing all the
426 *associated data (control tracefiles, per cpu tracefiles, event
429 *When a trace is closed, all the associated facilities, types and fields
430 *are released as well.
431 ****************************************************************************/
433 LttTrace
*ltt_trace_open(char *pathname
)
436 LttSystemDescription
* sys_description
;
437 char eventdefs
[DIR_NAME_SIZE
];
438 char info
[DIR_NAME_SIZE
];
439 char control
[DIR_NAME_SIZE
];
440 char cpu
[DIR_NAME_SIZE
];
441 char tmp
[DIR_NAME_SIZE
];
442 gboolean has_slash
= FALSE
;
444 //establish the pathname to different directories
445 if(pathname
[strlen(pathname
)-1] == '/')has_slash
= TRUE
;
446 strcpy(eventdefs
,pathname
);
447 if(!has_slash
)strcat(eventdefs
,"/");
448 strcat(eventdefs
,"eventdefs/");
450 strcpy(info
,pathname
);
451 if(!has_slash
)strcat(info
,"/");
452 strcat(info
,"info/");
454 strcpy(control
,pathname
);
455 if(!has_slash
)strcat(control
,"/");
456 strcat(control
,"control/");
458 strcpy(cpu
,pathname
);
459 if(!has_slash
)strcat(cpu
,"/");
463 t
= g_new(LttTrace
, 1);
464 sys_description
= g_new(LttSystemDescription
, 1);
465 t
->pathname
= g_strdup(pathname
);
466 t
->facility_number
= 0;
467 t
->control_tracefile_number
= 0;
468 t
->per_cpu_tracefile_number
= 0;
469 t
->system_description
= sys_description
;
470 t
->control_tracefiles
= g_ptr_array_new();
471 t
->per_cpu_tracefiles
= g_ptr_array_new();
472 t
->facilities
= g_ptr_array_new();
473 getDataEndianType(&(t
->my_arch_size
), &(t
->my_arch_endian
));
475 //get system description
477 strcat(tmp
,"system.xml");
478 getSystemInfo(sys_description
, tmp
);
480 //get control tracefile info
481 getControlFileInfo(t
,control
);
483 //get cpu tracefile info
484 getCpuFileInfo(t
,cpu
);
486 //get facilities info
487 getFacilityInfo(t
,eventdefs
);
492 void ltt_trace_close(LttTrace
*t
)
500 //free system_description
501 g_free(t
->system_description
->description
);
502 g_free(t
->system_description
->node_name
);
503 g_free(t
->system_description
->domain_name
);
504 g_free(t
->system_description
->kernel_name
);
505 g_free(t
->system_description
->kernel_release
);
506 g_free(t
->system_description
->kernel_version
);
507 g_free(t
->system_description
->machine
);
508 g_free(t
->system_description
->processor
);
509 g_free(t
->system_description
->hardware_platform
);
510 g_free(t
->system_description
->operating_system
);
511 g_free(t
->system_description
);
513 //free control_tracefiles
514 for(i
=0;i
<t
->control_tracefile_number
;i
++){
515 tf
= (LttTracefile
*)g_ptr_array_index(t
->control_tracefiles
,i
);
516 ltt_tracefile_close(tf
);
518 g_ptr_array_free(t
->control_tracefiles
, FALSE
);
520 //free per_cpu_tracefiles
521 for(i
=0;i
<t
->per_cpu_tracefile_number
;i
++){
522 tf
= (LttTracefile
*)g_ptr_array_index(t
->per_cpu_tracefiles
,i
);
523 ltt_tracefile_close(tf
);
525 g_ptr_array_free(t
->per_cpu_tracefiles
, FALSE
);
528 for(i
=0;i
<t
->facility_number
;i
++){
529 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
,i
);
530 ltt_facility_close(f
);
532 g_ptr_array_free(t
->facilities
, FALSE
);
538 /*****************************************************************************
539 *Get the system description of the trace
540 ****************************************************************************/
542 LttSystemDescription
*ltt_trace_system_description(LttTrace
*t
)
544 return t
->system_description
;
547 /*****************************************************************************
548 * The following functions discover the facilities of the trace
549 ****************************************************************************/
551 unsigned ltt_trace_facility_number(LttTrace
*t
)
553 return (unsigned)(t
->facility_number
);
556 LttFacility
*ltt_trace_facility_get(LttTrace
*t
, unsigned i
)
558 return (LttFacility
*)g_ptr_array_index(t
->facilities
, i
);
561 /*****************************************************************************
563 * ltt_trace_facility_find : find facilities in the trace
566 * name : facility name
568 * position : position of the facility in the trace
570 * : the number of facilities
571 ****************************************************************************/
573 unsigned ltt_trace_facility_find(LttTrace
*t
, char *name
, unsigned *position
)
577 for(i
=0;i
=t
->facility_number
;i
++){
578 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, i
);
579 if(strcmp(f
->name
,name
)==0){
581 if(count
==1) *position
= i
;
589 /*****************************************************************************
590 * Functions to discover all the event types in the trace
591 ****************************************************************************/
593 unsigned ltt_trace_eventtype_number(LttTrace
*t
)
598 for(i
=0;i
=t
->facility_number
;i
++){
599 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, i
);
600 count
+= f
->event_number
;
605 LttFacility
* ltt_trace_facility_by_id(LttTrace
* trace
, unsigned id
)
607 LttFacility
* facility
;
609 for(i
=0;i
<trace
->facility_number
;i
++){
610 facility
= (LttFacility
*) g_ptr_array_index(trace
->facilities
,i
);
611 if(id
>= facility
->base_id
&&
612 id
< facility
->base_id
+ facility
->event_number
)
615 if(i
==trace
->facility_number
) return NULL
;
616 else return facility
;
619 LttEventType
*ltt_trace_eventtype_get(LttTrace
*t
, unsigned evId
)
622 f
= ltt_trace_facility_by_id(t
,evId
);
624 return f
->events
[evId
- f
->base_id
];
627 /*****************************************************************************
628 *There is one "per cpu" tracefile for each CPU, numbered from 0 to
629 *the maximum number of CPU in the system. When the number of CPU installed
630 *is less than the maximum, some positions are unused. There are also a
631 *number of "control" tracefiles (facilities, interrupts...).
632 ****************************************************************************/
633 unsigned ltt_trace_control_tracefile_number(LttTrace
*t
)
635 return t
->control_tracefile_number
;
638 unsigned ltt_trace_per_cpu_tracefile_number(LttTrace
*t
)
640 return t
->per_cpu_tracefile_number
;
643 /*****************************************************************************
644 *It is possible to search for the tracefiles by name or by CPU position.
645 *The index within the tracefiles of the same type is returned if found
646 *and a negative value otherwise.
647 ****************************************************************************/
649 int ltt_trace_control_tracefile_find(LttTrace
*t
, char *name
)
651 LttTracefile
* tracefile
;
653 for(i
=0;i
<t
->control_tracefile_number
;i
++){
654 tracefile
= (LttTracefile
*)g_ptr_array_index(t
->control_tracefiles
, i
);
655 if(strcmp(tracefile
->name
, name
)==0)break;
657 if(i
== t
->control_tracefile_number
) return -1;
661 int ltt_trace_per_cpu_tracefile_find(LttTrace
*t
, unsigned i
)
663 LttTracefile
* tracefile
;
665 for(j
=0;j
<t
->per_cpu_tracefile_number
;j
++){
666 tracefile
= (LttTracefile
*)g_ptr_array_index(t
->per_cpu_tracefiles
, j
);
667 name
= atoi(tracefile
->name
);
668 if(name
== (int)i
)break;
670 if(j
== t
->per_cpu_tracefile_number
) return -1;
674 /*****************************************************************************
675 *Get a specific tracefile
676 ****************************************************************************/
678 LttTracefile
*ltt_trace_control_tracefile_get(LttTrace
*t
, unsigned i
)
680 return (LttTracefile
*)g_ptr_array_index(t
->per_cpu_tracefiles
, i
);
683 LttTracefile
*ltt_trace_per_cpu_tracefile_get(LttTrace
*t
, unsigned i
)
685 return (LttTracefile
*)g_ptr_array_index(t
->per_cpu_tracefiles
, i
);
688 /*****************************************************************************
689 *Get the name of a tracefile
690 ****************************************************************************/
692 char *ltt_tracefile_name(LttTracefile
*tf
)
697 /*****************************************************************************
699 * ltt_tracefile_seek_time: seek to the first event of the trace with time
700 * larger or equal to time
703 * time : criteria of the time
704 ****************************************************************************/
706 void ltt_tracefile_seek_time(LttTracefile
*t
, LttTime time
)
710 int headTime
= timecmp(&(t
->a_block_start
->time
), &time
);
711 int tailTime
= timecmp(&(t
->a_block_end
->time
), &time
);
713 if(headTime
< 0 && tailTime
> 0){
714 lttTime
= getEventTime(t
);
715 err
= timecmp(<tTime
, &time
);
717 if(t
->which_event
==2 || timecmp(&t
->prev_event_time
,&time
)<0){
721 return ltt_tracefile_seek_time(t
, time
);
724 err
= t
->which_block
;
725 if(ltt_tracefile_read(t
) == NULL
){
726 g_print("End of file\n");
729 if(t
->which_block
== err
)
730 return ltt_tracefile_seek_time(t
,time
);
732 }else if(headTime
> 0){
733 if(t
->which_block
== 1){
736 if(timecmp(&(t
->prev_block_end_time
),&time
) >= 0 ){
737 err
=readBlock(t
,t
->which_block
-1);
738 if(err
) g_error("Can not read tracefile: %s\n", t
->name
);
739 return ltt_tracefile_seek_time(t
, time
) ;
744 }else if(tailTime
< 0){
745 if(t
->which_block
!= t
->block_number
){
746 err
=readBlock(t
,t
->which_block
+1);
747 if(err
) g_error("Can not read tracefile: %s\n", t
->name
);
749 g_print("End of file\n");
752 if(tailTime
< 0) return ltt_tracefile_seek_time(t
, time
);
753 }else if(headTime
== 0){
755 }else if(tailTime
== 0){
756 t
->cur_event_pos
= t
->a_block_end
- EVENT_HEADER_SIZE
;
761 /*****************************************************************************
763 * ltt_tracefile_read : read the current event, set the pointer to the next
767 * LttEvent * : an event to be processed
768 ****************************************************************************/
770 LttEvent
*ltt_tracefile_read(LttTracefile
*t
)
772 LttEvent
* lttEvent
= (LttEvent
*)g_new(LttEvent
, 1);
775 lttEvent
->event_id
= (int)(*(uint16_t *)(t
->cur_event_pos
));
776 if(lttEvent
->event_id
== TRACE_TIME_HEARTBEAT
)
777 t
->cur_heart_beat_number
++;
779 t
->prev_event_time
= t
->current_event_time
;
780 t
->current_event_time
= getEventTime(t
);
782 lttEvent
->time_delta
= *(uint32_t*)(t
->cur_event_pos
+ EVENT_ID_SIZE
);
783 lttEvent
->event_time
= t
->current_event_time
;
785 lttEvent
->event_cycle_count
= ((uint64_t)1)<<32 * t
->cur_heart_beat_number
786 + lttEvent
->time_delta
;
788 lttEvent
->tracefile
= t
;
789 lttEvent
->data
= t
->cur_event_pos
+ EVENT_HEADER_SIZE
;
791 //update the fields of the current event and go to the next event
793 if(err
== ENOENT
) return NULL
;
794 if(err
== ERANGE
) g_error("event id is out of range\n");
795 if(err
)g_error("Can not read tracefile\n");
800 /****************************************************************************
802 * readFile : wrap function to read from a file
804 * fd : file descriptor
805 * buf : buf to contain the content
806 * size : number of bytes to be read
807 * mesg : message to be printed if some thing goes wrong
810 * EIO : can not read from the file
811 ****************************************************************************/
813 int readFile(int fd
, void * buf
, size_t size
, char * mesg
)
816 nbBytes
= read(fd
, buf
, size
);
824 /****************************************************************************
826 * readBlock : read a block from the file
828 * lttdes : ltt trace file
829 * whichBlock : the block which will be read
832 * EINVAL : lseek fail
833 * EIO : can not read from the file
834 ****************************************************************************/
836 int readBlock(LttTracefile
* tf
, int whichBlock
)
841 if(whichBlock
- tf
->which_block
== 1 && tf
->which_block
!= 0){
842 tf
->prev_block_end_time
= tf
->a_block_end
->time
;
843 tf
->prev_event_time
= tf
->a_block_end
->time
;
844 tf
->current_event_time
= tf
->a_block_end
->time
;
846 tf
->prev_block_end_time
.tv_sec
= 0;
847 tf
->prev_block_end_time
.tv_nsec
= 0;
848 tf
->prev_event_time
.tv_sec
= 0;
849 tf
->prev_event_time
.tv_nsec
= 0;
850 tf
->current_event_time
.tv_sec
= 0;
851 tf
->current_event_time
.tv_nsec
= 0;
854 nbBytes
=lseek(tf
->fd
,(off_t
)((whichBlock
-1)*tf
->block_size
), SEEK_SET
);
855 if(nbBytes
== -1) return EINVAL
;
857 if(readFile(tf
->fd
,tf
->buffer
,tf
->block_size
,"Unable to read a block"))
860 tf
->a_block_start
=(BlockStart
*) (tf
->buffer
+ EVENT_HEADER_SIZE
);
861 lostSize
= *(uint32_t*)(tf
->buffer
+ tf
->block_size
- sizeof(uint32_t));
862 tf
->a_block_end
=(BlockEnd
*)(tf
->buffer
+ tf
->block_size
-
863 lostSize
+ EVENT_HEADER_SIZE
);
865 tf
->which_block
= whichBlock
;
867 tf
->cur_event_pos
= tf
->buffer
;//the beginning of the block, block start ev
868 tf
->cur_heart_beat_number
= 0;
872 // tf->current_event_time = getEventTime(tf);
877 /*****************************************************************************
879 * updateTracefile : reinitialize the info of the block which is already
883 ****************************************************************************/
885 void updateTracefile(LttTracefile
* tf
)
888 tf
->cur_event_pos
= tf
->buffer
;
889 // tf->current_event_time = getEventTime(tf);
890 tf
->current_event_time
.tv_sec
= 0;
891 tf
->current_event_time
.tv_nsec
= 0;
892 tf
->cur_heart_beat_number
= 0;
894 tf
->prev_event_time
.tv_sec
= 0;
895 tf
->prev_event_time
.tv_nsec
= 0;
898 /*****************************************************************************
900 * skipEvent : go to the next event, update the fields of the current event
905 * EINVAL : lseek fail
906 * EIO : can not read from the file
907 * ENOENT : end of file
908 * ERANGE : event id is out of range
909 ****************************************************************************/
911 int skipEvent(LttTracefile
* t
)
918 evId
= (int)(*(uint16_t *)(t
->cur_event_pos
));
919 evData
= t
->cur_event_pos
+ EVENT_HEADER_SIZE
;
920 evT
= ltt_trace_eventtype_get(t
->trace
,(unsigned)evId
);
922 if(evT
) rootFld
= evT
->root_field
;
925 //event has string/sequence or the last event is not the same event
926 if((evT
->latest_block
!=t
->which_block
|| evT
->latest_event
!=t
->which_event
)
927 && rootFld
->field_fixed
== 0){
928 setFieldsOffset(t
, evT
, evData
, t
->trace
);
930 t
->cur_event_pos
+= EVENT_HEADER_SIZE
+ rootFld
->field_size
;
932 evT
->latest_block
= t
->which_block
;
933 evT
->latest_event
= t
->which_event
;
935 //the next event is in the next block
936 if(evId
== TRACE_BLOCK_END
){
937 if(t
->which_block
== t
->block_number
) return ENOENT
;
938 err
= readBlock(t
, t
->which_block
+ 1);
947 /*****************************************************************************
949 * getCyclePerNsec : calculate cycles per nsec for current block
952 ****************************************************************************/
954 void getCyclePerNsec(LttTracefile
* t
)
956 LttTime lBufTotalTime
; /* Total time for this buffer */
957 LttCycleCount lBufTotalNSec
; /* Total time for this buffer in nsecs */
958 LttCycleCount lBufTotalCycle
;/* Total cycles for this buffer */
960 /* Calculate the total time for this buffer */
961 TimeSub(lBufTotalTime
,t
->a_block_end
->time
, t
->a_block_start
->time
);
963 /* Calculate the total cycles for this bufffer */
964 lBufTotalCycle
= t
->a_block_end
->cycle_count
965 - t
->a_block_start
->cycle_count
;
967 /* Convert the total time to nsecs */
968 lBufTotalNSec
= lBufTotalTime
.tv_sec
* 1000000000 + lBufTotalTime
.tv_nsec
;
970 t
->cycle_per_nsec
= (double)lBufTotalCycle
/ (double)lBufTotalNSec
;
973 /****************************************************************************
975 * getEventTime : obtain the time of an event
979 * LttTime : the time of the event
980 ****************************************************************************/
982 LttTime
getEventTime(LttTracefile
* tf
)
985 LttCycleCount cycle_count
; // cycle count for the current event
986 LttCycleCount lEventTotalCycle
; // Total cycles from start for event
987 double lEventNSec
; // Total usecs from start for event
988 LttTime lTimeOffset
; // Time offset in struct LttTime
991 evId
= *(uint16_t*)tf
->cur_event_pos
;
992 if(evId
== TRACE_BLOCK_START
)
993 return tf
->a_block_start
->time
;
994 else if(evId
== TRACE_BLOCK_END
)
995 return tf
->a_block_end
->time
;
998 // Calculate total time in cycles from start of buffer for this event
999 cycle_count
= (LttCycleCount
)*(uint32_t*)(tf
->cur_event_pos
+ EVENT_ID_SIZE
);
1000 if(tf
->cur_heart_beat_number
)
1001 cycle_count
+= ((uint64_t)1)<<32 * tf
->cur_heart_beat_number
;
1002 lEventTotalCycle
= cycle_count
- tf
->a_block_start
->cycle_count
;
1004 // Convert it to nsecs
1005 lEventNSec
= lEventTotalCycle
/ tf
->cycle_per_nsec
;
1007 // Determine offset in struct LttTime
1008 lTimeOffset
.tv_nsec
= (long)lEventNSec
% 1000000000;
1009 lTimeOffset
.tv_sec
= (long)lEventNSec
/ 1000000000;
1011 TimeAdd(time
, tf
->a_block_start
->time
, lTimeOffset
);
1016 /*****************************************************************************
1018 * setFieldsOffset : set offset of the fields
1020 * tracefile : opened trace file
1021 * evT : the event type
1022 * evD : event data, it may be NULL
1023 ****************************************************************************/
1025 void setFieldsOffset(LttTracefile
*tf
,LttEventType
*evT
,void *evD
,LttTrace
* t
)
1027 LttField
* rootFld
= evT
->root_field
;
1028 // rootFld->base_address = evD;
1031 rootFld
->field_size
= getFieldtypeSize(tf
, evT
, 0,0,rootFld
, evD
,t
);
1034 /*****************************************************************************
1036 * getFieldtypeSize: get the size of the field type (primitive type)
1038 * tracefile : opened trace file
1040 * offsetRoot : offset from the root
1041 * offsetParent : offset from the parrent
1043 * evD : event data, it may be NULL
1045 * int : size of the field
1046 ****************************************************************************/
1048 int getFieldtypeSize(LttTracefile
* t
, LttEventType
* evT
, int offsetRoot
,
1049 int offsetParent
, LttField
* fld
, void *evD
, LttTrace
*trace
)
1051 int size
, size1
, element_number
, i
, offset1
, offset2
;
1052 LttType
* type
= fld
->field_type
;
1055 if(evT
->latest_block
==t
->which_block
&& evT
->latest_event
==t
->which_event
){
1056 return fld
->field_size
;
1060 if(fld
->field_fixed
== 1){
1061 if(fld
== evT
->root_field
) return fld
->field_size
;
1064 if(type
->type_class
!= LTT_STRUCT
&& type
->type_class
!= LTT_ARRAY
&&
1065 type
->type_class
!= LTT_SEQUENCE
&& type
->type_class
!= LTT_STRING
){
1066 if(fld
->field_fixed
== -1){
1067 size
= (int) ltt_type_size(trace
, type
);
1068 fld
->field_fixed
= 1;
1069 }else size
= fld
->field_size
;
1071 }else if(type
->type_class
== LTT_ARRAY
){
1072 element_number
= (int) type
->element_number
;
1073 if(fld
->field_fixed
== -1){
1074 size
= getFieldtypeSize(t
, evT
, offsetRoot
,0,fld
->child
[0], NULL
, trace
);
1075 if(size
== 0){ //has string or sequence
1076 fld
->field_fixed
= 0;
1078 fld
->field_fixed
= 1;
1079 size
*= element_number
;
1081 }else if(fld
->field_fixed
== 0){// has string or sequence
1083 for(i
=0;i
<element_number
;i
++){
1084 size
+= getFieldtypeSize(t
, evT
, offsetRoot
+size
,size
,
1085 fld
->child
[0], evD
+size
, trace
);
1087 }else size
= fld
->field_size
;
1089 }else if(type
->type_class
== LTT_SEQUENCE
){
1090 size1
= (int) ltt_type_size(trace
, type
);
1091 if(fld
->field_fixed
== -1){
1092 fld
->field_fixed
= 0;
1093 size
= getFieldtypeSize(t
, evT
, offsetRoot
,0,fld
->child
[0], NULL
, trace
);
1094 fld
->element_size
= size
;
1096 element_number
= getIntNumber(size1
,evD
);
1097 type
->element_number
= element_number
;
1098 if(fld
->element_size
> 0){
1099 size
= element_number
* fld
->element_size
;
1100 }else{//sequence has string or sequence
1102 for(i
=0;i
<element_number
;i
++){
1103 size
+= getFieldtypeSize(t
, evT
, offsetRoot
+size
+size1
,size
+size1
,
1104 fld
->child
[0], evD
+size
+size1
, trace
);
1110 }else if(type
->type_class
== LTT_STRING
){
1112 if(fld
->field_fixed
== -1){
1113 fld
->field_fixed
= 0;
1115 size
= sizeof((char*)evD
) + 1; //include end : '\0'
1118 }else if(type
->type_class
== LTT_STRUCT
){
1119 element_number
= (int) type
->element_number
;
1121 if(fld
->field_fixed
== -1){
1122 offset1
= offsetRoot
;
1124 for(i
=0;i
<element_number
;i
++){
1125 size1
=getFieldtypeSize(t
, evT
,offset1
,offset2
, fld
->child
[i
], NULL
, trace
);
1126 if(size1
> 0 && size
>= 0){
1128 if(offset1
>= 0) offset1
+= size1
;
1137 fld
->field_fixed
= 0;
1139 }else fld
->field_fixed
= 1;
1140 }else if(fld
->field_fixed
== 0){
1141 offset1
= offsetRoot
;
1143 for(i
=0;i
<element_number
;i
++){
1144 size
=getFieldtypeSize(t
,evT
,offset1
,offset2
,fld
->child
[i
],evD
+offset2
, trace
);
1149 }else size
= fld
->field_size
;
1152 fld
->offset_root
= offsetRoot
;
1153 fld
->offset_parent
= offsetParent
;
1155 fld
->fixed_root
= (offsetRoot
==-1) ? 0 : 1;
1156 fld
->fixed_parent
= (offsetParent
==-1) ? 0 : 1;
1158 fld
->field_size
= size
;
1163 /*****************************************************************************
1165 * timecmp : compare two time
1170 * int : 0: t1 == t2; -1: t1 < t2; 1: t1 > t2
1171 ****************************************************************************/
1173 int timecmp(LttTime
* t1
, LttTime
* t2
)
1176 TimeSub(T
, *t1
, *t2
);
1177 if(T
.tv_sec
== 0 && T
.tv_nsec
== 0) return 0;
1178 else if(T
.tv_sec
> 0 || (T
.tv_sec
==0 && T
.tv_nsec
> 0)) return 1;
1182 /*****************************************************************************
1184 * getIntNumber : get an integer number
1186 * size : the size of the integer
1187 * evD : the event data
1190 ****************************************************************************/
1192 int getIntNumber(int size
, void *evD
)
1195 if(size
== 1) i
= *(int8_t *)evD
;
1196 else if(size
== 2) i
= *(int16_t *)evD
;
1197 else if(size
== 4) i
= *(int32_t *)evD
;
1198 else if(size
== 8) i
= *(int64_t *)evD
;
1203 /*****************************************************************************
1205 * getDataEndianType : get the data type size and endian type of the local
1208 * size : size of data type
1209 * endian : endian type, little or big
1210 ****************************************************************************/
1212 void getDataEndianType(LttArchSize
* size
, LttArchEndian
* endian
)
1216 int sizeInt
=sizeof(int), sizeLong
=sizeof(long), sizePointer
=sizeof(void *);
1218 if(c
== 1) *endian
= LTT_LITTLE_ENDIAN
;
1219 else *endian
= LTT_BIG_ENDIAN
;
1221 if(sizeInt
== 2 && sizeLong
== 4 && sizePointer
== 4)
1223 else if(sizeInt
== 4 && sizeLong
== 4 && sizePointer
== 4)
1225 else if(sizeInt
== 4 && sizeLong
== 8 && sizePointer
== 8)
1227 else if(sizeInt
== 8 && sizeLong
== 8 && sizePointer
== 8)
1229 else *size
= LTT_UNKNOWN
;