| 1 | /* |
| 2 | * SPDX-License-Identifier: MIT |
| 3 | * |
| 4 | * Copyright 2019 Philippe Proulx <pproulx@efficios.com> |
| 5 | */ |
| 6 | |
| 7 | #define BT_LOG_TAG "LIB/MIP" |
| 8 | #include "lib/logging.h" |
| 9 | |
| 10 | #include "lib/assert-pre.h" |
| 11 | #include "lib/assert-post.h" |
| 12 | #include <stdbool.h> |
| 13 | #include <unistd.h> |
| 14 | #include <glib.h> |
| 15 | #include <babeltrace2/graph/graph.h> |
| 16 | |
| 17 | #include "common/assert.h" |
| 18 | #include "compat/compiler.h" |
| 19 | #include "common/common.h" |
| 20 | #include "lib/value.h" |
| 21 | #include "component-descriptor-set.h" |
| 22 | #include "lib/integer-range-set.h" |
| 23 | |
| 24 | static |
| 25 | bool unsigned_integer_range_set_contains( |
| 26 | const struct bt_integer_range_set *range_set, uint64_t value) |
| 27 | { |
| 28 | bool contains = false; |
| 29 | uint64_t i; |
| 30 | |
| 31 | BT_ASSERT(range_set); |
| 32 | |
| 33 | for (i = 0; i < range_set->ranges->len; i++) { |
| 34 | const struct bt_integer_range *range = |
| 35 | BT_INTEGER_RANGE_SET_RANGE_AT_INDEX(range_set, i); |
| 36 | |
| 37 | if (value >= range->lower.u && value <= range->upper.u) { |
| 38 | contains = true; |
| 39 | goto end; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | end: |
| 44 | return contains; |
| 45 | } |
| 46 | |
| 47 | /* |
| 48 | * As of this version, this function only validates that all the |
| 49 | * component descriptors in `descriptors` support MIP version 0, which |
| 50 | * is the only version supported by this library. |
| 51 | * |
| 52 | * If any component descriptor does not support MIP version 0, then this |
| 53 | * function returns `BT_FUNC_STATUS_NO_MATCH`. |
| 54 | */ |
| 55 | static |
| 56 | int validate_operative_mip_version_in_array(GPtrArray *descriptors, |
| 57 | enum bt_logging_level log_level) |
| 58 | { |
| 59 | typedef bt_component_class_get_supported_mip_versions_method_status |
| 60 | (*method_t)( |
| 61 | void * /* component class */, |
| 62 | const struct bt_value *, |
| 63 | void * /* init method data */, |
| 64 | enum bt_logging_level, |
| 65 | struct bt_integer_range_set *); |
| 66 | |
| 67 | int status = BT_FUNC_STATUS_OK; |
| 68 | uint64_t i; |
| 69 | struct bt_integer_range_set *range_set = NULL; |
| 70 | |
| 71 | for (i = 0; i < descriptors->len; i++) { |
| 72 | struct bt_component_descriptor_set_entry *descr = |
| 73 | descriptors->pdata[i]; |
| 74 | method_t method = NULL; |
| 75 | bt_component_class_get_supported_mip_versions_method_status method_status; |
| 76 | |
| 77 | switch (descr->comp_cls->type) { |
| 78 | case BT_COMPONENT_CLASS_TYPE_SOURCE: |
| 79 | { |
| 80 | struct bt_component_class_source *src_cc = (void *) |
| 81 | descr->comp_cls; |
| 82 | |
| 83 | method = (method_t) src_cc->methods.get_supported_mip_versions; |
| 84 | break; |
| 85 | } |
| 86 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
| 87 | { |
| 88 | struct bt_component_class_filter *flt_cc = (void *) |
| 89 | descr->comp_cls; |
| 90 | |
| 91 | method = (method_t) flt_cc->methods.get_supported_mip_versions; |
| 92 | break; |
| 93 | } |
| 94 | case BT_COMPONENT_CLASS_TYPE_SINK: |
| 95 | { |
| 96 | struct bt_component_class_sink *sink_cc = (void *) |
| 97 | descr->comp_cls; |
| 98 | |
| 99 | method = (method_t) sink_cc->methods.get_supported_mip_versions; |
| 100 | break; |
| 101 | } |
| 102 | default: |
| 103 | bt_common_abort(); |
| 104 | } |
| 105 | |
| 106 | if (!method) { |
| 107 | /* Assume 0 */ |
| 108 | continue; |
| 109 | } |
| 110 | |
| 111 | range_set = (void *) bt_integer_range_set_unsigned_create(); |
| 112 | if (!range_set) { |
| 113 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
| 114 | goto end; |
| 115 | } |
| 116 | |
| 117 | BT_ASSERT(descr->params); |
| 118 | BT_LIB_LOGD("Calling user's \"get supported MIP versions\" method: " |
| 119 | "%![cc-]+C, %![params-]+v, init-method-data=%p, " |
| 120 | "log-level=%s", |
| 121 | descr->comp_cls, descr->params, |
| 122 | descr->init_method_data, |
| 123 | bt_common_logging_level_string(log_level)); |
| 124 | method_status = method(descr->comp_cls, descr->params, |
| 125 | descr->init_method_data, log_level, |
| 126 | range_set); |
| 127 | BT_LIB_LOGD("User method returned: status=%s", |
| 128 | bt_common_func_status_string(method_status)); |
| 129 | BT_ASSERT_POST(method_status != BT_FUNC_STATUS_OK || |
| 130 | range_set->ranges->len > 0, |
| 131 | "User method returned `BT_FUNC_STATUS_OK` without " |
| 132 | "adding a range to the supported MIP version range set."); |
| 133 | BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(method_status); |
| 134 | if (method_status < 0) { |
| 135 | BT_LIB_LOGW_APPEND_CAUSE( |
| 136 | "Component class's \"get supported MIP versions\" method failed: " |
| 137 | "%![cc-]+C, %![params-]+v, init-method-data=%p, " |
| 138 | "log-level=%s", |
| 139 | descr->comp_cls, descr->params, |
| 140 | descr->init_method_data, |
| 141 | bt_common_logging_level_string(log_level)); |
| 142 | status = (int) method_status; |
| 143 | goto end; |
| 144 | } |
| 145 | |
| 146 | if (!unsigned_integer_range_set_contains(range_set, 0)) { |
| 147 | /* |
| 148 | * Supported MIP versions do not include 0, |
| 149 | * which is the only MIP versions currently |
| 150 | * supported by the library itself. |
| 151 | */ |
| 152 | status = BT_FUNC_STATUS_NO_MATCH; |
| 153 | goto end; |
| 154 | } |
| 155 | |
| 156 | BT_OBJECT_PUT_REF_AND_RESET(range_set); |
| 157 | } |
| 158 | |
| 159 | end: |
| 160 | bt_object_put_ref(range_set); |
| 161 | return status; |
| 162 | } |
| 163 | |
| 164 | /* |
| 165 | * The purpose of this function is eventually to find the greatest |
| 166 | * common supported MIP version amongst all the component descriptors. |
| 167 | * But as of this version of the library, only MIP version 0 is |
| 168 | * supported, so it only checks that they all support MIP version 0 and |
| 169 | * always sets `*operative_mip_version` to 0. |
| 170 | * |
| 171 | * When any component descriptor does not support MIP version 0, this |
| 172 | * function returns `BT_FUNC_STATUS_NO_MATCH`. |
| 173 | */ |
| 174 | enum bt_get_greatest_operative_mip_version_status |
| 175 | bt_get_greatest_operative_mip_version( |
| 176 | const struct bt_component_descriptor_set *comp_descr_set, |
| 177 | enum bt_logging_level log_level, |
| 178 | uint64_t *operative_mip_version) |
| 179 | { |
| 180 | int status = BT_FUNC_STATUS_OK; |
| 181 | |
| 182 | BT_ASSERT_PRE_NO_ERROR(); |
| 183 | BT_ASSERT_PRE_NON_NULL(comp_descr_set, "Component descriptor set"); |
| 184 | BT_ASSERT_PRE_NON_NULL(operative_mip_version, |
| 185 | "Operative MIP version (output)"); |
| 186 | BT_ASSERT_PRE(comp_descr_set->sources->len + |
| 187 | comp_descr_set->filters->len + |
| 188 | comp_descr_set->sinks->len > 0, |
| 189 | "Component descriptor set is empty: addr=%p", comp_descr_set); |
| 190 | status = validate_operative_mip_version_in_array( |
| 191 | comp_descr_set->sources, log_level); |
| 192 | if (status) { |
| 193 | goto end; |
| 194 | } |
| 195 | |
| 196 | status = validate_operative_mip_version_in_array( |
| 197 | comp_descr_set->filters, log_level); |
| 198 | if (status) { |
| 199 | goto end; |
| 200 | } |
| 201 | |
| 202 | status = validate_operative_mip_version_in_array( |
| 203 | comp_descr_set->sinks, log_level); |
| 204 | if (status) { |
| 205 | goto end; |
| 206 | } |
| 207 | |
| 208 | *operative_mip_version = 0; |
| 209 | |
| 210 | end: |
| 211 | return status; |
| 212 | } |
| 213 | |
| 214 | uint64_t bt_get_maximal_mip_version(void) |
| 215 | { |
| 216 | return 0; |
| 217 | } |