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
:
1000 case LTT_ARCH_TYPE_POWERPC
:
1004 g_error("Trace from unsupported architecture.");
1006 textlen
+=strlen(text
);
1007 if(textlen
>= PATH_MAX
) goto name_error
;
1008 strcat(desc_file_name
, text
);
1012 textlen
+=strlen(text
);
1013 if(textlen
>= PATH_MAX
) goto name_error
;
1014 strcat(desc_file_name
, text
);
1016 err
= ltt_facility_open(f
, t
, desc_file_name
);
1017 if(err
) goto facility_error
;
1026 static void ltt_fac_ids_destroy(gpointer data
)
1028 GArray
*fac_ids
= (GArray
*)data
;
1030 g_array_free(fac_ids
, TRUE
);
1034 /* Presumes the tracefile is already seeked at the beginning. It makes sense,
1035 * because it must be done just after the opening */
1036 static int ltt_process_facility_tracefile(LttTracefile
*tf
)
1045 err
= ltt_tracefile_read_seek(tf
);
1046 if(err
== EPERM
) goto seek_error
;
1047 else if(err
== ERANGE
) break; /* End of tracefile */
1049 err
= ltt_tracefile_read_update_event(tf
);
1050 if(err
) goto update_error
;
1052 /* We are on a facility load/or facility unload/ or heartbeat event */
1054 * * facility 0 is hardcoded : this is the core facility. It will be shown
1055 * in the facility array though, and is shown as "loaded builtin" in the
1057 * It contains event :
1059 * 1 : facility unload
1060 * 2 : state dump facility load
1063 if(tf
->event
.facility_id
!= LTT_FACILITY_CORE
) {
1064 /* Should only contain core facility */
1065 g_warning("Error in processing facility file %s, "
1066 "should not contain facility id %u.", g_quark_to_string(tf
->name
),
1067 tf
->event
.facility_id
);
1072 struct LttFacilityLoad
*fac_load_data
;
1073 struct LttStateDumpFacilityLoad
*fac_state_dump_load_data
;
1078 switch((enum ltt_core_events
)tf
->event
.event_id
) {
1079 case LTT_EVENT_FACILITY_LOAD
:
1080 fac_name
= (char*)(tf
->event
.data
);
1081 g_debug("Doing LTT_EVENT_FACILITY_LOAD of facility %s",
1083 pos
= (tf
->event
.data
+ strlen(fac_name
) + 1);
1084 pos
+= ltt_align((size_t)pos
, sizeof(guint32
), tf
->has_alignment
);
1085 fac_load_data
= (struct LttFacilityLoad
*)pos
;
1087 fac
= &g_array_index (tf
->trace
->facilities_by_num
, LttFacility
,
1088 ltt_get_uint32(LTT_GET_BO(tf
), &fac_load_data
->id
));
1089 /* facility may already exist if trace is paused/unpaused */
1090 if(fac
->exists
) continue;
1091 fac
->name
= g_quark_from_string(fac_name
);
1092 fac
->checksum
= ltt_get_uint32(LTT_GET_BO(tf
),
1093 &fac_load_data
->checksum
);
1094 fac
->id
= ltt_get_uint32(LTT_GET_BO(tf
), &fac_load_data
->id
);
1095 fac
->pointer_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1096 &fac_load_data
->pointer_size
);
1097 fac
->int_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1098 &fac_load_data
->int_size
);
1099 fac
->long_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1100 &fac_load_data
->long_size
);
1101 fac
->size_t_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1102 &fac_load_data
->size_t_size
);
1103 fac
->alignment
= ltt_get_uint32(LTT_GET_BO(tf
),
1104 &fac_load_data
->has_alignment
);
1106 if(ltt_get_facility_description(fac
, tf
->trace
, tf
))
1107 continue; /* error opening description */
1109 fac
->trace
= tf
->trace
;
1111 /* Preset the field offsets */
1112 for(i
=0; i
<fac
->events
->len
; i
++){
1113 et
= &g_array_index(fac
->events
, LttEventType
, i
);
1114 precompute_offsets(fac
, et
);
1119 fac_ids
= g_datalist_id_get_data(&tf
->trace
->facilities_by_name
,
1121 if(fac_ids
== NULL
) {
1122 fac_ids
= g_array_sized_new (FALSE
, TRUE
, sizeof(guint
), 1);
1123 g_datalist_id_set_data_full(&tf
->trace
->facilities_by_name
,
1125 fac_ids
, ltt_fac_ids_destroy
);
1127 g_array_append_val(fac_ids
, fac
->id
);
1130 case LTT_EVENT_FACILITY_UNLOAD
:
1131 g_debug("Doing LTT_EVENT_FACILITY_UNLOAD");
1132 /* We don't care about unload : facilities ID are valid for the whole
1133 * trace. They simply won't be used after the unload. */
1135 case LTT_EVENT_STATE_DUMP_FACILITY_LOAD
:
1136 fac_name
= (char*)(tf
->event
.data
);
1137 g_debug("Doing LTT_EVENT_STATE_DUMP_FACILITY_LOAD of facility %s",
1139 pos
= (tf
->event
.data
+ strlen(fac_name
) + 1);
1140 pos
+= ltt_align((size_t)pos
, sizeof(guint32
), tf
->has_alignment
);
1141 fac_state_dump_load_data
= (struct LttStateDumpFacilityLoad
*)pos
;
1143 fac
= &g_array_index (tf
->trace
->facilities_by_num
, LttFacility
,
1144 ltt_get_uint32(LTT_GET_BO(tf
), &fac_state_dump_load_data
->id
));
1145 /* facility may already exist if trace is paused/unpaused */
1146 if(fac
->exists
) continue;
1147 fac
->name
= g_quark_from_string(fac_name
);
1148 fac
->checksum
= ltt_get_uint32(LTT_GET_BO(tf
),
1149 &fac_state_dump_load_data
->checksum
);
1150 fac
->id
= ltt_get_uint32(LTT_GET_BO(tf
),
1151 &fac_state_dump_load_data
->id
);
1152 fac
->pointer_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1153 &fac_state_dump_load_data
->pointer_size
);
1154 fac
->int_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1155 &fac_state_dump_load_data
->int_size
);
1156 fac
->long_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1157 &fac_state_dump_load_data
->long_size
);
1158 fac
->size_t_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1159 &fac_state_dump_load_data
->size_t_size
);
1160 fac
->alignment
= ltt_get_uint32(LTT_GET_BO(tf
),
1161 &fac_state_dump_load_data
->has_alignment
);
1162 if(ltt_get_facility_description(fac
, tf
->trace
, tf
))
1163 continue; /* error opening description */
1165 fac
->trace
= tf
->trace
;
1167 /* Preset the field offsets */
1168 for(i
=0; i
<fac
->events
->len
; i
++){
1169 et
= &g_array_index(fac
->events
, LttEventType
, i
);
1170 precompute_offsets(fac
, et
);
1175 fac_ids
= g_datalist_id_get_data(&tf
->trace
->facilities_by_name
,
1177 if(fac_ids
== NULL
) {
1178 fac_ids
= g_array_sized_new (FALSE
, TRUE
, sizeof(guint
), 1);
1179 g_datalist_id_set_data_full(&tf
->trace
->facilities_by_name
,
1181 fac_ids
, ltt_fac_ids_destroy
);
1183 g_array_append_val(fac_ids
, fac
->id
);
1186 case LTT_EVENT_HEARTBEAT
:
1189 g_warning("Error in processing facility file %s, "
1190 "unknown event id %hhu in core facility.",
1191 g_quark_to_string(tf
->name
),
1192 tf
->event
.event_id
);
1194 goto event_id_error
;
1200 /* Error handling */
1205 g_warning("An error occured in facility tracefile parsing");
1210 LttTrace
*ltt_trace_open(const gchar
*pathname
)
1212 gchar abs_path
[PATH_MAX
];
1217 struct ltt_block_start_header
*header
;
1219 struct dirent
*entry
;
1220 guint control_found
= 0;
1221 guint eventdefs_found
= 0;
1222 struct stat stat_buf
;
1223 gchar path
[PATH_MAX
];
1225 t
= g_new(LttTrace
, 1);
1226 if(!t
) goto alloc_error
;
1228 get_absolute_pathname(pathname
, abs_path
);
1229 t
->pathname
= g_quark_from_string(abs_path
);
1231 g_datalist_init(&t
->tracefiles
);
1233 /* Test to see if it looks like a trace */
1234 dir
= opendir(abs_path
);
1239 while((entry
= readdir(dir
)) != NULL
) {
1240 strcpy(path
, abs_path
);
1242 strcat(path
, entry
->d_name
);
1243 ret
= stat(path
, &stat_buf
);
1248 if(S_ISDIR(stat_buf
.st_mode
)) {
1249 if(strcmp(entry
->d_name
, "control") == 0) {
1252 if(strcmp(entry
->d_name
, "eventdefs") == 0) {
1253 eventdefs_found
= 1;
1259 if(!control_found
|| !eventdefs_found
) goto find_error
;
1261 /* Open all the tracefiles */
1262 if(open_tracefiles(t
, abs_path
, "")) {
1263 g_warning("Error opening tracefile %s", abs_path
);
1267 /* Prepare the facilities containers : array and mapping */
1268 /* Array is zeroed : the "exists" field is set to false by default */
1269 t
->facilities_by_num
= g_array_sized_new (FALSE
,
1270 TRUE
, sizeof(LttFacility
),
1272 t
->facilities_by_num
= g_array_set_size(t
->facilities_by_num
, NUM_FACILITIES
);
1274 g_datalist_init(&t
->facilities_by_name
);
1276 /* Parse each trace control/facilitiesN files : get runtime fac. info */
1277 group
= g_datalist_id_get_data(&t
->tracefiles
, LTT_TRACEFILE_NAME_FACILITIES
);
1279 g_error("Trace %s has no facility tracefile", abs_path
);
1281 goto facilities_error
;
1284 /* Get the trace information for the control/facility 0 tracefile */
1285 g_assert(group
->len
> 0);
1286 tf
= &g_array_index (group
, LttTracefile
, 0);
1287 header
= (struct ltt_block_start_header
*)tf
->buffer
.head
;
1288 g_assert(parse_trace_header(header
->trace
,
1291 t
->num_cpu
= group
->len
;
1293 for(i
=0; i
<group
->len
; i
++) {
1294 tf
= &g_array_index (group
, LttTracefile
, i
);
1295 if(ltt_process_facility_tracefile(tf
))
1296 goto facilities_error
;
1301 /* Error handling */
1303 g_datalist_clear(&t
->facilities_by_name
);
1304 g_array_free(t
->facilities_by_num
, TRUE
);
1306 g_datalist_clear(&t
->tracefiles
);
1314 GQuark
ltt_trace_name(const LttTrace
*t
)
1320 /******************************************************************************
1321 * When we copy a trace, we want all the opening actions to happen again :
1322 * the trace will be reopened and totally independant from the original.
1323 * That's why we call ltt_trace_open.
1324 *****************************************************************************/
1325 LttTrace
*ltt_trace_copy(LttTrace
*self
)
1327 return ltt_trace_open(g_quark_to_string(self
->pathname
));
1330 void ltt_trace_close(LttTrace
*t
)
1335 for(i
=0; i
<t
->facilities_by_num
->len
; i
++) {
1336 fac
= &g_array_index (t
->facilities_by_num
, LttFacility
, i
);
1338 ltt_facility_close(fac
);
1341 g_datalist_clear(&t
->facilities_by_name
);
1342 g_array_free(t
->facilities_by_num
, TRUE
);
1343 g_datalist_clear(&t
->tracefiles
);
1348 /*****************************************************************************
1349 *Get the system description of the trace
1350 ****************************************************************************/
1352 LttFacility
*ltt_trace_facility_by_id(LttTrace
*t
, guint8 id
)
1354 g_assert(id
< t
->facilities_by_num
->len
);
1355 return &g_array_index(t
->facilities_by_num
, LttFacility
, id
);
1358 /* ltt_trace_facility_get_by_name
1360 * Returns the GArray of facility indexes. All the fac_ids that matches the
1361 * requested facility name.
1363 * If name is not found, returns NULL.
1365 GArray
*ltt_trace_facility_get_by_name(LttTrace
*t
, GQuark name
)
1367 return g_datalist_id_get_data(&t
->facilities_by_name
, name
);
1370 /*****************************************************************************
1371 * Functions to discover all the event types in the trace
1372 ****************************************************************************/
1375 unsigned ltt_trace_eventtype_number(LttTrace
*t
)
1379 unsigned int num
= t
->facility_number
;
1383 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, i
);
1384 count
+= f
->event_number
;
1391 //use an iteration on all the trace facilities, and inside iteration on all the
1392 //event types in each facilities instead.
1393 LttEventType
*ltt_trace_eventtype_get(LttTrace
*t
, unsigned evId
)
1395 LttEventType
*event_type
;
1398 f
= ltt_trace_facility_by_id(t
,evId
);
1400 if(unlikely(!f
)) event_type
= NULL
;
1401 else event_type
= f
->events
[evId
- f
->base_id
];
1408 /*****************************************************************************
1409 * ltt_trace_find_tracefile
1411 * Find a tracefile by name and index in the group.
1413 * Returns a pointer to the tracefiles, else NULL.
1414 ****************************************************************************/
1416 LttTracefile
*ltt_trace_find_tracefile(LttTrace
*t
, const gchar
*name
)
1421 /*****************************************************************************
1422 * Get the start time and end time of the trace
1423 ****************************************************************************/
1425 static void ltt_tracefile_time_span_get(LttTracefile
*tf
,
1426 LttTime
*start
, LttTime
*end
)
1430 err
= map_block(tf
, 0);
1432 g_error("Can not map block");
1433 *start
= ltt_time_infinite
;
1435 *start
= tf
->buffer
.begin
.timestamp
;
1437 err
= map_block(tf
, tf
->num_blocks
- 1); /* Last block */
1439 g_error("Can not map block");
1440 *end
= ltt_time_zero
;
1442 *end
= tf
->buffer
.end
.timestamp
;
1445 struct tracefile_time_span_get_args
{
1451 static void group_time_span_get(GQuark name
, gpointer data
, gpointer user_data
)
1453 struct tracefile_time_span_get_args
*args
=
1454 (struct tracefile_time_span_get_args
*)user_data
;
1456 GArray
*group
= (GArray
*)data
;
1462 for(i
=0; i
<group
->len
; i
++) {
1463 tf
= &g_array_index (group
, LttTracefile
, i
);
1464 if(tf
->cpu_online
) {
1465 ltt_tracefile_time_span_get(tf
, &tmp_start
, &tmp_end
);
1466 if(ltt_time_compare(*args
->start
, tmp_start
)>0) *args
->start
= tmp_start
;
1467 if(ltt_time_compare(*args
->end
, tmp_end
)<0) *args
->end
= tmp_end
;
1472 void ltt_trace_time_span_get(LttTrace
*t
, LttTime
*start
, LttTime
*end
)
1474 LttTime min_start
= ltt_time_infinite
;
1475 LttTime max_end
= ltt_time_zero
;
1476 struct tracefile_time_span_get_args args
= { t
, &min_start
, &max_end
};
1478 g_datalist_foreach(&t
->tracefiles
, &group_time_span_get
, &args
);
1480 if(start
!= NULL
) *start
= min_start
;
1481 if(end
!= NULL
) *end
= max_end
;
1486 /*****************************************************************************
1487 *Get the name of a tracefile
1488 ****************************************************************************/
1490 GQuark
ltt_tracefile_name(const LttTracefile
*tf
)
1495 GQuark
ltt_tracefile_long_name(const LttTracefile
*tf
)
1497 return tf
->long_name
;
1502 guint
ltt_tracefile_cpu(LttTracefile
*tf
)
1507 guint
ltt_tracefile_tid(LttTracefile
*tf
)
1512 guint
ltt_tracefile_pgid(LttTracefile
*tf
)
1517 guint64
ltt_tracefile_creation(LttTracefile
*tf
)
1519 return tf
->creation
;
1521 /*****************************************************************************
1522 * Get the number of blocks in the tracefile
1523 ****************************************************************************/
1525 guint
ltt_tracefile_block_number(LttTracefile
*tf
)
1527 return tf
->num_blocks
;
1531 /* Seek to the first event in a tracefile that has a time equal or greater than
1532 * the time passed in parameter.
1534 * If the time parameter is outside the tracefile time span, seek to the first
1535 * event or if after, return ERANGE.
1537 * If the time parameter is before the first event, we have to seek specially to
1540 * If the time is after the end of the trace, return ERANGE.
1542 * Do a binary search to find the right block, then a sequential search in the
1543 * block to find the event.
1545 * In the special case where the time requested fits inside a block that has no
1546 * event corresponding to the requested time, the first event of the next block
1549 * IMPORTANT NOTE : // FIXME everywhere...
1551 * You MUST NOT do a ltt_tracefile_read right after a ltt_tracefile_seek_time :
1552 * you will jump over an event if you do.
1554 * Return value : 0 : no error, the tf->event can be used
1555 * ERANGE : time if after the last event of the trace
1556 * otherwise : this is an error.
1560 int ltt_tracefile_seek_time(LttTracefile
*tf
, LttTime time
)
1564 unsigned int block_num
, high
, low
;
1566 /* seek at the beginning of trace */
1567 err
= map_block(tf
, 0); /* First block */
1569 g_error("Can not map block");
1573 /* If the time is lower or equal the beginning of the trace,
1574 * go to the first event. */
1575 if(ltt_time_compare(time
, tf
->buffer
.begin
.timestamp
) <= 0) {
1576 ret
= ltt_tracefile_read(tf
);
1577 if(ret
== ERANGE
) goto range
;
1578 else if (ret
) goto fail
;
1579 goto found
; /* There is either no event in the trace or the event points
1580 to the first event in the trace */
1583 err
= map_block(tf
, tf
->num_blocks
- 1); /* Last block */
1585 g_error("Can not map block");
1589 /* If the time is after the end of the trace, return ERANGE. */
1590 if(ltt_time_compare(time
, tf
->buffer
.end
.timestamp
) > 0) {
1594 /* Binary search the block */
1595 high
= tf
->num_blocks
- 1;
1599 block_num
= ((high
-low
) / 2) + low
;
1601 err
= map_block(tf
, block_num
);
1603 g_error("Can not map block");
1607 /* We cannot divide anymore : this is what would happen if the time
1608 * requested was exactly between two consecutive buffers'end and start
1609 * timestamps. This is also what would happend if we didn't deal with out
1610 * of span cases prior in this function. */
1611 /* The event is right in the buffer!
1612 * (or in the next buffer first event) */
1614 ret
= ltt_tracefile_read(tf
);
1615 if(ret
== ERANGE
) goto range
; /* ERANGE or EPERM */
1616 else if(ret
) goto fail
;
1618 if(ltt_time_compare(time
, tf
->event
.event_time
) <= 0)
1622 } else if(ltt_time_compare(time
, tf
->buffer
.begin
.timestamp
) < 0) {
1623 /* go to lower part */
1624 high
= block_num
- 1;
1625 } else if(ltt_time_compare(time
, tf
->buffer
.end
.timestamp
) > 0) {
1626 /* go to higher part */
1627 low
= block_num
+ 1;
1628 } else {/* The event is right in the buffer!
1629 (or in the next buffer first event) */
1631 ret
= ltt_tracefile_read(tf
);
1632 if(ret
== ERANGE
) goto range
; /* ERANGE or EPERM */
1633 else if(ret
) goto fail
;
1635 if(ltt_time_compare(time
, tf
->event
.event_time
) <= 0)
1647 /* Error handling */
1649 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1650 g_quark_to_string(tf
->name
));
1655 int ltt_tracefile_seek_position(LttTracefile
*tf
, const LttEventPosition
*ep
) {
1659 if(ep
->tracefile
!= tf
) {
1663 err
= map_block(tf
, ep
->block
);
1665 g_error("Can not map block");
1669 tf
->event
.offset
= ep
->offset
;
1671 /* Put back the event real tsc */
1672 tf
->event
.tsc
= ep
->tsc
;
1673 tf
->buffer
.tsc
= ep
->tsc
;
1675 err
= ltt_tracefile_read_update_event(tf
);
1677 err
= ltt_tracefile_read_op(tf
);
1683 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1684 g_quark_to_string(tf
->name
));
1688 LttTime
ltt_interpolate_time_from_tsc(LttTracefile
*tf
, guint64 tsc
)
1692 if(tsc
> tf
->trace
->start_tsc
) {
1693 time
= ltt_time_from_uint64(
1694 (double)(tsc
- tf
->trace
->start_tsc
)
1695 * (1000000000.0 / tf
->trace
->freq_scale
)
1696 / (double)tf
->trace
->start_freq
);
1697 time
= ltt_time_add(tf
->trace
->start_time_from_tsc
, time
);
1699 time
= ltt_time_from_uint64(
1700 (double)(tf
->trace
->start_tsc
- tsc
)
1701 * (1000000000.0 / tf
->trace
->freq_scale
)
1702 / (double)tf
->trace
->start_freq
);
1703 time
= ltt_time_sub(tf
->trace
->start_time_from_tsc
, time
);
1708 /* Calculate the real event time based on the buffer boundaries */
1709 LttTime
ltt_interpolate_time(LttTracefile
*tf
, LttEvent
*event
)
1711 return ltt_interpolate_time_from_tsc(tf
, tf
->buffer
.tsc
);
1715 /* Get the current event of the tracefile : valid until the next read */
1716 LttEvent
*ltt_tracefile_get_event(LttTracefile
*tf
)
1723 /*****************************************************************************
1725 * ltt_tracefile_read : Read the next event in the tracefile
1730 * Returns 0 if an event can be used in tf->event.
1731 * Returns ERANGE on end of trace. The event in tf->event still can be used
1732 * (if the last block was not empty).
1733 * Returns EPERM on error.
1735 * This function does make the tracefile event structure point to the event
1736 * currently pointed to by the tf->event.
1738 * Note : you must call a ltt_tracefile_seek to the beginning of the trace to
1739 * reinitialize it after an error if you want results to be coherent.
1740 * It would be the case if a end of trace last buffer has no event : the end
1741 * of trace wouldn't be returned, but an error.
1742 * We make the assumption there is at least one event per buffer.
1743 ****************************************************************************/
1745 int ltt_tracefile_read(LttTracefile
*tf
)
1749 err
= ltt_tracefile_read_seek(tf
);
1751 err
= ltt_tracefile_read_update_event(tf
);
1753 err
= ltt_tracefile_read_op(tf
);
1759 int ltt_tracefile_read_seek(LttTracefile
*tf
)
1763 /* Get next buffer until we finally have an event, or end of trace */
1765 err
= ltt_seek_next_event(tf
);
1766 if(unlikely(err
== ENOPROTOOPT
)) {
1770 /* Are we at the end of the buffer ? */
1772 if(unlikely(tf
->buffer
.index
== tf
->num_blocks
-1)){ /* end of trace ? */
1775 /* get next block */
1776 err
= map_block(tf
, tf
->buffer
.index
+ 1);
1778 g_error("Can not map block");
1782 } else break; /* We found an event ! */
1789 /* do specific operation on events */
1790 int ltt_tracefile_read_op(LttTracefile
*tf
)
1796 /* do event specific operation */
1798 /* do something if its an heartbeat event : increment the heartbeat count */
1799 //if(event->facility_id == LTT_FACILITY_CORE)
1800 // if(event->event_id == LTT_EVENT_HEARTBEAT)
1801 // tf->cur_heart_beat_number++;
1807 /* same as ltt_tracefile_read, but does not seek to the next event nor call
1808 * event specific operation. */
1809 int ltt_tracefile_read_update_event(LttTracefile
*tf
)
1815 pos
= tf
->buffer
.head
+ event
->offset
;
1817 /* Read event header */
1819 /* Align the head */
1820 pos
+= ltt_align((size_t)pos
, tf
->trace
->arch_size
, tf
->has_alignment
);
1822 if(tf
->trace
->has_heartbeat
) {
1823 event
->timestamp
= ltt_get_uint32(LTT_GET_BO(tf
),
1825 /* 32 bits -> 64 bits tsc */
1826 /* note : still works for seek and non seek cases. */
1827 if(event
->timestamp
< (0xFFFFFFFFULL
&tf
->buffer
.tsc
)) {
1828 tf
->buffer
.tsc
= ((tf
->buffer
.tsc
&0xFFFFFFFF00000000ULL
)
1830 | (guint64
)event
->timestamp
;
1831 event
->tsc
= tf
->buffer
.tsc
;
1834 tf
->buffer
.tsc
= (tf
->buffer
.tsc
&0xFFFFFFFF00000000ULL
)
1835 | (guint64
)event
->timestamp
;
1836 event
->tsc
= tf
->buffer
.tsc
;
1838 pos
+= sizeof(guint32
);
1840 event
->tsc
= ltt_get_uint64(LTT_GET_BO(tf
), pos
);
1841 tf
->buffer
.tsc
= event
->tsc
;
1842 pos
+= sizeof(guint64
);
1844 event
->event_time
= ltt_interpolate_time(tf
, event
);
1845 event
->facility_id
= *(guint8
*)pos
;
1846 pos
+= sizeof(guint8
);
1848 event
->event_id
= *(guint8
*)pos
;
1849 pos
+= sizeof(guint8
);
1851 event
->event_size
= ltt_get_uint16(LTT_GET_BO(tf
), pos
);
1852 pos
+= sizeof(guint16
);
1854 /* Align the head */
1855 pos
+= ltt_align((size_t)pos
, tf
->trace
->arch_size
, tf
->has_alignment
);
1859 /* get the data size and update the event fields with the current
1861 ltt_update_event_size(tf
);
1867 /****************************************************************************
1869 * map_block : map a block from the file
1871 * lttdes : ltt trace file
1872 * whichBlock : the block which will be read
1875 * EINVAL : lseek fail
1876 * EIO : can not read from the file
1877 ****************************************************************************/
1879 static gint
map_block(LttTracefile
* tf
, guint block_num
)
1881 int page_size
= getpagesize();
1882 struct ltt_block_start_header
*header
;
1884 g_assert(block_num
< tf
->num_blocks
);
1886 if(tf
->buffer
.head
!= NULL
) {
1887 if(munmap(tf
->buffer
.head
, PAGE_ALIGN(tf
->buf_size
))) {
1888 g_warning("unmap size : %u\n",
1889 PAGE_ALIGN(tf
->buf_size
));
1890 perror("munmap error");
1896 /* Multiple of pages aligned head */
1897 tf
->buffer
.head
= mmap(0,
1898 PAGE_ALIGN(tf
->buf_size
),
1899 PROT_READ
, MAP_PRIVATE
, tf
->fd
,
1900 PAGE_ALIGN((off_t
)tf
->buf_size
* (off_t
)block_num
));
1902 if(tf
->buffer
.head
== MAP_FAILED
) {
1903 perror("Error in allocating memory for buffer of tracefile");
1907 g_assert( ( (guint
)tf
->buffer
.head
&(8-1) ) == 0); // make sure it's aligned.
1910 tf
->buffer
.index
= block_num
;
1912 header
= (struct ltt_block_start_header
*)tf
->buffer
.head
;
1915 tf
->buffer
.begin
.timestamp
= ltt_time_add(
1916 ltt_time_from_uint64(
1917 ltt_get_uint64(LTT_GET_BO(tf
),
1918 &header
->begin
.timestamp
)
1919 - tf
->trace
->start_monotonic
),
1920 tf
->trace
->start_time
);
1922 //g_debug("block %u begin : %lu.%lu", block_num,
1923 // tf->buffer.begin.timestamp.tv_sec, tf->buffer.begin.timestamp.tv_nsec);
1924 tf
->buffer
.begin
.cycle_count
= ltt_get_uint64(LTT_GET_BO(tf
),
1925 &header
->begin
.cycle_count
);
1926 tf
->buffer
.begin
.freq
= ltt_get_uint64(LTT_GET_BO(tf
),
1927 &header
->begin
.freq
);
1928 if(tf
->buffer
.begin
.freq
== 0)
1929 tf
->buffer
.begin
.freq
= tf
->trace
->start_freq
;
1931 tf
->buffer
.begin
.timestamp
= ltt_interpolate_time_from_tsc(tf
,
1932 tf
->buffer
.begin
.cycle_count
);
1935 ltt_time_from_uint64(
1936 (double)(tf
->buffer
.begin
.cycle_count
1937 - tf
->trace
->start_tsc
) * 1000000.0
1938 / (double)tf
->trace
->start_freq
),
1939 tf
->trace
->start_time_from_tsc
);
1943 tf
->buffer
.end
.timestamp
= ltt_time_add(
1944 ltt_time_from_uint64(
1945 ltt_get_uint64(LTT_GET_BO(tf
),
1946 &header
->end
.timestamp
)
1947 - tf
->trace
->start_monotonic
),
1948 tf
->trace
->start_time
);
1950 //g_debug("block %u end : %lu.%lu", block_num,
1951 // tf->buffer.end.timestamp.tv_sec, tf->buffer.end.timestamp.tv_nsec);
1952 tf
->buffer
.end
.cycle_count
= ltt_get_uint64(LTT_GET_BO(tf
),
1953 &header
->end
.cycle_count
);
1954 tf
->buffer
.end
.freq
= ltt_get_uint64(LTT_GET_BO(tf
),
1956 if(tf
->buffer
.end
.freq
== 0)
1957 tf
->buffer
.end
.freq
= tf
->trace
->start_freq
;
1959 tf
->buffer
.lost_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1960 &header
->lost_size
);
1961 tf
->buffer
.end
.timestamp
= ltt_interpolate_time_from_tsc(tf
,
1962 tf
->buffer
.end
.cycle_count
);
1965 ltt_time_from_uint64(
1966 (double)(tf
->buffer
.end
.cycle_count
1967 - tf
->trace
->start_tsc
) * 1000000.0
1968 / (double)tf
->trace
->start_freq
),
1969 tf
->trace
->start_time_from_tsc
);
1971 tf
->buffer
.tsc
= tf
->buffer
.begin
.cycle_count
;
1972 tf
->event
.tsc
= tf
->buffer
.tsc
;
1973 tf
->buffer
.freq
= tf
->buffer
.begin
.freq
;
1976 * eventually support variable buffer size : will need a partial pre-read of
1977 * the headers to create an index when we open the trace... eventually. */
1978 g_assert(tf
->buf_size
== ltt_get_uint32(LTT_GET_BO(tf
),
1979 &header
->buf_size
));
1981 /* Now that the buffer is mapped, calculate the time interpolation for the
1984 // tf->buffer.nsecs_per_cycle = calc_nsecs_per_cycle(tf);
1985 //tf->buffer.cyc2ns_scale = calc_nsecs_per_cycle(tf);
1987 /* Make the current event point to the beginning of the buffer :
1988 * it means that the event read must get the first event. */
1989 tf
->event
.tracefile
= tf
;
1990 tf
->event
.block
= block_num
;
1991 tf
->event
.offset
= 0;
2000 /* It will update the fields offsets too */
2001 void ltt_update_event_size(LttTracefile
*tf
)
2005 /* Specific handling of core events : necessary to read the facility control
2007 LttFacility
*f
= ltt_trace_get_facility_by_num(tf
->trace
,
2008 tf
->event
.facility_id
);
2010 if(likely(tf
->event
.facility_id
== LTT_FACILITY_CORE
)) {
2011 switch((enum ltt_core_events
)tf
->event
.event_id
) {
2012 case LTT_EVENT_FACILITY_LOAD
:
2013 size
= strlen((char*)tf
->event
.data
) + 1;
2014 //g_debug("Update Event facility load of facility %s", (char*)tf->event.data);
2015 size
+= ltt_align(size
, sizeof(guint32
), tf
->has_alignment
);
2016 size
+= sizeof(struct LttFacilityLoad
);
2018 case LTT_EVENT_FACILITY_UNLOAD
:
2019 //g_debug("Update Event facility unload");
2020 size
= sizeof(struct LttFacilityUnload
);
2022 case LTT_EVENT_STATE_DUMP_FACILITY_LOAD
:
2023 size
= strlen((char*)tf
->event
.data
) + 1;
2024 size
+= ltt_align(size
, sizeof(guint32
), tf
->has_alignment
);
2025 //g_debug("Update Event facility load state dump of facility %s",
2026 // (char*)tf->event.data);
2027 size
+= sizeof(struct LttStateDumpFacilityLoad
);
2029 case LTT_EVENT_HEARTBEAT
:
2030 //g_debug("Update Event heartbeat");
2031 size
= sizeof(TimeHeartbeat
);
2034 g_warning("Error in getting event size : tracefile %s, "
2035 "unknown event id %hhu in core facility.",
2036 g_quark_to_string(tf
->name
),
2037 tf
->event
.event_id
);
2038 goto event_id_error
;
2043 g_warning("Unknown facility %hhu (0x%hhx) in tracefile %s",
2044 tf
->event
.facility_id
,
2045 tf
->event
.facility_id
,
2046 g_quark_to_string(tf
->name
));
2047 goto facility_error
;
2050 LttEventType
*event_type
=
2051 ltt_facility_eventtype_get(f
, tf
->event
.event_id
);
2054 g_warning("Unknown event id %hhu in facility %s in tracefile %s",
2056 g_quark_to_string(f
->name
),
2057 g_quark_to_string(tf
->name
));
2058 goto event_type_error
;
2061 /* Compute the dynamic offsets */
2062 compute_offsets(tf
, f
, event_type
, &size
, tf
->event
.data
);
2064 //g_debug("Event root field : f.e %hhu.%hhu size %zd",
2065 // tf->event.facility_id,
2066 // tf->event.event_id, size);
2069 tf
->event
.data_size
= size
;
2071 /* Check consistency between kernel and LTTV structure sizes */
2072 if(tf
->event
.event_size
== 0xFFFF) {
2073 /* Event size too big to fit in the event size field */
2074 tf
->event
.event_size
= tf
->event
.data_size
;
2076 g_assert(tf
->event
.data_size
== tf
->event
.event_size
);
2083 if(tf
->event
.event_size
== 0xFFFF) {
2084 g_error("Cannot jump over an unknown event bigger than 0xFFFE bytes");
2086 /* The facility is unknown : use the kernel information about this event
2087 * to jump over it. */
2088 tf
->event
.data_size
= tf
->event
.event_size
;
2092 /* Take the tf current event offset and use the event facility id and event id
2093 * to figure out where is the next event offset.
2095 * This is an internal function not aiming at being used elsewhere : it will
2096 * not jump over the current block limits. Please consider using
2097 * ltt_tracefile_read to do this.
2099 * Returns 0 on success
2100 * ERANGE if we are at the end of the buffer.
2101 * ENOPROTOOPT if an error occured when getting the current event size.
2103 static int ltt_seek_next_event(LttTracefile
*tf
)
2108 /* seek over the buffer header if we are at the buffer start */
2109 if(tf
->event
.offset
== 0) {
2110 tf
->event
.offset
+= tf
->buffer_header_size
;
2112 if(tf
->event
.offset
== tf
->buf_size
- tf
->buffer
.lost_size
) {
2119 pos
= tf
->event
.data
;
2121 if(tf
->event
.data_size
< 0) goto error
;
2123 pos
+= (size_t)tf
->event
.data_size
;
2125 tf
->event
.offset
= pos
- tf
->buffer
.head
;
2127 if(tf
->event
.offset
== tf
->buf_size
- tf
->buffer
.lost_size
) {
2131 g_assert(tf
->event
.offset
< tf
->buf_size
- tf
->buffer
.lost_size
);
2137 g_error("Error in ltt_seek_next_event for tracefile %s",
2138 g_quark_to_string(tf
->name
));
2143 /*****************************************************************************
2145 * calc_nsecs_per_cycle : calculate nsecs per cycle for current block
2147 * 1.0 / (freq(khz) *1000) * 1000000000
2150 ****************************************************************************/
2151 /* from timer_tsc.c */
2152 #define CYC2NS_SCALE_FACTOR 10
2153 static guint32
calc_nsecs_per_cycle(LttTracefile
* tf
)
2155 //return 1e6 / (double)tf->buffer.freq;
2156 guint32 cpu_mhz
= tf
->buffer
.freq
/ 1000;
2157 guint32 cyc2ns_scale
= (1000 << CYC2NS_SCALE_FACTOR
)/cpu_mhz
;
2159 return cyc2ns_scale
;
2160 // return 1e6 / (double)tf->buffer.freq;
2163 static guint64
cycles_2_ns(LttTracefile
*tf
, guint64 cycles
)
2165 return (cycles
* tf
->buffer
.cyc2ns_scale
) >> CYC2NS_SCALE_FACTOR
;
2170 void setFieldsOffset(LttTracefile
*tf
, LttEventType
*evT
,void *evD
)
2172 LttField
* rootFld
= evT
->root_field
;
2173 // rootFld->base_address = evD;
2176 rootFld
->field_size
= getFieldtypeSize(tf
, evT
->facility
,
2177 evT
, 0,0,rootFld
, evD
);
2181 /*****************************************************************************
2183 * set_fields_offsets : set the precomputable offset of the fields
2185 * tracefile : opened trace file
2186 * event_type : the event type
2187 ****************************************************************************/
2189 void set_fields_offsets(LttTracefile
*tf
, LttEventType
*event_type
)
2191 LttField
*field
= event_type
->root_field
;
2192 enum field_status fixed_root
= FIELD_FIXED
, fixed_parent
= FIELD_FIXED
;
2195 preset_field_type_size(tf
, event_type
, 0, 0,
2196 &fixed_root
, &fixed_parent
,
2203 /*****************************************************************************
2205 * get_alignment : Get the alignment needed for a field.
2209 * returns : The size on which it must be aligned.
2211 ****************************************************************************/
2212 off_t
get_alignment(LttField
*field
)
2214 LttType
*type
= &field
->field_type
;
2216 switch(type
->type_class
) {
2218 case LTT_UINT_FIXED
:
2233 /* Align offset on type size */
2234 g_assert(field
->field_size
!= 0);
2235 return field
->field_size
;
2241 g_assert(type
->fields
->len
== 1);
2243 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
2244 return get_alignment(child
);
2248 g_assert(type
->fields
->len
== 2);
2251 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
2253 localign
= max(localign
, get_alignment(child
));
2255 child
= &g_array_index(type
->fields
, LttField
, 1);
2256 localign
= max(localign
, get_alignment(child
));
2267 for(i
=0; i
<type
->fields
->len
; i
++) {
2268 LttField
*child
= &g_array_index(type
->fields
, LttField
, i
);
2269 localign
= max(localign
, get_alignment(child
));
2276 g_error("get_alignment : unknown type");
2281 /*****************************************************************************
2283 * field_compute_static_size : Determine the size of fields known by their
2284 * sole definition. Unions, arrays and struct sizes might be known, but
2285 * the parser does not give that information.
2290 ****************************************************************************/
2292 void field_compute_static_size(LttFacility
*fac
, LttField
*field
)
2294 LttType
*type
= &field
->field_type
;
2296 switch(type
->type_class
) {
2298 case LTT_UINT_FIXED
:
2317 /* note this : array type size is the number of elements in the array,
2318 * while array field size of the length of the array in bytes */
2319 g_assert(type
->fields
->len
== 1);
2321 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
2322 field_compute_static_size(fac
, child
);
2324 if(child
->field_size
!= 0) {
2325 field
->field_size
= type
->size
* child
->field_size
;
2326 field
->dynamic_offsets
= g_array_sized_new(FALSE
, TRUE
,
2327 sizeof(off_t
), type
->size
);
2329 field
->field_size
= 0;
2334 g_assert(type
->fields
->len
== 2);
2336 off_t local_offset
= 0;
2337 LttField
*child
= &g_array_index(type
->fields
, LttField
, 1);
2338 field_compute_static_size(fac
, child
);
2339 field
->field_size
= 0;
2341 if(child
->field_size
!= 0) {
2342 field
->dynamic_offsets
= g_array_sized_new(FALSE
, TRUE
,
2343 sizeof(off_t
), SEQUENCE_AVG_ELEMENTS
);
2351 for(i
=0;i
<type
->fields
->len
;i
++) {
2352 LttField
*child
= &g_array_index(type
->fields
, LttField
, i
);
2353 field_compute_static_size(fac
, child
);
2354 if(child
->field_size
!= 0) {
2355 type
->size
+= ltt_align(type
->size
, get_alignment(child
),
2357 type
->size
+= child
->field_size
;
2359 /* As soon as we find a child with variable size, we have
2360 * a variable size */
2365 field
->field_size
= type
->size
;
2369 g_error("field_static_size : unknown type");
2376 /*****************************************************************************
2378 * precompute_fields_offsets : set the precomputable offset of the fields
2382 * offset : pointer to the current offset, must be incremented
2384 * return : 1 : found a variable length field, stop the processing.
2386 ****************************************************************************/
2389 gint
precompute_fields_offsets(LttFacility
*fac
, LttField
*field
, off_t
*offset
)
2391 LttType
*type
= &field
->field_type
;
2393 switch(type
->type_class
) {
2395 case LTT_UINT_FIXED
:
2410 g_assert(field
->field_size
!= 0);
2411 /* Align offset on type size */
2412 *offset
+= ltt_align(*offset
, get_alignment(field
),
2414 /* remember offset */
2415 field
->offset_root
= *offset
;
2416 field
->fixed_root
= FIELD_FIXED
;
2417 /* Increment offset */
2418 *offset
+= field
->field_size
;
2422 field
->offset_root
= *offset
;
2423 field
->fixed_root
= FIELD_FIXED
;
2427 g_assert(type
->fields
->len
== 1);
2429 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
2431 *offset
+= ltt_align(*offset
, get_alignment(field
),
2434 /* remember offset */
2435 field
->offset_root
= *offset
;
2436 field
->array_offset
= *offset
;
2437 field
->fixed_root
= FIELD_FIXED
;
2439 /* Let the child be variable */
2440 //precompute_fields_offsets(tf, child, offset);
2442 if(field
->field_size
!= 0) {
2443 /* Increment offset */
2444 /* field_size is the array size in bytes */
2445 *offset
+= field
->field_size
;
2453 g_assert(type
->fields
->len
== 2);
2458 *offset
+= ltt_align(*offset
, get_alignment(field
),
2461 /* remember offset */
2462 field
->offset_root
= *offset
;
2463 field
->fixed_root
= FIELD_FIXED
;
2465 child
= &g_array_index(type
->fields
, LttField
, 0);
2466 ret
= precompute_fields_offsets(fac
, child
, offset
);
2467 g_assert(ret
== 0); /* Seq len cannot have variable len */
2469 child
= &g_array_index(type
->fields
, LttField
, 1);
2470 *offset
+= ltt_align(*offset
, get_alignment(child
),
2472 field
->array_offset
= *offset
;
2473 /* Let the child be variable. */
2474 //ret = precompute_fields_offsets(fac, child, offset);
2476 /* Cannot precompute fields offsets of sequence members, and has
2477 * variable length. */
2487 *offset
+= ltt_align(*offset
, get_alignment(field
),
2489 /* remember offset */
2490 field
->offset_root
= *offset
;
2491 field
->fixed_root
= FIELD_FIXED
;
2493 for(i
=0; i
< type
->fields
->len
; i
++) {
2494 child
= &g_array_index(type
->fields
, LttField
, i
);
2495 ret
= precompute_fields_offsets(fac
, child
, offset
);
2508 *offset
+= ltt_align(*offset
, get_alignment(field
),
2510 /* remember offset */
2511 field
->offset_root
= *offset
;
2512 field
->fixed_root
= FIELD_FIXED
;
2514 for(i
=0; i
< type
->fields
->len
; i
++) {
2515 *offset
= field
->offset_root
;
2516 child
= &g_array_index(type
->fields
, LttField
, i
);
2517 ret
= precompute_fields_offsets(fac
, child
, offset
);
2521 *offset
= field
->offset_root
+ field
->field_size
;
2528 g_error("precompute_fields_offsets : unknown type");
2535 /*****************************************************************************
2537 * precompute_offsets : set the precomputable offset of an event type
2540 * event : event type
2542 ****************************************************************************/
2543 void precompute_offsets(LttFacility
*fac
, LttEventType
*event
)
2549 /* First, compute the size of fixed size fields. Will determine size for
2550 * arrays, struct and unions, which is not done by the parser */
2551 for(i
=0; i
<event
->fields
->len
; i
++) {
2552 LttField
*field
= &g_array_index(event
->fields
, LttField
, i
);
2553 field_compute_static_size(fac
, field
);
2556 /* Precompute all known offsets */
2557 for(i
=0; i
<event
->fields
->len
; i
++) {
2558 LttField
*field
= &g_array_index(event
->fields
, LttField
, i
);
2559 ret
= precompute_fields_offsets(fac
, field
, &offset
);
2567 /*****************************************************************************
2569 * preset_field_type_size : set the fixed sizes of the field type
2572 * event_type : event type
2573 * offset_root : offset from the root
2574 * offset_parent : offset from the parent
2575 * fixed_root : Do we know a fixed offset to the root ?
2576 * fixed_parent : Do we know a fixed offset to the parent ?
2578 ****************************************************************************/
2582 // preset the fixed size offsets. Calculate them just like genevent-new : an
2583 // increment of a *to value that represents the offset from the start of the
2585 // The preset information is : offsets up to (and including) the first element
2586 // of variable size. All subsequent fields must be flagged "VARIABLE OFFSET".
2588 void preset_field_type_size(LttTracefile
*tf
, LttEventType
*event_type
,
2589 off_t offset_root
, off_t offset_parent
,
2590 enum field_status
*fixed_root
, enum field_status
*fixed_parent
,
2593 enum field_status local_fixed_root
, local_fixed_parent
;
2597 g_assert(field
->fixed_root
== FIELD_UNKNOWN
);
2598 g_assert(field
->fixed_parent
== FIELD_UNKNOWN
);
2599 g_assert(field
->fixed_size
== FIELD_UNKNOWN
);
2601 type
= field
->field_type
;
2603 field
->fixed_root
= *fixed_root
;
2604 if(field
->fixed_root
== FIELD_FIXED
)
2605 field
->offset_root
= offset_root
;
2607 field
->offset_root
= 0;
2609 field
->fixed_parent
= *fixed_parent
;
2610 if(field
->fixed_parent
== FIELD_FIXED
)
2611 field
->offset_parent
= offset_parent
;
2613 field
->offset_parent
= 0;
2615 size_t current_root_offset
;
2616 size_t current_offset
;
2617 enum field_status current_child_status
, final_child_status
;
2620 switch(type
->type_class
) {
2622 case LTT_UINT_FIXED
:
2631 field
->field_size
= ltt_type_size(tf
->trace
, type
);
2632 field
->fixed_size
= FIELD_FIXED
;
2635 field
->field_size
= (off_t
)event_type
->facility
->pointer_size
;
2636 field
->fixed_size
= FIELD_FIXED
;
2640 field
->field_size
= (off_t
)event_type
->facility
->long_size
;
2641 field
->fixed_size
= FIELD_FIXED
;
2646 field
->field_size
= (off_t
)event_type
->facility
->size_t_size
;
2647 field
->fixed_size
= FIELD_FIXED
;
2650 local_fixed_root
= FIELD_VARIABLE
;
2651 local_fixed_parent
= FIELD_VARIABLE
;
2652 preset_field_type_size(tf
, event_type
,
2654 &local_fixed_root
, &local_fixed_parent
,
2656 field
->fixed_size
= FIELD_VARIABLE
;
2657 field
->field_size
= 0;
2658 *fixed_root
= FIELD_VARIABLE
;
2659 *fixed_parent
= FIELD_VARIABLE
;
2662 field
->fixed_size
= FIELD_VARIABLE
;
2663 field
->field_size
= 0;
2664 *fixed_root
= FIELD_VARIABLE
;
2665 *fixed_parent
= FIELD_VARIABLE
;
2668 local_fixed_root
= FIELD_VARIABLE
;
2669 local_fixed_parent
= FIELD_VARIABLE
;
2670 preset_field_type_size(tf
, event_type
,
2672 &local_fixed_root
, &local_fixed_parent
,
2674 field
->fixed_size
= field
->child
[0]->fixed_size
;
2675 if(field
->fixed_size
== FIELD_FIXED
) {
2676 field
->field_size
= type
->element_number
* field
->child
[0]->field_size
;
2678 field
->field_size
= 0;
2679 *fixed_root
= FIELD_VARIABLE
;
2680 *fixed_parent
= FIELD_VARIABLE
;
2684 current_root_offset
= field
->offset_root
;
2686 current_child_status
= FIELD_FIXED
;
2687 for(i
=0;i
<type
->element_number
;i
++) {
2688 preset_field_type_size(tf
, event_type
,
2689 current_root_offset
, current_offset
,
2690 fixed_root
, ¤t_child_status
,
2692 if(current_child_status
== FIELD_FIXED
) {
2693 current_root_offset
+= field
->child
[i
]->field_size
;
2694 current_offset
+= field
->child
[i
]->field_size
;
2696 current_root_offset
= 0;
2700 if(current_child_status
!= FIELD_FIXED
) {
2701 *fixed_parent
= current_child_status
;
2702 field
->field_size
= 0;
2703 field
->fixed_size
= current_child_status
;
2705 field
->field_size
= current_offset
;
2706 field
->fixed_size
= FIELD_FIXED
;
2710 current_root_offset
= field
->offset_root
;
2713 final_child_status
= FIELD_FIXED
;
2714 for(i
=0;i
<type
->element_number
;i
++) {
2715 enum field_status current_root_child_status
= FIELD_FIXED
;
2716 enum field_status current_child_status
= FIELD_FIXED
;
2717 preset_field_type_size(tf
, event_type
,
2718 current_root_offset
, current_offset
,
2719 ¤t_root_child_status
, ¤t_child_status
,
2721 if(current_child_status
!= FIELD_FIXED
)
2722 final_child_status
= current_child_status
;
2724 max_size
= max(max_size
, field
->child
[i
]->field_size
);
2726 if(final_child_status
!= FIELD_FIXED
) {
2727 g_error("LTTV does not support variable size fields in unions.");
2728 /* This will stop the application. */
2729 *fixed_root
= final_child_status
;
2730 *fixed_parent
= final_child_status
;
2731 field
->field_size
= 0;
2732 field
->fixed_size
= current_child_status
;
2734 field
->field_size
= max_size
;
2735 field
->fixed_size
= FIELD_FIXED
;
2739 g_error("unexpected type NONE");
2746 /*****************************************************************************
2748 * check_fields_compatibility : Check for compatibility between two fields :
2749 * do they use the same inner structure ?
2751 * event_type1 : event type
2752 * event_type2 : event type
2755 *Returns : 0 if identical
2757 ****************************************************************************/
2758 // this function checks for equality of field types. Therefore, it does not use
2759 // per se offsets. For instance, an aligned version of a structure is
2760 // compatible with an unaligned version of the same structure.
2761 gint
check_fields_compatibility(LttEventType
*event_type1
,
2762 LttEventType
*event_type2
,
2763 LttField
*field1
, LttField
*field2
)
2765 guint different
= 0;
2769 if(field1
== NULL
) {
2770 if(field2
== NULL
) goto end
;
2775 } else if(field2
== NULL
) {
2780 type1
= &field1
->field_type
;
2781 type2
= &field2
->field_type
;
2783 if(type1
->type_class
!= type2
->type_class
) {
2787 if(type1
->network
!= type2
->network
) {
2792 switch(type1
->type_class
) {
2794 case LTT_UINT_FIXED
:
2809 if(field1
->field_size
!= field2
->field_size
)
2816 LttField
*child1
= &g_array_index(type1
->fields
, LttField
, 0);
2817 LttField
*child2
= &g_array_index(type2
->fields
, LttField
, 0);
2819 if(type1
->size
!= type2
->size
)
2821 if(check_fields_compatibility(event_type1
, event_type2
, child1
, child2
))
2827 LttField
*child1
= &g_array_index(type1
->fields
, LttField
, 1);
2828 LttField
*child2
= &g_array_index(type2
->fields
, LttField
, 1);
2830 if(check_fields_compatibility(event_type1
, event_type2
, child1
, child2
))
2840 if(type1
->fields
->len
!= type2
->fields
->len
) {
2845 for(i
=0; i
< type1
->fields
->len
; i
++) {
2848 child1
= &g_array_index(type1
->fields
, LttField
, i
);
2849 child2
= &g_array_index(type2
->fields
, LttField
, i
);
2850 different
= check_fields_compatibility(event_type1
,
2851 event_type2
, child1
, child2
);
2853 if(different
) break;
2859 g_error("precompute_fields_offsets : unknown type");
2868 gint
check_fields_compatibility(LttEventType
*event_type1
,
2869 LttEventType
*event_type2
,
2870 LttField
*field1
, LttField
*field2
)
2872 guint different
= 0;
2877 if(field1
== NULL
) {
2878 if(field2
== NULL
) goto end
;
2883 } else if(field2
== NULL
) {
2888 g_assert(field1
->fixed_root
!= FIELD_UNKNOWN
);
2889 g_assert(field2
->fixed_root
!= FIELD_UNKNOWN
);
2890 g_assert(field1
->fixed_parent
!= FIELD_UNKNOWN
);
2891 g_assert(field2
->fixed_parent
!= FIELD_UNKNOWN
);
2892 g_assert(field1
->fixed_size
!= FIELD_UNKNOWN
);
2893 g_assert(field2
->fixed_size
!= FIELD_UNKNOWN
);
2895 type1
= field1
->field_type
;
2896 type2
= field2
->field_type
;
2898 if(type1
->type_class
!= type2
->type_class
) {
2902 if(type1
->element_name
!= type2
->element_name
) {
2907 switch(type1
->type_class
) {
2909 case LTT_UINT_FIXED
:
2924 if(field1
->field_size
!= field2
->field_size
) {
2930 if(type1
->element_number
!= type2
->element_number
) {
2934 for(i
=0;i
<type1
->element_number
;i
++) {
2935 if(type1
->enum_strings
[i
] != type2
->enum_strings
[i
]) {
2942 /* Two elements : size and child */
2943 g_assert(type1
->element_number
!= type2
->element_number
);
2944 for(i
=0;i
<type1
->element_number
;i
++) {
2945 if(check_fields_compatibility(event_type1
, event_type2
,
2946 field1
->child
[0], field2
->child
[0])) {
2955 if(field1
->field_size
!= field2
->field_size
) {
2959 /* Two elements : size and child */
2960 g_assert(type1
->element_number
!= type2
->element_number
);
2961 for(i
=0;i
<type1
->element_number
;i
++) {
2962 if(check_fields_compatibility(event_type1
, event_type2
,
2963 field1
->child
[0], field2
->child
[0])) {
2971 if(type1
->element_number
!= type2
->element_number
) {
2975 for(i
=0;i
<type1
->element_number
;i
++) {
2976 if(check_fields_compatibility(event_type1
, event_type2
,
2977 field1
->child
[0], field2
->child
[0])) {
2990 /*****************************************************************************
2992 * ltt_get_int : get an integer number
2994 * reverse_byte_order: must we reverse the byte order ?
2995 * size : the size of the integer
2996 * ptr : the data pointer
2998 * gint64 : a 64 bits integer
2999 ****************************************************************************/
3001 gint64
ltt_get_int(gboolean reverse_byte_order
, gint size
, void *data
)
3006 case 1: val
= *((gint8
*)data
); break;
3007 case 2: val
= ltt_get_int16(reverse_byte_order
, data
); break;
3008 case 4: val
= ltt_get_int32(reverse_byte_order
, data
); break;
3009 case 8: val
= ltt_get_int64(reverse_byte_order
, data
); break;
3010 default: val
= ltt_get_int64(reverse_byte_order
, data
);
3011 g_critical("get_int : integer size %d unknown", size
);
3018 /*****************************************************************************
3020 * ltt_get_uint : get an unsigned integer number
3022 * reverse_byte_order: must we reverse the byte order ?
3023 * size : the size of the integer
3024 * ptr : the data pointer
3026 * guint64 : a 64 bits unsigned integer
3027 ****************************************************************************/
3029 guint64
ltt_get_uint(gboolean reverse_byte_order
, gint size
, void *data
)
3034 case 1: val
= *((gint8
*)data
); break;
3035 case 2: val
= ltt_get_uint16(reverse_byte_order
, data
); break;
3036 case 4: val
= ltt_get_uint32(reverse_byte_order
, data
); break;
3037 case 8: val
= ltt_get_uint64(reverse_byte_order
, data
); break;
3038 default: val
= ltt_get_uint64(reverse_byte_order
, data
);
3039 g_critical("get_uint : unsigned integer size %d unknown",
3048 /* get the node name of the system */
3050 char * ltt_trace_system_description_node_name (LttSystemDescription
* s
)
3052 return s
->node_name
;
3056 /* get the domain name of the system */
3058 char * ltt_trace_system_description_domain_name (LttSystemDescription
* s
)
3060 return s
->domain_name
;
3064 /* get the description of the system */
3066 char * ltt_trace_system_description_description (LttSystemDescription
* s
)
3068 return s
->description
;
3072 /* get the NTP corrected start time of the trace */
3073 LttTime
ltt_trace_start_time(LttTrace
*t
)
3075 return t
->start_time
;
3078 /* get the monotonic start time of the trace */
3079 LttTime
ltt_trace_start_time_monotonic(LttTrace
*t
)
3081 return t
->start_time_from_tsc
;
3084 LttTracefile
*ltt_tracefile_new()
3086 return g_new(LttTracefile
, 1);
3089 void ltt_tracefile_destroy(LttTracefile
*tf
)
3094 void ltt_tracefile_copy(LttTracefile
*dest
, const LttTracefile
*src
)
3099 /* Before library loading... */
3101 static void __attribute__((constructor
)) init(void)
3103 LTT_FACILITY_NAME_HEARTBEAT
= g_quark_from_string("heartbeat");
3104 LTT_EVENT_NAME_HEARTBEAT
= g_quark_from_string("heartbeat");
3106 LTT_TRACEFILE_NAME_FACILITIES
= g_quark_from_string("/control/facilities");