1 /* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2005 Mathieu Desnoyers
4 * Complete rewrite from the original version made by XangXiu Yang.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License Version 2.1 as published by the Free Software Foundation.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
30 #include <sys/types.h>
45 #include "ltt-private.h"
46 #include <ltt/trace.h>
47 #include <ltt/event.h>
48 //#include <ltt/type.h>
49 #include <ltt/ltt-types.h>
50 #include <ltt/marker.h>
52 /* Facility names used in this file */
54 GQuark LTT_FACILITY_NAME_HEARTBEAT
,
55 LTT_EVENT_NAME_HEARTBEAT
,
56 LTT_EVENT_NAME_HEARTBEAT_FULL
;
57 GQuark LTT_TRACEFILE_NAME_FACILITIES
;
64 #define __UNUSED__ __attribute__((__unused__))
66 #define g_info(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, format)
69 #define g_debug(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format)
74 /* Those macros must be called from within a function where page_size is a known
76 #define PAGE_MASK (~(page_size-1))
77 #define PAGE_ALIGN(addr) (((addr)+page_size-1)&PAGE_MASK)
79 /* set the offset of the fields belonging to the event,
80 need the information of the archecture */
81 //void set_fields_offsets(LttTracefile *tf, LttEventType *event_type);
82 //size_t get_fields_offsets(LttTracefile *tf, LttEventType *event_type, void *data);
84 /* get the size of the field type according to
85 * The facility size information. */
87 static inline void preset_field_type_size(LttTracefile
*tf
,
88 LttEventType
*event_type
,
89 off_t offset_root
, off_t offset_parent
,
90 enum field_status
*fixed_root
, enum field_status
*fixed_parent
,
94 /* map a fixed size or a block information from the file (fd) */
95 static gint
map_block(LttTracefile
* tf
, guint block_num
);
97 /* calculate nsec per cycles for current block */
99 static guint32
calc_nsecs_per_cycle(LttTracefile
* t
);
100 static guint64
cycles_2_ns(LttTracefile
*tf
, guint64 cycles
);
103 /* go to the next event */
104 static int ltt_seek_next_event(LttTracefile
*tf
);
106 //void ltt_update_event_size(LttTracefile *tf);
108 static int open_tracefiles(LttTrace
*trace
, gchar
*root_path
,
109 gchar
*relative_path
);
110 static int ltt_process_facility_tracefile(LttTracefile
*tf
);
111 static void ltt_tracefile_time_span_get(LttTracefile
*tf
,
112 LttTime
*start
, LttTime
*end
);
113 static void group_time_span_get(GQuark name
, gpointer data
, gpointer user_data
);
114 static gint
map_block(LttTracefile
* tf
, guint block_num
);
115 static int ltt_seek_next_event(LttTracefile
*tf
);
116 static void __attribute__((constructor
)) init(void);
117 static void ltt_update_event_size(LttTracefile
*tf
);
119 /* Enable event debugging */
120 static int a_event_debug
= 0;
122 void ltt_event_debug(int state
)
124 a_event_debug
= state
;
127 //void precompute_offsets(LttFacility *fac, LttEventType *event);
130 /* Functions to parse system.xml file (using glib xml parser) */
131 static void parser_start_element (GMarkupParseContext __UNUSED__
*context
,
132 const gchar
*element_name
,
133 const gchar
**attribute_names
,
134 const gchar
**attribute_values
,
139 LttSystemDescription
* des
= (LttSystemDescription
* )user_data
;
140 if(strcmp("system", element_name
)){
141 *error
= g_error_new(G_MARKUP_ERROR
,
143 "This is not system.xml file");
147 while(attribute_names
[i
]){
148 if(strcmp("node_name", attribute_names
[i
])==0){
149 des
->node_name
= g_strdup(attribute_values
[i
]);
150 }else if(strcmp("domainname", attribute_names
[i
])==0){
151 des
->domain_name
= g_strdup(attribute_values
[i
]);
152 }else if(strcmp("cpu", attribute_names
[i
])==0){
153 des
->nb_cpu
= atoi(attribute_values
[i
]);
154 }else if(strcmp("arch_size", attribute_names
[i
])==0){
155 if(strcmp(attribute_values
[i
],"LP32") == 0) des
->size
= LTT_LP32
;
156 else if(strcmp(attribute_values
[i
],"ILP32") == 0) des
->size
= LTT_ILP32
;
157 else if(strcmp(attribute_values
[i
],"LP64") == 0) des
->size
= LTT_LP64
;
158 else if(strcmp(attribute_values
[i
],"ILP64") == 0) des
->size
= LTT_ILP64
;
159 else if(strcmp(attribute_values
[i
],"UNKNOWN") == 0) des
->size
= LTT_UNKNOWN
;
160 }else if(strcmp("endian", attribute_names
[i
])==0){
161 if(strcmp(attribute_values
[i
],"LITTLE_ENDIAN") == 0)
162 des
->endian
= LTT_LITTLE_ENDIAN
;
163 else if(strcmp(attribute_values
[i
],"BIG_ENDIAN") == 0)
164 des
->endian
= LTT_BIG_ENDIAN
;
165 }else if(strcmp("kernel_name", attribute_names
[i
])==0){
166 des
->kernel_name
= g_strdup(attribute_values
[i
]);
167 }else if(strcmp("kernel_release", attribute_names
[i
])==0){
168 des
->kernel_release
= g_strdup(attribute_values
[i
]);
169 }else if(strcmp("kernel_version", attribute_names
[i
])==0){
170 des
->kernel_version
= g_strdup(attribute_values
[i
]);
171 }else if(strcmp("machine", attribute_names
[i
])==0){
172 des
->machine
= g_strdup(attribute_values
[i
]);
173 }else if(strcmp("processor", attribute_names
[i
])==0){
174 des
->processor
= g_strdup(attribute_values
[i
]);
175 }else if(strcmp("hardware_platform", attribute_names
[i
])==0){
176 des
->hardware_platform
= g_strdup(attribute_values
[i
]);
177 }else if(strcmp("operating_system", attribute_names
[i
])==0){
178 des
->operating_system
= g_strdup(attribute_values
[i
]);
179 }else if(strcmp("ltt_major_version", attribute_names
[i
])==0){
180 des
->ltt_major_version
= atoi(attribute_values
[i
]);
181 }else if(strcmp("ltt_minor_version", attribute_names
[i
])==0){
182 des
->ltt_minor_version
= atoi(attribute_values
[i
]);
183 }else if(strcmp("ltt_block_size", attribute_names
[i
])==0){
184 des
->ltt_block_size
= atoi(attribute_values
[i
]);
186 *error
= g_error_new(G_MARKUP_ERROR
,
188 "Not a valid attribute");
195 static void parser_characters (GMarkupParseContext __UNUSED__
*context
,
197 gsize __UNUSED__ text_len
,
199 GError __UNUSED__
**error
)
201 LttSystemDescription
* des
= (LttSystemDescription
* )user_data
;
202 des
->description
= g_strdup(text
);
207 LttFacility
*ltt_trace_get_facility_by_num(LttTrace
*t
,
210 g_assert(num
< t
->facilities_by_num
->len
);
212 return &g_array_index(t
->facilities_by_num
, LttFacility
, num
);
217 guint
ltt_trace_get_num_cpu(LttTrace
*t
)
225 * Return value : 0 success, 1 bad tracefile
227 int parse_trace_header(void *header
, LttTracefile
*tf
, LttTrace
*t
)
229 guint32
*magic_number
= (guint32
*)header
;
230 struct ltt_trace_header_any
*any
= (struct ltt_trace_header_any
*)header
;
232 if(*magic_number
== LTT_MAGIC_NUMBER
)
234 else if(*magic_number
== LTT_REV_MAGIC_NUMBER
)
236 else /* invalid magic number, bad tracefile ! */
239 /* Get float byte order : might be different from int byte order
240 * (or is set to 0 if the trace has no float (kernel trace)) */
241 tf
->float_word_order
= any
->float_word_order
;
242 tf
->alignment
= any
->alignment
;
243 tf
->has_heartbeat
= any
->has_heartbeat
;
246 t
->arch_type
= ltt_get_uint32(LTT_GET_BO(tf
),
248 t
->arch_variant
= ltt_get_uint32(LTT_GET_BO(tf
),
250 t
->arch_size
= any
->arch_size
;
251 t
->ltt_major_version
= any
->major_version
;
252 t
->ltt_minor_version
= any
->minor_version
;
253 t
->flight_recorder
= any
->flight_recorder
;
254 // t->compact_facilities = NULL;
257 switch(any
->major_version
) {
260 g_warning("Unsupported trace version : %hhu.%hhu",
261 any
->major_version
, any
->minor_version
);
265 switch(any
->minor_version
) {
268 struct ltt_trace_header_1_0
*vheader
=
269 (struct ltt_trace_header_1_0
*)header
;
270 tf
->buffer_header_size
=
271 sizeof(struct ltt_block_start_header
)
272 + sizeof(struct ltt_trace_header_1_0
);
273 tf
->tsc_lsb_truncate
= vheader
->tsc_lsb_truncate
;
274 tf
->tscbits
= vheader
->tscbits
;
275 tf
->tsc_msb_cutoff
= 32 - tf
->tsc_lsb_truncate
- tf
->tscbits
;
276 tf
->compact_event_bits
= 32 - vheader
->compact_data_shift
;
277 tf
->tsc_mask
= ((1ULL << (tf
->tscbits
))-1);
278 tf
->tsc_mask
= tf
->tsc_mask
<< tf
->tsc_lsb_truncate
;
279 tf
->tsc_mask_next_bit
= (1ULL<<(tf
->tscbits
));
280 tf
->tsc_mask_next_bit
= tf
->tsc_mask_next_bit
<< tf
->tsc_lsb_truncate
;
282 t
->start_freq
= ltt_get_uint64(LTT_GET_BO(tf
),
283 &vheader
->start_freq
);
284 t
->freq_scale
= ltt_get_uint32(LTT_GET_BO(tf
),
285 &vheader
->freq_scale
);
286 t
->start_tsc
= ltt_get_uint64(LTT_GET_BO(tf
),
287 &vheader
->start_tsc
);
288 t
->start_monotonic
= ltt_get_uint64(LTT_GET_BO(tf
),
289 &vheader
->start_monotonic
);
290 t
->start_time
.tv_sec
= ltt_get_uint64(LTT_GET_BO(tf
),
291 &vheader
->start_time_sec
);
292 t
->start_time
.tv_nsec
= ltt_get_uint64(LTT_GET_BO(tf
),
293 &vheader
->start_time_usec
);
294 t
->start_time
.tv_nsec
*= 1000; /* microsec to nanosec */
296 t
->start_time_from_tsc
= ltt_time_from_uint64(
298 * (1000000000.0 / tf
->trace
->freq_scale
)
299 / (double)t
->start_freq
);
304 g_warning("Unsupported trace version : %hhu.%hhu",
305 any
->major_version
, any
->minor_version
);
310 g_warning("Unsupported trace version : %hhu.%hhu",
311 any
->major_version
, any
->minor_version
);
321 /*****************************************************************************
323 * ltt_tracefile_open : open a trace file, construct a LttTracefile
325 * t : the trace containing the tracefile
326 * fileName : path name of the trace file
327 * tf : the tracefile structure
329 * : 0 for success, -1 otherwise.
330 ****************************************************************************/
332 gint
ltt_tracefile_open(LttTrace
*t
, gchar
* fileName
, LttTracefile
*tf
)
334 struct stat lTDFStat
; /* Trace data file status */
335 struct ltt_block_start_header
*header
;
336 int page_size
= getpagesize();
339 tf
->long_name
= g_quark_from_string(fileName
);
341 tf
->fd
= open(fileName
, O_RDONLY
);
343 g_warning("Unable to open input data file %s\n", fileName
);
347 // Get the file's status
348 if(fstat(tf
->fd
, &lTDFStat
) < 0){
349 g_warning("Unable to get the status of the input data file %s\n", fileName
);
353 // Is the file large enough to contain a trace
354 if(lTDFStat
.st_size
<
355 (off_t
)(sizeof(struct ltt_block_start_header
)
356 + sizeof(struct ltt_trace_header_any
))){
357 g_print("The input data file %s does not contain a trace\n", fileName
);
361 /* Temporarily map the buffer start header to get trace information */
362 /* Multiple of pages aligned head */
363 tf
->buffer
.head
= mmap(0,
364 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)
365 + sizeof(struct ltt_trace_header_any
)), PROT_READ
,
366 MAP_PRIVATE
, tf
->fd
, 0);
367 if(tf
->buffer
.head
== MAP_FAILED
) {
368 perror("Error in allocating memory for buffer of tracefile");
371 g_assert( ( (gulong
)tf
->buffer
.head
&(8-1) ) == 0); // make sure it's aligned.
373 header
= (struct ltt_block_start_header
*)tf
->buffer
.head
;
375 if(parse_trace_header(header
->trace
, tf
, NULL
)) {
376 g_warning("parse_trace_header error");
380 //store the size of the file
381 tf
->file_size
= lTDFStat
.st_size
;
382 tf
->buf_size
= ltt_get_uint32(LTT_GET_BO(tf
), &header
->buf_size
);
383 tf
->num_blocks
= tf
->file_size
/ tf
->buf_size
;
385 if(munmap(tf
->buffer
.head
,
386 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)
387 + sizeof(struct ltt_trace_header_any
)))) {
388 g_warning("unmap size : %u\n",
389 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)
390 + sizeof(struct ltt_trace_header_any
)));
391 perror("munmap error");
394 tf
->buffer
.head
= NULL
;
396 //read the first block
397 if(map_block(tf
,0)) {
398 perror("Cannot map block for tracefile");
406 if(munmap(tf
->buffer
.head
,
407 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)
408 + sizeof(struct ltt_trace_header_any
)))) {
409 g_warning("unmap size : %u\n",
410 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)
411 + sizeof(struct ltt_trace_header_any
)));
412 perror("munmap error");
421 LttTrace
*ltt_tracefile_get_trace(LttTracefile
*tf
)
427 /*****************************************************************************
428 *Open control and per cpu tracefiles
429 ****************************************************************************/
431 void ltt_tracefile_open_cpu(LttTrace
*t
, gchar
* tracefile_name
)
434 tf
= ltt_tracefile_open(t
,tracefile_name
);
436 t
->per_cpu_tracefile_number
++;
437 g_ptr_array_add(t
->per_cpu_tracefiles
, tf
);
440 gint
ltt_tracefile_open_control(LttTrace
*t
, gchar
* control_name
)
449 tf
= ltt_tracefile_open(t
,control_name
);
451 g_warning("ltt_tracefile_open_control : bad file descriptor");
454 t
->control_tracefile_number
++;
455 g_ptr_array_add(t
->control_tracefiles
,tf
);
457 //parse facilities tracefile to get base_id
458 if(strcmp(&control_name
[strlen(control_name
)-10],"facilities") ==0){
460 if(!ltt_tracefile_read(tf
,&ev
)) return 0; // end of file
462 if(ev
.event_id
== TRACE_FACILITY_LOAD
){
464 fLoad
.name
= (gchar
*)pos
;
465 fLoad
.checksum
= *(LttChecksum
*)(pos
+ strlen(fLoad
.name
));
466 fLoad
.base_code
= *(guint32
*)(pos
+ strlen(fLoad
.name
) + sizeof(LttChecksum
));
468 for(i
=0;i
<t
->facility_number
;i
++){
469 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
,i
);
470 if(strcmp(f
->name
,fLoad
.name
)==0 && fLoad
.checksum
==f
->checksum
){
471 f
->base_id
= fLoad
.base_code
;
475 if(i
==t
->facility_number
) {
476 g_warning("Facility: %s, checksum: %u is not found",
477 fLoad
.name
,(unsigned int)fLoad
.checksum
);
480 }else if(ev
.event_id
== TRACE_BLOCK_START
){
482 }else if(ev
.event_id
== TRACE_BLOCK_END
){
485 g_warning("Not valid facilities trace file");
494 /*****************************************************************************
496 * ltt_tracefile_close: close a trace file,
498 * t : tracefile which will be closed
499 ****************************************************************************/
501 void ltt_tracefile_close(LttTracefile
*t
)
503 int page_size
= getpagesize();
505 if(t
->buffer
.head
!= NULL
)
506 if(munmap(t
->buffer
.head
, PAGE_ALIGN(t
->buf_size
))) {
507 g_warning("unmap size : %u\n",
508 PAGE_ALIGN(t
->buf_size
));
509 perror("munmap error");
517 /*****************************************************************************
518 *Get system information
519 ****************************************************************************/
521 gint
getSystemInfo(LttSystemDescription
* des
, gchar
* pathname
)
528 GMarkupParseContext
* context
;
529 GError
* error
= NULL
;
530 GMarkupParser markup_parser
=
532 parser_start_element
,
535 NULL
, /* passthrough */
539 fd
= g_open(pathname
, O_RDONLY
, 0);
541 g_warning("Can not open file : %s\n", pathname
);
545 iochan
= g_io_channel_unix_new(fd
);
547 context
= g_markup_parse_context_new(&markup_parser
, 0, des
,NULL
);
549 //while(fgets(buf,DIR_NAME_SIZE, fp) != NULL){
550 while(g_io_channel_read_line(iochan
, &buf
, &length
, NULL
, &error
)
551 != G_IO_STATUS_EOF
) {
554 g_warning("Can not read xml file: \n%s\n", error
->message
);
557 if(!g_markup_parse_context_parse(context
, buf
, length
, &error
)){
559 g_warning("Can not parse xml file: \n%s\n", error
->message
);
562 g_markup_parse_context_free(context
);
564 g_io_channel_shutdown(iochan
, FALSE
, &error
); /* No flush */
566 g_warning("Can not close file: \n%s\n", error
->message
);
574 g_markup_parse_context_free(context
);
576 g_io_channel_shutdown(iochan
, FALSE
, &error
); /* No flush */
578 g_warning("Can not close file: \n%s\n", error
->message
);
589 /*****************************************************************************
590 *The following functions get facility/tracefile information
591 ****************************************************************************/
593 gint
getFacilityInfo(LttTrace
*t
, gchar
* eventdefs
)
600 gchar fullname
[DIR_NAME_SIZE
];
601 GError
* error
= NULL
;
603 dir
= g_dir_open(eventdefs
, 0, &error
);
606 g_warning("Can not open directory: %s, %s\n", eventdefs
, error
->message
);
611 while((name
= g_dir_read_name(dir
)) != NULL
){
612 if(!g_pattern_match_simple("*.xml", name
)) continue;
613 strcpy(fullname
,eventdefs
);
614 strcat(fullname
,name
);
615 ltt_facility_open(t
,fullname
);
619 for(j
=0;j
<t
->facility_number
;j
++){
620 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, j
);
621 for(i
=0; i
<f
->event_number
; i
++){
623 setFieldsOffset(NULL
, et
, NULL
, t
);
630 /*****************************************************************************
631 *A trace is specified as a pathname to the directory containing all the
632 *associated data (control tracefiles, per cpu tracefiles, event
635 *When a trace is closed, all the associated facilities, types and fields
636 *are released as well.
640 /****************************************************************************
641 * get_absolute_pathname
643 * return the unique pathname in the system
645 * MD : Fixed this function so it uses realpath, dealing well with
646 * forgotten cases (.. were not used correctly before).
648 ****************************************************************************/
649 void get_absolute_pathname(const gchar
*pathname
, gchar
* abs_pathname
)
651 abs_pathname
[0] = '\0';
653 if ( realpath (pathname
, abs_pathname
) != NULL
)
657 /* error, return the original path unmodified */
658 strcpy(abs_pathname
, pathname
);
664 /* Search for something like : .*_.*
666 * The left side is the name, the right side is the number.
669 int get_tracefile_name_number(gchar
*raw_name
,
676 guint raw_name_len
= strlen(raw_name
);
677 gchar char_name
[PATH_MAX
];
684 for(i
=raw_name_len
-1;i
>=0;i
--) {
685 if(raw_name
[i
] == '_') break;
687 if(i
==-1) { /* Either not found or name length is 0 */
688 /* This is a userspace tracefile */
689 strncpy(char_name
, raw_name
, raw_name_len
);
690 char_name
[raw_name_len
] = '\0';
691 *name
= g_quark_from_string(char_name
);
692 *num
= 0; /* unknown cpu */
693 for(i
=0;i
<raw_name_len
;i
++) {
694 if(raw_name
[i
] == '/') {
699 for(;i
<raw_name_len
;i
++) {
700 if(raw_name
[i
] == '/') {
705 for(;i
<raw_name_len
;i
++) {
706 if(raw_name
[i
] == '-') {
710 if(i
== raw_name_len
) return -1;
712 tmpptr
= &raw_name
[i
];
713 for(;i
<raw_name_len
;i
++) {
714 if(raw_name
[i
] == '.') {
719 *tid
= strtoul(tmpptr
, &endptr
, 10);
721 return -1; /* No digit */
722 if(*tid
== ULONG_MAX
)
723 return -1; /* underflow / overflow */
725 tmpptr
= &raw_name
[i
];
726 for(;i
<raw_name_len
;i
++) {
727 if(raw_name
[i
] == '.') {
732 *pgid
= strtoul(tmpptr
, &endptr
, 10);
734 return -1; /* No digit */
735 if(*pgid
== ULONG_MAX
)
736 return -1; /* underflow / overflow */
738 tmpptr
= &raw_name
[i
];
739 *creation
= strtoull(tmpptr
, &endptr
, 10);
741 return -1; /* No digit */
742 if(*creation
== G_MAXUINT64
)
743 return -1; /* underflow / overflow */
747 cpu_num
= strtol(raw_name
+underscore_pos
+1, &endptr
, 10);
749 if(endptr
== raw_name
+underscore_pos
+1)
750 return -1; /* No digit */
751 if(cpu_num
== LONG_MIN
|| cpu_num
== LONG_MAX
)
752 return -1; /* underflow / overflow */
754 strncpy(char_name
, raw_name
, underscore_pos
);
755 char_name
[underscore_pos
] = '\0';
757 *name
= g_quark_from_string(char_name
);
766 GData
**ltt_trace_get_tracefiles_groups(LttTrace
*trace
)
768 return &trace
->tracefiles
;
772 void compute_tracefile_group(GQuark key_id
,
774 struct compute_tracefile_group_args
*args
)
779 for(i
=0; i
<group
->len
; i
++) {
780 tf
= &g_array_index (group
, LttTracefile
, i
);
782 args
->func(tf
, args
->func_args
);
787 void ltt_tracefile_group_destroy(gpointer data
)
789 GArray
*group
= (GArray
*)data
;
793 for(i
=0; i
<group
->len
; i
++) {
794 tf
= &g_array_index (group
, LttTracefile
, i
);
796 ltt_tracefile_close(tf
);
798 g_array_free(group
, TRUE
);
801 gboolean
ltt_tracefile_group_has_cpu_online(gpointer data
)
803 GArray
*group
= (GArray
*)data
;
807 for(i
=0; i
<group
->len
; i
++) {
808 tf
= &g_array_index (group
, LttTracefile
, i
);
816 /* Open each tracefile under a specific directory. Put them in a
817 * GData : permits to access them using their tracefile group pathname.
818 * i.e. access control/modules tracefile group by index :
821 * relative path is the path relative to the trace root
822 * root path is the full path
824 * A tracefile group is simply an array where all the per cpu tracefiles sit.
827 int open_tracefiles(LttTrace
*trace
, gchar
*root_path
, gchar
*relative_path
)
829 DIR *dir
= opendir(root_path
);
830 struct dirent
*entry
;
831 struct stat stat_buf
;
834 gchar path
[PATH_MAX
];
839 gchar rel_path
[PATH_MAX
];
848 strncpy(path
, root_path
, PATH_MAX
-1);
849 path_len
= strlen(path
);
850 path
[path_len
] = '/';
852 path_ptr
= path
+ path_len
;
854 strncpy(rel_path
, relative_path
, PATH_MAX
-1);
855 rel_path_len
= strlen(rel_path
);
856 rel_path
[rel_path_len
] = '/';
858 rel_path_ptr
= rel_path
+ rel_path_len
;
860 while((entry
= readdir(dir
)) != NULL
) {
862 if(entry
->d_name
[0] == '.') continue;
864 strncpy(path_ptr
, entry
->d_name
, PATH_MAX
- path_len
);
865 strncpy(rel_path_ptr
, entry
->d_name
, PATH_MAX
- rel_path_len
);
867 ret
= stat(path
, &stat_buf
);
873 g_debug("Tracefile file or directory : %s\n", path
);
875 // if(strcmp(rel_path, "/eventdefs") == 0) continue;
877 if(S_ISDIR(stat_buf
.st_mode
)) {
879 g_debug("Entering subdirectory...\n");
880 ret
= open_tracefiles(trace
, path
, rel_path
);
881 if(ret
< 0) continue;
882 } else if(S_ISREG(stat_buf
.st_mode
)) {
891 if(get_tracefile_name_number(rel_path
, &name
, &num
, &tid
, &pgid
, &creation
))
892 continue; /* invalid name */
894 g_debug("Opening file.\n");
895 if(ltt_tracefile_open(trace
, path
, &tmp_tf
)) {
896 g_info("Error opening tracefile %s", path
);
898 continue; /* error opening the tracefile : bad magic number ? */
901 g_debug("Tracefile name is %s and number is %u",
902 g_quark_to_string(name
), num
);
904 tmp_tf
.cpu_online
= 1;
905 tmp_tf
.cpu_num
= num
;
909 tmp_tf
.creation
= creation
;
910 if(tmp_tf
.name
== g_quark_from_string("/compact")
911 || tmp_tf
.name
== g_quark_from_string("/flight-compact"))
915 group
= g_datalist_id_get_data(&trace
->tracefiles
, name
);
917 /* Elements are automatically cleared when the array is allocated.
918 * It makes the cpu_online variable set to 0 : cpu offline, by default.
920 group
= g_array_sized_new (FALSE
, TRUE
, sizeof(LttTracefile
), 10);
921 g_datalist_id_set_data_full(&trace
->tracefiles
, name
,
922 group
, ltt_tracefile_group_destroy
);
925 /* Add the per cpu tracefile to the named group */
926 unsigned int old_len
= group
->len
;
928 group
= g_array_set_size(group
, num
+1);
929 g_array_index (group
, LttTracefile
, num
) = tmp_tf
;
930 g_array_index (group
, LttTracefile
, num
).event
.tracefile
=
931 &g_array_index (group
, LttTracefile
, num
);
940 /* ltt_get_facility_description
942 * Opens the file corresponding to the requested facility (identified by fac_id
945 * The name searched is : %trace root%/eventdefs/facname_checksum.xml
947 * Returns 0 on success, or 1 on failure.
950 static int ltt_get_facility_description(LttFacility
*f
,
952 LttTracefile
*fac_tf
)
954 char desc_file_name
[PATH_MAX
];
961 text
= g_quark_to_string(t
->pathname
);
962 textlen
= strlen(text
);
964 if(textlen
>= PATH_MAX
) goto name_error
;
965 strcpy(desc_file_name
, text
);
967 text
= "/eventdefs/";
968 textlen
+=strlen(text
);
969 if(textlen
>= PATH_MAX
) goto name_error
;
970 strcat(desc_file_name
, text
);
972 text
= g_quark_to_string(f
->name
);
973 fac_name_len
= strlen(text
);
974 textlen
+=fac_name_len
;
975 if(textlen
>= PATH_MAX
) goto name_error
;
976 strcat(desc_file_name
, text
);
978 /* arch specific facilities are named like this : name_arch */
979 if(fac_name_len
+1 < sizeof("_arch"))
982 if(!strcmp(&text
[fac_name_len
+1-sizeof("_arch")], "_arch"))
990 textlen
+=strlen(text
);
991 if(textlen
>= PATH_MAX
) goto name_error
;
992 strcat(desc_file_name
, text
);
994 err
= snprintf(desc_file_name
+textlen
, PATH_MAX
-textlen
-1,
996 if(err
< 0) goto name_error
;
998 textlen
=strlen(desc_file_name
);
1003 switch(t
->arch_type
) {
1004 case LTT_ARCH_TYPE_I386
:
1007 case LTT_ARCH_TYPE_PPC
:
1010 case LTT_ARCH_TYPE_SH
:
1013 case LTT_ARCH_TYPE_S390
:
1016 case LTT_ARCH_TYPE_MIPS
:
1019 case LTT_ARCH_TYPE_ARM
:
1022 case LTT_ARCH_TYPE_PPC64
:
1025 case LTT_ARCH_TYPE_X86_64
:
1028 case LTT_ARCH_TYPE_C2
:
1031 case LTT_ARCH_TYPE_POWERPC
:
1035 g_error("Trace from unsupported architecture.");
1037 textlen
+=strlen(text
);
1038 if(textlen
>= PATH_MAX
) goto name_error
;
1039 strcat(desc_file_name
, text
);
1043 textlen
+=strlen(text
);
1044 if(textlen
>= PATH_MAX
) goto name_error
;
1045 strcat(desc_file_name
, text
);
1047 err
= ltt_facility_open(f
, t
, desc_file_name
);
1048 if(err
) goto facility_error
;
1057 static void ltt_fac_ids_destroy(gpointer data
)
1059 GArray
*fac_ids
= (GArray
*)data
;
1061 g_array_free(fac_ids
, TRUE
);
1065 /* Presumes the tracefile is already seeked at the beginning. It makes sense,
1066 * because it must be done just after the opening */
1067 int ltt_process_facility_tracefile(LttTracefile
*tf
)
1076 err
= ltt_tracefile_read_seek(tf
);
1077 if(err
== EPERM
) goto seek_error
;
1078 else if(err
== ERANGE
) break; /* End of tracefile */
1080 err
= ltt_tracefile_read_update_event(tf
);
1081 if(err
) goto update_error
;
1083 /* We are on a facility load/or facility unload/ or heartbeat event */
1085 * * facility 0 is hardcoded : this is the core facility. It will be shown
1086 * in the facility array though, and is shown as "loaded builtin" in the
1088 * It contains event :
1090 * 1 : facility unload
1091 * 2 : state dump facility load
1094 if(tf
->event
.event_id
>= MARKER_CORE_IDS
) {
1095 /* Should only contain core facility */
1096 g_warning("Error in processing facility file %s, "
1097 "should not contain event id %u.", g_quark_to_string(tf
->name
),
1098 tf
->event
.event_id
);
1100 goto event_id_error
;
1104 const char *marker_name
, *format
;
1106 guint8 int_size
, long_size
, pointer_size
, size_t_size
, alignment
;
1109 switch((enum marker_id
)tf
->event
.event_id
) {
1110 case MARKER_ID_SET_MARKER_ID
:
1111 marker_name
= pos
= tf
->event
.data
;
1112 g_debug("Doing MARKER_ID_SET_MARKER_ID of marker %s", marker_name
);
1113 pos
+= strlen(marker_name
) + 1;
1114 //remove genevent compatibility
1115 //pos += ltt_align((size_t)pos, tf->trace->arch_size, tf->alignment);
1116 pos
+= ltt_align((size_t)pos
, sizeof(uint16_t), tf
->alignment
);
1117 id
= ltt_get_uint16(LTT_GET_BO(tf
), pos
);
1118 g_debug("In MARKER_ID_SET_MARKER_ID of marker %s id %hu",
1120 pos
+= sizeof(guint16
);
1121 int_size
= *(guint8
*)pos
;
1122 pos
+= sizeof(guint8
);
1123 long_size
= *(guint8
*)pos
;
1124 pos
+= sizeof(guint8
);
1125 pointer_size
= *(guint8
*)pos
;
1126 pos
+= sizeof(guint8
);
1127 size_t_size
= *(guint8
*)pos
;
1128 pos
+= sizeof(guint8
);
1129 alignment
= *(guint8
*)pos
;
1130 pos
+= sizeof(guint8
);
1131 marker_id_event(tf
->trace
, g_quark_from_string(marker_name
),
1132 id
, int_size
, long_size
,
1133 pointer_size
, size_t_size
, alignment
);
1135 case MARKER_ID_SET_MARKER_FORMAT
:
1136 marker_name
= pos
= tf
->event
.data
;
1137 g_debug("Doing MARKER_ID_SET_MARKER_FORMAT of marker %s",
1139 pos
+= strlen(marker_name
) + 1;
1141 //pos += ltt_align((size_t)pos, tf->trace->arch_size, tf->alignment);
1143 pos
+= strlen(format
) + 1;
1145 //pos += ltt_align((size_t)pos, tf->trace->arch_size, tf->alignment);
1146 marker_format_event(tf
->trace
, g_quark_from_string(marker_name
),
1148 /* get information from dictionnary TODO */
1150 case MARKER_ID_HEARTBEAT_32
:
1151 case MARKER_ID_HEARTBEAT_64
:
1154 g_warning("Error in processing facility file %s, "
1155 "unknown event id %hhu.",
1156 g_quark_to_string(tf
->name
),
1157 tf
->event
.event_id
);
1159 goto event_id_error
;
1165 /* Error handling */
1169 g_warning("An error occured in facility tracefile parsing");
1174 LttTrace
*ltt_trace_open(const gchar
*pathname
)
1176 gchar abs_path
[PATH_MAX
];
1181 struct ltt_block_start_header
*header
;
1183 struct dirent
*entry
;
1184 guint control_found
= 0;
1185 struct stat stat_buf
;
1186 gchar path
[PATH_MAX
];
1188 t
= g_new(LttTrace
, 1);
1189 if(!t
) goto alloc_error
;
1191 get_absolute_pathname(pathname
, abs_path
);
1192 t
->pathname
= g_quark_from_string(abs_path
);
1194 g_datalist_init(&t
->tracefiles
);
1196 /* Test to see if it looks like a trace */
1197 dir
= opendir(abs_path
);
1202 while((entry
= readdir(dir
)) != NULL
) {
1203 strcpy(path
, abs_path
);
1205 strcat(path
, entry
->d_name
);
1206 ret
= stat(path
, &stat_buf
);
1211 if(S_ISDIR(stat_buf
.st_mode
)) {
1212 if(strcmp(entry
->d_name
, "control") == 0) {
1219 if(!control_found
) goto find_error
;
1221 /* Open all the tracefiles */
1222 if(open_tracefiles(t
, abs_path
, "")) {
1223 g_warning("Error opening tracefile %s", abs_path
);
1227 /* Parse each trace control/facilitiesN files : get runtime fac. info */
1228 group
= g_datalist_id_get_data(&t
->tracefiles
, LTT_TRACEFILE_NAME_FACILITIES
);
1230 g_error("Trace %s has no facility tracefile", abs_path
);
1232 goto facilities_error
;
1235 /* Get the trace information for the control/facility 0 tracefile */
1236 g_assert(group
->len
> 0);
1237 tf
= &g_array_index (group
, LttTracefile
, 0);
1238 header
= (struct ltt_block_start_header
*)tf
->buffer
.head
;
1239 g_assert(parse_trace_header(header
->trace
,
1242 t
->num_cpu
= group
->len
;
1244 ret
= allocate_marker_data(t
);
1246 g_error("Error in allocating marker data");
1248 for(i
=0; i
<group
->len
; i
++) {
1249 tf
= &g_array_index (group
, LttTracefile
, i
);
1250 if(ltt_process_facility_tracefile(tf
))
1251 goto facilities_error
;
1256 /* Error handling */
1258 destroy_marker_data(t
);
1260 g_datalist_clear(&t
->tracefiles
);
1268 GQuark
ltt_trace_name(const LttTrace
*t
)
1274 /******************************************************************************
1275 * When we copy a trace, we want all the opening actions to happen again :
1276 * the trace will be reopened and totally independant from the original.
1277 * That's why we call ltt_trace_open.
1278 *****************************************************************************/
1279 LttTrace
*ltt_trace_copy(LttTrace
*self
)
1281 return ltt_trace_open(g_quark_to_string(self
->pathname
));
1284 void ltt_trace_close(LttTrace
*t
)
1286 g_datalist_clear(&t
->tracefiles
);
1291 /*****************************************************************************
1292 *Get the system description of the trace
1293 ****************************************************************************/
1295 LttFacility
*ltt_trace_facility_by_id(LttTrace
*t
, guint8 id
)
1297 g_assert(id
< t
->facilities_by_num
->len
);
1298 return &g_array_index(t
->facilities_by_num
, LttFacility
, id
);
1301 /* ltt_trace_facility_get_by_name
1303 * Returns the GArray of facility indexes. All the fac_ids that matches the
1304 * requested facility name.
1306 * If name is not found, returns NULL.
1308 GArray
*ltt_trace_facility_get_by_name(LttTrace
*t
, GQuark name
)
1310 return g_datalist_id_get_data(&t
->facilities_by_name
, name
);
1314 /*****************************************************************************
1315 * Functions to discover all the event types in the trace
1316 ****************************************************************************/
1319 unsigned ltt_trace_eventtype_number(LttTrace
*t
)
1323 unsigned int num
= t
->facility_number
;
1327 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, i
);
1328 count
+= f
->event_number
;
1335 //use an iteration on all the trace facilities, and inside iteration on all the
1336 //event types in each facilities instead.
1337 LttEventType
*ltt_trace_eventtype_get(LttTrace
*t
, unsigned evId
)
1339 LttEventType
*event_type
;
1342 f
= ltt_trace_facility_by_id(t
,evId
);
1344 if(unlikely(!f
)) event_type
= NULL
;
1345 else event_type
= f
->events
[evId
- f
->base_id
];
1352 /*****************************************************************************
1353 * ltt_trace_find_tracefile
1355 * Find a tracefile by name and index in the group.
1357 * Returns a pointer to the tracefiles, else NULL.
1358 ****************************************************************************/
1360 LttTracefile
*ltt_trace_find_tracefile(LttTrace
*t
, const gchar
*name
)
1365 /*****************************************************************************
1366 * Get the start time and end time of the trace
1367 ****************************************************************************/
1369 void ltt_tracefile_time_span_get(LttTracefile
*tf
,
1370 LttTime
*start
, LttTime
*end
)
1374 err
= map_block(tf
, 0);
1376 g_error("Can not map block");
1377 *start
= ltt_time_infinite
;
1379 *start
= tf
->buffer
.begin
.timestamp
;
1381 err
= map_block(tf
, tf
->num_blocks
- 1); /* Last block */
1383 g_error("Can not map block");
1384 *end
= ltt_time_zero
;
1386 *end
= tf
->buffer
.end
.timestamp
;
1389 struct tracefile_time_span_get_args
{
1395 void group_time_span_get(GQuark name
, gpointer data
, gpointer user_data
)
1397 struct tracefile_time_span_get_args
*args
=
1398 (struct tracefile_time_span_get_args
*)user_data
;
1400 GArray
*group
= (GArray
*)data
;
1406 for(i
=0; i
<group
->len
; i
++) {
1407 tf
= &g_array_index (group
, LttTracefile
, i
);
1408 if(tf
->cpu_online
) {
1409 ltt_tracefile_time_span_get(tf
, &tmp_start
, &tmp_end
);
1410 if(ltt_time_compare(*args
->start
, tmp_start
)>0) *args
->start
= tmp_start
;
1411 if(ltt_time_compare(*args
->end
, tmp_end
)<0) *args
->end
= tmp_end
;
1416 void ltt_trace_time_span_get(LttTrace
*t
, LttTime
*start
, LttTime
*end
)
1418 LttTime min_start
= ltt_time_infinite
;
1419 LttTime max_end
= ltt_time_zero
;
1420 struct tracefile_time_span_get_args args
= { t
, &min_start
, &max_end
};
1422 g_datalist_foreach(&t
->tracefiles
, &group_time_span_get
, &args
);
1424 if(start
!= NULL
) *start
= min_start
;
1425 if(end
!= NULL
) *end
= max_end
;
1430 /*****************************************************************************
1431 *Get the name of a tracefile
1432 ****************************************************************************/
1434 GQuark
ltt_tracefile_name(const LttTracefile
*tf
)
1439 GQuark
ltt_tracefile_long_name(const LttTracefile
*tf
)
1441 return tf
->long_name
;
1446 guint
ltt_tracefile_cpu(LttTracefile
*tf
)
1451 guint
ltt_tracefile_tid(LttTracefile
*tf
)
1456 guint
ltt_tracefile_pgid(LttTracefile
*tf
)
1461 guint64
ltt_tracefile_creation(LttTracefile
*tf
)
1463 return tf
->creation
;
1465 /*****************************************************************************
1466 * Get the number of blocks in the tracefile
1467 ****************************************************************************/
1469 guint
ltt_tracefile_block_number(LttTracefile
*tf
)
1471 return tf
->num_blocks
;
1475 /* Seek to the first event in a tracefile that has a time equal or greater than
1476 * the time passed in parameter.
1478 * If the time parameter is outside the tracefile time span, seek to the first
1479 * event or if after, return ERANGE.
1481 * If the time parameter is before the first event, we have to seek specially to
1484 * If the time is after the end of the trace, return ERANGE.
1486 * Do a binary search to find the right block, then a sequential search in the
1487 * block to find the event.
1489 * In the special case where the time requested fits inside a block that has no
1490 * event corresponding to the requested time, the first event of the next block
1493 * IMPORTANT NOTE : // FIXME everywhere...
1495 * You MUST NOT do a ltt_tracefile_read right after a ltt_tracefile_seek_time :
1496 * you will jump over an event if you do.
1498 * Return value : 0 : no error, the tf->event can be used
1499 * ERANGE : time if after the last event of the trace
1500 * otherwise : this is an error.
1504 int ltt_tracefile_seek_time(LttTracefile
*tf
, LttTime time
)
1508 unsigned int block_num
, high
, low
;
1510 /* seek at the beginning of trace */
1511 err
= map_block(tf
, 0); /* First block */
1513 g_error("Can not map block");
1517 /* If the time is lower or equal the beginning of the trace,
1518 * go to the first event. */
1519 if(ltt_time_compare(time
, tf
->buffer
.begin
.timestamp
) <= 0) {
1520 ret
= ltt_tracefile_read(tf
);
1521 if(ret
== ERANGE
) goto range
;
1522 else if (ret
) goto fail
;
1523 goto found
; /* There is either no event in the trace or the event points
1524 to the first event in the trace */
1527 err
= map_block(tf
, tf
->num_blocks
- 1); /* Last block */
1529 g_error("Can not map block");
1533 /* If the time is after the end of the trace, return ERANGE. */
1534 if(ltt_time_compare(time
, tf
->buffer
.end
.timestamp
) > 0) {
1538 /* Binary search the block */
1539 high
= tf
->num_blocks
- 1;
1543 block_num
= ((high
-low
) / 2) + low
;
1545 err
= map_block(tf
, block_num
);
1547 g_error("Can not map block");
1551 /* We cannot divide anymore : this is what would happen if the time
1552 * requested was exactly between two consecutive buffers'end and start
1553 * timestamps. This is also what would happend if we didn't deal with out
1554 * of span cases prior in this function. */
1555 /* The event is right in the buffer!
1556 * (or in the next buffer first event) */
1558 ret
= ltt_tracefile_read(tf
);
1559 if(ret
== ERANGE
) goto range
; /* ERANGE or EPERM */
1560 else if(ret
) goto fail
;
1562 if(ltt_time_compare(time
, tf
->event
.event_time
) <= 0)
1566 } else if(ltt_time_compare(time
, tf
->buffer
.begin
.timestamp
) < 0) {
1567 /* go to lower part */
1568 high
= block_num
- 1;
1569 } else if(ltt_time_compare(time
, tf
->buffer
.end
.timestamp
) > 0) {
1570 /* go to higher part */
1571 low
= block_num
+ 1;
1572 } else {/* The event is right in the buffer!
1573 (or in the next buffer first event) */
1575 ret
= ltt_tracefile_read(tf
);
1576 if(ret
== ERANGE
) goto range
; /* ERANGE or EPERM */
1577 else if(ret
) goto fail
;
1579 if(ltt_time_compare(time
, tf
->event
.event_time
) <= 0)
1591 /* Error handling */
1593 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1594 g_quark_to_string(tf
->name
));
1599 int ltt_tracefile_seek_position(LttTracefile
*tf
, const LttEventPosition
*ep
) {
1603 if(ep
->tracefile
!= tf
) {
1607 err
= map_block(tf
, ep
->block
);
1609 g_error("Can not map block");
1613 tf
->event
.offset
= ep
->offset
;
1615 /* Put back the event real tsc */
1616 tf
->event
.tsc
= ep
->tsc
;
1617 tf
->buffer
.tsc
= ep
->tsc
;
1619 err
= ltt_tracefile_read_update_event(tf
);
1621 err
= ltt_tracefile_read_op(tf
);
1627 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1628 g_quark_to_string(tf
->name
));
1632 LttTime
ltt_interpolate_time_from_tsc(LttTracefile
*tf
, guint64 tsc
)
1636 if(tsc
> tf
->trace
->start_tsc
) {
1637 time
= ltt_time_from_uint64(
1638 (double)(tsc
- tf
->trace
->start_tsc
)
1639 * (1000000000.0 / tf
->trace
->freq_scale
)
1640 / (double)tf
->trace
->start_freq
);
1641 time
= ltt_time_add(tf
->trace
->start_time_from_tsc
, time
);
1643 time
= ltt_time_from_uint64(
1644 (double)(tf
->trace
->start_tsc
- tsc
)
1645 * (1000000000.0 / tf
->trace
->freq_scale
)
1646 / (double)tf
->trace
->start_freq
);
1647 time
= ltt_time_sub(tf
->trace
->start_time_from_tsc
, time
);
1652 /* Calculate the real event time based on the buffer boundaries */
1653 LttTime
ltt_interpolate_time(LttTracefile
*tf
, LttEvent
*event
)
1655 return ltt_interpolate_time_from_tsc(tf
, tf
->buffer
.tsc
);
1659 /* Get the current event of the tracefile : valid until the next read */
1660 LttEvent
*ltt_tracefile_get_event(LttTracefile
*tf
)
1667 /*****************************************************************************
1669 * ltt_tracefile_read : Read the next event in the tracefile
1674 * Returns 0 if an event can be used in tf->event.
1675 * Returns ERANGE on end of trace. The event in tf->event still can be used
1676 * (if the last block was not empty).
1677 * Returns EPERM on error.
1679 * This function does make the tracefile event structure point to the event
1680 * currently pointed to by the tf->event.
1682 * Note : you must call a ltt_tracefile_seek to the beginning of the trace to
1683 * reinitialize it after an error if you want results to be coherent.
1684 * It would be the case if a end of trace last buffer has no event : the end
1685 * of trace wouldn't be returned, but an error.
1686 * We make the assumption there is at least one event per buffer.
1687 ****************************************************************************/
1689 int ltt_tracefile_read(LttTracefile
*tf
)
1693 err
= ltt_tracefile_read_seek(tf
);
1695 err
= ltt_tracefile_read_update_event(tf
);
1697 err
= ltt_tracefile_read_op(tf
);
1703 int ltt_tracefile_read_seek(LttTracefile
*tf
)
1707 /* Get next buffer until we finally have an event, or end of trace */
1709 err
= ltt_seek_next_event(tf
);
1710 if(unlikely(err
== ENOPROTOOPT
)) {
1714 /* Are we at the end of the buffer ? */
1716 if(unlikely(tf
->buffer
.index
== tf
->num_blocks
-1)){ /* end of trace ? */
1719 /* get next block */
1720 err
= map_block(tf
, tf
->buffer
.index
+ 1);
1722 g_error("Can not map block");
1726 } else break; /* We found an event ! */
1733 /* do specific operation on events */
1734 int ltt_tracefile_read_op(LttTracefile
*tf
)
1740 /* do event specific operation */
1742 /* do something if its an heartbeat event : increment the heartbeat count */
1743 //if(event->facility_id == LTT_FACILITY_CORE)
1744 // if(event->event_id == LTT_EVENT_HEARTBEAT)
1745 // tf->cur_heart_beat_number++;
1750 static void print_debug_event_header(LttEvent
*ev
, void *start_pos
, void *end_pos
)
1752 unsigned int offset
= 0;
1755 g_printf("Event header (tracefile %s offset %llx):\n",
1756 g_quark_to_string(ev
->tracefile
->long_name
),
1757 ((uint64_t)ev
->tracefile
->buffer
.index
* ev
->tracefile
->buf_size
)
1758 + (long)start_pos
- (long)ev
->tracefile
->buffer
.head
);
1760 while (offset
< (long)end_pos
- (long)start_pos
) {
1761 g_printf("%8lx", (long)start_pos
- (long)ev
->tracefile
->buffer
.head
+ offset
);
1764 for (i
= 0; i
< 4 ; i
++) {
1765 for (j
= 0; j
< 4; j
++) {
1766 if (offset
+ ((i
* 4) + j
) <
1767 (long)end_pos
- (long)start_pos
)
1769 ((char*)start_pos
)[offset
+ ((i
* 4) + j
)]);
1783 /* same as ltt_tracefile_read, but does not seek to the next event nor call
1784 * event specific operation. */
1785 int ltt_tracefile_read_update_event(LttTracefile
*tf
)
1792 pos
= tf
->buffer
.head
+ event
->offset
;
1794 /* Read event header */
1796 /* Align the head */
1798 pos
+= ltt_align((size_t)pos
, tf
->trace
->arch_size
, tf
->alignment
);
1800 g_assert(tf
->has_heartbeat
);
1801 pos
+= ltt_align((size_t)pos
, sizeof(uint32_t), tf
->alignment
);
1805 if(tf
->has_heartbeat
) {
1806 event
->timestamp
= ltt_get_uint32(LTT_GET_BO(tf
),
1809 /* 32 bits -> 64 bits tsc */
1810 /* note : still works for seek and non seek cases. */
1811 if(event
->timestamp
< (0xFFFFFFFFULL
&tf
->buffer
.tsc
)) {
1812 tf
->buffer
.tsc
= ((tf
->buffer
.tsc
&0xFFFFFFFF00000000ULL
)
1814 | (guint64
)event
->timestamp
;
1815 event
->tsc
= tf
->buffer
.tsc
;
1818 tf
->buffer
.tsc
= (tf
->buffer
.tsc
&0xFFFFFFFF00000000ULL
)
1819 | (guint64
)event
->timestamp
;
1820 event
->tsc
= tf
->buffer
.tsc
;
1821 event
->compact_data
= 0;
1824 /* Compact header */
1825 /* We keep the LSB of the previous timestamp, to make sure
1826 * we never go back */
1827 event
->event_id
= event
->timestamp
>> tf
->tscbits
;
1828 event
->event_id
= event
->event_id
& ((1 << tf
->compact_event_bits
) - 1);
1829 event
->compact_data
= event
->timestamp
>>
1830 (tf
->compact_event_bits
+ tf
->tscbits
);
1831 //printf("tsc bits %u, ev bits %u init data %u\n",
1832 // tf->tscbits, tf->trace->compact_event_bits, event->compact_data);
1833 /* Put the compact data back in original endianness */
1834 event
->compact_data
= ltt_get_uint32(LTT_GET_BO(tf
), &event
->compact_data
);
1835 event
->event_size
= 0xFFFF;
1836 //printf("Found compact event %d\n", event->event_id);
1837 //printf("Compact data %d\n", event->compact_data);
1838 event
->timestamp
= event
->timestamp
<< tf
->tsc_lsb_truncate
;
1839 event
->timestamp
= event
->timestamp
& tf
->tsc_mask
;
1840 //printf("timestamp 0x%lX\n", event->timestamp);
1841 //printf("mask 0x%llX\n", tf->tsc_mask);
1842 //printf("mask_next 0x%llX\n", tf->tsc_mask_next_bit);
1843 //printf("previous tsc 0x%llX\n", tf->buffer.tsc);
1844 //printf("previous tsc&mask 0x%llX\n", tf->tsc_mask&tf->buffer.tsc);
1845 //printf("previous tsc&(~mask) 0x%llX\n", tf->buffer.tsc&(~tf->tsc_mask));
1846 if(event
->timestamp
< (tf
->tsc_mask
&tf
->buffer
.tsc
)) {
1848 tf
->buffer
.tsc
= ((tf
->buffer
.tsc
&(~tf
->tsc_mask
))
1849 + tf
->tsc_mask_next_bit
)
1850 | (guint64
)event
->timestamp
;
1851 event
->tsc
= tf
->buffer
.tsc
;
1853 //printf("no wrap\n");
1855 tf
->buffer
.tsc
= (tf
->buffer
.tsc
&(~tf
->tsc_mask
))
1856 | (guint64
)event
->timestamp
;
1857 event
->tsc
= tf
->buffer
.tsc
;
1859 //printf("current tsc 0x%llX\n", tf->buffer.tsc);
1861 pos
+= sizeof(guint32
);
1863 event
->tsc
= ltt_get_uint64(LTT_GET_BO(tf
), pos
);
1864 tf
->buffer
.tsc
= event
->tsc
;
1865 event
->compact_data
= 0;
1866 pos
+= sizeof(guint64
);
1868 event
->event_time
= ltt_interpolate_time(tf
, event
);
1871 event
->event_id
= *(guint16
*)pos
;
1872 pos
+= sizeof(guint16
);
1874 event
->event_size
= ltt_get_uint16(LTT_GET_BO(tf
), pos
);
1875 pos
+= sizeof(guint16
);
1878 event
->event_size
= 0xFFFF;
1882 print_debug_event_header(event
, pos_aligned
, pos
);
1884 /* Align the head */
1886 pos
+= ltt_align((size_t)pos
, tf
->trace
->arch_size
, tf
->alignment
);
1890 /* get the data size and update the event fields with the current
1891 * information. Also update the time if a heartbeat_full event is found. */
1892 ltt_update_event_size(tf
);
1898 /****************************************************************************
1900 * map_block : map a block from the file
1902 * lttdes : ltt trace file
1903 * whichBlock : the block which will be read
1906 * EINVAL : lseek fail
1907 * EIO : can not read from the file
1908 ****************************************************************************/
1910 gint
map_block(LttTracefile
* tf
, guint block_num
)
1912 int page_size
= getpagesize();
1913 struct ltt_block_start_header
*header
;
1915 g_assert(block_num
< tf
->num_blocks
);
1917 if(tf
->buffer
.head
!= NULL
) {
1918 if(munmap(tf
->buffer
.head
, PAGE_ALIGN(tf
->buf_size
))) {
1919 g_warning("unmap size : %u\n",
1920 PAGE_ALIGN(tf
->buf_size
));
1921 perror("munmap error");
1927 /* Multiple of pages aligned head */
1928 tf
->buffer
.head
= mmap(0,
1929 PAGE_ALIGN(tf
->buf_size
),
1930 PROT_READ
, MAP_PRIVATE
, tf
->fd
,
1931 PAGE_ALIGN((off_t
)tf
->buf_size
* (off_t
)block_num
));
1933 if(tf
->buffer
.head
== MAP_FAILED
) {
1934 perror("Error in allocating memory for buffer of tracefile");
1938 g_assert( ( (gulong
)tf
->buffer
.head
&(8-1) ) == 0); // make sure it's aligned.
1941 tf
->buffer
.index
= block_num
;
1943 header
= (struct ltt_block_start_header
*)tf
->buffer
.head
;
1946 tf
->buffer
.begin
.timestamp
= ltt_time_add(
1947 ltt_time_from_uint64(
1948 ltt_get_uint64(LTT_GET_BO(tf
),
1949 &header
->begin
.timestamp
)
1950 - tf
->trace
->start_monotonic
),
1951 tf
->trace
->start_time
);
1953 //g_debug("block %u begin : %lu.%lu", block_num,
1954 // tf->buffer.begin.timestamp.tv_sec, tf->buffer.begin.timestamp.tv_nsec);
1955 tf
->buffer
.begin
.cycle_count
= ltt_get_uint64(LTT_GET_BO(tf
),
1956 &header
->begin
.cycle_count
);
1957 tf
->buffer
.begin
.freq
= ltt_get_uint64(LTT_GET_BO(tf
),
1958 &header
->begin
.freq
);
1959 if(tf
->buffer
.begin
.freq
== 0)
1960 tf
->buffer
.begin
.freq
= tf
->trace
->start_freq
;
1962 tf
->buffer
.begin
.timestamp
= ltt_interpolate_time_from_tsc(tf
,
1963 tf
->buffer
.begin
.cycle_count
);
1966 ltt_time_from_uint64(
1967 (double)(tf
->buffer
.begin
.cycle_count
1968 - tf
->trace
->start_tsc
) * 1000000.0
1969 / (double)tf
->trace
->start_freq
),
1970 tf
->trace
->start_time_from_tsc
);
1974 tf
->buffer
.end
.timestamp
= ltt_time_add(
1975 ltt_time_from_uint64(
1976 ltt_get_uint64(LTT_GET_BO(tf
),
1977 &header
->end
.timestamp
)
1978 - tf
->trace
->start_monotonic
),
1979 tf
->trace
->start_time
);
1981 //g_debug("block %u end : %lu.%lu", block_num,
1982 // tf->buffer.end.timestamp.tv_sec, tf->buffer.end.timestamp.tv_nsec);
1983 tf
->buffer
.end
.cycle_count
= ltt_get_uint64(LTT_GET_BO(tf
),
1984 &header
->end
.cycle_count
);
1985 tf
->buffer
.end
.freq
= ltt_get_uint64(LTT_GET_BO(tf
),
1987 if(tf
->buffer
.end
.freq
== 0)
1988 tf
->buffer
.end
.freq
= tf
->trace
->start_freq
;
1990 tf
->buffer
.lost_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1991 &header
->lost_size
);
1992 tf
->buffer
.end
.timestamp
= ltt_interpolate_time_from_tsc(tf
,
1993 tf
->buffer
.end
.cycle_count
);
1996 ltt_time_from_uint64(
1997 (double)(tf
->buffer
.end
.cycle_count
1998 - tf
->trace
->start_tsc
) * 1000000.0
1999 / (double)tf
->trace
->start_freq
),
2000 tf
->trace
->start_time_from_tsc
);
2002 tf
->buffer
.tsc
= tf
->buffer
.begin
.cycle_count
;
2003 tf
->event
.tsc
= tf
->buffer
.tsc
;
2004 tf
->buffer
.freq
= tf
->buffer
.begin
.freq
;
2007 * eventually support variable buffer size : will need a partial pre-read of
2008 * the headers to create an index when we open the trace... eventually. */
2009 g_assert(tf
->buf_size
== ltt_get_uint32(LTT_GET_BO(tf
),
2010 &header
->buf_size
));
2012 /* Now that the buffer is mapped, calculate the time interpolation for the
2015 // tf->buffer.nsecs_per_cycle = calc_nsecs_per_cycle(tf);
2016 //tf->buffer.cyc2ns_scale = calc_nsecs_per_cycle(tf);
2018 /* Make the current event point to the beginning of the buffer :
2019 * it means that the event read must get the first event. */
2020 tf
->event
.tracefile
= tf
;
2021 tf
->event
.block
= block_num
;
2022 tf
->event
.offset
= 0;
2031 static void print_debug_event_data(LttEvent
*ev
)
2033 unsigned int offset
= 0;
2036 if (!max(ev
->event_size
, ev
->data_size
))
2039 g_printf("Event data (tracefile %s offset %llx):\n",
2040 g_quark_to_string(ev
->tracefile
->long_name
),
2041 ((uint64_t)ev
->tracefile
->buffer
.index
* ev
->tracefile
->buf_size
)
2042 + (long)ev
->data
- (long)ev
->tracefile
->buffer
.head
);
2044 while (offset
< max(ev
->event_size
, ev
->data_size
)) {
2045 g_printf("%8lx", (long)ev
->data
+ offset
2046 - (long)ev
->tracefile
->buffer
.head
);
2049 for (i
= 0; i
< 4 ; i
++) {
2050 for (j
= 0; j
< 4; j
++) {
2051 if (offset
+ ((i
* 4) + j
) < max(ev
->event_size
, ev
->data_size
))
2052 g_printf("%02hhX", ((char*)ev
->data
)[offset
+ ((i
* 4) + j
)]);
2063 for (i
= 0; i
< 4; i
++) {
2064 for (j
= 0; j
< 4; j
++) {
2065 if (offset
+ ((i
* 4) + j
) < max(ev
->event_size
, ev
->data_size
)) {
2066 if (isprint(((char*)ev
->data
)[offset
+ ((i
* 4) + j
)]))
2067 g_printf("%c", ((char*)ev
->data
)[offset
+ ((i
* 4) + j
)]);
2079 /* It will update the fields offsets too */
2080 void ltt_update_event_size(LttTracefile
*tf
)
2084 struct marker_info
*info
;
2086 switch((enum marker_id
)tf
->event
.event_id
) {
2087 case MARKER_ID_SET_MARKER_ID
:
2088 size
= strlen((char*)tf
->event
.data
) + 1;
2089 g_debug("marker %s id set", (char*)tf
->event
.data
);
2090 size
+= ltt_align(size
, sizeof(guint16
), tf
->alignment
);
2091 size
+= sizeof(guint16
);
2092 size
+= sizeof(guint8
);
2093 size
+= sizeof(guint8
);
2094 size
+= sizeof(guint8
);
2095 size
+= sizeof(guint8
);
2096 size
+= sizeof(guint8
);
2098 case MARKER_ID_SET_MARKER_FORMAT
:
2099 g_debug("marker %s format set", (char*)tf
->event
.data
);
2100 size
= strlen((char*)tf
->event
.data
) + 1;
2101 size
+= strlen((char*)tf
->event
.data
+ size
) + 1;
2103 case MARKER_ID_HEARTBEAT_32
:
2104 g_debug("Update Event heartbeat 32 bits");
2106 case MARKER_ID_HEARTBEAT_64
:
2107 g_debug("Update Event heartbeat 64 bits");
2108 tscdata
= (char*)(tf
->event
.data
);
2109 tf
->event
.tsc
= ltt_get_uint64(LTT_GET_BO(tf
), tscdata
);
2110 tf
->buffer
.tsc
= tf
->event
.tsc
;
2111 tf
->event
.event_time
= ltt_interpolate_time(tf
, &tf
->event
);
2112 size
= ltt_align(size
, sizeof(guint64
), tf
->alignment
);
2113 size
+= sizeof(guint64
);
2117 info
= marker_get_info_from_id(tf
->trace
, tf
->event
.event_id
);
2119 if (tf
->event
.event_id
>= MARKER_CORE_IDS
)
2120 g_assert(info
!= NULL
);
2122 /* Do not update field offsets of core markers when initially reading the
2123 * facility tracefile when the infos about these markers do not exist yet.
2125 if (likely(info
&& info
->fields
)) {
2126 if (info
->size
!= -1)
2129 size
= marker_update_fields_offsets(marker_get_info_from_id(tf
->trace
,
2130 tf
->event
.event_id
), tf
->event
.data
);
2133 tf
->event
.data_size
= size
;
2135 /* Check consistency between kernel and LTTV structure sizes */
2136 if(tf
->event
.event_size
== 0xFFFF) {
2137 /* Event size too big to fit in the event size field */
2138 tf
->event
.event_size
= tf
->event
.data_size
;
2142 print_debug_event_data(&tf
->event
);
2144 /* Having a marker load or marker format event out of the facilities
2145 * tracefiles is a serious bug. */
2146 switch((enum marker_id
)tf
->event
.event_id
) {
2147 case MARKER_ID_SET_MARKER_ID
:
2148 case MARKER_ID_SET_MARKER_FORMAT
:
2149 if (tf
->name
!= g_quark_from_string("/control/facilities"))
2150 g_error("Trace inconsistency : metadata event found in data "
2151 "tracefile %s", g_quark_to_string(tf
->long_name
));
2154 if (tf
->event
.data_size
!= tf
->event
.event_size
) {
2155 struct marker_info
*info
= marker_get_info_from_id(tf
->trace
,
2156 tf
->event
.event_id
);
2157 g_error("Kernel/LTTV event size differs for event %s: kernel %u, LTTV %u",
2158 g_quark_to_string(info
->name
),
2159 tf
->event
.event_size
, tf
->event
.data_size
);
2164 LttEventType
*event_type
=
2165 ltt_facility_eventtype_get(f
, tf
->event
.event_id
);
2168 g_warning("Unknown event id %hhu in facility %s in tracefile %s",
2170 g_quark_to_string(f
->name
),
2171 g_quark_to_string(tf
->name
));
2172 goto event_type_error
;
2175 /* Compute the dynamic offsets */
2176 compute_offsets(tf
, f
, event_type
, &size
, tf
->event
.data
);
2178 //g_debug("Event root field : f.e %hhu.%hhu size %zd",
2179 // tf->event.facility_id,
2180 // tf->event.event_id, size);
2183 tf
->event
.data_size
= size
;
2185 /* Check consistency between kernel and LTTV structure sizes */
2186 if(tf
->event
.event_size
== 0xFFFF) {
2187 /* Event size too big to fit in the event size field */
2188 tf
->event
.event_size
= tf
->event
.data_size
;
2190 if (tf
->event
.data_size
!= tf
->event
.event_size
) {
2191 g_error("Kernel/LTTV event size differs for event %s.%s: kernel %u, LTTV %u",
2192 g_quark_to_string(f
->name
), g_quark_to_string(event_type
->name
),
2193 tf
->event
.event_size
, tf
->event
.data_size
);
2196 //g_assert(tf->event.data_size == tf->event.event_size);
2202 if(tf
->event
.event_size
== 0xFFFF) {
2203 g_error("Cannot jump over an unknown event bigger than 0xFFFE bytes");
2205 /* The facility is unknown : use the kernel information about this event
2206 * to jump over it. */
2207 tf
->event
.data_size
= tf
->event
.event_size
;
2212 /* Take the tf current event offset and use the event facility id and event id
2213 * to figure out where is the next event offset.
2215 * This is an internal function not aiming at being used elsewhere : it will
2216 * not jump over the current block limits. Please consider using
2217 * ltt_tracefile_read to do this.
2219 * Returns 0 on success
2220 * ERANGE if we are at the end of the buffer.
2221 * ENOPROTOOPT if an error occured when getting the current event size.
2223 int ltt_seek_next_event(LttTracefile
*tf
)
2228 /* seek over the buffer header if we are at the buffer start */
2229 if(tf
->event
.offset
== 0) {
2230 tf
->event
.offset
+= tf
->buffer_header_size
;
2232 if(tf
->event
.offset
== tf
->buf_size
- tf
->buffer
.lost_size
) {
2239 pos
= tf
->event
.data
;
2241 if(tf
->event
.data_size
< 0) goto error
;
2243 pos
+= (size_t)tf
->event
.data_size
;
2245 tf
->event
.offset
= pos
- tf
->buffer
.head
;
2247 if(tf
->event
.offset
== tf
->buf_size
- tf
->buffer
.lost_size
) {
2251 g_assert(tf
->event
.offset
< tf
->buf_size
- tf
->buffer
.lost_size
);
2257 g_error("Error in ltt_seek_next_event for tracefile %s",
2258 g_quark_to_string(tf
->name
));
2263 /*****************************************************************************
2265 * calc_nsecs_per_cycle : calculate nsecs per cycle for current block
2267 * 1.0 / (freq(khz) *1000) * 1000000000
2270 ****************************************************************************/
2271 /* from timer_tsc.c */
2272 #define CYC2NS_SCALE_FACTOR 10
2273 static guint32
calc_nsecs_per_cycle(LttTracefile
* tf
)
2275 //return 1e6 / (double)tf->buffer.freq;
2276 guint32 cpu_mhz
= tf
->buffer
.freq
/ 1000;
2277 guint32 cyc2ns_scale
= (1000 << CYC2NS_SCALE_FACTOR
)/cpu_mhz
;
2279 return cyc2ns_scale
;
2280 // return 1e6 / (double)tf->buffer.freq;
2283 static guint64
cycles_2_ns(LttTracefile
*tf
, guint64 cycles
)
2285 return (cycles
* tf
->buffer
.cyc2ns_scale
) >> CYC2NS_SCALE_FACTOR
;
2290 void setFieldsOffset(LttTracefile
*tf
, LttEventType
*evT
,void *evD
)
2292 LttField
* rootFld
= evT
->root_field
;
2293 // rootFld->base_address = evD;
2296 rootFld
->field_size
= getFieldtypeSize(tf
, evT
->facility
,
2297 evT
, 0,0,rootFld
, evD
);
2301 /*****************************************************************************
2303 * set_fields_offsets : set the precomputable offset of the fields
2305 * tracefile : opened trace file
2306 * event_type : the event type
2307 ****************************************************************************/
2309 void set_fields_offsets(LttTracefile
*tf
, LttEventType
*event_type
)
2311 LttField
*field
= event_type
->root_field
;
2312 enum field_status fixed_root
= FIELD_FIXED
, fixed_parent
= FIELD_FIXED
;
2315 preset_field_type_size(tf
, event_type
, 0, 0,
2316 &fixed_root
, &fixed_parent
,
2323 /*****************************************************************************
2325 * get_alignment : Get the alignment needed for a field.
2329 * returns : The size on which it must be aligned.
2331 ****************************************************************************/
2333 off_t
get_alignment(LttField
*field
)
2335 LttType
*type
= &field
->field_type
;
2337 switch(type
->type_class
) {
2339 case LTT_UINT_FIXED
:
2354 /* Align offset on type size */
2355 g_assert(field
->field_size
!= 0);
2356 return field
->field_size
;
2362 g_assert(type
->fields
->len
== 1);
2364 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
2365 return get_alignment(child
);
2369 g_assert(type
->fields
->len
== 2);
2372 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
2374 localign
= max(localign
, get_alignment(child
));
2376 child
= &g_array_index(type
->fields
, LttField
, 1);
2377 localign
= max(localign
, get_alignment(child
));
2388 for(i
=0; i
<type
->fields
->len
; i
++) {
2389 LttField
*child
= &g_array_index(type
->fields
, LttField
, i
);
2390 localign
= max(localign
, get_alignment(child
));
2397 g_error("get_alignment : unknown type");
2404 /*****************************************************************************
2406 * field_compute_static_size : Determine the size of fields known by their
2407 * sole definition. Unions, arrays and struct sizes might be known, but
2408 * the parser does not give that information.
2413 ****************************************************************************/
2415 void field_compute_static_size(LttFacility
*fac
, LttField
*field
)
2417 LttType
*type
= &field
->field_type
;
2419 switch(type
->type_class
) {
2421 case LTT_UINT_FIXED
:
2440 /* note this : array type size is the number of elements in the array,
2441 * while array field size of the length of the array in bytes */
2442 g_assert(type
->fields
->len
== 1);
2444 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
2445 field_compute_static_size(fac
, child
);
2447 if(child
->field_size
!= 0) {
2448 field
->field_size
= type
->size
* child
->field_size
;
2449 field
->dynamic_offsets
= g_array_sized_new(FALSE
, TRUE
,
2450 sizeof(off_t
), type
->size
);
2452 field
->field_size
= 0;
2457 g_assert(type
->fields
->len
== 2);
2459 off_t local_offset
= 0;
2460 LttField
*child
= &g_array_index(type
->fields
, LttField
, 1);
2461 field_compute_static_size(fac
, child
);
2462 field
->field_size
= 0;
2464 if(child
->field_size
!= 0) {
2465 field
->dynamic_offsets
= g_array_sized_new(FALSE
, TRUE
,
2466 sizeof(off_t
), SEQUENCE_AVG_ELEMENTS
);
2474 for(i
=0;i
<type
->fields
->len
;i
++) {
2475 LttField
*child
= &g_array_index(type
->fields
, LttField
, i
);
2476 field_compute_static_size(fac
, child
);
2477 if(child
->field_size
!= 0) {
2478 type
->size
+= ltt_align(type
->size
, get_alignment(child
),
2480 type
->size
+= child
->field_size
;
2482 /* As soon as we find a child with variable size, we have
2483 * a variable size */
2488 field
->field_size
= type
->size
;
2492 g_error("field_static_size : unknown type");
2499 /*****************************************************************************
2501 * precompute_fields_offsets : set the precomputable offset of the fields
2505 * offset : pointer to the current offset, must be incremented
2507 * return : 1 : found a variable length field, stop the processing.
2509 ****************************************************************************/
2512 gint
precompute_fields_offsets(LttFacility
*fac
, LttField
*field
, off_t
*offset
, gint is_compact
)
2514 LttType
*type
= &field
->field_type
;
2516 if(unlikely(is_compact
)) {
2517 g_assert(field
->field_size
!= 0);
2518 /* FIXME THIS IS A HUUUUUGE hack :
2519 * offset is between the compact_data field in struct LttEvent
2520 * and the address of the field root in the memory map.
2521 * ark. Both will stay at the same addresses while the event
2522 * is readable, so it's ok.
2524 field
->offset_root
= 0;
2525 field
->fixed_root
= FIELD_FIXED
;
2529 switch(type
->type_class
) {
2531 case LTT_UINT_FIXED
:
2546 g_assert(field
->field_size
!= 0);
2547 /* Align offset on type size */
2548 *offset
+= ltt_align(*offset
, get_alignment(field
),
2550 /* remember offset */
2551 field
->offset_root
= *offset
;
2552 field
->fixed_root
= FIELD_FIXED
;
2553 /* Increment offset */
2554 *offset
+= field
->field_size
;
2558 field
->offset_root
= *offset
;
2559 field
->fixed_root
= FIELD_FIXED
;
2563 g_assert(type
->fields
->len
== 1);
2565 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
2567 *offset
+= ltt_align(*offset
, get_alignment(field
),
2570 /* remember offset */
2571 field
->offset_root
= *offset
;
2572 field
->array_offset
= *offset
;
2573 field
->fixed_root
= FIELD_FIXED
;
2575 /* Let the child be variable */
2576 //precompute_fields_offsets(tf, child, offset);
2578 if(field
->field_size
!= 0) {
2579 /* Increment offset */
2580 /* field_size is the array size in bytes */
2581 *offset
+= field
->field_size
;
2589 g_assert(type
->fields
->len
== 2);
2594 *offset
+= ltt_align(*offset
, get_alignment(field
),
2597 /* remember offset */
2598 field
->offset_root
= *offset
;
2599 field
->fixed_root
= FIELD_FIXED
;
2601 child
= &g_array_index(type
->fields
, LttField
, 0);
2602 ret
= precompute_fields_offsets(fac
, child
, offset
, is_compact
);
2603 g_assert(ret
== 0); /* Seq len cannot have variable len */
2605 child
= &g_array_index(type
->fields
, LttField
, 1);
2606 *offset
+= ltt_align(*offset
, get_alignment(child
),
2608 field
->array_offset
= *offset
;
2609 /* Let the child be variable. */
2610 //ret = precompute_fields_offsets(fac, child, offset);
2612 /* Cannot precompute fields offsets of sequence members, and has
2613 * variable length. */
2623 *offset
+= ltt_align(*offset
, get_alignment(field
),
2625 /* remember offset */
2626 field
->offset_root
= *offset
;
2627 field
->fixed_root
= FIELD_FIXED
;
2629 for(i
=0; i
< type
->fields
->len
; i
++) {
2630 child
= &g_array_index(type
->fields
, LttField
, i
);
2631 ret
= precompute_fields_offsets(fac
, child
, offset
, is_compact
);
2644 *offset
+= ltt_align(*offset
, get_alignment(field
),
2646 /* remember offset */
2647 field
->offset_root
= *offset
;
2648 field
->fixed_root
= FIELD_FIXED
;
2650 for(i
=0; i
< type
->fields
->len
; i
++) {
2651 *offset
= field
->offset_root
;
2652 child
= &g_array_index(type
->fields
, LttField
, i
);
2653 ret
= precompute_fields_offsets(fac
, child
, offset
, is_compact
);
2657 *offset
= field
->offset_root
+ field
->field_size
;
2664 g_error("precompute_fields_offsets : unknown type");
2673 /*****************************************************************************
2675 * precompute_offsets : set the precomputable offset of an event type
2678 * event : event type
2680 ****************************************************************************/
2681 void precompute_offsets(LttFacility
*fac
, LttEventType
*event
)
2687 /* First, compute the size of fixed size fields. Will determine size for
2688 * arrays, struct and unions, which is not done by the parser */
2689 for(i
=0; i
<event
->fields
->len
; i
++) {
2690 LttField
*field
= &g_array_index(event
->fields
, LttField
, i
);
2691 field_compute_static_size(fac
, field
);
2694 /* Precompute all known offsets */
2695 for(i
=0; i
<event
->fields
->len
; i
++) {
2696 LttField
*field
= &g_array_index(event
->fields
, LttField
, i
);
2697 if(event
->has_compact_data
&& i
== 0)
2698 ret
= precompute_fields_offsets(fac
, field
, &offset
, 1);
2700 ret
= precompute_fields_offsets(fac
, field
, &offset
, 0);
2708 /*****************************************************************************
2710 * preset_field_type_size : set the fixed sizes of the field type
2713 * event_type : event type
2714 * offset_root : offset from the root
2715 * offset_parent : offset from the parent
2716 * fixed_root : Do we know a fixed offset to the root ?
2717 * fixed_parent : Do we know a fixed offset to the parent ?
2719 ****************************************************************************/
2723 // preset the fixed size offsets. Calculate them just like genevent-new : an
2724 // increment of a *to value that represents the offset from the start of the
2726 // The preset information is : offsets up to (and including) the first element
2727 // of variable size. All subsequent fields must be flagged "VARIABLE OFFSET".
2729 void preset_field_type_size(LttTracefile
*tf
, LttEventType
*event_type
,
2730 off_t offset_root
, off_t offset_parent
,
2731 enum field_status
*fixed_root
, enum field_status
*fixed_parent
,
2734 enum field_status local_fixed_root
, local_fixed_parent
;
2738 g_assert(field
->fixed_root
== FIELD_UNKNOWN
);
2739 g_assert(field
->fixed_parent
== FIELD_UNKNOWN
);
2740 g_assert(field
->fixed_size
== FIELD_UNKNOWN
);
2742 type
= field
->field_type
;
2744 field
->fixed_root
= *fixed_root
;
2745 if(field
->fixed_root
== FIELD_FIXED
)
2746 field
->offset_root
= offset_root
;
2748 field
->offset_root
= 0;
2750 field
->fixed_parent
= *fixed_parent
;
2751 if(field
->fixed_parent
== FIELD_FIXED
)
2752 field
->offset_parent
= offset_parent
;
2754 field
->offset_parent
= 0;
2756 size_t current_root_offset
;
2757 size_t current_offset
;
2758 enum field_status current_child_status
, final_child_status
;
2761 switch(type
->type_class
) {
2763 case LTT_UINT_FIXED
:
2772 field
->field_size
= ltt_type_size(tf
->trace
, type
);
2773 field
->fixed_size
= FIELD_FIXED
;
2776 field
->field_size
= (off_t
)event_type
->facility
->pointer_size
;
2777 field
->fixed_size
= FIELD_FIXED
;
2781 field
->field_size
= (off_t
)event_type
->facility
->long_size
;
2782 field
->fixed_size
= FIELD_FIXED
;
2787 field
->field_size
= (off_t
)event_type
->facility
->size_t_size
;
2788 field
->fixed_size
= FIELD_FIXED
;
2791 local_fixed_root
= FIELD_VARIABLE
;
2792 local_fixed_parent
= FIELD_VARIABLE
;
2793 preset_field_type_size(tf
, event_type
,
2795 &local_fixed_root
, &local_fixed_parent
,
2797 field
->fixed_size
= FIELD_VARIABLE
;
2798 field
->field_size
= 0;
2799 *fixed_root
= FIELD_VARIABLE
;
2800 *fixed_parent
= FIELD_VARIABLE
;
2803 field
->fixed_size
= FIELD_VARIABLE
;
2804 field
->field_size
= 0;
2805 *fixed_root
= FIELD_VARIABLE
;
2806 *fixed_parent
= FIELD_VARIABLE
;
2809 local_fixed_root
= FIELD_VARIABLE
;
2810 local_fixed_parent
= FIELD_VARIABLE
;
2811 preset_field_type_size(tf
, event_type
,
2813 &local_fixed_root
, &local_fixed_parent
,
2815 field
->fixed_size
= field
->child
[0]->fixed_size
;
2816 if(field
->fixed_size
== FIELD_FIXED
) {
2817 field
->field_size
= type
->element_number
* field
->child
[0]->field_size
;
2819 field
->field_size
= 0;
2820 *fixed_root
= FIELD_VARIABLE
;
2821 *fixed_parent
= FIELD_VARIABLE
;
2825 current_root_offset
= field
->offset_root
;
2827 current_child_status
= FIELD_FIXED
;
2828 for(i
=0;i
<type
->element_number
;i
++) {
2829 preset_field_type_size(tf
, event_type
,
2830 current_root_offset
, current_offset
,
2831 fixed_root
, ¤t_child_status
,
2833 if(current_child_status
== FIELD_FIXED
) {
2834 current_root_offset
+= field
->child
[i
]->field_size
;
2835 current_offset
+= field
->child
[i
]->field_size
;
2837 current_root_offset
= 0;
2841 if(current_child_status
!= FIELD_FIXED
) {
2842 *fixed_parent
= current_child_status
;
2843 field
->field_size
= 0;
2844 field
->fixed_size
= current_child_status
;
2846 field
->field_size
= current_offset
;
2847 field
->fixed_size
= FIELD_FIXED
;
2851 current_root_offset
= field
->offset_root
;
2854 final_child_status
= FIELD_FIXED
;
2855 for(i
=0;i
<type
->element_number
;i
++) {
2856 enum field_status current_root_child_status
= FIELD_FIXED
;
2857 enum field_status current_child_status
= FIELD_FIXED
;
2858 preset_field_type_size(tf
, event_type
,
2859 current_root_offset
, current_offset
,
2860 ¤t_root_child_status
, ¤t_child_status
,
2862 if(current_child_status
!= FIELD_FIXED
)
2863 final_child_status
= current_child_status
;
2865 max_size
= max(max_size
, field
->child
[i
]->field_size
);
2867 if(final_child_status
!= FIELD_FIXED
) {
2868 g_error("LTTV does not support variable size fields in unions.");
2869 /* This will stop the application. */
2870 *fixed_root
= final_child_status
;
2871 *fixed_parent
= final_child_status
;
2872 field
->field_size
= 0;
2873 field
->fixed_size
= current_child_status
;
2875 field
->field_size
= max_size
;
2876 field
->fixed_size
= FIELD_FIXED
;
2880 g_error("unexpected type NONE");
2887 /*****************************************************************************
2889 * check_fields_compatibility : Check for compatibility between two fields :
2890 * do they use the same inner structure ?
2892 * event_type1 : event type
2893 * event_type2 : event type
2896 *Returns : 0 if identical
2898 ****************************************************************************/
2899 // this function checks for equality of field types. Therefore, it does not use
2900 // per se offsets. For instance, an aligned version of a structure is
2901 // compatible with an unaligned version of the same structure.
2903 gint
check_fields_compatibility(LttEventType
*event_type1
,
2904 LttEventType
*event_type2
,
2905 LttField
*field1
, LttField
*field2
)
2907 guint different
= 0;
2911 if(field1
== NULL
) {
2912 if(field2
== NULL
) goto end
;
2917 } else if(field2
== NULL
) {
2922 type1
= &field1
->field_type
;
2923 type2
= &field2
->field_type
;
2925 if(type1
->type_class
!= type2
->type_class
) {
2929 if(type1
->network
!= type2
->network
) {
2934 switch(type1
->type_class
) {
2936 case LTT_UINT_FIXED
:
2951 if(field1
->field_size
!= field2
->field_size
)
2958 LttField
*child1
= &g_array_index(type1
->fields
, LttField
, 0);
2959 LttField
*child2
= &g_array_index(type2
->fields
, LttField
, 0);
2961 if(type1
->size
!= type2
->size
)
2963 if(check_fields_compatibility(event_type1
, event_type2
, child1
, child2
))
2969 LttField
*child1
= &g_array_index(type1
->fields
, LttField
, 1);
2970 LttField
*child2
= &g_array_index(type2
->fields
, LttField
, 1);
2972 if(check_fields_compatibility(event_type1
, event_type2
, child1
, child2
))
2982 if(type1
->fields
->len
!= type2
->fields
->len
) {
2987 for(i
=0; i
< type1
->fields
->len
; i
++) {
2990 child1
= &g_array_index(type1
->fields
, LttField
, i
);
2991 child2
= &g_array_index(type2
->fields
, LttField
, i
);
2992 different
= check_fields_compatibility(event_type1
,
2993 event_type2
, child1
, child2
);
2995 if(different
) break;
3001 g_error("check_fields_compatibility : unknown type");
3010 gint
check_fields_compatibility(LttEventType
*event_type1
,
3011 LttEventType
*event_type2
,
3012 LttField
*field1
, LttField
*field2
)
3014 guint different
= 0;
3019 if(field1
== NULL
) {
3020 if(field2
== NULL
) goto end
;
3025 } else if(field2
== NULL
) {
3030 g_assert(field1
->fixed_root
!= FIELD_UNKNOWN
);
3031 g_assert(field2
->fixed_root
!= FIELD_UNKNOWN
);
3032 g_assert(field1
->fixed_parent
!= FIELD_UNKNOWN
);
3033 g_assert(field2
->fixed_parent
!= FIELD_UNKNOWN
);
3034 g_assert(field1
->fixed_size
!= FIELD_UNKNOWN
);
3035 g_assert(field2
->fixed_size
!= FIELD_UNKNOWN
);
3037 type1
= field1
->field_type
;
3038 type2
= field2
->field_type
;
3040 if(type1
->type_class
!= type2
->type_class
) {
3044 if(type1
->element_name
!= type2
->element_name
) {
3049 switch(type1
->type_class
) {
3051 case LTT_UINT_FIXED
:
3066 if(field1
->field_size
!= field2
->field_size
) {
3072 if(type1
->element_number
!= type2
->element_number
) {
3076 for(i
=0;i
<type1
->element_number
;i
++) {
3077 if(type1
->enum_strings
[i
] != type2
->enum_strings
[i
]) {
3084 /* Two elements : size and child */
3085 g_assert(type1
->element_number
!= type2
->element_number
);
3086 for(i
=0;i
<type1
->element_number
;i
++) {
3087 if(check_fields_compatibility(event_type1
, event_type2
,
3088 field1
->child
[0], field2
->child
[0])) {
3097 if(field1
->field_size
!= field2
->field_size
) {
3101 /* Two elements : size and child */
3102 g_assert(type1
->element_number
!= type2
->element_number
);
3103 for(i
=0;i
<type1
->element_number
;i
++) {
3104 if(check_fields_compatibility(event_type1
, event_type2
,
3105 field1
->child
[0], field2
->child
[0])) {
3113 if(type1
->element_number
!= type2
->element_number
) {
3117 for(i
=0;i
<type1
->element_number
;i
++) {
3118 if(check_fields_compatibility(event_type1
, event_type2
,
3119 field1
->child
[0], field2
->child
[0])) {
3132 /*****************************************************************************
3134 * ltt_get_int : get an integer number
3136 * reverse_byte_order: must we reverse the byte order ?
3137 * size : the size of the integer
3138 * ptr : the data pointer
3140 * gint64 : a 64 bits integer
3141 ****************************************************************************/
3143 gint64
ltt_get_int(gboolean reverse_byte_order
, gint size
, void *data
)
3148 case 1: val
= *((gint8
*)data
); break;
3149 case 2: val
= ltt_get_int16(reverse_byte_order
, data
); break;
3150 case 4: val
= ltt_get_int32(reverse_byte_order
, data
); break;
3151 case 8: val
= ltt_get_int64(reverse_byte_order
, data
); break;
3152 default: val
= ltt_get_int64(reverse_byte_order
, data
);
3153 g_critical("get_int : integer size %d unknown", size
);
3160 /*****************************************************************************
3162 * ltt_get_uint : get an unsigned integer number
3164 * reverse_byte_order: must we reverse the byte order ?
3165 * size : the size of the integer
3166 * ptr : the data pointer
3168 * guint64 : a 64 bits unsigned integer
3169 ****************************************************************************/
3171 guint64
ltt_get_uint(gboolean reverse_byte_order
, gint size
, void *data
)
3176 case 1: val
= *((gint8
*)data
); break;
3177 case 2: val
= ltt_get_uint16(reverse_byte_order
, data
); break;
3178 case 4: val
= ltt_get_uint32(reverse_byte_order
, data
); break;
3179 case 8: val
= ltt_get_uint64(reverse_byte_order
, data
); break;
3180 default: val
= ltt_get_uint64(reverse_byte_order
, data
);
3181 g_critical("get_uint : unsigned integer size %d unknown",
3190 /* get the node name of the system */
3192 char * ltt_trace_system_description_node_name (LttSystemDescription
* s
)
3194 return s
->node_name
;
3198 /* get the domain name of the system */
3200 char * ltt_trace_system_description_domain_name (LttSystemDescription
* s
)
3202 return s
->domain_name
;
3206 /* get the description of the system */
3208 char * ltt_trace_system_description_description (LttSystemDescription
* s
)
3210 return s
->description
;
3214 /* get the NTP corrected start time of the trace */
3215 LttTime
ltt_trace_start_time(LttTrace
*t
)
3217 return t
->start_time
;
3220 /* get the monotonic start time of the trace */
3221 LttTime
ltt_trace_start_time_monotonic(LttTrace
*t
)
3223 return t
->start_time_from_tsc
;
3226 LttTracefile
*ltt_tracefile_new()
3229 tf
= g_new(LttTracefile
, 1);
3230 tf
->event
.tracefile
= tf
;
3234 void ltt_tracefile_destroy(LttTracefile
*tf
)
3239 void ltt_tracefile_copy(LttTracefile
*dest
, const LttTracefile
*src
)
3244 /* Before library loading... */
3248 LTT_FACILITY_NAME_HEARTBEAT
= g_quark_from_string("heartbeat");
3249 LTT_EVENT_NAME_HEARTBEAT
= g_quark_from_string("heartbeat");
3250 LTT_EVENT_NAME_HEARTBEAT_FULL
= g_quark_from_string("heartbeat_full");
3252 LTT_TRACEFILE_NAME_FACILITIES
= g_quark_from_string("/control/facilities");