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_1
*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 if(munmap(tf
->buffer
.head
,
270 PAGE_ALIGN(ltt_subbuffer_header_size()))) {
271 g_warning("unmap size : %u\n",
272 PAGE_ALIGN(ltt_subbuffer_header_size()));
273 perror("munmap error");
276 tf
->buffer
.head
= NULL
;
278 //read the first block
279 if(map_block(tf
,0)) {
280 perror("Cannot map block for tracefile");
288 if(munmap(tf
->buffer
.head
,
289 PAGE_ALIGN(ltt_subbuffer_header_size()))) {
290 g_warning("unmap size : %u\n",
291 PAGE_ALIGN(ltt_subbuffer_header_size()));
292 perror("munmap error");
302 /*****************************************************************************
304 * ltt_tracefile_close: close a trace file,
306 * t : tracefile which will be closed
307 ****************************************************************************/
309 static void ltt_tracefile_close(LttTracefile
*t
)
311 int page_size
= getpagesize();
313 if(t
->buffer
.head
!= NULL
)
314 if(munmap(t
->buffer
.head
, PAGE_ALIGN(t
->buf_size
))) {
315 g_warning("unmap size : %u\n",
316 PAGE_ALIGN(t
->buf_size
));
317 perror("munmap error");
324 /****************************************************************************
325 * get_absolute_pathname
327 * return the unique pathname in the system
329 * MD : Fixed this function so it uses realpath, dealing well with
330 * forgotten cases (.. were not used correctly before).
332 ****************************************************************************/
333 void get_absolute_pathname(const gchar
*pathname
, gchar
* abs_pathname
)
335 abs_pathname
[0] = '\0';
337 if (realpath(pathname
, abs_pathname
) != NULL
)
341 /* error, return the original path unmodified */
342 strcpy(abs_pathname
, pathname
);
348 /* Search for something like : .*_.*
350 * The left side is the name, the right side is the number.
353 static int get_tracefile_name_number(gchar
*raw_name
,
360 guint raw_name_len
= strlen(raw_name
);
361 gchar char_name
[PATH_MAX
];
368 for(i
=raw_name_len
-1;i
>=0;i
--) {
369 if(raw_name
[i
] == '_') break;
371 if(i
==-1) { /* Either not found or name length is 0 */
372 /* This is a userspace tracefile */
373 strncpy(char_name
, raw_name
, raw_name_len
);
374 char_name
[raw_name_len
] = '\0';
375 *name
= g_quark_from_string(char_name
);
376 *num
= 0; /* unknown cpu */
377 for(i
=0;i
<raw_name_len
;i
++) {
378 if(raw_name
[i
] == '/') {
383 for(;i
<raw_name_len
;i
++) {
384 if(raw_name
[i
] == '/') {
389 for(;i
<raw_name_len
;i
++) {
390 if(raw_name
[i
] == '-') {
394 if(i
== raw_name_len
) return -1;
396 tmpptr
= &raw_name
[i
];
397 for(;i
<raw_name_len
;i
++) {
398 if(raw_name
[i
] == '.') {
403 *tid
= strtoul(tmpptr
, &endptr
, 10);
405 return -1; /* No digit */
406 if(*tid
== ULONG_MAX
)
407 return -1; /* underflow / overflow */
409 tmpptr
= &raw_name
[i
];
410 for(;i
<raw_name_len
;i
++) {
411 if(raw_name
[i
] == '.') {
416 *pgid
= strtoul(tmpptr
, &endptr
, 10);
418 return -1; /* No digit */
419 if(*pgid
== ULONG_MAX
)
420 return -1; /* underflow / overflow */
422 tmpptr
= &raw_name
[i
];
423 *creation
= strtoull(tmpptr
, &endptr
, 10);
425 return -1; /* No digit */
426 if(*creation
== G_MAXUINT64
)
427 return -1; /* underflow / overflow */
431 cpu_num
= strtol(raw_name
+underscore_pos
+1, &endptr
, 10);
433 if(endptr
== raw_name
+underscore_pos
+1)
434 return -1; /* No digit */
435 if(cpu_num
== LONG_MIN
|| cpu_num
== LONG_MAX
)
436 return -1; /* underflow / overflow */
438 strncpy(char_name
, raw_name
, underscore_pos
);
439 char_name
[underscore_pos
] = '\0';
441 *name
= g_quark_from_string(char_name
);
450 GData
**ltt_trace_get_tracefiles_groups(LttTrace
*trace
)
452 return &trace
->tracefiles
;
456 void compute_tracefile_group(GQuark key_id
,
458 struct compute_tracefile_group_args
*args
)
463 for(i
=0; i
<group
->len
; i
++) {
464 tf
= &g_array_index (group
, LttTracefile
, i
);
466 args
->func(tf
, args
->func_args
);
471 static void ltt_tracefile_group_destroy(gpointer data
)
473 GArray
*group
= (GArray
*)data
;
477 for(i
=0; i
<group
->len
; i
++) {
478 tf
= &g_array_index (group
, LttTracefile
, i
);
480 ltt_tracefile_close(tf
);
482 g_array_free(group
, TRUE
);
485 static gboolean
ltt_tracefile_group_has_cpu_online(gpointer data
)
487 GArray
*group
= (GArray
*)data
;
491 for(i
=0; i
<group
->len
; i
++) {
492 tf
= &g_array_index (group
, LttTracefile
, i
);
500 /* Open each tracefile under a specific directory. Put them in a
501 * GData : permits to access them using their tracefile group pathname.
502 * i.e. access control/modules tracefile group by index :
505 * relative path is the path relative to the trace root
506 * root path is the full path
508 * A tracefile group is simply an array where all the per cpu tracefiles sit.
511 static int open_tracefiles(LttTrace
*trace
, gchar
*root_path
, gchar
*relative_path
)
513 DIR *dir
= opendir(root_path
);
514 struct dirent
*entry
;
515 struct stat stat_buf
;
518 gchar path
[PATH_MAX
];
523 gchar rel_path
[PATH_MAX
];
532 strncpy(path
, root_path
, PATH_MAX
-1);
533 path_len
= strlen(path
);
534 path
[path_len
] = '/';
536 path_ptr
= path
+ path_len
;
538 strncpy(rel_path
, relative_path
, PATH_MAX
-1);
539 rel_path_len
= strlen(rel_path
);
540 rel_path
[rel_path_len
] = '/';
542 rel_path_ptr
= rel_path
+ rel_path_len
;
544 while((entry
= readdir(dir
)) != NULL
) {
546 if(entry
->d_name
[0] == '.') continue;
548 strncpy(path_ptr
, entry
->d_name
, PATH_MAX
- path_len
);
549 strncpy(rel_path_ptr
, entry
->d_name
, PATH_MAX
- rel_path_len
);
551 ret
= stat(path
, &stat_buf
);
557 g_debug("Tracefile file or directory : %s\n", path
);
559 // if(strcmp(rel_path, "/eventdefs") == 0) continue;
561 if(S_ISDIR(stat_buf
.st_mode
)) {
563 g_debug("Entering subdirectory...\n");
564 ret
= open_tracefiles(trace
, path
, rel_path
);
565 if(ret
< 0) continue;
566 } else if(S_ISREG(stat_buf
.st_mode
)) {
575 if(get_tracefile_name_number(rel_path
, &name
, &num
, &tid
, &pgid
, &creation
))
576 continue; /* invalid name */
578 g_debug("Opening file.\n");
579 if(ltt_tracefile_open(trace
, path
, &tmp_tf
)) {
580 g_info("Error opening tracefile %s", path
);
582 continue; /* error opening the tracefile : bad magic number ? */
585 g_debug("Tracefile name is %s and number is %u",
586 g_quark_to_string(name
), num
);
588 tmp_tf
.cpu_online
= 1;
589 tmp_tf
.cpu_num
= num
;
593 tmp_tf
.creation
= creation
;
594 group
= g_datalist_id_get_data(&trace
->tracefiles
, name
);
596 /* Elements are automatically cleared when the array is allocated.
597 * It makes the cpu_online variable set to 0 : cpu offline, by default.
599 group
= g_array_sized_new (FALSE
, TRUE
, sizeof(LttTracefile
), 10);
600 g_datalist_id_set_data_full(&trace
->tracefiles
, name
,
601 group
, ltt_tracefile_group_destroy
);
604 /* Add the per cpu tracefile to the named group */
605 unsigned int old_len
= group
->len
;
607 group
= g_array_set_size(group
, num
+1);
608 g_array_index (group
, LttTracefile
, num
) = tmp_tf
;
609 g_array_index (group
, LttTracefile
, num
).event
.tracefile
=
610 &g_array_index (group
, LttTracefile
, num
);
620 /* Presumes the tracefile is already seeked at the beginning. It makes sense,
621 * because it must be done just after the opening */
622 static int ltt_process_metadata_tracefile(LttTracefile
*tf
)
628 err
= ltt_tracefile_read_seek(tf
);
629 if(err
== EPERM
) goto seek_error
;
630 else if(err
== ERANGE
) break; /* End of tracefile */
632 err
= ltt_tracefile_read_update_event(tf
);
633 if(err
) goto update_error
;
636 * It contains only core events :
638 * 1 : set_marker_format
640 if(tf
->event
.event_id
>= MARKER_CORE_IDS
) {
641 /* Should only contain core events */
642 g_warning("Error in processing metadata file %s, "
643 "should not contain event id %u.", g_quark_to_string(tf
->name
),
649 const char *marker_name
, *format
;
651 guint8 int_size
, long_size
, pointer_size
, size_t_size
, alignment
;
653 switch((enum marker_id
)tf
->event
.event_id
) {
654 case MARKER_ID_SET_MARKER_ID
:
655 marker_name
= pos
= tf
->event
.data
;
656 g_debug("Doing MARKER_ID_SET_MARKER_ID of marker %s", marker_name
);
657 pos
+= strlen(marker_name
) + 1;
658 pos
+= ltt_align((size_t)pos
, sizeof(guint16
), tf
->alignment
);
659 id
= ltt_get_uint16(LTT_GET_BO(tf
), pos
);
660 g_debug("In MARKER_ID_SET_MARKER_ID of marker %s id %hu",
662 pos
+= sizeof(guint16
);
663 int_size
= *(guint8
*)pos
;
664 pos
+= sizeof(guint8
);
665 long_size
= *(guint8
*)pos
;
666 pos
+= sizeof(guint8
);
667 pointer_size
= *(guint8
*)pos
;
668 pos
+= sizeof(guint8
);
669 size_t_size
= *(guint8
*)pos
;
670 pos
+= sizeof(guint8
);
671 alignment
= *(guint8
*)pos
;
672 pos
+= sizeof(guint8
);
673 marker_id_event(tf
->trace
, g_quark_from_string(marker_name
),
674 id
, int_size
, long_size
,
675 pointer_size
, size_t_size
, alignment
);
677 case MARKER_ID_SET_MARKER_FORMAT
:
678 marker_name
= pos
= tf
->event
.data
;
679 g_debug("Doing MARKER_ID_SET_MARKER_FORMAT of marker %s",
681 pos
+= strlen(marker_name
) + 1;
683 pos
+= strlen(format
) + 1;
684 marker_format_event(tf
->trace
, g_quark_from_string(marker_name
),
686 /* get information from dictionary TODO */
689 g_warning("Error in processing metadata file %s, "
690 "unknown event id %hhu.",
691 g_quark_to_string(tf
->name
),
704 g_warning("An error occured in metadata tracefile parsing");
709 * Open a trace and return its LttTrace handle.
711 * pathname must be the directory of the trace
714 LttTrace
*ltt_trace_open(const gchar
*pathname
)
716 gchar abs_path
[PATH_MAX
];
721 ltt_subbuffer_header_t
*header
;
723 struct dirent
*entry
;
724 guint control_found
= 0;
725 struct stat stat_buf
;
726 gchar path
[PATH_MAX
];
728 t
= g_new(LttTrace
, 1);
729 if(!t
) goto alloc_error
;
731 get_absolute_pathname(pathname
, abs_path
);
732 t
->pathname
= g_quark_from_string(abs_path
);
734 g_datalist_init(&t
->tracefiles
);
736 /* Test to see if it looks like a trace */
737 dir
= opendir(abs_path
);
742 while((entry
= readdir(dir
)) != NULL
) {
743 strcpy(path
, abs_path
);
745 strcat(path
, entry
->d_name
);
746 ret
= stat(path
, &stat_buf
);
751 if(S_ISDIR(stat_buf
.st_mode
)) {
752 if(strcmp(entry
->d_name
, "control") == 0) {
759 if(!control_found
) goto find_error
;
761 /* Open all the tracefiles */
762 if(open_tracefiles(t
, abs_path
, "")) {
763 g_warning("Error opening tracefile %s", abs_path
);
767 /* Parse each trace control/metadata_N files : get runtime fac. info */
768 group
= g_datalist_id_get_data(&t
->tracefiles
, LTT_TRACEFILE_NAME_METADATA
);
770 g_error("Trace %s has no metadata tracefile", abs_path
);
776 * Get the trace information for the control/metadata_0 tracefile.
777 * Getting a correct trace start_time and start_tsc is insured by the fact
778 * that no subbuffers are supposed to be lost in the metadata channel.
779 * Therefore, the first subbuffer contains the start_tsc timestamp in its
782 g_assert(group
->len
> 0);
783 tf
= &g_array_index (group
, LttTracefile
, 0);
784 header
= (ltt_subbuffer_header_t
*)tf
->buffer
.head
;
785 ret
= parse_trace_header(header
, tf
, t
);
788 t
->num_cpu
= group
->len
;
790 ret
= allocate_marker_data(t
);
792 g_error("Error in allocating marker data");
794 for(i
=0; i
<group
->len
; i
++) {
795 tf
= &g_array_index (group
, LttTracefile
, i
);
797 if(ltt_process_metadata_tracefile(tf
))
805 destroy_marker_data(t
);
807 g_datalist_clear(&t
->tracefiles
);
815 /* Open another, completely independant, instance of a trace.
817 * A read on this new instance will read the first event of the trace.
819 * When we copy a trace, we want all the opening actions to happen again :
820 * the trace will be reopened and totally independant from the original.
821 * That's why we call ltt_trace_open.
823 LttTrace
*ltt_trace_copy(LttTrace
*self
)
825 return ltt_trace_open(g_quark_to_string(self
->pathname
));
832 void ltt_trace_close(LttTrace
*t
)
834 g_datalist_clear(&t
->tracefiles
);
839 /*****************************************************************************
840 * Get the start time and end time of the trace
841 ****************************************************************************/
843 void ltt_tracefile_time_span_get(LttTracefile
*tf
,
844 LttTime
*start
, LttTime
*end
)
848 err
= map_block(tf
, 0);
850 g_error("Can not map block");
851 *start
= ltt_time_infinite
;
853 *start
= tf
->buffer
.begin
.timestamp
;
855 err
= map_block(tf
, tf
->num_blocks
- 1); /* Last block */
857 g_error("Can not map block");
858 *end
= ltt_time_zero
;
860 *end
= tf
->buffer
.end
.timestamp
;
863 struct tracefile_time_span_get_args
{
869 static void group_time_span_get(GQuark name
, gpointer data
, gpointer user_data
)
871 struct tracefile_time_span_get_args
*args
=
872 (struct tracefile_time_span_get_args
*)user_data
;
874 GArray
*group
= (GArray
*)data
;
880 for(i
=0; i
<group
->len
; i
++) {
881 tf
= &g_array_index (group
, LttTracefile
, i
);
883 ltt_tracefile_time_span_get(tf
, &tmp_start
, &tmp_end
);
884 if(ltt_time_compare(*args
->start
, tmp_start
)>0) *args
->start
= tmp_start
;
885 if(ltt_time_compare(*args
->end
, tmp_end
)<0) *args
->end
= tmp_end
;
890 /* return the start and end time of a trace */
892 void ltt_trace_time_span_get(LttTrace
*t
, LttTime
*start
, LttTime
*end
)
894 LttTime min_start
= ltt_time_infinite
;
895 LttTime max_end
= ltt_time_zero
;
896 struct tracefile_time_span_get_args args
= { t
, &min_start
, &max_end
};
898 g_datalist_foreach(&t
->tracefiles
, &group_time_span_get
, &args
);
900 if(start
!= NULL
) *start
= min_start
;
901 if(end
!= NULL
) *end
= max_end
;
906 /* Seek to the first event in a tracefile that has a time equal or greater than
907 * the time passed in parameter.
909 * If the time parameter is outside the tracefile time span, seek to the first
910 * event or if after, return ERANGE.
912 * If the time parameter is before the first event, we have to seek specially to
915 * If the time is after the end of the trace, return ERANGE.
917 * Do a binary search to find the right block, then a sequential search in the
918 * block to find the event.
920 * In the special case where the time requested fits inside a block that has no
921 * event corresponding to the requested time, the first event of the next block
924 * IMPORTANT NOTE : // FIXME everywhere...
926 * You MUST NOT do a ltt_tracefile_read right after a ltt_tracefile_seek_time :
927 * you will jump over an event if you do.
929 * Return value : 0 : no error, the tf->event can be used
930 * ERANGE : time if after the last event of the trace
931 * otherwise : this is an error.
935 int ltt_tracefile_seek_time(LttTracefile
*tf
, LttTime time
)
939 unsigned int block_num
, high
, low
;
941 /* seek at the beginning of trace */
942 err
= map_block(tf
, 0); /* First block */
944 g_error("Can not map block");
948 /* If the time is lower or equal the beginning of the trace,
949 * go to the first event. */
950 if(ltt_time_compare(time
, tf
->buffer
.begin
.timestamp
) <= 0) {
951 ret
= ltt_tracefile_read(tf
);
952 if(ret
== ERANGE
) goto range
;
953 else if (ret
) goto fail
;
954 goto found
; /* There is either no event in the trace or the event points
955 to the first event in the trace */
958 err
= map_block(tf
, tf
->num_blocks
- 1); /* Last block */
960 g_error("Can not map block");
964 /* If the time is after the end of the trace, return ERANGE. */
965 if(ltt_time_compare(time
, tf
->buffer
.end
.timestamp
) > 0) {
969 /* Binary search the block */
970 high
= tf
->num_blocks
- 1;
974 block_num
= ((high
-low
) / 2) + low
;
976 err
= map_block(tf
, block_num
);
978 g_error("Can not map block");
982 /* We cannot divide anymore : this is what would happen if the time
983 * requested was exactly between two consecutive buffers'end and start
984 * timestamps. This is also what would happend if we didn't deal with out
985 * of span cases prior in this function. */
986 /* The event is right in the buffer!
987 * (or in the next buffer first event) */
989 ret
= ltt_tracefile_read(tf
);
990 if(ret
== ERANGE
) goto range
; /* ERANGE or EPERM */
991 else if(ret
) goto fail
;
993 if(ltt_time_compare(time
, tf
->event
.event_time
) <= 0)
997 } else if(ltt_time_compare(time
, tf
->buffer
.begin
.timestamp
) < 0) {
998 /* go to lower part */
999 high
= block_num
- 1;
1000 } else if(ltt_time_compare(time
, tf
->buffer
.end
.timestamp
) > 0) {
1001 /* go to higher part */
1002 low
= block_num
+ 1;
1003 } else {/* The event is right in the buffer!
1004 (or in the next buffer first event) */
1006 ret
= ltt_tracefile_read(tf
);
1007 if(ret
== ERANGE
) goto range
; /* ERANGE or EPERM */
1008 else if(ret
) goto fail
;
1010 if(ltt_time_compare(time
, tf
->event
.event_time
) <= 0)
1022 /* Error handling */
1024 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1025 g_quark_to_string(tf
->name
));
1029 /* Seek to a position indicated by an LttEventPosition
1032 int ltt_tracefile_seek_position(LttTracefile
*tf
, const LttEventPosition
*ep
)
1036 if(ep
->tracefile
!= tf
) {
1040 err
= map_block(tf
, ep
->block
);
1042 g_error("Can not map block");
1046 tf
->event
.offset
= ep
->offset
;
1048 /* Put back the event real tsc */
1049 tf
->event
.tsc
= ep
->tsc
;
1050 tf
->buffer
.tsc
= ep
->tsc
;
1052 err
= ltt_tracefile_read_update_event(tf
);
1055 /* deactivate this, as it does nothing for now
1056 err = ltt_tracefile_read_op(tf);
1063 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1064 g_quark_to_string(tf
->name
));
1068 /* Given a TSC value, return the LttTime (seconds,nanoseconds) it
1072 LttTime
ltt_interpolate_time_from_tsc(LttTracefile
*tf
, guint64 tsc
)
1076 if(tsc
> tf
->trace
->start_tsc
) {
1077 time
= ltt_time_from_uint64(
1078 (double)(tsc
- tf
->trace
->start_tsc
)
1079 * (1000000000.0 / tf
->trace
->freq_scale
)
1080 / (double)tf
->trace
->start_freq
);
1081 time
= ltt_time_add(tf
->trace
->start_time_from_tsc
, time
);
1083 time
= ltt_time_from_uint64(
1084 (double)(tf
->trace
->start_tsc
- tsc
)
1085 * (1000000000.0 / tf
->trace
->freq_scale
)
1086 / (double)tf
->trace
->start_freq
);
1087 time
= ltt_time_sub(tf
->trace
->start_time_from_tsc
, time
);
1092 /* Calculate the real event time based on the buffer boundaries */
1093 LttTime
ltt_interpolate_time(LttTracefile
*tf
, LttEvent
*event
)
1095 return ltt_interpolate_time_from_tsc(tf
, tf
->buffer
.tsc
);
1099 /* Get the current event of the tracefile : valid until the next read */
1100 LttEvent
*ltt_tracefile_get_event(LttTracefile
*tf
)
1107 /*****************************************************************************
1109 * ltt_tracefile_read : Read the next event in the tracefile
1114 * Returns 0 if an event can be used in tf->event.
1115 * Returns ERANGE on end of trace. The event in tf->event still can be used
1116 * (if the last block was not empty).
1117 * Returns EPERM on error.
1119 * This function does make the tracefile event structure point to the event
1120 * currently pointed to by the tf->event.
1122 * Note : you must call a ltt_tracefile_seek to the beginning of the trace to
1123 * reinitialize it after an error if you want results to be coherent.
1124 * It would be the case if a end of trace last buffer has no event : the end
1125 * of trace wouldn't be returned, but an error.
1126 * We make the assumption there is at least one event per buffer.
1127 ****************************************************************************/
1129 int ltt_tracefile_read(LttTracefile
*tf
)
1133 err
= ltt_tracefile_read_seek(tf
);
1135 err
= ltt_tracefile_read_update_event(tf
);
1138 /* deactivate this, as it does nothing for now
1139 err = ltt_tracefile_read_op(tf);
1146 int ltt_tracefile_read_seek(LttTracefile
*tf
)
1150 /* Get next buffer until we finally have an event, or end of trace */
1152 err
= ltt_seek_next_event(tf
);
1153 if(unlikely(err
== ENOPROTOOPT
)) {
1157 /* Are we at the end of the buffer ? */
1159 if(unlikely(tf
->buffer
.index
== tf
->num_blocks
-1)){ /* end of trace ? */
1162 /* get next block */
1163 err
= map_block(tf
, tf
->buffer
.index
+ 1);
1165 g_error("Can not map block");
1169 } else break; /* We found an event ! */
1175 /* do an operation when reading a new event */
1177 /* This function does nothing for now */
1179 int ltt_tracefile_read_op(LttTracefile
*tf
)
1185 /* do event specific operation */
1193 static void print_debug_event_header(LttEvent
*ev
, void *start_pos
, void *end_pos
)
1195 unsigned int offset
= 0;
1198 g_printf("Event header (tracefile %s offset %llx):\n",
1199 g_quark_to_string(ev
->tracefile
->long_name
),
1200 ((uint64_t)ev
->tracefile
->buffer
.index
* ev
->tracefile
->buf_size
)
1201 + (long)start_pos
- (long)ev
->tracefile
->buffer
.head
);
1203 while (offset
< (long)end_pos
- (long)start_pos
) {
1204 g_printf("%8lx", (long)start_pos
- (long)ev
->tracefile
->buffer
.head
+ offset
);
1207 for (i
= 0; i
< 4 ; i
++) {
1208 for (j
= 0; j
< 4; j
++) {
1209 if (offset
+ ((i
* 4) + j
) <
1210 (long)end_pos
- (long)start_pos
)
1212 ((char*)start_pos
)[offset
+ ((i
* 4) + j
)]);
1226 /* same as ltt_tracefile_read, but does not seek to the next event nor call
1227 * event specific operation. */
1228 int ltt_tracefile_read_update_event(LttTracefile
*tf
)
1235 pos
= tf
->buffer
.head
+ event
->offset
;
1237 /* Read event header */
1239 /* Align the head */
1240 pos
+= ltt_align((size_t)pos
, sizeof(guint32
), tf
->alignment
);
1243 event
->timestamp
= ltt_get_uint32(LTT_GET_BO(tf
), pos
);
1244 event
->event_id
= event
->timestamp
>> tf
->tscbits
;
1245 event
->timestamp
= event
->timestamp
& tf
->tsc_mask
;
1246 pos
+= sizeof(guint32
);
1248 switch (event
->event_id
) {
1249 case 29: /* LTT_RFLAG_ID_SIZE_TSC */
1250 event
->event_id
= ltt_get_uint16(LTT_GET_BO(tf
), pos
);
1251 pos
+= sizeof(guint16
);
1252 event
->event_size
= ltt_get_uint16(LTT_GET_BO(tf
), pos
);
1253 pos
+= sizeof(guint16
);
1254 if (event
->event_size
== 0xFFFF) {
1255 event
->event_size
= ltt_get_uint32(LTT_GET_BO(tf
), pos
);
1256 pos
+= sizeof(guint32
);
1258 pos
+= ltt_align((size_t)pos
, sizeof(guint64
), tf
->alignment
);
1259 tf
->buffer
.tsc
= ltt_get_uint64(LTT_GET_BO(tf
), pos
);
1260 pos
+= sizeof(guint64
);
1262 case 30: /* LTT_RFLAG_ID_SIZE */
1263 event
->event_id
= ltt_get_uint16(LTT_GET_BO(tf
), pos
);
1264 pos
+= sizeof(guint16
);
1265 event
->event_size
= ltt_get_uint16(LTT_GET_BO(tf
), pos
);
1266 pos
+= sizeof(guint16
);
1267 if (event
->event_size
== 0xFFFF) {
1268 event
->event_size
= ltt_get_uint32(LTT_GET_BO(tf
), pos
);
1269 pos
+= sizeof(guint32
);
1272 case 31: /* LTT_RFLAG_ID */
1273 event
->event_id
= ltt_get_uint16(LTT_GET_BO(tf
), pos
);
1274 pos
+= sizeof(guint16
);
1275 event
->event_size
= G_MAXUINT
;
1278 event
->event_size
= G_MAXUINT
;
1282 if (likely(event
->event_id
!= 29)) {
1283 /* No extended timestamp */
1284 if (event
->timestamp
< (tf
->buffer
.tsc
& tf
->tsc_mask
))
1285 tf
->buffer
.tsc
= ((tf
->buffer
.tsc
& ~tf
->tsc_mask
) /* overflow */
1286 + tf
->tsc_mask_next_bit
)
1287 | (guint64
)event
->timestamp
;
1289 tf
->buffer
.tsc
= (tf
->buffer
.tsc
& ~tf
->tsc_mask
) /* no overflow */
1290 | (guint64
)event
->timestamp
;
1292 event
->tsc
= tf
->buffer
.tsc
;
1294 event
->event_time
= ltt_interpolate_time(tf
, event
);
1297 print_debug_event_header(event
, pos_aligned
, pos
);
1302 * Let ltt_update_event_size update event->data according to the largest
1303 * alignment within the payload.
1304 * Get the data size and update the event fields with the current
1306 ltt_update_event_size(tf
);
1312 /****************************************************************************
1314 * map_block : map a block from the file
1316 * lttdes : ltt trace file
1317 * whichBlock : the block which will be read
1320 * EINVAL : lseek fail
1321 * EIO : can not read from the file
1322 ****************************************************************************/
1324 static gint
map_block(LttTracefile
* tf
, guint block_num
)
1326 int page_size
= getpagesize();
1327 ltt_subbuffer_header_t
*header
;
1329 g_assert(block_num
< tf
->num_blocks
);
1331 if(tf
->buffer
.head
!= NULL
) {
1332 if(munmap(tf
->buffer
.head
, PAGE_ALIGN(tf
->buf_size
))) {
1333 g_warning("unmap size : %u\n",
1334 PAGE_ALIGN(tf
->buf_size
));
1335 perror("munmap error");
1340 /* Multiple of pages aligned head */
1341 tf
->buffer
.head
= mmap(0,
1342 PAGE_ALIGN(tf
->buf_size
),
1343 PROT_READ
, MAP_PRIVATE
, tf
->fd
,
1344 PAGE_ALIGN((off_t
)tf
->buf_size
* (off_t
)block_num
));
1346 if(tf
->buffer
.head
== MAP_FAILED
) {
1347 perror("Error in allocating memory for buffer of tracefile");
1351 g_assert( ( (gulong
)tf
->buffer
.head
&(8-1) ) == 0); // make sure it's aligned.
1354 tf
->buffer
.index
= block_num
;
1356 header
= (ltt_subbuffer_header_t
*)tf
->buffer
.head
;
1358 tf
->buffer
.begin
.cycle_count
= ltt_get_uint64(LTT_GET_BO(tf
),
1359 &header
->cycle_count_begin
);
1360 tf
->buffer
.begin
.freq
= tf
->trace
->start_freq
;
1362 tf
->buffer
.begin
.timestamp
= ltt_interpolate_time_from_tsc(tf
,
1363 tf
->buffer
.begin
.cycle_count
);
1364 tf
->buffer
.end
.cycle_count
= ltt_get_uint64(LTT_GET_BO(tf
),
1365 &header
->cycle_count_end
);
1366 tf
->buffer
.end
.freq
= tf
->trace
->start_freq
;
1368 tf
->buffer
.lost_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1369 &header
->lost_size
);
1370 tf
->buffer
.end
.timestamp
= ltt_interpolate_time_from_tsc(tf
,
1371 tf
->buffer
.end
.cycle_count
);
1372 tf
->buffer
.tsc
= tf
->buffer
.begin
.cycle_count
;
1373 tf
->event
.tsc
= tf
->buffer
.tsc
;
1374 tf
->buffer
.freq
= tf
->buffer
.begin
.freq
;
1377 * eventually support variable buffer size : will need a partial pre-read of
1378 * the headers to create an index when we open the trace... eventually. */
1379 g_assert(tf
->buf_size
== ltt_get_uint32(LTT_GET_BO(tf
),
1380 &header
->buf_size
));
1382 /* Make the current event point to the beginning of the buffer :
1383 * it means that the event read must get the first event. */
1384 tf
->event
.tracefile
= tf
;
1385 tf
->event
.block
= block_num
;
1386 tf
->event
.offset
= 0;
1394 static void print_debug_event_data(LttEvent
*ev
)
1396 unsigned int offset
= 0;
1399 if (!max(ev
->event_size
, ev
->data_size
))
1402 g_printf("Event data (tracefile %s offset %llx):\n",
1403 g_quark_to_string(ev
->tracefile
->long_name
),
1404 ((uint64_t)ev
->tracefile
->buffer
.index
* ev
->tracefile
->buf_size
)
1405 + (long)ev
->data
- (long)ev
->tracefile
->buffer
.head
);
1407 while (offset
< max(ev
->event_size
, ev
->data_size
)) {
1408 g_printf("%8lx", (long)ev
->data
+ offset
1409 - (long)ev
->tracefile
->buffer
.head
);
1412 for (i
= 0; i
< 4 ; i
++) {
1413 for (j
= 0; j
< 4; j
++) {
1414 if (offset
+ ((i
* 4) + j
) < max(ev
->event_size
, ev
->data_size
))
1415 g_printf("%02hhX", ((char*)ev
->data
)[offset
+ ((i
* 4) + j
)]);
1426 for (i
= 0; i
< 4; i
++) {
1427 for (j
= 0; j
< 4; j
++) {
1428 if (offset
+ ((i
* 4) + j
) < max(ev
->event_size
, ev
->data_size
)) {
1429 if (isprint(((char*)ev
->data
)[offset
+ ((i
* 4) + j
)]))
1430 g_printf("%c", ((char*)ev
->data
)[offset
+ ((i
* 4) + j
)]);
1442 /* It will update the fields offsets too */
1443 void ltt_update_event_size(LttTracefile
*tf
)
1447 struct marker_info
*info
;
1449 switch((enum marker_id
)tf
->event
.event_id
) {
1450 case MARKER_ID_SET_MARKER_ID
:
1451 size
= strlen((char*)tf
->event
.data
) + 1;
1452 g_debug("marker %s id set", (char*)tf
->event
.data
);
1453 size
+= ltt_align(size
, sizeof(guint16
), tf
->alignment
);
1454 size
+= sizeof(guint16
);
1455 size
+= sizeof(guint8
);
1456 size
+= sizeof(guint8
);
1457 size
+= sizeof(guint8
);
1458 size
+= sizeof(guint8
);
1459 size
+= sizeof(guint8
);
1461 case MARKER_ID_SET_MARKER_FORMAT
:
1462 g_debug("marker %s format set", (char*)tf
->event
.data
);
1463 size
= strlen((char*)tf
->event
.data
) + 1;
1464 size
+= strlen((char*)tf
->event
.data
+ size
) + 1;
1468 info
= marker_get_info_from_id(tf
->trace
, tf
->event
.event_id
);
1470 if (tf
->event
.event_id
>= MARKER_CORE_IDS
)
1471 g_assert(info
!= NULL
);
1473 /* Do not update field offsets of core markers when initially reading the
1474 * metadata tracefile when the infos about these markers do not exist yet.
1476 if (likely(info
&& info
->fields
)) {
1478 tf
->event
.data
+= ltt_align((off_t
)(unsigned long)tf
->event
.data
,
1479 info
->largest_align
,
1481 /* size, dynamically computed */
1482 if (info
->size
!= -1)
1485 size
= marker_update_fields_offsets(marker_get_info_from_id(tf
->trace
,
1486 tf
->event
.event_id
), tf
->event
.data
);
1489 tf
->event
.data_size
= size
;
1491 /* Check consistency between kernel and LTTV structure sizes */
1492 if(tf
->event
.event_size
== G_MAXUINT
) {
1493 /* Event size too big to fit in the event size field */
1494 tf
->event
.event_size
= tf
->event
.data_size
;
1498 print_debug_event_data(&tf
->event
);
1500 /* Having a marker load or marker format event out of the metadata
1501 * tracefiles is a serious bug. */
1502 switch((enum marker_id
)tf
->event
.event_id
) {
1503 case MARKER_ID_SET_MARKER_ID
:
1504 case MARKER_ID_SET_MARKER_FORMAT
:
1505 if (tf
->name
!= g_quark_from_string("/control/metadata"))
1506 g_error("Trace inconsistency : metadata event found in data "
1507 "tracefile %s", g_quark_to_string(tf
->long_name
));
1510 if (tf
->event
.data_size
!= tf
->event
.event_size
) {
1511 struct marker_info
*info
= marker_get_info_from_id(tf
->trace
,
1512 tf
->event
.event_id
);
1513 g_error("Kernel/LTTV event size differs for event %s: kernel %u, LTTV %u",
1514 g_quark_to_string(info
->name
),
1515 tf
->event
.event_size
, tf
->event
.data_size
);
1521 /* Take the tf current event offset and use the event id to figure out where is
1522 * the next event offset.
1524 * This is an internal function not aiming at being used elsewhere : it will
1525 * not jump over the current block limits. Please consider using
1526 * ltt_tracefile_read to do this.
1528 * Returns 0 on success
1529 * ERANGE if we are at the end of the buffer.
1530 * ENOPROTOOPT if an error occured when getting the current event size.
1532 static int ltt_seek_next_event(LttTracefile
*tf
)
1537 /* seek over the buffer header if we are at the buffer start */
1538 if(tf
->event
.offset
== 0) {
1539 tf
->event
.offset
+= tf
->buffer_header_size
;
1541 if(tf
->event
.offset
== tf
->buf_size
- tf
->buffer
.lost_size
) {
1547 pos
= tf
->event
.data
;
1549 if(tf
->event
.data_size
< 0) goto error
;
1551 pos
+= (size_t)tf
->event
.data_size
;
1553 tf
->event
.offset
= pos
- tf
->buffer
.head
;
1555 if(tf
->event
.offset
== tf
->buf_size
- tf
->buffer
.lost_size
) {
1559 g_assert(tf
->event
.offset
< tf
->buf_size
- tf
->buffer
.lost_size
);
1565 g_error("Error in ltt_seek_next_event for tracefile %s",
1566 g_quark_to_string(tf
->name
));
1571 /*****************************************************************************
1573 * set_fields_offsets : set the precomputable offset of the fields
1575 * tracefile : opened trace file
1576 * event_type : the event type
1577 ****************************************************************************/
1579 void set_fields_offsets(LttTracefile
*tf
, LttEventType
*event_type
)
1581 LttField
*field
= event_type
->root_field
;
1582 enum field_status fixed_root
= FIELD_FIXED
, fixed_parent
= FIELD_FIXED
;
1585 preset_field_type_size(tf
, event_type
, 0, 0,
1586 &fixed_root
, &fixed_parent
,
1593 /*****************************************************************************
1595 * get_alignment : Get the alignment needed for a field.
1599 * returns : The size on which it must be aligned.
1601 ****************************************************************************/
1603 off_t
get_alignment(LttField
*field
)
1605 LttType
*type
= &field
->field_type
;
1607 switch(type
->type_class
) {
1609 case LTT_UINT_FIXED
:
1624 /* Align offset on type size */
1625 g_assert(field
->field_size
!= 0);
1626 return field
->field_size
;
1632 g_assert(type
->fields
->len
== 1);
1634 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
1635 return get_alignment(child
);
1639 g_assert(type
->fields
->len
== 2);
1642 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
1644 localign
= max(localign
, get_alignment(child
));
1646 child
= &g_array_index(type
->fields
, LttField
, 1);
1647 localign
= max(localign
, get_alignment(child
));
1658 for(i
=0; i
<type
->fields
->len
; i
++) {
1659 LttField
*child
= &g_array_index(type
->fields
, LttField
, i
);
1660 localign
= max(localign
, get_alignment(child
));
1667 g_error("get_alignment : unknown type");
1674 /*****************************************************************************
1676 * field_compute_static_size : Determine the size of fields known by their
1677 * sole definition. Unions, arrays and struct sizes might be known, but
1678 * the parser does not give that information.
1683 ****************************************************************************/
1685 void field_compute_static_size(LttFacility
*fac
, LttField
*field
)
1687 LttType
*type
= &field
->field_type
;
1689 switch(type
->type_class
) {
1691 case LTT_UINT_FIXED
:
1710 /* note this : array type size is the number of elements in the array,
1711 * while array field size of the length of the array in bytes */
1712 g_assert(type
->fields
->len
== 1);
1714 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
1715 field_compute_static_size(fac
, child
);
1717 if(child
->field_size
!= 0) {
1718 field
->field_size
= type
->size
* child
->field_size
;
1719 field
->dynamic_offsets
= g_array_sized_new(FALSE
, TRUE
,
1720 sizeof(off_t
), type
->size
);
1722 field
->field_size
= 0;
1727 g_assert(type
->fields
->len
== 2);
1729 off_t local_offset
= 0;
1730 LttField
*child
= &g_array_index(type
->fields
, LttField
, 1);
1731 field_compute_static_size(fac
, child
);
1732 field
->field_size
= 0;
1734 if(child
->field_size
!= 0) {
1735 field
->dynamic_offsets
= g_array_sized_new(FALSE
, TRUE
,
1736 sizeof(off_t
), SEQUENCE_AVG_ELEMENTS
);
1744 for(i
=0;i
<type
->fields
->len
;i
++) {
1745 LttField
*child
= &g_array_index(type
->fields
, LttField
, i
);
1746 field_compute_static_size(fac
, child
);
1747 if(child
->field_size
!= 0) {
1748 type
->size
+= ltt_align(type
->size
, get_alignment(child
),
1750 type
->size
+= child
->field_size
;
1752 /* As soon as we find a child with variable size, we have
1753 * a variable size */
1758 field
->field_size
= type
->size
;
1762 g_error("field_static_size : unknown type");
1769 /*****************************************************************************
1771 * precompute_fields_offsets : set the precomputable offset of the fields
1775 * offset : pointer to the current offset, must be incremented
1777 * return : 1 : found a variable length field, stop the processing.
1779 ****************************************************************************/
1782 gint
precompute_fields_offsets(LttFacility
*fac
, LttField
*field
, off_t
*offset
, gint is_compact
)
1784 LttType
*type
= &field
->field_type
;
1786 if(unlikely(is_compact
)) {
1787 g_assert(field
->field_size
!= 0);
1788 /* FIXME THIS IS A HUUUUUGE hack :
1789 * offset is between the compact_data field in struct LttEvent
1790 * and the address of the field root in the memory map.
1791 * ark. Both will stay at the same addresses while the event
1792 * is readable, so it's ok.
1794 field
->offset_root
= 0;
1795 field
->fixed_root
= FIELD_FIXED
;
1799 switch(type
->type_class
) {
1801 case LTT_UINT_FIXED
:
1816 g_assert(field
->field_size
!= 0);
1817 /* Align offset on type size */
1818 *offset
+= ltt_align(*offset
, get_alignment(field
),
1820 /* remember offset */
1821 field
->offset_root
= *offset
;
1822 field
->fixed_root
= FIELD_FIXED
;
1823 /* Increment offset */
1824 *offset
+= field
->field_size
;
1828 field
->offset_root
= *offset
;
1829 field
->fixed_root
= FIELD_FIXED
;
1833 g_assert(type
->fields
->len
== 1);
1835 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
1837 *offset
+= ltt_align(*offset
, get_alignment(field
),
1840 /* remember offset */
1841 field
->offset_root
= *offset
;
1842 field
->array_offset
= *offset
;
1843 field
->fixed_root
= FIELD_FIXED
;
1845 /* Let the child be variable */
1846 //precompute_fields_offsets(tf, child, offset);
1848 if(field
->field_size
!= 0) {
1849 /* Increment offset */
1850 /* field_size is the array size in bytes */
1851 *offset
+= field
->field_size
;
1859 g_assert(type
->fields
->len
== 2);
1864 *offset
+= ltt_align(*offset
, get_alignment(field
),
1867 /* remember offset */
1868 field
->offset_root
= *offset
;
1869 field
->fixed_root
= FIELD_FIXED
;
1871 child
= &g_array_index(type
->fields
, LttField
, 0);
1872 ret
= precompute_fields_offsets(fac
, child
, offset
, is_compact
);
1873 g_assert(ret
== 0); /* Seq len cannot have variable len */
1875 child
= &g_array_index(type
->fields
, LttField
, 1);
1876 *offset
+= ltt_align(*offset
, get_alignment(child
),
1878 field
->array_offset
= *offset
;
1879 /* Let the child be variable. */
1880 //ret = precompute_fields_offsets(fac, child, offset);
1882 /* Cannot precompute fields offsets of sequence members, and has
1883 * variable length. */
1893 *offset
+= ltt_align(*offset
, get_alignment(field
),
1895 /* remember offset */
1896 field
->offset_root
= *offset
;
1897 field
->fixed_root
= FIELD_FIXED
;
1899 for(i
=0; i
< type
->fields
->len
; i
++) {
1900 child
= &g_array_index(type
->fields
, LttField
, i
);
1901 ret
= precompute_fields_offsets(fac
, child
, offset
, is_compact
);
1914 *offset
+= ltt_align(*offset
, get_alignment(field
),
1916 /* remember offset */
1917 field
->offset_root
= *offset
;
1918 field
->fixed_root
= FIELD_FIXED
;
1920 for(i
=0; i
< type
->fields
->len
; i
++) {
1921 *offset
= field
->offset_root
;
1922 child
= &g_array_index(type
->fields
, LttField
, i
);
1923 ret
= precompute_fields_offsets(fac
, child
, offset
, is_compact
);
1927 *offset
= field
->offset_root
+ field
->field_size
;
1934 g_error("precompute_fields_offsets : unknown type");
1943 /*****************************************************************************
1945 * precompute_offsets : set the precomputable offset of an event type
1948 * event : event type
1950 ****************************************************************************/
1951 void precompute_offsets(LttFacility
*fac
, LttEventType
*event
)
1957 /* First, compute the size of fixed size fields. Will determine size for
1958 * arrays, struct and unions, which is not done by the parser */
1959 for(i
=0; i
<event
->fields
->len
; i
++) {
1960 LttField
*field
= &g_array_index(event
->fields
, LttField
, i
);
1961 field_compute_static_size(fac
, field
);
1964 /* Precompute all known offsets */
1965 for(i
=0; i
<event
->fields
->len
; i
++) {
1966 LttField
*field
= &g_array_index(event
->fields
, LttField
, i
);
1967 if(event
->has_compact_data
&& i
== 0)
1968 ret
= precompute_fields_offsets(fac
, field
, &offset
, 1);
1970 ret
= precompute_fields_offsets(fac
, field
, &offset
, 0);
1978 /*****************************************************************************
1980 * preset_field_type_size : set the fixed sizes of the field type
1983 * event_type : event type
1984 * offset_root : offset from the root
1985 * offset_parent : offset from the parent
1986 * fixed_root : Do we know a fixed offset to the root ?
1987 * fixed_parent : Do we know a fixed offset to the parent ?
1989 ****************************************************************************/
1993 // preset the fixed size offsets. Calculate them just like genevent-new : an
1994 // increment of a *to value that represents the offset from the start of the
1996 // The preset information is : offsets up to (and including) the first element
1997 // of variable size. All subsequent fields must be flagged "VARIABLE OFFSET".
1999 void preset_field_type_size(LttTracefile
*tf
, LttEventType
*event_type
,
2000 off_t offset_root
, off_t offset_parent
,
2001 enum field_status
*fixed_root
, enum field_status
*fixed_parent
,
2004 enum field_status local_fixed_root
, local_fixed_parent
;
2008 g_assert(field
->fixed_root
== FIELD_UNKNOWN
);
2009 g_assert(field
->fixed_parent
== FIELD_UNKNOWN
);
2010 g_assert(field
->fixed_size
== FIELD_UNKNOWN
);
2012 type
= field
->field_type
;
2014 field
->fixed_root
= *fixed_root
;
2015 if(field
->fixed_root
== FIELD_FIXED
)
2016 field
->offset_root
= offset_root
;
2018 field
->offset_root
= 0;
2020 field
->fixed_parent
= *fixed_parent
;
2021 if(field
->fixed_parent
== FIELD_FIXED
)
2022 field
->offset_parent
= offset_parent
;
2024 field
->offset_parent
= 0;
2026 size_t current_root_offset
;
2027 size_t current_offset
;
2028 enum field_status current_child_status
, final_child_status
;
2031 switch(type
->type_class
) {
2033 case LTT_UINT_FIXED
:
2042 field
->field_size
= ltt_type_size(tf
->trace
, type
);
2043 field
->fixed_size
= FIELD_FIXED
;
2046 field
->field_size
= (off_t
)event_type
->facility
->pointer_size
;
2047 field
->fixed_size
= FIELD_FIXED
;
2051 field
->field_size
= (off_t
)event_type
->facility
->long_size
;
2052 field
->fixed_size
= FIELD_FIXED
;
2057 field
->field_size
= (off_t
)event_type
->facility
->size_t_size
;
2058 field
->fixed_size
= FIELD_FIXED
;
2061 local_fixed_root
= FIELD_VARIABLE
;
2062 local_fixed_parent
= FIELD_VARIABLE
;
2063 preset_field_type_size(tf
, event_type
,
2065 &local_fixed_root
, &local_fixed_parent
,
2067 field
->fixed_size
= FIELD_VARIABLE
;
2068 field
->field_size
= 0;
2069 *fixed_root
= FIELD_VARIABLE
;
2070 *fixed_parent
= FIELD_VARIABLE
;
2073 field
->fixed_size
= FIELD_VARIABLE
;
2074 field
->field_size
= 0;
2075 *fixed_root
= FIELD_VARIABLE
;
2076 *fixed_parent
= FIELD_VARIABLE
;
2079 local_fixed_root
= FIELD_VARIABLE
;
2080 local_fixed_parent
= FIELD_VARIABLE
;
2081 preset_field_type_size(tf
, event_type
,
2083 &local_fixed_root
, &local_fixed_parent
,
2085 field
->fixed_size
= field
->child
[0]->fixed_size
;
2086 if(field
->fixed_size
== FIELD_FIXED
) {
2087 field
->field_size
= type
->element_number
* field
->child
[0]->field_size
;
2089 field
->field_size
= 0;
2090 *fixed_root
= FIELD_VARIABLE
;
2091 *fixed_parent
= FIELD_VARIABLE
;
2095 current_root_offset
= field
->offset_root
;
2097 current_child_status
= FIELD_FIXED
;
2098 for(i
=0;i
<type
->element_number
;i
++) {
2099 preset_field_type_size(tf
, event_type
,
2100 current_root_offset
, current_offset
,
2101 fixed_root
, ¤t_child_status
,
2103 if(current_child_status
== FIELD_FIXED
) {
2104 current_root_offset
+= field
->child
[i
]->field_size
;
2105 current_offset
+= field
->child
[i
]->field_size
;
2107 current_root_offset
= 0;
2111 if(current_child_status
!= FIELD_FIXED
) {
2112 *fixed_parent
= current_child_status
;
2113 field
->field_size
= 0;
2114 field
->fixed_size
= current_child_status
;
2116 field
->field_size
= current_offset
;
2117 field
->fixed_size
= FIELD_FIXED
;
2121 current_root_offset
= field
->offset_root
;
2124 final_child_status
= FIELD_FIXED
;
2125 for(i
=0;i
<type
->element_number
;i
++) {
2126 enum field_status current_root_child_status
= FIELD_FIXED
;
2127 enum field_status current_child_status
= FIELD_FIXED
;
2128 preset_field_type_size(tf
, event_type
,
2129 current_root_offset
, current_offset
,
2130 ¤t_root_child_status
, ¤t_child_status
,
2132 if(current_child_status
!= FIELD_FIXED
)
2133 final_child_status
= current_child_status
;
2135 max_size
= max(max_size
, field
->child
[i
]->field_size
);
2137 if(final_child_status
!= FIELD_FIXED
) {
2138 g_error("LTTV does not support variable size fields in unions.");
2139 /* This will stop the application. */
2140 *fixed_root
= final_child_status
;
2141 *fixed_parent
= final_child_status
;
2142 field
->field_size
= 0;
2143 field
->fixed_size
= current_child_status
;
2145 field
->field_size
= max_size
;
2146 field
->fixed_size
= FIELD_FIXED
;
2150 g_error("unexpected type NONE");
2157 /*****************************************************************************
2159 * check_fields_compatibility : Check for compatibility between two fields :
2160 * do they use the same inner structure ?
2162 * event_type1 : event type
2163 * event_type2 : event type
2166 *Returns : 0 if identical
2168 ****************************************************************************/
2169 // this function checks for equality of field types. Therefore, it does not use
2170 // per se offsets. For instance, an aligned version of a structure is
2171 // compatible with an unaligned version of the same structure.
2173 gint
check_fields_compatibility(LttEventType
*event_type1
,
2174 LttEventType
*event_type2
,
2175 LttField
*field1
, LttField
*field2
)
2177 guint different
= 0;
2181 if(field1
== NULL
) {
2182 if(field2
== NULL
) goto end
;
2187 } else if(field2
== NULL
) {
2192 type1
= &field1
->field_type
;
2193 type2
= &field2
->field_type
;
2195 if(type1
->type_class
!= type2
->type_class
) {
2199 if(type1
->network
!= type2
->network
) {
2204 switch(type1
->type_class
) {
2206 case LTT_UINT_FIXED
:
2221 if(field1
->field_size
!= field2
->field_size
)
2228 LttField
*child1
= &g_array_index(type1
->fields
, LttField
, 0);
2229 LttField
*child2
= &g_array_index(type2
->fields
, LttField
, 0);
2231 if(type1
->size
!= type2
->size
)
2233 if(check_fields_compatibility(event_type1
, event_type2
, child1
, child2
))
2239 LttField
*child1
= &g_array_index(type1
->fields
, LttField
, 1);
2240 LttField
*child2
= &g_array_index(type2
->fields
, LttField
, 1);
2242 if(check_fields_compatibility(event_type1
, event_type2
, child1
, child2
))
2252 if(type1
->fields
->len
!= type2
->fields
->len
) {
2257 for(i
=0; i
< type1
->fields
->len
; i
++) {
2260 child1
= &g_array_index(type1
->fields
, LttField
, i
);
2261 child2
= &g_array_index(type2
->fields
, LttField
, i
);
2262 different
= check_fields_compatibility(event_type1
,
2263 event_type2
, child1
, child2
);
2265 if(different
) break;
2271 g_error("check_fields_compatibility : unknown type");
2280 gint
check_fields_compatibility(LttEventType
*event_type1
,
2281 LttEventType
*event_type2
,
2282 LttField
*field1
, LttField
*field2
)
2284 guint different
= 0;
2289 if(field1
== NULL
) {
2290 if(field2
== NULL
) goto end
;
2295 } else if(field2
== NULL
) {
2300 g_assert(field1
->fixed_root
!= FIELD_UNKNOWN
);
2301 g_assert(field2
->fixed_root
!= FIELD_UNKNOWN
);
2302 g_assert(field1
->fixed_parent
!= FIELD_UNKNOWN
);
2303 g_assert(field2
->fixed_parent
!= FIELD_UNKNOWN
);
2304 g_assert(field1
->fixed_size
!= FIELD_UNKNOWN
);
2305 g_assert(field2
->fixed_size
!= FIELD_UNKNOWN
);
2307 type1
= field1
->field_type
;
2308 type2
= field2
->field_type
;
2310 if(type1
->type_class
!= type2
->type_class
) {
2314 if(type1
->element_name
!= type2
->element_name
) {
2319 switch(type1
->type_class
) {
2321 case LTT_UINT_FIXED
:
2336 if(field1
->field_size
!= field2
->field_size
) {
2342 if(type1
->element_number
!= type2
->element_number
) {
2346 for(i
=0;i
<type1
->element_number
;i
++) {
2347 if(type1
->enum_strings
[i
] != type2
->enum_strings
[i
]) {
2354 /* Two elements : size and child */
2355 g_assert(type1
->element_number
!= type2
->element_number
);
2356 for(i
=0;i
<type1
->element_number
;i
++) {
2357 if(check_fields_compatibility(event_type1
, event_type2
,
2358 field1
->child
[0], field2
->child
[0])) {
2367 if(field1
->field_size
!= field2
->field_size
) {
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])) {
2383 if(type1
->element_number
!= type2
->element_number
) {
2387 for(i
=0;i
<type1
->element_number
;i
++) {
2388 if(check_fields_compatibility(event_type1
, event_type2
,
2389 field1
->child
[0], field2
->child
[0])) {
2402 /*****************************************************************************
2404 * ltt_get_int : get an integer number
2406 * reverse_byte_order: must we reverse the byte order ?
2407 * size : the size of the integer
2408 * ptr : the data pointer
2410 * gint64 : a 64 bits integer
2411 ****************************************************************************/
2413 gint64
ltt_get_int(gboolean reverse_byte_order
, gint size
, void *data
)
2418 case 1: val
= *((gint8
*)data
); break;
2419 case 2: val
= ltt_get_int16(reverse_byte_order
, data
); break;
2420 case 4: val
= ltt_get_int32(reverse_byte_order
, data
); break;
2421 case 8: val
= ltt_get_int64(reverse_byte_order
, data
); break;
2422 default: val
= ltt_get_int64(reverse_byte_order
, data
);
2423 g_critical("get_int : integer size %d unknown", size
);
2430 /*****************************************************************************
2432 * ltt_get_uint : get an unsigned integer number
2434 * reverse_byte_order: must we reverse the byte order ?
2435 * size : the size of the integer
2436 * ptr : the data pointer
2438 * guint64 : a 64 bits unsigned integer
2439 ****************************************************************************/
2441 guint64
ltt_get_uint(gboolean reverse_byte_order
, gint size
, void *data
)
2446 case 1: val
= *((gint8
*)data
); break;
2447 case 2: val
= ltt_get_uint16(reverse_byte_order
, data
); break;
2448 case 4: val
= ltt_get_uint32(reverse_byte_order
, data
); break;
2449 case 8: val
= ltt_get_uint64(reverse_byte_order
, data
); break;
2450 default: val
= ltt_get_uint64(reverse_byte_order
, data
);
2451 g_critical("get_uint : unsigned integer size %d unknown",
2460 /* get the node name of the system */
2462 char * ltt_trace_system_description_node_name (LttSystemDescription
* s
)
2464 return s
->node_name
;
2468 /* get the domain name of the system */
2470 char * ltt_trace_system_description_domain_name (LttSystemDescription
* s
)
2472 return s
->domain_name
;
2476 /* get the description of the system */
2478 char * ltt_trace_system_description_description (LttSystemDescription
* s
)
2480 return s
->description
;
2484 /* get the NTP corrected start time of the trace */
2485 LttTime
ltt_trace_start_time(LttTrace
*t
)
2487 return t
->start_time
;
2490 /* get the monotonic start time of the trace */
2491 LttTime
ltt_trace_start_time_monotonic(LttTrace
*t
)
2493 return t
->start_time_from_tsc
;
2496 static LttTracefile
*ltt_tracefile_new()
2499 tf
= g_new(LttTracefile
, 1);
2500 tf
->event
.tracefile
= tf
;
2504 static void ltt_tracefile_destroy(LttTracefile
*tf
)
2509 static void ltt_tracefile_copy(LttTracefile
*dest
, const LttTracefile
*src
)
2514 /* Before library loading... */
2516 static __attribute__((constructor
)) void init(void)
2518 LTT_TRACEFILE_NAME_METADATA
= g_quark_from_string("/control/metadata");