6 #include <linux/errno.h>
11 #define DIR_NAME_SIZE 256
13 /* set the offset of the fields belonging to the event,
14 need the information of the archecture */
15 void setFieldsOffset(LttTracefile
*tf
,LttEventType
*evT
,void *evD
,LttTrace
*t
);
17 /* get the size of the field type according to the archtecture's
18 size and endian type(info of the archecture) */
19 int getFieldtypeSize(LttTracefile
* tf
, LttEventType
* evT
, int offsetRoot
,
20 int offsetParent
, LttField
*fld
, void *evD
, LttTrace
* t
);
22 /* read a fixed size or a block information from the file (fd) */
23 int readFile(int fd
, void * buf
, size_t size
, char * mesg
);
24 int readBlock(LttTracefile
* tf
, int whichBlock
);
26 /* calculate cycles per nsec for current block */
27 void getCyclePerNsec(LttTracefile
* t
);
29 /* reinitialize the info of the block which is already in the buffer */
30 void updateTracefile(LttTracefile
* tf
);
32 /* go to the next event */
33 int skipEvent(LttTracefile
* t
);
36 /*****************************************************************************
38 * ltt_tracefile_open : open a trace file, construct a LttTracefile
40 * t : the trace containing the tracefile
41 * fileName : path name of the trace file
43 * : a pointer to a tracefile
44 ****************************************************************************/
46 LttTracefile
* ltt_tracefile_open(LttTrace
* t
, char * fileName
)
49 struct stat lTDFStat
; /* Trace data file status */
50 BlockStart a_block_start
;
52 tf
= g_new(LttTracefile
, 1);
55 tf
->name
= g_strdup(fileName
);
57 tf
->fd
= open(fileName
, O_RDONLY
, 0);
59 g_error("Unable to open input data file %s\n", fileName
);
62 // Get the file's status
63 if(fstat(tf
->fd
, &lTDFStat
) < 0){
64 g_error("Unable to get the status of the input data file %s\n", fileName
);
67 // Is the file large enough to contain a trace
68 if(lTDFStat
.st_size
< sizeof(BlockStart
) + EVENT_HEADER_SIZE
){
69 g_print("The input data file %s does not contain a trace\n", fileName
);
76 //store the size of the file
77 tf
->file_size
= lTDFStat
.st_size
;
78 tf
->block_size
= t
->system_description
->ltt_block_size
;
79 tf
->block_number
= tf
->file_size
/ tf
->block_size
;
82 //allocate memory to contain the info of a block
83 tf
->buffer
= (void *) g_new(char, t
->system_description
->ltt_block_size
);
85 //read the first block
86 if(readBlock(tf
,1)) exit(1);
92 /*****************************************************************************
93 *Open control and per cpu tracefiles
94 ****************************************************************************/
96 void ltt_tracefile_open_cpu(LttTrace
*t
, char * tracefile_name
)
99 tf
= ltt_tracefile_open(t
,tracefile_name
);
101 t
->per_cpu_tracefile_number
++;
102 g_ptr_array_add(t
->per_cpu_tracefiles
, tf
);
105 void ltt_tracefile_open_control(LttTrace
*t
, char * control_name
)
115 tf
= ltt_tracefile_open(t
,control_name
);
117 t
->control_tracefile_number
++;
118 g_ptr_array_add(t
->control_tracefiles
,tf
);
120 //parse facilities tracefile to get base_id
121 if(strcmp(&control_name
[strlen(control_name
)-10],"facilities") ==0){
123 ev
= ltt_tracefile_read(tf
);
124 if(!ev
)return; // end of file
126 if(ev
->event_id
== TRACE_FACILITY_LOAD
){
128 fLoad
.name
= (char*)pos
;
129 fLoad
.checksum
= *(LttChecksum
*)(pos
+ strlen(fLoad
.name
));
130 fLoad
.base_code
= *(guint32
*)(pos
+ strlen(fLoad
.name
) + sizeof(LttChecksum
));
132 for(i
=0;i
<t
->facility_number
;i
++){
133 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
,i
);
134 if(strcmp(f
->name
,fLoad
.name
)==0 && fLoad
.checksum
==f
->checksum
){
135 f
->base_id
= fLoad
.base_code
;
139 if(i
==t
->facility_number
)
140 g_error("Facility: %s, checksum: %d is not founded\n",
141 fLoad
.name
,fLoad
.checksum
);
142 }else if(ev
->event_id
== TRACE_BLOCK_START
){
144 }else if(ev
->event_id
== TRACE_BLOCK_END
){
146 }else g_error("Not valid facilities trace file\n");
151 /*****************************************************************************
153 * ltt_tracefile_close: close a trace file,
155 * t : tracefile which will be closed
156 ****************************************************************************/
158 void ltt_tracefile_close(LttTracefile
*t
)
167 /*****************************************************************************
168 *Get system information
169 ****************************************************************************/
170 void getSystemInfo(LttSystemDescription
* des
, char * pathname
)
174 int entry_number
= 15;
175 char buf
[DIR_NAME_SIZE
];
176 char description
[4*DIR_NAME_SIZE
];
179 fp
= fopen(pathname
,"r");
181 g_error("Can not open file : %s\n", pathname
);
184 while(fgets(buf
,DIR_NAME_SIZE
, fp
)!= NULL
){
186 while(isspace(*ptr
)) ptr
++;
187 if(strlen(ptr
) == 0) continue;
191 if(strlen(ptr
) == 0) g_error("Not a valid file: %s\n", pathname
);
192 if(strncmp("<system",ptr
,7) !=0)g_error("Not a valid file: %s\n", pathname
);
194 for(i
=0;i
<entry_number
;i
++){
195 if(fgets(buf
,DIR_NAME_SIZE
, fp
)== NULL
)
196 g_error("Not a valid file: %s\n", pathname
);
197 buf
[strlen(buf
)-1] = '\0';
199 while(isspace(*ptr
)) ptr
++;
202 if(strncmp("node_name=",ptr
,10)!=0)
203 g_error("Not a valid file: %s\n", pathname
);
204 des
->node_name
= g_strdup(ptr
+10);
207 if(strncmp("domainname=",ptr
,11)!=0)
208 g_error("Not a valid file: %s\n", pathname
);
209 des
->domain_name
= g_strdup(ptr
+11);
212 if(strncmp("cpu=",ptr
,4)!=0)
213 g_error("Not a valid file: %s\n", pathname
);
214 des
->nb_cpu
= (unsigned)atoi(ptr
+4);
217 if(strncmp("arch_size=",ptr
,10)!=0)
218 g_error("Not a valid file: %s\n", pathname
);
219 if(strcmp(ptr
+10,"\"LP32\"") == 0) des
->size
= LTT_LP32
;
220 else if(strcmp(ptr
+10,"\"ILP32\"") == 0) des
->size
= LTT_ILP32
;
221 else if(strcmp(ptr
+10,"\"LP64\"") == 0) des
->size
= LTT_LP64
;
222 else if(strcmp(ptr
+10,"\"ILP64\"") == 0) des
->size
= LTT_ILP64
;
223 else if(strcmp(ptr
+10,"\"UNKNOWN\"") == 0) des
->size
= LTT_UNKNOWN
;
226 if(strncmp("endian=",ptr
,7)!=0)
227 g_error("Not a valid file: %s\n", pathname
);
228 if(strcmp(ptr
+7,"\"LITTLE_ENDIAN\"") == 0)
229 des
->endian
= LTT_LITTLE_ENDIAN
;
230 else if(strcmp(ptr
+7,"\"BIG_ENDIAN\"") == 0)
231 des
->endian
= LTT_BIG_ENDIAN
;
234 if(strncmp("kernel_name=",ptr
,12)!=0)
235 g_error("Not a valid file: %s\n", pathname
);
236 des
->kernel_name
= g_strdup(ptr
+12);
239 if(strncmp("kernel_release=",ptr
,15)!=0)
240 g_error("Not a valid file: %s\n", pathname
);
241 des
->kernel_release
= g_strdup(ptr
+15);
244 if(strncmp("kernel_version=",ptr
,15)!=0)
245 g_error("Not a valid file: %s\n", pathname
);
246 des
->kernel_version
= g_strdup(ptr
+15);
249 if(strncmp("machine=",ptr
,8)!=0)
250 g_error("Not a valid file: %s\n", pathname
);
251 des
->machine
= g_strdup(ptr
+8);
254 if(strncmp("processor=",ptr
,10)!=0)
255 g_error("Not a valid file: %s\n", pathname
);
256 des
->processor
= g_strdup(ptr
+10);
259 if(strncmp("hardware_platform=",ptr
,18)!=0)
260 g_error("Not a valid file: %s\n", pathname
);
261 des
->hardware_platform
= g_strdup(ptr
+18);
264 if(strncmp("operating_system=",ptr
,17)!=0)
265 g_error("Not a valid file: %s\n", pathname
);
266 des
->operating_system
= g_strdup(ptr
+17);
269 if(strncmp("ltt_major_version=",ptr
,18)!=0)
270 g_error("Not a valid file: %s\n", pathname
);
272 // ptr++;//skip begining "
273 // ptr[strlen(ptr)-1] = '\0'; //get rid of the ending "
274 des
->ltt_major_version
= (unsigned)atoi(ptr
);
277 if(strncmp("ltt_minor_version=",ptr
,18)!=0)
278 g_error("Not a valid file: %s\n", pathname
);
280 // ptr++;//skip begining "
281 // ptr[strlen(ptr)-1] = '\0'; //get rid of the ending "
282 des
->ltt_minor_version
= (unsigned)atoi(ptr
);
285 if(strncmp("ltt_block_size=",ptr
,15)!=0)
286 g_error("Not a valid file: %s\n", pathname
);
288 // ptr++;//skip begining "
289 // ptr[strlen(ptr)-1] = '\0'; //get rid of the ending "
290 des
->ltt_block_size
= (unsigned)atoi(ptr
);
293 g_error("Not a valid file: %s\n", pathname
);
298 description
[0] = '\0';
299 if(fgets(buf
,DIR_NAME_SIZE
, fp
)== NULL
)
300 g_error("Not a valid file: %s\n", pathname
);
302 while(isspace(*ptr
)) ptr
++;
303 if(*ptr
!= '>') g_error("Not a valid file: %s\n", pathname
);
304 while((ptr
=fgets(buf
,DIR_NAME_SIZE
, fp
))!= NULL
){
306 while(isspace(*ptr
)) ptr
++;
307 if(strncmp("</system>",ptr
,9) == 0 )break;
308 strcat(description
, buf
);
310 if(!ptr
)g_error("Not a valid file: %s\n", pathname
);
311 if(description
[0] = '\0')des
->description
= NULL
;
312 des
->description
= g_strdup(description
);
317 /*****************************************************************************
318 *The following functions get facility/tracefile information
319 ****************************************************************************/
321 void getFacilityInfo(LttTrace
*t
, char* eventdefs
)
324 struct dirent
*entry
;
329 char name
[DIR_NAME_SIZE
];
331 dir
= opendir(eventdefs
);
332 if(!dir
) g_error("Can not open directory: %s\n", eventdefs
);
334 while((entry
= readdir(dir
)) != NULL
){
335 ptr
= &entry
->d_name
[strlen(entry
->d_name
)-4];
336 if(strcmp(ptr
,".xml") != 0) continue;
337 strcpy(name
,eventdefs
);
338 strcat(name
,entry
->d_name
);
339 ltt_facility_open(t
,name
);
343 for(j
=0;j
<t
->facility_number
;j
++){
344 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, j
);
345 for(i
=0; i
<f
->event_number
; i
++){
347 setFieldsOffset(NULL
, et
, NULL
, t
);
352 void getControlFileInfo(LttTrace
*t
, char* control
)
355 struct dirent
*entry
;
356 char name
[DIR_NAME_SIZE
];
358 dir
= opendir(control
);
359 if(!dir
) g_error("Can not open directory: %s\n", control
);
361 while((entry
= readdir(dir
)) != NULL
){
362 if(strcmp(entry
->d_name
,"facilities") != 0 &&
363 strcmp(entry
->d_name
,"interrupts") != 0 &&
364 strcmp(entry
->d_name
,"processes") != 0) continue;
366 strcpy(name
,control
);
367 strcat(name
,entry
->d_name
);
368 ltt_tracefile_open_control(t
,name
);
373 void getCpuFileInfo(LttTrace
*t
, char* cpu
)
376 struct dirent
*entry
;
377 char name
[DIR_NAME_SIZE
];
380 if(!dir
) g_error("Can not open directory: %s\n", cpu
);
382 while((entry
= readdir(dir
)) != NULL
){
383 if(strcmp(entry
->d_name
,".") != 0 &&
384 strcmp(entry
->d_name
,"..") != 0 ){
386 strcat(name
,entry
->d_name
);
387 ltt_tracefile_open_cpu(t
,name
);
393 /*****************************************************************************
394 *A trace is specified as a pathname to the directory containing all the
395 *associated data (control tracefiles, per cpu tracefiles, event
398 *When a trace is closed, all the associated facilities, types and fields
399 *are released as well.
400 ****************************************************************************/
402 LttTrace
*ltt_trace_open(const char *pathname
)
405 LttSystemDescription
* sys_description
;
406 char eventdefs
[DIR_NAME_SIZE
];
407 char info
[DIR_NAME_SIZE
];
408 char control
[DIR_NAME_SIZE
];
409 char cpu
[DIR_NAME_SIZE
];
410 char tmp
[DIR_NAME_SIZE
];
411 gboolean has_slash
= FALSE
;
413 //establish the pathname to different directories
414 if(pathname
[strlen(pathname
)-1] == '/')has_slash
= TRUE
;
415 strcpy(eventdefs
,pathname
);
416 if(!has_slash
)strcat(eventdefs
,"/");
417 strcat(eventdefs
,"eventdefs/");
419 strcpy(info
,pathname
);
420 if(!has_slash
)strcat(info
,"/");
421 strcat(info
,"info/");
423 strcpy(control
,pathname
);
424 if(!has_slash
)strcat(control
,"/");
425 strcat(control
,"control/");
427 strcpy(cpu
,pathname
);
428 if(!has_slash
)strcat(cpu
,"/");
432 t
= g_new(LttTrace
, 1);
433 sys_description
= g_new(LttSystemDescription
, 1);
434 t
->pathname
= g_strdup(pathname
);
435 t
->facility_number
= 0;
436 t
->control_tracefile_number
= 0;
437 t
->per_cpu_tracefile_number
= 0;
438 t
->system_description
= sys_description
;
439 t
->control_tracefiles
= g_ptr_array_new();
440 t
->per_cpu_tracefiles
= g_ptr_array_new();
441 t
->facilities
= g_ptr_array_new();
442 getDataEndianType(&(t
->my_arch_size
), &(t
->my_arch_endian
));
444 //get system description
446 strcat(tmp
,"system.xml");
447 getSystemInfo(sys_description
, tmp
);
449 //get facilities info
450 getFacilityInfo(t
,eventdefs
);
452 //get control tracefile info
453 getControlFileInfo(t
,control
);
455 //get cpu tracefile info
456 getCpuFileInfo(t
,cpu
);
461 char * ltt_trace_name(LttTrace
*t
)
467 /******************************************************************************
468 * When we copy a trace, we want all the opening actions to happen again :
469 * the trace will be reopened and totally independant from the original.
470 * That's why we call ltt_trace_open.
471 *****************************************************************************/
472 LttTrace
*ltt_trace_copy(LttTrace
*self
)
474 return ltt_trace_open(self
->pathname
);
477 void ltt_trace_close(LttTrace
*t
)
485 //free system_description
486 g_free(t
->system_description
->description
);
487 g_free(t
->system_description
->node_name
);
488 g_free(t
->system_description
->domain_name
);
489 g_free(t
->system_description
->kernel_name
);
490 g_free(t
->system_description
->kernel_release
);
491 g_free(t
->system_description
->kernel_version
);
492 g_free(t
->system_description
->machine
);
493 g_free(t
->system_description
->processor
);
494 g_free(t
->system_description
->hardware_platform
);
495 g_free(t
->system_description
->operating_system
);
496 g_free(t
->system_description
);
498 //free control_tracefiles
499 for(i
=0;i
<t
->control_tracefile_number
;i
++){
500 tf
= (LttTracefile
*)g_ptr_array_index(t
->control_tracefiles
,i
);
501 ltt_tracefile_close(tf
);
503 g_ptr_array_free(t
->control_tracefiles
, TRUE
);
505 //free per_cpu_tracefiles
506 for(i
=0;i
<t
->per_cpu_tracefile_number
;i
++){
507 tf
= (LttTracefile
*)g_ptr_array_index(t
->per_cpu_tracefiles
,i
);
508 ltt_tracefile_close(tf
);
510 g_ptr_array_free(t
->per_cpu_tracefiles
, TRUE
);
513 for(i
=0;i
<t
->facility_number
;i
++){
514 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
,i
);
515 ltt_facility_close(f
);
517 g_ptr_array_free(t
->facilities
, TRUE
);
525 /*****************************************************************************
526 *Get the system description of the trace
527 ****************************************************************************/
529 LttSystemDescription
*ltt_trace_system_description(LttTrace
*t
)
531 return t
->system_description
;
534 /*****************************************************************************
535 * The following functions discover the facilities of the trace
536 ****************************************************************************/
538 unsigned ltt_trace_facility_number(LttTrace
*t
)
540 return (unsigned)(t
->facility_number
);
543 LttFacility
*ltt_trace_facility_get(LttTrace
*t
, unsigned i
)
545 return (LttFacility
*)g_ptr_array_index(t
->facilities
, i
);
548 /*****************************************************************************
550 * ltt_trace_facility_find : find facilities in the trace
553 * name : facility name
555 * position : position of the facility in the trace
557 * : the number of facilities
558 ****************************************************************************/
560 unsigned ltt_trace_facility_find(LttTrace
*t
, char *name
, unsigned *position
)
564 for(i
=0;i
<t
->facility_number
;i
++){
565 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, i
);
566 if(strcmp(f
->name
,name
)==0){
568 if(count
==1) *position
= i
;
576 /*****************************************************************************
577 * Functions to discover all the event types in the trace
578 ****************************************************************************/
580 unsigned ltt_trace_eventtype_number(LttTrace
*t
)
585 for(i
=0;i
<t
->facility_number
;i
++){
586 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, i
);
587 count
+= f
->event_number
;
592 LttFacility
* ltt_trace_facility_by_id(LttTrace
* trace
, unsigned id
)
594 LttFacility
* facility
;
596 for(i
=0;i
<trace
->facility_number
;i
++){
597 facility
= (LttFacility
*) g_ptr_array_index(trace
->facilities
,i
);
598 if(id
>= facility
->base_id
&&
599 id
< facility
->base_id
+ facility
->event_number
)
602 if(i
==trace
->facility_number
) return NULL
;
603 else return facility
;
606 LttEventType
*ltt_trace_eventtype_get(LttTrace
*t
, unsigned evId
)
609 f
= ltt_trace_facility_by_id(t
,evId
);
611 return f
->events
[evId
- f
->base_id
];
614 /*****************************************************************************
615 *There is one "per cpu" tracefile for each CPU, numbered from 0 to
616 *the maximum number of CPU in the system. When the number of CPU installed
617 *is less than the maximum, some positions are unused. There are also a
618 *number of "control" tracefiles (facilities, interrupts...).
619 ****************************************************************************/
620 unsigned ltt_trace_control_tracefile_number(LttTrace
*t
)
622 return t
->control_tracefile_number
;
625 unsigned ltt_trace_per_cpu_tracefile_number(LttTrace
*t
)
627 return t
->per_cpu_tracefile_number
;
630 /*****************************************************************************
631 *It is possible to search for the tracefiles by name or by CPU position.
632 *The index within the tracefiles of the same type is returned if found
633 *and a negative value otherwise.
634 ****************************************************************************/
636 int ltt_trace_control_tracefile_find(LttTrace
*t
, char *name
)
638 LttTracefile
* tracefile
;
640 for(i
=0;i
<t
->control_tracefile_number
;i
++){
641 tracefile
= (LttTracefile
*)g_ptr_array_index(t
->control_tracefiles
, i
);
642 if(strcmp(tracefile
->name
, name
)==0)break;
644 if(i
== t
->control_tracefile_number
) return -1;
648 int ltt_trace_per_cpu_tracefile_find(LttTrace
*t
, unsigned i
)
650 LttTracefile
* tracefile
;
652 for(j
=0;j
<t
->per_cpu_tracefile_number
;j
++){
653 tracefile
= (LttTracefile
*)g_ptr_array_index(t
->per_cpu_tracefiles
, j
);
654 name
= atoi(tracefile
->name
);
655 if(name
== (int)i
)break;
657 if(j
== t
->per_cpu_tracefile_number
) return -1;
661 /*****************************************************************************
662 *Get a specific tracefile
663 ****************************************************************************/
665 LttTracefile
*ltt_trace_control_tracefile_get(LttTrace
*t
, unsigned i
)
667 return (LttTracefile
*)g_ptr_array_index(t
->control_tracefiles
, i
);
670 LttTracefile
*ltt_trace_per_cpu_tracefile_get(LttTrace
*t
, unsigned i
)
672 return (LttTracefile
*)g_ptr_array_index(t
->per_cpu_tracefiles
, i
);
675 /*****************************************************************************
676 * Get the start time and end time of the trace
677 ****************************************************************************/
679 void ltt_trace_time_span_get(LttTrace
*t
, LttTime
*start
, LttTime
*end
)
681 LttTime startSmall
, startTmp
, endBig
, endTmp
;
685 for(i
=0;i
<t
->control_tracefile_number
;i
++){
686 tf
= g_ptr_array_index(t
->control_tracefiles
, i
);
688 startTmp
= tf
->a_block_start
->time
;
689 readBlock(tf
,tf
->block_number
);
690 endTmp
= tf
->a_block_end
->time
;
692 startSmall
= startTmp
;
697 if(ltt_time_compare(startSmall
,startTmp
) > 0) startSmall
= startTmp
;
698 if(ltt_time_compare(endBig
,endTmp
) < 0) endBig
= endTmp
;
701 for(i
=0;i
<t
->per_cpu_tracefile_number
;i
++){
702 tf
= g_ptr_array_index(t
->per_cpu_tracefiles
, i
);
704 startTmp
= tf
->a_block_start
->time
;
705 readBlock(tf
,tf
->block_number
);
706 endTmp
= tf
->a_block_end
->time
;
708 startSmall
= startTmp
;
712 if(ltt_time_compare(startSmall
,startTmp
) > 0) startSmall
= startTmp
;
713 if(ltt_time_compare(endBig
,endTmp
) < 0) endBig
= endTmp
;
721 /*****************************************************************************
722 *Get the name of a tracefile
723 ****************************************************************************/
725 char *ltt_tracefile_name(LttTracefile
*tf
)
730 /*****************************************************************************
731 * Get the number of blocks in the tracefile
732 ****************************************************************************/
734 unsigned ltt_tracefile_block_number(LttTracefile
*tf
)
736 return tf
->block_number
;
739 /*****************************************************************************
741 * ltt_tracefile_seek_time: seek to the first event of the trace with time
742 * larger or equal to time
745 * time : criteria of the time
746 ****************************************************************************/
747 void ltt_tracefile_find_time_block(LttTracefile
*t
, LttTime time
,
748 int start_block
, int end_block
)
750 int err
, tmp_block
, s
, e
;
754 err
=readBlock(t
,start_block
);
755 if(err
) g_error("Can not read tracefile: %s\n", t
->name
);
756 if(start_block
== end_block
)return;
758 tailTime
= ltt_time_compare(t
->a_block_end
->time
, time
);
759 if(tailTime
>= 0) return;
761 err
=readBlock(t
,end_block
);
762 if(err
) g_error("Can not read tracefile: %s\n", t
->name
);
763 if(start_block
+1 == end_block
)return;
765 headTime
= ltt_time_compare(t
->a_block_start
->time
, time
);
766 if(headTime
<= 0 ) return;
768 tmp_block
= (end_block
+ start_block
)/2;
769 err
=readBlock(t
,tmp_block
);
770 if(err
) g_error("Can not read tracefile: %s\n", t
->name
);
772 headTime
= ltt_time_compare(t
->a_block_start
->time
, time
);
773 tailTime
= ltt_time_compare(t
->a_block_end
->time
, time
);
774 if(headTime
<= 0 && tailTime
>= 0) return;
780 ltt_tracefile_find_time_block(t
, time
, s
, e
);
788 ltt_tracefile_find_time_block(t
, time
, s
, e
);
793 void ltt_tracefile_backward_find_time_block(LttTracefile
*t
, LttTime time
)
795 int t_time
, h_time
, err
;
796 err
=readBlock(t
,t
->which_block
-1);
797 if(err
) g_error("Can not read tracefile: %s\n", t
->name
);
798 h_time
= ltt_time_compare(t
->a_block_start
->time
, time
);
799 t_time
= ltt_time_compare(t
->a_block_end
->time
, time
);
802 if(t
->which_block
== 1) return;
803 err
=readBlock(t
,t
->which_block
-1);
804 if(err
) g_error("Can not read tracefile: %s\n", t
->name
);
805 tmp
= ltt_time_compare(t
->a_block_end
->time
, time
);
806 if(tmp
== 0) return ltt_tracefile_seek_time(t
, time
);
807 err
=readBlock(t
,t
->which_block
+1);
808 if(err
) g_error("Can not read tracefile: %s\n", t
->name
);
809 }else if(h_time
> 0){
810 ltt_tracefile_find_time_block(t
, time
, 1, t
->which_block
);
811 return ltt_tracefile_seek_time(t
, time
) ;
813 if(t_time
>= 0) return ltt_tracefile_seek_time(t
, time
);
814 err
=readBlock(t
,t
->which_block
+1);
815 if(err
) g_error("Can not read tracefile: %s\n", t
->name
);
819 void ltt_tracefile_seek_time(LttTracefile
*t
, LttTime time
)
823 int headTime
= ltt_time_compare(t
->a_block_start
->time
, time
);
824 int tailTime
= ltt_time_compare(t
->a_block_end
->time
, time
);
827 if(headTime
< 0 && tailTime
> 0){
828 if(ltt_time_compare(t
->a_block_end
->time
, t
->current_event_time
) !=0) {
829 lttTime
= getEventTime(t
);
830 err
= ltt_time_compare(lttTime
, time
);
832 if(t
->which_event
==2 || (&t
->prev_event_time
,&time
)<0){
836 return ltt_tracefile_seek_time(t
, time
);
840 ev
= ltt_tracefile_read(t
);
842 g_print("End of file\n");
845 lttTime
= getEventTime(t
);
846 err
= ltt_time_compare(lttTime
, time
);
850 }else{//we are at the end of the block
852 return ltt_tracefile_seek_time(t
, time
);
854 }else if(headTime
>= 0){
855 if(t
->which_block
== 1){
858 if(ltt_time_compare(t
->prev_block_end_time
, time
) >= 0 ||
859 (t
->prev_block_end_time
.tv_sec
== 0 &&
860 t
->prev_block_end_time
.tv_nsec
== 0 )){
861 ltt_tracefile_backward_find_time_block(t
, time
);
866 }else if(tailTime
< 0){
867 if(t
->which_block
!= t
->block_number
){
868 ltt_tracefile_find_time_block(t
, time
, t
->which_block
+1, t
->block_number
);
869 return ltt_tracefile_seek_time(t
, time
);
871 g_print("End of file\n");
874 }else if(tailTime
== 0){
875 t
->cur_event_pos
= t
->last_event_pos
;
876 t
->current_event_time
= time
;
877 t
->cur_heart_beat_number
= 0;
878 t
->prev_event_time
.tv_sec
= 0;
879 t
->prev_event_time
.tv_nsec
= 0;
884 /*****************************************************************************
885 * Seek to the first event with position equal or larger to ep
886 ****************************************************************************/
888 void ltt_tracefile_seek_position(LttTracefile
*t
, LttEventPosition
*ep
)
890 //if we are at the right place, just return
891 if(t
->which_block
== ep
->block_num
&& t
->which_event
== ep
->event_num
)
894 if(t
->which_block
== ep
->block_num
) updateTracefile(t
);
895 else readBlock(t
,ep
->block_num
);
897 //event offset is availiable
898 if(ep
->old_position
){
899 t
->cur_heart_beat_number
= ep
->heart_beat_number
;
900 t
->cur_event_pos
= t
->buffer
+ ep
->event_offset
;
904 //only block number and event index are availiable
905 while(t
->which_event
< ep
->event_num
) ltt_tracefile_read(t
);
910 /*****************************************************************************
912 * ltt_tracefile_read : read the current event, set the pointer to the next
916 * LttEvent * : an event to be processed
917 ****************************************************************************/
919 LttEvent
*ltt_tracefile_read(LttTracefile
*t
)
921 LttEvent
* lttEvent
= &t
->an_event
;
924 if(t
->cur_event_pos
== t
->buffer
+ t
->block_size
){
925 if(t
->which_block
== t
->block_number
){
928 err
= readBlock(t
, t
->which_block
+ 1);
929 if(err
)g_error("Can not read tracefile");
932 lttEvent
->event_id
= (int)(*(guint16
*)(t
->cur_event_pos
));
933 if(lttEvent
->event_id
== TRACE_TIME_HEARTBEAT
)
934 t
->cur_heart_beat_number
++;
936 t
->prev_event_time
= t
->current_event_time
;
937 // t->current_event_time = getEventTime(t);
939 lttEvent
->time_delta
= *(guint32
*)(t
->cur_event_pos
+ EVENT_ID_SIZE
);
940 lttEvent
->event_time
= t
->current_event_time
;
942 lttEvent
->tracefile
= t
;
943 lttEvent
->data
= t
->cur_event_pos
+ EVENT_HEADER_SIZE
;
944 lttEvent
->which_block
= t
->which_block
;
945 lttEvent
->which_event
= t
->which_event
;
947 //update the fields of the current event and go to the next event
949 if(err
== ERANGE
) g_error("event id is out of range\n");
951 lttEvent
->event_cycle_count
= t
->cur_cycle_count
;
956 /****************************************************************************
958 * readFile : wrap function to read from a file
960 * fd : file descriptor
961 * buf : buf to contain the content
962 * size : number of bytes to be read
963 * mesg : message to be printed if some thing goes wrong
966 * EIO : can not read from the file
967 ****************************************************************************/
969 int readFile(int fd
, void * buf
, size_t size
, char * mesg
)
972 nbBytes
= read(fd
, buf
, size
);
980 /****************************************************************************
982 * readBlock : read a block from the file
984 * lttdes : ltt trace file
985 * whichBlock : the block which will be read
988 * EINVAL : lseek fail
989 * EIO : can not read from the file
990 ****************************************************************************/
992 int readBlock(LttTracefile
* tf
, int whichBlock
)
997 if(whichBlock
- tf
->which_block
== 1 && tf
->which_block
!= 0){
998 tf
->prev_block_end_time
= tf
->a_block_end
->time
;
999 tf
->prev_event_time
= tf
->a_block_end
->time
;
1001 tf
->prev_block_end_time
.tv_sec
= 0;
1002 tf
->prev_block_end_time
.tv_nsec
= 0;
1003 tf
->prev_event_time
.tv_sec
= 0;
1004 tf
->prev_event_time
.tv_nsec
= 0;
1007 nbBytes
=lseek(tf
->fd
,(off_t
)((whichBlock
-1)*tf
->block_size
), SEEK_SET
);
1008 if(nbBytes
== -1) return EINVAL
;
1010 if(readFile(tf
->fd
,tf
->buffer
,tf
->block_size
,"Unable to read a block"))
1013 tf
->a_block_start
=(BlockStart
*) (tf
->buffer
+ EVENT_HEADER_SIZE
);
1014 lostSize
= *(guint32
*)(tf
->buffer
+ tf
->block_size
- sizeof(guint32
));
1015 tf
->a_block_end
=(BlockEnd
*)(tf
->buffer
+ tf
->block_size
-
1016 lostSize
+ EVENT_HEADER_SIZE
);
1017 tf
->last_event_pos
= tf
->buffer
+ tf
->block_size
- lostSize
;
1019 tf
->which_block
= whichBlock
;
1020 tf
->which_event
= 1;
1021 tf
->cur_event_pos
= tf
->buffer
;//the beginning of the block, block start ev
1022 tf
->cur_heart_beat_number
= 0;
1024 getCyclePerNsec(tf
);
1026 tf
->current_event_time
= getEventTime(tf
);
1031 /*****************************************************************************
1033 * updateTracefile : reinitialize the info of the block which is already
1037 ****************************************************************************/
1039 void updateTracefile(LttTracefile
* tf
)
1041 tf
->which_event
= 1;
1042 tf
->cur_event_pos
= tf
->buffer
;
1043 tf
->current_event_time
= getEventTime(tf
);
1044 tf
->cur_heart_beat_number
= 0;
1046 tf
->prev_event_time
.tv_sec
= 0;
1047 tf
->prev_event_time
.tv_nsec
= 0;
1050 /*****************************************************************************
1052 * skipEvent : go to the next event, update the fields of the current event
1057 * ERANGE : event id is out of range
1058 ****************************************************************************/
1060 int skipEvent(LttTracefile
* t
)
1067 evId
= (int)(*(guint16
*)(t
->cur_event_pos
));
1068 evData
= t
->cur_event_pos
+ EVENT_HEADER_SIZE
;
1070 evT
= ltt_trace_eventtype_get(t
->trace
,(unsigned)evId
);
1072 if(evT
) rootFld
= evT
->root_field
;
1076 //event has string/sequence or the last event is not the same event
1077 if((evT
->latest_block
!=t
->which_block
|| evT
->latest_event
!=t
->which_event
)
1078 && rootFld
->field_fixed
== 0){
1079 setFieldsOffset(t
, evT
, evData
, t
->trace
);
1081 t
->cur_event_pos
+= EVENT_HEADER_SIZE
+ rootFld
->field_size
;
1082 }else t
->cur_event_pos
+= EVENT_HEADER_SIZE
;
1084 evT
->latest_block
= t
->which_block
;
1085 evT
->latest_event
= t
->which_event
;
1087 //the next event is in the next block
1088 if(evId
== TRACE_BLOCK_END
){
1089 t
->cur_event_pos
= t
->buffer
+ t
->block_size
;
1092 t
->current_event_time
= getEventTime(t
);
1098 /*****************************************************************************
1100 * getCyclePerNsec : calculate cycles per nsec for current block
1103 ****************************************************************************/
1105 void getCyclePerNsec(LttTracefile
* t
)
1107 LttTime lBufTotalTime
; /* Total time for this buffer */
1108 LttCycleCount lBufTotalNSec
; /* Total time for this buffer in nsecs */
1109 LttCycleCount lBufTotalCycle
;/* Total cycles for this buffer */
1111 /* Calculate the total time for this buffer */
1112 lBufTotalTime
= ltt_time_sub(t
->a_block_end
->time
, t
->a_block_start
->time
);
1114 /* Calculate the total cycles for this bufffer */
1115 lBufTotalCycle
= t
->a_block_end
->cycle_count
;
1116 lBufTotalCycle
-= t
->a_block_start
->cycle_count
;
1118 /* Convert the total time to nsecs */
1119 lBufTotalNSec
= lBufTotalTime
.tv_sec
;
1120 lBufTotalNSec
*= NANOSECONDS_PER_SECOND
;
1121 lBufTotalNSec
+= lBufTotalTime
.tv_nsec
;
1123 t
->cycle_per_nsec
= (double)lBufTotalCycle
/ (double)lBufTotalNSec
;
1126 /****************************************************************************
1128 * getEventTime : obtain the time of an event
1132 * LttTime : the time of the event
1133 ****************************************************************************/
1135 LttTime
getEventTime(LttTracefile
* tf
)
1138 LttCycleCount cycle_count
; // cycle count for the current event
1139 LttCycleCount lEventTotalCycle
; // Total cycles from start for event
1140 double lEventNSec
; // Total usecs from start for event
1141 LttTime lTimeOffset
; // Time offset in struct LttTime
1143 gint64 nanoSec
, tmpCycleCount
= (((guint64
)1)<<32);
1144 static LttCycleCount preCycleCount
= 0;
1145 static int count
= 0;
1147 evId
= *(guint16
*)tf
->cur_event_pos
;
1148 if(evId
== TRACE_BLOCK_START
){
1151 tf
->cur_cycle_count
= tf
->a_block_start
->cycle_count
;
1152 return tf
->a_block_start
->time
;
1153 }else if(evId
== TRACE_BLOCK_END
){
1156 tf
->cur_cycle_count
= tf
->a_block_end
->cycle_count
;
1157 return tf
->a_block_end
->time
;
1160 // Calculate total time in cycles from start of buffer for this event
1161 cycle_count
= (LttCycleCount
)*(guint32
*)(tf
->cur_event_pos
+ EVENT_ID_SIZE
);
1163 if(cycle_count
< preCycleCount
)count
++;
1164 preCycleCount
= cycle_count
;
1165 cycle_count
+= tmpCycleCount
* count
;
1167 if(tf
->cur_heart_beat_number
> count
)
1168 cycle_count
+= tmpCycleCount
* (tf
->cur_heart_beat_number
- count
);
1170 tf
->cur_cycle_count
= cycle_count
;
1172 lEventTotalCycle
= cycle_count
;
1173 lEventTotalCycle
-= tf
->a_block_start
->cycle_count
;
1175 // Convert it to nsecs
1176 lEventNSec
= lEventTotalCycle
/ tf
->cycle_per_nsec
;
1177 nanoSec
= lEventNSec
;
1179 // Determine offset in struct LttTime
1180 lTimeOffset
.tv_nsec
= nanoSec
% NANOSECONDS_PER_SECOND
;
1181 lTimeOffset
.tv_sec
= nanoSec
/ NANOSECONDS_PER_SECOND
;
1183 time
= ltt_time_add(tf
->a_block_start
->time
, lTimeOffset
);
1188 /*****************************************************************************
1190 * setFieldsOffset : set offset of the fields
1192 * tracefile : opened trace file
1193 * evT : the event type
1194 * evD : event data, it may be NULL
1195 ****************************************************************************/
1197 void setFieldsOffset(LttTracefile
*tf
,LttEventType
*evT
,void *evD
,LttTrace
* t
)
1199 LttField
* rootFld
= evT
->root_field
;
1200 // rootFld->base_address = evD;
1203 rootFld
->field_size
= getFieldtypeSize(tf
, evT
, 0,0,rootFld
, evD
,t
);
1206 /*****************************************************************************
1208 * getFieldtypeSize: get the size of the field type (primitive type)
1210 * tracefile : opened trace file
1212 * offsetRoot : offset from the root
1213 * offsetParent : offset from the parrent
1215 * evD : event data, it may be NULL
1217 * int : size of the field
1218 ****************************************************************************/
1220 int getFieldtypeSize(LttTracefile
* t
, LttEventType
* evT
, int offsetRoot
,
1221 int offsetParent
, LttField
* fld
, void *evD
, LttTrace
*trace
)
1223 int size
, size1
, element_number
, i
, offset1
, offset2
;
1224 LttType
* type
= fld
->field_type
;
1227 if(evT
->latest_block
==t
->which_block
&& evT
->latest_event
==t
->which_event
){
1228 return fld
->field_size
;
1232 if(fld
->field_fixed
== 1){
1233 if(fld
== evT
->root_field
) return fld
->field_size
;
1236 if(type
->type_class
!= LTT_STRUCT
&& type
->type_class
!= LTT_ARRAY
&&
1237 type
->type_class
!= LTT_SEQUENCE
&& type
->type_class
!= LTT_STRING
){
1238 if(fld
->field_fixed
== -1){
1239 size
= (int) ltt_type_size(trace
, type
);
1240 fld
->field_fixed
= 1;
1241 }else size
= fld
->field_size
;
1243 }else if(type
->type_class
== LTT_ARRAY
){
1244 element_number
= (int) type
->element_number
;
1245 if(fld
->field_fixed
== -1){
1246 size
= getFieldtypeSize(t
, evT
, offsetRoot
,0,fld
->child
[0], NULL
, trace
);
1247 if(size
== 0){ //has string or sequence
1248 fld
->field_fixed
= 0;
1250 fld
->field_fixed
= 1;
1251 size
*= element_number
;
1253 }else if(fld
->field_fixed
== 0){// has string or sequence
1255 for(i
=0;i
<element_number
;i
++){
1256 size
+= getFieldtypeSize(t
, evT
, offsetRoot
+size
,size
,
1257 fld
->child
[0], evD
+size
, trace
);
1259 }else size
= fld
->field_size
;
1261 }else if(type
->type_class
== LTT_SEQUENCE
){
1262 size1
= (int) ltt_type_size(trace
, type
);
1263 if(fld
->field_fixed
== -1){
1264 fld
->sequ_number_size
= size1
;
1265 fld
->field_fixed
= 0;
1266 size
= getFieldtypeSize(t
, evT
, offsetRoot
,0,fld
->child
[0], NULL
, trace
);
1267 fld
->element_size
= size
;
1269 element_number
= getIntNumber(size1
,evD
);
1270 type
->element_number
= element_number
;
1271 if(fld
->element_size
> 0){
1272 size
= element_number
* fld
->element_size
;
1273 }else{//sequence has string or sequence
1275 for(i
=0;i
<element_number
;i
++){
1276 size
+= getFieldtypeSize(t
, evT
, offsetRoot
+size
+size1
,size
+size1
,
1277 fld
->child
[0], evD
+size
+size1
, trace
);
1283 }else if(type
->type_class
== LTT_STRING
){
1285 if(fld
->field_fixed
== -1){
1286 fld
->field_fixed
= 0;
1288 size
= strlen((char*)evD
) + 1; //include end : '\0'
1291 }else if(type
->type_class
== LTT_STRUCT
){
1292 element_number
= (int) type
->element_number
;
1294 if(fld
->field_fixed
== -1){
1295 offset1
= offsetRoot
;
1297 for(i
=0;i
<element_number
;i
++){
1298 size1
=getFieldtypeSize(t
, evT
,offset1
,offset2
, fld
->child
[i
], NULL
, trace
);
1299 if(size1
> 0 && size
>= 0){
1301 if(offset1
>= 0) offset1
+= size1
;
1310 fld
->field_fixed
= 0;
1312 }else fld
->field_fixed
= 1;
1313 }else if(fld
->field_fixed
== 0){
1314 offset1
= offsetRoot
;
1316 for(i
=0;i
<element_number
;i
++){
1317 size
=getFieldtypeSize(t
,evT
,offset1
,offset2
,fld
->child
[i
],evD
+offset2
, trace
);
1322 }else size
= fld
->field_size
;
1325 fld
->offset_root
= offsetRoot
;
1326 fld
->offset_parent
= offsetParent
;
1328 fld
->fixed_root
= (offsetRoot
==-1) ? 0 : 1;
1329 fld
->fixed_parent
= (offsetParent
==-1) ? 0 : 1;
1331 fld
->field_size
= size
;
1337 /*****************************************************************************
1339 * getIntNumber : get an integer number
1341 * size : the size of the integer
1342 * evD : the event data
1345 ****************************************************************************/
1347 int getIntNumber(int size
, void *evD
)
1350 if(size
== 1) i
= *(gint8
*)evD
;
1351 else if(size
== 2) i
= *(gint16
*)evD
;
1352 else if(size
== 4) i
= *(gint32
*)evD
;
1353 else if(size
== 8) i
= *(gint64
*)evD
;
1358 /*****************************************************************************
1360 * getDataEndianType : get the data type size and endian type of the local
1363 * size : size of data type
1364 * endian : endian type, little or big
1365 ****************************************************************************/
1367 void getDataEndianType(LttArchSize
* size
, LttArchEndian
* endian
)
1371 int sizeInt
=sizeof(int), sizeLong
=sizeof(long), sizePointer
=sizeof(void *);
1373 if(c
== 1) *endian
= LTT_LITTLE_ENDIAN
;
1374 else *endian
= LTT_BIG_ENDIAN
;
1376 if(sizeInt
== 2 && sizeLong
== 4 && sizePointer
== 4)
1378 else if(sizeInt
== 4 && sizeLong
== 4 && sizePointer
== 4)
1380 else if(sizeInt
== 4 && sizeLong
== 8 && sizePointer
== 8)
1382 else if(sizeInt
== 8 && sizeLong
== 8 && sizePointer
== 8)
1384 else *size
= LTT_UNKNOWN
;