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>
46 #include "ltt-private.h"
47 #include <ltt/trace.h>
48 #include <ltt/event.h>
49 //#include <ltt/type.h>
50 #include <ltt/ltt-types.h>
51 #include <ltt/marker.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);
109 static int open_tracefiles(LttTrace
*trace
, gchar
*root_path
,
110 gchar
*relative_path
);
111 static int ltt_process_facility_tracefile(LttTracefile
*tf
);
112 static void ltt_tracefile_time_span_get(LttTracefile
*tf
,
113 LttTime
*start
, LttTime
*end
);
114 static void group_time_span_get(GQuark name
, gpointer data
, gpointer user_data
);
115 static gint
map_block(LttTracefile
* tf
, guint block_num
);
116 static int ltt_seek_next_event(LttTracefile
*tf
);
117 static void __attribute__((constructor
)) init(void);
118 static void ltt_update_event_size(LttTracefile
*tf
);
119 //void precompute_offsets(LttFacility *fac, LttEventType *event);
122 /* Functions to parse system.xml file (using glib xml parser) */
123 static void parser_start_element (GMarkupParseContext __UNUSED__
*context
,
124 const gchar
*element_name
,
125 const gchar
**attribute_names
,
126 const gchar
**attribute_values
,
131 LttSystemDescription
* des
= (LttSystemDescription
* )user_data
;
132 if(strcmp("system", element_name
)){
133 *error
= g_error_new(G_MARKUP_ERROR
,
135 "This is not system.xml file");
139 while(attribute_names
[i
]){
140 if(strcmp("node_name", attribute_names
[i
])==0){
141 des
->node_name
= g_strdup(attribute_values
[i
]);
142 }else if(strcmp("domainname", attribute_names
[i
])==0){
143 des
->domain_name
= g_strdup(attribute_values
[i
]);
144 }else if(strcmp("cpu", attribute_names
[i
])==0){
145 des
->nb_cpu
= atoi(attribute_values
[i
]);
146 }else if(strcmp("arch_size", attribute_names
[i
])==0){
147 if(strcmp(attribute_values
[i
],"LP32") == 0) des
->size
= LTT_LP32
;
148 else if(strcmp(attribute_values
[i
],"ILP32") == 0) des
->size
= LTT_ILP32
;
149 else if(strcmp(attribute_values
[i
],"LP64") == 0) des
->size
= LTT_LP64
;
150 else if(strcmp(attribute_values
[i
],"ILP64") == 0) des
->size
= LTT_ILP64
;
151 else if(strcmp(attribute_values
[i
],"UNKNOWN") == 0) des
->size
= LTT_UNKNOWN
;
152 }else if(strcmp("endian", attribute_names
[i
])==0){
153 if(strcmp(attribute_values
[i
],"LITTLE_ENDIAN") == 0)
154 des
->endian
= LTT_LITTLE_ENDIAN
;
155 else if(strcmp(attribute_values
[i
],"BIG_ENDIAN") == 0)
156 des
->endian
= LTT_BIG_ENDIAN
;
157 }else if(strcmp("kernel_name", attribute_names
[i
])==0){
158 des
->kernel_name
= g_strdup(attribute_values
[i
]);
159 }else if(strcmp("kernel_release", attribute_names
[i
])==0){
160 des
->kernel_release
= g_strdup(attribute_values
[i
]);
161 }else if(strcmp("kernel_version", attribute_names
[i
])==0){
162 des
->kernel_version
= g_strdup(attribute_values
[i
]);
163 }else if(strcmp("machine", attribute_names
[i
])==0){
164 des
->machine
= g_strdup(attribute_values
[i
]);
165 }else if(strcmp("processor", attribute_names
[i
])==0){
166 des
->processor
= g_strdup(attribute_values
[i
]);
167 }else if(strcmp("hardware_platform", attribute_names
[i
])==0){
168 des
->hardware_platform
= g_strdup(attribute_values
[i
]);
169 }else if(strcmp("operating_system", attribute_names
[i
])==0){
170 des
->operating_system
= g_strdup(attribute_values
[i
]);
171 }else if(strcmp("ltt_major_version", attribute_names
[i
])==0){
172 des
->ltt_major_version
= atoi(attribute_values
[i
]);
173 }else if(strcmp("ltt_minor_version", attribute_names
[i
])==0){
174 des
->ltt_minor_version
= atoi(attribute_values
[i
]);
175 }else if(strcmp("ltt_block_size", attribute_names
[i
])==0){
176 des
->ltt_block_size
= atoi(attribute_values
[i
]);
178 *error
= g_error_new(G_MARKUP_ERROR
,
180 "Not a valid attribute");
187 static void parser_characters (GMarkupParseContext __UNUSED__
*context
,
189 gsize __UNUSED__ text_len
,
191 GError __UNUSED__
**error
)
193 LttSystemDescription
* des
= (LttSystemDescription
* )user_data
;
194 des
->description
= g_strdup(text
);
199 LttFacility
*ltt_trace_get_facility_by_num(LttTrace
*t
,
202 g_assert(num
< t
->facilities_by_num
->len
);
204 return &g_array_index(t
->facilities_by_num
, LttFacility
, num
);
209 guint
ltt_trace_get_num_cpu(LttTrace
*t
)
217 * Return value : 0 success, 1 bad tracefile
219 int parse_trace_header(void *header
, LttTracefile
*tf
, LttTrace
*t
)
221 guint32
*magic_number
= (guint32
*)header
;
222 struct ltt_trace_header_any
*any
= (struct ltt_trace_header_any
*)header
;
224 if(*magic_number
== LTT_MAGIC_NUMBER
)
226 else if(*magic_number
== LTT_REV_MAGIC_NUMBER
)
228 else /* invalid magic number, bad tracefile ! */
231 /* Get float byte order : might be different from int byte order
232 * (or is set to 0 if the trace has no float (kernel trace)) */
233 tf
->float_word_order
= any
->float_word_order
;
234 tf
->has_alignment
= any
->has_alignment
;
235 tf
->has_heartbeat
= any
->has_heartbeat
;
238 t
->arch_type
= ltt_get_uint32(LTT_GET_BO(tf
),
240 t
->arch_variant
= ltt_get_uint32(LTT_GET_BO(tf
),
242 t
->arch_size
= any
->arch_size
;
243 t
->ltt_major_version
= any
->major_version
;
244 t
->ltt_minor_version
= any
->minor_version
;
245 t
->flight_recorder
= any
->flight_recorder
;
246 // t->compact_facilities = NULL;
250 switch(any
->major_version
) {
253 g_warning("Unsupported trace version : %hhu.%hhu",
254 any
->major_version
, any
->minor_version
);
258 switch(any
->minor_version
) {
261 struct ltt_trace_header_1_0
*vheader
=
262 (struct ltt_trace_header_1_0
*)header
;
263 tf
->buffer_header_size
=
264 sizeof(struct ltt_block_start_header
)
265 + sizeof(struct ltt_trace_header_1_0
);
266 tf
->tsc_lsb_truncate
= vheader
->tsc_lsb_truncate
;
267 tf
->tscbits
= vheader
->tscbits
;
268 tf
->tsc_msb_cutoff
= 32 - tf
->tsc_lsb_truncate
- tf
->tscbits
;
269 tf
->tsc_mask
= ((1ULL << (tf
->tscbits
))-1);
270 tf
->tsc_mask
= tf
->tsc_mask
<< tf
->tsc_lsb_truncate
;
271 tf
->tsc_mask_next_bit
= (1ULL<<(tf
->tscbits
));
272 tf
->tsc_mask_next_bit
= tf
->tsc_mask_next_bit
<< tf
->tsc_lsb_truncate
;
274 t
->start_freq
= ltt_get_uint64(LTT_GET_BO(tf
),
275 &vheader
->start_freq
);
276 t
->freq_scale
= ltt_get_uint32(LTT_GET_BO(tf
),
277 &vheader
->freq_scale
);
278 t
->start_tsc
= ltt_get_uint64(LTT_GET_BO(tf
),
279 &vheader
->start_tsc
);
280 t
->start_monotonic
= ltt_get_uint64(LTT_GET_BO(tf
),
281 &vheader
->start_monotonic
);
282 t
->start_time
.tv_sec
= ltt_get_uint64(LTT_GET_BO(tf
),
283 &vheader
->start_time_sec
);
284 t
->start_time
.tv_nsec
= ltt_get_uint64(LTT_GET_BO(tf
),
285 &vheader
->start_time_usec
);
286 t
->start_time
.tv_nsec
*= 1000; /* microsec to nanosec */
288 t
->start_time_from_tsc
= ltt_time_from_uint64(
290 * (1000000000.0 / tf
->trace
->freq_scale
)
291 / (double)t
->start_freq
);
292 t
->compact_event_bits
= 0;
297 g_warning("Unsupported trace version : %hhu.%hhu",
298 any
->major_version
, any
->minor_version
);
303 g_warning("Unsupported trace version : %hhu.%hhu",
304 any
->major_version
, any
->minor_version
);
314 /*****************************************************************************
316 * ltt_tracefile_open : open a trace file, construct a LttTracefile
318 * t : the trace containing the tracefile
319 * fileName : path name of the trace file
320 * tf : the tracefile structure
322 * : 0 for success, -1 otherwise.
323 ****************************************************************************/
325 gint
ltt_tracefile_open(LttTrace
*t
, gchar
* fileName
, LttTracefile
*tf
)
327 struct stat lTDFStat
; /* Trace data file status */
328 struct ltt_block_start_header
*header
;
329 int page_size
= getpagesize();
332 tf
->long_name
= g_quark_from_string(fileName
);
334 tf
->fd
= open(fileName
, O_RDONLY
);
336 g_warning("Unable to open input data file %s\n", fileName
);
340 // Get the file's status
341 if(fstat(tf
->fd
, &lTDFStat
) < 0){
342 g_warning("Unable to get the status of the input data file %s\n", fileName
);
346 // Is the file large enough to contain a trace
347 if(lTDFStat
.st_size
<
348 (off_t
)(sizeof(struct ltt_block_start_header
)
349 + sizeof(struct ltt_trace_header_any
))){
350 g_print("The input data file %s does not contain a trace\n", fileName
);
354 /* Temporarily map the buffer start header to get trace information */
355 /* Multiple of pages aligned head */
356 tf
->buffer
.head
= mmap(0,
357 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)
358 + sizeof(struct ltt_trace_header_any
)), PROT_READ
,
359 MAP_PRIVATE
, tf
->fd
, 0);
360 if(tf
->buffer
.head
== MAP_FAILED
) {
361 perror("Error in allocating memory for buffer of tracefile");
364 g_assert( ( (guint
)tf
->buffer
.head
&(8-1) ) == 0); // make sure it's aligned.
366 header
= (struct ltt_block_start_header
*)tf
->buffer
.head
;
368 if(parse_trace_header(header
->trace
, tf
, NULL
)) {
369 g_warning("parse_trace_header error");
373 //store the size of the file
374 tf
->file_size
= lTDFStat
.st_size
;
375 tf
->buf_size
= ltt_get_uint32(LTT_GET_BO(tf
), &header
->buf_size
);
376 tf
->num_blocks
= tf
->file_size
/ tf
->buf_size
;
378 if(munmap(tf
->buffer
.head
,
379 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)
380 + sizeof(struct ltt_trace_header_any
)))) {
381 g_warning("unmap size : %u\n",
382 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)
383 + sizeof(struct ltt_trace_header_any
)));
384 perror("munmap error");
387 tf
->buffer
.head
= NULL
;
389 //read the first block
390 if(map_block(tf
,0)) {
391 perror("Cannot map block for tracefile");
399 if(munmap(tf
->buffer
.head
,
400 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)
401 + sizeof(struct ltt_trace_header_any
)))) {
402 g_warning("unmap size : %u\n",
403 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)
404 + sizeof(struct ltt_trace_header_any
)));
405 perror("munmap error");
414 LttTrace
*ltt_tracefile_get_trace(LttTracefile
*tf
)
420 /*****************************************************************************
421 *Open control and per cpu tracefiles
422 ****************************************************************************/
424 void ltt_tracefile_open_cpu(LttTrace
*t
, gchar
* tracefile_name
)
427 tf
= ltt_tracefile_open(t
,tracefile_name
);
429 t
->per_cpu_tracefile_number
++;
430 g_ptr_array_add(t
->per_cpu_tracefiles
, tf
);
433 gint
ltt_tracefile_open_control(LttTrace
*t
, gchar
* control_name
)
442 tf
= ltt_tracefile_open(t
,control_name
);
444 g_warning("ltt_tracefile_open_control : bad file descriptor");
447 t
->control_tracefile_number
++;
448 g_ptr_array_add(t
->control_tracefiles
,tf
);
450 //parse facilities tracefile to get base_id
451 if(strcmp(&control_name
[strlen(control_name
)-10],"facilities") ==0){
453 if(!ltt_tracefile_read(tf
,&ev
)) return 0; // end of file
455 if(ev
.event_id
== TRACE_FACILITY_LOAD
){
457 fLoad
.name
= (gchar
*)pos
;
458 fLoad
.checksum
= *(LttChecksum
*)(pos
+ strlen(fLoad
.name
));
459 fLoad
.base_code
= *(guint32
*)(pos
+ strlen(fLoad
.name
) + sizeof(LttChecksum
));
461 for(i
=0;i
<t
->facility_number
;i
++){
462 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
,i
);
463 if(strcmp(f
->name
,fLoad
.name
)==0 && fLoad
.checksum
==f
->checksum
){
464 f
->base_id
= fLoad
.base_code
;
468 if(i
==t
->facility_number
) {
469 g_warning("Facility: %s, checksum: %u is not found",
470 fLoad
.name
,(unsigned int)fLoad
.checksum
);
473 }else if(ev
.event_id
== TRACE_BLOCK_START
){
475 }else if(ev
.event_id
== TRACE_BLOCK_END
){
478 g_warning("Not valid facilities trace file");
487 /*****************************************************************************
489 * ltt_tracefile_close: close a trace file,
491 * t : tracefile which will be closed
492 ****************************************************************************/
494 void ltt_tracefile_close(LttTracefile
*t
)
496 int page_size
= getpagesize();
498 if(t
->buffer
.head
!= NULL
)
499 if(munmap(t
->buffer
.head
, PAGE_ALIGN(t
->buf_size
))) {
500 g_warning("unmap size : %u\n",
501 PAGE_ALIGN(t
->buf_size
));
502 perror("munmap error");
510 /*****************************************************************************
511 *Get system information
512 ****************************************************************************/
514 gint
getSystemInfo(LttSystemDescription
* des
, gchar
* pathname
)
521 GMarkupParseContext
* context
;
522 GError
* error
= NULL
;
523 GMarkupParser markup_parser
=
525 parser_start_element
,
528 NULL
, /* passthrough */
532 fd
= g_open(pathname
, O_RDONLY
, 0);
534 g_warning("Can not open file : %s\n", pathname
);
538 iochan
= g_io_channel_unix_new(fd
);
540 context
= g_markup_parse_context_new(&markup_parser
, 0, des
,NULL
);
542 //while(fgets(buf,DIR_NAME_SIZE, fp) != NULL){
543 while(g_io_channel_read_line(iochan
, &buf
, &length
, NULL
, &error
)
544 != G_IO_STATUS_EOF
) {
547 g_warning("Can not read xml file: \n%s\n", error
->message
);
550 if(!g_markup_parse_context_parse(context
, buf
, length
, &error
)){
552 g_warning("Can not parse xml file: \n%s\n", error
->message
);
555 g_markup_parse_context_free(context
);
557 g_io_channel_shutdown(iochan
, FALSE
, &error
); /* No flush */
559 g_warning("Can not close file: \n%s\n", error
->message
);
567 g_markup_parse_context_free(context
);
569 g_io_channel_shutdown(iochan
, FALSE
, &error
); /* No flush */
571 g_warning("Can not close file: \n%s\n", error
->message
);
582 /*****************************************************************************
583 *The following functions get facility/tracefile information
584 ****************************************************************************/
586 gint
getFacilityInfo(LttTrace
*t
, gchar
* eventdefs
)
593 gchar fullname
[DIR_NAME_SIZE
];
594 GError
* error
= NULL
;
596 dir
= g_dir_open(eventdefs
, 0, &error
);
599 g_warning("Can not open directory: %s, %s\n", eventdefs
, error
->message
);
604 while((name
= g_dir_read_name(dir
)) != NULL
){
605 if(!g_pattern_match_simple("*.xml", name
)) continue;
606 strcpy(fullname
,eventdefs
);
607 strcat(fullname
,name
);
608 ltt_facility_open(t
,fullname
);
612 for(j
=0;j
<t
->facility_number
;j
++){
613 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, j
);
614 for(i
=0; i
<f
->event_number
; i
++){
616 setFieldsOffset(NULL
, et
, NULL
, t
);
623 /*****************************************************************************
624 *A trace is specified as a pathname to the directory containing all the
625 *associated data (control tracefiles, per cpu tracefiles, event
628 *When a trace is closed, all the associated facilities, types and fields
629 *are released as well.
633 /****************************************************************************
634 * get_absolute_pathname
636 * return the unique pathname in the system
638 * MD : Fixed this function so it uses realpath, dealing well with
639 * forgotten cases (.. were not used correctly before).
641 ****************************************************************************/
642 void get_absolute_pathname(const gchar
*pathname
, gchar
* abs_pathname
)
644 abs_pathname
[0] = '\0';
646 if ( realpath (pathname
, abs_pathname
) != NULL
)
650 /* error, return the original path unmodified */
651 strcpy(abs_pathname
, pathname
);
657 /* Search for something like : .*_.*
659 * The left side is the name, the right side is the number.
662 int get_tracefile_name_number(gchar
*raw_name
,
669 guint raw_name_len
= strlen(raw_name
);
670 gchar char_name
[PATH_MAX
];
677 for(i
=raw_name_len
-1;i
>=0;i
--) {
678 if(raw_name
[i
] == '_') break;
680 if(i
==-1) { /* Either not found or name length is 0 */
681 /* This is a userspace tracefile */
682 strncpy(char_name
, raw_name
, raw_name_len
);
683 char_name
[raw_name_len
] = '\0';
684 *name
= g_quark_from_string(char_name
);
685 *num
= 0; /* unknown cpu */
686 for(i
=0;i
<raw_name_len
;i
++) {
687 if(raw_name
[i
] == '/') {
692 for(;i
<raw_name_len
;i
++) {
693 if(raw_name
[i
] == '/') {
698 for(;i
<raw_name_len
;i
++) {
699 if(raw_name
[i
] == '-') {
703 if(i
== raw_name_len
) return -1;
705 tmpptr
= &raw_name
[i
];
706 for(;i
<raw_name_len
;i
++) {
707 if(raw_name
[i
] == '.') {
712 *tid
= strtoul(tmpptr
, &endptr
, 10);
714 return -1; /* No digit */
715 if(*tid
== ULONG_MAX
)
716 return -1; /* underflow / overflow */
718 tmpptr
= &raw_name
[i
];
719 for(;i
<raw_name_len
;i
++) {
720 if(raw_name
[i
] == '.') {
725 *pgid
= strtoul(tmpptr
, &endptr
, 10);
727 return -1; /* No digit */
728 if(*pgid
== ULONG_MAX
)
729 return -1; /* underflow / overflow */
731 tmpptr
= &raw_name
[i
];
732 *creation
= strtoull(tmpptr
, &endptr
, 10);
734 return -1; /* No digit */
735 if(*creation
== G_MAXUINT64
)
736 return -1; /* underflow / overflow */
740 cpu_num
= strtol(raw_name
+underscore_pos
+1, &endptr
, 10);
742 if(endptr
== raw_name
+underscore_pos
+1)
743 return -1; /* No digit */
744 if(cpu_num
== LONG_MIN
|| cpu_num
== LONG_MAX
)
745 return -1; /* underflow / overflow */
747 strncpy(char_name
, raw_name
, underscore_pos
);
748 char_name
[underscore_pos
] = '\0';
750 *name
= g_quark_from_string(char_name
);
759 GData
**ltt_trace_get_tracefiles_groups(LttTrace
*trace
)
761 return &trace
->tracefiles
;
765 void compute_tracefile_group(GQuark key_id
,
767 struct compute_tracefile_group_args
*args
)
772 for(i
=0; i
<group
->len
; i
++) {
773 tf
= &g_array_index (group
, LttTracefile
, i
);
775 args
->func(tf
, args
->func_args
);
780 void ltt_tracefile_group_destroy(gpointer data
)
782 GArray
*group
= (GArray
*)data
;
786 for(i
=0; i
<group
->len
; i
++) {
787 tf
= &g_array_index (group
, LttTracefile
, i
);
789 ltt_tracefile_close(tf
);
791 g_array_free(group
, TRUE
);
794 gboolean
ltt_tracefile_group_has_cpu_online(gpointer data
)
796 GArray
*group
= (GArray
*)data
;
800 for(i
=0; i
<group
->len
; i
++) {
801 tf
= &g_array_index (group
, LttTracefile
, i
);
809 /* Open each tracefile under a specific directory. Put them in a
810 * GData : permits to access them using their tracefile group pathname.
811 * i.e. access control/modules tracefile group by index :
814 * relative path is the path relative to the trace root
815 * root path is the full path
817 * A tracefile group is simply an array where all the per cpu tracefiles sit.
820 int open_tracefiles(LttTrace
*trace
, gchar
*root_path
, gchar
*relative_path
)
822 DIR *dir
= opendir(root_path
);
823 struct dirent
*entry
;
824 struct stat stat_buf
;
827 gchar path
[PATH_MAX
];
832 gchar rel_path
[PATH_MAX
];
841 strncpy(path
, root_path
, PATH_MAX
-1);
842 path_len
= strlen(path
);
843 path
[path_len
] = '/';
845 path_ptr
= path
+ path_len
;
847 strncpy(rel_path
, relative_path
, PATH_MAX
-1);
848 rel_path_len
= strlen(rel_path
);
849 rel_path
[rel_path_len
] = '/';
851 rel_path_ptr
= rel_path
+ rel_path_len
;
853 while((entry
= readdir(dir
)) != NULL
) {
855 if(entry
->d_name
[0] == '.') continue;
857 strncpy(path_ptr
, entry
->d_name
, PATH_MAX
- path_len
);
858 strncpy(rel_path_ptr
, entry
->d_name
, PATH_MAX
- rel_path_len
);
860 ret
= stat(path
, &stat_buf
);
866 g_debug("Tracefile file or directory : %s\n", path
);
868 if(strcmp(rel_path
, "/eventdefs") == 0) continue;
870 if(S_ISDIR(stat_buf
.st_mode
)) {
872 g_debug("Entering subdirectory...\n");
873 ret
= open_tracefiles(trace
, path
, rel_path
);
874 if(ret
< 0) continue;
875 } else if(S_ISREG(stat_buf
.st_mode
)) {
877 guint num
, tid
, pgid
;
880 num
= tid
= pgid
= 0;
882 if(get_tracefile_name_number(rel_path
, &name
, &num
, &tid
, &pgid
, &creation
))
883 continue; /* invalid name */
885 g_debug("Opening file.\n");
886 if(ltt_tracefile_open(trace
, path
, &tmp_tf
)) {
887 g_info("Error opening tracefile %s", path
);
889 continue; /* error opening the tracefile : bad magic number ? */
892 g_debug("Tracefile name is %s and number is %u",
893 g_quark_to_string(name
), num
);
895 tmp_tf
.cpu_online
= 1;
896 tmp_tf
.cpu_num
= num
;
900 tmp_tf
.creation
= creation
;
901 if(tmp_tf
.name
== g_quark_from_string("/compact")
902 || tmp_tf
.name
== g_quark_from_string("/flight-compact"))
906 group
= g_datalist_id_get_data(&trace
->tracefiles
, name
);
908 /* Elements are automatically cleared when the array is allocated.
909 * It makes the cpu_online variable set to 0 : cpu offline, by default.
911 group
= g_array_sized_new (FALSE
, TRUE
, sizeof(LttTracefile
), 10);
912 g_datalist_id_set_data_full(&trace
->tracefiles
, name
,
913 group
, ltt_tracefile_group_destroy
);
916 /* Add the per cpu tracefile to the named group */
917 unsigned int old_len
= group
->len
;
919 group
= g_array_set_size(group
, num
+1);
920 g_array_index (group
, LttTracefile
, num
) = tmp_tf
;
930 /* ltt_get_facility_description
932 * Opens the file corresponding to the requested facility (identified by fac_id
935 * The name searched is : %trace root%/eventdefs/facname_checksum.xml
937 * Returns 0 on success, or 1 on failure.
940 static int ltt_get_facility_description(LttFacility
*f
,
942 LttTracefile
*fac_tf
)
944 char desc_file_name
[PATH_MAX
];
951 text
= g_quark_to_string(t
->pathname
);
952 textlen
= strlen(text
);
954 if(textlen
>= PATH_MAX
) goto name_error
;
955 strcpy(desc_file_name
, text
);
957 text
= "/eventdefs/";
958 textlen
+=strlen(text
);
959 if(textlen
>= PATH_MAX
) goto name_error
;
960 strcat(desc_file_name
, text
);
962 text
= g_quark_to_string(f
->name
);
963 fac_name_len
= strlen(text
);
964 textlen
+=fac_name_len
;
965 if(textlen
>= PATH_MAX
) goto name_error
;
966 strcat(desc_file_name
, text
);
968 /* arch specific facilities are named like this : name_arch */
969 if(fac_name_len
+1 < sizeof("_arch"))
972 if(!strcmp(&text
[fac_name_len
+1-sizeof("_arch")], "_arch"))
980 textlen
+=strlen(text
);
981 if(textlen
>= PATH_MAX
) goto name_error
;
982 strcat(desc_file_name
, text
);
984 err
= snprintf(desc_file_name
+textlen
, PATH_MAX
-textlen
-1,
986 if(err
< 0) goto name_error
;
988 textlen
=strlen(desc_file_name
);
993 switch(t
->arch_type
) {
994 case LTT_ARCH_TYPE_I386
:
997 case LTT_ARCH_TYPE_PPC
:
1000 case LTT_ARCH_TYPE_SH
:
1003 case LTT_ARCH_TYPE_S390
:
1006 case LTT_ARCH_TYPE_MIPS
:
1009 case LTT_ARCH_TYPE_ARM
:
1012 case LTT_ARCH_TYPE_PPC64
:
1015 case LTT_ARCH_TYPE_X86_64
:
1018 case LTT_ARCH_TYPE_C2
:
1021 case LTT_ARCH_TYPE_POWERPC
:
1025 g_error("Trace from unsupported architecture.");
1027 textlen
+=strlen(text
);
1028 if(textlen
>= PATH_MAX
) goto name_error
;
1029 strcat(desc_file_name
, text
);
1033 textlen
+=strlen(text
);
1034 if(textlen
>= PATH_MAX
) goto name_error
;
1035 strcat(desc_file_name
, text
);
1037 err
= ltt_facility_open(f
, t
, desc_file_name
);
1038 if(err
) goto facility_error
;
1047 static void ltt_fac_ids_destroy(gpointer data
)
1049 GArray
*fac_ids
= (GArray
*)data
;
1051 g_array_free(fac_ids
, TRUE
);
1055 /* Presumes the tracefile is already seeked at the beginning. It makes sense,
1056 * because it must be done just after the opening */
1057 int ltt_process_facility_tracefile(LttTracefile
*tf
)
1066 err
= ltt_tracefile_read_seek(tf
);
1067 if(err
== EPERM
) goto seek_error
;
1068 else if(err
== ERANGE
) break; /* End of tracefile */
1070 err
= ltt_tracefile_read_update_event(tf
);
1071 if(err
) goto update_error
;
1073 /* We are on a facility load/or facility unload/ or heartbeat event */
1075 * * facility 0 is hardcoded : this is the core facility. It will be shown
1076 * in the facility array though, and is shown as "loaded builtin" in the
1078 * It contains event :
1080 * 1 : facility unload
1081 * 2 : state dump facility load
1084 if(tf
->event
.event_id
>= MARKER_CORE_IDS
) {
1085 /* Should only contain core facility */
1086 g_warning("Error in processing facility file %s, "
1087 "should not contain event id %u.", g_quark_to_string(tf
->name
),
1088 tf
->event
.event_id
);
1090 goto event_id_error
;
1094 const char *marker_name
, *format
;
1096 guint8 int_size
, long_size
, pointer_size
, size_t_size
, alignment
;
1099 switch((enum marker_id
)tf
->event
.event_id
) {
1100 case MARKER_ID_SET_MARKER_ID
:
1101 marker_name
= (char*)(tf
->event
.data
);
1102 g_debug("Doing MARKER_ID_SET_MARKER_ID of marker %s", marker_name
);
1103 pos
= (tf
->event
.data
+ strlen(marker_name
) + 1);
1104 pos
+= ltt_align((size_t)pos
, tf
->trace
->arch_size
, tf
->has_alignment
);
1105 pos
+= ltt_align((size_t)pos
, sizeof(uint16_t), tf
->has_alignment
);
1106 id
= ltt_get_uint16(LTT_GET_BO(tf
), pos
);
1107 pos
+= sizeof(guint16
);
1108 int_size
= *(guint8
*)pos
;
1109 pos
+= sizeof(guint8
);
1110 long_size
= *(guint8
*)pos
;
1111 pos
+= sizeof(guint8
);
1112 pointer_size
= *(guint8
*)pos
;
1113 pos
+= sizeof(guint8
);
1114 size_t_size
= *(guint8
*)pos
;
1115 pos
+= sizeof(guint8
);
1116 alignment
= *(guint8
*)pos
;
1117 pos
+= sizeof(guint8
);
1118 marker_id_event(tf
->trace
, g_quark_from_string(marker_name
),
1119 id
, int_size
, long_size
,
1120 pointer_size
, size_t_size
, alignment
);
1122 case MARKER_ID_SET_MARKER_FORMAT
:
1123 marker_name
= (char*)(tf
->event
.data
);
1124 g_debug("Doing MARKER_ID_SET_MARKER_FORMAT of marker %s",
1126 pos
= (tf
->event
.data
+ strlen(marker_name
) + 1);
1127 pos
+= ltt_align((size_t)pos
, tf
->trace
->arch_size
, tf
->has_alignment
);
1128 format
= (const char*)pos
;
1129 pos
+= strlen(format
) + 1;
1130 pos
+= ltt_align((size_t)pos
, tf
->trace
->arch_size
, tf
->has_alignment
);
1131 marker_format_event(tf
->trace
, g_quark_from_string(marker_name
),
1133 /* get information from dictionnary TODO */
1135 case MARKER_ID_HEARTBEAT_32
:
1136 case MARKER_ID_HEARTBEAT_64
:
1139 g_warning("Error in processing facility file %s, "
1140 "unknown event id %hhu.",
1141 g_quark_to_string(tf
->name
),
1142 tf
->event
.event_id
);
1144 goto event_id_error
;
1150 /* Error handling */
1154 g_warning("An error occured in facility tracefile parsing");
1159 LttTrace
*ltt_trace_open(const gchar
*pathname
)
1161 gchar abs_path
[PATH_MAX
];
1166 struct ltt_block_start_header
*header
;
1168 struct dirent
*entry
;
1169 guint control_found
= 0;
1170 guint eventdefs_found
= 0;
1171 struct stat stat_buf
;
1172 gchar path
[PATH_MAX
];
1174 t
= g_new(LttTrace
, 1);
1175 if(!t
) goto alloc_error
;
1177 get_absolute_pathname(pathname
, abs_path
);
1178 t
->pathname
= g_quark_from_string(abs_path
);
1180 g_datalist_init(&t
->tracefiles
);
1182 /* Test to see if it looks like a trace */
1183 dir
= opendir(abs_path
);
1188 while((entry
= readdir(dir
)) != NULL
) {
1189 strcpy(path
, abs_path
);
1191 strcat(path
, entry
->d_name
);
1192 ret
= stat(path
, &stat_buf
);
1197 if(S_ISDIR(stat_buf
.st_mode
)) {
1198 if(strcmp(entry
->d_name
, "control") == 0) {
1201 if(strcmp(entry
->d_name
, "eventdefs") == 0) {
1202 eventdefs_found
= 1;
1208 if(!control_found
|| !eventdefs_found
) goto find_error
;
1210 /* Open all the tracefiles */
1211 if(open_tracefiles(t
, abs_path
, "")) {
1212 g_warning("Error opening tracefile %s", abs_path
);
1216 /* Parse each trace control/facilitiesN files : get runtime fac. info */
1217 group
= g_datalist_id_get_data(&t
->tracefiles
, LTT_TRACEFILE_NAME_FACILITIES
);
1219 g_error("Trace %s has no facility tracefile", abs_path
);
1221 goto facilities_error
;
1224 /* Get the trace information for the control/facility 0 tracefile */
1225 g_assert(group
->len
> 0);
1226 tf
= &g_array_index (group
, LttTracefile
, 0);
1227 header
= (struct ltt_block_start_header
*)tf
->buffer
.head
;
1228 g_assert(parse_trace_header(header
->trace
,
1231 t
->num_cpu
= group
->len
;
1233 ret
= allocate_marker_data(t
);
1235 g_error("Error in allocating marker data");
1237 for(i
=0; i
<group
->len
; i
++) {
1238 tf
= &g_array_index (group
, LttTracefile
, i
);
1239 if(ltt_process_facility_tracefile(tf
))
1240 goto facilities_error
;
1245 /* Error handling */
1247 destroy_marker_data(t
);
1249 g_datalist_clear(&t
->tracefiles
);
1257 GQuark
ltt_trace_name(const LttTrace
*t
)
1263 /******************************************************************************
1264 * When we copy a trace, we want all the opening actions to happen again :
1265 * the trace will be reopened and totally independant from the original.
1266 * That's why we call ltt_trace_open.
1267 *****************************************************************************/
1268 LttTrace
*ltt_trace_copy(LttTrace
*self
)
1270 return ltt_trace_open(g_quark_to_string(self
->pathname
));
1273 void ltt_trace_close(LttTrace
*t
)
1275 g_datalist_clear(&t
->tracefiles
);
1280 /*****************************************************************************
1281 *Get the system description of the trace
1282 ****************************************************************************/
1284 LttFacility
*ltt_trace_facility_by_id(LttTrace
*t
, guint8 id
)
1286 g_assert(id
< t
->facilities_by_num
->len
);
1287 return &g_array_index(t
->facilities_by_num
, LttFacility
, id
);
1290 /* ltt_trace_facility_get_by_name
1292 * Returns the GArray of facility indexes. All the fac_ids that matches the
1293 * requested facility name.
1295 * If name is not found, returns NULL.
1297 GArray
*ltt_trace_facility_get_by_name(LttTrace
*t
, GQuark name
)
1299 return g_datalist_id_get_data(&t
->facilities_by_name
, name
);
1303 /*****************************************************************************
1304 * Functions to discover all the event types in the trace
1305 ****************************************************************************/
1308 unsigned ltt_trace_eventtype_number(LttTrace
*t
)
1312 unsigned int num
= t
->facility_number
;
1316 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, i
);
1317 count
+= f
->event_number
;
1324 //use an iteration on all the trace facilities, and inside iteration on all the
1325 //event types in each facilities instead.
1326 LttEventType
*ltt_trace_eventtype_get(LttTrace
*t
, unsigned evId
)
1328 LttEventType
*event_type
;
1331 f
= ltt_trace_facility_by_id(t
,evId
);
1333 if(unlikely(!f
)) event_type
= NULL
;
1334 else event_type
= f
->events
[evId
- f
->base_id
];
1341 /*****************************************************************************
1342 * ltt_trace_find_tracefile
1344 * Find a tracefile by name and index in the group.
1346 * Returns a pointer to the tracefiles, else NULL.
1347 ****************************************************************************/
1349 LttTracefile
*ltt_trace_find_tracefile(LttTrace
*t
, const gchar
*name
)
1354 /*****************************************************************************
1355 * Get the start time and end time of the trace
1356 ****************************************************************************/
1358 void ltt_tracefile_time_span_get(LttTracefile
*tf
,
1359 LttTime
*start
, LttTime
*end
)
1363 err
= map_block(tf
, 0);
1365 g_error("Can not map block");
1366 *start
= ltt_time_infinite
;
1368 *start
= tf
->buffer
.begin
.timestamp
;
1370 err
= map_block(tf
, tf
->num_blocks
- 1); /* Last block */
1372 g_error("Can not map block");
1373 *end
= ltt_time_zero
;
1375 *end
= tf
->buffer
.end
.timestamp
;
1378 struct tracefile_time_span_get_args
{
1384 void group_time_span_get(GQuark name
, gpointer data
, gpointer user_data
)
1386 struct tracefile_time_span_get_args
*args
=
1387 (struct tracefile_time_span_get_args
*)user_data
;
1389 GArray
*group
= (GArray
*)data
;
1395 for(i
=0; i
<group
->len
; i
++) {
1396 tf
= &g_array_index (group
, LttTracefile
, i
);
1397 if(tf
->cpu_online
) {
1398 ltt_tracefile_time_span_get(tf
, &tmp_start
, &tmp_end
);
1399 if(ltt_time_compare(*args
->start
, tmp_start
)>0) *args
->start
= tmp_start
;
1400 if(ltt_time_compare(*args
->end
, tmp_end
)<0) *args
->end
= tmp_end
;
1405 void ltt_trace_time_span_get(LttTrace
*t
, LttTime
*start
, LttTime
*end
)
1407 LttTime min_start
= ltt_time_infinite
;
1408 LttTime max_end
= ltt_time_zero
;
1409 struct tracefile_time_span_get_args args
= { t
, &min_start
, &max_end
};
1411 g_datalist_foreach(&t
->tracefiles
, &group_time_span_get
, &args
);
1413 if(start
!= NULL
) *start
= min_start
;
1414 if(end
!= NULL
) *end
= max_end
;
1419 /*****************************************************************************
1420 *Get the name of a tracefile
1421 ****************************************************************************/
1423 GQuark
ltt_tracefile_name(const LttTracefile
*tf
)
1428 GQuark
ltt_tracefile_long_name(const LttTracefile
*tf
)
1430 return tf
->long_name
;
1435 guint
ltt_tracefile_cpu(LttTracefile
*tf
)
1440 guint
ltt_tracefile_tid(LttTracefile
*tf
)
1445 guint
ltt_tracefile_pgid(LttTracefile
*tf
)
1450 guint64
ltt_tracefile_creation(LttTracefile
*tf
)
1452 return tf
->creation
;
1454 /*****************************************************************************
1455 * Get the number of blocks in the tracefile
1456 ****************************************************************************/
1458 guint
ltt_tracefile_block_number(LttTracefile
*tf
)
1460 return tf
->num_blocks
;
1464 /* Seek to the first event in a tracefile that has a time equal or greater than
1465 * the time passed in parameter.
1467 * If the time parameter is outside the tracefile time span, seek to the first
1468 * event or if after, return ERANGE.
1470 * If the time parameter is before the first event, we have to seek specially to
1473 * If the time is after the end of the trace, return ERANGE.
1475 * Do a binary search to find the right block, then a sequential search in the
1476 * block to find the event.
1478 * In the special case where the time requested fits inside a block that has no
1479 * event corresponding to the requested time, the first event of the next block
1482 * IMPORTANT NOTE : // FIXME everywhere...
1484 * You MUST NOT do a ltt_tracefile_read right after a ltt_tracefile_seek_time :
1485 * you will jump over an event if you do.
1487 * Return value : 0 : no error, the tf->event can be used
1488 * ERANGE : time if after the last event of the trace
1489 * otherwise : this is an error.
1493 int ltt_tracefile_seek_time(LttTracefile
*tf
, LttTime time
)
1497 unsigned int block_num
, high
, low
;
1499 /* seek at the beginning of trace */
1500 err
= map_block(tf
, 0); /* First block */
1502 g_error("Can not map block");
1506 /* If the time is lower or equal the beginning of the trace,
1507 * go to the first event. */
1508 if(ltt_time_compare(time
, tf
->buffer
.begin
.timestamp
) <= 0) {
1509 ret
= ltt_tracefile_read(tf
);
1510 if(ret
== ERANGE
) goto range
;
1511 else if (ret
) goto fail
;
1512 goto found
; /* There is either no event in the trace or the event points
1513 to the first event in the trace */
1516 err
= map_block(tf
, tf
->num_blocks
- 1); /* Last block */
1518 g_error("Can not map block");
1522 /* If the time is after the end of the trace, return ERANGE. */
1523 if(ltt_time_compare(time
, tf
->buffer
.end
.timestamp
) > 0) {
1527 /* Binary search the block */
1528 high
= tf
->num_blocks
- 1;
1532 block_num
= ((high
-low
) / 2) + low
;
1534 err
= map_block(tf
, block_num
);
1536 g_error("Can not map block");
1540 /* We cannot divide anymore : this is what would happen if the time
1541 * requested was exactly between two consecutive buffers'end and start
1542 * timestamps. This is also what would happend if we didn't deal with out
1543 * of span cases prior in this function. */
1544 /* The event is right in the buffer!
1545 * (or in the next buffer first event) */
1547 ret
= ltt_tracefile_read(tf
);
1548 if(ret
== ERANGE
) goto range
; /* ERANGE or EPERM */
1549 else if(ret
) goto fail
;
1551 if(ltt_time_compare(time
, tf
->event
.event_time
) <= 0)
1555 } else if(ltt_time_compare(time
, tf
->buffer
.begin
.timestamp
) < 0) {
1556 /* go to lower part */
1557 high
= block_num
- 1;
1558 } else if(ltt_time_compare(time
, tf
->buffer
.end
.timestamp
) > 0) {
1559 /* go to higher part */
1560 low
= block_num
+ 1;
1561 } else {/* The event is right in the buffer!
1562 (or in the next buffer first event) */
1564 ret
= ltt_tracefile_read(tf
);
1565 if(ret
== ERANGE
) goto range
; /* ERANGE or EPERM */
1566 else if(ret
) goto fail
;
1568 if(ltt_time_compare(time
, tf
->event
.event_time
) <= 0)
1580 /* Error handling */
1582 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1583 g_quark_to_string(tf
->name
));
1588 int ltt_tracefile_seek_position(LttTracefile
*tf
, const LttEventPosition
*ep
) {
1592 if(ep
->tracefile
!= tf
) {
1596 err
= map_block(tf
, ep
->block
);
1598 g_error("Can not map block");
1602 tf
->event
.offset
= ep
->offset
;
1604 /* Put back the event real tsc */
1605 tf
->event
.tsc
= ep
->tsc
;
1606 tf
->buffer
.tsc
= ep
->tsc
;
1608 err
= ltt_tracefile_read_update_event(tf
);
1610 err
= ltt_tracefile_read_op(tf
);
1616 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1617 g_quark_to_string(tf
->name
));
1621 LttTime
ltt_interpolate_time_from_tsc(LttTracefile
*tf
, guint64 tsc
)
1625 if(tsc
> tf
->trace
->start_tsc
) {
1626 time
= ltt_time_from_uint64(
1627 (double)(tsc
- tf
->trace
->start_tsc
)
1628 * (1000000000.0 / tf
->trace
->freq_scale
)
1629 / (double)tf
->trace
->start_freq
);
1630 time
= ltt_time_add(tf
->trace
->start_time_from_tsc
, time
);
1632 time
= ltt_time_from_uint64(
1633 (double)(tf
->trace
->start_tsc
- tsc
)
1634 * (1000000000.0 / tf
->trace
->freq_scale
)
1635 / (double)tf
->trace
->start_freq
);
1636 time
= ltt_time_sub(tf
->trace
->start_time_from_tsc
, time
);
1641 /* Calculate the real event time based on the buffer boundaries */
1642 LttTime
ltt_interpolate_time(LttTracefile
*tf
, LttEvent
*event
)
1644 return ltt_interpolate_time_from_tsc(tf
, tf
->buffer
.tsc
);
1648 /* Get the current event of the tracefile : valid until the next read */
1649 LttEvent
*ltt_tracefile_get_event(LttTracefile
*tf
)
1656 /*****************************************************************************
1658 * ltt_tracefile_read : Read the next event in the tracefile
1663 * Returns 0 if an event can be used in tf->event.
1664 * Returns ERANGE on end of trace. The event in tf->event still can be used
1665 * (if the last block was not empty).
1666 * Returns EPERM on error.
1668 * This function does make the tracefile event structure point to the event
1669 * currently pointed to by the tf->event.
1671 * Note : you must call a ltt_tracefile_seek to the beginning of the trace to
1672 * reinitialize it after an error if you want results to be coherent.
1673 * It would be the case if a end of trace last buffer has no event : the end
1674 * of trace wouldn't be returned, but an error.
1675 * We make the assumption there is at least one event per buffer.
1676 ****************************************************************************/
1678 int ltt_tracefile_read(LttTracefile
*tf
)
1682 err
= ltt_tracefile_read_seek(tf
);
1684 err
= ltt_tracefile_read_update_event(tf
);
1686 err
= ltt_tracefile_read_op(tf
);
1692 int ltt_tracefile_read_seek(LttTracefile
*tf
)
1696 /* Get next buffer until we finally have an event, or end of trace */
1698 err
= ltt_seek_next_event(tf
);
1699 if(unlikely(err
== ENOPROTOOPT
)) {
1703 /* Are we at the end of the buffer ? */
1705 if(unlikely(tf
->buffer
.index
== tf
->num_blocks
-1)){ /* end of trace ? */
1708 /* get next block */
1709 err
= map_block(tf
, tf
->buffer
.index
+ 1);
1711 g_error("Can not map block");
1715 } else break; /* We found an event ! */
1722 /* do specific operation on events */
1723 int ltt_tracefile_read_op(LttTracefile
*tf
)
1729 /* do event specific operation */
1731 /* do something if its an heartbeat event : increment the heartbeat count */
1732 //if(event->facility_id == LTT_FACILITY_CORE)
1733 // if(event->event_id == LTT_EVENT_HEARTBEAT)
1734 // tf->cur_heart_beat_number++;
1740 /* same as ltt_tracefile_read, but does not seek to the next event nor call
1741 * event specific operation. */
1742 int ltt_tracefile_read_update_event(LttTracefile
*tf
)
1748 pos
= tf
->buffer
.head
+ event
->offset
;
1750 /* Read event header */
1752 /* Align the head */
1754 pos
+= ltt_align((size_t)pos
, tf
->trace
->arch_size
, tf
->has_alignment
);
1756 g_assert(tf
->has_heartbeat
);
1757 pos
+= ltt_align((size_t)pos
, sizeof(uint32_t), tf
->has_alignment
);
1760 if(tf
->has_heartbeat
) {
1761 event
->timestamp
= ltt_get_uint32(LTT_GET_BO(tf
),
1764 /* 32 bits -> 64 bits tsc */
1765 /* note : still works for seek and non seek cases. */
1766 if(event
->timestamp
< (0xFFFFFFFFULL
&tf
->buffer
.tsc
)) {
1767 tf
->buffer
.tsc
= ((tf
->buffer
.tsc
&0xFFFFFFFF00000000ULL
)
1769 | (guint64
)event
->timestamp
;
1770 event
->tsc
= tf
->buffer
.tsc
;
1773 tf
->buffer
.tsc
= (tf
->buffer
.tsc
&0xFFFFFFFF00000000ULL
)
1774 | (guint64
)event
->timestamp
;
1775 event
->tsc
= tf
->buffer
.tsc
;
1776 event
->compact_data
= 0;
1779 /* Compact header */
1780 /* We keep the LSB of the previous timestamp, to make sure
1781 * we never go back */
1782 event
->event_id
= event
->timestamp
>> tf
->tscbits
;
1783 event
->event_id
= event
->event_id
& ((1 << tf
->trace
->compact_event_bits
) - 1);
1784 event
->compact_data
= event
->timestamp
>>
1785 (tf
->trace
->compact_event_bits
+ tf
->tscbits
);
1786 //printf("tsc bits %u, ev bits %u init data %u\n",
1787 // tf->tscbits, tf->trace->compact_event_bits, event->compact_data);
1788 /* Put the compact data back in original endianness */
1789 event
->compact_data
= ltt_get_uint32(LTT_GET_BO(tf
), &event
->compact_data
);
1790 event
->event_size
= 0xFFFF;
1791 //printf("Found compact event %d\n", event->event_id);
1792 //printf("Compact data %d\n", event->compact_data);
1793 event
->timestamp
= event
->timestamp
<< tf
->tsc_lsb_truncate
;
1794 event
->timestamp
= event
->timestamp
& tf
->tsc_mask
;
1795 //printf("timestamp 0x%lX\n", event->timestamp);
1796 //printf("mask 0x%llX\n", tf->tsc_mask);
1797 //printf("mask_next 0x%llX\n", tf->tsc_mask_next_bit);
1798 //printf("previous tsc 0x%llX\n", tf->buffer.tsc);
1799 //printf("previous tsc&mask 0x%llX\n", tf->tsc_mask&tf->buffer.tsc);
1800 //printf("previous tsc&(~mask) 0x%llX\n", tf->buffer.tsc&(~tf->tsc_mask));
1801 if(event
->timestamp
< (tf
->tsc_mask
&tf
->buffer
.tsc
)) {
1803 tf
->buffer
.tsc
= ((tf
->buffer
.tsc
&(~tf
->tsc_mask
))
1804 + tf
->tsc_mask_next_bit
)
1805 | (guint64
)event
->timestamp
;
1806 event
->tsc
= tf
->buffer
.tsc
;
1808 //printf("no wrap\n");
1810 tf
->buffer
.tsc
= (tf
->buffer
.tsc
&(~tf
->tsc_mask
))
1811 | (guint64
)event
->timestamp
;
1812 event
->tsc
= tf
->buffer
.tsc
;
1814 //printf("current tsc 0x%llX\n", tf->buffer.tsc);
1816 pos
+= sizeof(guint32
);
1818 event
->tsc
= ltt_get_uint64(LTT_GET_BO(tf
), pos
);
1819 tf
->buffer
.tsc
= event
->tsc
;
1820 event
->compact_data
= 0;
1821 pos
+= sizeof(guint64
);
1823 event
->event_time
= ltt_interpolate_time(tf
, event
);
1826 event
->event_id
= *(guint16
*)pos
;
1827 pos
+= sizeof(guint16
);
1829 event
->event_size
= ltt_get_uint16(LTT_GET_BO(tf
), pos
);
1830 pos
+= sizeof(guint16
);
1834 /* Align the head */
1836 pos
+= ltt_align((size_t)pos
, tf
->trace
->arch_size
, tf
->has_alignment
);
1840 /* get the data size and update the event fields with the current
1841 * information. Also update the time if a heartbeat_full event is found. */
1842 ltt_update_event_size(tf
);
1848 /****************************************************************************
1850 * map_block : map a block from the file
1852 * lttdes : ltt trace file
1853 * whichBlock : the block which will be read
1856 * EINVAL : lseek fail
1857 * EIO : can not read from the file
1858 ****************************************************************************/
1860 gint
map_block(LttTracefile
* tf
, guint block_num
)
1862 int page_size
= getpagesize();
1863 struct ltt_block_start_header
*header
;
1865 g_assert(block_num
< tf
->num_blocks
);
1867 if(tf
->buffer
.head
!= NULL
) {
1868 if(munmap(tf
->buffer
.head
, PAGE_ALIGN(tf
->buf_size
))) {
1869 g_warning("unmap size : %u\n",
1870 PAGE_ALIGN(tf
->buf_size
));
1871 perror("munmap error");
1877 /* Multiple of pages aligned head */
1878 tf
->buffer
.head
= mmap(0,
1879 PAGE_ALIGN(tf
->buf_size
),
1880 PROT_READ
, MAP_PRIVATE
, tf
->fd
,
1881 PAGE_ALIGN((off_t
)tf
->buf_size
* (off_t
)block_num
));
1883 if(tf
->buffer
.head
== MAP_FAILED
) {
1884 perror("Error in allocating memory for buffer of tracefile");
1888 g_assert( ( (guint
)tf
->buffer
.head
&(8-1) ) == 0); // make sure it's aligned.
1891 tf
->buffer
.index
= block_num
;
1893 header
= (struct ltt_block_start_header
*)tf
->buffer
.head
;
1896 tf
->buffer
.begin
.timestamp
= ltt_time_add(
1897 ltt_time_from_uint64(
1898 ltt_get_uint64(LTT_GET_BO(tf
),
1899 &header
->begin
.timestamp
)
1900 - tf
->trace
->start_monotonic
),
1901 tf
->trace
->start_time
);
1903 //g_debug("block %u begin : %lu.%lu", block_num,
1904 // tf->buffer.begin.timestamp.tv_sec, tf->buffer.begin.timestamp.tv_nsec);
1905 tf
->buffer
.begin
.cycle_count
= ltt_get_uint64(LTT_GET_BO(tf
),
1906 &header
->begin
.cycle_count
);
1907 tf
->buffer
.begin
.freq
= ltt_get_uint64(LTT_GET_BO(tf
),
1908 &header
->begin
.freq
);
1909 if(tf
->buffer
.begin
.freq
== 0)
1910 tf
->buffer
.begin
.freq
= tf
->trace
->start_freq
;
1912 tf
->buffer
.begin
.timestamp
= ltt_interpolate_time_from_tsc(tf
,
1913 tf
->buffer
.begin
.cycle_count
);
1916 ltt_time_from_uint64(
1917 (double)(tf
->buffer
.begin
.cycle_count
1918 - tf
->trace
->start_tsc
) * 1000000.0
1919 / (double)tf
->trace
->start_freq
),
1920 tf
->trace
->start_time_from_tsc
);
1924 tf
->buffer
.end
.timestamp
= ltt_time_add(
1925 ltt_time_from_uint64(
1926 ltt_get_uint64(LTT_GET_BO(tf
),
1927 &header
->end
.timestamp
)
1928 - tf
->trace
->start_monotonic
),
1929 tf
->trace
->start_time
);
1931 //g_debug("block %u end : %lu.%lu", block_num,
1932 // tf->buffer.end.timestamp.tv_sec, tf->buffer.end.timestamp.tv_nsec);
1933 tf
->buffer
.end
.cycle_count
= ltt_get_uint64(LTT_GET_BO(tf
),
1934 &header
->end
.cycle_count
);
1935 tf
->buffer
.end
.freq
= ltt_get_uint64(LTT_GET_BO(tf
),
1937 if(tf
->buffer
.end
.freq
== 0)
1938 tf
->buffer
.end
.freq
= tf
->trace
->start_freq
;
1940 tf
->buffer
.lost_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1941 &header
->lost_size
);
1942 tf
->buffer
.end
.timestamp
= ltt_interpolate_time_from_tsc(tf
,
1943 tf
->buffer
.end
.cycle_count
);
1946 ltt_time_from_uint64(
1947 (double)(tf
->buffer
.end
.cycle_count
1948 - tf
->trace
->start_tsc
) * 1000000.0
1949 / (double)tf
->trace
->start_freq
),
1950 tf
->trace
->start_time_from_tsc
);
1952 tf
->buffer
.tsc
= tf
->buffer
.begin
.cycle_count
;
1953 tf
->event
.tsc
= tf
->buffer
.tsc
;
1954 tf
->buffer
.freq
= tf
->buffer
.begin
.freq
;
1957 * eventually support variable buffer size : will need a partial pre-read of
1958 * the headers to create an index when we open the trace... eventually. */
1959 g_assert(tf
->buf_size
== ltt_get_uint32(LTT_GET_BO(tf
),
1960 &header
->buf_size
));
1962 /* Now that the buffer is mapped, calculate the time interpolation for the
1965 // tf->buffer.nsecs_per_cycle = calc_nsecs_per_cycle(tf);
1966 //tf->buffer.cyc2ns_scale = calc_nsecs_per_cycle(tf);
1968 /* Make the current event point to the beginning of the buffer :
1969 * it means that the event read must get the first event. */
1970 tf
->event
.tracefile
= tf
;
1971 tf
->event
.block
= block_num
;
1972 tf
->event
.offset
= 0;
1981 /* It will update the fields offsets too */
1982 void ltt_update_event_size(LttTracefile
*tf
)
1986 struct marker_info
*info
;
1988 switch((enum marker_id
)tf
->event
.event_id
) {
1989 case MARKER_ID_SET_MARKER_ID
:
1990 size
= strlen((char*)tf
->event
.data
) + 1;
1991 //g_debug("marker %s id set", (char*)tf->event.data);
1992 size
+= ltt_align(size
, sizeof(guint16
), tf
->has_alignment
);
1993 size
+= sizeof(guint16
);
1994 size
+= sizeof(guint8
);
1995 size
+= sizeof(guint8
);
1996 size
+= sizeof(guint8
);
1997 size
+= sizeof(guint8
);
1998 size
+= sizeof(guint8
);
2000 case MARKER_ID_SET_MARKER_FORMAT
:
2001 //g_debug("marker %s format set", (char*)tf->event.data);
2002 size
= strlen((char*)tf
->event
.data
) + 1;
2003 size
+= strlen((char*)tf
->event
.data
) + 1;
2005 case MARKER_ID_HEARTBEAT_32
:
2006 //g_debug("Update Event heartbeat 32 bits");
2007 size
= ltt_align(size
, sizeof(guint32
), tf
->has_alignment
);
2008 size
+= sizeof(guint32
);
2010 case MARKER_ID_HEARTBEAT_64
:
2011 //g_debug("Update Event heartbeat 64 bits");
2012 tscdata
= (char*)(tf
->event
.data
);
2013 tf
->event
.tsc
= ltt_get_uint64(LTT_GET_BO(tf
), tscdata
);
2014 tf
->buffer
.tsc
= tf
->event
.tsc
;
2015 tf
->event
.event_time
= ltt_interpolate_time(tf
, &tf
->event
);
2016 size
= ltt_align(size
, sizeof(guint64
), tf
->has_alignment
);
2017 size
+= sizeof(guint64
);
2020 info
= marker_get_info_from_id(tf
->trace
, tf
->event
.event_id
);
2021 g_assert(info
!= NULL
);
2022 if (info
->size
!= -1) {
2025 size
= marker_update_fields_offsets(marker_get_info_from_id(tf
->trace
,
2026 tf
->event
.event_id
), tf
->event
.data
);
2030 tf
->event
.data_size
= size
;
2032 /* Check consistency between kernel and LTTV structure sizes */
2033 if(tf
->event
.event_size
== 0xFFFF) {
2034 /* Event size too big to fit in the event size field */
2035 tf
->event
.event_size
= tf
->event
.data_size
;
2037 if (tf
->event
.data_size
!= tf
->event
.event_size
) {
2038 struct marker_info
*info
= marker_get_info_from_id(tf
->trace
,
2039 tf
->event
.event_id
);
2040 g_error("Kernel/LTTV event size differs for event %s: kernel %u, LTTV %u",
2041 g_quark_to_string(info
->name
),
2042 tf
->event
.event_size
, tf
->event
.data_size
);
2047 LttEventType
*event_type
=
2048 ltt_facility_eventtype_get(f
, tf
->event
.event_id
);
2051 g_warning("Unknown event id %hhu in facility %s in tracefile %s",
2053 g_quark_to_string(f
->name
),
2054 g_quark_to_string(tf
->name
));
2055 goto event_type_error
;
2058 /* Compute the dynamic offsets */
2059 compute_offsets(tf
, f
, event_type
, &size
, tf
->event
.data
);
2061 //g_debug("Event root field : f.e %hhu.%hhu size %zd",
2062 // tf->event.facility_id,
2063 // tf->event.event_id, size);
2066 tf
->event
.data_size
= size
;
2068 /* Check consistency between kernel and LTTV structure sizes */
2069 if(tf
->event
.event_size
== 0xFFFF) {
2070 /* Event size too big to fit in the event size field */
2071 tf
->event
.event_size
= tf
->event
.data_size
;
2073 if (tf
->event
.data_size
!= tf
->event
.event_size
) {
2074 g_error("Kernel/LTTV event size differs for event %s.%s: kernel %u, LTTV %u",
2075 g_quark_to_string(f
->name
), g_quark_to_string(event_type
->name
),
2076 tf
->event
.event_size
, tf
->event
.data_size
);
2079 //g_assert(tf->event.data_size == tf->event.event_size);
2085 if(tf
->event
.event_size
== 0xFFFF) {
2086 g_error("Cannot jump over an unknown event bigger than 0xFFFE bytes");
2088 /* The facility is unknown : use the kernel information about this event
2089 * to jump over it. */
2090 tf
->event
.data_size
= tf
->event
.event_size
;
2095 /* Take the tf current event offset and use the event facility id and event id
2096 * to figure out where is the next event offset.
2098 * This is an internal function not aiming at being used elsewhere : it will
2099 * not jump over the current block limits. Please consider using
2100 * ltt_tracefile_read to do this.
2102 * Returns 0 on success
2103 * ERANGE if we are at the end of the buffer.
2104 * ENOPROTOOPT if an error occured when getting the current event size.
2106 int ltt_seek_next_event(LttTracefile
*tf
)
2111 /* seek over the buffer header if we are at the buffer start */
2112 if(tf
->event
.offset
== 0) {
2113 tf
->event
.offset
+= tf
->buffer_header_size
;
2115 if(tf
->event
.offset
== tf
->buf_size
- tf
->buffer
.lost_size
) {
2122 pos
= tf
->event
.data
;
2124 if(tf
->event
.data_size
< 0) goto error
;
2126 pos
+= (size_t)tf
->event
.data_size
;
2128 tf
->event
.offset
= pos
- tf
->buffer
.head
;
2130 if(tf
->event
.offset
== tf
->buf_size
- tf
->buffer
.lost_size
) {
2134 g_assert(tf
->event
.offset
< tf
->buf_size
- tf
->buffer
.lost_size
);
2140 g_error("Error in ltt_seek_next_event for tracefile %s",
2141 g_quark_to_string(tf
->name
));
2146 /*****************************************************************************
2148 * calc_nsecs_per_cycle : calculate nsecs per cycle for current block
2150 * 1.0 / (freq(khz) *1000) * 1000000000
2153 ****************************************************************************/
2154 /* from timer_tsc.c */
2155 #define CYC2NS_SCALE_FACTOR 10
2156 static guint32
calc_nsecs_per_cycle(LttTracefile
* tf
)
2158 //return 1e6 / (double)tf->buffer.freq;
2159 guint32 cpu_mhz
= tf
->buffer
.freq
/ 1000;
2160 guint32 cyc2ns_scale
= (1000 << CYC2NS_SCALE_FACTOR
)/cpu_mhz
;
2162 return cyc2ns_scale
;
2163 // return 1e6 / (double)tf->buffer.freq;
2166 static guint64
cycles_2_ns(LttTracefile
*tf
, guint64 cycles
)
2168 return (cycles
* tf
->buffer
.cyc2ns_scale
) >> CYC2NS_SCALE_FACTOR
;
2173 void setFieldsOffset(LttTracefile
*tf
, LttEventType
*evT
,void *evD
)
2175 LttField
* rootFld
= evT
->root_field
;
2176 // rootFld->base_address = evD;
2179 rootFld
->field_size
= getFieldtypeSize(tf
, evT
->facility
,
2180 evT
, 0,0,rootFld
, evD
);
2184 /*****************************************************************************
2186 * set_fields_offsets : set the precomputable offset of the fields
2188 * tracefile : opened trace file
2189 * event_type : the event type
2190 ****************************************************************************/
2192 void set_fields_offsets(LttTracefile
*tf
, LttEventType
*event_type
)
2194 LttField
*field
= event_type
->root_field
;
2195 enum field_status fixed_root
= FIELD_FIXED
, fixed_parent
= FIELD_FIXED
;
2198 preset_field_type_size(tf
, event_type
, 0, 0,
2199 &fixed_root
, &fixed_parent
,
2206 /*****************************************************************************
2208 * get_alignment : Get the alignment needed for a field.
2212 * returns : The size on which it must be aligned.
2214 ****************************************************************************/
2216 off_t
get_alignment(LttField
*field
)
2218 LttType
*type
= &field
->field_type
;
2220 switch(type
->type_class
) {
2222 case LTT_UINT_FIXED
:
2237 /* Align offset on type size */
2238 g_assert(field
->field_size
!= 0);
2239 return field
->field_size
;
2245 g_assert(type
->fields
->len
== 1);
2247 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
2248 return get_alignment(child
);
2252 g_assert(type
->fields
->len
== 2);
2255 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
2257 localign
= max(localign
, get_alignment(child
));
2259 child
= &g_array_index(type
->fields
, LttField
, 1);
2260 localign
= max(localign
, get_alignment(child
));
2271 for(i
=0; i
<type
->fields
->len
; i
++) {
2272 LttField
*child
= &g_array_index(type
->fields
, LttField
, i
);
2273 localign
= max(localign
, get_alignment(child
));
2280 g_error("get_alignment : unknown type");
2287 /*****************************************************************************
2289 * field_compute_static_size : Determine the size of fields known by their
2290 * sole definition. Unions, arrays and struct sizes might be known, but
2291 * the parser does not give that information.
2296 ****************************************************************************/
2298 void field_compute_static_size(LttFacility
*fac
, LttField
*field
)
2300 LttType
*type
= &field
->field_type
;
2302 switch(type
->type_class
) {
2304 case LTT_UINT_FIXED
:
2323 /* note this : array type size is the number of elements in the array,
2324 * while array field size of the length of the array in bytes */
2325 g_assert(type
->fields
->len
== 1);
2327 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
2328 field_compute_static_size(fac
, child
);
2330 if(child
->field_size
!= 0) {
2331 field
->field_size
= type
->size
* child
->field_size
;
2332 field
->dynamic_offsets
= g_array_sized_new(FALSE
, TRUE
,
2333 sizeof(off_t
), type
->size
);
2335 field
->field_size
= 0;
2340 g_assert(type
->fields
->len
== 2);
2342 off_t local_offset
= 0;
2343 LttField
*child
= &g_array_index(type
->fields
, LttField
, 1);
2344 field_compute_static_size(fac
, child
);
2345 field
->field_size
= 0;
2347 if(child
->field_size
!= 0) {
2348 field
->dynamic_offsets
= g_array_sized_new(FALSE
, TRUE
,
2349 sizeof(off_t
), SEQUENCE_AVG_ELEMENTS
);
2357 for(i
=0;i
<type
->fields
->len
;i
++) {
2358 LttField
*child
= &g_array_index(type
->fields
, LttField
, i
);
2359 field_compute_static_size(fac
, child
);
2360 if(child
->field_size
!= 0) {
2361 type
->size
+= ltt_align(type
->size
, get_alignment(child
),
2363 type
->size
+= child
->field_size
;
2365 /* As soon as we find a child with variable size, we have
2366 * a variable size */
2371 field
->field_size
= type
->size
;
2375 g_error("field_static_size : unknown type");
2382 /*****************************************************************************
2384 * precompute_fields_offsets : set the precomputable offset of the fields
2388 * offset : pointer to the current offset, must be incremented
2390 * return : 1 : found a variable length field, stop the processing.
2392 ****************************************************************************/
2395 gint
precompute_fields_offsets(LttFacility
*fac
, LttField
*field
, off_t
*offset
, gint is_compact
)
2397 LttType
*type
= &field
->field_type
;
2399 if(unlikely(is_compact
)) {
2400 g_assert(field
->field_size
!= 0);
2401 /* FIXME THIS IS A HUUUUUGE hack :
2402 * offset is between the compact_data field in struct LttEvent
2403 * and the address of the field root in the memory map.
2404 * ark. Both will stay at the same addresses while the event
2405 * is readable, so it's ok.
2407 field
->offset_root
= 0;
2408 field
->fixed_root
= FIELD_FIXED
;
2412 switch(type
->type_class
) {
2414 case LTT_UINT_FIXED
:
2429 g_assert(field
->field_size
!= 0);
2430 /* Align offset on type size */
2431 *offset
+= ltt_align(*offset
, get_alignment(field
),
2433 /* remember offset */
2434 field
->offset_root
= *offset
;
2435 field
->fixed_root
= FIELD_FIXED
;
2436 /* Increment offset */
2437 *offset
+= field
->field_size
;
2441 field
->offset_root
= *offset
;
2442 field
->fixed_root
= FIELD_FIXED
;
2446 g_assert(type
->fields
->len
== 1);
2448 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
2450 *offset
+= ltt_align(*offset
, get_alignment(field
),
2453 /* remember offset */
2454 field
->offset_root
= *offset
;
2455 field
->array_offset
= *offset
;
2456 field
->fixed_root
= FIELD_FIXED
;
2458 /* Let the child be variable */
2459 //precompute_fields_offsets(tf, child, offset);
2461 if(field
->field_size
!= 0) {
2462 /* Increment offset */
2463 /* field_size is the array size in bytes */
2464 *offset
+= field
->field_size
;
2472 g_assert(type
->fields
->len
== 2);
2477 *offset
+= ltt_align(*offset
, get_alignment(field
),
2480 /* remember offset */
2481 field
->offset_root
= *offset
;
2482 field
->fixed_root
= FIELD_FIXED
;
2484 child
= &g_array_index(type
->fields
, LttField
, 0);
2485 ret
= precompute_fields_offsets(fac
, child
, offset
, is_compact
);
2486 g_assert(ret
== 0); /* Seq len cannot have variable len */
2488 child
= &g_array_index(type
->fields
, LttField
, 1);
2489 *offset
+= ltt_align(*offset
, get_alignment(child
),
2491 field
->array_offset
= *offset
;
2492 /* Let the child be variable. */
2493 //ret = precompute_fields_offsets(fac, child, offset);
2495 /* Cannot precompute fields offsets of sequence members, and has
2496 * variable length. */
2506 *offset
+= ltt_align(*offset
, get_alignment(field
),
2508 /* remember offset */
2509 field
->offset_root
= *offset
;
2510 field
->fixed_root
= FIELD_FIXED
;
2512 for(i
=0; i
< type
->fields
->len
; i
++) {
2513 child
= &g_array_index(type
->fields
, LttField
, i
);
2514 ret
= precompute_fields_offsets(fac
, child
, offset
, is_compact
);
2527 *offset
+= ltt_align(*offset
, get_alignment(field
),
2529 /* remember offset */
2530 field
->offset_root
= *offset
;
2531 field
->fixed_root
= FIELD_FIXED
;
2533 for(i
=0; i
< type
->fields
->len
; i
++) {
2534 *offset
= field
->offset_root
;
2535 child
= &g_array_index(type
->fields
, LttField
, i
);
2536 ret
= precompute_fields_offsets(fac
, child
, offset
, is_compact
);
2540 *offset
= field
->offset_root
+ field
->field_size
;
2547 g_error("precompute_fields_offsets : unknown type");
2556 /*****************************************************************************
2558 * precompute_offsets : set the precomputable offset of an event type
2561 * event : event type
2563 ****************************************************************************/
2564 void precompute_offsets(LttFacility
*fac
, LttEventType
*event
)
2570 /* First, compute the size of fixed size fields. Will determine size for
2571 * arrays, struct and unions, which is not done by the parser */
2572 for(i
=0; i
<event
->fields
->len
; i
++) {
2573 LttField
*field
= &g_array_index(event
->fields
, LttField
, i
);
2574 field_compute_static_size(fac
, field
);
2577 /* Precompute all known offsets */
2578 for(i
=0; i
<event
->fields
->len
; i
++) {
2579 LttField
*field
= &g_array_index(event
->fields
, LttField
, i
);
2580 if(event
->has_compact_data
&& i
== 0)
2581 ret
= precompute_fields_offsets(fac
, field
, &offset
, 1);
2583 ret
= precompute_fields_offsets(fac
, field
, &offset
, 0);
2591 /*****************************************************************************
2593 * preset_field_type_size : set the fixed sizes of the field type
2596 * event_type : event type
2597 * offset_root : offset from the root
2598 * offset_parent : offset from the parent
2599 * fixed_root : Do we know a fixed offset to the root ?
2600 * fixed_parent : Do we know a fixed offset to the parent ?
2602 ****************************************************************************/
2606 // preset the fixed size offsets. Calculate them just like genevent-new : an
2607 // increment of a *to value that represents the offset from the start of the
2609 // The preset information is : offsets up to (and including) the first element
2610 // of variable size. All subsequent fields must be flagged "VARIABLE OFFSET".
2612 void preset_field_type_size(LttTracefile
*tf
, LttEventType
*event_type
,
2613 off_t offset_root
, off_t offset_parent
,
2614 enum field_status
*fixed_root
, enum field_status
*fixed_parent
,
2617 enum field_status local_fixed_root
, local_fixed_parent
;
2621 g_assert(field
->fixed_root
== FIELD_UNKNOWN
);
2622 g_assert(field
->fixed_parent
== FIELD_UNKNOWN
);
2623 g_assert(field
->fixed_size
== FIELD_UNKNOWN
);
2625 type
= field
->field_type
;
2627 field
->fixed_root
= *fixed_root
;
2628 if(field
->fixed_root
== FIELD_FIXED
)
2629 field
->offset_root
= offset_root
;
2631 field
->offset_root
= 0;
2633 field
->fixed_parent
= *fixed_parent
;
2634 if(field
->fixed_parent
== FIELD_FIXED
)
2635 field
->offset_parent
= offset_parent
;
2637 field
->offset_parent
= 0;
2639 size_t current_root_offset
;
2640 size_t current_offset
;
2641 enum field_status current_child_status
, final_child_status
;
2644 switch(type
->type_class
) {
2646 case LTT_UINT_FIXED
:
2655 field
->field_size
= ltt_type_size(tf
->trace
, type
);
2656 field
->fixed_size
= FIELD_FIXED
;
2659 field
->field_size
= (off_t
)event_type
->facility
->pointer_size
;
2660 field
->fixed_size
= FIELD_FIXED
;
2664 field
->field_size
= (off_t
)event_type
->facility
->long_size
;
2665 field
->fixed_size
= FIELD_FIXED
;
2670 field
->field_size
= (off_t
)event_type
->facility
->size_t_size
;
2671 field
->fixed_size
= FIELD_FIXED
;
2674 local_fixed_root
= FIELD_VARIABLE
;
2675 local_fixed_parent
= FIELD_VARIABLE
;
2676 preset_field_type_size(tf
, event_type
,
2678 &local_fixed_root
, &local_fixed_parent
,
2680 field
->fixed_size
= FIELD_VARIABLE
;
2681 field
->field_size
= 0;
2682 *fixed_root
= FIELD_VARIABLE
;
2683 *fixed_parent
= FIELD_VARIABLE
;
2686 field
->fixed_size
= FIELD_VARIABLE
;
2687 field
->field_size
= 0;
2688 *fixed_root
= FIELD_VARIABLE
;
2689 *fixed_parent
= FIELD_VARIABLE
;
2692 local_fixed_root
= FIELD_VARIABLE
;
2693 local_fixed_parent
= FIELD_VARIABLE
;
2694 preset_field_type_size(tf
, event_type
,
2696 &local_fixed_root
, &local_fixed_parent
,
2698 field
->fixed_size
= field
->child
[0]->fixed_size
;
2699 if(field
->fixed_size
== FIELD_FIXED
) {
2700 field
->field_size
= type
->element_number
* field
->child
[0]->field_size
;
2702 field
->field_size
= 0;
2703 *fixed_root
= FIELD_VARIABLE
;
2704 *fixed_parent
= FIELD_VARIABLE
;
2708 current_root_offset
= field
->offset_root
;
2710 current_child_status
= FIELD_FIXED
;
2711 for(i
=0;i
<type
->element_number
;i
++) {
2712 preset_field_type_size(tf
, event_type
,
2713 current_root_offset
, current_offset
,
2714 fixed_root
, ¤t_child_status
,
2716 if(current_child_status
== FIELD_FIXED
) {
2717 current_root_offset
+= field
->child
[i
]->field_size
;
2718 current_offset
+= field
->child
[i
]->field_size
;
2720 current_root_offset
= 0;
2724 if(current_child_status
!= FIELD_FIXED
) {
2725 *fixed_parent
= current_child_status
;
2726 field
->field_size
= 0;
2727 field
->fixed_size
= current_child_status
;
2729 field
->field_size
= current_offset
;
2730 field
->fixed_size
= FIELD_FIXED
;
2734 current_root_offset
= field
->offset_root
;
2737 final_child_status
= FIELD_FIXED
;
2738 for(i
=0;i
<type
->element_number
;i
++) {
2739 enum field_status current_root_child_status
= FIELD_FIXED
;
2740 enum field_status current_child_status
= FIELD_FIXED
;
2741 preset_field_type_size(tf
, event_type
,
2742 current_root_offset
, current_offset
,
2743 ¤t_root_child_status
, ¤t_child_status
,
2745 if(current_child_status
!= FIELD_FIXED
)
2746 final_child_status
= current_child_status
;
2748 max_size
= max(max_size
, field
->child
[i
]->field_size
);
2750 if(final_child_status
!= FIELD_FIXED
) {
2751 g_error("LTTV does not support variable size fields in unions.");
2752 /* This will stop the application. */
2753 *fixed_root
= final_child_status
;
2754 *fixed_parent
= final_child_status
;
2755 field
->field_size
= 0;
2756 field
->fixed_size
= current_child_status
;
2758 field
->field_size
= max_size
;
2759 field
->fixed_size
= FIELD_FIXED
;
2763 g_error("unexpected type NONE");
2770 /*****************************************************************************
2772 * check_fields_compatibility : Check for compatibility between two fields :
2773 * do they use the same inner structure ?
2775 * event_type1 : event type
2776 * event_type2 : event type
2779 *Returns : 0 if identical
2781 ****************************************************************************/
2782 // this function checks for equality of field types. Therefore, it does not use
2783 // per se offsets. For instance, an aligned version of a structure is
2784 // compatible with an unaligned version of the same structure.
2786 gint
check_fields_compatibility(LttEventType
*event_type1
,
2787 LttEventType
*event_type2
,
2788 LttField
*field1
, LttField
*field2
)
2790 guint different
= 0;
2794 if(field1
== NULL
) {
2795 if(field2
== NULL
) goto end
;
2800 } else if(field2
== NULL
) {
2805 type1
= &field1
->field_type
;
2806 type2
= &field2
->field_type
;
2808 if(type1
->type_class
!= type2
->type_class
) {
2812 if(type1
->network
!= type2
->network
) {
2817 switch(type1
->type_class
) {
2819 case LTT_UINT_FIXED
:
2834 if(field1
->field_size
!= field2
->field_size
)
2841 LttField
*child1
= &g_array_index(type1
->fields
, LttField
, 0);
2842 LttField
*child2
= &g_array_index(type2
->fields
, LttField
, 0);
2844 if(type1
->size
!= type2
->size
)
2846 if(check_fields_compatibility(event_type1
, event_type2
, child1
, child2
))
2852 LttField
*child1
= &g_array_index(type1
->fields
, LttField
, 1);
2853 LttField
*child2
= &g_array_index(type2
->fields
, LttField
, 1);
2855 if(check_fields_compatibility(event_type1
, event_type2
, child1
, child2
))
2865 if(type1
->fields
->len
!= type2
->fields
->len
) {
2870 for(i
=0; i
< type1
->fields
->len
; i
++) {
2873 child1
= &g_array_index(type1
->fields
, LttField
, i
);
2874 child2
= &g_array_index(type2
->fields
, LttField
, i
);
2875 different
= check_fields_compatibility(event_type1
,
2876 event_type2
, child1
, child2
);
2878 if(different
) break;
2884 g_error("check_fields_compatibility : unknown type");
2893 gint
check_fields_compatibility(LttEventType
*event_type1
,
2894 LttEventType
*event_type2
,
2895 LttField
*field1
, LttField
*field2
)
2897 guint different
= 0;
2902 if(field1
== NULL
) {
2903 if(field2
== NULL
) goto end
;
2908 } else if(field2
== NULL
) {
2913 g_assert(field1
->fixed_root
!= FIELD_UNKNOWN
);
2914 g_assert(field2
->fixed_root
!= FIELD_UNKNOWN
);
2915 g_assert(field1
->fixed_parent
!= FIELD_UNKNOWN
);
2916 g_assert(field2
->fixed_parent
!= FIELD_UNKNOWN
);
2917 g_assert(field1
->fixed_size
!= FIELD_UNKNOWN
);
2918 g_assert(field2
->fixed_size
!= FIELD_UNKNOWN
);
2920 type1
= field1
->field_type
;
2921 type2
= field2
->field_type
;
2923 if(type1
->type_class
!= type2
->type_class
) {
2927 if(type1
->element_name
!= type2
->element_name
) {
2932 switch(type1
->type_class
) {
2934 case LTT_UINT_FIXED
:
2949 if(field1
->field_size
!= field2
->field_size
) {
2955 if(type1
->element_number
!= type2
->element_number
) {
2959 for(i
=0;i
<type1
->element_number
;i
++) {
2960 if(type1
->enum_strings
[i
] != type2
->enum_strings
[i
]) {
2967 /* Two elements : size and child */
2968 g_assert(type1
->element_number
!= type2
->element_number
);
2969 for(i
=0;i
<type1
->element_number
;i
++) {
2970 if(check_fields_compatibility(event_type1
, event_type2
,
2971 field1
->child
[0], field2
->child
[0])) {
2980 if(field1
->field_size
!= field2
->field_size
) {
2984 /* Two elements : size and child */
2985 g_assert(type1
->element_number
!= type2
->element_number
);
2986 for(i
=0;i
<type1
->element_number
;i
++) {
2987 if(check_fields_compatibility(event_type1
, event_type2
,
2988 field1
->child
[0], field2
->child
[0])) {
2996 if(type1
->element_number
!= type2
->element_number
) {
3000 for(i
=0;i
<type1
->element_number
;i
++) {
3001 if(check_fields_compatibility(event_type1
, event_type2
,
3002 field1
->child
[0], field2
->child
[0])) {
3015 /*****************************************************************************
3017 * ltt_get_int : get an integer number
3019 * reverse_byte_order: must we reverse the byte order ?
3020 * size : the size of the integer
3021 * ptr : the data pointer
3023 * gint64 : a 64 bits integer
3024 ****************************************************************************/
3026 gint64
ltt_get_int(gboolean reverse_byte_order
, gint size
, void *data
)
3031 case 1: val
= *((gint8
*)data
); break;
3032 case 2: val
= ltt_get_int16(reverse_byte_order
, data
); break;
3033 case 4: val
= ltt_get_int32(reverse_byte_order
, data
); break;
3034 case 8: val
= ltt_get_int64(reverse_byte_order
, data
); break;
3035 default: val
= ltt_get_int64(reverse_byte_order
, data
);
3036 g_critical("get_int : integer size %d unknown", size
);
3043 /*****************************************************************************
3045 * ltt_get_uint : get an unsigned integer number
3047 * reverse_byte_order: must we reverse the byte order ?
3048 * size : the size of the integer
3049 * ptr : the data pointer
3051 * guint64 : a 64 bits unsigned integer
3052 ****************************************************************************/
3054 guint64
ltt_get_uint(gboolean reverse_byte_order
, gint size
, void *data
)
3059 case 1: val
= *((gint8
*)data
); break;
3060 case 2: val
= ltt_get_uint16(reverse_byte_order
, data
); break;
3061 case 4: val
= ltt_get_uint32(reverse_byte_order
, data
); break;
3062 case 8: val
= ltt_get_uint64(reverse_byte_order
, data
); break;
3063 default: val
= ltt_get_uint64(reverse_byte_order
, data
);
3064 g_critical("get_uint : unsigned integer size %d unknown",
3073 /* get the node name of the system */
3075 char * ltt_trace_system_description_node_name (LttSystemDescription
* s
)
3077 return s
->node_name
;
3081 /* get the domain name of the system */
3083 char * ltt_trace_system_description_domain_name (LttSystemDescription
* s
)
3085 return s
->domain_name
;
3089 /* get the description of the system */
3091 char * ltt_trace_system_description_description (LttSystemDescription
* s
)
3093 return s
->description
;
3097 /* get the NTP corrected start time of the trace */
3098 LttTime
ltt_trace_start_time(LttTrace
*t
)
3100 return t
->start_time
;
3103 /* get the monotonic start time of the trace */
3104 LttTime
ltt_trace_start_time_monotonic(LttTrace
*t
)
3106 return t
->start_time_from_tsc
;
3109 LttTracefile
*ltt_tracefile_new()
3111 return g_new(LttTracefile
, 1);
3114 void ltt_tracefile_destroy(LttTracefile
*tf
)
3119 void ltt_tracefile_copy(LttTracefile
*dest
, const LttTracefile
*src
)
3124 /* Before library loading... */
3128 LTT_FACILITY_NAME_HEARTBEAT
= g_quark_from_string("heartbeat");
3129 LTT_EVENT_NAME_HEARTBEAT
= g_quark_from_string("heartbeat");
3130 LTT_EVENT_NAME_HEARTBEAT_FULL
= g_quark_from_string("heartbeat_full");
3132 LTT_TRACEFILE_NAME_FACILITIES
= g_quark_from_string("/control/facilities");