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
;
63 #define __UNUSED__ __attribute__((__unused__))
65 #define g_info(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, format)
66 #define g_debug(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format)
70 /* obtain the time of an event */
72 static inline LttTime
getEventTime(LttTracefile
* tf
);
75 /* set the offset of the fields belonging to the event,
76 need the information of the archecture */
77 void set_fields_offsets(LttTracefile
*tf
, LttEventType
*event_type
);
78 size_t get_fields_offsets(LttTracefile
*tf
, LttEventType
*event_type
, void *data
);
80 /* get the size of the field type according to
81 * The facility size information. */
82 static inline void preset_field_type_size(LttTracefile
*tf
,
83 LttEventType
*event_type
,
84 off_t offset_root
, off_t offset_parent
,
85 enum field_status
*fixed_root
, enum field_status
*fixed_parent
,
89 static inline size_t get_field_type_size(LttTracefile
*tf
,
90 LttEventType
*event_type
,
91 off_t offset_root
, off_t offset_parent
,
92 LttField
*field
, void *data
)
95 /* map a fixed size or a block information from the file (fd) */
96 int map_block(LttTracefile
* tf
, unsigned int block_num
);
98 /* calculate nsec per cycles for current block */
99 static double calc_nsecs_per_cycle(LttTracefile
* t
);
101 /* go to the next event */
102 static int ltt_seek_next_event(LttTracefile
*tf
);
104 /* Functions to parse system.xml file (using glib xml parser) */
105 static void parser_start_element (GMarkupParseContext __UNUSED__
*context
,
106 const gchar
*element_name
,
107 const gchar
**attribute_names
,
108 const gchar
**attribute_values
,
113 LttSystemDescription
* des
= (LttSystemDescription
* )user_data
;
114 if(strcmp("system", element_name
)){
115 *error
= g_error_new(G_MARKUP_ERROR
,
117 "This is not system.xml file");
121 while(attribute_names
[i
]){
122 if(strcmp("node_name", attribute_names
[i
])==0){
123 des
->node_name
= g_strdup(attribute_values
[i
]);
124 }else if(strcmp("domainname", attribute_names
[i
])==0){
125 des
->domain_name
= g_strdup(attribute_values
[i
]);
126 }else if(strcmp("cpu", attribute_names
[i
])==0){
127 des
->nb_cpu
= atoi(attribute_values
[i
]);
128 }else if(strcmp("arch_size", attribute_names
[i
])==0){
129 if(strcmp(attribute_values
[i
],"LP32") == 0) des
->size
= LTT_LP32
;
130 else if(strcmp(attribute_values
[i
],"ILP32") == 0) des
->size
= LTT_ILP32
;
131 else if(strcmp(attribute_values
[i
],"LP64") == 0) des
->size
= LTT_LP64
;
132 else if(strcmp(attribute_values
[i
],"ILP64") == 0) des
->size
= LTT_ILP64
;
133 else if(strcmp(attribute_values
[i
],"UNKNOWN") == 0) des
->size
= LTT_UNKNOWN
;
134 }else if(strcmp("endian", attribute_names
[i
])==0){
135 if(strcmp(attribute_values
[i
],"LITTLE_ENDIAN") == 0)
136 des
->endian
= LTT_LITTLE_ENDIAN
;
137 else if(strcmp(attribute_values
[i
],"BIG_ENDIAN") == 0)
138 des
->endian
= LTT_BIG_ENDIAN
;
139 }else if(strcmp("kernel_name", attribute_names
[i
])==0){
140 des
->kernel_name
= g_strdup(attribute_values
[i
]);
141 }else if(strcmp("kernel_release", attribute_names
[i
])==0){
142 des
->kernel_release
= g_strdup(attribute_values
[i
]);
143 }else if(strcmp("kernel_version", attribute_names
[i
])==0){
144 des
->kernel_version
= g_strdup(attribute_values
[i
]);
145 }else if(strcmp("machine", attribute_names
[i
])==0){
146 des
->machine
= g_strdup(attribute_values
[i
]);
147 }else if(strcmp("processor", attribute_names
[i
])==0){
148 des
->processor
= g_strdup(attribute_values
[i
]);
149 }else if(strcmp("hardware_platform", attribute_names
[i
])==0){
150 des
->hardware_platform
= g_strdup(attribute_values
[i
]);
151 }else if(strcmp("operating_system", attribute_names
[i
])==0){
152 des
->operating_system
= g_strdup(attribute_values
[i
]);
153 }else if(strcmp("ltt_major_version", attribute_names
[i
])==0){
154 des
->ltt_major_version
= atoi(attribute_values
[i
]);
155 }else if(strcmp("ltt_minor_version", attribute_names
[i
])==0){
156 des
->ltt_minor_version
= atoi(attribute_values
[i
]);
157 }else if(strcmp("ltt_block_size", attribute_names
[i
])==0){
158 des
->ltt_block_size
= atoi(attribute_values
[i
]);
160 *error
= g_error_new(G_MARKUP_ERROR
,
162 "Not a valid attribute");
169 static void parser_characters (GMarkupParseContext __UNUSED__
*context
,
171 gsize __UNUSED__ text_len
,
173 GError __UNUSED__
**error
)
175 LttSystemDescription
* des
= (LttSystemDescription
* )user_data
;
176 des
->description
= g_strdup(text
);
179 static inline LttFacility
*ltt_trace_get_facility_by_num(LttTrace
*t
,
182 g_assert(num
< t
->facilities_by_num
->len
);
184 return &g_array_index(t
->facilities_by_num
, LttFacility
, num
);
189 /*****************************************************************************
191 * ltt_tracefile_open : open a trace file, construct a LttTracefile
193 * t : the trace containing the tracefile
194 * fileName : path name of the trace file
195 * tf : the tracefile structure
197 * : 0 for success, -1 otherwise.
198 ****************************************************************************/
200 int ltt_tracefile_open(LttTrace
*t
, gchar
* fileName
, LttTracefile
*tf
)
202 struct stat lTDFStat
; /* Trace data file status */
203 struct ltt_block_start_header
*header
;
206 tf
->name
= g_quark_from_string(fileName
);
208 tf
->fd
= g_open(fileName
, O_RDONLY
, 0);
210 g_warning("Unable to open input data file %s\n", fileName
);
214 // Get the file's status
215 if(fstat(tf
->fd
, &lTDFStat
) < 0){
216 g_warning("Unable to get the status of the input data file %s\n", fileName
);
220 // Is the file large enough to contain a trace
221 if(lTDFStat
.st_size
< (off_t
)(sizeof(BlockStart
))){
222 g_print("The input data file %s does not contain a trace\n", fileName
);
226 /* Temporarily map the buffer start header to get trace information */
227 /* Multiple of pages aligned head */
228 tf
->buffer
.head
= mmap(0, sizeof(struct ltt_block_start_header
), PROT_READ
,
230 if(tf
->buffer
== NULL
) {
231 perror("Error in allocating memory for buffer of tracefile %s\n", fileName
);
234 g_assert(tf
->buffer
.head
& (8-1) == 0); // make sure it's aligned.
236 header
= (struct ltt_block_start_header
*)tf
->buffer
.head
;
238 if(header
->traceset
.magic_number
== LTT_MAGIC_NUMBER
)
240 else if(header
->traceset
.magic_number
== LTT_REV_MAGIC_NUMBER
)
242 else /* invalid magic number, bad tracefile ! */
245 //store the size of the file
246 tf
->file_size
= lTDFStat
.st_size
;
247 tf
->block_size
= header
->buf_size
;
248 tf
->block_number
= tf
->file_size
/ tf
->block_size
;
250 vfree(tf
->buffer
.head
);
251 tf
->buffer
.head
= NULL
;
253 //read the first block
254 if(map_block(tf
,0)) {
255 perror("Cannot map block %u for tracefile %s\n", 0, fileName
);
263 munmap(tf
->buffer
.head
, sizeof(struct ltt_block_start_header
));
271 /*****************************************************************************
272 *Open control and per cpu tracefiles
273 ****************************************************************************/
275 void ltt_tracefile_open_cpu(LttTrace
*t
, gchar
* tracefile_name
)
278 tf
= ltt_tracefile_open(t
,tracefile_name
);
280 t
->per_cpu_tracefile_number
++;
281 g_ptr_array_add(t
->per_cpu_tracefiles
, tf
);
284 gint
ltt_tracefile_open_control(LttTrace
*t
, gchar
* control_name
)
293 tf
= ltt_tracefile_open(t
,control_name
);
295 g_warning("ltt_tracefile_open_control : bad file descriptor");
298 t
->control_tracefile_number
++;
299 g_ptr_array_add(t
->control_tracefiles
,tf
);
301 //parse facilities tracefile to get base_id
302 if(strcmp(&control_name
[strlen(control_name
)-10],"facilities") ==0){
304 if(!ltt_tracefile_read(tf
,&ev
)) return 0; // end of file
306 if(ev
.event_id
== TRACE_FACILITY_LOAD
){
308 fLoad
.name
= (gchar
*)pos
;
309 fLoad
.checksum
= *(LttChecksum
*)(pos
+ strlen(fLoad
.name
));
310 fLoad
.base_code
= *(guint32
*)(pos
+ strlen(fLoad
.name
) + sizeof(LttChecksum
));
312 for(i
=0;i
<t
->facility_number
;i
++){
313 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
,i
);
314 if(strcmp(f
->name
,fLoad
.name
)==0 && fLoad
.checksum
==f
->checksum
){
315 f
->base_id
= fLoad
.base_code
;
319 if(i
==t
->facility_number
) {
320 g_warning("Facility: %s, checksum: %u is not found",
321 fLoad
.name
,(unsigned int)fLoad
.checksum
);
324 }else if(ev
.event_id
== TRACE_BLOCK_START
){
326 }else if(ev
.event_id
== TRACE_BLOCK_END
){
329 g_warning("Not valid facilities trace file");
337 /*****************************************************************************
339 * ltt_tracefile_close: close a trace file,
341 * t : tracefile which will be closed
342 ****************************************************************************/
344 void ltt_tracefile_close(LttTracefile
*t
)
346 if(t
->buffer
.head
!= NULL
)
347 munmap(t
->buffer
.head
, t
->buf_size
);
352 /*****************************************************************************
353 *Get system information
354 ****************************************************************************/
355 gint
getSystemInfo(LttSystemDescription
* des
, gchar
* pathname
)
362 GMarkupParseContext
* context
;
363 GError
* error
= NULL
;
364 GMarkupParser markup_parser
=
366 parser_start_element
,
369 NULL
, /* passthrough */
373 fd
= g_open(pathname
, O_RDONLY
, 0);
375 g_warning("Can not open file : %s\n", pathname
);
379 iochan
= g_io_channel_unix_new(fd
);
381 context
= g_markup_parse_context_new(&markup_parser
, 0, des
,NULL
);
383 //while(fgets(buf,DIR_NAME_SIZE, fp) != NULL){
384 while(g_io_channel_read_line(iochan
, &buf
, &length
, NULL
, &error
)
385 != G_IO_STATUS_EOF
) {
388 g_warning("Can not read xml file: \n%s\n", error
->message
);
391 if(!g_markup_parse_context_parse(context
, buf
, length
, &error
)){
393 g_warning("Can not parse xml file: \n%s\n", error
->message
);
396 g_markup_parse_context_free(context
);
398 g_io_channel_shutdown(iochan
, FALSE
, &error
); /* No flush */
400 g_warning("Can not close file: \n%s\n", error
->message
);
408 g_markup_parse_context_free(context
);
410 g_io_channel_shutdown(iochan
, FALSE
, &error
); /* No flush */
412 g_warning("Can not close file: \n%s\n", error
->message
);
422 /*****************************************************************************
423 *The following functions get facility/tracefile information
424 ****************************************************************************/
426 gint
getFacilityInfo(LttTrace
*t
, gchar
* eventdefs
)
433 gchar fullname
[DIR_NAME_SIZE
];
434 GError
* error
= NULL
;
436 dir
= g_dir_open(eventdefs
, 0, &error
);
439 g_warning("Can not open directory: %s, %s\n", eventdefs
, error
->message
);
444 while((name
= g_dir_read_name(dir
)) != NULL
){
445 if(!g_pattern_match_simple("*.xml", name
)) continue;
446 strcpy(fullname
,eventdefs
);
447 strcat(fullname
,name
);
448 ltt_facility_open(t
,fullname
);
452 for(j
=0;j
<t
->facility_number
;j
++){
453 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, j
);
454 for(i
=0; i
<f
->event_number
; i
++){
456 setFieldsOffset(NULL
, et
, NULL
, t
);
462 /*****************************************************************************
463 *A trace is specified as a pathname to the directory containing all the
464 *associated data (control tracefiles, per cpu tracefiles, event
467 *When a trace is closed, all the associated facilities, types and fields
468 *are released as well.
472 /****************************************************************************
473 * get_absolute_pathname
475 * return the unique pathname in the system
477 * MD : Fixed this function so it uses realpath, dealing well with
478 * forgotten cases (.. were not used correctly before).
480 ****************************************************************************/
481 void get_absolute_pathname(const gchar
*pathname
, gchar
* abs_pathname
)
483 abs_pathname
[0] = '\0';
485 if ( realpath (pathname
, abs_pathname
) != NULL
)
489 /* error, return the original path unmodified */
490 strcpy(abs_pathname
, pathname
);
496 /* Search for something like : .*_.*
498 * The left side is the name, the right side is the number.
501 int get_tracefile_name_number(const gchar
*raw_name
,
505 guint raw_name_len
= strlen(raw_name
);
506 gchar char_name
[PATH_MAX
]
513 for(i
=raw_name_len
-1;i
>=0;i
--) {
514 if(raw_name
[i
] == '_') break;
516 if(i
==0) /* Either not found or name length is 0 */
520 cpu_num
= strtol(raw_name
+underscore_pos
+1, &endptr
, 10);
522 if(endptr
== raw_name
+underscore_pos
+1)
523 return -1; /* No digit */
524 if(cpu_num
== LONG_MIN
|| cpu_num
== LONG_MAX
)
525 return -1; /* underflow / overflow */
527 char_name
= strncpy(char_name
, raw_name
, underscore_pos
);
529 *name
= g_quark_from_string(char_name
);
536 void ltt_tracefile_group_destroy(gpointer data
)
538 GArray
*group
= (GArray
*)data
;
542 for(i
=0; i
<group
->len
; i
++) {
543 tf
= &g_array_index (group
, LttTracefile
, i
);
545 ltt_tracefile_close(tf
);
547 g_array_free(group
, TRUE
);
550 gboolean
ltt_tracefile_group_has_cpu_online(gpointer data
)
552 GArray
*group
= (GArray
*)data
;
556 for(i
=0; i
<group
->len
; i
++) {
557 tf
= &g_array_index (group
, LttTracefile
, i
);
558 if(tf
->cpu_online
) return 1;
564 /* Open each tracefile under a specific directory. Put them in a
565 * GData : permits to access them using their tracefile group pathname.
566 * i.e. access control/modules tracefile group by index :
569 * A tracefile group is simply an array where all the per cpu tracefiles sits.
572 static int open_tracefiles(LttTrace
*trace
, char *root_path
, GData
*tracefiles
)
574 DIR *dir
= opendir(root_path
);
575 struct dirent
*entry
;
576 struct stat stat_buf
;
582 if(channel_dir
== NULL
) {
583 perror(subchannel_name
);
587 strncpy(path
, root_path
, PATH_MAX
-1);
588 path_len
= strlen(path
);
589 path
[path_len
] = '/';
591 path_ptr
= path
+ path_len
;
593 while((entry
= readdir(channel_dir
)) != NULL
) {
595 if(entry
->d_name
[0] == '.') continue;
597 strncpy(path_ptr
, entry
->d_name
, PATH_MAX
- path_len
);
599 ret
= stat(path
, &stat_buf
);
605 g_debug("Tracefile file or directory : %s\n", path
);
607 if(S_ISDIR(stat_buf
.st_mode
)) {
609 g_debug("Entering subdirectory...\n");
610 ret
= open_tracefiles(path
, tracefiles
);
611 if(ret
< 0) continue;
612 } else if(S_ISREG(stat_buf
.st_mode
)) {
613 g_debug("Opening file.\n");
621 if(get_tracefile_name_number(path
, &name
, &num
))
622 continue; /* invalid name */
624 group
= g_datalist_get_data(tracefiles
, name
);
626 /* Elements are automatically cleared when the array is allocated.
627 * It makes the cpu_online variable set to 0 : cpu offline, by default.
629 group
= g_array_sized_new (FALSE
, TRUE
, sizeof(LttTracefile
), 10);
630 g_datalist_set_data_full(tracefiles
, name
,
631 group
, ltt_tracefile_group_destroy
);
633 /* Add the per cpu tracefile to the named group */
634 unsigned int old_len
= group
->len
;
636 group
= g_array_set_size(group
, num
+1);
637 tf
= &g_array_index (group
, LttTracefile
, num
);
639 if(ltt_tracefile_open(trace
, path
, tf
)) {
640 g_info("Error opening tracefile %s", path
);
641 g_array_set_size(group
, old_len
);
643 if(!ltt_tracefile_group_has_cpu_online(group
))
644 g_datalist_remove_data(tracefiles
, name
);
646 continue; /* error opening the tracefile : bad magic number ? */
658 /* ltt_get_facility_description
660 * Opens the trace corresponding to the requested facility (identified by fac_id
663 * The name searched is : %trace root%/eventdefs/facname_checksum.xml
665 * Returns 0 on success, or 1 on failure.
668 static int ltt_get_facility_description(LttFacility
*f
,
671 char desc_file_name
[PATH_MAX
];
677 text
= g_quark_to_string(t
->pathname
);
678 textlen
= strlen(text
);
680 if(textlen
>= PATH_MAX
) goto name_error
;
681 strcpy(desc_file_name
, text
);
683 text
= "/eventdefs/";
684 textlen
+=strlen(text
);
685 if(textlen
>= PATH_MAX
) goto name_error
;
686 strcat(desc_file_name
, text
);
688 text
= g_quark_to_string(f
->name
);
689 textlen
+=strlen(text
);
690 if(textlen
>= PATH_MAX
) goto name_error
;
691 strcat(desc_file_name
, text
);
694 textlen
+=strlen(text
);
695 if(textlen
>= PATH_MAX
) goto name_error
;
696 strcat(desc_file_name
, text
);
698 err
= snprintf(desc_file_name
+textlen
, PATH_MAX
-textlen
-1,
700 if(err
) goto name_error
;
702 textlen
=strlen(desc_file_name
);
705 textlen
+=strlen(text
);
706 if(textlen
>= PATH_MAX
) goto name_error
;
707 strcat(desc_file_name
, text
);
709 err
= ltt_facility_open(f
, t
, desc_file_name
);
710 if(err
) goto facility_error
;
712 for(i
=0;i
<t
->facilities_by_num
->len
;i
++){
713 f
= &g_array_index(t
->facilities_by_num
, LttFacility
, i
);
715 for(j
=0; j
<f
->events
->len
; j
++){
716 et
= &g_array_index(f
->events
, LttEventType
, j
);
717 setFieldsOffset(NULL
, et
, NULL
);
730 static void ltt_tracefile_ids_destroy(gpointer data
)
732 GArray
*fac_ids
= (GArray
*)data
;
736 for(i
=0; i
<group
->len
; i
++) {
737 fac
= &g_array_index (fac_ids
, LttFacility
, i
);
738 ltt_facility_close(fac
);
741 g_array_free(array
, TRUE
);
745 /* Presumes the tracefile is already seeked at the beginning. It makes sense,
746 * because it must be done just after the opening */
747 static int ltt_process_facility_tracefile(LttTracefile
*tf
)
754 err
= ltt_tracefile_read_seek(tf
);
755 if(err
== EPERM
) goto seek_error
;;
756 else if(err
== ERANGE
) break; /* End of tracefile */
758 err
= ltt_tracefile_read_update_event(tf
);
759 if(err
) goto update_error
;
761 /* We are on a facility load/or facility unload/ or heartbeat event */
763 * * facility 0 is hardcoded : this is the core facility. It will be shown
764 * in the facility array though, and is shown as "loaded builtin" in the
766 * It contains event :
768 * 1 : facility unload
769 * 2 : state dump facility load
770 * Facility 1 : (heartbeat)
773 if(tf
->event
.facility_id
> 1) { /* Should only contain core and heartbeat
775 g_warning("Error in processing facility file %s, "
776 "should not contain facility id %u.", g_quark_to_string(tf
->name
),
777 tf
->event
.facility_id
);
780 } else if(tf
->event
.facility_id
== 0) {
783 switch((enum ltt_core_events
)tf
->event
.event_id
) {
784 case LTT_EVENT_FACILITY_LOAD
:
785 struct LttFacilityLoad
*fac_load_data
=
786 (struct LttFacilityLoad
*)tf
->event
.data
;
788 (char*)(tf
->event
.data
+ sizeof(struct LttFacilityLoad
));
789 fac
= &g_array_index (tf
->facilities_by_num
, LttTracefile
,
791 g_assert(fac
->exists
== 0);
792 fac
->name
= g_quark_from_string(fac_name
);
793 fac
->checksum
= ltt_get_uint32(LTT_GET_BO(tf
),
794 fac_load_data
->checksum
);
795 fac
->id
= ltt_get_uint8(LTT_GET_BO(tf
), fac_load_data
->id
);
796 fac
->pointer_size
= ltt_get_uint32(LTT_GET_BO(tf
),
797 fac_load_data
->pointer_size
);
798 fac
->size_t_size
= ltt_get_uin32(LTT_GET_BO(tf
),
799 fac_load_data
->size_t_size
);
800 fac
->alignment
= ltt_get_uint32(LTT_GET_BO(tf
),
801 fac_load_data
->alignment
);
803 if(ltt_get_facility_description(fac
, tf
->trace
))
806 fac
->trace
= tf
->trace
;
809 fac_ids
= g_datalist_get_data(tf
->facilities_by_name
, fac
->name
);
810 if(fac_ids
== NULL
) {
811 fac_ids
= g_array_sized_new (FALSE
, TRUE
, sizeof(guint
), 1);
812 g_datalist_set_data_full(tf
->facilities_by_name
, fac
->name
,
813 fac_ids
, ltt_fac_ids_destroy
);
815 g_array_append_val(fac_ids
, fac
->id
);
818 case LTT_EVENT_FACILITY_UNLOAD
:
819 /* We don't care about unload : facilities ID are valid for the whole
820 * trace. They simply won't be used after the unload. */
822 case LTT_EVENT_STATE_DUMP_FACILITY_LOAD
:
823 struct LttFacilityLoad
*fac_load_data
=
824 (struct LttFacilityLoad
*)tf
->event
.data
;
826 (char*)(tf
->event
.data
+ sizeof(struct LttFacilityLoad
));
827 fac
= &g_array_index (tf
->facilities_by_num
, LttTracefile
,
829 g_assert(fac
->exists
== 0);
830 fac
->name
= g_quark_from_string(fac_name
);
831 fac
->checksum
= ltt_get_uint32(LTT_GET_BO(tf
),
832 fac_load_data
->checksum
);
833 fac
->id
= ltt_get_uint8(LTT_GET_BO(tf
), fac_load_data
->id
);
834 fac
->pointer_size
= ltt_get_uint32(LTT_GET_BO(tf
),
835 fac_load_data
->pointer_size
);
836 fac
->size_t_size
= ltt_get_uin32(LTT_GET_BO(tf
),
837 fac_load_data
->size_t_size
);
838 fac
->alignment
= ltt_get_uint32(LTT_GET_BO(tf
),
839 fac_load_data
->alignment
);
842 fac
->named_types_number
;
845 fac_ids
= g_datalist_get_data(tf
->facilities_by_name
, fac
->name
);
846 if(fac_ids
== NULL
) {
847 fac_ids
= g_array_sized_new (FALSE
, TRUE
, sizeof(guint
), 1);
848 g_datalist_set_data_full(tf
->facilities_by_name
, fac
->name
,
849 fac_ids
, ltt_fac_ids_destroy
);
851 g_array_append_val(fac_ids
, fac
->id
);
855 g_warning("Error in processing facility file %s, "
856 "unknown event id %hhu in core facility.",
857 g_quark_to_string(tf
->name
),
862 } else if(tf
->event
.facility_id
== 1) {
864 switch((enum ltt_heartbeat_events
)tf
->event
.event_id
) {
865 case LTT_EVENT_HEARTBEAT
:
868 g_warning("Error in processing facility file %s, "
869 "unknown event id %hhu in heartbeat facility.",
870 g_quark_to_string(tf
->name
),
889 LttTrace
*ltt_trace_open(const gchar
*pathname
)
891 gchar abs_path
[PATH_MAX
];
897 LttTrace
* t
= g_new(LttTrace
, 1);
898 if(!t
) goto alloc_error
;
900 get_absolute_pathname(pathname
, abs_path
);
901 t
->pathname
= g_quark_from_string(abs_path
);
903 /* Open all the tracefiles */
904 g_datalist_init(t
->tracefiles
);
905 if(open_tracefiles(t
, abs_path
, t
->tracefiles
))
908 /* Prepare the facilities containers : array and mapping */
909 /* Array is zeroed : the "exists" field is set to false by default */
910 t
->facilities_by_num
= g_array_sized_new (FALSE
,
911 TRUE
, sizeof(LttFacility
),
913 t
->facilities_by_num
= g_array_set_size(t
->facilities_by_num
, NUM_FACILITIES
);
915 g_datalist_init(t
->tracefiles_by_name
);
917 /* Parse each trace control/facilitiesN files : get runtime fac. info */
918 group
= g_datalist_get_data(t
->tracefiles
, LTT_TRACEFILE_NAME_FACILITIES
);
920 g_error("Trace %s has no facility tracefile", abs_path
);
921 goto facilities_error
;
924 for(i
=0; i
<group
->len
; i
++) {
925 tf
= &g_array_index (group
, LttTracefile
, i
);
926 if(ltt_process_facility_tracefile(tf
))
927 goto facilities_error
;
936 g_datalist_free(t
->tracefiles_by_name
);
937 g_array_free(t
->facilities_by_num
, TRUE
);
939 g_datalist_clear(t
->tracefiles
);
946 GQuark
ltt_trace_name(LttTrace
*t
)
952 /******************************************************************************
953 * When we copy a trace, we want all the opening actions to happen again :
954 * the trace will be reopened and totally independant from the original.
955 * That's why we call ltt_trace_open.
956 *****************************************************************************/
957 LttTrace
*ltt_trace_copy(LttTrace
*self
)
959 return ltt_trace_open(self
->pathname
);
962 void ltt_trace_close(LttTrace
*t
)
964 g_datalist_free(t
->tracefiles_by_name
);
965 g_array_free(t
->facilities_by_num
, TRUE
);
966 g_datalist_clear(t
->tracefiles
);
971 /*****************************************************************************
972 *Get the system description of the trace
973 ****************************************************************************/
975 LttFacility
*ltt_trace_facility_by_id(LttTrace
*t
, guint8 id
)
977 g_assert(index
< t
->facilities_by_num
->len
);
978 return &g_array_index(t
->facilities_by_num
, LttFacility
, id
);
981 /* ltt_trace_facility_get_by_name
983 * Returns the GArray of facility indexes. All the fac_ids that matches the
984 * requested facility name.
986 * If name is not found, returns NULL.
988 GArray
*ltt_trace_facility_get_by_name(LttTrace
*t
, GQuark name
)
990 return g_datalist_id_get_data(t
->facilities_by_name
, name
);
993 /*****************************************************************************
994 * Functions to discover all the event types in the trace
995 ****************************************************************************/
998 unsigned ltt_trace_eventtype_number(LttTrace
*t
)
1002 unsigned int num
= t
->facility_number
;
1006 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, i
);
1007 count
+= f
->event_number
;
1014 //use an iteration on all the trace facilities, and inside iteration on all the
1015 //event types in each facilities instead.
1016 LttEventType
*ltt_trace_eventtype_get(LttTrace
*t
, unsigned evId
)
1018 LttEventType
*event_type
;
1021 f
= ltt_trace_facility_by_id(t
,evId
);
1023 if(unlikely(!f
)) event_type
= NULL
;
1024 else event_type
= f
->events
[evId
- f
->base_id
];
1031 /*****************************************************************************
1032 * ltt_trace_find_tracefile
1034 * Find a tracefile by name and index in the group.
1036 * Returns a pointer to the tracefiles, else NULL.
1037 ****************************************************************************/
1039 LttTracefile
*ltt_trace_find_tracefile(LttTrace
*t
, const gchar
*name
)
1044 /*****************************************************************************
1045 * Get the start time and end time of the trace
1046 ****************************************************************************/
1048 static void ltt_tracefile_time_span_get(LttTracefile
*tf
,
1049 LttTime
*start
, LttTime
*end
)
1051 struct ltt_block_start_header
* header
;
1054 err
= map_block(tf
, 0);
1056 g_error("Can not map block");
1057 *start
= { 0xFFFFFFFF, 0xFFFFFFFF };
1059 *start
= tf
->buffer
.begin
.timestamp
;
1061 err
= map_block(tf
, tf
->num_blocks
- 1); /* Last block */
1063 g_error("Can not map block");
1066 *end
= tf
->buffer
.end
.timestamp
;
1069 struct tracefile_time_span_get_args
{
1075 static void group_time_span_get(GQuark name
, gpointer data
, gpointer user_data
)
1077 struct tracefile_time_span_get_args
*args
=
1078 (struct tracefile_time_span_get_args
*)user_data
;
1080 GArray
*group
= (GArray
*)data
;
1086 for(i
=0; i
<group
->len
; i
++) {
1087 tf
= &g_array_index (group
, LttTracefile
, i
);
1088 if(tf
->cpu_online
) {
1089 ltt_tracefile_time_span_get(tf
, &tmp_start
, &tmp_end
);
1090 if(ltt_time_compare(*args
->start
, tmp_start
)>0) *args
->start
= tmp_start
;
1091 if(ltt_time_compare(*args
->end
, tmp_end
)<0) *args
->end
= tmp_end
;
1096 void ltt_trace_time_span_get(LttTrace
*t
, LttTime
*start
, LttTime
*end
)
1098 LttTime min_start
= { 0xFFFFFFFF, 0xFFFFFFFF };
1099 LttTime max_end
= { 0, 0 };
1100 struct tracefile_time_span_get_args args
= { t
, &min_start
, &max_end
};
1102 g_datalist_foreach(t
->tracefiles
, &group_time_span_get
, &args
);
1104 if(start
!= NULL
) *start
= min_start
;
1105 if(end
!= NULL
) *end
= max_end
;
1110 /*****************************************************************************
1111 *Get the name of a tracefile
1112 ****************************************************************************/
1114 GQuark
ltt_tracefile_name(LttTracefile
*tf
)
1119 /*****************************************************************************
1120 * Get the number of blocks in the tracefile
1121 ****************************************************************************/
1123 guint
ltt_tracefile_block_number(LttTracefile
*tf
)
1125 return tf
->block_number
;
1129 /* Seek to the first event in a tracefile that has a time equal or greater than
1130 * the time passed in parameter.
1132 * If the time parameter is outside the tracefile time span, seek to the first
1133 * or the last event of the tracefile.
1135 * If the time parameter is before the first event, we have to seek specially to
1138 * If the time is after the end of the trace, get the last event.
1140 * Do a binary search to find the right block, then a sequential search in the
1141 * block to find the event.
1143 * In the special case where the time requested fits inside a block that has no
1144 * event corresponding to the requested time, the first event of the next block
1147 * IMPORTANT NOTE : // FIXME everywhere...
1149 * You MUST NOT do a ltt_tracefile_read right after a ltt_tracefile_seek_time :
1150 * you will jump over an event if you do.
1152 * Return value : 0 : no error, the tf->event can be used
1153 * otherwise : this is an error.
1157 int ltt_tracefile_seek_time(LttTracefile
*tf
, LttTime time
)
1161 unsigned int block_num
, high
, low
;
1163 /* seek at the beginning of trace */
1164 err
= map_block(tf
, 0); /* First block */
1166 g_error("Can not map block");
1170 /* If the time is lower or equal the beginning of the trace,
1171 * go to the first event. */
1172 if(ltt_time_compare(time
, tf
->buffer
.start
.timestamp
) <= 0) {
1173 ret
= ltt_tracefile_read(tf
)
1174 goto found
; /* There is either no event in the trace or the event points
1175 to the first event in the trace */
1178 err
= map_block(tf
, tf
->num_blocks
- 1); /* Last block */
1180 g_error("Can not map block");
1184 /* If the time is after the end of the trace, get the last event. */
1185 if(ltt_time_compare(time
, tf
->buffer
.end
.timestamp
) >= 0) {
1186 /* While the ltt_tracefile_read doesn't return ERANGE or EPERM,
1190 ret
= ltt_tracefile_read(tf
);
1191 if(ret
== ERANGE
) goto found
; /* ERANGE or EPERM */
1192 else if(ret
) goto fail
;
1196 /* Binary search the block */
1197 high
= tf
->num_blocks
- 1;
1201 block_num
= ((high
-low
) / 2) + low
;
1203 err
= map_block(tf
, block_num
);
1205 g_error("Can not map block");
1209 /* We cannot divide anymore : this is what would happen if the time
1210 * requested was exactly between two consecutive buffers'end and start
1211 * timestamps. This is also what would happend if we didn't deal with out
1212 * of span cases prior in this function. */
1213 /* The event is right in the buffer!
1214 * (or in the next buffer first event) */
1216 ret
= ltt_tracefile_read(tf
);
1217 if(ret
== ERANGE
) goto found
; /* ERANGE or EPERM */
1218 else if(ret
) goto fail
;
1220 if(ltt_time_compare(time
, tf
->event
.event_time
) >= 0)
1224 } if(ltt_time_compare(time
, tf
->buffer
.start
.timestamp
) < 0) {
1225 /* go to lower part */
1227 } else if(ltt_time_compare(time
, tf
->buffer
.end
.timestamp
) > 0) {
1228 /* go to higher part */
1230 } else {/* The event is right in the buffer!
1231 (or in the next buffer first event) */
1233 ltt_tracefile_read(tf
);
1234 if(ret
== ERANGE
) goto found
; /* ERANGE or EPERM */
1235 else if(ret
) goto fail
;
1237 if(ltt_time_compare(time
, tf
->event
.event_time
) >= 0)
1247 /* Error handling */
1249 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1250 g_quark_to_string(tf
->name
));
1255 int ltt_tracefile_seek_position(LttTracefile
*tf
, const LttEventPosition
*ep
) {
1259 if(ep
->tracefile
!= tf
) {
1263 err
= map_block(tf
, ep
->block
);
1265 g_error("Can not map block");
1269 tf
->event
.offset
= ep
->offset
;
1271 err
= ltt_tracefile_read_update_event(tf
);
1273 err
= ltt_tracefile_read_op(tf
);
1279 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1280 g_quark_to_string(tf
->name
));
1283 /* Calculate the real event time based on the buffer boundaries */
1284 LttTime
ltt_interpolate_time(LttTracefile
*tf
, LttEvent
*event
)
1288 g_assert(t
->trace
->has_tsc
);
1290 time
= ltt_time_from_uint64(
1291 (guint64
)tf
->buffer
.tsc
*tf
->buffer
.nsecs_per_cycle
);
1292 time
= ltt_time_add(tf
->buffer
.begin
.timestamp
, time
);
1297 /*****************************************************************************
1299 * ltt_tracefile_read : Read the next event in the tracefile
1304 * Returns 0 if an event can be used in tf->event.
1305 * Returns ERANGE on end of trace. The event in tf->event still can be used.
1306 * Returns EPERM on error.
1308 * This function does make the tracefile event structure point to the event
1309 * currently pointed to by the tf->event.
1311 * Note : you must call a ltt_tracefile_seek to the beginning of the trace to
1312 * reinitialize it after an error if you want results to be coherent.
1313 * It would be the case if a end of trace last buffer has no event : the end
1314 * of trace wouldn't be returned, but an error.
1315 * We make the assumption there is at least one event per buffer.
1316 ****************************************************************************/
1318 int ltt_tracefile_read(LttTracefile
*tf
)
1322 err
= ltt_tracefile_read_seek(tf
);
1324 err
= ltt_tracefile_read_update_event(tf
);
1326 err
= ltt_tracefile_read_op(tf
);
1332 int ltt_tracefile_read_seek(LttTracefile
*tf
)
1336 /* Get next buffer until we finally have an event, or end of trace */
1338 err
= ltt_seek_next_event(tf
);
1339 if(unlikely(err
== ENOPROTOOPT
)) {
1343 /* Are we at the end of the buffer ? */
1345 if(unlikely(tf
->buffer
.index
== tf
->num_blocks
-1)){ /* end of trace ? */
1348 /* get next block */
1349 err
= map_block(tf
, tf
->buffer
.index
+ 1);
1351 g_error("Can not map block");
1355 } else break; /* We found an event ! */
1362 /* do specific operation on events */
1363 int ltt_tracefile_read_op(LttTracefile
*tf
)
1372 /* do event specific operation */
1374 /* do something if its an heartbeat event : increment the heartbeat count */
1375 if(event
->facility_id
== LTT_FACILITY_CORE
)
1376 if(event
->event_id
== LTT_EVENT_HEARTBEAT
)
1377 t
->cur_heart_beat_number
++;
1383 /* same as ltt_tracefile_read, but does not seek to the next event nor call
1384 * event specific operation. */
1385 int ltt_tracefile_read_update_event(LttTracefile
*tf
)
1393 pos
= event
->offset
;
1395 /* Read event header */
1399 if(tf
->trace
->has_tsc
) {
1400 event
->time
.timestamp
= ltt_get_uint32(LTT_GET_BO(t
),
1402 /* 32 bits -> 64 bits tsc */
1403 /* note : still works for seek and non seek cases. */
1404 if(event
->time
.timestamp
< (0xFFFFFFFFULL
&tf
->buffer
.tsc
)) {
1405 tf
->buffer
.tsc
= ((tf
->buffer
.tsc
&0xFFFFFFFF00000000ULL
)
1407 | (guint64
)event
->time
.timestamp
;
1408 event
->tsc
= tf
->buffer
.tsc
;
1411 tf
->buffer
.tsc
= (tf
->buffer
.tsc
&0xFFFFFFFF00000000ULL
)
1412 | (guint64
)event
->time
.timestamp
;
1413 event
->tsc
= tf
->buffer
.tsc
;
1416 event
->event_time
= ltt_interpolate_time(tf
, event
);
1418 pos
+= sizeof(uint32
);
1420 event
->time
.delta
= ltt_get_uint32(LTT_GET_BO(tf
),
1423 event
->tsc
= tf
->buffer
.tsc
;
1425 event
->event_time
= ltt_time_add(tf
->buffer
.begin
.timestamp
,
1427 pos
+= sizeof(uint32
);
1430 event
->facility_id
= ltt_get_uint8(LTT_GET_BO(tf
),
1432 pos
+= sizeof(uint8
);
1434 event
->event_id
= ltt_get_uint8(LTT_GET_BO(tf
),
1436 pos
+= sizeof(uint8
);
1438 event
->data
= tf
->cur_event_pos
+ EVENT_HEADER_SIZE
;
1446 /****************************************************************************
1448 * map_block : map a block from the file
1450 * lttdes : ltt trace file
1451 * whichBlock : the block which will be read
1454 * EINVAL : lseek fail
1455 * EIO : can not read from the file
1456 ****************************************************************************/
1458 static int map_block(LttTracefile
* tf
, int block_num
)
1460 struct ltt_block_start_header
*header
;
1462 g_assert(block_num
< tf
->num_blocks
);
1464 if(tf
->buffer
.head
!= NULL
)
1465 munmap(tf
->buffer
.head
, tf
->buf_size
);
1467 /* Multiple of pages aligned head */
1468 tf
->buffer
.head
= mmap(0, tf
->block_size
, PROT_READ
, tf
->fd
,
1469 (off_t
)tf
->block_size
* (off_t
)block_num
);
1471 if(tf
->buffer
.head
== NULL
) {
1472 perror("Error in allocating memory for buffer of tracefile %s\n", fileName
);
1475 g_assert(tf
->buffer
.head
& (8-1) == 0); // make sure it's aligned.
1478 tf
->buffer
.index
= block_num
;
1480 header
= (struct ltt_block_start_header
*)tf
->buffer
.head
;
1482 tf
->buffer
.begin
.timestamp
= ltt_get_uint64(LTT_GET_BO(tf
),
1483 header
->begin
.timestamp
)
1485 tf
->buffer
.begin
.cycle_count
= ltt_get_uint64(LTT_GET_BO(tf
),
1486 header
->begin
.cycle_count
);
1487 tf
->buffer
.end
.timestamp
= ltt_get_uint64(LTT_GET_BO(tf
),
1488 header
->end
.timestamp
)
1490 tf
->buffer
.end
.cycle_count
= ltt_get_uint64(LTT_GET_BO(tf
),
1491 header
->end
.cycle_count
);
1492 tf
->buffer
.lost_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1495 tf
->buffer
.tsc
= tf
->buffer
.begin
.cycle_count
;
1496 tf
->event
.tsc
= tf
->buffer
.tsc
;
1499 * eventually support variable buffer size : will need a partial pre-read of
1500 * the headers to create an index when we open the trace... eventually. */
1501 g_assert(tf
->block_size
== ltt_get_uint32(header
->buf_size
));
1503 /* Now that the buffer is mapped, calculate the time interpolation for the
1506 tf
->buffer
.nsecs_per_cycle
= calc_nsecs_per_cycle(&tf
->buffer
);
1508 /* Make the current event point to the beginning of the buffer :
1509 * it means that the event read must get the first event. */
1510 tf
->event
.tracefile
= tf
;
1511 tf
->event
.block
= block_num
;
1512 tf
->event
.offset
= tf
->buffer
.head
;
1521 ssize_t
ltt_get_event_size(LttTracefile
*tf
)
1525 /* Specific handling of core events : necessary to read the facility control
1527 if(unlikely(tf
->event
.facility_id
== LTT_FACILITY_CORE
)) {
1528 switch((enum ltt_core_events
)tf
->event
.event_id
) {
1529 case LTT_EVENT_FACILITY_LOAD
:
1530 size
= sizeof(struct LttFacilityLoad
);
1532 case LTT_EVENT_FACILITY_UNLOAD
:
1533 size
= sizeof(struct LttFacilityUnload
);
1535 case LTT_EVENT_STATE_DUMP_FACILITY_LOAD
:
1536 size
= sizeof(struct LttStateDumpFacilityLoad
);
1538 case LTT_EVENT_HEARTBEAT
:
1539 size
= sizeof(TimeHeartbeat
);
1542 g_warning("Error in getting event size : tracefile %s, "
1543 "unknown event id %hhu in core facility.",
1544 g_quark_to_string(tf
->name
),
1545 tf
->event
.event_id
);
1546 goto event_id_error
;
1551 LttFacility
*f
= ltt_trace_get_facility_by_num(tf
->trace
,
1552 tf
->event
.facility_id
);
1553 LttEventType
*event_type
=
1554 ltt_facility_eventtype_get(f
, tf
->event
.event_id
);
1555 size
= get_fields_offsets(tf
, event_type
, tf
->event
.data
);
1565 /* Take the tf current event offset and use the event facility id and event id
1566 * to figure out where is the next event offset.
1568 * This is an internal function not aiming at being used elsewhere : it will
1569 * not jump over the current block limits. Please consider using
1570 * ltt_tracefile_read to do this.
1572 * Returns 0 on success
1573 * ERANGE if we are at the end of the buffer.
1574 * ENOPROTOOPT if an error occured when getting the current event size.
1576 static int ltt_seek_next_event(LttTracefile
*tf
)
1582 /* seek over the buffer header if we are at the buffer start */
1583 if(tf
->event
.offset
== tf
->buffer
.head
) {
1584 tf
->event
.offset
+= sizeof(struct ltt_block_start_header
);
1589 if(tf
->event
.offset
== tf
->buffer
.head
+ tf
->buffer
.lost_size
) {
1594 pos
= tf
->event
.data
;
1596 event_size
= ltt_get_event_size(tf
);
1597 if(event_size
< 0) goto error
;
1599 pos
+= (size_t)event_size
;
1601 tf
->event
.offset
= pos
;
1607 g_error("Error in ltt_seek_next_event for tracefile %s",
1608 g_quark_to_string(tf
->name
));
1613 /*****************************************************************************
1615 * calc_nsecs_per_cycle : calculate nsecs per cycle for current block
1618 ****************************************************************************/
1620 static double calc_nsecs_per_cycle(LttTracefile
* t
)
1622 LttTime lBufTotalTime
; /* Total time for this buffer */
1623 double lBufTotalNSec
; /* Total time for this buffer in nsecs */
1624 LttCycleCount lBufTotalCycle
;/* Total cycles for this buffer */
1626 /* Calculate the total time for this buffer */
1627 lBufTotalTime
= ltt_time_sub(
1628 ltt_get_time(t
->buffer
.end
.timestamp
),
1629 ltt_get_time(t
->buffer
.begin
.timestamp
));
1631 /* Calculate the total cycles for this bufffer */
1632 lBufTotalCycle
= t
->buffer
.end
.cycle_count
;
1633 lBufTotalCycle
-= t
->buffer
.start
.cycle_count
;
1635 /* Convert the total time to double */
1636 lBufTotalNSec
= ltt_time_to_double(lBufTotalTime
);
1638 return lBufTotalNSec
/ (double)lBufTotalCycle
;
1642 void setFieldsOffset(LttTracefile
*tf
, LttEventType
*evT
,void *evD
)
1644 LttField
* rootFld
= evT
->root_field
;
1645 // rootFld->base_address = evD;
1648 rootFld
->field_size
= getFieldtypeSize(tf
, evT
->facility
,
1649 evT
, 0,0,rootFld
, evD
);
1653 /*****************************************************************************
1655 * set_fields_offsets : set the precomputable offset of the fields
1657 * tracefile : opened trace file
1658 * event_type : the event type
1659 ****************************************************************************/
1661 void set_fields_offsets(LttTracefile
*tf
, LttEventType
*event_type
)
1663 LttField
*field
= event_type
->root_field
;
1666 preset_field_type_size(tf
, event_type
->facility
, event_type
, 0, 0,
1667 FIELD_FIXED
, FIELD_FIXED
,
1673 /*****************************************************************************
1675 * preset_field_type_size : set the fixed sizes of the field type
1678 * event_type : event type
1679 * offset_root : offset from the root
1680 * offset_parent : offset from the parent
1681 * fixed_root : Do we know a fixed offset to the root ?
1682 * fixed_parent : Do we know a fixed offset to the parent ?
1684 ****************************************************************************/
1685 void preset_field_type_size(LttTracefile
*tf
, LttEventType
*event_type
,
1686 off_t offset_root
, off_t offset_parent
,
1687 enum field_status
*fixed_root
, enum field_status
*fixed_parent
,
1693 g_assert(field
->fixed_root
== FIELD_UNKNOWN
);
1694 g_assert(field
->fixed_parent
== FIELD_UNKNOWN
);
1695 g_assert(field
->fixed_size
== FIELD_UNKNOWN
);
1697 type
= field
->field_type
;
1699 field
->fixed_root
= *fixed_root
;
1700 if(field
->fixed_root
== FIELD_FIXED
)
1701 field
->offset_root
= offset_root
;
1703 field
->offset_root
= 0;
1705 field
->fixed_parent
= *fixed_parent
;
1706 if(field
->fixed_parent
== FIELD_FIXED
)
1707 field
->offset_parent
= offset_parent
;
1709 field
->offset_parent
= 0;
1712 switch(type
->type_class
) {
1717 field
->field_size
= (off_t
) ltt_type_size(LTT_GET_BO(tf
),
1718 event_type
->facility
, type
);
1719 field
->fixed_size
= FIELD_FIXED
;
1722 field
->field_size
= (off_t
)event_type
->facility
->pointer_size
;
1723 field
->fixed_size
= FIELD_FIXED
;
1727 field
->field_size
= (off_t
)event_type
->facility
->pointer_size
;
1728 field
->fixed_size
= FIELD_FIXED
;
1733 field
->field_size
= (off_t
)event_type
->facility
->size_t_size
;
1734 field
->fixed_size
= FIELD_FIXED
;
1737 preset_field_type_size(tf
, event_type
->facility
, event_type
,
1739 FIELD_VARIABLE
, FIELD_VARIABLE
,
1741 field
->fixed_size
= FIELD_VARIABLE
;
1742 field
->field_size
= 0;
1745 field
->fixed_size
= FIELD_VARIABLE
;
1746 field
->field_size
= 0;
1749 preset_field_type_size(tf
, event_type
->facility
, event_type
,
1751 FIELD_VARIABLE
, FIELD_VARIABLE
,
1753 field
->fixed_size
= field
->child
[0]->fixed_size
;
1754 if(field
->fixed_size
== FIELD_FIXED
)
1755 field
->field_size
= type
->element_number
* field
->child
[0]->field_size
;
1757 field
->field_size
= 0;
1760 size_t current_root_offset
= field
->offset_root
;
1761 size_t current_offset
= 0;
1762 enum field_status current_child_status
= FIELD_FIXED
;
1763 for(i
=0;i
<type
->element_number
;i
++) {
1764 preset_field_type_size(tf
, event_type
->facility
, event_type
,
1765 current_root_offset
, current_offset
,
1766 fixed_root
, ¤t_child_status
,
1768 if(current_child_status
== FIELD_FIXED
) {
1769 current_root_offset
+= field
->child
[i
]->field_size
;
1770 current_offset
+= field
->child
[i
]->field_size
;
1772 current_root_offset
= 0;
1776 if(current_child_status
!= FIELD_FIXED
) {
1777 *fixed_parent
= current_child_status
;
1778 field
->field_size
= 0;
1779 field
->fixed_size
= current_child_status
;
1781 field
->field_size
= current_offset
;
1782 field
->fixed_size
= FIELD_FIXED
;
1786 size_t current_root_offset
= field
->offset_root
;
1787 size_t current_offset
= 0;
1788 size_t max_size
= 0;
1789 enum field_status final_child_status
= FIELD_FIXED
;
1790 for(i
=0;i
<type
->element_number
;i
++) {
1791 enum field_status current_root_child_status
= FIELD_FIXED
;
1792 enum field_status current_child_status
= FIELD_FIXED
;
1793 preset_field_type_size(tf
, event_type
->facility
, event_type
,
1794 current_root_offset
, current_offset
,
1795 ¤t_root_child_status
, ¤t_child_status
,
1797 if(current_child_status
!= FIELD_FIXED
)
1798 final_child_status
= current_child_status
;
1800 max_size
= max(max_size
, field
->child
[i
]->field_size
);
1802 if(final_child_status
!= FIELD_FIXED
) {
1803 *fixed_root
= final_child_status
;
1804 *fixed_parent
= final_child_status
;
1805 field
->field_size
= 0;
1806 field
->fixed_size
= current_child_status
;
1808 field
->field_size
= max_size
;
1809 field
->fixed_size
= FIELD_FIXED
;
1817 /*****************************************************************************
1819 * get_field_type_size : set the fixed and dynamic sizes of the field type
1820 * from the data read.
1823 * event_type : event type
1824 * offset_root : offset from the root
1825 * offset_parent : offset from the parent
1827 * data : a pointer to the event data.
1828 *Returns the field type size.
1829 ****************************************************************************/
1830 size_t get_field_type_size(LttTracefile
*tf
, LttEventType
*event_type
,
1831 off_t offset_root
, off_t offset_parent
,
1832 LttField
*field
, void *data
)
1838 g_assert(field
->fixed_root
!= FIELD_UNKNOWN
);
1839 g_assert(field
->fixed_parent
!= FIELD_UNKNOWN
);
1840 g_assert(field
->fixed_size
!= FIELD_UNKNOWN
);
1842 field
->offset_root
= offset_root
;
1843 field
->offset_parent
= offset_parent
;
1845 type
= field
->field_type
;
1847 switch(type
->type_class
) {
1858 g_assert(field
->fixed_size
== FIELD_FIXED
);
1859 size
= field
->field_size
;
1862 gint seqnum
= ltt_get_uint(LTT_GET_BO(tf
),
1863 field
->sequ_number_size
,
1864 data
+ offset_root
);
1866 if(field
->child
[0]->fixed_size
== FIELD_FIXED
) {
1867 size
= field
->sequ_number_size
+
1868 (seqnum
* get_field_type_size(tf
, event_type
->facility
, event_type
,
1869 offset_root
, offset_parent
,
1870 field
->child
[0], data
));
1872 size
+= field
->sequ_number_size
;
1873 for(i
=0;i
<seqnum
;i
++) {
1875 child_size
= get_field_type_size(tf
, event_type
->facility
,
1877 offset_root
, offset_parent
,
1878 field
->child
[0], data
);
1879 offset_root
+= child_size
;
1880 offset_parent
+= child_size
;
1884 field
->field_size
= size
;
1887 size
= strlen((char*)(data
+offset_root
)) + 1;// length + \0
1888 field
->field_size
= size
;
1891 if(field
->fixed_size
== FIELD_FIXED
)
1892 size
= field
->field_size
;
1894 for(i
=0;i
<seqnum
;i
++) {
1896 child_size
= get_field_type_size(tf
, event_type
->facility
,
1898 offset_root
, offset_parent
,
1899 field
->child
[0], data
);
1900 offset_root
+= child_size
;
1901 offset_parent
+= child_size
;
1904 field
->field_size
= size
;
1908 if(field
->fixed_size
== FIELD_FIXED
)
1909 size
= field
->field_size
;
1911 size_t current_root_offset
= offset_root
;
1912 size_t current_offset
= 0;
1913 size_t child_size
= 0;
1914 for(i
=0;i
<type
->element_number
;i
++) {
1915 child_size
= get_field_type_size(tf
, event_type
->facility
,
1916 event_type
, current_root_offset
, current_offset
,
1917 field
->child
[i
], data
);
1918 current_offset
+= child_size
;
1919 current_root_offset
+= child_size
;
1922 size
= current_offset
;
1923 field
->field_size
= size
;
1927 if(field
->fixed_size
== FIELD_FIXED
)
1928 size
= field
->field_size
;
1930 size_t current_root_offset
= field
->offset_root
;
1931 size_t current_offset
= 0;
1932 for(i
=0;i
<type
->element_number
;i
++) {
1933 size
= get_field_type_size(tf
, event_type
->facility
, event_type
,
1934 current_root_offset
, current_offset
,
1935 field
->child
[i
], data
);
1936 size
= max(size
, field
->child
[i
]->field_size
);
1938 field
->field_size
= size
;
1949 /*****************************************************************************
1951 * getFieldtypeSize: get the size of the field type (primitive type)
1954 * offsetRoot : offset from the root
1955 * offsetParent : offset from the parrent
1957 * evD : event data, it may be NULL
1959 * int : size of the field
1960 ****************************************************************************/
1962 static inline gint
getFieldtypeSize(LttTracefile
*tf
,
1963 LttEventType
* evT
, gint offsetRoot
,
1964 gint offsetParent
, LttField
* fld
, void *evD
)
1966 gint size
, size1
, element_number
, i
, offset1
, offset2
;
1967 LttType
* type
= fld
->field_type
;
1969 /* This likely has been tested with gcov : half of them.. */
1970 if(unlikely(fld
->field_fixed
== 1)){
1972 if(unlikely(fld
== evT
->root_field
)) {
1973 size
= fld
->field_size
;
1974 goto end_getFieldtypeSize
;
1978 /* From gcov profiling : half string, half struct, can we gain something
1979 * from that ? (Mathieu) */
1980 switch(type
->type_class
) {
1982 element_number
= (int) type
->element_number
;
1983 if(fld
->field_fixed
== -1){
1984 size
= getFieldtypeSize(tf
, evT
, offsetRoot
,
1985 0,fld
->child
[0], NULL
);
1986 if(size
== 0){ //has string or sequence
1987 fld
->field_fixed
= 0;
1989 fld
->field_fixed
= 1;
1990 size
*= element_number
;
1992 }else if(fld
->field_fixed
== 0){// has string or sequence
1994 for(i
=0;i
<element_number
;i
++){
1995 size
+= getFieldtypeSize(tf
, evT
, offsetRoot
+size
,size
,
1996 fld
->child
[0], evD
+size
);
1998 }else size
= fld
->field_size
;
2000 fld
->fixed_root
= (offsetRoot
==-1) ? 0 : 1;
2001 fld
->fixed_parent
= (offsetParent
==-1) ? 0 : 1;
2007 size1
= (int) ltt_type_size(fac
, type
);
2008 if(fld
->field_fixed
== -1){
2009 fld
->sequ_number_size
= size1
;
2010 fld
->field_fixed
= 0;
2011 size
= getFieldtypeSize(evT
, offsetRoot
,
2012 0,fld
->child
[0], NULL
);
2013 fld
->element_size
= size
;
2015 element_number
= getIntNumber(tf
,size1
,evD
);
2016 type
->element_number
= element_number
;
2017 if(fld
->element_size
> 0){
2018 size
= element_number
* fld
->element_size
;
2019 }else{//sequence has string or sequence
2021 for(i
=0;i
<element_number
;i
++){
2022 size
+= getFieldtypeSize(tf
, evT
,
2023 offsetRoot
+size
+size1
,size
+size1
,
2024 fld
->child
[0], evD
+size
+size1
);
2030 fld
->fixed_root
= (offsetRoot
==-1) ? 0 : 1;
2031 fld
->fixed_parent
= (offsetParent
==-1) ? 0 : 1;
2038 if(fld
->field_fixed
== -1){
2039 fld
->field_fixed
= 0;
2041 /* Hope my implementation is faster than strlen (Mathieu) */
2042 char *ptr
=(char*)evD
;
2044 /* from gcov : many many strings are empty, make it the common case.*/
2045 while(unlikely(*ptr
!= '\0')) { size
++; ptr
++; }
2046 //size = ptr - (char*)evD + 1; //include end : '\0'
2048 fld
->fixed_root
= (offsetRoot
==-1) ? 0 : 1;
2049 fld
->fixed_parent
= (offsetParent
==-1) ? 0 : 1;
2054 element_number
= (int) type
->element_number
;
2056 /* tested with gcov */
2057 if(unlikely(fld
->field_fixed
== -1)){
2058 offset1
= offsetRoot
;
2060 for(i
=0;i
<element_number
;i
++){
2061 size1
=getFieldtypeSize(tf
, evT
,offset1
,offset2
,
2062 fld
->child
[i
], NULL
);
2063 if(likely(size1
> 0 && size
>= 0)){
2065 if(likely(offset1
>= 0)) offset1
+= size1
;
2073 if(unlikely(size
== -1)){
2074 fld
->field_fixed
= 0;
2076 }else fld
->field_fixed
= 1;
2077 }else if(likely(fld
->field_fixed
== 0)){
2078 offset1
= offsetRoot
;
2080 for(i
=0;unlikely(i
<element_number
);i
++){
2081 size
=getFieldtypeSize(tf
, evT
, offset1
, offset2
,
2082 fld
->child
[i
], evD
+offset2
);
2087 }else size
= fld
->field_size
;
2088 fld
->fixed_root
= (offsetRoot
==-1) ? 0 : 1;
2089 fld
->fixed_parent
= (offsetParent
==-1) ? 0 : 1;
2093 if(unlikely(fld
->field_fixed
== -1)){
2094 size
= (int) ltt_type_size(LTT_GET_BO(tf
), type
);
2095 fld
->field_fixed
= 1;
2096 }else size
= fld
->field_size
;
2098 fld
->fixed_root
= (offsetRoot
==-1) ? 0 : 1;
2099 fld
->fixed_parent
= (offsetParent
==-1) ? 0 : 1;
2104 fld
->offset_root
= offsetRoot
;
2105 fld
->offset_parent
= offsetParent
;
2106 fld
->field_size
= size
;
2108 end_getFieldtypeSize
:
2114 /*****************************************************************************
2116 * get_int : get an integer number
2118 * reverse_byte_order: must we reverse the byte order ?
2119 * size : the size of the integer
2120 * ptr : the data pointer
2122 * gint64 : a 64 bits integer
2123 ****************************************************************************/
2125 gint64
get_int(gboolean reverse_byte_order
, gint size
, void *data
)
2130 case 1: val
= *((gint8
*)data
); break;
2131 case 2: val
= ltt_get_int16(reverse_byte_order
, data
); break;
2132 case 4: val
= ltt_get_int32(reverse_byte_order
, data
); break;
2133 case 8: val
= ltt_get_int64(reverse_byte_order
, data
); break;
2134 default: val
= ltt_get_int64(reverse_byte_order
, data
);
2135 g_critical("get_int : integer size %d unknown", size
);
2142 /*****************************************************************************
2144 * get_uint : get an unsigned integer number
2146 * reverse_byte_order: must we reverse the byte order ?
2147 * size : the size of the integer
2148 * ptr : the data pointer
2150 * guint64 : a 64 bits unsigned integer
2151 ****************************************************************************/
2153 guint64
get_uint(gboolean reverse_byte_order
, gint size
, void *data
)
2158 case 1: val
= *((gint8
*)data
); break;
2159 case 2: val
= ltt_get_uint16(reverse_byte_order
, data
); break;
2160 case 4: val
= ltt_get_uint32(reverse_byte_order
, data
); break;
2161 case 8: val
= ltt_get_uint64(reverse_byte_order
, data
); break;
2162 default: val
= ltt_get_uint64(reverse_byte_order
, data
);
2163 g_critical("get_uint : unsigned integer size %d unknown",
2172 /* get the node name of the system */
2174 char * ltt_trace_system_description_node_name (LttSystemDescription
* s
)
2176 return s
->node_name
;
2180 /* get the domain name of the system */
2182 char * ltt_trace_system_description_domain_name (LttSystemDescription
* s
)
2184 return s
->domain_name
;
2188 /* get the description of the system */
2190 char * ltt_trace_system_description_description (LttSystemDescription
* s
)
2192 return s
->description
;
2196 /* get the start time of the trace */
2198 LttTime
ltt_trace_system_description_trace_start_time(LttSystemDescription
*s
)
2200 return s
->trace_start
;
2204 LttTracefile
*ltt_tracefile_new()
2206 return g_new(LttTracefile
, 1);
2209 void ltt_tracefile_destroy(LttTracefile
*tf
)
2214 void ltt_tracefile_copy(LttTracefile
*dest
, const LttTracefile
*src
)
2219 /* Before library loading... */
2221 static void __attribute__((constructor
)) init(void)
2223 LTT_FACILITY_NAME_HEARTBEAT
= g_quark_from_string("heartbeat");
2224 LTT_EVENT_NAME_HEARTBEAT
= g_quark_from_string("heartbeat");
2226 LTT_TRACEFILE_NAME_FACILITIES
= g_quark_from_string("control/facilities");