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