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>
33 #include "ltt-private.h"
34 #include <ltt/trace.h>
35 #include <ltt/facility.h>
37 #define DIR_NAME_SIZE 256
39 /* set the offset of the fields belonging to the event,
40 need the information of the archecture */
41 void setFieldsOffset(LttTracefile
*tf
,LttEventType
*evT
,void *evD
,LttTrace
*t
);
43 /* get the size of the field type according to the archtecture's
44 size and endian type(info of the archecture) */
45 int getFieldtypeSize(LttTracefile
* tf
, LttEventType
* evT
, int offsetRoot
,
46 int offsetParent
, LttField
*fld
, void *evD
, LttTrace
* t
);
48 /* read a fixed size or a block information from the file (fd) */
49 int readFile(int fd
, void * buf
, size_t size
, char * mesg
);
50 int readBlock(LttTracefile
* tf
, int whichBlock
);
52 /* calculate cycles per nsec for current block */
53 void getCyclePerNsec(LttTracefile
* t
);
55 /* reinitialize the info of the block which is already in the buffer */
56 void updateTracefile(LttTracefile
* tf
);
58 /* go to the next event */
59 int skipEvent(LttTracefile
* t
);
62 /* Functions to parse system.xml file (using glib xml parser) */
63 static void parser_start_element (GMarkupParseContext
*context
,
64 const gchar
*element_name
,
65 const gchar
**attribute_names
,
66 const gchar
**attribute_values
,
71 LttSystemDescription
* des
= (LttSystemDescription
* )user_data
;
72 if(strcmp("system", element_name
)){
73 g_warning("This is not system.xml file\n");
77 while(attribute_names
[i
]){
78 if(strcmp("node_name", attribute_names
[i
])==0){
79 des
->node_name
= g_strdup(attribute_values
[i
]);
80 }else if(strcmp("domainname", attribute_names
[i
])==0){
81 des
->domain_name
= g_strdup(attribute_values
[i
]);
82 }else if(strcmp("cpu", attribute_names
[i
])==0){
83 des
->nb_cpu
= atoi(attribute_values
[i
]);
84 }else if(strcmp("arch_size", attribute_names
[i
])==0){
85 if(strcmp(attribute_values
[i
],"LP32") == 0) des
->size
= LTT_LP32
;
86 else if(strcmp(attribute_values
[i
],"ILP32") == 0) des
->size
= LTT_ILP32
;
87 else if(strcmp(attribute_values
[i
],"LP64") == 0) des
->size
= LTT_LP64
;
88 else if(strcmp(attribute_values
[i
],"ILP64") == 0) des
->size
= LTT_ILP64
;
89 else if(strcmp(attribute_values
[i
],"UNKNOWN") == 0) des
->size
= LTT_UNKNOWN
;
90 }else if(strcmp("endian", attribute_names
[i
])==0){
91 if(strcmp(attribute_values
[i
],"LITTLE_ENDIAN") == 0)
92 des
->endian
= LTT_LITTLE_ENDIAN
;
93 else if(strcmp(attribute_values
[i
],"BIG_ENDIAN") == 0)
94 des
->endian
= LTT_BIG_ENDIAN
;
95 }else if(strcmp("kernel_name", attribute_names
[i
])==0){
96 des
->kernel_name
= g_strdup(attribute_values
[i
]);
97 }else if(strcmp("kernel_release", attribute_names
[i
])==0){
98 des
->kernel_release
= g_strdup(attribute_values
[i
]);
99 }else if(strcmp("kernel_version", attribute_names
[i
])==0){
100 des
->kernel_version
= g_strdup(attribute_values
[i
]);
101 }else if(strcmp("machine", attribute_names
[i
])==0){
102 des
->machine
= g_strdup(attribute_values
[i
]);
103 }else if(strcmp("processor", attribute_names
[i
])==0){
104 des
->processor
= g_strdup(attribute_values
[i
]);
105 }else if(strcmp("hardware_platform", attribute_names
[i
])==0){
106 des
->hardware_platform
= g_strdup(attribute_values
[i
]);
107 }else if(strcmp("operating_system", attribute_names
[i
])==0){
108 des
->operating_system
= g_strdup(attribute_values
[i
]);
109 }else if(strcmp("ltt_major_version", attribute_names
[i
])==0){
110 des
->ltt_major_version
= atoi(attribute_values
[i
]);
111 }else if(strcmp("ltt_minor_version", attribute_names
[i
])==0){
112 des
->ltt_minor_version
= atoi(attribute_values
[i
]);
113 }else if(strcmp("ltt_block_size", attribute_names
[i
])==0){
114 des
->ltt_block_size
= atoi(attribute_values
[i
]);
116 g_warning("Not a valid attribute\n");
123 static void parser_end_element (GMarkupParseContext
*context
,
124 const gchar
*element_name
,
130 static void parser_characters (GMarkupParseContext
*context
,
136 LttSystemDescription
* des
= (LttSystemDescription
* )user_data
;
137 des
->description
= g_strdup(text
);
141 /*****************************************************************************
143 * ltt_tracefile_open : open a trace file, construct a LttTracefile
145 * t : the trace containing the tracefile
146 * fileName : path name of the trace file
148 * : a pointer to a tracefile
149 ****************************************************************************/
151 LttTracefile
* ltt_tracefile_open(LttTrace
* t
, char * fileName
)
154 struct stat lTDFStat
; /* Trace data file status */
155 BlockStart a_block_start
;
157 tf
= g_new(LttTracefile
, 1);
160 tf
->name
= g_strdup(fileName
);
162 tf
->fd
= open(fileName
, O_RDONLY
, 0);
164 g_error("Unable to open input data file %s\n", fileName
);
167 // Get the file's status
168 if(fstat(tf
->fd
, &lTDFStat
) < 0){
169 g_error("Unable to get the status of the input data file %s\n", fileName
);
172 // Is the file large enough to contain a trace
173 if(lTDFStat
.st_size
< sizeof(BlockStart
) + EVENT_HEADER_SIZE
){
174 g_print("The input data file %s does not contain a trace\n", fileName
);
181 //store the size of the file
182 tf
->file_size
= lTDFStat
.st_size
;
183 tf
->block_size
= t
->system_description
->ltt_block_size
;
184 tf
->block_number
= tf
->file_size
/ tf
->block_size
;
187 //allocate memory to contain the info of a block
188 tf
->buffer
= (void *) g_new(char, t
->system_description
->ltt_block_size
);
190 //read the first block
191 if(readBlock(tf
,1)) exit(1);
197 /*****************************************************************************
198 *Open control and per cpu tracefiles
199 ****************************************************************************/
201 void ltt_tracefile_open_cpu(LttTrace
*t
, char * tracefile_name
)
204 tf
= ltt_tracefile_open(t
,tracefile_name
);
206 t
->per_cpu_tracefile_number
++;
207 g_ptr_array_add(t
->per_cpu_tracefiles
, tf
);
210 void ltt_tracefile_open_control(LttTrace
*t
, char * control_name
)
220 tf
= ltt_tracefile_open(t
,control_name
);
222 t
->control_tracefile_number
++;
223 g_ptr_array_add(t
->control_tracefiles
,tf
);
225 //parse facilities tracefile to get base_id
226 if(strcmp(&control_name
[strlen(control_name
)-10],"facilities") ==0){
228 ev
= ltt_tracefile_read(tf
);
229 if(!ev
)return; // end of file
231 if(ev
->event_id
== TRACE_FACILITY_LOAD
){
233 fLoad
.name
= (char*)pos
;
234 fLoad
.checksum
= *(LttChecksum
*)(pos
+ strlen(fLoad
.name
));
235 fLoad
.base_code
= *(guint32
*)(pos
+ strlen(fLoad
.name
) + sizeof(LttChecksum
));
237 for(i
=0;i
<t
->facility_number
;i
++){
238 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
,i
);
239 if(strcmp(f
->name
,fLoad
.name
)==0 && fLoad
.checksum
==f
->checksum
){
240 f
->base_id
= fLoad
.base_code
;
244 if(i
==t
->facility_number
)
245 g_error("Facility: %s, checksum: %d is not founded\n",
246 fLoad
.name
,fLoad
.checksum
);
247 }else if(ev
->event_id
== TRACE_BLOCK_START
){
249 }else if(ev
->event_id
== TRACE_BLOCK_END
){
251 }else g_error("Not valid facilities trace file\n");
256 /*****************************************************************************
258 * ltt_tracefile_close: close a trace file,
260 * t : tracefile which will be closed
261 ****************************************************************************/
263 void ltt_tracefile_close(LttTracefile
*t
)
272 /*****************************************************************************
273 *Get system information
274 ****************************************************************************/
275 void getSystemInfo(LttSystemDescription
* des
, char * pathname
)
278 char buf
[DIR_NAME_SIZE
];
279 char description
[4*DIR_NAME_SIZE
];
281 GMarkupParseContext
* context
;
283 GMarkupParser markup_parser
=
285 parser_start_element
,
288 NULL
, /* passthrough */
292 fp
= fopen(pathname
,"r");
294 g_error("Can not open file : %s\n", pathname
);
297 context
= g_markup_parse_context_new(&markup_parser
, 0, des
,NULL
);
299 while(fgets(buf
,DIR_NAME_SIZE
, fp
) != NULL
){
300 if(!g_markup_parse_context_parse(context
, buf
, DIR_NAME_SIZE
, &error
)){
301 g_warning("Can not parse xml file: \n%s\n", error
->message
);
308 /*****************************************************************************
309 *The following functions get facility/tracefile information
310 ****************************************************************************/
312 void getFacilityInfo(LttTrace
*t
, char* eventdefs
)
315 struct dirent
*entry
;
320 char name
[DIR_NAME_SIZE
];
322 dir
= opendir(eventdefs
);
323 if(!dir
) g_error("Can not open directory: %s\n", eventdefs
);
325 while((entry
= readdir(dir
)) != NULL
){
326 ptr
= &entry
->d_name
[strlen(entry
->d_name
)-4];
327 if(strcmp(ptr
,".xml") != 0) continue;
328 strcpy(name
,eventdefs
);
329 strcat(name
,entry
->d_name
);
330 ltt_facility_open(t
,name
);
334 for(j
=0;j
<t
->facility_number
;j
++){
335 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, j
);
336 for(i
=0; i
<f
->event_number
; i
++){
338 setFieldsOffset(NULL
, et
, NULL
, t
);
343 void getControlFileInfo(LttTrace
*t
, char* control
)
346 struct dirent
*entry
;
347 char name
[DIR_NAME_SIZE
];
349 dir
= opendir(control
);
350 if(!dir
) g_error("Can not open directory: %s\n", control
);
352 while((entry
= readdir(dir
)) != NULL
){
353 if(strcmp(entry
->d_name
,"facilities") != 0 &&
354 strcmp(entry
->d_name
,"interrupts") != 0 &&
355 strcmp(entry
->d_name
,"processes") != 0) continue;
357 strcpy(name
,control
);
358 strcat(name
,entry
->d_name
);
359 ltt_tracefile_open_control(t
,name
);
364 void getCpuFileInfo(LttTrace
*t
, char* cpu
)
367 struct dirent
*entry
;
368 char name
[DIR_NAME_SIZE
];
371 if(!dir
) g_error("Can not open directory: %s\n", cpu
);
373 while((entry
= readdir(dir
)) != NULL
){
374 if(strcmp(entry
->d_name
,".") != 0 &&
375 strcmp(entry
->d_name
,"..") != 0 &&
376 strcmp(entry
->d_name
,".svn") != 0){
378 strcat(name
,entry
->d_name
);
379 ltt_tracefile_open_cpu(t
,name
);
385 /*****************************************************************************
386 *A trace is specified as a pathname to the directory containing all the
387 *associated data (control tracefiles, per cpu tracefiles, event
390 *When a trace is closed, all the associated facilities, types and fields
391 *are released as well.
393 * MD : Fixed this function so it uses realpath, dealing well with
394 * forgotten cases (.. were not used correctly before).
396 ****************************************************************************/
398 void get_absolute_pathname(const char *pathname
, char * abs_pathname
)
401 size_t size
= DIR_NAME_SIZE
;
402 abs_pathname
[0] = '\0';
404 if ( realpath (pathname
, abs_pathname
) != NULL
)
408 // FIXME : Path is wrong, is it ok to return the pathname unmodified ?
409 strcpy(abs_pathname
, pathname
);
417 LttTrace
*ltt_trace_open(const char *pathname
)
420 LttSystemDescription
* sys_description
;
421 char eventdefs
[DIR_NAME_SIZE
];
422 char info
[DIR_NAME_SIZE
];
423 char control
[DIR_NAME_SIZE
];
424 char cpu
[DIR_NAME_SIZE
];
425 char tmp
[DIR_NAME_SIZE
];
426 char abs_path
[DIR_NAME_SIZE
];
427 gboolean has_slash
= FALSE
;
429 get_absolute_pathname(pathname
, abs_path
);
430 //establish the pathname to different directories
431 if(abs_path
[strlen(abs_path
)-1] == '/')has_slash
= TRUE
;
432 strcpy(eventdefs
,abs_path
);
433 if(!has_slash
)strcat(eventdefs
,"/");
434 strcat(eventdefs
,"eventdefs/");
436 strcpy(info
,abs_path
);
437 if(!has_slash
)strcat(info
,"/");
438 strcat(info
,"info/");
440 strcpy(control
,abs_path
);
441 if(!has_slash
)strcat(control
,"/");
442 strcat(control
,"control/");
444 strcpy(cpu
,abs_path
);
445 if(!has_slash
)strcat(cpu
,"/");
449 t
= g_new(LttTrace
, 1);
450 sys_description
= g_new(LttSystemDescription
, 1);
451 t
->pathname
= g_strdup(abs_path
);
452 t
->facility_number
= 0;
453 t
->control_tracefile_number
= 0;
454 t
->per_cpu_tracefile_number
= 0;
455 t
->system_description
= sys_description
;
456 t
->control_tracefiles
= g_ptr_array_new();
457 t
->per_cpu_tracefiles
= g_ptr_array_new();
458 t
->facilities
= g_ptr_array_new();
459 getDataEndianType(&(t
->my_arch_size
), &(t
->my_arch_endian
));
461 //get system description
463 strcat(tmp
,"system.xml");
464 getSystemInfo(sys_description
, tmp
);
466 //get facilities info
467 getFacilityInfo(t
,eventdefs
);
469 //get control tracefile info
470 getControlFileInfo(t
,control
);
472 //get cpu tracefile info
473 getCpuFileInfo(t
,cpu
);
478 char * ltt_trace_name(LttTrace
*t
)
484 /******************************************************************************
485 * When we copy a trace, we want all the opening actions to happen again :
486 * the trace will be reopened and totally independant from the original.
487 * That's why we call ltt_trace_open.
488 *****************************************************************************/
489 LttTrace
*ltt_trace_copy(LttTrace
*self
)
491 return ltt_trace_open(self
->pathname
);
494 void ltt_trace_close(LttTrace
*t
)
502 //free system_description
503 g_free(t
->system_description
->description
);
504 g_free(t
->system_description
->node_name
);
505 g_free(t
->system_description
->domain_name
);
506 g_free(t
->system_description
->kernel_name
);
507 g_free(t
->system_description
->kernel_release
);
508 g_free(t
->system_description
->kernel_version
);
509 g_free(t
->system_description
->machine
);
510 g_free(t
->system_description
->processor
);
511 g_free(t
->system_description
->hardware_platform
);
512 g_free(t
->system_description
->operating_system
);
513 g_free(t
->system_description
);
515 //free control_tracefiles
516 for(i
=0;i
<t
->control_tracefile_number
;i
++){
517 tf
= (LttTracefile
*)g_ptr_array_index(t
->control_tracefiles
,i
);
518 ltt_tracefile_close(tf
);
520 g_ptr_array_free(t
->control_tracefiles
, TRUE
);
522 //free per_cpu_tracefiles
523 for(i
=0;i
<t
->per_cpu_tracefile_number
;i
++){
524 tf
= (LttTracefile
*)g_ptr_array_index(t
->per_cpu_tracefiles
,i
);
525 ltt_tracefile_close(tf
);
527 g_ptr_array_free(t
->per_cpu_tracefiles
, TRUE
);
530 for(i
=0;i
<t
->facility_number
;i
++){
531 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
,i
);
532 ltt_facility_close(f
);
534 g_ptr_array_free(t
->facilities
, TRUE
);
542 /*****************************************************************************
543 *Get the system description of the trace
544 ****************************************************************************/
546 LttSystemDescription
*ltt_trace_system_description(LttTrace
*t
)
548 return t
->system_description
;
551 /*****************************************************************************
552 * The following functions discover the facilities of the trace
553 ****************************************************************************/
555 unsigned ltt_trace_facility_number(LttTrace
*t
)
557 return (unsigned)(t
->facility_number
);
560 LttFacility
*ltt_trace_facility_get(LttTrace
*t
, unsigned i
)
562 return (LttFacility
*)g_ptr_array_index(t
->facilities
, i
);
565 /*****************************************************************************
567 * ltt_trace_facility_find : find facilities in the trace
570 * name : facility name
572 * position : position of the facility in the trace
574 * : the number of facilities
575 ****************************************************************************/
577 unsigned ltt_trace_facility_find(LttTrace
*t
, char *name
, unsigned *position
)
581 for(i
=0;i
<t
->facility_number
;i
++){
582 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, i
);
583 if(strcmp(f
->name
,name
)==0){
585 if(count
==1) *position
= i
;
593 /*****************************************************************************
594 * Functions to discover all the event types in the trace
595 ****************************************************************************/
597 unsigned ltt_trace_eventtype_number(LttTrace
*t
)
602 for(i
=0;i
<t
->facility_number
;i
++){
603 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, i
);
604 count
+= f
->event_number
;
609 LttFacility
* ltt_trace_facility_by_id(LttTrace
* trace
, unsigned id
)
611 LttFacility
* facility
;
613 for(i
=0;i
<trace
->facility_number
;i
++){
614 facility
= (LttFacility
*) g_ptr_array_index(trace
->facilities
,i
);
615 if(id
>= facility
->base_id
&&
616 id
< facility
->base_id
+ facility
->event_number
)
619 if(i
==trace
->facility_number
) return NULL
;
620 else return facility
;
623 LttEventType
*ltt_trace_eventtype_get(LttTrace
*t
, unsigned evId
)
626 f
= ltt_trace_facility_by_id(t
,evId
);
628 return f
->events
[evId
- f
->base_id
];
631 /*****************************************************************************
632 *There is one "per cpu" tracefile for each CPU, numbered from 0 to
633 *the maximum number of CPU in the system. When the number of CPU installed
634 *is less than the maximum, some positions are unused. There are also a
635 *number of "control" tracefiles (facilities, interrupts...).
636 ****************************************************************************/
637 unsigned ltt_trace_control_tracefile_number(LttTrace
*t
)
639 return t
->control_tracefile_number
;
642 unsigned ltt_trace_per_cpu_tracefile_number(LttTrace
*t
)
644 return t
->per_cpu_tracefile_number
;
647 /*****************************************************************************
648 *It is possible to search for the tracefiles by name or by CPU position.
649 *The index within the tracefiles of the same type is returned if found
650 *and a negative value otherwise.
651 ****************************************************************************/
653 int ltt_trace_control_tracefile_find(LttTrace
*t
, char *name
)
655 LttTracefile
* tracefile
;
657 for(i
=0;i
<t
->control_tracefile_number
;i
++){
658 tracefile
= (LttTracefile
*)g_ptr_array_index(t
->control_tracefiles
, i
);
659 if(strcmp(tracefile
->name
, name
)==0)break;
661 if(i
== t
->control_tracefile_number
) return -1;
665 int ltt_trace_per_cpu_tracefile_find(LttTrace
*t
, unsigned i
)
667 LttTracefile
* tracefile
;
669 for(j
=0;j
<t
->per_cpu_tracefile_number
;j
++){
670 tracefile
= (LttTracefile
*)g_ptr_array_index(t
->per_cpu_tracefiles
, j
);
671 name
= atoi(tracefile
->name
);
672 if(name
== (int)i
)break;
674 if(j
== t
->per_cpu_tracefile_number
) return -1;
678 /*****************************************************************************
679 *Get a specific tracefile
680 ****************************************************************************/
682 LttTracefile
*ltt_trace_control_tracefile_get(LttTrace
*t
, unsigned i
)
684 return (LttTracefile
*)g_ptr_array_index(t
->control_tracefiles
, i
);
687 LttTracefile
*ltt_trace_per_cpu_tracefile_get(LttTrace
*t
, unsigned i
)
689 return (LttTracefile
*)g_ptr_array_index(t
->per_cpu_tracefiles
, i
);
692 /*****************************************************************************
693 * Get the start time and end time of the trace
694 ****************************************************************************/
696 void ltt_trace_time_span_get(LttTrace
*t
, LttTime
*start
, LttTime
*end
)
698 LttTime startSmall
, startTmp
, endBig
, endTmp
;
702 for(i
=0;i
<t
->control_tracefile_number
;i
++){
703 tf
= g_ptr_array_index(t
->control_tracefiles
, i
);
705 startTmp
= tf
->a_block_start
->time
;
706 readBlock(tf
,tf
->block_number
);
707 endTmp
= tf
->a_block_end
->time
;
709 startSmall
= startTmp
;
714 if(ltt_time_compare(startSmall
,startTmp
) > 0) startSmall
= startTmp
;
715 if(ltt_time_compare(endBig
,endTmp
) < 0) endBig
= endTmp
;
718 for(i
=0;i
<t
->per_cpu_tracefile_number
;i
++){
719 tf
= g_ptr_array_index(t
->per_cpu_tracefiles
, i
);
721 startTmp
= tf
->a_block_start
->time
;
722 readBlock(tf
,tf
->block_number
);
723 endTmp
= tf
->a_block_end
->time
;
725 startSmall
= startTmp
;
729 if(ltt_time_compare(startSmall
,startTmp
) > 0) startSmall
= startTmp
;
730 if(ltt_time_compare(endBig
,endTmp
) < 0) endBig
= endTmp
;
738 /*****************************************************************************
739 *Get the name of a tracefile
740 ****************************************************************************/
742 char *ltt_tracefile_name(LttTracefile
*tf
)
747 /*****************************************************************************
748 * Get the number of blocks in the tracefile
749 ****************************************************************************/
751 unsigned ltt_tracefile_block_number(LttTracefile
*tf
)
753 return tf
->block_number
;
756 /*****************************************************************************
758 * ltt_tracefile_seek_time: seek to the first event of the trace with time
759 * larger or equal to time
762 * time : criteria of the time
763 ****************************************************************************/
764 void ltt_tracefile_find_time_block(LttTracefile
*t
, LttTime time
,
765 int start_block
, int end_block
)
767 int err
, tmp_block
, s
, e
;
771 err
=readBlock(t
,start_block
);
772 if(err
) g_error("Can not read tracefile: %s\n", t
->name
);
773 if(start_block
== end_block
)return;
775 tailTime
= ltt_time_compare(t
->a_block_end
->time
, time
);
776 if(tailTime
>= 0) return;
778 err
=readBlock(t
,end_block
);
779 if(err
) g_error("Can not read tracefile: %s\n", t
->name
);
780 if(start_block
+1 == end_block
)return;
782 headTime
= ltt_time_compare(t
->a_block_start
->time
, time
);
783 if(headTime
<= 0 ) return;
785 tmp_block
= (end_block
+ start_block
)/2;
786 err
=readBlock(t
,tmp_block
);
787 if(err
) g_error("Can not read tracefile: %s\n", t
->name
);
789 headTime
= ltt_time_compare(t
->a_block_start
->time
, time
);
790 tailTime
= ltt_time_compare(t
->a_block_end
->time
, time
);
791 if(headTime
<= 0 && tailTime
>= 0) return;
797 ltt_tracefile_find_time_block(t
, time
, s
, e
);
805 ltt_tracefile_find_time_block(t
, time
, s
, e
);
810 void ltt_tracefile_backward_find_time_block(LttTracefile
*t
, LttTime time
)
812 int t_time
, h_time
, err
;
813 err
=readBlock(t
,t
->which_block
-1);
814 if(err
) g_error("Can not read tracefile: %s\n", t
->name
);
815 h_time
= ltt_time_compare(t
->a_block_start
->time
, time
);
816 t_time
= ltt_time_compare(t
->a_block_end
->time
, time
);
819 if(t
->which_block
== 1) return;
820 err
=readBlock(t
,t
->which_block
-1);
821 if(err
) g_error("Can not read tracefile: %s\n", t
->name
);
822 tmp
= ltt_time_compare(t
->a_block_end
->time
, time
);
823 if(tmp
== 0) return ltt_tracefile_seek_time(t
, time
);
824 err
=readBlock(t
,t
->which_block
+1);
825 if(err
) g_error("Can not read tracefile: %s\n", t
->name
);
826 }else if(h_time
> 0){
827 ltt_tracefile_find_time_block(t
, time
, 1, t
->which_block
);
828 return ltt_tracefile_seek_time(t
, time
) ;
830 if(t_time
>= 0) return ltt_tracefile_seek_time(t
, time
);
831 err
=readBlock(t
,t
->which_block
+1);
832 if(err
) g_error("Can not read tracefile: %s\n", t
->name
);
836 void ltt_tracefile_seek_time(LttTracefile
*t
, LttTime time
)
840 int headTime
= ltt_time_compare(t
->a_block_start
->time
, time
);
841 int tailTime
= ltt_time_compare(t
->a_block_end
->time
, time
);
844 if(headTime
< 0 && tailTime
> 0){
845 if(ltt_time_compare(t
->a_block_end
->time
, t
->current_event_time
) !=0) {
846 lttTime
= getEventTime(t
);
847 err
= ltt_time_compare(lttTime
, time
);
849 if(t
->which_event
==2 || (&t
->prev_event_time
,&time
)<0){
853 return ltt_tracefile_seek_time(t
, time
);
857 ev
= ltt_tracefile_read(t
);
859 g_print("End of file\n");
862 lttTime
= getEventTime(t
);
863 err
= ltt_time_compare(lttTime
, time
);
867 }else{//we are at the end of the block
869 return ltt_tracefile_seek_time(t
, time
);
871 }else if(headTime
>= 0){
872 if(t
->which_block
== 1){
875 if(ltt_time_compare(t
->prev_block_end_time
, time
) >= 0 ||
876 (t
->prev_block_end_time
.tv_sec
== 0 &&
877 t
->prev_block_end_time
.tv_nsec
== 0 )){
878 ltt_tracefile_backward_find_time_block(t
, time
);
883 }else if(tailTime
< 0){
884 if(t
->which_block
!= t
->block_number
){
885 ltt_tracefile_find_time_block(t
, time
, t
->which_block
+1, t
->block_number
);
886 return ltt_tracefile_seek_time(t
, time
);
888 t
->cur_event_pos
= t
->buffer
+ t
->block_size
;
889 g_print("End of file\n");
892 }else if(tailTime
== 0){
893 t
->cur_event_pos
= t
->last_event_pos
;
894 t
->current_event_time
= time
;
895 t
->cur_heart_beat_number
= 0;
896 t
->prev_event_time
.tv_sec
= 0;
897 t
->prev_event_time
.tv_nsec
= 0;
902 /*****************************************************************************
903 * Seek to the first event with position equal or larger to ep
904 ****************************************************************************/
906 void ltt_tracefile_seek_position(LttTracefile
*t
, const LttEventPosition
*ep
)
908 //if we are at the right place, just return
909 if(t
->which_block
== ep
->block_num
&& t
->which_event
== ep
->event_num
)
912 if(t
->which_block
== ep
->block_num
) updateTracefile(t
);
913 else readBlock(t
,ep
->block_num
);
915 //event offset is availiable
916 if(ep
->old_position
){
917 t
->cur_heart_beat_number
= ep
->heart_beat_number
;
918 t
->cur_event_pos
= t
->buffer
+ ep
->event_offset
;
922 //only block number and event index are availiable
923 while(t
->which_event
< ep
->event_num
) ltt_tracefile_read(t
);
928 /*****************************************************************************
930 * ltt_tracefile_read : read the current event, set the pointer to the next
934 * LttEvent * : an event to be processed
935 ****************************************************************************/
937 LttEvent
*ltt_tracefile_read(LttTracefile
*t
)
939 LttEvent
* lttEvent
= &t
->an_event
;
942 if(t
->cur_event_pos
== t
->buffer
+ t
->block_size
){
943 if(t
->which_block
== t
->block_number
){
946 err
= readBlock(t
, t
->which_block
+ 1);
947 if(err
)g_error("Can not read tracefile");
950 lttEvent
->event_id
= (int)(*(guint16
*)(t
->cur_event_pos
));
951 if(lttEvent
->event_id
== TRACE_TIME_HEARTBEAT
)
952 t
->cur_heart_beat_number
++;
954 t
->prev_event_time
= t
->current_event_time
;
955 // t->current_event_time = getEventTime(t);
957 lttEvent
->time_delta
= *(guint32
*)(t
->cur_event_pos
+ EVENT_ID_SIZE
);
958 lttEvent
->event_time
= t
->current_event_time
;
959 lttEvent
->event_cycle_count
= t
->cur_cycle_count
;
961 lttEvent
->tracefile
= t
;
962 lttEvent
->data
= t
->cur_event_pos
+ EVENT_HEADER_SIZE
;
963 lttEvent
->which_block
= t
->which_block
;
964 lttEvent
->which_event
= t
->which_event
;
966 //update the fields of the current event and go to the next event
968 if(err
== ERANGE
) g_error("event id is out of range\n");
973 /****************************************************************************
975 * readFile : wrap function to read from a file
977 * fd : file descriptor
978 * buf : buf to contain the content
979 * size : number of bytes to be read
980 * mesg : message to be printed if some thing goes wrong
983 * EIO : can not read from the file
984 ****************************************************************************/
986 int readFile(int fd
, void * buf
, size_t size
, char * mesg
)
989 nbBytes
= read(fd
, buf
, size
);
997 /****************************************************************************
999 * readBlock : read a block from the file
1001 * lttdes : ltt trace file
1002 * whichBlock : the block which will be read
1005 * EINVAL : lseek fail
1006 * EIO : can not read from the file
1007 ****************************************************************************/
1009 int readBlock(LttTracefile
* tf
, int whichBlock
)
1014 if(whichBlock
- tf
->which_block
== 1 && tf
->which_block
!= 0){
1015 tf
->prev_block_end_time
= tf
->a_block_end
->time
;
1016 tf
->prev_event_time
= tf
->a_block_end
->time
;
1018 tf
->prev_block_end_time
.tv_sec
= 0;
1019 tf
->prev_block_end_time
.tv_nsec
= 0;
1020 tf
->prev_event_time
.tv_sec
= 0;
1021 tf
->prev_event_time
.tv_nsec
= 0;
1024 nbBytes
=lseek(tf
->fd
,(off_t
)((whichBlock
-1)*tf
->block_size
), SEEK_SET
);
1025 if(nbBytes
== -1) return EINVAL
;
1027 if(readFile(tf
->fd
,tf
->buffer
,tf
->block_size
,"Unable to read a block"))
1030 tf
->a_block_start
=(BlockStart
*) (tf
->buffer
+ EVENT_HEADER_SIZE
);
1031 lostSize
= *(guint32
*)(tf
->buffer
+ tf
->block_size
- sizeof(guint32
));
1032 tf
->a_block_end
=(BlockEnd
*)(tf
->buffer
+ tf
->block_size
-
1033 lostSize
+ EVENT_HEADER_SIZE
);
1034 tf
->last_event_pos
= tf
->buffer
+ tf
->block_size
- lostSize
;
1036 tf
->which_block
= whichBlock
;
1037 tf
->which_event
= 1;
1038 tf
->cur_event_pos
= tf
->buffer
;//the beginning of the block, block start ev
1039 tf
->cur_heart_beat_number
= 0;
1041 getCyclePerNsec(tf
);
1043 tf
->current_event_time
= getEventTime(tf
);
1048 /*****************************************************************************
1050 * updateTracefile : reinitialize the info of the block which is already
1054 ****************************************************************************/
1056 void updateTracefile(LttTracefile
* tf
)
1058 tf
->which_event
= 1;
1059 tf
->cur_event_pos
= tf
->buffer
;
1060 tf
->current_event_time
= getEventTime(tf
);
1061 tf
->cur_heart_beat_number
= 0;
1063 tf
->prev_event_time
.tv_sec
= 0;
1064 tf
->prev_event_time
.tv_nsec
= 0;
1067 /*****************************************************************************
1069 * skipEvent : go to the next event, update the fields of the current event
1074 * ERANGE : event id is out of range
1075 ****************************************************************************/
1077 int skipEvent(LttTracefile
* t
)
1084 evId
= (int)(*(guint16
*)(t
->cur_event_pos
));
1085 evData
= t
->cur_event_pos
+ EVENT_HEADER_SIZE
;
1087 evT
= ltt_trace_eventtype_get(t
->trace
,(unsigned)evId
);
1089 if(evT
) rootFld
= evT
->root_field
;
1093 //event has string/sequence or the last event is not the same event
1094 if((evT
->latest_block
!=t
->which_block
|| evT
->latest_event
!=t
->which_event
)
1095 && rootFld
->field_fixed
== 0){
1096 setFieldsOffset(t
, evT
, evData
, t
->trace
);
1098 t
->cur_event_pos
+= EVENT_HEADER_SIZE
+ rootFld
->field_size
;
1099 }else t
->cur_event_pos
+= EVENT_HEADER_SIZE
;
1101 evT
->latest_block
= t
->which_block
;
1102 evT
->latest_event
= t
->which_event
;
1104 //the next event is in the next block
1105 if(evId
== TRACE_BLOCK_END
){
1106 t
->cur_event_pos
= t
->buffer
+ t
->block_size
;
1109 t
->current_event_time
= getEventTime(t
);
1115 /*****************************************************************************
1117 * getCyclePerNsec : calculate cycles per nsec for current block
1120 ****************************************************************************/
1122 void getCyclePerNsec(LttTracefile
* t
)
1124 LttTime lBufTotalTime
; /* Total time for this buffer */
1125 LttCycleCount lBufTotalNSec
; /* Total time for this buffer in nsecs */
1126 LttCycleCount lBufTotalCycle
;/* Total cycles for this buffer */
1128 /* Calculate the total time for this buffer */
1129 lBufTotalTime
= ltt_time_sub(t
->a_block_end
->time
, t
->a_block_start
->time
);
1131 /* Calculate the total cycles for this bufffer */
1132 lBufTotalCycle
= t
->a_block_end
->cycle_count
;
1133 lBufTotalCycle
-= t
->a_block_start
->cycle_count
;
1135 /* Convert the total time to nsecs */
1136 lBufTotalNSec
= lBufTotalTime
.tv_sec
;
1137 lBufTotalNSec
*= NANOSECONDS_PER_SECOND
;
1138 lBufTotalNSec
+= lBufTotalTime
.tv_nsec
;
1140 t
->cycle_per_nsec
= (double)lBufTotalCycle
/ (double)lBufTotalNSec
;
1143 /****************************************************************************
1145 * getEventTime : obtain the time of an event
1149 * LttTime : the time of the event
1150 ****************************************************************************/
1152 LttTime
getEventTime(LttTracefile
* tf
)
1155 LttCycleCount cycle_count
; // cycle count for the current event
1156 LttCycleCount lEventTotalCycle
; // Total cycles from start for event
1157 LttCycleCount lEventNSec
; // Total usecs from start for event
1158 LttTime lTimeOffset
; // Time offset in struct LttTime
1160 LttCycleCount tmpCycleCount
= (((LttCycleCount
)1)<<32);
1162 evId
= *(guint16
*)tf
->cur_event_pos
;
1163 if(evId
== TRACE_BLOCK_START
){
1165 tf
->pre_cycle_count
= 0;
1166 tf
->cur_cycle_count
= tf
->a_block_start
->cycle_count
;
1167 return tf
->a_block_start
->time
;
1168 }else if(evId
== TRACE_BLOCK_END
){
1170 tf
->pre_cycle_count
= 0;
1171 tf
->cur_cycle_count
= tf
->a_block_end
->cycle_count
;
1172 return tf
->a_block_end
->time
;
1175 // Calculate total time in cycles from start of buffer for this event
1176 cycle_count
= (LttCycleCount
)*(guint32
*)(tf
->cur_event_pos
+ EVENT_ID_SIZE
);
1178 if(cycle_count
< tf
->pre_cycle_count
)tf
->count
++;
1179 tf
->pre_cycle_count
= cycle_count
;
1180 cycle_count
+= tmpCycleCount
* tf
->count
;
1182 // if(tf->cur_heart_beat_number > tf->count)
1183 // cycle_count += tmpCycleCount * (tf->cur_heart_beat_number - tf->count);
1185 tf
->cur_cycle_count
= cycle_count
;
1187 lEventTotalCycle
= cycle_count
;
1188 lEventTotalCycle
-= tf
->a_block_start
->cycle_count
;
1190 // Convert it to nsecs
1191 lEventNSec
= (double)lEventTotalCycle
/ (double)tf
->cycle_per_nsec
;
1193 // Determine offset in struct LttTime
1194 lTimeOffset
.tv_nsec
= lEventNSec
% NANOSECONDS_PER_SECOND
;
1195 lTimeOffset
.tv_sec
= lEventNSec
/ NANOSECONDS_PER_SECOND
;
1197 time
= ltt_time_add(tf
->a_block_start
->time
, lTimeOffset
);
1202 /*****************************************************************************
1204 * setFieldsOffset : set offset of the fields
1206 * tracefile : opened trace file
1207 * evT : the event type
1208 * evD : event data, it may be NULL
1209 ****************************************************************************/
1211 void setFieldsOffset(LttTracefile
*tf
,LttEventType
*evT
,void *evD
,LttTrace
* t
)
1213 LttField
* rootFld
= evT
->root_field
;
1214 // rootFld->base_address = evD;
1217 rootFld
->field_size
= getFieldtypeSize(tf
, evT
, 0,0,rootFld
, evD
,t
);
1220 /*****************************************************************************
1222 * getFieldtypeSize: get the size of the field type (primitive type)
1224 * tracefile : opened trace file
1226 * offsetRoot : offset from the root
1227 * offsetParent : offset from the parrent
1229 * evD : event data, it may be NULL
1231 * int : size of the field
1232 ****************************************************************************/
1234 int getFieldtypeSize(LttTracefile
* t
, LttEventType
* evT
, int offsetRoot
,
1235 int offsetParent
, LttField
* fld
, void *evD
, LttTrace
*trace
)
1237 int size
, size1
, element_number
, i
, offset1
, offset2
;
1238 LttType
* type
= fld
->field_type
;
1241 if(evT
->latest_block
==t
->which_block
&& evT
->latest_event
==t
->which_event
){
1242 return fld
->field_size
;
1246 if(fld
->field_fixed
== 1){
1247 if(fld
== evT
->root_field
) return fld
->field_size
;
1250 if(type
->type_class
!= LTT_STRUCT
&& type
->type_class
!= LTT_ARRAY
&&
1251 type
->type_class
!= LTT_SEQUENCE
&& type
->type_class
!= LTT_STRING
){
1252 if(fld
->field_fixed
== -1){
1253 size
= (int) ltt_type_size(trace
, type
);
1254 fld
->field_fixed
= 1;
1255 }else size
= fld
->field_size
;
1257 }else if(type
->type_class
== LTT_ARRAY
){
1258 element_number
= (int) type
->element_number
;
1259 if(fld
->field_fixed
== -1){
1260 size
= getFieldtypeSize(t
, evT
, offsetRoot
,0,fld
->child
[0], NULL
, trace
);
1261 if(size
== 0){ //has string or sequence
1262 fld
->field_fixed
= 0;
1264 fld
->field_fixed
= 1;
1265 size
*= element_number
;
1267 }else if(fld
->field_fixed
== 0){// has string or sequence
1269 for(i
=0;i
<element_number
;i
++){
1270 size
+= getFieldtypeSize(t
, evT
, offsetRoot
+size
,size
,
1271 fld
->child
[0], evD
+size
, trace
);
1273 }else size
= fld
->field_size
;
1275 }else if(type
->type_class
== LTT_SEQUENCE
){
1276 size1
= (int) ltt_type_size(trace
, type
);
1277 if(fld
->field_fixed
== -1){
1278 fld
->sequ_number_size
= size1
;
1279 fld
->field_fixed
= 0;
1280 size
= getFieldtypeSize(t
, evT
, offsetRoot
,0,fld
->child
[0], NULL
, trace
);
1281 fld
->element_size
= size
;
1283 element_number
= getIntNumber(size1
,evD
);
1284 type
->element_number
= element_number
;
1285 if(fld
->element_size
> 0){
1286 size
= element_number
* fld
->element_size
;
1287 }else{//sequence has string or sequence
1289 for(i
=0;i
<element_number
;i
++){
1290 size
+= getFieldtypeSize(t
, evT
, offsetRoot
+size
+size1
,size
+size1
,
1291 fld
->child
[0], evD
+size
+size1
, trace
);
1297 }else if(type
->type_class
== LTT_STRING
){
1299 if(fld
->field_fixed
== -1){
1300 fld
->field_fixed
= 0;
1302 size
= strlen((char*)evD
) + 1; //include end : '\0'
1305 }else if(type
->type_class
== LTT_STRUCT
){
1306 element_number
= (int) type
->element_number
;
1308 if(fld
->field_fixed
== -1){
1309 offset1
= offsetRoot
;
1311 for(i
=0;i
<element_number
;i
++){
1312 size1
=getFieldtypeSize(t
, evT
,offset1
,offset2
, fld
->child
[i
], NULL
, trace
);
1313 if(size1
> 0 && size
>= 0){
1315 if(offset1
>= 0) offset1
+= size1
;
1324 fld
->field_fixed
= 0;
1326 }else fld
->field_fixed
= 1;
1327 }else if(fld
->field_fixed
== 0){
1328 offset1
= offsetRoot
;
1330 for(i
=0;i
<element_number
;i
++){
1331 size
=getFieldtypeSize(t
,evT
,offset1
,offset2
,fld
->child
[i
],evD
+offset2
, trace
);
1336 }else size
= fld
->field_size
;
1339 fld
->offset_root
= offsetRoot
;
1340 fld
->offset_parent
= offsetParent
;
1342 fld
->fixed_root
= (offsetRoot
==-1) ? 0 : 1;
1343 fld
->fixed_parent
= (offsetParent
==-1) ? 0 : 1;
1345 fld
->field_size
= size
;
1351 /*****************************************************************************
1353 * getIntNumber : get an integer number
1355 * size : the size of the integer
1356 * evD : the event data
1359 ****************************************************************************/
1361 int getIntNumber(int size
, void *evD
)
1364 if(size
== 1) i
= *(gint8
*)evD
;
1365 else if(size
== 2) i
= *(gint16
*)evD
;
1366 else if(size
== 4) i
= *(gint32
*)evD
;
1367 else if(size
== 8) i
= *(gint64
*)evD
;
1372 /*****************************************************************************
1374 * getDataEndianType : get the data type size and endian type of the local
1377 * size : size of data type
1378 * endian : endian type, little or big
1379 ****************************************************************************/
1381 void getDataEndianType(LttArchSize
* size
, LttArchEndian
* endian
)
1385 int sizeInt
=sizeof(int), sizeLong
=sizeof(long), sizePointer
=sizeof(void *);
1387 if(c
== 1) *endian
= LTT_LITTLE_ENDIAN
;
1388 else *endian
= LTT_BIG_ENDIAN
;
1390 if(sizeInt
== 2 && sizeLong
== 4 && sizePointer
== 4)
1392 else if(sizeInt
== 4 && sizeLong
== 4 && sizePointer
== 4)
1394 else if(sizeInt
== 4 && sizeLong
== 8 && sizePointer
== 8)
1396 else if(sizeInt
== 8 && sizeLong
== 8 && sizePointer
== 8)
1398 else *size
= LTT_UNKNOWN
;
1401 /* get the node name of the system */
1403 char * ltt_trace_system_description_node_name (LttSystemDescription
* s
)
1405 return s
->node_name
;
1409 /* get the domain name of the system */
1411 char * ltt_trace_system_description_domain_name (LttSystemDescription
* s
)
1413 return s
->domain_name
;
1417 /* get the description of the system */
1419 char * ltt_trace_system_description_description (LttSystemDescription
* s
)
1421 return s
->description
;
1425 /* get the start time of the trace */
1427 LttTime
ltt_trace_system_description_trace_start_time(LttSystemDescription
*s
)
1429 return s
->trace_start
;