6 #include <linux/errno.h>
8 #include <ltt/LTTTypes.h>
10 #include <ltt/trace.h>
12 #define DIR_NAME_SIZE 256
14 /* set the offset of the fields belonging to the event,
15 need the information of the archecture */
16 void setFieldsOffset(LttTracefile
*tf
,LttEventType
*evT
,void *evD
,LttTrace
*t
);
18 /* get the size of the field type according to the archtecture's
19 size and endian type(info of the archecture) */
20 int getFieldtypeSize(LttTracefile
* tf
, LttEventType
* evT
, int offsetRoot
,
21 int offsetParent
, LttField
*fld
, void *evD
, LttTrace
* t
);
23 /* read a fixed size or a block information from the file (fd) */
24 int readFile(int fd
, void * buf
, size_t size
, char * mesg
);
25 int readBlock(LttTracefile
* tf
, int whichBlock
);
27 /* calculate cycles per nsec for current block */
28 void getCyclePerNsec(LttTracefile
* t
);
30 /* reinitialize the info of the block which is already in the buffer */
31 void updateTracefile(LttTracefile
* tf
);
33 /* go to the next event */
34 int skipEvent(LttTracefile
* t
);
36 /* compare two time (LttTime), 0:t1=t2, -1:t1<t2, 1:t1>t2 */
37 int timecmp(LttTime
* t1
, LttTime
* t2
);
41 /*****************************************************************************
43 * ltt_tracefile_open : open a trace file, construct a LttTracefile
45 * t : the trace containing the tracefile
46 * fileName : path name of the trace file
48 * : a pointer to a tracefile
49 ****************************************************************************/
51 LttTracefile
* ltt_tracefile_open(LttTrace
* t
, char * fileName
)
54 struct stat lTDFStat
; /* Trace data file status */
55 BlockStart a_block_start
;
57 tf
= g_new(LttTracefile
, 1);
60 tf
->name
= g_strdup(fileName
);
62 tf
->fd
= open(fileName
, O_RDONLY
, 0);
64 g_error("Unable to open input data file %s\n", fileName
);
67 // Get the file's status
68 if(fstat(tf
->fd
, &lTDFStat
) < 0){
69 g_error("Unable to get the status of the input data file %s\n", fileName
);
72 // Is the file large enough to contain a trace
73 if(lTDFStat
.st_size
< sizeof(BlockStart
) + EVENT_HEADER_SIZE
){
74 g_print("The input data file %s does not contain a trace\n", fileName
);
81 //store the size of the file
82 tf
->file_size
= lTDFStat
.st_size
;
83 tf
->block_size
= t
->system_description
->ltt_block_size
;
84 tf
->block_number
= tf
->file_size
/ tf
->block_size
;
87 //allocate memory to contain the info of a block
88 tf
->buffer
= (void *) g_new(char, t
->system_description
->ltt_block_size
);
90 //read the first block
91 if(readBlock(tf
,1)) exit(1);
97 /*****************************************************************************
98 *Open control and per cpu tracefiles
99 ****************************************************************************/
101 void ltt_tracefile_open_cpu(LttTrace
*t
, char * tracefile_name
)
104 tf
= ltt_tracefile_open(t
,tracefile_name
);
106 t
->per_cpu_tracefile_number
++;
107 g_ptr_array_add(t
->per_cpu_tracefiles
, tf
);
110 void ltt_tracefile_open_control(LttTrace
*t
, char * control_name
)
120 tf
= ltt_tracefile_open(t
,control_name
);
122 t
->control_tracefile_number
++;
123 g_ptr_array_add(t
->control_tracefiles
,tf
);
125 //parse facilities tracefile to get base_id
126 if(strcmp(&control_name
[strlen(control_name
)-10],"facilities") ==0){
128 ev
= ltt_tracefile_read(tf
);
129 if(!ev
)return; // end of file
131 if(ev
->event_id
== TRACE_FACILITY_LOAD
){
133 fLoad
.name
= (char*)pos
;
134 fLoad
.checksum
= *(LttChecksum
*)(pos
+ strlen(fLoad
.name
));
135 fLoad
.base_code
= *(uint32_t*)(pos
+ strlen(fLoad
.name
) + sizeof(LttChecksum
));
137 for(i
=0;i
<t
->facility_number
;i
++){
138 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
,i
);
139 if(strcmp(f
->name
,fLoad
.name
)==0 && fLoad
.checksum
==f
->checksum
){
140 f
->base_id
= fLoad
.base_code
;
144 if(i
==t
->facility_number
)
145 g_error("Facility: %s, checksum: %d is not founded\n",
146 fLoad
.name
,fLoad
.checksum
);
147 }else if(ev
->event_id
== TRACE_BLOCK_START
){
149 }else if(ev
->event_id
== TRACE_BLOCK_END
){
151 }else g_error("Not valid facilities trace file\n");
156 /*****************************************************************************
158 * ltt_tracefile_close: close a trace file,
160 * t : tracefile which will be closed
161 ****************************************************************************/
163 void ltt_tracefile_close(LttTracefile
*t
)
172 /*****************************************************************************
173 *Get system information
174 ****************************************************************************/
175 void getSystemInfo(LttSystemDescription
* des
, char * pathname
)
179 int entry_number
= 15;
180 char buf
[DIR_NAME_SIZE
];
181 char description
[4*DIR_NAME_SIZE
];
184 fp
= fopen(pathname
,"r");
186 g_error("Can not open file : %s\n", pathname
);
189 while(fgets(buf
,DIR_NAME_SIZE
, fp
)!= NULL
){
191 while(isspace(*ptr
)) ptr
++;
192 if(strlen(ptr
) == 0) continue;
196 if(strlen(ptr
) == 0) g_error("Not a valid file: %s\n", pathname
);
197 if(strncmp("<system",ptr
,7) !=0)g_error("Not a valid file: %s\n", pathname
);
199 for(i
=0;i
<entry_number
;i
++){
200 if(fgets(buf
,DIR_NAME_SIZE
, fp
)== NULL
)
201 g_error("Not a valid file: %s\n", pathname
);
202 buf
[strlen(buf
)-1] = '\0';
204 while(isspace(*ptr
)) ptr
++;
207 if(strncmp("node_name=",ptr
,10)!=0)
208 g_error("Not a valid file: %s\n", pathname
);
209 des
->node_name
= g_strdup(ptr
+10);
212 if(strncmp("domainname=",ptr
,11)!=0)
213 g_error("Not a valid file: %s\n", pathname
);
214 des
->domain_name
= g_strdup(ptr
+11);
217 if(strncmp("cpu=",ptr
,4)!=0)
218 g_error("Not a valid file: %s\n", pathname
);
219 des
->nb_cpu
= (unsigned)atoi(ptr
+4);
222 if(strncmp("arch_size=",ptr
,10)!=0)
223 g_error("Not a valid file: %s\n", pathname
);
224 if(strcmp(ptr
+10,"\"LP32\"") == 0) des
->size
= LTT_LP32
;
225 else if(strcmp(ptr
+10,"\"ILP32\"") == 0) des
->size
= LTT_ILP32
;
226 else if(strcmp(ptr
+10,"\"LP64\"") == 0) des
->size
= LTT_LP64
;
227 else if(strcmp(ptr
+10,"\"ILP64\"") == 0) des
->size
= LTT_ILP64
;
228 else if(strcmp(ptr
+10,"\"UNKNOWN\"") == 0) des
->size
= LTT_UNKNOWN
;
231 if(strncmp("endian=",ptr
,7)!=0)
232 g_error("Not a valid file: %s\n", pathname
);
233 if(strcmp(ptr
+7,"\"LITTLE_ENDIAN\"") == 0)
234 des
->endian
= LTT_LITTLE_ENDIAN
;
235 else if(strcmp(ptr
+7,"\"BIG_ENDIAN\"") == 0)
236 des
->endian
= LTT_BIG_ENDIAN
;
239 if(strncmp("kernel_name=",ptr
,12)!=0)
240 g_error("Not a valid file: %s\n", pathname
);
241 des
->kernel_name
= g_strdup(ptr
+12);
244 if(strncmp("kernel_release=",ptr
,15)!=0)
245 g_error("Not a valid file: %s\n", pathname
);
246 des
->kernel_release
= g_strdup(ptr
+15);
249 if(strncmp("kernel_version=",ptr
,15)!=0)
250 g_error("Not a valid file: %s\n", pathname
);
251 des
->kernel_version
= g_strdup(ptr
+15);
254 if(strncmp("machine=",ptr
,8)!=0)
255 g_error("Not a valid file: %s\n", pathname
);
256 des
->machine
= g_strdup(ptr
+8);
259 if(strncmp("processor=",ptr
,10)!=0)
260 g_error("Not a valid file: %s\n", pathname
);
261 des
->processor
= g_strdup(ptr
+10);
264 if(strncmp("hardware_platform=",ptr
,18)!=0)
265 g_error("Not a valid file: %s\n", pathname
);
266 des
->hardware_platform
= g_strdup(ptr
+18);
269 if(strncmp("operating_system=",ptr
,17)!=0)
270 g_error("Not a valid file: %s\n", pathname
);
271 des
->operating_system
= g_strdup(ptr
+17);
274 if(strncmp("ltt_major_version=",ptr
,18)!=0)
275 g_error("Not a valid file: %s\n", pathname
);
277 // ptr++;//skip begining "
278 // ptr[strlen(ptr)-1] = '\0'; //get rid of the ending "
279 des
->ltt_major_version
= (unsigned)atoi(ptr
);
282 if(strncmp("ltt_minor_version=",ptr
,18)!=0)
283 g_error("Not a valid file: %s\n", pathname
);
285 // ptr++;//skip begining "
286 // ptr[strlen(ptr)-1] = '\0'; //get rid of the ending "
287 des
->ltt_minor_version
= (unsigned)atoi(ptr
);
290 if(strncmp("ltt_block_size=",ptr
,15)!=0)
291 g_error("Not a valid file: %s\n", pathname
);
293 // ptr++;//skip begining "
294 // ptr[strlen(ptr)-1] = '\0'; //get rid of the ending "
295 des
->ltt_block_size
= (unsigned)atoi(ptr
);
298 g_error("Not a valid file: %s\n", pathname
);
303 description
[0] = '\0';
304 if(fgets(buf
,DIR_NAME_SIZE
, fp
)== NULL
)
305 g_error("Not a valid file: %s\n", pathname
);
307 while(isspace(*ptr
)) ptr
++;
308 if(*ptr
!= '>') g_error("Not a valid file: %s\n", pathname
);
309 while((ptr
=fgets(buf
,DIR_NAME_SIZE
, fp
))!= NULL
){
311 while(isspace(*ptr
)) ptr
++;
312 if(strncmp("</system>",ptr
,9) == 0 )break;
313 strcat(description
, buf
);
315 if(!ptr
)g_error("Not a valid file: %s\n", pathname
);
316 if(description
[0] = '\0')des
->description
= NULL
;
317 des
->description
= g_strdup(description
);
322 /*****************************************************************************
323 *The following functions get facility/tracefile information
324 ****************************************************************************/
326 void getFacilityInfo(LttTrace
*t
, char* eventdefs
)
329 struct dirent
*entry
;
334 char name
[DIR_NAME_SIZE
];
336 dir
= opendir(eventdefs
);
337 if(!dir
) g_error("Can not open directory: %s\n", eventdefs
);
339 while((entry
= readdir(dir
)) != NULL
){
340 ptr
= &entry
->d_name
[strlen(entry
->d_name
)-4];
341 if(strcmp(ptr
,".xml") != 0) continue;
342 strcpy(name
,eventdefs
);
343 strcat(name
,entry
->d_name
);
344 ltt_facility_open(t
,name
);
348 for(j
=0;j
<t
->facility_number
;j
++){
349 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, j
);
350 for(i
=0; i
<f
->event_number
; i
++){
352 setFieldsOffset(NULL
, et
, NULL
, t
);
357 void getControlFileInfo(LttTrace
*t
, char* control
)
360 struct dirent
*entry
;
361 char name
[DIR_NAME_SIZE
];
363 dir
= opendir(control
);
364 if(!dir
) g_error("Can not open directory: %s\n", control
);
366 while((entry
= readdir(dir
)) != NULL
){
367 if(strcmp(entry
->d_name
,"facilities") != 0 &&
368 strcmp(entry
->d_name
,"interrupts") != 0 &&
369 strcmp(entry
->d_name
,"processes") != 0) continue;
371 strcpy(name
,control
);
372 strcat(name
,entry
->d_name
);
373 ltt_tracefile_open_control(t
,name
);
378 void getCpuFileInfo(LttTrace
*t
, char* cpu
)
381 struct dirent
*entry
;
382 char name
[DIR_NAME_SIZE
];
385 if(!dir
) g_error("Can not open directory: %s\n", cpu
);
387 while((entry
= readdir(dir
)) != NULL
){
388 if(strcmp(entry
->d_name
,".") != 0 &&
389 strcmp(entry
->d_name
,"..") != 0 ){
391 strcat(name
,entry
->d_name
);
392 ltt_tracefile_open_cpu(t
,name
);
398 /*****************************************************************************
399 *A trace is specified as a pathname to the directory containing all the
400 *associated data (control tracefiles, per cpu tracefiles, event
403 *When a trace is closed, all the associated facilities, types and fields
404 *are released as well.
405 ****************************************************************************/
407 LttTrace
*ltt_trace_open(const char *pathname
)
410 LttSystemDescription
* sys_description
;
411 char eventdefs
[DIR_NAME_SIZE
];
412 char info
[DIR_NAME_SIZE
];
413 char control
[DIR_NAME_SIZE
];
414 char cpu
[DIR_NAME_SIZE
];
415 char tmp
[DIR_NAME_SIZE
];
416 gboolean has_slash
= FALSE
;
418 //establish the pathname to different directories
419 if(pathname
[strlen(pathname
)-1] == '/')has_slash
= TRUE
;
420 strcpy(eventdefs
,pathname
);
421 if(!has_slash
)strcat(eventdefs
,"/");
422 strcat(eventdefs
,"eventdefs/");
424 strcpy(info
,pathname
);
425 if(!has_slash
)strcat(info
,"/");
426 strcat(info
,"info/");
428 strcpy(control
,pathname
);
429 if(!has_slash
)strcat(control
,"/");
430 strcat(control
,"control/");
432 strcpy(cpu
,pathname
);
433 if(!has_slash
)strcat(cpu
,"/");
437 t
= g_new(LttTrace
, 1);
438 sys_description
= g_new(LttSystemDescription
, 1);
439 t
->pathname
= g_strdup(pathname
);
440 t
->facility_number
= 0;
441 t
->control_tracefile_number
= 0;
442 t
->per_cpu_tracefile_number
= 0;
443 t
->system_description
= sys_description
;
444 t
->control_tracefiles
= g_ptr_array_new();
445 t
->per_cpu_tracefiles
= g_ptr_array_new();
446 t
->facilities
= g_ptr_array_new();
447 getDataEndianType(&(t
->my_arch_size
), &(t
->my_arch_endian
));
449 //get system description
451 strcat(tmp
,"system.xml");
452 getSystemInfo(sys_description
, tmp
);
454 //get facilities info
455 getFacilityInfo(t
,eventdefs
);
457 //get control tracefile info
458 getControlFileInfo(t
,control
);
460 //get cpu tracefile info
461 getCpuFileInfo(t
,cpu
);
466 /******************************************************************************
467 * When we copy a trace, we want all the opening actions to happen again :
468 * the trace will be reopened and totally independant from the original.
469 * That's why we call ltt_trace_open.
470 *****************************************************************************/
471 LttTrace
*ltt_trace_copy(LttTrace
*self
)
473 return ltt_trace_open(self
->pathname
);
476 void ltt_trace_close(LttTrace
*t
)
484 //free system_description
485 g_free(t
->system_description
->description
);
486 g_free(t
->system_description
->node_name
);
487 g_free(t
->system_description
->domain_name
);
488 g_free(t
->system_description
->kernel_name
);
489 g_free(t
->system_description
->kernel_release
);
490 g_free(t
->system_description
->kernel_version
);
491 g_free(t
->system_description
->machine
);
492 g_free(t
->system_description
->processor
);
493 g_free(t
->system_description
->hardware_platform
);
494 g_free(t
->system_description
->operating_system
);
495 g_free(t
->system_description
);
497 //free control_tracefiles
498 for(i
=0;i
<t
->control_tracefile_number
;i
++){
499 tf
= (LttTracefile
*)g_ptr_array_index(t
->control_tracefiles
,i
);
500 ltt_tracefile_close(tf
);
502 g_ptr_array_free(t
->control_tracefiles
, TRUE
);
504 //free per_cpu_tracefiles
505 for(i
=0;i
<t
->per_cpu_tracefile_number
;i
++){
506 tf
= (LttTracefile
*)g_ptr_array_index(t
->per_cpu_tracefiles
,i
);
507 ltt_tracefile_close(tf
);
509 g_ptr_array_free(t
->per_cpu_tracefiles
, TRUE
);
512 for(i
=0;i
<t
->facility_number
;i
++){
513 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
,i
);
514 ltt_facility_close(f
);
516 g_ptr_array_free(t
->facilities
, TRUE
);
524 /*****************************************************************************
525 *Get the system description of the trace
526 ****************************************************************************/
528 LttSystemDescription
*ltt_trace_system_description(LttTrace
*t
)
530 return t
->system_description
;
533 /*****************************************************************************
534 * The following functions discover the facilities of the trace
535 ****************************************************************************/
537 unsigned ltt_trace_facility_number(LttTrace
*t
)
539 return (unsigned)(t
->facility_number
);
542 LttFacility
*ltt_trace_facility_get(LttTrace
*t
, unsigned i
)
544 return (LttFacility
*)g_ptr_array_index(t
->facilities
, i
);
547 /*****************************************************************************
549 * ltt_trace_facility_find : find facilities in the trace
552 * name : facility name
554 * position : position of the facility in the trace
556 * : the number of facilities
557 ****************************************************************************/
559 unsigned ltt_trace_facility_find(LttTrace
*t
, char *name
, unsigned *position
)
563 for(i
=0;i
<t
->facility_number
;i
++){
564 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, i
);
565 if(strcmp(f
->name
,name
)==0){
567 if(count
==1) *position
= i
;
575 /*****************************************************************************
576 * Functions to discover all the event types in the trace
577 ****************************************************************************/
579 unsigned ltt_trace_eventtype_number(LttTrace
*t
)
584 for(i
=0;i
<t
->facility_number
;i
++){
585 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, i
);
586 count
+= f
->event_number
;
591 LttFacility
* ltt_trace_facility_by_id(LttTrace
* trace
, unsigned id
)
593 LttFacility
* facility
;
595 for(i
=0;i
<trace
->facility_number
;i
++){
596 facility
= (LttFacility
*) g_ptr_array_index(trace
->facilities
,i
);
597 if(id
>= facility
->base_id
&&
598 id
< facility
->base_id
+ facility
->event_number
)
601 if(i
==trace
->facility_number
) return NULL
;
602 else return facility
;
605 LttEventType
*ltt_trace_eventtype_get(LttTrace
*t
, unsigned evId
)
608 f
= ltt_trace_facility_by_id(t
,evId
);
610 return f
->events
[evId
- f
->base_id
];
613 /*****************************************************************************
614 *There is one "per cpu" tracefile for each CPU, numbered from 0 to
615 *the maximum number of CPU in the system. When the number of CPU installed
616 *is less than the maximum, some positions are unused. There are also a
617 *number of "control" tracefiles (facilities, interrupts...).
618 ****************************************************************************/
619 unsigned ltt_trace_control_tracefile_number(LttTrace
*t
)
621 return t
->control_tracefile_number
;
624 unsigned ltt_trace_per_cpu_tracefile_number(LttTrace
*t
)
626 return t
->per_cpu_tracefile_number
;
629 /*****************************************************************************
630 *It is possible to search for the tracefiles by name or by CPU position.
631 *The index within the tracefiles of the same type is returned if found
632 *and a negative value otherwise.
633 ****************************************************************************/
635 int ltt_trace_control_tracefile_find(LttTrace
*t
, char *name
)
637 LttTracefile
* tracefile
;
639 for(i
=0;i
<t
->control_tracefile_number
;i
++){
640 tracefile
= (LttTracefile
*)g_ptr_array_index(t
->control_tracefiles
, i
);
641 if(strcmp(tracefile
->name
, name
)==0)break;
643 if(i
== t
->control_tracefile_number
) return -1;
647 int ltt_trace_per_cpu_tracefile_find(LttTrace
*t
, unsigned i
)
649 LttTracefile
* tracefile
;
651 for(j
=0;j
<t
->per_cpu_tracefile_number
;j
++){
652 tracefile
= (LttTracefile
*)g_ptr_array_index(t
->per_cpu_tracefiles
, j
);
653 name
= atoi(tracefile
->name
);
654 if(name
== (int)i
)break;
656 if(j
== t
->per_cpu_tracefile_number
) return -1;
660 /*****************************************************************************
661 *Get a specific tracefile
662 ****************************************************************************/
664 LttTracefile
*ltt_trace_control_tracefile_get(LttTrace
*t
, unsigned i
)
666 return (LttTracefile
*)g_ptr_array_index(t
->control_tracefiles
, i
);
669 LttTracefile
*ltt_trace_per_cpu_tracefile_get(LttTrace
*t
, unsigned i
)
671 return (LttTracefile
*)g_ptr_array_index(t
->per_cpu_tracefiles
, i
);
674 /*****************************************************************************
675 * Get the start time and end time of the trace
676 ****************************************************************************/
678 void ltt_trace_time_span_get(LttTrace
*t
, LttTime
*start
, LttTime
*end
)
680 LttTime startSmall
, startTmp
, endBig
, endTmp
;
684 for(i
=0;i
<t
->control_tracefile_number
;i
++){
685 tf
= g_ptr_array_index(t
->control_tracefiles
, i
);
687 startTmp
= tf
->a_block_start
->time
;
688 readBlock(tf
,tf
->block_number
);
689 endTmp
= tf
->a_block_end
->time
;
691 startSmall
= startTmp
;
696 if(timecmp(&startSmall
,&startTmp
) > 0) startSmall
= startTmp
;
697 if(timecmp(&endBig
,&endTmp
) < 0) endBig
= endTmp
;
700 for(i
=0;i
<t
->per_cpu_tracefile_number
;i
++){
701 tf
= g_ptr_array_index(t
->per_cpu_tracefiles
, i
);
703 startTmp
= tf
->a_block_start
->time
;
704 readBlock(tf
,tf
->block_number
);
705 endTmp
= tf
->a_block_end
->time
;
707 startSmall
= startTmp
;
711 if(timecmp(&startSmall
,&startTmp
) > 0) startSmall
= startTmp
;
712 if(timecmp(&endBig
,&endTmp
) < 0) endBig
= endTmp
;
720 /*****************************************************************************
721 *Get the name of a tracefile
722 ****************************************************************************/
724 char *ltt_tracefile_name(LttTracefile
*tf
)
729 /*****************************************************************************
730 * Get the number of blocks in the tracefile
731 ****************************************************************************/
733 unsigned ltt_tracefile_block_number(LttTracefile
*tf
)
735 return tf
->block_number
;
738 /*****************************************************************************
740 * ltt_tracefile_seek_time: seek to the first event of the trace with time
741 * larger or equal to time
744 * time : criteria of the time
745 ****************************************************************************/
747 void ltt_tracefile_seek_time(LttTracefile
*t
, LttTime time
)
751 int headTime
= timecmp(&(t
->a_block_start
->time
), &time
);
752 int tailTime
= timecmp(&(t
->a_block_end
->time
), &time
);
755 if(headTime
< 0 && tailTime
> 0){
756 if(timecmp(&(t
->a_block_end
->time
),&(t
->current_event_time
)) !=0){
757 lttTime
= getEventTime(t
);
758 err
= timecmp(<tTime
, &time
);
760 if(t
->which_event
==2 || timecmp(&t
->prev_event_time
,&time
)<0){
764 return ltt_tracefile_seek_time(t
, time
);
768 ev
= ltt_tracefile_read(t
);
770 g_print("End of file\n");
773 lttTime
= getEventTime(t
);
774 err
= timecmp(<tTime
, &time
);
778 }else{//we are at the end of the block
780 return ltt_tracefile_seek_time(t
, time
);
782 }else if(headTime
>= 0){
783 if(t
->which_block
== 1){
786 if(timecmp(&(t
->prev_block_end_time
),&time
) >= 0 ){
787 err
=readBlock(t
,t
->which_block
-1);
788 if(err
) g_error("Can not read tracefile: %s\n", t
->name
);
789 return ltt_tracefile_seek_time(t
, time
) ;
794 }else if(tailTime
< 0){
795 if(t
->which_block
!= t
->block_number
){
796 err
=readBlock(t
,t
->which_block
+1);
797 if(err
) g_error("Can not read tracefile: %s\n", t
->name
);
799 g_print("End of file\n");
802 if(tailTime
< 0) return ltt_tracefile_seek_time(t
, time
);
803 }else if(tailTime
== 0){
804 t
->cur_event_pos
= t
->last_event_pos
;
805 t
->current_event_time
= time
;
806 t
->cur_heart_beat_number
= 0;
807 t
->prev_event_time
.tv_sec
= 0;
808 t
->prev_event_time
.tv_nsec
= 0;
813 /*****************************************************************************
814 * Seek to the first event with position equal or larger to ep
815 ****************************************************************************/
817 void ltt_tracefile_seek_position(LttTracefile
*t
, LttEventPosition
*ep
)
819 //if we are at the right place, just return
820 if(t
->which_block
== ep
->block_num
&& t
->which_event
== ep
->event_num
)
823 if(t
->which_block
== ep
->block_num
) updateTracefile(t
);
824 else readBlock(t
,ep
->block_num
);
826 //event offset is availiable
827 if(ep
->old_position
){
828 t
->cur_heart_beat_number
= ep
->heart_beat_number
;
829 t
->cur_event_pos
= t
->buffer
+ ep
->event_offset
;
833 //only block number and event index are availiable
834 while(t
->which_event
< ep
->event_num
) ltt_tracefile_read(t
);
839 /*****************************************************************************
841 * ltt_tracefile_read : read the current event, set the pointer to the next
845 * LttEvent * : an event to be processed
846 ****************************************************************************/
848 LttEvent
*ltt_tracefile_read(LttTracefile
*t
)
850 LttEvent
* lttEvent
= &t
->an_event
;
853 if(t
->cur_event_pos
== t
->buffer
+ t
->block_size
){
854 if(t
->which_block
== t
->block_number
){
857 err
= readBlock(t
, t
->which_block
+ 1);
858 if(err
)g_error("Can not read tracefile");
861 lttEvent
->event_id
= (int)(*(uint16_t *)(t
->cur_event_pos
));
862 if(lttEvent
->event_id
== TRACE_TIME_HEARTBEAT
)
863 t
->cur_heart_beat_number
++;
865 t
->prev_event_time
= t
->current_event_time
;
866 // t->current_event_time = getEventTime(t);
868 lttEvent
->time_delta
= *(uint32_t*)(t
->cur_event_pos
+ EVENT_ID_SIZE
);
869 lttEvent
->event_time
= t
->current_event_time
;
871 lttEvent
->tracefile
= t
;
872 lttEvent
->data
= t
->cur_event_pos
+ EVENT_HEADER_SIZE
;
873 lttEvent
->which_block
= t
->which_block
;
874 lttEvent
->which_event
= t
->which_event
;
876 //update the fields of the current event and go to the next event
878 if(err
== ERANGE
) g_error("event id is out of range\n");
880 lttEvent
->event_cycle_count
= t
->cur_cycle_count
;
885 /****************************************************************************
887 * readFile : wrap function to read from a file
889 * fd : file descriptor
890 * buf : buf to contain the content
891 * size : number of bytes to be read
892 * mesg : message to be printed if some thing goes wrong
895 * EIO : can not read from the file
896 ****************************************************************************/
898 int readFile(int fd
, void * buf
, size_t size
, char * mesg
)
901 nbBytes
= read(fd
, buf
, size
);
909 /****************************************************************************
911 * readBlock : read a block from the file
913 * lttdes : ltt trace file
914 * whichBlock : the block which will be read
917 * EINVAL : lseek fail
918 * EIO : can not read from the file
919 ****************************************************************************/
921 int readBlock(LttTracefile
* tf
, int whichBlock
)
926 if(whichBlock
- tf
->which_block
== 1 && tf
->which_block
!= 0){
927 tf
->prev_block_end_time
= tf
->a_block_end
->time
;
928 tf
->prev_event_time
= tf
->a_block_end
->time
;
930 tf
->prev_block_end_time
.tv_sec
= 0;
931 tf
->prev_block_end_time
.tv_nsec
= 0;
932 tf
->prev_event_time
.tv_sec
= 0;
933 tf
->prev_event_time
.tv_nsec
= 0;
936 nbBytes
=lseek(tf
->fd
,(off_t
)((whichBlock
-1)*tf
->block_size
), SEEK_SET
);
937 if(nbBytes
== -1) return EINVAL
;
939 if(readFile(tf
->fd
,tf
->buffer
,tf
->block_size
,"Unable to read a block"))
942 tf
->a_block_start
=(BlockStart
*) (tf
->buffer
+ EVENT_HEADER_SIZE
);
943 lostSize
= *(uint32_t*)(tf
->buffer
+ tf
->block_size
- sizeof(uint32_t));
944 tf
->a_block_end
=(BlockEnd
*)(tf
->buffer
+ tf
->block_size
-
945 lostSize
+ EVENT_HEADER_SIZE
);
946 tf
->last_event_pos
= tf
->buffer
+ tf
->block_size
- lostSize
;
948 tf
->which_block
= whichBlock
;
950 tf
->cur_event_pos
= tf
->buffer
;//the beginning of the block, block start ev
951 tf
->cur_heart_beat_number
= 0;
955 tf
->current_event_time
= getEventTime(tf
);
960 /*****************************************************************************
962 * updateTracefile : reinitialize the info of the block which is already
966 ****************************************************************************/
968 void updateTracefile(LttTracefile
* tf
)
971 tf
->cur_event_pos
= tf
->buffer
;
972 tf
->current_event_time
= getEventTime(tf
);
973 tf
->cur_heart_beat_number
= 0;
975 tf
->prev_event_time
.tv_sec
= 0;
976 tf
->prev_event_time
.tv_nsec
= 0;
979 /*****************************************************************************
981 * skipEvent : go to the next event, update the fields of the current event
986 * ERANGE : event id is out of range
987 ****************************************************************************/
989 int skipEvent(LttTracefile
* t
)
996 evId
= (int)(*(uint16_t *)(t
->cur_event_pos
));
997 evData
= t
->cur_event_pos
+ EVENT_HEADER_SIZE
;
999 evT
= ltt_trace_eventtype_get(t
->trace
,(unsigned)evId
);
1001 if(evT
) rootFld
= evT
->root_field
;
1005 //event has string/sequence or the last event is not the same event
1006 if((evT
->latest_block
!=t
->which_block
|| evT
->latest_event
!=t
->which_event
)
1007 && rootFld
->field_fixed
== 0){
1008 setFieldsOffset(t
, evT
, evData
, t
->trace
);
1010 t
->cur_event_pos
+= EVENT_HEADER_SIZE
+ rootFld
->field_size
;
1011 }else t
->cur_event_pos
+= EVENT_HEADER_SIZE
;
1013 evT
->latest_block
= t
->which_block
;
1014 evT
->latest_event
= t
->which_event
;
1016 //the next event is in the next block
1017 if(evId
== TRACE_BLOCK_END
){
1018 t
->cur_event_pos
= t
->buffer
+ t
->block_size
;
1021 t
->current_event_time
= getEventTime(t
);
1027 /*****************************************************************************
1029 * getCyclePerNsec : calculate cycles per nsec for current block
1032 ****************************************************************************/
1034 void getCyclePerNsec(LttTracefile
* t
)
1036 LttTime lBufTotalTime
; /* Total time for this buffer */
1037 LttCycleCount lBufTotalNSec
; /* Total time for this buffer in nsecs */
1038 LttCycleCount lBufTotalCycle
;/* Total cycles for this buffer */
1040 /* Calculate the total time for this buffer */
1041 TimeSub(lBufTotalTime
,t
->a_block_end
->time
, t
->a_block_start
->time
);
1043 /* Calculate the total cycles for this bufffer */
1044 lBufTotalCycle
= t
->a_block_end
->cycle_count
;
1045 lBufTotalCycle
-= t
->a_block_start
->cycle_count
;
1047 /* Convert the total time to nsecs */
1048 lBufTotalNSec
= lBufTotalTime
.tv_sec
;
1049 lBufTotalNSec
*= NANSECOND_CONST
;
1050 lBufTotalNSec
+= lBufTotalTime
.tv_nsec
;
1052 t
->cycle_per_nsec
= (double)lBufTotalCycle
/ (double)lBufTotalNSec
;
1055 /****************************************************************************
1057 * getEventTime : obtain the time of an event
1061 * LttTime : the time of the event
1062 ****************************************************************************/
1064 LttTime
getEventTime(LttTracefile
* tf
)
1067 LttCycleCount cycle_count
; // cycle count for the current event
1068 LttCycleCount lEventTotalCycle
; // Total cycles from start for event
1069 double lEventNSec
; // Total usecs from start for event
1070 LttTime lTimeOffset
; // Time offset in struct LttTime
1072 int64_t nanoSec
, tmpCycleCount
= (((uint64_t)1)<<32);
1073 static LttCycleCount preCycleCount
= 0;
1074 static int count
= 0;
1076 evId
= *(uint16_t*)tf
->cur_event_pos
;
1077 if(evId
== TRACE_BLOCK_START
){
1080 tf
->cur_cycle_count
= tf
->a_block_start
->cycle_count
;
1081 return tf
->a_block_start
->time
;
1082 }else if(evId
== TRACE_BLOCK_END
){
1085 tf
->cur_cycle_count
= tf
->a_block_end
->cycle_count
;
1086 return tf
->a_block_end
->time
;
1089 // Calculate total time in cycles from start of buffer for this event
1090 cycle_count
= (LttCycleCount
)*(uint32_t*)(tf
->cur_event_pos
+ EVENT_ID_SIZE
);
1092 if(cycle_count
< preCycleCount
)count
++;
1093 preCycleCount
= cycle_count
;
1094 cycle_count
+= tmpCycleCount
* count
;
1096 if(tf
->cur_heart_beat_number
> count
)
1097 cycle_count
+= tmpCycleCount
* (tf
->cur_heart_beat_number
- count
);
1099 tf
->cur_cycle_count
= cycle_count
;
1101 lEventTotalCycle
= cycle_count
;
1102 lEventTotalCycle
-= tf
->a_block_start
->cycle_count
;
1104 // Convert it to nsecs
1105 lEventNSec
= lEventTotalCycle
/ tf
->cycle_per_nsec
;
1106 nanoSec
= lEventNSec
;
1108 // Determine offset in struct LttTime
1109 lTimeOffset
.tv_nsec
= nanoSec
% NANSECOND_CONST
;
1110 lTimeOffset
.tv_sec
= nanoSec
/ NANSECOND_CONST
;
1112 TimeAdd(time
, tf
->a_block_start
->time
, lTimeOffset
);
1117 /*****************************************************************************
1119 * setFieldsOffset : set offset of the fields
1121 * tracefile : opened trace file
1122 * evT : the event type
1123 * evD : event data, it may be NULL
1124 ****************************************************************************/
1126 void setFieldsOffset(LttTracefile
*tf
,LttEventType
*evT
,void *evD
,LttTrace
* t
)
1128 LttField
* rootFld
= evT
->root_field
;
1129 // rootFld->base_address = evD;
1132 rootFld
->field_size
= getFieldtypeSize(tf
, evT
, 0,0,rootFld
, evD
,t
);
1135 /*****************************************************************************
1137 * getFieldtypeSize: get the size of the field type (primitive type)
1139 * tracefile : opened trace file
1141 * offsetRoot : offset from the root
1142 * offsetParent : offset from the parrent
1144 * evD : event data, it may be NULL
1146 * int : size of the field
1147 ****************************************************************************/
1149 int getFieldtypeSize(LttTracefile
* t
, LttEventType
* evT
, int offsetRoot
,
1150 int offsetParent
, LttField
* fld
, void *evD
, LttTrace
*trace
)
1152 int size
, size1
, element_number
, i
, offset1
, offset2
;
1153 LttType
* type
= fld
->field_type
;
1156 if(evT
->latest_block
==t
->which_block
&& evT
->latest_event
==t
->which_event
){
1157 return fld
->field_size
;
1161 if(fld
->field_fixed
== 1){
1162 if(fld
== evT
->root_field
) return fld
->field_size
;
1165 if(type
->type_class
!= LTT_STRUCT
&& type
->type_class
!= LTT_ARRAY
&&
1166 type
->type_class
!= LTT_SEQUENCE
&& type
->type_class
!= LTT_STRING
){
1167 if(fld
->field_fixed
== -1){
1168 size
= (int) ltt_type_size(trace
, type
);
1169 fld
->field_fixed
= 1;
1170 }else size
= fld
->field_size
;
1172 }else if(type
->type_class
== LTT_ARRAY
){
1173 element_number
= (int) type
->element_number
;
1174 if(fld
->field_fixed
== -1){
1175 size
= getFieldtypeSize(t
, evT
, offsetRoot
,0,fld
->child
[0], NULL
, trace
);
1176 if(size
== 0){ //has string or sequence
1177 fld
->field_fixed
= 0;
1179 fld
->field_fixed
= 1;
1180 size
*= element_number
;
1182 }else if(fld
->field_fixed
== 0){// has string or sequence
1184 for(i
=0;i
<element_number
;i
++){
1185 size
+= getFieldtypeSize(t
, evT
, offsetRoot
+size
,size
,
1186 fld
->child
[0], evD
+size
, trace
);
1188 }else size
= fld
->field_size
;
1190 }else if(type
->type_class
== LTT_SEQUENCE
){
1191 size1
= (int) ltt_type_size(trace
, type
);
1192 if(fld
->field_fixed
== -1){
1193 fld
->sequ_number_size
= size1
;
1194 fld
->field_fixed
= 0;
1195 size
= getFieldtypeSize(t
, evT
, offsetRoot
,0,fld
->child
[0], NULL
, trace
);
1196 fld
->element_size
= size
;
1198 element_number
= getIntNumber(size1
,evD
);
1199 type
->element_number
= element_number
;
1200 if(fld
->element_size
> 0){
1201 size
= element_number
* fld
->element_size
;
1202 }else{//sequence has string or sequence
1204 for(i
=0;i
<element_number
;i
++){
1205 size
+= getFieldtypeSize(t
, evT
, offsetRoot
+size
+size1
,size
+size1
,
1206 fld
->child
[0], evD
+size
+size1
, trace
);
1212 }else if(type
->type_class
== LTT_STRING
){
1214 if(fld
->field_fixed
== -1){
1215 fld
->field_fixed
= 0;
1217 size
= strlen((char*)evD
) + 1; //include end : '\0'
1220 }else if(type
->type_class
== LTT_STRUCT
){
1221 element_number
= (int) type
->element_number
;
1223 if(fld
->field_fixed
== -1){
1224 offset1
= offsetRoot
;
1226 for(i
=0;i
<element_number
;i
++){
1227 size1
=getFieldtypeSize(t
, evT
,offset1
,offset2
, fld
->child
[i
], NULL
, trace
);
1228 if(size1
> 0 && size
>= 0){
1230 if(offset1
>= 0) offset1
+= size1
;
1239 fld
->field_fixed
= 0;
1241 }else fld
->field_fixed
= 1;
1242 }else if(fld
->field_fixed
== 0){
1243 offset1
= offsetRoot
;
1245 for(i
=0;i
<element_number
;i
++){
1246 size
=getFieldtypeSize(t
,evT
,offset1
,offset2
,fld
->child
[i
],evD
+offset2
, trace
);
1251 }else size
= fld
->field_size
;
1254 fld
->offset_root
= offsetRoot
;
1255 fld
->offset_parent
= offsetParent
;
1257 fld
->fixed_root
= (offsetRoot
==-1) ? 0 : 1;
1258 fld
->fixed_parent
= (offsetParent
==-1) ? 0 : 1;
1260 fld
->field_size
= size
;
1265 /*****************************************************************************
1267 * timecmp : compare two time
1272 * int : 0: t1 == t2; -1: t1 < t2; 1: t1 > t2
1273 ****************************************************************************/
1275 int timecmp(LttTime
* t1
, LttTime
* t2
)
1277 if(t1
->tv_sec
< t2
->tv_sec
) return -1;
1278 if(t1
->tv_sec
> t2
->tv_sec
) return 1;
1279 if(t1
->tv_nsec
< t2
->tv_nsec
) return -1;
1280 if(t1
->tv_nsec
> t2
->tv_nsec
) return 1;
1284 /*****************************************************************************
1286 * getIntNumber : get an integer number
1288 * size : the size of the integer
1289 * evD : the event data
1292 ****************************************************************************/
1294 int getIntNumber(int size
, void *evD
)
1297 if(size
== 1) i
= *(int8_t *)evD
;
1298 else if(size
== 2) i
= *(int16_t *)evD
;
1299 else if(size
== 4) i
= *(int32_t *)evD
;
1300 else if(size
== 8) i
= *(int64_t *)evD
;
1305 /*****************************************************************************
1307 * getDataEndianType : get the data type size and endian type of the local
1310 * size : size of data type
1311 * endian : endian type, little or big
1312 ****************************************************************************/
1314 void getDataEndianType(LttArchSize
* size
, LttArchEndian
* endian
)
1318 int sizeInt
=sizeof(int), sizeLong
=sizeof(long), sizePointer
=sizeof(void *);
1320 if(c
== 1) *endian
= LTT_LITTLE_ENDIAN
;
1321 else *endian
= LTT_BIG_ENDIAN
;
1323 if(sizeInt
== 2 && sizeLong
== 4 && sizePointer
== 4)
1325 else if(sizeInt
== 4 && sizeLong
== 4 && sizePointer
== 4)
1327 else if(sizeInt
== 4 && sizeLong
== 8 && sizePointer
== 8)
1329 else if(sizeInt
== 8 && sizeLong
== 8 && sizePointer
== 8)
1331 else *size
= LTT_UNKNOWN
;