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(ltt_subbuffer_header_t
*header
,
126 LttTracefile
*tf
, LttTrace
*t
)
128 if (header
->magic_number
== LTT_MAGIC_NUMBER
)
130 else if(header
->magic_number
== LTT_REV_MAGIC_NUMBER
)
132 else /* invalid magic number, bad tracefile ! */
136 t
->ltt_major_version
= header
->major_version
;
137 t
->ltt_minor_version
= header
->minor_version
;
138 t
->arch_size
= header
->arch_size
;
140 tf
->alignment
= header
->alignment
;
142 /* Get float byte order : might be different from int byte order
143 * (or is set to 0 if the trace has no float (kernel trace)) */
144 tf
->float_word_order
= 0;
146 switch(header
->major_version
) {
149 g_warning("Unsupported trace version : %hhu.%hhu",
150 header
->major_version
, header
->minor_version
);
154 switch(header
->minor_version
) {
157 struct ltt_subbuffer_header_2_2
*vheader
= header
;
158 tf
->buffer_header_size
= ltt_subbuffer_header_size();
161 tf
->tsc_mask
= ((1ULL << tf
->tscbits
) - 1);
162 tf
->tsc_mask_next_bit
= (1ULL << tf
->tscbits
);
165 t
->start_freq
= ltt_get_uint64(LTT_GET_BO(tf
),
166 &vheader
->start_freq
);
167 t
->freq_scale
= ltt_get_uint32(LTT_GET_BO(tf
),
168 &vheader
->freq_scale
);
170 t
->start_freq
= father_trace
->start_freq
;
171 t
->freq_scale
= father_trace
->freq_scale
;
175 t
->start_tsc
= ltt_get_uint64(LTT_GET_BO(tf
),
176 &vheader
->cycle_count_begin
);
177 t
->start_monotonic
= 0;
178 t
->start_time
.tv_sec
= ltt_get_uint64(LTT_GET_BO(tf
),
179 &vheader
->start_time_sec
);
180 t
->start_time
.tv_nsec
= ltt_get_uint64(LTT_GET_BO(tf
),
181 &vheader
->start_time_usec
);
182 t
->start_time
.tv_nsec
*= 1000; /* microsec to nanosec */
184 t
->start_time_from_tsc
= ltt_time_from_uint64(
186 * 1000000000.0 * tf
->trace
->freq_scale
187 / (double)t
->start_freq
);
192 g_warning("Unsupported trace version : %hhu.%hhu",
193 header
->major_version
, header
->minor_version
);
198 g_warning("Unsupported trace version : %hhu.%hhu",
199 header
->major_version
, header
->minor_version
);
207 /*****************************************************************************
209 * ltt_tracefile_open : open a trace file, construct a LttTracefile
211 * t : the trace containing the tracefile
212 * fileName : path name of the trace file
213 * tf : the tracefile structure
215 * : 0 for success, -1 otherwise.
216 ****************************************************************************/
218 static gint
ltt_tracefile_open(LttTrace
*t
, gchar
* fileName
, LttTracefile
*tf
)
220 struct stat lTDFStat
; /* Trace data file status */
221 ltt_subbuffer_header_t
*header
;
222 int page_size
= getpagesize();
225 tf
->long_name
= g_quark_from_string(fileName
);
227 tf
->fd
= open(fileName
, O_RDONLY
);
229 g_warning("Unable to open input data file %s\n", fileName
);
233 // Get the file's status
234 if(fstat(tf
->fd
, &lTDFStat
) < 0){
235 g_warning("Unable to get the status of the input data file %s\n", fileName
);
239 // Is the file large enough to contain a trace
240 if(lTDFStat
.st_size
<
241 (off_t
)(ltt_subbuffer_header_size())){
242 g_print("The input data file %s does not contain a trace\n", fileName
);
246 /* Temporarily map the buffer start header to get trace information */
247 /* Multiple of pages aligned head */
248 tf
->buffer
.head
= mmap(0,
249 PAGE_ALIGN(ltt_subbuffer_header_size()), PROT_READ
,
250 MAP_PRIVATE
, tf
->fd
, 0);
251 if(tf
->buffer
.head
== MAP_FAILED
) {
252 perror("Error in allocating memory for buffer of tracefile");
255 g_assert( ( (gulong
)tf
->buffer
.head
&(8-1) ) == 0); // make sure it's aligned.
257 header
= (ltt_subbuffer_header_t
*)tf
->buffer
.head
;
259 if(parse_trace_header(header
, tf
, NULL
)) {
260 g_warning("parse_trace_header error");
264 //store the size of the file
265 tf
->file_size
= lTDFStat
.st_size
;
266 tf
->buf_size
= ltt_get_uint32(LTT_GET_BO(tf
), &header
->buf_size
);
267 tf
->num_blocks
= tf
->file_size
/ tf
->buf_size
;
269 tf
->subbuf_corrupt
= 0;
271 if(munmap(tf
->buffer
.head
,
272 PAGE_ALIGN(ltt_subbuffer_header_size()))) {
273 g_warning("unmap size : %u\n",
274 PAGE_ALIGN(ltt_subbuffer_header_size()));
275 perror("munmap error");
278 tf
->buffer
.head
= NULL
;
280 //read the first block
281 if(map_block(tf
,0)) {
282 perror("Cannot map block for tracefile");
290 if(munmap(tf
->buffer
.head
,
291 PAGE_ALIGN(ltt_subbuffer_header_size()))) {
292 g_warning("unmap size : %u\n",
293 PAGE_ALIGN(ltt_subbuffer_header_size()));
294 perror("munmap error");
304 /*****************************************************************************
306 * ltt_tracefile_close: close a trace file,
308 * t : tracefile which will be closed
309 ****************************************************************************/
311 static void ltt_tracefile_close(LttTracefile
*t
)
313 int page_size
= getpagesize();
315 if(t
->buffer
.head
!= NULL
)
316 if(munmap(t
->buffer
.head
, PAGE_ALIGN(t
->buf_size
))) {
317 g_warning("unmap size : %u\n",
318 PAGE_ALIGN(t
->buf_size
));
319 perror("munmap error");
326 /****************************************************************************
327 * get_absolute_pathname
329 * return the unique pathname in the system
331 * MD : Fixed this function so it uses realpath, dealing well with
332 * forgotten cases (.. were not used correctly before).
334 ****************************************************************************/
335 void get_absolute_pathname(const gchar
*pathname
, gchar
* abs_pathname
)
337 abs_pathname
[0] = '\0';
339 if (realpath(pathname
, abs_pathname
) != NULL
)
343 /* error, return the original path unmodified */
344 strcpy(abs_pathname
, pathname
);
350 /* Search for something like : .*_.*
352 * The left side is the name, the right side is the number.
355 static int get_tracefile_name_number(gchar
*raw_name
,
362 guint raw_name_len
= strlen(raw_name
);
363 gchar char_name
[PATH_MAX
];
370 for(i
=raw_name_len
-1;i
>=0;i
--) {
371 if(raw_name
[i
] == '_') break;
373 if(i
==-1) { /* Either not found or name length is 0 */
374 /* This is a userspace tracefile */
375 strncpy(char_name
, raw_name
, raw_name_len
);
376 char_name
[raw_name_len
] = '\0';
377 *name
= g_quark_from_string(char_name
);
378 *num
= 0; /* unknown cpu */
379 for(i
=0;i
<raw_name_len
;i
++) {
380 if(raw_name
[i
] == '/') {
385 for(;i
<raw_name_len
;i
++) {
386 if(raw_name
[i
] == '/') {
391 for(;i
<raw_name_len
;i
++) {
392 if(raw_name
[i
] == '-') {
396 if(i
== raw_name_len
) return -1;
398 tmpptr
= &raw_name
[i
];
399 for(;i
<raw_name_len
;i
++) {
400 if(raw_name
[i
] == '.') {
405 *tid
= strtoul(tmpptr
, &endptr
, 10);
407 return -1; /* No digit */
408 if(*tid
== ULONG_MAX
)
409 return -1; /* underflow / overflow */
411 tmpptr
= &raw_name
[i
];
412 for(;i
<raw_name_len
;i
++) {
413 if(raw_name
[i
] == '.') {
418 *pgid
= strtoul(tmpptr
, &endptr
, 10);
420 return -1; /* No digit */
421 if(*pgid
== ULONG_MAX
)
422 return -1; /* underflow / overflow */
424 tmpptr
= &raw_name
[i
];
425 *creation
= strtoull(tmpptr
, &endptr
, 10);
427 return -1; /* No digit */
428 if(*creation
== G_MAXUINT64
)
429 return -1; /* underflow / overflow */
433 cpu_num
= strtol(raw_name
+underscore_pos
+1, &endptr
, 10);
435 if(endptr
== raw_name
+underscore_pos
+1)
436 return -1; /* No digit */
437 if(cpu_num
== LONG_MIN
|| cpu_num
== LONG_MAX
)
438 return -1; /* underflow / overflow */
440 strncpy(char_name
, raw_name
, underscore_pos
);
441 char_name
[underscore_pos
] = '\0';
443 *name
= g_quark_from_string(char_name
);
452 GData
**ltt_trace_get_tracefiles_groups(LttTrace
*trace
)
454 return &trace
->tracefiles
;
458 void compute_tracefile_group(GQuark key_id
,
460 struct compute_tracefile_group_args
*args
)
465 for(i
=0; i
<group
->len
; i
++) {
466 tf
= &g_array_index (group
, LttTracefile
, i
);
468 args
->func(tf
, args
->func_args
);
473 static void ltt_tracefile_group_destroy(gpointer data
)
475 GArray
*group
= (GArray
*)data
;
479 for(i
=0; i
<group
->len
; i
++) {
480 tf
= &g_array_index (group
, LttTracefile
, i
);
482 ltt_tracefile_close(tf
);
484 g_array_free(group
, TRUE
);
487 static gboolean
ltt_tracefile_group_has_cpu_online(gpointer data
)
489 GArray
*group
= (GArray
*)data
;
493 for(i
=0; i
<group
->len
; i
++) {
494 tf
= &g_array_index (group
, LttTracefile
, i
);
502 /* Open each tracefile under a specific directory. Put them in a
503 * GData : permits to access them using their tracefile group pathname.
504 * i.e. access control/modules tracefile group by index :
507 * relative path is the path relative to the trace root
508 * root path is the full path
510 * A tracefile group is simply an array where all the per cpu tracefiles sit.
513 static int open_tracefiles(LttTrace
*trace
, gchar
*root_path
, gchar
*relative_path
)
515 DIR *dir
= opendir(root_path
);
516 struct dirent
*entry
;
517 struct stat stat_buf
;
520 gchar path
[PATH_MAX
];
525 gchar rel_path
[PATH_MAX
];
534 strncpy(path
, root_path
, PATH_MAX
-1);
535 path_len
= strlen(path
);
536 path
[path_len
] = '/';
538 path_ptr
= path
+ path_len
;
540 strncpy(rel_path
, relative_path
, PATH_MAX
-1);
541 rel_path_len
= strlen(rel_path
);
542 rel_path
[rel_path_len
] = '/';
544 rel_path_ptr
= rel_path
+ rel_path_len
;
546 while((entry
= readdir(dir
)) != NULL
) {
548 if(entry
->d_name
[0] == '.') continue;
550 strncpy(path_ptr
, entry
->d_name
, PATH_MAX
- path_len
);
551 strncpy(rel_path_ptr
, entry
->d_name
, PATH_MAX
- rel_path_len
);
553 ret
= stat(path
, &stat_buf
);
559 g_debug("Tracefile file or directory : %s\n", path
);
561 // if(strcmp(rel_path, "/eventdefs") == 0) continue;
563 if(S_ISDIR(stat_buf
.st_mode
)) {
565 g_debug("Entering subdirectory...\n");
566 ret
= open_tracefiles(trace
, path
, rel_path
);
567 if(ret
< 0) continue;
568 } else if(S_ISREG(stat_buf
.st_mode
)) {
577 if(get_tracefile_name_number(rel_path
, &name
, &num
, &tid
, &pgid
, &creation
))
578 continue; /* invalid name */
580 g_debug("Opening file.\n");
581 if(ltt_tracefile_open(trace
, path
, &tmp_tf
)) {
582 g_info("Error opening tracefile %s", path
);
584 continue; /* error opening the tracefile : bad magic number ? */
587 g_debug("Tracefile name is %s and number is %u",
588 g_quark_to_string(name
), num
);
590 tmp_tf
.cpu_online
= 1;
591 tmp_tf
.cpu_num
= num
;
595 tmp_tf
.creation
= creation
;
596 group
= g_datalist_id_get_data(&trace
->tracefiles
, name
);
598 /* Elements are automatically cleared when the array is allocated.
599 * It makes the cpu_online variable set to 0 : cpu offline, by default.
601 group
= g_array_sized_new (FALSE
, TRUE
, sizeof(LttTracefile
), 10);
602 g_datalist_id_set_data_full(&trace
->tracefiles
, name
,
603 group
, ltt_tracefile_group_destroy
);
606 /* Add the per cpu tracefile to the named group */
607 unsigned int old_len
= group
->len
;
609 group
= g_array_set_size(group
, num
+1);
610 g_array_index (group
, LttTracefile
, num
) = tmp_tf
;
611 g_array_index (group
, LttTracefile
, num
).event
.tracefile
=
612 &g_array_index (group
, LttTracefile
, num
);
622 /* Presumes the tracefile is already seeked at the beginning. It makes sense,
623 * because it must be done just after the opening */
624 static int ltt_process_metadata_tracefile(LttTracefile
*tf
)
630 err
= ltt_tracefile_read_seek(tf
);
631 if(err
== EPERM
) goto seek_error
;
632 else if(err
== ERANGE
) break; /* End of tracefile */
634 err
= ltt_tracefile_read_update_event(tf
);
635 if(err
) goto update_error
;
638 * It contains only core events :
640 * 1 : set_marker_format
642 if(tf
->event
.event_id
>= MARKER_CORE_IDS
) {
643 /* Should only contain core events */
644 g_warning("Error in processing metadata file %s, "
645 "should not contain event id %u.", g_quark_to_string(tf
->name
),
651 const char *marker_name
, *format
;
653 guint8 int_size
, long_size
, pointer_size
, size_t_size
, alignment
;
655 switch((enum marker_id
)tf
->event
.event_id
) {
656 case MARKER_ID_SET_MARKER_ID
:
657 marker_name
= pos
= tf
->event
.data
;
658 g_debug("Doing MARKER_ID_SET_MARKER_ID of marker %s", marker_name
);
659 pos
+= strlen(marker_name
) + 1;
660 pos
+= ltt_align((size_t)pos
, sizeof(guint16
), tf
->alignment
);
661 id
= ltt_get_uint16(LTT_GET_BO(tf
), pos
);
662 g_debug("In MARKER_ID_SET_MARKER_ID of marker %s id %hu",
664 pos
+= sizeof(guint16
);
665 int_size
= *(guint8
*)pos
;
666 pos
+= sizeof(guint8
);
667 long_size
= *(guint8
*)pos
;
668 pos
+= sizeof(guint8
);
669 pointer_size
= *(guint8
*)pos
;
670 pos
+= sizeof(guint8
);
671 size_t_size
= *(guint8
*)pos
;
672 pos
+= sizeof(guint8
);
673 alignment
= *(guint8
*)pos
;
674 pos
+= sizeof(guint8
);
675 marker_id_event(tf
->trace
, g_quark_from_string(marker_name
),
676 id
, int_size
, long_size
,
677 pointer_size
, size_t_size
, alignment
);
679 case MARKER_ID_SET_MARKER_FORMAT
:
680 marker_name
= pos
= tf
->event
.data
;
681 g_debug("Doing MARKER_ID_SET_MARKER_FORMAT of marker %s",
683 pos
+= strlen(marker_name
) + 1;
685 pos
+= strlen(format
) + 1;
686 marker_format_event(tf
->trace
, g_quark_from_string(marker_name
),
688 /* get information from dictionary TODO */
691 g_warning("Error in processing metadata file %s, "
692 "unknown event id %hhu.",
693 g_quark_to_string(tf
->name
),
706 g_warning("An error occured in metadata tracefile parsing");
711 * Open a trace and return its LttTrace handle.
713 * pathname must be the directory of the trace
716 LttTrace
*ltt_trace_open(const gchar
*pathname
)
718 gchar abs_path
[PATH_MAX
];
723 ltt_subbuffer_header_t
*header
;
725 struct dirent
*entry
;
726 guint control_found
= 0;
727 struct stat stat_buf
;
728 gchar path
[PATH_MAX
];
730 t
= g_new(LttTrace
, 1);
731 if(!t
) goto alloc_error
;
733 get_absolute_pathname(pathname
, abs_path
);
734 t
->pathname
= g_quark_from_string(abs_path
);
736 g_datalist_init(&t
->tracefiles
);
738 /* Test to see if it looks like a trace */
739 dir
= opendir(abs_path
);
744 while((entry
= readdir(dir
)) != NULL
) {
745 strcpy(path
, abs_path
);
747 strcat(path
, entry
->d_name
);
748 ret
= stat(path
, &stat_buf
);
753 if(S_ISDIR(stat_buf
.st_mode
)) {
754 if(strcmp(entry
->d_name
, "control") == 0) {
761 if(!control_found
) goto find_error
;
763 /* Open all the tracefiles */
764 if(open_tracefiles(t
, abs_path
, "")) {
765 g_warning("Error opening tracefile %s", abs_path
);
769 /* Parse each trace control/metadata_N files : get runtime fac. info */
770 group
= g_datalist_id_get_data(&t
->tracefiles
, LTT_TRACEFILE_NAME_METADATA
);
772 g_error("Trace %s has no metadata tracefile", abs_path
);
778 * Get the trace information for the control/metadata_0 tracefile.
779 * Getting a correct trace start_time and start_tsc is insured by the fact
780 * that no subbuffers are supposed to be lost in the metadata channel.
781 * Therefore, the first subbuffer contains the start_tsc timestamp in its
784 g_assert(group
->len
> 0);
785 tf
= &g_array_index (group
, LttTracefile
, 0);
786 header
= (ltt_subbuffer_header_t
*)tf
->buffer
.head
;
787 ret
= parse_trace_header(header
, tf
, t
);
790 t
->num_cpu
= group
->len
;
792 ret
= allocate_marker_data(t
);
794 g_error("Error in allocating marker data");
796 for(i
=0; i
<group
->len
; i
++) {
797 tf
= &g_array_index (group
, LttTracefile
, i
);
799 if(ltt_process_metadata_tracefile(tf
))
807 destroy_marker_data(t
);
809 g_datalist_clear(&t
->tracefiles
);
817 /* Open another, completely independant, instance of a trace.
819 * A read on this new instance will read the first event of the trace.
821 * When we copy a trace, we want all the opening actions to happen again :
822 * the trace will be reopened and totally independant from the original.
823 * That's why we call ltt_trace_open.
825 LttTrace
*ltt_trace_copy(LttTrace
*self
)
827 return ltt_trace_open(g_quark_to_string(self
->pathname
));
834 void ltt_trace_close(LttTrace
*t
)
836 g_datalist_clear(&t
->tracefiles
);
841 /*****************************************************************************
842 * Get the start time and end time of the trace
843 ****************************************************************************/
845 void ltt_tracefile_time_span_get(LttTracefile
*tf
,
846 LttTime
*start
, LttTime
*end
)
850 err
= map_block(tf
, 0);
852 g_error("Can not map block");
853 *start
= ltt_time_infinite
;
855 *start
= tf
->buffer
.begin
.timestamp
;
857 err
= map_block(tf
, tf
->num_blocks
- 1); /* Last block */
859 g_error("Can not map block");
860 *end
= ltt_time_zero
;
862 *end
= tf
->buffer
.end
.timestamp
;
865 struct tracefile_time_span_get_args
{
871 static void group_time_span_get(GQuark name
, gpointer data
, gpointer user_data
)
873 struct tracefile_time_span_get_args
*args
=
874 (struct tracefile_time_span_get_args
*)user_data
;
876 GArray
*group
= (GArray
*)data
;
882 for(i
=0; i
<group
->len
; i
++) {
883 tf
= &g_array_index (group
, LttTracefile
, i
);
885 ltt_tracefile_time_span_get(tf
, &tmp_start
, &tmp_end
);
886 if(ltt_time_compare(*args
->start
, tmp_start
)>0) *args
->start
= tmp_start
;
887 if(ltt_time_compare(*args
->end
, tmp_end
)<0) *args
->end
= tmp_end
;
892 /* return the start and end time of a trace */
894 void ltt_trace_time_span_get(LttTrace
*t
, LttTime
*start
, LttTime
*end
)
896 LttTime min_start
= ltt_time_infinite
;
897 LttTime max_end
= ltt_time_zero
;
898 struct tracefile_time_span_get_args args
= { t
, &min_start
, &max_end
};
900 g_datalist_foreach(&t
->tracefiles
, &group_time_span_get
, &args
);
902 if(start
!= NULL
) *start
= min_start
;
903 if(end
!= NULL
) *end
= max_end
;
908 /* Seek to the first event in a tracefile that has a time equal or greater than
909 * the time passed in parameter.
911 * If the time parameter is outside the tracefile time span, seek to the first
912 * event or if after, return ERANGE.
914 * If the time parameter is before the first event, we have to seek specially to
917 * If the time is after the end of the trace, return ERANGE.
919 * Do a binary search to find the right block, then a sequential search in the
920 * block to find the event.
922 * In the special case where the time requested fits inside a block that has no
923 * event corresponding to the requested time, the first event of the next block
926 * IMPORTANT NOTE : // FIXME everywhere...
928 * You MUST NOT do a ltt_tracefile_read right after a ltt_tracefile_seek_time :
929 * you will jump over an event if you do.
931 * Return value : 0 : no error, the tf->event can be used
932 * ERANGE : time if after the last event of the trace
933 * otherwise : this is an error.
937 int ltt_tracefile_seek_time(LttTracefile
*tf
, LttTime time
)
941 unsigned int block_num
, high
, low
;
943 /* seek at the beginning of trace */
944 err
= map_block(tf
, 0); /* First block */
946 g_error("Can not map block");
950 /* If the time is lower or equal the beginning of the trace,
951 * go to the first event. */
952 if(ltt_time_compare(time
, tf
->buffer
.begin
.timestamp
) <= 0) {
953 ret
= ltt_tracefile_read(tf
);
954 if(ret
== ERANGE
) goto range
;
955 else if (ret
) goto fail
;
956 goto found
; /* There is either no event in the trace or the event points
957 to the first event in the trace */
960 err
= map_block(tf
, tf
->num_blocks
- 1); /* Last block */
962 g_error("Can not map block");
966 /* If the time is after the end of the trace, return ERANGE. */
967 if(ltt_time_compare(time
, tf
->buffer
.end
.timestamp
) > 0) {
971 /* Binary search the block */
972 high
= tf
->num_blocks
- 1;
976 block_num
= ((high
-low
) / 2) + low
;
978 err
= map_block(tf
, block_num
);
980 g_error("Can not map block");
984 /* We cannot divide anymore : this is what would happen if the time
985 * requested was exactly between two consecutive buffers'end and start
986 * timestamps. This is also what would happend if we didn't deal with out
987 * of span cases prior in this function. */
988 /* The event is right in the buffer!
989 * (or in the next buffer first event) */
991 ret
= ltt_tracefile_read(tf
);
992 if(ret
== ERANGE
) goto range
; /* ERANGE or EPERM */
993 else if(ret
) goto fail
;
995 if(ltt_time_compare(time
, tf
->event
.event_time
) <= 0)
999 } else if(ltt_time_compare(time
, tf
->buffer
.begin
.timestamp
) < 0) {
1000 /* go to lower part */
1001 high
= block_num
- 1;
1002 } else if(ltt_time_compare(time
, tf
->buffer
.end
.timestamp
) > 0) {
1003 /* go to higher part */
1004 low
= block_num
+ 1;
1005 } else {/* The event is right in the buffer!
1006 (or in the next buffer first event) */
1008 ret
= ltt_tracefile_read(tf
);
1009 if(ret
== ERANGE
) goto range
; /* ERANGE or EPERM */
1010 else if(ret
) goto fail
;
1012 if(ltt_time_compare(time
, tf
->event
.event_time
) <= 0)
1024 /* Error handling */
1026 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1027 g_quark_to_string(tf
->name
));
1031 /* Seek to a position indicated by an LttEventPosition
1034 int ltt_tracefile_seek_position(LttTracefile
*tf
, const LttEventPosition
*ep
)
1038 if(ep
->tracefile
!= tf
) {
1042 err
= map_block(tf
, ep
->block
);
1044 g_error("Can not map block");
1048 tf
->event
.offset
= ep
->offset
;
1050 /* Put back the event real tsc */
1051 tf
->event
.tsc
= ep
->tsc
;
1052 tf
->buffer
.tsc
= ep
->tsc
;
1054 err
= ltt_tracefile_read_update_event(tf
);
1057 /* deactivate this, as it does nothing for now
1058 err = ltt_tracefile_read_op(tf);
1065 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1066 g_quark_to_string(tf
->name
));
1070 /* Given a TSC value, return the LttTime (seconds,nanoseconds) it
1074 LttTime
ltt_interpolate_time_from_tsc(LttTracefile
*tf
, guint64 tsc
)
1078 if(tsc
> tf
->trace
->start_tsc
) {
1079 time
= ltt_time_from_uint64(
1080 (double)(tsc
- tf
->trace
->start_tsc
)
1081 * 1000000000.0 * tf
->trace
->freq_scale
1082 / (double)tf
->trace
->start_freq
);
1083 time
= ltt_time_add(tf
->trace
->start_time_from_tsc
, time
);
1085 time
= ltt_time_from_uint64(
1086 (double)(tf
->trace
->start_tsc
- tsc
)
1087 * 1000000000.0 * tf
->trace
->freq_scale
1088 / (double)tf
->trace
->start_freq
);
1089 time
= ltt_time_sub(tf
->trace
->start_time_from_tsc
, time
);
1094 /* Calculate the real event time based on the buffer boundaries */
1095 LttTime
ltt_interpolate_time(LttTracefile
*tf
, LttEvent
*event
)
1097 return ltt_interpolate_time_from_tsc(tf
, tf
->buffer
.tsc
);
1101 /* Get the current event of the tracefile : valid until the next read */
1102 LttEvent
*ltt_tracefile_get_event(LttTracefile
*tf
)
1109 /*****************************************************************************
1111 * ltt_tracefile_read : Read the next event in the tracefile
1116 * Returns 0 if an event can be used in tf->event.
1117 * Returns ERANGE on end of trace. The event in tf->event still can be used
1118 * (if the last block was not empty).
1119 * Returns EPERM on error.
1121 * This function does make the tracefile event structure point to the event
1122 * currently pointed to by the tf->event.
1124 * Note : you must call a ltt_tracefile_seek to the beginning of the trace to
1125 * reinitialize it after an error if you want results to be coherent.
1126 * It would be the case if a end of trace last buffer has no event : the end
1127 * of trace wouldn't be returned, but an error.
1128 * We make the assumption there is at least one event per buffer.
1129 ****************************************************************************/
1131 int ltt_tracefile_read(LttTracefile
*tf
)
1135 err
= ltt_tracefile_read_seek(tf
);
1137 err
= ltt_tracefile_read_update_event(tf
);
1140 /* deactivate this, as it does nothing for now
1141 err = ltt_tracefile_read_op(tf);
1148 int ltt_tracefile_read_seek(LttTracefile
*tf
)
1152 /* Get next buffer until we finally have an event, or end of trace */
1154 err
= ltt_seek_next_event(tf
);
1155 if(unlikely(err
== ENOPROTOOPT
)) {
1159 /* Are we at the end of the buffer ? */
1161 if(unlikely(tf
->buffer
.index
== tf
->num_blocks
-1)){ /* end of trace ? */
1164 /* get next block */
1165 err
= map_block(tf
, tf
->buffer
.index
+ 1);
1167 g_error("Can not map block");
1171 } else break; /* We found an event ! */
1177 /* do an operation when reading a new event */
1179 /* This function does nothing for now */
1181 int ltt_tracefile_read_op(LttTracefile
*tf
)
1187 /* do event specific operation */
1195 static void print_debug_event_header(LttEvent
*ev
, void *start_pos
, void *end_pos
)
1197 unsigned int offset
= 0;
1200 g_printf("Event header (tracefile %s offset %llx):\n",
1201 g_quark_to_string(ev
->tracefile
->long_name
),
1202 ((uint64_t)ev
->tracefile
->buffer
.index
* ev
->tracefile
->buf_size
)
1203 + (long)start_pos
- (long)ev
->tracefile
->buffer
.head
);
1205 while (offset
< (long)end_pos
- (long)start_pos
) {
1206 g_printf("%8lx", (long)start_pos
- (long)ev
->tracefile
->buffer
.head
+ offset
);
1209 for (i
= 0; i
< 4 ; i
++) {
1210 for (j
= 0; j
< 4; j
++) {
1211 if (offset
+ ((i
* 4) + j
) <
1212 (long)end_pos
- (long)start_pos
)
1214 ((char*)start_pos
)[offset
+ ((i
* 4) + j
)]);
1228 /* same as ltt_tracefile_read, but does not seek to the next event nor call
1229 * event specific operation. */
1230 int ltt_tracefile_read_update_event(LttTracefile
*tf
)
1237 pos
= tf
->buffer
.head
+ event
->offset
;
1239 /* Read event header */
1241 /* Align the head */
1242 pos
+= ltt_align((size_t)pos
, sizeof(guint32
), tf
->alignment
);
1245 event
->timestamp
= ltt_get_uint32(LTT_GET_BO(tf
), pos
);
1246 event
->event_id
= event
->timestamp
>> tf
->tscbits
;
1247 event
->timestamp
= event
->timestamp
& tf
->tsc_mask
;
1248 pos
+= sizeof(guint32
);
1250 switch (event
->event_id
) {
1251 case 29: /* LTT_RFLAG_ID_SIZE_TSC */
1252 event
->event_id
= ltt_get_uint16(LTT_GET_BO(tf
), pos
);
1253 pos
+= sizeof(guint16
);
1254 event
->event_size
= ltt_get_uint16(LTT_GET_BO(tf
), pos
);
1255 pos
+= sizeof(guint16
);
1256 if (event
->event_size
== 0xFFFF) {
1257 event
->event_size
= ltt_get_uint32(LTT_GET_BO(tf
), pos
);
1258 pos
+= sizeof(guint32
);
1260 pos
+= ltt_align((size_t)pos
, sizeof(guint64
), tf
->alignment
);
1261 tf
->buffer
.tsc
= ltt_get_uint64(LTT_GET_BO(tf
), pos
);
1262 pos
+= sizeof(guint64
);
1264 case 30: /* LTT_RFLAG_ID_SIZE */
1265 event
->event_id
= ltt_get_uint16(LTT_GET_BO(tf
), pos
);
1266 pos
+= sizeof(guint16
);
1267 event
->event_size
= ltt_get_uint16(LTT_GET_BO(tf
), pos
);
1268 pos
+= sizeof(guint16
);
1269 if (event
->event_size
== 0xFFFF) {
1270 event
->event_size
= ltt_get_uint32(LTT_GET_BO(tf
), pos
);
1271 pos
+= sizeof(guint32
);
1274 case 31: /* LTT_RFLAG_ID */
1275 event
->event_id
= ltt_get_uint16(LTT_GET_BO(tf
), pos
);
1276 pos
+= sizeof(guint16
);
1277 event
->event_size
= G_MAXUINT
;
1280 event
->event_size
= G_MAXUINT
;
1284 if (likely(event
->event_id
!= 29)) {
1285 /* No extended timestamp */
1286 if (event
->timestamp
< (tf
->buffer
.tsc
& tf
->tsc_mask
))
1287 tf
->buffer
.tsc
= ((tf
->buffer
.tsc
& ~tf
->tsc_mask
) /* overflow */
1288 + tf
->tsc_mask_next_bit
)
1289 | (guint64
)event
->timestamp
;
1291 tf
->buffer
.tsc
= (tf
->buffer
.tsc
& ~tf
->tsc_mask
) /* no overflow */
1292 | (guint64
)event
->timestamp
;
1294 event
->tsc
= tf
->buffer
.tsc
;
1296 event
->event_time
= ltt_interpolate_time(tf
, event
);
1299 print_debug_event_header(event
, pos_aligned
, pos
);
1304 * Let ltt_update_event_size update event->data according to the largest
1305 * alignment within the payload.
1306 * Get the data size and update the event fields with the current
1308 ltt_update_event_size(tf
);
1314 /****************************************************************************
1316 * map_block : map a block from the file
1318 * lttdes : ltt trace file
1319 * whichBlock : the block which will be read
1322 * EINVAL : lseek fail
1323 * EIO : can not read from the file
1324 ****************************************************************************/
1326 static gint
map_block(LttTracefile
* tf
, guint block_num
)
1328 int page_size
= getpagesize();
1329 ltt_subbuffer_header_t
*header
;
1331 g_assert(block_num
< tf
->num_blocks
);
1333 if(tf
->buffer
.head
!= NULL
) {
1334 if(munmap(tf
->buffer
.head
, PAGE_ALIGN(tf
->buf_size
))) {
1335 g_warning("unmap size : %u\n",
1336 PAGE_ALIGN(tf
->buf_size
));
1337 perror("munmap error");
1342 /* Multiple of pages aligned head */
1343 tf
->buffer
.head
= mmap(0,
1344 PAGE_ALIGN(tf
->buf_size
),
1345 PROT_READ
, MAP_PRIVATE
, tf
->fd
,
1346 PAGE_ALIGN((off_t
)tf
->buf_size
* (off_t
)block_num
));
1348 if(tf
->buffer
.head
== MAP_FAILED
) {
1349 perror("Error in allocating memory for buffer of tracefile");
1353 g_assert( ( (gulong
)tf
->buffer
.head
&(8-1) ) == 0); // make sure it's aligned.
1356 tf
->buffer
.index
= block_num
;
1358 header
= (ltt_subbuffer_header_t
*)tf
->buffer
.head
;
1360 tf
->buffer
.begin
.cycle_count
= ltt_get_uint64(LTT_GET_BO(tf
),
1361 &header
->cycle_count_begin
);
1362 tf
->buffer
.begin
.freq
= tf
->trace
->start_freq
;
1364 tf
->buffer
.begin
.timestamp
= ltt_interpolate_time_from_tsc(tf
,
1365 tf
->buffer
.begin
.cycle_count
);
1366 tf
->buffer
.end
.cycle_count
= ltt_get_uint64(LTT_GET_BO(tf
),
1367 &header
->cycle_count_end
);
1368 tf
->buffer
.end
.freq
= tf
->trace
->start_freq
;
1370 tf
->buffer
.lost_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1371 &header
->lost_size
);
1372 tf
->buffer
.end
.timestamp
= ltt_interpolate_time_from_tsc(tf
,
1373 tf
->buffer
.end
.cycle_count
);
1374 tf
->buffer
.tsc
= tf
->buffer
.begin
.cycle_count
;
1375 tf
->event
.tsc
= tf
->buffer
.tsc
;
1376 tf
->buffer
.freq
= tf
->buffer
.begin
.freq
;
1379 * eventually support variable buffer size : will need a partial pre-read of
1380 * the headers to create an index when we open the trace... eventually. */
1381 g_assert(tf
->buf_size
== ltt_get_uint32(LTT_GET_BO(tf
),
1382 &header
->buf_size
));
1384 /* Make the current event point to the beginning of the buffer :
1385 * it means that the event read must get the first event. */
1386 tf
->event
.tracefile
= tf
;
1387 tf
->event
.block
= block_num
;
1388 tf
->event
.offset
= 0;
1390 if (header
->events_lost
) {
1391 g_warning("%d events lost so far in tracefile %s at block %u",
1392 (guint
)header
->events_lost
,
1393 g_quark_to_string(tf
->long_name
),
1395 tf
->events_lost
= header
->events_lost
;
1397 if (header
->subbuf_corrupt
) {
1398 g_warning("%d subbuffer(s) corrupted so far in tracefile %s at block %u",
1399 (guint
)header
->subbuf_corrupt
,
1400 g_quark_to_string(tf
->long_name
),
1402 tf
->subbuf_corrupt
= header
->subbuf_corrupt
;
1411 static void print_debug_event_data(LttEvent
*ev
)
1413 unsigned int offset
= 0;
1416 if (!max(ev
->event_size
, ev
->data_size
))
1419 g_printf("Event data (tracefile %s offset %llx):\n",
1420 g_quark_to_string(ev
->tracefile
->long_name
),
1421 ((uint64_t)ev
->tracefile
->buffer
.index
* ev
->tracefile
->buf_size
)
1422 + (long)ev
->data
- (long)ev
->tracefile
->buffer
.head
);
1424 while (offset
< max(ev
->event_size
, ev
->data_size
)) {
1425 g_printf("%8lx", (long)ev
->data
+ offset
1426 - (long)ev
->tracefile
->buffer
.head
);
1429 for (i
= 0; i
< 4 ; i
++) {
1430 for (j
= 0; j
< 4; j
++) {
1431 if (offset
+ ((i
* 4) + j
) < max(ev
->event_size
, ev
->data_size
))
1432 g_printf("%02hhX", ((char*)ev
->data
)[offset
+ ((i
* 4) + j
)]);
1443 for (i
= 0; i
< 4; i
++) {
1444 for (j
= 0; j
< 4; j
++) {
1445 if (offset
+ ((i
* 4) + j
) < max(ev
->event_size
, ev
->data_size
)) {
1446 if (isprint(((char*)ev
->data
)[offset
+ ((i
* 4) + j
)]))
1447 g_printf("%c", ((char*)ev
->data
)[offset
+ ((i
* 4) + j
)]);
1459 /* It will update the fields offsets too */
1460 void ltt_update_event_size(LttTracefile
*tf
)
1464 struct marker_info
*info
;
1466 switch((enum marker_id
)tf
->event
.event_id
) {
1467 case MARKER_ID_SET_MARKER_ID
:
1468 size
= strlen((char*)tf
->event
.data
) + 1;
1469 g_debug("marker %s id set", (char*)tf
->event
.data
);
1470 size
+= ltt_align(size
, sizeof(guint16
), tf
->alignment
);
1471 size
+= sizeof(guint16
);
1472 size
+= sizeof(guint8
);
1473 size
+= sizeof(guint8
);
1474 size
+= sizeof(guint8
);
1475 size
+= sizeof(guint8
);
1476 size
+= sizeof(guint8
);
1478 case MARKER_ID_SET_MARKER_FORMAT
:
1479 g_debug("marker %s format set", (char*)tf
->event
.data
);
1480 size
= strlen((char*)tf
->event
.data
) + 1;
1481 size
+= strlen((char*)tf
->event
.data
+ size
) + 1;
1485 info
= marker_get_info_from_id(tf
->trace
, tf
->event
.event_id
);
1487 if (tf
->event
.event_id
>= MARKER_CORE_IDS
)
1488 g_assert(info
!= NULL
);
1490 /* Do not update field offsets of core markers when initially reading the
1491 * metadata tracefile when the infos about these markers do not exist yet.
1493 if (likely(info
&& info
->fields
)) {
1495 tf
->event
.data
+= ltt_align((off_t
)(unsigned long)tf
->event
.data
,
1496 info
->largest_align
,
1498 /* size, dynamically computed */
1499 if (info
->size
!= -1)
1502 size
= marker_update_fields_offsets(marker_get_info_from_id(tf
->trace
,
1503 tf
->event
.event_id
), tf
->event
.data
);
1506 tf
->event
.data_size
= size
;
1508 /* Check consistency between kernel and LTTV structure sizes */
1509 if(tf
->event
.event_size
== G_MAXUINT
) {
1510 /* Event size too big to fit in the event size field */
1511 tf
->event
.event_size
= tf
->event
.data_size
;
1515 print_debug_event_data(&tf
->event
);
1517 /* Having a marker load or marker format event out of the metadata
1518 * tracefiles is a serious bug. */
1519 switch((enum marker_id
)tf
->event
.event_id
) {
1520 case MARKER_ID_SET_MARKER_ID
:
1521 case MARKER_ID_SET_MARKER_FORMAT
:
1522 if (tf
->name
!= g_quark_from_string("/control/metadata"))
1523 g_error("Trace inconsistency : metadata event found in data "
1524 "tracefile %s", g_quark_to_string(tf
->long_name
));
1527 if (tf
->event
.data_size
!= tf
->event
.event_size
) {
1528 struct marker_info
*info
= marker_get_info_from_id(tf
->trace
,
1529 tf
->event
.event_id
);
1530 g_error("Kernel/LTTV event size differs for event %s: kernel %u, LTTV %u",
1531 g_quark_to_string(info
->name
),
1532 tf
->event
.event_size
, tf
->event
.data_size
);
1538 /* Take the tf current event offset and use the event id to figure out where is
1539 * the next event offset.
1541 * This is an internal function not aiming at being used elsewhere : it will
1542 * not jump over the current block limits. Please consider using
1543 * ltt_tracefile_read to do this.
1545 * Returns 0 on success
1546 * ERANGE if we are at the end of the buffer.
1547 * ENOPROTOOPT if an error occured when getting the current event size.
1549 static int ltt_seek_next_event(LttTracefile
*tf
)
1554 /* seek over the buffer header if we are at the buffer start */
1555 if(tf
->event
.offset
== 0) {
1556 tf
->event
.offset
+= tf
->buffer_header_size
;
1558 if(tf
->event
.offset
== tf
->buf_size
- tf
->buffer
.lost_size
) {
1564 pos
= tf
->event
.data
;
1566 if(tf
->event
.data_size
< 0) goto error
;
1568 pos
+= (size_t)tf
->event
.data_size
;
1570 tf
->event
.offset
= pos
- tf
->buffer
.head
;
1572 if(tf
->event
.offset
== tf
->buf_size
- tf
->buffer
.lost_size
) {
1576 g_assert(tf
->event
.offset
< tf
->buf_size
- tf
->buffer
.lost_size
);
1582 g_error("Error in ltt_seek_next_event for tracefile %s",
1583 g_quark_to_string(tf
->name
));
1588 /*****************************************************************************
1590 * set_fields_offsets : set the precomputable offset of the fields
1592 * tracefile : opened trace file
1593 * event_type : the event type
1594 ****************************************************************************/
1596 void set_fields_offsets(LttTracefile
*tf
, LttEventType
*event_type
)
1598 LttField
*field
= event_type
->root_field
;
1599 enum field_status fixed_root
= FIELD_FIXED
, fixed_parent
= FIELD_FIXED
;
1602 preset_field_type_size(tf
, event_type
, 0, 0,
1603 &fixed_root
, &fixed_parent
,
1610 /*****************************************************************************
1612 * get_alignment : Get the alignment needed for a field.
1616 * returns : The size on which it must be aligned.
1618 ****************************************************************************/
1620 off_t
get_alignment(LttField
*field
)
1622 LttType
*type
= &field
->field_type
;
1624 switch(type
->type_class
) {
1626 case LTT_UINT_FIXED
:
1641 /* Align offset on type size */
1642 g_assert(field
->field_size
!= 0);
1643 return field
->field_size
;
1649 g_assert(type
->fields
->len
== 1);
1651 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
1652 return get_alignment(child
);
1656 g_assert(type
->fields
->len
== 2);
1659 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
1661 localign
= max(localign
, get_alignment(child
));
1663 child
= &g_array_index(type
->fields
, LttField
, 1);
1664 localign
= max(localign
, get_alignment(child
));
1675 for(i
=0; i
<type
->fields
->len
; i
++) {
1676 LttField
*child
= &g_array_index(type
->fields
, LttField
, i
);
1677 localign
= max(localign
, get_alignment(child
));
1684 g_error("get_alignment : unknown type");
1691 /*****************************************************************************
1693 * field_compute_static_size : Determine the size of fields known by their
1694 * sole definition. Unions, arrays and struct sizes might be known, but
1695 * the parser does not give that information.
1700 ****************************************************************************/
1702 void field_compute_static_size(LttFacility
*fac
, LttField
*field
)
1704 LttType
*type
= &field
->field_type
;
1706 switch(type
->type_class
) {
1708 case LTT_UINT_FIXED
:
1727 /* note this : array type size is the number of elements in the array,
1728 * while array field size of the length of the array in bytes */
1729 g_assert(type
->fields
->len
== 1);
1731 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
1732 field_compute_static_size(fac
, child
);
1734 if(child
->field_size
!= 0) {
1735 field
->field_size
= type
->size
* child
->field_size
;
1736 field
->dynamic_offsets
= g_array_sized_new(FALSE
, TRUE
,
1737 sizeof(off_t
), type
->size
);
1739 field
->field_size
= 0;
1744 g_assert(type
->fields
->len
== 2);
1746 off_t local_offset
= 0;
1747 LttField
*child
= &g_array_index(type
->fields
, LttField
, 1);
1748 field_compute_static_size(fac
, child
);
1749 field
->field_size
= 0;
1751 if(child
->field_size
!= 0) {
1752 field
->dynamic_offsets
= g_array_sized_new(FALSE
, TRUE
,
1753 sizeof(off_t
), SEQUENCE_AVG_ELEMENTS
);
1761 for(i
=0;i
<type
->fields
->len
;i
++) {
1762 LttField
*child
= &g_array_index(type
->fields
, LttField
, i
);
1763 field_compute_static_size(fac
, child
);
1764 if(child
->field_size
!= 0) {
1765 type
->size
+= ltt_align(type
->size
, get_alignment(child
),
1767 type
->size
+= child
->field_size
;
1769 /* As soon as we find a child with variable size, we have
1770 * a variable size */
1775 field
->field_size
= type
->size
;
1779 g_error("field_static_size : unknown type");
1786 /*****************************************************************************
1788 * precompute_fields_offsets : set the precomputable offset of the fields
1792 * offset : pointer to the current offset, must be incremented
1794 * return : 1 : found a variable length field, stop the processing.
1796 ****************************************************************************/
1799 gint
precompute_fields_offsets(LttFacility
*fac
, LttField
*field
, off_t
*offset
, gint is_compact
)
1801 LttType
*type
= &field
->field_type
;
1803 if(unlikely(is_compact
)) {
1804 g_assert(field
->field_size
!= 0);
1805 /* FIXME THIS IS A HUUUUUGE hack :
1806 * offset is between the compact_data field in struct LttEvent
1807 * and the address of the field root in the memory map.
1808 * ark. Both will stay at the same addresses while the event
1809 * is readable, so it's ok.
1811 field
->offset_root
= 0;
1812 field
->fixed_root
= FIELD_FIXED
;
1816 switch(type
->type_class
) {
1818 case LTT_UINT_FIXED
:
1833 g_assert(field
->field_size
!= 0);
1834 /* Align offset on type size */
1835 *offset
+= ltt_align(*offset
, get_alignment(field
),
1837 /* remember offset */
1838 field
->offset_root
= *offset
;
1839 field
->fixed_root
= FIELD_FIXED
;
1840 /* Increment offset */
1841 *offset
+= field
->field_size
;
1845 field
->offset_root
= *offset
;
1846 field
->fixed_root
= FIELD_FIXED
;
1850 g_assert(type
->fields
->len
== 1);
1852 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
1854 *offset
+= ltt_align(*offset
, get_alignment(field
),
1857 /* remember offset */
1858 field
->offset_root
= *offset
;
1859 field
->array_offset
= *offset
;
1860 field
->fixed_root
= FIELD_FIXED
;
1862 /* Let the child be variable */
1863 //precompute_fields_offsets(tf, child, offset);
1865 if(field
->field_size
!= 0) {
1866 /* Increment offset */
1867 /* field_size is the array size in bytes */
1868 *offset
+= field
->field_size
;
1876 g_assert(type
->fields
->len
== 2);
1881 *offset
+= ltt_align(*offset
, get_alignment(field
),
1884 /* remember offset */
1885 field
->offset_root
= *offset
;
1886 field
->fixed_root
= FIELD_FIXED
;
1888 child
= &g_array_index(type
->fields
, LttField
, 0);
1889 ret
= precompute_fields_offsets(fac
, child
, offset
, is_compact
);
1890 g_assert(ret
== 0); /* Seq len cannot have variable len */
1892 child
= &g_array_index(type
->fields
, LttField
, 1);
1893 *offset
+= ltt_align(*offset
, get_alignment(child
),
1895 field
->array_offset
= *offset
;
1896 /* Let the child be variable. */
1897 //ret = precompute_fields_offsets(fac, child, offset);
1899 /* Cannot precompute fields offsets of sequence members, and has
1900 * variable length. */
1910 *offset
+= ltt_align(*offset
, get_alignment(field
),
1912 /* remember offset */
1913 field
->offset_root
= *offset
;
1914 field
->fixed_root
= FIELD_FIXED
;
1916 for(i
=0; i
< type
->fields
->len
; i
++) {
1917 child
= &g_array_index(type
->fields
, LttField
, i
);
1918 ret
= precompute_fields_offsets(fac
, child
, offset
, is_compact
);
1931 *offset
+= ltt_align(*offset
, get_alignment(field
),
1933 /* remember offset */
1934 field
->offset_root
= *offset
;
1935 field
->fixed_root
= FIELD_FIXED
;
1937 for(i
=0; i
< type
->fields
->len
; i
++) {
1938 *offset
= field
->offset_root
;
1939 child
= &g_array_index(type
->fields
, LttField
, i
);
1940 ret
= precompute_fields_offsets(fac
, child
, offset
, is_compact
);
1944 *offset
= field
->offset_root
+ field
->field_size
;
1951 g_error("precompute_fields_offsets : unknown type");
1960 /*****************************************************************************
1962 * precompute_offsets : set the precomputable offset of an event type
1965 * event : event type
1967 ****************************************************************************/
1968 void precompute_offsets(LttFacility
*fac
, LttEventType
*event
)
1974 /* First, compute the size of fixed size fields. Will determine size for
1975 * arrays, struct and unions, which is not done by the parser */
1976 for(i
=0; i
<event
->fields
->len
; i
++) {
1977 LttField
*field
= &g_array_index(event
->fields
, LttField
, i
);
1978 field_compute_static_size(fac
, field
);
1981 /* Precompute all known offsets */
1982 for(i
=0; i
<event
->fields
->len
; i
++) {
1983 LttField
*field
= &g_array_index(event
->fields
, LttField
, i
);
1984 if(event
->has_compact_data
&& i
== 0)
1985 ret
= precompute_fields_offsets(fac
, field
, &offset
, 1);
1987 ret
= precompute_fields_offsets(fac
, field
, &offset
, 0);
1995 /*****************************************************************************
1997 * preset_field_type_size : set the fixed sizes of the field type
2000 * event_type : event type
2001 * offset_root : offset from the root
2002 * offset_parent : offset from the parent
2003 * fixed_root : Do we know a fixed offset to the root ?
2004 * fixed_parent : Do we know a fixed offset to the parent ?
2006 ****************************************************************************/
2010 // preset the fixed size offsets. Calculate them just like genevent-new : an
2011 // increment of a *to value that represents the offset from the start of the
2013 // The preset information is : offsets up to (and including) the first element
2014 // of variable size. All subsequent fields must be flagged "VARIABLE OFFSET".
2016 void preset_field_type_size(LttTracefile
*tf
, LttEventType
*event_type
,
2017 off_t offset_root
, off_t offset_parent
,
2018 enum field_status
*fixed_root
, enum field_status
*fixed_parent
,
2021 enum field_status local_fixed_root
, local_fixed_parent
;
2025 g_assert(field
->fixed_root
== FIELD_UNKNOWN
);
2026 g_assert(field
->fixed_parent
== FIELD_UNKNOWN
);
2027 g_assert(field
->fixed_size
== FIELD_UNKNOWN
);
2029 type
= field
->field_type
;
2031 field
->fixed_root
= *fixed_root
;
2032 if(field
->fixed_root
== FIELD_FIXED
)
2033 field
->offset_root
= offset_root
;
2035 field
->offset_root
= 0;
2037 field
->fixed_parent
= *fixed_parent
;
2038 if(field
->fixed_parent
== FIELD_FIXED
)
2039 field
->offset_parent
= offset_parent
;
2041 field
->offset_parent
= 0;
2043 size_t current_root_offset
;
2044 size_t current_offset
;
2045 enum field_status current_child_status
, final_child_status
;
2048 switch(type
->type_class
) {
2050 case LTT_UINT_FIXED
:
2059 field
->field_size
= ltt_type_size(tf
->trace
, type
);
2060 field
->fixed_size
= FIELD_FIXED
;
2063 field
->field_size
= (off_t
)event_type
->facility
->pointer_size
;
2064 field
->fixed_size
= FIELD_FIXED
;
2068 field
->field_size
= (off_t
)event_type
->facility
->long_size
;
2069 field
->fixed_size
= FIELD_FIXED
;
2074 field
->field_size
= (off_t
)event_type
->facility
->size_t_size
;
2075 field
->fixed_size
= FIELD_FIXED
;
2078 local_fixed_root
= FIELD_VARIABLE
;
2079 local_fixed_parent
= FIELD_VARIABLE
;
2080 preset_field_type_size(tf
, event_type
,
2082 &local_fixed_root
, &local_fixed_parent
,
2084 field
->fixed_size
= FIELD_VARIABLE
;
2085 field
->field_size
= 0;
2086 *fixed_root
= FIELD_VARIABLE
;
2087 *fixed_parent
= FIELD_VARIABLE
;
2090 field
->fixed_size
= FIELD_VARIABLE
;
2091 field
->field_size
= 0;
2092 *fixed_root
= FIELD_VARIABLE
;
2093 *fixed_parent
= FIELD_VARIABLE
;
2096 local_fixed_root
= FIELD_VARIABLE
;
2097 local_fixed_parent
= FIELD_VARIABLE
;
2098 preset_field_type_size(tf
, event_type
,
2100 &local_fixed_root
, &local_fixed_parent
,
2102 field
->fixed_size
= field
->child
[0]->fixed_size
;
2103 if(field
->fixed_size
== FIELD_FIXED
) {
2104 field
->field_size
= type
->element_number
* field
->child
[0]->field_size
;
2106 field
->field_size
= 0;
2107 *fixed_root
= FIELD_VARIABLE
;
2108 *fixed_parent
= FIELD_VARIABLE
;
2112 current_root_offset
= field
->offset_root
;
2114 current_child_status
= FIELD_FIXED
;
2115 for(i
=0;i
<type
->element_number
;i
++) {
2116 preset_field_type_size(tf
, event_type
,
2117 current_root_offset
, current_offset
,
2118 fixed_root
, ¤t_child_status
,
2120 if(current_child_status
== FIELD_FIXED
) {
2121 current_root_offset
+= field
->child
[i
]->field_size
;
2122 current_offset
+= field
->child
[i
]->field_size
;
2124 current_root_offset
= 0;
2128 if(current_child_status
!= FIELD_FIXED
) {
2129 *fixed_parent
= current_child_status
;
2130 field
->field_size
= 0;
2131 field
->fixed_size
= current_child_status
;
2133 field
->field_size
= current_offset
;
2134 field
->fixed_size
= FIELD_FIXED
;
2138 current_root_offset
= field
->offset_root
;
2141 final_child_status
= FIELD_FIXED
;
2142 for(i
=0;i
<type
->element_number
;i
++) {
2143 enum field_status current_root_child_status
= FIELD_FIXED
;
2144 enum field_status current_child_status
= FIELD_FIXED
;
2145 preset_field_type_size(tf
, event_type
,
2146 current_root_offset
, current_offset
,
2147 ¤t_root_child_status
, ¤t_child_status
,
2149 if(current_child_status
!= FIELD_FIXED
)
2150 final_child_status
= current_child_status
;
2152 max_size
= max(max_size
, field
->child
[i
]->field_size
);
2154 if(final_child_status
!= FIELD_FIXED
) {
2155 g_error("LTTV does not support variable size fields in unions.");
2156 /* This will stop the application. */
2157 *fixed_root
= final_child_status
;
2158 *fixed_parent
= final_child_status
;
2159 field
->field_size
= 0;
2160 field
->fixed_size
= current_child_status
;
2162 field
->field_size
= max_size
;
2163 field
->fixed_size
= FIELD_FIXED
;
2167 g_error("unexpected type NONE");
2174 /*****************************************************************************
2176 * check_fields_compatibility : Check for compatibility between two fields :
2177 * do they use the same inner structure ?
2179 * event_type1 : event type
2180 * event_type2 : event type
2183 *Returns : 0 if identical
2185 ****************************************************************************/
2186 // this function checks for equality of field types. Therefore, it does not use
2187 // per se offsets. For instance, an aligned version of a structure is
2188 // compatible with an unaligned version of the same structure.
2190 gint
check_fields_compatibility(LttEventType
*event_type1
,
2191 LttEventType
*event_type2
,
2192 LttField
*field1
, LttField
*field2
)
2194 guint different
= 0;
2198 if(field1
== NULL
) {
2199 if(field2
== NULL
) goto end
;
2204 } else if(field2
== NULL
) {
2209 type1
= &field1
->field_type
;
2210 type2
= &field2
->field_type
;
2212 if(type1
->type_class
!= type2
->type_class
) {
2216 if(type1
->network
!= type2
->network
) {
2221 switch(type1
->type_class
) {
2223 case LTT_UINT_FIXED
:
2238 if(field1
->field_size
!= field2
->field_size
)
2245 LttField
*child1
= &g_array_index(type1
->fields
, LttField
, 0);
2246 LttField
*child2
= &g_array_index(type2
->fields
, LttField
, 0);
2248 if(type1
->size
!= type2
->size
)
2250 if(check_fields_compatibility(event_type1
, event_type2
, child1
, child2
))
2256 LttField
*child1
= &g_array_index(type1
->fields
, LttField
, 1);
2257 LttField
*child2
= &g_array_index(type2
->fields
, LttField
, 1);
2259 if(check_fields_compatibility(event_type1
, event_type2
, child1
, child2
))
2269 if(type1
->fields
->len
!= type2
->fields
->len
) {
2274 for(i
=0; i
< type1
->fields
->len
; i
++) {
2277 child1
= &g_array_index(type1
->fields
, LttField
, i
);
2278 child2
= &g_array_index(type2
->fields
, LttField
, i
);
2279 different
= check_fields_compatibility(event_type1
,
2280 event_type2
, child1
, child2
);
2282 if(different
) break;
2288 g_error("check_fields_compatibility : unknown type");
2297 gint
check_fields_compatibility(LttEventType
*event_type1
,
2298 LttEventType
*event_type2
,
2299 LttField
*field1
, LttField
*field2
)
2301 guint different
= 0;
2306 if(field1
== NULL
) {
2307 if(field2
== NULL
) goto end
;
2312 } else if(field2
== NULL
) {
2317 g_assert(field1
->fixed_root
!= FIELD_UNKNOWN
);
2318 g_assert(field2
->fixed_root
!= FIELD_UNKNOWN
);
2319 g_assert(field1
->fixed_parent
!= FIELD_UNKNOWN
);
2320 g_assert(field2
->fixed_parent
!= FIELD_UNKNOWN
);
2321 g_assert(field1
->fixed_size
!= FIELD_UNKNOWN
);
2322 g_assert(field2
->fixed_size
!= FIELD_UNKNOWN
);
2324 type1
= field1
->field_type
;
2325 type2
= field2
->field_type
;
2327 if(type1
->type_class
!= type2
->type_class
) {
2331 if(type1
->element_name
!= type2
->element_name
) {
2336 switch(type1
->type_class
) {
2338 case LTT_UINT_FIXED
:
2353 if(field1
->field_size
!= field2
->field_size
) {
2359 if(type1
->element_number
!= type2
->element_number
) {
2363 for(i
=0;i
<type1
->element_number
;i
++) {
2364 if(type1
->enum_strings
[i
] != type2
->enum_strings
[i
]) {
2371 /* Two elements : size and child */
2372 g_assert(type1
->element_number
!= type2
->element_number
);
2373 for(i
=0;i
<type1
->element_number
;i
++) {
2374 if(check_fields_compatibility(event_type1
, event_type2
,
2375 field1
->child
[0], field2
->child
[0])) {
2384 if(field1
->field_size
!= field2
->field_size
) {
2388 /* Two elements : size and child */
2389 g_assert(type1
->element_number
!= type2
->element_number
);
2390 for(i
=0;i
<type1
->element_number
;i
++) {
2391 if(check_fields_compatibility(event_type1
, event_type2
,
2392 field1
->child
[0], field2
->child
[0])) {
2400 if(type1
->element_number
!= type2
->element_number
) {
2404 for(i
=0;i
<type1
->element_number
;i
++) {
2405 if(check_fields_compatibility(event_type1
, event_type2
,
2406 field1
->child
[0], field2
->child
[0])) {
2419 /*****************************************************************************
2421 * ltt_get_int : get an integer number
2423 * reverse_byte_order: must we reverse the byte order ?
2424 * size : the size of the integer
2425 * ptr : the data pointer
2427 * gint64 : a 64 bits integer
2428 ****************************************************************************/
2430 gint64
ltt_get_int(gboolean reverse_byte_order
, gint size
, void *data
)
2435 case 1: val
= *((gint8
*)data
); break;
2436 case 2: val
= ltt_get_int16(reverse_byte_order
, data
); break;
2437 case 4: val
= ltt_get_int32(reverse_byte_order
, data
); break;
2438 case 8: val
= ltt_get_int64(reverse_byte_order
, data
); break;
2439 default: val
= ltt_get_int64(reverse_byte_order
, data
);
2440 g_critical("get_int : integer size %d unknown", size
);
2447 /*****************************************************************************
2449 * ltt_get_uint : get an unsigned integer number
2451 * reverse_byte_order: must we reverse the byte order ?
2452 * size : the size of the integer
2453 * ptr : the data pointer
2455 * guint64 : a 64 bits unsigned integer
2456 ****************************************************************************/
2458 guint64
ltt_get_uint(gboolean reverse_byte_order
, gint size
, void *data
)
2463 case 1: val
= *((gint8
*)data
); break;
2464 case 2: val
= ltt_get_uint16(reverse_byte_order
, data
); break;
2465 case 4: val
= ltt_get_uint32(reverse_byte_order
, data
); break;
2466 case 8: val
= ltt_get_uint64(reverse_byte_order
, data
); break;
2467 default: val
= ltt_get_uint64(reverse_byte_order
, data
);
2468 g_critical("get_uint : unsigned integer size %d unknown",
2477 /* get the node name of the system */
2479 char * ltt_trace_system_description_node_name (LttSystemDescription
* s
)
2481 return s
->node_name
;
2485 /* get the domain name of the system */
2487 char * ltt_trace_system_description_domain_name (LttSystemDescription
* s
)
2489 return s
->domain_name
;
2493 /* get the description of the system */
2495 char * ltt_trace_system_description_description (LttSystemDescription
* s
)
2497 return s
->description
;
2501 /* get the NTP corrected start time of the trace */
2502 LttTime
ltt_trace_start_time(LttTrace
*t
)
2504 return t
->start_time
;
2507 /* get the monotonic start time of the trace */
2508 LttTime
ltt_trace_start_time_monotonic(LttTrace
*t
)
2510 return t
->start_time_from_tsc
;
2513 static LttTracefile
*ltt_tracefile_new()
2516 tf
= g_new(LttTracefile
, 1);
2517 tf
->event
.tracefile
= tf
;
2521 static void ltt_tracefile_destroy(LttTracefile
*tf
)
2526 static void ltt_tracefile_copy(LttTracefile
*dest
, const LttTracefile
*src
)
2531 /* Before library loading... */
2533 static __attribute__((constructor
)) void init(void)
2535 LTT_TRACEFILE_NAME_METADATA
= g_quark_from_string("/control/metadata");