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
);
112 //store the size of the file
113 tf
->file_size
= lTDFStat
.st_size
;
114 tf
->block_size
= t
->system_description
->ltt_block_size
;
115 tf
->block_number
= tf
->file_size
/ tf
->block_size
;
118 //allocate memory to contain the info of a block
119 tf
->buffer
= (void *) g_new(char, t
->system_description
->ltt_block_size
);
121 //read the first block
122 if(readBlock(tf
,1)) exit(1);
128 /*****************************************************************************
129 *Open control and per cpu tracefiles
130 ****************************************************************************/
132 void ltt_tracefile_open_cpu(LttTrace
*t
, char * tracefile_name
)
135 tf
= ltt_tracefile_open(t
,tracefile_name
);
137 t
->per_cpu_tracefile_number
++;
138 g_ptr_array_add(t
->per_cpu_tracefiles
, tf
);
141 void ltt_tracefile_open_control(LttTrace
*t
, char * control_name
)
151 tf
= ltt_tracefile_open(t
,control_name
);
153 t
->control_tracefile_number
++;
154 g_ptr_array_add(t
->control_tracefiles
,tf
);
156 //parse facilities tracefile to get base_id
157 if(strcmp(&control_name
[strlen(control_name
)-10],"facilities") ==0){
159 ev
= ltt_tracefile_read(tf
);
160 if(!ev
)return; // end of file
162 if(ev
->event_id
== TRACE_FACILITY_LOAD
){
164 fLoad
.name
= (char*)pos
;
165 fLoad
.checksum
= *(LttChecksum
*)(pos
+ strlen(fLoad
.name
));
166 fLoad
.base_code
= *(uint32_t*)(pos
+ strlen(fLoad
.name
) + sizeof(LttChecksum
));
168 for(i
=0;i
<t
->facility_number
;i
++){
169 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
,i
);
170 if(strcmp(f
->name
,fLoad
.name
)==0 && fLoad
.checksum
==f
->checksum
){
171 f
->base_id
= fLoad
.base_code
;
175 if(i
==t
->facility_number
)
176 g_error("Facility: %s, checksum: %d is not founded\n",
177 fLoad
.name
,fLoad
.checksum
);
178 }else if(ev
->event_id
== TRACE_BLOCK_START
){
180 }else if(ev
->event_id
== TRACE_BLOCK_END
){
182 }else g_error("Not valid facilities trace file\n");
187 /*****************************************************************************
189 * ltt_tracefile_close: close a trace file,
191 * t : tracefile which will be closed
192 ****************************************************************************/
194 void ltt_tracefile_close(LttTracefile
*t
)
203 /*****************************************************************************
204 *Get system information
205 ****************************************************************************/
206 void getSystemInfo(LttSystemDescription
* des
, char * pathname
)
210 int entry_number
= 15;
211 char buf
[DIR_NAME_SIZE
];
212 char description
[4*DIR_NAME_SIZE
];
215 fp
= fopen(pathname
,"r");
217 g_error("Can not open file : %s\n", pathname
);
220 while(fgets(buf
,DIR_NAME_SIZE
, fp
)!= NULL
){
222 while(isspace(*ptr
)) ptr
++;
223 if(strlen(ptr
) == 0) continue;
227 if(strlen(ptr
) == 0) g_error("Not a valid file: %s\n", pathname
);
228 if(strncmp("<system",ptr
,7) !=0)g_error("Not a valid file: %s\n", pathname
);
230 for(i
=0;i
<entry_number
;i
++){
231 if(fgets(buf
,DIR_NAME_SIZE
, fp
)== NULL
)
232 g_error("Not a valid file: %s\n", pathname
);
233 buf
[strlen(buf
)-1] = '\0';
235 while(isspace(*ptr
)) ptr
++;
238 if(strncmp("node_name=",ptr
,10)!=0)
239 g_error("Not a valid file: %s\n", pathname
);
240 des
->node_name
= g_strdup(ptr
+10);
243 if(strncmp("domainname=",ptr
,11)!=0)
244 g_error("Not a valid file: %s\n", pathname
);
245 des
->domain_name
= g_strdup(ptr
+11);
248 if(strncmp("cpu=",ptr
,4)!=0)
249 g_error("Not a valid file: %s\n", pathname
);
250 des
->nb_cpu
= (unsigned)atoi(ptr
+4);
253 if(strncmp("arch_size=",ptr
,10)!=0)
254 g_error("Not a valid file: %s\n", pathname
);
255 if(strcmp(ptr
+10,"\"LP32\"") == 0) des
->size
= LTT_LP32
;
256 else if(strcmp(ptr
+10,"\"ILP32\"") == 0) des
->size
= LTT_ILP32
;
257 else if(strcmp(ptr
+10,"\"LP64\"") == 0) des
->size
= LTT_LP64
;
258 else if(strcmp(ptr
+10,"\"ILP64\"") == 0) des
->size
= LTT_ILP64
;
259 else if(strcmp(ptr
+10,"\"UNKNOWN\"") == 0) des
->size
= LTT_UNKNOWN
;
262 if(strncmp("endian=",ptr
,7)!=0)
263 g_error("Not a valid file: %s\n", pathname
);
264 if(strcmp(ptr
+7,"\"LITTLE_ENDIAN\"") == 0)
265 des
->endian
= LTT_LITTLE_ENDIAN
;
266 else if(strcmp(ptr
+7,"\"BIG_ENDIAN\"") == 0)
267 des
->endian
= LTT_BIG_ENDIAN
;
270 if(strncmp("kernel_name=",ptr
,12)!=0)
271 g_error("Not a valid file: %s\n", pathname
);
272 des
->kernel_name
= g_strdup(ptr
+12);
275 if(strncmp("kernel_release=",ptr
,15)!=0)
276 g_error("Not a valid file: %s\n", pathname
);
277 des
->kernel_release
= g_strdup(ptr
+15);
280 if(strncmp("kernel_version=",ptr
,15)!=0)
281 g_error("Not a valid file: %s\n", pathname
);
282 des
->kernel_version
= g_strdup(ptr
+15);
285 if(strncmp("machine=",ptr
,8)!=0)
286 g_error("Not a valid file: %s\n", pathname
);
287 des
->machine
= g_strdup(ptr
+8);
290 if(strncmp("processor=",ptr
,10)!=0)
291 g_error("Not a valid file: %s\n", pathname
);
292 des
->processor
= g_strdup(ptr
+10);
295 if(strncmp("hardware_platform=",ptr
,18)!=0)
296 g_error("Not a valid file: %s\n", pathname
);
297 des
->hardware_platform
= g_strdup(ptr
+18);
300 if(strncmp("operating_system=",ptr
,17)!=0)
301 g_error("Not a valid file: %s\n", pathname
);
302 des
->operating_system
= g_strdup(ptr
+17);
305 if(strncmp("ltt_major_version=",ptr
,18)!=0)
306 g_error("Not a valid file: %s\n", pathname
);
308 // ptr++;//skip begining "
309 // ptr[strlen(ptr)-1] = '\0'; //get rid of the ending "
310 des
->ltt_major_version
= (unsigned)atoi(ptr
);
313 if(strncmp("ltt_minor_version=",ptr
,18)!=0)
314 g_error("Not a valid file: %s\n", pathname
);
316 // ptr++;//skip begining "
317 // ptr[strlen(ptr)-1] = '\0'; //get rid of the ending "
318 des
->ltt_minor_version
= (unsigned)atoi(ptr
);
321 if(strncmp("ltt_block_size=",ptr
,15)!=0)
322 g_error("Not a valid file: %s\n", pathname
);
324 // ptr++;//skip begining "
325 // ptr[strlen(ptr)-1] = '\0'; //get rid of the ending "
326 des
->ltt_block_size
= (unsigned)atoi(ptr
);
329 g_error("Not a valid file: %s\n", pathname
);
334 description
[0] = '\0';
335 if(fgets(buf
,DIR_NAME_SIZE
, fp
)== NULL
)
336 g_error("Not a valid file: %s\n", pathname
);
338 while(isspace(*ptr
)) ptr
++;
339 if(*ptr
!= '>') g_error("Not a valid file: %s\n", pathname
);
340 while((ptr
=fgets(buf
,DIR_NAME_SIZE
, fp
))!= NULL
){
342 while(isspace(*ptr
)) ptr
++;
343 if(strncmp("</system>",ptr
,9) == 0 )break;
344 strcat(description
, buf
);
346 if(!ptr
)g_error("Not a valid file: %s\n", pathname
);
347 if(description
[0] = '\0')des
->description
= NULL
;
348 des
->description
= g_strdup(description
);
353 /*****************************************************************************
354 *The following functions get facility/tracefile information
355 ****************************************************************************/
357 void getFacilityInfo(LttTrace
*t
, char* eventdefs
)
360 struct dirent
*entry
;
365 char name
[DIR_NAME_SIZE
];
367 dir
= opendir(eventdefs
);
368 if(!dir
) g_error("Can not open directory: %s\n", eventdefs
);
370 while((entry
= readdir(dir
)) != NULL
){
371 ptr
= &entry
->d_name
[strlen(entry
->d_name
)-4];
372 if(strcmp(ptr
,".xml") != 0) continue;
373 strcpy(name
,eventdefs
);
374 strcat(name
,entry
->d_name
);
375 ltt_facility_open(t
,name
);
379 for(j
=0;j
<t
->facility_number
;j
++){
380 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, j
);
381 for(i
=0; i
<f
->event_number
; i
++){
383 setFieldsOffset(NULL
, et
, NULL
, t
);
388 void getControlFileInfo(LttTrace
*t
, char* control
)
391 struct dirent
*entry
;
392 char name
[DIR_NAME_SIZE
];
394 dir
= opendir(control
);
395 if(!dir
) g_error("Can not open directory: %s\n", control
);
397 while((entry
= readdir(dir
)) != NULL
){
398 if(strcmp(entry
->d_name
,"facilities") != 0 &&
399 strcmp(entry
->d_name
,"interrupts") != 0 &&
400 strcmp(entry
->d_name
,"processes") != 0) continue;
402 strcpy(name
,control
);
403 strcat(name
,entry
->d_name
);
404 ltt_tracefile_open_control(t
,name
);
409 void getCpuFileInfo(LttTrace
*t
, char* cpu
)
412 struct dirent
*entry
;
413 char name
[DIR_NAME_SIZE
];
416 if(!dir
) g_error("Can not open directory: %s\n", cpu
);
418 while((entry
= readdir(dir
)) != NULL
){
419 if(strcmp(entry
->d_name
,".") != 0 &&
420 strcmp(entry
->d_name
,"..") != 0 ){
422 strcat(name
,entry
->d_name
);
423 ltt_tracefile_open_cpu(t
,name
);
429 /*****************************************************************************
430 *A trace is specified as a pathname to the directory containing all the
431 *associated data (control tracefiles, per cpu tracefiles, event
434 *When a trace is closed, all the associated facilities, types and fields
435 *are released as well.
436 ****************************************************************************/
438 LttTrace
*ltt_trace_open(char *pathname
)
441 LttSystemDescription
* sys_description
;
442 char eventdefs
[DIR_NAME_SIZE
];
443 char info
[DIR_NAME_SIZE
];
444 char control
[DIR_NAME_SIZE
];
445 char cpu
[DIR_NAME_SIZE
];
446 char tmp
[DIR_NAME_SIZE
];
447 gboolean has_slash
= FALSE
;
449 //establish the pathname to different directories
450 if(pathname
[strlen(pathname
)-1] == '/')has_slash
= TRUE
;
451 strcpy(eventdefs
,pathname
);
452 if(!has_slash
)strcat(eventdefs
,"/");
453 strcat(eventdefs
,"eventdefs/");
455 strcpy(info
,pathname
);
456 if(!has_slash
)strcat(info
,"/");
457 strcat(info
,"info/");
459 strcpy(control
,pathname
);
460 if(!has_slash
)strcat(control
,"/");
461 strcat(control
,"control/");
463 strcpy(cpu
,pathname
);
464 if(!has_slash
)strcat(cpu
,"/");
468 t
= g_new(LttTrace
, 1);
469 sys_description
= g_new(LttSystemDescription
, 1);
470 t
->pathname
= g_strdup(pathname
);
471 t
->facility_number
= 0;
472 t
->control_tracefile_number
= 0;
473 t
->per_cpu_tracefile_number
= 0;
474 t
->system_description
= sys_description
;
475 t
->control_tracefiles
= g_ptr_array_new();
476 t
->per_cpu_tracefiles
= g_ptr_array_new();
477 t
->facilities
= g_ptr_array_new();
478 getDataEndianType(&(t
->my_arch_size
), &(t
->my_arch_endian
));
480 //get system description
482 strcat(tmp
,"system.xml");
483 getSystemInfo(sys_description
, tmp
);
485 //get facilities info
486 getFacilityInfo(t
,eventdefs
);
488 //get control tracefile info
489 getControlFileInfo(t
,control
);
491 //get cpu tracefile info
492 getCpuFileInfo(t
,cpu
);
497 void ltt_trace_close(LttTrace
*t
)
505 //free system_description
506 g_free(t
->system_description
->description
);
507 g_free(t
->system_description
->node_name
);
508 g_free(t
->system_description
->domain_name
);
509 g_free(t
->system_description
->kernel_name
);
510 g_free(t
->system_description
->kernel_release
);
511 g_free(t
->system_description
->kernel_version
);
512 g_free(t
->system_description
->machine
);
513 g_free(t
->system_description
->processor
);
514 g_free(t
->system_description
->hardware_platform
);
515 g_free(t
->system_description
->operating_system
);
516 g_free(t
->system_description
);
518 //free control_tracefiles
519 for(i
=0;i
<t
->control_tracefile_number
;i
++){
520 tf
= (LttTracefile
*)g_ptr_array_index(t
->control_tracefiles
,i
);
521 ltt_tracefile_close(tf
);
523 g_ptr_array_free(t
->control_tracefiles
, FALSE
);
525 //free per_cpu_tracefiles
526 for(i
=0;i
<t
->per_cpu_tracefile_number
;i
++){
527 tf
= (LttTracefile
*)g_ptr_array_index(t
->per_cpu_tracefiles
,i
);
528 ltt_tracefile_close(tf
);
530 g_ptr_array_free(t
->per_cpu_tracefiles
, FALSE
);
533 for(i
=0;i
<t
->facility_number
;i
++){
534 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
,i
);
535 ltt_facility_close(f
);
537 g_ptr_array_free(t
->facilities
, FALSE
);
543 /*****************************************************************************
544 *Get the system description of the trace
545 ****************************************************************************/
547 LttSystemDescription
*ltt_trace_system_description(LttTrace
*t
)
549 return t
->system_description
;
552 /*****************************************************************************
553 * The following functions discover the facilities of the trace
554 ****************************************************************************/
556 unsigned ltt_trace_facility_number(LttTrace
*t
)
558 return (unsigned)(t
->facility_number
);
561 LttFacility
*ltt_trace_facility_get(LttTrace
*t
, unsigned i
)
563 return (LttFacility
*)g_ptr_array_index(t
->facilities
, i
);
566 /*****************************************************************************
568 * ltt_trace_facility_find : find facilities in the trace
571 * name : facility name
573 * position : position of the facility in the trace
575 * : the number of facilities
576 ****************************************************************************/
578 unsigned ltt_trace_facility_find(LttTrace
*t
, char *name
, unsigned *position
)
582 for(i
=0;i
=t
->facility_number
;i
++){
583 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, i
);
584 if(strcmp(f
->name
,name
)==0){
586 if(count
==1) *position
= i
;
594 /*****************************************************************************
595 * Functions to discover all the event types in the trace
596 ****************************************************************************/
598 unsigned ltt_trace_eventtype_number(LttTrace
*t
)
603 for(i
=0;i
=t
->facility_number
;i
++){
604 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, i
);
605 count
+= f
->event_number
;
610 LttFacility
* ltt_trace_facility_by_id(LttTrace
* trace
, unsigned id
)
612 LttFacility
* facility
;
614 for(i
=0;i
<trace
->facility_number
;i
++){
615 facility
= (LttFacility
*) g_ptr_array_index(trace
->facilities
,i
);
616 if(id
>= facility
->base_id
&&
617 id
< facility
->base_id
+ facility
->event_number
)
620 if(i
==trace
->facility_number
) return NULL
;
621 else return facility
;
624 LttEventType
*ltt_trace_eventtype_get(LttTrace
*t
, unsigned evId
)
627 f
= ltt_trace_facility_by_id(t
,evId
);
629 return f
->events
[evId
- f
->base_id
];
632 /*****************************************************************************
633 *There is one "per cpu" tracefile for each CPU, numbered from 0 to
634 *the maximum number of CPU in the system. When the number of CPU installed
635 *is less than the maximum, some positions are unused. There are also a
636 *number of "control" tracefiles (facilities, interrupts...).
637 ****************************************************************************/
638 unsigned ltt_trace_control_tracefile_number(LttTrace
*t
)
640 return t
->control_tracefile_number
;
643 unsigned ltt_trace_per_cpu_tracefile_number(LttTrace
*t
)
645 return t
->per_cpu_tracefile_number
;
648 /*****************************************************************************
649 *It is possible to search for the tracefiles by name or by CPU position.
650 *The index within the tracefiles of the same type is returned if found
651 *and a negative value otherwise.
652 ****************************************************************************/
654 int ltt_trace_control_tracefile_find(LttTrace
*t
, char *name
)
656 LttTracefile
* tracefile
;
658 for(i
=0;i
<t
->control_tracefile_number
;i
++){
659 tracefile
= (LttTracefile
*)g_ptr_array_index(t
->control_tracefiles
, i
);
660 if(strcmp(tracefile
->name
, name
)==0)break;
662 if(i
== t
->control_tracefile_number
) return -1;
666 int ltt_trace_per_cpu_tracefile_find(LttTrace
*t
, unsigned i
)
668 LttTracefile
* tracefile
;
670 for(j
=0;j
<t
->per_cpu_tracefile_number
;j
++){
671 tracefile
= (LttTracefile
*)g_ptr_array_index(t
->per_cpu_tracefiles
, j
);
672 name
= atoi(tracefile
->name
);
673 if(name
== (int)i
)break;
675 if(j
== t
->per_cpu_tracefile_number
) return -1;
679 /*****************************************************************************
680 *Get a specific tracefile
681 ****************************************************************************/
683 LttTracefile
*ltt_trace_control_tracefile_get(LttTrace
*t
, unsigned i
)
685 return (LttTracefile
*)g_ptr_array_index(t
->per_cpu_tracefiles
, i
);
688 LttTracefile
*ltt_trace_per_cpu_tracefile_get(LttTrace
*t
, unsigned i
)
690 return (LttTracefile
*)g_ptr_array_index(t
->per_cpu_tracefiles
, i
);
693 /*****************************************************************************
694 *Get the name of a tracefile
695 ****************************************************************************/
697 char *ltt_tracefile_name(LttTracefile
*tf
)
702 /*****************************************************************************
704 * ltt_tracefile_seek_time: seek to the first event of the trace with time
705 * larger or equal to time
708 * time : criteria of the time
709 ****************************************************************************/
711 void ltt_tracefile_seek_time(LttTracefile
*t
, LttTime time
)
715 int headTime
= timecmp(&(t
->a_block_start
->time
), &time
);
716 int tailTime
= timecmp(&(t
->a_block_end
->time
), &time
);
718 if(headTime
< 0 && tailTime
> 0){
719 lttTime
= getEventTime(t
);
720 err
= timecmp(<tTime
, &time
);
722 if(t
->which_event
==2 || timecmp(&t
->prev_event_time
,&time
)<0){
726 return ltt_tracefile_seek_time(t
, time
);
729 err
= t
->which_block
;
730 if(ltt_tracefile_read(t
) == NULL
){
731 g_print("End of file\n");
734 if(t
->which_block
== err
)
735 return ltt_tracefile_seek_time(t
,time
);
737 }else if(headTime
> 0){
738 if(t
->which_block
== 1){
741 if(timecmp(&(t
->prev_block_end_time
),&time
) >= 0 ){
742 err
=readBlock(t
,t
->which_block
-1);
743 if(err
) g_error("Can not read tracefile: %s\n", t
->name
);
744 return ltt_tracefile_seek_time(t
, time
) ;
749 }else if(tailTime
< 0){
750 if(t
->which_block
!= t
->block_number
){
751 err
=readBlock(t
,t
->which_block
+1);
752 if(err
) g_error("Can not read tracefile: %s\n", t
->name
);
754 g_print("End of file\n");
757 if(tailTime
< 0) return ltt_tracefile_seek_time(t
, time
);
758 }else if(headTime
== 0){
760 }else if(tailTime
== 0){
761 t
->cur_event_pos
= t
->a_block_end
- EVENT_HEADER_SIZE
;
766 /*****************************************************************************
768 * ltt_tracefile_read : read the current event, set the pointer to the next
772 * LttEvent * : an event to be processed
773 ****************************************************************************/
775 LttEvent
*ltt_tracefile_read(LttTracefile
*t
)
777 LttEvent
* lttEvent
= (LttEvent
*)g_new(LttEvent
, 1);
780 lttEvent
->event_id
= (int)(*(uint16_t *)(t
->cur_event_pos
));
781 if(lttEvent
->event_id
== TRACE_TIME_HEARTBEAT
)
782 t
->cur_heart_beat_number
++;
784 t
->prev_event_time
= t
->current_event_time
;
785 t
->current_event_time
= getEventTime(t
);
787 lttEvent
->time_delta
= *(uint32_t*)(t
->cur_event_pos
+ EVENT_ID_SIZE
);
788 lttEvent
->event_time
= t
->current_event_time
;
790 lttEvent
->event_cycle_count
= ((uint64_t)1)<<32 * t
->cur_heart_beat_number
791 + lttEvent
->time_delta
;
793 lttEvent
->tracefile
= t
;
794 lttEvent
->data
= t
->cur_event_pos
+ EVENT_HEADER_SIZE
;
796 //update the fields of the current event and go to the next event
802 if(err
== ENOENT
) return lttEvent
;
803 if(err
== ERANGE
) g_error("event id is out of range\n");
804 if(err
)g_error("Can not read tracefile\n");
809 /****************************************************************************
811 * readFile : wrap function to read from a file
813 * fd : file descriptor
814 * buf : buf to contain the content
815 * size : number of bytes to be read
816 * mesg : message to be printed if some thing goes wrong
819 * EIO : can not read from the file
820 ****************************************************************************/
822 int readFile(int fd
, void * buf
, size_t size
, char * mesg
)
825 nbBytes
= read(fd
, buf
, size
);
833 /****************************************************************************
835 * readBlock : read a block from the file
837 * lttdes : ltt trace file
838 * whichBlock : the block which will be read
841 * EINVAL : lseek fail
842 * EIO : can not read from the file
843 ****************************************************************************/
845 int readBlock(LttTracefile
* tf
, int whichBlock
)
850 if(whichBlock
- tf
->which_block
== 1 && tf
->which_block
!= 0){
851 tf
->prev_block_end_time
= tf
->a_block_end
->time
;
852 tf
->prev_event_time
= tf
->a_block_end
->time
;
853 tf
->current_event_time
= tf
->a_block_end
->time
;
855 tf
->prev_block_end_time
.tv_sec
= 0;
856 tf
->prev_block_end_time
.tv_nsec
= 0;
857 tf
->prev_event_time
.tv_sec
= 0;
858 tf
->prev_event_time
.tv_nsec
= 0;
859 tf
->current_event_time
.tv_sec
= 0;
860 tf
->current_event_time
.tv_nsec
= 0;
863 nbBytes
=lseek(tf
->fd
,(off_t
)((whichBlock
-1)*tf
->block_size
), SEEK_SET
);
864 if(nbBytes
== -1) return EINVAL
;
866 if(readFile(tf
->fd
,tf
->buffer
,tf
->block_size
,"Unable to read a block"))
869 tf
->a_block_start
=(BlockStart
*) (tf
->buffer
+ EVENT_HEADER_SIZE
);
870 lostSize
= *(uint32_t*)(tf
->buffer
+ tf
->block_size
- sizeof(uint32_t));
871 tf
->a_block_end
=(BlockEnd
*)(tf
->buffer
+ tf
->block_size
-
872 lostSize
+ EVENT_HEADER_SIZE
);
874 tf
->which_block
= whichBlock
;
876 tf
->cur_event_pos
= tf
->buffer
;//the beginning of the block, block start ev
877 tf
->cur_heart_beat_number
= 0;
881 // tf->current_event_time = getEventTime(tf);
886 /*****************************************************************************
888 * updateTracefile : reinitialize the info of the block which is already
892 ****************************************************************************/
894 void updateTracefile(LttTracefile
* tf
)
897 tf
->cur_event_pos
= tf
->buffer
;
898 // tf->current_event_time = getEventTime(tf);
899 tf
->current_event_time
.tv_sec
= 0;
900 tf
->current_event_time
.tv_nsec
= 0;
901 tf
->cur_heart_beat_number
= 0;
903 tf
->prev_event_time
.tv_sec
= 0;
904 tf
->prev_event_time
.tv_nsec
= 0;
907 /*****************************************************************************
909 * skipEvent : go to the next event, update the fields of the current event
914 * EINVAL : lseek fail
915 * EIO : can not read from the file
916 * ENOMEM : end of file
917 * ENOENT : last event
918 * ERANGE : event id is out of range
919 ****************************************************************************/
921 int skipEvent(LttTracefile
* t
)
927 static int evCount
= 0;
930 if(t
->which_block
== t
->block_number
&&
931 evCount
== t
->which_event
){
936 evId
= (int)(*(uint16_t *)(t
->cur_event_pos
));
937 evData
= t
->cur_event_pos
+ EVENT_HEADER_SIZE
;
939 //regard BLOCK_START, END and HEARTBEAT as special case, there are buildin events
940 if(evId
!= TRACE_BLOCK_START
&& evId
!= TRACE_BLOCK_END
&& evId
!= TRACE_TIME_HEARTBEAT
){
941 evT
= ltt_trace_eventtype_get(t
->trace
,(unsigned)evId
);
943 if(evT
) rootFld
= evT
->root_field
;
947 //event has string/sequence or the last event is not the same event
948 if((evT
->latest_block
!=t
->which_block
|| evT
->latest_event
!=t
->which_event
)
949 && rootFld
->field_fixed
== 0){
950 setFieldsOffset(t
, evT
, evData
, t
->trace
);
952 t
->cur_event_pos
+= EVENT_HEADER_SIZE
+ rootFld
->field_size
;
953 }else t
->cur_event_pos
+= EVENT_HEADER_SIZE
;
955 evT
->latest_block
= t
->which_block
;
956 evT
->latest_event
= t
->which_event
;
958 if(evId
== TRACE_BLOCK_START
|| evId
== TRACE_BLOCK_END
){
959 t
->cur_event_pos
+= sizeof(BlockStart
) + EVENT_HEADER_SIZE
;
961 t
->cur_event_pos
+= sizeof(TimeHeartbeat
) + EVENT_HEADER_SIZE
;
965 //the next event is in the next block
966 if(evId
== TRACE_BLOCK_END
){
967 if(t
->which_block
== t
->block_number
){
969 evCount
= t
->which_event
;
972 err
= readBlock(t
, t
->which_block
+ 1);
981 /*****************************************************************************
983 * getCyclePerNsec : calculate cycles per nsec for current block
986 ****************************************************************************/
988 void getCyclePerNsec(LttTracefile
* t
)
990 LttTime lBufTotalTime
; /* Total time for this buffer */
991 LttCycleCount lBufTotalNSec
; /* Total time for this buffer in nsecs */
992 LttCycleCount lBufTotalCycle
;/* Total cycles for this buffer */
994 /* Calculate the total time for this buffer */
995 TimeSub(lBufTotalTime
,t
->a_block_end
->time
, t
->a_block_start
->time
);
997 /* Calculate the total cycles for this bufffer */
998 lBufTotalCycle
= t
->a_block_end
->cycle_count
999 - t
->a_block_start
->cycle_count
;
1001 /* Convert the total time to nsecs */
1002 lBufTotalNSec
= lBufTotalTime
.tv_sec
* 1000000000 + lBufTotalTime
.tv_nsec
;
1004 t
->cycle_per_nsec
= (double)lBufTotalCycle
/ (double)lBufTotalNSec
;
1007 /****************************************************************************
1009 * getEventTime : obtain the time of an event
1013 * LttTime : the time of the event
1014 ****************************************************************************/
1016 LttTime
getEventTime(LttTracefile
* tf
)
1019 LttCycleCount cycle_count
; // cycle count for the current event
1020 LttCycleCount lEventTotalCycle
; // Total cycles from start for event
1021 double lEventNSec
; // Total usecs from start for event
1022 LttTime lTimeOffset
; // Time offset in struct LttTime
1025 evId
= *(uint16_t*)tf
->cur_event_pos
;
1026 if(evId
== TRACE_BLOCK_START
)
1027 return tf
->a_block_start
->time
;
1028 else if(evId
== TRACE_BLOCK_END
)
1029 return tf
->a_block_end
->time
;
1032 // Calculate total time in cycles from start of buffer for this event
1033 cycle_count
= (LttCycleCount
)*(uint32_t*)(tf
->cur_event_pos
+ EVENT_ID_SIZE
);
1034 if(tf
->cur_heart_beat_number
)
1035 cycle_count
+= ((uint64_t)1)<<32 * tf
->cur_heart_beat_number
;
1036 lEventTotalCycle
= cycle_count
- tf
->a_block_start
->cycle_count
;
1038 // Convert it to nsecs
1039 lEventNSec
= lEventTotalCycle
/ tf
->cycle_per_nsec
;
1041 // Determine offset in struct LttTime
1042 lTimeOffset
.tv_nsec
= (long)lEventNSec
% 1000000000;
1043 lTimeOffset
.tv_sec
= (long)lEventNSec
/ 1000000000;
1045 TimeAdd(time
, tf
->a_block_start
->time
, lTimeOffset
);
1050 /*****************************************************************************
1052 * setFieldsOffset : set offset of the fields
1054 * tracefile : opened trace file
1055 * evT : the event type
1056 * evD : event data, it may be NULL
1057 ****************************************************************************/
1059 void setFieldsOffset(LttTracefile
*tf
,LttEventType
*evT
,void *evD
,LttTrace
* t
)
1061 LttField
* rootFld
= evT
->root_field
;
1062 // rootFld->base_address = evD;
1065 rootFld
->field_size
= getFieldtypeSize(tf
, evT
, 0,0,rootFld
, evD
,t
);
1068 /*****************************************************************************
1070 * getFieldtypeSize: get the size of the field type (primitive type)
1072 * tracefile : opened trace file
1074 * offsetRoot : offset from the root
1075 * offsetParent : offset from the parrent
1077 * evD : event data, it may be NULL
1079 * int : size of the field
1080 ****************************************************************************/
1082 int getFieldtypeSize(LttTracefile
* t
, LttEventType
* evT
, int offsetRoot
,
1083 int offsetParent
, LttField
* fld
, void *evD
, LttTrace
*trace
)
1085 int size
, size1
, element_number
, i
, offset1
, offset2
;
1086 LttType
* type
= fld
->field_type
;
1089 if(evT
->latest_block
==t
->which_block
&& evT
->latest_event
==t
->which_event
){
1090 return fld
->field_size
;
1094 if(fld
->field_fixed
== 1){
1095 if(fld
== evT
->root_field
) return fld
->field_size
;
1098 if(type
->type_class
!= LTT_STRUCT
&& type
->type_class
!= LTT_ARRAY
&&
1099 type
->type_class
!= LTT_SEQUENCE
&& type
->type_class
!= LTT_STRING
){
1100 if(fld
->field_fixed
== -1){
1101 size
= (int) ltt_type_size(trace
, type
);
1102 fld
->field_fixed
= 1;
1103 }else size
= fld
->field_size
;
1105 }else if(type
->type_class
== LTT_ARRAY
){
1106 element_number
= (int) type
->element_number
;
1107 if(fld
->field_fixed
== -1){
1108 size
= getFieldtypeSize(t
, evT
, offsetRoot
,0,fld
->child
[0], NULL
, trace
);
1109 if(size
== 0){ //has string or sequence
1110 fld
->field_fixed
= 0;
1112 fld
->field_fixed
= 1;
1113 size
*= element_number
;
1115 }else if(fld
->field_fixed
== 0){// has string or sequence
1117 for(i
=0;i
<element_number
;i
++){
1118 size
+= getFieldtypeSize(t
, evT
, offsetRoot
+size
,size
,
1119 fld
->child
[0], evD
+size
, trace
);
1121 }else size
= fld
->field_size
;
1123 }else if(type
->type_class
== LTT_SEQUENCE
){
1124 size1
= (int) ltt_type_size(trace
, type
);
1125 if(fld
->field_fixed
== -1){
1126 fld
->field_fixed
= 0;
1127 size
= getFieldtypeSize(t
, evT
, offsetRoot
,0,fld
->child
[0], NULL
, trace
);
1128 fld
->element_size
= size
;
1130 element_number
= getIntNumber(size1
,evD
);
1131 type
->element_number
= element_number
;
1132 if(fld
->element_size
> 0){
1133 size
= element_number
* fld
->element_size
;
1134 }else{//sequence has string or sequence
1136 for(i
=0;i
<element_number
;i
++){
1137 size
+= getFieldtypeSize(t
, evT
, offsetRoot
+size
+size1
,size
+size1
,
1138 fld
->child
[0], evD
+size
+size1
, trace
);
1144 }else if(type
->type_class
== LTT_STRING
){
1146 if(fld
->field_fixed
== -1){
1147 fld
->field_fixed
= 0;
1149 size
= strlen((char*)evD
) + 1; //include end : '\0'
1152 }else if(type
->type_class
== LTT_STRUCT
){
1153 element_number
= (int) type
->element_number
;
1155 if(fld
->field_fixed
== -1){
1156 offset1
= offsetRoot
;
1158 for(i
=0;i
<element_number
;i
++){
1159 size1
=getFieldtypeSize(t
, evT
,offset1
,offset2
, fld
->child
[i
], NULL
, trace
);
1160 if(size1
> 0 && size
>= 0){
1162 if(offset1
>= 0) offset1
+= size1
;
1171 fld
->field_fixed
= 0;
1173 }else fld
->field_fixed
= 1;
1174 }else if(fld
->field_fixed
== 0){
1175 offset1
= offsetRoot
;
1177 for(i
=0;i
<element_number
;i
++){
1178 size
=getFieldtypeSize(t
,evT
,offset1
,offset2
,fld
->child
[i
],evD
+offset2
, trace
);
1183 }else size
= fld
->field_size
;
1186 fld
->offset_root
= offsetRoot
;
1187 fld
->offset_parent
= offsetParent
;
1189 fld
->fixed_root
= (offsetRoot
==-1) ? 0 : 1;
1190 fld
->fixed_parent
= (offsetParent
==-1) ? 0 : 1;
1192 fld
->field_size
= size
;
1197 /*****************************************************************************
1199 * timecmp : compare two time
1204 * int : 0: t1 == t2; -1: t1 < t2; 1: t1 > t2
1205 ****************************************************************************/
1207 int timecmp(LttTime
* t1
, LttTime
* t2
)
1210 TimeSub(T
, *t1
, *t2
);
1211 if(T
.tv_sec
== 0 && T
.tv_nsec
== 0) return 0;
1212 else if(T
.tv_sec
> 0 || (T
.tv_sec
==0 && T
.tv_nsec
> 0)) return 1;
1216 /*****************************************************************************
1218 * getIntNumber : get an integer number
1220 * size : the size of the integer
1221 * evD : the event data
1224 ****************************************************************************/
1226 int getIntNumber(int size
, void *evD
)
1229 if(size
== 1) i
= *(int8_t *)evD
;
1230 else if(size
== 2) i
= *(int16_t *)evD
;
1231 else if(size
== 4) i
= *(int32_t *)evD
;
1232 else if(size
== 8) i
= *(int64_t *)evD
;
1237 /*****************************************************************************
1239 * getDataEndianType : get the data type size and endian type of the local
1242 * size : size of data type
1243 * endian : endian type, little or big
1244 ****************************************************************************/
1246 void getDataEndianType(LttArchSize
* size
, LttArchEndian
* endian
)
1250 int sizeInt
=sizeof(int), sizeLong
=sizeof(long), sizePointer
=sizeof(void *);
1252 if(c
== 1) *endian
= LTT_LITTLE_ENDIAN
;
1253 else *endian
= LTT_BIG_ENDIAN
;
1255 if(sizeInt
== 2 && sizeLong
== 4 && sizePointer
== 4)
1257 else if(sizeInt
== 4 && sizeLong
== 4 && sizePointer
== 4)
1259 else if(sizeInt
== 4 && sizeLong
== 8 && sizePointer
== 8)
1261 else if(sizeInt
== 8 && sizeLong
== 8 && sizePointer
== 8)
1263 else *size
= LTT_UNKNOWN
;