From 056deb59eb33e87c06c0067907768ab08aff74a2 Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Sat, 10 Aug 2019 23:24:13 -0400 Subject: [PATCH] lib: bt_graph_create(): accept MIP version This patch makes bt_graph_create() accept the message interchange protocol (MIP) version with which to configure the graph. This MIP version will usually come from bt_get_greatest_operative_mip_version() or bt_get_maximal_mip_version(). The components can access the configured MIP version with bt_self_component_get_graph_mip_version(). As of this patch the only possible MIP version is 0. bt_graph_create() does not accept a MIP version that is greater than bt_get_maximal_mip_version() because almost all the library functions need to know the configured MIP version to behave accordingly, should the semantics change in the future. In Python, Graph.__init__() accepts a `mip_version` parameter which defaults to 0. A Python component can get the configured MIP version of its operating graph with the `_UserComponent._graph_mip_version` property. All the sites where bt_graph_create() is called pass 0 to it. This is correct for tests as we always control the component classes and initialization parameters, so we know that 0 is always supported by them. The locations where bt_get_greatest_operative_mip_version() must be used are the CLI and `bt2.TraceCollectionMessageIterator`. Those sites will be addressed by subsequent patches. Signed-off-by: Philippe Proulx Change-Id: I324a317922a8caa5557043ae02b97e58fc91d92d Reviewed-on: https://review.lttng.org/c/babeltrace/+/1878 Tested-by: jenkins Reviewed-by: Simon Marchi --- include/babeltrace2/graph/graph.h | 2 +- include/babeltrace2/graph/self-component.h | 3 +++ src/bindings/python/bt2/bt2/component.py | 5 +++++ src/bindings/python/bt2/bt2/graph.py | 9 +++++++-- src/cli/babeltrace2.c | 2 +- src/lib/graph/component.c | 9 +++++++++ src/lib/graph/graph.c | 7 ++++++- src/lib/graph/graph.h | 2 ++ tests/bindings/python/bt2/test_component.py | 10 ++++++++++ tests/bindings/python/bt2/test_graph.py | 15 +++++++++++++-- tests/lib/plugin.c | 2 +- tests/lib/test_graph_topo.c | 2 +- tests/lib/test_simple_sink.c | 2 +- tests/lib/test_trace_ir_ref.c | 2 +- 14 files changed, 61 insertions(+), 11 deletions(-) diff --git a/include/babeltrace2/graph/graph.h b/include/babeltrace2/graph/graph.h index 284eb0e8..981fd6a4 100644 --- a/include/babeltrace2/graph/graph.h +++ b/include/babeltrace2/graph/graph.h @@ -117,7 +117,7 @@ typedef bt_graph_simple_sink_component_consume_func_status typedef void (*bt_graph_simple_sink_component_finalize_func)(void *data); -extern bt_graph *bt_graph_create(void); +extern bt_graph *bt_graph_create(uint64_t mip_version); typedef enum bt_graph_add_component_status { BT_GRAPH_ADD_COMPONENT_STATUS_OK = __BT_FUNC_STATUS_OK, diff --git a/include/babeltrace2/graph/self-component.h b/include/babeltrace2/graph/self-component.h index f85a50fc..ba722d98 100644 --- a/include/babeltrace2/graph/self-component.h +++ b/include/babeltrace2/graph/self-component.h @@ -46,6 +46,9 @@ const bt_component *bt_self_component_as_component( return __BT_UPCAST(bt_component, self_component); } +extern +uint64_t bt_self_component_get_graph_mip_version(bt_self_component *self_component); + extern void *bt_self_component_get_data( const bt_self_component *self_component); diff --git a/src/bindings/python/bt2/bt2/component.py b/src/bindings/python/bt2/bt2/component.py index 2d0cd922..a68da615 100644 --- a/src/bindings/python/bt2/bt2/component.py +++ b/src/bindings/python/bt2/bt2/component.py @@ -677,6 +677,11 @@ class _UserComponent(metaclass=_UserComponentType): def addr(self): return int(self._bt_ptr) + @property + def _graph_mip_version(self): + ptr = self._bt_as_self_component_ptr(self._bt_ptr) + return native_bt.self_component_get_graph_mip_version(ptr) + def __init__(self, params=None, obj=None): pass diff --git a/src/bindings/python/bt2/bt2/graph.py b/src/bindings/python/bt2/bt2/graph.py index 3812788f..6c9db7ec 100644 --- a/src/bindings/python/bt2/bt2/graph.py +++ b/src/bindings/python/bt2/bt2/graph.py @@ -70,8 +70,13 @@ class Graph(object._SharedObject): _get_ref = staticmethod(native_bt.graph_get_ref) _put_ref = staticmethod(native_bt.graph_put_ref) - def __init__(self): - ptr = native_bt.graph_create() + def __init__(self, mip_version=0): + utils._check_uint64(mip_version) + + if mip_version > bt2.get_maximal_mip_version(): + raise ValueError('unknown MIP version {}'.format(mip_version)) + + ptr = native_bt.graph_create(mip_version) if ptr is None: raise bt2._MemoryError('cannot create graph object') diff --git a/src/cli/babeltrace2.c b/src/cli/babeltrace2.c index 66743c97..b67acd74 100644 --- a/src/cli/babeltrace2.c +++ b/src/cli/babeltrace2.c @@ -1745,7 +1745,7 @@ int cmd_run_ctx_init(struct cmd_run_ctx *ctx, struct bt_config *cfg) } } - ctx->graph = bt_graph_create(); + ctx->graph = bt_graph_create(0); if (!ctx->graph) { goto error; } diff --git a/src/lib/graph/component.c b/src/lib/graph/component.c index 11144fb4..2aed6932 100644 --- a/src/lib/graph/component.c +++ b/src/lib/graph/component.c @@ -605,6 +605,15 @@ bt_logging_level bt_component_get_logging_level( return component->log_level; } +uint64_t bt_self_component_get_graph_mip_version( + bt_self_component *self_component) +{ + struct bt_component *comp = (void *) self_component; + + BT_ASSERT_PRE_NON_NULL(self_component, "Component"); + return bt_component_borrow_graph(comp)->mip_version; +} + void bt_component_get_ref(const struct bt_component *component) { bt_object_get_ref(component); diff --git a/src/lib/graph/graph.c b/src/lib/graph/graph.c index ebcd9db8..70c42c6a 100644 --- a/src/lib/graph/graph.c +++ b/src/lib/graph/graph.c @@ -262,11 +262,15 @@ void notify_message_graph_is_destroyed(struct bt_message *msg) bt_message_unlink_graph(msg); } -struct bt_graph *bt_graph_create(void) +struct bt_graph *bt_graph_create(uint64_t mip_version) { struct bt_graph *graph; int ret; + BT_ASSERT_PRE(mip_version <= bt_get_maximal_mip_version(), + "Unknown MIP version: mip-version=%" PRIu64 ", " + "max-mip-version=%" PRIu64, + mip_version, bt_get_maximal_mip_version()); BT_LOGI_STR("Creating graph object."); graph = g_new0(struct bt_graph, 1); if (!graph) { @@ -275,6 +279,7 @@ struct bt_graph *bt_graph_create(void) } bt_object_init_shared(&graph->base, destroy_graph); + graph->mip_version = mip_version; graph->connections = g_ptr_array_new_with_free_func( (GDestroyNotify) bt_object_try_spec_release); if (!graph->connections) { diff --git a/src/lib/graph/graph.h b/src/lib/graph/graph.h index 5cda8a1d..72cf121b 100644 --- a/src/lib/graph/graph.h +++ b/src/lib/graph/graph.h @@ -90,6 +90,8 @@ struct bt_graph { /* Queue of pointers (weak references) to sink bt_components. */ GQueue *sinks_to_consume; + uint64_t mip_version; + /* * Array of `struct bt_interrupter *`, each one owned by this. * If any interrupter is set, then this graph is deemed diff --git a/tests/bindings/python/bt2/test_component.py b/tests/bindings/python/bt2/test_component.py index 59a9bc19..65338c19 100644 --- a/tests/bindings/python/bt2/test_component.py +++ b/tests/bindings/python/bt2/test_component.py @@ -50,6 +50,16 @@ class UserComponentTestCase(unittest.TestCase): comp = self._create_comp(MySink, 'yaes', bt2.LoggingLevel.INFO) + def test_graph_mip_version(self): + class MySink(bt2._UserSinkComponent): + def __init__(comp_self, params, obj): + self.assertEqual(comp_self._graph_mip_version, 0) + + def _user_consume(self): + pass + + comp = self._create_comp(MySink, 'yaes', bt2.LoggingLevel.INFO) + def test_class(self): class MySink(bt2._UserSinkComponent): def __init__(comp_self, params, obj): diff --git a/tests/bindings/python/bt2/test_graph.py b/tests/bindings/python/bt2/test_graph.py index 0c9d9279..b719d9ca 100644 --- a/tests/bindings/python/bt2/test_graph.py +++ b/tests/bindings/python/bt2/test_graph.py @@ -51,8 +51,19 @@ class GraphTestCase(unittest.TestCase): def tearDown(self): del self._graph - def test_create_empty(self): - graph = bt2.Graph() + def test_create_default(self): + bt2.Graph() + + def test_create_known_mip_version(self): + bt2.Graph(0) + + def test_create_invalid_mip_version_type(self): + with self.assertRaises(TypeError): + bt2.Graph('') + + def test_create_unknown_mip_version(self): + with self.assertRaisesRegex(ValueError, 'unknown MIP version'): + bt2.Graph(1) def test_add_component_user_cls(self): class MySink(bt2._UserSinkComponent): diff --git a/tests/lib/plugin.c b/tests/lib/plugin.c index 3b5c05fc..a5bbe0c7 100644 --- a/tests/lib/plugin.c +++ b/tests/lib/plugin.c @@ -199,7 +199,7 @@ static void test_sfs(const char *plugin_dir) bt_component_class_sink_get_ref(sink_comp_class); diag("> putting the plugin set object here"); BT_PLUGIN_SET_PUT_REF_AND_RESET(plugin_set); - graph = bt_graph_create(); + graph = bt_graph_create(0); BT_ASSERT(graph); graph_ret = bt_graph_add_sink_component(graph, sink_comp_class, "the-sink", NULL, BT_LOGGING_LEVEL_NONE, &sink_component); diff --git a/tests/lib/test_graph_topo.c b/tests/lib/test_graph_topo.c index 5efffde9..ae2da5aa 100644 --- a/tests/lib/test_graph_topo.c +++ b/tests/lib/test_graph_topo.c @@ -449,7 +449,7 @@ const bt_component_sink *create_sink(bt_graph *graph) static bt_graph *create_graph(void) { - bt_graph *graph = bt_graph_create(); + bt_graph *graph = bt_graph_create(0); int ret; BT_ASSERT(graph); diff --git a/tests/lib/test_simple_sink.c b/tests/lib/test_simple_sink.c index f0599bb8..31e2b6f7 100644 --- a/tests/lib/test_simple_sink.c +++ b/tests/lib/test_simple_sink.c @@ -94,7 +94,7 @@ bt_graph *create_graph_with_source(const bt_port_output **out_port) set_method_status = bt_component_class_source_set_init_method( src_comp_cls, src_init); BT_ASSERT(set_method_status == BT_COMPONENT_CLASS_SET_METHOD_STATUS_OK); - graph = bt_graph_create(); + graph = bt_graph_create(0); BT_ASSERT(graph); add_comp_status = bt_graph_add_source_component(graph, src_comp_cls, "src", NULL, BT_LOGGING_LEVEL_NONE, &src_comp); diff --git a/tests/lib/test_trace_ir_ref.c b/tests/lib/test_trace_ir_ref.c index 7eeef185..33591800 100644 --- a/tests/lib/test_trace_ir_ref.c +++ b/tests/lib/test_trace_ir_ref.c @@ -436,7 +436,7 @@ static void test_example_scenario_in_graph(void) BT_ASSERT(comp_cls); ret = bt_component_class_source_set_init_method(comp_cls, src_init); BT_ASSERT(ret == 0); - graph = bt_graph_create(); + graph = bt_graph_create(0); ret = bt_graph_add_source_component(graph, comp_cls, "src-comp", NULL, BT_LOGGING_LEVEL_NONE, NULL); BT_ASSERT(ret == 0); -- 2.34.1