tracefile.c: Seek fix
[lttv.git] / ltt / tracefile.c
index 2dbbbcf119be2573dc1991afe5ad36fecf5ffc7a..012ce10801ec27cf63819614a477ccf5fa173e3d 100644 (file)
@@ -55,6 +55,8 @@
 
 /* from marker.c */
 extern long marker_update_fields_offsets(struct marker_info *info, const char *data);
+extern void marker_update_event_fields_offsets(GArray *fields_offsets,
+                                              struct marker_info *info);
 
 /* Tracefile names used in this file */
 
@@ -333,7 +335,13 @@ static gint ltt_tracefile_open(LttTrace *t, gchar * fileName, LttTracefile *tf)
     perror("Cannot map block for tracefile");
     goto close_file;
   }
-  
+  /* Create fields offset table */
+  tf->event.fields_offsets = g_array_sized_new(FALSE, FALSE,
+                                               sizeof(struct LttField), 1);
+  if (!tf->event.fields_offsets)
+    goto close_file;
+
   return 0;
 
   /* Error */
@@ -376,6 +384,7 @@ static void ltt_tracefile_close(LttTracefile *t)
   close(t->fd);
   if (t->buf_index)
     g_array_free(t->buf_index, TRUE);
+  g_array_free(t->event.fields_offsets, TRUE);
 }
 
 /****************************************************************************
@@ -544,12 +553,12 @@ static void ltt_tracefile_group_destroy(gpointer data)
   unsigned int i;
   LttTracefile *tf;
 
+  if (group->len > 0)
+    destroy_marker_data(g_array_index (group, LttTracefile, 0).mdata);
   for(i=0; i<group->len; i++) {
     tf = &g_array_index (group, LttTracefile, i);
-    if(tf->cpu_online) {
-      destroy_marker_data(tf->mdata);
+    if(tf->cpu_online)
       ltt_tracefile_close(tf);
-    }
   }
   g_array_free(group, TRUE);
 }
@@ -585,7 +594,8 @@ static int open_tracefiles(LttTrace *trace, gchar *root_path, gchar *relative_pa
   DIR *dir = opendir(root_path);
   struct dirent *entry;
   struct stat stat_buf;
-  int ret;
+  int ret, i;
+  struct marker_data *mdata;
   
   gchar path[PATH_MAX];
   int path_len;
@@ -595,7 +605,6 @@ static int open_tracefiles(LttTrace *trace, gchar *root_path, gchar *relative_pa
   gchar rel_path[PATH_MAX];
   gchar *rel_path_ptr;
   LttTracefile tmp_tf;
-  struct marker_data **mdata;
 
   if(dir == NULL) {
     perror(root_path);
@@ -658,6 +667,7 @@ static int open_tracefiles(LttTrace *trace, gchar *root_path, gchar *relative_pa
       g_debug("Tracefile name is %s and number is %u", 
           g_quark_to_string(name), num);
 
+      mdata = NULL;
       tmp_tf.cpu_online = 1;
       tmp_tf.cpu_num = num;
       tmp_tf.name = name;
@@ -672,6 +682,9 @@ static int open_tracefiles(LttTrace *trace, gchar *root_path, gchar *relative_pa
         group = g_array_sized_new (FALSE, TRUE, sizeof(LttTracefile), 10);
         g_datalist_id_set_data_full(&trace->tracefiles, name,
                                  group, ltt_tracefile_group_destroy);
+        mdata = allocate_marker_data();
+        if (!mdata)
+          g_error("Error in allocating marker data");
       }
 
       /* Add the per cpu tracefile to the named group */
@@ -679,13 +692,15 @@ static int open_tracefiles(LttTrace *trace, gchar *root_path, gchar *relative_pa
       if(num+1 > old_len)
         group = g_array_set_size(group, num+1);
 
+      g_assert(group->len > 0);
+      if (!mdata)
+        mdata = g_array_index (group, LttTracefile, 0).mdata;
+
       g_array_index (group, LttTracefile, num) = tmp_tf;
       g_array_index (group, LttTracefile, num).event.tracefile = 
         &g_array_index (group, LttTracefile, num);
-      mdata = &g_array_index (group, LttTracefile, num).mdata;
-      *mdata = allocate_marker_data();
-      if (!*mdata)
-        g_error("Error in allocating marker data");
+      for (i = 0; i < group->len; i++)
+        g_array_index (group, LttTracefile, i).mdata = mdata;
     }
   }
   
@@ -1079,8 +1094,11 @@ int ltt_tracefile_seek_time(LttTracefile *tf, LttTime time)
       }
 
     } else if(ltt_time_compare(time, tf->buffer.begin.timestamp) < 0) {
-      /* go to lower part */
-      high = block_num - 1;
+      /*
+       * Go to lower part. We don't want block_num - 1 since block_num
+       * can equal low , in which case high < low.
+       */
+      high = block_num;
     } else if(ltt_time_compare(time, tf->buffer.end.timestamp) > 0) {
       /* go to higher part */
       low = block_num + 1;
@@ -1587,8 +1605,9 @@ void ltt_update_event_size(LttTracefile *tf)
     if (info->size != -1)
       size = info->size;
     else
-      size = marker_update_fields_offsets(marker_get_info_from_id(tf->mdata,
-                                   tf->event.event_id), tf->event.data);
+      size = marker_update_fields_offsets(info, tf->event.data);
+    /* Update per-tracefile offsets */
+    marker_update_event_fields_offsets(tf->event.fields_offsets, info);
   }
 
   tf->event.data_size = size;
This page took 0.026029 seconds and 4 git commands to generate.