From c34ccddd3910efe68ed20faf40b7da76282bb727 Mon Sep 17 00:00:00 2001 From: Mathieu Desnoyers Date: Thu, 1 Sep 2011 10:02:52 -0400 Subject: [PATCH] Add callback API Implementation still has to be done. Signed-off-by: Mathieu Desnoyers --- converter/babeltrace-lib.c | 71 ++++++++++++++++++++++++++++++++ include/babeltrace/babeltrace.h | 73 +++++++++++++++++++++++++++++++++ include/babeltrace/ctf/types.h | 3 ++ 3 files changed, 147 insertions(+) diff --git a/converter/babeltrace-lib.c b/converter/babeltrace-lib.c index 37975081..d078a690 100644 --- a/converter/babeltrace-lib.c +++ b/converter/babeltrace-lib.c @@ -32,6 +32,7 @@ #include #include #include +#include struct stream_saved_pos { /* @@ -48,6 +49,24 @@ struct babeltrace_saved_pos { GArray *stream_saved_pos; /* Contains struct stream_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); +}; + +struct bt_callback_chain { + GArray *callback; /* Array of struct bt_callback, ordered by priority */ +}; + +/* + * per id callbacks need to be per stream class because event ID vs + * event name mapping can vary from stream to stream. + */ +struct bt_stream_callbacks { + GArray *per_id_callbacks; /* Array of struct bt_callback_chain */ +}; + /* * struct babeltrace_iter: data structure representing an iterator on a trace * collection. @@ -56,8 +75,60 @@ 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 */ + struct bt_callback_chain main_callbacks; /* For all events */ + /* + * Flag indicating if dependency graph needs to be recalculated. + * Set by babeltrace_iter_add_callback(), and checked (and + * cleared) by upon entry into babeltrace_iter_read_event(). + * babeltrace_iter_read_event() is responsible for calling dep + * graph calculation if it sees this flag set. + */ + int recalculate_dep_graph; + /* + * Array of pointers to struct bt_dependencies, for garbage + * collection. We're not using a linked list here because each + * struct bt_dependencies can belong to more than one + * babeltrace_iter. + */ + GPtrArray *dep_gc; }; +struct bt_dependencies { + GArray *deps; /* Array of GQuarks */ + int refcount; /* free when decremented to 0 */ +}; + +static +struct bt_dependencies *_babeltrace_dependencies_create(const char *first, + va_list ap) +{ + const char *iter; + struct bt_dependencies *dep; + + dep = g_new0(struct bt_dependencies, 1); + dep->refcount = 1; + dep->deps = g_array_new(FALSE, TRUE, sizeof(GQuark)); + iter = first; + while (iter) { + GQuark q = g_quark_from_string(iter); + g_array_append_val(dep->deps, q); + iter = va_arg(ap, const char *); + } + return dep; +} + +struct bt_dependencies *babeltrace_dependencies_create(const char *first, ...) +{ + va_list ap; + struct bt_dependencies *deps; + + va_start(ap, first); + deps = _babeltrace_dependencies_create(first, ap); + va_end(ap); + return deps; +} + static int stream_read_event(struct ctf_file_stream *sin) { int ret; diff --git a/include/babeltrace/babeltrace.h b/include/babeltrace/babeltrace.h index 6c3fe967..eb15de00 100644 --- a/include/babeltrace/babeltrace.h +++ b/include/babeltrace/babeltrace.h @@ -17,12 +17,24 @@ * all copies or substantial portions of the Software. */ +#include + +typedef GQuark bt_event_name; + /* Forward declarations */ struct babeltrace_iter; struct trace_collection; struct ctf_stream_event; struct ctf_stream; struct babeltrace_saved_pos; +struct bt_dependencies; + +enum bt_cb_ret { + BT_CB_OK = 0, + BT_CB_OK_STOP = 1, + BT_CB_ERROR_STOP = 2, + BT_CB_ERROR_CONTINUE = 3, +}; struct trace_collection_pos { enum { @@ -102,4 +114,65 @@ int babeltrace_iter_read_event(struct babeltrace_iter *iter, struct ctf_stream **stream, struct ctf_stream_event **event); +/* + * Receives a variable number of strings as parameter, ended with NULL. + */ +struct bt_dependencies *babeltrace_dependencies_create(const char *first, ...); + +/* + * struct bt_dependencies must be destroyed explicitly if not passed as + * parameter to a babeltrace_iter_add_callback(). + */ +void babeltrace_dependencies_destroy(struct bt_dependencies *dep); + +/* + * babeltrace_iter_add_callback: Add a callback to iterator. + * + * @iter: trace collection iterator (input) + * @event: event to target. 0 for all events. + * @private_data: private data pointer to pass to the callback + * @flags: specific flags controlling the behavior of this callback + * (or'd). + * + * @callback: function pointer to call + * @depends: struct bt_dependency detailing the required computation results. + * Ends with 0. + * @weak_depends: struct bt_dependency detailing the optional computation + * results that can be optionally consumed by this + * callback. + * @provides: struct bt_dependency detailing the computation results + * provided by this callback. + * Ends with 0. + * + * "depends", "weak_depends" and "provides" memory is handled by the + * babeltrace library after this call succeeds or fails. These objects + * can still be used by the caller until the babeltrace iterator is + * destroyed, but they belong to the babeltrace library. + * + * (note to implementor: we need to keep a gptrarray of struct + * bt_dependencies to "garbage collect" in struct babeltrace_iter, and + * dependencies need to have a refcount to handle the case where they + * would be passed to more than one iterator. Upon iterator detroy, we + * iterate on all the gc ptrarray and decrement the refcounts, freeing + * if we reach 0.) + * (note to implementor: we calculate the dependency graph when + * babeltrace_iter_read_event() is executed after a + * babeltrace_iter_add_callback(). Beware that it is valid to create/add + * callbacks/read/add more callbacks/read some more.) + */ +int babeltrace_iter_add_callback(struct babeltrace_iter *iter, + bt_event_name event, void *private_data, int flags, + enum bt_cb_ret (*callback)(void *private_data, + void *caller_data), + struct bt_dependencies *depends, + struct bt_dependencies *weak_depends, + struct bt_dependencies *provides); + +/* + * For flags parameter above. + */ +enum { + BT_FLAGS_FREE_PRIVATE_DATA = (1 << 0), +}; + #endif /* _BABELTRACE_H */ diff --git a/include/babeltrace/ctf/types.h b/include/babeltrace/ctf/types.h index cebaca7a..54fd8462 100644 --- a/include/babeltrace/ctf/types.h +++ b/include/babeltrace/ctf/types.h @@ -31,6 +31,8 @@ #include #include +struct bt_stream_callbacks; + struct packet_index { off_t offset; /* offset of the packet in the file, in bytes */ off_t data_offset; /* offset of data within the packet, in bits */ @@ -60,6 +62,7 @@ struct ctf_stream_pos { size_t cur_index; /* current index in packet index */ int dummy; /* dummy position, for length calculation */ + struct bt_stream_callbacks *cb; /* Callbacks registered for iterator. */ }; static inline -- 2.34.1