sync frequency of all traces with the first loaded
[lttv.git] / ltt / branches / poly / ltt / tracefile.c
index e38873755766b493e9111e717a747b335303b7e6..2bb0162969c042bf562f4f54e7622ca9eaabf827 100644 (file)
@@ -41,7 +41,6 @@
 #include <stdlib.h>
 
 
-#include "parser.h"
 #include <ltt/ltt.h>
 #include "ltt-private.h"
 #include <ltt/trace.h>
@@ -77,6 +76,8 @@ GQuark LTT_TRACEFILE_NAME_FACILITIES;
 #define PAGE_MASK (~(page_size-1))
 #define PAGE_ALIGN(addr)  (((addr)+page_size-1)&PAGE_MASK)
 
+LttTrace *father_trace = NULL;
+
 /* set the offset of the fields belonging to the event,
    need the information of the archecture */
 //void set_fields_offsets(LttTracefile *tf, LttEventType *event_type);
@@ -116,6 +117,15 @@ static gint map_block(LttTracefile * tf, guint block_num);
 static int ltt_seek_next_event(LttTracefile *tf);
 static void __attribute__((constructor)) init(void);
 static void ltt_update_event_size(LttTracefile *tf);
+
+/* Enable event debugging */
+static int a_event_debug = 0;
+
+void ltt_event_debug(int state)
+{
+  a_event_debug = state;
+}
+
 //void precompute_offsets(LttFacility *fac, LttEventType *event);
 
 #if 0
@@ -265,6 +275,7 @@ int parse_trace_header(void *header, LttTracefile *tf, LttTrace *t)
         tf->tsc_lsb_truncate = vheader->tsc_lsb_truncate;
         tf->tscbits = vheader->tscbits;
         tf->tsc_msb_cutoff = 32 - tf->tsc_lsb_truncate - tf->tscbits;
+        tf->compact_event_bits = 32 - vheader->compact_data_shift;
         tf->tsc_mask = ((1ULL << (tf->tscbits))-1);
         tf->tsc_mask = tf->tsc_mask << tf->tsc_lsb_truncate;
         tf->tsc_mask_next_bit = (1ULL<<(tf->tscbits));
@@ -274,6 +285,13 @@ int parse_trace_header(void *header, LttTracefile *tf, LttTrace *t)
                                          &vheader->start_freq);
           t->freq_scale = ltt_get_uint32(LTT_GET_BO(tf),
                                          &vheader->freq_scale);
+          if(father_trace) {
+            t->start_freq = father_trace->start_freq;
+            t->freq_scale = father_trace->freq_scale;
+          }
+          else {
+            father_trace = t;
+          }
           t->start_tsc = ltt_get_uint64(LTT_GET_BO(tf),
                                         &vheader->start_tsc);
           t->start_monotonic = ltt_get_uint64(LTT_GET_BO(tf),
@@ -288,7 +306,6 @@ int parse_trace_header(void *header, LttTracefile *tf, LttTrace *t)
               (double)t->start_tsc
               * (1000000000.0 / tf->trace->freq_scale)
               / (double)t->start_freq);
-          t->compact_event_bits = 0;
         }
       }
       break;
@@ -360,7 +377,7 @@ gint ltt_tracefile_open(LttTrace *t, gchar * fileName, LttTracefile *tf)
     perror("Error in allocating memory for buffer of tracefile");
     goto close_file;
   }
-  g_assert( ( (guint)tf->buffer.head&(8-1) ) == 0); // make sure it's aligned.
+  g_assert( ( (gulong)tf->buffer.head&(8-1) ) == 0); // make sure it's aligned.
   
   header = (struct ltt_block_start_header*)tf->buffer.head;
   
@@ -661,8 +678,8 @@ void get_absolute_pathname(const gchar *pathname, gchar * abs_pathname)
 int get_tracefile_name_number(gchar *raw_name,
                               GQuark *name,
                               guint *num,
-                              guint *tid,
-                              guint *pgid,
+                              gulong *tid,
+                              gulong *pgid,
                               guint64 *creation)
 {
   guint raw_name_len = strlen(raw_name);
@@ -864,7 +881,7 @@ int open_tracefiles(LttTrace *trace, gchar *root_path, gchar *relative_path)
     
     g_debug("Tracefile file or directory : %s\n", path);
     
-    if(strcmp(rel_path, "/eventdefs") == 0) continue;
+  //  if(strcmp(rel_path, "/eventdefs") == 0) continue;
     
     if(S_ISDIR(stat_buf.st_mode)) {
 
@@ -873,10 +890,12 @@ int open_tracefiles(LttTrace *trace, gchar *root_path, gchar *relative_path)
       if(ret < 0) continue;
     } else if(S_ISREG(stat_buf.st_mode)) {
       GQuark name;
-      guint num, tid, pgid;
+      guint num;
+      gulong tid, pgid;
       guint64 creation;
       GArray *group;
-      num = tid = pgid = 0;
+      num = 0;
+      tid = pgid = 0;
       creation = 0;
       if(get_tracefile_name_number(rel_path, &name, &num, &tid, &pgid, &creation))
         continue; /* invalid name */
@@ -917,7 +936,8 @@ int open_tracefiles(LttTrace *trace, gchar *root_path, gchar *relative_path)
       if(num+1 > old_len)
         group = g_array_set_size(group, num+1);
       g_array_index (group, LttTracefile, num) = tmp_tf;
-
+      g_array_index (group, LttTracefile, num).event.tracefile = 
+        &g_array_index (group, LttTracefile, num);
     }
   }
   
@@ -1171,7 +1191,6 @@ LttTrace *ltt_trace_open(const gchar *pathname)
   DIR *dir;
   struct dirent *entry;
   guint control_found = 0;
-  guint eventdefs_found = 0;
   struct stat stat_buf;
   gchar path[PATH_MAX];
   
@@ -1202,14 +1221,11 @@ LttTrace *ltt_trace_open(const gchar *pathname)
       if(strcmp(entry->d_name, "control") == 0) {
         control_found = 1;
       }
-      if(strcmp(entry->d_name, "eventdefs") == 0) {
-        eventdefs_found = 1;
-      }
     }
   }
   closedir(dir);
   
-  if(!control_found || !eventdefs_found) goto find_error;
+  if(!control_found) goto find_error;
   
   /* Open all the tracefiles */
   if(open_tracefiles(t, abs_path, "")) {
@@ -1740,6 +1756,38 @@ int ltt_tracefile_read_op(LttTracefile *tf)
   return 0;
 }
 
+static void print_debug_event_header(LttEvent *ev, void *start_pos, void *end_pos)
+{
+  unsigned int offset = 0;
+  int i, j;
+
+  g_printf("Event header (tracefile %s offset %llx):\n",
+    g_quark_to_string(ev->tracefile->long_name),
+    ((uint64_t)ev->tracefile->buffer.index * ev->tracefile->buf_size)
+      + (long)start_pos - (long)ev->tracefile->buffer.head);
+
+  while (offset < (long)end_pos - (long)start_pos) {
+    g_printf("%8lx", (long)start_pos - (long)ev->tracefile->buffer.head + offset);
+    g_printf("    ");
+    
+    for (i = 0; i < 4 ; i++) {
+      for (j = 0; j < 4; j++) {
+       if (offset + ((i * 4) + j) <
+               (long)end_pos - (long)start_pos)
+          g_printf("%02hhX",
+            ((char*)start_pos)[offset + ((i * 4) + j)]);
+       else
+         g_printf("  ");
+        g_printf(" ");
+      }
+      if (i < 4)
+      g_printf(" ");
+    }
+    offset+=16;
+    g_printf("\n");
+  }
+}
+
 
 /* same as ltt_tracefile_read, but does not seek to the next event nor call
  * event specific operation. */
@@ -1747,6 +1795,7 @@ int ltt_tracefile_read_update_event(LttTracefile *tf)
 {
   void * pos;
   LttEvent *event;
+  void *pos_aligned;
  
   event = &tf->event;
   pos = tf->buffer.head + event->offset;
@@ -1760,6 +1809,7 @@ int ltt_tracefile_read_update_event(LttTracefile *tf)
     g_assert(tf->has_heartbeat);
     pos += ltt_align((size_t)pos, sizeof(uint32_t), tf->alignment);
   }
+  pos_aligned = pos;
   
   if(tf->has_heartbeat) {
     event->timestamp = ltt_get_uint32(LTT_GET_BO(tf),
@@ -1784,9 +1834,9 @@ int ltt_tracefile_read_update_event(LttTracefile *tf)
       /* We keep the LSB of the previous timestamp, to make sure
        * we never go back */
       event->event_id = event->timestamp >> tf->tscbits;
-      event->event_id = event->event_id & ((1 << tf->trace->compact_event_bits) - 1);
+      event->event_id = event->event_id & ((1 << tf->compact_event_bits) - 1);
       event->compact_data = event->timestamp >> 
-        (tf->trace->compact_event_bits + tf->tscbits);
+        (tf->compact_event_bits + tf->tscbits);
       //printf("tsc bits %u, ev bits %u init data %u\n",
       //  tf->tscbits, tf->trace->compact_event_bits, event->compact_data);
       /* Put the compact data back in original endianness */
@@ -1834,7 +1884,12 @@ int ltt_tracefile_read_update_event(LttTracefile *tf)
     pos += sizeof(guint16);
   } else {
     /* Compact event */
+    event->event_size = 0xFFFF;
   }
+
+  if (a_event_debug)
+    print_debug_event_header(event, pos_aligned, pos);
+
   /* Align the head */
   if(!tf->compact)
     pos += ltt_align((size_t)pos, tf->trace->arch_size, tf->alignment);
@@ -1889,7 +1944,7 @@ gint map_block(LttTracefile * tf, guint block_num)
     g_assert(0);
     goto map_error;
   }
-  g_assert( ( (guint)tf->buffer.head&(8-1) ) == 0); // make sure it's aligned.
+  g_assert( ( (gulong)tf->buffer.head&(8-1) ) == 0); // make sure it's aligned.
   
 
   tf->buffer.index = block_num;
@@ -1982,6 +2037,54 @@ map_error:
 
 }
 
+static void print_debug_event_data(LttEvent *ev)
+{
+  unsigned int offset = 0;
+  int i, j;
+
+  if (!max(ev->event_size, ev->data_size))
+    return;
+
+  g_printf("Event data (tracefile %s offset %llx):\n",
+    g_quark_to_string(ev->tracefile->long_name),
+    ((uint64_t)ev->tracefile->buffer.index * ev->tracefile->buf_size)
+      + (long)ev->data - (long)ev->tracefile->buffer.head);
+
+  while (offset < max(ev->event_size, ev->data_size)) {
+    g_printf("%8lx", (long)ev->data + offset
+      - (long)ev->tracefile->buffer.head);
+    g_printf("    ");
+    
+    for (i = 0; i < 4 ; i++) {
+      for (j = 0; j < 4; j++) {
+       if (offset + ((i * 4) + j) < max(ev->event_size, ev->data_size))
+          g_printf("%02hhX", ((char*)ev->data)[offset + ((i * 4) + j)]);
+       else
+         g_printf("  ");
+        g_printf(" ");
+      }
+      if (i < 4)
+      g_printf(" ");
+    }
+
+    g_printf("    ");
+
+    for (i = 0; i < 4; i++) {
+      for (j = 0; j < 4; j++) {
+       if (offset + ((i * 4) + j) < max(ev->event_size, ev->data_size)) {
+         if (isprint(((char*)ev->data)[offset + ((i * 4) + j)]))
+            g_printf("%c", ((char*)ev->data)[offset + ((i * 4) + j)]);
+         else
+            g_printf(".");
+       } else
+         g_printf("  ");
+      }
+    }
+    offset+=16;
+    g_printf("\n");
+  }
+}
+
 /* It will update the fields offsets too */
 void ltt_update_event_size(LttTracefile *tf)
 {
@@ -1992,7 +2095,7 @@ void ltt_update_event_size(LttTracefile *tf)
   switch((enum marker_id)tf->event.event_id) {
     case MARKER_ID_SET_MARKER_ID:
       size = strlen((char*)tf->event.data) + 1;
-      //g_debug("marker %s id set", (char*)tf->event.data);
+      g_debug("marker %s id set", (char*)tf->event.data);
       size += ltt_align(size, sizeof(guint16), tf->alignment);
       size += sizeof(guint16);
       size += sizeof(guint8);
@@ -2002,15 +2105,15 @@ void ltt_update_event_size(LttTracefile *tf)
       size += sizeof(guint8);
       break;
     case MARKER_ID_SET_MARKER_FORMAT:
-      //g_debug("marker %s format set", (char*)tf->event.data);
+      g_debug("marker %s format set", (char*)tf->event.data);
       size = strlen((char*)tf->event.data) + 1;
       size += strlen((char*)tf->event.data + size) + 1;
       break;
     case MARKER_ID_HEARTBEAT_32:
-      //g_debug("Update Event heartbeat 32 bits");
+      g_debug("Update Event heartbeat 32 bits");
       break;
     case MARKER_ID_HEARTBEAT_64:
-      //g_debug("Update Event heartbeat 64 bits");
+      g_debug("Update Event heartbeat 64 bits");
       tscdata = (char*)(tf->event.data);
       tf->event.tsc = ltt_get_uint64(LTT_GET_BO(tf), tscdata);
       tf->buffer.tsc = tf->event.tsc;
@@ -2021,6 +2124,7 @@ void ltt_update_event_size(LttTracefile *tf)
   }
 
   info = marker_get_info_from_id(tf->trace, tf->event.event_id);
+
   if (tf->event.event_id >= MARKER_CORE_IDS)
     g_assert(info != NULL);
 
@@ -2042,6 +2146,20 @@ void ltt_update_event_size(LttTracefile *tf)
     /* Event size too big to fit in the event size field */
     tf->event.event_size = tf->event.data_size;
   }
+
+  if (a_event_debug)
+    print_debug_event_data(&tf->event);
+
+  /* Having a marker load or marker format event out of the facilities
+   * tracefiles is a serious bug. */
+  switch((enum marker_id)tf->event.event_id) {
+    case MARKER_ID_SET_MARKER_ID:
+    case MARKER_ID_SET_MARKER_FORMAT:
+      if (tf->name != g_quark_from_string("/control/facilities"))
+        g_error("Trace inconsistency : metadata event found in data "
+                "tracefile %s", g_quark_to_string(tf->long_name));
+  }
+
   if (tf->event.data_size != tf->event.event_size) {
     struct marker_info *info = marker_get_info_from_id(tf->trace,
                                                        tf->event.event_id);
@@ -3116,7 +3234,10 @@ LttTime ltt_trace_start_time_monotonic(LttTrace *t)
 
 LttTracefile *ltt_tracefile_new()
 {
-  return g_new(LttTracefile, 1);
+  LttTracefile *tf;
+  tf = g_new(LttTracefile, 1);
+  tf->event.tracefile = tf;
+  return tf;
 }
 
 void ltt_tracefile_destroy(LttTracefile *tf)
This page took 0.02692 seconds and 4 git commands to generate.