Introduce contexts
authorJulien Desfossez <julien.desfossez@efficios.com>
Wed, 25 Jan 2012 19:30:12 +0000 (14:30 -0500)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Wed, 25 Jan 2012 19:30:12 +0000 (14:30 -0500)
The context is an object in which the trace_collection is opened. The
iterator (and later callbacks) depend on the context. This patch, does
not modify the behaviour, it is just the introduction of the contexts.
Also moved convert_trace() in the converter code since it is not a
public API function.

Signed-off-by: Julien Desfossez <julien.desfossez@efficios.com>
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
converter/babeltrace.c
include/Makefile.am
include/babeltrace/babeltrace-internal.h
include/babeltrace/babeltrace.h
include/babeltrace/format.h
include/babeltrace/iterator-internal.h
lib/Makefile.am
lib/babeltrace.c
lib/callbacks.c
lib/iterator.c

index 6af4a8b9bccf1a816c4993bf10b42507b0b8f19e..269ed4639d75b5db219013898d007e45b0f8f5e1 100644 (file)
 
 #define _XOPEN_SOURCE 700
 #include <config.h>
-#include <babeltrace/babeltrace-internal.h>
+#include <babeltrace/babeltrace.h>
 #include <babeltrace/format.h>
+#include <babeltrace/context.h>
+#include <babeltrace/ctf/types.h>
+#include <babeltrace/ctf-text/types.h>
 #include <popt.h>
 #include <errno.h>
 #include <stdlib.h>
@@ -240,6 +243,44 @@ static void trace_collection_add(struct trace_collection *tc,
        g_ptr_array_add(tc->array, td);
 }
 
+int convert_trace(struct trace_descriptor *td_write,
+                 struct bt_context *ctx)
+{
+       struct babeltrace_iter *iter;
+       struct ctf_stream *stream;
+       struct ctf_stream_event *event;
+       struct ctf_text_stream_pos *sout;
+       struct trace_collection_pos begin_pos;
+       int ret;
+
+       sout = container_of(td_write, struct ctf_text_stream_pos,
+                       trace_descriptor);
+
+       begin_pos.type = BT_SEEK_BEGIN;
+       iter = babeltrace_iter_create(ctx, &begin_pos, NULL);
+       if (!iter) {
+               ret = -1;
+               goto error_iter;
+       }
+       while (babeltrace_iter_read_event(iter, &stream, &event) == 0) {
+               ret = sout->parent.event_cb(&sout->parent, stream);
+               if (ret) {
+                       fprintf(stdout, "[error] Writing event failed.\n");
+                       goto end;
+               }
+               ret = babeltrace_iter_next(iter);
+               if (ret < 0)
+                       goto end;
+       }
+       ret = 0;
+
+end:
+       babeltrace_iter_destroy(iter);
+error_iter:
+       return ret;
+}
+
+
 /*
  * traverse_dir() is the callback functiion for File Tree Walk (nftw).
  * it receives the path of the current entry (file, dir, link..etc) with
@@ -287,6 +328,7 @@ int main(int argc, char **argv)
        int ret;
        struct format *fmt_write;
        struct trace_descriptor *td_write;
+       struct bt_context *ctx;
 
        ret = parse_options(argc, argv);
        if (ret < 0) {
@@ -348,7 +390,11 @@ int main(int argc, char **argv)
                                                " no output was generated\n");
                return 0;
        }
-
+       ctx = bt_context_create(&trace_collection_read);
+       if (!ctx) {
+               fprintf(stdout, "Error allocating a new context\n");
+               goto error_td_read;
+       }
        td_write = fmt_write->open_trace(NULL, opt_output_path, O_RDWR, NULL, NULL);
        if (!td_write) {
                fprintf(stdout, "Error opening trace \"%s\" for writing.\n\n",
@@ -356,7 +402,7 @@ int main(int argc, char **argv)
                goto error_td_write;
        }
 
-       ret = convert_trace(td_write, &trace_collection_read);
+       ret = convert_trace(td_write, ctx);
        if (ret) {
                fprintf(stdout, "Error printing trace.\n\n");
                goto error_copy_trace;
@@ -364,6 +410,7 @@ int main(int argc, char **argv)
 
        fmt_write->close_trace(td_write);
        finalize_trace_collection(&trace_collection_read);
+       bt_context_destroy(ctx);
        printf_verbose("finished converting. Output written to:\n%s\n",
                        opt_output_path ? : "<stdout>");
        exit(EXIT_SUCCESS);
index 70cdd2a18320793035c8a6018ca8d757e85db247..727704273288196ec855ad84769094b13ffa41d3 100644 (file)
@@ -1,6 +1,7 @@
 babeltraceinclude_HEADERS = \
        babeltrace/babeltrace.h \
-       babeltrace/format.h
+       babeltrace/format.h \
+       babeltrace/context.h
 
 
 noinst_HEADERS = \
index e556092b76136799d1873988092464c5cd482c8c..1c12a8be23f2ec7cb6687a3b539a90b27eb09573 100644 (file)
@@ -23,12 +23,9 @@ extern int babeltrace_verbose, babeltrace_debug;
 
 struct trace_descriptor;
 struct trace_collection {
-       GPtrArray *array;
+       GPtrArray *array; /* struct trace_descriptor */
 };
 
-int convert_trace(struct trace_descriptor *td_write,
-                 struct trace_collection *trace_collection_read);
-
 extern int opt_all_field_names,
        opt_scope_field_names,
        opt_header_field_names,
index bc8bf76f1ec6dca248ad099c51e243f7a0c946b5..792eeab08196b6c6b75efe39344973b2533912cf 100644 (file)
@@ -20,6 +20,7 @@
 #include <glib.h>
 #include <stdint.h>
 #include <babeltrace/format.h>
+#include <babeltrace/context.h>
 
 typedef GQuark bt_event_name;
 
@@ -30,6 +31,7 @@ struct ctf_stream_event;
 struct ctf_stream;
 struct babeltrace_saved_pos;
 struct bt_dependencies;
+struct bt_context;
 
 enum bt_cb_ret {
        BT_CB_OK                = 0,
@@ -69,7 +71,7 @@ struct bt_ctf_data {
  * creation. By default, if end_pos is NULL, a BT_SEEK_END (end of
  * trace) is the EOF criterion.
  */
-struct babeltrace_iter *babeltrace_iter_create(struct trace_collection *tc,
+struct babeltrace_iter *babeltrace_iter_create(struct bt_context *ctx,
                struct trace_collection_pos *begin_pos,
                struct trace_collection_pos *end_pos);
 
index 46120fafe66de55e8e8e344038f42429c2f4c196..93d1e25146495920c40537b942d556b54541391c 100644 (file)
  */
 
 #include <babeltrace/types.h>
-#include <babeltrace/ctf/types.h>
 #include <stdint.h>
 #include <stdio.h>
 #include <glib.h>
 
+/* forward declaration */
+struct ctf_stream_pos;
+
 /* Parent trace descriptor */
 struct trace_descriptor {
 };
index 511e9a92e291caa955af5ddd8c1f6323802dad58..3410f92646b6488bead9c383c2907d29fa968c66 100644 (file)
@@ -27,7 +27,7 @@
  */
 struct babeltrace_iter {
        struct ptr_heap *stream_heap;
-       struct trace_collection *tc;
+       struct bt_context *ctx;
        struct trace_collection_pos *end_pos;
        GArray *callbacks;                              /* Array of struct bt_stream_callbacks */
        struct bt_callback_chain main_callbacks;        /* For all events */
index fccf9ab55da1ca7176e968c4f075110ff7bfbc49..cb3597d7622b7a99f9a1688d5a3c421b20515c82 100644 (file)
@@ -6,7 +6,8 @@ lib_LTLIBRARIES = libbabeltrace.la
 
 libbabeltrace_la_SOURCES = babeltrace.c \
                           callbacks.c \
-                          iterator.c
+                          iterator.c \
+                          context.c
 
 libbabeltrace_la_LIBADD = \
        $(top_builddir)/types/libbabeltrace_types.la \
index 8fe44fbf35caa842661357b50488dd2161e36103..82f01f9b4fc795f89bf59ffd57144313b83a8564 100644 (file)
  */
 
 #include <babeltrace/babeltrace.h>
+#include <babeltrace/context.h>
 #include <babeltrace/ctf-text/types.h>
 #include <stdlib.h>
 
 int babeltrace_verbose, babeltrace_debug;
 
-int convert_trace(struct trace_descriptor *td_write,
-                 struct trace_collection *trace_collection_read)
-{
-       struct babeltrace_iter *iter;
-       struct ctf_stream *stream;
-       struct ctf_stream_event *event;
-       struct ctf_text_stream_pos *sout;
-       struct trace_collection_pos begin_pos;
-       int ret = 0;
-
-       sout = container_of(td_write, struct ctf_text_stream_pos,
-                       trace_descriptor);
-
-       begin_pos.type = BT_SEEK_BEGIN;
-       iter = babeltrace_iter_create(trace_collection_read, &begin_pos, NULL);
-       while (babeltrace_iter_read_event(iter, &stream, &event) == 0) {
-               ret = sout->parent.event_cb(&sout->parent, stream);
-               if (ret) {
-                       fprintf(stdout, "[error] Writing event failed.\n");
-                       goto end;
-               }
-               ret = babeltrace_iter_next(iter);
-               if (ret < 0)
-                       goto end;
-       }
-end:
-       babeltrace_iter_destroy(iter);
-       return ret;
-}
-
 static
 void __attribute__((constructor)) init_babeltrace_lib(void)
 {
index ec6edba80ba76bf434e50964d1f51aba5c6f81aa..fc2c4b3d38e675e8d539e5854eed91ffd40d13f8 100644 (file)
@@ -21,6 +21,7 @@
 #include <babeltrace/babeltrace.h>
 #include <babeltrace/babeltrace-internal.h>
 #include <babeltrace/callbacks-internal.h>
+#include <babeltrace/context.h>
 #include <babeltrace/ctf-ir/metadata.h>
 #include <babeltrace/iterator-internal.h>
 #include <inttypes.h>
@@ -69,7 +70,7 @@ int babeltrace_iter_add_callback(struct babeltrace_iter *iter,
        int i, stream_id;
        gpointer *event_id_ptr;
        unsigned long event_id;
-       struct trace_collection *tc = iter->tc;
+       struct trace_collection *tc = iter->ctx->tc;
 
        for (i = 0; i < tc->array->len; i++) {
                struct ctf_trace *tin;
index 8e30a075a55302d305aee34520881926f4961ef4..82cd42979d5adbf053dd3c08f716ce805202b0dd 100644 (file)
@@ -21,6 +21,7 @@
 #include <stdlib.h>
 #include <babeltrace/babeltrace.h>
 #include <babeltrace/callbacks-internal.h>
+#include <babeltrace/context.h>
 #include <babeltrace/ctf/metadata.h>
 #include <babeltrace/iterator-internal.h>
 #include <babeltrace/prio_heap.h>
@@ -107,7 +108,7 @@ int babeltrace_iter_seek(struct babeltrace_iter *iter,
 {
        int i, stream_id;
        int ret = 0;
-       struct trace_collection *tc = iter->tc;
+       struct trace_collection *tc = iter->ctx->tc;
 
        for (i = 0; i < tc->array->len; i++) {
                struct ctf_trace *tin;
@@ -140,7 +141,7 @@ end:
        return ret;
 }
 
-struct babeltrace_iter *babeltrace_iter_create(struct trace_collection *tc,
+struct babeltrace_iter *babeltrace_iter_create(struct bt_context *ctx,
                struct trace_collection_pos *begin_pos,
                struct trace_collection_pos *end_pos)
 {
@@ -152,22 +153,24 @@ struct babeltrace_iter *babeltrace_iter_create(struct trace_collection *tc,
        if (!iter)
                goto error_malloc;
        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();
+       if (bt_context_get(ctx) != 0)
+               goto error_ctx;
+       iter->ctx = ctx;
 
        ret = heap_init(iter->stream_heap, 0, stream_compare);
        if (ret < 0)
                goto error_heap_init;
 
-       for (i = 0; i < tc->array->len; i++) {
+       for (i = 0; i < ctx->tc->array->len; i++) {
                struct ctf_trace *tin;
                struct trace_descriptor *td_read;
 
-               td_read = g_ptr_array_index(tc->array, i);
+               td_read = g_ptr_array_index(ctx->tc->array, i);
                tin = container_of(td_read, struct ctf_trace, parent);
 
                /* Populate heap with each stream */
@@ -210,6 +213,7 @@ error:
        heap_free(iter->stream_heap);
 error_heap_init:
        g_free(iter->stream_heap);
+error_ctx:
        free(iter);
 error_malloc:
        return NULL;
@@ -244,6 +248,8 @@ void babeltrace_iter_destroy(struct babeltrace_iter *iter)
                g_array_free(bt_stream_cb->per_id_callbacks, TRUE);
        }
 
+       bt_context_put(iter->ctx);
+
        free(iter);
 }
 
This page took 0.044612 seconds and 4 git commands to generate.