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
);
119 //void precompute_offsets(LttFacility *fac, LttEventType *event);
122 /* Functions to parse system.xml file (using glib xml parser) */
123 static void parser_start_element (GMarkupParseContext __UNUSED__
*context
,
124 const gchar
*element_name
,
125 const gchar
**attribute_names
,
126 const gchar
**attribute_values
,
131 LttSystemDescription
* des
= (LttSystemDescription
* )user_data
;
132 if(strcmp("system", element_name
)){
133 *error
= g_error_new(G_MARKUP_ERROR
,
135 "This is not system.xml file");
139 while(attribute_names
[i
]){
140 if(strcmp("node_name", attribute_names
[i
])==0){
141 des
->node_name
= g_strdup(attribute_values
[i
]);
142 }else if(strcmp("domainname", attribute_names
[i
])==0){
143 des
->domain_name
= g_strdup(attribute_values
[i
]);
144 }else if(strcmp("cpu", attribute_names
[i
])==0){
145 des
->nb_cpu
= atoi(attribute_values
[i
]);
146 }else if(strcmp("arch_size", attribute_names
[i
])==0){
147 if(strcmp(attribute_values
[i
],"LP32") == 0) des
->size
= LTT_LP32
;
148 else if(strcmp(attribute_values
[i
],"ILP32") == 0) des
->size
= LTT_ILP32
;
149 else if(strcmp(attribute_values
[i
],"LP64") == 0) des
->size
= LTT_LP64
;
150 else if(strcmp(attribute_values
[i
],"ILP64") == 0) des
->size
= LTT_ILP64
;
151 else if(strcmp(attribute_values
[i
],"UNKNOWN") == 0) des
->size
= LTT_UNKNOWN
;
152 }else if(strcmp("endian", attribute_names
[i
])==0){
153 if(strcmp(attribute_values
[i
],"LITTLE_ENDIAN") == 0)
154 des
->endian
= LTT_LITTLE_ENDIAN
;
155 else if(strcmp(attribute_values
[i
],"BIG_ENDIAN") == 0)
156 des
->endian
= LTT_BIG_ENDIAN
;
157 }else if(strcmp("kernel_name", attribute_names
[i
])==0){
158 des
->kernel_name
= g_strdup(attribute_values
[i
]);
159 }else if(strcmp("kernel_release", attribute_names
[i
])==0){
160 des
->kernel_release
= g_strdup(attribute_values
[i
]);
161 }else if(strcmp("kernel_version", attribute_names
[i
])==0){
162 des
->kernel_version
= g_strdup(attribute_values
[i
]);
163 }else if(strcmp("machine", attribute_names
[i
])==0){
164 des
->machine
= g_strdup(attribute_values
[i
]);
165 }else if(strcmp("processor", attribute_names
[i
])==0){
166 des
->processor
= g_strdup(attribute_values
[i
]);
167 }else if(strcmp("hardware_platform", attribute_names
[i
])==0){
168 des
->hardware_platform
= g_strdup(attribute_values
[i
]);
169 }else if(strcmp("operating_system", attribute_names
[i
])==0){
170 des
->operating_system
= g_strdup(attribute_values
[i
]);
171 }else if(strcmp("ltt_major_version", attribute_names
[i
])==0){
172 des
->ltt_major_version
= atoi(attribute_values
[i
]);
173 }else if(strcmp("ltt_minor_version", attribute_names
[i
])==0){
174 des
->ltt_minor_version
= atoi(attribute_values
[i
]);
175 }else if(strcmp("ltt_block_size", attribute_names
[i
])==0){
176 des
->ltt_block_size
= atoi(attribute_values
[i
]);
178 *error
= g_error_new(G_MARKUP_ERROR
,
180 "Not a valid attribute");
187 static void parser_characters (GMarkupParseContext __UNUSED__
*context
,
189 gsize __UNUSED__ text_len
,
191 GError __UNUSED__
**error
)
193 LttSystemDescription
* des
= (LttSystemDescription
* )user_data
;
194 des
->description
= g_strdup(text
);
199 LttFacility
*ltt_trace_get_facility_by_num(LttTrace
*t
,
202 g_assert(num
< t
->facilities_by_num
->len
);
204 return &g_array_index(t
->facilities_by_num
, LttFacility
, num
);
209 guint
ltt_trace_get_num_cpu(LttTrace
*t
)
217 * Return value : 0 success, 1 bad tracefile
219 int parse_trace_header(void *header
, LttTracefile
*tf
, LttTrace
*t
)
221 guint32
*magic_number
= (guint32
*)header
;
222 struct ltt_trace_header_any
*any
= (struct ltt_trace_header_any
*)header
;
224 if(*magic_number
== LTT_MAGIC_NUMBER
)
226 else if(*magic_number
== LTT_REV_MAGIC_NUMBER
)
228 else /* invalid magic number, bad tracefile ! */
231 /* Get float byte order : might be different from int byte order
232 * (or is set to 0 if the trace has no float (kernel trace)) */
233 tf
->float_word_order
= any
->float_word_order
;
234 tf
->alignment
= any
->alignment
;
235 tf
->has_heartbeat
= any
->has_heartbeat
;
238 t
->arch_type
= ltt_get_uint32(LTT_GET_BO(tf
),
240 t
->arch_variant
= ltt_get_uint32(LTT_GET_BO(tf
),
242 t
->arch_size
= any
->arch_size
;
243 t
->ltt_major_version
= any
->major_version
;
244 t
->ltt_minor_version
= any
->minor_version
;
245 t
->flight_recorder
= any
->flight_recorder
;
246 // t->compact_facilities = NULL;
249 printf("init size : %d\n", sizeof(LttTracefile
));
252 switch(any
->major_version
) {
255 g_warning("Unsupported trace version : %hhu.%hhu",
256 any
->major_version
, any
->minor_version
);
260 switch(any
->minor_version
) {
263 struct ltt_trace_header_1_0
*vheader
=
264 (struct ltt_trace_header_1_0
*)header
;
265 tf
->buffer_header_size
=
266 sizeof(struct ltt_block_start_header
)
267 + sizeof(struct ltt_trace_header_1_0
);
268 tf
->tsc_lsb_truncate
= vheader
->tsc_lsb_truncate
;
269 tf
->tscbits
= vheader
->tscbits
;
270 tf
->tsc_msb_cutoff
= 32 - tf
->tsc_lsb_truncate
- tf
->tscbits
;
271 tf
->tsc_mask
= ((1ULL << (tf
->tscbits
))-1);
272 tf
->tsc_mask
= tf
->tsc_mask
<< tf
->tsc_lsb_truncate
;
273 tf
->tsc_mask_next_bit
= (1ULL<<(tf
->tscbits
));
274 tf
->tsc_mask_next_bit
= tf
->tsc_mask_next_bit
<< tf
->tsc_lsb_truncate
;
276 t
->start_freq
= ltt_get_uint64(LTT_GET_BO(tf
),
277 &vheader
->start_freq
);
278 t
->freq_scale
= ltt_get_uint32(LTT_GET_BO(tf
),
279 &vheader
->freq_scale
);
280 t
->start_tsc
= ltt_get_uint64(LTT_GET_BO(tf
),
281 &vheader
->start_tsc
);
282 t
->start_monotonic
= ltt_get_uint64(LTT_GET_BO(tf
),
283 &vheader
->start_monotonic
);
284 t
->start_time
.tv_sec
= ltt_get_uint64(LTT_GET_BO(tf
),
285 &vheader
->start_time_sec
);
286 t
->start_time
.tv_nsec
= ltt_get_uint64(LTT_GET_BO(tf
),
287 &vheader
->start_time_usec
);
288 t
->start_time
.tv_nsec
*= 1000; /* microsec to nanosec */
290 t
->start_time_from_tsc
= ltt_time_from_uint64(
292 * (1000000000.0 / tf
->trace
->freq_scale
)
293 / (double)t
->start_freq
);
294 t
->compact_event_bits
= 0;
299 g_warning("Unsupported trace version : %hhu.%hhu",
300 any
->major_version
, any
->minor_version
);
305 g_warning("Unsupported trace version : %hhu.%hhu",
306 any
->major_version
, any
->minor_version
);
316 /*****************************************************************************
318 * ltt_tracefile_open : open a trace file, construct a LttTracefile
320 * t : the trace containing the tracefile
321 * fileName : path name of the trace file
322 * tf : the tracefile structure
324 * : 0 for success, -1 otherwise.
325 ****************************************************************************/
327 gint
ltt_tracefile_open(LttTrace
*t
, gchar
* fileName
, LttTracefile
*tf
)
329 struct stat lTDFStat
; /* Trace data file status */
330 struct ltt_block_start_header
*header
;
331 int page_size
= getpagesize();
334 tf
->long_name
= g_quark_from_string(fileName
);
336 tf
->fd
= open(fileName
, O_RDONLY
);
338 g_warning("Unable to open input data file %s\n", fileName
);
342 // Get the file's status
343 if(fstat(tf
->fd
, &lTDFStat
) < 0){
344 g_warning("Unable to get the status of the input data file %s\n", fileName
);
348 // Is the file large enough to contain a trace
349 if(lTDFStat
.st_size
<
350 (off_t
)(sizeof(struct ltt_block_start_header
)
351 + sizeof(struct ltt_trace_header_any
))){
352 g_print("The input data file %s does not contain a trace\n", fileName
);
356 /* Temporarily map the buffer start header to get trace information */
357 /* Multiple of pages aligned head */
358 tf
->buffer
.head
= mmap(0,
359 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)
360 + sizeof(struct ltt_trace_header_any
)), PROT_READ
,
361 MAP_PRIVATE
, tf
->fd
, 0);
362 if(tf
->buffer
.head
== MAP_FAILED
) {
363 perror("Error in allocating memory for buffer of tracefile");
366 g_assert( ( (guint
)tf
->buffer
.head
&(8-1) ) == 0); // make sure it's aligned.
368 header
= (struct ltt_block_start_header
*)tf
->buffer
.head
;
370 if(parse_trace_header(header
->trace
, tf
, NULL
)) {
371 g_warning("parse_trace_header error");
375 //store the size of the file
376 tf
->file_size
= lTDFStat
.st_size
;
377 tf
->buf_size
= ltt_get_uint32(LTT_GET_BO(tf
), &header
->buf_size
);
378 tf
->num_blocks
= tf
->file_size
/ tf
->buf_size
;
380 if(munmap(tf
->buffer
.head
,
381 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)
382 + sizeof(struct ltt_trace_header_any
)))) {
383 g_warning("unmap size : %u\n",
384 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)
385 + sizeof(struct ltt_trace_header_any
)));
386 perror("munmap error");
389 tf
->buffer
.head
= NULL
;
391 //read the first block
392 if(map_block(tf
,0)) {
393 perror("Cannot map block for tracefile");
401 if(munmap(tf
->buffer
.head
,
402 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)
403 + sizeof(struct ltt_trace_header_any
)))) {
404 g_warning("unmap size : %u\n",
405 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)
406 + sizeof(struct ltt_trace_header_any
)));
407 perror("munmap error");
416 LttTrace
*ltt_tracefile_get_trace(LttTracefile
*tf
)
422 /*****************************************************************************
423 *Open control and per cpu tracefiles
424 ****************************************************************************/
426 void ltt_tracefile_open_cpu(LttTrace
*t
, gchar
* tracefile_name
)
429 tf
= ltt_tracefile_open(t
,tracefile_name
);
431 t
->per_cpu_tracefile_number
++;
432 g_ptr_array_add(t
->per_cpu_tracefiles
, tf
);
435 gint
ltt_tracefile_open_control(LttTrace
*t
, gchar
* control_name
)
444 tf
= ltt_tracefile_open(t
,control_name
);
446 g_warning("ltt_tracefile_open_control : bad file descriptor");
449 t
->control_tracefile_number
++;
450 g_ptr_array_add(t
->control_tracefiles
,tf
);
452 //parse facilities tracefile to get base_id
453 if(strcmp(&control_name
[strlen(control_name
)-10],"facilities") ==0){
455 if(!ltt_tracefile_read(tf
,&ev
)) return 0; // end of file
457 if(ev
.event_id
== TRACE_FACILITY_LOAD
){
459 fLoad
.name
= (gchar
*)pos
;
460 fLoad
.checksum
= *(LttChecksum
*)(pos
+ strlen(fLoad
.name
));
461 fLoad
.base_code
= *(guint32
*)(pos
+ strlen(fLoad
.name
) + sizeof(LttChecksum
));
463 for(i
=0;i
<t
->facility_number
;i
++){
464 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
,i
);
465 if(strcmp(f
->name
,fLoad
.name
)==0 && fLoad
.checksum
==f
->checksum
){
466 f
->base_id
= fLoad
.base_code
;
470 if(i
==t
->facility_number
) {
471 g_warning("Facility: %s, checksum: %u is not found",
472 fLoad
.name
,(unsigned int)fLoad
.checksum
);
475 }else if(ev
.event_id
== TRACE_BLOCK_START
){
477 }else if(ev
.event_id
== TRACE_BLOCK_END
){
480 g_warning("Not valid facilities trace file");
489 /*****************************************************************************
491 * ltt_tracefile_close: close a trace file,
493 * t : tracefile which will be closed
494 ****************************************************************************/
496 void ltt_tracefile_close(LttTracefile
*t
)
498 int page_size
= getpagesize();
500 if(t
->buffer
.head
!= NULL
)
501 if(munmap(t
->buffer
.head
, PAGE_ALIGN(t
->buf_size
))) {
502 g_warning("unmap size : %u\n",
503 PAGE_ALIGN(t
->buf_size
));
504 perror("munmap error");
512 /*****************************************************************************
513 *Get system information
514 ****************************************************************************/
516 gint
getSystemInfo(LttSystemDescription
* des
, gchar
* pathname
)
523 GMarkupParseContext
* context
;
524 GError
* error
= NULL
;
525 GMarkupParser markup_parser
=
527 parser_start_element
,
530 NULL
, /* passthrough */
534 fd
= g_open(pathname
, O_RDONLY
, 0);
536 g_warning("Can not open file : %s\n", pathname
);
540 iochan
= g_io_channel_unix_new(fd
);
542 context
= g_markup_parse_context_new(&markup_parser
, 0, des
,NULL
);
544 //while(fgets(buf,DIR_NAME_SIZE, fp) != NULL){
545 while(g_io_channel_read_line(iochan
, &buf
, &length
, NULL
, &error
)
546 != G_IO_STATUS_EOF
) {
549 g_warning("Can not read xml file: \n%s\n", error
->message
);
552 if(!g_markup_parse_context_parse(context
, buf
, length
, &error
)){
554 g_warning("Can not parse xml file: \n%s\n", error
->message
);
557 g_markup_parse_context_free(context
);
559 g_io_channel_shutdown(iochan
, FALSE
, &error
); /* No flush */
561 g_warning("Can not close file: \n%s\n", error
->message
);
569 g_markup_parse_context_free(context
);
571 g_io_channel_shutdown(iochan
, FALSE
, &error
); /* No flush */
573 g_warning("Can not close file: \n%s\n", error
->message
);
584 /*****************************************************************************
585 *The following functions get facility/tracefile information
586 ****************************************************************************/
588 gint
getFacilityInfo(LttTrace
*t
, gchar
* eventdefs
)
595 gchar fullname
[DIR_NAME_SIZE
];
596 GError
* error
= NULL
;
598 dir
= g_dir_open(eventdefs
, 0, &error
);
601 g_warning("Can not open directory: %s, %s\n", eventdefs
, error
->message
);
606 while((name
= g_dir_read_name(dir
)) != NULL
){
607 if(!g_pattern_match_simple("*.xml", name
)) continue;
608 strcpy(fullname
,eventdefs
);
609 strcat(fullname
,name
);
610 ltt_facility_open(t
,fullname
);
614 for(j
=0;j
<t
->facility_number
;j
++){
615 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, j
);
616 for(i
=0; i
<f
->event_number
; i
++){
618 setFieldsOffset(NULL
, et
, NULL
, t
);
625 /*****************************************************************************
626 *A trace is specified as a pathname to the directory containing all the
627 *associated data (control tracefiles, per cpu tracefiles, event
630 *When a trace is closed, all the associated facilities, types and fields
631 *are released as well.
635 /****************************************************************************
636 * get_absolute_pathname
638 * return the unique pathname in the system
640 * MD : Fixed this function so it uses realpath, dealing well with
641 * forgotten cases (.. were not used correctly before).
643 ****************************************************************************/
644 void get_absolute_pathname(const gchar
*pathname
, gchar
* abs_pathname
)
646 abs_pathname
[0] = '\0';
648 if ( realpath (pathname
, abs_pathname
) != NULL
)
652 /* error, return the original path unmodified */
653 strcpy(abs_pathname
, pathname
);
659 /* Search for something like : .*_.*
661 * The left side is the name, the right side is the number.
664 int get_tracefile_name_number(gchar
*raw_name
,
671 guint raw_name_len
= strlen(raw_name
);
672 gchar char_name
[PATH_MAX
];
679 for(i
=raw_name_len
-1;i
>=0;i
--) {
680 if(raw_name
[i
] == '_') break;
682 if(i
==-1) { /* Either not found or name length is 0 */
683 /* This is a userspace tracefile */
684 strncpy(char_name
, raw_name
, raw_name_len
);
685 char_name
[raw_name_len
] = '\0';
686 *name
= g_quark_from_string(char_name
);
687 *num
= 0; /* unknown cpu */
688 for(i
=0;i
<raw_name_len
;i
++) {
689 if(raw_name
[i
] == '/') {
694 for(;i
<raw_name_len
;i
++) {
695 if(raw_name
[i
] == '/') {
700 for(;i
<raw_name_len
;i
++) {
701 if(raw_name
[i
] == '-') {
705 if(i
== raw_name_len
) return -1;
707 tmpptr
= &raw_name
[i
];
708 for(;i
<raw_name_len
;i
++) {
709 if(raw_name
[i
] == '.') {
714 *tid
= strtoul(tmpptr
, &endptr
, 10);
716 return -1; /* No digit */
717 if(*tid
== ULONG_MAX
)
718 return -1; /* underflow / overflow */
720 tmpptr
= &raw_name
[i
];
721 for(;i
<raw_name_len
;i
++) {
722 if(raw_name
[i
] == '.') {
727 *pgid
= strtoul(tmpptr
, &endptr
, 10);
729 return -1; /* No digit */
730 if(*pgid
== ULONG_MAX
)
731 return -1; /* underflow / overflow */
733 tmpptr
= &raw_name
[i
];
734 *creation
= strtoull(tmpptr
, &endptr
, 10);
736 return -1; /* No digit */
737 if(*creation
== G_MAXUINT64
)
738 return -1; /* underflow / overflow */
742 cpu_num
= strtol(raw_name
+underscore_pos
+1, &endptr
, 10);
744 if(endptr
== raw_name
+underscore_pos
+1)
745 return -1; /* No digit */
746 if(cpu_num
== LONG_MIN
|| cpu_num
== LONG_MAX
)
747 return -1; /* underflow / overflow */
749 strncpy(char_name
, raw_name
, underscore_pos
);
750 char_name
[underscore_pos
] = '\0';
752 *name
= g_quark_from_string(char_name
);
761 GData
**ltt_trace_get_tracefiles_groups(LttTrace
*trace
)
763 return &trace
->tracefiles
;
767 void compute_tracefile_group(GQuark key_id
,
769 struct compute_tracefile_group_args
*args
)
774 for(i
=0; i
<group
->len
; i
++) {
775 tf
= &g_array_index (group
, LttTracefile
, i
);
777 args
->func(tf
, args
->func_args
);
782 void ltt_tracefile_group_destroy(gpointer data
)
784 GArray
*group
= (GArray
*)data
;
788 for(i
=0; i
<group
->len
; i
++) {
789 tf
= &g_array_index (group
, LttTracefile
, i
);
791 ltt_tracefile_close(tf
);
793 g_array_free(group
, TRUE
);
796 gboolean
ltt_tracefile_group_has_cpu_online(gpointer data
)
798 GArray
*group
= (GArray
*)data
;
802 for(i
=0; i
<group
->len
; i
++) {
803 tf
= &g_array_index (group
, LttTracefile
, i
);
811 /* Open each tracefile under a specific directory. Put them in a
812 * GData : permits to access them using their tracefile group pathname.
813 * i.e. access control/modules tracefile group by index :
816 * relative path is the path relative to the trace root
817 * root path is the full path
819 * A tracefile group is simply an array where all the per cpu tracefiles sit.
822 int open_tracefiles(LttTrace
*trace
, gchar
*root_path
, gchar
*relative_path
)
824 DIR *dir
= opendir(root_path
);
825 struct dirent
*entry
;
826 struct stat stat_buf
;
829 gchar path
[PATH_MAX
];
834 gchar rel_path
[PATH_MAX
];
843 strncpy(path
, root_path
, PATH_MAX
-1);
844 path_len
= strlen(path
);
845 path
[path_len
] = '/';
847 path_ptr
= path
+ path_len
;
849 strncpy(rel_path
, relative_path
, PATH_MAX
-1);
850 rel_path_len
= strlen(rel_path
);
851 rel_path
[rel_path_len
] = '/';
853 rel_path_ptr
= rel_path
+ rel_path_len
;
855 while((entry
= readdir(dir
)) != NULL
) {
857 if(entry
->d_name
[0] == '.') continue;
859 strncpy(path_ptr
, entry
->d_name
, PATH_MAX
- path_len
);
860 strncpy(rel_path_ptr
, entry
->d_name
, PATH_MAX
- rel_path_len
);
862 ret
= stat(path
, &stat_buf
);
868 g_debug("Tracefile file or directory : %s\n", path
);
870 if(strcmp(rel_path
, "/eventdefs") == 0) continue;
872 if(S_ISDIR(stat_buf
.st_mode
)) {
874 g_debug("Entering subdirectory...\n");
875 ret
= open_tracefiles(trace
, path
, rel_path
);
876 if(ret
< 0) continue;
877 } else if(S_ISREG(stat_buf
.st_mode
)) {
879 guint num
, tid
, pgid
;
882 num
= tid
= pgid
= 0;
884 if(get_tracefile_name_number(rel_path
, &name
, &num
, &tid
, &pgid
, &creation
))
885 continue; /* invalid name */
887 g_debug("Opening file.\n");
888 if(ltt_tracefile_open(trace
, path
, &tmp_tf
)) {
889 g_info("Error opening tracefile %s", path
);
891 continue; /* error opening the tracefile : bad magic number ? */
894 g_debug("Tracefile name is %s and number is %u",
895 g_quark_to_string(name
), num
);
897 tmp_tf
.cpu_online
= 1;
898 tmp_tf
.cpu_num
= num
;
902 tmp_tf
.creation
= creation
;
903 if(tmp_tf
.name
== g_quark_from_string("/compact")
904 || tmp_tf
.name
== g_quark_from_string("/flight-compact"))
908 group
= g_datalist_id_get_data(&trace
->tracefiles
, name
);
910 /* Elements are automatically cleared when the array is allocated.
911 * It makes the cpu_online variable set to 0 : cpu offline, by default.
913 group
= g_array_sized_new (FALSE
, TRUE
, sizeof(LttTracefile
), 10);
914 g_datalist_id_set_data_full(&trace
->tracefiles
, name
,
915 group
, ltt_tracefile_group_destroy
);
918 /* Add the per cpu tracefile to the named group */
919 unsigned int old_len
= group
->len
;
921 group
= g_array_set_size(group
, num
+1);
922 g_array_index (group
, LttTracefile
, num
) = tmp_tf
;
932 /* ltt_get_facility_description
934 * Opens the file corresponding to the requested facility (identified by fac_id
937 * The name searched is : %trace root%/eventdefs/facname_checksum.xml
939 * Returns 0 on success, or 1 on failure.
942 static int ltt_get_facility_description(LttFacility
*f
,
944 LttTracefile
*fac_tf
)
946 char desc_file_name
[PATH_MAX
];
953 text
= g_quark_to_string(t
->pathname
);
954 textlen
= strlen(text
);
956 if(textlen
>= PATH_MAX
) goto name_error
;
957 strcpy(desc_file_name
, text
);
959 text
= "/eventdefs/";
960 textlen
+=strlen(text
);
961 if(textlen
>= PATH_MAX
) goto name_error
;
962 strcat(desc_file_name
, text
);
964 text
= g_quark_to_string(f
->name
);
965 fac_name_len
= strlen(text
);
966 textlen
+=fac_name_len
;
967 if(textlen
>= PATH_MAX
) goto name_error
;
968 strcat(desc_file_name
, text
);
970 /* arch specific facilities are named like this : name_arch */
971 if(fac_name_len
+1 < sizeof("_arch"))
974 if(!strcmp(&text
[fac_name_len
+1-sizeof("_arch")], "_arch"))
982 textlen
+=strlen(text
);
983 if(textlen
>= PATH_MAX
) goto name_error
;
984 strcat(desc_file_name
, text
);
986 err
= snprintf(desc_file_name
+textlen
, PATH_MAX
-textlen
-1,
988 if(err
< 0) goto name_error
;
990 textlen
=strlen(desc_file_name
);
995 switch(t
->arch_type
) {
996 case LTT_ARCH_TYPE_I386
:
999 case LTT_ARCH_TYPE_PPC
:
1002 case LTT_ARCH_TYPE_SH
:
1005 case LTT_ARCH_TYPE_S390
:
1008 case LTT_ARCH_TYPE_MIPS
:
1011 case LTT_ARCH_TYPE_ARM
:
1014 case LTT_ARCH_TYPE_PPC64
:
1017 case LTT_ARCH_TYPE_X86_64
:
1020 case LTT_ARCH_TYPE_C2
:
1023 case LTT_ARCH_TYPE_POWERPC
:
1027 g_error("Trace from unsupported architecture.");
1029 textlen
+=strlen(text
);
1030 if(textlen
>= PATH_MAX
) goto name_error
;
1031 strcat(desc_file_name
, text
);
1035 textlen
+=strlen(text
);
1036 if(textlen
>= PATH_MAX
) goto name_error
;
1037 strcat(desc_file_name
, text
);
1039 err
= ltt_facility_open(f
, t
, desc_file_name
);
1040 if(err
) goto facility_error
;
1049 static void ltt_fac_ids_destroy(gpointer data
)
1051 GArray
*fac_ids
= (GArray
*)data
;
1053 g_array_free(fac_ids
, TRUE
);
1057 /* Presumes the tracefile is already seeked at the beginning. It makes sense,
1058 * because it must be done just after the opening */
1059 int ltt_process_facility_tracefile(LttTracefile
*tf
)
1068 err
= ltt_tracefile_read_seek(tf
);
1069 if(err
== EPERM
) goto seek_error
;
1070 else if(err
== ERANGE
) break; /* End of tracefile */
1072 err
= ltt_tracefile_read_update_event(tf
);
1073 if(err
) goto update_error
;
1075 /* We are on a facility load/or facility unload/ or heartbeat event */
1077 * * facility 0 is hardcoded : this is the core facility. It will be shown
1078 * in the facility array though, and is shown as "loaded builtin" in the
1080 * It contains event :
1082 * 1 : facility unload
1083 * 2 : state dump facility load
1086 if(tf
->event
.event_id
>= MARKER_CORE_IDS
) {
1087 /* Should only contain core facility */
1088 g_warning("Error in processing facility file %s, "
1089 "should not contain event id %u.", g_quark_to_string(tf
->name
),
1090 tf
->event
.event_id
);
1092 goto event_id_error
;
1096 const char *marker_name
, *format
;
1098 guint8 int_size
, long_size
, pointer_size
, size_t_size
, alignment
;
1101 switch((enum marker_id
)tf
->event
.event_id
) {
1102 case MARKER_ID_SET_MARKER_ID
:
1103 marker_name
= pos
= tf
->event
.data
;
1104 g_debug("Doing MARKER_ID_SET_MARKER_ID of marker %s", marker_name
);
1105 pos
+= strlen(marker_name
) + 1;
1106 //remove genevent compatibility
1107 //pos += ltt_align((size_t)pos, tf->trace->arch_size, tf->alignment);
1108 pos
+= ltt_align((size_t)pos
, sizeof(uint16_t), tf
->alignment
);
1109 id
= ltt_get_uint16(LTT_GET_BO(tf
), pos
);
1110 pos
+= sizeof(guint16
);
1111 int_size
= *(guint8
*)pos
;
1112 pos
+= sizeof(guint8
);
1113 long_size
= *(guint8
*)pos
;
1114 pos
+= sizeof(guint8
);
1115 pointer_size
= *(guint8
*)pos
;
1116 pos
+= sizeof(guint8
);
1117 size_t_size
= *(guint8
*)pos
;
1118 pos
+= sizeof(guint8
);
1119 alignment
= *(guint8
*)pos
;
1120 pos
+= sizeof(guint8
);
1121 marker_id_event(tf
->trace
, g_quark_from_string(marker_name
),
1122 id
, int_size
, long_size
,
1123 pointer_size
, size_t_size
, alignment
);
1125 case MARKER_ID_SET_MARKER_FORMAT
:
1126 marker_name
= pos
= tf
->event
.data
;
1127 g_debug("Doing MARKER_ID_SET_MARKER_FORMAT of marker %s",
1129 pos
+= strlen(marker_name
) + 1;
1131 //pos += ltt_align((size_t)pos, tf->trace->arch_size, tf->alignment);
1133 pos
+= strlen(format
) + 1;
1135 //pos += ltt_align((size_t)pos, tf->trace->arch_size, tf->alignment);
1136 marker_format_event(tf
->trace
, g_quark_from_string(marker_name
),
1138 /* get information from dictionnary TODO */
1140 case MARKER_ID_HEARTBEAT_32
:
1141 case MARKER_ID_HEARTBEAT_64
:
1144 g_warning("Error in processing facility file %s, "
1145 "unknown event id %hhu.",
1146 g_quark_to_string(tf
->name
),
1147 tf
->event
.event_id
);
1149 goto event_id_error
;
1155 /* Error handling */
1159 g_warning("An error occured in facility tracefile parsing");
1164 LttTrace
*ltt_trace_open(const gchar
*pathname
)
1166 gchar abs_path
[PATH_MAX
];
1171 struct ltt_block_start_header
*header
;
1173 struct dirent
*entry
;
1174 guint control_found
= 0;
1175 guint eventdefs_found
= 0;
1176 struct stat stat_buf
;
1177 gchar path
[PATH_MAX
];
1179 t
= g_new(LttTrace
, 1);
1180 if(!t
) goto alloc_error
;
1182 get_absolute_pathname(pathname
, abs_path
);
1183 t
->pathname
= g_quark_from_string(abs_path
);
1185 g_datalist_init(&t
->tracefiles
);
1187 /* Test to see if it looks like a trace */
1188 dir
= opendir(abs_path
);
1193 while((entry
= readdir(dir
)) != NULL
) {
1194 strcpy(path
, abs_path
);
1196 strcat(path
, entry
->d_name
);
1197 ret
= stat(path
, &stat_buf
);
1202 if(S_ISDIR(stat_buf
.st_mode
)) {
1203 if(strcmp(entry
->d_name
, "control") == 0) {
1206 if(strcmp(entry
->d_name
, "eventdefs") == 0) {
1207 eventdefs_found
= 1;
1213 if(!control_found
|| !eventdefs_found
) goto find_error
;
1215 /* Open all the tracefiles */
1216 if(open_tracefiles(t
, abs_path
, "")) {
1217 g_warning("Error opening tracefile %s", abs_path
);
1221 /* Parse each trace control/facilitiesN files : get runtime fac. info */
1222 group
= g_datalist_id_get_data(&t
->tracefiles
, LTT_TRACEFILE_NAME_FACILITIES
);
1224 g_error("Trace %s has no facility tracefile", abs_path
);
1226 goto facilities_error
;
1229 /* Get the trace information for the control/facility 0 tracefile */
1230 g_assert(group
->len
> 0);
1231 tf
= &g_array_index (group
, LttTracefile
, 0);
1232 header
= (struct ltt_block_start_header
*)tf
->buffer
.head
;
1233 g_assert(parse_trace_header(header
->trace
,
1236 t
->num_cpu
= group
->len
;
1238 ret
= allocate_marker_data(t
);
1240 g_error("Error in allocating marker data");
1242 for(i
=0; i
<group
->len
; i
++) {
1243 tf
= &g_array_index (group
, LttTracefile
, i
);
1244 if(ltt_process_facility_tracefile(tf
))
1245 goto facilities_error
;
1250 /* Error handling */
1252 destroy_marker_data(t
);
1254 g_datalist_clear(&t
->tracefiles
);
1262 GQuark
ltt_trace_name(const LttTrace
*t
)
1268 /******************************************************************************
1269 * When we copy a trace, we want all the opening actions to happen again :
1270 * the trace will be reopened and totally independant from the original.
1271 * That's why we call ltt_trace_open.
1272 *****************************************************************************/
1273 LttTrace
*ltt_trace_copy(LttTrace
*self
)
1275 return ltt_trace_open(g_quark_to_string(self
->pathname
));
1278 void ltt_trace_close(LttTrace
*t
)
1280 g_datalist_clear(&t
->tracefiles
);
1285 /*****************************************************************************
1286 *Get the system description of the trace
1287 ****************************************************************************/
1289 LttFacility
*ltt_trace_facility_by_id(LttTrace
*t
, guint8 id
)
1291 g_assert(id
< t
->facilities_by_num
->len
);
1292 return &g_array_index(t
->facilities_by_num
, LttFacility
, id
);
1295 /* ltt_trace_facility_get_by_name
1297 * Returns the GArray of facility indexes. All the fac_ids that matches the
1298 * requested facility name.
1300 * If name is not found, returns NULL.
1302 GArray
*ltt_trace_facility_get_by_name(LttTrace
*t
, GQuark name
)
1304 return g_datalist_id_get_data(&t
->facilities_by_name
, name
);
1308 /*****************************************************************************
1309 * Functions to discover all the event types in the trace
1310 ****************************************************************************/
1313 unsigned ltt_trace_eventtype_number(LttTrace
*t
)
1317 unsigned int num
= t
->facility_number
;
1321 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, i
);
1322 count
+= f
->event_number
;
1329 //use an iteration on all the trace facilities, and inside iteration on all the
1330 //event types in each facilities instead.
1331 LttEventType
*ltt_trace_eventtype_get(LttTrace
*t
, unsigned evId
)
1333 LttEventType
*event_type
;
1336 f
= ltt_trace_facility_by_id(t
,evId
);
1338 if(unlikely(!f
)) event_type
= NULL
;
1339 else event_type
= f
->events
[evId
- f
->base_id
];
1346 /*****************************************************************************
1347 * ltt_trace_find_tracefile
1349 * Find a tracefile by name and index in the group.
1351 * Returns a pointer to the tracefiles, else NULL.
1352 ****************************************************************************/
1354 LttTracefile
*ltt_trace_find_tracefile(LttTrace
*t
, const gchar
*name
)
1359 /*****************************************************************************
1360 * Get the start time and end time of the trace
1361 ****************************************************************************/
1363 void ltt_tracefile_time_span_get(LttTracefile
*tf
,
1364 LttTime
*start
, LttTime
*end
)
1368 err
= map_block(tf
, 0);
1370 g_error("Can not map block");
1371 *start
= ltt_time_infinite
;
1373 *start
= tf
->buffer
.begin
.timestamp
;
1375 err
= map_block(tf
, tf
->num_blocks
- 1); /* Last block */
1377 g_error("Can not map block");
1378 *end
= ltt_time_zero
;
1380 *end
= tf
->buffer
.end
.timestamp
;
1383 struct tracefile_time_span_get_args
{
1389 void group_time_span_get(GQuark name
, gpointer data
, gpointer user_data
)
1391 struct tracefile_time_span_get_args
*args
=
1392 (struct tracefile_time_span_get_args
*)user_data
;
1394 GArray
*group
= (GArray
*)data
;
1400 for(i
=0; i
<group
->len
; i
++) {
1401 tf
= &g_array_index (group
, LttTracefile
, i
);
1402 if(tf
->cpu_online
) {
1403 ltt_tracefile_time_span_get(tf
, &tmp_start
, &tmp_end
);
1404 if(ltt_time_compare(*args
->start
, tmp_start
)>0) *args
->start
= tmp_start
;
1405 if(ltt_time_compare(*args
->end
, tmp_end
)<0) *args
->end
= tmp_end
;
1410 void ltt_trace_time_span_get(LttTrace
*t
, LttTime
*start
, LttTime
*end
)
1412 LttTime min_start
= ltt_time_infinite
;
1413 LttTime max_end
= ltt_time_zero
;
1414 struct tracefile_time_span_get_args args
= { t
, &min_start
, &max_end
};
1416 g_datalist_foreach(&t
->tracefiles
, &group_time_span_get
, &args
);
1418 if(start
!= NULL
) *start
= min_start
;
1419 if(end
!= NULL
) *end
= max_end
;
1424 /*****************************************************************************
1425 *Get the name of a tracefile
1426 ****************************************************************************/
1428 GQuark
ltt_tracefile_name(const LttTracefile
*tf
)
1433 GQuark
ltt_tracefile_long_name(const LttTracefile
*tf
)
1435 return tf
->long_name
;
1440 guint
ltt_tracefile_cpu(LttTracefile
*tf
)
1445 guint
ltt_tracefile_tid(LttTracefile
*tf
)
1450 guint
ltt_tracefile_pgid(LttTracefile
*tf
)
1455 guint64
ltt_tracefile_creation(LttTracefile
*tf
)
1457 return tf
->creation
;
1459 /*****************************************************************************
1460 * Get the number of blocks in the tracefile
1461 ****************************************************************************/
1463 guint
ltt_tracefile_block_number(LttTracefile
*tf
)
1465 return tf
->num_blocks
;
1469 /* Seek to the first event in a tracefile that has a time equal or greater than
1470 * the time passed in parameter.
1472 * If the time parameter is outside the tracefile time span, seek to the first
1473 * event or if after, return ERANGE.
1475 * If the time parameter is before the first event, we have to seek specially to
1478 * If the time is after the end of the trace, return ERANGE.
1480 * Do a binary search to find the right block, then a sequential search in the
1481 * block to find the event.
1483 * In the special case where the time requested fits inside a block that has no
1484 * event corresponding to the requested time, the first event of the next block
1487 * IMPORTANT NOTE : // FIXME everywhere...
1489 * You MUST NOT do a ltt_tracefile_read right after a ltt_tracefile_seek_time :
1490 * you will jump over an event if you do.
1492 * Return value : 0 : no error, the tf->event can be used
1493 * ERANGE : time if after the last event of the trace
1494 * otherwise : this is an error.
1498 int ltt_tracefile_seek_time(LttTracefile
*tf
, LttTime time
)
1502 unsigned int block_num
, high
, low
;
1504 /* seek at the beginning of trace */
1505 err
= map_block(tf
, 0); /* First block */
1507 g_error("Can not map block");
1511 /* If the time is lower or equal the beginning of the trace,
1512 * go to the first event. */
1513 if(ltt_time_compare(time
, tf
->buffer
.begin
.timestamp
) <= 0) {
1514 ret
= ltt_tracefile_read(tf
);
1515 if(ret
== ERANGE
) goto range
;
1516 else if (ret
) goto fail
;
1517 goto found
; /* There is either no event in the trace or the event points
1518 to the first event in the trace */
1521 err
= map_block(tf
, tf
->num_blocks
- 1); /* Last block */
1523 g_error("Can not map block");
1527 /* If the time is after the end of the trace, return ERANGE. */
1528 if(ltt_time_compare(time
, tf
->buffer
.end
.timestamp
) > 0) {
1532 /* Binary search the block */
1533 high
= tf
->num_blocks
- 1;
1537 block_num
= ((high
-low
) / 2) + low
;
1539 err
= map_block(tf
, block_num
);
1541 g_error("Can not map block");
1545 /* We cannot divide anymore : this is what would happen if the time
1546 * requested was exactly between two consecutive buffers'end and start
1547 * timestamps. This is also what would happend if we didn't deal with out
1548 * of span cases prior in this function. */
1549 /* The event is right in the buffer!
1550 * (or in the next buffer first event) */
1552 ret
= ltt_tracefile_read(tf
);
1553 if(ret
== ERANGE
) goto range
; /* ERANGE or EPERM */
1554 else if(ret
) goto fail
;
1556 if(ltt_time_compare(time
, tf
->event
.event_time
) <= 0)
1560 } else if(ltt_time_compare(time
, tf
->buffer
.begin
.timestamp
) < 0) {
1561 /* go to lower part */
1562 high
= block_num
- 1;
1563 } else if(ltt_time_compare(time
, tf
->buffer
.end
.timestamp
) > 0) {
1564 /* go to higher part */
1565 low
= block_num
+ 1;
1566 } else {/* The event is right in the buffer!
1567 (or in the next buffer first event) */
1569 ret
= ltt_tracefile_read(tf
);
1570 if(ret
== ERANGE
) goto range
; /* ERANGE or EPERM */
1571 else if(ret
) goto fail
;
1573 if(ltt_time_compare(time
, tf
->event
.event_time
) <= 0)
1585 /* Error handling */
1587 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1588 g_quark_to_string(tf
->name
));
1593 int ltt_tracefile_seek_position(LttTracefile
*tf
, const LttEventPosition
*ep
) {
1597 if(ep
->tracefile
!= tf
) {
1601 err
= map_block(tf
, ep
->block
);
1603 g_error("Can not map block");
1607 tf
->event
.offset
= ep
->offset
;
1609 /* Put back the event real tsc */
1610 tf
->event
.tsc
= ep
->tsc
;
1611 tf
->buffer
.tsc
= ep
->tsc
;
1613 err
= ltt_tracefile_read_update_event(tf
);
1615 err
= ltt_tracefile_read_op(tf
);
1621 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1622 g_quark_to_string(tf
->name
));
1626 LttTime
ltt_interpolate_time_from_tsc(LttTracefile
*tf
, guint64 tsc
)
1630 if(tsc
> tf
->trace
->start_tsc
) {
1631 time
= ltt_time_from_uint64(
1632 (double)(tsc
- tf
->trace
->start_tsc
)
1633 * (1000000000.0 / tf
->trace
->freq_scale
)
1634 / (double)tf
->trace
->start_freq
);
1635 time
= ltt_time_add(tf
->trace
->start_time_from_tsc
, time
);
1637 time
= ltt_time_from_uint64(
1638 (double)(tf
->trace
->start_tsc
- tsc
)
1639 * (1000000000.0 / tf
->trace
->freq_scale
)
1640 / (double)tf
->trace
->start_freq
);
1641 time
= ltt_time_sub(tf
->trace
->start_time_from_tsc
, time
);
1646 /* Calculate the real event time based on the buffer boundaries */
1647 LttTime
ltt_interpolate_time(LttTracefile
*tf
, LttEvent
*event
)
1649 return ltt_interpolate_time_from_tsc(tf
, tf
->buffer
.tsc
);
1653 /* Get the current event of the tracefile : valid until the next read */
1654 LttEvent
*ltt_tracefile_get_event(LttTracefile
*tf
)
1661 /*****************************************************************************
1663 * ltt_tracefile_read : Read the next event in the tracefile
1668 * Returns 0 if an event can be used in tf->event.
1669 * Returns ERANGE on end of trace. The event in tf->event still can be used
1670 * (if the last block was not empty).
1671 * Returns EPERM on error.
1673 * This function does make the tracefile event structure point to the event
1674 * currently pointed to by the tf->event.
1676 * Note : you must call a ltt_tracefile_seek to the beginning of the trace to
1677 * reinitialize it after an error if you want results to be coherent.
1678 * It would be the case if a end of trace last buffer has no event : the end
1679 * of trace wouldn't be returned, but an error.
1680 * We make the assumption there is at least one event per buffer.
1681 ****************************************************************************/
1683 int ltt_tracefile_read(LttTracefile
*tf
)
1687 err
= ltt_tracefile_read_seek(tf
);
1689 err
= ltt_tracefile_read_update_event(tf
);
1691 err
= ltt_tracefile_read_op(tf
);
1697 int ltt_tracefile_read_seek(LttTracefile
*tf
)
1701 /* Get next buffer until we finally have an event, or end of trace */
1703 err
= ltt_seek_next_event(tf
);
1704 if(unlikely(err
== ENOPROTOOPT
)) {
1708 /* Are we at the end of the buffer ? */
1710 if(unlikely(tf
->buffer
.index
== tf
->num_blocks
-1)){ /* end of trace ? */
1713 /* get next block */
1714 err
= map_block(tf
, tf
->buffer
.index
+ 1);
1716 g_error("Can not map block");
1720 } else break; /* We found an event ! */
1727 /* do specific operation on events */
1728 int ltt_tracefile_read_op(LttTracefile
*tf
)
1734 /* do event specific operation */
1736 /* do something if its an heartbeat event : increment the heartbeat count */
1737 //if(event->facility_id == LTT_FACILITY_CORE)
1738 // if(event->event_id == LTT_EVENT_HEARTBEAT)
1739 // tf->cur_heart_beat_number++;
1745 /* same as ltt_tracefile_read, but does not seek to the next event nor call
1746 * event specific operation. */
1747 int ltt_tracefile_read_update_event(LttTracefile
*tf
)
1753 pos
= tf
->buffer
.head
+ event
->offset
;
1755 /* Read event header */
1757 /* Align the head */
1759 pos
+= ltt_align((size_t)pos
, tf
->trace
->arch_size
, tf
->alignment
);
1761 g_assert(tf
->has_heartbeat
);
1762 pos
+= ltt_align((size_t)pos
, sizeof(uint32_t), tf
->alignment
);
1765 if(tf
->has_heartbeat
) {
1766 event
->timestamp
= ltt_get_uint32(LTT_GET_BO(tf
),
1769 /* 32 bits -> 64 bits tsc */
1770 /* note : still works for seek and non seek cases. */
1771 if(event
->timestamp
< (0xFFFFFFFFULL
&tf
->buffer
.tsc
)) {
1772 tf
->buffer
.tsc
= ((tf
->buffer
.tsc
&0xFFFFFFFF00000000ULL
)
1774 | (guint64
)event
->timestamp
;
1775 event
->tsc
= tf
->buffer
.tsc
;
1778 tf
->buffer
.tsc
= (tf
->buffer
.tsc
&0xFFFFFFFF00000000ULL
)
1779 | (guint64
)event
->timestamp
;
1780 event
->tsc
= tf
->buffer
.tsc
;
1781 event
->compact_data
= 0;
1784 /* Compact header */
1785 /* We keep the LSB of the previous timestamp, to make sure
1786 * we never go back */
1787 event
->event_id
= event
->timestamp
>> tf
->tscbits
;
1788 event
->event_id
= event
->event_id
& ((1 << tf
->trace
->compact_event_bits
) - 1);
1789 event
->compact_data
= event
->timestamp
>>
1790 (tf
->trace
->compact_event_bits
+ tf
->tscbits
);
1791 //printf("tsc bits %u, ev bits %u init data %u\n",
1792 // tf->tscbits, tf->trace->compact_event_bits, event->compact_data);
1793 /* Put the compact data back in original endianness */
1794 event
->compact_data
= ltt_get_uint32(LTT_GET_BO(tf
), &event
->compact_data
);
1795 event
->event_size
= 0xFFFF;
1796 //printf("Found compact event %d\n", event->event_id);
1797 //printf("Compact data %d\n", event->compact_data);
1798 event
->timestamp
= event
->timestamp
<< tf
->tsc_lsb_truncate
;
1799 event
->timestamp
= event
->timestamp
& tf
->tsc_mask
;
1800 //printf("timestamp 0x%lX\n", event->timestamp);
1801 //printf("mask 0x%llX\n", tf->tsc_mask);
1802 //printf("mask_next 0x%llX\n", tf->tsc_mask_next_bit);
1803 //printf("previous tsc 0x%llX\n", tf->buffer.tsc);
1804 //printf("previous tsc&mask 0x%llX\n", tf->tsc_mask&tf->buffer.tsc);
1805 //printf("previous tsc&(~mask) 0x%llX\n", tf->buffer.tsc&(~tf->tsc_mask));
1806 if(event
->timestamp
< (tf
->tsc_mask
&tf
->buffer
.tsc
)) {
1808 tf
->buffer
.tsc
= ((tf
->buffer
.tsc
&(~tf
->tsc_mask
))
1809 + tf
->tsc_mask_next_bit
)
1810 | (guint64
)event
->timestamp
;
1811 event
->tsc
= tf
->buffer
.tsc
;
1813 //printf("no wrap\n");
1815 tf
->buffer
.tsc
= (tf
->buffer
.tsc
&(~tf
->tsc_mask
))
1816 | (guint64
)event
->timestamp
;
1817 event
->tsc
= tf
->buffer
.tsc
;
1819 //printf("current tsc 0x%llX\n", tf->buffer.tsc);
1821 pos
+= sizeof(guint32
);
1823 event
->tsc
= ltt_get_uint64(LTT_GET_BO(tf
), pos
);
1824 tf
->buffer
.tsc
= event
->tsc
;
1825 event
->compact_data
= 0;
1826 pos
+= sizeof(guint64
);
1828 event
->event_time
= ltt_interpolate_time(tf
, event
);
1831 event
->event_id
= *(guint16
*)pos
;
1832 pos
+= sizeof(guint16
);
1834 event
->event_size
= ltt_get_uint16(LTT_GET_BO(tf
), pos
);
1835 pos
+= sizeof(guint16
);
1839 /* Align the head */
1841 pos
+= ltt_align((size_t)pos
, tf
->trace
->arch_size
, tf
->alignment
);
1845 /* get the data size and update the event fields with the current
1846 * information. Also update the time if a heartbeat_full event is found. */
1847 ltt_update_event_size(tf
);
1853 /****************************************************************************
1855 * map_block : map a block from the file
1857 * lttdes : ltt trace file
1858 * whichBlock : the block which will be read
1861 * EINVAL : lseek fail
1862 * EIO : can not read from the file
1863 ****************************************************************************/
1865 gint
map_block(LttTracefile
* tf
, guint block_num
)
1867 int page_size
= getpagesize();
1868 struct ltt_block_start_header
*header
;
1870 g_assert(block_num
< tf
->num_blocks
);
1872 if(tf
->buffer
.head
!= NULL
) {
1873 if(munmap(tf
->buffer
.head
, PAGE_ALIGN(tf
->buf_size
))) {
1874 g_warning("unmap size : %u\n",
1875 PAGE_ALIGN(tf
->buf_size
));
1876 perror("munmap error");
1882 /* Multiple of pages aligned head */
1883 tf
->buffer
.head
= mmap(0,
1884 PAGE_ALIGN(tf
->buf_size
),
1885 PROT_READ
, MAP_PRIVATE
, tf
->fd
,
1886 PAGE_ALIGN((off_t
)tf
->buf_size
* (off_t
)block_num
));
1888 if(tf
->buffer
.head
== MAP_FAILED
) {
1889 perror("Error in allocating memory for buffer of tracefile");
1893 g_assert( ( (guint
)tf
->buffer
.head
&(8-1) ) == 0); // make sure it's aligned.
1896 tf
->buffer
.index
= block_num
;
1898 header
= (struct ltt_block_start_header
*)tf
->buffer
.head
;
1901 tf
->buffer
.begin
.timestamp
= ltt_time_add(
1902 ltt_time_from_uint64(
1903 ltt_get_uint64(LTT_GET_BO(tf
),
1904 &header
->begin
.timestamp
)
1905 - tf
->trace
->start_monotonic
),
1906 tf
->trace
->start_time
);
1908 //g_debug("block %u begin : %lu.%lu", block_num,
1909 // tf->buffer.begin.timestamp.tv_sec, tf->buffer.begin.timestamp.tv_nsec);
1910 tf
->buffer
.begin
.cycle_count
= ltt_get_uint64(LTT_GET_BO(tf
),
1911 &header
->begin
.cycle_count
);
1912 tf
->buffer
.begin
.freq
= ltt_get_uint64(LTT_GET_BO(tf
),
1913 &header
->begin
.freq
);
1914 if(tf
->buffer
.begin
.freq
== 0)
1915 tf
->buffer
.begin
.freq
= tf
->trace
->start_freq
;
1917 tf
->buffer
.begin
.timestamp
= ltt_interpolate_time_from_tsc(tf
,
1918 tf
->buffer
.begin
.cycle_count
);
1921 ltt_time_from_uint64(
1922 (double)(tf
->buffer
.begin
.cycle_count
1923 - tf
->trace
->start_tsc
) * 1000000.0
1924 / (double)tf
->trace
->start_freq
),
1925 tf
->trace
->start_time_from_tsc
);
1929 tf
->buffer
.end
.timestamp
= ltt_time_add(
1930 ltt_time_from_uint64(
1931 ltt_get_uint64(LTT_GET_BO(tf
),
1932 &header
->end
.timestamp
)
1933 - tf
->trace
->start_monotonic
),
1934 tf
->trace
->start_time
);
1936 //g_debug("block %u end : %lu.%lu", block_num,
1937 // tf->buffer.end.timestamp.tv_sec, tf->buffer.end.timestamp.tv_nsec);
1938 tf
->buffer
.end
.cycle_count
= ltt_get_uint64(LTT_GET_BO(tf
),
1939 &header
->end
.cycle_count
);
1940 tf
->buffer
.end
.freq
= ltt_get_uint64(LTT_GET_BO(tf
),
1942 if(tf
->buffer
.end
.freq
== 0)
1943 tf
->buffer
.end
.freq
= tf
->trace
->start_freq
;
1945 tf
->buffer
.lost_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1946 &header
->lost_size
);
1947 tf
->buffer
.end
.timestamp
= ltt_interpolate_time_from_tsc(tf
,
1948 tf
->buffer
.end
.cycle_count
);
1951 ltt_time_from_uint64(
1952 (double)(tf
->buffer
.end
.cycle_count
1953 - tf
->trace
->start_tsc
) * 1000000.0
1954 / (double)tf
->trace
->start_freq
),
1955 tf
->trace
->start_time_from_tsc
);
1957 tf
->buffer
.tsc
= tf
->buffer
.begin
.cycle_count
;
1958 tf
->event
.tsc
= tf
->buffer
.tsc
;
1959 tf
->buffer
.freq
= tf
->buffer
.begin
.freq
;
1962 * eventually support variable buffer size : will need a partial pre-read of
1963 * the headers to create an index when we open the trace... eventually. */
1964 g_assert(tf
->buf_size
== ltt_get_uint32(LTT_GET_BO(tf
),
1965 &header
->buf_size
));
1967 /* Now that the buffer is mapped, calculate the time interpolation for the
1970 // tf->buffer.nsecs_per_cycle = calc_nsecs_per_cycle(tf);
1971 //tf->buffer.cyc2ns_scale = calc_nsecs_per_cycle(tf);
1973 /* Make the current event point to the beginning of the buffer :
1974 * it means that the event read must get the first event. */
1975 tf
->event
.tracefile
= tf
;
1976 tf
->event
.block
= block_num
;
1977 tf
->event
.offset
= 0;
1986 /* It will update the fields offsets too */
1987 void ltt_update_event_size(LttTracefile
*tf
)
1991 struct marker_info
*info
;
1993 switch((enum marker_id
)tf
->event
.event_id
) {
1994 case MARKER_ID_SET_MARKER_ID
:
1995 size
= strlen((char*)tf
->event
.data
) + 1;
1996 //g_debug("marker %s id set", (char*)tf->event.data);
1997 size
+= ltt_align(size
, sizeof(guint16
), tf
->alignment
);
1998 size
+= sizeof(guint16
);
1999 size
+= sizeof(guint8
);
2000 size
+= sizeof(guint8
);
2001 size
+= sizeof(guint8
);
2002 size
+= sizeof(guint8
);
2003 size
+= sizeof(guint8
);
2005 case MARKER_ID_SET_MARKER_FORMAT
:
2006 //g_debug("marker %s format set", (char*)tf->event.data);
2007 size
= strlen((char*)tf
->event
.data
) + 1;
2008 size
+= strlen((char*)tf
->event
.data
+ size
) + 1;
2010 case MARKER_ID_HEARTBEAT_32
:
2011 //g_debug("Update Event heartbeat 32 bits");
2013 case MARKER_ID_HEARTBEAT_64
:
2014 //g_debug("Update Event heartbeat 64 bits");
2015 tscdata
= (char*)(tf
->event
.data
);
2016 tf
->event
.tsc
= ltt_get_uint64(LTT_GET_BO(tf
), tscdata
);
2017 tf
->buffer
.tsc
= tf
->event
.tsc
;
2018 tf
->event
.event_time
= ltt_interpolate_time(tf
, &tf
->event
);
2019 size
= ltt_align(size
, sizeof(guint64
), tf
->alignment
);
2020 size
+= sizeof(guint64
);
2024 info
= marker_get_info_from_id(tf
->trace
, tf
->event
.event_id
);
2025 if (tf
->event
.event_id
>= MARKER_CORE_IDS
)
2026 g_assert(info
!= NULL
);
2028 /* Do not update field offsets of core markers when initially reading the
2029 * facility tracefile when the infos about these markers do not exist yet.
2031 if (likely(info
&& info
->fields
)) {
2032 if (info
->size
!= -1)
2035 size
= marker_update_fields_offsets(marker_get_info_from_id(tf
->trace
,
2036 tf
->event
.event_id
), tf
->event
.data
);
2039 tf
->event
.data_size
= size
;
2041 /* Check consistency between kernel and LTTV structure sizes */
2042 if(tf
->event
.event_size
== 0xFFFF) {
2043 /* Event size too big to fit in the event size field */
2044 tf
->event
.event_size
= tf
->event
.data_size
;
2046 if (tf
->event
.data_size
!= tf
->event
.event_size
) {
2047 struct marker_info
*info
= marker_get_info_from_id(tf
->trace
,
2048 tf
->event
.event_id
);
2049 g_error("Kernel/LTTV event size differs for event %s: kernel %u, LTTV %u",
2050 g_quark_to_string(info
->name
),
2051 tf
->event
.event_size
, tf
->event
.data_size
);
2056 LttEventType
*event_type
=
2057 ltt_facility_eventtype_get(f
, tf
->event
.event_id
);
2060 g_warning("Unknown event id %hhu in facility %s in tracefile %s",
2062 g_quark_to_string(f
->name
),
2063 g_quark_to_string(tf
->name
));
2064 goto event_type_error
;
2067 /* Compute the dynamic offsets */
2068 compute_offsets(tf
, f
, event_type
, &size
, tf
->event
.data
);
2070 //g_debug("Event root field : f.e %hhu.%hhu size %zd",
2071 // tf->event.facility_id,
2072 // tf->event.event_id, size);
2075 tf
->event
.data_size
= size
;
2077 /* Check consistency between kernel and LTTV structure sizes */
2078 if(tf
->event
.event_size
== 0xFFFF) {
2079 /* Event size too big to fit in the event size field */
2080 tf
->event
.event_size
= tf
->event
.data_size
;
2082 if (tf
->event
.data_size
!= tf
->event
.event_size
) {
2083 g_error("Kernel/LTTV event size differs for event %s.%s: kernel %u, LTTV %u",
2084 g_quark_to_string(f
->name
), g_quark_to_string(event_type
->name
),
2085 tf
->event
.event_size
, tf
->event
.data_size
);
2088 //g_assert(tf->event.data_size == tf->event.event_size);
2094 if(tf
->event
.event_size
== 0xFFFF) {
2095 g_error("Cannot jump over an unknown event bigger than 0xFFFE bytes");
2097 /* The facility is unknown : use the kernel information about this event
2098 * to jump over it. */
2099 tf
->event
.data_size
= tf
->event
.event_size
;
2104 /* Take the tf current event offset and use the event facility id and event id
2105 * to figure out where is the next event offset.
2107 * This is an internal function not aiming at being used elsewhere : it will
2108 * not jump over the current block limits. Please consider using
2109 * ltt_tracefile_read to do this.
2111 * Returns 0 on success
2112 * ERANGE if we are at the end of the buffer.
2113 * ENOPROTOOPT if an error occured when getting the current event size.
2115 int ltt_seek_next_event(LttTracefile
*tf
)
2120 /* seek over the buffer header if we are at the buffer start */
2121 if(tf
->event
.offset
== 0) {
2122 tf
->event
.offset
+= tf
->buffer_header_size
;
2124 if(tf
->event
.offset
== tf
->buf_size
- tf
->buffer
.lost_size
) {
2131 pos
= tf
->event
.data
;
2133 if(tf
->event
.data_size
< 0) goto error
;
2135 pos
+= (size_t)tf
->event
.data_size
;
2137 tf
->event
.offset
= pos
- tf
->buffer
.head
;
2139 if(tf
->event
.offset
== tf
->buf_size
- tf
->buffer
.lost_size
) {
2143 g_assert(tf
->event
.offset
< tf
->buf_size
- tf
->buffer
.lost_size
);
2149 g_error("Error in ltt_seek_next_event for tracefile %s",
2150 g_quark_to_string(tf
->name
));
2155 /*****************************************************************************
2157 * calc_nsecs_per_cycle : calculate nsecs per cycle for current block
2159 * 1.0 / (freq(khz) *1000) * 1000000000
2162 ****************************************************************************/
2163 /* from timer_tsc.c */
2164 #define CYC2NS_SCALE_FACTOR 10
2165 static guint32
calc_nsecs_per_cycle(LttTracefile
* tf
)
2167 //return 1e6 / (double)tf->buffer.freq;
2168 guint32 cpu_mhz
= tf
->buffer
.freq
/ 1000;
2169 guint32 cyc2ns_scale
= (1000 << CYC2NS_SCALE_FACTOR
)/cpu_mhz
;
2171 return cyc2ns_scale
;
2172 // return 1e6 / (double)tf->buffer.freq;
2175 static guint64
cycles_2_ns(LttTracefile
*tf
, guint64 cycles
)
2177 return (cycles
* tf
->buffer
.cyc2ns_scale
) >> CYC2NS_SCALE_FACTOR
;
2182 void setFieldsOffset(LttTracefile
*tf
, LttEventType
*evT
,void *evD
)
2184 LttField
* rootFld
= evT
->root_field
;
2185 // rootFld->base_address = evD;
2188 rootFld
->field_size
= getFieldtypeSize(tf
, evT
->facility
,
2189 evT
, 0,0,rootFld
, evD
);
2193 /*****************************************************************************
2195 * set_fields_offsets : set the precomputable offset of the fields
2197 * tracefile : opened trace file
2198 * event_type : the event type
2199 ****************************************************************************/
2201 void set_fields_offsets(LttTracefile
*tf
, LttEventType
*event_type
)
2203 LttField
*field
= event_type
->root_field
;
2204 enum field_status fixed_root
= FIELD_FIXED
, fixed_parent
= FIELD_FIXED
;
2207 preset_field_type_size(tf
, event_type
, 0, 0,
2208 &fixed_root
, &fixed_parent
,
2215 /*****************************************************************************
2217 * get_alignment : Get the alignment needed for a field.
2221 * returns : The size on which it must be aligned.
2223 ****************************************************************************/
2225 off_t
get_alignment(LttField
*field
)
2227 LttType
*type
= &field
->field_type
;
2229 switch(type
->type_class
) {
2231 case LTT_UINT_FIXED
:
2246 /* Align offset on type size */
2247 g_assert(field
->field_size
!= 0);
2248 return field
->field_size
;
2254 g_assert(type
->fields
->len
== 1);
2256 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
2257 return get_alignment(child
);
2261 g_assert(type
->fields
->len
== 2);
2264 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
2266 localign
= max(localign
, get_alignment(child
));
2268 child
= &g_array_index(type
->fields
, LttField
, 1);
2269 localign
= max(localign
, get_alignment(child
));
2280 for(i
=0; i
<type
->fields
->len
; i
++) {
2281 LttField
*child
= &g_array_index(type
->fields
, LttField
, i
);
2282 localign
= max(localign
, get_alignment(child
));
2289 g_error("get_alignment : unknown type");
2296 /*****************************************************************************
2298 * field_compute_static_size : Determine the size of fields known by their
2299 * sole definition. Unions, arrays and struct sizes might be known, but
2300 * the parser does not give that information.
2305 ****************************************************************************/
2307 void field_compute_static_size(LttFacility
*fac
, LttField
*field
)
2309 LttType
*type
= &field
->field_type
;
2311 switch(type
->type_class
) {
2313 case LTT_UINT_FIXED
:
2332 /* note this : array type size is the number of elements in the array,
2333 * while array field size of the length of the array in bytes */
2334 g_assert(type
->fields
->len
== 1);
2336 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
2337 field_compute_static_size(fac
, child
);
2339 if(child
->field_size
!= 0) {
2340 field
->field_size
= type
->size
* child
->field_size
;
2341 field
->dynamic_offsets
= g_array_sized_new(FALSE
, TRUE
,
2342 sizeof(off_t
), type
->size
);
2344 field
->field_size
= 0;
2349 g_assert(type
->fields
->len
== 2);
2351 off_t local_offset
= 0;
2352 LttField
*child
= &g_array_index(type
->fields
, LttField
, 1);
2353 field_compute_static_size(fac
, child
);
2354 field
->field_size
= 0;
2356 if(child
->field_size
!= 0) {
2357 field
->dynamic_offsets
= g_array_sized_new(FALSE
, TRUE
,
2358 sizeof(off_t
), SEQUENCE_AVG_ELEMENTS
);
2366 for(i
=0;i
<type
->fields
->len
;i
++) {
2367 LttField
*child
= &g_array_index(type
->fields
, LttField
, i
);
2368 field_compute_static_size(fac
, child
);
2369 if(child
->field_size
!= 0) {
2370 type
->size
+= ltt_align(type
->size
, get_alignment(child
),
2372 type
->size
+= child
->field_size
;
2374 /* As soon as we find a child with variable size, we have
2375 * a variable size */
2380 field
->field_size
= type
->size
;
2384 g_error("field_static_size : unknown type");
2391 /*****************************************************************************
2393 * precompute_fields_offsets : set the precomputable offset of the fields
2397 * offset : pointer to the current offset, must be incremented
2399 * return : 1 : found a variable length field, stop the processing.
2401 ****************************************************************************/
2404 gint
precompute_fields_offsets(LttFacility
*fac
, LttField
*field
, off_t
*offset
, gint is_compact
)
2406 LttType
*type
= &field
->field_type
;
2408 if(unlikely(is_compact
)) {
2409 g_assert(field
->field_size
!= 0);
2410 /* FIXME THIS IS A HUUUUUGE hack :
2411 * offset is between the compact_data field in struct LttEvent
2412 * and the address of the field root in the memory map.
2413 * ark. Both will stay at the same addresses while the event
2414 * is readable, so it's ok.
2416 field
->offset_root
= 0;
2417 field
->fixed_root
= FIELD_FIXED
;
2421 switch(type
->type_class
) {
2423 case LTT_UINT_FIXED
:
2438 g_assert(field
->field_size
!= 0);
2439 /* Align offset on type size */
2440 *offset
+= ltt_align(*offset
, get_alignment(field
),
2442 /* remember offset */
2443 field
->offset_root
= *offset
;
2444 field
->fixed_root
= FIELD_FIXED
;
2445 /* Increment offset */
2446 *offset
+= field
->field_size
;
2450 field
->offset_root
= *offset
;
2451 field
->fixed_root
= FIELD_FIXED
;
2455 g_assert(type
->fields
->len
== 1);
2457 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
2459 *offset
+= ltt_align(*offset
, get_alignment(field
),
2462 /* remember offset */
2463 field
->offset_root
= *offset
;
2464 field
->array_offset
= *offset
;
2465 field
->fixed_root
= FIELD_FIXED
;
2467 /* Let the child be variable */
2468 //precompute_fields_offsets(tf, child, offset);
2470 if(field
->field_size
!= 0) {
2471 /* Increment offset */
2472 /* field_size is the array size in bytes */
2473 *offset
+= field
->field_size
;
2481 g_assert(type
->fields
->len
== 2);
2486 *offset
+= ltt_align(*offset
, get_alignment(field
),
2489 /* remember offset */
2490 field
->offset_root
= *offset
;
2491 field
->fixed_root
= FIELD_FIXED
;
2493 child
= &g_array_index(type
->fields
, LttField
, 0);
2494 ret
= precompute_fields_offsets(fac
, child
, offset
, is_compact
);
2495 g_assert(ret
== 0); /* Seq len cannot have variable len */
2497 child
= &g_array_index(type
->fields
, LttField
, 1);
2498 *offset
+= ltt_align(*offset
, get_alignment(child
),
2500 field
->array_offset
= *offset
;
2501 /* Let the child be variable. */
2502 //ret = precompute_fields_offsets(fac, child, offset);
2504 /* Cannot precompute fields offsets of sequence members, and has
2505 * variable length. */
2515 *offset
+= ltt_align(*offset
, get_alignment(field
),
2517 /* remember offset */
2518 field
->offset_root
= *offset
;
2519 field
->fixed_root
= FIELD_FIXED
;
2521 for(i
=0; i
< type
->fields
->len
; i
++) {
2522 child
= &g_array_index(type
->fields
, LttField
, i
);
2523 ret
= precompute_fields_offsets(fac
, child
, offset
, is_compact
);
2536 *offset
+= ltt_align(*offset
, get_alignment(field
),
2538 /* remember offset */
2539 field
->offset_root
= *offset
;
2540 field
->fixed_root
= FIELD_FIXED
;
2542 for(i
=0; i
< type
->fields
->len
; i
++) {
2543 *offset
= field
->offset_root
;
2544 child
= &g_array_index(type
->fields
, LttField
, i
);
2545 ret
= precompute_fields_offsets(fac
, child
, offset
, is_compact
);
2549 *offset
= field
->offset_root
+ field
->field_size
;
2556 g_error("precompute_fields_offsets : unknown type");
2565 /*****************************************************************************
2567 * precompute_offsets : set the precomputable offset of an event type
2570 * event : event type
2572 ****************************************************************************/
2573 void precompute_offsets(LttFacility
*fac
, LttEventType
*event
)
2579 /* First, compute the size of fixed size fields. Will determine size for
2580 * arrays, struct and unions, which is not done by the parser */
2581 for(i
=0; i
<event
->fields
->len
; i
++) {
2582 LttField
*field
= &g_array_index(event
->fields
, LttField
, i
);
2583 field_compute_static_size(fac
, field
);
2586 /* Precompute all known offsets */
2587 for(i
=0; i
<event
->fields
->len
; i
++) {
2588 LttField
*field
= &g_array_index(event
->fields
, LttField
, i
);
2589 if(event
->has_compact_data
&& i
== 0)
2590 ret
= precompute_fields_offsets(fac
, field
, &offset
, 1);
2592 ret
= precompute_fields_offsets(fac
, field
, &offset
, 0);
2600 /*****************************************************************************
2602 * preset_field_type_size : set the fixed sizes of the field type
2605 * event_type : event type
2606 * offset_root : offset from the root
2607 * offset_parent : offset from the parent
2608 * fixed_root : Do we know a fixed offset to the root ?
2609 * fixed_parent : Do we know a fixed offset to the parent ?
2611 ****************************************************************************/
2615 // preset the fixed size offsets. Calculate them just like genevent-new : an
2616 // increment of a *to value that represents the offset from the start of the
2618 // The preset information is : offsets up to (and including) the first element
2619 // of variable size. All subsequent fields must be flagged "VARIABLE OFFSET".
2621 void preset_field_type_size(LttTracefile
*tf
, LttEventType
*event_type
,
2622 off_t offset_root
, off_t offset_parent
,
2623 enum field_status
*fixed_root
, enum field_status
*fixed_parent
,
2626 enum field_status local_fixed_root
, local_fixed_parent
;
2630 g_assert(field
->fixed_root
== FIELD_UNKNOWN
);
2631 g_assert(field
->fixed_parent
== FIELD_UNKNOWN
);
2632 g_assert(field
->fixed_size
== FIELD_UNKNOWN
);
2634 type
= field
->field_type
;
2636 field
->fixed_root
= *fixed_root
;
2637 if(field
->fixed_root
== FIELD_FIXED
)
2638 field
->offset_root
= offset_root
;
2640 field
->offset_root
= 0;
2642 field
->fixed_parent
= *fixed_parent
;
2643 if(field
->fixed_parent
== FIELD_FIXED
)
2644 field
->offset_parent
= offset_parent
;
2646 field
->offset_parent
= 0;
2648 size_t current_root_offset
;
2649 size_t current_offset
;
2650 enum field_status current_child_status
, final_child_status
;
2653 switch(type
->type_class
) {
2655 case LTT_UINT_FIXED
:
2664 field
->field_size
= ltt_type_size(tf
->trace
, type
);
2665 field
->fixed_size
= FIELD_FIXED
;
2668 field
->field_size
= (off_t
)event_type
->facility
->pointer_size
;
2669 field
->fixed_size
= FIELD_FIXED
;
2673 field
->field_size
= (off_t
)event_type
->facility
->long_size
;
2674 field
->fixed_size
= FIELD_FIXED
;
2679 field
->field_size
= (off_t
)event_type
->facility
->size_t_size
;
2680 field
->fixed_size
= FIELD_FIXED
;
2683 local_fixed_root
= FIELD_VARIABLE
;
2684 local_fixed_parent
= FIELD_VARIABLE
;
2685 preset_field_type_size(tf
, event_type
,
2687 &local_fixed_root
, &local_fixed_parent
,
2689 field
->fixed_size
= FIELD_VARIABLE
;
2690 field
->field_size
= 0;
2691 *fixed_root
= FIELD_VARIABLE
;
2692 *fixed_parent
= FIELD_VARIABLE
;
2695 field
->fixed_size
= FIELD_VARIABLE
;
2696 field
->field_size
= 0;
2697 *fixed_root
= FIELD_VARIABLE
;
2698 *fixed_parent
= FIELD_VARIABLE
;
2701 local_fixed_root
= FIELD_VARIABLE
;
2702 local_fixed_parent
= FIELD_VARIABLE
;
2703 preset_field_type_size(tf
, event_type
,
2705 &local_fixed_root
, &local_fixed_parent
,
2707 field
->fixed_size
= field
->child
[0]->fixed_size
;
2708 if(field
->fixed_size
== FIELD_FIXED
) {
2709 field
->field_size
= type
->element_number
* field
->child
[0]->field_size
;
2711 field
->field_size
= 0;
2712 *fixed_root
= FIELD_VARIABLE
;
2713 *fixed_parent
= FIELD_VARIABLE
;
2717 current_root_offset
= field
->offset_root
;
2719 current_child_status
= FIELD_FIXED
;
2720 for(i
=0;i
<type
->element_number
;i
++) {
2721 preset_field_type_size(tf
, event_type
,
2722 current_root_offset
, current_offset
,
2723 fixed_root
, ¤t_child_status
,
2725 if(current_child_status
== FIELD_FIXED
) {
2726 current_root_offset
+= field
->child
[i
]->field_size
;
2727 current_offset
+= field
->child
[i
]->field_size
;
2729 current_root_offset
= 0;
2733 if(current_child_status
!= FIELD_FIXED
) {
2734 *fixed_parent
= current_child_status
;
2735 field
->field_size
= 0;
2736 field
->fixed_size
= current_child_status
;
2738 field
->field_size
= current_offset
;
2739 field
->fixed_size
= FIELD_FIXED
;
2743 current_root_offset
= field
->offset_root
;
2746 final_child_status
= FIELD_FIXED
;
2747 for(i
=0;i
<type
->element_number
;i
++) {
2748 enum field_status current_root_child_status
= FIELD_FIXED
;
2749 enum field_status current_child_status
= FIELD_FIXED
;
2750 preset_field_type_size(tf
, event_type
,
2751 current_root_offset
, current_offset
,
2752 ¤t_root_child_status
, ¤t_child_status
,
2754 if(current_child_status
!= FIELD_FIXED
)
2755 final_child_status
= current_child_status
;
2757 max_size
= max(max_size
, field
->child
[i
]->field_size
);
2759 if(final_child_status
!= FIELD_FIXED
) {
2760 g_error("LTTV does not support variable size fields in unions.");
2761 /* This will stop the application. */
2762 *fixed_root
= final_child_status
;
2763 *fixed_parent
= final_child_status
;
2764 field
->field_size
= 0;
2765 field
->fixed_size
= current_child_status
;
2767 field
->field_size
= max_size
;
2768 field
->fixed_size
= FIELD_FIXED
;
2772 g_error("unexpected type NONE");
2779 /*****************************************************************************
2781 * check_fields_compatibility : Check for compatibility between two fields :
2782 * do they use the same inner structure ?
2784 * event_type1 : event type
2785 * event_type2 : event type
2788 *Returns : 0 if identical
2790 ****************************************************************************/
2791 // this function checks for equality of field types. Therefore, it does not use
2792 // per se offsets. For instance, an aligned version of a structure is
2793 // compatible with an unaligned version of the same structure.
2795 gint
check_fields_compatibility(LttEventType
*event_type1
,
2796 LttEventType
*event_type2
,
2797 LttField
*field1
, LttField
*field2
)
2799 guint different
= 0;
2803 if(field1
== NULL
) {
2804 if(field2
== NULL
) goto end
;
2809 } else if(field2
== NULL
) {
2814 type1
= &field1
->field_type
;
2815 type2
= &field2
->field_type
;
2817 if(type1
->type_class
!= type2
->type_class
) {
2821 if(type1
->network
!= type2
->network
) {
2826 switch(type1
->type_class
) {
2828 case LTT_UINT_FIXED
:
2843 if(field1
->field_size
!= field2
->field_size
)
2850 LttField
*child1
= &g_array_index(type1
->fields
, LttField
, 0);
2851 LttField
*child2
= &g_array_index(type2
->fields
, LttField
, 0);
2853 if(type1
->size
!= type2
->size
)
2855 if(check_fields_compatibility(event_type1
, event_type2
, child1
, child2
))
2861 LttField
*child1
= &g_array_index(type1
->fields
, LttField
, 1);
2862 LttField
*child2
= &g_array_index(type2
->fields
, LttField
, 1);
2864 if(check_fields_compatibility(event_type1
, event_type2
, child1
, child2
))
2874 if(type1
->fields
->len
!= type2
->fields
->len
) {
2879 for(i
=0; i
< type1
->fields
->len
; i
++) {
2882 child1
= &g_array_index(type1
->fields
, LttField
, i
);
2883 child2
= &g_array_index(type2
->fields
, LttField
, i
);
2884 different
= check_fields_compatibility(event_type1
,
2885 event_type2
, child1
, child2
);
2887 if(different
) break;
2893 g_error("check_fields_compatibility : unknown type");
2902 gint
check_fields_compatibility(LttEventType
*event_type1
,
2903 LttEventType
*event_type2
,
2904 LttField
*field1
, LttField
*field2
)
2906 guint different
= 0;
2911 if(field1
== NULL
) {
2912 if(field2
== NULL
) goto end
;
2917 } else if(field2
== NULL
) {
2922 g_assert(field1
->fixed_root
!= FIELD_UNKNOWN
);
2923 g_assert(field2
->fixed_root
!= FIELD_UNKNOWN
);
2924 g_assert(field1
->fixed_parent
!= FIELD_UNKNOWN
);
2925 g_assert(field2
->fixed_parent
!= FIELD_UNKNOWN
);
2926 g_assert(field1
->fixed_size
!= FIELD_UNKNOWN
);
2927 g_assert(field2
->fixed_size
!= FIELD_UNKNOWN
);
2929 type1
= field1
->field_type
;
2930 type2
= field2
->field_type
;
2932 if(type1
->type_class
!= type2
->type_class
) {
2936 if(type1
->element_name
!= type2
->element_name
) {
2941 switch(type1
->type_class
) {
2943 case LTT_UINT_FIXED
:
2958 if(field1
->field_size
!= field2
->field_size
) {
2964 if(type1
->element_number
!= type2
->element_number
) {
2968 for(i
=0;i
<type1
->element_number
;i
++) {
2969 if(type1
->enum_strings
[i
] != type2
->enum_strings
[i
]) {
2976 /* Two elements : size and child */
2977 g_assert(type1
->element_number
!= type2
->element_number
);
2978 for(i
=0;i
<type1
->element_number
;i
++) {
2979 if(check_fields_compatibility(event_type1
, event_type2
,
2980 field1
->child
[0], field2
->child
[0])) {
2989 if(field1
->field_size
!= field2
->field_size
) {
2993 /* Two elements : size and child */
2994 g_assert(type1
->element_number
!= type2
->element_number
);
2995 for(i
=0;i
<type1
->element_number
;i
++) {
2996 if(check_fields_compatibility(event_type1
, event_type2
,
2997 field1
->child
[0], field2
->child
[0])) {
3005 if(type1
->element_number
!= type2
->element_number
) {
3009 for(i
=0;i
<type1
->element_number
;i
++) {
3010 if(check_fields_compatibility(event_type1
, event_type2
,
3011 field1
->child
[0], field2
->child
[0])) {
3024 /*****************************************************************************
3026 * ltt_get_int : get an integer number
3028 * reverse_byte_order: must we reverse the byte order ?
3029 * size : the size of the integer
3030 * ptr : the data pointer
3032 * gint64 : a 64 bits integer
3033 ****************************************************************************/
3035 gint64
ltt_get_int(gboolean reverse_byte_order
, gint size
, void *data
)
3040 case 1: val
= *((gint8
*)data
); break;
3041 case 2: val
= ltt_get_int16(reverse_byte_order
, data
); break;
3042 case 4: val
= ltt_get_int32(reverse_byte_order
, data
); break;
3043 case 8: val
= ltt_get_int64(reverse_byte_order
, data
); break;
3044 default: val
= ltt_get_int64(reverse_byte_order
, data
);
3045 g_critical("get_int : integer size %d unknown", size
);
3052 /*****************************************************************************
3054 * ltt_get_uint : get an unsigned integer number
3056 * reverse_byte_order: must we reverse the byte order ?
3057 * size : the size of the integer
3058 * ptr : the data pointer
3060 * guint64 : a 64 bits unsigned integer
3061 ****************************************************************************/
3063 guint64
ltt_get_uint(gboolean reverse_byte_order
, gint size
, void *data
)
3068 case 1: val
= *((gint8
*)data
); break;
3069 case 2: val
= ltt_get_uint16(reverse_byte_order
, data
); break;
3070 case 4: val
= ltt_get_uint32(reverse_byte_order
, data
); break;
3071 case 8: val
= ltt_get_uint64(reverse_byte_order
, data
); break;
3072 default: val
= ltt_get_uint64(reverse_byte_order
, data
);
3073 g_critical("get_uint : unsigned integer size %d unknown",
3082 /* get the node name of the system */
3084 char * ltt_trace_system_description_node_name (LttSystemDescription
* s
)
3086 return s
->node_name
;
3090 /* get the domain name of the system */
3092 char * ltt_trace_system_description_domain_name (LttSystemDescription
* s
)
3094 return s
->domain_name
;
3098 /* get the description of the system */
3100 char * ltt_trace_system_description_description (LttSystemDescription
* s
)
3102 return s
->description
;
3106 /* get the NTP corrected start time of the trace */
3107 LttTime
ltt_trace_start_time(LttTrace
*t
)
3109 return t
->start_time
;
3112 /* get the monotonic start time of the trace */
3113 LttTime
ltt_trace_start_time_monotonic(LttTrace
*t
)
3115 return t
->start_time_from_tsc
;
3118 LttTracefile
*ltt_tracefile_new()
3120 return g_new(LttTracefile
, 1);
3123 void ltt_tracefile_destroy(LttTracefile
*tf
)
3128 void ltt_tracefile_copy(LttTracefile
*dest
, const LttTracefile
*src
)
3133 /* Before library loading... */
3137 LTT_FACILITY_NAME_HEARTBEAT
= g_quark_from_string("heartbeat");
3138 LTT_EVENT_NAME_HEARTBEAT
= g_quark_from_string("heartbeat");
3139 LTT_EVENT_NAME_HEARTBEAT_FULL
= g_quark_from_string("heartbeat_full");
3141 LTT_TRACEFILE_NAME_FACILITIES
= g_quark_from_string("/control/facilities");