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_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 ev
= ltt_tracefile_read(tf
);
154 if(!ev
)return; // end of file
156 if(ev
->event_id
== TRACE_FACILITY_LOAD
){
158 fLoad
.name
= (char*)pos
;
159 fLoad
.checksum
= *(LttChecksum
*)(pos
+ sizeof(pos
));
160 fLoad
.base_code
= *(uint32_t*)(pos
+ sizeof(pos
) + sizeof(LttChecksum
));
162 for(i
=0;i
<t
->facility_number
;i
++){
163 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
,i
);
164 if(strcmp(f
->name
,fLoad
.name
)==0 && fLoad
.checksum
==f
->checksum
){
165 f
->base_id
= fLoad
.base_code
;
169 if(i
==t
->facility_number
)
170 g_error("Facility: %s, checksum: %d is not founded\n",
171 fLoad
.name
,fLoad
.checksum
);
172 }else if(ev
->event_id
== TRACE_BLOCK_START
){
174 }else if(ev
->event_id
== TRACE_BLOCK_END
){
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
;
358 dir
= opendir(eventdefs
);
359 if(!dir
) g_error("Can not open directory: %s\n", eventdefs
);
361 while((entry
= readdir(dir
)) != NULL
){
362 ptr
= &entry
->d_name
[strlen(entry
->d_name
)-4];
363 if(strcmp(ptr
,".xml") != 0) continue;
364 ltt_facility_open(t
,entry
->d_name
);
368 for(j
=0;j
<t
->facility_number
;j
++){
369 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, i
);
370 for(i
=0; i
<f
->event_number
; i
++){
372 setFieldsOffset(NULL
, et
, NULL
, t
);
377 void getControlFileInfo(LttTrace
*t
, char* control
)
380 struct dirent
*entry
;
382 dir
= opendir(control
);
383 if(!dir
) g_error("Can not open directory: %s\n", control
);
385 while((entry
= readdir(dir
)) != NULL
){
386 if(strcmp(entry
->d_name
,"facilities") != 0 ||
387 strcmp(entry
->d_name
,"interrupts") != 0 ||
388 strcmp(entry
->d_name
,"processes") != 0) continue;
390 ltt_tracefile_open_control(t
,entry
->d_name
);
395 void getCpuFileInfo(LttTrace
*t
, char* cpu
)
398 struct dirent
*entry
;
401 if(!dir
) g_error("Can not open directory: %s\n", cpu
);
403 while((entry
= readdir(dir
)) != NULL
){
404 if(strcmp(entry
->d_name
,".") != 0 ||
405 strcmp(entry
->d_name
,"..") != 0 ){
406 ltt_tracefile_open_cpu(t
,entry
->d_name
);
412 /*****************************************************************************
413 *A trace is specified as a pathname to the directory containing all the
414 *associated data (control tracefiles, per cpu tracefiles, event
417 *When a trace is closed, all the associated facilities, types and fields
418 *are released as well.
419 ****************************************************************************/
421 LttTrace
*ltt_trace_open(char *pathname
)
424 LttSystemDescription
* sys_description
;
425 char eventdefs
[DIR_NAME_SIZE
];
426 char info
[DIR_NAME_SIZE
];
427 char control
[DIR_NAME_SIZE
];
428 char cpu
[DIR_NAME_SIZE
];
429 char tmp
[DIR_NAME_SIZE
];
430 gboolean has_slash
= FALSE
;
432 //establish the pathname to different directories
433 if(pathname
[strlen(pathname
)-1] == '/')has_slash
= TRUE
;
434 strcpy(eventdefs
,pathname
);
435 if(!has_slash
)strcat(eventdefs
,"/");
436 strcat(eventdefs
,"eventdefs/");
438 strcpy(info
,pathname
);
439 if(!has_slash
)strcat(info
,"/");
440 strcat(info
,"info/");
442 strcpy(control
,pathname
);
443 if(!has_slash
)strcat(control
,"/");
444 strcat(control
,"control/");
446 strcpy(cpu
,pathname
);
447 if(!has_slash
)strcat(cpu
,"/");
451 t
= g_new(LttTrace
, 1);
452 sys_description
= g_new(LttSystemDescription
, 1);
453 t
->pathname
= g_strdup(pathname
);
454 t
->facility_number
= 0;
455 t
->control_tracefile_number
= 0;
456 t
->per_cpu_tracefile_number
= 0;
457 t
->system_description
= sys_description
;
458 t
->control_tracefiles
= g_ptr_array_new();
459 t
->per_cpu_tracefiles
= g_ptr_array_new();
460 t
->facilities
= g_ptr_array_new();
461 getDataEndianType(&(t
->my_arch_size
), &(t
->my_arch_endian
));
463 //get system description
465 strcat(tmp
,"system.xml");
466 getSystemInfo(sys_description
, tmp
);
468 //get control tracefile info
469 getControlFileInfo(t
,control
);
471 //get cpu tracefile info
472 getCpuFileInfo(t
,cpu
);
474 //get facilities info
475 getFacilityInfo(t
,eventdefs
);
480 void ltt_trace_close(LttTrace
*t
)
488 //free system_description
489 g_free(t
->system_description
->description
);
490 g_free(t
->system_description
->node_name
);
491 g_free(t
->system_description
->domain_name
);
492 g_free(t
->system_description
->kernel_name
);
493 g_free(t
->system_description
->kernel_release
);
494 g_free(t
->system_description
->kernel_version
);
495 g_free(t
->system_description
->machine
);
496 g_free(t
->system_description
->processor
);
497 g_free(t
->system_description
->hardware_platform
);
498 g_free(t
->system_description
->operating_system
);
499 g_free(t
->system_description
);
501 //free control_tracefiles
502 for(i
=0;i
<t
->control_tracefile_number
;i
++){
503 tf
= (LttTracefile
*)g_ptr_array_index(t
->control_tracefiles
,i
);
504 ltt_tracefile_close(tf
);
506 g_ptr_array_free(t
->control_tracefiles
, FALSE
);
508 //free per_cpu_tracefiles
509 for(i
=0;i
<t
->per_cpu_tracefile_number
;i
++){
510 tf
= (LttTracefile
*)g_ptr_array_index(t
->per_cpu_tracefiles
,i
);
511 ltt_tracefile_close(tf
);
513 g_ptr_array_free(t
->per_cpu_tracefiles
, FALSE
);
516 for(i
=0;i
<t
->facility_number
;i
++){
517 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
,i
);
518 ltt_facility_close(f
);
520 g_ptr_array_free(t
->facilities
, FALSE
);
526 /*****************************************************************************
527 *Get the system description of the trace
528 ****************************************************************************/
530 LttSystemDescription
*ltt_trace_system_description(LttTrace
*t
)
532 return t
->system_description
;
535 /*****************************************************************************
536 * The following functions discover the facilities of the trace
537 ****************************************************************************/
539 unsigned ltt_trace_facility_number(LttTrace
*t
)
541 return (unsigned)(t
->facility_number
);
544 LttFacility
*ltt_trace_facility_get(LttTrace
*t
, unsigned i
)
546 return (LttFacility
*)g_ptr_array_index(t
->facilities
, i
);
549 /*****************************************************************************
551 * ltt_trace_facility_find : find facilities in the trace
554 * name : facility name
556 * position : position of the facility in the trace
558 * : the number of facilities
559 ****************************************************************************/
561 unsigned ltt_trace_facility_find(LttTrace
*t
, char *name
, unsigned *position
)
565 for(i
=0;i
=t
->facility_number
;i
++){
566 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, i
);
567 if(strcmp(f
->name
,name
)==0){
569 if(count
==1) *position
= i
;
577 /*****************************************************************************
578 * Functions to discover all the event types in the trace
579 ****************************************************************************/
581 unsigned ltt_trace_eventtype_number(LttTrace
*t
)
586 for(i
=0;i
=t
->facility_number
;i
++){
587 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, i
);
588 count
+= f
->event_number
;
593 LttFacility
* ltt_trace_facility_by_id(LttTrace
* trace
, unsigned id
)
595 LttFacility
* facility
;
597 for(i
=0;i
<trace
->facility_number
;i
++){
598 facility
= (LttFacility
*) g_ptr_array_index(trace
->facilities
,i
);
599 if(id
>= facility
->base_id
&&
600 id
< facility
->base_id
+ facility
->event_number
)
603 if(i
==trace
->facility_number
) return NULL
;
604 else return facility
;
607 LttEventType
*ltt_trace_eventtype_get(LttTrace
*t
, unsigned evId
)
610 f
= ltt_trace_facility_by_id(t
,evId
);
612 return f
->events
[evId
- f
->base_id
];
615 /*****************************************************************************
616 *There is one "per cpu" tracefile for each CPU, numbered from 0 to
617 *the maximum number of CPU in the system. When the number of CPU installed
618 *is less than the maximum, some positions are unused. There are also a
619 *number of "control" tracefiles (facilities, interrupts...).
620 ****************************************************************************/
621 unsigned ltt_trace_control_tracefile_number(LttTrace
*t
)
623 return t
->control_tracefile_number
;
626 unsigned ltt_trace_per_cpu_tracefile_number(LttTrace
*t
)
628 return t
->per_cpu_tracefile_number
;
631 /*****************************************************************************
632 *It is possible to search for the tracefiles by name or by CPU position.
633 *The index within the tracefiles of the same type is returned if found
634 *and a negative value otherwise.
635 ****************************************************************************/
637 int ltt_trace_control_tracefile_find(LttTrace
*t
, char *name
)
639 LttTracefile
* tracefile
;
641 for(i
=0;i
<t
->control_tracefile_number
;i
++){
642 tracefile
= (LttTracefile
*)g_ptr_array_index(t
->control_tracefiles
, i
);
643 if(strcmp(tracefile
->name
, name
)==0)break;
645 if(i
== t
->control_tracefile_number
) return -1;
649 int ltt_trace_per_cpu_tracefile_find(LttTrace
*t
, unsigned i
)
651 LttTracefile
* tracefile
;
653 for(j
=0;j
<t
->per_cpu_tracefile_number
;j
++){
654 tracefile
= (LttTracefile
*)g_ptr_array_index(t
->per_cpu_tracefiles
, j
);
655 name
= atoi(tracefile
->name
);
656 if(name
== (int)i
)break;
658 if(j
== t
->per_cpu_tracefile_number
) return -1;
662 /*****************************************************************************
663 *Get a specific tracefile
664 ****************************************************************************/
666 LttTracefile
*ltt_trace_control_tracefile_get(LttTrace
*t
, unsigned i
)
668 return (LttTracefile
*)g_ptr_array_index(t
->per_cpu_tracefiles
, i
);
671 LttTracefile
*ltt_trace_per_cpu_tracefile_get(LttTrace
*t
, unsigned i
)
673 return (LttTracefile
*)g_ptr_array_index(t
->per_cpu_tracefiles
, i
);
676 /*****************************************************************************
677 *Get the name of a tracefile
678 ****************************************************************************/
680 char *ltt_tracefile_name(LttTracefile
*tf
)
685 /*****************************************************************************
687 * ltt_tracefile_seek_time: seek to the first event of the trace with time
688 * larger or equal to time
691 * time : criteria of the time
692 ****************************************************************************/
694 void ltt_tracefile_seek_time(LttTracefile
*t
, LttTime time
)
698 int headTime
= timecmp(&(t
->a_block_start
->time
), &time
);
699 int tailTime
= timecmp(&(t
->a_block_end
->time
), &time
);
701 if(headTime
< 0 && tailTime
> 0){
702 lttTime
= getEventTime(t
);
703 err
= timecmp(<tTime
, &time
);
705 if(t
->which_event
==2 || timecmp(&t
->prev_event_time
,&time
)<0){
709 return ltt_tracefile_seek_time(t
, time
);
712 err
= t
->which_block
;
713 if(ltt_tracefile_read(t
) == NULL
){
714 g_printf("End of file\n");
717 if(t
->which_block
== err
)
718 return ltt_tracefile_seek_time(t
,time
);
720 }else if(headTime
> 0){
721 if(t
->which_block
== 1){
724 if(timecmp(&(t
->prev_block_end_time
),&time
) >= 0 ){
725 err
=readBlock(t
,t
->which_block
-1);
726 if(err
) g_error("Can not read tracefile: %s\n", t
->name
);
727 return ltt_tracefile_seek_time(t
, time
) ;
732 }else if(tailTime
< 0){
733 if(t
->which_block
!= t
->block_number
){
734 err
=readBlock(t
,t
->which_block
+1);
735 if(err
) g_error("Can not read tracefile: %s\n", t
->name
);
737 g_printf("End of file\n");
740 if(tailTime
< 0) return ltt_tracefile_seek_time(t
, time
);
741 }else if(headTime
== 0){
743 }else if(tailTime
== 0){
744 t
->cur_event_pos
= t
->a_block_end
- EVENT_HEADER_SIZE
;
749 /*****************************************************************************
751 * ltt_tracefile_read : read the current event, set the pointer to the next
755 * LttEvent * : an event to be processed
756 ****************************************************************************/
758 LttEvent
*ltt_tracefile_read(LttTracefile
*t
)
760 LttEvent
* lttEvent
= (LttEvent
*)g_new(LttEvent
, 1);
763 lttEvent
->event_id
= (int)(*(uint16_t *)(t
->cur_event_pos
));
764 if(lttEvent
->event_id
== TRACE_TIME_HEARTBEAT
)
765 t
->cur_heart_beat_number
++;
767 t
->prev_event_time
= t
->current_event_time
;
768 t
->current_event_time
= getEventTime(t
);
770 lttEvent
->time_delta
= *(uint32_t*)(t
->cur_event_pos
+ EVENT_ID_SIZE
);
771 lttEvent
->event_time
= t
->current_event_time
;
773 lttEvent
->event_cycle_count
= ((uint64_t)1)<<32 * t
->cur_heart_beat_number
774 + lttEvent
->time_delta
;
776 lttEvent
->tracefile
= t
;
777 lttEvent
->data
= t
->cur_event_pos
+ EVENT_HEADER_SIZE
;
779 //update the fields of the current event and go to the next event
781 if(err
== ENOENT
) return NULL
;
782 if(err
== ERANGE
) g_error("event id is out of range\n");
783 if(err
)g_error("Can not read tracefile\n");
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
;
831 tf
->prev_event_time
= tf
->a_block_end
->time
;
832 tf
->current_event_time
= tf
->a_block_end
->time
;
834 tf
->prev_block_end_time
.tv_sec
= 0;
835 tf
->prev_block_end_time
.tv_nsec
= 0;
836 tf
->prev_event_time
.tv_sec
= 0;
837 tf
->prev_event_time
.tv_nsec
= 0;
838 tf
->current_event_time
.tv_sec
= 0;
839 tf
->current_event_time
.tv_nsec
= 0;
842 nbBytes
=lseek(tf
->fd
,(off_t
)((whichBlock
-1)*tf
->block_size
), SEEK_SET
);
843 if(nbBytes
== -1) return EINVAL
;
845 if(readFile(tf
->fd
,tf
->buffer
,tf
->block_size
,"Unable to read a block"))
848 tf
->a_block_start
=(BlockStart
*) (tf
->buffer
+ EVENT_HEADER_SIZE
);
849 lostSize
= *(uint32_t*)(tf
->buffer
+ tf
->block_size
- sizeof(uint32_t));
850 tf
->a_block_end
=(BlockEnd
*)(tf
->buffer
+ tf
->block_size
-
851 lostSize
+ EVENT_HEADER_SIZE
);
853 tf
->which_block
= whichBlock
;
855 tf
->cur_event_pos
= tf
->buffer
;//the beginning of the block, block start ev
856 tf
->cur_heart_beat_number
= 0;
860 // tf->current_event_time = getEventTime(tf);
865 /*****************************************************************************
867 * updateTracefile : reinitialize the info of the block which is already
871 ****************************************************************************/
873 void updateTracefile(LttTracefile
* tf
)
876 tf
->cur_event_pos
= tf
->buffer
;
877 // tf->current_event_time = getEventTime(tf);
878 tf
->current_event_time
.tv_sec
= 0;
879 tf
->current_event_time
.tv_nsec
= 0;
880 tf
->cur_heart_beat_number
= 0;
882 tf
->prev_event_time
.tv_sec
= 0;
883 tf
->prev_event_time
.tv_nsec
= 0;
886 /*****************************************************************************
888 * skipEvent : go to the next event, update the fields of the current event
893 * EINVAL : lseek fail
894 * EIO : can not read from the file
895 * ENOENT : end of file
896 * ERANGE : event id is out of range
897 ****************************************************************************/
899 int skipEvent(LttTracefile
* t
)
906 evId
= (int)(*(uint16_t *)(t
->cur_event_pos
));
907 evData
= t
->cur_event_pos
+ EVENT_HEADER_SIZE
;
908 evT
= ltt_trace_eventtype_get(t
->trace
,(unsigned)evId
);
910 if(evT
) rootFld
= evT
->root_field
;
913 //event has string/sequence or the last event is not the same event
914 if((evT
->latest_block
!=t
->which_block
|| evT
->latest_event
!=t
->which_event
)
915 && rootFld
->field_fixed
== 0){
916 setFieldsOffset(t
, evT
, evData
, t
->trace
);
918 t
->cur_event_pos
+= EVENT_HEADER_SIZE
+ rootFld
->field_size
;
920 evT
->latest_block
= t
->which_block
;
921 evT
->latest_event
= t
->which_event
;
923 //the next event is in the next block
924 if(evId
== TRACE_BLOCK_END
){
925 if(t
->which_block
== t
->block_number
) return ENOENT
;
926 err
= readBlock(t
, t
->which_block
+ 1);
935 /*****************************************************************************
937 * getCyclePerNsec : calculate cycles per nsec for current block
940 ****************************************************************************/
942 void getCyclePerNsec(LttTracefile
* t
)
944 LttTime lBufTotalTime
; /* Total time for this buffer */
945 LttCycleCount lBufTotalNSec
; /* Total time for this buffer in nsecs */
946 LttCycleCount lBufTotalCycle
;/* Total cycles for this buffer */
948 /* Calculate the total time for this buffer */
949 TimeSub(lBufTotalTime
,t
->a_block_end
->time
, t
->a_block_start
->time
);
951 /* Calculate the total cycles for this bufffer */
952 lBufTotalCycle
= t
->a_block_end
->cycle_count
953 - t
->a_block_start
->cycle_count
;
955 /* Convert the total time to nsecs */
956 lBufTotalNSec
= lBufTotalTime
.tv_sec
* 1000000000 + lBufTotalTime
.tv_nsec
;
958 t
->cycle_per_nsec
= (double)lBufTotalCycle
/ (double)lBufTotalNSec
;
961 /****************************************************************************
963 * getEventTime : obtain the time of an event
967 * LttTime : the time of the event
968 ****************************************************************************/
970 LttTime
getEventTime(LttTracefile
* tf
)
973 LttCycleCount cycle_count
; // cycle count for the current event
974 LttCycleCount lEventTotalCycle
; // Total cycles from start for event
975 double lEventNSec
; // Total usecs from start for event
976 LttTime lTimeOffset
; // Time offset in struct LttTime
979 evId
= *(uint16_t*)tf
->cur_event_pos
;
980 if(evId
== TRACE_BLOCK_START
)
981 return tf
->a_block_start
->time
;
982 else if(evId
== TRACE_BLOCK_END
)
983 return tf
->a_block_end
->time
;
986 // Calculate total time in cycles from start of buffer for this event
987 cycle_count
= (LttCycleCount
)*(uint32_t*)(tf
->cur_event_pos
+ EVENT_ID_SIZE
);
988 if(tf
->cur_heart_beat_number
)
989 cycle_count
+= ((uint64_t)1)<<32 * tf
->cur_heart_beat_number
;
990 lEventTotalCycle
= cycle_count
- tf
->a_block_start
->cycle_count
;
992 // Convert it to nsecs
993 lEventNSec
= lEventTotalCycle
/ tf
->cycle_per_nsec
;
995 // Determine offset in struct LttTime
996 lTimeOffset
.tv_nsec
= (long)lEventNSec
% 1000000000;
997 lTimeOffset
.tv_sec
= (long)lEventNSec
/ 1000000000;
999 TimeAdd(time
, tf
->a_block_start
->time
, lTimeOffset
);
1004 /*****************************************************************************
1006 * setFieldsOffset : set offset of the fields
1008 * tracefile : opened trace file
1009 * evT : the event type
1010 * evD : event data, it may be NULL
1011 ****************************************************************************/
1013 void setFieldsOffset(LttTracefile
*tf
,LttEventType
*evT
,void *evD
,LttTrace
* t
)
1015 LttField
* rootFld
= evT
->root_field
;
1016 // rootFld->base_address = evD;
1018 rootFld
->field_size
= getFieldtypeSize(tf
, evT
, 0,0,rootFld
, evD
,t
);
1021 /*****************************************************************************
1023 * getFieldtypeSize: get the size of the field type (primitive type)
1025 * tracefile : opened trace file
1027 * offsetRoot : offset from the root
1028 * offsetParent : offset from the parrent
1030 * evD : event data, it may be NULL
1032 * int : size of the field
1033 ****************************************************************************/
1035 int getFieldtypeSize(LttTracefile
* t
, LttEventType
* evT
, int offsetRoot
,
1036 int offsetParent
, LttField
* fld
, void *evD
, LttTrace
*trace
)
1038 int size
, size1
, element_number
, i
, offset1
, offset2
;
1039 LttType
* type
= fld
->field_type
;
1042 if(evT
->latest_block
==t
->which_block
&& evT
->latest_event
==t
->which_event
){
1043 return fld
->field_size
;
1047 if(fld
->field_fixed
== 1){
1048 if(fld
== evT
->root_field
) return fld
->field_size
;
1051 if(type
->type_class
!= LTT_STRUCT
&& type
->type_class
!= LTT_ARRAY
&&
1052 type
->type_class
!= LTT_SEQUENCE
&& type
->type_class
!= LTT_STRING
){
1053 if(fld
->field_fixed
== -1){
1054 size
= (int) ltt_type_size(trace
, type
);
1055 fld
->field_fixed
= 1;
1056 }else size
= fld
->field_size
;
1058 }else if(type
->type_class
== LTT_ARRAY
){
1059 element_number
= (int) type
->element_number
;
1060 if(fld
->field_fixed
== -1){
1061 size
= getFieldtypeSize(t
, evT
, offsetRoot
,0,fld
->child
[0], NULL
, trace
);
1062 if(size
== 0){ //has string or sequence
1063 fld
->field_fixed
= 0;
1065 fld
->field_fixed
= 1;
1066 size
*= element_number
;
1068 }else if(fld
->field_fixed
== 0){// has string or sequence
1070 for(i
=0;i
<element_number
;i
++){
1071 size
+= getFieldtypeSize(t
, evT
, offsetRoot
+size
,size
,
1072 fld
->child
[0], evD
+size
, trace
);
1074 }else size
= fld
->field_size
;
1076 }else if(type
->type_class
== LTT_SEQUENCE
){
1077 size1
= (int) ltt_type_size(trace
, type
);
1078 if(fld
->field_fixed
== -1){
1079 fld
->field_fixed
= 0;
1080 size
= getFieldtypeSize(t
, evT
, offsetRoot
,0,fld
->child
[0], NULL
, trace
);
1081 fld
->element_size
= size
;
1083 element_number
= getIntNumber(size1
,evD
);
1084 type
->element_number
= element_number
;
1085 if(fld
->element_size
> 0){
1086 size
= element_number
* fld
->element_size
;
1087 }else{//sequence has string or sequence
1089 for(i
=0;i
<element_number
;i
++){
1090 size
+= getFieldtypeSize(t
, evT
, offsetRoot
+size
+size1
,size
+size1
,
1091 fld
->child
[0], evD
+size
+size1
, trace
);
1097 }else if(type
->type_class
== LTT_STRING
){
1099 if(fld
->field_fixed
== -1){
1100 fld
->field_fixed
= 0;
1102 size
= sizeof((char*)evD
) + 1; //include end : '\0'
1105 }else if(type
->type_class
== LTT_STRUCT
){
1106 element_number
= (int) type
->element_number
;
1108 if(fld
->field_fixed
== -1){
1109 offset1
= offsetRoot
;
1111 for(i
=0;i
<element_number
;i
++){
1112 size1
=getFieldtypeSize(t
, evT
,offset1
,offset2
, fld
->child
[i
], NULL
, trace
);
1113 if(size1
> 0 && size
>= 0){
1115 if(offset1
>= 0) offset1
+= size1
;
1124 fld
->field_fixed
= 0;
1126 }else fld
->field_fixed
= 1;
1127 }else if(fld
->field_fixed
== 0){
1128 offset1
= offsetRoot
;
1130 for(i
=0;i
<element_number
;i
++){
1131 size
=getFieldtypeSize(t
,evT
,offset1
,offset2
,fld
->child
[i
],evD
+offset2
, trace
);
1136 }else size
= fld
->field_size
;
1139 fld
->offset_root
= offsetRoot
;
1140 fld
->offset_parent
= offsetParent
;
1142 fld
->fixed_root
= (offsetRoot
==-1) ? 0 : 1;
1143 fld
->fixed_parent
= (offsetParent
==-1) ? 0 : 1;
1145 fld
->field_size
= size
;
1150 /*****************************************************************************
1152 * timecmp : compare two time
1157 * int : 0: t1 == t2; -1: t1 < t2; 1: t1 > t2
1158 ****************************************************************************/
1160 int timecmp(LttTime
* t1
, LttTime
* t2
)
1163 TimeSub(T
, *t1
, *t2
);
1164 if(T
.tv_sec
== 0 && T
.tv_nsec
== 0) return 0;
1165 else if(T
.tv_sec
> 0 || (T
.tv_sec
==0 && T
.tv_nsec
> 0)) return 1;
1169 /*****************************************************************************
1171 * getIntNumber : get an integer number
1173 * size : the size of the integer
1174 * evD : the event data
1177 ****************************************************************************/
1179 int getIntNumber(int size
, void *evD
)
1182 if(size
== 1) i
= *(int8_t *)evD
;
1183 else if(size
== 2) i
= *(int16_t *)evD
;
1184 else if(size
== 4) i
= *(int32_t *)evD
;
1185 else if(size
== 8) i
= *(int64_t *)evD
;
1190 /*****************************************************************************
1192 * getDataEndianType : get the data type size and endian type of the local
1195 * size : size of data type
1196 * endian : endian type, little or big
1197 ****************************************************************************/
1199 void getDataEndianType(LttArchSize
* size
, LttArchEndian
* endian
)
1203 int sizeInt
=sizeof(int), sizeLong
=sizeof(long), sizePointer
=sizeof(void *);
1205 if(c
== 1) *endian
= LTT_LITTLE_ENDIAN
;
1206 else *endian
= LTT_BIG_ENDIAN
;
1208 if(sizeInt
== 2 && sizeLong
== 4 && sizePointer
== 4)
1210 else if(sizeInt
== 4 && sizeLong
== 4 && sizePointer
== 4)
1212 else if(sizeInt
== 4 && sizeLong
== 8 && sizePointer
== 8)
1214 else if(sizeInt
== 8 && sizeLong
== 8 && sizePointer
== 8)
1216 else *size
= LTT_UNKNOWN
;