initial port of the mainwindow
[lttv.git] / lttv / lttv / traceset.c
index b28efb3ac6f490c15377757ee6b2dc227a29fd0d..97499b59fff20a654ffd9029a953420577977e94 100644 (file)
 
 #include <lttv/traceset.h>
 #include <lttv/iattribute.h>
+#include <lttv/state.h>
+#include <lttv/hook.h>
 #include <stdio.h>
+#include <babeltrace/babeltrace.h>
 #include <babeltrace/context.h>
+#include <babeltrace/ctf/iterator.h>
+#include <babeltrace/ctf/events.h>
+
+/* To traverse a tree recursively */
+#include <fcntl.h>
+#include <fts.h>
+/* For the use of realpath*/
+#include <limits.h>
+#include <stdlib.h>
+/* For strcpy*/
+#include <string.h>
 
 /* A trace is a sequence of events gathered in the same tracing session. The
    events may be stored in several tracefiles in the same directory. 
    possibly to study the interactions between events in the different traces. 
 */
 
-struct _LttvTraceset {
-       char * filename;
-       GPtrArray *traces;
-       struct bt_context *context;
-       LttvAttribute *a;
-};
 
-
-struct _LttvTrace {
-  // Trace id for babeltrace
-       gint id;
-       LttvAttribute *a;
-       guint ref_count;
-};
-
-LttvTraceset *lttv_traceset_new() 
+LttvTraceset *lttv_traceset_new(void)
 {
        LttvTraceset *s;
+        struct bt_iter_pos begin_pos;
 
        s = g_new(LttvTraceset, 1);
        s->filename = NULL;
        s->traces = g_ptr_array_new();
        s->context = bt_context_create();
        s->a = g_object_new(LTTV_ATTRIBUTE_TYPE, NULL);
+       //TODO remove this when we have really mecanism
+       //s->tmpState = g_new(LttvTraceState *, 1);
+       //lttv_trace_state_init(s->tmpState,0);
+
+       s->iter = 0;
+        s->event_hooks = lttv_hooks_new();
+        
+       s->state_trace_handle_index =  g_ptr_array_new();
+
        return s;
 }
 
@@ -75,6 +85,30 @@ LttvTrace *lttv_trace_new(LttTrace *t)
        return new_trace;
 }
 #endif
+/*
+ * get_absolute_pathname : Return the unique pathname in the system
+ * 
+ * pathname is the relative path.
+ * 
+ * abs_pathname is being set to the absolute path.
+ * 
+ */
+void get_absolute_pathname(const gchar *pathname, gchar * abs_pathname)
+{
+  abs_pathname[0] = '\0';
+
+  if (realpath(pathname, abs_pathname) != NULL)
+    return;
+  else
+  {
+    /* error, return the original path unmodified */
+    strcpy(abs_pathname, pathname);
+    return;
+  }
+  return;
+}
+
+
 
 /*
  * lttv_trace_create : Create a trace from a path
@@ -106,6 +140,14 @@ static LttvTrace *lttv_trace_create(LttvTraceset *ts, const char *path)
   new_trace->a = g_object_new(LTTV_ATTRIBUTE_TYPE, NULL);
   new_trace->id = id;
   new_trace->ref_count = 0;
+  new_trace->traceset = ts;
+  new_trace->state = g_new(LttvTraceState,1);
+  lttv_trace_state_init(new_trace->state,new_trace);
+
+  /* Add the state to the trace_handle to state index */
+  g_ptr_array_set_size(ts->state_trace_handle_index,id+1);
+  g_ptr_array_index(ts->state_trace_handle_index,id) = new_trace->state;
+
   return new_trace;
 }
 
@@ -137,12 +179,18 @@ LttvTraceset *lttv_traceset_copy(LttvTraceset *s_orig)
        s = g_new(LttvTraceset, 1);
        s->filename = NULL;
        s->traces = g_ptr_array_new();
+       s->state_trace_handle_index = g_ptr_array_new();
        for(i=0;i<s_orig->traces->len;i++)
        {
                trace = g_ptr_array_index(s_orig->traces, i);
                trace->ref_count++;
 
+               /* WARNING: this is an alias, not a copy. */
                g_ptr_array_add(s->traces, trace);
+
+               g_ptr_array_set_size(s->state_trace_handle_index,trace->id+1);
+               g_ptr_array_index(s->state_trace_handle_index,trace->id) = trace->state;
+               
        }
        s->context = s_orig->context;
        bt_context_get(s->context);
@@ -200,24 +248,98 @@ struct bt_context *lttv_traceset_get_context(LttvTraceset *s)
        return s->context;
 }
 
+LttvTraceset *lttv_trace_get_traceset(LttvTrace *trace)
+{
+       return trace->traceset;
+}
+
+LttvHooks *lttv_traceset_get_hooks(LttvTraceset *s)
+{
+       return s->event_hooks;
+}
+
 void lttv_trace_destroy(LttvTrace *t) 
 {
        g_object_unref(t->a);
        g_free(t);
 }
 
-
 void lttv_traceset_add(LttvTraceset *s, LttvTrace *t) 
 {
        t->ref_count++;
        g_ptr_array_add(s->traces, t);
 }
 
-int lttv_traceset_add_path(LttvTraceset *ts, const char *trace_path)
+int lttv_traceset_add_path(LttvTraceset *ts, char *trace_path)
 {
-  // todo mdenis 2012-03-27: add trace recursively and update comment
-  int ret = lttv_traceset_create_trace(ts, trace_path);
-  return ret;
+
+       FTS *tree;
+       FTSENT *node;
+       char * const paths[2] = { trace_path, NULL };
+       int ret = -1;
+
+       ts->filename = trace_path;
+       
+       tree = fts_open(paths, FTS_NOCHDIR | FTS_LOGICAL, 0);
+       if (tree == NULL) {
+               g_warning("Cannot traverse \"%s\" for reading.\n",
+                               trace_path);
+               return ret;
+       }
+
+       int dirfd, metafd;
+       while ((node = fts_read(tree))) {
+
+               if (!(node->fts_info & FTS_D))
+                       continue;
+
+               dirfd = open(node->fts_accpath, 0);
+               if (dirfd < 0) {
+                       g_warning("Unable to open trace "
+                                       "directory file descriptor : %s.", node->fts_accpath);
+                       ret = dirfd;
+                       goto error;
+               }
+
+               // Check if a metadata file exists in the current directory
+               metafd = openat(dirfd, "metadata", O_RDONLY);
+               if (metafd < 0) {
+                       ret = close(dirfd);
+                       if (ret < 0) {
+                               g_warning("Unable to open metadata "
+                                               "file descriptor : %s.", node->fts_accpath);
+                               goto error;
+                       }
+               } else {
+                       ret = close(metafd);
+                       if (ret < 0) {
+                               g_warning("Unable to close metadata "
+                                               "file descriptor : %s.", node->fts_accpath);
+                               goto error;
+                       }
+                       ret = close(dirfd);
+                       if (ret < 0) {
+                               g_warning("Unable to close trace "
+                                               "directory file descriptor : %s.", node->fts_accpath);
+                               goto error;
+                       }
+
+                       ret = lttv_traceset_create_trace(ts, node->fts_accpath);
+                       if (ret < 0) {
+                               g_warning("Opening trace \"%s\" from %s "
+                                               "for reading.", node->fts_accpath, trace_path);
+                               goto error;
+                       }
+               }
+       }
+
+error:
+       ret = fts_close(tree);
+       if (ret < 0) {
+               g_warning("Unable to close tree  "
+                               "file descriptor : %s.", trace_path);
+       }
+       return ret;
 }
 
 unsigned lttv_traceset_number(LttvTraceset *s) 
@@ -258,12 +380,6 @@ LttvAttribute *lttv_trace_attribute(LttvTrace *t)
        return t->a;
 }
 
-#ifdef BABEL_CLEANUP
-LttTrace *lttv_trace(LttvTrace *t)
-{
-       return t->t;
-}
-#endif
 
 gint lttv_trace_get_id(LttvTrace *t)
 {
@@ -291,3 +407,126 @@ guint lttv_trace_unref(LttvTrace * t)
        return t->ref_count;
 }
 
+guint lttv_trace_get_num_cpu(LttvTrace *t)
+{
+#warning "TODO - Set the right number of CPU"
+       return 24;
+}
+
+LttvTracesetPosition *lttv_traceset_create_position(LttvTraceset *traceset)
+{
+       LttvTracesetPosition *traceset_pos;
+       
+       traceset_pos = g_new(LttvTracesetPosition, 1);
+
+       /* Check in the new passed */
+       if(traceset_pos == NULL) {
+               return NULL;
+       }
+
+       traceset_pos->iter = traceset->iter;
+       traceset_pos->bt_pos = bt_iter_get_pos(bt_ctf_get_iter(traceset->iter));
+       
+       return traceset_pos;
+}
+
+void lttv_traceset_destroy_position(LttvTracesetPosition *traceset_pos)
+{
+       bt_iter_free_pos(traceset_pos->bt_pos);
+       g_free(traceset_pos);
+}
+
+void lttv_traceset_seek_to_position(LttvTracesetPosition *traceset_pos)
+{
+       bt_iter_set_pos(traceset_pos->iter, traceset_pos->bt_pos);
+}
+
+guint lttv_traceset_get_cpuid_from_event(LttvEvent *event)
+{
+       struct definition *scope;
+       unsigned long timestamp;
+       unsigned int cpu_id;
+       
+       struct bt_ctf_event *ctf_event = event->bt_event;
+       timestamp = bt_ctf_get_timestamp(ctf_event);
+       if (timestamp == -1ULL) {
+               return 0;
+       }
+       scope = bt_ctf_get_top_level_scope(ctf_event, BT_STREAM_PACKET_CONTEXT);
+       if (bt_ctf_field_get_error()) {
+               return 0;
+       }
+       cpu_id = bt_ctf_get_uint64(bt_ctf_get_field(ctf_event, scope, "cpu_id"));
+       if (bt_ctf_field_get_error()) {
+               return 0;
+       } else {
+               return cpu_id;
+       }
+}
+/*
+ * lttv_traceset_get_timestamp_begin : returns the  minimum timestamp of 
+ * all the traces in the traceset.
+ * 
+ */
+
+guint64 lttv_traceset_get_timestamp_begin(LttvTraceset *traceset)
+{
+  struct bt_context *bt_ctx;
+  bt_ctx = lttv_traceset_get_context(traceset);
+  guint64 timestamp_min = G_MAXUINT64, timestamp_cur = 0;
+  int i;
+  int trace_count;
+  LttvTrace *currentTrace;
+  trace_count = traceset->traces->len;
+  if(trace_count == 0) 
+    timestamp_min = 0;
+  else{
+    timestamp_min = G_MAXUINT64;
+    
+    for(i = 0; i < trace_count;i++)
+    {
+      currentTrace = g_ptr_array_index(traceset->traces,i);
+      timestamp_cur = bt_trace_handle_get_timestamp_begin(bt_ctx, currentTrace->id);
+      if(timestamp_cur < timestamp_min)
+       timestamp_min = timestamp_cur;
+    }
+  }
+  return timestamp_min;
+}
+
+/*
+ * lttv_traceset_get_timestamp_end: returns the maximum timestamp of
+ * all the traces in the traceset.
+ * 
+ */
+guint64 lttv_traceset_get_timestamp_end(LttvTraceset *traceset)
+{
+  struct bt_context *bt_ctx;
+  bt_ctx = lttv_traceset_get_context(traceset);
+  guint64 timestamp_max, timestamp_cur = 0;
+  int i;
+  int trace_count;
+  LttvTrace *currentTrace;
+  trace_count = traceset->traces->len;
+  
+  if(trace_count == 0)
+    timestamp_max = 1;
+  else
+  {
+    timestamp_max = 0;
+    for(i =0; i < trace_count;i++)
+    {
+      currentTrace = g_ptr_array_index(traceset->traces,i);
+      timestamp_cur = bt_trace_handle_get_timestamp_end(bt_ctx, currentTrace->id);
+      if(timestamp_cur > timestamp_max)
+       timestamp_max = timestamp_cur;
+    }
+  }
+  return timestamp_max;
+  
+}
+
+const char *lttv_traceset_get_name_from_event(LttvEvent *event)
+{
+       return bt_ctf_event_name(event->bt_event);
+}
This page took 0.025918 seconds and 4 git commands to generate.