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