free bugfix
[lttv.git] / ltt / branches / poly / ltt / tracefile.c
index 0a649b5e6e3ff312fde89ec4b6255e0a3bcc8a83..470eb5e09a619570bffa7a6e57d2a456fe3cb02c 100644 (file)
@@ -1,6 +1,8 @@
 /* This file is part of the Linux Trace Toolkit viewer
- * Copyright (C) 2003-2004 Xiangxiu Yang, Mathieu Desnoyers
+ * Copyright (C) 2005 Mathieu Desnoyers
  *
+ * Complete rewrite from the original version made by XangXiu Yang.
+ * 
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License Version 2 as
  * published by the Free Software Foundation;
  * MA 02111-1307, USA.
  */
 
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
 #include <stdio.h>
 #include <fcntl.h>
 #include <string.h>
@@ -26,6 +32,8 @@
 #include <unistd.h>
 #include <math.h>
 #include <glib.h>
+#include <malloc.h>
+#include <sys/mman.h>
 
 // For realpath
 #include <limits.h>
 #include <ltt/type.h>
 #include <ltt/ltt-types.h>
 
+
+/* Facility names used in this file */
+
+GQuark LTT_FACILITY_NAME_HEARTBEAT,
+       LTT_EVENT_NAME_HEARTBEAT;
+GQuark LTT_TRACEFILE_NAME_FACILITIES;
+
 #ifndef g_open
 #define g_open open
 #endif
 
 
-#define DIR_NAME_SIZE 256
 #define __UNUSED__ __attribute__((__unused__))
 
 #define g_info(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, format)
+
+#ifndef g_debug
 #define g_debug(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format)
+#endif
 
 #define g_close close
 
-/* obtain the time of an event */
-
-static inline LttTime getEventTime(LttTracefile * tf);
-
+/* Those macros must be called from within a function where page_size is a known
+ * variable */
+#define PAGE_MASK (~(page_size-1))
+#define PAGE_ALIGN(addr)  (((addr)+page_size-1)&PAGE_MASK)
 
 /* set the offset of the fields belonging to the event,
    need the information of the archecture */
-void setFieldsOffset(LttTracefile *tf,LttEventType *evT,void *evD,LttTrace *t);
+void set_fields_offsets(LttTracefile *tf, LttEventType *event_type);
+//size_t get_fields_offsets(LttTracefile *tf, LttEventType *event_type, void *data);
 
-/* get the size of the field type according to the archtecture's
-   size and endian type(info of the archecture) */
-static inline gint getFieldtypeSize(LttTracefile * tf,
-         LttEventType * evT, gint offsetRoot,
-                    gint offsetParent, LttField *fld, void *evD, LttTrace* t);
+/* get the size of the field type according to 
+ * The facility size information. */
+static inline void preset_field_type_size(LttTracefile *tf,
+    LttEventType *event_type,
+    off_t offset_root, off_t offset_parent,
+    enum field_status *fixed_root, enum field_status *fixed_parent,
+    LttField *field);
 
-/* read a fixed size or a block information from the file (fd) */
-int readFile(int fd, void * buf, size_t size, gchar * mesg);
-int readBlock(LttTracefile * tf, int whichBlock);
+/* map a fixed size or a block information from the file (fd) */
+static gint map_block(LttTracefile * tf, guint block_num);
 
-/* calculate cycles per nsec for current block */
-void getCyclePerNsec(LttTracefile * t);
-
-/* reinitialize the info of the block which is already in the buffer */
-void updateTracefile(LttTracefile * tf);
+/* calculate nsec per cycles for current block */
+#if 0
+static guint32 calc_nsecs_per_cycle(LttTracefile * t);
+static guint64 cycles_2_ns(LttTracefile *tf, guint64 cycles);
+#endif //0
 
 /* go to the next event */
-int skipEvent(LttTracefile * t);
+static int ltt_seek_next_event(LttTracefile *tf);
 
+void ltt_update_event_size(LttTracefile *tf);
 
+#if 0
 /* Functions to parse system.xml file (using glib xml parser) */
 static void parser_start_element (GMarkupParseContext  __UNUSED__ *context,
                                  const gchar          *element_name,
@@ -157,6 +178,107 @@ static void  parser_characters   (GMarkupParseContext __UNUSED__ *context,
   LttSystemDescription* des = (LttSystemDescription* )user_data;
   des->description = g_strdup(text);
 }
+#endif //0
+LttFacility *ltt_trace_get_facility_by_num(LttTrace *t,
+    guint num)
+{
+  g_assert(num < t->facilities_by_num->len);
+  
+  return &g_array_index(t->facilities_by_num, LttFacility, num);
+
+}
+
+guint ltt_trace_get_num_cpu(LttTrace *t)
+{
+  return t->num_cpu;
+}
+
+
+/* trace can be NULL
+ *
+ * Return value : 0 success, 1 bad tracefile
+ */
+int parse_trace_header(void *header, LttTracefile *tf, LttTrace *t)
+{
+  guint32 *magic_number = (guint32*)header;
+  struct ltt_trace_header_any *any = (struct ltt_trace_header_any *)header;
+
+  if(*magic_number == LTT_MAGIC_NUMBER)
+    tf->reverse_bo = 0;
+  else if(*magic_number == LTT_REV_MAGIC_NUMBER)
+    tf->reverse_bo = 1;
+  else  /* invalid magic number, bad tracefile ! */
+    return 1;
+    
+  /* Get float byte order : might be different from int byte order
+   * (or is set to 0 if the trace has no float (kernel trace)) */
+  tf->float_word_order = any->float_word_order;
+
+  if(t) {
+    t->arch_type = ltt_get_uint32(LTT_GET_BO(tf),
+                          &any->arch_type);
+    t->arch_variant = ltt_get_uint32(LTT_GET_BO(tf),
+        &any->arch_variant);
+    t->arch_size = any->arch_size;
+    t->ltt_major_version = any->major_version;
+    t->ltt_minor_version = any->minor_version;
+    t->flight_recorder = any->flight_recorder;
+    t->has_heartbeat = any->has_heartbeat;
+    t->has_alignment = any->has_alignment;
+    t->has_tsc = any->has_tsc;
+  }
+
+  switch(any->major_version) {
+
+  case 0:
+    switch(any->minor_version) {
+    case 3:
+      {
+        tf->buffer_header_size =
+         sizeof(struct ltt_block_start_header) 
+            + sizeof(struct ltt_trace_header_0_3);
+        g_warning("Unsupported trace version : %hhu.%hhu",
+              any->major_version, any->minor_version);
+        return 1;
+      }
+      break;
+    case 4:
+      {
+        struct ltt_trace_header_0_4 *vheader =
+          (struct ltt_trace_header_0_4 *)header;
+        tf->buffer_header_size =
+         sizeof(struct ltt_block_start_header) 
+            + sizeof(struct ltt_trace_header_0_4);
+        if(t) {
+          t->start_freq = ltt_get_uint64(LTT_GET_BO(tf),
+                                         &vheader->start_freq);
+          t->start_tsc = ltt_get_uint64(LTT_GET_BO(tf),
+                                        &vheader->start_tsc);
+          t->start_monotonic = ltt_get_uint64(LTT_GET_BO(tf),
+                                              &vheader->start_monotonic);
+          t->start_time = ltt_get_time(LTT_GET_BO(tf),
+                                       &vheader->start_time);
+        }
+      }
+      break;
+    default:
+      g_warning("Unsupported trace version : %hhu.%hhu",
+            any->major_version, any->minor_version);
+      return 1;
+    }
+    break;
+
+  default:
+    g_warning("Unsupported trace version : %hhu.%hhu",
+            any->major_version, any->minor_version);
+    return 1;
+  }
+
+
+  return 0;
+}
+
 
 
 /*****************************************************************************
@@ -165,62 +287,106 @@ static void  parser_characters   (GMarkupParseContext __UNUSED__ *context,
  *Input params
  *    t                  : the trace containing the tracefile
  *    fileName           : path name of the trace file
+ *    tf                 : the tracefile structure
  *Return value
- *                       : a pointer to a tracefile
+ *                       : 0 for success, -1 otherwise.
  ****************************************************************************/ 
 
-LttTracefile* ltt_tracefile_open(LttTrace * t, gchar * fileName)
+gint ltt_tracefile_open(LttTrace *t, gchar * fileName, LttTracefile *tf)
 {
-  LttTracefile * tf;
   struct stat    lTDFStat;    /* Trace data file status */
-
-  tf = g_new(LttTracefile, 1);  
+  struct ltt_block_start_header *header;
+  int page_size = getpagesize();
 
   //open the file
-  tf->name = g_strdup(fileName);
+  tf->long_name = g_quark_from_string(fileName);
   tf->trace = t;
-  tf->fd = g_open(fileName, O_RDONLY, 0);
+  tf->fd = open(fileName, O_RDONLY);
   if(tf->fd < 0){
     g_warning("Unable to open input data file %s\n", fileName);
-    g_free(tf->name);
-    g_free(tf);
-    return NULL;
+    goto end;
   }
  
   // Get the file's status 
   if(fstat(tf->fd, &lTDFStat) < 0){
     g_warning("Unable to get the status of the input data file %s\n", fileName);
-    g_free(tf->name);
-    g_close(tf->fd);
-    g_free(tf);
-    return NULL;
+    goto close_file;
   }
 
   // Is the file large enough to contain a trace 
-  if(lTDFStat.st_size < (off_t)(sizeof(BlockStart) + EVENT_HEADER_SIZE)){
+  if(lTDFStat.st_size <
+      (off_t)(sizeof(struct ltt_block_start_header) 
+                     + sizeof(struct ltt_trace_header_any))){
     g_print("The input data file %s does not contain a trace\n", fileName);
-    g_free(tf->name);
-    g_close(tf->fd);
-    g_free(tf);
-    return NULL;
+    goto close_file;
+  }
+  
+  /* Temporarily map the buffer start header to get trace information */
+  /* Multiple of pages aligned head */
+  tf->buffer.head = mmap(0,
+      PAGE_ALIGN(sizeof(struct ltt_block_start_header)
+          + sizeof(struct ltt_trace_header_any)), PROT_READ, 
+      MAP_PRIVATE, tf->fd, 0);
+  if(tf->buffer.head == MAP_FAILED) {
+    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.
   
+  header = (struct ltt_block_start_header*)tf->buffer.head;
+  
+  if(parse_trace_header(header->trace, tf, NULL)) {
+    g_warning("parse_trace_header error");
+    goto unmap_file;
+  }
+    
   //store the size of the file
   tf->file_size = lTDFStat.st_size;
-  tf->block_size = t->system_description->ltt_block_size;
-  tf->block_number = tf->file_size / tf->block_size;
-  tf->which_block = 0;
-
-  //allocate memory to contain the info of a block
-  tf->buffer = (void *) g_new(gchar, t->system_description->ltt_block_size);
+  tf->buf_size = ltt_get_uint32(LTT_GET_BO(tf), &header->buf_size);
+  tf->num_blocks = tf->file_size / tf->buf_size;
+
+  if(munmap(tf->buffer.head,
+        PAGE_ALIGN(sizeof(struct ltt_block_start_header)
+            + sizeof(struct ltt_trace_header_any)))) {
+    g_warning("unmap size : %u\n",
+        PAGE_ALIGN(sizeof(struct ltt_block_start_header)
+            + sizeof(struct ltt_trace_header_any)));
+    perror("munmap error");
+    g_assert(0);
+  }
+  tf->buffer.head = NULL;
 
   //read the first block
-  if(readBlock(tf,1)) exit(1);
+  if(map_block(tf,0)) {
+    perror("Cannot map block for tracefile");
+    goto close_file;
+  }
+  
+  return 0;
 
-  return tf;
+  /* Error */
+unmap_file:
+  if(munmap(tf->buffer.head,
+        PAGE_ALIGN(sizeof(struct ltt_block_start_header)
+            + sizeof(struct ltt_trace_header_any)))) {
+    g_warning("unmap size : %u\n",
+        PAGE_ALIGN(sizeof(struct ltt_block_start_header)
+            + sizeof(struct ltt_trace_header_any)));
+    perror("munmap error");
+    g_assert(0);
+  }
+close_file:
+  close(tf->fd);
+end:
+  return -1;
 }
 
+LttTrace *ltt_tracefile_get_trace(LttTracefile *tf)
+{
+  return tf->trace;
+}
 
+#if 0
 /*****************************************************************************
  *Open control and per cpu tracefiles
  ****************************************************************************/
@@ -286,6 +452,7 @@ gint ltt_tracefile_open_control(LttTrace *t, gchar * control_name)
   }
   return 0;
 }
+#endif //0
 
 /*****************************************************************************
  *Function name
@@ -296,16 +463,24 @@ gint ltt_tracefile_open_control(LttTrace *t, gchar * control_name)
 
 void ltt_tracefile_close(LttTracefile *t)
 {
-  g_free(t->name);
-  g_free(t->buffer);
+  int page_size = getpagesize();
+
+  if(t->buffer.head != NULL)
+    if(munmap(t->buffer.head, PAGE_ALIGN(t->buf_size))) {
+    g_warning("unmap size : %u\n",
+        PAGE_ALIGN(t->buf_size));
+    perror("munmap error");
+    g_assert(0);
+  }
+
   close(t->fd);
-  g_free(t);
 }
 
 
 /*****************************************************************************
  *Get system information
  ****************************************************************************/
+#if 0
 gint getSystemInfo(LttSystemDescription* des, gchar * pathname)
 {
   int fd;
@@ -372,11 +547,12 @@ gint getSystemInfo(LttSystemDescription* des, gchar * pathname)
   g_free(buf);
   return 0;
 }
+#endif //0
 
 /*****************************************************************************
  *The following functions get facility/tracefile information
  ****************************************************************************/
-
+#if 0
 gint getFacilityInfo(LttTrace *t, gchar* eventdefs)
 {
   GDir * dir;
@@ -412,63 +588,7 @@ gint getFacilityInfo(LttTrace *t, gchar* eventdefs)
   }
   return 0;
 }
-
-gint getControlFileInfo(LttTrace *t, gchar* control)
-{
-  GDir * dir;
-  const gchar *name;
-  gchar fullname[DIR_NAME_SIZE];
-  GError * error = NULL;
-
-  dir = g_dir_open(control, 0, &error);
-
-  if(error != NULL) {
-    g_warning("Can not open directory: %s, %s\n", control, error->message);
-    g_error_free(error);
-    return -1;
-  }
-
-  while((name = g_dir_read_name(dir)) != NULL){
-    if(strcmp(name,"facilities") != 0 &&
-       strcmp(name,"interrupts") != 0 &&
-       strcmp(name,"processes") != 0) continue;
-    
-    strcpy(fullname,control);
-    strcat(fullname,name);
-    if(ltt_tracefile_open_control(t,fullname)) {
-      g_dir_close(dir);
-      return -1;
-    }
-  }  
-  g_dir_close(dir);
-  return 0;
-}
-
-gint getCpuFileInfo(LttTrace *t, char* cpu)
-{
-  GDir * dir;
-  const gchar * name;
-  gchar fullname[DIR_NAME_SIZE];
-  GError * error = NULL;
-
-  dir = g_dir_open(cpu, 0, &error);
-
-  if(error != NULL) {
-    g_warning("Can not open directory: %s, %s\n", cpu, error->message);
-    g_error_free(error);
-    return -1;
-  }
-
-  while((name = g_dir_read_name(dir)) != NULL){
-    if(strcmp(name,".svn") != 0){     /* . and .. already excluded */
-      strcpy(fullname,cpu);
-      strcat(fullname,name);
-      ltt_tracefile_open_cpu(t,fullname);
-    }else continue;
-  }  
-  g_dir_close(dir);
-  return 0;
-}
+#endif //0
 
 /*****************************************************************************
  *A trace is specified as a pathname to the directory containing all the
@@ -504,389 +624,718 @@ void get_absolute_pathname(const gchar *pathname, gchar * abs_pathname)
   return;
 }
 
-LttTrace *ltt_trace_open(const gchar *pathname)
+/* Search for something like : .*_.*
+ *
+ * The left side is the name, the right side is the number.
+ */
+
+int get_tracefile_name_number(const gchar *raw_name,
+                              GQuark *name,
+                              guint *num)
 {
-  LttTrace  * t;
-  LttSystemDescription * sys_description;
-  gchar eventdefs[DIR_NAME_SIZE];
-  gchar info[DIR_NAME_SIZE];
-  gchar control[DIR_NAME_SIZE];
-  gchar cpu[DIR_NAME_SIZE];
-  gchar tmp[DIR_NAME_SIZE];
-  gchar abs_path[DIR_NAME_SIZE];
-  gboolean has_slash = FALSE;
+  guint raw_name_len = strlen(raw_name);
+  gchar char_name[PATH_MAX];
+  int i;
+  int underscore_pos;
+  long int cpu_num;
+  gchar *endptr;
+
+  for(i=raw_name_len-1;i>=0;i--) {
+    if(raw_name[i] == '_') break;
+  }
+  if(i==0)  /* Either not found or name length is 0 */
+    return -1;
+  underscore_pos = i;
 
-  get_absolute_pathname(pathname, abs_path);
-  //establish the pathname to different directories
-  if(abs_path[strlen(abs_path)-1] == '/')has_slash = TRUE;
-  strcpy(eventdefs,abs_path);
-  if(!has_slash)strcat(eventdefs,"/");
-  strcat(eventdefs,"eventdefs/");
-
-  strcpy(info,abs_path);
-  if(!has_slash)strcat(info,"/");
-  strcat(info,"info/");
-
-  strcpy(control,abs_path);
-  if(!has_slash)strcat(control,"/");
-  strcat(control,"control/");
-
-  strcpy(cpu,abs_path);
-  if(!has_slash)strcat(cpu,"/");
-  strcat(cpu,"cpu/");
-
-  //new trace
-  sys_description = g_new(LttSystemDescription, 1);  
-  t               = g_new(LttTrace, 1);
-  t->pathname     = g_strdup(abs_path);
-  t->facility_number          = 0;
-  t->control_tracefile_number = 0;
-  t->per_cpu_tracefile_number = 0;
-  t->system_description = sys_description;
-  t->control_tracefiles = g_ptr_array_new();
-  t->per_cpu_tracefiles = g_ptr_array_new();
-  t->facilities         = g_ptr_array_new();
-  //getDataEndianType(&(t->my_arch_size), &(t->my_arch_endian));
+  cpu_num = strtol(raw_name+underscore_pos+1, &endptr, 10);
+
+  if(endptr == raw_name+underscore_pos+1)
+    return -1; /* No digit */
+  if(cpu_num == LONG_MIN || cpu_num == LONG_MAX)
+    return -1; /* underflow / overflow */
   
-  //get system description  
-  strcpy(tmp,info);
-  strcat(tmp,"system.xml");
-  if(getSystemInfo(sys_description, tmp)) {
-    g_ptr_array_free(t->facilities, TRUE);
-    g_ptr_array_free(t->per_cpu_tracefiles, TRUE);
-    g_ptr_array_free(t->control_tracefiles, TRUE);
-    g_free(sys_description);
-    g_free(t->pathname);
-    g_free(t);
-    return NULL;
-  }
+  strncpy(char_name, raw_name, underscore_pos);
   
-  /* Set the reverse byte order between trace and reader */
-  if(sys_description->endian == LTT_LITTLE_ENDIAN 
-          && G_BYTE_ORDER != G_LITTLE_ENDIAN) {
-    t->reverse_byte_order = 1;
-  } else if(sys_description->endian == LTT_BIG_ENDIAN
-          && G_BYTE_ORDER != G_BIG_ENDIAN) {
-    t->reverse_byte_order = 1;
-  } else t->reverse_byte_order = 0;
-
-  //get facilities info
-  if(getFacilityInfo(t,eventdefs)) {
-    g_ptr_array_free(t->facilities, TRUE);
-    g_ptr_array_free(t->per_cpu_tracefiles, TRUE);
-    g_ptr_array_free(t->control_tracefiles, TRUE);
-    g_free(sys_description);
-    g_free(t->pathname);
-    g_free(t);
-    return NULL;
-  }
+  char_name[underscore_pos] = '\0';
   
-  //get control tracefile info
-  getControlFileInfo(t,control);
-  /*
-  if(getControlFileInfo(t,control)) {
-    g_ptr_array_free(t->facilities, TRUE);
-    g_ptr_array_free(t->per_cpu_tracefiles, TRUE);
-    g_ptr_array_free(t->control_tracefiles, TRUE);
-    g_free(sys_description);
-    g_free(t->pathname);
-    g_free(t);
-    return NULL;
-  }*/ // With fatal error
-
-  //get cpu tracefile info
-  if(getCpuFileInfo(t,cpu)) {
-    g_ptr_array_free(t->facilities, TRUE);
-    g_ptr_array_free(t->per_cpu_tracefiles, TRUE);
-    g_ptr_array_free(t->control_tracefiles, TRUE);
-    g_free(sys_description);
-    g_free(t->pathname);
-    g_free(t);
-    return NULL;
-  }
-
-  return t;
+  *name = g_quark_from_string(char_name);
+  *num = cpu_num;
+  
+  return 0;
 }
 
-char * ltt_trace_name(LttTrace *t)
+
+GData **ltt_trace_get_tracefiles_groups(LttTrace *trace)
 {
-  return t->pathname;
+  return &trace->tracefiles;
 }
 
 
-/******************************************************************************
- * When we copy a trace, we want all the opening actions to happen again :
- * the trace will be reopened and totally independant from the original.
- * That's why we call ltt_trace_open.
- *****************************************************************************/
-LttTrace *ltt_trace_copy(LttTrace *self)
+void compute_tracefile_group(GQuark key_id,
+                             GArray *group,
+                             struct compute_tracefile_group_args *args)
 {
-  return ltt_trace_open(self->pathname);
+  int i;
+  LttTracefile *tf;
+
+  for(i=0; i<group->len; i++) {
+    tf = &g_array_index (group, LttTracefile, i);
+    if(tf->cpu_online)
+      args->func(tf, args->func_args);
+  }
 }
 
-void ltt_trace_close(LttTrace *t)
-{
-  unsigned int i;
-  LttTracefile * tf;
-  LttFacility * f;
 
-  g_free(t->pathname);
-  //free system_description
-  g_free(t->system_description->description);
-  g_free(t->system_description->node_name);
-  g_free(t->system_description->domain_name);
-  g_free(t->system_description->kernel_name);
-  g_free(t->system_description->kernel_release);
-  g_free(t->system_description->kernel_version);
-  g_free(t->system_description->machine);
-  g_free(t->system_description->processor);
-  g_free(t->system_description->hardware_platform);
-  g_free(t->system_description->operating_system);
-  g_free(t->system_description);
-
-  //free control_tracefiles
-  for(i=0;i<t->control_tracefile_number;i++){
-    tf = (LttTracefile*)g_ptr_array_index(t->control_tracefiles,i);
-    ltt_tracefile_close(tf);
+void ltt_tracefile_group_destroy(gpointer data)
+{
+  GArray *group = (GArray *)data;
+  int i;
+  LttTracefile *tf;
+
+  for(i=0; i<group->len; i++) {
+    tf = &g_array_index (group, LttTracefile, i);
+    if(tf->cpu_online)
+      ltt_tracefile_close(tf);
   }
-  g_ptr_array_free(t->control_tracefiles, TRUE);
+  g_array_free(group, TRUE);
+}
 
-  //free per_cpu_tracefiles
-  for(i=0;i<t->per_cpu_tracefile_number;i++){
-    tf = (LttTracefile*)g_ptr_array_index(t->per_cpu_tracefiles,i);
-    ltt_tracefile_close(tf);
-  }
-  g_ptr_array_free(t->per_cpu_tracefiles, TRUE);
+gboolean ltt_tracefile_group_has_cpu_online(gpointer data)
+{
+  GArray *group = (GArray *)data;
+  int i;
+  LttTracefile *tf;
 
-  //free facilities
-  for(i=0;i<t->facility_number;i++){
-    f = (LttFacility*)g_ptr_array_index(t->facilities,i);
-    ltt_facility_close(f);
+  for(i=0; i<group->len; i++) {
+    tf = &g_array_index (group, LttTracefile, i);
+    if(tf->cpu_online) return 1;
   }
-  g_ptr_array_free(t->facilities, TRUE);
-
-  g_free(t);
-  g_blow_chunks();
+  return 0;
 }
 
 
-/*****************************************************************************
- *Get the system description of the trace
- ****************************************************************************/
+/* Open each tracefile under a specific directory. Put them in a
+ * GData : permits to access them using their tracefile group pathname.
+ * i.e. access control/modules tracefile group by index :
+ * "control/module".
+ * 
+ * relative path is the path relative to the trace root
+ * root path is the full path
+ *
+ * A tracefile group is simply an array where all the per cpu tracefiles sits.
+ */
 
-LttSystemDescription *ltt_trace_system_description(LttTrace *t)
+static int open_tracefiles(LttTrace *trace, gchar *root_path,
+    gchar *relative_path)
 {
-  return t->system_description;
-}
+       DIR *dir = opendir(root_path);
+       struct dirent *entry;
+       struct stat stat_buf;
+       int ret;
+  
+       gchar path[PATH_MAX];
+       int path_len;
+       gchar *path_ptr;
+
+  int rel_path_len;
+  gchar rel_path[PATH_MAX];
+  gchar *rel_path_ptr;
+  LttTracefile tmp_tf;
+
+       if(dir == NULL) {
+               perror(root_path);
+               return ENOENT;
+       }
 
-/*****************************************************************************
- * The following functions discover the facilities of the trace
- ****************************************************************************/
+       strncpy(path, root_path, PATH_MAX-1);
+       path_len = strlen(path);
+       path[path_len] = '/';
+       path_len++;
+       path_ptr = path + path_len;
+
+  strncpy(rel_path, relative_path, PATH_MAX-1);
+  rel_path_len = strlen(rel_path);
+  rel_path[rel_path_len] = '/';
+  rel_path_len++;
+  rel_path_ptr = rel_path + rel_path_len;
+  
+       while((entry = readdir(dir)) != NULL) {
+
+               if(entry->d_name[0] == '.') continue;
+               
+               strncpy(path_ptr, entry->d_name, PATH_MAX - path_len);
+               strncpy(rel_path_ptr, entry->d_name, PATH_MAX - rel_path_len);
+               
+               ret = stat(path, &stat_buf);
+               if(ret == -1) {
+                       perror(path);
+                       continue;
+               }
+               
+               g_debug("Tracefile file or directory : %s\n", path);
+               
+    if(strcmp(rel_path, "/eventdefs") == 0) continue;
+    
+               if(S_ISDIR(stat_buf.st_mode)) {
+
+                       g_debug("Entering subdirectory...\n");
+                       ret = open_tracefiles(trace, path, rel_path);
+                       if(ret < 0) continue;
+               } else if(S_ISREG(stat_buf.st_mode)) {
+                       GQuark name;
+      guint num;
+      GArray *group;
+      
+      if(get_tracefile_name_number(rel_path, &name, &num))
+        continue; /* invalid name */
+      
+                       g_debug("Opening file.\n");
+      if(ltt_tracefile_open(trace, path, &tmp_tf)) {
+        g_info("Error opening tracefile %s", path);
 
-unsigned ltt_trace_facility_number(LttTrace *t)
-{
-  return (unsigned)(t->facility_number);
-}
+        continue; /* error opening the tracefile : bad magic number ? */
+      }
 
-LttFacility *ltt_trace_facility_get(LttTrace *t, unsigned i)
-{
-  return (LttFacility*)g_ptr_array_index(t->facilities, i);
-}
+      g_debug("Tracefile name is %s and number is %u", 
+          g_quark_to_string(name), num);
+      
+      tmp_tf.cpu_online = 1;
+      tmp_tf.cpu_num = num;
+      tmp_tf.name = name;
+
+      group = g_datalist_id_get_data(&trace->tracefiles, name);
+      if(group == NULL) {
+        /* Elements are automatically cleared when the array is allocated.
+         * It makes the cpu_online variable set to 0 : cpu offline, by default.
+         */
+        group = g_array_sized_new (FALSE, TRUE, sizeof(LttTracefile), 10);
+        g_datalist_id_set_data_full(&trace->tracefiles, name,
+                                 group, ltt_tracefile_group_destroy);
+      }
 
-/*****************************************************************************
- *Function name
- *    ltt_trace_facility_find : find facilities in the trace
- *Input params
- *    t                       : the trace 
- *    name                    : facility name
- *Output params
- *    position                : position of the facility in the trace
- *Return value
- *                            : the number of facilities
- ****************************************************************************/
+      /* Add the per cpu tracefile to the named group */
+      unsigned int old_len = group->len;
+      if(num+1 > old_len)
+        group = g_array_set_size(group, num+1);
+      g_array_index (group, LttTracefile, num) = tmp_tf;
 
-unsigned ltt_trace_facility_find(LttTrace *t, char *name, unsigned *position)
-{
-  unsigned int i, count=0;
-  LttFacility * f;
-  for(i=0;i<t->facility_number;i++){
-    f = (LttFacility*)g_ptr_array_index(t->facilities, i);
-    if(strcmp(f->name,name)==0){
-      count++;
-      if(count==1) *position = i;      
-    }else{
-      if(count) break;
-    }
-  }
-  return count;
+               }
+       }
+       
+       closedir(dir);
+
+       return 0;
 }
 
-/*****************************************************************************
- * Functions to discover all the event types in the trace 
- ****************************************************************************/
+/* ltt_get_facility_description
+ *
+ * Opens the file corresponding to the requested facility (identified by fac_id
+ * and checksum).
+ *
+ * The name searched is : %trace root%/eventdefs/facname_checksum.xml
+ *
+ * Returns 0 on success, or 1 on failure.
+ */
 
-unsigned ltt_trace_eventtype_number(LttTrace *t)
+static int ltt_get_facility_description(LttFacility *f, 
+                                        LttTrace *t,
+                                        LttTracefile *fac_tf)
 {
-  unsigned int i;
-  unsigned count = 0;
-  unsigned int num = t->facility_number;
-  LttFacility * f;
+  char desc_file_name[PATH_MAX];
+  const gchar *text;
+  guint textlen;
+  gint err;
+
+  text = g_quark_to_string(t->pathname);
+  textlen = strlen(text);
   
-  for(i=0;i<num;i++){
-    f = (LttFacility*)g_ptr_array_index(t->facilities, i);
-    count += f->event_number;
-  }
-  return count;
-}
+  if(textlen >= PATH_MAX) goto name_error;
+  strcpy(desc_file_name, text);
 
-/* FIXME : performances could be improved with a better design for this
- * function : sequential search through a container has never been the
- * best on the critical path. */
-LttFacility * ltt_trace_facility_by_id(LttTrace * trace, unsigned id)
-{
-  LttFacility * facility = NULL;
-  unsigned int i;
-  unsigned int num = trace->facility_number;
-  GPtrArray *facilities = trace->facilities;
+  text = "/eventdefs/";
+  textlen+=strlen(text);
+  if(textlen >= PATH_MAX) goto name_error;
+  strcat(desc_file_name, text);
+  
+  text = g_quark_to_string(f->name);
+  textlen+=strlen(text);
+  if(textlen >= PATH_MAX) goto name_error;
+  strcat(desc_file_name, text);
+#if 0
+  text = "_";
+  textlen+=strlen(text);
+  if(textlen >= PATH_MAX) goto name_error;
+  strcat(desc_file_name, text);
 
-  for(i=0;unlikely(i<num);){
-    LttFacility *iter_facility =
-                      (LttFacility*) g_ptr_array_index(facilities,i);
-    unsigned base_id = iter_facility->base_id;
+  err = snprintf(desc_file_name+textlen, PATH_MAX-textlen-1,
+      "%u", f->checksum);
+  if(err < 0) goto name_error;
 
-    if(likely(id >= base_id && 
-       id < base_id + iter_facility->event_number)) {
-      facility = iter_facility;
-      break;
-    } else {
-      i++;
-    }
-  }
+  textlen=strlen(desc_file_name);
   
-  return facility;
-}
+#endif //0
+  text = ".xml";
+  textlen+=strlen(text);
+  if(textlen >= PATH_MAX) goto name_error;
+  strcat(desc_file_name, text);
 
-LttEventType *ltt_trace_eventtype_get(LttTrace *t, unsigned evId)
-{
-  LttEventType *event_type;
-  
-  LttFacility * f;
-  f = ltt_trace_facility_by_id(t,evId);
+  err = ltt_facility_open(f, t, desc_file_name);
+  if(err) goto facility_error;
 
-  if(unlikely(!f)) event_type = NULL;
-  else event_type = f->events[evId - f->base_id];
+  return 0;
 
-  return event_type;
+facility_error:
+name_error:
+  return 1;
 }
 
-/*****************************************************************************
- *There is one "per cpu" tracefile for each CPU, numbered from 0 to
- *the maximum number of CPU in the system. When the number of CPU installed
- *is less than the maximum, some positions are unused. There are also a
- *number of "control" tracefiles (facilities, interrupts...). 
- ****************************************************************************/
-unsigned ltt_trace_control_tracefile_number(LttTrace *t)
+static void ltt_fac_ids_destroy(gpointer data)
 {
-  return t->control_tracefile_number;
-}
+  GArray *fac_ids = (GArray *)data;
 
-unsigned ltt_trace_per_cpu_tracefile_number(LttTrace *t)
-{
-  return t->per_cpu_tracefile_number;
+  g_array_free(fac_ids, TRUE);
 }
 
-/*****************************************************************************
- *It is possible to search for the tracefiles by name or by CPU position.
- *The index within the tracefiles of the same type is returned if found
- *and a negative value otherwise. 
- ****************************************************************************/
 
-int ltt_trace_control_tracefile_find(LttTrace *t, const gchar *name)
+/* Presumes the tracefile is already seeked at the beginning. It makes sense,
+ * because it must be done just after the opening */
+static int ltt_process_facility_tracefile(LttTracefile *tf)
 {
-  LttTracefile * tracefile;
-  unsigned int i;
-  for(i=0;i<t->control_tracefile_number;i++){
-    tracefile = (LttTracefile*)g_ptr_array_index(t->control_tracefiles, i);
-    if(strcmp(tracefile->name, name)==0)break;
-  }
-  if(i == t->control_tracefile_number) return -1;
-  return i;
-}
+  int err;
+  LttFacility *fac;
+  GArray *fac_ids;
+  guint i;
+  LttEventType *et;
+  
+  while(1) {
+    err = ltt_tracefile_read_seek(tf);
+    if(err == EPERM) goto seek_error;
+    else if(err == ERANGE) break; /* End of tracefile */
+
+    err = ltt_tracefile_read_update_event(tf);
+    if(err) goto update_error;
+
+    /* We are on a facility load/or facility unload/ or heartbeat event */
+    /* The rules are :
+     * * facility 0 is hardcoded : this is the core facility. It will be shown
+     *   in the facility array though, and is shown as "loaded builtin" in the
+     *   trace.
+     * It contains event :
+     *  0 : facility load
+     *  1 : facility unload
+     *  2 : state dump facility load
+     *  3 : heartbeat
+     */
+    if(tf->event.facility_id != LTT_FACILITY_CORE) {
+      /* Should only contain core facility */
+      g_warning("Error in processing facility file %s, "
+          "should not contain facility id  %u.", g_quark_to_string(tf->name),
+          tf->event.facility_id);
+      err = EPERM;
+      goto fac_id_error;
+    } else {
+    
+      struct LttFacilityLoad *fac_load_data;
+      struct LttStateDumpFacilityLoad *fac_state_dump_load_data;
+      char *fac_name;
+
+      // FIXME align
+      switch((enum ltt_core_events)tf->event.event_id) {
+        case LTT_EVENT_FACILITY_LOAD:
+          fac_name = (char*)(tf->event.data);
+          g_debug("Doing LTT_EVENT_FACILITY_LOAD of facility %s",
+              fac_name);
+          fac_load_data =
+            (struct LttFacilityLoad *)
+                (tf->event.data + strlen(fac_name) + 1);
+          fac = &g_array_index (tf->trace->facilities_by_num, LttFacility,
+              ltt_get_uint32(LTT_GET_BO(tf), &fac_load_data->id));
+          g_assert(fac->exists == 0);
+          fac->name = g_quark_from_string(fac_name);
+          fac->checksum = ltt_get_uint32(LTT_GET_BO(tf),
+                          &fac_load_data->checksum);
+          fac->id = ltt_get_uint32(LTT_GET_BO(tf), &fac_load_data->id);
+          fac->pointer_size = ltt_get_uint32(LTT_GET_BO(tf),
+                          &fac_load_data->pointer_size);
+          fac->long_size = ltt_get_uint32(LTT_GET_BO(tf),
+                          &fac_load_data->long_size);
+          fac->size_t_size = ltt_get_uint32(LTT_GET_BO(tf),
+                          &fac_load_data->size_t_size);
+          fac->alignment = ltt_get_uint32(LTT_GET_BO(tf),
+                          &fac_load_data->alignment);
+
+          if(ltt_get_facility_description(fac, tf->trace, tf))
+            continue; /* error opening description */
+          
+          fac->trace = tf->trace;
+
+          /* Preset the field offsets */
+          for(i=0; i<fac->events->len; i++){
+            et = &g_array_index(fac->events, LttEventType, i);
+            set_fields_offsets(tf, et);
+          }
 
-/* not really useful. We just have to know that cpu tracefiles
- * comes before control tracefiles.
- */
-int ltt_trace_per_cpu_tracefile_find(LttTrace *t, const gchar *name)
-{
-  LttTracefile * tracefile;
-  unsigned int i;
-  for(i=0;i<t->per_cpu_tracefile_number;i++){
-    tracefile = (LttTracefile*)g_ptr_array_index(t->per_cpu_tracefiles, i);
-    if(strcmp(tracefile->name, name)==0)break;
+          fac->exists = 1;
+
+          fac_ids = g_datalist_id_get_data(&tf->trace->facilities_by_name,
+                                fac->name);
+          if(fac_ids == NULL) {
+            fac_ids = g_array_sized_new (FALSE, TRUE, sizeof(guint), 1);
+            g_datalist_id_set_data_full(&tf->trace->facilities_by_name,
+                                     fac->name,
+                                     fac_ids, ltt_fac_ids_destroy);
+          }
+          g_array_append_val(fac_ids, fac->id);
+
+          break;
+        case LTT_EVENT_FACILITY_UNLOAD:
+          g_debug("Doing LTT_EVENT_FACILITY_UNLOAD");
+          /* We don't care about unload : facilities ID are valid for the whole
+           * trace. They simply won't be used after the unload. */
+          break;
+        case LTT_EVENT_STATE_DUMP_FACILITY_LOAD:
+          fac_name = (char*)(tf->event.data);
+          g_debug("Doing LTT_EVENT_STATE_DUMP_FACILITY_LOAD of facility %s",
+              fac_name);
+          fac_state_dump_load_data =
+            (struct LttStateDumpFacilityLoad *)
+                (tf->event.data + strlen(fac_name) + 1);
+          fac = &g_array_index (tf->trace->facilities_by_num, LttFacility,
+              ltt_get_uint32(LTT_GET_BO(tf), &fac_state_dump_load_data->id));
+          g_assert(fac->exists == 0);
+          fac->name = g_quark_from_string(fac_name);
+          fac->checksum = ltt_get_uint32(LTT_GET_BO(tf),
+                          &fac_state_dump_load_data->checksum);
+          fac->id = fac_state_dump_load_data->id;
+          fac->pointer_size = ltt_get_uint32(LTT_GET_BO(tf),
+                          &fac_state_dump_load_data->pointer_size);
+          fac->long_size = ltt_get_uint32(LTT_GET_BO(tf),
+                          &fac_state_dump_load_data->long_size);
+          fac->size_t_size = ltt_get_uint32(LTT_GET_BO(tf),
+                          &fac_state_dump_load_data->size_t_size);
+          fac->alignment = ltt_get_uint32(LTT_GET_BO(tf),
+                          &fac_state_dump_load_data->alignment);
+          if(ltt_get_facility_description(fac, tf->trace, tf))
+            continue; /* error opening description */
+          
+          fac->trace = tf->trace;
+
+          /* Preset the field offsets */
+          for(i=0; i<fac->events->len; i++){
+            et = &g_array_index(fac->events, LttEventType, i);
+            set_fields_offsets(tf, et);
+          }
+
+          fac->exists = 1;
+          
+          fac_ids = g_datalist_id_get_data(&tf->trace->facilities_by_name,
+              fac->name);
+          if(fac_ids == NULL) {
+            fac_ids = g_array_sized_new (FALSE, TRUE, sizeof(guint), 1);
+            g_datalist_id_set_data_full(&tf->trace->facilities_by_name,
+                                     fac->name,
+                                     fac_ids, ltt_fac_ids_destroy);
+          }
+          g_array_append_val(fac_ids, fac->id);
+
+          break;
+        case LTT_EVENT_HEARTBEAT:
+          break;
+        default:
+          g_warning("Error in processing facility file %s, "
+              "unknown event id %hhu in core facility.",
+              g_quark_to_string(tf->name),
+              tf->event.event_id);
+          err = EPERM;
+          goto event_id_error;
+      }
+    }
   }
-  if(i == t->per_cpu_tracefile_number) return -1;
-  return i;
+  return 0;
+
+  /* Error handling */
+event_id_error:
+fac_id_error:
+update_error:
+seek_error:
+  g_warning("An error occured in facility tracefile parsing");
+  return err;
 }
 
-/*****************************************************************************
- *Get a specific tracefile 
- ****************************************************************************/
 
-LttTracefile *ltt_trace_control_tracefile_get(LttTrace *t, unsigned i)
+LttTrace *ltt_trace_open(const gchar *pathname)
 {
-  return (LttTracefile*)g_ptr_array_index(t->control_tracefiles, i);  
-}
+  gchar abs_path[PATH_MAX];
+  LttTrace  * t;
+  LttTracefile *tf;
+  GArray *group;
+  int i, ret;
+  struct ltt_block_start_header *header;
+       DIR *dir;
+       struct dirent *entry;
+  guint control_found = 0;
+  guint eventdefs_found = 0;
+       struct stat stat_buf;
+  gchar path[PATH_MAX];
+  
+  t = g_new(LttTrace, 1);
+  if(!t) goto alloc_error;
+
+  get_absolute_pathname(pathname, abs_path);
+  t->pathname = g_quark_from_string(abs_path);
+
+  g_datalist_init(&t->tracefiles);
+
+  /* Test to see if it looks like a trace */
+       dir = opendir(abs_path);
+       if(dir == NULL) {
+               perror(abs_path);
+               goto open_error;
+       }
+       while((entry = readdir(dir)) != NULL) {
+    strcpy(path, abs_path);
+    strcat(path, "/");
+    strcat(path, entry->d_name);
+               ret = stat(path, &stat_buf);
+               if(ret == -1) {
+                       perror(path);
+                       continue;
+               }
+               if(S_ISDIR(stat_buf.st_mode)) {
+      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;
+  
+  /* Open all the tracefiles */
+  if(open_tracefiles(t, abs_path, "")) {
+    g_warning("Error opening tracefile %s", abs_path);
+    goto find_error;
+  }
+  
+  /* Prepare the facilities containers : array and mapping */
+  /* Array is zeroed : the "exists" field is set to false by default */
+  t->facilities_by_num = g_array_sized_new (FALSE, 
+                                            TRUE, sizeof(LttFacility),
+                                            NUM_FACILITIES);
+  t->facilities_by_num = g_array_set_size(t->facilities_by_num, NUM_FACILITIES);
+
+  g_datalist_init(&t->facilities_by_name);
+  
+  /* Parse each trace control/facilitiesN files : get runtime fac. info */
+  group = g_datalist_id_get_data(&t->tracefiles, LTT_TRACEFILE_NAME_FACILITIES);
+  if(group == NULL) {
+    g_error("Trace %s has no facility tracefile", abs_path);
+    g_assert(0);
+    goto facilities_error;
+  }
+
+  /* Get the trace information for the control/facility 0 tracefile */
+  g_assert(group->len > 0);
+  tf = &g_array_index (group, LttTracefile, 0);
+  header = (struct ltt_block_start_header*)tf->buffer.head;
+  g_assert(parse_trace_header(header->trace,
+                                  tf, t) == 0);
+
+  t->num_cpu = group->len;
+  
+  for(i=0; i<group->len; i++) {
+    tf = &g_array_index (group, LttTracefile, i);
+    if(ltt_process_facility_tracefile(tf))
+      goto facilities_error;
+  }
+  
+  return t;
+
+  /* Error handling */
+facilities_error:
+  g_datalist_clear(&t->facilities_by_name);
+  g_array_free(t->facilities_by_num, TRUE);
+find_error:
+  g_datalist_clear(&t->tracefiles);
+open_error:
+  g_free(t);
+alloc_error:
+  return NULL;
+
+}
+
+GQuark ltt_trace_name(const LttTrace *t)
+{
+  return t->pathname;
+}
+
+
+/******************************************************************************
+ * When we copy a trace, we want all the opening actions to happen again :
+ * the trace will be reopened and totally independant from the original.
+ * That's why we call ltt_trace_open.
+ *****************************************************************************/
+LttTrace *ltt_trace_copy(LttTrace *self)
+{
+  return ltt_trace_open(g_quark_to_string(self->pathname));
+}
+
+void ltt_trace_close(LttTrace *t)
+{
+  guint i;
+  LttFacility *fac;
+
+  for(i=0; i<t->facilities_by_num->len; i++) {
+    fac = &g_array_index (t->facilities_by_num, LttFacility, i);
+    if(fac->exists)
+      ltt_facility_close(fac);
+  }
+
+  g_datalist_clear(&t->facilities_by_name);
+  g_array_free(t->facilities_by_num, TRUE);
+  g_datalist_clear(&t->tracefiles);
+  g_free(t);
+}
 
-LttTracefile *ltt_trace_per_cpu_tracefile_get(LttTrace *t, unsigned i)
-{
-  return (LttTracefile*)g_ptr_array_index(t->per_cpu_tracefiles, i);
-}
 
 /*****************************************************************************
- * Get the start time and end time of the trace 
+ *Get the system description of the trace
  ****************************************************************************/
 
-void ltt_trace_time_span_get(LttTrace *t, LttTime *start, LttTime *end)
+LttFacility *ltt_trace_facility_by_id(LttTrace *t, guint8 id)
 {
-  LttTime startSmall, startTmp, endBig, endTmp;
-  unsigned int i, j=0;
-  LttTracefile * tf;
+  g_assert(id < t->facilities_by_num->len);
+  return &g_array_index(t->facilities_by_num, LttFacility, id);
+}
 
-  for(i=0;i<t->control_tracefile_number;i++){
-    tf = g_ptr_array_index(t->control_tracefiles, i);
-    readBlock(tf,1);
-    startTmp = ltt_get_time(t->reverse_byte_order, &tf->a_block_start->time);
-    readBlock(tf,tf->block_number);
-    endTmp = ltt_get_time(t->reverse_byte_order, &tf->a_block_end->time);
-    if(i==0){
-      startSmall = startTmp;
-      endBig     = endTmp;
-      j = 1;
-      continue;
-    }
-    if(ltt_time_compare(startSmall,startTmp) > 0) startSmall = startTmp;
-    if(ltt_time_compare(endBig,endTmp) < 0) endBig = endTmp;
+/* ltt_trace_facility_get_by_name
+ *
+ * Returns the GArray of facility indexes. All the fac_ids that matches the
+ * requested facility name.
+ *
+ * If name is not found, returns NULL.
+ */
+GArray *ltt_trace_facility_get_by_name(LttTrace *t, GQuark name)
+{
+  return g_datalist_id_get_data(&t->facilities_by_name, name);
+}
+
+/*****************************************************************************
+ * Functions to discover all the event types in the trace 
+ ****************************************************************************/
+
+#if 0
+unsigned ltt_trace_eventtype_number(LttTrace *t)
+{
+  unsigned int i;
+  unsigned count = 0;
+  unsigned int num = t->facility_number;
+  LttFacility * f;
+  
+  for(i=0;i<num;i++){
+    f = (LttFacility*)g_ptr_array_index(t->facilities, i);
+    count += f->event_number;
   }
+  return count;
+}
+#endif //0
+
+#if 0
+//use an iteration on all the trace facilities, and inside iteration on all the
+//event types in each facilities instead.
+LttEventType *ltt_trace_eventtype_get(LttTrace *t, unsigned evId)
+{
+  LttEventType *event_type;
+  
+  LttFacility * f;
+  f = ltt_trace_facility_by_id(t,evId);
 
-  for(i=0;i<t->per_cpu_tracefile_number;i++){
-    tf = g_ptr_array_index(t->per_cpu_tracefiles, i);
-    readBlock(tf,1);
-    startTmp = ltt_get_time(t->reverse_byte_order, &tf->a_block_start->time);
-    readBlock(tf,tf->block_number);
-    endTmp = ltt_get_time(t->reverse_byte_order, &tf->a_block_end->time);
-    if(j == 0 && i==0){
-      startSmall = startTmp;
-      endBig     = endTmp;
-      continue;
+  if(unlikely(!f)) event_type = NULL;
+  else event_type = f->events[evId - f->base_id];
+
+  return event_type;
+}
+#endif //0
+
+#if 0
+/*****************************************************************************
+ * ltt_trace_find_tracefile
+ *
+ * Find a tracefile by name and index in the group.
+ *
+ * Returns a pointer to the tracefiles, else NULL.
+ ****************************************************************************/
+
+LttTracefile *ltt_trace_find_tracefile(LttTrace *t, const gchar *name)
+{
+}
+#endif //0
+
+/*****************************************************************************
+ * Get the start time and end time of the trace 
+ ****************************************************************************/
+
+static void ltt_tracefile_time_span_get(LttTracefile *tf,
+                                        LttTime *start, LttTime *end)
+{
+  int err;
+
+  err = map_block(tf, 0);
+  if(unlikely(err)) {
+    g_error("Can not map block");
+    *start = ltt_time_infinite;
+  } else
+    *start = tf->buffer.begin.timestamp;
+
+  err = map_block(tf, tf->num_blocks - 1);  /* Last block */
+  if(unlikely(err)) {
+    g_error("Can not map block");
+    *end = ltt_time_zero;
+  } else
+    *end = tf->buffer.end.timestamp;
+}
+
+struct tracefile_time_span_get_args {
+  LttTrace *t;
+  LttTime *start;
+  LttTime *end;
+};
+
+static void group_time_span_get(GQuark name, gpointer data, gpointer user_data)
+{
+  struct tracefile_time_span_get_args *args =
+          (struct tracefile_time_span_get_args*)user_data;
+
+  GArray *group = (GArray *)data;
+  int i;
+  LttTracefile *tf;
+  LttTime tmp_start;
+  LttTime tmp_end;
+
+  for(i=0; i<group->len; i++) {
+    tf = &g_array_index (group, LttTracefile, i);
+    if(tf->cpu_online) {
+      ltt_tracefile_time_span_get(tf, &tmp_start, &tmp_end);
+      if(ltt_time_compare(*args->start, tmp_start)>0) *args->start = tmp_start;
+      if(ltt_time_compare(*args->end, tmp_end)<0) *args->end = tmp_end;
     }
-    if(ltt_time_compare(startSmall,startTmp) > 0) startSmall = startTmp;
-    if(ltt_time_compare(endBig,endTmp) < 0) endBig = endTmp;
   }
+}
+
+void ltt_trace_time_span_get(LttTrace *t, LttTime *start, LttTime *end)
+{
+  LttTime min_start = ltt_time_infinite;
+  LttTime max_end = ltt_time_zero;
+  struct tracefile_time_span_get_args args = { t, &min_start, &max_end };
 
-  if(start != NULL) *start = startSmall;
-  if(end != NULL) *end = endBig;
+  g_datalist_foreach(&t->tracefiles, &group_time_span_get, &args);
+  
+  if(start != NULL) *start = min_start;
+  if(end != NULL) *end = max_end;
+  
 }
 
 
@@ -894,318 +1343,369 @@ void ltt_trace_time_span_get(LttTrace *t, LttTime *start, LttTime *end)
  *Get the name of a tracefile
  ****************************************************************************/
 
-char *ltt_tracefile_name(LttTracefile *tf)
+GQuark ltt_tracefile_name(const LttTracefile *tf)
 {
   return tf->name;
 }
 
+GQuark ltt_tracefile_long_name(const LttTracefile *tf)
+{
+  return tf->long_name;
+}
+
+
+
+guint ltt_tracefile_num(LttTracefile *tf)
+{
+  return tf->cpu_num;
+}
+
 /*****************************************************************************
  * Get the number of blocks in the tracefile 
  ****************************************************************************/
 
-unsigned ltt_tracefile_block_number(LttTracefile *tf)
+guint ltt_tracefile_block_number(LttTracefile *tf)
 {
-  return tf->block_number
+  return tf->num_blocks
 }
 
-/*****************************************************************************
- *Function name
- *    ltt_tracefile_seek_time: seek to the first event of the trace with time 
- *                             larger or equal to time
- *Input params
- *    t                      : tracefile
- *    time                   : criteria of the time
- ****************************************************************************/
-void ltt_tracefile_find_time_block(LttTracefile *t, LttTime time, 
-                                  int start_block, int end_block)
+
+/* Seek to the first event in a tracefile that has a time equal or greater than
+ * the time passed in parameter.
+ *
+ * If the time parameter is outside the tracefile time span, seek to the first
+ * event or if after, return ERANGE.
+ *
+ * If the time parameter is before the first event, we have to seek specially to
+ * there.
+ *
+ * If the time is after the end of the trace, return ERANGE.
+ *
+ * Do a binary search to find the right block, then a sequential search in the
+ * block to find the event. 
+ *
+ * In the special case where the time requested fits inside a block that has no
+ * event corresponding to the requested time, the first event of the next block
+ * will be seeked.
+ *
+ * IMPORTANT NOTE : // FIXME everywhere...
+ *
+ * You MUST NOT do a ltt_tracefile_read right after a ltt_tracefile_seek_time :
+ * you will jump over an event if you do.
+ *
+ * Return value : 0 : no error, the tf->event can be used
+ *                ERANGE : time if after the last event of the trace
+ *                otherwise : this is an error.
+ *
+ * */
+
+int ltt_tracefile_seek_time(LttTracefile *tf, LttTime time)
 {
-  int err, tmp_block, s, e; 
-  int headTime;
-  int tailTime;
-  
-  err=readBlock(t,start_block);
-  if(err) g_error("Can not read tracefile: %s\n", t->name); 
-  if(start_block == end_block)return;
+  int ret = 0;
+  int err;
+  unsigned int block_num, high, low;
 
-  tailTime = ltt_time_compare(ltt_get_time(t->trace->reverse_byte_order,
-                                           &t->a_block_end->time), time);
-  if(tailTime >= 0) return;
-  
-  err=readBlock(t,end_block);
-  if(err) g_error("Can not read tracefile: %s\n", t->name); 
-  if(start_block+1 == end_block)return;
-  
-  headTime = ltt_time_compare(ltt_get_time(t->trace->reverse_byte_order,
-                                           &t->a_block_start->time), time);
-  if(headTime <= 0 ) return;
-  
-  tmp_block = (end_block + start_block)/2;
-  err=readBlock(t,tmp_block);
-  if(err) g_error("Can not read tracefile: %s\n", t->name); 
-
-  headTime = ltt_time_compare(ltt_get_time(t->trace->reverse_byte_order,
-                                           &t->a_block_start->time), time);
-  tailTime = ltt_time_compare(ltt_get_time(t->trace->reverse_byte_order,
-                                           &t->a_block_end->time), time);
-  if(headTime <= 0 && tailTime >= 0) return;
-  
-  if(headTime > 0){
-    s = start_block + 1;
-    e = tmp_block - 1;
-    if(s <= e)
-      ltt_tracefile_find_time_block(t, time, s, e);
-    else return;
+  /* seek at the beginning of trace */
+  err = map_block(tf, 0);  /* First block */
+  if(unlikely(err)) {
+    g_error("Can not map block");
+    goto fail;
   }
 
-  if(tailTime < 0){
-    s = tmp_block + 1;
-    e = end_block - 1;
-    if(s <= e)
-      ltt_tracefile_find_time_block(t, time, s, e);
-    else return;
-  }  
-}
-
-void ltt_tracefile_backward_find_time_block(LttTracefile *t, LttTime time)
-{
-  int t_time, h_time, err;
-  err=readBlock(t,t->which_block-1);
-  if(err) g_error("Can not read tracefile: %s\n", t->name); 
-  h_time = ltt_time_compare(ltt_get_time(t->trace->reverse_byte_order,
-                                         &t->a_block_start->time), time);
-  t_time = ltt_time_compare(ltt_get_time(t->trace->reverse_byte_order,
-                                         &t->a_block_end->time), time);
-  if(h_time == 0){
-    int tmp;
-    if(t->which_block == 1) return;
-    err=readBlock(t,t->which_block-1);
-    if(err) g_error("Can not read tracefile: %s\n", t->name); 
-    tmp = ltt_time_compare(ltt_get_time(t->trace->reverse_byte_order,
-                                        &t->a_block_end->time), time);
-    if(tmp == 0) return ltt_tracefile_seek_time(t, time);
-    err=readBlock(t,t->which_block+1);
-    if(err) g_error("Can not read tracefile: %s\n", t->name);     
-  }else if(h_time > 0){
-    ltt_tracefile_find_time_block(t, time, 1, t->which_block);
-    return ltt_tracefile_seek_time(t, time) ;    
-  }else{
-    if(t_time >= 0) return ltt_tracefile_seek_time(t, time);
-    err=readBlock(t,t->which_block+1);
-    if(err) g_error("Can not read tracefile: %s\n", t->name);    
+ /* If the time is lower or equal the beginning of the trace,
+  * go to the first event. */
+  if(ltt_time_compare(time, tf->buffer.begin.timestamp) <= 0) {
+    ret = ltt_tracefile_read(tf);
+    if(ret == ERANGE) goto range;
+    else if (ret) goto fail;
+    goto found; /* There is either no event in the trace or the event points
+                   to the first event in the trace */
   }
-}
 
-void ltt_tracefile_seek_time(LttTracefile *t, LttTime time)
-{
-  int err;
-  LttTime lttTime;
-  int headTime = ltt_time_compare(ltt_get_time(t->trace->reverse_byte_order,
-                                               &t->a_block_start->time), time);
-  int tailTime = ltt_time_compare(ltt_get_time(t->trace->reverse_byte_order,
-                                               &t->a_block_end->time), time);
-  LttEvent ev;
+  err = map_block(tf, tf->num_blocks - 1);  /* Last block */
+  if(unlikely(err)) {
+    g_error("Can not map block");
+    goto fail;
+  }
 
-  if(headTime < 0 && tailTime > 0){
-    if(ltt_time_compare(ltt_get_time(t->trace->reverse_byte_order, 
-                                     &t->a_block_end->time),
-                        t->current_event_time) !=0) {
-      lttTime = getEventTime(t);
-      err = ltt_time_compare(lttTime, time);
-      if(err > 0){
-       if(t->which_event==2 || ltt_time_compare(t->prev_event_time,time)<0){
-         return;
-       }else{
-         updateTracefile(t);
-         return ltt_tracefile_seek_time(t, time);
-       }
-      }else if(err < 0){
-       while(1){
-         if(ltt_tracefile_read(t,&ev) == NULL) {
-           g_print("End of file\n");      
-           return;
-         }
-         lttTime = getEventTime(t);
-         err = ltt_time_compare(lttTime, time);
-         if(err >= 0)return;
-       }
-      }else return;    
-    }else{//we are at the end of the block
-      updateTracefile(t);
-      return ltt_tracefile_seek_time(t, time);      
+ /* If the time is after the end of the trace, return ERANGE. */
+  if(ltt_time_compare(time, tf->buffer.end.timestamp) > 0) {
+    goto range;
+  }
+
+  /* Binary search the block */
+  high = tf->num_blocks - 1;
+  low = 0;
+  
+  while(1) {
+    block_num = ((high-low) / 2) + low;
+
+    err = map_block(tf, block_num);
+    if(unlikely(err)) {
+      g_error("Can not map block");
+      goto fail;
     }
-  }else if(headTime >= 0){
-    if(t->which_block == 1){
-      updateTracefile(t);      
-    }else{
-      if(ltt_time_compare(t->prev_block_end_time, time) >= 0 ||
-        (t->prev_block_end_time.tv_sec == 0 && 
-         t->prev_block_end_time.tv_nsec == 0 )){
-       ltt_tracefile_backward_find_time_block(t, time);
-      }else{
-       updateTracefile(t);
+    if(high == low) {
+      /* We cannot divide anymore : this is what would happen if the time
+       * requested was exactly between two consecutive buffers'end and start 
+       * timestamps. This is also what would happend if we didn't deal with out
+       * of span cases prior in this function. */
+      /* The event is right in the buffer!
+       * (or in the next buffer first event) */
+      while(1) {
+        ret = ltt_tracefile_read(tf);
+        if(ret == ERANGE) goto range; /* ERANGE or EPERM */
+        else if(ret) goto fail;
+
+        if(ltt_time_compare(time, tf->event.event_time) <= 0)
+          goto found;
+      }
+
+    } else if(ltt_time_compare(time, tf->buffer.begin.timestamp) < 0) {
+      /* go to lower part */
+      high = block_num - 1;
+    } else if(ltt_time_compare(time, tf->buffer.end.timestamp) > 0) {
+      /* go to higher part */
+      low = block_num + 1;
+    } else {/* The event is right in the buffer!
+               (or in the next buffer first event) */
+      while(1) {
+        ret = ltt_tracefile_read(tf);
+        if(ret == ERANGE) goto range; /* ERANGE or EPERM */
+        else if(ret) goto fail;
+
+        if(ltt_time_compare(time, tf->event.event_time) <= 0)
+          break;
       }
+      goto found;
     }
-  }else if(tailTime < 0){
-    if(t->which_block != t->block_number){
-      ltt_tracefile_find_time_block(t, time, t->which_block+1, t->block_number);
-      return ltt_tracefile_seek_time(t, time);
-    }else {
-     t->cur_event_pos = t->buffer + t->block_size;
-     g_print("End of file\n");      
-      return;      
-    }    
-  }else if(tailTime == 0){
-    t->cur_event_pos = t->last_event_pos;
-    t->current_event_time = time;  
-    t->cur_heart_beat_number = 0;
-    t->prev_event_time.tv_sec = 0;
-    t->prev_event_time.tv_nsec = 0;
-    return;
   }
+
+found:
+  return 0;
+range:
+  return ERANGE;
+
+  /* Error handling */
+fail:
+  g_error("ltt_tracefile_seek_time failed on tracefile %s", 
+      g_quark_to_string(tf->name));
+  return EPERM;
 }
 
-/*****************************************************************************
- * Seek to the first event with position equal or larger to ep 
- *
- * Modified by Mathieu Desnoyers to used faster offset position instead of
- * re-reading the whole buffer.
- ****************************************************************************/
 
-void ltt_tracefile_seek_position(LttTracefile *t, const LttEventPosition *ep)
-{
-  //if we are at the right place, just return
-  if(likely(t->which_block == ep->block_num && t->which_event == ep->event_num))
-    return;
+int ltt_tracefile_seek_position(LttTracefile *tf, const LttEventPosition *ep) {
   
-  if(likely(t->which_block == ep->block_num)) updateTracefile(t);
-  else readBlock(t,ep->block_num);
-  //event offset is available
-  if(likely(ep->old_position)){
-    int err;
-
-    t->which_event = ep->event_num;
-    t->cur_event_pos = t->buffer + ep->event_offset;
-    t->prev_event_time = ep->event_time;
-    t->current_event_time = ep->event_time;
-    t->cur_heart_beat_number = ep->heart_beat_number;
-    t->cur_cycle_count = ep->event_cycle_count;
-
-    /* This is a workaround for fast position seek */
-    t->last_event_pos = ep->last_event_pos;
-    t->prev_block_end_time = ep->prev_block_end_time;
-    t->prev_event_time = ep->prev_event_time;
-    t->pre_cycle_count = ep->pre_cycle_count;
-    t->count = ep->count;
-    t->overflow_nsec = ep->overflow_nsec;
-    t->last_heartbeat = ep->last_heartbeat;
-    /* end of workaround */
-
-    //update the fields of the current event and go to the next event
-    err = skipEvent(t);
-    if(unlikely(err == ERANGE)) g_error("event id is out of range\n");
-      
-    return;
+  int err;
+  
+  if(ep->tracefile != tf) {
+    goto fail;
+  }
+
+  err = map_block(tf, ep->block);
+  if(unlikely(err)) {
+    g_error("Can not map block");
+    goto fail;
   }
 
-  //only block number and event index are available
-  //MD: warning : this is slow!
-  g_warning("using slow O(n) tracefile seek position");
+  tf->event.offset = ep->offset;
 
-  LttEvent event;
-  while(likely(t->which_event < ep->event_num)) ltt_tracefile_read(t, &event);
+  err = ltt_tracefile_read_update_event(tf);
+  if(err) goto fail;
+  err = ltt_tracefile_read_op(tf);
+  if(err) goto fail;
 
-  return;
+  return 0;
+
+fail:
+  g_error("ltt_tracefile_seek_time failed on tracefile %s", 
+      g_quark_to_string(tf->name));
+  return 1;
+}
+
+/* Calculate the real event time based on the buffer boundaries */
+LttTime ltt_interpolate_time(LttTracefile *tf, LttEvent *event)
+{
+  LttTime time;
+
+  g_assert(tf->trace->has_tsc);
+
+//  time = ltt_time_from_uint64(
+//      cycles_2_ns(tf, (guint64)(tf->buffer.tsc - tf->buffer.begin.cycle_count)));
+  time = ltt_time_from_uint64((tf->buffer.tsc - tf->trace->start_tsc) * 1000000
+                                  / (double)tf->trace->start_freq);
+  //time = ltt_time_add(tf->buffer.begin.timestamp, time);
+  time = ltt_time_add(tf->trace->start_time, time);
+
+  return time;
+}
+
+
+/* Get the current event of the tracefile : valid until the next read */
+LttEvent *ltt_tracefile_get_event(LttTracefile *tf)
+{
+  return &tf->event;
 }
 
+
+
 /*****************************************************************************
  *Function name
- *    ltt_tracefile_read : read the current event, set the pointer to the next
+ *    ltt_tracefile_read : Read the next event in the tracefile
  *Input params
  *    t                  : tracefile
  *Return value
- *    LttEvent *        : an event to be processed
+ *
+ *    Returns 0 if an event can be used in tf->event.
+ *    Returns ERANGE on end of trace. The event in tf->event still can be used
+ *    (if the last block was not empty).
+ *    Returns EPERM on error.
+ *
+ *    This function does make the tracefile event structure point to the event
+ *    currently pointed to by the tf->event.
+ *
+ *    Note : you must call a ltt_tracefile_seek to the beginning of the trace to
+ *    reinitialize it after an error if you want results to be coherent.
+ *    It would be the case if a end of trace last buffer has no event : the end
+ *    of trace wouldn't be returned, but an error.
+ *    We make the assumption there is at least one event per buffer.
  ****************************************************************************/
 
-LttEvent *ltt_tracefile_read(LttTracefile *t, LttEvent *event)
+int ltt_tracefile_read(LttTracefile *tf)
+{
+  int err;
+
+  err = ltt_tracefile_read_seek(tf);
+  if(err) return err;
+  err = ltt_tracefile_read_update_event(tf);
+  if(err) return err;
+  err = ltt_tracefile_read_op(tf);
+  if(err) return err;
+
+  return 0;
+}
+
+int ltt_tracefile_read_seek(LttTracefile *tf)
 {
   int err;
 
-  if(unlikely(t->cur_event_pos == t->buffer + t->block_size)){
-    if(unlikely(t->which_block == t->block_number)){
-      return NULL;
+  /* Get next buffer until we finally have an event, or end of trace */
+  while(1) {
+    err = ltt_seek_next_event(tf);
+    if(unlikely(err == ENOPROTOOPT)) {
+      return EPERM;
     }
-    err = readBlock(t, t->which_block + 1);
-    if(unlikely(err))g_error("Can not read tracefile");    
-  }
 
-  event->event_id = ltt_get_uint16(t->trace->reverse_byte_order, t->cur_event_pos);
-  if(unlikely(event->event_id == TRACE_TIME_HEARTBEAT))
-    t->cur_heart_beat_number++;
-
-  t->prev_event_time  = t->current_event_time;
-  //  t->current_event_time = getEventTime(t);
-
-  event->time_delta = ltt_get_uint32(t->trace->reverse_byte_order, t->cur_event_pos + EVENT_ID_SIZE);
-  event->event_time = t->current_event_time;
-  event->event_cycle_count = t->cur_cycle_count;
-
-  event->tracefile = t;
-  event->data = t->cur_event_pos + EVENT_HEADER_SIZE;
-  event->which_block = t->which_block;
-  event->which_event = t->which_event;
-
-  /* This is a workaround for fast position seek */
-  event->last_event_pos = t->last_event_pos;
-  event->prev_block_end_time = t->prev_block_end_time;
-  event->prev_event_time = t->prev_event_time;
-  event->pre_cycle_count = t->pre_cycle_count;
-  event->count = t->count;
-  event->overflow_nsec = t->overflow_nsec;
-  event->last_heartbeat = t->last_heartbeat;
+    /* Are we at the end of the buffer ? */
+    if(err == ERANGE) {
+      if(unlikely(tf->buffer.index == tf->num_blocks-1)){ /* end of trace ? */
+        return ERANGE;
+      } else {
+        /* get next block */
+        err = map_block(tf, tf->buffer.index + 1);
+        if(unlikely(err)) {
+          g_error("Can not map block");
+          return EPERM;
+        }
+      }
+    } else break; /* We found an event ! */
+  }
   
-  /* end of workaround */
+  return 0;
+}
 
 
+/* do specific operation on events */
+int ltt_tracefile_read_op(LttTracefile *tf)
+{
+  LttEvent *event;
+
+  event = &tf->event;
 
-  //update the fields of the current event and go to the next event
-  err = skipEvent(t);
-  if(unlikely(err == ERANGE)) g_error("event id is out of range\n");
+   /* do event specific operation */
 
-  return event;
+  /* do something if its an heartbeat event : increment the heartbeat count */
+  //if(event->facility_id == LTT_FACILITY_CORE)
+  //  if(event->event_id == LTT_EVENT_HEARTBEAT)
+  //    tf->cur_heart_beat_number++;
+  
+  return 0;
 }
 
-/****************************************************************************
- *Function name
- *    readFile    : wrap function to read from a file
- *Input Params
- *    fd          : file descriptor
- *    buf         : buf to contain the content
- *    size        : number of bytes to be read
- *    mesg        : message to be printed if some thing goes wrong
- *return value 
- *    0           : success
- *    EIO         : can not read from the file
- ****************************************************************************/
 
-int readFile(int fd, void * buf, size_t size, char * mesg)
+/* same as ltt_tracefile_read, but does not seek to the next event nor call
+ * event specific operation. */
+int ltt_tracefile_read_update_event(LttTracefile *tf)
 {
-   ssize_t nbBytes = read(fd, buf, size);
+  void * pos;
+  LttEvent *event;
+  event = &tf->event;
+  pos = tf->buffer.head + event->offset;
+
+  /* Read event header */
+  
+  //TODO align
+  
+  if(tf->trace->has_tsc) {
+    if(tf->trace->has_heartbeat) {
+      event->time.timestamp = ltt_get_uint32(LTT_GET_BO(tf),
+                                            pos);
+      /* 32 bits -> 64 bits tsc */
+      /* note : still works for seek and non seek cases. */
+      if(event->time.timestamp < (0xFFFFFFFFULL&tf->buffer.tsc)) {
+        tf->buffer.tsc = ((tf->buffer.tsc&0xFFFFFFFF00000000ULL)
+                            + 0x100000000ULL)
+                                | (guint64)event->time.timestamp;
+        event->tsc = tf->buffer.tsc;
+      } else {
+        /* no overflow */
+        tf->buffer.tsc = (tf->buffer.tsc&0xFFFFFFFF00000000ULL) 
+                                | (guint64)event->time.timestamp;
+        event->tsc = tf->buffer.tsc;
+      }
+      pos += sizeof(guint32);
+    } else {
+      event->tsc = ltt_get_uint64(LTT_GET_BO(tf), pos);
+      tf->buffer.tsc = event->tsc;
+      pos += sizeof(guint64);
+    }
+    
+    event->event_time = ltt_interpolate_time(tf, event);
+  } else {
+    event->time.delta.tv_sec = 0;
+    event->time.delta.tv_nsec = ltt_get_uint32(LTT_GET_BO(tf),
+                                          pos) * NSEC_PER_USEC;
+    tf->buffer.tsc = 0;
+    event->tsc = tf->buffer.tsc;
+
+    event->event_time = ltt_time_add(tf->buffer.begin.timestamp,
+                                     event->time.delta);
+    pos += sizeof(guint32);
+  }
+
+  event->facility_id = *(guint8*)pos;
+  pos += sizeof(guint8);
 
-   if((size_t)nbBytes != size) {
-     if(nbBytes < 0) {
-       perror("Error in readFile : ");
-     } else {
-       g_warning("%s",mesg);
-     }
-     return EIO;
-   }
-   return 0;
+  event->event_id = *(guint8*)pos;
+  pos += sizeof(guint8);
+
+  event->event_size = ltt_get_uint16(LTT_GET_BO(tf), pos);
+  pos += sizeof(guint16);
+  
+  event->data = pos;
+
+  /* get the data size and update the event fields with the current
+   * information */
+  ltt_update_event_size(tf);
+
+  return 0;
 }
 
 
 /****************************************************************************
  *Function name
- *    readBlock       : read a block from the file
+ *    map_block       : map a block from the file
  *Input Params
  *    lttdes          : ltt trace file 
  *    whichBlock      : the block which will be read
@@ -1215,270 +1715,600 @@ int readFile(int fd, void * buf, size_t size, char * mesg)
  *    EIO             : can not read from the file
  ****************************************************************************/
 
-int readBlock(LttTracefile * tf, int whichBlock)
+static gint map_block(LttTracefile * tf, guint block_num)
 {
-  off_t nbBytes;
-  guint32 lostSize;
+  int page_size = getpagesize();
+  struct ltt_block_start_header *header;
 
-  /* same block already opened requested */
-  if((guint)whichBlock == tf->which_block) return 0;
-  
-  if(likely(whichBlock - tf->which_block == 1 && tf->which_block != 0)){
-    tf->prev_block_end_time = ltt_get_time(tf->trace->reverse_byte_order,
-                                           &tf->a_block_end->time);
-    tf->prev_event_time     = ltt_get_time(tf->trace->reverse_byte_order,
-                                           &tf->a_block_end->time);
-  }else{
-    tf->prev_block_end_time.tv_sec = 0;
-    tf->prev_block_end_time.tv_nsec = 0;
-    tf->prev_event_time.tv_sec = 0;
-    tf->prev_event_time.tv_nsec = 0;
-  }
+  g_assert(block_num < tf->num_blocks);
 
-  nbBytes=lseek(tf->fd,(off_t)((whichBlock-1)*tf->block_size), SEEK_SET);
-  if(unlikely(nbBytes == -1)) return EINVAL;
+  if(tf->buffer.head != NULL) {
+    if(munmap(tf->buffer.head, PAGE_ALIGN(tf->buf_size))) {
+    g_warning("unmap size : %u\n",
+        PAGE_ALIGN(tf->buf_size));
+      perror("munmap error");
+      g_assert(0);
+    }
+  }
+    
   
-  if(unlikely(readFile(tf->fd,tf->buffer,tf->block_size,"Unable to read a block")))
-    return EIO;
-
-  tf->a_block_start=(BlockStart *) (tf->buffer + EVENT_HEADER_SIZE);
-  lostSize = *(guint32 *)(tf->buffer + tf->block_size - sizeof(guint32));
-  tf->a_block_end=(BlockEnd *)(tf->buffer + tf->block_size
-                                       - sizeof(guint32) - lostSize - sizeof(BlockEnd)); 
-  tf->last_event_pos = tf->buffer + tf->block_size - 
-                              sizeof(guint32) - lostSize
-                              - sizeof(BlockEnd) - EVENT_HEADER_SIZE;
-
-  tf->which_block = whichBlock;
-  tf->which_event = 1;
-  tf->cur_event_pos = tf->buffer;//the beginning of the block, block start ev
-  tf->cur_heart_beat_number = 0;
-  tf->last_heartbeat = NULL;
+  /* Multiple of pages aligned head */
+  tf->buffer.head = mmap(0,
+      PAGE_ALIGN(tf->buf_size),
+      PROT_READ, MAP_PRIVATE, tf->fd,
+      PAGE_ALIGN((off_t)tf->buf_size * (off_t)block_num));
+
+  if(tf->buffer.head == MAP_FAILED) {
+    perror("Error in allocating memory for buffer of tracefile");
+    g_assert(0);
+    goto map_error;
+  }
+  g_assert( ( (guint)tf->buffer.head&(8-1) ) == 0); // make sure it's aligned.
   
-  /* read the whole block to precalculate total of cycles in it */
-  tf->count = 0;
-  tf->pre_cycle_count = 0;
-  tf->cur_cycle_count = ltt_get_uint32(tf->trace->reverse_byte_order, tf->cur_event_pos + EVENT_ID_SIZE);
 
-  getCyclePerNsec(tf);
+  tf->buffer.index = block_num;
 
-  tf->overflow_nsec = 
-               (-((double)
-               (ltt_get_uint64(tf->trace->reverse_byte_order,
-                               &tf->a_block_start->cycle_count)&0xFFFFFFFF))
-                                        * tf->nsec_per_cycle);
+  header = (struct ltt_block_start_header*)tf->buffer.head;
 
-  tf->current_event_time = getEventTime(tf);  
+#if 0
+  tf->buffer.begin.timestamp = ltt_time_add(
+                                ltt_time_from_uint64(
+                                 ltt_get_uint64(LTT_GET_BO(tf),
+                                  &header->begin.timestamp)
+                                    - tf->trace->start_monotonic),
+                                  tf->trace->start_time);
+#endif //0
+  //g_debug("block %u begin : %lu.%lu", block_num,
+  //    tf->buffer.begin.timestamp.tv_sec, tf->buffer.begin.timestamp.tv_nsec);
+  tf->buffer.begin.cycle_count = ltt_get_uint64(LTT_GET_BO(tf),
+                                              &header->begin.cycle_count);
+  tf->buffer.begin.freq = ltt_get_uint64(LTT_GET_BO(tf),
+                                         &header->begin.freq);
+  tf->buffer.begin.timestamp = ltt_time_add(
+                                ltt_time_from_uint64(
+                                  (tf->buffer.begin.cycle_count
+                                  - tf->trace->start_tsc) * 1000000
+                                    / (double)tf->trace->start_freq),
+                                tf->trace->start_time);
+#if 0
 
-  return 0;  
-}
+  tf->buffer.end.timestamp = ltt_time_add(
+                                ltt_time_from_uint64(
+                                 ltt_get_uint64(LTT_GET_BO(tf),
+                                  &header->end.timestamp)
+                                    - tf->trace->start_monotonic),
+                                  tf->trace->start_time);
+#endif //0
+  //g_debug("block %u end : %lu.%lu", block_num,
+  //    tf->buffer.end.timestamp.tv_sec, tf->buffer.end.timestamp.tv_nsec);
+  tf->buffer.end.cycle_count = ltt_get_uint64(LTT_GET_BO(tf),
+                                              &header->end.cycle_count);
+  tf->buffer.end.freq = ltt_get_uint64(LTT_GET_BO(tf),
+                                       &header->end.freq);
+  tf->buffer.lost_size = ltt_get_uint32(LTT_GET_BO(tf),
+                                        &header->lost_size);
+  tf->buffer.end.timestamp = ltt_time_add(
+                                ltt_time_from_uint64(
+                                  (tf->buffer.end.cycle_count
+                                  - tf->trace->start_tsc) * 1000000
+                                    / (double)tf->trace->start_freq),
+                                tf->trace->start_time);
+  tf->buffer.tsc =  tf->buffer.begin.cycle_count;
+  tf->event.tsc = tf->buffer.tsc;
+  tf->buffer.freq = tf->buffer.begin.freq;
+
+  /* FIXME
+   * eventually support variable buffer size : will need a partial pre-read of
+   * the headers to create an index when we open the trace... eventually. */
+  g_assert(tf->buf_size  == ltt_get_uint32(LTT_GET_BO(tf), 
+                                             &header->buf_size));
+  
+  /* Now that the buffer is mapped, calculate the time interpolation for the
+   * block. */
+  
+//  tf->buffer.nsecs_per_cycle = calc_nsecs_per_cycle(tf);
+  //tf->buffer.cyc2ns_scale = calc_nsecs_per_cycle(tf);
+  /* Make the current event point to the beginning of the buffer :
+   * it means that the event read must get the first event. */
+  tf->event.tracefile = tf;
+  tf->event.block = block_num;
+  tf->event.offset = 0;
+  
+  return 0;
 
-/*****************************************************************************
- *Function name
- *    updateTracefile : reinitialize the info of the block which is already 
- *                      in the buffer
- *Input params 
- *    tf              : tracefile
- ****************************************************************************/
+map_error:
+  return -errno;
+
+}
 
-void updateTracefile(LttTracefile * tf)
+/* It will update the fields offsets too */
+void ltt_update_event_size(LttTracefile *tf)
 {
-  tf->which_event = 1;
-  tf->cur_event_pos = tf->buffer;
-  tf->current_event_time = getEventTime(tf);  
-  tf->cur_heart_beat_number = 0;
+  ssize_t size = 0;
+
+  /* Specific handling of core events : necessary to read the facility control
+   * tracefile. */
+  LttFacility *f = ltt_trace_get_facility_by_num(tf->trace, 
+                                          tf->event.facility_id);
+
+  if(likely(tf->event.facility_id == LTT_FACILITY_CORE)) {
+    switch((enum ltt_core_events)tf->event.event_id) {
+  case LTT_EVENT_FACILITY_LOAD:
+    size = strlen((char*)tf->event.data) + 1;
+    //g_debug("Update Event facility load of facility %s", (char*)tf->event.data);
+    size += sizeof(struct LttFacilityLoad);
+    break;
+  case LTT_EVENT_FACILITY_UNLOAD:
+    //g_debug("Update Event facility unload");
+    size = sizeof(struct LttFacilityUnload);
+    break;
+  case LTT_EVENT_STATE_DUMP_FACILITY_LOAD:
+    size = strlen((char*)tf->event.data) + 1;
+    //g_debug("Update Event facility load state dump of facility %s",
+    //    (char*)tf->event.data);
+    size += sizeof(struct LttStateDumpFacilityLoad);
+    break;
+  case LTT_EVENT_HEARTBEAT:
+    //g_debug("Update Event heartbeat");
+    size = sizeof(TimeHeartbeat);
+    break;
+  default:
+    g_warning("Error in getting event size : tracefile %s, "
+        "unknown event id %hhu in core facility.",
+        g_quark_to_string(tf->name),
+        tf->event.event_id);
+    goto event_id_error;
 
-  tf->prev_event_time.tv_sec = 0;
-  tf->prev_event_time.tv_nsec = 0;
-  tf->count = 0;
+    }
+  } else {
+    if(!f->exists) {
+      g_error("Unknown facility %hhu (0x%hhx) in tracefile %s",
+          tf->event.facility_id,
+          tf->event.facility_id,
+          g_quark_to_string(tf->name));
+      goto facility_error;
+    }
+
+    LttEventType *event_type = 
+      ltt_facility_eventtype_get(f, tf->event.event_id);
+
+    if(!event_type) {
+      g_error("Unknown event id %hhu in facility %s in tracefile %s",
+          tf->event.event_id,
+          g_quark_to_string(f->name),
+          g_quark_to_string(tf->name));
+      goto event_type_error;
+    }
+    if(event_type->root_field)
+      size = get_field_type_size(tf, event_type,
+          0, 0, event_type->root_field, tf->event.data);
+    else
+      size = 0;
+
+    //g_debug("Event root field : f.e %hhu.%hhu size %zd",
+    //    tf->event.facility_id,
+    //    tf->event.event_id, size);
+  }
+  
+  tf->event.data_size = size;
+  
+  /* Check consistency between kernel and LTTV structure sizes */
+  g_assert(tf->event.data_size == tf->event.event_size);
 
-  tf->overflow_nsec = 
-    (-((double)ltt_get_uint64(tf->trace->reverse_byte_order,
-                              &tf->a_block_start->cycle_count))
-                                        * tf->nsec_per_cycle);
+  return;
 
+facility_error:
+event_type_error:
+event_id_error:
+  tf->event.data_size = 0;
 }
 
-/*****************************************************************************
- *Function name
- *    skipEvent : go to the next event, update the fields of the current event
- *Input params 
- *    t         : tracefile
- *return value 
- *    0               : success
- *    ERANGE          : event id is out of range
- ****************************************************************************/
 
-int skipEvent(LttTracefile * t)
+/* Take the tf current event offset and use the event facility id and event id
+ * to figure out where is the next event offset.
+ *
+ * This is an internal function not aiming at being used elsewhere : it will
+ * not jump over the current block limits. Please consider using
+ * ltt_tracefile_read to do this.
+ *
+ * Returns 0 on success
+ *         ERANGE if we are at the end of the buffer.
+ *         ENOPROTOOPT if an error occured when getting the current event size.
+ */
+static int ltt_seek_next_event(LttTracefile *tf)
 {
-  int evId;
-  void * evData;
-  LttEventType * evT;
-  LttField * rootFld;
+  int ret = 0;
+  void *pos;
+  
+  /* seek over the buffer header if we are at the buffer start */
+  if(tf->event.offset == 0) {
+    tf->event.offset += tf->buffer_header_size;
 
-  evId   = ltt_get_uint16(t->trace->reverse_byte_order, t->cur_event_pos);
-  evData = t->cur_event_pos + EVENT_HEADER_SIZE;
+    if(tf->event.offset == tf->buf_size - tf->buffer.lost_size) {
+      ret = ERANGE;
+    }
+    goto found;
+  }
 
-  evT    = ltt_trace_eventtype_get(t->trace,(unsigned)evId);
-    
-  if(likely(evT)) rootFld = evT->root_field;
-  else return ERANGE;
   
-  if(likely(rootFld)){
-    //event has string/sequence or the last event is not the same event
-    if(likely((evT->latest_block!=t->which_block || evT->latest_event!=t->which_event)
-       && rootFld->field_fixed == 0)){
-      setFieldsOffset(t, evT, evData, t->trace);
-    }
-    t->cur_event_pos += EVENT_HEADER_SIZE + rootFld->field_size;
-  }else t->cur_event_pos += EVENT_HEADER_SIZE;
-    
-  evT->latest_block = t->which_block;
-  evT->latest_event = t->which_event;
-
-  //the next event is in the next block
-  if(unlikely(evId == TRACE_BLOCK_END)){
-    t->cur_event_pos = t->buffer + t->block_size;
-  }else{
-    t->cur_cycle_count = ltt_get_uint32(t->trace->reverse_byte_order,
-                                t->cur_event_pos + EVENT_ID_SIZE);
-    t->which_event++;
-    t->current_event_time = getEventTime(t);
+  pos = tf->event.data;
+
+  if(tf->event.data_size < 0) goto error;
+
+  pos += (size_t)tf->event.data_size;
+  
+  tf->event.offset = pos - tf->buffer.head;
+  
+  if(tf->event.offset == tf->buf_size - tf->buffer.lost_size) {
+    ret = ERANGE;
+    goto found;
   }
+  g_assert(tf->event.offset < tf->buf_size - tf->buffer.lost_size);
 
-  return 0;
-}
+found:
+  return ret;
 
+error:
+  g_error("Error in ltt_seek_next_event for tracefile %s",
+      g_quark_to_string(tf->name));
+  return ENOPROTOOPT;
+}
 
+#if 0
 /*****************************************************************************
  *Function name
- *    getCyclePerNsec : calculate cycles per nsec for current block
- *    MD: should have tracefile_read the whole block, so we know the
- *    total of cycles in it before being called.
+ *    calc_nsecs_per_cycle : calculate nsecs per cycle for current block
+ *
+ *    1.0 / (freq(khz) *1000)  * 1000000000
  *Input Params
  *    t               : tracefile
  ****************************************************************************/
+/* from timer_tsc.c */
+#define CYC2NS_SCALE_FACTOR 10
+static guint32 calc_nsecs_per_cycle(LttTracefile * tf)
+{
+  //return 1e6 / (double)tf->buffer.freq;
+  guint32 cpu_mhz = tf->buffer.freq / 1000;
+  guint32 cyc2ns_scale = (1000 << CYC2NS_SCALE_FACTOR)/cpu_mhz;
+  
+  return cyc2ns_scale;
+ // return 1e6 / (double)tf->buffer.freq;
+}
 
-void getCyclePerNsec(LttTracefile * t)
+static guint64 cycles_2_ns(LttTracefile *tf, guint64 cycles)
 {
-  LttTime           lBufTotalTime; /* Total time for this buffer */
-  double            lBufTotalNSec; /* Total time for this buffer in nsecs */
-  LttCycleCount     lBufTotalCycle;/* Total cycles for this buffer */
+  return (cycles * tf->buffer.cyc2ns_scale) >> CYC2NS_SCALE_FACTOR;
+}
+#endif //0
 
-  /* Calculate the total time for this buffer */
-  lBufTotalTime = ltt_time_sub(
-       ltt_get_time(t->trace->reverse_byte_order, &t->a_block_end->time),
-       ltt_get_time(t->trace->reverse_byte_order, &t->a_block_start->time));
+#if 0
+void setFieldsOffset(LttTracefile *tf, LttEventType *evT,void *evD)
+{
+  LttField * rootFld = evT->root_field;
+  //  rootFld->base_address = evD;
 
-  /* Calculate the total cycles for this bufffer */
-  lBufTotalCycle  = ltt_get_uint64(t->trace->reverse_byte_order,
-                          &t->a_block_end->cycle_count);
-  lBufTotalCycle -= ltt_get_uint64(t->trace->reverse_byte_order,
-                          &t->a_block_start->cycle_count);
+  if(likely(rootFld))
+    rootFld->field_size = getFieldtypeSize(tf, evT->facility,
+        evT, 0,0,rootFld, evD);  
+}
+#endif //0
 
-  /* Convert the total time to double */
-  lBufTotalNSec  = ltt_time_to_double(lBufTotalTime);
-  
-  t->nsec_per_cycle = (double)lBufTotalNSec / (double)lBufTotalCycle;
+/*****************************************************************************
+ *Function name
+ *    set_fields_offsets : set the precomputable offset of the fields
+ *Input params 
+ *    tracefile       : opened trace file  
+ *    event_type      : the event type
+ ****************************************************************************/
+
+void set_fields_offsets(LttTracefile *tf, LttEventType *event_type)
+{
+  LttField *field = event_type->root_field;
+  enum field_status fixed_root = FIELD_FIXED, fixed_parent = FIELD_FIXED;
 
-  /* Pre-multiply one overflow (2^32 cycles) by nsec_per_cycle */
-  t->one_overflow_nsec = t->nsec_per_cycle * (double)0x100000000ULL;
+  if(likely(field))
+    preset_field_type_size(tf, event_type, 0, 0, 
+        &fixed_root, &fixed_parent,
+        field);
 
 }
 
-/****************************************************************************
+
+/*****************************************************************************
  *Function name
- *    getEventTime    : obtain the time of an event 
- *                      NOTE : this function _really_ is on critical path.
+ *    preset_field_type_size : set the fixed sizes of the field type
  *Input params 
  *    tf              : tracefile
- *Return value
- *    LttTime        : the time of the event
+ *    event_type      : event type
+ *    offset_root     : offset from the root
+ *    offset_parent   : offset from the parent
+ *    fixed_root      : Do we know a fixed offset to the root ?
+ *    fixed_parent    : Do we know a fixed offset to the parent ?
+ *    field           : field
  ****************************************************************************/
-
-static inline LttTime getEventTime(LttTracefile * tf)
+void preset_field_type_size(LttTracefile *tf, LttEventType *event_type,
+    off_t offset_root, off_t offset_parent,
+    enum field_status *fixed_root, enum field_status *fixed_parent,
+    LttField *field)
 {
-  LttTime       time;
-  LttCycleCount cycle_count;      // cycle count for the current event
-  //LttCycleCount lEventTotalCycle; // Total cycles from start for event
-  gint64       lEventNSec;       // Total nsecs from start for event
-  LttTime       lTimeOffset;      // Time offset in struct LttTime
-  guint16       evId;
-
-  evId = ltt_get_uint16(tf->trace->reverse_byte_order,
-                          tf->cur_event_pos);
+  enum field_status local_fixed_root, local_fixed_parent;
+  guint i;
+  LttType *type;
   
-  cycle_count = ltt_get_uint32(tf->trace->reverse_byte_order,
-                          tf->cur_event_pos + EVENT_ID_SIZE);
+  g_assert(field->fixed_root == FIELD_UNKNOWN);
+  g_assert(field->fixed_parent == FIELD_UNKNOWN);
+  g_assert(field->fixed_size == FIELD_UNKNOWN);
 
-  gboolean comp_count = cycle_count < tf->pre_cycle_count;
+  type = field->field_type;
 
-  tf->pre_cycle_count = cycle_count;
-  
-  if(unlikely(comp_count)) {
-    /* Overflow */
-    tf->overflow_nsec += tf->one_overflow_nsec;
-    tf->count++; //increment overflow count
-  }
+  field->fixed_root = *fixed_root;
+  if(field->fixed_root == FIELD_FIXED)
+    field->offset_root = offset_root;
+  else
+    field->offset_root = 0;
 
-  if(unlikely(evId == TRACE_BLOCK_START)) {
-     lEventNSec = 0;
-  } else if(unlikely(evId == TRACE_BLOCK_END)) {
-    lEventNSec = ((double)
-          (ltt_get_uint64(tf->trace->reverse_byte_order,
-                            &tf->a_block_end->cycle_count) 
-           - ltt_get_uint64(tf->trace->reverse_byte_order,
-                            &tf->a_block_start->cycle_count))
-                           * tf->nsec_per_cycle);
-  }
-#if 0
-  /* If you want to make heart beat a special case and use their own 64 bits
-   * TSC, activate this.
-   */
-  else if(unlikely(evId == TRACE_TIME_HEARTBEAT)) {
-
-    tf->last_heartbeat = (TimeHeartbeat*)(tf->cur_event_pos+EVENT_HEADER_SIZE);
-    lEventNSec = ((double)(tf->last_heartbeat->cycle_count 
-                           - tf->a_block_start->cycle_count)
-                  * tf->nsec_per_cycle);
-  }
-#endif //0
-  else {
-    lEventNSec = (gint64)((double)cycle_count * tf->nsec_per_cycle)
-                                +tf->overflow_nsec;
+  field->fixed_parent = *fixed_parent;
+  if(field->fixed_parent == FIELD_FIXED)
+    field->offset_parent = offset_parent;
+  else
+    field->offset_parent = 0;
+
+  size_t current_root_offset;
+  size_t current_offset;
+  enum field_status current_child_status, final_child_status;
+  size_t max_size;
+
+  switch(type->type_class) {
+    case LTT_INT:
+    case LTT_UINT:
+    case LTT_FLOAT:
+    case LTT_ENUM:
+      field->field_size = ltt_type_size(tf->trace, type);
+      field->fixed_size = FIELD_FIXED;
+      break;
+    case LTT_POINTER:
+      field->field_size = (off_t)event_type->facility->pointer_size; 
+      field->fixed_size = FIELD_FIXED;
+      break;
+    case LTT_LONG:
+    case LTT_ULONG:
+      field->field_size = (off_t)event_type->facility->long_size; 
+      field->fixed_size = FIELD_FIXED;
+      break;
+    case LTT_SIZE_T:
+    case LTT_SSIZE_T:
+    case LTT_OFF_T:
+      field->field_size = (off_t)event_type->facility->size_t_size; 
+      field->fixed_size = FIELD_FIXED;
+      break;
+    case LTT_SEQUENCE:
+      local_fixed_root = FIELD_VARIABLE;
+      local_fixed_parent = FIELD_VARIABLE;
+      preset_field_type_size(tf, event_type,
+        0, 0, 
+        &local_fixed_root, &local_fixed_parent,
+        field->child[0]);
+      field->fixed_size = FIELD_VARIABLE;
+      field->field_size = 0;
+      *fixed_root = FIELD_VARIABLE;
+      *fixed_parent = FIELD_VARIABLE;
+      break;
+    case LTT_STRING:
+      field->fixed_size = FIELD_VARIABLE;
+      field->field_size = 0;
+      *fixed_root = FIELD_VARIABLE;
+      *fixed_parent = FIELD_VARIABLE;
+      break;
+    case LTT_ARRAY:
+      local_fixed_root = FIELD_VARIABLE;
+      local_fixed_parent = FIELD_VARIABLE;
+      preset_field_type_size(tf, event_type,
+        0, 0, 
+        &local_fixed_root, &local_fixed_parent,
+        field->child[0]);
+      field->fixed_size = field->child[0]->fixed_size;
+      if(field->fixed_size == FIELD_FIXED) {
+        field->field_size = type->element_number * field->child[0]->field_size;
+      } else {
+        field->field_size = 0;
+        *fixed_root = FIELD_VARIABLE;
+        *fixed_parent = FIELD_VARIABLE;
+      }
+      break;
+    case LTT_STRUCT:
+      current_root_offset = field->offset_root;
+      current_offset = 0;
+      current_child_status = FIELD_FIXED;
+      for(i=0;i<type->element_number;i++) {
+        preset_field_type_size(tf, event_type,
+          current_root_offset, current_offset, 
+          fixed_root, &current_child_status,
+          field->child[i]);
+        if(current_child_status == FIELD_FIXED) {
+          current_root_offset += field->child[i]->field_size;
+          current_offset += field->child[i]->field_size;
+        } else {
+          current_root_offset = 0;
+          current_offset = 0;
+        }
+      }
+      if(current_child_status != FIELD_FIXED) {
+        *fixed_parent = current_child_status;
+        field->field_size = 0;
+        field->fixed_size = current_child_status;
+      } else {
+        field->field_size = current_offset;
+        field->fixed_size = FIELD_FIXED;
+      }
+      break;
+    case LTT_UNION:
+      current_root_offset = field->offset_root;
+      current_offset = 0;
+      max_size = 0;
+      final_child_status = FIELD_FIXED;
+      for(i=0;i<type->element_number;i++) {
+        enum field_status current_root_child_status = FIELD_FIXED;
+        enum field_status current_child_status = FIELD_FIXED;
+        preset_field_type_size(tf, event_type,
+          current_root_offset, current_offset, 
+          &current_root_child_status, &current_child_status,
+          field->child[i]);
+        if(current_child_status != FIELD_FIXED)
+          final_child_status = current_child_status;
+        else
+          max_size = max(max_size, field->child[i]->field_size);
+      }
+      if(final_child_status != FIELD_FIXED) {
+        *fixed_root = final_child_status;
+        *fixed_parent = final_child_status;
+        field->field_size = 0;
+        field->fixed_size = current_child_status;
+      } else {
+        field->field_size = max_size;
+        field->fixed_size = FIELD_FIXED;
+      }
+      break;
   }
 
-  lTimeOffset = ltt_time_from_uint64(lEventNSec);
-  
-  time = ltt_time_add(ltt_get_time(tf->trace->reverse_byte_order,
-                                   &tf->a_block_start->time), lTimeOffset);  
-  
-  return time;
 }
 
+
 /*****************************************************************************
  *Function name
- *    setFieldsOffset : set offset of the fields
+ *    check_fields_compatibility : Check for compatibility between two fields :
+ *    do they use the same inner structure ?
  *Input params 
- *    tracefile       : opened trace file  
- *    evT             : the event type
- *    evD             : event data, it may be NULL
+ *    event_type1     : event type
+ *    event_type2     : event type
+ *    field1          : field
+ *    field2          : field
+ *Returns : 0 if identical
+ *          1 if not.
  ****************************************************************************/
-
-void setFieldsOffset(LttTracefile *tf,LttEventType *evT,void *evD,LttTrace* t)
+gint check_fields_compatibility(LttEventType *event_type1,
+    LttEventType *event_type2,
+    LttField *field1, LttField *field2)
 {
-  LttField * rootFld = evT->root_field;
-  //  rootFld->base_address = evD;
-
-  if(likely(rootFld))
-    rootFld->field_size = getFieldtypeSize(tf, evT, 0,0,rootFld, evD,t);  
+  guint different = 0;
+  guint i;
+  LttType *type1;
+  LttType *type2;
+  
+  if(field1 == NULL) {
+    if(field2 == NULL) goto end;
+    else {
+      different = 1;
+      goto end;
+    }
+  } else if(field2 == NULL) {
+    different = 1;
+    goto end;
+  }
+  
+  g_assert(field1->fixed_root != FIELD_UNKNOWN);
+  g_assert(field2->fixed_root != FIELD_UNKNOWN);
+  g_assert(field1->fixed_parent != FIELD_UNKNOWN);
+  g_assert(field2->fixed_parent != FIELD_UNKNOWN);
+  g_assert(field1->fixed_size != FIELD_UNKNOWN);
+  g_assert(field2->fixed_size != FIELD_UNKNOWN);
+
+  type1 = field1->field_type;
+  type2 = field2->field_type;
+
+  if(type1->type_class != type2->type_class) {
+    different = 1;
+    goto end;
+  }
+  if(type1->element_name != type2->element_name) {
+    different = 1;
+    goto end;
+  }
+    
+  switch(type1->type_class) {
+    case LTT_INT:
+    case LTT_UINT:
+    case LTT_FLOAT:
+    case LTT_POINTER:
+    case LTT_LONG:
+    case LTT_ULONG:
+    case LTT_SIZE_T:
+    case LTT_SSIZE_T:
+    case LTT_OFF_T:
+      if(field1->field_size != field2->field_size) {
+        different = 1;
+        goto end;
+      }
+      break;
+    case LTT_ENUM:
+      if(type1->element_number != type2->element_number) {
+        different = 1;
+        goto end;
+      }
+      for(i=0;i<type1->element_number;i++) {
+        if(type1->enum_strings[i] != type2->enum_strings[i]) {
+          different = 1;
+          goto end;
+        }
+      }
+      break;
+    case LTT_SEQUENCE:
+      /* Two elements : size and child */
+      g_assert(type1->element_number != type2->element_number);
+      for(i=0;i<type1->element_number;i++) {
+        if(check_fields_compatibility(event_type1, event_type2,
+          field1->child[0], field2->child[0])) {
+          different = 1;
+          goto end;
+        }
+      }
+      break;
+    case LTT_STRING:
+      break;
+    case LTT_ARRAY:
+      if(field1->field_size != field2->field_size) {
+        different = 1;
+        goto end;
+      }
+      /* Two elements : size and child */
+      g_assert(type1->element_number != type2->element_number);
+      for(i=0;i<type1->element_number;i++) {
+        if(check_fields_compatibility(event_type1, event_type2,
+          field1->child[0], field2->child[0])) {
+          different = 1;
+          goto end;
+        }
+      }
+      break;
+    case LTT_STRUCT:
+    case LTT_UNION:
+      if(type1->element_number != type2->element_number) {
+        different = 1;
+        break;
+      }
+      for(i=0;i<type1->element_number;i++) {
+        if(check_fields_compatibility(event_type1, event_type2,
+          field1->child[0], field2->child[0])) {
+          different = 1;
+          goto end;
+        }
+      }
+      break;
+  }
+end:
+  return different;
 }
 
+
+
+
+#if 0
 /*****************************************************************************
  *Function name
  *    getFieldtypeSize: get the size of the field type (primitive type)
  *Input params 
- *    tracefile       : opened trace file 
  *    evT             : event type
  *    offsetRoot      : offset from the root
  *    offsetParent    : offset from the parrent
@@ -1488,151 +2318,146 @@ void setFieldsOffset(LttTracefile *tf,LttEventType *evT,void *evD,LttTrace* t)
  *    int             : size of the field
  ****************************************************************************/
 
-static inline gint getFieldtypeSize(LttTracefile * t,
+static inline gint getFieldtypeSize(LttTracefile *tf,
        LttEventType * evT, gint offsetRoot,
-            gint offsetParent, LttField * fld, void *evD, LttTrace *trace)
+            gint offsetParent, LttField * fld, void *evD)
 {
   gint size, size1, element_number, i, offset1, offset2;
   LttType * type = fld->field_type;
 
-  if(unlikely(t && evT->latest_block==t->which_block &&
-                   evT->latest_event==t->which_event)){
-    size = fld->field_size;
-    goto end_getFieldtypeSize;
-  } else {
-    /* This likely has been tested with gcov : half of them.. */
-    if(unlikely(fld->field_fixed == 1)){
-      /* tested : none */
-      if(unlikely(fld == evT->root_field)) {
-        size = fld->field_size;
-        goto end_getFieldtypeSize;
-      }
+  /* This likely has been tested with gcov : half of them.. */
+  if(unlikely(fld->field_fixed == 1)){
+    /* tested : none */
+    if(unlikely(fld == evT->root_field)) {
+      size = fld->field_size;
+      goto end_getFieldtypeSize;
     }
+  }
 
-    /* From gcov profiling : half string, half struct, can we gain something
-     * from that ? (Mathieu) */
-    switch(type->type_class) {
-      case LTT_ARRAY:
-        element_number = (int) type->element_number;
-        if(fld->field_fixed == -1){
-          size = getFieldtypeSize(t, evT, offsetRoot,
-                                  0,fld->child[0], NULL, trace);
-          if(size == 0){ //has string or sequence
-            fld->field_fixed = 0;
-          }else{
-            fld->field_fixed = 1;
-            size *= element_number; 
-          }
-        }else if(fld->field_fixed == 0){// has string or sequence
-          size = 0;
-          for(i=0;i<element_number;i++){
-            size += getFieldtypeSize(t, evT, offsetRoot+size,size, 
-            fld->child[0], evD+size, trace);
-          }
-        }else size = fld->field_size;
-        if(unlikely(!evD)){
-          fld->fixed_root    = (offsetRoot==-1)   ? 0 : 1;
-          fld->fixed_parent  = (offsetParent==-1) ? 0 : 1;
-        }
-
-        break;
-
-      case LTT_SEQUENCE:
-        size1 = (int) ltt_type_size(trace, type);
-        if(fld->field_fixed == -1){
-          fld->sequ_number_size = size1;
+  /* From gcov profiling : half string, half struct, can we gain something
+   * from that ? (Mathieu) */
+  switch(type->type_class) {
+    case LTT_ARRAY:
+      element_number = (int) type->element_number;
+      if(fld->field_fixed == -1){
+        size = getFieldtypeSize(tf, evT, offsetRoot,
+                                0,fld->child[0], NULL);
+        if(size == 0){ //has string or sequence
           fld->field_fixed = 0;
-          size = getFieldtypeSize(t, evT, offsetRoot,
-                                  0,fld->child[0], NULL, trace);      
-          fld->element_size = size;
-        }else{//0: sequence
-          element_number = getIntNumber(t->trace->reverse_byte_order,size1,evD);
-          type->element_number = element_number;
-          if(fld->element_size > 0){
-            size = element_number * fld->element_size;
-          }else{//sequence has string or sequence
-            size = 0;
-            for(i=0;i<element_number;i++){
-              size += getFieldtypeSize(t, evT, offsetRoot+size+size1,size+size1, 
-                                       fld->child[0], evD+size+size1, trace);
-            }
-          }
-          size += size1;
-        }
-        if(unlikely(!evD)){
-          fld->fixed_root    = (offsetRoot==-1)   ? 0 : 1;
-          fld->fixed_parent  = (offsetParent==-1) ? 0 : 1;
+        }else{
+          fld->field_fixed = 1;
+          size *= element_number; 
         }
-
-        break;
-        
-      case LTT_STRING:
+      }else if(fld->field_fixed == 0){// has string or sequence
         size = 0;
-        if(fld->field_fixed == -1){
-          fld->field_fixed = 0;
-        }else{//0: string
-          /* Hope my implementation is faster than strlen (Mathieu) */
-          char *ptr=(char*)evD;
-          size = 1;
-          /* from gcov : many many strings are empty, make it the common case.*/
-          while(unlikely(*ptr != '\0')) { size++; ptr++; }
-          //size = ptr - (char*)evD + 1; //include end : '\0'
+        for(i=0;i<element_number;i++){
+          size += getFieldtypeSize(tf, evT, offsetRoot+size,size, 
+          fld->child[0], evD+size);
         }
+      }else size = fld->field_size;
+      if(unlikely(!evD)){
         fld->fixed_root    = (offsetRoot==-1)   ? 0 : 1;
         fld->fixed_parent  = (offsetParent==-1) ? 0 : 1;
+      }
 
-        break;
-        
-      case LTT_STRUCT:
-        element_number = (int) type->element_number;
-        size = 0;
-        /* tested with gcov */
-        if(unlikely(fld->field_fixed == -1)){
-          offset1 = offsetRoot;
-          offset2 = 0;
+      break;
+
+    case LTT_SEQUENCE:
+      size1 = (int) ltt_type_size(fac, type);
+      if(fld->field_fixed == -1){
+        fld->sequ_number_size = size1;
+        fld->field_fixed = 0;
+        size = getFieldtypeSize(evT, offsetRoot,
+                                0,fld->child[0], NULL);      
+        fld->element_size = size;
+      }else{//0: sequence
+        element_number = getIntNumber(tf,size1,evD);
+        type->element_number = element_number;
+        if(fld->element_size > 0){
+          size = element_number * fld->element_size;
+        }else{//sequence has string or sequence
+          size = 0;
           for(i=0;i<element_number;i++){
-            size1=getFieldtypeSize(t, evT,offset1,offset2,
-                                   fld->child[i], NULL, trace);
-            if(likely(size1 > 0 && size >= 0)){
-              size += size1;
-              if(likely(offset1 >= 0)) offset1 += size1;
-                offset2 += size1;
-            }else{
-              size = -1;
-              offset1 = -1;
-              offset2 = -1;
-            }
+            size += getFieldtypeSize(tf, evT,
+                                     offsetRoot+size+size1,size+size1, 
+                                     fld->child[0], evD+size+size1);
           }
-          if(unlikely(size == -1)){
-             fld->field_fixed = 0;
-             size = 0;
-          }else fld->field_fixed = 1;
-        }else if(likely(fld->field_fixed == 0)){
-          offset1 = offsetRoot;
-          offset2 = 0;
-          for(i=0;unlikely(i<element_number);i++){
-            size=getFieldtypeSize(t,evT,offset1,offset2,
-                                  fld->child[i],evD+offset2, trace);
-            offset1 += size;
-            offset2 += size;
-          }      
-          size = offset2;
-        }else size = fld->field_size;
+        }
+        size += size1;
+      }
+      if(unlikely(!evD)){
         fld->fixed_root    = (offsetRoot==-1)   ? 0 : 1;
         fld->fixed_parent  = (offsetParent==-1) ? 0 : 1;
-        break;
+      }
 
-      default:
-        if(unlikely(fld->field_fixed == -1)){
-          size = (int) ltt_type_size(trace, type);
-          fld->field_fixed = 1;
-        }else size = fld->field_size;
-        if(unlikely(!evD)){
-          fld->fixed_root    = (offsetRoot==-1)   ? 0 : 1;
-          fld->fixed_parent  = (offsetParent==-1) ? 0 : 1;
+      break;
+      
+    case LTT_STRING:
+      size = 0;
+      if(fld->field_fixed == -1){
+        fld->field_fixed = 0;
+      }else{//0: string
+        /* Hope my implementation is faster than strlen (Mathieu) */
+        char *ptr=(char*)evD;
+        size = 1;
+        /* from gcov : many many strings are empty, make it the common case.*/
+        while(unlikely(*ptr != '\0')) { size++; ptr++; }
+        //size = ptr - (char*)evD + 1; //include end : '\0'
+      }
+      fld->fixed_root    = (offsetRoot==-1)   ? 0 : 1;
+      fld->fixed_parent  = (offsetParent==-1) ? 0 : 1;
+
+      break;
+      
+    case LTT_STRUCT:
+      element_number = (int) type->element_number;
+      size = 0;
+      /* tested with gcov */
+      if(unlikely(fld->field_fixed == -1)){
+        offset1 = offsetRoot;
+        offset2 = 0;
+        for(i=0;i<element_number;i++){
+          size1=getFieldtypeSize(tf, evT,offset1,offset2,
+                                 fld->child[i], NULL);
+          if(likely(size1 > 0 && size >= 0)){
+            size += size1;
+            if(likely(offset1 >= 0)) offset1 += size1;
+              offset2 += size1;
+          }else{
+            size = -1;
+            offset1 = -1;
+            offset2 = -1;
+          }
         }
-        break;
-    }
+        if(unlikely(size == -1)){
+           fld->field_fixed = 0;
+           size = 0;
+        }else fld->field_fixed = 1;
+      }else if(likely(fld->field_fixed == 0)){
+        offset1 = offsetRoot;
+        offset2 = 0;
+        for(i=0;unlikely(i<element_number);i++){
+          size=getFieldtypeSize(tf, evT, offset1, offset2,
+                                fld->child[i], evD+offset2);
+          offset1 += size;
+          offset2 += size;
+        }      
+        size = offset2;
+      }else size = fld->field_size;
+      fld->fixed_root    = (offsetRoot==-1)   ? 0 : 1;
+      fld->fixed_parent  = (offsetParent==-1) ? 0 : 1;
+      break;
+
+    default:
+      if(unlikely(fld->field_fixed == -1)){
+        size = (int) ltt_type_size(LTT_GET_BO(tf), type);
+        fld->field_fixed = 1;
+      }else size = fld->field_size;
+      if(unlikely(!evD)){
+        fld->fixed_root    = (offsetRoot==-1)   ? 0 : 1;
+        fld->fixed_parent  = (offsetParent==-1) ? 0 : 1;
+      }
+      break;
   }
 
   fld->offset_root     = offsetRoot;
@@ -1643,64 +2468,66 @@ end_getFieldtypeSize:
 
   return size;
 }
-
+#endif //0
 
 /*****************************************************************************
  *Function name
- *    getIntNumber    : get an integer number
+ *    ltt_get_int    : get an integer number
  *Input params 
+ *    reverse_byte_order: must we reverse the byte order ?
  *    size            : the size of the integer
- *    evD             : the event data
+ *    ptr             : the data pointer
  *Return value
  *    gint64          : a 64 bits integer
  ****************************************************************************/
 
-gint64 getIntNumber(gboolean reverse_byte_order, int size, void *evD)
+gint64 ltt_get_int(gboolean reverse_byte_order, gint size, void *data)
 {
-  gint64 i;
+  gint64 val;
 
   switch(size) {
-    case 1: i = *((gint8*)evD); break;
-    case 2: i = ltt_get_int16(reverse_byte_order, evD); break;
-    case 4: i = ltt_get_int32(reverse_byte_order, evD); break;
-    case 8: i = ltt_get_int64(reverse_byte_order, evD); break;
-    default: i = ltt_get_int64(reverse_byte_order, evD);
-             g_critical("getIntNumber : integer size %d unknown", size);
+    case 1: val = *((gint8*)data); break;
+    case 2: val = ltt_get_int16(reverse_byte_order, data); break;
+    case 4: val = ltt_get_int32(reverse_byte_order, data); break;
+    case 8: val = ltt_get_int64(reverse_byte_order, data); break;
+    default: val = ltt_get_int64(reverse_byte_order, data);
+             g_critical("get_int : integer size %d unknown", size);
              break;
   }
 
-  return i;
+  return val;
 }
-#if 0
+
 /*****************************************************************************
  *Function name
- *    getDataEndianType : get the data type size and endian type of the local
- *                        machine
+ *    ltt_get_uint    : get an unsigned integer number
  *Input params 
- *    size              : size of data type
- *    endian            : endian type, little or big
+ *    reverse_byte_order: must we reverse the byte order ?
+ *    size            : the size of the integer
+ *    ptr             : the data pointer
+ *Return value
+ *    guint64         : a 64 bits unsigned integer
  ****************************************************************************/
 
-void getDataEndianType(LttArchSize * size, LttArchEndian * endian)
+guint64 ltt_get_uint(gboolean reverse_byte_order, gint size, void *data)
 {
-  int i = 1;
-  char c = (char) i;
-  int sizeInt=sizeof(int), sizeLong=sizeof(long), sizePointer=sizeof(void *);
+  guint64 val;
 
-  if(c == 1) *endian = LTT_LITTLE_ENDIAN;
-  else *endian = LTT_BIG_ENDIAN;
+  switch(size) {
+    case 1: val = *((gint8*)data); break;
+    case 2: val = ltt_get_uint16(reverse_byte_order, data); break;
+    case 4: val = ltt_get_uint32(reverse_byte_order, data); break;
+    case 8: val = ltt_get_uint64(reverse_byte_order, data); break;
+    default: val = ltt_get_uint64(reverse_byte_order, data);
+             g_critical("get_uint : unsigned integer size %d unknown",
+                 size);
+             break;
+  }
 
-  if(sizeInt == 2 && sizeLong == 4 && sizePointer == 4) 
-    *size = LTT_LP32;
-  else if(sizeInt == 4 && sizeLong == 4 && sizePointer == 4) 
-    *size = LTT_ILP32;
-  else if(sizeInt == 4 && sizeLong == 8 && sizePointer == 8) 
-    *size = LTT_LP64;
-  else if(sizeInt == 8 && sizeLong == 8 && sizePointer == 8) 
-    *size = LTT_ILP64;
-  else *size = LTT_UNKNOWN;
+  return val;
 }
-#endif //0
+
+
 /* get the node name of the system */
 
 char * ltt_trace_system_description_node_name (LttSystemDescription * s)
@@ -1748,3 +2575,13 @@ void ltt_tracefile_copy(LttTracefile *dest, const LttTracefile *src)
   *dest = *src;
 }
 
+/* Before library loading... */
+
+static void __attribute__((constructor)) init(void)
+{
+  LTT_FACILITY_NAME_HEARTBEAT = g_quark_from_string("heartbeat");
+  LTT_EVENT_NAME_HEARTBEAT = g_quark_from_string("heartbeat");
+  
+  LTT_TRACEFILE_NAME_FACILITIES = g_quark_from_string("/control/facilities");
+}
+
This page took 0.053648 seconds and 4 git commands to generate.