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( ( (guint
)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
)) {
884 guint num
, tid
, pgid
;
887 num
= tid
= pgid
= 0;
889 if(get_tracefile_name_number(rel_path
, &name
, &num
, &tid
, &pgid
, &creation
))
890 continue; /* invalid name */
892 g_debug("Opening file.\n");
893 if(ltt_tracefile_open(trace
, path
, &tmp_tf
)) {
894 g_info("Error opening tracefile %s", path
);
896 continue; /* error opening the tracefile : bad magic number ? */
899 g_debug("Tracefile name is %s and number is %u",
900 g_quark_to_string(name
), num
);
902 tmp_tf
.cpu_online
= 1;
903 tmp_tf
.cpu_num
= num
;
907 tmp_tf
.creation
= creation
;
908 if(tmp_tf
.name
== g_quark_from_string("/compact")
909 || tmp_tf
.name
== g_quark_from_string("/flight-compact"))
913 group
= g_datalist_id_get_data(&trace
->tracefiles
, name
);
915 /* Elements are automatically cleared when the array is allocated.
916 * It makes the cpu_online variable set to 0 : cpu offline, by default.
918 group
= g_array_sized_new (FALSE
, TRUE
, sizeof(LttTracefile
), 10);
919 g_datalist_id_set_data_full(&trace
->tracefiles
, name
,
920 group
, ltt_tracefile_group_destroy
);
923 /* Add the per cpu tracefile to the named group */
924 unsigned int old_len
= group
->len
;
926 group
= g_array_set_size(group
, num
+1);
927 g_array_index (group
, LttTracefile
, num
) = tmp_tf
;
928 g_array_index (group
, LttTracefile
, num
).event
.tracefile
=
929 &g_array_index (group
, LttTracefile
, num
);
938 /* ltt_get_facility_description
940 * Opens the file corresponding to the requested facility (identified by fac_id
943 * The name searched is : %trace root%/eventdefs/facname_checksum.xml
945 * Returns 0 on success, or 1 on failure.
948 static int ltt_get_facility_description(LttFacility
*f
,
950 LttTracefile
*fac_tf
)
952 char desc_file_name
[PATH_MAX
];
959 text
= g_quark_to_string(t
->pathname
);
960 textlen
= strlen(text
);
962 if(textlen
>= PATH_MAX
) goto name_error
;
963 strcpy(desc_file_name
, text
);
965 text
= "/eventdefs/";
966 textlen
+=strlen(text
);
967 if(textlen
>= PATH_MAX
) goto name_error
;
968 strcat(desc_file_name
, text
);
970 text
= g_quark_to_string(f
->name
);
971 fac_name_len
= strlen(text
);
972 textlen
+=fac_name_len
;
973 if(textlen
>= PATH_MAX
) goto name_error
;
974 strcat(desc_file_name
, text
);
976 /* arch specific facilities are named like this : name_arch */
977 if(fac_name_len
+1 < sizeof("_arch"))
980 if(!strcmp(&text
[fac_name_len
+1-sizeof("_arch")], "_arch"))
988 textlen
+=strlen(text
);
989 if(textlen
>= PATH_MAX
) goto name_error
;
990 strcat(desc_file_name
, text
);
992 err
= snprintf(desc_file_name
+textlen
, PATH_MAX
-textlen
-1,
994 if(err
< 0) goto name_error
;
996 textlen
=strlen(desc_file_name
);
1001 switch(t
->arch_type
) {
1002 case LTT_ARCH_TYPE_I386
:
1005 case LTT_ARCH_TYPE_PPC
:
1008 case LTT_ARCH_TYPE_SH
:
1011 case LTT_ARCH_TYPE_S390
:
1014 case LTT_ARCH_TYPE_MIPS
:
1017 case LTT_ARCH_TYPE_ARM
:
1020 case LTT_ARCH_TYPE_PPC64
:
1023 case LTT_ARCH_TYPE_X86_64
:
1026 case LTT_ARCH_TYPE_C2
:
1029 case LTT_ARCH_TYPE_POWERPC
:
1033 g_error("Trace from unsupported architecture.");
1035 textlen
+=strlen(text
);
1036 if(textlen
>= PATH_MAX
) goto name_error
;
1037 strcat(desc_file_name
, text
);
1041 textlen
+=strlen(text
);
1042 if(textlen
>= PATH_MAX
) goto name_error
;
1043 strcat(desc_file_name
, text
);
1045 err
= ltt_facility_open(f
, t
, desc_file_name
);
1046 if(err
) goto facility_error
;
1055 static void ltt_fac_ids_destroy(gpointer data
)
1057 GArray
*fac_ids
= (GArray
*)data
;
1059 g_array_free(fac_ids
, TRUE
);
1063 /* Presumes the tracefile is already seeked at the beginning. It makes sense,
1064 * because it must be done just after the opening */
1065 int ltt_process_facility_tracefile(LttTracefile
*tf
)
1074 err
= ltt_tracefile_read_seek(tf
);
1075 if(err
== EPERM
) goto seek_error
;
1076 else if(err
== ERANGE
) break; /* End of tracefile */
1078 err
= ltt_tracefile_read_update_event(tf
);
1079 if(err
) goto update_error
;
1081 /* We are on a facility load/or facility unload/ or heartbeat event */
1083 * * facility 0 is hardcoded : this is the core facility. It will be shown
1084 * in the facility array though, and is shown as "loaded builtin" in the
1086 * It contains event :
1088 * 1 : facility unload
1089 * 2 : state dump facility load
1092 if(tf
->event
.event_id
>= MARKER_CORE_IDS
) {
1093 /* Should only contain core facility */
1094 g_warning("Error in processing facility file %s, "
1095 "should not contain event id %u.", g_quark_to_string(tf
->name
),
1096 tf
->event
.event_id
);
1098 goto event_id_error
;
1102 const char *marker_name
, *format
;
1104 guint8 int_size
, long_size
, pointer_size
, size_t_size
, alignment
;
1107 switch((enum marker_id
)tf
->event
.event_id
) {
1108 case MARKER_ID_SET_MARKER_ID
:
1109 marker_name
= pos
= tf
->event
.data
;
1110 g_debug("Doing MARKER_ID_SET_MARKER_ID of marker %s", marker_name
);
1111 pos
+= strlen(marker_name
) + 1;
1112 //remove genevent compatibility
1113 //pos += ltt_align((size_t)pos, tf->trace->arch_size, tf->alignment);
1114 pos
+= ltt_align((size_t)pos
, sizeof(uint16_t), tf
->alignment
);
1115 id
= ltt_get_uint16(LTT_GET_BO(tf
), pos
);
1116 g_debug("In MARKER_ID_SET_MARKER_ID of marker %s id %hu",
1118 pos
+= sizeof(guint16
);
1119 int_size
= *(guint8
*)pos
;
1120 pos
+= sizeof(guint8
);
1121 long_size
= *(guint8
*)pos
;
1122 pos
+= sizeof(guint8
);
1123 pointer_size
= *(guint8
*)pos
;
1124 pos
+= sizeof(guint8
);
1125 size_t_size
= *(guint8
*)pos
;
1126 pos
+= sizeof(guint8
);
1127 alignment
= *(guint8
*)pos
;
1128 pos
+= sizeof(guint8
);
1129 marker_id_event(tf
->trace
, g_quark_from_string(marker_name
),
1130 id
, int_size
, long_size
,
1131 pointer_size
, size_t_size
, alignment
);
1133 case MARKER_ID_SET_MARKER_FORMAT
:
1134 marker_name
= pos
= tf
->event
.data
;
1135 g_debug("Doing MARKER_ID_SET_MARKER_FORMAT of marker %s",
1137 pos
+= strlen(marker_name
) + 1;
1139 //pos += ltt_align((size_t)pos, tf->trace->arch_size, tf->alignment);
1141 pos
+= strlen(format
) + 1;
1143 //pos += ltt_align((size_t)pos, tf->trace->arch_size, tf->alignment);
1144 marker_format_event(tf
->trace
, g_quark_from_string(marker_name
),
1146 /* get information from dictionnary TODO */
1148 case MARKER_ID_HEARTBEAT_32
:
1149 case MARKER_ID_HEARTBEAT_64
:
1152 g_warning("Error in processing facility file %s, "
1153 "unknown event id %hhu.",
1154 g_quark_to_string(tf
->name
),
1155 tf
->event
.event_id
);
1157 goto event_id_error
;
1163 /* Error handling */
1167 g_warning("An error occured in facility tracefile parsing");
1172 LttTrace
*ltt_trace_open(const gchar
*pathname
)
1174 gchar abs_path
[PATH_MAX
];
1179 struct ltt_block_start_header
*header
;
1181 struct dirent
*entry
;
1182 guint control_found
= 0;
1183 struct stat stat_buf
;
1184 gchar path
[PATH_MAX
];
1186 t
= g_new(LttTrace
, 1);
1187 if(!t
) goto alloc_error
;
1189 get_absolute_pathname(pathname
, abs_path
);
1190 t
->pathname
= g_quark_from_string(abs_path
);
1192 g_datalist_init(&t
->tracefiles
);
1194 /* Test to see if it looks like a trace */
1195 dir
= opendir(abs_path
);
1200 while((entry
= readdir(dir
)) != NULL
) {
1201 strcpy(path
, abs_path
);
1203 strcat(path
, entry
->d_name
);
1204 ret
= stat(path
, &stat_buf
);
1209 if(S_ISDIR(stat_buf
.st_mode
)) {
1210 if(strcmp(entry
->d_name
, "control") == 0) {
1217 if(!control_found
) goto find_error
;
1219 /* Open all the tracefiles */
1220 if(open_tracefiles(t
, abs_path
, "")) {
1221 g_warning("Error opening tracefile %s", abs_path
);
1225 /* Parse each trace control/facilitiesN files : get runtime fac. info */
1226 group
= g_datalist_id_get_data(&t
->tracefiles
, LTT_TRACEFILE_NAME_FACILITIES
);
1228 g_error("Trace %s has no facility tracefile", abs_path
);
1230 goto facilities_error
;
1233 /* Get the trace information for the control/facility 0 tracefile */
1234 g_assert(group
->len
> 0);
1235 tf
= &g_array_index (group
, LttTracefile
, 0);
1236 header
= (struct ltt_block_start_header
*)tf
->buffer
.head
;
1237 g_assert(parse_trace_header(header
->trace
,
1240 t
->num_cpu
= group
->len
;
1242 ret
= allocate_marker_data(t
);
1244 g_error("Error in allocating marker data");
1246 for(i
=0; i
<group
->len
; i
++) {
1247 tf
= &g_array_index (group
, LttTracefile
, i
);
1248 if(ltt_process_facility_tracefile(tf
))
1249 goto facilities_error
;
1254 /* Error handling */
1256 destroy_marker_data(t
);
1258 g_datalist_clear(&t
->tracefiles
);
1266 GQuark
ltt_trace_name(const LttTrace
*t
)
1272 /******************************************************************************
1273 * When we copy a trace, we want all the opening actions to happen again :
1274 * the trace will be reopened and totally independant from the original.
1275 * That's why we call ltt_trace_open.
1276 *****************************************************************************/
1277 LttTrace
*ltt_trace_copy(LttTrace
*self
)
1279 return ltt_trace_open(g_quark_to_string(self
->pathname
));
1282 void ltt_trace_close(LttTrace
*t
)
1284 g_datalist_clear(&t
->tracefiles
);
1289 /*****************************************************************************
1290 *Get the system description of the trace
1291 ****************************************************************************/
1293 LttFacility
*ltt_trace_facility_by_id(LttTrace
*t
, guint8 id
)
1295 g_assert(id
< t
->facilities_by_num
->len
);
1296 return &g_array_index(t
->facilities_by_num
, LttFacility
, id
);
1299 /* ltt_trace_facility_get_by_name
1301 * Returns the GArray of facility indexes. All the fac_ids that matches the
1302 * requested facility name.
1304 * If name is not found, returns NULL.
1306 GArray
*ltt_trace_facility_get_by_name(LttTrace
*t
, GQuark name
)
1308 return g_datalist_id_get_data(&t
->facilities_by_name
, name
);
1312 /*****************************************************************************
1313 * Functions to discover all the event types in the trace
1314 ****************************************************************************/
1317 unsigned ltt_trace_eventtype_number(LttTrace
*t
)
1321 unsigned int num
= t
->facility_number
;
1325 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, i
);
1326 count
+= f
->event_number
;
1333 //use an iteration on all the trace facilities, and inside iteration on all the
1334 //event types in each facilities instead.
1335 LttEventType
*ltt_trace_eventtype_get(LttTrace
*t
, unsigned evId
)
1337 LttEventType
*event_type
;
1340 f
= ltt_trace_facility_by_id(t
,evId
);
1342 if(unlikely(!f
)) event_type
= NULL
;
1343 else event_type
= f
->events
[evId
- f
->base_id
];
1350 /*****************************************************************************
1351 * ltt_trace_find_tracefile
1353 * Find a tracefile by name and index in the group.
1355 * Returns a pointer to the tracefiles, else NULL.
1356 ****************************************************************************/
1358 LttTracefile
*ltt_trace_find_tracefile(LttTrace
*t
, const gchar
*name
)
1363 /*****************************************************************************
1364 * Get the start time and end time of the trace
1365 ****************************************************************************/
1367 void ltt_tracefile_time_span_get(LttTracefile
*tf
,
1368 LttTime
*start
, LttTime
*end
)
1372 err
= map_block(tf
, 0);
1374 g_error("Can not map block");
1375 *start
= ltt_time_infinite
;
1377 *start
= tf
->buffer
.begin
.timestamp
;
1379 err
= map_block(tf
, tf
->num_blocks
- 1); /* Last block */
1381 g_error("Can not map block");
1382 *end
= ltt_time_zero
;
1384 *end
= tf
->buffer
.end
.timestamp
;
1387 struct tracefile_time_span_get_args
{
1393 void group_time_span_get(GQuark name
, gpointer data
, gpointer user_data
)
1395 struct tracefile_time_span_get_args
*args
=
1396 (struct tracefile_time_span_get_args
*)user_data
;
1398 GArray
*group
= (GArray
*)data
;
1404 for(i
=0; i
<group
->len
; i
++) {
1405 tf
= &g_array_index (group
, LttTracefile
, i
);
1406 if(tf
->cpu_online
) {
1407 ltt_tracefile_time_span_get(tf
, &tmp_start
, &tmp_end
);
1408 if(ltt_time_compare(*args
->start
, tmp_start
)>0) *args
->start
= tmp_start
;
1409 if(ltt_time_compare(*args
->end
, tmp_end
)<0) *args
->end
= tmp_end
;
1414 void ltt_trace_time_span_get(LttTrace
*t
, LttTime
*start
, LttTime
*end
)
1416 LttTime min_start
= ltt_time_infinite
;
1417 LttTime max_end
= ltt_time_zero
;
1418 struct tracefile_time_span_get_args args
= { t
, &min_start
, &max_end
};
1420 g_datalist_foreach(&t
->tracefiles
, &group_time_span_get
, &args
);
1422 if(start
!= NULL
) *start
= min_start
;
1423 if(end
!= NULL
) *end
= max_end
;
1428 /*****************************************************************************
1429 *Get the name of a tracefile
1430 ****************************************************************************/
1432 GQuark
ltt_tracefile_name(const LttTracefile
*tf
)
1437 GQuark
ltt_tracefile_long_name(const LttTracefile
*tf
)
1439 return tf
->long_name
;
1444 guint
ltt_tracefile_cpu(LttTracefile
*tf
)
1449 guint
ltt_tracefile_tid(LttTracefile
*tf
)
1454 guint
ltt_tracefile_pgid(LttTracefile
*tf
)
1459 guint64
ltt_tracefile_creation(LttTracefile
*tf
)
1461 return tf
->creation
;
1463 /*****************************************************************************
1464 * Get the number of blocks in the tracefile
1465 ****************************************************************************/
1467 guint
ltt_tracefile_block_number(LttTracefile
*tf
)
1469 return tf
->num_blocks
;
1473 /* Seek to the first event in a tracefile that has a time equal or greater than
1474 * the time passed in parameter.
1476 * If the time parameter is outside the tracefile time span, seek to the first
1477 * event or if after, return ERANGE.
1479 * If the time parameter is before the first event, we have to seek specially to
1482 * If the time is after the end of the trace, return ERANGE.
1484 * Do a binary search to find the right block, then a sequential search in the
1485 * block to find the event.
1487 * In the special case where the time requested fits inside a block that has no
1488 * event corresponding to the requested time, the first event of the next block
1491 * IMPORTANT NOTE : // FIXME everywhere...
1493 * You MUST NOT do a ltt_tracefile_read right after a ltt_tracefile_seek_time :
1494 * you will jump over an event if you do.
1496 * Return value : 0 : no error, the tf->event can be used
1497 * ERANGE : time if after the last event of the trace
1498 * otherwise : this is an error.
1502 int ltt_tracefile_seek_time(LttTracefile
*tf
, LttTime time
)
1506 unsigned int block_num
, high
, low
;
1508 /* seek at the beginning of trace */
1509 err
= map_block(tf
, 0); /* First block */
1511 g_error("Can not map block");
1515 /* If the time is lower or equal the beginning of the trace,
1516 * go to the first event. */
1517 if(ltt_time_compare(time
, tf
->buffer
.begin
.timestamp
) <= 0) {
1518 ret
= ltt_tracefile_read(tf
);
1519 if(ret
== ERANGE
) goto range
;
1520 else if (ret
) goto fail
;
1521 goto found
; /* There is either no event in the trace or the event points
1522 to the first event in the trace */
1525 err
= map_block(tf
, tf
->num_blocks
- 1); /* Last block */
1527 g_error("Can not map block");
1531 /* If the time is after the end of the trace, return ERANGE. */
1532 if(ltt_time_compare(time
, tf
->buffer
.end
.timestamp
) > 0) {
1536 /* Binary search the block */
1537 high
= tf
->num_blocks
- 1;
1541 block_num
= ((high
-low
) / 2) + low
;
1543 err
= map_block(tf
, block_num
);
1545 g_error("Can not map block");
1549 /* We cannot divide anymore : this is what would happen if the time
1550 * requested was exactly between two consecutive buffers'end and start
1551 * timestamps. This is also what would happend if we didn't deal with out
1552 * of span cases prior in this function. */
1553 /* The event is right in the buffer!
1554 * (or in the next buffer first event) */
1556 ret
= ltt_tracefile_read(tf
);
1557 if(ret
== ERANGE
) goto range
; /* ERANGE or EPERM */
1558 else if(ret
) goto fail
;
1560 if(ltt_time_compare(time
, tf
->event
.event_time
) <= 0)
1564 } else if(ltt_time_compare(time
, tf
->buffer
.begin
.timestamp
) < 0) {
1565 /* go to lower part */
1566 high
= block_num
- 1;
1567 } else if(ltt_time_compare(time
, tf
->buffer
.end
.timestamp
) > 0) {
1568 /* go to higher part */
1569 low
= block_num
+ 1;
1570 } else {/* The event is right in the buffer!
1571 (or in the next buffer first event) */
1573 ret
= ltt_tracefile_read(tf
);
1574 if(ret
== ERANGE
) goto range
; /* ERANGE or EPERM */
1575 else if(ret
) goto fail
;
1577 if(ltt_time_compare(time
, tf
->event
.event_time
) <= 0)
1589 /* Error handling */
1591 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1592 g_quark_to_string(tf
->name
));
1597 int ltt_tracefile_seek_position(LttTracefile
*tf
, const LttEventPosition
*ep
) {
1601 if(ep
->tracefile
!= tf
) {
1605 err
= map_block(tf
, ep
->block
);
1607 g_error("Can not map block");
1611 tf
->event
.offset
= ep
->offset
;
1613 /* Put back the event real tsc */
1614 tf
->event
.tsc
= ep
->tsc
;
1615 tf
->buffer
.tsc
= ep
->tsc
;
1617 err
= ltt_tracefile_read_update_event(tf
);
1619 err
= ltt_tracefile_read_op(tf
);
1625 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1626 g_quark_to_string(tf
->name
));
1630 LttTime
ltt_interpolate_time_from_tsc(LttTracefile
*tf
, guint64 tsc
)
1634 if(tsc
> tf
->trace
->start_tsc
) {
1635 time
= ltt_time_from_uint64(
1636 (double)(tsc
- tf
->trace
->start_tsc
)
1637 * (1000000000.0 / tf
->trace
->freq_scale
)
1638 / (double)tf
->trace
->start_freq
);
1639 time
= ltt_time_add(tf
->trace
->start_time_from_tsc
, time
);
1641 time
= ltt_time_from_uint64(
1642 (double)(tf
->trace
->start_tsc
- tsc
)
1643 * (1000000000.0 / tf
->trace
->freq_scale
)
1644 / (double)tf
->trace
->start_freq
);
1645 time
= ltt_time_sub(tf
->trace
->start_time_from_tsc
, time
);
1650 /* Calculate the real event time based on the buffer boundaries */
1651 LttTime
ltt_interpolate_time(LttTracefile
*tf
, LttEvent
*event
)
1653 return ltt_interpolate_time_from_tsc(tf
, tf
->buffer
.tsc
);
1657 /* Get the current event of the tracefile : valid until the next read */
1658 LttEvent
*ltt_tracefile_get_event(LttTracefile
*tf
)
1665 /*****************************************************************************
1667 * ltt_tracefile_read : Read the next event in the tracefile
1672 * Returns 0 if an event can be used in tf->event.
1673 * Returns ERANGE on end of trace. The event in tf->event still can be used
1674 * (if the last block was not empty).
1675 * Returns EPERM on error.
1677 * This function does make the tracefile event structure point to the event
1678 * currently pointed to by the tf->event.
1680 * Note : you must call a ltt_tracefile_seek to the beginning of the trace to
1681 * reinitialize it after an error if you want results to be coherent.
1682 * It would be the case if a end of trace last buffer has no event : the end
1683 * of trace wouldn't be returned, but an error.
1684 * We make the assumption there is at least one event per buffer.
1685 ****************************************************************************/
1687 int ltt_tracefile_read(LttTracefile
*tf
)
1691 err
= ltt_tracefile_read_seek(tf
);
1693 err
= ltt_tracefile_read_update_event(tf
);
1695 err
= ltt_tracefile_read_op(tf
);
1701 int ltt_tracefile_read_seek(LttTracefile
*tf
)
1705 /* Get next buffer until we finally have an event, or end of trace */
1707 err
= ltt_seek_next_event(tf
);
1708 if(unlikely(err
== ENOPROTOOPT
)) {
1712 /* Are we at the end of the buffer ? */
1714 if(unlikely(tf
->buffer
.index
== tf
->num_blocks
-1)){ /* end of trace ? */
1717 /* get next block */
1718 err
= map_block(tf
, tf
->buffer
.index
+ 1);
1720 g_error("Can not map block");
1724 } else break; /* We found an event ! */
1731 /* do specific operation on events */
1732 int ltt_tracefile_read_op(LttTracefile
*tf
)
1738 /* do event specific operation */
1740 /* do something if its an heartbeat event : increment the heartbeat count */
1741 //if(event->facility_id == LTT_FACILITY_CORE)
1742 // if(event->event_id == LTT_EVENT_HEARTBEAT)
1743 // tf->cur_heart_beat_number++;
1748 static void print_debug_event_header(LttEvent
*ev
, void *start_pos
, void *end_pos
)
1750 unsigned int offset
= 0;
1753 g_printf("Event header (tracefile %s offset %llx):\n",
1754 g_quark_to_string(ev
->tracefile
->long_name
),
1755 ((uint64_t)ev
->tracefile
->buffer
.index
* ev
->tracefile
->buf_size
)
1756 + (long)start_pos
- (long)ev
->tracefile
->buffer
.head
);
1758 while (offset
< (long)end_pos
- (long)start_pos
) {
1759 g_printf("%8lx", (long)start_pos
- (long)ev
->tracefile
->buffer
.head
+ offset
);
1762 for (i
= 0; i
< 4 ; i
++) {
1763 for (j
= 0; j
< 4; j
++) {
1764 if (offset
+ ((i
* 4) + j
) <
1765 (long)end_pos
- (long)start_pos
)
1767 ((char*)start_pos
)[offset
+ ((i
* 4) + j
)]);
1781 /* same as ltt_tracefile_read, but does not seek to the next event nor call
1782 * event specific operation. */
1783 int ltt_tracefile_read_update_event(LttTracefile
*tf
)
1790 pos
= tf
->buffer
.head
+ event
->offset
;
1792 /* Read event header */
1794 /* Align the head */
1796 pos
+= ltt_align((size_t)pos
, tf
->trace
->arch_size
, tf
->alignment
);
1798 g_assert(tf
->has_heartbeat
);
1799 pos
+= ltt_align((size_t)pos
, sizeof(uint32_t), tf
->alignment
);
1803 if(tf
->has_heartbeat
) {
1804 event
->timestamp
= ltt_get_uint32(LTT_GET_BO(tf
),
1807 /* 32 bits -> 64 bits tsc */
1808 /* note : still works for seek and non seek cases. */
1809 if(event
->timestamp
< (0xFFFFFFFFULL
&tf
->buffer
.tsc
)) {
1810 tf
->buffer
.tsc
= ((tf
->buffer
.tsc
&0xFFFFFFFF00000000ULL
)
1812 | (guint64
)event
->timestamp
;
1813 event
->tsc
= tf
->buffer
.tsc
;
1816 tf
->buffer
.tsc
= (tf
->buffer
.tsc
&0xFFFFFFFF00000000ULL
)
1817 | (guint64
)event
->timestamp
;
1818 event
->tsc
= tf
->buffer
.tsc
;
1819 event
->compact_data
= 0;
1822 /* Compact header */
1823 /* We keep the LSB of the previous timestamp, to make sure
1824 * we never go back */
1825 event
->event_id
= event
->timestamp
>> tf
->tscbits
;
1826 event
->event_id
= event
->event_id
& ((1 << tf
->compact_event_bits
) - 1);
1827 event
->compact_data
= event
->timestamp
>>
1828 (tf
->compact_event_bits
+ tf
->tscbits
);
1829 //printf("tsc bits %u, ev bits %u init data %u\n",
1830 // tf->tscbits, tf->trace->compact_event_bits, event->compact_data);
1831 /* Put the compact data back in original endianness */
1832 event
->compact_data
= ltt_get_uint32(LTT_GET_BO(tf
), &event
->compact_data
);
1833 event
->event_size
= 0xFFFF;
1834 //printf("Found compact event %d\n", event->event_id);
1835 //printf("Compact data %d\n", event->compact_data);
1836 event
->timestamp
= event
->timestamp
<< tf
->tsc_lsb_truncate
;
1837 event
->timestamp
= event
->timestamp
& tf
->tsc_mask
;
1838 //printf("timestamp 0x%lX\n", event->timestamp);
1839 //printf("mask 0x%llX\n", tf->tsc_mask);
1840 //printf("mask_next 0x%llX\n", tf->tsc_mask_next_bit);
1841 //printf("previous tsc 0x%llX\n", tf->buffer.tsc);
1842 //printf("previous tsc&mask 0x%llX\n", tf->tsc_mask&tf->buffer.tsc);
1843 //printf("previous tsc&(~mask) 0x%llX\n", tf->buffer.tsc&(~tf->tsc_mask));
1844 if(event
->timestamp
< (tf
->tsc_mask
&tf
->buffer
.tsc
)) {
1846 tf
->buffer
.tsc
= ((tf
->buffer
.tsc
&(~tf
->tsc_mask
))
1847 + tf
->tsc_mask_next_bit
)
1848 | (guint64
)event
->timestamp
;
1849 event
->tsc
= tf
->buffer
.tsc
;
1851 //printf("no wrap\n");
1853 tf
->buffer
.tsc
= (tf
->buffer
.tsc
&(~tf
->tsc_mask
))
1854 | (guint64
)event
->timestamp
;
1855 event
->tsc
= tf
->buffer
.tsc
;
1857 //printf("current tsc 0x%llX\n", tf->buffer.tsc);
1859 pos
+= sizeof(guint32
);
1861 event
->tsc
= ltt_get_uint64(LTT_GET_BO(tf
), pos
);
1862 tf
->buffer
.tsc
= event
->tsc
;
1863 event
->compact_data
= 0;
1864 pos
+= sizeof(guint64
);
1866 event
->event_time
= ltt_interpolate_time(tf
, event
);
1869 event
->event_id
= *(guint16
*)pos
;
1870 pos
+= sizeof(guint16
);
1872 event
->event_size
= ltt_get_uint16(LTT_GET_BO(tf
), pos
);
1873 pos
+= sizeof(guint16
);
1876 event
->event_size
= 0xFFFF;
1880 print_debug_event_header(event
, pos_aligned
, pos
);
1882 /* Align the head */
1884 pos
+= ltt_align((size_t)pos
, tf
->trace
->arch_size
, tf
->alignment
);
1888 /* get the data size and update the event fields with the current
1889 * information. Also update the time if a heartbeat_full event is found. */
1890 ltt_update_event_size(tf
);
1896 /****************************************************************************
1898 * map_block : map a block from the file
1900 * lttdes : ltt trace file
1901 * whichBlock : the block which will be read
1904 * EINVAL : lseek fail
1905 * EIO : can not read from the file
1906 ****************************************************************************/
1908 gint
map_block(LttTracefile
* tf
, guint block_num
)
1910 int page_size
= getpagesize();
1911 struct ltt_block_start_header
*header
;
1913 g_assert(block_num
< tf
->num_blocks
);
1915 if(tf
->buffer
.head
!= NULL
) {
1916 if(munmap(tf
->buffer
.head
, PAGE_ALIGN(tf
->buf_size
))) {
1917 g_warning("unmap size : %u\n",
1918 PAGE_ALIGN(tf
->buf_size
));
1919 perror("munmap error");
1925 /* Multiple of pages aligned head */
1926 tf
->buffer
.head
= mmap(0,
1927 PAGE_ALIGN(tf
->buf_size
),
1928 PROT_READ
, MAP_PRIVATE
, tf
->fd
,
1929 PAGE_ALIGN((off_t
)tf
->buf_size
* (off_t
)block_num
));
1931 if(tf
->buffer
.head
== MAP_FAILED
) {
1932 perror("Error in allocating memory for buffer of tracefile");
1936 g_assert( ( (guint
)tf
->buffer
.head
&(8-1) ) == 0); // make sure it's aligned.
1939 tf
->buffer
.index
= block_num
;
1941 header
= (struct ltt_block_start_header
*)tf
->buffer
.head
;
1944 tf
->buffer
.begin
.timestamp
= ltt_time_add(
1945 ltt_time_from_uint64(
1946 ltt_get_uint64(LTT_GET_BO(tf
),
1947 &header
->begin
.timestamp
)
1948 - tf
->trace
->start_monotonic
),
1949 tf
->trace
->start_time
);
1951 //g_debug("block %u begin : %lu.%lu", block_num,
1952 // tf->buffer.begin.timestamp.tv_sec, tf->buffer.begin.timestamp.tv_nsec);
1953 tf
->buffer
.begin
.cycle_count
= ltt_get_uint64(LTT_GET_BO(tf
),
1954 &header
->begin
.cycle_count
);
1955 tf
->buffer
.begin
.freq
= ltt_get_uint64(LTT_GET_BO(tf
),
1956 &header
->begin
.freq
);
1957 if(tf
->buffer
.begin
.freq
== 0)
1958 tf
->buffer
.begin
.freq
= tf
->trace
->start_freq
;
1960 tf
->buffer
.begin
.timestamp
= ltt_interpolate_time_from_tsc(tf
,
1961 tf
->buffer
.begin
.cycle_count
);
1964 ltt_time_from_uint64(
1965 (double)(tf
->buffer
.begin
.cycle_count
1966 - tf
->trace
->start_tsc
) * 1000000.0
1967 / (double)tf
->trace
->start_freq
),
1968 tf
->trace
->start_time_from_tsc
);
1972 tf
->buffer
.end
.timestamp
= ltt_time_add(
1973 ltt_time_from_uint64(
1974 ltt_get_uint64(LTT_GET_BO(tf
),
1975 &header
->end
.timestamp
)
1976 - tf
->trace
->start_monotonic
),
1977 tf
->trace
->start_time
);
1979 //g_debug("block %u end : %lu.%lu", block_num,
1980 // tf->buffer.end.timestamp.tv_sec, tf->buffer.end.timestamp.tv_nsec);
1981 tf
->buffer
.end
.cycle_count
= ltt_get_uint64(LTT_GET_BO(tf
),
1982 &header
->end
.cycle_count
);
1983 tf
->buffer
.end
.freq
= ltt_get_uint64(LTT_GET_BO(tf
),
1985 if(tf
->buffer
.end
.freq
== 0)
1986 tf
->buffer
.end
.freq
= tf
->trace
->start_freq
;
1988 tf
->buffer
.lost_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1989 &header
->lost_size
);
1990 tf
->buffer
.end
.timestamp
= ltt_interpolate_time_from_tsc(tf
,
1991 tf
->buffer
.end
.cycle_count
);
1994 ltt_time_from_uint64(
1995 (double)(tf
->buffer
.end
.cycle_count
1996 - tf
->trace
->start_tsc
) * 1000000.0
1997 / (double)tf
->trace
->start_freq
),
1998 tf
->trace
->start_time_from_tsc
);
2000 tf
->buffer
.tsc
= tf
->buffer
.begin
.cycle_count
;
2001 tf
->event
.tsc
= tf
->buffer
.tsc
;
2002 tf
->buffer
.freq
= tf
->buffer
.begin
.freq
;
2005 * eventually support variable buffer size : will need a partial pre-read of
2006 * the headers to create an index when we open the trace... eventually. */
2007 g_assert(tf
->buf_size
== ltt_get_uint32(LTT_GET_BO(tf
),
2008 &header
->buf_size
));
2010 /* Now that the buffer is mapped, calculate the time interpolation for the
2013 // tf->buffer.nsecs_per_cycle = calc_nsecs_per_cycle(tf);
2014 //tf->buffer.cyc2ns_scale = calc_nsecs_per_cycle(tf);
2016 /* Make the current event point to the beginning of the buffer :
2017 * it means that the event read must get the first event. */
2018 tf
->event
.tracefile
= tf
;
2019 tf
->event
.block
= block_num
;
2020 tf
->event
.offset
= 0;
2029 static void print_debug_event_data(LttEvent
*ev
)
2031 unsigned int offset
= 0;
2034 if (!max(ev
->event_size
, ev
->data_size
))
2037 g_printf("Event data (tracefile %s offset %llx):\n",
2038 g_quark_to_string(ev
->tracefile
->long_name
),
2039 ((uint64_t)ev
->tracefile
->buffer
.index
* ev
->tracefile
->buf_size
)
2040 + (long)ev
->data
- (long)ev
->tracefile
->buffer
.head
);
2042 while (offset
< max(ev
->event_size
, ev
->data_size
)) {
2043 g_printf("%8lx", (long)ev
->data
+ offset
2044 - (long)ev
->tracefile
->buffer
.head
);
2047 for (i
= 0; i
< 4 ; i
++) {
2048 for (j
= 0; j
< 4; j
++) {
2049 if (offset
+ ((i
* 4) + j
) < max(ev
->event_size
, ev
->data_size
))
2050 g_printf("%02hhX", ((char*)ev
->data
)[offset
+ ((i
* 4) + j
)]);
2061 for (i
= 0; i
< 4; i
++) {
2062 for (j
= 0; j
< 4; j
++) {
2063 if (offset
+ ((i
* 4) + j
) < max(ev
->event_size
, ev
->data_size
)) {
2064 if (isprint(((char*)ev
->data
)[offset
+ ((i
* 4) + j
)]))
2065 g_printf("%c", ((char*)ev
->data
)[offset
+ ((i
* 4) + j
)]);
2077 /* It will update the fields offsets too */
2078 void ltt_update_event_size(LttTracefile
*tf
)
2082 struct marker_info
*info
;
2084 switch((enum marker_id
)tf
->event
.event_id
) {
2085 case MARKER_ID_SET_MARKER_ID
:
2086 size
= strlen((char*)tf
->event
.data
) + 1;
2087 g_debug("marker %s id set", (char*)tf
->event
.data
);
2088 size
+= ltt_align(size
, sizeof(guint16
), tf
->alignment
);
2089 size
+= sizeof(guint16
);
2090 size
+= sizeof(guint8
);
2091 size
+= sizeof(guint8
);
2092 size
+= sizeof(guint8
);
2093 size
+= sizeof(guint8
);
2094 size
+= sizeof(guint8
);
2096 case MARKER_ID_SET_MARKER_FORMAT
:
2097 g_debug("marker %s format set", (char*)tf
->event
.data
);
2098 size
= strlen((char*)tf
->event
.data
) + 1;
2099 size
+= strlen((char*)tf
->event
.data
+ size
) + 1;
2101 case MARKER_ID_HEARTBEAT_32
:
2102 g_debug("Update Event heartbeat 32 bits");
2104 case MARKER_ID_HEARTBEAT_64
:
2105 g_debug("Update Event heartbeat 64 bits");
2106 tscdata
= (char*)(tf
->event
.data
);
2107 tf
->event
.tsc
= ltt_get_uint64(LTT_GET_BO(tf
), tscdata
);
2108 tf
->buffer
.tsc
= tf
->event
.tsc
;
2109 tf
->event
.event_time
= ltt_interpolate_time(tf
, &tf
->event
);
2110 size
= ltt_align(size
, sizeof(guint64
), tf
->alignment
);
2111 size
+= sizeof(guint64
);
2115 info
= marker_get_info_from_id(tf
->trace
, tf
->event
.event_id
);
2117 if (tf
->event
.event_id
>= MARKER_CORE_IDS
)
2118 g_assert(info
!= NULL
);
2120 /* Do not update field offsets of core markers when initially reading the
2121 * facility tracefile when the infos about these markers do not exist yet.
2123 if (likely(info
&& info
->fields
)) {
2124 if (info
->size
!= -1)
2127 size
= marker_update_fields_offsets(marker_get_info_from_id(tf
->trace
,
2128 tf
->event
.event_id
), tf
->event
.data
);
2131 tf
->event
.data_size
= size
;
2133 /* Check consistency between kernel and LTTV structure sizes */
2134 if(tf
->event
.event_size
== 0xFFFF) {
2135 /* Event size too big to fit in the event size field */
2136 tf
->event
.event_size
= tf
->event
.data_size
;
2140 print_debug_event_data(&tf
->event
);
2142 /* Having a marker load or marker format event out of the facilities
2143 * tracefiles is a serious bug. */
2144 switch((enum marker_id
)tf
->event
.event_id
) {
2145 case MARKER_ID_SET_MARKER_ID
:
2146 case MARKER_ID_SET_MARKER_FORMAT
:
2147 if (tf
->name
!= g_quark_from_string("/control/facilities"))
2148 g_error("Trace inconsistency : metadata event found in data "
2149 "tracefile %s", g_quark_to_string(tf
->long_name
));
2152 if (tf
->event
.data_size
!= tf
->event
.event_size
) {
2153 struct marker_info
*info
= marker_get_info_from_id(tf
->trace
,
2154 tf
->event
.event_id
);
2155 g_error("Kernel/LTTV event size differs for event %s: kernel %u, LTTV %u",
2156 g_quark_to_string(info
->name
),
2157 tf
->event
.event_size
, tf
->event
.data_size
);
2162 LttEventType
*event_type
=
2163 ltt_facility_eventtype_get(f
, tf
->event
.event_id
);
2166 g_warning("Unknown event id %hhu in facility %s in tracefile %s",
2168 g_quark_to_string(f
->name
),
2169 g_quark_to_string(tf
->name
));
2170 goto event_type_error
;
2173 /* Compute the dynamic offsets */
2174 compute_offsets(tf
, f
, event_type
, &size
, tf
->event
.data
);
2176 //g_debug("Event root field : f.e %hhu.%hhu size %zd",
2177 // tf->event.facility_id,
2178 // tf->event.event_id, size);
2181 tf
->event
.data_size
= size
;
2183 /* Check consistency between kernel and LTTV structure sizes */
2184 if(tf
->event
.event_size
== 0xFFFF) {
2185 /* Event size too big to fit in the event size field */
2186 tf
->event
.event_size
= tf
->event
.data_size
;
2188 if (tf
->event
.data_size
!= tf
->event
.event_size
) {
2189 g_error("Kernel/LTTV event size differs for event %s.%s: kernel %u, LTTV %u",
2190 g_quark_to_string(f
->name
), g_quark_to_string(event_type
->name
),
2191 tf
->event
.event_size
, tf
->event
.data_size
);
2194 //g_assert(tf->event.data_size == tf->event.event_size);
2200 if(tf
->event
.event_size
== 0xFFFF) {
2201 g_error("Cannot jump over an unknown event bigger than 0xFFFE bytes");
2203 /* The facility is unknown : use the kernel information about this event
2204 * to jump over it. */
2205 tf
->event
.data_size
= tf
->event
.event_size
;
2210 /* Take the tf current event offset and use the event facility id and event id
2211 * to figure out where is the next event offset.
2213 * This is an internal function not aiming at being used elsewhere : it will
2214 * not jump over the current block limits. Please consider using
2215 * ltt_tracefile_read to do this.
2217 * Returns 0 on success
2218 * ERANGE if we are at the end of the buffer.
2219 * ENOPROTOOPT if an error occured when getting the current event size.
2221 int ltt_seek_next_event(LttTracefile
*tf
)
2226 /* seek over the buffer header if we are at the buffer start */
2227 if(tf
->event
.offset
== 0) {
2228 tf
->event
.offset
+= tf
->buffer_header_size
;
2230 if(tf
->event
.offset
== tf
->buf_size
- tf
->buffer
.lost_size
) {
2237 pos
= tf
->event
.data
;
2239 if(tf
->event
.data_size
< 0) goto error
;
2241 pos
+= (size_t)tf
->event
.data_size
;
2243 tf
->event
.offset
= pos
- tf
->buffer
.head
;
2245 if(tf
->event
.offset
== tf
->buf_size
- tf
->buffer
.lost_size
) {
2249 g_assert(tf
->event
.offset
< tf
->buf_size
- tf
->buffer
.lost_size
);
2255 g_error("Error in ltt_seek_next_event for tracefile %s",
2256 g_quark_to_string(tf
->name
));
2261 /*****************************************************************************
2263 * calc_nsecs_per_cycle : calculate nsecs per cycle for current block
2265 * 1.0 / (freq(khz) *1000) * 1000000000
2268 ****************************************************************************/
2269 /* from timer_tsc.c */
2270 #define CYC2NS_SCALE_FACTOR 10
2271 static guint32
calc_nsecs_per_cycle(LttTracefile
* tf
)
2273 //return 1e6 / (double)tf->buffer.freq;
2274 guint32 cpu_mhz
= tf
->buffer
.freq
/ 1000;
2275 guint32 cyc2ns_scale
= (1000 << CYC2NS_SCALE_FACTOR
)/cpu_mhz
;
2277 return cyc2ns_scale
;
2278 // return 1e6 / (double)tf->buffer.freq;
2281 static guint64
cycles_2_ns(LttTracefile
*tf
, guint64 cycles
)
2283 return (cycles
* tf
->buffer
.cyc2ns_scale
) >> CYC2NS_SCALE_FACTOR
;
2288 void setFieldsOffset(LttTracefile
*tf
, LttEventType
*evT
,void *evD
)
2290 LttField
* rootFld
= evT
->root_field
;
2291 // rootFld->base_address = evD;
2294 rootFld
->field_size
= getFieldtypeSize(tf
, evT
->facility
,
2295 evT
, 0,0,rootFld
, evD
);
2299 /*****************************************************************************
2301 * set_fields_offsets : set the precomputable offset of the fields
2303 * tracefile : opened trace file
2304 * event_type : the event type
2305 ****************************************************************************/
2307 void set_fields_offsets(LttTracefile
*tf
, LttEventType
*event_type
)
2309 LttField
*field
= event_type
->root_field
;
2310 enum field_status fixed_root
= FIELD_FIXED
, fixed_parent
= FIELD_FIXED
;
2313 preset_field_type_size(tf
, event_type
, 0, 0,
2314 &fixed_root
, &fixed_parent
,
2321 /*****************************************************************************
2323 * get_alignment : Get the alignment needed for a field.
2327 * returns : The size on which it must be aligned.
2329 ****************************************************************************/
2331 off_t
get_alignment(LttField
*field
)
2333 LttType
*type
= &field
->field_type
;
2335 switch(type
->type_class
) {
2337 case LTT_UINT_FIXED
:
2352 /* Align offset on type size */
2353 g_assert(field
->field_size
!= 0);
2354 return field
->field_size
;
2360 g_assert(type
->fields
->len
== 1);
2362 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
2363 return get_alignment(child
);
2367 g_assert(type
->fields
->len
== 2);
2370 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
2372 localign
= max(localign
, get_alignment(child
));
2374 child
= &g_array_index(type
->fields
, LttField
, 1);
2375 localign
= max(localign
, get_alignment(child
));
2386 for(i
=0; i
<type
->fields
->len
; i
++) {
2387 LttField
*child
= &g_array_index(type
->fields
, LttField
, i
);
2388 localign
= max(localign
, get_alignment(child
));
2395 g_error("get_alignment : unknown type");
2402 /*****************************************************************************
2404 * field_compute_static_size : Determine the size of fields known by their
2405 * sole definition. Unions, arrays and struct sizes might be known, but
2406 * the parser does not give that information.
2411 ****************************************************************************/
2413 void field_compute_static_size(LttFacility
*fac
, LttField
*field
)
2415 LttType
*type
= &field
->field_type
;
2417 switch(type
->type_class
) {
2419 case LTT_UINT_FIXED
:
2438 /* note this : array type size is the number of elements in the array,
2439 * while array field size of the length of the array in bytes */
2440 g_assert(type
->fields
->len
== 1);
2442 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
2443 field_compute_static_size(fac
, child
);
2445 if(child
->field_size
!= 0) {
2446 field
->field_size
= type
->size
* child
->field_size
;
2447 field
->dynamic_offsets
= g_array_sized_new(FALSE
, TRUE
,
2448 sizeof(off_t
), type
->size
);
2450 field
->field_size
= 0;
2455 g_assert(type
->fields
->len
== 2);
2457 off_t local_offset
= 0;
2458 LttField
*child
= &g_array_index(type
->fields
, LttField
, 1);
2459 field_compute_static_size(fac
, child
);
2460 field
->field_size
= 0;
2462 if(child
->field_size
!= 0) {
2463 field
->dynamic_offsets
= g_array_sized_new(FALSE
, TRUE
,
2464 sizeof(off_t
), SEQUENCE_AVG_ELEMENTS
);
2472 for(i
=0;i
<type
->fields
->len
;i
++) {
2473 LttField
*child
= &g_array_index(type
->fields
, LttField
, i
);
2474 field_compute_static_size(fac
, child
);
2475 if(child
->field_size
!= 0) {
2476 type
->size
+= ltt_align(type
->size
, get_alignment(child
),
2478 type
->size
+= child
->field_size
;
2480 /* As soon as we find a child with variable size, we have
2481 * a variable size */
2486 field
->field_size
= type
->size
;
2490 g_error("field_static_size : unknown type");
2497 /*****************************************************************************
2499 * precompute_fields_offsets : set the precomputable offset of the fields
2503 * offset : pointer to the current offset, must be incremented
2505 * return : 1 : found a variable length field, stop the processing.
2507 ****************************************************************************/
2510 gint
precompute_fields_offsets(LttFacility
*fac
, LttField
*field
, off_t
*offset
, gint is_compact
)
2512 LttType
*type
= &field
->field_type
;
2514 if(unlikely(is_compact
)) {
2515 g_assert(field
->field_size
!= 0);
2516 /* FIXME THIS IS A HUUUUUGE hack :
2517 * offset is between the compact_data field in struct LttEvent
2518 * and the address of the field root in the memory map.
2519 * ark. Both will stay at the same addresses while the event
2520 * is readable, so it's ok.
2522 field
->offset_root
= 0;
2523 field
->fixed_root
= FIELD_FIXED
;
2527 switch(type
->type_class
) {
2529 case LTT_UINT_FIXED
:
2544 g_assert(field
->field_size
!= 0);
2545 /* Align offset on type size */
2546 *offset
+= ltt_align(*offset
, get_alignment(field
),
2548 /* remember offset */
2549 field
->offset_root
= *offset
;
2550 field
->fixed_root
= FIELD_FIXED
;
2551 /* Increment offset */
2552 *offset
+= field
->field_size
;
2556 field
->offset_root
= *offset
;
2557 field
->fixed_root
= FIELD_FIXED
;
2561 g_assert(type
->fields
->len
== 1);
2563 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
2565 *offset
+= ltt_align(*offset
, get_alignment(field
),
2568 /* remember offset */
2569 field
->offset_root
= *offset
;
2570 field
->array_offset
= *offset
;
2571 field
->fixed_root
= FIELD_FIXED
;
2573 /* Let the child be variable */
2574 //precompute_fields_offsets(tf, child, offset);
2576 if(field
->field_size
!= 0) {
2577 /* Increment offset */
2578 /* field_size is the array size in bytes */
2579 *offset
+= field
->field_size
;
2587 g_assert(type
->fields
->len
== 2);
2592 *offset
+= ltt_align(*offset
, get_alignment(field
),
2595 /* remember offset */
2596 field
->offset_root
= *offset
;
2597 field
->fixed_root
= FIELD_FIXED
;
2599 child
= &g_array_index(type
->fields
, LttField
, 0);
2600 ret
= precompute_fields_offsets(fac
, child
, offset
, is_compact
);
2601 g_assert(ret
== 0); /* Seq len cannot have variable len */
2603 child
= &g_array_index(type
->fields
, LttField
, 1);
2604 *offset
+= ltt_align(*offset
, get_alignment(child
),
2606 field
->array_offset
= *offset
;
2607 /* Let the child be variable. */
2608 //ret = precompute_fields_offsets(fac, child, offset);
2610 /* Cannot precompute fields offsets of sequence members, and has
2611 * variable length. */
2621 *offset
+= ltt_align(*offset
, get_alignment(field
),
2623 /* remember offset */
2624 field
->offset_root
= *offset
;
2625 field
->fixed_root
= FIELD_FIXED
;
2627 for(i
=0; i
< type
->fields
->len
; i
++) {
2628 child
= &g_array_index(type
->fields
, LttField
, i
);
2629 ret
= precompute_fields_offsets(fac
, child
, offset
, is_compact
);
2642 *offset
+= ltt_align(*offset
, get_alignment(field
),
2644 /* remember offset */
2645 field
->offset_root
= *offset
;
2646 field
->fixed_root
= FIELD_FIXED
;
2648 for(i
=0; i
< type
->fields
->len
; i
++) {
2649 *offset
= field
->offset_root
;
2650 child
= &g_array_index(type
->fields
, LttField
, i
);
2651 ret
= precompute_fields_offsets(fac
, child
, offset
, is_compact
);
2655 *offset
= field
->offset_root
+ field
->field_size
;
2662 g_error("precompute_fields_offsets : unknown type");
2671 /*****************************************************************************
2673 * precompute_offsets : set the precomputable offset of an event type
2676 * event : event type
2678 ****************************************************************************/
2679 void precompute_offsets(LttFacility
*fac
, LttEventType
*event
)
2685 /* First, compute the size of fixed size fields. Will determine size for
2686 * arrays, struct and unions, which is not done by the parser */
2687 for(i
=0; i
<event
->fields
->len
; i
++) {
2688 LttField
*field
= &g_array_index(event
->fields
, LttField
, i
);
2689 field_compute_static_size(fac
, field
);
2692 /* Precompute all known offsets */
2693 for(i
=0; i
<event
->fields
->len
; i
++) {
2694 LttField
*field
= &g_array_index(event
->fields
, LttField
, i
);
2695 if(event
->has_compact_data
&& i
== 0)
2696 ret
= precompute_fields_offsets(fac
, field
, &offset
, 1);
2698 ret
= precompute_fields_offsets(fac
, field
, &offset
, 0);
2706 /*****************************************************************************
2708 * preset_field_type_size : set the fixed sizes of the field type
2711 * event_type : event type
2712 * offset_root : offset from the root
2713 * offset_parent : offset from the parent
2714 * fixed_root : Do we know a fixed offset to the root ?
2715 * fixed_parent : Do we know a fixed offset to the parent ?
2717 ****************************************************************************/
2721 // preset the fixed size offsets. Calculate them just like genevent-new : an
2722 // increment of a *to value that represents the offset from the start of the
2724 // The preset information is : offsets up to (and including) the first element
2725 // of variable size. All subsequent fields must be flagged "VARIABLE OFFSET".
2727 void preset_field_type_size(LttTracefile
*tf
, LttEventType
*event_type
,
2728 off_t offset_root
, off_t offset_parent
,
2729 enum field_status
*fixed_root
, enum field_status
*fixed_parent
,
2732 enum field_status local_fixed_root
, local_fixed_parent
;
2736 g_assert(field
->fixed_root
== FIELD_UNKNOWN
);
2737 g_assert(field
->fixed_parent
== FIELD_UNKNOWN
);
2738 g_assert(field
->fixed_size
== FIELD_UNKNOWN
);
2740 type
= field
->field_type
;
2742 field
->fixed_root
= *fixed_root
;
2743 if(field
->fixed_root
== FIELD_FIXED
)
2744 field
->offset_root
= offset_root
;
2746 field
->offset_root
= 0;
2748 field
->fixed_parent
= *fixed_parent
;
2749 if(field
->fixed_parent
== FIELD_FIXED
)
2750 field
->offset_parent
= offset_parent
;
2752 field
->offset_parent
= 0;
2754 size_t current_root_offset
;
2755 size_t current_offset
;
2756 enum field_status current_child_status
, final_child_status
;
2759 switch(type
->type_class
) {
2761 case LTT_UINT_FIXED
:
2770 field
->field_size
= ltt_type_size(tf
->trace
, type
);
2771 field
->fixed_size
= FIELD_FIXED
;
2774 field
->field_size
= (off_t
)event_type
->facility
->pointer_size
;
2775 field
->fixed_size
= FIELD_FIXED
;
2779 field
->field_size
= (off_t
)event_type
->facility
->long_size
;
2780 field
->fixed_size
= FIELD_FIXED
;
2785 field
->field_size
= (off_t
)event_type
->facility
->size_t_size
;
2786 field
->fixed_size
= FIELD_FIXED
;
2789 local_fixed_root
= FIELD_VARIABLE
;
2790 local_fixed_parent
= FIELD_VARIABLE
;
2791 preset_field_type_size(tf
, event_type
,
2793 &local_fixed_root
, &local_fixed_parent
,
2795 field
->fixed_size
= FIELD_VARIABLE
;
2796 field
->field_size
= 0;
2797 *fixed_root
= FIELD_VARIABLE
;
2798 *fixed_parent
= FIELD_VARIABLE
;
2801 field
->fixed_size
= FIELD_VARIABLE
;
2802 field
->field_size
= 0;
2803 *fixed_root
= FIELD_VARIABLE
;
2804 *fixed_parent
= FIELD_VARIABLE
;
2807 local_fixed_root
= FIELD_VARIABLE
;
2808 local_fixed_parent
= FIELD_VARIABLE
;
2809 preset_field_type_size(tf
, event_type
,
2811 &local_fixed_root
, &local_fixed_parent
,
2813 field
->fixed_size
= field
->child
[0]->fixed_size
;
2814 if(field
->fixed_size
== FIELD_FIXED
) {
2815 field
->field_size
= type
->element_number
* field
->child
[0]->field_size
;
2817 field
->field_size
= 0;
2818 *fixed_root
= FIELD_VARIABLE
;
2819 *fixed_parent
= FIELD_VARIABLE
;
2823 current_root_offset
= field
->offset_root
;
2825 current_child_status
= FIELD_FIXED
;
2826 for(i
=0;i
<type
->element_number
;i
++) {
2827 preset_field_type_size(tf
, event_type
,
2828 current_root_offset
, current_offset
,
2829 fixed_root
, ¤t_child_status
,
2831 if(current_child_status
== FIELD_FIXED
) {
2832 current_root_offset
+= field
->child
[i
]->field_size
;
2833 current_offset
+= field
->child
[i
]->field_size
;
2835 current_root_offset
= 0;
2839 if(current_child_status
!= FIELD_FIXED
) {
2840 *fixed_parent
= current_child_status
;
2841 field
->field_size
= 0;
2842 field
->fixed_size
= current_child_status
;
2844 field
->field_size
= current_offset
;
2845 field
->fixed_size
= FIELD_FIXED
;
2849 current_root_offset
= field
->offset_root
;
2852 final_child_status
= FIELD_FIXED
;
2853 for(i
=0;i
<type
->element_number
;i
++) {
2854 enum field_status current_root_child_status
= FIELD_FIXED
;
2855 enum field_status current_child_status
= FIELD_FIXED
;
2856 preset_field_type_size(tf
, event_type
,
2857 current_root_offset
, current_offset
,
2858 ¤t_root_child_status
, ¤t_child_status
,
2860 if(current_child_status
!= FIELD_FIXED
)
2861 final_child_status
= current_child_status
;
2863 max_size
= max(max_size
, field
->child
[i
]->field_size
);
2865 if(final_child_status
!= FIELD_FIXED
) {
2866 g_error("LTTV does not support variable size fields in unions.");
2867 /* This will stop the application. */
2868 *fixed_root
= final_child_status
;
2869 *fixed_parent
= final_child_status
;
2870 field
->field_size
= 0;
2871 field
->fixed_size
= current_child_status
;
2873 field
->field_size
= max_size
;
2874 field
->fixed_size
= FIELD_FIXED
;
2878 g_error("unexpected type NONE");
2885 /*****************************************************************************
2887 * check_fields_compatibility : Check for compatibility between two fields :
2888 * do they use the same inner structure ?
2890 * event_type1 : event type
2891 * event_type2 : event type
2894 *Returns : 0 if identical
2896 ****************************************************************************/
2897 // this function checks for equality of field types. Therefore, it does not use
2898 // per se offsets. For instance, an aligned version of a structure is
2899 // compatible with an unaligned version of the same structure.
2901 gint
check_fields_compatibility(LttEventType
*event_type1
,
2902 LttEventType
*event_type2
,
2903 LttField
*field1
, LttField
*field2
)
2905 guint different
= 0;
2909 if(field1
== NULL
) {
2910 if(field2
== NULL
) goto end
;
2915 } else if(field2
== NULL
) {
2920 type1
= &field1
->field_type
;
2921 type2
= &field2
->field_type
;
2923 if(type1
->type_class
!= type2
->type_class
) {
2927 if(type1
->network
!= type2
->network
) {
2932 switch(type1
->type_class
) {
2934 case LTT_UINT_FIXED
:
2949 if(field1
->field_size
!= field2
->field_size
)
2956 LttField
*child1
= &g_array_index(type1
->fields
, LttField
, 0);
2957 LttField
*child2
= &g_array_index(type2
->fields
, LttField
, 0);
2959 if(type1
->size
!= type2
->size
)
2961 if(check_fields_compatibility(event_type1
, event_type2
, child1
, child2
))
2967 LttField
*child1
= &g_array_index(type1
->fields
, LttField
, 1);
2968 LttField
*child2
= &g_array_index(type2
->fields
, LttField
, 1);
2970 if(check_fields_compatibility(event_type1
, event_type2
, child1
, child2
))
2980 if(type1
->fields
->len
!= type2
->fields
->len
) {
2985 for(i
=0; i
< type1
->fields
->len
; i
++) {
2988 child1
= &g_array_index(type1
->fields
, LttField
, i
);
2989 child2
= &g_array_index(type2
->fields
, LttField
, i
);
2990 different
= check_fields_compatibility(event_type1
,
2991 event_type2
, child1
, child2
);
2993 if(different
) break;
2999 g_error("check_fields_compatibility : unknown type");
3008 gint
check_fields_compatibility(LttEventType
*event_type1
,
3009 LttEventType
*event_type2
,
3010 LttField
*field1
, LttField
*field2
)
3012 guint different
= 0;
3017 if(field1
== NULL
) {
3018 if(field2
== NULL
) goto end
;
3023 } else if(field2
== NULL
) {
3028 g_assert(field1
->fixed_root
!= FIELD_UNKNOWN
);
3029 g_assert(field2
->fixed_root
!= FIELD_UNKNOWN
);
3030 g_assert(field1
->fixed_parent
!= FIELD_UNKNOWN
);
3031 g_assert(field2
->fixed_parent
!= FIELD_UNKNOWN
);
3032 g_assert(field1
->fixed_size
!= FIELD_UNKNOWN
);
3033 g_assert(field2
->fixed_size
!= FIELD_UNKNOWN
);
3035 type1
= field1
->field_type
;
3036 type2
= field2
->field_type
;
3038 if(type1
->type_class
!= type2
->type_class
) {
3042 if(type1
->element_name
!= type2
->element_name
) {
3047 switch(type1
->type_class
) {
3049 case LTT_UINT_FIXED
:
3064 if(field1
->field_size
!= field2
->field_size
) {
3070 if(type1
->element_number
!= type2
->element_number
) {
3074 for(i
=0;i
<type1
->element_number
;i
++) {
3075 if(type1
->enum_strings
[i
] != type2
->enum_strings
[i
]) {
3082 /* Two elements : size and child */
3083 g_assert(type1
->element_number
!= type2
->element_number
);
3084 for(i
=0;i
<type1
->element_number
;i
++) {
3085 if(check_fields_compatibility(event_type1
, event_type2
,
3086 field1
->child
[0], field2
->child
[0])) {
3095 if(field1
->field_size
!= field2
->field_size
) {
3099 /* Two elements : size and child */
3100 g_assert(type1
->element_number
!= type2
->element_number
);
3101 for(i
=0;i
<type1
->element_number
;i
++) {
3102 if(check_fields_compatibility(event_type1
, event_type2
,
3103 field1
->child
[0], field2
->child
[0])) {
3111 if(type1
->element_number
!= type2
->element_number
) {
3115 for(i
=0;i
<type1
->element_number
;i
++) {
3116 if(check_fields_compatibility(event_type1
, event_type2
,
3117 field1
->child
[0], field2
->child
[0])) {
3130 /*****************************************************************************
3132 * ltt_get_int : get an integer number
3134 * reverse_byte_order: must we reverse the byte order ?
3135 * size : the size of the integer
3136 * ptr : the data pointer
3138 * gint64 : a 64 bits integer
3139 ****************************************************************************/
3141 gint64
ltt_get_int(gboolean reverse_byte_order
, gint size
, void *data
)
3146 case 1: val
= *((gint8
*)data
); break;
3147 case 2: val
= ltt_get_int16(reverse_byte_order
, data
); break;
3148 case 4: val
= ltt_get_int32(reverse_byte_order
, data
); break;
3149 case 8: val
= ltt_get_int64(reverse_byte_order
, data
); break;
3150 default: val
= ltt_get_int64(reverse_byte_order
, data
);
3151 g_critical("get_int : integer size %d unknown", size
);
3158 /*****************************************************************************
3160 * ltt_get_uint : get an unsigned integer number
3162 * reverse_byte_order: must we reverse the byte order ?
3163 * size : the size of the integer
3164 * ptr : the data pointer
3166 * guint64 : a 64 bits unsigned integer
3167 ****************************************************************************/
3169 guint64
ltt_get_uint(gboolean reverse_byte_order
, gint size
, void *data
)
3174 case 1: val
= *((gint8
*)data
); break;
3175 case 2: val
= ltt_get_uint16(reverse_byte_order
, data
); break;
3176 case 4: val
= ltt_get_uint32(reverse_byte_order
, data
); break;
3177 case 8: val
= ltt_get_uint64(reverse_byte_order
, data
); break;
3178 default: val
= ltt_get_uint64(reverse_byte_order
, data
);
3179 g_critical("get_uint : unsigned integer size %d unknown",
3188 /* get the node name of the system */
3190 char * ltt_trace_system_description_node_name (LttSystemDescription
* s
)
3192 return s
->node_name
;
3196 /* get the domain name of the system */
3198 char * ltt_trace_system_description_domain_name (LttSystemDescription
* s
)
3200 return s
->domain_name
;
3204 /* get the description of the system */
3206 char * ltt_trace_system_description_description (LttSystemDescription
* s
)
3208 return s
->description
;
3212 /* get the NTP corrected start time of the trace */
3213 LttTime
ltt_trace_start_time(LttTrace
*t
)
3215 return t
->start_time
;
3218 /* get the monotonic start time of the trace */
3219 LttTime
ltt_trace_start_time_monotonic(LttTrace
*t
)
3221 return t
->start_time_from_tsc
;
3224 LttTracefile
*ltt_tracefile_new()
3227 tf
= g_new(LttTracefile
, 1);
3228 tf
->event
.tracefile
= tf
;
3232 void ltt_tracefile_destroy(LttTracefile
*tf
)
3237 void ltt_tracefile_copy(LttTracefile
*dest
, const LttTracefile
*src
)
3242 /* Before library loading... */
3246 LTT_FACILITY_NAME_HEARTBEAT
= g_quark_from_string("heartbeat");
3247 LTT_EVENT_NAME_HEARTBEAT
= g_quark_from_string("heartbeat");
3248 LTT_EVENT_NAME_HEARTBEAT_FULL
= g_quark_from_string("heartbeat_full");
3250 LTT_TRACEFILE_NAME_FACILITIES
= g_quark_from_string("/control/facilities");