1 /* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2005 Mathieu Desnoyers
4 * Complete rewrite from the original version made by XangXiu Yang.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License Version 2.1 as published by the Free Software Foundation.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
30 #include <sys/types.h>
45 #include "ltt-private.h"
46 #include <ltt/trace.h>
47 #include <ltt/facility.h>
48 #include <ltt/event.h>
50 #include <ltt/ltt-types.h>
53 /* Facility names used in this file */
55 GQuark LTT_FACILITY_NAME_HEARTBEAT
,
56 LTT_EVENT_NAME_HEARTBEAT
;
57 GQuark LTT_TRACEFILE_NAME_FACILITIES
;
64 #define __UNUSED__ __attribute__((__unused__))
66 #define g_info(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, format)
69 #define g_debug(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format)
74 /* Those macros must be called from within a function where page_size is a known
76 #define PAGE_MASK (~(page_size-1))
77 #define PAGE_ALIGN(addr) (((addr)+page_size-1)&PAGE_MASK)
79 /* set the offset of the fields belonging to the event,
80 need the information of the archecture */
81 //void set_fields_offsets(LttTracefile *tf, LttEventType *event_type);
82 //size_t get_fields_offsets(LttTracefile *tf, LttEventType *event_type, void *data);
84 /* get the size of the field type according to
85 * The facility size information. */
87 static inline void preset_field_type_size(LttTracefile
*tf
,
88 LttEventType
*event_type
,
89 off_t offset_root
, off_t offset_parent
,
90 enum field_status
*fixed_root
, enum field_status
*fixed_parent
,
94 /* map a fixed size or a block information from the file (fd) */
95 static gint
map_block(LttTracefile
* tf
, guint block_num
);
97 /* calculate nsec per cycles for current block */
99 static guint32
calc_nsecs_per_cycle(LttTracefile
* t
);
100 static guint64
cycles_2_ns(LttTracefile
*tf
, guint64 cycles
);
103 /* go to the next event */
104 static int ltt_seek_next_event(LttTracefile
*tf
);
106 void ltt_update_event_size(LttTracefile
*tf
);
109 void precompute_offsets(LttFacility
*fac
, LttEventType
*event
);
112 /* Functions to parse system.xml file (using glib xml parser) */
113 static void parser_start_element (GMarkupParseContext __UNUSED__
*context
,
114 const gchar
*element_name
,
115 const gchar
**attribute_names
,
116 const gchar
**attribute_values
,
121 LttSystemDescription
* des
= (LttSystemDescription
* )user_data
;
122 if(strcmp("system", element_name
)){
123 *error
= g_error_new(G_MARKUP_ERROR
,
125 "This is not system.xml file");
129 while(attribute_names
[i
]){
130 if(strcmp("node_name", attribute_names
[i
])==0){
131 des
->node_name
= g_strdup(attribute_values
[i
]);
132 }else if(strcmp("domainname", attribute_names
[i
])==0){
133 des
->domain_name
= g_strdup(attribute_values
[i
]);
134 }else if(strcmp("cpu", attribute_names
[i
])==0){
135 des
->nb_cpu
= atoi(attribute_values
[i
]);
136 }else if(strcmp("arch_size", attribute_names
[i
])==0){
137 if(strcmp(attribute_values
[i
],"LP32") == 0) des
->size
= LTT_LP32
;
138 else if(strcmp(attribute_values
[i
],"ILP32") == 0) des
->size
= LTT_ILP32
;
139 else if(strcmp(attribute_values
[i
],"LP64") == 0) des
->size
= LTT_LP64
;
140 else if(strcmp(attribute_values
[i
],"ILP64") == 0) des
->size
= LTT_ILP64
;
141 else if(strcmp(attribute_values
[i
],"UNKNOWN") == 0) des
->size
= LTT_UNKNOWN
;
142 }else if(strcmp("endian", attribute_names
[i
])==0){
143 if(strcmp(attribute_values
[i
],"LITTLE_ENDIAN") == 0)
144 des
->endian
= LTT_LITTLE_ENDIAN
;
145 else if(strcmp(attribute_values
[i
],"BIG_ENDIAN") == 0)
146 des
->endian
= LTT_BIG_ENDIAN
;
147 }else if(strcmp("kernel_name", attribute_names
[i
])==0){
148 des
->kernel_name
= g_strdup(attribute_values
[i
]);
149 }else if(strcmp("kernel_release", attribute_names
[i
])==0){
150 des
->kernel_release
= g_strdup(attribute_values
[i
]);
151 }else if(strcmp("kernel_version", attribute_names
[i
])==0){
152 des
->kernel_version
= g_strdup(attribute_values
[i
]);
153 }else if(strcmp("machine", attribute_names
[i
])==0){
154 des
->machine
= g_strdup(attribute_values
[i
]);
155 }else if(strcmp("processor", attribute_names
[i
])==0){
156 des
->processor
= g_strdup(attribute_values
[i
]);
157 }else if(strcmp("hardware_platform", attribute_names
[i
])==0){
158 des
->hardware_platform
= g_strdup(attribute_values
[i
]);
159 }else if(strcmp("operating_system", attribute_names
[i
])==0){
160 des
->operating_system
= g_strdup(attribute_values
[i
]);
161 }else if(strcmp("ltt_major_version", attribute_names
[i
])==0){
162 des
->ltt_major_version
= atoi(attribute_values
[i
]);
163 }else if(strcmp("ltt_minor_version", attribute_names
[i
])==0){
164 des
->ltt_minor_version
= atoi(attribute_values
[i
]);
165 }else if(strcmp("ltt_block_size", attribute_names
[i
])==0){
166 des
->ltt_block_size
= atoi(attribute_values
[i
]);
168 *error
= g_error_new(G_MARKUP_ERROR
,
170 "Not a valid attribute");
177 static void parser_characters (GMarkupParseContext __UNUSED__
*context
,
179 gsize __UNUSED__ text_len
,
181 GError __UNUSED__
**error
)
183 LttSystemDescription
* des
= (LttSystemDescription
* )user_data
;
184 des
->description
= g_strdup(text
);
189 LttFacility
*ltt_trace_get_facility_by_num(LttTrace
*t
,
192 g_assert(num
< t
->facilities_by_num
->len
);
194 return &g_array_index(t
->facilities_by_num
, LttFacility
, num
);
198 guint
ltt_trace_get_num_cpu(LttTrace
*t
)
206 * Return value : 0 success, 1 bad tracefile
208 int parse_trace_header(void *header
, LttTracefile
*tf
, LttTrace
*t
)
210 guint32
*magic_number
= (guint32
*)header
;
211 struct ltt_trace_header_any
*any
= (struct ltt_trace_header_any
*)header
;
213 if(*magic_number
== LTT_MAGIC_NUMBER
)
215 else if(*magic_number
== LTT_REV_MAGIC_NUMBER
)
217 else /* invalid magic number, bad tracefile ! */
220 /* Get float byte order : might be different from int byte order
221 * (or is set to 0 if the trace has no float (kernel trace)) */
222 tf
->float_word_order
= any
->float_word_order
;
223 tf
->has_alignment
= any
->has_alignment
;
226 t
->arch_type
= ltt_get_uint32(LTT_GET_BO(tf
),
228 t
->arch_variant
= ltt_get_uint32(LTT_GET_BO(tf
),
230 t
->arch_size
= any
->arch_size
;
231 t
->ltt_major_version
= any
->major_version
;
232 t
->ltt_minor_version
= any
->minor_version
;
233 t
->flight_recorder
= any
->flight_recorder
;
234 t
->has_heartbeat
= any
->has_heartbeat
;
238 switch(any
->major_version
) {
241 switch(any
->minor_version
) {
244 tf
->buffer_header_size
=
245 sizeof(struct ltt_block_start_header
)
246 + sizeof(struct ltt_trace_header_0_3
);
247 g_warning("Unsupported trace version : %hhu.%hhu",
248 any
->major_version
, any
->minor_version
);
254 struct ltt_trace_header_0_7
*vheader
=
255 (struct ltt_trace_header_0_7
*)header
;
256 tf
->buffer_header_size
=
257 sizeof(struct ltt_block_start_header
)
258 + sizeof(struct ltt_trace_header_0_7
);
260 t
->start_freq
= ltt_get_uint64(LTT_GET_BO(tf
),
261 &vheader
->start_freq
);
262 t
->freq_scale
= ltt_get_uint32(LTT_GET_BO(tf
),
263 &vheader
->freq_scale
);
264 t
->start_tsc
= ltt_get_uint64(LTT_GET_BO(tf
),
265 &vheader
->start_tsc
);
266 t
->start_monotonic
= ltt_get_uint64(LTT_GET_BO(tf
),
267 &vheader
->start_monotonic
);
268 t
->start_time
.tv_sec
= ltt_get_uint64(LTT_GET_BO(tf
),
269 &vheader
->start_time_sec
);
270 t
->start_time
.tv_nsec
= ltt_get_uint64(LTT_GET_BO(tf
),
271 &vheader
->start_time_usec
);
272 t
->start_time
.tv_nsec
*= 1000; /* microsec to nanosec */
274 t
->start_time_from_tsc
= ltt_time_from_uint64(
276 * (1000000000.0 / tf
->trace
->freq_scale
)
277 / (double)t
->start_freq
);
282 g_warning("Unsupported trace version : %hhu.%hhu",
283 any
->major_version
, any
->minor_version
);
289 g_warning("Unsupported trace version : %hhu.%hhu",
290 any
->major_version
, any
->minor_version
);
300 /*****************************************************************************
302 * ltt_tracefile_open : open a trace file, construct a LttTracefile
304 * t : the trace containing the tracefile
305 * fileName : path name of the trace file
306 * tf : the tracefile structure
308 * : 0 for success, -1 otherwise.
309 ****************************************************************************/
311 gint
ltt_tracefile_open(LttTrace
*t
, gchar
* fileName
, LttTracefile
*tf
)
313 struct stat lTDFStat
; /* Trace data file status */
314 struct ltt_block_start_header
*header
;
315 int page_size
= getpagesize();
318 tf
->long_name
= g_quark_from_string(fileName
);
320 tf
->fd
= open(fileName
, O_RDONLY
);
322 g_warning("Unable to open input data file %s\n", fileName
);
326 // Get the file's status
327 if(fstat(tf
->fd
, &lTDFStat
) < 0){
328 g_warning("Unable to get the status of the input data file %s\n", fileName
);
332 // Is the file large enough to contain a trace
333 if(lTDFStat
.st_size
<
334 (off_t
)(sizeof(struct ltt_block_start_header
)
335 + sizeof(struct ltt_trace_header_any
))){
336 g_print("The input data file %s does not contain a trace\n", fileName
);
340 /* Temporarily map the buffer start header to get trace information */
341 /* Multiple of pages aligned head */
342 tf
->buffer
.head
= mmap(0,
343 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)
344 + sizeof(struct ltt_trace_header_any
)), PROT_READ
,
345 MAP_PRIVATE
, tf
->fd
, 0);
346 if(tf
->buffer
.head
== MAP_FAILED
) {
347 perror("Error in allocating memory for buffer of tracefile");
350 g_assert( ( (guint
)tf
->buffer
.head
&(8-1) ) == 0); // make sure it's aligned.
352 header
= (struct ltt_block_start_header
*)tf
->buffer
.head
;
354 if(parse_trace_header(header
->trace
, tf
, NULL
)) {
355 g_warning("parse_trace_header error");
359 //store the size of the file
360 tf
->file_size
= lTDFStat
.st_size
;
361 tf
->buf_size
= ltt_get_uint32(LTT_GET_BO(tf
), &header
->buf_size
);
362 tf
->num_blocks
= tf
->file_size
/ tf
->buf_size
;
364 if(munmap(tf
->buffer
.head
,
365 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)
366 + sizeof(struct ltt_trace_header_any
)))) {
367 g_warning("unmap size : %u\n",
368 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)
369 + sizeof(struct ltt_trace_header_any
)));
370 perror("munmap error");
373 tf
->buffer
.head
= NULL
;
375 //read the first block
376 if(map_block(tf
,0)) {
377 perror("Cannot map block for tracefile");
385 if(munmap(tf
->buffer
.head
,
386 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)
387 + sizeof(struct ltt_trace_header_any
)))) {
388 g_warning("unmap size : %u\n",
389 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)
390 + sizeof(struct ltt_trace_header_any
)));
391 perror("munmap error");
400 LttTrace
*ltt_tracefile_get_trace(LttTracefile
*tf
)
406 /*****************************************************************************
407 *Open control and per cpu tracefiles
408 ****************************************************************************/
410 void ltt_tracefile_open_cpu(LttTrace
*t
, gchar
* tracefile_name
)
413 tf
= ltt_tracefile_open(t
,tracefile_name
);
415 t
->per_cpu_tracefile_number
++;
416 g_ptr_array_add(t
->per_cpu_tracefiles
, tf
);
419 gint
ltt_tracefile_open_control(LttTrace
*t
, gchar
* control_name
)
428 tf
= ltt_tracefile_open(t
,control_name
);
430 g_warning("ltt_tracefile_open_control : bad file descriptor");
433 t
->control_tracefile_number
++;
434 g_ptr_array_add(t
->control_tracefiles
,tf
);
436 //parse facilities tracefile to get base_id
437 if(strcmp(&control_name
[strlen(control_name
)-10],"facilities") ==0){
439 if(!ltt_tracefile_read(tf
,&ev
)) return 0; // end of file
441 if(ev
.event_id
== TRACE_FACILITY_LOAD
){
443 fLoad
.name
= (gchar
*)pos
;
444 fLoad
.checksum
= *(LttChecksum
*)(pos
+ strlen(fLoad
.name
));
445 fLoad
.base_code
= *(guint32
*)(pos
+ strlen(fLoad
.name
) + sizeof(LttChecksum
));
447 for(i
=0;i
<t
->facility_number
;i
++){
448 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
,i
);
449 if(strcmp(f
->name
,fLoad
.name
)==0 && fLoad
.checksum
==f
->checksum
){
450 f
->base_id
= fLoad
.base_code
;
454 if(i
==t
->facility_number
) {
455 g_warning("Facility: %s, checksum: %u is not found",
456 fLoad
.name
,(unsigned int)fLoad
.checksum
);
459 }else if(ev
.event_id
== TRACE_BLOCK_START
){
461 }else if(ev
.event_id
== TRACE_BLOCK_END
){
464 g_warning("Not valid facilities trace file");
473 /*****************************************************************************
475 * ltt_tracefile_close: close a trace file,
477 * t : tracefile which will be closed
478 ****************************************************************************/
480 void ltt_tracefile_close(LttTracefile
*t
)
482 int page_size
= getpagesize();
484 if(t
->buffer
.head
!= NULL
)
485 if(munmap(t
->buffer
.head
, PAGE_ALIGN(t
->buf_size
))) {
486 g_warning("unmap size : %u\n",
487 PAGE_ALIGN(t
->buf_size
));
488 perror("munmap error");
496 /*****************************************************************************
497 *Get system information
498 ****************************************************************************/
500 gint
getSystemInfo(LttSystemDescription
* des
, gchar
* pathname
)
507 GMarkupParseContext
* context
;
508 GError
* error
= NULL
;
509 GMarkupParser markup_parser
=
511 parser_start_element
,
514 NULL
, /* passthrough */
518 fd
= g_open(pathname
, O_RDONLY
, 0);
520 g_warning("Can not open file : %s\n", pathname
);
524 iochan
= g_io_channel_unix_new(fd
);
526 context
= g_markup_parse_context_new(&markup_parser
, 0, des
,NULL
);
528 //while(fgets(buf,DIR_NAME_SIZE, fp) != NULL){
529 while(g_io_channel_read_line(iochan
, &buf
, &length
, NULL
, &error
)
530 != G_IO_STATUS_EOF
) {
533 g_warning("Can not read xml file: \n%s\n", error
->message
);
536 if(!g_markup_parse_context_parse(context
, buf
, length
, &error
)){
538 g_warning("Can not parse xml file: \n%s\n", error
->message
);
541 g_markup_parse_context_free(context
);
543 g_io_channel_shutdown(iochan
, FALSE
, &error
); /* No flush */
545 g_warning("Can not close file: \n%s\n", error
->message
);
553 g_markup_parse_context_free(context
);
555 g_io_channel_shutdown(iochan
, FALSE
, &error
); /* No flush */
557 g_warning("Can not close file: \n%s\n", error
->message
);
568 /*****************************************************************************
569 *The following functions get facility/tracefile information
570 ****************************************************************************/
572 gint
getFacilityInfo(LttTrace
*t
, gchar
* eventdefs
)
579 gchar fullname
[DIR_NAME_SIZE
];
580 GError
* error
= NULL
;
582 dir
= g_dir_open(eventdefs
, 0, &error
);
585 g_warning("Can not open directory: %s, %s\n", eventdefs
, error
->message
);
590 while((name
= g_dir_read_name(dir
)) != NULL
){
591 if(!g_pattern_match_simple("*.xml", name
)) continue;
592 strcpy(fullname
,eventdefs
);
593 strcat(fullname
,name
);
594 ltt_facility_open(t
,fullname
);
598 for(j
=0;j
<t
->facility_number
;j
++){
599 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, j
);
600 for(i
=0; i
<f
->event_number
; i
++){
602 setFieldsOffset(NULL
, et
, NULL
, t
);
609 /*****************************************************************************
610 *A trace is specified as a pathname to the directory containing all the
611 *associated data (control tracefiles, per cpu tracefiles, event
614 *When a trace is closed, all the associated facilities, types and fields
615 *are released as well.
619 /****************************************************************************
620 * get_absolute_pathname
622 * return the unique pathname in the system
624 * MD : Fixed this function so it uses realpath, dealing well with
625 * forgotten cases (.. were not used correctly before).
627 ****************************************************************************/
628 void get_absolute_pathname(const gchar
*pathname
, gchar
* abs_pathname
)
630 abs_pathname
[0] = '\0';
632 if ( realpath (pathname
, abs_pathname
) != NULL
)
636 /* error, return the original path unmodified */
637 strcpy(abs_pathname
, pathname
);
643 /* Search for something like : .*_.*
645 * The left side is the name, the right side is the number.
648 int get_tracefile_name_number(gchar
*raw_name
,
655 guint raw_name_len
= strlen(raw_name
);
656 gchar char_name
[PATH_MAX
];
663 for(i
=raw_name_len
-1;i
>=0;i
--) {
664 if(raw_name
[i
] == '_') break;
666 if(i
==-1) { /* Either not found or name length is 0 */
667 /* This is a userspace tracefile */
668 strncpy(char_name
, raw_name
, raw_name_len
);
669 char_name
[raw_name_len
] = '\0';
670 *name
= g_quark_from_string(char_name
);
671 *num
= 0; /* unknown cpu */
672 for(i
=0;i
<raw_name_len
;i
++) {
673 if(raw_name
[i
] == '/') {
678 for(;i
<raw_name_len
;i
++) {
679 if(raw_name
[i
] == '/') {
684 for(;i
<raw_name_len
;i
++) {
685 if(raw_name
[i
] == '-') {
689 if(i
== raw_name_len
) return -1;
691 tmpptr
= &raw_name
[i
];
692 for(;i
<raw_name_len
;i
++) {
693 if(raw_name
[i
] == '.') {
698 *tid
= strtoul(tmpptr
, &endptr
, 10);
700 return -1; /* No digit */
701 if(*tid
== ULONG_MAX
)
702 return -1; /* underflow / overflow */
704 tmpptr
= &raw_name
[i
];
705 for(;i
<raw_name_len
;i
++) {
706 if(raw_name
[i
] == '.') {
711 *pgid
= strtoul(tmpptr
, &endptr
, 10);
713 return -1; /* No digit */
714 if(*pgid
== ULONG_MAX
)
715 return -1; /* underflow / overflow */
717 tmpptr
= &raw_name
[i
];
718 *creation
= strtoull(tmpptr
, &endptr
, 10);
720 return -1; /* No digit */
721 if(*creation
== G_MAXUINT64
)
722 return -1; /* underflow / overflow */
726 cpu_num
= strtol(raw_name
+underscore_pos
+1, &endptr
, 10);
728 if(endptr
== raw_name
+underscore_pos
+1)
729 return -1; /* No digit */
730 if(cpu_num
== LONG_MIN
|| cpu_num
== LONG_MAX
)
731 return -1; /* underflow / overflow */
733 strncpy(char_name
, raw_name
, underscore_pos
);
734 char_name
[underscore_pos
] = '\0';
736 *name
= g_quark_from_string(char_name
);
745 GData
**ltt_trace_get_tracefiles_groups(LttTrace
*trace
)
747 return &trace
->tracefiles
;
751 void compute_tracefile_group(GQuark key_id
,
753 struct compute_tracefile_group_args
*args
)
758 for(i
=0; i
<group
->len
; i
++) {
759 tf
= &g_array_index (group
, LttTracefile
, i
);
761 args
->func(tf
, args
->func_args
);
766 void ltt_tracefile_group_destroy(gpointer data
)
768 GArray
*group
= (GArray
*)data
;
772 for(i
=0; i
<group
->len
; i
++) {
773 tf
= &g_array_index (group
, LttTracefile
, i
);
775 ltt_tracefile_close(tf
);
777 g_array_free(group
, TRUE
);
780 gboolean
ltt_tracefile_group_has_cpu_online(gpointer data
)
782 GArray
*group
= (GArray
*)data
;
786 for(i
=0; i
<group
->len
; i
++) {
787 tf
= &g_array_index (group
, LttTracefile
, i
);
788 if(tf
->cpu_online
) return 1;
794 /* Open each tracefile under a specific directory. Put them in a
795 * GData : permits to access them using their tracefile group pathname.
796 * i.e. access control/modules tracefile group by index :
799 * relative path is the path relative to the trace root
800 * root path is the full path
802 * A tracefile group is simply an array where all the per cpu tracefiles sits.
805 static int open_tracefiles(LttTrace
*trace
, gchar
*root_path
,
806 gchar
*relative_path
)
808 DIR *dir
= opendir(root_path
);
809 struct dirent
*entry
;
810 struct stat stat_buf
;
813 gchar path
[PATH_MAX
];
818 gchar rel_path
[PATH_MAX
];
827 strncpy(path
, root_path
, PATH_MAX
-1);
828 path_len
= strlen(path
);
829 path
[path_len
] = '/';
831 path_ptr
= path
+ path_len
;
833 strncpy(rel_path
, relative_path
, PATH_MAX
-1);
834 rel_path_len
= strlen(rel_path
);
835 rel_path
[rel_path_len
] = '/';
837 rel_path_ptr
= rel_path
+ rel_path_len
;
839 while((entry
= readdir(dir
)) != NULL
) {
841 if(entry
->d_name
[0] == '.') continue;
843 strncpy(path_ptr
, entry
->d_name
, PATH_MAX
- path_len
);
844 strncpy(rel_path_ptr
, entry
->d_name
, PATH_MAX
- rel_path_len
);
846 ret
= stat(path
, &stat_buf
);
852 g_debug("Tracefile file or directory : %s\n", path
);
854 if(strcmp(rel_path
, "/eventdefs") == 0) continue;
856 if(S_ISDIR(stat_buf
.st_mode
)) {
858 g_debug("Entering subdirectory...\n");
859 ret
= open_tracefiles(trace
, path
, rel_path
);
860 if(ret
< 0) continue;
861 } else if(S_ISREG(stat_buf
.st_mode
)) {
863 guint num
, tid
, pgid
;
866 num
= tid
= pgid
= 0;
868 if(get_tracefile_name_number(rel_path
, &name
, &num
, &tid
, &pgid
, &creation
))
869 continue; /* invalid name */
871 g_debug("Opening file.\n");
872 if(ltt_tracefile_open(trace
, path
, &tmp_tf
)) {
873 g_info("Error opening tracefile %s", path
);
875 continue; /* error opening the tracefile : bad magic number ? */
878 g_debug("Tracefile name is %s and number is %u",
879 g_quark_to_string(name
), num
);
881 tmp_tf
.cpu_online
= 1;
882 tmp_tf
.cpu_num
= num
;
886 tmp_tf
.creation
= creation
;
888 group
= g_datalist_id_get_data(&trace
->tracefiles
, name
);
890 /* Elements are automatically cleared when the array is allocated.
891 * It makes the cpu_online variable set to 0 : cpu offline, by default.
893 group
= g_array_sized_new (FALSE
, TRUE
, sizeof(LttTracefile
), 10);
894 g_datalist_id_set_data_full(&trace
->tracefiles
, name
,
895 group
, ltt_tracefile_group_destroy
);
898 /* Add the per cpu tracefile to the named group */
899 unsigned int old_len
= group
->len
;
901 group
= g_array_set_size(group
, num
+1);
902 g_array_index (group
, LttTracefile
, num
) = tmp_tf
;
912 /* ltt_get_facility_description
914 * Opens the file corresponding to the requested facility (identified by fac_id
917 * The name searched is : %trace root%/eventdefs/facname_checksum.xml
919 * Returns 0 on success, or 1 on failure.
922 static int ltt_get_facility_description(LttFacility
*f
,
924 LttTracefile
*fac_tf
)
926 char desc_file_name
[PATH_MAX
];
933 text
= g_quark_to_string(t
->pathname
);
934 textlen
= strlen(text
);
936 if(textlen
>= PATH_MAX
) goto name_error
;
937 strcpy(desc_file_name
, text
);
939 text
= "/eventdefs/";
940 textlen
+=strlen(text
);
941 if(textlen
>= PATH_MAX
) goto name_error
;
942 strcat(desc_file_name
, text
);
944 text
= g_quark_to_string(f
->name
);
945 fac_name_len
= strlen(text
);
946 textlen
+=fac_name_len
;
947 if(textlen
>= PATH_MAX
) goto name_error
;
948 strcat(desc_file_name
, text
);
950 /* arch specific facilities are named like this : name_arch */
951 if(fac_name_len
+1 < sizeof("_arch"))
954 if(!strcmp(&text
[fac_name_len
+1-sizeof("_arch")], "_arch"))
962 textlen
+=strlen(text
);
963 if(textlen
>= PATH_MAX
) goto name_error
;
964 strcat(desc_file_name
, text
);
966 err
= snprintf(desc_file_name
+textlen
, PATH_MAX
-textlen
-1,
968 if(err
< 0) goto name_error
;
970 textlen
=strlen(desc_file_name
);
975 switch(t
->arch_type
) {
976 case LTT_ARCH_TYPE_I386
:
979 case LTT_ARCH_TYPE_PPC
:
982 case LTT_ARCH_TYPE_SH
:
985 case LTT_ARCH_TYPE_S390
:
988 case LTT_ARCH_TYPE_MIPS
:
991 case LTT_ARCH_TYPE_ARM
:
994 case LTT_ARCH_TYPE_PPC64
:
997 case LTT_ARCH_TYPE_X86_64
:
1000 case LTT_ARCH_TYPE_C2
:
1003 case LTT_ARCH_TYPE_POWERPC
:
1007 g_error("Trace from unsupported architecture.");
1009 textlen
+=strlen(text
);
1010 if(textlen
>= PATH_MAX
) goto name_error
;
1011 strcat(desc_file_name
, text
);
1015 textlen
+=strlen(text
);
1016 if(textlen
>= PATH_MAX
) goto name_error
;
1017 strcat(desc_file_name
, text
);
1019 err
= ltt_facility_open(f
, t
, desc_file_name
);
1020 if(err
) goto facility_error
;
1029 static void ltt_fac_ids_destroy(gpointer data
)
1031 GArray
*fac_ids
= (GArray
*)data
;
1033 g_array_free(fac_ids
, TRUE
);
1037 /* Presumes the tracefile is already seeked at the beginning. It makes sense,
1038 * because it must be done just after the opening */
1039 static int ltt_process_facility_tracefile(LttTracefile
*tf
)
1048 err
= ltt_tracefile_read_seek(tf
);
1049 if(err
== EPERM
) goto seek_error
;
1050 else if(err
== ERANGE
) break; /* End of tracefile */
1052 err
= ltt_tracefile_read_update_event(tf
);
1053 if(err
) goto update_error
;
1055 /* We are on a facility load/or facility unload/ or heartbeat event */
1057 * * facility 0 is hardcoded : this is the core facility. It will be shown
1058 * in the facility array though, and is shown as "loaded builtin" in the
1060 * It contains event :
1062 * 1 : facility unload
1063 * 2 : state dump facility load
1066 if(tf
->event
.facility_id
!= LTT_FACILITY_CORE
) {
1067 /* Should only contain core facility */
1068 g_warning("Error in processing facility file %s, "
1069 "should not contain facility id %u.", g_quark_to_string(tf
->name
),
1070 tf
->event
.facility_id
);
1075 struct LttFacilityLoad
*fac_load_data
;
1076 struct LttStateDumpFacilityLoad
*fac_state_dump_load_data
;
1081 switch((enum ltt_core_events
)tf
->event
.event_id
) {
1082 case LTT_EVENT_FACILITY_LOAD
:
1083 fac_name
= (char*)(tf
->event
.data
);
1084 g_debug("Doing LTT_EVENT_FACILITY_LOAD of facility %s",
1086 pos
= (tf
->event
.data
+ strlen(fac_name
) + 1);
1087 pos
+= ltt_align((size_t)pos
, tf
->trace
->arch_size
, tf
->has_alignment
);
1088 fac_load_data
= (struct LttFacilityLoad
*)pos
;
1090 fac
= &g_array_index (tf
->trace
->facilities_by_num
, LttFacility
,
1091 ltt_get_uint32(LTT_GET_BO(tf
), &fac_load_data
->id
));
1092 /* facility may already exist if trace is paused/unpaused */
1093 if(fac
->exists
) continue;
1094 fac
->name
= g_quark_from_string(fac_name
);
1095 fac
->checksum
= ltt_get_uint32(LTT_GET_BO(tf
),
1096 &fac_load_data
->checksum
);
1097 fac
->id
= ltt_get_uint32(LTT_GET_BO(tf
), &fac_load_data
->id
);
1098 fac
->pointer_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1099 &fac_load_data
->pointer_size
);
1100 fac
->int_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1101 &fac_load_data
->int_size
);
1102 fac
->long_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1103 &fac_load_data
->long_size
);
1104 fac
->size_t_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1105 &fac_load_data
->size_t_size
);
1106 fac
->alignment
= ltt_get_uint32(LTT_GET_BO(tf
),
1107 &fac_load_data
->has_alignment
);
1109 if(ltt_get_facility_description(fac
, tf
->trace
, tf
))
1110 continue; /* error opening description */
1112 fac
->trace
= tf
->trace
;
1114 /* Preset the field offsets */
1115 for(i
=0; i
<fac
->events
->len
; i
++){
1116 et
= &g_array_index(fac
->events
, LttEventType
, i
);
1117 precompute_offsets(fac
, et
);
1122 fac_ids
= g_datalist_id_get_data(&tf
->trace
->facilities_by_name
,
1124 if(fac_ids
== NULL
) {
1125 fac_ids
= g_array_sized_new (FALSE
, TRUE
, sizeof(guint
), 1);
1126 g_datalist_id_set_data_full(&tf
->trace
->facilities_by_name
,
1128 fac_ids
, ltt_fac_ids_destroy
);
1130 g_array_append_val(fac_ids
, fac
->id
);
1133 case LTT_EVENT_FACILITY_UNLOAD
:
1134 g_debug("Doing LTT_EVENT_FACILITY_UNLOAD");
1135 /* We don't care about unload : facilities ID are valid for the whole
1136 * trace. They simply won't be used after the unload. */
1138 case LTT_EVENT_STATE_DUMP_FACILITY_LOAD
:
1139 fac_name
= (char*)(tf
->event
.data
);
1140 g_debug("Doing LTT_EVENT_STATE_DUMP_FACILITY_LOAD of facility %s",
1142 pos
= (tf
->event
.data
+ strlen(fac_name
) + 1);
1143 pos
+= ltt_align((size_t)pos
, tf
->trace
->arch_size
, tf
->has_alignment
);
1144 fac_state_dump_load_data
= (struct LttStateDumpFacilityLoad
*)pos
;
1146 fac
= &g_array_index (tf
->trace
->facilities_by_num
, LttFacility
,
1147 ltt_get_uint32(LTT_GET_BO(tf
), &fac_state_dump_load_data
->id
));
1148 /* facility may already exist if trace is paused/unpaused */
1149 if(fac
->exists
) continue;
1150 fac
->name
= g_quark_from_string(fac_name
);
1151 fac
->checksum
= ltt_get_uint32(LTT_GET_BO(tf
),
1152 &fac_state_dump_load_data
->checksum
);
1153 fac
->id
= ltt_get_uint32(LTT_GET_BO(tf
),
1154 &fac_state_dump_load_data
->id
);
1155 fac
->pointer_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1156 &fac_state_dump_load_data
->pointer_size
);
1157 fac
->int_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1158 &fac_state_dump_load_data
->int_size
);
1159 fac
->long_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1160 &fac_state_dump_load_data
->long_size
);
1161 fac
->size_t_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1162 &fac_state_dump_load_data
->size_t_size
);
1163 fac
->alignment
= ltt_get_uint32(LTT_GET_BO(tf
),
1164 &fac_state_dump_load_data
->has_alignment
);
1165 if(ltt_get_facility_description(fac
, tf
->trace
, tf
))
1166 continue; /* error opening description */
1168 fac
->trace
= tf
->trace
;
1170 /* Preset the field offsets */
1171 for(i
=0; i
<fac
->events
->len
; i
++){
1172 et
= &g_array_index(fac
->events
, LttEventType
, i
);
1173 precompute_offsets(fac
, et
);
1178 fac_ids
= g_datalist_id_get_data(&tf
->trace
->facilities_by_name
,
1180 if(fac_ids
== NULL
) {
1181 fac_ids
= g_array_sized_new (FALSE
, TRUE
, sizeof(guint
), 1);
1182 g_datalist_id_set_data_full(&tf
->trace
->facilities_by_name
,
1184 fac_ids
, ltt_fac_ids_destroy
);
1186 g_array_append_val(fac_ids
, fac
->id
);
1189 case LTT_EVENT_HEARTBEAT
:
1192 g_warning("Error in processing facility file %s, "
1193 "unknown event id %hhu in core facility.",
1194 g_quark_to_string(tf
->name
),
1195 tf
->event
.event_id
);
1197 goto event_id_error
;
1203 /* Error handling */
1208 g_warning("An error occured in facility tracefile parsing");
1213 LttTrace
*ltt_trace_open(const gchar
*pathname
)
1215 gchar abs_path
[PATH_MAX
];
1220 struct ltt_block_start_header
*header
;
1222 struct dirent
*entry
;
1223 guint control_found
= 0;
1224 guint eventdefs_found
= 0;
1225 struct stat stat_buf
;
1226 gchar path
[PATH_MAX
];
1228 t
= g_new(LttTrace
, 1);
1229 if(!t
) goto alloc_error
;
1231 get_absolute_pathname(pathname
, abs_path
);
1232 t
->pathname
= g_quark_from_string(abs_path
);
1234 g_datalist_init(&t
->tracefiles
);
1236 /* Test to see if it looks like a trace */
1237 dir
= opendir(abs_path
);
1242 while((entry
= readdir(dir
)) != NULL
) {
1243 strcpy(path
, abs_path
);
1245 strcat(path
, entry
->d_name
);
1246 ret
= stat(path
, &stat_buf
);
1251 if(S_ISDIR(stat_buf
.st_mode
)) {
1252 if(strcmp(entry
->d_name
, "control") == 0) {
1255 if(strcmp(entry
->d_name
, "eventdefs") == 0) {
1256 eventdefs_found
= 1;
1262 if(!control_found
|| !eventdefs_found
) goto find_error
;
1264 /* Open all the tracefiles */
1265 if(open_tracefiles(t
, abs_path
, "")) {
1266 g_warning("Error opening tracefile %s", abs_path
);
1270 /* Prepare the facilities containers : array and mapping */
1271 /* Array is zeroed : the "exists" field is set to false by default */
1272 t
->facilities_by_num
= g_array_sized_new (FALSE
,
1273 TRUE
, sizeof(LttFacility
),
1275 t
->facilities_by_num
= g_array_set_size(t
->facilities_by_num
, NUM_FACILITIES
);
1277 g_datalist_init(&t
->facilities_by_name
);
1279 /* Parse each trace control/facilitiesN files : get runtime fac. info */
1280 group
= g_datalist_id_get_data(&t
->tracefiles
, LTT_TRACEFILE_NAME_FACILITIES
);
1282 g_error("Trace %s has no facility tracefile", abs_path
);
1284 goto facilities_error
;
1287 /* Get the trace information for the control/facility 0 tracefile */
1288 g_assert(group
->len
> 0);
1289 tf
= &g_array_index (group
, LttTracefile
, 0);
1290 header
= (struct ltt_block_start_header
*)tf
->buffer
.head
;
1291 g_assert(parse_trace_header(header
->trace
,
1294 t
->num_cpu
= group
->len
;
1296 for(i
=0; i
<group
->len
; i
++) {
1297 tf
= &g_array_index (group
, LttTracefile
, i
);
1298 if(ltt_process_facility_tracefile(tf
))
1299 goto facilities_error
;
1304 /* Error handling */
1306 g_datalist_clear(&t
->facilities_by_name
);
1307 g_array_free(t
->facilities_by_num
, TRUE
);
1309 g_datalist_clear(&t
->tracefiles
);
1317 GQuark
ltt_trace_name(const LttTrace
*t
)
1323 /******************************************************************************
1324 * When we copy a trace, we want all the opening actions to happen again :
1325 * the trace will be reopened and totally independant from the original.
1326 * That's why we call ltt_trace_open.
1327 *****************************************************************************/
1328 LttTrace
*ltt_trace_copy(LttTrace
*self
)
1330 return ltt_trace_open(g_quark_to_string(self
->pathname
));
1333 void ltt_trace_close(LttTrace
*t
)
1338 for(i
=0; i
<t
->facilities_by_num
->len
; i
++) {
1339 fac
= &g_array_index (t
->facilities_by_num
, LttFacility
, i
);
1341 ltt_facility_close(fac
);
1344 g_datalist_clear(&t
->facilities_by_name
);
1345 g_array_free(t
->facilities_by_num
, TRUE
);
1346 g_datalist_clear(&t
->tracefiles
);
1351 /*****************************************************************************
1352 *Get the system description of the trace
1353 ****************************************************************************/
1355 LttFacility
*ltt_trace_facility_by_id(LttTrace
*t
, guint8 id
)
1357 g_assert(id
< t
->facilities_by_num
->len
);
1358 return &g_array_index(t
->facilities_by_num
, LttFacility
, id
);
1361 /* ltt_trace_facility_get_by_name
1363 * Returns the GArray of facility indexes. All the fac_ids that matches the
1364 * requested facility name.
1366 * If name is not found, returns NULL.
1368 GArray
*ltt_trace_facility_get_by_name(LttTrace
*t
, GQuark name
)
1370 return g_datalist_id_get_data(&t
->facilities_by_name
, name
);
1373 /*****************************************************************************
1374 * Functions to discover all the event types in the trace
1375 ****************************************************************************/
1378 unsigned ltt_trace_eventtype_number(LttTrace
*t
)
1382 unsigned int num
= t
->facility_number
;
1386 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, i
);
1387 count
+= f
->event_number
;
1394 //use an iteration on all the trace facilities, and inside iteration on all the
1395 //event types in each facilities instead.
1396 LttEventType
*ltt_trace_eventtype_get(LttTrace
*t
, unsigned evId
)
1398 LttEventType
*event_type
;
1401 f
= ltt_trace_facility_by_id(t
,evId
);
1403 if(unlikely(!f
)) event_type
= NULL
;
1404 else event_type
= f
->events
[evId
- f
->base_id
];
1411 /*****************************************************************************
1412 * ltt_trace_find_tracefile
1414 * Find a tracefile by name and index in the group.
1416 * Returns a pointer to the tracefiles, else NULL.
1417 ****************************************************************************/
1419 LttTracefile
*ltt_trace_find_tracefile(LttTrace
*t
, const gchar
*name
)
1424 /*****************************************************************************
1425 * Get the start time and end time of the trace
1426 ****************************************************************************/
1428 static void ltt_tracefile_time_span_get(LttTracefile
*tf
,
1429 LttTime
*start
, LttTime
*end
)
1433 err
= map_block(tf
, 0);
1435 g_error("Can not map block");
1436 *start
= ltt_time_infinite
;
1438 *start
= tf
->buffer
.begin
.timestamp
;
1440 err
= map_block(tf
, tf
->num_blocks
- 1); /* Last block */
1442 g_error("Can not map block");
1443 *end
= ltt_time_zero
;
1445 *end
= tf
->buffer
.end
.timestamp
;
1448 struct tracefile_time_span_get_args
{
1454 static void group_time_span_get(GQuark name
, gpointer data
, gpointer user_data
)
1456 struct tracefile_time_span_get_args
*args
=
1457 (struct tracefile_time_span_get_args
*)user_data
;
1459 GArray
*group
= (GArray
*)data
;
1465 for(i
=0; i
<group
->len
; i
++) {
1466 tf
= &g_array_index (group
, LttTracefile
, i
);
1467 if(tf
->cpu_online
) {
1468 ltt_tracefile_time_span_get(tf
, &tmp_start
, &tmp_end
);
1469 if(ltt_time_compare(*args
->start
, tmp_start
)>0) *args
->start
= tmp_start
;
1470 if(ltt_time_compare(*args
->end
, tmp_end
)<0) *args
->end
= tmp_end
;
1475 void ltt_trace_time_span_get(LttTrace
*t
, LttTime
*start
, LttTime
*end
)
1477 LttTime min_start
= ltt_time_infinite
;
1478 LttTime max_end
= ltt_time_zero
;
1479 struct tracefile_time_span_get_args args
= { t
, &min_start
, &max_end
};
1481 g_datalist_foreach(&t
->tracefiles
, &group_time_span_get
, &args
);
1483 if(start
!= NULL
) *start
= min_start
;
1484 if(end
!= NULL
) *end
= max_end
;
1489 /*****************************************************************************
1490 *Get the name of a tracefile
1491 ****************************************************************************/
1493 GQuark
ltt_tracefile_name(const LttTracefile
*tf
)
1498 GQuark
ltt_tracefile_long_name(const LttTracefile
*tf
)
1500 return tf
->long_name
;
1505 guint
ltt_tracefile_cpu(LttTracefile
*tf
)
1510 guint
ltt_tracefile_tid(LttTracefile
*tf
)
1515 guint
ltt_tracefile_pgid(LttTracefile
*tf
)
1520 guint64
ltt_tracefile_creation(LttTracefile
*tf
)
1522 return tf
->creation
;
1524 /*****************************************************************************
1525 * Get the number of blocks in the tracefile
1526 ****************************************************************************/
1528 guint
ltt_tracefile_block_number(LttTracefile
*tf
)
1530 return tf
->num_blocks
;
1534 /* Seek to the first event in a tracefile that has a time equal or greater than
1535 * the time passed in parameter.
1537 * If the time parameter is outside the tracefile time span, seek to the first
1538 * event or if after, return ERANGE.
1540 * If the time parameter is before the first event, we have to seek specially to
1543 * If the time is after the end of the trace, return ERANGE.
1545 * Do a binary search to find the right block, then a sequential search in the
1546 * block to find the event.
1548 * In the special case where the time requested fits inside a block that has no
1549 * event corresponding to the requested time, the first event of the next block
1552 * IMPORTANT NOTE : // FIXME everywhere...
1554 * You MUST NOT do a ltt_tracefile_read right after a ltt_tracefile_seek_time :
1555 * you will jump over an event if you do.
1557 * Return value : 0 : no error, the tf->event can be used
1558 * ERANGE : time if after the last event of the trace
1559 * otherwise : this is an error.
1563 int ltt_tracefile_seek_time(LttTracefile
*tf
, LttTime time
)
1567 unsigned int block_num
, high
, low
;
1569 /* seek at the beginning of trace */
1570 err
= map_block(tf
, 0); /* First block */
1572 g_error("Can not map block");
1576 /* If the time is lower or equal the beginning of the trace,
1577 * go to the first event. */
1578 if(ltt_time_compare(time
, tf
->buffer
.begin
.timestamp
) <= 0) {
1579 ret
= ltt_tracefile_read(tf
);
1580 if(ret
== ERANGE
) goto range
;
1581 else if (ret
) goto fail
;
1582 goto found
; /* There is either no event in the trace or the event points
1583 to the first event in the trace */
1586 err
= map_block(tf
, tf
->num_blocks
- 1); /* Last block */
1588 g_error("Can not map block");
1592 /* If the time is after the end of the trace, return ERANGE. */
1593 if(ltt_time_compare(time
, tf
->buffer
.end
.timestamp
) > 0) {
1597 /* Binary search the block */
1598 high
= tf
->num_blocks
- 1;
1602 block_num
= ((high
-low
) / 2) + low
;
1604 err
= map_block(tf
, block_num
);
1606 g_error("Can not map block");
1610 /* We cannot divide anymore : this is what would happen if the time
1611 * requested was exactly between two consecutive buffers'end and start
1612 * timestamps. This is also what would happend if we didn't deal with out
1613 * of span cases prior in this function. */
1614 /* The event is right in the buffer!
1615 * (or in the next buffer first event) */
1617 ret
= ltt_tracefile_read(tf
);
1618 if(ret
== ERANGE
) goto range
; /* ERANGE or EPERM */
1619 else if(ret
) goto fail
;
1621 if(ltt_time_compare(time
, tf
->event
.event_time
) <= 0)
1625 } else if(ltt_time_compare(time
, tf
->buffer
.begin
.timestamp
) < 0) {
1626 /* go to lower part */
1627 high
= block_num
- 1;
1628 } else if(ltt_time_compare(time
, tf
->buffer
.end
.timestamp
) > 0) {
1629 /* go to higher part */
1630 low
= block_num
+ 1;
1631 } else {/* The event is right in the buffer!
1632 (or in the next buffer first event) */
1634 ret
= ltt_tracefile_read(tf
);
1635 if(ret
== ERANGE
) goto range
; /* ERANGE or EPERM */
1636 else if(ret
) goto fail
;
1638 if(ltt_time_compare(time
, tf
->event
.event_time
) <= 0)
1650 /* Error handling */
1652 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1653 g_quark_to_string(tf
->name
));
1658 int ltt_tracefile_seek_position(LttTracefile
*tf
, const LttEventPosition
*ep
) {
1662 if(ep
->tracefile
!= tf
) {
1666 err
= map_block(tf
, ep
->block
);
1668 g_error("Can not map block");
1672 tf
->event
.offset
= ep
->offset
;
1674 /* Put back the event real tsc */
1675 tf
->event
.tsc
= ep
->tsc
;
1676 tf
->buffer
.tsc
= ep
->tsc
;
1678 err
= ltt_tracefile_read_update_event(tf
);
1680 err
= ltt_tracefile_read_op(tf
);
1686 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1687 g_quark_to_string(tf
->name
));
1691 LttTime
ltt_interpolate_time_from_tsc(LttTracefile
*tf
, guint64 tsc
)
1695 if(tsc
> tf
->trace
->start_tsc
) {
1696 time
= ltt_time_from_uint64(
1697 (double)(tsc
- tf
->trace
->start_tsc
)
1698 * (1000000000.0 / tf
->trace
->freq_scale
)
1699 / (double)tf
->trace
->start_freq
);
1700 time
= ltt_time_add(tf
->trace
->start_time_from_tsc
, time
);
1702 time
= ltt_time_from_uint64(
1703 (double)(tf
->trace
->start_tsc
- tsc
)
1704 * (1000000000.0 / tf
->trace
->freq_scale
)
1705 / (double)tf
->trace
->start_freq
);
1706 time
= ltt_time_sub(tf
->trace
->start_time_from_tsc
, time
);
1711 /* Calculate the real event time based on the buffer boundaries */
1712 LttTime
ltt_interpolate_time(LttTracefile
*tf
, LttEvent
*event
)
1714 return ltt_interpolate_time_from_tsc(tf
, tf
->buffer
.tsc
);
1718 /* Get the current event of the tracefile : valid until the next read */
1719 LttEvent
*ltt_tracefile_get_event(LttTracefile
*tf
)
1726 /*****************************************************************************
1728 * ltt_tracefile_read : Read the next event in the tracefile
1733 * Returns 0 if an event can be used in tf->event.
1734 * Returns ERANGE on end of trace. The event in tf->event still can be used
1735 * (if the last block was not empty).
1736 * Returns EPERM on error.
1738 * This function does make the tracefile event structure point to the event
1739 * currently pointed to by the tf->event.
1741 * Note : you must call a ltt_tracefile_seek to the beginning of the trace to
1742 * reinitialize it after an error if you want results to be coherent.
1743 * It would be the case if a end of trace last buffer has no event : the end
1744 * of trace wouldn't be returned, but an error.
1745 * We make the assumption there is at least one event per buffer.
1746 ****************************************************************************/
1748 int ltt_tracefile_read(LttTracefile
*tf
)
1752 err
= ltt_tracefile_read_seek(tf
);
1754 err
= ltt_tracefile_read_update_event(tf
);
1756 err
= ltt_tracefile_read_op(tf
);
1762 int ltt_tracefile_read_seek(LttTracefile
*tf
)
1766 /* Get next buffer until we finally have an event, or end of trace */
1768 err
= ltt_seek_next_event(tf
);
1769 if(unlikely(err
== ENOPROTOOPT
)) {
1773 /* Are we at the end of the buffer ? */
1775 if(unlikely(tf
->buffer
.index
== tf
->num_blocks
-1)){ /* end of trace ? */
1778 /* get next block */
1779 err
= map_block(tf
, tf
->buffer
.index
+ 1);
1781 g_error("Can not map block");
1785 } else break; /* We found an event ! */
1792 /* do specific operation on events */
1793 int ltt_tracefile_read_op(LttTracefile
*tf
)
1799 /* do event specific operation */
1801 /* do something if its an heartbeat event : increment the heartbeat count */
1802 //if(event->facility_id == LTT_FACILITY_CORE)
1803 // if(event->event_id == LTT_EVENT_HEARTBEAT)
1804 // tf->cur_heart_beat_number++;
1810 /* same as ltt_tracefile_read, but does not seek to the next event nor call
1811 * event specific operation. */
1812 int ltt_tracefile_read_update_event(LttTracefile
*tf
)
1818 pos
= tf
->buffer
.head
+ event
->offset
;
1820 /* Read event header */
1822 /* Align the head */
1823 pos
+= ltt_align((size_t)pos
, tf
->trace
->arch_size
, tf
->has_alignment
);
1825 if(tf
->trace
->has_heartbeat
) {
1826 event
->timestamp
= ltt_get_uint32(LTT_GET_BO(tf
),
1828 /* 32 bits -> 64 bits tsc */
1829 /* note : still works for seek and non seek cases. */
1830 if(event
->timestamp
< (0xFFFFFFFFULL
&tf
->buffer
.tsc
)) {
1831 tf
->buffer
.tsc
= ((tf
->buffer
.tsc
&0xFFFFFFFF00000000ULL
)
1833 | (guint64
)event
->timestamp
;
1834 event
->tsc
= tf
->buffer
.tsc
;
1837 tf
->buffer
.tsc
= (tf
->buffer
.tsc
&0xFFFFFFFF00000000ULL
)
1838 | (guint64
)event
->timestamp
;
1839 event
->tsc
= tf
->buffer
.tsc
;
1841 pos
+= sizeof(guint32
);
1843 event
->tsc
= ltt_get_uint64(LTT_GET_BO(tf
), pos
);
1844 tf
->buffer
.tsc
= event
->tsc
;
1845 pos
+= sizeof(guint64
);
1847 event
->event_time
= ltt_interpolate_time(tf
, event
);
1848 event
->facility_id
= *(guint8
*)pos
;
1849 pos
+= sizeof(guint8
);
1851 event
->event_id
= *(guint8
*)pos
;
1852 pos
+= sizeof(guint8
);
1854 event
->event_size
= ltt_get_uint16(LTT_GET_BO(tf
), pos
);
1855 pos
+= sizeof(guint16
);
1857 /* Align the head */
1858 pos
+= ltt_align((size_t)pos
, tf
->trace
->arch_size
, tf
->has_alignment
);
1862 /* get the data size and update the event fields with the current
1864 ltt_update_event_size(tf
);
1870 /****************************************************************************
1872 * map_block : map a block from the file
1874 * lttdes : ltt trace file
1875 * whichBlock : the block which will be read
1878 * EINVAL : lseek fail
1879 * EIO : can not read from the file
1880 ****************************************************************************/
1882 static gint
map_block(LttTracefile
* tf
, guint block_num
)
1884 int page_size
= getpagesize();
1885 struct ltt_block_start_header
*header
;
1887 g_assert(block_num
< tf
->num_blocks
);
1889 if(tf
->buffer
.head
!= NULL
) {
1890 if(munmap(tf
->buffer
.head
, PAGE_ALIGN(tf
->buf_size
))) {
1891 g_warning("unmap size : %u\n",
1892 PAGE_ALIGN(tf
->buf_size
));
1893 perror("munmap error");
1899 /* Multiple of pages aligned head */
1900 tf
->buffer
.head
= mmap(0,
1901 PAGE_ALIGN(tf
->buf_size
),
1902 PROT_READ
, MAP_PRIVATE
, tf
->fd
,
1903 PAGE_ALIGN((off_t
)tf
->buf_size
* (off_t
)block_num
));
1905 if(tf
->buffer
.head
== MAP_FAILED
) {
1906 perror("Error in allocating memory for buffer of tracefile");
1910 g_assert( ( (guint
)tf
->buffer
.head
&(8-1) ) == 0); // make sure it's aligned.
1913 tf
->buffer
.index
= block_num
;
1915 header
= (struct ltt_block_start_header
*)tf
->buffer
.head
;
1918 tf
->buffer
.begin
.timestamp
= ltt_time_add(
1919 ltt_time_from_uint64(
1920 ltt_get_uint64(LTT_GET_BO(tf
),
1921 &header
->begin
.timestamp
)
1922 - tf
->trace
->start_monotonic
),
1923 tf
->trace
->start_time
);
1925 //g_debug("block %u begin : %lu.%lu", block_num,
1926 // tf->buffer.begin.timestamp.tv_sec, tf->buffer.begin.timestamp.tv_nsec);
1927 tf
->buffer
.begin
.cycle_count
= ltt_get_uint64(LTT_GET_BO(tf
),
1928 &header
->begin
.cycle_count
);
1929 tf
->buffer
.begin
.freq
= ltt_get_uint64(LTT_GET_BO(tf
),
1930 &header
->begin
.freq
);
1931 if(tf
->buffer
.begin
.freq
== 0)
1932 tf
->buffer
.begin
.freq
= tf
->trace
->start_freq
;
1934 tf
->buffer
.begin
.timestamp
= ltt_interpolate_time_from_tsc(tf
,
1935 tf
->buffer
.begin
.cycle_count
);
1938 ltt_time_from_uint64(
1939 (double)(tf
->buffer
.begin
.cycle_count
1940 - tf
->trace
->start_tsc
) * 1000000.0
1941 / (double)tf
->trace
->start_freq
),
1942 tf
->trace
->start_time_from_tsc
);
1946 tf
->buffer
.end
.timestamp
= ltt_time_add(
1947 ltt_time_from_uint64(
1948 ltt_get_uint64(LTT_GET_BO(tf
),
1949 &header
->end
.timestamp
)
1950 - tf
->trace
->start_monotonic
),
1951 tf
->trace
->start_time
);
1953 //g_debug("block %u end : %lu.%lu", block_num,
1954 // tf->buffer.end.timestamp.tv_sec, tf->buffer.end.timestamp.tv_nsec);
1955 tf
->buffer
.end
.cycle_count
= ltt_get_uint64(LTT_GET_BO(tf
),
1956 &header
->end
.cycle_count
);
1957 tf
->buffer
.end
.freq
= ltt_get_uint64(LTT_GET_BO(tf
),
1959 if(tf
->buffer
.end
.freq
== 0)
1960 tf
->buffer
.end
.freq
= tf
->trace
->start_freq
;
1962 tf
->buffer
.lost_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1963 &header
->lost_size
);
1964 tf
->buffer
.end
.timestamp
= ltt_interpolate_time_from_tsc(tf
,
1965 tf
->buffer
.end
.cycle_count
);
1968 ltt_time_from_uint64(
1969 (double)(tf
->buffer
.end
.cycle_count
1970 - tf
->trace
->start_tsc
) * 1000000.0
1971 / (double)tf
->trace
->start_freq
),
1972 tf
->trace
->start_time_from_tsc
);
1974 tf
->buffer
.tsc
= tf
->buffer
.begin
.cycle_count
;
1975 tf
->event
.tsc
= tf
->buffer
.tsc
;
1976 tf
->buffer
.freq
= tf
->buffer
.begin
.freq
;
1979 * eventually support variable buffer size : will need a partial pre-read of
1980 * the headers to create an index when we open the trace... eventually. */
1981 g_assert(tf
->buf_size
== ltt_get_uint32(LTT_GET_BO(tf
),
1982 &header
->buf_size
));
1984 /* Now that the buffer is mapped, calculate the time interpolation for the
1987 // tf->buffer.nsecs_per_cycle = calc_nsecs_per_cycle(tf);
1988 //tf->buffer.cyc2ns_scale = calc_nsecs_per_cycle(tf);
1990 /* Make the current event point to the beginning of the buffer :
1991 * it means that the event read must get the first event. */
1992 tf
->event
.tracefile
= tf
;
1993 tf
->event
.block
= block_num
;
1994 tf
->event
.offset
= 0;
2003 /* It will update the fields offsets too */
2004 void ltt_update_event_size(LttTracefile
*tf
)
2007 LttFacility
*f
= ltt_trace_get_facility_by_num(tf
->trace
,
2008 tf
->event
.facility_id
);
2011 /* Specific handling of core events : necessary to read the facility control
2014 if(likely(tf
->event
.facility_id
== LTT_FACILITY_CORE
)) {
2015 switch((enum ltt_core_events
)tf
->event
.event_id
) {
2016 case LTT_EVENT_FACILITY_LOAD
:
2017 size
= strlen((char*)tf
->event
.data
) + 1;
2018 //g_debug("Update Event facility load of facility %s", (char*)tf->event.data);
2019 size
+= ltt_align(size
, sizeof(guint32
), tf
->has_alignment
);
2020 size
+= sizeof(struct LttFacilityLoad
);
2022 case LTT_EVENT_FACILITY_UNLOAD
:
2023 //g_debug("Update Event facility unload");
2024 size
= sizeof(struct LttFacilityUnload
);
2026 case LTT_EVENT_STATE_DUMP_FACILITY_LOAD
:
2027 size
= strlen((char*)tf
->event
.data
) + 1;
2028 size
+= ltt_align(size
, sizeof(guint32
), tf
->has_alignment
);
2029 //g_debug("Update Event facility load state dump of facility %s",
2030 // (char*)tf->event.data);
2031 size
+= sizeof(struct LttStateDumpFacilityLoad
);
2033 case LTT_EVENT_HEARTBEAT
:
2034 //g_debug("Update Event heartbeat");
2035 size
= sizeof(TimeHeartbeat
);
2038 g_warning("Error in getting event size : tracefile %s, "
2039 "unknown event id %hhu in core facility.",
2040 g_quark_to_string(tf
->name
),
2041 tf
->event
.event_id
);
2042 goto event_id_error
;
2045 goto no_offset
; /* Skip the field computation */
2047 g_warning("Unknown facility %hhu (0x%hhx) in tracefile %s",
2048 tf
->event
.facility_id
,
2049 tf
->event
.facility_id
,
2050 g_quark_to_string(tf
->name
));
2051 goto facility_error
;
2055 LttEventType
*event_type
=
2056 ltt_facility_eventtype_get(f
, tf
->event
.event_id
);
2059 g_warning("Unknown event id %hhu in facility %s in tracefile %s",
2061 g_quark_to_string(f
->name
),
2062 g_quark_to_string(tf
->name
));
2063 goto event_type_error
;
2066 /* Compute the dynamic offsets */
2067 compute_offsets(tf
, f
, event_type
, &size
, tf
->event
.data
);
2069 //g_debug("Event root field : f.e %hhu.%hhu size %zd",
2070 // tf->event.facility_id,
2071 // tf->event.event_id, size);
2074 tf
->event
.data_size
= size
;
2076 /* Check consistency between kernel and LTTV structure sizes */
2077 if(tf
->event
.event_size
== 0xFFFF) {
2078 /* Event size too big to fit in the event size field */
2079 tf
->event
.event_size
= tf
->event
.data_size
;
2081 g_assert(tf
->event
.data_size
== tf
->event
.event_size
);
2088 if(tf
->event
.event_size
== 0xFFFF) {
2089 g_error("Cannot jump over an unknown event bigger than 0xFFFE bytes");
2091 /* The facility is unknown : use the kernel information about this event
2092 * to jump over it. */
2093 tf
->event
.data_size
= tf
->event
.event_size
;
2097 /* Take the tf current event offset and use the event facility id and event id
2098 * to figure out where is the next event offset.
2100 * This is an internal function not aiming at being used elsewhere : it will
2101 * not jump over the current block limits. Please consider using
2102 * ltt_tracefile_read to do this.
2104 * Returns 0 on success
2105 * ERANGE if we are at the end of the buffer.
2106 * ENOPROTOOPT if an error occured when getting the current event size.
2108 static int ltt_seek_next_event(LttTracefile
*tf
)
2113 /* seek over the buffer header if we are at the buffer start */
2114 if(tf
->event
.offset
== 0) {
2115 tf
->event
.offset
+= tf
->buffer_header_size
;
2117 if(tf
->event
.offset
== tf
->buf_size
- tf
->buffer
.lost_size
) {
2124 pos
= tf
->event
.data
;
2126 if(tf
->event
.data_size
< 0) goto error
;
2128 pos
+= (size_t)tf
->event
.data_size
;
2130 tf
->event
.offset
= pos
- tf
->buffer
.head
;
2132 if(tf
->event
.offset
== tf
->buf_size
- tf
->buffer
.lost_size
) {
2136 g_assert(tf
->event
.offset
< tf
->buf_size
- tf
->buffer
.lost_size
);
2142 g_error("Error in ltt_seek_next_event for tracefile %s",
2143 g_quark_to_string(tf
->name
));
2148 /*****************************************************************************
2150 * calc_nsecs_per_cycle : calculate nsecs per cycle for current block
2152 * 1.0 / (freq(khz) *1000) * 1000000000
2155 ****************************************************************************/
2156 /* from timer_tsc.c */
2157 #define CYC2NS_SCALE_FACTOR 10
2158 static guint32
calc_nsecs_per_cycle(LttTracefile
* tf
)
2160 //return 1e6 / (double)tf->buffer.freq;
2161 guint32 cpu_mhz
= tf
->buffer
.freq
/ 1000;
2162 guint32 cyc2ns_scale
= (1000 << CYC2NS_SCALE_FACTOR
)/cpu_mhz
;
2164 return cyc2ns_scale
;
2165 // return 1e6 / (double)tf->buffer.freq;
2168 static guint64
cycles_2_ns(LttTracefile
*tf
, guint64 cycles
)
2170 return (cycles
* tf
->buffer
.cyc2ns_scale
) >> CYC2NS_SCALE_FACTOR
;
2175 void setFieldsOffset(LttTracefile
*tf
, LttEventType
*evT
,void *evD
)
2177 LttField
* rootFld
= evT
->root_field
;
2178 // rootFld->base_address = evD;
2181 rootFld
->field_size
= getFieldtypeSize(tf
, evT
->facility
,
2182 evT
, 0,0,rootFld
, evD
);
2186 /*****************************************************************************
2188 * set_fields_offsets : set the precomputable offset of the fields
2190 * tracefile : opened trace file
2191 * event_type : the event type
2192 ****************************************************************************/
2194 void set_fields_offsets(LttTracefile
*tf
, LttEventType
*event_type
)
2196 LttField
*field
= event_type
->root_field
;
2197 enum field_status fixed_root
= FIELD_FIXED
, fixed_parent
= FIELD_FIXED
;
2200 preset_field_type_size(tf
, event_type
, 0, 0,
2201 &fixed_root
, &fixed_parent
,
2208 /*****************************************************************************
2210 * get_alignment : Get the alignment needed for a field.
2214 * returns : The size on which it must be aligned.
2216 ****************************************************************************/
2217 off_t
get_alignment(LttField
*field
)
2219 LttType
*type
= &field
->field_type
;
2221 switch(type
->type_class
) {
2223 case LTT_UINT_FIXED
:
2238 /* Align offset on type size */
2239 g_assert(field
->field_size
!= 0);
2240 return field
->field_size
;
2246 g_assert(type
->fields
->len
== 1);
2248 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
2249 return get_alignment(child
);
2253 g_assert(type
->fields
->len
== 2);
2256 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
2258 localign
= max(localign
, get_alignment(child
));
2260 child
= &g_array_index(type
->fields
, LttField
, 1);
2261 localign
= max(localign
, get_alignment(child
));
2272 for(i
=0; i
<type
->fields
->len
; i
++) {
2273 LttField
*child
= &g_array_index(type
->fields
, LttField
, i
);
2274 localign
= max(localign
, get_alignment(child
));
2281 g_error("get_alignment : unknown type");
2286 /*****************************************************************************
2288 * field_compute_static_size : Determine the size of fields known by their
2289 * sole definition. Unions, arrays and struct sizes might be known, but
2290 * the parser does not give that information.
2295 ****************************************************************************/
2297 void field_compute_static_size(LttFacility
*fac
, LttField
*field
)
2299 LttType
*type
= &field
->field_type
;
2301 switch(type
->type_class
) {
2303 case LTT_UINT_FIXED
:
2322 /* note this : array type size is the number of elements in the array,
2323 * while array field size of the length of the array in bytes */
2324 g_assert(type
->fields
->len
== 1);
2326 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
2327 field_compute_static_size(fac
, child
);
2329 if(child
->field_size
!= 0) {
2330 field
->field_size
= type
->size
* child
->field_size
;
2331 field
->dynamic_offsets
= g_array_sized_new(FALSE
, TRUE
,
2332 sizeof(off_t
), type
->size
);
2334 field
->field_size
= 0;
2339 g_assert(type
->fields
->len
== 2);
2341 off_t local_offset
= 0;
2342 LttField
*child
= &g_array_index(type
->fields
, LttField
, 1);
2343 field_compute_static_size(fac
, child
);
2344 field
->field_size
= 0;
2346 if(child
->field_size
!= 0) {
2347 field
->dynamic_offsets
= g_array_sized_new(FALSE
, TRUE
,
2348 sizeof(off_t
), SEQUENCE_AVG_ELEMENTS
);
2356 for(i
=0;i
<type
->fields
->len
;i
++) {
2357 LttField
*child
= &g_array_index(type
->fields
, LttField
, i
);
2358 field_compute_static_size(fac
, child
);
2359 if(child
->field_size
!= 0) {
2360 type
->size
+= ltt_align(type
->size
, get_alignment(child
),
2362 type
->size
+= child
->field_size
;
2364 /* As soon as we find a child with variable size, we have
2365 * a variable size */
2370 field
->field_size
= type
->size
;
2374 g_error("field_static_size : unknown type");
2381 /*****************************************************************************
2383 * precompute_fields_offsets : set the precomputable offset of the fields
2387 * offset : pointer to the current offset, must be incremented
2389 * return : 1 : found a variable length field, stop the processing.
2391 ****************************************************************************/
2394 gint
precompute_fields_offsets(LttFacility
*fac
, LttField
*field
, off_t
*offset
)
2396 LttType
*type
= &field
->field_type
;
2398 switch(type
->type_class
) {
2400 case LTT_UINT_FIXED
:
2415 g_assert(field
->field_size
!= 0);
2416 /* Align offset on type size */
2417 *offset
+= ltt_align(*offset
, get_alignment(field
),
2419 /* remember offset */
2420 field
->offset_root
= *offset
;
2421 field
->fixed_root
= FIELD_FIXED
;
2422 /* Increment offset */
2423 *offset
+= field
->field_size
;
2427 field
->offset_root
= *offset
;
2428 field
->fixed_root
= FIELD_FIXED
;
2432 g_assert(type
->fields
->len
== 1);
2434 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
2436 *offset
+= ltt_align(*offset
, get_alignment(field
),
2439 /* remember offset */
2440 field
->offset_root
= *offset
;
2441 field
->array_offset
= *offset
;
2442 field
->fixed_root
= FIELD_FIXED
;
2444 /* Let the child be variable */
2445 //precompute_fields_offsets(tf, child, offset);
2447 if(field
->field_size
!= 0) {
2448 /* Increment offset */
2449 /* field_size is the array size in bytes */
2450 *offset
+= field
->field_size
;
2458 g_assert(type
->fields
->len
== 2);
2463 *offset
+= ltt_align(*offset
, get_alignment(field
),
2466 /* remember offset */
2467 field
->offset_root
= *offset
;
2468 field
->fixed_root
= FIELD_FIXED
;
2470 child
= &g_array_index(type
->fields
, LttField
, 0);
2471 ret
= precompute_fields_offsets(fac
, child
, offset
);
2472 g_assert(ret
== 0); /* Seq len cannot have variable len */
2474 child
= &g_array_index(type
->fields
, LttField
, 1);
2475 *offset
+= ltt_align(*offset
, get_alignment(child
),
2477 field
->array_offset
= *offset
;
2478 /* Let the child be variable. */
2479 //ret = precompute_fields_offsets(fac, child, offset);
2481 /* Cannot precompute fields offsets of sequence members, and has
2482 * variable length. */
2492 *offset
+= ltt_align(*offset
, get_alignment(field
),
2494 /* remember offset */
2495 field
->offset_root
= *offset
;
2496 field
->fixed_root
= FIELD_FIXED
;
2498 for(i
=0; i
< type
->fields
->len
; i
++) {
2499 child
= &g_array_index(type
->fields
, LttField
, i
);
2500 ret
= precompute_fields_offsets(fac
, child
, offset
);
2513 *offset
+= ltt_align(*offset
, get_alignment(field
),
2515 /* remember offset */
2516 field
->offset_root
= *offset
;
2517 field
->fixed_root
= FIELD_FIXED
;
2519 for(i
=0; i
< type
->fields
->len
; i
++) {
2520 *offset
= field
->offset_root
;
2521 child
= &g_array_index(type
->fields
, LttField
, i
);
2522 ret
= precompute_fields_offsets(fac
, child
, offset
);
2526 *offset
= field
->offset_root
+ field
->field_size
;
2533 g_error("precompute_fields_offsets : unknown type");
2540 /*****************************************************************************
2542 * precompute_offsets : set the precomputable offset of an event type
2545 * event : event type
2547 ****************************************************************************/
2548 void precompute_offsets(LttFacility
*fac
, LttEventType
*event
)
2554 /* First, compute the size of fixed size fields. Will determine size for
2555 * arrays, struct and unions, which is not done by the parser */
2556 for(i
=0; i
<event
->fields
->len
; i
++) {
2557 LttField
*field
= &g_array_index(event
->fields
, LttField
, i
);
2558 field_compute_static_size(fac
, field
);
2561 /* Precompute all known offsets */
2562 for(i
=0; i
<event
->fields
->len
; i
++) {
2563 LttField
*field
= &g_array_index(event
->fields
, LttField
, i
);
2564 ret
= precompute_fields_offsets(fac
, field
, &offset
);
2572 /*****************************************************************************
2574 * preset_field_type_size : set the fixed sizes of the field type
2577 * event_type : event type
2578 * offset_root : offset from the root
2579 * offset_parent : offset from the parent
2580 * fixed_root : Do we know a fixed offset to the root ?
2581 * fixed_parent : Do we know a fixed offset to the parent ?
2583 ****************************************************************************/
2587 // preset the fixed size offsets. Calculate them just like genevent-new : an
2588 // increment of a *to value that represents the offset from the start of the
2590 // The preset information is : offsets up to (and including) the first element
2591 // of variable size. All subsequent fields must be flagged "VARIABLE OFFSET".
2593 void preset_field_type_size(LttTracefile
*tf
, LttEventType
*event_type
,
2594 off_t offset_root
, off_t offset_parent
,
2595 enum field_status
*fixed_root
, enum field_status
*fixed_parent
,
2598 enum field_status local_fixed_root
, local_fixed_parent
;
2602 g_assert(field
->fixed_root
== FIELD_UNKNOWN
);
2603 g_assert(field
->fixed_parent
== FIELD_UNKNOWN
);
2604 g_assert(field
->fixed_size
== FIELD_UNKNOWN
);
2606 type
= field
->field_type
;
2608 field
->fixed_root
= *fixed_root
;
2609 if(field
->fixed_root
== FIELD_FIXED
)
2610 field
->offset_root
= offset_root
;
2612 field
->offset_root
= 0;
2614 field
->fixed_parent
= *fixed_parent
;
2615 if(field
->fixed_parent
== FIELD_FIXED
)
2616 field
->offset_parent
= offset_parent
;
2618 field
->offset_parent
= 0;
2620 size_t current_root_offset
;
2621 size_t current_offset
;
2622 enum field_status current_child_status
, final_child_status
;
2625 switch(type
->type_class
) {
2627 case LTT_UINT_FIXED
:
2636 field
->field_size
= ltt_type_size(tf
->trace
, type
);
2637 field
->fixed_size
= FIELD_FIXED
;
2640 field
->field_size
= (off_t
)event_type
->facility
->pointer_size
;
2641 field
->fixed_size
= FIELD_FIXED
;
2645 field
->field_size
= (off_t
)event_type
->facility
->long_size
;
2646 field
->fixed_size
= FIELD_FIXED
;
2651 field
->field_size
= (off_t
)event_type
->facility
->size_t_size
;
2652 field
->fixed_size
= FIELD_FIXED
;
2655 local_fixed_root
= FIELD_VARIABLE
;
2656 local_fixed_parent
= FIELD_VARIABLE
;
2657 preset_field_type_size(tf
, event_type
,
2659 &local_fixed_root
, &local_fixed_parent
,
2661 field
->fixed_size
= FIELD_VARIABLE
;
2662 field
->field_size
= 0;
2663 *fixed_root
= FIELD_VARIABLE
;
2664 *fixed_parent
= FIELD_VARIABLE
;
2667 field
->fixed_size
= FIELD_VARIABLE
;
2668 field
->field_size
= 0;
2669 *fixed_root
= FIELD_VARIABLE
;
2670 *fixed_parent
= FIELD_VARIABLE
;
2673 local_fixed_root
= FIELD_VARIABLE
;
2674 local_fixed_parent
= FIELD_VARIABLE
;
2675 preset_field_type_size(tf
, event_type
,
2677 &local_fixed_root
, &local_fixed_parent
,
2679 field
->fixed_size
= field
->child
[0]->fixed_size
;
2680 if(field
->fixed_size
== FIELD_FIXED
) {
2681 field
->field_size
= type
->element_number
* field
->child
[0]->field_size
;
2683 field
->field_size
= 0;
2684 *fixed_root
= FIELD_VARIABLE
;
2685 *fixed_parent
= FIELD_VARIABLE
;
2689 current_root_offset
= field
->offset_root
;
2691 current_child_status
= FIELD_FIXED
;
2692 for(i
=0;i
<type
->element_number
;i
++) {
2693 preset_field_type_size(tf
, event_type
,
2694 current_root_offset
, current_offset
,
2695 fixed_root
, ¤t_child_status
,
2697 if(current_child_status
== FIELD_FIXED
) {
2698 current_root_offset
+= field
->child
[i
]->field_size
;
2699 current_offset
+= field
->child
[i
]->field_size
;
2701 current_root_offset
= 0;
2705 if(current_child_status
!= FIELD_FIXED
) {
2706 *fixed_parent
= current_child_status
;
2707 field
->field_size
= 0;
2708 field
->fixed_size
= current_child_status
;
2710 field
->field_size
= current_offset
;
2711 field
->fixed_size
= FIELD_FIXED
;
2715 current_root_offset
= field
->offset_root
;
2718 final_child_status
= FIELD_FIXED
;
2719 for(i
=0;i
<type
->element_number
;i
++) {
2720 enum field_status current_root_child_status
= FIELD_FIXED
;
2721 enum field_status current_child_status
= FIELD_FIXED
;
2722 preset_field_type_size(tf
, event_type
,
2723 current_root_offset
, current_offset
,
2724 ¤t_root_child_status
, ¤t_child_status
,
2726 if(current_child_status
!= FIELD_FIXED
)
2727 final_child_status
= current_child_status
;
2729 max_size
= max(max_size
, field
->child
[i
]->field_size
);
2731 if(final_child_status
!= FIELD_FIXED
) {
2732 g_error("LTTV does not support variable size fields in unions.");
2733 /* This will stop the application. */
2734 *fixed_root
= final_child_status
;
2735 *fixed_parent
= final_child_status
;
2736 field
->field_size
= 0;
2737 field
->fixed_size
= current_child_status
;
2739 field
->field_size
= max_size
;
2740 field
->fixed_size
= FIELD_FIXED
;
2744 g_error("unexpected type NONE");
2751 /*****************************************************************************
2753 * check_fields_compatibility : Check for compatibility between two fields :
2754 * do they use the same inner structure ?
2756 * event_type1 : event type
2757 * event_type2 : event type
2760 *Returns : 0 if identical
2762 ****************************************************************************/
2763 // this function checks for equality of field types. Therefore, it does not use
2764 // per se offsets. For instance, an aligned version of a structure is
2765 // compatible with an unaligned version of the same structure.
2766 gint
check_fields_compatibility(LttEventType
*event_type1
,
2767 LttEventType
*event_type2
,
2768 LttField
*field1
, LttField
*field2
)
2770 guint different
= 0;
2774 if(field1
== NULL
) {
2775 if(field2
== NULL
) goto end
;
2780 } else if(field2
== NULL
) {
2785 type1
= &field1
->field_type
;
2786 type2
= &field2
->field_type
;
2788 if(type1
->type_class
!= type2
->type_class
) {
2792 if(type1
->network
!= type2
->network
) {
2797 switch(type1
->type_class
) {
2799 case LTT_UINT_FIXED
:
2814 if(field1
->field_size
!= field2
->field_size
)
2821 LttField
*child1
= &g_array_index(type1
->fields
, LttField
, 0);
2822 LttField
*child2
= &g_array_index(type2
->fields
, LttField
, 0);
2824 if(type1
->size
!= type2
->size
)
2826 if(check_fields_compatibility(event_type1
, event_type2
, child1
, child2
))
2832 LttField
*child1
= &g_array_index(type1
->fields
, LttField
, 1);
2833 LttField
*child2
= &g_array_index(type2
->fields
, LttField
, 1);
2835 if(check_fields_compatibility(event_type1
, event_type2
, child1
, child2
))
2845 if(type1
->fields
->len
!= type2
->fields
->len
) {
2850 for(i
=0; i
< type1
->fields
->len
; i
++) {
2853 child1
= &g_array_index(type1
->fields
, LttField
, i
);
2854 child2
= &g_array_index(type2
->fields
, LttField
, i
);
2855 different
= check_fields_compatibility(event_type1
,
2856 event_type2
, child1
, child2
);
2858 if(different
) break;
2864 g_error("precompute_fields_offsets : unknown type");
2873 gint
check_fields_compatibility(LttEventType
*event_type1
,
2874 LttEventType
*event_type2
,
2875 LttField
*field1
, LttField
*field2
)
2877 guint different
= 0;
2882 if(field1
== NULL
) {
2883 if(field2
== NULL
) goto end
;
2888 } else if(field2
== NULL
) {
2893 g_assert(field1
->fixed_root
!= FIELD_UNKNOWN
);
2894 g_assert(field2
->fixed_root
!= FIELD_UNKNOWN
);
2895 g_assert(field1
->fixed_parent
!= FIELD_UNKNOWN
);
2896 g_assert(field2
->fixed_parent
!= FIELD_UNKNOWN
);
2897 g_assert(field1
->fixed_size
!= FIELD_UNKNOWN
);
2898 g_assert(field2
->fixed_size
!= FIELD_UNKNOWN
);
2900 type1
= field1
->field_type
;
2901 type2
= field2
->field_type
;
2903 if(type1
->type_class
!= type2
->type_class
) {
2907 if(type1
->element_name
!= type2
->element_name
) {
2912 switch(type1
->type_class
) {
2914 case LTT_UINT_FIXED
:
2929 if(field1
->field_size
!= field2
->field_size
) {
2935 if(type1
->element_number
!= type2
->element_number
) {
2939 for(i
=0;i
<type1
->element_number
;i
++) {
2940 if(type1
->enum_strings
[i
] != type2
->enum_strings
[i
]) {
2947 /* Two elements : size and child */
2948 g_assert(type1
->element_number
!= type2
->element_number
);
2949 for(i
=0;i
<type1
->element_number
;i
++) {
2950 if(check_fields_compatibility(event_type1
, event_type2
,
2951 field1
->child
[0], field2
->child
[0])) {
2960 if(field1
->field_size
!= field2
->field_size
) {
2964 /* Two elements : size and child */
2965 g_assert(type1
->element_number
!= type2
->element_number
);
2966 for(i
=0;i
<type1
->element_number
;i
++) {
2967 if(check_fields_compatibility(event_type1
, event_type2
,
2968 field1
->child
[0], field2
->child
[0])) {
2976 if(type1
->element_number
!= type2
->element_number
) {
2980 for(i
=0;i
<type1
->element_number
;i
++) {
2981 if(check_fields_compatibility(event_type1
, event_type2
,
2982 field1
->child
[0], field2
->child
[0])) {
2995 /*****************************************************************************
2997 * ltt_get_int : get an integer number
2999 * reverse_byte_order: must we reverse the byte order ?
3000 * size : the size of the integer
3001 * ptr : the data pointer
3003 * gint64 : a 64 bits integer
3004 ****************************************************************************/
3006 gint64
ltt_get_int(gboolean reverse_byte_order
, gint size
, void *data
)
3011 case 1: val
= *((gint8
*)data
); break;
3012 case 2: val
= ltt_get_int16(reverse_byte_order
, data
); break;
3013 case 4: val
= ltt_get_int32(reverse_byte_order
, data
); break;
3014 case 8: val
= ltt_get_int64(reverse_byte_order
, data
); break;
3015 default: val
= ltt_get_int64(reverse_byte_order
, data
);
3016 g_critical("get_int : integer size %d unknown", size
);
3023 /*****************************************************************************
3025 * ltt_get_uint : get an unsigned integer number
3027 * reverse_byte_order: must we reverse the byte order ?
3028 * size : the size of the integer
3029 * ptr : the data pointer
3031 * guint64 : a 64 bits unsigned integer
3032 ****************************************************************************/
3034 guint64
ltt_get_uint(gboolean reverse_byte_order
, gint size
, void *data
)
3039 case 1: val
= *((gint8
*)data
); break;
3040 case 2: val
= ltt_get_uint16(reverse_byte_order
, data
); break;
3041 case 4: val
= ltt_get_uint32(reverse_byte_order
, data
); break;
3042 case 8: val
= ltt_get_uint64(reverse_byte_order
, data
); break;
3043 default: val
= ltt_get_uint64(reverse_byte_order
, data
);
3044 g_critical("get_uint : unsigned integer size %d unknown",
3053 /* get the node name of the system */
3055 char * ltt_trace_system_description_node_name (LttSystemDescription
* s
)
3057 return s
->node_name
;
3061 /* get the domain name of the system */
3063 char * ltt_trace_system_description_domain_name (LttSystemDescription
* s
)
3065 return s
->domain_name
;
3069 /* get the description of the system */
3071 char * ltt_trace_system_description_description (LttSystemDescription
* s
)
3073 return s
->description
;
3077 /* get the NTP corrected start time of the trace */
3078 LttTime
ltt_trace_start_time(LttTrace
*t
)
3080 return t
->start_time
;
3083 /* get the monotonic start time of the trace */
3084 LttTime
ltt_trace_start_time_monotonic(LttTrace
*t
)
3086 return t
->start_time_from_tsc
;
3089 LttTracefile
*ltt_tracefile_new()
3091 return g_new(LttTracefile
, 1);
3094 void ltt_tracefile_destroy(LttTracefile
*tf
)
3099 void ltt_tracefile_copy(LttTracefile
*dest
, const LttTracefile
*src
)
3104 /* Before library loading... */
3106 static void __attribute__((constructor
)) init(void)
3108 LTT_FACILITY_NAME_HEARTBEAT
= g_quark_from_string("heartbeat");
3109 LTT_EVENT_NAME_HEARTBEAT
= g_quark_from_string("heartbeat");
3111 LTT_TRACEFILE_NAME_FACILITIES
= g_quark_from_string("/control/facilities");