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>
35 #include <glib/gprintf.h>
48 #include "ltt-private.h"
49 #include <ltt/trace.h>
50 #include <ltt/event.h>
51 #include <ltt/ltt-types.h>
52 #include <ltt/marker.h>
54 #define DEFAULT_N_BLOCKS 32
57 extern long marker_update_fields_offsets(struct marker_info
*info
, const char *data
);
58 extern void marker_update_event_fields_offsets(GArray
*fields_offsets
,
59 struct marker_info
*info
);
61 /* Tracefile names used in this file */
63 GQuark LTT_TRACEFILE_NAME_METADATA
;
70 #define __UNUSED__ __attribute__((__unused__))
72 #define g_info(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, format)
75 #define g_debug(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format)
80 /* Those macros must be called from within a function where page_size is a known
82 #define PAGE_MASK (~(page_size-1))
83 #define PAGE_ALIGN(addr) (((addr)+page_size-1)&PAGE_MASK)
85 /* set the offset of the fields belonging to the event,
86 need the information of the archecture */
87 //void set_fields_offsets(LttTracefile *tf, LttEventType *event_type);
88 //size_t get_fields_offsets(LttTracefile *tf, LttEventType *event_type, void *data);
90 /* map a fixed size or a block information from the file (fd) */
91 static gint
map_block(LttTracefile
* tf
, guint block_num
);
93 /* calculate nsec per cycles for current block */
95 static guint32
calc_nsecs_per_cycle(LttTracefile
* t
);
96 static guint64
cycles_2_ns(LttTracefile
*tf
, guint64 cycles
);
99 /* go to the next event */
100 static int ltt_seek_next_event(LttTracefile
*tf
);
102 static int open_tracefiles(LttTrace
*trace
, gchar
*root_path
,
103 gchar
*relative_path
);
104 static int ltt_process_metadata_tracefile(LttTracefile
*tf
);
105 static void ltt_tracefile_time_span_get(LttTracefile
*tf
,
106 LttTime
*start
, LttTime
*end
);
107 static void group_time_span_get(GQuark name
, gpointer data
, gpointer user_data
);
108 static gint
map_block(LttTracefile
* tf
, guint block_num
);
109 static void ltt_update_event_size(LttTracefile
*tf
);
110 static LttTrace
*_ltt_trace_open(const gchar
*pathname
, gboolean is_live
);
111 static int ltt_tracefile_update(LttTracefile
*tf
);
112 static void update_tracefile_group(GQuark name
, gpointer data
, gpointer user_data
);
114 /* Enable event debugging */
115 static int a_event_debug
= 0;
117 void ltt_event_debug(int state
)
119 a_event_debug
= state
;
124 * Return value : 0 success, 1 bad tracefile
126 static int parse_trace_header(ltt_subbuffer_header_t
*header
,
127 LttTracefile
*tf
, LttTrace
*t
)
129 if (header
->magic_number
== LTT_MAGIC_NUMBER
)
131 else if(header
->magic_number
== LTT_REV_MAGIC_NUMBER
)
133 else /* invalid magic number, bad tracefile ! */
137 t
->ltt_major_version
= header
->major_version
;
138 t
->ltt_minor_version
= header
->minor_version
;
139 t
->arch_size
= header
->arch_size
;
141 tf
->alignment
= header
->alignment
;
143 /* Get float byte order : might be different from int byte order
144 * (or is set to 0 if the trace has no float (kernel trace)) */
145 tf
->float_word_order
= 0;
147 switch(header
->major_version
) {
150 g_warning("Unsupported trace version : %hhu.%hhu",
151 header
->major_version
, header
->minor_version
);
155 switch(header
->minor_version
) {
158 struct ltt_subbuffer_header_2_6
*vheader
= header
;
159 tf
->buffer_header_size
= ltt_subbuffer_header_size();
162 tf
->tsc_mask
= ((1ULL << tf
->tscbits
) - 1);
163 tf
->tsc_mask_next_bit
= (1ULL << tf
->tscbits
);
166 t
->start_freq
= ltt_get_uint64(LTT_GET_BO(tf
),
167 &vheader
->start_freq
);
168 t
->freq_scale
= ltt_get_uint32(LTT_GET_BO(tf
),
169 &vheader
->freq_scale
);
170 t
->start_tsc
= ltt_get_uint64(LTT_GET_BO(tf
),
171 &vheader
->cycle_count_begin
);
172 t
->start_monotonic
= 0;
173 t
->start_time
.tv_sec
= ltt_get_uint64(LTT_GET_BO(tf
),
174 &vheader
->start_time_sec
);
175 t
->start_time
.tv_nsec
= ltt_get_uint64(LTT_GET_BO(tf
),
176 &vheader
->start_time_usec
);
177 t
->start_time
.tv_nsec
*= 1000; /* microsec to nanosec */
179 t
->start_time_from_tsc
=
180 ltt_time_from_uint64(tsc_to_uint64(t
->freq_scale
,
181 t
->start_freq
, t
->start_tsc
));
186 g_warning("Unsupported trace version : %hhu.%hhu",
187 header
->major_version
, header
->minor_version
);
192 g_warning("Unsupported trace version : %hhu.%hhu",
193 header
->major_version
, header
->minor_version
);
199 int get_block_offset_size(LttTracefile
*tf
, guint block_num
,
200 uint64_t *offset
, uint32_t *size
)
204 if (unlikely(block_num
>= tf
->num_blocks
))
207 offa
= g_array_index(tf
->buf_index
, uint64_t, block_num
);
208 if (likely(block_num
< tf
->num_blocks
- 1))
209 offb
= g_array_index(tf
->buf_index
, uint64_t, block_num
+ 1);
211 offb
= tf
->file_size
;
217 static int ltt_trace_update_block_index(LttTracefile
*tf
, uint64_t offset
,
218 unsigned long firstBlock
)
221 int page_size
= getpagesize();
222 unsigned int header_map_size
= PAGE_ALIGN(ltt_subbuffer_header_size());
224 g_assert(tf
->buf_index
->len
== i
);
225 while (offset
< tf
->file_size
) {
226 ltt_subbuffer_header_t
*header
;
230 /* map block header */
231 header
= mmap(0, header_map_size
, PROT_READ
,
232 MAP_PRIVATE
, tf
->fd
, (off_t
)offset
);
233 if(header
== MAP_FAILED
) {
234 perror("Error in allocating memory for buffer of tracefile");
238 /* read len, offset += len */
239 size
= ltt_get_uint32(LTT_GET_BO(tf
), &header
->sb_size
);
241 /* Only index completly writen blocks */
242 if (offset
+ size
<= tf
->file_size
) {
244 tf
->buf_index
= g_array_set_size(tf
->buf_index
, i
+ 1);
245 off
= &g_array_index(tf
->buf_index
, uint64_t, i
);
248 /* Store current buffer end cycle as the last file timestamp */
249 /* TODO ybrosseau 2010-11-04: Might want to convert it to a LttTime */
250 tf
->end_timestamp
= ltt_get_uint64(LTT_GET_BO(tf
),
251 &header
->cycle_count_end
);
256 /* unmap block header */
257 if(munmap(header
, header_map_size
)) {
258 g_warning("unmap size : %u\n", header_map_size
);
259 perror("munmap error");
268 /* parse the new information from the file and reajust the number of blocks.
270 * Return value : 0 success, -1 error
272 int ltt_trace_continue_block_index(LttTracefile
*tf
)
276 uint32_t last_block_size
;
277 unsigned long i
= tf
->num_blocks
;
278 int page_size
= getpagesize();
279 unsigned int header_map_size
= PAGE_ALIGN(ltt_subbuffer_header_size());
281 get_block_offset_size(tf
, tf
->num_blocks
-1, &offset
, &last_block_size
);
283 ltt_subbuffer_header_t
*header_tmp
= mmap(0, header_map_size
, PROT_READ
,
284 MAP_PRIVATE
, tf
->fd
, (off_t
)offset
);
285 if(header_tmp
== MAP_FAILED
) {
286 perror("Error in allocating memory for buffer of tracefile");
290 /* read len, offset += len */
291 offset
+= ltt_get_uint32(LTT_GET_BO(tf
), &header_tmp
->sb_size
);
293 ret
= ltt_trace_update_block_index(tf
, offset
, i
);
298 int ltt_trace_create_block_index(LttTracefile
*tf
)
304 tf
->buf_index
= g_array_sized_new(FALSE
, TRUE
, sizeof(uint64_t),
308 ret
= ltt_trace_update_block_index(tf
, offset
, i
);
313 Read file header and initialise buffer indexes
315 Return value : 0 for success, -1 otherwise.
317 static int ltt_tracefile_init(LttTracefile
*tf
)
319 ltt_subbuffer_header_t
*header
;
320 int page_size
= getpagesize();
322 /* Temporarily map the buffer start header to get trace information */
323 /* Multiple of pages aligned head */
324 tf
->buffer
.head
= mmap(0,
325 PAGE_ALIGN(ltt_subbuffer_header_size()), PROT_READ
,
326 MAP_PRIVATE
, tf
->fd
, 0);
327 if(tf
->buffer
.head
== MAP_FAILED
) {
328 perror("Error in allocating memory for buffer of tracefile");
331 g_assert( ( (gulong
)tf
->buffer
.head
&(8-1) ) == 0); // make sure it's aligned.
333 header
= (ltt_subbuffer_header_t
*)tf
->buffer
.head
;
335 if(parse_trace_header(header
, tf
, tf
->trace
))
337 g_warning("parse_trace_header error");
341 if(munmap(tf
->buffer
.head
,
342 PAGE_ALIGN(ltt_subbuffer_header_size())))
344 g_warning("unmap size : %zu\n",
345 PAGE_ALIGN(ltt_subbuffer_header_size()));
346 perror("munmap error");
349 tf
->buffer
.head
= NULL
;
351 /* Create fields offset table */
352 tf
->event
.fields_offsets
= g_array_sized_new(FALSE
, FALSE
,
353 sizeof(struct LttField
), 1);
354 if (!tf
->event
.fields_offsets
) {
355 g_warning("Cannot create fields offset table");
359 /* Create block index */
360 ltt_trace_create_block_index(tf
);
364 perror("Cannot map block for tracefile");
370 if(munmap(tf
->buffer
.head
,
371 PAGE_ALIGN(ltt_subbuffer_header_size()))) {
372 g_warning("unmap size : %zu\n",
373 PAGE_ALIGN(ltt_subbuffer_header_size()));
374 perror("munmap error");
380 /*****************************************************************************
382 * ltt_tracefile_open : open a trace file, construct a LttTracefile
384 * t : the trace containing the tracefile
385 * fileName : path name of the trace file
386 * tf : the tracefile structure
388 * : 0 for success, -1 otherwise.
389 ****************************************************************************/
390 static gint
ltt_tracefile_open(LttTrace
*t
, gchar
* fileName
, LttTracefile
*tf
)
392 struct stat lTDFStat
; /* Trace data file status */
395 tf
->long_name
= g_quark_from_string(fileName
);
397 tf
->fd
= open(fileName
, O_RDONLY
);
398 tf
->buf_index
= NULL
;
401 g_warning("Unable to open input data file %s\n", fileName
);
405 // Get the file's status
406 if(fstat(tf
->fd
, &lTDFStat
) < 0){
407 g_warning("Unable to get the status of the input data file %s\n", fileName
);
410 //store the size of the file
411 tf
->file_size
= lTDFStat
.st_size
;
413 tf
->subbuf_corrupt
= 0;
414 tf
->buffer
.head
= NULL
;
415 tf
->event
.fields_offsets
= NULL
;
417 /* Is the file large enough to contain a trace */
418 if(lTDFStat
.st_size
< (off_t
)(ltt_subbuffer_header_size())){
420 /* It a live trace so the file can be empty at the start of the analysis */
423 g_print("The input data file %s does not contain a trace\n", fileName
);
428 if(ltt_tracefile_init(tf
) < 0) {
439 g_array_free(tf
->buf_index
, TRUE
);
443 /*****************************************************************************
445 * ltt_tracefile_close: close a trace file,
447 * t : tracefile which will be closed
448 ****************************************************************************/
450 static void ltt_tracefile_close(LttTracefile
*t
)
452 int page_size
= getpagesize();
454 if(t
->buffer
.head
!= NULL
)
455 if(munmap(t
->buffer
.head
, PAGE_ALIGN(t
->buffer
.size
))) {
456 g_warning("unmap size : %u\n",
457 PAGE_ALIGN(t
->buffer
.size
));
458 perror("munmap error");
464 g_array_free(t
->buf_index
, TRUE
);
465 if (t
->event
.fields_offsets
) {
466 g_array_free(t
->event
.fields_offsets
, TRUE
);
470 /****************************************************************************
471 * get_absolute_pathname
473 * return the unique pathname in the system
475 * MD : Fixed this function so it uses realpath, dealing well with
476 * forgotten cases (.. were not used correctly before).
478 ****************************************************************************/
479 void get_absolute_pathname(const gchar
*pathname
, gchar
* abs_pathname
)
481 abs_pathname
[0] = '\0';
483 if (realpath(pathname
, abs_pathname
) != NULL
)
487 /* error, return the original path unmodified */
488 strcpy(abs_pathname
, pathname
);
494 /* Search for something like : .*_.*
496 * The left side is the name, the right side is the number.
498 * Exclude flight- prefix.
501 static int get_tracefile_name_number(gchar
*raw_name
,
508 guint raw_name_len
= strlen(raw_name
);
509 gchar char_name
[PATH_MAX
];
517 for(i
= 0; i
< raw_name_len
-1;i
++) {
518 if(raw_name
[i
] != '/')
521 raw_name
= &raw_name
[i
];
522 raw_name_len
= strlen(raw_name
);
524 for(i
=raw_name_len
-1;i
>=0;i
--) {
525 if(raw_name
[i
] == '_') break;
527 if(i
==-1) { /* Either not found or name length is 0 */
528 /* This is a userspace tracefile */
529 strncpy(char_name
, raw_name
, raw_name_len
);
530 char_name
[raw_name_len
] = '\0';
531 *name
= g_quark_from_string(char_name
);
532 *num
= 0; /* unknown cpu */
533 for(i
=0;i
<raw_name_len
;i
++) {
534 if(raw_name
[i
] == '/') {
539 for(;i
<raw_name_len
;i
++) {
540 if(raw_name
[i
] == '/') {
545 for(;i
<raw_name_len
;i
++) {
546 if(raw_name
[i
] == '-') {
550 if(i
== raw_name_len
) return -1;
552 tmpptr
= &raw_name
[i
];
553 for(;i
<raw_name_len
;i
++) {
554 if(raw_name
[i
] == '.') {
559 *tid
= strtoul(tmpptr
, &endptr
, 10);
561 return -1; /* No digit */
562 if(*tid
== ULONG_MAX
)
563 return -1; /* underflow / overflow */
565 tmpptr
= &raw_name
[i
];
566 for(;i
<raw_name_len
;i
++) {
567 if(raw_name
[i
] == '.') {
572 *pgid
= strtoul(tmpptr
, &endptr
, 10);
574 return -1; /* No digit */
575 if(*pgid
== ULONG_MAX
)
576 return -1; /* underflow / overflow */
578 tmpptr
= &raw_name
[i
];
579 *creation
= strtoull(tmpptr
, &endptr
, 10);
581 return -1; /* No digit */
582 if(*creation
== G_MAXUINT64
)
583 return -1; /* underflow / overflow */
587 cpu_num
= strtol(raw_name
+underscore_pos
+1, &endptr
, 10);
589 if(endptr
== raw_name
+underscore_pos
+1)
590 return -1; /* No digit */
591 if(cpu_num
== LONG_MIN
|| cpu_num
== LONG_MAX
)
592 return -1; /* underflow / overflow */
594 if (!strncmp(raw_name
, "flight-", sizeof("flight-") - 1)) {
595 raw_name
+= sizeof("flight-") - 1;
596 underscore_pos
-= sizeof("flight-") - 1;
598 strncpy(char_name
, raw_name
, underscore_pos
);
599 char_name
[underscore_pos
] = '\0';
600 *name
= g_quark_from_string(char_name
);
609 GData
**ltt_trace_get_tracefiles_groups(LttTrace
*trace
)
611 return &trace
->tracefiles
;
615 void compute_tracefile_group(GQuark key_id
,
617 struct compute_tracefile_group_args
*args
)
622 for(i
=0; i
<group
->len
; i
++) {
623 tf
= &g_array_index (group
, LttTracefile
, i
);
625 args
->func(tf
, args
->func_args
);
630 static void ltt_tracefile_group_destroy(gpointer data
)
632 GArray
*group
= (GArray
*)data
;
637 destroy_marker_data(g_array_index (group
, LttTracefile
, 0).mdata
);
638 for(i
=0; i
<group
->len
; i
++) {
639 tf
= &g_array_index (group
, LttTracefile
, i
);
641 ltt_tracefile_close(tf
);
643 g_array_free(group
, TRUE
);
646 static __attribute__ ((__unused__
)) gboolean
ltt_tracefile_group_has_cpu_online(gpointer data
)
648 GArray
*group
= (GArray
*)data
;
652 for(i
=0; i
<group
->len
; i
++) {
653 tf
= &g_array_index (group
, LttTracefile
, i
);
661 /* Open each tracefile under a specific directory. Put them in a
662 * GData : permits to access them using their tracefile group pathname.
663 * i.e. access control/modules tracefile group by index :
666 * relative path is the path relative to the trace root
667 * root path is the full path
669 * A tracefile group is simply an array where all the per cpu tracefiles sit.
672 static int open_tracefiles(LttTrace
*trace
, gchar
*root_path
, gchar
*relative_path
)
674 DIR *dir
= opendir(root_path
);
675 struct dirent
*entry
;
676 struct stat stat_buf
;
678 struct marker_data
*mdata
;
680 gchar path
[PATH_MAX
];
685 gchar rel_path
[PATH_MAX
];
694 strncpy(path
, root_path
, PATH_MAX
-1);
695 path_len
= strlen(path
);
696 path
[path_len
] = '/';
698 path_ptr
= path
+ path_len
;
700 strncpy(rel_path
, relative_path
, PATH_MAX
-1);
701 rel_path_len
= strlen(rel_path
);
702 rel_path
[rel_path_len
] = '/';
704 rel_path_ptr
= rel_path
+ rel_path_len
;
706 while((entry
= readdir(dir
)) != NULL
) {
707 if(entry
->d_name
[0] == '.') continue;
709 strncpy(path_ptr
, entry
->d_name
, PATH_MAX
- path_len
);
710 strncpy(rel_path_ptr
, entry
->d_name
, PATH_MAX
- rel_path_len
);
712 ret
= stat(path
, &stat_buf
);
718 g_debug("Tracefile file or directory : %s\n", path
);
720 // if(strcmp(rel_path, "/eventdefs") == 0) continue;
722 if(S_ISDIR(stat_buf
.st_mode
)) {
724 g_debug("Entering subdirectory...\n");
725 ret
= open_tracefiles(trace
, path
, rel_path
);
726 if(ret
< 0) continue;
727 } else if(S_ISREG(stat_buf
.st_mode
)) {
736 if(get_tracefile_name_number(rel_path
, &name
, &num
, &tid
, &pgid
, &creation
))
737 continue; /* invalid name */
739 g_debug("Opening file.\n");
740 if(ltt_tracefile_open(trace
, path
, &tmp_tf
)) {
741 /* Only consider the error for non live trace */
742 if (!trace
->is_live
) {
744 g_info("Error opening tracefile %s", path
);
745 continue; /* error opening the tracefile : bad magic number ? */
749 g_debug("Tracefile name is %s and number is %u",
750 g_quark_to_string(name
), num
);
753 tmp_tf
.cpu_online
= 1;
754 tmp_tf
.cpu_num
= num
;
758 tmp_tf
.creation
= creation
;
759 group
= g_datalist_id_get_data(&trace
->tracefiles
, name
);
761 /* Elements are automatically cleared when the array is allocated.
762 * It makes the cpu_online variable set to 0 : cpu offline, by default.
764 group
= g_array_sized_new (FALSE
, TRUE
, sizeof(LttTracefile
), 10);
765 g_datalist_id_set_data_full(&trace
->tracefiles
, name
,
766 group
, ltt_tracefile_group_destroy
);
767 mdata
= allocate_marker_data();
769 g_error("Error in allocating marker data");
772 /* Add the per cpu tracefile to the named group */
773 unsigned int old_len
= group
->len
;
775 group
= g_array_set_size(group
, num
+1);
777 g_assert(group
->len
> 0);
779 mdata
= g_array_index (group
, LttTracefile
, 0).mdata
;
781 g_array_index (group
, LttTracefile
, num
) = tmp_tf
;
782 g_array_index (group
, LttTracefile
, num
).event
.tracefile
=
783 &g_array_index (group
, LttTracefile
, num
);
784 for (i
= 0; i
< group
->len
; i
++)
785 g_array_index (group
, LttTracefile
, i
).mdata
= mdata
;
795 /* Presumes the tracefile is already seeked at the beginning. It makes sense,
796 * because it must be done just after the opening */
797 static int ltt_process_metadata_tracefile(LttTracefile
*tf
)
802 err
= ltt_tracefile_read_seek(tf
);
803 if(err
== EPERM
) goto seek_error
;
804 else if(err
== ERANGE
) break; /* End of tracefile */
806 err
= ltt_tracefile_read_update_event(tf
);
807 if(err
) goto update_error
;
810 * It contains only core events :
812 * 1 : set_marker_format
814 if(tf
->event
.event_id
>= MARKER_CORE_IDS
) {
815 /* Should only contain core events */
816 g_warning("Error in processing metadata file %s, "
817 "should not contain event id %u.", g_quark_to_string(tf
->name
),
823 const char *channel_name
, *marker_name
, *format
;
825 guint8 int_size
, long_size
, pointer_size
, size_t_size
, alignment
;
827 switch((enum marker_id
)tf
->event
.event_id
) {
828 case MARKER_ID_SET_MARKER_ID
:
829 channel_name
= pos
= tf
->event
.data
;
830 pos
+= strlen(channel_name
) + 1;
832 g_debug("Doing MARKER_ID_SET_MARKER_ID of marker %s.%s",
833 channel_name
, marker_name
);
834 pos
+= strlen(marker_name
) + 1;
835 pos
+= ltt_align((size_t)pos
, sizeof(guint16
), tf
->alignment
);
836 id
= ltt_get_uint16(LTT_GET_BO(tf
), pos
);
837 g_debug("In MARKER_ID_SET_MARKER_ID of marker %s.%s id %hu",
838 channel_name
, marker_name
, id
);
839 pos
+= sizeof(guint16
);
840 int_size
= *(guint8
*)pos
;
841 pos
+= sizeof(guint8
);
842 long_size
= *(guint8
*)pos
;
843 pos
+= sizeof(guint8
);
844 pointer_size
= *(guint8
*)pos
;
845 pos
+= sizeof(guint8
);
846 size_t_size
= *(guint8
*)pos
;
847 pos
+= sizeof(guint8
);
848 alignment
= *(guint8
*)pos
;
849 pos
+= sizeof(guint8
);
850 marker_id_event(tf
->trace
,
851 g_quark_from_string(channel_name
),
852 g_quark_from_string(marker_name
),
853 id
, int_size
, long_size
,
854 pointer_size
, size_t_size
, alignment
);
856 case MARKER_ID_SET_MARKER_FORMAT
:
857 channel_name
= pos
= tf
->event
.data
;
858 pos
+= strlen(channel_name
) + 1;
860 g_debug("Doing MARKER_ID_SET_MARKER_FORMAT of marker %s.%s",
861 channel_name
, marker_name
);
862 pos
+= strlen(marker_name
) + 1;
864 pos
+= strlen(format
) + 1;
865 marker_format_event(tf
->trace
,
866 g_quark_from_string(channel_name
),
867 g_quark_from_string(marker_name
),
869 /* get information from dictionary TODO */
872 g_warning("Error in processing metadata file %s, "
873 "unknown event id %hhu.",
874 g_quark_to_string(tf
->name
),
887 g_warning("An error occured in metadata tracefile parsing");
892 LttTrace
*ltt_trace_open(const gchar
*pathname
)
894 return _ltt_trace_open(pathname
, FALSE
);
897 LttTrace
*ltt_trace_open_live(const gchar
*pathname
)
899 return _ltt_trace_open(pathname
, TRUE
);
903 * Open a trace and return its LttTrace handle.
905 * pathname must be the directory of the trace
908 static LttTrace
*_ltt_trace_open(const gchar
*pathname
, gboolean is_live
)
910 gchar abs_path
[PATH_MAX
];
916 ltt_subbuffer_header_t
*header
;
918 struct dirent
*entry
;
919 struct stat stat_buf
;
920 gchar path
[PATH_MAX
];
922 t
= g_new(LttTrace
, 1);
923 if(!t
) goto alloc_error
;
925 get_absolute_pathname(pathname
, abs_path
);
926 t
->pathname
= g_quark_from_string(abs_path
);
928 g_datalist_init(&t
->tracefiles
);
930 /* Test to see if it looks like a trace */
931 dir
= opendir(abs_path
);
936 while((entry
= readdir(dir
)) != NULL
) {
937 strcpy(path
, abs_path
);
939 strcat(path
, entry
->d_name
);
940 ret
= stat(path
, &stat_buf
);
948 t
->is_live
= is_live
;
949 t
->live_safe_timestamp
= ltt_time_zero
;
951 /* Open all the tracefiles */
953 if(open_tracefiles(t
, abs_path
, "")) {
954 g_warning("Error opening tracefile %s", abs_path
);
958 /* Parse each trace metadata_N files : get runtime fac. info */
959 group
= g_datalist_id_get_data(&t
->tracefiles
, LTT_TRACEFILE_NAME_METADATA
);
961 g_warning("Trace %s has no metadata tracefile", abs_path
);
966 * Get the trace information for the first valid metadata tracefile.
967 * In live trace mode, the metadata_0 might be empty
968 * Getting a correct trace start_time and start_tsc is insured by the fact
969 * that no subbuffers are supposed to be lost in the metadata channel.
970 * Therefore, the first subbuffer contains the start_tsc timestamp in its
973 g_assert(group
->len
> 0);
975 for(i
=0; i
<group
->len
; i
++) {
976 tf
= &g_array_index (group
, LttTracefile
, i
);
977 header
= (ltt_subbuffer_header_t
*)tf
->buffer
.head
;
982 if (header
== NULL
) {
983 g_warning("Trace %s has not one valid metadata tracefile", abs_path
);
987 ret
= parse_trace_header(header
, tf
, t
);
990 t
->num_cpu
= group
->len
;
994 //ret = allocate_marker_data(t);
996 // g_error("Error in allocating marker data");
998 for(i
=0; i
<group
->len
; i
++) {
999 tf
= &g_array_index (group
, LttTracefile
, i
);
1000 if (tf
->cpu_online
&& tf
->buffer
.head
)
1001 if(ltt_process_metadata_tracefile(tf
))
1003 // goto metadata_error;
1008 /* Error handling */
1010 // destroy_marker_data(t);
1012 g_datalist_clear(&t
->tracefiles
);
1021 /*****************************************************************************
1022 Update the informations concerning the tracefile
1024 Must be called periodically to update trace file and file size
1030 : Number of tracefile with available events
1032 ****************************************************************************/
1033 int ltt_trace_update(LttTrace
*trace
)
1035 int trace_updated_count
= 0;
1037 /* Only update live traces */
1038 if(trace
->is_live
) {
1040 /* Iterate on all tracefiles */
1041 g_datalist_foreach(&trace
->tracefiles
,
1042 &update_tracefile_group
,
1043 &trace_updated_count
);
1045 return trace_updated_count
;
1051 static void update_tracefile_group(GQuark name
, gpointer data
, gpointer user_data
)
1053 int *trace_updated_count
= (int *)user_data
;
1056 GArray
*group
= (GArray
*)data
;
1058 g_debug("Updating tracefile group %s", g_quark_to_string(name
));
1059 for(i
=0; i
<group
->len
; i
++) {
1060 tf
= &g_array_index (group
, LttTracefile
, i
);
1062 /* Update safe timestamp */
1063 tf
->trace
->live_safe_timestamp
=
1064 LTT_TIME_MAX(tf
->trace
->live_safe_timestamp
,
1065 ltt_interpolate_time_from_tsc(tf
,
1066 tf
->end_timestamp
));
1068 /* Update tracefile */
1069 int ret
= ltt_tracefile_update(tf
);
1071 g_warning("LIVE: Cannot update tracefile %s",
1072 g_quark_to_string(ltt_tracefile_long_name(tf
)));
1073 *trace_updated_count
= -1;
1076 *trace_updated_count
+= 1;
1082 /*****************************************************************************
1084 * ltt_tracefile_update : Update the informations concerning a tracefile
1085 * Must be called periodically to update trace file and file size
1088 * tf : the tracefile
1090 * : 1 for success and an event is available
1091 0 for success but no event available,
1093 ****************************************************************************/
1094 static int ltt_tracefile_update(LttTracefile
*tf
)
1096 struct stat lTDFStat
; /* Trace data file status */
1097 if(fstat(tf
->fd
, &lTDFStat
) < 0){
1098 perror("error in getting the tracefile informations.");
1102 /* Process the file only on size change */
1103 if(tf
->file_size
< lTDFStat
.st_size
) {
1104 /* Update the size of the file */
1105 tf
->file_size
= lTDFStat
.st_size
;
1106 g_debug("Updating tracefile %s", g_quark_to_string(tf
->long_name
));
1108 if( tf
->file_size
>= (off_t
)(ltt_subbuffer_header_size()) ) {
1110 if(tf
->buf_index
== NULL
) {
1111 if(ltt_tracefile_init(tf
) < 0) {
1114 if(tf
->name
== LTT_TRACEFILE_NAME_METADATA
) {
1119 ltt_process_metadata_tracefile(tf
);
1121 ltt_tracefile_seek_time(tf
, start
);
1122 tf
->event
.offset
= 0;
1128 /* Retrieve the new subbuffers and index them */
1129 ltt_trace_continue_block_index(tf
);
1131 if(tf
->name
== LTT_TRACEFILE_NAME_METADATA
) {
1132 LttEventPosition
*pos
= ltt_event_position_new();
1133 ltt_tracefile_get_current_position(tf
, pos
);
1134 ltt_process_metadata_tracefile(tf
);
1135 ltt_tracefile_seek_position(tf
, pos
);
1149 /* Open another, completely independant, instance of a trace.
1151 * A read on this new instance will read the first event of the trace.
1153 * When we copy a trace, we want all the opening actions to happen again :
1154 * the trace will be reopened and totally independant from the original.
1155 * That's why we call ltt_trace_open.
1157 LttTrace
*ltt_trace_copy(LttTrace
*self
)
1159 return _ltt_trace_open(g_quark_to_string(self
->pathname
), self
->is_live
);
1166 void ltt_trace_close(LttTrace
*t
)
1168 g_datalist_clear(&t
->tracefiles
);
1173 /*****************************************************************************
1174 * Get the start time and end time of the trace
1175 ****************************************************************************/
1177 void ltt_tracefile_time_span_get(LttTracefile
*tf
,
1178 LttTime
*start
, LttTime
*end
)
1183 err
= map_block(tf
, 0);
1184 /* Only empty live traces will return ERANGE */
1186 *start
= ltt_time_infinite
;
1187 *end
= ltt_time_zero
;
1189 } else if(unlikely(err
)) {
1190 g_error("Can not map block");
1191 *start
= ltt_time_infinite
;
1193 *start
= tf
->buffer
.begin
.timestamp
;
1195 err
= map_block(tf
, tf
->num_blocks
- 1); /* Last block */
1197 g_error("Can not map block");
1198 *end
= ltt_time_zero
;
1200 *end
= tf
->buffer
.end
.timestamp
;
1202 g_assert(end
->tv_sec
<= G_MAXUINT
);
1205 struct tracefile_time_span_get_args
{
1211 static void group_time_span_get(GQuark name
, gpointer data
, gpointer user_data
)
1213 struct tracefile_time_span_get_args
*args
=
1214 (struct tracefile_time_span_get_args
*)user_data
;
1216 GArray
*group
= (GArray
*)data
;
1222 for(i
=0; i
<group
->len
; i
++) {
1223 tf
= &g_array_index (group
, LttTracefile
, i
);
1224 if(tf
->cpu_online
) {
1225 ltt_tracefile_time_span_get(tf
, &tmp_start
, &tmp_end
);
1226 if(ltt_time_compare(*args
->start
, tmp_start
)>0) *args
->start
= tmp_start
;
1227 if(ltt_time_compare(*args
->end
, tmp_end
)<0) *args
->end
= tmp_end
;
1232 /* return the start and end time of a trace */
1234 void ltt_trace_time_span_get(LttTrace
*t
, LttTime
*start
, LttTime
*end
)
1236 LttTime min_start
= ltt_time_infinite
;
1237 LttTime max_end
= ltt_time_zero
;
1238 struct tracefile_time_span_get_args args
= { t
, &min_start
, &max_end
};
1240 g_datalist_foreach(&t
->tracefiles
, &group_time_span_get
, &args
);
1242 if(start
!= NULL
) *start
= min_start
;
1243 if(end
!= NULL
) *end
= max_end
;
1248 /* Seek to the first event in a tracefile that has a time equal or greater than
1249 * the time passed in parameter.
1251 * If the time parameter is outside the tracefile time span, seek to the first
1252 * event or if after, return ERANGE.
1254 * If the time parameter is before the first event, we have to seek specially to
1257 * If the time is after the end of the trace, return ERANGE.
1259 * Do a binary search to find the right block, then a sequential search in the
1260 * block to find the event.
1262 * In the special case where the time requested fits inside a block that has no
1263 * event corresponding to the requested time, the first event of the next block
1266 * IMPORTANT NOTE : // FIXME everywhere...
1268 * You MUST NOT do a ltt_tracefile_read right after a ltt_tracefile_seek_time :
1269 * you will jump over an event if you do.
1271 * Return value : 0 : no error, the tf->event can be used
1272 * ERANGE : time if after the last event of the trace
1273 * otherwise : this is an error.
1277 int ltt_tracefile_seek_time(LttTracefile
*tf
, LttTime time
)
1281 unsigned int block_num
, high
, low
;
1283 /* seek at the beginning of trace */
1284 err
= map_block(tf
, 0); /* First block */
1285 if(unlikely(err
== ERANGE
)) {
1287 } else if(unlikely(err
)) {
1288 g_error("Can not map block");
1292 /* If the time is lower or equal the beginning of the trace,
1293 * go to the first event. */
1294 if(ltt_time_compare(time
, tf
->buffer
.begin
.timestamp
) <= 0) {
1295 ret
= ltt_tracefile_read(tf
);
1296 if(ret
== ERANGE
) goto range
;
1297 else if (ret
) goto fail
;
1298 goto found
; /* There is either no event in the trace or the event points
1299 to the first event in the trace */
1302 err
= map_block(tf
, tf
->num_blocks
- 1); /* Last block */
1304 g_error("Can not map block");
1308 /* If the time is after the end of the trace, return ERANGE. */
1309 if(ltt_time_compare(time
, tf
->buffer
.end
.timestamp
) > 0) {
1313 /* Binary search the block */
1314 high
= tf
->num_blocks
- 1;
1318 block_num
= ((high
-low
) / 2) + low
;
1320 err
= map_block(tf
, block_num
);
1322 g_error("Can not map block");
1326 /* We cannot divide anymore : this is what would happen if the time
1327 * requested was exactly between two consecutive buffers'end and start
1328 * timestamps. This is also what would happend if we didn't deal with out
1329 * of span cases prior in this function. */
1330 /* The event is right in the buffer!
1331 * (or in the next buffer first event) */
1333 ret
= ltt_tracefile_read(tf
);
1334 if(ret
== ERANGE
) goto range
; /* ERANGE or EPERM */
1335 else if(ret
) goto fail
;
1337 if(ltt_time_compare(time
, tf
->event
.event_time
) <= 0)
1341 } else if(ltt_time_compare(time
, tf
->buffer
.begin
.timestamp
) < 0) {
1343 * Go to lower part. We don't want block_num - 1 since block_num
1344 * can equal low , in which case high < low.
1347 } else if(ltt_time_compare(time
, tf
->buffer
.end
.timestamp
) > 0) {
1348 /* go to higher part */
1349 low
= block_num
+ 1;
1350 } else {/* The event is right in the buffer!
1351 (or in the next buffer first event) */
1353 ret
= ltt_tracefile_read(tf
);
1354 if(ret
== ERANGE
) goto range
; /* ERANGE or EPERM */
1355 else if(ret
) goto fail
;
1357 if(ltt_time_compare(time
, tf
->event
.event_time
) <= 0)
1369 /* Error handling */
1371 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1372 g_quark_to_string(tf
->name
));
1377 /* Save the current tracefile position in the passed position pointer */
1378 int ltt_tracefile_get_current_position(const LttTracefile
*tf
, LttEventPosition
*ep
)
1380 /* TODO ybrosseau 2011-06-07: Maybe add some error checking
1381 (ex: check the validity of the arguments pointer) */
1382 ltt_event_position(&(tf
->event
), ep
);
1387 /* Seek to a position indicated by an LttEventPosition
1390 int ltt_tracefile_seek_position(LttTracefile
*tf
, const LttEventPosition
*ep
)
1394 if(ep
->tracefile
!= tf
) {
1398 err
= map_block(tf
, ep
->block
);
1400 g_error("Can not map block");
1404 tf
->event
.offset
= ep
->offset
;
1406 /* Put back the event real tsc */
1407 tf
->event
.tsc
= ep
->tsc
;
1408 tf
->buffer
.tsc
= ep
->tsc
;
1410 err
= ltt_tracefile_read_update_event(tf
);
1413 /* deactivate this, as it does nothing for now
1414 err = ltt_tracefile_read_op(tf);
1421 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1422 g_quark_to_string(tf
->name
));
1427 * Convert a value in "TSC scale" to a value in nanoseconds
1429 guint64
tsc_to_uint64(guint32 freq_scale
, uint64_t start_freq
, guint64 tsc
)
1431 return (double) tsc
* NANOSECONDS_PER_SECOND
* freq_scale
/ start_freq
;
1434 /* Given a TSC value, return the LttTime (seconds,nanoseconds) it
1437 LttTime
ltt_interpolate_time_from_tsc(LttTracefile
*tf
, guint64 tsc
)
1439 return ltt_time_from_uint64(tsc_to_uint64(tf
->trace
->freq_scale
,
1440 tf
->trace
->start_freq
, tf
->trace
->drift
* tsc
+
1441 tf
->trace
->offset
));
1444 /* Calculate the real event time based on the buffer boundaries */
1445 LttTime
ltt_interpolate_time(LttTracefile
*tf
, LttEvent
*event
)
1447 return ltt_interpolate_time_from_tsc(tf
, tf
->buffer
.tsc
);
1451 /* Get the current event of the tracefile : valid until the next read */
1452 LttEvent
*ltt_tracefile_get_event(LttTracefile
*tf
)
1457 /*****************************************************************************
1459 * ltt_tracefile_read : Read the next event in the tracefile
1464 * Returns 0 if an event can be used in tf->event.
1465 * Returns ERANGE on end of trace. The event in tf->event still can be used
1466 * (if the last block was not empty).
1467 * Returns EPERM on error.
1469 * This function does make the tracefile event structure point to the event
1470 * currently pointed to by the tf->event.
1472 * Note : you must call a ltt_tracefile_seek to the beginning of the trace to
1473 * reinitialize it after an error if you want results to be coherent.
1474 * It would be the case if a end of trace last buffer has no event : the end
1475 * of trace wouldn't be returned, but an error.
1476 * We make the assumption there is at least one event per buffer.
1477 ****************************************************************************/
1479 int ltt_tracefile_read(LttTracefile
*tf
)
1483 err
= ltt_tracefile_read_seek(tf
);
1485 err
= ltt_tracefile_read_update_event(tf
);
1488 /* deactivate this, as it does nothing for now
1489 err = ltt_tracefile_read_op(tf);
1496 int ltt_tracefile_read_seek(LttTracefile
*tf
)
1500 /* Get next buffer until we finally have an event, or end of trace */
1502 err
= ltt_seek_next_event(tf
);
1503 if(unlikely(err
== ENOPROTOOPT
)) {
1507 /* Are we at the end of the buffer ? */
1509 if(unlikely(tf
->buffer
.index
== tf
->num_blocks
-1)){ /* end of trace ? */
1512 /* get next block */
1513 err
= map_block(tf
, tf
->buffer
.index
+ 1);
1515 g_error("Can not map block");
1519 } else break; /* We found an event ! */
1525 /* do an operation when reading a new event */
1527 /* This function does nothing for now */
1529 int ltt_tracefile_read_op(LttTracefile
*tf
)
1535 /* do event specific operation */
1543 static void print_debug_event_header(LttEvent
*ev
, void *start_pos
, void *end_pos
)
1545 unsigned int offset
= 0;
1548 g_printf("Event header (tracefile %s offset %" PRIx64
"):\n",
1549 g_quark_to_string(ev
->tracefile
->long_name
),
1550 (uint64_t)ev
->tracefile
->buffer
.offset
+
1551 (long)start_pos
- (long)ev
->tracefile
->buffer
.head
);
1553 while (offset
< (long)end_pos
- (long)start_pos
) {
1554 g_printf("%8lx", (long)start_pos
- (long)ev
->tracefile
->buffer
.head
+ offset
);
1557 for (i
= 0; i
< 4 ; i
++) {
1558 for (j
= 0; j
< 4; j
++) {
1559 if (offset
+ ((i
* 4) + j
) <
1560 (long)end_pos
- (long)start_pos
)
1562 ((char*)start_pos
)[offset
+ ((i
* 4) + j
)]);
1576 /* same as ltt_tracefile_read, but does not seek to the next event nor call
1577 * event specific operation. */
1578 int ltt_tracefile_read_update_event(LttTracefile
*tf
)
1583 guint16 packed_evid
; /* event id reader from the 5 bits in header */
1586 pos
= tf
->buffer
.head
+ event
->offset
;
1588 /* Read event header */
1590 /* Align the head */
1591 pos
+= ltt_align((size_t)pos
, sizeof(guint32
), tf
->alignment
);
1594 event
->timestamp
= ltt_get_uint32(LTT_GET_BO(tf
), pos
);
1595 event
->event_id
= packed_evid
= event
->timestamp
>> tf
->tscbits
;
1596 event
->timestamp
= event
->timestamp
& tf
->tsc_mask
;
1597 pos
+= sizeof(guint32
);
1599 switch (packed_evid
) {
1600 case 29: /* LTT_RFLAG_ID_SIZE_TSC */
1601 event
->event_id
= ltt_get_uint16(LTT_GET_BO(tf
), pos
);
1602 pos
+= sizeof(guint16
);
1603 event
->event_size
= ltt_get_uint16(LTT_GET_BO(tf
), pos
);
1604 pos
+= sizeof(guint16
);
1605 if (event
->event_size
== 0xFFFF) {
1606 event
->event_size
= ltt_get_uint32(LTT_GET_BO(tf
), pos
);
1607 pos
+= sizeof(guint32
);
1609 pos
+= ltt_align((size_t)pos
, sizeof(guint64
), tf
->alignment
);
1610 tf
->buffer
.tsc
= ltt_get_uint64(LTT_GET_BO(tf
), pos
);
1611 pos
+= sizeof(guint64
);
1613 case 30: /* LTT_RFLAG_ID_SIZE */
1614 event
->event_id
= ltt_get_uint16(LTT_GET_BO(tf
), pos
);
1615 pos
+= sizeof(guint16
);
1616 event
->event_size
= ltt_get_uint16(LTT_GET_BO(tf
), pos
);
1617 pos
+= sizeof(guint16
);
1618 if (event
->event_size
== 0xFFFF) {
1619 event
->event_size
= ltt_get_uint32(LTT_GET_BO(tf
), pos
);
1620 pos
+= sizeof(guint32
);
1623 case 31: /* LTT_RFLAG_ID */
1624 event
->event_id
= ltt_get_uint16(LTT_GET_BO(tf
), pos
);
1625 pos
+= sizeof(guint16
);
1626 event
->event_size
= G_MAXUINT
;
1629 event
->event_size
= G_MAXUINT
;
1633 if (likely(packed_evid
!= 29)) {
1634 /* No extended timestamp */
1635 if (event
->timestamp
< (tf
->buffer
.tsc
& tf
->tsc_mask
))
1636 tf
->buffer
.tsc
= ((tf
->buffer
.tsc
& ~tf
->tsc_mask
) /* overflow */
1637 + tf
->tsc_mask_next_bit
)
1638 | (guint64
)event
->timestamp
;
1640 tf
->buffer
.tsc
= (tf
->buffer
.tsc
& ~tf
->tsc_mask
) /* no overflow */
1641 | (guint64
)event
->timestamp
;
1643 event
->tsc
= tf
->buffer
.tsc
;
1645 event
->event_time
= ltt_interpolate_time(tf
, event
);
1648 print_debug_event_header(event
, pos_aligned
, pos
);
1653 * Let ltt_update_event_size update event->data according to the largest
1654 * alignment within the payload.
1655 * Get the data size and update the event fields with the current
1657 ltt_update_event_size(tf
);
1663 /****************************************************************************
1665 * map_block : map a block from the file
1667 * lttdes : ltt trace file
1668 * whichBlock : the block which will be read
1671 * EINVAL : lseek fail
1672 * EIO : can not read from the file
1673 ****************************************************************************/
1675 static gint
map_block(LttTracefile
* tf
, guint block_num
)
1677 int page_size
= getpagesize();
1678 ltt_subbuffer_header_t
*header
;
1683 if(tf
->num_blocks
== 0) {
1688 g_assert(block_num
< tf
->num_blocks
);
1690 if(tf
->buffer
.head
!= NULL
) {
1691 if(munmap(tf
->buffer
.head
, PAGE_ALIGN(tf
->buffer
.size
))) {
1692 g_warning("unmap size : %u\n",
1693 PAGE_ALIGN(tf
->buffer
.size
));
1694 perror("munmap error");
1699 ret
= get_block_offset_size(tf
, block_num
, &offset
, &size
);
1702 g_debug("Map block %u, offset %llu, size %u\n", block_num
,
1703 (unsigned long long)offset
, (unsigned int)size
);
1705 /* Multiple of pages aligned head */
1706 tf
->buffer
.head
= mmap(0, (size_t)size
, PROT_READ
, MAP_PRIVATE
,
1707 tf
->fd
, (off_t
)offset
);
1709 if(tf
->buffer
.head
== MAP_FAILED
) {
1710 perror("Error in allocating memory for buffer of tracefile");
1714 g_assert( ( (gulong
)tf
->buffer
.head
&(8-1) ) == 0); // make sure it's aligned.
1716 tf
->buffer
.index
= block_num
;
1718 header
= (ltt_subbuffer_header_t
*)tf
->buffer
.head
;
1720 tf
->buffer
.begin
.cycle_count
= ltt_get_uint64(LTT_GET_BO(tf
),
1721 &header
->cycle_count_begin
);
1722 tf
->buffer
.end
.cycle_count
= ltt_get_uint64(LTT_GET_BO(tf
),
1723 &header
->cycle_count_end
);
1724 tf
->buffer
.offset
= offset
;
1725 tf
->buffer
.size
= ltt_get_uint32(LTT_GET_BO(tf
),
1727 tf
->buffer
.data_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1728 &header
->data_size
);
1729 tf
->buffer
.tsc
= tf
->buffer
.begin
.cycle_count
;
1730 tf
->event
.tsc
= tf
->buffer
.tsc
;
1731 tf
->buffer
.freq
= tf
->buffer
.begin
.freq
;
1733 g_assert(size
== tf
->buffer
.size
);
1734 g_assert(tf
->buffer
.data_size
<= tf
->buffer
.size
);
1736 if (tf
->trace
->start_freq
)
1738 tf
->buffer
.begin
.freq
= tf
->trace
->start_freq
;
1739 tf
->buffer
.begin
.timestamp
= ltt_interpolate_time_from_tsc(tf
,
1740 tf
->buffer
.begin
.cycle_count
);
1741 tf
->buffer
.end
.freq
= tf
->trace
->start_freq
;
1742 tf
->buffer
.end
.timestamp
= ltt_interpolate_time_from_tsc(tf
,
1743 tf
->buffer
.end
.cycle_count
);
1746 /* Make the current event point to the beginning of the buffer :
1747 * it means that the event read must get the first event. */
1748 tf
->event
.tracefile
= tf
;
1749 tf
->event
.block
= block_num
;
1750 tf
->event
.offset
= 0;
1752 if (header
->events_lost
) {
1753 g_warning("%d events lost so far in tracefile %s at block %u",
1754 (guint
)header
->events_lost
,
1755 g_quark_to_string(tf
->long_name
),
1757 tf
->events_lost
= header
->events_lost
;
1759 if (header
->subbuf_corrupt
) {
1760 g_warning("%d subbuffer(s) corrupted so far in tracefile %s at block %u",
1761 (guint
)header
->subbuf_corrupt
,
1762 g_quark_to_string(tf
->long_name
),
1764 tf
->subbuf_corrupt
= header
->subbuf_corrupt
;
1773 static void print_debug_event_data(LttEvent
*ev
)
1775 unsigned int offset
= 0;
1778 if (!max(ev
->event_size
, ev
->data_size
))
1781 g_printf("Event data (tracefile %s offset %" PRIx64
"):\n",
1782 g_quark_to_string(ev
->tracefile
->long_name
),
1783 (uint64_t)ev
->tracefile
->buffer
.offset
1784 + (long)ev
->data
- (long)ev
->tracefile
->buffer
.head
);
1786 while (offset
< max(ev
->event_size
, ev
->data_size
)) {
1787 g_printf("%8lx", (long)ev
->data
+ offset
1788 - (long)ev
->tracefile
->buffer
.head
);
1791 for (i
= 0; i
< 4 ; i
++) {
1792 for (j
= 0; j
< 4; j
++) {
1793 if (offset
+ ((i
* 4) + j
) < max(ev
->event_size
, ev
->data_size
))
1794 g_printf("%02hhX", ((char*)ev
->data
)[offset
+ ((i
* 4) + j
)]);
1805 for (i
= 0; i
< 4; i
++) {
1806 for (j
= 0; j
< 4; j
++) {
1807 if (offset
+ ((i
* 4) + j
) < max(ev
->event_size
, ev
->data_size
)) {
1808 if (isprint(((char*)ev
->data
)[offset
+ ((i
* 4) + j
)]))
1809 g_printf("%c", ((char*)ev
->data
)[offset
+ ((i
* 4) + j
)]);
1821 /* It will update the fields offsets too */
1822 void ltt_update_event_size(LttTracefile
*tf
)
1825 struct marker_info
*info
;
1827 if (tf
->name
== LTT_TRACEFILE_NAME_METADATA
) {
1828 switch((enum marker_id
)tf
->event
.event_id
) {
1829 case MARKER_ID_SET_MARKER_ID
:
1830 size
= strlen((char*)tf
->event
.data
) + 1;
1831 g_debug("marker %s id set", (char*)tf
->event
.data
+ size
);
1832 size
+= strlen((char*)tf
->event
.data
+ size
) + 1;
1833 size
+= ltt_align(size
, sizeof(guint16
), tf
->alignment
);
1834 size
+= sizeof(guint16
);
1835 size
+= sizeof(guint8
);
1836 size
+= sizeof(guint8
);
1837 size
+= sizeof(guint8
);
1838 size
+= sizeof(guint8
);
1839 size
+= sizeof(guint8
);
1841 case MARKER_ID_SET_MARKER_FORMAT
:
1842 size
= strlen((char*)tf
->event
.data
) + 1;
1843 g_debug("marker %s format set", (char*)tf
->event
.data
);
1844 size
+= strlen((char*)tf
->event
.data
+ size
) + 1;
1845 size
+= strlen((char*)tf
->event
.data
+ size
) + 1;
1850 info
= marker_get_info_from_id(tf
->mdata
, tf
->event
.event_id
);
1852 if (tf
->event
.event_id
>= MARKER_CORE_IDS
)
1853 g_assert(info
!= NULL
);
1855 /* Do not update field offsets of core markers when initially reading the
1856 * metadata tracefile when the infos about these markers do not exist yet.
1858 if (likely(info
&& info
->fields
)) {
1860 tf
->event
.data
+= ltt_align((off_t
)(unsigned long)tf
->event
.data
,
1861 info
->largest_align
,
1863 /* size, dynamically computed */
1864 if (info
->size
!= -1)
1867 size
= marker_update_fields_offsets(info
, tf
->event
.data
);
1868 /* Update per-tracefile offsets */
1869 marker_update_event_fields_offsets(tf
->event
.fields_offsets
, info
);
1872 tf
->event
.data_size
= size
;
1874 /* Check consistency between kernel and LTTV structure sizes */
1875 if(tf
->event
.event_size
== G_MAXUINT
) {
1876 /* Event size too big to fit in the event size field */
1877 tf
->event
.event_size
= tf
->event
.data_size
;
1881 print_debug_event_data(&tf
->event
);
1883 if (tf
->event
.data_size
!= tf
->event
.event_size
) {
1884 struct marker_info
*info
= marker_get_info_from_id(tf
->mdata
,
1885 tf
->event
.event_id
);
1887 g_error("Undescribed event %hhu in channel %s", tf
->event
.event_id
,
1888 g_quark_to_string(tf
->name
));
1889 g_error("Kernel/LTTV event size differs for event %s: kernel %u, LTTV %u",
1890 g_quark_to_string(info
->name
),
1891 tf
->event
.event_size
, tf
->event
.data_size
);
1897 /* Take the tf current event offset and use the event id to figure out where is
1898 * the next event offset.
1900 * This is an internal function not aiming at being used elsewhere : it will
1901 * not jump over the current block limits. Please consider using
1902 * ltt_tracefile_read to do this.
1904 * Returns 0 on success
1905 * ERANGE if we are at the end of the buffer.
1906 * ENOPROTOOPT if an error occured when getting the current event size.
1908 static int ltt_seek_next_event(LttTracefile
*tf
)
1913 /* seek over the buffer header if we are at the buffer start */
1914 if(tf
->event
.offset
== 0) {
1915 tf
->event
.offset
+= tf
->buffer_header_size
;
1917 if(tf
->event
.offset
== tf
->buffer
.data_size
) {
1923 pos
= tf
->event
.data
;
1925 if(tf
->event
.data_size
< 0) goto error
;
1927 pos
+= (size_t)tf
->event
.data_size
;
1929 tf
->event
.offset
= pos
- tf
->buffer
.head
;
1931 if(tf
->event
.offset
>= tf
->buffer
.data_size
) {
1935 g_assert(tf
->event
.offset
< tf
->buffer
.data_size
);
1941 g_error("Error in ltt_seek_next_event for tracefile %s",
1942 g_quark_to_string(tf
->name
));
1947 /*****************************************************************************
1949 * ltt_get_int : get an integer number
1951 * reverse_byte_order: must we reverse the byte order ?
1952 * size : the size of the integer
1953 * ptr : the data pointer
1955 * gint64 : a 64 bits integer
1956 ****************************************************************************/
1958 gint64
ltt_get_int(gboolean reverse_byte_order
, gint size
, void *data
)
1963 case 1: val
= *((gint8
*)data
); break;
1964 case 2: val
= ltt_get_int16(reverse_byte_order
, data
); break;
1965 case 4: val
= ltt_get_int32(reverse_byte_order
, data
); break;
1966 case 8: val
= ltt_get_int64(reverse_byte_order
, data
); break;
1967 default: val
= ltt_get_int64(reverse_byte_order
, data
);
1968 g_critical("get_int : integer size %d unknown", size
);
1975 /*****************************************************************************
1977 * ltt_get_uint : get an unsigned integer number
1979 * reverse_byte_order: must we reverse the byte order ?
1980 * size : the size of the integer
1981 * ptr : the data pointer
1983 * guint64 : a 64 bits unsigned integer
1984 ****************************************************************************/
1986 guint64
ltt_get_uint(gboolean reverse_byte_order
, gint size
, void *data
)
1991 case 1: val
= *((gint8
*)data
); break;
1992 case 2: val
= ltt_get_uint16(reverse_byte_order
, data
); break;
1993 case 4: val
= ltt_get_uint32(reverse_byte_order
, data
); break;
1994 case 8: val
= ltt_get_uint64(reverse_byte_order
, data
); break;
1995 default: val
= ltt_get_uint64(reverse_byte_order
, data
);
1996 g_critical("get_uint : unsigned integer size %d unknown",
2005 /* get the node name of the system */
2007 char * ltt_trace_system_description_node_name (LttSystemDescription
* s
)
2009 return s
->node_name
;
2013 /* get the domain name of the system */
2015 char * ltt_trace_system_description_domain_name (LttSystemDescription
* s
)
2017 return s
->domain_name
;
2021 /* get the description of the system */
2023 char * ltt_trace_system_description_description (LttSystemDescription
* s
)
2025 return s
->description
;
2029 /* get the NTP corrected start time of the trace */
2030 LttTime
ltt_trace_start_time(LttTrace
*t
)
2032 return t
->start_time
;
2035 /* get the monotonic start time of the trace */
2036 LttTime
ltt_trace_start_time_monotonic(LttTrace
*t
)
2038 return t
->start_time_from_tsc
;
2041 static __attribute__ ((__unused__
)) LttTracefile
*ltt_tracefile_new()
2044 tf
= g_new(LttTracefile
, 1);
2045 tf
->event
.tracefile
= tf
;
2049 static __attribute__ ((__unused__
)) void ltt_tracefile_destroy(LttTracefile
*tf
)
2054 static __attribute__ ((__unused__
)) void ltt_tracefile_copy(LttTracefile
*dest
, const LttTracefile
*src
)
2059 /* Before library loading... */
2061 static __attribute__((constructor
)) void init(void)
2063 LTT_TRACEFILE_NAME_METADATA
= g_quark_from_string("metadata");
2066 /*****************************************************************************
2068 * ltt_tracefile_open_header : based on ltt_tracefile_open but it stops
2069 * when it gets the header
2071 * fileName : path to the tracefile
2072 * tf : the tracefile (metadata_0) where the header will be read
2074 * ltt_subbuffer_header_t : the header containing the version number
2075 ****************************************************************************/
2076 static ltt_subbuffer_header_t
* ltt_tracefile_open_header(gchar
*fileName
, LttTracefile
*tf
)
2078 struct stat lTDFStat
; /* Trace data file status */
2079 ltt_subbuffer_header_t
*header
;
2080 int page_size
= getpagesize();
2083 tf
->long_name
= g_quark_from_string(fileName
);
2084 tf
->fd
= open(fileName
, O_RDONLY
);
2086 g_warning("Unable to open input data file %s\n", fileName
);
2090 /* Get the file's status */
2091 if(fstat(tf
->fd
, &lTDFStat
) < 0){
2092 g_warning("Unable to get the status of the input data file %s\n", fileName
);
2096 /* Is the file large enough to contain a trace */
2097 if(lTDFStat
.st_size
< (off_t
)(ltt_subbuffer_header_size())) {
2098 g_print("The input data file %s does not contain a trace\n", fileName
);
2102 /* Temporarily map the buffer start header to get trace information */
2103 /* Multiple of pages aligned head */
2104 tf
->buffer
.head
= mmap(0,PAGE_ALIGN(ltt_subbuffer_header_size()), PROT_READ
, MAP_PRIVATE
, tf
->fd
, 0);
2106 if(tf
->buffer
.head
== MAP_FAILED
) {
2107 perror("Error in allocating memory for buffer of tracefile");
2110 g_assert( ( (gulong
)tf
->buffer
.head
&(8-1) ) == 0); // make sure it's aligned.
2112 header
= (ltt_subbuffer_header_t
*)tf
->buffer
.head
;
2123 /*****************************************************************************
2125 * get_version : get the trace version from a metadata_0 trace file
2127 * pathname : path to the trace
2128 * version_number : the struct that will get the version number
2130 * int : 1 if succeed, -1 if error
2131 ****************************************************************************/
2132 int ltt_get_trace_version(const gchar
*pathname
, struct LttTraceVersion
*version_number
)
2134 gchar abs_path
[PATH_MAX
];
2137 struct dirent
*entry
;
2138 struct stat stat_buf
;
2139 gchar path
[PATH_MAX
];
2141 LttTracefile tmp_tf
;
2143 ltt_subbuffer_header_t
*header
;
2145 t
= g_new(LttTrace
, 1);
2147 get_absolute_pathname(pathname
, abs_path
);
2149 /* Test to see if it looks like a trace */
2150 dir
= opendir(abs_path
);
2157 while((entry
= readdir(dir
)) != NULL
) {
2158 strcpy(path
, abs_path
);
2160 strcat(path
, entry
->d_name
);
2161 ret
= stat(path
, &stat_buf
);
2169 dir
= opendir(abs_path
);
2171 while((entry
= readdir(dir
)) != NULL
) {
2172 if(entry
->d_name
[0] == '.') continue;
2173 if(g_str_has_prefix(entry
->d_name
, "metadata_") != 0) continue;
2175 strcpy(path
, abs_path
);
2177 strcat(path
, entry
->d_name
);
2183 header
= ltt_tracefile_open_header(path
, &tmp_tf
);
2185 if(header
== NULL
) {
2186 g_info("Error getting the header %s", path
);
2187 continue; /* error opening the tracefile : bad magic number ? */
2190 version_number
->ltt_major_version
= header
->major_version
;
2191 version_number
->ltt_minor_version
= header
->minor_version
;