1 /* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2005 Mathieu Desnoyers
4 * Complete rewrite from the original version made by XangXiu Yang.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License Version 2 as
8 * published by the Free Software Foundation;
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 #include <sys/types.h>
45 #include "ltt-private.h"
46 #include <ltt/trace.h>
47 #include <ltt/facility.h>
48 #include <ltt/event.h>
50 #include <ltt/ltt-types.h>
53 /* Facility names used in this file */
55 GQuark LTT_FACILITY_NAME_HEARTBEAT
,
56 LTT_EVENT_NAME_HEARTBEAT
;
57 GQuark LTT_TRACEFILE_NAME_FACILITIES
;
64 #define __UNUSED__ __attribute__((__unused__))
66 #define g_info(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, format)
69 #define g_debug(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format)
74 /* Those macros must be called from within a function where page_size is a known
76 #define PAGE_MASK (~(page_size-1))
77 #define PAGE_ALIGN(addr) (((addr)+page_size-1)&PAGE_MASK)
79 /* set the offset of the fields belonging to the event,
80 need the information of the archecture */
81 void set_fields_offsets(LttTracefile
*tf
, LttEventType
*event_type
);
82 //size_t get_fields_offsets(LttTracefile *tf, LttEventType *event_type, void *data);
84 /* get the size of the field type according to
85 * The facility size information. */
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
,
92 /* map a fixed size or a block information from the file (fd) */
93 static gint
map_block(LttTracefile
* tf
, guint block_num
);
95 /* calculate nsec per cycles for current block */
96 static double calc_nsecs_per_cycle(LttTracefile
* t
);
98 /* go to the next event */
99 static int ltt_seek_next_event(LttTracefile
*tf
);
101 void ltt_update_event_size(LttTracefile
*tf
);
104 /* Functions to parse system.xml file (using glib xml parser) */
105 static void parser_start_element (GMarkupParseContext __UNUSED__
*context
,
106 const gchar
*element_name
,
107 const gchar
**attribute_names
,
108 const gchar
**attribute_values
,
113 LttSystemDescription
* des
= (LttSystemDescription
* )user_data
;
114 if(strcmp("system", element_name
)){
115 *error
= g_error_new(G_MARKUP_ERROR
,
117 "This is not system.xml file");
121 while(attribute_names
[i
]){
122 if(strcmp("node_name", attribute_names
[i
])==0){
123 des
->node_name
= g_strdup(attribute_values
[i
]);
124 }else if(strcmp("domainname", attribute_names
[i
])==0){
125 des
->domain_name
= g_strdup(attribute_values
[i
]);
126 }else if(strcmp("cpu", attribute_names
[i
])==0){
127 des
->nb_cpu
= atoi(attribute_values
[i
]);
128 }else if(strcmp("arch_size", attribute_names
[i
])==0){
129 if(strcmp(attribute_values
[i
],"LP32") == 0) des
->size
= LTT_LP32
;
130 else if(strcmp(attribute_values
[i
],"ILP32") == 0) des
->size
= LTT_ILP32
;
131 else if(strcmp(attribute_values
[i
],"LP64") == 0) des
->size
= LTT_LP64
;
132 else if(strcmp(attribute_values
[i
],"ILP64") == 0) des
->size
= LTT_ILP64
;
133 else if(strcmp(attribute_values
[i
],"UNKNOWN") == 0) des
->size
= LTT_UNKNOWN
;
134 }else if(strcmp("endian", attribute_names
[i
])==0){
135 if(strcmp(attribute_values
[i
],"LITTLE_ENDIAN") == 0)
136 des
->endian
= LTT_LITTLE_ENDIAN
;
137 else if(strcmp(attribute_values
[i
],"BIG_ENDIAN") == 0)
138 des
->endian
= LTT_BIG_ENDIAN
;
139 }else if(strcmp("kernel_name", attribute_names
[i
])==0){
140 des
->kernel_name
= g_strdup(attribute_values
[i
]);
141 }else if(strcmp("kernel_release", attribute_names
[i
])==0){
142 des
->kernel_release
= g_strdup(attribute_values
[i
]);
143 }else if(strcmp("kernel_version", attribute_names
[i
])==0){
144 des
->kernel_version
= g_strdup(attribute_values
[i
]);
145 }else if(strcmp("machine", attribute_names
[i
])==0){
146 des
->machine
= g_strdup(attribute_values
[i
]);
147 }else if(strcmp("processor", attribute_names
[i
])==0){
148 des
->processor
= g_strdup(attribute_values
[i
]);
149 }else if(strcmp("hardware_platform", attribute_names
[i
])==0){
150 des
->hardware_platform
= g_strdup(attribute_values
[i
]);
151 }else if(strcmp("operating_system", attribute_names
[i
])==0){
152 des
->operating_system
= g_strdup(attribute_values
[i
]);
153 }else if(strcmp("ltt_major_version", attribute_names
[i
])==0){
154 des
->ltt_major_version
= atoi(attribute_values
[i
]);
155 }else if(strcmp("ltt_minor_version", attribute_names
[i
])==0){
156 des
->ltt_minor_version
= atoi(attribute_values
[i
]);
157 }else if(strcmp("ltt_block_size", attribute_names
[i
])==0){
158 des
->ltt_block_size
= atoi(attribute_values
[i
]);
160 *error
= g_error_new(G_MARKUP_ERROR
,
162 "Not a valid attribute");
169 static void parser_characters (GMarkupParseContext __UNUSED__
*context
,
171 gsize __UNUSED__ text_len
,
173 GError __UNUSED__
**error
)
175 LttSystemDescription
* des
= (LttSystemDescription
* )user_data
;
176 des
->description
= g_strdup(text
);
179 LttFacility
*ltt_trace_get_facility_by_num(LttTrace
*t
,
182 g_assert(num
< t
->facilities_by_num
->len
);
184 return &g_array_index(t
->facilities_by_num
, LttFacility
, num
);
189 /*****************************************************************************
191 * ltt_tracefile_open : open a trace file, construct a LttTracefile
193 * t : the trace containing the tracefile
194 * fileName : path name of the trace file
195 * tf : the tracefile structure
197 * : 0 for success, -1 otherwise.
198 ****************************************************************************/
200 gint
ltt_tracefile_open(LttTrace
*t
, gchar
* fileName
, LttTracefile
*tf
)
202 struct stat lTDFStat
; /* Trace data file status */
203 struct ltt_block_start_header
*header
;
204 int page_size
= getpagesize();
207 tf
->long_name
= g_quark_from_string(fileName
);
209 tf
->fd
= open(fileName
, O_RDONLY
);
211 g_warning("Unable to open input data file %s\n", fileName
);
215 // Get the file's status
216 if(fstat(tf
->fd
, &lTDFStat
) < 0){
217 g_warning("Unable to get the status of the input data file %s\n", fileName
);
221 // Is the file large enough to contain a trace
222 if(lTDFStat
.st_size
< (off_t
)(sizeof(struct ltt_block_start_header
))){
223 g_print("The input data file %s does not contain a trace\n", fileName
);
227 /* Temporarily map the buffer start header to get trace information */
228 /* Multiple of pages aligned head */
229 tf
->buffer
.head
= mmap(0,
230 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)), PROT_READ
,
231 MAP_PRIVATE
, tf
->fd
, 0);
232 if(tf
->buffer
.head
== MAP_FAILED
) {
233 perror("Error in allocating memory for buffer of tracefile");
236 g_assert( ( (guint
)tf
->buffer
.head
&(8-1) ) == 0); // make sure it's aligned.
238 header
= (struct ltt_block_start_header
*)tf
->buffer
.head
;
240 if(header
->trace
.magic_number
== LTT_MAGIC_NUMBER
)
242 else if(header
->trace
.magic_number
== LTT_REV_MAGIC_NUMBER
)
244 else /* invalid magic number, bad tracefile ! */
247 //store the size of the file
248 tf
->file_size
= lTDFStat
.st_size
;
249 tf
->buf_size
= ltt_get_uint32(LTT_GET_BO(tf
), &header
->buf_size
);
250 tf
->num_blocks
= tf
->file_size
/ tf
->buf_size
;
252 if(munmap(tf
->buffer
.head
,
253 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)))) {
254 g_warning("unmap size : %u\n",
255 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)));
256 perror("munmap error");
259 tf
->buffer
.head
= NULL
;
261 //read the first block
262 if(map_block(tf
,0)) {
263 perror("Cannot map block for tracefile");
271 if(munmap(tf
->buffer
.head
,
272 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)))) {
273 g_warning("unmap size : %u\n",
274 PAGE_ALIGN(sizeof(struct ltt_block_start_header
)));
275 perror("munmap error");
284 LttTrace
*ltt_tracefile_get_trace(LttTracefile
*tf
)
290 /*****************************************************************************
291 *Open control and per cpu tracefiles
292 ****************************************************************************/
294 void ltt_tracefile_open_cpu(LttTrace
*t
, gchar
* tracefile_name
)
297 tf
= ltt_tracefile_open(t
,tracefile_name
);
299 t
->per_cpu_tracefile_number
++;
300 g_ptr_array_add(t
->per_cpu_tracefiles
, tf
);
303 gint
ltt_tracefile_open_control(LttTrace
*t
, gchar
* control_name
)
312 tf
= ltt_tracefile_open(t
,control_name
);
314 g_warning("ltt_tracefile_open_control : bad file descriptor");
317 t
->control_tracefile_number
++;
318 g_ptr_array_add(t
->control_tracefiles
,tf
);
320 //parse facilities tracefile to get base_id
321 if(strcmp(&control_name
[strlen(control_name
)-10],"facilities") ==0){
323 if(!ltt_tracefile_read(tf
,&ev
)) return 0; // end of file
325 if(ev
.event_id
== TRACE_FACILITY_LOAD
){
327 fLoad
.name
= (gchar
*)pos
;
328 fLoad
.checksum
= *(LttChecksum
*)(pos
+ strlen(fLoad
.name
));
329 fLoad
.base_code
= *(guint32
*)(pos
+ strlen(fLoad
.name
) + sizeof(LttChecksum
));
331 for(i
=0;i
<t
->facility_number
;i
++){
332 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
,i
);
333 if(strcmp(f
->name
,fLoad
.name
)==0 && fLoad
.checksum
==f
->checksum
){
334 f
->base_id
= fLoad
.base_code
;
338 if(i
==t
->facility_number
) {
339 g_warning("Facility: %s, checksum: %u is not found",
340 fLoad
.name
,(unsigned int)fLoad
.checksum
);
343 }else if(ev
.event_id
== TRACE_BLOCK_START
){
345 }else if(ev
.event_id
== TRACE_BLOCK_END
){
348 g_warning("Not valid facilities trace file");
357 /*****************************************************************************
359 * ltt_tracefile_close: close a trace file,
361 * t : tracefile which will be closed
362 ****************************************************************************/
364 void ltt_tracefile_close(LttTracefile
*t
)
366 int page_size
= getpagesize();
368 if(t
->buffer
.head
!= NULL
)
369 if(munmap(t
->buffer
.head
, PAGE_ALIGN(t
->buf_size
))) {
370 g_warning("unmap size : %u\n",
371 PAGE_ALIGN(t
->buf_size
));
372 perror("munmap error");
380 /*****************************************************************************
381 *Get system information
382 ****************************************************************************/
384 gint
getSystemInfo(LttSystemDescription
* des
, gchar
* pathname
)
391 GMarkupParseContext
* context
;
392 GError
* error
= NULL
;
393 GMarkupParser markup_parser
=
395 parser_start_element
,
398 NULL
, /* passthrough */
402 fd
= g_open(pathname
, O_RDONLY
, 0);
404 g_warning("Can not open file : %s\n", pathname
);
408 iochan
= g_io_channel_unix_new(fd
);
410 context
= g_markup_parse_context_new(&markup_parser
, 0, des
,NULL
);
412 //while(fgets(buf,DIR_NAME_SIZE, fp) != NULL){
413 while(g_io_channel_read_line(iochan
, &buf
, &length
, NULL
, &error
)
414 != G_IO_STATUS_EOF
) {
417 g_warning("Can not read xml file: \n%s\n", error
->message
);
420 if(!g_markup_parse_context_parse(context
, buf
, length
, &error
)){
422 g_warning("Can not parse xml file: \n%s\n", error
->message
);
425 g_markup_parse_context_free(context
);
427 g_io_channel_shutdown(iochan
, FALSE
, &error
); /* No flush */
429 g_warning("Can not close file: \n%s\n", error
->message
);
437 g_markup_parse_context_free(context
);
439 g_io_channel_shutdown(iochan
, FALSE
, &error
); /* No flush */
441 g_warning("Can not close file: \n%s\n", error
->message
);
452 /*****************************************************************************
453 *The following functions get facility/tracefile information
454 ****************************************************************************/
456 gint
getFacilityInfo(LttTrace
*t
, gchar
* eventdefs
)
463 gchar fullname
[DIR_NAME_SIZE
];
464 GError
* error
= NULL
;
466 dir
= g_dir_open(eventdefs
, 0, &error
);
469 g_warning("Can not open directory: %s, %s\n", eventdefs
, error
->message
);
474 while((name
= g_dir_read_name(dir
)) != NULL
){
475 if(!g_pattern_match_simple("*.xml", name
)) continue;
476 strcpy(fullname
,eventdefs
);
477 strcat(fullname
,name
);
478 ltt_facility_open(t
,fullname
);
482 for(j
=0;j
<t
->facility_number
;j
++){
483 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, j
);
484 for(i
=0; i
<f
->event_number
; i
++){
486 setFieldsOffset(NULL
, et
, NULL
, t
);
493 /*****************************************************************************
494 *A trace is specified as a pathname to the directory containing all the
495 *associated data (control tracefiles, per cpu tracefiles, event
498 *When a trace is closed, all the associated facilities, types and fields
499 *are released as well.
503 /****************************************************************************
504 * get_absolute_pathname
506 * return the unique pathname in the system
508 * MD : Fixed this function so it uses realpath, dealing well with
509 * forgotten cases (.. were not used correctly before).
511 ****************************************************************************/
512 void get_absolute_pathname(const gchar
*pathname
, gchar
* abs_pathname
)
514 abs_pathname
[0] = '\0';
516 if ( realpath (pathname
, abs_pathname
) != NULL
)
520 /* error, return the original path unmodified */
521 strcpy(abs_pathname
, pathname
);
527 /* Search for something like : .*_.*
529 * The left side is the name, the right side is the number.
532 int get_tracefile_name_number(const gchar
*raw_name
,
536 guint raw_name_len
= strlen(raw_name
);
537 gchar char_name
[PATH_MAX
];
543 for(i
=raw_name_len
-1;i
>=0;i
--) {
544 if(raw_name
[i
] == '_') break;
546 if(i
==0) /* Either not found or name length is 0 */
550 cpu_num
= strtol(raw_name
+underscore_pos
+1, &endptr
, 10);
552 if(endptr
== raw_name
+underscore_pos
+1)
553 return -1; /* No digit */
554 if(cpu_num
== LONG_MIN
|| cpu_num
== LONG_MAX
)
555 return -1; /* underflow / overflow */
557 strncpy(char_name
, raw_name
, underscore_pos
);
559 char_name
[underscore_pos
] = '\0';
561 *name
= g_quark_from_string(char_name
);
568 GData
**ltt_trace_get_tracefiles_groups(LttTrace
*trace
)
570 return &trace
->tracefiles
;
574 void compute_tracefile_group(GQuark key_id
,
576 struct compute_tracefile_group_args
*args
)
581 for(i
=0; i
<group
->len
; i
++) {
582 tf
= &g_array_index (group
, LttTracefile
, i
);
584 args
->func(tf
, args
->func_args
);
589 void ltt_tracefile_group_destroy(gpointer data
)
591 GArray
*group
= (GArray
*)data
;
595 for(i
=0; i
<group
->len
; i
++) {
596 tf
= &g_array_index (group
, LttTracefile
, i
);
598 ltt_tracefile_close(tf
);
600 g_array_free(group
, TRUE
);
603 gboolean
ltt_tracefile_group_has_cpu_online(gpointer data
)
605 GArray
*group
= (GArray
*)data
;
609 for(i
=0; i
<group
->len
; i
++) {
610 tf
= &g_array_index (group
, LttTracefile
, i
);
611 if(tf
->cpu_online
) return 1;
617 /* Open each tracefile under a specific directory. Put them in a
618 * GData : permits to access them using their tracefile group pathname.
619 * i.e. access control/modules tracefile group by index :
622 * relative path is the path relative to the trace root
623 * root path is the full path
625 * A tracefile group is simply an array where all the per cpu tracefiles sits.
628 static int open_tracefiles(LttTrace
*trace
, gchar
*root_path
,
629 gchar
*relative_path
)
631 DIR *dir
= opendir(root_path
);
632 struct dirent
*entry
;
633 struct stat stat_buf
;
636 gchar path
[PATH_MAX
];
641 gchar rel_path
[PATH_MAX
];
650 strncpy(path
, root_path
, PATH_MAX
-1);
651 path_len
= strlen(path
);
652 path
[path_len
] = '/';
654 path_ptr
= path
+ path_len
;
656 strncpy(rel_path
, relative_path
, PATH_MAX
-1);
657 rel_path_len
= strlen(rel_path
);
658 rel_path
[rel_path_len
] = '/';
660 rel_path_ptr
= rel_path
+ rel_path_len
;
662 while((entry
= readdir(dir
)) != NULL
) {
664 if(entry
->d_name
[0] == '.') continue;
666 strncpy(path_ptr
, entry
->d_name
, PATH_MAX
- path_len
);
667 strncpy(rel_path_ptr
, entry
->d_name
, PATH_MAX
- rel_path_len
);
669 ret
= stat(path
, &stat_buf
);
675 g_debug("Tracefile file or directory : %s\n", path
);
677 if(S_ISDIR(stat_buf
.st_mode
)) {
679 g_debug("Entering subdirectory...\n");
680 ret
= open_tracefiles(trace
, path
, rel_path
);
681 if(ret
< 0) continue;
682 } else if(S_ISREG(stat_buf
.st_mode
)) {
687 if(get_tracefile_name_number(rel_path
, &name
, &num
))
688 continue; /* invalid name */
690 g_debug("Opening file.\n");
691 if(ltt_tracefile_open(trace
, path
, &tmp_tf
)) {
692 g_info("Error opening tracefile %s", path
);
694 continue; /* error opening the tracefile : bad magic number ? */
697 g_debug("Tracefile name is %s and number is %u",
698 g_quark_to_string(name
), num
);
700 tmp_tf
.cpu_online
= 1;
701 tmp_tf
.cpu_num
= num
;
704 group
= g_datalist_id_get_data(&trace
->tracefiles
, name
);
706 /* Elements are automatically cleared when the array is allocated.
707 * It makes the cpu_online variable set to 0 : cpu offline, by default.
709 group
= g_array_sized_new (FALSE
, TRUE
, sizeof(LttTracefile
), 10);
710 g_datalist_id_set_data_full(&trace
->tracefiles
, name
,
711 group
, ltt_tracefile_group_destroy
);
714 /* Add the per cpu tracefile to the named group */
715 unsigned int old_len
= group
->len
;
717 group
= g_array_set_size(group
, num
+1);
718 g_array_index (group
, LttTracefile
, num
) = tmp_tf
;
728 /* ltt_get_facility_description
730 * Opens the trace corresponding to the requested facility (identified by fac_id
733 * The name searched is : %trace root%/eventdefs/facname_checksum.xml
735 * Returns 0 on success, or 1 on failure.
738 static int ltt_get_facility_description(LttFacility
*f
,
740 LttTracefile
*fac_tf
)
742 char desc_file_name
[PATH_MAX
];
747 text
= g_quark_to_string(t
->pathname
);
748 textlen
= strlen(text
);
750 if(textlen
>= PATH_MAX
) goto name_error
;
751 strcpy(desc_file_name
, text
);
753 text
= "/eventdefs/";
754 textlen
+=strlen(text
);
755 if(textlen
>= PATH_MAX
) goto name_error
;
756 strcat(desc_file_name
, text
);
758 text
= g_quark_to_string(f
->name
);
759 textlen
+=strlen(text
);
760 if(textlen
>= PATH_MAX
) goto name_error
;
761 strcat(desc_file_name
, text
);
764 textlen
+=strlen(text
);
765 if(textlen
>= PATH_MAX
) goto name_error
;
766 strcat(desc_file_name
, text
);
768 err
= snprintf(desc_file_name
+textlen
, PATH_MAX
-textlen
-1,
770 if(err
< 0) goto name_error
;
772 textlen
=strlen(desc_file_name
);
775 textlen
+=strlen(text
);
776 if(textlen
>= PATH_MAX
) goto name_error
;
777 strcat(desc_file_name
, text
);
779 err
= ltt_facility_open(f
, t
, desc_file_name
);
780 if(err
) goto facility_error
;
789 static void ltt_fac_ids_destroy(gpointer data
)
791 GArray
*fac_ids
= (GArray
*)data
;
793 g_array_free(fac_ids
, TRUE
);
797 /* Presumes the tracefile is already seeked at the beginning. It makes sense,
798 * because it must be done just after the opening */
799 static int ltt_process_facility_tracefile(LttTracefile
*tf
)
808 err
= ltt_tracefile_read_seek(tf
);
809 if(err
== EPERM
) goto seek_error
;
810 else if(err
== ERANGE
) break; /* End of tracefile */
812 err
= ltt_tracefile_read_update_event(tf
);
813 if(err
) goto update_error
;
815 /* We are on a facility load/or facility unload/ or heartbeat event */
817 * * facility 0 is hardcoded : this is the core facility. It will be shown
818 * in the facility array though, and is shown as "loaded builtin" in the
820 * It contains event :
822 * 1 : facility unload
823 * 2 : state dump facility load
826 if(tf
->event
.facility_id
!= LTT_FACILITY_CORE
) {
827 /* Should only contain core facility */
828 g_warning("Error in processing facility file %s, "
829 "should not contain facility id %u.", g_quark_to_string(tf
->name
),
830 tf
->event
.facility_id
);
835 struct LttFacilityLoad
*fac_load_data
;
836 struct LttStateDumpFacilityLoad
*fac_state_dump_load_data
;
840 switch((enum ltt_core_events
)tf
->event
.event_id
) {
841 case LTT_EVENT_FACILITY_LOAD
:
842 fac_name
= (char*)(tf
->event
.data
);
843 g_debug("Doing LTT_EVENT_FACILITY_LOAD of facility %s",
846 (struct LttFacilityLoad
*)
847 (tf
->event
.data
+ strlen(fac_name
) + 1);
848 fac
= &g_array_index (tf
->trace
->facilities_by_num
, LttFacility
,
849 ltt_get_uint32(LTT_GET_BO(tf
), &fac_load_data
->id
));
850 g_assert(fac
->exists
== 0);
851 fac
->name
= g_quark_from_string(fac_name
);
852 fac
->checksum
= ltt_get_uint32(LTT_GET_BO(tf
),
853 &fac_load_data
->checksum
);
854 fac
->id
= ltt_get_uint32(LTT_GET_BO(tf
), &fac_load_data
->id
);
855 fac
->pointer_size
= ltt_get_uint32(LTT_GET_BO(tf
),
856 &fac_load_data
->pointer_size
);
857 fac
->long_size
= ltt_get_uint32(LTT_GET_BO(tf
),
858 &fac_load_data
->long_size
);
859 fac
->size_t_size
= ltt_get_uint32(LTT_GET_BO(tf
),
860 &fac_load_data
->size_t_size
);
861 fac
->alignment
= ltt_get_uint32(LTT_GET_BO(tf
),
862 &fac_load_data
->alignment
);
864 if(ltt_get_facility_description(fac
, tf
->trace
, tf
))
865 continue; /* error opening description */
867 fac
->trace
= tf
->trace
;
869 /* Preset the field offsets */
870 for(i
=0; i
<fac
->events
->len
; i
++){
871 et
= &g_array_index(fac
->events
, LttEventType
, i
);
872 set_fields_offsets(tf
, et
);
877 fac_ids
= g_datalist_id_get_data(&tf
->trace
->facilities_by_name
,
879 if(fac_ids
== NULL
) {
880 fac_ids
= g_array_sized_new (FALSE
, TRUE
, sizeof(guint
), 1);
881 g_datalist_id_set_data_full(&tf
->trace
->facilities_by_name
,
883 fac_ids
, ltt_fac_ids_destroy
);
885 g_array_append_val(fac_ids
, fac
->id
);
888 case LTT_EVENT_FACILITY_UNLOAD
:
889 g_debug("Doing LTT_EVENT_FACILITY_UNLOAD");
890 /* We don't care about unload : facilities ID are valid for the whole
891 * trace. They simply won't be used after the unload. */
893 case LTT_EVENT_STATE_DUMP_FACILITY_LOAD
:
894 fac_name
= (char*)(tf
->event
.data
);
895 g_debug("Doing LTT_EVENT_STATE_DUMP_FACILITY_LOAD of facility %s",
897 fac_state_dump_load_data
=
898 (struct LttStateDumpFacilityLoad
*)
899 (tf
->event
.data
+ strlen(fac_name
) + 1);
900 fac
= &g_array_index (tf
->trace
->facilities_by_num
, LttFacility
,
901 ltt_get_uint32(LTT_GET_BO(tf
), &fac_state_dump_load_data
->id
));
902 g_assert(fac
->exists
== 0);
903 fac
->name
= g_quark_from_string(fac_name
);
904 fac
->checksum
= ltt_get_uint32(LTT_GET_BO(tf
),
905 &fac_state_dump_load_data
->checksum
);
906 fac
->id
= fac_state_dump_load_data
->id
;
907 fac
->pointer_size
= ltt_get_uint32(LTT_GET_BO(tf
),
908 &fac_state_dump_load_data
->pointer_size
);
909 fac
->long_size
= ltt_get_uint32(LTT_GET_BO(tf
),
910 &fac_state_dump_load_data
->long_size
);
911 fac
->size_t_size
= ltt_get_uint32(LTT_GET_BO(tf
),
912 &fac_state_dump_load_data
->size_t_size
);
913 fac
->alignment
= ltt_get_uint32(LTT_GET_BO(tf
),
914 &fac_state_dump_load_data
->alignment
);
915 if(ltt_get_facility_description(fac
, tf
->trace
, tf
))
916 continue; /* error opening description */
918 fac
->trace
= tf
->trace
;
920 /* Preset the field offsets */
921 for(i
=0; i
<fac
->events
->len
; i
++){
922 et
= &g_array_index(fac
->events
, LttEventType
, i
);
923 set_fields_offsets(tf
, et
);
928 fac_ids
= g_datalist_id_get_data(&tf
->trace
->facilities_by_name
,
930 if(fac_ids
== NULL
) {
931 fac_ids
= g_array_sized_new (FALSE
, TRUE
, sizeof(guint
), 1);
932 g_datalist_id_set_data_full(&tf
->trace
->facilities_by_name
,
934 fac_ids
, ltt_fac_ids_destroy
);
936 g_array_append_val(fac_ids
, fac
->id
);
939 case LTT_EVENT_HEARTBEAT
:
942 g_warning("Error in processing facility file %s, "
943 "unknown event id %hhu in core facility.",
944 g_quark_to_string(tf
->name
),
958 g_warning("An error occured in facility tracefile parsing");
963 LttTrace
*ltt_trace_open(const gchar
*pathname
)
965 gchar abs_path
[PATH_MAX
];
970 struct ltt_block_start_header
*header
;
972 t
= g_new(LttTrace
, 1);
973 if(!t
) goto alloc_error
;
975 get_absolute_pathname(pathname
, abs_path
);
976 t
->pathname
= g_quark_from_string(abs_path
);
978 /* Open all the tracefiles */
979 g_datalist_init(&t
->tracefiles
);
980 if(open_tracefiles(t
, abs_path
, "")) {
981 g_warning("Error opening tracefile %s", abs_path
);
985 /* Prepare the facilities containers : array and mapping */
986 /* Array is zeroed : the "exists" field is set to false by default */
987 t
->facilities_by_num
= g_array_sized_new (FALSE
,
988 TRUE
, sizeof(LttFacility
),
990 t
->facilities_by_num
= g_array_set_size(t
->facilities_by_num
, NUM_FACILITIES
);
992 g_datalist_init(&t
->facilities_by_name
);
994 /* Parse each trace control/facilitiesN files : get runtime fac. info */
995 group
= g_datalist_id_get_data(&t
->tracefiles
, LTT_TRACEFILE_NAME_FACILITIES
);
997 g_error("Trace %s has no facility tracefile", abs_path
);
999 goto facilities_error
;
1002 /* Get the trace information for the control/facility 0 tracefile */
1003 g_assert(group
->len
> 0);
1004 tf
= &g_array_index (group
, LttTracefile
, 0);
1005 header
= (struct ltt_block_start_header
*)tf
->buffer
.head
;
1006 t
->arch_type
= ltt_get_uint32(LTT_GET_BO(tf
), &header
->trace
.arch_type
);
1007 t
->arch_variant
= ltt_get_uint32(LTT_GET_BO(tf
), &header
->trace
.arch_variant
);
1008 t
->arch_size
= header
->trace
.arch_size
;
1009 t
->ltt_major_version
= header
->trace
.major_version
;
1010 t
->ltt_minor_version
= header
->trace
.minor_version
;
1011 t
->flight_recorder
= header
->trace
.flight_recorder
;
1012 t
->has_heartbeat
= header
->trace
.has_heartbeat
;
1013 t
->has_alignment
= header
->trace
.has_alignment
;
1014 t
->has_tsc
= header
->trace
.has_tsc
;
1017 for(i
=0; i
<group
->len
; i
++) {
1018 tf
= &g_array_index (group
, LttTracefile
, i
);
1019 if(ltt_process_facility_tracefile(tf
))
1020 goto facilities_error
;
1025 /* Error handling */
1027 g_datalist_clear(&t
->facilities_by_name
);
1028 g_array_free(t
->facilities_by_num
, TRUE
);
1030 g_datalist_clear(&t
->tracefiles
);
1037 GQuark
ltt_trace_name(const LttTrace
*t
)
1043 /******************************************************************************
1044 * When we copy a trace, we want all the opening actions to happen again :
1045 * the trace will be reopened and totally independant from the original.
1046 * That's why we call ltt_trace_open.
1047 *****************************************************************************/
1048 LttTrace
*ltt_trace_copy(LttTrace
*self
)
1050 return ltt_trace_open(g_quark_to_string(self
->pathname
));
1053 void ltt_trace_close(LttTrace
*t
)
1058 for(i
=0; i
<t
->facilities_by_num
->len
; i
++) {
1059 fac
= &g_array_index (t
->facilities_by_num
, LttFacility
, i
);
1061 ltt_facility_close(fac
);
1064 g_datalist_clear(&t
->facilities_by_name
);
1065 g_array_free(t
->facilities_by_num
, TRUE
);
1066 g_datalist_clear(&t
->tracefiles
);
1071 /*****************************************************************************
1072 *Get the system description of the trace
1073 ****************************************************************************/
1075 LttFacility
*ltt_trace_facility_by_id(LttTrace
*t
, guint8 id
)
1077 g_assert(id
< t
->facilities_by_num
->len
);
1078 return &g_array_index(t
->facilities_by_num
, LttFacility
, id
);
1081 /* ltt_trace_facility_get_by_name
1083 * Returns the GArray of facility indexes. All the fac_ids that matches the
1084 * requested facility name.
1086 * If name is not found, returns NULL.
1088 GArray
*ltt_trace_facility_get_by_name(LttTrace
*t
, GQuark name
)
1090 return g_datalist_id_get_data(&t
->facilities_by_name
, name
);
1093 /*****************************************************************************
1094 * Functions to discover all the event types in the trace
1095 ****************************************************************************/
1098 unsigned ltt_trace_eventtype_number(LttTrace
*t
)
1102 unsigned int num
= t
->facility_number
;
1106 f
= (LttFacility
*)g_ptr_array_index(t
->facilities
, i
);
1107 count
+= f
->event_number
;
1114 //use an iteration on all the trace facilities, and inside iteration on all the
1115 //event types in each facilities instead.
1116 LttEventType
*ltt_trace_eventtype_get(LttTrace
*t
, unsigned evId
)
1118 LttEventType
*event_type
;
1121 f
= ltt_trace_facility_by_id(t
,evId
);
1123 if(unlikely(!f
)) event_type
= NULL
;
1124 else event_type
= f
->events
[evId
- f
->base_id
];
1131 /*****************************************************************************
1132 * ltt_trace_find_tracefile
1134 * Find a tracefile by name and index in the group.
1136 * Returns a pointer to the tracefiles, else NULL.
1137 ****************************************************************************/
1139 LttTracefile
*ltt_trace_find_tracefile(LttTrace
*t
, const gchar
*name
)
1144 /*****************************************************************************
1145 * Get the start time and end time of the trace
1146 ****************************************************************************/
1148 static void ltt_tracefile_time_span_get(LttTracefile
*tf
,
1149 LttTime
*start
, LttTime
*end
)
1153 err
= map_block(tf
, 0);
1155 g_error("Can not map block");
1156 *start
= ltt_time_infinite
;
1158 *start
= tf
->buffer
.begin
.timestamp
;
1160 err
= map_block(tf
, tf
->num_blocks
- 1); /* Last block */
1162 g_error("Can not map block");
1163 *end
= ltt_time_zero
;
1165 *end
= tf
->buffer
.end
.timestamp
;
1168 struct tracefile_time_span_get_args
{
1174 static void group_time_span_get(GQuark name
, gpointer data
, gpointer user_data
)
1176 struct tracefile_time_span_get_args
*args
=
1177 (struct tracefile_time_span_get_args
*)user_data
;
1179 GArray
*group
= (GArray
*)data
;
1185 for(i
=0; i
<group
->len
; i
++) {
1186 tf
= &g_array_index (group
, LttTracefile
, i
);
1187 if(tf
->cpu_online
) {
1188 ltt_tracefile_time_span_get(tf
, &tmp_start
, &tmp_end
);
1189 if(ltt_time_compare(*args
->start
, tmp_start
)>0) *args
->start
= tmp_start
;
1190 if(ltt_time_compare(*args
->end
, tmp_end
)<0) *args
->end
= tmp_end
;
1195 void ltt_trace_time_span_get(LttTrace
*t
, LttTime
*start
, LttTime
*end
)
1197 LttTime min_start
= ltt_time_infinite
;
1198 LttTime max_end
= ltt_time_zero
;
1199 struct tracefile_time_span_get_args args
= { t
, &min_start
, &max_end
};
1201 g_datalist_foreach(&t
->tracefiles
, &group_time_span_get
, &args
);
1203 if(start
!= NULL
) *start
= min_start
;
1204 if(end
!= NULL
) *end
= max_end
;
1209 /*****************************************************************************
1210 *Get the name of a tracefile
1211 ****************************************************************************/
1213 GQuark
ltt_tracefile_name(LttTracefile
*tf
)
1219 guint
ltt_tracefile_num(LttTracefile
*tf
)
1224 /*****************************************************************************
1225 * Get the number of blocks in the tracefile
1226 ****************************************************************************/
1228 guint
ltt_tracefile_block_number(LttTracefile
*tf
)
1230 return tf
->num_blocks
;
1234 /* Seek to the first event in a tracefile that has a time equal or greater than
1235 * the time passed in parameter.
1237 * If the time parameter is outside the tracefile time span, seek to the first
1238 * event or if after, return ERANGE.
1240 * If the time parameter is before the first event, we have to seek specially to
1243 * If the time is after the end of the trace, return ERANGE.
1245 * Do a binary search to find the right block, then a sequential search in the
1246 * block to find the event.
1248 * In the special case where the time requested fits inside a block that has no
1249 * event corresponding to the requested time, the first event of the next block
1252 * IMPORTANT NOTE : // FIXME everywhere...
1254 * You MUST NOT do a ltt_tracefile_read right after a ltt_tracefile_seek_time :
1255 * you will jump over an event if you do.
1257 * Return value : 0 : no error, the tf->event can be used
1258 * ERANGE : time if after the last event of the trace
1259 * otherwise : this is an error.
1263 int ltt_tracefile_seek_time(LttTracefile
*tf
, LttTime time
)
1267 unsigned int block_num
, high
, low
;
1269 /* seek at the beginning of trace */
1270 err
= map_block(tf
, 0); /* First block */
1272 g_error("Can not map block");
1276 /* If the time is lower or equal the beginning of the trace,
1277 * go to the first event. */
1278 if(ltt_time_compare(time
, tf
->buffer
.begin
.timestamp
) <= 0) {
1279 ret
= ltt_tracefile_read(tf
);
1280 if(ret
== ERANGE
) goto range
;
1281 else if (ret
) goto fail
;
1282 goto found
; /* There is either no event in the trace or the event points
1283 to the first event in the trace */
1286 err
= map_block(tf
, tf
->num_blocks
- 1); /* Last block */
1288 g_error("Can not map block");
1292 /* If the time is after the end of the trace, return ERANGE. */
1293 if(ltt_time_compare(time
, tf
->buffer
.end
.timestamp
) > 0) {
1297 /* Binary search the block */
1298 high
= tf
->num_blocks
- 1;
1302 block_num
= ((high
-low
) / 2) + low
;
1304 err
= map_block(tf
, block_num
);
1306 g_error("Can not map block");
1310 /* We cannot divide anymore : this is what would happen if the time
1311 * requested was exactly between two consecutive buffers'end and start
1312 * timestamps. This is also what would happend if we didn't deal with out
1313 * of span cases prior in this function. */
1314 /* The event is right in the buffer!
1315 * (or in the next buffer first event) */
1317 ret
= ltt_tracefile_read(tf
);
1318 if(ret
== ERANGE
) goto range
; /* ERANGE or EPERM */
1319 else if(ret
) goto fail
;
1321 if(ltt_time_compare(time
, tf
->event
.event_time
) >= 0)
1325 } else if(ltt_time_compare(time
, tf
->buffer
.begin
.timestamp
) < 0) {
1326 /* go to lower part */
1328 } else if(ltt_time_compare(time
, tf
->buffer
.end
.timestamp
) > 0) {
1329 /* go to higher part */
1331 } else {/* The event is right in the buffer!
1332 (or in the next buffer first event) */
1334 ret
= ltt_tracefile_read(tf
);
1335 if(ret
== ERANGE
) goto range
; /* ERANGE or EPERM */
1336 else if(ret
) goto fail
;
1338 if(ltt_time_compare(time
, tf
->event
.event_time
) >= 0)
1350 /* Error handling */
1352 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1353 g_quark_to_string(tf
->name
));
1358 int ltt_tracefile_seek_position(LttTracefile
*tf
, const LttEventPosition
*ep
) {
1362 if(ep
->tracefile
!= tf
) {
1366 err
= map_block(tf
, ep
->block
);
1368 g_error("Can not map block");
1372 tf
->event
.offset
= ep
->offset
;
1374 err
= ltt_tracefile_read_update_event(tf
);
1376 err
= ltt_tracefile_read_op(tf
);
1382 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1383 g_quark_to_string(tf
->name
));
1387 /* Calculate the real event time based on the buffer boundaries */
1388 LttTime
ltt_interpolate_time(LttTracefile
*tf
, LttEvent
*event
)
1392 g_assert(tf
->trace
->has_tsc
);
1394 time
= ltt_time_from_uint64(
1395 (guint64
)(tf
->buffer
.tsc
- tf
->buffer
.begin
.cycle_count
) *
1396 tf
->buffer
.nsecs_per_cycle
);
1397 time
= ltt_time_add(tf
->buffer
.begin
.timestamp
, time
);
1403 /* Get the current event of the tracefile : valid until the next read */
1404 LttEvent
*ltt_tracefile_get_event(LttTracefile
*tf
)
1411 /*****************************************************************************
1413 * ltt_tracefile_read : Read the next event in the tracefile
1418 * Returns 0 if an event can be used in tf->event.
1419 * Returns ERANGE on end of trace. The event in tf->event still can be used
1420 * (if the last block was not empty).
1421 * Returns EPERM on error.
1423 * This function does make the tracefile event structure point to the event
1424 * currently pointed to by the tf->event.
1426 * Note : you must call a ltt_tracefile_seek to the beginning of the trace to
1427 * reinitialize it after an error if you want results to be coherent.
1428 * It would be the case if a end of trace last buffer has no event : the end
1429 * of trace wouldn't be returned, but an error.
1430 * We make the assumption there is at least one event per buffer.
1431 ****************************************************************************/
1433 int ltt_tracefile_read(LttTracefile
*tf
)
1437 err
= ltt_tracefile_read_seek(tf
);
1439 err
= ltt_tracefile_read_update_event(tf
);
1441 err
= ltt_tracefile_read_op(tf
);
1447 int ltt_tracefile_read_seek(LttTracefile
*tf
)
1451 /* Get next buffer until we finally have an event, or end of trace */
1453 err
= ltt_seek_next_event(tf
);
1454 if(unlikely(err
== ENOPROTOOPT
)) {
1458 /* Are we at the end of the buffer ? */
1460 if(unlikely(tf
->buffer
.index
== tf
->num_blocks
-1)){ /* end of trace ? */
1463 /* get next block */
1464 err
= map_block(tf
, tf
->buffer
.index
+ 1);
1466 g_error("Can not map block");
1470 } else break; /* We found an event ! */
1477 /* do specific operation on events */
1478 int ltt_tracefile_read_op(LttTracefile
*tf
)
1484 /* do event specific operation */
1486 /* do something if its an heartbeat event : increment the heartbeat count */
1487 //if(event->facility_id == LTT_FACILITY_CORE)
1488 // if(event->event_id == LTT_EVENT_HEARTBEAT)
1489 // tf->cur_heart_beat_number++;
1495 /* same as ltt_tracefile_read, but does not seek to the next event nor call
1496 * event specific operation. */
1497 int ltt_tracefile_read_update_event(LttTracefile
*tf
)
1503 pos
= tf
->buffer
.head
+ event
->offset
;
1505 /* Read event header */
1509 if(tf
->trace
->has_tsc
) {
1510 if(tf
->trace
->has_heartbeat
) {
1511 event
->time
.timestamp
= ltt_get_uint32(LTT_GET_BO(tf
),
1513 /* 32 bits -> 64 bits tsc */
1514 /* note : still works for seek and non seek cases. */
1515 if(event
->time
.timestamp
< (0xFFFFFFFFULL
&tf
->buffer
.tsc
)) {
1516 tf
->buffer
.tsc
= ((tf
->buffer
.tsc
&0xFFFFFFFF00000000ULL
)
1518 | (guint64
)event
->time
.timestamp
;
1519 event
->tsc
= tf
->buffer
.tsc
;
1522 tf
->buffer
.tsc
= (tf
->buffer
.tsc
&0xFFFFFFFF00000000ULL
)
1523 | (guint64
)event
->time
.timestamp
;
1524 event
->tsc
= tf
->buffer
.tsc
;
1526 pos
+= sizeof(guint32
);
1528 event
->tsc
= ltt_get_uint64(LTT_GET_BO(tf
), pos
);
1529 tf
->buffer
.tsc
= event
->tsc
;
1530 pos
+= sizeof(guint64
);
1533 event
->event_time
= ltt_interpolate_time(tf
, event
);
1535 event
->time
.delta
.tv_sec
= 0;
1536 event
->time
.delta
.tv_nsec
= ltt_get_uint32(LTT_GET_BO(tf
),
1537 pos
) * NSEC_PER_USEC
;
1539 event
->tsc
= tf
->buffer
.tsc
;
1541 event
->event_time
= ltt_time_add(tf
->buffer
.begin
.timestamp
,
1543 pos
+= sizeof(guint32
);
1546 event
->facility_id
= *(guint8
*)pos
;
1547 pos
+= sizeof(guint8
);
1549 event
->event_id
= *(guint8
*)pos
;
1550 pos
+= sizeof(guint8
);
1552 event
->event_size
= ltt_get_uint16(LTT_GET_BO(tf
), pos
);
1553 pos
+= sizeof(guint16
);
1557 /* get the data size and update the event fields with the current
1559 ltt_update_event_size(tf
);
1565 /****************************************************************************
1567 * map_block : map a block from the file
1569 * lttdes : ltt trace file
1570 * whichBlock : the block which will be read
1573 * EINVAL : lseek fail
1574 * EIO : can not read from the file
1575 ****************************************************************************/
1577 static gint
map_block(LttTracefile
* tf
, guint block_num
)
1579 int page_size
= getpagesize();
1580 struct ltt_block_start_header
*header
;
1582 g_assert(block_num
< tf
->num_blocks
);
1584 if(tf
->buffer
.head
!= NULL
) {
1585 if(munmap(tf
->buffer
.head
, PAGE_ALIGN(tf
->buf_size
))) {
1586 g_warning("unmap size : %u\n",
1587 PAGE_ALIGN(tf
->buf_size
));
1588 perror("munmap error");
1594 /* Multiple of pages aligned head */
1595 tf
->buffer
.head
= mmap(0,
1596 PAGE_ALIGN(tf
->buf_size
),
1597 PROT_READ
, MAP_PRIVATE
, tf
->fd
,
1598 PAGE_ALIGN((off_t
)tf
->buf_size
* (off_t
)block_num
));
1600 if(tf
->buffer
.head
== MAP_FAILED
) {
1601 perror("Error in allocating memory for buffer of tracefile");
1605 g_assert( ( (guint
)tf
->buffer
.head
&(8-1) ) == 0); // make sure it's aligned.
1608 tf
->buffer
.index
= block_num
;
1610 header
= (struct ltt_block_start_header
*)tf
->buffer
.head
;
1612 tf
->buffer
.begin
.timestamp
= ltt_get_time(LTT_GET_BO(tf
),
1613 &header
->begin
.timestamp
);
1614 tf
->buffer
.begin
.timestamp
.tv_nsec
*= NSEC_PER_USEC
;
1615 g_debug("block %u begin : %lu.%lu", block_num
,
1616 tf
->buffer
.begin
.timestamp
.tv_sec
, tf
->buffer
.begin
.timestamp
.tv_nsec
);
1617 tf
->buffer
.begin
.cycle_count
= ltt_get_uint64(LTT_GET_BO(tf
),
1618 &header
->begin
.cycle_count
);
1619 tf
->buffer
.end
.timestamp
= ltt_get_time(LTT_GET_BO(tf
),
1620 &header
->end
.timestamp
);
1621 tf
->buffer
.end
.timestamp
.tv_nsec
*= NSEC_PER_USEC
;
1622 g_debug("block %u end : %lu.%lu", block_num
,
1623 tf
->buffer
.end
.timestamp
.tv_sec
, tf
->buffer
.end
.timestamp
.tv_nsec
);
1624 tf
->buffer
.end
.cycle_count
= ltt_get_uint64(LTT_GET_BO(tf
),
1625 &header
->end
.cycle_count
);
1626 tf
->buffer
.lost_size
= ltt_get_uint32(LTT_GET_BO(tf
),
1627 &header
->lost_size
);
1629 tf
->buffer
.tsc
= tf
->buffer
.begin
.cycle_count
;
1630 tf
->event
.tsc
= tf
->buffer
.tsc
;
1633 * eventually support variable buffer size : will need a partial pre-read of
1634 * the headers to create an index when we open the trace... eventually. */
1635 g_assert(tf
->buf_size
== ltt_get_uint32(LTT_GET_BO(tf
),
1636 &header
->buf_size
));
1638 /* Now that the buffer is mapped, calculate the time interpolation for the
1641 tf
->buffer
.nsecs_per_cycle
= calc_nsecs_per_cycle(tf
);
1643 /* Make the current event point to the beginning of the buffer :
1644 * it means that the event read must get the first event. */
1645 tf
->event
.tracefile
= tf
;
1646 tf
->event
.block
= block_num
;
1647 tf
->event
.offset
= 0;
1656 /* It will update the fields offsets too */
1657 void ltt_update_event_size(LttTracefile
*tf
)
1661 /* Specific handling of core events : necessary to read the facility control
1663 LttFacility
*f
= ltt_trace_get_facility_by_num(tf
->trace
,
1664 tf
->event
.facility_id
);
1666 if(likely(tf
->event
.facility_id
== LTT_FACILITY_CORE
)) {
1667 switch((enum ltt_core_events
)tf
->event
.event_id
) {
1668 case LTT_EVENT_FACILITY_LOAD
:
1669 size
= strlen((char*)tf
->event
.data
) + 1;
1670 g_debug("Update Event facility load of facility %s", (char*)tf
->event
.data
);
1671 size
+= sizeof(struct LttFacilityLoad
);
1673 case LTT_EVENT_FACILITY_UNLOAD
:
1674 g_debug("Update Event facility unload");
1675 size
= sizeof(struct LttFacilityUnload
);
1677 case LTT_EVENT_STATE_DUMP_FACILITY_LOAD
:
1678 size
= strlen((char*)tf
->event
.data
) + 1;
1679 g_debug("Update Event facility load state dump of facility %s",
1680 (char*)tf
->event
.data
);
1681 size
+= sizeof(struct LttStateDumpFacilityLoad
);
1683 case LTT_EVENT_HEARTBEAT
:
1684 g_debug("Update Event heartbeat");
1685 size
= sizeof(TimeHeartbeat
);
1688 g_warning("Error in getting event size : tracefile %s, "
1689 "unknown event id %hhu in core facility.",
1690 g_quark_to_string(tf
->name
),
1691 tf
->event
.event_id
);
1692 goto event_id_error
;
1697 g_error("Unknown facility %hhu (0x%hhx) in tracefile %s",
1698 tf
->event
.facility_id
,
1699 tf
->event
.facility_id
,
1700 g_quark_to_string(tf
->name
));
1701 goto facility_error
;
1704 LttEventType
*event_type
=
1705 ltt_facility_eventtype_get(f
, tf
->event
.event_id
);
1708 g_error("Unknown event id %hhu in facility %s in tracefile %s",
1710 g_quark_to_string(f
->name
),
1711 g_quark_to_string(tf
->name
));
1712 goto event_type_error
;
1715 if(event_type
->root_field
)
1716 size
= get_field_type_size(tf
, event_type
,
1717 0, 0, event_type
->root_field
, tf
->event
.data
);
1721 g_debug("Event root field : f.e %hhu.%hhu size %zd",
1722 tf
->event
.facility_id
,
1723 tf
->event
.event_id
, size
);
1726 tf
->event
.data_size
= size
;
1728 /* Check consistency between kernel and LTTV structure sizes */
1729 g_assert(tf
->event
.data_size
== tf
->event
.event_size
);
1736 tf
->event
.data_size
= 0;
1740 /* Take the tf current event offset and use the event facility id and event id
1741 * to figure out where is the next event offset.
1743 * This is an internal function not aiming at being used elsewhere : it will
1744 * not jump over the current block limits. Please consider using
1745 * ltt_tracefile_read to do this.
1747 * Returns 0 on success
1748 * ERANGE if we are at the end of the buffer.
1749 * ENOPROTOOPT if an error occured when getting the current event size.
1751 static int ltt_seek_next_event(LttTracefile
*tf
)
1756 /* seek over the buffer header if we are at the buffer start */
1757 if(tf
->event
.offset
== 0) {
1758 tf
->event
.offset
+= sizeof(struct ltt_block_start_header
);
1760 if(tf
->event
.offset
== tf
->buf_size
- tf
->buffer
.lost_size
) {
1767 pos
= tf
->event
.data
;
1769 if(tf
->event
.data_size
< 0) goto error
;
1771 pos
+= (size_t)tf
->event
.data_size
;
1773 tf
->event
.offset
= pos
- tf
->buffer
.head
;
1775 if(tf
->event
.offset
== tf
->buf_size
- tf
->buffer
.lost_size
) {
1784 g_error("Error in ltt_seek_next_event for tracefile %s",
1785 g_quark_to_string(tf
->name
));
1790 /*****************************************************************************
1792 * calc_nsecs_per_cycle : calculate nsecs per cycle for current block
1795 ****************************************************************************/
1797 static double calc_nsecs_per_cycle(LttTracefile
* tf
)
1799 LttTime lBufTotalTime
; /* Total time for this buffer */
1800 double lBufTotalNSec
; /* Total time for this buffer in nsecs */
1801 LttCycleCount lBufTotalCycle
;/* Total cycles for this buffer */
1803 /* Calculate the total time for this buffer */
1804 lBufTotalTime
= ltt_time_sub(tf
->buffer
.end
.timestamp
,
1805 tf
->buffer
.begin
.timestamp
);
1807 /* Calculate the total cycles for this bufffer */
1808 lBufTotalCycle
= tf
->buffer
.end
.cycle_count
;
1809 lBufTotalCycle
-= tf
->buffer
.begin
.cycle_count
;
1811 /* Convert the total time to double */
1812 lBufTotalNSec
= ltt_time_to_double(lBufTotalTime
);
1814 return lBufTotalNSec
/ (double)lBufTotalCycle
;
1818 void setFieldsOffset(LttTracefile
*tf
, LttEventType
*evT
,void *evD
)
1820 LttField
* rootFld
= evT
->root_field
;
1821 // rootFld->base_address = evD;
1824 rootFld
->field_size
= getFieldtypeSize(tf
, evT
->facility
,
1825 evT
, 0,0,rootFld
, evD
);
1829 /*****************************************************************************
1831 * set_fields_offsets : set the precomputable offset of the fields
1833 * tracefile : opened trace file
1834 * event_type : the event type
1835 ****************************************************************************/
1837 void set_fields_offsets(LttTracefile
*tf
, LttEventType
*event_type
)
1839 LttField
*field
= event_type
->root_field
;
1840 enum field_status fixed_root
= FIELD_FIXED
, fixed_parent
= FIELD_FIXED
;
1843 preset_field_type_size(tf
, event_type
, 0, 0,
1844 &fixed_root
, &fixed_parent
,
1850 /*****************************************************************************
1852 * preset_field_type_size : set the fixed sizes of the field type
1855 * event_type : event type
1856 * offset_root : offset from the root
1857 * offset_parent : offset from the parent
1858 * fixed_root : Do we know a fixed offset to the root ?
1859 * fixed_parent : Do we know a fixed offset to the parent ?
1861 ****************************************************************************/
1862 void preset_field_type_size(LttTracefile
*tf
, LttEventType
*event_type
,
1863 off_t offset_root
, off_t offset_parent
,
1864 enum field_status
*fixed_root
, enum field_status
*fixed_parent
,
1867 enum field_status local_fixed_root
, local_fixed_parent
;
1871 g_assert(field
->fixed_root
== FIELD_UNKNOWN
);
1872 g_assert(field
->fixed_parent
== FIELD_UNKNOWN
);
1873 g_assert(field
->fixed_size
== FIELD_UNKNOWN
);
1875 type
= field
->field_type
;
1877 field
->fixed_root
= *fixed_root
;
1878 if(field
->fixed_root
== FIELD_FIXED
)
1879 field
->offset_root
= offset_root
;
1881 field
->offset_root
= 0;
1883 field
->fixed_parent
= *fixed_parent
;
1884 if(field
->fixed_parent
== FIELD_FIXED
)
1885 field
->offset_parent
= offset_parent
;
1887 field
->offset_parent
= 0;
1889 size_t current_root_offset
;
1890 size_t current_offset
;
1891 enum field_status current_child_status
, final_child_status
;
1894 switch(type
->type_class
) {
1899 field
->field_size
= ltt_type_size(tf
->trace
, type
);
1900 field
->fixed_size
= FIELD_FIXED
;
1903 field
->field_size
= (off_t
)event_type
->facility
->pointer_size
;
1904 field
->fixed_size
= FIELD_FIXED
;
1908 field
->field_size
= (off_t
)event_type
->facility
->long_size
;
1909 field
->fixed_size
= FIELD_FIXED
;
1914 field
->field_size
= (off_t
)event_type
->facility
->size_t_size
;
1915 field
->fixed_size
= FIELD_FIXED
;
1918 local_fixed_root
= FIELD_VARIABLE
;
1919 local_fixed_parent
= FIELD_VARIABLE
;
1920 preset_field_type_size(tf
, event_type
,
1922 &local_fixed_root
, &local_fixed_parent
,
1924 field
->fixed_size
= FIELD_VARIABLE
;
1925 field
->field_size
= 0;
1926 *fixed_root
= FIELD_VARIABLE
;
1927 *fixed_parent
= FIELD_VARIABLE
;
1930 field
->fixed_size
= FIELD_VARIABLE
;
1931 field
->field_size
= 0;
1932 *fixed_root
= FIELD_VARIABLE
;
1933 *fixed_parent
= FIELD_VARIABLE
;
1936 local_fixed_root
= FIELD_VARIABLE
;
1937 local_fixed_parent
= FIELD_VARIABLE
;
1938 preset_field_type_size(tf
, event_type
,
1940 &local_fixed_root
, &local_fixed_parent
,
1942 field
->fixed_size
= field
->child
[0]->fixed_size
;
1943 if(field
->fixed_size
== FIELD_FIXED
) {
1944 field
->field_size
= type
->element_number
* field
->child
[0]->field_size
;
1946 field
->field_size
= 0;
1947 *fixed_root
= FIELD_VARIABLE
;
1948 *fixed_parent
= FIELD_VARIABLE
;
1952 current_root_offset
= field
->offset_root
;
1954 current_child_status
= FIELD_FIXED
;
1955 for(i
=0;i
<type
->element_number
;i
++) {
1956 preset_field_type_size(tf
, event_type
,
1957 current_root_offset
, current_offset
,
1958 fixed_root
, ¤t_child_status
,
1960 if(current_child_status
== FIELD_FIXED
) {
1961 current_root_offset
+= field
->child
[i
]->field_size
;
1962 current_offset
+= field
->child
[i
]->field_size
;
1964 current_root_offset
= 0;
1968 if(current_child_status
!= FIELD_FIXED
) {
1969 *fixed_parent
= current_child_status
;
1970 field
->field_size
= 0;
1971 field
->fixed_size
= current_child_status
;
1973 field
->field_size
= current_offset
;
1974 field
->fixed_size
= FIELD_FIXED
;
1978 current_root_offset
= field
->offset_root
;
1981 final_child_status
= FIELD_FIXED
;
1982 for(i
=0;i
<type
->element_number
;i
++) {
1983 enum field_status current_root_child_status
= FIELD_FIXED
;
1984 enum field_status current_child_status
= FIELD_FIXED
;
1985 preset_field_type_size(tf
, event_type
,
1986 current_root_offset
, current_offset
,
1987 ¤t_root_child_status
, ¤t_child_status
,
1989 if(current_child_status
!= FIELD_FIXED
)
1990 final_child_status
= current_child_status
;
1992 max_size
= max(max_size
, field
->child
[i
]->field_size
);
1994 if(final_child_status
!= FIELD_FIXED
) {
1995 *fixed_root
= final_child_status
;
1996 *fixed_parent
= final_child_status
;
1997 field
->field_size
= 0;
1998 field
->fixed_size
= current_child_status
;
2000 field
->field_size
= max_size
;
2001 field
->fixed_size
= FIELD_FIXED
;
2009 /*****************************************************************************
2011 * check_fields_compatibility : Check for compatibility between two fields :
2012 * do they use the same inner structure ?
2014 * event_type1 : event type
2015 * event_type2 : event type
2018 *Returns : 0 if identical
2020 ****************************************************************************/
2021 gint
check_fields_compatibility(LttEventType
*event_type1
,
2022 LttEventType
*event_type2
,
2023 LttField
*field1
, LttField
*field2
)
2025 guint different
= 0;
2030 if(field1
== NULL
) {
2031 if(field2
== NULL
) goto end
;
2036 } else if(field2
== NULL
) {
2041 g_assert(field1
->fixed_root
!= FIELD_UNKNOWN
);
2042 g_assert(field2
->fixed_root
!= FIELD_UNKNOWN
);
2043 g_assert(field1
->fixed_parent
!= FIELD_UNKNOWN
);
2044 g_assert(field2
->fixed_parent
!= FIELD_UNKNOWN
);
2045 g_assert(field1
->fixed_size
!= FIELD_UNKNOWN
);
2046 g_assert(field2
->fixed_size
!= FIELD_UNKNOWN
);
2048 type1
= field1
->field_type
;
2049 type2
= field2
->field_type
;
2051 if(type1
->type_class
!= type2
->type_class
) {
2055 if(type1
->element_name
!= type2
->element_name
) {
2060 switch(type1
->type_class
) {
2070 if(field1
->field_size
!= field2
->field_size
) {
2076 if(type1
->element_number
!= type2
->element_number
) {
2080 for(i
=0;i
<type1
->element_number
;i
++) {
2081 if(type1
->enum_strings
[i
] != type2
->enum_strings
[i
]) {
2088 /* Two elements : size and child */
2089 g_assert(type1
->element_number
!= type2
->element_number
);
2090 for(i
=0;i
<type1
->element_number
;i
++) {
2091 if(check_fields_compatibility(event_type1
, event_type2
,
2092 field1
->child
[0], field2
->child
[0])) {
2101 if(field1
->field_size
!= field2
->field_size
) {
2105 /* Two elements : size and child */
2106 g_assert(type1
->element_number
!= type2
->element_number
);
2107 for(i
=0;i
<type1
->element_number
;i
++) {
2108 if(check_fields_compatibility(event_type1
, event_type2
,
2109 field1
->child
[0], field2
->child
[0])) {
2117 if(type1
->element_number
!= type2
->element_number
) {
2121 for(i
=0;i
<type1
->element_number
;i
++) {
2122 if(check_fields_compatibility(event_type1
, event_type2
,
2123 field1
->child
[0], field2
->child
[0])) {
2138 /*****************************************************************************
2140 * getFieldtypeSize: get the size of the field type (primitive type)
2143 * offsetRoot : offset from the root
2144 * offsetParent : offset from the parrent
2146 * evD : event data, it may be NULL
2148 * int : size of the field
2149 ****************************************************************************/
2151 static inline gint
getFieldtypeSize(LttTracefile
*tf
,
2152 LttEventType
* evT
, gint offsetRoot
,
2153 gint offsetParent
, LttField
* fld
, void *evD
)
2155 gint size
, size1
, element_number
, i
, offset1
, offset2
;
2156 LttType
* type
= fld
->field_type
;
2158 /* This likely has been tested with gcov : half of them.. */
2159 if(unlikely(fld
->field_fixed
== 1)){
2161 if(unlikely(fld
== evT
->root_field
)) {
2162 size
= fld
->field_size
;
2163 goto end_getFieldtypeSize
;
2167 /* From gcov profiling : half string, half struct, can we gain something
2168 * from that ? (Mathieu) */
2169 switch(type
->type_class
) {
2171 element_number
= (int) type
->element_number
;
2172 if(fld
->field_fixed
== -1){
2173 size
= getFieldtypeSize(tf
, evT
, offsetRoot
,
2174 0,fld
->child
[0], NULL
);
2175 if(size
== 0){ //has string or sequence
2176 fld
->field_fixed
= 0;
2178 fld
->field_fixed
= 1;
2179 size
*= element_number
;
2181 }else if(fld
->field_fixed
== 0){// has string or sequence
2183 for(i
=0;i
<element_number
;i
++){
2184 size
+= getFieldtypeSize(tf
, evT
, offsetRoot
+size
,size
,
2185 fld
->child
[0], evD
+size
);
2187 }else size
= fld
->field_size
;
2189 fld
->fixed_root
= (offsetRoot
==-1) ? 0 : 1;
2190 fld
->fixed_parent
= (offsetParent
==-1) ? 0 : 1;
2196 size1
= (int) ltt_type_size(fac
, type
);
2197 if(fld
->field_fixed
== -1){
2198 fld
->sequ_number_size
= size1
;
2199 fld
->field_fixed
= 0;
2200 size
= getFieldtypeSize(evT
, offsetRoot
,
2201 0,fld
->child
[0], NULL
);
2202 fld
->element_size
= size
;
2204 element_number
= getIntNumber(tf
,size1
,evD
);
2205 type
->element_number
= element_number
;
2206 if(fld
->element_size
> 0){
2207 size
= element_number
* fld
->element_size
;
2208 }else{//sequence has string or sequence
2210 for(i
=0;i
<element_number
;i
++){
2211 size
+= getFieldtypeSize(tf
, evT
,
2212 offsetRoot
+size
+size1
,size
+size1
,
2213 fld
->child
[0], evD
+size
+size1
);
2219 fld
->fixed_root
= (offsetRoot
==-1) ? 0 : 1;
2220 fld
->fixed_parent
= (offsetParent
==-1) ? 0 : 1;
2227 if(fld
->field_fixed
== -1){
2228 fld
->field_fixed
= 0;
2230 /* Hope my implementation is faster than strlen (Mathieu) */
2231 char *ptr
=(char*)evD
;
2233 /* from gcov : many many strings are empty, make it the common case.*/
2234 while(unlikely(*ptr
!= '\0')) { size
++; ptr
++; }
2235 //size = ptr - (char*)evD + 1; //include end : '\0'
2237 fld
->fixed_root
= (offsetRoot
==-1) ? 0 : 1;
2238 fld
->fixed_parent
= (offsetParent
==-1) ? 0 : 1;
2243 element_number
= (int) type
->element_number
;
2245 /* tested with gcov */
2246 if(unlikely(fld
->field_fixed
== -1)){
2247 offset1
= offsetRoot
;
2249 for(i
=0;i
<element_number
;i
++){
2250 size1
=getFieldtypeSize(tf
, evT
,offset1
,offset2
,
2251 fld
->child
[i
], NULL
);
2252 if(likely(size1
> 0 && size
>= 0)){
2254 if(likely(offset1
>= 0)) offset1
+= size1
;
2262 if(unlikely(size
== -1)){
2263 fld
->field_fixed
= 0;
2265 }else fld
->field_fixed
= 1;
2266 }else if(likely(fld
->field_fixed
== 0)){
2267 offset1
= offsetRoot
;
2269 for(i
=0;unlikely(i
<element_number
);i
++){
2270 size
=getFieldtypeSize(tf
, evT
, offset1
, offset2
,
2271 fld
->child
[i
], evD
+offset2
);
2276 }else size
= fld
->field_size
;
2277 fld
->fixed_root
= (offsetRoot
==-1) ? 0 : 1;
2278 fld
->fixed_parent
= (offsetParent
==-1) ? 0 : 1;
2282 if(unlikely(fld
->field_fixed
== -1)){
2283 size
= (int) ltt_type_size(LTT_GET_BO(tf
), type
);
2284 fld
->field_fixed
= 1;
2285 }else size
= fld
->field_size
;
2287 fld
->fixed_root
= (offsetRoot
==-1) ? 0 : 1;
2288 fld
->fixed_parent
= (offsetParent
==-1) ? 0 : 1;
2293 fld
->offset_root
= offsetRoot
;
2294 fld
->offset_parent
= offsetParent
;
2295 fld
->field_size
= size
;
2297 end_getFieldtypeSize
:
2303 /*****************************************************************************
2305 * ltt_get_int : get an integer number
2307 * reverse_byte_order: must we reverse the byte order ?
2308 * size : the size of the integer
2309 * ptr : the data pointer
2311 * gint64 : a 64 bits integer
2312 ****************************************************************************/
2314 gint64
ltt_get_int(gboolean reverse_byte_order
, gint size
, void *data
)
2319 case 1: val
= *((gint8
*)data
); break;
2320 case 2: val
= ltt_get_int16(reverse_byte_order
, data
); break;
2321 case 4: val
= ltt_get_int32(reverse_byte_order
, data
); break;
2322 case 8: val
= ltt_get_int64(reverse_byte_order
, data
); break;
2323 default: val
= ltt_get_int64(reverse_byte_order
, data
);
2324 g_critical("get_int : integer size %d unknown", size
);
2331 /*****************************************************************************
2333 * ltt_get_uint : get an unsigned integer number
2335 * reverse_byte_order: must we reverse the byte order ?
2336 * size : the size of the integer
2337 * ptr : the data pointer
2339 * guint64 : a 64 bits unsigned integer
2340 ****************************************************************************/
2342 guint64
ltt_get_uint(gboolean reverse_byte_order
, gint size
, void *data
)
2347 case 1: val
= *((gint8
*)data
); break;
2348 case 2: val
= ltt_get_uint16(reverse_byte_order
, data
); break;
2349 case 4: val
= ltt_get_uint32(reverse_byte_order
, data
); break;
2350 case 8: val
= ltt_get_uint64(reverse_byte_order
, data
); break;
2351 default: val
= ltt_get_uint64(reverse_byte_order
, data
);
2352 g_critical("get_uint : unsigned integer size %d unknown",
2361 /* get the node name of the system */
2363 char * ltt_trace_system_description_node_name (LttSystemDescription
* s
)
2365 return s
->node_name
;
2369 /* get the domain name of the system */
2371 char * ltt_trace_system_description_domain_name (LttSystemDescription
* s
)
2373 return s
->domain_name
;
2377 /* get the description of the system */
2379 char * ltt_trace_system_description_description (LttSystemDescription
* s
)
2381 return s
->description
;
2385 /* get the start time of the trace */
2387 LttTime
ltt_trace_system_description_trace_start_time(LttSystemDescription
*s
)
2389 return s
->trace_start
;
2393 LttTracefile
*ltt_tracefile_new()
2395 return g_new(LttTracefile
, 1);
2398 void ltt_tracefile_destroy(LttTracefile
*tf
)
2403 void ltt_tracefile_copy(LttTracefile
*dest
, const LttTracefile
*src
)
2408 /* Before library loading... */
2410 static void __attribute__((constructor
)) init(void)
2412 LTT_FACILITY_NAME_HEARTBEAT
= g_quark_from_string("heartbeat");
2413 LTT_EVENT_NAME_HEARTBEAT
= g_quark_from_string("heartbeat");
2415 LTT_TRACEFILE_NAME_FACILITIES
= g_quark_from_string("/control/facilities");