6 #include <linux/errno.h>
10 #include "ltt-private.h"
11 #include <ltt/trace.h>
12 #include <ltt/facility.h>
14 #define DIR_NAME_SIZE 256
16 /* set the offset of the fields belonging to the event,
17 need the information of the archecture */
18 void setFieldsOffset(LttTracefile
*tf
,LttEventType
*evT
,void *evD
,LttTrace
*t
);
20 /* get the size of the field type according to the archtecture's
21 size and endian type(info of the archecture) */
22 int getFieldtypeSize(LttTracefile
* tf
, LttEventType
* evT
, int offsetRoot
,
23 int offsetParent
, LttField
*fld
, void *evD
, LttTrace
* t
);
25 /* read a fixed size or a block information from the file (fd) */
26 int readFile(int fd
, void * buf
, size_t size
, char * mesg
);
27 int readBlock(LttTracefile
* tf
, int whichBlock
);
29 /* calculate cycles per nsec for current block */
30 void getCyclePerNsec(LttTracefile
* t
);
32 /* reinitialize the info of the block which is already in the buffer */
33 void updateTracefile(LttTracefile
* tf
);
35 /* go to the next event */
36 int skipEvent(LttTracefile
* t
);
39 /*****************************************************************************
41 * ltt_tracefile_open : open a trace file, construct a LttTracefile
43 * t : the trace containing the tracefile
44 * fileName : path name of the trace file
46 * : a pointer to a tracefile
47 ****************************************************************************/
49 LttTracefile
* ltt_tracefile_open(LttTrace
* t
, char * fileName
)
52 struct stat lTDFStat
; /* Trace data file status */
53 BlockStart a_block_start
;
55 tf
= g_new(LttTracefile
, 1);
58 tf
->name
= g_strdup(fileName
);
60 tf
->fd
= open(fileName
, O_RDONLY
, 0);
62 g_error("Unable to open input data file %s\n", fileName
);
65 // Get the file's status
66 if(fstat(tf
->fd
, &lTDFStat
) < 0){
67 g_error("Unable to get the status of the input data file %s\n", fileName
);
70 // Is the file large enough to contain a trace
71 if(lTDFStat
.st_size
< sizeof(BlockStart
) + EVENT_HEADER_SIZE
){
72 g_print("The input data file %s does not contain a trace\n", fileName
);
79 //store the size of the file
80 tf
->file_size
= lTDFStat
.st_size
;
81 tf
->block_size
= t
->system_description
->ltt_block_size
;
82 tf
->block_number
= tf
->file_size
/ tf
->block_size
;
85 //allocate memory to contain the info of a block
86 tf
->buffer
= (void *) g_new(char, t
->system_description
->ltt_block_size
);
88 //read the first block
89 if(readBlock(tf
,1)) exit(1);
95 /*****************************************************************************
96 *Open control and per cpu tracefiles
97 ****************************************************************************/
99 void ltt_tracefile_open_cpu(LttTrace
*t
, char * tracefile_name
)
102 tf
= ltt_tracefile_open(t
,tracefile_name
);
104 t
->per_cpu_tracefile_number
++;
105 g_ptr_array_add(t
->per_cpu_tracefiles
, tf
);
108 void ltt_tracefile_open_control(LttTrace
*t
, char * control_name
)
118 tf
= ltt_tracefile_open(t
,control_name
);
120 t
->control_tracefile_number
++;
121 g_ptr_array_add(t
->control_tracefiles
,tf
);
123 //parse facilities tracefile to get base_id
124 if(strcmp(&control_name
[strlen(control_name
)-10],"facilities") ==0){
126 ev
= ltt_tracefile_read(tf
);
127 if(!ev
)return; // end of file
129 if(ev
->event_id
== TRACE_FACILITY_LOAD
){
131 fLoad
.name
= (char*)pos
;
132 fLoad
.checksum
= *(LttChecksum
*)(pos
+ strlen(fLoad
.name
));
133 fLoad
.base_code
= *(guint32
*)(pos
+ strlen(fLoad
.name
) + sizeof(LttChecksum
));
135 for(i
=0;i
<t
->facility_number
;i
++){
136 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
,i
);
137 if(strcmp(f
->name
,fLoad
.name
)==0 && fLoad
.checksum
==f
->checksum
){
138 f
->base_id
= fLoad
.base_code
;
142 if(i
==t
->facility_number
)
143 g_error("Facility: %s, checksum: %d is not founded\n",
144 fLoad
.name
,fLoad
.checksum
);
145 }else if(ev
->event_id
== TRACE_BLOCK_START
){
147 }else if(ev
->event_id
== TRACE_BLOCK_END
){
149 }else g_error("Not valid facilities trace file\n");
154 /*****************************************************************************
156 * ltt_tracefile_close: close a trace file,
158 * t : tracefile which will be closed
159 ****************************************************************************/
161 void ltt_tracefile_close(LttTracefile
*t
)
170 /*****************************************************************************
171 *Get system information
172 ****************************************************************************/
173 void getSystemInfo(LttSystemDescription
* des
, char * pathname
)
177 int entry_number
= 15;
178 char buf
[DIR_NAME_SIZE
];
179 char description
[4*DIR_NAME_SIZE
];
182 fp
= fopen(pathname
,"r");
184 g_error("Can not open file : %s\n", pathname
);
187 while(fgets(buf
,DIR_NAME_SIZE
, fp
)!= NULL
){
189 while(isspace(*ptr
)) ptr
++;
190 if(strlen(ptr
) == 0) continue;
194 if(strlen(ptr
) == 0) g_error("Not a valid file: %s\n", pathname
);
195 if(strncmp("<system",ptr
,7) !=0)g_error("Not a valid file: %s\n", pathname
);
197 for(i
=0;i
<entry_number
;i
++){
198 if(fgets(buf
,DIR_NAME_SIZE
, fp
)== NULL
)
199 g_error("Not a valid file: %s\n", pathname
);
200 buf
[strlen(buf
)-1] = '\0';
202 while(isspace(*ptr
)) ptr
++;
205 if(strncmp("node_name=",ptr
,10)!=0)
206 g_error("Not a valid file: %s\n", pathname
);
207 des
->node_name
= g_strdup(ptr
+10);
210 if(strncmp("domainname=",ptr
,11)!=0)
211 g_error("Not a valid file: %s\n", pathname
);
212 des
->domain_name
= g_strdup(ptr
+11);
215 if(strncmp("cpu=",ptr
,4)!=0)
216 g_error("Not a valid file: %s\n", pathname
);
217 des
->nb_cpu
= (unsigned)atoi(ptr
+4);
220 if(strncmp("arch_size=",ptr
,10)!=0)
221 g_error("Not a valid file: %s\n", pathname
);
222 if(strcmp(ptr
+10,"\"LP32\"") == 0) des
->size
= LTT_LP32
;
223 else if(strcmp(ptr
+10,"\"ILP32\"") == 0) des
->size
= LTT_ILP32
;
224 else if(strcmp(ptr
+10,"\"LP64\"") == 0) des
->size
= LTT_LP64
;
225 else if(strcmp(ptr
+10,"\"ILP64\"") == 0) des
->size
= LTT_ILP64
;
226 else if(strcmp(ptr
+10,"\"UNKNOWN\"") == 0) des
->size
= LTT_UNKNOWN
;
229 if(strncmp("endian=",ptr
,7)!=0)
230 g_error("Not a valid file: %s\n", pathname
);
231 if(strcmp(ptr
+7,"\"LITTLE_ENDIAN\"") == 0)
232 des
->endian
= LTT_LITTLE_ENDIAN
;
233 else if(strcmp(ptr
+7,"\"BIG_ENDIAN\"") == 0)
234 des
->endian
= LTT_BIG_ENDIAN
;
237 if(strncmp("kernel_name=",ptr
,12)!=0)
238 g_error("Not a valid file: %s\n", pathname
);
239 des
->kernel_name
= g_strdup(ptr
+12);
242 if(strncmp("kernel_release=",ptr
,15)!=0)
243 g_error("Not a valid file: %s\n", pathname
);
244 des
->kernel_release
= g_strdup(ptr
+15);
247 if(strncmp("kernel_version=",ptr
,15)!=0)
248 g_error("Not a valid file: %s\n", pathname
);
249 des
->kernel_version
= g_strdup(ptr
+15);
252 if(strncmp("machine=",ptr
,8)!=0)
253 g_error("Not a valid file: %s\n", pathname
);
254 des
->machine
= g_strdup(ptr
+8);
257 if(strncmp("processor=",ptr
,10)!=0)
258 g_error("Not a valid file: %s\n", pathname
);
259 des
->processor
= g_strdup(ptr
+10);
262 if(strncmp("hardware_platform=",ptr
,18)!=0)
263 g_error("Not a valid file: %s\n", pathname
);
264 des
->hardware_platform
= g_strdup(ptr
+18);
267 if(strncmp("operating_system=",ptr
,17)!=0)
268 g_error("Not a valid file: %s\n", pathname
);
269 des
->operating_system
= g_strdup(ptr
+17);
272 if(strncmp("ltt_major_version=",ptr
,18)!=0)
273 g_error("Not a valid file: %s\n", pathname
);
275 // ptr++;//skip begining "
276 // ptr[strlen(ptr)-1] = '\0'; //get rid of the ending "
277 des
->ltt_major_version
= (unsigned)atoi(ptr
);
280 if(strncmp("ltt_minor_version=",ptr
,18)!=0)
281 g_error("Not a valid file: %s\n", pathname
);
283 // ptr++;//skip begining "
284 // ptr[strlen(ptr)-1] = '\0'; //get rid of the ending "
285 des
->ltt_minor_version
= (unsigned)atoi(ptr
);
288 if(strncmp("ltt_block_size=",ptr
,15)!=0)
289 g_error("Not a valid file: %s\n", pathname
);
291 // ptr++;//skip begining "
292 // ptr[strlen(ptr)-1] = '\0'; //get rid of the ending "
293 des
->ltt_block_size
= (unsigned)atoi(ptr
);
296 g_error("Not a valid file: %s\n", pathname
);
301 description
[0] = '\0';
302 if(fgets(buf
,DIR_NAME_SIZE
, fp
)== NULL
)
303 g_error("Not a valid file: %s\n", pathname
);
305 while(isspace(*ptr
)) ptr
++;
306 if(*ptr
!= '>') g_error("Not a valid file: %s\n", pathname
);
307 while((ptr
=fgets(buf
,DIR_NAME_SIZE
, fp
))!= NULL
){
309 while(isspace(*ptr
)) ptr
++;
310 if(strncmp("</system>",ptr
,9) == 0 )break;
311 strcat(description
, buf
);
313 if(!ptr
)g_error("Not a valid file: %s\n", pathname
);
314 if(description
[0] = '\0')des
->description
= NULL
;
315 des
->description
= g_strdup(description
);
320 /*****************************************************************************
321 *The following functions get facility/tracefile information
322 ****************************************************************************/
324 void getFacilityInfo(LttTrace
*t
, char* eventdefs
)
327 struct dirent
*entry
;
332 char name
[DIR_NAME_SIZE
];
334 dir
= opendir(eventdefs
);
335 if(!dir
) g_error("Can not open directory: %s\n", eventdefs
);
337 while((entry
= readdir(dir
)) != NULL
){
338 ptr
= &entry
->d_name
[strlen(entry
->d_name
)-4];
339 if(strcmp(ptr
,".xml") != 0) continue;
340 strcpy(name
,eventdefs
);
341 strcat(name
,entry
->d_name
);
342 ltt_facility_open(t
,name
);
346 for(j
=0;j
<t
->facility_number
;j
++){
347 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, j
);
348 for(i
=0; i
<f
->event_number
; i
++){
350 setFieldsOffset(NULL
, et
, NULL
, t
);
355 void getControlFileInfo(LttTrace
*t
, char* control
)
358 struct dirent
*entry
;
359 char name
[DIR_NAME_SIZE
];
361 dir
= opendir(control
);
362 if(!dir
) g_error("Can not open directory: %s\n", control
);
364 while((entry
= readdir(dir
)) != NULL
){
365 if(strcmp(entry
->d_name
,"facilities") != 0 &&
366 strcmp(entry
->d_name
,"interrupts") != 0 &&
367 strcmp(entry
->d_name
,"processes") != 0) continue;
369 strcpy(name
,control
);
370 strcat(name
,entry
->d_name
);
371 ltt_tracefile_open_control(t
,name
);
376 void getCpuFileInfo(LttTrace
*t
, char* cpu
)
379 struct dirent
*entry
;
380 char name
[DIR_NAME_SIZE
];
383 if(!dir
) g_error("Can not open directory: %s\n", cpu
);
385 while((entry
= readdir(dir
)) != NULL
){
386 if(strcmp(entry
->d_name
,".") != 0 &&
387 strcmp(entry
->d_name
,"..") != 0 ){
389 strcat(name
,entry
->d_name
);
390 ltt_tracefile_open_cpu(t
,name
);
396 /*****************************************************************************
397 *A trace is specified as a pathname to the directory containing all the
398 *associated data (control tracefiles, per cpu tracefiles, event
401 *When a trace is closed, all the associated facilities, types and fields
402 *are released as well.
403 ****************************************************************************/
405 LttTrace
*ltt_trace_open(const char *pathname
)
408 LttSystemDescription
* sys_description
;
409 char eventdefs
[DIR_NAME_SIZE
];
410 char info
[DIR_NAME_SIZE
];
411 char control
[DIR_NAME_SIZE
];
412 char cpu
[DIR_NAME_SIZE
];
413 char tmp
[DIR_NAME_SIZE
];
414 gboolean has_slash
= FALSE
;
416 //establish the pathname to different directories
417 if(pathname
[strlen(pathname
)-1] == '/')has_slash
= TRUE
;
418 strcpy(eventdefs
,pathname
);
419 if(!has_slash
)strcat(eventdefs
,"/");
420 strcat(eventdefs
,"eventdefs/");
422 strcpy(info
,pathname
);
423 if(!has_slash
)strcat(info
,"/");
424 strcat(info
,"info/");
426 strcpy(control
,pathname
);
427 if(!has_slash
)strcat(control
,"/");
428 strcat(control
,"control/");
430 strcpy(cpu
,pathname
);
431 if(!has_slash
)strcat(cpu
,"/");
435 t
= g_new(LttTrace
, 1);
436 sys_description
= g_new(LttSystemDescription
, 1);
437 t
->pathname
= g_strdup(pathname
);
438 t
->facility_number
= 0;
439 t
->control_tracefile_number
= 0;
440 t
->per_cpu_tracefile_number
= 0;
441 t
->system_description
= sys_description
;
442 t
->control_tracefiles
= g_ptr_array_new();
443 t
->per_cpu_tracefiles
= g_ptr_array_new();
444 t
->facilities
= g_ptr_array_new();
445 getDataEndianType(&(t
->my_arch_size
), &(t
->my_arch_endian
));
447 //get system description
449 strcat(tmp
,"system.xml");
450 getSystemInfo(sys_description
, tmp
);
452 //get facilities info
453 getFacilityInfo(t
,eventdefs
);
455 //get control tracefile info
456 getControlFileInfo(t
,control
);
458 //get cpu tracefile info
459 getCpuFileInfo(t
,cpu
);
464 char * ltt_trace_name(LttTrace
*t
)
470 /******************************************************************************
471 * When we copy a trace, we want all the opening actions to happen again :
472 * the trace will be reopened and totally independant from the original.
473 * That's why we call ltt_trace_open.
474 *****************************************************************************/
475 LttTrace
*ltt_trace_copy(LttTrace
*self
)
477 return ltt_trace_open(self
->pathname
);
480 void ltt_trace_close(LttTrace
*t
)
488 //free system_description
489 g_free(t
->system_description
->description
);
490 g_free(t
->system_description
->node_name
);
491 g_free(t
->system_description
->domain_name
);
492 g_free(t
->system_description
->kernel_name
);
493 g_free(t
->system_description
->kernel_release
);
494 g_free(t
->system_description
->kernel_version
);
495 g_free(t
->system_description
->machine
);
496 g_free(t
->system_description
->processor
);
497 g_free(t
->system_description
->hardware_platform
);
498 g_free(t
->system_description
->operating_system
);
499 g_free(t
->system_description
);
501 //free control_tracefiles
502 for(i
=0;i
<t
->control_tracefile_number
;i
++){
503 tf
= (LttTracefile
*)g_ptr_array_index(t
->control_tracefiles
,i
);
504 ltt_tracefile_close(tf
);
506 g_ptr_array_free(t
->control_tracefiles
, TRUE
);
508 //free per_cpu_tracefiles
509 for(i
=0;i
<t
->per_cpu_tracefile_number
;i
++){
510 tf
= (LttTracefile
*)g_ptr_array_index(t
->per_cpu_tracefiles
,i
);
511 ltt_tracefile_close(tf
);
513 g_ptr_array_free(t
->per_cpu_tracefiles
, TRUE
);
516 for(i
=0;i
<t
->facility_number
;i
++){
517 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
,i
);
518 ltt_facility_close(f
);
520 g_ptr_array_free(t
->facilities
, TRUE
);
528 /*****************************************************************************
529 *Get the system description of the trace
530 ****************************************************************************/
532 LttSystemDescription
*ltt_trace_system_description(LttTrace
*t
)
534 return t
->system_description
;
537 /*****************************************************************************
538 * The following functions discover the facilities of the trace
539 ****************************************************************************/
541 unsigned ltt_trace_facility_number(LttTrace
*t
)
543 return (unsigned)(t
->facility_number
);
546 LttFacility
*ltt_trace_facility_get(LttTrace
*t
, unsigned i
)
548 return (LttFacility
*)g_ptr_array_index(t
->facilities
, i
);
551 /*****************************************************************************
553 * ltt_trace_facility_find : find facilities in the trace
556 * name : facility name
558 * position : position of the facility in the trace
560 * : the number of facilities
561 ****************************************************************************/
563 unsigned ltt_trace_facility_find(LttTrace
*t
, char *name
, unsigned *position
)
567 for(i
=0;i
<t
->facility_number
;i
++){
568 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, i
);
569 if(strcmp(f
->name
,name
)==0){
571 if(count
==1) *position
= i
;
579 /*****************************************************************************
580 * Functions to discover all the event types in the trace
581 ****************************************************************************/
583 unsigned ltt_trace_eventtype_number(LttTrace
*t
)
588 for(i
=0;i
<t
->facility_number
;i
++){
589 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, i
);
590 count
+= f
->event_number
;
595 LttFacility
* ltt_trace_facility_by_id(LttTrace
* trace
, unsigned id
)
597 LttFacility
* facility
;
599 for(i
=0;i
<trace
->facility_number
;i
++){
600 facility
= (LttFacility
*) g_ptr_array_index(trace
->facilities
,i
);
601 if(id
>= facility
->base_id
&&
602 id
< facility
->base_id
+ facility
->event_number
)
605 if(i
==trace
->facility_number
) return NULL
;
606 else return facility
;
609 LttEventType
*ltt_trace_eventtype_get(LttTrace
*t
, unsigned evId
)
612 f
= ltt_trace_facility_by_id(t
,evId
);
614 return f
->events
[evId
- f
->base_id
];
617 /*****************************************************************************
618 *There is one "per cpu" tracefile for each CPU, numbered from 0 to
619 *the maximum number of CPU in the system. When the number of CPU installed
620 *is less than the maximum, some positions are unused. There are also a
621 *number of "control" tracefiles (facilities, interrupts...).
622 ****************************************************************************/
623 unsigned ltt_trace_control_tracefile_number(LttTrace
*t
)
625 return t
->control_tracefile_number
;
628 unsigned ltt_trace_per_cpu_tracefile_number(LttTrace
*t
)
630 return t
->per_cpu_tracefile_number
;
633 /*****************************************************************************
634 *It is possible to search for the tracefiles by name or by CPU position.
635 *The index within the tracefiles of the same type is returned if found
636 *and a negative value otherwise.
637 ****************************************************************************/
639 int ltt_trace_control_tracefile_find(LttTrace
*t
, char *name
)
641 LttTracefile
* tracefile
;
643 for(i
=0;i
<t
->control_tracefile_number
;i
++){
644 tracefile
= (LttTracefile
*)g_ptr_array_index(t
->control_tracefiles
, i
);
645 if(strcmp(tracefile
->name
, name
)==0)break;
647 if(i
== t
->control_tracefile_number
) return -1;
651 int ltt_trace_per_cpu_tracefile_find(LttTrace
*t
, unsigned i
)
653 LttTracefile
* tracefile
;
655 for(j
=0;j
<t
->per_cpu_tracefile_number
;j
++){
656 tracefile
= (LttTracefile
*)g_ptr_array_index(t
->per_cpu_tracefiles
, j
);
657 name
= atoi(tracefile
->name
);
658 if(name
== (int)i
)break;
660 if(j
== t
->per_cpu_tracefile_number
) return -1;
664 /*****************************************************************************
665 *Get a specific tracefile
666 ****************************************************************************/
668 LttTracefile
*ltt_trace_control_tracefile_get(LttTrace
*t
, unsigned i
)
670 return (LttTracefile
*)g_ptr_array_index(t
->control_tracefiles
, i
);
673 LttTracefile
*ltt_trace_per_cpu_tracefile_get(LttTrace
*t
, unsigned i
)
675 return (LttTracefile
*)g_ptr_array_index(t
->per_cpu_tracefiles
, i
);
678 /*****************************************************************************
679 * Get the start time and end time of the trace
680 ****************************************************************************/
682 void ltt_trace_time_span_get(LttTrace
*t
, LttTime
*start
, LttTime
*end
)
684 LttTime startSmall
, startTmp
, endBig
, endTmp
;
688 for(i
=0;i
<t
->control_tracefile_number
;i
++){
689 tf
= g_ptr_array_index(t
->control_tracefiles
, i
);
691 startTmp
= tf
->a_block_start
->time
;
692 readBlock(tf
,tf
->block_number
);
693 endTmp
= tf
->a_block_end
->time
;
695 startSmall
= startTmp
;
700 if(ltt_time_compare(startSmall
,startTmp
) > 0) startSmall
= startTmp
;
701 if(ltt_time_compare(endBig
,endTmp
) < 0) endBig
= endTmp
;
704 for(i
=0;i
<t
->per_cpu_tracefile_number
;i
++){
705 tf
= g_ptr_array_index(t
->per_cpu_tracefiles
, i
);
707 startTmp
= tf
->a_block_start
->time
;
708 readBlock(tf
,tf
->block_number
);
709 endTmp
= tf
->a_block_end
->time
;
711 startSmall
= startTmp
;
715 if(ltt_time_compare(startSmall
,startTmp
) > 0) startSmall
= startTmp
;
716 if(ltt_time_compare(endBig
,endTmp
) < 0) endBig
= endTmp
;
724 /*****************************************************************************
725 *Get the name of a tracefile
726 ****************************************************************************/
728 char *ltt_tracefile_name(LttTracefile
*tf
)
733 /*****************************************************************************
734 * Get the number of blocks in the tracefile
735 ****************************************************************************/
737 unsigned ltt_tracefile_block_number(LttTracefile
*tf
)
739 return tf
->block_number
;
742 /*****************************************************************************
744 * ltt_tracefile_seek_time: seek to the first event of the trace with time
745 * larger or equal to time
748 * time : criteria of the time
749 ****************************************************************************/
750 void ltt_tracefile_find_time_block(LttTracefile
*t
, LttTime time
,
751 int start_block
, int end_block
)
753 int err
, tmp_block
, s
, e
;
757 err
=readBlock(t
,start_block
);
758 if(err
) g_error("Can not read tracefile: %s\n", t
->name
);
759 if(start_block
== end_block
)return;
761 tailTime
= ltt_time_compare(t
->a_block_end
->time
, time
);
762 if(tailTime
>= 0) return;
764 err
=readBlock(t
,end_block
);
765 if(err
) g_error("Can not read tracefile: %s\n", t
->name
);
766 if(start_block
+1 == end_block
)return;
768 headTime
= ltt_time_compare(t
->a_block_start
->time
, time
);
769 if(headTime
<= 0 ) return;
771 tmp_block
= (end_block
+ start_block
)/2;
772 err
=readBlock(t
,tmp_block
);
773 if(err
) g_error("Can not read tracefile: %s\n", t
->name
);
775 headTime
= ltt_time_compare(t
->a_block_start
->time
, time
);
776 tailTime
= ltt_time_compare(t
->a_block_end
->time
, time
);
777 if(headTime
<= 0 && tailTime
>= 0) return;
783 ltt_tracefile_find_time_block(t
, time
, s
, e
);
791 ltt_tracefile_find_time_block(t
, time
, s
, e
);
796 void ltt_tracefile_backward_find_time_block(LttTracefile
*t
, LttTime time
)
798 int t_time
, h_time
, err
;
799 err
=readBlock(t
,t
->which_block
-1);
800 if(err
) g_error("Can not read tracefile: %s\n", t
->name
);
801 h_time
= ltt_time_compare(t
->a_block_start
->time
, time
);
802 t_time
= ltt_time_compare(t
->a_block_end
->time
, time
);
805 if(t
->which_block
== 1) return;
806 err
=readBlock(t
,t
->which_block
-1);
807 if(err
) g_error("Can not read tracefile: %s\n", t
->name
);
808 tmp
= ltt_time_compare(t
->a_block_end
->time
, time
);
809 if(tmp
== 0) return ltt_tracefile_seek_time(t
, time
);
810 err
=readBlock(t
,t
->which_block
+1);
811 if(err
) g_error("Can not read tracefile: %s\n", t
->name
);
812 }else if(h_time
> 0){
813 ltt_tracefile_find_time_block(t
, time
, 1, t
->which_block
);
814 return ltt_tracefile_seek_time(t
, time
) ;
816 if(t_time
>= 0) return ltt_tracefile_seek_time(t
, time
);
817 err
=readBlock(t
,t
->which_block
+1);
818 if(err
) g_error("Can not read tracefile: %s\n", t
->name
);
822 void ltt_tracefile_seek_time(LttTracefile
*t
, LttTime time
)
826 int headTime
= ltt_time_compare(t
->a_block_start
->time
, time
);
827 int tailTime
= ltt_time_compare(t
->a_block_end
->time
, time
);
830 if(headTime
< 0 && tailTime
> 0){
831 if(ltt_time_compare(t
->a_block_end
->time
, t
->current_event_time
) !=0) {
832 lttTime
= getEventTime(t
);
833 err
= ltt_time_compare(lttTime
, time
);
835 if(t
->which_event
==2 || (&t
->prev_event_time
,&time
)<0){
839 return ltt_tracefile_seek_time(t
, time
);
843 ev
= ltt_tracefile_read(t
);
845 g_print("End of file\n");
848 lttTime
= getEventTime(t
);
849 err
= ltt_time_compare(lttTime
, time
);
853 }else{//we are at the end of the block
855 return ltt_tracefile_seek_time(t
, time
);
857 }else if(headTime
>= 0){
858 if(t
->which_block
== 1){
861 if(ltt_time_compare(t
->prev_block_end_time
, time
) >= 0 ||
862 (t
->prev_block_end_time
.tv_sec
== 0 &&
863 t
->prev_block_end_time
.tv_nsec
== 0 )){
864 ltt_tracefile_backward_find_time_block(t
, time
);
869 }else if(tailTime
< 0){
870 if(t
->which_block
!= t
->block_number
){
871 ltt_tracefile_find_time_block(t
, time
, t
->which_block
+1, t
->block_number
);
872 return ltt_tracefile_seek_time(t
, time
);
874 g_print("End of file\n");
877 }else if(tailTime
== 0){
878 t
->cur_event_pos
= t
->last_event_pos
;
879 t
->current_event_time
= time
;
880 t
->cur_heart_beat_number
= 0;
881 t
->prev_event_time
.tv_sec
= 0;
882 t
->prev_event_time
.tv_nsec
= 0;
887 /*****************************************************************************
888 * Seek to the first event with position equal or larger to ep
889 ****************************************************************************/
891 void ltt_tracefile_seek_position(LttTracefile
*t
, LttEventPosition
*ep
)
893 //if we are at the right place, just return
894 if(t
->which_block
== ep
->block_num
&& t
->which_event
== ep
->event_num
)
897 if(t
->which_block
== ep
->block_num
) updateTracefile(t
);
898 else readBlock(t
,ep
->block_num
);
900 //event offset is availiable
901 if(ep
->old_position
){
902 t
->cur_heart_beat_number
= ep
->heart_beat_number
;
903 t
->cur_event_pos
= t
->buffer
+ ep
->event_offset
;
907 //only block number and event index are availiable
908 while(t
->which_event
< ep
->event_num
) ltt_tracefile_read(t
);
913 /*****************************************************************************
915 * ltt_tracefile_read : read the current event, set the pointer to the next
919 * LttEvent * : an event to be processed
920 ****************************************************************************/
922 LttEvent
*ltt_tracefile_read(LttTracefile
*t
)
924 LttEvent
* lttEvent
= &t
->an_event
;
927 if(t
->cur_event_pos
== t
->buffer
+ t
->block_size
){
928 if(t
->which_block
== t
->block_number
){
931 err
= readBlock(t
, t
->which_block
+ 1);
932 if(err
)g_error("Can not read tracefile");
935 lttEvent
->event_id
= (int)(*(guint16
*)(t
->cur_event_pos
));
936 if(lttEvent
->event_id
== TRACE_TIME_HEARTBEAT
)
937 t
->cur_heart_beat_number
++;
939 t
->prev_event_time
= t
->current_event_time
;
940 // t->current_event_time = getEventTime(t);
942 lttEvent
->time_delta
= *(guint32
*)(t
->cur_event_pos
+ EVENT_ID_SIZE
);
943 lttEvent
->event_time
= t
->current_event_time
;
945 lttEvent
->tracefile
= t
;
946 lttEvent
->data
= t
->cur_event_pos
+ EVENT_HEADER_SIZE
;
947 lttEvent
->which_block
= t
->which_block
;
948 lttEvent
->which_event
= t
->which_event
;
950 //update the fields of the current event and go to the next event
952 if(err
== ERANGE
) g_error("event id is out of range\n");
954 lttEvent
->event_cycle_count
= t
->cur_cycle_count
;
959 /****************************************************************************
961 * readFile : wrap function to read from a file
963 * fd : file descriptor
964 * buf : buf to contain the content
965 * size : number of bytes to be read
966 * mesg : message to be printed if some thing goes wrong
969 * EIO : can not read from the file
970 ****************************************************************************/
972 int readFile(int fd
, void * buf
, size_t size
, char * mesg
)
975 nbBytes
= read(fd
, buf
, size
);
983 /****************************************************************************
985 * readBlock : read a block from the file
987 * lttdes : ltt trace file
988 * whichBlock : the block which will be read
991 * EINVAL : lseek fail
992 * EIO : can not read from the file
993 ****************************************************************************/
995 int readBlock(LttTracefile
* tf
, int whichBlock
)
1000 if(whichBlock
- tf
->which_block
== 1 && tf
->which_block
!= 0){
1001 tf
->prev_block_end_time
= tf
->a_block_end
->time
;
1002 tf
->prev_event_time
= tf
->a_block_end
->time
;
1004 tf
->prev_block_end_time
.tv_sec
= 0;
1005 tf
->prev_block_end_time
.tv_nsec
= 0;
1006 tf
->prev_event_time
.tv_sec
= 0;
1007 tf
->prev_event_time
.tv_nsec
= 0;
1010 nbBytes
=lseek(tf
->fd
,(off_t
)((whichBlock
-1)*tf
->block_size
), SEEK_SET
);
1011 if(nbBytes
== -1) return EINVAL
;
1013 if(readFile(tf
->fd
,tf
->buffer
,tf
->block_size
,"Unable to read a block"))
1016 tf
->a_block_start
=(BlockStart
*) (tf
->buffer
+ EVENT_HEADER_SIZE
);
1017 lostSize
= *(guint32
*)(tf
->buffer
+ tf
->block_size
- sizeof(guint32
));
1018 tf
->a_block_end
=(BlockEnd
*)(tf
->buffer
+ tf
->block_size
-
1019 lostSize
+ EVENT_HEADER_SIZE
);
1020 tf
->last_event_pos
= tf
->buffer
+ tf
->block_size
- lostSize
;
1022 tf
->which_block
= whichBlock
;
1023 tf
->which_event
= 1;
1024 tf
->cur_event_pos
= tf
->buffer
;//the beginning of the block, block start ev
1025 tf
->cur_heart_beat_number
= 0;
1027 getCyclePerNsec(tf
);
1029 tf
->current_event_time
= getEventTime(tf
);
1034 /*****************************************************************************
1036 * updateTracefile : reinitialize the info of the block which is already
1040 ****************************************************************************/
1042 void updateTracefile(LttTracefile
* tf
)
1044 tf
->which_event
= 1;
1045 tf
->cur_event_pos
= tf
->buffer
;
1046 tf
->current_event_time
= getEventTime(tf
);
1047 tf
->cur_heart_beat_number
= 0;
1049 tf
->prev_event_time
.tv_sec
= 0;
1050 tf
->prev_event_time
.tv_nsec
= 0;
1053 /*****************************************************************************
1055 * skipEvent : go to the next event, update the fields of the current event
1060 * ERANGE : event id is out of range
1061 ****************************************************************************/
1063 int skipEvent(LttTracefile
* t
)
1070 evId
= (int)(*(guint16
*)(t
->cur_event_pos
));
1071 evData
= t
->cur_event_pos
+ EVENT_HEADER_SIZE
;
1073 evT
= ltt_trace_eventtype_get(t
->trace
,(unsigned)evId
);
1075 if(evT
) rootFld
= evT
->root_field
;
1079 //event has string/sequence or the last event is not the same event
1080 if((evT
->latest_block
!=t
->which_block
|| evT
->latest_event
!=t
->which_event
)
1081 && rootFld
->field_fixed
== 0){
1082 setFieldsOffset(t
, evT
, evData
, t
->trace
);
1084 t
->cur_event_pos
+= EVENT_HEADER_SIZE
+ rootFld
->field_size
;
1085 }else t
->cur_event_pos
+= EVENT_HEADER_SIZE
;
1087 evT
->latest_block
= t
->which_block
;
1088 evT
->latest_event
= t
->which_event
;
1090 //the next event is in the next block
1091 if(evId
== TRACE_BLOCK_END
){
1092 t
->cur_event_pos
= t
->buffer
+ t
->block_size
;
1095 t
->current_event_time
= getEventTime(t
);
1101 /*****************************************************************************
1103 * getCyclePerNsec : calculate cycles per nsec for current block
1106 ****************************************************************************/
1108 void getCyclePerNsec(LttTracefile
* t
)
1110 LttTime lBufTotalTime
; /* Total time for this buffer */
1111 LttCycleCount lBufTotalNSec
; /* Total time for this buffer in nsecs */
1112 LttCycleCount lBufTotalCycle
;/* Total cycles for this buffer */
1114 /* Calculate the total time for this buffer */
1115 lBufTotalTime
= ltt_time_sub(t
->a_block_end
->time
, t
->a_block_start
->time
);
1117 /* Calculate the total cycles for this bufffer */
1118 lBufTotalCycle
= t
->a_block_end
->cycle_count
;
1119 lBufTotalCycle
-= t
->a_block_start
->cycle_count
;
1121 /* Convert the total time to nsecs */
1122 lBufTotalNSec
= lBufTotalTime
.tv_sec
;
1123 lBufTotalNSec
*= NANOSECONDS_PER_SECOND
;
1124 lBufTotalNSec
+= lBufTotalTime
.tv_nsec
;
1126 t
->cycle_per_nsec
= (double)lBufTotalCycle
/ (double)lBufTotalNSec
;
1129 /****************************************************************************
1131 * getEventTime : obtain the time of an event
1135 * LttTime : the time of the event
1136 ****************************************************************************/
1138 LttTime
getEventTime(LttTracefile
* tf
)
1141 LttCycleCount cycle_count
; // cycle count for the current event
1142 LttCycleCount lEventTotalCycle
; // Total cycles from start for event
1143 double lEventNSec
; // Total usecs from start for event
1144 LttTime lTimeOffset
; // Time offset in struct LttTime
1146 gint64 nanoSec
, tmpCycleCount
= (((guint64
)1)<<32);
1147 static LttCycleCount preCycleCount
= 0;
1148 static int count
= 0;
1150 evId
= *(guint16
*)tf
->cur_event_pos
;
1151 if(evId
== TRACE_BLOCK_START
){
1154 tf
->cur_cycle_count
= tf
->a_block_start
->cycle_count
;
1155 return tf
->a_block_start
->time
;
1156 }else if(evId
== TRACE_BLOCK_END
){
1159 tf
->cur_cycle_count
= tf
->a_block_end
->cycle_count
;
1160 return tf
->a_block_end
->time
;
1163 // Calculate total time in cycles from start of buffer for this event
1164 cycle_count
= (LttCycleCount
)*(guint32
*)(tf
->cur_event_pos
+ EVENT_ID_SIZE
);
1166 if(cycle_count
< preCycleCount
)count
++;
1167 preCycleCount
= cycle_count
;
1168 cycle_count
+= tmpCycleCount
* count
;
1170 if(tf
->cur_heart_beat_number
> count
)
1171 cycle_count
+= tmpCycleCount
* (tf
->cur_heart_beat_number
- count
);
1173 tf
->cur_cycle_count
= cycle_count
;
1175 lEventTotalCycle
= cycle_count
;
1176 lEventTotalCycle
-= tf
->a_block_start
->cycle_count
;
1178 // Convert it to nsecs
1179 lEventNSec
= lEventTotalCycle
/ tf
->cycle_per_nsec
;
1180 nanoSec
= lEventNSec
;
1182 // Determine offset in struct LttTime
1183 lTimeOffset
.tv_nsec
= nanoSec
% NANOSECONDS_PER_SECOND
;
1184 lTimeOffset
.tv_sec
= nanoSec
/ NANOSECONDS_PER_SECOND
;
1186 time
= ltt_time_add(tf
->a_block_start
->time
, lTimeOffset
);
1191 /*****************************************************************************
1193 * setFieldsOffset : set offset of the fields
1195 * tracefile : opened trace file
1196 * evT : the event type
1197 * evD : event data, it may be NULL
1198 ****************************************************************************/
1200 void setFieldsOffset(LttTracefile
*tf
,LttEventType
*evT
,void *evD
,LttTrace
* t
)
1202 LttField
* rootFld
= evT
->root_field
;
1203 // rootFld->base_address = evD;
1206 rootFld
->field_size
= getFieldtypeSize(tf
, evT
, 0,0,rootFld
, evD
,t
);
1209 /*****************************************************************************
1211 * getFieldtypeSize: get the size of the field type (primitive type)
1213 * tracefile : opened trace file
1215 * offsetRoot : offset from the root
1216 * offsetParent : offset from the parrent
1218 * evD : event data, it may be NULL
1220 * int : size of the field
1221 ****************************************************************************/
1223 int getFieldtypeSize(LttTracefile
* t
, LttEventType
* evT
, int offsetRoot
,
1224 int offsetParent
, LttField
* fld
, void *evD
, LttTrace
*trace
)
1226 int size
, size1
, element_number
, i
, offset1
, offset2
;
1227 LttType
* type
= fld
->field_type
;
1230 if(evT
->latest_block
==t
->which_block
&& evT
->latest_event
==t
->which_event
){
1231 return fld
->field_size
;
1235 if(fld
->field_fixed
== 1){
1236 if(fld
== evT
->root_field
) return fld
->field_size
;
1239 if(type
->type_class
!= LTT_STRUCT
&& type
->type_class
!= LTT_ARRAY
&&
1240 type
->type_class
!= LTT_SEQUENCE
&& type
->type_class
!= LTT_STRING
){
1241 if(fld
->field_fixed
== -1){
1242 size
= (int) ltt_type_size(trace
, type
);
1243 fld
->field_fixed
= 1;
1244 }else size
= fld
->field_size
;
1246 }else if(type
->type_class
== LTT_ARRAY
){
1247 element_number
= (int) type
->element_number
;
1248 if(fld
->field_fixed
== -1){
1249 size
= getFieldtypeSize(t
, evT
, offsetRoot
,0,fld
->child
[0], NULL
, trace
);
1250 if(size
== 0){ //has string or sequence
1251 fld
->field_fixed
= 0;
1253 fld
->field_fixed
= 1;
1254 size
*= element_number
;
1256 }else if(fld
->field_fixed
== 0){// has string or sequence
1258 for(i
=0;i
<element_number
;i
++){
1259 size
+= getFieldtypeSize(t
, evT
, offsetRoot
+size
,size
,
1260 fld
->child
[0], evD
+size
, trace
);
1262 }else size
= fld
->field_size
;
1264 }else if(type
->type_class
== LTT_SEQUENCE
){
1265 size1
= (int) ltt_type_size(trace
, type
);
1266 if(fld
->field_fixed
== -1){
1267 fld
->sequ_number_size
= size1
;
1268 fld
->field_fixed
= 0;
1269 size
= getFieldtypeSize(t
, evT
, offsetRoot
,0,fld
->child
[0], NULL
, trace
);
1270 fld
->element_size
= size
;
1272 element_number
= getIntNumber(size1
,evD
);
1273 type
->element_number
= element_number
;
1274 if(fld
->element_size
> 0){
1275 size
= element_number
* fld
->element_size
;
1276 }else{//sequence has string or sequence
1278 for(i
=0;i
<element_number
;i
++){
1279 size
+= getFieldtypeSize(t
, evT
, offsetRoot
+size
+size1
,size
+size1
,
1280 fld
->child
[0], evD
+size
+size1
, trace
);
1286 }else if(type
->type_class
== LTT_STRING
){
1288 if(fld
->field_fixed
== -1){
1289 fld
->field_fixed
= 0;
1291 size
= strlen((char*)evD
) + 1; //include end : '\0'
1294 }else if(type
->type_class
== LTT_STRUCT
){
1295 element_number
= (int) type
->element_number
;
1297 if(fld
->field_fixed
== -1){
1298 offset1
= offsetRoot
;
1300 for(i
=0;i
<element_number
;i
++){
1301 size1
=getFieldtypeSize(t
, evT
,offset1
,offset2
, fld
->child
[i
], NULL
, trace
);
1302 if(size1
> 0 && size
>= 0){
1304 if(offset1
>= 0) offset1
+= size1
;
1313 fld
->field_fixed
= 0;
1315 }else fld
->field_fixed
= 1;
1316 }else if(fld
->field_fixed
== 0){
1317 offset1
= offsetRoot
;
1319 for(i
=0;i
<element_number
;i
++){
1320 size
=getFieldtypeSize(t
,evT
,offset1
,offset2
,fld
->child
[i
],evD
+offset2
, trace
);
1325 }else size
= fld
->field_size
;
1328 fld
->offset_root
= offsetRoot
;
1329 fld
->offset_parent
= offsetParent
;
1331 fld
->fixed_root
= (offsetRoot
==-1) ? 0 : 1;
1332 fld
->fixed_parent
= (offsetParent
==-1) ? 0 : 1;
1334 fld
->field_size
= size
;
1340 /*****************************************************************************
1342 * getIntNumber : get an integer number
1344 * size : the size of the integer
1345 * evD : the event data
1348 ****************************************************************************/
1350 int getIntNumber(int size
, void *evD
)
1353 if(size
== 1) i
= *(gint8
*)evD
;
1354 else if(size
== 2) i
= *(gint16
*)evD
;
1355 else if(size
== 4) i
= *(gint32
*)evD
;
1356 else if(size
== 8) i
= *(gint64
*)evD
;
1361 /*****************************************************************************
1363 * getDataEndianType : get the data type size and endian type of the local
1366 * size : size of data type
1367 * endian : endian type, little or big
1368 ****************************************************************************/
1370 void getDataEndianType(LttArchSize
* size
, LttArchEndian
* endian
)
1374 int sizeInt
=sizeof(int), sizeLong
=sizeof(long), sizePointer
=sizeof(void *);
1376 if(c
== 1) *endian
= LTT_LITTLE_ENDIAN
;
1377 else *endian
= LTT_BIG_ENDIAN
;
1379 if(sizeInt
== 2 && sizeLong
== 4 && sizePointer
== 4)
1381 else if(sizeInt
== 4 && sizeLong
== 4 && sizePointer
== 4)
1383 else if(sizeInt
== 4 && sizeLong
== 8 && sizePointer
== 8)
1385 else if(sizeInt
== 8 && sizeLong
== 8 && sizePointer
== 8)
1387 else *size
= LTT_UNKNOWN
;
1390 /* get the node name of the system */
1392 char * ltt_trace_system_description_node_name (LttSystemDescription
* s
)
1394 return s
->node_name
;
1398 /* get the domain name of the system */
1400 char * ltt_trace_system_description_domain_name (LttSystemDescription
* s
)
1402 return s
->domain_name
;
1406 /* get the description of the system */
1408 char * ltt_trace_system_description_description (LttSystemDescription
* s
)
1410 return s
->description
;
1414 /* get the start time of the trace */
1416 LttTime
ltt_trace_system_description_trace_start_time(LttSystemDescription
*s
)
1418 return s
->trace_start
;