lib: add bt_get_{greatest_operative,maximal}_mip_version() functions
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Sat, 10 Aug 2019 22:40:50 +0000 (18:40 -0400)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Tue, 13 Aug 2019 00:28:02 +0000 (20:28 -0400)
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 <eeppeliteloop@gmail.com>
Change-Id: If3875b3d76ddca2b4d58a18a829394eadb3bcd39
Reviewed-on: https://review.lttng.org/c/babeltrace/+/1872
Tested-by: jenkins <jenkins@lttng.org>
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
include/Makefile.am
include/babeltrace2/babeltrace.h
include/babeltrace2/func-status.h
include/babeltrace2/graph/mip.h [new file with mode: 0644]
src/lib/func-status.h
src/lib/graph/Makefile.am
src/lib/graph/component-descriptor-set.h
src/lib/graph/mip.c [new file with mode: 0644]

index 4f7c406f82cddb1ed7f2ee2c802feaaf15d234d4..1033ec3b4fbe0a8aaedbec5680cf78b3c9650663 100644 (file)
@@ -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 \
index 743ca78d146c43a9b91a367390f1749470575518..fd0bb7fa68ae0a28cde561d586d157ae6f9279cb 100644 (file)
 #include <babeltrace2/graph/graph.h>
 #include <babeltrace2/graph/interrupter-const.h>
 #include <babeltrace2/graph/interrupter.h>
+#include <babeltrace2/graph/mip.h>
 #include <babeltrace2/graph/port-const.h>
 #include <babeltrace2/graph/port-input-const.h>
 #include <babeltrace2/graph/port-output-const.h>
index 75cee213e689fe83ed719bb30a204558974a7e16..0f1829af8e7e17fcba6d1c1755ad0665371498dd 100644 (file)
 # 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 (file)
index 0000000..f5da87c
--- /dev/null
@@ -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 <babeltrace2/babeltrace.h> instead."
+#endif
+
+#include <stdint.h>
+
+#include <babeltrace2/types.h>
+
+#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 */
index d373b3c493fb84787656e29c1e6b3db0c9412dbb..1b9c5cdb19a564a0baffd2c651358cfd6d96dbb1 100644 (file)
@@ -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
index 9c5527682f471c39c786f2797e27c30e68769766..7a9d63ee15b810a3b48a3521b8aac1c36877a53a 100644 (file)
@@ -25,6 +25,7 @@ libgraph_la_SOURCES = \
        interrupter.c \
        interrupter.h \
        iterator.c \
+       mip.c \
        port.c \
        port.h \
        query-executor.c \
index e275d77d607b6c4780f72a7615a54d901477ecd4..4b9b78fd540cc4b1e4471aeb486b981d4de3249a 100644 (file)
 #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 (file)
index 0000000..887ae15
--- /dev/null
@@ -0,0 +1,230 @@
+/*
+ * Copyright 2019 Philippe Proulx <pproulx@efficios.com>
+ *
+ * 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 <unistd.h>
+#include <glib.h>
+#include <babeltrace2/graph/mip.h>
+
+#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;
+}
This page took 0.031053 seconds and 4 git commands to generate.