1 /* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2005 Mathieu Desnoyers
4 * Complete rewrite from the original version made by XangXiu Yang.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License Version 2 as
8 * published by the Free Software Foundation;
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 #include <sys/types.h>
45 #include "ltt-private.h"
46 #include <ltt/trace.h>
47 #include <ltt/facility.h>
48 #include <ltt/event.h>
50 #include <ltt/ltt-types.h>
53 /* Facility names used in this file */
55 GQuark LTT_FACILITY_NAME_HEARTBEAT
,
56 LTT_EVENT_NAME_HEARTBEAT
;
57 GQuark LTT_TRACEFILE_NAME_FACILITIES
;
64 #define __UNUSED__ __attribute__((__unused__))
66 #define g_info(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, format)
69 #define g_debug(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format)
74 /* obtain the time of an event */
76 static inline LttTime
getEventTime(LttTracefile
* tf
);
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. */
86 static inline void preset_field_type_size(LttTracefile
*tf
,
87 LttEventType
*event_type
,
88 off_t offset_root
, off_t offset_parent
,
89 enum field_status
*fixed_root
, enum field_status
*fixed_parent
,
93 static inline size_t get_field_type_size(LttTracefile
*tf
,
94 LttEventType
*event_type
,
95 off_t offset_root
, off_t offset_parent
,
96 LttField
*field
, void *data
);
99 /* map a fixed size or a block information from the file (fd) */
100 static gint
map_block(LttTracefile
* tf
, guint block_num
);
102 /* calculate nsec per cycles for current block */
103 static double calc_nsecs_per_cycle(LttTracefile
* t
);
105 /* go to the next event */
106 static int ltt_seek_next_event(LttTracefile
*tf
);
109 /* Functions to parse system.xml file (using glib xml parser) */
110 static void parser_start_element (GMarkupParseContext __UNUSED__
*context
,
111 const gchar
*element_name
,
112 const gchar
**attribute_names
,
113 const gchar
**attribute_values
,
118 LttSystemDescription
* des
= (LttSystemDescription
* )user_data
;
119 if(strcmp("system", element_name
)){
120 *error
= g_error_new(G_MARKUP_ERROR
,
122 "This is not system.xml file");
126 while(attribute_names
[i
]){
127 if(strcmp("node_name", attribute_names
[i
])==0){
128 des
->node_name
= g_strdup(attribute_values
[i
]);
129 }else if(strcmp("domainname", attribute_names
[i
])==0){
130 des
->domain_name
= g_strdup(attribute_values
[i
]);
131 }else if(strcmp("cpu", attribute_names
[i
])==0){
132 des
->nb_cpu
= atoi(attribute_values
[i
]);
133 }else if(strcmp("arch_size", attribute_names
[i
])==0){
134 if(strcmp(attribute_values
[i
],"LP32") == 0) des
->size
= LTT_LP32
;
135 else if(strcmp(attribute_values
[i
],"ILP32") == 0) des
->size
= LTT_ILP32
;
136 else if(strcmp(attribute_values
[i
],"LP64") == 0) des
->size
= LTT_LP64
;
137 else if(strcmp(attribute_values
[i
],"ILP64") == 0) des
->size
= LTT_ILP64
;
138 else if(strcmp(attribute_values
[i
],"UNKNOWN") == 0) des
->size
= LTT_UNKNOWN
;
139 }else if(strcmp("endian", attribute_names
[i
])==0){
140 if(strcmp(attribute_values
[i
],"LITTLE_ENDIAN") == 0)
141 des
->endian
= LTT_LITTLE_ENDIAN
;
142 else if(strcmp(attribute_values
[i
],"BIG_ENDIAN") == 0)
143 des
->endian
= LTT_BIG_ENDIAN
;
144 }else if(strcmp("kernel_name", attribute_names
[i
])==0){
145 des
->kernel_name
= g_strdup(attribute_values
[i
]);
146 }else if(strcmp("kernel_release", attribute_names
[i
])==0){
147 des
->kernel_release
= g_strdup(attribute_values
[i
]);
148 }else if(strcmp("kernel_version", attribute_names
[i
])==0){
149 des
->kernel_version
= g_strdup(attribute_values
[i
]);
150 }else if(strcmp("machine", attribute_names
[i
])==0){
151 des
->machine
= g_strdup(attribute_values
[i
]);
152 }else if(strcmp("processor", attribute_names
[i
])==0){
153 des
->processor
= g_strdup(attribute_values
[i
]);
154 }else if(strcmp("hardware_platform", attribute_names
[i
])==0){
155 des
->hardware_platform
= g_strdup(attribute_values
[i
]);
156 }else if(strcmp("operating_system", attribute_names
[i
])==0){
157 des
->operating_system
= g_strdup(attribute_values
[i
]);
158 }else if(strcmp("ltt_major_version", attribute_names
[i
])==0){
159 des
->ltt_major_version
= atoi(attribute_values
[i
]);
160 }else if(strcmp("ltt_minor_version", attribute_names
[i
])==0){
161 des
->ltt_minor_version
= atoi(attribute_values
[i
]);
162 }else if(strcmp("ltt_block_size", attribute_names
[i
])==0){
163 des
->ltt_block_size
= atoi(attribute_values
[i
]);
165 *error
= g_error_new(G_MARKUP_ERROR
,
167 "Not a valid attribute");
174 static void parser_characters (GMarkupParseContext __UNUSED__
*context
,
176 gsize __UNUSED__ text_len
,
178 GError __UNUSED__
**error
)
180 LttSystemDescription
* des
= (LttSystemDescription
* )user_data
;
181 des
->description
= g_strdup(text
);
184 LttFacility
*ltt_trace_get_facility_by_num(LttTrace
*t
,
187 g_assert(num
< t
->facilities_by_num
->len
);
189 return &g_array_index(t
->facilities_by_num
, LttFacility
, num
);
194 /*****************************************************************************
196 * ltt_tracefile_open : open a trace file, construct a LttTracefile
198 * t : the trace containing the tracefile
199 * fileName : path name of the trace file
200 * tf : the tracefile structure
202 * : 0 for success, -1 otherwise.
203 ****************************************************************************/
205 gint
ltt_tracefile_open(LttTrace
*t
, gchar
* fileName
, LttTracefile
*tf
)
207 struct stat lTDFStat
; /* Trace data file status */
208 struct ltt_block_start_header
*header
;
211 tf
->name
= g_quark_from_string(fileName
);
213 tf
->fd
= g_open(fileName
, O_RDONLY
, 0);
215 g_warning("Unable to open input data file %s\n", fileName
);
219 // Get the file's status
220 if(fstat(tf
->fd
, &lTDFStat
) < 0){
221 g_warning("Unable to get the status of the input data file %s\n", fileName
);
225 // Is the file large enough to contain a trace
226 if(lTDFStat
.st_size
< (off_t
)(sizeof(struct ltt_block_start_header
))){
227 g_print("The input data file %s does not contain a trace\n", fileName
);
231 /* Temporarily map the buffer start header to get trace information */
232 /* Multiple of pages aligned head */
233 tf
->buffer
.head
= mmap(0, sizeof(struct ltt_block_start_header
), PROT_READ
,
234 MAP_PRIVATE
, tf
->fd
, 0);
235 if(tf
->buffer
.head
== NULL
) {
236 perror("Error in allocating memory for buffer of tracefile");
239 g_assert(((guint
)tf
->buffer
.head
& (8-1)) == 0); // make sure it's aligned.
241 header
= (struct ltt_block_start_header
*)tf
->buffer
.head
;
243 if(header
->trace
.magic_number
== LTT_MAGIC_NUMBER
)
245 else if(header
->trace
.magic_number
== LTT_REV_MAGIC_NUMBER
)
247 else /* invalid magic number, bad tracefile ! */
250 //store the size of the file
251 tf
->file_size
= lTDFStat
.st_size
;
252 tf
->block_size
= header
->buf_size
;
253 tf
->num_blocks
= tf
->file_size
/ tf
->block_size
;
255 vfree(tf
->buffer
.head
);
256 tf
->buffer
.head
= NULL
;
258 //read the first block
259 if(map_block(tf
,0)) {
260 perror("Cannot map block for tracefile");
268 munmap(tf
->buffer
.head
, sizeof(struct ltt_block_start_header
));
276 /*****************************************************************************
277 *Open control and per cpu tracefiles
278 ****************************************************************************/
280 void ltt_tracefile_open_cpu(LttTrace
*t
, gchar
* tracefile_name
)
283 tf
= ltt_tracefile_open(t
,tracefile_name
);
285 t
->per_cpu_tracefile_number
++;
286 g_ptr_array_add(t
->per_cpu_tracefiles
, tf
);
289 gint
ltt_tracefile_open_control(LttTrace
*t
, gchar
* control_name
)
298 tf
= ltt_tracefile_open(t
,control_name
);
300 g_warning("ltt_tracefile_open_control : bad file descriptor");
303 t
->control_tracefile_number
++;
304 g_ptr_array_add(t
->control_tracefiles
,tf
);
306 //parse facilities tracefile to get base_id
307 if(strcmp(&control_name
[strlen(control_name
)-10],"facilities") ==0){
309 if(!ltt_tracefile_read(tf
,&ev
)) return 0; // end of file
311 if(ev
.event_id
== TRACE_FACILITY_LOAD
){
313 fLoad
.name
= (gchar
*)pos
;
314 fLoad
.checksum
= *(LttChecksum
*)(pos
+ strlen(fLoad
.name
));
315 fLoad
.base_code
= *(guint32
*)(pos
+ strlen(fLoad
.name
) + sizeof(LttChecksum
));
317 for(i
=0;i
<t
->facility_number
;i
++){
318 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
,i
);
319 if(strcmp(f
->name
,fLoad
.name
)==0 && fLoad
.checksum
==f
->checksum
){
320 f
->base_id
= fLoad
.base_code
;
324 if(i
==t
->facility_number
) {
325 g_warning("Facility: %s, checksum: %u is not found",
326 fLoad
.name
,(unsigned int)fLoad
.checksum
);
329 }else if(ev
.event_id
== TRACE_BLOCK_START
){
331 }else if(ev
.event_id
== TRACE_BLOCK_END
){
334 g_warning("Not valid facilities trace file");
343 /*****************************************************************************
345 * ltt_tracefile_close: close a trace file,
347 * t : tracefile which will be closed
348 ****************************************************************************/
350 void ltt_tracefile_close(LttTracefile
*t
)
352 if(t
->buffer
.head
!= NULL
)
353 munmap(t
->buffer
.head
, t
->buf_size
);
358 /*****************************************************************************
359 *Get system information
360 ****************************************************************************/
362 gint
getSystemInfo(LttSystemDescription
* des
, gchar
* pathname
)
369 GMarkupParseContext
* context
;
370 GError
* error
= NULL
;
371 GMarkupParser markup_parser
=
373 parser_start_element
,
376 NULL
, /* passthrough */
380 fd
= g_open(pathname
, O_RDONLY
, 0);
382 g_warning("Can not open file : %s\n", pathname
);
386 iochan
= g_io_channel_unix_new(fd
);
388 context
= g_markup_parse_context_new(&markup_parser
, 0, des
,NULL
);
390 //while(fgets(buf,DIR_NAME_SIZE, fp) != NULL){
391 while(g_io_channel_read_line(iochan
, &buf
, &length
, NULL
, &error
)
392 != G_IO_STATUS_EOF
) {
395 g_warning("Can not read xml file: \n%s\n", error
->message
);
398 if(!g_markup_parse_context_parse(context
, buf
, length
, &error
)){
400 g_warning("Can not parse xml file: \n%s\n", error
->message
);
403 g_markup_parse_context_free(context
);
405 g_io_channel_shutdown(iochan
, FALSE
, &error
); /* No flush */
407 g_warning("Can not close file: \n%s\n", error
->message
);
415 g_markup_parse_context_free(context
);
417 g_io_channel_shutdown(iochan
, FALSE
, &error
); /* No flush */
419 g_warning("Can not close file: \n%s\n", error
->message
);
430 /*****************************************************************************
431 *The following functions get facility/tracefile information
432 ****************************************************************************/
434 gint
getFacilityInfo(LttTrace
*t
, gchar
* eventdefs
)
441 gchar fullname
[DIR_NAME_SIZE
];
442 GError
* error
= NULL
;
444 dir
= g_dir_open(eventdefs
, 0, &error
);
447 g_warning("Can not open directory: %s, %s\n", eventdefs
, error
->message
);
452 while((name
= g_dir_read_name(dir
)) != NULL
){
453 if(!g_pattern_match_simple("*.xml", name
)) continue;
454 strcpy(fullname
,eventdefs
);
455 strcat(fullname
,name
);
456 ltt_facility_open(t
,fullname
);
460 for(j
=0;j
<t
->facility_number
;j
++){
461 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, j
);
462 for(i
=0; i
<f
->event_number
; i
++){
464 setFieldsOffset(NULL
, et
, NULL
, t
);
471 /*****************************************************************************
472 *A trace is specified as a pathname to the directory containing all the
473 *associated data (control tracefiles, per cpu tracefiles, event
476 *When a trace is closed, all the associated facilities, types and fields
477 *are released as well.
481 /****************************************************************************
482 * get_absolute_pathname
484 * return the unique pathname in the system
486 * MD : Fixed this function so it uses realpath, dealing well with
487 * forgotten cases (.. were not used correctly before).
489 ****************************************************************************/
490 void get_absolute_pathname(const gchar
*pathname
, gchar
* abs_pathname
)
492 abs_pathname
[0] = '\0';
494 if ( realpath (pathname
, abs_pathname
) != NULL
)
498 /* error, return the original path unmodified */
499 strcpy(abs_pathname
, pathname
);
505 /* Search for something like : .*_.*
507 * The left side is the name, the right side is the number.
510 int get_tracefile_name_number(const gchar
*raw_name
,
514 guint raw_name_len
= strlen(raw_name
);
515 gchar char_name
[PATH_MAX
];
522 for(i
=raw_name_len
-1;i
>=0;i
--) {
523 if(raw_name
[i
] == '_') break;
525 if(i
==0) /* Either not found or name length is 0 */
529 cpu_num
= strtol(raw_name
+underscore_pos
+1, &endptr
, 10);
531 if(endptr
== raw_name
+underscore_pos
+1)
532 return -1; /* No digit */
533 if(cpu_num
== LONG_MIN
|| cpu_num
== LONG_MAX
)
534 return -1; /* underflow / overflow */
536 strncpy(char_name
, raw_name
, underscore_pos
);
538 *name
= g_quark_from_string(char_name
);
545 GData
*ltt_trace_get_tracefiles_groups(LttTrace
*trace
)
547 return trace
->tracefiles
;
551 void compute_tracefile_group(GArray
*group
,
552 struct compute_tracefile_group_args args
)
557 for(i
=0; i
<group
->len
; i
++) {
558 tf
= &g_array_index (group
, LttTracefile
, i
);
560 args
.func(tf
, args
.func_args
);
565 void ltt_tracefile_group_destroy(gpointer data
)
567 GArray
*group
= (GArray
*)data
;
571 for(i
=0; i
<group
->len
; i
++) {
572 tf
= &g_array_index (group
, LttTracefile
, i
);
574 ltt_tracefile_close(tf
);
576 g_array_free(group
, TRUE
);
579 gboolean
ltt_tracefile_group_has_cpu_online(gpointer data
)
581 GArray
*group
= (GArray
*)data
;
585 for(i
=0; i
<group
->len
; i
++) {
586 tf
= &g_array_index (group
, LttTracefile
, i
);
587 if(tf
->cpu_online
) return 1;
593 /* Open each tracefile under a specific directory. Put them in a
594 * GData : permits to access them using their tracefile group pathname.
595 * i.e. access control/modules tracefile group by index :
598 * A tracefile group is simply an array where all the per cpu tracefiles sits.
601 static int open_tracefiles(LttTrace
*trace
, char *root_path
, GData
*tracefiles
)
603 DIR *dir
= opendir(root_path
);
604 struct dirent
*entry
;
605 struct stat stat_buf
;
616 strncpy(path
, root_path
, PATH_MAX
-1);
617 path_len
= strlen(path
);
618 path
[path_len
] = '/';
620 path_ptr
= path
+ path_len
;
622 while((entry
= readdir(dir
)) != NULL
) {
624 if(entry
->d_name
[0] == '.') continue;
626 strncpy(path_ptr
, entry
->d_name
, PATH_MAX
- path_len
);
628 ret
= stat(path
, &stat_buf
);
634 g_debug("Tracefile file or directory : %s\n", path
);
636 if(S_ISDIR(stat_buf
.st_mode
)) {
638 g_debug("Entering subdirectory...\n");
639 ret
= open_tracefiles(trace
, path
, tracefiles
);
640 if(ret
< 0) continue;
641 } else if(S_ISREG(stat_buf
.st_mode
)) {
642 g_debug("Opening file.\n");
650 if(get_tracefile_name_number(path
, &name
, &num
))
651 continue; /* invalid name */
653 group
= g_datalist_id_get_data(&tracefiles
, name
);
655 /* Elements are automatically cleared when the array is allocated.
656 * It makes the cpu_online variable set to 0 : cpu offline, by default.
658 group
= g_array_sized_new (FALSE
, TRUE
, sizeof(LttTracefile
), 10);
659 g_datalist_id_set_data_full(&tracefiles
, name
,
660 group
, ltt_tracefile_group_destroy
);
662 /* Add the per cpu tracefile to the named group */
663 unsigned int old_len
= group
->len
;
665 group
= g_array_set_size(group
, num
+1);
666 tf
= &g_array_index (group
, LttTracefile
, num
);
668 if(ltt_tracefile_open(trace
, path
, tf
)) {
669 g_info("Error opening tracefile %s", path
);
670 g_array_set_size(group
, old_len
);
672 if(!ltt_tracefile_group_has_cpu_online(group
))
673 g_datalist_id_remove_data(&tracefiles
, name
);
675 continue; /* error opening the tracefile : bad magic number ? */
687 /* ltt_get_facility_description
689 * Opens the trace corresponding to the requested facility (identified by fac_id
692 * The name searched is : %trace root%/eventdefs/facname_checksum.xml
694 * Returns 0 on success, or 1 on failure.
697 static int ltt_get_facility_description(LttFacility
*f
,
699 LttTracefile
*fac_tf
)
701 char desc_file_name
[PATH_MAX
];
708 text
= g_quark_to_string(t
->pathname
);
709 textlen
= strlen(text
);
711 if(textlen
>= PATH_MAX
) goto name_error
;
712 strcpy(desc_file_name
, text
);
714 text
= "/eventdefs/";
715 textlen
+=strlen(text
);
716 if(textlen
>= PATH_MAX
) goto name_error
;
717 strcat(desc_file_name
, text
);
719 text
= g_quark_to_string(f
->name
);
720 textlen
+=strlen(text
);
721 if(textlen
>= PATH_MAX
) goto name_error
;
722 strcat(desc_file_name
, text
);
725 textlen
+=strlen(text
);
726 if(textlen
>= PATH_MAX
) goto name_error
;
727 strcat(desc_file_name
, text
);
729 err
= snprintf(desc_file_name
+textlen
, PATH_MAX
-textlen
-1,
731 if(err
) goto name_error
;
733 textlen
=strlen(desc_file_name
);
736 textlen
+=strlen(text
);
737 if(textlen
>= PATH_MAX
) goto name_error
;
738 strcat(desc_file_name
, text
);
740 err
= ltt_facility_open(f
, t
, desc_file_name
);
741 if(err
) goto facility_error
;
743 for(i
=0;i
<t
->facilities_by_num
->len
;i
++){
744 f
= &g_array_index(t
->facilities_by_num
, LttFacility
, i
);
746 for(j
=0; j
<f
->events
->len
; j
++){
747 et
= &g_array_index(f
->events
, LttEventType
, j
);
748 set_fields_offsets(fac_tf
, et
);
761 static void ltt_fac_ids_destroy(gpointer data
)
763 GArray
*fac_ids
= (GArray
*)data
;
767 for(i
=0; i
<fac_ids
->len
; i
++) {
768 fac
= &g_array_index (fac_ids
, LttFacility
, i
);
769 ltt_facility_close(fac
);
772 g_array_free(fac_ids
, TRUE
);
776 /* Presumes the tracefile is already seeked at the beginning. It makes sense,
777 * because it must be done just after the opening */
778 static int ltt_process_facility_tracefile(LttTracefile
*tf
)
785 err
= ltt_tracefile_read_seek(tf
);
786 if(err
== EPERM
) goto seek_error
;
787 else if(err
== ERANGE
) break; /* End of tracefile */
789 err
= ltt_tracefile_read_update_event(tf
);
790 if(err
) goto update_error
;
792 /* We are on a facility load/or facility unload/ or heartbeat event */
794 * * facility 0 is hardcoded : this is the core facility. It will be shown
795 * in the facility array though, and is shown as "loaded builtin" in the
797 * It contains event :
799 * 1 : facility unload
800 * 2 : state dump facility load
801 * Facility 1 : (heartbeat)
804 if(tf
->event
.facility_id
> 1) { /* Should only contain core and heartbeat
806 g_warning("Error in processing facility file %s, "
807 "should not contain facility id %u.", g_quark_to_string(tf
->name
),
808 tf
->event
.facility_id
);
811 } else if(tf
->event
.facility_id
== LTT_FACILITY_CORE
) {
813 struct LttFacilityLoad
*fac_load_data
;
817 switch((enum ltt_core_events
)tf
->event
.event_id
) {
818 case LTT_EVENT_FACILITY_LOAD
:
820 (struct LttFacilityLoad
*)tf
->event
.data
;
822 (char*)(tf
->event
.data
+ sizeof(struct LttFacilityLoad
));
823 fac
= &g_array_index (tf
->trace
->facilities_by_num
, LttFacility
,
824 ltt_get_uint32(LTT_GET_BO(tf
), &fac_load_data
->id
));
825 g_assert(fac
->exists
== 0);
826 fac
->name
= g_quark_from_string(fac_name
);
827 fac
->checksum
= ltt_get_uint32(LTT_GET_BO(tf
),
828 &fac_load_data
->checksum
);
829 fac
->id
= ltt_get_uint32(LTT_GET_BO(tf
), &fac_load_data
->id
);
830 fac
->pointer_size
= ltt_get_uint32(LTT_GET_BO(tf
),
831 &fac_load_data
->pointer_size
);
832 fac
->size_t_size
= ltt_get_uin32(LTT_GET_BO(tf
),
833 &fac_load_data
->size_t_size
);
834 fac
->alignment
= ltt_get_uint32(LTT_GET_BO(tf
),
835 &fac_load_data
->alignment
);
837 if(ltt_get_facility_description(fac
, tf
->trace
, tf
))
840 fac
->trace
= tf
->trace
;
843 fac_ids
= g_datalist_id_get_data(&tf
->trace
->facilities_by_name
,
845 if(fac_ids
== NULL
) {
846 fac_ids
= g_array_sized_new (FALSE
, TRUE
, sizeof(guint
), 1);
847 g_datalist_id_set_data_full(&tf
->trace
->facilities_by_name
,
849 fac_ids
, ltt_fac_ids_destroy
);
851 g_array_append_val(fac_ids
, fac
->id
);
854 case LTT_EVENT_FACILITY_UNLOAD
:
855 /* We don't care about unload : facilities ID are valid for the whole
856 * trace. They simply won't be used after the unload. */
858 case LTT_EVENT_STATE_DUMP_FACILITY_LOAD
:
860 (struct LttFacilityLoad
*)tf
->event
.data
;
862 (char*)(tf
->event
.data
+ sizeof(struct LttFacilityLoad
));
863 fac
= &g_array_index (tf
->trace
->facilities_by_num
, LttFacility
,
864 ltt_get_uint32(LTT_GET_BO(tf
), &fac_load_data
->id
));
865 g_assert(fac
->exists
== 0);
866 fac
->name
= g_quark_from_string(fac_name
);
867 fac
->checksum
= ltt_get_uint32(LTT_GET_BO(tf
),
868 &fac_load_data
->checksum
);
869 fac
->id
= ltt_get_uint8(LTT_GET_BO(tf
), &fac_load_data
->id
);
870 fac
->pointer_size
= ltt_get_uint32(LTT_GET_BO(tf
),
871 &fac_load_data
->pointer_size
);
872 fac
->size_t_size
= ltt_get_uin32(LTT_GET_BO(tf
),
873 &fac_load_data
->size_t_size
);
874 fac
->alignment
= ltt_get_uint32(LTT_GET_BO(tf
),
875 &fac_load_data
->alignment
);
876 if(ltt_get_facility_description(fac
, tf
->trace
, tf
))
879 fac
->trace
= tf
->trace
;
883 fac_ids
= g_datalist_id_get_data(&tf
->trace
->facilities_by_name
,
885 if(fac_ids
== NULL
) {
886 fac_ids
= g_array_sized_new (FALSE
, TRUE
, sizeof(guint
), 1);
887 g_datalist_id_set_data_full(&tf
->trace
->facilities_by_name
,
889 fac_ids
, ltt_fac_ids_destroy
);
891 g_array_append_val(fac_ids
, fac
->id
);
894 case LTT_EVENT_HEARTBEAT
:
897 g_warning("Error in processing facility file %s, "
898 "unknown event id %hhu in core facility.",
899 g_quark_to_string(tf
->name
),
918 LttTrace
*ltt_trace_open(const gchar
*pathname
)
920 gchar abs_path
[PATH_MAX
];
926 t
= g_new(LttTrace
, 1);
927 if(!t
) goto alloc_error
;
929 get_absolute_pathname(pathname
, abs_path
);
930 t
->pathname
= g_quark_from_string(abs_path
);
932 /* Open all the tracefiles */
933 g_datalist_init(&t
->tracefiles
);
934 if(open_tracefiles(t
, abs_path
, t
->tracefiles
))
937 /* Prepare the facilities containers : array and mapping */
938 /* Array is zeroed : the "exists" field is set to false by default */
939 t
->facilities_by_num
= g_array_sized_new (FALSE
,
940 TRUE
, sizeof(LttFacility
),
942 t
->facilities_by_num
= g_array_set_size(t
->facilities_by_num
, NUM_FACILITIES
);
944 g_datalist_init(&t
->facilities_by_name
);
946 /* Parse each trace control/facilitiesN files : get runtime fac. info */
947 group
= g_datalist_id_get_data(&t
->tracefiles
, LTT_TRACEFILE_NAME_FACILITIES
);
949 g_error("Trace %s has no facility tracefile", abs_path
);
950 goto facilities_error
;
953 for(i
=0; i
<group
->len
; i
++) {
954 tf
= &g_array_index (group
, LttTracefile
, i
);
955 if(ltt_process_facility_tracefile(tf
))
956 goto facilities_error
;
965 g_datalist_clear(&t
->facilities_by_name
);
966 g_array_free(t
->facilities_by_num
, TRUE
);
968 g_datalist_clear(&t
->tracefiles
);
975 GQuark
ltt_trace_name(LttTrace
*t
)
981 /******************************************************************************
982 * When we copy a trace, we want all the opening actions to happen again :
983 * the trace will be reopened and totally independant from the original.
984 * That's why we call ltt_trace_open.
985 *****************************************************************************/
986 LttTrace
*ltt_trace_copy(LttTrace
*self
)
988 return ltt_trace_open(g_quark_to_string(self
->pathname
));
991 void ltt_trace_close(LttTrace
*t
)
993 g_datalist_clear(&t
->facilities_by_name
);
994 g_array_free(t
->facilities_by_num
, TRUE
);
995 g_datalist_clear(&t
->tracefiles
);
1000 /*****************************************************************************
1001 *Get the system description of the trace
1002 ****************************************************************************/
1004 LttFacility
*ltt_trace_facility_by_id(LttTrace
*t
, guint8 id
)
1006 g_assert(id
< t
->facilities_by_num
->len
);
1007 return &g_array_index(t
->facilities_by_num
, LttFacility
, id
);
1010 /* ltt_trace_facility_get_by_name
1012 * Returns the GArray of facility indexes. All the fac_ids that matches the
1013 * requested facility name.
1015 * If name is not found, returns NULL.
1017 GArray
*ltt_trace_facility_get_by_name(LttTrace
*t
, GQuark name
)
1019 return g_datalist_id_get_data(&t
->facilities_by_name
, name
);
1022 /*****************************************************************************
1023 * Functions to discover all the event types in the trace
1024 ****************************************************************************/
1027 unsigned ltt_trace_eventtype_number(LttTrace
*t
)
1031 unsigned int num
= t
->facility_number
;
1035 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, i
);
1036 count
+= f
->event_number
;
1043 //use an iteration on all the trace facilities, and inside iteration on all the
1044 //event types in each facilities instead.
1045 LttEventType
*ltt_trace_eventtype_get(LttTrace
*t
, unsigned evId
)
1047 LttEventType
*event_type
;
1050 f
= ltt_trace_facility_by_id(t
,evId
);
1052 if(unlikely(!f
)) event_type
= NULL
;
1053 else event_type
= f
->events
[evId
- f
->base_id
];
1060 /*****************************************************************************
1061 * ltt_trace_find_tracefile
1063 * Find a tracefile by name and index in the group.
1065 * Returns a pointer to the tracefiles, else NULL.
1066 ****************************************************************************/
1068 LttTracefile
*ltt_trace_find_tracefile(LttTrace
*t
, const gchar
*name
)
1073 /*****************************************************************************
1074 * Get the start time and end time of the trace
1075 ****************************************************************************/
1077 static void ltt_tracefile_time_span_get(LttTracefile
*tf
,
1078 LttTime
*start
, LttTime
*end
)
1080 struct ltt_block_start_header
* header
;
1083 err
= map_block(tf
, 0);
1085 g_error("Can not map block");
1086 *start
= ltt_time_infinite
;
1088 *start
= tf
->buffer
.begin
.timestamp
;
1090 err
= map_block(tf
, tf
->num_blocks
- 1); /* Last block */
1092 g_error("Can not map block");
1093 *end
= ltt_time_zero
;
1095 *end
= tf
->buffer
.end
.timestamp
;
1098 struct tracefile_time_span_get_args
{
1104 static void group_time_span_get(GQuark name
, gpointer data
, gpointer user_data
)
1106 struct tracefile_time_span_get_args
*args
=
1107 (struct tracefile_time_span_get_args
*)user_data
;
1109 GArray
*group
= (GArray
*)data
;
1115 for(i
=0; i
<group
->len
; i
++) {
1116 tf
= &g_array_index (group
, LttTracefile
, i
);
1117 if(tf
->cpu_online
) {
1118 ltt_tracefile_time_span_get(tf
, &tmp_start
, &tmp_end
);
1119 if(ltt_time_compare(*args
->start
, tmp_start
)>0) *args
->start
= tmp_start
;
1120 if(ltt_time_compare(*args
->end
, tmp_end
)<0) *args
->end
= tmp_end
;
1125 void ltt_trace_time_span_get(LttTrace
*t
, LttTime
*start
, LttTime
*end
)
1127 LttTime min_start
= ltt_time_infinite
;
1128 LttTime max_end
= ltt_time_zero
;
1129 struct tracefile_time_span_get_args args
= { t
, &min_start
, &max_end
};
1131 g_datalist_foreach(&t
->tracefiles
, &group_time_span_get
, &args
);
1133 if(start
!= NULL
) *start
= min_start
;
1134 if(end
!= NULL
) *end
= max_end
;
1139 /*****************************************************************************
1140 *Get the name of a tracefile
1141 ****************************************************************************/
1143 GQuark
ltt_tracefile_name(LttTracefile
*tf
)
1148 /*****************************************************************************
1149 * Get the number of blocks in the tracefile
1150 ****************************************************************************/
1152 guint
ltt_tracefile_block_number(LttTracefile
*tf
)
1154 return tf
->num_blocks
;
1158 /* Seek to the first event in a tracefile that has a time equal or greater than
1159 * the time passed in parameter.
1161 * If the time parameter is outside the tracefile time span, seek to the first
1162 * or the last event of the tracefile.
1164 * If the time parameter is before the first event, we have to seek specially to
1167 * If the time is after the end of the trace, get the last event.
1169 * Do a binary search to find the right block, then a sequential search in the
1170 * block to find the event.
1172 * In the special case where the time requested fits inside a block that has no
1173 * event corresponding to the requested time, the first event of the next block
1176 * IMPORTANT NOTE : // FIXME everywhere...
1178 * You MUST NOT do a ltt_tracefile_read right after a ltt_tracefile_seek_time :
1179 * you will jump over an event if you do.
1181 * Return value : 0 : no error, the tf->event can be used
1182 * otherwise : this is an error.
1186 int ltt_tracefile_seek_time(LttTracefile
*tf
, LttTime time
)
1190 unsigned int block_num
, high
, low
;
1192 /* seek at the beginning of trace */
1193 err
= map_block(tf
, 0); /* First block */
1195 g_error("Can not map block");
1199 /* If the time is lower or equal the beginning of the trace,
1200 * go to the first event. */
1201 if(ltt_time_compare(time
, tf
->buffer
.begin
.timestamp
) <= 0) {
1202 ret
= ltt_tracefile_read(tf
);
1203 goto found
; /* There is either no event in the trace or the event points
1204 to the first event in the trace */
1207 err
= map_block(tf
, tf
->num_blocks
- 1); /* Last block */
1209 g_error("Can not map block");
1213 /* If the time is after the end of the trace, get the last event. */
1214 if(ltt_time_compare(time
, tf
->buffer
.end
.timestamp
) >= 0) {
1215 /* While the ltt_tracefile_read doesn't return ERANGE or EPERM,
1219 ret
= ltt_tracefile_read(tf
);
1220 if(ret
== ERANGE
) goto found
; /* ERANGE or EPERM */
1221 else if(ret
) goto fail
;
1225 /* Binary search the block */
1226 high
= tf
->num_blocks
- 1;
1230 block_num
= ((high
-low
) / 2) + low
;
1232 err
= map_block(tf
, block_num
);
1234 g_error("Can not map block");
1238 /* We cannot divide anymore : this is what would happen if the time
1239 * requested was exactly between two consecutive buffers'end and start
1240 * timestamps. This is also what would happend if we didn't deal with out
1241 * of span cases prior in this function. */
1242 /* The event is right in the buffer!
1243 * (or in the next buffer first event) */
1245 ret
= ltt_tracefile_read(tf
);
1246 if(ret
== ERANGE
) goto found
; /* ERANGE or EPERM */
1247 else if(ret
) goto fail
;
1249 if(ltt_time_compare(time
, tf
->event
.event_time
) >= 0)
1253 } if(ltt_time_compare(time
, tf
->buffer
.begin
.timestamp
) < 0) {
1254 /* go to lower part */
1256 } else if(ltt_time_compare(time
, tf
->buffer
.end
.timestamp
) > 0) {
1257 /* go to higher part */
1259 } else {/* The event is right in the buffer!
1260 (or in the next buffer first event) */
1262 ltt_tracefile_read(tf
);
1263 if(ret
== ERANGE
) goto found
; /* ERANGE or EPERM */
1264 else if(ret
) goto fail
;
1266 if(ltt_time_compare(time
, tf
->event
.event_time
) >= 0)
1276 /* Error handling */
1278 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1279 g_quark_to_string(tf
->name
));
1284 int ltt_tracefile_seek_position(LttTracefile
*tf
, const LttEventPosition
*ep
) {
1288 if(ep
->tracefile
!= tf
) {
1292 err
= map_block(tf
, ep
->block
);
1294 g_error("Can not map block");
1298 tf
->event
.offset
= ep
->offset
;
1300 err
= ltt_tracefile_read_update_event(tf
);
1302 err
= ltt_tracefile_read_op(tf
);
1308 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1309 g_quark_to_string(tf
->name
));
1312 /* Calculate the real event time based on the buffer boundaries */
1313 LttTime
ltt_interpolate_time(LttTracefile
*tf
, LttEvent
*event
)
1317 g_assert(tf
->trace
->has_tsc
);
1319 time
= ltt_time_from_uint64(
1320 (guint64
)tf
->buffer
.tsc
*tf
->buffer
.nsecs_per_cycle
);
1321 time
= ltt_time_add(tf
->buffer
.begin
.timestamp
, time
);
1326 /*****************************************************************************
1328 * ltt_tracefile_read : Read the next event in the tracefile
1333 * Returns 0 if an event can be used in tf->event.
1334 * Returns ERANGE on end of trace. The event in tf->event still can be used.
1335 * Returns EPERM on error.
1337 * This function does make the tracefile event structure point to the event
1338 * currently pointed to by the tf->event.
1340 * Note : you must call a ltt_tracefile_seek to the beginning of the trace to
1341 * reinitialize it after an error if you want results to be coherent.
1342 * It would be the case if a end of trace last buffer has no event : the end
1343 * of trace wouldn't be returned, but an error.
1344 * We make the assumption there is at least one event per buffer.
1345 ****************************************************************************/
1347 int ltt_tracefile_read(LttTracefile
*tf
)
1351 err
= ltt_tracefile_read_seek(tf
);
1353 err
= ltt_tracefile_read_update_event(tf
);
1355 err
= ltt_tracefile_read_op(tf
);
1361 int ltt_tracefile_read_seek(LttTracefile
*tf
)
1365 /* Get next buffer until we finally have an event, or end of trace */
1367 err
= ltt_seek_next_event(tf
);
1368 if(unlikely(err
== ENOPROTOOPT
)) {
1372 /* Are we at the end of the buffer ? */
1374 if(unlikely(tf
->buffer
.index
== tf
->num_blocks
-1)){ /* end of trace ? */
1377 /* get next block */
1378 err
= map_block(tf
, tf
->buffer
.index
+ 1);
1380 g_error("Can not map block");
1384 } else break; /* We found an event ! */
1391 /* do specific operation on events */
1392 int ltt_tracefile_read_op(LttTracefile
*tf
)
1401 /* do event specific operation */
1403 /* do something if its an heartbeat event : increment the heartbeat count */
1404 //if(event->facility_id == LTT_FACILITY_CORE)
1405 // if(event->event_id == LTT_EVENT_HEARTBEAT)
1406 // tf->cur_heart_beat_number++;
1412 /* same as ltt_tracefile_read, but does not seek to the next event nor call
1413 * event specific operation. */
1414 int ltt_tracefile_read_update_event(LttTracefile
*tf
)
1422 pos
= event
->offset
;
1424 /* Read event header */
1428 if(tf
->trace
->has_tsc
) {
1429 event
->time
.timestamp
= ltt_get_uint32(LTT_GET_BO(tf
),
1431 /* 32 bits -> 64 bits tsc */
1432 /* note : still works for seek and non seek cases. */
1433 if(event
->time
.timestamp
< (0xFFFFFFFFULL
&tf
->buffer
.tsc
)) {
1434 tf
->buffer
.tsc
= ((tf
->buffer
.tsc
&0xFFFFFFFF00000000ULL
)
1436 | (guint64
)event
->time
.timestamp
;
1437 event
->tsc
= tf
->buffer
.tsc
;
1440 tf
->buffer
.tsc
= (tf
->buffer
.tsc
&0xFFFFFFFF00000000ULL
)
1441 | (guint64
)event
->time
.timestamp
;
1442 event
->tsc
= tf
->buffer
.tsc
;
1445 event
->event_time
= ltt_interpolate_time(tf
, event
);
1447 pos
+= sizeof(guint32
);
1449 event
->time
.delta
.tv_sec
= 0;
1450 event
->time
.delta
.tv_nsec
= ltt_get_uint32(LTT_GET_BO(tf
),
1451 pos
) * NSEC_PER_USEC
;
1453 event
->tsc
= tf
->buffer
.tsc
;
1455 event
->event_time
= ltt_time_add(tf
->buffer
.begin
.timestamp
,
1457 pos
+= sizeof(guint32
);
1460 event
->facility_id
= ltt_get_uint8(LTT_GET_BO(tf
),
1462 pos
+= sizeof(guint8
);
1464 event
->event_id
= ltt_get_uint8(LTT_GET_BO(tf
),
1466 pos
+= sizeof(guint8
);
1470 /* get the data size and update the event fields with the current
1472 event
->data_size
= ltt_update_event_size(tf
);
1478 /****************************************************************************
1480 * map_block : map a block from the file
1482 * lttdes : ltt trace file
1483 * whichBlock : the block which will be read
1486 * EINVAL : lseek fail
1487 * EIO : can not read from the file
1488 ****************************************************************************/
1490 static gint
map_block(LttTracefile
* tf
, guint block_num
)
1492 struct ltt_block_start_header
*header
;
1494 g_assert(block_num
< tf
->num_blocks
);
1496 if(tf
->buffer
.head
!= NULL
)
1497 munmap(tf
->buffer
.head
, tf
->buf_size
);
1499 /* Multiple of pages aligned head */
1500 tf
->buffer
.head
= mmap(0, tf
->block_size
, PROT_READ
, tf
->fd
, MAP_PRIVATE
,
1501 (off_t
)tf
->block_size
* (off_t
)block_num
);
1503 if(tf
->buffer
.head
== NULL
) {
1504 perror("Error in allocating memory for buffer of tracefile");
1507 g_assert(((guint
)tf
->buffer
.head
) & (8-1) == 0); // make sure it's aligned.
1510 tf
->buffer
.index
= block_num
;
1512 header
= (struct ltt_block_start_header
*)tf
->buffer
.head
;
1514 tf
->buffer
.begin
.timestamp
= ltt_get_time(LTT_GET_BO(tf
),
1515 &header
->begin
.timestamp
);
1516 tf
->buffer
.begin
.timestamp
.tv_nsec
*= NSEC_PER_USEC
;
1517 tf
->buffer
.begin
.cycle_count
= ltt_get_uint64(LTT_GET_BO(tf
),
1518 &header
->begin
.cycle_count
);
1519 tf
->buffer
.end
.timestamp
= ltt_get_time(LTT_GET_BO(tf
),
1520 &header
->end
.timestamp
);
1521 tf
->buffer
.end
.timestamp
.tv_nsec
*= NSEC_PER_USEC
;
1522 tf
->buffer
.end
.cycle_count
= ltt_get_uint64(LTT_GET_BO(tf
),
1523 &header
->end
.cycle_count
);
1524 tf
->buffer
.lost_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1525 &header
->lost_size
);
1527 tf
->buffer
.tsc
= tf
->buffer
.begin
.cycle_count
;
1528 tf
->event
.tsc
= tf
->buffer
.tsc
;
1531 * eventually support variable buffer size : will need a partial pre-read of
1532 * the headers to create an index when we open the trace... eventually. */
1533 g_assert(tf
->block_size
== ltt_get_uint32(LTT_GET_BO(tf
),
1534 &header
->buf_size
));
1536 /* Now that the buffer is mapped, calculate the time interpolation for the
1539 tf
->buffer
.nsecs_per_cycle
= calc_nsecs_per_cycle(tf
);
1541 /* Make the current event point to the beginning of the buffer :
1542 * it means that the event read must get the first event. */
1543 tf
->event
.tracefile
= tf
;
1544 tf
->event
.block
= block_num
;
1545 tf
->event
.offset
= tf
->buffer
.head
;
1554 /* It will update the fields offsets too */
1555 void ltt_update_event_size(LttTracefile
*tf
)
1559 /* Specific handling of core events : necessary to read the facility control
1561 LttFacility
*f
= ltt_trace_get_facility_by_num(tf
->trace
,
1562 tf
->event
.facility_id
);
1564 if(unlikely(f
== NULL
)) {
1565 if(likely(tf
->event
.facility_id
== LTT_FACILITY_CORE
)) {
1566 switch((enum ltt_core_events
)tf
->event
.event_id
) {
1567 case LTT_EVENT_FACILITY_LOAD
:
1568 size
= sizeof(struct LttFacilityLoad
);
1570 case LTT_EVENT_FACILITY_UNLOAD
:
1571 size
= sizeof(struct LttFacilityUnload
);
1573 case LTT_EVENT_STATE_DUMP_FACILITY_LOAD
:
1574 size
= sizeof(struct LttStateDumpFacilityLoad
);
1576 case LTT_EVENT_HEARTBEAT
:
1577 size
= sizeof(TimeHeartbeat
);
1580 g_warning("Error in getting event size : tracefile %s, "
1581 "unknown event id %hhu in core facility.",
1582 g_quark_to_string(tf
->name
),
1583 tf
->event
.event_id
);
1584 goto event_id_error
;
1589 LttEventType
*event_type
=
1590 ltt_facility_eventtype_get(f
, tf
->event
.event_id
);
1591 size
= get_fields_offsets(tf
, event_type
, tf
->event
.data
);
1594 tf
->event
.data_size
= size
;
1600 /* Take the tf current event offset and use the event facility id and event id
1601 * to figure out where is the next event offset.
1603 * This is an internal function not aiming at being used elsewhere : it will
1604 * not jump over the current block limits. Please consider using
1605 * ltt_tracefile_read to do this.
1607 * Returns 0 on success
1608 * ERANGE if we are at the end of the buffer.
1609 * ENOPROTOOPT if an error occured when getting the current event size.
1611 static int ltt_seek_next_event(LttTracefile
*tf
)
1617 /* seek over the buffer header if we are at the buffer start */
1618 if(tf
->event
.offset
== tf
->buffer
.head
) {
1619 tf
->event
.offset
+= sizeof(struct ltt_block_start_header
);
1624 if(tf
->event
.offset
== tf
->buffer
.head
+ tf
->buffer
.lost_size
) {
1629 pos
= tf
->event
.data
;
1631 if(tf
->event
.data_size
< 0) goto error
;
1633 pos
+= (size_t)tf
->event
.data_size
;
1635 tf
->event
.offset
= pos
;
1641 g_error("Error in ltt_seek_next_event for tracefile %s",
1642 g_quark_to_string(tf
->name
));
1647 /*****************************************************************************
1649 * calc_nsecs_per_cycle : calculate nsecs per cycle for current block
1652 ****************************************************************************/
1654 static double calc_nsecs_per_cycle(LttTracefile
* tf
)
1656 LttTime lBufTotalTime
; /* Total time for this buffer */
1657 double lBufTotalNSec
; /* Total time for this buffer in nsecs */
1658 LttCycleCount lBufTotalCycle
;/* Total cycles for this buffer */
1660 /* Calculate the total time for this buffer */
1661 lBufTotalTime
= ltt_time_sub(
1662 ltt_get_time(LTT_GET_BO(tf
), &tf
->buffer
.end
.timestamp
),
1663 ltt_get_time(LTT_GET_BO(tf
), &tf
->buffer
.begin
.timestamp
));
1665 /* Calculate the total cycles for this bufffer */
1666 lBufTotalCycle
= ltt_get_uint64(LTT_GET_BO(tf
), &tf
->buffer
.end
.cycle_count
);
1667 lBufTotalCycle
-= ltt_get_uint64(LTT_GET_BO(tf
),
1668 &tf
->buffer
.begin
.cycle_count
);
1670 /* Convert the total time to double */
1671 lBufTotalNSec
= ltt_time_to_double(lBufTotalTime
);
1673 return lBufTotalNSec
/ (double)lBufTotalCycle
;
1677 void setFieldsOffset(LttTracefile
*tf
, LttEventType
*evT
,void *evD
)
1679 LttField
* rootFld
= evT
->root_field
;
1680 // rootFld->base_address = evD;
1683 rootFld
->field_size
= getFieldtypeSize(tf
, evT
->facility
,
1684 evT
, 0,0,rootFld
, evD
);
1688 /*****************************************************************************
1690 * set_fields_offsets : set the precomputable offset of the fields
1692 * tracefile : opened trace file
1693 * event_type : the event type
1694 ****************************************************************************/
1696 void set_fields_offsets(LttTracefile
*tf
, LttEventType
*event_type
)
1698 LttField
*field
= event_type
->root_field
;
1699 enum field_status fixed_root
= FIELD_FIXED
, fixed_parent
= FIELD_FIXED
;
1702 preset_field_type_size(tf
, event_type
, 0, 0,
1703 &fixed_root
, &fixed_parent
,
1709 /*****************************************************************************
1711 * preset_field_type_size : set the fixed sizes of the field type
1714 * event_type : event type
1715 * offset_root : offset from the root
1716 * offset_parent : offset from the parent
1717 * fixed_root : Do we know a fixed offset to the root ?
1718 * fixed_parent : Do we know a fixed offset to the parent ?
1720 ****************************************************************************/
1721 void preset_field_type_size(LttTracefile
*tf
, LttEventType
*event_type
,
1722 off_t offset_root
, off_t offset_parent
,
1723 enum field_status
*fixed_root
, enum field_status
*fixed_parent
,
1726 enum field_status local_fixed_root
, local_fixed_parent
;
1730 g_assert(field
->fixed_root
== FIELD_UNKNOWN
);
1731 g_assert(field
->fixed_parent
== FIELD_UNKNOWN
);
1732 g_assert(field
->fixed_size
== FIELD_UNKNOWN
);
1734 type
= field
->field_type
;
1736 field
->fixed_root
= *fixed_root
;
1737 if(field
->fixed_root
== FIELD_FIXED
)
1738 field
->offset_root
= offset_root
;
1740 field
->offset_root
= 0;
1742 field
->fixed_parent
= *fixed_parent
;
1743 if(field
->fixed_parent
== FIELD_FIXED
)
1744 field
->offset_parent
= offset_parent
;
1746 field
->offset_parent
= 0;
1748 size_t current_root_offset
;
1749 size_t current_offset
;
1750 enum field_status current_child_status
, final_child_status
;
1753 switch(type
->type_class
) {
1758 field
->field_size
= ltt_type_size(tf
->trace
, type
);
1759 field
->fixed_size
= FIELD_FIXED
;
1762 field
->field_size
= (off_t
)event_type
->facility
->pointer_size
;
1763 field
->fixed_size
= FIELD_FIXED
;
1767 field
->field_size
= (off_t
)event_type
->facility
->pointer_size
;
1768 field
->fixed_size
= FIELD_FIXED
;
1773 field
->field_size
= (off_t
)event_type
->facility
->size_t_size
;
1774 field
->fixed_size
= FIELD_FIXED
;
1777 local_fixed_root
= FIELD_VARIABLE
;
1778 local_fixed_parent
= FIELD_VARIABLE
;
1779 preset_field_type_size(tf
, event_type
,
1781 &local_fixed_root
, &local_fixed_parent
,
1783 field
->fixed_size
= FIELD_VARIABLE
;
1784 field
->field_size
= 0;
1787 field
->fixed_size
= FIELD_VARIABLE
;
1788 field
->field_size
= 0;
1791 local_fixed_root
= FIELD_VARIABLE
;
1792 local_fixed_parent
= FIELD_VARIABLE
;
1793 preset_field_type_size(tf
, event_type
,
1795 &local_fixed_root
, &local_fixed_parent
,
1797 field
->fixed_size
= field
->child
[0]->fixed_size
;
1798 if(field
->fixed_size
== FIELD_FIXED
)
1799 field
->field_size
= type
->element_number
* field
->child
[0]->field_size
;
1801 field
->field_size
= 0;
1804 current_root_offset
= field
->offset_root
;
1806 current_child_status
= FIELD_FIXED
;
1807 for(i
=0;i
<type
->element_number
;i
++) {
1808 preset_field_type_size(tf
, event_type
,
1809 current_root_offset
, current_offset
,
1810 fixed_root
, ¤t_child_status
,
1812 if(current_child_status
== FIELD_FIXED
) {
1813 current_root_offset
+= field
->child
[i
]->field_size
;
1814 current_offset
+= field
->child
[i
]->field_size
;
1816 current_root_offset
= 0;
1820 if(current_child_status
!= FIELD_FIXED
) {
1821 *fixed_parent
= current_child_status
;
1822 field
->field_size
= 0;
1823 field
->fixed_size
= current_child_status
;
1825 field
->field_size
= current_offset
;
1826 field
->fixed_size
= FIELD_FIXED
;
1830 current_root_offset
= field
->offset_root
;
1833 final_child_status
= FIELD_FIXED
;
1834 for(i
=0;i
<type
->element_number
;i
++) {
1835 enum field_status current_root_child_status
= FIELD_FIXED
;
1836 enum field_status current_child_status
= FIELD_FIXED
;
1837 preset_field_type_size(tf
, event_type
,
1838 current_root_offset
, current_offset
,
1839 ¤t_root_child_status
, ¤t_child_status
,
1841 if(current_child_status
!= FIELD_FIXED
)
1842 final_child_status
= current_child_status
;
1844 max_size
= max(max_size
, field
->child
[i
]->field_size
);
1846 if(final_child_status
!= FIELD_FIXED
) {
1847 *fixed_root
= final_child_status
;
1848 *fixed_parent
= final_child_status
;
1849 field
->field_size
= 0;
1850 field
->fixed_size
= current_child_status
;
1852 field
->field_size
= max_size
;
1853 field
->fixed_size
= FIELD_FIXED
;
1861 /*****************************************************************************
1863 * get_field_type_size : set the fixed and dynamic sizes of the field type
1864 * from the data read.
1867 * event_type : event type
1868 * offset_root : offset from the root
1869 * offset_parent : offset from the parent
1871 * data : a pointer to the event data.
1872 *Returns the field type size.
1873 ****************************************************************************/
1874 size_t get_field_type_size(LttTracefile
*tf
, LttEventType
*event_type
,
1875 off_t offset_root
, off_t offset_parent
,
1876 LttField
*field
, void *data
)
1882 g_assert(field
->fixed_root
!= FIELD_UNKNOWN
);
1883 g_assert(field
->fixed_parent
!= FIELD_UNKNOWN
);
1884 g_assert(field
->fixed_size
!= FIELD_UNKNOWN
);
1886 field
->offset_root
= offset_root
;
1887 field
->offset_parent
= offset_parent
;
1889 type
= field
->field_type
;
1891 switch(type
->type_class
) {
1902 g_assert(field
->fixed_size
== FIELD_FIXED
);
1903 size
= field
->field_size
;
1907 gint seqnum
= ltt_get_uint(LTT_GET_BO(tf
),
1908 field
->sequ_number_size
,
1909 data
+ offset_root
);
1911 if(field
->child
[0]->fixed_size
== FIELD_FIXED
) {
1912 size
= field
->sequ_number_size
+
1913 (seqnum
* get_field_type_size(tf
, event_type
,
1914 offset_root
, offset_parent
,
1915 field
->child
[0], data
));
1917 size
+= field
->sequ_number_size
;
1918 for(i
=0;i
<seqnum
;i
++) {
1920 child_size
= get_field_type_size(tf
, event_type
,
1921 offset_root
, offset_parent
,
1922 field
->child
[0], data
);
1923 offset_root
+= child_size
;
1924 offset_parent
+= child_size
;
1928 field
->field_size
= size
;
1932 size
= strlen((char*)(data
+offset_root
)) + 1;// length + \0
1933 field
->field_size
= size
;
1936 if(field
->fixed_size
== FIELD_FIXED
)
1937 size
= field
->field_size
;
1939 for(i
=0;i
<field
->field_type
->element_number
;i
++) {
1941 child_size
= get_field_type_size(tf
, event_type
,
1942 offset_root
, offset_parent
,
1943 field
->child
[0], data
);
1944 offset_root
+= child_size
;
1945 offset_parent
+= child_size
;
1948 field
->field_size
= size
;
1952 if(field
->fixed_size
== FIELD_FIXED
)
1953 size
= field
->field_size
;
1955 size_t current_root_offset
= offset_root
;
1956 size_t current_offset
= 0;
1957 size_t child_size
= 0;
1958 for(i
=0;i
<type
->element_number
;i
++) {
1959 child_size
= get_field_type_size(tf
,
1960 event_type
, current_root_offset
, current_offset
,
1961 field
->child
[i
], data
);
1962 current_offset
+= child_size
;
1963 current_root_offset
+= child_size
;
1966 size
= current_offset
;
1967 field
->field_size
= size
;
1971 if(field
->fixed_size
== FIELD_FIXED
)
1972 size
= field
->field_size
;
1974 size_t current_root_offset
= field
->offset_root
;
1975 size_t current_offset
= 0;
1976 for(i
=0;i
<type
->element_number
;i
++) {
1977 size
= get_field_type_size(tf
, event_type
,
1978 current_root_offset
, current_offset
,
1979 field
->child
[i
], data
);
1980 size
= max(size
, field
->child
[i
]->field_size
);
1982 field
->field_size
= size
;
1991 /*****************************************************************************
1993 * check_fields_compatibility : Check for compatibility between two fields :
1994 * do they use the same inner structure ?
1996 * event_type1 : event type
1997 * event_type2 : event type
2000 *Returns : 0 if identical
2002 ****************************************************************************/
2003 gint
check_fields_compatibility(LttEventType
*event_type1
,
2004 LttEventType
*event_type2
,
2005 LttField
*field1
, LttField
*field2
)
2007 guint different
= 0;
2008 enum field_status local_fixed_root
, local_fixed_parent
;
2013 if(field1
== NULL
) {
2014 if(field2
== NULL
) goto end
;
2019 } else if(field2
== NULL
) {
2024 g_assert(field1
->fixed_root
!= FIELD_UNKNOWN
);
2025 g_assert(field2
->fixed_root
!= FIELD_UNKNOWN
);
2026 g_assert(field1
->fixed_parent
!= FIELD_UNKNOWN
);
2027 g_assert(field2
->fixed_parent
!= FIELD_UNKNOWN
);
2028 g_assert(field1
->fixed_size
!= FIELD_UNKNOWN
);
2029 g_assert(field2
->fixed_size
!= FIELD_UNKNOWN
);
2031 type1
= field1
->field_type
;
2032 type2
= field2
->field_type
;
2034 size_t current_root_offset
;
2035 size_t current_offset
;
2036 enum field_status current_child_status
, final_child_status
;
2039 if(type1
->type_class
!= type2
->type_class
) {
2043 if(type1
->element_name
!= type2
->element_name
) {
2048 switch(type1
->type_class
) {
2058 if(field1
->field_size
!= field2
->field_size
) {
2064 if(type1
->element_number
!= type2
->element_number
) {
2068 for(i
=0;i
<type1
->element_number
;i
++) {
2069 if(type1
->enum_strings
[i
] != type2
->enum_strings
[i
]) {
2076 /* Two elements : size and child */
2077 g_assert(type1
->element_number
!= type2
->element_number
);
2078 for(i
=0;i
<type1
->element_number
;i
++) {
2079 if(check_fields_compatibility(event_type1
, event_type2
,
2080 field1
->child
[0], field2
->child
[0])) {
2089 if(field1
->field_size
!= field2
->field_size
) {
2093 /* Two elements : size and child */
2094 g_assert(type1
->element_number
!= type2
->element_number
);
2095 for(i
=0;i
<type1
->element_number
;i
++) {
2096 if(check_fields_compatibility(event_type1
, event_type2
,
2097 field1
->child
[0], field2
->child
[0])) {
2105 if(type1
->element_number
!= type2
->element_number
) {
2109 for(i
=0;i
<type1
->element_number
;i
++) {
2110 if(check_fields_compatibility(event_type1
, event_type2
,
2111 field1
->child
[0], field2
->child
[0])) {
2126 /*****************************************************************************
2128 * getFieldtypeSize: get the size of the field type (primitive type)
2131 * offsetRoot : offset from the root
2132 * offsetParent : offset from the parrent
2134 * evD : event data, it may be NULL
2136 * int : size of the field
2137 ****************************************************************************/
2139 static inline gint
getFieldtypeSize(LttTracefile
*tf
,
2140 LttEventType
* evT
, gint offsetRoot
,
2141 gint offsetParent
, LttField
* fld
, void *evD
)
2143 gint size
, size1
, element_number
, i
, offset1
, offset2
;
2144 LttType
* type
= fld
->field_type
;
2146 /* This likely has been tested with gcov : half of them.. */
2147 if(unlikely(fld
->field_fixed
== 1)){
2149 if(unlikely(fld
== evT
->root_field
)) {
2150 size
= fld
->field_size
;
2151 goto end_getFieldtypeSize
;
2155 /* From gcov profiling : half string, half struct, can we gain something
2156 * from that ? (Mathieu) */
2157 switch(type
->type_class
) {
2159 element_number
= (int) type
->element_number
;
2160 if(fld
->field_fixed
== -1){
2161 size
= getFieldtypeSize(tf
, evT
, offsetRoot
,
2162 0,fld
->child
[0], NULL
);
2163 if(size
== 0){ //has string or sequence
2164 fld
->field_fixed
= 0;
2166 fld
->field_fixed
= 1;
2167 size
*= element_number
;
2169 }else if(fld
->field_fixed
== 0){// has string or sequence
2171 for(i
=0;i
<element_number
;i
++){
2172 size
+= getFieldtypeSize(tf
, evT
, offsetRoot
+size
,size
,
2173 fld
->child
[0], evD
+size
);
2175 }else size
= fld
->field_size
;
2177 fld
->fixed_root
= (offsetRoot
==-1) ? 0 : 1;
2178 fld
->fixed_parent
= (offsetParent
==-1) ? 0 : 1;
2184 size1
= (int) ltt_type_size(fac
, type
);
2185 if(fld
->field_fixed
== -1){
2186 fld
->sequ_number_size
= size1
;
2187 fld
->field_fixed
= 0;
2188 size
= getFieldtypeSize(evT
, offsetRoot
,
2189 0,fld
->child
[0], NULL
);
2190 fld
->element_size
= size
;
2192 element_number
= getIntNumber(tf
,size1
,evD
);
2193 type
->element_number
= element_number
;
2194 if(fld
->element_size
> 0){
2195 size
= element_number
* fld
->element_size
;
2196 }else{//sequence has string or sequence
2198 for(i
=0;i
<element_number
;i
++){
2199 size
+= getFieldtypeSize(tf
, evT
,
2200 offsetRoot
+size
+size1
,size
+size1
,
2201 fld
->child
[0], evD
+size
+size1
);
2207 fld
->fixed_root
= (offsetRoot
==-1) ? 0 : 1;
2208 fld
->fixed_parent
= (offsetParent
==-1) ? 0 : 1;
2215 if(fld
->field_fixed
== -1){
2216 fld
->field_fixed
= 0;
2218 /* Hope my implementation is faster than strlen (Mathieu) */
2219 char *ptr
=(char*)evD
;
2221 /* from gcov : many many strings are empty, make it the common case.*/
2222 while(unlikely(*ptr
!= '\0')) { size
++; ptr
++; }
2223 //size = ptr - (char*)evD + 1; //include end : '\0'
2225 fld
->fixed_root
= (offsetRoot
==-1) ? 0 : 1;
2226 fld
->fixed_parent
= (offsetParent
==-1) ? 0 : 1;
2231 element_number
= (int) type
->element_number
;
2233 /* tested with gcov */
2234 if(unlikely(fld
->field_fixed
== -1)){
2235 offset1
= offsetRoot
;
2237 for(i
=0;i
<element_number
;i
++){
2238 size1
=getFieldtypeSize(tf
, evT
,offset1
,offset2
,
2239 fld
->child
[i
], NULL
);
2240 if(likely(size1
> 0 && size
>= 0)){
2242 if(likely(offset1
>= 0)) offset1
+= size1
;
2250 if(unlikely(size
== -1)){
2251 fld
->field_fixed
= 0;
2253 }else fld
->field_fixed
= 1;
2254 }else if(likely(fld
->field_fixed
== 0)){
2255 offset1
= offsetRoot
;
2257 for(i
=0;unlikely(i
<element_number
);i
++){
2258 size
=getFieldtypeSize(tf
, evT
, offset1
, offset2
,
2259 fld
->child
[i
], evD
+offset2
);
2264 }else size
= fld
->field_size
;
2265 fld
->fixed_root
= (offsetRoot
==-1) ? 0 : 1;
2266 fld
->fixed_parent
= (offsetParent
==-1) ? 0 : 1;
2270 if(unlikely(fld
->field_fixed
== -1)){
2271 size
= (int) ltt_type_size(LTT_GET_BO(tf
), type
);
2272 fld
->field_fixed
= 1;
2273 }else size
= fld
->field_size
;
2275 fld
->fixed_root
= (offsetRoot
==-1) ? 0 : 1;
2276 fld
->fixed_parent
= (offsetParent
==-1) ? 0 : 1;
2281 fld
->offset_root
= offsetRoot
;
2282 fld
->offset_parent
= offsetParent
;
2283 fld
->field_size
= size
;
2285 end_getFieldtypeSize
:
2291 /*****************************************************************************
2293 * get_int : get an integer number
2295 * reverse_byte_order: must we reverse the byte order ?
2296 * size : the size of the integer
2297 * ptr : the data pointer
2299 * gint64 : a 64 bits integer
2300 ****************************************************************************/
2302 gint64
get_int(gboolean reverse_byte_order
, gint size
, void *data
)
2307 case 1: val
= *((gint8
*)data
); break;
2308 case 2: val
= ltt_get_int16(reverse_byte_order
, data
); break;
2309 case 4: val
= ltt_get_int32(reverse_byte_order
, data
); break;
2310 case 8: val
= ltt_get_int64(reverse_byte_order
, data
); break;
2311 default: val
= ltt_get_int64(reverse_byte_order
, data
);
2312 g_critical("get_int : integer size %d unknown", size
);
2319 /*****************************************************************************
2321 * get_uint : get an unsigned integer number
2323 * reverse_byte_order: must we reverse the byte order ?
2324 * size : the size of the integer
2325 * ptr : the data pointer
2327 * guint64 : a 64 bits unsigned integer
2328 ****************************************************************************/
2330 guint64
get_uint(gboolean reverse_byte_order
, gint size
, void *data
)
2335 case 1: val
= *((gint8
*)data
); break;
2336 case 2: val
= ltt_get_uint16(reverse_byte_order
, data
); break;
2337 case 4: val
= ltt_get_uint32(reverse_byte_order
, data
); break;
2338 case 8: val
= ltt_get_uint64(reverse_byte_order
, data
); break;
2339 default: val
= ltt_get_uint64(reverse_byte_order
, data
);
2340 g_critical("get_uint : unsigned integer size %d unknown",
2349 /* get the node name of the system */
2351 char * ltt_trace_system_description_node_name (LttSystemDescription
* s
)
2353 return s
->node_name
;
2357 /* get the domain name of the system */
2359 char * ltt_trace_system_description_domain_name (LttSystemDescription
* s
)
2361 return s
->domain_name
;
2365 /* get the description of the system */
2367 char * ltt_trace_system_description_description (LttSystemDescription
* s
)
2369 return s
->description
;
2373 /* get the start time of the trace */
2375 LttTime
ltt_trace_system_description_trace_start_time(LttSystemDescription
*s
)
2377 return s
->trace_start
;
2381 LttTracefile
*ltt_tracefile_new()
2383 return g_new(LttTracefile
, 1);
2386 void ltt_tracefile_destroy(LttTracefile
*tf
)
2391 void ltt_tracefile_copy(LttTracefile
*dest
, const LttTracefile
*src
)
2396 /* Before library loading... */
2398 static void __attribute__((constructor
)) init(void)
2400 LTT_FACILITY_NAME_HEARTBEAT
= g_quark_from_string("heartbeat");
2401 LTT_EVENT_NAME_HEARTBEAT
= g_quark_from_string("heartbeat");
2403 LTT_TRACEFILE_NAME_FACILITIES
= g_quark_from_string("control/facilities");