saved state fixes
[lttv.git] / ltt / branches / poly / lttv / lttv / tracecontext.c
index 5b6cdba7f379122440a57b3aa212ff5547d4315b..406431261da4f8f14c929f6e71fa97ad9ed8ee39 100644 (file)
@@ -45,13 +45,14 @@ gint compare_tracefile(gconstpointer a, gconstpointer b)
 }
 
 struct _LttvTraceContextPosition {
-  LttEventPosition **tf_pos;          /* Position in each trace           */
+  LttEventPosition **tf_pos;         /* Position in each tracefile       */
   guint nb_tracefile;                /* Number of tracefiles (check)     */
 };
 
 struct _LttvTracesetContextPosition {
-  LttvTraceContextPosition *t_pos;    /* Position in each trace           */
+  LttvTraceContextPosition *t_pos;    /* Position in each trace          */
   guint nb_trace;                    /* Number of traces (check)         */
+  LttTime timestamp;                 /* Current time at the saved position */
 };
 
 void lttv_context_init(LttvTracesetContext *self, LttvTraceset *ts)
@@ -101,7 +102,7 @@ lttv_context_new_tracefile_context(LttvTracesetContext *self)
  ***************************************************************************/
 static void lttv_traceset_context_compute_time_span(
                                           LttvTracesetContext *self,
-                                                                 TimeInterval time_span)
+                                                                 TimeInterval *time_span)
 {
   LttvTraceset * traceset = self->ts;
   int numTraces = lttv_traceset_number(traceset);
@@ -110,10 +111,10 @@ static void lttv_traceset_context_compute_time_span(
   LttvTraceContext *tc;
   LttTrace * trace;
 
-  time_span.startTime.tv_sec = 0;
-  time_span.startTime.tv_nsec = 0;
-  time_span.endTime.tv_sec = 0;
-  time_span.endTime.tv_nsec = 0;
+  time_span->start_time.tv_sec = 0;
+  time_span->start_time.tv_nsec = 0;
+  time_span->end_time.tv_sec = 0;
+  time_span->end_time.tv_nsec = 0;
   
   for(i=0; i<numTraces;i++){
     tc = self->traces[i];
@@ -122,17 +123,17 @@ static void lttv_traceset_context_compute_time_span(
     ltt_trace_time_span_get(trace, &s, &e);
 
     if(i==0){
-      time_span.startTime = s;
-      time_span.endTime   = e;
+      time_span->start_time = s;
+      time_span->end_time   = e;
     }else{
-      if(s.tv_sec < time_span.startTime.tv_sec 
-          || (s.tv_sec == time_span.startTime.tv_sec 
-               && s.tv_nsec < time_span.startTime.tv_nsec))
-             time_span.startTime = s;
-      if(e.tv_sec > time_span.endTime.tv_sec
-          || (e.tv_sec == time_span.endTime.tv_sec 
-               && e.tv_nsec > time_span.endTime.tv_nsec))
-        time_span.endTime = e;      
+      if(s.tv_sec < time_span->start_time.tv_sec 
+          || (s.tv_sec == time_span->start_time.tv_sec 
+               && s.tv_nsec < time_span->start_time.tv_nsec))
+             time_span->start_time = s;
+      if(e.tv_sec > time_span->end_time.tv_sec
+          || (e.tv_sec == time_span->end_time.tv_sec 
+               && e.tv_nsec > time_span->end_time.tv_nsec))
+        time_span->end_time = e;      
     }
   }
 }
@@ -188,11 +189,10 @@ init(LttvTracesetContext *self, LttvTraceset *ts)
       tfc->a = g_object_new(LTTV_ATTRIBUTE_TYPE, NULL);
     }
   }
+  self->pqueue = g_tree_new(compare_tracefile);
   lttv_process_traceset_seek_time(self, null_time);
-  lttv_traceset_context_compute_time_span(self, self->time_span);
-  self->e = NULL;
+  lttv_traceset_context_compute_time_span(self, &self->time_span);
 
-  self->pqueue = g_tree_new(compare_tracefile);
 }
 
 
@@ -599,7 +599,7 @@ void lttv_process_traceset_begin(LttvTracesetContext *self,
 /* Note : a _middle must be preceded from a _seek or another middle */
 guint lttv_process_traceset_middle(LttvTracesetContext *self,
                               LttTime end,
-                              unsigned nb_events,
+                              guint nb_events,
                               const LttvTracesetContextPosition *end_position)
 {
   GTree *pqueue = self->pqueue;
@@ -612,6 +612,8 @@ guint lttv_process_traceset_middle(LttvTracesetContext *self,
 
   unsigned count = 0;
 
+  gboolean last_ret = FALSE; /* return value of the last hook list called */
+
   /* Get the next event from the pqueue, call its hooks, 
      reinsert in the pqueue the following event from the same tracefile 
      unless the tracefile is finished or the event is later than the 
@@ -620,9 +622,9 @@ guint lttv_process_traceset_middle(LttvTracesetContext *self,
   while(TRUE) {
     tfc = NULL;
     g_tree_foreach(pqueue, get_first, &tfc);
+    /* End of traceset : tfc is NULL */
     if(tfc == NULL)
     {
-      self->e = event;
       return count;
     }
 
@@ -634,12 +636,12 @@ guint lttv_process_traceset_middle(LttvTracesetContext *self,
      * break the loop.
      */
 
-    if(count >= nb_events ||
-       lttv_traceset_context_ctx_pos_compare(self,
-                                             end_position) >= 0 ||
+    if(last_ret == TRUE ||
+       count >= nb_events ||
+     ((end_position==NULL)?FALSE:(lttv_traceset_context_ctx_pos_compare(self,
+                                                          end_position) >= 0))||
        ltt_time_compare(tfc->timestamp, end) >= 0)
     {
-      self->e = event;
       return count;
     }
 
@@ -651,17 +653,14 @@ guint lttv_process_traceset_middle(LttvTracesetContext *self,
     count++;
 
     id = ltt_event_eventtype_id(tfc->e);
-    lttv_hooks_call_merge(tfc->event, tfc,
+    last_ret = lttv_hooks_call_merge(tfc->event, tfc,
                         lttv_hooks_by_id_get(tfc->event_by_id, id), tfc);
 
     event = ltt_tracefile_read(tfc->tf);
     if(event != NULL) {
       tfc->e = event;
       tfc->timestamp = ltt_event_time(event);
-      if(tfc->timestamp.tv_sec < end.tv_sec ||
-                 (tfc->timestamp.tv_sec == end.tv_sec &&
-                tfc->timestamp.tv_nsec <= end.tv_nsec))
-             g_tree_insert(pqueue, tfc, tfc);
+           g_tree_insert(pqueue, tfc, tfc);
     }
   }
 }
@@ -692,17 +691,20 @@ void lttv_process_trace_seek_time(LttvTraceContext *self, LttTime start)
 
   LttEvent *event;
 
+  GTree *pqueue = self->ts_context->pqueue;
+
   nb_tracefile = ltt_trace_control_tracefile_number(self->t) +
       ltt_trace_per_cpu_tracefile_number(self->t);
 
   for(i = 0 ; i < nb_tracefile ; i++) {
     tfc = self->tracefiles[i];
     ltt_tracefile_seek_time(tfc->tf, start);
+    g_tree_remove(pqueue, tfc);
     event = ltt_tracefile_read(tfc->tf);
     tfc->e = event;
     if(event != NULL) {
       tfc->timestamp = ltt_event_time(event);
-      g_tree_insert(self->ts_context->pqueue, tfc, tfc);
+      g_tree_insert(pqueue, tfc, tfc);
     }
   }
 }
@@ -716,15 +718,6 @@ void lttv_process_traceset_seek_time(LttvTracesetContext *self, LttTime start)
 
   LttvTracefileContext *tfc;
 
-  /* Empty the pqueue */
-
-  while(TRUE){
-    tfc = NULL;
-    g_tree_foreach(self->pqueue, get_first, &tfc);
-    if(tfc == NULL) break;
-    g_tree_remove(self->pqueue, &(tfc->timestamp));
-  }
-
   nb_trace = lttv_traceset_number(self->ts);
   for(i = 0 ; i < nb_trace ; i++) {
     tc = self->traces[i];
@@ -733,6 +726,27 @@ void lttv_process_traceset_seek_time(LttvTracesetContext *self, LttTime start)
 }
 
 
+gboolean lttv_process_tracefile_seek_position(LttvTracefileContext *self, 
+                                              const LttEventPosition *pos)
+{
+  LttvTracefileContext *tfc = self;
+
+  LttEvent *event;
+
+  GTree *pqueue = self->t_context->ts_context->pqueue;
+  
+  ltt_tracefile_seek_position(tfc->tf, pos);
+  g_tree_remove(pqueue, tfc);
+  event = ltt_tracefile_read(tfc->tf);
+  tfc->e = event;
+  if(event != NULL) {
+    tfc->timestamp = ltt_event_time(event);
+    g_tree_insert(pqueue, tfc, tfc);
+  }
+
+
+}
+
 gboolean lttv_process_trace_seek_position(LttvTraceContext *self, 
                                         const LttvTraceContextPosition *pos)
 {
@@ -750,13 +764,7 @@ gboolean lttv_process_trace_seek_position(LttvTraceContext *self,
 
   for(i = 0 ; i < nb_tracefile ; i++) {
     tfc = self->tracefiles[i];
-    ltt_tracefile_seek_position(tfc->tf, pos->tf_pos[i]);
-    event = ltt_tracefile_read(tfc->tf);
-    tfc->e = event;
-    if(event != NULL) {
-      tfc->timestamp = ltt_event_time(event);
-      g_tree_insert(self->ts_context->pqueue, tfc, tfc);
-    }
+    lttv_process_tracefile_seek_position(tfc, pos->tf_pos[i]);
   }
 
   return TRUE;
@@ -779,15 +787,6 @@ gboolean lttv_process_traceset_seek_position(LttvTracesetContext *self,
   if(nb_trace != pos->nb_trace)
     return FALSE; /* Error */
 
-  /* Empty the pqueue */
-
-  while(TRUE){
-    tfc = NULL;
-    g_tree_foreach(self->pqueue, get_first, &tfc);
-    if(tfc == NULL) break;
-    g_tree_remove(self->pqueue, &(tfc->timestamp));
-  }
-
   for(i = 0 ; i < nb_trace ; i++) {
     tc = self->traces[i];
     sum_ret = sum_ret && lttv_process_trace_seek_position(tc, &pos->t_pos[i]);
@@ -850,6 +849,12 @@ lttv_trace_find_hook(LttTrace *t, char *facility, char *event_type,
 }
 
 
+LttvTracesetContextPosition *ltt_traceset_context_position_new()
+{
+  return g_new(LttvTracesetContextPosition,1);
+}
+
+
 void lttv_traceset_context_position_save(const LttvTracesetContext *self,
                                     LttvTracesetContextPosition *pos)
 {
@@ -862,6 +867,8 @@ void lttv_traceset_context_position_save(const LttvTracesetContext *self,
 
   LttEvent *event;
 
+  LttTime timestamp = self->time_span.end_time;
+
   pos->nb_trace = nb_trace = lttv_traceset_number(self->ts);
   pos->t_pos = g_new(LttvTraceContextPosition, nb_trace);
   
@@ -873,14 +880,21 @@ void lttv_traceset_context_position_save(const LttvTracesetContext *self,
 
     pos->t_pos[iter_trace].tf_pos = g_new(LttEventPosition*, nb_tracefile);
     for(iter_tracefile = 0; iter_tracefile < nb_tracefile; iter_tracefile++) {
-      pos->t_pos[iter_trace].tf_pos[iter_tracefile] 
-                                                = ltt_event_position_new();
       tfc = tc->tracefiles[iter_tracefile];
       event = tfc->e;
-      ltt_event_position(event, 
-                         pos->t_pos[iter_trace].tf_pos[iter_tracefile]);
+      if(event!=NULL) {
+        pos->t_pos[iter_trace].tf_pos[iter_tracefile] 
+                                                = ltt_event_position_new();
+        ltt_event_position(event, 
+                           pos->t_pos[iter_trace].tf_pos[iter_tracefile]);
+      } else {
+        pos->t_pos[iter_trace].tf_pos[iter_tracefile] = NULL;
+      }
+      if(ltt_time_compare(tfc->timestamp, timestamp) < 0)
+        timestamp = tfc->timestamp;
     }
   }
+  pos->timestamp = timestamp;
 }
 
 void lttv_traceset_context_position_destroy(LttvTracesetContextPosition *pos)
@@ -894,8 +908,8 @@ void lttv_traceset_context_position_destroy(LttvTracesetContextPosition *pos)
     for(iter_tracefile = 0; iter_tracefile < 
                         pos->t_pos[iter_trace].nb_tracefile;
                         iter_tracefile++) {
-      
-      g_free(pos->t_pos[iter_trace].tf_pos[iter_tracefile]);
+      if(pos->t_pos[iter_trace].tf_pos[iter_tracefile] != NULL)
+        g_free(pos->t_pos[iter_trace].tf_pos[iter_tracefile]);
     }
     g_free(pos->t_pos[iter_trace].tf_pos);
   }
@@ -903,6 +917,35 @@ void lttv_traceset_context_position_destroy(LttvTracesetContextPosition *pos)
 
 }
 
+void lttv_traceset_context_position_copy(LttvTracesetContextPosition *dest,
+                                   const LttvTracesetContextPosition *src)
+{
+  guint nb_trace, nb_tracefile;
+  guint iter_trace, iter_tracefile;
+  
+  nb_trace = dest->nb_trace = src->nb_trace;
+  dest->t_pos = g_new(LttvTraceContextPosition, nb_trace);
+  for(iter_trace = 0 ; iter_trace < nb_trace ; iter_trace++) {
+
+    nb_tracefile = dest->t_pos[iter_trace].nb_tracefile =
+                    src->t_pos[iter_trace].nb_tracefile;
+
+    for(iter_tracefile = 0; iter_tracefile < nb_tracefile; iter_tracefile++) {
+      dest->t_pos[iter_trace].tf_pos[iter_tracefile] = 
+                      ltt_event_position_new();
+      if(src->t_pos[iter_trace].tf_pos[iter_tracefile] != NULL)
+        ltt_event_position_copy(
+              dest->t_pos[iter_trace].tf_pos[iter_tracefile],
+              src->t_pos[iter_trace].tf_pos[iter_tracefile]);
+      else
+        dest->t_pos[iter_trace].tf_pos[iter_tracefile] = NULL;
+    }
+  }
+
+  dest->timestamp = src->timestamp;
+}
+
 gint lttv_traceset_context_ctx_pos_compare(const LttvTracesetContext *self,
                                         const LttvTracesetContextPosition *pos)
 {
@@ -976,3 +1019,19 @@ gint lttv_traceset_context_pos_pos_compare(
 }
 
 
+LttTime lttv_traceset_context_position_get_time(
+                                  const LttvTracesetContextPosition *pos)
+{
+  return pos->timestamp;
+}
+
+
+LttvTracefileContext *lttv_traceset_context_get_current_tfc(LttvTracesetContext *self)
+{
+  GTree *pqueue = self->pqueue;
+  LttvTracefileContext *tfc = NULL;
+
+  g_tree_foreach(pqueue, get_first, &tfc);
+
+  return tfc;
+}
This page took 0.027496 seconds and 4 git commands to generate.