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/facility.h>
48 #include <ltt/event.h>
50 #include <ltt/ltt-types.h>
53 /* Facility names used in this file */
55 GQuark LTT_FACILITY_NAME_HEARTBEAT
,
56 LTT_EVENT_NAME_HEARTBEAT
;
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
);
109 void precompute_offsets(LttFacility
*fac
, LttEventType
*event
);
112 /* Functions to parse system.xml file (using glib xml parser) */
113 static void parser_start_element (GMarkupParseContext __UNUSED__
*context
,
114 const gchar
*element_name
,
115 const gchar
**attribute_names
,
116 const gchar
**attribute_values
,
121 LttSystemDescription
* des
= (LttSystemDescription
* )user_data
;
122 if(strcmp("system", element_name
)){
123 *error
= g_error_new(G_MARKUP_ERROR
,
125 "This is not system.xml file");
129 while(attribute_names
[i
]){
130 if(strcmp("node_name", attribute_names
[i
])==0){
131 des
->node_name
= g_strdup(attribute_values
[i
]);
132 }else if(strcmp("domainname", attribute_names
[i
])==0){
133 des
->domain_name
= g_strdup(attribute_values
[i
]);
134 }else if(strcmp("cpu", attribute_names
[i
])==0){
135 des
->nb_cpu
= atoi(attribute_values
[i
]);
136 }else if(strcmp("arch_size", attribute_names
[i
])==0){
137 if(strcmp(attribute_values
[i
],"LP32") == 0) des
->size
= LTT_LP32
;
138 else if(strcmp(attribute_values
[i
],"ILP32") == 0) des
->size
= LTT_ILP32
;
139 else if(strcmp(attribute_values
[i
],"LP64") == 0) des
->size
= LTT_LP64
;
140 else if(strcmp(attribute_values
[i
],"ILP64") == 0) des
->size
= LTT_ILP64
;
141 else if(strcmp(attribute_values
[i
],"UNKNOWN") == 0) des
->size
= LTT_UNKNOWN
;
142 }else if(strcmp("endian", attribute_names
[i
])==0){
143 if(strcmp(attribute_values
[i
],"LITTLE_ENDIAN") == 0)
144 des
->endian
= LTT_LITTLE_ENDIAN
;
145 else if(strcmp(attribute_values
[i
],"BIG_ENDIAN") == 0)
146 des
->endian
= LTT_BIG_ENDIAN
;
147 }else if(strcmp("kernel_name", attribute_names
[i
])==0){
148 des
->kernel_name
= g_strdup(attribute_values
[i
]);
149 }else if(strcmp("kernel_release", attribute_names
[i
])==0){
150 des
->kernel_release
= g_strdup(attribute_values
[i
]);
151 }else if(strcmp("kernel_version", attribute_names
[i
])==0){
152 des
->kernel_version
= g_strdup(attribute_values
[i
]);
153 }else if(strcmp("machine", attribute_names
[i
])==0){
154 des
->machine
= g_strdup(attribute_values
[i
]);
155 }else if(strcmp("processor", attribute_names
[i
])==0){
156 des
->processor
= g_strdup(attribute_values
[i
]);
157 }else if(strcmp("hardware_platform", attribute_names
[i
])==0){
158 des
->hardware_platform
= g_strdup(attribute_values
[i
]);
159 }else if(strcmp("operating_system", attribute_names
[i
])==0){
160 des
->operating_system
= g_strdup(attribute_values
[i
]);
161 }else if(strcmp("ltt_major_version", attribute_names
[i
])==0){
162 des
->ltt_major_version
= atoi(attribute_values
[i
]);
163 }else if(strcmp("ltt_minor_version", attribute_names
[i
])==0){
164 des
->ltt_minor_version
= atoi(attribute_values
[i
]);
165 }else if(strcmp("ltt_block_size", attribute_names
[i
])==0){
166 des
->ltt_block_size
= atoi(attribute_values
[i
]);
168 *error
= g_error_new(G_MARKUP_ERROR
,
170 "Not a valid attribute");
177 static void parser_characters (GMarkupParseContext __UNUSED__
*context
,
179 gsize __UNUSED__ text_len
,
181 GError __UNUSED__
**error
)
183 LttSystemDescription
* des
= (LttSystemDescription
* )user_data
;
184 des
->description
= g_strdup(text
);
189 LttFacility
*ltt_trace_get_facility_by_num(LttTrace
*t
,
192 g_assert(num
< t
->facilities_by_num
->len
);
194 return &g_array_index(t
->facilities_by_num
, LttFacility
, num
);
198 guint
ltt_trace_get_num_cpu(LttTrace
*t
)
206 * Return value : 0 success, 1 bad tracefile
208 int parse_trace_header(void *header
, LttTracefile
*tf
, LttTrace
*t
)
210 guint32
*magic_number
= (guint32
*)header
;
211 struct ltt_trace_header_any
*any
= (struct ltt_trace_header_any
*)header
;
213 if(*magic_number
== LTT_MAGIC_NUMBER
)
215 else if(*magic_number
== LTT_REV_MAGIC_NUMBER
)
217 else /* invalid magic number, bad tracefile ! */
220 /* Get float byte order : might be different from int byte order
221 * (or is set to 0 if the trace has no float (kernel trace)) */
222 tf
->float_word_order
= any
->float_word_order
;
223 tf
->has_alignment
= any
->has_alignment
;
226 t
->arch_type
= ltt_get_uint32(LTT_GET_BO(tf
),
228 t
->arch_variant
= ltt_get_uint32(LTT_GET_BO(tf
),
230 t
->arch_size
= any
->arch_size
;
231 t
->ltt_major_version
= any
->major_version
;
232 t
->ltt_minor_version
= any
->minor_version
;
233 t
->flight_recorder
= any
->flight_recorder
;
234 t
->has_heartbeat
= any
->has_heartbeat
;
238 switch(any
->major_version
) {
241 switch(any
->minor_version
) {
244 tf
->buffer_header_size
=
245 sizeof(struct ltt_block_start_header
)
246 + sizeof(struct ltt_trace_header_0_3
);
247 g_warning("Unsupported trace version : %hhu.%hhu",
248 any
->major_version
, any
->minor_version
);
254 struct ltt_trace_header_0_7
*vheader
=
255 (struct ltt_trace_header_0_7
*)header
;
256 tf
->buffer_header_size
=
257 sizeof(struct ltt_block_start_header
)
258 + sizeof(struct ltt_trace_header_0_7
);
260 t
->start_freq
= ltt_get_uint64(LTT_GET_BO(tf
),
261 &vheader
->start_freq
);
262 t
->freq_scale
= ltt_get_uint32(LTT_GET_BO(tf
),
263 &vheader
->freq_scale
);
264 t
->start_tsc
= ltt_get_uint64(LTT_GET_BO(tf
),
265 &vheader
->start_tsc
);
266 t
->start_monotonic
= ltt_get_uint64(LTT_GET_BO(tf
),
267 &vheader
->start_monotonic
);
268 t
->start_time
.tv_sec
= ltt_get_uint64(LTT_GET_BO(tf
),
269 &vheader
->start_time_sec
);
270 t
->start_time
.tv_nsec
= ltt_get_uint64(LTT_GET_BO(tf
),
271 &vheader
->start_time_usec
);
272 t
->start_time
.tv_nsec
*= 1000; /* microsec to nanosec */
274 t
->start_time_from_tsc
= ltt_time_from_uint64(
275 (double)t
->start_tsc
* 1000000.0 / (double)t
->start_freq
);
280 g_warning("Unsupported trace version : %hhu.%hhu",
281 any
->major_version
, any
->minor_version
);
287 g_warning("Unsupported trace version : %hhu.%hhu",
288 any
->major_version
, any
->minor_version
);
298 /*****************************************************************************
300 * ltt_tracefile_open : open a trace file, construct a LttTracefile
302 * t : the trace containing the tracefile
303 * fileName : path name of the trace file
304 * tf : the tracefile structure
306 * : 0 for success, -1 otherwise.
307 ****************************************************************************/
309 gint
ltt_tracefile_open(LttTrace
*t
, gchar
* fileName
, LttTracefile
*tf
)
311 struct stat lTDFStat
; /* Trace data file status */
312 struct ltt_block_start_header
*header
;
313 int page_size
= getpagesize();
316 tf
->long_name
= g_quark_from_string(fileName
);
318 tf
->fd
= open(fileName
, O_RDONLY
);
320 g_warning("Unable to open input data file %s\n", fileName
);
324 // Get the file's status
325 if(fstat(tf
->fd
, &lTDFStat
) < 0){
326 g_warning("Unable to get the status of the input data file %s\n", fileName
);
330 // Is the file large enough to contain a trace
331 if(lTDFStat
.st_size
<
332 (off_t
)(sizeof(struct ltt_block_start_header
)
333 + sizeof(struct ltt_trace_header_any
))){
334 g_print("The input data file %s does not contain a trace\n", fileName
);
338 /* Temporarily map the buffer start header to get trace information */
339 /* Multiple of pages aligned head */
340 tf
->buffer
.head
= mmap(0,
341 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)
342 + sizeof(struct ltt_trace_header_any
)), PROT_READ
,
343 MAP_PRIVATE
, tf
->fd
, 0);
344 if(tf
->buffer
.head
== MAP_FAILED
) {
345 perror("Error in allocating memory for buffer of tracefile");
348 g_assert( ( (guint
)tf
->buffer
.head
&(8-1) ) == 0); // make sure it's aligned.
350 header
= (struct ltt_block_start_header
*)tf
->buffer
.head
;
352 if(parse_trace_header(header
->trace
, tf
, NULL
)) {
353 g_warning("parse_trace_header error");
357 //store the size of the file
358 tf
->file_size
= lTDFStat
.st_size
;
359 tf
->buf_size
= ltt_get_uint32(LTT_GET_BO(tf
), &header
->buf_size
);
360 tf
->num_blocks
= tf
->file_size
/ tf
->buf_size
;
362 if(munmap(tf
->buffer
.head
,
363 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)
364 + sizeof(struct ltt_trace_header_any
)))) {
365 g_warning("unmap size : %u\n",
366 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)
367 + sizeof(struct ltt_trace_header_any
)));
368 perror("munmap error");
371 tf
->buffer
.head
= NULL
;
373 //read the first block
374 if(map_block(tf
,0)) {
375 perror("Cannot map block for tracefile");
383 if(munmap(tf
->buffer
.head
,
384 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)
385 + sizeof(struct ltt_trace_header_any
)))) {
386 g_warning("unmap size : %u\n",
387 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)
388 + sizeof(struct ltt_trace_header_any
)));
389 perror("munmap error");
398 LttTrace
*ltt_tracefile_get_trace(LttTracefile
*tf
)
404 /*****************************************************************************
405 *Open control and per cpu tracefiles
406 ****************************************************************************/
408 void ltt_tracefile_open_cpu(LttTrace
*t
, gchar
* tracefile_name
)
411 tf
= ltt_tracefile_open(t
,tracefile_name
);
413 t
->per_cpu_tracefile_number
++;
414 g_ptr_array_add(t
->per_cpu_tracefiles
, tf
);
417 gint
ltt_tracefile_open_control(LttTrace
*t
, gchar
* control_name
)
426 tf
= ltt_tracefile_open(t
,control_name
);
428 g_warning("ltt_tracefile_open_control : bad file descriptor");
431 t
->control_tracefile_number
++;
432 g_ptr_array_add(t
->control_tracefiles
,tf
);
434 //parse facilities tracefile to get base_id
435 if(strcmp(&control_name
[strlen(control_name
)-10],"facilities") ==0){
437 if(!ltt_tracefile_read(tf
,&ev
)) return 0; // end of file
439 if(ev
.event_id
== TRACE_FACILITY_LOAD
){
441 fLoad
.name
= (gchar
*)pos
;
442 fLoad
.checksum
= *(LttChecksum
*)(pos
+ strlen(fLoad
.name
));
443 fLoad
.base_code
= *(guint32
*)(pos
+ strlen(fLoad
.name
) + sizeof(LttChecksum
));
445 for(i
=0;i
<t
->facility_number
;i
++){
446 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
,i
);
447 if(strcmp(f
->name
,fLoad
.name
)==0 && fLoad
.checksum
==f
->checksum
){
448 f
->base_id
= fLoad
.base_code
;
452 if(i
==t
->facility_number
) {
453 g_warning("Facility: %s, checksum: %u is not found",
454 fLoad
.name
,(unsigned int)fLoad
.checksum
);
457 }else if(ev
.event_id
== TRACE_BLOCK_START
){
459 }else if(ev
.event_id
== TRACE_BLOCK_END
){
462 g_warning("Not valid facilities trace file");
471 /*****************************************************************************
473 * ltt_tracefile_close: close a trace file,
475 * t : tracefile which will be closed
476 ****************************************************************************/
478 void ltt_tracefile_close(LttTracefile
*t
)
480 int page_size
= getpagesize();
482 if(t
->buffer
.head
!= NULL
)
483 if(munmap(t
->buffer
.head
, PAGE_ALIGN(t
->buf_size
))) {
484 g_warning("unmap size : %u\n",
485 PAGE_ALIGN(t
->buf_size
));
486 perror("munmap error");
494 /*****************************************************************************
495 *Get system information
496 ****************************************************************************/
498 gint
getSystemInfo(LttSystemDescription
* des
, gchar
* pathname
)
505 GMarkupParseContext
* context
;
506 GError
* error
= NULL
;
507 GMarkupParser markup_parser
=
509 parser_start_element
,
512 NULL
, /* passthrough */
516 fd
= g_open(pathname
, O_RDONLY
, 0);
518 g_warning("Can not open file : %s\n", pathname
);
522 iochan
= g_io_channel_unix_new(fd
);
524 context
= g_markup_parse_context_new(&markup_parser
, 0, des
,NULL
);
526 //while(fgets(buf,DIR_NAME_SIZE, fp) != NULL){
527 while(g_io_channel_read_line(iochan
, &buf
, &length
, NULL
, &error
)
528 != G_IO_STATUS_EOF
) {
531 g_warning("Can not read xml file: \n%s\n", error
->message
);
534 if(!g_markup_parse_context_parse(context
, buf
, length
, &error
)){
536 g_warning("Can not parse xml file: \n%s\n", error
->message
);
539 g_markup_parse_context_free(context
);
541 g_io_channel_shutdown(iochan
, FALSE
, &error
); /* No flush */
543 g_warning("Can not close file: \n%s\n", error
->message
);
551 g_markup_parse_context_free(context
);
553 g_io_channel_shutdown(iochan
, FALSE
, &error
); /* No flush */
555 g_warning("Can not close file: \n%s\n", error
->message
);
566 /*****************************************************************************
567 *The following functions get facility/tracefile information
568 ****************************************************************************/
570 gint
getFacilityInfo(LttTrace
*t
, gchar
* eventdefs
)
577 gchar fullname
[DIR_NAME_SIZE
];
578 GError
* error
= NULL
;
580 dir
= g_dir_open(eventdefs
, 0, &error
);
583 g_warning("Can not open directory: %s, %s\n", eventdefs
, error
->message
);
588 while((name
= g_dir_read_name(dir
)) != NULL
){
589 if(!g_pattern_match_simple("*.xml", name
)) continue;
590 strcpy(fullname
,eventdefs
);
591 strcat(fullname
,name
);
592 ltt_facility_open(t
,fullname
);
596 for(j
=0;j
<t
->facility_number
;j
++){
597 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, j
);
598 for(i
=0; i
<f
->event_number
; i
++){
600 setFieldsOffset(NULL
, et
, NULL
, t
);
607 /*****************************************************************************
608 *A trace is specified as a pathname to the directory containing all the
609 *associated data (control tracefiles, per cpu tracefiles, event
612 *When a trace is closed, all the associated facilities, types and fields
613 *are released as well.
617 /****************************************************************************
618 * get_absolute_pathname
620 * return the unique pathname in the system
622 * MD : Fixed this function so it uses realpath, dealing well with
623 * forgotten cases (.. were not used correctly before).
625 ****************************************************************************/
626 void get_absolute_pathname(const gchar
*pathname
, gchar
* abs_pathname
)
628 abs_pathname
[0] = '\0';
630 if ( realpath (pathname
, abs_pathname
) != NULL
)
634 /* error, return the original path unmodified */
635 strcpy(abs_pathname
, pathname
);
641 /* Search for something like : .*_.*
643 * The left side is the name, the right side is the number.
646 int get_tracefile_name_number(gchar
*raw_name
,
653 guint raw_name_len
= strlen(raw_name
);
654 gchar char_name
[PATH_MAX
];
661 for(i
=raw_name_len
-1;i
>=0;i
--) {
662 if(raw_name
[i
] == '_') break;
664 if(i
==-1) { /* Either not found or name length is 0 */
665 /* This is a userspace tracefile */
666 strncpy(char_name
, raw_name
, raw_name_len
);
667 *name
= g_quark_from_string(char_name
);
668 *num
= 0; /* unknown cpu */
669 for(i
=0;i
<raw_name_len
;i
++) {
670 if(raw_name
[i
] == '/') {
675 for(;i
<raw_name_len
;i
++) {
676 if(raw_name
[i
] == '/') {
681 for(;i
<raw_name_len
;i
++) {
682 if(raw_name
[i
] == '-') {
686 if(i
== raw_name_len
) return -1;
688 tmpptr
= &raw_name
[i
];
689 for(;i
<raw_name_len
;i
++) {
690 if(raw_name
[i
] == '.') {
695 *tid
= strtoul(tmpptr
, &endptr
, 10);
697 return -1; /* No digit */
698 if(*tid
== ULONG_MAX
)
699 return -1; /* underflow / overflow */
701 tmpptr
= &raw_name
[i
];
702 for(;i
<raw_name_len
;i
++) {
703 if(raw_name
[i
] == '.') {
708 *pgid
= strtoul(tmpptr
, &endptr
, 10);
710 return -1; /* No digit */
711 if(*pgid
== ULONG_MAX
)
712 return -1; /* underflow / overflow */
714 tmpptr
= &raw_name
[i
];
715 *creation
= strtoull(tmpptr
, &endptr
, 10);
717 return -1; /* No digit */
718 if(*creation
== G_MAXUINT64
)
719 return -1; /* underflow / overflow */
723 cpu_num
= strtol(raw_name
+underscore_pos
+1, &endptr
, 10);
725 if(endptr
== raw_name
+underscore_pos
+1)
726 return -1; /* No digit */
727 if(cpu_num
== LONG_MIN
|| cpu_num
== LONG_MAX
)
728 return -1; /* underflow / overflow */
730 strncpy(char_name
, raw_name
, underscore_pos
);
731 char_name
[underscore_pos
] = '\0';
733 *name
= g_quark_from_string(char_name
);
742 GData
**ltt_trace_get_tracefiles_groups(LttTrace
*trace
)
744 return &trace
->tracefiles
;
748 void compute_tracefile_group(GQuark key_id
,
750 struct compute_tracefile_group_args
*args
)
755 for(i
=0; i
<group
->len
; i
++) {
756 tf
= &g_array_index (group
, LttTracefile
, i
);
758 args
->func(tf
, args
->func_args
);
763 void ltt_tracefile_group_destroy(gpointer data
)
765 GArray
*group
= (GArray
*)data
;
769 for(i
=0; i
<group
->len
; i
++) {
770 tf
= &g_array_index (group
, LttTracefile
, i
);
772 ltt_tracefile_close(tf
);
774 g_array_free(group
, TRUE
);
777 gboolean
ltt_tracefile_group_has_cpu_online(gpointer data
)
779 GArray
*group
= (GArray
*)data
;
783 for(i
=0; i
<group
->len
; i
++) {
784 tf
= &g_array_index (group
, LttTracefile
, i
);
785 if(tf
->cpu_online
) return 1;
791 /* Open each tracefile under a specific directory. Put them in a
792 * GData : permits to access them using their tracefile group pathname.
793 * i.e. access control/modules tracefile group by index :
796 * relative path is the path relative to the trace root
797 * root path is the full path
799 * A tracefile group is simply an array where all the per cpu tracefiles sits.
802 static int open_tracefiles(LttTrace
*trace
, gchar
*root_path
,
803 gchar
*relative_path
)
805 DIR *dir
= opendir(root_path
);
806 struct dirent
*entry
;
807 struct stat stat_buf
;
810 gchar path
[PATH_MAX
];
815 gchar rel_path
[PATH_MAX
];
824 strncpy(path
, root_path
, PATH_MAX
-1);
825 path_len
= strlen(path
);
826 path
[path_len
] = '/';
828 path_ptr
= path
+ path_len
;
830 strncpy(rel_path
, relative_path
, PATH_MAX
-1);
831 rel_path_len
= strlen(rel_path
);
832 rel_path
[rel_path_len
] = '/';
834 rel_path_ptr
= rel_path
+ rel_path_len
;
836 while((entry
= readdir(dir
)) != NULL
) {
838 if(entry
->d_name
[0] == '.') continue;
840 strncpy(path_ptr
, entry
->d_name
, PATH_MAX
- path_len
);
841 strncpy(rel_path_ptr
, entry
->d_name
, PATH_MAX
- rel_path_len
);
843 ret
= stat(path
, &stat_buf
);
849 g_debug("Tracefile file or directory : %s\n", path
);
851 if(strcmp(rel_path
, "/eventdefs") == 0) continue;
853 if(S_ISDIR(stat_buf
.st_mode
)) {
855 g_debug("Entering subdirectory...\n");
856 ret
= open_tracefiles(trace
, path
, rel_path
);
857 if(ret
< 0) continue;
858 } else if(S_ISREG(stat_buf
.st_mode
)) {
860 guint num
, tid
, pgid
;
863 num
= tid
= pgid
= 0;
865 if(get_tracefile_name_number(rel_path
, &name
, &num
, &tid
, &pgid
, &creation
))
866 continue; /* invalid name */
868 g_debug("Opening file.\n");
869 if(ltt_tracefile_open(trace
, path
, &tmp_tf
)) {
870 g_info("Error opening tracefile %s", path
);
872 continue; /* error opening the tracefile : bad magic number ? */
875 g_debug("Tracefile name is %s and number is %u",
876 g_quark_to_string(name
), num
);
878 tmp_tf
.cpu_online
= 1;
879 tmp_tf
.cpu_num
= num
;
883 tmp_tf
.creation
= creation
;
885 group
= g_datalist_id_get_data(&trace
->tracefiles
, name
);
887 /* Elements are automatically cleared when the array is allocated.
888 * It makes the cpu_online variable set to 0 : cpu offline, by default.
890 group
= g_array_sized_new (FALSE
, TRUE
, sizeof(LttTracefile
), 10);
891 g_datalist_id_set_data_full(&trace
->tracefiles
, name
,
892 group
, ltt_tracefile_group_destroy
);
895 /* Add the per cpu tracefile to the named group */
896 unsigned int old_len
= group
->len
;
898 group
= g_array_set_size(group
, num
+1);
899 g_array_index (group
, LttTracefile
, num
) = tmp_tf
;
909 /* ltt_get_facility_description
911 * Opens the file corresponding to the requested facility (identified by fac_id
914 * The name searched is : %trace root%/eventdefs/facname_checksum.xml
916 * Returns 0 on success, or 1 on failure.
919 static int ltt_get_facility_description(LttFacility
*f
,
921 LttTracefile
*fac_tf
)
923 char desc_file_name
[PATH_MAX
];
930 text
= g_quark_to_string(t
->pathname
);
931 textlen
= strlen(text
);
933 if(textlen
>= PATH_MAX
) goto name_error
;
934 strcpy(desc_file_name
, text
);
936 text
= "/eventdefs/";
937 textlen
+=strlen(text
);
938 if(textlen
>= PATH_MAX
) goto name_error
;
939 strcat(desc_file_name
, text
);
941 text
= g_quark_to_string(f
->name
);
942 fac_name_len
= strlen(text
);
943 textlen
+=fac_name_len
;
944 if(textlen
>= PATH_MAX
) goto name_error
;
945 strcat(desc_file_name
, text
);
947 /* arch specific facilities are named like this : name_arch */
948 if(fac_name_len
+1 < sizeof("_arch"))
951 if(!strcmp(&text
[fac_name_len
+1-sizeof("_arch")], "_arch"))
959 textlen
+=strlen(text
);
960 if(textlen
>= PATH_MAX
) goto name_error
;
961 strcat(desc_file_name
, text
);
963 err
= snprintf(desc_file_name
+textlen
, PATH_MAX
-textlen
-1,
965 if(err
< 0) goto name_error
;
967 textlen
=strlen(desc_file_name
);
972 switch(t
->arch_type
) {
973 case LTT_ARCH_TYPE_I386
:
976 case LTT_ARCH_TYPE_PPC
:
979 case LTT_ARCH_TYPE_SH
:
982 case LTT_ARCH_TYPE_S390
:
985 case LTT_ARCH_TYPE_MIPS
:
988 case LTT_ARCH_TYPE_ARM
:
991 case LTT_ARCH_TYPE_PPC64
:
994 case LTT_ARCH_TYPE_X86_64
:
997 case LTT_ARCH_TYPE_C2
:
1001 g_error("Trace from unsupported architecture.");
1003 textlen
+=strlen(text
);
1004 if(textlen
>= PATH_MAX
) goto name_error
;
1005 strcat(desc_file_name
, text
);
1009 textlen
+=strlen(text
);
1010 if(textlen
>= PATH_MAX
) goto name_error
;
1011 strcat(desc_file_name
, text
);
1013 err
= ltt_facility_open(f
, t
, desc_file_name
);
1014 if(err
) goto facility_error
;
1023 static void ltt_fac_ids_destroy(gpointer data
)
1025 GArray
*fac_ids
= (GArray
*)data
;
1027 g_array_free(fac_ids
, TRUE
);
1031 /* Presumes the tracefile is already seeked at the beginning. It makes sense,
1032 * because it must be done just after the opening */
1033 static int ltt_process_facility_tracefile(LttTracefile
*tf
)
1042 err
= ltt_tracefile_read_seek(tf
);
1043 if(err
== EPERM
) goto seek_error
;
1044 else if(err
== ERANGE
) break; /* End of tracefile */
1046 err
= ltt_tracefile_read_update_event(tf
);
1047 if(err
) goto update_error
;
1049 /* We are on a facility load/or facility unload/ or heartbeat event */
1051 * * facility 0 is hardcoded : this is the core facility. It will be shown
1052 * in the facility array though, and is shown as "loaded builtin" in the
1054 * It contains event :
1056 * 1 : facility unload
1057 * 2 : state dump facility load
1060 if(tf
->event
.facility_id
!= LTT_FACILITY_CORE
) {
1061 /* Should only contain core facility */
1062 g_warning("Error in processing facility file %s, "
1063 "should not contain facility id %u.", g_quark_to_string(tf
->name
),
1064 tf
->event
.facility_id
);
1069 struct LttFacilityLoad
*fac_load_data
;
1070 struct LttStateDumpFacilityLoad
*fac_state_dump_load_data
;
1075 switch((enum ltt_core_events
)tf
->event
.event_id
) {
1076 case LTT_EVENT_FACILITY_LOAD
:
1077 fac_name
= (char*)(tf
->event
.data
);
1078 g_debug("Doing LTT_EVENT_FACILITY_LOAD of facility %s",
1080 pos
= (tf
->event
.data
+ strlen(fac_name
) + 1);
1081 pos
+= ltt_align((size_t)pos
, sizeof(guint32
), tf
->has_alignment
);
1082 fac_load_data
= (struct LttFacilityLoad
*)pos
;
1084 fac
= &g_array_index (tf
->trace
->facilities_by_num
, LttFacility
,
1085 ltt_get_uint32(LTT_GET_BO(tf
), &fac_load_data
->id
));
1086 /* facility may already exist if trace is paused/unpaused */
1087 if(fac
->exists
) continue;
1088 fac
->name
= g_quark_from_string(fac_name
);
1089 fac
->checksum
= ltt_get_uint32(LTT_GET_BO(tf
),
1090 &fac_load_data
->checksum
);
1091 fac
->id
= ltt_get_uint32(LTT_GET_BO(tf
), &fac_load_data
->id
);
1092 fac
->pointer_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1093 &fac_load_data
->pointer_size
);
1094 fac
->int_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1095 &fac_load_data
->int_size
);
1096 fac
->long_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1097 &fac_load_data
->long_size
);
1098 fac
->size_t_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1099 &fac_load_data
->size_t_size
);
1100 fac
->alignment
= ltt_get_uint32(LTT_GET_BO(tf
),
1101 &fac_load_data
->has_alignment
);
1103 if(ltt_get_facility_description(fac
, tf
->trace
, tf
))
1104 continue; /* error opening description */
1106 fac
->trace
= tf
->trace
;
1108 /* Preset the field offsets */
1109 for(i
=0; i
<fac
->events
->len
; i
++){
1110 et
= &g_array_index(fac
->events
, LttEventType
, i
);
1111 precompute_offsets(fac
, et
);
1116 fac_ids
= g_datalist_id_get_data(&tf
->trace
->facilities_by_name
,
1118 if(fac_ids
== NULL
) {
1119 fac_ids
= g_array_sized_new (FALSE
, TRUE
, sizeof(guint
), 1);
1120 g_datalist_id_set_data_full(&tf
->trace
->facilities_by_name
,
1122 fac_ids
, ltt_fac_ids_destroy
);
1124 g_array_append_val(fac_ids
, fac
->id
);
1127 case LTT_EVENT_FACILITY_UNLOAD
:
1128 g_debug("Doing LTT_EVENT_FACILITY_UNLOAD");
1129 /* We don't care about unload : facilities ID are valid for the whole
1130 * trace. They simply won't be used after the unload. */
1132 case LTT_EVENT_STATE_DUMP_FACILITY_LOAD
:
1133 fac_name
= (char*)(tf
->event
.data
);
1134 g_debug("Doing LTT_EVENT_STATE_DUMP_FACILITY_LOAD of facility %s",
1136 pos
= (tf
->event
.data
+ strlen(fac_name
) + 1);
1137 pos
+= ltt_align((size_t)pos
, sizeof(guint32
), tf
->has_alignment
);
1138 fac_state_dump_load_data
= (struct LttStateDumpFacilityLoad
*)pos
;
1140 fac
= &g_array_index (tf
->trace
->facilities_by_num
, LttFacility
,
1141 ltt_get_uint32(LTT_GET_BO(tf
), &fac_state_dump_load_data
->id
));
1142 /* facility may already exist if trace is paused/unpaused */
1143 if(fac
->exists
) continue;
1144 fac
->name
= g_quark_from_string(fac_name
);
1145 fac
->checksum
= ltt_get_uint32(LTT_GET_BO(tf
),
1146 &fac_state_dump_load_data
->checksum
);
1147 fac
->id
= ltt_get_uint32(LTT_GET_BO(tf
),
1148 &fac_state_dump_load_data
->id
);
1149 fac
->pointer_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1150 &fac_state_dump_load_data
->pointer_size
);
1151 fac
->int_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1152 &fac_state_dump_load_data
->int_size
);
1153 fac
->long_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1154 &fac_state_dump_load_data
->long_size
);
1155 fac
->size_t_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1156 &fac_state_dump_load_data
->size_t_size
);
1157 fac
->alignment
= ltt_get_uint32(LTT_GET_BO(tf
),
1158 &fac_state_dump_load_data
->has_alignment
);
1159 if(ltt_get_facility_description(fac
, tf
->trace
, tf
))
1160 continue; /* error opening description */
1162 fac
->trace
= tf
->trace
;
1164 /* Preset the field offsets */
1165 for(i
=0; i
<fac
->events
->len
; i
++){
1166 et
= &g_array_index(fac
->events
, LttEventType
, i
);
1167 precompute_offsets(fac
, et
);
1172 fac_ids
= g_datalist_id_get_data(&tf
->trace
->facilities_by_name
,
1174 if(fac_ids
== NULL
) {
1175 fac_ids
= g_array_sized_new (FALSE
, TRUE
, sizeof(guint
), 1);
1176 g_datalist_id_set_data_full(&tf
->trace
->facilities_by_name
,
1178 fac_ids
, ltt_fac_ids_destroy
);
1180 g_array_append_val(fac_ids
, fac
->id
);
1183 case LTT_EVENT_HEARTBEAT
:
1186 g_warning("Error in processing facility file %s, "
1187 "unknown event id %hhu in core facility.",
1188 g_quark_to_string(tf
->name
),
1189 tf
->event
.event_id
);
1191 goto event_id_error
;
1197 /* Error handling */
1202 g_warning("An error occured in facility tracefile parsing");
1207 LttTrace
*ltt_trace_open(const gchar
*pathname
)
1209 gchar abs_path
[PATH_MAX
];
1214 struct ltt_block_start_header
*header
;
1216 struct dirent
*entry
;
1217 guint control_found
= 0;
1218 guint eventdefs_found
= 0;
1219 struct stat stat_buf
;
1220 gchar path
[PATH_MAX
];
1222 t
= g_new(LttTrace
, 1);
1223 if(!t
) goto alloc_error
;
1225 get_absolute_pathname(pathname
, abs_path
);
1226 t
->pathname
= g_quark_from_string(abs_path
);
1228 g_datalist_init(&t
->tracefiles
);
1230 /* Test to see if it looks like a trace */
1231 dir
= opendir(abs_path
);
1236 while((entry
= readdir(dir
)) != NULL
) {
1237 strcpy(path
, abs_path
);
1239 strcat(path
, entry
->d_name
);
1240 ret
= stat(path
, &stat_buf
);
1245 if(S_ISDIR(stat_buf
.st_mode
)) {
1246 if(strcmp(entry
->d_name
, "control") == 0) {
1249 if(strcmp(entry
->d_name
, "eventdefs") == 0) {
1250 eventdefs_found
= 1;
1256 if(!control_found
|| !eventdefs_found
) goto find_error
;
1258 /* Open all the tracefiles */
1259 if(open_tracefiles(t
, abs_path
, "")) {
1260 g_warning("Error opening tracefile %s", abs_path
);
1264 /* Prepare the facilities containers : array and mapping */
1265 /* Array is zeroed : the "exists" field is set to false by default */
1266 t
->facilities_by_num
= g_array_sized_new (FALSE
,
1267 TRUE
, sizeof(LttFacility
),
1269 t
->facilities_by_num
= g_array_set_size(t
->facilities_by_num
, NUM_FACILITIES
);
1271 g_datalist_init(&t
->facilities_by_name
);
1273 /* Parse each trace control/facilitiesN files : get runtime fac. info */
1274 group
= g_datalist_id_get_data(&t
->tracefiles
, LTT_TRACEFILE_NAME_FACILITIES
);
1276 g_error("Trace %s has no facility tracefile", abs_path
);
1278 goto facilities_error
;
1281 /* Get the trace information for the control/facility 0 tracefile */
1282 g_assert(group
->len
> 0);
1283 tf
= &g_array_index (group
, LttTracefile
, 0);
1284 header
= (struct ltt_block_start_header
*)tf
->buffer
.head
;
1285 g_assert(parse_trace_header(header
->trace
,
1288 t
->num_cpu
= group
->len
;
1290 for(i
=0; i
<group
->len
; i
++) {
1291 tf
= &g_array_index (group
, LttTracefile
, i
);
1292 if(ltt_process_facility_tracefile(tf
))
1293 goto facilities_error
;
1298 /* Error handling */
1300 g_datalist_clear(&t
->facilities_by_name
);
1301 g_array_free(t
->facilities_by_num
, TRUE
);
1303 g_datalist_clear(&t
->tracefiles
);
1311 GQuark
ltt_trace_name(const LttTrace
*t
)
1317 /******************************************************************************
1318 * When we copy a trace, we want all the opening actions to happen again :
1319 * the trace will be reopened and totally independant from the original.
1320 * That's why we call ltt_trace_open.
1321 *****************************************************************************/
1322 LttTrace
*ltt_trace_copy(LttTrace
*self
)
1324 return ltt_trace_open(g_quark_to_string(self
->pathname
));
1327 void ltt_trace_close(LttTrace
*t
)
1332 for(i
=0; i
<t
->facilities_by_num
->len
; i
++) {
1333 fac
= &g_array_index (t
->facilities_by_num
, LttFacility
, i
);
1335 ltt_facility_close(fac
);
1338 g_datalist_clear(&t
->facilities_by_name
);
1339 g_array_free(t
->facilities_by_num
, TRUE
);
1340 g_datalist_clear(&t
->tracefiles
);
1345 /*****************************************************************************
1346 *Get the system description of the trace
1347 ****************************************************************************/
1349 LttFacility
*ltt_trace_facility_by_id(LttTrace
*t
, guint8 id
)
1351 g_assert(id
< t
->facilities_by_num
->len
);
1352 return &g_array_index(t
->facilities_by_num
, LttFacility
, id
);
1355 /* ltt_trace_facility_get_by_name
1357 * Returns the GArray of facility indexes. All the fac_ids that matches the
1358 * requested facility name.
1360 * If name is not found, returns NULL.
1362 GArray
*ltt_trace_facility_get_by_name(LttTrace
*t
, GQuark name
)
1364 return g_datalist_id_get_data(&t
->facilities_by_name
, name
);
1367 /*****************************************************************************
1368 * Functions to discover all the event types in the trace
1369 ****************************************************************************/
1372 unsigned ltt_trace_eventtype_number(LttTrace
*t
)
1376 unsigned int num
= t
->facility_number
;
1380 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, i
);
1381 count
+= f
->event_number
;
1388 //use an iteration on all the trace facilities, and inside iteration on all the
1389 //event types in each facilities instead.
1390 LttEventType
*ltt_trace_eventtype_get(LttTrace
*t
, unsigned evId
)
1392 LttEventType
*event_type
;
1395 f
= ltt_trace_facility_by_id(t
,evId
);
1397 if(unlikely(!f
)) event_type
= NULL
;
1398 else event_type
= f
->events
[evId
- f
->base_id
];
1405 /*****************************************************************************
1406 * ltt_trace_find_tracefile
1408 * Find a tracefile by name and index in the group.
1410 * Returns a pointer to the tracefiles, else NULL.
1411 ****************************************************************************/
1413 LttTracefile
*ltt_trace_find_tracefile(LttTrace
*t
, const gchar
*name
)
1418 /*****************************************************************************
1419 * Get the start time and end time of the trace
1420 ****************************************************************************/
1422 static void ltt_tracefile_time_span_get(LttTracefile
*tf
,
1423 LttTime
*start
, LttTime
*end
)
1427 err
= map_block(tf
, 0);
1429 g_error("Can not map block");
1430 *start
= ltt_time_infinite
;
1432 *start
= tf
->buffer
.begin
.timestamp
;
1434 err
= map_block(tf
, tf
->num_blocks
- 1); /* Last block */
1436 g_error("Can not map block");
1437 *end
= ltt_time_zero
;
1439 *end
= tf
->buffer
.end
.timestamp
;
1442 struct tracefile_time_span_get_args
{
1448 static void group_time_span_get(GQuark name
, gpointer data
, gpointer user_data
)
1450 struct tracefile_time_span_get_args
*args
=
1451 (struct tracefile_time_span_get_args
*)user_data
;
1453 GArray
*group
= (GArray
*)data
;
1459 for(i
=0; i
<group
->len
; i
++) {
1460 tf
= &g_array_index (group
, LttTracefile
, i
);
1461 if(tf
->cpu_online
) {
1462 ltt_tracefile_time_span_get(tf
, &tmp_start
, &tmp_end
);
1463 if(ltt_time_compare(*args
->start
, tmp_start
)>0) *args
->start
= tmp_start
;
1464 if(ltt_time_compare(*args
->end
, tmp_end
)<0) *args
->end
= tmp_end
;
1469 void ltt_trace_time_span_get(LttTrace
*t
, LttTime
*start
, LttTime
*end
)
1471 LttTime min_start
= ltt_time_infinite
;
1472 LttTime max_end
= ltt_time_zero
;
1473 struct tracefile_time_span_get_args args
= { t
, &min_start
, &max_end
};
1475 g_datalist_foreach(&t
->tracefiles
, &group_time_span_get
, &args
);
1477 if(start
!= NULL
) *start
= min_start
;
1478 if(end
!= NULL
) *end
= max_end
;
1483 /*****************************************************************************
1484 *Get the name of a tracefile
1485 ****************************************************************************/
1487 GQuark
ltt_tracefile_name(const LttTracefile
*tf
)
1492 GQuark
ltt_tracefile_long_name(const LttTracefile
*tf
)
1494 return tf
->long_name
;
1499 guint
ltt_tracefile_cpu(LttTracefile
*tf
)
1504 guint
ltt_tracefile_tid(LttTracefile
*tf
)
1509 guint
ltt_tracefile_pgid(LttTracefile
*tf
)
1514 guint64
ltt_tracefile_creation(LttTracefile
*tf
)
1516 return tf
->creation
;
1518 /*****************************************************************************
1519 * Get the number of blocks in the tracefile
1520 ****************************************************************************/
1522 guint
ltt_tracefile_block_number(LttTracefile
*tf
)
1524 return tf
->num_blocks
;
1528 /* Seek to the first event in a tracefile that has a time equal or greater than
1529 * the time passed in parameter.
1531 * If the time parameter is outside the tracefile time span, seek to the first
1532 * event or if after, return ERANGE.
1534 * If the time parameter is before the first event, we have to seek specially to
1537 * If the time is after the end of the trace, return ERANGE.
1539 * Do a binary search to find the right block, then a sequential search in the
1540 * block to find the event.
1542 * In the special case where the time requested fits inside a block that has no
1543 * event corresponding to the requested time, the first event of the next block
1546 * IMPORTANT NOTE : // FIXME everywhere...
1548 * You MUST NOT do a ltt_tracefile_read right after a ltt_tracefile_seek_time :
1549 * you will jump over an event if you do.
1551 * Return value : 0 : no error, the tf->event can be used
1552 * ERANGE : time if after the last event of the trace
1553 * otherwise : this is an error.
1557 int ltt_tracefile_seek_time(LttTracefile
*tf
, LttTime time
)
1561 unsigned int block_num
, high
, low
;
1563 /* seek at the beginning of trace */
1564 err
= map_block(tf
, 0); /* First block */
1566 g_error("Can not map block");
1570 /* If the time is lower or equal the beginning of the trace,
1571 * go to the first event. */
1572 if(ltt_time_compare(time
, tf
->buffer
.begin
.timestamp
) <= 0) {
1573 ret
= ltt_tracefile_read(tf
);
1574 if(ret
== ERANGE
) goto range
;
1575 else if (ret
) goto fail
;
1576 goto found
; /* There is either no event in the trace or the event points
1577 to the first event in the trace */
1580 err
= map_block(tf
, tf
->num_blocks
- 1); /* Last block */
1582 g_error("Can not map block");
1586 /* If the time is after the end of the trace, return ERANGE. */
1587 if(ltt_time_compare(time
, tf
->buffer
.end
.timestamp
) > 0) {
1591 /* Binary search the block */
1592 high
= tf
->num_blocks
- 1;
1596 block_num
= ((high
-low
) / 2) + low
;
1598 err
= map_block(tf
, block_num
);
1600 g_error("Can not map block");
1604 /* We cannot divide anymore : this is what would happen if the time
1605 * requested was exactly between two consecutive buffers'end and start
1606 * timestamps. This is also what would happend if we didn't deal with out
1607 * of span cases prior in this function. */
1608 /* The event is right in the buffer!
1609 * (or in the next buffer first event) */
1611 ret
= ltt_tracefile_read(tf
);
1612 if(ret
== ERANGE
) goto range
; /* ERANGE or EPERM */
1613 else if(ret
) goto fail
;
1615 if(ltt_time_compare(time
, tf
->event
.event_time
) <= 0)
1619 } else if(ltt_time_compare(time
, tf
->buffer
.begin
.timestamp
) < 0) {
1620 /* go to lower part */
1621 high
= block_num
- 1;
1622 } else if(ltt_time_compare(time
, tf
->buffer
.end
.timestamp
) > 0) {
1623 /* go to higher part */
1624 low
= block_num
+ 1;
1625 } else {/* The event is right in the buffer!
1626 (or in the next buffer first event) */
1628 ret
= ltt_tracefile_read(tf
);
1629 if(ret
== ERANGE
) goto range
; /* ERANGE or EPERM */
1630 else if(ret
) goto fail
;
1632 if(ltt_time_compare(time
, tf
->event
.event_time
) <= 0)
1644 /* Error handling */
1646 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1647 g_quark_to_string(tf
->name
));
1652 int ltt_tracefile_seek_position(LttTracefile
*tf
, const LttEventPosition
*ep
) {
1656 if(ep
->tracefile
!= tf
) {
1660 err
= map_block(tf
, ep
->block
);
1662 g_error("Can not map block");
1666 tf
->event
.offset
= ep
->offset
;
1668 /* Put back the event real tsc */
1669 tf
->event
.tsc
= ep
->tsc
;
1670 tf
->buffer
.tsc
= ep
->tsc
;
1672 err
= ltt_tracefile_read_update_event(tf
);
1674 err
= ltt_tracefile_read_op(tf
);
1680 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1681 g_quark_to_string(tf
->name
));
1685 LttTime
ltt_interpolate_time_from_tsc(LttTracefile
*tf
, guint64 tsc
)
1689 if(tsc
> tf
->trace
->start_tsc
) {
1690 time
= ltt_time_from_uint64(
1691 (double)(tsc
- tf
->trace
->start_tsc
)
1692 * (1000000000.0 / tf
->trace
->freq_scale
)
1693 / (double)tf
->trace
->start_freq
);
1694 time
= ltt_time_add(tf
->trace
->start_time_from_tsc
, time
);
1696 time
= ltt_time_from_uint64(
1697 (double)(tf
->trace
->start_tsc
- tsc
)
1698 * (1000000000.0 / tf
->trace
->freq_scale
)
1699 / (double)tf
->trace
->start_freq
);
1700 time
= ltt_time_sub(tf
->trace
->start_time_from_tsc
, time
);
1705 /* Calculate the real event time based on the buffer boundaries */
1706 LttTime
ltt_interpolate_time(LttTracefile
*tf
, LttEvent
*event
)
1708 return ltt_interpolate_time_from_tsc(tf
, tf
->buffer
.tsc
);
1712 /* Get the current event of the tracefile : valid until the next read */
1713 LttEvent
*ltt_tracefile_get_event(LttTracefile
*tf
)
1720 /*****************************************************************************
1722 * ltt_tracefile_read : Read the next event in the tracefile
1727 * Returns 0 if an event can be used in tf->event.
1728 * Returns ERANGE on end of trace. The event in tf->event still can be used
1729 * (if the last block was not empty).
1730 * Returns EPERM on error.
1732 * This function does make the tracefile event structure point to the event
1733 * currently pointed to by the tf->event.
1735 * Note : you must call a ltt_tracefile_seek to the beginning of the trace to
1736 * reinitialize it after an error if you want results to be coherent.
1737 * It would be the case if a end of trace last buffer has no event : the end
1738 * of trace wouldn't be returned, but an error.
1739 * We make the assumption there is at least one event per buffer.
1740 ****************************************************************************/
1742 int ltt_tracefile_read(LttTracefile
*tf
)
1746 err
= ltt_tracefile_read_seek(tf
);
1748 err
= ltt_tracefile_read_update_event(tf
);
1750 err
= ltt_tracefile_read_op(tf
);
1756 int ltt_tracefile_read_seek(LttTracefile
*tf
)
1760 /* Get next buffer until we finally have an event, or end of trace */
1762 err
= ltt_seek_next_event(tf
);
1763 if(unlikely(err
== ENOPROTOOPT
)) {
1767 /* Are we at the end of the buffer ? */
1769 if(unlikely(tf
->buffer
.index
== tf
->num_blocks
-1)){ /* end of trace ? */
1772 /* get next block */
1773 err
= map_block(tf
, tf
->buffer
.index
+ 1);
1775 g_error("Can not map block");
1779 } else break; /* We found an event ! */
1786 /* do specific operation on events */
1787 int ltt_tracefile_read_op(LttTracefile
*tf
)
1793 /* do event specific operation */
1795 /* do something if its an heartbeat event : increment the heartbeat count */
1796 //if(event->facility_id == LTT_FACILITY_CORE)
1797 // if(event->event_id == LTT_EVENT_HEARTBEAT)
1798 // tf->cur_heart_beat_number++;
1804 /* same as ltt_tracefile_read, but does not seek to the next event nor call
1805 * event specific operation. */
1806 int ltt_tracefile_read_update_event(LttTracefile
*tf
)
1812 pos
= tf
->buffer
.head
+ event
->offset
;
1814 /* Read event header */
1816 /* Align the head */
1817 pos
+= ltt_align((size_t)pos
, tf
->trace
->arch_size
, tf
->has_alignment
);
1819 if(tf
->trace
->has_heartbeat
) {
1820 event
->timestamp
= ltt_get_uint32(LTT_GET_BO(tf
),
1822 /* 32 bits -> 64 bits tsc */
1823 /* note : still works for seek and non seek cases. */
1824 if(event
->timestamp
< (0xFFFFFFFFULL
&tf
->buffer
.tsc
)) {
1825 tf
->buffer
.tsc
= ((tf
->buffer
.tsc
&0xFFFFFFFF00000000ULL
)
1827 | (guint64
)event
->timestamp
;
1828 event
->tsc
= tf
->buffer
.tsc
;
1831 tf
->buffer
.tsc
= (tf
->buffer
.tsc
&0xFFFFFFFF00000000ULL
)
1832 | (guint64
)event
->timestamp
;
1833 event
->tsc
= tf
->buffer
.tsc
;
1835 pos
+= sizeof(guint32
);
1837 event
->tsc
= ltt_get_uint64(LTT_GET_BO(tf
), pos
);
1838 tf
->buffer
.tsc
= event
->tsc
;
1839 pos
+= sizeof(guint64
);
1841 event
->event_time
= ltt_interpolate_time(tf
, event
);
1842 event
->facility_id
= *(guint8
*)pos
;
1843 pos
+= sizeof(guint8
);
1845 event
->event_id
= *(guint8
*)pos
;
1846 pos
+= sizeof(guint8
);
1848 event
->event_size
= ltt_get_uint16(LTT_GET_BO(tf
), pos
);
1849 pos
+= sizeof(guint16
);
1851 /* Align the head */
1852 pos
+= ltt_align((size_t)pos
, tf
->trace
->arch_size
, tf
->has_alignment
);
1856 /* get the data size and update the event fields with the current
1858 ltt_update_event_size(tf
);
1864 /****************************************************************************
1866 * map_block : map a block from the file
1868 * lttdes : ltt trace file
1869 * whichBlock : the block which will be read
1872 * EINVAL : lseek fail
1873 * EIO : can not read from the file
1874 ****************************************************************************/
1876 static gint
map_block(LttTracefile
* tf
, guint block_num
)
1878 int page_size
= getpagesize();
1879 struct ltt_block_start_header
*header
;
1881 g_assert(block_num
< tf
->num_blocks
);
1883 if(tf
->buffer
.head
!= NULL
) {
1884 if(munmap(tf
->buffer
.head
, PAGE_ALIGN(tf
->buf_size
))) {
1885 g_warning("unmap size : %u\n",
1886 PAGE_ALIGN(tf
->buf_size
));
1887 perror("munmap error");
1893 /* Multiple of pages aligned head */
1894 tf
->buffer
.head
= mmap(0,
1895 PAGE_ALIGN(tf
->buf_size
),
1896 PROT_READ
, MAP_PRIVATE
, tf
->fd
,
1897 PAGE_ALIGN((off_t
)tf
->buf_size
* (off_t
)block_num
));
1899 if(tf
->buffer
.head
== MAP_FAILED
) {
1900 perror("Error in allocating memory for buffer of tracefile");
1904 g_assert( ( (guint
)tf
->buffer
.head
&(8-1) ) == 0); // make sure it's aligned.
1907 tf
->buffer
.index
= block_num
;
1909 header
= (struct ltt_block_start_header
*)tf
->buffer
.head
;
1912 tf
->buffer
.begin
.timestamp
= ltt_time_add(
1913 ltt_time_from_uint64(
1914 ltt_get_uint64(LTT_GET_BO(tf
),
1915 &header
->begin
.timestamp
)
1916 - tf
->trace
->start_monotonic
),
1917 tf
->trace
->start_time
);
1919 //g_debug("block %u begin : %lu.%lu", block_num,
1920 // tf->buffer.begin.timestamp.tv_sec, tf->buffer.begin.timestamp.tv_nsec);
1921 tf
->buffer
.begin
.cycle_count
= ltt_get_uint64(LTT_GET_BO(tf
),
1922 &header
->begin
.cycle_count
);
1923 tf
->buffer
.begin
.freq
= ltt_get_uint64(LTT_GET_BO(tf
),
1924 &header
->begin
.freq
);
1925 if(tf
->buffer
.begin
.freq
== 0)
1926 tf
->buffer
.begin
.freq
= tf
->trace
->start_freq
;
1928 tf
->buffer
.begin
.timestamp
= ltt_interpolate_time_from_tsc(tf
,
1929 tf
->buffer
.begin
.cycle_count
);
1932 ltt_time_from_uint64(
1933 (double)(tf
->buffer
.begin
.cycle_count
1934 - tf
->trace
->start_tsc
) * 1000000.0
1935 / (double)tf
->trace
->start_freq
),
1936 tf
->trace
->start_time_from_tsc
);
1940 tf
->buffer
.end
.timestamp
= ltt_time_add(
1941 ltt_time_from_uint64(
1942 ltt_get_uint64(LTT_GET_BO(tf
),
1943 &header
->end
.timestamp
)
1944 - tf
->trace
->start_monotonic
),
1945 tf
->trace
->start_time
);
1947 //g_debug("block %u end : %lu.%lu", block_num,
1948 // tf->buffer.end.timestamp.tv_sec, tf->buffer.end.timestamp.tv_nsec);
1949 tf
->buffer
.end
.cycle_count
= ltt_get_uint64(LTT_GET_BO(tf
),
1950 &header
->end
.cycle_count
);
1951 tf
->buffer
.end
.freq
= ltt_get_uint64(LTT_GET_BO(tf
),
1953 if(tf
->buffer
.end
.freq
== 0)
1954 tf
->buffer
.end
.freq
= tf
->trace
->start_freq
;
1956 tf
->buffer
.lost_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1957 &header
->lost_size
);
1958 tf
->buffer
.end
.timestamp
= ltt_interpolate_time_from_tsc(tf
,
1959 tf
->buffer
.end
.cycle_count
);
1962 ltt_time_from_uint64(
1963 (double)(tf
->buffer
.end
.cycle_count
1964 - tf
->trace
->start_tsc
) * 1000000.0
1965 / (double)tf
->trace
->start_freq
),
1966 tf
->trace
->start_time_from_tsc
);
1968 tf
->buffer
.tsc
= tf
->buffer
.begin
.cycle_count
;
1969 tf
->event
.tsc
= tf
->buffer
.tsc
;
1970 tf
->buffer
.freq
= tf
->buffer
.begin
.freq
;
1973 * eventually support variable buffer size : will need a partial pre-read of
1974 * the headers to create an index when we open the trace... eventually. */
1975 g_assert(tf
->buf_size
== ltt_get_uint32(LTT_GET_BO(tf
),
1976 &header
->buf_size
));
1978 /* Now that the buffer is mapped, calculate the time interpolation for the
1981 // tf->buffer.nsecs_per_cycle = calc_nsecs_per_cycle(tf);
1982 //tf->buffer.cyc2ns_scale = calc_nsecs_per_cycle(tf);
1984 /* Make the current event point to the beginning of the buffer :
1985 * it means that the event read must get the first event. */
1986 tf
->event
.tracefile
= tf
;
1987 tf
->event
.block
= block_num
;
1988 tf
->event
.offset
= 0;
1997 /* It will update the fields offsets too */
1998 void ltt_update_event_size(LttTracefile
*tf
)
2002 /* Specific handling of core events : necessary to read the facility control
2004 LttFacility
*f
= ltt_trace_get_facility_by_num(tf
->trace
,
2005 tf
->event
.facility_id
);
2007 if(likely(tf
->event
.facility_id
== LTT_FACILITY_CORE
)) {
2008 switch((enum ltt_core_events
)tf
->event
.event_id
) {
2009 case LTT_EVENT_FACILITY_LOAD
:
2010 size
= strlen((char*)tf
->event
.data
) + 1;
2011 //g_debug("Update Event facility load of facility %s", (char*)tf->event.data);
2012 size
+= ltt_align(size
, sizeof(guint32
), tf
->has_alignment
);
2013 size
+= sizeof(struct LttFacilityLoad
);
2015 case LTT_EVENT_FACILITY_UNLOAD
:
2016 //g_debug("Update Event facility unload");
2017 size
= sizeof(struct LttFacilityUnload
);
2019 case LTT_EVENT_STATE_DUMP_FACILITY_LOAD
:
2020 size
= strlen((char*)tf
->event
.data
) + 1;
2021 size
+= ltt_align(size
, sizeof(guint32
), tf
->has_alignment
);
2022 //g_debug("Update Event facility load state dump of facility %s",
2023 // (char*)tf->event.data);
2024 size
+= sizeof(struct LttStateDumpFacilityLoad
);
2026 case LTT_EVENT_HEARTBEAT
:
2027 //g_debug("Update Event heartbeat");
2028 size
= sizeof(TimeHeartbeat
);
2031 g_warning("Error in getting event size : tracefile %s, "
2032 "unknown event id %hhu in core facility.",
2033 g_quark_to_string(tf
->name
),
2034 tf
->event
.event_id
);
2035 goto event_id_error
;
2040 g_warning("Unknown facility %hhu (0x%hhx) in tracefile %s",
2041 tf
->event
.facility_id
,
2042 tf
->event
.facility_id
,
2043 g_quark_to_string(tf
->name
));
2044 goto facility_error
;
2047 LttEventType
*event_type
=
2048 ltt_facility_eventtype_get(f
, tf
->event
.event_id
);
2051 g_warning("Unknown event id %hhu in facility %s in tracefile %s",
2053 g_quark_to_string(f
->name
),
2054 g_quark_to_string(tf
->name
));
2055 goto event_type_error
;
2058 /* Compute the dynamic offsets */
2059 compute_offsets(tf
, f
, event_type
, &size
, tf
->event
.data
);
2061 //g_debug("Event root field : f.e %hhu.%hhu size %zd",
2062 // tf->event.facility_id,
2063 // tf->event.event_id, size);
2066 tf
->event
.data_size
= size
;
2068 /* Check consistency between kernel and LTTV structure sizes */
2069 if(tf
->event
.event_size
== 0xFFFF) {
2070 /* Event size too big to fit in the event size field */
2071 tf
->event
.event_size
= tf
->event
.data_size
;
2073 g_assert(tf
->event
.data_size
== tf
->event
.event_size
);
2080 if(tf
->event
.event_size
== 0xFFFF) {
2081 g_error("Cannot jump over an unknown event bigger than 0xFFFE bytes");
2083 /* The facility is unknown : use the kernel information about this event
2084 * to jump over it. */
2085 tf
->event
.data_size
= tf
->event
.event_size
;
2089 /* Take the tf current event offset and use the event facility id and event id
2090 * to figure out where is the next event offset.
2092 * This is an internal function not aiming at being used elsewhere : it will
2093 * not jump over the current block limits. Please consider using
2094 * ltt_tracefile_read to do this.
2096 * Returns 0 on success
2097 * ERANGE if we are at the end of the buffer.
2098 * ENOPROTOOPT if an error occured when getting the current event size.
2100 static int ltt_seek_next_event(LttTracefile
*tf
)
2105 /* seek over the buffer header if we are at the buffer start */
2106 if(tf
->event
.offset
== 0) {
2107 tf
->event
.offset
+= tf
->buffer_header_size
;
2109 if(tf
->event
.offset
== tf
->buf_size
- tf
->buffer
.lost_size
) {
2116 pos
= tf
->event
.data
;
2118 if(tf
->event
.data_size
< 0) goto error
;
2120 pos
+= (size_t)tf
->event
.data_size
;
2122 tf
->event
.offset
= pos
- tf
->buffer
.head
;
2124 if(tf
->event
.offset
== tf
->buf_size
- tf
->buffer
.lost_size
) {
2128 g_assert(tf
->event
.offset
< tf
->buf_size
- tf
->buffer
.lost_size
);
2134 g_error("Error in ltt_seek_next_event for tracefile %s",
2135 g_quark_to_string(tf
->name
));
2140 /*****************************************************************************
2142 * calc_nsecs_per_cycle : calculate nsecs per cycle for current block
2144 * 1.0 / (freq(khz) *1000) * 1000000000
2147 ****************************************************************************/
2148 /* from timer_tsc.c */
2149 #define CYC2NS_SCALE_FACTOR 10
2150 static guint32
calc_nsecs_per_cycle(LttTracefile
* tf
)
2152 //return 1e6 / (double)tf->buffer.freq;
2153 guint32 cpu_mhz
= tf
->buffer
.freq
/ 1000;
2154 guint32 cyc2ns_scale
= (1000 << CYC2NS_SCALE_FACTOR
)/cpu_mhz
;
2156 return cyc2ns_scale
;
2157 // return 1e6 / (double)tf->buffer.freq;
2160 static guint64
cycles_2_ns(LttTracefile
*tf
, guint64 cycles
)
2162 return (cycles
* tf
->buffer
.cyc2ns_scale
) >> CYC2NS_SCALE_FACTOR
;
2167 void setFieldsOffset(LttTracefile
*tf
, LttEventType
*evT
,void *evD
)
2169 LttField
* rootFld
= evT
->root_field
;
2170 // rootFld->base_address = evD;
2173 rootFld
->field_size
= getFieldtypeSize(tf
, evT
->facility
,
2174 evT
, 0,0,rootFld
, evD
);
2178 /*****************************************************************************
2180 * set_fields_offsets : set the precomputable offset of the fields
2182 * tracefile : opened trace file
2183 * event_type : the event type
2184 ****************************************************************************/
2186 void set_fields_offsets(LttTracefile
*tf
, LttEventType
*event_type
)
2188 LttField
*field
= event_type
->root_field
;
2189 enum field_status fixed_root
= FIELD_FIXED
, fixed_parent
= FIELD_FIXED
;
2192 preset_field_type_size(tf
, event_type
, 0, 0,
2193 &fixed_root
, &fixed_parent
,
2200 /*****************************************************************************
2202 * get_alignment : Get the alignment needed for a field.
2206 * returns : The size on which it must be aligned.
2208 ****************************************************************************/
2209 off_t
get_alignment(LttField
*field
)
2211 LttType
*type
= &field
->field_type
;
2213 switch(type
->type_class
) {
2215 case LTT_UINT_FIXED
:
2230 /* Align offset on type size */
2231 return field
->field_size
;
2237 g_assert(type
->fields
->len
== 1);
2239 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
2240 return get_alignment(child
);
2244 g_assert(type
->fields
->len
== 2);
2247 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
2249 localign
= max(localign
, get_alignment(child
));
2251 child
= &g_array_index(type
->fields
, LttField
, 1);
2252 localign
= max(localign
, get_alignment(child
));
2263 for(i
=0; i
<type
->fields
->len
; i
++) {
2264 LttField
*child
= &g_array_index(type
->fields
, LttField
, i
);
2265 localign
= max(localign
, get_alignment(child
));
2272 g_error("get_alignment : unknown type");
2277 /*****************************************************************************
2279 * field_compute_static_size : Determine the size of fields known by their
2280 * sole definition. Unions, arrays and struct sizes might be known, but
2281 * the parser does not give that information.
2286 ****************************************************************************/
2288 void field_compute_static_size(LttFacility
*fac
, LttField
*field
)
2290 LttType
*type
= &field
->field_type
;
2292 switch(type
->type_class
) {
2294 case LTT_UINT_FIXED
:
2313 /* note this : array type size is the number of elements in the array,
2314 * while array field size of the length of the array in bytes */
2315 g_assert(type
->fields
->len
== 1);
2317 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
2318 field_compute_static_size(fac
, child
);
2320 if(child
->field_size
!= 0) {
2321 field
->field_size
= type
->size
* child
->field_size
;
2322 field
->dynamic_offsets
= g_array_sized_new(FALSE
, TRUE
,
2323 sizeof(off_t
), type
->size
);
2325 field
->field_size
= 0;
2330 g_assert(type
->fields
->len
== 2);
2332 off_t local_offset
= 0;
2333 LttField
*child
= &g_array_index(type
->fields
, LttField
, 1);
2334 field_compute_static_size(fac
, child
);
2335 field
->field_size
= 0;
2337 if(child
->field_size
!= 0) {
2338 field
->dynamic_offsets
= g_array_sized_new(FALSE
, TRUE
,
2339 sizeof(off_t
), SEQUENCE_AVG_ELEMENTS
);
2347 for(i
=0;i
<type
->fields
->len
;i
++) {
2348 LttField
*child
= &g_array_index(type
->fields
, LttField
, i
);
2349 field_compute_static_size(fac
, child
);
2350 if(child
->field_size
!= 0) {
2351 type
->size
+= ltt_align(type
->size
, get_alignment(child
),
2353 type
->size
+= child
->field_size
;
2355 /* As soon as we find a child with variable size, we have
2356 * a variable size */
2361 field
->field_size
= type
->size
;
2365 g_error("field_static_size : unknown type");
2372 /*****************************************************************************
2374 * precompute_fields_offsets : set the precomputable offset of the fields
2378 * offset : pointer to the current offset, must be incremented
2380 * return : 1 : found a variable length field, stop the processing.
2382 ****************************************************************************/
2385 gint
precompute_fields_offsets(LttFacility
*fac
, LttField
*field
, off_t
*offset
)
2387 LttType
*type
= &field
->field_type
;
2389 switch(type
->type_class
) {
2391 case LTT_UINT_FIXED
:
2406 g_assert(field
->field_size
!= 0);
2407 /* Align offset on type size */
2408 *offset
+= ltt_align(*offset
, get_alignment(field
),
2410 /* remember offset */
2411 field
->offset_root
= *offset
;
2412 field
->fixed_root
= FIELD_FIXED
;
2413 /* Increment offset */
2414 *offset
+= field
->field_size
;
2418 field
->offset_root
= *offset
;
2419 field
->fixed_root
= FIELD_FIXED
;
2423 g_assert(type
->fields
->len
== 1);
2425 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
2427 *offset
+= ltt_align(*offset
, get_alignment(field
),
2430 /* remember offset */
2431 field
->offset_root
= *offset
;
2432 field
->array_offset
= *offset
;
2433 field
->fixed_root
= FIELD_FIXED
;
2435 /* Let the child be variable */
2436 //precompute_fields_offsets(tf, child, offset);
2438 if(field
->field_size
!= 0) {
2439 /* Increment offset */
2440 /* field_size is the array size in bytes */
2441 *offset
+= field
->field_size
;
2449 g_assert(type
->fields
->len
== 2);
2454 *offset
+= ltt_align(*offset
, get_alignment(field
),
2457 /* remember offset */
2458 field
->offset_root
= *offset
;
2459 field
->fixed_root
= FIELD_FIXED
;
2461 child
= &g_array_index(type
->fields
, LttField
, 0);
2462 ret
= precompute_fields_offsets(fac
, child
, offset
);
2463 g_assert(ret
== 0); /* Seq len cannot have variable len */
2465 child
= &g_array_index(type
->fields
, LttField
, 1);
2466 *offset
+= ltt_align(*offset
, get_alignment(child
),
2468 field
->array_offset
= *offset
;
2469 /* Let the child be variable. */
2470 //ret = precompute_fields_offsets(fac, child, offset);
2472 /* Cannot precompute fields offsets of sequence members, and has
2473 * variable length. */
2483 *offset
+= ltt_align(*offset
, get_alignment(field
),
2485 /* remember offset */
2486 field
->offset_root
= *offset
;
2487 field
->fixed_root
= FIELD_FIXED
;
2489 for(i
=0; i
< type
->fields
->len
; i
++) {
2490 child
= &g_array_index(type
->fields
, LttField
, i
);
2491 ret
= precompute_fields_offsets(fac
, child
, offset
);
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 *offset
= field
->offset_root
;
2512 child
= &g_array_index(type
->fields
, LttField
, i
);
2513 ret
= precompute_fields_offsets(fac
, child
, offset
);
2517 *offset
= field
->offset_root
+ field
->field_size
;
2524 g_error("precompute_fields_offsets : unknown type");
2531 /*****************************************************************************
2533 * precompute_offsets : set the precomputable offset of an event type
2536 * event : event type
2538 ****************************************************************************/
2539 void precompute_offsets(LttFacility
*fac
, LttEventType
*event
)
2545 /* First, compute the size of fixed size fields. Will determine size for
2546 * arrays, struct and unions, which is not done by the parser */
2547 for(i
=0; i
<event
->fields
->len
; i
++) {
2548 LttField
*field
= &g_array_index(event
->fields
, LttField
, i
);
2549 field_compute_static_size(fac
, field
);
2552 /* Precompute all known offsets */
2553 for(i
=0; i
<event
->fields
->len
; i
++) {
2554 LttField
*field
= &g_array_index(event
->fields
, LttField
, i
);
2555 ret
= precompute_fields_offsets(fac
, field
, &offset
);
2563 /*****************************************************************************
2565 * preset_field_type_size : set the fixed sizes of the field type
2568 * event_type : event type
2569 * offset_root : offset from the root
2570 * offset_parent : offset from the parent
2571 * fixed_root : Do we know a fixed offset to the root ?
2572 * fixed_parent : Do we know a fixed offset to the parent ?
2574 ****************************************************************************/
2578 // preset the fixed size offsets. Calculate them just like genevent-new : an
2579 // increment of a *to value that represents the offset from the start of the
2581 // The preset information is : offsets up to (and including) the first element
2582 // of variable size. All subsequent fields must be flagged "VARIABLE OFFSET".
2584 void preset_field_type_size(LttTracefile
*tf
, LttEventType
*event_type
,
2585 off_t offset_root
, off_t offset_parent
,
2586 enum field_status
*fixed_root
, enum field_status
*fixed_parent
,
2589 enum field_status local_fixed_root
, local_fixed_parent
;
2593 g_assert(field
->fixed_root
== FIELD_UNKNOWN
);
2594 g_assert(field
->fixed_parent
== FIELD_UNKNOWN
);
2595 g_assert(field
->fixed_size
== FIELD_UNKNOWN
);
2597 type
= field
->field_type
;
2599 field
->fixed_root
= *fixed_root
;
2600 if(field
->fixed_root
== FIELD_FIXED
)
2601 field
->offset_root
= offset_root
;
2603 field
->offset_root
= 0;
2605 field
->fixed_parent
= *fixed_parent
;
2606 if(field
->fixed_parent
== FIELD_FIXED
)
2607 field
->offset_parent
= offset_parent
;
2609 field
->offset_parent
= 0;
2611 size_t current_root_offset
;
2612 size_t current_offset
;
2613 enum field_status current_child_status
, final_child_status
;
2616 switch(type
->type_class
) {
2621 field
->field_size
= ltt_type_size(tf
->trace
, type
);
2622 field
->fixed_size
= FIELD_FIXED
;
2625 field
->field_size
= (off_t
)event_type
->facility
->pointer_size
;
2626 field
->fixed_size
= FIELD_FIXED
;
2630 field
->field_size
= (off_t
)event_type
->facility
->long_size
;
2631 field
->fixed_size
= FIELD_FIXED
;
2636 field
->field_size
= (off_t
)event_type
->facility
->size_t_size
;
2637 field
->fixed_size
= FIELD_FIXED
;
2640 local_fixed_root
= FIELD_VARIABLE
;
2641 local_fixed_parent
= FIELD_VARIABLE
;
2642 preset_field_type_size(tf
, event_type
,
2644 &local_fixed_root
, &local_fixed_parent
,
2646 field
->fixed_size
= FIELD_VARIABLE
;
2647 field
->field_size
= 0;
2648 *fixed_root
= FIELD_VARIABLE
;
2649 *fixed_parent
= FIELD_VARIABLE
;
2652 field
->fixed_size
= FIELD_VARIABLE
;
2653 field
->field_size
= 0;
2654 *fixed_root
= FIELD_VARIABLE
;
2655 *fixed_parent
= FIELD_VARIABLE
;
2658 local_fixed_root
= FIELD_VARIABLE
;
2659 local_fixed_parent
= FIELD_VARIABLE
;
2660 preset_field_type_size(tf
, event_type
,
2662 &local_fixed_root
, &local_fixed_parent
,
2664 field
->fixed_size
= field
->child
[0]->fixed_size
;
2665 if(field
->fixed_size
== FIELD_FIXED
) {
2666 field
->field_size
= type
->element_number
* field
->child
[0]->field_size
;
2668 field
->field_size
= 0;
2669 *fixed_root
= FIELD_VARIABLE
;
2670 *fixed_parent
= FIELD_VARIABLE
;
2674 current_root_offset
= field
->offset_root
;
2676 current_child_status
= FIELD_FIXED
;
2677 for(i
=0;i
<type
->element_number
;i
++) {
2678 preset_field_type_size(tf
, event_type
,
2679 current_root_offset
, current_offset
,
2680 fixed_root
, ¤t_child_status
,
2682 if(current_child_status
== FIELD_FIXED
) {
2683 current_root_offset
+= field
->child
[i
]->field_size
;
2684 current_offset
+= field
->child
[i
]->field_size
;
2686 current_root_offset
= 0;
2690 if(current_child_status
!= FIELD_FIXED
) {
2691 *fixed_parent
= current_child_status
;
2692 field
->field_size
= 0;
2693 field
->fixed_size
= current_child_status
;
2695 field
->field_size
= current_offset
;
2696 field
->fixed_size
= FIELD_FIXED
;
2700 current_root_offset
= field
->offset_root
;
2703 final_child_status
= FIELD_FIXED
;
2704 for(i
=0;i
<type
->element_number
;i
++) {
2705 enum field_status current_root_child_status
= FIELD_FIXED
;
2706 enum field_status current_child_status
= FIELD_FIXED
;
2707 preset_field_type_size(tf
, event_type
,
2708 current_root_offset
, current_offset
,
2709 ¤t_root_child_status
, ¤t_child_status
,
2711 if(current_child_status
!= FIELD_FIXED
)
2712 final_child_status
= current_child_status
;
2714 max_size
= max(max_size
, field
->child
[i
]->field_size
);
2716 if(final_child_status
!= FIELD_FIXED
) {
2717 g_error("LTTV does not support variable size fields in unions.");
2718 /* This will stop the application. */
2719 *fixed_root
= final_child_status
;
2720 *fixed_parent
= final_child_status
;
2721 field
->field_size
= 0;
2722 field
->fixed_size
= current_child_status
;
2724 field
->field_size
= max_size
;
2725 field
->fixed_size
= FIELD_FIXED
;
2733 /*****************************************************************************
2735 * check_fields_compatibility : Check for compatibility between two fields :
2736 * do they use the same inner structure ?
2738 * event_type1 : event type
2739 * event_type2 : event type
2742 *Returns : 0 if identical
2744 ****************************************************************************/
2745 // this function checks for equality of field types. Therefore, it does not use
2746 // per se offsets. For instance, an aligned version of a structure is
2747 // compatible with an unaligned version of the same structure.
2748 gint
check_fields_compatibility(LttEventType
*event_type1
,
2749 LttEventType
*event_type2
,
2750 LttField
*field1
, LttField
*field2
)
2752 guint different
= 0;
2756 if(field1
== NULL
) {
2757 if(field2
== NULL
) goto end
;
2762 } else if(field2
== NULL
) {
2767 type1
= &field1
->field_type
;
2768 type2
= &field2
->field_type
;
2770 if(type1
->type_class
!= type2
->type_class
) {
2774 if(type1
->network
!= type2
->network
) {
2779 switch(type1
->type_class
) {
2781 case LTT_UINT_FIXED
:
2796 if(field1
->field_size
!= field2
->field_size
)
2803 LttField
*child1
= &g_array_index(type1
->fields
, LttField
, 0);
2804 LttField
*child2
= &g_array_index(type2
->fields
, LttField
, 0);
2806 if(type1
->size
!= type2
->size
)
2808 if(check_fields_compatibility(event_type1
, event_type2
, child1
, child2
))
2814 LttField
*child1
= &g_array_index(type1
->fields
, LttField
, 1);
2815 LttField
*child2
= &g_array_index(type2
->fields
, LttField
, 1);
2817 if(check_fields_compatibility(event_type1
, event_type2
, child1
, child2
))
2827 if(type1
->fields
->len
!= type2
->fields
->len
) {
2832 for(i
=0; i
< type1
->fields
->len
; i
++) {
2835 child1
= &g_array_index(type1
->fields
, LttField
, i
);
2836 child2
= &g_array_index(type2
->fields
, LttField
, i
);
2837 different
= check_fields_compatibility(event_type1
,
2838 event_type2
, child1
, child2
);
2840 if(different
) break;
2846 g_error("precompute_fields_offsets : unknown type");
2855 gint
check_fields_compatibility(LttEventType
*event_type1
,
2856 LttEventType
*event_type2
,
2857 LttField
*field1
, LttField
*field2
)
2859 guint different
= 0;
2864 if(field1
== NULL
) {
2865 if(field2
== NULL
) goto end
;
2870 } else if(field2
== NULL
) {
2875 g_assert(field1
->fixed_root
!= FIELD_UNKNOWN
);
2876 g_assert(field2
->fixed_root
!= FIELD_UNKNOWN
);
2877 g_assert(field1
->fixed_parent
!= FIELD_UNKNOWN
);
2878 g_assert(field2
->fixed_parent
!= FIELD_UNKNOWN
);
2879 g_assert(field1
->fixed_size
!= FIELD_UNKNOWN
);
2880 g_assert(field2
->fixed_size
!= FIELD_UNKNOWN
);
2882 type1
= field1
->field_type
;
2883 type2
= field2
->field_type
;
2885 if(type1
->type_class
!= type2
->type_class
) {
2889 if(type1
->element_name
!= type2
->element_name
) {
2894 switch(type1
->type_class
) {
2904 if(field1
->field_size
!= field2
->field_size
) {
2910 if(type1
->element_number
!= type2
->element_number
) {
2914 for(i
=0;i
<type1
->element_number
;i
++) {
2915 if(type1
->enum_strings
[i
] != type2
->enum_strings
[i
]) {
2922 /* Two elements : size and child */
2923 g_assert(type1
->element_number
!= type2
->element_number
);
2924 for(i
=0;i
<type1
->element_number
;i
++) {
2925 if(check_fields_compatibility(event_type1
, event_type2
,
2926 field1
->child
[0], field2
->child
[0])) {
2935 if(field1
->field_size
!= field2
->field_size
) {
2939 /* Two elements : size and child */
2940 g_assert(type1
->element_number
!= type2
->element_number
);
2941 for(i
=0;i
<type1
->element_number
;i
++) {
2942 if(check_fields_compatibility(event_type1
, event_type2
,
2943 field1
->child
[0], field2
->child
[0])) {
2951 if(type1
->element_number
!= type2
->element_number
) {
2955 for(i
=0;i
<type1
->element_number
;i
++) {
2956 if(check_fields_compatibility(event_type1
, event_type2
,
2957 field1
->child
[0], field2
->child
[0])) {
2970 /*****************************************************************************
2972 * ltt_get_int : get an integer number
2974 * reverse_byte_order: must we reverse the byte order ?
2975 * size : the size of the integer
2976 * ptr : the data pointer
2978 * gint64 : a 64 bits integer
2979 ****************************************************************************/
2981 gint64
ltt_get_int(gboolean reverse_byte_order
, gint size
, void *data
)
2986 case 1: val
= *((gint8
*)data
); break;
2987 case 2: val
= ltt_get_int16(reverse_byte_order
, data
); break;
2988 case 4: val
= ltt_get_int32(reverse_byte_order
, data
); break;
2989 case 8: val
= ltt_get_int64(reverse_byte_order
, data
); break;
2990 default: val
= ltt_get_int64(reverse_byte_order
, data
);
2991 g_critical("get_int : integer size %d unknown", size
);
2998 /*****************************************************************************
3000 * ltt_get_uint : get an unsigned integer number
3002 * reverse_byte_order: must we reverse the byte order ?
3003 * size : the size of the integer
3004 * ptr : the data pointer
3006 * guint64 : a 64 bits unsigned integer
3007 ****************************************************************************/
3009 guint64
ltt_get_uint(gboolean reverse_byte_order
, gint size
, void *data
)
3014 case 1: val
= *((gint8
*)data
); break;
3015 case 2: val
= ltt_get_uint16(reverse_byte_order
, data
); break;
3016 case 4: val
= ltt_get_uint32(reverse_byte_order
, data
); break;
3017 case 8: val
= ltt_get_uint64(reverse_byte_order
, data
); break;
3018 default: val
= ltt_get_uint64(reverse_byte_order
, data
);
3019 g_critical("get_uint : unsigned integer size %d unknown",
3028 /* get the node name of the system */
3030 char * ltt_trace_system_description_node_name (LttSystemDescription
* s
)
3032 return s
->node_name
;
3036 /* get the domain name of the system */
3038 char * ltt_trace_system_description_domain_name (LttSystemDescription
* s
)
3040 return s
->domain_name
;
3044 /* get the description of the system */
3046 char * ltt_trace_system_description_description (LttSystemDescription
* s
)
3048 return s
->description
;
3052 /* get the NTP corrected start time of the trace */
3053 LttTime
ltt_trace_start_time(LttTrace
*t
)
3055 return t
->start_time
;
3058 /* get the monotonic start time of the trace */
3059 LttTime
ltt_trace_start_time_monotonic(LttTrace
*t
)
3061 return t
->start_time_from_tsc
;
3064 LttTracefile
*ltt_tracefile_new()
3066 return g_new(LttTracefile
, 1);
3069 void ltt_tracefile_destroy(LttTracefile
*tf
)
3074 void ltt_tracefile_copy(LttTracefile
*dest
, const LttTracefile
*src
)
3079 /* Before library loading... */
3081 static void __attribute__((constructor
)) init(void)
3083 LTT_FACILITY_NAME_HEARTBEAT
= g_quark_from_string("heartbeat");
3084 LTT_EVENT_NAME_HEARTBEAT
= g_quark_from_string("heartbeat");
3086 LTT_TRACEFILE_NAME_FACILITIES
= g_quark_from_string("/control/facilities");