1 /* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2005 Mathieu Desnoyers
4 * Complete rewrite from the original version made by XangXiu Yang.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License Version 2.1 as published by the Free Software Foundation.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
30 #include <sys/types.h>
46 #include "ltt-private.h"
47 #include <ltt/trace.h>
48 #include <ltt/facility.h>
49 #include <ltt/event.h>
51 #include <ltt/ltt-types.h>
52 #include <ltt/markers.h>
54 /* Facility names used in this file */
56 GQuark LTT_FACILITY_NAME_HEARTBEAT
,
57 LTT_EVENT_NAME_HEARTBEAT
,
58 LTT_EVENT_NAME_HEARTBEAT_FULL
;
59 GQuark LTT_TRACEFILE_NAME_FACILITIES
;
66 #define __UNUSED__ __attribute__((__unused__))
68 #define g_info(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, format)
71 #define g_debug(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format)
76 /* Those macros must be called from within a function where page_size is a known
78 #define PAGE_MASK (~(page_size-1))
79 #define PAGE_ALIGN(addr) (((addr)+page_size-1)&PAGE_MASK)
81 /* set the offset of the fields belonging to the event,
82 need the information of the archecture */
83 //void set_fields_offsets(LttTracefile *tf, LttEventType *event_type);
84 //size_t get_fields_offsets(LttTracefile *tf, LttEventType *event_type, void *data);
86 /* get the size of the field type according to
87 * The facility size information. */
89 static inline void preset_field_type_size(LttTracefile
*tf
,
90 LttEventType
*event_type
,
91 off_t offset_root
, off_t offset_parent
,
92 enum field_status
*fixed_root
, enum field_status
*fixed_parent
,
96 /* map a fixed size or a block information from the file (fd) */
97 static gint
map_block(LttTracefile
* tf
, guint block_num
);
99 /* calculate nsec per cycles for current block */
101 static guint32
calc_nsecs_per_cycle(LttTracefile
* t
);
102 static guint64
cycles_2_ns(LttTracefile
*tf
, guint64 cycles
);
105 /* go to the next event */
106 static int ltt_seek_next_event(LttTracefile
*tf
);
108 //void ltt_update_event_size(LttTracefile *tf);
110 static int open_tracefiles(LttTrace
*trace
, gchar
*root_path
,
111 gchar
*relative_path
);
112 static int ltt_process_facility_tracefile(LttTracefile
*tf
);
113 static void ltt_tracefile_time_span_get(LttTracefile
*tf
,
114 LttTime
*start
, LttTime
*end
);
115 static void group_time_span_get(GQuark name
, gpointer data
, gpointer user_data
);
116 static gint
map_block(LttTracefile
* tf
, guint block_num
);
117 static int ltt_seek_next_event(LttTracefile
*tf
);
118 static void __attribute__((constructor
)) init(void);
119 static void ltt_update_event_size(LttTracefile
*tf
);
120 //void precompute_offsets(LttFacility *fac, LttEventType *event);
123 /* Functions to parse system.xml file (using glib xml parser) */
124 static void parser_start_element (GMarkupParseContext __UNUSED__
*context
,
125 const gchar
*element_name
,
126 const gchar
**attribute_names
,
127 const gchar
**attribute_values
,
132 LttSystemDescription
* des
= (LttSystemDescription
* )user_data
;
133 if(strcmp("system", element_name
)){
134 *error
= g_error_new(G_MARKUP_ERROR
,
136 "This is not system.xml file");
140 while(attribute_names
[i
]){
141 if(strcmp("node_name", attribute_names
[i
])==0){
142 des
->node_name
= g_strdup(attribute_values
[i
]);
143 }else if(strcmp("domainname", attribute_names
[i
])==0){
144 des
->domain_name
= g_strdup(attribute_values
[i
]);
145 }else if(strcmp("cpu", attribute_names
[i
])==0){
146 des
->nb_cpu
= atoi(attribute_values
[i
]);
147 }else if(strcmp("arch_size", attribute_names
[i
])==0){
148 if(strcmp(attribute_values
[i
],"LP32") == 0) des
->size
= LTT_LP32
;
149 else if(strcmp(attribute_values
[i
],"ILP32") == 0) des
->size
= LTT_ILP32
;
150 else if(strcmp(attribute_values
[i
],"LP64") == 0) des
->size
= LTT_LP64
;
151 else if(strcmp(attribute_values
[i
],"ILP64") == 0) des
->size
= LTT_ILP64
;
152 else if(strcmp(attribute_values
[i
],"UNKNOWN") == 0) des
->size
= LTT_UNKNOWN
;
153 }else if(strcmp("endian", attribute_names
[i
])==0){
154 if(strcmp(attribute_values
[i
],"LITTLE_ENDIAN") == 0)
155 des
->endian
= LTT_LITTLE_ENDIAN
;
156 else if(strcmp(attribute_values
[i
],"BIG_ENDIAN") == 0)
157 des
->endian
= LTT_BIG_ENDIAN
;
158 }else if(strcmp("kernel_name", attribute_names
[i
])==0){
159 des
->kernel_name
= g_strdup(attribute_values
[i
]);
160 }else if(strcmp("kernel_release", attribute_names
[i
])==0){
161 des
->kernel_release
= g_strdup(attribute_values
[i
]);
162 }else if(strcmp("kernel_version", attribute_names
[i
])==0){
163 des
->kernel_version
= g_strdup(attribute_values
[i
]);
164 }else if(strcmp("machine", attribute_names
[i
])==0){
165 des
->machine
= g_strdup(attribute_values
[i
]);
166 }else if(strcmp("processor", attribute_names
[i
])==0){
167 des
->processor
= g_strdup(attribute_values
[i
]);
168 }else if(strcmp("hardware_platform", attribute_names
[i
])==0){
169 des
->hardware_platform
= g_strdup(attribute_values
[i
]);
170 }else if(strcmp("operating_system", attribute_names
[i
])==0){
171 des
->operating_system
= g_strdup(attribute_values
[i
]);
172 }else if(strcmp("ltt_major_version", attribute_names
[i
])==0){
173 des
->ltt_major_version
= atoi(attribute_values
[i
]);
174 }else if(strcmp("ltt_minor_version", attribute_names
[i
])==0){
175 des
->ltt_minor_version
= atoi(attribute_values
[i
]);
176 }else if(strcmp("ltt_block_size", attribute_names
[i
])==0){
177 des
->ltt_block_size
= atoi(attribute_values
[i
]);
179 *error
= g_error_new(G_MARKUP_ERROR
,
181 "Not a valid attribute");
188 static void parser_characters (GMarkupParseContext __UNUSED__
*context
,
190 gsize __UNUSED__ text_len
,
192 GError __UNUSED__
**error
)
194 LttSystemDescription
* des
= (LttSystemDescription
* )user_data
;
195 des
->description
= g_strdup(text
);
200 LttFacility
*ltt_trace_get_facility_by_num(LttTrace
*t
,
203 g_assert(num
< t
->facilities_by_num
->len
);
205 return &g_array_index(t
->facilities_by_num
, LttFacility
, num
);
210 guint
ltt_trace_get_num_cpu(LttTrace
*t
)
218 * Return value : 0 success, 1 bad tracefile
220 int parse_trace_header(void *header
, LttTracefile
*tf
, LttTrace
*t
)
222 guint32
*magic_number
= (guint32
*)header
;
223 struct ltt_trace_header_any
*any
= (struct ltt_trace_header_any
*)header
;
225 if(*magic_number
== LTT_MAGIC_NUMBER
)
227 else if(*magic_number
== LTT_REV_MAGIC_NUMBER
)
229 else /* invalid magic number, bad tracefile ! */
232 /* Get float byte order : might be different from int byte order
233 * (or is set to 0 if the trace has no float (kernel trace)) */
234 tf
->float_word_order
= any
->float_word_order
;
235 tf
->has_alignment
= any
->has_alignment
;
236 tf
->has_heartbeat
= any
->has_heartbeat
;
239 t
->arch_type
= ltt_get_uint32(LTT_GET_BO(tf
),
241 t
->arch_variant
= ltt_get_uint32(LTT_GET_BO(tf
),
243 t
->arch_size
= any
->arch_size
;
244 t
->ltt_major_version
= any
->major_version
;
245 t
->ltt_minor_version
= any
->minor_version
;
246 t
->flight_recorder
= any
->flight_recorder
;
247 // t->compact_facilities = NULL;
251 switch(any
->major_version
) {
254 g_warning("Unsupported trace version : %hhu.%hhu",
255 any
->major_version
, any
->minor_version
);
259 switch(any
->minor_version
) {
262 struct ltt_trace_header_1_0
*vheader
=
263 (struct ltt_trace_header_1_0
*)header
;
264 tf
->buffer_header_size
=
265 sizeof(struct ltt_block_start_header
)
266 + sizeof(struct ltt_trace_header_1_0
);
267 tf
->tsc_lsb_truncate
= vheader
->tsc_lsb_truncate
;
268 tf
->tscbits
= vheader
->tscbits
;
269 tf
->tsc_msb_cutoff
= 32 - tf
->tsc_lsb_truncate
- tf
->tscbits
;
270 tf
->tsc_mask
= ((1ULL << (tf
->tscbits
))-1);
271 tf
->tsc_mask
= tf
->tsc_mask
<< tf
->tsc_lsb_truncate
;
272 tf
->tsc_mask_next_bit
= (1ULL<<(tf
->tscbits
));
273 tf
->tsc_mask_next_bit
= tf
->tsc_mask_next_bit
<< tf
->tsc_lsb_truncate
;
275 t
->start_freq
= ltt_get_uint64(LTT_GET_BO(tf
),
276 &vheader
->start_freq
);
277 t
->freq_scale
= ltt_get_uint32(LTT_GET_BO(tf
),
278 &vheader
->freq_scale
);
279 t
->start_tsc
= ltt_get_uint64(LTT_GET_BO(tf
),
280 &vheader
->start_tsc
);
281 t
->start_monotonic
= ltt_get_uint64(LTT_GET_BO(tf
),
282 &vheader
->start_monotonic
);
283 t
->start_time
.tv_sec
= ltt_get_uint64(LTT_GET_BO(tf
),
284 &vheader
->start_time_sec
);
285 t
->start_time
.tv_nsec
= ltt_get_uint64(LTT_GET_BO(tf
),
286 &vheader
->start_time_usec
);
287 t
->start_time
.tv_nsec
*= 1000; /* microsec to nanosec */
289 t
->start_time_from_tsc
= ltt_time_from_uint64(
291 * (1000000000.0 / tf
->trace
->freq_scale
)
292 / (double)t
->start_freq
);
293 t
->compact_event_bits
= 0;
298 g_warning("Unsupported trace version : %hhu.%hhu",
299 any
->major_version
, any
->minor_version
);
304 g_warning("Unsupported trace version : %hhu.%hhu",
305 any
->major_version
, any
->minor_version
);
315 /*****************************************************************************
317 * ltt_tracefile_open : open a trace file, construct a LttTracefile
319 * t : the trace containing the tracefile
320 * fileName : path name of the trace file
321 * tf : the tracefile structure
323 * : 0 for success, -1 otherwise.
324 ****************************************************************************/
326 gint
ltt_tracefile_open(LttTrace
*t
, gchar
* fileName
, LttTracefile
*tf
)
328 struct stat lTDFStat
; /* Trace data file status */
329 struct ltt_block_start_header
*header
;
330 int page_size
= getpagesize();
333 tf
->long_name
= g_quark_from_string(fileName
);
335 tf
->fd
= open(fileName
, O_RDONLY
);
337 g_warning("Unable to open input data file %s\n", fileName
);
341 // Get the file's status
342 if(fstat(tf
->fd
, &lTDFStat
) < 0){
343 g_warning("Unable to get the status of the input data file %s\n", fileName
);
347 // Is the file large enough to contain a trace
348 if(lTDFStat
.st_size
<
349 (off_t
)(sizeof(struct ltt_block_start_header
)
350 + sizeof(struct ltt_trace_header_any
))){
351 g_print("The input data file %s does not contain a trace\n", fileName
);
355 /* Temporarily map the buffer start header to get trace information */
356 /* Multiple of pages aligned head */
357 tf
->buffer
.head
= mmap(0,
358 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)
359 + sizeof(struct ltt_trace_header_any
)), PROT_READ
,
360 MAP_PRIVATE
, tf
->fd
, 0);
361 if(tf
->buffer
.head
== MAP_FAILED
) {
362 perror("Error in allocating memory for buffer of tracefile");
365 g_assert( ( (guint
)tf
->buffer
.head
&(8-1) ) == 0); // make sure it's aligned.
367 header
= (struct ltt_block_start_header
*)tf
->buffer
.head
;
369 if(parse_trace_header(header
->trace
, tf
, NULL
)) {
370 g_warning("parse_trace_header error");
374 //store the size of the file
375 tf
->file_size
= lTDFStat
.st_size
;
376 tf
->buf_size
= ltt_get_uint32(LTT_GET_BO(tf
), &header
->buf_size
);
377 tf
->num_blocks
= tf
->file_size
/ tf
->buf_size
;
379 if(munmap(tf
->buffer
.head
,
380 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)
381 + sizeof(struct ltt_trace_header_any
)))) {
382 g_warning("unmap size : %u\n",
383 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)
384 + sizeof(struct ltt_trace_header_any
)));
385 perror("munmap error");
388 tf
->buffer
.head
= NULL
;
390 //read the first block
391 if(map_block(tf
,0)) {
392 perror("Cannot map block for tracefile");
400 if(munmap(tf
->buffer
.head
,
401 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)
402 + sizeof(struct ltt_trace_header_any
)))) {
403 g_warning("unmap size : %u\n",
404 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)
405 + sizeof(struct ltt_trace_header_any
)));
406 perror("munmap error");
415 LttTrace
*ltt_tracefile_get_trace(LttTracefile
*tf
)
421 /*****************************************************************************
422 *Open control and per cpu tracefiles
423 ****************************************************************************/
425 void ltt_tracefile_open_cpu(LttTrace
*t
, gchar
* tracefile_name
)
428 tf
= ltt_tracefile_open(t
,tracefile_name
);
430 t
->per_cpu_tracefile_number
++;
431 g_ptr_array_add(t
->per_cpu_tracefiles
, tf
);
434 gint
ltt_tracefile_open_control(LttTrace
*t
, gchar
* control_name
)
443 tf
= ltt_tracefile_open(t
,control_name
);
445 g_warning("ltt_tracefile_open_control : bad file descriptor");
448 t
->control_tracefile_number
++;
449 g_ptr_array_add(t
->control_tracefiles
,tf
);
451 //parse facilities tracefile to get base_id
452 if(strcmp(&control_name
[strlen(control_name
)-10],"facilities") ==0){
454 if(!ltt_tracefile_read(tf
,&ev
)) return 0; // end of file
456 if(ev
.event_id
== TRACE_FACILITY_LOAD
){
458 fLoad
.name
= (gchar
*)pos
;
459 fLoad
.checksum
= *(LttChecksum
*)(pos
+ strlen(fLoad
.name
));
460 fLoad
.base_code
= *(guint32
*)(pos
+ strlen(fLoad
.name
) + sizeof(LttChecksum
));
462 for(i
=0;i
<t
->facility_number
;i
++){
463 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
,i
);
464 if(strcmp(f
->name
,fLoad
.name
)==0 && fLoad
.checksum
==f
->checksum
){
465 f
->base_id
= fLoad
.base_code
;
469 if(i
==t
->facility_number
) {
470 g_warning("Facility: %s, checksum: %u is not found",
471 fLoad
.name
,(unsigned int)fLoad
.checksum
);
474 }else if(ev
.event_id
== TRACE_BLOCK_START
){
476 }else if(ev
.event_id
== TRACE_BLOCK_END
){
479 g_warning("Not valid facilities trace file");
488 /*****************************************************************************
490 * ltt_tracefile_close: close a trace file,
492 * t : tracefile which will be closed
493 ****************************************************************************/
495 void ltt_tracefile_close(LttTracefile
*t
)
497 int page_size
= getpagesize();
499 if(t
->buffer
.head
!= NULL
)
500 if(munmap(t
->buffer
.head
, PAGE_ALIGN(t
->buf_size
))) {
501 g_warning("unmap size : %u\n",
502 PAGE_ALIGN(t
->buf_size
));
503 perror("munmap error");
511 /*****************************************************************************
512 *Get system information
513 ****************************************************************************/
515 gint
getSystemInfo(LttSystemDescription
* des
, gchar
* pathname
)
522 GMarkupParseContext
* context
;
523 GError
* error
= NULL
;
524 GMarkupParser markup_parser
=
526 parser_start_element
,
529 NULL
, /* passthrough */
533 fd
= g_open(pathname
, O_RDONLY
, 0);
535 g_warning("Can not open file : %s\n", pathname
);
539 iochan
= g_io_channel_unix_new(fd
);
541 context
= g_markup_parse_context_new(&markup_parser
, 0, des
,NULL
);
543 //while(fgets(buf,DIR_NAME_SIZE, fp) != NULL){
544 while(g_io_channel_read_line(iochan
, &buf
, &length
, NULL
, &error
)
545 != G_IO_STATUS_EOF
) {
548 g_warning("Can not read xml file: \n%s\n", error
->message
);
551 if(!g_markup_parse_context_parse(context
, buf
, length
, &error
)){
553 g_warning("Can not parse xml file: \n%s\n", error
->message
);
556 g_markup_parse_context_free(context
);
558 g_io_channel_shutdown(iochan
, FALSE
, &error
); /* No flush */
560 g_warning("Can not close file: \n%s\n", error
->message
);
568 g_markup_parse_context_free(context
);
570 g_io_channel_shutdown(iochan
, FALSE
, &error
); /* No flush */
572 g_warning("Can not close file: \n%s\n", error
->message
);
583 /*****************************************************************************
584 *The following functions get facility/tracefile information
585 ****************************************************************************/
587 gint
getFacilityInfo(LttTrace
*t
, gchar
* eventdefs
)
594 gchar fullname
[DIR_NAME_SIZE
];
595 GError
* error
= NULL
;
597 dir
= g_dir_open(eventdefs
, 0, &error
);
600 g_warning("Can not open directory: %s, %s\n", eventdefs
, error
->message
);
605 while((name
= g_dir_read_name(dir
)) != NULL
){
606 if(!g_pattern_match_simple("*.xml", name
)) continue;
607 strcpy(fullname
,eventdefs
);
608 strcat(fullname
,name
);
609 ltt_facility_open(t
,fullname
);
613 for(j
=0;j
<t
->facility_number
;j
++){
614 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, j
);
615 for(i
=0; i
<f
->event_number
; i
++){
617 setFieldsOffset(NULL
, et
, NULL
, t
);
624 /*****************************************************************************
625 *A trace is specified as a pathname to the directory containing all the
626 *associated data (control tracefiles, per cpu tracefiles, event
629 *When a trace is closed, all the associated facilities, types and fields
630 *are released as well.
634 /****************************************************************************
635 * get_absolute_pathname
637 * return the unique pathname in the system
639 * MD : Fixed this function so it uses realpath, dealing well with
640 * forgotten cases (.. were not used correctly before).
642 ****************************************************************************/
643 void get_absolute_pathname(const gchar
*pathname
, gchar
* abs_pathname
)
645 abs_pathname
[0] = '\0';
647 if ( realpath (pathname
, abs_pathname
) != NULL
)
651 /* error, return the original path unmodified */
652 strcpy(abs_pathname
, pathname
);
658 /* Search for something like : .*_.*
660 * The left side is the name, the right side is the number.
663 int get_tracefile_name_number(gchar
*raw_name
,
670 guint raw_name_len
= strlen(raw_name
);
671 gchar char_name
[PATH_MAX
];
678 for(i
=raw_name_len
-1;i
>=0;i
--) {
679 if(raw_name
[i
] == '_') break;
681 if(i
==-1) { /* Either not found or name length is 0 */
682 /* This is a userspace tracefile */
683 strncpy(char_name
, raw_name
, raw_name_len
);
684 char_name
[raw_name_len
] = '\0';
685 *name
= g_quark_from_string(char_name
);
686 *num
= 0; /* unknown cpu */
687 for(i
=0;i
<raw_name_len
;i
++) {
688 if(raw_name
[i
] == '/') {
693 for(;i
<raw_name_len
;i
++) {
694 if(raw_name
[i
] == '/') {
699 for(;i
<raw_name_len
;i
++) {
700 if(raw_name
[i
] == '-') {
704 if(i
== raw_name_len
) return -1;
706 tmpptr
= &raw_name
[i
];
707 for(;i
<raw_name_len
;i
++) {
708 if(raw_name
[i
] == '.') {
713 *tid
= strtoul(tmpptr
, &endptr
, 10);
715 return -1; /* No digit */
716 if(*tid
== ULONG_MAX
)
717 return -1; /* underflow / overflow */
719 tmpptr
= &raw_name
[i
];
720 for(;i
<raw_name_len
;i
++) {
721 if(raw_name
[i
] == '.') {
726 *pgid
= strtoul(tmpptr
, &endptr
, 10);
728 return -1; /* No digit */
729 if(*pgid
== ULONG_MAX
)
730 return -1; /* underflow / overflow */
732 tmpptr
= &raw_name
[i
];
733 *creation
= strtoull(tmpptr
, &endptr
, 10);
735 return -1; /* No digit */
736 if(*creation
== G_MAXUINT64
)
737 return -1; /* underflow / overflow */
741 cpu_num
= strtol(raw_name
+underscore_pos
+1, &endptr
, 10);
743 if(endptr
== raw_name
+underscore_pos
+1)
744 return -1; /* No digit */
745 if(cpu_num
== LONG_MIN
|| cpu_num
== LONG_MAX
)
746 return -1; /* underflow / overflow */
748 strncpy(char_name
, raw_name
, underscore_pos
);
749 char_name
[underscore_pos
] = '\0';
751 *name
= g_quark_from_string(char_name
);
760 GData
**ltt_trace_get_tracefiles_groups(LttTrace
*trace
)
762 return &trace
->tracefiles
;
766 void compute_tracefile_group(GQuark key_id
,
768 struct compute_tracefile_group_args
*args
)
773 for(i
=0; i
<group
->len
; i
++) {
774 tf
= &g_array_index (group
, LttTracefile
, i
);
776 args
->func(tf
, args
->func_args
);
781 void ltt_tracefile_group_destroy(gpointer data
)
783 GArray
*group
= (GArray
*)data
;
787 for(i
=0; i
<group
->len
; i
++) {
788 tf
= &g_array_index (group
, LttTracefile
, i
);
790 ltt_tracefile_close(tf
);
792 g_array_free(group
, TRUE
);
795 gboolean
ltt_tracefile_group_has_cpu_online(gpointer data
)
797 GArray
*group
= (GArray
*)data
;
801 for(i
=0; i
<group
->len
; i
++) {
802 tf
= &g_array_index (group
, LttTracefile
, i
);
810 /* Open each tracefile under a specific directory. Put them in a
811 * GData : permits to access them using their tracefile group pathname.
812 * i.e. access control/modules tracefile group by index :
815 * relative path is the path relative to the trace root
816 * root path is the full path
818 * A tracefile group is simply an array where all the per cpu tracefiles sit.
821 int open_tracefiles(LttTrace
*trace
, gchar
*root_path
, gchar
*relative_path
)
823 DIR *dir
= opendir(root_path
);
824 struct dirent
*entry
;
825 struct stat stat_buf
;
828 gchar path
[PATH_MAX
];
833 gchar rel_path
[PATH_MAX
];
842 strncpy(path
, root_path
, PATH_MAX
-1);
843 path_len
= strlen(path
);
844 path
[path_len
] = '/';
846 path_ptr
= path
+ path_len
;
848 strncpy(rel_path
, relative_path
, PATH_MAX
-1);
849 rel_path_len
= strlen(rel_path
);
850 rel_path
[rel_path_len
] = '/';
852 rel_path_ptr
= rel_path
+ rel_path_len
;
854 while((entry
= readdir(dir
)) != NULL
) {
856 if(entry
->d_name
[0] == '.') continue;
858 strncpy(path_ptr
, entry
->d_name
, PATH_MAX
- path_len
);
859 strncpy(rel_path_ptr
, entry
->d_name
, PATH_MAX
- rel_path_len
);
861 ret
= stat(path
, &stat_buf
);
867 g_debug("Tracefile file or directory : %s\n", path
);
869 if(strcmp(rel_path
, "/eventdefs") == 0) continue;
871 if(S_ISDIR(stat_buf
.st_mode
)) {
873 g_debug("Entering subdirectory...\n");
874 ret
= open_tracefiles(trace
, path
, rel_path
);
875 if(ret
< 0) continue;
876 } else if(S_ISREG(stat_buf
.st_mode
)) {
878 guint num
, tid
, pgid
;
881 num
= tid
= pgid
= 0;
883 if(get_tracefile_name_number(rel_path
, &name
, &num
, &tid
, &pgid
, &creation
))
884 continue; /* invalid name */
886 g_debug("Opening file.\n");
887 if(ltt_tracefile_open(trace
, path
, &tmp_tf
)) {
888 g_info("Error opening tracefile %s", path
);
890 continue; /* error opening the tracefile : bad magic number ? */
893 g_debug("Tracefile name is %s and number is %u",
894 g_quark_to_string(name
), num
);
896 tmp_tf
.cpu_online
= 1;
897 tmp_tf
.cpu_num
= num
;
901 tmp_tf
.creation
= creation
;
902 if(tmp_tf
.name
== g_quark_from_string("/compact")
903 || tmp_tf
.name
== g_quark_from_string("/flight-compact"))
907 group
= g_datalist_id_get_data(&trace
->tracefiles
, name
);
909 /* Elements are automatically cleared when the array is allocated.
910 * It makes the cpu_online variable set to 0 : cpu offline, by default.
912 group
= g_array_sized_new (FALSE
, TRUE
, sizeof(LttTracefile
), 10);
913 g_datalist_id_set_data_full(&trace
->tracefiles
, name
,
914 group
, ltt_tracefile_group_destroy
);
917 /* Add the per cpu tracefile to the named group */
918 unsigned int old_len
= group
->len
;
920 group
= g_array_set_size(group
, num
+1);
921 g_array_index (group
, LttTracefile
, num
) = tmp_tf
;
931 /* ltt_get_facility_description
933 * Opens the file corresponding to the requested facility (identified by fac_id
936 * The name searched is : %trace root%/eventdefs/facname_checksum.xml
938 * Returns 0 on success, or 1 on failure.
941 static int ltt_get_facility_description(LttFacility
*f
,
943 LttTracefile
*fac_tf
)
945 char desc_file_name
[PATH_MAX
];
952 text
= g_quark_to_string(t
->pathname
);
953 textlen
= strlen(text
);
955 if(textlen
>= PATH_MAX
) goto name_error
;
956 strcpy(desc_file_name
, text
);
958 text
= "/eventdefs/";
959 textlen
+=strlen(text
);
960 if(textlen
>= PATH_MAX
) goto name_error
;
961 strcat(desc_file_name
, text
);
963 text
= g_quark_to_string(f
->name
);
964 fac_name_len
= strlen(text
);
965 textlen
+=fac_name_len
;
966 if(textlen
>= PATH_MAX
) goto name_error
;
967 strcat(desc_file_name
, text
);
969 /* arch specific facilities are named like this : name_arch */
970 if(fac_name_len
+1 < sizeof("_arch"))
973 if(!strcmp(&text
[fac_name_len
+1-sizeof("_arch")], "_arch"))
981 textlen
+=strlen(text
);
982 if(textlen
>= PATH_MAX
) goto name_error
;
983 strcat(desc_file_name
, text
);
985 err
= snprintf(desc_file_name
+textlen
, PATH_MAX
-textlen
-1,
987 if(err
< 0) goto name_error
;
989 textlen
=strlen(desc_file_name
);
994 switch(t
->arch_type
) {
995 case LTT_ARCH_TYPE_I386
:
998 case LTT_ARCH_TYPE_PPC
:
1001 case LTT_ARCH_TYPE_SH
:
1004 case LTT_ARCH_TYPE_S390
:
1007 case LTT_ARCH_TYPE_MIPS
:
1010 case LTT_ARCH_TYPE_ARM
:
1013 case LTT_ARCH_TYPE_PPC64
:
1016 case LTT_ARCH_TYPE_X86_64
:
1019 case LTT_ARCH_TYPE_C2
:
1022 case LTT_ARCH_TYPE_POWERPC
:
1026 g_error("Trace from unsupported architecture.");
1028 textlen
+=strlen(text
);
1029 if(textlen
>= PATH_MAX
) goto name_error
;
1030 strcat(desc_file_name
, text
);
1034 textlen
+=strlen(text
);
1035 if(textlen
>= PATH_MAX
) goto name_error
;
1036 strcat(desc_file_name
, text
);
1038 err
= ltt_facility_open(f
, t
, desc_file_name
);
1039 if(err
) goto facility_error
;
1048 static void ltt_fac_ids_destroy(gpointer data
)
1050 GArray
*fac_ids
= (GArray
*)data
;
1052 g_array_free(fac_ids
, TRUE
);
1056 /* Presumes the tracefile is already seeked at the beginning. It makes sense,
1057 * because it must be done just after the opening */
1058 int ltt_process_facility_tracefile(LttTracefile
*tf
)
1067 err
= ltt_tracefile_read_seek(tf
);
1068 if(err
== EPERM
) goto seek_error
;
1069 else if(err
== ERANGE
) break; /* End of tracefile */
1071 err
= ltt_tracefile_read_update_event(tf
);
1072 if(err
) goto update_error
;
1074 /* We are on a facility load/or facility unload/ or heartbeat event */
1076 * * facility 0 is hardcoded : this is the core facility. It will be shown
1077 * in the facility array though, and is shown as "loaded builtin" in the
1079 * It contains event :
1081 * 1 : facility unload
1082 * 2 : state dump facility load
1085 if(tf
->event
.event_id
>= MARKER_CORE_IDS
) {
1086 /* Should only contain core facility */
1087 g_warning("Error in processing facility file %s, "
1088 "should not contain event id %u.", g_quark_to_string(tf
->name
),
1089 tf
->event
.event_id
);
1091 goto event_id_error
;
1095 const char *marker_name
, *format
;
1097 guint8 int_size
, long_size
, pointer_size
, size_t_size
, alignment
;
1100 switch((enum marker_id
)tf
->event
.event_id
) {
1101 case MARKER_ID_SET_MARKER_ID
:
1102 marker_name
= (char*)(tf
->event
.data
);
1103 g_debug("Doing MARKER_ID_SET_MARKER_ID of marker %s", marker_name
);
1104 pos
= (tf
->event
.data
+ strlen(marker_name
) + 1);
1105 pos
+= ltt_align((size_t)pos
, tf
->trace
->arch_size
, tf
->has_alignment
);
1106 pos
+= ltt_align((size_t)pos
, sizeof(uint16_t), tf
->has_alignment
);
1107 id
= ltt_get_uint16(LTT_GET_BO(tf
), pos
);
1108 pos
+= sizeof(guint16
);
1109 int_size
= *(guint8
*)pos
;
1110 pos
+= sizeof(guint8
);
1111 long_size
= *(guint8
*)pos
;
1112 pos
+= sizeof(guint8
);
1113 pointer_size
= *(guint8
*)pos
;
1114 pos
+= sizeof(guint8
);
1115 size_t_size
= *(guint8
*)pos
;
1116 pos
+= sizeof(guint8
);
1117 alignment
= *(guint8
*)pos
;
1118 pos
+= sizeof(guint8
);
1119 marker_id_event(tf
->trace
, g_quark_from_string(marker_name
),
1120 id
, int_size
, long_size
,
1121 pointer_size
, size_t_size
, alignment
);
1123 case MARKER_ID_SET_MARKER_FORMAT
:
1124 marker_name
= (char*)(tf
->event
.data
);
1125 g_debug("Doing MARKER_ID_SET_MARKER_FORMAT of marker %s",
1127 pos
= (tf
->event
.data
+ strlen(marker_name
) + 1);
1128 pos
+= ltt_align((size_t)pos
, tf
->trace
->arch_size
, tf
->has_alignment
);
1129 format
= (const char*)pos
;
1130 pos
+= strlen(format
) + 1;
1131 pos
+= ltt_align((size_t)pos
, tf
->trace
->arch_size
, tf
->has_alignment
);
1132 marker_format_event(tf
->trace
, g_quark_from_string(marker_name
),
1134 /* get information from dictionnary TODO */
1136 case MARKER_ID_HEARTBEAT_32
:
1137 case MARKER_ID_HEARTBEAT_64
:
1140 g_warning("Error in processing facility file %s, "
1141 "unknown event id %hhu.",
1142 g_quark_to_string(tf
->name
),
1143 tf
->event
.event_id
);
1145 goto event_id_error
;
1151 /* Error handling */
1155 g_warning("An error occured in facility tracefile parsing");
1160 LttTrace
*ltt_trace_open(const gchar
*pathname
)
1162 gchar abs_path
[PATH_MAX
];
1167 struct ltt_block_start_header
*header
;
1169 struct dirent
*entry
;
1170 guint control_found
= 0;
1171 guint eventdefs_found
= 0;
1172 struct stat stat_buf
;
1173 gchar path
[PATH_MAX
];
1175 t
= g_new(LttTrace
, 1);
1176 if(!t
) goto alloc_error
;
1178 get_absolute_pathname(pathname
, abs_path
);
1179 t
->pathname
= g_quark_from_string(abs_path
);
1181 g_datalist_init(&t
->tracefiles
);
1183 /* Test to see if it looks like a trace */
1184 dir
= opendir(abs_path
);
1189 while((entry
= readdir(dir
)) != NULL
) {
1190 strcpy(path
, abs_path
);
1192 strcat(path
, entry
->d_name
);
1193 ret
= stat(path
, &stat_buf
);
1198 if(S_ISDIR(stat_buf
.st_mode
)) {
1199 if(strcmp(entry
->d_name
, "control") == 0) {
1202 if(strcmp(entry
->d_name
, "eventdefs") == 0) {
1203 eventdefs_found
= 1;
1209 if(!control_found
|| !eventdefs_found
) goto find_error
;
1211 /* Open all the tracefiles */
1212 if(open_tracefiles(t
, abs_path
, "")) {
1213 g_warning("Error opening tracefile %s", abs_path
);
1217 /* Parse each trace control/facilitiesN files : get runtime fac. info */
1218 group
= g_datalist_id_get_data(&t
->tracefiles
, LTT_TRACEFILE_NAME_FACILITIES
);
1220 g_error("Trace %s has no facility tracefile", abs_path
);
1222 goto facilities_error
;
1225 /* Get the trace information for the control/facility 0 tracefile */
1226 g_assert(group
->len
> 0);
1227 tf
= &g_array_index (group
, LttTracefile
, 0);
1228 header
= (struct ltt_block_start_header
*)tf
->buffer
.head
;
1229 g_assert(parse_trace_header(header
->trace
,
1232 t
->num_cpu
= group
->len
;
1234 for(i
=0; i
<group
->len
; i
++) {
1235 tf
= &g_array_index (group
, LttTracefile
, i
);
1236 if(ltt_process_facility_tracefile(tf
))
1237 goto facilities_error
;
1242 /* Error handling */
1244 g_datalist_clear(&t
->facilities_by_name
);
1245 g_array_free(t
->facilities_by_num
, TRUE
);
1247 g_datalist_clear(&t
->tracefiles
);
1255 GQuark
ltt_trace_name(const LttTrace
*t
)
1261 /******************************************************************************
1262 * When we copy a trace, we want all the opening actions to happen again :
1263 * the trace will be reopened and totally independant from the original.
1264 * That's why we call ltt_trace_open.
1265 *****************************************************************************/
1266 LttTrace
*ltt_trace_copy(LttTrace
*self
)
1268 return ltt_trace_open(g_quark_to_string(self
->pathname
));
1271 void ltt_trace_close(LttTrace
*t
)
1273 g_datalist_clear(&t
->tracefiles
);
1278 /*****************************************************************************
1279 *Get the system description of the trace
1280 ****************************************************************************/
1282 LttFacility
*ltt_trace_facility_by_id(LttTrace
*t
, guint8 id
)
1284 g_assert(id
< t
->facilities_by_num
->len
);
1285 return &g_array_index(t
->facilities_by_num
, LttFacility
, id
);
1288 /* ltt_trace_facility_get_by_name
1290 * Returns the GArray of facility indexes. All the fac_ids that matches the
1291 * requested facility name.
1293 * If name is not found, returns NULL.
1295 GArray
*ltt_trace_facility_get_by_name(LttTrace
*t
, GQuark name
)
1297 return g_datalist_id_get_data(&t
->facilities_by_name
, name
);
1301 /*****************************************************************************
1302 * Functions to discover all the event types in the trace
1303 ****************************************************************************/
1306 unsigned ltt_trace_eventtype_number(LttTrace
*t
)
1310 unsigned int num
= t
->facility_number
;
1314 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, i
);
1315 count
+= f
->event_number
;
1322 //use an iteration on all the trace facilities, and inside iteration on all the
1323 //event types in each facilities instead.
1324 LttEventType
*ltt_trace_eventtype_get(LttTrace
*t
, unsigned evId
)
1326 LttEventType
*event_type
;
1329 f
= ltt_trace_facility_by_id(t
,evId
);
1331 if(unlikely(!f
)) event_type
= NULL
;
1332 else event_type
= f
->events
[evId
- f
->base_id
];
1339 /*****************************************************************************
1340 * ltt_trace_find_tracefile
1342 * Find a tracefile by name and index in the group.
1344 * Returns a pointer to the tracefiles, else NULL.
1345 ****************************************************************************/
1347 LttTracefile
*ltt_trace_find_tracefile(LttTrace
*t
, const gchar
*name
)
1352 /*****************************************************************************
1353 * Get the start time and end time of the trace
1354 ****************************************************************************/
1356 void ltt_tracefile_time_span_get(LttTracefile
*tf
,
1357 LttTime
*start
, LttTime
*end
)
1361 err
= map_block(tf
, 0);
1363 g_error("Can not map block");
1364 *start
= ltt_time_infinite
;
1366 *start
= tf
->buffer
.begin
.timestamp
;
1368 err
= map_block(tf
, tf
->num_blocks
- 1); /* Last block */
1370 g_error("Can not map block");
1371 *end
= ltt_time_zero
;
1373 *end
= tf
->buffer
.end
.timestamp
;
1376 struct tracefile_time_span_get_args
{
1382 void group_time_span_get(GQuark name
, gpointer data
, gpointer user_data
)
1384 struct tracefile_time_span_get_args
*args
=
1385 (struct tracefile_time_span_get_args
*)user_data
;
1387 GArray
*group
= (GArray
*)data
;
1393 for(i
=0; i
<group
->len
; i
++) {
1394 tf
= &g_array_index (group
, LttTracefile
, i
);
1395 if(tf
->cpu_online
) {
1396 ltt_tracefile_time_span_get(tf
, &tmp_start
, &tmp_end
);
1397 if(ltt_time_compare(*args
->start
, tmp_start
)>0) *args
->start
= tmp_start
;
1398 if(ltt_time_compare(*args
->end
, tmp_end
)<0) *args
->end
= tmp_end
;
1403 void ltt_trace_time_span_get(LttTrace
*t
, LttTime
*start
, LttTime
*end
)
1405 LttTime min_start
= ltt_time_infinite
;
1406 LttTime max_end
= ltt_time_zero
;
1407 struct tracefile_time_span_get_args args
= { t
, &min_start
, &max_end
};
1409 g_datalist_foreach(&t
->tracefiles
, &group_time_span_get
, &args
);
1411 if(start
!= NULL
) *start
= min_start
;
1412 if(end
!= NULL
) *end
= max_end
;
1417 /*****************************************************************************
1418 *Get the name of a tracefile
1419 ****************************************************************************/
1421 GQuark
ltt_tracefile_name(const LttTracefile
*tf
)
1426 GQuark
ltt_tracefile_long_name(const LttTracefile
*tf
)
1428 return tf
->long_name
;
1433 guint
ltt_tracefile_cpu(LttTracefile
*tf
)
1438 guint
ltt_tracefile_tid(LttTracefile
*tf
)
1443 guint
ltt_tracefile_pgid(LttTracefile
*tf
)
1448 guint64
ltt_tracefile_creation(LttTracefile
*tf
)
1450 return tf
->creation
;
1452 /*****************************************************************************
1453 * Get the number of blocks in the tracefile
1454 ****************************************************************************/
1456 guint
ltt_tracefile_block_number(LttTracefile
*tf
)
1458 return tf
->num_blocks
;
1462 /* Seek to the first event in a tracefile that has a time equal or greater than
1463 * the time passed in parameter.
1465 * If the time parameter is outside the tracefile time span, seek to the first
1466 * event or if after, return ERANGE.
1468 * If the time parameter is before the first event, we have to seek specially to
1471 * If the time is after the end of the trace, return ERANGE.
1473 * Do a binary search to find the right block, then a sequential search in the
1474 * block to find the event.
1476 * In the special case where the time requested fits inside a block that has no
1477 * event corresponding to the requested time, the first event of the next block
1480 * IMPORTANT NOTE : // FIXME everywhere...
1482 * You MUST NOT do a ltt_tracefile_read right after a ltt_tracefile_seek_time :
1483 * you will jump over an event if you do.
1485 * Return value : 0 : no error, the tf->event can be used
1486 * ERANGE : time if after the last event of the trace
1487 * otherwise : this is an error.
1491 int ltt_tracefile_seek_time(LttTracefile
*tf
, LttTime time
)
1495 unsigned int block_num
, high
, low
;
1497 /* seek at the beginning of trace */
1498 err
= map_block(tf
, 0); /* First block */
1500 g_error("Can not map block");
1504 /* If the time is lower or equal the beginning of the trace,
1505 * go to the first event. */
1506 if(ltt_time_compare(time
, tf
->buffer
.begin
.timestamp
) <= 0) {
1507 ret
= ltt_tracefile_read(tf
);
1508 if(ret
== ERANGE
) goto range
;
1509 else if (ret
) goto fail
;
1510 goto found
; /* There is either no event in the trace or the event points
1511 to the first event in the trace */
1514 err
= map_block(tf
, tf
->num_blocks
- 1); /* Last block */
1516 g_error("Can not map block");
1520 /* If the time is after the end of the trace, return ERANGE. */
1521 if(ltt_time_compare(time
, tf
->buffer
.end
.timestamp
) > 0) {
1525 /* Binary search the block */
1526 high
= tf
->num_blocks
- 1;
1530 block_num
= ((high
-low
) / 2) + low
;
1532 err
= map_block(tf
, block_num
);
1534 g_error("Can not map block");
1538 /* We cannot divide anymore : this is what would happen if the time
1539 * requested was exactly between two consecutive buffers'end and start
1540 * timestamps. This is also what would happend if we didn't deal with out
1541 * of span cases prior in this function. */
1542 /* The event is right in the buffer!
1543 * (or in the next buffer first event) */
1545 ret
= ltt_tracefile_read(tf
);
1546 if(ret
== ERANGE
) goto range
; /* ERANGE or EPERM */
1547 else if(ret
) goto fail
;
1549 if(ltt_time_compare(time
, tf
->event
.event_time
) <= 0)
1553 } else if(ltt_time_compare(time
, tf
->buffer
.begin
.timestamp
) < 0) {
1554 /* go to lower part */
1555 high
= block_num
- 1;
1556 } else if(ltt_time_compare(time
, tf
->buffer
.end
.timestamp
) > 0) {
1557 /* go to higher part */
1558 low
= block_num
+ 1;
1559 } else {/* The event is right in the buffer!
1560 (or in the next buffer first event) */
1562 ret
= ltt_tracefile_read(tf
);
1563 if(ret
== ERANGE
) goto range
; /* ERANGE or EPERM */
1564 else if(ret
) goto fail
;
1566 if(ltt_time_compare(time
, tf
->event
.event_time
) <= 0)
1578 /* Error handling */
1580 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1581 g_quark_to_string(tf
->name
));
1586 int ltt_tracefile_seek_position(LttTracefile
*tf
, const LttEventPosition
*ep
) {
1590 if(ep
->tracefile
!= tf
) {
1594 err
= map_block(tf
, ep
->block
);
1596 g_error("Can not map block");
1600 tf
->event
.offset
= ep
->offset
;
1602 /* Put back the event real tsc */
1603 tf
->event
.tsc
= ep
->tsc
;
1604 tf
->buffer
.tsc
= ep
->tsc
;
1606 err
= ltt_tracefile_read_update_event(tf
);
1608 err
= ltt_tracefile_read_op(tf
);
1614 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1615 g_quark_to_string(tf
->name
));
1619 LttTime
ltt_interpolate_time_from_tsc(LttTracefile
*tf
, guint64 tsc
)
1623 if(tsc
> tf
->trace
->start_tsc
) {
1624 time
= ltt_time_from_uint64(
1625 (double)(tsc
- tf
->trace
->start_tsc
)
1626 * (1000000000.0 / tf
->trace
->freq_scale
)
1627 / (double)tf
->trace
->start_freq
);
1628 time
= ltt_time_add(tf
->trace
->start_time_from_tsc
, time
);
1630 time
= ltt_time_from_uint64(
1631 (double)(tf
->trace
->start_tsc
- tsc
)
1632 * (1000000000.0 / tf
->trace
->freq_scale
)
1633 / (double)tf
->trace
->start_freq
);
1634 time
= ltt_time_sub(tf
->trace
->start_time_from_tsc
, time
);
1639 /* Calculate the real event time based on the buffer boundaries */
1640 LttTime
ltt_interpolate_time(LttTracefile
*tf
, LttEvent
*event
)
1642 return ltt_interpolate_time_from_tsc(tf
, tf
->buffer
.tsc
);
1646 /* Get the current event of the tracefile : valid until the next read */
1647 LttEvent
*ltt_tracefile_get_event(LttTracefile
*tf
)
1654 /*****************************************************************************
1656 * ltt_tracefile_read : Read the next event in the tracefile
1661 * Returns 0 if an event can be used in tf->event.
1662 * Returns ERANGE on end of trace. The event in tf->event still can be used
1663 * (if the last block was not empty).
1664 * Returns EPERM on error.
1666 * This function does make the tracefile event structure point to the event
1667 * currently pointed to by the tf->event.
1669 * Note : you must call a ltt_tracefile_seek to the beginning of the trace to
1670 * reinitialize it after an error if you want results to be coherent.
1671 * It would be the case if a end of trace last buffer has no event : the end
1672 * of trace wouldn't be returned, but an error.
1673 * We make the assumption there is at least one event per buffer.
1674 ****************************************************************************/
1676 int ltt_tracefile_read(LttTracefile
*tf
)
1680 err
= ltt_tracefile_read_seek(tf
);
1682 err
= ltt_tracefile_read_update_event(tf
);
1684 err
= ltt_tracefile_read_op(tf
);
1690 int ltt_tracefile_read_seek(LttTracefile
*tf
)
1694 /* Get next buffer until we finally have an event, or end of trace */
1696 err
= ltt_seek_next_event(tf
);
1697 if(unlikely(err
== ENOPROTOOPT
)) {
1701 /* Are we at the end of the buffer ? */
1703 if(unlikely(tf
->buffer
.index
== tf
->num_blocks
-1)){ /* end of trace ? */
1706 /* get next block */
1707 err
= map_block(tf
, tf
->buffer
.index
+ 1);
1709 g_error("Can not map block");
1713 } else break; /* We found an event ! */
1720 /* do specific operation on events */
1721 int ltt_tracefile_read_op(LttTracefile
*tf
)
1727 /* do event specific operation */
1729 /* do something if its an heartbeat event : increment the heartbeat count */
1730 //if(event->facility_id == LTT_FACILITY_CORE)
1731 // if(event->event_id == LTT_EVENT_HEARTBEAT)
1732 // tf->cur_heart_beat_number++;
1738 /* same as ltt_tracefile_read, but does not seek to the next event nor call
1739 * event specific operation. */
1740 int ltt_tracefile_read_update_event(LttTracefile
*tf
)
1746 pos
= tf
->buffer
.head
+ event
->offset
;
1748 /* Read event header */
1750 /* Align the head */
1752 pos
+= ltt_align((size_t)pos
, tf
->trace
->arch_size
, tf
->has_alignment
);
1754 g_assert(tf
->has_heartbeat
);
1755 pos
+= ltt_align((size_t)pos
, sizeof(uint32_t), tf
->has_alignment
);
1758 if(tf
->has_heartbeat
) {
1759 event
->timestamp
= ltt_get_uint32(LTT_GET_BO(tf
),
1762 /* 32 bits -> 64 bits tsc */
1763 /* note : still works for seek and non seek cases. */
1764 if(event
->timestamp
< (0xFFFFFFFFULL
&tf
->buffer
.tsc
)) {
1765 tf
->buffer
.tsc
= ((tf
->buffer
.tsc
&0xFFFFFFFF00000000ULL
)
1767 | (guint64
)event
->timestamp
;
1768 event
->tsc
= tf
->buffer
.tsc
;
1771 tf
->buffer
.tsc
= (tf
->buffer
.tsc
&0xFFFFFFFF00000000ULL
)
1772 | (guint64
)event
->timestamp
;
1773 event
->tsc
= tf
->buffer
.tsc
;
1774 event
->compact_data
= 0;
1777 /* Compact header */
1778 /* We keep the LSB of the previous timestamp, to make sure
1779 * we never go back */
1780 event
->event_id
= event
->timestamp
>> tf
->tscbits
;
1781 event
->event_id
= event
->event_id
& ((1 << tf
->trace
->compact_event_bits
) - 1);
1782 event
->compact_data
= event
->timestamp
>>
1783 (tf
->trace
->compact_event_bits
+ tf
->tscbits
);
1784 //printf("tsc bits %u, ev bits %u init data %u\n",
1785 // tf->tscbits, tf->trace->compact_event_bits, event->compact_data);
1786 /* Put the compact data back in original endianness */
1787 event
->compact_data
= ltt_get_uint32(LTT_GET_BO(tf
), &event
->compact_data
);
1788 event
->event_size
= 0xFFFF;
1789 //printf("Found compact event %d\n", event->event_id);
1790 //printf("Compact data %d\n", event->compact_data);
1791 event
->timestamp
= event
->timestamp
<< tf
->tsc_lsb_truncate
;
1792 event
->timestamp
= event
->timestamp
& tf
->tsc_mask
;
1793 //printf("timestamp 0x%lX\n", event->timestamp);
1794 //printf("mask 0x%llX\n", tf->tsc_mask);
1795 //printf("mask_next 0x%llX\n", tf->tsc_mask_next_bit);
1796 //printf("previous tsc 0x%llX\n", tf->buffer.tsc);
1797 //printf("previous tsc&mask 0x%llX\n", tf->tsc_mask&tf->buffer.tsc);
1798 //printf("previous tsc&(~mask) 0x%llX\n", tf->buffer.tsc&(~tf->tsc_mask));
1799 if(event
->timestamp
< (tf
->tsc_mask
&tf
->buffer
.tsc
)) {
1801 tf
->buffer
.tsc
= ((tf
->buffer
.tsc
&(~tf
->tsc_mask
))
1802 + tf
->tsc_mask_next_bit
)
1803 | (guint64
)event
->timestamp
;
1804 event
->tsc
= tf
->buffer
.tsc
;
1806 //printf("no wrap\n");
1808 tf
->buffer
.tsc
= (tf
->buffer
.tsc
&(~tf
->tsc_mask
))
1809 | (guint64
)event
->timestamp
;
1810 event
->tsc
= tf
->buffer
.tsc
;
1812 //printf("current tsc 0x%llX\n", tf->buffer.tsc);
1814 pos
+= sizeof(guint32
);
1816 event
->tsc
= ltt_get_uint64(LTT_GET_BO(tf
), pos
);
1817 tf
->buffer
.tsc
= event
->tsc
;
1818 event
->compact_data
= 0;
1819 pos
+= sizeof(guint64
);
1821 event
->event_time
= ltt_interpolate_time(tf
, event
);
1824 event
->event_id
= *(guint16
*)pos
;
1825 pos
+= sizeof(guint16
);
1827 event
->event_size
= ltt_get_uint16(LTT_GET_BO(tf
), pos
);
1828 pos
+= sizeof(guint16
);
1832 /* Align the head */
1834 pos
+= ltt_align((size_t)pos
, tf
->trace
->arch_size
, tf
->has_alignment
);
1838 /* get the data size and update the event fields with the current
1839 * information. Also update the time if a heartbeat_full event is found. */
1840 ltt_update_event_size(tf
);
1846 /****************************************************************************
1848 * map_block : map a block from the file
1850 * lttdes : ltt trace file
1851 * whichBlock : the block which will be read
1854 * EINVAL : lseek fail
1855 * EIO : can not read from the file
1856 ****************************************************************************/
1858 gint
map_block(LttTracefile
* tf
, guint block_num
)
1860 int page_size
= getpagesize();
1861 struct ltt_block_start_header
*header
;
1863 g_assert(block_num
< tf
->num_blocks
);
1865 if(tf
->buffer
.head
!= NULL
) {
1866 if(munmap(tf
->buffer
.head
, PAGE_ALIGN(tf
->buf_size
))) {
1867 g_warning("unmap size : %u\n",
1868 PAGE_ALIGN(tf
->buf_size
));
1869 perror("munmap error");
1875 /* Multiple of pages aligned head */
1876 tf
->buffer
.head
= mmap(0,
1877 PAGE_ALIGN(tf
->buf_size
),
1878 PROT_READ
, MAP_PRIVATE
, tf
->fd
,
1879 PAGE_ALIGN((off_t
)tf
->buf_size
* (off_t
)block_num
));
1881 if(tf
->buffer
.head
== MAP_FAILED
) {
1882 perror("Error in allocating memory for buffer of tracefile");
1886 g_assert( ( (guint
)tf
->buffer
.head
&(8-1) ) == 0); // make sure it's aligned.
1889 tf
->buffer
.index
= block_num
;
1891 header
= (struct ltt_block_start_header
*)tf
->buffer
.head
;
1894 tf
->buffer
.begin
.timestamp
= ltt_time_add(
1895 ltt_time_from_uint64(
1896 ltt_get_uint64(LTT_GET_BO(tf
),
1897 &header
->begin
.timestamp
)
1898 - tf
->trace
->start_monotonic
),
1899 tf
->trace
->start_time
);
1901 //g_debug("block %u begin : %lu.%lu", block_num,
1902 // tf->buffer.begin.timestamp.tv_sec, tf->buffer.begin.timestamp.tv_nsec);
1903 tf
->buffer
.begin
.cycle_count
= ltt_get_uint64(LTT_GET_BO(tf
),
1904 &header
->begin
.cycle_count
);
1905 tf
->buffer
.begin
.freq
= ltt_get_uint64(LTT_GET_BO(tf
),
1906 &header
->begin
.freq
);
1907 if(tf
->buffer
.begin
.freq
== 0)
1908 tf
->buffer
.begin
.freq
= tf
->trace
->start_freq
;
1910 tf
->buffer
.begin
.timestamp
= ltt_interpolate_time_from_tsc(tf
,
1911 tf
->buffer
.begin
.cycle_count
);
1914 ltt_time_from_uint64(
1915 (double)(tf
->buffer
.begin
.cycle_count
1916 - tf
->trace
->start_tsc
) * 1000000.0
1917 / (double)tf
->trace
->start_freq
),
1918 tf
->trace
->start_time_from_tsc
);
1922 tf
->buffer
.end
.timestamp
= ltt_time_add(
1923 ltt_time_from_uint64(
1924 ltt_get_uint64(LTT_GET_BO(tf
),
1925 &header
->end
.timestamp
)
1926 - tf
->trace
->start_monotonic
),
1927 tf
->trace
->start_time
);
1929 //g_debug("block %u end : %lu.%lu", block_num,
1930 // tf->buffer.end.timestamp.tv_sec, tf->buffer.end.timestamp.tv_nsec);
1931 tf
->buffer
.end
.cycle_count
= ltt_get_uint64(LTT_GET_BO(tf
),
1932 &header
->end
.cycle_count
);
1933 tf
->buffer
.end
.freq
= ltt_get_uint64(LTT_GET_BO(tf
),
1935 if(tf
->buffer
.end
.freq
== 0)
1936 tf
->buffer
.end
.freq
= tf
->trace
->start_freq
;
1938 tf
->buffer
.lost_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1939 &header
->lost_size
);
1940 tf
->buffer
.end
.timestamp
= ltt_interpolate_time_from_tsc(tf
,
1941 tf
->buffer
.end
.cycle_count
);
1944 ltt_time_from_uint64(
1945 (double)(tf
->buffer
.end
.cycle_count
1946 - tf
->trace
->start_tsc
) * 1000000.0
1947 / (double)tf
->trace
->start_freq
),
1948 tf
->trace
->start_time_from_tsc
);
1950 tf
->buffer
.tsc
= tf
->buffer
.begin
.cycle_count
;
1951 tf
->event
.tsc
= tf
->buffer
.tsc
;
1952 tf
->buffer
.freq
= tf
->buffer
.begin
.freq
;
1955 * eventually support variable buffer size : will need a partial pre-read of
1956 * the headers to create an index when we open the trace... eventually. */
1957 g_assert(tf
->buf_size
== ltt_get_uint32(LTT_GET_BO(tf
),
1958 &header
->buf_size
));
1960 /* Now that the buffer is mapped, calculate the time interpolation for the
1963 // tf->buffer.nsecs_per_cycle = calc_nsecs_per_cycle(tf);
1964 //tf->buffer.cyc2ns_scale = calc_nsecs_per_cycle(tf);
1966 /* Make the current event point to the beginning of the buffer :
1967 * it means that the event read must get the first event. */
1968 tf
->event
.tracefile
= tf
;
1969 tf
->event
.block
= block_num
;
1970 tf
->event
.offset
= 0;
1979 /* It will update the fields offsets too */
1980 void ltt_update_event_size(LttTracefile
*tf
)
1984 struct marker_info
*info
;
1986 switch((enum marker_id
)tf
->event
.event_id
) {
1987 case MARKER_ID_SET_MARKER_ID
:
1988 size
= strlen((char*)tf
->event
.data
) + 1;
1989 //g_debug("marker %s id set", (char*)tf->event.data);
1990 size
+= ltt_align(size
, sizeof(guint16
), tf
->has_alignment
);
1991 size
+= sizeof(guint16
);
1992 size
+= sizeof(guint8
);
1993 size
+= sizeof(guint8
);
1994 size
+= sizeof(guint8
);
1995 size
+= sizeof(guint8
);
1996 size
+= sizeof(guint8
);
1998 case MARKER_ID_SET_MARKER_FORMAT
:
1999 //g_debug("marker %s format set", (char*)tf->event.data);
2000 size
= strlen((char*)tf
->event
.data
) + 1;
2001 size
+= strlen((char*)tf
->event
.data
) + 1;
2003 case MARKER_ID_HEARTBEAT_32
:
2004 //g_debug("Update Event heartbeat 32 bits");
2005 size
= ltt_align(size
, sizeof(guint32
), tf
->has_alignment
);
2006 size
+= sizeof(guint32
);
2008 case MARKER_ID_HEARTBEAT_64
:
2009 //g_debug("Update Event heartbeat 64 bits");
2010 tscdata
= (char*)(tf
->event
.data
);
2011 tf
->event
.tsc
= ltt_get_uint64(LTT_GET_BO(tf
), tscdata
);
2012 tf
->buffer
.tsc
= tf
->event
.tsc
;
2013 tf
->event
.event_time
= ltt_interpolate_time(tf
, &tf
->event
);
2014 size
= ltt_align(size
, sizeof(guint64
), tf
->has_alignment
);
2015 size
+= sizeof(guint64
);
2018 info
= marker_get_info_from_id(tf
->trace
, tf
->event
.event_id
);
2019 g_assert(info
!= NULL
);
2020 if (info
->size
!= -1) {
2023 size
= marker_update_fields_offsets(marker_get_info_from_id(tf
->trace
,
2024 tf
->event
.event_id
), tf
->event
.data
);
2028 tf
->event
.data_size
= size
;
2030 /* Check consistency between kernel and LTTV structure sizes */
2031 if(tf
->event
.event_size
== 0xFFFF) {
2032 /* Event size too big to fit in the event size field */
2033 tf
->event
.event_size
= tf
->event
.data_size
;
2035 if (tf
->event
.data_size
!= tf
->event
.event_size
) {
2036 struct marker_info
*info
= marker_get_info_from_id(tf
->trace
,
2037 tf
->event
.event_id
);
2038 g_error("Kernel/LTTV event size differs for event %s: kernel %u, LTTV %u",
2039 g_quark_to_string(info
->name
),
2040 tf
->event
.event_size
, tf
->event
.data_size
);
2045 LttEventType
*event_type
=
2046 ltt_facility_eventtype_get(f
, tf
->event
.event_id
);
2049 g_warning("Unknown event id %hhu in facility %s in tracefile %s",
2051 g_quark_to_string(f
->name
),
2052 g_quark_to_string(tf
->name
));
2053 goto event_type_error
;
2056 /* Compute the dynamic offsets */
2057 compute_offsets(tf
, f
, event_type
, &size
, tf
->event
.data
);
2059 //g_debug("Event root field : f.e %hhu.%hhu size %zd",
2060 // tf->event.facility_id,
2061 // tf->event.event_id, size);
2064 tf
->event
.data_size
= size
;
2066 /* Check consistency between kernel and LTTV structure sizes */
2067 if(tf
->event
.event_size
== 0xFFFF) {
2068 /* Event size too big to fit in the event size field */
2069 tf
->event
.event_size
= tf
->event
.data_size
;
2071 if (tf
->event
.data_size
!= tf
->event
.event_size
) {
2072 g_error("Kernel/LTTV event size differs for event %s.%s: kernel %u, LTTV %u",
2073 g_quark_to_string(f
->name
), g_quark_to_string(event_type
->name
),
2074 tf
->event
.event_size
, tf
->event
.data_size
);
2077 //g_assert(tf->event.data_size == tf->event.event_size);
2083 if(tf
->event
.event_size
== 0xFFFF) {
2084 g_error("Cannot jump over an unknown event bigger than 0xFFFE bytes");
2086 /* The facility is unknown : use the kernel information about this event
2087 * to jump over it. */
2088 tf
->event
.data_size
= tf
->event
.event_size
;
2093 /* Take the tf current event offset and use the event facility id and event id
2094 * to figure out where is the next event offset.
2096 * This is an internal function not aiming at being used elsewhere : it will
2097 * not jump over the current block limits. Please consider using
2098 * ltt_tracefile_read to do this.
2100 * Returns 0 on success
2101 * ERANGE if we are at the end of the buffer.
2102 * ENOPROTOOPT if an error occured when getting the current event size.
2104 int ltt_seek_next_event(LttTracefile
*tf
)
2109 /* seek over the buffer header if we are at the buffer start */
2110 if(tf
->event
.offset
== 0) {
2111 tf
->event
.offset
+= tf
->buffer_header_size
;
2113 if(tf
->event
.offset
== tf
->buf_size
- tf
->buffer
.lost_size
) {
2120 pos
= tf
->event
.data
;
2122 if(tf
->event
.data_size
< 0) goto error
;
2124 pos
+= (size_t)tf
->event
.data_size
;
2126 tf
->event
.offset
= pos
- tf
->buffer
.head
;
2128 if(tf
->event
.offset
== tf
->buf_size
- tf
->buffer
.lost_size
) {
2132 g_assert(tf
->event
.offset
< tf
->buf_size
- tf
->buffer
.lost_size
);
2138 g_error("Error in ltt_seek_next_event for tracefile %s",
2139 g_quark_to_string(tf
->name
));
2144 /*****************************************************************************
2146 * calc_nsecs_per_cycle : calculate nsecs per cycle for current block
2148 * 1.0 / (freq(khz) *1000) * 1000000000
2151 ****************************************************************************/
2152 /* from timer_tsc.c */
2153 #define CYC2NS_SCALE_FACTOR 10
2154 static guint32
calc_nsecs_per_cycle(LttTracefile
* tf
)
2156 //return 1e6 / (double)tf->buffer.freq;
2157 guint32 cpu_mhz
= tf
->buffer
.freq
/ 1000;
2158 guint32 cyc2ns_scale
= (1000 << CYC2NS_SCALE_FACTOR
)/cpu_mhz
;
2160 return cyc2ns_scale
;
2161 // return 1e6 / (double)tf->buffer.freq;
2164 static guint64
cycles_2_ns(LttTracefile
*tf
, guint64 cycles
)
2166 return (cycles
* tf
->buffer
.cyc2ns_scale
) >> CYC2NS_SCALE_FACTOR
;
2171 void setFieldsOffset(LttTracefile
*tf
, LttEventType
*evT
,void *evD
)
2173 LttField
* rootFld
= evT
->root_field
;
2174 // rootFld->base_address = evD;
2177 rootFld
->field_size
= getFieldtypeSize(tf
, evT
->facility
,
2178 evT
, 0,0,rootFld
, evD
);
2182 /*****************************************************************************
2184 * set_fields_offsets : set the precomputable offset of the fields
2186 * tracefile : opened trace file
2187 * event_type : the event type
2188 ****************************************************************************/
2190 void set_fields_offsets(LttTracefile
*tf
, LttEventType
*event_type
)
2192 LttField
*field
= event_type
->root_field
;
2193 enum field_status fixed_root
= FIELD_FIXED
, fixed_parent
= FIELD_FIXED
;
2196 preset_field_type_size(tf
, event_type
, 0, 0,
2197 &fixed_root
, &fixed_parent
,
2204 /*****************************************************************************
2206 * get_alignment : Get the alignment needed for a field.
2210 * returns : The size on which it must be aligned.
2212 ****************************************************************************/
2214 off_t
get_alignment(LttField
*field
)
2216 LttType
*type
= &field
->field_type
;
2218 switch(type
->type_class
) {
2220 case LTT_UINT_FIXED
:
2235 /* Align offset on type size */
2236 g_assert(field
->field_size
!= 0);
2237 return field
->field_size
;
2243 g_assert(type
->fields
->len
== 1);
2245 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
2246 return get_alignment(child
);
2250 g_assert(type
->fields
->len
== 2);
2253 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
2255 localign
= max(localign
, get_alignment(child
));
2257 child
= &g_array_index(type
->fields
, LttField
, 1);
2258 localign
= max(localign
, get_alignment(child
));
2269 for(i
=0; i
<type
->fields
->len
; i
++) {
2270 LttField
*child
= &g_array_index(type
->fields
, LttField
, i
);
2271 localign
= max(localign
, get_alignment(child
));
2278 g_error("get_alignment : unknown type");
2285 /*****************************************************************************
2287 * field_compute_static_size : Determine the size of fields known by their
2288 * sole definition. Unions, arrays and struct sizes might be known, but
2289 * the parser does not give that information.
2294 ****************************************************************************/
2296 void field_compute_static_size(LttFacility
*fac
, LttField
*field
)
2298 LttType
*type
= &field
->field_type
;
2300 switch(type
->type_class
) {
2302 case LTT_UINT_FIXED
:
2321 /* note this : array type size is the number of elements in the array,
2322 * while array field size of the length of the array in bytes */
2323 g_assert(type
->fields
->len
== 1);
2325 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
2326 field_compute_static_size(fac
, child
);
2328 if(child
->field_size
!= 0) {
2329 field
->field_size
= type
->size
* child
->field_size
;
2330 field
->dynamic_offsets
= g_array_sized_new(FALSE
, TRUE
,
2331 sizeof(off_t
), type
->size
);
2333 field
->field_size
= 0;
2338 g_assert(type
->fields
->len
== 2);
2340 off_t local_offset
= 0;
2341 LttField
*child
= &g_array_index(type
->fields
, LttField
, 1);
2342 field_compute_static_size(fac
, child
);
2343 field
->field_size
= 0;
2345 if(child
->field_size
!= 0) {
2346 field
->dynamic_offsets
= g_array_sized_new(FALSE
, TRUE
,
2347 sizeof(off_t
), SEQUENCE_AVG_ELEMENTS
);
2355 for(i
=0;i
<type
->fields
->len
;i
++) {
2356 LttField
*child
= &g_array_index(type
->fields
, LttField
, i
);
2357 field_compute_static_size(fac
, child
);
2358 if(child
->field_size
!= 0) {
2359 type
->size
+= ltt_align(type
->size
, get_alignment(child
),
2361 type
->size
+= child
->field_size
;
2363 /* As soon as we find a child with variable size, we have
2364 * a variable size */
2369 field
->field_size
= type
->size
;
2373 g_error("field_static_size : unknown type");
2380 /*****************************************************************************
2382 * precompute_fields_offsets : set the precomputable offset of the fields
2386 * offset : pointer to the current offset, must be incremented
2388 * return : 1 : found a variable length field, stop the processing.
2390 ****************************************************************************/
2393 gint
precompute_fields_offsets(LttFacility
*fac
, LttField
*field
, off_t
*offset
, gint is_compact
)
2395 LttType
*type
= &field
->field_type
;
2397 if(unlikely(is_compact
)) {
2398 g_assert(field
->field_size
!= 0);
2399 /* FIXME THIS IS A HUUUUUGE hack :
2400 * offset is between the compact_data field in struct LttEvent
2401 * and the address of the field root in the memory map.
2402 * ark. Both will stay at the same addresses while the event
2403 * is readable, so it's ok.
2405 field
->offset_root
= 0;
2406 field
->fixed_root
= FIELD_FIXED
;
2410 switch(type
->type_class
) {
2412 case LTT_UINT_FIXED
:
2427 g_assert(field
->field_size
!= 0);
2428 /* Align offset on type size */
2429 *offset
+= ltt_align(*offset
, get_alignment(field
),
2431 /* remember offset */
2432 field
->offset_root
= *offset
;
2433 field
->fixed_root
= FIELD_FIXED
;
2434 /* Increment offset */
2435 *offset
+= field
->field_size
;
2439 field
->offset_root
= *offset
;
2440 field
->fixed_root
= FIELD_FIXED
;
2444 g_assert(type
->fields
->len
== 1);
2446 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
2448 *offset
+= ltt_align(*offset
, get_alignment(field
),
2451 /* remember offset */
2452 field
->offset_root
= *offset
;
2453 field
->array_offset
= *offset
;
2454 field
->fixed_root
= FIELD_FIXED
;
2456 /* Let the child be variable */
2457 //precompute_fields_offsets(tf, child, offset);
2459 if(field
->field_size
!= 0) {
2460 /* Increment offset */
2461 /* field_size is the array size in bytes */
2462 *offset
+= field
->field_size
;
2470 g_assert(type
->fields
->len
== 2);
2475 *offset
+= ltt_align(*offset
, get_alignment(field
),
2478 /* remember offset */
2479 field
->offset_root
= *offset
;
2480 field
->fixed_root
= FIELD_FIXED
;
2482 child
= &g_array_index(type
->fields
, LttField
, 0);
2483 ret
= precompute_fields_offsets(fac
, child
, offset
, is_compact
);
2484 g_assert(ret
== 0); /* Seq len cannot have variable len */
2486 child
= &g_array_index(type
->fields
, LttField
, 1);
2487 *offset
+= ltt_align(*offset
, get_alignment(child
),
2489 field
->array_offset
= *offset
;
2490 /* Let the child be variable. */
2491 //ret = precompute_fields_offsets(fac, child, offset);
2493 /* Cannot precompute fields offsets of sequence members, and has
2494 * variable length. */
2504 *offset
+= ltt_align(*offset
, get_alignment(field
),
2506 /* remember offset */
2507 field
->offset_root
= *offset
;
2508 field
->fixed_root
= FIELD_FIXED
;
2510 for(i
=0; i
< type
->fields
->len
; i
++) {
2511 child
= &g_array_index(type
->fields
, LttField
, i
);
2512 ret
= precompute_fields_offsets(fac
, child
, offset
, is_compact
);
2525 *offset
+= ltt_align(*offset
, get_alignment(field
),
2527 /* remember offset */
2528 field
->offset_root
= *offset
;
2529 field
->fixed_root
= FIELD_FIXED
;
2531 for(i
=0; i
< type
->fields
->len
; i
++) {
2532 *offset
= field
->offset_root
;
2533 child
= &g_array_index(type
->fields
, LttField
, i
);
2534 ret
= precompute_fields_offsets(fac
, child
, offset
, is_compact
);
2538 *offset
= field
->offset_root
+ field
->field_size
;
2545 g_error("precompute_fields_offsets : unknown type");
2554 /*****************************************************************************
2556 * precompute_offsets : set the precomputable offset of an event type
2559 * event : event type
2561 ****************************************************************************/
2562 void precompute_offsets(LttFacility
*fac
, LttEventType
*event
)
2568 /* First, compute the size of fixed size fields. Will determine size for
2569 * arrays, struct and unions, which is not done by the parser */
2570 for(i
=0; i
<event
->fields
->len
; i
++) {
2571 LttField
*field
= &g_array_index(event
->fields
, LttField
, i
);
2572 field_compute_static_size(fac
, field
);
2575 /* Precompute all known offsets */
2576 for(i
=0; i
<event
->fields
->len
; i
++) {
2577 LttField
*field
= &g_array_index(event
->fields
, LttField
, i
);
2578 if(event
->has_compact_data
&& i
== 0)
2579 ret
= precompute_fields_offsets(fac
, field
, &offset
, 1);
2581 ret
= precompute_fields_offsets(fac
, field
, &offset
, 0);
2589 /*****************************************************************************
2591 * preset_field_type_size : set the fixed sizes of the field type
2594 * event_type : event type
2595 * offset_root : offset from the root
2596 * offset_parent : offset from the parent
2597 * fixed_root : Do we know a fixed offset to the root ?
2598 * fixed_parent : Do we know a fixed offset to the parent ?
2600 ****************************************************************************/
2604 // preset the fixed size offsets. Calculate them just like genevent-new : an
2605 // increment of a *to value that represents the offset from the start of the
2607 // The preset information is : offsets up to (and including) the first element
2608 // of variable size. All subsequent fields must be flagged "VARIABLE OFFSET".
2610 void preset_field_type_size(LttTracefile
*tf
, LttEventType
*event_type
,
2611 off_t offset_root
, off_t offset_parent
,
2612 enum field_status
*fixed_root
, enum field_status
*fixed_parent
,
2615 enum field_status local_fixed_root
, local_fixed_parent
;
2619 g_assert(field
->fixed_root
== FIELD_UNKNOWN
);
2620 g_assert(field
->fixed_parent
== FIELD_UNKNOWN
);
2621 g_assert(field
->fixed_size
== FIELD_UNKNOWN
);
2623 type
= field
->field_type
;
2625 field
->fixed_root
= *fixed_root
;
2626 if(field
->fixed_root
== FIELD_FIXED
)
2627 field
->offset_root
= offset_root
;
2629 field
->offset_root
= 0;
2631 field
->fixed_parent
= *fixed_parent
;
2632 if(field
->fixed_parent
== FIELD_FIXED
)
2633 field
->offset_parent
= offset_parent
;
2635 field
->offset_parent
= 0;
2637 size_t current_root_offset
;
2638 size_t current_offset
;
2639 enum field_status current_child_status
, final_child_status
;
2642 switch(type
->type_class
) {
2644 case LTT_UINT_FIXED
:
2653 field
->field_size
= ltt_type_size(tf
->trace
, type
);
2654 field
->fixed_size
= FIELD_FIXED
;
2657 field
->field_size
= (off_t
)event_type
->facility
->pointer_size
;
2658 field
->fixed_size
= FIELD_FIXED
;
2662 field
->field_size
= (off_t
)event_type
->facility
->long_size
;
2663 field
->fixed_size
= FIELD_FIXED
;
2668 field
->field_size
= (off_t
)event_type
->facility
->size_t_size
;
2669 field
->fixed_size
= FIELD_FIXED
;
2672 local_fixed_root
= FIELD_VARIABLE
;
2673 local_fixed_parent
= FIELD_VARIABLE
;
2674 preset_field_type_size(tf
, event_type
,
2676 &local_fixed_root
, &local_fixed_parent
,
2678 field
->fixed_size
= FIELD_VARIABLE
;
2679 field
->field_size
= 0;
2680 *fixed_root
= FIELD_VARIABLE
;
2681 *fixed_parent
= FIELD_VARIABLE
;
2684 field
->fixed_size
= FIELD_VARIABLE
;
2685 field
->field_size
= 0;
2686 *fixed_root
= FIELD_VARIABLE
;
2687 *fixed_parent
= FIELD_VARIABLE
;
2690 local_fixed_root
= FIELD_VARIABLE
;
2691 local_fixed_parent
= FIELD_VARIABLE
;
2692 preset_field_type_size(tf
, event_type
,
2694 &local_fixed_root
, &local_fixed_parent
,
2696 field
->fixed_size
= field
->child
[0]->fixed_size
;
2697 if(field
->fixed_size
== FIELD_FIXED
) {
2698 field
->field_size
= type
->element_number
* field
->child
[0]->field_size
;
2700 field
->field_size
= 0;
2701 *fixed_root
= FIELD_VARIABLE
;
2702 *fixed_parent
= FIELD_VARIABLE
;
2706 current_root_offset
= field
->offset_root
;
2708 current_child_status
= FIELD_FIXED
;
2709 for(i
=0;i
<type
->element_number
;i
++) {
2710 preset_field_type_size(tf
, event_type
,
2711 current_root_offset
, current_offset
,
2712 fixed_root
, ¤t_child_status
,
2714 if(current_child_status
== FIELD_FIXED
) {
2715 current_root_offset
+= field
->child
[i
]->field_size
;
2716 current_offset
+= field
->child
[i
]->field_size
;
2718 current_root_offset
= 0;
2722 if(current_child_status
!= FIELD_FIXED
) {
2723 *fixed_parent
= current_child_status
;
2724 field
->field_size
= 0;
2725 field
->fixed_size
= current_child_status
;
2727 field
->field_size
= current_offset
;
2728 field
->fixed_size
= FIELD_FIXED
;
2732 current_root_offset
= field
->offset_root
;
2735 final_child_status
= FIELD_FIXED
;
2736 for(i
=0;i
<type
->element_number
;i
++) {
2737 enum field_status current_root_child_status
= FIELD_FIXED
;
2738 enum field_status current_child_status
= FIELD_FIXED
;
2739 preset_field_type_size(tf
, event_type
,
2740 current_root_offset
, current_offset
,
2741 ¤t_root_child_status
, ¤t_child_status
,
2743 if(current_child_status
!= FIELD_FIXED
)
2744 final_child_status
= current_child_status
;
2746 max_size
= max(max_size
, field
->child
[i
]->field_size
);
2748 if(final_child_status
!= FIELD_FIXED
) {
2749 g_error("LTTV does not support variable size fields in unions.");
2750 /* This will stop the application. */
2751 *fixed_root
= final_child_status
;
2752 *fixed_parent
= final_child_status
;
2753 field
->field_size
= 0;
2754 field
->fixed_size
= current_child_status
;
2756 field
->field_size
= max_size
;
2757 field
->fixed_size
= FIELD_FIXED
;
2761 g_error("unexpected type NONE");
2768 /*****************************************************************************
2770 * check_fields_compatibility : Check for compatibility between two fields :
2771 * do they use the same inner structure ?
2773 * event_type1 : event type
2774 * event_type2 : event type
2777 *Returns : 0 if identical
2779 ****************************************************************************/
2780 // this function checks for equality of field types. Therefore, it does not use
2781 // per se offsets. For instance, an aligned version of a structure is
2782 // compatible with an unaligned version of the same structure.
2784 gint
check_fields_compatibility(LttEventType
*event_type1
,
2785 LttEventType
*event_type2
,
2786 LttField
*field1
, LttField
*field2
)
2788 guint different
= 0;
2792 if(field1
== NULL
) {
2793 if(field2
== NULL
) goto end
;
2798 } else if(field2
== NULL
) {
2803 type1
= &field1
->field_type
;
2804 type2
= &field2
->field_type
;
2806 if(type1
->type_class
!= type2
->type_class
) {
2810 if(type1
->network
!= type2
->network
) {
2815 switch(type1
->type_class
) {
2817 case LTT_UINT_FIXED
:
2832 if(field1
->field_size
!= field2
->field_size
)
2839 LttField
*child1
= &g_array_index(type1
->fields
, LttField
, 0);
2840 LttField
*child2
= &g_array_index(type2
->fields
, LttField
, 0);
2842 if(type1
->size
!= type2
->size
)
2844 if(check_fields_compatibility(event_type1
, event_type2
, child1
, child2
))
2850 LttField
*child1
= &g_array_index(type1
->fields
, LttField
, 1);
2851 LttField
*child2
= &g_array_index(type2
->fields
, LttField
, 1);
2853 if(check_fields_compatibility(event_type1
, event_type2
, child1
, child2
))
2863 if(type1
->fields
->len
!= type2
->fields
->len
) {
2868 for(i
=0; i
< type1
->fields
->len
; i
++) {
2871 child1
= &g_array_index(type1
->fields
, LttField
, i
);
2872 child2
= &g_array_index(type2
->fields
, LttField
, i
);
2873 different
= check_fields_compatibility(event_type1
,
2874 event_type2
, child1
, child2
);
2876 if(different
) break;
2882 g_error("check_fields_compatibility : unknown type");
2891 gint
check_fields_compatibility(LttEventType
*event_type1
,
2892 LttEventType
*event_type2
,
2893 LttField
*field1
, LttField
*field2
)
2895 guint different
= 0;
2900 if(field1
== NULL
) {
2901 if(field2
== NULL
) goto end
;
2906 } else if(field2
== NULL
) {
2911 g_assert(field1
->fixed_root
!= FIELD_UNKNOWN
);
2912 g_assert(field2
->fixed_root
!= FIELD_UNKNOWN
);
2913 g_assert(field1
->fixed_parent
!= FIELD_UNKNOWN
);
2914 g_assert(field2
->fixed_parent
!= FIELD_UNKNOWN
);
2915 g_assert(field1
->fixed_size
!= FIELD_UNKNOWN
);
2916 g_assert(field2
->fixed_size
!= FIELD_UNKNOWN
);
2918 type1
= field1
->field_type
;
2919 type2
= field2
->field_type
;
2921 if(type1
->type_class
!= type2
->type_class
) {
2925 if(type1
->element_name
!= type2
->element_name
) {
2930 switch(type1
->type_class
) {
2932 case LTT_UINT_FIXED
:
2947 if(field1
->field_size
!= field2
->field_size
) {
2953 if(type1
->element_number
!= type2
->element_number
) {
2957 for(i
=0;i
<type1
->element_number
;i
++) {
2958 if(type1
->enum_strings
[i
] != type2
->enum_strings
[i
]) {
2965 /* Two elements : size and child */
2966 g_assert(type1
->element_number
!= type2
->element_number
);
2967 for(i
=0;i
<type1
->element_number
;i
++) {
2968 if(check_fields_compatibility(event_type1
, event_type2
,
2969 field1
->child
[0], field2
->child
[0])) {
2978 if(field1
->field_size
!= field2
->field_size
) {
2982 /* Two elements : size and child */
2983 g_assert(type1
->element_number
!= type2
->element_number
);
2984 for(i
=0;i
<type1
->element_number
;i
++) {
2985 if(check_fields_compatibility(event_type1
, event_type2
,
2986 field1
->child
[0], field2
->child
[0])) {
2994 if(type1
->element_number
!= type2
->element_number
) {
2998 for(i
=0;i
<type1
->element_number
;i
++) {
2999 if(check_fields_compatibility(event_type1
, event_type2
,
3000 field1
->child
[0], field2
->child
[0])) {
3013 /*****************************************************************************
3015 * ltt_get_int : get an integer number
3017 * reverse_byte_order: must we reverse the byte order ?
3018 * size : the size of the integer
3019 * ptr : the data pointer
3021 * gint64 : a 64 bits integer
3022 ****************************************************************************/
3024 gint64
ltt_get_int(gboolean reverse_byte_order
, gint size
, void *data
)
3029 case 1: val
= *((gint8
*)data
); break;
3030 case 2: val
= ltt_get_int16(reverse_byte_order
, data
); break;
3031 case 4: val
= ltt_get_int32(reverse_byte_order
, data
); break;
3032 case 8: val
= ltt_get_int64(reverse_byte_order
, data
); break;
3033 default: val
= ltt_get_int64(reverse_byte_order
, data
);
3034 g_critical("get_int : integer size %d unknown", size
);
3041 /*****************************************************************************
3043 * ltt_get_uint : get an unsigned integer number
3045 * reverse_byte_order: must we reverse the byte order ?
3046 * size : the size of the integer
3047 * ptr : the data pointer
3049 * guint64 : a 64 bits unsigned integer
3050 ****************************************************************************/
3052 guint64
ltt_get_uint(gboolean reverse_byte_order
, gint size
, void *data
)
3057 case 1: val
= *((gint8
*)data
); break;
3058 case 2: val
= ltt_get_uint16(reverse_byte_order
, data
); break;
3059 case 4: val
= ltt_get_uint32(reverse_byte_order
, data
); break;
3060 case 8: val
= ltt_get_uint64(reverse_byte_order
, data
); break;
3061 default: val
= ltt_get_uint64(reverse_byte_order
, data
);
3062 g_critical("get_uint : unsigned integer size %d unknown",
3071 /* get the node name of the system */
3073 char * ltt_trace_system_description_node_name (LttSystemDescription
* s
)
3075 return s
->node_name
;
3079 /* get the domain name of the system */
3081 char * ltt_trace_system_description_domain_name (LttSystemDescription
* s
)
3083 return s
->domain_name
;
3087 /* get the description of the system */
3089 char * ltt_trace_system_description_description (LttSystemDescription
* s
)
3091 return s
->description
;
3095 /* get the NTP corrected start time of the trace */
3096 LttTime
ltt_trace_start_time(LttTrace
*t
)
3098 return t
->start_time
;
3101 /* get the monotonic start time of the trace */
3102 LttTime
ltt_trace_start_time_monotonic(LttTrace
*t
)
3104 return t
->start_time_from_tsc
;
3107 LttTracefile
*ltt_tracefile_new()
3109 return g_new(LttTracefile
, 1);
3112 void ltt_tracefile_destroy(LttTracefile
*tf
)
3117 void ltt_tracefile_copy(LttTracefile
*dest
, const LttTracefile
*src
)
3122 /* Before library loading... */
3126 LTT_FACILITY_NAME_HEARTBEAT
= g_quark_from_string("heartbeat");
3127 LTT_EVENT_NAME_HEARTBEAT
= g_quark_from_string("heartbeat");
3128 LTT_EVENT_NAME_HEARTBEAT_FULL
= g_quark_from_string("heartbeat_full");
3130 LTT_TRACEFILE_NAME_FACILITIES
= g_quark_from_string("/control/facilities");