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