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,
24 #include <sys/types.h>
35 #include "ltt-private.h"
36 #include <ltt/trace.h>
37 #include <ltt/facility.h>
38 #include <ltt/event.h>
41 #define DIR_NAME_SIZE 256
42 #define __UNUSED__ __attribute__((__unused__))
44 #define g_info(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, format)
45 #define g_debug(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format)
48 /* obtain the time of an event */
50 static inline LttTime
getEventTime(LttTracefile
* tf
);
53 /* set the offset of the fields belonging to the event,
54 need the information of the archecture */
55 void setFieldsOffset(LttTracefile
*tf
,LttEventType
*evT
,void *evD
,LttTrace
*t
);
57 /* get the size of the field type according to the archtecture's
58 size and endian type(info of the archecture) */
59 int getFieldtypeSize(LttTracefile
* tf
, LttEventType
* evT
, int offsetRoot
,
60 int offsetParent
, LttField
*fld
, void *evD
, LttTrace
* t
);
62 /* read a fixed size or a block information from the file (fd) */
63 int readFile(int fd
, void * buf
, size_t size
, char * mesg
);
64 int readBlock(LttTracefile
* tf
, int whichBlock
);
66 /* calculate cycles per nsec for current block */
67 void getCyclePerNsec(LttTracefile
* t
);
69 /* reinitialize the info of the block which is already in the buffer */
70 void updateTracefile(LttTracefile
* tf
);
72 /* go to the next event */
73 int skipEvent(LttTracefile
* t
);
76 /* Functions to parse system.xml file (using glib xml parser) */
77 static void parser_start_element (GMarkupParseContext __UNUSED__
*context
,
78 const gchar
*element_name
,
79 const gchar
**attribute_names
,
80 const gchar
**attribute_values
,
85 LttSystemDescription
* des
= (LttSystemDescription
* )user_data
;
86 if(strcmp("system", element_name
)){
87 *error
= g_error_new(G_MARKUP_ERROR
,
89 "This is not system.xml file");
93 while(attribute_names
[i
]){
94 if(strcmp("node_name", attribute_names
[i
])==0){
95 des
->node_name
= g_strdup(attribute_values
[i
]);
96 }else if(strcmp("domainname", attribute_names
[i
])==0){
97 des
->domain_name
= g_strdup(attribute_values
[i
]);
98 }else if(strcmp("cpu", attribute_names
[i
])==0){
99 des
->nb_cpu
= atoi(attribute_values
[i
]);
100 }else if(strcmp("arch_size", attribute_names
[i
])==0){
101 if(strcmp(attribute_values
[i
],"LP32") == 0) des
->size
= LTT_LP32
;
102 else if(strcmp(attribute_values
[i
],"ILP32") == 0) des
->size
= LTT_ILP32
;
103 else if(strcmp(attribute_values
[i
],"LP64") == 0) des
->size
= LTT_LP64
;
104 else if(strcmp(attribute_values
[i
],"ILP64") == 0) des
->size
= LTT_ILP64
;
105 else if(strcmp(attribute_values
[i
],"UNKNOWN") == 0) des
->size
= LTT_UNKNOWN
;
106 }else if(strcmp("endian", attribute_names
[i
])==0){
107 if(strcmp(attribute_values
[i
],"LITTLE_ENDIAN") == 0)
108 des
->endian
= LTT_LITTLE_ENDIAN
;
109 else if(strcmp(attribute_values
[i
],"BIG_ENDIAN") == 0)
110 des
->endian
= LTT_BIG_ENDIAN
;
111 }else if(strcmp("kernel_name", attribute_names
[i
])==0){
112 des
->kernel_name
= g_strdup(attribute_values
[i
]);
113 }else if(strcmp("kernel_release", attribute_names
[i
])==0){
114 des
->kernel_release
= g_strdup(attribute_values
[i
]);
115 }else if(strcmp("kernel_version", attribute_names
[i
])==0){
116 des
->kernel_version
= g_strdup(attribute_values
[i
]);
117 }else if(strcmp("machine", attribute_names
[i
])==0){
118 des
->machine
= g_strdup(attribute_values
[i
]);
119 }else if(strcmp("processor", attribute_names
[i
])==0){
120 des
->processor
= g_strdup(attribute_values
[i
]);
121 }else if(strcmp("hardware_platform", attribute_names
[i
])==0){
122 des
->hardware_platform
= g_strdup(attribute_values
[i
]);
123 }else if(strcmp("operating_system", attribute_names
[i
])==0){
124 des
->operating_system
= g_strdup(attribute_values
[i
]);
125 }else if(strcmp("ltt_major_version", attribute_names
[i
])==0){
126 des
->ltt_major_version
= atoi(attribute_values
[i
]);
127 }else if(strcmp("ltt_minor_version", attribute_names
[i
])==0){
128 des
->ltt_minor_version
= atoi(attribute_values
[i
]);
129 }else if(strcmp("ltt_block_size", attribute_names
[i
])==0){
130 des
->ltt_block_size
= atoi(attribute_values
[i
]);
132 *error
= g_error_new(G_MARKUP_ERROR
,
134 "Not a valid attribute");
141 static void parser_characters (GMarkupParseContext __UNUSED__
*context
,
143 gsize __UNUSED__ text_len
,
145 GError __UNUSED__
**error
)
147 LttSystemDescription
* des
= (LttSystemDescription
* )user_data
;
148 des
->description
= g_strdup(text
);
152 /*****************************************************************************
154 * ltt_tracefile_open : open a trace file, construct a LttTracefile
156 * t : the trace containing the tracefile
157 * fileName : path name of the trace file
159 * : a pointer to a tracefile
160 ****************************************************************************/
162 LttTracefile
* ltt_tracefile_open(LttTrace
* t
, char * fileName
)
165 struct stat lTDFStat
; /* Trace data file status */
167 tf
= g_new(LttTracefile
, 1);
170 tf
->name
= g_strdup(fileName
);
172 tf
->fd
= open(fileName
, O_RDONLY
, 0);
174 g_warning("Unable to open input data file %s\n", fileName
);
180 // Get the file's status
181 if(fstat(tf
->fd
, &lTDFStat
) < 0){
182 g_warning("Unable to get the status of the input data file %s\n", fileName
);
189 // Is the file large enough to contain a trace
190 if(lTDFStat
.st_size
< (off_t
)(sizeof(BlockStart
) + EVENT_HEADER_SIZE
)){
191 g_print("The input data file %s does not contain a trace\n", fileName
);
198 //store the size of the file
199 tf
->file_size
= lTDFStat
.st_size
;
200 tf
->block_size
= t
->system_description
->ltt_block_size
;
201 tf
->block_number
= tf
->file_size
/ tf
->block_size
;
204 //allocate memory to contain the info of a block
205 tf
->buffer
= (void *) g_new(char, t
->system_description
->ltt_block_size
);
207 //read the first block
208 if(readBlock(tf
,1)) exit(1);
214 /*****************************************************************************
215 *Open control and per cpu tracefiles
216 ****************************************************************************/
218 void ltt_tracefile_open_cpu(LttTrace
*t
, char * tracefile_name
)
221 tf
= ltt_tracefile_open(t
,tracefile_name
);
223 t
->per_cpu_tracefile_number
++;
224 g_ptr_array_add(t
->per_cpu_tracefiles
, tf
);
227 gint
ltt_tracefile_open_control(LttTrace
*t
, char * control_name
)
236 tf
= ltt_tracefile_open(t
,control_name
);
238 g_warning("ltt_tracefile_open_control : bad file descriptor");
241 t
->control_tracefile_number
++;
242 g_ptr_array_add(t
->control_tracefiles
,tf
);
244 //parse facilities tracefile to get base_id
245 if(strcmp(&control_name
[strlen(control_name
)-10],"facilities") ==0){
247 if(!ltt_tracefile_read(tf
,&ev
)) return 0; // end of file
249 if(ev
.event_id
== TRACE_FACILITY_LOAD
){
251 fLoad
.name
= (char*)pos
;
252 fLoad
.checksum
= *(LttChecksum
*)(pos
+ strlen(fLoad
.name
));
253 fLoad
.base_code
= *(guint32
*)(pos
+ strlen(fLoad
.name
) + sizeof(LttChecksum
));
255 for(i
=0;i
<t
->facility_number
;i
++){
256 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
,i
);
257 if(strcmp(f
->name
,fLoad
.name
)==0 && fLoad
.checksum
==f
->checksum
){
258 f
->base_id
= fLoad
.base_code
;
262 if(i
==t
->facility_number
) {
263 g_warning("Facility: %s, checksum: %u is not found",
264 fLoad
.name
,(unsigned int)fLoad
.checksum
);
267 }else if(ev
.event_id
== TRACE_BLOCK_START
){
269 }else if(ev
.event_id
== TRACE_BLOCK_END
){
272 g_warning("Not valid facilities trace file");
280 /*****************************************************************************
282 * ltt_tracefile_close: close a trace file,
284 * t : tracefile which will be closed
285 ****************************************************************************/
287 void ltt_tracefile_close(LttTracefile
*t
)
296 /*****************************************************************************
297 *Get system information
298 ****************************************************************************/
299 gint
getSystemInfo(LttSystemDescription
* des
, char * pathname
)
302 char buf
[DIR_NAME_SIZE
];
304 GMarkupParseContext
* context
;
305 GError
* error
= NULL
;
306 GMarkupParser markup_parser
=
308 parser_start_element
,
311 NULL
, /* passthrough */
315 fp
= fopen(pathname
,"r");
317 g_warning("Can not open file : %s\n", pathname
);
321 context
= g_markup_parse_context_new(&markup_parser
, 0, des
,NULL
);
323 while(fgets(buf
,DIR_NAME_SIZE
, fp
) != NULL
){
324 if(!g_markup_parse_context_parse(context
, buf
, DIR_NAME_SIZE
, &error
)){
326 g_warning("Can not parse xml file: \n%s\n", error
->message
);
329 g_markup_parse_context_free(context
);
334 g_markup_parse_context_free(context
);
339 /*****************************************************************************
340 *The following functions get facility/tracefile information
341 ****************************************************************************/
343 gint
getFacilityInfo(LttTrace
*t
, char* eventdefs
)
346 struct dirent
*entry
;
351 char name
[DIR_NAME_SIZE
];
353 dir
= opendir(eventdefs
);
355 g_warning("Can not open directory: %s\n", eventdefs
);
359 while((entry
= readdir(dir
)) != NULL
){
360 ptr
= &entry
->d_name
[strlen(entry
->d_name
)-4];
361 if(strcmp(ptr
,".xml") != 0) continue;
362 strcpy(name
,eventdefs
);
363 strcat(name
,entry
->d_name
);
364 ltt_facility_open(t
,name
);
368 for(j
=0;j
<t
->facility_number
;j
++){
369 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, j
);
370 for(i
=0; i
<f
->event_number
; i
++){
372 setFieldsOffset(NULL
, et
, NULL
, t
);
378 gint
getControlFileInfo(LttTrace
*t
, char* control
)
381 struct dirent
*entry
;
382 char name
[DIR_NAME_SIZE
];
384 dir
= opendir(control
);
386 g_warning("Can not open directory: %s\n", control
);
390 while((entry
= readdir(dir
)) != NULL
){
391 if(strcmp(entry
->d_name
,"facilities") != 0 &&
392 strcmp(entry
->d_name
,"interrupts") != 0 &&
393 strcmp(entry
->d_name
,"processes") != 0) continue;
395 strcpy(name
,control
);
396 strcat(name
,entry
->d_name
);
397 if(ltt_tracefile_open_control(t
,name
))
404 gint
getCpuFileInfo(LttTrace
*t
, char* cpu
)
407 struct dirent
*entry
;
408 char name
[DIR_NAME_SIZE
];
412 g_warning("Can not open directory: %s\n", cpu
);
416 while((entry
= readdir(dir
)) != NULL
){
417 if(strcmp(entry
->d_name
,".") != 0 &&
418 strcmp(entry
->d_name
,"..") != 0 &&
419 strcmp(entry
->d_name
,".svn") != 0){
421 strcat(name
,entry
->d_name
);
422 ltt_tracefile_open_cpu(t
,name
);
429 /*****************************************************************************
430 *A trace is specified as a pathname to the directory containing all the
431 *associated data (control tracefiles, per cpu tracefiles, event
434 *When a trace is closed, all the associated facilities, types and fields
435 *are released as well.
439 /****************************************************************************
440 * get_absolute_pathname
442 * return the unique pathname in the system
444 * MD : Fixed this function so it uses realpath, dealing well with
445 * forgotten cases (.. were not used correctly before).
447 ****************************************************************************/
448 void get_absolute_pathname(const char *pathname
, char * abs_pathname
)
450 abs_pathname
[0] = '\0';
452 if ( realpath (pathname
, abs_pathname
) != NULL
)
456 /* error, return the original path unmodified */
457 strcpy(abs_pathname
, pathname
);
463 LttTrace
*ltt_trace_open(const char *pathname
)
466 LttSystemDescription
* sys_description
;
467 char eventdefs
[DIR_NAME_SIZE
];
468 char info
[DIR_NAME_SIZE
];
469 char control
[DIR_NAME_SIZE
];
470 char cpu
[DIR_NAME_SIZE
];
471 char tmp
[DIR_NAME_SIZE
];
472 char abs_path
[DIR_NAME_SIZE
];
473 gboolean has_slash
= FALSE
;
475 get_absolute_pathname(pathname
, abs_path
);
476 //establish the pathname to different directories
477 if(abs_path
[strlen(abs_path
)-1] == '/')has_slash
= TRUE
;
478 strcpy(eventdefs
,abs_path
);
479 if(!has_slash
)strcat(eventdefs
,"/");
480 strcat(eventdefs
,"eventdefs/");
482 strcpy(info
,abs_path
);
483 if(!has_slash
)strcat(info
,"/");
484 strcat(info
,"info/");
486 strcpy(control
,abs_path
);
487 if(!has_slash
)strcat(control
,"/");
488 strcat(control
,"control/");
490 strcpy(cpu
,abs_path
);
491 if(!has_slash
)strcat(cpu
,"/");
495 sys_description
= g_new(LttSystemDescription
, 1);
496 t
= g_new(LttTrace
, 1);
497 t
->pathname
= g_strdup(abs_path
);
498 t
->facility_number
= 0;
499 t
->control_tracefile_number
= 0;
500 t
->per_cpu_tracefile_number
= 0;
501 t
->system_description
= sys_description
;
502 t
->control_tracefiles
= g_ptr_array_new();
503 t
->per_cpu_tracefiles
= g_ptr_array_new();
504 t
->facilities
= g_ptr_array_new();
505 getDataEndianType(&(t
->my_arch_size
), &(t
->my_arch_endian
));
507 //get system description
509 strcat(tmp
,"system.xml");
510 if(getSystemInfo(sys_description
, tmp
)) {
511 g_ptr_array_free(t
->facilities
, TRUE
);
512 g_ptr_array_free(t
->per_cpu_tracefiles
, TRUE
);
513 g_ptr_array_free(t
->control_tracefiles
, TRUE
);
514 g_free(sys_description
);
522 //get facilities info
523 if(getFacilityInfo(t
,eventdefs
)) {
524 g_ptr_array_free(t
->facilities
, TRUE
);
525 g_ptr_array_free(t
->per_cpu_tracefiles
, TRUE
);
526 g_ptr_array_free(t
->control_tracefiles
, TRUE
);
527 g_free(sys_description
);
533 //get control tracefile info
534 getControlFileInfo(t
,control
);
536 if(getControlFileInfo(t,control)) {
537 g_ptr_array_free(t->facilities, TRUE);
538 g_ptr_array_free(t->per_cpu_tracefiles, TRUE);
539 g_ptr_array_free(t->control_tracefiles, TRUE);
540 g_free(sys_description);
544 }*/ // With fatal error
546 //get cpu tracefile info
547 if(getCpuFileInfo(t
,cpu
)) {
548 g_ptr_array_free(t
->facilities
, TRUE
);
549 g_ptr_array_free(t
->per_cpu_tracefiles
, TRUE
);
550 g_ptr_array_free(t
->control_tracefiles
, TRUE
);
551 g_free(sys_description
);
560 char * ltt_trace_name(LttTrace
*t
)
566 /******************************************************************************
567 * When we copy a trace, we want all the opening actions to happen again :
568 * the trace will be reopened and totally independant from the original.
569 * That's why we call ltt_trace_open.
570 *****************************************************************************/
571 LttTrace
*ltt_trace_copy(LttTrace
*self
)
573 return ltt_trace_open(self
->pathname
);
576 void ltt_trace_close(LttTrace
*t
)
584 //free system_description
585 g_free(t
->system_description
->description
);
586 g_free(t
->system_description
->node_name
);
587 g_free(t
->system_description
->domain_name
);
588 g_free(t
->system_description
->kernel_name
);
589 g_free(t
->system_description
->kernel_release
);
590 g_free(t
->system_description
->kernel_version
);
591 g_free(t
->system_description
->machine
);
592 g_free(t
->system_description
->processor
);
593 g_free(t
->system_description
->hardware_platform
);
594 g_free(t
->system_description
->operating_system
);
595 g_free(t
->system_description
);
597 //free control_tracefiles
598 for(i
=0;i
<t
->control_tracefile_number
;i
++){
599 tf
= (LttTracefile
*)g_ptr_array_index(t
->control_tracefiles
,i
);
600 ltt_tracefile_close(tf
);
602 g_ptr_array_free(t
->control_tracefiles
, TRUE
);
604 //free per_cpu_tracefiles
605 for(i
=0;i
<t
->per_cpu_tracefile_number
;i
++){
606 tf
= (LttTracefile
*)g_ptr_array_index(t
->per_cpu_tracefiles
,i
);
607 ltt_tracefile_close(tf
);
609 g_ptr_array_free(t
->per_cpu_tracefiles
, TRUE
);
612 for(i
=0;i
<t
->facility_number
;i
++){
613 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
,i
);
614 ltt_facility_close(f
);
616 g_ptr_array_free(t
->facilities
, TRUE
);
624 /*****************************************************************************
625 *Get the system description of the trace
626 ****************************************************************************/
628 LttSystemDescription
*ltt_trace_system_description(LttTrace
*t
)
630 return t
->system_description
;
633 /*****************************************************************************
634 * The following functions discover the facilities of the trace
635 ****************************************************************************/
637 unsigned ltt_trace_facility_number(LttTrace
*t
)
639 return (unsigned)(t
->facility_number
);
642 LttFacility
*ltt_trace_facility_get(LttTrace
*t
, unsigned i
)
644 return (LttFacility
*)g_ptr_array_index(t
->facilities
, i
);
647 /*****************************************************************************
649 * ltt_trace_facility_find : find facilities in the trace
652 * name : facility name
654 * position : position of the facility in the trace
656 * : the number of facilities
657 ****************************************************************************/
659 unsigned ltt_trace_facility_find(LttTrace
*t
, char *name
, unsigned *position
)
661 unsigned int i
, count
=0;
663 for(i
=0;i
<t
->facility_number
;i
++){
664 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, i
);
665 if(strcmp(f
->name
,name
)==0){
667 if(count
==1) *position
= i
;
675 /*****************************************************************************
676 * Functions to discover all the event types in the trace
677 ****************************************************************************/
679 unsigned ltt_trace_eventtype_number(LttTrace
*t
)
684 for(i
=0;i
<t
->facility_number
;i
++){
685 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, i
);
686 count
+= f
->event_number
;
691 /* FIXME : performances could be improved with a better design for this
693 LttFacility
* ltt_trace_facility_by_id(LttTrace
* trace
, unsigned id
)
695 LttFacility
* facility
= NULL
;
698 for(i
=0;i
<trace
->facility_number
;i
++){
699 LttFacility
*iter_facility
=
700 (LttFacility
*) g_ptr_array_index(trace
->facilities
,i
);
701 if(unlikely(id
>= iter_facility
->base_id
&&
702 id
< iter_facility
->base_id
+ iter_facility
->event_number
)) {
703 facility
= iter_facility
;
711 LttEventType
*ltt_trace_eventtype_get(LttTrace
*t
, unsigned evId
)
714 f
= ltt_trace_facility_by_id(t
,evId
);
716 return f
->events
[evId
- f
->base_id
];
719 /*****************************************************************************
720 *There is one "per cpu" tracefile for each CPU, numbered from 0 to
721 *the maximum number of CPU in the system. When the number of CPU installed
722 *is less than the maximum, some positions are unused. There are also a
723 *number of "control" tracefiles (facilities, interrupts...).
724 ****************************************************************************/
725 unsigned ltt_trace_control_tracefile_number(LttTrace
*t
)
727 return t
->control_tracefile_number
;
730 unsigned ltt_trace_per_cpu_tracefile_number(LttTrace
*t
)
732 return t
->per_cpu_tracefile_number
;
735 /*****************************************************************************
736 *It is possible to search for the tracefiles by name or by CPU position.
737 *The index within the tracefiles of the same type is returned if found
738 *and a negative value otherwise.
739 ****************************************************************************/
741 int ltt_trace_control_tracefile_find(LttTrace
*t
, const gchar
*name
)
743 LttTracefile
* tracefile
;
745 for(i
=0;i
<t
->control_tracefile_number
;i
++){
746 tracefile
= (LttTracefile
*)g_ptr_array_index(t
->control_tracefiles
, i
);
747 if(strcmp(tracefile
->name
, name
)==0)break;
749 if(i
== t
->control_tracefile_number
) return -1;
753 /* not really useful. We just have to know that cpu tracefiles
754 * comes before control tracefiles.
756 int ltt_trace_per_cpu_tracefile_find(LttTrace
*t
, const gchar
*name
)
758 LttTracefile
* tracefile
;
760 for(i
=0;i
<t
->per_cpu_tracefile_number
;i
++){
761 tracefile
= (LttTracefile
*)g_ptr_array_index(t
->per_cpu_tracefiles
, i
);
762 if(strcmp(tracefile
->name
, name
)==0)break;
764 if(i
== t
->per_cpu_tracefile_number
) return -1;
768 /*****************************************************************************
769 *Get a specific tracefile
770 ****************************************************************************/
772 LttTracefile
*ltt_trace_control_tracefile_get(LttTrace
*t
, unsigned i
)
774 return (LttTracefile
*)g_ptr_array_index(t
->control_tracefiles
, i
);
777 LttTracefile
*ltt_trace_per_cpu_tracefile_get(LttTrace
*t
, unsigned i
)
779 return (LttTracefile
*)g_ptr_array_index(t
->per_cpu_tracefiles
, i
);
782 /*****************************************************************************
783 * Get the start time and end time of the trace
784 ****************************************************************************/
786 void ltt_trace_time_span_get(LttTrace
*t
, LttTime
*start
, LttTime
*end
)
788 LttTime startSmall
, startTmp
, endBig
, endTmp
;
792 for(i
=0;i
<t
->control_tracefile_number
;i
++){
793 tf
= g_ptr_array_index(t
->control_tracefiles
, i
);
795 startTmp
= tf
->a_block_start
->time
;
796 readBlock(tf
,tf
->block_number
);
797 endTmp
= tf
->a_block_end
->time
;
799 startSmall
= startTmp
;
804 if(ltt_time_compare(startSmall
,startTmp
) > 0) startSmall
= startTmp
;
805 if(ltt_time_compare(endBig
,endTmp
) < 0) endBig
= endTmp
;
808 for(i
=0;i
<t
->per_cpu_tracefile_number
;i
++){
809 tf
= g_ptr_array_index(t
->per_cpu_tracefiles
, i
);
811 startTmp
= tf
->a_block_start
->time
;
812 readBlock(tf
,tf
->block_number
);
813 endTmp
= tf
->a_block_end
->time
;
815 startSmall
= startTmp
;
819 if(ltt_time_compare(startSmall
,startTmp
) > 0) startSmall
= startTmp
;
820 if(ltt_time_compare(endBig
,endTmp
) < 0) endBig
= endTmp
;
823 if(start
!= NULL
) *start
= startSmall
;
824 if(end
!= NULL
) *end
= endBig
;
828 /*****************************************************************************
829 *Get the name of a tracefile
830 ****************************************************************************/
832 char *ltt_tracefile_name(LttTracefile
*tf
)
837 /*****************************************************************************
838 * Get the number of blocks in the tracefile
839 ****************************************************************************/
841 unsigned ltt_tracefile_block_number(LttTracefile
*tf
)
843 return tf
->block_number
;
846 /*****************************************************************************
848 * ltt_tracefile_seek_time: seek to the first event of the trace with time
849 * larger or equal to time
852 * time : criteria of the time
853 ****************************************************************************/
854 void ltt_tracefile_find_time_block(LttTracefile
*t
, LttTime time
,
855 int start_block
, int end_block
)
857 int err
, tmp_block
, s
, e
;
861 err
=readBlock(t
,start_block
);
862 if(err
) g_error("Can not read tracefile: %s\n", t
->name
);
863 if(start_block
== end_block
)return;
865 tailTime
= ltt_time_compare(t
->a_block_end
->time
, time
);
866 if(tailTime
>= 0) return;
868 err
=readBlock(t
,end_block
);
869 if(err
) g_error("Can not read tracefile: %s\n", t
->name
);
870 if(start_block
+1 == end_block
)return;
872 headTime
= ltt_time_compare(t
->a_block_start
->time
, time
);
873 if(headTime
<= 0 ) return;
875 tmp_block
= (end_block
+ start_block
)/2;
876 err
=readBlock(t
,tmp_block
);
877 if(err
) g_error("Can not read tracefile: %s\n", t
->name
);
879 headTime
= ltt_time_compare(t
->a_block_start
->time
, time
);
880 tailTime
= ltt_time_compare(t
->a_block_end
->time
, time
);
881 if(headTime
<= 0 && tailTime
>= 0) return;
887 ltt_tracefile_find_time_block(t
, time
, s
, e
);
895 ltt_tracefile_find_time_block(t
, time
, s
, e
);
900 void ltt_tracefile_backward_find_time_block(LttTracefile
*t
, LttTime time
)
902 int t_time
, h_time
, err
;
903 err
=readBlock(t
,t
->which_block
-1);
904 if(err
) g_error("Can not read tracefile: %s\n", t
->name
);
905 h_time
= ltt_time_compare(t
->a_block_start
->time
, time
);
906 t_time
= ltt_time_compare(t
->a_block_end
->time
, time
);
909 if(t
->which_block
== 1) return;
910 err
=readBlock(t
,t
->which_block
-1);
911 if(err
) g_error("Can not read tracefile: %s\n", t
->name
);
912 tmp
= ltt_time_compare(t
->a_block_end
->time
, time
);
913 if(tmp
== 0) return ltt_tracefile_seek_time(t
, time
);
914 err
=readBlock(t
,t
->which_block
+1);
915 if(err
) g_error("Can not read tracefile: %s\n", t
->name
);
916 }else if(h_time
> 0){
917 ltt_tracefile_find_time_block(t
, time
, 1, t
->which_block
);
918 return ltt_tracefile_seek_time(t
, time
) ;
920 if(t_time
>= 0) return ltt_tracefile_seek_time(t
, time
);
921 err
=readBlock(t
,t
->which_block
+1);
922 if(err
) g_error("Can not read tracefile: %s\n", t
->name
);
926 void ltt_tracefile_seek_time(LttTracefile
*t
, LttTime time
)
930 int headTime
= ltt_time_compare(t
->a_block_start
->time
, time
);
931 int tailTime
= ltt_time_compare(t
->a_block_end
->time
, time
);
934 if(headTime
< 0 && tailTime
> 0){
935 if(ltt_time_compare(t
->a_block_end
->time
, t
->current_event_time
) !=0) {
936 lttTime
= getEventTime(t
);
937 err
= ltt_time_compare(lttTime
, time
);
939 if(t
->which_event
==2 || ltt_time_compare(t
->prev_event_time
,time
)<0){
943 return ltt_tracefile_seek_time(t
, time
);
947 if(ltt_tracefile_read(t
,&ev
) == NULL
) {
948 g_print("End of file\n");
951 lttTime
= getEventTime(t
);
952 err
= ltt_time_compare(lttTime
, time
);
956 }else{//we are at the end of the block
958 return ltt_tracefile_seek_time(t
, time
);
960 }else if(headTime
>= 0){
961 if(t
->which_block
== 1){
964 if(ltt_time_compare(t
->prev_block_end_time
, time
) >= 0 ||
965 (t
->prev_block_end_time
.tv_sec
== 0 &&
966 t
->prev_block_end_time
.tv_nsec
== 0 )){
967 ltt_tracefile_backward_find_time_block(t
, time
);
972 }else if(tailTime
< 0){
973 if(t
->which_block
!= t
->block_number
){
974 ltt_tracefile_find_time_block(t
, time
, t
->which_block
+1, t
->block_number
);
975 return ltt_tracefile_seek_time(t
, time
);
977 t
->cur_event_pos
= t
->buffer
+ t
->block_size
;
978 g_print("End of file\n");
981 }else if(tailTime
== 0){
982 t
->cur_event_pos
= t
->last_event_pos
;
983 t
->current_event_time
= time
;
984 t
->cur_heart_beat_number
= 0;
985 t
->prev_event_time
.tv_sec
= 0;
986 t
->prev_event_time
.tv_nsec
= 0;
991 /*****************************************************************************
992 * Seek to the first event with position equal or larger to ep
994 * Modified by Mathieu Desnoyers to used faster offset position instead of
995 * re-reading the whole buffer.
996 ****************************************************************************/
998 void ltt_tracefile_seek_position(LttTracefile
*t
, const LttEventPosition
*ep
)
1000 //if we are at the right place, just return
1001 if(likely(t
->which_block
== ep
->block_num
&& t
->which_event
== ep
->event_num
))
1004 if(likely(t
->which_block
== ep
->block_num
)) updateTracefile(t
);
1005 else readBlock(t
,ep
->block_num
);
1006 //event offset is available
1007 if(likely(ep
->old_position
)){
1010 t
->which_event
= ep
->event_num
;
1011 t
->cur_event_pos
= t
->buffer
+ ep
->event_offset
;
1012 t
->prev_event_time
= ep
->event_time
;
1013 t
->current_event_time
= ep
->event_time
;
1014 t
->cur_heart_beat_number
= ep
->heart_beat_number
;
1015 t
->cur_cycle_count
= ep
->event_cycle_count
;
1017 /* This is a workaround for fast position seek */
1018 t
->last_event_pos
= ep
->last_event_pos
;
1019 t
->prev_block_end_time
= ep
->prev_block_end_time
;
1020 t
->prev_event_time
= ep
->prev_event_time
;
1021 t
->pre_cycle_count
= ep
->pre_cycle_count
;
1022 t
->count
= ep
->count
;
1023 /* end of workaround */
1025 //update the fields of the current event and go to the next event
1027 if(unlikely(err
== ERANGE
)) g_error("event id is out of range\n");
1032 //only block number and event index are available
1033 //MD: warning : this is slow!
1034 g_warning("using slow O(n) tracefile seek position");
1037 while(likely(t
->which_event
< ep
->event_num
)) ltt_tracefile_read(t
, &event
);
1042 /*****************************************************************************
1044 * ltt_tracefile_read : read the current event, set the pointer to the next
1048 * LttEvent * : an event to be processed
1049 ****************************************************************************/
1051 LttEvent
*ltt_tracefile_read(LttTracefile
*t
, LttEvent
*event
)
1055 if(unlikely(t
->cur_event_pos
== t
->buffer
+ t
->block_size
)){
1056 if(unlikely(t
->which_block
== t
->block_number
)){
1059 err
= readBlock(t
, t
->which_block
+ 1);
1060 if(unlikely(err
))g_error("Can not read tracefile");
1063 event
->event_id
= (int)(*(guint16
*)(t
->cur_event_pos
));
1064 if(unlikely(event
->event_id
== TRACE_TIME_HEARTBEAT
))
1065 t
->cur_heart_beat_number
++;
1067 t
->prev_event_time
= t
->current_event_time
;
1068 // t->current_event_time = getEventTime(t);
1070 event
->time_delta
= *(guint32
*)(t
->cur_event_pos
+ EVENT_ID_SIZE
);
1071 event
->event_time
= t
->current_event_time
;
1072 event
->event_cycle_count
= t
->cur_cycle_count
;
1074 event
->tracefile
= t
;
1075 event
->data
= t
->cur_event_pos
+ EVENT_HEADER_SIZE
;
1076 event
->which_block
= t
->which_block
;
1077 event
->which_event
= t
->which_event
;
1079 /* This is a workaround for fast position seek */
1080 event
->last_event_pos
= t
->last_event_pos
;
1081 event
->prev_block_end_time
= t
->prev_block_end_time
;
1082 event
->prev_event_time
= t
->prev_event_time
;
1083 event
->pre_cycle_count
= t
->pre_cycle_count
;
1084 event
->count
= t
->count
;
1085 /* end of workaround */
1089 //update the fields of the current event and go to the next event
1091 if(unlikely(err
== ERANGE
)) g_error("event id is out of range\n");
1096 /****************************************************************************
1098 * readFile : wrap function to read from a file
1100 * fd : file descriptor
1101 * buf : buf to contain the content
1102 * size : number of bytes to be read
1103 * mesg : message to be printed if some thing goes wrong
1106 * EIO : can not read from the file
1107 ****************************************************************************/
1109 int readFile(int fd
, void * buf
, size_t size
, char * mesg
)
1111 ssize_t nbBytes
= read(fd
, buf
, size
);
1113 if((size_t)nbBytes
!= size
) {
1115 perror("Error in readFile : ");
1117 g_warning("%s",mesg
);
1124 /*****************************************************************************
1126 * skipEvent_pre_read_cycles : go to the next event,
1127 * update the necessary fields of the current event
1128 * increment the cycle counter, save it at the end.
1133 * ERANGE : event id is out of range
1134 ****************************************************************************/
1136 int skipEvent_pre_read_cycles(LttTracefile
* t
)
1143 evId
= (int)(*(guint16
*)(t
->cur_event_pos
));
1144 evData
= t
->cur_event_pos
+ EVENT_HEADER_SIZE
;
1146 evT
= ltt_trace_eventtype_get(t
->trace
,(unsigned)evId
);
1148 if(likely(evT
)) rootFld
= evT
->root_field
;
1151 if(likely(rootFld
)){
1152 //event has string/sequence or the last event is not the same event
1153 if(likely((evT
->latest_block
!=t
->which_block
|| evT
->latest_event
!=t
->which_event
)
1154 && rootFld
->field_fixed
== 0)){
1155 setFieldsOffset(t
, evT
, evData
, t
->trace
);
1157 t
->cur_event_pos
+= EVENT_HEADER_SIZE
+ rootFld
->field_size
;
1158 }else t
->cur_event_pos
+= EVENT_HEADER_SIZE
;
1160 //evT->latest_block = t->which_block;
1161 //evT->latest_event = t->which_event;
1163 //the next event is in the next block
1164 //if(unlikely(evId == TRACE_BLOCK_END)){
1165 // Specify end of buffer reached.
1166 // t->cur_event_pos = t->buffer + t->block_size;
1168 //g_critical("COUNT : %lu", t->cur_cycle_count);
1170 // t->current_event_time = getEventTime(t);
1179 /*****************************************************************************
1181 * ltt_tracefile_pre_read_cycles :
1182 * read the current event, increment the cycle counter
1186 * False : end of bloc reached
1187 ****************************************************************************/
1189 gboolean
ltt_tracefile_pre_read_cycles(LttTracefile
*tf
)
1194 // if(unlikely(t->cur_event_pos == t->buffer + t->block_size)){
1195 //if(unlikely(t->which_block == t->block_number)){
1198 // return FALSE; // end of bloc reached
1199 //err = readBlock(t, t->which_block + 1);
1200 //if(unlikely(err))g_error("Can not read tracefile");
1203 //event.event_id = (int)(*(guint16 *)(t->cur_event_pos));
1204 //if(unlikely(event.event_id == TRACE_TIME_HEARTBEAT))
1205 // t->cur_heart_beat_number++;
1207 //t->prev_event_time = t->current_event_time;
1208 // t->current_event_time = getEventTime(t);
1210 //event.time_delta = *(guint32 *)(t->cur_event_pos + EVENT_ID_SIZE);
1211 //event.event_time = t->current_event_time;
1212 //event.event_cycle_count = t->cur_cycle_count;
1214 //event.tracefile = t;
1215 //event.data = t->cur_event_pos + EVENT_HEADER_SIZE;
1216 //event.which_block = t->which_block;
1217 //event.which_event = t->which_event;
1219 /* This is a workaround for fast position seek */
1220 //event.last_event_pos = t->last_event_pos;
1221 //event.prev_block_end_time = t->prev_block_end_time;
1222 //event.prev_event_time = t->prev_event_time;
1223 //event.pre_cycle_count = t->pre_cycle_count;
1224 //event.count = t->count;
1225 /* end of workaround */
1228 /* Increment the cycle counter for the bloc */
1230 LttCycleCount cycle_count
; // cycle count for the current event
1231 LttCycleCount lEventTotalCycle
; // Total cycles from start for event
1232 LttCycleCount lEventNSec
; // Total nsecs from start for event
1233 LttTime lTimeOffset
; // Time offset in struct LttTime
1236 evId
= *(guint16
*)tf
->cur_event_pos
;
1238 // Calculate total time in cycles from start of buffer for this event
1239 cycle_count
= (LttCycleCount
)*(guint32
*)(tf
->cur_event_pos
+ EVENT_ID_SIZE
);
1240 //g_debug("event cycle count %llu", cycle_count);
1242 //gint64 delta_count = (gint64)(cycle_count - tf->pre_cycle_count);
1243 //LttCycleCount res_delta_count;
1244 gboolean comp_count
= cycle_count
< tf
->pre_cycle_count
;
1245 tf
->pre_cycle_count
= cycle_count
;
1247 if(unlikely(comp_count
)) {
1249 tf
->count
++; //increment wrap count
1252 //if(unlikely(cycle_count < tf->pre_cycle_count)) tf->count++;
1253 //if(unlikely(delta_count < 0)) {
1254 // tf->count++; //increment wrap count
1255 // keep in mind that delta_count is negative here.
1256 // res_delta_count = delta_count + 0x100000000ULL ;
1258 // res_delta_count = (LttCycleCount)delta_count;
1260 //cycle_count += (LttCycleCount)tf->count << 32;
1263 // if(tf->cur_heart_beat_number > tf->count)
1264 // cycle_count += (tf->cur_heart_beat_number - tf->count) << 32;
1266 //tf->cur_cycle_count = tf->cur_cycle_count + res_delta_count;
1267 tf
->cur_cycle_count
= cycle_count
| ((LttCycleCount
)tf
->count
<< 32);
1268 g_debug("cur cycle count %llu", tf
->cur_cycle_count
);
1273 if(unlikely(evId
== TRACE_BLOCK_START
)){
1274 g_debug("BLOCK START");
1275 }else if(unlikely(evId
== TRACE_BLOCK_END
)){
1276 g_debug("BLOCK END");
1278 /* The goal of all this pre reading */
1279 tf
->a_block_end
->cycle_count
= tf
->cur_cycle_count
;
1280 g_debug("end of block cycle count : %llu", tf
->cur_cycle_count
);
1285 //update the fields of the current event and go to the next event
1286 err
= skipEvent_pre_read_cycles(tf
);
1287 if(unlikely(err
== ERANGE
)) g_error("event id is out of range\n");
1293 /****************************************************************************
1295 * readBlock : read a block from the file
1297 * lttdes : ltt trace file
1298 * whichBlock : the block which will be read
1301 * EINVAL : lseek fail
1302 * EIO : can not read from the file
1303 ****************************************************************************/
1305 int readBlock(LttTracefile
* tf
, int whichBlock
)
1310 /* same block already opened requested */
1311 if((guint
)whichBlock
== tf
->which_block
) return 0;
1313 if(likely(whichBlock
- tf
->which_block
== 1 && tf
->which_block
!= 0)){
1314 tf
->prev_block_end_time
= tf
->a_block_end
->time
;
1315 tf
->prev_event_time
= tf
->a_block_end
->time
;
1317 tf
->prev_block_end_time
.tv_sec
= 0;
1318 tf
->prev_block_end_time
.tv_nsec
= 0;
1319 tf
->prev_event_time
.tv_sec
= 0;
1320 tf
->prev_event_time
.tv_nsec
= 0;
1323 nbBytes
=lseek(tf
->fd
,(off_t
)((whichBlock
-1)*tf
->block_size
), SEEK_SET
);
1324 if(unlikely(nbBytes
== -1)) return EINVAL
;
1326 if(unlikely(readFile(tf
->fd
,tf
->buffer
,tf
->block_size
,"Unable to read a block")))
1329 tf
->a_block_start
=(BlockStart
*) (tf
->buffer
+ EVENT_HEADER_SIZE
);
1330 lostSize
= *(guint32
*)(tf
->buffer
+ tf
->block_size
- sizeof(guint32
));
1331 tf
->a_block_end
=(BlockEnd
*)(tf
->buffer
+ tf
->block_size
-
1332 lostSize
+ EVENT_HEADER_SIZE
);
1333 tf
->last_event_pos
= tf
->buffer
+ tf
->block_size
- lostSize
;
1335 tf
->which_block
= whichBlock
;
1336 tf
->which_event
= 1;
1337 tf
->cur_event_pos
= tf
->buffer
;//the beginning of the block, block start ev
1338 tf
->cur_heart_beat_number
= 0;
1340 /* read the whole block to precalculate total of cycles in it */
1342 tf
->pre_cycle_count
= 0;
1343 tf
->cur_cycle_count
= 0;
1344 g_debug("precalculating cycles begin for block %i", whichBlock
);
1345 while(likely(ltt_tracefile_pre_read_cycles(tf
)));
1346 /* Rough approximation of cycles per usec to calculate
1347 * the real block start and end time.
1349 getCyclePerNsec(tf
);
1350 /* we are at end position, make end time more precise */
1351 tf
->a_block_end
->time
= getEventTime(tf
);
1353 g_debug("precalculating cycles end for block %i", whichBlock
);
1355 /* put back pointer at the beginning */
1357 tf
->pre_cycle_count
= 0;
1358 tf
->cur_cycle_count
= 0;
1359 tf
->which_event
= 1;
1360 tf
->cur_event_pos
= tf
->buffer
;//the beginning of the block, block start ev
1361 tf
->cur_heart_beat_number
= 0;
1363 /* Make start time more precise */
1364 tf
->a_block_start
->time
= getEventTime(tf
);
1366 /* recalculate the cycles per nsec, with now more precise start and end time
1368 getCyclePerNsec(tf
);
1370 tf
->current_event_time
= getEventTime(tf
);
1375 /*****************************************************************************
1377 * updateTracefile : reinitialize the info of the block which is already
1381 ****************************************************************************/
1383 void updateTracefile(LttTracefile
* tf
)
1385 tf
->which_event
= 1;
1386 tf
->cur_event_pos
= tf
->buffer
;
1387 tf
->current_event_time
= getEventTime(tf
);
1388 tf
->cur_heart_beat_number
= 0;
1390 tf
->prev_event_time
.tv_sec
= 0;
1391 tf
->prev_event_time
.tv_nsec
= 0;
1394 /*****************************************************************************
1396 * skipEvent : go to the next event, update the fields of the current event
1401 * ERANGE : event id is out of range
1402 ****************************************************************************/
1404 int skipEvent(LttTracefile
* t
)
1411 evId
= (int)(*(guint16
*)(t
->cur_event_pos
));
1412 evData
= t
->cur_event_pos
+ EVENT_HEADER_SIZE
;
1414 evT
= ltt_trace_eventtype_get(t
->trace
,(unsigned)evId
);
1416 if(likely(evT
)) rootFld
= evT
->root_field
;
1419 if(likely(rootFld
)){
1420 //event has string/sequence or the last event is not the same event
1421 if(likely((evT
->latest_block
!=t
->which_block
|| evT
->latest_event
!=t
->which_event
)
1422 && rootFld
->field_fixed
== 0)){
1423 setFieldsOffset(t
, evT
, evData
, t
->trace
);
1425 t
->cur_event_pos
+= EVENT_HEADER_SIZE
+ rootFld
->field_size
;
1426 }else t
->cur_event_pos
+= EVENT_HEADER_SIZE
;
1428 evT
->latest_block
= t
->which_block
;
1429 evT
->latest_event
= t
->which_event
;
1431 //the next event is in the next block
1432 if(unlikely(evId
== TRACE_BLOCK_END
)){
1433 t
->cur_event_pos
= t
->buffer
+ t
->block_size
;
1436 t
->current_event_time
= getEventTime(t
);
1443 /*****************************************************************************
1445 * getCyclePerNsec : calculate cycles per nsec for current block
1446 * MD: should have tracefile_read the whole block, so we know the
1447 * total of cycles in it before being called.
1450 ****************************************************************************/
1452 void getCyclePerNsec(LttTracefile
* t
)
1454 LttTime lBufTotalTime
; /* Total time for this buffer */
1455 double lBufTotalNSec
; /* Total time for this buffer in nsecs */
1456 double lBufTotalCycle
;/* Total cycles for this buffer */
1458 /* Calculate the total time for this buffer */
1459 lBufTotalTime
= ltt_time_sub(t
->a_block_end
->time
, t
->a_block_start
->time
);
1461 /* Calculate the total cycles for this bufffer */
1462 lBufTotalCycle
= t
->a_block_end
->cycle_count
;
1463 lBufTotalCycle
-= t
->a_block_start
->cycle_count
;
1465 /* Convert the total time to nsecs */
1466 lBufTotalNSec
= ltt_time_to_double(lBufTotalTime
);
1468 t
->nsec_per_cycle
= (double)lBufTotalNSec
/ (double)lBufTotalCycle
;
1469 /* See : http://www.azillionmonkeys.com/qed/adiv.html */
1470 // precalculate the reciprocal, so divisions will be really fast.
1471 // 2^32-1 == 0xFFFFFFFFULL
1473 // double int_res = lBufTotalCycle/lBufTotalNSec;
1474 // t->cycles_per_nsec_reciprocal =
1475 // ((0xFFFF+int_res)/int_res);
1480 /****************************************************************************
1482 * getEventTime : obtain the time of an event
1483 * NOTE : this function _really_ is on critical path.
1487 * LttTime : the time of the event
1488 ****************************************************************************/
1490 static inline LttTime
getEventTime(LttTracefile
* tf
)
1493 LttCycleCount cycle_count
; // cycle count for the current event
1494 LttCycleCount lEventTotalCycle
; // Total cycles from start for event
1495 LttCycleCount lEventNSec
; // Total nsecs from start for event
1496 LttTime lTimeOffset
; // Time offset in struct LttTime
1499 evId
= *(guint16
*)tf
->cur_event_pos
;
1500 //if(unlikely(evId == TRACE_BLOCK_START)){
1502 // tf->pre_cycle_count = 0;
1503 // tf->cur_cycle_count = tf->a_block_start->cycle_count;
1504 // return tf->a_block_start->time;
1505 //}//else if(unlikely(evId == TRACE_BLOCK_END)){
1507 //tf->pre_cycle_count = 0;
1508 //tf->cur_cycle_count = tf->a_block_end->cycle_count;
1509 //return tf->a_block_end->time;
1512 // Calculate total time in cycles from start of buffer for this event
1513 cycle_count
= (LttCycleCount
)*(guint32
*)(tf
->cur_event_pos
+ EVENT_ID_SIZE
);
1514 //g_debug("event cycle count %llu", cycle_count);
1516 //gint64 delta_count = (gint64)(cycle_count - tf->pre_cycle_count);
1517 //LttCycleCount res_delta_count;
1518 gboolean comp_count
= cycle_count
< tf
->pre_cycle_count
;
1519 tf
->pre_cycle_count
= cycle_count
;
1521 if(unlikely(comp_count
)) {
1523 tf
->count
++; //increment wrap count
1526 //if(unlikely(cycle_count < tf->pre_cycle_count)) tf->count++;
1527 //if(unlikely(delta_count < 0)) {
1528 // tf->count++; //increment wrap count
1529 // keep in mind that delta_count is negative here.
1530 // res_delta_count = delta_count + 0x100000000ULL ;
1532 // res_delta_count = (LttCycleCount)delta_count;
1534 //cycle_count += (LttCycleCount)tf->count << 32;
1537 // if(tf->cur_heart_beat_number > tf->count)
1538 // cycle_count += (tf->cur_heart_beat_number - tf->count) << 32;
1540 //tf->cur_cycle_count = tf->cur_cycle_count + res_delta_count;
1541 tf
->cur_cycle_count
= cycle_count
| ((LttCycleCount
)tf
->count
<< 32);
1542 g_debug("cur cycle count %llu", tf
->cur_cycle_count
);
1545 lEventTotalCycle
= tf
->cur_cycle_count
;
1550 // Calculate total time in cycles from start of buffer for this event
1551 cycle_count
= (LttCycleCount
)*(guint32
*)(tf
->cur_event_pos
+ EVENT_ID_SIZE
);
1553 if(unlikely(cycle_count
< tf
->pre_cycle_count
)) tf
->count
++;
1554 tf
->pre_cycle_count
= cycle_count
;
1555 cycle_count
+= (LttCycleCount
)tf
->count
<< 32;
1558 // if(tf->cur_heart_beat_number > tf->count)
1559 // cycle_count += (tf->cur_heart_beat_number - tf->count) << 32;
1561 tf
->cur_cycle_count
= cycle_count
;
1563 lEventTotalCycle
= cycle_count
;
1564 lEventTotalCycle
-= tf
->a_block_start
->cycle_count
;
1566 // Convert it to nsecs
1567 lEventNSec
= (double)lEventTotalCycle
* (double)tf
->nsec_per_cycle
;
1568 //lEventNSec = (tf->cycles_per_nsec_reciprocal * lEventTotalCycle) >> 16;
1570 // Determine offset in struct LttTime
1571 lTimeOffset
= ltt_time_from_double(lEventNSec
);
1573 time
= ltt_time_add(tf
->a_block_start
->time
, lTimeOffset
);
1578 /*****************************************************************************
1580 * setFieldsOffset : set offset of the fields
1582 * tracefile : opened trace file
1583 * evT : the event type
1584 * evD : event data, it may be NULL
1585 ****************************************************************************/
1587 void setFieldsOffset(LttTracefile
*tf
,LttEventType
*evT
,void *evD
,LttTrace
* t
)
1589 LttField
* rootFld
= evT
->root_field
;
1590 // rootFld->base_address = evD;
1593 rootFld
->field_size
= getFieldtypeSize(tf
, evT
, 0,0,rootFld
, evD
,t
);
1596 /*****************************************************************************
1598 * getFieldtypeSize: get the size of the field type (primitive type)
1600 * tracefile : opened trace file
1602 * offsetRoot : offset from the root
1603 * offsetParent : offset from the parrent
1605 * evD : event data, it may be NULL
1607 * int : size of the field
1608 ****************************************************************************/
1610 int getFieldtypeSize(LttTracefile
* t
, LttEventType
* evT
, int offsetRoot
,
1611 int offsetParent
, LttField
* fld
, void *evD
, LttTrace
*trace
)
1613 int size
, size1
, element_number
, i
, offset1
, offset2
;
1614 LttType
* type
= fld
->field_type
;
1617 if(unlikely(evT
->latest_block
==t
->which_block
&& evT
->latest_event
==t
->which_event
)){
1618 return fld
->field_size
;
1622 if(likely(fld
->field_fixed
== 1)){
1623 if(fld
== evT
->root_field
) return fld
->field_size
;
1626 switch(type
->type_class
) {
1628 element_number
= (int) type
->element_number
;
1629 if(fld
->field_fixed
== -1){
1630 size
= getFieldtypeSize(t
, evT
, offsetRoot
,
1631 0,fld
->child
[0], NULL
, trace
);
1632 if(size
== 0){ //has string or sequence
1633 fld
->field_fixed
= 0;
1635 fld
->field_fixed
= 1;
1636 size
*= element_number
;
1638 }else if(fld
->field_fixed
== 0){// has string or sequence
1640 for(i
=0;i
<element_number
;i
++){
1641 size
+= getFieldtypeSize(t
, evT
, offsetRoot
+size
,size
,
1642 fld
->child
[0], evD
+size
, trace
);
1644 }else size
= fld
->field_size
;
1648 size1
= (int) ltt_type_size(trace
, type
);
1649 if(fld
->field_fixed
== -1){
1650 fld
->sequ_number_size
= size1
;
1651 fld
->field_fixed
= 0;
1652 size
= getFieldtypeSize(t
, evT
, offsetRoot
,
1653 0,fld
->child
[0], NULL
, trace
);
1654 fld
->element_size
= size
;
1656 element_number
= getIntNumber(size1
,evD
);
1657 type
->element_number
= element_number
;
1658 if(fld
->element_size
> 0){
1659 size
= element_number
* fld
->element_size
;
1660 }else{//sequence has string or sequence
1662 for(i
=0;i
<element_number
;i
++){
1663 size
+= getFieldtypeSize(t
, evT
, offsetRoot
+size
+size1
,size
+size1
,
1664 fld
->child
[0], evD
+size
+size1
, trace
);
1673 if(fld
->field_fixed
== -1){
1674 fld
->field_fixed
= 0;
1676 size
= strlen((char*)evD
) + 1; //include end : '\0'
1681 element_number
= (int) type
->element_number
;
1683 if(fld
->field_fixed
== -1){
1684 offset1
= offsetRoot
;
1686 for(i
=0;i
<element_number
;i
++){
1687 size1
=getFieldtypeSize(t
, evT
,offset1
,offset2
,
1688 fld
->child
[i
], NULL
, trace
);
1689 if(size1
> 0 && size
>= 0){
1691 if(offset1
>= 0) offset1
+= size1
;
1700 fld
->field_fixed
= 0;
1702 }else fld
->field_fixed
= 1;
1703 }else if(fld
->field_fixed
== 0){
1704 offset1
= offsetRoot
;
1706 for(i
=0;i
<element_number
;i
++){
1707 size
=getFieldtypeSize(t
,evT
,offset1
,offset2
,
1708 fld
->child
[i
],evD
+offset2
, trace
);
1713 }else size
= fld
->field_size
;
1717 if(fld
->field_fixed
== -1){
1718 size
= (int) ltt_type_size(trace
, type
);
1719 fld
->field_fixed
= 1;
1720 }else size
= fld
->field_size
;
1727 if(type
->type_class
!= LTT_STRUCT
&& type
->type_class
!= LTT_ARRAY
&&
1728 type
->type_class
!= LTT_SEQUENCE
&& type
->type_class
!= LTT_STRING
){
1729 if(fld
->field_fixed
== -1){
1730 size
= (int) ltt_type_size(trace
, type
);
1731 fld
->field_fixed
= 1;
1732 }else size
= fld
->field_size
;
1734 }else if(type
->type_class
== LTT_ARRAY
){
1735 element_number
= (int) type
->element_number
;
1736 if(fld
->field_fixed
== -1){
1737 size
= getFieldtypeSize(t
, evT
, offsetRoot
,0,fld
->child
[0], NULL
, trace
);
1738 if(size
== 0){ //has string or sequence
1739 fld
->field_fixed
= 0;
1741 fld
->field_fixed
= 1;
1742 size
*= element_number
;
1744 }else if(fld
->field_fixed
== 0){// has string or sequence
1746 for(i
=0;i
<element_number
;i
++){
1747 size
+= getFieldtypeSize(t
, evT
, offsetRoot
+size
,size
,
1748 fld
->child
[0], evD
+size
, trace
);
1750 }else size
= fld
->field_size
;
1752 }else if(type
->type_class
== LTT_SEQUENCE
){
1753 size1
= (int) ltt_type_size(trace
, type
);
1754 if(fld
->field_fixed
== -1){
1755 fld
->sequ_number_size
= size1
;
1756 fld
->field_fixed
= 0;
1757 size
= getFieldtypeSize(t
, evT
, offsetRoot
,0,fld
->child
[0], NULL
, trace
);
1758 fld
->element_size
= size
;
1760 element_number
= getIntNumber(size1
,evD
);
1761 type
->element_number
= element_number
;
1762 if(fld
->element_size
> 0){
1763 size
= element_number
* fld
->element_size
;
1764 }else{//sequence has string or sequence
1766 for(i
=0;i
<element_number
;i
++){
1767 size
+= getFieldtypeSize(t
, evT
, offsetRoot
+size
+size1
,size
+size1
,
1768 fld
->child
[0], evD
+size
+size1
, trace
);
1774 }else if(type
->type_class
== LTT_STRING
){
1776 if(fld
->field_fixed
== -1){
1777 fld
->field_fixed
= 0;
1779 size
= strlen((char*)evD
) + 1; //include end : '\0'
1782 }else if(type
->type_class
== LTT_STRUCT
){
1783 element_number
= (int) type
->element_number
;
1785 if(fld
->field_fixed
== -1){
1786 offset1
= offsetRoot
;
1788 for(i
=0;i
<element_number
;i
++){
1789 size1
=getFieldtypeSize(t
, evT
,offset1
,offset2
, fld
->child
[i
], NULL
, trace
);
1790 if(size1
> 0 && size
>= 0){
1792 if(offset1
>= 0) offset1
+= size1
;
1801 fld
->field_fixed
= 0;
1803 }else fld
->field_fixed
= 1;
1804 }else if(fld
->field_fixed
== 0){
1805 offset1
= offsetRoot
;
1807 for(i
=0;i
<element_number
;i
++){
1808 size
=getFieldtypeSize(t
,evT
,offset1
,offset2
,fld
->child
[i
],evD
+offset2
, trace
);
1813 }else size
= fld
->field_size
;
1817 fld
->offset_root
= offsetRoot
;
1818 fld
->offset_parent
= offsetParent
;
1820 fld
->fixed_root
= (offsetRoot
==-1) ? 0 : 1;
1821 fld
->fixed_parent
= (offsetParent
==-1) ? 0 : 1;
1823 fld
->field_size
= size
;
1829 /*****************************************************************************
1831 * getIntNumber : get an integer number
1833 * size : the size of the integer
1834 * evD : the event data
1836 * gint64 : a 64 bits integer
1837 ****************************************************************************/
1839 gint64
getIntNumber(int size
, void *evD
)
1844 case 1: i
= *(gint8
*)evD
; break;
1845 case 2: i
= *(gint16
*)evD
; break;
1846 case 4: i
= *(gint32
*)evD
; break;
1847 case 8: i
= *(gint64
*)evD
; break;
1848 default: i
= *(gint64
*)evD
;
1849 g_critical("getIntNumber : integer size %d unknown", size
);
1854 if(size
== 1) i
= *(gint8
*)evD
;
1855 else if(size
== 2) i
= *(gint16
*)evD
;
1856 else if(size
== 4) i
= *(gint32
*)evD
;
1857 else if(size
== 8) i
= *(gint64
*)evD
;
1863 /*****************************************************************************
1865 * getDataEndianType : get the data type size and endian type of the local
1868 * size : size of data type
1869 * endian : endian type, little or big
1870 ****************************************************************************/
1872 void getDataEndianType(LttArchSize
* size
, LttArchEndian
* endian
)
1876 int sizeInt
=sizeof(int), sizeLong
=sizeof(long), sizePointer
=sizeof(void *);
1878 if(c
== 1) *endian
= LTT_LITTLE_ENDIAN
;
1879 else *endian
= LTT_BIG_ENDIAN
;
1881 if(sizeInt
== 2 && sizeLong
== 4 && sizePointer
== 4)
1883 else if(sizeInt
== 4 && sizeLong
== 4 && sizePointer
== 4)
1885 else if(sizeInt
== 4 && sizeLong
== 8 && sizePointer
== 8)
1887 else if(sizeInt
== 8 && sizeLong
== 8 && sizePointer
== 8)
1889 else *size
= LTT_UNKNOWN
;
1892 /* get the node name of the system */
1894 char * ltt_trace_system_description_node_name (LttSystemDescription
* s
)
1896 return s
->node_name
;
1900 /* get the domain name of the system */
1902 char * ltt_trace_system_description_domain_name (LttSystemDescription
* s
)
1904 return s
->domain_name
;
1908 /* get the description of the system */
1910 char * ltt_trace_system_description_description (LttSystemDescription
* s
)
1912 return s
->description
;
1916 /* get the start time of the trace */
1918 LttTime
ltt_trace_system_description_trace_start_time(LttSystemDescription
*s
)
1920 return s
->trace_start
;
1924 LttTracefile
*ltt_tracefile_new()
1926 return g_new(LttTracefile
, 1);
1929 void ltt_tracefile_destroy(LttTracefile
*tf
)
1934 void ltt_tracefile_copy(LttTracefile
*dest
, const LttTracefile
*src
)