From 95d36295f18e15c7f68a97fbab3eb1961d21cd70 Mon Sep 17 00:00:00 2001 From: Julien Desfossez Date: Wed, 25 Jan 2012 14:30:12 -0500 Subject: [PATCH] Introduce contexts 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 Signed-off-by: Mathieu Desnoyers --- converter/babeltrace.c | 53 ++++++++++++++++++++++-- include/Makefile.am | 3 +- include/babeltrace/babeltrace-internal.h | 5 +-- include/babeltrace/babeltrace.h | 4 +- include/babeltrace/format.h | 4 +- include/babeltrace/iterator-internal.h | 2 +- lib/Makefile.am | 3 +- lib/babeltrace.c | 31 +------------- lib/callbacks.c | 3 +- lib/iterator.c | 16 ++++--- 10 files changed, 76 insertions(+), 48 deletions(-) diff --git a/converter/babeltrace.c b/converter/babeltrace.c index 6af4a8b9..269ed463 100644 --- a/converter/babeltrace.c +++ b/converter/babeltrace.c @@ -20,8 +20,11 @@ #define _XOPEN_SOURCE 700 #include -#include +#include #include +#include +#include +#include #include #include #include @@ -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 ? : ""); exit(EXIT_SUCCESS); diff --git a/include/Makefile.am b/include/Makefile.am index 70cdd2a1..72770427 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -1,6 +1,7 @@ babeltraceinclude_HEADERS = \ babeltrace/babeltrace.h \ - babeltrace/format.h + babeltrace/format.h \ + babeltrace/context.h noinst_HEADERS = \ diff --git a/include/babeltrace/babeltrace-internal.h b/include/babeltrace/babeltrace-internal.h index e556092b..1c12a8be 100644 --- a/include/babeltrace/babeltrace-internal.h +++ b/include/babeltrace/babeltrace-internal.h @@ -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, diff --git a/include/babeltrace/babeltrace.h b/include/babeltrace/babeltrace.h index bc8bf76f..792eeab0 100644 --- a/include/babeltrace/babeltrace.h +++ b/include/babeltrace/babeltrace.h @@ -20,6 +20,7 @@ #include #include #include +#include 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); diff --git a/include/babeltrace/format.h b/include/babeltrace/format.h index 46120faf..93d1e251 100644 --- a/include/babeltrace/format.h +++ b/include/babeltrace/format.h @@ -22,11 +22,13 @@ */ #include -#include #include #include #include +/* forward declaration */ +struct ctf_stream_pos; + /* Parent trace descriptor */ struct trace_descriptor { }; diff --git a/include/babeltrace/iterator-internal.h b/include/babeltrace/iterator-internal.h index 511e9a92..3410f926 100644 --- a/include/babeltrace/iterator-internal.h +++ b/include/babeltrace/iterator-internal.h @@ -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 */ diff --git a/lib/Makefile.am b/lib/Makefile.am index fccf9ab5..cb3597d7 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -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 \ diff --git a/lib/babeltrace.c b/lib/babeltrace.c index 8fe44fbf..82f01f9b 100644 --- a/lib/babeltrace.c +++ b/lib/babeltrace.c @@ -19,41 +19,12 @@ */ #include +#include #include #include 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) { diff --git a/lib/callbacks.c b/lib/callbacks.c index ec6edba8..fc2c4b3d 100644 --- a/lib/callbacks.c +++ b/lib/callbacks.c @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -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; diff --git a/lib/iterator.c b/lib/iterator.c index 8e30a075..82cd4297 100644 --- a/lib/iterator.c +++ b/lib/iterator.c @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -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); } -- 2.34.1