lib: add bt_get_{greatest_operative,maximal}_mip_version() functions
[babeltrace.git] / src / lib / graph / mip.c
1 /*
2 * Copyright 2019 Philippe Proulx <pproulx@efficios.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 #define BT_LOG_TAG "LIB/MIP"
24 #include "lib/logging.h"
25
26 #include "lib/assert-pre.h"
27 #include "lib/assert-post.h"
28 #include <unistd.h>
29 #include <glib.h>
30 #include <babeltrace2/graph/mip.h>
31
32 #include "common/assert.h"
33 #include "compat/compiler.h"
34 #include "common/common.h"
35 #include "lib/value.h"
36 #include "component-descriptor-set.h"
37 #include "lib/integer-range-set.h"
38
39 static
40 bool unsigned_integer_range_set_contains(
41 const struct bt_integer_range_set *range_set, uint64_t value)
42 {
43 bool contains = false;
44 uint64_t i;
45
46 BT_ASSERT(range_set);
47
48 for (i = 0; i < range_set->ranges->len; i++) {
49 const struct bt_integer_range *range =
50 BT_INTEGER_RANGE_SET_RANGE_AT_INDEX(range_set, i);
51
52 if (value >= range->lower.u && value <= range->upper.u) {
53 contains = true;
54 goto end;
55 }
56 }
57
58 end:
59 return contains;
60 }
61
62 /*
63 * As of this version, this function only validates that all the
64 * component descriptors in `descriptors` support MIP version 0, which
65 * is the only version supported by this library.
66 *
67 * If any component descriptor does not support MIP version 0, then this
68 * function returns `BT_FUNC_STATUS_NO_MATCH`.
69 */
70 static
71 int validate_operative_mip_version_in_array(GPtrArray *descriptors,
72 enum bt_logging_level log_level)
73 {
74 typedef bt_component_class_get_supported_mip_versions_method_status
75 (*method_t)(
76 void * /* component class */,
77 const struct bt_value *,
78 void * /* init method data */,
79 enum bt_logging_level,
80 struct bt_integer_range_set *);
81
82 int status = BT_FUNC_STATUS_OK;
83 uint64_t i;
84 struct bt_integer_range_set *range_set = NULL;
85
86 for (i = 0; i < descriptors->len; i++) {
87 struct bt_component_descriptor_set_entry *descr =
88 descriptors->pdata[i];
89 method_t method = NULL;
90 bt_component_class_get_supported_mip_versions_method_status method_status;
91
92 switch (descr->comp_cls->type) {
93 case BT_COMPONENT_CLASS_TYPE_SOURCE:
94 {
95 struct bt_component_class_source *src_cc = (void *)
96 descr->comp_cls;
97
98 method = (method_t) src_cc->methods.get_supported_mip_versions;
99 break;
100 }
101 case BT_COMPONENT_CLASS_TYPE_FILTER:
102 {
103 struct bt_component_class_filter *flt_cc = (void *)
104 descr->comp_cls;
105
106 method = (method_t) flt_cc->methods.get_supported_mip_versions;
107 break;
108 }
109 case BT_COMPONENT_CLASS_TYPE_SINK:
110 {
111 struct bt_component_class_sink *sink_cc = (void *)
112 descr->comp_cls;
113
114 method = (method_t) sink_cc->methods.get_supported_mip_versions;
115 break;
116 }
117 default:
118 abort();
119 }
120
121 if (!method) {
122 /* Assume 0 */
123 continue;
124 }
125
126 range_set = (void *) bt_integer_range_set_unsigned_create();
127 if (!range_set) {
128 status = BT_FUNC_STATUS_MEMORY_ERROR;
129 goto end;
130 }
131
132 BT_ASSERT(descr->params);
133 BT_LIB_LOGD("Calling user's \"get supported MIP versions\" method: "
134 "%![cc-]+C, %![params-]+v, init-method-data=%p, "
135 "log-level=%s",
136 descr->comp_cls, descr->params,
137 descr->init_method_data,
138 bt_common_logging_level_string(log_level));
139 method_status = method(descr->comp_cls, descr->params,
140 descr->init_method_data, log_level,
141 range_set);
142 BT_LIB_LOGD("User method returned: status=%s",
143 bt_common_func_status_string(method_status));
144 BT_ASSERT_POST(method_status != BT_FUNC_STATUS_OK ||
145 range_set->ranges->len > 0,
146 "User method returned `BT_FUNC_STATUS_OK` without "
147 "adding a range to the supported MIP version range set.");
148 if (method_status < 0) {
149 BT_LIB_LOGW_APPEND_CAUSE(
150 "Component class's \"get supported MIP versions\" method failed: "
151 "%![cc-]+C, %![params-]+v, init-method-data=%p, "
152 "log-level=%s",
153 descr->comp_cls, descr->params,
154 descr->init_method_data,
155 bt_common_logging_level_string(log_level));
156 status = (int) method_status;
157 goto end;
158 }
159
160 if (!unsigned_integer_range_set_contains(range_set, 0)) {
161 /*
162 * Supported MIP versions do not include 0,
163 * which is the only MIP versions currently
164 * supported by the library itself.
165 */
166 status = BT_FUNC_STATUS_NO_MATCH;
167 goto end;
168 }
169
170 BT_OBJECT_PUT_REF_AND_RESET(range_set);
171 }
172
173 end:
174 bt_object_put_ref(range_set);
175 return status;
176 }
177
178 /*
179 * The purpose of this function is eventually to find the greatest
180 * common supported MIP version amongst all the component descriptors.
181 * But as of this version of the library, only MIP version 0 is
182 * supported, so it only checks that they all support MIP version 0 and
183 * always sets `*operative_mip_version` to 0.
184 *
185 * When any component descriptor does not support MIP version 0, this
186 * function returns `BT_FUNC_STATUS_NO_MATCH`.
187 */
188 enum bt_get_greatest_operative_mip_version_status
189 bt_get_greatest_operative_mip_version(
190 const struct bt_component_descriptor_set *comp_descr_set,
191 enum bt_logging_level log_level,
192 uint64_t *operative_mip_version)
193 {
194 int status = BT_FUNC_STATUS_OK;
195
196 BT_ASSERT_PRE_NON_NULL(comp_descr_set, "Component descriptor set");
197 BT_ASSERT_PRE_NON_NULL(operative_mip_version,
198 "Operative MIP version (output)");
199 BT_ASSERT_PRE(comp_descr_set->sources->len +
200 comp_descr_set->filters->len +
201 comp_descr_set->sinks->len > 0,
202 "Component descriptor set is empty: addr=%p", comp_descr_set);
203 status = validate_operative_mip_version_in_array(
204 comp_descr_set->sources, log_level);
205 if (status) {
206 goto end;
207 }
208
209 status = validate_operative_mip_version_in_array(
210 comp_descr_set->filters, log_level);
211 if (status) {
212 goto end;
213 }
214
215 status = validate_operative_mip_version_in_array(
216 comp_descr_set->sinks, log_level);
217 if (status) {
218 goto end;
219 }
220
221 *operative_mip_version = 0;
222
223 end:
224 return status;
225 }
226
227 uint64_t bt_get_maximal_mip_version(void)
228 {
229 return 0;
230 }
This page took 0.034748 seconds and 5 git commands to generate.