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 static inline 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 void ltt_tracefile_group_destroy(gpointer data
)
547 GArray
*group
= (GArray
*)data
;
551 for(i
=0; i
<group
->len
; i
++) {
552 tf
= &g_array_index (group
, LttTracefile
, i
);
554 ltt_tracefile_close(tf
);
556 g_array_free(group
, TRUE
);
559 gboolean
ltt_tracefile_group_has_cpu_online(gpointer data
)
561 GArray
*group
= (GArray
*)data
;
565 for(i
=0; i
<group
->len
; i
++) {
566 tf
= &g_array_index (group
, LttTracefile
, i
);
567 if(tf
->cpu_online
) return 1;
573 /* Open each tracefile under a specific directory. Put them in a
574 * GData : permits to access them using their tracefile group pathname.
575 * i.e. access control/modules tracefile group by index :
578 * A tracefile group is simply an array where all the per cpu tracefiles sits.
581 static int open_tracefiles(LttTrace
*trace
, char *root_path
, GData
*tracefiles
)
583 DIR *dir
= opendir(root_path
);
584 struct dirent
*entry
;
585 struct stat stat_buf
;
596 strncpy(path
, root_path
, PATH_MAX
-1);
597 path_len
= strlen(path
);
598 path
[path_len
] = '/';
600 path_ptr
= path
+ path_len
;
602 while((entry
= readdir(dir
)) != NULL
) {
604 if(entry
->d_name
[0] == '.') continue;
606 strncpy(path_ptr
, entry
->d_name
, PATH_MAX
- path_len
);
608 ret
= stat(path
, &stat_buf
);
614 g_debug("Tracefile file or directory : %s\n", path
);
616 if(S_ISDIR(stat_buf
.st_mode
)) {
618 g_debug("Entering subdirectory...\n");
619 ret
= open_tracefiles(trace
, path
, tracefiles
);
620 if(ret
< 0) continue;
621 } else if(S_ISREG(stat_buf
.st_mode
)) {
622 g_debug("Opening file.\n");
630 if(get_tracefile_name_number(path
, &name
, &num
))
631 continue; /* invalid name */
633 group
= g_datalist_id_get_data(&tracefiles
, name
);
635 /* Elements are automatically cleared when the array is allocated.
636 * It makes the cpu_online variable set to 0 : cpu offline, by default.
638 group
= g_array_sized_new (FALSE
, TRUE
, sizeof(LttTracefile
), 10);
639 g_datalist_id_set_data_full(&tracefiles
, name
,
640 group
, ltt_tracefile_group_destroy
);
642 /* Add the per cpu tracefile to the named group */
643 unsigned int old_len
= group
->len
;
645 group
= g_array_set_size(group
, num
+1);
646 tf
= &g_array_index (group
, LttTracefile
, num
);
648 if(ltt_tracefile_open(trace
, path
, tf
)) {
649 g_info("Error opening tracefile %s", path
);
650 g_array_set_size(group
, old_len
);
652 if(!ltt_tracefile_group_has_cpu_online(group
))
653 g_datalist_id_remove_data(&tracefiles
, name
);
655 continue; /* error opening the tracefile : bad magic number ? */
667 /* ltt_get_facility_description
669 * Opens the trace corresponding to the requested facility (identified by fac_id
672 * The name searched is : %trace root%/eventdefs/facname_checksum.xml
674 * Returns 0 on success, or 1 on failure.
677 static int ltt_get_facility_description(LttFacility
*f
,
679 LttTracefile
*fac_tf
)
681 char desc_file_name
[PATH_MAX
];
688 text
= g_quark_to_string(t
->pathname
);
689 textlen
= strlen(text
);
691 if(textlen
>= PATH_MAX
) goto name_error
;
692 strcpy(desc_file_name
, text
);
694 text
= "/eventdefs/";
695 textlen
+=strlen(text
);
696 if(textlen
>= PATH_MAX
) goto name_error
;
697 strcat(desc_file_name
, text
);
699 text
= g_quark_to_string(f
->name
);
700 textlen
+=strlen(text
);
701 if(textlen
>= PATH_MAX
) goto name_error
;
702 strcat(desc_file_name
, text
);
705 textlen
+=strlen(text
);
706 if(textlen
>= PATH_MAX
) goto name_error
;
707 strcat(desc_file_name
, text
);
709 err
= snprintf(desc_file_name
+textlen
, PATH_MAX
-textlen
-1,
711 if(err
) goto name_error
;
713 textlen
=strlen(desc_file_name
);
716 textlen
+=strlen(text
);
717 if(textlen
>= PATH_MAX
) goto name_error
;
718 strcat(desc_file_name
, text
);
720 err
= ltt_facility_open(f
, t
, desc_file_name
);
721 if(err
) goto facility_error
;
723 for(i
=0;i
<t
->facilities_by_num
->len
;i
++){
724 f
= &g_array_index(t
->facilities_by_num
, LttFacility
, i
);
726 for(j
=0; j
<f
->events
->len
; j
++){
727 et
= &g_array_index(f
->events
, LttEventType
, j
);
728 set_fields_offsets(fac_tf
, et
);
741 static void ltt_fac_ids_destroy(gpointer data
)
743 GArray
*fac_ids
= (GArray
*)data
;
747 for(i
=0; i
<fac_ids
->len
; i
++) {
748 fac
= &g_array_index (fac_ids
, LttFacility
, i
);
749 ltt_facility_close(fac
);
752 g_array_free(fac_ids
, TRUE
);
756 /* Presumes the tracefile is already seeked at the beginning. It makes sense,
757 * because it must be done just after the opening */
758 static int ltt_process_facility_tracefile(LttTracefile
*tf
)
765 err
= ltt_tracefile_read_seek(tf
);
766 if(err
== EPERM
) goto seek_error
;
767 else if(err
== ERANGE
) break; /* End of tracefile */
769 err
= ltt_tracefile_read_update_event(tf
);
770 if(err
) goto update_error
;
772 /* We are on a facility load/or facility unload/ or heartbeat event */
774 * * facility 0 is hardcoded : this is the core facility. It will be shown
775 * in the facility array though, and is shown as "loaded builtin" in the
777 * It contains event :
779 * 1 : facility unload
780 * 2 : state dump facility load
781 * Facility 1 : (heartbeat)
784 if(tf
->event
.facility_id
> 1) { /* Should only contain core and heartbeat
786 g_warning("Error in processing facility file %s, "
787 "should not contain facility id %u.", g_quark_to_string(tf
->name
),
788 tf
->event
.facility_id
);
791 } else if(tf
->event
.facility_id
== LTT_FACILITY_CORE
) {
793 struct LttFacilityLoad
*fac_load_data
;
797 switch((enum ltt_core_events
)tf
->event
.event_id
) {
798 case LTT_EVENT_FACILITY_LOAD
:
800 (struct LttFacilityLoad
*)tf
->event
.data
;
802 (char*)(tf
->event
.data
+ sizeof(struct LttFacilityLoad
));
803 fac
= &g_array_index (tf
->trace
->facilities_by_num
, LttFacility
,
804 ltt_get_uint32(LTT_GET_BO(tf
), &fac_load_data
->id
));
805 g_assert(fac
->exists
== 0);
806 fac
->name
= g_quark_from_string(fac_name
);
807 fac
->checksum
= ltt_get_uint32(LTT_GET_BO(tf
),
808 &fac_load_data
->checksum
);
809 fac
->id
= ltt_get_uint32(LTT_GET_BO(tf
), &fac_load_data
->id
);
810 fac
->pointer_size
= ltt_get_uint32(LTT_GET_BO(tf
),
811 &fac_load_data
->pointer_size
);
812 fac
->size_t_size
= ltt_get_uin32(LTT_GET_BO(tf
),
813 &fac_load_data
->size_t_size
);
814 fac
->alignment
= ltt_get_uint32(LTT_GET_BO(tf
),
815 &fac_load_data
->alignment
);
817 if(ltt_get_facility_description(fac
, tf
->trace
, tf
))
820 fac
->trace
= tf
->trace
;
823 fac_ids
= g_datalist_id_get_data(&tf
->trace
->facilities_by_name
, fac
->name
);
824 if(fac_ids
== NULL
) {
825 fac_ids
= g_array_sized_new (FALSE
, TRUE
, sizeof(guint
), 1);
826 g_datalist_id_set_data_full(&tf
->trace
->facilities_by_name
,
828 fac_ids
, ltt_fac_ids_destroy
);
830 g_array_append_val(fac_ids
, fac
->id
);
833 case LTT_EVENT_FACILITY_UNLOAD
:
834 /* We don't care about unload : facilities ID are valid for the whole
835 * trace. They simply won't be used after the unload. */
837 case LTT_EVENT_STATE_DUMP_FACILITY_LOAD
:
839 (struct LttFacilityLoad
*)tf
->event
.data
;
841 (char*)(tf
->event
.data
+ sizeof(struct LttFacilityLoad
));
842 fac
= &g_array_index (tf
->trace
->facilities_by_num
, LttFacility
,
843 ltt_get_uint32(LTT_GET_BO(tf
), &fac_load_data
->id
));
844 g_assert(fac
->exists
== 0);
845 fac
->name
= g_quark_from_string(fac_name
);
846 fac
->checksum
= ltt_get_uint32(LTT_GET_BO(tf
),
847 &fac_load_data
->checksum
);
848 fac
->id
= ltt_get_uint8(LTT_GET_BO(tf
), &fac_load_data
->id
);
849 fac
->pointer_size
= ltt_get_uint32(LTT_GET_BO(tf
),
850 &fac_load_data
->pointer_size
);
851 fac
->size_t_size
= ltt_get_uin32(LTT_GET_BO(tf
),
852 &fac_load_data
->size_t_size
);
853 fac
->alignment
= ltt_get_uint32(LTT_GET_BO(tf
),
854 &fac_load_data
->alignment
);
855 if(ltt_get_facility_description(fac
, tf
->trace
, tf
))
858 fac
->trace
= tf
->trace
;
862 fac_ids
= g_datalist_id_get_data(&tf
->trace
->facilities_by_name
,
864 if(fac_ids
== NULL
) {
865 fac_ids
= g_array_sized_new (FALSE
, TRUE
, sizeof(guint
), 1);
866 g_datalist_id_set_data_full(&tf
->trace
->facilities_by_name
,
868 fac_ids
, ltt_fac_ids_destroy
);
870 g_array_append_val(fac_ids
, fac
->id
);
873 case LTT_EVENT_HEARTBEAT
:
876 g_warning("Error in processing facility file %s, "
877 "unknown event id %hhu in core facility.",
878 g_quark_to_string(tf
->name
),
897 LttTrace
*ltt_trace_open(const gchar
*pathname
)
899 gchar abs_path
[PATH_MAX
];
905 t
= g_new(LttTrace
, 1);
906 if(!t
) goto alloc_error
;
908 get_absolute_pathname(pathname
, abs_path
);
909 t
->pathname
= g_quark_from_string(abs_path
);
911 /* Open all the tracefiles */
912 g_datalist_init(&t
->tracefiles
);
913 if(open_tracefiles(t
, abs_path
, t
->tracefiles
))
916 /* Prepare the facilities containers : array and mapping */
917 /* Array is zeroed : the "exists" field is set to false by default */
918 t
->facilities_by_num
= g_array_sized_new (FALSE
,
919 TRUE
, sizeof(LttFacility
),
921 t
->facilities_by_num
= g_array_set_size(t
->facilities_by_num
, NUM_FACILITIES
);
923 g_datalist_init(&t
->facilities_by_name
);
925 /* Parse each trace control/facilitiesN files : get runtime fac. info */
926 group
= g_datalist_id_get_data(&t
->tracefiles
, LTT_TRACEFILE_NAME_FACILITIES
);
928 g_error("Trace %s has no facility tracefile", abs_path
);
929 goto facilities_error
;
932 for(i
=0; i
<group
->len
; i
++) {
933 tf
= &g_array_index (group
, LttTracefile
, i
);
934 if(ltt_process_facility_tracefile(tf
))
935 goto facilities_error
;
944 g_datalist_clear(&t
->facilities_by_name
);
945 g_array_free(t
->facilities_by_num
, TRUE
);
947 g_datalist_clear(&t
->tracefiles
);
954 GQuark
ltt_trace_name(LttTrace
*t
)
960 /******************************************************************************
961 * When we copy a trace, we want all the opening actions to happen again :
962 * the trace will be reopened and totally independant from the original.
963 * That's why we call ltt_trace_open.
964 *****************************************************************************/
965 LttTrace
*ltt_trace_copy(LttTrace
*self
)
967 return ltt_trace_open(g_quark_to_string(self
->pathname
));
970 void ltt_trace_close(LttTrace
*t
)
972 g_datalist_clear(&t
->facilities_by_name
);
973 g_array_free(t
->facilities_by_num
, TRUE
);
974 g_datalist_clear(&t
->tracefiles
);
979 /*****************************************************************************
980 *Get the system description of the trace
981 ****************************************************************************/
983 LttFacility
*ltt_trace_facility_by_id(LttTrace
*t
, guint8 id
)
985 g_assert(id
< t
->facilities_by_num
->len
);
986 return &g_array_index(t
->facilities_by_num
, LttFacility
, id
);
989 /* ltt_trace_facility_get_by_name
991 * Returns the GArray of facility indexes. All the fac_ids that matches the
992 * requested facility name.
994 * If name is not found, returns NULL.
996 GArray
*ltt_trace_facility_get_by_name(LttTrace
*t
, GQuark name
)
998 return g_datalist_id_get_data(&t
->facilities_by_name
, name
);
1001 /*****************************************************************************
1002 * Functions to discover all the event types in the trace
1003 ****************************************************************************/
1006 unsigned ltt_trace_eventtype_number(LttTrace
*t
)
1010 unsigned int num
= t
->facility_number
;
1014 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, i
);
1015 count
+= f
->event_number
;
1022 //use an iteration on all the trace facilities, and inside iteration on all the
1023 //event types in each facilities instead.
1024 LttEventType
*ltt_trace_eventtype_get(LttTrace
*t
, unsigned evId
)
1026 LttEventType
*event_type
;
1029 f
= ltt_trace_facility_by_id(t
,evId
);
1031 if(unlikely(!f
)) event_type
= NULL
;
1032 else event_type
= f
->events
[evId
- f
->base_id
];
1039 /*****************************************************************************
1040 * ltt_trace_find_tracefile
1042 * Find a tracefile by name and index in the group.
1044 * Returns a pointer to the tracefiles, else NULL.
1045 ****************************************************************************/
1047 LttTracefile
*ltt_trace_find_tracefile(LttTrace
*t
, const gchar
*name
)
1052 /*****************************************************************************
1053 * Get the start time and end time of the trace
1054 ****************************************************************************/
1056 static void ltt_tracefile_time_span_get(LttTracefile
*tf
,
1057 LttTime
*start
, LttTime
*end
)
1059 struct ltt_block_start_header
* header
;
1062 err
= map_block(tf
, 0);
1064 g_error("Can not map block");
1065 *start
= ltt_time_infinite
;
1067 *start
= tf
->buffer
.begin
.timestamp
;
1069 err
= map_block(tf
, tf
->num_blocks
- 1); /* Last block */
1071 g_error("Can not map block");
1072 *end
= ltt_time_zero
;
1074 *end
= tf
->buffer
.end
.timestamp
;
1077 struct tracefile_time_span_get_args
{
1083 static void group_time_span_get(GQuark name
, gpointer data
, gpointer user_data
)
1085 struct tracefile_time_span_get_args
*args
=
1086 (struct tracefile_time_span_get_args
*)user_data
;
1088 GArray
*group
= (GArray
*)data
;
1094 for(i
=0; i
<group
->len
; i
++) {
1095 tf
= &g_array_index (group
, LttTracefile
, i
);
1096 if(tf
->cpu_online
) {
1097 ltt_tracefile_time_span_get(tf
, &tmp_start
, &tmp_end
);
1098 if(ltt_time_compare(*args
->start
, tmp_start
)>0) *args
->start
= tmp_start
;
1099 if(ltt_time_compare(*args
->end
, tmp_end
)<0) *args
->end
= tmp_end
;
1104 void ltt_trace_time_span_get(LttTrace
*t
, LttTime
*start
, LttTime
*end
)
1106 LttTime min_start
= ltt_time_infinite
;
1107 LttTime max_end
= ltt_time_zero
;
1108 struct tracefile_time_span_get_args args
= { t
, &min_start
, &max_end
};
1110 g_datalist_foreach(&t
->tracefiles
, &group_time_span_get
, &args
);
1112 if(start
!= NULL
) *start
= min_start
;
1113 if(end
!= NULL
) *end
= max_end
;
1118 /*****************************************************************************
1119 *Get the name of a tracefile
1120 ****************************************************************************/
1122 GQuark
ltt_tracefile_name(LttTracefile
*tf
)
1127 /*****************************************************************************
1128 * Get the number of blocks in the tracefile
1129 ****************************************************************************/
1131 guint
ltt_tracefile_block_number(LttTracefile
*tf
)
1133 return tf
->num_blocks
;
1137 /* Seek to the first event in a tracefile that has a time equal or greater than
1138 * the time passed in parameter.
1140 * If the time parameter is outside the tracefile time span, seek to the first
1141 * or the last event of the tracefile.
1143 * If the time parameter is before the first event, we have to seek specially to
1146 * If the time is after the end of the trace, get the last event.
1148 * Do a binary search to find the right block, then a sequential search in the
1149 * block to find the event.
1151 * In the special case where the time requested fits inside a block that has no
1152 * event corresponding to the requested time, the first event of the next block
1155 * IMPORTANT NOTE : // FIXME everywhere...
1157 * You MUST NOT do a ltt_tracefile_read right after a ltt_tracefile_seek_time :
1158 * you will jump over an event if you do.
1160 * Return value : 0 : no error, the tf->event can be used
1161 * otherwise : this is an error.
1165 int ltt_tracefile_seek_time(LttTracefile
*tf
, LttTime time
)
1169 unsigned int block_num
, high
, low
;
1171 /* seek at the beginning of trace */
1172 err
= map_block(tf
, 0); /* First block */
1174 g_error("Can not map block");
1178 /* If the time is lower or equal the beginning of the trace,
1179 * go to the first event. */
1180 if(ltt_time_compare(time
, tf
->buffer
.begin
.timestamp
) <= 0) {
1181 ret
= ltt_tracefile_read(tf
);
1182 goto found
; /* There is either no event in the trace or the event points
1183 to the first event in the trace */
1186 err
= map_block(tf
, tf
->num_blocks
- 1); /* Last block */
1188 g_error("Can not map block");
1192 /* If the time is after the end of the trace, get the last event. */
1193 if(ltt_time_compare(time
, tf
->buffer
.end
.timestamp
) >= 0) {
1194 /* While the ltt_tracefile_read doesn't return ERANGE or EPERM,
1198 ret
= ltt_tracefile_read(tf
);
1199 if(ret
== ERANGE
) goto found
; /* ERANGE or EPERM */
1200 else if(ret
) goto fail
;
1204 /* Binary search the block */
1205 high
= tf
->num_blocks
- 1;
1209 block_num
= ((high
-low
) / 2) + low
;
1211 err
= map_block(tf
, block_num
);
1213 g_error("Can not map block");
1217 /* We cannot divide anymore : this is what would happen if the time
1218 * requested was exactly between two consecutive buffers'end and start
1219 * timestamps. This is also what would happend if we didn't deal with out
1220 * of span cases prior in this function. */
1221 /* The event is right in the buffer!
1222 * (or in the next buffer first event) */
1224 ret
= ltt_tracefile_read(tf
);
1225 if(ret
== ERANGE
) goto found
; /* ERANGE or EPERM */
1226 else if(ret
) goto fail
;
1228 if(ltt_time_compare(time
, tf
->event
.event_time
) >= 0)
1232 } if(ltt_time_compare(time
, tf
->buffer
.begin
.timestamp
) < 0) {
1233 /* go to lower part */
1235 } else if(ltt_time_compare(time
, tf
->buffer
.end
.timestamp
) > 0) {
1236 /* go to higher part */
1238 } else {/* The event is right in the buffer!
1239 (or in the next buffer first event) */
1241 ltt_tracefile_read(tf
);
1242 if(ret
== ERANGE
) goto found
; /* ERANGE or EPERM */
1243 else if(ret
) goto fail
;
1245 if(ltt_time_compare(time
, tf
->event
.event_time
) >= 0)
1255 /* Error handling */
1257 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1258 g_quark_to_string(tf
->name
));
1263 int ltt_tracefile_seek_position(LttTracefile
*tf
, const LttEventPosition
*ep
) {
1267 if(ep
->tracefile
!= tf
) {
1271 err
= map_block(tf
, ep
->block
);
1273 g_error("Can not map block");
1277 tf
->event
.offset
= ep
->offset
;
1279 err
= ltt_tracefile_read_update_event(tf
);
1281 err
= ltt_tracefile_read_op(tf
);
1287 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1288 g_quark_to_string(tf
->name
));
1291 /* Calculate the real event time based on the buffer boundaries */
1292 LttTime
ltt_interpolate_time(LttTracefile
*tf
, LttEvent
*event
)
1296 g_assert(tf
->trace
->has_tsc
);
1298 time
= ltt_time_from_uint64(
1299 (guint64
)tf
->buffer
.tsc
*tf
->buffer
.nsecs_per_cycle
);
1300 time
= ltt_time_add(tf
->buffer
.begin
.timestamp
, time
);
1305 /*****************************************************************************
1307 * ltt_tracefile_read : Read the next event in the tracefile
1312 * Returns 0 if an event can be used in tf->event.
1313 * Returns ERANGE on end of trace. The event in tf->event still can be used.
1314 * Returns EPERM on error.
1316 * This function does make the tracefile event structure point to the event
1317 * currently pointed to by the tf->event.
1319 * Note : you must call a ltt_tracefile_seek to the beginning of the trace to
1320 * reinitialize it after an error if you want results to be coherent.
1321 * It would be the case if a end of trace last buffer has no event : the end
1322 * of trace wouldn't be returned, but an error.
1323 * We make the assumption there is at least one event per buffer.
1324 ****************************************************************************/
1326 int ltt_tracefile_read(LttTracefile
*tf
)
1330 err
= ltt_tracefile_read_seek(tf
);
1332 err
= ltt_tracefile_read_update_event(tf
);
1334 err
= ltt_tracefile_read_op(tf
);
1340 int ltt_tracefile_read_seek(LttTracefile
*tf
)
1344 /* Get next buffer until we finally have an event, or end of trace */
1346 err
= ltt_seek_next_event(tf
);
1347 if(unlikely(err
== ENOPROTOOPT
)) {
1351 /* Are we at the end of the buffer ? */
1353 if(unlikely(tf
->buffer
.index
== tf
->num_blocks
-1)){ /* end of trace ? */
1356 /* get next block */
1357 err
= map_block(tf
, tf
->buffer
.index
+ 1);
1359 g_error("Can not map block");
1363 } else break; /* We found an event ! */
1370 /* do specific operation on events */
1371 int ltt_tracefile_read_op(LttTracefile
*tf
)
1380 /* do event specific operation */
1382 /* do something if its an heartbeat event : increment the heartbeat count */
1383 //if(event->facility_id == LTT_FACILITY_CORE)
1384 // if(event->event_id == LTT_EVENT_HEARTBEAT)
1385 // tf->cur_heart_beat_number++;
1391 /* same as ltt_tracefile_read, but does not seek to the next event nor call
1392 * event specific operation. */
1393 int ltt_tracefile_read_update_event(LttTracefile
*tf
)
1401 pos
= event
->offset
;
1403 /* Read event header */
1407 if(tf
->trace
->has_tsc
) {
1408 event
->time
.timestamp
= ltt_get_uint32(LTT_GET_BO(tf
),
1410 /* 32 bits -> 64 bits tsc */
1411 /* note : still works for seek and non seek cases. */
1412 if(event
->time
.timestamp
< (0xFFFFFFFFULL
&tf
->buffer
.tsc
)) {
1413 tf
->buffer
.tsc
= ((tf
->buffer
.tsc
&0xFFFFFFFF00000000ULL
)
1415 | (guint64
)event
->time
.timestamp
;
1416 event
->tsc
= tf
->buffer
.tsc
;
1419 tf
->buffer
.tsc
= (tf
->buffer
.tsc
&0xFFFFFFFF00000000ULL
)
1420 | (guint64
)event
->time
.timestamp
;
1421 event
->tsc
= tf
->buffer
.tsc
;
1424 event
->event_time
= ltt_interpolate_time(tf
, event
);
1426 pos
+= sizeof(guint32
);
1428 event
->time
.delta
.tv_sec
= 0;
1429 event
->time
.delta
.tv_nsec
= ltt_get_uint32(LTT_GET_BO(tf
),
1430 pos
) * NSEC_PER_USEC
;
1432 event
->tsc
= tf
->buffer
.tsc
;
1434 event
->event_time
= ltt_time_add(tf
->buffer
.begin
.timestamp
,
1436 pos
+= sizeof(guint32
);
1439 event
->facility_id
= ltt_get_uint8(LTT_GET_BO(tf
),
1441 pos
+= sizeof(guint8
);
1443 event
->event_id
= ltt_get_uint8(LTT_GET_BO(tf
),
1445 pos
+= sizeof(guint8
);
1453 /****************************************************************************
1455 * map_block : map a block from the file
1457 * lttdes : ltt trace file
1458 * whichBlock : the block which will be read
1461 * EINVAL : lseek fail
1462 * EIO : can not read from the file
1463 ****************************************************************************/
1465 static gint
map_block(LttTracefile
* tf
, guint block_num
)
1467 struct ltt_block_start_header
*header
;
1469 g_assert(block_num
< tf
->num_blocks
);
1471 if(tf
->buffer
.head
!= NULL
)
1472 munmap(tf
->buffer
.head
, tf
->buf_size
);
1474 /* Multiple of pages aligned head */
1475 tf
->buffer
.head
= mmap(0, tf
->block_size
, PROT_READ
, tf
->fd
, MAP_PRIVATE
,
1476 (off_t
)tf
->block_size
* (off_t
)block_num
);
1478 if(tf
->buffer
.head
== NULL
) {
1479 perror("Error in allocating memory for buffer of tracefile");
1482 g_assert(((guint
)tf
->buffer
.head
) & (8-1) == 0); // make sure it's aligned.
1485 tf
->buffer
.index
= block_num
;
1487 header
= (struct ltt_block_start_header
*)tf
->buffer
.head
;
1489 tf
->buffer
.begin
.timestamp
= ltt_get_time(LTT_GET_BO(tf
),
1490 &header
->begin
.timestamp
);
1491 tf
->buffer
.begin
.timestamp
.tv_nsec
*= NSEC_PER_USEC
;
1492 tf
->buffer
.begin
.cycle_count
= ltt_get_uint64(LTT_GET_BO(tf
),
1493 &header
->begin
.cycle_count
);
1494 tf
->buffer
.end
.timestamp
= ltt_get_time(LTT_GET_BO(tf
),
1495 &header
->end
.timestamp
);
1496 tf
->buffer
.end
.timestamp
.tv_nsec
*= NSEC_PER_USEC
;
1497 tf
->buffer
.end
.cycle_count
= ltt_get_uint64(LTT_GET_BO(tf
),
1498 &header
->end
.cycle_count
);
1499 tf
->buffer
.lost_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1500 &header
->lost_size
);
1502 tf
->buffer
.tsc
= tf
->buffer
.begin
.cycle_count
;
1503 tf
->event
.tsc
= tf
->buffer
.tsc
;
1506 * eventually support variable buffer size : will need a partial pre-read of
1507 * the headers to create an index when we open the trace... eventually. */
1508 g_assert(tf
->block_size
== ltt_get_uint32(LTT_GET_BO(tf
),
1509 &header
->buf_size
));
1511 /* Now that the buffer is mapped, calculate the time interpolation for the
1514 tf
->buffer
.nsecs_per_cycle
= calc_nsecs_per_cycle(tf
);
1516 /* Make the current event point to the beginning of the buffer :
1517 * it means that the event read must get the first event. */
1518 tf
->event
.tracefile
= tf
;
1519 tf
->event
.block
= block_num
;
1520 tf
->event
.offset
= tf
->buffer
.head
;
1529 ssize_t
ltt_get_event_size(LttTracefile
*tf
)
1533 /* Specific handling of core events : necessary to read the facility control
1535 if(unlikely(tf
->event
.facility_id
== LTT_FACILITY_CORE
)) {
1536 switch((enum ltt_core_events
)tf
->event
.event_id
) {
1537 case LTT_EVENT_FACILITY_LOAD
:
1538 size
= sizeof(struct LttFacilityLoad
);
1540 case LTT_EVENT_FACILITY_UNLOAD
:
1541 size
= sizeof(struct LttFacilityUnload
);
1543 case LTT_EVENT_STATE_DUMP_FACILITY_LOAD
:
1544 size
= sizeof(struct LttStateDumpFacilityLoad
);
1546 case LTT_EVENT_HEARTBEAT
:
1547 size
= sizeof(TimeHeartbeat
);
1550 g_warning("Error in getting event size : tracefile %s, "
1551 "unknown event id %hhu in core facility.",
1552 g_quark_to_string(tf
->name
),
1553 tf
->event
.event_id
);
1554 goto event_id_error
;
1559 LttFacility
*f
= ltt_trace_get_facility_by_num(tf
->trace
,
1560 tf
->event
.facility_id
);
1561 LttEventType
*event_type
=
1562 ltt_facility_eventtype_get(f
, tf
->event
.event_id
);
1563 size
= get_fields_offsets(tf
, event_type
, tf
->event
.data
);
1573 /* Take the tf current event offset and use the event facility id and event id
1574 * to figure out where is the next event offset.
1576 * This is an internal function not aiming at being used elsewhere : it will
1577 * not jump over the current block limits. Please consider using
1578 * ltt_tracefile_read to do this.
1580 * Returns 0 on success
1581 * ERANGE if we are at the end of the buffer.
1582 * ENOPROTOOPT if an error occured when getting the current event size.
1584 static int ltt_seek_next_event(LttTracefile
*tf
)
1590 /* seek over the buffer header if we are at the buffer start */
1591 if(tf
->event
.offset
== tf
->buffer
.head
) {
1592 tf
->event
.offset
+= sizeof(struct ltt_block_start_header
);
1597 if(tf
->event
.offset
== tf
->buffer
.head
+ tf
->buffer
.lost_size
) {
1602 pos
= tf
->event
.data
;
1604 event_size
= ltt_get_event_size(tf
);
1605 if(event_size
< 0) goto error
;
1607 pos
+= (size_t)event_size
;
1609 tf
->event
.offset
= pos
;
1615 g_error("Error in ltt_seek_next_event for tracefile %s",
1616 g_quark_to_string(tf
->name
));
1621 /*****************************************************************************
1623 * calc_nsecs_per_cycle : calculate nsecs per cycle for current block
1626 ****************************************************************************/
1628 static double calc_nsecs_per_cycle(LttTracefile
* tf
)
1630 LttTime lBufTotalTime
; /* Total time for this buffer */
1631 double lBufTotalNSec
; /* Total time for this buffer in nsecs */
1632 LttCycleCount lBufTotalCycle
;/* Total cycles for this buffer */
1634 /* Calculate the total time for this buffer */
1635 lBufTotalTime
= ltt_time_sub(
1636 ltt_get_time(LTT_GET_BO(tf
), &tf
->buffer
.end
.timestamp
),
1637 ltt_get_time(LTT_GET_BO(tf
), &tf
->buffer
.begin
.timestamp
));
1639 /* Calculate the total cycles for this bufffer */
1640 lBufTotalCycle
= ltt_get_uint64(LTT_GET_BO(tf
), &tf
->buffer
.end
.cycle_count
);
1641 lBufTotalCycle
-= ltt_get_uint64(LTT_GET_BO(tf
),
1642 &tf
->buffer
.begin
.cycle_count
);
1644 /* Convert the total time to double */
1645 lBufTotalNSec
= ltt_time_to_double(lBufTotalTime
);
1647 return lBufTotalNSec
/ (double)lBufTotalCycle
;
1651 void setFieldsOffset(LttTracefile
*tf
, LttEventType
*evT
,void *evD
)
1653 LttField
* rootFld
= evT
->root_field
;
1654 // rootFld->base_address = evD;
1657 rootFld
->field_size
= getFieldtypeSize(tf
, evT
->facility
,
1658 evT
, 0,0,rootFld
, evD
);
1662 /*****************************************************************************
1664 * set_fields_offsets : set the precomputable offset of the fields
1666 * tracefile : opened trace file
1667 * event_type : the event type
1668 ****************************************************************************/
1670 void set_fields_offsets(LttTracefile
*tf
, LttEventType
*event_type
)
1672 LttField
*field
= event_type
->root_field
;
1673 enum field_status fixed_root
= FIELD_FIXED
, fixed_parent
= FIELD_FIXED
;
1676 preset_field_type_size(tf
, event_type
, 0, 0,
1677 &fixed_root
, &fixed_parent
,
1683 /*****************************************************************************
1685 * preset_field_type_size : set the fixed sizes of the field type
1688 * event_type : event type
1689 * offset_root : offset from the root
1690 * offset_parent : offset from the parent
1691 * fixed_root : Do we know a fixed offset to the root ?
1692 * fixed_parent : Do we know a fixed offset to the parent ?
1694 ****************************************************************************/
1695 void preset_field_type_size(LttTracefile
*tf
, LttEventType
*event_type
,
1696 off_t offset_root
, off_t offset_parent
,
1697 enum field_status
*fixed_root
, enum field_status
*fixed_parent
,
1700 enum field_status local_fixed_root
, local_fixed_parent
;
1704 g_assert(field
->fixed_root
== FIELD_UNKNOWN
);
1705 g_assert(field
->fixed_parent
== FIELD_UNKNOWN
);
1706 g_assert(field
->fixed_size
== FIELD_UNKNOWN
);
1708 type
= field
->field_type
;
1710 field
->fixed_root
= *fixed_root
;
1711 if(field
->fixed_root
== FIELD_FIXED
)
1712 field
->offset_root
= offset_root
;
1714 field
->offset_root
= 0;
1716 field
->fixed_parent
= *fixed_parent
;
1717 if(field
->fixed_parent
== FIELD_FIXED
)
1718 field
->offset_parent
= offset_parent
;
1720 field
->offset_parent
= 0;
1722 size_t current_root_offset
;
1723 size_t current_offset
;
1724 enum field_status current_child_status
, final_child_status
;
1727 switch(type
->type_class
) {
1732 field
->field_size
= ltt_type_size(tf
->trace
, type
);
1733 field
->fixed_size
= FIELD_FIXED
;
1736 field
->field_size
= (off_t
)event_type
->facility
->pointer_size
;
1737 field
->fixed_size
= FIELD_FIXED
;
1741 field
->field_size
= (off_t
)event_type
->facility
->pointer_size
;
1742 field
->fixed_size
= FIELD_FIXED
;
1747 field
->field_size
= (off_t
)event_type
->facility
->size_t_size
;
1748 field
->fixed_size
= FIELD_FIXED
;
1751 local_fixed_root
= FIELD_VARIABLE
;
1752 local_fixed_parent
= FIELD_VARIABLE
;
1753 preset_field_type_size(tf
, event_type
,
1755 &local_fixed_root
, &local_fixed_parent
,
1757 field
->fixed_size
= FIELD_VARIABLE
;
1758 field
->field_size
= 0;
1761 field
->fixed_size
= FIELD_VARIABLE
;
1762 field
->field_size
= 0;
1765 local_fixed_root
= FIELD_VARIABLE
;
1766 local_fixed_parent
= FIELD_VARIABLE
;
1767 preset_field_type_size(tf
, event_type
,
1769 &local_fixed_root
, &local_fixed_parent
,
1771 field
->fixed_size
= field
->child
[0]->fixed_size
;
1772 if(field
->fixed_size
== FIELD_FIXED
)
1773 field
->field_size
= type
->element_number
* field
->child
[0]->field_size
;
1775 field
->field_size
= 0;
1778 current_root_offset
= field
->offset_root
;
1780 current_child_status
= FIELD_FIXED
;
1781 for(i
=0;i
<type
->element_number
;i
++) {
1782 preset_field_type_size(tf
, event_type
,
1783 current_root_offset
, current_offset
,
1784 fixed_root
, ¤t_child_status
,
1786 if(current_child_status
== FIELD_FIXED
) {
1787 current_root_offset
+= field
->child
[i
]->field_size
;
1788 current_offset
+= field
->child
[i
]->field_size
;
1790 current_root_offset
= 0;
1794 if(current_child_status
!= FIELD_FIXED
) {
1795 *fixed_parent
= current_child_status
;
1796 field
->field_size
= 0;
1797 field
->fixed_size
= current_child_status
;
1799 field
->field_size
= current_offset
;
1800 field
->fixed_size
= FIELD_FIXED
;
1804 current_root_offset
= field
->offset_root
;
1807 final_child_status
= FIELD_FIXED
;
1808 for(i
=0;i
<type
->element_number
;i
++) {
1809 enum field_status current_root_child_status
= FIELD_FIXED
;
1810 enum field_status current_child_status
= FIELD_FIXED
;
1811 preset_field_type_size(tf
, event_type
,
1812 current_root_offset
, current_offset
,
1813 ¤t_root_child_status
, ¤t_child_status
,
1815 if(current_child_status
!= FIELD_FIXED
)
1816 final_child_status
= current_child_status
;
1818 max_size
= max(max_size
, field
->child
[i
]->field_size
);
1820 if(final_child_status
!= FIELD_FIXED
) {
1821 *fixed_root
= final_child_status
;
1822 *fixed_parent
= final_child_status
;
1823 field
->field_size
= 0;
1824 field
->fixed_size
= current_child_status
;
1826 field
->field_size
= max_size
;
1827 field
->fixed_size
= FIELD_FIXED
;
1835 /*****************************************************************************
1837 * get_field_type_size : set the fixed and dynamic sizes of the field type
1838 * from the data read.
1841 * event_type : event type
1842 * offset_root : offset from the root
1843 * offset_parent : offset from the parent
1845 * data : a pointer to the event data.
1846 *Returns the field type size.
1847 ****************************************************************************/
1848 size_t get_field_type_size(LttTracefile
*tf
, LttEventType
*event_type
,
1849 off_t offset_root
, off_t offset_parent
,
1850 LttField
*field
, void *data
)
1856 g_assert(field
->fixed_root
!= FIELD_UNKNOWN
);
1857 g_assert(field
->fixed_parent
!= FIELD_UNKNOWN
);
1858 g_assert(field
->fixed_size
!= FIELD_UNKNOWN
);
1860 field
->offset_root
= offset_root
;
1861 field
->offset_parent
= offset_parent
;
1863 type
= field
->field_type
;
1865 switch(type
->type_class
) {
1876 g_assert(field
->fixed_size
== FIELD_FIXED
);
1877 size
= field
->field_size
;
1881 gint seqnum
= ltt_get_uint(LTT_GET_BO(tf
),
1882 field
->sequ_number_size
,
1883 data
+ offset_root
);
1885 if(field
->child
[0]->fixed_size
== FIELD_FIXED
) {
1886 size
= field
->sequ_number_size
+
1887 (seqnum
* get_field_type_size(tf
, event_type
,
1888 offset_root
, offset_parent
,
1889 field
->child
[0], data
));
1891 size
+= field
->sequ_number_size
;
1892 for(i
=0;i
<seqnum
;i
++) {
1894 child_size
= get_field_type_size(tf
, event_type
,
1895 offset_root
, offset_parent
,
1896 field
->child
[0], data
);
1897 offset_root
+= child_size
;
1898 offset_parent
+= child_size
;
1902 field
->field_size
= size
;
1906 size
= strlen((char*)(data
+offset_root
)) + 1;// length + \0
1907 field
->field_size
= size
;
1910 if(field
->fixed_size
== FIELD_FIXED
)
1911 size
= field
->field_size
;
1913 for(i
=0;i
<field
->field_type
->element_number
;i
++) {
1915 child_size
= get_field_type_size(tf
, event_type
,
1916 offset_root
, offset_parent
,
1917 field
->child
[0], data
);
1918 offset_root
+= child_size
;
1919 offset_parent
+= child_size
;
1922 field
->field_size
= size
;
1926 if(field
->fixed_size
== FIELD_FIXED
)
1927 size
= field
->field_size
;
1929 size_t current_root_offset
= offset_root
;
1930 size_t current_offset
= 0;
1931 size_t child_size
= 0;
1932 for(i
=0;i
<type
->element_number
;i
++) {
1933 child_size
= get_field_type_size(tf
,
1934 event_type
, current_root_offset
, current_offset
,
1935 field
->child
[i
], data
);
1936 current_offset
+= child_size
;
1937 current_root_offset
+= child_size
;
1940 size
= current_offset
;
1941 field
->field_size
= size
;
1945 if(field
->fixed_size
== FIELD_FIXED
)
1946 size
= field
->field_size
;
1948 size_t current_root_offset
= field
->offset_root
;
1949 size_t current_offset
= 0;
1950 for(i
=0;i
<type
->element_number
;i
++) {
1951 size
= get_field_type_size(tf
, event_type
,
1952 current_root_offset
, current_offset
,
1953 field
->child
[i
], data
);
1954 size
= max(size
, field
->child
[i
]->field_size
);
1956 field
->field_size
= size
;
1967 /*****************************************************************************
1969 * getFieldtypeSize: get the size of the field type (primitive type)
1972 * offsetRoot : offset from the root
1973 * offsetParent : offset from the parrent
1975 * evD : event data, it may be NULL
1977 * int : size of the field
1978 ****************************************************************************/
1980 static inline gint
getFieldtypeSize(LttTracefile
*tf
,
1981 LttEventType
* evT
, gint offsetRoot
,
1982 gint offsetParent
, LttField
* fld
, void *evD
)
1984 gint size
, size1
, element_number
, i
, offset1
, offset2
;
1985 LttType
* type
= fld
->field_type
;
1987 /* This likely has been tested with gcov : half of them.. */
1988 if(unlikely(fld
->field_fixed
== 1)){
1990 if(unlikely(fld
== evT
->root_field
)) {
1991 size
= fld
->field_size
;
1992 goto end_getFieldtypeSize
;
1996 /* From gcov profiling : half string, half struct, can we gain something
1997 * from that ? (Mathieu) */
1998 switch(type
->type_class
) {
2000 element_number
= (int) type
->element_number
;
2001 if(fld
->field_fixed
== -1){
2002 size
= getFieldtypeSize(tf
, evT
, offsetRoot
,
2003 0,fld
->child
[0], NULL
);
2004 if(size
== 0){ //has string or sequence
2005 fld
->field_fixed
= 0;
2007 fld
->field_fixed
= 1;
2008 size
*= element_number
;
2010 }else if(fld
->field_fixed
== 0){// has string or sequence
2012 for(i
=0;i
<element_number
;i
++){
2013 size
+= getFieldtypeSize(tf
, evT
, offsetRoot
+size
,size
,
2014 fld
->child
[0], evD
+size
);
2016 }else size
= fld
->field_size
;
2018 fld
->fixed_root
= (offsetRoot
==-1) ? 0 : 1;
2019 fld
->fixed_parent
= (offsetParent
==-1) ? 0 : 1;
2025 size1
= (int) ltt_type_size(fac
, type
);
2026 if(fld
->field_fixed
== -1){
2027 fld
->sequ_number_size
= size1
;
2028 fld
->field_fixed
= 0;
2029 size
= getFieldtypeSize(evT
, offsetRoot
,
2030 0,fld
->child
[0], NULL
);
2031 fld
->element_size
= size
;
2033 element_number
= getIntNumber(tf
,size1
,evD
);
2034 type
->element_number
= element_number
;
2035 if(fld
->element_size
> 0){
2036 size
= element_number
* fld
->element_size
;
2037 }else{//sequence has string or sequence
2039 for(i
=0;i
<element_number
;i
++){
2040 size
+= getFieldtypeSize(tf
, evT
,
2041 offsetRoot
+size
+size1
,size
+size1
,
2042 fld
->child
[0], evD
+size
+size1
);
2048 fld
->fixed_root
= (offsetRoot
==-1) ? 0 : 1;
2049 fld
->fixed_parent
= (offsetParent
==-1) ? 0 : 1;
2056 if(fld
->field_fixed
== -1){
2057 fld
->field_fixed
= 0;
2059 /* Hope my implementation is faster than strlen (Mathieu) */
2060 char *ptr
=(char*)evD
;
2062 /* from gcov : many many strings are empty, make it the common case.*/
2063 while(unlikely(*ptr
!= '\0')) { size
++; ptr
++; }
2064 //size = ptr - (char*)evD + 1; //include end : '\0'
2066 fld
->fixed_root
= (offsetRoot
==-1) ? 0 : 1;
2067 fld
->fixed_parent
= (offsetParent
==-1) ? 0 : 1;
2072 element_number
= (int) type
->element_number
;
2074 /* tested with gcov */
2075 if(unlikely(fld
->field_fixed
== -1)){
2076 offset1
= offsetRoot
;
2078 for(i
=0;i
<element_number
;i
++){
2079 size1
=getFieldtypeSize(tf
, evT
,offset1
,offset2
,
2080 fld
->child
[i
], NULL
);
2081 if(likely(size1
> 0 && size
>= 0)){
2083 if(likely(offset1
>= 0)) offset1
+= size1
;
2091 if(unlikely(size
== -1)){
2092 fld
->field_fixed
= 0;
2094 }else fld
->field_fixed
= 1;
2095 }else if(likely(fld
->field_fixed
== 0)){
2096 offset1
= offsetRoot
;
2098 for(i
=0;unlikely(i
<element_number
);i
++){
2099 size
=getFieldtypeSize(tf
, evT
, offset1
, offset2
,
2100 fld
->child
[i
], evD
+offset2
);
2105 }else size
= fld
->field_size
;
2106 fld
->fixed_root
= (offsetRoot
==-1) ? 0 : 1;
2107 fld
->fixed_parent
= (offsetParent
==-1) ? 0 : 1;
2111 if(unlikely(fld
->field_fixed
== -1)){
2112 size
= (int) ltt_type_size(LTT_GET_BO(tf
), type
);
2113 fld
->field_fixed
= 1;
2114 }else size
= fld
->field_size
;
2116 fld
->fixed_root
= (offsetRoot
==-1) ? 0 : 1;
2117 fld
->fixed_parent
= (offsetParent
==-1) ? 0 : 1;
2122 fld
->offset_root
= offsetRoot
;
2123 fld
->offset_parent
= offsetParent
;
2124 fld
->field_size
= size
;
2126 end_getFieldtypeSize
:
2132 /*****************************************************************************
2134 * get_int : get an integer number
2136 * reverse_byte_order: must we reverse the byte order ?
2137 * size : the size of the integer
2138 * ptr : the data pointer
2140 * gint64 : a 64 bits integer
2141 ****************************************************************************/
2143 gint64
get_int(gboolean reverse_byte_order
, gint size
, void *data
)
2148 case 1: val
= *((gint8
*)data
); break;
2149 case 2: val
= ltt_get_int16(reverse_byte_order
, data
); break;
2150 case 4: val
= ltt_get_int32(reverse_byte_order
, data
); break;
2151 case 8: val
= ltt_get_int64(reverse_byte_order
, data
); break;
2152 default: val
= ltt_get_int64(reverse_byte_order
, data
);
2153 g_critical("get_int : integer size %d unknown", size
);
2160 /*****************************************************************************
2162 * get_uint : get an unsigned integer number
2164 * reverse_byte_order: must we reverse the byte order ?
2165 * size : the size of the integer
2166 * ptr : the data pointer
2168 * guint64 : a 64 bits unsigned integer
2169 ****************************************************************************/
2171 guint64
get_uint(gboolean reverse_byte_order
, gint size
, void *data
)
2176 case 1: val
= *((gint8
*)data
); break;
2177 case 2: val
= ltt_get_uint16(reverse_byte_order
, data
); break;
2178 case 4: val
= ltt_get_uint32(reverse_byte_order
, data
); break;
2179 case 8: val
= ltt_get_uint64(reverse_byte_order
, data
); break;
2180 default: val
= ltt_get_uint64(reverse_byte_order
, data
);
2181 g_critical("get_uint : unsigned integer size %d unknown",
2190 /* get the node name of the system */
2192 char * ltt_trace_system_description_node_name (LttSystemDescription
* s
)
2194 return s
->node_name
;
2198 /* get the domain name of the system */
2200 char * ltt_trace_system_description_domain_name (LttSystemDescription
* s
)
2202 return s
->domain_name
;
2206 /* get the description of the system */
2208 char * ltt_trace_system_description_description (LttSystemDescription
* s
)
2210 return s
->description
;
2214 /* get the start time of the trace */
2216 LttTime
ltt_trace_system_description_trace_start_time(LttSystemDescription
*s
)
2218 return s
->trace_start
;
2222 LttTracefile
*ltt_tracefile_new()
2224 return g_new(LttTracefile
, 1);
2227 void ltt_tracefile_destroy(LttTracefile
*tf
)
2232 void ltt_tracefile_copy(LttTracefile
*dest
, const LttTracefile
*src
)
2237 /* Before library loading... */
2239 static void __attribute__((constructor
)) init(void)
2241 LTT_FACILITY_NAME_HEARTBEAT
= g_quark_from_string("heartbeat");
2242 LTT_EVENT_NAME_HEARTBEAT
= g_quark_from_string("heartbeat");
2244 LTT_TRACEFILE_NAME_FACILITIES
= g_quark_from_string("control/facilities");