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