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
);
37 /*****************************************************************************
39 * ltt_tracefile_open : open a trace file, construct a LttTracefile
41 * t : the trace containing the tracefile
42 * fileName : path name of the trace file
44 * : a pointer to a tracefile
45 ****************************************************************************/
47 LttTracefile
* ltt_tracefile_open(LttTrace
* t
, char * fileName
)
50 struct stat lTDFStat
; /* Trace data file status */
51 BlockStart a_block_start
;
53 tf
= g_new(LttTracefile
, 1);
56 tf
->name
= g_strdup(fileName
);
58 tf
->fd
= open(fileName
, O_RDONLY
, 0);
60 g_error("Unable to open input data file %s\n", fileName
);
63 // Get the file's status
64 if(fstat(tf
->fd
, &lTDFStat
) < 0){
65 g_error("Unable to get the status of the input data file %s\n", fileName
);
68 // Is the file large enough to contain a trace
69 if(lTDFStat
.st_size
< sizeof(BlockStart
) + EVENT_HEADER_SIZE
){
70 g_print("The input data file %s does not contain a trace\n", fileName
);
77 //store the size of the file
78 tf
->file_size
= lTDFStat
.st_size
;
79 tf
->block_size
= t
->system_description
->ltt_block_size
;
80 tf
->block_number
= tf
->file_size
/ tf
->block_size
;
83 //allocate memory to contain the info of a block
84 tf
->buffer
= (void *) g_new(char, t
->system_description
->ltt_block_size
);
86 //read the first block
87 if(readBlock(tf
,1)) exit(1);
93 /*****************************************************************************
94 *Open control and per cpu tracefiles
95 ****************************************************************************/
97 void ltt_tracefile_open_cpu(LttTrace
*t
, char * tracefile_name
)
100 tf
= ltt_tracefile_open(t
,tracefile_name
);
102 t
->per_cpu_tracefile_number
++;
103 g_ptr_array_add(t
->per_cpu_tracefiles
, tf
);
106 void ltt_tracefile_open_control(LttTrace
*t
, char * control_name
)
116 tf
= ltt_tracefile_open(t
,control_name
);
118 t
->control_tracefile_number
++;
119 g_ptr_array_add(t
->control_tracefiles
,tf
);
121 //parse facilities tracefile to get base_id
122 if(strcmp(&control_name
[strlen(control_name
)-10],"facilities") ==0){
124 ev
= ltt_tracefile_read(tf
);
125 if(!ev
)return; // end of file
127 if(ev
->event_id
== TRACE_FACILITY_LOAD
){
129 fLoad
.name
= (char*)pos
;
130 fLoad
.checksum
= *(LttChecksum
*)(pos
+ strlen(fLoad
.name
));
131 fLoad
.base_code
= *(uint32_t*)(pos
+ strlen(fLoad
.name
) + sizeof(LttChecksum
));
133 for(i
=0;i
<t
->facility_number
;i
++){
134 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
,i
);
135 if(strcmp(f
->name
,fLoad
.name
)==0 && fLoad
.checksum
==f
->checksum
){
136 f
->base_id
= fLoad
.base_code
;
140 if(i
==t
->facility_number
)
141 g_error("Facility: %s, checksum: %d is not founded\n",
142 fLoad
.name
,fLoad
.checksum
);
143 }else if(ev
->event_id
== TRACE_BLOCK_START
){
145 }else if(ev
->event_id
== TRACE_BLOCK_END
){
147 }else g_error("Not valid facilities trace file\n");
152 /*****************************************************************************
154 * ltt_tracefile_close: close a trace file,
156 * t : tracefile which will be closed
157 ****************************************************************************/
159 void ltt_tracefile_close(LttTracefile
*t
)
168 /*****************************************************************************
169 *Get system information
170 ****************************************************************************/
171 void getSystemInfo(LttSystemDescription
* des
, char * pathname
)
175 int entry_number
= 15;
176 char buf
[DIR_NAME_SIZE
];
177 char description
[4*DIR_NAME_SIZE
];
180 fp
= fopen(pathname
,"r");
182 g_error("Can not open file : %s\n", pathname
);
185 while(fgets(buf
,DIR_NAME_SIZE
, fp
)!= NULL
){
187 while(isspace(*ptr
)) ptr
++;
188 if(strlen(ptr
) == 0) continue;
192 if(strlen(ptr
) == 0) g_error("Not a valid file: %s\n", pathname
);
193 if(strncmp("<system",ptr
,7) !=0)g_error("Not a valid file: %s\n", pathname
);
195 for(i
=0;i
<entry_number
;i
++){
196 if(fgets(buf
,DIR_NAME_SIZE
, fp
)== NULL
)
197 g_error("Not a valid file: %s\n", pathname
);
198 buf
[strlen(buf
)-1] = '\0';
200 while(isspace(*ptr
)) ptr
++;
203 if(strncmp("node_name=",ptr
,10)!=0)
204 g_error("Not a valid file: %s\n", pathname
);
205 des
->node_name
= g_strdup(ptr
+10);
208 if(strncmp("domainname=",ptr
,11)!=0)
209 g_error("Not a valid file: %s\n", pathname
);
210 des
->domain_name
= g_strdup(ptr
+11);
213 if(strncmp("cpu=",ptr
,4)!=0)
214 g_error("Not a valid file: %s\n", pathname
);
215 des
->nb_cpu
= (unsigned)atoi(ptr
+4);
218 if(strncmp("arch_size=",ptr
,10)!=0)
219 g_error("Not a valid file: %s\n", pathname
);
220 if(strcmp(ptr
+10,"\"LP32\"") == 0) des
->size
= LTT_LP32
;
221 else if(strcmp(ptr
+10,"\"ILP32\"") == 0) des
->size
= LTT_ILP32
;
222 else if(strcmp(ptr
+10,"\"LP64\"") == 0) des
->size
= LTT_LP64
;
223 else if(strcmp(ptr
+10,"\"ILP64\"") == 0) des
->size
= LTT_ILP64
;
224 else if(strcmp(ptr
+10,"\"UNKNOWN\"") == 0) des
->size
= LTT_UNKNOWN
;
227 if(strncmp("endian=",ptr
,7)!=0)
228 g_error("Not a valid file: %s\n", pathname
);
229 if(strcmp(ptr
+7,"\"LITTLE_ENDIAN\"") == 0)
230 des
->endian
= LTT_LITTLE_ENDIAN
;
231 else if(strcmp(ptr
+7,"\"BIG_ENDIAN\"") == 0)
232 des
->endian
= LTT_BIG_ENDIAN
;
235 if(strncmp("kernel_name=",ptr
,12)!=0)
236 g_error("Not a valid file: %s\n", pathname
);
237 des
->kernel_name
= g_strdup(ptr
+12);
240 if(strncmp("kernel_release=",ptr
,15)!=0)
241 g_error("Not a valid file: %s\n", pathname
);
242 des
->kernel_release
= g_strdup(ptr
+15);
245 if(strncmp("kernel_version=",ptr
,15)!=0)
246 g_error("Not a valid file: %s\n", pathname
);
247 des
->kernel_version
= g_strdup(ptr
+15);
250 if(strncmp("machine=",ptr
,8)!=0)
251 g_error("Not a valid file: %s\n", pathname
);
252 des
->machine
= g_strdup(ptr
+8);
255 if(strncmp("processor=",ptr
,10)!=0)
256 g_error("Not a valid file: %s\n", pathname
);
257 des
->processor
= g_strdup(ptr
+10);
260 if(strncmp("hardware_platform=",ptr
,18)!=0)
261 g_error("Not a valid file: %s\n", pathname
);
262 des
->hardware_platform
= g_strdup(ptr
+18);
265 if(strncmp("operating_system=",ptr
,17)!=0)
266 g_error("Not a valid file: %s\n", pathname
);
267 des
->operating_system
= g_strdup(ptr
+17);
270 if(strncmp("ltt_major_version=",ptr
,18)!=0)
271 g_error("Not a valid file: %s\n", pathname
);
273 // ptr++;//skip begining "
274 // ptr[strlen(ptr)-1] = '\0'; //get rid of the ending "
275 des
->ltt_major_version
= (unsigned)atoi(ptr
);
278 if(strncmp("ltt_minor_version=",ptr
,18)!=0)
279 g_error("Not a valid file: %s\n", pathname
);
281 // ptr++;//skip begining "
282 // ptr[strlen(ptr)-1] = '\0'; //get rid of the ending "
283 des
->ltt_minor_version
= (unsigned)atoi(ptr
);
286 if(strncmp("ltt_block_size=",ptr
,15)!=0)
287 g_error("Not a valid file: %s\n", pathname
);
289 // ptr++;//skip begining "
290 // ptr[strlen(ptr)-1] = '\0'; //get rid of the ending "
291 des
->ltt_block_size
= (unsigned)atoi(ptr
);
294 g_error("Not a valid file: %s\n", pathname
);
299 description
[0] = '\0';
300 if(fgets(buf
,DIR_NAME_SIZE
, fp
)== NULL
)
301 g_error("Not a valid file: %s\n", pathname
);
303 while(isspace(*ptr
)) ptr
++;
304 if(*ptr
!= '>') g_error("Not a valid file: %s\n", pathname
);
305 while((ptr
=fgets(buf
,DIR_NAME_SIZE
, fp
))!= NULL
){
307 while(isspace(*ptr
)) ptr
++;
308 if(strncmp("</system>",ptr
,9) == 0 )break;
309 strcat(description
, buf
);
311 if(!ptr
)g_error("Not a valid file: %s\n", pathname
);
312 if(description
[0] = '\0')des
->description
= NULL
;
313 des
->description
= g_strdup(description
);
318 /*****************************************************************************
319 *The following functions get facility/tracefile information
320 ****************************************************************************/
322 void getFacilityInfo(LttTrace
*t
, char* eventdefs
)
325 struct dirent
*entry
;
330 char name
[DIR_NAME_SIZE
];
332 dir
= opendir(eventdefs
);
333 if(!dir
) g_error("Can not open directory: %s\n", eventdefs
);
335 while((entry
= readdir(dir
)) != NULL
){
336 ptr
= &entry
->d_name
[strlen(entry
->d_name
)-4];
337 if(strcmp(ptr
,".xml") != 0) continue;
338 strcpy(name
,eventdefs
);
339 strcat(name
,entry
->d_name
);
340 ltt_facility_open(t
,name
);
344 for(j
=0;j
<t
->facility_number
;j
++){
345 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, j
);
346 for(i
=0; i
<f
->event_number
; i
++){
348 setFieldsOffset(NULL
, et
, NULL
, t
);
353 void getControlFileInfo(LttTrace
*t
, char* control
)
356 struct dirent
*entry
;
357 char name
[DIR_NAME_SIZE
];
359 dir
= opendir(control
);
360 if(!dir
) g_error("Can not open directory: %s\n", control
);
362 while((entry
= readdir(dir
)) != NULL
){
363 if(strcmp(entry
->d_name
,"facilities") != 0 &&
364 strcmp(entry
->d_name
,"interrupts") != 0 &&
365 strcmp(entry
->d_name
,"processes") != 0) continue;
367 strcpy(name
,control
);
368 strcat(name
,entry
->d_name
);
369 ltt_tracefile_open_control(t
,name
);
374 void getCpuFileInfo(LttTrace
*t
, char* cpu
)
377 struct dirent
*entry
;
378 char name
[DIR_NAME_SIZE
];
381 if(!dir
) g_error("Can not open directory: %s\n", cpu
);
383 while((entry
= readdir(dir
)) != NULL
){
384 if(strcmp(entry
->d_name
,".") != 0 &&
385 strcmp(entry
->d_name
,"..") != 0 ){
387 strcat(name
,entry
->d_name
);
388 ltt_tracefile_open_cpu(t
,name
);
394 /*****************************************************************************
395 *A trace is specified as a pathname to the directory containing all the
396 *associated data (control tracefiles, per cpu tracefiles, event
399 *When a trace is closed, all the associated facilities, types and fields
400 *are released as well.
401 ****************************************************************************/
403 LttTrace
*ltt_trace_open(const char *pathname
)
406 LttSystemDescription
* sys_description
;
407 char eventdefs
[DIR_NAME_SIZE
];
408 char info
[DIR_NAME_SIZE
];
409 char control
[DIR_NAME_SIZE
];
410 char cpu
[DIR_NAME_SIZE
];
411 char tmp
[DIR_NAME_SIZE
];
412 gboolean has_slash
= FALSE
;
414 //establish the pathname to different directories
415 if(pathname
[strlen(pathname
)-1] == '/')has_slash
= TRUE
;
416 strcpy(eventdefs
,pathname
);
417 if(!has_slash
)strcat(eventdefs
,"/");
418 strcat(eventdefs
,"eventdefs/");
420 strcpy(info
,pathname
);
421 if(!has_slash
)strcat(info
,"/");
422 strcat(info
,"info/");
424 strcpy(control
,pathname
);
425 if(!has_slash
)strcat(control
,"/");
426 strcat(control
,"control/");
428 strcpy(cpu
,pathname
);
429 if(!has_slash
)strcat(cpu
,"/");
433 t
= g_new(LttTrace
, 1);
434 sys_description
= g_new(LttSystemDescription
, 1);
435 t
->pathname
= g_strdup(pathname
);
436 t
->facility_number
= 0;
437 t
->control_tracefile_number
= 0;
438 t
->per_cpu_tracefile_number
= 0;
439 t
->system_description
= sys_description
;
440 t
->control_tracefiles
= g_ptr_array_new();
441 t
->per_cpu_tracefiles
= g_ptr_array_new();
442 t
->facilities
= g_ptr_array_new();
443 getDataEndianType(&(t
->my_arch_size
), &(t
->my_arch_endian
));
445 //get system description
447 strcat(tmp
,"system.xml");
448 getSystemInfo(sys_description
, tmp
);
450 //get facilities info
451 getFacilityInfo(t
,eventdefs
);
453 //get control tracefile info
454 getControlFileInfo(t
,control
);
456 //get cpu tracefile info
457 getCpuFileInfo(t
,cpu
);
462 /******************************************************************************
463 * When we copy a trace, we want all the opening actions to happen again :
464 * the trace will be reopened and totally independant from the original.
465 * That's why we call ltt_trace_open.
466 *****************************************************************************/
467 LttTrace
*ltt_trace_copy(LttTrace
*self
)
469 return ltt_trace_open(self
->pathname
);
472 void ltt_trace_close(LttTrace
*t
)
480 //free system_description
481 g_free(t
->system_description
->description
);
482 g_free(t
->system_description
->node_name
);
483 g_free(t
->system_description
->domain_name
);
484 g_free(t
->system_description
->kernel_name
);
485 g_free(t
->system_description
->kernel_release
);
486 g_free(t
->system_description
->kernel_version
);
487 g_free(t
->system_description
->machine
);
488 g_free(t
->system_description
->processor
);
489 g_free(t
->system_description
->hardware_platform
);
490 g_free(t
->system_description
->operating_system
);
491 g_free(t
->system_description
);
493 //free control_tracefiles
494 for(i
=0;i
<t
->control_tracefile_number
;i
++){
495 tf
= (LttTracefile
*)g_ptr_array_index(t
->control_tracefiles
,i
);
496 ltt_tracefile_close(tf
);
498 g_ptr_array_free(t
->control_tracefiles
, TRUE
);
500 //free per_cpu_tracefiles
501 for(i
=0;i
<t
->per_cpu_tracefile_number
;i
++){
502 tf
= (LttTracefile
*)g_ptr_array_index(t
->per_cpu_tracefiles
,i
);
503 ltt_tracefile_close(tf
);
505 g_ptr_array_free(t
->per_cpu_tracefiles
, TRUE
);
508 for(i
=0;i
<t
->facility_number
;i
++){
509 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
,i
);
510 ltt_facility_close(f
);
512 g_ptr_array_free(t
->facilities
, TRUE
);
520 /*****************************************************************************
521 *Get the system description of the trace
522 ****************************************************************************/
524 LttSystemDescription
*ltt_trace_system_description(LttTrace
*t
)
526 return t
->system_description
;
529 /*****************************************************************************
530 * The following functions discover the facilities of the trace
531 ****************************************************************************/
533 unsigned ltt_trace_facility_number(LttTrace
*t
)
535 return (unsigned)(t
->facility_number
);
538 LttFacility
*ltt_trace_facility_get(LttTrace
*t
, unsigned i
)
540 return (LttFacility
*)g_ptr_array_index(t
->facilities
, i
);
543 /*****************************************************************************
545 * ltt_trace_facility_find : find facilities in the trace
548 * name : facility name
550 * position : position of the facility in the trace
552 * : the number of facilities
553 ****************************************************************************/
555 unsigned ltt_trace_facility_find(LttTrace
*t
, char *name
, unsigned *position
)
559 for(i
=0;i
<t
->facility_number
;i
++){
560 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, i
);
561 if(strcmp(f
->name
,name
)==0){
563 if(count
==1) *position
= i
;
571 /*****************************************************************************
572 * Functions to discover all the event types in the trace
573 ****************************************************************************/
575 unsigned ltt_trace_eventtype_number(LttTrace
*t
)
580 for(i
=0;i
<t
->facility_number
;i
++){
581 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, i
);
582 count
+= f
->event_number
;
587 LttFacility
* ltt_trace_facility_by_id(LttTrace
* trace
, unsigned id
)
589 LttFacility
* facility
;
591 for(i
=0;i
<trace
->facility_number
;i
++){
592 facility
= (LttFacility
*) g_ptr_array_index(trace
->facilities
,i
);
593 if(id
>= facility
->base_id
&&
594 id
< facility
->base_id
+ facility
->event_number
)
597 if(i
==trace
->facility_number
) return NULL
;
598 else return facility
;
601 LttEventType
*ltt_trace_eventtype_get(LttTrace
*t
, unsigned evId
)
604 f
= ltt_trace_facility_by_id(t
,evId
);
606 return f
->events
[evId
- f
->base_id
];
609 /*****************************************************************************
610 *There is one "per cpu" tracefile for each CPU, numbered from 0 to
611 *the maximum number of CPU in the system. When the number of CPU installed
612 *is less than the maximum, some positions are unused. There are also a
613 *number of "control" tracefiles (facilities, interrupts...).
614 ****************************************************************************/
615 unsigned ltt_trace_control_tracefile_number(LttTrace
*t
)
617 return t
->control_tracefile_number
;
620 unsigned ltt_trace_per_cpu_tracefile_number(LttTrace
*t
)
622 return t
->per_cpu_tracefile_number
;
625 /*****************************************************************************
626 *It is possible to search for the tracefiles by name or by CPU position.
627 *The index within the tracefiles of the same type is returned if found
628 *and a negative value otherwise.
629 ****************************************************************************/
631 int ltt_trace_control_tracefile_find(LttTrace
*t
, char *name
)
633 LttTracefile
* tracefile
;
635 for(i
=0;i
<t
->control_tracefile_number
;i
++){
636 tracefile
= (LttTracefile
*)g_ptr_array_index(t
->control_tracefiles
, i
);
637 if(strcmp(tracefile
->name
, name
)==0)break;
639 if(i
== t
->control_tracefile_number
) return -1;
643 int ltt_trace_per_cpu_tracefile_find(LttTrace
*t
, unsigned i
)
645 LttTracefile
* tracefile
;
647 for(j
=0;j
<t
->per_cpu_tracefile_number
;j
++){
648 tracefile
= (LttTracefile
*)g_ptr_array_index(t
->per_cpu_tracefiles
, j
);
649 name
= atoi(tracefile
->name
);
650 if(name
== (int)i
)break;
652 if(j
== t
->per_cpu_tracefile_number
) return -1;
656 /*****************************************************************************
657 *Get a specific tracefile
658 ****************************************************************************/
660 LttTracefile
*ltt_trace_control_tracefile_get(LttTrace
*t
, unsigned i
)
662 return (LttTracefile
*)g_ptr_array_index(t
->control_tracefiles
, i
);
665 LttTracefile
*ltt_trace_per_cpu_tracefile_get(LttTrace
*t
, unsigned i
)
667 return (LttTracefile
*)g_ptr_array_index(t
->per_cpu_tracefiles
, i
);
670 /*****************************************************************************
671 * Get the start time and end time of the trace
672 ****************************************************************************/
674 void ltt_trace_time_span_get(LttTrace
*t
, LttTime
*start
, LttTime
*end
)
676 LttTime startSmall
, startTmp
, endBig
, endTmp
;
680 for(i
=0;i
<t
->control_tracefile_number
;i
++){
681 tf
= g_ptr_array_index(t
->control_tracefiles
, i
);
683 startTmp
= tf
->a_block_start
->time
;
684 readBlock(tf
,tf
->block_number
);
685 endTmp
= tf
->a_block_end
->time
;
687 startSmall
= startTmp
;
692 if(ltt_time_compare(startSmall
,startTmp
) > 0) startSmall
= startTmp
;
693 if(ltt_time_compare(endBig
,endTmp
) < 0) endBig
= endTmp
;
696 for(i
=0;i
<t
->per_cpu_tracefile_number
;i
++){
697 tf
= g_ptr_array_index(t
->per_cpu_tracefiles
, i
);
699 startTmp
= tf
->a_block_start
->time
;
700 readBlock(tf
,tf
->block_number
);
701 endTmp
= tf
->a_block_end
->time
;
703 startSmall
= startTmp
;
707 if(ltt_time_compare(startSmall
,startTmp
) > 0) startSmall
= startTmp
;
708 if(ltt_time_compare(endBig
,endTmp
) < 0) endBig
= endTmp
;
716 /*****************************************************************************
717 *Get the name of a tracefile
718 ****************************************************************************/
720 char *ltt_tracefile_name(LttTracefile
*tf
)
725 /*****************************************************************************
726 * Get the number of blocks in the tracefile
727 ****************************************************************************/
729 unsigned ltt_tracefile_block_number(LttTracefile
*tf
)
731 return tf
->block_number
;
734 /*****************************************************************************
736 * ltt_tracefile_seek_time: seek to the first event of the trace with time
737 * larger or equal to time
740 * time : criteria of the time
741 ****************************************************************************/
743 void ltt_tracefile_seek_time(LttTracefile
*t
, LttTime time
)
747 int headTime
= ltt_time_compare(t
->a_block_start
->time
, time
);
748 int tailTime
= ltt_time_compare(t
->a_block_end
->time
, time
);
751 if(headTime
< 0 && tailTime
> 0){
752 if(ltt_time_compare(t
->a_block_end
->time
, t
->current_event_time
) !=0) {
753 lttTime
= getEventTime(t
);
754 err
= ltt_time_compare(lttTime
, time
);
756 if(t
->which_event
==2 || (&t
->prev_event_time
,&time
)<0){
760 return ltt_tracefile_seek_time(t
, time
);
764 ev
= ltt_tracefile_read(t
);
766 g_print("End of file\n");
769 lttTime
= getEventTime(t
);
770 err
= ltt_time_compare(lttTime
, time
);
774 }else{//we are at the end of the block
776 return ltt_tracefile_seek_time(t
, time
);
778 }else if(headTime
>= 0){
779 if(t
->which_block
== 1){
782 if(ltt_time_compare(t
->prev_block_end_time
, time
) >= 0 ||
783 (t
->prev_block_end_time
.tv_sec
== 0 &&
784 t
->prev_block_end_time
.tv_nsec
== 0 )){
785 err
=readBlock(t
,t
->which_block
-1);
786 if(err
) g_error("Can not read tracefile: %s\n", t
->name
);
787 return ltt_tracefile_seek_time(t
, time
) ;
792 }else if(tailTime
< 0){
793 if(t
->which_block
!= t
->block_number
){
794 err
=readBlock(t
,t
->which_block
+1);
795 if(err
) g_error("Can not read tracefile: %s\n", t
->name
);
797 g_print("End of file\n");
800 if(tailTime
< 0) return ltt_tracefile_seek_time(t
, time
);
801 }else if(tailTime
== 0){
802 t
->cur_event_pos
= t
->last_event_pos
;
803 t
->current_event_time
= time
;
804 t
->cur_heart_beat_number
= 0;
805 t
->prev_event_time
.tv_sec
= 0;
806 t
->prev_event_time
.tv_nsec
= 0;
811 /*****************************************************************************
812 * Seek to the first event with position equal or larger to ep
813 ****************************************************************************/
815 void ltt_tracefile_seek_position(LttTracefile
*t
, LttEventPosition
*ep
)
817 //if we are at the right place, just return
818 if(t
->which_block
== ep
->block_num
&& t
->which_event
== ep
->event_num
)
821 if(t
->which_block
== ep
->block_num
) updateTracefile(t
);
822 else readBlock(t
,ep
->block_num
);
824 //event offset is availiable
825 if(ep
->old_position
){
826 t
->cur_heart_beat_number
= ep
->heart_beat_number
;
827 t
->cur_event_pos
= t
->buffer
+ ep
->event_offset
;
831 //only block number and event index are availiable
832 while(t
->which_event
< ep
->event_num
) ltt_tracefile_read(t
);
837 /*****************************************************************************
839 * ltt_tracefile_read : read the current event, set the pointer to the next
843 * LttEvent * : an event to be processed
844 ****************************************************************************/
846 LttEvent
*ltt_tracefile_read(LttTracefile
*t
)
848 LttEvent
* lttEvent
= &t
->an_event
;
851 if(t
->cur_event_pos
== t
->buffer
+ t
->block_size
){
852 if(t
->which_block
== t
->block_number
){
855 err
= readBlock(t
, t
->which_block
+ 1);
856 if(err
)g_error("Can not read tracefile");
859 lttEvent
->event_id
= (int)(*(uint16_t *)(t
->cur_event_pos
));
860 if(lttEvent
->event_id
== TRACE_TIME_HEARTBEAT
)
861 t
->cur_heart_beat_number
++;
863 t
->prev_event_time
= t
->current_event_time
;
864 // t->current_event_time = getEventTime(t);
866 lttEvent
->time_delta
= *(uint32_t*)(t
->cur_event_pos
+ EVENT_ID_SIZE
);
867 lttEvent
->event_time
= t
->current_event_time
;
869 lttEvent
->tracefile
= t
;
870 lttEvent
->data
= t
->cur_event_pos
+ EVENT_HEADER_SIZE
;
871 lttEvent
->which_block
= t
->which_block
;
872 lttEvent
->which_event
= t
->which_event
;
874 //update the fields of the current event and go to the next event
876 if(err
== ERANGE
) g_error("event id is out of range\n");
878 lttEvent
->event_cycle_count
= t
->cur_cycle_count
;
883 /****************************************************************************
885 * readFile : wrap function to read from a file
887 * fd : file descriptor
888 * buf : buf to contain the content
889 * size : number of bytes to be read
890 * mesg : message to be printed if some thing goes wrong
893 * EIO : can not read from the file
894 ****************************************************************************/
896 int readFile(int fd
, void * buf
, size_t size
, char * mesg
)
899 nbBytes
= read(fd
, buf
, size
);
907 /****************************************************************************
909 * readBlock : read a block from the file
911 * lttdes : ltt trace file
912 * whichBlock : the block which will be read
915 * EINVAL : lseek fail
916 * EIO : can not read from the file
917 ****************************************************************************/
919 int readBlock(LttTracefile
* tf
, int whichBlock
)
924 if(whichBlock
- tf
->which_block
== 1 && tf
->which_block
!= 0){
925 tf
->prev_block_end_time
= tf
->a_block_end
->time
;
926 tf
->prev_event_time
= tf
->a_block_end
->time
;
928 tf
->prev_block_end_time
.tv_sec
= 0;
929 tf
->prev_block_end_time
.tv_nsec
= 0;
930 tf
->prev_event_time
.tv_sec
= 0;
931 tf
->prev_event_time
.tv_nsec
= 0;
934 nbBytes
=lseek(tf
->fd
,(off_t
)((whichBlock
-1)*tf
->block_size
), SEEK_SET
);
935 if(nbBytes
== -1) return EINVAL
;
937 if(readFile(tf
->fd
,tf
->buffer
,tf
->block_size
,"Unable to read a block"))
940 tf
->a_block_start
=(BlockStart
*) (tf
->buffer
+ EVENT_HEADER_SIZE
);
941 lostSize
= *(uint32_t*)(tf
->buffer
+ tf
->block_size
- sizeof(uint32_t));
942 tf
->a_block_end
=(BlockEnd
*)(tf
->buffer
+ tf
->block_size
-
943 lostSize
+ EVENT_HEADER_SIZE
);
944 tf
->last_event_pos
= tf
->buffer
+ tf
->block_size
- lostSize
;
946 tf
->which_block
= whichBlock
;
948 tf
->cur_event_pos
= tf
->buffer
;//the beginning of the block, block start ev
949 tf
->cur_heart_beat_number
= 0;
953 tf
->current_event_time
= getEventTime(tf
);
958 /*****************************************************************************
960 * updateTracefile : reinitialize the info of the block which is already
964 ****************************************************************************/
966 void updateTracefile(LttTracefile
* tf
)
969 tf
->cur_event_pos
= tf
->buffer
;
970 tf
->current_event_time
= getEventTime(tf
);
971 tf
->cur_heart_beat_number
= 0;
973 tf
->prev_event_time
.tv_sec
= 0;
974 tf
->prev_event_time
.tv_nsec
= 0;
977 /*****************************************************************************
979 * skipEvent : go to the next event, update the fields of the current event
984 * ERANGE : event id is out of range
985 ****************************************************************************/
987 int skipEvent(LttTracefile
* t
)
994 evId
= (int)(*(uint16_t *)(t
->cur_event_pos
));
995 evData
= t
->cur_event_pos
+ EVENT_HEADER_SIZE
;
997 evT
= ltt_trace_eventtype_get(t
->trace
,(unsigned)evId
);
999 if(evT
) rootFld
= evT
->root_field
;
1003 //event has string/sequence or the last event is not the same event
1004 if((evT
->latest_block
!=t
->which_block
|| evT
->latest_event
!=t
->which_event
)
1005 && rootFld
->field_fixed
== 0){
1006 setFieldsOffset(t
, evT
, evData
, t
->trace
);
1008 t
->cur_event_pos
+= EVENT_HEADER_SIZE
+ rootFld
->field_size
;
1009 }else t
->cur_event_pos
+= EVENT_HEADER_SIZE
;
1011 evT
->latest_block
= t
->which_block
;
1012 evT
->latest_event
= t
->which_event
;
1014 //the next event is in the next block
1015 if(evId
== TRACE_BLOCK_END
){
1016 t
->cur_event_pos
= t
->buffer
+ t
->block_size
;
1019 t
->current_event_time
= getEventTime(t
);
1025 /*****************************************************************************
1027 * getCyclePerNsec : calculate cycles per nsec for current block
1030 ****************************************************************************/
1032 void getCyclePerNsec(LttTracefile
* t
)
1034 LttTime lBufTotalTime
; /* Total time for this buffer */
1035 LttCycleCount lBufTotalNSec
; /* Total time for this buffer in nsecs */
1036 LttCycleCount lBufTotalCycle
;/* Total cycles for this buffer */
1038 /* Calculate the total time for this buffer */
1039 lBufTotalTime
= ltt_time_sub(t
->a_block_end
->time
, t
->a_block_start
->time
);
1041 /* Calculate the total cycles for this bufffer */
1042 lBufTotalCycle
= t
->a_block_end
->cycle_count
;
1043 lBufTotalCycle
-= t
->a_block_start
->cycle_count
;
1045 /* Convert the total time to nsecs */
1046 lBufTotalNSec
= lBufTotalTime
.tv_sec
;
1047 lBufTotalNSec
*= NANOSECONDS_PER_SECOND
;
1048 lBufTotalNSec
+= lBufTotalTime
.tv_nsec
;
1050 t
->cycle_per_nsec
= (double)lBufTotalCycle
/ (double)lBufTotalNSec
;
1053 /****************************************************************************
1055 * getEventTime : obtain the time of an event
1059 * LttTime : the time of the event
1060 ****************************************************************************/
1062 LttTime
getEventTime(LttTracefile
* tf
)
1065 LttCycleCount cycle_count
; // cycle count for the current event
1066 LttCycleCount lEventTotalCycle
; // Total cycles from start for event
1067 double lEventNSec
; // Total usecs from start for event
1068 LttTime lTimeOffset
; // Time offset in struct LttTime
1070 int64_t nanoSec
, tmpCycleCount
= (((uint64_t)1)<<32);
1071 static LttCycleCount preCycleCount
= 0;
1072 static int count
= 0;
1074 evId
= *(uint16_t*)tf
->cur_event_pos
;
1075 if(evId
== TRACE_BLOCK_START
){
1078 tf
->cur_cycle_count
= tf
->a_block_start
->cycle_count
;
1079 return tf
->a_block_start
->time
;
1080 }else if(evId
== TRACE_BLOCK_END
){
1083 tf
->cur_cycle_count
= tf
->a_block_end
->cycle_count
;
1084 return tf
->a_block_end
->time
;
1087 // Calculate total time in cycles from start of buffer for this event
1088 cycle_count
= (LttCycleCount
)*(uint32_t*)(tf
->cur_event_pos
+ EVENT_ID_SIZE
);
1090 if(cycle_count
< preCycleCount
)count
++;
1091 preCycleCount
= cycle_count
;
1092 cycle_count
+= tmpCycleCount
* count
;
1094 if(tf
->cur_heart_beat_number
> count
)
1095 cycle_count
+= tmpCycleCount
* (tf
->cur_heart_beat_number
- count
);
1097 tf
->cur_cycle_count
= cycle_count
;
1099 lEventTotalCycle
= cycle_count
;
1100 lEventTotalCycle
-= tf
->a_block_start
->cycle_count
;
1102 // Convert it to nsecs
1103 lEventNSec
= lEventTotalCycle
/ tf
->cycle_per_nsec
;
1104 nanoSec
= lEventNSec
;
1106 // Determine offset in struct LttTime
1107 lTimeOffset
.tv_nsec
= nanoSec
% NANOSECONDS_PER_SECOND
;
1108 lTimeOffset
.tv_sec
= nanoSec
/ NANOSECONDS_PER_SECOND
;
1110 time
= ltt_time_add(tf
->a_block_start
->time
, lTimeOffset
);
1115 /*****************************************************************************
1117 * setFieldsOffset : set offset of the fields
1119 * tracefile : opened trace file
1120 * evT : the event type
1121 * evD : event data, it may be NULL
1122 ****************************************************************************/
1124 void setFieldsOffset(LttTracefile
*tf
,LttEventType
*evT
,void *evD
,LttTrace
* t
)
1126 LttField
* rootFld
= evT
->root_field
;
1127 // rootFld->base_address = evD;
1130 rootFld
->field_size
= getFieldtypeSize(tf
, evT
, 0,0,rootFld
, evD
,t
);
1133 /*****************************************************************************
1135 * getFieldtypeSize: get the size of the field type (primitive type)
1137 * tracefile : opened trace file
1139 * offsetRoot : offset from the root
1140 * offsetParent : offset from the parrent
1142 * evD : event data, it may be NULL
1144 * int : size of the field
1145 ****************************************************************************/
1147 int getFieldtypeSize(LttTracefile
* t
, LttEventType
* evT
, int offsetRoot
,
1148 int offsetParent
, LttField
* fld
, void *evD
, LttTrace
*trace
)
1150 int size
, size1
, element_number
, i
, offset1
, offset2
;
1151 LttType
* type
= fld
->field_type
;
1154 if(evT
->latest_block
==t
->which_block
&& evT
->latest_event
==t
->which_event
){
1155 return fld
->field_size
;
1159 if(fld
->field_fixed
== 1){
1160 if(fld
== evT
->root_field
) return fld
->field_size
;
1163 if(type
->type_class
!= LTT_STRUCT
&& type
->type_class
!= LTT_ARRAY
&&
1164 type
->type_class
!= LTT_SEQUENCE
&& type
->type_class
!= LTT_STRING
){
1165 if(fld
->field_fixed
== -1){
1166 size
= (int) ltt_type_size(trace
, type
);
1167 fld
->field_fixed
= 1;
1168 }else size
= fld
->field_size
;
1170 }else if(type
->type_class
== LTT_ARRAY
){
1171 element_number
= (int) type
->element_number
;
1172 if(fld
->field_fixed
== -1){
1173 size
= getFieldtypeSize(t
, evT
, offsetRoot
,0,fld
->child
[0], NULL
, trace
);
1174 if(size
== 0){ //has string or sequence
1175 fld
->field_fixed
= 0;
1177 fld
->field_fixed
= 1;
1178 size
*= element_number
;
1180 }else if(fld
->field_fixed
== 0){// has string or sequence
1182 for(i
=0;i
<element_number
;i
++){
1183 size
+= getFieldtypeSize(t
, evT
, offsetRoot
+size
,size
,
1184 fld
->child
[0], evD
+size
, trace
);
1186 }else size
= fld
->field_size
;
1188 }else if(type
->type_class
== LTT_SEQUENCE
){
1189 size1
= (int) ltt_type_size(trace
, type
);
1190 if(fld
->field_fixed
== -1){
1191 fld
->sequ_number_size
= size1
;
1192 fld
->field_fixed
= 0;
1193 size
= getFieldtypeSize(t
, evT
, offsetRoot
,0,fld
->child
[0], NULL
, trace
);
1194 fld
->element_size
= size
;
1196 element_number
= getIntNumber(size1
,evD
);
1197 type
->element_number
= element_number
;
1198 if(fld
->element_size
> 0){
1199 size
= element_number
* fld
->element_size
;
1200 }else{//sequence has string or sequence
1202 for(i
=0;i
<element_number
;i
++){
1203 size
+= getFieldtypeSize(t
, evT
, offsetRoot
+size
+size1
,size
+size1
,
1204 fld
->child
[0], evD
+size
+size1
, trace
);
1210 }else if(type
->type_class
== LTT_STRING
){
1212 if(fld
->field_fixed
== -1){
1213 fld
->field_fixed
= 0;
1215 size
= strlen((char*)evD
) + 1; //include end : '\0'
1218 }else if(type
->type_class
== LTT_STRUCT
){
1219 element_number
= (int) type
->element_number
;
1221 if(fld
->field_fixed
== -1){
1222 offset1
= offsetRoot
;
1224 for(i
=0;i
<element_number
;i
++){
1225 size1
=getFieldtypeSize(t
, evT
,offset1
,offset2
, fld
->child
[i
], NULL
, trace
);
1226 if(size1
> 0 && size
>= 0){
1228 if(offset1
>= 0) offset1
+= size1
;
1237 fld
->field_fixed
= 0;
1239 }else fld
->field_fixed
= 1;
1240 }else if(fld
->field_fixed
== 0){
1241 offset1
= offsetRoot
;
1243 for(i
=0;i
<element_number
;i
++){
1244 size
=getFieldtypeSize(t
,evT
,offset1
,offset2
,fld
->child
[i
],evD
+offset2
, trace
);
1249 }else size
= fld
->field_size
;
1252 fld
->offset_root
= offsetRoot
;
1253 fld
->offset_parent
= offsetParent
;
1255 fld
->fixed_root
= (offsetRoot
==-1) ? 0 : 1;
1256 fld
->fixed_parent
= (offsetParent
==-1) ? 0 : 1;
1258 fld
->field_size
= size
;
1264 /*****************************************************************************
1266 * getIntNumber : get an integer number
1268 * size : the size of the integer
1269 * evD : the event data
1272 ****************************************************************************/
1274 int getIntNumber(int size
, void *evD
)
1277 if(size
== 1) i
= *(int8_t *)evD
;
1278 else if(size
== 2) i
= *(int16_t *)evD
;
1279 else if(size
== 4) i
= *(int32_t *)evD
;
1280 else if(size
== 8) i
= *(int64_t *)evD
;
1285 /*****************************************************************************
1287 * getDataEndianType : get the data type size and endian type of the local
1290 * size : size of data type
1291 * endian : endian type, little or big
1292 ****************************************************************************/
1294 void getDataEndianType(LttArchSize
* size
, LttArchEndian
* endian
)
1298 int sizeInt
=sizeof(int), sizeLong
=sizeof(long), sizePointer
=sizeof(void *);
1300 if(c
== 1) *endian
= LTT_LITTLE_ENDIAN
;
1301 else *endian
= LTT_BIG_ENDIAN
;
1303 if(sizeInt
== 2 && sizeLong
== 4 && sizePointer
== 4)
1305 else if(sizeInt
== 4 && sizeLong
== 4 && sizePointer
== 4)
1307 else if(sizeInt
== 4 && sizeLong
== 8 && sizePointer
== 8)
1309 else if(sizeInt
== 8 && sizeLong
== 8 && sizePointer
== 8)
1311 else *size
= LTT_UNKNOWN
;