1 /* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2003-2004 Xiangxiu Yang
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License Version 2 as
6 * published by the Free Software Foundation;
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22 #include <sys/types.h>
24 #include <linux/errno.h>
28 #include "ltt-private.h"
29 #include <ltt/trace.h>
30 #include <ltt/facility.h>
32 #define DIR_NAME_SIZE 256
34 /* set the offset of the fields belonging to the event,
35 need the information of the archecture */
36 void setFieldsOffset(LttTracefile
*tf
,LttEventType
*evT
,void *evD
,LttTrace
*t
);
38 /* get the size of the field type according to the archtecture's
39 size and endian type(info of the archecture) */
40 int getFieldtypeSize(LttTracefile
* tf
, LttEventType
* evT
, int offsetRoot
,
41 int offsetParent
, LttField
*fld
, void *evD
, LttTrace
* t
);
43 /* read a fixed size or a block information from the file (fd) */
44 int readFile(int fd
, void * buf
, size_t size
, char * mesg
);
45 int readBlock(LttTracefile
* tf
, int whichBlock
);
47 /* calculate cycles per nsec for current block */
48 void getCyclePerNsec(LttTracefile
* t
);
50 /* reinitialize the info of the block which is already in the buffer */
51 void updateTracefile(LttTracefile
* tf
);
53 /* go to the next event */
54 int skipEvent(LttTracefile
* t
);
57 /* Functions to parse system.xml file (using glib xml parser) */
58 static void parser_start_element (GMarkupParseContext
*context
,
59 const gchar
*element_name
,
60 const gchar
**attribute_names
,
61 const gchar
**attribute_values
,
66 LttSystemDescription
* des
= (LttSystemDescription
* )user_data
;
67 if(strcmp("system", element_name
)){
68 g_warning("This is not system.xml file\n");
72 while(attribute_names
[i
]){
73 if(strcmp("node_name", attribute_names
[i
])==0){
74 des
->node_name
= g_strdup(attribute_values
[i
]);
75 }else if(strcmp("domainname", attribute_names
[i
])==0){
76 des
->domain_name
= g_strdup(attribute_values
[i
]);
77 }else if(strcmp("cpu", attribute_names
[i
])==0){
78 des
->nb_cpu
= atoi(attribute_values
[i
]);
79 }else if(strcmp("arch_size", attribute_names
[i
])==0){
80 if(strcmp(attribute_values
[i
],"LP32") == 0) des
->size
= LTT_LP32
;
81 else if(strcmp(attribute_values
[i
],"ILP32") == 0) des
->size
= LTT_ILP32
;
82 else if(strcmp(attribute_values
[i
],"LP64") == 0) des
->size
= LTT_LP64
;
83 else if(strcmp(attribute_values
[i
],"ILP64") == 0) des
->size
= LTT_ILP64
;
84 else if(strcmp(attribute_values
[i
],"UNKNOWN") == 0) des
->size
= LTT_UNKNOWN
;
85 }else if(strcmp("endian", attribute_names
[i
])==0){
86 if(strcmp(attribute_values
[i
],"LITTLE_ENDIAN") == 0)
87 des
->endian
= LTT_LITTLE_ENDIAN
;
88 else if(strcmp(attribute_values
[i
],"BIG_ENDIAN") == 0)
89 des
->endian
= LTT_BIG_ENDIAN
;
90 }else if(strcmp("kernel_name", attribute_names
[i
])==0){
91 des
->kernel_name
= g_strdup(attribute_values
[i
]);
92 }else if(strcmp("kernel_release", attribute_names
[i
])==0){
93 des
->kernel_release
= g_strdup(attribute_values
[i
]);
94 }else if(strcmp("kernel_version", attribute_names
[i
])==0){
95 des
->kernel_version
= g_strdup(attribute_values
[i
]);
96 }else if(strcmp("machine", attribute_names
[i
])==0){
97 des
->machine
= g_strdup(attribute_values
[i
]);
98 }else if(strcmp("processor", attribute_names
[i
])==0){
99 des
->processor
= g_strdup(attribute_values
[i
]);
100 }else if(strcmp("hardware_platform", attribute_names
[i
])==0){
101 des
->hardware_platform
= g_strdup(attribute_values
[i
]);
102 }else if(strcmp("operating_system", attribute_names
[i
])==0){
103 des
->operating_system
= g_strdup(attribute_values
[i
]);
104 }else if(strcmp("ltt_major_version", attribute_names
[i
])==0){
105 des
->ltt_major_version
= atoi(attribute_values
[i
]);
106 }else if(strcmp("ltt_minor_version", attribute_names
[i
])==0){
107 des
->ltt_minor_version
= atoi(attribute_values
[i
]);
108 }else if(strcmp("ltt_block_size", attribute_names
[i
])==0){
109 des
->ltt_block_size
= atoi(attribute_values
[i
]);
111 g_warning("Not a valid attribute\n");
118 static void parser_end_element (GMarkupParseContext
*context
,
119 const gchar
*element_name
,
125 static void parser_characters (GMarkupParseContext
*context
,
131 LttSystemDescription
* des
= (LttSystemDescription
* )user_data
;
132 des
->description
= g_strdup(text
);
136 /*****************************************************************************
138 * ltt_tracefile_open : open a trace file, construct a LttTracefile
140 * t : the trace containing the tracefile
141 * fileName : path name of the trace file
143 * : a pointer to a tracefile
144 ****************************************************************************/
146 LttTracefile
* ltt_tracefile_open(LttTrace
* t
, char * fileName
)
149 struct stat lTDFStat
; /* Trace data file status */
150 BlockStart a_block_start
;
152 tf
= g_new(LttTracefile
, 1);
155 tf
->name
= g_strdup(fileName
);
157 tf
->fd
= open(fileName
, O_RDONLY
, 0);
159 g_error("Unable to open input data file %s\n", fileName
);
162 // Get the file's status
163 if(fstat(tf
->fd
, &lTDFStat
) < 0){
164 g_error("Unable to get the status of the input data file %s\n", fileName
);
167 // Is the file large enough to contain a trace
168 if(lTDFStat
.st_size
< sizeof(BlockStart
) + EVENT_HEADER_SIZE
){
169 g_print("The input data file %s does not contain a trace\n", fileName
);
176 //store the size of the file
177 tf
->file_size
= lTDFStat
.st_size
;
178 tf
->block_size
= t
->system_description
->ltt_block_size
;
179 tf
->block_number
= tf
->file_size
/ tf
->block_size
;
182 //allocate memory to contain the info of a block
183 tf
->buffer
= (void *) g_new(char, t
->system_description
->ltt_block_size
);
185 //read the first block
186 if(readBlock(tf
,1)) exit(1);
192 /*****************************************************************************
193 *Open control and per cpu tracefiles
194 ****************************************************************************/
196 void ltt_tracefile_open_cpu(LttTrace
*t
, char * tracefile_name
)
199 tf
= ltt_tracefile_open(t
,tracefile_name
);
201 t
->per_cpu_tracefile_number
++;
202 g_ptr_array_add(t
->per_cpu_tracefiles
, tf
);
205 void ltt_tracefile_open_control(LttTrace
*t
, char * control_name
)
215 tf
= ltt_tracefile_open(t
,control_name
);
217 t
->control_tracefile_number
++;
218 g_ptr_array_add(t
->control_tracefiles
,tf
);
220 //parse facilities tracefile to get base_id
221 if(strcmp(&control_name
[strlen(control_name
)-10],"facilities") ==0){
223 ev
= ltt_tracefile_read(tf
);
224 if(!ev
)return; // end of file
226 if(ev
->event_id
== TRACE_FACILITY_LOAD
){
228 fLoad
.name
= (char*)pos
;
229 fLoad
.checksum
= *(LttChecksum
*)(pos
+ strlen(fLoad
.name
));
230 fLoad
.base_code
= *(guint32
*)(pos
+ strlen(fLoad
.name
) + sizeof(LttChecksum
));
232 for(i
=0;i
<t
->facility_number
;i
++){
233 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
,i
);
234 if(strcmp(f
->name
,fLoad
.name
)==0 && fLoad
.checksum
==f
->checksum
){
235 f
->base_id
= fLoad
.base_code
;
239 if(i
==t
->facility_number
)
240 g_error("Facility: %s, checksum: %d is not founded\n",
241 fLoad
.name
,fLoad
.checksum
);
242 }else if(ev
->event_id
== TRACE_BLOCK_START
){
244 }else if(ev
->event_id
== TRACE_BLOCK_END
){
246 }else g_error("Not valid facilities trace file\n");
251 /*****************************************************************************
253 * ltt_tracefile_close: close a trace file,
255 * t : tracefile which will be closed
256 ****************************************************************************/
258 void ltt_tracefile_close(LttTracefile
*t
)
267 /*****************************************************************************
268 *Get system information
269 ****************************************************************************/
270 void getSystemInfo(LttSystemDescription
* des
, char * pathname
)
273 char buf
[DIR_NAME_SIZE
];
274 char description
[4*DIR_NAME_SIZE
];
276 GMarkupParseContext
* context
;
278 GMarkupParser markup_parser
=
280 parser_start_element
,
283 NULL
, /* passthrough */
287 fp
= fopen(pathname
,"r");
289 g_error("Can not open file : %s\n", pathname
);
292 context
= g_markup_parse_context_new(&markup_parser
, 0, des
,NULL
);
294 while(fgets(buf
,DIR_NAME_SIZE
, fp
) != NULL
){
295 if(!g_markup_parse_context_parse(context
, buf
, DIR_NAME_SIZE
, &error
)){
296 g_warning("Can not parse xml file: \n%s\n", error
->message
);
303 /*****************************************************************************
304 *The following functions get facility/tracefile information
305 ****************************************************************************/
307 void getFacilityInfo(LttTrace
*t
, char* eventdefs
)
310 struct dirent
*entry
;
315 char name
[DIR_NAME_SIZE
];
317 dir
= opendir(eventdefs
);
318 if(!dir
) g_error("Can not open directory: %s\n", eventdefs
);
320 while((entry
= readdir(dir
)) != NULL
){
321 ptr
= &entry
->d_name
[strlen(entry
->d_name
)-4];
322 if(strcmp(ptr
,".xml") != 0) continue;
323 strcpy(name
,eventdefs
);
324 strcat(name
,entry
->d_name
);
325 ltt_facility_open(t
,name
);
329 for(j
=0;j
<t
->facility_number
;j
++){
330 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, j
);
331 for(i
=0; i
<f
->event_number
; i
++){
333 setFieldsOffset(NULL
, et
, NULL
, t
);
338 void getControlFileInfo(LttTrace
*t
, char* control
)
341 struct dirent
*entry
;
342 char name
[DIR_NAME_SIZE
];
344 dir
= opendir(control
);
345 if(!dir
) g_error("Can not open directory: %s\n", control
);
347 while((entry
= readdir(dir
)) != NULL
){
348 if(strcmp(entry
->d_name
,"facilities") != 0 &&
349 strcmp(entry
->d_name
,"interrupts") != 0 &&
350 strcmp(entry
->d_name
,"processes") != 0) continue;
352 strcpy(name
,control
);
353 strcat(name
,entry
->d_name
);
354 ltt_tracefile_open_control(t
,name
);
359 void getCpuFileInfo(LttTrace
*t
, char* cpu
)
362 struct dirent
*entry
;
363 char name
[DIR_NAME_SIZE
];
366 if(!dir
) g_error("Can not open directory: %s\n", cpu
);
368 while((entry
= readdir(dir
)) != NULL
){
369 if(strcmp(entry
->d_name
,".") != 0 &&
370 strcmp(entry
->d_name
,"..") != 0 &&
371 strcmp(entry
->d_name
,".svn") != 0){
373 strcat(name
,entry
->d_name
);
374 ltt_tracefile_open_cpu(t
,name
);
380 /*****************************************************************************
381 *A trace is specified as a pathname to the directory containing all the
382 *associated data (control tracefiles, per cpu tracefiles, event
385 *When a trace is closed, all the associated facilities, types and fields
386 *are released as well.
388 * MD : If pathname is already absolute, we do not add current working
391 ****************************************************************************/
393 void get_absolute_pathname(const char *pathname
, char * abs_pathname
)
396 size_t size
= DIR_NAME_SIZE
;
397 abs_pathname
[0] = '\0';
399 if(pathname
[0] == '/')
401 strcat(abs_pathname
, pathname
);
405 if(!getcwd(abs_pathname
, size
)){
406 g_warning("Can not get current working directory\n");
407 strcat(abs_pathname
, pathname
);
411 strcat(abs_pathname
,"/");
413 ptr
= (char*)pathname
;
415 while(*ptr
== '.' && *ptr1
== '.'){
419 strcat(abs_pathname
,ptr
);
422 LttTrace
*ltt_trace_open(const char *pathname
)
425 LttSystemDescription
* sys_description
;
426 char eventdefs
[DIR_NAME_SIZE
];
427 char info
[DIR_NAME_SIZE
];
428 char control
[DIR_NAME_SIZE
];
429 char cpu
[DIR_NAME_SIZE
];
430 char tmp
[DIR_NAME_SIZE
];
431 char abs_path
[DIR_NAME_SIZE
];
432 gboolean has_slash
= FALSE
;
434 get_absolute_pathname(pathname
, abs_path
);
436 //establish the pathname to different directories
437 if(abs_path
[strlen(abs_path
)-1] == '/')has_slash
= TRUE
;
438 strcpy(eventdefs
,abs_path
);
439 if(!has_slash
)strcat(eventdefs
,"/");
440 strcat(eventdefs
,"eventdefs/");
442 strcpy(info
,abs_path
);
443 if(!has_slash
)strcat(info
,"/");
444 strcat(info
,"info/");
446 strcpy(control
,abs_path
);
447 if(!has_slash
)strcat(control
,"/");
448 strcat(control
,"control/");
450 strcpy(cpu
,abs_path
);
451 if(!has_slash
)strcat(cpu
,"/");
455 t
= g_new(LttTrace
, 1);
456 sys_description
= g_new(LttSystemDescription
, 1);
457 t
->pathname
= g_strdup(abs_path
);
458 t
->facility_number
= 0;
459 t
->control_tracefile_number
= 0;
460 t
->per_cpu_tracefile_number
= 0;
461 t
->system_description
= sys_description
;
462 t
->control_tracefiles
= g_ptr_array_new();
463 t
->per_cpu_tracefiles
= g_ptr_array_new();
464 t
->facilities
= g_ptr_array_new();
465 getDataEndianType(&(t
->my_arch_size
), &(t
->my_arch_endian
));
467 //get system description
469 strcat(tmp
,"system.xml");
470 getSystemInfo(sys_description
, tmp
);
472 //get facilities info
473 getFacilityInfo(t
,eventdefs
);
475 //get control tracefile info
476 getControlFileInfo(t
,control
);
478 //get cpu tracefile info
479 getCpuFileInfo(t
,cpu
);
484 char * ltt_trace_name(LttTrace
*t
)
490 /******************************************************************************
491 * When we copy a trace, we want all the opening actions to happen again :
492 * the trace will be reopened and totally independant from the original.
493 * That's why we call ltt_trace_open.
494 *****************************************************************************/
495 LttTrace
*ltt_trace_copy(LttTrace
*self
)
497 return ltt_trace_open(self
->pathname
);
500 void ltt_trace_close(LttTrace
*t
)
508 //free system_description
509 g_free(t
->system_description
->description
);
510 g_free(t
->system_description
->node_name
);
511 g_free(t
->system_description
->domain_name
);
512 g_free(t
->system_description
->kernel_name
);
513 g_free(t
->system_description
->kernel_release
);
514 g_free(t
->system_description
->kernel_version
);
515 g_free(t
->system_description
->machine
);
516 g_free(t
->system_description
->processor
);
517 g_free(t
->system_description
->hardware_platform
);
518 g_free(t
->system_description
->operating_system
);
519 g_free(t
->system_description
);
521 //free control_tracefiles
522 for(i
=0;i
<t
->control_tracefile_number
;i
++){
523 tf
= (LttTracefile
*)g_ptr_array_index(t
->control_tracefiles
,i
);
524 ltt_tracefile_close(tf
);
526 g_ptr_array_free(t
->control_tracefiles
, TRUE
);
528 //free per_cpu_tracefiles
529 for(i
=0;i
<t
->per_cpu_tracefile_number
;i
++){
530 tf
= (LttTracefile
*)g_ptr_array_index(t
->per_cpu_tracefiles
,i
);
531 ltt_tracefile_close(tf
);
533 g_ptr_array_free(t
->per_cpu_tracefiles
, TRUE
);
536 for(i
=0;i
<t
->facility_number
;i
++){
537 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
,i
);
538 ltt_facility_close(f
);
540 g_ptr_array_free(t
->facilities
, TRUE
);
548 /*****************************************************************************
549 *Get the system description of the trace
550 ****************************************************************************/
552 LttSystemDescription
*ltt_trace_system_description(LttTrace
*t
)
554 return t
->system_description
;
557 /*****************************************************************************
558 * The following functions discover the facilities of the trace
559 ****************************************************************************/
561 unsigned ltt_trace_facility_number(LttTrace
*t
)
563 return (unsigned)(t
->facility_number
);
566 LttFacility
*ltt_trace_facility_get(LttTrace
*t
, unsigned i
)
568 return (LttFacility
*)g_ptr_array_index(t
->facilities
, i
);
571 /*****************************************************************************
573 * ltt_trace_facility_find : find facilities in the trace
576 * name : facility name
578 * position : position of the facility in the trace
580 * : the number of facilities
581 ****************************************************************************/
583 unsigned ltt_trace_facility_find(LttTrace
*t
, char *name
, unsigned *position
)
587 for(i
=0;i
<t
->facility_number
;i
++){
588 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, i
);
589 if(strcmp(f
->name
,name
)==0){
591 if(count
==1) *position
= i
;
599 /*****************************************************************************
600 * Functions to discover all the event types in the trace
601 ****************************************************************************/
603 unsigned ltt_trace_eventtype_number(LttTrace
*t
)
608 for(i
=0;i
<t
->facility_number
;i
++){
609 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, i
);
610 count
+= f
->event_number
;
615 LttFacility
* ltt_trace_facility_by_id(LttTrace
* trace
, unsigned id
)
617 LttFacility
* facility
;
619 for(i
=0;i
<trace
->facility_number
;i
++){
620 facility
= (LttFacility
*) g_ptr_array_index(trace
->facilities
,i
);
621 if(id
>= facility
->base_id
&&
622 id
< facility
->base_id
+ facility
->event_number
)
625 if(i
==trace
->facility_number
) return NULL
;
626 else return facility
;
629 LttEventType
*ltt_trace_eventtype_get(LttTrace
*t
, unsigned evId
)
632 f
= ltt_trace_facility_by_id(t
,evId
);
634 return f
->events
[evId
- f
->base_id
];
637 /*****************************************************************************
638 *There is one "per cpu" tracefile for each CPU, numbered from 0 to
639 *the maximum number of CPU in the system. When the number of CPU installed
640 *is less than the maximum, some positions are unused. There are also a
641 *number of "control" tracefiles (facilities, interrupts...).
642 ****************************************************************************/
643 unsigned ltt_trace_control_tracefile_number(LttTrace
*t
)
645 return t
->control_tracefile_number
;
648 unsigned ltt_trace_per_cpu_tracefile_number(LttTrace
*t
)
650 return t
->per_cpu_tracefile_number
;
653 /*****************************************************************************
654 *It is possible to search for the tracefiles by name or by CPU position.
655 *The index within the tracefiles of the same type is returned if found
656 *and a negative value otherwise.
657 ****************************************************************************/
659 int ltt_trace_control_tracefile_find(LttTrace
*t
, char *name
)
661 LttTracefile
* tracefile
;
663 for(i
=0;i
<t
->control_tracefile_number
;i
++){
664 tracefile
= (LttTracefile
*)g_ptr_array_index(t
->control_tracefiles
, i
);
665 if(strcmp(tracefile
->name
, name
)==0)break;
667 if(i
== t
->control_tracefile_number
) return -1;
671 int ltt_trace_per_cpu_tracefile_find(LttTrace
*t
, unsigned i
)
673 LttTracefile
* tracefile
;
675 for(j
=0;j
<t
->per_cpu_tracefile_number
;j
++){
676 tracefile
= (LttTracefile
*)g_ptr_array_index(t
->per_cpu_tracefiles
, j
);
677 name
= atoi(tracefile
->name
);
678 if(name
== (int)i
)break;
680 if(j
== t
->per_cpu_tracefile_number
) return -1;
684 /*****************************************************************************
685 *Get a specific tracefile
686 ****************************************************************************/
688 LttTracefile
*ltt_trace_control_tracefile_get(LttTrace
*t
, unsigned i
)
690 return (LttTracefile
*)g_ptr_array_index(t
->control_tracefiles
, i
);
693 LttTracefile
*ltt_trace_per_cpu_tracefile_get(LttTrace
*t
, unsigned i
)
695 return (LttTracefile
*)g_ptr_array_index(t
->per_cpu_tracefiles
, i
);
698 /*****************************************************************************
699 * Get the start time and end time of the trace
700 ****************************************************************************/
702 void ltt_trace_time_span_get(LttTrace
*t
, LttTime
*start
, LttTime
*end
)
704 LttTime startSmall
, startTmp
, endBig
, endTmp
;
708 for(i
=0;i
<t
->control_tracefile_number
;i
++){
709 tf
= g_ptr_array_index(t
->control_tracefiles
, i
);
711 startTmp
= tf
->a_block_start
->time
;
712 readBlock(tf
,tf
->block_number
);
713 endTmp
= tf
->a_block_end
->time
;
715 startSmall
= startTmp
;
720 if(ltt_time_compare(startSmall
,startTmp
) > 0) startSmall
= startTmp
;
721 if(ltt_time_compare(endBig
,endTmp
) < 0) endBig
= endTmp
;
724 for(i
=0;i
<t
->per_cpu_tracefile_number
;i
++){
725 tf
= g_ptr_array_index(t
->per_cpu_tracefiles
, i
);
727 startTmp
= tf
->a_block_start
->time
;
728 readBlock(tf
,tf
->block_number
);
729 endTmp
= tf
->a_block_end
->time
;
731 startSmall
= startTmp
;
735 if(ltt_time_compare(startSmall
,startTmp
) > 0) startSmall
= startTmp
;
736 if(ltt_time_compare(endBig
,endTmp
) < 0) endBig
= endTmp
;
744 /*****************************************************************************
745 *Get the name of a tracefile
746 ****************************************************************************/
748 char *ltt_tracefile_name(LttTracefile
*tf
)
753 /*****************************************************************************
754 * Get the number of blocks in the tracefile
755 ****************************************************************************/
757 unsigned ltt_tracefile_block_number(LttTracefile
*tf
)
759 return tf
->block_number
;
762 /*****************************************************************************
764 * ltt_tracefile_seek_time: seek to the first event of the trace with time
765 * larger or equal to time
768 * time : criteria of the time
769 ****************************************************************************/
770 void ltt_tracefile_find_time_block(LttTracefile
*t
, LttTime time
,
771 int start_block
, int end_block
)
773 int err
, tmp_block
, s
, e
;
777 err
=readBlock(t
,start_block
);
778 if(err
) g_error("Can not read tracefile: %s\n", t
->name
);
779 if(start_block
== end_block
)return;
781 tailTime
= ltt_time_compare(t
->a_block_end
->time
, time
);
782 if(tailTime
>= 0) return;
784 err
=readBlock(t
,end_block
);
785 if(err
) g_error("Can not read tracefile: %s\n", t
->name
);
786 if(start_block
+1 == end_block
)return;
788 headTime
= ltt_time_compare(t
->a_block_start
->time
, time
);
789 if(headTime
<= 0 ) return;
791 tmp_block
= (end_block
+ start_block
)/2;
792 err
=readBlock(t
,tmp_block
);
793 if(err
) g_error("Can not read tracefile: %s\n", t
->name
);
795 headTime
= ltt_time_compare(t
->a_block_start
->time
, time
);
796 tailTime
= ltt_time_compare(t
->a_block_end
->time
, time
);
797 if(headTime
<= 0 && tailTime
>= 0) return;
803 ltt_tracefile_find_time_block(t
, time
, s
, e
);
811 ltt_tracefile_find_time_block(t
, time
, s
, e
);
816 void ltt_tracefile_backward_find_time_block(LttTracefile
*t
, LttTime time
)
818 int t_time
, h_time
, err
;
819 err
=readBlock(t
,t
->which_block
-1);
820 if(err
) g_error("Can not read tracefile: %s\n", t
->name
);
821 h_time
= ltt_time_compare(t
->a_block_start
->time
, time
);
822 t_time
= ltt_time_compare(t
->a_block_end
->time
, time
);
825 if(t
->which_block
== 1) return;
826 err
=readBlock(t
,t
->which_block
-1);
827 if(err
) g_error("Can not read tracefile: %s\n", t
->name
);
828 tmp
= ltt_time_compare(t
->a_block_end
->time
, time
);
829 if(tmp
== 0) return ltt_tracefile_seek_time(t
, time
);
830 err
=readBlock(t
,t
->which_block
+1);
831 if(err
) g_error("Can not read tracefile: %s\n", t
->name
);
832 }else if(h_time
> 0){
833 ltt_tracefile_find_time_block(t
, time
, 1, t
->which_block
);
834 return ltt_tracefile_seek_time(t
, time
) ;
836 if(t_time
>= 0) return ltt_tracefile_seek_time(t
, time
);
837 err
=readBlock(t
,t
->which_block
+1);
838 if(err
) g_error("Can not read tracefile: %s\n", t
->name
);
842 void ltt_tracefile_seek_time(LttTracefile
*t
, LttTime time
)
846 int headTime
= ltt_time_compare(t
->a_block_start
->time
, time
);
847 int tailTime
= ltt_time_compare(t
->a_block_end
->time
, time
);
850 if(headTime
< 0 && tailTime
> 0){
851 if(ltt_time_compare(t
->a_block_end
->time
, t
->current_event_time
) !=0) {
852 lttTime
= getEventTime(t
);
853 err
= ltt_time_compare(lttTime
, time
);
855 if(t
->which_event
==2 || (&t
->prev_event_time
,&time
)<0){
859 return ltt_tracefile_seek_time(t
, time
);
863 ev
= ltt_tracefile_read(t
);
865 g_print("End of file\n");
868 lttTime
= getEventTime(t
);
869 err
= ltt_time_compare(lttTime
, time
);
873 }else{//we are at the end of the block
875 return ltt_tracefile_seek_time(t
, time
);
877 }else if(headTime
>= 0){
878 if(t
->which_block
== 1){
881 if(ltt_time_compare(t
->prev_block_end_time
, time
) >= 0 ||
882 (t
->prev_block_end_time
.tv_sec
== 0 &&
883 t
->prev_block_end_time
.tv_nsec
== 0 )){
884 ltt_tracefile_backward_find_time_block(t
, time
);
889 }else if(tailTime
< 0){
890 if(t
->which_block
!= t
->block_number
){
891 ltt_tracefile_find_time_block(t
, time
, t
->which_block
+1, t
->block_number
);
892 return ltt_tracefile_seek_time(t
, time
);
894 t
->cur_event_pos
= t
->buffer
+ t
->block_size
;
895 g_print("End of file\n");
898 }else if(tailTime
== 0){
899 t
->cur_event_pos
= t
->last_event_pos
;
900 t
->current_event_time
= time
;
901 t
->cur_heart_beat_number
= 0;
902 t
->prev_event_time
.tv_sec
= 0;
903 t
->prev_event_time
.tv_nsec
= 0;
908 /*****************************************************************************
909 * Seek to the first event with position equal or larger to ep
910 ****************************************************************************/
912 void ltt_tracefile_seek_position(LttTracefile
*t
, LttEventPosition
*ep
)
914 //if we are at the right place, just return
915 if(t
->which_block
== ep
->block_num
&& t
->which_event
== ep
->event_num
)
918 if(t
->which_block
== ep
->block_num
) updateTracefile(t
);
919 else readBlock(t
,ep
->block_num
);
921 //event offset is availiable
922 if(ep
->old_position
){
923 t
->cur_heart_beat_number
= ep
->heart_beat_number
;
924 t
->cur_event_pos
= t
->buffer
+ ep
->event_offset
;
928 //only block number and event index are availiable
929 while(t
->which_event
< ep
->event_num
) ltt_tracefile_read(t
);
934 /*****************************************************************************
936 * ltt_tracefile_read : read the current event, set the pointer to the next
940 * LttEvent * : an event to be processed
941 ****************************************************************************/
943 LttEvent
*ltt_tracefile_read(LttTracefile
*t
)
945 LttEvent
* lttEvent
= &t
->an_event
;
948 if(t
->cur_event_pos
== t
->buffer
+ t
->block_size
){
949 if(t
->which_block
== t
->block_number
){
952 err
= readBlock(t
, t
->which_block
+ 1);
953 if(err
)g_error("Can not read tracefile");
956 lttEvent
->event_id
= (int)(*(guint16
*)(t
->cur_event_pos
));
957 if(lttEvent
->event_id
== TRACE_TIME_HEARTBEAT
)
958 t
->cur_heart_beat_number
++;
960 t
->prev_event_time
= t
->current_event_time
;
961 // t->current_event_time = getEventTime(t);
963 lttEvent
->time_delta
= *(guint32
*)(t
->cur_event_pos
+ EVENT_ID_SIZE
);
964 lttEvent
->event_time
= t
->current_event_time
;
965 lttEvent
->event_cycle_count
= t
->cur_cycle_count
;
967 lttEvent
->tracefile
= t
;
968 lttEvent
->data
= t
->cur_event_pos
+ EVENT_HEADER_SIZE
;
969 lttEvent
->which_block
= t
->which_block
;
970 lttEvent
->which_event
= t
->which_event
;
972 //update the fields of the current event and go to the next event
974 if(err
== ERANGE
) g_error("event id is out of range\n");
979 /****************************************************************************
981 * readFile : wrap function to read from a file
983 * fd : file descriptor
984 * buf : buf to contain the content
985 * size : number of bytes to be read
986 * mesg : message to be printed if some thing goes wrong
989 * EIO : can not read from the file
990 ****************************************************************************/
992 int readFile(int fd
, void * buf
, size_t size
, char * mesg
)
995 nbBytes
= read(fd
, buf
, size
);
1003 /****************************************************************************
1005 * readBlock : read a block from the file
1007 * lttdes : ltt trace file
1008 * whichBlock : the block which will be read
1011 * EINVAL : lseek fail
1012 * EIO : can not read from the file
1013 ****************************************************************************/
1015 int readBlock(LttTracefile
* tf
, int whichBlock
)
1020 if(whichBlock
- tf
->which_block
== 1 && tf
->which_block
!= 0){
1021 tf
->prev_block_end_time
= tf
->a_block_end
->time
;
1022 tf
->prev_event_time
= tf
->a_block_end
->time
;
1024 tf
->prev_block_end_time
.tv_sec
= 0;
1025 tf
->prev_block_end_time
.tv_nsec
= 0;
1026 tf
->prev_event_time
.tv_sec
= 0;
1027 tf
->prev_event_time
.tv_nsec
= 0;
1030 nbBytes
=lseek(tf
->fd
,(off_t
)((whichBlock
-1)*tf
->block_size
), SEEK_SET
);
1031 if(nbBytes
== -1) return EINVAL
;
1033 if(readFile(tf
->fd
,tf
->buffer
,tf
->block_size
,"Unable to read a block"))
1036 tf
->a_block_start
=(BlockStart
*) (tf
->buffer
+ EVENT_HEADER_SIZE
);
1037 lostSize
= *(guint32
*)(tf
->buffer
+ tf
->block_size
- sizeof(guint32
));
1038 tf
->a_block_end
=(BlockEnd
*)(tf
->buffer
+ tf
->block_size
-
1039 lostSize
+ EVENT_HEADER_SIZE
);
1040 tf
->last_event_pos
= tf
->buffer
+ tf
->block_size
- lostSize
;
1042 tf
->which_block
= whichBlock
;
1043 tf
->which_event
= 1;
1044 tf
->cur_event_pos
= tf
->buffer
;//the beginning of the block, block start ev
1045 tf
->cur_heart_beat_number
= 0;
1047 getCyclePerNsec(tf
);
1049 tf
->current_event_time
= getEventTime(tf
);
1054 /*****************************************************************************
1056 * updateTracefile : reinitialize the info of the block which is already
1060 ****************************************************************************/
1062 void updateTracefile(LttTracefile
* tf
)
1064 tf
->which_event
= 1;
1065 tf
->cur_event_pos
= tf
->buffer
;
1066 tf
->current_event_time
= getEventTime(tf
);
1067 tf
->cur_heart_beat_number
= 0;
1069 tf
->prev_event_time
.tv_sec
= 0;
1070 tf
->prev_event_time
.tv_nsec
= 0;
1073 /*****************************************************************************
1075 * skipEvent : go to the next event, update the fields of the current event
1080 * ERANGE : event id is out of range
1081 ****************************************************************************/
1083 int skipEvent(LttTracefile
* t
)
1090 evId
= (int)(*(guint16
*)(t
->cur_event_pos
));
1091 evData
= t
->cur_event_pos
+ EVENT_HEADER_SIZE
;
1093 evT
= ltt_trace_eventtype_get(t
->trace
,(unsigned)evId
);
1095 if(evT
) rootFld
= evT
->root_field
;
1099 //event has string/sequence or the last event is not the same event
1100 if((evT
->latest_block
!=t
->which_block
|| evT
->latest_event
!=t
->which_event
)
1101 && rootFld
->field_fixed
== 0){
1102 setFieldsOffset(t
, evT
, evData
, t
->trace
);
1104 t
->cur_event_pos
+= EVENT_HEADER_SIZE
+ rootFld
->field_size
;
1105 }else t
->cur_event_pos
+= EVENT_HEADER_SIZE
;
1107 evT
->latest_block
= t
->which_block
;
1108 evT
->latest_event
= t
->which_event
;
1110 //the next event is in the next block
1111 if(evId
== TRACE_BLOCK_END
){
1112 t
->cur_event_pos
= t
->buffer
+ t
->block_size
;
1115 t
->current_event_time
= getEventTime(t
);
1121 /*****************************************************************************
1123 * getCyclePerNsec : calculate cycles per nsec for current block
1126 ****************************************************************************/
1128 void getCyclePerNsec(LttTracefile
* t
)
1130 LttTime lBufTotalTime
; /* Total time for this buffer */
1131 LttCycleCount lBufTotalNSec
; /* Total time for this buffer in nsecs */
1132 LttCycleCount lBufTotalCycle
;/* Total cycles for this buffer */
1134 /* Calculate the total time for this buffer */
1135 lBufTotalTime
= ltt_time_sub(t
->a_block_end
->time
, t
->a_block_start
->time
);
1137 /* Calculate the total cycles for this bufffer */
1138 lBufTotalCycle
= t
->a_block_end
->cycle_count
;
1139 lBufTotalCycle
-= t
->a_block_start
->cycle_count
;
1141 /* Convert the total time to nsecs */
1142 lBufTotalNSec
= lBufTotalTime
.tv_sec
;
1143 lBufTotalNSec
*= NANOSECONDS_PER_SECOND
;
1144 lBufTotalNSec
+= lBufTotalTime
.tv_nsec
;
1146 t
->cycle_per_nsec
= (double)lBufTotalCycle
/ (double)lBufTotalNSec
;
1149 /****************************************************************************
1151 * getEventTime : obtain the time of an event
1155 * LttTime : the time of the event
1156 ****************************************************************************/
1158 LttTime
getEventTime(LttTracefile
* tf
)
1161 LttCycleCount cycle_count
; // cycle count for the current event
1162 LttCycleCount lEventTotalCycle
; // Total cycles from start for event
1163 LttCycleCount lEventNSec
; // Total usecs from start for event
1164 LttTime lTimeOffset
; // Time offset in struct LttTime
1166 LttCycleCount tmpCycleCount
= (((LttCycleCount
)1)<<32);
1168 evId
= *(guint16
*)tf
->cur_event_pos
;
1169 if(evId
== TRACE_BLOCK_START
){
1171 tf
->pre_cycle_count
= 0;
1172 tf
->cur_cycle_count
= tf
->a_block_start
->cycle_count
;
1173 return tf
->a_block_start
->time
;
1174 }else if(evId
== TRACE_BLOCK_END
){
1176 tf
->pre_cycle_count
= 0;
1177 tf
->cur_cycle_count
= tf
->a_block_end
->cycle_count
;
1178 return tf
->a_block_end
->time
;
1181 // Calculate total time in cycles from start of buffer for this event
1182 cycle_count
= (LttCycleCount
)*(guint32
*)(tf
->cur_event_pos
+ EVENT_ID_SIZE
);
1184 if(cycle_count
< tf
->pre_cycle_count
)tf
->count
++;
1185 tf
->pre_cycle_count
= cycle_count
;
1186 cycle_count
+= tmpCycleCount
* tf
->count
;
1188 // if(tf->cur_heart_beat_number > tf->count)
1189 // cycle_count += tmpCycleCount * (tf->cur_heart_beat_number - tf->count);
1191 tf
->cur_cycle_count
= cycle_count
;
1193 lEventTotalCycle
= cycle_count
;
1194 lEventTotalCycle
-= tf
->a_block_start
->cycle_count
;
1196 // Convert it to nsecs
1197 lEventNSec
= (double)lEventTotalCycle
/ (double)tf
->cycle_per_nsec
;
1199 // Determine offset in struct LttTime
1200 lTimeOffset
.tv_nsec
= lEventNSec
% NANOSECONDS_PER_SECOND
;
1201 lTimeOffset
.tv_sec
= lEventNSec
/ NANOSECONDS_PER_SECOND
;
1203 time
= ltt_time_add(tf
->a_block_start
->time
, lTimeOffset
);
1208 /*****************************************************************************
1210 * setFieldsOffset : set offset of the fields
1212 * tracefile : opened trace file
1213 * evT : the event type
1214 * evD : event data, it may be NULL
1215 ****************************************************************************/
1217 void setFieldsOffset(LttTracefile
*tf
,LttEventType
*evT
,void *evD
,LttTrace
* t
)
1219 LttField
* rootFld
= evT
->root_field
;
1220 // rootFld->base_address = evD;
1223 rootFld
->field_size
= getFieldtypeSize(tf
, evT
, 0,0,rootFld
, evD
,t
);
1226 /*****************************************************************************
1228 * getFieldtypeSize: get the size of the field type (primitive type)
1230 * tracefile : opened trace file
1232 * offsetRoot : offset from the root
1233 * offsetParent : offset from the parrent
1235 * evD : event data, it may be NULL
1237 * int : size of the field
1238 ****************************************************************************/
1240 int getFieldtypeSize(LttTracefile
* t
, LttEventType
* evT
, int offsetRoot
,
1241 int offsetParent
, LttField
* fld
, void *evD
, LttTrace
*trace
)
1243 int size
, size1
, element_number
, i
, offset1
, offset2
;
1244 LttType
* type
= fld
->field_type
;
1247 if(evT
->latest_block
==t
->which_block
&& evT
->latest_event
==t
->which_event
){
1248 return fld
->field_size
;
1252 if(fld
->field_fixed
== 1){
1253 if(fld
== evT
->root_field
) return fld
->field_size
;
1256 if(type
->type_class
!= LTT_STRUCT
&& type
->type_class
!= LTT_ARRAY
&&
1257 type
->type_class
!= LTT_SEQUENCE
&& type
->type_class
!= LTT_STRING
){
1258 if(fld
->field_fixed
== -1){
1259 size
= (int) ltt_type_size(trace
, type
);
1260 fld
->field_fixed
= 1;
1261 }else size
= fld
->field_size
;
1263 }else if(type
->type_class
== LTT_ARRAY
){
1264 element_number
= (int) type
->element_number
;
1265 if(fld
->field_fixed
== -1){
1266 size
= getFieldtypeSize(t
, evT
, offsetRoot
,0,fld
->child
[0], NULL
, trace
);
1267 if(size
== 0){ //has string or sequence
1268 fld
->field_fixed
= 0;
1270 fld
->field_fixed
= 1;
1271 size
*= element_number
;
1273 }else if(fld
->field_fixed
== 0){// has string or sequence
1275 for(i
=0;i
<element_number
;i
++){
1276 size
+= getFieldtypeSize(t
, evT
, offsetRoot
+size
,size
,
1277 fld
->child
[0], evD
+size
, trace
);
1279 }else size
= fld
->field_size
;
1281 }else if(type
->type_class
== LTT_SEQUENCE
){
1282 size1
= (int) ltt_type_size(trace
, type
);
1283 if(fld
->field_fixed
== -1){
1284 fld
->sequ_number_size
= size1
;
1285 fld
->field_fixed
= 0;
1286 size
= getFieldtypeSize(t
, evT
, offsetRoot
,0,fld
->child
[0], NULL
, trace
);
1287 fld
->element_size
= size
;
1289 element_number
= getIntNumber(size1
,evD
);
1290 type
->element_number
= element_number
;
1291 if(fld
->element_size
> 0){
1292 size
= element_number
* fld
->element_size
;
1293 }else{//sequence has string or sequence
1295 for(i
=0;i
<element_number
;i
++){
1296 size
+= getFieldtypeSize(t
, evT
, offsetRoot
+size
+size1
,size
+size1
,
1297 fld
->child
[0], evD
+size
+size1
, trace
);
1303 }else if(type
->type_class
== LTT_STRING
){
1305 if(fld
->field_fixed
== -1){
1306 fld
->field_fixed
= 0;
1308 size
= strlen((char*)evD
) + 1; //include end : '\0'
1311 }else if(type
->type_class
== LTT_STRUCT
){
1312 element_number
= (int) type
->element_number
;
1314 if(fld
->field_fixed
== -1){
1315 offset1
= offsetRoot
;
1317 for(i
=0;i
<element_number
;i
++){
1318 size1
=getFieldtypeSize(t
, evT
,offset1
,offset2
, fld
->child
[i
], NULL
, trace
);
1319 if(size1
> 0 && size
>= 0){
1321 if(offset1
>= 0) offset1
+= size1
;
1330 fld
->field_fixed
= 0;
1332 }else fld
->field_fixed
= 1;
1333 }else if(fld
->field_fixed
== 0){
1334 offset1
= offsetRoot
;
1336 for(i
=0;i
<element_number
;i
++){
1337 size
=getFieldtypeSize(t
,evT
,offset1
,offset2
,fld
->child
[i
],evD
+offset2
, trace
);
1342 }else size
= fld
->field_size
;
1345 fld
->offset_root
= offsetRoot
;
1346 fld
->offset_parent
= offsetParent
;
1348 fld
->fixed_root
= (offsetRoot
==-1) ? 0 : 1;
1349 fld
->fixed_parent
= (offsetParent
==-1) ? 0 : 1;
1351 fld
->field_size
= size
;
1357 /*****************************************************************************
1359 * getIntNumber : get an integer number
1361 * size : the size of the integer
1362 * evD : the event data
1365 ****************************************************************************/
1367 int getIntNumber(int size
, void *evD
)
1370 if(size
== 1) i
= *(gint8
*)evD
;
1371 else if(size
== 2) i
= *(gint16
*)evD
;
1372 else if(size
== 4) i
= *(gint32
*)evD
;
1373 else if(size
== 8) i
= *(gint64
*)evD
;
1378 /*****************************************************************************
1380 * getDataEndianType : get the data type size and endian type of the local
1383 * size : size of data type
1384 * endian : endian type, little or big
1385 ****************************************************************************/
1387 void getDataEndianType(LttArchSize
* size
, LttArchEndian
* endian
)
1391 int sizeInt
=sizeof(int), sizeLong
=sizeof(long), sizePointer
=sizeof(void *);
1393 if(c
== 1) *endian
= LTT_LITTLE_ENDIAN
;
1394 else *endian
= LTT_BIG_ENDIAN
;
1396 if(sizeInt
== 2 && sizeLong
== 4 && sizePointer
== 4)
1398 else if(sizeInt
== 4 && sizeLong
== 4 && sizePointer
== 4)
1400 else if(sizeInt
== 4 && sizeLong
== 8 && sizePointer
== 8)
1402 else if(sizeInt
== 8 && sizeLong
== 8 && sizePointer
== 8)
1404 else *size
= LTT_UNKNOWN
;
1407 /* get the node name of the system */
1409 char * ltt_trace_system_description_node_name (LttSystemDescription
* s
)
1411 return s
->node_name
;
1415 /* get the domain name of the system */
1417 char * ltt_trace_system_description_domain_name (LttSystemDescription
* s
)
1419 return s
->domain_name
;
1423 /* get the description of the system */
1425 char * ltt_trace_system_description_description (LttSystemDescription
* s
)
1427 return s
->description
;
1431 /* get the start time of the trace */
1433 LttTime
ltt_trace_system_description_trace_start_time(LttSystemDescription
*s
)
1435 return s
->trace_start
;