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
;
235 t
->freq_scale
= any
->freq_scale
;
239 switch(any
->major_version
) {
242 switch(any
->minor_version
) {
245 tf
->buffer_header_size
=
246 sizeof(struct ltt_block_start_header
)
247 + sizeof(struct ltt_trace_header_0_3
);
248 g_warning("Unsupported trace version : %hhu.%hhu",
249 any
->major_version
, any
->minor_version
);
255 struct ltt_trace_header_0_7
*vheader
=
256 (struct ltt_trace_header_0_7
*)header
;
257 tf
->buffer_header_size
=
258 sizeof(struct ltt_block_start_header
)
259 + sizeof(struct ltt_trace_header_0_7
);
261 t
->start_freq
= ltt_get_uint64(LTT_GET_BO(tf
),
262 &vheader
->start_freq
);
263 t
->start_tsc
= ltt_get_uint64(LTT_GET_BO(tf
),
264 &vheader
->start_tsc
);
265 t
->start_monotonic
= ltt_get_uint64(LTT_GET_BO(tf
),
266 &vheader
->start_monotonic
);
267 t
->start_time
.tv_sec
= ltt_get_uint64(LTT_GET_BO(tf
),
268 &vheader
->start_time_sec
);
269 t
->start_time
.tv_nsec
= ltt_get_uint64(LTT_GET_BO(tf
),
270 &vheader
->start_time_usec
);
271 t
->start_time
.tv_nsec
*= 1000; /* microsec to nanosec */
273 t
->start_time_from_tsc
= ltt_time_from_uint64(
274 (double)t
->start_tsc
* 1000000.0 / (double)t
->start_freq
);
279 g_warning("Unsupported trace version : %hhu.%hhu",
280 any
->major_version
, any
->minor_version
);
286 g_warning("Unsupported trace version : %hhu.%hhu",
287 any
->major_version
, any
->minor_version
);
297 /*****************************************************************************
299 * ltt_tracefile_open : open a trace file, construct a LttTracefile
301 * t : the trace containing the tracefile
302 * fileName : path name of the trace file
303 * tf : the tracefile structure
305 * : 0 for success, -1 otherwise.
306 ****************************************************************************/
308 gint
ltt_tracefile_open(LttTrace
*t
, gchar
* fileName
, LttTracefile
*tf
)
310 struct stat lTDFStat
; /* Trace data file status */
311 struct ltt_block_start_header
*header
;
312 int page_size
= getpagesize();
315 tf
->long_name
= g_quark_from_string(fileName
);
317 tf
->fd
= open(fileName
, O_RDONLY
);
319 g_warning("Unable to open input data file %s\n", fileName
);
323 // Get the file's status
324 if(fstat(tf
->fd
, &lTDFStat
) < 0){
325 g_warning("Unable to get the status of the input data file %s\n", fileName
);
329 // Is the file large enough to contain a trace
330 if(lTDFStat
.st_size
<
331 (off_t
)(sizeof(struct ltt_block_start_header
)
332 + sizeof(struct ltt_trace_header_any
))){
333 g_print("The input data file %s does not contain a trace\n", fileName
);
337 /* Temporarily map the buffer start header to get trace information */
338 /* Multiple of pages aligned head */
339 tf
->buffer
.head
= mmap(0,
340 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)
341 + sizeof(struct ltt_trace_header_any
)), PROT_READ
,
342 MAP_PRIVATE
, tf
->fd
, 0);
343 if(tf
->buffer
.head
== MAP_FAILED
) {
344 perror("Error in allocating memory for buffer of tracefile");
347 g_assert( ( (guint
)tf
->buffer
.head
&(8-1) ) == 0); // make sure it's aligned.
349 header
= (struct ltt_block_start_header
*)tf
->buffer
.head
;
351 if(parse_trace_header(header
->trace
, tf
, NULL
)) {
352 g_warning("parse_trace_header error");
356 //store the size of the file
357 tf
->file_size
= lTDFStat
.st_size
;
358 tf
->buf_size
= ltt_get_uint32(LTT_GET_BO(tf
), &header
->buf_size
);
359 tf
->num_blocks
= tf
->file_size
/ tf
->buf_size
;
361 if(munmap(tf
->buffer
.head
,
362 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)
363 + sizeof(struct ltt_trace_header_any
)))) {
364 g_warning("unmap size : %u\n",
365 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)
366 + sizeof(struct ltt_trace_header_any
)));
367 perror("munmap error");
370 tf
->buffer
.head
= NULL
;
372 //read the first block
373 if(map_block(tf
,0)) {
374 perror("Cannot map block for tracefile");
382 if(munmap(tf
->buffer
.head
,
383 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)
384 + sizeof(struct ltt_trace_header_any
)))) {
385 g_warning("unmap size : %u\n",
386 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)
387 + sizeof(struct ltt_trace_header_any
)));
388 perror("munmap error");
397 LttTrace
*ltt_tracefile_get_trace(LttTracefile
*tf
)
403 /*****************************************************************************
404 *Open control and per cpu tracefiles
405 ****************************************************************************/
407 void ltt_tracefile_open_cpu(LttTrace
*t
, gchar
* tracefile_name
)
410 tf
= ltt_tracefile_open(t
,tracefile_name
);
412 t
->per_cpu_tracefile_number
++;
413 g_ptr_array_add(t
->per_cpu_tracefiles
, tf
);
416 gint
ltt_tracefile_open_control(LttTrace
*t
, gchar
* control_name
)
425 tf
= ltt_tracefile_open(t
,control_name
);
427 g_warning("ltt_tracefile_open_control : bad file descriptor");
430 t
->control_tracefile_number
++;
431 g_ptr_array_add(t
->control_tracefiles
,tf
);
433 //parse facilities tracefile to get base_id
434 if(strcmp(&control_name
[strlen(control_name
)-10],"facilities") ==0){
436 if(!ltt_tracefile_read(tf
,&ev
)) return 0; // end of file
438 if(ev
.event_id
== TRACE_FACILITY_LOAD
){
440 fLoad
.name
= (gchar
*)pos
;
441 fLoad
.checksum
= *(LttChecksum
*)(pos
+ strlen(fLoad
.name
));
442 fLoad
.base_code
= *(guint32
*)(pos
+ strlen(fLoad
.name
) + sizeof(LttChecksum
));
444 for(i
=0;i
<t
->facility_number
;i
++){
445 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
,i
);
446 if(strcmp(f
->name
,fLoad
.name
)==0 && fLoad
.checksum
==f
->checksum
){
447 f
->base_id
= fLoad
.base_code
;
451 if(i
==t
->facility_number
) {
452 g_warning("Facility: %s, checksum: %u is not found",
453 fLoad
.name
,(unsigned int)fLoad
.checksum
);
456 }else if(ev
.event_id
== TRACE_BLOCK_START
){
458 }else if(ev
.event_id
== TRACE_BLOCK_END
){
461 g_warning("Not valid facilities trace file");
470 /*****************************************************************************
472 * ltt_tracefile_close: close a trace file,
474 * t : tracefile which will be closed
475 ****************************************************************************/
477 void ltt_tracefile_close(LttTracefile
*t
)
479 int page_size
= getpagesize();
481 if(t
->buffer
.head
!= NULL
)
482 if(munmap(t
->buffer
.head
, PAGE_ALIGN(t
->buf_size
))) {
483 g_warning("unmap size : %u\n",
484 PAGE_ALIGN(t
->buf_size
));
485 perror("munmap error");
493 /*****************************************************************************
494 *Get system information
495 ****************************************************************************/
497 gint
getSystemInfo(LttSystemDescription
* des
, gchar
* pathname
)
504 GMarkupParseContext
* context
;
505 GError
* error
= NULL
;
506 GMarkupParser markup_parser
=
508 parser_start_element
,
511 NULL
, /* passthrough */
515 fd
= g_open(pathname
, O_RDONLY
, 0);
517 g_warning("Can not open file : %s\n", pathname
);
521 iochan
= g_io_channel_unix_new(fd
);
523 context
= g_markup_parse_context_new(&markup_parser
, 0, des
,NULL
);
525 //while(fgets(buf,DIR_NAME_SIZE, fp) != NULL){
526 while(g_io_channel_read_line(iochan
, &buf
, &length
, NULL
, &error
)
527 != G_IO_STATUS_EOF
) {
530 g_warning("Can not read xml file: \n%s\n", error
->message
);
533 if(!g_markup_parse_context_parse(context
, buf
, length
, &error
)){
535 g_warning("Can not parse xml file: \n%s\n", error
->message
);
538 g_markup_parse_context_free(context
);
540 g_io_channel_shutdown(iochan
, FALSE
, &error
); /* No flush */
542 g_warning("Can not close file: \n%s\n", error
->message
);
550 g_markup_parse_context_free(context
);
552 g_io_channel_shutdown(iochan
, FALSE
, &error
); /* No flush */
554 g_warning("Can not close file: \n%s\n", error
->message
);
565 /*****************************************************************************
566 *The following functions get facility/tracefile information
567 ****************************************************************************/
569 gint
getFacilityInfo(LttTrace
*t
, gchar
* eventdefs
)
576 gchar fullname
[DIR_NAME_SIZE
];
577 GError
* error
= NULL
;
579 dir
= g_dir_open(eventdefs
, 0, &error
);
582 g_warning("Can not open directory: %s, %s\n", eventdefs
, error
->message
);
587 while((name
= g_dir_read_name(dir
)) != NULL
){
588 if(!g_pattern_match_simple("*.xml", name
)) continue;
589 strcpy(fullname
,eventdefs
);
590 strcat(fullname
,name
);
591 ltt_facility_open(t
,fullname
);
595 for(j
=0;j
<t
->facility_number
;j
++){
596 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, j
);
597 for(i
=0; i
<f
->event_number
; i
++){
599 setFieldsOffset(NULL
, et
, NULL
, t
);
606 /*****************************************************************************
607 *A trace is specified as a pathname to the directory containing all the
608 *associated data (control tracefiles, per cpu tracefiles, event
611 *When a trace is closed, all the associated facilities, types and fields
612 *are released as well.
616 /****************************************************************************
617 * get_absolute_pathname
619 * return the unique pathname in the system
621 * MD : Fixed this function so it uses realpath, dealing well with
622 * forgotten cases (.. were not used correctly before).
624 ****************************************************************************/
625 void get_absolute_pathname(const gchar
*pathname
, gchar
* abs_pathname
)
627 abs_pathname
[0] = '\0';
629 if ( realpath (pathname
, abs_pathname
) != NULL
)
633 /* error, return the original path unmodified */
634 strcpy(abs_pathname
, pathname
);
640 /* Search for something like : .*_.*
642 * The left side is the name, the right side is the number.
645 int get_tracefile_name_number(gchar
*raw_name
,
652 guint raw_name_len
= strlen(raw_name
);
653 gchar char_name
[PATH_MAX
];
660 for(i
=raw_name_len
-1;i
>=0;i
--) {
661 if(raw_name
[i
] == '_') break;
663 if(i
==-1) { /* Either not found or name length is 0 */
664 /* This is a userspace tracefile */
665 strncpy(char_name
, raw_name
, raw_name_len
);
666 *name
= g_quark_from_string(char_name
);
667 *num
= 0; /* unknown cpu */
668 for(i
=0;i
<raw_name_len
;i
++) {
669 if(raw_name
[i
] == '/') {
674 for(;i
<raw_name_len
;i
++) {
675 if(raw_name
[i
] == '/') {
680 for(;i
<raw_name_len
;i
++) {
681 if(raw_name
[i
] == '-') {
685 if(i
== raw_name_len
) return -1;
687 tmpptr
= &raw_name
[i
];
688 for(;i
<raw_name_len
;i
++) {
689 if(raw_name
[i
] == '.') {
694 *tid
= strtoul(tmpptr
, &endptr
, 10);
696 return -1; /* No digit */
697 if(*tid
== ULONG_MAX
)
698 return -1; /* underflow / overflow */
700 tmpptr
= &raw_name
[i
];
701 for(;i
<raw_name_len
;i
++) {
702 if(raw_name
[i
] == '.') {
707 *pgid
= strtoul(tmpptr
, &endptr
, 10);
709 return -1; /* No digit */
710 if(*pgid
== ULONG_MAX
)
711 return -1; /* underflow / overflow */
713 tmpptr
= &raw_name
[i
];
714 *creation
= strtoull(tmpptr
, &endptr
, 10);
716 return -1; /* No digit */
717 if(*creation
== G_MAXUINT64
)
718 return -1; /* underflow / overflow */
722 cpu_num
= strtol(raw_name
+underscore_pos
+1, &endptr
, 10);
724 if(endptr
== raw_name
+underscore_pos
+1)
725 return -1; /* No digit */
726 if(cpu_num
== LONG_MIN
|| cpu_num
== LONG_MAX
)
727 return -1; /* underflow / overflow */
729 strncpy(char_name
, raw_name
, underscore_pos
);
730 char_name
[underscore_pos
] = '\0';
732 *name
= g_quark_from_string(char_name
);
741 GData
**ltt_trace_get_tracefiles_groups(LttTrace
*trace
)
743 return &trace
->tracefiles
;
747 void compute_tracefile_group(GQuark key_id
,
749 struct compute_tracefile_group_args
*args
)
754 for(i
=0; i
<group
->len
; i
++) {
755 tf
= &g_array_index (group
, LttTracefile
, i
);
757 args
->func(tf
, args
->func_args
);
762 void ltt_tracefile_group_destroy(gpointer data
)
764 GArray
*group
= (GArray
*)data
;
768 for(i
=0; i
<group
->len
; i
++) {
769 tf
= &g_array_index (group
, LttTracefile
, i
);
771 ltt_tracefile_close(tf
);
773 g_array_free(group
, TRUE
);
776 gboolean
ltt_tracefile_group_has_cpu_online(gpointer data
)
778 GArray
*group
= (GArray
*)data
;
782 for(i
=0; i
<group
->len
; i
++) {
783 tf
= &g_array_index (group
, LttTracefile
, i
);
784 if(tf
->cpu_online
) return 1;
790 /* Open each tracefile under a specific directory. Put them in a
791 * GData : permits to access them using their tracefile group pathname.
792 * i.e. access control/modules tracefile group by index :
795 * relative path is the path relative to the trace root
796 * root path is the full path
798 * A tracefile group is simply an array where all the per cpu tracefiles sits.
801 static int open_tracefiles(LttTrace
*trace
, gchar
*root_path
,
802 gchar
*relative_path
)
804 DIR *dir
= opendir(root_path
);
805 struct dirent
*entry
;
806 struct stat stat_buf
;
809 gchar path
[PATH_MAX
];
814 gchar rel_path
[PATH_MAX
];
823 strncpy(path
, root_path
, PATH_MAX
-1);
824 path_len
= strlen(path
);
825 path
[path_len
] = '/';
827 path_ptr
= path
+ path_len
;
829 strncpy(rel_path
, relative_path
, PATH_MAX
-1);
830 rel_path_len
= strlen(rel_path
);
831 rel_path
[rel_path_len
] = '/';
833 rel_path_ptr
= rel_path
+ rel_path_len
;
835 while((entry
= readdir(dir
)) != NULL
) {
837 if(entry
->d_name
[0] == '.') continue;
839 strncpy(path_ptr
, entry
->d_name
, PATH_MAX
- path_len
);
840 strncpy(rel_path_ptr
, entry
->d_name
, PATH_MAX
- rel_path_len
);
842 ret
= stat(path
, &stat_buf
);
848 g_debug("Tracefile file or directory : %s\n", path
);
850 if(strcmp(rel_path
, "/eventdefs") == 0) continue;
852 if(S_ISDIR(stat_buf
.st_mode
)) {
854 g_debug("Entering subdirectory...\n");
855 ret
= open_tracefiles(trace
, path
, rel_path
);
856 if(ret
< 0) continue;
857 } else if(S_ISREG(stat_buf
.st_mode
)) {
859 guint num
, tid
, pgid
;
862 num
= tid
= pgid
= 0;
864 if(get_tracefile_name_number(rel_path
, &name
, &num
, &tid
, &pgid
, &creation
))
865 continue; /* invalid name */
867 g_debug("Opening file.\n");
868 if(ltt_tracefile_open(trace
, path
, &tmp_tf
)) {
869 g_info("Error opening tracefile %s", path
);
871 continue; /* error opening the tracefile : bad magic number ? */
874 g_debug("Tracefile name is %s and number is %u",
875 g_quark_to_string(name
), num
);
877 tmp_tf
.cpu_online
= 1;
878 tmp_tf
.cpu_num
= num
;
882 tmp_tf
.creation
= creation
;
884 group
= g_datalist_id_get_data(&trace
->tracefiles
, name
);
886 /* Elements are automatically cleared when the array is allocated.
887 * It makes the cpu_online variable set to 0 : cpu offline, by default.
889 group
= g_array_sized_new (FALSE
, TRUE
, sizeof(LttTracefile
), 10);
890 g_datalist_id_set_data_full(&trace
->tracefiles
, name
,
891 group
, ltt_tracefile_group_destroy
);
894 /* Add the per cpu tracefile to the named group */
895 unsigned int old_len
= group
->len
;
897 group
= g_array_set_size(group
, num
+1);
898 g_array_index (group
, LttTracefile
, num
) = tmp_tf
;
908 /* ltt_get_facility_description
910 * Opens the file corresponding to the requested facility (identified by fac_id
913 * The name searched is : %trace root%/eventdefs/facname_checksum.xml
915 * Returns 0 on success, or 1 on failure.
918 static int ltt_get_facility_description(LttFacility
*f
,
920 LttTracefile
*fac_tf
)
922 char desc_file_name
[PATH_MAX
];
929 text
= g_quark_to_string(t
->pathname
);
930 textlen
= strlen(text
);
932 if(textlen
>= PATH_MAX
) goto name_error
;
933 strcpy(desc_file_name
, text
);
935 text
= "/eventdefs/";
936 textlen
+=strlen(text
);
937 if(textlen
>= PATH_MAX
) goto name_error
;
938 strcat(desc_file_name
, text
);
940 text
= g_quark_to_string(f
->name
);
941 fac_name_len
= strlen(text
);
942 textlen
+=fac_name_len
;
943 if(textlen
>= PATH_MAX
) goto name_error
;
944 strcat(desc_file_name
, text
);
946 /* arch specific facilities are named like this : name_arch */
947 if(fac_name_len
+1 < sizeof("_arch"))
950 if(!strcmp(&text
[fac_name_len
+1-sizeof("_arch")], "_arch"))
958 textlen
+=strlen(text
);
959 if(textlen
>= PATH_MAX
) goto name_error
;
960 strcat(desc_file_name
, text
);
962 err
= snprintf(desc_file_name
+textlen
, PATH_MAX
-textlen
-1,
964 if(err
< 0) goto name_error
;
966 textlen
=strlen(desc_file_name
);
971 switch(t
->arch_type
) {
972 case LTT_ARCH_TYPE_I386
:
975 case LTT_ARCH_TYPE_PPC
:
978 case LTT_ARCH_TYPE_SH
:
981 case LTT_ARCH_TYPE_S390
:
984 case LTT_ARCH_TYPE_MIPS
:
987 case LTT_ARCH_TYPE_ARM
:
990 case LTT_ARCH_TYPE_PPC64
:
993 case LTT_ARCH_TYPE_X86_64
:
996 case LTT_ARCH_TYPE_C2
:
1000 g_error("Trace from unsupported architecture.");
1002 textlen
+=strlen(text
);
1003 if(textlen
>= PATH_MAX
) goto name_error
;
1004 strcat(desc_file_name
, text
);
1008 textlen
+=strlen(text
);
1009 if(textlen
>= PATH_MAX
) goto name_error
;
1010 strcat(desc_file_name
, text
);
1012 err
= ltt_facility_open(f
, t
, desc_file_name
);
1013 if(err
) goto facility_error
;
1022 static void ltt_fac_ids_destroy(gpointer data
)
1024 GArray
*fac_ids
= (GArray
*)data
;
1026 g_array_free(fac_ids
, TRUE
);
1030 /* Presumes the tracefile is already seeked at the beginning. It makes sense,
1031 * because it must be done just after the opening */
1032 static int ltt_process_facility_tracefile(LttTracefile
*tf
)
1041 err
= ltt_tracefile_read_seek(tf
);
1042 if(err
== EPERM
) goto seek_error
;
1043 else if(err
== ERANGE
) break; /* End of tracefile */
1045 err
= ltt_tracefile_read_update_event(tf
);
1046 if(err
) goto update_error
;
1048 /* We are on a facility load/or facility unload/ or heartbeat event */
1050 * * facility 0 is hardcoded : this is the core facility. It will be shown
1051 * in the facility array though, and is shown as "loaded builtin" in the
1053 * It contains event :
1055 * 1 : facility unload
1056 * 2 : state dump facility load
1059 if(tf
->event
.facility_id
!= LTT_FACILITY_CORE
) {
1060 /* Should only contain core facility */
1061 g_warning("Error in processing facility file %s, "
1062 "should not contain facility id %u.", g_quark_to_string(tf
->name
),
1063 tf
->event
.facility_id
);
1068 struct LttFacilityLoad
*fac_load_data
;
1069 struct LttStateDumpFacilityLoad
*fac_state_dump_load_data
;
1074 switch((enum ltt_core_events
)tf
->event
.event_id
) {
1075 case LTT_EVENT_FACILITY_LOAD
:
1076 fac_name
= (char*)(tf
->event
.data
);
1077 g_debug("Doing LTT_EVENT_FACILITY_LOAD of facility %s",
1079 pos
= (tf
->event
.data
+ strlen(fac_name
) + 1);
1080 pos
+= ltt_align((size_t)pos
, sizeof(guint32
), tf
->has_alignment
);
1081 fac_load_data
= (struct LttFacilityLoad
*)pos
;
1083 fac
= &g_array_index (tf
->trace
->facilities_by_num
, LttFacility
,
1084 ltt_get_uint32(LTT_GET_BO(tf
), &fac_load_data
->id
));
1085 /* facility may already exist if trace is paused/unpaused */
1086 if(fac
->exists
) continue;
1087 fac
->name
= g_quark_from_string(fac_name
);
1088 fac
->checksum
= ltt_get_uint32(LTT_GET_BO(tf
),
1089 &fac_load_data
->checksum
);
1090 fac
->id
= ltt_get_uint32(LTT_GET_BO(tf
), &fac_load_data
->id
);
1091 fac
->pointer_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1092 &fac_load_data
->pointer_size
);
1093 fac
->int_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1094 &fac_load_data
->int_size
);
1095 fac
->long_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1096 &fac_load_data
->long_size
);
1097 fac
->size_t_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1098 &fac_load_data
->size_t_size
);
1099 fac
->alignment
= ltt_get_uint32(LTT_GET_BO(tf
),
1100 &fac_load_data
->has_alignment
);
1102 if(ltt_get_facility_description(fac
, tf
->trace
, tf
))
1103 continue; /* error opening description */
1105 fac
->trace
= tf
->trace
;
1107 /* Preset the field offsets */
1108 for(i
=0; i
<fac
->events
->len
; i
++){
1109 et
= &g_array_index(fac
->events
, LttEventType
, i
);
1110 precompute_offsets(fac
, et
);
1115 fac_ids
= g_datalist_id_get_data(&tf
->trace
->facilities_by_name
,
1117 if(fac_ids
== NULL
) {
1118 fac_ids
= g_array_sized_new (FALSE
, TRUE
, sizeof(guint
), 1);
1119 g_datalist_id_set_data_full(&tf
->trace
->facilities_by_name
,
1121 fac_ids
, ltt_fac_ids_destroy
);
1123 g_array_append_val(fac_ids
, fac
->id
);
1126 case LTT_EVENT_FACILITY_UNLOAD
:
1127 g_debug("Doing LTT_EVENT_FACILITY_UNLOAD");
1128 /* We don't care about unload : facilities ID are valid for the whole
1129 * trace. They simply won't be used after the unload. */
1131 case LTT_EVENT_STATE_DUMP_FACILITY_LOAD
:
1132 fac_name
= (char*)(tf
->event
.data
);
1133 g_debug("Doing LTT_EVENT_STATE_DUMP_FACILITY_LOAD of facility %s",
1135 pos
= (tf
->event
.data
+ strlen(fac_name
) + 1);
1136 pos
+= ltt_align((size_t)pos
, sizeof(guint32
), tf
->has_alignment
);
1137 fac_state_dump_load_data
= (struct LttStateDumpFacilityLoad
*)pos
;
1139 fac
= &g_array_index (tf
->trace
->facilities_by_num
, LttFacility
,
1140 ltt_get_uint32(LTT_GET_BO(tf
), &fac_state_dump_load_data
->id
));
1141 /* facility may already exist if trace is paused/unpaused */
1142 if(fac
->exists
) continue;
1143 fac
->name
= g_quark_from_string(fac_name
);
1144 fac
->checksum
= ltt_get_uint32(LTT_GET_BO(tf
),
1145 &fac_state_dump_load_data
->checksum
);
1146 fac
->id
= ltt_get_uint32(LTT_GET_BO(tf
),
1147 &fac_state_dump_load_data
->id
);
1148 fac
->pointer_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1149 &fac_state_dump_load_data
->pointer_size
);
1150 fac
->int_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1151 &fac_state_dump_load_data
->int_size
);
1152 fac
->long_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1153 &fac_state_dump_load_data
->long_size
);
1154 fac
->size_t_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1155 &fac_state_dump_load_data
->size_t_size
);
1156 fac
->alignment
= ltt_get_uint32(LTT_GET_BO(tf
),
1157 &fac_state_dump_load_data
->has_alignment
);
1158 if(ltt_get_facility_description(fac
, tf
->trace
, tf
))
1159 continue; /* error opening description */
1161 fac
->trace
= tf
->trace
;
1163 /* Preset the field offsets */
1164 for(i
=0; i
<fac
->events
->len
; i
++){
1165 et
= &g_array_index(fac
->events
, LttEventType
, i
);
1166 precompute_offsets(fac
, et
);
1171 fac_ids
= g_datalist_id_get_data(&tf
->trace
->facilities_by_name
,
1173 if(fac_ids
== NULL
) {
1174 fac_ids
= g_array_sized_new (FALSE
, TRUE
, sizeof(guint
), 1);
1175 g_datalist_id_set_data_full(&tf
->trace
->facilities_by_name
,
1177 fac_ids
, ltt_fac_ids_destroy
);
1179 g_array_append_val(fac_ids
, fac
->id
);
1182 case LTT_EVENT_HEARTBEAT
:
1185 g_warning("Error in processing facility file %s, "
1186 "unknown event id %hhu in core facility.",
1187 g_quark_to_string(tf
->name
),
1188 tf
->event
.event_id
);
1190 goto event_id_error
;
1196 /* Error handling */
1201 g_warning("An error occured in facility tracefile parsing");
1206 LttTrace
*ltt_trace_open(const gchar
*pathname
)
1208 gchar abs_path
[PATH_MAX
];
1213 struct ltt_block_start_header
*header
;
1215 struct dirent
*entry
;
1216 guint control_found
= 0;
1217 guint eventdefs_found
= 0;
1218 struct stat stat_buf
;
1219 gchar path
[PATH_MAX
];
1221 t
= g_new(LttTrace
, 1);
1222 if(!t
) goto alloc_error
;
1224 get_absolute_pathname(pathname
, abs_path
);
1225 t
->pathname
= g_quark_from_string(abs_path
);
1227 g_datalist_init(&t
->tracefiles
);
1229 /* Test to see if it looks like a trace */
1230 dir
= opendir(abs_path
);
1235 while((entry
= readdir(dir
)) != NULL
) {
1236 strcpy(path
, abs_path
);
1238 strcat(path
, entry
->d_name
);
1239 ret
= stat(path
, &stat_buf
);
1244 if(S_ISDIR(stat_buf
.st_mode
)) {
1245 if(strcmp(entry
->d_name
, "control") == 0) {
1248 if(strcmp(entry
->d_name
, "eventdefs") == 0) {
1249 eventdefs_found
= 1;
1255 if(!control_found
|| !eventdefs_found
) goto find_error
;
1257 /* Open all the tracefiles */
1258 if(open_tracefiles(t
, abs_path
, "")) {
1259 g_warning("Error opening tracefile %s", abs_path
);
1263 /* Prepare the facilities containers : array and mapping */
1264 /* Array is zeroed : the "exists" field is set to false by default */
1265 t
->facilities_by_num
= g_array_sized_new (FALSE
,
1266 TRUE
, sizeof(LttFacility
),
1268 t
->facilities_by_num
= g_array_set_size(t
->facilities_by_num
, NUM_FACILITIES
);
1270 g_datalist_init(&t
->facilities_by_name
);
1272 /* Parse each trace control/facilitiesN files : get runtime fac. info */
1273 group
= g_datalist_id_get_data(&t
->tracefiles
, LTT_TRACEFILE_NAME_FACILITIES
);
1275 g_error("Trace %s has no facility tracefile", abs_path
);
1277 goto facilities_error
;
1280 /* Get the trace information for the control/facility 0 tracefile */
1281 g_assert(group
->len
> 0);
1282 tf
= &g_array_index (group
, LttTracefile
, 0);
1283 header
= (struct ltt_block_start_header
*)tf
->buffer
.head
;
1284 g_assert(parse_trace_header(header
->trace
,
1287 t
->num_cpu
= group
->len
;
1289 for(i
=0; i
<group
->len
; i
++) {
1290 tf
= &g_array_index (group
, LttTracefile
, i
);
1291 if(ltt_process_facility_tracefile(tf
))
1292 goto facilities_error
;
1297 /* Error handling */
1299 g_datalist_clear(&t
->facilities_by_name
);
1300 g_array_free(t
->facilities_by_num
, TRUE
);
1302 g_datalist_clear(&t
->tracefiles
);
1310 GQuark
ltt_trace_name(const LttTrace
*t
)
1316 /******************************************************************************
1317 * When we copy a trace, we want all the opening actions to happen again :
1318 * the trace will be reopened and totally independant from the original.
1319 * That's why we call ltt_trace_open.
1320 *****************************************************************************/
1321 LttTrace
*ltt_trace_copy(LttTrace
*self
)
1323 return ltt_trace_open(g_quark_to_string(self
->pathname
));
1326 void ltt_trace_close(LttTrace
*t
)
1331 for(i
=0; i
<t
->facilities_by_num
->len
; i
++) {
1332 fac
= &g_array_index (t
->facilities_by_num
, LttFacility
, i
);
1334 ltt_facility_close(fac
);
1337 g_datalist_clear(&t
->facilities_by_name
);
1338 g_array_free(t
->facilities_by_num
, TRUE
);
1339 g_datalist_clear(&t
->tracefiles
);
1344 /*****************************************************************************
1345 *Get the system description of the trace
1346 ****************************************************************************/
1348 LttFacility
*ltt_trace_facility_by_id(LttTrace
*t
, guint8 id
)
1350 g_assert(id
< t
->facilities_by_num
->len
);
1351 return &g_array_index(t
->facilities_by_num
, LttFacility
, id
);
1354 /* ltt_trace_facility_get_by_name
1356 * Returns the GArray of facility indexes. All the fac_ids that matches the
1357 * requested facility name.
1359 * If name is not found, returns NULL.
1361 GArray
*ltt_trace_facility_get_by_name(LttTrace
*t
, GQuark name
)
1363 return g_datalist_id_get_data(&t
->facilities_by_name
, name
);
1366 /*****************************************************************************
1367 * Functions to discover all the event types in the trace
1368 ****************************************************************************/
1371 unsigned ltt_trace_eventtype_number(LttTrace
*t
)
1375 unsigned int num
= t
->facility_number
;
1379 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, i
);
1380 count
+= f
->event_number
;
1387 //use an iteration on all the trace facilities, and inside iteration on all the
1388 //event types in each facilities instead.
1389 LttEventType
*ltt_trace_eventtype_get(LttTrace
*t
, unsigned evId
)
1391 LttEventType
*event_type
;
1394 f
= ltt_trace_facility_by_id(t
,evId
);
1396 if(unlikely(!f
)) event_type
= NULL
;
1397 else event_type
= f
->events
[evId
- f
->base_id
];
1404 /*****************************************************************************
1405 * ltt_trace_find_tracefile
1407 * Find a tracefile by name and index in the group.
1409 * Returns a pointer to the tracefiles, else NULL.
1410 ****************************************************************************/
1412 LttTracefile
*ltt_trace_find_tracefile(LttTrace
*t
, const gchar
*name
)
1417 /*****************************************************************************
1418 * Get the start time and end time of the trace
1419 ****************************************************************************/
1421 static void ltt_tracefile_time_span_get(LttTracefile
*tf
,
1422 LttTime
*start
, LttTime
*end
)
1426 err
= map_block(tf
, 0);
1428 g_error("Can not map block");
1429 *start
= ltt_time_infinite
;
1431 *start
= tf
->buffer
.begin
.timestamp
;
1433 err
= map_block(tf
, tf
->num_blocks
- 1); /* Last block */
1435 g_error("Can not map block");
1436 *end
= ltt_time_zero
;
1438 *end
= tf
->buffer
.end
.timestamp
;
1441 struct tracefile_time_span_get_args
{
1447 static void group_time_span_get(GQuark name
, gpointer data
, gpointer user_data
)
1449 struct tracefile_time_span_get_args
*args
=
1450 (struct tracefile_time_span_get_args
*)user_data
;
1452 GArray
*group
= (GArray
*)data
;
1458 for(i
=0; i
<group
->len
; i
++) {
1459 tf
= &g_array_index (group
, LttTracefile
, i
);
1460 if(tf
->cpu_online
) {
1461 ltt_tracefile_time_span_get(tf
, &tmp_start
, &tmp_end
);
1462 if(ltt_time_compare(*args
->start
, tmp_start
)>0) *args
->start
= tmp_start
;
1463 if(ltt_time_compare(*args
->end
, tmp_end
)<0) *args
->end
= tmp_end
;
1468 void ltt_trace_time_span_get(LttTrace
*t
, LttTime
*start
, LttTime
*end
)
1470 LttTime min_start
= ltt_time_infinite
;
1471 LttTime max_end
= ltt_time_zero
;
1472 struct tracefile_time_span_get_args args
= { t
, &min_start
, &max_end
};
1474 g_datalist_foreach(&t
->tracefiles
, &group_time_span_get
, &args
);
1476 if(start
!= NULL
) *start
= min_start
;
1477 if(end
!= NULL
) *end
= max_end
;
1482 /*****************************************************************************
1483 *Get the name of a tracefile
1484 ****************************************************************************/
1486 GQuark
ltt_tracefile_name(const LttTracefile
*tf
)
1491 GQuark
ltt_tracefile_long_name(const LttTracefile
*tf
)
1493 return tf
->long_name
;
1498 guint
ltt_tracefile_cpu(LttTracefile
*tf
)
1503 guint
ltt_tracefile_tid(LttTracefile
*tf
)
1508 guint
ltt_tracefile_pgid(LttTracefile
*tf
)
1513 guint64
ltt_tracefile_creation(LttTracefile
*tf
)
1515 return tf
->creation
;
1517 /*****************************************************************************
1518 * Get the number of blocks in the tracefile
1519 ****************************************************************************/
1521 guint
ltt_tracefile_block_number(LttTracefile
*tf
)
1523 return tf
->num_blocks
;
1527 /* Seek to the first event in a tracefile that has a time equal or greater than
1528 * the time passed in parameter.
1530 * If the time parameter is outside the tracefile time span, seek to the first
1531 * event or if after, return ERANGE.
1533 * If the time parameter is before the first event, we have to seek specially to
1536 * If the time is after the end of the trace, return ERANGE.
1538 * Do a binary search to find the right block, then a sequential search in the
1539 * block to find the event.
1541 * In the special case where the time requested fits inside a block that has no
1542 * event corresponding to the requested time, the first event of the next block
1545 * IMPORTANT NOTE : // FIXME everywhere...
1547 * You MUST NOT do a ltt_tracefile_read right after a ltt_tracefile_seek_time :
1548 * you will jump over an event if you do.
1550 * Return value : 0 : no error, the tf->event can be used
1551 * ERANGE : time if after the last event of the trace
1552 * otherwise : this is an error.
1556 int ltt_tracefile_seek_time(LttTracefile
*tf
, LttTime time
)
1560 unsigned int block_num
, high
, low
;
1562 /* seek at the beginning of trace */
1563 err
= map_block(tf
, 0); /* First block */
1565 g_error("Can not map block");
1569 /* If the time is lower or equal the beginning of the trace,
1570 * go to the first event. */
1571 if(ltt_time_compare(time
, tf
->buffer
.begin
.timestamp
) <= 0) {
1572 ret
= ltt_tracefile_read(tf
);
1573 if(ret
== ERANGE
) goto range
;
1574 else if (ret
) goto fail
;
1575 goto found
; /* There is either no event in the trace or the event points
1576 to the first event in the trace */
1579 err
= map_block(tf
, tf
->num_blocks
- 1); /* Last block */
1581 g_error("Can not map block");
1585 /* If the time is after the end of the trace, return ERANGE. */
1586 if(ltt_time_compare(time
, tf
->buffer
.end
.timestamp
) > 0) {
1590 /* Binary search the block */
1591 high
= tf
->num_blocks
- 1;
1595 block_num
= ((high
-low
) / 2) + low
;
1597 err
= map_block(tf
, block_num
);
1599 g_error("Can not map block");
1603 /* We cannot divide anymore : this is what would happen if the time
1604 * requested was exactly between two consecutive buffers'end and start
1605 * timestamps. This is also what would happend if we didn't deal with out
1606 * of span cases prior in this function. */
1607 /* The event is right in the buffer!
1608 * (or in the next buffer first event) */
1610 ret
= ltt_tracefile_read(tf
);
1611 if(ret
== ERANGE
) goto range
; /* ERANGE or EPERM */
1612 else if(ret
) goto fail
;
1614 if(ltt_time_compare(time
, tf
->event
.event_time
) <= 0)
1618 } else if(ltt_time_compare(time
, tf
->buffer
.begin
.timestamp
) < 0) {
1619 /* go to lower part */
1620 high
= block_num
- 1;
1621 } else if(ltt_time_compare(time
, tf
->buffer
.end
.timestamp
) > 0) {
1622 /* go to higher part */
1623 low
= block_num
+ 1;
1624 } else {/* The event is right in the buffer!
1625 (or in the next buffer first event) */
1627 ret
= ltt_tracefile_read(tf
);
1628 if(ret
== ERANGE
) goto range
; /* ERANGE or EPERM */
1629 else if(ret
) goto fail
;
1631 if(ltt_time_compare(time
, tf
->event
.event_time
) <= 0)
1643 /* Error handling */
1645 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1646 g_quark_to_string(tf
->name
));
1651 int ltt_tracefile_seek_position(LttTracefile
*tf
, const LttEventPosition
*ep
) {
1655 if(ep
->tracefile
!= tf
) {
1659 err
= map_block(tf
, ep
->block
);
1661 g_error("Can not map block");
1665 tf
->event
.offset
= ep
->offset
;
1667 /* Put back the event real tsc */
1668 tf
->event
.tsc
= ep
->tsc
;
1669 tf
->buffer
.tsc
= ep
->tsc
;
1671 err
= ltt_tracefile_read_update_event(tf
);
1673 err
= ltt_tracefile_read_op(tf
);
1679 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1680 g_quark_to_string(tf
->name
));
1684 LttTime
ltt_interpolate_time_from_tsc(LttTracefile
*tf
, guint64 tsc
)
1688 if(tsc
> tf
->trace
->start_tsc
) {
1689 time
= ltt_time_from_uint64(
1690 (double)(tsc
- tf
->trace
->start_tsc
)
1691 * (1000000000.0 / tf
->trace
->freq_scale
)
1692 / (double)tf
->trace
->start_freq
);
1693 time
= ltt_time_add(tf
->trace
->start_time_from_tsc
, time
);
1695 time
= ltt_time_from_uint64(
1696 (double)(tf
->trace
->start_tsc
- tsc
)
1697 * (1000000000.0 / tf
->trace
->freq_scale
)
1698 / (double)tf
->trace
->start_freq
);
1699 time
= ltt_time_sub(tf
->trace
->start_time_from_tsc
, time
);
1704 /* Calculate the real event time based on the buffer boundaries */
1705 LttTime
ltt_interpolate_time(LttTracefile
*tf
, LttEvent
*event
)
1707 return ltt_interpolate_time_from_tsc(tf
, tf
->buffer
.tsc
);
1711 /* Get the current event of the tracefile : valid until the next read */
1712 LttEvent
*ltt_tracefile_get_event(LttTracefile
*tf
)
1719 /*****************************************************************************
1721 * ltt_tracefile_read : Read the next event in the tracefile
1726 * Returns 0 if an event can be used in tf->event.
1727 * Returns ERANGE on end of trace. The event in tf->event still can be used
1728 * (if the last block was not empty).
1729 * Returns EPERM on error.
1731 * This function does make the tracefile event structure point to the event
1732 * currently pointed to by the tf->event.
1734 * Note : you must call a ltt_tracefile_seek to the beginning of the trace to
1735 * reinitialize it after an error if you want results to be coherent.
1736 * It would be the case if a end of trace last buffer has no event : the end
1737 * of trace wouldn't be returned, but an error.
1738 * We make the assumption there is at least one event per buffer.
1739 ****************************************************************************/
1741 int ltt_tracefile_read(LttTracefile
*tf
)
1745 err
= ltt_tracefile_read_seek(tf
);
1747 err
= ltt_tracefile_read_update_event(tf
);
1749 err
= ltt_tracefile_read_op(tf
);
1755 int ltt_tracefile_read_seek(LttTracefile
*tf
)
1759 /* Get next buffer until we finally have an event, or end of trace */
1761 err
= ltt_seek_next_event(tf
);
1762 if(unlikely(err
== ENOPROTOOPT
)) {
1766 /* Are we at the end of the buffer ? */
1768 if(unlikely(tf
->buffer
.index
== tf
->num_blocks
-1)){ /* end of trace ? */
1771 /* get next block */
1772 err
= map_block(tf
, tf
->buffer
.index
+ 1);
1774 g_error("Can not map block");
1778 } else break; /* We found an event ! */
1785 /* do specific operation on events */
1786 int ltt_tracefile_read_op(LttTracefile
*tf
)
1792 /* do event specific operation */
1794 /* do something if its an heartbeat event : increment the heartbeat count */
1795 //if(event->facility_id == LTT_FACILITY_CORE)
1796 // if(event->event_id == LTT_EVENT_HEARTBEAT)
1797 // tf->cur_heart_beat_number++;
1803 /* same as ltt_tracefile_read, but does not seek to the next event nor call
1804 * event specific operation. */
1805 int ltt_tracefile_read_update_event(LttTracefile
*tf
)
1811 pos
= tf
->buffer
.head
+ event
->offset
;
1813 /* Read event header */
1815 /* Align the head */
1816 pos
+= ltt_align((size_t)pos
, tf
->trace
->arch_size
, tf
->has_alignment
);
1818 if(tf
->trace
->has_heartbeat
) {
1819 event
->timestamp
= ltt_get_uint32(LTT_GET_BO(tf
),
1821 /* 32 bits -> 64 bits tsc */
1822 /* note : still works for seek and non seek cases. */
1823 if(event
->timestamp
< (0xFFFFFFFFULL
&tf
->buffer
.tsc
)) {
1824 tf
->buffer
.tsc
= ((tf
->buffer
.tsc
&0xFFFFFFFF00000000ULL
)
1826 | (guint64
)event
->timestamp
;
1827 event
->tsc
= tf
->buffer
.tsc
;
1830 tf
->buffer
.tsc
= (tf
->buffer
.tsc
&0xFFFFFFFF00000000ULL
)
1831 | (guint64
)event
->timestamp
;
1832 event
->tsc
= tf
->buffer
.tsc
;
1834 pos
+= sizeof(guint32
);
1836 event
->tsc
= ltt_get_uint64(LTT_GET_BO(tf
), pos
);
1837 tf
->buffer
.tsc
= event
->tsc
;
1838 pos
+= sizeof(guint64
);
1840 event
->event_time
= ltt_interpolate_time(tf
, event
);
1841 event
->facility_id
= *(guint8
*)pos
;
1842 pos
+= sizeof(guint8
);
1844 event
->event_id
= *(guint8
*)pos
;
1845 pos
+= sizeof(guint8
);
1847 event
->event_size
= ltt_get_uint16(LTT_GET_BO(tf
), pos
);
1848 pos
+= sizeof(guint16
);
1850 /* Align the head */
1851 pos
+= ltt_align((size_t)pos
, tf
->trace
->arch_size
, tf
->has_alignment
);
1855 /* get the data size and update the event fields with the current
1857 ltt_update_event_size(tf
);
1863 /****************************************************************************
1865 * map_block : map a block from the file
1867 * lttdes : ltt trace file
1868 * whichBlock : the block which will be read
1871 * EINVAL : lseek fail
1872 * EIO : can not read from the file
1873 ****************************************************************************/
1875 static gint
map_block(LttTracefile
* tf
, guint block_num
)
1877 int page_size
= getpagesize();
1878 struct ltt_block_start_header
*header
;
1880 g_assert(block_num
< tf
->num_blocks
);
1882 if(tf
->buffer
.head
!= NULL
) {
1883 if(munmap(tf
->buffer
.head
, PAGE_ALIGN(tf
->buf_size
))) {
1884 g_warning("unmap size : %u\n",
1885 PAGE_ALIGN(tf
->buf_size
));
1886 perror("munmap error");
1892 /* Multiple of pages aligned head */
1893 tf
->buffer
.head
= mmap(0,
1894 PAGE_ALIGN(tf
->buf_size
),
1895 PROT_READ
, MAP_PRIVATE
, tf
->fd
,
1896 PAGE_ALIGN((off_t
)tf
->buf_size
* (off_t
)block_num
));
1898 if(tf
->buffer
.head
== MAP_FAILED
) {
1899 perror("Error in allocating memory for buffer of tracefile");
1903 g_assert( ( (guint
)tf
->buffer
.head
&(8-1) ) == 0); // make sure it's aligned.
1906 tf
->buffer
.index
= block_num
;
1908 header
= (struct ltt_block_start_header
*)tf
->buffer
.head
;
1911 tf
->buffer
.begin
.timestamp
= ltt_time_add(
1912 ltt_time_from_uint64(
1913 ltt_get_uint64(LTT_GET_BO(tf
),
1914 &header
->begin
.timestamp
)
1915 - tf
->trace
->start_monotonic
),
1916 tf
->trace
->start_time
);
1918 //g_debug("block %u begin : %lu.%lu", block_num,
1919 // tf->buffer.begin.timestamp.tv_sec, tf->buffer.begin.timestamp.tv_nsec);
1920 tf
->buffer
.begin
.cycle_count
= ltt_get_uint64(LTT_GET_BO(tf
),
1921 &header
->begin
.cycle_count
);
1922 tf
->buffer
.begin
.freq
= ltt_get_uint64(LTT_GET_BO(tf
),
1923 &header
->begin
.freq
);
1924 if(tf
->buffer
.begin
.freq
== 0)
1925 tf
->buffer
.begin
.freq
= tf
->trace
->start_freq
;
1927 tf
->buffer
.begin
.timestamp
= ltt_interpolate_time_from_tsc(tf
,
1928 tf
->buffer
.begin
.cycle_count
);
1931 ltt_time_from_uint64(
1932 (double)(tf
->buffer
.begin
.cycle_count
1933 - tf
->trace
->start_tsc
) * 1000000.0
1934 / (double)tf
->trace
->start_freq
),
1935 tf
->trace
->start_time_from_tsc
);
1939 tf
->buffer
.end
.timestamp
= ltt_time_add(
1940 ltt_time_from_uint64(
1941 ltt_get_uint64(LTT_GET_BO(tf
),
1942 &header
->end
.timestamp
)
1943 - tf
->trace
->start_monotonic
),
1944 tf
->trace
->start_time
);
1946 //g_debug("block %u end : %lu.%lu", block_num,
1947 // tf->buffer.end.timestamp.tv_sec, tf->buffer.end.timestamp.tv_nsec);
1948 tf
->buffer
.end
.cycle_count
= ltt_get_uint64(LTT_GET_BO(tf
),
1949 &header
->end
.cycle_count
);
1950 tf
->buffer
.end
.freq
= ltt_get_uint64(LTT_GET_BO(tf
),
1952 if(tf
->buffer
.end
.freq
== 0)
1953 tf
->buffer
.end
.freq
= tf
->trace
->start_freq
;
1955 tf
->buffer
.lost_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1956 &header
->lost_size
);
1957 tf
->buffer
.end
.timestamp
= ltt_interpolate_time_from_tsc(tf
,
1958 tf
->buffer
.end
.cycle_count
);
1961 ltt_time_from_uint64(
1962 (double)(tf
->buffer
.end
.cycle_count
1963 - tf
->trace
->start_tsc
) * 1000000.0
1964 / (double)tf
->trace
->start_freq
),
1965 tf
->trace
->start_time_from_tsc
);
1967 tf
->buffer
.tsc
= tf
->buffer
.begin
.cycle_count
;
1968 tf
->event
.tsc
= tf
->buffer
.tsc
;
1969 tf
->buffer
.freq
= tf
->buffer
.begin
.freq
;
1972 * eventually support variable buffer size : will need a partial pre-read of
1973 * the headers to create an index when we open the trace... eventually. */
1974 g_assert(tf
->buf_size
== ltt_get_uint32(LTT_GET_BO(tf
),
1975 &header
->buf_size
));
1977 /* Now that the buffer is mapped, calculate the time interpolation for the
1980 // tf->buffer.nsecs_per_cycle = calc_nsecs_per_cycle(tf);
1981 //tf->buffer.cyc2ns_scale = calc_nsecs_per_cycle(tf);
1983 /* Make the current event point to the beginning of the buffer :
1984 * it means that the event read must get the first event. */
1985 tf
->event
.tracefile
= tf
;
1986 tf
->event
.block
= block_num
;
1987 tf
->event
.offset
= 0;
1996 /* It will update the fields offsets too */
1997 void ltt_update_event_size(LttTracefile
*tf
)
2001 /* Specific handling of core events : necessary to read the facility control
2003 LttFacility
*f
= ltt_trace_get_facility_by_num(tf
->trace
,
2004 tf
->event
.facility_id
);
2006 if(likely(tf
->event
.facility_id
== LTT_FACILITY_CORE
)) {
2007 switch((enum ltt_core_events
)tf
->event
.event_id
) {
2008 case LTT_EVENT_FACILITY_LOAD
:
2009 size
= strlen((char*)tf
->event
.data
) + 1;
2010 //g_debug("Update Event facility load of facility %s", (char*)tf->event.data);
2011 size
+= ltt_align(size
, sizeof(guint32
), tf
->has_alignment
);
2012 size
+= sizeof(struct LttFacilityLoad
);
2014 case LTT_EVENT_FACILITY_UNLOAD
:
2015 //g_debug("Update Event facility unload");
2016 size
= sizeof(struct LttFacilityUnload
);
2018 case LTT_EVENT_STATE_DUMP_FACILITY_LOAD
:
2019 size
= strlen((char*)tf
->event
.data
) + 1;
2020 size
+= ltt_align(size
, sizeof(guint32
), tf
->has_alignment
);
2021 //g_debug("Update Event facility load state dump of facility %s",
2022 // (char*)tf->event.data);
2023 size
+= sizeof(struct LttStateDumpFacilityLoad
);
2025 case LTT_EVENT_HEARTBEAT
:
2026 //g_debug("Update Event heartbeat");
2027 size
= sizeof(TimeHeartbeat
);
2030 g_warning("Error in getting event size : tracefile %s, "
2031 "unknown event id %hhu in core facility.",
2032 g_quark_to_string(tf
->name
),
2033 tf
->event
.event_id
);
2034 goto event_id_error
;
2039 g_warning("Unknown facility %hhu (0x%hhx) in tracefile %s",
2040 tf
->event
.facility_id
,
2041 tf
->event
.facility_id
,
2042 g_quark_to_string(tf
->name
));
2043 goto facility_error
;
2046 LttEventType
*event_type
=
2047 ltt_facility_eventtype_get(f
, tf
->event
.event_id
);
2050 g_warning("Unknown event id %hhu in facility %s in tracefile %s",
2052 g_quark_to_string(f
->name
),
2053 g_quark_to_string(tf
->name
));
2054 goto event_type_error
;
2057 /* Compute the dynamic offsets */
2058 compute_offsets(tf
, f
, event_type
, &size
, tf
->event
.data
);
2060 //g_debug("Event root field : f.e %hhu.%hhu size %zd",
2061 // tf->event.facility_id,
2062 // tf->event.event_id, size);
2065 tf
->event
.data_size
= size
;
2067 /* Check consistency between kernel and LTTV structure sizes */
2068 if(tf
->event
.event_size
== 0xFFFF) {
2069 /* Event size too big to fit in the event size field */
2070 tf
->event
.event_size
= tf
->event
.data_size
;
2072 g_assert(tf
->event
.data_size
== tf
->event
.event_size
);
2079 if(tf
->event
.event_size
== 0xFFFF) {
2080 g_error("Cannot jump over an unknown event bigger than 0xFFFE bytes");
2082 /* The facility is unknown : use the kernel information about this event
2083 * to jump over it. */
2084 tf
->event
.data_size
= tf
->event
.event_size
;
2088 /* Take the tf current event offset and use the event facility id and event id
2089 * to figure out where is the next event offset.
2091 * This is an internal function not aiming at being used elsewhere : it will
2092 * not jump over the current block limits. Please consider using
2093 * ltt_tracefile_read to do this.
2095 * Returns 0 on success
2096 * ERANGE if we are at the end of the buffer.
2097 * ENOPROTOOPT if an error occured when getting the current event size.
2099 static int ltt_seek_next_event(LttTracefile
*tf
)
2104 /* seek over the buffer header if we are at the buffer start */
2105 if(tf
->event
.offset
== 0) {
2106 tf
->event
.offset
+= tf
->buffer_header_size
;
2108 if(tf
->event
.offset
== tf
->buf_size
- tf
->buffer
.lost_size
) {
2115 pos
= tf
->event
.data
;
2117 if(tf
->event
.data_size
< 0) goto error
;
2119 pos
+= (size_t)tf
->event
.data_size
;
2121 tf
->event
.offset
= pos
- tf
->buffer
.head
;
2123 if(tf
->event
.offset
== tf
->buf_size
- tf
->buffer
.lost_size
) {
2127 g_assert(tf
->event
.offset
< tf
->buf_size
- tf
->buffer
.lost_size
);
2133 g_error("Error in ltt_seek_next_event for tracefile %s",
2134 g_quark_to_string(tf
->name
));
2139 /*****************************************************************************
2141 * calc_nsecs_per_cycle : calculate nsecs per cycle for current block
2143 * 1.0 / (freq(khz) *1000) * 1000000000
2146 ****************************************************************************/
2147 /* from timer_tsc.c */
2148 #define CYC2NS_SCALE_FACTOR 10
2149 static guint32
calc_nsecs_per_cycle(LttTracefile
* tf
)
2151 //return 1e6 / (double)tf->buffer.freq;
2152 guint32 cpu_mhz
= tf
->buffer
.freq
/ 1000;
2153 guint32 cyc2ns_scale
= (1000 << CYC2NS_SCALE_FACTOR
)/cpu_mhz
;
2155 return cyc2ns_scale
;
2156 // return 1e6 / (double)tf->buffer.freq;
2159 static guint64
cycles_2_ns(LttTracefile
*tf
, guint64 cycles
)
2161 return (cycles
* tf
->buffer
.cyc2ns_scale
) >> CYC2NS_SCALE_FACTOR
;
2166 void setFieldsOffset(LttTracefile
*tf
, LttEventType
*evT
,void *evD
)
2168 LttField
* rootFld
= evT
->root_field
;
2169 // rootFld->base_address = evD;
2172 rootFld
->field_size
= getFieldtypeSize(tf
, evT
->facility
,
2173 evT
, 0,0,rootFld
, evD
);
2177 /*****************************************************************************
2179 * set_fields_offsets : set the precomputable offset of the fields
2181 * tracefile : opened trace file
2182 * event_type : the event type
2183 ****************************************************************************/
2185 void set_fields_offsets(LttTracefile
*tf
, LttEventType
*event_type
)
2187 LttField
*field
= event_type
->root_field
;
2188 enum field_status fixed_root
= FIELD_FIXED
, fixed_parent
= FIELD_FIXED
;
2191 preset_field_type_size(tf
, event_type
, 0, 0,
2192 &fixed_root
, &fixed_parent
,
2199 /*****************************************************************************
2201 * get_alignment : Get the alignment needed for a field.
2205 * returns : The size on which it must be aligned.
2207 ****************************************************************************/
2208 off_t
get_alignment(LttField
*field
)
2210 LttType
*type
= &field
->field_type
;
2212 switch(type
->type_class
) {
2214 case LTT_UINT_FIXED
:
2229 /* Align offset on type size */
2230 return field
->field_size
;
2236 g_assert(type
->fields
->len
== 1);
2238 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
2239 return get_alignment(child
);
2243 g_assert(type
->fields
->len
== 2);
2246 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
2248 localign
= max(localign
, get_alignment(child
));
2250 child
= &g_array_index(type
->fields
, LttField
, 1);
2251 localign
= max(localign
, get_alignment(child
));
2262 for(i
=0; i
<type
->fields
->len
; i
++) {
2263 LttField
*child
= &g_array_index(type
->fields
, LttField
, i
);
2264 localign
= max(localign
, get_alignment(child
));
2271 g_error("get_alignment : unknown type");
2276 /*****************************************************************************
2278 * field_compute_static_size : Determine the size of fields known by their
2279 * sole definition. Unions, arrays and struct sizes might be known, but
2280 * the parser does not give that information.
2285 ****************************************************************************/
2287 void field_compute_static_size(LttFacility
*fac
, LttField
*field
)
2289 LttType
*type
= &field
->field_type
;
2291 switch(type
->type_class
) {
2293 case LTT_UINT_FIXED
:
2312 /* note this : array type size is the number of elements in the array,
2313 * while array field size of the length of the array in bytes */
2314 g_assert(type
->fields
->len
== 1);
2316 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
2317 field_compute_static_size(fac
, child
);
2319 if(child
->field_size
!= 0) {
2320 field
->field_size
= type
->size
* child
->field_size
;
2321 field
->dynamic_offsets
= g_array_sized_new(FALSE
, TRUE
,
2322 sizeof(off_t
), type
->size
);
2324 field
->field_size
= 0;
2329 g_assert(type
->fields
->len
== 2);
2331 off_t local_offset
= 0;
2332 LttField
*child
= &g_array_index(type
->fields
, LttField
, 1);
2333 field_compute_static_size(fac
, child
);
2334 field
->field_size
= 0;
2336 if(child
->field_size
!= 0) {
2337 field
->dynamic_offsets
= g_array_sized_new(FALSE
, TRUE
,
2338 sizeof(off_t
), SEQUENCE_AVG_ELEMENTS
);
2346 for(i
=0;i
<type
->fields
->len
;i
++) {
2347 LttField
*child
= &g_array_index(type
->fields
, LttField
, i
);
2348 field_compute_static_size(fac
, child
);
2349 if(child
->field_size
!= 0) {
2350 type
->size
+= ltt_align(type
->size
, get_alignment(child
),
2352 type
->size
+= child
->field_size
;
2354 /* As soon as we find a child with variable size, we have
2355 * a variable size */
2360 field
->field_size
= type
->size
;
2364 g_error("field_static_size : unknown type");
2371 /*****************************************************************************
2373 * precompute_fields_offsets : set the precomputable offset of the fields
2377 * offset : pointer to the current offset, must be incremented
2379 * return : 1 : found a variable length field, stop the processing.
2381 ****************************************************************************/
2384 gint
precompute_fields_offsets(LttFacility
*fac
, LttField
*field
, off_t
*offset
)
2386 LttType
*type
= &field
->field_type
;
2388 switch(type
->type_class
) {
2390 case LTT_UINT_FIXED
:
2405 g_assert(field
->field_size
!= 0);
2406 /* Align offset on type size */
2407 *offset
+= ltt_align(*offset
, get_alignment(field
),
2409 /* remember offset */
2410 field
->offset_root
= *offset
;
2411 field
->fixed_root
= FIELD_FIXED
;
2412 /* Increment offset */
2413 *offset
+= field
->field_size
;
2417 field
->offset_root
= *offset
;
2418 field
->fixed_root
= FIELD_FIXED
;
2422 g_assert(type
->fields
->len
== 1);
2424 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
2426 *offset
+= ltt_align(*offset
, get_alignment(field
),
2429 /* remember offset */
2430 field
->offset_root
= *offset
;
2431 field
->array_offset
= *offset
;
2432 field
->fixed_root
= FIELD_FIXED
;
2434 /* Let the child be variable */
2435 //precompute_fields_offsets(tf, child, offset);
2437 if(field
->field_size
!= 0) {
2438 /* Increment offset */
2439 /* field_size is the array size in bytes */
2440 *offset
+= field
->field_size
;
2448 g_assert(type
->fields
->len
== 2);
2453 *offset
+= ltt_align(*offset
, get_alignment(field
),
2456 /* remember offset */
2457 field
->offset_root
= *offset
;
2458 field
->fixed_root
= FIELD_FIXED
;
2460 child
= &g_array_index(type
->fields
, LttField
, 0);
2461 ret
= precompute_fields_offsets(fac
, child
, offset
);
2462 g_assert(ret
== 0); /* Seq len cannot have variable len */
2464 child
= &g_array_index(type
->fields
, LttField
, 1);
2465 *offset
+= ltt_align(*offset
, get_alignment(child
),
2467 field
->array_offset
= *offset
;
2468 /* Let the child be variable. */
2469 //ret = precompute_fields_offsets(fac, child, offset);
2471 /* Cannot precompute fields offsets of sequence members, and has
2472 * variable length. */
2482 *offset
+= ltt_align(*offset
, get_alignment(field
),
2484 /* remember offset */
2485 field
->offset_root
= *offset
;
2486 field
->fixed_root
= FIELD_FIXED
;
2488 for(i
=0; i
< type
->fields
->len
; i
++) {
2489 child
= &g_array_index(type
->fields
, LttField
, i
);
2490 ret
= precompute_fields_offsets(fac
, child
, offset
);
2503 *offset
+= ltt_align(*offset
, get_alignment(field
),
2505 /* remember offset */
2506 field
->offset_root
= *offset
;
2507 field
->fixed_root
= FIELD_FIXED
;
2509 for(i
=0; i
< type
->fields
->len
; i
++) {
2510 *offset
= field
->offset_root
;
2511 child
= &g_array_index(type
->fields
, LttField
, i
);
2512 ret
= precompute_fields_offsets(fac
, child
, offset
);
2516 *offset
= field
->offset_root
+ field
->field_size
;
2523 g_error("precompute_fields_offsets : unknown type");
2530 /*****************************************************************************
2532 * precompute_offsets : set the precomputable offset of an event type
2535 * event : event type
2537 ****************************************************************************/
2538 void precompute_offsets(LttFacility
*fac
, LttEventType
*event
)
2544 /* First, compute the size of fixed size fields. Will determine size for
2545 * arrays, struct and unions, which is not done by the parser */
2546 for(i
=0; i
<event
->fields
->len
; i
++) {
2547 LttField
*field
= &g_array_index(event
->fields
, LttField
, i
);
2548 field_compute_static_size(fac
, field
);
2551 /* Precompute all known offsets */
2552 for(i
=0; i
<event
->fields
->len
; i
++) {
2553 LttField
*field
= &g_array_index(event
->fields
, LttField
, i
);
2554 ret
= precompute_fields_offsets(fac
, field
, &offset
);
2562 /*****************************************************************************
2564 * preset_field_type_size : set the fixed sizes of the field type
2567 * event_type : event type
2568 * offset_root : offset from the root
2569 * offset_parent : offset from the parent
2570 * fixed_root : Do we know a fixed offset to the root ?
2571 * fixed_parent : Do we know a fixed offset to the parent ?
2573 ****************************************************************************/
2577 // preset the fixed size offsets. Calculate them just like genevent-new : an
2578 // increment of a *to value that represents the offset from the start of the
2580 // The preset information is : offsets up to (and including) the first element
2581 // of variable size. All subsequent fields must be flagged "VARIABLE OFFSET".
2583 void preset_field_type_size(LttTracefile
*tf
, LttEventType
*event_type
,
2584 off_t offset_root
, off_t offset_parent
,
2585 enum field_status
*fixed_root
, enum field_status
*fixed_parent
,
2588 enum field_status local_fixed_root
, local_fixed_parent
;
2592 g_assert(field
->fixed_root
== FIELD_UNKNOWN
);
2593 g_assert(field
->fixed_parent
== FIELD_UNKNOWN
);
2594 g_assert(field
->fixed_size
== FIELD_UNKNOWN
);
2596 type
= field
->field_type
;
2598 field
->fixed_root
= *fixed_root
;
2599 if(field
->fixed_root
== FIELD_FIXED
)
2600 field
->offset_root
= offset_root
;
2602 field
->offset_root
= 0;
2604 field
->fixed_parent
= *fixed_parent
;
2605 if(field
->fixed_parent
== FIELD_FIXED
)
2606 field
->offset_parent
= offset_parent
;
2608 field
->offset_parent
= 0;
2610 size_t current_root_offset
;
2611 size_t current_offset
;
2612 enum field_status current_child_status
, final_child_status
;
2615 switch(type
->type_class
) {
2620 field
->field_size
= ltt_type_size(tf
->trace
, type
);
2621 field
->fixed_size
= FIELD_FIXED
;
2624 field
->field_size
= (off_t
)event_type
->facility
->pointer_size
;
2625 field
->fixed_size
= FIELD_FIXED
;
2629 field
->field_size
= (off_t
)event_type
->facility
->long_size
;
2630 field
->fixed_size
= FIELD_FIXED
;
2635 field
->field_size
= (off_t
)event_type
->facility
->size_t_size
;
2636 field
->fixed_size
= FIELD_FIXED
;
2639 local_fixed_root
= FIELD_VARIABLE
;
2640 local_fixed_parent
= FIELD_VARIABLE
;
2641 preset_field_type_size(tf
, event_type
,
2643 &local_fixed_root
, &local_fixed_parent
,
2645 field
->fixed_size
= FIELD_VARIABLE
;
2646 field
->field_size
= 0;
2647 *fixed_root
= FIELD_VARIABLE
;
2648 *fixed_parent
= FIELD_VARIABLE
;
2651 field
->fixed_size
= FIELD_VARIABLE
;
2652 field
->field_size
= 0;
2653 *fixed_root
= FIELD_VARIABLE
;
2654 *fixed_parent
= FIELD_VARIABLE
;
2657 local_fixed_root
= FIELD_VARIABLE
;
2658 local_fixed_parent
= FIELD_VARIABLE
;
2659 preset_field_type_size(tf
, event_type
,
2661 &local_fixed_root
, &local_fixed_parent
,
2663 field
->fixed_size
= field
->child
[0]->fixed_size
;
2664 if(field
->fixed_size
== FIELD_FIXED
) {
2665 field
->field_size
= type
->element_number
* field
->child
[0]->field_size
;
2667 field
->field_size
= 0;
2668 *fixed_root
= FIELD_VARIABLE
;
2669 *fixed_parent
= FIELD_VARIABLE
;
2673 current_root_offset
= field
->offset_root
;
2675 current_child_status
= FIELD_FIXED
;
2676 for(i
=0;i
<type
->element_number
;i
++) {
2677 preset_field_type_size(tf
, event_type
,
2678 current_root_offset
, current_offset
,
2679 fixed_root
, ¤t_child_status
,
2681 if(current_child_status
== FIELD_FIXED
) {
2682 current_root_offset
+= field
->child
[i
]->field_size
;
2683 current_offset
+= field
->child
[i
]->field_size
;
2685 current_root_offset
= 0;
2689 if(current_child_status
!= FIELD_FIXED
) {
2690 *fixed_parent
= current_child_status
;
2691 field
->field_size
= 0;
2692 field
->fixed_size
= current_child_status
;
2694 field
->field_size
= current_offset
;
2695 field
->fixed_size
= FIELD_FIXED
;
2699 current_root_offset
= field
->offset_root
;
2702 final_child_status
= FIELD_FIXED
;
2703 for(i
=0;i
<type
->element_number
;i
++) {
2704 enum field_status current_root_child_status
= FIELD_FIXED
;
2705 enum field_status current_child_status
= FIELD_FIXED
;
2706 preset_field_type_size(tf
, event_type
,
2707 current_root_offset
, current_offset
,
2708 ¤t_root_child_status
, ¤t_child_status
,
2710 if(current_child_status
!= FIELD_FIXED
)
2711 final_child_status
= current_child_status
;
2713 max_size
= max(max_size
, field
->child
[i
]->field_size
);
2715 if(final_child_status
!= FIELD_FIXED
) {
2716 g_error("LTTV does not support variable size fields in unions.");
2717 /* This will stop the application. */
2718 *fixed_root
= final_child_status
;
2719 *fixed_parent
= final_child_status
;
2720 field
->field_size
= 0;
2721 field
->fixed_size
= current_child_status
;
2723 field
->field_size
= max_size
;
2724 field
->fixed_size
= FIELD_FIXED
;
2732 /*****************************************************************************
2734 * check_fields_compatibility : Check for compatibility between two fields :
2735 * do they use the same inner structure ?
2737 * event_type1 : event type
2738 * event_type2 : event type
2741 *Returns : 0 if identical
2743 ****************************************************************************/
2744 // this function checks for equality of field types. Therefore, it does not use
2745 // per se offsets. For instance, an aligned version of a structure is
2746 // compatible with an unaligned version of the same structure.
2747 gint
check_fields_compatibility(LttEventType
*event_type1
,
2748 LttEventType
*event_type2
,
2749 LttField
*field1
, LttField
*field2
)
2751 guint different
= 0;
2755 if(field1
== NULL
) {
2756 if(field2
== NULL
) goto end
;
2761 } else if(field2
== NULL
) {
2766 type1
= &field1
->field_type
;
2767 type2
= &field2
->field_type
;
2769 if(type1
->type_class
!= type2
->type_class
) {
2773 if(type1
->network
!= type2
->network
) {
2778 switch(type1
->type_class
) {
2780 case LTT_UINT_FIXED
:
2795 if(field1
->field_size
!= field2
->field_size
)
2802 LttField
*child1
= &g_array_index(type1
->fields
, LttField
, 0);
2803 LttField
*child2
= &g_array_index(type2
->fields
, LttField
, 0);
2805 if(type1
->size
!= type2
->size
)
2807 if(check_fields_compatibility(event_type1
, event_type2
, child1
, child2
))
2813 LttField
*child1
= &g_array_index(type1
->fields
, LttField
, 1);
2814 LttField
*child2
= &g_array_index(type2
->fields
, LttField
, 1);
2816 if(check_fields_compatibility(event_type1
, event_type2
, child1
, child2
))
2826 if(type1
->fields
->len
!= type2
->fields
->len
) {
2831 for(i
=0; i
< type1
->fields
->len
; i
++) {
2834 child1
= &g_array_index(type1
->fields
, LttField
, i
);
2835 child2
= &g_array_index(type2
->fields
, LttField
, i
);
2836 different
= check_fields_compatibility(event_type1
,
2837 event_type2
, child1
, child2
);
2839 if(different
) break;
2845 g_error("precompute_fields_offsets : unknown type");
2854 gint
check_fields_compatibility(LttEventType
*event_type1
,
2855 LttEventType
*event_type2
,
2856 LttField
*field1
, LttField
*field2
)
2858 guint different
= 0;
2863 if(field1
== NULL
) {
2864 if(field2
== NULL
) goto end
;
2869 } else if(field2
== NULL
) {
2874 g_assert(field1
->fixed_root
!= FIELD_UNKNOWN
);
2875 g_assert(field2
->fixed_root
!= FIELD_UNKNOWN
);
2876 g_assert(field1
->fixed_parent
!= FIELD_UNKNOWN
);
2877 g_assert(field2
->fixed_parent
!= FIELD_UNKNOWN
);
2878 g_assert(field1
->fixed_size
!= FIELD_UNKNOWN
);
2879 g_assert(field2
->fixed_size
!= FIELD_UNKNOWN
);
2881 type1
= field1
->field_type
;
2882 type2
= field2
->field_type
;
2884 if(type1
->type_class
!= type2
->type_class
) {
2888 if(type1
->element_name
!= type2
->element_name
) {
2893 switch(type1
->type_class
) {
2903 if(field1
->field_size
!= field2
->field_size
) {
2909 if(type1
->element_number
!= type2
->element_number
) {
2913 for(i
=0;i
<type1
->element_number
;i
++) {
2914 if(type1
->enum_strings
[i
] != type2
->enum_strings
[i
]) {
2921 /* Two elements : size and child */
2922 g_assert(type1
->element_number
!= type2
->element_number
);
2923 for(i
=0;i
<type1
->element_number
;i
++) {
2924 if(check_fields_compatibility(event_type1
, event_type2
,
2925 field1
->child
[0], field2
->child
[0])) {
2934 if(field1
->field_size
!= field2
->field_size
) {
2938 /* Two elements : size and child */
2939 g_assert(type1
->element_number
!= type2
->element_number
);
2940 for(i
=0;i
<type1
->element_number
;i
++) {
2941 if(check_fields_compatibility(event_type1
, event_type2
,
2942 field1
->child
[0], field2
->child
[0])) {
2950 if(type1
->element_number
!= type2
->element_number
) {
2954 for(i
=0;i
<type1
->element_number
;i
++) {
2955 if(check_fields_compatibility(event_type1
, event_type2
,
2956 field1
->child
[0], field2
->child
[0])) {
2969 /*****************************************************************************
2971 * ltt_get_int : get an integer number
2973 * reverse_byte_order: must we reverse the byte order ?
2974 * size : the size of the integer
2975 * ptr : the data pointer
2977 * gint64 : a 64 bits integer
2978 ****************************************************************************/
2980 gint64
ltt_get_int(gboolean reverse_byte_order
, gint size
, void *data
)
2985 case 1: val
= *((gint8
*)data
); break;
2986 case 2: val
= ltt_get_int16(reverse_byte_order
, data
); break;
2987 case 4: val
= ltt_get_int32(reverse_byte_order
, data
); break;
2988 case 8: val
= ltt_get_int64(reverse_byte_order
, data
); break;
2989 default: val
= ltt_get_int64(reverse_byte_order
, data
);
2990 g_critical("get_int : integer size %d unknown", size
);
2997 /*****************************************************************************
2999 * ltt_get_uint : get an unsigned integer number
3001 * reverse_byte_order: must we reverse the byte order ?
3002 * size : the size of the integer
3003 * ptr : the data pointer
3005 * guint64 : a 64 bits unsigned integer
3006 ****************************************************************************/
3008 guint64
ltt_get_uint(gboolean reverse_byte_order
, gint size
, void *data
)
3013 case 1: val
= *((gint8
*)data
); break;
3014 case 2: val
= ltt_get_uint16(reverse_byte_order
, data
); break;
3015 case 4: val
= ltt_get_uint32(reverse_byte_order
, data
); break;
3016 case 8: val
= ltt_get_uint64(reverse_byte_order
, data
); break;
3017 default: val
= ltt_get_uint64(reverse_byte_order
, data
);
3018 g_critical("get_uint : unsigned integer size %d unknown",
3027 /* get the node name of the system */
3029 char * ltt_trace_system_description_node_name (LttSystemDescription
* s
)
3031 return s
->node_name
;
3035 /* get the domain name of the system */
3037 char * ltt_trace_system_description_domain_name (LttSystemDescription
* s
)
3039 return s
->domain_name
;
3043 /* get the description of the system */
3045 char * ltt_trace_system_description_description (LttSystemDescription
* s
)
3047 return s
->description
;
3051 /* get the NTP corrected start time of the trace */
3052 LttTime
ltt_trace_start_time(LttTrace
*t
)
3054 return t
->start_time
;
3057 /* get the monotonic start time of the trace */
3058 LttTime
ltt_trace_start_time_monotonic(LttTrace
*t
)
3060 return t
->start_time_from_tsc
;
3063 LttTracefile
*ltt_tracefile_new()
3065 return g_new(LttTracefile
, 1);
3068 void ltt_tracefile_destroy(LttTracefile
*tf
)
3073 void ltt_tracefile_copy(LttTracefile
*dest
, const LttTracefile
*src
)
3078 /* Before library loading... */
3080 static void __attribute__((constructor
)) init(void)
3082 LTT_FACILITY_NAME_HEARTBEAT
= g_quark_from_string("heartbeat");
3083 LTT_EVENT_NAME_HEARTBEAT
= g_quark_from_string("heartbeat");
3085 LTT_TRACEFILE_NAME_FACILITIES
= g_quark_from_string("/control/facilities");