From c1e730febb17c72e96c356d54b8df31d5c0c0b94 Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Wed, 26 Apr 2017 09:14:43 -0400 Subject: [PATCH] Add bt_ctf_trace_get_stream_count() and bt_ctf_trace_get_stream() MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit With the two new functions you can access the streams of a trace. The use case is for any component to be able to check if it has received a "stream end" notification for each stream of a given trace and discard anything associated with this trace if it's the case. Signed-off-by: Philippe Proulx Signed-off-by: Jérémie Galarneau --- include/babeltrace/ctf-ir/trace.h | 44 +++++++++++++++++++++++++++++++ lib/ctf-ir/trace.c | 30 +++++++++++++++++++++ tests/lib/test_ctf_writer.c | 9 +++++++ tests/lib/test_trace_listener.c | 1 + 4 files changed, 84 insertions(+) diff --git a/include/babeltrace/ctf-ir/trace.h b/include/babeltrace/ctf-ir/trace.h index 91b09f80..858d9681 100644 --- a/include/babeltrace/ctf-ir/trace.h +++ b/include/babeltrace/ctf-ir/trace.h @@ -76,6 +76,10 @@ can add an event class to a stream class with bt_ctf_stream_class_add_event_class(). You can add a stream class to a trace class with bt_ctf_trace_add_stream_class(). +You can access the streams of a trace, that is, the streams which were +created from the trace's stream classes with bt_ctf_stream_create(), +with bt_ctf_trace_get_stream(). + A trace class owns the trace packet header \link ctfirfieldtypes field type\endlink, which represents the \c trace.packet.header CTF scope. This field type describes the @@ -720,6 +724,46 @@ extern int bt_ctf_trace_add_stream_class(struct bt_ctf_trace *trace_class, /** @} */ +/** +@name Stream children functions +@{ +*/ + +/** +@brief Returns the number of streams contained in the CTF IR trace + class \p trace_class. + +@param[in] trace_class Trace class of which to get the number + of children streams. +@returns Number of children streams + contained in \p trace_class, or a negative + value on error. + +@prenotnull{trace_class} +@postrefcountsame{trace_class} +*/ +extern int bt_ctf_trace_get_stream_count(struct bt_ctf_trace *trace_class); + +/** +@brief Returns the stream at index \p index in the CTF IR trace + class \p trace_class. + +@param[in] trace_class Trace class of which to get the stream. +@param[in] index Index of the stream to find. +@returns Stream at index \p index, or \c NULL + on error. + +@prenotnull{trace_class} +@pre \p index is lesser than the number of streams contained in + the trace class \p trace_class (see + bt_ctf_trace_get_stream_count()). +@postrefcountsame{trace_class} +*/ +extern struct bt_ctf_stream *bt_ctf_trace_get_stream( + struct bt_ctf_trace *trace_class, int index); + +/** @} */ + /** @name Misc. functions @{ diff --git a/lib/ctf-ir/trace.c b/lib/ctf-ir/trace.c index 9514c6d7..b850b4ff 100644 --- a/lib/ctf-ir/trace.c +++ b/lib/ctf-ir/trace.c @@ -715,6 +715,36 @@ end: return ret; } +int bt_ctf_trace_get_stream_count(struct bt_ctf_trace *trace) +{ + int ret; + + if (!trace) { + ret = -1; + goto end; + } + + ret = trace->streams->len; + +end: + return ret; +} + +struct bt_ctf_stream *bt_ctf_trace_get_stream(struct bt_ctf_trace *trace, + int index) +{ + struct bt_ctf_stream *stream = NULL; + + if (!trace || index >= trace->streams->len) { + goto end; + } + + stream = bt_get(g_ptr_array_index(trace->streams, index)); + +end: + return stream; +} + int bt_ctf_trace_get_stream_class_count(struct bt_ctf_trace *trace) { int ret; diff --git a/tests/lib/test_ctf_writer.c b/tests/lib/test_ctf_writer.c index 9f76f7b3..1995d3d9 100644 --- a/tests/lib/test_ctf_writer.c +++ b/tests/lib/test_ctf_writer.c @@ -2795,6 +2795,7 @@ int main(int argc, char **argv) struct bt_ctf_clock_class *ret_clock_class; struct bt_ctf_stream_class *stream_class, *ret_stream_class; struct bt_ctf_stream *stream1; + struct bt_ctf_stream *stream; const char *ret_string; const unsigned char *ret_uuid; unsigned char tmp_uuid[16] = { 0 }; @@ -3272,8 +3273,16 @@ int main(int argc, char **argv) /* Instantiate a stream and append events */ ret = bt_ctf_writer_add_clock(writer, clock); assert(ret == 0); + ok(bt_ctf_trace_get_stream_count(trace) == 0, + "bt_ctf_trace_get_stream_count() succeeds and returns the correct value (0)"); stream1 = bt_ctf_writer_create_stream(writer, stream_class); ok(stream1, "Instanciate a stream class from writer"); + ok(bt_ctf_trace_get_stream_count(trace) == 1, + "bt_ctf_trace_get_stream_count() succeeds and returns the correct value (1)"); + stream = bt_ctf_trace_get_stream(trace, 0); + ok(stream == stream1, + "bt_ctf_trace_get_stream() succeeds and returns the correct value"); + BT_PUT(stream); /* * Creating a stream through a writer adds the given stream diff --git a/tests/lib/test_trace_listener.c b/tests/lib/test_trace_listener.c index 469399fb..18255eea 100644 --- a/tests/lib/test_trace_listener.c +++ b/tests/lib/test_trace_listener.c @@ -24,6 +24,7 @@ #include #include #include +#include #include #include -- 2.34.1