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
* t
, LttEventType
*evT
, void *evD
);
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
* t
, LttEventType
* evT
, int offsetRoot
,
21 int offsetParent
, LttField
* fld
, void * evD
);
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_error("The input data file %s does not contain a trace\n", fileName
);
108 //store the size of the file
109 tf
->file_size
= lTDFStat
.st_size
;
110 tf
->block_size
= t
->system_description
->ltt_block_size
;
111 tf
->block_number
= tf
->file_size
/ tf
->block_size
;
114 //allocate memory to contain the info of a block
115 tf
->buffer
= (void *) g_new(char, t
->system_description
->ltt_block_size
);
117 //read the first block
118 if(readBlock(tf
,1)) exit(1);
124 /*****************************************************************************
125 *Open control and per cpu tracefiles
126 ****************************************************************************/
128 void ltt_tracefile_open_cpu(LttTrace
*t
, char * tracefile_name
)
131 tf
= ltt_tracefile_open(t
,tracefile_name
);
132 t
->per_cpu_tracefile_number
++;
133 g_ptr_array_add(t
->per_cpu_tracefiles
, tf
);
136 void ltt_tracefile_open_control(LttTrace
*t
, char * control_name
)
146 tf
= ltt_tracefile_open(t
,control_name
);
147 t
->control_tracefile_number
++;
148 g_ptr_array_add(t
->control_tracefiles
,tf
);
150 //parse facilities tracefile to get base_id
151 if(strcmp(control_name
,"facilities") ==0){
153 evId
= *(uint16_t*)tf
->cur_event_pos
;
154 if(evId
== TRACE_FACILITY_LOAD
){
155 pos
= tf
->cur_event_pos
+ EVENT_HEADER_SIZE
;
156 fLoad
.name
= (char*)pos
;
157 fLoad
.checksum
= *(LttChecksum
*)(pos
+ sizeof(pos
));
158 fLoad
.base_code
= *(uint32_t*)(pos
+ sizeof(pos
) + sizeof(LttChecksum
));
160 for(i
=0;i
<t
->facility_number
;i
++){
161 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
,i
);
162 if(strcmp(f
->name
,fLoad
.name
)==0 && fLoad
.checksum
==f
->checksum
){
163 f
->base_id
= fLoad
.base_code
;
167 if(i
==t
->facility_number
)
168 g_error("Facility: %s, checksum: %d is not founded\n",
169 fLoad
.name
,fLoad
.checksum
);
171 ev
= ltt_tracefile_read(tf
); //get next event
172 if(!ev
) break; //end of tracefile
173 }else if(evId
== TRACE_BLOCK_END
){
174 //can only reach here if it is the first event
175 g_error("Facilities does not contain any facility_load event\n");
176 }else g_error("Not valid facilities trace file\n");
181 /*****************************************************************************
183 * ltt_tracefile_close: close a trace file,
185 * t : tracefile which will be closed
186 ****************************************************************************/
188 void ltt_tracefile_close(LttTracefile
*t
)
196 /*****************************************************************************
197 *Get system information
198 ****************************************************************************/
199 void getSystemInfo(LttSystemDescription
* des
, char * pathname
)
203 int entry_number
= 15;
204 char buf
[DIR_NAME_SIZE
];
205 char description
[4*DIR_NAME_SIZE
];
208 fp
= fopen(pathname
,"r");
210 g_error("Can not open file : %s\n", pathname
);
213 while(fgets(buf
,DIR_NAME_SIZE
, fp
)!= NULL
){
215 while(isspace(*ptr
)) ptr
++;
216 if(strlen(ptr
) == 0) continue;
220 if(strlen(ptr
) == 0) g_error("Not a valid file: %s\n", pathname
);
221 if(strncmp("<system",ptr
,7) !=0)g_error("Not a valid file: %s\n", pathname
);
223 for(i
=0;i
<entry_number
;i
++){
224 if(fgets(buf
,DIR_NAME_SIZE
, fp
)== NULL
)
225 g_error("Not a valid file: %s\n", pathname
);
227 while(isspace(*ptr
)) ptr
++;
230 if(strncmp("node_name=",ptr
,10)!=0)
231 g_error("Not a valid file: %s\n", pathname
);
232 des
->node_name
= g_strdup(ptr
+10);
235 if(strncmp("domainname=",ptr
,11)!=0)
236 g_error("Not a valid file: %s\n", pathname
);
237 des
->domain_name
= g_strdup(ptr
+11);
240 if(strncmp("cpu=",ptr
,4)!=0)
241 g_error("Not a valid file: %s\n", pathname
);
242 des
->nb_cpu
= (unsigned)atoi(ptr
+4);
245 if(strncmp("arch_size=",ptr
,10)!=0)
246 g_error("Not a valid file: %s\n", pathname
);
247 if(strcmp(ptr
+10,"\"LP32\"") == 0) des
->size
= LTT_LP32
;
248 else if(strcmp(ptr
+10,"\"ILP32\"") == 0) des
->size
= LTT_ILP32
;
249 else if(strcmp(ptr
+10,"\"LP64\"") == 0) des
->size
= LTT_LP64
;
250 else if(strcmp(ptr
+10,"\"ILP64\"") == 0) des
->size
= LTT_ILP64
;
251 else if(strcmp(ptr
+10,"\"UNKNOWN\"") == 0) des
->size
= LTT_UNKNOWN
;
254 if(strncmp("endian=",ptr
,7)!=0)
255 g_error("Not a valid file: %s\n", pathname
);
256 if(strcmp(ptr
+7,"\"LITTLE_ENDIAN\"") == 0)
257 des
->endian
= LTT_LITTLE_ENDIAN
;
258 else if(strcmp(ptr
+7,"\"BIG_ENDIAN\"") == 0)
259 des
->endian
= LTT_BIG_ENDIAN
;
262 if(strncmp("kernel_name=",ptr
,12)!=0)
263 g_error("Not a valid file: %s\n", pathname
);
264 des
->kernel_name
= g_strdup(ptr
+12);
267 if(strncmp("kernel_release=",ptr
,15)!=0)
268 g_error("Not a valid file: %s\n", pathname
);
269 des
->kernel_release
= g_strdup(ptr
+15);
272 if(strncmp("kernel_version=",ptr
,15)!=0)
273 g_error("Not a valid file: %s\n", pathname
);
274 des
->kernel_version
= g_strdup(ptr
+15);
277 if(strncmp("machine=",ptr
,8)!=0)
278 g_error("Not a valid file: %s\n", pathname
);
279 des
->machine
= g_strdup(ptr
+8);
282 if(strncmp("processor=",ptr
,10)!=0)
283 g_error("Not a valid file: %s\n", pathname
);
284 des
->processor
= g_strdup(ptr
+10);
287 if(strncmp("hardware_platform=",ptr
,18)!=0)
288 g_error("Not a valid file: %s\n", pathname
);
289 des
->hardware_platform
= g_strdup(ptr
+18);
292 if(strncmp("operating_system=",ptr
,17)!=0)
293 g_error("Not a valid file: %s\n", pathname
);
294 des
->operating_system
= g_strdup(ptr
+17);
297 if(strncmp("ltt_major_version=",ptr
,18)!=0)
298 g_error("Not a valid file: %s\n", pathname
);
300 ptr
++;//skip begining "
301 ptr
[strlen(ptr
)-1] = '\0'; //get rid of the ending "
302 des
->ltt_major_version
= (unsigned)atoi(ptr
);
305 if(strncmp("ltt_minor_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_minor_version
= (unsigned)atoi(ptr
);
313 if(strncmp("ltt_block_size=",ptr
,15)!=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_block_size
= (unsigned)atoi(ptr
);
321 g_error("Not a valid file: %s\n", pathname
);
326 description
[0] = '\0';
327 if(fgets(buf
,DIR_NAME_SIZE
, fp
)== NULL
)
328 g_error("Not a valid file: %s\n", pathname
);
330 while(isspace(*ptr
)) ptr
++;
331 if(*ptr
!= '>') g_error("Not a valid file: %s\n", pathname
);
332 while((ptr
=fgets(buf
,DIR_NAME_SIZE
, fp
))!= NULL
){
334 while(isspace(*ptr
)) ptr
++;
335 if(strncmp("</system>",ptr
,9) == 0 )break;
336 strcat(description
, buf
);
338 if(!ptr
)g_error("Not a valid file: %s\n", pathname
);
339 if(description
[0] = '\0')des
->description
= NULL
;
340 des
->description
= g_strdup(description
);
345 /*****************************************************************************
346 *The following functions get facility/tracefile information
347 ****************************************************************************/
349 void getFacilityInfo(LttTrace
*t
, char* eventdefs
)
352 struct dirent
*entry
;
357 LttTracefile
* tracefile
;
359 dir
= opendir(eventdefs
);
360 if(!dir
) g_error("Can not open directory: %s\n", eventdefs
);
362 while((entry
= readdir(dir
)) != NULL
){
363 ptr
= &entry
->d_name
[strlen(entry
->d_name
)-4];
364 if(strcmp(ptr
,".xml") != 0) continue;
365 ltt_facility_open(t
,entry
->d_name
);
369 tracefile
= (LttTracefile
*)g_ptr_array_index(t
->per_cpu_tracefiles
,0);
370 for(j
=0;j
<t
->facility_number
;j
++){
371 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, i
);
372 for(i
=0; i
<f
->event_number
; i
++){
374 setFieldsOffset(tracefile
, et
, NULL
);
379 void getControlFileInfo(LttTrace
*t
, char* control
)
382 struct dirent
*entry
;
384 dir
= opendir(control
);
385 if(!dir
) g_error("Can not open directory: %s\n", control
);
387 while((entry
= readdir(dir
)) != NULL
){
388 if(strcmp(entry
->d_name
,"facilities") != 0 ||
389 strcmp(entry
->d_name
,"interrupts") != 0 ||
390 strcmp(entry
->d_name
,"processes") != 0) continue;
392 ltt_tracefile_open_control(t
,entry
->d_name
);
397 void getCpuFileInfo(LttTrace
*t
, char* cpu
)
400 struct dirent
*entry
;
403 if(!dir
) g_error("Can not open directory: %s\n", cpu
);
405 while((entry
= readdir(dir
)) != NULL
){
406 if(strcmp(entry
->d_name
,".") != 0 ||
407 strcmp(entry
->d_name
,"..") != 0 ){
408 ltt_tracefile_open_cpu(t
,entry
->d_name
);
414 /*****************************************************************************
415 *A trace is specified as a pathname to the directory containing all the
416 *associated data (control tracefiles, per cpu tracefiles, event
419 *When a trace is closed, all the associated facilities, types and fields
420 *are released as well.
421 ****************************************************************************/
423 LttTrace
*ltt_trace_open(char *pathname
)
426 LttSystemDescription
* sys_description
;
427 char eventdefs
[DIR_NAME_SIZE
];
428 char info
[DIR_NAME_SIZE
];
429 char control
[DIR_NAME_SIZE
];
430 char cpu
[DIR_NAME_SIZE
];
431 char tmp
[DIR_NAME_SIZE
];
432 gboolean has_slash
= FALSE
;
434 //establish the pathname to different directories
435 if(pathname
[strlen(pathname
)-1] == '/')has_slash
= TRUE
;
436 strcpy(eventdefs
,pathname
);
437 if(!has_slash
)strcat(eventdefs
,"/");
438 strcat(eventdefs
,"eventdefs/");
440 strcpy(info
,pathname
);
441 if(!has_slash
)strcat(info
,"/");
442 strcat(info
,"info/");
444 strcpy(control
,pathname
);
445 if(!has_slash
)strcat(control
,"/");
446 strcat(control
,"control/");
448 strcpy(cpu
,pathname
);
449 if(!has_slash
)strcat(cpu
,"/");
453 t
= g_new(LttTrace
, 1);
454 sys_description
= g_new(LttSystemDescription
, 1);
455 t
->pathname
= g_strdup(pathname
);
456 t
->facility_number
= 0;
457 t
->control_tracefile_number
= 0;
458 t
->per_cpu_tracefile_number
= 0;
459 t
->system_description
= sys_description
;
460 t
->control_tracefiles
= g_ptr_array_new();
461 t
->per_cpu_tracefiles
= g_ptr_array_new();
462 t
->facilities
= g_ptr_array_new();
463 getDataEndianType(&(t
->my_arch_size
), &(t
->my_arch_endian
));
465 //get system description
467 strcat(tmp
,"system.xml");
468 getSystemInfo(sys_description
, tmp
);
470 //get control tracefile info
471 getControlFileInfo(t
,control
);
473 //get cpu tracefile info
474 getCpuFileInfo(t
,cpu
);
476 //get facilities info
477 getFacilityInfo(t
,eventdefs
);
482 void ltt_trace_close(LttTrace
*t
)
490 //free system_description
491 g_free(t
->system_description
->description
);
492 g_free(t
->system_description
->node_name
);
493 g_free(t
->system_description
->domain_name
);
494 g_free(t
->system_description
->kernel_name
);
495 g_free(t
->system_description
->kernel_release
);
496 g_free(t
->system_description
->kernel_version
);
497 g_free(t
->system_description
->machine
);
498 g_free(t
->system_description
->processor
);
499 g_free(t
->system_description
->hardware_platform
);
500 g_free(t
->system_description
->operating_system
);
501 g_free(t
->system_description
);
503 //free control_tracefiles
504 for(i
=0;i
<t
->control_tracefile_number
;i
++){
505 tf
= (LttTracefile
*)g_ptr_array_index(t
->control_tracefiles
,i
);
506 ltt_tracefile_close(tf
);
508 g_ptr_array_free(t
->control_tracefiles
, FALSE
);
510 //free per_cpu_tracefiles
511 for(i
=0;i
<t
->per_cpu_tracefile_number
;i
++){
512 tf
= (LttTracefile
*)g_ptr_array_index(t
->per_cpu_tracefiles
,i
);
513 ltt_tracefile_close(tf
);
515 g_ptr_array_free(t
->per_cpu_tracefiles
, FALSE
);
518 for(i
=0;i
<t
->facility_number
;i
++){
519 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
,i
);
520 ltt_facility_close(f
);
522 g_ptr_array_free(t
->facilities
, FALSE
);
528 /*****************************************************************************
529 *Get the system description of the trace
530 ****************************************************************************/
532 LttSystemDescription
*ltt_trace_system_description(LttTrace
*t
)
534 return t
->system_description
;
537 /*****************************************************************************
538 * The following functions discover the facilities of the trace
539 ****************************************************************************/
541 unsigned ltt_trace_facility_number(LttTrace
*t
)
543 return (unsigned)(t
->facility_number
);
546 LttFacility
*ltt_trace_facility_get(LttTrace
*t
, unsigned i
)
548 return (LttFacility
*)g_ptr_array_index(t
->facilities
, i
);
551 /*****************************************************************************
553 * ltt_trace_facility_find : find facilities in the trace
556 * name : facility name
558 * position : position of the facility in the trace
560 * : the number of facilities
561 ****************************************************************************/
563 unsigned ltt_trace_facility_find(LttTrace
*t
, char *name
, unsigned *position
)
567 for(i
=0;i
=t
->facility_number
;i
++){
568 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, i
);
569 if(strcmp(f
->name
,name
)==0){
571 if(count
==1) *position
= i
;
579 /*****************************************************************************
580 * Functions to discover all the event types in the trace
581 ****************************************************************************/
583 unsigned ltt_trace_eventtype_number(LttTrace
*t
)
588 for(i
=0;i
=t
->facility_number
;i
++){
589 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, i
);
590 count
+= f
->event_number
;
595 LttFacility
* ltt_trace_facility_by_id(LttTrace
* trace
, unsigned id
)
597 LttFacility
* facility
;
599 for(i
=0;i
<trace
->facility_number
;i
++){
600 facility
= (LttFacility
*) g_ptr_array_index(trace
->facilities
,i
);
601 if(id
>= facility
->base_id
&&
602 id
< facility
->base_id
+ facility
->event_number
)
605 if(i
==trace
->facility_number
) return NULL
;
606 else return facility
;
609 LttEventType
*ltt_trace_eventtype_get(LttTrace
*t
, unsigned evId
)
612 f
= ltt_trace_facility_by_id(t
,evId
);
614 return f
->events
[evId
- f
->base_id
];
617 /*****************************************************************************
618 *There is one "per cpu" tracefile for each CPU, numbered from 0 to
619 *the maximum number of CPU in the system. When the number of CPU installed
620 *is less than the maximum, some positions are unused. There are also a
621 *number of "control" tracefiles (facilities, interrupts...).
622 ****************************************************************************/
623 unsigned ltt_trace_control_tracefile_number(LttTrace
*t
)
625 return t
->control_tracefile_number
;
628 unsigned ltt_trace_per_cpu_tracefile_number(LttTrace
*t
)
630 return t
->per_cpu_tracefile_number
;
633 /*****************************************************************************
634 *It is possible to search for the tracefiles by name or by CPU position.
635 *The index within the tracefiles of the same type is returned if found
636 *and a negative value otherwise.
637 ****************************************************************************/
639 int ltt_trace_control_tracefile_find(LttTrace
*t
, char *name
)
641 LttTracefile
* tracefile
;
643 for(i
=0;i
<t
->control_tracefile_number
;i
++){
644 tracefile
= (LttTracefile
*)g_ptr_array_index(t
->control_tracefiles
, i
);
645 if(strcmp(tracefile
->name
, name
)==0)break;
647 if(i
== t
->control_tracefile_number
) return -1;
651 int ltt_trace_per_cpu_tracefile_find(LttTrace
*t
, unsigned i
)
653 LttTracefile
* tracefile
;
655 for(j
=0;j
<t
->per_cpu_tracefile_number
;j
++){
656 tracefile
= (LttTracefile
*)g_ptr_array_index(t
->per_cpu_tracefiles
, j
);
657 name
= atoi(tracefile
->name
);
658 if(name
== (int)i
)break;
660 if(j
== t
->per_cpu_tracefile_number
) return -1;
664 /*****************************************************************************
665 *Get a specific tracefile
666 ****************************************************************************/
668 LttTracefile
*ltt_trace_control_tracefile_get(LttTrace
*t
, unsigned i
)
670 return (LttTracefile
*)g_ptr_array_index(t
->per_cpu_tracefiles
, i
);
673 LttTracefile
*ltt_trace_per_cpu_tracefile_get(LttTrace
*t
, unsigned i
)
675 return (LttTracefile
*)g_ptr_array_index(t
->per_cpu_tracefiles
, i
);
678 /*****************************************************************************
679 *Get the name of a tracefile
680 ****************************************************************************/
682 char *ltt_tracefile_name(LttTracefile
*tf
)
687 /*****************************************************************************
689 * ltt_tracefile_seek_time: seek to the first event of the trace with time
690 * larger or equal to time
693 * time : criteria of the time
694 ****************************************************************************/
696 void ltt_tracefile_seek_time(LttTracefile
*t
, LttTime time
)
700 int headTime
= timecmp(&(t
->a_block_start
->time
), &time
);
701 int tailTime
= timecmp(&(t
->a_block_end
->time
), &time
);
703 if(headTime
< 0 && tailTime
> 0){
704 lttTime
= getEventTime(t
);
705 err
= timecmp(<tTime
, &time
);
707 if(t
->which_event
==1 || timecmp(&t
->prev_event_time
,&time
)<0){
711 return ltt_tracefile_seek_time(t
, time
);
714 err
= t
->which_block
;
715 if(ltt_tracefile_read(t
) == NULL
){
716 g_printf("End of file\n");
719 if(t
->which_block
== err
)
720 return ltt_tracefile_seek_time(t
,time
);
722 }else if(headTime
> 0){
723 if(t
->which_block
== 1){
726 if( (t
->prev_block_end_time
.tv_sec
== 0 &&
727 t
->prev_block_end_time
.tv_nsec
== 0 ) ||
728 timecmp(&(t
->prev_block_end_time
),&time
) > 0 ){
729 err
=readBlock(t
,t
->which_block
-1);
730 if(err
) g_error("Can not read tracefile: %s\n", t
->name
);
731 return ltt_tracefile_seek_time(t
, time
) ;
736 }else if(tailTime
<= 0){
737 if(t
->which_block
!= t
->block_number
){
738 err
=readBlock(t
,t
->which_block
+1);
739 if(err
) g_error("Can not read tracefile: %s\n", t
->name
);
741 g_printf("End of file\n");
744 if(tailTime
< 0) return ltt_tracefile_seek_time(t
, time
);
745 }else if(headTime
== 0){
750 /*****************************************************************************
752 * ltt_tracefile_read : read the next event
756 * LttEvent * : an event to be processed
757 ****************************************************************************/
759 LttEvent
*ltt_tracefile_read(LttTracefile
*t
)
761 LttEvent
* lttEvent
= (LttEvent
*)g_new(LttEvent
, 1);
764 //update the fields of the current event and go to the next event
766 if(err
== ENOENT
) return NULL
;
767 if(err
== ERANGE
) g_error("event id is out of range\n");
768 if(err
)g_error("Can not read tracefile\n");
770 lttEvent
->event_id
= (int)(*(uint16_t *)(t
->cur_event_pos
));
771 if(lttEvent
->event_id
== TRACE_TIME_HEARTBEAT
)
772 t
->cur_heart_beat_number
++;
774 t
->current_event_time
= getEventTime(t
);
776 lttEvent
->time_delta
= *(uint32_t*)(t
->cur_event_pos
+ EVENT_ID_SIZE
);
777 lttEvent
->event_time
= t
->current_event_time
;
779 lttEvent
->event_cycle_count
= ((uint64_t)1)<<32 * t
->cur_heart_beat_number
780 + lttEvent
->time_delta
;
782 lttEvent
->tracefile
= t
;
783 lttEvent
->data
= t
->cur_event_pos
+ EVENT_HEADER_SIZE
;
788 /****************************************************************************
790 * readFile : wrap function to read from a file
792 * fd : file descriptor
793 * buf : buf to contain the content
794 * size : number of bytes to be read
795 * mesg : message to be printed if some thing goes wrong
798 * EIO : can not read from the file
799 ****************************************************************************/
801 int readFile(int fd
, void * buf
, size_t size
, char * mesg
)
804 nbBytes
= read(fd
, buf
, size
);
812 /****************************************************************************
814 * readBlock : read a block from the file
816 * lttdes : ltt trace file
817 * whichBlock : the block which will be read
820 * EINVAL : lseek fail
821 * EIO : can not read from the file
822 ****************************************************************************/
824 int readBlock(LttTracefile
* tf
, int whichBlock
)
829 if(whichBlock
- tf
->which_block
== 1 && tf
->which_block
!= 0){
830 tf
->prev_block_end_time
= tf
->a_block_end
->time
;
832 tf
->prev_block_end_time
.tv_sec
= 0;
833 tf
->prev_block_end_time
.tv_nsec
= 0;
835 tf
->prev_event_time
.tv_sec
= 0;
836 tf
->prev_event_time
.tv_nsec
= 0;
838 nbBytes
=lseek(tf
->fd
,(off_t
)((whichBlock
-1)*tf
->block_size
), SEEK_SET
);
839 if(nbBytes
== -1) return EINVAL
;
841 if(readFile(tf
->fd
,tf
->buffer
,tf
->block_size
,"Unable to read a block"))
844 tf
->a_block_start
=(BlockStart
*) (tf
->buffer
+ EVENT_HEADER_SIZE
);
845 lostSize
= *(uint32_t*)(tf
->buffer
+ tf
->block_size
- sizeof(uint32_t));
846 tf
->a_block_end
=(BlockEnd
*)(tf
->buffer
+ tf
->block_size
-
847 lostSize
+ EVENT_HEADER_SIZE
);
849 tf
->which_block
= whichBlock
;
851 tf
->cur_event_pos
= tf
->a_block_start
+ sizeof(BlockStart
); //first event
852 tf
->cur_heart_beat_number
= 0;
854 tf
->current_event_time
= getEventTime(tf
);
861 /*****************************************************************************
863 * updateTracefile : reinitialize the info of the block which is already
867 ****************************************************************************/
869 void updateTracefile(LttTracefile
* tf
)
872 tf
->cur_event_pos
= tf
->a_block_start
+ sizeof(BlockStart
);
873 tf
->current_event_time
= getEventTime(tf
);
874 tf
->cur_heart_beat_number
= 0;
876 tf
->prev_event_time
.tv_sec
= 0;
877 tf
->prev_event_time
.tv_nsec
= 0;
880 /*****************************************************************************
882 * skipEvent : go to the next event, update the fields of the current event
887 * EINVAL : lseek fail
888 * EIO : can not read from the file
889 * ENOENT : end of file
890 * ERANGE : event id is out of range
891 ****************************************************************************/
893 int skipEvent(LttTracefile
* t
)
900 evId
= (int)(*(uint16_t *)(t
->cur_event_pos
));
901 evData
= t
->cur_event_pos
+ EVENT_HEADER_SIZE
;
902 evT
= ltt_trace_eventtype_get(t
->trace
,(unsigned)evId
);
904 if(evT
) rootFld
= evT
->root_field
;
907 t
->prev_event_time
= getEventTime(t
);
909 //event has string/sequence or the last event is not the same event
910 if((evT
->latest_block
!=t
->which_block
|| evT
->latest_event
!=t
->which_event
)
911 && rootFld
->field_fixed
== 0){
912 setFieldsOffset(t
, evT
, evData
);
914 t
->cur_event_pos
+= EVENT_HEADER_SIZE
+ rootFld
->field_size
;
916 evT
->latest_block
= t
->which_block
;
917 evT
->latest_event
= t
->which_event
;
919 //the next event is in the next block
920 if(evId
== TRACE_BLOCK_END
){
921 if(t
->which_block
== t
->block_number
) return ENOENT
;
922 err
= readBlock(t
, t
->which_block
+ 1);
931 /*****************************************************************************
933 * getCyclePerNsec : calculate cycles per nsec for current block
936 ****************************************************************************/
938 void getCyclePerNsec(LttTracefile
* t
)
940 LttTime lBufTotalTime
; /* Total time for this buffer */
941 LttCycleCount lBufTotalNSec
; /* Total time for this buffer in nsecs */
942 LttCycleCount lBufTotalCycle
;/* Total cycles for this buffer */
944 /* Calculate the total time for this buffer */
945 TimeSub(lBufTotalTime
,t
->a_block_end
->time
, t
->a_block_start
->time
);
947 /* Calculate the total cycles for this bufffer */
948 lBufTotalCycle
= t
->a_block_end
->cycle_count
949 - t
->a_block_start
->cycle_count
;
951 /* Convert the total time to nsecs */
952 lBufTotalNSec
= lBufTotalTime
.tv_sec
* 1000000000 + lBufTotalTime
.tv_nsec
;
954 t
->cycle_per_nsec
= (double)lBufTotalCycle
/ (double)lBufTotalNSec
;
957 /****************************************************************************
959 * getEventTime : obtain the time of an event
963 * LttTime : the time of the event
964 ****************************************************************************/
966 LttTime
getEventTime(LttTracefile
* tf
)
969 LttCycleCount cycle_count
; // cycle count for the current event
970 LttCycleCount lEventTotalCycle
; // Total cycles from start for event
971 double lEventNSec
; // Total usecs from start for event
972 LttTime lTimeOffset
; // Time offset in struct LttTime
974 // Calculate total time in cycles from start of buffer for this event
975 cycle_count
= (LttCycleCount
)*(uint32_t*)(tf
->cur_event_pos
+ EVENT_ID_SIZE
);
976 if(tf
->cur_heart_beat_number
)
977 cycle_count
+= ((uint64_t)1)<<32 * tf
->cur_heart_beat_number
;
978 lEventTotalCycle
= cycle_count
- tf
->a_block_start
->cycle_count
;
980 // Convert it to nsecs
981 lEventNSec
= lEventTotalCycle
/ tf
->cycle_per_nsec
;
983 // Determine offset in struct LttTime
984 lTimeOffset
.tv_nsec
= (long)lEventNSec
% 1000000000;
985 lTimeOffset
.tv_sec
= (long)lEventNSec
/ 1000000000;
987 TimeAdd(time
, tf
->a_block_start
->time
, lTimeOffset
);
992 /*****************************************************************************
994 * setFieldsOffset : set offset of the fields
996 * tracefile : opened trace file
997 * evT : the event type
998 * evD : event data, it may be NULL
999 ****************************************************************************/
1001 void setFieldsOffset(LttTracefile
* t
, LttEventType
* evT
, void * evD
)
1003 LttField
* rootFld
= evT
->root_field
;
1004 // rootFld->base_address = evD;
1006 rootFld
->field_size
= getFieldtypeSize(t
, evT
, 0,0,rootFld
, evD
);
1009 /*****************************************************************************
1011 * getFieldtypeSize: get the size of the field type (primitive type)
1013 * tracefile : opened trace file
1015 * offsetRoot : offset from the root
1016 * offsetParent : offset from the parrent
1018 * evD : event data, it may be NULL
1020 * int : size of the field
1021 ****************************************************************************/
1023 int getFieldtypeSize(LttTracefile
* t
, LttEventType
* evT
, int offsetRoot
,
1024 int offsetParent
, LttField
* fld
, void * evD
)
1026 int size
, size1
, element_number
, i
, offset1
, offset2
;
1027 LttType
* type
= fld
->field_type
;
1030 if(evT
->latest_block
==t
->which_block
&& evT
->latest_event
==t
->which_event
){
1031 return fld
->field_size
;
1035 if(fld
->field_fixed
== 1){
1036 if(fld
== evT
->root_field
) return fld
->field_size
;
1039 if(type
->type_class
!= LTT_STRUCT
&& type
->type_class
!= LTT_ARRAY
&&
1040 type
->type_class
!= LTT_SEQUENCE
&& type
->type_class
!= LTT_STRING
){
1041 if(fld
->field_fixed
== -1){
1042 size
= (int) ltt_type_size(t
->trace
, type
);
1043 fld
->field_fixed
= 1;
1044 }else size
= fld
->field_size
;
1046 }else if(type
->type_class
== LTT_ARRAY
){
1047 element_number
= (int) type
->element_number
;
1048 if(fld
->field_fixed
== -1){
1049 size
= getFieldtypeSize(t
, evT
, offsetRoot
,0,fld
->child
[0], NULL
);
1050 if(size
== 0){ //has string or sequence
1051 fld
->field_fixed
= 0;
1053 fld
->field_fixed
= 1;
1054 size
*= element_number
;
1056 }else if(fld
->field_fixed
== 0){// has string or sequence
1058 for(i
=0;i
<element_number
;i
++){
1059 size
+= getFieldtypeSize(t
, evT
, offsetRoot
+size
,size
,
1060 fld
->child
[0], evD
+size
);
1062 }else size
= fld
->field_size
;
1064 }else if(type
->type_class
== LTT_SEQUENCE
){
1065 size1
= (int) ltt_type_size(t
->trace
, type
);
1066 if(fld
->field_fixed
== -1){
1067 fld
->field_fixed
= 0;
1068 size
= getFieldtypeSize(t
, evT
, offsetRoot
,0,fld
->child
[0], NULL
);
1069 fld
->element_size
= size
;
1071 element_number
= getIntNumber(size1
,evD
);
1072 type
->element_number
= element_number
;
1073 if(fld
->element_size
> 0){
1074 size
= element_number
* fld
->element_size
;
1075 }else{//sequence has string or sequence
1077 for(i
=0;i
<element_number
;i
++){
1078 size
+= getFieldtypeSize(t
, evT
, offsetRoot
+size
+size1
,size
+size1
,
1079 fld
->child
[0], evD
+size
+size1
);
1085 }else if(type
->type_class
== LTT_STRING
){
1087 if(fld
->field_fixed
== -1){
1088 fld
->field_fixed
= 0;
1090 size
= sizeof((char*)evD
) + 1; //include end : '\0'
1093 }else if(type
->type_class
== LTT_STRUCT
){
1094 element_number
= (int) type
->element_number
;
1096 if(fld
->field_fixed
== -1){
1097 offset1
= offsetRoot
;
1099 for(i
=0;i
<element_number
;i
++){
1100 size1
=getFieldtypeSize(t
, evT
,offset1
,offset2
, fld
->child
[i
], NULL
);
1101 if(size1
> 0 && size
>= 0){
1103 if(offset1
>= 0) offset1
+= size1
;
1112 fld
->field_fixed
= 0;
1114 }else fld
->field_fixed
= 1;
1115 }else if(fld
->field_fixed
== 0){
1116 offset1
= offsetRoot
;
1118 for(i
=0;i
<element_number
;i
++){
1119 size
=getFieldtypeSize(t
,evT
,offset1
,offset2
,fld
->child
[i
],evD
+offset2
);
1124 }else size
= fld
->field_size
;
1127 fld
->offset_root
= offsetRoot
;
1128 fld
->offset_parent
= offsetParent
;
1130 fld
->fixed_root
= (offsetRoot
==-1) ? 0 : 1;
1131 fld
->fixed_parent
= (offsetParent
==-1) ? 0 : 1;
1133 fld
->field_size
= size
;
1138 /*****************************************************************************
1140 * timecmp : compare two time
1145 * int : 0: t1 == t2; -1: t1 < t2; 1: t1 > t2
1146 ****************************************************************************/
1148 int timecmp(LttTime
* t1
, LttTime
* t2
)
1151 TimeSub(T
, *t1
, *t2
);
1152 if(T
.tv_sec
== 0 && T
.tv_nsec
== 0) return 0;
1153 else if(T
.tv_sec
> 0 || (T
.tv_sec
==0 && T
.tv_nsec
> 0)) return 1;
1157 /*****************************************************************************
1159 * getIntNumber : get an integer number
1161 * size : the size of the integer
1162 * evD : the event data
1165 ****************************************************************************/
1167 int getIntNumber(int size
, void *evD
)
1170 if(size
== 1) i
= *(int8_t *)evD
;
1171 else if(size
== 2) i
= *(int16_t *)evD
;
1172 else if(size
== 4) i
= *(int32_t *)evD
;
1173 else if(size
== 8) i
= *(int64_t *)evD
;
1178 /*****************************************************************************
1180 * getDataEndianType : get the data type size and endian type of the local
1183 * size : size of data type
1184 * endian : endian type, little or big
1185 ****************************************************************************/
1187 void getDataEndianType(LttArchSize
* size
, LttArchEndian
* endian
)
1191 int sizeInt
=sizeof(int), sizeLong
=sizeof(long), sizePointer
=sizeof(void *);
1193 if(c
== 1) *endian
= LTT_LITTLE_ENDIAN
;
1194 else *endian
= LTT_BIG_ENDIAN
;
1196 if(sizeInt
== 2 && sizeLong
== 4 && sizePointer
== 4)
1198 else if(sizeInt
== 4 && sizeLong
== 4 && sizePointer
== 4)
1200 else if(sizeInt
== 4 && sizeLong
== 8 && sizePointer
== 8)
1202 else if(sizeInt
== 8 && sizeLong
== 8 && sizePointer
== 8)
1204 else *size
= LTT_UNKNOWN
;