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");
188 /*****************************************************************************
190 * ltt_tracefile_close: close a trace file,
192 * t : tracefile which will be closed
193 ****************************************************************************/
195 void ltt_tracefile_close(LttTracefile
*t
)
204 /*****************************************************************************
205 *Get system information
206 ****************************************************************************/
207 void getSystemInfo(LttSystemDescription
* des
, char * pathname
)
211 int entry_number
= 15;
212 char buf
[DIR_NAME_SIZE
];
213 char description
[4*DIR_NAME_SIZE
];
216 fp
= fopen(pathname
,"r");
218 g_error("Can not open file : %s\n", pathname
);
221 while(fgets(buf
,DIR_NAME_SIZE
, fp
)!= NULL
){
223 while(isspace(*ptr
)) ptr
++;
224 if(strlen(ptr
) == 0) continue;
228 if(strlen(ptr
) == 0) g_error("Not a valid file: %s\n", pathname
);
229 if(strncmp("<system",ptr
,7) !=0)g_error("Not a valid file: %s\n", pathname
);
231 for(i
=0;i
<entry_number
;i
++){
232 if(fgets(buf
,DIR_NAME_SIZE
, fp
)== NULL
)
233 g_error("Not a valid file: %s\n", pathname
);
234 buf
[strlen(buf
)-1] = '\0';
236 while(isspace(*ptr
)) ptr
++;
239 if(strncmp("node_name=",ptr
,10)!=0)
240 g_error("Not a valid file: %s\n", pathname
);
241 des
->node_name
= g_strdup(ptr
+10);
244 if(strncmp("domainname=",ptr
,11)!=0)
245 g_error("Not a valid file: %s\n", pathname
);
246 des
->domain_name
= g_strdup(ptr
+11);
249 if(strncmp("cpu=",ptr
,4)!=0)
250 g_error("Not a valid file: %s\n", pathname
);
251 des
->nb_cpu
= (unsigned)atoi(ptr
+4);
254 if(strncmp("arch_size=",ptr
,10)!=0)
255 g_error("Not a valid file: %s\n", pathname
);
256 if(strcmp(ptr
+10,"\"LP32\"") == 0) des
->size
= LTT_LP32
;
257 else if(strcmp(ptr
+10,"\"ILP32\"") == 0) des
->size
= LTT_ILP32
;
258 else if(strcmp(ptr
+10,"\"LP64\"") == 0) des
->size
= LTT_LP64
;
259 else if(strcmp(ptr
+10,"\"ILP64\"") == 0) des
->size
= LTT_ILP64
;
260 else if(strcmp(ptr
+10,"\"UNKNOWN\"") == 0) des
->size
= LTT_UNKNOWN
;
263 if(strncmp("endian=",ptr
,7)!=0)
264 g_error("Not a valid file: %s\n", pathname
);
265 if(strcmp(ptr
+7,"\"LITTLE_ENDIAN\"") == 0)
266 des
->endian
= LTT_LITTLE_ENDIAN
;
267 else if(strcmp(ptr
+7,"\"BIG_ENDIAN\"") == 0)
268 des
->endian
= LTT_BIG_ENDIAN
;
271 if(strncmp("kernel_name=",ptr
,12)!=0)
272 g_error("Not a valid file: %s\n", pathname
);
273 des
->kernel_name
= g_strdup(ptr
+12);
276 if(strncmp("kernel_release=",ptr
,15)!=0)
277 g_error("Not a valid file: %s\n", pathname
);
278 des
->kernel_release
= g_strdup(ptr
+15);
281 if(strncmp("kernel_version=",ptr
,15)!=0)
282 g_error("Not a valid file: %s\n", pathname
);
283 des
->kernel_version
= g_strdup(ptr
+15);
286 if(strncmp("machine=",ptr
,8)!=0)
287 g_error("Not a valid file: %s\n", pathname
);
288 des
->machine
= g_strdup(ptr
+8);
291 if(strncmp("processor=",ptr
,10)!=0)
292 g_error("Not a valid file: %s\n", pathname
);
293 des
->processor
= g_strdup(ptr
+10);
296 if(strncmp("hardware_platform=",ptr
,18)!=0)
297 g_error("Not a valid file: %s\n", pathname
);
298 des
->hardware_platform
= g_strdup(ptr
+18);
301 if(strncmp("operating_system=",ptr
,17)!=0)
302 g_error("Not a valid file: %s\n", pathname
);
303 des
->operating_system
= g_strdup(ptr
+17);
306 if(strncmp("ltt_major_version=",ptr
,18)!=0)
307 g_error("Not a valid file: %s\n", pathname
);
309 // ptr++;//skip begining "
310 // ptr[strlen(ptr)-1] = '\0'; //get rid of the ending "
311 des
->ltt_major_version
= (unsigned)atoi(ptr
);
314 if(strncmp("ltt_minor_version=",ptr
,18)!=0)
315 g_error("Not a valid file: %s\n", pathname
);
317 // ptr++;//skip begining "
318 // ptr[strlen(ptr)-1] = '\0'; //get rid of the ending "
319 des
->ltt_minor_version
= (unsigned)atoi(ptr
);
322 if(strncmp("ltt_block_size=",ptr
,15)!=0)
323 g_error("Not a valid file: %s\n", pathname
);
325 // ptr++;//skip begining "
326 // ptr[strlen(ptr)-1] = '\0'; //get rid of the ending "
327 des
->ltt_block_size
= (unsigned)atoi(ptr
);
330 g_error("Not a valid file: %s\n", pathname
);
335 description
[0] = '\0';
336 if(fgets(buf
,DIR_NAME_SIZE
, fp
)== NULL
)
337 g_error("Not a valid file: %s\n", pathname
);
339 while(isspace(*ptr
)) ptr
++;
340 if(*ptr
!= '>') g_error("Not a valid file: %s\n", pathname
);
341 while((ptr
=fgets(buf
,DIR_NAME_SIZE
, fp
))!= NULL
){
343 while(isspace(*ptr
)) ptr
++;
344 if(strncmp("</system>",ptr
,9) == 0 )break;
345 strcat(description
, buf
);
347 if(!ptr
)g_error("Not a valid file: %s\n", pathname
);
348 if(description
[0] = '\0')des
->description
= NULL
;
349 des
->description
= g_strdup(description
);
354 /*****************************************************************************
355 *The following functions get facility/tracefile information
356 ****************************************************************************/
358 void getFacilityInfo(LttTrace
*t
, char* eventdefs
)
361 struct dirent
*entry
;
366 char name
[DIR_NAME_SIZE
];
368 dir
= opendir(eventdefs
);
369 if(!dir
) g_error("Can not open directory: %s\n", eventdefs
);
371 while((entry
= readdir(dir
)) != NULL
){
372 ptr
= &entry
->d_name
[strlen(entry
->d_name
)-4];
373 if(strcmp(ptr
,".xml") != 0) continue;
374 strcpy(name
,eventdefs
);
375 strcat(name
,entry
->d_name
);
376 ltt_facility_open(t
,name
);
380 for(j
=0;j
<t
->facility_number
;j
++){
381 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, j
);
382 for(i
=0; i
<f
->event_number
; i
++){
384 setFieldsOffset(NULL
, et
, NULL
, t
);
389 void getControlFileInfo(LttTrace
*t
, char* control
)
392 struct dirent
*entry
;
393 char name
[DIR_NAME_SIZE
];
395 dir
= opendir(control
);
396 if(!dir
) g_error("Can not open directory: %s\n", control
);
398 while((entry
= readdir(dir
)) != NULL
){
399 if(strcmp(entry
->d_name
,"facilities") != 0 &&
400 strcmp(entry
->d_name
,"interrupts") != 0 &&
401 strcmp(entry
->d_name
,"processes") != 0) continue;
403 strcpy(name
,control
);
404 strcat(name
,entry
->d_name
);
405 ltt_tracefile_open_control(t
,name
);
410 void getCpuFileInfo(LttTrace
*t
, char* cpu
)
413 struct dirent
*entry
;
414 char name
[DIR_NAME_SIZE
];
417 if(!dir
) g_error("Can not open directory: %s\n", cpu
);
419 while((entry
= readdir(dir
)) != NULL
){
420 if(strcmp(entry
->d_name
,".") != 0 &&
421 strcmp(entry
->d_name
,"..") != 0 ){
423 strcat(name
,entry
->d_name
);
424 ltt_tracefile_open_cpu(t
,name
);
430 /*****************************************************************************
431 *A trace is specified as a pathname to the directory containing all the
432 *associated data (control tracefiles, per cpu tracefiles, event
435 *When a trace is closed, all the associated facilities, types and fields
436 *are released as well.
437 ****************************************************************************/
439 LttTrace
*ltt_trace_open(char *pathname
)
442 LttSystemDescription
* sys_description
;
443 char eventdefs
[DIR_NAME_SIZE
];
444 char info
[DIR_NAME_SIZE
];
445 char control
[DIR_NAME_SIZE
];
446 char cpu
[DIR_NAME_SIZE
];
447 char tmp
[DIR_NAME_SIZE
];
448 gboolean has_slash
= FALSE
;
450 //establish the pathname to different directories
451 if(pathname
[strlen(pathname
)-1] == '/')has_slash
= TRUE
;
452 strcpy(eventdefs
,pathname
);
453 if(!has_slash
)strcat(eventdefs
,"/");
454 strcat(eventdefs
,"eventdefs/");
456 strcpy(info
,pathname
);
457 if(!has_slash
)strcat(info
,"/");
458 strcat(info
,"info/");
460 strcpy(control
,pathname
);
461 if(!has_slash
)strcat(control
,"/");
462 strcat(control
,"control/");
464 strcpy(cpu
,pathname
);
465 if(!has_slash
)strcat(cpu
,"/");
469 t
= g_new(LttTrace
, 1);
470 sys_description
= g_new(LttSystemDescription
, 1);
471 t
->pathname
= g_strdup(pathname
);
472 t
->facility_number
= 0;
473 t
->control_tracefile_number
= 0;
474 t
->per_cpu_tracefile_number
= 0;
475 t
->system_description
= sys_description
;
476 t
->control_tracefiles
= g_ptr_array_new();
477 t
->per_cpu_tracefiles
= g_ptr_array_new();
478 t
->facilities
= g_ptr_array_new();
479 getDataEndianType(&(t
->my_arch_size
), &(t
->my_arch_endian
));
481 //get system description
483 strcat(tmp
,"system.xml");
484 getSystemInfo(sys_description
, tmp
);
486 //get facilities info
487 getFacilityInfo(t
,eventdefs
);
489 //get control tracefile info
490 getControlFileInfo(t
,control
);
492 //get cpu tracefile info
493 getCpuFileInfo(t
,cpu
);
498 void ltt_trace_close(LttTrace
*t
)
506 //free system_description
507 g_free(t
->system_description
->description
);
508 g_free(t
->system_description
->node_name
);
509 g_free(t
->system_description
->domain_name
);
510 g_free(t
->system_description
->kernel_name
);
511 g_free(t
->system_description
->kernel_release
);
512 g_free(t
->system_description
->kernel_version
);
513 g_free(t
->system_description
->machine
);
514 g_free(t
->system_description
->processor
);
515 g_free(t
->system_description
->hardware_platform
);
516 g_free(t
->system_description
->operating_system
);
517 g_free(t
->system_description
);
519 //free control_tracefiles
520 for(i
=0;i
<t
->control_tracefile_number
;i
++){
521 tf
= (LttTracefile
*)g_ptr_array_index(t
->control_tracefiles
,i
);
522 ltt_tracefile_close(tf
);
524 g_ptr_array_free(t
->control_tracefiles
, FALSE
);
526 //free per_cpu_tracefiles
527 for(i
=0;i
<t
->per_cpu_tracefile_number
;i
++){
528 tf
= (LttTracefile
*)g_ptr_array_index(t
->per_cpu_tracefiles
,i
);
529 ltt_tracefile_close(tf
);
531 g_ptr_array_free(t
->per_cpu_tracefiles
, FALSE
);
534 for(i
=0;i
<t
->facility_number
;i
++){
535 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
,i
);
536 ltt_facility_close(f
);
538 g_ptr_array_free(t
->facilities
, FALSE
);
544 /*****************************************************************************
545 *Get the system description of the trace
546 ****************************************************************************/
548 LttSystemDescription
*ltt_trace_system_description(LttTrace
*t
)
550 return t
->system_description
;
553 /*****************************************************************************
554 * The following functions discover the facilities of the trace
555 ****************************************************************************/
557 unsigned ltt_trace_facility_number(LttTrace
*t
)
559 return (unsigned)(t
->facility_number
);
562 LttFacility
*ltt_trace_facility_get(LttTrace
*t
, unsigned i
)
564 return (LttFacility
*)g_ptr_array_index(t
->facilities
, i
);
567 /*****************************************************************************
569 * ltt_trace_facility_find : find facilities in the trace
572 * name : facility name
574 * position : position of the facility in the trace
576 * : the number of facilities
577 ****************************************************************************/
579 unsigned ltt_trace_facility_find(LttTrace
*t
, char *name
, unsigned *position
)
583 for(i
=0;i
=t
->facility_number
;i
++){
584 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, i
);
585 if(strcmp(f
->name
,name
)==0){
587 if(count
==1) *position
= i
;
595 /*****************************************************************************
596 * Functions to discover all the event types in the trace
597 ****************************************************************************/
599 unsigned ltt_trace_eventtype_number(LttTrace
*t
)
604 for(i
=0;i
=t
->facility_number
;i
++){
605 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, i
);
606 count
+= f
->event_number
;
611 LttFacility
* ltt_trace_facility_by_id(LttTrace
* trace
, unsigned id
)
613 LttFacility
* facility
;
615 for(i
=0;i
<trace
->facility_number
;i
++){
616 facility
= (LttFacility
*) g_ptr_array_index(trace
->facilities
,i
);
617 if(id
>= facility
->base_id
&&
618 id
< facility
->base_id
+ facility
->event_number
)
621 if(i
==trace
->facility_number
) return NULL
;
622 else return facility
;
625 LttEventType
*ltt_trace_eventtype_get(LttTrace
*t
, unsigned evId
)
628 f
= ltt_trace_facility_by_id(t
,evId
);
630 return f
->events
[evId
- f
->base_id
];
633 /*****************************************************************************
634 *There is one "per cpu" tracefile for each CPU, numbered from 0 to
635 *the maximum number of CPU in the system. When the number of CPU installed
636 *is less than the maximum, some positions are unused. There are also a
637 *number of "control" tracefiles (facilities, interrupts...).
638 ****************************************************************************/
639 unsigned ltt_trace_control_tracefile_number(LttTrace
*t
)
641 return t
->control_tracefile_number
;
644 unsigned ltt_trace_per_cpu_tracefile_number(LttTrace
*t
)
646 return t
->per_cpu_tracefile_number
;
649 /*****************************************************************************
650 *It is possible to search for the tracefiles by name or by CPU position.
651 *The index within the tracefiles of the same type is returned if found
652 *and a negative value otherwise.
653 ****************************************************************************/
655 int ltt_trace_control_tracefile_find(LttTrace
*t
, char *name
)
657 LttTracefile
* tracefile
;
659 for(i
=0;i
<t
->control_tracefile_number
;i
++){
660 tracefile
= (LttTracefile
*)g_ptr_array_index(t
->control_tracefiles
, i
);
661 if(strcmp(tracefile
->name
, name
)==0)break;
663 if(i
== t
->control_tracefile_number
) return -1;
667 int ltt_trace_per_cpu_tracefile_find(LttTrace
*t
, unsigned i
)
669 LttTracefile
* tracefile
;
671 for(j
=0;j
<t
->per_cpu_tracefile_number
;j
++){
672 tracefile
= (LttTracefile
*)g_ptr_array_index(t
->per_cpu_tracefiles
, j
);
673 name
= atoi(tracefile
->name
);
674 if(name
== (int)i
)break;
676 if(j
== t
->per_cpu_tracefile_number
) return -1;
680 /*****************************************************************************
681 *Get a specific tracefile
682 ****************************************************************************/
684 LttTracefile
*ltt_trace_control_tracefile_get(LttTrace
*t
, unsigned i
)
686 return (LttTracefile
*)g_ptr_array_index(t
->per_cpu_tracefiles
, i
);
689 LttTracefile
*ltt_trace_per_cpu_tracefile_get(LttTrace
*t
, unsigned i
)
691 return (LttTracefile
*)g_ptr_array_index(t
->per_cpu_tracefiles
, i
);
694 /*****************************************************************************
695 *Get the name of a tracefile
696 ****************************************************************************/
698 char *ltt_tracefile_name(LttTracefile
*tf
)
703 /*****************************************************************************
705 * ltt_tracefile_seek_time: seek to the first event of the trace with time
706 * larger or equal to time
709 * time : criteria of the time
710 ****************************************************************************/
712 void ltt_tracefile_seek_time(LttTracefile
*t
, LttTime time
)
716 int headTime
= timecmp(&(t
->a_block_start
->time
), &time
);
717 int tailTime
= timecmp(&(t
->a_block_end
->time
), &time
);
720 if(headTime
< 0 && tailTime
> 0){
721 lttTime
= getEventTime(t
);
722 err
= timecmp(<tTime
, &time
);
724 if(t
->which_event
==2 || timecmp(&t
->prev_event_time
,&time
)<0){
728 return ltt_tracefile_seek_time(t
, time
);
732 ev
= ltt_tracefile_read(t
);
734 g_print("End of file\n");
738 lttTime
= getEventTime(t
);
739 err
= timecmp(<tTime
, &time
);
743 }else if(headTime
> 0){
744 if(t
->which_block
== 1){
747 if(timecmp(&(t
->prev_block_end_time
),&time
) >= 0 ){
748 err
=readBlock(t
,t
->which_block
-1);
749 if(err
) g_error("Can not read tracefile: %s\n", t
->name
);
750 return ltt_tracefile_seek_time(t
, time
) ;
755 }else if(tailTime
< 0){
756 if(t
->which_block
!= t
->block_number
){
757 err
=readBlock(t
,t
->which_block
+1);
758 if(err
) g_error("Can not read tracefile: %s\n", t
->name
);
760 g_print("End of file\n");
763 if(tailTime
< 0) return ltt_tracefile_seek_time(t
, time
);
764 }else if(headTime
== 0){
766 }else if(tailTime
== 0){
767 t
->cur_event_pos
= t
->a_block_end
- EVENT_HEADER_SIZE
;
768 t
->current_event_time
= time
;
769 t
->cur_heart_beat_number
= 0;
770 t
->prev_event_time
.tv_sec
= 0;
771 t
->prev_event_time
.tv_nsec
= 0;
776 /*****************************************************************************
778 * ltt_tracefile_read : read the current event, set the pointer to the next
782 * LttEvent * : an event to be processed
783 ****************************************************************************/
785 LttEvent
*ltt_tracefile_read(LttTracefile
*t
)
787 LttEvent
* lttEvent
= (LttEvent
*)g_new(LttEvent
, 1);
790 lttEvent
->event_id
= (int)(*(uint16_t *)(t
->cur_event_pos
));
791 if(lttEvent
->event_id
== TRACE_TIME_HEARTBEAT
)
792 t
->cur_heart_beat_number
++;
794 t
->prev_event_time
= t
->current_event_time
;
795 // t->current_event_time = getEventTime(t);
797 lttEvent
->time_delta
= *(uint32_t*)(t
->cur_event_pos
+ EVENT_ID_SIZE
);
798 lttEvent
->event_time
= t
->current_event_time
;
800 lttEvent
->event_cycle_count
= ((uint64_t)1)<<32 * t
->cur_heart_beat_number
801 + lttEvent
->time_delta
;
803 lttEvent
->tracefile
= t
;
804 lttEvent
->data
= t
->cur_event_pos
+ EVENT_HEADER_SIZE
;
806 //update the fields of the current event and go to the next event
812 if(err
== ENOENT
) return lttEvent
;
813 if(err
== ERANGE
) g_error("event id is out of range\n");
814 if(err
)g_error("Can not read tracefile\n");
819 /****************************************************************************
821 * readFile : wrap function to read from a file
823 * fd : file descriptor
824 * buf : buf to contain the content
825 * size : number of bytes to be read
826 * mesg : message to be printed if some thing goes wrong
829 * EIO : can not read from the file
830 ****************************************************************************/
832 int readFile(int fd
, void * buf
, size_t size
, char * mesg
)
835 nbBytes
= read(fd
, buf
, size
);
843 /****************************************************************************
845 * readBlock : read a block from the file
847 * lttdes : ltt trace file
848 * whichBlock : the block which will be read
851 * EINVAL : lseek fail
852 * EIO : can not read from the file
853 ****************************************************************************/
855 int readBlock(LttTracefile
* tf
, int whichBlock
)
860 if(whichBlock
- tf
->which_block
== 1 && tf
->which_block
!= 0){
861 tf
->prev_block_end_time
= tf
->a_block_end
->time
;
862 tf
->prev_event_time
= tf
->a_block_end
->time
;
864 tf
->prev_block_end_time
.tv_sec
= 0;
865 tf
->prev_block_end_time
.tv_nsec
= 0;
866 tf
->prev_event_time
.tv_sec
= 0;
867 tf
->prev_event_time
.tv_nsec
= 0;
870 nbBytes
=lseek(tf
->fd
,(off_t
)((whichBlock
-1)*tf
->block_size
), SEEK_SET
);
871 if(nbBytes
== -1) return EINVAL
;
873 if(readFile(tf
->fd
,tf
->buffer
,tf
->block_size
,"Unable to read a block"))
876 tf
->a_block_start
=(BlockStart
*) (tf
->buffer
+ EVENT_HEADER_SIZE
);
877 lostSize
= *(uint32_t*)(tf
->buffer
+ tf
->block_size
- sizeof(uint32_t));
878 tf
->a_block_end
=(BlockEnd
*)(tf
->buffer
+ tf
->block_size
-
879 lostSize
+ EVENT_HEADER_SIZE
);
881 tf
->which_block
= whichBlock
;
883 tf
->cur_event_pos
= tf
->buffer
;//the beginning of the block, block start ev
884 tf
->cur_heart_beat_number
= 0;
888 tf
->current_event_time
= getEventTime(tf
);
893 /*****************************************************************************
895 * updateTracefile : reinitialize the info of the block which is already
899 ****************************************************************************/
901 void updateTracefile(LttTracefile
* tf
)
904 tf
->cur_event_pos
= tf
->buffer
;
905 tf
->current_event_time
= getEventTime(tf
);
906 tf
->cur_heart_beat_number
= 0;
908 tf
->prev_event_time
.tv_sec
= 0;
909 tf
->prev_event_time
.tv_nsec
= 0;
912 /*****************************************************************************
914 * skipEvent : go to the next event, update the fields of the current event
919 * EINVAL : lseek fail
920 * EIO : can not read from the file
921 * ENOMEM : end of file
922 * ENOENT : last event
923 * ERANGE : event id is out of range
924 ****************************************************************************/
926 int skipEvent(LttTracefile
* t
)
932 static int evCount
= 0;
935 if(t
->which_block
== t
->block_number
&&
936 evCount
== t
->which_event
){
941 evId
= (int)(*(uint16_t *)(t
->cur_event_pos
));
942 evData
= t
->cur_event_pos
+ EVENT_HEADER_SIZE
;
944 //regard BLOCK_START, END and HEARTBEAT as special case, there are buildin events
945 if(evId
!= TRACE_BLOCK_START
&& evId
!= TRACE_BLOCK_END
&& evId
!= TRACE_TIME_HEARTBEAT
){
946 evT
= ltt_trace_eventtype_get(t
->trace
,(unsigned)evId
);
948 if(evT
) rootFld
= evT
->root_field
;
952 //event has string/sequence or the last event is not the same event
953 if((evT
->latest_block
!=t
->which_block
|| evT
->latest_event
!=t
->which_event
)
954 && rootFld
->field_fixed
== 0){
955 setFieldsOffset(t
, evT
, evData
, t
->trace
);
957 t
->cur_event_pos
+= EVENT_HEADER_SIZE
+ rootFld
->field_size
;
958 }else t
->cur_event_pos
+= EVENT_HEADER_SIZE
;
960 evT
->latest_block
= t
->which_block
;
961 evT
->latest_event
= t
->which_event
;
963 if(evId
== TRACE_BLOCK_START
|| evId
== TRACE_BLOCK_END
){
964 t
->cur_event_pos
+= sizeof(BlockStart
) + EVENT_HEADER_SIZE
;
966 t
->cur_event_pos
+= sizeof(TimeHeartbeat
) + EVENT_HEADER_SIZE
;
970 //the next event is in the next block
971 if(evId
== TRACE_BLOCK_END
){
972 if(t
->which_block
== t
->block_number
){
974 evCount
= t
->which_event
;
977 err
= readBlock(t
, t
->which_block
+ 1);
981 t
->current_event_time
= getEventTime(t
);
987 /*****************************************************************************
989 * getCyclePerNsec : calculate cycles per nsec for current block
992 ****************************************************************************/
994 void getCyclePerNsec(LttTracefile
* t
)
996 LttTime lBufTotalTime
; /* Total time for this buffer */
997 LttCycleCount lBufTotalNSec
; /* Total time for this buffer in nsecs */
998 LttCycleCount lBufTotalCycle
;/* Total cycles for this buffer */
1000 /* Calculate the total time for this buffer */
1001 TimeSub(lBufTotalTime
,t
->a_block_end
->time
, t
->a_block_start
->time
);
1003 /* Calculate the total cycles for this bufffer */
1004 lBufTotalCycle
= t
->a_block_end
->cycle_count
1005 - t
->a_block_start
->cycle_count
;
1007 /* Convert the total time to nsecs */
1008 lBufTotalNSec
= lBufTotalTime
.tv_sec
* 1000000000 + lBufTotalTime
.tv_nsec
;
1010 t
->cycle_per_nsec
= (double)lBufTotalCycle
/ (double)lBufTotalNSec
;
1013 /****************************************************************************
1015 * getEventTime : obtain the time of an event
1019 * LttTime : the time of the event
1020 ****************************************************************************/
1022 LttTime
getEventTime(LttTracefile
* tf
)
1025 LttCycleCount cycle_count
; // cycle count for the current event
1026 LttCycleCount lEventTotalCycle
; // Total cycles from start for event
1027 double lEventNSec
; // Total usecs from start for event
1028 LttTime lTimeOffset
; // Time offset in struct LttTime
1031 evId
= *(uint16_t*)tf
->cur_event_pos
;
1032 if(evId
== TRACE_BLOCK_START
)
1033 return tf
->a_block_start
->time
;
1034 else if(evId
== TRACE_BLOCK_END
)
1035 return tf
->a_block_end
->time
;
1038 // Calculate total time in cycles from start of buffer for this event
1039 cycle_count
= (LttCycleCount
)*(uint32_t*)(tf
->cur_event_pos
+ EVENT_ID_SIZE
);
1040 if(tf
->cur_heart_beat_number
)
1041 cycle_count
+= ((uint64_t)1)<<32 * tf
->cur_heart_beat_number
;
1042 lEventTotalCycle
= cycle_count
- tf
->a_block_start
->cycle_count
;
1044 // Convert it to nsecs
1045 lEventNSec
= lEventTotalCycle
/ tf
->cycle_per_nsec
;
1047 // Determine offset in struct LttTime
1048 lTimeOffset
.tv_nsec
= (long)lEventNSec
% 1000000000;
1049 lTimeOffset
.tv_sec
= (long)lEventNSec
/ 1000000000;
1051 TimeAdd(time
, tf
->a_block_start
->time
, lTimeOffset
);
1056 /*****************************************************************************
1058 * setFieldsOffset : set offset of the fields
1060 * tracefile : opened trace file
1061 * evT : the event type
1062 * evD : event data, it may be NULL
1063 ****************************************************************************/
1065 void setFieldsOffset(LttTracefile
*tf
,LttEventType
*evT
,void *evD
,LttTrace
* t
)
1067 LttField
* rootFld
= evT
->root_field
;
1068 // rootFld->base_address = evD;
1071 rootFld
->field_size
= getFieldtypeSize(tf
, evT
, 0,0,rootFld
, evD
,t
);
1074 /*****************************************************************************
1076 * getFieldtypeSize: get the size of the field type (primitive type)
1078 * tracefile : opened trace file
1080 * offsetRoot : offset from the root
1081 * offsetParent : offset from the parrent
1083 * evD : event data, it may be NULL
1085 * int : size of the field
1086 ****************************************************************************/
1088 int getFieldtypeSize(LttTracefile
* t
, LttEventType
* evT
, int offsetRoot
,
1089 int offsetParent
, LttField
* fld
, void *evD
, LttTrace
*trace
)
1091 int size
, size1
, element_number
, i
, offset1
, offset2
;
1092 LttType
* type
= fld
->field_type
;
1095 if(evT
->latest_block
==t
->which_block
&& evT
->latest_event
==t
->which_event
){
1096 return fld
->field_size
;
1100 if(fld
->field_fixed
== 1){
1101 if(fld
== evT
->root_field
) return fld
->field_size
;
1104 if(type
->type_class
!= LTT_STRUCT
&& type
->type_class
!= LTT_ARRAY
&&
1105 type
->type_class
!= LTT_SEQUENCE
&& type
->type_class
!= LTT_STRING
){
1106 if(fld
->field_fixed
== -1){
1107 size
= (int) ltt_type_size(trace
, type
);
1108 fld
->field_fixed
= 1;
1109 }else size
= fld
->field_size
;
1111 }else if(type
->type_class
== LTT_ARRAY
){
1112 element_number
= (int) type
->element_number
;
1113 if(fld
->field_fixed
== -1){
1114 size
= getFieldtypeSize(t
, evT
, offsetRoot
,0,fld
->child
[0], NULL
, trace
);
1115 if(size
== 0){ //has string or sequence
1116 fld
->field_fixed
= 0;
1118 fld
->field_fixed
= 1;
1119 size
*= element_number
;
1121 }else if(fld
->field_fixed
== 0){// has string or sequence
1123 for(i
=0;i
<element_number
;i
++){
1124 size
+= getFieldtypeSize(t
, evT
, offsetRoot
+size
,size
,
1125 fld
->child
[0], evD
+size
, trace
);
1127 }else size
= fld
->field_size
;
1129 }else if(type
->type_class
== LTT_SEQUENCE
){
1130 size1
= (int) ltt_type_size(trace
, type
);
1131 if(fld
->field_fixed
== -1){
1132 fld
->field_fixed
= 0;
1133 size
= getFieldtypeSize(t
, evT
, offsetRoot
,0,fld
->child
[0], NULL
, trace
);
1134 fld
->element_size
= size
;
1136 element_number
= getIntNumber(size1
,evD
);
1137 type
->element_number
= element_number
;
1138 if(fld
->element_size
> 0){
1139 size
= element_number
* fld
->element_size
;
1140 }else{//sequence has string or sequence
1142 for(i
=0;i
<element_number
;i
++){
1143 size
+= getFieldtypeSize(t
, evT
, offsetRoot
+size
+size1
,size
+size1
,
1144 fld
->child
[0], evD
+size
+size1
, trace
);
1150 }else if(type
->type_class
== LTT_STRING
){
1152 if(fld
->field_fixed
== -1){
1153 fld
->field_fixed
= 0;
1155 size
= strlen((char*)evD
) + 1; //include end : '\0'
1158 }else if(type
->type_class
== LTT_STRUCT
){
1159 element_number
= (int) type
->element_number
;
1161 if(fld
->field_fixed
== -1){
1162 offset1
= offsetRoot
;
1164 for(i
=0;i
<element_number
;i
++){
1165 size1
=getFieldtypeSize(t
, evT
,offset1
,offset2
, fld
->child
[i
], NULL
, trace
);
1166 if(size1
> 0 && size
>= 0){
1168 if(offset1
>= 0) offset1
+= size1
;
1177 fld
->field_fixed
= 0;
1179 }else fld
->field_fixed
= 1;
1180 }else if(fld
->field_fixed
== 0){
1181 offset1
= offsetRoot
;
1183 for(i
=0;i
<element_number
;i
++){
1184 size
=getFieldtypeSize(t
,evT
,offset1
,offset2
,fld
->child
[i
],evD
+offset2
, trace
);
1189 }else size
= fld
->field_size
;
1192 fld
->offset_root
= offsetRoot
;
1193 fld
->offset_parent
= offsetParent
;
1195 fld
->fixed_root
= (offsetRoot
==-1) ? 0 : 1;
1196 fld
->fixed_parent
= (offsetParent
==-1) ? 0 : 1;
1198 fld
->field_size
= size
;
1203 /*****************************************************************************
1205 * timecmp : compare two time
1210 * int : 0: t1 == t2; -1: t1 < t2; 1: t1 > t2
1211 ****************************************************************************/
1213 int timecmp(LttTime
* t1
, LttTime
* t2
)
1216 TimeSub(T
, *t1
, *t2
);
1217 if(T
.tv_sec
== 0 && T
.tv_nsec
== 0) return 0;
1218 else if(T
.tv_sec
> 0 || (T
.tv_sec
==0 && T
.tv_nsec
> 0)) return 1;
1222 /*****************************************************************************
1224 * getIntNumber : get an integer number
1226 * size : the size of the integer
1227 * evD : the event data
1230 ****************************************************************************/
1232 int getIntNumber(int size
, void *evD
)
1235 if(size
== 1) i
= *(int8_t *)evD
;
1236 else if(size
== 2) i
= *(int16_t *)evD
;
1237 else if(size
== 4) i
= *(int32_t *)evD
;
1238 else if(size
== 8) i
= *(int64_t *)evD
;
1243 /*****************************************************************************
1245 * getDataEndianType : get the data type size and endian type of the local
1248 * size : size of data type
1249 * endian : endian type, little or big
1250 ****************************************************************************/
1252 void getDataEndianType(LttArchSize
* size
, LttArchEndian
* endian
)
1256 int sizeInt
=sizeof(int), sizeLong
=sizeof(long), sizePointer
=sizeof(void *);
1258 if(c
== 1) *endian
= LTT_LITTLE_ENDIAN
;
1259 else *endian
= LTT_BIG_ENDIAN
;
1261 if(sizeInt
== 2 && sizeLong
== 4 && sizePointer
== 4)
1263 else if(sizeInt
== 4 && sizeLong
== 4 && sizePointer
== 4)
1265 else if(sizeInt
== 4 && sizeLong
== 8 && sizePointer
== 8)
1267 else if(sizeInt
== 8 && sizeLong
== 8 && sizePointer
== 8)
1269 else *size
= LTT_UNKNOWN
;