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