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