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 program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License Version 2 as
8 * published by the Free Software Foundation;
10 * This program 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
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
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(LttTracefile
*tf
, 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
->has_tsc
= any
->has_tsc
;
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_4
*vheader
=
256 (struct ltt_trace_header_0_4
*)header
;
257 tf
->buffer_header_size
=
258 sizeof(struct ltt_block_start_header
)
259 + sizeof(struct ltt_trace_header_0_4
);
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
= ltt_get_time(LTT_GET_BO(tf
),
268 &vheader
->start_time
);
269 t
->start_time
.tv_nsec
*= 1000; /* microsec to nanosec */
271 t
->start_time_from_tsc
= ltt_time_from_uint64(
272 (double)t
->start_tsc
* 1000000.0 / (double)t
->start_freq
);
277 g_warning("Unsupported trace version : %hhu.%hhu",
278 any
->major_version
, any
->minor_version
);
284 g_warning("Unsupported trace version : %hhu.%hhu",
285 any
->major_version
, any
->minor_version
);
295 /*****************************************************************************
297 * ltt_tracefile_open : open a trace file, construct a LttTracefile
299 * t : the trace containing the tracefile
300 * fileName : path name of the trace file
301 * tf : the tracefile structure
303 * : 0 for success, -1 otherwise.
304 ****************************************************************************/
306 gint
ltt_tracefile_open(LttTrace
*t
, gchar
* fileName
, LttTracefile
*tf
)
308 struct stat lTDFStat
; /* Trace data file status */
309 struct ltt_block_start_header
*header
;
310 int page_size
= getpagesize();
313 tf
->long_name
= g_quark_from_string(fileName
);
315 tf
->fd
= open(fileName
, O_RDONLY
);
317 g_warning("Unable to open input data file %s\n", fileName
);
321 // Get the file's status
322 if(fstat(tf
->fd
, &lTDFStat
) < 0){
323 g_warning("Unable to get the status of the input data file %s\n", fileName
);
327 // Is the file large enough to contain a trace
328 if(lTDFStat
.st_size
<
329 (off_t
)(sizeof(struct ltt_block_start_header
)
330 + sizeof(struct ltt_trace_header_any
))){
331 g_print("The input data file %s does not contain a trace\n", fileName
);
335 /* Temporarily map the buffer start header to get trace information */
336 /* Multiple of pages aligned head */
337 tf
->buffer
.head
= mmap(0,
338 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)
339 + sizeof(struct ltt_trace_header_any
)), PROT_READ
,
340 MAP_PRIVATE
, tf
->fd
, 0);
341 if(tf
->buffer
.head
== MAP_FAILED
) {
342 perror("Error in allocating memory for buffer of tracefile");
345 g_assert( ( (guint
)tf
->buffer
.head
&(8-1) ) == 0); // make sure it's aligned.
347 header
= (struct ltt_block_start_header
*)tf
->buffer
.head
;
349 if(parse_trace_header(header
->trace
, tf
, NULL
)) {
350 g_warning("parse_trace_header error");
354 //store the size of the file
355 tf
->file_size
= lTDFStat
.st_size
;
356 tf
->buf_size
= ltt_get_uint32(LTT_GET_BO(tf
), &header
->buf_size
);
357 tf
->num_blocks
= tf
->file_size
/ tf
->buf_size
;
359 if(munmap(tf
->buffer
.head
,
360 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)
361 + sizeof(struct ltt_trace_header_any
)))) {
362 g_warning("unmap size : %u\n",
363 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)
364 + sizeof(struct ltt_trace_header_any
)));
365 perror("munmap error");
368 tf
->buffer
.head
= NULL
;
370 //read the first block
371 if(map_block(tf
,0)) {
372 perror("Cannot map block for tracefile");
380 if(munmap(tf
->buffer
.head
,
381 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)
382 + sizeof(struct ltt_trace_header_any
)))) {
383 g_warning("unmap size : %u\n",
384 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)
385 + sizeof(struct ltt_trace_header_any
)));
386 perror("munmap error");
395 LttTrace
*ltt_tracefile_get_trace(LttTracefile
*tf
)
401 /*****************************************************************************
402 *Open control and per cpu tracefiles
403 ****************************************************************************/
405 void ltt_tracefile_open_cpu(LttTrace
*t
, gchar
* tracefile_name
)
408 tf
= ltt_tracefile_open(t
,tracefile_name
);
410 t
->per_cpu_tracefile_number
++;
411 g_ptr_array_add(t
->per_cpu_tracefiles
, tf
);
414 gint
ltt_tracefile_open_control(LttTrace
*t
, gchar
* control_name
)
423 tf
= ltt_tracefile_open(t
,control_name
);
425 g_warning("ltt_tracefile_open_control : bad file descriptor");
428 t
->control_tracefile_number
++;
429 g_ptr_array_add(t
->control_tracefiles
,tf
);
431 //parse facilities tracefile to get base_id
432 if(strcmp(&control_name
[strlen(control_name
)-10],"facilities") ==0){
434 if(!ltt_tracefile_read(tf
,&ev
)) return 0; // end of file
436 if(ev
.event_id
== TRACE_FACILITY_LOAD
){
438 fLoad
.name
= (gchar
*)pos
;
439 fLoad
.checksum
= *(LttChecksum
*)(pos
+ strlen(fLoad
.name
));
440 fLoad
.base_code
= *(guint32
*)(pos
+ strlen(fLoad
.name
) + sizeof(LttChecksum
));
442 for(i
=0;i
<t
->facility_number
;i
++){
443 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
,i
);
444 if(strcmp(f
->name
,fLoad
.name
)==0 && fLoad
.checksum
==f
->checksum
){
445 f
->base_id
= fLoad
.base_code
;
449 if(i
==t
->facility_number
) {
450 g_warning("Facility: %s, checksum: %u is not found",
451 fLoad
.name
,(unsigned int)fLoad
.checksum
);
454 }else if(ev
.event_id
== TRACE_BLOCK_START
){
456 }else if(ev
.event_id
== TRACE_BLOCK_END
){
459 g_warning("Not valid facilities trace file");
468 /*****************************************************************************
470 * ltt_tracefile_close: close a trace file,
472 * t : tracefile which will be closed
473 ****************************************************************************/
475 void ltt_tracefile_close(LttTracefile
*t
)
477 int page_size
= getpagesize();
479 if(t
->buffer
.head
!= NULL
)
480 if(munmap(t
->buffer
.head
, PAGE_ALIGN(t
->buf_size
))) {
481 g_warning("unmap size : %u\n",
482 PAGE_ALIGN(t
->buf_size
));
483 perror("munmap error");
491 /*****************************************************************************
492 *Get system information
493 ****************************************************************************/
495 gint
getSystemInfo(LttSystemDescription
* des
, gchar
* pathname
)
502 GMarkupParseContext
* context
;
503 GError
* error
= NULL
;
504 GMarkupParser markup_parser
=
506 parser_start_element
,
509 NULL
, /* passthrough */
513 fd
= g_open(pathname
, O_RDONLY
, 0);
515 g_warning("Can not open file : %s\n", pathname
);
519 iochan
= g_io_channel_unix_new(fd
);
521 context
= g_markup_parse_context_new(&markup_parser
, 0, des
,NULL
);
523 //while(fgets(buf,DIR_NAME_SIZE, fp) != NULL){
524 while(g_io_channel_read_line(iochan
, &buf
, &length
, NULL
, &error
)
525 != G_IO_STATUS_EOF
) {
528 g_warning("Can not read xml file: \n%s\n", error
->message
);
531 if(!g_markup_parse_context_parse(context
, buf
, length
, &error
)){
533 g_warning("Can not parse xml file: \n%s\n", error
->message
);
536 g_markup_parse_context_free(context
);
538 g_io_channel_shutdown(iochan
, FALSE
, &error
); /* No flush */
540 g_warning("Can not close file: \n%s\n", error
->message
);
548 g_markup_parse_context_free(context
);
550 g_io_channel_shutdown(iochan
, FALSE
, &error
); /* No flush */
552 g_warning("Can not close file: \n%s\n", error
->message
);
563 /*****************************************************************************
564 *The following functions get facility/tracefile information
565 ****************************************************************************/
567 gint
getFacilityInfo(LttTrace
*t
, gchar
* eventdefs
)
574 gchar fullname
[DIR_NAME_SIZE
];
575 GError
* error
= NULL
;
577 dir
= g_dir_open(eventdefs
, 0, &error
);
580 g_warning("Can not open directory: %s, %s\n", eventdefs
, error
->message
);
585 while((name
= g_dir_read_name(dir
)) != NULL
){
586 if(!g_pattern_match_simple("*.xml", name
)) continue;
587 strcpy(fullname
,eventdefs
);
588 strcat(fullname
,name
);
589 ltt_facility_open(t
,fullname
);
593 for(j
=0;j
<t
->facility_number
;j
++){
594 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, j
);
595 for(i
=0; i
<f
->event_number
; i
++){
597 setFieldsOffset(NULL
, et
, NULL
, t
);
604 /*****************************************************************************
605 *A trace is specified as a pathname to the directory containing all the
606 *associated data (control tracefiles, per cpu tracefiles, event
609 *When a trace is closed, all the associated facilities, types and fields
610 *are released as well.
614 /****************************************************************************
615 * get_absolute_pathname
617 * return the unique pathname in the system
619 * MD : Fixed this function so it uses realpath, dealing well with
620 * forgotten cases (.. were not used correctly before).
622 ****************************************************************************/
623 void get_absolute_pathname(const gchar
*pathname
, gchar
* abs_pathname
)
625 abs_pathname
[0] = '\0';
627 if ( realpath (pathname
, abs_pathname
) != NULL
)
631 /* error, return the original path unmodified */
632 strcpy(abs_pathname
, pathname
);
638 /* Search for something like : .*_.*
640 * The left side is the name, the right side is the number.
643 int get_tracefile_name_number(const gchar
*raw_name
,
647 guint raw_name_len
= strlen(raw_name
);
648 gchar char_name
[PATH_MAX
];
654 for(i
=raw_name_len
-1;i
>=0;i
--) {
655 if(raw_name
[i
] == '_') break;
657 if(i
==0) /* Either not found or name length is 0 */
661 cpu_num
= strtol(raw_name
+underscore_pos
+1, &endptr
, 10);
663 if(endptr
== raw_name
+underscore_pos
+1)
664 return -1; /* No digit */
665 if(cpu_num
== LONG_MIN
|| cpu_num
== LONG_MAX
)
666 return -1; /* underflow / overflow */
668 strncpy(char_name
, raw_name
, underscore_pos
);
670 char_name
[underscore_pos
] = '\0';
672 *name
= g_quark_from_string(char_name
);
679 GData
**ltt_trace_get_tracefiles_groups(LttTrace
*trace
)
681 return &trace
->tracefiles
;
685 void compute_tracefile_group(GQuark key_id
,
687 struct compute_tracefile_group_args
*args
)
692 for(i
=0; i
<group
->len
; i
++) {
693 tf
= &g_array_index (group
, LttTracefile
, i
);
695 args
->func(tf
, args
->func_args
);
700 void ltt_tracefile_group_destroy(gpointer data
)
702 GArray
*group
= (GArray
*)data
;
706 for(i
=0; i
<group
->len
; i
++) {
707 tf
= &g_array_index (group
, LttTracefile
, i
);
709 ltt_tracefile_close(tf
);
711 g_array_free(group
, TRUE
);
714 gboolean
ltt_tracefile_group_has_cpu_online(gpointer data
)
716 GArray
*group
= (GArray
*)data
;
720 for(i
=0; i
<group
->len
; i
++) {
721 tf
= &g_array_index (group
, LttTracefile
, i
);
722 if(tf
->cpu_online
) return 1;
728 /* Open each tracefile under a specific directory. Put them in a
729 * GData : permits to access them using their tracefile group pathname.
730 * i.e. access control/modules tracefile group by index :
733 * relative path is the path relative to the trace root
734 * root path is the full path
736 * A tracefile group is simply an array where all the per cpu tracefiles sits.
739 static int open_tracefiles(LttTrace
*trace
, gchar
*root_path
,
740 gchar
*relative_path
)
742 DIR *dir
= opendir(root_path
);
743 struct dirent
*entry
;
744 struct stat stat_buf
;
747 gchar path
[PATH_MAX
];
752 gchar rel_path
[PATH_MAX
];
761 strncpy(path
, root_path
, PATH_MAX
-1);
762 path_len
= strlen(path
);
763 path
[path_len
] = '/';
765 path_ptr
= path
+ path_len
;
767 strncpy(rel_path
, relative_path
, PATH_MAX
-1);
768 rel_path_len
= strlen(rel_path
);
769 rel_path
[rel_path_len
] = '/';
771 rel_path_ptr
= rel_path
+ rel_path_len
;
773 while((entry
= readdir(dir
)) != NULL
) {
775 if(entry
->d_name
[0] == '.') continue;
777 strncpy(path_ptr
, entry
->d_name
, PATH_MAX
- path_len
);
778 strncpy(rel_path_ptr
, entry
->d_name
, PATH_MAX
- rel_path_len
);
780 ret
= stat(path
, &stat_buf
);
786 g_debug("Tracefile file or directory : %s\n", path
);
788 if(strcmp(rel_path
, "/eventdefs") == 0) continue;
790 if(S_ISDIR(stat_buf
.st_mode
)) {
792 g_debug("Entering subdirectory...\n");
793 ret
= open_tracefiles(trace
, path
, rel_path
);
794 if(ret
< 0) continue;
795 } else if(S_ISREG(stat_buf
.st_mode
)) {
800 if(get_tracefile_name_number(rel_path
, &name
, &num
))
801 continue; /* invalid name */
803 g_debug("Opening file.\n");
804 if(ltt_tracefile_open(trace
, path
, &tmp_tf
)) {
805 g_info("Error opening tracefile %s", path
);
807 continue; /* error opening the tracefile : bad magic number ? */
810 g_debug("Tracefile name is %s and number is %u",
811 g_quark_to_string(name
), num
);
813 tmp_tf
.cpu_online
= 1;
814 tmp_tf
.cpu_num
= num
;
817 group
= g_datalist_id_get_data(&trace
->tracefiles
, name
);
819 /* Elements are automatically cleared when the array is allocated.
820 * It makes the cpu_online variable set to 0 : cpu offline, by default.
822 group
= g_array_sized_new (FALSE
, TRUE
, sizeof(LttTracefile
), 10);
823 g_datalist_id_set_data_full(&trace
->tracefiles
, name
,
824 group
, ltt_tracefile_group_destroy
);
827 /* Add the per cpu tracefile to the named group */
828 unsigned int old_len
= group
->len
;
830 group
= g_array_set_size(group
, num
+1);
831 g_array_index (group
, LttTracefile
, num
) = tmp_tf
;
841 /* ltt_get_facility_description
843 * Opens the file corresponding to the requested facility (identified by fac_id
846 * The name searched is : %trace root%/eventdefs/facname_checksum.xml
848 * Returns 0 on success, or 1 on failure.
851 static int ltt_get_facility_description(LttFacility
*f
,
853 LttTracefile
*fac_tf
)
855 char desc_file_name
[PATH_MAX
];
860 text
= g_quark_to_string(t
->pathname
);
861 textlen
= strlen(text
);
863 if(textlen
>= PATH_MAX
) goto name_error
;
864 strcpy(desc_file_name
, text
);
866 text
= "/eventdefs/";
867 textlen
+=strlen(text
);
868 if(textlen
>= PATH_MAX
) goto name_error
;
869 strcat(desc_file_name
, text
);
871 text
= g_quark_to_string(f
->name
);
872 textlen
+=strlen(text
);
873 if(textlen
>= PATH_MAX
) goto name_error
;
874 strcat(desc_file_name
, text
);
877 textlen
+=strlen(text
);
878 if(textlen
>= PATH_MAX
) goto name_error
;
879 strcat(desc_file_name
, text
);
881 err
= snprintf(desc_file_name
+textlen
, PATH_MAX
-textlen
-1,
883 if(err
< 0) goto name_error
;
885 textlen
=strlen(desc_file_name
);
889 textlen
+=strlen(text
);
890 if(textlen
>= PATH_MAX
) goto name_error
;
891 strcat(desc_file_name
, text
);
893 err
= ltt_facility_open(f
, t
, desc_file_name
);
894 if(err
) goto facility_error
;
903 static void ltt_fac_ids_destroy(gpointer data
)
905 GArray
*fac_ids
= (GArray
*)data
;
907 g_array_free(fac_ids
, TRUE
);
911 /* Presumes the tracefile is already seeked at the beginning. It makes sense,
912 * because it must be done just after the opening */
913 static int ltt_process_facility_tracefile(LttTracefile
*tf
)
922 err
= ltt_tracefile_read_seek(tf
);
923 if(err
== EPERM
) goto seek_error
;
924 else if(err
== ERANGE
) break; /* End of tracefile */
926 err
= ltt_tracefile_read_update_event(tf
);
927 if(err
) goto update_error
;
929 /* We are on a facility load/or facility unload/ or heartbeat event */
931 * * facility 0 is hardcoded : this is the core facility. It will be shown
932 * in the facility array though, and is shown as "loaded builtin" in the
934 * It contains event :
936 * 1 : facility unload
937 * 2 : state dump facility load
940 if(tf
->event
.facility_id
!= LTT_FACILITY_CORE
) {
941 /* Should only contain core facility */
942 g_warning("Error in processing facility file %s, "
943 "should not contain facility id %u.", g_quark_to_string(tf
->name
),
944 tf
->event
.facility_id
);
949 struct LttFacilityLoad
*fac_load_data
;
950 struct LttStateDumpFacilityLoad
*fac_state_dump_load_data
;
954 switch((enum ltt_core_events
)tf
->event
.event_id
) {
955 case LTT_EVENT_FACILITY_LOAD
:
956 fac_name
= (char*)(tf
->event
.data
);
957 g_debug("Doing LTT_EVENT_FACILITY_LOAD of facility %s",
960 (struct LttFacilityLoad
*)
961 (tf
->event
.data
+ strlen(fac_name
) + 1);
962 fac
= &g_array_index (tf
->trace
->facilities_by_num
, LttFacility
,
963 ltt_get_uint32(LTT_GET_BO(tf
), &fac_load_data
->id
));
964 /* facility may already exist if trace is paused/unpaused */
965 if(fac
->exists
) continue;
966 fac
->name
= g_quark_from_string(fac_name
);
967 fac
->checksum
= ltt_get_uint32(LTT_GET_BO(tf
),
968 &fac_load_data
->checksum
);
969 fac
->id
= ltt_get_uint32(LTT_GET_BO(tf
), &fac_load_data
->id
);
970 fac
->pointer_size
= ltt_get_uint32(LTT_GET_BO(tf
),
971 &fac_load_data
->pointer_size
);
972 fac
->int_size
= ltt_get_uint32(LTT_GET_BO(tf
),
973 &fac_load_data
->int_size
);
974 fac
->long_size
= ltt_get_uint32(LTT_GET_BO(tf
),
975 &fac_load_data
->long_size
);
976 fac
->size_t_size
= ltt_get_uint32(LTT_GET_BO(tf
),
977 &fac_load_data
->size_t_size
);
978 fac
->alignment
= ltt_get_uint32(LTT_GET_BO(tf
),
979 &fac_load_data
->has_alignment
);
981 if(ltt_get_facility_description(fac
, tf
->trace
, tf
))
982 continue; /* error opening description */
984 fac
->trace
= tf
->trace
;
986 /* Preset the field offsets */
987 for(i
=0; i
<fac
->events
->len
; i
++){
988 et
= &g_array_index(fac
->events
, LttEventType
, i
);
989 precompute_offsets(tf
, et
);
994 fac_ids
= g_datalist_id_get_data(&tf
->trace
->facilities_by_name
,
996 if(fac_ids
== NULL
) {
997 fac_ids
= g_array_sized_new (FALSE
, TRUE
, sizeof(guint
), 1);
998 g_datalist_id_set_data_full(&tf
->trace
->facilities_by_name
,
1000 fac_ids
, ltt_fac_ids_destroy
);
1002 g_array_append_val(fac_ids
, fac
->id
);
1005 case LTT_EVENT_FACILITY_UNLOAD
:
1006 g_debug("Doing LTT_EVENT_FACILITY_UNLOAD");
1007 /* We don't care about unload : facilities ID are valid for the whole
1008 * trace. They simply won't be used after the unload. */
1010 case LTT_EVENT_STATE_DUMP_FACILITY_LOAD
:
1011 fac_name
= (char*)(tf
->event
.data
);
1012 g_debug("Doing LTT_EVENT_STATE_DUMP_FACILITY_LOAD of facility %s",
1014 fac_state_dump_load_data
=
1015 (struct LttStateDumpFacilityLoad
*)
1016 (tf
->event
.data
+ strlen(fac_name
) + 1);
1017 fac
= &g_array_index (tf
->trace
->facilities_by_num
, LttFacility
,
1018 ltt_get_uint32(LTT_GET_BO(tf
), &fac_state_dump_load_data
->id
));
1019 /* facility may already exist if trace is paused/unpaused */
1020 if(fac
->exists
) continue;
1021 fac
->name
= g_quark_from_string(fac_name
);
1022 fac
->checksum
= ltt_get_uint32(LTT_GET_BO(tf
),
1023 &fac_state_dump_load_data
->checksum
);
1024 fac
->id
= ltt_get_uint32(LTT_GET_BO(tf
),
1025 &fac_state_dump_load_data
->id
);
1026 fac
->pointer_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1027 &fac_state_dump_load_data
->pointer_size
);
1028 fac
->int_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1029 &fac_state_dump_load_data
->int_size
);
1030 fac
->long_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1031 &fac_state_dump_load_data
->long_size
);
1032 fac
->size_t_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1033 &fac_state_dump_load_data
->size_t_size
);
1034 fac
->alignment
= ltt_get_uint32(LTT_GET_BO(tf
),
1035 &fac_state_dump_load_data
->has_alignment
);
1036 if(ltt_get_facility_description(fac
, tf
->trace
, tf
))
1037 continue; /* error opening description */
1039 fac
->trace
= tf
->trace
;
1041 /* Preset the field offsets */
1042 for(i
=0; i
<fac
->events
->len
; i
++){
1043 et
= &g_array_index(fac
->events
, LttEventType
, i
);
1044 precompute_offsets(tf
, et
);
1049 fac_ids
= g_datalist_id_get_data(&tf
->trace
->facilities_by_name
,
1051 if(fac_ids
== NULL
) {
1052 fac_ids
= g_array_sized_new (FALSE
, TRUE
, sizeof(guint
), 1);
1053 g_datalist_id_set_data_full(&tf
->trace
->facilities_by_name
,
1055 fac_ids
, ltt_fac_ids_destroy
);
1057 g_array_append_val(fac_ids
, fac
->id
);
1060 case LTT_EVENT_HEARTBEAT
:
1063 g_warning("Error in processing facility file %s, "
1064 "unknown event id %hhu in core facility.",
1065 g_quark_to_string(tf
->name
),
1066 tf
->event
.event_id
);
1068 goto event_id_error
;
1074 /* Error handling */
1079 g_warning("An error occured in facility tracefile parsing");
1084 LttTrace
*ltt_trace_open(const gchar
*pathname
)
1086 gchar abs_path
[PATH_MAX
];
1091 struct ltt_block_start_header
*header
;
1093 struct dirent
*entry
;
1094 guint control_found
= 0;
1095 guint eventdefs_found
= 0;
1096 struct stat stat_buf
;
1097 gchar path
[PATH_MAX
];
1099 t
= g_new(LttTrace
, 1);
1100 if(!t
) goto alloc_error
;
1102 get_absolute_pathname(pathname
, abs_path
);
1103 t
->pathname
= g_quark_from_string(abs_path
);
1105 g_datalist_init(&t
->tracefiles
);
1107 /* Test to see if it looks like a trace */
1108 dir
= opendir(abs_path
);
1113 while((entry
= readdir(dir
)) != NULL
) {
1114 strcpy(path
, abs_path
);
1116 strcat(path
, entry
->d_name
);
1117 ret
= stat(path
, &stat_buf
);
1122 if(S_ISDIR(stat_buf
.st_mode
)) {
1123 if(strcmp(entry
->d_name
, "control") == 0) {
1126 if(strcmp(entry
->d_name
, "eventdefs") == 0) {
1127 eventdefs_found
= 1;
1133 if(!control_found
|| !eventdefs_found
) goto find_error
;
1135 /* Open all the tracefiles */
1136 if(open_tracefiles(t
, abs_path
, "")) {
1137 g_warning("Error opening tracefile %s", abs_path
);
1141 /* Prepare the facilities containers : array and mapping */
1142 /* Array is zeroed : the "exists" field is set to false by default */
1143 t
->facilities_by_num
= g_array_sized_new (FALSE
,
1144 TRUE
, sizeof(LttFacility
),
1146 t
->facilities_by_num
= g_array_set_size(t
->facilities_by_num
, NUM_FACILITIES
);
1148 g_datalist_init(&t
->facilities_by_name
);
1150 /* Parse each trace control/facilitiesN files : get runtime fac. info */
1151 group
= g_datalist_id_get_data(&t
->tracefiles
, LTT_TRACEFILE_NAME_FACILITIES
);
1153 g_error("Trace %s has no facility tracefile", abs_path
);
1155 goto facilities_error
;
1158 /* Get the trace information for the control/facility 0 tracefile */
1159 g_assert(group
->len
> 0);
1160 tf
= &g_array_index (group
, LttTracefile
, 0);
1161 header
= (struct ltt_block_start_header
*)tf
->buffer
.head
;
1162 g_assert(parse_trace_header(header
->trace
,
1165 t
->num_cpu
= group
->len
;
1167 for(i
=0; i
<group
->len
; i
++) {
1168 tf
= &g_array_index (group
, LttTracefile
, i
);
1169 if(ltt_process_facility_tracefile(tf
))
1170 goto facilities_error
;
1175 /* Error handling */
1177 g_datalist_clear(&t
->facilities_by_name
);
1178 g_array_free(t
->facilities_by_num
, TRUE
);
1180 g_datalist_clear(&t
->tracefiles
);
1188 GQuark
ltt_trace_name(const LttTrace
*t
)
1194 /******************************************************************************
1195 * When we copy a trace, we want all the opening actions to happen again :
1196 * the trace will be reopened and totally independant from the original.
1197 * That's why we call ltt_trace_open.
1198 *****************************************************************************/
1199 LttTrace
*ltt_trace_copy(LttTrace
*self
)
1201 return ltt_trace_open(g_quark_to_string(self
->pathname
));
1204 void ltt_trace_close(LttTrace
*t
)
1209 for(i
=0; i
<t
->facilities_by_num
->len
; i
++) {
1210 fac
= &g_array_index (t
->facilities_by_num
, LttFacility
, i
);
1212 ltt_facility_close(fac
);
1215 g_datalist_clear(&t
->facilities_by_name
);
1216 g_array_free(t
->facilities_by_num
, TRUE
);
1217 g_datalist_clear(&t
->tracefiles
);
1222 /*****************************************************************************
1223 *Get the system description of the trace
1224 ****************************************************************************/
1226 LttFacility
*ltt_trace_facility_by_id(LttTrace
*t
, guint8 id
)
1228 g_assert(id
< t
->facilities_by_num
->len
);
1229 return &g_array_index(t
->facilities_by_num
, LttFacility
, id
);
1232 /* ltt_trace_facility_get_by_name
1234 * Returns the GArray of facility indexes. All the fac_ids that matches the
1235 * requested facility name.
1237 * If name is not found, returns NULL.
1239 GArray
*ltt_trace_facility_get_by_name(LttTrace
*t
, GQuark name
)
1241 return g_datalist_id_get_data(&t
->facilities_by_name
, name
);
1244 /*****************************************************************************
1245 * Functions to discover all the event types in the trace
1246 ****************************************************************************/
1249 unsigned ltt_trace_eventtype_number(LttTrace
*t
)
1253 unsigned int num
= t
->facility_number
;
1257 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, i
);
1258 count
+= f
->event_number
;
1265 //use an iteration on all the trace facilities, and inside iteration on all the
1266 //event types in each facilities instead.
1267 LttEventType
*ltt_trace_eventtype_get(LttTrace
*t
, unsigned evId
)
1269 LttEventType
*event_type
;
1272 f
= ltt_trace_facility_by_id(t
,evId
);
1274 if(unlikely(!f
)) event_type
= NULL
;
1275 else event_type
= f
->events
[evId
- f
->base_id
];
1282 /*****************************************************************************
1283 * ltt_trace_find_tracefile
1285 * Find a tracefile by name and index in the group.
1287 * Returns a pointer to the tracefiles, else NULL.
1288 ****************************************************************************/
1290 LttTracefile
*ltt_trace_find_tracefile(LttTrace
*t
, const gchar
*name
)
1295 /*****************************************************************************
1296 * Get the start time and end time of the trace
1297 ****************************************************************************/
1299 static void ltt_tracefile_time_span_get(LttTracefile
*tf
,
1300 LttTime
*start
, LttTime
*end
)
1304 err
= map_block(tf
, 0);
1306 g_error("Can not map block");
1307 *start
= ltt_time_infinite
;
1309 *start
= tf
->buffer
.begin
.timestamp
;
1311 err
= map_block(tf
, tf
->num_blocks
- 1); /* Last block */
1313 g_error("Can not map block");
1314 *end
= ltt_time_zero
;
1316 *end
= tf
->buffer
.end
.timestamp
;
1319 struct tracefile_time_span_get_args
{
1325 static void group_time_span_get(GQuark name
, gpointer data
, gpointer user_data
)
1327 struct tracefile_time_span_get_args
*args
=
1328 (struct tracefile_time_span_get_args
*)user_data
;
1330 GArray
*group
= (GArray
*)data
;
1336 for(i
=0; i
<group
->len
; i
++) {
1337 tf
= &g_array_index (group
, LttTracefile
, i
);
1338 if(tf
->cpu_online
) {
1339 ltt_tracefile_time_span_get(tf
, &tmp_start
, &tmp_end
);
1340 if(ltt_time_compare(*args
->start
, tmp_start
)>0) *args
->start
= tmp_start
;
1341 if(ltt_time_compare(*args
->end
, tmp_end
)<0) *args
->end
= tmp_end
;
1346 void ltt_trace_time_span_get(LttTrace
*t
, LttTime
*start
, LttTime
*end
)
1348 LttTime min_start
= ltt_time_infinite
;
1349 LttTime max_end
= ltt_time_zero
;
1350 struct tracefile_time_span_get_args args
= { t
, &min_start
, &max_end
};
1352 g_datalist_foreach(&t
->tracefiles
, &group_time_span_get
, &args
);
1354 if(start
!= NULL
) *start
= min_start
;
1355 if(end
!= NULL
) *end
= max_end
;
1360 /*****************************************************************************
1361 *Get the name of a tracefile
1362 ****************************************************************************/
1364 GQuark
ltt_tracefile_name(const LttTracefile
*tf
)
1369 GQuark
ltt_tracefile_long_name(const LttTracefile
*tf
)
1371 return tf
->long_name
;
1376 guint
ltt_tracefile_num(LttTracefile
*tf
)
1381 /*****************************************************************************
1382 * Get the number of blocks in the tracefile
1383 ****************************************************************************/
1385 guint
ltt_tracefile_block_number(LttTracefile
*tf
)
1387 return tf
->num_blocks
;
1391 /* Seek to the first event in a tracefile that has a time equal or greater than
1392 * the time passed in parameter.
1394 * If the time parameter is outside the tracefile time span, seek to the first
1395 * event or if after, return ERANGE.
1397 * If the time parameter is before the first event, we have to seek specially to
1400 * If the time is after the end of the trace, return ERANGE.
1402 * Do a binary search to find the right block, then a sequential search in the
1403 * block to find the event.
1405 * In the special case where the time requested fits inside a block that has no
1406 * event corresponding to the requested time, the first event of the next block
1409 * IMPORTANT NOTE : // FIXME everywhere...
1411 * You MUST NOT do a ltt_tracefile_read right after a ltt_tracefile_seek_time :
1412 * you will jump over an event if you do.
1414 * Return value : 0 : no error, the tf->event can be used
1415 * ERANGE : time if after the last event of the trace
1416 * otherwise : this is an error.
1420 int ltt_tracefile_seek_time(LttTracefile
*tf
, LttTime time
)
1424 unsigned int block_num
, high
, low
;
1426 /* seek at the beginning of trace */
1427 err
= map_block(tf
, 0); /* First block */
1429 g_error("Can not map block");
1433 /* If the time is lower or equal the beginning of the trace,
1434 * go to the first event. */
1435 if(ltt_time_compare(time
, tf
->buffer
.begin
.timestamp
) <= 0) {
1436 ret
= ltt_tracefile_read(tf
);
1437 if(ret
== ERANGE
) goto range
;
1438 else if (ret
) goto fail
;
1439 goto found
; /* There is either no event in the trace or the event points
1440 to the first event in the trace */
1443 err
= map_block(tf
, tf
->num_blocks
- 1); /* Last block */
1445 g_error("Can not map block");
1449 /* If the time is after the end of the trace, return ERANGE. */
1450 if(ltt_time_compare(time
, tf
->buffer
.end
.timestamp
) > 0) {
1454 /* Binary search the block */
1455 high
= tf
->num_blocks
- 1;
1459 block_num
= ((high
-low
) / 2) + low
;
1461 err
= map_block(tf
, block_num
);
1463 g_error("Can not map block");
1467 /* We cannot divide anymore : this is what would happen if the time
1468 * requested was exactly between two consecutive buffers'end and start
1469 * timestamps. This is also what would happend if we didn't deal with out
1470 * of span cases prior in this function. */
1471 /* The event is right in the buffer!
1472 * (or in the next buffer first event) */
1474 ret
= ltt_tracefile_read(tf
);
1475 if(ret
== ERANGE
) goto range
; /* ERANGE or EPERM */
1476 else if(ret
) goto fail
;
1478 if(ltt_time_compare(time
, tf
->event
.event_time
) <= 0)
1482 } else if(ltt_time_compare(time
, tf
->buffer
.begin
.timestamp
) < 0) {
1483 /* go to lower part */
1484 high
= block_num
- 1;
1485 } else if(ltt_time_compare(time
, tf
->buffer
.end
.timestamp
) > 0) {
1486 /* go to higher part */
1487 low
= block_num
+ 1;
1488 } else {/* The event is right in the buffer!
1489 (or in the next buffer first event) */
1491 ret
= ltt_tracefile_read(tf
);
1492 if(ret
== ERANGE
) goto range
; /* ERANGE or EPERM */
1493 else if(ret
) goto fail
;
1495 if(ltt_time_compare(time
, tf
->event
.event_time
) <= 0)
1507 /* Error handling */
1509 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1510 g_quark_to_string(tf
->name
));
1515 int ltt_tracefile_seek_position(LttTracefile
*tf
, const LttEventPosition
*ep
) {
1519 if(ep
->tracefile
!= tf
) {
1523 err
= map_block(tf
, ep
->block
);
1525 g_error("Can not map block");
1529 tf
->event
.offset
= ep
->offset
;
1531 err
= ltt_tracefile_read_update_event(tf
);
1533 err
= ltt_tracefile_read_op(tf
);
1539 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1540 g_quark_to_string(tf
->name
));
1544 /* Calculate the real event time based on the buffer boundaries */
1545 LttTime
ltt_interpolate_time(LttTracefile
*tf
, LttEvent
*event
)
1549 g_assert(tf
->trace
->has_tsc
);
1551 // time = ltt_time_from_uint64(
1552 // cycles_2_ns(tf, (guint64)(tf->buffer.tsc - tf->buffer.begin.cycle_count)));
1553 time
= ltt_time_from_uint64(
1554 (double)(tf
->buffer
.tsc
- tf
->trace
->start_tsc
) * 1000000.0
1555 / (double)tf
->trace
->start_freq
);
1556 //time = ltt_time_add(tf->buffer.begin.timestamp, time);
1557 time
= ltt_time_add(tf
->trace
->start_time_from_tsc
, time
);
1563 /* Get the current event of the tracefile : valid until the next read */
1564 LttEvent
*ltt_tracefile_get_event(LttTracefile
*tf
)
1571 /*****************************************************************************
1573 * ltt_tracefile_read : Read the next event in the tracefile
1578 * Returns 0 if an event can be used in tf->event.
1579 * Returns ERANGE on end of trace. The event in tf->event still can be used
1580 * (if the last block was not empty).
1581 * Returns EPERM on error.
1583 * This function does make the tracefile event structure point to the event
1584 * currently pointed to by the tf->event.
1586 * Note : you must call a ltt_tracefile_seek to the beginning of the trace to
1587 * reinitialize it after an error if you want results to be coherent.
1588 * It would be the case if a end of trace last buffer has no event : the end
1589 * of trace wouldn't be returned, but an error.
1590 * We make the assumption there is at least one event per buffer.
1591 ****************************************************************************/
1593 int ltt_tracefile_read(LttTracefile
*tf
)
1597 err
= ltt_tracefile_read_seek(tf
);
1599 err
= ltt_tracefile_read_update_event(tf
);
1601 err
= ltt_tracefile_read_op(tf
);
1607 int ltt_tracefile_read_seek(LttTracefile
*tf
)
1611 /* Get next buffer until we finally have an event, or end of trace */
1613 err
= ltt_seek_next_event(tf
);
1614 if(unlikely(err
== ENOPROTOOPT
)) {
1618 /* Are we at the end of the buffer ? */
1620 if(unlikely(tf
->buffer
.index
== tf
->num_blocks
-1)){ /* end of trace ? */
1623 /* get next block */
1624 err
= map_block(tf
, tf
->buffer
.index
+ 1);
1626 g_error("Can not map block");
1630 } else break; /* We found an event ! */
1637 /* do specific operation on events */
1638 int ltt_tracefile_read_op(LttTracefile
*tf
)
1644 /* do event specific operation */
1646 /* do something if its an heartbeat event : increment the heartbeat count */
1647 //if(event->facility_id == LTT_FACILITY_CORE)
1648 // if(event->event_id == LTT_EVENT_HEARTBEAT)
1649 // tf->cur_heart_beat_number++;
1655 /* same as ltt_tracefile_read, but does not seek to the next event nor call
1656 * event specific operation. */
1657 int ltt_tracefile_read_update_event(LttTracefile
*tf
)
1663 pos
= tf
->buffer
.head
+ event
->offset
;
1665 /* Read event header */
1667 /* Align the head */
1668 pos
+= ltt_align((size_t)pos
, tf
->trace
->arch_size
, tf
->has_alignment
);
1670 if(tf
->trace
->has_tsc
) {
1671 if(tf
->trace
->has_heartbeat
) {
1672 event
->time
.timestamp
= ltt_get_uint32(LTT_GET_BO(tf
),
1674 /* 32 bits -> 64 bits tsc */
1675 /* note : still works for seek and non seek cases. */
1676 if(event
->time
.timestamp
< (0xFFFFFFFFULL
&tf
->buffer
.tsc
)) {
1677 tf
->buffer
.tsc
= ((tf
->buffer
.tsc
&0xFFFFFFFF00000000ULL
)
1679 | (guint64
)event
->time
.timestamp
;
1680 event
->tsc
= tf
->buffer
.tsc
;
1683 tf
->buffer
.tsc
= (tf
->buffer
.tsc
&0xFFFFFFFF00000000ULL
)
1684 | (guint64
)event
->time
.timestamp
;
1685 event
->tsc
= tf
->buffer
.tsc
;
1687 pos
+= sizeof(guint32
);
1689 event
->tsc
= ltt_get_uint64(LTT_GET_BO(tf
), pos
);
1690 tf
->buffer
.tsc
= event
->tsc
;
1691 pos
+= sizeof(guint64
);
1694 event
->event_time
= ltt_interpolate_time(tf
, event
);
1696 event
->time
.delta
.tv_sec
= 0;
1697 event
->time
.delta
.tv_nsec
= ltt_get_uint32(LTT_GET_BO(tf
),
1698 pos
) * NSEC_PER_USEC
;
1700 event
->tsc
= tf
->buffer
.tsc
;
1702 event
->event_time
= ltt_time_add(tf
->buffer
.begin
.timestamp
,
1704 pos
+= sizeof(guint32
);
1707 event
->facility_id
= *(guint8
*)pos
;
1708 pos
+= sizeof(guint8
);
1710 event
->event_id
= *(guint8
*)pos
;
1711 pos
+= sizeof(guint8
);
1713 event
->event_size
= ltt_get_uint16(LTT_GET_BO(tf
), pos
);
1714 pos
+= sizeof(guint16
);
1716 /* Align the head */
1717 pos
+= ltt_align((size_t)pos
, tf
->trace
->arch_size
, tf
->has_alignment
);
1721 /* get the data size and update the event fields with the current
1723 ltt_update_event_size(tf
);
1729 /****************************************************************************
1731 * map_block : map a block from the file
1733 * lttdes : ltt trace file
1734 * whichBlock : the block which will be read
1737 * EINVAL : lseek fail
1738 * EIO : can not read from the file
1739 ****************************************************************************/
1741 static gint
map_block(LttTracefile
* tf
, guint block_num
)
1743 int page_size
= getpagesize();
1744 struct ltt_block_start_header
*header
;
1746 g_assert(block_num
< tf
->num_blocks
);
1748 if(tf
->buffer
.head
!= NULL
) {
1749 if(munmap(tf
->buffer
.head
, PAGE_ALIGN(tf
->buf_size
))) {
1750 g_warning("unmap size : %u\n",
1751 PAGE_ALIGN(tf
->buf_size
));
1752 perror("munmap error");
1758 /* Multiple of pages aligned head */
1759 tf
->buffer
.head
= mmap(0,
1760 PAGE_ALIGN(tf
->buf_size
),
1761 PROT_READ
, MAP_PRIVATE
, tf
->fd
,
1762 PAGE_ALIGN((off_t
)tf
->buf_size
* (off_t
)block_num
));
1764 if(tf
->buffer
.head
== MAP_FAILED
) {
1765 perror("Error in allocating memory for buffer of tracefile");
1769 g_assert( ( (guint
)tf
->buffer
.head
&(8-1) ) == 0); // make sure it's aligned.
1772 tf
->buffer
.index
= block_num
;
1774 header
= (struct ltt_block_start_header
*)tf
->buffer
.head
;
1777 tf
->buffer
.begin
.timestamp
= ltt_time_add(
1778 ltt_time_from_uint64(
1779 ltt_get_uint64(LTT_GET_BO(tf
),
1780 &header
->begin
.timestamp
)
1781 - tf
->trace
->start_monotonic
),
1782 tf
->trace
->start_time
);
1784 //g_debug("block %u begin : %lu.%lu", block_num,
1785 // tf->buffer.begin.timestamp.tv_sec, tf->buffer.begin.timestamp.tv_nsec);
1786 tf
->buffer
.begin
.cycle_count
= ltt_get_uint64(LTT_GET_BO(tf
),
1787 &header
->begin
.cycle_count
);
1788 tf
->buffer
.begin
.freq
= ltt_get_uint64(LTT_GET_BO(tf
),
1789 &header
->begin
.freq
);
1790 tf
->buffer
.begin
.timestamp
= ltt_time_add(
1791 ltt_time_from_uint64(
1792 (double)(tf
->buffer
.begin
.cycle_count
1793 - tf
->trace
->start_tsc
) * 1000000.0
1794 / (double)tf
->trace
->start_freq
),
1795 tf
->trace
->start_time_from_tsc
);
1798 tf
->buffer
.end
.timestamp
= ltt_time_add(
1799 ltt_time_from_uint64(
1800 ltt_get_uint64(LTT_GET_BO(tf
),
1801 &header
->end
.timestamp
)
1802 - tf
->trace
->start_monotonic
),
1803 tf
->trace
->start_time
);
1805 //g_debug("block %u end : %lu.%lu", block_num,
1806 // tf->buffer.end.timestamp.tv_sec, tf->buffer.end.timestamp.tv_nsec);
1807 tf
->buffer
.end
.cycle_count
= ltt_get_uint64(LTT_GET_BO(tf
),
1808 &header
->end
.cycle_count
);
1809 tf
->buffer
.end
.freq
= ltt_get_uint64(LTT_GET_BO(tf
),
1811 tf
->buffer
.lost_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1812 &header
->lost_size
);
1813 tf
->buffer
.end
.timestamp
= ltt_time_add(
1814 ltt_time_from_uint64(
1815 (double)(tf
->buffer
.end
.cycle_count
1816 - tf
->trace
->start_tsc
) * 1000000.0
1817 / (double)tf
->trace
->start_freq
),
1818 tf
->trace
->start_time_from_tsc
);
1820 tf
->buffer
.tsc
= tf
->buffer
.begin
.cycle_count
;
1821 tf
->event
.tsc
= tf
->buffer
.tsc
;
1822 tf
->buffer
.freq
= tf
->buffer
.begin
.freq
;
1825 * eventually support variable buffer size : will need a partial pre-read of
1826 * the headers to create an index when we open the trace... eventually. */
1827 g_assert(tf
->buf_size
== ltt_get_uint32(LTT_GET_BO(tf
),
1828 &header
->buf_size
));
1830 /* Now that the buffer is mapped, calculate the time interpolation for the
1833 // tf->buffer.nsecs_per_cycle = calc_nsecs_per_cycle(tf);
1834 //tf->buffer.cyc2ns_scale = calc_nsecs_per_cycle(tf);
1836 /* Make the current event point to the beginning of the buffer :
1837 * it means that the event read must get the first event. */
1838 tf
->event
.tracefile
= tf
;
1839 tf
->event
.block
= block_num
;
1840 tf
->event
.offset
= 0;
1849 /* It will update the fields offsets too */
1850 void ltt_update_event_size(LttTracefile
*tf
)
1854 /* Specific handling of core events : necessary to read the facility control
1856 LttFacility
*f
= ltt_trace_get_facility_by_num(tf
->trace
,
1857 tf
->event
.facility_id
);
1859 if(likely(tf
->event
.facility_id
== LTT_FACILITY_CORE
)) {
1860 switch((enum ltt_core_events
)tf
->event
.event_id
) {
1861 case LTT_EVENT_FACILITY_LOAD
:
1862 size
= strlen((char*)tf
->event
.data
) + 1;
1863 //g_debug("Update Event facility load of facility %s", (char*)tf->event.data);
1864 size
+= sizeof(struct LttFacilityLoad
);
1866 case LTT_EVENT_FACILITY_UNLOAD
:
1867 //g_debug("Update Event facility unload");
1868 size
= sizeof(struct LttFacilityUnload
);
1870 case LTT_EVENT_STATE_DUMP_FACILITY_LOAD
:
1871 size
= strlen((char*)tf
->event
.data
) + 1;
1872 //g_debug("Update Event facility load state dump of facility %s",
1873 // (char*)tf->event.data);
1874 size
+= sizeof(struct LttStateDumpFacilityLoad
);
1876 case LTT_EVENT_HEARTBEAT
:
1877 //g_debug("Update Event heartbeat");
1878 size
= sizeof(TimeHeartbeat
);
1881 g_warning("Error in getting event size : tracefile %s, "
1882 "unknown event id %hhu in core facility.",
1883 g_quark_to_string(tf
->name
),
1884 tf
->event
.event_id
);
1885 goto event_id_error
;
1890 g_error("Unknown facility %hhu (0x%hhx) in tracefile %s",
1891 tf
->event
.facility_id
,
1892 tf
->event
.facility_id
,
1893 g_quark_to_string(tf
->name
));
1894 goto facility_error
;
1897 LttEventType
*event_type
=
1898 ltt_facility_eventtype_get(f
, tf
->event
.event_id
);
1901 g_error("Unknown event id %hhu in facility %s in tracefile %s",
1903 g_quark_to_string(f
->name
),
1904 g_quark_to_string(tf
->name
));
1905 goto event_type_error
;
1908 /* Compute the dynamic offsets */
1909 compute_offsets(tf
, event_type
, &size
, tf
->event
.data
);
1911 //g_debug("Event root field : f.e %hhu.%hhu size %zd",
1912 // tf->event.facility_id,
1913 // tf->event.event_id, size);
1916 tf
->event
.data_size
= size
;
1918 /* Check consistency between kernel and LTTV structure sizes */
1919 g_assert(tf
->event
.data_size
== tf
->event
.event_size
);
1926 tf
->event
.data_size
= 0;
1930 /* Take the tf current event offset and use the event facility id and event id
1931 * to figure out where is the next event offset.
1933 * This is an internal function not aiming at being used elsewhere : it will
1934 * not jump over the current block limits. Please consider using
1935 * ltt_tracefile_read to do this.
1937 * Returns 0 on success
1938 * ERANGE if we are at the end of the buffer.
1939 * ENOPROTOOPT if an error occured when getting the current event size.
1941 static int ltt_seek_next_event(LttTracefile
*tf
)
1946 /* seek over the buffer header if we are at the buffer start */
1947 if(tf
->event
.offset
== 0) {
1948 tf
->event
.offset
+= tf
->buffer_header_size
;
1950 if(tf
->event
.offset
== tf
->buf_size
- tf
->buffer
.lost_size
) {
1957 pos
= tf
->event
.data
;
1959 if(tf
->event
.data_size
< 0) goto error
;
1961 pos
+= (size_t)tf
->event
.data_size
;
1963 tf
->event
.offset
= pos
- tf
->buffer
.head
;
1965 if(tf
->event
.offset
== tf
->buf_size
- tf
->buffer
.lost_size
) {
1969 g_assert(tf
->event
.offset
< tf
->buf_size
- tf
->buffer
.lost_size
);
1975 g_error("Error in ltt_seek_next_event for tracefile %s",
1976 g_quark_to_string(tf
->name
));
1981 /*****************************************************************************
1983 * calc_nsecs_per_cycle : calculate nsecs per cycle for current block
1985 * 1.0 / (freq(khz) *1000) * 1000000000
1988 ****************************************************************************/
1989 /* from timer_tsc.c */
1990 #define CYC2NS_SCALE_FACTOR 10
1991 static guint32
calc_nsecs_per_cycle(LttTracefile
* tf
)
1993 //return 1e6 / (double)tf->buffer.freq;
1994 guint32 cpu_mhz
= tf
->buffer
.freq
/ 1000;
1995 guint32 cyc2ns_scale
= (1000 << CYC2NS_SCALE_FACTOR
)/cpu_mhz
;
1997 return cyc2ns_scale
;
1998 // return 1e6 / (double)tf->buffer.freq;
2001 static guint64
cycles_2_ns(LttTracefile
*tf
, guint64 cycles
)
2003 return (cycles
* tf
->buffer
.cyc2ns_scale
) >> CYC2NS_SCALE_FACTOR
;
2008 void setFieldsOffset(LttTracefile
*tf
, LttEventType
*evT
,void *evD
)
2010 LttField
* rootFld
= evT
->root_field
;
2011 // rootFld->base_address = evD;
2014 rootFld
->field_size
= getFieldtypeSize(tf
, evT
->facility
,
2015 evT
, 0,0,rootFld
, evD
);
2019 /*****************************************************************************
2021 * set_fields_offsets : set the precomputable offset of the fields
2023 * tracefile : opened trace file
2024 * event_type : the event type
2025 ****************************************************************************/
2027 void set_fields_offsets(LttTracefile
*tf
, LttEventType
*event_type
)
2029 LttField
*field
= event_type
->root_field
;
2030 enum field_status fixed_root
= FIELD_FIXED
, fixed_parent
= FIELD_FIXED
;
2033 preset_field_type_size(tf
, event_type
, 0, 0,
2034 &fixed_root
, &fixed_parent
,
2041 /*****************************************************************************
2043 * get_alignment : Get the alignment needed for a field.
2048 * returns : The size on which it must be aligned.
2050 ****************************************************************************/
2051 off_t
get_alignment(LttTracefile
*tf
, LttField
*field
)
2053 LttType
*type
= &field
->field_type
;
2055 switch(type
->type_class
) {
2057 case LTT_UINT_FIXED
:
2072 /* Align offset on type size */
2073 return field
->field_size
;
2079 g_assert(type
->fields
->len
== 1);
2081 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
2082 return get_alignment(tf
, child
);
2086 g_assert(type
->fields
->len
== 2);
2089 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
2091 localign
= max(localign
, get_alignment(tf
, child
));
2093 child
= &g_array_index(type
->fields
, LttField
, 1);
2094 localign
= max(localign
, get_alignment(tf
, child
));
2105 for(i
=0; i
<type
->fields
->len
; i
++) {
2106 LttField
*child
= &g_array_index(type
->fields
, LttField
, i
);
2107 localign
= max(localign
, get_alignment(tf
, child
));
2114 g_error("get_alignment : unknown type");
2119 /*****************************************************************************
2121 * field_compute_static_size : Determine the size of fields known by their
2122 * sole definition. Unions, arrays and struct sizes might be known, but
2123 * the parser does not give that information.
2128 ****************************************************************************/
2130 void field_compute_static_size(LttTracefile
*tf
, LttField
*field
)
2132 LttType
*type
= &field
->field_type
;
2134 switch(type
->type_class
) {
2136 case LTT_UINT_FIXED
:
2155 /* note this : array type size is the number of elements in the array,
2156 * while array field size of the length of the array in bytes */
2157 g_assert(type
->fields
->len
== 1);
2159 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
2160 field_compute_static_size(tf
, child
);
2162 if(child
->field_size
!= 0) {
2163 field
->field_size
= type
->size
* child
->field_size
;
2164 field
->dynamic_offsets
= g_array_sized_new(FALSE
, TRUE
,
2165 sizeof(off_t
), type
->size
);
2167 field
->field_size
= 0;
2172 g_assert(type
->fields
->len
== 2);
2174 off_t local_offset
= 0;
2175 LttField
*child
= &g_array_index(type
->fields
, LttField
, 1);
2176 field_compute_static_size(tf
, child
);
2177 field
->field_size
= 0;
2179 if(child
->field_size
!= 0) {
2180 field
->dynamic_offsets
= g_array_sized_new(FALSE
, TRUE
,
2181 sizeof(off_t
), SEQUENCE_AVG_ELEMENTS
);
2189 for(i
=0;i
<type
->fields
->len
;i
++) {
2190 LttField
*child
= &g_array_index(type
->fields
, LttField
, i
);
2191 field_compute_static_size(tf
, child
);
2192 if(child
->field_size
!= 0) {
2193 type
->size
+= ltt_align(type
->size
, get_alignment(tf
, child
),
2195 type
->size
+= child
->field_size
;
2197 /* As soon as we find a child with variable size, we have
2198 * a variable size */
2203 field
->field_size
= type
->size
;
2207 g_error("field_static_size : unknown type");
2214 /*****************************************************************************
2216 * precompute_fields_offsets : set the precomputable offset of the fields
2220 * offset : pointer to the current offset, must be incremented
2222 * return : 1 : found a variable length field, stop the processing.
2224 ****************************************************************************/
2227 gint
precompute_fields_offsets(LttTracefile
*tf
, LttField
*field
, off_t
*offset
)
2229 LttType
*type
= &field
->field_type
;
2231 switch(type
->type_class
) {
2233 case LTT_UINT_FIXED
:
2248 /* Align offset on type size */
2249 *offset
+= ltt_align(*offset
, get_alignment(tf
, field
),
2251 /* remember offset */
2252 field
->offset_root
= *offset
;
2253 field
->fixed_root
= FIELD_FIXED
;
2254 /* Increment offset */
2255 *offset
+= field
->field_size
;
2259 field
->offset_root
= *offset
;
2260 field
->fixed_root
= FIELD_FIXED
;
2264 g_assert(type
->fields
->len
== 1);
2266 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
2268 *offset
+= ltt_align(*offset
, get_alignment(tf
, field
),
2271 /* remember offset */
2272 field
->offset_root
= *offset
;
2273 field
->array_offset
= *offset
;
2274 field
->fixed_root
= FIELD_FIXED
;
2276 /* Let the child be variable */
2277 //precompute_fields_offsets(tf, child, offset);
2279 if(field
->field_size
!= 0) {
2280 /* Increment offset */
2281 /* field_size is the array size in bytes */
2282 *offset
+= field
->field_size
;
2290 g_assert(type
->fields
->len
== 2);
2295 *offset
+= ltt_align(*offset
, get_alignment(tf
, field
),
2298 /* remember offset */
2299 field
->offset_root
= *offset
;
2300 field
->fixed_root
= FIELD_FIXED
;
2302 child
= &g_array_index(type
->fields
, LttField
, 0);
2303 ret
= precompute_fields_offsets(tf
, child
, offset
);
2304 g_assert(ret
== 0); /* Seq len cannot have variable len */
2306 child
= &g_array_index(type
->fields
, LttField
, 1);
2307 *offset
+= ltt_align(*offset
, get_alignment(tf
, child
),
2309 field
->array_offset
= *offset
;
2310 /* Set the offset position at position 0 */
2311 ret
= precompute_fields_offsets(tf
, child
, offset
);
2313 /* Cannot precompute fields offsets of sequence members, and has
2314 * variable length. */
2324 *offset
+= ltt_align(*offset
, get_alignment(tf
, field
),
2326 /* remember offset */
2327 field
->offset_root
= *offset
;
2328 field
->fixed_root
= FIELD_FIXED
;
2330 for(i
=0; i
< type
->fields
->len
; i
++) {
2331 child
= &g_array_index(type
->fields
, LttField
, i
);
2332 ret
= precompute_fields_offsets(tf
, child
, offset
);
2345 *offset
+= ltt_align(*offset
, get_alignment(tf
, field
),
2347 /* remember offset */
2348 field
->offset_root
= *offset
;
2349 field
->fixed_root
= FIELD_FIXED
;
2351 for(i
=0; i
< type
->fields
->len
; i
++) {
2352 *offset
= field
->offset_root
;
2353 child
= &g_array_index(type
->fields
, LttField
, i
);
2354 ret
= precompute_fields_offsets(tf
, child
, offset
);
2358 *offset
= field
->offset_root
+ field
->field_size
;
2365 g_error("precompute_fields_offsets : unknown type");
2372 /*****************************************************************************
2374 * precompute_offsets : set the precomputable offset of an event type
2377 * event : event type
2379 ****************************************************************************/
2380 void precompute_offsets(LttTracefile
*tf
, LttEventType
*event
)
2386 /* First, compute the size of fixed size fields. Will determine size for
2387 * arrays, struct and unions, which is not done by the parser */
2388 for(i
=0; i
<event
->fields
->len
; i
++) {
2389 LttField
*field
= &g_array_index(event
->fields
, LttField
, i
);
2390 field_compute_static_size(tf
, field
);
2393 /* Precompute all known offsets */
2394 for(i
=0; i
<event
->fields
->len
; i
++) {
2395 LttField
*field
= &g_array_index(event
->fields
, LttField
, i
);
2396 ret
= precompute_fields_offsets(tf
, field
, &offset
);
2404 /*****************************************************************************
2406 * preset_field_type_size : set the fixed sizes of the field type
2409 * event_type : event type
2410 * offset_root : offset from the root
2411 * offset_parent : offset from the parent
2412 * fixed_root : Do we know a fixed offset to the root ?
2413 * fixed_parent : Do we know a fixed offset to the parent ?
2415 ****************************************************************************/
2419 // preset the fixed size offsets. Calculate them just like genevent-new : an
2420 // increment of a *to value that represents the offset from the start of the
2422 // The preset information is : offsets up to (and including) the first element
2423 // of variable size. All subsequent fields must be flagged "VARIABLE OFFSET".
2425 void preset_field_type_size(LttTracefile
*tf
, LttEventType
*event_type
,
2426 off_t offset_root
, off_t offset_parent
,
2427 enum field_status
*fixed_root
, enum field_status
*fixed_parent
,
2430 enum field_status local_fixed_root
, local_fixed_parent
;
2434 g_assert(field
->fixed_root
== FIELD_UNKNOWN
);
2435 g_assert(field
->fixed_parent
== FIELD_UNKNOWN
);
2436 g_assert(field
->fixed_size
== FIELD_UNKNOWN
);
2438 type
= field
->field_type
;
2440 field
->fixed_root
= *fixed_root
;
2441 if(field
->fixed_root
== FIELD_FIXED
)
2442 field
->offset_root
= offset_root
;
2444 field
->offset_root
= 0;
2446 field
->fixed_parent
= *fixed_parent
;
2447 if(field
->fixed_parent
== FIELD_FIXED
)
2448 field
->offset_parent
= offset_parent
;
2450 field
->offset_parent
= 0;
2452 size_t current_root_offset
;
2453 size_t current_offset
;
2454 enum field_status current_child_status
, final_child_status
;
2457 switch(type
->type_class
) {
2462 field
->field_size
= ltt_type_size(tf
->trace
, type
);
2463 field
->fixed_size
= FIELD_FIXED
;
2466 field
->field_size
= (off_t
)event_type
->facility
->pointer_size
;
2467 field
->fixed_size
= FIELD_FIXED
;
2471 field
->field_size
= (off_t
)event_type
->facility
->long_size
;
2472 field
->fixed_size
= FIELD_FIXED
;
2477 field
->field_size
= (off_t
)event_type
->facility
->size_t_size
;
2478 field
->fixed_size
= FIELD_FIXED
;
2481 local_fixed_root
= FIELD_VARIABLE
;
2482 local_fixed_parent
= FIELD_VARIABLE
;
2483 preset_field_type_size(tf
, event_type
,
2485 &local_fixed_root
, &local_fixed_parent
,
2487 field
->fixed_size
= FIELD_VARIABLE
;
2488 field
->field_size
= 0;
2489 *fixed_root
= FIELD_VARIABLE
;
2490 *fixed_parent
= FIELD_VARIABLE
;
2493 field
->fixed_size
= FIELD_VARIABLE
;
2494 field
->field_size
= 0;
2495 *fixed_root
= FIELD_VARIABLE
;
2496 *fixed_parent
= FIELD_VARIABLE
;
2499 local_fixed_root
= FIELD_VARIABLE
;
2500 local_fixed_parent
= FIELD_VARIABLE
;
2501 preset_field_type_size(tf
, event_type
,
2503 &local_fixed_root
, &local_fixed_parent
,
2505 field
->fixed_size
= field
->child
[0]->fixed_size
;
2506 if(field
->fixed_size
== FIELD_FIXED
) {
2507 field
->field_size
= type
->element_number
* field
->child
[0]->field_size
;
2509 field
->field_size
= 0;
2510 *fixed_root
= FIELD_VARIABLE
;
2511 *fixed_parent
= FIELD_VARIABLE
;
2515 current_root_offset
= field
->offset_root
;
2517 current_child_status
= FIELD_FIXED
;
2518 for(i
=0;i
<type
->element_number
;i
++) {
2519 preset_field_type_size(tf
, event_type
,
2520 current_root_offset
, current_offset
,
2521 fixed_root
, ¤t_child_status
,
2523 if(current_child_status
== FIELD_FIXED
) {
2524 current_root_offset
+= field
->child
[i
]->field_size
;
2525 current_offset
+= field
->child
[i
]->field_size
;
2527 current_root_offset
= 0;
2531 if(current_child_status
!= FIELD_FIXED
) {
2532 *fixed_parent
= current_child_status
;
2533 field
->field_size
= 0;
2534 field
->fixed_size
= current_child_status
;
2536 field
->field_size
= current_offset
;
2537 field
->fixed_size
= FIELD_FIXED
;
2541 current_root_offset
= field
->offset_root
;
2544 final_child_status
= FIELD_FIXED
;
2545 for(i
=0;i
<type
->element_number
;i
++) {
2546 enum field_status current_root_child_status
= FIELD_FIXED
;
2547 enum field_status current_child_status
= FIELD_FIXED
;
2548 preset_field_type_size(tf
, event_type
,
2549 current_root_offset
, current_offset
,
2550 ¤t_root_child_status
, ¤t_child_status
,
2552 if(current_child_status
!= FIELD_FIXED
)
2553 final_child_status
= current_child_status
;
2555 max_size
= max(max_size
, field
->child
[i
]->field_size
);
2557 if(final_child_status
!= FIELD_FIXED
) {
2558 g_error("LTTV does not support variable size fields in unions.");
2559 /* This will stop the application. */
2560 *fixed_root
= final_child_status
;
2561 *fixed_parent
= final_child_status
;
2562 field
->field_size
= 0;
2563 field
->fixed_size
= current_child_status
;
2565 field
->field_size
= max_size
;
2566 field
->fixed_size
= FIELD_FIXED
;
2574 /*****************************************************************************
2576 * check_fields_compatibility : Check for compatibility between two fields :
2577 * do they use the same inner structure ?
2579 * event_type1 : event type
2580 * event_type2 : event type
2583 *Returns : 0 if identical
2585 ****************************************************************************/
2586 // this function checks for equality of field types. Therefore, it does not use
2587 // per se offsets. For instance, an aligned version of a structure is
2588 // compatible with an unaligned version of the same structure.
2589 gint
check_fields_compatibility(LttEventType
*event_type1
,
2590 LttEventType
*event_type2
,
2591 LttField
*field1
, LttField
*field2
)
2593 guint different
= 0;
2597 if(field1
== NULL
) {
2598 if(field2
== NULL
) goto end
;
2603 } else if(field2
== NULL
) {
2608 type1
= &field1
->field_type
;
2609 type2
= &field2
->field_type
;
2611 if(type1
->type_class
!= type2
->type_class
) {
2616 switch(type1
->type_class
) {
2618 case LTT_UINT_FIXED
:
2633 if(field1
->field_size
!= field2
->field_size
)
2640 LttField
*child1
= &g_array_index(type1
->fields
, LttField
, 0);
2641 LttField
*child2
= &g_array_index(type2
->fields
, LttField
, 0);
2643 if(type1
->size
!= type2
->size
)
2645 if(check_fields_compatibility(event_type1
, event_type2
, child1
, child2
))
2651 LttField
*child1
= &g_array_index(type1
->fields
, LttField
, 1);
2652 LttField
*child2
= &g_array_index(type2
->fields
, LttField
, 1);
2654 if(check_fields_compatibility(event_type1
, event_type2
, child1
, child2
))
2664 if(type1
->fields
->len
!= type2
->fields
->len
) {
2669 for(i
=0; i
< type1
->fields
->len
; i
++) {
2672 child1
= &g_array_index(type1
->fields
, LttField
, i
);
2673 child2
= &g_array_index(type2
->fields
, LttField
, i
);
2674 different
= check_fields_compatibility(event_type1
,
2675 event_type2
, child1
, child2
);
2677 if(different
) break;
2683 g_error("precompute_fields_offsets : unknown type");
2692 gint
check_fields_compatibility(LttEventType
*event_type1
,
2693 LttEventType
*event_type2
,
2694 LttField
*field1
, LttField
*field2
)
2696 guint different
= 0;
2701 if(field1
== NULL
) {
2702 if(field2
== NULL
) goto end
;
2707 } else if(field2
== NULL
) {
2712 g_assert(field1
->fixed_root
!= FIELD_UNKNOWN
);
2713 g_assert(field2
->fixed_root
!= FIELD_UNKNOWN
);
2714 g_assert(field1
->fixed_parent
!= FIELD_UNKNOWN
);
2715 g_assert(field2
->fixed_parent
!= FIELD_UNKNOWN
);
2716 g_assert(field1
->fixed_size
!= FIELD_UNKNOWN
);
2717 g_assert(field2
->fixed_size
!= FIELD_UNKNOWN
);
2719 type1
= field1
->field_type
;
2720 type2
= field2
->field_type
;
2722 if(type1
->type_class
!= type2
->type_class
) {
2726 if(type1
->element_name
!= type2
->element_name
) {
2731 switch(type1
->type_class
) {
2741 if(field1
->field_size
!= field2
->field_size
) {
2747 if(type1
->element_number
!= type2
->element_number
) {
2751 for(i
=0;i
<type1
->element_number
;i
++) {
2752 if(type1
->enum_strings
[i
] != type2
->enum_strings
[i
]) {
2759 /* Two elements : size and child */
2760 g_assert(type1
->element_number
!= type2
->element_number
);
2761 for(i
=0;i
<type1
->element_number
;i
++) {
2762 if(check_fields_compatibility(event_type1
, event_type2
,
2763 field1
->child
[0], field2
->child
[0])) {
2772 if(field1
->field_size
!= field2
->field_size
) {
2776 /* Two elements : size and child */
2777 g_assert(type1
->element_number
!= type2
->element_number
);
2778 for(i
=0;i
<type1
->element_number
;i
++) {
2779 if(check_fields_compatibility(event_type1
, event_type2
,
2780 field1
->child
[0], field2
->child
[0])) {
2788 if(type1
->element_number
!= type2
->element_number
) {
2792 for(i
=0;i
<type1
->element_number
;i
++) {
2793 if(check_fields_compatibility(event_type1
, event_type2
,
2794 field1
->child
[0], field2
->child
[0])) {
2807 /*****************************************************************************
2809 * ltt_get_int : get an integer number
2811 * reverse_byte_order: must we reverse the byte order ?
2812 * size : the size of the integer
2813 * ptr : the data pointer
2815 * gint64 : a 64 bits integer
2816 ****************************************************************************/
2818 gint64
ltt_get_int(gboolean reverse_byte_order
, gint size
, void *data
)
2823 case 1: val
= *((gint8
*)data
); break;
2824 case 2: val
= ltt_get_int16(reverse_byte_order
, data
); break;
2825 case 4: val
= ltt_get_int32(reverse_byte_order
, data
); break;
2826 case 8: val
= ltt_get_int64(reverse_byte_order
, data
); break;
2827 default: val
= ltt_get_int64(reverse_byte_order
, data
);
2828 g_critical("get_int : integer size %d unknown", size
);
2835 /*****************************************************************************
2837 * ltt_get_uint : get an unsigned integer number
2839 * reverse_byte_order: must we reverse the byte order ?
2840 * size : the size of the integer
2841 * ptr : the data pointer
2843 * guint64 : a 64 bits unsigned integer
2844 ****************************************************************************/
2846 guint64
ltt_get_uint(gboolean reverse_byte_order
, gint size
, void *data
)
2851 case 1: val
= *((gint8
*)data
); break;
2852 case 2: val
= ltt_get_uint16(reverse_byte_order
, data
); break;
2853 case 4: val
= ltt_get_uint32(reverse_byte_order
, data
); break;
2854 case 8: val
= ltt_get_uint64(reverse_byte_order
, data
); break;
2855 default: val
= ltt_get_uint64(reverse_byte_order
, data
);
2856 g_critical("get_uint : unsigned integer size %d unknown",
2865 /* get the node name of the system */
2867 char * ltt_trace_system_description_node_name (LttSystemDescription
* s
)
2869 return s
->node_name
;
2873 /* get the domain name of the system */
2875 char * ltt_trace_system_description_domain_name (LttSystemDescription
* s
)
2877 return s
->domain_name
;
2881 /* get the description of the system */
2883 char * ltt_trace_system_description_description (LttSystemDescription
* s
)
2885 return s
->description
;
2889 /* get the NTP corrected start time of the trace */
2890 LttTime
ltt_trace_start_time(LttTrace
*t
)
2892 return t
->start_time
;
2895 /* get the monotonic start time of the trace */
2896 LttTime
ltt_trace_start_time_monotonic(LttTrace
*t
)
2898 return t
->start_time_from_tsc
;
2901 LttTracefile
*ltt_tracefile_new()
2903 return g_new(LttTracefile
, 1);
2906 void ltt_tracefile_destroy(LttTracefile
*tf
)
2911 void ltt_tracefile_copy(LttTracefile
*dest
, const LttTracefile
*src
)
2916 /* Before library loading... */
2918 static void __attribute__((constructor
)) init(void)
2920 LTT_FACILITY_NAME_HEARTBEAT
= g_quark_from_string("heartbeat");
2921 LTT_EVENT_NAME_HEARTBEAT
= g_quark_from_string("heartbeat");
2923 LTT_TRACEFILE_NAME_FACILITIES
= g_quark_from_string("/control/facilities");