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 LTT_EVENT_NAME_HEARTBEAT_FULL
;
58 GQuark LTT_TRACEFILE_NAME_FACILITIES
;
65 #define __UNUSED__ __attribute__((__unused__))
67 #define g_info(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, format)
70 #define g_debug(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format)
75 /* Those macros must be called from within a function where page_size is a known
77 #define PAGE_MASK (~(page_size-1))
78 #define PAGE_ALIGN(addr) (((addr)+page_size-1)&PAGE_MASK)
80 /* set the offset of the fields belonging to the event,
81 need the information of the archecture */
82 //void set_fields_offsets(LttTracefile *tf, LttEventType *event_type);
83 //size_t get_fields_offsets(LttTracefile *tf, LttEventType *event_type, void *data);
85 /* get the size of the field type according to
86 * The facility size information. */
88 static inline void preset_field_type_size(LttTracefile
*tf
,
89 LttEventType
*event_type
,
90 off_t offset_root
, off_t offset_parent
,
91 enum field_status
*fixed_root
, enum field_status
*fixed_parent
,
95 /* map a fixed size or a block information from the file (fd) */
96 static gint
map_block(LttTracefile
* tf
, guint block_num
);
98 /* calculate nsec per cycles for current block */
100 static guint32
calc_nsecs_per_cycle(LttTracefile
* t
);
101 static guint64
cycles_2_ns(LttTracefile
*tf
, guint64 cycles
);
104 /* go to the next event */
105 static int ltt_seek_next_event(LttTracefile
*tf
);
107 void ltt_update_event_size(LttTracefile
*tf
);
110 void precompute_offsets(LttFacility
*fac
, LttEventType
*event
);
113 /* Functions to parse system.xml file (using glib xml parser) */
114 static void parser_start_element (GMarkupParseContext __UNUSED__
*context
,
115 const gchar
*element_name
,
116 const gchar
**attribute_names
,
117 const gchar
**attribute_values
,
122 LttSystemDescription
* des
= (LttSystemDescription
* )user_data
;
123 if(strcmp("system", element_name
)){
124 *error
= g_error_new(G_MARKUP_ERROR
,
126 "This is not system.xml file");
130 while(attribute_names
[i
]){
131 if(strcmp("node_name", attribute_names
[i
])==0){
132 des
->node_name
= g_strdup(attribute_values
[i
]);
133 }else if(strcmp("domainname", attribute_names
[i
])==0){
134 des
->domain_name
= g_strdup(attribute_values
[i
]);
135 }else if(strcmp("cpu", attribute_names
[i
])==0){
136 des
->nb_cpu
= atoi(attribute_values
[i
]);
137 }else if(strcmp("arch_size", attribute_names
[i
])==0){
138 if(strcmp(attribute_values
[i
],"LP32") == 0) des
->size
= LTT_LP32
;
139 else if(strcmp(attribute_values
[i
],"ILP32") == 0) des
->size
= LTT_ILP32
;
140 else if(strcmp(attribute_values
[i
],"LP64") == 0) des
->size
= LTT_LP64
;
141 else if(strcmp(attribute_values
[i
],"ILP64") == 0) des
->size
= LTT_ILP64
;
142 else if(strcmp(attribute_values
[i
],"UNKNOWN") == 0) des
->size
= LTT_UNKNOWN
;
143 }else if(strcmp("endian", attribute_names
[i
])==0){
144 if(strcmp(attribute_values
[i
],"LITTLE_ENDIAN") == 0)
145 des
->endian
= LTT_LITTLE_ENDIAN
;
146 else if(strcmp(attribute_values
[i
],"BIG_ENDIAN") == 0)
147 des
->endian
= LTT_BIG_ENDIAN
;
148 }else if(strcmp("kernel_name", attribute_names
[i
])==0){
149 des
->kernel_name
= g_strdup(attribute_values
[i
]);
150 }else if(strcmp("kernel_release", attribute_names
[i
])==0){
151 des
->kernel_release
= g_strdup(attribute_values
[i
]);
152 }else if(strcmp("kernel_version", attribute_names
[i
])==0){
153 des
->kernel_version
= g_strdup(attribute_values
[i
]);
154 }else if(strcmp("machine", attribute_names
[i
])==0){
155 des
->machine
= g_strdup(attribute_values
[i
]);
156 }else if(strcmp("processor", attribute_names
[i
])==0){
157 des
->processor
= g_strdup(attribute_values
[i
]);
158 }else if(strcmp("hardware_platform", attribute_names
[i
])==0){
159 des
->hardware_platform
= g_strdup(attribute_values
[i
]);
160 }else if(strcmp("operating_system", attribute_names
[i
])==0){
161 des
->operating_system
= g_strdup(attribute_values
[i
]);
162 }else if(strcmp("ltt_major_version", attribute_names
[i
])==0){
163 des
->ltt_major_version
= atoi(attribute_values
[i
]);
164 }else if(strcmp("ltt_minor_version", attribute_names
[i
])==0){
165 des
->ltt_minor_version
= atoi(attribute_values
[i
]);
166 }else if(strcmp("ltt_block_size", attribute_names
[i
])==0){
167 des
->ltt_block_size
= atoi(attribute_values
[i
]);
169 *error
= g_error_new(G_MARKUP_ERROR
,
171 "Not a valid attribute");
178 static void parser_characters (GMarkupParseContext __UNUSED__
*context
,
180 gsize __UNUSED__ text_len
,
182 GError __UNUSED__
**error
)
184 LttSystemDescription
* des
= (LttSystemDescription
* )user_data
;
185 des
->description
= g_strdup(text
);
190 LttFacility
*ltt_trace_get_facility_by_num(LttTrace
*t
,
193 g_assert(num
< t
->facilities_by_num
->len
);
195 return &g_array_index(t
->facilities_by_num
, LttFacility
, num
);
199 guint
ltt_trace_get_num_cpu(LttTrace
*t
)
207 * Return value : 0 success, 1 bad tracefile
209 int parse_trace_header(void *header
, LttTracefile
*tf
, LttTrace
*t
)
211 guint32
*magic_number
= (guint32
*)header
;
212 struct ltt_trace_header_any
*any
= (struct ltt_trace_header_any
*)header
;
214 if(*magic_number
== LTT_MAGIC_NUMBER
)
216 else if(*magic_number
== LTT_REV_MAGIC_NUMBER
)
218 else /* invalid magic number, bad tracefile ! */
221 /* Get float byte order : might be different from int byte order
222 * (or is set to 0 if the trace has no float (kernel trace)) */
223 tf
->float_word_order
= any
->float_word_order
;
224 tf
->has_alignment
= any
->has_alignment
;
227 t
->arch_type
= ltt_get_uint32(LTT_GET_BO(tf
),
229 t
->arch_variant
= ltt_get_uint32(LTT_GET_BO(tf
),
231 t
->arch_size
= any
->arch_size
;
232 t
->ltt_major_version
= any
->major_version
;
233 t
->ltt_minor_version
= any
->minor_version
;
234 t
->flight_recorder
= any
->flight_recorder
;
235 t
->has_heartbeat
= any
->has_heartbeat
;
236 t
->compact_facilities
= NULL
;
240 switch(any
->major_version
) {
243 switch(any
->minor_version
) {
246 tf
->buffer_header_size
=
247 sizeof(struct ltt_block_start_header
)
248 + sizeof(struct ltt_trace_header_0_3
);
249 g_warning("Unsupported trace version : %hhu.%hhu",
250 any
->major_version
, any
->minor_version
);
256 struct ltt_trace_header_0_7
*vheader
=
257 (struct ltt_trace_header_0_7
*)header
;
258 tf
->buffer_header_size
=
259 sizeof(struct ltt_block_start_header
)
260 + sizeof(struct ltt_trace_header_0_7
);
261 tf
->tsc_lsb_truncate
= 0;
263 tf
->tsc_msb_cutoff
= 32 - tf
->tsc_lsb_truncate
- tf
->tscbits
;
264 tf
->tsc_mask
= (1ULL<<32)-1;
265 tf
->tsc_mask_next_bit
= (1ULL<<32);
267 t
->start_freq
= ltt_get_uint64(LTT_GET_BO(tf
),
268 &vheader
->start_freq
);
269 t
->freq_scale
= ltt_get_uint32(LTT_GET_BO(tf
),
270 &vheader
->freq_scale
);
271 t
->start_tsc
= ltt_get_uint64(LTT_GET_BO(tf
),
272 &vheader
->start_tsc
);
273 t
->start_monotonic
= ltt_get_uint64(LTT_GET_BO(tf
),
274 &vheader
->start_monotonic
);
275 t
->start_time
.tv_sec
= ltt_get_uint64(LTT_GET_BO(tf
),
276 &vheader
->start_time_sec
);
277 t
->start_time
.tv_nsec
= ltt_get_uint64(LTT_GET_BO(tf
),
278 &vheader
->start_time_usec
);
279 t
->start_time
.tv_nsec
*= 1000; /* microsec to nanosec */
281 t
->start_time_from_tsc
= ltt_time_from_uint64(
283 * (1000000000.0 / tf
->trace
->freq_scale
)
284 / (double)t
->start_freq
);
290 struct ltt_trace_header_0_8
*vheader
=
291 (struct ltt_trace_header_0_8
*)header
;
292 tf
->buffer_header_size
=
293 sizeof(struct ltt_block_start_header
)
294 + sizeof(struct ltt_trace_header_0_8
);
295 tf
->tsc_lsb_truncate
= vheader
->tsc_lsb_truncate
;
296 tf
->tscbits
= vheader
->tscbits
;
297 tf
->tsc_msb_cutoff
= 32 - tf
->tsc_lsb_truncate
- tf
->tscbits
;
298 tf
->tsc_mask
= ((1ULL << (tf
->tscbits
))-1);
299 tf
->tsc_mask
= tf
->tsc_mask
<< tf
->tsc_lsb_truncate
;
300 tf
->tsc_mask_next_bit
= (1ULL<<(tf
->tscbits
));
301 tf
->tsc_mask_next_bit
= tf
->tsc_mask_next_bit
<< tf
->tsc_lsb_truncate
;
303 t
->start_freq
= ltt_get_uint64(LTT_GET_BO(tf
),
304 &vheader
->start_freq
);
305 t
->freq_scale
= ltt_get_uint32(LTT_GET_BO(tf
),
306 &vheader
->freq_scale
);
307 t
->start_tsc
= ltt_get_uint64(LTT_GET_BO(tf
),
308 &vheader
->start_tsc
);
309 t
->start_monotonic
= ltt_get_uint64(LTT_GET_BO(tf
),
310 &vheader
->start_monotonic
);
311 t
->start_time
.tv_sec
= ltt_get_uint64(LTT_GET_BO(tf
),
312 &vheader
->start_time_sec
);
313 t
->start_time
.tv_nsec
= ltt_get_uint64(LTT_GET_BO(tf
),
314 &vheader
->start_time_usec
);
315 t
->start_time
.tv_nsec
*= 1000; /* microsec to nanosec */
317 t
->start_time_from_tsc
= ltt_time_from_uint64(
319 * (1000000000.0 / tf
->trace
->freq_scale
)
320 / (double)t
->start_freq
);
325 g_warning("Unsupported trace version : %hhu.%hhu",
326 any
->major_version
, any
->minor_version
);
332 g_warning("Unsupported trace version : %hhu.%hhu",
333 any
->major_version
, any
->minor_version
);
343 /*****************************************************************************
345 * ltt_tracefile_open : open a trace file, construct a LttTracefile
347 * t : the trace containing the tracefile
348 * fileName : path name of the trace file
349 * tf : the tracefile structure
351 * : 0 for success, -1 otherwise.
352 ****************************************************************************/
354 gint
ltt_tracefile_open(LttTrace
*t
, gchar
* fileName
, LttTracefile
*tf
)
356 struct stat lTDFStat
; /* Trace data file status */
357 struct ltt_block_start_header
*header
;
358 int page_size
= getpagesize();
361 tf
->long_name
= g_quark_from_string(fileName
);
363 tf
->fd
= open(fileName
, O_RDONLY
);
365 g_warning("Unable to open input data file %s\n", fileName
);
369 // Get the file's status
370 if(fstat(tf
->fd
, &lTDFStat
) < 0){
371 g_warning("Unable to get the status of the input data file %s\n", fileName
);
375 // Is the file large enough to contain a trace
376 if(lTDFStat
.st_size
<
377 (off_t
)(sizeof(struct ltt_block_start_header
)
378 + sizeof(struct ltt_trace_header_any
))){
379 g_print("The input data file %s does not contain a trace\n", fileName
);
383 /* Temporarily map the buffer start header to get trace information */
384 /* Multiple of pages aligned head */
385 tf
->buffer
.head
= mmap(0,
386 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)
387 + sizeof(struct ltt_trace_header_any
)), PROT_READ
,
388 MAP_PRIVATE
, tf
->fd
, 0);
389 if(tf
->buffer
.head
== MAP_FAILED
) {
390 perror("Error in allocating memory for buffer of tracefile");
393 g_assert( ( (guint
)tf
->buffer
.head
&(8-1) ) == 0); // make sure it's aligned.
395 header
= (struct ltt_block_start_header
*)tf
->buffer
.head
;
397 if(parse_trace_header(header
->trace
, tf
, NULL
)) {
398 g_warning("parse_trace_header error");
402 //store the size of the file
403 tf
->file_size
= lTDFStat
.st_size
;
404 tf
->buf_size
= ltt_get_uint32(LTT_GET_BO(tf
), &header
->buf_size
);
405 tf
->num_blocks
= tf
->file_size
/ tf
->buf_size
;
407 if(munmap(tf
->buffer
.head
,
408 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)
409 + sizeof(struct ltt_trace_header_any
)))) {
410 g_warning("unmap size : %u\n",
411 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)
412 + sizeof(struct ltt_trace_header_any
)));
413 perror("munmap error");
416 tf
->buffer
.head
= NULL
;
418 //read the first block
419 if(map_block(tf
,0)) {
420 perror("Cannot map block for tracefile");
428 if(munmap(tf
->buffer
.head
,
429 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)
430 + sizeof(struct ltt_trace_header_any
)))) {
431 g_warning("unmap size : %u\n",
432 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)
433 + sizeof(struct ltt_trace_header_any
)));
434 perror("munmap error");
443 LttTrace
*ltt_tracefile_get_trace(LttTracefile
*tf
)
449 /*****************************************************************************
450 *Open control and per cpu tracefiles
451 ****************************************************************************/
453 void ltt_tracefile_open_cpu(LttTrace
*t
, gchar
* tracefile_name
)
456 tf
= ltt_tracefile_open(t
,tracefile_name
);
458 t
->per_cpu_tracefile_number
++;
459 g_ptr_array_add(t
->per_cpu_tracefiles
, tf
);
462 gint
ltt_tracefile_open_control(LttTrace
*t
, gchar
* control_name
)
471 tf
= ltt_tracefile_open(t
,control_name
);
473 g_warning("ltt_tracefile_open_control : bad file descriptor");
476 t
->control_tracefile_number
++;
477 g_ptr_array_add(t
->control_tracefiles
,tf
);
479 //parse facilities tracefile to get base_id
480 if(strcmp(&control_name
[strlen(control_name
)-10],"facilities") ==0){
482 if(!ltt_tracefile_read(tf
,&ev
)) return 0; // end of file
484 if(ev
.event_id
== TRACE_FACILITY_LOAD
){
486 fLoad
.name
= (gchar
*)pos
;
487 fLoad
.checksum
= *(LttChecksum
*)(pos
+ strlen(fLoad
.name
));
488 fLoad
.base_code
= *(guint32
*)(pos
+ strlen(fLoad
.name
) + sizeof(LttChecksum
));
490 for(i
=0;i
<t
->facility_number
;i
++){
491 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
,i
);
492 if(strcmp(f
->name
,fLoad
.name
)==0 && fLoad
.checksum
==f
->checksum
){
493 f
->base_id
= fLoad
.base_code
;
497 if(i
==t
->facility_number
) {
498 g_warning("Facility: %s, checksum: %u is not found",
499 fLoad
.name
,(unsigned int)fLoad
.checksum
);
502 }else if(ev
.event_id
== TRACE_BLOCK_START
){
504 }else if(ev
.event_id
== TRACE_BLOCK_END
){
507 g_warning("Not valid facilities trace file");
516 /*****************************************************************************
518 * ltt_tracefile_close: close a trace file,
520 * t : tracefile which will be closed
521 ****************************************************************************/
523 void ltt_tracefile_close(LttTracefile
*t
)
525 int page_size
= getpagesize();
527 if(t
->buffer
.head
!= NULL
)
528 if(munmap(t
->buffer
.head
, PAGE_ALIGN(t
->buf_size
))) {
529 g_warning("unmap size : %u\n",
530 PAGE_ALIGN(t
->buf_size
));
531 perror("munmap error");
539 /*****************************************************************************
540 *Get system information
541 ****************************************************************************/
543 gint
getSystemInfo(LttSystemDescription
* des
, gchar
* pathname
)
550 GMarkupParseContext
* context
;
551 GError
* error
= NULL
;
552 GMarkupParser markup_parser
=
554 parser_start_element
,
557 NULL
, /* passthrough */
561 fd
= g_open(pathname
, O_RDONLY
, 0);
563 g_warning("Can not open file : %s\n", pathname
);
567 iochan
= g_io_channel_unix_new(fd
);
569 context
= g_markup_parse_context_new(&markup_parser
, 0, des
,NULL
);
571 //while(fgets(buf,DIR_NAME_SIZE, fp) != NULL){
572 while(g_io_channel_read_line(iochan
, &buf
, &length
, NULL
, &error
)
573 != G_IO_STATUS_EOF
) {
576 g_warning("Can not read xml file: \n%s\n", error
->message
);
579 if(!g_markup_parse_context_parse(context
, buf
, length
, &error
)){
581 g_warning("Can not parse xml file: \n%s\n", error
->message
);
584 g_markup_parse_context_free(context
);
586 g_io_channel_shutdown(iochan
, FALSE
, &error
); /* No flush */
588 g_warning("Can not close file: \n%s\n", error
->message
);
596 g_markup_parse_context_free(context
);
598 g_io_channel_shutdown(iochan
, FALSE
, &error
); /* No flush */
600 g_warning("Can not close file: \n%s\n", error
->message
);
611 /*****************************************************************************
612 *The following functions get facility/tracefile information
613 ****************************************************************************/
615 gint
getFacilityInfo(LttTrace
*t
, gchar
* eventdefs
)
622 gchar fullname
[DIR_NAME_SIZE
];
623 GError
* error
= NULL
;
625 dir
= g_dir_open(eventdefs
, 0, &error
);
628 g_warning("Can not open directory: %s, %s\n", eventdefs
, error
->message
);
633 while((name
= g_dir_read_name(dir
)) != NULL
){
634 if(!g_pattern_match_simple("*.xml", name
)) continue;
635 strcpy(fullname
,eventdefs
);
636 strcat(fullname
,name
);
637 ltt_facility_open(t
,fullname
);
641 for(j
=0;j
<t
->facility_number
;j
++){
642 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, j
);
643 for(i
=0; i
<f
->event_number
; i
++){
645 setFieldsOffset(NULL
, et
, NULL
, t
);
652 /*****************************************************************************
653 *A trace is specified as a pathname to the directory containing all the
654 *associated data (control tracefiles, per cpu tracefiles, event
657 *When a trace is closed, all the associated facilities, types and fields
658 *are released as well.
662 /****************************************************************************
663 * get_absolute_pathname
665 * return the unique pathname in the system
667 * MD : Fixed this function so it uses realpath, dealing well with
668 * forgotten cases (.. were not used correctly before).
670 ****************************************************************************/
671 void get_absolute_pathname(const gchar
*pathname
, gchar
* abs_pathname
)
673 abs_pathname
[0] = '\0';
675 if ( realpath (pathname
, abs_pathname
) != NULL
)
679 /* error, return the original path unmodified */
680 strcpy(abs_pathname
, pathname
);
686 /* Search for something like : .*_.*
688 * The left side is the name, the right side is the number.
691 int get_tracefile_name_number(gchar
*raw_name
,
698 guint raw_name_len
= strlen(raw_name
);
699 gchar char_name
[PATH_MAX
];
706 for(i
=raw_name_len
-1;i
>=0;i
--) {
707 if(raw_name
[i
] == '_') break;
709 if(i
==-1) { /* Either not found or name length is 0 */
710 /* This is a userspace tracefile */
711 strncpy(char_name
, raw_name
, raw_name_len
);
712 char_name
[raw_name_len
] = '\0';
713 *name
= g_quark_from_string(char_name
);
714 *num
= 0; /* unknown cpu */
715 for(i
=0;i
<raw_name_len
;i
++) {
716 if(raw_name
[i
] == '/') {
721 for(;i
<raw_name_len
;i
++) {
722 if(raw_name
[i
] == '/') {
727 for(;i
<raw_name_len
;i
++) {
728 if(raw_name
[i
] == '-') {
732 if(i
== raw_name_len
) return -1;
734 tmpptr
= &raw_name
[i
];
735 for(;i
<raw_name_len
;i
++) {
736 if(raw_name
[i
] == '.') {
741 *tid
= strtoul(tmpptr
, &endptr
, 10);
743 return -1; /* No digit */
744 if(*tid
== ULONG_MAX
)
745 return -1; /* underflow / overflow */
747 tmpptr
= &raw_name
[i
];
748 for(;i
<raw_name_len
;i
++) {
749 if(raw_name
[i
] == '.') {
754 *pgid
= strtoul(tmpptr
, &endptr
, 10);
756 return -1; /* No digit */
757 if(*pgid
== ULONG_MAX
)
758 return -1; /* underflow / overflow */
760 tmpptr
= &raw_name
[i
];
761 *creation
= strtoull(tmpptr
, &endptr
, 10);
763 return -1; /* No digit */
764 if(*creation
== G_MAXUINT64
)
765 return -1; /* underflow / overflow */
769 cpu_num
= strtol(raw_name
+underscore_pos
+1, &endptr
, 10);
771 if(endptr
== raw_name
+underscore_pos
+1)
772 return -1; /* No digit */
773 if(cpu_num
== LONG_MIN
|| cpu_num
== LONG_MAX
)
774 return -1; /* underflow / overflow */
776 strncpy(char_name
, raw_name
, underscore_pos
);
777 char_name
[underscore_pos
] = '\0';
779 *name
= g_quark_from_string(char_name
);
788 GData
**ltt_trace_get_tracefiles_groups(LttTrace
*trace
)
790 return &trace
->tracefiles
;
794 void compute_tracefile_group(GQuark key_id
,
796 struct compute_tracefile_group_args
*args
)
801 for(i
=0; i
<group
->len
; i
++) {
802 tf
= &g_array_index (group
, LttTracefile
, i
);
804 args
->func(tf
, args
->func_args
);
809 void ltt_tracefile_group_destroy(gpointer data
)
811 GArray
*group
= (GArray
*)data
;
815 for(i
=0; i
<group
->len
; i
++) {
816 tf
= &g_array_index (group
, LttTracefile
, i
);
818 ltt_tracefile_close(tf
);
820 g_array_free(group
, TRUE
);
823 gboolean
ltt_tracefile_group_has_cpu_online(gpointer data
)
825 GArray
*group
= (GArray
*)data
;
829 for(i
=0; i
<group
->len
; i
++) {
830 tf
= &g_array_index (group
, LttTracefile
, i
);
831 if(tf
->cpu_online
) return 1;
837 /* Open each tracefile under a specific directory. Put them in a
838 * GData : permits to access them using their tracefile group pathname.
839 * i.e. access control/modules tracefile group by index :
842 * relative path is the path relative to the trace root
843 * root path is the full path
845 * A tracefile group is simply an array where all the per cpu tracefiles sits.
848 static int open_tracefiles(LttTrace
*trace
, gchar
*root_path
,
849 gchar
*relative_path
)
851 DIR *dir
= opendir(root_path
);
852 struct dirent
*entry
;
853 struct stat stat_buf
;
856 gchar path
[PATH_MAX
];
861 gchar rel_path
[PATH_MAX
];
870 strncpy(path
, root_path
, PATH_MAX
-1);
871 path_len
= strlen(path
);
872 path
[path_len
] = '/';
874 path_ptr
= path
+ path_len
;
876 strncpy(rel_path
, relative_path
, PATH_MAX
-1);
877 rel_path_len
= strlen(rel_path
);
878 rel_path
[rel_path_len
] = '/';
880 rel_path_ptr
= rel_path
+ rel_path_len
;
882 while((entry
= readdir(dir
)) != NULL
) {
884 if(entry
->d_name
[0] == '.') continue;
886 strncpy(path_ptr
, entry
->d_name
, PATH_MAX
- path_len
);
887 strncpy(rel_path_ptr
, entry
->d_name
, PATH_MAX
- rel_path_len
);
889 ret
= stat(path
, &stat_buf
);
895 g_debug("Tracefile file or directory : %s\n", path
);
897 if(strcmp(rel_path
, "/eventdefs") == 0) continue;
899 if(S_ISDIR(stat_buf
.st_mode
)) {
901 g_debug("Entering subdirectory...\n");
902 ret
= open_tracefiles(trace
, path
, rel_path
);
903 if(ret
< 0) continue;
904 } else if(S_ISREG(stat_buf
.st_mode
)) {
906 guint num
, tid
, pgid
;
909 num
= tid
= pgid
= 0;
911 if(get_tracefile_name_number(rel_path
, &name
, &num
, &tid
, &pgid
, &creation
))
912 continue; /* invalid name */
914 g_debug("Opening file.\n");
915 if(ltt_tracefile_open(trace
, path
, &tmp_tf
)) {
916 g_info("Error opening tracefile %s", path
);
918 continue; /* error opening the tracefile : bad magic number ? */
921 g_debug("Tracefile name is %s and number is %u",
922 g_quark_to_string(name
), num
);
924 tmp_tf
.cpu_online
= 1;
925 tmp_tf
.cpu_num
= num
;
929 tmp_tf
.creation
= creation
;
930 if(tmp_tf
.name
== g_quark_from_string("/compact")
931 || tmp_tf
.name
== g_quark_from_string("/flight-compact"))
935 group
= g_datalist_id_get_data(&trace
->tracefiles
, name
);
937 /* Elements are automatically cleared when the array is allocated.
938 * It makes the cpu_online variable set to 0 : cpu offline, by default.
940 group
= g_array_sized_new (FALSE
, TRUE
, sizeof(LttTracefile
), 10);
941 g_datalist_id_set_data_full(&trace
->tracefiles
, name
,
942 group
, ltt_tracefile_group_destroy
);
945 /* Add the per cpu tracefile to the named group */
946 unsigned int old_len
= group
->len
;
948 group
= g_array_set_size(group
, num
+1);
949 g_array_index (group
, LttTracefile
, num
) = tmp_tf
;
959 /* ltt_get_facility_description
961 * Opens the file corresponding to the requested facility (identified by fac_id
964 * The name searched is : %trace root%/eventdefs/facname_checksum.xml
966 * Returns 0 on success, or 1 on failure.
969 static int ltt_get_facility_description(LttFacility
*f
,
971 LttTracefile
*fac_tf
)
973 char desc_file_name
[PATH_MAX
];
980 text
= g_quark_to_string(t
->pathname
);
981 textlen
= strlen(text
);
983 if(textlen
>= PATH_MAX
) goto name_error
;
984 strcpy(desc_file_name
, text
);
986 text
= "/eventdefs/";
987 textlen
+=strlen(text
);
988 if(textlen
>= PATH_MAX
) goto name_error
;
989 strcat(desc_file_name
, text
);
991 text
= g_quark_to_string(f
->name
);
992 fac_name_len
= strlen(text
);
993 textlen
+=fac_name_len
;
994 if(textlen
>= PATH_MAX
) goto name_error
;
995 strcat(desc_file_name
, text
);
997 /* arch specific facilities are named like this : name_arch */
998 if(fac_name_len
+1 < sizeof("_arch"))
1001 if(!strcmp(&text
[fac_name_len
+1-sizeof("_arch")], "_arch"))
1009 textlen
+=strlen(text
);
1010 if(textlen
>= PATH_MAX
) goto name_error
;
1011 strcat(desc_file_name
, text
);
1013 err
= snprintf(desc_file_name
+textlen
, PATH_MAX
-textlen
-1,
1015 if(err
< 0) goto name_error
;
1017 textlen
=strlen(desc_file_name
);
1022 switch(t
->arch_type
) {
1023 case LTT_ARCH_TYPE_I386
:
1026 case LTT_ARCH_TYPE_PPC
:
1029 case LTT_ARCH_TYPE_SH
:
1032 case LTT_ARCH_TYPE_S390
:
1035 case LTT_ARCH_TYPE_MIPS
:
1038 case LTT_ARCH_TYPE_ARM
:
1041 case LTT_ARCH_TYPE_PPC64
:
1044 case LTT_ARCH_TYPE_X86_64
:
1047 case LTT_ARCH_TYPE_C2
:
1050 case LTT_ARCH_TYPE_POWERPC
:
1054 g_error("Trace from unsupported architecture.");
1056 textlen
+=strlen(text
);
1057 if(textlen
>= PATH_MAX
) goto name_error
;
1058 strcat(desc_file_name
, text
);
1062 textlen
+=strlen(text
);
1063 if(textlen
>= PATH_MAX
) goto name_error
;
1064 strcat(desc_file_name
, text
);
1066 err
= ltt_facility_open(f
, t
, desc_file_name
);
1067 if(err
) goto facility_error
;
1076 static void ltt_fac_ids_destroy(gpointer data
)
1078 GArray
*fac_ids
= (GArray
*)data
;
1080 g_array_free(fac_ids
, TRUE
);
1084 /* Presumes the tracefile is already seeked at the beginning. It makes sense,
1085 * because it must be done just after the opening */
1086 static int ltt_process_facility_tracefile(LttTracefile
*tf
)
1095 err
= ltt_tracefile_read_seek(tf
);
1096 if(err
== EPERM
) goto seek_error
;
1097 else if(err
== ERANGE
) break; /* End of tracefile */
1099 err
= ltt_tracefile_read_update_event(tf
);
1100 if(err
) goto update_error
;
1102 /* We are on a facility load/or facility unload/ or heartbeat event */
1104 * * facility 0 is hardcoded : this is the core facility. It will be shown
1105 * in the facility array though, and is shown as "loaded builtin" in the
1107 * It contains event :
1109 * 1 : facility unload
1110 * 2 : state dump facility load
1113 if(tf
->event
.facility_id
!= LTT_FACILITY_CORE
) {
1114 /* Should only contain core facility */
1115 g_warning("Error in processing facility file %s, "
1116 "should not contain facility id %u.", g_quark_to_string(tf
->name
),
1117 tf
->event
.facility_id
);
1122 struct LttFacilityLoad
*fac_load_data
;
1123 struct LttStateDumpFacilityLoad
*fac_state_dump_load_data
;
1128 switch((enum ltt_core_events
)tf
->event
.event_id
) {
1129 case LTT_EVENT_FACILITY_LOAD
:
1130 fac_name
= (char*)(tf
->event
.data
);
1131 g_debug("Doing LTT_EVENT_FACILITY_LOAD of facility %s",
1133 pos
= (tf
->event
.data
+ strlen(fac_name
) + 1);
1134 pos
+= ltt_align((size_t)pos
, tf
->trace
->arch_size
, tf
->has_alignment
);
1135 fac_load_data
= (struct LttFacilityLoad
*)pos
;
1137 fac
= &g_array_index (tf
->trace
->facilities_by_num
, LttFacility
,
1138 ltt_get_uint32(LTT_GET_BO(tf
), &fac_load_data
->id
));
1139 /* facility may already exist if trace is paused/unpaused */
1140 if(fac
->exists
) continue;
1141 fac
->name
= g_quark_from_string(fac_name
);
1142 fac
->checksum
= ltt_get_uint32(LTT_GET_BO(tf
),
1143 &fac_load_data
->checksum
);
1144 fac
->id
= ltt_get_uint32(LTT_GET_BO(tf
), &fac_load_data
->id
);
1145 fac
->pointer_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1146 &fac_load_data
->pointer_size
);
1147 fac
->int_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1148 &fac_load_data
->int_size
);
1149 fac
->long_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1150 &fac_load_data
->long_size
);
1151 fac
->size_t_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1152 &fac_load_data
->size_t_size
);
1153 fac
->alignment
= ltt_get_uint32(LTT_GET_BO(tf
),
1154 &fac_load_data
->has_alignment
);
1156 if(ltt_get_facility_description(fac
, tf
->trace
, tf
))
1157 continue; /* error opening description */
1159 fac
->trace
= tf
->trace
;
1161 /* Preset the field offsets */
1162 for(i
=0; i
<fac
->events
->len
; i
++){
1163 et
= &g_array_index(fac
->events
, LttEventType
, i
);
1164 precompute_offsets(fac
, et
);
1169 fac_ids
= g_datalist_id_get_data(&tf
->trace
->facilities_by_name
,
1171 if(fac_ids
== NULL
) {
1172 fac_ids
= g_array_sized_new (FALSE
, TRUE
, sizeof(guint
), 1);
1173 g_datalist_id_set_data_full(&tf
->trace
->facilities_by_name
,
1175 fac_ids
, ltt_fac_ids_destroy
);
1177 g_array_append_val(fac_ids
, fac
->id
);
1180 case LTT_EVENT_FACILITY_UNLOAD
:
1181 g_debug("Doing LTT_EVENT_FACILITY_UNLOAD");
1182 /* We don't care about unload : facilities ID are valid for the whole
1183 * trace. They simply won't be used after the unload. */
1185 case LTT_EVENT_STATE_DUMP_FACILITY_LOAD
:
1186 fac_name
= (char*)(tf
->event
.data
);
1187 g_debug("Doing LTT_EVENT_STATE_DUMP_FACILITY_LOAD of facility %s",
1189 pos
= (tf
->event
.data
+ strlen(fac_name
) + 1);
1190 pos
+= ltt_align((size_t)pos
, tf
->trace
->arch_size
, tf
->has_alignment
);
1191 fac_state_dump_load_data
= (struct LttStateDumpFacilityLoad
*)pos
;
1193 fac
= &g_array_index (tf
->trace
->facilities_by_num
, LttFacility
,
1194 ltt_get_uint32(LTT_GET_BO(tf
), &fac_state_dump_load_data
->id
));
1195 /* facility may already exist if trace is paused/unpaused */
1196 if(fac
->exists
) continue;
1197 fac
->name
= g_quark_from_string(fac_name
);
1198 fac
->checksum
= ltt_get_uint32(LTT_GET_BO(tf
),
1199 &fac_state_dump_load_data
->checksum
);
1200 fac
->id
= ltt_get_uint32(LTT_GET_BO(tf
),
1201 &fac_state_dump_load_data
->id
);
1202 fac
->pointer_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1203 &fac_state_dump_load_data
->pointer_size
);
1204 fac
->int_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1205 &fac_state_dump_load_data
->int_size
);
1206 fac
->long_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1207 &fac_state_dump_load_data
->long_size
);
1208 fac
->size_t_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1209 &fac_state_dump_load_data
->size_t_size
);
1210 fac
->alignment
= ltt_get_uint32(LTT_GET_BO(tf
),
1211 &fac_state_dump_load_data
->has_alignment
);
1212 if(ltt_get_facility_description(fac
, tf
->trace
, tf
))
1213 continue; /* error opening description */
1215 fac
->trace
= tf
->trace
;
1217 /* Preset the field offsets */
1218 for(i
=0; i
<fac
->events
->len
; i
++){
1219 et
= &g_array_index(fac
->events
, LttEventType
, i
);
1220 precompute_offsets(fac
, et
);
1225 fac_ids
= g_datalist_id_get_data(&tf
->trace
->facilities_by_name
,
1227 if(fac_ids
== NULL
) {
1228 fac_ids
= g_array_sized_new (FALSE
, TRUE
, sizeof(guint
), 1);
1229 g_datalist_id_set_data_full(&tf
->trace
->facilities_by_name
,
1231 fac_ids
, ltt_fac_ids_destroy
);
1233 g_array_append_val(fac_ids
, fac
->id
);
1234 g_debug("fac id : %u", fac
->id
);
1237 case LTT_EVENT_HEARTBEAT
:
1239 case LTT_EVENT_HEARTBEAT_FULL
:
1242 g_warning("Error in processing facility file %s, "
1243 "unknown event id %hhu in core facility.",
1244 g_quark_to_string(tf
->name
),
1245 tf
->event
.event_id
);
1247 goto event_id_error
;
1253 /* Error handling */
1258 g_warning("An error occured in facility tracefile parsing");
1263 LttTrace
*ltt_trace_open(const gchar
*pathname
)
1265 gchar abs_path
[PATH_MAX
];
1270 struct ltt_block_start_header
*header
;
1272 struct dirent
*entry
;
1273 guint control_found
= 0;
1274 guint eventdefs_found
= 0;
1275 struct stat stat_buf
;
1276 gchar path
[PATH_MAX
];
1278 t
= g_new(LttTrace
, 1);
1279 if(!t
) goto alloc_error
;
1281 get_absolute_pathname(pathname
, abs_path
);
1282 t
->pathname
= g_quark_from_string(abs_path
);
1284 g_datalist_init(&t
->tracefiles
);
1286 /* Test to see if it looks like a trace */
1287 dir
= opendir(abs_path
);
1292 while((entry
= readdir(dir
)) != NULL
) {
1293 strcpy(path
, abs_path
);
1295 strcat(path
, entry
->d_name
);
1296 ret
= stat(path
, &stat_buf
);
1301 if(S_ISDIR(stat_buf
.st_mode
)) {
1302 if(strcmp(entry
->d_name
, "control") == 0) {
1305 if(strcmp(entry
->d_name
, "eventdefs") == 0) {
1306 eventdefs_found
= 1;
1312 if(!control_found
|| !eventdefs_found
) goto find_error
;
1314 /* Open all the tracefiles */
1315 if(open_tracefiles(t
, abs_path
, "")) {
1316 g_warning("Error opening tracefile %s", abs_path
);
1320 /* Prepare the facilities containers : array and mapping */
1321 /* Array is zeroed : the "exists" field is set to false by default */
1322 t
->facilities_by_num
= g_array_sized_new (FALSE
,
1323 TRUE
, sizeof(LttFacility
),
1325 t
->facilities_by_num
= g_array_set_size(t
->facilities_by_num
, NUM_FACILITIES
);
1327 g_datalist_init(&t
->facilities_by_name
);
1329 /* Parse each trace control/facilitiesN files : get runtime fac. info */
1330 group
= g_datalist_id_get_data(&t
->tracefiles
, LTT_TRACEFILE_NAME_FACILITIES
);
1332 g_error("Trace %s has no facility tracefile", abs_path
);
1334 goto facilities_error
;
1337 /* Get the trace information for the control/facility 0 tracefile */
1338 g_assert(group
->len
> 0);
1339 tf
= &g_array_index (group
, LttTracefile
, 0);
1340 header
= (struct ltt_block_start_header
*)tf
->buffer
.head
;
1341 g_assert(parse_trace_header(header
->trace
,
1344 t
->num_cpu
= group
->len
;
1346 for(i
=0; i
<group
->len
; i
++) {
1347 tf
= &g_array_index (group
, LttTracefile
, i
);
1348 if(ltt_process_facility_tracefile(tf
))
1349 goto facilities_error
;
1351 t
->compact_facilities
= ltt_trace_facility_get_by_name(t
,
1352 g_quark_from_string("compact"));
1353 if(!t
->compact_facilities
)
1354 t
->compact_facilities
= ltt_trace_facility_get_by_name(t
,
1355 g_quark_from_string("flight-compact"));
1359 /* Error handling */
1361 g_datalist_clear(&t
->facilities_by_name
);
1362 g_array_free(t
->facilities_by_num
, TRUE
);
1364 g_datalist_clear(&t
->tracefiles
);
1372 GQuark
ltt_trace_name(const LttTrace
*t
)
1378 /******************************************************************************
1379 * When we copy a trace, we want all the opening actions to happen again :
1380 * the trace will be reopened and totally independant from the original.
1381 * That's why we call ltt_trace_open.
1382 *****************************************************************************/
1383 LttTrace
*ltt_trace_copy(LttTrace
*self
)
1385 return ltt_trace_open(g_quark_to_string(self
->pathname
));
1388 void ltt_trace_close(LttTrace
*t
)
1393 for(i
=0; i
<t
->facilities_by_num
->len
; i
++) {
1394 fac
= &g_array_index (t
->facilities_by_num
, LttFacility
, i
);
1396 ltt_facility_close(fac
);
1399 g_datalist_clear(&t
->facilities_by_name
);
1400 g_array_free(t
->facilities_by_num
, TRUE
);
1401 g_datalist_clear(&t
->tracefiles
);
1406 /*****************************************************************************
1407 *Get the system description of the trace
1408 ****************************************************************************/
1410 LttFacility
*ltt_trace_facility_by_id(LttTrace
*t
, guint8 id
)
1412 g_assert(id
< t
->facilities_by_num
->len
);
1413 return &g_array_index(t
->facilities_by_num
, LttFacility
, id
);
1416 /* ltt_trace_facility_get_by_name
1418 * Returns the GArray of facility indexes. All the fac_ids that matches the
1419 * requested facility name.
1421 * If name is not found, returns NULL.
1423 GArray
*ltt_trace_facility_get_by_name(LttTrace
*t
, GQuark name
)
1425 return g_datalist_id_get_data(&t
->facilities_by_name
, name
);
1428 /*****************************************************************************
1429 * Functions to discover all the event types in the trace
1430 ****************************************************************************/
1433 unsigned ltt_trace_eventtype_number(LttTrace
*t
)
1437 unsigned int num
= t
->facility_number
;
1441 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, i
);
1442 count
+= f
->event_number
;
1449 //use an iteration on all the trace facilities, and inside iteration on all the
1450 //event types in each facilities instead.
1451 LttEventType
*ltt_trace_eventtype_get(LttTrace
*t
, unsigned evId
)
1453 LttEventType
*event_type
;
1456 f
= ltt_trace_facility_by_id(t
,evId
);
1458 if(unlikely(!f
)) event_type
= NULL
;
1459 else event_type
= f
->events
[evId
- f
->base_id
];
1466 /*****************************************************************************
1467 * ltt_trace_find_tracefile
1469 * Find a tracefile by name and index in the group.
1471 * Returns a pointer to the tracefiles, else NULL.
1472 ****************************************************************************/
1474 LttTracefile
*ltt_trace_find_tracefile(LttTrace
*t
, const gchar
*name
)
1479 /*****************************************************************************
1480 * Get the start time and end time of the trace
1481 ****************************************************************************/
1483 static void ltt_tracefile_time_span_get(LttTracefile
*tf
,
1484 LttTime
*start
, LttTime
*end
)
1488 err
= map_block(tf
, 0);
1490 g_error("Can not map block");
1491 *start
= ltt_time_infinite
;
1493 *start
= tf
->buffer
.begin
.timestamp
;
1495 err
= map_block(tf
, tf
->num_blocks
- 1); /* Last block */
1497 g_error("Can not map block");
1498 *end
= ltt_time_zero
;
1500 *end
= tf
->buffer
.end
.timestamp
;
1503 struct tracefile_time_span_get_args
{
1509 static void group_time_span_get(GQuark name
, gpointer data
, gpointer user_data
)
1511 struct tracefile_time_span_get_args
*args
=
1512 (struct tracefile_time_span_get_args
*)user_data
;
1514 GArray
*group
= (GArray
*)data
;
1520 for(i
=0; i
<group
->len
; i
++) {
1521 tf
= &g_array_index (group
, LttTracefile
, i
);
1522 if(tf
->cpu_online
) {
1523 ltt_tracefile_time_span_get(tf
, &tmp_start
, &tmp_end
);
1524 if(ltt_time_compare(*args
->start
, tmp_start
)>0) *args
->start
= tmp_start
;
1525 if(ltt_time_compare(*args
->end
, tmp_end
)<0) *args
->end
= tmp_end
;
1530 void ltt_trace_time_span_get(LttTrace
*t
, LttTime
*start
, LttTime
*end
)
1532 LttTime min_start
= ltt_time_infinite
;
1533 LttTime max_end
= ltt_time_zero
;
1534 struct tracefile_time_span_get_args args
= { t
, &min_start
, &max_end
};
1536 g_datalist_foreach(&t
->tracefiles
, &group_time_span_get
, &args
);
1538 if(start
!= NULL
) *start
= min_start
;
1539 if(end
!= NULL
) *end
= max_end
;
1544 /*****************************************************************************
1545 *Get the name of a tracefile
1546 ****************************************************************************/
1548 GQuark
ltt_tracefile_name(const LttTracefile
*tf
)
1553 GQuark
ltt_tracefile_long_name(const LttTracefile
*tf
)
1555 return tf
->long_name
;
1560 guint
ltt_tracefile_cpu(LttTracefile
*tf
)
1565 guint
ltt_tracefile_tid(LttTracefile
*tf
)
1570 guint
ltt_tracefile_pgid(LttTracefile
*tf
)
1575 guint64
ltt_tracefile_creation(LttTracefile
*tf
)
1577 return tf
->creation
;
1579 /*****************************************************************************
1580 * Get the number of blocks in the tracefile
1581 ****************************************************************************/
1583 guint
ltt_tracefile_block_number(LttTracefile
*tf
)
1585 return tf
->num_blocks
;
1589 /* Seek to the first event in a tracefile that has a time equal or greater than
1590 * the time passed in parameter.
1592 * If the time parameter is outside the tracefile time span, seek to the first
1593 * event or if after, return ERANGE.
1595 * If the time parameter is before the first event, we have to seek specially to
1598 * If the time is after the end of the trace, return ERANGE.
1600 * Do a binary search to find the right block, then a sequential search in the
1601 * block to find the event.
1603 * In the special case where the time requested fits inside a block that has no
1604 * event corresponding to the requested time, the first event of the next block
1607 * IMPORTANT NOTE : // FIXME everywhere...
1609 * You MUST NOT do a ltt_tracefile_read right after a ltt_tracefile_seek_time :
1610 * you will jump over an event if you do.
1612 * Return value : 0 : no error, the tf->event can be used
1613 * ERANGE : time if after the last event of the trace
1614 * otherwise : this is an error.
1618 int ltt_tracefile_seek_time(LttTracefile
*tf
, LttTime time
)
1622 unsigned int block_num
, high
, low
;
1624 /* seek at the beginning of trace */
1625 err
= map_block(tf
, 0); /* First block */
1627 g_error("Can not map block");
1631 /* If the time is lower or equal the beginning of the trace,
1632 * go to the first event. */
1633 if(ltt_time_compare(time
, tf
->buffer
.begin
.timestamp
) <= 0) {
1634 ret
= ltt_tracefile_read(tf
);
1635 if(ret
== ERANGE
) goto range
;
1636 else if (ret
) goto fail
;
1637 goto found
; /* There is either no event in the trace or the event points
1638 to the first event in the trace */
1641 err
= map_block(tf
, tf
->num_blocks
- 1); /* Last block */
1643 g_error("Can not map block");
1647 /* If the time is after the end of the trace, return ERANGE. */
1648 if(ltt_time_compare(time
, tf
->buffer
.end
.timestamp
) > 0) {
1652 /* Binary search the block */
1653 high
= tf
->num_blocks
- 1;
1657 block_num
= ((high
-low
) / 2) + low
;
1659 err
= map_block(tf
, block_num
);
1661 g_error("Can not map block");
1665 /* We cannot divide anymore : this is what would happen if the time
1666 * requested was exactly between two consecutive buffers'end and start
1667 * timestamps. This is also what would happend if we didn't deal with out
1668 * of span cases prior in this function. */
1669 /* The event is right in the buffer!
1670 * (or in the next buffer first event) */
1672 ret
= ltt_tracefile_read(tf
);
1673 if(ret
== ERANGE
) goto range
; /* ERANGE or EPERM */
1674 else if(ret
) goto fail
;
1676 if(ltt_time_compare(time
, tf
->event
.event_time
) <= 0)
1680 } else if(ltt_time_compare(time
, tf
->buffer
.begin
.timestamp
) < 0) {
1681 /* go to lower part */
1682 high
= block_num
- 1;
1683 } else if(ltt_time_compare(time
, tf
->buffer
.end
.timestamp
) > 0) {
1684 /* go to higher part */
1685 low
= block_num
+ 1;
1686 } else {/* The event is right in the buffer!
1687 (or in the next buffer first event) */
1689 ret
= ltt_tracefile_read(tf
);
1690 if(ret
== ERANGE
) goto range
; /* ERANGE or EPERM */
1691 else if(ret
) goto fail
;
1693 if(ltt_time_compare(time
, tf
->event
.event_time
) <= 0)
1705 /* Error handling */
1707 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1708 g_quark_to_string(tf
->name
));
1713 int ltt_tracefile_seek_position(LttTracefile
*tf
, const LttEventPosition
*ep
) {
1717 if(ep
->tracefile
!= tf
) {
1721 err
= map_block(tf
, ep
->block
);
1723 g_error("Can not map block");
1727 tf
->event
.offset
= ep
->offset
;
1729 /* Put back the event real tsc */
1730 tf
->event
.tsc
= ep
->tsc
;
1731 tf
->buffer
.tsc
= ep
->tsc
;
1733 err
= ltt_tracefile_read_update_event(tf
);
1735 err
= ltt_tracefile_read_op(tf
);
1741 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1742 g_quark_to_string(tf
->name
));
1746 LttTime
ltt_interpolate_time_from_tsc(LttTracefile
*tf
, guint64 tsc
)
1750 if(tsc
> tf
->trace
->start_tsc
) {
1751 time
= ltt_time_from_uint64(
1752 (double)(tsc
- tf
->trace
->start_tsc
)
1753 * (1000000000.0 / tf
->trace
->freq_scale
)
1754 / (double)tf
->trace
->start_freq
);
1755 time
= ltt_time_add(tf
->trace
->start_time_from_tsc
, time
);
1757 time
= ltt_time_from_uint64(
1758 (double)(tf
->trace
->start_tsc
- tsc
)
1759 * (1000000000.0 / tf
->trace
->freq_scale
)
1760 / (double)tf
->trace
->start_freq
);
1761 time
= ltt_time_sub(tf
->trace
->start_time_from_tsc
, time
);
1766 /* Calculate the real event time based on the buffer boundaries */
1767 LttTime
ltt_interpolate_time(LttTracefile
*tf
, LttEvent
*event
)
1769 return ltt_interpolate_time_from_tsc(tf
, tf
->buffer
.tsc
);
1773 /* Get the current event of the tracefile : valid until the next read */
1774 LttEvent
*ltt_tracefile_get_event(LttTracefile
*tf
)
1781 /*****************************************************************************
1783 * ltt_tracefile_read : Read the next event in the tracefile
1788 * Returns 0 if an event can be used in tf->event.
1789 * Returns ERANGE on end of trace. The event in tf->event still can be used
1790 * (if the last block was not empty).
1791 * Returns EPERM on error.
1793 * This function does make the tracefile event structure point to the event
1794 * currently pointed to by the tf->event.
1796 * Note : you must call a ltt_tracefile_seek to the beginning of the trace to
1797 * reinitialize it after an error if you want results to be coherent.
1798 * It would be the case if a end of trace last buffer has no event : the end
1799 * of trace wouldn't be returned, but an error.
1800 * We make the assumption there is at least one event per buffer.
1801 ****************************************************************************/
1803 int ltt_tracefile_read(LttTracefile
*tf
)
1807 err
= ltt_tracefile_read_seek(tf
);
1809 err
= ltt_tracefile_read_update_event(tf
);
1811 err
= ltt_tracefile_read_op(tf
);
1817 int ltt_tracefile_read_seek(LttTracefile
*tf
)
1821 /* Get next buffer until we finally have an event, or end of trace */
1823 err
= ltt_seek_next_event(tf
);
1824 if(unlikely(err
== ENOPROTOOPT
)) {
1828 /* Are we at the end of the buffer ? */
1830 if(unlikely(tf
->buffer
.index
== tf
->num_blocks
-1)){ /* end of trace ? */
1833 /* get next block */
1834 err
= map_block(tf
, tf
->buffer
.index
+ 1);
1836 g_error("Can not map block");
1840 } else break; /* We found an event ! */
1847 /* do specific operation on events */
1848 int ltt_tracefile_read_op(LttTracefile
*tf
)
1854 /* do event specific operation */
1856 /* do something if its an heartbeat event : increment the heartbeat count */
1857 //if(event->facility_id == LTT_FACILITY_CORE)
1858 // if(event->event_id == LTT_EVENT_HEARTBEAT)
1859 // tf->cur_heart_beat_number++;
1865 /* same as ltt_tracefile_read, but does not seek to the next event nor call
1866 * event specific operation. */
1867 int ltt_tracefile_read_update_event(LttTracefile
*tf
)
1873 pos
= tf
->buffer
.head
+ event
->offset
;
1875 /* Read event header */
1877 /* Align the head */
1879 pos
+= ltt_align((size_t)pos
, tf
->trace
->arch_size
, tf
->has_alignment
);
1881 g_assert(tf
->trace
->has_heartbeat
);
1882 pos
+= ltt_align((size_t)pos
, sizeof(uint32_t), tf
->has_alignment
);
1885 if(tf
->trace
->has_heartbeat
) {
1886 event
->timestamp
= ltt_get_uint32(LTT_GET_BO(tf
),
1889 /* 32 bits -> 64 bits tsc */
1890 /* note : still works for seek and non seek cases. */
1891 if(event
->timestamp
< (0xFFFFFFFFULL
&tf
->buffer
.tsc
)) {
1892 tf
->buffer
.tsc
= ((tf
->buffer
.tsc
&0xFFFFFFFF00000000ULL
)
1894 | (guint64
)event
->timestamp
;
1895 event
->tsc
= tf
->buffer
.tsc
;
1898 tf
->buffer
.tsc
= (tf
->buffer
.tsc
&0xFFFFFFFF00000000ULL
)
1899 | (guint64
)event
->timestamp
;
1900 event
->tsc
= tf
->buffer
.tsc
;
1903 /* Compact header */
1904 /* We keep the LSB of the previous timestamp, to make sure
1905 * we never go back */
1906 event
->event_id
= event
->timestamp
>> tf
->tscbits
;
1907 event
->event_size
= 0xFFFF;
1908 printf("Found compact event %d\n", event
->event_id
);
1909 event
->timestamp
= event
->timestamp
<< tf
->tsc_lsb_truncate
;
1910 event
->timestamp
= event
->timestamp
& tf
->tsc_mask
;
1911 printf("timestamp 0x%lX\n", event
->timestamp
);
1912 printf("mask 0x%llX\n", tf
->tsc_mask
);
1913 printf("mask_next 0x%llX\n", tf
->tsc_mask_next_bit
);
1914 printf("previous tsc 0x%llX\n", tf
->buffer
.tsc
);
1915 printf("previous tsc&mask 0x%llX\n", tf
->tsc_mask
&tf
->buffer
.tsc
);
1916 printf("previous tsc&(~mask) 0x%llX\n", tf
->buffer
.tsc
&(~tf
->tsc_mask
));
1917 if(event
->timestamp
< (tf
->tsc_mask
&tf
->buffer
.tsc
)) {
1919 tf
->buffer
.tsc
= ((tf
->buffer
.tsc
&(~tf
->tsc_mask
))
1920 + tf
->tsc_mask_next_bit
)
1921 | (guint64
)event
->timestamp
;
1922 event
->tsc
= tf
->buffer
.tsc
;
1924 printf("no wrap\n");
1926 tf
->buffer
.tsc
= (tf
->buffer
.tsc
&(~tf
->tsc_mask
))
1927 | (guint64
)event
->timestamp
;
1928 event
->tsc
= tf
->buffer
.tsc
;
1930 printf("current tsc 0x%llX\n", tf
->buffer
.tsc
);
1931 g_assert(tf
->trace
->compact_facilities
);
1932 /* FIXME : currently does not support unload/load of compact
1933 * facility during tracing. Should check for the currently loaded
1934 * version of the facility. */
1935 g_assert(tf
->trace
->compact_facilities
->len
== 1);
1936 event
->facility_id
= g_array_index(tf
->trace
->compact_facilities
, guint
, 0);
1938 pos
+= sizeof(guint32
);
1940 event
->tsc
= ltt_get_uint64(LTT_GET_BO(tf
), pos
);
1941 tf
->buffer
.tsc
= event
->tsc
;
1942 pos
+= sizeof(guint64
);
1944 event
->event_time
= ltt_interpolate_time(tf
, event
);
1947 event
->facility_id
= *(guint8
*)pos
;
1948 pos
+= sizeof(guint8
);
1950 event
->event_id
= *(guint8
*)pos
;
1951 pos
+= sizeof(guint8
);
1953 event
->event_size
= ltt_get_uint16(LTT_GET_BO(tf
), pos
);
1954 pos
+= sizeof(guint16
);
1958 /* Align the head */
1960 pos
+= ltt_align((size_t)pos
, tf
->trace
->arch_size
, tf
->has_alignment
);
1964 /* get the data size and update the event fields with the current
1965 * information. Also update the time if a heartbeat_full event is found. */
1966 ltt_update_event_size(tf
);
1972 /****************************************************************************
1974 * map_block : map a block from the file
1976 * lttdes : ltt trace file
1977 * whichBlock : the block which will be read
1980 * EINVAL : lseek fail
1981 * EIO : can not read from the file
1982 ****************************************************************************/
1984 static gint
map_block(LttTracefile
* tf
, guint block_num
)
1986 int page_size
= getpagesize();
1987 struct ltt_block_start_header
*header
;
1989 g_assert(block_num
< tf
->num_blocks
);
1991 if(tf
->buffer
.head
!= NULL
) {
1992 if(munmap(tf
->buffer
.head
, PAGE_ALIGN(tf
->buf_size
))) {
1993 g_warning("unmap size : %u\n",
1994 PAGE_ALIGN(tf
->buf_size
));
1995 perror("munmap error");
2001 /* Multiple of pages aligned head */
2002 tf
->buffer
.head
= mmap(0,
2003 PAGE_ALIGN(tf
->buf_size
),
2004 PROT_READ
, MAP_PRIVATE
, tf
->fd
,
2005 PAGE_ALIGN((off_t
)tf
->buf_size
* (off_t
)block_num
));
2007 if(tf
->buffer
.head
== MAP_FAILED
) {
2008 perror("Error in allocating memory for buffer of tracefile");
2012 g_assert( ( (guint
)tf
->buffer
.head
&(8-1) ) == 0); // make sure it's aligned.
2015 tf
->buffer
.index
= block_num
;
2017 header
= (struct ltt_block_start_header
*)tf
->buffer
.head
;
2020 tf
->buffer
.begin
.timestamp
= ltt_time_add(
2021 ltt_time_from_uint64(
2022 ltt_get_uint64(LTT_GET_BO(tf
),
2023 &header
->begin
.timestamp
)
2024 - tf
->trace
->start_monotonic
),
2025 tf
->trace
->start_time
);
2027 //g_debug("block %u begin : %lu.%lu", block_num,
2028 // tf->buffer.begin.timestamp.tv_sec, tf->buffer.begin.timestamp.tv_nsec);
2029 tf
->buffer
.begin
.cycle_count
= ltt_get_uint64(LTT_GET_BO(tf
),
2030 &header
->begin
.cycle_count
);
2031 tf
->buffer
.begin
.freq
= ltt_get_uint64(LTT_GET_BO(tf
),
2032 &header
->begin
.freq
);
2033 if(tf
->buffer
.begin
.freq
== 0)
2034 tf
->buffer
.begin
.freq
= tf
->trace
->start_freq
;
2036 tf
->buffer
.begin
.timestamp
= ltt_interpolate_time_from_tsc(tf
,
2037 tf
->buffer
.begin
.cycle_count
);
2040 ltt_time_from_uint64(
2041 (double)(tf
->buffer
.begin
.cycle_count
2042 - tf
->trace
->start_tsc
) * 1000000.0
2043 / (double)tf
->trace
->start_freq
),
2044 tf
->trace
->start_time_from_tsc
);
2048 tf
->buffer
.end
.timestamp
= ltt_time_add(
2049 ltt_time_from_uint64(
2050 ltt_get_uint64(LTT_GET_BO(tf
),
2051 &header
->end
.timestamp
)
2052 - tf
->trace
->start_monotonic
),
2053 tf
->trace
->start_time
);
2055 //g_debug("block %u end : %lu.%lu", block_num,
2056 // tf->buffer.end.timestamp.tv_sec, tf->buffer.end.timestamp.tv_nsec);
2057 tf
->buffer
.end
.cycle_count
= ltt_get_uint64(LTT_GET_BO(tf
),
2058 &header
->end
.cycle_count
);
2059 tf
->buffer
.end
.freq
= ltt_get_uint64(LTT_GET_BO(tf
),
2061 if(tf
->buffer
.end
.freq
== 0)
2062 tf
->buffer
.end
.freq
= tf
->trace
->start_freq
;
2064 tf
->buffer
.lost_size
= ltt_get_uint32(LTT_GET_BO(tf
),
2065 &header
->lost_size
);
2066 tf
->buffer
.end
.timestamp
= ltt_interpolate_time_from_tsc(tf
,
2067 tf
->buffer
.end
.cycle_count
);
2070 ltt_time_from_uint64(
2071 (double)(tf
->buffer
.end
.cycle_count
2072 - tf
->trace
->start_tsc
) * 1000000.0
2073 / (double)tf
->trace
->start_freq
),
2074 tf
->trace
->start_time_from_tsc
);
2076 tf
->buffer
.tsc
= tf
->buffer
.begin
.cycle_count
;
2077 tf
->event
.tsc
= tf
->buffer
.tsc
;
2078 tf
->buffer
.freq
= tf
->buffer
.begin
.freq
;
2081 * eventually support variable buffer size : will need a partial pre-read of
2082 * the headers to create an index when we open the trace... eventually. */
2083 g_assert(tf
->buf_size
== ltt_get_uint32(LTT_GET_BO(tf
),
2084 &header
->buf_size
));
2086 /* Now that the buffer is mapped, calculate the time interpolation for the
2089 // tf->buffer.nsecs_per_cycle = calc_nsecs_per_cycle(tf);
2090 //tf->buffer.cyc2ns_scale = calc_nsecs_per_cycle(tf);
2092 /* Make the current event point to the beginning of the buffer :
2093 * it means that the event read must get the first event. */
2094 tf
->event
.tracefile
= tf
;
2095 tf
->event
.block
= block_num
;
2096 tf
->event
.offset
= 0;
2105 /* It will update the fields offsets too */
2106 void ltt_update_event_size(LttTracefile
*tf
)
2109 LttFacility
*f
= ltt_trace_get_facility_by_num(tf
->trace
,
2110 tf
->event
.facility_id
);
2114 /* Specific handling of core events : necessary to read the facility control
2117 if(likely(tf
->event
.facility_id
== LTT_FACILITY_CORE
)) {
2118 switch((enum ltt_core_events
)tf
->event
.event_id
) {
2119 case LTT_EVENT_FACILITY_LOAD
:
2120 size
= strlen((char*)tf
->event
.data
) + 1;
2121 //g_debug("Update Event facility load of facility %s", (char*)tf->event.data);
2122 size
+= ltt_align(size
, sizeof(guint32
), tf
->has_alignment
);
2123 size
+= sizeof(struct LttFacilityLoad
);
2125 case LTT_EVENT_FACILITY_UNLOAD
:
2126 //g_debug("Update Event facility unload");
2127 size
= sizeof(struct LttFacilityUnload
);
2129 case LTT_EVENT_STATE_DUMP_FACILITY_LOAD
:
2130 size
= strlen((char*)tf
->event
.data
) + 1;
2131 size
+= ltt_align(size
, sizeof(guint32
), tf
->has_alignment
);
2132 //g_debug("Update Event facility load state dump of facility %s",
2133 // (char*)tf->event.data);
2134 size
+= sizeof(struct LttStateDumpFacilityLoad
);
2136 case LTT_EVENT_HEARTBEAT
:
2137 //g_debug("Update Event heartbeat");
2138 size
= sizeof(TimeHeartbeat
);
2140 case LTT_EVENT_HEARTBEAT_FULL
:
2141 //g_debug("Update Event heartbeat full");
2142 tscdata
= (char*)(tf
->event
.data
);
2143 tf
->event
.tsc
= ltt_get_uint64(LTT_GET_BO(tf
), tscdata
);
2144 tf
->buffer
.tsc
= tf
->event
.tsc
;
2145 tf
->event
.event_time
= ltt_interpolate_time(tf
, &tf
->event
);
2146 size
= sizeof(TimeHeartbeatFull
);
2147 size
+= ltt_align(size
, sizeof(guint64
), tf
->has_alignment
);
2150 g_warning("Error in getting event size : tracefile %s, "
2151 "unknown event id %hhu in core facility.",
2152 g_quark_to_string(tf
->name
),
2153 tf
->event
.event_id
);
2154 goto event_id_error
;
2157 goto no_offset
; /* Skip the field computation */
2159 g_warning("Unknown facility %hhu (0x%hhx) in tracefile %s",
2160 tf
->event
.facility_id
,
2161 tf
->event
.facility_id
,
2162 g_quark_to_string(tf
->name
));
2163 goto facility_error
;
2167 LttEventType
*event_type
=
2168 ltt_facility_eventtype_get(f
, tf
->event
.event_id
);
2171 g_warning("Unknown event id %hhu in facility %s in tracefile %s",
2173 g_quark_to_string(f
->name
),
2174 g_quark_to_string(tf
->name
));
2175 goto event_type_error
;
2178 /* Compute the dynamic offsets */
2179 compute_offsets(tf
, f
, event_type
, &size
, tf
->event
.data
);
2181 //g_debug("Event root field : f.e %hhu.%hhu size %zd",
2182 // tf->event.facility_id,
2183 // tf->event.event_id, size);
2186 tf
->event
.data_size
= size
;
2188 /* Check consistency between kernel and LTTV structure sizes */
2189 if(tf
->event
.event_size
== 0xFFFF) {
2190 /* Event size too big to fit in the event size field */
2191 tf
->event
.event_size
= tf
->event
.data_size
;
2193 g_assert(tf
->event
.data_size
== tf
->event
.event_size
);
2200 if(tf
->event
.event_size
== 0xFFFF) {
2201 g_error("Cannot jump over an unknown event bigger than 0xFFFE bytes");
2203 /* The facility is unknown : use the kernel information about this event
2204 * to jump over it. */
2205 tf
->event
.data_size
= tf
->event
.event_size
;
2209 /* Take the tf current event offset and use the event facility id and event id
2210 * to figure out where is the next event offset.
2212 * This is an internal function not aiming at being used elsewhere : it will
2213 * not jump over the current block limits. Please consider using
2214 * ltt_tracefile_read to do this.
2216 * Returns 0 on success
2217 * ERANGE if we are at the end of the buffer.
2218 * ENOPROTOOPT if an error occured when getting the current event size.
2220 static int ltt_seek_next_event(LttTracefile
*tf
)
2225 /* seek over the buffer header if we are at the buffer start */
2226 if(tf
->event
.offset
== 0) {
2227 tf
->event
.offset
+= tf
->buffer_header_size
;
2229 if(tf
->event
.offset
== tf
->buf_size
- tf
->buffer
.lost_size
) {
2236 pos
= tf
->event
.data
;
2238 if(tf
->event
.data_size
< 0) goto error
;
2240 pos
+= (size_t)tf
->event
.data_size
;
2242 tf
->event
.offset
= pos
- tf
->buffer
.head
;
2244 if(tf
->event
.offset
== tf
->buf_size
- tf
->buffer
.lost_size
) {
2248 g_assert(tf
->event
.offset
< tf
->buf_size
- tf
->buffer
.lost_size
);
2254 g_error("Error in ltt_seek_next_event for tracefile %s",
2255 g_quark_to_string(tf
->name
));
2260 /*****************************************************************************
2262 * calc_nsecs_per_cycle : calculate nsecs per cycle for current block
2264 * 1.0 / (freq(khz) *1000) * 1000000000
2267 ****************************************************************************/
2268 /* from timer_tsc.c */
2269 #define CYC2NS_SCALE_FACTOR 10
2270 static guint32
calc_nsecs_per_cycle(LttTracefile
* tf
)
2272 //return 1e6 / (double)tf->buffer.freq;
2273 guint32 cpu_mhz
= tf
->buffer
.freq
/ 1000;
2274 guint32 cyc2ns_scale
= (1000 << CYC2NS_SCALE_FACTOR
)/cpu_mhz
;
2276 return cyc2ns_scale
;
2277 // return 1e6 / (double)tf->buffer.freq;
2280 static guint64
cycles_2_ns(LttTracefile
*tf
, guint64 cycles
)
2282 return (cycles
* tf
->buffer
.cyc2ns_scale
) >> CYC2NS_SCALE_FACTOR
;
2287 void setFieldsOffset(LttTracefile
*tf
, LttEventType
*evT
,void *evD
)
2289 LttField
* rootFld
= evT
->root_field
;
2290 // rootFld->base_address = evD;
2293 rootFld
->field_size
= getFieldtypeSize(tf
, evT
->facility
,
2294 evT
, 0,0,rootFld
, evD
);
2298 /*****************************************************************************
2300 * set_fields_offsets : set the precomputable offset of the fields
2302 * tracefile : opened trace file
2303 * event_type : the event type
2304 ****************************************************************************/
2306 void set_fields_offsets(LttTracefile
*tf
, LttEventType
*event_type
)
2308 LttField
*field
= event_type
->root_field
;
2309 enum field_status fixed_root
= FIELD_FIXED
, fixed_parent
= FIELD_FIXED
;
2312 preset_field_type_size(tf
, event_type
, 0, 0,
2313 &fixed_root
, &fixed_parent
,
2320 /*****************************************************************************
2322 * get_alignment : Get the alignment needed for a field.
2326 * returns : The size on which it must be aligned.
2328 ****************************************************************************/
2329 off_t
get_alignment(LttField
*field
)
2331 LttType
*type
= &field
->field_type
;
2333 switch(type
->type_class
) {
2335 case LTT_UINT_FIXED
:
2350 /* Align offset on type size */
2351 g_assert(field
->field_size
!= 0);
2352 return field
->field_size
;
2358 g_assert(type
->fields
->len
== 1);
2360 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
2361 return get_alignment(child
);
2365 g_assert(type
->fields
->len
== 2);
2368 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
2370 localign
= max(localign
, get_alignment(child
));
2372 child
= &g_array_index(type
->fields
, LttField
, 1);
2373 localign
= max(localign
, get_alignment(child
));
2384 for(i
=0; i
<type
->fields
->len
; i
++) {
2385 LttField
*child
= &g_array_index(type
->fields
, LttField
, i
);
2386 localign
= max(localign
, get_alignment(child
));
2393 g_error("get_alignment : unknown type");
2398 /*****************************************************************************
2400 * field_compute_static_size : Determine the size of fields known by their
2401 * sole definition. Unions, arrays and struct sizes might be known, but
2402 * the parser does not give that information.
2407 ****************************************************************************/
2409 void field_compute_static_size(LttFacility
*fac
, LttField
*field
)
2411 LttType
*type
= &field
->field_type
;
2413 switch(type
->type_class
) {
2415 case LTT_UINT_FIXED
:
2434 /* note this : array type size is the number of elements in the array,
2435 * while array field size of the length of the array in bytes */
2436 g_assert(type
->fields
->len
== 1);
2438 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
2439 field_compute_static_size(fac
, child
);
2441 if(child
->field_size
!= 0) {
2442 field
->field_size
= type
->size
* child
->field_size
;
2443 field
->dynamic_offsets
= g_array_sized_new(FALSE
, TRUE
,
2444 sizeof(off_t
), type
->size
);
2446 field
->field_size
= 0;
2451 g_assert(type
->fields
->len
== 2);
2453 off_t local_offset
= 0;
2454 LttField
*child
= &g_array_index(type
->fields
, LttField
, 1);
2455 field_compute_static_size(fac
, child
);
2456 field
->field_size
= 0;
2458 if(child
->field_size
!= 0) {
2459 field
->dynamic_offsets
= g_array_sized_new(FALSE
, TRUE
,
2460 sizeof(off_t
), SEQUENCE_AVG_ELEMENTS
);
2468 for(i
=0;i
<type
->fields
->len
;i
++) {
2469 LttField
*child
= &g_array_index(type
->fields
, LttField
, i
);
2470 field_compute_static_size(fac
, child
);
2471 if(child
->field_size
!= 0) {
2472 type
->size
+= ltt_align(type
->size
, get_alignment(child
),
2474 type
->size
+= child
->field_size
;
2476 /* As soon as we find a child with variable size, we have
2477 * a variable size */
2482 field
->field_size
= type
->size
;
2486 g_error("field_static_size : unknown type");
2493 /*****************************************************************************
2495 * precompute_fields_offsets : set the precomputable offset of the fields
2499 * offset : pointer to the current offset, must be incremented
2501 * return : 1 : found a variable length field, stop the processing.
2503 ****************************************************************************/
2506 gint
precompute_fields_offsets(LttFacility
*fac
, LttField
*field
, off_t
*offset
)
2508 LttType
*type
= &field
->field_type
;
2510 switch(type
->type_class
) {
2512 case LTT_UINT_FIXED
:
2527 g_assert(field
->field_size
!= 0);
2528 /* Align offset on type size */
2529 *offset
+= ltt_align(*offset
, get_alignment(field
),
2531 /* remember offset */
2532 field
->offset_root
= *offset
;
2533 field
->fixed_root
= FIELD_FIXED
;
2534 /* Increment offset */
2535 *offset
+= field
->field_size
;
2539 field
->offset_root
= *offset
;
2540 field
->fixed_root
= FIELD_FIXED
;
2544 g_assert(type
->fields
->len
== 1);
2546 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
2548 *offset
+= ltt_align(*offset
, get_alignment(field
),
2551 /* remember offset */
2552 field
->offset_root
= *offset
;
2553 field
->array_offset
= *offset
;
2554 field
->fixed_root
= FIELD_FIXED
;
2556 /* Let the child be variable */
2557 //precompute_fields_offsets(tf, child, offset);
2559 if(field
->field_size
!= 0) {
2560 /* Increment offset */
2561 /* field_size is the array size in bytes */
2562 *offset
+= field
->field_size
;
2570 g_assert(type
->fields
->len
== 2);
2575 *offset
+= ltt_align(*offset
, get_alignment(field
),
2578 /* remember offset */
2579 field
->offset_root
= *offset
;
2580 field
->fixed_root
= FIELD_FIXED
;
2582 child
= &g_array_index(type
->fields
, LttField
, 0);
2583 ret
= precompute_fields_offsets(fac
, child
, offset
);
2584 g_assert(ret
== 0); /* Seq len cannot have variable len */
2586 child
= &g_array_index(type
->fields
, LttField
, 1);
2587 *offset
+= ltt_align(*offset
, get_alignment(child
),
2589 field
->array_offset
= *offset
;
2590 /* Let the child be variable. */
2591 //ret = precompute_fields_offsets(fac, child, offset);
2593 /* Cannot precompute fields offsets of sequence members, and has
2594 * variable length. */
2604 *offset
+= ltt_align(*offset
, get_alignment(field
),
2606 /* remember offset */
2607 field
->offset_root
= *offset
;
2608 field
->fixed_root
= FIELD_FIXED
;
2610 for(i
=0; i
< type
->fields
->len
; i
++) {
2611 child
= &g_array_index(type
->fields
, LttField
, i
);
2612 ret
= precompute_fields_offsets(fac
, child
, offset
);
2625 *offset
+= ltt_align(*offset
, get_alignment(field
),
2627 /* remember offset */
2628 field
->offset_root
= *offset
;
2629 field
->fixed_root
= FIELD_FIXED
;
2631 for(i
=0; i
< type
->fields
->len
; i
++) {
2632 *offset
= field
->offset_root
;
2633 child
= &g_array_index(type
->fields
, LttField
, i
);
2634 ret
= precompute_fields_offsets(fac
, child
, offset
);
2638 *offset
= field
->offset_root
+ field
->field_size
;
2645 g_error("precompute_fields_offsets : unknown type");
2652 /*****************************************************************************
2654 * precompute_offsets : set the precomputable offset of an event type
2657 * event : event type
2659 ****************************************************************************/
2660 void precompute_offsets(LttFacility
*fac
, LttEventType
*event
)
2666 /* First, compute the size of fixed size fields. Will determine size for
2667 * arrays, struct and unions, which is not done by the parser */
2668 for(i
=0; i
<event
->fields
->len
; i
++) {
2669 LttField
*field
= &g_array_index(event
->fields
, LttField
, i
);
2670 field_compute_static_size(fac
, field
);
2673 /* Precompute all known offsets */
2674 for(i
=0; i
<event
->fields
->len
; i
++) {
2675 LttField
*field
= &g_array_index(event
->fields
, LttField
, i
);
2676 ret
= precompute_fields_offsets(fac
, field
, &offset
);
2684 /*****************************************************************************
2686 * preset_field_type_size : set the fixed sizes of the field type
2689 * event_type : event type
2690 * offset_root : offset from the root
2691 * offset_parent : offset from the parent
2692 * fixed_root : Do we know a fixed offset to the root ?
2693 * fixed_parent : Do we know a fixed offset to the parent ?
2695 ****************************************************************************/
2699 // preset the fixed size offsets. Calculate them just like genevent-new : an
2700 // increment of a *to value that represents the offset from the start of the
2702 // The preset information is : offsets up to (and including) the first element
2703 // of variable size. All subsequent fields must be flagged "VARIABLE OFFSET".
2705 void preset_field_type_size(LttTracefile
*tf
, LttEventType
*event_type
,
2706 off_t offset_root
, off_t offset_parent
,
2707 enum field_status
*fixed_root
, enum field_status
*fixed_parent
,
2710 enum field_status local_fixed_root
, local_fixed_parent
;
2714 g_assert(field
->fixed_root
== FIELD_UNKNOWN
);
2715 g_assert(field
->fixed_parent
== FIELD_UNKNOWN
);
2716 g_assert(field
->fixed_size
== FIELD_UNKNOWN
);
2718 type
= field
->field_type
;
2720 field
->fixed_root
= *fixed_root
;
2721 if(field
->fixed_root
== FIELD_FIXED
)
2722 field
->offset_root
= offset_root
;
2724 field
->offset_root
= 0;
2726 field
->fixed_parent
= *fixed_parent
;
2727 if(field
->fixed_parent
== FIELD_FIXED
)
2728 field
->offset_parent
= offset_parent
;
2730 field
->offset_parent
= 0;
2732 size_t current_root_offset
;
2733 size_t current_offset
;
2734 enum field_status current_child_status
, final_child_status
;
2737 switch(type
->type_class
) {
2739 case LTT_UINT_FIXED
:
2748 field
->field_size
= ltt_type_size(tf
->trace
, type
);
2749 field
->fixed_size
= FIELD_FIXED
;
2752 field
->field_size
= (off_t
)event_type
->facility
->pointer_size
;
2753 field
->fixed_size
= FIELD_FIXED
;
2757 field
->field_size
= (off_t
)event_type
->facility
->long_size
;
2758 field
->fixed_size
= FIELD_FIXED
;
2763 field
->field_size
= (off_t
)event_type
->facility
->size_t_size
;
2764 field
->fixed_size
= FIELD_FIXED
;
2767 local_fixed_root
= FIELD_VARIABLE
;
2768 local_fixed_parent
= FIELD_VARIABLE
;
2769 preset_field_type_size(tf
, event_type
,
2771 &local_fixed_root
, &local_fixed_parent
,
2773 field
->fixed_size
= FIELD_VARIABLE
;
2774 field
->field_size
= 0;
2775 *fixed_root
= FIELD_VARIABLE
;
2776 *fixed_parent
= FIELD_VARIABLE
;
2779 field
->fixed_size
= FIELD_VARIABLE
;
2780 field
->field_size
= 0;
2781 *fixed_root
= FIELD_VARIABLE
;
2782 *fixed_parent
= FIELD_VARIABLE
;
2785 local_fixed_root
= FIELD_VARIABLE
;
2786 local_fixed_parent
= FIELD_VARIABLE
;
2787 preset_field_type_size(tf
, event_type
,
2789 &local_fixed_root
, &local_fixed_parent
,
2791 field
->fixed_size
= field
->child
[0]->fixed_size
;
2792 if(field
->fixed_size
== FIELD_FIXED
) {
2793 field
->field_size
= type
->element_number
* field
->child
[0]->field_size
;
2795 field
->field_size
= 0;
2796 *fixed_root
= FIELD_VARIABLE
;
2797 *fixed_parent
= FIELD_VARIABLE
;
2801 current_root_offset
= field
->offset_root
;
2803 current_child_status
= FIELD_FIXED
;
2804 for(i
=0;i
<type
->element_number
;i
++) {
2805 preset_field_type_size(tf
, event_type
,
2806 current_root_offset
, current_offset
,
2807 fixed_root
, ¤t_child_status
,
2809 if(current_child_status
== FIELD_FIXED
) {
2810 current_root_offset
+= field
->child
[i
]->field_size
;
2811 current_offset
+= field
->child
[i
]->field_size
;
2813 current_root_offset
= 0;
2817 if(current_child_status
!= FIELD_FIXED
) {
2818 *fixed_parent
= current_child_status
;
2819 field
->field_size
= 0;
2820 field
->fixed_size
= current_child_status
;
2822 field
->field_size
= current_offset
;
2823 field
->fixed_size
= FIELD_FIXED
;
2827 current_root_offset
= field
->offset_root
;
2830 final_child_status
= FIELD_FIXED
;
2831 for(i
=0;i
<type
->element_number
;i
++) {
2832 enum field_status current_root_child_status
= FIELD_FIXED
;
2833 enum field_status current_child_status
= FIELD_FIXED
;
2834 preset_field_type_size(tf
, event_type
,
2835 current_root_offset
, current_offset
,
2836 ¤t_root_child_status
, ¤t_child_status
,
2838 if(current_child_status
!= FIELD_FIXED
)
2839 final_child_status
= current_child_status
;
2841 max_size
= max(max_size
, field
->child
[i
]->field_size
);
2843 if(final_child_status
!= FIELD_FIXED
) {
2844 g_error("LTTV does not support variable size fields in unions.");
2845 /* This will stop the application. */
2846 *fixed_root
= final_child_status
;
2847 *fixed_parent
= final_child_status
;
2848 field
->field_size
= 0;
2849 field
->fixed_size
= current_child_status
;
2851 field
->field_size
= max_size
;
2852 field
->fixed_size
= FIELD_FIXED
;
2856 g_error("unexpected type NONE");
2863 /*****************************************************************************
2865 * check_fields_compatibility : Check for compatibility between two fields :
2866 * do they use the same inner structure ?
2868 * event_type1 : event type
2869 * event_type2 : event type
2872 *Returns : 0 if identical
2874 ****************************************************************************/
2875 // this function checks for equality of field types. Therefore, it does not use
2876 // per se offsets. For instance, an aligned version of a structure is
2877 // compatible with an unaligned version of the same structure.
2878 gint
check_fields_compatibility(LttEventType
*event_type1
,
2879 LttEventType
*event_type2
,
2880 LttField
*field1
, LttField
*field2
)
2882 guint different
= 0;
2886 if(field1
== NULL
) {
2887 if(field2
== NULL
) goto end
;
2892 } else if(field2
== NULL
) {
2897 type1
= &field1
->field_type
;
2898 type2
= &field2
->field_type
;
2900 if(type1
->type_class
!= type2
->type_class
) {
2904 if(type1
->network
!= type2
->network
) {
2909 switch(type1
->type_class
) {
2911 case LTT_UINT_FIXED
:
2926 if(field1
->field_size
!= field2
->field_size
)
2933 LttField
*child1
= &g_array_index(type1
->fields
, LttField
, 0);
2934 LttField
*child2
= &g_array_index(type2
->fields
, LttField
, 0);
2936 if(type1
->size
!= type2
->size
)
2938 if(check_fields_compatibility(event_type1
, event_type2
, child1
, child2
))
2944 LttField
*child1
= &g_array_index(type1
->fields
, LttField
, 1);
2945 LttField
*child2
= &g_array_index(type2
->fields
, LttField
, 1);
2947 if(check_fields_compatibility(event_type1
, event_type2
, child1
, child2
))
2957 if(type1
->fields
->len
!= type2
->fields
->len
) {
2962 for(i
=0; i
< type1
->fields
->len
; i
++) {
2965 child1
= &g_array_index(type1
->fields
, LttField
, i
);
2966 child2
= &g_array_index(type2
->fields
, LttField
, i
);
2967 different
= check_fields_compatibility(event_type1
,
2968 event_type2
, child1
, child2
);
2970 if(different
) break;
2976 g_error("precompute_fields_offsets : unknown type");
2985 gint
check_fields_compatibility(LttEventType
*event_type1
,
2986 LttEventType
*event_type2
,
2987 LttField
*field1
, LttField
*field2
)
2989 guint different
= 0;
2994 if(field1
== NULL
) {
2995 if(field2
== NULL
) goto end
;
3000 } else if(field2
== NULL
) {
3005 g_assert(field1
->fixed_root
!= FIELD_UNKNOWN
);
3006 g_assert(field2
->fixed_root
!= FIELD_UNKNOWN
);
3007 g_assert(field1
->fixed_parent
!= FIELD_UNKNOWN
);
3008 g_assert(field2
->fixed_parent
!= FIELD_UNKNOWN
);
3009 g_assert(field1
->fixed_size
!= FIELD_UNKNOWN
);
3010 g_assert(field2
->fixed_size
!= FIELD_UNKNOWN
);
3012 type1
= field1
->field_type
;
3013 type2
= field2
->field_type
;
3015 if(type1
->type_class
!= type2
->type_class
) {
3019 if(type1
->element_name
!= type2
->element_name
) {
3024 switch(type1
->type_class
) {
3026 case LTT_UINT_FIXED
:
3041 if(field1
->field_size
!= field2
->field_size
) {
3047 if(type1
->element_number
!= type2
->element_number
) {
3051 for(i
=0;i
<type1
->element_number
;i
++) {
3052 if(type1
->enum_strings
[i
] != type2
->enum_strings
[i
]) {
3059 /* Two elements : size and child */
3060 g_assert(type1
->element_number
!= type2
->element_number
);
3061 for(i
=0;i
<type1
->element_number
;i
++) {
3062 if(check_fields_compatibility(event_type1
, event_type2
,
3063 field1
->child
[0], field2
->child
[0])) {
3072 if(field1
->field_size
!= field2
->field_size
) {
3076 /* Two elements : size and child */
3077 g_assert(type1
->element_number
!= type2
->element_number
);
3078 for(i
=0;i
<type1
->element_number
;i
++) {
3079 if(check_fields_compatibility(event_type1
, event_type2
,
3080 field1
->child
[0], field2
->child
[0])) {
3088 if(type1
->element_number
!= type2
->element_number
) {
3092 for(i
=0;i
<type1
->element_number
;i
++) {
3093 if(check_fields_compatibility(event_type1
, event_type2
,
3094 field1
->child
[0], field2
->child
[0])) {
3107 /*****************************************************************************
3109 * ltt_get_int : get an integer number
3111 * reverse_byte_order: must we reverse the byte order ?
3112 * size : the size of the integer
3113 * ptr : the data pointer
3115 * gint64 : a 64 bits integer
3116 ****************************************************************************/
3118 gint64
ltt_get_int(gboolean reverse_byte_order
, gint size
, void *data
)
3123 case 1: val
= *((gint8
*)data
); break;
3124 case 2: val
= ltt_get_int16(reverse_byte_order
, data
); break;
3125 case 4: val
= ltt_get_int32(reverse_byte_order
, data
); break;
3126 case 8: val
= ltt_get_int64(reverse_byte_order
, data
); break;
3127 default: val
= ltt_get_int64(reverse_byte_order
, data
);
3128 g_critical("get_int : integer size %d unknown", size
);
3135 /*****************************************************************************
3137 * ltt_get_uint : get an unsigned integer number
3139 * reverse_byte_order: must we reverse the byte order ?
3140 * size : the size of the integer
3141 * ptr : the data pointer
3143 * guint64 : a 64 bits unsigned integer
3144 ****************************************************************************/
3146 guint64
ltt_get_uint(gboolean reverse_byte_order
, gint size
, void *data
)
3151 case 1: val
= *((gint8
*)data
); break;
3152 case 2: val
= ltt_get_uint16(reverse_byte_order
, data
); break;
3153 case 4: val
= ltt_get_uint32(reverse_byte_order
, data
); break;
3154 case 8: val
= ltt_get_uint64(reverse_byte_order
, data
); break;
3155 default: val
= ltt_get_uint64(reverse_byte_order
, data
);
3156 g_critical("get_uint : unsigned integer size %d unknown",
3165 /* get the node name of the system */
3167 char * ltt_trace_system_description_node_name (LttSystemDescription
* s
)
3169 return s
->node_name
;
3173 /* get the domain name of the system */
3175 char * ltt_trace_system_description_domain_name (LttSystemDescription
* s
)
3177 return s
->domain_name
;
3181 /* get the description of the system */
3183 char * ltt_trace_system_description_description (LttSystemDescription
* s
)
3185 return s
->description
;
3189 /* get the NTP corrected start time of the trace */
3190 LttTime
ltt_trace_start_time(LttTrace
*t
)
3192 return t
->start_time
;
3195 /* get the monotonic start time of the trace */
3196 LttTime
ltt_trace_start_time_monotonic(LttTrace
*t
)
3198 return t
->start_time_from_tsc
;
3201 LttTracefile
*ltt_tracefile_new()
3203 return g_new(LttTracefile
, 1);
3206 void ltt_tracefile_destroy(LttTracefile
*tf
)
3211 void ltt_tracefile_copy(LttTracefile
*dest
, const LttTracefile
*src
)
3216 /* Before library loading... */
3218 static void __attribute__((constructor
)) init(void)
3220 LTT_FACILITY_NAME_HEARTBEAT
= g_quark_from_string("heartbeat");
3221 LTT_EVENT_NAME_HEARTBEAT
= g_quark_from_string("heartbeat");
3222 LTT_EVENT_NAME_HEARTBEAT_FULL
= g_quark_from_string("heartbeat_full");
3224 LTT_TRACEFILE_NAME_FACILITIES
= g_quark_from_string("/control/facilities");