sync frequency of all traces with the first loaded
[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 library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License Version 2.1 as published by the Free Software Foundation.
9 *
10 * This library 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 GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, 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 #include <string.h>
38
39 // For realpath
40 #include <limits.h>
41 #include <stdlib.h>
42
43
44 #include <ltt/ltt.h>
45 #include "ltt-private.h"
46 #include <ltt/trace.h>
47 #include <ltt/event.h>
48 //#include <ltt/type.h>
49 #include <ltt/ltt-types.h>
50 #include <ltt/marker.h>
51
52 /* Facility names used in this file */
53
54 GQuark LTT_FACILITY_NAME_HEARTBEAT,
55 LTT_EVENT_NAME_HEARTBEAT,
56 LTT_EVENT_NAME_HEARTBEAT_FULL;
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 LttTrace *father_trace = NULL;
80
81 /* set the offset of the fields belonging to the event,
82 need the information of the archecture */
83 //void set_fields_offsets(LttTracefile *tf, LttEventType *event_type);
84 //size_t get_fields_offsets(LttTracefile *tf, LttEventType *event_type, void *data);
85
86 /* get the size of the field type according to
87 * The facility size information. */
88 #if 0
89 static inline void preset_field_type_size(LttTracefile *tf,
90 LttEventType *event_type,
91 off_t offset_root, off_t offset_parent,
92 enum field_status *fixed_root, enum field_status *fixed_parent,
93 LttField *field);
94 #endif //0
95
96 /* map a fixed size or a block information from the file (fd) */
97 static gint map_block(LttTracefile * tf, guint block_num);
98
99 /* calculate nsec per cycles for current block */
100 #if 0
101 static guint32 calc_nsecs_per_cycle(LttTracefile * t);
102 static guint64 cycles_2_ns(LttTracefile *tf, guint64 cycles);
103 #endif //0
104
105 /* go to the next event */
106 static int ltt_seek_next_event(LttTracefile *tf);
107
108 //void ltt_update_event_size(LttTracefile *tf);
109
110 static int open_tracefiles(LttTrace *trace, gchar *root_path,
111 gchar *relative_path);
112 static int ltt_process_facility_tracefile(LttTracefile *tf);
113 static void ltt_tracefile_time_span_get(LttTracefile *tf,
114 LttTime *start, LttTime *end);
115 static void group_time_span_get(GQuark name, gpointer data, gpointer user_data);
116 static gint map_block(LttTracefile * tf, guint block_num);
117 static int ltt_seek_next_event(LttTracefile *tf);
118 static void __attribute__((constructor)) init(void);
119 static void ltt_update_event_size(LttTracefile *tf);
120
121 /* Enable event debugging */
122 static int a_event_debug = 0;
123
124 void ltt_event_debug(int state)
125 {
126 a_event_debug = state;
127 }
128
129 //void precompute_offsets(LttFacility *fac, LttEventType *event);
130
131 #if 0
132 /* Functions to parse system.xml file (using glib xml parser) */
133 static void parser_start_element (GMarkupParseContext __UNUSED__ *context,
134 const gchar *element_name,
135 const gchar **attribute_names,
136 const gchar **attribute_values,
137 gpointer user_data,
138 GError **error)
139 {
140 int i=0;
141 LttSystemDescription* des = (LttSystemDescription* )user_data;
142 if(strcmp("system", element_name)){
143 *error = g_error_new(G_MARKUP_ERROR,
144 G_LOG_LEVEL_WARNING,
145 "This is not system.xml file");
146 return;
147 }
148
149 while(attribute_names[i]){
150 if(strcmp("node_name", attribute_names[i])==0){
151 des->node_name = g_strdup(attribute_values[i]);
152 }else if(strcmp("domainname", attribute_names[i])==0){
153 des->domain_name = g_strdup(attribute_values[i]);
154 }else if(strcmp("cpu", attribute_names[i])==0){
155 des->nb_cpu = atoi(attribute_values[i]);
156 }else if(strcmp("arch_size", attribute_names[i])==0){
157 if(strcmp(attribute_values[i],"LP32") == 0) des->size = LTT_LP32;
158 else if(strcmp(attribute_values[i],"ILP32") == 0) des->size = LTT_ILP32;
159 else if(strcmp(attribute_values[i],"LP64") == 0) des->size = LTT_LP64;
160 else if(strcmp(attribute_values[i],"ILP64") == 0) des->size = LTT_ILP64;
161 else if(strcmp(attribute_values[i],"UNKNOWN") == 0) des->size = LTT_UNKNOWN;
162 }else if(strcmp("endian", attribute_names[i])==0){
163 if(strcmp(attribute_values[i],"LITTLE_ENDIAN") == 0)
164 des->endian = LTT_LITTLE_ENDIAN;
165 else if(strcmp(attribute_values[i],"BIG_ENDIAN") == 0)
166 des->endian = LTT_BIG_ENDIAN;
167 }else if(strcmp("kernel_name", attribute_names[i])==0){
168 des->kernel_name = g_strdup(attribute_values[i]);
169 }else if(strcmp("kernel_release", attribute_names[i])==0){
170 des->kernel_release = g_strdup(attribute_values[i]);
171 }else if(strcmp("kernel_version", attribute_names[i])==0){
172 des->kernel_version = g_strdup(attribute_values[i]);
173 }else if(strcmp("machine", attribute_names[i])==0){
174 des->machine = g_strdup(attribute_values[i]);
175 }else if(strcmp("processor", attribute_names[i])==0){
176 des->processor = g_strdup(attribute_values[i]);
177 }else if(strcmp("hardware_platform", attribute_names[i])==0){
178 des->hardware_platform = g_strdup(attribute_values[i]);
179 }else if(strcmp("operating_system", attribute_names[i])==0){
180 des->operating_system = g_strdup(attribute_values[i]);
181 }else if(strcmp("ltt_major_version", attribute_names[i])==0){
182 des->ltt_major_version = atoi(attribute_values[i]);
183 }else if(strcmp("ltt_minor_version", attribute_names[i])==0){
184 des->ltt_minor_version = atoi(attribute_values[i]);
185 }else if(strcmp("ltt_block_size", attribute_names[i])==0){
186 des->ltt_block_size = atoi(attribute_values[i]);
187 }else{
188 *error = g_error_new(G_MARKUP_ERROR,
189 G_LOG_LEVEL_WARNING,
190 "Not a valid attribute");
191 return;
192 }
193 i++;
194 }
195 }
196
197 static void parser_characters (GMarkupParseContext __UNUSED__ *context,
198 const gchar *text,
199 gsize __UNUSED__ text_len,
200 gpointer user_data,
201 GError __UNUSED__ **error)
202 {
203 LttSystemDescription* des = (LttSystemDescription* )user_data;
204 des->description = g_strdup(text);
205 }
206 #endif //0
207
208 #if 0
209 LttFacility *ltt_trace_get_facility_by_num(LttTrace *t,
210 guint num)
211 {
212 g_assert(num < t->facilities_by_num->len);
213
214 return &g_array_index(t->facilities_by_num, LttFacility, num);
215
216 }
217 #endif //0
218
219 guint ltt_trace_get_num_cpu(LttTrace *t)
220 {
221 return t->num_cpu;
222 }
223
224
225 /* trace can be NULL
226 *
227 * Return value : 0 success, 1 bad tracefile
228 */
229 int parse_trace_header(void *header, LttTracefile *tf, LttTrace *t)
230 {
231 guint32 *magic_number = (guint32*)header;
232 struct ltt_trace_header_any *any = (struct ltt_trace_header_any *)header;
233
234 if(*magic_number == LTT_MAGIC_NUMBER)
235 tf->reverse_bo = 0;
236 else if(*magic_number == LTT_REV_MAGIC_NUMBER)
237 tf->reverse_bo = 1;
238 else /* invalid magic number, bad tracefile ! */
239 return 1;
240
241 /* Get float byte order : might be different from int byte order
242 * (or is set to 0 if the trace has no float (kernel trace)) */
243 tf->float_word_order = any->float_word_order;
244 tf->alignment = any->alignment;
245 tf->has_heartbeat = any->has_heartbeat;
246
247 if(t) {
248 t->arch_type = ltt_get_uint32(LTT_GET_BO(tf),
249 &any->arch_type);
250 t->arch_variant = ltt_get_uint32(LTT_GET_BO(tf),
251 &any->arch_variant);
252 t->arch_size = any->arch_size;
253 t->ltt_major_version = any->major_version;
254 t->ltt_minor_version = any->minor_version;
255 t->flight_recorder = any->flight_recorder;
256 // t->compact_facilities = NULL;
257 }
258
259 switch(any->major_version) {
260
261 case 0:
262 g_warning("Unsupported trace version : %hhu.%hhu",
263 any->major_version, any->minor_version);
264 return 1;
265 break;
266 case 1:
267 switch(any->minor_version) {
268 case 0:
269 {
270 struct ltt_trace_header_1_0 *vheader =
271 (struct ltt_trace_header_1_0 *)header;
272 tf->buffer_header_size =
273 sizeof(struct ltt_block_start_header)
274 + sizeof(struct ltt_trace_header_1_0);
275 tf->tsc_lsb_truncate = vheader->tsc_lsb_truncate;
276 tf->tscbits = vheader->tscbits;
277 tf->tsc_msb_cutoff = 32 - tf->tsc_lsb_truncate - tf->tscbits;
278 tf->compact_event_bits = 32 - vheader->compact_data_shift;
279 tf->tsc_mask = ((1ULL << (tf->tscbits))-1);
280 tf->tsc_mask = tf->tsc_mask << tf->tsc_lsb_truncate;
281 tf->tsc_mask_next_bit = (1ULL<<(tf->tscbits));
282 tf->tsc_mask_next_bit = tf->tsc_mask_next_bit << tf->tsc_lsb_truncate;
283 if(t) {
284 t->start_freq = ltt_get_uint64(LTT_GET_BO(tf),
285 &vheader->start_freq);
286 t->freq_scale = ltt_get_uint32(LTT_GET_BO(tf),
287 &vheader->freq_scale);
288 if(father_trace) {
289 t->start_freq = father_trace->start_freq;
290 t->freq_scale = father_trace->freq_scale;
291 }
292 else {
293 father_trace = t;
294 }
295 t->start_tsc = ltt_get_uint64(LTT_GET_BO(tf),
296 &vheader->start_tsc);
297 t->start_monotonic = ltt_get_uint64(LTT_GET_BO(tf),
298 &vheader->start_monotonic);
299 t->start_time.tv_sec = ltt_get_uint64(LTT_GET_BO(tf),
300 &vheader->start_time_sec);
301 t->start_time.tv_nsec = ltt_get_uint64(LTT_GET_BO(tf),
302 &vheader->start_time_usec);
303 t->start_time.tv_nsec *= 1000; /* microsec to nanosec */
304
305 t->start_time_from_tsc = ltt_time_from_uint64(
306 (double)t->start_tsc
307 * (1000000000.0 / tf->trace->freq_scale)
308 / (double)t->start_freq);
309 }
310 }
311 break;
312 default:
313 g_warning("Unsupported trace version : %hhu.%hhu",
314 any->major_version, any->minor_version);
315 return 1;
316 }
317 break;
318 default:
319 g_warning("Unsupported trace version : %hhu.%hhu",
320 any->major_version, any->minor_version);
321 return 1;
322 }
323
324
325 return 0;
326 }
327
328
329
330 /*****************************************************************************
331 *Function name
332 * ltt_tracefile_open : open a trace file, construct a LttTracefile
333 *Input params
334 * t : the trace containing the tracefile
335 * fileName : path name of the trace file
336 * tf : the tracefile structure
337 *Return value
338 * : 0 for success, -1 otherwise.
339 ****************************************************************************/
340
341 gint ltt_tracefile_open(LttTrace *t, gchar * fileName, LttTracefile *tf)
342 {
343 struct stat lTDFStat; /* Trace data file status */
344 struct ltt_block_start_header *header;
345 int page_size = getpagesize();
346
347 //open the file
348 tf->long_name = g_quark_from_string(fileName);
349 tf->trace = t;
350 tf->fd = open(fileName, O_RDONLY);
351 if(tf->fd < 0){
352 g_warning("Unable to open input data file %s\n", fileName);
353 goto end;
354 }
355
356 // Get the file's status
357 if(fstat(tf->fd, &lTDFStat) < 0){
358 g_warning("Unable to get the status of the input data file %s\n", fileName);
359 goto close_file;
360 }
361
362 // Is the file large enough to contain a trace
363 if(lTDFStat.st_size <
364 (off_t)(sizeof(struct ltt_block_start_header)
365 + sizeof(struct ltt_trace_header_any))){
366 g_print("The input data file %s does not contain a trace\n", fileName);
367 goto close_file;
368 }
369
370 /* Temporarily map the buffer start header to get trace information */
371 /* Multiple of pages aligned head */
372 tf->buffer.head = mmap(0,
373 PAGE_ALIGN(sizeof(struct ltt_block_start_header)
374 + sizeof(struct ltt_trace_header_any)), PROT_READ,
375 MAP_PRIVATE, tf->fd, 0);
376 if(tf->buffer.head == MAP_FAILED) {
377 perror("Error in allocating memory for buffer of tracefile");
378 goto close_file;
379 }
380 g_assert( ( (gulong)tf->buffer.head&(8-1) ) == 0); // make sure it's aligned.
381
382 header = (struct ltt_block_start_header*)tf->buffer.head;
383
384 if(parse_trace_header(header->trace, tf, NULL)) {
385 g_warning("parse_trace_header error");
386 goto unmap_file;
387 }
388
389 //store the size of the file
390 tf->file_size = lTDFStat.st_size;
391 tf->buf_size = ltt_get_uint32(LTT_GET_BO(tf), &header->buf_size);
392 tf->num_blocks = tf->file_size / tf->buf_size;
393
394 if(munmap(tf->buffer.head,
395 PAGE_ALIGN(sizeof(struct ltt_block_start_header)
396 + sizeof(struct ltt_trace_header_any)))) {
397 g_warning("unmap size : %u\n",
398 PAGE_ALIGN(sizeof(struct ltt_block_start_header)
399 + sizeof(struct ltt_trace_header_any)));
400 perror("munmap error");
401 g_assert(0);
402 }
403 tf->buffer.head = NULL;
404
405 //read the first block
406 if(map_block(tf,0)) {
407 perror("Cannot map block for tracefile");
408 goto close_file;
409 }
410
411 return 0;
412
413 /* Error */
414 unmap_file:
415 if(munmap(tf->buffer.head,
416 PAGE_ALIGN(sizeof(struct ltt_block_start_header)
417 + sizeof(struct ltt_trace_header_any)))) {
418 g_warning("unmap size : %u\n",
419 PAGE_ALIGN(sizeof(struct ltt_block_start_header)
420 + sizeof(struct ltt_trace_header_any)));
421 perror("munmap error");
422 g_assert(0);
423 }
424 close_file:
425 close(tf->fd);
426 end:
427 return -1;
428 }
429
430 LttTrace *ltt_tracefile_get_trace(LttTracefile *tf)
431 {
432 return tf->trace;
433 }
434
435 #if 0
436 /*****************************************************************************
437 *Open control and per cpu tracefiles
438 ****************************************************************************/
439
440 void ltt_tracefile_open_cpu(LttTrace *t, gchar * tracefile_name)
441 {
442 LttTracefile * tf;
443 tf = ltt_tracefile_open(t,tracefile_name);
444 if(!tf) return;
445 t->per_cpu_tracefile_number++;
446 g_ptr_array_add(t->per_cpu_tracefiles, tf);
447 }
448
449 gint ltt_tracefile_open_control(LttTrace *t, gchar * control_name)
450 {
451 LttTracefile * tf;
452 LttEvent ev;
453 LttFacility * f;
454 void * pos;
455 FacilityLoad fLoad;
456 unsigned int i;
457
458 tf = ltt_tracefile_open(t,control_name);
459 if(!tf) {
460 g_warning("ltt_tracefile_open_control : bad file descriptor");
461 return -1;
462 }
463 t->control_tracefile_number++;
464 g_ptr_array_add(t->control_tracefiles,tf);
465
466 //parse facilities tracefile to get base_id
467 if(strcmp(&control_name[strlen(control_name)-10],"facilities") ==0){
468 while(1){
469 if(!ltt_tracefile_read(tf,&ev)) return 0; // end of file
470
471 if(ev.event_id == TRACE_FACILITY_LOAD){
472 pos = ev.data;
473 fLoad.name = (gchar*)pos;
474 fLoad.checksum = *(LttChecksum*)(pos + strlen(fLoad.name));
475 fLoad.base_code = *(guint32 *)(pos + strlen(fLoad.name) + sizeof(LttChecksum));
476
477 for(i=0;i<t->facility_number;i++){
478 f = (LttFacility*)g_ptr_array_index(t->facilities,i);
479 if(strcmp(f->name,fLoad.name)==0 && fLoad.checksum==f->checksum){
480 f->base_id = fLoad.base_code;
481 break;
482 }
483 }
484 if(i==t->facility_number) {
485 g_warning("Facility: %s, checksum: %u is not found",
486 fLoad.name,(unsigned int)fLoad.checksum);
487 return -1;
488 }
489 }else if(ev.event_id == TRACE_BLOCK_START){
490 continue;
491 }else if(ev.event_id == TRACE_BLOCK_END){
492 break;
493 }else {
494 g_warning("Not valid facilities trace file");
495 return -1;
496 }
497 }
498 }
499 return 0;
500 }
501 #endif //0
502
503 /*****************************************************************************
504 *Function name
505 * ltt_tracefile_close: close a trace file,
506 *Input params
507 * t : tracefile which will be closed
508 ****************************************************************************/
509
510 void ltt_tracefile_close(LttTracefile *t)
511 {
512 int page_size = getpagesize();
513
514 if(t->buffer.head != NULL)
515 if(munmap(t->buffer.head, PAGE_ALIGN(t->buf_size))) {
516 g_warning("unmap size : %u\n",
517 PAGE_ALIGN(t->buf_size));
518 perror("munmap error");
519 g_assert(0);
520 }
521
522 close(t->fd);
523 }
524
525
526 /*****************************************************************************
527 *Get system information
528 ****************************************************************************/
529 #if 0
530 gint getSystemInfo(LttSystemDescription* des, gchar * pathname)
531 {
532 int fd;
533 GIOChannel *iochan;
534 gchar *buf = NULL;
535 gsize length;
536
537 GMarkupParseContext * context;
538 GError * error = NULL;
539 GMarkupParser markup_parser =
540 {
541 parser_start_element,
542 NULL,
543 parser_characters,
544 NULL, /* passthrough */
545 NULL /* error */
546 };
547
548 fd = g_open(pathname, O_RDONLY, 0);
549 if(fd == -1){
550 g_warning("Can not open file : %s\n", pathname);
551 return -1;
552 }
553
554 iochan = g_io_channel_unix_new(fd);
555
556 context = g_markup_parse_context_new(&markup_parser, 0, des,NULL);
557
558 //while(fgets(buf,DIR_NAME_SIZE, fp) != NULL){
559 while(g_io_channel_read_line(iochan, &buf, &length, NULL, &error)
560 != G_IO_STATUS_EOF) {
561
562 if(error != NULL) {
563 g_warning("Can not read xml file: \n%s\n", error->message);
564 g_error_free(error);
565 }
566 if(!g_markup_parse_context_parse(context, buf, length, &error)){
567 if(error != NULL) {
568 g_warning("Can not parse xml file: \n%s\n", error->message);
569 g_error_free(error);
570 }
571 g_markup_parse_context_free(context);
572
573 g_io_channel_shutdown(iochan, FALSE, &error); /* No flush */
574 if(error != NULL) {
575 g_warning("Can not close file: \n%s\n", error->message);
576 g_error_free(error);
577 }
578
579 close(fd);
580 return -1;
581 }
582 }
583 g_markup_parse_context_free(context);
584
585 g_io_channel_shutdown(iochan, FALSE, &error); /* No flush */
586 if(error != NULL) {
587 g_warning("Can not close file: \n%s\n", error->message);
588 g_error_free(error);
589 }
590
591 g_close(fd);
592
593 g_free(buf);
594 return 0;
595 }
596 #endif //0
597
598 /*****************************************************************************
599 *The following functions get facility/tracefile information
600 ****************************************************************************/
601 #if 0
602 gint getFacilityInfo(LttTrace *t, gchar* eventdefs)
603 {
604 GDir * dir;
605 const gchar * name;
606 unsigned int i,j;
607 LttFacility * f;
608 LttEventType * et;
609 gchar fullname[DIR_NAME_SIZE];
610 GError * error = NULL;
611
612 dir = g_dir_open(eventdefs, 0, &error);
613
614 if(error != NULL) {
615 g_warning("Can not open directory: %s, %s\n", eventdefs, error->message);
616 g_error_free(error);
617 return -1;
618 }
619
620 while((name = g_dir_read_name(dir)) != NULL){
621 if(!g_pattern_match_simple("*.xml", name)) continue;
622 strcpy(fullname,eventdefs);
623 strcat(fullname,name);
624 ltt_facility_open(t,fullname);
625 }
626 g_dir_close(dir);
627
628 for(j=0;j<t->facility_number;j++){
629 f = (LttFacility*)g_ptr_array_index(t->facilities, j);
630 for(i=0; i<f->event_number; i++){
631 et = f->events[i];
632 setFieldsOffset(NULL, et, NULL, t);
633 }
634 }
635 return 0;
636 }
637 #endif //0
638
639 /*****************************************************************************
640 *A trace is specified as a pathname to the directory containing all the
641 *associated data (control tracefiles, per cpu tracefiles, event
642 *descriptions...).
643 *
644 *When a trace is closed, all the associated facilities, types and fields
645 *are released as well.
646 */
647
648
649 /****************************************************************************
650 * get_absolute_pathname
651 *
652 * return the unique pathname in the system
653 *
654 * MD : Fixed this function so it uses realpath, dealing well with
655 * forgotten cases (.. were not used correctly before).
656 *
657 ****************************************************************************/
658 void get_absolute_pathname(const gchar *pathname, gchar * abs_pathname)
659 {
660 abs_pathname[0] = '\0';
661
662 if ( realpath (pathname, abs_pathname) != NULL)
663 return;
664 else
665 {
666 /* error, return the original path unmodified */
667 strcpy(abs_pathname, pathname);
668 return;
669 }
670 return;
671 }
672
673 /* Search for something like : .*_.*
674 *
675 * The left side is the name, the right side is the number.
676 */
677
678 int get_tracefile_name_number(gchar *raw_name,
679 GQuark *name,
680 guint *num,
681 gulong *tid,
682 gulong *pgid,
683 guint64 *creation)
684 {
685 guint raw_name_len = strlen(raw_name);
686 gchar char_name[PATH_MAX];
687 int i;
688 int underscore_pos;
689 long int cpu_num;
690 gchar *endptr;
691 gchar *tmpptr;
692
693 for(i=raw_name_len-1;i>=0;i--) {
694 if(raw_name[i] == '_') break;
695 }
696 if(i==-1) { /* Either not found or name length is 0 */
697 /* This is a userspace tracefile */
698 strncpy(char_name, raw_name, raw_name_len);
699 char_name[raw_name_len] = '\0';
700 *name = g_quark_from_string(char_name);
701 *num = 0; /* unknown cpu */
702 for(i=0;i<raw_name_len;i++) {
703 if(raw_name[i] == '/') {
704 break;
705 }
706 }
707 i++;
708 for(;i<raw_name_len;i++) {
709 if(raw_name[i] == '/') {
710 break;
711 }
712 }
713 i++;
714 for(;i<raw_name_len;i++) {
715 if(raw_name[i] == '-') {
716 break;
717 }
718 }
719 if(i == raw_name_len) return -1;
720 i++;
721 tmpptr = &raw_name[i];
722 for(;i<raw_name_len;i++) {
723 if(raw_name[i] == '.') {
724 raw_name[i] = ' ';
725 break;
726 }
727 }
728 *tid = strtoul(tmpptr, &endptr, 10);
729 if(endptr == tmpptr)
730 return -1; /* No digit */
731 if(*tid == ULONG_MAX)
732 return -1; /* underflow / overflow */
733 i++;
734 tmpptr = &raw_name[i];
735 for(;i<raw_name_len;i++) {
736 if(raw_name[i] == '.') {
737 raw_name[i] = ' ';
738 break;
739 }
740 }
741 *pgid = strtoul(tmpptr, &endptr, 10);
742 if(endptr == tmpptr)
743 return -1; /* No digit */
744 if(*pgid == ULONG_MAX)
745 return -1; /* underflow / overflow */
746 i++;
747 tmpptr = &raw_name[i];
748 *creation = strtoull(tmpptr, &endptr, 10);
749 if(endptr == tmpptr)
750 return -1; /* No digit */
751 if(*creation == G_MAXUINT64)
752 return -1; /* underflow / overflow */
753 } else {
754 underscore_pos = i;
755
756 cpu_num = strtol(raw_name+underscore_pos+1, &endptr, 10);
757
758 if(endptr == raw_name+underscore_pos+1)
759 return -1; /* No digit */
760 if(cpu_num == LONG_MIN || cpu_num == LONG_MAX)
761 return -1; /* underflow / overflow */
762
763 strncpy(char_name, raw_name, underscore_pos);
764 char_name[underscore_pos] = '\0';
765
766 *name = g_quark_from_string(char_name);
767 *num = cpu_num;
768 }
769
770
771 return 0;
772 }
773
774
775 GData **ltt_trace_get_tracefiles_groups(LttTrace *trace)
776 {
777 return &trace->tracefiles;
778 }
779
780
781 void compute_tracefile_group(GQuark key_id,
782 GArray *group,
783 struct compute_tracefile_group_args *args)
784 {
785 int i;
786 LttTracefile *tf;
787
788 for(i=0; i<group->len; i++) {
789 tf = &g_array_index (group, LttTracefile, i);
790 if(tf->cpu_online)
791 args->func(tf, args->func_args);
792 }
793 }
794
795
796 void ltt_tracefile_group_destroy(gpointer data)
797 {
798 GArray *group = (GArray *)data;
799 int i;
800 LttTracefile *tf;
801
802 for(i=0; i<group->len; i++) {
803 tf = &g_array_index (group, LttTracefile, i);
804 if(tf->cpu_online)
805 ltt_tracefile_close(tf);
806 }
807 g_array_free(group, TRUE);
808 }
809
810 gboolean ltt_tracefile_group_has_cpu_online(gpointer data)
811 {
812 GArray *group = (GArray *)data;
813 int i;
814 LttTracefile *tf;
815
816 for(i=0; i<group->len; i++) {
817 tf = &g_array_index (group, LttTracefile, i);
818 if(tf->cpu_online)
819 return 1;
820 }
821 return 0;
822 }
823
824
825 /* Open each tracefile under a specific directory. Put them in a
826 * GData : permits to access them using their tracefile group pathname.
827 * i.e. access control/modules tracefile group by index :
828 * "control/module".
829 *
830 * relative path is the path relative to the trace root
831 * root path is the full path
832 *
833 * A tracefile group is simply an array where all the per cpu tracefiles sit.
834 */
835
836 int open_tracefiles(LttTrace *trace, gchar *root_path, gchar *relative_path)
837 {
838 DIR *dir = opendir(root_path);
839 struct dirent *entry;
840 struct stat stat_buf;
841 int ret;
842
843 gchar path[PATH_MAX];
844 int path_len;
845 gchar *path_ptr;
846
847 int rel_path_len;
848 gchar rel_path[PATH_MAX];
849 gchar *rel_path_ptr;
850 LttTracefile tmp_tf;
851
852 if(dir == NULL) {
853 perror(root_path);
854 return ENOENT;
855 }
856
857 strncpy(path, root_path, PATH_MAX-1);
858 path_len = strlen(path);
859 path[path_len] = '/';
860 path_len++;
861 path_ptr = path + path_len;
862
863 strncpy(rel_path, relative_path, PATH_MAX-1);
864 rel_path_len = strlen(rel_path);
865 rel_path[rel_path_len] = '/';
866 rel_path_len++;
867 rel_path_ptr = rel_path + rel_path_len;
868
869 while((entry = readdir(dir)) != NULL) {
870
871 if(entry->d_name[0] == '.') continue;
872
873 strncpy(path_ptr, entry->d_name, PATH_MAX - path_len);
874 strncpy(rel_path_ptr, entry->d_name, PATH_MAX - rel_path_len);
875
876 ret = stat(path, &stat_buf);
877 if(ret == -1) {
878 perror(path);
879 continue;
880 }
881
882 g_debug("Tracefile file or directory : %s\n", path);
883
884 // if(strcmp(rel_path, "/eventdefs") == 0) continue;
885
886 if(S_ISDIR(stat_buf.st_mode)) {
887
888 g_debug("Entering subdirectory...\n");
889 ret = open_tracefiles(trace, path, rel_path);
890 if(ret < 0) continue;
891 } else if(S_ISREG(stat_buf.st_mode)) {
892 GQuark name;
893 guint num;
894 gulong tid, pgid;
895 guint64 creation;
896 GArray *group;
897 num = 0;
898 tid = pgid = 0;
899 creation = 0;
900 if(get_tracefile_name_number(rel_path, &name, &num, &tid, &pgid, &creation))
901 continue; /* invalid name */
902
903 g_debug("Opening file.\n");
904 if(ltt_tracefile_open(trace, path, &tmp_tf)) {
905 g_info("Error opening tracefile %s", path);
906
907 continue; /* error opening the tracefile : bad magic number ? */
908 }
909
910 g_debug("Tracefile name is %s and number is %u",
911 g_quark_to_string(name), num);
912
913 tmp_tf.cpu_online = 1;
914 tmp_tf.cpu_num = num;
915 tmp_tf.name = name;
916 tmp_tf.tid = tid;
917 tmp_tf.pgid = pgid;
918 tmp_tf.creation = creation;
919 if(tmp_tf.name == g_quark_from_string("/compact")
920 || tmp_tf.name == g_quark_from_string("/flight-compact"))
921 tmp_tf.compact = 1;
922 else
923 tmp_tf.compact = 0;
924 group = g_datalist_id_get_data(&trace->tracefiles, name);
925 if(group == NULL) {
926 /* Elements are automatically cleared when the array is allocated.
927 * It makes the cpu_online variable set to 0 : cpu offline, by default.
928 */
929 group = g_array_sized_new (FALSE, TRUE, sizeof(LttTracefile), 10);
930 g_datalist_id_set_data_full(&trace->tracefiles, name,
931 group, ltt_tracefile_group_destroy);
932 }
933
934 /* Add the per cpu tracefile to the named group */
935 unsigned int old_len = group->len;
936 if(num+1 > old_len)
937 group = g_array_set_size(group, num+1);
938 g_array_index (group, LttTracefile, num) = tmp_tf;
939 g_array_index (group, LttTracefile, num).event.tracefile =
940 &g_array_index (group, LttTracefile, num);
941 }
942 }
943
944 closedir(dir);
945
946 return 0;
947 }
948
949 /* ltt_get_facility_description
950 *
951 * Opens the file corresponding to the requested facility (identified by fac_id
952 * and checksum).
953 *
954 * The name searched is : %trace root%/eventdefs/facname_checksum.xml
955 *
956 * Returns 0 on success, or 1 on failure.
957 */
958 #if 0
959 static int ltt_get_facility_description(LttFacility *f,
960 LttTrace *t,
961 LttTracefile *fac_tf)
962 {
963 char desc_file_name[PATH_MAX];
964 const gchar *text;
965 guint textlen;
966 gint err;
967 gint arch_spec;
968 gint fac_name_len;
969
970 text = g_quark_to_string(t->pathname);
971 textlen = strlen(text);
972
973 if(textlen >= PATH_MAX) goto name_error;
974 strcpy(desc_file_name, text);
975
976 text = "/eventdefs/";
977 textlen+=strlen(text);
978 if(textlen >= PATH_MAX) goto name_error;
979 strcat(desc_file_name, text);
980
981 text = g_quark_to_string(f->name);
982 fac_name_len = strlen(text);
983 textlen+=fac_name_len;
984 if(textlen >= PATH_MAX) goto name_error;
985 strcat(desc_file_name, text);
986
987 /* arch specific facilities are named like this : name_arch */
988 if(fac_name_len+1 < sizeof("_arch"))
989 arch_spec = 0;
990 else {
991 if(!strcmp(&text[fac_name_len+1-sizeof("_arch")], "_arch"))
992 arch_spec = 1;
993 else
994 arch_spec = 0;
995 }
996
997 #if 0
998 text = "_";
999 textlen+=strlen(text);
1000 if(textlen >= PATH_MAX) goto name_error;
1001 strcat(desc_file_name, text);
1002
1003 err = snprintf(desc_file_name+textlen, PATH_MAX-textlen-1,
1004 "%u", f->checksum);
1005 if(err < 0) goto name_error;
1006
1007 textlen=strlen(desc_file_name);
1008
1009 #endif //0
1010
1011 if(arch_spec) {
1012 switch(t->arch_type) {
1013 case LTT_ARCH_TYPE_I386:
1014 text = "_i386";
1015 break;
1016 case LTT_ARCH_TYPE_PPC:
1017 text = "_ppc";
1018 break;
1019 case LTT_ARCH_TYPE_SH:
1020 text = "_sh";
1021 break;
1022 case LTT_ARCH_TYPE_S390:
1023 text = "_s390";
1024 break;
1025 case LTT_ARCH_TYPE_MIPS:
1026 text = "_mips";
1027 break;
1028 case LTT_ARCH_TYPE_ARM:
1029 text = "_arm";
1030 break;
1031 case LTT_ARCH_TYPE_PPC64:
1032 text = "_ppc64";
1033 break;
1034 case LTT_ARCH_TYPE_X86_64:
1035 text = "_x86_64";
1036 break;
1037 case LTT_ARCH_TYPE_C2:
1038 text = "_c2";
1039 break;
1040 case LTT_ARCH_TYPE_POWERPC:
1041 text = "_powerpc";
1042 break;
1043 default:
1044 g_error("Trace from unsupported architecture.");
1045 }
1046 textlen+=strlen(text);
1047 if(textlen >= PATH_MAX) goto name_error;
1048 strcat(desc_file_name, text);
1049 }
1050
1051 text = ".xml";
1052 textlen+=strlen(text);
1053 if(textlen >= PATH_MAX) goto name_error;
1054 strcat(desc_file_name, text);
1055
1056 err = ltt_facility_open(f, t, desc_file_name);
1057 if(err) goto facility_error;
1058
1059 return 0;
1060
1061 facility_error:
1062 name_error:
1063 return 1;
1064 }
1065
1066 static void ltt_fac_ids_destroy(gpointer data)
1067 {
1068 GArray *fac_ids = (GArray *)data;
1069
1070 g_array_free(fac_ids, TRUE);
1071 }
1072 #endif //0
1073
1074 /* Presumes the tracefile is already seeked at the beginning. It makes sense,
1075 * because it must be done just after the opening */
1076 int ltt_process_facility_tracefile(LttTracefile *tf)
1077 {
1078 int err;
1079 //LttFacility *fac;
1080 //GArray *fac_ids;
1081 guint i;
1082 //LttEventType *et;
1083
1084 while(1) {
1085 err = ltt_tracefile_read_seek(tf);
1086 if(err == EPERM) goto seek_error;
1087 else if(err == ERANGE) break; /* End of tracefile */
1088
1089 err = ltt_tracefile_read_update_event(tf);
1090 if(err) goto update_error;
1091
1092 /* We are on a facility load/or facility unload/ or heartbeat event */
1093 /* The rules are :
1094 * * facility 0 is hardcoded : this is the core facility. It will be shown
1095 * in the facility array though, and is shown as "loaded builtin" in the
1096 * trace.
1097 * It contains event :
1098 * 0 : facility load
1099 * 1 : facility unload
1100 * 2 : state dump facility load
1101 * 3 : heartbeat
1102 */
1103 if(tf->event.event_id >= MARKER_CORE_IDS) {
1104 /* Should only contain core facility */
1105 g_warning("Error in processing facility file %s, "
1106 "should not contain event id %u.", g_quark_to_string(tf->name),
1107 tf->event.event_id);
1108 err = EPERM;
1109 goto event_id_error;
1110 } else {
1111
1112 char *pos;
1113 const char *marker_name, *format;
1114 uint16_t id;
1115 guint8 int_size, long_size, pointer_size, size_t_size, alignment;
1116
1117 // FIXME align
1118 switch((enum marker_id)tf->event.event_id) {
1119 case MARKER_ID_SET_MARKER_ID:
1120 marker_name = pos = tf->event.data;
1121 g_debug("Doing MARKER_ID_SET_MARKER_ID of marker %s", marker_name);
1122 pos += strlen(marker_name) + 1;
1123 //remove genevent compatibility
1124 //pos += ltt_align((size_t)pos, tf->trace->arch_size, tf->alignment);
1125 pos += ltt_align((size_t)pos, sizeof(uint16_t), tf->alignment);
1126 id = ltt_get_uint16(LTT_GET_BO(tf), pos);
1127 g_debug("In MARKER_ID_SET_MARKER_ID of marker %s id %hu",
1128 marker_name, id);
1129 pos += sizeof(guint16);
1130 int_size = *(guint8*)pos;
1131 pos += sizeof(guint8);
1132 long_size = *(guint8*)pos;
1133 pos += sizeof(guint8);
1134 pointer_size = *(guint8*)pos;
1135 pos += sizeof(guint8);
1136 size_t_size = *(guint8*)pos;
1137 pos += sizeof(guint8);
1138 alignment = *(guint8*)pos;
1139 pos += sizeof(guint8);
1140 marker_id_event(tf->trace, g_quark_from_string(marker_name),
1141 id, int_size, long_size,
1142 pointer_size, size_t_size, alignment);
1143 break;
1144 case MARKER_ID_SET_MARKER_FORMAT:
1145 marker_name = pos = tf->event.data;
1146 g_debug("Doing MARKER_ID_SET_MARKER_FORMAT of marker %s",
1147 marker_name);
1148 pos += strlen(marker_name) + 1;
1149 //break genevent.
1150 //pos += ltt_align((size_t)pos, tf->trace->arch_size, tf->alignment);
1151 format = pos;
1152 pos += strlen(format) + 1;
1153 //break genevent
1154 //pos += ltt_align((size_t)pos, tf->trace->arch_size, tf->alignment);
1155 marker_format_event(tf->trace, g_quark_from_string(marker_name),
1156 format);
1157 /* get information from dictionnary TODO */
1158 break;
1159 case MARKER_ID_HEARTBEAT_32:
1160 case MARKER_ID_HEARTBEAT_64:
1161 break;
1162 default:
1163 g_warning("Error in processing facility file %s, "
1164 "unknown event id %hhu.",
1165 g_quark_to_string(tf->name),
1166 tf->event.event_id);
1167 err = EPERM;
1168 goto event_id_error;
1169 }
1170 }
1171 }
1172 return 0;
1173
1174 /* Error handling */
1175 event_id_error:
1176 update_error:
1177 seek_error:
1178 g_warning("An error occured in facility tracefile parsing");
1179 return err;
1180 }
1181
1182
1183 LttTrace *ltt_trace_open(const gchar *pathname)
1184 {
1185 gchar abs_path[PATH_MAX];
1186 LttTrace * t;
1187 LttTracefile *tf;
1188 GArray *group;
1189 int i, ret;
1190 struct ltt_block_start_header *header;
1191 DIR *dir;
1192 struct dirent *entry;
1193 guint control_found = 0;
1194 struct stat stat_buf;
1195 gchar path[PATH_MAX];
1196
1197 t = g_new(LttTrace, 1);
1198 if(!t) goto alloc_error;
1199
1200 get_absolute_pathname(pathname, abs_path);
1201 t->pathname = g_quark_from_string(abs_path);
1202
1203 g_datalist_init(&t->tracefiles);
1204
1205 /* Test to see if it looks like a trace */
1206 dir = opendir(abs_path);
1207 if(dir == NULL) {
1208 perror(abs_path);
1209 goto open_error;
1210 }
1211 while((entry = readdir(dir)) != NULL) {
1212 strcpy(path, abs_path);
1213 strcat(path, "/");
1214 strcat(path, entry->d_name);
1215 ret = stat(path, &stat_buf);
1216 if(ret == -1) {
1217 perror(path);
1218 continue;
1219 }
1220 if(S_ISDIR(stat_buf.st_mode)) {
1221 if(strcmp(entry->d_name, "control") == 0) {
1222 control_found = 1;
1223 }
1224 }
1225 }
1226 closedir(dir);
1227
1228 if(!control_found) goto find_error;
1229
1230 /* Open all the tracefiles */
1231 if(open_tracefiles(t, abs_path, "")) {
1232 g_warning("Error opening tracefile %s", abs_path);
1233 goto find_error;
1234 }
1235
1236 /* Parse each trace control/facilitiesN files : get runtime fac. info */
1237 group = g_datalist_id_get_data(&t->tracefiles, LTT_TRACEFILE_NAME_FACILITIES);
1238 if(group == NULL) {
1239 g_error("Trace %s has no facility tracefile", abs_path);
1240 g_assert(0);
1241 goto facilities_error;
1242 }
1243
1244 /* Get the trace information for the control/facility 0 tracefile */
1245 g_assert(group->len > 0);
1246 tf = &g_array_index (group, LttTracefile, 0);
1247 header = (struct ltt_block_start_header*)tf->buffer.head;
1248 g_assert(parse_trace_header(header->trace,
1249 tf, t) == 0);
1250
1251 t->num_cpu = group->len;
1252
1253 ret = allocate_marker_data(t);
1254 if (ret)
1255 g_error("Error in allocating marker data");
1256
1257 for(i=0; i<group->len; i++) {
1258 tf = &g_array_index (group, LttTracefile, i);
1259 if(ltt_process_facility_tracefile(tf))
1260 goto facilities_error;
1261 }
1262
1263 return t;
1264
1265 /* Error handling */
1266 facilities_error:
1267 destroy_marker_data(t);
1268 find_error:
1269 g_datalist_clear(&t->tracefiles);
1270 open_error:
1271 g_free(t);
1272 alloc_error:
1273 return NULL;
1274
1275 }
1276
1277 GQuark ltt_trace_name(const LttTrace *t)
1278 {
1279 return t->pathname;
1280 }
1281
1282
1283 /******************************************************************************
1284 * When we copy a trace, we want all the opening actions to happen again :
1285 * the trace will be reopened and totally independant from the original.
1286 * That's why we call ltt_trace_open.
1287 *****************************************************************************/
1288 LttTrace *ltt_trace_copy(LttTrace *self)
1289 {
1290 return ltt_trace_open(g_quark_to_string(self->pathname));
1291 }
1292
1293 void ltt_trace_close(LttTrace *t)
1294 {
1295 g_datalist_clear(&t->tracefiles);
1296 g_free(t);
1297 }
1298
1299
1300 /*****************************************************************************
1301 *Get the system description of the trace
1302 ****************************************************************************/
1303 #if 0
1304 LttFacility *ltt_trace_facility_by_id(LttTrace *t, guint8 id)
1305 {
1306 g_assert(id < t->facilities_by_num->len);
1307 return &g_array_index(t->facilities_by_num, LttFacility, id);
1308 }
1309
1310 /* ltt_trace_facility_get_by_name
1311 *
1312 * Returns the GArray of facility indexes. All the fac_ids that matches the
1313 * requested facility name.
1314 *
1315 * If name is not found, returns NULL.
1316 */
1317 GArray *ltt_trace_facility_get_by_name(LttTrace *t, GQuark name)
1318 {
1319 return g_datalist_id_get_data(&t->facilities_by_name, name);
1320 }
1321 #endif //0
1322
1323 /*****************************************************************************
1324 * Functions to discover all the event types in the trace
1325 ****************************************************************************/
1326
1327 #if 0
1328 unsigned ltt_trace_eventtype_number(LttTrace *t)
1329 {
1330 unsigned int i;
1331 unsigned count = 0;
1332 unsigned int num = t->facility_number;
1333 LttFacility * f;
1334
1335 for(i=0;i<num;i++){
1336 f = (LttFacility*)g_ptr_array_index(t->facilities, i);
1337 count += f->event_number;
1338 }
1339 return count;
1340 }
1341 #endif //0
1342
1343 #if 0
1344 //use an iteration on all the trace facilities, and inside iteration on all the
1345 //event types in each facilities instead.
1346 LttEventType *ltt_trace_eventtype_get(LttTrace *t, unsigned evId)
1347 {
1348 LttEventType *event_type;
1349
1350 LttFacility * f;
1351 f = ltt_trace_facility_by_id(t,evId);
1352
1353 if(unlikely(!f)) event_type = NULL;
1354 else event_type = f->events[evId - f->base_id];
1355
1356 return event_type;
1357 }
1358 #endif //0
1359
1360 #if 0
1361 /*****************************************************************************
1362 * ltt_trace_find_tracefile
1363 *
1364 * Find a tracefile by name and index in the group.
1365 *
1366 * Returns a pointer to the tracefiles, else NULL.
1367 ****************************************************************************/
1368
1369 LttTracefile *ltt_trace_find_tracefile(LttTrace *t, const gchar *name)
1370 {
1371 }
1372 #endif //0
1373
1374 /*****************************************************************************
1375 * Get the start time and end time of the trace
1376 ****************************************************************************/
1377
1378 void ltt_tracefile_time_span_get(LttTracefile *tf,
1379 LttTime *start, LttTime *end)
1380 {
1381 int err;
1382
1383 err = map_block(tf, 0);
1384 if(unlikely(err)) {
1385 g_error("Can not map block");
1386 *start = ltt_time_infinite;
1387 } else
1388 *start = tf->buffer.begin.timestamp;
1389
1390 err = map_block(tf, tf->num_blocks - 1); /* Last block */
1391 if(unlikely(err)) {
1392 g_error("Can not map block");
1393 *end = ltt_time_zero;
1394 } else
1395 *end = tf->buffer.end.timestamp;
1396 }
1397
1398 struct tracefile_time_span_get_args {
1399 LttTrace *t;
1400 LttTime *start;
1401 LttTime *end;
1402 };
1403
1404 void group_time_span_get(GQuark name, gpointer data, gpointer user_data)
1405 {
1406 struct tracefile_time_span_get_args *args =
1407 (struct tracefile_time_span_get_args*)user_data;
1408
1409 GArray *group = (GArray *)data;
1410 int i;
1411 LttTracefile *tf;
1412 LttTime tmp_start;
1413 LttTime tmp_end;
1414
1415 for(i=0; i<group->len; i++) {
1416 tf = &g_array_index (group, LttTracefile, i);
1417 if(tf->cpu_online) {
1418 ltt_tracefile_time_span_get(tf, &tmp_start, &tmp_end);
1419 if(ltt_time_compare(*args->start, tmp_start)>0) *args->start = tmp_start;
1420 if(ltt_time_compare(*args->end, tmp_end)<0) *args->end = tmp_end;
1421 }
1422 }
1423 }
1424
1425 void ltt_trace_time_span_get(LttTrace *t, LttTime *start, LttTime *end)
1426 {
1427 LttTime min_start = ltt_time_infinite;
1428 LttTime max_end = ltt_time_zero;
1429 struct tracefile_time_span_get_args args = { t, &min_start, &max_end };
1430
1431 g_datalist_foreach(&t->tracefiles, &group_time_span_get, &args);
1432
1433 if(start != NULL) *start = min_start;
1434 if(end != NULL) *end = max_end;
1435
1436 }
1437
1438
1439 /*****************************************************************************
1440 *Get the name of a tracefile
1441 ****************************************************************************/
1442
1443 GQuark ltt_tracefile_name(const LttTracefile *tf)
1444 {
1445 return tf->name;
1446 }
1447
1448 GQuark ltt_tracefile_long_name(const LttTracefile *tf)
1449 {
1450 return tf->long_name;
1451 }
1452
1453
1454
1455 guint ltt_tracefile_cpu(LttTracefile *tf)
1456 {
1457 return tf->cpu_num;
1458 }
1459
1460 guint ltt_tracefile_tid(LttTracefile *tf)
1461 {
1462 return tf->tid;
1463 }
1464
1465 guint ltt_tracefile_pgid(LttTracefile *tf)
1466 {
1467 return tf->pgid;
1468 }
1469
1470 guint64 ltt_tracefile_creation(LttTracefile *tf)
1471 {
1472 return tf->creation;
1473 }
1474 /*****************************************************************************
1475 * Get the number of blocks in the tracefile
1476 ****************************************************************************/
1477
1478 guint ltt_tracefile_block_number(LttTracefile *tf)
1479 {
1480 return tf->num_blocks;
1481 }
1482
1483
1484 /* Seek to the first event in a tracefile that has a time equal or greater than
1485 * the time passed in parameter.
1486 *
1487 * If the time parameter is outside the tracefile time span, seek to the first
1488 * event or if after, return ERANGE.
1489 *
1490 * If the time parameter is before the first event, we have to seek specially to
1491 * there.
1492 *
1493 * If the time is after the end of the trace, return ERANGE.
1494 *
1495 * Do a binary search to find the right block, then a sequential search in the
1496 * block to find the event.
1497 *
1498 * In the special case where the time requested fits inside a block that has no
1499 * event corresponding to the requested time, the first event of the next block
1500 * will be seeked.
1501 *
1502 * IMPORTANT NOTE : // FIXME everywhere...
1503 *
1504 * You MUST NOT do a ltt_tracefile_read right after a ltt_tracefile_seek_time :
1505 * you will jump over an event if you do.
1506 *
1507 * Return value : 0 : no error, the tf->event can be used
1508 * ERANGE : time if after the last event of the trace
1509 * otherwise : this is an error.
1510 *
1511 * */
1512
1513 int ltt_tracefile_seek_time(LttTracefile *tf, LttTime time)
1514 {
1515 int ret = 0;
1516 int err;
1517 unsigned int block_num, high, low;
1518
1519 /* seek at the beginning of trace */
1520 err = map_block(tf, 0); /* First block */
1521 if(unlikely(err)) {
1522 g_error("Can not map block");
1523 goto fail;
1524 }
1525
1526 /* If the time is lower or equal the beginning of the trace,
1527 * go to the first event. */
1528 if(ltt_time_compare(time, tf->buffer.begin.timestamp) <= 0) {
1529 ret = ltt_tracefile_read(tf);
1530 if(ret == ERANGE) goto range;
1531 else if (ret) goto fail;
1532 goto found; /* There is either no event in the trace or the event points
1533 to the first event in the trace */
1534 }
1535
1536 err = map_block(tf, tf->num_blocks - 1); /* Last block */
1537 if(unlikely(err)) {
1538 g_error("Can not map block");
1539 goto fail;
1540 }
1541
1542 /* If the time is after the end of the trace, return ERANGE. */
1543 if(ltt_time_compare(time, tf->buffer.end.timestamp) > 0) {
1544 goto range;
1545 }
1546
1547 /* Binary search the block */
1548 high = tf->num_blocks - 1;
1549 low = 0;
1550
1551 while(1) {
1552 block_num = ((high-low) / 2) + low;
1553
1554 err = map_block(tf, block_num);
1555 if(unlikely(err)) {
1556 g_error("Can not map block");
1557 goto fail;
1558 }
1559 if(high == low) {
1560 /* We cannot divide anymore : this is what would happen if the time
1561 * requested was exactly between two consecutive buffers'end and start
1562 * timestamps. This is also what would happend if we didn't deal with out
1563 * of span cases prior in this function. */
1564 /* The event is right in the buffer!
1565 * (or in the next buffer first event) */
1566 while(1) {
1567 ret = ltt_tracefile_read(tf);
1568 if(ret == ERANGE) goto range; /* ERANGE or EPERM */
1569 else if(ret) goto fail;
1570
1571 if(ltt_time_compare(time, tf->event.event_time) <= 0)
1572 goto found;
1573 }
1574
1575 } else if(ltt_time_compare(time, tf->buffer.begin.timestamp) < 0) {
1576 /* go to lower part */
1577 high = block_num - 1;
1578 } else if(ltt_time_compare(time, tf->buffer.end.timestamp) > 0) {
1579 /* go to higher part */
1580 low = block_num + 1;
1581 } else {/* The event is right in the buffer!
1582 (or in the next buffer first event) */
1583 while(1) {
1584 ret = ltt_tracefile_read(tf);
1585 if(ret == ERANGE) goto range; /* ERANGE or EPERM */
1586 else if(ret) goto fail;
1587
1588 if(ltt_time_compare(time, tf->event.event_time) <= 0)
1589 break;
1590 }
1591 goto found;
1592 }
1593 }
1594
1595 found:
1596 return 0;
1597 range:
1598 return ERANGE;
1599
1600 /* Error handling */
1601 fail:
1602 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1603 g_quark_to_string(tf->name));
1604 return EPERM;
1605 }
1606
1607
1608 int ltt_tracefile_seek_position(LttTracefile *tf, const LttEventPosition *ep) {
1609
1610 int err;
1611
1612 if(ep->tracefile != tf) {
1613 goto fail;
1614 }
1615
1616 err = map_block(tf, ep->block);
1617 if(unlikely(err)) {
1618 g_error("Can not map block");
1619 goto fail;
1620 }
1621
1622 tf->event.offset = ep->offset;
1623
1624 /* Put back the event real tsc */
1625 tf->event.tsc = ep->tsc;
1626 tf->buffer.tsc = ep->tsc;
1627
1628 err = ltt_tracefile_read_update_event(tf);
1629 if(err) goto fail;
1630 err = ltt_tracefile_read_op(tf);
1631 if(err) goto fail;
1632
1633 return 0;
1634
1635 fail:
1636 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1637 g_quark_to_string(tf->name));
1638 return 1;
1639 }
1640
1641 LttTime ltt_interpolate_time_from_tsc(LttTracefile *tf, guint64 tsc)
1642 {
1643 LttTime time;
1644
1645 if(tsc > tf->trace->start_tsc) {
1646 time = ltt_time_from_uint64(
1647 (double)(tsc - tf->trace->start_tsc)
1648 * (1000000000.0 / tf->trace->freq_scale)
1649 / (double)tf->trace->start_freq);
1650 time = ltt_time_add(tf->trace->start_time_from_tsc, time);
1651 } else {
1652 time = ltt_time_from_uint64(
1653 (double)(tf->trace->start_tsc - tsc)
1654 * (1000000000.0 / tf->trace->freq_scale)
1655 / (double)tf->trace->start_freq);
1656 time = ltt_time_sub(tf->trace->start_time_from_tsc, time);
1657 }
1658 return time;
1659 }
1660
1661 /* Calculate the real event time based on the buffer boundaries */
1662 LttTime ltt_interpolate_time(LttTracefile *tf, LttEvent *event)
1663 {
1664 return ltt_interpolate_time_from_tsc(tf, tf->buffer.tsc);
1665 }
1666
1667
1668 /* Get the current event of the tracefile : valid until the next read */
1669 LttEvent *ltt_tracefile_get_event(LttTracefile *tf)
1670 {
1671 return &tf->event;
1672 }
1673
1674
1675
1676 /*****************************************************************************
1677 *Function name
1678 * ltt_tracefile_read : Read the next event in the tracefile
1679 *Input params
1680 * t : tracefile
1681 *Return value
1682 *
1683 * Returns 0 if an event can be used in tf->event.
1684 * Returns ERANGE on end of trace. The event in tf->event still can be used
1685 * (if the last block was not empty).
1686 * Returns EPERM on error.
1687 *
1688 * This function does make the tracefile event structure point to the event
1689 * currently pointed to by the tf->event.
1690 *
1691 * Note : you must call a ltt_tracefile_seek to the beginning of the trace to
1692 * reinitialize it after an error if you want results to be coherent.
1693 * It would be the case if a end of trace last buffer has no event : the end
1694 * of trace wouldn't be returned, but an error.
1695 * We make the assumption there is at least one event per buffer.
1696 ****************************************************************************/
1697
1698 int ltt_tracefile_read(LttTracefile *tf)
1699 {
1700 int err;
1701
1702 err = ltt_tracefile_read_seek(tf);
1703 if(err) return err;
1704 err = ltt_tracefile_read_update_event(tf);
1705 if(err) return err;
1706 err = ltt_tracefile_read_op(tf);
1707 if(err) return err;
1708
1709 return 0;
1710 }
1711
1712 int ltt_tracefile_read_seek(LttTracefile *tf)
1713 {
1714 int err;
1715
1716 /* Get next buffer until we finally have an event, or end of trace */
1717 while(1) {
1718 err = ltt_seek_next_event(tf);
1719 if(unlikely(err == ENOPROTOOPT)) {
1720 return EPERM;
1721 }
1722
1723 /* Are we at the end of the buffer ? */
1724 if(err == ERANGE) {
1725 if(unlikely(tf->buffer.index == tf->num_blocks-1)){ /* end of trace ? */
1726 return ERANGE;
1727 } else {
1728 /* get next block */
1729 err = map_block(tf, tf->buffer.index + 1);
1730 if(unlikely(err)) {
1731 g_error("Can not map block");
1732 return EPERM;
1733 }
1734 }
1735 } else break; /* We found an event ! */
1736 }
1737
1738 return 0;
1739 }
1740
1741
1742 /* do specific operation on events */
1743 int ltt_tracefile_read_op(LttTracefile *tf)
1744 {
1745 LttEvent *event;
1746
1747 event = &tf->event;
1748
1749 /* do event specific operation */
1750
1751 /* do something if its an heartbeat event : increment the heartbeat count */
1752 //if(event->facility_id == LTT_FACILITY_CORE)
1753 // if(event->event_id == LTT_EVENT_HEARTBEAT)
1754 // tf->cur_heart_beat_number++;
1755
1756 return 0;
1757 }
1758
1759 static void print_debug_event_header(LttEvent *ev, void *start_pos, void *end_pos)
1760 {
1761 unsigned int offset = 0;
1762 int i, j;
1763
1764 g_printf("Event header (tracefile %s offset %llx):\n",
1765 g_quark_to_string(ev->tracefile->long_name),
1766 ((uint64_t)ev->tracefile->buffer.index * ev->tracefile->buf_size)
1767 + (long)start_pos - (long)ev->tracefile->buffer.head);
1768
1769 while (offset < (long)end_pos - (long)start_pos) {
1770 g_printf("%8lx", (long)start_pos - (long)ev->tracefile->buffer.head + offset);
1771 g_printf(" ");
1772
1773 for (i = 0; i < 4 ; i++) {
1774 for (j = 0; j < 4; j++) {
1775 if (offset + ((i * 4) + j) <
1776 (long)end_pos - (long)start_pos)
1777 g_printf("%02hhX",
1778 ((char*)start_pos)[offset + ((i * 4) + j)]);
1779 else
1780 g_printf(" ");
1781 g_printf(" ");
1782 }
1783 if (i < 4)
1784 g_printf(" ");
1785 }
1786 offset+=16;
1787 g_printf("\n");
1788 }
1789 }
1790
1791
1792 /* same as ltt_tracefile_read, but does not seek to the next event nor call
1793 * event specific operation. */
1794 int ltt_tracefile_read_update_event(LttTracefile *tf)
1795 {
1796 void * pos;
1797 LttEvent *event;
1798 void *pos_aligned;
1799
1800 event = &tf->event;
1801 pos = tf->buffer.head + event->offset;
1802
1803 /* Read event header */
1804
1805 /* Align the head */
1806 if(!tf->compact)
1807 pos += ltt_align((size_t)pos, tf->trace->arch_size, tf->alignment);
1808 else {
1809 g_assert(tf->has_heartbeat);
1810 pos += ltt_align((size_t)pos, sizeof(uint32_t), tf->alignment);
1811 }
1812 pos_aligned = pos;
1813
1814 if(tf->has_heartbeat) {
1815 event->timestamp = ltt_get_uint32(LTT_GET_BO(tf),
1816 pos);
1817 if(!tf->compact) {
1818 /* 32 bits -> 64 bits tsc */
1819 /* note : still works for seek and non seek cases. */
1820 if(event->timestamp < (0xFFFFFFFFULL&tf->buffer.tsc)) {
1821 tf->buffer.tsc = ((tf->buffer.tsc&0xFFFFFFFF00000000ULL)
1822 + 0x100000000ULL)
1823 | (guint64)event->timestamp;
1824 event->tsc = tf->buffer.tsc;
1825 } else {
1826 /* no overflow */
1827 tf->buffer.tsc = (tf->buffer.tsc&0xFFFFFFFF00000000ULL)
1828 | (guint64)event->timestamp;
1829 event->tsc = tf->buffer.tsc;
1830 event->compact_data = 0;
1831 }
1832 } else {
1833 /* Compact header */
1834 /* We keep the LSB of the previous timestamp, to make sure
1835 * we never go back */
1836 event->event_id = event->timestamp >> tf->tscbits;
1837 event->event_id = event->event_id & ((1 << tf->compact_event_bits) - 1);
1838 event->compact_data = event->timestamp >>
1839 (tf->compact_event_bits + tf->tscbits);
1840 //printf("tsc bits %u, ev bits %u init data %u\n",
1841 // tf->tscbits, tf->trace->compact_event_bits, event->compact_data);
1842 /* Put the compact data back in original endianness */
1843 event->compact_data = ltt_get_uint32(LTT_GET_BO(tf), &event->compact_data);
1844 event->event_size = 0xFFFF;
1845 //printf("Found compact event %d\n", event->event_id);
1846 //printf("Compact data %d\n", event->compact_data);
1847 event->timestamp = event->timestamp << tf->tsc_lsb_truncate;
1848 event->timestamp = event->timestamp & tf->tsc_mask;
1849 //printf("timestamp 0x%lX\n", event->timestamp);
1850 //printf("mask 0x%llX\n", tf->tsc_mask);
1851 //printf("mask_next 0x%llX\n", tf->tsc_mask_next_bit);
1852 //printf("previous tsc 0x%llX\n", tf->buffer.tsc);
1853 //printf("previous tsc&mask 0x%llX\n", tf->tsc_mask&tf->buffer.tsc);
1854 //printf("previous tsc&(~mask) 0x%llX\n", tf->buffer.tsc&(~tf->tsc_mask));
1855 if(event->timestamp < (tf->tsc_mask&tf->buffer.tsc)) {
1856 //printf("wrap\n");
1857 tf->buffer.tsc = ((tf->buffer.tsc&(~tf->tsc_mask))
1858 + tf->tsc_mask_next_bit)
1859 | (guint64)event->timestamp;
1860 event->tsc = tf->buffer.tsc;
1861 } else {
1862 //printf("no wrap\n");
1863 /* no overflow */
1864 tf->buffer.tsc = (tf->buffer.tsc&(~tf->tsc_mask))
1865 | (guint64)event->timestamp;
1866 event->tsc = tf->buffer.tsc;
1867 }
1868 //printf("current tsc 0x%llX\n", tf->buffer.tsc);
1869 }
1870 pos += sizeof(guint32);
1871 } else {
1872 event->tsc = ltt_get_uint64(LTT_GET_BO(tf), pos);
1873 tf->buffer.tsc = event->tsc;
1874 event->compact_data = 0;
1875 pos += sizeof(guint64);
1876 }
1877 event->event_time = ltt_interpolate_time(tf, event);
1878
1879 if(!tf->compact) {
1880 event->event_id = *(guint16*)pos;
1881 pos += sizeof(guint16);
1882
1883 event->event_size = ltt_get_uint16(LTT_GET_BO(tf), pos);
1884 pos += sizeof(guint16);
1885 } else {
1886 /* Compact event */
1887 event->event_size = 0xFFFF;
1888 }
1889
1890 if (a_event_debug)
1891 print_debug_event_header(event, pos_aligned, pos);
1892
1893 /* Align the head */
1894 if(!tf->compact)
1895 pos += ltt_align((size_t)pos, tf->trace->arch_size, tf->alignment);
1896
1897 event->data = pos;
1898
1899 /* get the data size and update the event fields with the current
1900 * information. Also update the time if a heartbeat_full event is found. */
1901 ltt_update_event_size(tf);
1902
1903 return 0;
1904 }
1905
1906
1907 /****************************************************************************
1908 *Function name
1909 * map_block : map a block from the file
1910 *Input Params
1911 * lttdes : ltt trace file
1912 * whichBlock : the block which will be read
1913 *return value
1914 * 0 : success
1915 * EINVAL : lseek fail
1916 * EIO : can not read from the file
1917 ****************************************************************************/
1918
1919 gint map_block(LttTracefile * tf, guint block_num)
1920 {
1921 int page_size = getpagesize();
1922 struct ltt_block_start_header *header;
1923
1924 g_assert(block_num < tf->num_blocks);
1925
1926 if(tf->buffer.head != NULL) {
1927 if(munmap(tf->buffer.head, PAGE_ALIGN(tf->buf_size))) {
1928 g_warning("unmap size : %u\n",
1929 PAGE_ALIGN(tf->buf_size));
1930 perror("munmap error");
1931 g_assert(0);
1932 }
1933 }
1934
1935
1936 /* Multiple of pages aligned head */
1937 tf->buffer.head = mmap(0,
1938 PAGE_ALIGN(tf->buf_size),
1939 PROT_READ, MAP_PRIVATE, tf->fd,
1940 PAGE_ALIGN((off_t)tf->buf_size * (off_t)block_num));
1941
1942 if(tf->buffer.head == MAP_FAILED) {
1943 perror("Error in allocating memory for buffer of tracefile");
1944 g_assert(0);
1945 goto map_error;
1946 }
1947 g_assert( ( (gulong)tf->buffer.head&(8-1) ) == 0); // make sure it's aligned.
1948
1949
1950 tf->buffer.index = block_num;
1951
1952 header = (struct ltt_block_start_header*)tf->buffer.head;
1953
1954 #if 0
1955 tf->buffer.begin.timestamp = ltt_time_add(
1956 ltt_time_from_uint64(
1957 ltt_get_uint64(LTT_GET_BO(tf),
1958 &header->begin.timestamp)
1959 - tf->trace->start_monotonic),
1960 tf->trace->start_time);
1961 #endif //0
1962 //g_debug("block %u begin : %lu.%lu", block_num,
1963 // tf->buffer.begin.timestamp.tv_sec, tf->buffer.begin.timestamp.tv_nsec);
1964 tf->buffer.begin.cycle_count = ltt_get_uint64(LTT_GET_BO(tf),
1965 &header->begin.cycle_count);
1966 tf->buffer.begin.freq = ltt_get_uint64(LTT_GET_BO(tf),
1967 &header->begin.freq);
1968 if(tf->buffer.begin.freq == 0)
1969 tf->buffer.begin.freq = tf->trace->start_freq;
1970
1971 tf->buffer.begin.timestamp = ltt_interpolate_time_from_tsc(tf,
1972 tf->buffer.begin.cycle_count);
1973 #if 0
1974 ltt_time_add(
1975 ltt_time_from_uint64(
1976 (double)(tf->buffer.begin.cycle_count
1977 - tf->trace->start_tsc) * 1000000.0
1978 / (double)tf->trace->start_freq),
1979 tf->trace->start_time_from_tsc);
1980 #endif //0
1981 #if 0
1982
1983 tf->buffer.end.timestamp = ltt_time_add(
1984 ltt_time_from_uint64(
1985 ltt_get_uint64(LTT_GET_BO(tf),
1986 &header->end.timestamp)
1987 - tf->trace->start_monotonic),
1988 tf->trace->start_time);
1989 #endif //0
1990 //g_debug("block %u end : %lu.%lu", block_num,
1991 // tf->buffer.end.timestamp.tv_sec, tf->buffer.end.timestamp.tv_nsec);
1992 tf->buffer.end.cycle_count = ltt_get_uint64(LTT_GET_BO(tf),
1993 &header->end.cycle_count);
1994 tf->buffer.end.freq = ltt_get_uint64(LTT_GET_BO(tf),
1995 &header->end.freq);
1996 if(tf->buffer.end.freq == 0)
1997 tf->buffer.end.freq = tf->trace->start_freq;
1998
1999 tf->buffer.lost_size = ltt_get_uint32(LTT_GET_BO(tf),
2000 &header->lost_size);
2001 tf->buffer.end.timestamp = ltt_interpolate_time_from_tsc(tf,
2002 tf->buffer.end.cycle_count);
2003 #if 0
2004 ltt_time_add(
2005 ltt_time_from_uint64(
2006 (double)(tf->buffer.end.cycle_count
2007 - tf->trace->start_tsc) * 1000000.0
2008 / (double)tf->trace->start_freq),
2009 tf->trace->start_time_from_tsc);
2010 #endif //0
2011 tf->buffer.tsc = tf->buffer.begin.cycle_count;
2012 tf->event.tsc = tf->buffer.tsc;
2013 tf->buffer.freq = tf->buffer.begin.freq;
2014
2015 /* FIXME
2016 * eventually support variable buffer size : will need a partial pre-read of
2017 * the headers to create an index when we open the trace... eventually. */
2018 g_assert(tf->buf_size == ltt_get_uint32(LTT_GET_BO(tf),
2019 &header->buf_size));
2020
2021 /* Now that the buffer is mapped, calculate the time interpolation for the
2022 * block. */
2023
2024 // tf->buffer.nsecs_per_cycle = calc_nsecs_per_cycle(tf);
2025 //tf->buffer.cyc2ns_scale = calc_nsecs_per_cycle(tf);
2026
2027 /* Make the current event point to the beginning of the buffer :
2028 * it means that the event read must get the first event. */
2029 tf->event.tracefile = tf;
2030 tf->event.block = block_num;
2031 tf->event.offset = 0;
2032
2033 return 0;
2034
2035 map_error:
2036 return -errno;
2037
2038 }
2039
2040 static void print_debug_event_data(LttEvent *ev)
2041 {
2042 unsigned int offset = 0;
2043 int i, j;
2044
2045 if (!max(ev->event_size, ev->data_size))
2046 return;
2047
2048 g_printf("Event data (tracefile %s offset %llx):\n",
2049 g_quark_to_string(ev->tracefile->long_name),
2050 ((uint64_t)ev->tracefile->buffer.index * ev->tracefile->buf_size)
2051 + (long)ev->data - (long)ev->tracefile->buffer.head);
2052
2053 while (offset < max(ev->event_size, ev->data_size)) {
2054 g_printf("%8lx", (long)ev->data + offset
2055 - (long)ev->tracefile->buffer.head);
2056 g_printf(" ");
2057
2058 for (i = 0; i < 4 ; i++) {
2059 for (j = 0; j < 4; j++) {
2060 if (offset + ((i * 4) + j) < max(ev->event_size, ev->data_size))
2061 g_printf("%02hhX", ((char*)ev->data)[offset + ((i * 4) + j)]);
2062 else
2063 g_printf(" ");
2064 g_printf(" ");
2065 }
2066 if (i < 4)
2067 g_printf(" ");
2068 }
2069
2070 g_printf(" ");
2071
2072 for (i = 0; i < 4; i++) {
2073 for (j = 0; j < 4; j++) {
2074 if (offset + ((i * 4) + j) < max(ev->event_size, ev->data_size)) {
2075 if (isprint(((char*)ev->data)[offset + ((i * 4) + j)]))
2076 g_printf("%c", ((char*)ev->data)[offset + ((i * 4) + j)]);
2077 else
2078 g_printf(".");
2079 } else
2080 g_printf(" ");
2081 }
2082 }
2083 offset+=16;
2084 g_printf("\n");
2085 }
2086 }
2087
2088 /* It will update the fields offsets too */
2089 void ltt_update_event_size(LttTracefile *tf)
2090 {
2091 off_t size = 0;
2092 char *tscdata;
2093 struct marker_info *info;
2094
2095 switch((enum marker_id)tf->event.event_id) {
2096 case MARKER_ID_SET_MARKER_ID:
2097 size = strlen((char*)tf->event.data) + 1;
2098 g_debug("marker %s id set", (char*)tf->event.data);
2099 size += ltt_align(size, sizeof(guint16), tf->alignment);
2100 size += sizeof(guint16);
2101 size += sizeof(guint8);
2102 size += sizeof(guint8);
2103 size += sizeof(guint8);
2104 size += sizeof(guint8);
2105 size += sizeof(guint8);
2106 break;
2107 case MARKER_ID_SET_MARKER_FORMAT:
2108 g_debug("marker %s format set", (char*)tf->event.data);
2109 size = strlen((char*)tf->event.data) + 1;
2110 size += strlen((char*)tf->event.data + size) + 1;
2111 break;
2112 case MARKER_ID_HEARTBEAT_32:
2113 g_debug("Update Event heartbeat 32 bits");
2114 break;
2115 case MARKER_ID_HEARTBEAT_64:
2116 g_debug("Update Event heartbeat 64 bits");
2117 tscdata = (char*)(tf->event.data);
2118 tf->event.tsc = ltt_get_uint64(LTT_GET_BO(tf), tscdata);
2119 tf->buffer.tsc = tf->event.tsc;
2120 tf->event.event_time = ltt_interpolate_time(tf, &tf->event);
2121 size = ltt_align(size, sizeof(guint64), tf->alignment);
2122 size += sizeof(guint64);
2123 break;
2124 }
2125
2126 info = marker_get_info_from_id(tf->trace, tf->event.event_id);
2127
2128 if (tf->event.event_id >= MARKER_CORE_IDS)
2129 g_assert(info != NULL);
2130
2131 /* Do not update field offsets of core markers when initially reading the
2132 * facility tracefile when the infos about these markers do not exist yet.
2133 */
2134 if (likely(info && info->fields)) {
2135 if (info->size != -1)
2136 size = info->size;
2137 else
2138 size = marker_update_fields_offsets(marker_get_info_from_id(tf->trace,
2139 tf->event.event_id), tf->event.data);
2140 }
2141
2142 tf->event.data_size = size;
2143
2144 /* Check consistency between kernel and LTTV structure sizes */
2145 if(tf->event.event_size == 0xFFFF) {
2146 /* Event size too big to fit in the event size field */
2147 tf->event.event_size = tf->event.data_size;
2148 }
2149
2150 if (a_event_debug)
2151 print_debug_event_data(&tf->event);
2152
2153 /* Having a marker load or marker format event out of the facilities
2154 * tracefiles is a serious bug. */
2155 switch((enum marker_id)tf->event.event_id) {
2156 case MARKER_ID_SET_MARKER_ID:
2157 case MARKER_ID_SET_MARKER_FORMAT:
2158 if (tf->name != g_quark_from_string("/control/facilities"))
2159 g_error("Trace inconsistency : metadata event found in data "
2160 "tracefile %s", g_quark_to_string(tf->long_name));
2161 }
2162
2163 if (tf->event.data_size != tf->event.event_size) {
2164 struct marker_info *info = marker_get_info_from_id(tf->trace,
2165 tf->event.event_id);
2166 g_error("Kernel/LTTV event size differs for event %s: kernel %u, LTTV %u",
2167 g_quark_to_string(info->name),
2168 tf->event.event_size, tf->event.data_size);
2169 exit(-1);
2170 }
2171
2172 #if 0
2173 LttEventType *event_type =
2174 ltt_facility_eventtype_get(f, tf->event.event_id);
2175
2176 if(!event_type) {
2177 g_warning("Unknown event id %hhu in facility %s in tracefile %s",
2178 tf->event.event_id,
2179 g_quark_to_string(f->name),
2180 g_quark_to_string(tf->name));
2181 goto event_type_error;
2182 }
2183
2184 /* Compute the dynamic offsets */
2185 compute_offsets(tf, f, event_type, &size, tf->event.data);
2186
2187 //g_debug("Event root field : f.e %hhu.%hhu size %zd",
2188 // tf->event.facility_id,
2189 // tf->event.event_id, size);
2190
2191 no_offset:
2192 tf->event.data_size = size;
2193
2194 /* Check consistency between kernel and LTTV structure sizes */
2195 if(tf->event.event_size == 0xFFFF) {
2196 /* Event size too big to fit in the event size field */
2197 tf->event.event_size = tf->event.data_size;
2198 }
2199 if (tf->event.data_size != tf->event.event_size) {
2200 g_error("Kernel/LTTV event size differs for event %s.%s: kernel %u, LTTV %u",
2201 g_quark_to_string(f->name), g_quark_to_string(event_type->name),
2202 tf->event.event_size, tf->event.data_size);
2203 exit(-1);
2204 }
2205 //g_assert(tf->event.data_size == tf->event.event_size);
2206
2207 return;
2208
2209 event_type_error:
2210 event_id_error:
2211 if(tf->event.event_size == 0xFFFF) {
2212 g_error("Cannot jump over an unknown event bigger than 0xFFFE bytes");
2213 }
2214 /* The facility is unknown : use the kernel information about this event
2215 * to jump over it. */
2216 tf->event.data_size = tf->event.event_size;
2217 #endif //0
2218 }
2219
2220
2221 /* Take the tf current event offset and use the event facility id and event id
2222 * to figure out where is the next event offset.
2223 *
2224 * This is an internal function not aiming at being used elsewhere : it will
2225 * not jump over the current block limits. Please consider using
2226 * ltt_tracefile_read to do this.
2227 *
2228 * Returns 0 on success
2229 * ERANGE if we are at the end of the buffer.
2230 * ENOPROTOOPT if an error occured when getting the current event size.
2231 */
2232 int ltt_seek_next_event(LttTracefile *tf)
2233 {
2234 int ret = 0;
2235 void *pos;
2236
2237 /* seek over the buffer header if we are at the buffer start */
2238 if(tf->event.offset == 0) {
2239 tf->event.offset += tf->buffer_header_size;
2240
2241 if(tf->event.offset == tf->buf_size - tf->buffer.lost_size) {
2242 ret = ERANGE;
2243 }
2244 goto found;
2245 }
2246
2247
2248 pos = tf->event.data;
2249
2250 if(tf->event.data_size < 0) goto error;
2251
2252 pos += (size_t)tf->event.data_size;
2253
2254 tf->event.offset = pos - tf->buffer.head;
2255
2256 if(tf->event.offset == tf->buf_size - tf->buffer.lost_size) {
2257 ret = ERANGE;
2258 goto found;
2259 }
2260 g_assert(tf->event.offset < tf->buf_size - tf->buffer.lost_size);
2261
2262 found:
2263 return ret;
2264
2265 error:
2266 g_error("Error in ltt_seek_next_event for tracefile %s",
2267 g_quark_to_string(tf->name));
2268 return ENOPROTOOPT;
2269 }
2270
2271 #if 0
2272 /*****************************************************************************
2273 *Function name
2274 * calc_nsecs_per_cycle : calculate nsecs per cycle for current block
2275 *
2276 * 1.0 / (freq(khz) *1000) * 1000000000
2277 *Input Params
2278 * t : tracefile
2279 ****************************************************************************/
2280 /* from timer_tsc.c */
2281 #define CYC2NS_SCALE_FACTOR 10
2282 static guint32 calc_nsecs_per_cycle(LttTracefile * tf)
2283 {
2284 //return 1e6 / (double)tf->buffer.freq;
2285 guint32 cpu_mhz = tf->buffer.freq / 1000;
2286 guint32 cyc2ns_scale = (1000 << CYC2NS_SCALE_FACTOR)/cpu_mhz;
2287
2288 return cyc2ns_scale;
2289 // return 1e6 / (double)tf->buffer.freq;
2290 }
2291
2292 static guint64 cycles_2_ns(LttTracefile *tf, guint64 cycles)
2293 {
2294 return (cycles * tf->buffer.cyc2ns_scale) >> CYC2NS_SCALE_FACTOR;
2295 }
2296 #endif //0
2297
2298 #if 0
2299 void setFieldsOffset(LttTracefile *tf, LttEventType *evT,void *evD)
2300 {
2301 LttField * rootFld = evT->root_field;
2302 // rootFld->base_address = evD;
2303
2304 if(likely(rootFld))
2305 rootFld->field_size = getFieldtypeSize(tf, evT->facility,
2306 evT, 0,0,rootFld, evD);
2307 }
2308 #endif //0
2309 #if 0
2310 /*****************************************************************************
2311 *Function name
2312 * set_fields_offsets : set the precomputable offset of the fields
2313 *Input params
2314 * tracefile : opened trace file
2315 * event_type : the event type
2316 ****************************************************************************/
2317
2318 void set_fields_offsets(LttTracefile *tf, LttEventType *event_type)
2319 {
2320 LttField *field = event_type->root_field;
2321 enum field_status fixed_root = FIELD_FIXED, fixed_parent = FIELD_FIXED;
2322
2323 if(likely(field))
2324 preset_field_type_size(tf, event_type, 0, 0,
2325 &fixed_root, &fixed_parent,
2326 field);
2327
2328 }
2329 #endif //0
2330
2331
2332 /*****************************************************************************
2333 *Function name
2334 * get_alignment : Get the alignment needed for a field.
2335 *Input params
2336 * field : field
2337 *
2338 * returns : The size on which it must be aligned.
2339 *
2340 ****************************************************************************/
2341 #if 0
2342 off_t get_alignment(LttField *field)
2343 {
2344 LttType *type = &field->field_type;
2345
2346 switch(type->type_class) {
2347 case LTT_INT_FIXED:
2348 case LTT_UINT_FIXED:
2349 case LTT_POINTER:
2350 case LTT_CHAR:
2351 case LTT_UCHAR:
2352 case LTT_SHORT:
2353 case LTT_USHORT:
2354 case LTT_INT:
2355 case LTT_UINT:
2356 case LTT_LONG:
2357 case LTT_ULONG:
2358 case LTT_SIZE_T:
2359 case LTT_SSIZE_T:
2360 case LTT_OFF_T:
2361 case LTT_FLOAT:
2362 case LTT_ENUM:
2363 /* Align offset on type size */
2364 g_assert(field->field_size != 0);
2365 return field->field_size;
2366 break;
2367 case LTT_STRING:
2368 return 1;
2369 break;
2370 case LTT_ARRAY:
2371 g_assert(type->fields->len == 1);
2372 {
2373 LttField *child = &g_array_index(type->fields, LttField, 0);
2374 return get_alignment(child);
2375 }
2376 break;
2377 case LTT_SEQUENCE:
2378 g_assert(type->fields->len == 2);
2379 {
2380 off_t localign = 1;
2381 LttField *child = &g_array_index(type->fields, LttField, 0);
2382
2383 localign = max(localign, get_alignment(child));
2384
2385 child = &g_array_index(type->fields, LttField, 1);
2386 localign = max(localign, get_alignment(child));
2387
2388 return localign;
2389 }
2390 break;
2391 case LTT_STRUCT:
2392 case LTT_UNION:
2393 {
2394 guint i;
2395 off_t localign = 1;
2396
2397 for(i=0; i<type->fields->len; i++) {
2398 LttField *child = &g_array_index(type->fields, LttField, i);
2399 localign = max(localign, get_alignment(child));
2400 }
2401 return localign;
2402 }
2403 break;
2404 case LTT_NONE:
2405 default:
2406 g_error("get_alignment : unknown type");
2407 return -1;
2408 }
2409 }
2410
2411 #endif //0
2412
2413 /*****************************************************************************
2414 *Function name
2415 * field_compute_static_size : Determine the size of fields known by their
2416 * sole definition. Unions, arrays and struct sizes might be known, but
2417 * the parser does not give that information.
2418 *Input params
2419 * tf : tracefile
2420 * field : field
2421 *
2422 ****************************************************************************/
2423 #if 0
2424 void field_compute_static_size(LttFacility *fac, LttField *field)
2425 {
2426 LttType *type = &field->field_type;
2427
2428 switch(type->type_class) {
2429 case LTT_INT_FIXED:
2430 case LTT_UINT_FIXED:
2431 case LTT_POINTER:
2432 case LTT_CHAR:
2433 case LTT_UCHAR:
2434 case LTT_SHORT:
2435 case LTT_USHORT:
2436 case LTT_INT:
2437 case LTT_UINT:
2438 case LTT_LONG:
2439 case LTT_ULONG:
2440 case LTT_SIZE_T:
2441 case LTT_SSIZE_T:
2442 case LTT_OFF_T:
2443 case LTT_FLOAT:
2444 case LTT_ENUM:
2445 case LTT_STRING:
2446 /* nothing to do */
2447 break;
2448 case LTT_ARRAY:
2449 /* note this : array type size is the number of elements in the array,
2450 * while array field size of the length of the array in bytes */
2451 g_assert(type->fields->len == 1);
2452 {
2453 LttField *child = &g_array_index(type->fields, LttField, 0);
2454 field_compute_static_size(fac, child);
2455
2456 if(child->field_size != 0) {
2457 field->field_size = type->size * child->field_size;
2458 field->dynamic_offsets = g_array_sized_new(FALSE, TRUE,
2459 sizeof(off_t), type->size);
2460 } else {
2461 field->field_size = 0;
2462 }
2463 }
2464 break;
2465 case LTT_SEQUENCE:
2466 g_assert(type->fields->len == 2);
2467 {
2468 off_t local_offset = 0;
2469 LttField *child = &g_array_index(type->fields, LttField, 1);
2470 field_compute_static_size(fac, child);
2471 field->field_size = 0;
2472 type->size = 0;
2473 if(child->field_size != 0) {
2474 field->dynamic_offsets = g_array_sized_new(FALSE, TRUE,
2475 sizeof(off_t), SEQUENCE_AVG_ELEMENTS);
2476 }
2477 }
2478 break;
2479 case LTT_STRUCT:
2480 case LTT_UNION:
2481 {
2482 guint i;
2483 for(i=0;i<type->fields->len;i++) {
2484 LttField *child = &g_array_index(type->fields, LttField, i);
2485 field_compute_static_size(fac, child);
2486 if(child->field_size != 0) {
2487 type->size += ltt_align(type->size, get_alignment(child),
2488 fac->alignment);
2489 type->size += child->field_size;
2490 } else {
2491 /* As soon as we find a child with variable size, we have
2492 * a variable size */
2493 type->size = 0;
2494 break;
2495 }
2496 }
2497 field->field_size = type->size;
2498 }
2499 break;
2500 default:
2501 g_error("field_static_size : unknown type");
2502 }
2503
2504 }
2505 #endif //0
2506
2507
2508 /*****************************************************************************
2509 *Function name
2510 * precompute_fields_offsets : set the precomputable offset of the fields
2511 *Input params
2512 * fac : facility
2513 * field : the field
2514 * offset : pointer to the current offset, must be incremented
2515 *
2516 * return : 1 : found a variable length field, stop the processing.
2517 * 0 otherwise.
2518 ****************************************************************************/
2519
2520 #if 0
2521 gint precompute_fields_offsets(LttFacility *fac, LttField *field, off_t *offset, gint is_compact)
2522 {
2523 LttType *type = &field->field_type;
2524
2525 if(unlikely(is_compact)) {
2526 g_assert(field->field_size != 0);
2527 /* FIXME THIS IS A HUUUUUGE hack :
2528 * offset is between the compact_data field in struct LttEvent
2529 * and the address of the field root in the memory map.
2530 * ark. Both will stay at the same addresses while the event
2531 * is readable, so it's ok.
2532 */
2533 field->offset_root = 0;
2534 field->fixed_root = FIELD_FIXED;
2535 return 0;
2536 }
2537
2538 switch(type->type_class) {
2539 case LTT_INT_FIXED:
2540 case LTT_UINT_FIXED:
2541 case LTT_POINTER:
2542 case LTT_CHAR:
2543 case LTT_UCHAR:
2544 case LTT_SHORT:
2545 case LTT_USHORT:
2546 case LTT_INT:
2547 case LTT_UINT:
2548 case LTT_LONG:
2549 case LTT_ULONG:
2550 case LTT_SIZE_T:
2551 case LTT_SSIZE_T:
2552 case LTT_OFF_T:
2553 case LTT_FLOAT:
2554 case LTT_ENUM:
2555 g_assert(field->field_size != 0);
2556 /* Align offset on type size */
2557 *offset += ltt_align(*offset, get_alignment(field),
2558 fac->alignment);
2559 /* remember offset */
2560 field->offset_root = *offset;
2561 field->fixed_root = FIELD_FIXED;
2562 /* Increment offset */
2563 *offset += field->field_size;
2564 return 0;
2565 break;
2566 case LTT_STRING:
2567 field->offset_root = *offset;
2568 field->fixed_root = FIELD_FIXED;
2569 return 1;
2570 break;
2571 case LTT_ARRAY:
2572 g_assert(type->fields->len == 1);
2573 {
2574 LttField *child = &g_array_index(type->fields, LttField, 0);
2575
2576 *offset += ltt_align(*offset, get_alignment(field),
2577 fac->alignment);
2578
2579 /* remember offset */
2580 field->offset_root = *offset;
2581 field->array_offset = *offset;
2582 field->fixed_root = FIELD_FIXED;
2583
2584 /* Let the child be variable */
2585 //precompute_fields_offsets(tf, child, offset);
2586
2587 if(field->field_size != 0) {
2588 /* Increment offset */
2589 /* field_size is the array size in bytes */
2590 *offset += field->field_size;
2591 return 0;
2592 } else {
2593 return 1;
2594 }
2595 }
2596 break;
2597 case LTT_SEQUENCE:
2598 g_assert(type->fields->len == 2);
2599 {
2600 LttField *child;
2601 guint ret;
2602
2603 *offset += ltt_align(*offset, get_alignment(field),
2604 fac->alignment);
2605
2606 /* remember offset */
2607 field->offset_root = *offset;
2608 field->fixed_root = FIELD_FIXED;
2609
2610 child = &g_array_index(type->fields, LttField, 0);
2611 ret = precompute_fields_offsets(fac, child, offset, is_compact);
2612 g_assert(ret == 0); /* Seq len cannot have variable len */
2613
2614 child = &g_array_index(type->fields, LttField, 1);
2615 *offset += ltt_align(*offset, get_alignment(child),
2616 fac->alignment);
2617 field->array_offset = *offset;
2618 /* Let the child be variable. */
2619 //ret = precompute_fields_offsets(fac, child, offset);
2620
2621 /* Cannot precompute fields offsets of sequence members, and has
2622 * variable length. */
2623 return 1;
2624 }
2625 break;
2626 case LTT_STRUCT:
2627 {
2628 LttField *child;
2629 guint i;
2630 gint ret=0;
2631
2632 *offset += ltt_align(*offset, get_alignment(field),
2633 fac->alignment);
2634 /* remember offset */
2635 field->offset_root = *offset;
2636 field->fixed_root = FIELD_FIXED;
2637
2638 for(i=0; i< type->fields->len; i++) {
2639 child = &g_array_index(type->fields, LttField, i);
2640 ret = precompute_fields_offsets(fac, child, offset, is_compact);
2641
2642 if(ret) break;
2643 }
2644 return ret;
2645 }
2646 break;
2647 case LTT_UNION:
2648 {
2649 LttField *child;
2650 guint i;
2651 gint ret=0;
2652
2653 *offset += ltt_align(*offset, get_alignment(field),
2654 fac->alignment);
2655 /* remember offset */
2656 field->offset_root = *offset;
2657 field->fixed_root = FIELD_FIXED;
2658
2659 for(i=0; i< type->fields->len; i++) {
2660 *offset = field->offset_root;
2661 child = &g_array_index(type->fields, LttField, i);
2662 ret = precompute_fields_offsets(fac, child, offset, is_compact);
2663
2664 if(ret) break;
2665 }
2666 *offset = field->offset_root + field->field_size;
2667 return ret;
2668 }
2669
2670 break;
2671 case LTT_NONE:
2672 default:
2673 g_error("precompute_fields_offsets : unknown type");
2674 return 1;
2675 }
2676
2677 }
2678
2679 #endif //0
2680
2681 #if 0
2682 /*****************************************************************************
2683 *Function name
2684 * precompute_offsets : set the precomputable offset of an event type
2685 *Input params
2686 * tf : tracefile
2687 * event : event type
2688 *
2689 ****************************************************************************/
2690 void precompute_offsets(LttFacility *fac, LttEventType *event)
2691 {
2692 guint i;
2693 off_t offset = 0;
2694 gint ret;
2695
2696 /* First, compute the size of fixed size fields. Will determine size for
2697 * arrays, struct and unions, which is not done by the parser */
2698 for(i=0; i<event->fields->len; i++) {
2699 LttField *field = &g_array_index(event->fields, LttField, i);
2700 field_compute_static_size(fac, field);
2701 }
2702
2703 /* Precompute all known offsets */
2704 for(i=0; i<event->fields->len; i++) {
2705 LttField *field = &g_array_index(event->fields, LttField, i);
2706 if(event->has_compact_data && i == 0)
2707 ret = precompute_fields_offsets(fac, field, &offset, 1);
2708 else
2709 ret = precompute_fields_offsets(fac, field, &offset, 0);
2710 if(ret) break;
2711 }
2712 }
2713 #endif //0
2714
2715
2716
2717 /*****************************************************************************
2718 *Function name
2719 * preset_field_type_size : set the fixed sizes of the field type
2720 *Input params
2721 * tf : tracefile
2722 * event_type : event type
2723 * offset_root : offset from the root
2724 * offset_parent : offset from the parent
2725 * fixed_root : Do we know a fixed offset to the root ?
2726 * fixed_parent : Do we know a fixed offset to the parent ?
2727 * field : field
2728 ****************************************************************************/
2729
2730
2731
2732 // preset the fixed size offsets. Calculate them just like genevent-new : an
2733 // increment of a *to value that represents the offset from the start of the
2734 // event data.
2735 // The preset information is : offsets up to (and including) the first element
2736 // of variable size. All subsequent fields must be flagged "VARIABLE OFFSET".
2737 #if 0
2738 void preset_field_type_size(LttTracefile *tf, LttEventType *event_type,
2739 off_t offset_root, off_t offset_parent,
2740 enum field_status *fixed_root, enum field_status *fixed_parent,
2741 LttField *field)
2742 {
2743 enum field_status local_fixed_root, local_fixed_parent;
2744 guint i;
2745 LttType *type;
2746
2747 g_assert(field->fixed_root == FIELD_UNKNOWN);
2748 g_assert(field->fixed_parent == FIELD_UNKNOWN);
2749 g_assert(field->fixed_size == FIELD_UNKNOWN);
2750
2751 type = field->field_type;
2752
2753 field->fixed_root = *fixed_root;
2754 if(field->fixed_root == FIELD_FIXED)
2755 field->offset_root = offset_root;
2756 else
2757 field->offset_root = 0;
2758
2759 field->fixed_parent = *fixed_parent;
2760 if(field->fixed_parent == FIELD_FIXED)
2761 field->offset_parent = offset_parent;
2762 else
2763 field->offset_parent = 0;
2764
2765 size_t current_root_offset;
2766 size_t current_offset;
2767 enum field_status current_child_status, final_child_status;
2768 size_t max_size;
2769
2770 switch(type->type_class) {
2771 case LTT_INT_FIXED:
2772 case LTT_UINT_FIXED:
2773 case LTT_CHAR:
2774 case LTT_UCHAR:
2775 case LTT_SHORT:
2776 case LTT_USHORT:
2777 case LTT_INT:
2778 case LTT_UINT:
2779 case LTT_FLOAT:
2780 case LTT_ENUM:
2781 field->field_size = ltt_type_size(tf->trace, type);
2782 field->fixed_size = FIELD_FIXED;
2783 break;
2784 case LTT_POINTER:
2785 field->field_size = (off_t)event_type->facility->pointer_size;
2786 field->fixed_size = FIELD_FIXED;
2787 break;
2788 case LTT_LONG:
2789 case LTT_ULONG:
2790 field->field_size = (off_t)event_type->facility->long_size;
2791 field->fixed_size = FIELD_FIXED;
2792 break;
2793 case LTT_SIZE_T:
2794 case LTT_SSIZE_T:
2795 case LTT_OFF_T:
2796 field->field_size = (off_t)event_type->facility->size_t_size;
2797 field->fixed_size = FIELD_FIXED;
2798 break;
2799 case LTT_SEQUENCE:
2800 local_fixed_root = FIELD_VARIABLE;
2801 local_fixed_parent = FIELD_VARIABLE;
2802 preset_field_type_size(tf, event_type,
2803 0, 0,
2804 &local_fixed_root, &local_fixed_parent,
2805 field->child[0]);
2806 field->fixed_size = FIELD_VARIABLE;
2807 field->field_size = 0;
2808 *fixed_root = FIELD_VARIABLE;
2809 *fixed_parent = FIELD_VARIABLE;
2810 break;
2811 case LTT_STRING:
2812 field->fixed_size = FIELD_VARIABLE;
2813 field->field_size = 0;
2814 *fixed_root = FIELD_VARIABLE;
2815 *fixed_parent = FIELD_VARIABLE;
2816 break;
2817 case LTT_ARRAY:
2818 local_fixed_root = FIELD_VARIABLE;
2819 local_fixed_parent = FIELD_VARIABLE;
2820 preset_field_type_size(tf, event_type,
2821 0, 0,
2822 &local_fixed_root, &local_fixed_parent,
2823 field->child[0]);
2824 field->fixed_size = field->child[0]->fixed_size;
2825 if(field->fixed_size == FIELD_FIXED) {
2826 field->field_size = type->element_number * field->child[0]->field_size;
2827 } else {
2828 field->field_size = 0;
2829 *fixed_root = FIELD_VARIABLE;
2830 *fixed_parent = FIELD_VARIABLE;
2831 }
2832 break;
2833 case LTT_STRUCT:
2834 current_root_offset = field->offset_root;
2835 current_offset = 0;
2836 current_child_status = FIELD_FIXED;
2837 for(i=0;i<type->element_number;i++) {
2838 preset_field_type_size(tf, event_type,
2839 current_root_offset, current_offset,
2840 fixed_root, &current_child_status,
2841 field->child[i]);
2842 if(current_child_status == FIELD_FIXED) {
2843 current_root_offset += field->child[i]->field_size;
2844 current_offset += field->child[i]->field_size;
2845 } else {
2846 current_root_offset = 0;
2847 current_offset = 0;
2848 }
2849 }
2850 if(current_child_status != FIELD_FIXED) {
2851 *fixed_parent = current_child_status;
2852 field->field_size = 0;
2853 field->fixed_size = current_child_status;
2854 } else {
2855 field->field_size = current_offset;
2856 field->fixed_size = FIELD_FIXED;
2857 }
2858 break;
2859 case LTT_UNION:
2860 current_root_offset = field->offset_root;
2861 current_offset = 0;
2862 max_size = 0;
2863 final_child_status = FIELD_FIXED;
2864 for(i=0;i<type->element_number;i++) {
2865 enum field_status current_root_child_status = FIELD_FIXED;
2866 enum field_status current_child_status = FIELD_FIXED;
2867 preset_field_type_size(tf, event_type,
2868 current_root_offset, current_offset,
2869 &current_root_child_status, &current_child_status,
2870 field->child[i]);
2871 if(current_child_status != FIELD_FIXED)
2872 final_child_status = current_child_status;
2873 else
2874 max_size = max(max_size, field->child[i]->field_size);
2875 }
2876 if(final_child_status != FIELD_FIXED) {
2877 g_error("LTTV does not support variable size fields in unions.");
2878 /* This will stop the application. */
2879 *fixed_root = final_child_status;
2880 *fixed_parent = final_child_status;
2881 field->field_size = 0;
2882 field->fixed_size = current_child_status;
2883 } else {
2884 field->field_size = max_size;
2885 field->fixed_size = FIELD_FIXED;
2886 }
2887 break;
2888 case LTT_NONE:
2889 g_error("unexpected type NONE");
2890 break;
2891 }
2892
2893 }
2894 #endif //0
2895
2896 /*****************************************************************************
2897 *Function name
2898 * check_fields_compatibility : Check for compatibility between two fields :
2899 * do they use the same inner structure ?
2900 *Input params
2901 * event_type1 : event type
2902 * event_type2 : event type
2903 * field1 : field
2904 * field2 : field
2905 *Returns : 0 if identical
2906 * 1 if not.
2907 ****************************************************************************/
2908 // this function checks for equality of field types. Therefore, it does not use
2909 // per se offsets. For instance, an aligned version of a structure is
2910 // compatible with an unaligned version of the same structure.
2911 #if 0
2912 gint check_fields_compatibility(LttEventType *event_type1,
2913 LttEventType *event_type2,
2914 LttField *field1, LttField *field2)
2915 {
2916 guint different = 0;
2917 LttType *type1;
2918 LttType *type2;
2919
2920 if(field1 == NULL) {
2921 if(field2 == NULL) goto end;
2922 else {
2923 different = 1;
2924 goto end;
2925 }
2926 } else if(field2 == NULL) {
2927 different = 1;
2928 goto end;
2929 }
2930
2931 type1 = &field1->field_type;
2932 type2 = &field2->field_type;
2933
2934 if(type1->type_class != type2->type_class) {
2935 different = 1;
2936 goto end;
2937 }
2938 if(type1->network != type2->network) {
2939 different = 1;
2940 goto end;
2941 }
2942
2943 switch(type1->type_class) {
2944 case LTT_INT_FIXED:
2945 case LTT_UINT_FIXED:
2946 case LTT_POINTER:
2947 case LTT_CHAR:
2948 case LTT_UCHAR:
2949 case LTT_SHORT:
2950 case LTT_USHORT:
2951 case LTT_INT:
2952 case LTT_UINT:
2953 case LTT_LONG:
2954 case LTT_ULONG:
2955 case LTT_SIZE_T:
2956 case LTT_SSIZE_T:
2957 case LTT_OFF_T:
2958 case LTT_FLOAT:
2959 case LTT_ENUM:
2960 if(field1->field_size != field2->field_size)
2961 different = 1;
2962 break;
2963 case LTT_STRING:
2964 break;
2965 case LTT_ARRAY:
2966 {
2967 LttField *child1 = &g_array_index(type1->fields, LttField, 0);
2968 LttField *child2 = &g_array_index(type2->fields, LttField, 0);
2969
2970 if(type1->size != type2->size)
2971 different = 1;
2972 if(check_fields_compatibility(event_type1, event_type2, child1, child2))
2973 different = 1;
2974 }
2975 break;
2976 case LTT_SEQUENCE:
2977 {
2978 LttField *child1 = &g_array_index(type1->fields, LttField, 1);
2979 LttField *child2 = &g_array_index(type2->fields, LttField, 1);
2980
2981 if(check_fields_compatibility(event_type1, event_type2, child1, child2))
2982 different = 1;
2983 }
2984 break;
2985 case LTT_STRUCT:
2986 case LTT_UNION:
2987 {
2988 LttField *child;
2989 guint i;
2990
2991 if(type1->fields->len != type2->fields->len) {
2992 different = 1;
2993 goto end;
2994 }
2995
2996 for(i=0; i< type1->fields->len; i++) {
2997 LttField *child1;
2998 LttField *child2;
2999 child1 = &g_array_index(type1->fields, LttField, i);
3000 child2 = &g_array_index(type2->fields, LttField, i);
3001 different = check_fields_compatibility(event_type1,
3002 event_type2, child1, child2);
3003
3004 if(different) break;
3005 }
3006 }
3007 break;
3008 case LTT_NONE:
3009 default:
3010 g_error("check_fields_compatibility : unknown type");
3011 }
3012
3013 end:
3014 return different;
3015 }
3016 #endif //0
3017
3018 #if 0
3019 gint check_fields_compatibility(LttEventType *event_type1,
3020 LttEventType *event_type2,
3021 LttField *field1, LttField *field2)
3022 {
3023 guint different = 0;
3024 guint i;
3025 LttType *type1;
3026 LttType *type2;
3027
3028 if(field1 == NULL) {
3029 if(field2 == NULL) goto end;
3030 else {
3031 different = 1;
3032 goto end;
3033 }
3034 } else if(field2 == NULL) {
3035 different = 1;
3036 goto end;
3037 }
3038
3039 g_assert(field1->fixed_root != FIELD_UNKNOWN);
3040 g_assert(field2->fixed_root != FIELD_UNKNOWN);
3041 g_assert(field1->fixed_parent != FIELD_UNKNOWN);
3042 g_assert(field2->fixed_parent != FIELD_UNKNOWN);
3043 g_assert(field1->fixed_size != FIELD_UNKNOWN);
3044 g_assert(field2->fixed_size != FIELD_UNKNOWN);
3045
3046 type1 = field1->field_type;
3047 type2 = field2->field_type;
3048
3049 if(type1->type_class != type2->type_class) {
3050 different = 1;
3051 goto end;
3052 }
3053 if(type1->element_name != type2->element_name) {
3054 different = 1;
3055 goto end;
3056 }
3057
3058 switch(type1->type_class) {
3059 case LTT_INT_FIXED:
3060 case LTT_UINT_FIXED:
3061 case LTT_POINTER:
3062 case LTT_CHAR:
3063 case LTT_UCHAR:
3064 case LTT_SHORT:
3065 case LTT_USHORT:
3066 case LTT_INT:
3067 case LTT_UINT:
3068 case LTT_FLOAT:
3069 case LTT_POINTER:
3070 case LTT_LONG:
3071 case LTT_ULONG:
3072 case LTT_SIZE_T:
3073 case LTT_SSIZE_T:
3074 case LTT_OFF_T:
3075 if(field1->field_size != field2->field_size) {
3076 different = 1;
3077 goto end;
3078 }
3079 break;
3080 case LTT_ENUM:
3081 if(type1->element_number != type2->element_number) {
3082 different = 1;
3083 goto end;
3084 }
3085 for(i=0;i<type1->element_number;i++) {
3086 if(type1->enum_strings[i] != type2->enum_strings[i]) {
3087 different = 1;
3088 goto end;
3089 }
3090 }
3091 break;
3092 case LTT_SEQUENCE:
3093 /* Two elements : size and child */
3094 g_assert(type1->element_number != type2->element_number);
3095 for(i=0;i<type1->element_number;i++) {
3096 if(check_fields_compatibility(event_type1, event_type2,
3097 field1->child[0], field2->child[0])) {
3098 different = 1;
3099 goto end;
3100 }
3101 }
3102 break;
3103 case LTT_STRING:
3104 break;
3105 case LTT_ARRAY:
3106 if(field1->field_size != field2->field_size) {
3107 different = 1;
3108 goto end;
3109 }
3110 /* Two elements : size and child */
3111 g_assert(type1->element_number != type2->element_number);
3112 for(i=0;i<type1->element_number;i++) {
3113 if(check_fields_compatibility(event_type1, event_type2,
3114 field1->child[0], field2->child[0])) {
3115 different = 1;
3116 goto end;
3117 }
3118 }
3119 break;
3120 case LTT_STRUCT:
3121 case LTT_UNION:
3122 if(type1->element_number != type2->element_number) {
3123 different = 1;
3124 break;
3125 }
3126 for(i=0;i<type1->element_number;i++) {
3127 if(check_fields_compatibility(event_type1, event_type2,
3128 field1->child[0], field2->child[0])) {
3129 different = 1;
3130 goto end;
3131 }
3132 }
3133 break;
3134 }
3135 end:
3136 return different;
3137 }
3138 #endif //0
3139
3140
3141 /*****************************************************************************
3142 *Function name
3143 * ltt_get_int : get an integer number
3144 *Input params
3145 * reverse_byte_order: must we reverse the byte order ?
3146 * size : the size of the integer
3147 * ptr : the data pointer
3148 *Return value
3149 * gint64 : a 64 bits integer
3150 ****************************************************************************/
3151
3152 gint64 ltt_get_int(gboolean reverse_byte_order, gint size, void *data)
3153 {
3154 gint64 val;
3155
3156 switch(size) {
3157 case 1: val = *((gint8*)data); break;
3158 case 2: val = ltt_get_int16(reverse_byte_order, data); break;
3159 case 4: val = ltt_get_int32(reverse_byte_order, data); break;
3160 case 8: val = ltt_get_int64(reverse_byte_order, data); break;
3161 default: val = ltt_get_int64(reverse_byte_order, data);
3162 g_critical("get_int : integer size %d unknown", size);
3163 break;
3164 }
3165
3166 return val;
3167 }
3168
3169 /*****************************************************************************
3170 *Function name
3171 * ltt_get_uint : get an unsigned integer number
3172 *Input params
3173 * reverse_byte_order: must we reverse the byte order ?
3174 * size : the size of the integer
3175 * ptr : the data pointer
3176 *Return value
3177 * guint64 : a 64 bits unsigned integer
3178 ****************************************************************************/
3179
3180 guint64 ltt_get_uint(gboolean reverse_byte_order, gint size, void *data)
3181 {
3182 guint64 val;
3183
3184 switch(size) {
3185 case 1: val = *((gint8*)data); break;
3186 case 2: val = ltt_get_uint16(reverse_byte_order, data); break;
3187 case 4: val = ltt_get_uint32(reverse_byte_order, data); break;
3188 case 8: val = ltt_get_uint64(reverse_byte_order, data); break;
3189 default: val = ltt_get_uint64(reverse_byte_order, data);
3190 g_critical("get_uint : unsigned integer size %d unknown",
3191 size);
3192 break;
3193 }
3194
3195 return val;
3196 }
3197
3198
3199 /* get the node name of the system */
3200
3201 char * ltt_trace_system_description_node_name (LttSystemDescription * s)
3202 {
3203 return s->node_name;
3204 }
3205
3206
3207 /* get the domain name of the system */
3208
3209 char * ltt_trace_system_description_domain_name (LttSystemDescription * s)
3210 {
3211 return s->domain_name;
3212 }
3213
3214
3215 /* get the description of the system */
3216
3217 char * ltt_trace_system_description_description (LttSystemDescription * s)
3218 {
3219 return s->description;
3220 }
3221
3222
3223 /* get the NTP corrected start time of the trace */
3224 LttTime ltt_trace_start_time(LttTrace *t)
3225 {
3226 return t->start_time;
3227 }
3228
3229 /* get the monotonic start time of the trace */
3230 LttTime ltt_trace_start_time_monotonic(LttTrace *t)
3231 {
3232 return t->start_time_from_tsc;
3233 }
3234
3235 LttTracefile *ltt_tracefile_new()
3236 {
3237 LttTracefile *tf;
3238 tf = g_new(LttTracefile, 1);
3239 tf->event.tracefile = tf;
3240 return tf;
3241 }
3242
3243 void ltt_tracefile_destroy(LttTracefile *tf)
3244 {
3245 g_free(tf);
3246 }
3247
3248 void ltt_tracefile_copy(LttTracefile *dest, const LttTracefile *src)
3249 {
3250 *dest = *src;
3251 }
3252
3253 /* Before library loading... */
3254
3255 void init(void)
3256 {
3257 LTT_FACILITY_NAME_HEARTBEAT = g_quark_from_string("heartbeat");
3258 LTT_EVENT_NAME_HEARTBEAT = g_quark_from_string("heartbeat");
3259 LTT_EVENT_NAME_HEARTBEAT_FULL = g_quark_from_string("heartbeat_full");
3260
3261 LTT_TRACEFILE_NAME_FACILITIES = g_quark_from_string("/control/facilities");
3262 }
This page took 0.157328 seconds and 4 git commands to generate.