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>
46 #include "ltt-private.h"
47 #include <ltt/trace.h>
48 #include <ltt/event.h>
49 //#include <ltt/type.h>
50 #include <ltt/ltt-types.h>
51 #include <ltt/marker.h>
53 /* Facility names used in this file */
55 GQuark LTT_FACILITY_NAME_HEARTBEAT
,
56 LTT_EVENT_NAME_HEARTBEAT
,
57 LTT_EVENT_NAME_HEARTBEAT_FULL
;
58 GQuark LTT_TRACEFILE_NAME_FACILITIES
;
65 #define __UNUSED__ __attribute__((__unused__))
67 #define g_info(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, format)
70 #define g_debug(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format)
75 /* Those macros must be called from within a function where page_size is a known
77 #define PAGE_MASK (~(page_size-1))
78 #define PAGE_ALIGN(addr) (((addr)+page_size-1)&PAGE_MASK)
80 /* set the offset of the fields belonging to the event,
81 need the information of the archecture */
82 //void set_fields_offsets(LttTracefile *tf, LttEventType *event_type);
83 //size_t get_fields_offsets(LttTracefile *tf, LttEventType *event_type, void *data);
85 /* get the size of the field type according to
86 * The facility size information. */
88 static inline void preset_field_type_size(LttTracefile
*tf
,
89 LttEventType
*event_type
,
90 off_t offset_root
, off_t offset_parent
,
91 enum field_status
*fixed_root
, enum field_status
*fixed_parent
,
95 /* map a fixed size or a block information from the file (fd) */
96 static gint
map_block(LttTracefile
* tf
, guint block_num
);
98 /* calculate nsec per cycles for current block */
100 static guint32
calc_nsecs_per_cycle(LttTracefile
* t
);
101 static guint64
cycles_2_ns(LttTracefile
*tf
, guint64 cycles
);
104 /* go to the next event */
105 static int ltt_seek_next_event(LttTracefile
*tf
);
107 //void ltt_update_event_size(LttTracefile *tf);
109 static int open_tracefiles(LttTrace
*trace
, gchar
*root_path
,
110 gchar
*relative_path
);
111 static int ltt_process_facility_tracefile(LttTracefile
*tf
);
112 static void ltt_tracefile_time_span_get(LttTracefile
*tf
,
113 LttTime
*start
, LttTime
*end
);
114 static void group_time_span_get(GQuark name
, gpointer data
, gpointer user_data
);
115 static gint
map_block(LttTracefile
* tf
, guint block_num
);
116 static int ltt_seek_next_event(LttTracefile
*tf
);
117 static void __attribute__((constructor
)) init(void);
118 static void ltt_update_event_size(LttTracefile
*tf
);
120 /* Enable event debugging */
121 static int a_event_debug
= 0;
123 void ltt_event_debug(int state
)
125 a_event_debug
= state
;
128 //void precompute_offsets(LttFacility *fac, LttEventType *event);
131 /* Functions to parse system.xml file (using glib xml parser) */
132 static void parser_start_element (GMarkupParseContext __UNUSED__
*context
,
133 const gchar
*element_name
,
134 const gchar
**attribute_names
,
135 const gchar
**attribute_values
,
140 LttSystemDescription
* des
= (LttSystemDescription
* )user_data
;
141 if(strcmp("system", element_name
)){
142 *error
= g_error_new(G_MARKUP_ERROR
,
144 "This is not system.xml file");
148 while(attribute_names
[i
]){
149 if(strcmp("node_name", attribute_names
[i
])==0){
150 des
->node_name
= g_strdup(attribute_values
[i
]);
151 }else if(strcmp("domainname", attribute_names
[i
])==0){
152 des
->domain_name
= g_strdup(attribute_values
[i
]);
153 }else if(strcmp("cpu", attribute_names
[i
])==0){
154 des
->nb_cpu
= atoi(attribute_values
[i
]);
155 }else if(strcmp("arch_size", attribute_names
[i
])==0){
156 if(strcmp(attribute_values
[i
],"LP32") == 0) des
->size
= LTT_LP32
;
157 else if(strcmp(attribute_values
[i
],"ILP32") == 0) des
->size
= LTT_ILP32
;
158 else if(strcmp(attribute_values
[i
],"LP64") == 0) des
->size
= LTT_LP64
;
159 else if(strcmp(attribute_values
[i
],"ILP64") == 0) des
->size
= LTT_ILP64
;
160 else if(strcmp(attribute_values
[i
],"UNKNOWN") == 0) des
->size
= LTT_UNKNOWN
;
161 }else if(strcmp("endian", attribute_names
[i
])==0){
162 if(strcmp(attribute_values
[i
],"LITTLE_ENDIAN") == 0)
163 des
->endian
= LTT_LITTLE_ENDIAN
;
164 else if(strcmp(attribute_values
[i
],"BIG_ENDIAN") == 0)
165 des
->endian
= LTT_BIG_ENDIAN
;
166 }else if(strcmp("kernel_name", attribute_names
[i
])==0){
167 des
->kernel_name
= g_strdup(attribute_values
[i
]);
168 }else if(strcmp("kernel_release", attribute_names
[i
])==0){
169 des
->kernel_release
= g_strdup(attribute_values
[i
]);
170 }else if(strcmp("kernel_version", attribute_names
[i
])==0){
171 des
->kernel_version
= g_strdup(attribute_values
[i
]);
172 }else if(strcmp("machine", attribute_names
[i
])==0){
173 des
->machine
= g_strdup(attribute_values
[i
]);
174 }else if(strcmp("processor", attribute_names
[i
])==0){
175 des
->processor
= g_strdup(attribute_values
[i
]);
176 }else if(strcmp("hardware_platform", attribute_names
[i
])==0){
177 des
->hardware_platform
= g_strdup(attribute_values
[i
]);
178 }else if(strcmp("operating_system", attribute_names
[i
])==0){
179 des
->operating_system
= g_strdup(attribute_values
[i
]);
180 }else if(strcmp("ltt_major_version", attribute_names
[i
])==0){
181 des
->ltt_major_version
= atoi(attribute_values
[i
]);
182 }else if(strcmp("ltt_minor_version", attribute_names
[i
])==0){
183 des
->ltt_minor_version
= atoi(attribute_values
[i
]);
184 }else if(strcmp("ltt_block_size", attribute_names
[i
])==0){
185 des
->ltt_block_size
= atoi(attribute_values
[i
]);
187 *error
= g_error_new(G_MARKUP_ERROR
,
189 "Not a valid attribute");
196 static void parser_characters (GMarkupParseContext __UNUSED__
*context
,
198 gsize __UNUSED__ text_len
,
200 GError __UNUSED__
**error
)
202 LttSystemDescription
* des
= (LttSystemDescription
* )user_data
;
203 des
->description
= g_strdup(text
);
208 LttFacility
*ltt_trace_get_facility_by_num(LttTrace
*t
,
211 g_assert(num
< t
->facilities_by_num
->len
);
213 return &g_array_index(t
->facilities_by_num
, LttFacility
, num
);
218 guint
ltt_trace_get_num_cpu(LttTrace
*t
)
226 * Return value : 0 success, 1 bad tracefile
228 int parse_trace_header(void *header
, LttTracefile
*tf
, LttTrace
*t
)
230 guint32
*magic_number
= (guint32
*)header
;
231 struct ltt_trace_header_any
*any
= (struct ltt_trace_header_any
*)header
;
233 if(*magic_number
== LTT_MAGIC_NUMBER
)
235 else if(*magic_number
== LTT_REV_MAGIC_NUMBER
)
237 else /* invalid magic number, bad tracefile ! */
240 /* Get float byte order : might be different from int byte order
241 * (or is set to 0 if the trace has no float (kernel trace)) */
242 tf
->float_word_order
= any
->float_word_order
;
243 tf
->alignment
= any
->alignment
;
244 tf
->has_heartbeat
= any
->has_heartbeat
;
247 t
->arch_type
= ltt_get_uint32(LTT_GET_BO(tf
),
249 t
->arch_variant
= ltt_get_uint32(LTT_GET_BO(tf
),
251 t
->arch_size
= any
->arch_size
;
252 t
->ltt_major_version
= any
->major_version
;
253 t
->ltt_minor_version
= any
->minor_version
;
254 t
->flight_recorder
= any
->flight_recorder
;
255 // t->compact_facilities = NULL;
258 switch(any
->major_version
) {
261 g_warning("Unsupported trace version : %hhu.%hhu",
262 any
->major_version
, any
->minor_version
);
266 switch(any
->minor_version
) {
269 struct ltt_trace_header_1_0
*vheader
=
270 (struct ltt_trace_header_1_0
*)header
;
271 tf
->buffer_header_size
=
272 sizeof(struct ltt_block_start_header
)
273 + sizeof(struct ltt_trace_header_1_0
);
274 tf
->tsc_lsb_truncate
= vheader
->tsc_lsb_truncate
;
275 tf
->tscbits
= vheader
->tscbits
;
276 tf
->tsc_msb_cutoff
= 32 - tf
->tsc_lsb_truncate
- tf
->tscbits
;
277 tf
->compact_event_bits
= 32 - vheader
->compact_data_shift
;
278 tf
->tsc_mask
= ((1ULL << (tf
->tscbits
))-1);
279 tf
->tsc_mask
= tf
->tsc_mask
<< tf
->tsc_lsb_truncate
;
280 tf
->tsc_mask_next_bit
= (1ULL<<(tf
->tscbits
));
281 tf
->tsc_mask_next_bit
= tf
->tsc_mask_next_bit
<< tf
->tsc_lsb_truncate
;
283 t
->start_freq
= ltt_get_uint64(LTT_GET_BO(tf
),
284 &vheader
->start_freq
);
285 t
->freq_scale
= ltt_get_uint32(LTT_GET_BO(tf
),
286 &vheader
->freq_scale
);
287 t
->start_tsc
= ltt_get_uint64(LTT_GET_BO(tf
),
288 &vheader
->start_tsc
);
289 t
->start_monotonic
= ltt_get_uint64(LTT_GET_BO(tf
),
290 &vheader
->start_monotonic
);
291 t
->start_time
.tv_sec
= ltt_get_uint64(LTT_GET_BO(tf
),
292 &vheader
->start_time_sec
);
293 t
->start_time
.tv_nsec
= ltt_get_uint64(LTT_GET_BO(tf
),
294 &vheader
->start_time_usec
);
295 t
->start_time
.tv_nsec
*= 1000; /* microsec to nanosec */
297 t
->start_time_from_tsc
= ltt_time_from_uint64(
299 * (1000000000.0 / tf
->trace
->freq_scale
)
300 / (double)t
->start_freq
);
305 g_warning("Unsupported trace version : %hhu.%hhu",
306 any
->major_version
, any
->minor_version
);
311 g_warning("Unsupported trace version : %hhu.%hhu",
312 any
->major_version
, any
->minor_version
);
322 /*****************************************************************************
324 * ltt_tracefile_open : open a trace file, construct a LttTracefile
326 * t : the trace containing the tracefile
327 * fileName : path name of the trace file
328 * tf : the tracefile structure
330 * : 0 for success, -1 otherwise.
331 ****************************************************************************/
333 gint
ltt_tracefile_open(LttTrace
*t
, gchar
* fileName
, LttTracefile
*tf
)
335 struct stat lTDFStat
; /* Trace data file status */
336 struct ltt_block_start_header
*header
;
337 int page_size
= getpagesize();
340 tf
->long_name
= g_quark_from_string(fileName
);
342 tf
->fd
= open(fileName
, O_RDONLY
);
344 g_warning("Unable to open input data file %s\n", fileName
);
348 // Get the file's status
349 if(fstat(tf
->fd
, &lTDFStat
) < 0){
350 g_warning("Unable to get the status of the input data file %s\n", fileName
);
354 // Is the file large enough to contain a trace
355 if(lTDFStat
.st_size
<
356 (off_t
)(sizeof(struct ltt_block_start_header
)
357 + sizeof(struct ltt_trace_header_any
))){
358 g_print("The input data file %s does not contain a trace\n", fileName
);
362 /* Temporarily map the buffer start header to get trace information */
363 /* Multiple of pages aligned head */
364 tf
->buffer
.head
= mmap(0,
365 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)
366 + sizeof(struct ltt_trace_header_any
)), PROT_READ
,
367 MAP_PRIVATE
, tf
->fd
, 0);
368 if(tf
->buffer
.head
== MAP_FAILED
) {
369 perror("Error in allocating memory for buffer of tracefile");
372 g_assert( ( (guint
)tf
->buffer
.head
&(8-1) ) == 0); // make sure it's aligned.
374 header
= (struct ltt_block_start_header
*)tf
->buffer
.head
;
376 if(parse_trace_header(header
->trace
, tf
, NULL
)) {
377 g_warning("parse_trace_header error");
381 //store the size of the file
382 tf
->file_size
= lTDFStat
.st_size
;
383 tf
->buf_size
= ltt_get_uint32(LTT_GET_BO(tf
), &header
->buf_size
);
384 tf
->num_blocks
= tf
->file_size
/ tf
->buf_size
;
386 if(munmap(tf
->buffer
.head
,
387 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)
388 + sizeof(struct ltt_trace_header_any
)))) {
389 g_warning("unmap size : %u\n",
390 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)
391 + sizeof(struct ltt_trace_header_any
)));
392 perror("munmap error");
395 tf
->buffer
.head
= NULL
;
397 //read the first block
398 if(map_block(tf
,0)) {
399 perror("Cannot map block for tracefile");
407 if(munmap(tf
->buffer
.head
,
408 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)
409 + sizeof(struct ltt_trace_header_any
)))) {
410 g_warning("unmap size : %u\n",
411 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)
412 + sizeof(struct ltt_trace_header_any
)));
413 perror("munmap error");
422 LttTrace
*ltt_tracefile_get_trace(LttTracefile
*tf
)
428 /*****************************************************************************
429 *Open control and per cpu tracefiles
430 ****************************************************************************/
432 void ltt_tracefile_open_cpu(LttTrace
*t
, gchar
* tracefile_name
)
435 tf
= ltt_tracefile_open(t
,tracefile_name
);
437 t
->per_cpu_tracefile_number
++;
438 g_ptr_array_add(t
->per_cpu_tracefiles
, tf
);
441 gint
ltt_tracefile_open_control(LttTrace
*t
, gchar
* control_name
)
450 tf
= ltt_tracefile_open(t
,control_name
);
452 g_warning("ltt_tracefile_open_control : bad file descriptor");
455 t
->control_tracefile_number
++;
456 g_ptr_array_add(t
->control_tracefiles
,tf
);
458 //parse facilities tracefile to get base_id
459 if(strcmp(&control_name
[strlen(control_name
)-10],"facilities") ==0){
461 if(!ltt_tracefile_read(tf
,&ev
)) return 0; // end of file
463 if(ev
.event_id
== TRACE_FACILITY_LOAD
){
465 fLoad
.name
= (gchar
*)pos
;
466 fLoad
.checksum
= *(LttChecksum
*)(pos
+ strlen(fLoad
.name
));
467 fLoad
.base_code
= *(guint32
*)(pos
+ strlen(fLoad
.name
) + sizeof(LttChecksum
));
469 for(i
=0;i
<t
->facility_number
;i
++){
470 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
,i
);
471 if(strcmp(f
->name
,fLoad
.name
)==0 && fLoad
.checksum
==f
->checksum
){
472 f
->base_id
= fLoad
.base_code
;
476 if(i
==t
->facility_number
) {
477 g_warning("Facility: %s, checksum: %u is not found",
478 fLoad
.name
,(unsigned int)fLoad
.checksum
);
481 }else if(ev
.event_id
== TRACE_BLOCK_START
){
483 }else if(ev
.event_id
== TRACE_BLOCK_END
){
486 g_warning("Not valid facilities trace file");
495 /*****************************************************************************
497 * ltt_tracefile_close: close a trace file,
499 * t : tracefile which will be closed
500 ****************************************************************************/
502 void ltt_tracefile_close(LttTracefile
*t
)
504 int page_size
= getpagesize();
506 if(t
->buffer
.head
!= NULL
)
507 if(munmap(t
->buffer
.head
, PAGE_ALIGN(t
->buf_size
))) {
508 g_warning("unmap size : %u\n",
509 PAGE_ALIGN(t
->buf_size
));
510 perror("munmap error");
518 /*****************************************************************************
519 *Get system information
520 ****************************************************************************/
522 gint
getSystemInfo(LttSystemDescription
* des
, gchar
* pathname
)
529 GMarkupParseContext
* context
;
530 GError
* error
= NULL
;
531 GMarkupParser markup_parser
=
533 parser_start_element
,
536 NULL
, /* passthrough */
540 fd
= g_open(pathname
, O_RDONLY
, 0);
542 g_warning("Can not open file : %s\n", pathname
);
546 iochan
= g_io_channel_unix_new(fd
);
548 context
= g_markup_parse_context_new(&markup_parser
, 0, des
,NULL
);
550 //while(fgets(buf,DIR_NAME_SIZE, fp) != NULL){
551 while(g_io_channel_read_line(iochan
, &buf
, &length
, NULL
, &error
)
552 != G_IO_STATUS_EOF
) {
555 g_warning("Can not read xml file: \n%s\n", error
->message
);
558 if(!g_markup_parse_context_parse(context
, buf
, length
, &error
)){
560 g_warning("Can not parse xml file: \n%s\n", error
->message
);
563 g_markup_parse_context_free(context
);
565 g_io_channel_shutdown(iochan
, FALSE
, &error
); /* No flush */
567 g_warning("Can not close file: \n%s\n", error
->message
);
575 g_markup_parse_context_free(context
);
577 g_io_channel_shutdown(iochan
, FALSE
, &error
); /* No flush */
579 g_warning("Can not close file: \n%s\n", error
->message
);
590 /*****************************************************************************
591 *The following functions get facility/tracefile information
592 ****************************************************************************/
594 gint
getFacilityInfo(LttTrace
*t
, gchar
* eventdefs
)
601 gchar fullname
[DIR_NAME_SIZE
];
602 GError
* error
= NULL
;
604 dir
= g_dir_open(eventdefs
, 0, &error
);
607 g_warning("Can not open directory: %s, %s\n", eventdefs
, error
->message
);
612 while((name
= g_dir_read_name(dir
)) != NULL
){
613 if(!g_pattern_match_simple("*.xml", name
)) continue;
614 strcpy(fullname
,eventdefs
);
615 strcat(fullname
,name
);
616 ltt_facility_open(t
,fullname
);
620 for(j
=0;j
<t
->facility_number
;j
++){
621 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, j
);
622 for(i
=0; i
<f
->event_number
; i
++){
624 setFieldsOffset(NULL
, et
, NULL
, t
);
631 /*****************************************************************************
632 *A trace is specified as a pathname to the directory containing all the
633 *associated data (control tracefiles, per cpu tracefiles, event
636 *When a trace is closed, all the associated facilities, types and fields
637 *are released as well.
641 /****************************************************************************
642 * get_absolute_pathname
644 * return the unique pathname in the system
646 * MD : Fixed this function so it uses realpath, dealing well with
647 * forgotten cases (.. were not used correctly before).
649 ****************************************************************************/
650 void get_absolute_pathname(const gchar
*pathname
, gchar
* abs_pathname
)
652 abs_pathname
[0] = '\0';
654 if ( realpath (pathname
, abs_pathname
) != NULL
)
658 /* error, return the original path unmodified */
659 strcpy(abs_pathname
, pathname
);
665 /* Search for something like : .*_.*
667 * The left side is the name, the right side is the number.
670 int get_tracefile_name_number(gchar
*raw_name
,
677 guint raw_name_len
= strlen(raw_name
);
678 gchar char_name
[PATH_MAX
];
685 for(i
=raw_name_len
-1;i
>=0;i
--) {
686 if(raw_name
[i
] == '_') break;
688 if(i
==-1) { /* Either not found or name length is 0 */
689 /* This is a userspace tracefile */
690 strncpy(char_name
, raw_name
, raw_name_len
);
691 char_name
[raw_name_len
] = '\0';
692 *name
= g_quark_from_string(char_name
);
693 *num
= 0; /* unknown cpu */
694 for(i
=0;i
<raw_name_len
;i
++) {
695 if(raw_name
[i
] == '/') {
700 for(;i
<raw_name_len
;i
++) {
701 if(raw_name
[i
] == '/') {
706 for(;i
<raw_name_len
;i
++) {
707 if(raw_name
[i
] == '-') {
711 if(i
== raw_name_len
) return -1;
713 tmpptr
= &raw_name
[i
];
714 for(;i
<raw_name_len
;i
++) {
715 if(raw_name
[i
] == '.') {
720 *tid
= strtoul(tmpptr
, &endptr
, 10);
722 return -1; /* No digit */
723 if(*tid
== ULONG_MAX
)
724 return -1; /* underflow / overflow */
726 tmpptr
= &raw_name
[i
];
727 for(;i
<raw_name_len
;i
++) {
728 if(raw_name
[i
] == '.') {
733 *pgid
= strtoul(tmpptr
, &endptr
, 10);
735 return -1; /* No digit */
736 if(*pgid
== ULONG_MAX
)
737 return -1; /* underflow / overflow */
739 tmpptr
= &raw_name
[i
];
740 *creation
= strtoull(tmpptr
, &endptr
, 10);
742 return -1; /* No digit */
743 if(*creation
== G_MAXUINT64
)
744 return -1; /* underflow / overflow */
748 cpu_num
= strtol(raw_name
+underscore_pos
+1, &endptr
, 10);
750 if(endptr
== raw_name
+underscore_pos
+1)
751 return -1; /* No digit */
752 if(cpu_num
== LONG_MIN
|| cpu_num
== LONG_MAX
)
753 return -1; /* underflow / overflow */
755 strncpy(char_name
, raw_name
, underscore_pos
);
756 char_name
[underscore_pos
] = '\0';
758 *name
= g_quark_from_string(char_name
);
767 GData
**ltt_trace_get_tracefiles_groups(LttTrace
*trace
)
769 return &trace
->tracefiles
;
773 void compute_tracefile_group(GQuark key_id
,
775 struct compute_tracefile_group_args
*args
)
780 for(i
=0; i
<group
->len
; i
++) {
781 tf
= &g_array_index (group
, LttTracefile
, i
);
783 args
->func(tf
, args
->func_args
);
788 void ltt_tracefile_group_destroy(gpointer data
)
790 GArray
*group
= (GArray
*)data
;
794 for(i
=0; i
<group
->len
; i
++) {
795 tf
= &g_array_index (group
, LttTracefile
, i
);
797 ltt_tracefile_close(tf
);
799 g_array_free(group
, TRUE
);
802 gboolean
ltt_tracefile_group_has_cpu_online(gpointer data
)
804 GArray
*group
= (GArray
*)data
;
808 for(i
=0; i
<group
->len
; i
++) {
809 tf
= &g_array_index (group
, LttTracefile
, i
);
817 /* Open each tracefile under a specific directory. Put them in a
818 * GData : permits to access them using their tracefile group pathname.
819 * i.e. access control/modules tracefile group by index :
822 * relative path is the path relative to the trace root
823 * root path is the full path
825 * A tracefile group is simply an array where all the per cpu tracefiles sit.
828 int open_tracefiles(LttTrace
*trace
, gchar
*root_path
, gchar
*relative_path
)
830 DIR *dir
= opendir(root_path
);
831 struct dirent
*entry
;
832 struct stat stat_buf
;
835 gchar path
[PATH_MAX
];
840 gchar rel_path
[PATH_MAX
];
849 strncpy(path
, root_path
, PATH_MAX
-1);
850 path_len
= strlen(path
);
851 path
[path_len
] = '/';
853 path_ptr
= path
+ path_len
;
855 strncpy(rel_path
, relative_path
, PATH_MAX
-1);
856 rel_path_len
= strlen(rel_path
);
857 rel_path
[rel_path_len
] = '/';
859 rel_path_ptr
= rel_path
+ rel_path_len
;
861 while((entry
= readdir(dir
)) != NULL
) {
863 if(entry
->d_name
[0] == '.') continue;
865 strncpy(path_ptr
, entry
->d_name
, PATH_MAX
- path_len
);
866 strncpy(rel_path_ptr
, entry
->d_name
, PATH_MAX
- rel_path_len
);
868 ret
= stat(path
, &stat_buf
);
874 g_debug("Tracefile file or directory : %s\n", path
);
876 // if(strcmp(rel_path, "/eventdefs") == 0) continue;
878 if(S_ISDIR(stat_buf
.st_mode
)) {
880 g_debug("Entering subdirectory...\n");
881 ret
= open_tracefiles(trace
, path
, rel_path
);
882 if(ret
< 0) continue;
883 } else if(S_ISREG(stat_buf
.st_mode
)) {
885 guint num
, tid
, pgid
;
888 num
= tid
= pgid
= 0;
890 if(get_tracefile_name_number(rel_path
, &name
, &num
, &tid
, &pgid
, &creation
))
891 continue; /* invalid name */
893 g_debug("Opening file.\n");
894 if(ltt_tracefile_open(trace
, path
, &tmp_tf
)) {
895 g_info("Error opening tracefile %s", path
);
897 continue; /* error opening the tracefile : bad magic number ? */
900 g_debug("Tracefile name is %s and number is %u",
901 g_quark_to_string(name
), num
);
903 tmp_tf
.cpu_online
= 1;
904 tmp_tf
.cpu_num
= num
;
908 tmp_tf
.creation
= creation
;
909 if(tmp_tf
.name
== g_quark_from_string("/compact")
910 || tmp_tf
.name
== g_quark_from_string("/flight-compact"))
914 group
= g_datalist_id_get_data(&trace
->tracefiles
, name
);
916 /* Elements are automatically cleared when the array is allocated.
917 * It makes the cpu_online variable set to 0 : cpu offline, by default.
919 group
= g_array_sized_new (FALSE
, TRUE
, sizeof(LttTracefile
), 10);
920 g_datalist_id_set_data_full(&trace
->tracefiles
, name
,
921 group
, ltt_tracefile_group_destroy
);
924 /* Add the per cpu tracefile to the named group */
925 unsigned int old_len
= group
->len
;
927 group
= g_array_set_size(group
, num
+1);
928 g_array_index (group
, LttTracefile
, num
) = tmp_tf
;
929 g_array_index (group
, LttTracefile
, num
).event
.tracefile
=
930 &g_array_index (group
, LttTracefile
, num
);
939 /* ltt_get_facility_description
941 * Opens the file corresponding to the requested facility (identified by fac_id
944 * The name searched is : %trace root%/eventdefs/facname_checksum.xml
946 * Returns 0 on success, or 1 on failure.
949 static int ltt_get_facility_description(LttFacility
*f
,
951 LttTracefile
*fac_tf
)
953 char desc_file_name
[PATH_MAX
];
960 text
= g_quark_to_string(t
->pathname
);
961 textlen
= strlen(text
);
963 if(textlen
>= PATH_MAX
) goto name_error
;
964 strcpy(desc_file_name
, text
);
966 text
= "/eventdefs/";
967 textlen
+=strlen(text
);
968 if(textlen
>= PATH_MAX
) goto name_error
;
969 strcat(desc_file_name
, text
);
971 text
= g_quark_to_string(f
->name
);
972 fac_name_len
= strlen(text
);
973 textlen
+=fac_name_len
;
974 if(textlen
>= PATH_MAX
) goto name_error
;
975 strcat(desc_file_name
, text
);
977 /* arch specific facilities are named like this : name_arch */
978 if(fac_name_len
+1 < sizeof("_arch"))
981 if(!strcmp(&text
[fac_name_len
+1-sizeof("_arch")], "_arch"))
989 textlen
+=strlen(text
);
990 if(textlen
>= PATH_MAX
) goto name_error
;
991 strcat(desc_file_name
, text
);
993 err
= snprintf(desc_file_name
+textlen
, PATH_MAX
-textlen
-1,
995 if(err
< 0) goto name_error
;
997 textlen
=strlen(desc_file_name
);
1002 switch(t
->arch_type
) {
1003 case LTT_ARCH_TYPE_I386
:
1006 case LTT_ARCH_TYPE_PPC
:
1009 case LTT_ARCH_TYPE_SH
:
1012 case LTT_ARCH_TYPE_S390
:
1015 case LTT_ARCH_TYPE_MIPS
:
1018 case LTT_ARCH_TYPE_ARM
:
1021 case LTT_ARCH_TYPE_PPC64
:
1024 case LTT_ARCH_TYPE_X86_64
:
1027 case LTT_ARCH_TYPE_C2
:
1030 case LTT_ARCH_TYPE_POWERPC
:
1034 g_error("Trace from unsupported architecture.");
1036 textlen
+=strlen(text
);
1037 if(textlen
>= PATH_MAX
) goto name_error
;
1038 strcat(desc_file_name
, text
);
1042 textlen
+=strlen(text
);
1043 if(textlen
>= PATH_MAX
) goto name_error
;
1044 strcat(desc_file_name
, text
);
1046 err
= ltt_facility_open(f
, t
, desc_file_name
);
1047 if(err
) goto facility_error
;
1056 static void ltt_fac_ids_destroy(gpointer data
)
1058 GArray
*fac_ids
= (GArray
*)data
;
1060 g_array_free(fac_ids
, TRUE
);
1064 /* Presumes the tracefile is already seeked at the beginning. It makes sense,
1065 * because it must be done just after the opening */
1066 int ltt_process_facility_tracefile(LttTracefile
*tf
)
1075 err
= ltt_tracefile_read_seek(tf
);
1076 if(err
== EPERM
) goto seek_error
;
1077 else if(err
== ERANGE
) break; /* End of tracefile */
1079 err
= ltt_tracefile_read_update_event(tf
);
1080 if(err
) goto update_error
;
1082 /* We are on a facility load/or facility unload/ or heartbeat event */
1084 * * facility 0 is hardcoded : this is the core facility. It will be shown
1085 * in the facility array though, and is shown as "loaded builtin" in the
1087 * It contains event :
1089 * 1 : facility unload
1090 * 2 : state dump facility load
1093 if(tf
->event
.event_id
>= MARKER_CORE_IDS
) {
1094 /* Should only contain core facility */
1095 g_warning("Error in processing facility file %s, "
1096 "should not contain event id %u.", g_quark_to_string(tf
->name
),
1097 tf
->event
.event_id
);
1099 goto event_id_error
;
1103 const char *marker_name
, *format
;
1105 guint8 int_size
, long_size
, pointer_size
, size_t_size
, alignment
;
1108 switch((enum marker_id
)tf
->event
.event_id
) {
1109 case MARKER_ID_SET_MARKER_ID
:
1110 marker_name
= pos
= tf
->event
.data
;
1111 g_debug("Doing MARKER_ID_SET_MARKER_ID of marker %s", marker_name
);
1112 pos
+= strlen(marker_name
) + 1;
1113 //remove genevent compatibility
1114 //pos += ltt_align((size_t)pos, tf->trace->arch_size, tf->alignment);
1115 pos
+= ltt_align((size_t)pos
, sizeof(uint16_t), tf
->alignment
);
1116 id
= ltt_get_uint16(LTT_GET_BO(tf
), pos
);
1117 g_debug("In MARKER_ID_SET_MARKER_ID of marker %s id %hu",
1119 pos
+= sizeof(guint16
);
1120 int_size
= *(guint8
*)pos
;
1121 pos
+= sizeof(guint8
);
1122 long_size
= *(guint8
*)pos
;
1123 pos
+= sizeof(guint8
);
1124 pointer_size
= *(guint8
*)pos
;
1125 pos
+= sizeof(guint8
);
1126 size_t_size
= *(guint8
*)pos
;
1127 pos
+= sizeof(guint8
);
1128 alignment
= *(guint8
*)pos
;
1129 pos
+= sizeof(guint8
);
1130 marker_id_event(tf
->trace
, g_quark_from_string(marker_name
),
1131 id
, int_size
, long_size
,
1132 pointer_size
, size_t_size
, alignment
);
1134 case MARKER_ID_SET_MARKER_FORMAT
:
1135 marker_name
= pos
= tf
->event
.data
;
1136 g_debug("Doing MARKER_ID_SET_MARKER_FORMAT of marker %s",
1138 pos
+= strlen(marker_name
) + 1;
1140 //pos += ltt_align((size_t)pos, tf->trace->arch_size, tf->alignment);
1142 pos
+= strlen(format
) + 1;
1144 //pos += ltt_align((size_t)pos, tf->trace->arch_size, tf->alignment);
1145 marker_format_event(tf
->trace
, g_quark_from_string(marker_name
),
1147 /* get information from dictionnary TODO */
1149 case MARKER_ID_HEARTBEAT_32
:
1150 case MARKER_ID_HEARTBEAT_64
:
1153 g_warning("Error in processing facility file %s, "
1154 "unknown event id %hhu.",
1155 g_quark_to_string(tf
->name
),
1156 tf
->event
.event_id
);
1158 goto event_id_error
;
1164 /* Error handling */
1168 g_warning("An error occured in facility tracefile parsing");
1173 LttTrace
*ltt_trace_open(const gchar
*pathname
)
1175 gchar abs_path
[PATH_MAX
];
1180 struct ltt_block_start_header
*header
;
1182 struct dirent
*entry
;
1183 guint control_found
= 0;
1184 struct stat stat_buf
;
1185 gchar path
[PATH_MAX
];
1187 t
= g_new(LttTrace
, 1);
1188 if(!t
) goto alloc_error
;
1190 get_absolute_pathname(pathname
, abs_path
);
1191 t
->pathname
= g_quark_from_string(abs_path
);
1193 g_datalist_init(&t
->tracefiles
);
1195 /* Test to see if it looks like a trace */
1196 dir
= opendir(abs_path
);
1201 while((entry
= readdir(dir
)) != NULL
) {
1202 strcpy(path
, abs_path
);
1204 strcat(path
, entry
->d_name
);
1205 ret
= stat(path
, &stat_buf
);
1210 if(S_ISDIR(stat_buf
.st_mode
)) {
1211 if(strcmp(entry
->d_name
, "control") == 0) {
1218 if(!control_found
) goto find_error
;
1220 /* Open all the tracefiles */
1221 if(open_tracefiles(t
, abs_path
, "")) {
1222 g_warning("Error opening tracefile %s", abs_path
);
1226 /* Parse each trace control/facilitiesN files : get runtime fac. info */
1227 group
= g_datalist_id_get_data(&t
->tracefiles
, LTT_TRACEFILE_NAME_FACILITIES
);
1229 g_error("Trace %s has no facility tracefile", abs_path
);
1231 goto facilities_error
;
1234 /* Get the trace information for the control/facility 0 tracefile */
1235 g_assert(group
->len
> 0);
1236 tf
= &g_array_index (group
, LttTracefile
, 0);
1237 header
= (struct ltt_block_start_header
*)tf
->buffer
.head
;
1238 g_assert(parse_trace_header(header
->trace
,
1241 t
->num_cpu
= group
->len
;
1243 ret
= allocate_marker_data(t
);
1245 g_error("Error in allocating marker data");
1247 for(i
=0; i
<group
->len
; i
++) {
1248 tf
= &g_array_index (group
, LttTracefile
, i
);
1249 if(ltt_process_facility_tracefile(tf
))
1250 goto facilities_error
;
1255 /* Error handling */
1257 destroy_marker_data(t
);
1259 g_datalist_clear(&t
->tracefiles
);
1267 GQuark
ltt_trace_name(const LttTrace
*t
)
1273 /******************************************************************************
1274 * When we copy a trace, we want all the opening actions to happen again :
1275 * the trace will be reopened and totally independant from the original.
1276 * That's why we call ltt_trace_open.
1277 *****************************************************************************/
1278 LttTrace
*ltt_trace_copy(LttTrace
*self
)
1280 return ltt_trace_open(g_quark_to_string(self
->pathname
));
1283 void ltt_trace_close(LttTrace
*t
)
1285 g_datalist_clear(&t
->tracefiles
);
1290 /*****************************************************************************
1291 *Get the system description of the trace
1292 ****************************************************************************/
1294 LttFacility
*ltt_trace_facility_by_id(LttTrace
*t
, guint8 id
)
1296 g_assert(id
< t
->facilities_by_num
->len
);
1297 return &g_array_index(t
->facilities_by_num
, LttFacility
, id
);
1300 /* ltt_trace_facility_get_by_name
1302 * Returns the GArray of facility indexes. All the fac_ids that matches the
1303 * requested facility name.
1305 * If name is not found, returns NULL.
1307 GArray
*ltt_trace_facility_get_by_name(LttTrace
*t
, GQuark name
)
1309 return g_datalist_id_get_data(&t
->facilities_by_name
, name
);
1313 /*****************************************************************************
1314 * Functions to discover all the event types in the trace
1315 ****************************************************************************/
1318 unsigned ltt_trace_eventtype_number(LttTrace
*t
)
1322 unsigned int num
= t
->facility_number
;
1326 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, i
);
1327 count
+= f
->event_number
;
1334 //use an iteration on all the trace facilities, and inside iteration on all the
1335 //event types in each facilities instead.
1336 LttEventType
*ltt_trace_eventtype_get(LttTrace
*t
, unsigned evId
)
1338 LttEventType
*event_type
;
1341 f
= ltt_trace_facility_by_id(t
,evId
);
1343 if(unlikely(!f
)) event_type
= NULL
;
1344 else event_type
= f
->events
[evId
- f
->base_id
];
1351 /*****************************************************************************
1352 * ltt_trace_find_tracefile
1354 * Find a tracefile by name and index in the group.
1356 * Returns a pointer to the tracefiles, else NULL.
1357 ****************************************************************************/
1359 LttTracefile
*ltt_trace_find_tracefile(LttTrace
*t
, const gchar
*name
)
1364 /*****************************************************************************
1365 * Get the start time and end time of the trace
1366 ****************************************************************************/
1368 void ltt_tracefile_time_span_get(LttTracefile
*tf
,
1369 LttTime
*start
, LttTime
*end
)
1373 err
= map_block(tf
, 0);
1375 g_error("Can not map block");
1376 *start
= ltt_time_infinite
;
1378 *start
= tf
->buffer
.begin
.timestamp
;
1380 err
= map_block(tf
, tf
->num_blocks
- 1); /* Last block */
1382 g_error("Can not map block");
1383 *end
= ltt_time_zero
;
1385 *end
= tf
->buffer
.end
.timestamp
;
1388 struct tracefile_time_span_get_args
{
1394 void group_time_span_get(GQuark name
, gpointer data
, gpointer user_data
)
1396 struct tracefile_time_span_get_args
*args
=
1397 (struct tracefile_time_span_get_args
*)user_data
;
1399 GArray
*group
= (GArray
*)data
;
1405 for(i
=0; i
<group
->len
; i
++) {
1406 tf
= &g_array_index (group
, LttTracefile
, i
);
1407 if(tf
->cpu_online
) {
1408 ltt_tracefile_time_span_get(tf
, &tmp_start
, &tmp_end
);
1409 if(ltt_time_compare(*args
->start
, tmp_start
)>0) *args
->start
= tmp_start
;
1410 if(ltt_time_compare(*args
->end
, tmp_end
)<0) *args
->end
= tmp_end
;
1415 void ltt_trace_time_span_get(LttTrace
*t
, LttTime
*start
, LttTime
*end
)
1417 LttTime min_start
= ltt_time_infinite
;
1418 LttTime max_end
= ltt_time_zero
;
1419 struct tracefile_time_span_get_args args
= { t
, &min_start
, &max_end
};
1421 g_datalist_foreach(&t
->tracefiles
, &group_time_span_get
, &args
);
1423 if(start
!= NULL
) *start
= min_start
;
1424 if(end
!= NULL
) *end
= max_end
;
1429 /*****************************************************************************
1430 *Get the name of a tracefile
1431 ****************************************************************************/
1433 GQuark
ltt_tracefile_name(const LttTracefile
*tf
)
1438 GQuark
ltt_tracefile_long_name(const LttTracefile
*tf
)
1440 return tf
->long_name
;
1445 guint
ltt_tracefile_cpu(LttTracefile
*tf
)
1450 guint
ltt_tracefile_tid(LttTracefile
*tf
)
1455 guint
ltt_tracefile_pgid(LttTracefile
*tf
)
1460 guint64
ltt_tracefile_creation(LttTracefile
*tf
)
1462 return tf
->creation
;
1464 /*****************************************************************************
1465 * Get the number of blocks in the tracefile
1466 ****************************************************************************/
1468 guint
ltt_tracefile_block_number(LttTracefile
*tf
)
1470 return tf
->num_blocks
;
1474 /* Seek to the first event in a tracefile that has a time equal or greater than
1475 * the time passed in parameter.
1477 * If the time parameter is outside the tracefile time span, seek to the first
1478 * event or if after, return ERANGE.
1480 * If the time parameter is before the first event, we have to seek specially to
1483 * If the time is after the end of the trace, return ERANGE.
1485 * Do a binary search to find the right block, then a sequential search in the
1486 * block to find the event.
1488 * In the special case where the time requested fits inside a block that has no
1489 * event corresponding to the requested time, the first event of the next block
1492 * IMPORTANT NOTE : // FIXME everywhere...
1494 * You MUST NOT do a ltt_tracefile_read right after a ltt_tracefile_seek_time :
1495 * you will jump over an event if you do.
1497 * Return value : 0 : no error, the tf->event can be used
1498 * ERANGE : time if after the last event of the trace
1499 * otherwise : this is an error.
1503 int ltt_tracefile_seek_time(LttTracefile
*tf
, LttTime time
)
1507 unsigned int block_num
, high
, low
;
1509 /* seek at the beginning of trace */
1510 err
= map_block(tf
, 0); /* First block */
1512 g_error("Can not map block");
1516 /* If the time is lower or equal the beginning of the trace,
1517 * go to the first event. */
1518 if(ltt_time_compare(time
, tf
->buffer
.begin
.timestamp
) <= 0) {
1519 ret
= ltt_tracefile_read(tf
);
1520 if(ret
== ERANGE
) goto range
;
1521 else if (ret
) goto fail
;
1522 goto found
; /* There is either no event in the trace or the event points
1523 to the first event in the trace */
1526 err
= map_block(tf
, tf
->num_blocks
- 1); /* Last block */
1528 g_error("Can not map block");
1532 /* If the time is after the end of the trace, return ERANGE. */
1533 if(ltt_time_compare(time
, tf
->buffer
.end
.timestamp
) > 0) {
1537 /* Binary search the block */
1538 high
= tf
->num_blocks
- 1;
1542 block_num
= ((high
-low
) / 2) + low
;
1544 err
= map_block(tf
, block_num
);
1546 g_error("Can not map block");
1550 /* We cannot divide anymore : this is what would happen if the time
1551 * requested was exactly between two consecutive buffers'end and start
1552 * timestamps. This is also what would happend if we didn't deal with out
1553 * of span cases prior in this function. */
1554 /* The event is right in the buffer!
1555 * (or in the next buffer first event) */
1557 ret
= ltt_tracefile_read(tf
);
1558 if(ret
== ERANGE
) goto range
; /* ERANGE or EPERM */
1559 else if(ret
) goto fail
;
1561 if(ltt_time_compare(time
, tf
->event
.event_time
) <= 0)
1565 } else if(ltt_time_compare(time
, tf
->buffer
.begin
.timestamp
) < 0) {
1566 /* go to lower part */
1567 high
= block_num
- 1;
1568 } else if(ltt_time_compare(time
, tf
->buffer
.end
.timestamp
) > 0) {
1569 /* go to higher part */
1570 low
= block_num
+ 1;
1571 } else {/* The event is right in the buffer!
1572 (or in the next buffer first event) */
1574 ret
= ltt_tracefile_read(tf
);
1575 if(ret
== ERANGE
) goto range
; /* ERANGE or EPERM */
1576 else if(ret
) goto fail
;
1578 if(ltt_time_compare(time
, tf
->event
.event_time
) <= 0)
1590 /* Error handling */
1592 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1593 g_quark_to_string(tf
->name
));
1598 int ltt_tracefile_seek_position(LttTracefile
*tf
, const LttEventPosition
*ep
) {
1602 if(ep
->tracefile
!= tf
) {
1606 err
= map_block(tf
, ep
->block
);
1608 g_error("Can not map block");
1612 tf
->event
.offset
= ep
->offset
;
1614 /* Put back the event real tsc */
1615 tf
->event
.tsc
= ep
->tsc
;
1616 tf
->buffer
.tsc
= ep
->tsc
;
1618 err
= ltt_tracefile_read_update_event(tf
);
1620 err
= ltt_tracefile_read_op(tf
);
1626 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1627 g_quark_to_string(tf
->name
));
1631 LttTime
ltt_interpolate_time_from_tsc(LttTracefile
*tf
, guint64 tsc
)
1635 if(tsc
> tf
->trace
->start_tsc
) {
1636 time
= ltt_time_from_uint64(
1637 (double)(tsc
- tf
->trace
->start_tsc
)
1638 * (1000000000.0 / tf
->trace
->freq_scale
)
1639 / (double)tf
->trace
->start_freq
);
1640 time
= ltt_time_add(tf
->trace
->start_time_from_tsc
, time
);
1642 time
= ltt_time_from_uint64(
1643 (double)(tf
->trace
->start_tsc
- tsc
)
1644 * (1000000000.0 / tf
->trace
->freq_scale
)
1645 / (double)tf
->trace
->start_freq
);
1646 time
= ltt_time_sub(tf
->trace
->start_time_from_tsc
, time
);
1651 /* Calculate the real event time based on the buffer boundaries */
1652 LttTime
ltt_interpolate_time(LttTracefile
*tf
, LttEvent
*event
)
1654 return ltt_interpolate_time_from_tsc(tf
, tf
->buffer
.tsc
);
1658 /* Get the current event of the tracefile : valid until the next read */
1659 LttEvent
*ltt_tracefile_get_event(LttTracefile
*tf
)
1666 /*****************************************************************************
1668 * ltt_tracefile_read : Read the next event in the tracefile
1673 * Returns 0 if an event can be used in tf->event.
1674 * Returns ERANGE on end of trace. The event in tf->event still can be used
1675 * (if the last block was not empty).
1676 * Returns EPERM on error.
1678 * This function does make the tracefile event structure point to the event
1679 * currently pointed to by the tf->event.
1681 * Note : you must call a ltt_tracefile_seek to the beginning of the trace to
1682 * reinitialize it after an error if you want results to be coherent.
1683 * It would be the case if a end of trace last buffer has no event : the end
1684 * of trace wouldn't be returned, but an error.
1685 * We make the assumption there is at least one event per buffer.
1686 ****************************************************************************/
1688 int ltt_tracefile_read(LttTracefile
*tf
)
1692 err
= ltt_tracefile_read_seek(tf
);
1694 err
= ltt_tracefile_read_update_event(tf
);
1696 err
= ltt_tracefile_read_op(tf
);
1702 int ltt_tracefile_read_seek(LttTracefile
*tf
)
1706 /* Get next buffer until we finally have an event, or end of trace */
1708 err
= ltt_seek_next_event(tf
);
1709 if(unlikely(err
== ENOPROTOOPT
)) {
1713 /* Are we at the end of the buffer ? */
1715 if(unlikely(tf
->buffer
.index
== tf
->num_blocks
-1)){ /* end of trace ? */
1718 /* get next block */
1719 err
= map_block(tf
, tf
->buffer
.index
+ 1);
1721 g_error("Can not map block");
1725 } else break; /* We found an event ! */
1732 /* do specific operation on events */
1733 int ltt_tracefile_read_op(LttTracefile
*tf
)
1739 /* do event specific operation */
1741 /* do something if its an heartbeat event : increment the heartbeat count */
1742 //if(event->facility_id == LTT_FACILITY_CORE)
1743 // if(event->event_id == LTT_EVENT_HEARTBEAT)
1744 // tf->cur_heart_beat_number++;
1749 static void print_debug_event_header(LttEvent
*ev
, void *start_pos
, void *end_pos
)
1751 unsigned int offset
= 0;
1754 g_printf("Event header (tracefile %s offset %llx):\n",
1755 g_quark_to_string(ev
->tracefile
->long_name
),
1756 ((uint64_t)ev
->tracefile
->buffer
.index
* ev
->tracefile
->buf_size
)
1757 + (long)start_pos
- (long)ev
->tracefile
->buffer
.head
);
1759 while (offset
< (long)end_pos
- (long)start_pos
) {
1760 g_printf("%8lx", (long)start_pos
- (long)ev
->tracefile
->buffer
.head
+ offset
);
1763 for (i
= 0; i
< 4 ; i
++) {
1764 for (j
= 0; j
< 4; j
++) {
1765 if (offset
+ ((i
* 4) + j
) <
1766 (long)end_pos
- (long)start_pos
)
1768 ((char*)start_pos
)[offset
+ ((i
* 4) + j
)]);
1782 /* same as ltt_tracefile_read, but does not seek to the next event nor call
1783 * event specific operation. */
1784 int ltt_tracefile_read_update_event(LttTracefile
*tf
)
1791 pos
= tf
->buffer
.head
+ event
->offset
;
1793 /* Read event header */
1795 /* Align the head */
1797 pos
+= ltt_align((size_t)pos
, tf
->trace
->arch_size
, tf
->alignment
);
1799 g_assert(tf
->has_heartbeat
);
1800 pos
+= ltt_align((size_t)pos
, sizeof(uint32_t), tf
->alignment
);
1804 if(tf
->has_heartbeat
) {
1805 event
->timestamp
= ltt_get_uint32(LTT_GET_BO(tf
),
1808 /* 32 bits -> 64 bits tsc */
1809 /* note : still works for seek and non seek cases. */
1810 if(event
->timestamp
< (0xFFFFFFFFULL
&tf
->buffer
.tsc
)) {
1811 tf
->buffer
.tsc
= ((tf
->buffer
.tsc
&0xFFFFFFFF00000000ULL
)
1813 | (guint64
)event
->timestamp
;
1814 event
->tsc
= tf
->buffer
.tsc
;
1817 tf
->buffer
.tsc
= (tf
->buffer
.tsc
&0xFFFFFFFF00000000ULL
)
1818 | (guint64
)event
->timestamp
;
1819 event
->tsc
= tf
->buffer
.tsc
;
1820 event
->compact_data
= 0;
1823 /* Compact header */
1824 /* We keep the LSB of the previous timestamp, to make sure
1825 * we never go back */
1826 event
->event_id
= event
->timestamp
>> tf
->tscbits
;
1827 event
->event_id
= event
->event_id
& ((1 << tf
->compact_event_bits
) - 1);
1828 event
->compact_data
= event
->timestamp
>>
1829 (tf
->compact_event_bits
+ tf
->tscbits
);
1830 //printf("tsc bits %u, ev bits %u init data %u\n",
1831 // tf->tscbits, tf->trace->compact_event_bits, event->compact_data);
1832 /* Put the compact data back in original endianness */
1833 event
->compact_data
= ltt_get_uint32(LTT_GET_BO(tf
), &event
->compact_data
);
1834 event
->event_size
= 0xFFFF;
1835 //printf("Found compact event %d\n", event->event_id);
1836 //printf("Compact data %d\n", event->compact_data);
1837 event
->timestamp
= event
->timestamp
<< tf
->tsc_lsb_truncate
;
1838 event
->timestamp
= event
->timestamp
& tf
->tsc_mask
;
1839 //printf("timestamp 0x%lX\n", event->timestamp);
1840 //printf("mask 0x%llX\n", tf->tsc_mask);
1841 //printf("mask_next 0x%llX\n", tf->tsc_mask_next_bit);
1842 //printf("previous tsc 0x%llX\n", tf->buffer.tsc);
1843 //printf("previous tsc&mask 0x%llX\n", tf->tsc_mask&tf->buffer.tsc);
1844 //printf("previous tsc&(~mask) 0x%llX\n", tf->buffer.tsc&(~tf->tsc_mask));
1845 if(event
->timestamp
< (tf
->tsc_mask
&tf
->buffer
.tsc
)) {
1847 tf
->buffer
.tsc
= ((tf
->buffer
.tsc
&(~tf
->tsc_mask
))
1848 + tf
->tsc_mask_next_bit
)
1849 | (guint64
)event
->timestamp
;
1850 event
->tsc
= tf
->buffer
.tsc
;
1852 //printf("no wrap\n");
1854 tf
->buffer
.tsc
= (tf
->buffer
.tsc
&(~tf
->tsc_mask
))
1855 | (guint64
)event
->timestamp
;
1856 event
->tsc
= tf
->buffer
.tsc
;
1858 //printf("current tsc 0x%llX\n", tf->buffer.tsc);
1860 pos
+= sizeof(guint32
);
1862 event
->tsc
= ltt_get_uint64(LTT_GET_BO(tf
), pos
);
1863 tf
->buffer
.tsc
= event
->tsc
;
1864 event
->compact_data
= 0;
1865 pos
+= sizeof(guint64
);
1867 event
->event_time
= ltt_interpolate_time(tf
, event
);
1870 event
->event_id
= *(guint16
*)pos
;
1871 pos
+= sizeof(guint16
);
1873 event
->event_size
= ltt_get_uint16(LTT_GET_BO(tf
), pos
);
1874 pos
+= sizeof(guint16
);
1880 print_debug_event_header(event
, pos_aligned
, pos
);
1882 /* Align the head */
1884 pos
+= ltt_align((size_t)pos
, tf
->trace
->arch_size
, tf
->alignment
);
1888 /* get the data size and update the event fields with the current
1889 * information. Also update the time if a heartbeat_full event is found. */
1890 ltt_update_event_size(tf
);
1896 /****************************************************************************
1898 * map_block : map a block from the file
1900 * lttdes : ltt trace file
1901 * whichBlock : the block which will be read
1904 * EINVAL : lseek fail
1905 * EIO : can not read from the file
1906 ****************************************************************************/
1908 gint
map_block(LttTracefile
* tf
, guint block_num
)
1910 int page_size
= getpagesize();
1911 struct ltt_block_start_header
*header
;
1913 g_assert(block_num
< tf
->num_blocks
);
1915 if(tf
->buffer
.head
!= NULL
) {
1916 if(munmap(tf
->buffer
.head
, PAGE_ALIGN(tf
->buf_size
))) {
1917 g_warning("unmap size : %u\n",
1918 PAGE_ALIGN(tf
->buf_size
));
1919 perror("munmap error");
1925 /* Multiple of pages aligned head */
1926 tf
->buffer
.head
= mmap(0,
1927 PAGE_ALIGN(tf
->buf_size
),
1928 PROT_READ
, MAP_PRIVATE
, tf
->fd
,
1929 PAGE_ALIGN((off_t
)tf
->buf_size
* (off_t
)block_num
));
1931 if(tf
->buffer
.head
== MAP_FAILED
) {
1932 perror("Error in allocating memory for buffer of tracefile");
1936 g_assert( ( (guint
)tf
->buffer
.head
&(8-1) ) == 0); // make sure it's aligned.
1939 tf
->buffer
.index
= block_num
;
1941 header
= (struct ltt_block_start_header
*)tf
->buffer
.head
;
1944 tf
->buffer
.begin
.timestamp
= ltt_time_add(
1945 ltt_time_from_uint64(
1946 ltt_get_uint64(LTT_GET_BO(tf
),
1947 &header
->begin
.timestamp
)
1948 - tf
->trace
->start_monotonic
),
1949 tf
->trace
->start_time
);
1951 //g_debug("block %u begin : %lu.%lu", block_num,
1952 // tf->buffer.begin.timestamp.tv_sec, tf->buffer.begin.timestamp.tv_nsec);
1953 tf
->buffer
.begin
.cycle_count
= ltt_get_uint64(LTT_GET_BO(tf
),
1954 &header
->begin
.cycle_count
);
1955 tf
->buffer
.begin
.freq
= ltt_get_uint64(LTT_GET_BO(tf
),
1956 &header
->begin
.freq
);
1957 if(tf
->buffer
.begin
.freq
== 0)
1958 tf
->buffer
.begin
.freq
= tf
->trace
->start_freq
;
1960 tf
->buffer
.begin
.timestamp
= ltt_interpolate_time_from_tsc(tf
,
1961 tf
->buffer
.begin
.cycle_count
);
1964 ltt_time_from_uint64(
1965 (double)(tf
->buffer
.begin
.cycle_count
1966 - tf
->trace
->start_tsc
) * 1000000.0
1967 / (double)tf
->trace
->start_freq
),
1968 tf
->trace
->start_time_from_tsc
);
1972 tf
->buffer
.end
.timestamp
= ltt_time_add(
1973 ltt_time_from_uint64(
1974 ltt_get_uint64(LTT_GET_BO(tf
),
1975 &header
->end
.timestamp
)
1976 - tf
->trace
->start_monotonic
),
1977 tf
->trace
->start_time
);
1979 //g_debug("block %u end : %lu.%lu", block_num,
1980 // tf->buffer.end.timestamp.tv_sec, tf->buffer.end.timestamp.tv_nsec);
1981 tf
->buffer
.end
.cycle_count
= ltt_get_uint64(LTT_GET_BO(tf
),
1982 &header
->end
.cycle_count
);
1983 tf
->buffer
.end
.freq
= ltt_get_uint64(LTT_GET_BO(tf
),
1985 if(tf
->buffer
.end
.freq
== 0)
1986 tf
->buffer
.end
.freq
= tf
->trace
->start_freq
;
1988 tf
->buffer
.lost_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1989 &header
->lost_size
);
1990 tf
->buffer
.end
.timestamp
= ltt_interpolate_time_from_tsc(tf
,
1991 tf
->buffer
.end
.cycle_count
);
1994 ltt_time_from_uint64(
1995 (double)(tf
->buffer
.end
.cycle_count
1996 - tf
->trace
->start_tsc
) * 1000000.0
1997 / (double)tf
->trace
->start_freq
),
1998 tf
->trace
->start_time_from_tsc
);
2000 tf
->buffer
.tsc
= tf
->buffer
.begin
.cycle_count
;
2001 tf
->event
.tsc
= tf
->buffer
.tsc
;
2002 tf
->buffer
.freq
= tf
->buffer
.begin
.freq
;
2005 * eventually support variable buffer size : will need a partial pre-read of
2006 * the headers to create an index when we open the trace... eventually. */
2007 g_assert(tf
->buf_size
== ltt_get_uint32(LTT_GET_BO(tf
),
2008 &header
->buf_size
));
2010 /* Now that the buffer is mapped, calculate the time interpolation for the
2013 // tf->buffer.nsecs_per_cycle = calc_nsecs_per_cycle(tf);
2014 //tf->buffer.cyc2ns_scale = calc_nsecs_per_cycle(tf);
2016 /* Make the current event point to the beginning of the buffer :
2017 * it means that the event read must get the first event. */
2018 tf
->event
.tracefile
= tf
;
2019 tf
->event
.block
= block_num
;
2020 tf
->event
.offset
= 0;
2029 static void print_debug_event_data(LttEvent
*ev
)
2031 unsigned int offset
= 0;
2034 if (!max(ev
->event_size
, ev
->data_size
))
2037 g_printf("Event data (tracefile %s offset %llx):\n",
2038 g_quark_to_string(ev
->tracefile
->long_name
),
2039 ((uint64_t)ev
->tracefile
->buffer
.index
* ev
->tracefile
->buf_size
)
2040 + (long)ev
->data
- (long)ev
->tracefile
->buffer
.head
);
2042 while (offset
< max(ev
->event_size
, ev
->data_size
)) {
2043 g_printf("%8lx", (long)ev
->data
+ offset
2044 - (long)ev
->tracefile
->buffer
.head
);
2047 for (i
= 0; i
< 4 ; i
++) {
2048 for (j
= 0; j
< 4; j
++) {
2049 if (offset
+ ((i
* 4) + j
) < max(ev
->event_size
, ev
->data_size
))
2050 g_printf("%02hhX", ((char*)ev
->data
)[offset
+ ((i
* 4) + j
)]);
2061 for (i
= 0; i
< 4; i
++) {
2062 for (j
= 0; j
< 4; j
++) {
2063 if (offset
+ ((i
* 4) + j
) < max(ev
->event_size
, ev
->data_size
)) {
2064 if (isprint(((char*)ev
->data
)[offset
+ ((i
* 4) + j
)]))
2065 g_printf("%c", ((char*)ev
->data
)[offset
+ ((i
* 4) + j
)]);
2077 /* It will update the fields offsets too */
2078 void ltt_update_event_size(LttTracefile
*tf
)
2082 struct marker_info
*info
;
2084 switch((enum marker_id
)tf
->event
.event_id
) {
2085 case MARKER_ID_SET_MARKER_ID
:
2086 size
= strlen((char*)tf
->event
.data
) + 1;
2087 g_debug("marker %s id set", (char*)tf
->event
.data
);
2088 size
+= ltt_align(size
, sizeof(guint16
), tf
->alignment
);
2089 size
+= sizeof(guint16
);
2090 size
+= sizeof(guint8
);
2091 size
+= sizeof(guint8
);
2092 size
+= sizeof(guint8
);
2093 size
+= sizeof(guint8
);
2094 size
+= sizeof(guint8
);
2096 case MARKER_ID_SET_MARKER_FORMAT
:
2097 g_debug("marker %s format set", (char*)tf
->event
.data
);
2098 size
= strlen((char*)tf
->event
.data
) + 1;
2099 size
+= strlen((char*)tf
->event
.data
+ size
) + 1;
2101 case MARKER_ID_HEARTBEAT_32
:
2102 g_debug("Update Event heartbeat 32 bits");
2104 case MARKER_ID_HEARTBEAT_64
:
2105 g_debug("Update Event heartbeat 64 bits");
2106 tscdata
= (char*)(tf
->event
.data
);
2107 tf
->event
.tsc
= ltt_get_uint64(LTT_GET_BO(tf
), tscdata
);
2108 tf
->buffer
.tsc
= tf
->event
.tsc
;
2109 tf
->event
.event_time
= ltt_interpolate_time(tf
, &tf
->event
);
2110 size
= ltt_align(size
, sizeof(guint64
), tf
->alignment
);
2111 size
+= sizeof(guint64
);
2115 info
= marker_get_info_from_id(tf
->trace
, tf
->event
.event_id
);
2116 if (tf
->event
.event_id
>= MARKER_CORE_IDS
)
2117 g_assert(info
!= NULL
);
2119 /* Do not update field offsets of core markers when initially reading the
2120 * facility tracefile when the infos about these markers do not exist yet.
2122 if (likely(info
&& info
->fields
)) {
2123 if (info
->size
!= -1)
2126 size
= marker_update_fields_offsets(marker_get_info_from_id(tf
->trace
,
2127 tf
->event
.event_id
), tf
->event
.data
);
2130 tf
->event
.data_size
= size
;
2132 /* Check consistency between kernel and LTTV structure sizes */
2133 if(tf
->event
.event_size
== 0xFFFF) {
2134 /* Event size too big to fit in the event size field */
2135 tf
->event
.event_size
= tf
->event
.data_size
;
2139 print_debug_event_data(&tf
->event
);
2141 /* Having a marker load or marker format event out of the facilities
2142 * tracefiles is a serious bug. */
2143 switch((enum marker_id
)tf
->event
.event_id
) {
2144 case MARKER_ID_SET_MARKER_ID
:
2145 case MARKER_ID_SET_MARKER_FORMAT
:
2146 if (tf
->name
!= g_quark_from_string("/control/facilities"))
2147 g_error("Trace inconsistency : metadata event found in data "
2148 "tracefile %s", g_quark_to_string(tf
->long_name
));
2151 if (tf
->event
.data_size
!= tf
->event
.event_size
) {
2152 struct marker_info
*info
= marker_get_info_from_id(tf
->trace
,
2153 tf
->event
.event_id
);
2154 g_error("Kernel/LTTV event size differs for event %s: kernel %u, LTTV %u",
2155 g_quark_to_string(info
->name
),
2156 tf
->event
.event_size
, tf
->event
.data_size
);
2161 LttEventType
*event_type
=
2162 ltt_facility_eventtype_get(f
, tf
->event
.event_id
);
2165 g_warning("Unknown event id %hhu in facility %s in tracefile %s",
2167 g_quark_to_string(f
->name
),
2168 g_quark_to_string(tf
->name
));
2169 goto event_type_error
;
2172 /* Compute the dynamic offsets */
2173 compute_offsets(tf
, f
, event_type
, &size
, tf
->event
.data
);
2175 //g_debug("Event root field : f.e %hhu.%hhu size %zd",
2176 // tf->event.facility_id,
2177 // tf->event.event_id, size);
2180 tf
->event
.data_size
= size
;
2182 /* Check consistency between kernel and LTTV structure sizes */
2183 if(tf
->event
.event_size
== 0xFFFF) {
2184 /* Event size too big to fit in the event size field */
2185 tf
->event
.event_size
= tf
->event
.data_size
;
2187 if (tf
->event
.data_size
!= tf
->event
.event_size
) {
2188 g_error("Kernel/LTTV event size differs for event %s.%s: kernel %u, LTTV %u",
2189 g_quark_to_string(f
->name
), g_quark_to_string(event_type
->name
),
2190 tf
->event
.event_size
, tf
->event
.data_size
);
2193 //g_assert(tf->event.data_size == tf->event.event_size);
2199 if(tf
->event
.event_size
== 0xFFFF) {
2200 g_error("Cannot jump over an unknown event bigger than 0xFFFE bytes");
2202 /* The facility is unknown : use the kernel information about this event
2203 * to jump over it. */
2204 tf
->event
.data_size
= tf
->event
.event_size
;
2209 /* Take the tf current event offset and use the event facility id and event id
2210 * to figure out where is the next event offset.
2212 * This is an internal function not aiming at being used elsewhere : it will
2213 * not jump over the current block limits. Please consider using
2214 * ltt_tracefile_read to do this.
2216 * Returns 0 on success
2217 * ERANGE if we are at the end of the buffer.
2218 * ENOPROTOOPT if an error occured when getting the current event size.
2220 int ltt_seek_next_event(LttTracefile
*tf
)
2225 /* seek over the buffer header if we are at the buffer start */
2226 if(tf
->event
.offset
== 0) {
2227 tf
->event
.offset
+= tf
->buffer_header_size
;
2229 if(tf
->event
.offset
== tf
->buf_size
- tf
->buffer
.lost_size
) {
2236 pos
= tf
->event
.data
;
2238 if(tf
->event
.data_size
< 0) goto error
;
2240 pos
+= (size_t)tf
->event
.data_size
;
2242 tf
->event
.offset
= pos
- tf
->buffer
.head
;
2244 if(tf
->event
.offset
== tf
->buf_size
- tf
->buffer
.lost_size
) {
2248 g_assert(tf
->event
.offset
< tf
->buf_size
- tf
->buffer
.lost_size
);
2254 g_error("Error in ltt_seek_next_event for tracefile %s",
2255 g_quark_to_string(tf
->name
));
2260 /*****************************************************************************
2262 * calc_nsecs_per_cycle : calculate nsecs per cycle for current block
2264 * 1.0 / (freq(khz) *1000) * 1000000000
2267 ****************************************************************************/
2268 /* from timer_tsc.c */
2269 #define CYC2NS_SCALE_FACTOR 10
2270 static guint32
calc_nsecs_per_cycle(LttTracefile
* tf
)
2272 //return 1e6 / (double)tf->buffer.freq;
2273 guint32 cpu_mhz
= tf
->buffer
.freq
/ 1000;
2274 guint32 cyc2ns_scale
= (1000 << CYC2NS_SCALE_FACTOR
)/cpu_mhz
;
2276 return cyc2ns_scale
;
2277 // return 1e6 / (double)tf->buffer.freq;
2280 static guint64
cycles_2_ns(LttTracefile
*tf
, guint64 cycles
)
2282 return (cycles
* tf
->buffer
.cyc2ns_scale
) >> CYC2NS_SCALE_FACTOR
;
2287 void setFieldsOffset(LttTracefile
*tf
, LttEventType
*evT
,void *evD
)
2289 LttField
* rootFld
= evT
->root_field
;
2290 // rootFld->base_address = evD;
2293 rootFld
->field_size
= getFieldtypeSize(tf
, evT
->facility
,
2294 evT
, 0,0,rootFld
, evD
);
2298 /*****************************************************************************
2300 * set_fields_offsets : set the precomputable offset of the fields
2302 * tracefile : opened trace file
2303 * event_type : the event type
2304 ****************************************************************************/
2306 void set_fields_offsets(LttTracefile
*tf
, LttEventType
*event_type
)
2308 LttField
*field
= event_type
->root_field
;
2309 enum field_status fixed_root
= FIELD_FIXED
, fixed_parent
= FIELD_FIXED
;
2312 preset_field_type_size(tf
, event_type
, 0, 0,
2313 &fixed_root
, &fixed_parent
,
2320 /*****************************************************************************
2322 * get_alignment : Get the alignment needed for a field.
2326 * returns : The size on which it must be aligned.
2328 ****************************************************************************/
2330 off_t
get_alignment(LttField
*field
)
2332 LttType
*type
= &field
->field_type
;
2334 switch(type
->type_class
) {
2336 case LTT_UINT_FIXED
:
2351 /* Align offset on type size */
2352 g_assert(field
->field_size
!= 0);
2353 return field
->field_size
;
2359 g_assert(type
->fields
->len
== 1);
2361 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
2362 return get_alignment(child
);
2366 g_assert(type
->fields
->len
== 2);
2369 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
2371 localign
= max(localign
, get_alignment(child
));
2373 child
= &g_array_index(type
->fields
, LttField
, 1);
2374 localign
= max(localign
, get_alignment(child
));
2385 for(i
=0; i
<type
->fields
->len
; i
++) {
2386 LttField
*child
= &g_array_index(type
->fields
, LttField
, i
);
2387 localign
= max(localign
, get_alignment(child
));
2394 g_error("get_alignment : unknown type");
2401 /*****************************************************************************
2403 * field_compute_static_size : Determine the size of fields known by their
2404 * sole definition. Unions, arrays and struct sizes might be known, but
2405 * the parser does not give that information.
2410 ****************************************************************************/
2412 void field_compute_static_size(LttFacility
*fac
, LttField
*field
)
2414 LttType
*type
= &field
->field_type
;
2416 switch(type
->type_class
) {
2418 case LTT_UINT_FIXED
:
2437 /* note this : array type size is the number of elements in the array,
2438 * while array field size of the length of the array in bytes */
2439 g_assert(type
->fields
->len
== 1);
2441 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
2442 field_compute_static_size(fac
, child
);
2444 if(child
->field_size
!= 0) {
2445 field
->field_size
= type
->size
* child
->field_size
;
2446 field
->dynamic_offsets
= g_array_sized_new(FALSE
, TRUE
,
2447 sizeof(off_t
), type
->size
);
2449 field
->field_size
= 0;
2454 g_assert(type
->fields
->len
== 2);
2456 off_t local_offset
= 0;
2457 LttField
*child
= &g_array_index(type
->fields
, LttField
, 1);
2458 field_compute_static_size(fac
, child
);
2459 field
->field_size
= 0;
2461 if(child
->field_size
!= 0) {
2462 field
->dynamic_offsets
= g_array_sized_new(FALSE
, TRUE
,
2463 sizeof(off_t
), SEQUENCE_AVG_ELEMENTS
);
2471 for(i
=0;i
<type
->fields
->len
;i
++) {
2472 LttField
*child
= &g_array_index(type
->fields
, LttField
, i
);
2473 field_compute_static_size(fac
, child
);
2474 if(child
->field_size
!= 0) {
2475 type
->size
+= ltt_align(type
->size
, get_alignment(child
),
2477 type
->size
+= child
->field_size
;
2479 /* As soon as we find a child with variable size, we have
2480 * a variable size */
2485 field
->field_size
= type
->size
;
2489 g_error("field_static_size : unknown type");
2496 /*****************************************************************************
2498 * precompute_fields_offsets : set the precomputable offset of the fields
2502 * offset : pointer to the current offset, must be incremented
2504 * return : 1 : found a variable length field, stop the processing.
2506 ****************************************************************************/
2509 gint
precompute_fields_offsets(LttFacility
*fac
, LttField
*field
, off_t
*offset
, gint is_compact
)
2511 LttType
*type
= &field
->field_type
;
2513 if(unlikely(is_compact
)) {
2514 g_assert(field
->field_size
!= 0);
2515 /* FIXME THIS IS A HUUUUUGE hack :
2516 * offset is between the compact_data field in struct LttEvent
2517 * and the address of the field root in the memory map.
2518 * ark. Both will stay at the same addresses while the event
2519 * is readable, so it's ok.
2521 field
->offset_root
= 0;
2522 field
->fixed_root
= FIELD_FIXED
;
2526 switch(type
->type_class
) {
2528 case LTT_UINT_FIXED
:
2543 g_assert(field
->field_size
!= 0);
2544 /* Align offset on type size */
2545 *offset
+= ltt_align(*offset
, get_alignment(field
),
2547 /* remember offset */
2548 field
->offset_root
= *offset
;
2549 field
->fixed_root
= FIELD_FIXED
;
2550 /* Increment offset */
2551 *offset
+= field
->field_size
;
2555 field
->offset_root
= *offset
;
2556 field
->fixed_root
= FIELD_FIXED
;
2560 g_assert(type
->fields
->len
== 1);
2562 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
2564 *offset
+= ltt_align(*offset
, get_alignment(field
),
2567 /* remember offset */
2568 field
->offset_root
= *offset
;
2569 field
->array_offset
= *offset
;
2570 field
->fixed_root
= FIELD_FIXED
;
2572 /* Let the child be variable */
2573 //precompute_fields_offsets(tf, child, offset);
2575 if(field
->field_size
!= 0) {
2576 /* Increment offset */
2577 /* field_size is the array size in bytes */
2578 *offset
+= field
->field_size
;
2586 g_assert(type
->fields
->len
== 2);
2591 *offset
+= ltt_align(*offset
, get_alignment(field
),
2594 /* remember offset */
2595 field
->offset_root
= *offset
;
2596 field
->fixed_root
= FIELD_FIXED
;
2598 child
= &g_array_index(type
->fields
, LttField
, 0);
2599 ret
= precompute_fields_offsets(fac
, child
, offset
, is_compact
);
2600 g_assert(ret
== 0); /* Seq len cannot have variable len */
2602 child
= &g_array_index(type
->fields
, LttField
, 1);
2603 *offset
+= ltt_align(*offset
, get_alignment(child
),
2605 field
->array_offset
= *offset
;
2606 /* Let the child be variable. */
2607 //ret = precompute_fields_offsets(fac, child, offset);
2609 /* Cannot precompute fields offsets of sequence members, and has
2610 * variable length. */
2620 *offset
+= ltt_align(*offset
, get_alignment(field
),
2622 /* remember offset */
2623 field
->offset_root
= *offset
;
2624 field
->fixed_root
= FIELD_FIXED
;
2626 for(i
=0; i
< type
->fields
->len
; i
++) {
2627 child
= &g_array_index(type
->fields
, LttField
, i
);
2628 ret
= precompute_fields_offsets(fac
, child
, offset
, is_compact
);
2641 *offset
+= ltt_align(*offset
, get_alignment(field
),
2643 /* remember offset */
2644 field
->offset_root
= *offset
;
2645 field
->fixed_root
= FIELD_FIXED
;
2647 for(i
=0; i
< type
->fields
->len
; i
++) {
2648 *offset
= field
->offset_root
;
2649 child
= &g_array_index(type
->fields
, LttField
, i
);
2650 ret
= precompute_fields_offsets(fac
, child
, offset
, is_compact
);
2654 *offset
= field
->offset_root
+ field
->field_size
;
2661 g_error("precompute_fields_offsets : unknown type");
2670 /*****************************************************************************
2672 * precompute_offsets : set the precomputable offset of an event type
2675 * event : event type
2677 ****************************************************************************/
2678 void precompute_offsets(LttFacility
*fac
, LttEventType
*event
)
2684 /* First, compute the size of fixed size fields. Will determine size for
2685 * arrays, struct and unions, which is not done by the parser */
2686 for(i
=0; i
<event
->fields
->len
; i
++) {
2687 LttField
*field
= &g_array_index(event
->fields
, LttField
, i
);
2688 field_compute_static_size(fac
, field
);
2691 /* Precompute all known offsets */
2692 for(i
=0; i
<event
->fields
->len
; i
++) {
2693 LttField
*field
= &g_array_index(event
->fields
, LttField
, i
);
2694 if(event
->has_compact_data
&& i
== 0)
2695 ret
= precompute_fields_offsets(fac
, field
, &offset
, 1);
2697 ret
= precompute_fields_offsets(fac
, field
, &offset
, 0);
2705 /*****************************************************************************
2707 * preset_field_type_size : set the fixed sizes of the field type
2710 * event_type : event type
2711 * offset_root : offset from the root
2712 * offset_parent : offset from the parent
2713 * fixed_root : Do we know a fixed offset to the root ?
2714 * fixed_parent : Do we know a fixed offset to the parent ?
2716 ****************************************************************************/
2720 // preset the fixed size offsets. Calculate them just like genevent-new : an
2721 // increment of a *to value that represents the offset from the start of the
2723 // The preset information is : offsets up to (and including) the first element
2724 // of variable size. All subsequent fields must be flagged "VARIABLE OFFSET".
2726 void preset_field_type_size(LttTracefile
*tf
, LttEventType
*event_type
,
2727 off_t offset_root
, off_t offset_parent
,
2728 enum field_status
*fixed_root
, enum field_status
*fixed_parent
,
2731 enum field_status local_fixed_root
, local_fixed_parent
;
2735 g_assert(field
->fixed_root
== FIELD_UNKNOWN
);
2736 g_assert(field
->fixed_parent
== FIELD_UNKNOWN
);
2737 g_assert(field
->fixed_size
== FIELD_UNKNOWN
);
2739 type
= field
->field_type
;
2741 field
->fixed_root
= *fixed_root
;
2742 if(field
->fixed_root
== FIELD_FIXED
)
2743 field
->offset_root
= offset_root
;
2745 field
->offset_root
= 0;
2747 field
->fixed_parent
= *fixed_parent
;
2748 if(field
->fixed_parent
== FIELD_FIXED
)
2749 field
->offset_parent
= offset_parent
;
2751 field
->offset_parent
= 0;
2753 size_t current_root_offset
;
2754 size_t current_offset
;
2755 enum field_status current_child_status
, final_child_status
;
2758 switch(type
->type_class
) {
2760 case LTT_UINT_FIXED
:
2769 field
->field_size
= ltt_type_size(tf
->trace
, type
);
2770 field
->fixed_size
= FIELD_FIXED
;
2773 field
->field_size
= (off_t
)event_type
->facility
->pointer_size
;
2774 field
->fixed_size
= FIELD_FIXED
;
2778 field
->field_size
= (off_t
)event_type
->facility
->long_size
;
2779 field
->fixed_size
= FIELD_FIXED
;
2784 field
->field_size
= (off_t
)event_type
->facility
->size_t_size
;
2785 field
->fixed_size
= FIELD_FIXED
;
2788 local_fixed_root
= FIELD_VARIABLE
;
2789 local_fixed_parent
= FIELD_VARIABLE
;
2790 preset_field_type_size(tf
, event_type
,
2792 &local_fixed_root
, &local_fixed_parent
,
2794 field
->fixed_size
= FIELD_VARIABLE
;
2795 field
->field_size
= 0;
2796 *fixed_root
= FIELD_VARIABLE
;
2797 *fixed_parent
= FIELD_VARIABLE
;
2800 field
->fixed_size
= FIELD_VARIABLE
;
2801 field
->field_size
= 0;
2802 *fixed_root
= FIELD_VARIABLE
;
2803 *fixed_parent
= FIELD_VARIABLE
;
2806 local_fixed_root
= FIELD_VARIABLE
;
2807 local_fixed_parent
= FIELD_VARIABLE
;
2808 preset_field_type_size(tf
, event_type
,
2810 &local_fixed_root
, &local_fixed_parent
,
2812 field
->fixed_size
= field
->child
[0]->fixed_size
;
2813 if(field
->fixed_size
== FIELD_FIXED
) {
2814 field
->field_size
= type
->element_number
* field
->child
[0]->field_size
;
2816 field
->field_size
= 0;
2817 *fixed_root
= FIELD_VARIABLE
;
2818 *fixed_parent
= FIELD_VARIABLE
;
2822 current_root_offset
= field
->offset_root
;
2824 current_child_status
= FIELD_FIXED
;
2825 for(i
=0;i
<type
->element_number
;i
++) {
2826 preset_field_type_size(tf
, event_type
,
2827 current_root_offset
, current_offset
,
2828 fixed_root
, ¤t_child_status
,
2830 if(current_child_status
== FIELD_FIXED
) {
2831 current_root_offset
+= field
->child
[i
]->field_size
;
2832 current_offset
+= field
->child
[i
]->field_size
;
2834 current_root_offset
= 0;
2838 if(current_child_status
!= FIELD_FIXED
) {
2839 *fixed_parent
= current_child_status
;
2840 field
->field_size
= 0;
2841 field
->fixed_size
= current_child_status
;
2843 field
->field_size
= current_offset
;
2844 field
->fixed_size
= FIELD_FIXED
;
2848 current_root_offset
= field
->offset_root
;
2851 final_child_status
= FIELD_FIXED
;
2852 for(i
=0;i
<type
->element_number
;i
++) {
2853 enum field_status current_root_child_status
= FIELD_FIXED
;
2854 enum field_status current_child_status
= FIELD_FIXED
;
2855 preset_field_type_size(tf
, event_type
,
2856 current_root_offset
, current_offset
,
2857 ¤t_root_child_status
, ¤t_child_status
,
2859 if(current_child_status
!= FIELD_FIXED
)
2860 final_child_status
= current_child_status
;
2862 max_size
= max(max_size
, field
->child
[i
]->field_size
);
2864 if(final_child_status
!= FIELD_FIXED
) {
2865 g_error("LTTV does not support variable size fields in unions.");
2866 /* This will stop the application. */
2867 *fixed_root
= final_child_status
;
2868 *fixed_parent
= final_child_status
;
2869 field
->field_size
= 0;
2870 field
->fixed_size
= current_child_status
;
2872 field
->field_size
= max_size
;
2873 field
->fixed_size
= FIELD_FIXED
;
2877 g_error("unexpected type NONE");
2884 /*****************************************************************************
2886 * check_fields_compatibility : Check for compatibility between two fields :
2887 * do they use the same inner structure ?
2889 * event_type1 : event type
2890 * event_type2 : event type
2893 *Returns : 0 if identical
2895 ****************************************************************************/
2896 // this function checks for equality of field types. Therefore, it does not use
2897 // per se offsets. For instance, an aligned version of a structure is
2898 // compatible with an unaligned version of the same structure.
2900 gint
check_fields_compatibility(LttEventType
*event_type1
,
2901 LttEventType
*event_type2
,
2902 LttField
*field1
, LttField
*field2
)
2904 guint different
= 0;
2908 if(field1
== NULL
) {
2909 if(field2
== NULL
) goto end
;
2914 } else if(field2
== NULL
) {
2919 type1
= &field1
->field_type
;
2920 type2
= &field2
->field_type
;
2922 if(type1
->type_class
!= type2
->type_class
) {
2926 if(type1
->network
!= type2
->network
) {
2931 switch(type1
->type_class
) {
2933 case LTT_UINT_FIXED
:
2948 if(field1
->field_size
!= field2
->field_size
)
2955 LttField
*child1
= &g_array_index(type1
->fields
, LttField
, 0);
2956 LttField
*child2
= &g_array_index(type2
->fields
, LttField
, 0);
2958 if(type1
->size
!= type2
->size
)
2960 if(check_fields_compatibility(event_type1
, event_type2
, child1
, child2
))
2966 LttField
*child1
= &g_array_index(type1
->fields
, LttField
, 1);
2967 LttField
*child2
= &g_array_index(type2
->fields
, LttField
, 1);
2969 if(check_fields_compatibility(event_type1
, event_type2
, child1
, child2
))
2979 if(type1
->fields
->len
!= type2
->fields
->len
) {
2984 for(i
=0; i
< type1
->fields
->len
; i
++) {
2987 child1
= &g_array_index(type1
->fields
, LttField
, i
);
2988 child2
= &g_array_index(type2
->fields
, LttField
, i
);
2989 different
= check_fields_compatibility(event_type1
,
2990 event_type2
, child1
, child2
);
2992 if(different
) break;
2998 g_error("check_fields_compatibility : unknown type");
3007 gint
check_fields_compatibility(LttEventType
*event_type1
,
3008 LttEventType
*event_type2
,
3009 LttField
*field1
, LttField
*field2
)
3011 guint different
= 0;
3016 if(field1
== NULL
) {
3017 if(field2
== NULL
) goto end
;
3022 } else if(field2
== NULL
) {
3027 g_assert(field1
->fixed_root
!= FIELD_UNKNOWN
);
3028 g_assert(field2
->fixed_root
!= FIELD_UNKNOWN
);
3029 g_assert(field1
->fixed_parent
!= FIELD_UNKNOWN
);
3030 g_assert(field2
->fixed_parent
!= FIELD_UNKNOWN
);
3031 g_assert(field1
->fixed_size
!= FIELD_UNKNOWN
);
3032 g_assert(field2
->fixed_size
!= FIELD_UNKNOWN
);
3034 type1
= field1
->field_type
;
3035 type2
= field2
->field_type
;
3037 if(type1
->type_class
!= type2
->type_class
) {
3041 if(type1
->element_name
!= type2
->element_name
) {
3046 switch(type1
->type_class
) {
3048 case LTT_UINT_FIXED
:
3063 if(field1
->field_size
!= field2
->field_size
) {
3069 if(type1
->element_number
!= type2
->element_number
) {
3073 for(i
=0;i
<type1
->element_number
;i
++) {
3074 if(type1
->enum_strings
[i
] != type2
->enum_strings
[i
]) {
3081 /* Two elements : size and child */
3082 g_assert(type1
->element_number
!= type2
->element_number
);
3083 for(i
=0;i
<type1
->element_number
;i
++) {
3084 if(check_fields_compatibility(event_type1
, event_type2
,
3085 field1
->child
[0], field2
->child
[0])) {
3094 if(field1
->field_size
!= field2
->field_size
) {
3098 /* Two elements : size and child */
3099 g_assert(type1
->element_number
!= type2
->element_number
);
3100 for(i
=0;i
<type1
->element_number
;i
++) {
3101 if(check_fields_compatibility(event_type1
, event_type2
,
3102 field1
->child
[0], field2
->child
[0])) {
3110 if(type1
->element_number
!= type2
->element_number
) {
3114 for(i
=0;i
<type1
->element_number
;i
++) {
3115 if(check_fields_compatibility(event_type1
, event_type2
,
3116 field1
->child
[0], field2
->child
[0])) {
3129 /*****************************************************************************
3131 * ltt_get_int : get an integer number
3133 * reverse_byte_order: must we reverse the byte order ?
3134 * size : the size of the integer
3135 * ptr : the data pointer
3137 * gint64 : a 64 bits integer
3138 ****************************************************************************/
3140 gint64
ltt_get_int(gboolean reverse_byte_order
, gint size
, void *data
)
3145 case 1: val
= *((gint8
*)data
); break;
3146 case 2: val
= ltt_get_int16(reverse_byte_order
, data
); break;
3147 case 4: val
= ltt_get_int32(reverse_byte_order
, data
); break;
3148 case 8: val
= ltt_get_int64(reverse_byte_order
, data
); break;
3149 default: val
= ltt_get_int64(reverse_byte_order
, data
);
3150 g_critical("get_int : integer size %d unknown", size
);
3157 /*****************************************************************************
3159 * ltt_get_uint : get an unsigned integer number
3161 * reverse_byte_order: must we reverse the byte order ?
3162 * size : the size of the integer
3163 * ptr : the data pointer
3165 * guint64 : a 64 bits unsigned integer
3166 ****************************************************************************/
3168 guint64
ltt_get_uint(gboolean reverse_byte_order
, gint size
, void *data
)
3173 case 1: val
= *((gint8
*)data
); break;
3174 case 2: val
= ltt_get_uint16(reverse_byte_order
, data
); break;
3175 case 4: val
= ltt_get_uint32(reverse_byte_order
, data
); break;
3176 case 8: val
= ltt_get_uint64(reverse_byte_order
, data
); break;
3177 default: val
= ltt_get_uint64(reverse_byte_order
, data
);
3178 g_critical("get_uint : unsigned integer size %d unknown",
3187 /* get the node name of the system */
3189 char * ltt_trace_system_description_node_name (LttSystemDescription
* s
)
3191 return s
->node_name
;
3195 /* get the domain name of the system */
3197 char * ltt_trace_system_description_domain_name (LttSystemDescription
* s
)
3199 return s
->domain_name
;
3203 /* get the description of the system */
3205 char * ltt_trace_system_description_description (LttSystemDescription
* s
)
3207 return s
->description
;
3211 /* get the NTP corrected start time of the trace */
3212 LttTime
ltt_trace_start_time(LttTrace
*t
)
3214 return t
->start_time
;
3217 /* get the monotonic start time of the trace */
3218 LttTime
ltt_trace_start_time_monotonic(LttTrace
*t
)
3220 return t
->start_time_from_tsc
;
3223 LttTracefile
*ltt_tracefile_new()
3226 tf
= g_new(LttTracefile
, 1);
3227 tf
->event
.tracefile
= tf
;
3231 void ltt_tracefile_destroy(LttTracefile
*tf
)
3236 void ltt_tracefile_copy(LttTracefile
*dest
, const LttTracefile
*src
)
3241 /* Before library loading... */
3245 LTT_FACILITY_NAME_HEARTBEAT
= g_quark_from_string("heartbeat");
3246 LTT_EVENT_NAME_HEARTBEAT
= g_quark_from_string("heartbeat");
3247 LTT_EVENT_NAME_HEARTBEAT_FULL
= g_quark_from_string("heartbeat_full");
3249 LTT_TRACEFILE_NAME_FACILITIES
= g_quark_from_string("/control/facilities");