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/event.h>
48 #include <ltt/ltt-types.h>
49 #include <ltt/marker.h>
51 /* Tracefile names used in this file */
53 GQuark LTT_TRACEFILE_NAME_METADATA
;
60 #define __UNUSED__ __attribute__((__unused__))
62 #define g_info(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, format)
65 #define g_debug(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format)
70 /* Those macros must be called from within a function where page_size is a known
72 #define PAGE_MASK (~(page_size-1))
73 #define PAGE_ALIGN(addr) (((addr)+page_size-1)&PAGE_MASK)
75 LttTrace
*father_trace
= NULL
;
77 /* set the offset of the fields belonging to the event,
78 need the information of the archecture */
79 //void set_fields_offsets(LttTracefile *tf, LttEventType *event_type);
80 //size_t get_fields_offsets(LttTracefile *tf, LttEventType *event_type, void *data);
83 /* get the size of the field type according to
84 * The facility size information. */
85 static inline void preset_field_type_size(LttTracefile
*tf
,
86 LttEventType
*event_type
,
87 off_t offset_root
, off_t offset_parent
,
88 enum field_status
*fixed_root
, enum field_status
*fixed_parent
,
92 /* map a fixed size or a block information from the file (fd) */
93 static gint
map_block(LttTracefile
* tf
, guint block_num
);
95 /* calculate nsec per cycles for current block */
97 static guint32
calc_nsecs_per_cycle(LttTracefile
* t
);
98 static guint64
cycles_2_ns(LttTracefile
*tf
, guint64 cycles
);
101 /* go to the next event */
102 static int ltt_seek_next_event(LttTracefile
*tf
);
104 static int open_tracefiles(LttTrace
*trace
, gchar
*root_path
,
105 gchar
*relative_path
);
106 static int ltt_process_metadata_tracefile(LttTracefile
*tf
);
107 static void ltt_tracefile_time_span_get(LttTracefile
*tf
,
108 LttTime
*start
, LttTime
*end
);
109 static void group_time_span_get(GQuark name
, gpointer data
, gpointer user_data
);
110 static gint
map_block(LttTracefile
* tf
, guint block_num
);
111 static void ltt_update_event_size(LttTracefile
*tf
);
113 /* Enable event debugging */
114 static int a_event_debug
= 0;
116 void ltt_event_debug(int state
)
118 a_event_debug
= state
;
123 * Return value : 0 success, 1 bad tracefile
125 static int parse_trace_header(void *header
, LttTracefile
*tf
, LttTrace
*t
)
127 guint32
*magic_number
= (guint32
*)header
;
128 struct ltt_trace_header_any
*any
= (struct ltt_trace_header_any
*)header
;
130 if(*magic_number
== LTT_MAGIC_NUMBER
)
132 else if(*magic_number
== LTT_REV_MAGIC_NUMBER
)
134 else /* invalid magic number, bad tracefile ! */
137 /* Get float byte order : might be different from int byte order
138 * (or is set to 0 if the trace has no float (kernel trace)) */
139 tf
->float_word_order
= any
->float_word_order
;
140 tf
->alignment
= any
->alignment
;
143 t
->arch_type
= ltt_get_uint32(LTT_GET_BO(tf
),
145 t
->arch_variant
= ltt_get_uint32(LTT_GET_BO(tf
),
147 t
->arch_size
= any
->arch_size
;
148 t
->ltt_major_version
= any
->major_version
;
149 t
->ltt_minor_version
= any
->minor_version
;
150 t
->flight_recorder
= any
->flight_recorder
;
153 switch(any
->major_version
) {
156 g_warning("Unsupported trace version : %hhu.%hhu",
157 any
->major_version
, any
->minor_version
);
161 switch(any
->minor_version
) {
164 struct ltt_trace_header_2_0
*vheader
=
165 (struct ltt_trace_header_2_0
*)header
;
166 tf
->buffer_header_size
=
167 sizeof(struct ltt_block_start_header
)
168 + sizeof(struct ltt_trace_header_1_0
);
169 tf
->tscbits
= vheader
->tscbits
;
170 tf
->eventbits
= vheader
->eventbits
;
171 tf
->tsc_mask
= ((1ULL << tf
->tscbits
) - 1);
172 tf
->tsc_mask_next_bit
= (1ULL << tf
->tscbits
);
175 t
->start_freq
= ltt_get_uint64(LTT_GET_BO(tf
),
176 &vheader
->start_freq
);
177 t
->freq_scale
= ltt_get_uint32(LTT_GET_BO(tf
),
178 &vheader
->freq_scale
);
180 t
->start_freq
= father_trace
->start_freq
;
181 t
->freq_scale
= father_trace
->freq_scale
;
186 t
->start_tsc
= ltt_get_uint64(LTT_GET_BO(tf
),
187 &vheader
->start_tsc
);
188 t
->start_monotonic
= ltt_get_uint64(LTT_GET_BO(tf
),
189 &vheader
->start_monotonic
);
190 t
->start_time
.tv_sec
= ltt_get_uint64(LTT_GET_BO(tf
),
191 &vheader
->start_time_sec
);
192 t
->start_time
.tv_nsec
= ltt_get_uint64(LTT_GET_BO(tf
),
193 &vheader
->start_time_usec
);
194 t
->start_time
.tv_nsec
*= 1000; /* microsec to nanosec */
196 t
->start_time_from_tsc
= ltt_time_from_uint64(
198 * (1000000000.0 / tf
->trace
->freq_scale
)
199 / (double)t
->start_freq
);
204 g_warning("Unsupported trace version : %hhu.%hhu",
205 any
->major_version
, any
->minor_version
);
210 g_warning("Unsupported trace version : %hhu.%hhu",
211 any
->major_version
, any
->minor_version
);
221 /*****************************************************************************
223 * ltt_tracefile_open : open a trace file, construct a LttTracefile
225 * t : the trace containing the tracefile
226 * fileName : path name of the trace file
227 * tf : the tracefile structure
229 * : 0 for success, -1 otherwise.
230 ****************************************************************************/
232 static gint
ltt_tracefile_open(LttTrace
*t
, gchar
* fileName
, LttTracefile
*tf
)
234 struct stat lTDFStat
; /* Trace data file status */
235 struct ltt_block_start_header
*header
;
236 int page_size
= getpagesize();
239 tf
->long_name
= g_quark_from_string(fileName
);
241 tf
->fd
= open(fileName
, O_RDONLY
);
243 g_warning("Unable to open input data file %s\n", fileName
);
247 // Get the file's status
248 if(fstat(tf
->fd
, &lTDFStat
) < 0){
249 g_warning("Unable to get the status of the input data file %s\n", fileName
);
253 // Is the file large enough to contain a trace
254 if(lTDFStat
.st_size
<
255 (off_t
)(sizeof(struct ltt_block_start_header
)
256 + sizeof(struct ltt_trace_header_any
))){
257 g_print("The input data file %s does not contain a trace\n", fileName
);
261 /* Temporarily map the buffer start header to get trace information */
262 /* Multiple of pages aligned head */
263 tf
->buffer
.head
= mmap(0,
264 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)
265 + sizeof(struct ltt_trace_header_any
)), PROT_READ
,
266 MAP_PRIVATE
, tf
->fd
, 0);
267 if(tf
->buffer
.head
== MAP_FAILED
) {
268 perror("Error in allocating memory for buffer of tracefile");
271 g_assert( ( (gulong
)tf
->buffer
.head
&(8-1) ) == 0); // make sure it's aligned.
273 header
= (struct ltt_block_start_header
*)tf
->buffer
.head
;
275 if(parse_trace_header(header
->trace
, tf
, NULL
)) {
276 g_warning("parse_trace_header error");
280 //store the size of the file
281 tf
->file_size
= lTDFStat
.st_size
;
282 tf
->buf_size
= ltt_get_uint32(LTT_GET_BO(tf
), &header
->buf_size
);
283 tf
->num_blocks
= tf
->file_size
/ tf
->buf_size
;
285 if(munmap(tf
->buffer
.head
,
286 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)
287 + sizeof(struct ltt_trace_header_any
)))) {
288 g_warning("unmap size : %u\n",
289 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)
290 + sizeof(struct ltt_trace_header_any
)));
291 perror("munmap error");
294 tf
->buffer
.head
= NULL
;
296 //read the first block
297 if(map_block(tf
,0)) {
298 perror("Cannot map block for tracefile");
306 if(munmap(tf
->buffer
.head
,
307 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)
308 + sizeof(struct ltt_trace_header_any
)))) {
309 g_warning("unmap size : %u\n",
310 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)
311 + sizeof(struct ltt_trace_header_any
)));
312 perror("munmap error");
322 /*****************************************************************************
324 * ltt_tracefile_close: close a trace file,
326 * t : tracefile which will be closed
327 ****************************************************************************/
329 static void ltt_tracefile_close(LttTracefile
*t
)
331 int page_size
= getpagesize();
333 if(t
->buffer
.head
!= NULL
)
334 if(munmap(t
->buffer
.head
, PAGE_ALIGN(t
->buf_size
))) {
335 g_warning("unmap size : %u\n",
336 PAGE_ALIGN(t
->buf_size
));
337 perror("munmap error");
344 /****************************************************************************
345 * get_absolute_pathname
347 * return the unique pathname in the system
349 * MD : Fixed this function so it uses realpath, dealing well with
350 * forgotten cases (.. were not used correctly before).
352 ****************************************************************************/
353 void get_absolute_pathname(const gchar
*pathname
, gchar
* abs_pathname
)
355 abs_pathname
[0] = '\0';
357 if (realpath(pathname
, abs_pathname
) != NULL
)
361 /* error, return the original path unmodified */
362 strcpy(abs_pathname
, pathname
);
368 /* Search for something like : .*_.*
370 * The left side is the name, the right side is the number.
373 static int get_tracefile_name_number(gchar
*raw_name
,
380 guint raw_name_len
= strlen(raw_name
);
381 gchar char_name
[PATH_MAX
];
388 for(i
=raw_name_len
-1;i
>=0;i
--) {
389 if(raw_name
[i
] == '_') break;
391 if(i
==-1) { /* Either not found or name length is 0 */
392 /* This is a userspace tracefile */
393 strncpy(char_name
, raw_name
, raw_name_len
);
394 char_name
[raw_name_len
] = '\0';
395 *name
= g_quark_from_string(char_name
);
396 *num
= 0; /* unknown cpu */
397 for(i
=0;i
<raw_name_len
;i
++) {
398 if(raw_name
[i
] == '/') {
403 for(;i
<raw_name_len
;i
++) {
404 if(raw_name
[i
] == '/') {
409 for(;i
<raw_name_len
;i
++) {
410 if(raw_name
[i
] == '-') {
414 if(i
== raw_name_len
) return -1;
416 tmpptr
= &raw_name
[i
];
417 for(;i
<raw_name_len
;i
++) {
418 if(raw_name
[i
] == '.') {
423 *tid
= strtoul(tmpptr
, &endptr
, 10);
425 return -1; /* No digit */
426 if(*tid
== ULONG_MAX
)
427 return -1; /* underflow / overflow */
429 tmpptr
= &raw_name
[i
];
430 for(;i
<raw_name_len
;i
++) {
431 if(raw_name
[i
] == '.') {
436 *pgid
= strtoul(tmpptr
, &endptr
, 10);
438 return -1; /* No digit */
439 if(*pgid
== ULONG_MAX
)
440 return -1; /* underflow / overflow */
442 tmpptr
= &raw_name
[i
];
443 *creation
= strtoull(tmpptr
, &endptr
, 10);
445 return -1; /* No digit */
446 if(*creation
== G_MAXUINT64
)
447 return -1; /* underflow / overflow */
451 cpu_num
= strtol(raw_name
+underscore_pos
+1, &endptr
, 10);
453 if(endptr
== raw_name
+underscore_pos
+1)
454 return -1; /* No digit */
455 if(cpu_num
== LONG_MIN
|| cpu_num
== LONG_MAX
)
456 return -1; /* underflow / overflow */
458 strncpy(char_name
, raw_name
, underscore_pos
);
459 char_name
[underscore_pos
] = '\0';
461 *name
= g_quark_from_string(char_name
);
470 GData
**ltt_trace_get_tracefiles_groups(LttTrace
*trace
)
472 return &trace
->tracefiles
;
476 void compute_tracefile_group(GQuark key_id
,
478 struct compute_tracefile_group_args
*args
)
483 for(i
=0; i
<group
->len
; i
++) {
484 tf
= &g_array_index (group
, LttTracefile
, i
);
486 args
->func(tf
, args
->func_args
);
491 static void ltt_tracefile_group_destroy(gpointer data
)
493 GArray
*group
= (GArray
*)data
;
497 for(i
=0; i
<group
->len
; i
++) {
498 tf
= &g_array_index (group
, LttTracefile
, i
);
500 ltt_tracefile_close(tf
);
502 g_array_free(group
, TRUE
);
505 static gboolean
ltt_tracefile_group_has_cpu_online(gpointer data
)
507 GArray
*group
= (GArray
*)data
;
511 for(i
=0; i
<group
->len
; i
++) {
512 tf
= &g_array_index (group
, LttTracefile
, i
);
520 /* Open each tracefile under a specific directory. Put them in a
521 * GData : permits to access them using their tracefile group pathname.
522 * i.e. access control/modules tracefile group by index :
525 * relative path is the path relative to the trace root
526 * root path is the full path
528 * A tracefile group is simply an array where all the per cpu tracefiles sit.
531 static int open_tracefiles(LttTrace
*trace
, gchar
*root_path
, gchar
*relative_path
)
533 DIR *dir
= opendir(root_path
);
534 struct dirent
*entry
;
535 struct stat stat_buf
;
538 gchar path
[PATH_MAX
];
543 gchar rel_path
[PATH_MAX
];
552 strncpy(path
, root_path
, PATH_MAX
-1);
553 path_len
= strlen(path
);
554 path
[path_len
] = '/';
556 path_ptr
= path
+ path_len
;
558 strncpy(rel_path
, relative_path
, PATH_MAX
-1);
559 rel_path_len
= strlen(rel_path
);
560 rel_path
[rel_path_len
] = '/';
562 rel_path_ptr
= rel_path
+ rel_path_len
;
564 while((entry
= readdir(dir
)) != NULL
) {
566 if(entry
->d_name
[0] == '.') continue;
568 strncpy(path_ptr
, entry
->d_name
, PATH_MAX
- path_len
);
569 strncpy(rel_path_ptr
, entry
->d_name
, PATH_MAX
- rel_path_len
);
571 ret
= stat(path
, &stat_buf
);
577 g_debug("Tracefile file or directory : %s\n", path
);
579 // if(strcmp(rel_path, "/eventdefs") == 0) continue;
581 if(S_ISDIR(stat_buf
.st_mode
)) {
583 g_debug("Entering subdirectory...\n");
584 ret
= open_tracefiles(trace
, path
, rel_path
);
585 if(ret
< 0) continue;
586 } else if(S_ISREG(stat_buf
.st_mode
)) {
595 if(get_tracefile_name_number(rel_path
, &name
, &num
, &tid
, &pgid
, &creation
))
596 continue; /* invalid name */
598 g_debug("Opening file.\n");
599 if(ltt_tracefile_open(trace
, path
, &tmp_tf
)) {
600 g_info("Error opening tracefile %s", path
);
602 continue; /* error opening the tracefile : bad magic number ? */
605 g_debug("Tracefile name is %s and number is %u",
606 g_quark_to_string(name
), num
);
608 tmp_tf
.cpu_online
= 1;
609 tmp_tf
.cpu_num
= num
;
613 tmp_tf
.creation
= creation
;
614 group
= g_datalist_id_get_data(&trace
->tracefiles
, name
);
616 /* Elements are automatically cleared when the array is allocated.
617 * It makes the cpu_online variable set to 0 : cpu offline, by default.
619 group
= g_array_sized_new (FALSE
, TRUE
, sizeof(LttTracefile
), 10);
620 g_datalist_id_set_data_full(&trace
->tracefiles
, name
,
621 group
, ltt_tracefile_group_destroy
);
624 /* Add the per cpu tracefile to the named group */
625 unsigned int old_len
= group
->len
;
627 group
= g_array_set_size(group
, num
+1);
628 g_array_index (group
, LttTracefile
, num
) = tmp_tf
;
629 g_array_index (group
, LttTracefile
, num
).event
.tracefile
=
630 &g_array_index (group
, LttTracefile
, num
);
640 /* Presumes the tracefile is already seeked at the beginning. It makes sense,
641 * because it must be done just after the opening */
642 static int ltt_process_metadata_tracefile(LttTracefile
*tf
)
648 err
= ltt_tracefile_read_seek(tf
);
649 if(err
== EPERM
) goto seek_error
;
650 else if(err
== ERANGE
) break; /* End of tracefile */
652 err
= ltt_tracefile_read_update_event(tf
);
653 if(err
) goto update_error
;
656 * It contains only core events :
658 * 1 : set_marker_format
660 if(tf
->event
.event_id
>= MARKER_CORE_IDS
) {
661 /* Should only contain core events */
662 g_warning("Error in processing metadata file %s, "
663 "should not contain event id %u.", g_quark_to_string(tf
->name
),
669 const char *marker_name
, *format
;
671 guint8 int_size
, long_size
, pointer_size
, size_t_size
, alignment
;
673 switch((enum marker_id
)tf
->event
.event_id
) {
674 case MARKER_ID_SET_MARKER_ID
:
675 marker_name
= pos
= tf
->event
.data
;
676 g_debug("Doing MARKER_ID_SET_MARKER_ID of marker %s", marker_name
);
677 pos
+= strlen(marker_name
) + 1;
678 pos
+= ltt_align((size_t)pos
, sizeof(guint16
), tf
->alignment
);
679 id
= ltt_get_uint16(LTT_GET_BO(tf
), pos
);
680 g_debug("In MARKER_ID_SET_MARKER_ID of marker %s id %hu",
682 pos
+= sizeof(guint16
);
683 int_size
= *(guint8
*)pos
;
684 pos
+= sizeof(guint8
);
685 long_size
= *(guint8
*)pos
;
686 pos
+= sizeof(guint8
);
687 pointer_size
= *(guint8
*)pos
;
688 pos
+= sizeof(guint8
);
689 size_t_size
= *(guint8
*)pos
;
690 pos
+= sizeof(guint8
);
691 alignment
= *(guint8
*)pos
;
692 pos
+= sizeof(guint8
);
693 marker_id_event(tf
->trace
, g_quark_from_string(marker_name
),
694 id
, int_size
, long_size
,
695 pointer_size
, size_t_size
, alignment
);
697 case MARKER_ID_SET_MARKER_FORMAT
:
698 marker_name
= pos
= tf
->event
.data
;
699 g_debug("Doing MARKER_ID_SET_MARKER_FORMAT of marker %s",
701 pos
+= strlen(marker_name
) + 1;
703 pos
+= strlen(format
) + 1;
704 marker_format_event(tf
->trace
, g_quark_from_string(marker_name
),
706 /* get information from dictionary TODO */
709 g_warning("Error in processing metadata file %s, "
710 "unknown event id %hhu.",
711 g_quark_to_string(tf
->name
),
724 g_warning("An error occured in metadata tracefile parsing");
729 * Open a trace and return its LttTrace handle.
731 * pathname must be the directory of the trace
734 LttTrace
*ltt_trace_open(const gchar
*pathname
)
736 gchar abs_path
[PATH_MAX
];
741 struct ltt_block_start_header
*header
;
743 struct dirent
*entry
;
744 guint control_found
= 0;
745 struct stat stat_buf
;
746 gchar path
[PATH_MAX
];
748 t
= g_new(LttTrace
, 1);
749 if(!t
) goto alloc_error
;
751 get_absolute_pathname(pathname
, abs_path
);
752 t
->pathname
= g_quark_from_string(abs_path
);
754 g_datalist_init(&t
->tracefiles
);
756 /* Test to see if it looks like a trace */
757 dir
= opendir(abs_path
);
762 while((entry
= readdir(dir
)) != NULL
) {
763 strcpy(path
, abs_path
);
765 strcat(path
, entry
->d_name
);
766 ret
= stat(path
, &stat_buf
);
771 if(S_ISDIR(stat_buf
.st_mode
)) {
772 if(strcmp(entry
->d_name
, "control") == 0) {
779 if(!control_found
) goto find_error
;
781 /* Open all the tracefiles */
782 if(open_tracefiles(t
, abs_path
, "")) {
783 g_warning("Error opening tracefile %s", abs_path
);
787 /* Parse each trace control/metadata_N files : get runtime fac. info */
788 group
= g_datalist_id_get_data(&t
->tracefiles
, LTT_TRACEFILE_NAME_METADATA
);
790 g_error("Trace %s has no metadata tracefile", abs_path
);
795 /* Get the trace information for the control/metadata_0 tracefile */
796 g_assert(group
->len
> 0);
797 tf
= &g_array_index (group
, LttTracefile
, 0);
798 header
= (struct ltt_block_start_header
*)tf
->buffer
.head
;
799 g_assert(parse_trace_header(header
->trace
,
802 t
->num_cpu
= group
->len
;
804 ret
= allocate_marker_data(t
);
806 g_error("Error in allocating marker data");
808 for(i
=0; i
<group
->len
; i
++) {
809 tf
= &g_array_index (group
, LttTracefile
, i
);
811 if(ltt_process_metadata_tracefile(tf
))
819 destroy_marker_data(t
);
821 g_datalist_clear(&t
->tracefiles
);
829 /* Open another, completely independant, instance of a trace.
831 * A read on this new instance will read the first event of the trace.
833 * When we copy a trace, we want all the opening actions to happen again :
834 * the trace will be reopened and totally independant from the original.
835 * That's why we call ltt_trace_open.
837 LttTrace
*ltt_trace_copy(LttTrace
*self
)
839 return ltt_trace_open(g_quark_to_string(self
->pathname
));
846 void ltt_trace_close(LttTrace
*t
)
848 g_datalist_clear(&t
->tracefiles
);
853 /*****************************************************************************
854 * Get the start time and end time of the trace
855 ****************************************************************************/
857 void ltt_tracefile_time_span_get(LttTracefile
*tf
,
858 LttTime
*start
, LttTime
*end
)
862 err
= map_block(tf
, 0);
864 g_error("Can not map block");
865 *start
= ltt_time_infinite
;
867 *start
= tf
->buffer
.begin
.timestamp
;
869 err
= map_block(tf
, tf
->num_blocks
- 1); /* Last block */
871 g_error("Can not map block");
872 *end
= ltt_time_zero
;
874 *end
= tf
->buffer
.end
.timestamp
;
877 struct tracefile_time_span_get_args
{
883 static void group_time_span_get(GQuark name
, gpointer data
, gpointer user_data
)
885 struct tracefile_time_span_get_args
*args
=
886 (struct tracefile_time_span_get_args
*)user_data
;
888 GArray
*group
= (GArray
*)data
;
894 for(i
=0; i
<group
->len
; i
++) {
895 tf
= &g_array_index (group
, LttTracefile
, i
);
897 ltt_tracefile_time_span_get(tf
, &tmp_start
, &tmp_end
);
898 if(ltt_time_compare(*args
->start
, tmp_start
)>0) *args
->start
= tmp_start
;
899 if(ltt_time_compare(*args
->end
, tmp_end
)<0) *args
->end
= tmp_end
;
904 /* return the start and end time of a trace */
906 void ltt_trace_time_span_get(LttTrace
*t
, LttTime
*start
, LttTime
*end
)
908 LttTime min_start
= ltt_time_infinite
;
909 LttTime max_end
= ltt_time_zero
;
910 struct tracefile_time_span_get_args args
= { t
, &min_start
, &max_end
};
912 g_datalist_foreach(&t
->tracefiles
, &group_time_span_get
, &args
);
914 if(start
!= NULL
) *start
= min_start
;
915 if(end
!= NULL
) *end
= max_end
;
920 /* Seek to the first event in a tracefile that has a time equal or greater than
921 * the time passed in parameter.
923 * If the time parameter is outside the tracefile time span, seek to the first
924 * event or if after, return ERANGE.
926 * If the time parameter is before the first event, we have to seek specially to
929 * If the time is after the end of the trace, return ERANGE.
931 * Do a binary search to find the right block, then a sequential search in the
932 * block to find the event.
934 * In the special case where the time requested fits inside a block that has no
935 * event corresponding to the requested time, the first event of the next block
938 * IMPORTANT NOTE : // FIXME everywhere...
940 * You MUST NOT do a ltt_tracefile_read right after a ltt_tracefile_seek_time :
941 * you will jump over an event if you do.
943 * Return value : 0 : no error, the tf->event can be used
944 * ERANGE : time if after the last event of the trace
945 * otherwise : this is an error.
949 int ltt_tracefile_seek_time(LttTracefile
*tf
, LttTime time
)
953 unsigned int block_num
, high
, low
;
955 /* seek at the beginning of trace */
956 err
= map_block(tf
, 0); /* First block */
958 g_error("Can not map block");
962 /* If the time is lower or equal the beginning of the trace,
963 * go to the first event. */
964 if(ltt_time_compare(time
, tf
->buffer
.begin
.timestamp
) <= 0) {
965 ret
= ltt_tracefile_read(tf
);
966 if(ret
== ERANGE
) goto range
;
967 else if (ret
) goto fail
;
968 goto found
; /* There is either no event in the trace or the event points
969 to the first event in the trace */
972 err
= map_block(tf
, tf
->num_blocks
- 1); /* Last block */
974 g_error("Can not map block");
978 /* If the time is after the end of the trace, return ERANGE. */
979 if(ltt_time_compare(time
, tf
->buffer
.end
.timestamp
) > 0) {
983 /* Binary search the block */
984 high
= tf
->num_blocks
- 1;
988 block_num
= ((high
-low
) / 2) + low
;
990 err
= map_block(tf
, block_num
);
992 g_error("Can not map block");
996 /* We cannot divide anymore : this is what would happen if the time
997 * requested was exactly between two consecutive buffers'end and start
998 * timestamps. This is also what would happend if we didn't deal with out
999 * of span cases prior in this function. */
1000 /* The event is right in the buffer!
1001 * (or in the next buffer first event) */
1003 ret
= ltt_tracefile_read(tf
);
1004 if(ret
== ERANGE
) goto range
; /* ERANGE or EPERM */
1005 else if(ret
) goto fail
;
1007 if(ltt_time_compare(time
, tf
->event
.event_time
) <= 0)
1011 } else if(ltt_time_compare(time
, tf
->buffer
.begin
.timestamp
) < 0) {
1012 /* go to lower part */
1013 high
= block_num
- 1;
1014 } else if(ltt_time_compare(time
, tf
->buffer
.end
.timestamp
) > 0) {
1015 /* go to higher part */
1016 low
= block_num
+ 1;
1017 } else {/* The event is right in the buffer!
1018 (or in the next buffer first event) */
1020 ret
= ltt_tracefile_read(tf
);
1021 if(ret
== ERANGE
) goto range
; /* ERANGE or EPERM */
1022 else if(ret
) goto fail
;
1024 if(ltt_time_compare(time
, tf
->event
.event_time
) <= 0)
1036 /* Error handling */
1038 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1039 g_quark_to_string(tf
->name
));
1043 /* Seek to a position indicated by an LttEventPosition
1046 int ltt_tracefile_seek_position(LttTracefile
*tf
, const LttEventPosition
*ep
)
1050 if(ep
->tracefile
!= tf
) {
1054 err
= map_block(tf
, ep
->block
);
1056 g_error("Can not map block");
1060 tf
->event
.offset
= ep
->offset
;
1062 /* Put back the event real tsc */
1063 tf
->event
.tsc
= ep
->tsc
;
1064 tf
->buffer
.tsc
= ep
->tsc
;
1066 err
= ltt_tracefile_read_update_event(tf
);
1069 /* deactivate this, as it does nothing for now
1070 err = ltt_tracefile_read_op(tf);
1077 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1078 g_quark_to_string(tf
->name
));
1082 /* Given a TSC value, return the LttTime (seconds,nanoseconds) it
1086 LttTime
ltt_interpolate_time_from_tsc(LttTracefile
*tf
, guint64 tsc
)
1090 if(tsc
> tf
->trace
->start_tsc
) {
1091 time
= ltt_time_from_uint64(
1092 (double)(tsc
- tf
->trace
->start_tsc
)
1093 * (1000000000.0 / tf
->trace
->freq_scale
)
1094 / (double)tf
->trace
->start_freq
);
1095 time
= ltt_time_add(tf
->trace
->start_time_from_tsc
, time
);
1097 time
= ltt_time_from_uint64(
1098 (double)(tf
->trace
->start_tsc
- tsc
)
1099 * (1000000000.0 / tf
->trace
->freq_scale
)
1100 / (double)tf
->trace
->start_freq
);
1101 time
= ltt_time_sub(tf
->trace
->start_time_from_tsc
, time
);
1106 /* Calculate the real event time based on the buffer boundaries */
1107 LttTime
ltt_interpolate_time(LttTracefile
*tf
, LttEvent
*event
)
1109 return ltt_interpolate_time_from_tsc(tf
, tf
->buffer
.tsc
);
1113 /* Get the current event of the tracefile : valid until the next read */
1114 LttEvent
*ltt_tracefile_get_event(LttTracefile
*tf
)
1121 /*****************************************************************************
1123 * ltt_tracefile_read : Read the next event in the tracefile
1128 * Returns 0 if an event can be used in tf->event.
1129 * Returns ERANGE on end of trace. The event in tf->event still can be used
1130 * (if the last block was not empty).
1131 * Returns EPERM on error.
1133 * This function does make the tracefile event structure point to the event
1134 * currently pointed to by the tf->event.
1136 * Note : you must call a ltt_tracefile_seek to the beginning of the trace to
1137 * reinitialize it after an error if you want results to be coherent.
1138 * It would be the case if a end of trace last buffer has no event : the end
1139 * of trace wouldn't be returned, but an error.
1140 * We make the assumption there is at least one event per buffer.
1141 ****************************************************************************/
1143 int ltt_tracefile_read(LttTracefile
*tf
)
1147 err
= ltt_tracefile_read_seek(tf
);
1149 err
= ltt_tracefile_read_update_event(tf
);
1152 /* deactivate this, as it does nothing for now
1153 err = ltt_tracefile_read_op(tf);
1160 int ltt_tracefile_read_seek(LttTracefile
*tf
)
1164 /* Get next buffer until we finally have an event, or end of trace */
1166 err
= ltt_seek_next_event(tf
);
1167 if(unlikely(err
== ENOPROTOOPT
)) {
1171 /* Are we at the end of the buffer ? */
1173 if(unlikely(tf
->buffer
.index
== tf
->num_blocks
-1)){ /* end of trace ? */
1176 /* get next block */
1177 err
= map_block(tf
, tf
->buffer
.index
+ 1);
1179 g_error("Can not map block");
1183 } else break; /* We found an event ! */
1189 /* do an operation when reading a new event */
1191 /* This function does nothing for now */
1193 int ltt_tracefile_read_op(LttTracefile
*tf
)
1199 /* do event specific operation */
1207 static void print_debug_event_header(LttEvent
*ev
, void *start_pos
, void *end_pos
)
1209 unsigned int offset
= 0;
1212 g_printf("Event header (tracefile %s offset %llx):\n",
1213 g_quark_to_string(ev
->tracefile
->long_name
),
1214 ((uint64_t)ev
->tracefile
->buffer
.index
* ev
->tracefile
->buf_size
)
1215 + (long)start_pos
- (long)ev
->tracefile
->buffer
.head
);
1217 while (offset
< (long)end_pos
- (long)start_pos
) {
1218 g_printf("%8lx", (long)start_pos
- (long)ev
->tracefile
->buffer
.head
+ offset
);
1221 for (i
= 0; i
< 4 ; i
++) {
1222 for (j
= 0; j
< 4; j
++) {
1223 if (offset
+ ((i
* 4) + j
) <
1224 (long)end_pos
- (long)start_pos
)
1226 ((char*)start_pos
)[offset
+ ((i
* 4) + j
)]);
1240 /* same as ltt_tracefile_read, but does not seek to the next event nor call
1241 * event specific operation. */
1242 int ltt_tracefile_read_update_event(LttTracefile
*tf
)
1249 pos
= tf
->buffer
.head
+ event
->offset
;
1251 /* Read event header */
1253 /* Align the head */
1254 pos
+= ltt_align((size_t)pos
, sizeof(guint32
), tf
->alignment
);
1257 event
->timestamp
= ltt_get_uint32(LTT_GET_BO(tf
), pos
);
1258 event
->event_id
= event
->timestamp
>> tf
->tscbits
;
1259 event
->timestamp
= event
->timestamp
& tsc_mask
;
1260 pos
+= sizeof(guint32
);
1262 switch (event
->event_id
) {
1263 case 29: /* LTT_RFLAG_ID_SIZE_TSC */
1264 event
->event_id
= ltt_get_uint16(LTT_GET_BO(tf
), pos
);
1265 pos
+= sizeof(guint16
);
1266 event
->event_size
= ltt_get_uint16(LTT_GET_BO(tf
), pos
);
1267 pos
+= sizeof(guint16
);
1268 if (event
->event_size
== 0xFFFF) {
1269 event
->event_size
= ltt_get_uint32(LTT_GET_BO(tf
), pos
);
1270 pos
+= sizeof(guint32
);
1272 pos
+= ltt_align((size_t)pos
, sizeof(guint64
), tf
->alignment
);
1273 tf
->buffer
.tsc
= ltt_get_uint64(LTT_GET_BO(tf
), pos
);
1274 pos
+= sizeof(guint64
);
1276 case 30: /* LTT_RFLAG_ID_SIZE */
1277 event
->event_id
= ltt_get_uint16(LTT_GET_BO(tf
), pos
);
1278 pos
+= sizeof(guint16
);
1279 event
->event_size
= ltt_get_uint16(LTT_GET_BO(tf
), pos
);
1280 pos
+= sizeof(guint16
);
1281 if (event
->event_size
== 0xFFFF) {
1282 event
->event_size
= ltt_get_uint32(LTT_GET_BO(tf
), pos
);
1283 pos
+= sizeof(guint32
);
1286 case 31: /* LTT_RFLAG_ID */
1287 event
->event_id
= ltt_get_uint16(LTT_GET_BO(tf
), pos
);
1288 pos
+= sizeof(guint16
);
1289 event
->event_size
= G_MAXUINT
;
1292 event
->event_size
= G_MAXUINT
;
1296 if (likely(event
->event_id
!= 29)) {
1297 /* No extended timestamp */
1298 if (event
->timestamp
< (tf
->buffer
.tsc
& tf
->tsc_mask
))
1299 tf
->buffer
.tsc
= ((tf
->buffer
.tsc
& ~tf
->tsc_mask
) /* overflow */
1300 + tf
->tsc_mask_next_bit
)
1301 | (guint64
)event
->timestamp
;
1303 tf
->buffer
.tsc
= (tf
->buffer
.tsc
& ~tf
->tsc_mask
) /* no overflow */
1304 | (guint64
)event
->timestamp
;
1306 event
->tsc
= tf
->buffer
.tsc
;
1308 event
->event_time
= ltt_interpolate_time(tf
, event
);
1311 print_debug_event_header(event
, pos_aligned
, pos
);
1316 * Let ltt_update_event_size update event->data according to the largest
1317 * alignment within the payload.
1318 * Get the data size and update the event fields with the current
1320 ltt_update_event_size(tf
);
1326 /****************************************************************************
1328 * map_block : map a block from the file
1330 * lttdes : ltt trace file
1331 * whichBlock : the block which will be read
1334 * EINVAL : lseek fail
1335 * EIO : can not read from the file
1336 ****************************************************************************/
1338 static gint
map_block(LttTracefile
* tf
, guint block_num
)
1340 int page_size
= getpagesize();
1341 struct ltt_block_start_header
*header
;
1343 g_assert(block_num
< tf
->num_blocks
);
1345 if(tf
->buffer
.head
!= NULL
) {
1346 if(munmap(tf
->buffer
.head
, PAGE_ALIGN(tf
->buf_size
))) {
1347 g_warning("unmap size : %u\n",
1348 PAGE_ALIGN(tf
->buf_size
));
1349 perror("munmap error");
1354 /* Multiple of pages aligned head */
1355 tf
->buffer
.head
= mmap(0,
1356 PAGE_ALIGN(tf
->buf_size
),
1357 PROT_READ
, MAP_PRIVATE
, tf
->fd
,
1358 PAGE_ALIGN((off_t
)tf
->buf_size
* (off_t
)block_num
));
1360 if(tf
->buffer
.head
== MAP_FAILED
) {
1361 perror("Error in allocating memory for buffer of tracefile");
1365 g_assert( ( (gulong
)tf
->buffer
.head
&(8-1) ) == 0); // make sure it's aligned.
1368 tf
->buffer
.index
= block_num
;
1370 header
= (struct ltt_block_start_header
*)tf
->buffer
.head
;
1372 tf
->buffer
.begin
.cycle_count
= ltt_get_uint64(LTT_GET_BO(tf
),
1373 &header
->begin
.cycle_count
);
1374 tf
->buffer
.begin
.freq
= ltt_get_uint64(LTT_GET_BO(tf
),
1375 &header
->begin
.freq
);
1376 if(tf
->buffer
.begin
.freq
== 0)
1377 tf
->buffer
.begin
.freq
= tf
->trace
->start_freq
;
1379 tf
->buffer
.begin
.timestamp
= ltt_interpolate_time_from_tsc(tf
,
1380 tf
->buffer
.begin
.cycle_count
);
1381 tf
->buffer
.end
.cycle_count
= ltt_get_uint64(LTT_GET_BO(tf
),
1382 &header
->end
.cycle_count
);
1383 tf
->buffer
.end
.freq
= ltt_get_uint64(LTT_GET_BO(tf
),
1385 if(tf
->buffer
.end
.freq
== 0)
1386 tf
->buffer
.end
.freq
= tf
->trace
->start_freq
;
1388 tf
->buffer
.lost_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1389 &header
->lost_size
);
1390 tf
->buffer
.end
.timestamp
= ltt_interpolate_time_from_tsc(tf
,
1391 tf
->buffer
.end
.cycle_count
);
1392 tf
->buffer
.tsc
= tf
->buffer
.begin
.cycle_count
;
1393 tf
->event
.tsc
= tf
->buffer
.tsc
;
1394 tf
->buffer
.freq
= tf
->buffer
.begin
.freq
;
1397 * eventually support variable buffer size : will need a partial pre-read of
1398 * the headers to create an index when we open the trace... eventually. */
1399 g_assert(tf
->buf_size
== ltt_get_uint32(LTT_GET_BO(tf
),
1400 &header
->buf_size
));
1402 /* Make the current event point to the beginning of the buffer :
1403 * it means that the event read must get the first event. */
1404 tf
->event
.tracefile
= tf
;
1405 tf
->event
.block
= block_num
;
1406 tf
->event
.offset
= 0;
1414 static void print_debug_event_data(LttEvent
*ev
)
1416 unsigned int offset
= 0;
1419 if (!max(ev
->event_size
, ev
->data_size
))
1422 g_printf("Event data (tracefile %s offset %llx):\n",
1423 g_quark_to_string(ev
->tracefile
->long_name
),
1424 ((uint64_t)ev
->tracefile
->buffer
.index
* ev
->tracefile
->buf_size
)
1425 + (long)ev
->data
- (long)ev
->tracefile
->buffer
.head
);
1427 while (offset
< max(ev
->event_size
, ev
->data_size
)) {
1428 g_printf("%8lx", (long)ev
->data
+ offset
1429 - (long)ev
->tracefile
->buffer
.head
);
1432 for (i
= 0; i
< 4 ; i
++) {
1433 for (j
= 0; j
< 4; j
++) {
1434 if (offset
+ ((i
* 4) + j
) < max(ev
->event_size
, ev
->data_size
))
1435 g_printf("%02hhX", ((char*)ev
->data
)[offset
+ ((i
* 4) + j
)]);
1446 for (i
= 0; i
< 4; i
++) {
1447 for (j
= 0; j
< 4; j
++) {
1448 if (offset
+ ((i
* 4) + j
) < max(ev
->event_size
, ev
->data_size
)) {
1449 if (isprint(((char*)ev
->data
)[offset
+ ((i
* 4) + j
)]))
1450 g_printf("%c", ((char*)ev
->data
)[offset
+ ((i
* 4) + j
)]);
1462 /* It will update the fields offsets too */
1463 void ltt_update_event_size(LttTracefile
*tf
)
1467 struct marker_info
*info
;
1469 switch((enum marker_id
)tf
->event
.event_id
) {
1470 case MARKER_ID_SET_MARKER_ID
:
1471 size
= strlen((char*)tf
->event
.data
) + 1;
1472 g_debug("marker %s id set", (char*)tf
->event
.data
);
1473 size
+= ltt_align(size
, sizeof(guint16
), tf
->alignment
);
1474 size
+= sizeof(guint16
);
1475 size
+= sizeof(guint8
);
1476 size
+= sizeof(guint8
);
1477 size
+= sizeof(guint8
);
1478 size
+= sizeof(guint8
);
1479 size
+= sizeof(guint8
);
1481 case MARKER_ID_SET_MARKER_FORMAT
:
1482 g_debug("marker %s format set", (char*)tf
->event
.data
);
1483 size
= strlen((char*)tf
->event
.data
) + 1;
1484 size
+= strlen((char*)tf
->event
.data
+ size
) + 1;
1488 info
= marker_get_info_from_id(tf
->trace
, tf
->event
.event_id
);
1490 if (tf
->event
.event_id
>= MARKER_CORE_IDS
)
1491 g_assert(info
!= NULL
);
1493 /* Do not update field offsets of core markers when initially reading the
1494 * metadata tracefile when the infos about these markers do not exist yet.
1496 if (likely(info
&& info
->fields
)) {
1498 event
->data
+= ltt_align((off_t
)event
->data
, info
->largest_align
,
1500 /* size, dynamically computed */
1501 if (info
->size
!= -1)
1504 size
= marker_update_fields_offsets(marker_get_info_from_id(tf
->trace
,
1505 tf
->event
.event_id
), tf
->event
.data
);
1508 tf
->event
.data_size
= size
;
1510 /* Check consistency between kernel and LTTV structure sizes */
1511 if(tf
->event
.event_size
== G_MAXUINT
) {
1512 /* Event size too big to fit in the event size field */
1513 tf
->event
.event_size
= tf
->event
.data_size
;
1517 print_debug_event_data(&tf
->event
);
1519 /* Having a marker load or marker format event out of the metadata
1520 * tracefiles is a serious bug. */
1521 switch((enum marker_id
)tf
->event
.event_id
) {
1522 case MARKER_ID_SET_MARKER_ID
:
1523 case MARKER_ID_SET_MARKER_FORMAT
:
1524 if (tf
->name
!= g_quark_from_string("/control/metadata"))
1525 g_error("Trace inconsistency : metadata event found in data "
1526 "tracefile %s", g_quark_to_string(tf
->long_name
));
1529 if (tf
->event
.data_size
!= tf
->event
.event_size
) {
1530 struct marker_info
*info
= marker_get_info_from_id(tf
->trace
,
1531 tf
->event
.event_id
);
1532 g_error("Kernel/LTTV event size differs for event %s: kernel %u, LTTV %u",
1533 g_quark_to_string(info
->name
),
1534 tf
->event
.event_size
, tf
->event
.data_size
);
1540 /* Take the tf current event offset and use the event id to figure out where is
1541 * the next event offset.
1543 * This is an internal function not aiming at being used elsewhere : it will
1544 * not jump over the current block limits. Please consider using
1545 * ltt_tracefile_read to do this.
1547 * Returns 0 on success
1548 * ERANGE if we are at the end of the buffer.
1549 * ENOPROTOOPT if an error occured when getting the current event size.
1551 static int ltt_seek_next_event(LttTracefile
*tf
)
1556 /* seek over the buffer header if we are at the buffer start */
1557 if(tf
->event
.offset
== 0) {
1558 tf
->event
.offset
+= tf
->buffer_header_size
;
1560 if(tf
->event
.offset
== tf
->buf_size
- tf
->buffer
.lost_size
) {
1566 pos
= tf
->event
.data
;
1568 if(tf
->event
.data_size
< 0) goto error
;
1570 pos
+= (size_t)tf
->event
.data_size
;
1572 tf
->event
.offset
= pos
- tf
->buffer
.head
;
1574 if(tf
->event
.offset
== tf
->buf_size
- tf
->buffer
.lost_size
) {
1578 g_assert(tf
->event
.offset
< tf
->buf_size
- tf
->buffer
.lost_size
);
1584 g_error("Error in ltt_seek_next_event for tracefile %s",
1585 g_quark_to_string(tf
->name
));
1590 /*****************************************************************************
1592 * set_fields_offsets : set the precomputable offset of the fields
1594 * tracefile : opened trace file
1595 * event_type : the event type
1596 ****************************************************************************/
1598 void set_fields_offsets(LttTracefile
*tf
, LttEventType
*event_type
)
1600 LttField
*field
= event_type
->root_field
;
1601 enum field_status fixed_root
= FIELD_FIXED
, fixed_parent
= FIELD_FIXED
;
1604 preset_field_type_size(tf
, event_type
, 0, 0,
1605 &fixed_root
, &fixed_parent
,
1612 /*****************************************************************************
1614 * get_alignment : Get the alignment needed for a field.
1618 * returns : The size on which it must be aligned.
1620 ****************************************************************************/
1622 off_t
get_alignment(LttField
*field
)
1624 LttType
*type
= &field
->field_type
;
1626 switch(type
->type_class
) {
1628 case LTT_UINT_FIXED
:
1643 /* Align offset on type size */
1644 g_assert(field
->field_size
!= 0);
1645 return field
->field_size
;
1651 g_assert(type
->fields
->len
== 1);
1653 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
1654 return get_alignment(child
);
1658 g_assert(type
->fields
->len
== 2);
1661 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
1663 localign
= max(localign
, get_alignment(child
));
1665 child
= &g_array_index(type
->fields
, LttField
, 1);
1666 localign
= max(localign
, get_alignment(child
));
1677 for(i
=0; i
<type
->fields
->len
; i
++) {
1678 LttField
*child
= &g_array_index(type
->fields
, LttField
, i
);
1679 localign
= max(localign
, get_alignment(child
));
1686 g_error("get_alignment : unknown type");
1693 /*****************************************************************************
1695 * field_compute_static_size : Determine the size of fields known by their
1696 * sole definition. Unions, arrays and struct sizes might be known, but
1697 * the parser does not give that information.
1702 ****************************************************************************/
1704 void field_compute_static_size(LttFacility
*fac
, LttField
*field
)
1706 LttType
*type
= &field
->field_type
;
1708 switch(type
->type_class
) {
1710 case LTT_UINT_FIXED
:
1729 /* note this : array type size is the number of elements in the array,
1730 * while array field size of the length of the array in bytes */
1731 g_assert(type
->fields
->len
== 1);
1733 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
1734 field_compute_static_size(fac
, child
);
1736 if(child
->field_size
!= 0) {
1737 field
->field_size
= type
->size
* child
->field_size
;
1738 field
->dynamic_offsets
= g_array_sized_new(FALSE
, TRUE
,
1739 sizeof(off_t
), type
->size
);
1741 field
->field_size
= 0;
1746 g_assert(type
->fields
->len
== 2);
1748 off_t local_offset
= 0;
1749 LttField
*child
= &g_array_index(type
->fields
, LttField
, 1);
1750 field_compute_static_size(fac
, child
);
1751 field
->field_size
= 0;
1753 if(child
->field_size
!= 0) {
1754 field
->dynamic_offsets
= g_array_sized_new(FALSE
, TRUE
,
1755 sizeof(off_t
), SEQUENCE_AVG_ELEMENTS
);
1763 for(i
=0;i
<type
->fields
->len
;i
++) {
1764 LttField
*child
= &g_array_index(type
->fields
, LttField
, i
);
1765 field_compute_static_size(fac
, child
);
1766 if(child
->field_size
!= 0) {
1767 type
->size
+= ltt_align(type
->size
, get_alignment(child
),
1769 type
->size
+= child
->field_size
;
1771 /* As soon as we find a child with variable size, we have
1772 * a variable size */
1777 field
->field_size
= type
->size
;
1781 g_error("field_static_size : unknown type");
1788 /*****************************************************************************
1790 * precompute_fields_offsets : set the precomputable offset of the fields
1794 * offset : pointer to the current offset, must be incremented
1796 * return : 1 : found a variable length field, stop the processing.
1798 ****************************************************************************/
1801 gint
precompute_fields_offsets(LttFacility
*fac
, LttField
*field
, off_t
*offset
, gint is_compact
)
1803 LttType
*type
= &field
->field_type
;
1805 if(unlikely(is_compact
)) {
1806 g_assert(field
->field_size
!= 0);
1807 /* FIXME THIS IS A HUUUUUGE hack :
1808 * offset is between the compact_data field in struct LttEvent
1809 * and the address of the field root in the memory map.
1810 * ark. Both will stay at the same addresses while the event
1811 * is readable, so it's ok.
1813 field
->offset_root
= 0;
1814 field
->fixed_root
= FIELD_FIXED
;
1818 switch(type
->type_class
) {
1820 case LTT_UINT_FIXED
:
1835 g_assert(field
->field_size
!= 0);
1836 /* Align offset on type size */
1837 *offset
+= ltt_align(*offset
, get_alignment(field
),
1839 /* remember offset */
1840 field
->offset_root
= *offset
;
1841 field
->fixed_root
= FIELD_FIXED
;
1842 /* Increment offset */
1843 *offset
+= field
->field_size
;
1847 field
->offset_root
= *offset
;
1848 field
->fixed_root
= FIELD_FIXED
;
1852 g_assert(type
->fields
->len
== 1);
1854 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
1856 *offset
+= ltt_align(*offset
, get_alignment(field
),
1859 /* remember offset */
1860 field
->offset_root
= *offset
;
1861 field
->array_offset
= *offset
;
1862 field
->fixed_root
= FIELD_FIXED
;
1864 /* Let the child be variable */
1865 //precompute_fields_offsets(tf, child, offset);
1867 if(field
->field_size
!= 0) {
1868 /* Increment offset */
1869 /* field_size is the array size in bytes */
1870 *offset
+= field
->field_size
;
1878 g_assert(type
->fields
->len
== 2);
1883 *offset
+= ltt_align(*offset
, get_alignment(field
),
1886 /* remember offset */
1887 field
->offset_root
= *offset
;
1888 field
->fixed_root
= FIELD_FIXED
;
1890 child
= &g_array_index(type
->fields
, LttField
, 0);
1891 ret
= precompute_fields_offsets(fac
, child
, offset
, is_compact
);
1892 g_assert(ret
== 0); /* Seq len cannot have variable len */
1894 child
= &g_array_index(type
->fields
, LttField
, 1);
1895 *offset
+= ltt_align(*offset
, get_alignment(child
),
1897 field
->array_offset
= *offset
;
1898 /* Let the child be variable. */
1899 //ret = precompute_fields_offsets(fac, child, offset);
1901 /* Cannot precompute fields offsets of sequence members, and has
1902 * variable length. */
1912 *offset
+= ltt_align(*offset
, get_alignment(field
),
1914 /* remember offset */
1915 field
->offset_root
= *offset
;
1916 field
->fixed_root
= FIELD_FIXED
;
1918 for(i
=0; i
< type
->fields
->len
; i
++) {
1919 child
= &g_array_index(type
->fields
, LttField
, i
);
1920 ret
= precompute_fields_offsets(fac
, child
, offset
, is_compact
);
1933 *offset
+= ltt_align(*offset
, get_alignment(field
),
1935 /* remember offset */
1936 field
->offset_root
= *offset
;
1937 field
->fixed_root
= FIELD_FIXED
;
1939 for(i
=0; i
< type
->fields
->len
; i
++) {
1940 *offset
= field
->offset_root
;
1941 child
= &g_array_index(type
->fields
, LttField
, i
);
1942 ret
= precompute_fields_offsets(fac
, child
, offset
, is_compact
);
1946 *offset
= field
->offset_root
+ field
->field_size
;
1953 g_error("precompute_fields_offsets : unknown type");
1962 /*****************************************************************************
1964 * precompute_offsets : set the precomputable offset of an event type
1967 * event : event type
1969 ****************************************************************************/
1970 void precompute_offsets(LttFacility
*fac
, LttEventType
*event
)
1976 /* First, compute the size of fixed size fields. Will determine size for
1977 * arrays, struct and unions, which is not done by the parser */
1978 for(i
=0; i
<event
->fields
->len
; i
++) {
1979 LttField
*field
= &g_array_index(event
->fields
, LttField
, i
);
1980 field_compute_static_size(fac
, field
);
1983 /* Precompute all known offsets */
1984 for(i
=0; i
<event
->fields
->len
; i
++) {
1985 LttField
*field
= &g_array_index(event
->fields
, LttField
, i
);
1986 if(event
->has_compact_data
&& i
== 0)
1987 ret
= precompute_fields_offsets(fac
, field
, &offset
, 1);
1989 ret
= precompute_fields_offsets(fac
, field
, &offset
, 0);
1997 /*****************************************************************************
1999 * preset_field_type_size : set the fixed sizes of the field type
2002 * event_type : event type
2003 * offset_root : offset from the root
2004 * offset_parent : offset from the parent
2005 * fixed_root : Do we know a fixed offset to the root ?
2006 * fixed_parent : Do we know a fixed offset to the parent ?
2008 ****************************************************************************/
2012 // preset the fixed size offsets. Calculate them just like genevent-new : an
2013 // increment of a *to value that represents the offset from the start of the
2015 // The preset information is : offsets up to (and including) the first element
2016 // of variable size. All subsequent fields must be flagged "VARIABLE OFFSET".
2018 void preset_field_type_size(LttTracefile
*tf
, LttEventType
*event_type
,
2019 off_t offset_root
, off_t offset_parent
,
2020 enum field_status
*fixed_root
, enum field_status
*fixed_parent
,
2023 enum field_status local_fixed_root
, local_fixed_parent
;
2027 g_assert(field
->fixed_root
== FIELD_UNKNOWN
);
2028 g_assert(field
->fixed_parent
== FIELD_UNKNOWN
);
2029 g_assert(field
->fixed_size
== FIELD_UNKNOWN
);
2031 type
= field
->field_type
;
2033 field
->fixed_root
= *fixed_root
;
2034 if(field
->fixed_root
== FIELD_FIXED
)
2035 field
->offset_root
= offset_root
;
2037 field
->offset_root
= 0;
2039 field
->fixed_parent
= *fixed_parent
;
2040 if(field
->fixed_parent
== FIELD_FIXED
)
2041 field
->offset_parent
= offset_parent
;
2043 field
->offset_parent
= 0;
2045 size_t current_root_offset
;
2046 size_t current_offset
;
2047 enum field_status current_child_status
, final_child_status
;
2050 switch(type
->type_class
) {
2052 case LTT_UINT_FIXED
:
2061 field
->field_size
= ltt_type_size(tf
->trace
, type
);
2062 field
->fixed_size
= FIELD_FIXED
;
2065 field
->field_size
= (off_t
)event_type
->facility
->pointer_size
;
2066 field
->fixed_size
= FIELD_FIXED
;
2070 field
->field_size
= (off_t
)event_type
->facility
->long_size
;
2071 field
->fixed_size
= FIELD_FIXED
;
2076 field
->field_size
= (off_t
)event_type
->facility
->size_t_size
;
2077 field
->fixed_size
= FIELD_FIXED
;
2080 local_fixed_root
= FIELD_VARIABLE
;
2081 local_fixed_parent
= FIELD_VARIABLE
;
2082 preset_field_type_size(tf
, event_type
,
2084 &local_fixed_root
, &local_fixed_parent
,
2086 field
->fixed_size
= FIELD_VARIABLE
;
2087 field
->field_size
= 0;
2088 *fixed_root
= FIELD_VARIABLE
;
2089 *fixed_parent
= FIELD_VARIABLE
;
2092 field
->fixed_size
= FIELD_VARIABLE
;
2093 field
->field_size
= 0;
2094 *fixed_root
= FIELD_VARIABLE
;
2095 *fixed_parent
= FIELD_VARIABLE
;
2098 local_fixed_root
= FIELD_VARIABLE
;
2099 local_fixed_parent
= FIELD_VARIABLE
;
2100 preset_field_type_size(tf
, event_type
,
2102 &local_fixed_root
, &local_fixed_parent
,
2104 field
->fixed_size
= field
->child
[0]->fixed_size
;
2105 if(field
->fixed_size
== FIELD_FIXED
) {
2106 field
->field_size
= type
->element_number
* field
->child
[0]->field_size
;
2108 field
->field_size
= 0;
2109 *fixed_root
= FIELD_VARIABLE
;
2110 *fixed_parent
= FIELD_VARIABLE
;
2114 current_root_offset
= field
->offset_root
;
2116 current_child_status
= FIELD_FIXED
;
2117 for(i
=0;i
<type
->element_number
;i
++) {
2118 preset_field_type_size(tf
, event_type
,
2119 current_root_offset
, current_offset
,
2120 fixed_root
, ¤t_child_status
,
2122 if(current_child_status
== FIELD_FIXED
) {
2123 current_root_offset
+= field
->child
[i
]->field_size
;
2124 current_offset
+= field
->child
[i
]->field_size
;
2126 current_root_offset
= 0;
2130 if(current_child_status
!= FIELD_FIXED
) {
2131 *fixed_parent
= current_child_status
;
2132 field
->field_size
= 0;
2133 field
->fixed_size
= current_child_status
;
2135 field
->field_size
= current_offset
;
2136 field
->fixed_size
= FIELD_FIXED
;
2140 current_root_offset
= field
->offset_root
;
2143 final_child_status
= FIELD_FIXED
;
2144 for(i
=0;i
<type
->element_number
;i
++) {
2145 enum field_status current_root_child_status
= FIELD_FIXED
;
2146 enum field_status current_child_status
= FIELD_FIXED
;
2147 preset_field_type_size(tf
, event_type
,
2148 current_root_offset
, current_offset
,
2149 ¤t_root_child_status
, ¤t_child_status
,
2151 if(current_child_status
!= FIELD_FIXED
)
2152 final_child_status
= current_child_status
;
2154 max_size
= max(max_size
, field
->child
[i
]->field_size
);
2156 if(final_child_status
!= FIELD_FIXED
) {
2157 g_error("LTTV does not support variable size fields in unions.");
2158 /* This will stop the application. */
2159 *fixed_root
= final_child_status
;
2160 *fixed_parent
= final_child_status
;
2161 field
->field_size
= 0;
2162 field
->fixed_size
= current_child_status
;
2164 field
->field_size
= max_size
;
2165 field
->fixed_size
= FIELD_FIXED
;
2169 g_error("unexpected type NONE");
2176 /*****************************************************************************
2178 * check_fields_compatibility : Check for compatibility between two fields :
2179 * do they use the same inner structure ?
2181 * event_type1 : event type
2182 * event_type2 : event type
2185 *Returns : 0 if identical
2187 ****************************************************************************/
2188 // this function checks for equality of field types. Therefore, it does not use
2189 // per se offsets. For instance, an aligned version of a structure is
2190 // compatible with an unaligned version of the same structure.
2192 gint
check_fields_compatibility(LttEventType
*event_type1
,
2193 LttEventType
*event_type2
,
2194 LttField
*field1
, LttField
*field2
)
2196 guint different
= 0;
2200 if(field1
== NULL
) {
2201 if(field2
== NULL
) goto end
;
2206 } else if(field2
== NULL
) {
2211 type1
= &field1
->field_type
;
2212 type2
= &field2
->field_type
;
2214 if(type1
->type_class
!= type2
->type_class
) {
2218 if(type1
->network
!= type2
->network
) {
2223 switch(type1
->type_class
) {
2225 case LTT_UINT_FIXED
:
2240 if(field1
->field_size
!= field2
->field_size
)
2247 LttField
*child1
= &g_array_index(type1
->fields
, LttField
, 0);
2248 LttField
*child2
= &g_array_index(type2
->fields
, LttField
, 0);
2250 if(type1
->size
!= type2
->size
)
2252 if(check_fields_compatibility(event_type1
, event_type2
, child1
, child2
))
2258 LttField
*child1
= &g_array_index(type1
->fields
, LttField
, 1);
2259 LttField
*child2
= &g_array_index(type2
->fields
, LttField
, 1);
2261 if(check_fields_compatibility(event_type1
, event_type2
, child1
, child2
))
2271 if(type1
->fields
->len
!= type2
->fields
->len
) {
2276 for(i
=0; i
< type1
->fields
->len
; i
++) {
2279 child1
= &g_array_index(type1
->fields
, LttField
, i
);
2280 child2
= &g_array_index(type2
->fields
, LttField
, i
);
2281 different
= check_fields_compatibility(event_type1
,
2282 event_type2
, child1
, child2
);
2284 if(different
) break;
2290 g_error("check_fields_compatibility : unknown type");
2299 gint
check_fields_compatibility(LttEventType
*event_type1
,
2300 LttEventType
*event_type2
,
2301 LttField
*field1
, LttField
*field2
)
2303 guint different
= 0;
2308 if(field1
== NULL
) {
2309 if(field2
== NULL
) goto end
;
2314 } else if(field2
== NULL
) {
2319 g_assert(field1
->fixed_root
!= FIELD_UNKNOWN
);
2320 g_assert(field2
->fixed_root
!= FIELD_UNKNOWN
);
2321 g_assert(field1
->fixed_parent
!= FIELD_UNKNOWN
);
2322 g_assert(field2
->fixed_parent
!= FIELD_UNKNOWN
);
2323 g_assert(field1
->fixed_size
!= FIELD_UNKNOWN
);
2324 g_assert(field2
->fixed_size
!= FIELD_UNKNOWN
);
2326 type1
= field1
->field_type
;
2327 type2
= field2
->field_type
;
2329 if(type1
->type_class
!= type2
->type_class
) {
2333 if(type1
->element_name
!= type2
->element_name
) {
2338 switch(type1
->type_class
) {
2340 case LTT_UINT_FIXED
:
2355 if(field1
->field_size
!= field2
->field_size
) {
2361 if(type1
->element_number
!= type2
->element_number
) {
2365 for(i
=0;i
<type1
->element_number
;i
++) {
2366 if(type1
->enum_strings
[i
] != type2
->enum_strings
[i
]) {
2373 /* Two elements : size and child */
2374 g_assert(type1
->element_number
!= type2
->element_number
);
2375 for(i
=0;i
<type1
->element_number
;i
++) {
2376 if(check_fields_compatibility(event_type1
, event_type2
,
2377 field1
->child
[0], field2
->child
[0])) {
2386 if(field1
->field_size
!= field2
->field_size
) {
2390 /* Two elements : size and child */
2391 g_assert(type1
->element_number
!= type2
->element_number
);
2392 for(i
=0;i
<type1
->element_number
;i
++) {
2393 if(check_fields_compatibility(event_type1
, event_type2
,
2394 field1
->child
[0], field2
->child
[0])) {
2402 if(type1
->element_number
!= type2
->element_number
) {
2406 for(i
=0;i
<type1
->element_number
;i
++) {
2407 if(check_fields_compatibility(event_type1
, event_type2
,
2408 field1
->child
[0], field2
->child
[0])) {
2421 /*****************************************************************************
2423 * ltt_get_int : get an integer number
2425 * reverse_byte_order: must we reverse the byte order ?
2426 * size : the size of the integer
2427 * ptr : the data pointer
2429 * gint64 : a 64 bits integer
2430 ****************************************************************************/
2432 gint64
ltt_get_int(gboolean reverse_byte_order
, gint size
, void *data
)
2437 case 1: val
= *((gint8
*)data
); break;
2438 case 2: val
= ltt_get_int16(reverse_byte_order
, data
); break;
2439 case 4: val
= ltt_get_int32(reverse_byte_order
, data
); break;
2440 case 8: val
= ltt_get_int64(reverse_byte_order
, data
); break;
2441 default: val
= ltt_get_int64(reverse_byte_order
, data
);
2442 g_critical("get_int : integer size %d unknown", size
);
2449 /*****************************************************************************
2451 * ltt_get_uint : get an unsigned integer number
2453 * reverse_byte_order: must we reverse the byte order ?
2454 * size : the size of the integer
2455 * ptr : the data pointer
2457 * guint64 : a 64 bits unsigned integer
2458 ****************************************************************************/
2460 guint64
ltt_get_uint(gboolean reverse_byte_order
, gint size
, void *data
)
2465 case 1: val
= *((gint8
*)data
); break;
2466 case 2: val
= ltt_get_uint16(reverse_byte_order
, data
); break;
2467 case 4: val
= ltt_get_uint32(reverse_byte_order
, data
); break;
2468 case 8: val
= ltt_get_uint64(reverse_byte_order
, data
); break;
2469 default: val
= ltt_get_uint64(reverse_byte_order
, data
);
2470 g_critical("get_uint : unsigned integer size %d unknown",
2479 /* get the node name of the system */
2481 char * ltt_trace_system_description_node_name (LttSystemDescription
* s
)
2483 return s
->node_name
;
2487 /* get the domain name of the system */
2489 char * ltt_trace_system_description_domain_name (LttSystemDescription
* s
)
2491 return s
->domain_name
;
2495 /* get the description of the system */
2497 char * ltt_trace_system_description_description (LttSystemDescription
* s
)
2499 return s
->description
;
2503 /* get the NTP corrected start time of the trace */
2504 LttTime
ltt_trace_start_time(LttTrace
*t
)
2506 return t
->start_time
;
2509 /* get the monotonic start time of the trace */
2510 LttTime
ltt_trace_start_time_monotonic(LttTrace
*t
)
2512 return t
->start_time_from_tsc
;
2515 static LttTracefile
*ltt_tracefile_new()
2518 tf
= g_new(LttTracefile
, 1);
2519 tf
->event
.tracefile
= tf
;
2523 static void ltt_tracefile_destroy(LttTracefile
*tf
)
2528 static void ltt_tracefile_copy(LttTracefile
*dest
, const LttTracefile
*src
)
2533 /* Before library loading... */
2535 static __attribute__((constructor
)) void init(void)
2537 LTT_TRACEFILE_NAME_METADATA
= g_quark_from_string("/control/metadata");