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"
30 #include <babeltrace2/graph/mip.h>
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"
40 bool unsigned_integer_range_set_contains(
41 const struct bt_integer_range_set
*range_set
, uint64_t value
)
43 bool contains
= false;
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
);
52 if (value
>= range
->lower
.u
&& value
<= range
->upper
.u
) {
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.
67 * If any component descriptor does not support MIP version 0, then this
68 * function returns `BT_FUNC_STATUS_NO_MATCH`.
71 int validate_operative_mip_version_in_array(GPtrArray
*descriptors
,
72 enum bt_logging_level log_level
)
74 typedef bt_component_class_get_supported_mip_versions_method_status
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
*);
82 int status
= BT_FUNC_STATUS_OK
;
84 struct bt_integer_range_set
*range_set
= NULL
;
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
;
92 switch (descr
->comp_cls
->type
) {
93 case BT_COMPONENT_CLASS_TYPE_SOURCE
:
95 struct bt_component_class_source
*src_cc
= (void *)
98 method
= (method_t
) src_cc
->methods
.get_supported_mip_versions
;
101 case BT_COMPONENT_CLASS_TYPE_FILTER
:
103 struct bt_component_class_filter
*flt_cc
= (void *)
106 method
= (method_t
) flt_cc
->methods
.get_supported_mip_versions
;
109 case BT_COMPONENT_CLASS_TYPE_SINK
:
111 struct bt_component_class_sink
*sink_cc
= (void *)
114 method
= (method_t
) sink_cc
->methods
.get_supported_mip_versions
;
126 range_set
= (void *) bt_integer_range_set_unsigned_create();
128 status
= BT_FUNC_STATUS_MEMORY_ERROR
;
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, "
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
,
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, "
153 descr
->comp_cls
, descr
->params
,
154 descr
->init_method_data
,
155 bt_common_logging_level_string(log_level
));
156 status
= (int) method_status
;
160 if (!unsigned_integer_range_set_contains(range_set
, 0)) {
162 * Supported MIP versions do not include 0,
163 * which is the only MIP versions currently
164 * supported by the library itself.
166 status
= BT_FUNC_STATUS_NO_MATCH
;
170 BT_OBJECT_PUT_REF_AND_RESET(range_set
);
174 bt_object_put_ref(range_set
);
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.
185 * When any component descriptor does not support MIP version 0, this
186 * function returns `BT_FUNC_STATUS_NO_MATCH`.
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
)
194 int status
= BT_FUNC_STATUS_OK
;
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
);
209 status
= validate_operative_mip_version_in_array(
210 comp_descr_set
->filters
, log_level
);
215 status
= validate_operative_mip_version_in_array(
216 comp_descr_set
->sinks
, log_level
);
221 *operative_mip_version
= 0;
227 uint64_t bt_get_maximal_mip_version(void)