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