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