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 char_name
[raw_name_len
] = '\0';
668 *name
= g_quark_from_string(char_name
);
669 *num
= 0; /* unknown cpu */
670 for(i
=0;i
<raw_name_len
;i
++) {
671 if(raw_name
[i
] == '/') {
676 for(;i
<raw_name_len
;i
++) {
677 if(raw_name
[i
] == '/') {
682 for(;i
<raw_name_len
;i
++) {
683 if(raw_name
[i
] == '-') {
687 if(i
== raw_name_len
) return -1;
689 tmpptr
= &raw_name
[i
];
690 for(;i
<raw_name_len
;i
++) {
691 if(raw_name
[i
] == '.') {
696 *tid
= strtoul(tmpptr
, &endptr
, 10);
698 return -1; /* No digit */
699 if(*tid
== ULONG_MAX
)
700 return -1; /* underflow / overflow */
702 tmpptr
= &raw_name
[i
];
703 for(;i
<raw_name_len
;i
++) {
704 if(raw_name
[i
] == '.') {
709 *pgid
= strtoul(tmpptr
, &endptr
, 10);
711 return -1; /* No digit */
712 if(*pgid
== ULONG_MAX
)
713 return -1; /* underflow / overflow */
715 tmpptr
= &raw_name
[i
];
716 *creation
= strtoull(tmpptr
, &endptr
, 10);
718 return -1; /* No digit */
719 if(*creation
== G_MAXUINT64
)
720 return -1; /* underflow / overflow */
724 cpu_num
= strtol(raw_name
+underscore_pos
+1, &endptr
, 10);
726 if(endptr
== raw_name
+underscore_pos
+1)
727 return -1; /* No digit */
728 if(cpu_num
== LONG_MIN
|| cpu_num
== LONG_MAX
)
729 return -1; /* underflow / overflow */
731 strncpy(char_name
, raw_name
, underscore_pos
);
732 char_name
[underscore_pos
] = '\0';
734 *name
= g_quark_from_string(char_name
);
743 GData
**ltt_trace_get_tracefiles_groups(LttTrace
*trace
)
745 return &trace
->tracefiles
;
749 void compute_tracefile_group(GQuark key_id
,
751 struct compute_tracefile_group_args
*args
)
756 for(i
=0; i
<group
->len
; i
++) {
757 tf
= &g_array_index (group
, LttTracefile
, i
);
759 args
->func(tf
, args
->func_args
);
764 void ltt_tracefile_group_destroy(gpointer data
)
766 GArray
*group
= (GArray
*)data
;
770 for(i
=0; i
<group
->len
; i
++) {
771 tf
= &g_array_index (group
, LttTracefile
, i
);
773 ltt_tracefile_close(tf
);
775 g_array_free(group
, TRUE
);
778 gboolean
ltt_tracefile_group_has_cpu_online(gpointer data
)
780 GArray
*group
= (GArray
*)data
;
784 for(i
=0; i
<group
->len
; i
++) {
785 tf
= &g_array_index (group
, LttTracefile
, i
);
786 if(tf
->cpu_online
) return 1;
792 /* Open each tracefile under a specific directory. Put them in a
793 * GData : permits to access them using their tracefile group pathname.
794 * i.e. access control/modules tracefile group by index :
797 * relative path is the path relative to the trace root
798 * root path is the full path
800 * A tracefile group is simply an array where all the per cpu tracefiles sits.
803 static int open_tracefiles(LttTrace
*trace
, gchar
*root_path
,
804 gchar
*relative_path
)
806 DIR *dir
= opendir(root_path
);
807 struct dirent
*entry
;
808 struct stat stat_buf
;
811 gchar path
[PATH_MAX
];
816 gchar rel_path
[PATH_MAX
];
825 strncpy(path
, root_path
, PATH_MAX
-1);
826 path_len
= strlen(path
);
827 path
[path_len
] = '/';
829 path_ptr
= path
+ path_len
;
831 strncpy(rel_path
, relative_path
, PATH_MAX
-1);
832 rel_path_len
= strlen(rel_path
);
833 rel_path
[rel_path_len
] = '/';
835 rel_path_ptr
= rel_path
+ rel_path_len
;
837 while((entry
= readdir(dir
)) != NULL
) {
839 if(entry
->d_name
[0] == '.') continue;
841 strncpy(path_ptr
, entry
->d_name
, PATH_MAX
- path_len
);
842 strncpy(rel_path_ptr
, entry
->d_name
, PATH_MAX
- rel_path_len
);
844 ret
= stat(path
, &stat_buf
);
850 g_debug("Tracefile file or directory : %s\n", path
);
852 if(strcmp(rel_path
, "/eventdefs") == 0) continue;
854 if(S_ISDIR(stat_buf
.st_mode
)) {
856 g_debug("Entering subdirectory...\n");
857 ret
= open_tracefiles(trace
, path
, rel_path
);
858 if(ret
< 0) continue;
859 } else if(S_ISREG(stat_buf
.st_mode
)) {
861 guint num
, tid
, pgid
;
864 num
= tid
= pgid
= 0;
866 if(get_tracefile_name_number(rel_path
, &name
, &num
, &tid
, &pgid
, &creation
))
867 continue; /* invalid name */
869 g_debug("Opening file.\n");
870 if(ltt_tracefile_open(trace
, path
, &tmp_tf
)) {
871 g_info("Error opening tracefile %s", path
);
873 continue; /* error opening the tracefile : bad magic number ? */
876 g_debug("Tracefile name is %s and number is %u",
877 g_quark_to_string(name
), num
);
879 tmp_tf
.cpu_online
= 1;
880 tmp_tf
.cpu_num
= num
;
884 tmp_tf
.creation
= creation
;
886 group
= g_datalist_id_get_data(&trace
->tracefiles
, name
);
888 /* Elements are automatically cleared when the array is allocated.
889 * It makes the cpu_online variable set to 0 : cpu offline, by default.
891 group
= g_array_sized_new (FALSE
, TRUE
, sizeof(LttTracefile
), 10);
892 g_datalist_id_set_data_full(&trace
->tracefiles
, name
,
893 group
, ltt_tracefile_group_destroy
);
896 /* Add the per cpu tracefile to the named group */
897 unsigned int old_len
= group
->len
;
899 group
= g_array_set_size(group
, num
+1);
900 g_array_index (group
, LttTracefile
, num
) = tmp_tf
;
910 /* ltt_get_facility_description
912 * Opens the file corresponding to the requested facility (identified by fac_id
915 * The name searched is : %trace root%/eventdefs/facname_checksum.xml
917 * Returns 0 on success, or 1 on failure.
920 static int ltt_get_facility_description(LttFacility
*f
,
922 LttTracefile
*fac_tf
)
924 char desc_file_name
[PATH_MAX
];
931 text
= g_quark_to_string(t
->pathname
);
932 textlen
= strlen(text
);
934 if(textlen
>= PATH_MAX
) goto name_error
;
935 strcpy(desc_file_name
, text
);
937 text
= "/eventdefs/";
938 textlen
+=strlen(text
);
939 if(textlen
>= PATH_MAX
) goto name_error
;
940 strcat(desc_file_name
, text
);
942 text
= g_quark_to_string(f
->name
);
943 fac_name_len
= strlen(text
);
944 textlen
+=fac_name_len
;
945 if(textlen
>= PATH_MAX
) goto name_error
;
946 strcat(desc_file_name
, text
);
948 /* arch specific facilities are named like this : name_arch */
949 if(fac_name_len
+1 < sizeof("_arch"))
952 if(!strcmp(&text
[fac_name_len
+1-sizeof("_arch")], "_arch"))
960 textlen
+=strlen(text
);
961 if(textlen
>= PATH_MAX
) goto name_error
;
962 strcat(desc_file_name
, text
);
964 err
= snprintf(desc_file_name
+textlen
, PATH_MAX
-textlen
-1,
966 if(err
< 0) goto name_error
;
968 textlen
=strlen(desc_file_name
);
973 switch(t
->arch_type
) {
974 case LTT_ARCH_TYPE_I386
:
977 case LTT_ARCH_TYPE_PPC
:
980 case LTT_ARCH_TYPE_SH
:
983 case LTT_ARCH_TYPE_S390
:
986 case LTT_ARCH_TYPE_MIPS
:
989 case LTT_ARCH_TYPE_ARM
:
992 case LTT_ARCH_TYPE_PPC64
:
995 case LTT_ARCH_TYPE_X86_64
:
998 case LTT_ARCH_TYPE_C2
:
1001 case LTT_ARCH_TYPE_POWERPC
:
1005 g_error("Trace from unsupported architecture.");
1007 textlen
+=strlen(text
);
1008 if(textlen
>= PATH_MAX
) goto name_error
;
1009 strcat(desc_file_name
, text
);
1013 textlen
+=strlen(text
);
1014 if(textlen
>= PATH_MAX
) goto name_error
;
1015 strcat(desc_file_name
, text
);
1017 err
= ltt_facility_open(f
, t
, desc_file_name
);
1018 if(err
) goto facility_error
;
1027 static void ltt_fac_ids_destroy(gpointer data
)
1029 GArray
*fac_ids
= (GArray
*)data
;
1031 g_array_free(fac_ids
, TRUE
);
1035 /* Presumes the tracefile is already seeked at the beginning. It makes sense,
1036 * because it must be done just after the opening */
1037 static int ltt_process_facility_tracefile(LttTracefile
*tf
)
1046 err
= ltt_tracefile_read_seek(tf
);
1047 if(err
== EPERM
) goto seek_error
;
1048 else if(err
== ERANGE
) break; /* End of tracefile */
1050 err
= ltt_tracefile_read_update_event(tf
);
1051 if(err
) goto update_error
;
1053 /* We are on a facility load/or facility unload/ or heartbeat event */
1055 * * facility 0 is hardcoded : this is the core facility. It will be shown
1056 * in the facility array though, and is shown as "loaded builtin" in the
1058 * It contains event :
1060 * 1 : facility unload
1061 * 2 : state dump facility load
1064 if(tf
->event
.facility_id
!= LTT_FACILITY_CORE
) {
1065 /* Should only contain core facility */
1066 g_warning("Error in processing facility file %s, "
1067 "should not contain facility id %u.", g_quark_to_string(tf
->name
),
1068 tf
->event
.facility_id
);
1073 struct LttFacilityLoad
*fac_load_data
;
1074 struct LttStateDumpFacilityLoad
*fac_state_dump_load_data
;
1079 switch((enum ltt_core_events
)tf
->event
.event_id
) {
1080 case LTT_EVENT_FACILITY_LOAD
:
1081 fac_name
= (char*)(tf
->event
.data
);
1082 g_debug("Doing LTT_EVENT_FACILITY_LOAD of facility %s",
1084 pos
= (tf
->event
.data
+ strlen(fac_name
) + 1);
1085 pos
+= ltt_align((size_t)pos
, tf
->trace
->arch_size
, tf
->has_alignment
);
1086 fac_load_data
= (struct LttFacilityLoad
*)pos
;
1088 fac
= &g_array_index (tf
->trace
->facilities_by_num
, LttFacility
,
1089 ltt_get_uint32(LTT_GET_BO(tf
), &fac_load_data
->id
));
1090 /* facility may already exist if trace is paused/unpaused */
1091 if(fac
->exists
) continue;
1092 fac
->name
= g_quark_from_string(fac_name
);
1093 fac
->checksum
= ltt_get_uint32(LTT_GET_BO(tf
),
1094 &fac_load_data
->checksum
);
1095 fac
->id
= ltt_get_uint32(LTT_GET_BO(tf
), &fac_load_data
->id
);
1096 fac
->pointer_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1097 &fac_load_data
->pointer_size
);
1098 fac
->int_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1099 &fac_load_data
->int_size
);
1100 fac
->long_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1101 &fac_load_data
->long_size
);
1102 fac
->size_t_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1103 &fac_load_data
->size_t_size
);
1104 fac
->alignment
= ltt_get_uint32(LTT_GET_BO(tf
),
1105 &fac_load_data
->has_alignment
);
1107 if(ltt_get_facility_description(fac
, tf
->trace
, tf
))
1108 continue; /* error opening description */
1110 fac
->trace
= tf
->trace
;
1112 /* Preset the field offsets */
1113 for(i
=0; i
<fac
->events
->len
; i
++){
1114 et
= &g_array_index(fac
->events
, LttEventType
, i
);
1115 precompute_offsets(fac
, et
);
1120 fac_ids
= g_datalist_id_get_data(&tf
->trace
->facilities_by_name
,
1122 if(fac_ids
== NULL
) {
1123 fac_ids
= g_array_sized_new (FALSE
, TRUE
, sizeof(guint
), 1);
1124 g_datalist_id_set_data_full(&tf
->trace
->facilities_by_name
,
1126 fac_ids
, ltt_fac_ids_destroy
);
1128 g_array_append_val(fac_ids
, fac
->id
);
1131 case LTT_EVENT_FACILITY_UNLOAD
:
1132 g_debug("Doing LTT_EVENT_FACILITY_UNLOAD");
1133 /* We don't care about unload : facilities ID are valid for the whole
1134 * trace. They simply won't be used after the unload. */
1136 case LTT_EVENT_STATE_DUMP_FACILITY_LOAD
:
1137 fac_name
= (char*)(tf
->event
.data
);
1138 g_debug("Doing LTT_EVENT_STATE_DUMP_FACILITY_LOAD of facility %s",
1140 pos
= (tf
->event
.data
+ strlen(fac_name
) + 1);
1141 pos
+= ltt_align((size_t)pos
, tf
->trace
->arch_size
, tf
->has_alignment
);
1142 fac_state_dump_load_data
= (struct LttStateDumpFacilityLoad
*)pos
;
1144 fac
= &g_array_index (tf
->trace
->facilities_by_num
, LttFacility
,
1145 ltt_get_uint32(LTT_GET_BO(tf
), &fac_state_dump_load_data
->id
));
1146 /* facility may already exist if trace is paused/unpaused */
1147 if(fac
->exists
) continue;
1148 fac
->name
= g_quark_from_string(fac_name
);
1149 fac
->checksum
= ltt_get_uint32(LTT_GET_BO(tf
),
1150 &fac_state_dump_load_data
->checksum
);
1151 fac
->id
= ltt_get_uint32(LTT_GET_BO(tf
),
1152 &fac_state_dump_load_data
->id
);
1153 fac
->pointer_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1154 &fac_state_dump_load_data
->pointer_size
);
1155 fac
->int_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1156 &fac_state_dump_load_data
->int_size
);
1157 fac
->long_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1158 &fac_state_dump_load_data
->long_size
);
1159 fac
->size_t_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1160 &fac_state_dump_load_data
->size_t_size
);
1161 fac
->alignment
= ltt_get_uint32(LTT_GET_BO(tf
),
1162 &fac_state_dump_load_data
->has_alignment
);
1163 if(ltt_get_facility_description(fac
, tf
->trace
, tf
))
1164 continue; /* error opening description */
1166 fac
->trace
= tf
->trace
;
1168 /* Preset the field offsets */
1169 for(i
=0; i
<fac
->events
->len
; i
++){
1170 et
= &g_array_index(fac
->events
, LttEventType
, i
);
1171 precompute_offsets(fac
, et
);
1176 fac_ids
= g_datalist_id_get_data(&tf
->trace
->facilities_by_name
,
1178 if(fac_ids
== NULL
) {
1179 fac_ids
= g_array_sized_new (FALSE
, TRUE
, sizeof(guint
), 1);
1180 g_datalist_id_set_data_full(&tf
->trace
->facilities_by_name
,
1182 fac_ids
, ltt_fac_ids_destroy
);
1184 g_array_append_val(fac_ids
, fac
->id
);
1187 case LTT_EVENT_HEARTBEAT
:
1190 g_warning("Error in processing facility file %s, "
1191 "unknown event id %hhu in core facility.",
1192 g_quark_to_string(tf
->name
),
1193 tf
->event
.event_id
);
1195 goto event_id_error
;
1201 /* Error handling */
1206 g_warning("An error occured in facility tracefile parsing");
1211 LttTrace
*ltt_trace_open(const gchar
*pathname
)
1213 gchar abs_path
[PATH_MAX
];
1218 struct ltt_block_start_header
*header
;
1220 struct dirent
*entry
;
1221 guint control_found
= 0;
1222 guint eventdefs_found
= 0;
1223 struct stat stat_buf
;
1224 gchar path
[PATH_MAX
];
1226 t
= g_new(LttTrace
, 1);
1227 if(!t
) goto alloc_error
;
1229 get_absolute_pathname(pathname
, abs_path
);
1230 t
->pathname
= g_quark_from_string(abs_path
);
1232 g_datalist_init(&t
->tracefiles
);
1234 /* Test to see if it looks like a trace */
1235 dir
= opendir(abs_path
);
1240 while((entry
= readdir(dir
)) != NULL
) {
1241 strcpy(path
, abs_path
);
1243 strcat(path
, entry
->d_name
);
1244 ret
= stat(path
, &stat_buf
);
1249 if(S_ISDIR(stat_buf
.st_mode
)) {
1250 if(strcmp(entry
->d_name
, "control") == 0) {
1253 if(strcmp(entry
->d_name
, "eventdefs") == 0) {
1254 eventdefs_found
= 1;
1260 if(!control_found
|| !eventdefs_found
) goto find_error
;
1262 /* Open all the tracefiles */
1263 if(open_tracefiles(t
, abs_path
, "")) {
1264 g_warning("Error opening tracefile %s", abs_path
);
1268 /* Prepare the facilities containers : array and mapping */
1269 /* Array is zeroed : the "exists" field is set to false by default */
1270 t
->facilities_by_num
= g_array_sized_new (FALSE
,
1271 TRUE
, sizeof(LttFacility
),
1273 t
->facilities_by_num
= g_array_set_size(t
->facilities_by_num
, NUM_FACILITIES
);
1275 g_datalist_init(&t
->facilities_by_name
);
1277 /* Parse each trace control/facilitiesN files : get runtime fac. info */
1278 group
= g_datalist_id_get_data(&t
->tracefiles
, LTT_TRACEFILE_NAME_FACILITIES
);
1280 g_error("Trace %s has no facility tracefile", abs_path
);
1282 goto facilities_error
;
1285 /* Get the trace information for the control/facility 0 tracefile */
1286 g_assert(group
->len
> 0);
1287 tf
= &g_array_index (group
, LttTracefile
, 0);
1288 header
= (struct ltt_block_start_header
*)tf
->buffer
.head
;
1289 g_assert(parse_trace_header(header
->trace
,
1292 t
->num_cpu
= group
->len
;
1294 for(i
=0; i
<group
->len
; i
++) {
1295 tf
= &g_array_index (group
, LttTracefile
, i
);
1296 if(ltt_process_facility_tracefile(tf
))
1297 goto facilities_error
;
1302 /* Error handling */
1304 g_datalist_clear(&t
->facilities_by_name
);
1305 g_array_free(t
->facilities_by_num
, TRUE
);
1307 g_datalist_clear(&t
->tracefiles
);
1315 GQuark
ltt_trace_name(const LttTrace
*t
)
1321 /******************************************************************************
1322 * When we copy a trace, we want all the opening actions to happen again :
1323 * the trace will be reopened and totally independant from the original.
1324 * That's why we call ltt_trace_open.
1325 *****************************************************************************/
1326 LttTrace
*ltt_trace_copy(LttTrace
*self
)
1328 return ltt_trace_open(g_quark_to_string(self
->pathname
));
1331 void ltt_trace_close(LttTrace
*t
)
1336 for(i
=0; i
<t
->facilities_by_num
->len
; i
++) {
1337 fac
= &g_array_index (t
->facilities_by_num
, LttFacility
, i
);
1339 ltt_facility_close(fac
);
1342 g_datalist_clear(&t
->facilities_by_name
);
1343 g_array_free(t
->facilities_by_num
, TRUE
);
1344 g_datalist_clear(&t
->tracefiles
);
1349 /*****************************************************************************
1350 *Get the system description of the trace
1351 ****************************************************************************/
1353 LttFacility
*ltt_trace_facility_by_id(LttTrace
*t
, guint8 id
)
1355 g_assert(id
< t
->facilities_by_num
->len
);
1356 return &g_array_index(t
->facilities_by_num
, LttFacility
, id
);
1359 /* ltt_trace_facility_get_by_name
1361 * Returns the GArray of facility indexes. All the fac_ids that matches the
1362 * requested facility name.
1364 * If name is not found, returns NULL.
1366 GArray
*ltt_trace_facility_get_by_name(LttTrace
*t
, GQuark name
)
1368 return g_datalist_id_get_data(&t
->facilities_by_name
, name
);
1371 /*****************************************************************************
1372 * Functions to discover all the event types in the trace
1373 ****************************************************************************/
1376 unsigned ltt_trace_eventtype_number(LttTrace
*t
)
1380 unsigned int num
= t
->facility_number
;
1384 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, i
);
1385 count
+= f
->event_number
;
1392 //use an iteration on all the trace facilities, and inside iteration on all the
1393 //event types in each facilities instead.
1394 LttEventType
*ltt_trace_eventtype_get(LttTrace
*t
, unsigned evId
)
1396 LttEventType
*event_type
;
1399 f
= ltt_trace_facility_by_id(t
,evId
);
1401 if(unlikely(!f
)) event_type
= NULL
;
1402 else event_type
= f
->events
[evId
- f
->base_id
];
1409 /*****************************************************************************
1410 * ltt_trace_find_tracefile
1412 * Find a tracefile by name and index in the group.
1414 * Returns a pointer to the tracefiles, else NULL.
1415 ****************************************************************************/
1417 LttTracefile
*ltt_trace_find_tracefile(LttTrace
*t
, const gchar
*name
)
1422 /*****************************************************************************
1423 * Get the start time and end time of the trace
1424 ****************************************************************************/
1426 static void ltt_tracefile_time_span_get(LttTracefile
*tf
,
1427 LttTime
*start
, LttTime
*end
)
1431 err
= map_block(tf
, 0);
1433 g_error("Can not map block");
1434 *start
= ltt_time_infinite
;
1436 *start
= tf
->buffer
.begin
.timestamp
;
1438 err
= map_block(tf
, tf
->num_blocks
- 1); /* Last block */
1440 g_error("Can not map block");
1441 *end
= ltt_time_zero
;
1443 *end
= tf
->buffer
.end
.timestamp
;
1446 struct tracefile_time_span_get_args
{
1452 static void group_time_span_get(GQuark name
, gpointer data
, gpointer user_data
)
1454 struct tracefile_time_span_get_args
*args
=
1455 (struct tracefile_time_span_get_args
*)user_data
;
1457 GArray
*group
= (GArray
*)data
;
1463 for(i
=0; i
<group
->len
; i
++) {
1464 tf
= &g_array_index (group
, LttTracefile
, i
);
1465 if(tf
->cpu_online
) {
1466 ltt_tracefile_time_span_get(tf
, &tmp_start
, &tmp_end
);
1467 if(ltt_time_compare(*args
->start
, tmp_start
)>0) *args
->start
= tmp_start
;
1468 if(ltt_time_compare(*args
->end
, tmp_end
)<0) *args
->end
= tmp_end
;
1473 void ltt_trace_time_span_get(LttTrace
*t
, LttTime
*start
, LttTime
*end
)
1475 LttTime min_start
= ltt_time_infinite
;
1476 LttTime max_end
= ltt_time_zero
;
1477 struct tracefile_time_span_get_args args
= { t
, &min_start
, &max_end
};
1479 g_datalist_foreach(&t
->tracefiles
, &group_time_span_get
, &args
);
1481 if(start
!= NULL
) *start
= min_start
;
1482 if(end
!= NULL
) *end
= max_end
;
1487 /*****************************************************************************
1488 *Get the name of a tracefile
1489 ****************************************************************************/
1491 GQuark
ltt_tracefile_name(const LttTracefile
*tf
)
1496 GQuark
ltt_tracefile_long_name(const LttTracefile
*tf
)
1498 return tf
->long_name
;
1503 guint
ltt_tracefile_cpu(LttTracefile
*tf
)
1508 guint
ltt_tracefile_tid(LttTracefile
*tf
)
1513 guint
ltt_tracefile_pgid(LttTracefile
*tf
)
1518 guint64
ltt_tracefile_creation(LttTracefile
*tf
)
1520 return tf
->creation
;
1522 /*****************************************************************************
1523 * Get the number of blocks in the tracefile
1524 ****************************************************************************/
1526 guint
ltt_tracefile_block_number(LttTracefile
*tf
)
1528 return tf
->num_blocks
;
1532 /* Seek to the first event in a tracefile that has a time equal or greater than
1533 * the time passed in parameter.
1535 * If the time parameter is outside the tracefile time span, seek to the first
1536 * event or if after, return ERANGE.
1538 * If the time parameter is before the first event, we have to seek specially to
1541 * If the time is after the end of the trace, return ERANGE.
1543 * Do a binary search to find the right block, then a sequential search in the
1544 * block to find the event.
1546 * In the special case where the time requested fits inside a block that has no
1547 * event corresponding to the requested time, the first event of the next block
1550 * IMPORTANT NOTE : // FIXME everywhere...
1552 * You MUST NOT do a ltt_tracefile_read right after a ltt_tracefile_seek_time :
1553 * you will jump over an event if you do.
1555 * Return value : 0 : no error, the tf->event can be used
1556 * ERANGE : time if after the last event of the trace
1557 * otherwise : this is an error.
1561 int ltt_tracefile_seek_time(LttTracefile
*tf
, LttTime time
)
1565 unsigned int block_num
, high
, low
;
1567 /* seek at the beginning of trace */
1568 err
= map_block(tf
, 0); /* First block */
1570 g_error("Can not map block");
1574 /* If the time is lower or equal the beginning of the trace,
1575 * go to the first event. */
1576 if(ltt_time_compare(time
, tf
->buffer
.begin
.timestamp
) <= 0) {
1577 ret
= ltt_tracefile_read(tf
);
1578 if(ret
== ERANGE
) goto range
;
1579 else if (ret
) goto fail
;
1580 goto found
; /* There is either no event in the trace or the event points
1581 to the first event in the trace */
1584 err
= map_block(tf
, tf
->num_blocks
- 1); /* Last block */
1586 g_error("Can not map block");
1590 /* If the time is after the end of the trace, return ERANGE. */
1591 if(ltt_time_compare(time
, tf
->buffer
.end
.timestamp
) > 0) {
1595 /* Binary search the block */
1596 high
= tf
->num_blocks
- 1;
1600 block_num
= ((high
-low
) / 2) + low
;
1602 err
= map_block(tf
, block_num
);
1604 g_error("Can not map block");
1608 /* We cannot divide anymore : this is what would happen if the time
1609 * requested was exactly between two consecutive buffers'end and start
1610 * timestamps. This is also what would happend if we didn't deal with out
1611 * of span cases prior in this function. */
1612 /* The event is right in the buffer!
1613 * (or in the next buffer first event) */
1615 ret
= ltt_tracefile_read(tf
);
1616 if(ret
== ERANGE
) goto range
; /* ERANGE or EPERM */
1617 else if(ret
) goto fail
;
1619 if(ltt_time_compare(time
, tf
->event
.event_time
) <= 0)
1623 } else if(ltt_time_compare(time
, tf
->buffer
.begin
.timestamp
) < 0) {
1624 /* go to lower part */
1625 high
= block_num
- 1;
1626 } else if(ltt_time_compare(time
, tf
->buffer
.end
.timestamp
) > 0) {
1627 /* go to higher part */
1628 low
= block_num
+ 1;
1629 } else {/* The event is right in the buffer!
1630 (or in the next buffer first event) */
1632 ret
= ltt_tracefile_read(tf
);
1633 if(ret
== ERANGE
) goto range
; /* ERANGE or EPERM */
1634 else if(ret
) goto fail
;
1636 if(ltt_time_compare(time
, tf
->event
.event_time
) <= 0)
1648 /* Error handling */
1650 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1651 g_quark_to_string(tf
->name
));
1656 int ltt_tracefile_seek_position(LttTracefile
*tf
, const LttEventPosition
*ep
) {
1660 if(ep
->tracefile
!= tf
) {
1664 err
= map_block(tf
, ep
->block
);
1666 g_error("Can not map block");
1670 tf
->event
.offset
= ep
->offset
;
1672 /* Put back the event real tsc */
1673 tf
->event
.tsc
= ep
->tsc
;
1674 tf
->buffer
.tsc
= ep
->tsc
;
1676 err
= ltt_tracefile_read_update_event(tf
);
1678 err
= ltt_tracefile_read_op(tf
);
1684 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1685 g_quark_to_string(tf
->name
));
1689 LttTime
ltt_interpolate_time_from_tsc(LttTracefile
*tf
, guint64 tsc
)
1693 if(tsc
> tf
->trace
->start_tsc
) {
1694 time
= ltt_time_from_uint64(
1695 (double)(tsc
- tf
->trace
->start_tsc
)
1696 * (1000000000.0 / tf
->trace
->freq_scale
)
1697 / (double)tf
->trace
->start_freq
);
1698 time
= ltt_time_add(tf
->trace
->start_time_from_tsc
, time
);
1700 time
= ltt_time_from_uint64(
1701 (double)(tf
->trace
->start_tsc
- tsc
)
1702 * (1000000000.0 / tf
->trace
->freq_scale
)
1703 / (double)tf
->trace
->start_freq
);
1704 time
= ltt_time_sub(tf
->trace
->start_time_from_tsc
, time
);
1709 /* Calculate the real event time based on the buffer boundaries */
1710 LttTime
ltt_interpolate_time(LttTracefile
*tf
, LttEvent
*event
)
1712 return ltt_interpolate_time_from_tsc(tf
, tf
->buffer
.tsc
);
1716 /* Get the current event of the tracefile : valid until the next read */
1717 LttEvent
*ltt_tracefile_get_event(LttTracefile
*tf
)
1724 /*****************************************************************************
1726 * ltt_tracefile_read : Read the next event in the tracefile
1731 * Returns 0 if an event can be used in tf->event.
1732 * Returns ERANGE on end of trace. The event in tf->event still can be used
1733 * (if the last block was not empty).
1734 * Returns EPERM on error.
1736 * This function does make the tracefile event structure point to the event
1737 * currently pointed to by the tf->event.
1739 * Note : you must call a ltt_tracefile_seek to the beginning of the trace to
1740 * reinitialize it after an error if you want results to be coherent.
1741 * It would be the case if a end of trace last buffer has no event : the end
1742 * of trace wouldn't be returned, but an error.
1743 * We make the assumption there is at least one event per buffer.
1744 ****************************************************************************/
1746 int ltt_tracefile_read(LttTracefile
*tf
)
1750 err
= ltt_tracefile_read_seek(tf
);
1752 err
= ltt_tracefile_read_update_event(tf
);
1754 err
= ltt_tracefile_read_op(tf
);
1760 int ltt_tracefile_read_seek(LttTracefile
*tf
)
1764 /* Get next buffer until we finally have an event, or end of trace */
1766 err
= ltt_seek_next_event(tf
);
1767 if(unlikely(err
== ENOPROTOOPT
)) {
1771 /* Are we at the end of the buffer ? */
1773 if(unlikely(tf
->buffer
.index
== tf
->num_blocks
-1)){ /* end of trace ? */
1776 /* get next block */
1777 err
= map_block(tf
, tf
->buffer
.index
+ 1);
1779 g_error("Can not map block");
1783 } else break; /* We found an event ! */
1790 /* do specific operation on events */
1791 int ltt_tracefile_read_op(LttTracefile
*tf
)
1797 /* do event specific operation */
1799 /* do something if its an heartbeat event : increment the heartbeat count */
1800 //if(event->facility_id == LTT_FACILITY_CORE)
1801 // if(event->event_id == LTT_EVENT_HEARTBEAT)
1802 // tf->cur_heart_beat_number++;
1808 /* same as ltt_tracefile_read, but does not seek to the next event nor call
1809 * event specific operation. */
1810 int ltt_tracefile_read_update_event(LttTracefile
*tf
)
1816 pos
= tf
->buffer
.head
+ event
->offset
;
1818 /* Read event header */
1820 /* Align the head */
1821 pos
+= ltt_align((size_t)pos
, tf
->trace
->arch_size
, tf
->has_alignment
);
1823 if(tf
->trace
->has_heartbeat
) {
1824 event
->timestamp
= ltt_get_uint32(LTT_GET_BO(tf
),
1826 /* 32 bits -> 64 bits tsc */
1827 /* note : still works for seek and non seek cases. */
1828 if(event
->timestamp
< (0xFFFFFFFFULL
&tf
->buffer
.tsc
)) {
1829 tf
->buffer
.tsc
= ((tf
->buffer
.tsc
&0xFFFFFFFF00000000ULL
)
1831 | (guint64
)event
->timestamp
;
1832 event
->tsc
= tf
->buffer
.tsc
;
1835 tf
->buffer
.tsc
= (tf
->buffer
.tsc
&0xFFFFFFFF00000000ULL
)
1836 | (guint64
)event
->timestamp
;
1837 event
->tsc
= tf
->buffer
.tsc
;
1839 pos
+= sizeof(guint32
);
1841 event
->tsc
= ltt_get_uint64(LTT_GET_BO(tf
), pos
);
1842 tf
->buffer
.tsc
= event
->tsc
;
1843 pos
+= sizeof(guint64
);
1845 event
->event_time
= ltt_interpolate_time(tf
, event
);
1846 event
->facility_id
= *(guint8
*)pos
;
1847 pos
+= sizeof(guint8
);
1849 event
->event_id
= *(guint8
*)pos
;
1850 pos
+= sizeof(guint8
);
1852 event
->event_size
= ltt_get_uint16(LTT_GET_BO(tf
), pos
);
1853 pos
+= sizeof(guint16
);
1855 /* Align the head */
1856 pos
+= ltt_align((size_t)pos
, tf
->trace
->arch_size
, tf
->has_alignment
);
1860 /* get the data size and update the event fields with the current
1862 ltt_update_event_size(tf
);
1868 /****************************************************************************
1870 * map_block : map a block from the file
1872 * lttdes : ltt trace file
1873 * whichBlock : the block which will be read
1876 * EINVAL : lseek fail
1877 * EIO : can not read from the file
1878 ****************************************************************************/
1880 static gint
map_block(LttTracefile
* tf
, guint block_num
)
1882 int page_size
= getpagesize();
1883 struct ltt_block_start_header
*header
;
1885 g_assert(block_num
< tf
->num_blocks
);
1887 if(tf
->buffer
.head
!= NULL
) {
1888 if(munmap(tf
->buffer
.head
, PAGE_ALIGN(tf
->buf_size
))) {
1889 g_warning("unmap size : %u\n",
1890 PAGE_ALIGN(tf
->buf_size
));
1891 perror("munmap error");
1897 /* Multiple of pages aligned head */
1898 tf
->buffer
.head
= mmap(0,
1899 PAGE_ALIGN(tf
->buf_size
),
1900 PROT_READ
, MAP_PRIVATE
, tf
->fd
,
1901 PAGE_ALIGN((off_t
)tf
->buf_size
* (off_t
)block_num
));
1903 if(tf
->buffer
.head
== MAP_FAILED
) {
1904 perror("Error in allocating memory for buffer of tracefile");
1908 g_assert( ( (guint
)tf
->buffer
.head
&(8-1) ) == 0); // make sure it's aligned.
1911 tf
->buffer
.index
= block_num
;
1913 header
= (struct ltt_block_start_header
*)tf
->buffer
.head
;
1916 tf
->buffer
.begin
.timestamp
= ltt_time_add(
1917 ltt_time_from_uint64(
1918 ltt_get_uint64(LTT_GET_BO(tf
),
1919 &header
->begin
.timestamp
)
1920 - tf
->trace
->start_monotonic
),
1921 tf
->trace
->start_time
);
1923 //g_debug("block %u begin : %lu.%lu", block_num,
1924 // tf->buffer.begin.timestamp.tv_sec, tf->buffer.begin.timestamp.tv_nsec);
1925 tf
->buffer
.begin
.cycle_count
= ltt_get_uint64(LTT_GET_BO(tf
),
1926 &header
->begin
.cycle_count
);
1927 tf
->buffer
.begin
.freq
= ltt_get_uint64(LTT_GET_BO(tf
),
1928 &header
->begin
.freq
);
1929 if(tf
->buffer
.begin
.freq
== 0)
1930 tf
->buffer
.begin
.freq
= tf
->trace
->start_freq
;
1932 tf
->buffer
.begin
.timestamp
= ltt_interpolate_time_from_tsc(tf
,
1933 tf
->buffer
.begin
.cycle_count
);
1936 ltt_time_from_uint64(
1937 (double)(tf
->buffer
.begin
.cycle_count
1938 - tf
->trace
->start_tsc
) * 1000000.0
1939 / (double)tf
->trace
->start_freq
),
1940 tf
->trace
->start_time_from_tsc
);
1944 tf
->buffer
.end
.timestamp
= ltt_time_add(
1945 ltt_time_from_uint64(
1946 ltt_get_uint64(LTT_GET_BO(tf
),
1947 &header
->end
.timestamp
)
1948 - tf
->trace
->start_monotonic
),
1949 tf
->trace
->start_time
);
1951 //g_debug("block %u end : %lu.%lu", block_num,
1952 // tf->buffer.end.timestamp.tv_sec, tf->buffer.end.timestamp.tv_nsec);
1953 tf
->buffer
.end
.cycle_count
= ltt_get_uint64(LTT_GET_BO(tf
),
1954 &header
->end
.cycle_count
);
1955 tf
->buffer
.end
.freq
= ltt_get_uint64(LTT_GET_BO(tf
),
1957 if(tf
->buffer
.end
.freq
== 0)
1958 tf
->buffer
.end
.freq
= tf
->trace
->start_freq
;
1960 tf
->buffer
.lost_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1961 &header
->lost_size
);
1962 tf
->buffer
.end
.timestamp
= ltt_interpolate_time_from_tsc(tf
,
1963 tf
->buffer
.end
.cycle_count
);
1966 ltt_time_from_uint64(
1967 (double)(tf
->buffer
.end
.cycle_count
1968 - tf
->trace
->start_tsc
) * 1000000.0
1969 / (double)tf
->trace
->start_freq
),
1970 tf
->trace
->start_time_from_tsc
);
1972 tf
->buffer
.tsc
= tf
->buffer
.begin
.cycle_count
;
1973 tf
->event
.tsc
= tf
->buffer
.tsc
;
1974 tf
->buffer
.freq
= tf
->buffer
.begin
.freq
;
1977 * eventually support variable buffer size : will need a partial pre-read of
1978 * the headers to create an index when we open the trace... eventually. */
1979 g_assert(tf
->buf_size
== ltt_get_uint32(LTT_GET_BO(tf
),
1980 &header
->buf_size
));
1982 /* Now that the buffer is mapped, calculate the time interpolation for the
1985 // tf->buffer.nsecs_per_cycle = calc_nsecs_per_cycle(tf);
1986 //tf->buffer.cyc2ns_scale = calc_nsecs_per_cycle(tf);
1988 /* Make the current event point to the beginning of the buffer :
1989 * it means that the event read must get the first event. */
1990 tf
->event
.tracefile
= tf
;
1991 tf
->event
.block
= block_num
;
1992 tf
->event
.offset
= 0;
2001 /* It will update the fields offsets too */
2002 void ltt_update_event_size(LttTracefile
*tf
)
2005 LttFacility
*f
= ltt_trace_get_facility_by_num(tf
->trace
,
2006 tf
->event
.facility_id
);
2009 /* Specific handling of core events : necessary to read the facility control
2012 if(likely(tf
->event
.facility_id
== LTT_FACILITY_CORE
)) {
2013 switch((enum ltt_core_events
)tf
->event
.event_id
) {
2014 case LTT_EVENT_FACILITY_LOAD
:
2015 size
= strlen((char*)tf
->event
.data
) + 1;
2016 //g_debug("Update Event facility load of facility %s", (char*)tf->event.data);
2017 size
+= ltt_align(size
, sizeof(guint32
), tf
->has_alignment
);
2018 size
+= sizeof(struct LttFacilityLoad
);
2020 case LTT_EVENT_FACILITY_UNLOAD
:
2021 //g_debug("Update Event facility unload");
2022 size
= sizeof(struct LttFacilityUnload
);
2024 case LTT_EVENT_STATE_DUMP_FACILITY_LOAD
:
2025 size
= strlen((char*)tf
->event
.data
) + 1;
2026 size
+= ltt_align(size
, sizeof(guint32
), tf
->has_alignment
);
2027 //g_debug("Update Event facility load state dump of facility %s",
2028 // (char*)tf->event.data);
2029 size
+= sizeof(struct LttStateDumpFacilityLoad
);
2031 case LTT_EVENT_HEARTBEAT
:
2032 //g_debug("Update Event heartbeat");
2033 size
= sizeof(TimeHeartbeat
);
2036 g_warning("Error in getting event size : tracefile %s, "
2037 "unknown event id %hhu in core facility.",
2038 g_quark_to_string(tf
->name
),
2039 tf
->event
.event_id
);
2040 goto event_id_error
;
2043 goto no_offset
; /* Skip the field computation */
2045 g_warning("Unknown facility %hhu (0x%hhx) in tracefile %s",
2046 tf
->event
.facility_id
,
2047 tf
->event
.facility_id
,
2048 g_quark_to_string(tf
->name
));
2049 goto facility_error
;
2053 LttEventType
*event_type
=
2054 ltt_facility_eventtype_get(f
, tf
->event
.event_id
);
2057 g_warning("Unknown event id %hhu in facility %s in tracefile %s",
2059 g_quark_to_string(f
->name
),
2060 g_quark_to_string(tf
->name
));
2061 goto event_type_error
;
2064 /* Compute the dynamic offsets */
2065 compute_offsets(tf
, f
, event_type
, &size
, tf
->event
.data
);
2067 //g_debug("Event root field : f.e %hhu.%hhu size %zd",
2068 // tf->event.facility_id,
2069 // tf->event.event_id, size);
2072 tf
->event
.data_size
= size
;
2074 /* Check consistency between kernel and LTTV structure sizes */
2075 if(tf
->event
.event_size
== 0xFFFF) {
2076 /* Event size too big to fit in the event size field */
2077 tf
->event
.event_size
= tf
->event
.data_size
;
2079 g_assert(tf
->event
.data_size
== tf
->event
.event_size
);
2086 if(tf
->event
.event_size
== 0xFFFF) {
2087 g_error("Cannot jump over an unknown event bigger than 0xFFFE bytes");
2089 /* The facility is unknown : use the kernel information about this event
2090 * to jump over it. */
2091 tf
->event
.data_size
= tf
->event
.event_size
;
2095 /* Take the tf current event offset and use the event facility id and event id
2096 * to figure out where is the next event offset.
2098 * This is an internal function not aiming at being used elsewhere : it will
2099 * not jump over the current block limits. Please consider using
2100 * ltt_tracefile_read to do this.
2102 * Returns 0 on success
2103 * ERANGE if we are at the end of the buffer.
2104 * ENOPROTOOPT if an error occured when getting the current event size.
2106 static int ltt_seek_next_event(LttTracefile
*tf
)
2111 /* seek over the buffer header if we are at the buffer start */
2112 if(tf
->event
.offset
== 0) {
2113 tf
->event
.offset
+= tf
->buffer_header_size
;
2115 if(tf
->event
.offset
== tf
->buf_size
- tf
->buffer
.lost_size
) {
2122 pos
= tf
->event
.data
;
2124 if(tf
->event
.data_size
< 0) goto error
;
2126 pos
+= (size_t)tf
->event
.data_size
;
2128 tf
->event
.offset
= pos
- tf
->buffer
.head
;
2130 if(tf
->event
.offset
== tf
->buf_size
- tf
->buffer
.lost_size
) {
2134 g_assert(tf
->event
.offset
< tf
->buf_size
- tf
->buffer
.lost_size
);
2140 g_error("Error in ltt_seek_next_event for tracefile %s",
2141 g_quark_to_string(tf
->name
));
2146 /*****************************************************************************
2148 * calc_nsecs_per_cycle : calculate nsecs per cycle for current block
2150 * 1.0 / (freq(khz) *1000) * 1000000000
2153 ****************************************************************************/
2154 /* from timer_tsc.c */
2155 #define CYC2NS_SCALE_FACTOR 10
2156 static guint32
calc_nsecs_per_cycle(LttTracefile
* tf
)
2158 //return 1e6 / (double)tf->buffer.freq;
2159 guint32 cpu_mhz
= tf
->buffer
.freq
/ 1000;
2160 guint32 cyc2ns_scale
= (1000 << CYC2NS_SCALE_FACTOR
)/cpu_mhz
;
2162 return cyc2ns_scale
;
2163 // return 1e6 / (double)tf->buffer.freq;
2166 static guint64
cycles_2_ns(LttTracefile
*tf
, guint64 cycles
)
2168 return (cycles
* tf
->buffer
.cyc2ns_scale
) >> CYC2NS_SCALE_FACTOR
;
2173 void setFieldsOffset(LttTracefile
*tf
, LttEventType
*evT
,void *evD
)
2175 LttField
* rootFld
= evT
->root_field
;
2176 // rootFld->base_address = evD;
2179 rootFld
->field_size
= getFieldtypeSize(tf
, evT
->facility
,
2180 evT
, 0,0,rootFld
, evD
);
2184 /*****************************************************************************
2186 * set_fields_offsets : set the precomputable offset of the fields
2188 * tracefile : opened trace file
2189 * event_type : the event type
2190 ****************************************************************************/
2192 void set_fields_offsets(LttTracefile
*tf
, LttEventType
*event_type
)
2194 LttField
*field
= event_type
->root_field
;
2195 enum field_status fixed_root
= FIELD_FIXED
, fixed_parent
= FIELD_FIXED
;
2198 preset_field_type_size(tf
, event_type
, 0, 0,
2199 &fixed_root
, &fixed_parent
,
2206 /*****************************************************************************
2208 * get_alignment : Get the alignment needed for a field.
2212 * returns : The size on which it must be aligned.
2214 ****************************************************************************/
2215 off_t
get_alignment(LttField
*field
)
2217 LttType
*type
= &field
->field_type
;
2219 switch(type
->type_class
) {
2221 case LTT_UINT_FIXED
:
2236 /* Align offset on type size */
2237 g_assert(field
->field_size
!= 0);
2238 return field
->field_size
;
2244 g_assert(type
->fields
->len
== 1);
2246 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
2247 return get_alignment(child
);
2251 g_assert(type
->fields
->len
== 2);
2254 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
2256 localign
= max(localign
, get_alignment(child
));
2258 child
= &g_array_index(type
->fields
, LttField
, 1);
2259 localign
= max(localign
, get_alignment(child
));
2270 for(i
=0; i
<type
->fields
->len
; i
++) {
2271 LttField
*child
= &g_array_index(type
->fields
, LttField
, i
);
2272 localign
= max(localign
, get_alignment(child
));
2279 g_error("get_alignment : unknown type");
2284 /*****************************************************************************
2286 * field_compute_static_size : Determine the size of fields known by their
2287 * sole definition. Unions, arrays and struct sizes might be known, but
2288 * the parser does not give that information.
2293 ****************************************************************************/
2295 void field_compute_static_size(LttFacility
*fac
, LttField
*field
)
2297 LttType
*type
= &field
->field_type
;
2299 switch(type
->type_class
) {
2301 case LTT_UINT_FIXED
:
2320 /* note this : array type size is the number of elements in the array,
2321 * while array field size of the length of the array in bytes */
2322 g_assert(type
->fields
->len
== 1);
2324 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
2325 field_compute_static_size(fac
, child
);
2327 if(child
->field_size
!= 0) {
2328 field
->field_size
= type
->size
* child
->field_size
;
2329 field
->dynamic_offsets
= g_array_sized_new(FALSE
, TRUE
,
2330 sizeof(off_t
), type
->size
);
2332 field
->field_size
= 0;
2337 g_assert(type
->fields
->len
== 2);
2339 off_t local_offset
= 0;
2340 LttField
*child
= &g_array_index(type
->fields
, LttField
, 1);
2341 field_compute_static_size(fac
, child
);
2342 field
->field_size
= 0;
2344 if(child
->field_size
!= 0) {
2345 field
->dynamic_offsets
= g_array_sized_new(FALSE
, TRUE
,
2346 sizeof(off_t
), SEQUENCE_AVG_ELEMENTS
);
2354 for(i
=0;i
<type
->fields
->len
;i
++) {
2355 LttField
*child
= &g_array_index(type
->fields
, LttField
, i
);
2356 field_compute_static_size(fac
, child
);
2357 if(child
->field_size
!= 0) {
2358 type
->size
+= ltt_align(type
->size
, get_alignment(child
),
2360 type
->size
+= child
->field_size
;
2362 /* As soon as we find a child with variable size, we have
2363 * a variable size */
2368 field
->field_size
= type
->size
;
2372 g_error("field_static_size : unknown type");
2379 /*****************************************************************************
2381 * precompute_fields_offsets : set the precomputable offset of the fields
2385 * offset : pointer to the current offset, must be incremented
2387 * return : 1 : found a variable length field, stop the processing.
2389 ****************************************************************************/
2392 gint
precompute_fields_offsets(LttFacility
*fac
, LttField
*field
, off_t
*offset
)
2394 LttType
*type
= &field
->field_type
;
2396 switch(type
->type_class
) {
2398 case LTT_UINT_FIXED
:
2413 g_assert(field
->field_size
!= 0);
2414 /* Align offset on type size */
2415 *offset
+= ltt_align(*offset
, get_alignment(field
),
2417 /* remember offset */
2418 field
->offset_root
= *offset
;
2419 field
->fixed_root
= FIELD_FIXED
;
2420 /* Increment offset */
2421 *offset
+= field
->field_size
;
2425 field
->offset_root
= *offset
;
2426 field
->fixed_root
= FIELD_FIXED
;
2430 g_assert(type
->fields
->len
== 1);
2432 LttField
*child
= &g_array_index(type
->fields
, LttField
, 0);
2434 *offset
+= ltt_align(*offset
, get_alignment(field
),
2437 /* remember offset */
2438 field
->offset_root
= *offset
;
2439 field
->array_offset
= *offset
;
2440 field
->fixed_root
= FIELD_FIXED
;
2442 /* Let the child be variable */
2443 //precompute_fields_offsets(tf, child, offset);
2445 if(field
->field_size
!= 0) {
2446 /* Increment offset */
2447 /* field_size is the array size in bytes */
2448 *offset
+= field
->field_size
;
2456 g_assert(type
->fields
->len
== 2);
2461 *offset
+= ltt_align(*offset
, get_alignment(field
),
2464 /* remember offset */
2465 field
->offset_root
= *offset
;
2466 field
->fixed_root
= FIELD_FIXED
;
2468 child
= &g_array_index(type
->fields
, LttField
, 0);
2469 ret
= precompute_fields_offsets(fac
, child
, offset
);
2470 g_assert(ret
== 0); /* Seq len cannot have variable len */
2472 child
= &g_array_index(type
->fields
, LttField
, 1);
2473 *offset
+= ltt_align(*offset
, get_alignment(child
),
2475 field
->array_offset
= *offset
;
2476 /* Let the child be variable. */
2477 //ret = precompute_fields_offsets(fac, child, offset);
2479 /* Cannot precompute fields offsets of sequence members, and has
2480 * variable length. */
2490 *offset
+= ltt_align(*offset
, get_alignment(field
),
2492 /* remember offset */
2493 field
->offset_root
= *offset
;
2494 field
->fixed_root
= FIELD_FIXED
;
2496 for(i
=0; i
< type
->fields
->len
; i
++) {
2497 child
= &g_array_index(type
->fields
, LttField
, i
);
2498 ret
= precompute_fields_offsets(fac
, child
, offset
);
2511 *offset
+= ltt_align(*offset
, get_alignment(field
),
2513 /* remember offset */
2514 field
->offset_root
= *offset
;
2515 field
->fixed_root
= FIELD_FIXED
;
2517 for(i
=0; i
< type
->fields
->len
; i
++) {
2518 *offset
= field
->offset_root
;
2519 child
= &g_array_index(type
->fields
, LttField
, i
);
2520 ret
= precompute_fields_offsets(fac
, child
, offset
);
2524 *offset
= field
->offset_root
+ field
->field_size
;
2531 g_error("precompute_fields_offsets : unknown type");
2538 /*****************************************************************************
2540 * precompute_offsets : set the precomputable offset of an event type
2543 * event : event type
2545 ****************************************************************************/
2546 void precompute_offsets(LttFacility
*fac
, LttEventType
*event
)
2552 /* First, compute the size of fixed size fields. Will determine size for
2553 * arrays, struct and unions, which is not done by the parser */
2554 for(i
=0; i
<event
->fields
->len
; i
++) {
2555 LttField
*field
= &g_array_index(event
->fields
, LttField
, i
);
2556 field_compute_static_size(fac
, field
);
2559 /* Precompute all known offsets */
2560 for(i
=0; i
<event
->fields
->len
; i
++) {
2561 LttField
*field
= &g_array_index(event
->fields
, LttField
, i
);
2562 ret
= precompute_fields_offsets(fac
, field
, &offset
);
2570 /*****************************************************************************
2572 * preset_field_type_size : set the fixed sizes of the field type
2575 * event_type : event type
2576 * offset_root : offset from the root
2577 * offset_parent : offset from the parent
2578 * fixed_root : Do we know a fixed offset to the root ?
2579 * fixed_parent : Do we know a fixed offset to the parent ?
2581 ****************************************************************************/
2585 // preset the fixed size offsets. Calculate them just like genevent-new : an
2586 // increment of a *to value that represents the offset from the start of the
2588 // The preset information is : offsets up to (and including) the first element
2589 // of variable size. All subsequent fields must be flagged "VARIABLE OFFSET".
2591 void preset_field_type_size(LttTracefile
*tf
, LttEventType
*event_type
,
2592 off_t offset_root
, off_t offset_parent
,
2593 enum field_status
*fixed_root
, enum field_status
*fixed_parent
,
2596 enum field_status local_fixed_root
, local_fixed_parent
;
2600 g_assert(field
->fixed_root
== FIELD_UNKNOWN
);
2601 g_assert(field
->fixed_parent
== FIELD_UNKNOWN
);
2602 g_assert(field
->fixed_size
== FIELD_UNKNOWN
);
2604 type
= field
->field_type
;
2606 field
->fixed_root
= *fixed_root
;
2607 if(field
->fixed_root
== FIELD_FIXED
)
2608 field
->offset_root
= offset_root
;
2610 field
->offset_root
= 0;
2612 field
->fixed_parent
= *fixed_parent
;
2613 if(field
->fixed_parent
== FIELD_FIXED
)
2614 field
->offset_parent
= offset_parent
;
2616 field
->offset_parent
= 0;
2618 size_t current_root_offset
;
2619 size_t current_offset
;
2620 enum field_status current_child_status
, final_child_status
;
2623 switch(type
->type_class
) {
2625 case LTT_UINT_FIXED
:
2634 field
->field_size
= ltt_type_size(tf
->trace
, type
);
2635 field
->fixed_size
= FIELD_FIXED
;
2638 field
->field_size
= (off_t
)event_type
->facility
->pointer_size
;
2639 field
->fixed_size
= FIELD_FIXED
;
2643 field
->field_size
= (off_t
)event_type
->facility
->long_size
;
2644 field
->fixed_size
= FIELD_FIXED
;
2649 field
->field_size
= (off_t
)event_type
->facility
->size_t_size
;
2650 field
->fixed_size
= FIELD_FIXED
;
2653 local_fixed_root
= FIELD_VARIABLE
;
2654 local_fixed_parent
= FIELD_VARIABLE
;
2655 preset_field_type_size(tf
, event_type
,
2657 &local_fixed_root
, &local_fixed_parent
,
2659 field
->fixed_size
= FIELD_VARIABLE
;
2660 field
->field_size
= 0;
2661 *fixed_root
= FIELD_VARIABLE
;
2662 *fixed_parent
= FIELD_VARIABLE
;
2665 field
->fixed_size
= FIELD_VARIABLE
;
2666 field
->field_size
= 0;
2667 *fixed_root
= FIELD_VARIABLE
;
2668 *fixed_parent
= FIELD_VARIABLE
;
2671 local_fixed_root
= FIELD_VARIABLE
;
2672 local_fixed_parent
= FIELD_VARIABLE
;
2673 preset_field_type_size(tf
, event_type
,
2675 &local_fixed_root
, &local_fixed_parent
,
2677 field
->fixed_size
= field
->child
[0]->fixed_size
;
2678 if(field
->fixed_size
== FIELD_FIXED
) {
2679 field
->field_size
= type
->element_number
* field
->child
[0]->field_size
;
2681 field
->field_size
= 0;
2682 *fixed_root
= FIELD_VARIABLE
;
2683 *fixed_parent
= FIELD_VARIABLE
;
2687 current_root_offset
= field
->offset_root
;
2689 current_child_status
= FIELD_FIXED
;
2690 for(i
=0;i
<type
->element_number
;i
++) {
2691 preset_field_type_size(tf
, event_type
,
2692 current_root_offset
, current_offset
,
2693 fixed_root
, ¤t_child_status
,
2695 if(current_child_status
== FIELD_FIXED
) {
2696 current_root_offset
+= field
->child
[i
]->field_size
;
2697 current_offset
+= field
->child
[i
]->field_size
;
2699 current_root_offset
= 0;
2703 if(current_child_status
!= FIELD_FIXED
) {
2704 *fixed_parent
= current_child_status
;
2705 field
->field_size
= 0;
2706 field
->fixed_size
= current_child_status
;
2708 field
->field_size
= current_offset
;
2709 field
->fixed_size
= FIELD_FIXED
;
2713 current_root_offset
= field
->offset_root
;
2716 final_child_status
= FIELD_FIXED
;
2717 for(i
=0;i
<type
->element_number
;i
++) {
2718 enum field_status current_root_child_status
= FIELD_FIXED
;
2719 enum field_status current_child_status
= FIELD_FIXED
;
2720 preset_field_type_size(tf
, event_type
,
2721 current_root_offset
, current_offset
,
2722 ¤t_root_child_status
, ¤t_child_status
,
2724 if(current_child_status
!= FIELD_FIXED
)
2725 final_child_status
= current_child_status
;
2727 max_size
= max(max_size
, field
->child
[i
]->field_size
);
2729 if(final_child_status
!= FIELD_FIXED
) {
2730 g_error("LTTV does not support variable size fields in unions.");
2731 /* This will stop the application. */
2732 *fixed_root
= final_child_status
;
2733 *fixed_parent
= final_child_status
;
2734 field
->field_size
= 0;
2735 field
->fixed_size
= current_child_status
;
2737 field
->field_size
= max_size
;
2738 field
->fixed_size
= FIELD_FIXED
;
2742 g_error("unexpected type NONE");
2749 /*****************************************************************************
2751 * check_fields_compatibility : Check for compatibility between two fields :
2752 * do they use the same inner structure ?
2754 * event_type1 : event type
2755 * event_type2 : event type
2758 *Returns : 0 if identical
2760 ****************************************************************************/
2761 // this function checks for equality of field types. Therefore, it does not use
2762 // per se offsets. For instance, an aligned version of a structure is
2763 // compatible with an unaligned version of the same structure.
2764 gint
check_fields_compatibility(LttEventType
*event_type1
,
2765 LttEventType
*event_type2
,
2766 LttField
*field1
, LttField
*field2
)
2768 guint different
= 0;
2772 if(field1
== NULL
) {
2773 if(field2
== NULL
) goto end
;
2778 } else if(field2
== NULL
) {
2783 type1
= &field1
->field_type
;
2784 type2
= &field2
->field_type
;
2786 if(type1
->type_class
!= type2
->type_class
) {
2790 if(type1
->network
!= type2
->network
) {
2795 switch(type1
->type_class
) {
2797 case LTT_UINT_FIXED
:
2812 if(field1
->field_size
!= field2
->field_size
)
2819 LttField
*child1
= &g_array_index(type1
->fields
, LttField
, 0);
2820 LttField
*child2
= &g_array_index(type2
->fields
, LttField
, 0);
2822 if(type1
->size
!= type2
->size
)
2824 if(check_fields_compatibility(event_type1
, event_type2
, child1
, child2
))
2830 LttField
*child1
= &g_array_index(type1
->fields
, LttField
, 1);
2831 LttField
*child2
= &g_array_index(type2
->fields
, LttField
, 1);
2833 if(check_fields_compatibility(event_type1
, event_type2
, child1
, child2
))
2843 if(type1
->fields
->len
!= type2
->fields
->len
) {
2848 for(i
=0; i
< type1
->fields
->len
; i
++) {
2851 child1
= &g_array_index(type1
->fields
, LttField
, i
);
2852 child2
= &g_array_index(type2
->fields
, LttField
, i
);
2853 different
= check_fields_compatibility(event_type1
,
2854 event_type2
, child1
, child2
);
2856 if(different
) break;
2862 g_error("precompute_fields_offsets : unknown type");
2871 gint
check_fields_compatibility(LttEventType
*event_type1
,
2872 LttEventType
*event_type2
,
2873 LttField
*field1
, LttField
*field2
)
2875 guint different
= 0;
2880 if(field1
== NULL
) {
2881 if(field2
== NULL
) goto end
;
2886 } else if(field2
== NULL
) {
2891 g_assert(field1
->fixed_root
!= FIELD_UNKNOWN
);
2892 g_assert(field2
->fixed_root
!= FIELD_UNKNOWN
);
2893 g_assert(field1
->fixed_parent
!= FIELD_UNKNOWN
);
2894 g_assert(field2
->fixed_parent
!= FIELD_UNKNOWN
);
2895 g_assert(field1
->fixed_size
!= FIELD_UNKNOWN
);
2896 g_assert(field2
->fixed_size
!= FIELD_UNKNOWN
);
2898 type1
= field1
->field_type
;
2899 type2
= field2
->field_type
;
2901 if(type1
->type_class
!= type2
->type_class
) {
2905 if(type1
->element_name
!= type2
->element_name
) {
2910 switch(type1
->type_class
) {
2912 case LTT_UINT_FIXED
:
2927 if(field1
->field_size
!= field2
->field_size
) {
2933 if(type1
->element_number
!= type2
->element_number
) {
2937 for(i
=0;i
<type1
->element_number
;i
++) {
2938 if(type1
->enum_strings
[i
] != type2
->enum_strings
[i
]) {
2945 /* Two elements : size and child */
2946 g_assert(type1
->element_number
!= type2
->element_number
);
2947 for(i
=0;i
<type1
->element_number
;i
++) {
2948 if(check_fields_compatibility(event_type1
, event_type2
,
2949 field1
->child
[0], field2
->child
[0])) {
2958 if(field1
->field_size
!= field2
->field_size
) {
2962 /* Two elements : size and child */
2963 g_assert(type1
->element_number
!= type2
->element_number
);
2964 for(i
=0;i
<type1
->element_number
;i
++) {
2965 if(check_fields_compatibility(event_type1
, event_type2
,
2966 field1
->child
[0], field2
->child
[0])) {
2974 if(type1
->element_number
!= type2
->element_number
) {
2978 for(i
=0;i
<type1
->element_number
;i
++) {
2979 if(check_fields_compatibility(event_type1
, event_type2
,
2980 field1
->child
[0], field2
->child
[0])) {
2993 /*****************************************************************************
2995 * ltt_get_int : get an integer number
2997 * reverse_byte_order: must we reverse the byte order ?
2998 * size : the size of the integer
2999 * ptr : the data pointer
3001 * gint64 : a 64 bits integer
3002 ****************************************************************************/
3004 gint64
ltt_get_int(gboolean reverse_byte_order
, gint size
, void *data
)
3009 case 1: val
= *((gint8
*)data
); break;
3010 case 2: val
= ltt_get_int16(reverse_byte_order
, data
); break;
3011 case 4: val
= ltt_get_int32(reverse_byte_order
, data
); break;
3012 case 8: val
= ltt_get_int64(reverse_byte_order
, data
); break;
3013 default: val
= ltt_get_int64(reverse_byte_order
, data
);
3014 g_critical("get_int : integer size %d unknown", size
);
3021 /*****************************************************************************
3023 * ltt_get_uint : get an unsigned integer number
3025 * reverse_byte_order: must we reverse the byte order ?
3026 * size : the size of the integer
3027 * ptr : the data pointer
3029 * guint64 : a 64 bits unsigned integer
3030 ****************************************************************************/
3032 guint64
ltt_get_uint(gboolean reverse_byte_order
, gint size
, void *data
)
3037 case 1: val
= *((gint8
*)data
); break;
3038 case 2: val
= ltt_get_uint16(reverse_byte_order
, data
); break;
3039 case 4: val
= ltt_get_uint32(reverse_byte_order
, data
); break;
3040 case 8: val
= ltt_get_uint64(reverse_byte_order
, data
); break;
3041 default: val
= ltt_get_uint64(reverse_byte_order
, data
);
3042 g_critical("get_uint : unsigned integer size %d unknown",
3051 /* get the node name of the system */
3053 char * ltt_trace_system_description_node_name (LttSystemDescription
* s
)
3055 return s
->node_name
;
3059 /* get the domain name of the system */
3061 char * ltt_trace_system_description_domain_name (LttSystemDescription
* s
)
3063 return s
->domain_name
;
3067 /* get the description of the system */
3069 char * ltt_trace_system_description_description (LttSystemDescription
* s
)
3071 return s
->description
;
3075 /* get the NTP corrected start time of the trace */
3076 LttTime
ltt_trace_start_time(LttTrace
*t
)
3078 return t
->start_time
;
3081 /* get the monotonic start time of the trace */
3082 LttTime
ltt_trace_start_time_monotonic(LttTrace
*t
)
3084 return t
->start_time_from_tsc
;
3087 LttTracefile
*ltt_tracefile_new()
3089 return g_new(LttTracefile
, 1);
3092 void ltt_tracefile_destroy(LttTracefile
*tf
)
3097 void ltt_tracefile_copy(LttTracefile
*dest
, const LttTracefile
*src
)
3102 /* Before library loading... */
3104 static void __attribute__((constructor
)) init(void)
3106 LTT_FACILITY_NAME_HEARTBEAT
= g_quark_from_string("heartbeat");
3107 LTT_EVENT_NAME_HEARTBEAT
= g_quark_from_string("heartbeat");
3109 LTT_TRACEFILE_NAME_FACILITIES
= g_quark_from_string("/control/facilities");