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