Add time delta to ctf-text
[babeltrace.git] / converter / babeltrace-lib.c
index 86fadc4a6ea23f5a91d580070c30acb55ec36d76..518ec6129dcaf622e072ebeac0613cd5ad8c6361 100644 (file)
@@ -34,6 +34,8 @@
 #include <babeltrace/ctf-ir/metadata.h>
 #include <stdarg.h>
 
+int babeltrace_verbose, babeltrace_debug;
+
 struct stream_saved_pos {
        /*
         * Use file_stream pointer to check if the trace collection we
@@ -52,7 +54,12 @@ struct babeltrace_saved_pos {
 struct bt_callback {
        int prio;               /* Callback order priority. Lower first. Dynamically assigned from dependency graph. */
        void *private_data;
-       enum bt_cb_ret (*callback)(void *private_data, void *caller_data);
+       int flags;
+       struct bt_dependencies *depends;
+       struct bt_dependencies *weak_depends;
+       struct bt_dependencies *provides;
+       enum bt_cb_ret (*callback)(struct bt_ctf_data *ctf_data,
+                                  void *private_data);
 };
 
 struct bt_callback_chain {
@@ -75,7 +82,7 @@ struct babeltrace_iter {
        struct ptr_heap *stream_heap;
        struct trace_collection *tc;
        struct trace_collection_pos *end_pos;
-       GArray *callbacks;                              /* Array of struct bt_stream_hooks */
+       GArray *callbacks;                              /* Array of struct bt_stream_callbacks */
        struct bt_callback_chain main_callbacks;        /* For all events */
        /*
         * Flag indicating if dependency graph needs to be recalculated.
@@ -129,6 +136,103 @@ struct bt_dependencies *babeltrace_dependencies_create(const char *first, ...)
        return deps;
 }
 
+/*
+ * babeltrace_iter_add_callback: Add a callback to iterator.
+ */
+int babeltrace_iter_add_callback(struct babeltrace_iter *iter,
+               bt_event_name event, void *private_data, int flags,
+               enum bt_cb_ret (*callback)(struct bt_ctf_data *ctf_data,
+                                          void *private_data),
+               struct bt_dependencies *depends,
+               struct bt_dependencies *weak_depends,
+               struct bt_dependencies *provides)
+{
+       int i, stream_id;
+       gpointer *event_id_ptr;
+       unsigned long event_id;
+       struct trace_collection *tc = iter->tc;
+
+       for (i = 0; i < tc->array->len; i++) {
+               struct ctf_trace *tin;
+               struct trace_descriptor *td_read;
+
+               td_read = g_ptr_array_index(tc->array, i);
+               tin = container_of(td_read, struct ctf_trace, parent);
+
+               for (stream_id = 0; stream_id < tin->streams->len; stream_id++) {
+                       struct ctf_stream_class *stream;
+                       struct bt_stream_callbacks *bt_stream_cb = NULL;
+                       struct bt_callback_chain *bt_chain = NULL;
+                       struct bt_callback new_callback;
+
+                       stream = g_ptr_array_index(tin->streams, stream_id);
+
+                       /* find or create the bt_stream_callbacks for this stream */
+                       if (iter->callbacks->len >= stream_id) {
+                               bt_stream_cb = &g_array_index(iter->callbacks,
+                                               struct bt_stream_callbacks, stream->stream_id);
+                       } else {
+                               g_array_set_size(iter->callbacks, stream->stream_id);
+                       }
+                       if (!bt_stream_cb || !bt_stream_cb->per_id_callbacks) {
+                               struct bt_stream_callbacks new_stream_cb;
+                               new_stream_cb.per_id_callbacks = g_array_new(1, 1,
+                                               sizeof(struct bt_callback_chain));
+                               g_array_insert_val(iter->callbacks, stream->stream_id, new_stream_cb);
+                               bt_stream_cb = &g_array_index(iter->callbacks,
+                                               struct bt_stream_callbacks, stream->stream_id);
+                       }
+
+                       if (event) {
+                               /* find the event id */
+                               event_id_ptr = g_hash_table_lookup(stream->event_quark_to_id,
+                                               (gconstpointer) (unsigned long) event);
+                               /* event not found in this stream class */
+                               if (!event_id_ptr) {
+                                       printf("event not found\n");
+                                       continue;
+                               }
+                               event_id = (uint64_t)(unsigned long)*event_id_ptr;
+
+                               /* find or create the bt_callback_chain for this event */
+                               if (bt_stream_cb->per_id_callbacks->len >= event_id) {
+                                       bt_chain = &g_array_index(bt_stream_cb->per_id_callbacks,
+                                                       struct bt_callback_chain, event_id);
+                               } else {
+                                       g_array_set_size(bt_stream_cb->per_id_callbacks, event_id);
+                               }
+                               if (!bt_chain || !bt_chain->callback) {
+                                       struct bt_callback_chain new_chain;
+                                       new_chain.callback = g_array_new(1, 1, sizeof(struct bt_callback));
+                                       g_array_insert_val(bt_stream_cb->per_id_callbacks, event_id,
+                                                       new_chain);
+                                       bt_chain = &g_array_index(bt_stream_cb->per_id_callbacks,
+                                                       struct bt_callback_chain, event_id);
+                               }
+                       } else {
+                               /* callback for all events */
+                               if (!iter->main_callbacks.callback) {
+                                       iter->main_callbacks.callback = g_array_new(1, 1,
+                                                       sizeof(struct bt_callback));
+                               }
+                               bt_chain = &iter->main_callbacks;
+                       }
+
+                       new_callback.private_data = private_data;
+                       new_callback.flags = flags;
+                       new_callback.callback = callback;
+                       new_callback.depends = depends;
+                       new_callback.weak_depends = weak_depends;
+                       new_callback.provides = provides;
+
+                       /* TODO : take care of priority, for now just FIFO */
+                       g_array_append_val(bt_chain->callback, new_callback);
+               }
+       }
+
+       return 0;
+}
+
 static int stream_read_event(struct ctf_file_stream *sin)
 {
        int ret;
@@ -175,7 +279,7 @@ static int babeltrace_filestream_seek(struct ctf_file_stream *file_stream,
                 */
                break;
        case BT_SEEK_BEGIN:
-               ctf_move_pos_slow(&file_stream->pos, 0, SEEK_SET);
+               file_stream->pos.move_pos_slow(&file_stream->pos, 0, SEEK_SET);
                ret = stream_read_event(file_stream);
                break;
        case BT_SEEK_TIME:
@@ -243,6 +347,10 @@ struct babeltrace_iter *babeltrace_iter_create(struct trace_collection *tc,
        iter->stream_heap = g_new(struct ptr_heap, 1);
        iter->tc = tc;
        iter->end_pos = end_pos;
+       iter->callbacks = g_array_new(0, 1, sizeof(struct bt_stream_callbacks));
+       iter->recalculate_dep_graph = 0;
+       iter->main_callbacks.callback = NULL;
+       iter->dep_gc = g_ptr_array_new();
 
        ret = heap_init(iter->stream_heap, 0, stream_compare);
        if (ret < 0)
@@ -302,8 +410,33 @@ error_malloc:
 
 void babeltrace_iter_destroy(struct babeltrace_iter *iter)
 {
+       struct bt_stream_callbacks *bt_stream_cb;
+       struct bt_callback_chain *bt_chain;
+       int i, j;
+
        heap_free(iter->stream_heap);
        g_free(iter->stream_heap);
+
+       /* free all events callbacks */
+       if (iter->main_callbacks.callback)
+               g_array_free(iter->main_callbacks.callback, TRUE);
+
+       /* free per-event callbacks */
+       for (i = 0; i < iter->callbacks->len; i++) {
+               bt_stream_cb = &g_array_index(iter->callbacks,
+                               struct bt_stream_callbacks, i);
+               if (!bt_stream_cb || !bt_stream_cb->per_id_callbacks)
+                       continue;
+               for (j = 0; j < bt_stream_cb->per_id_callbacks->len; j++) {
+                       bt_chain = &g_array_index(bt_stream_cb->per_id_callbacks,
+                                       struct bt_callback_chain, j);
+                       if (bt_chain->callback) {
+                               g_array_free(bt_chain->callback, TRUE);
+                       }
+               }
+               g_array_free(bt_stream_cb->per_id_callbacks, TRUE);
+       }
+
        free(iter);
 }
 
@@ -336,6 +469,94 @@ end:
        return ret;
 }
 
+static
+struct ctf_stream_event *extract_ctf_stream_event(struct ctf_stream *stream)
+{
+       struct ctf_stream_class *stream_class = stream->stream_class;
+       struct ctf_event *event_class;
+       struct ctf_stream_event *event;
+       uint64_t id = stream->event_id;
+
+       if (id >= stream_class->events_by_id->len) {
+               fprintf(stdout, "[error] Event id %" PRIu64 " is outside range.\n", id);
+               return NULL;
+       }
+       event = g_ptr_array_index(stream->events_by_id, id);
+       if (!event) {
+               fprintf(stdout, "[error] Event id %" PRIu64 " is unknown.\n", id);
+               return NULL;
+       }
+       event_class = g_ptr_array_index(stream_class->events_by_id, id);
+       if (!event_class) {
+               fprintf(stdout, "[error] Event id %" PRIu64 " is unknown.\n", id);
+               return NULL;
+       }
+
+       return event;
+}
+
+static
+void process_callbacks(struct babeltrace_iter *iter,
+                      struct ctf_stream *stream)
+{
+       struct bt_stream_callbacks *bt_stream_cb;
+       struct bt_callback_chain *bt_chain;
+       struct bt_callback *cb;
+       int i;
+       enum bt_cb_ret ret;
+       struct bt_ctf_data ctf_data;
+
+       ctf_data.event = extract_ctf_stream_event(stream);
+       ctf_data.stream = stream;
+
+       /* process all events callback first */
+       if (iter->main_callbacks.callback) {
+               for (i = 0; i < iter->main_callbacks.callback->len; i++) {
+                       cb = &g_array_index(iter->main_callbacks.callback, struct bt_callback, i);
+                       if (!cb)
+                               goto end;
+                       ret = cb->callback(&ctf_data, cb->private_data);
+                       switch (ret) {
+                       case BT_CB_OK_STOP:
+                       case BT_CB_ERROR_STOP:
+                               goto end;
+                       default:
+                               break;
+                       }
+               }
+       }
+
+       /* process per event callbacks */
+       bt_stream_cb = &g_array_index(iter->callbacks,
+                       struct bt_stream_callbacks, stream->stream_id);
+       if (!bt_stream_cb || !bt_stream_cb->per_id_callbacks)
+               goto end;
+
+       if (stream->event_id > bt_stream_cb->per_id_callbacks->len)
+               goto end;
+       bt_chain = &g_array_index(bt_stream_cb->per_id_callbacks,
+                       struct bt_callback_chain, stream->event_id);
+       if (!bt_chain || !bt_chain->callback)
+               goto end;
+
+       for (i = 0; i < bt_chain->callback->len; i++) {
+               cb = &g_array_index(bt_chain->callback, struct bt_callback, i);
+               if (!cb)
+                       goto end;
+               ret = cb->callback(&ctf_data, cb->private_data);
+               switch (ret) {
+               case BT_CB_OK_STOP:
+               case BT_CB_ERROR_STOP:
+                       goto end;
+               default:
+                       break;
+               }
+       }
+
+end:
+       return;
+}
+
 int babeltrace_iter_read_event(struct babeltrace_iter *iter,
                struct ctf_stream **stream,
                struct ctf_stream_event **event)
@@ -351,6 +572,12 @@ int babeltrace_iter_read_event(struct babeltrace_iter *iter,
        }
        *stream = &file_stream->parent;
        *event = g_ptr_array_index((*stream)->events_by_id, (*stream)->event_id);
+
+       if ((*stream)->stream_id > iter->callbacks->len)
+               goto end;
+
+       process_callbacks(iter, *stream);
+
 end:
        return ret;
 }
@@ -384,3 +611,12 @@ end:
        babeltrace_iter_destroy(iter);
        return ret;
 }
+
+static
+void __attribute__((constructor)) init_babeltrace_lib(void)
+{
+       if (getenv("BABELTRACE_VERBOSE"))
+               babeltrace_verbose = 1;
+       if (getenv("BABELTRACE_DEBUG"))
+               babeltrace_debug = 1;
+}
This page took 0.0269 seconds and 4 git commands to generate.