2 * Copyright 2019 Philippe Proulx <pproulx@efficios.com>
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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
23 #define BT_LOG_TAG "LIB/MIP"
24 #include "lib/logging.h"
26 #include "lib/assert-pre.h"
27 #include "lib/assert-post.h"
31 #include <babeltrace2/graph/mip.h>
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"
41 bool unsigned_integer_range_set_contains(
42 const struct bt_integer_range_set
*range_set
, uint64_t value
)
44 bool contains
= false;
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
);
53 if (value
>= range
->lower
.u
&& value
<= range
->upper
.u
) {
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.
68 * If any component descriptor does not support MIP version 0, then this
69 * function returns `BT_FUNC_STATUS_NO_MATCH`.
72 int validate_operative_mip_version_in_array(GPtrArray
*descriptors
,
73 enum bt_logging_level log_level
)
75 typedef bt_component_class_get_supported_mip_versions_method_status
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
*);
83 int status
= BT_FUNC_STATUS_OK
;
85 struct bt_integer_range_set
*range_set
= NULL
;
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
;
93 switch (descr
->comp_cls
->type
) {
94 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
96 struct bt_component_class_source
*src_cc
= (void *)
99 method
= (method_t
) src_cc
->methods
.get_supported_mip_versions
;
102 case BT_COMPONENT_CLASS_TYPE_FILTER
:
104 struct bt_component_class_filter
*flt_cc
= (void *)
107 method
= (method_t
) flt_cc
->methods
.get_supported_mip_versions
;
110 case BT_COMPONENT_CLASS_TYPE_SINK
:
112 struct bt_component_class_sink
*sink_cc
= (void *)
115 method
= (method_t
) sink_cc
->methods
.get_supported_mip_versions
;
127 range_set
= (void *) bt_integer_range_set_unsigned_create();
129 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
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, "
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
,
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 if (method_status
< 0) {
150 BT_LIB_LOGW_APPEND_CAUSE(
151 "Component class's \"get supported MIP versions\" method failed: "
152 "%![cc-]+C, %![params-]+v, init-method-data=%p, "
154 descr
->comp_cls
, descr
->params
,
155 descr
->init_method_data
,
156 bt_common_logging_level_string(log_level
));
157 status
= (int) method_status
;
161 if (!unsigned_integer_range_set_contains(range_set
, 0)) {
163 * Supported MIP versions do not include 0,
164 * which is the only MIP versions currently
165 * supported by the library itself.
167 status
= BT_FUNC_STATUS_NO_MATCH
;
171 BT_OBJECT_PUT_REF_AND_RESET(range_set
);
175 bt_object_put_ref(range_set
);
180 * The purpose of this function is eventually to find the greatest
181 * common supported MIP version amongst all the component descriptors.
182 * But as of this version of the library, only MIP version 0 is
183 * supported, so it only checks that they all support MIP version 0 and
184 * always sets `*operative_mip_version` to 0.
186 * When any component descriptor does not support MIP version 0, this
187 * function returns `BT_FUNC_STATUS_NO_MATCH`.
189 enum bt_get_greatest_operative_mip_version_status
190 bt_get_greatest_operative_mip_version(
191 const struct bt_component_descriptor_set
*comp_descr_set
,
192 enum bt_logging_level log_level
,
193 uint64_t *operative_mip_version
)
195 int status
= BT_FUNC_STATUS_OK
;
197 BT_ASSERT_PRE_NON_NULL(comp_descr_set
, "Component descriptor set");
198 BT_ASSERT_PRE_NON_NULL(operative_mip_version
,
199 "Operative MIP version (output)");
200 BT_ASSERT_PRE(comp_descr_set
->sources
->len
+
201 comp_descr_set
->filters
->len
+
202 comp_descr_set
->sinks
->len
> 0,
203 "Component descriptor set is empty: addr=%p", comp_descr_set
);
204 status
= validate_operative_mip_version_in_array(
205 comp_descr_set
->sources
, log_level
);
210 status
= validate_operative_mip_version_in_array(
211 comp_descr_set
->filters
, log_level
);
216 status
= validate_operative_mip_version_in_array(
217 comp_descr_set
->sinks
, log_level
);
222 *operative_mip_version
= 0;
228 uint64_t bt_get_maximal_mip_version(void)