put start_time in nanosec because do_gettimeofday in kernel
[lttv.git] / ltt / branches / poly / ltt / tracefile.c
1 /* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2005 Mathieu Desnoyers
3 *
4 * Complete rewrite from the original version made by XangXiu Yang.
5 *
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;
9 *
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.
14 *
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,
18 * MA 02111-1307, USA.
19 */
20
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 #include <stdio.h>
26 #include <fcntl.h>
27 #include <string.h>
28 #include <dirent.h>
29 #include <sys/stat.h>
30 #include <sys/types.h>
31 #include <errno.h>
32 #include <unistd.h>
33 #include <math.h>
34 #include <glib.h>
35 #include <malloc.h>
36 #include <sys/mman.h>
37
38 // For realpath
39 #include <limits.h>
40 #include <stdlib.h>
41
42
43 #include "parser.h"
44 #include <ltt/ltt.h>
45 #include "ltt-private.h"
46 #include <ltt/trace.h>
47 #include <ltt/facility.h>
48 #include <ltt/event.h>
49 #include <ltt/type.h>
50 #include <ltt/ltt-types.h>
51
52
53 /* Facility names used in this file */
54
55 GQuark LTT_FACILITY_NAME_HEARTBEAT,
56 LTT_EVENT_NAME_HEARTBEAT;
57 GQuark LTT_TRACEFILE_NAME_FACILITIES;
58
59 #ifndef g_open
60 #define g_open open
61 #endif
62
63
64 #define __UNUSED__ __attribute__((__unused__))
65
66 #define g_info(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, format)
67
68 #ifndef g_debug
69 #define g_debug(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format)
70 #endif
71
72 #define g_close close
73
74 /* Those macros must be called from within a function where page_size is a known
75 * variable */
76 #define PAGE_MASK (~(page_size-1))
77 #define PAGE_ALIGN(addr) (((addr)+page_size-1)&PAGE_MASK)
78
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);
83
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,
90 LttField *field);
91
92 /* map a fixed size or a block information from the file (fd) */
93 static gint map_block(LttTracefile * tf, guint block_num);
94
95 /* calculate nsec per cycles for current block */
96 #if 0
97 static guint32 calc_nsecs_per_cycle(LttTracefile * t);
98 static guint64 cycles_2_ns(LttTracefile *tf, guint64 cycles);
99 #endif //0
100
101 /* go to the next event */
102 static int ltt_seek_next_event(LttTracefile *tf);
103
104 void ltt_update_event_size(LttTracefile *tf);
105
106 #if 0
107 /* Functions to parse system.xml file (using glib xml parser) */
108 static void parser_start_element (GMarkupParseContext __UNUSED__ *context,
109 const gchar *element_name,
110 const gchar **attribute_names,
111 const gchar **attribute_values,
112 gpointer user_data,
113 GError **error)
114 {
115 int i=0;
116 LttSystemDescription* des = (LttSystemDescription* )user_data;
117 if(strcmp("system", element_name)){
118 *error = g_error_new(G_MARKUP_ERROR,
119 G_LOG_LEVEL_WARNING,
120 "This is not system.xml file");
121 return;
122 }
123
124 while(attribute_names[i]){
125 if(strcmp("node_name", attribute_names[i])==0){
126 des->node_name = g_strdup(attribute_values[i]);
127 }else if(strcmp("domainname", attribute_names[i])==0){
128 des->domain_name = g_strdup(attribute_values[i]);
129 }else if(strcmp("cpu", attribute_names[i])==0){
130 des->nb_cpu = atoi(attribute_values[i]);
131 }else if(strcmp("arch_size", attribute_names[i])==0){
132 if(strcmp(attribute_values[i],"LP32") == 0) des->size = LTT_LP32;
133 else if(strcmp(attribute_values[i],"ILP32") == 0) des->size = LTT_ILP32;
134 else if(strcmp(attribute_values[i],"LP64") == 0) des->size = LTT_LP64;
135 else if(strcmp(attribute_values[i],"ILP64") == 0) des->size = LTT_ILP64;
136 else if(strcmp(attribute_values[i],"UNKNOWN") == 0) des->size = LTT_UNKNOWN;
137 }else if(strcmp("endian", attribute_names[i])==0){
138 if(strcmp(attribute_values[i],"LITTLE_ENDIAN") == 0)
139 des->endian = LTT_LITTLE_ENDIAN;
140 else if(strcmp(attribute_values[i],"BIG_ENDIAN") == 0)
141 des->endian = LTT_BIG_ENDIAN;
142 }else if(strcmp("kernel_name", attribute_names[i])==0){
143 des->kernel_name = g_strdup(attribute_values[i]);
144 }else if(strcmp("kernel_release", attribute_names[i])==0){
145 des->kernel_release = g_strdup(attribute_values[i]);
146 }else if(strcmp("kernel_version", attribute_names[i])==0){
147 des->kernel_version = g_strdup(attribute_values[i]);
148 }else if(strcmp("machine", attribute_names[i])==0){
149 des->machine = g_strdup(attribute_values[i]);
150 }else if(strcmp("processor", attribute_names[i])==0){
151 des->processor = g_strdup(attribute_values[i]);
152 }else if(strcmp("hardware_platform", attribute_names[i])==0){
153 des->hardware_platform = g_strdup(attribute_values[i]);
154 }else if(strcmp("operating_system", attribute_names[i])==0){
155 des->operating_system = g_strdup(attribute_values[i]);
156 }else if(strcmp("ltt_major_version", attribute_names[i])==0){
157 des->ltt_major_version = atoi(attribute_values[i]);
158 }else if(strcmp("ltt_minor_version", attribute_names[i])==0){
159 des->ltt_minor_version = atoi(attribute_values[i]);
160 }else if(strcmp("ltt_block_size", attribute_names[i])==0){
161 des->ltt_block_size = atoi(attribute_values[i]);
162 }else{
163 *error = g_error_new(G_MARKUP_ERROR,
164 G_LOG_LEVEL_WARNING,
165 "Not a valid attribute");
166 return;
167 }
168 i++;
169 }
170 }
171
172 static void parser_characters (GMarkupParseContext __UNUSED__ *context,
173 const gchar *text,
174 gsize __UNUSED__ text_len,
175 gpointer user_data,
176 GError __UNUSED__ **error)
177 {
178 LttSystemDescription* des = (LttSystemDescription* )user_data;
179 des->description = g_strdup(text);
180 }
181 #endif //0
182 LttFacility *ltt_trace_get_facility_by_num(LttTrace *t,
183 guint num)
184 {
185 g_assert(num < t->facilities_by_num->len);
186
187 return &g_array_index(t->facilities_by_num, LttFacility, num);
188
189 }
190
191 guint ltt_trace_get_num_cpu(LttTrace *t)
192 {
193 return t->num_cpu;
194 }
195
196
197 /* trace can be NULL
198 *
199 * Return value : 0 success, 1 bad tracefile
200 */
201 int parse_trace_header(void *header, LttTracefile *tf, LttTrace *t)
202 {
203 guint32 *magic_number = (guint32*)header;
204 struct ltt_trace_header_any *any = (struct ltt_trace_header_any *)header;
205
206 if(*magic_number == LTT_MAGIC_NUMBER)
207 tf->reverse_bo = 0;
208 else if(*magic_number == LTT_REV_MAGIC_NUMBER)
209 tf->reverse_bo = 1;
210 else /* invalid magic number, bad tracefile ! */
211 return 1;
212
213 /* Get float byte order : might be different from int byte order
214 * (or is set to 0 if the trace has no float (kernel trace)) */
215 tf->float_word_order = any->float_word_order;
216
217 if(t) {
218 t->arch_type = ltt_get_uint32(LTT_GET_BO(tf),
219 &any->arch_type);
220 t->arch_variant = ltt_get_uint32(LTT_GET_BO(tf),
221 &any->arch_variant);
222 t->arch_size = any->arch_size;
223 t->ltt_major_version = any->major_version;
224 t->ltt_minor_version = any->minor_version;
225 t->flight_recorder = any->flight_recorder;
226 t->has_heartbeat = any->has_heartbeat;
227 t->has_alignment = any->has_alignment;
228 t->has_tsc = any->has_tsc;
229 }
230
231
232 switch(any->major_version) {
233
234 case 0:
235 switch(any->minor_version) {
236 case 3:
237 {
238 tf->buffer_header_size =
239 sizeof(struct ltt_block_start_header)
240 + sizeof(struct ltt_trace_header_0_3);
241 g_warning("Unsupported trace version : %hhu.%hhu",
242 any->major_version, any->minor_version);
243 return 1;
244 }
245 break;
246 case 4:
247 {
248 struct ltt_trace_header_0_4 *vheader =
249 (struct ltt_trace_header_0_4 *)header;
250 tf->buffer_header_size =
251 sizeof(struct ltt_block_start_header)
252 + sizeof(struct ltt_trace_header_0_4);
253 if(t) {
254 t->start_freq = ltt_get_uint64(LTT_GET_BO(tf),
255 &vheader->start_freq);
256 t->start_tsc = ltt_get_uint64(LTT_GET_BO(tf),
257 &vheader->start_tsc);
258 t->start_monotonic = ltt_get_uint64(LTT_GET_BO(tf),
259 &vheader->start_monotonic);
260 t->start_time = ltt_get_time(LTT_GET_BO(tf),
261 &vheader->start_time);
262 t->start_time.tv_nsec *= 1000; /* microsec to nanosec */
263
264 t->start_time_from_tsc = ltt_time_from_uint64(
265 (double)t->start_tsc * 1000000.0 / (double)t->start_freq);
266 }
267 }
268 break;
269 default:
270 g_warning("Unsupported trace version : %hhu.%hhu",
271 any->major_version, any->minor_version);
272 return 1;
273 }
274 break;
275
276 default:
277 g_warning("Unsupported trace version : %hhu.%hhu",
278 any->major_version, any->minor_version);
279 return 1;
280 }
281
282
283 return 0;
284 }
285
286
287
288 /*****************************************************************************
289 *Function name
290 * ltt_tracefile_open : open a trace file, construct a LttTracefile
291 *Input params
292 * t : the trace containing the tracefile
293 * fileName : path name of the trace file
294 * tf : the tracefile structure
295 *Return value
296 * : 0 for success, -1 otherwise.
297 ****************************************************************************/
298
299 gint ltt_tracefile_open(LttTrace *t, gchar * fileName, LttTracefile *tf)
300 {
301 struct stat lTDFStat; /* Trace data file status */
302 struct ltt_block_start_header *header;
303 int page_size = getpagesize();
304
305 //open the file
306 tf->long_name = g_quark_from_string(fileName);
307 tf->trace = t;
308 tf->fd = open(fileName, O_RDONLY);
309 if(tf->fd < 0){
310 g_warning("Unable to open input data file %s\n", fileName);
311 goto end;
312 }
313
314 // Get the file's status
315 if(fstat(tf->fd, &lTDFStat) < 0){
316 g_warning("Unable to get the status of the input data file %s\n", fileName);
317 goto close_file;
318 }
319
320 // Is the file large enough to contain a trace
321 if(lTDFStat.st_size <
322 (off_t)(sizeof(struct ltt_block_start_header)
323 + sizeof(struct ltt_trace_header_any))){
324 g_print("The input data file %s does not contain a trace\n", fileName);
325 goto close_file;
326 }
327
328 /* Temporarily map the buffer start header to get trace information */
329 /* Multiple of pages aligned head */
330 tf->buffer.head = mmap(0,
331 PAGE_ALIGN(sizeof(struct ltt_block_start_header)
332 + sizeof(struct ltt_trace_header_any)), PROT_READ,
333 MAP_PRIVATE, tf->fd, 0);
334 if(tf->buffer.head == MAP_FAILED) {
335 perror("Error in allocating memory for buffer of tracefile");
336 goto close_file;
337 }
338 g_assert( ( (guint)tf->buffer.head&(8-1) ) == 0); // make sure it's aligned.
339
340 header = (struct ltt_block_start_header*)tf->buffer.head;
341
342 if(parse_trace_header(header->trace, tf, NULL)) {
343 g_warning("parse_trace_header error");
344 goto unmap_file;
345 }
346
347 //store the size of the file
348 tf->file_size = lTDFStat.st_size;
349 tf->buf_size = ltt_get_uint32(LTT_GET_BO(tf), &header->buf_size);
350 tf->num_blocks = tf->file_size / tf->buf_size;
351
352 if(munmap(tf->buffer.head,
353 PAGE_ALIGN(sizeof(struct ltt_block_start_header)
354 + sizeof(struct ltt_trace_header_any)))) {
355 g_warning("unmap size : %u\n",
356 PAGE_ALIGN(sizeof(struct ltt_block_start_header)
357 + sizeof(struct ltt_trace_header_any)));
358 perror("munmap error");
359 g_assert(0);
360 }
361 tf->buffer.head = NULL;
362
363 //read the first block
364 if(map_block(tf,0)) {
365 perror("Cannot map block for tracefile");
366 goto close_file;
367 }
368
369 return 0;
370
371 /* Error */
372 unmap_file:
373 if(munmap(tf->buffer.head,
374 PAGE_ALIGN(sizeof(struct ltt_block_start_header)
375 + sizeof(struct ltt_trace_header_any)))) {
376 g_warning("unmap size : %u\n",
377 PAGE_ALIGN(sizeof(struct ltt_block_start_header)
378 + sizeof(struct ltt_trace_header_any)));
379 perror("munmap error");
380 g_assert(0);
381 }
382 close_file:
383 close(tf->fd);
384 end:
385 return -1;
386 }
387
388 LttTrace *ltt_tracefile_get_trace(LttTracefile *tf)
389 {
390 return tf->trace;
391 }
392
393 #if 0
394 /*****************************************************************************
395 *Open control and per cpu tracefiles
396 ****************************************************************************/
397
398 void ltt_tracefile_open_cpu(LttTrace *t, gchar * tracefile_name)
399 {
400 LttTracefile * tf;
401 tf = ltt_tracefile_open(t,tracefile_name);
402 if(!tf) return;
403 t->per_cpu_tracefile_number++;
404 g_ptr_array_add(t->per_cpu_tracefiles, tf);
405 }
406
407 gint ltt_tracefile_open_control(LttTrace *t, gchar * control_name)
408 {
409 LttTracefile * tf;
410 LttEvent ev;
411 LttFacility * f;
412 void * pos;
413 FacilityLoad fLoad;
414 unsigned int i;
415
416 tf = ltt_tracefile_open(t,control_name);
417 if(!tf) {
418 g_warning("ltt_tracefile_open_control : bad file descriptor");
419 return -1;
420 }
421 t->control_tracefile_number++;
422 g_ptr_array_add(t->control_tracefiles,tf);
423
424 //parse facilities tracefile to get base_id
425 if(strcmp(&control_name[strlen(control_name)-10],"facilities") ==0){
426 while(1){
427 if(!ltt_tracefile_read(tf,&ev)) return 0; // end of file
428
429 if(ev.event_id == TRACE_FACILITY_LOAD){
430 pos = ev.data;
431 fLoad.name = (gchar*)pos;
432 fLoad.checksum = *(LttChecksum*)(pos + strlen(fLoad.name));
433 fLoad.base_code = *(guint32 *)(pos + strlen(fLoad.name) + sizeof(LttChecksum));
434
435 for(i=0;i<t->facility_number;i++){
436 f = (LttFacility*)g_ptr_array_index(t->facilities,i);
437 if(strcmp(f->name,fLoad.name)==0 && fLoad.checksum==f->checksum){
438 f->base_id = fLoad.base_code;
439 break;
440 }
441 }
442 if(i==t->facility_number) {
443 g_warning("Facility: %s, checksum: %u is not found",
444 fLoad.name,(unsigned int)fLoad.checksum);
445 return -1;
446 }
447 }else if(ev.event_id == TRACE_BLOCK_START){
448 continue;
449 }else if(ev.event_id == TRACE_BLOCK_END){
450 break;
451 }else {
452 g_warning("Not valid facilities trace file");
453 return -1;
454 }
455 }
456 }
457 return 0;
458 }
459 #endif //0
460
461 /*****************************************************************************
462 *Function name
463 * ltt_tracefile_close: close a trace file,
464 *Input params
465 * t : tracefile which will be closed
466 ****************************************************************************/
467
468 void ltt_tracefile_close(LttTracefile *t)
469 {
470 int page_size = getpagesize();
471
472 if(t->buffer.head != NULL)
473 if(munmap(t->buffer.head, PAGE_ALIGN(t->buf_size))) {
474 g_warning("unmap size : %u\n",
475 PAGE_ALIGN(t->buf_size));
476 perror("munmap error");
477 g_assert(0);
478 }
479
480 close(t->fd);
481 }
482
483
484 /*****************************************************************************
485 *Get system information
486 ****************************************************************************/
487 #if 0
488 gint getSystemInfo(LttSystemDescription* des, gchar * pathname)
489 {
490 int fd;
491 GIOChannel *iochan;
492 gchar *buf = NULL;
493 gsize length;
494
495 GMarkupParseContext * context;
496 GError * error = NULL;
497 GMarkupParser markup_parser =
498 {
499 parser_start_element,
500 NULL,
501 parser_characters,
502 NULL, /* passthrough */
503 NULL /* error */
504 };
505
506 fd = g_open(pathname, O_RDONLY, 0);
507 if(fd == -1){
508 g_warning("Can not open file : %s\n", pathname);
509 return -1;
510 }
511
512 iochan = g_io_channel_unix_new(fd);
513
514 context = g_markup_parse_context_new(&markup_parser, 0, des,NULL);
515
516 //while(fgets(buf,DIR_NAME_SIZE, fp) != NULL){
517 while(g_io_channel_read_line(iochan, &buf, &length, NULL, &error)
518 != G_IO_STATUS_EOF) {
519
520 if(error != NULL) {
521 g_warning("Can not read xml file: \n%s\n", error->message);
522 g_error_free(error);
523 }
524 if(!g_markup_parse_context_parse(context, buf, length, &error)){
525 if(error != NULL) {
526 g_warning("Can not parse xml file: \n%s\n", error->message);
527 g_error_free(error);
528 }
529 g_markup_parse_context_free(context);
530
531 g_io_channel_shutdown(iochan, FALSE, &error); /* No flush */
532 if(error != NULL) {
533 g_warning("Can not close file: \n%s\n", error->message);
534 g_error_free(error);
535 }
536
537 close(fd);
538 return -1;
539 }
540 }
541 g_markup_parse_context_free(context);
542
543 g_io_channel_shutdown(iochan, FALSE, &error); /* No flush */
544 if(error != NULL) {
545 g_warning("Can not close file: \n%s\n", error->message);
546 g_error_free(error);
547 }
548
549 g_close(fd);
550
551 g_free(buf);
552 return 0;
553 }
554 #endif //0
555
556 /*****************************************************************************
557 *The following functions get facility/tracefile information
558 ****************************************************************************/
559 #if 0
560 gint getFacilityInfo(LttTrace *t, gchar* eventdefs)
561 {
562 GDir * dir;
563 const gchar * name;
564 unsigned int i,j;
565 LttFacility * f;
566 LttEventType * et;
567 gchar fullname[DIR_NAME_SIZE];
568 GError * error = NULL;
569
570 dir = g_dir_open(eventdefs, 0, &error);
571
572 if(error != NULL) {
573 g_warning("Can not open directory: %s, %s\n", eventdefs, error->message);
574 g_error_free(error);
575 return -1;
576 }
577
578 while((name = g_dir_read_name(dir)) != NULL){
579 if(!g_pattern_match_simple("*.xml", name)) continue;
580 strcpy(fullname,eventdefs);
581 strcat(fullname,name);
582 ltt_facility_open(t,fullname);
583 }
584 g_dir_close(dir);
585
586 for(j=0;j<t->facility_number;j++){
587 f = (LttFacility*)g_ptr_array_index(t->facilities, j);
588 for(i=0; i<f->event_number; i++){
589 et = f->events[i];
590 setFieldsOffset(NULL, et, NULL, t);
591 }
592 }
593 return 0;
594 }
595 #endif //0
596
597 /*****************************************************************************
598 *A trace is specified as a pathname to the directory containing all the
599 *associated data (control tracefiles, per cpu tracefiles, event
600 *descriptions...).
601 *
602 *When a trace is closed, all the associated facilities, types and fields
603 *are released as well.
604 */
605
606
607 /****************************************************************************
608 * get_absolute_pathname
609 *
610 * return the unique pathname in the system
611 *
612 * MD : Fixed this function so it uses realpath, dealing well with
613 * forgotten cases (.. were not used correctly before).
614 *
615 ****************************************************************************/
616 void get_absolute_pathname(const gchar *pathname, gchar * abs_pathname)
617 {
618 abs_pathname[0] = '\0';
619
620 if ( realpath (pathname, abs_pathname) != NULL)
621 return;
622 else
623 {
624 /* error, return the original path unmodified */
625 strcpy(abs_pathname, pathname);
626 return;
627 }
628 return;
629 }
630
631 /* Search for something like : .*_.*
632 *
633 * The left side is the name, the right side is the number.
634 */
635
636 int get_tracefile_name_number(const gchar *raw_name,
637 GQuark *name,
638 guint *num)
639 {
640 guint raw_name_len = strlen(raw_name);
641 gchar char_name[PATH_MAX];
642 int i;
643 int underscore_pos;
644 long int cpu_num;
645 gchar *endptr;
646
647 for(i=raw_name_len-1;i>=0;i--) {
648 if(raw_name[i] == '_') break;
649 }
650 if(i==0) /* Either not found or name length is 0 */
651 return -1;
652 underscore_pos = i;
653
654 cpu_num = strtol(raw_name+underscore_pos+1, &endptr, 10);
655
656 if(endptr == raw_name+underscore_pos+1)
657 return -1; /* No digit */
658 if(cpu_num == LONG_MIN || cpu_num == LONG_MAX)
659 return -1; /* underflow / overflow */
660
661 strncpy(char_name, raw_name, underscore_pos);
662
663 char_name[underscore_pos] = '\0';
664
665 *name = g_quark_from_string(char_name);
666 *num = cpu_num;
667
668 return 0;
669 }
670
671
672 GData **ltt_trace_get_tracefiles_groups(LttTrace *trace)
673 {
674 return &trace->tracefiles;
675 }
676
677
678 void compute_tracefile_group(GQuark key_id,
679 GArray *group,
680 struct compute_tracefile_group_args *args)
681 {
682 int i;
683 LttTracefile *tf;
684
685 for(i=0; i<group->len; i++) {
686 tf = &g_array_index (group, LttTracefile, i);
687 if(tf->cpu_online)
688 args->func(tf, args->func_args);
689 }
690 }
691
692
693 void ltt_tracefile_group_destroy(gpointer data)
694 {
695 GArray *group = (GArray *)data;
696 int i;
697 LttTracefile *tf;
698
699 for(i=0; i<group->len; i++) {
700 tf = &g_array_index (group, LttTracefile, i);
701 if(tf->cpu_online)
702 ltt_tracefile_close(tf);
703 }
704 g_array_free(group, TRUE);
705 }
706
707 gboolean ltt_tracefile_group_has_cpu_online(gpointer data)
708 {
709 GArray *group = (GArray *)data;
710 int i;
711 LttTracefile *tf;
712
713 for(i=0; i<group->len; i++) {
714 tf = &g_array_index (group, LttTracefile, i);
715 if(tf->cpu_online) return 1;
716 }
717 return 0;
718 }
719
720
721 /* Open each tracefile under a specific directory. Put them in a
722 * GData : permits to access them using their tracefile group pathname.
723 * i.e. access control/modules tracefile group by index :
724 * "control/module".
725 *
726 * relative path is the path relative to the trace root
727 * root path is the full path
728 *
729 * A tracefile group is simply an array where all the per cpu tracefiles sits.
730 */
731
732 static int open_tracefiles(LttTrace *trace, gchar *root_path,
733 gchar *relative_path)
734 {
735 DIR *dir = opendir(root_path);
736 struct dirent *entry;
737 struct stat stat_buf;
738 int ret;
739
740 gchar path[PATH_MAX];
741 int path_len;
742 gchar *path_ptr;
743
744 int rel_path_len;
745 gchar rel_path[PATH_MAX];
746 gchar *rel_path_ptr;
747 LttTracefile tmp_tf;
748
749 if(dir == NULL) {
750 perror(root_path);
751 return ENOENT;
752 }
753
754 strncpy(path, root_path, PATH_MAX-1);
755 path_len = strlen(path);
756 path[path_len] = '/';
757 path_len++;
758 path_ptr = path + path_len;
759
760 strncpy(rel_path, relative_path, PATH_MAX-1);
761 rel_path_len = strlen(rel_path);
762 rel_path[rel_path_len] = '/';
763 rel_path_len++;
764 rel_path_ptr = rel_path + rel_path_len;
765
766 while((entry = readdir(dir)) != NULL) {
767
768 if(entry->d_name[0] == '.') continue;
769
770 strncpy(path_ptr, entry->d_name, PATH_MAX - path_len);
771 strncpy(rel_path_ptr, entry->d_name, PATH_MAX - rel_path_len);
772
773 ret = stat(path, &stat_buf);
774 if(ret == -1) {
775 perror(path);
776 continue;
777 }
778
779 g_debug("Tracefile file or directory : %s\n", path);
780
781 if(strcmp(rel_path, "/eventdefs") == 0) continue;
782
783 if(S_ISDIR(stat_buf.st_mode)) {
784
785 g_debug("Entering subdirectory...\n");
786 ret = open_tracefiles(trace, path, rel_path);
787 if(ret < 0) continue;
788 } else if(S_ISREG(stat_buf.st_mode)) {
789 GQuark name;
790 guint num;
791 GArray *group;
792
793 if(get_tracefile_name_number(rel_path, &name, &num))
794 continue; /* invalid name */
795
796 g_debug("Opening file.\n");
797 if(ltt_tracefile_open(trace, path, &tmp_tf)) {
798 g_info("Error opening tracefile %s", path);
799
800 continue; /* error opening the tracefile : bad magic number ? */
801 }
802
803 g_debug("Tracefile name is %s and number is %u",
804 g_quark_to_string(name), num);
805
806 tmp_tf.cpu_online = 1;
807 tmp_tf.cpu_num = num;
808 tmp_tf.name = name;
809
810 group = g_datalist_id_get_data(&trace->tracefiles, name);
811 if(group == NULL) {
812 /* Elements are automatically cleared when the array is allocated.
813 * It makes the cpu_online variable set to 0 : cpu offline, by default.
814 */
815 group = g_array_sized_new (FALSE, TRUE, sizeof(LttTracefile), 10);
816 g_datalist_id_set_data_full(&trace->tracefiles, name,
817 group, ltt_tracefile_group_destroy);
818 }
819
820 /* Add the per cpu tracefile to the named group */
821 unsigned int old_len = group->len;
822 if(num+1 > old_len)
823 group = g_array_set_size(group, num+1);
824 g_array_index (group, LttTracefile, num) = tmp_tf;
825
826 }
827 }
828
829 closedir(dir);
830
831 return 0;
832 }
833
834 /* ltt_get_facility_description
835 *
836 * Opens the file corresponding to the requested facility (identified by fac_id
837 * and checksum).
838 *
839 * The name searched is : %trace root%/eventdefs/facname_checksum.xml
840 *
841 * Returns 0 on success, or 1 on failure.
842 */
843
844 static int ltt_get_facility_description(LttFacility *f,
845 LttTrace *t,
846 LttTracefile *fac_tf)
847 {
848 char desc_file_name[PATH_MAX];
849 const gchar *text;
850 guint textlen;
851 gint err;
852
853 text = g_quark_to_string(t->pathname);
854 textlen = strlen(text);
855
856 if(textlen >= PATH_MAX) goto name_error;
857 strcpy(desc_file_name, text);
858
859 text = "/eventdefs/";
860 textlen+=strlen(text);
861 if(textlen >= PATH_MAX) goto name_error;
862 strcat(desc_file_name, text);
863
864 text = g_quark_to_string(f->name);
865 textlen+=strlen(text);
866 if(textlen >= PATH_MAX) goto name_error;
867 strcat(desc_file_name, text);
868 #if 0
869 text = "_";
870 textlen+=strlen(text);
871 if(textlen >= PATH_MAX) goto name_error;
872 strcat(desc_file_name, text);
873
874 err = snprintf(desc_file_name+textlen, PATH_MAX-textlen-1,
875 "%u", f->checksum);
876 if(err < 0) goto name_error;
877
878 textlen=strlen(desc_file_name);
879
880 #endif //0
881 text = ".xml";
882 textlen+=strlen(text);
883 if(textlen >= PATH_MAX) goto name_error;
884 strcat(desc_file_name, text);
885
886 err = ltt_facility_open(f, t, desc_file_name);
887 if(err) goto facility_error;
888
889 return 0;
890
891 facility_error:
892 name_error:
893 return 1;
894 }
895
896 static void ltt_fac_ids_destroy(gpointer data)
897 {
898 GArray *fac_ids = (GArray *)data;
899
900 g_array_free(fac_ids, TRUE);
901 }
902
903
904 /* Presumes the tracefile is already seeked at the beginning. It makes sense,
905 * because it must be done just after the opening */
906 static int ltt_process_facility_tracefile(LttTracefile *tf)
907 {
908 int err;
909 LttFacility *fac;
910 GArray *fac_ids;
911 guint i;
912 LttEventType *et;
913
914 while(1) {
915 err = ltt_tracefile_read_seek(tf);
916 if(err == EPERM) goto seek_error;
917 else if(err == ERANGE) break; /* End of tracefile */
918
919 err = ltt_tracefile_read_update_event(tf);
920 if(err) goto update_error;
921
922 /* We are on a facility load/or facility unload/ or heartbeat event */
923 /* The rules are :
924 * * facility 0 is hardcoded : this is the core facility. It will be shown
925 * in the facility array though, and is shown as "loaded builtin" in the
926 * trace.
927 * It contains event :
928 * 0 : facility load
929 * 1 : facility unload
930 * 2 : state dump facility load
931 * 3 : heartbeat
932 */
933 if(tf->event.facility_id != LTT_FACILITY_CORE) {
934 /* Should only contain core facility */
935 g_warning("Error in processing facility file %s, "
936 "should not contain facility id %u.", g_quark_to_string(tf->name),
937 tf->event.facility_id);
938 err = EPERM;
939 goto fac_id_error;
940 } else {
941
942 struct LttFacilityLoad *fac_load_data;
943 struct LttStateDumpFacilityLoad *fac_state_dump_load_data;
944 char *fac_name;
945
946 // FIXME align
947 switch((enum ltt_core_events)tf->event.event_id) {
948 case LTT_EVENT_FACILITY_LOAD:
949 fac_name = (char*)(tf->event.data);
950 g_debug("Doing LTT_EVENT_FACILITY_LOAD of facility %s",
951 fac_name);
952 fac_load_data =
953 (struct LttFacilityLoad *)
954 (tf->event.data + strlen(fac_name) + 1);
955 fac = &g_array_index (tf->trace->facilities_by_num, LttFacility,
956 ltt_get_uint32(LTT_GET_BO(tf), &fac_load_data->id));
957 g_assert(fac->exists == 0);
958 fac->name = g_quark_from_string(fac_name);
959 fac->checksum = ltt_get_uint32(LTT_GET_BO(tf),
960 &fac_load_data->checksum);
961 fac->id = ltt_get_uint32(LTT_GET_BO(tf), &fac_load_data->id);
962 fac->pointer_size = ltt_get_uint32(LTT_GET_BO(tf),
963 &fac_load_data->pointer_size);
964 fac->long_size = ltt_get_uint32(LTT_GET_BO(tf),
965 &fac_load_data->long_size);
966 fac->size_t_size = ltt_get_uint32(LTT_GET_BO(tf),
967 &fac_load_data->size_t_size);
968 fac->alignment = ltt_get_uint32(LTT_GET_BO(tf),
969 &fac_load_data->alignment);
970
971 if(ltt_get_facility_description(fac, tf->trace, tf))
972 continue; /* error opening description */
973
974 fac->trace = tf->trace;
975
976 /* Preset the field offsets */
977 for(i=0; i<fac->events->len; i++){
978 et = &g_array_index(fac->events, LttEventType, i);
979 set_fields_offsets(tf, et);
980 }
981
982 fac->exists = 1;
983
984 fac_ids = g_datalist_id_get_data(&tf->trace->facilities_by_name,
985 fac->name);
986 if(fac_ids == NULL) {
987 fac_ids = g_array_sized_new (FALSE, TRUE, sizeof(guint), 1);
988 g_datalist_id_set_data_full(&tf->trace->facilities_by_name,
989 fac->name,
990 fac_ids, ltt_fac_ids_destroy);
991 }
992 g_array_append_val(fac_ids, fac->id);
993
994 break;
995 case LTT_EVENT_FACILITY_UNLOAD:
996 g_debug("Doing LTT_EVENT_FACILITY_UNLOAD");
997 /* We don't care about unload : facilities ID are valid for the whole
998 * trace. They simply won't be used after the unload. */
999 break;
1000 case LTT_EVENT_STATE_DUMP_FACILITY_LOAD:
1001 fac_name = (char*)(tf->event.data);
1002 g_debug("Doing LTT_EVENT_STATE_DUMP_FACILITY_LOAD of facility %s",
1003 fac_name);
1004 fac_state_dump_load_data =
1005 (struct LttStateDumpFacilityLoad *)
1006 (tf->event.data + strlen(fac_name) + 1);
1007 fac = &g_array_index (tf->trace->facilities_by_num, LttFacility,
1008 ltt_get_uint32(LTT_GET_BO(tf), &fac_state_dump_load_data->id));
1009 g_assert(fac->exists == 0);
1010 fac->name = g_quark_from_string(fac_name);
1011 fac->checksum = ltt_get_uint32(LTT_GET_BO(tf),
1012 &fac_state_dump_load_data->checksum);
1013 fac->id = fac_state_dump_load_data->id;
1014 fac->pointer_size = ltt_get_uint32(LTT_GET_BO(tf),
1015 &fac_state_dump_load_data->pointer_size);
1016 fac->long_size = ltt_get_uint32(LTT_GET_BO(tf),
1017 &fac_state_dump_load_data->long_size);
1018 fac->size_t_size = ltt_get_uint32(LTT_GET_BO(tf),
1019 &fac_state_dump_load_data->size_t_size);
1020 fac->alignment = ltt_get_uint32(LTT_GET_BO(tf),
1021 &fac_state_dump_load_data->alignment);
1022 if(ltt_get_facility_description(fac, tf->trace, tf))
1023 continue; /* error opening description */
1024
1025 fac->trace = tf->trace;
1026
1027 /* Preset the field offsets */
1028 for(i=0; i<fac->events->len; i++){
1029 et = &g_array_index(fac->events, LttEventType, i);
1030 set_fields_offsets(tf, et);
1031 }
1032
1033 fac->exists = 1;
1034
1035 fac_ids = g_datalist_id_get_data(&tf->trace->facilities_by_name,
1036 fac->name);
1037 if(fac_ids == NULL) {
1038 fac_ids = g_array_sized_new (FALSE, TRUE, sizeof(guint), 1);
1039 g_datalist_id_set_data_full(&tf->trace->facilities_by_name,
1040 fac->name,
1041 fac_ids, ltt_fac_ids_destroy);
1042 }
1043 g_array_append_val(fac_ids, fac->id);
1044
1045 break;
1046 case LTT_EVENT_HEARTBEAT:
1047 break;
1048 default:
1049 g_warning("Error in processing facility file %s, "
1050 "unknown event id %hhu in core facility.",
1051 g_quark_to_string(tf->name),
1052 tf->event.event_id);
1053 err = EPERM;
1054 goto event_id_error;
1055 }
1056 }
1057 }
1058 return 0;
1059
1060 /* Error handling */
1061 event_id_error:
1062 fac_id_error:
1063 update_error:
1064 seek_error:
1065 g_warning("An error occured in facility tracefile parsing");
1066 return err;
1067 }
1068
1069
1070 LttTrace *ltt_trace_open(const gchar *pathname)
1071 {
1072 gchar abs_path[PATH_MAX];
1073 LttTrace * t;
1074 LttTracefile *tf;
1075 GArray *group;
1076 int i, ret;
1077 struct ltt_block_start_header *header;
1078 DIR *dir;
1079 struct dirent *entry;
1080 guint control_found = 0;
1081 guint eventdefs_found = 0;
1082 struct stat stat_buf;
1083 gchar path[PATH_MAX];
1084
1085 t = g_new(LttTrace, 1);
1086 if(!t) goto alloc_error;
1087
1088 get_absolute_pathname(pathname, abs_path);
1089 t->pathname = g_quark_from_string(abs_path);
1090
1091 g_datalist_init(&t->tracefiles);
1092
1093 /* Test to see if it looks like a trace */
1094 dir = opendir(abs_path);
1095 if(dir == NULL) {
1096 perror(abs_path);
1097 goto open_error;
1098 }
1099 while((entry = readdir(dir)) != NULL) {
1100 strcpy(path, abs_path);
1101 strcat(path, "/");
1102 strcat(path, entry->d_name);
1103 ret = stat(path, &stat_buf);
1104 if(ret == -1) {
1105 perror(path);
1106 continue;
1107 }
1108 if(S_ISDIR(stat_buf.st_mode)) {
1109 if(strcmp(entry->d_name, "control") == 0) {
1110 control_found = 1;
1111 }
1112 if(strcmp(entry->d_name, "eventdefs") == 0) {
1113 eventdefs_found = 1;
1114 }
1115 }
1116 }
1117 closedir(dir);
1118
1119 if(!control_found || !eventdefs_found) goto find_error;
1120
1121 /* Open all the tracefiles */
1122 if(open_tracefiles(t, abs_path, "")) {
1123 g_warning("Error opening tracefile %s", abs_path);
1124 goto find_error;
1125 }
1126
1127 /* Prepare the facilities containers : array and mapping */
1128 /* Array is zeroed : the "exists" field is set to false by default */
1129 t->facilities_by_num = g_array_sized_new (FALSE,
1130 TRUE, sizeof(LttFacility),
1131 NUM_FACILITIES);
1132 t->facilities_by_num = g_array_set_size(t->facilities_by_num, NUM_FACILITIES);
1133
1134 g_datalist_init(&t->facilities_by_name);
1135
1136 /* Parse each trace control/facilitiesN files : get runtime fac. info */
1137 group = g_datalist_id_get_data(&t->tracefiles, LTT_TRACEFILE_NAME_FACILITIES);
1138 if(group == NULL) {
1139 g_error("Trace %s has no facility tracefile", abs_path);
1140 g_assert(0);
1141 goto facilities_error;
1142 }
1143
1144 /* Get the trace information for the control/facility 0 tracefile */
1145 g_assert(group->len > 0);
1146 tf = &g_array_index (group, LttTracefile, 0);
1147 header = (struct ltt_block_start_header*)tf->buffer.head;
1148 g_assert(parse_trace_header(header->trace,
1149 tf, t) == 0);
1150
1151 t->num_cpu = group->len;
1152
1153 for(i=0; i<group->len; i++) {
1154 tf = &g_array_index (group, LttTracefile, i);
1155 if(ltt_process_facility_tracefile(tf))
1156 goto facilities_error;
1157 }
1158
1159 return t;
1160
1161 /* Error handling */
1162 facilities_error:
1163 g_datalist_clear(&t->facilities_by_name);
1164 g_array_free(t->facilities_by_num, TRUE);
1165 find_error:
1166 g_datalist_clear(&t->tracefiles);
1167 open_error:
1168 g_free(t);
1169 alloc_error:
1170 return NULL;
1171
1172 }
1173
1174 GQuark ltt_trace_name(const LttTrace *t)
1175 {
1176 return t->pathname;
1177 }
1178
1179
1180 /******************************************************************************
1181 * When we copy a trace, we want all the opening actions to happen again :
1182 * the trace will be reopened and totally independant from the original.
1183 * That's why we call ltt_trace_open.
1184 *****************************************************************************/
1185 LttTrace *ltt_trace_copy(LttTrace *self)
1186 {
1187 return ltt_trace_open(g_quark_to_string(self->pathname));
1188 }
1189
1190 void ltt_trace_close(LttTrace *t)
1191 {
1192 guint i;
1193 LttFacility *fac;
1194
1195 for(i=0; i<t->facilities_by_num->len; i++) {
1196 fac = &g_array_index (t->facilities_by_num, LttFacility, i);
1197 if(fac->exists)
1198 ltt_facility_close(fac);
1199 }
1200
1201 g_datalist_clear(&t->facilities_by_name);
1202 g_array_free(t->facilities_by_num, TRUE);
1203 g_datalist_clear(&t->tracefiles);
1204 g_free(t);
1205 }
1206
1207
1208 /*****************************************************************************
1209 *Get the system description of the trace
1210 ****************************************************************************/
1211
1212 LttFacility *ltt_trace_facility_by_id(LttTrace *t, guint8 id)
1213 {
1214 g_assert(id < t->facilities_by_num->len);
1215 return &g_array_index(t->facilities_by_num, LttFacility, id);
1216 }
1217
1218 /* ltt_trace_facility_get_by_name
1219 *
1220 * Returns the GArray of facility indexes. All the fac_ids that matches the
1221 * requested facility name.
1222 *
1223 * If name is not found, returns NULL.
1224 */
1225 GArray *ltt_trace_facility_get_by_name(LttTrace *t, GQuark name)
1226 {
1227 return g_datalist_id_get_data(&t->facilities_by_name, name);
1228 }
1229
1230 /*****************************************************************************
1231 * Functions to discover all the event types in the trace
1232 ****************************************************************************/
1233
1234 #if 0
1235 unsigned ltt_trace_eventtype_number(LttTrace *t)
1236 {
1237 unsigned int i;
1238 unsigned count = 0;
1239 unsigned int num = t->facility_number;
1240 LttFacility * f;
1241
1242 for(i=0;i<num;i++){
1243 f = (LttFacility*)g_ptr_array_index(t->facilities, i);
1244 count += f->event_number;
1245 }
1246 return count;
1247 }
1248 #endif //0
1249
1250 #if 0
1251 //use an iteration on all the trace facilities, and inside iteration on all the
1252 //event types in each facilities instead.
1253 LttEventType *ltt_trace_eventtype_get(LttTrace *t, unsigned evId)
1254 {
1255 LttEventType *event_type;
1256
1257 LttFacility * f;
1258 f = ltt_trace_facility_by_id(t,evId);
1259
1260 if(unlikely(!f)) event_type = NULL;
1261 else event_type = f->events[evId - f->base_id];
1262
1263 return event_type;
1264 }
1265 #endif //0
1266
1267 #if 0
1268 /*****************************************************************************
1269 * ltt_trace_find_tracefile
1270 *
1271 * Find a tracefile by name and index in the group.
1272 *
1273 * Returns a pointer to the tracefiles, else NULL.
1274 ****************************************************************************/
1275
1276 LttTracefile *ltt_trace_find_tracefile(LttTrace *t, const gchar *name)
1277 {
1278 }
1279 #endif //0
1280
1281 /*****************************************************************************
1282 * Get the start time and end time of the trace
1283 ****************************************************************************/
1284
1285 static void ltt_tracefile_time_span_get(LttTracefile *tf,
1286 LttTime *start, LttTime *end)
1287 {
1288 int err;
1289
1290 err = map_block(tf, 0);
1291 if(unlikely(err)) {
1292 g_error("Can not map block");
1293 *start = ltt_time_infinite;
1294 } else
1295 *start = tf->buffer.begin.timestamp;
1296
1297 err = map_block(tf, tf->num_blocks - 1); /* Last block */
1298 if(unlikely(err)) {
1299 g_error("Can not map block");
1300 *end = ltt_time_zero;
1301 } else
1302 *end = tf->buffer.end.timestamp;
1303 }
1304
1305 struct tracefile_time_span_get_args {
1306 LttTrace *t;
1307 LttTime *start;
1308 LttTime *end;
1309 };
1310
1311 static void group_time_span_get(GQuark name, gpointer data, gpointer user_data)
1312 {
1313 struct tracefile_time_span_get_args *args =
1314 (struct tracefile_time_span_get_args*)user_data;
1315
1316 GArray *group = (GArray *)data;
1317 int i;
1318 LttTracefile *tf;
1319 LttTime tmp_start;
1320 LttTime tmp_end;
1321
1322 for(i=0; i<group->len; i++) {
1323 tf = &g_array_index (group, LttTracefile, i);
1324 if(tf->cpu_online) {
1325 ltt_tracefile_time_span_get(tf, &tmp_start, &tmp_end);
1326 if(ltt_time_compare(*args->start, tmp_start)>0) *args->start = tmp_start;
1327 if(ltt_time_compare(*args->end, tmp_end)<0) *args->end = tmp_end;
1328 }
1329 }
1330 }
1331
1332 void ltt_trace_time_span_get(LttTrace *t, LttTime *start, LttTime *end)
1333 {
1334 LttTime min_start = ltt_time_infinite;
1335 LttTime max_end = ltt_time_zero;
1336 struct tracefile_time_span_get_args args = { t, &min_start, &max_end };
1337
1338 g_datalist_foreach(&t->tracefiles, &group_time_span_get, &args);
1339
1340 if(start != NULL) *start = min_start;
1341 if(end != NULL) *end = max_end;
1342
1343 }
1344
1345
1346 /*****************************************************************************
1347 *Get the name of a tracefile
1348 ****************************************************************************/
1349
1350 GQuark ltt_tracefile_name(const LttTracefile *tf)
1351 {
1352 return tf->name;
1353 }
1354
1355 GQuark ltt_tracefile_long_name(const LttTracefile *tf)
1356 {
1357 return tf->long_name;
1358 }
1359
1360
1361
1362 guint ltt_tracefile_num(LttTracefile *tf)
1363 {
1364 return tf->cpu_num;
1365 }
1366
1367 /*****************************************************************************
1368 * Get the number of blocks in the tracefile
1369 ****************************************************************************/
1370
1371 guint ltt_tracefile_block_number(LttTracefile *tf)
1372 {
1373 return tf->num_blocks;
1374 }
1375
1376
1377 /* Seek to the first event in a tracefile that has a time equal or greater than
1378 * the time passed in parameter.
1379 *
1380 * If the time parameter is outside the tracefile time span, seek to the first
1381 * event or if after, return ERANGE.
1382 *
1383 * If the time parameter is before the first event, we have to seek specially to
1384 * there.
1385 *
1386 * If the time is after the end of the trace, return ERANGE.
1387 *
1388 * Do a binary search to find the right block, then a sequential search in the
1389 * block to find the event.
1390 *
1391 * In the special case where the time requested fits inside a block that has no
1392 * event corresponding to the requested time, the first event of the next block
1393 * will be seeked.
1394 *
1395 * IMPORTANT NOTE : // FIXME everywhere...
1396 *
1397 * You MUST NOT do a ltt_tracefile_read right after a ltt_tracefile_seek_time :
1398 * you will jump over an event if you do.
1399 *
1400 * Return value : 0 : no error, the tf->event can be used
1401 * ERANGE : time if after the last event of the trace
1402 * otherwise : this is an error.
1403 *
1404 * */
1405
1406 int ltt_tracefile_seek_time(LttTracefile *tf, LttTime time)
1407 {
1408 int ret = 0;
1409 int err;
1410 unsigned int block_num, high, low;
1411
1412 /* seek at the beginning of trace */
1413 err = map_block(tf, 0); /* First block */
1414 if(unlikely(err)) {
1415 g_error("Can not map block");
1416 goto fail;
1417 }
1418
1419 /* If the time is lower or equal the beginning of the trace,
1420 * go to the first event. */
1421 if(ltt_time_compare(time, tf->buffer.begin.timestamp) <= 0) {
1422 ret = ltt_tracefile_read(tf);
1423 if(ret == ERANGE) goto range;
1424 else if (ret) goto fail;
1425 goto found; /* There is either no event in the trace or the event points
1426 to the first event in the trace */
1427 }
1428
1429 err = map_block(tf, tf->num_blocks - 1); /* Last block */
1430 if(unlikely(err)) {
1431 g_error("Can not map block");
1432 goto fail;
1433 }
1434
1435 /* If the time is after the end of the trace, return ERANGE. */
1436 if(ltt_time_compare(time, tf->buffer.end.timestamp) > 0) {
1437 goto range;
1438 }
1439
1440 /* Binary search the block */
1441 high = tf->num_blocks - 1;
1442 low = 0;
1443
1444 while(1) {
1445 block_num = ((high-low) / 2) + low;
1446
1447 err = map_block(tf, block_num);
1448 if(unlikely(err)) {
1449 g_error("Can not map block");
1450 goto fail;
1451 }
1452 if(high == low) {
1453 /* We cannot divide anymore : this is what would happen if the time
1454 * requested was exactly between two consecutive buffers'end and start
1455 * timestamps. This is also what would happend if we didn't deal with out
1456 * of span cases prior in this function. */
1457 /* The event is right in the buffer!
1458 * (or in the next buffer first event) */
1459 while(1) {
1460 ret = ltt_tracefile_read(tf);
1461 if(ret == ERANGE) goto range; /* ERANGE or EPERM */
1462 else if(ret) goto fail;
1463
1464 if(ltt_time_compare(time, tf->event.event_time) <= 0)
1465 goto found;
1466 }
1467
1468 } else if(ltt_time_compare(time, tf->buffer.begin.timestamp) < 0) {
1469 /* go to lower part */
1470 high = block_num - 1;
1471 } else if(ltt_time_compare(time, tf->buffer.end.timestamp) > 0) {
1472 /* go to higher part */
1473 low = block_num + 1;
1474 } else {/* The event is right in the buffer!
1475 (or in the next buffer first event) */
1476 while(1) {
1477 ret = ltt_tracefile_read(tf);
1478 if(ret == ERANGE) goto range; /* ERANGE or EPERM */
1479 else if(ret) goto fail;
1480
1481 if(ltt_time_compare(time, tf->event.event_time) <= 0)
1482 break;
1483 }
1484 goto found;
1485 }
1486 }
1487
1488 found:
1489 return 0;
1490 range:
1491 return ERANGE;
1492
1493 /* Error handling */
1494 fail:
1495 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1496 g_quark_to_string(tf->name));
1497 return EPERM;
1498 }
1499
1500
1501 int ltt_tracefile_seek_position(LttTracefile *tf, const LttEventPosition *ep) {
1502
1503 int err;
1504
1505 if(ep->tracefile != tf) {
1506 goto fail;
1507 }
1508
1509 err = map_block(tf, ep->block);
1510 if(unlikely(err)) {
1511 g_error("Can not map block");
1512 goto fail;
1513 }
1514
1515 tf->event.offset = ep->offset;
1516
1517 err = ltt_tracefile_read_update_event(tf);
1518 if(err) goto fail;
1519 err = ltt_tracefile_read_op(tf);
1520 if(err) goto fail;
1521
1522 return 0;
1523
1524 fail:
1525 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1526 g_quark_to_string(tf->name));
1527 return 1;
1528 }
1529
1530 /* Calculate the real event time based on the buffer boundaries */
1531 LttTime ltt_interpolate_time(LttTracefile *tf, LttEvent *event)
1532 {
1533 LttTime time;
1534
1535 g_assert(tf->trace->has_tsc);
1536
1537 // time = ltt_time_from_uint64(
1538 // cycles_2_ns(tf, (guint64)(tf->buffer.tsc - tf->buffer.begin.cycle_count)));
1539 time = ltt_time_from_uint64(
1540 (double)(tf->buffer.tsc - tf->trace->start_tsc) * 1000000.0
1541 / (double)tf->trace->start_freq);
1542 //time = ltt_time_add(tf->buffer.begin.timestamp, time);
1543 time = ltt_time_add(tf->trace->start_time_from_tsc, time);
1544
1545 return time;
1546 }
1547
1548
1549 /* Get the current event of the tracefile : valid until the next read */
1550 LttEvent *ltt_tracefile_get_event(LttTracefile *tf)
1551 {
1552 return &tf->event;
1553 }
1554
1555
1556
1557 /*****************************************************************************
1558 *Function name
1559 * ltt_tracefile_read : Read the next event in the tracefile
1560 *Input params
1561 * t : tracefile
1562 *Return value
1563 *
1564 * Returns 0 if an event can be used in tf->event.
1565 * Returns ERANGE on end of trace. The event in tf->event still can be used
1566 * (if the last block was not empty).
1567 * Returns EPERM on error.
1568 *
1569 * This function does make the tracefile event structure point to the event
1570 * currently pointed to by the tf->event.
1571 *
1572 * Note : you must call a ltt_tracefile_seek to the beginning of the trace to
1573 * reinitialize it after an error if you want results to be coherent.
1574 * It would be the case if a end of trace last buffer has no event : the end
1575 * of trace wouldn't be returned, but an error.
1576 * We make the assumption there is at least one event per buffer.
1577 ****************************************************************************/
1578
1579 int ltt_tracefile_read(LttTracefile *tf)
1580 {
1581 int err;
1582
1583 err = ltt_tracefile_read_seek(tf);
1584 if(err) return err;
1585 err = ltt_tracefile_read_update_event(tf);
1586 if(err) return err;
1587 err = ltt_tracefile_read_op(tf);
1588 if(err) return err;
1589
1590 return 0;
1591 }
1592
1593 int ltt_tracefile_read_seek(LttTracefile *tf)
1594 {
1595 int err;
1596
1597 /* Get next buffer until we finally have an event, or end of trace */
1598 while(1) {
1599 err = ltt_seek_next_event(tf);
1600 if(unlikely(err == ENOPROTOOPT)) {
1601 return EPERM;
1602 }
1603
1604 /* Are we at the end of the buffer ? */
1605 if(err == ERANGE) {
1606 if(unlikely(tf->buffer.index == tf->num_blocks-1)){ /* end of trace ? */
1607 return ERANGE;
1608 } else {
1609 /* get next block */
1610 err = map_block(tf, tf->buffer.index + 1);
1611 if(unlikely(err)) {
1612 g_error("Can not map block");
1613 return EPERM;
1614 }
1615 }
1616 } else break; /* We found an event ! */
1617 }
1618
1619 return 0;
1620 }
1621
1622
1623 /* do specific operation on events */
1624 int ltt_tracefile_read_op(LttTracefile *tf)
1625 {
1626 LttEvent *event;
1627
1628 event = &tf->event;
1629
1630 /* do event specific operation */
1631
1632 /* do something if its an heartbeat event : increment the heartbeat count */
1633 //if(event->facility_id == LTT_FACILITY_CORE)
1634 // if(event->event_id == LTT_EVENT_HEARTBEAT)
1635 // tf->cur_heart_beat_number++;
1636
1637 return 0;
1638 }
1639
1640
1641 /* same as ltt_tracefile_read, but does not seek to the next event nor call
1642 * event specific operation. */
1643 int ltt_tracefile_read_update_event(LttTracefile *tf)
1644 {
1645 void * pos;
1646 LttEvent *event;
1647
1648 event = &tf->event;
1649 pos = tf->buffer.head + event->offset;
1650
1651 /* Read event header */
1652
1653 //TODO align
1654
1655 if(tf->trace->has_tsc) {
1656 if(tf->trace->has_heartbeat) {
1657 event->time.timestamp = ltt_get_uint32(LTT_GET_BO(tf),
1658 pos);
1659 /* 32 bits -> 64 bits tsc */
1660 /* note : still works for seek and non seek cases. */
1661 if(event->time.timestamp < (0xFFFFFFFFULL&tf->buffer.tsc)) {
1662 tf->buffer.tsc = ((tf->buffer.tsc&0xFFFFFFFF00000000ULL)
1663 + 0x100000000ULL)
1664 | (guint64)event->time.timestamp;
1665 event->tsc = tf->buffer.tsc;
1666 } else {
1667 /* no overflow */
1668 tf->buffer.tsc = (tf->buffer.tsc&0xFFFFFFFF00000000ULL)
1669 | (guint64)event->time.timestamp;
1670 event->tsc = tf->buffer.tsc;
1671 }
1672 pos += sizeof(guint32);
1673 } else {
1674 event->tsc = ltt_get_uint64(LTT_GET_BO(tf), pos);
1675 tf->buffer.tsc = event->tsc;
1676 pos += sizeof(guint64);
1677 }
1678
1679 event->event_time = ltt_interpolate_time(tf, event);
1680 } else {
1681 event->time.delta.tv_sec = 0;
1682 event->time.delta.tv_nsec = ltt_get_uint32(LTT_GET_BO(tf),
1683 pos) * NSEC_PER_USEC;
1684 tf->buffer.tsc = 0;
1685 event->tsc = tf->buffer.tsc;
1686
1687 event->event_time = ltt_time_add(tf->buffer.begin.timestamp,
1688 event->time.delta);
1689 pos += sizeof(guint32);
1690 }
1691
1692 event->facility_id = *(guint8*)pos;
1693 pos += sizeof(guint8);
1694
1695 event->event_id = *(guint8*)pos;
1696 pos += sizeof(guint8);
1697
1698 event->event_size = ltt_get_uint16(LTT_GET_BO(tf), pos);
1699 pos += sizeof(guint16);
1700
1701 event->data = pos;
1702
1703 /* get the data size and update the event fields with the current
1704 * information */
1705 ltt_update_event_size(tf);
1706
1707 return 0;
1708 }
1709
1710
1711 /****************************************************************************
1712 *Function name
1713 * map_block : map a block from the file
1714 *Input Params
1715 * lttdes : ltt trace file
1716 * whichBlock : the block which will be read
1717 *return value
1718 * 0 : success
1719 * EINVAL : lseek fail
1720 * EIO : can not read from the file
1721 ****************************************************************************/
1722
1723 static gint map_block(LttTracefile * tf, guint block_num)
1724 {
1725 int page_size = getpagesize();
1726 struct ltt_block_start_header *header;
1727
1728 g_assert(block_num < tf->num_blocks);
1729
1730 if(tf->buffer.head != NULL) {
1731 if(munmap(tf->buffer.head, PAGE_ALIGN(tf->buf_size))) {
1732 g_warning("unmap size : %u\n",
1733 PAGE_ALIGN(tf->buf_size));
1734 perror("munmap error");
1735 g_assert(0);
1736 }
1737 }
1738
1739
1740 /* Multiple of pages aligned head */
1741 tf->buffer.head = mmap(0,
1742 PAGE_ALIGN(tf->buf_size),
1743 PROT_READ, MAP_PRIVATE, tf->fd,
1744 PAGE_ALIGN((off_t)tf->buf_size * (off_t)block_num));
1745
1746 if(tf->buffer.head == MAP_FAILED) {
1747 perror("Error in allocating memory for buffer of tracefile");
1748 g_assert(0);
1749 goto map_error;
1750 }
1751 g_assert( ( (guint)tf->buffer.head&(8-1) ) == 0); // make sure it's aligned.
1752
1753
1754 tf->buffer.index = block_num;
1755
1756 header = (struct ltt_block_start_header*)tf->buffer.head;
1757
1758 #if 0
1759 tf->buffer.begin.timestamp = ltt_time_add(
1760 ltt_time_from_uint64(
1761 ltt_get_uint64(LTT_GET_BO(tf),
1762 &header->begin.timestamp)
1763 - tf->trace->start_monotonic),
1764 tf->trace->start_time);
1765 #endif //0
1766 //g_debug("block %u begin : %lu.%lu", block_num,
1767 // tf->buffer.begin.timestamp.tv_sec, tf->buffer.begin.timestamp.tv_nsec);
1768 tf->buffer.begin.cycle_count = ltt_get_uint64(LTT_GET_BO(tf),
1769 &header->begin.cycle_count);
1770 tf->buffer.begin.freq = ltt_get_uint64(LTT_GET_BO(tf),
1771 &header->begin.freq);
1772 tf->buffer.begin.timestamp = ltt_time_add(
1773 ltt_time_from_uint64(
1774 (double)(tf->buffer.begin.cycle_count
1775 - tf->trace->start_tsc) * 1000000.0
1776 / (double)tf->trace->start_freq),
1777 tf->trace->start_time_from_tsc);
1778 #if 0
1779
1780 tf->buffer.end.timestamp = ltt_time_add(
1781 ltt_time_from_uint64(
1782 ltt_get_uint64(LTT_GET_BO(tf),
1783 &header->end.timestamp)
1784 - tf->trace->start_monotonic),
1785 tf->trace->start_time);
1786 #endif //0
1787 //g_debug("block %u end : %lu.%lu", block_num,
1788 // tf->buffer.end.timestamp.tv_sec, tf->buffer.end.timestamp.tv_nsec);
1789 tf->buffer.end.cycle_count = ltt_get_uint64(LTT_GET_BO(tf),
1790 &header->end.cycle_count);
1791 tf->buffer.end.freq = ltt_get_uint64(LTT_GET_BO(tf),
1792 &header->end.freq);
1793 tf->buffer.lost_size = ltt_get_uint32(LTT_GET_BO(tf),
1794 &header->lost_size);
1795 tf->buffer.end.timestamp = ltt_time_add(
1796 ltt_time_from_uint64(
1797 (double)(tf->buffer.end.cycle_count
1798 - tf->trace->start_tsc) * 1000000.0
1799 / (double)tf->trace->start_freq),
1800 tf->trace->start_time_from_tsc);
1801
1802 tf->buffer.tsc = tf->buffer.begin.cycle_count;
1803 tf->event.tsc = tf->buffer.tsc;
1804 tf->buffer.freq = tf->buffer.begin.freq;
1805
1806 /* FIXME
1807 * eventually support variable buffer size : will need a partial pre-read of
1808 * the headers to create an index when we open the trace... eventually. */
1809 g_assert(tf->buf_size == ltt_get_uint32(LTT_GET_BO(tf),
1810 &header->buf_size));
1811
1812 /* Now that the buffer is mapped, calculate the time interpolation for the
1813 * block. */
1814
1815 // tf->buffer.nsecs_per_cycle = calc_nsecs_per_cycle(tf);
1816 //tf->buffer.cyc2ns_scale = calc_nsecs_per_cycle(tf);
1817
1818 /* Make the current event point to the beginning of the buffer :
1819 * it means that the event read must get the first event. */
1820 tf->event.tracefile = tf;
1821 tf->event.block = block_num;
1822 tf->event.offset = 0;
1823
1824 return 0;
1825
1826 map_error:
1827 return -errno;
1828
1829 }
1830
1831 /* It will update the fields offsets too */
1832 void ltt_update_event_size(LttTracefile *tf)
1833 {
1834 ssize_t size = 0;
1835
1836 /* Specific handling of core events : necessary to read the facility control
1837 * tracefile. */
1838 LttFacility *f = ltt_trace_get_facility_by_num(tf->trace,
1839 tf->event.facility_id);
1840
1841 if(likely(tf->event.facility_id == LTT_FACILITY_CORE)) {
1842 switch((enum ltt_core_events)tf->event.event_id) {
1843 case LTT_EVENT_FACILITY_LOAD:
1844 size = strlen((char*)tf->event.data) + 1;
1845 //g_debug("Update Event facility load of facility %s", (char*)tf->event.data);
1846 size += sizeof(struct LttFacilityLoad);
1847 break;
1848 case LTT_EVENT_FACILITY_UNLOAD:
1849 //g_debug("Update Event facility unload");
1850 size = sizeof(struct LttFacilityUnload);
1851 break;
1852 case LTT_EVENT_STATE_DUMP_FACILITY_LOAD:
1853 size = strlen((char*)tf->event.data) + 1;
1854 //g_debug("Update Event facility load state dump of facility %s",
1855 // (char*)tf->event.data);
1856 size += sizeof(struct LttStateDumpFacilityLoad);
1857 break;
1858 case LTT_EVENT_HEARTBEAT:
1859 //g_debug("Update Event heartbeat");
1860 size = sizeof(TimeHeartbeat);
1861 break;
1862 default:
1863 g_warning("Error in getting event size : tracefile %s, "
1864 "unknown event id %hhu in core facility.",
1865 g_quark_to_string(tf->name),
1866 tf->event.event_id);
1867 goto event_id_error;
1868
1869 }
1870 } else {
1871 if(!f->exists) {
1872 g_error("Unknown facility %hhu (0x%hhx) in tracefile %s",
1873 tf->event.facility_id,
1874 tf->event.facility_id,
1875 g_quark_to_string(tf->name));
1876 goto facility_error;
1877 }
1878
1879 LttEventType *event_type =
1880 ltt_facility_eventtype_get(f, tf->event.event_id);
1881
1882 if(!event_type) {
1883 g_error("Unknown event id %hhu in facility %s in tracefile %s",
1884 tf->event.event_id,
1885 g_quark_to_string(f->name),
1886 g_quark_to_string(tf->name));
1887 goto event_type_error;
1888 }
1889
1890 if(event_type->root_field)
1891 size = get_field_type_size(tf, event_type,
1892 0, 0, event_type->root_field, tf->event.data);
1893 else
1894 size = 0;
1895
1896 //g_debug("Event root field : f.e %hhu.%hhu size %zd",
1897 // tf->event.facility_id,
1898 // tf->event.event_id, size);
1899 }
1900
1901 tf->event.data_size = size;
1902
1903 /* Check consistency between kernel and LTTV structure sizes */
1904 g_assert(tf->event.data_size == tf->event.event_size);
1905
1906 return;
1907
1908 facility_error:
1909 event_type_error:
1910 event_id_error:
1911 tf->event.data_size = 0;
1912 }
1913
1914
1915 /* Take the tf current event offset and use the event facility id and event id
1916 * to figure out where is the next event offset.
1917 *
1918 * This is an internal function not aiming at being used elsewhere : it will
1919 * not jump over the current block limits. Please consider using
1920 * ltt_tracefile_read to do this.
1921 *
1922 * Returns 0 on success
1923 * ERANGE if we are at the end of the buffer.
1924 * ENOPROTOOPT if an error occured when getting the current event size.
1925 */
1926 static int ltt_seek_next_event(LttTracefile *tf)
1927 {
1928 int ret = 0;
1929 void *pos;
1930
1931 /* seek over the buffer header if we are at the buffer start */
1932 if(tf->event.offset == 0) {
1933 tf->event.offset += tf->buffer_header_size;
1934
1935 if(tf->event.offset == tf->buf_size - tf->buffer.lost_size) {
1936 ret = ERANGE;
1937 }
1938 goto found;
1939 }
1940
1941
1942 pos = tf->event.data;
1943
1944 if(tf->event.data_size < 0) goto error;
1945
1946 pos += (size_t)tf->event.data_size;
1947
1948 tf->event.offset = pos - tf->buffer.head;
1949
1950 if(tf->event.offset == tf->buf_size - tf->buffer.lost_size) {
1951 ret = ERANGE;
1952 goto found;
1953 }
1954 g_assert(tf->event.offset < tf->buf_size - tf->buffer.lost_size);
1955
1956 found:
1957 return ret;
1958
1959 error:
1960 g_error("Error in ltt_seek_next_event for tracefile %s",
1961 g_quark_to_string(tf->name));
1962 return ENOPROTOOPT;
1963 }
1964
1965 #if 0
1966 /*****************************************************************************
1967 *Function name
1968 * calc_nsecs_per_cycle : calculate nsecs per cycle for current block
1969 *
1970 * 1.0 / (freq(khz) *1000) * 1000000000
1971 *Input Params
1972 * t : tracefile
1973 ****************************************************************************/
1974 /* from timer_tsc.c */
1975 #define CYC2NS_SCALE_FACTOR 10
1976 static guint32 calc_nsecs_per_cycle(LttTracefile * tf)
1977 {
1978 //return 1e6 / (double)tf->buffer.freq;
1979 guint32 cpu_mhz = tf->buffer.freq / 1000;
1980 guint32 cyc2ns_scale = (1000 << CYC2NS_SCALE_FACTOR)/cpu_mhz;
1981
1982 return cyc2ns_scale;
1983 // return 1e6 / (double)tf->buffer.freq;
1984 }
1985
1986 static guint64 cycles_2_ns(LttTracefile *tf, guint64 cycles)
1987 {
1988 return (cycles * tf->buffer.cyc2ns_scale) >> CYC2NS_SCALE_FACTOR;
1989 }
1990 #endif //0
1991
1992 #if 0
1993 void setFieldsOffset(LttTracefile *tf, LttEventType *evT,void *evD)
1994 {
1995 LttField * rootFld = evT->root_field;
1996 // rootFld->base_address = evD;
1997
1998 if(likely(rootFld))
1999 rootFld->field_size = getFieldtypeSize(tf, evT->facility,
2000 evT, 0,0,rootFld, evD);
2001 }
2002 #endif //0
2003
2004 /*****************************************************************************
2005 *Function name
2006 * set_fields_offsets : set the precomputable offset of the fields
2007 *Input params
2008 * tracefile : opened trace file
2009 * event_type : the event type
2010 ****************************************************************************/
2011
2012 void set_fields_offsets(LttTracefile *tf, LttEventType *event_type)
2013 {
2014 LttField *field = event_type->root_field;
2015 enum field_status fixed_root = FIELD_FIXED, fixed_parent = FIELD_FIXED;
2016
2017 if(likely(field))
2018 preset_field_type_size(tf, event_type, 0, 0,
2019 &fixed_root, &fixed_parent,
2020 field);
2021
2022 }
2023
2024
2025 /*****************************************************************************
2026 *Function name
2027 * preset_field_type_size : set the fixed sizes of the field type
2028 *Input params
2029 * tf : tracefile
2030 * event_type : event type
2031 * offset_root : offset from the root
2032 * offset_parent : offset from the parent
2033 * fixed_root : Do we know a fixed offset to the root ?
2034 * fixed_parent : Do we know a fixed offset to the parent ?
2035 * field : field
2036 ****************************************************************************/
2037 void preset_field_type_size(LttTracefile *tf, LttEventType *event_type,
2038 off_t offset_root, off_t offset_parent,
2039 enum field_status *fixed_root, enum field_status *fixed_parent,
2040 LttField *field)
2041 {
2042 enum field_status local_fixed_root, local_fixed_parent;
2043 guint i;
2044 LttType *type;
2045
2046 g_assert(field->fixed_root == FIELD_UNKNOWN);
2047 g_assert(field->fixed_parent == FIELD_UNKNOWN);
2048 g_assert(field->fixed_size == FIELD_UNKNOWN);
2049
2050 type = field->field_type;
2051
2052 field->fixed_root = *fixed_root;
2053 if(field->fixed_root == FIELD_FIXED)
2054 field->offset_root = offset_root;
2055 else
2056 field->offset_root = 0;
2057
2058 field->fixed_parent = *fixed_parent;
2059 if(field->fixed_parent == FIELD_FIXED)
2060 field->offset_parent = offset_parent;
2061 else
2062 field->offset_parent = 0;
2063
2064 size_t current_root_offset;
2065 size_t current_offset;
2066 enum field_status current_child_status, final_child_status;
2067 size_t max_size;
2068
2069 switch(type->type_class) {
2070 case LTT_INT:
2071 case LTT_UINT:
2072 case LTT_FLOAT:
2073 case LTT_ENUM:
2074 field->field_size = ltt_type_size(tf->trace, type);
2075 field->fixed_size = FIELD_FIXED;
2076 break;
2077 case LTT_POINTER:
2078 field->field_size = (off_t)event_type->facility->pointer_size;
2079 field->fixed_size = FIELD_FIXED;
2080 break;
2081 case LTT_LONG:
2082 case LTT_ULONG:
2083 field->field_size = (off_t)event_type->facility->long_size;
2084 field->fixed_size = FIELD_FIXED;
2085 break;
2086 case LTT_SIZE_T:
2087 case LTT_SSIZE_T:
2088 case LTT_OFF_T:
2089 field->field_size = (off_t)event_type->facility->size_t_size;
2090 field->fixed_size = FIELD_FIXED;
2091 break;
2092 case LTT_SEQUENCE:
2093 local_fixed_root = FIELD_VARIABLE;
2094 local_fixed_parent = FIELD_VARIABLE;
2095 preset_field_type_size(tf, event_type,
2096 0, 0,
2097 &local_fixed_root, &local_fixed_parent,
2098 field->child[0]);
2099 field->fixed_size = FIELD_VARIABLE;
2100 field->field_size = 0;
2101 *fixed_root = FIELD_VARIABLE;
2102 *fixed_parent = FIELD_VARIABLE;
2103 break;
2104 case LTT_STRING:
2105 field->fixed_size = FIELD_VARIABLE;
2106 field->field_size = 0;
2107 *fixed_root = FIELD_VARIABLE;
2108 *fixed_parent = FIELD_VARIABLE;
2109 break;
2110 case LTT_ARRAY:
2111 local_fixed_root = FIELD_VARIABLE;
2112 local_fixed_parent = FIELD_VARIABLE;
2113 preset_field_type_size(tf, event_type,
2114 0, 0,
2115 &local_fixed_root, &local_fixed_parent,
2116 field->child[0]);
2117 field->fixed_size = field->child[0]->fixed_size;
2118 if(field->fixed_size == FIELD_FIXED) {
2119 field->field_size = type->element_number * field->child[0]->field_size;
2120 } else {
2121 field->field_size = 0;
2122 *fixed_root = FIELD_VARIABLE;
2123 *fixed_parent = FIELD_VARIABLE;
2124 }
2125 break;
2126 case LTT_STRUCT:
2127 current_root_offset = field->offset_root;
2128 current_offset = 0;
2129 current_child_status = FIELD_FIXED;
2130 for(i=0;i<type->element_number;i++) {
2131 preset_field_type_size(tf, event_type,
2132 current_root_offset, current_offset,
2133 fixed_root, &current_child_status,
2134 field->child[i]);
2135 if(current_child_status == FIELD_FIXED) {
2136 current_root_offset += field->child[i]->field_size;
2137 current_offset += field->child[i]->field_size;
2138 } else {
2139 current_root_offset = 0;
2140 current_offset = 0;
2141 }
2142 }
2143 if(current_child_status != FIELD_FIXED) {
2144 *fixed_parent = current_child_status;
2145 field->field_size = 0;
2146 field->fixed_size = current_child_status;
2147 } else {
2148 field->field_size = current_offset;
2149 field->fixed_size = FIELD_FIXED;
2150 }
2151 break;
2152 case LTT_UNION:
2153 current_root_offset = field->offset_root;
2154 current_offset = 0;
2155 max_size = 0;
2156 final_child_status = FIELD_FIXED;
2157 for(i=0;i<type->element_number;i++) {
2158 enum field_status current_root_child_status = FIELD_FIXED;
2159 enum field_status current_child_status = FIELD_FIXED;
2160 preset_field_type_size(tf, event_type,
2161 current_root_offset, current_offset,
2162 &current_root_child_status, &current_child_status,
2163 field->child[i]);
2164 if(current_child_status != FIELD_FIXED)
2165 final_child_status = current_child_status;
2166 else
2167 max_size = max(max_size, field->child[i]->field_size);
2168 }
2169 if(final_child_status != FIELD_FIXED) {
2170 *fixed_root = final_child_status;
2171 *fixed_parent = final_child_status;
2172 field->field_size = 0;
2173 field->fixed_size = current_child_status;
2174 } else {
2175 field->field_size = max_size;
2176 field->fixed_size = FIELD_FIXED;
2177 }
2178 break;
2179 }
2180
2181 }
2182
2183
2184 /*****************************************************************************
2185 *Function name
2186 * check_fields_compatibility : Check for compatibility between two fields :
2187 * do they use the same inner structure ?
2188 *Input params
2189 * event_type1 : event type
2190 * event_type2 : event type
2191 * field1 : field
2192 * field2 : field
2193 *Returns : 0 if identical
2194 * 1 if not.
2195 ****************************************************************************/
2196 gint check_fields_compatibility(LttEventType *event_type1,
2197 LttEventType *event_type2,
2198 LttField *field1, LttField *field2)
2199 {
2200 guint different = 0;
2201 guint i;
2202 LttType *type1;
2203 LttType *type2;
2204
2205 if(field1 == NULL) {
2206 if(field2 == NULL) goto end;
2207 else {
2208 different = 1;
2209 goto end;
2210 }
2211 } else if(field2 == NULL) {
2212 different = 1;
2213 goto end;
2214 }
2215
2216 g_assert(field1->fixed_root != FIELD_UNKNOWN);
2217 g_assert(field2->fixed_root != FIELD_UNKNOWN);
2218 g_assert(field1->fixed_parent != FIELD_UNKNOWN);
2219 g_assert(field2->fixed_parent != FIELD_UNKNOWN);
2220 g_assert(field1->fixed_size != FIELD_UNKNOWN);
2221 g_assert(field2->fixed_size != FIELD_UNKNOWN);
2222
2223 type1 = field1->field_type;
2224 type2 = field2->field_type;
2225
2226 if(type1->type_class != type2->type_class) {
2227 different = 1;
2228 goto end;
2229 }
2230 if(type1->element_name != type2->element_name) {
2231 different = 1;
2232 goto end;
2233 }
2234
2235 switch(type1->type_class) {
2236 case LTT_INT:
2237 case LTT_UINT:
2238 case LTT_FLOAT:
2239 case LTT_POINTER:
2240 case LTT_LONG:
2241 case LTT_ULONG:
2242 case LTT_SIZE_T:
2243 case LTT_SSIZE_T:
2244 case LTT_OFF_T:
2245 if(field1->field_size != field2->field_size) {
2246 different = 1;
2247 goto end;
2248 }
2249 break;
2250 case LTT_ENUM:
2251 if(type1->element_number != type2->element_number) {
2252 different = 1;
2253 goto end;
2254 }
2255 for(i=0;i<type1->element_number;i++) {
2256 if(type1->enum_strings[i] != type2->enum_strings[i]) {
2257 different = 1;
2258 goto end;
2259 }
2260 }
2261 break;
2262 case LTT_SEQUENCE:
2263 /* Two elements : size and child */
2264 g_assert(type1->element_number != type2->element_number);
2265 for(i=0;i<type1->element_number;i++) {
2266 if(check_fields_compatibility(event_type1, event_type2,
2267 field1->child[0], field2->child[0])) {
2268 different = 1;
2269 goto end;
2270 }
2271 }
2272 break;
2273 case LTT_STRING:
2274 break;
2275 case LTT_ARRAY:
2276 if(field1->field_size != field2->field_size) {
2277 different = 1;
2278 goto end;
2279 }
2280 /* Two elements : size and child */
2281 g_assert(type1->element_number != type2->element_number);
2282 for(i=0;i<type1->element_number;i++) {
2283 if(check_fields_compatibility(event_type1, event_type2,
2284 field1->child[0], field2->child[0])) {
2285 different = 1;
2286 goto end;
2287 }
2288 }
2289 break;
2290 case LTT_STRUCT:
2291 case LTT_UNION:
2292 if(type1->element_number != type2->element_number) {
2293 different = 1;
2294 break;
2295 }
2296 for(i=0;i<type1->element_number;i++) {
2297 if(check_fields_compatibility(event_type1, event_type2,
2298 field1->child[0], field2->child[0])) {
2299 different = 1;
2300 goto end;
2301 }
2302 }
2303 break;
2304 }
2305 end:
2306 return different;
2307 }
2308
2309
2310
2311
2312 #if 0
2313 /*****************************************************************************
2314 *Function name
2315 * getFieldtypeSize: get the size of the field type (primitive type)
2316 *Input params
2317 * evT : event type
2318 * offsetRoot : offset from the root
2319 * offsetParent : offset from the parrent
2320 * fld : field
2321 * evD : event data, it may be NULL
2322 *Return value
2323 * int : size of the field
2324 ****************************************************************************/
2325
2326 static inline gint getFieldtypeSize(LttTracefile *tf,
2327 LttEventType * evT, gint offsetRoot,
2328 gint offsetParent, LttField * fld, void *evD)
2329 {
2330 gint size, size1, element_number, i, offset1, offset2;
2331 LttType * type = fld->field_type;
2332
2333 /* This likely has been tested with gcov : half of them.. */
2334 if(unlikely(fld->field_fixed == 1)){
2335 /* tested : none */
2336 if(unlikely(fld == evT->root_field)) {
2337 size = fld->field_size;
2338 goto end_getFieldtypeSize;
2339 }
2340 }
2341
2342 /* From gcov profiling : half string, half struct, can we gain something
2343 * from that ? (Mathieu) */
2344 switch(type->type_class) {
2345 case LTT_ARRAY:
2346 element_number = (int) type->element_number;
2347 if(fld->field_fixed == -1){
2348 size = getFieldtypeSize(tf, evT, offsetRoot,
2349 0,fld->child[0], NULL);
2350 if(size == 0){ //has string or sequence
2351 fld->field_fixed = 0;
2352 }else{
2353 fld->field_fixed = 1;
2354 size *= element_number;
2355 }
2356 }else if(fld->field_fixed == 0){// has string or sequence
2357 size = 0;
2358 for(i=0;i<element_number;i++){
2359 size += getFieldtypeSize(tf, evT, offsetRoot+size,size,
2360 fld->child[0], evD+size);
2361 }
2362 }else size = fld->field_size;
2363 if(unlikely(!evD)){
2364 fld->fixed_root = (offsetRoot==-1) ? 0 : 1;
2365 fld->fixed_parent = (offsetParent==-1) ? 0 : 1;
2366 }
2367
2368 break;
2369
2370 case LTT_SEQUENCE:
2371 size1 = (int) ltt_type_size(fac, type);
2372 if(fld->field_fixed == -1){
2373 fld->sequ_number_size = size1;
2374 fld->field_fixed = 0;
2375 size = getFieldtypeSize(evT, offsetRoot,
2376 0,fld->child[0], NULL);
2377 fld->element_size = size;
2378 }else{//0: sequence
2379 element_number = getIntNumber(tf,size1,evD);
2380 type->element_number = element_number;
2381 if(fld->element_size > 0){
2382 size = element_number * fld->element_size;
2383 }else{//sequence has string or sequence
2384 size = 0;
2385 for(i=0;i<element_number;i++){
2386 size += getFieldtypeSize(tf, evT,
2387 offsetRoot+size+size1,size+size1,
2388 fld->child[0], evD+size+size1);
2389 }
2390 }
2391 size += size1;
2392 }
2393 if(unlikely(!evD)){
2394 fld->fixed_root = (offsetRoot==-1) ? 0 : 1;
2395 fld->fixed_parent = (offsetParent==-1) ? 0 : 1;
2396 }
2397
2398 break;
2399
2400 case LTT_STRING:
2401 size = 0;
2402 if(fld->field_fixed == -1){
2403 fld->field_fixed = 0;
2404 }else{//0: string
2405 /* Hope my implementation is faster than strlen (Mathieu) */
2406 char *ptr=(char*)evD;
2407 size = 1;
2408 /* from gcov : many many strings are empty, make it the common case.*/
2409 while(unlikely(*ptr != '\0')) { size++; ptr++; }
2410 //size = ptr - (char*)evD + 1; //include end : '\0'
2411 }
2412 fld->fixed_root = (offsetRoot==-1) ? 0 : 1;
2413 fld->fixed_parent = (offsetParent==-1) ? 0 : 1;
2414
2415 break;
2416
2417 case LTT_STRUCT:
2418 element_number = (int) type->element_number;
2419 size = 0;
2420 /* tested with gcov */
2421 if(unlikely(fld->field_fixed == -1)){
2422 offset1 = offsetRoot;
2423 offset2 = 0;
2424 for(i=0;i<element_number;i++){
2425 size1=getFieldtypeSize(tf, evT,offset1,offset2,
2426 fld->child[i], NULL);
2427 if(likely(size1 > 0 && size >= 0)){
2428 size += size1;
2429 if(likely(offset1 >= 0)) offset1 += size1;
2430 offset2 += size1;
2431 }else{
2432 size = -1;
2433 offset1 = -1;
2434 offset2 = -1;
2435 }
2436 }
2437 if(unlikely(size == -1)){
2438 fld->field_fixed = 0;
2439 size = 0;
2440 }else fld->field_fixed = 1;
2441 }else if(likely(fld->field_fixed == 0)){
2442 offset1 = offsetRoot;
2443 offset2 = 0;
2444 for(i=0;unlikely(i<element_number);i++){
2445 size=getFieldtypeSize(tf, evT, offset1, offset2,
2446 fld->child[i], evD+offset2);
2447 offset1 += size;
2448 offset2 += size;
2449 }
2450 size = offset2;
2451 }else size = fld->field_size;
2452 fld->fixed_root = (offsetRoot==-1) ? 0 : 1;
2453 fld->fixed_parent = (offsetParent==-1) ? 0 : 1;
2454 break;
2455
2456 default:
2457 if(unlikely(fld->field_fixed == -1)){
2458 size = (int) ltt_type_size(LTT_GET_BO(tf), type);
2459 fld->field_fixed = 1;
2460 }else size = fld->field_size;
2461 if(unlikely(!evD)){
2462 fld->fixed_root = (offsetRoot==-1) ? 0 : 1;
2463 fld->fixed_parent = (offsetParent==-1) ? 0 : 1;
2464 }
2465 break;
2466 }
2467
2468 fld->offset_root = offsetRoot;
2469 fld->offset_parent = offsetParent;
2470 fld->field_size = size;
2471
2472 end_getFieldtypeSize:
2473
2474 return size;
2475 }
2476 #endif //0
2477
2478 /*****************************************************************************
2479 *Function name
2480 * ltt_get_int : get an integer number
2481 *Input params
2482 * reverse_byte_order: must we reverse the byte order ?
2483 * size : the size of the integer
2484 * ptr : the data pointer
2485 *Return value
2486 * gint64 : a 64 bits integer
2487 ****************************************************************************/
2488
2489 gint64 ltt_get_int(gboolean reverse_byte_order, gint size, void *data)
2490 {
2491 gint64 val;
2492
2493 switch(size) {
2494 case 1: val = *((gint8*)data); break;
2495 case 2: val = ltt_get_int16(reverse_byte_order, data); break;
2496 case 4: val = ltt_get_int32(reverse_byte_order, data); break;
2497 case 8: val = ltt_get_int64(reverse_byte_order, data); break;
2498 default: val = ltt_get_int64(reverse_byte_order, data);
2499 g_critical("get_int : integer size %d unknown", size);
2500 break;
2501 }
2502
2503 return val;
2504 }
2505
2506 /*****************************************************************************
2507 *Function name
2508 * ltt_get_uint : get an unsigned integer number
2509 *Input params
2510 * reverse_byte_order: must we reverse the byte order ?
2511 * size : the size of the integer
2512 * ptr : the data pointer
2513 *Return value
2514 * guint64 : a 64 bits unsigned integer
2515 ****************************************************************************/
2516
2517 guint64 ltt_get_uint(gboolean reverse_byte_order, gint size, void *data)
2518 {
2519 guint64 val;
2520
2521 switch(size) {
2522 case 1: val = *((gint8*)data); break;
2523 case 2: val = ltt_get_uint16(reverse_byte_order, data); break;
2524 case 4: val = ltt_get_uint32(reverse_byte_order, data); break;
2525 case 8: val = ltt_get_uint64(reverse_byte_order, data); break;
2526 default: val = ltt_get_uint64(reverse_byte_order, data);
2527 g_critical("get_uint : unsigned integer size %d unknown",
2528 size);
2529 break;
2530 }
2531
2532 return val;
2533 }
2534
2535
2536 /* get the node name of the system */
2537
2538 char * ltt_trace_system_description_node_name (LttSystemDescription * s)
2539 {
2540 return s->node_name;
2541 }
2542
2543
2544 /* get the domain name of the system */
2545
2546 char * ltt_trace_system_description_domain_name (LttSystemDescription * s)
2547 {
2548 return s->domain_name;
2549 }
2550
2551
2552 /* get the description of the system */
2553
2554 char * ltt_trace_system_description_description (LttSystemDescription * s)
2555 {
2556 return s->description;
2557 }
2558
2559
2560 /* get the NTP corrected start time of the trace */
2561 LttTime ltt_trace_start_time(LttTrace *t)
2562 {
2563 return t->start_time;
2564 }
2565
2566 /* get the monotonic start time of the trace */
2567 LttTime ltt_trace_start_time_monotonic(LttTrace *t)
2568 {
2569 return t->start_time_from_tsc;
2570 }
2571
2572 LttTracefile *ltt_tracefile_new()
2573 {
2574 return g_new(LttTracefile, 1);
2575 }
2576
2577 void ltt_tracefile_destroy(LttTracefile *tf)
2578 {
2579 g_free(tf);
2580 }
2581
2582 void ltt_tracefile_copy(LttTracefile *dest, const LttTracefile *src)
2583 {
2584 *dest = *src;
2585 }
2586
2587 /* Before library loading... */
2588
2589 static void __attribute__((constructor)) init(void)
2590 {
2591 LTT_FACILITY_NAME_HEARTBEAT = g_quark_from_string("heartbeat");
2592 LTT_EVENT_NAME_HEARTBEAT = g_quark_from_string("heartbeat");
2593
2594 LTT_TRACEFILE_NAME_FACILITIES = g_quark_from_string("/control/facilities");
2595 }
2596
This page took 0.080572 seconds and 5 git commands to generate.