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 /******************************************************************************
462 * When we copy a trace, we want all the opening actions to happen again :
463 * the trace will be reopened and totally independant from the original.
464 * That's why we call ltt_trace_open.
465 *****************************************************************************/
466 LttTrace
*ltt_trace_copy(LttTrace
*self
)
468 return ltt_trace_open(self
->pathname
);
471 void ltt_trace_close(LttTrace
*t
)
479 //free system_description
480 g_free(t
->system_description
->description
);
481 g_free(t
->system_description
->node_name
);
482 g_free(t
->system_description
->domain_name
);
483 g_free(t
->system_description
->kernel_name
);
484 g_free(t
->system_description
->kernel_release
);
485 g_free(t
->system_description
->kernel_version
);
486 g_free(t
->system_description
->machine
);
487 g_free(t
->system_description
->processor
);
488 g_free(t
->system_description
->hardware_platform
);
489 g_free(t
->system_description
->operating_system
);
490 g_free(t
->system_description
);
492 //free control_tracefiles
493 for(i
=0;i
<t
->control_tracefile_number
;i
++){
494 tf
= (LttTracefile
*)g_ptr_array_index(t
->control_tracefiles
,i
);
495 ltt_tracefile_close(tf
);
497 g_ptr_array_free(t
->control_tracefiles
, TRUE
);
499 //free per_cpu_tracefiles
500 for(i
=0;i
<t
->per_cpu_tracefile_number
;i
++){
501 tf
= (LttTracefile
*)g_ptr_array_index(t
->per_cpu_tracefiles
,i
);
502 ltt_tracefile_close(tf
);
504 g_ptr_array_free(t
->per_cpu_tracefiles
, TRUE
);
507 for(i
=0;i
<t
->facility_number
;i
++){
508 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
,i
);
509 ltt_facility_close(f
);
511 g_ptr_array_free(t
->facilities
, TRUE
);
519 /*****************************************************************************
520 *Get the system description of the trace
521 ****************************************************************************/
523 LttSystemDescription
*ltt_trace_system_description(LttTrace
*t
)
525 return t
->system_description
;
528 /*****************************************************************************
529 * The following functions discover the facilities of the trace
530 ****************************************************************************/
532 unsigned ltt_trace_facility_number(LttTrace
*t
)
534 return (unsigned)(t
->facility_number
);
537 LttFacility
*ltt_trace_facility_get(LttTrace
*t
, unsigned i
)
539 return (LttFacility
*)g_ptr_array_index(t
->facilities
, i
);
542 /*****************************************************************************
544 * ltt_trace_facility_find : find facilities in the trace
547 * name : facility name
549 * position : position of the facility in the trace
551 * : the number of facilities
552 ****************************************************************************/
554 unsigned ltt_trace_facility_find(LttTrace
*t
, char *name
, unsigned *position
)
558 for(i
=0;i
<t
->facility_number
;i
++){
559 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, i
);
560 if(strcmp(f
->name
,name
)==0){
562 if(count
==1) *position
= i
;
570 /*****************************************************************************
571 * Functions to discover all the event types in the trace
572 ****************************************************************************/
574 unsigned ltt_trace_eventtype_number(LttTrace
*t
)
579 for(i
=0;i
<t
->facility_number
;i
++){
580 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, i
);
581 count
+= f
->event_number
;
586 LttFacility
* ltt_trace_facility_by_id(LttTrace
* trace
, unsigned id
)
588 LttFacility
* facility
;
590 for(i
=0;i
<trace
->facility_number
;i
++){
591 facility
= (LttFacility
*) g_ptr_array_index(trace
->facilities
,i
);
592 if(id
>= facility
->base_id
&&
593 id
< facility
->base_id
+ facility
->event_number
)
596 if(i
==trace
->facility_number
) return NULL
;
597 else return facility
;
600 LttEventType
*ltt_trace_eventtype_get(LttTrace
*t
, unsigned evId
)
603 f
= ltt_trace_facility_by_id(t
,evId
);
605 return f
->events
[evId
- f
->base_id
];
608 /*****************************************************************************
609 *There is one "per cpu" tracefile for each CPU, numbered from 0 to
610 *the maximum number of CPU in the system. When the number of CPU installed
611 *is less than the maximum, some positions are unused. There are also a
612 *number of "control" tracefiles (facilities, interrupts...).
613 ****************************************************************************/
614 unsigned ltt_trace_control_tracefile_number(LttTrace
*t
)
616 return t
->control_tracefile_number
;
619 unsigned ltt_trace_per_cpu_tracefile_number(LttTrace
*t
)
621 return t
->per_cpu_tracefile_number
;
624 /*****************************************************************************
625 *It is possible to search for the tracefiles by name or by CPU position.
626 *The index within the tracefiles of the same type is returned if found
627 *and a negative value otherwise.
628 ****************************************************************************/
630 int ltt_trace_control_tracefile_find(LttTrace
*t
, char *name
)
632 LttTracefile
* tracefile
;
634 for(i
=0;i
<t
->control_tracefile_number
;i
++){
635 tracefile
= (LttTracefile
*)g_ptr_array_index(t
->control_tracefiles
, i
);
636 if(strcmp(tracefile
->name
, name
)==0)break;
638 if(i
== t
->control_tracefile_number
) return -1;
642 int ltt_trace_per_cpu_tracefile_find(LttTrace
*t
, unsigned i
)
644 LttTracefile
* tracefile
;
646 for(j
=0;j
<t
->per_cpu_tracefile_number
;j
++){
647 tracefile
= (LttTracefile
*)g_ptr_array_index(t
->per_cpu_tracefiles
, j
);
648 name
= atoi(tracefile
->name
);
649 if(name
== (int)i
)break;
651 if(j
== t
->per_cpu_tracefile_number
) return -1;
655 /*****************************************************************************
656 *Get a specific tracefile
657 ****************************************************************************/
659 LttTracefile
*ltt_trace_control_tracefile_get(LttTrace
*t
, unsigned i
)
661 return (LttTracefile
*)g_ptr_array_index(t
->control_tracefiles
, i
);
664 LttTracefile
*ltt_trace_per_cpu_tracefile_get(LttTrace
*t
, unsigned i
)
666 return (LttTracefile
*)g_ptr_array_index(t
->per_cpu_tracefiles
, i
);
669 /*****************************************************************************
670 * Get the start time and end time of the trace
671 ****************************************************************************/
673 void ltt_trace_time_span_get(LttTrace
*t
, LttTime
*start
, LttTime
*end
)
675 LttTime startSmall
, startTmp
, endBig
, endTmp
;
679 for(i
=0;i
<t
->control_tracefile_number
;i
++){
680 tf
= g_ptr_array_index(t
->control_tracefiles
, i
);
682 startTmp
= tf
->a_block_start
->time
;
683 readBlock(tf
,tf
->block_number
);
684 endTmp
= tf
->a_block_end
->time
;
686 startSmall
= startTmp
;
691 if(ltt_time_compare(startSmall
,startTmp
) > 0) startSmall
= startTmp
;
692 if(ltt_time_compare(endBig
,endTmp
) < 0) endBig
= endTmp
;
695 for(i
=0;i
<t
->per_cpu_tracefile_number
;i
++){
696 tf
= g_ptr_array_index(t
->per_cpu_tracefiles
, i
);
698 startTmp
= tf
->a_block_start
->time
;
699 readBlock(tf
,tf
->block_number
);
700 endTmp
= tf
->a_block_end
->time
;
702 startSmall
= startTmp
;
706 if(ltt_time_compare(startSmall
,startTmp
) > 0) startSmall
= startTmp
;
707 if(ltt_time_compare(endBig
,endTmp
) < 0) endBig
= endTmp
;
715 /*****************************************************************************
716 *Get the name of a tracefile
717 ****************************************************************************/
719 char *ltt_tracefile_name(LttTracefile
*tf
)
724 /*****************************************************************************
725 * Get the number of blocks in the tracefile
726 ****************************************************************************/
728 unsigned ltt_tracefile_block_number(LttTracefile
*tf
)
730 return tf
->block_number
;
733 /*****************************************************************************
735 * ltt_tracefile_seek_time: seek to the first event of the trace with time
736 * larger or equal to time
739 * time : criteria of the time
740 ****************************************************************************/
741 void ltt_tracefile_find_time_block(LttTracefile
*t
, LttTime time
,
742 int start_block
, int end_block
)
744 int err
, tmp_block
, s
, e
;
748 err
=readBlock(t
,start_block
);
749 if(err
) g_error("Can not read tracefile: %s\n", t
->name
);
750 if(start_block
== end_block
)return;
752 tailTime
= ltt_time_compare(t
->a_block_end
->time
, time
);
753 if(tailTime
>= 0) return;
755 err
=readBlock(t
,end_block
);
756 if(err
) g_error("Can not read tracefile: %s\n", t
->name
);
757 if(start_block
+1 == end_block
)return;
759 headTime
= ltt_time_compare(t
->a_block_start
->time
, time
);
760 if(headTime
<= 0 ) return;
762 tmp_block
= (end_block
+ start_block
)/2;
763 err
=readBlock(t
,tmp_block
);
764 if(err
) g_error("Can not read tracefile: %s\n", t
->name
);
766 headTime
= ltt_time_compare(t
->a_block_start
->time
, time
);
767 tailTime
= ltt_time_compare(t
->a_block_end
->time
, time
);
768 if(headTime
<= 0 && tailTime
>= 0) return;
774 ltt_tracefile_find_time_block(t
, time
, s
, e
);
782 ltt_tracefile_find_time_block(t
, time
, s
, e
);
787 void ltt_tracefile_backward_find_time_block(LttTracefile
*t
, LttTime time
)
789 int t_time
, h_time
, err
;
790 err
=readBlock(t
,t
->which_block
-1);
791 if(err
) g_error("Can not read tracefile: %s\n", t
->name
);
792 h_time
= ltt_time_compare(t
->a_block_start
->time
, time
);
793 t_time
= ltt_time_compare(t
->a_block_end
->time
, time
);
796 if(t
->which_block
== 1) return;
797 err
=readBlock(t
,t
->which_block
-1);
798 if(err
) g_error("Can not read tracefile: %s\n", t
->name
);
799 tmp
= ltt_time_compare(t
->a_block_end
->time
, time
);
800 if(tmp
== 0) return ltt_tracefile_seek_time(t
, time
);
801 err
=readBlock(t
,t
->which_block
+1);
802 if(err
) g_error("Can not read tracefile: %s\n", t
->name
);
803 }else if(h_time
> 0){
804 ltt_tracefile_find_time_block(t
, time
, 1, t
->which_block
);
805 return ltt_tracefile_seek_time(t
, time
) ;
807 if(t_time
>= 0) return ltt_tracefile_seek_time(t
, time
);
808 err
=readBlock(t
,t
->which_block
+1);
809 if(err
) g_error("Can not read tracefile: %s\n", t
->name
);
813 void ltt_tracefile_seek_time(LttTracefile
*t
, LttTime time
)
817 int headTime
= ltt_time_compare(t
->a_block_start
->time
, time
);
818 int tailTime
= ltt_time_compare(t
->a_block_end
->time
, time
);
821 if(headTime
< 0 && tailTime
> 0){
822 if(ltt_time_compare(t
->a_block_end
->time
, t
->current_event_time
) !=0) {
823 lttTime
= getEventTime(t
);
824 err
= ltt_time_compare(lttTime
, time
);
826 if(t
->which_event
==2 || (&t
->prev_event_time
,&time
)<0){
830 return ltt_tracefile_seek_time(t
, time
);
834 ev
= ltt_tracefile_read(t
);
836 g_print("End of file\n");
839 lttTime
= getEventTime(t
);
840 err
= ltt_time_compare(lttTime
, time
);
844 }else{//we are at the end of the block
846 return ltt_tracefile_seek_time(t
, time
);
848 }else if(headTime
>= 0){
849 if(t
->which_block
== 1){
852 if(ltt_time_compare(t
->prev_block_end_time
, time
) >= 0 ||
853 (t
->prev_block_end_time
.tv_sec
== 0 &&
854 t
->prev_block_end_time
.tv_nsec
== 0 )){
855 ltt_tracefile_backward_find_time_block(t
, time
);
860 }else if(tailTime
< 0){
861 if(t
->which_block
!= t
->block_number
){
862 ltt_tracefile_find_time_block(t
, time
, t
->which_block
+1, t
->block_number
);
863 return ltt_tracefile_seek_time(t
, time
);
865 g_print("End of file\n");
868 }else if(tailTime
== 0){
869 t
->cur_event_pos
= t
->last_event_pos
;
870 t
->current_event_time
= time
;
871 t
->cur_heart_beat_number
= 0;
872 t
->prev_event_time
.tv_sec
= 0;
873 t
->prev_event_time
.tv_nsec
= 0;
878 /*****************************************************************************
879 * Seek to the first event with position equal or larger to ep
880 ****************************************************************************/
882 void ltt_tracefile_seek_position(LttTracefile
*t
, LttEventPosition
*ep
)
884 //if we are at the right place, just return
885 if(t
->which_block
== ep
->block_num
&& t
->which_event
== ep
->event_num
)
888 if(t
->which_block
== ep
->block_num
) updateTracefile(t
);
889 else readBlock(t
,ep
->block_num
);
891 //event offset is availiable
892 if(ep
->old_position
){
893 t
->cur_heart_beat_number
= ep
->heart_beat_number
;
894 t
->cur_event_pos
= t
->buffer
+ ep
->event_offset
;
898 //only block number and event index are availiable
899 while(t
->which_event
< ep
->event_num
) ltt_tracefile_read(t
);
904 /*****************************************************************************
906 * ltt_tracefile_read : read the current event, set the pointer to the next
910 * LttEvent * : an event to be processed
911 ****************************************************************************/
913 LttEvent
*ltt_tracefile_read(LttTracefile
*t
)
915 LttEvent
* lttEvent
= &t
->an_event
;
918 if(t
->cur_event_pos
== t
->buffer
+ t
->block_size
){
919 if(t
->which_block
== t
->block_number
){
922 err
= readBlock(t
, t
->which_block
+ 1);
923 if(err
)g_error("Can not read tracefile");
926 lttEvent
->event_id
= (int)(*(guint16
*)(t
->cur_event_pos
));
927 if(lttEvent
->event_id
== TRACE_TIME_HEARTBEAT
)
928 t
->cur_heart_beat_number
++;
930 t
->prev_event_time
= t
->current_event_time
;
931 // t->current_event_time = getEventTime(t);
933 lttEvent
->time_delta
= *(guint32
*)(t
->cur_event_pos
+ EVENT_ID_SIZE
);
934 lttEvent
->event_time
= t
->current_event_time
;
936 lttEvent
->tracefile
= t
;
937 lttEvent
->data
= t
->cur_event_pos
+ EVENT_HEADER_SIZE
;
938 lttEvent
->which_block
= t
->which_block
;
939 lttEvent
->which_event
= t
->which_event
;
941 //update the fields of the current event and go to the next event
943 if(err
== ERANGE
) g_error("event id is out of range\n");
945 lttEvent
->event_cycle_count
= t
->cur_cycle_count
;
950 /****************************************************************************
952 * readFile : wrap function to read from a file
954 * fd : file descriptor
955 * buf : buf to contain the content
956 * size : number of bytes to be read
957 * mesg : message to be printed if some thing goes wrong
960 * EIO : can not read from the file
961 ****************************************************************************/
963 int readFile(int fd
, void * buf
, size_t size
, char * mesg
)
966 nbBytes
= read(fd
, buf
, size
);
974 /****************************************************************************
976 * readBlock : read a block from the file
978 * lttdes : ltt trace file
979 * whichBlock : the block which will be read
982 * EINVAL : lseek fail
983 * EIO : can not read from the file
984 ****************************************************************************/
986 int readBlock(LttTracefile
* tf
, int whichBlock
)
991 if(whichBlock
- tf
->which_block
== 1 && tf
->which_block
!= 0){
992 tf
->prev_block_end_time
= tf
->a_block_end
->time
;
993 tf
->prev_event_time
= tf
->a_block_end
->time
;
995 tf
->prev_block_end_time
.tv_sec
= 0;
996 tf
->prev_block_end_time
.tv_nsec
= 0;
997 tf
->prev_event_time
.tv_sec
= 0;
998 tf
->prev_event_time
.tv_nsec
= 0;
1001 nbBytes
=lseek(tf
->fd
,(off_t
)((whichBlock
-1)*tf
->block_size
), SEEK_SET
);
1002 if(nbBytes
== -1) return EINVAL
;
1004 if(readFile(tf
->fd
,tf
->buffer
,tf
->block_size
,"Unable to read a block"))
1007 tf
->a_block_start
=(BlockStart
*) (tf
->buffer
+ EVENT_HEADER_SIZE
);
1008 lostSize
= *(guint32
*)(tf
->buffer
+ tf
->block_size
- sizeof(guint32
));
1009 tf
->a_block_end
=(BlockEnd
*)(tf
->buffer
+ tf
->block_size
-
1010 lostSize
+ EVENT_HEADER_SIZE
);
1011 tf
->last_event_pos
= tf
->buffer
+ tf
->block_size
- lostSize
;
1013 tf
->which_block
= whichBlock
;
1014 tf
->which_event
= 1;
1015 tf
->cur_event_pos
= tf
->buffer
;//the beginning of the block, block start ev
1016 tf
->cur_heart_beat_number
= 0;
1018 getCyclePerNsec(tf
);
1020 tf
->current_event_time
= getEventTime(tf
);
1025 /*****************************************************************************
1027 * updateTracefile : reinitialize the info of the block which is already
1031 ****************************************************************************/
1033 void updateTracefile(LttTracefile
* tf
)
1035 tf
->which_event
= 1;
1036 tf
->cur_event_pos
= tf
->buffer
;
1037 tf
->current_event_time
= getEventTime(tf
);
1038 tf
->cur_heart_beat_number
= 0;
1040 tf
->prev_event_time
.tv_sec
= 0;
1041 tf
->prev_event_time
.tv_nsec
= 0;
1044 /*****************************************************************************
1046 * skipEvent : go to the next event, update the fields of the current event
1051 * ERANGE : event id is out of range
1052 ****************************************************************************/
1054 int skipEvent(LttTracefile
* t
)
1061 evId
= (int)(*(guint16
*)(t
->cur_event_pos
));
1062 evData
= t
->cur_event_pos
+ EVENT_HEADER_SIZE
;
1064 evT
= ltt_trace_eventtype_get(t
->trace
,(unsigned)evId
);
1066 if(evT
) rootFld
= evT
->root_field
;
1070 //event has string/sequence or the last event is not the same event
1071 if((evT
->latest_block
!=t
->which_block
|| evT
->latest_event
!=t
->which_event
)
1072 && rootFld
->field_fixed
== 0){
1073 setFieldsOffset(t
, evT
, evData
, t
->trace
);
1075 t
->cur_event_pos
+= EVENT_HEADER_SIZE
+ rootFld
->field_size
;
1076 }else t
->cur_event_pos
+= EVENT_HEADER_SIZE
;
1078 evT
->latest_block
= t
->which_block
;
1079 evT
->latest_event
= t
->which_event
;
1081 //the next event is in the next block
1082 if(evId
== TRACE_BLOCK_END
){
1083 t
->cur_event_pos
= t
->buffer
+ t
->block_size
;
1086 t
->current_event_time
= getEventTime(t
);
1092 /*****************************************************************************
1094 * getCyclePerNsec : calculate cycles per nsec for current block
1097 ****************************************************************************/
1099 void getCyclePerNsec(LttTracefile
* t
)
1101 LttTime lBufTotalTime
; /* Total time for this buffer */
1102 LttCycleCount lBufTotalNSec
; /* Total time for this buffer in nsecs */
1103 LttCycleCount lBufTotalCycle
;/* Total cycles for this buffer */
1105 /* Calculate the total time for this buffer */
1106 lBufTotalTime
= ltt_time_sub(t
->a_block_end
->time
, t
->a_block_start
->time
);
1108 /* Calculate the total cycles for this bufffer */
1109 lBufTotalCycle
= t
->a_block_end
->cycle_count
;
1110 lBufTotalCycle
-= t
->a_block_start
->cycle_count
;
1112 /* Convert the total time to nsecs */
1113 lBufTotalNSec
= lBufTotalTime
.tv_sec
;
1114 lBufTotalNSec
*= NANOSECONDS_PER_SECOND
;
1115 lBufTotalNSec
+= lBufTotalTime
.tv_nsec
;
1117 t
->cycle_per_nsec
= (double)lBufTotalCycle
/ (double)lBufTotalNSec
;
1120 /****************************************************************************
1122 * getEventTime : obtain the time of an event
1126 * LttTime : the time of the event
1127 ****************************************************************************/
1129 LttTime
getEventTime(LttTracefile
* tf
)
1132 LttCycleCount cycle_count
; // cycle count for the current event
1133 LttCycleCount lEventTotalCycle
; // Total cycles from start for event
1134 double lEventNSec
; // Total usecs from start for event
1135 LttTime lTimeOffset
; // Time offset in struct LttTime
1137 gint64 nanoSec
, tmpCycleCount
= (((guint64
)1)<<32);
1138 static LttCycleCount preCycleCount
= 0;
1139 static int count
= 0;
1141 evId
= *(guint16
*)tf
->cur_event_pos
;
1142 if(evId
== TRACE_BLOCK_START
){
1145 tf
->cur_cycle_count
= tf
->a_block_start
->cycle_count
;
1146 return tf
->a_block_start
->time
;
1147 }else if(evId
== TRACE_BLOCK_END
){
1150 tf
->cur_cycle_count
= tf
->a_block_end
->cycle_count
;
1151 return tf
->a_block_end
->time
;
1154 // Calculate total time in cycles from start of buffer for this event
1155 cycle_count
= (LttCycleCount
)*(guint32
*)(tf
->cur_event_pos
+ EVENT_ID_SIZE
);
1157 if(cycle_count
< preCycleCount
)count
++;
1158 preCycleCount
= cycle_count
;
1159 cycle_count
+= tmpCycleCount
* count
;
1161 if(tf
->cur_heart_beat_number
> count
)
1162 cycle_count
+= tmpCycleCount
* (tf
->cur_heart_beat_number
- count
);
1164 tf
->cur_cycle_count
= cycle_count
;
1166 lEventTotalCycle
= cycle_count
;
1167 lEventTotalCycle
-= tf
->a_block_start
->cycle_count
;
1169 // Convert it to nsecs
1170 lEventNSec
= lEventTotalCycle
/ tf
->cycle_per_nsec
;
1171 nanoSec
= lEventNSec
;
1173 // Determine offset in struct LttTime
1174 lTimeOffset
.tv_nsec
= nanoSec
% NANOSECONDS_PER_SECOND
;
1175 lTimeOffset
.tv_sec
= nanoSec
/ NANOSECONDS_PER_SECOND
;
1177 time
= ltt_time_add(tf
->a_block_start
->time
, lTimeOffset
);
1182 /*****************************************************************************
1184 * setFieldsOffset : set offset of the fields
1186 * tracefile : opened trace file
1187 * evT : the event type
1188 * evD : event data, it may be NULL
1189 ****************************************************************************/
1191 void setFieldsOffset(LttTracefile
*tf
,LttEventType
*evT
,void *evD
,LttTrace
* t
)
1193 LttField
* rootFld
= evT
->root_field
;
1194 // rootFld->base_address = evD;
1197 rootFld
->field_size
= getFieldtypeSize(tf
, evT
, 0,0,rootFld
, evD
,t
);
1200 /*****************************************************************************
1202 * getFieldtypeSize: get the size of the field type (primitive type)
1204 * tracefile : opened trace file
1206 * offsetRoot : offset from the root
1207 * offsetParent : offset from the parrent
1209 * evD : event data, it may be NULL
1211 * int : size of the field
1212 ****************************************************************************/
1214 int getFieldtypeSize(LttTracefile
* t
, LttEventType
* evT
, int offsetRoot
,
1215 int offsetParent
, LttField
* fld
, void *evD
, LttTrace
*trace
)
1217 int size
, size1
, element_number
, i
, offset1
, offset2
;
1218 LttType
* type
= fld
->field_type
;
1221 if(evT
->latest_block
==t
->which_block
&& evT
->latest_event
==t
->which_event
){
1222 return fld
->field_size
;
1226 if(fld
->field_fixed
== 1){
1227 if(fld
== evT
->root_field
) return fld
->field_size
;
1230 if(type
->type_class
!= LTT_STRUCT
&& type
->type_class
!= LTT_ARRAY
&&
1231 type
->type_class
!= LTT_SEQUENCE
&& type
->type_class
!= LTT_STRING
){
1232 if(fld
->field_fixed
== -1){
1233 size
= (int) ltt_type_size(trace
, type
);
1234 fld
->field_fixed
= 1;
1235 }else size
= fld
->field_size
;
1237 }else if(type
->type_class
== LTT_ARRAY
){
1238 element_number
= (int) type
->element_number
;
1239 if(fld
->field_fixed
== -1){
1240 size
= getFieldtypeSize(t
, evT
, offsetRoot
,0,fld
->child
[0], NULL
, trace
);
1241 if(size
== 0){ //has string or sequence
1242 fld
->field_fixed
= 0;
1244 fld
->field_fixed
= 1;
1245 size
*= element_number
;
1247 }else if(fld
->field_fixed
== 0){// has string or sequence
1249 for(i
=0;i
<element_number
;i
++){
1250 size
+= getFieldtypeSize(t
, evT
, offsetRoot
+size
,size
,
1251 fld
->child
[0], evD
+size
, trace
);
1253 }else size
= fld
->field_size
;
1255 }else if(type
->type_class
== LTT_SEQUENCE
){
1256 size1
= (int) ltt_type_size(trace
, type
);
1257 if(fld
->field_fixed
== -1){
1258 fld
->sequ_number_size
= size1
;
1259 fld
->field_fixed
= 0;
1260 size
= getFieldtypeSize(t
, evT
, offsetRoot
,0,fld
->child
[0], NULL
, trace
);
1261 fld
->element_size
= size
;
1263 element_number
= getIntNumber(size1
,evD
);
1264 type
->element_number
= element_number
;
1265 if(fld
->element_size
> 0){
1266 size
= element_number
* fld
->element_size
;
1267 }else{//sequence has string or sequence
1269 for(i
=0;i
<element_number
;i
++){
1270 size
+= getFieldtypeSize(t
, evT
, offsetRoot
+size
+size1
,size
+size1
,
1271 fld
->child
[0], evD
+size
+size1
, trace
);
1277 }else if(type
->type_class
== LTT_STRING
){
1279 if(fld
->field_fixed
== -1){
1280 fld
->field_fixed
= 0;
1282 size
= strlen((char*)evD
) + 1; //include end : '\0'
1285 }else if(type
->type_class
== LTT_STRUCT
){
1286 element_number
= (int) type
->element_number
;
1288 if(fld
->field_fixed
== -1){
1289 offset1
= offsetRoot
;
1291 for(i
=0;i
<element_number
;i
++){
1292 size1
=getFieldtypeSize(t
, evT
,offset1
,offset2
, fld
->child
[i
], NULL
, trace
);
1293 if(size1
> 0 && size
>= 0){
1295 if(offset1
>= 0) offset1
+= size1
;
1304 fld
->field_fixed
= 0;
1306 }else fld
->field_fixed
= 1;
1307 }else if(fld
->field_fixed
== 0){
1308 offset1
= offsetRoot
;
1310 for(i
=0;i
<element_number
;i
++){
1311 size
=getFieldtypeSize(t
,evT
,offset1
,offset2
,fld
->child
[i
],evD
+offset2
, trace
);
1316 }else size
= fld
->field_size
;
1319 fld
->offset_root
= offsetRoot
;
1320 fld
->offset_parent
= offsetParent
;
1322 fld
->fixed_root
= (offsetRoot
==-1) ? 0 : 1;
1323 fld
->fixed_parent
= (offsetParent
==-1) ? 0 : 1;
1325 fld
->field_size
= size
;
1331 /*****************************************************************************
1333 * getIntNumber : get an integer number
1335 * size : the size of the integer
1336 * evD : the event data
1339 ****************************************************************************/
1341 int getIntNumber(int size
, void *evD
)
1344 if(size
== 1) i
= *(gint8
*)evD
;
1345 else if(size
== 2) i
= *(gint16
*)evD
;
1346 else if(size
== 4) i
= *(gint32
*)evD
;
1347 else if(size
== 8) i
= *(gint64
*)evD
;
1352 /*****************************************************************************
1354 * getDataEndianType : get the data type size and endian type of the local
1357 * size : size of data type
1358 * endian : endian type, little or big
1359 ****************************************************************************/
1361 void getDataEndianType(LttArchSize
* size
, LttArchEndian
* endian
)
1365 int sizeInt
=sizeof(int), sizeLong
=sizeof(long), sizePointer
=sizeof(void *);
1367 if(c
== 1) *endian
= LTT_LITTLE_ENDIAN
;
1368 else *endian
= LTT_BIG_ENDIAN
;
1370 if(sizeInt
== 2 && sizeLong
== 4 && sizePointer
== 4)
1372 else if(sizeInt
== 4 && sizeLong
== 4 && sizePointer
== 4)
1374 else if(sizeInt
== 4 && sizeLong
== 8 && sizePointer
== 8)
1376 else if(sizeInt
== 8 && sizeLong
== 8 && sizePointer
== 8)
1378 else *size
= LTT_UNKNOWN
;