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/type.h>
49 #include <ltt/ltt-types.h>
50 #include <ltt/marker.h>
52 /* Facility names used in this file */
54 GQuark LTT_FACILITY_NAME_HEARTBEAT
,
55 LTT_EVENT_NAME_HEARTBEAT
,
56 LTT_EVENT_NAME_HEARTBEAT_FULL
;
57 GQuark LTT_TRACEFILE_NAME_FACILITIES
;
64 #define __UNUSED__ __attribute__((__unused__))
66 #define g_info(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, format)
69 #define g_debug(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format)
74 /* Those macros must be called from within a function where page_size is a known
76 #define PAGE_MASK (~(page_size-1))
77 #define PAGE_ALIGN(addr) (((addr)+page_size-1)&PAGE_MASK)
79 LttTrace
*father_trace
= NULL
;
81 /* set the offset of the fields belonging to the event,
82 need the information of the archecture */
83 //void set_fields_offsets(LttTracefile *tf, LttEventType *event_type);
84 //size_t get_fields_offsets(LttTracefile *tf, LttEventType *event_type, void *data);
86 /* get the size of the field type according to
87 * The facility size information. */
89 static inline void preset_field_type_size(LttTracefile
*tf
,
90 LttEventType
*event_type
,
91 off_t offset_root
, off_t offset_parent
,
92 enum field_status
*fixed_root
, enum field_status
*fixed_parent
,
96 /* map a fixed size or a block information from the file (fd) */
97 static gint
map_block(LttTracefile
* tf
, guint block_num
);
99 /* calculate nsec per cycles for current block */
101 static guint32
calc_nsecs_per_cycle(LttTracefile
* t
);
102 static guint64
cycles_2_ns(LttTracefile
*tf
, guint64 cycles
);
105 /* go to the next event */
106 static int ltt_seek_next_event(LttTracefile
*tf
);
108 //void ltt_update_event_size(LttTracefile *tf);
110 static int open_tracefiles(LttTrace
*trace
, gchar
*root_path
,
111 gchar
*relative_path
);
112 static int ltt_process_facility_tracefile(LttTracefile
*tf
);
113 static void ltt_tracefile_time_span_get(LttTracefile
*tf
,
114 LttTime
*start
, LttTime
*end
);
115 static void group_time_span_get(GQuark name
, gpointer data
, gpointer user_data
);
116 static gint
map_block(LttTracefile
* tf
, guint block_num
);
117 static int ltt_seek_next_event(LttTracefile
*tf
);
118 static void __attribute__((constructor
)) init(void);
119 static void ltt_update_event_size(LttTracefile
*tf
);
121 /* Enable event debugging */
122 static int a_event_debug
= 0;
124 void ltt_event_debug(int state
)
126 a_event_debug
= state
;
129 //void precompute_offsets(LttFacility *fac, LttEventType *event);
132 /* Functions to parse system.xml file (using glib xml parser) */
133 static void parser_start_element (GMarkupParseContext __UNUSED__
*context
,
134 const gchar
*element_name
,
135 const gchar
**attribute_names
,
136 const gchar
**attribute_values
,
141 LttSystemDescription
* des
= (LttSystemDescription
* )user_data
;
142 if(strcmp("system", element_name
)){
143 *error
= g_error_new(G_MARKUP_ERROR
,
145 "This is not system.xml file");
149 while(attribute_names
[i
]){
150 if(strcmp("node_name", attribute_names
[i
])==0){
151 des
->node_name
= g_strdup(attribute_values
[i
]);
152 }else if(strcmp("domainname", attribute_names
[i
])==0){
153 des
->domain_name
= g_strdup(attribute_values
[i
]);
154 }else if(strcmp("cpu", attribute_names
[i
])==0){
155 des
->nb_cpu
= atoi(attribute_values
[i
]);
156 }else if(strcmp("arch_size", attribute_names
[i
])==0){
157 if(strcmp(attribute_values
[i
],"LP32") == 0) des
->size
= LTT_LP32
;
158 else if(strcmp(attribute_values
[i
],"ILP32") == 0) des
->size
= LTT_ILP32
;
159 else if(strcmp(attribute_values
[i
],"LP64") == 0) des
->size
= LTT_LP64
;
160 else if(strcmp(attribute_values
[i
],"ILP64") == 0) des
->size
= LTT_ILP64
;
161 else if(strcmp(attribute_values
[i
],"UNKNOWN") == 0) des
->size
= LTT_UNKNOWN
;
162 }else if(strcmp("endian", attribute_names
[i
])==0){
163 if(strcmp(attribute_values
[i
],"LITTLE_ENDIAN") == 0)
164 des
->endian
= LTT_LITTLE_ENDIAN
;
165 else if(strcmp(attribute_values
[i
],"BIG_ENDIAN") == 0)
166 des
->endian
= LTT_BIG_ENDIAN
;
167 }else if(strcmp("kernel_name", attribute_names
[i
])==0){
168 des
->kernel_name
= g_strdup(attribute_values
[i
]);
169 }else if(strcmp("kernel_release", attribute_names
[i
])==0){
170 des
->kernel_release
= g_strdup(attribute_values
[i
]);
171 }else if(strcmp("kernel_version", attribute_names
[i
])==0){
172 des
->kernel_version
= g_strdup(attribute_values
[i
]);
173 }else if(strcmp("machine", attribute_names
[i
])==0){
174 des
->machine
= g_strdup(attribute_values
[i
]);
175 }else if(strcmp("processor", attribute_names
[i
])==0){
176 des
->processor
= g_strdup(attribute_values
[i
]);
177 }else if(strcmp("hardware_platform", attribute_names
[i
])==0){
178 des
->hardware_platform
= g_strdup(attribute_values
[i
]);
179 }else if(strcmp("operating_system", attribute_names
[i
])==0){
180 des
->operating_system
= g_strdup(attribute_values
[i
]);
181 }else if(strcmp("ltt_major_version", attribute_names
[i
])==0){
182 des
->ltt_major_version
= atoi(attribute_values
[i
]);
183 }else if(strcmp("ltt_minor_version", attribute_names
[i
])==0){
184 des
->ltt_minor_version
= atoi(attribute_values
[i
]);
185 }else if(strcmp("ltt_block_size", attribute_names
[i
])==0){
186 des
->ltt_block_size
= atoi(attribute_values
[i
]);
188 *error
= g_error_new(G_MARKUP_ERROR
,
190 "Not a valid attribute");
197 static void parser_characters (GMarkupParseContext __UNUSED__
*context
,
199 gsize __UNUSED__ text_len
,
201 GError __UNUSED__
**error
)
203 LttSystemDescription
* des
= (LttSystemDescription
* )user_data
;
204 des
->description
= g_strdup(text
);
209 LttFacility
*ltt_trace_get_facility_by_num(LttTrace
*t
,
212 g_assert(num
< t
->facilities_by_num
->len
);
214 return &g_array_index(t
->facilities_by_num
, LttFacility
, num
);
219 guint
ltt_trace_get_num_cpu(LttTrace
*t
)
227 * Return value : 0 success, 1 bad tracefile
229 int parse_trace_header(void *header
, LttTracefile
*tf
, LttTrace
*t
)
231 guint32
*magic_number
= (guint32
*)header
;
232 struct ltt_trace_header_any
*any
= (struct ltt_trace_header_any
*)header
;
234 if(*magic_number
== LTT_MAGIC_NUMBER
)
236 else if(*magic_number
== LTT_REV_MAGIC_NUMBER
)
238 else /* invalid magic number, bad tracefile ! */
241 /* Get float byte order : might be different from int byte order
242 * (or is set to 0 if the trace has no float (kernel trace)) */
243 tf
->float_word_order
= any
->float_word_order
;
244 tf
->alignment
= any
->alignment
;
245 tf
->has_heartbeat
= any
->has_heartbeat
;
248 t
->arch_type
= ltt_get_uint32(LTT_GET_BO(tf
),
250 t
->arch_variant
= ltt_get_uint32(LTT_GET_BO(tf
),
252 t
->arch_size
= any
->arch_size
;
253 t
->ltt_major_version
= any
->major_version
;
254 t
->ltt_minor_version
= any
->minor_version
;
255 t
->flight_recorder
= any
->flight_recorder
;
256 // t->compact_facilities = NULL;
259 switch(any
->major_version
) {
262 g_warning("Unsupported trace version : %hhu.%hhu",
263 any
->major_version
, any
->minor_version
);
267 switch(any
->minor_version
) {
270 struct ltt_trace_header_1_0
*vheader
=
271 (struct ltt_trace_header_1_0
*)header
;
272 tf
->buffer_header_size
=
273 sizeof(struct ltt_block_start_header
)
274 + sizeof(struct ltt_trace_header_1_0
);
275 tf
->tsc_lsb_truncate
= vheader
->tsc_lsb_truncate
;
276 tf
->tscbits
= vheader
->tscbits
;
277 tf
->tsc_msb_cutoff
= 32 - tf
->tsc_lsb_truncate
- tf
->tscbits
;
278 tf
->compact_event_bits
= 32 - vheader
->compact_data_shift
;
279 tf
->tsc_mask
= ((1ULL << (tf
->tscbits
))-1);
280 tf
->tsc_mask
= tf
->tsc_mask
<< tf
->tsc_lsb_truncate
;
281 tf
->tsc_mask_next_bit
= (1ULL<<(tf
->tscbits
));
282 tf
->tsc_mask_next_bit
= tf
->tsc_mask_next_bit
<< tf
->tsc_lsb_truncate
;
284 t
->start_freq
= ltt_get_uint64(LTT_GET_BO(tf
),
285 &vheader
->start_freq
);
286 t
->freq_scale
= ltt_get_uint32(LTT_GET_BO(tf
),
287 &vheader
->freq_scale
);
289 t
->start_freq
= father_trace
->start_freq
;
290 t
->freq_scale
= father_trace
->freq_scale
;
295 t
->start_tsc
= ltt_get_uint64(LTT_GET_BO(tf
),
296 &vheader
->start_tsc
);
297 t
->start_monotonic
= ltt_get_uint64(LTT_GET_BO(tf
),
298 &vheader
->start_monotonic
);
299 t
->start_time
.tv_sec
= ltt_get_uint64(LTT_GET_BO(tf
),
300 &vheader
->start_time_sec
);
301 t
->start_time
.tv_nsec
= ltt_get_uint64(LTT_GET_BO(tf
),
302 &vheader
->start_time_usec
);
303 t
->start_time
.tv_nsec
*= 1000; /* microsec to nanosec */
305 t
->start_time_from_tsc
= ltt_time_from_uint64(
307 * (1000000000.0 / tf
->trace
->freq_scale
)
308 / (double)t
->start_freq
);
313 g_warning("Unsupported trace version : %hhu.%hhu",
314 any
->major_version
, any
->minor_version
);
319 g_warning("Unsupported trace version : %hhu.%hhu",
320 any
->major_version
, any
->minor_version
);
330 /*****************************************************************************
332 * ltt_tracefile_open : open a trace file, construct a LttTracefile
334 * t : the trace containing the tracefile
335 * fileName : path name of the trace file
336 * tf : the tracefile structure
338 * : 0 for success, -1 otherwise.
339 ****************************************************************************/
341 gint
ltt_tracefile_open(LttTrace
*t
, gchar
* fileName
, LttTracefile
*tf
)
343 struct stat lTDFStat
; /* Trace data file status */
344 struct ltt_block_start_header
*header
;
345 int page_size
= getpagesize();
348 tf
->long_name
= g_quark_from_string(fileName
);
350 tf
->fd
= open(fileName
, O_RDONLY
);
352 g_warning("Unable to open input data file %s\n", fileName
);
356 // Get the file's status
357 if(fstat(tf
->fd
, &lTDFStat
) < 0){
358 g_warning("Unable to get the status of the input data file %s\n", fileName
);
362 // Is the file large enough to contain a trace
363 if(lTDFStat
.st_size
<
364 (off_t
)(sizeof(struct ltt_block_start_header
)
365 + sizeof(struct ltt_trace_header_any
))){
366 g_print("The input data file %s does not contain a trace\n", fileName
);
370 /* Temporarily map the buffer start header to get trace information */
371 /* Multiple of pages aligned head */
372 tf
->buffer
.head
= mmap(0,
373 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)
374 + sizeof(struct ltt_trace_header_any
)), PROT_READ
,
375 MAP_PRIVATE
, tf
->fd
, 0);
376 if(tf
->buffer
.head
== MAP_FAILED
) {
377 perror("Error in allocating memory for buffer of tracefile");
380 g_assert( ( (gulong
)tf
->buffer
.head
&(8-1) ) == 0); // make sure it's aligned.
382 header
= (struct ltt_block_start_header
*)tf
->buffer
.head
;
384 if(parse_trace_header(header
->trace
, tf
, NULL
)) {
385 g_warning("parse_trace_header error");
389 //store the size of the file
390 tf
->file_size
= lTDFStat
.st_size
;
391 tf
->buf_size
= ltt_get_uint32(LTT_GET_BO(tf
), &header
->buf_size
);
392 tf
->num_blocks
= tf
->file_size
/ tf
->buf_size
;
394 if(munmap(tf
->buffer
.head
,
395 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)
396 + sizeof(struct ltt_trace_header_any
)))) {
397 g_warning("unmap size : %u\n",
398 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)
399 + sizeof(struct ltt_trace_header_any
)));
400 perror("munmap error");
403 tf
->buffer
.head
= NULL
;
405 //read the first block
406 if(map_block(tf
,0)) {
407 perror("Cannot map block for tracefile");
415 if(munmap(tf
->buffer
.head
,
416 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)
417 + sizeof(struct ltt_trace_header_any
)))) {
418 g_warning("unmap size : %u\n",
419 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)
420 + sizeof(struct ltt_trace_header_any
)));
421 perror("munmap error");
430 LttTrace
*ltt_tracefile_get_trace(LttTracefile
*tf
)
436 /*****************************************************************************
437 *Open control and per cpu tracefiles
438 ****************************************************************************/
440 void ltt_tracefile_open_cpu(LttTrace
*t
, gchar
* tracefile_name
)
443 tf
= ltt_tracefile_open(t
,tracefile_name
);
445 t
->per_cpu_tracefile_number
++;
446 g_ptr_array_add(t
->per_cpu_tracefiles
, tf
);
449 gint
ltt_tracefile_open_control(LttTrace
*t
, gchar
* control_name
)
458 tf
= ltt_tracefile_open(t
,control_name
);
460 g_warning("ltt_tracefile_open_control : bad file descriptor");
463 t
->control_tracefile_number
++;
464 g_ptr_array_add(t
->control_tracefiles
,tf
);
466 //parse facilities tracefile to get base_id
467 if(strcmp(&control_name
[strlen(control_name
)-10],"facilities") ==0){
469 if(!ltt_tracefile_read(tf
,&ev
)) return 0; // end of file
471 if(ev
.event_id
== TRACE_FACILITY_LOAD
){
473 fLoad
.name
= (gchar
*)pos
;
474 fLoad
.checksum
= *(LttChecksum
*)(pos
+ strlen(fLoad
.name
));
475 fLoad
.base_code
= *(guint32
*)(pos
+ strlen(fLoad
.name
) + sizeof(LttChecksum
));
477 for(i
=0;i
<t
->facility_number
;i
++){
478 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
,i
);
479 if(strcmp(f
->name
,fLoad
.name
)==0 && fLoad
.checksum
==f
->checksum
){
480 f
->base_id
= fLoad
.base_code
;
484 if(i
==t
->facility_number
) {
485 g_warning("Facility: %s, checksum: %u is not found",
486 fLoad
.name
,(unsigned int)fLoad
.checksum
);
489 }else if(ev
.event_id
== TRACE_BLOCK_START
){
491 }else if(ev
.event_id
== TRACE_BLOCK_END
){
494 g_warning("Not valid facilities trace file");
503 /*****************************************************************************
505 * ltt_tracefile_close: close a trace file,
507 * t : tracefile which will be closed
508 ****************************************************************************/
510 void ltt_tracefile_close(LttTracefile
*t
)
512 int page_size
= getpagesize();
514 if(t
->buffer
.head
!= NULL
)
515 if(munmap(t
->buffer
.head
, PAGE_ALIGN(t
->buf_size
))) {
516 g_warning("unmap size : %u\n",
517 PAGE_ALIGN(t
->buf_size
));
518 perror("munmap error");
526 /*****************************************************************************
527 *Get system information
528 ****************************************************************************/
530 gint
getSystemInfo(LttSystemDescription
* des
, gchar
* pathname
)
537 GMarkupParseContext
* context
;
538 GError
* error
= NULL
;
539 GMarkupParser markup_parser
=
541 parser_start_element
,
544 NULL
, /* passthrough */
548 fd
= g_open(pathname
, O_RDONLY
, 0);
550 g_warning("Can not open file : %s\n", pathname
);
554 iochan
= g_io_channel_unix_new(fd
);
556 context
= g_markup_parse_context_new(&markup_parser
, 0, des
,NULL
);
558 //while(fgets(buf,DIR_NAME_SIZE, fp) != NULL){
559 while(g_io_channel_read_line(iochan
, &buf
, &length
, NULL
, &error
)
560 != G_IO_STATUS_EOF
) {
563 g_warning("Can not read xml file: \n%s\n", error
->message
);
566 if(!g_markup_parse_context_parse(context
, buf
, length
, &error
)){
568 g_warning("Can not parse xml file: \n%s\n", error
->message
);
571 g_markup_parse_context_free(context
);
573 g_io_channel_shutdown(iochan
, FALSE
, &error
); /* No flush */
575 g_warning("Can not close file: \n%s\n", error
->message
);
583 g_markup_parse_context_free(context
);
585 g_io_channel_shutdown(iochan
, FALSE
, &error
); /* No flush */
587 g_warning("Can not close file: \n%s\n", error
->message
);
598 /*****************************************************************************
599 *The following functions get facility/tracefile information
600 ****************************************************************************/
602 gint
getFacilityInfo(LttTrace
*t
, gchar
* eventdefs
)
609 gchar fullname
[DIR_NAME_SIZE
];
610 GError
* error
= NULL
;
612 dir
= g_dir_open(eventdefs
, 0, &error
);
615 g_warning("Can not open directory: %s, %s\n", eventdefs
, error
->message
);
620 while((name
= g_dir_read_name(dir
)) != NULL
){
621 if(!g_pattern_match_simple("*.xml", name
)) continue;
622 strcpy(fullname
,eventdefs
);
623 strcat(fullname
,name
);
624 ltt_facility_open(t
,fullname
);
628 for(j
=0;j
<t
->facility_number
;j
++){
629 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, j
);
630 for(i
=0; i
<f
->event_number
; i
++){
632 setFieldsOffset(NULL
, et
, NULL
, t
);
639 /*****************************************************************************
640 *A trace is specified as a pathname to the directory containing all the
641 *associated data (control tracefiles, per cpu tracefiles, event
644 *When a trace is closed, all the associated facilities, types and fields
645 *are released as well.
649 /****************************************************************************
650 * get_absolute_pathname
652 * return the unique pathname in the system
654 * MD : Fixed this function so it uses realpath, dealing well with
655 * forgotten cases (.. were not used correctly before).
657 ****************************************************************************/
658 void get_absolute_pathname(const gchar
*pathname
, gchar
* abs_pathname
)
660 abs_pathname
[0] = '\0';
662 if ( realpath (pathname
, abs_pathname
) != NULL
)
666 /* error, return the original path unmodified */
667 strcpy(abs_pathname
, pathname
);
673 /* Search for something like : .*_.*
675 * The left side is the name, the right side is the number.
678 int get_tracefile_name_number(gchar
*raw_name
,
685 guint raw_name_len
= strlen(raw_name
);
686 gchar char_name
[PATH_MAX
];
693 for(i
=raw_name_len
-1;i
>=0;i
--) {
694 if(raw_name
[i
] == '_') break;
696 if(i
==-1) { /* Either not found or name length is 0 */
697 /* This is a userspace tracefile */
698 strncpy(char_name
, raw_name
, raw_name_len
);
699 char_name
[raw_name_len
] = '\0';
700 *name
= g_quark_from_string(char_name
);
701 *num
= 0; /* unknown cpu */
702 for(i
=0;i
<raw_name_len
;i
++) {
703 if(raw_name
[i
] == '/') {
708 for(;i
<raw_name_len
;i
++) {
709 if(raw_name
[i
] == '/') {
714 for(;i
<raw_name_len
;i
++) {
715 if(raw_name
[i
] == '-') {
719 if(i
== raw_name_len
) return -1;
721 tmpptr
= &raw_name
[i
];
722 for(;i
<raw_name_len
;i
++) {
723 if(raw_name
[i
] == '.') {
728 *tid
= strtoul(tmpptr
, &endptr
, 10);
730 return -1; /* No digit */
731 if(*tid
== ULONG_MAX
)
732 return -1; /* underflow / overflow */
734 tmpptr
= &raw_name
[i
];
735 for(;i
<raw_name_len
;i
++) {
736 if(raw_name
[i
] == '.') {
741 *pgid
= strtoul(tmpptr
, &endptr
, 10);
743 return -1; /* No digit */
744 if(*pgid
== ULONG_MAX
)
745 return -1; /* underflow / overflow */
747 tmpptr
= &raw_name
[i
];
748 *creation
= strtoull(tmpptr
, &endptr
, 10);
750 return -1; /* No digit */
751 if(*creation
== G_MAXUINT64
)
752 return -1; /* underflow / overflow */
756 cpu_num
= strtol(raw_name
+underscore_pos
+1, &endptr
, 10);
758 if(endptr
== raw_name
+underscore_pos
+1)
759 return -1; /* No digit */
760 if(cpu_num
== LONG_MIN
|| cpu_num
== LONG_MAX
)
761 return -1; /* underflow / overflow */
763 strncpy(char_name
, raw_name
, underscore_pos
);
764 char_name
[underscore_pos
] = '\0';
766 *name
= g_quark_from_string(char_name
);
775 GData
**ltt_trace_get_tracefiles_groups(LttTrace
*trace
)
777 return &trace
->tracefiles
;
781 void compute_tracefile_group(GQuark key_id
,
783 struct compute_tracefile_group_args
*args
)
788 for(i
=0; i
<group
->len
; i
++) {
789 tf
= &g_array_index (group
, LttTracefile
, i
);
791 args
->func(tf
, args
->func_args
);
796 void ltt_tracefile_group_destroy(gpointer data
)
798 GArray
*group
= (GArray
*)data
;
802 for(i
=0; i
<group
->len
; i
++) {
803 tf
= &g_array_index (group
, LttTracefile
, i
);
805 ltt_tracefile_close(tf
);
807 g_array_free(group
, TRUE
);
810 gboolean
ltt_tracefile_group_has_cpu_online(gpointer data
)
812 GArray
*group
= (GArray
*)data
;
816 for(i
=0; i
<group
->len
; i
++) {
817 tf
= &g_array_index (group
, LttTracefile
, i
);
825 /* Open each tracefile under a specific directory. Put them in a
826 * GData : permits to access them using their tracefile group pathname.
827 * i.e. access control/modules tracefile group by index :
830 * relative path is the path relative to the trace root
831 * root path is the full path
833 * A tracefile group is simply an array where all the per cpu tracefiles sit.
836 int open_tracefiles(LttTrace
*trace
, gchar
*root_path
, gchar
*relative_path
)
838 DIR *dir
= opendir(root_path
);
839 struct dirent
*entry
;
840 struct stat stat_buf
;
843 gchar path
[PATH_MAX
];
848 gchar rel_path
[PATH_MAX
];
857 strncpy(path
, root_path
, PATH_MAX
-1);
858 path_len
= strlen(path
);
859 path
[path_len
] = '/';
861 path_ptr
= path
+ path_len
;
863 strncpy(rel_path
, relative_path
, PATH_MAX
-1);
864 rel_path_len
= strlen(rel_path
);
865 rel_path
[rel_path_len
] = '/';
867 rel_path_ptr
= rel_path
+ rel_path_len
;
869 while((entry
= readdir(dir
)) != NULL
) {
871 if(entry
->d_name
[0] == '.') continue;
873 strncpy(path_ptr
, entry
->d_name
, PATH_MAX
- path_len
);
874 strncpy(rel_path_ptr
, entry
->d_name
, PATH_MAX
- rel_path_len
);
876 ret
= stat(path
, &stat_buf
);
882 g_debug("Tracefile file or directory : %s\n", path
);
884 // if(strcmp(rel_path, "/eventdefs") == 0) continue;
886 if(S_ISDIR(stat_buf
.st_mode
)) {
888 g_debug("Entering subdirectory...\n");
889 ret
= open_tracefiles(trace
, path
, rel_path
);
890 if(ret
< 0) continue;
891 } else if(S_ISREG(stat_buf
.st_mode
)) {
900 if(get_tracefile_name_number(rel_path
, &name
, &num
, &tid
, &pgid
, &creation
))
901 continue; /* invalid name */
903 g_debug("Opening file.\n");
904 if(ltt_tracefile_open(trace
, path
, &tmp_tf
)) {
905 g_info("Error opening tracefile %s", path
);
907 continue; /* error opening the tracefile : bad magic number ? */
910 g_debug("Tracefile name is %s and number is %u",
911 g_quark_to_string(name
), num
);
913 tmp_tf
.cpu_online
= 1;
914 tmp_tf
.cpu_num
= num
;
918 tmp_tf
.creation
= creation
;
919 if(tmp_tf
.name
== g_quark_from_string("/compact")
920 || tmp_tf
.name
== g_quark_from_string("/flight-compact"))
924 group
= g_datalist_id_get_data(&trace
->tracefiles
, name
);
926 /* Elements are automatically cleared when the array is allocated.
927 * It makes the cpu_online variable set to 0 : cpu offline, by default.
929 group
= g_array_sized_new (FALSE
, TRUE
, sizeof(LttTracefile
), 10);
930 g_datalist_id_set_data_full(&trace
->tracefiles
, name
,
931 group
, ltt_tracefile_group_destroy
);
934 /* Add the per cpu tracefile to the named group */
935 unsigned int old_len
= group
->len
;
937 group
= g_array_set_size(group
, num
+1);
938 g_array_index (group
, LttTracefile
, num
) = tmp_tf
;
939 g_array_index (group
, LttTracefile
, num
).event
.tracefile
=
940 &g_array_index (group
, LttTracefile
, num
);
949 /* ltt_get_facility_description
951 * Opens the file corresponding to the requested facility (identified by fac_id
954 * The name searched is : %trace root%/eventdefs/facname_checksum.xml
956 * Returns 0 on success, or 1 on failure.
959 static int ltt_get_facility_description(LttFacility
*f
,
961 LttTracefile
*fac_tf
)
963 char desc_file_name
[PATH_MAX
];
970 text
= g_quark_to_string(t
->pathname
);
971 textlen
= strlen(text
);
973 if(textlen
>= PATH_MAX
) goto name_error
;
974 strcpy(desc_file_name
, text
);
976 text
= "/eventdefs/";
977 textlen
+=strlen(text
);
978 if(textlen
>= PATH_MAX
) goto name_error
;
979 strcat(desc_file_name
, text
);
981 text
= g_quark_to_string(f
->name
);
982 fac_name_len
= strlen(text
);
983 textlen
+=fac_name_len
;
984 if(textlen
>= PATH_MAX
) goto name_error
;
985 strcat(desc_file_name
, text
);
987 /* arch specific facilities are named like this : name_arch */
988 if(fac_name_len
+1 < sizeof("_arch"))
991 if(!strcmp(&text
[fac_name_len
+1-sizeof("_arch")], "_arch"))
999 textlen
+=strlen(text
);
1000 if(textlen
>= PATH_MAX
) goto name_error
;
1001 strcat(desc_file_name
, text
);
1003 err
= snprintf(desc_file_name
+textlen
, PATH_MAX
-textlen
-1,
1005 if(err
< 0) goto name_error
;
1007 textlen
=strlen(desc_file_name
);
1012 switch(t
->arch_type
) {
1013 case LTT_ARCH_TYPE_I386
:
1016 case LTT_ARCH_TYPE_PPC
:
1019 case LTT_ARCH_TYPE_SH
:
1022 case LTT_ARCH_TYPE_S390
:
1025 case LTT_ARCH_TYPE_MIPS
:
1028 case LTT_ARCH_TYPE_ARM
:
1031 case LTT_ARCH_TYPE_PPC64
:
1034 case LTT_ARCH_TYPE_X86_64
:
1037 case LTT_ARCH_TYPE_C2
:
1040 case LTT_ARCH_TYPE_POWERPC
:
1044 g_error("Trace from unsupported architecture.");
1046 textlen
+=strlen(text
);
1047 if(textlen
>= PATH_MAX
) goto name_error
;
1048 strcat(desc_file_name
, text
);
1052 textlen
+=strlen(text
);
1053 if(textlen
>= PATH_MAX
) goto name_error
;
1054 strcat(desc_file_name
, text
);
1056 err
= ltt_facility_open(f
, t
, desc_file_name
);
1057 if(err
) goto facility_error
;
1066 static void ltt_fac_ids_destroy(gpointer data
)
1068 GArray
*fac_ids
= (GArray
*)data
;
1070 g_array_free(fac_ids
, TRUE
);
1074 /* Presumes the tracefile is already seeked at the beginning. It makes sense,
1075 * because it must be done just after the opening */
1076 int ltt_process_facility_tracefile(LttTracefile
*tf
)
1085 err
= ltt_tracefile_read_seek(tf
);
1086 if(err
== EPERM
) goto seek_error
;
1087 else if(err
== ERANGE
) break; /* End of tracefile */
1089 err
= ltt_tracefile_read_update_event(tf
);
1090 if(err
) goto update_error
;
1092 /* We are on a facility load/or facility unload/ or heartbeat event */
1094 * * facility 0 is hardcoded : this is the core facility. It will be shown
1095 * in the facility array though, and is shown as "loaded builtin" in the
1097 * It contains event :
1099 * 1 : facility unload
1100 * 2 : state dump facility load
1103 if(tf
->event
.event_id
>= MARKER_CORE_IDS
) {
1104 /* Should only contain core facility */
1105 g_warning("Error in processing facility file %s, "
1106 "should not contain event id %u.", g_quark_to_string(tf
->name
),
1107 tf
->event
.event_id
);
1109 goto event_id_error
;
1113 const char *marker_name
, *format
;
1115 guint8 int_size
, long_size
, pointer_size
, size_t_size
, alignment
;
1118 switch((enum marker_id
)tf
->event
.event_id
) {
1119 case MARKER_ID_SET_MARKER_ID
:
1120 marker_name
= pos
= tf
->event
.data
;
1121 g_debug("Doing MARKER_ID_SET_MARKER_ID of marker %s", marker_name
);
1122 pos
+= strlen(marker_name
) + 1;
1123 //remove genevent compatibility
1124 //pos += ltt_align((size_t)pos, tf->trace->arch_size, tf->alignment);
1125 pos
+= ltt_align((size_t)pos
, sizeof(uint16_t), tf
->alignment
);
1126 id
= ltt_get_uint16(LTT_GET_BO(tf
), pos
);
1127 g_debug("In MARKER_ID_SET_MARKER_ID of marker %s id %hu",
1129 pos
+= sizeof(guint16
);
1130 int_size
= *(guint8
*)pos
;
1131 pos
+= sizeof(guint8
);
1132 long_size
= *(guint8
*)pos
;
1133 pos
+= sizeof(guint8
);
1134 pointer_size
= *(guint8
*)pos
;
1135 pos
+= sizeof(guint8
);
1136 size_t_size
= *(guint8
*)pos
;
1137 pos
+= sizeof(guint8
);
1138 alignment
= *(guint8
*)pos
;
1139 pos
+= sizeof(guint8
);
1140 marker_id_event(tf
->trace
, g_quark_from_string(marker_name
),
1141 id
, int_size
, long_size
,
1142 pointer_size
, size_t_size
, alignment
);
1144 case MARKER_ID_SET_MARKER_FORMAT
:
1145 marker_name
= pos
= tf
->event
.data
;
1146 g_debug("Doing MARKER_ID_SET_MARKER_FORMAT of marker %s",
1148 pos
+= strlen(marker_name
) + 1;
1150 //pos += ltt_align((size_t)pos, tf->trace->arch_size, tf->alignment);
1152 pos
+= strlen(format
) + 1;
1154 //pos += ltt_align((size_t)pos, tf->trace->arch_size, tf->alignment);
1155 marker_format_event(tf
->trace
, g_quark_from_string(marker_name
),
1157 /* get information from dictionnary TODO */
1159 case MARKER_ID_HEARTBEAT_32
:
1160 case MARKER_ID_HEARTBEAT_64
:
1163 g_warning("Error in processing facility file %s, "
1164 "unknown event id %hhu.",
1165 g_quark_to_string(tf
->name
),
1166 tf
->event
.event_id
);
1168 goto event_id_error
;
1174 /* Error handling */
1178 g_warning("An error occured in facility tracefile parsing");
1183 LttTrace
*ltt_trace_open(const gchar
*pathname
)
1185 gchar abs_path
[PATH_MAX
];
1190 struct ltt_block_start_header
*header
;
1192 struct dirent
*entry
;
1193 guint control_found
= 0;
1194 struct stat stat_buf
;
1195 gchar path
[PATH_MAX
];
1197 t
= g_new(LttTrace
, 1);
1198 if(!t
) goto alloc_error
;
1200 get_absolute_pathname(pathname
, abs_path
);
1201 t
->pathname
= g_quark_from_string(abs_path
);
1203 g_datalist_init(&t
->tracefiles
);
1205 /* Test to see if it looks like a trace */
1206 dir
= opendir(abs_path
);
1211 while((entry
= readdir(dir
)) != NULL
) {
1212 strcpy(path
, abs_path
);
1214 strcat(path
, entry
->d_name
);
1215 ret
= stat(path
, &stat_buf
);
1220 if(S_ISDIR(stat_buf
.st_mode
)) {
1221 if(strcmp(entry
->d_name
, "control") == 0) {
1228 if(!control_found
) goto find_error
;
1230 /* Open all the tracefiles */
1231 if(open_tracefiles(t
, abs_path
, "")) {
1232 g_warning("Error opening tracefile %s", abs_path
);
1236 /* Parse each trace control/facilitiesN files : get runtime fac. info */
1237 group
= g_datalist_id_get_data(&t
->tracefiles
, LTT_TRACEFILE_NAME_FACILITIES
);
1239 g_error("Trace %s has no facility tracefile", abs_path
);
1241 goto facilities_error
;
1244 /* Get the trace information for the control/facility 0 tracefile */
1245 g_assert(group
->len
> 0);
1246 tf
= &g_array_index (group
, LttTracefile
, 0);
1247 header
= (struct ltt_block_start_header
*)tf
->buffer
.head
;
1248 g_assert(parse_trace_header(header
->trace
,
1251 t
->num_cpu
= group
->len
;
1253 ret
= allocate_marker_data(t
);
1255 g_error("Error in allocating marker data");
1257 for(i
=0; i
<group
->len
; i
++) {
1258 tf
= &g_array_index (group
, LttTracefile
, i
);
1259 if(ltt_process_facility_tracefile(tf
))
1260 goto facilities_error
;
1265 /* Error handling */
1267 destroy_marker_data(t
);
1269 g_datalist_clear(&t
->tracefiles
);
1277 GQuark
ltt_trace_name(const LttTrace
*t
)
1283 /******************************************************************************
1284 * When we copy a trace, we want all the opening actions to happen again :
1285 * the trace will be reopened and totally independant from the original.
1286 * That's why we call ltt_trace_open.
1287 *****************************************************************************/
1288 LttTrace
*ltt_trace_copy(LttTrace
*self
)
1290 return ltt_trace_open(g_quark_to_string(self
->pathname
));
1293 void ltt_trace_close(LttTrace
*t
)
1295 g_datalist_clear(&t
->tracefiles
);
1300 /*****************************************************************************
1301 *Get the system description of the trace
1302 ****************************************************************************/
1304 LttFacility
*ltt_trace_facility_by_id(LttTrace
*t
, guint8 id
)
1306 g_assert(id
< t
->facilities_by_num
->len
);
1307 return &g_array_index(t
->facilities_by_num
, LttFacility
, id
);
1310 /* ltt_trace_facility_get_by_name
1312 * Returns the GArray of facility indexes. All the fac_ids that matches the
1313 * requested facility name.
1315 * If name is not found, returns NULL.
1317 GArray
*ltt_trace_facility_get_by_name(LttTrace
*t
, GQuark name
)
1319 return g_datalist_id_get_data(&t
->facilities_by_name
, name
);
1323 /*****************************************************************************
1324 * Functions to discover all the event types in the trace
1325 ****************************************************************************/
1328 unsigned ltt_trace_eventtype_number(LttTrace
*t
)
1332 unsigned int num
= t
->facility_number
;
1336 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, i
);
1337 count
+= f
->event_number
;
1344 //use an iteration on all the trace facilities, and inside iteration on all the
1345 //event types in each facilities instead.
1346 LttEventType
*ltt_trace_eventtype_get(LttTrace
*t
, unsigned evId
)
1348 LttEventType
*event_type
;
1351 f
= ltt_trace_facility_by_id(t
,evId
);
1353 if(unlikely(!f
)) event_type
= NULL
;
1354 else event_type
= f
->events
[evId
- f
->base_id
];
1361 /*****************************************************************************
1362 * ltt_trace_find_tracefile
1364 * Find a tracefile by name and index in the group.
1366 * Returns a pointer to the tracefiles, else NULL.
1367 ****************************************************************************/
1369 LttTracefile
*ltt_trace_find_tracefile(LttTrace
*t
, const gchar
*name
)
1374 /*****************************************************************************
1375 * Get the start time and end time of the trace
1376 ****************************************************************************/
1378 void ltt_tracefile_time_span_get(LttTracefile
*tf
,
1379 LttTime
*start
, LttTime
*end
)
1383 err
= map_block(tf
, 0);
1385 g_error("Can not map block");
1386 *start
= ltt_time_infinite
;
1388 *start
= tf
->buffer
.begin
.timestamp
;
1390 err
= map_block(tf
, tf
->num_blocks
- 1); /* Last block */
1392 g_error("Can not map block");
1393 *end
= ltt_time_zero
;
1395 *end
= tf
->buffer
.end
.timestamp
;
1398 struct tracefile_time_span_get_args
{
1404 void group_time_span_get(GQuark name
, gpointer data
, gpointer user_data
)
1406 struct tracefile_time_span_get_args
*args
=
1407 (struct tracefile_time_span_get_args
*)user_data
;
1409 GArray
*group
= (GArray
*)data
;
1415 for(i
=0; i
<group
->len
; i
++) {
1416 tf
= &g_array_index (group
, LttTracefile
, i
);
1417 if(tf
->cpu_online
) {
1418 ltt_tracefile_time_span_get(tf
, &tmp_start
, &tmp_end
);
1419 if(ltt_time_compare(*args
->start
, tmp_start
)>0) *args
->start
= tmp_start
;
1420 if(ltt_time_compare(*args
->end
, tmp_end
)<0) *args
->end
= tmp_end
;
1425 void ltt_trace_time_span_get(LttTrace
*t
, LttTime
*start
, LttTime
*end
)
1427 LttTime min_start
= ltt_time_infinite
;
1428 LttTime max_end
= ltt_time_zero
;
1429 struct tracefile_time_span_get_args args
= { t
, &min_start
, &max_end
};
1431 g_datalist_foreach(&t
->tracefiles
, &group_time_span_get
, &args
);
1433 if(start
!= NULL
) *start
= min_start
;
1434 if(end
!= NULL
) *end
= max_end
;
1439 /*****************************************************************************
1440 *Get the name of a tracefile
1441 ****************************************************************************/
1443 GQuark
ltt_tracefile_name(const LttTracefile
*tf
)
1448 GQuark
ltt_tracefile_long_name(const LttTracefile
*tf
)
1450 return tf
->long_name
;
1455 guint
ltt_tracefile_cpu(LttTracefile
*tf
)
1460 guint
ltt_tracefile_tid(LttTracefile
*tf
)
1465 guint
ltt_tracefile_pgid(LttTracefile
*tf
)
1470 guint64
ltt_tracefile_creation(LttTracefile
*tf
)
1472 return tf
->creation
;
1474 /*****************************************************************************
1475 * Get the number of blocks in the tracefile
1476 ****************************************************************************/
1478 guint
ltt_tracefile_block_number(LttTracefile
*tf
)
1480 return tf
->num_blocks
;
1484 /* Seek to the first event in a tracefile that has a time equal or greater than
1485 * the time passed in parameter.
1487 * If the time parameter is outside the tracefile time span, seek to the first
1488 * event or if after, return ERANGE.
1490 * If the time parameter is before the first event, we have to seek specially to
1493 * If the time is after the end of the trace, return ERANGE.
1495 * Do a binary search to find the right block, then a sequential search in the
1496 * block to find the event.
1498 * In the special case where the time requested fits inside a block that has no
1499 * event corresponding to the requested time, the first event of the next block
1502 * IMPORTANT NOTE : // FIXME everywhere...
1504 * You MUST NOT do a ltt_tracefile_read right after a ltt_tracefile_seek_time :
1505 * you will jump over an event if you do.
1507 * Return value : 0 : no error, the tf->event can be used
1508 * ERANGE : time if after the last event of the trace
1509 * otherwise : this is an error.
1513 int ltt_tracefile_seek_time(LttTracefile
*tf
, LttTime time
)
1517 unsigned int block_num
, high
, low
;
1519 /* seek at the beginning of trace */
1520 err
= map_block(tf
, 0); /* First block */
1522 g_error("Can not map block");
1526 /* If the time is lower or equal the beginning of the trace,
1527 * go to the first event. */
1528 if(ltt_time_compare(time
, tf
->buffer
.begin
.timestamp
) <= 0) {
1529 ret
= ltt_tracefile_read(tf
);
1530 if(ret
== ERANGE
) goto range
;
1531 else if (ret
) goto fail
;
1532 goto found
; /* There is either no event in the trace or the event points
1533 to the first event in the trace */
1536 err
= map_block(tf
, tf
->num_blocks
- 1); /* Last block */
1538 g_error("Can not map block");
1542 /* If the time is after the end of the trace, return ERANGE. */
1543 if(ltt_time_compare(time
, tf
->buffer
.end
.timestamp
) > 0) {
1547 /* Binary search the block */
1548 high
= tf
->num_blocks
- 1;
1552 block_num
= ((high
-low
) / 2) + low
;
1554 err
= map_block(tf
, block_num
);
1556 g_error("Can not map block");
1560 /* We cannot divide anymore : this is what would happen if the time
1561 * requested was exactly between two consecutive buffers'end and start
1562 * timestamps. This is also what would happend if we didn't deal with out
1563 * of span cases prior in this function. */
1564 /* The event is right in the buffer!
1565 * (or in the next buffer first event) */
1567 ret
= ltt_tracefile_read(tf
);
1568 if(ret
== ERANGE
) goto range
; /* ERANGE or EPERM */
1569 else if(ret
) goto fail
;
1571 if(ltt_time_compare(time
, tf
->event
.event_time
) <= 0)
1575 } else if(ltt_time_compare(time
, tf
->buffer
.begin
.timestamp
) < 0) {
1576 /* go to lower part */
1577 high
= block_num
- 1;
1578 } else if(ltt_time_compare(time
, tf
->buffer
.end
.timestamp
) > 0) {
1579 /* go to higher part */
1580 low
= block_num
+ 1;
1581 } else {/* The event is right in the buffer!
1582 (or in the next buffer first event) */
1584 ret
= ltt_tracefile_read(tf
);
1585 if(ret
== ERANGE
) goto range
; /* ERANGE or EPERM */
1586 else if(ret
) goto fail
;
1588 if(ltt_time_compare(time
, tf
->event
.event_time
) <= 0)
1600 /* Error handling */
1602 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1603 g_quark_to_string(tf
->name
));
1608 int ltt_tracefile_seek_position(LttTracefile
*tf
, const LttEventPosition
*ep
) {
1612 if(ep
->tracefile
!= tf
) {
1616 err
= map_block(tf
, ep
->block
);
1618 g_error("Can not map block");
1622 tf
->event
.offset
= ep
->offset
;
1624 /* Put back the event real tsc */
1625 tf
->event
.tsc
= ep
->tsc
;
1626 tf
->buffer
.tsc
= ep
->tsc
;
1628 err
= ltt_tracefile_read_update_event(tf
);
1630 err
= ltt_tracefile_read_op(tf
);
1636 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1637 g_quark_to_string(tf
->name
));
1641 LttTime
ltt_interpolate_time_from_tsc(LttTracefile
*tf
, guint64 tsc
)
1645 if(tsc
> tf
->trace
->start_tsc
) {
1646 time
= ltt_time_from_uint64(
1647 (double)(tsc
- tf
->trace
->start_tsc
)
1648 * (1000000000.0 / tf
->trace
->freq_scale
)
1649 / (double)tf
->trace
->start_freq
);
1650 time
= ltt_time_add(tf
->trace
->start_time_from_tsc
, time
);
1652 time
= ltt_time_from_uint64(
1653 (double)(tf
->trace
->start_tsc
- tsc
)
1654 * (1000000000.0 / tf
->trace
->freq_scale
)
1655 / (double)tf
->trace
->start_freq
);
1656 time
= ltt_time_sub(tf
->trace
->start_time_from_tsc
, time
);
1661 /* Calculate the real event time based on the buffer boundaries */
1662 LttTime
ltt_interpolate_time(LttTracefile
*tf
, LttEvent
*event
)
1664 return ltt_interpolate_time_from_tsc(tf
, tf
->buffer
.tsc
);
1668 /* Get the current event of the tracefile : valid until the next read */
1669 LttEvent
*ltt_tracefile_get_event(LttTracefile
*tf
)
1676 /*****************************************************************************
1678 * ltt_tracefile_read : Read the next event in the tracefile
1683 * Returns 0 if an event can be used in tf->event.
1684 * Returns ERANGE on end of trace. The event in tf->event still can be used
1685 * (if the last block was not empty).
1686 * Returns EPERM on error.
1688 * This function does make the tracefile event structure point to the event
1689 * currently pointed to by the tf->event.
1691 * Note : you must call a ltt_tracefile_seek to the beginning of the trace to
1692 * reinitialize it after an error if you want results to be coherent.
1693 * It would be the case if a end of trace last buffer has no event : the end
1694 * of trace wouldn't be returned, but an error.
1695 * We make the assumption there is at least one event per buffer.
1696 ****************************************************************************/
1698 int ltt_tracefile_read(LttTracefile
*tf
)
1702 err
= ltt_tracefile_read_seek(tf
);
1704 err
= ltt_tracefile_read_update_event(tf
);
1706 err
= ltt_tracefile_read_op(tf
);
1712 int ltt_tracefile_read_seek(LttTracefile
*tf
)
1716 /* Get next buffer until we finally have an event, or end of trace */
1718 err
= ltt_seek_next_event(tf
);
1719 if(unlikely(err
== ENOPROTOOPT
)) {
1723 /* Are we at the end of the buffer ? */
1725 if(unlikely(tf
->buffer
.index
== tf
->num_blocks
-1)){ /* end of trace ? */
1728 /* get next block */
1729 err
= map_block(tf
, tf
->buffer
.index
+ 1);
1731 g_error("Can not map block");
1735 } else break; /* We found an event ! */
1742 /* do specific operation on events */
1743 int ltt_tracefile_read_op(LttTracefile
*tf
)
1749 /* do event specific operation */
1751 /* do something if its an heartbeat event : increment the heartbeat count */
1752 //if(event->facility_id == LTT_FACILITY_CORE)
1753 // if(event->event_id == LTT_EVENT_HEARTBEAT)
1754 // tf->cur_heart_beat_number++;
1759 static void print_debug_event_header(LttEvent
*ev
, void *start_pos
, void *end_pos
)
1761 unsigned int offset
= 0;
1764 g_printf("Event header (tracefile %s offset %llx):\n",
1765 g_quark_to_string(ev
->tracefile
->long_name
),
1766 ((uint64_t)ev
->tracefile
->buffer
.index
* ev
->tracefile
->buf_size
)
1767 + (long)start_pos
- (long)ev
->tracefile
->buffer
.head
);
1769 while (offset
< (long)end_pos
- (long)start_pos
) {
1770 g_printf("%8lx", (long)start_pos
- (long)ev
->tracefile
->buffer
.head
+ offset
);
1773 for (i
= 0; i
< 4 ; i
++) {
1774 for (j
= 0; j
< 4; j
++) {
1775 if (offset
+ ((i
* 4) + j
) <
1776 (long)end_pos
- (long)start_pos
)
1778 ((char*)start_pos
)[offset
+ ((i
* 4) + j
)]);
1792 /* same as ltt_tracefile_read, but does not seek to the next event nor call
1793 * event specific operation. */
1794 int ltt_tracefile_read_update_event(LttTracefile
*tf
)
1801 pos
= tf
->buffer
.head
+ event
->offset
;
1803 /* Read event header */
1805 /* Align the head */
1807 pos
+= ltt_align((size_t)pos
, tf
->trace
->arch_size
, tf
->alignment
);
1809 g_assert(tf
->has_heartbeat
);
1810 pos
+= ltt_align((size_t)pos
, sizeof(uint32_t), tf
->alignment
);
1814 if(tf
->has_heartbeat
) {
1815 event
->timestamp
= ltt_get_uint32(LTT_GET_BO(tf
),
1818 /* 32 bits -> 64 bits tsc */
1819 /* note : still works for seek and non seek cases. */
1820 if(event
->timestamp
< (0xFFFFFFFFULL
&tf
->buffer
.tsc
)) {
1821 tf
->buffer
.tsc
= ((tf
->buffer
.tsc
&0xFFFFFFFF00000000ULL
)
1823 | (guint64
)event
->timestamp
;
1824 event
->tsc
= tf
->buffer
.tsc
;
1827 tf
->buffer
.tsc
= (tf
->buffer
.tsc
&0xFFFFFFFF00000000ULL
)
1828 | (guint64
)event
->timestamp
;
1829 event
->tsc
= tf
->buffer
.tsc
;
1830 event
->compact_data
= 0;
1833 /* Compact header */
1834 /* We keep the LSB of the previous timestamp, to make sure
1835 * we never go back */
1836 event
->event_id
= event
->timestamp
>> tf
->tscbits
;
1837 event
->event_id
= event
->event_id
& ((1 << tf
->compact_event_bits
) - 1);
1838 event
->compact_data
= event
->timestamp
>>
1839 (tf
->compact_event_bits
+ tf
->tscbits
);
1840 //printf("tsc bits %u, ev bits %u init data %u\n",
1841 // tf->tscbits, tf->trace->compact_event_bits, event->compact_data);
1842 /* Put the compact data back in original endianness */
1843 event
->compact_data
= ltt_get_uint32(LTT_GET_BO(tf
), &event
->compact_data
);
1844 event
->event_size
= 0xFFFF;
1845 //printf("Found compact event %d\n", event->event_id);
1846 //printf("Compact data %d\n", event->compact_data);
1847 event
->timestamp
= event
->timestamp
<< tf
->tsc_lsb_truncate
;
1848 event
->timestamp
= event
->timestamp
& tf
->tsc_mask
;
1849 //printf("timestamp 0x%lX\n", event->timestamp);
1850 //printf("mask 0x%llX\n", tf->tsc_mask);
1851 //printf("mask_next 0x%llX\n", tf->tsc_mask_next_bit);
1852 //printf("previous tsc 0x%llX\n", tf->buffer.tsc);
1853 //printf("previous tsc&mask 0x%llX\n", tf->tsc_mask&tf->buffer.tsc);
1854 //printf("previous tsc&(~mask) 0x%llX\n", tf->buffer.tsc&(~tf->tsc_mask));
1855 if(event
->timestamp
< (tf
->tsc_mask
&tf
->buffer
.tsc
)) {
1857 tf
->buffer
.tsc
= ((tf
->buffer
.tsc
&(~tf
->tsc_mask
))
1858 + tf
->tsc_mask_next_bit
)
1859 | (guint64
)event
->timestamp
;
1860 event
->tsc
= tf
->buffer
.tsc
;
1862 //printf("no wrap\n");
1864 tf
->buffer
.tsc
= (tf
->buffer
.tsc
&(~tf
->tsc_mask
))
1865 | (guint64
)event
->timestamp
;
1866 event
->tsc
= tf
->buffer
.tsc
;
1868 //printf("current tsc 0x%llX\n", tf->buffer.tsc);
1870 pos
+= sizeof(guint32
);
1872 event
->tsc
= ltt_get_uint64(LTT_GET_BO(tf
), pos
);
1873 tf
->buffer
.tsc
= event
->tsc
;
1874 event
->compact_data
= 0;
1875 pos
+= sizeof(guint64
);
1877 event
->event_time
= ltt_interpolate_time(tf
, event
);
1880 event
->event_id
= ltt_get_uint16(LTT_GET_BO(tf
), pos
);
1881 pos
+= sizeof(guint16
);
1883 event
->event_size
= ltt_get_uint16(LTT_GET_BO(tf
), pos
);
1884 pos
+= sizeof(guint16
);
1887 event
->event_size
= 0xFFFF;
1891 print_debug_event_header(event
, pos_aligned
, pos
);
1893 /* Align the head */
1895 pos
+= ltt_align((size_t)pos
, tf
->trace
->arch_size
, tf
->alignment
);
1899 /* get the data size and update the event fields with the current
1900 * information. Also update the time if a heartbeat_full event is found. */
1901 ltt_update_event_size(tf
);
1907 /****************************************************************************
1909 * map_block : map a block from the file
1911 * lttdes : ltt trace file
1912 * whichBlock : the block which will be read
1915 * EINVAL : lseek fail
1916 * EIO : can not read from the file
1917 ****************************************************************************/
1919 gint
map_block(LttTracefile
* tf
, guint block_num
)
1921 int page_size
= getpagesize();
1922 struct ltt_block_start_header
*header
;
1924 g_assert(block_num
< tf
->num_blocks
);
1926 if(tf
->buffer
.head
!= NULL
) {
1927 if(munmap(tf
->buffer
.head
, PAGE_ALIGN(tf
->buf_size
))) {
1928 g_warning("unmap size : %u\n",
1929 PAGE_ALIGN(tf
->buf_size
));
1930 perror("munmap error");
1936 /* Multiple of pages aligned head */
1937 tf
->buffer
.head
= mmap(0,
1938 PAGE_ALIGN(tf
->buf_size
),
1939 PROT_READ
, MAP_PRIVATE
, tf
->fd
,
1940 PAGE_ALIGN((off_t
)tf
->buf_size
* (off_t
)block_num
));
1942 if(tf
->buffer
.head
== MAP_FAILED
) {
1943 perror("Error in allocating memory for buffer of tracefile");
1947 g_assert( ( (gulong
)tf
->buffer
.head
&(8-1) ) == 0); // make sure it's aligned.
1950 tf
->buffer
.index
= block_num
;
1952 header
= (struct ltt_block_start_header
*)tf
->buffer
.head
;
1955 tf
->buffer
.begin
.timestamp
= ltt_time_add(
1956 ltt_time_from_uint64(
1957 ltt_get_uint64(LTT_GET_BO(tf
),
1958 &header
->begin
.timestamp
)
1959 - tf
->trace
->start_monotonic
),
1960 tf
->trace
->start_time
);
1962 //g_debug("block %u begin : %lu.%lu", block_num,
1963 // tf->buffer.begin.timestamp.tv_sec, tf->buffer.begin.timestamp.tv_nsec);
1964 tf
->buffer
.begin
.cycle_count
= ltt_get_uint64(LTT_GET_BO(tf
),
1965 &header
->begin
.cycle_count
);
1966 tf
->buffer
.begin
.freq
= ltt_get_uint64(LTT_GET_BO(tf
),
1967 &header
->begin
.freq
);
1968 if(tf
->buffer
.begin
.freq
== 0)
1969 tf
->buffer
.begin
.freq
= tf
->trace
->start_freq
;
1971 tf
->buffer
.begin
.timestamp
= ltt_interpolate_time_from_tsc(tf
,
1972 tf
->buffer
.begin
.cycle_count
);
1975 ltt_time_from_uint64(
1976 (double)(tf
->buffer
.begin
.cycle_count
1977 - tf
->trace
->start_tsc
) * 1000000.0
1978 / (double)tf
->trace
->start_freq
),
1979 tf
->trace
->start_time_from_tsc
);
1983 tf
->buffer
.end
.timestamp
= ltt_time_add(
1984 ltt_time_from_uint64(
1985 ltt_get_uint64(LTT_GET_BO(tf
),
1986 &header
->end
.timestamp
)
1987 - tf
->trace
->start_monotonic
),
1988 tf
->trace
->start_time
);
1990 //g_debug("block %u end : %lu.%lu", block_num,
1991 // tf->buffer.end.timestamp.tv_sec, tf->buffer.end.timestamp.tv_nsec);
1992 tf
->buffer
.end
.cycle_count
= ltt_get_uint64(LTT_GET_BO(tf
),
1993 &header
->end
.cycle_count
);
1994 tf
->buffer
.end
.freq
= ltt_get_uint64(LTT_GET_BO(tf
),
1996 if(tf
->buffer
.end
.freq
== 0)
1997 tf
->buffer
.end
.freq
= tf
->trace
->start_freq
;
1999 tf
->buffer
.lost_size
= ltt_get_uint32(LTT_GET_BO(tf
),
2000 &header
->lost_size
);
2001 tf
->buffer
.end
.timestamp
= ltt_interpolate_time_from_tsc(tf
,
2002 tf
->buffer
.end
.cycle_count
);
2005 ltt_time_from_uint64(
2006 (double)(tf
->buffer
.end
.cycle_count
2007 - tf
->trace
->start_tsc
) * 1000000.0
2008 / (double)tf
->trace
->start_freq
),
2009 tf
->trace
->start_time_from_tsc
);
2011 tf
->buffer
.tsc
= tf
->buffer
.begin
.cycle_count
;
2012 tf
->event
.tsc
= tf
->buffer
.tsc
;
2013 tf
->buffer
.freq
= tf
->buffer
.begin
.freq
;
2016 * eventually support variable buffer size : will need a partial pre-read of
2017 * the headers to create an index when we open the trace... eventually. */
2018 g_assert(tf
->buf_size
== ltt_get_uint32(LTT_GET_BO(tf
),
2019 &header
->buf_size
));
2021 /* Now that the buffer is mapped, calculate the time interpolation for the
2024 // tf->buffer.nsecs_per_cycle = calc_nsecs_per_cycle(tf);
2025 //tf->buffer.cyc2ns_scale = calc_nsecs_per_cycle(tf);
2027 /* Make the current event point to the beginning of the buffer :
2028 * it means that the event read must get the first event. */
2029 tf
->event
.tracefile
= tf
;
2030 tf
->event
.block
= block_num
;
2031 tf
->event
.offset
= 0;
2040 static void print_debug_event_data(LttEvent
*ev
)
2042 unsigned int offset
= 0;
2045 if (!max(ev
->event_size
, ev
->data_size
))
2048 g_printf("Event data (tracefile %s offset %llx):\n",
2049 g_quark_to_string(ev
->tracefile
->long_name
),
2050 ((uint64_t)ev
->tracefile
->buffer
.index
* ev
->tracefile
->buf_size
)
2051 + (long)ev
->data
- (long)ev
->tracefile
->buffer
.head
);
2053 while (offset
< max(ev
->event_size
, ev
->data_size
)) {
2054 g_printf("%8lx", (long)ev
->data
+ offset
2055 - (long)ev
->tracefile
->buffer
.head
);
2058 for (i
= 0; i
< 4 ; i
++) {
2059 for (j
= 0; j
< 4; j
++) {
2060 if (offset
+ ((i
* 4) + j
) < max(ev
->event_size
, ev
->data_size
))
2061 g_printf("%02hhX", ((char*)ev
->data
)[offset
+ ((i
* 4) + j
)]);
2072 for (i
= 0; i
< 4; i
++) {
2073 for (j
= 0; j
< 4; j
++) {
2074 if (offset
+ ((i
* 4) + j
) < max(ev
->event_size
, ev
->data_size
)) {
2075 if (isprint(((char*)ev
->data
)[offset
+ ((i
* 4) + j
)]))
2076 g_printf("%c", ((char*)ev
->data
)[offset
+ ((i
* 4) + j
)]);
2088 /* It will update the fields offsets too */
2089 void ltt_update_event_size(LttTracefile
*tf
)
2093 struct marker_info
*info
;
2095 switch((enum marker_id
)tf
->event
.event_id
) {
2096 case MARKER_ID_SET_MARKER_ID
:
2097 size
= strlen((char*)tf
->event
.data
) + 1;
2098 g_debug("marker %s id set", (char*)tf
->event
.data
);
2099 size
+= ltt_align(size
, sizeof(guint16
), tf
->alignment
);
2100 size
+= sizeof(guint16
);
2101 size
+= sizeof(guint8
);
2102 size
+= sizeof(guint8
);
2103 size
+= sizeof(guint8
);
2104 size
+= sizeof(guint8
);
2105 size
+= sizeof(guint8
);
2107 case MARKER_ID_SET_MARKER_FORMAT
:
2108 g_debug("marker %s format set", (char*)tf
->event
.data
);
2109 size
= strlen((char*)tf
->event
.data
) + 1;
2110 size
+= strlen((char*)tf
->event
.data
+ size
) + 1;
2112 case MARKER_ID_HEARTBEAT_32
:
2113 g_debug("Update Event heartbeat 32 bits");
2115 case MARKER_ID_HEARTBEAT_64
:
2116 g_debug("Update Event heartbeat 64 bits");
2117 tscdata
= (char*)(tf
->event
.data
);
2118 tf
->event
.tsc
= ltt_get_uint64(LTT_GET_BO(tf
), tscdata
);
2119 tf
->buffer
.tsc
= tf
->event
.tsc
;
2120 tf
->event
.event_time
= ltt_interpolate_time(tf
, &tf
->event
);
2121 size
= ltt_align(size
, sizeof(guint64
), tf
->alignment
);
2122 size
+= sizeof(guint64
);
2126 info
= marker_get_info_from_id(tf
->trace
, tf
->event
.event_id
);
2128 if (tf
->event
.event_id
>= MARKER_CORE_IDS
)
2129 g_assert(info
!= NULL
);
2131 /* Do not update field offsets of core markers when initially reading the
2132 * facility tracefile when the infos about these markers do not exist yet.
2134 if (likely(info
&& info
->fields
)) {
2135 if (info
->size
!= -1)
2138 size
= marker_update_fields_offsets(marker_get_info_from_id(tf
->trace
,
2139 tf
->event
.event_id
), tf
->event
.data
);
2142 tf
->event
.data_size
= size
;
2144 /* Check consistency between kernel and LTTV structure sizes */
2145 if(tf
->event
.event_size
== 0xFFFF) {
2146 /* Event size too big to fit in the event size field */
2147 tf
->event
.event_size
= tf
->event
.data_size
;
2151 print_debug_event_data(&tf
->event
);
2153 /* Having a marker load or marker format event out of the facilities
2154 * tracefiles is a serious bug. */
2155 switch((enum marker_id
)tf
->event
.event_id
) {
2156 case MARKER_ID_SET_MARKER_ID
:
2157 case MARKER_ID_SET_MARKER_FORMAT
:
2158 if (tf
->name
!= g_quark_from_string("/control/facilities"))
2159 g_error("Trace inconsistency : metadata event found in data "
2160 "tracefile %s", g_quark_to_string(tf
->long_name
));
2163 if (tf
->event
.data_size
!= tf
->event
.event_size
) {
2164 struct marker_info
*info
= marker_get_info_from_id(tf
->trace
,
2165 tf
->event
.event_id
);
2166 g_error("Kernel/LTTV event size differs for event %s: kernel %u, LTTV %u",
2167 g_quark_to_string(info
->name
),
2168 tf
->event
.event_size
, tf
->event
.data_size
);
2173 LttEventType
*event_type
=
2174 ltt_facility_eventtype_get(f
, tf
->event
.event_id
);
2177 g_warning("Unknown event id %hhu in facility %s in tracefile %s",
2179 g_quark_to_string(f
->name
),
2180 g_quark_to_string(tf
->name
));
2181 goto event_type_error
;
2184 /* Compute the dynamic offsets */
2185 compute_offsets(tf
, f
, event_type
, &size
, tf
->event
.data
);
2187 //g_debug("Event root field : f.e %hhu.%hhu size %zd",
2188 // tf->event.facility_id,
2189 // tf->event.event_id, size);
2192 tf
->event
.data_size
= size
;
2194 /* Check consistency between kernel and LTTV structure sizes */
2195 if(tf
->event
.event_size
== 0xFFFF) {
2196 /* Event size too big to fit in the event size field */
2197 tf
->event
.event_size
= tf
->event
.data_size
;
2199 if (tf
->event
.data_size
!= tf
->event
.event_size
) {
2200 g_error("Kernel/LTTV event size differs for event %s.%s: kernel %u, LTTV %u",
2201 g_quark_to_string(f
->name
), g_quark_to_string(event_type
->name
),
2202 tf
->event
.event_size
, tf
->event
.data_size
);
2205 //g_assert(tf->event.data_size == tf->event.event_size);
2211 if(tf
->event
.event_size
== 0xFFFF) {
2212 g_error("Cannot jump over an unknown event bigger than 0xFFFE bytes");
2214 /* The facility is unknown : use the kernel information about this event
2215 * to jump over it. */
2216 tf
->event
.data_size
= tf
->event
.event_size
;
2221 /* Take the tf current event offset and use the event facility id and event id
2222 * to figure out where is the next event offset.
2224 * This is an internal function not aiming at being used elsewhere : it will
2225 * not jump over the current block limits. Please consider using
2226 * ltt_tracefile_read to do this.
2228 * Returns 0 on success
2229 * ERANGE if we are at the end of the buffer.
2230 * ENOPROTOOPT if an error occured when getting the current event size.
2232 int ltt_seek_next_event(LttTracefile
*tf
)
2237 /* seek over the buffer header if we are at the buffer start */
2238 if(tf
->event
.offset
== 0) {
2239 tf
->event
.offset
+= tf
->buffer_header_size
;
2241 if(tf
->event
.offset
== tf
->buf_size
- tf
->buffer
.lost_size
) {
2248 pos
= tf
->event
.data
;
2250 if(tf
->event
.data_size
< 0) goto error
;
2252 pos
+= (size_t)tf
->event
.data_size
;
2254 tf
->event
.offset
= pos
- tf
->buffer
.head
;
2256 if(tf
->event
.offset
== tf
->buf_size
- tf
->buffer
.lost_size
) {
2260 g_assert(tf
->event
.offset
< tf
->buf_size
- tf
->buffer
.lost_size
);
2266 g_error("Error in ltt_seek_next_event for tracefile %s",
2267 g_quark_to_string(tf
->name
));
2272 /*****************************************************************************
2274 * calc_nsecs_per_cycle : calculate nsecs per cycle for current block
2276 * 1.0 / (freq(khz) *1000) * 1000000000
2279 ****************************************************************************/
2280 /* from timer_tsc.c */
2281 #define CYC2NS_SCALE_FACTOR 10
2282 static guint32
calc_nsecs_per_cycle(LttTracefile
* tf
)
2284 //return 1e6 / (double)tf->buffer.freq;
2285 guint32 cpu_mhz
= tf
->buffer
.freq
/ 1000;
2286 guint32 cyc2ns_scale
= (1000 << CYC2NS_SCALE_FACTOR
)/cpu_mhz
;
2288 return cyc2ns_scale
;
2289 // return 1e6 / (double)tf->buffer.freq;
2292 static guint64
cycles_2_ns(LttTracefile
*tf
, guint64 cycles
)
2294 return (cycles
* tf
->buffer
.cyc2ns_scale
) >> CYC2NS_SCALE_FACTOR
;
2299 void setFieldsOffset(LttTracefile
*tf
, LttEventType
*evT
,void *evD
)
2301 LttField
* rootFld
= evT
->root_field
;
2302 // rootFld->base_address = evD;
2305 rootFld
->field_size
= getFieldtypeSize(tf
, evT
->facility
,
2306 evT
, 0,0,rootFld
, evD
);
2310 /*****************************************************************************
2312 * set_fields_offsets : set the precomputable offset of the fields
2314 * tracefile : opened trace file
2315 * event_type : the event type
2316 ****************************************************************************/
2318 void set_fields_offsets(LttTracefile
*tf
, LttEventType
*event_type
)
2320 LttField
*field
= event_type
->root_field
;
2321 enum field_status fixed_root
= FIELD_FIXED
, fixed_parent
= FIELD_FIXED
;
2324 preset_field_type_size(tf
, event_type
, 0, 0,
2325 &fixed_root
, &fixed_parent
,
2332 /*****************************************************************************
2334 * get_alignment : Get the alignment needed for a field.
2338 * returns : The size on which it must be aligned.
2340 ****************************************************************************/
2342 off_t
get_alignment(LttField
*field
)
2344 LttType
*type
= &field
->field_type
;
2346 switch(type
->type_class
) {
2348 case LTT_UINT_FIXED
:
2363 /* Align offset on type size */
2364 g_assert(field
->field_size
!= 0);
2365 return field
->field_size
;
2371 g_assert(type
->fields
->len
== 1);
2373 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
2374 return get_alignment(child
);
2378 g_assert(type
->fields
->len
== 2);
2381 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
2383 localign
= max(localign
, get_alignment(child
));
2385 child
= &g_array_index(type
->fields
, LttField
, 1);
2386 localign
= max(localign
, get_alignment(child
));
2397 for(i
=0; i
<type
->fields
->len
; i
++) {
2398 LttField
*child
= &g_array_index(type
->fields
, LttField
, i
);
2399 localign
= max(localign
, get_alignment(child
));
2406 g_error("get_alignment : unknown type");
2413 /*****************************************************************************
2415 * field_compute_static_size : Determine the size of fields known by their
2416 * sole definition. Unions, arrays and struct sizes might be known, but
2417 * the parser does not give that information.
2422 ****************************************************************************/
2424 void field_compute_static_size(LttFacility
*fac
, LttField
*field
)
2426 LttType
*type
= &field
->field_type
;
2428 switch(type
->type_class
) {
2430 case LTT_UINT_FIXED
:
2449 /* note this : array type size is the number of elements in the array,
2450 * while array field size of the length of the array in bytes */
2451 g_assert(type
->fields
->len
== 1);
2453 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
2454 field_compute_static_size(fac
, child
);
2456 if(child
->field_size
!= 0) {
2457 field
->field_size
= type
->size
* child
->field_size
;
2458 field
->dynamic_offsets
= g_array_sized_new(FALSE
, TRUE
,
2459 sizeof(off_t
), type
->size
);
2461 field
->field_size
= 0;
2466 g_assert(type
->fields
->len
== 2);
2468 off_t local_offset
= 0;
2469 LttField
*child
= &g_array_index(type
->fields
, LttField
, 1);
2470 field_compute_static_size(fac
, child
);
2471 field
->field_size
= 0;
2473 if(child
->field_size
!= 0) {
2474 field
->dynamic_offsets
= g_array_sized_new(FALSE
, TRUE
,
2475 sizeof(off_t
), SEQUENCE_AVG_ELEMENTS
);
2483 for(i
=0;i
<type
->fields
->len
;i
++) {
2484 LttField
*child
= &g_array_index(type
->fields
, LttField
, i
);
2485 field_compute_static_size(fac
, child
);
2486 if(child
->field_size
!= 0) {
2487 type
->size
+= ltt_align(type
->size
, get_alignment(child
),
2489 type
->size
+= child
->field_size
;
2491 /* As soon as we find a child with variable size, we have
2492 * a variable size */
2497 field
->field_size
= type
->size
;
2501 g_error("field_static_size : unknown type");
2508 /*****************************************************************************
2510 * precompute_fields_offsets : set the precomputable offset of the fields
2514 * offset : pointer to the current offset, must be incremented
2516 * return : 1 : found a variable length field, stop the processing.
2518 ****************************************************************************/
2521 gint
precompute_fields_offsets(LttFacility
*fac
, LttField
*field
, off_t
*offset
, gint is_compact
)
2523 LttType
*type
= &field
->field_type
;
2525 if(unlikely(is_compact
)) {
2526 g_assert(field
->field_size
!= 0);
2527 /* FIXME THIS IS A HUUUUUGE hack :
2528 * offset is between the compact_data field in struct LttEvent
2529 * and the address of the field root in the memory map.
2530 * ark. Both will stay at the same addresses while the event
2531 * is readable, so it's ok.
2533 field
->offset_root
= 0;
2534 field
->fixed_root
= FIELD_FIXED
;
2538 switch(type
->type_class
) {
2540 case LTT_UINT_FIXED
:
2555 g_assert(field
->field_size
!= 0);
2556 /* Align offset on type size */
2557 *offset
+= ltt_align(*offset
, get_alignment(field
),
2559 /* remember offset */
2560 field
->offset_root
= *offset
;
2561 field
->fixed_root
= FIELD_FIXED
;
2562 /* Increment offset */
2563 *offset
+= field
->field_size
;
2567 field
->offset_root
= *offset
;
2568 field
->fixed_root
= FIELD_FIXED
;
2572 g_assert(type
->fields
->len
== 1);
2574 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
2576 *offset
+= ltt_align(*offset
, get_alignment(field
),
2579 /* remember offset */
2580 field
->offset_root
= *offset
;
2581 field
->array_offset
= *offset
;
2582 field
->fixed_root
= FIELD_FIXED
;
2584 /* Let the child be variable */
2585 //precompute_fields_offsets(tf, child, offset);
2587 if(field
->field_size
!= 0) {
2588 /* Increment offset */
2589 /* field_size is the array size in bytes */
2590 *offset
+= field
->field_size
;
2598 g_assert(type
->fields
->len
== 2);
2603 *offset
+= ltt_align(*offset
, get_alignment(field
),
2606 /* remember offset */
2607 field
->offset_root
= *offset
;
2608 field
->fixed_root
= FIELD_FIXED
;
2610 child
= &g_array_index(type
->fields
, LttField
, 0);
2611 ret
= precompute_fields_offsets(fac
, child
, offset
, is_compact
);
2612 g_assert(ret
== 0); /* Seq len cannot have variable len */
2614 child
= &g_array_index(type
->fields
, LttField
, 1);
2615 *offset
+= ltt_align(*offset
, get_alignment(child
),
2617 field
->array_offset
= *offset
;
2618 /* Let the child be variable. */
2619 //ret = precompute_fields_offsets(fac, child, offset);
2621 /* Cannot precompute fields offsets of sequence members, and has
2622 * variable length. */
2632 *offset
+= ltt_align(*offset
, get_alignment(field
),
2634 /* remember offset */
2635 field
->offset_root
= *offset
;
2636 field
->fixed_root
= FIELD_FIXED
;
2638 for(i
=0; i
< type
->fields
->len
; i
++) {
2639 child
= &g_array_index(type
->fields
, LttField
, i
);
2640 ret
= precompute_fields_offsets(fac
, child
, offset
, is_compact
);
2653 *offset
+= ltt_align(*offset
, get_alignment(field
),
2655 /* remember offset */
2656 field
->offset_root
= *offset
;
2657 field
->fixed_root
= FIELD_FIXED
;
2659 for(i
=0; i
< type
->fields
->len
; i
++) {
2660 *offset
= field
->offset_root
;
2661 child
= &g_array_index(type
->fields
, LttField
, i
);
2662 ret
= precompute_fields_offsets(fac
, child
, offset
, is_compact
);
2666 *offset
= field
->offset_root
+ field
->field_size
;
2673 g_error("precompute_fields_offsets : unknown type");
2682 /*****************************************************************************
2684 * precompute_offsets : set the precomputable offset of an event type
2687 * event : event type
2689 ****************************************************************************/
2690 void precompute_offsets(LttFacility
*fac
, LttEventType
*event
)
2696 /* First, compute the size of fixed size fields. Will determine size for
2697 * arrays, struct and unions, which is not done by the parser */
2698 for(i
=0; i
<event
->fields
->len
; i
++) {
2699 LttField
*field
= &g_array_index(event
->fields
, LttField
, i
);
2700 field_compute_static_size(fac
, field
);
2703 /* Precompute all known offsets */
2704 for(i
=0; i
<event
->fields
->len
; i
++) {
2705 LttField
*field
= &g_array_index(event
->fields
, LttField
, i
);
2706 if(event
->has_compact_data
&& i
== 0)
2707 ret
= precompute_fields_offsets(fac
, field
, &offset
, 1);
2709 ret
= precompute_fields_offsets(fac
, field
, &offset
, 0);
2717 /*****************************************************************************
2719 * preset_field_type_size : set the fixed sizes of the field type
2722 * event_type : event type
2723 * offset_root : offset from the root
2724 * offset_parent : offset from the parent
2725 * fixed_root : Do we know a fixed offset to the root ?
2726 * fixed_parent : Do we know a fixed offset to the parent ?
2728 ****************************************************************************/
2732 // preset the fixed size offsets. Calculate them just like genevent-new : an
2733 // increment of a *to value that represents the offset from the start of the
2735 // The preset information is : offsets up to (and including) the first element
2736 // of variable size. All subsequent fields must be flagged "VARIABLE OFFSET".
2738 void preset_field_type_size(LttTracefile
*tf
, LttEventType
*event_type
,
2739 off_t offset_root
, off_t offset_parent
,
2740 enum field_status
*fixed_root
, enum field_status
*fixed_parent
,
2743 enum field_status local_fixed_root
, local_fixed_parent
;
2747 g_assert(field
->fixed_root
== FIELD_UNKNOWN
);
2748 g_assert(field
->fixed_parent
== FIELD_UNKNOWN
);
2749 g_assert(field
->fixed_size
== FIELD_UNKNOWN
);
2751 type
= field
->field_type
;
2753 field
->fixed_root
= *fixed_root
;
2754 if(field
->fixed_root
== FIELD_FIXED
)
2755 field
->offset_root
= offset_root
;
2757 field
->offset_root
= 0;
2759 field
->fixed_parent
= *fixed_parent
;
2760 if(field
->fixed_parent
== FIELD_FIXED
)
2761 field
->offset_parent
= offset_parent
;
2763 field
->offset_parent
= 0;
2765 size_t current_root_offset
;
2766 size_t current_offset
;
2767 enum field_status current_child_status
, final_child_status
;
2770 switch(type
->type_class
) {
2772 case LTT_UINT_FIXED
:
2781 field
->field_size
= ltt_type_size(tf
->trace
, type
);
2782 field
->fixed_size
= FIELD_FIXED
;
2785 field
->field_size
= (off_t
)event_type
->facility
->pointer_size
;
2786 field
->fixed_size
= FIELD_FIXED
;
2790 field
->field_size
= (off_t
)event_type
->facility
->long_size
;
2791 field
->fixed_size
= FIELD_FIXED
;
2796 field
->field_size
= (off_t
)event_type
->facility
->size_t_size
;
2797 field
->fixed_size
= FIELD_FIXED
;
2800 local_fixed_root
= FIELD_VARIABLE
;
2801 local_fixed_parent
= FIELD_VARIABLE
;
2802 preset_field_type_size(tf
, event_type
,
2804 &local_fixed_root
, &local_fixed_parent
,
2806 field
->fixed_size
= FIELD_VARIABLE
;
2807 field
->field_size
= 0;
2808 *fixed_root
= FIELD_VARIABLE
;
2809 *fixed_parent
= FIELD_VARIABLE
;
2812 field
->fixed_size
= FIELD_VARIABLE
;
2813 field
->field_size
= 0;
2814 *fixed_root
= FIELD_VARIABLE
;
2815 *fixed_parent
= FIELD_VARIABLE
;
2818 local_fixed_root
= FIELD_VARIABLE
;
2819 local_fixed_parent
= FIELD_VARIABLE
;
2820 preset_field_type_size(tf
, event_type
,
2822 &local_fixed_root
, &local_fixed_parent
,
2824 field
->fixed_size
= field
->child
[0]->fixed_size
;
2825 if(field
->fixed_size
== FIELD_FIXED
) {
2826 field
->field_size
= type
->element_number
* field
->child
[0]->field_size
;
2828 field
->field_size
= 0;
2829 *fixed_root
= FIELD_VARIABLE
;
2830 *fixed_parent
= FIELD_VARIABLE
;
2834 current_root_offset
= field
->offset_root
;
2836 current_child_status
= FIELD_FIXED
;
2837 for(i
=0;i
<type
->element_number
;i
++) {
2838 preset_field_type_size(tf
, event_type
,
2839 current_root_offset
, current_offset
,
2840 fixed_root
, ¤t_child_status
,
2842 if(current_child_status
== FIELD_FIXED
) {
2843 current_root_offset
+= field
->child
[i
]->field_size
;
2844 current_offset
+= field
->child
[i
]->field_size
;
2846 current_root_offset
= 0;
2850 if(current_child_status
!= FIELD_FIXED
) {
2851 *fixed_parent
= current_child_status
;
2852 field
->field_size
= 0;
2853 field
->fixed_size
= current_child_status
;
2855 field
->field_size
= current_offset
;
2856 field
->fixed_size
= FIELD_FIXED
;
2860 current_root_offset
= field
->offset_root
;
2863 final_child_status
= FIELD_FIXED
;
2864 for(i
=0;i
<type
->element_number
;i
++) {
2865 enum field_status current_root_child_status
= FIELD_FIXED
;
2866 enum field_status current_child_status
= FIELD_FIXED
;
2867 preset_field_type_size(tf
, event_type
,
2868 current_root_offset
, current_offset
,
2869 ¤t_root_child_status
, ¤t_child_status
,
2871 if(current_child_status
!= FIELD_FIXED
)
2872 final_child_status
= current_child_status
;
2874 max_size
= max(max_size
, field
->child
[i
]->field_size
);
2876 if(final_child_status
!= FIELD_FIXED
) {
2877 g_error("LTTV does not support variable size fields in unions.");
2878 /* This will stop the application. */
2879 *fixed_root
= final_child_status
;
2880 *fixed_parent
= final_child_status
;
2881 field
->field_size
= 0;
2882 field
->fixed_size
= current_child_status
;
2884 field
->field_size
= max_size
;
2885 field
->fixed_size
= FIELD_FIXED
;
2889 g_error("unexpected type NONE");
2896 /*****************************************************************************
2898 * check_fields_compatibility : Check for compatibility between two fields :
2899 * do they use the same inner structure ?
2901 * event_type1 : event type
2902 * event_type2 : event type
2905 *Returns : 0 if identical
2907 ****************************************************************************/
2908 // this function checks for equality of field types. Therefore, it does not use
2909 // per se offsets. For instance, an aligned version of a structure is
2910 // compatible with an unaligned version of the same structure.
2912 gint
check_fields_compatibility(LttEventType
*event_type1
,
2913 LttEventType
*event_type2
,
2914 LttField
*field1
, LttField
*field2
)
2916 guint different
= 0;
2920 if(field1
== NULL
) {
2921 if(field2
== NULL
) goto end
;
2926 } else if(field2
== NULL
) {
2931 type1
= &field1
->field_type
;
2932 type2
= &field2
->field_type
;
2934 if(type1
->type_class
!= type2
->type_class
) {
2938 if(type1
->network
!= type2
->network
) {
2943 switch(type1
->type_class
) {
2945 case LTT_UINT_FIXED
:
2960 if(field1
->field_size
!= field2
->field_size
)
2967 LttField
*child1
= &g_array_index(type1
->fields
, LttField
, 0);
2968 LttField
*child2
= &g_array_index(type2
->fields
, LttField
, 0);
2970 if(type1
->size
!= type2
->size
)
2972 if(check_fields_compatibility(event_type1
, event_type2
, child1
, child2
))
2978 LttField
*child1
= &g_array_index(type1
->fields
, LttField
, 1);
2979 LttField
*child2
= &g_array_index(type2
->fields
, LttField
, 1);
2981 if(check_fields_compatibility(event_type1
, event_type2
, child1
, child2
))
2991 if(type1
->fields
->len
!= type2
->fields
->len
) {
2996 for(i
=0; i
< type1
->fields
->len
; i
++) {
2999 child1
= &g_array_index(type1
->fields
, LttField
, i
);
3000 child2
= &g_array_index(type2
->fields
, LttField
, i
);
3001 different
= check_fields_compatibility(event_type1
,
3002 event_type2
, child1
, child2
);
3004 if(different
) break;
3010 g_error("check_fields_compatibility : unknown type");
3019 gint
check_fields_compatibility(LttEventType
*event_type1
,
3020 LttEventType
*event_type2
,
3021 LttField
*field1
, LttField
*field2
)
3023 guint different
= 0;
3028 if(field1
== NULL
) {
3029 if(field2
== NULL
) goto end
;
3034 } else if(field2
== NULL
) {
3039 g_assert(field1
->fixed_root
!= FIELD_UNKNOWN
);
3040 g_assert(field2
->fixed_root
!= FIELD_UNKNOWN
);
3041 g_assert(field1
->fixed_parent
!= FIELD_UNKNOWN
);
3042 g_assert(field2
->fixed_parent
!= FIELD_UNKNOWN
);
3043 g_assert(field1
->fixed_size
!= FIELD_UNKNOWN
);
3044 g_assert(field2
->fixed_size
!= FIELD_UNKNOWN
);
3046 type1
= field1
->field_type
;
3047 type2
= field2
->field_type
;
3049 if(type1
->type_class
!= type2
->type_class
) {
3053 if(type1
->element_name
!= type2
->element_name
) {
3058 switch(type1
->type_class
) {
3060 case LTT_UINT_FIXED
:
3075 if(field1
->field_size
!= field2
->field_size
) {
3081 if(type1
->element_number
!= type2
->element_number
) {
3085 for(i
=0;i
<type1
->element_number
;i
++) {
3086 if(type1
->enum_strings
[i
] != type2
->enum_strings
[i
]) {
3093 /* Two elements : size and child */
3094 g_assert(type1
->element_number
!= type2
->element_number
);
3095 for(i
=0;i
<type1
->element_number
;i
++) {
3096 if(check_fields_compatibility(event_type1
, event_type2
,
3097 field1
->child
[0], field2
->child
[0])) {
3106 if(field1
->field_size
!= field2
->field_size
) {
3110 /* Two elements : size and child */
3111 g_assert(type1
->element_number
!= type2
->element_number
);
3112 for(i
=0;i
<type1
->element_number
;i
++) {
3113 if(check_fields_compatibility(event_type1
, event_type2
,
3114 field1
->child
[0], field2
->child
[0])) {
3122 if(type1
->element_number
!= type2
->element_number
) {
3126 for(i
=0;i
<type1
->element_number
;i
++) {
3127 if(check_fields_compatibility(event_type1
, event_type2
,
3128 field1
->child
[0], field2
->child
[0])) {
3141 /*****************************************************************************
3143 * ltt_get_int : get an integer number
3145 * reverse_byte_order: must we reverse the byte order ?
3146 * size : the size of the integer
3147 * ptr : the data pointer
3149 * gint64 : a 64 bits integer
3150 ****************************************************************************/
3152 gint64
ltt_get_int(gboolean reverse_byte_order
, gint size
, void *data
)
3157 case 1: val
= *((gint8
*)data
); break;
3158 case 2: val
= ltt_get_int16(reverse_byte_order
, data
); break;
3159 case 4: val
= ltt_get_int32(reverse_byte_order
, data
); break;
3160 case 8: val
= ltt_get_int64(reverse_byte_order
, data
); break;
3161 default: val
= ltt_get_int64(reverse_byte_order
, data
);
3162 g_critical("get_int : integer size %d unknown", size
);
3169 /*****************************************************************************
3171 * ltt_get_uint : get an unsigned integer number
3173 * reverse_byte_order: must we reverse the byte order ?
3174 * size : the size of the integer
3175 * ptr : the data pointer
3177 * guint64 : a 64 bits unsigned integer
3178 ****************************************************************************/
3180 guint64
ltt_get_uint(gboolean reverse_byte_order
, gint size
, void *data
)
3185 case 1: val
= *((gint8
*)data
); break;
3186 case 2: val
= ltt_get_uint16(reverse_byte_order
, data
); break;
3187 case 4: val
= ltt_get_uint32(reverse_byte_order
, data
); break;
3188 case 8: val
= ltt_get_uint64(reverse_byte_order
, data
); break;
3189 default: val
= ltt_get_uint64(reverse_byte_order
, data
);
3190 g_critical("get_uint : unsigned integer size %d unknown",
3199 /* get the node name of the system */
3201 char * ltt_trace_system_description_node_name (LttSystemDescription
* s
)
3203 return s
->node_name
;
3207 /* get the domain name of the system */
3209 char * ltt_trace_system_description_domain_name (LttSystemDescription
* s
)
3211 return s
->domain_name
;
3215 /* get the description of the system */
3217 char * ltt_trace_system_description_description (LttSystemDescription
* s
)
3219 return s
->description
;
3223 /* get the NTP corrected start time of the trace */
3224 LttTime
ltt_trace_start_time(LttTrace
*t
)
3226 return t
->start_time
;
3229 /* get the monotonic start time of the trace */
3230 LttTime
ltt_trace_start_time_monotonic(LttTrace
*t
)
3232 return t
->start_time_from_tsc
;
3235 LttTracefile
*ltt_tracefile_new()
3238 tf
= g_new(LttTracefile
, 1);
3239 tf
->event
.tracefile
= tf
;
3243 void ltt_tracefile_destroy(LttTracefile
*tf
)
3248 void ltt_tracefile_copy(LttTracefile
*dest
, const LttTracefile
*src
)
3253 /* Before library loading... */
3257 LTT_FACILITY_NAME_HEARTBEAT
= g_quark_from_string("heartbeat");
3258 LTT_EVENT_NAME_HEARTBEAT
= g_quark_from_string("heartbeat");
3259 LTT_EVENT_NAME_HEARTBEAT_FULL
= g_quark_from_string("heartbeat_full");
3261 LTT_TRACEFILE_NAME_FACILITIES
= g_quark_from_string("/control/facilities");