From 00c988c958149f894567a1c41d480127425bfb3e Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Sat, 10 Aug 2019 18:40:50 -0400 Subject: [PATCH] lib: add bt_get_{greatest_operative,maximal}_mip_version() functions This patch adds the bt_get_greatest_operative_mip_version() and bt_get_maximal_mip_version() functions. The bt_get_greatest_operative_mip_version() accepts a set of component descriptors and returns, on success, the (greatest) operative message interchange protocol (MIP) version for all those descriptors. bt_get_greatest_operative_mip_version() can fail if any component class's "get supported MIP versions" fails. It can also return `BT_GET_OPERATIVE_MIP_VERSION_STATUS_NO_MATCH` if any component descriptor does not support MIP version 0, the only MIP version currently supported by the the library. bt_get_maximal_mip_version() returns the maximal MIP version supported by the library (currently 0). The purpose of both functions is to be used eventually to create a graph of which all the components and the library itself operate using a specific MIP. You can use bt_get_greatest_operative_mip_version() to find the greatest compatible MIP version to be used by that set of component descriptors, or you can use bt_get_maximal_mip_version() to get the greatest MIP version possible for that library. Signed-off-by: Philippe Proulx Change-Id: If3875b3d76ddca2b4d58a18a829394eadb3bcd39 Reviewed-on: https://review.lttng.org/c/babeltrace/+/1872 Tested-by: jenkins Reviewed-by: Simon Marchi --- include/Makefile.am | 1 + include/babeltrace2/babeltrace.h | 1 + include/babeltrace2/func-status.h | 5 + include/babeltrace2/graph/mip.h | 56 ++++++ src/lib/func-status.h | 1 + src/lib/graph/Makefile.am | 1 + src/lib/graph/component-descriptor-set.h | 4 - src/lib/graph/mip.c | 230 +++++++++++++++++++++++ 8 files changed, 295 insertions(+), 4 deletions(-) create mode 100644 include/babeltrace2/graph/mip.h create mode 100644 src/lib/graph/mip.c diff --git a/include/Makefile.am b/include/Makefile.am index 4f7c406f..1033ec3b 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -110,6 +110,7 @@ babeltrace2graphinclude_HEADERS = \ babeltrace2/graph/message-stream-const.h \ babeltrace2/graph/message-stream-end-const.h \ babeltrace2/graph/message-stream-end.h \ + babeltrace2/graph/mip.h \ babeltrace2/graph/port-const.h \ babeltrace2/graph/port-input-const.h \ babeltrace2/graph/port-output-const.h \ diff --git a/include/babeltrace2/babeltrace.h b/include/babeltrace2/babeltrace.h index 743ca78d..fd0bb7fa 100644 --- a/include/babeltrace2/babeltrace.h +++ b/include/babeltrace2/babeltrace.h @@ -142,6 +142,7 @@ #include #include #include +#include #include #include #include diff --git a/include/babeltrace2/func-status.h b/include/babeltrace2/func-status.h index 75cee213..0f1829af 100644 --- a/include/babeltrace2/func-status.h +++ b/include/babeltrace2/func-status.h @@ -72,6 +72,11 @@ # define __BT_FUNC_STATUS_INTERRUPTED 4 #endif +/* No match found */ +#ifndef __BT_FUNC_STATUS_NO_MATCH +# define __BT_FUNC_STATUS_NO_MATCH 6 +#endif + /* Try operation again later */ #ifndef __BT_FUNC_STATUS_AGAIN # define __BT_FUNC_STATUS_AGAIN 11 diff --git a/include/babeltrace2/graph/mip.h b/include/babeltrace2/graph/mip.h new file mode 100644 index 00000000..f5da87c5 --- /dev/null +++ b/include/babeltrace2/graph/mip.h @@ -0,0 +1,56 @@ +#ifndef BABELTRACE2_GRAPH_MIP_H +#define BABELTRACE2_GRAPH_MIP_H + +/* + * Copyright (c) 2010-2019 EfficiOS Inc. and Linux Foundation + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#ifndef __BT_IN_BABELTRACE_H +# error "Please include instead." +#endif + +#include + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +typedef enum bt_get_greatest_operative_mip_version_status { + BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_OK = __BT_FUNC_STATUS_OK, + BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_ERROR = __BT_FUNC_STATUS_ERROR, + BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_MEMORY_ERROR = __BT_FUNC_STATUS_MEMORY_ERROR, + BT_GET_GREATEST_OPERATIVE_MIP_VERSION_STATUS_NO_MATCH = __BT_FUNC_STATUS_NO_MATCH, +} bt_get_greatest_operative_mip_version_status; + +extern bt_get_greatest_operative_mip_version_status +bt_get_greatest_operative_mip_version( + const bt_component_descriptor_set *comp_descriptor_set, + bt_logging_level log_level, uint64_t *operative_mip_version); + +extern uint64_t bt_get_maximal_mip_version(void); + +#ifdef __cplusplus +} +#endif + +#endif /* BABELTRACE2_GRAPH_MIP_H */ diff --git a/src/lib/func-status.h b/src/lib/func-status.h index d373b3c4..1b9c5cdb 100644 --- a/src/lib/func-status.h +++ b/src/lib/func-status.h @@ -36,6 +36,7 @@ #define BT_FUNC_STATUS_INTERRUPTED __BT_FUNC_STATUS_INTERRUPTED #define BT_FUNC_STATUS_UNKNOWN_OBJECT __BT_FUNC_STATUS_UNKNOWN_OBJECT #define BT_FUNC_STATUS_MEMORY_ERROR __BT_FUNC_STATUS_MEMORY_ERROR +#define BT_FUNC_STATUS_NO_MATCH __BT_FUNC_STATUS_NO_MATCH #define BT_FUNC_STATUS_NOT_FOUND __BT_FUNC_STATUS_NOT_FOUND #define BT_FUNC_STATUS_OK __BT_FUNC_STATUS_OK #define BT_FUNC_STATUS_OVERFLOW_ERROR __BT_FUNC_STATUS_OVERFLOW_ERROR diff --git a/src/lib/graph/Makefile.am b/src/lib/graph/Makefile.am index 9c552768..7a9d63ee 100644 --- a/src/lib/graph/Makefile.am +++ b/src/lib/graph/Makefile.am @@ -25,6 +25,7 @@ libgraph_la_SOURCES = \ interrupter.c \ interrupter.h \ iterator.c \ + mip.c \ port.c \ port.h \ query-executor.c \ diff --git a/src/lib/graph/component-descriptor-set.h b/src/lib/graph/component-descriptor-set.h index e275d77d..4b9b78fd 100644 --- a/src/lib/graph/component-descriptor-set.h +++ b/src/lib/graph/component-descriptor-set.h @@ -38,10 +38,6 @@ #include "connection.h" #include "lib/func-status.h" -struct bt_component_descriptor_source; -struct bt_component_descriptor_filter; -struct bt_component_descriptor_sink; - /* * This structure describes an eventual component instance. */ diff --git a/src/lib/graph/mip.c b/src/lib/graph/mip.c new file mode 100644 index 00000000..887ae157 --- /dev/null +++ b/src/lib/graph/mip.c @@ -0,0 +1,230 @@ +/* + * Copyright 2019 Philippe Proulx + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#define BT_LOG_TAG "LIB/MIP" +#include "lib/logging.h" + +#include "lib/assert-pre.h" +#include "lib/assert-post.h" +#include +#include +#include + +#include "common/assert.h" +#include "compat/compiler.h" +#include "common/common.h" +#include "lib/value.h" +#include "component-descriptor-set.h" +#include "lib/integer-range-set.h" + +static +bool unsigned_integer_range_set_contains( + const struct bt_integer_range_set *range_set, uint64_t value) +{ + bool contains = false; + uint64_t i; + + BT_ASSERT(range_set); + + for (i = 0; i < range_set->ranges->len; i++) { + const struct bt_integer_range *range = + BT_INTEGER_RANGE_SET_RANGE_AT_INDEX(range_set, i); + + if (value >= range->lower.u && value <= range->upper.u) { + contains = true; + goto end; + } + } + +end: + return contains; +} + +/* + * As of this version, this function only validates that all the + * component descriptors in `descriptors` support MIP version 0, which + * is the only version supported by this library. + * + * If any component descriptor does not support MIP version 0, then this + * function returns `BT_FUNC_STATUS_NO_MATCH`. + */ +static +int validate_operative_mip_version_in_array(GPtrArray *descriptors, + enum bt_logging_level log_level) +{ + typedef bt_component_class_get_supported_mip_versions_method_status + (*method_t)( + void * /* component class */, + const struct bt_value *, + void * /* init method data */, + enum bt_logging_level, + struct bt_integer_range_set *); + + int status = BT_FUNC_STATUS_OK; + uint64_t i; + struct bt_integer_range_set *range_set = NULL; + + for (i = 0; i < descriptors->len; i++) { + struct bt_component_descriptor_set_entry *descr = + descriptors->pdata[i]; + method_t method = NULL; + bt_component_class_get_supported_mip_versions_method_status method_status; + + switch (descr->comp_cls->type) { + case BT_COMPONENT_CLASS_TYPE_SOURCE: + { + struct bt_component_class_source *src_cc = (void *) + descr->comp_cls; + + method = (method_t) src_cc->methods.get_supported_mip_versions; + break; + } + case BT_COMPONENT_CLASS_TYPE_FILTER: + { + struct bt_component_class_filter *flt_cc = (void *) + descr->comp_cls; + + method = (method_t) flt_cc->methods.get_supported_mip_versions; + break; + } + case BT_COMPONENT_CLASS_TYPE_SINK: + { + struct bt_component_class_sink *sink_cc = (void *) + descr->comp_cls; + + method = (method_t) sink_cc->methods.get_supported_mip_versions; + break; + } + default: + abort(); + } + + if (!method) { + /* Assume 0 */ + continue; + } + + range_set = (void *) bt_integer_range_set_unsigned_create(); + if (!range_set) { + status = BT_FUNC_STATUS_MEMORY_ERROR; + goto end; + } + + BT_ASSERT(descr->params); + BT_LIB_LOGD("Calling user's \"get supported MIP versions\" method: " + "%![cc-]+C, %![params-]+v, init-method-data=%p, " + "log-level=%s", + descr->comp_cls, descr->params, + descr->init_method_data, + bt_common_logging_level_string(log_level)); + method_status = method(descr->comp_cls, descr->params, + descr->init_method_data, log_level, + range_set); + BT_LIB_LOGD("User method returned: status=%s", + bt_common_func_status_string(method_status)); + BT_ASSERT_POST(method_status != BT_FUNC_STATUS_OK || + range_set->ranges->len > 0, + "User method returned `BT_FUNC_STATUS_OK` without " + "adding a range to the supported MIP version range set."); + if (method_status < 0) { + BT_LIB_LOGW_APPEND_CAUSE( + "Component class's \"get supported MIP versions\" method failed: " + "%![cc-]+C, %![params-]+v, init-method-data=%p, " + "log-level=%s", + descr->comp_cls, descr->params, + descr->init_method_data, + bt_common_logging_level_string(log_level)); + status = (int) method_status; + goto end; + } + + if (!unsigned_integer_range_set_contains(range_set, 0)) { + /* + * Supported MIP versions do not include 0, + * which is the only MIP versions currently + * supported by the library itself. + */ + status = BT_FUNC_STATUS_NO_MATCH; + goto end; + } + + BT_OBJECT_PUT_REF_AND_RESET(range_set); + } + +end: + bt_object_put_ref(range_set); + return status; +} + +/* + * The purpose of this function is eventually to find the greatest + * common supported MIP version amongst all the component descriptors. + * But as of this version of the library, only MIP version 0 is + * supported, so it only checks that they all support MIP version 0 and + * always sets `*operative_mip_version` to 0. + * + * When any component descriptor does not support MIP version 0, this + * function returns `BT_FUNC_STATUS_NO_MATCH`. + */ +enum bt_get_greatest_operative_mip_version_status +bt_get_greatest_operative_mip_version( + const struct bt_component_descriptor_set *comp_descr_set, + enum bt_logging_level log_level, + uint64_t *operative_mip_version) +{ + int status = BT_FUNC_STATUS_OK; + + BT_ASSERT_PRE_NON_NULL(comp_descr_set, "Component descriptor set"); + BT_ASSERT_PRE_NON_NULL(operative_mip_version, + "Operative MIP version (output)"); + BT_ASSERT_PRE(comp_descr_set->sources->len + + comp_descr_set->filters->len + + comp_descr_set->sinks->len > 0, + "Component descriptor set is empty: addr=%p", comp_descr_set); + status = validate_operative_mip_version_in_array( + comp_descr_set->sources, log_level); + if (status) { + goto end; + } + + status = validate_operative_mip_version_in_array( + comp_descr_set->filters, log_level); + if (status) { + goto end; + } + + status = validate_operative_mip_version_in_array( + comp_descr_set->sinks, log_level); + if (status) { + goto end; + } + + *operative_mip_version = 0; + +end: + return status; +} + +uint64_t bt_get_maximal_mip_version(void) +{ + return 0; +} -- 2.34.1