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 /* Functions to parse system.xml file (using glib xml parser) */
40 static void parser_start_element (GMarkupParseContext
*context
,
41 const gchar
*element_name
,
42 const gchar
**attribute_names
,
43 const gchar
**attribute_values
,
48 LttSystemDescription
* des
= (LttSystemDescription
* )user_data
;
49 if(strcmp("system", element_name
)){
50 g_warning("This is not system.xml file\n");
54 while(attribute_names
[i
]){
55 if(strcmp("node_name", attribute_names
[i
])==0){
56 des
->node_name
= g_strdup(attribute_values
[i
]);
57 }else if(strcmp("domainname", attribute_names
[i
])==0){
58 des
->domain_name
= g_strdup(attribute_values
[i
]);
59 }else if(strcmp("cpu", attribute_names
[i
])==0){
60 des
->nb_cpu
= atoi(attribute_values
[i
]);
61 }else if(strcmp("arch_size", attribute_names
[i
])==0){
62 if(strcmp(attribute_values
[i
],"LP32") == 0) des
->size
= LTT_LP32
;
63 else if(strcmp(attribute_values
[i
],"ILP32") == 0) des
->size
= LTT_ILP32
;
64 else if(strcmp(attribute_values
[i
],"LP64") == 0) des
->size
= LTT_LP64
;
65 else if(strcmp(attribute_values
[i
],"ILP64") == 0) des
->size
= LTT_ILP64
;
66 else if(strcmp(attribute_values
[i
],"UNKNOWN") == 0) des
->size
= LTT_UNKNOWN
;
67 }else if(strcmp("endian", attribute_names
[i
])==0){
68 if(strcmp(attribute_values
[i
],"LITTLE_ENDIAN") == 0)
69 des
->endian
= LTT_LITTLE_ENDIAN
;
70 else if(strcmp(attribute_values
[i
],"BIG_ENDIAN") == 0)
71 des
->endian
= LTT_BIG_ENDIAN
;
72 }else if(strcmp("kernel_name", attribute_names
[i
])==0){
73 des
->kernel_name
= g_strdup(attribute_values
[i
]);
74 }else if(strcmp("kernel_release", attribute_names
[i
])==0){
75 des
->kernel_release
= g_strdup(attribute_values
[i
]);
76 }else if(strcmp("kernel_version", attribute_names
[i
])==0){
77 des
->kernel_version
= g_strdup(attribute_values
[i
]);
78 }else if(strcmp("machine", attribute_names
[i
])==0){
79 des
->machine
= g_strdup(attribute_values
[i
]);
80 }else if(strcmp("processor", attribute_names
[i
])==0){
81 des
->processor
= g_strdup(attribute_values
[i
]);
82 }else if(strcmp("hardware_platform", attribute_names
[i
])==0){
83 des
->hardware_platform
= g_strdup(attribute_values
[i
]);
84 }else if(strcmp("operating_system", attribute_names
[i
])==0){
85 des
->operating_system
= g_strdup(attribute_values
[i
]);
86 }else if(strcmp("ltt_major_version", attribute_names
[i
])==0){
87 des
->ltt_major_version
= atoi(attribute_values
[i
]);
88 }else if(strcmp("ltt_minor_version", attribute_names
[i
])==0){
89 des
->ltt_minor_version
= atoi(attribute_values
[i
]);
90 }else if(strcmp("ltt_block_size", attribute_names
[i
])==0){
91 des
->ltt_block_size
= atoi(attribute_values
[i
]);
93 g_warning("Not a valid attribute\n");
100 static void parser_end_element (GMarkupParseContext
*context
,
101 const gchar
*element_name
,
107 static void parser_characters (GMarkupParseContext
*context
,
113 LttSystemDescription
* des
= (LttSystemDescription
* )user_data
;
114 des
->description
= g_strdup(text
);
118 /*****************************************************************************
120 * ltt_tracefile_open : open a trace file, construct a LttTracefile
122 * t : the trace containing the tracefile
123 * fileName : path name of the trace file
125 * : a pointer to a tracefile
126 ****************************************************************************/
128 LttTracefile
* ltt_tracefile_open(LttTrace
* t
, char * fileName
)
131 struct stat lTDFStat
; /* Trace data file status */
132 BlockStart a_block_start
;
134 tf
= g_new(LttTracefile
, 1);
137 tf
->name
= g_strdup(fileName
);
139 tf
->fd
= open(fileName
, O_RDONLY
, 0);
141 g_error("Unable to open input data file %s\n", fileName
);
144 // Get the file's status
145 if(fstat(tf
->fd
, &lTDFStat
) < 0){
146 g_error("Unable to get the status of the input data file %s\n", fileName
);
149 // Is the file large enough to contain a trace
150 if(lTDFStat
.st_size
< sizeof(BlockStart
) + EVENT_HEADER_SIZE
){
151 g_print("The input data file %s does not contain a trace\n", fileName
);
158 //store the size of the file
159 tf
->file_size
= lTDFStat
.st_size
;
160 tf
->block_size
= t
->system_description
->ltt_block_size
;
161 tf
->block_number
= tf
->file_size
/ tf
->block_size
;
164 //allocate memory to contain the info of a block
165 tf
->buffer
= (void *) g_new(char, t
->system_description
->ltt_block_size
);
167 //read the first block
168 if(readBlock(tf
,1)) exit(1);
174 /*****************************************************************************
175 *Open control and per cpu tracefiles
176 ****************************************************************************/
178 void ltt_tracefile_open_cpu(LttTrace
*t
, char * tracefile_name
)
181 tf
= ltt_tracefile_open(t
,tracefile_name
);
183 t
->per_cpu_tracefile_number
++;
184 g_ptr_array_add(t
->per_cpu_tracefiles
, tf
);
187 void ltt_tracefile_open_control(LttTrace
*t
, char * control_name
)
197 tf
= ltt_tracefile_open(t
,control_name
);
199 t
->control_tracefile_number
++;
200 g_ptr_array_add(t
->control_tracefiles
,tf
);
202 //parse facilities tracefile to get base_id
203 if(strcmp(&control_name
[strlen(control_name
)-10],"facilities") ==0){
205 ev
= ltt_tracefile_read(tf
);
206 if(!ev
)return; // end of file
208 if(ev
->event_id
== TRACE_FACILITY_LOAD
){
210 fLoad
.name
= (char*)pos
;
211 fLoad
.checksum
= *(LttChecksum
*)(pos
+ strlen(fLoad
.name
));
212 fLoad
.base_code
= *(guint32
*)(pos
+ strlen(fLoad
.name
) + sizeof(LttChecksum
));
214 for(i
=0;i
<t
->facility_number
;i
++){
215 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
,i
);
216 if(strcmp(f
->name
,fLoad
.name
)==0 && fLoad
.checksum
==f
->checksum
){
217 f
->base_id
= fLoad
.base_code
;
221 if(i
==t
->facility_number
)
222 g_error("Facility: %s, checksum: %d is not founded\n",
223 fLoad
.name
,fLoad
.checksum
);
224 }else if(ev
->event_id
== TRACE_BLOCK_START
){
226 }else if(ev
->event_id
== TRACE_BLOCK_END
){
228 }else g_error("Not valid facilities trace file\n");
233 /*****************************************************************************
235 * ltt_tracefile_close: close a trace file,
237 * t : tracefile which will be closed
238 ****************************************************************************/
240 void ltt_tracefile_close(LttTracefile
*t
)
249 /*****************************************************************************
250 *Get system information
251 ****************************************************************************/
252 void getSystemInfo(LttSystemDescription
* des
, char * pathname
)
255 char buf
[DIR_NAME_SIZE
];
256 char description
[4*DIR_NAME_SIZE
];
258 GMarkupParseContext
* context
;
260 GMarkupParser markup_parser
=
262 parser_start_element
,
265 NULL
, /* passthrough */
269 fp
= fopen(pathname
,"r");
271 g_error("Can not open file : %s\n", pathname
);
274 context
= g_markup_parse_context_new(&markup_parser
, 0, des
,NULL
);
276 while(fgets(buf
,DIR_NAME_SIZE
, fp
) != NULL
){
277 if(!g_markup_parse_context_parse(context
, buf
, DIR_NAME_SIZE
, &error
)){
278 g_warning("Can not parse xml file: \n%s\n", error
->message
);
285 /*****************************************************************************
286 *The following functions get facility/tracefile information
287 ****************************************************************************/
289 void getFacilityInfo(LttTrace
*t
, char* eventdefs
)
292 struct dirent
*entry
;
297 char name
[DIR_NAME_SIZE
];
299 dir
= opendir(eventdefs
);
300 if(!dir
) g_error("Can not open directory: %s\n", eventdefs
);
302 while((entry
= readdir(dir
)) != NULL
){
303 ptr
= &entry
->d_name
[strlen(entry
->d_name
)-4];
304 if(strcmp(ptr
,".xml") != 0) continue;
305 strcpy(name
,eventdefs
);
306 strcat(name
,entry
->d_name
);
307 ltt_facility_open(t
,name
);
311 for(j
=0;j
<t
->facility_number
;j
++){
312 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, j
);
313 for(i
=0; i
<f
->event_number
; i
++){
315 setFieldsOffset(NULL
, et
, NULL
, t
);
320 void getControlFileInfo(LttTrace
*t
, char* control
)
323 struct dirent
*entry
;
324 char name
[DIR_NAME_SIZE
];
326 dir
= opendir(control
);
327 if(!dir
) g_error("Can not open directory: %s\n", control
);
329 while((entry
= readdir(dir
)) != NULL
){
330 if(strcmp(entry
->d_name
,"facilities") != 0 &&
331 strcmp(entry
->d_name
,"interrupts") != 0 &&
332 strcmp(entry
->d_name
,"processes") != 0) continue;
334 strcpy(name
,control
);
335 strcat(name
,entry
->d_name
);
336 ltt_tracefile_open_control(t
,name
);
341 void getCpuFileInfo(LttTrace
*t
, char* cpu
)
344 struct dirent
*entry
;
345 char name
[DIR_NAME_SIZE
];
348 if(!dir
) g_error("Can not open directory: %s\n", cpu
);
350 while((entry
= readdir(dir
)) != NULL
){
351 if(strcmp(entry
->d_name
,".") != 0 &&
352 strcmp(entry
->d_name
,"..") != 0 ){
354 strcat(name
,entry
->d_name
);
355 ltt_tracefile_open_cpu(t
,name
);
361 /*****************************************************************************
362 *A trace is specified as a pathname to the directory containing all the
363 *associated data (control tracefiles, per cpu tracefiles, event
366 *When a trace is closed, all the associated facilities, types and fields
367 *are released as well.
369 * MD : If pathname is already absolute, we do not add current working
372 ****************************************************************************/
374 void get_absolute_pathname(const char *pathname
, char * abs_pathname
)
377 size_t size
= DIR_NAME_SIZE
;
378 abs_pathname
[0] = '\0';
380 if(pathname
[0] == '/')
382 strcat(abs_pathname
, pathname
);
386 if(!getcwd(abs_pathname
, size
)){
387 g_warning("Can not get current working directory\n");
388 strcat(abs_pathname
, pathname
);
392 strcat(abs_pathname
,"/");
394 ptr
= (char*)pathname
;
396 while(*ptr
== '.' && *ptr1
== '.'){
400 strcat(abs_pathname
,ptr
);
403 LttTrace
*ltt_trace_open(const char *pathname
)
406 LttSystemDescription
* sys_description
;
407 char eventdefs
[DIR_NAME_SIZE
];
408 char info
[DIR_NAME_SIZE
];
409 char control
[DIR_NAME_SIZE
];
410 char cpu
[DIR_NAME_SIZE
];
411 char tmp
[DIR_NAME_SIZE
];
412 char abs_path
[DIR_NAME_SIZE
];
413 gboolean has_slash
= FALSE
;
415 get_absolute_pathname(pathname
, abs_path
);
417 //establish the pathname to different directories
418 if(abs_path
[strlen(abs_path
)-1] == '/')has_slash
= TRUE
;
419 strcpy(eventdefs
,abs_path
);
420 if(!has_slash
)strcat(eventdefs
,"/");
421 strcat(eventdefs
,"eventdefs/");
423 strcpy(info
,abs_path
);
424 if(!has_slash
)strcat(info
,"/");
425 strcat(info
,"info/");
427 strcpy(control
,abs_path
);
428 if(!has_slash
)strcat(control
,"/");
429 strcat(control
,"control/");
431 strcpy(cpu
,abs_path
);
432 if(!has_slash
)strcat(cpu
,"/");
436 t
= g_new(LttTrace
, 1);
437 sys_description
= g_new(LttSystemDescription
, 1);
438 t
->pathname
= g_strdup(abs_path
);
439 t
->facility_number
= 0;
440 t
->control_tracefile_number
= 0;
441 t
->per_cpu_tracefile_number
= 0;
442 t
->system_description
= sys_description
;
443 t
->control_tracefiles
= g_ptr_array_new();
444 t
->per_cpu_tracefiles
= g_ptr_array_new();
445 t
->facilities
= g_ptr_array_new();
446 getDataEndianType(&(t
->my_arch_size
), &(t
->my_arch_endian
));
448 //get system description
450 strcat(tmp
,"system.xml");
451 getSystemInfo(sys_description
, tmp
);
453 //get facilities info
454 getFacilityInfo(t
,eventdefs
);
456 //get control tracefile info
457 getControlFileInfo(t
,control
);
459 //get cpu tracefile info
460 getCpuFileInfo(t
,cpu
);
465 char * ltt_trace_name(LttTrace
*t
)
471 /******************************************************************************
472 * When we copy a trace, we want all the opening actions to happen again :
473 * the trace will be reopened and totally independant from the original.
474 * That's why we call ltt_trace_open.
475 *****************************************************************************/
476 LttTrace
*ltt_trace_copy(LttTrace
*self
)
478 return ltt_trace_open(self
->pathname
);
481 void ltt_trace_close(LttTrace
*t
)
489 //free system_description
490 g_free(t
->system_description
->description
);
491 g_free(t
->system_description
->node_name
);
492 g_free(t
->system_description
->domain_name
);
493 g_free(t
->system_description
->kernel_name
);
494 g_free(t
->system_description
->kernel_release
);
495 g_free(t
->system_description
->kernel_version
);
496 g_free(t
->system_description
->machine
);
497 g_free(t
->system_description
->processor
);
498 g_free(t
->system_description
->hardware_platform
);
499 g_free(t
->system_description
->operating_system
);
500 g_free(t
->system_description
);
502 //free control_tracefiles
503 for(i
=0;i
<t
->control_tracefile_number
;i
++){
504 tf
= (LttTracefile
*)g_ptr_array_index(t
->control_tracefiles
,i
);
505 ltt_tracefile_close(tf
);
507 g_ptr_array_free(t
->control_tracefiles
, TRUE
);
509 //free per_cpu_tracefiles
510 for(i
=0;i
<t
->per_cpu_tracefile_number
;i
++){
511 tf
= (LttTracefile
*)g_ptr_array_index(t
->per_cpu_tracefiles
,i
);
512 ltt_tracefile_close(tf
);
514 g_ptr_array_free(t
->per_cpu_tracefiles
, TRUE
);
517 for(i
=0;i
<t
->facility_number
;i
++){
518 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
,i
);
519 ltt_facility_close(f
);
521 g_ptr_array_free(t
->facilities
, TRUE
);
529 /*****************************************************************************
530 *Get the system description of the trace
531 ****************************************************************************/
533 LttSystemDescription
*ltt_trace_system_description(LttTrace
*t
)
535 return t
->system_description
;
538 /*****************************************************************************
539 * The following functions discover the facilities of the trace
540 ****************************************************************************/
542 unsigned ltt_trace_facility_number(LttTrace
*t
)
544 return (unsigned)(t
->facility_number
);
547 LttFacility
*ltt_trace_facility_get(LttTrace
*t
, unsigned i
)
549 return (LttFacility
*)g_ptr_array_index(t
->facilities
, i
);
552 /*****************************************************************************
554 * ltt_trace_facility_find : find facilities in the trace
557 * name : facility name
559 * position : position of the facility in the trace
561 * : the number of facilities
562 ****************************************************************************/
564 unsigned ltt_trace_facility_find(LttTrace
*t
, char *name
, unsigned *position
)
568 for(i
=0;i
<t
->facility_number
;i
++){
569 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, i
);
570 if(strcmp(f
->name
,name
)==0){
572 if(count
==1) *position
= i
;
580 /*****************************************************************************
581 * Functions to discover all the event types in the trace
582 ****************************************************************************/
584 unsigned ltt_trace_eventtype_number(LttTrace
*t
)
589 for(i
=0;i
<t
->facility_number
;i
++){
590 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, i
);
591 count
+= f
->event_number
;
596 LttFacility
* ltt_trace_facility_by_id(LttTrace
* trace
, unsigned id
)
598 LttFacility
* facility
;
600 for(i
=0;i
<trace
->facility_number
;i
++){
601 facility
= (LttFacility
*) g_ptr_array_index(trace
->facilities
,i
);
602 if(id
>= facility
->base_id
&&
603 id
< facility
->base_id
+ facility
->event_number
)
606 if(i
==trace
->facility_number
) return NULL
;
607 else return facility
;
610 LttEventType
*ltt_trace_eventtype_get(LttTrace
*t
, unsigned evId
)
613 f
= ltt_trace_facility_by_id(t
,evId
);
615 return f
->events
[evId
- f
->base_id
];
618 /*****************************************************************************
619 *There is one "per cpu" tracefile for each CPU, numbered from 0 to
620 *the maximum number of CPU in the system. When the number of CPU installed
621 *is less than the maximum, some positions are unused. There are also a
622 *number of "control" tracefiles (facilities, interrupts...).
623 ****************************************************************************/
624 unsigned ltt_trace_control_tracefile_number(LttTrace
*t
)
626 return t
->control_tracefile_number
;
629 unsigned ltt_trace_per_cpu_tracefile_number(LttTrace
*t
)
631 return t
->per_cpu_tracefile_number
;
634 /*****************************************************************************
635 *It is possible to search for the tracefiles by name or by CPU position.
636 *The index within the tracefiles of the same type is returned if found
637 *and a negative value otherwise.
638 ****************************************************************************/
640 int ltt_trace_control_tracefile_find(LttTrace
*t
, char *name
)
642 LttTracefile
* tracefile
;
644 for(i
=0;i
<t
->control_tracefile_number
;i
++){
645 tracefile
= (LttTracefile
*)g_ptr_array_index(t
->control_tracefiles
, i
);
646 if(strcmp(tracefile
->name
, name
)==0)break;
648 if(i
== t
->control_tracefile_number
) return -1;
652 int ltt_trace_per_cpu_tracefile_find(LttTrace
*t
, unsigned i
)
654 LttTracefile
* tracefile
;
656 for(j
=0;j
<t
->per_cpu_tracefile_number
;j
++){
657 tracefile
= (LttTracefile
*)g_ptr_array_index(t
->per_cpu_tracefiles
, j
);
658 name
= atoi(tracefile
->name
);
659 if(name
== (int)i
)break;
661 if(j
== t
->per_cpu_tracefile_number
) return -1;
665 /*****************************************************************************
666 *Get a specific tracefile
667 ****************************************************************************/
669 LttTracefile
*ltt_trace_control_tracefile_get(LttTrace
*t
, unsigned i
)
671 return (LttTracefile
*)g_ptr_array_index(t
->control_tracefiles
, i
);
674 LttTracefile
*ltt_trace_per_cpu_tracefile_get(LttTrace
*t
, unsigned i
)
676 return (LttTracefile
*)g_ptr_array_index(t
->per_cpu_tracefiles
, i
);
679 /*****************************************************************************
680 * Get the start time and end time of the trace
681 ****************************************************************************/
683 void ltt_trace_time_span_get(LttTrace
*t
, LttTime
*start
, LttTime
*end
)
685 LttTime startSmall
, startTmp
, endBig
, endTmp
;
689 for(i
=0;i
<t
->control_tracefile_number
;i
++){
690 tf
= g_ptr_array_index(t
->control_tracefiles
, i
);
692 startTmp
= tf
->a_block_start
->time
;
693 readBlock(tf
,tf
->block_number
);
694 endTmp
= tf
->a_block_end
->time
;
696 startSmall
= startTmp
;
701 if(ltt_time_compare(startSmall
,startTmp
) > 0) startSmall
= startTmp
;
702 if(ltt_time_compare(endBig
,endTmp
) < 0) endBig
= endTmp
;
705 for(i
=0;i
<t
->per_cpu_tracefile_number
;i
++){
706 tf
= g_ptr_array_index(t
->per_cpu_tracefiles
, i
);
708 startTmp
= tf
->a_block_start
->time
;
709 readBlock(tf
,tf
->block_number
);
710 endTmp
= tf
->a_block_end
->time
;
712 startSmall
= startTmp
;
716 if(ltt_time_compare(startSmall
,startTmp
) > 0) startSmall
= startTmp
;
717 if(ltt_time_compare(endBig
,endTmp
) < 0) endBig
= endTmp
;
725 /*****************************************************************************
726 *Get the name of a tracefile
727 ****************************************************************************/
729 char *ltt_tracefile_name(LttTracefile
*tf
)
734 /*****************************************************************************
735 * Get the number of blocks in the tracefile
736 ****************************************************************************/
738 unsigned ltt_tracefile_block_number(LttTracefile
*tf
)
740 return tf
->block_number
;
743 /*****************************************************************************
745 * ltt_tracefile_seek_time: seek to the first event of the trace with time
746 * larger or equal to time
749 * time : criteria of the time
750 ****************************************************************************/
751 void ltt_tracefile_find_time_block(LttTracefile
*t
, LttTime time
,
752 int start_block
, int end_block
)
754 int err
, tmp_block
, s
, e
;
758 err
=readBlock(t
,start_block
);
759 if(err
) g_error("Can not read tracefile: %s\n", t
->name
);
760 if(start_block
== end_block
)return;
762 tailTime
= ltt_time_compare(t
->a_block_end
->time
, time
);
763 if(tailTime
>= 0) return;
765 err
=readBlock(t
,end_block
);
766 if(err
) g_error("Can not read tracefile: %s\n", t
->name
);
767 if(start_block
+1 == end_block
)return;
769 headTime
= ltt_time_compare(t
->a_block_start
->time
, time
);
770 if(headTime
<= 0 ) return;
772 tmp_block
= (end_block
+ start_block
)/2;
773 err
=readBlock(t
,tmp_block
);
774 if(err
) g_error("Can not read tracefile: %s\n", t
->name
);
776 headTime
= ltt_time_compare(t
->a_block_start
->time
, time
);
777 tailTime
= ltt_time_compare(t
->a_block_end
->time
, time
);
778 if(headTime
<= 0 && tailTime
>= 0) return;
784 ltt_tracefile_find_time_block(t
, time
, s
, e
);
792 ltt_tracefile_find_time_block(t
, time
, s
, e
);
797 void ltt_tracefile_backward_find_time_block(LttTracefile
*t
, LttTime time
)
799 int t_time
, h_time
, err
;
800 err
=readBlock(t
,t
->which_block
-1);
801 if(err
) g_error("Can not read tracefile: %s\n", t
->name
);
802 h_time
= ltt_time_compare(t
->a_block_start
->time
, time
);
803 t_time
= ltt_time_compare(t
->a_block_end
->time
, time
);
806 if(t
->which_block
== 1) return;
807 err
=readBlock(t
,t
->which_block
-1);
808 if(err
) g_error("Can not read tracefile: %s\n", t
->name
);
809 tmp
= ltt_time_compare(t
->a_block_end
->time
, time
);
810 if(tmp
== 0) return ltt_tracefile_seek_time(t
, time
);
811 err
=readBlock(t
,t
->which_block
+1);
812 if(err
) g_error("Can not read tracefile: %s\n", t
->name
);
813 }else if(h_time
> 0){
814 ltt_tracefile_find_time_block(t
, time
, 1, t
->which_block
);
815 return ltt_tracefile_seek_time(t
, time
) ;
817 if(t_time
>= 0) return ltt_tracefile_seek_time(t
, time
);
818 err
=readBlock(t
,t
->which_block
+1);
819 if(err
) g_error("Can not read tracefile: %s\n", t
->name
);
823 void ltt_tracefile_seek_time(LttTracefile
*t
, LttTime time
)
827 int headTime
= ltt_time_compare(t
->a_block_start
->time
, time
);
828 int tailTime
= ltt_time_compare(t
->a_block_end
->time
, time
);
831 if(headTime
< 0 && tailTime
> 0){
832 if(ltt_time_compare(t
->a_block_end
->time
, t
->current_event_time
) !=0) {
833 lttTime
= getEventTime(t
);
834 err
= ltt_time_compare(lttTime
, time
);
836 if(t
->which_event
==2 || (&t
->prev_event_time
,&time
)<0){
840 return ltt_tracefile_seek_time(t
, time
);
844 ev
= ltt_tracefile_read(t
);
846 g_print("End of file\n");
849 lttTime
= getEventTime(t
);
850 err
= ltt_time_compare(lttTime
, time
);
854 }else{//we are at the end of the block
856 return ltt_tracefile_seek_time(t
, time
);
858 }else if(headTime
>= 0){
859 if(t
->which_block
== 1){
862 if(ltt_time_compare(t
->prev_block_end_time
, time
) >= 0 ||
863 (t
->prev_block_end_time
.tv_sec
== 0 &&
864 t
->prev_block_end_time
.tv_nsec
== 0 )){
865 ltt_tracefile_backward_find_time_block(t
, time
);
870 }else if(tailTime
< 0){
871 if(t
->which_block
!= t
->block_number
){
872 ltt_tracefile_find_time_block(t
, time
, t
->which_block
+1, t
->block_number
);
873 return ltt_tracefile_seek_time(t
, time
);
875 t
->cur_event_pos
= t
->buffer
+ t
->block_size
;
876 g_print("End of file\n");
879 }else if(tailTime
== 0){
880 t
->cur_event_pos
= t
->last_event_pos
;
881 t
->current_event_time
= time
;
882 t
->cur_heart_beat_number
= 0;
883 t
->prev_event_time
.tv_sec
= 0;
884 t
->prev_event_time
.tv_nsec
= 0;
889 /*****************************************************************************
890 * Seek to the first event with position equal or larger to ep
891 ****************************************************************************/
893 void ltt_tracefile_seek_position(LttTracefile
*t
, LttEventPosition
*ep
)
895 //if we are at the right place, just return
896 if(t
->which_block
== ep
->block_num
&& t
->which_event
== ep
->event_num
)
899 if(t
->which_block
== ep
->block_num
) updateTracefile(t
);
900 else readBlock(t
,ep
->block_num
);
902 //event offset is availiable
903 if(ep
->old_position
){
904 t
->cur_heart_beat_number
= ep
->heart_beat_number
;
905 t
->cur_event_pos
= t
->buffer
+ ep
->event_offset
;
909 //only block number and event index are availiable
910 while(t
->which_event
< ep
->event_num
) ltt_tracefile_read(t
);
915 /*****************************************************************************
917 * ltt_tracefile_read : read the current event, set the pointer to the next
921 * LttEvent * : an event to be processed
922 ****************************************************************************/
924 LttEvent
*ltt_tracefile_read(LttTracefile
*t
)
926 LttEvent
* lttEvent
= &t
->an_event
;
929 if(t
->cur_event_pos
== t
->buffer
+ t
->block_size
){
930 if(t
->which_block
== t
->block_number
){
933 err
= readBlock(t
, t
->which_block
+ 1);
934 if(err
)g_error("Can not read tracefile");
937 lttEvent
->event_id
= (int)(*(guint16
*)(t
->cur_event_pos
));
938 if(lttEvent
->event_id
== TRACE_TIME_HEARTBEAT
)
939 t
->cur_heart_beat_number
++;
941 t
->prev_event_time
= t
->current_event_time
;
942 // t->current_event_time = getEventTime(t);
944 lttEvent
->time_delta
= *(guint32
*)(t
->cur_event_pos
+ EVENT_ID_SIZE
);
945 lttEvent
->event_time
= t
->current_event_time
;
947 lttEvent
->tracefile
= t
;
948 lttEvent
->data
= t
->cur_event_pos
+ EVENT_HEADER_SIZE
;
949 lttEvent
->which_block
= t
->which_block
;
950 lttEvent
->which_event
= t
->which_event
;
952 //update the fields of the current event and go to the next event
954 if(err
== ERANGE
) g_error("event id is out of range\n");
956 lttEvent
->event_cycle_count
= t
->cur_cycle_count
;
961 /****************************************************************************
963 * readFile : wrap function to read from a file
965 * fd : file descriptor
966 * buf : buf to contain the content
967 * size : number of bytes to be read
968 * mesg : message to be printed if some thing goes wrong
971 * EIO : can not read from the file
972 ****************************************************************************/
974 int readFile(int fd
, void * buf
, size_t size
, char * mesg
)
977 nbBytes
= read(fd
, buf
, size
);
985 /****************************************************************************
987 * readBlock : read a block from the file
989 * lttdes : ltt trace file
990 * whichBlock : the block which will be read
993 * EINVAL : lseek fail
994 * EIO : can not read from the file
995 ****************************************************************************/
997 int readBlock(LttTracefile
* tf
, int whichBlock
)
1002 if(whichBlock
- tf
->which_block
== 1 && tf
->which_block
!= 0){
1003 tf
->prev_block_end_time
= tf
->a_block_end
->time
;
1004 tf
->prev_event_time
= tf
->a_block_end
->time
;
1006 tf
->prev_block_end_time
.tv_sec
= 0;
1007 tf
->prev_block_end_time
.tv_nsec
= 0;
1008 tf
->prev_event_time
.tv_sec
= 0;
1009 tf
->prev_event_time
.tv_nsec
= 0;
1012 nbBytes
=lseek(tf
->fd
,(off_t
)((whichBlock
-1)*tf
->block_size
), SEEK_SET
);
1013 if(nbBytes
== -1) return EINVAL
;
1015 if(readFile(tf
->fd
,tf
->buffer
,tf
->block_size
,"Unable to read a block"))
1018 tf
->a_block_start
=(BlockStart
*) (tf
->buffer
+ EVENT_HEADER_SIZE
);
1019 lostSize
= *(guint32
*)(tf
->buffer
+ tf
->block_size
- sizeof(guint32
));
1020 tf
->a_block_end
=(BlockEnd
*)(tf
->buffer
+ tf
->block_size
-
1021 lostSize
+ EVENT_HEADER_SIZE
);
1022 tf
->last_event_pos
= tf
->buffer
+ tf
->block_size
- lostSize
;
1024 tf
->which_block
= whichBlock
;
1025 tf
->which_event
= 1;
1026 tf
->cur_event_pos
= tf
->buffer
;//the beginning of the block, block start ev
1027 tf
->cur_heart_beat_number
= 0;
1029 getCyclePerNsec(tf
);
1031 tf
->current_event_time
= getEventTime(tf
);
1036 /*****************************************************************************
1038 * updateTracefile : reinitialize the info of the block which is already
1042 ****************************************************************************/
1044 void updateTracefile(LttTracefile
* tf
)
1046 tf
->which_event
= 1;
1047 tf
->cur_event_pos
= tf
->buffer
;
1048 tf
->current_event_time
= getEventTime(tf
);
1049 tf
->cur_heart_beat_number
= 0;
1051 tf
->prev_event_time
.tv_sec
= 0;
1052 tf
->prev_event_time
.tv_nsec
= 0;
1055 /*****************************************************************************
1057 * skipEvent : go to the next event, update the fields of the current event
1062 * ERANGE : event id is out of range
1063 ****************************************************************************/
1065 int skipEvent(LttTracefile
* t
)
1072 evId
= (int)(*(guint16
*)(t
->cur_event_pos
));
1073 evData
= t
->cur_event_pos
+ EVENT_HEADER_SIZE
;
1075 evT
= ltt_trace_eventtype_get(t
->trace
,(unsigned)evId
);
1077 if(evT
) rootFld
= evT
->root_field
;
1081 //event has string/sequence or the last event is not the same event
1082 if((evT
->latest_block
!=t
->which_block
|| evT
->latest_event
!=t
->which_event
)
1083 && rootFld
->field_fixed
== 0){
1084 setFieldsOffset(t
, evT
, evData
, t
->trace
);
1086 t
->cur_event_pos
+= EVENT_HEADER_SIZE
+ rootFld
->field_size
;
1087 }else t
->cur_event_pos
+= EVENT_HEADER_SIZE
;
1089 evT
->latest_block
= t
->which_block
;
1090 evT
->latest_event
= t
->which_event
;
1092 //the next event is in the next block
1093 if(evId
== TRACE_BLOCK_END
){
1094 t
->cur_event_pos
= t
->buffer
+ t
->block_size
;
1097 t
->current_event_time
= getEventTime(t
);
1103 /*****************************************************************************
1105 * getCyclePerNsec : calculate cycles per nsec for current block
1108 ****************************************************************************/
1110 void getCyclePerNsec(LttTracefile
* t
)
1112 LttTime lBufTotalTime
; /* Total time for this buffer */
1113 LttCycleCount lBufTotalNSec
; /* Total time for this buffer in nsecs */
1114 LttCycleCount lBufTotalCycle
;/* Total cycles for this buffer */
1116 /* Calculate the total time for this buffer */
1117 lBufTotalTime
= ltt_time_sub(t
->a_block_end
->time
, t
->a_block_start
->time
);
1119 /* Calculate the total cycles for this bufffer */
1120 lBufTotalCycle
= t
->a_block_end
->cycle_count
;
1121 lBufTotalCycle
-= t
->a_block_start
->cycle_count
;
1123 /* Convert the total time to nsecs */
1124 lBufTotalNSec
= lBufTotalTime
.tv_sec
;
1125 lBufTotalNSec
*= NANOSECONDS_PER_SECOND
;
1126 lBufTotalNSec
+= lBufTotalTime
.tv_nsec
;
1128 t
->cycle_per_nsec
= (double)lBufTotalCycle
/ (double)lBufTotalNSec
;
1131 /****************************************************************************
1133 * getEventTime : obtain the time of an event
1137 * LttTime : the time of the event
1138 ****************************************************************************/
1140 LttTime
getEventTime(LttTracefile
* tf
)
1143 LttCycleCount cycle_count
; // cycle count for the current event
1144 LttCycleCount lEventTotalCycle
; // Total cycles from start for event
1145 double lEventNSec
; // Total usecs from start for event
1146 LttTime lTimeOffset
; // Time offset in struct LttTime
1148 gint64 nanoSec
, tmpCycleCount
= (((guint64
)1)<<32);
1150 evId
= *(guint16
*)tf
->cur_event_pos
;
1151 if(evId
== TRACE_BLOCK_START
){
1153 tf
->pre_cycle_count
= 0;
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
){
1158 tf
->pre_cycle_count
= 0;
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
< tf
->pre_cycle_count
)tf
->count
++;
1167 tf
->pre_cycle_count
= cycle_count
;
1168 cycle_count
+= tmpCycleCount
* tf
->count
;
1170 if(tf
->cur_heart_beat_number
> tf
->count
)
1171 cycle_count
+= tmpCycleCount
* (tf
->cur_heart_beat_number
- tf
->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
;