Commit | Line | Data |
---|---|---|
958f7d11 PP |
1 | /* |
2 | * Copyright 2017 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 | ||
fed72692 PP |
23 | #define BT_LOG_TAG "PLUGIN-UTILS-MUXER-FLT" |
24 | #include "logging.h" | |
25 | ||
958f7d11 | 26 | #include <babeltrace/babeltrace-internal.h> |
282c8cd0 | 27 | #include <babeltrace/compat/uuid-internal.h> |
9d408fca | 28 | #include <babeltrace/babeltrace.h> |
a1002510 | 29 | #include <babeltrace/value-internal.h> |
fed72692 | 30 | #include <babeltrace/graph/component-internal.h> |
a940e807 | 31 | #include <babeltrace/graph/message-iterator-internal.h> |
fed72692 | 32 | #include <babeltrace/graph/connection-internal.h> |
958f7d11 PP |
33 | #include <plugins-common.h> |
34 | #include <glib.h> | |
c55a9f58 | 35 | #include <stdbool.h> |
fed72692 | 36 | #include <inttypes.h> |
1d2f35fd | 37 | #include <babeltrace/assert-internal.h> |
310fc789 | 38 | #include <babeltrace/common-internal.h> |
0fbb9a9f | 39 | #include <stdlib.h> |
282c8cd0 | 40 | #include <string.h> |
958f7d11 | 41 | |
aa3ccb9a FD |
42 | #include "muxer.h" |
43 | ||
c3acd5f3 | 44 | #define ASSUME_ABSOLUTE_CLOCK_CLASSES_PARAM_NAME "assume-absolute-clock-classes" |
65ee897d | 45 | |
958f7d11 | 46 | struct muxer_comp { |
958f7d11 | 47 | /* Weak ref */ |
b727a996 | 48 | bt_self_component_filter *self_comp; |
110ebe2b | 49 | |
958f7d11 PP |
50 | unsigned int next_port_num; |
51 | size_t available_input_ports; | |
a940e807 | 52 | bool initializing_muxer_msg_iter; |
282c8cd0 | 53 | bool assume_absolute_clock_classes; |
958f7d11 PP |
54 | }; |
55 | ||
a940e807 | 56 | struct muxer_upstream_msg_iter { |
ab11110e | 57 | /* Owned by this, NULL if ended */ |
a940e807 | 58 | bt_self_component_port_input_message_iterator *msg_iter; |
958f7d11 | 59 | |
a940e807 PP |
60 | /* Contains `const bt_message *`, owned by this */ |
61 | GQueue *msgs; | |
958f7d11 PP |
62 | }; |
63 | ||
a940e807 PP |
64 | enum muxer_msg_iter_clock_class_expectation { |
65 | MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ANY = 0, | |
66 | MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ABSOLUTE, | |
67 | MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID, | |
68 | MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_NO_UUID, | |
282c8cd0 PP |
69 | }; |
70 | ||
a940e807 | 71 | struct muxer_msg_iter { |
ab11110e | 72 | /* |
a940e807 | 73 | * Array of struct muxer_upstream_msg_iter * (owned by this). |
ab11110e PP |
74 | * |
75 | * NOTE: This array is searched in linearly to find the youngest | |
a940e807 | 76 | * current message. Keep this until benchmarks confirm that |
ab11110e PP |
77 | * another data structure is faster than this for our typical |
78 | * use cases. | |
79 | */ | |
1dd153f5 PP |
80 | GPtrArray *active_muxer_upstream_msg_iters; |
81 | ||
82 | /* | |
83 | * Array of struct muxer_upstream_msg_iter * (owned by this). | |
84 | * | |
85 | * We move ended message iterators from | |
86 | * `active_muxer_upstream_msg_iters` to this array so as to be | |
87 | * able to restore them when seeking. | |
88 | */ | |
89 | GPtrArray *ended_muxer_upstream_msg_iters; | |
958f7d11 | 90 | |
a940e807 | 91 | /* Last time returned in a message */ |
958f7d11 | 92 | int64_t last_returned_ts_ns; |
282c8cd0 PP |
93 | |
94 | /* Clock class expectation state */ | |
a940e807 | 95 | enum muxer_msg_iter_clock_class_expectation clock_class_expectation; |
282c8cd0 PP |
96 | |
97 | /* | |
98 | * Expected clock class UUID, only valid when | |
99 | * clock_class_expectation is | |
a940e807 | 100 | * MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID. |
282c8cd0 PP |
101 | */ |
102 | unsigned char expected_clock_class_uuid[BABELTRACE_UUID_LEN]; | |
958f7d11 PP |
103 | }; |
104 | ||
1dd153f5 PP |
105 | static |
106 | void empty_message_queue(struct muxer_upstream_msg_iter *upstream_msg_iter) | |
107 | { | |
108 | const bt_message *msg; | |
109 | ||
110 | while ((msg = g_queue_pop_head(upstream_msg_iter->msgs))) { | |
111 | bt_message_put_ref(msg); | |
112 | } | |
113 | } | |
114 | ||
ab11110e | 115 | static |
a940e807 PP |
116 | void destroy_muxer_upstream_msg_iter( |
117 | struct muxer_upstream_msg_iter *muxer_upstream_msg_iter) | |
ab11110e | 118 | { |
a940e807 | 119 | if (!muxer_upstream_msg_iter) { |
ab11110e PP |
120 | return; |
121 | } | |
122 | ||
a940e807 PP |
123 | BT_LOGD("Destroying muxer's upstream message iterator wrapper: " |
124 | "addr=%p, msg-iter-addr=%p, queue-len=%u", | |
125 | muxer_upstream_msg_iter, | |
126 | muxer_upstream_msg_iter->msg_iter, | |
127 | muxer_upstream_msg_iter->msgs->length); | |
1dd153f5 PP |
128 | bt_self_component_port_input_message_iterator_put_ref( |
129 | muxer_upstream_msg_iter->msg_iter); | |
0d6f8bf6 | 130 | |
a940e807 | 131 | if (muxer_upstream_msg_iter->msgs) { |
1dd153f5 | 132 | empty_message_queue(muxer_upstream_msg_iter); |
a940e807 | 133 | g_queue_free(muxer_upstream_msg_iter->msgs); |
0d6f8bf6 PP |
134 | } |
135 | ||
a940e807 | 136 | g_free(muxer_upstream_msg_iter); |
ab11110e PP |
137 | } |
138 | ||
958f7d11 | 139 | static |
a940e807 PP |
140 | struct muxer_upstream_msg_iter *muxer_msg_iter_add_upstream_msg_iter( |
141 | struct muxer_msg_iter *muxer_msg_iter, | |
142 | bt_self_component_port_input_message_iterator *self_msg_iter) | |
958f7d11 | 143 | { |
a940e807 PP |
144 | struct muxer_upstream_msg_iter *muxer_upstream_msg_iter = |
145 | g_new0(struct muxer_upstream_msg_iter, 1); | |
958f7d11 | 146 | |
a940e807 PP |
147 | if (!muxer_upstream_msg_iter) { |
148 | BT_LOGE_STR("Failed to allocate one muxer's upstream message iterator wrapper."); | |
958f7d11 PP |
149 | goto end; |
150 | } | |
151 | ||
a940e807 PP |
152 | muxer_upstream_msg_iter->msg_iter = self_msg_iter; |
153 | bt_self_component_port_input_message_iterator_get_ref(muxer_upstream_msg_iter->msg_iter); | |
154 | muxer_upstream_msg_iter->msgs = g_queue_new(); | |
155 | if (!muxer_upstream_msg_iter->msgs) { | |
0d6f8bf6 | 156 | BT_LOGE_STR("Failed to allocate a GQueue."); |
0d6f8bf6 PP |
157 | goto end; |
158 | } | |
159 | ||
1dd153f5 | 160 | g_ptr_array_add(muxer_msg_iter->active_muxer_upstream_msg_iters, |
a940e807 PP |
161 | muxer_upstream_msg_iter); |
162 | BT_LOGD("Added muxer's upstream message iterator wrapper: " | |
163 | "addr=%p, muxer-msg-iter-addr=%p, msg-iter-addr=%p", | |
164 | muxer_upstream_msg_iter, muxer_msg_iter, | |
165 | self_msg_iter); | |
958f7d11 PP |
166 | |
167 | end: | |
a940e807 | 168 | return muxer_upstream_msg_iter; |
958f7d11 PP |
169 | } |
170 | ||
958f7d11 | 171 | static |
c67bbe08 | 172 | bt_self_component_status add_available_input_port( |
b727a996 | 173 | bt_self_component_filter *self_comp) |
958f7d11 | 174 | { |
110ebe2b | 175 | struct muxer_comp *muxer_comp = bt_self_component_get_data( |
5bb6a6f1 | 176 | bt_self_component_filter_as_self_component(self_comp)); |
43f077ae | 177 | bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK; |
958f7d11 | 178 | GString *port_name = NULL; |
958f7d11 | 179 | |
1d2f35fd | 180 | BT_ASSERT(muxer_comp); |
958f7d11 PP |
181 | port_name = g_string_new("in"); |
182 | if (!port_name) { | |
fed72692 | 183 | BT_LOGE_STR("Failed to allocate a GString."); |
110ebe2b | 184 | status = BT_SELF_COMPONENT_STATUS_NOMEM; |
958f7d11 PP |
185 | goto end; |
186 | } | |
187 | ||
188 | g_string_append_printf(port_name, "%u", muxer_comp->next_port_num); | |
110ebe2b PP |
189 | status = bt_self_component_filter_add_input_port( |
190 | self_comp, port_name->str, NULL, NULL); | |
191 | if (status != BT_SELF_COMPONENT_STATUS_OK) { | |
fed72692 PP |
192 | BT_LOGE("Cannot add input port to muxer component: " |
193 | "port-name=\"%s\", comp-addr=%p, status=%s", | |
110ebe2b PP |
194 | port_name->str, self_comp, |
195 | bt_self_component_status_string(status)); | |
958f7d11 PP |
196 | goto end; |
197 | } | |
198 | ||
199 | muxer_comp->available_input_ports++; | |
200 | muxer_comp->next_port_num++; | |
fed72692 PP |
201 | BT_LOGD("Added one input port to muxer component: " |
202 | "port-name=\"%s\", comp-addr=%p", | |
110ebe2b | 203 | port_name->str, self_comp); |
c67bbe08 | 204 | |
958f7d11 PP |
205 | end: |
206 | if (port_name) { | |
207 | g_string_free(port_name, TRUE); | |
208 | } | |
209 | ||
147337a3 | 210 | return status; |
958f7d11 PP |
211 | } |
212 | ||
958f7d11 | 213 | static |
43f077ae | 214 | bt_self_component_status create_output_port( |
b727a996 | 215 | bt_self_component_filter *self_comp) |
958f7d11 | 216 | { |
110ebe2b PP |
217 | return bt_self_component_filter_add_output_port( |
218 | self_comp, "out", NULL, NULL); | |
958f7d11 PP |
219 | } |
220 | ||
221 | static | |
222 | void destroy_muxer_comp(struct muxer_comp *muxer_comp) | |
223 | { | |
224 | if (!muxer_comp) { | |
225 | return; | |
226 | } | |
227 | ||
958f7d11 PP |
228 | g_free(muxer_comp); |
229 | } | |
230 | ||
65ee897d | 231 | static |
b727a996 | 232 | bt_value *get_default_params(void) |
65ee897d | 233 | { |
b727a996 | 234 | bt_value *params; |
65ee897d PP |
235 | int ret; |
236 | ||
600aa0de | 237 | params = bt_value_map_create(); |
65ee897d | 238 | if (!params) { |
fed72692 | 239 | BT_LOGE_STR("Cannot create a map value object."); |
65ee897d PP |
240 | goto error; |
241 | } | |
242 | ||
600aa0de | 243 | ret = bt_value_map_insert_bool_entry(params, |
c3acd5f3 | 244 | ASSUME_ABSOLUTE_CLOCK_CLASSES_PARAM_NAME, false); |
65ee897d | 245 | if (ret) { |
fed72692 | 246 | BT_LOGE_STR("Cannot add boolean value to map value object."); |
65ee897d PP |
247 | goto error; |
248 | } | |
249 | ||
250 | goto end; | |
251 | ||
252 | error: | |
b313ef44 | 253 | BT_VALUE_PUT_REF_AND_RESET(params); |
65ee897d PP |
254 | |
255 | end: | |
256 | return params; | |
257 | } | |
258 | ||
259 | static | |
600aa0de | 260 | int configure_muxer_comp(struct muxer_comp *muxer_comp, |
b727a996 | 261 | const bt_value *params) |
65ee897d | 262 | { |
b727a996 PP |
263 | bt_value *default_params = NULL; |
264 | bt_value *real_params = NULL; | |
265 | const bt_value *assume_absolute_clock_classes = NULL; | |
65ee897d | 266 | int ret = 0; |
c55a9f58 | 267 | bt_bool bool_val; |
65ee897d PP |
268 | |
269 | default_params = get_default_params(); | |
270 | if (!default_params) { | |
fed72692 PP |
271 | BT_LOGE("Cannot get default parameters: " |
272 | "muxer-comp-addr=%p", muxer_comp); | |
65ee897d PP |
273 | goto error; |
274 | } | |
275 | ||
0b0fff28 | 276 | ret = bt_value_map_extend(default_params, params, &real_params); |
e9edc0fb | 277 | if (ret) { |
fed72692 PP |
278 | BT_LOGE("Cannot extend default parameters map value: " |
279 | "muxer-comp-addr=%p, def-params-addr=%p, " | |
280 | "params-addr=%p", muxer_comp, default_params, | |
281 | params); | |
65ee897d PP |
282 | goto error; |
283 | } | |
284 | ||
600aa0de PP |
285 | assume_absolute_clock_classes = bt_value_map_borrow_entry_value(real_params, |
286 | ASSUME_ABSOLUTE_CLOCK_CLASSES_PARAM_NAME); | |
1d2f35fd PP |
287 | if (assume_absolute_clock_classes && |
288 | !bt_value_is_bool(assume_absolute_clock_classes)) { | |
fed72692 PP |
289 | BT_LOGE("Expecting a boolean value for the `%s` parameter: " |
290 | "muxer-comp-addr=%p, value-type=%s", | |
291 | ASSUME_ABSOLUTE_CLOCK_CLASSES_PARAM_NAME, muxer_comp, | |
310fc789 | 292 | bt_common_value_type_string( |
fed72692 | 293 | bt_value_get_type(assume_absolute_clock_classes))); |
65ee897d PP |
294 | goto error; |
295 | } | |
296 | ||
e9edc0fb | 297 | bool_val = bt_value_bool_get(assume_absolute_clock_classes); |
282c8cd0 | 298 | muxer_comp->assume_absolute_clock_classes = (bool) bool_val; |
fed72692 PP |
299 | BT_LOGD("Configured muxer component: muxer-comp-addr=%p, " |
300 | "assume-absolute-clock-classes=%d", | |
301 | muxer_comp, muxer_comp->assume_absolute_clock_classes); | |
65ee897d PP |
302 | goto end; |
303 | ||
304 | error: | |
305 | ret = -1; | |
306 | ||
307 | end: | |
b313ef44 PP |
308 | bt_value_put_ref(default_params); |
309 | bt_value_put_ref(real_params); | |
65ee897d PP |
310 | return ret; |
311 | } | |
312 | ||
958f7d11 | 313 | BT_HIDDEN |
43f077ae | 314 | bt_self_component_status muxer_init( |
b727a996 | 315 | bt_self_component_filter *self_comp, |
aa3ccb9a | 316 | const bt_value *params, void *init_data) |
958f7d11 PP |
317 | { |
318 | int ret; | |
43f077ae | 319 | bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK; |
958f7d11 PP |
320 | struct muxer_comp *muxer_comp = g_new0(struct muxer_comp, 1); |
321 | ||
fed72692 | 322 | BT_LOGD("Initializing muxer component: " |
110ebe2b | 323 | "comp-addr=%p, params-addr=%p", self_comp, params); |
fed72692 | 324 | |
958f7d11 | 325 | if (!muxer_comp) { |
fed72692 | 326 | BT_LOGE_STR("Failed to allocate one muxer component."); |
958f7d11 PP |
327 | goto error; |
328 | } | |
329 | ||
65ee897d PP |
330 | ret = configure_muxer_comp(muxer_comp, params); |
331 | if (ret) { | |
fed72692 PP |
332 | BT_LOGE("Cannot configure muxer component: " |
333 | "muxer-comp-addr=%p, params-addr=%p", | |
334 | muxer_comp, params); | |
65ee897d PP |
335 | goto error; |
336 | } | |
337 | ||
110ebe2b PP |
338 | muxer_comp->self_comp = self_comp; |
339 | bt_self_component_set_data( | |
5bb6a6f1 | 340 | bt_self_component_filter_as_self_component(self_comp), |
110ebe2b | 341 | muxer_comp); |
c67bbe08 | 342 | status = add_available_input_port(self_comp); |
110ebe2b | 343 | if (status != BT_SELF_COMPONENT_STATUS_OK) { |
fed72692 PP |
344 | BT_LOGE("Cannot ensure that at least one muxer component's input port is available: " |
345 | "muxer-comp-addr=%p, status=%s", | |
346 | muxer_comp, | |
110ebe2b | 347 | bt_self_component_status_string(status)); |
958f7d11 PP |
348 | goto error; |
349 | } | |
350 | ||
110ebe2b | 351 | status = create_output_port(self_comp); |
fed72692 PP |
352 | if (status) { |
353 | BT_LOGE("Cannot create muxer component's output port: " | |
354 | "muxer-comp-addr=%p, status=%s", | |
355 | muxer_comp, | |
110ebe2b | 356 | bt_self_component_status_string(status)); |
958f7d11 PP |
357 | goto error; |
358 | } | |
359 | ||
fed72692 PP |
360 | BT_LOGD("Initialized muxer component: " |
361 | "comp-addr=%p, params-addr=%p, muxer-comp-addr=%p", | |
110ebe2b | 362 | self_comp, params, muxer_comp); |
fed72692 | 363 | |
958f7d11 PP |
364 | goto end; |
365 | ||
366 | error: | |
367 | destroy_muxer_comp(muxer_comp); | |
110ebe2b | 368 | bt_self_component_set_data( |
5bb6a6f1 | 369 | bt_self_component_filter_as_self_component(self_comp), |
110ebe2b | 370 | NULL); |
147337a3 | 371 | |
110ebe2b PP |
372 | if (status == BT_SELF_COMPONENT_STATUS_OK) { |
373 | status = BT_SELF_COMPONENT_STATUS_ERROR; | |
147337a3 | 374 | } |
958f7d11 PP |
375 | |
376 | end: | |
377 | return status; | |
378 | } | |
379 | ||
380 | BT_HIDDEN | |
b727a996 | 381 | void muxer_finalize(bt_self_component_filter *self_comp) |
958f7d11 | 382 | { |
110ebe2b | 383 | struct muxer_comp *muxer_comp = bt_self_component_get_data( |
5bb6a6f1 | 384 | bt_self_component_filter_as_self_component(self_comp)); |
958f7d11 | 385 | |
fed72692 | 386 | BT_LOGD("Finalizing muxer component: comp-addr=%p", |
110ebe2b | 387 | self_comp); |
958f7d11 PP |
388 | destroy_muxer_comp(muxer_comp); |
389 | } | |
390 | ||
391 | static | |
a940e807 | 392 | bt_self_component_port_input_message_iterator * |
c67bbe08 | 393 | create_msg_iter_on_input_port(bt_self_component_port_input *self_port) |
958f7d11 | 394 | { |
b727a996 | 395 | const bt_port *port = bt_self_component_port_as_port( |
5bb6a6f1 | 396 | bt_self_component_port_input_as_self_component_port( |
110ebe2b | 397 | self_port)); |
a940e807 | 398 | bt_self_component_port_input_message_iterator *msg_iter = |
110ebe2b | 399 | NULL; |
958f7d11 | 400 | |
1d2f35fd PP |
401 | BT_ASSERT(port); |
402 | BT_ASSERT(bt_port_is_connected(port)); | |
958f7d11 | 403 | |
ab11110e | 404 | // TODO: Advance the iterator to >= the time of the latest |
a940e807 | 405 | // returned message by the muxer message |
ab11110e | 406 | // iterator which creates it. |
a940e807 | 407 | msg_iter = bt_self_component_port_input_message_iterator_create( |
110ebe2b | 408 | self_port); |
a940e807 PP |
409 | if (!msg_iter) { |
410 | BT_LOGE("Cannot create upstream message iterator on input port: " | |
110ebe2b PP |
411 | "port-addr=%p, port-name=\"%s\"", |
412 | port, bt_port_get_name(port)); | |
958f7d11 PP |
413 | goto end; |
414 | } | |
415 | ||
a940e807 PP |
416 | BT_LOGD("Created upstream message iterator on input port: " |
417 | "port-addr=%p, port-name=\"%s\", msg-iter-addr=%p", | |
418 | port, bt_port_get_name(port), msg_iter); | |
fed72692 | 419 | |
958f7d11 | 420 | end: |
a940e807 | 421 | return msg_iter; |
958f7d11 PP |
422 | } |
423 | ||
ab11110e | 424 | static |
12fe618d | 425 | bt_self_message_iterator_status muxer_upstream_msg_iter_next( |
1dd153f5 PP |
426 | struct muxer_upstream_msg_iter *muxer_upstream_msg_iter, |
427 | bool *is_ended) | |
ab11110e | 428 | { |
12fe618d FD |
429 | bt_self_message_iterator_status status; |
430 | bt_message_iterator_status input_port_iter_status; | |
a940e807 | 431 | bt_message_array_const msgs; |
0d6f8bf6 PP |
432 | uint64_t i; |
433 | uint64_t count; | |
ab11110e | 434 | |
a940e807 PP |
435 | BT_LOGV("Calling upstream message iterator's \"next\" method: " |
436 | "muxer-upstream-msg-iter-wrap-addr=%p, msg-iter-addr=%p", | |
437 | muxer_upstream_msg_iter, | |
438 | muxer_upstream_msg_iter->msg_iter); | |
12fe618d | 439 | input_port_iter_status = bt_self_component_port_input_message_iterator_next( |
a940e807 PP |
440 | muxer_upstream_msg_iter->msg_iter, &msgs, &count); |
441 | BT_LOGV("Upstream message iterator's \"next\" method returned: " | |
12fe618d | 442 | "status=%s", bt_message_iterator_status_string(input_port_iter_status)); |
ab11110e | 443 | |
12fe618d | 444 | switch (input_port_iter_status) { |
a940e807 | 445 | case BT_MESSAGE_ITERATOR_STATUS_OK: |
089717de | 446 | /* |
a940e807 | 447 | * Message iterator's current message is |
0d6f8bf6 | 448 | * valid: it must be considered for muxing operations. |
089717de | 449 | */ |
a940e807 | 450 | BT_LOGV_STR("Validated upstream message iterator wrapper."); |
0d6f8bf6 PP |
451 | BT_ASSERT(count > 0); |
452 | ||
a940e807 | 453 | /* Move messages to our queue */ |
0d6f8bf6 PP |
454 | for (i = 0; i < count; i++) { |
455 | /* | |
456 | * Push to tail in order; other side | |
a940e807 | 457 | * (muxer_msg_iter_do_next_one()) consumes |
0d6f8bf6 PP |
458 | * from the head first. |
459 | */ | |
a940e807 PP |
460 | g_queue_push_tail(muxer_upstream_msg_iter->msgs, |
461 | (void *) msgs[i]); | |
0d6f8bf6 | 462 | } |
12fe618d | 463 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_OK; |
ab11110e | 464 | break; |
a940e807 | 465 | case BT_MESSAGE_ITERATOR_STATUS_AGAIN: |
089717de | 466 | /* |
a940e807 | 467 | * Message iterator's current message is not |
089717de | 468 | * valid anymore. Return |
a940e807 | 469 | * BT_MESSAGE_ITERATOR_STATUS_AGAIN immediately. |
089717de | 470 | */ |
12fe618d | 471 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_AGAIN; |
ab11110e | 472 | break; |
a940e807 | 473 | case BT_MESSAGE_ITERATOR_STATUS_END: /* Fall-through. */ |
ab11110e | 474 | /* |
a940e807 | 475 | * Message iterator reached the end: release it. It |
ab11110e | 476 | * won't be considered again to find the youngest |
a940e807 | 477 | * message. |
ab11110e | 478 | */ |
1dd153f5 | 479 | *is_ended = true; |
12fe618d | 480 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_OK; |
089717de | 481 | break; |
ab11110e PP |
482 | default: |
483 | /* Error or unsupported status code */ | |
fed72692 | 484 | BT_LOGE("Error or unsupported status code: " |
12fe618d FD |
485 | "status-code=%d", input_port_iter_status); |
486 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR; | |
089717de | 487 | break; |
ab11110e PP |
488 | } |
489 | ||
089717de | 490 | return status; |
ab11110e PP |
491 | } |
492 | ||
958f7d11 | 493 | static |
a940e807 PP |
494 | int get_msg_ts_ns(struct muxer_comp *muxer_comp, |
495 | struct muxer_msg_iter *muxer_msg_iter, | |
496 | const bt_message *msg, int64_t last_returned_ts_ns, | |
958f7d11 PP |
497 | int64_t *ts_ns) |
498 | { | |
b727a996 | 499 | const bt_clock_class *clock_class = NULL; |
abcb4caf | 500 | const bt_clock_snapshot *clock_snapshot = NULL; |
958f7d11 | 501 | int ret = 0; |
282c8cd0 | 502 | const unsigned char *cc_uuid; |
fed72692 | 503 | const char *cc_name; |
43f077ae | 504 | bt_clock_snapshot_state cs_state = BT_CLOCK_SNAPSHOT_STATE_KNOWN; |
4f5b7d78 | 505 | bt_message_stream_activity_clock_snapshot_state sa_cs_state; |
958f7d11 | 506 | |
a940e807 | 507 | BT_ASSERT(msg); |
1d2f35fd | 508 | BT_ASSERT(ts_ns); |
958f7d11 | 509 | |
a940e807 PP |
510 | BT_LOGV("Getting message's timestamp: " |
511 | "muxer-msg-iter-addr=%p, msg-addr=%p, " | |
fed72692 | 512 | "last-returned-ts=%" PRId64, |
a940e807 | 513 | muxer_msg_iter, msg, last_returned_ts_ns); |
fed72692 | 514 | |
a940e807 PP |
515 | switch (bt_message_get_type(msg)) { |
516 | case BT_MESSAGE_TYPE_EVENT: | |
e087e50d PP |
517 | clock_class = |
518 | bt_message_event_borrow_stream_class_default_clock_class_const( | |
519 | msg); | |
520 | if (!clock_class) { | |
521 | goto no_clock_snapshot; | |
522 | } | |
523 | ||
3b59ce45 PP |
524 | cs_state = bt_message_event_borrow_default_clock_snapshot_const( |
525 | msg, &clock_snapshot); | |
958f7d11 | 526 | break; |
4f5b7d78 | 527 | case BT_MESSAGE_TYPE_PACKET_BEGINNING: |
83627662 PP |
528 | clock_class = |
529 | bt_message_packet_beginning_borrow_stream_class_default_clock_class_const( | |
530 | msg); | |
e087e50d PP |
531 | if (!clock_class) { |
532 | goto no_clock_snapshot; | |
533 | } | |
534 | ||
4f5b7d78 PP |
535 | cs_state = bt_message_packet_beginning_borrow_default_clock_snapshot_const( |
536 | msg, &clock_snapshot); | |
537 | break; | |
538 | case BT_MESSAGE_TYPE_PACKET_END: | |
83627662 PP |
539 | clock_class = |
540 | bt_message_packet_end_borrow_stream_class_default_clock_class_const( | |
541 | msg); | |
e087e50d PP |
542 | if (!clock_class) { |
543 | goto no_clock_snapshot; | |
544 | } | |
545 | ||
4f5b7d78 PP |
546 | cs_state = bt_message_packet_end_borrow_default_clock_snapshot_const( |
547 | msg, &clock_snapshot); | |
548 | break; | |
549 | case BT_MESSAGE_TYPE_DISCARDED_EVENTS: | |
83627662 PP |
550 | clock_class = |
551 | bt_message_discarded_events_borrow_stream_class_default_clock_class_const( | |
552 | msg); | |
e087e50d PP |
553 | if (!clock_class) { |
554 | goto no_clock_snapshot; | |
555 | } | |
556 | ||
4f5b7d78 PP |
557 | cs_state = bt_message_discarded_events_borrow_default_beginning_clock_snapshot_const( |
558 | msg, &clock_snapshot); | |
559 | break; | |
560 | case BT_MESSAGE_TYPE_DISCARDED_PACKETS: | |
83627662 PP |
561 | clock_class = |
562 | bt_message_discarded_packets_borrow_stream_class_default_clock_class_const( | |
563 | msg); | |
e087e50d PP |
564 | if (!clock_class) { |
565 | goto no_clock_snapshot; | |
566 | } | |
567 | ||
4f5b7d78 PP |
568 | cs_state = bt_message_discarded_packets_borrow_default_beginning_clock_snapshot_const( |
569 | msg, &clock_snapshot); | |
570 | break; | |
571 | case BT_MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING: | |
83627662 PP |
572 | clock_class = |
573 | bt_message_stream_activity_beginning_borrow_stream_class_default_clock_class_const( | |
574 | msg); | |
e087e50d PP |
575 | if (!clock_class) { |
576 | goto no_clock_snapshot; | |
577 | } | |
578 | ||
4f5b7d78 PP |
579 | sa_cs_state = bt_message_stream_activity_beginning_borrow_default_clock_snapshot_const( |
580 | msg, &clock_snapshot); | |
581 | if (sa_cs_state != BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_KNOWN) { | |
e087e50d | 582 | goto no_clock_snapshot; |
4f5b7d78 | 583 | } |
958f7d11 | 584 | |
4f5b7d78 PP |
585 | break; |
586 | case BT_MESSAGE_TYPE_STREAM_ACTIVITY_END: | |
83627662 PP |
587 | clock_class = |
588 | bt_message_stream_activity_end_borrow_stream_class_default_clock_class_const( | |
589 | msg); | |
e087e50d PP |
590 | if (!clock_class) { |
591 | goto no_clock_snapshot; | |
592 | } | |
593 | ||
4f5b7d78 PP |
594 | sa_cs_state = bt_message_stream_activity_end_borrow_default_clock_snapshot_const( |
595 | msg, &clock_snapshot); | |
596 | if (sa_cs_state != BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_KNOWN) { | |
e087e50d | 597 | goto no_clock_snapshot; |
4f5b7d78 PP |
598 | } |
599 | ||
600 | break; | |
9636ff62 | 601 | case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY: |
461b6faa | 602 | cs_state = |
9636ff62 | 603 | bt_message_message_iterator_inactivity_borrow_default_clock_snapshot_const( |
461b6faa | 604 | msg, &clock_snapshot); |
958f7d11 PP |
605 | break; |
606 | default: | |
a940e807 PP |
607 | /* All the other messages have a higher priority */ |
608 | BT_LOGV_STR("Message has no timestamp: using the last returned timestamp."); | |
958f7d11 PP |
609 | *ts_ns = last_returned_ts_ns; |
610 | goto end; | |
611 | } | |
612 | ||
461b6faa | 613 | if (cs_state != BT_CLOCK_SNAPSHOT_STATE_KNOWN) { |
abcb4caf | 614 | BT_LOGE_STR("Unsupported unknown clock snapshot."); |
69dc9aaf PP |
615 | ret = -1; |
616 | goto end; | |
617 | } | |
618 | ||
abcb4caf | 619 | clock_class = bt_clock_snapshot_borrow_clock_class_const(clock_snapshot); |
fb8afeef | 620 | BT_ASSERT(clock_class); |
50842bdc PP |
621 | cc_uuid = bt_clock_class_get_uuid(clock_class); |
622 | cc_name = bt_clock_class_get_name(clock_class); | |
282c8cd0 | 623 | |
a940e807 PP |
624 | if (muxer_msg_iter->clock_class_expectation == |
625 | MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ANY) { | |
282c8cd0 PP |
626 | /* |
627 | * This is the first clock class that this muxer | |
a940e807 | 628 | * message iterator encounters. Its properties |
282c8cd0 PP |
629 | * determine what to expect for the whole lifetime of |
630 | * the iterator without a true | |
631 | * `assume-absolute-clock-classes` parameter. | |
632 | */ | |
b30495a6 | 633 | if (bt_clock_class_origin_is_unix_epoch(clock_class)) { |
282c8cd0 | 634 | /* Expect absolute clock classes */ |
a940e807 PP |
635 | muxer_msg_iter->clock_class_expectation = |
636 | MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ABSOLUTE; | |
282c8cd0 PP |
637 | } else { |
638 | if (cc_uuid) { | |
639 | /* | |
640 | * Expect non-absolute clock classes | |
641 | * with a specific UUID. | |
642 | */ | |
a940e807 PP |
643 | muxer_msg_iter->clock_class_expectation = |
644 | MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID; | |
645 | memcpy(muxer_msg_iter->expected_clock_class_uuid, | |
282c8cd0 PP |
646 | cc_uuid, BABELTRACE_UUID_LEN); |
647 | } else { | |
648 | /* | |
649 | * Expect non-absolute clock classes | |
650 | * with no UUID. | |
651 | */ | |
a940e807 PP |
652 | muxer_msg_iter->clock_class_expectation = |
653 | MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_NO_UUID; | |
282c8cd0 PP |
654 | } |
655 | } | |
656 | } | |
657 | ||
658 | if (!muxer_comp->assume_absolute_clock_classes) { | |
a940e807 PP |
659 | switch (muxer_msg_iter->clock_class_expectation) { |
660 | case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ABSOLUTE: | |
b30495a6 | 661 | if (!bt_clock_class_origin_is_unix_epoch(clock_class)) { |
fed72692 PP |
662 | BT_LOGE("Expecting an absolute clock class, " |
663 | "but got a non-absolute one: " | |
664 | "clock-class-addr=%p, clock-class-name=\"%s\"", | |
665 | clock_class, cc_name); | |
282c8cd0 PP |
666 | goto error; |
667 | } | |
668 | break; | |
a940e807 | 669 | case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_NO_UUID: |
b30495a6 | 670 | if (bt_clock_class_origin_is_unix_epoch(clock_class)) { |
fed72692 PP |
671 | BT_LOGE("Expecting a non-absolute clock class with no UUID, " |
672 | "but got an absolute one: " | |
673 | "clock-class-addr=%p, clock-class-name=\"%s\"", | |
674 | clock_class, cc_name); | |
282c8cd0 PP |
675 | goto error; |
676 | } | |
677 | ||
678 | if (cc_uuid) { | |
fed72692 PP |
679 | BT_LOGE("Expecting a non-absolute clock class with no UUID, " |
680 | "but got one with a UUID: " | |
681 | "clock-class-addr=%p, clock-class-name=\"%s\", " | |
682 | "uuid=\"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\"", | |
683 | clock_class, cc_name, | |
684 | (unsigned int) cc_uuid[0], | |
685 | (unsigned int) cc_uuid[1], | |
686 | (unsigned int) cc_uuid[2], | |
687 | (unsigned int) cc_uuid[3], | |
688 | (unsigned int) cc_uuid[4], | |
689 | (unsigned int) cc_uuid[5], | |
690 | (unsigned int) cc_uuid[6], | |
691 | (unsigned int) cc_uuid[7], | |
692 | (unsigned int) cc_uuid[8], | |
693 | (unsigned int) cc_uuid[9], | |
694 | (unsigned int) cc_uuid[10], | |
695 | (unsigned int) cc_uuid[11], | |
696 | (unsigned int) cc_uuid[12], | |
697 | (unsigned int) cc_uuid[13], | |
698 | (unsigned int) cc_uuid[14], | |
699 | (unsigned int) cc_uuid[15]); | |
282c8cd0 PP |
700 | goto error; |
701 | } | |
702 | break; | |
a940e807 | 703 | case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID: |
b30495a6 | 704 | if (bt_clock_class_origin_is_unix_epoch(clock_class)) { |
fed72692 PP |
705 | BT_LOGE("Expecting a non-absolute clock class with a specific UUID, " |
706 | "but got an absolute one: " | |
707 | "clock-class-addr=%p, clock-class-name=\"%s\"", | |
708 | clock_class, cc_name); | |
282c8cd0 PP |
709 | goto error; |
710 | } | |
711 | ||
712 | if (!cc_uuid) { | |
fed72692 PP |
713 | BT_LOGE("Expecting a non-absolute clock class with a specific UUID, " |
714 | "but got one with no UUID: " | |
715 | "clock-class-addr=%p, clock-class-name=\"%s\"", | |
716 | clock_class, cc_name); | |
282c8cd0 PP |
717 | goto error; |
718 | } | |
719 | ||
a940e807 | 720 | if (memcmp(muxer_msg_iter->expected_clock_class_uuid, |
282c8cd0 | 721 | cc_uuid, BABELTRACE_UUID_LEN) != 0) { |
fed72692 PP |
722 | BT_LOGE("Expecting a non-absolute clock class with a specific UUID, " |
723 | "but got one with different UUID: " | |
724 | "clock-class-addr=%p, clock-class-name=\"%s\", " | |
725 | "expected-uuid=\"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\", " | |
726 | "uuid=\"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\"", | |
727 | clock_class, cc_name, | |
a940e807 PP |
728 | (unsigned int) muxer_msg_iter->expected_clock_class_uuid[0], |
729 | (unsigned int) muxer_msg_iter->expected_clock_class_uuid[1], | |
730 | (unsigned int) muxer_msg_iter->expected_clock_class_uuid[2], | |
731 | (unsigned int) muxer_msg_iter->expected_clock_class_uuid[3], | |
732 | (unsigned int) muxer_msg_iter->expected_clock_class_uuid[4], | |
733 | (unsigned int) muxer_msg_iter->expected_clock_class_uuid[5], | |
734 | (unsigned int) muxer_msg_iter->expected_clock_class_uuid[6], | |
735 | (unsigned int) muxer_msg_iter->expected_clock_class_uuid[7], | |
736 | (unsigned int) muxer_msg_iter->expected_clock_class_uuid[8], | |
737 | (unsigned int) muxer_msg_iter->expected_clock_class_uuid[9], | |
738 | (unsigned int) muxer_msg_iter->expected_clock_class_uuid[10], | |
739 | (unsigned int) muxer_msg_iter->expected_clock_class_uuid[11], | |
740 | (unsigned int) muxer_msg_iter->expected_clock_class_uuid[12], | |
741 | (unsigned int) muxer_msg_iter->expected_clock_class_uuid[13], | |
742 | (unsigned int) muxer_msg_iter->expected_clock_class_uuid[14], | |
743 | (unsigned int) muxer_msg_iter->expected_clock_class_uuid[15], | |
fed72692 PP |
744 | (unsigned int) cc_uuid[0], |
745 | (unsigned int) cc_uuid[1], | |
746 | (unsigned int) cc_uuid[2], | |
747 | (unsigned int) cc_uuid[3], | |
748 | (unsigned int) cc_uuid[4], | |
749 | (unsigned int) cc_uuid[5], | |
750 | (unsigned int) cc_uuid[6], | |
751 | (unsigned int) cc_uuid[7], | |
752 | (unsigned int) cc_uuid[8], | |
753 | (unsigned int) cc_uuid[9], | |
754 | (unsigned int) cc_uuid[10], | |
755 | (unsigned int) cc_uuid[11], | |
756 | (unsigned int) cc_uuid[12], | |
757 | (unsigned int) cc_uuid[13], | |
758 | (unsigned int) cc_uuid[14], | |
759 | (unsigned int) cc_uuid[15]); | |
282c8cd0 PP |
760 | goto error; |
761 | } | |
762 | break; | |
763 | default: | |
764 | /* Unexpected */ | |
fed72692 PP |
765 | BT_LOGF("Unexpected clock class expectation: " |
766 | "expectation-code=%d", | |
a940e807 | 767 | muxer_msg_iter->clock_class_expectation); |
282c8cd0 PP |
768 | abort(); |
769 | } | |
958f7d11 PP |
770 | } |
771 | ||
abcb4caf | 772 | ret = bt_clock_snapshot_get_ns_from_origin(clock_snapshot, ts_ns); |
958f7d11 | 773 | if (ret) { |
abcb4caf PP |
774 | BT_LOGE("Cannot get nanoseconds from Epoch of clock snapshot: " |
775 | "clock-snapshot-addr=%p", clock_snapshot); | |
958f7d11 PP |
776 | goto error; |
777 | } | |
778 | ||
779 | goto end; | |
780 | ||
e087e50d PP |
781 | no_clock_snapshot: |
782 | BT_LOGV_STR("Message's default clock snapshot is missing: " | |
783 | "using the last returned timestamp."); | |
784 | *ts_ns = last_returned_ts_ns; | |
785 | goto end; | |
786 | ||
958f7d11 PP |
787 | error: |
788 | ret = -1; | |
789 | ||
790 | end: | |
fed72692 | 791 | if (ret == 0) { |
a940e807 PP |
792 | BT_LOGV("Found message's timestamp: " |
793 | "muxer-msg-iter-addr=%p, msg-addr=%p, " | |
fed72692 | 794 | "last-returned-ts=%" PRId64 ", ts=%" PRId64, |
a940e807 | 795 | muxer_msg_iter, msg, last_returned_ts_ns, |
fed72692 PP |
796 | *ts_ns); |
797 | } | |
798 | ||
958f7d11 PP |
799 | return ret; |
800 | } | |
801 | ||
ab11110e | 802 | /* |
a940e807 PP |
803 | * This function finds the youngest available message amongst the |
804 | * non-ended upstream message iterators and returns the upstream | |
805 | * message iterator which has it, or | |
806 | * BT_MESSAGE_ITERATOR_STATUS_END if there's no available | |
807 | * message. | |
ab11110e PP |
808 | * |
809 | * This function does NOT: | |
810 | * | |
a940e807 | 811 | * * Update any upstream message iterator. |
a940e807 | 812 | * * Check the upstream message iterators to retry. |
ab11110e | 813 | * |
a940e807 PP |
814 | * On sucess, this function sets *muxer_upstream_msg_iter to the |
815 | * upstream message iterator of which the current message is | |
ab11110e PP |
816 | * the youngest, and sets *ts_ns to its time. |
817 | */ | |
958f7d11 | 818 | static |
12fe618d | 819 | bt_self_message_iterator_status |
a940e807 | 820 | muxer_msg_iter_youngest_upstream_msg_iter( |
958f7d11 | 821 | struct muxer_comp *muxer_comp, |
a940e807 PP |
822 | struct muxer_msg_iter *muxer_msg_iter, |
823 | struct muxer_upstream_msg_iter **muxer_upstream_msg_iter, | |
958f7d11 PP |
824 | int64_t *ts_ns) |
825 | { | |
826 | size_t i; | |
827 | int ret; | |
828 | int64_t youngest_ts_ns = INT64_MAX; | |
12fe618d FD |
829 | bt_self_message_iterator_status status = |
830 | BT_SELF_MESSAGE_ITERATOR_STATUS_OK; | |
958f7d11 | 831 | |
1d2f35fd | 832 | BT_ASSERT(muxer_comp); |
a940e807 PP |
833 | BT_ASSERT(muxer_msg_iter); |
834 | BT_ASSERT(muxer_upstream_msg_iter); | |
835 | *muxer_upstream_msg_iter = NULL; | |
836 | ||
1dd153f5 PP |
837 | for (i = 0; i < muxer_msg_iter->active_muxer_upstream_msg_iters->len; |
838 | i++) { | |
a940e807 PP |
839 | const bt_message *msg; |
840 | struct muxer_upstream_msg_iter *cur_muxer_upstream_msg_iter = | |
1dd153f5 PP |
841 | g_ptr_array_index( |
842 | muxer_msg_iter->active_muxer_upstream_msg_iters, | |
843 | i); | |
a940e807 PP |
844 | int64_t msg_ts_ns; |
845 | ||
846 | if (!cur_muxer_upstream_msg_iter->msg_iter) { | |
847 | /* This upstream message iterator is ended */ | |
848 | BT_LOGV("Skipping ended upstream message iterator: " | |
849 | "muxer-upstream-msg-iter-wrap-addr=%p", | |
850 | cur_muxer_upstream_msg_iter); | |
958f7d11 PP |
851 | continue; |
852 | } | |
853 | ||
a940e807 PP |
854 | BT_ASSERT(cur_muxer_upstream_msg_iter->msgs->length > 0); |
855 | msg = g_queue_peek_head(cur_muxer_upstream_msg_iter->msgs); | |
856 | BT_ASSERT(msg); | |
857 | ret = get_msg_ts_ns(muxer_comp, muxer_msg_iter, msg, | |
858 | muxer_msg_iter->last_returned_ts_ns, &msg_ts_ns); | |
958f7d11 | 859 | if (ret) { |
a940e807 PP |
860 | /* get_msg_ts_ns() logs errors */ |
861 | *muxer_upstream_msg_iter = NULL; | |
12fe618d | 862 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR; |
958f7d11 PP |
863 | goto end; |
864 | } | |
865 | ||
a940e807 PP |
866 | if (msg_ts_ns <= youngest_ts_ns) { |
867 | *muxer_upstream_msg_iter = | |
868 | cur_muxer_upstream_msg_iter; | |
869 | youngest_ts_ns = msg_ts_ns; | |
958f7d11 PP |
870 | *ts_ns = youngest_ts_ns; |
871 | } | |
872 | } | |
873 | ||
a940e807 | 874 | if (!*muxer_upstream_msg_iter) { |
12fe618d | 875 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_END; |
958f7d11 PP |
876 | *ts_ns = INT64_MIN; |
877 | } | |
878 | ||
879 | end: | |
880 | return status; | |
881 | } | |
882 | ||
883 | static | |
12fe618d | 884 | bt_self_message_iterator_status validate_muxer_upstream_msg_iter( |
1dd153f5 PP |
885 | struct muxer_upstream_msg_iter *muxer_upstream_msg_iter, |
886 | bool *is_ended) | |
958f7d11 | 887 | { |
12fe618d FD |
888 | bt_self_message_iterator_status status = |
889 | BT_SELF_MESSAGE_ITERATOR_STATUS_OK; | |
958f7d11 | 890 | |
a940e807 PP |
891 | BT_LOGV("Validating muxer's upstream message iterator wrapper: " |
892 | "muxer-upstream-msg-iter-wrap-addr=%p", | |
893 | muxer_upstream_msg_iter); | |
fed72692 | 894 | |
a940e807 PP |
895 | if (muxer_upstream_msg_iter->msgs->length > 0 || |
896 | !muxer_upstream_msg_iter->msg_iter) { | |
0d6f8bf6 | 897 | BT_LOGV("Already valid or not considered: " |
a940e807 PP |
898 | "queue-len=%u, upstream-msg-iter-addr=%p", |
899 | muxer_upstream_msg_iter->msgs->length, | |
900 | muxer_upstream_msg_iter->msg_iter); | |
ab11110e PP |
901 | goto end; |
902 | } | |
903 | ||
a940e807 | 904 | /* muxer_upstream_msg_iter_next() logs details/errors */ |
1dd153f5 PP |
905 | status = muxer_upstream_msg_iter_next(muxer_upstream_msg_iter, |
906 | is_ended); | |
089717de PP |
907 | |
908 | end: | |
909 | return status; | |
910 | } | |
911 | ||
912 | static | |
12fe618d | 913 | bt_self_message_iterator_status validate_muxer_upstream_msg_iters( |
a940e807 | 914 | struct muxer_msg_iter *muxer_msg_iter) |
089717de | 915 | { |
12fe618d FD |
916 | bt_self_message_iterator_status status = |
917 | BT_SELF_MESSAGE_ITERATOR_STATUS_OK; | |
089717de PP |
918 | size_t i; |
919 | ||
a940e807 PP |
920 | BT_LOGV("Validating muxer's upstream message iterator wrappers: " |
921 | "muxer-msg-iter-addr=%p", muxer_msg_iter); | |
fed72692 | 922 | |
1dd153f5 PP |
923 | for (i = 0; i < muxer_msg_iter->active_muxer_upstream_msg_iters->len; |
924 | i++) { | |
f7cd56ea | 925 | bool is_ended = false; |
a940e807 | 926 | struct muxer_upstream_msg_iter *muxer_upstream_msg_iter = |
089717de | 927 | g_ptr_array_index( |
1dd153f5 | 928 | muxer_msg_iter->active_muxer_upstream_msg_iters, |
089717de PP |
929 | i); |
930 | ||
a940e807 | 931 | status = validate_muxer_upstream_msg_iter( |
1dd153f5 | 932 | muxer_upstream_msg_iter, &is_ended); |
12fe618d | 933 | if (status != BT_SELF_MESSAGE_ITERATOR_STATUS_OK) { |
fed72692 | 934 | if (status < 0) { |
a940e807 PP |
935 | BT_LOGE("Cannot validate muxer's upstream message iterator wrapper: " |
936 | "muxer-msg-iter-addr=%p, " | |
937 | "muxer-upstream-msg-iter-wrap-addr=%p", | |
938 | muxer_msg_iter, | |
939 | muxer_upstream_msg_iter); | |
fed72692 | 940 | } else { |
a940e807 PP |
941 | BT_LOGV("Cannot validate muxer's upstream message iterator wrapper: " |
942 | "muxer-msg-iter-addr=%p, " | |
943 | "muxer-upstream-msg-iter-wrap-addr=%p", | |
944 | muxer_msg_iter, | |
945 | muxer_upstream_msg_iter); | |
fed72692 PP |
946 | } |
947 | ||
089717de PP |
948 | goto end; |
949 | } | |
744ba28b PP |
950 | |
951 | /* | |
1dd153f5 PP |
952 | * Move this muxer upstream message iterator to the |
953 | * array of ended iterators if it's ended. | |
744ba28b | 954 | */ |
1dd153f5 PP |
955 | if (unlikely(is_ended)) { |
956 | BT_LOGV("Muxer's upstream message iterator wrapper: ended or canceled: " | |
957 | "muxer-msg-iter-addr=%p, " | |
958 | "muxer-upstream-msg-iter-wrap-addr=%p", | |
959 | muxer_msg_iter, muxer_upstream_msg_iter); | |
960 | g_ptr_array_add( | |
961 | muxer_msg_iter->ended_muxer_upstream_msg_iters, | |
962 | muxer_upstream_msg_iter); | |
963 | muxer_msg_iter->active_muxer_upstream_msg_iters->pdata[i] = NULL; | |
964 | ||
744ba28b PP |
965 | /* |
966 | * Use g_ptr_array_remove_fast() because the | |
967 | * order of those elements is not important. | |
968 | */ | |
969 | g_ptr_array_remove_index_fast( | |
1dd153f5 | 970 | muxer_msg_iter->active_muxer_upstream_msg_iters, |
744ba28b PP |
971 | i); |
972 | i--; | |
973 | } | |
089717de PP |
974 | } |
975 | ||
976 | end: | |
977 | return status; | |
978 | } | |
979 | ||
0d6f8bf6 | 980 | static inline |
12fe618d | 981 | bt_self_message_iterator_status muxer_msg_iter_do_next_one( |
089717de | 982 | struct muxer_comp *muxer_comp, |
a940e807 PP |
983 | struct muxer_msg_iter *muxer_msg_iter, |
984 | const bt_message **msg) | |
089717de | 985 | { |
c67bbe08 | 986 | bt_self_message_iterator_status status; |
a940e807 | 987 | struct muxer_upstream_msg_iter *muxer_upstream_msg_iter = NULL; |
089717de PP |
988 | int64_t next_return_ts; |
989 | ||
c67bbe08 PP |
990 | status = validate_muxer_upstream_msg_iters(muxer_msg_iter); |
991 | if (status != BT_SELF_MESSAGE_ITERATOR_STATUS_OK) { | |
992 | /* validate_muxer_upstream_msg_iters() logs details */ | |
993 | goto end; | |
958f7d11 PP |
994 | } |
995 | ||
996 | /* | |
089717de | 997 | * At this point we know that all the existing upstream |
a940e807 PP |
998 | * message iterators are valid. We can find the one, |
999 | * amongst those, of which the current message is the | |
089717de | 1000 | * youngest. |
958f7d11 | 1001 | */ |
a940e807 PP |
1002 | status = muxer_msg_iter_youngest_upstream_msg_iter(muxer_comp, |
1003 | muxer_msg_iter, &muxer_upstream_msg_iter, | |
089717de | 1004 | &next_return_ts); |
12fe618d | 1005 | if (status < 0 || status == BT_SELF_MESSAGE_ITERATOR_STATUS_END) { |
0d6f8bf6 | 1006 | if (status < 0) { |
a940e807 | 1007 | BT_LOGE("Cannot find the youngest upstream message iterator wrapper: " |
fed72692 | 1008 | "status=%s", |
28b4f657 | 1009 | bt_common_self_message_iterator_status_string(status)); |
fed72692 | 1010 | } else { |
a940e807 | 1011 | BT_LOGV("Cannot find the youngest upstream message iterator wrapper: " |
fed72692 | 1012 | "status=%s", |
28b4f657 | 1013 | bt_common_self_message_iterator_status_string(status)); |
fed72692 PP |
1014 | } |
1015 | ||
958f7d11 PP |
1016 | goto end; |
1017 | } | |
1018 | ||
a940e807 PP |
1019 | if (next_return_ts < muxer_msg_iter->last_returned_ts_ns) { |
1020 | BT_LOGE("Youngest upstream message iterator wrapper's timestamp is less than muxer's message iterator's last returned timestamp: " | |
1021 | "muxer-msg-iter-addr=%p, ts=%" PRId64 ", " | |
fed72692 | 1022 | "last-returned-ts=%" PRId64, |
a940e807 PP |
1023 | muxer_msg_iter, next_return_ts, |
1024 | muxer_msg_iter->last_returned_ts_ns); | |
12fe618d | 1025 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR; |
958f7d11 PP |
1026 | goto end; |
1027 | } | |
1028 | ||
a940e807 PP |
1029 | BT_LOGV("Found youngest upstream message iterator wrapper: " |
1030 | "muxer-msg-iter-addr=%p, " | |
1031 | "muxer-upstream-msg-iter-wrap-addr=%p, " | |
fed72692 | 1032 | "ts=%" PRId64, |
a940e807 | 1033 | muxer_msg_iter, muxer_upstream_msg_iter, next_return_ts); |
12fe618d | 1034 | BT_ASSERT(status == BT_SELF_MESSAGE_ITERATOR_STATUS_OK); |
a940e807 | 1035 | BT_ASSERT(muxer_upstream_msg_iter); |
958f7d11 PP |
1036 | |
1037 | /* | |
0d6f8bf6 | 1038 | * Consume from the queue's head: other side |
a940e807 | 1039 | * (muxer_upstream_msg_iter_next()) writes to the tail. |
958f7d11 | 1040 | */ |
a940e807 PP |
1041 | *msg = g_queue_pop_head(muxer_upstream_msg_iter->msgs); |
1042 | BT_ASSERT(*msg); | |
1043 | muxer_msg_iter->last_returned_ts_ns = next_return_ts; | |
958f7d11 PP |
1044 | |
1045 | end: | |
0d6f8bf6 PP |
1046 | return status; |
1047 | } | |
1048 | ||
1049 | static | |
12fe618d | 1050 | bt_self_message_iterator_status muxer_msg_iter_do_next( |
0d6f8bf6 | 1051 | struct muxer_comp *muxer_comp, |
a940e807 PP |
1052 | struct muxer_msg_iter *muxer_msg_iter, |
1053 | bt_message_array_const msgs, uint64_t capacity, | |
0d6f8bf6 PP |
1054 | uint64_t *count) |
1055 | { | |
12fe618d FD |
1056 | bt_self_message_iterator_status status = |
1057 | BT_SELF_MESSAGE_ITERATOR_STATUS_OK; | |
0d6f8bf6 PP |
1058 | uint64_t i = 0; |
1059 | ||
90a9d4fe | 1060 | while (i < capacity && status == BT_SELF_MESSAGE_ITERATOR_STATUS_OK) { |
a940e807 PP |
1061 | status = muxer_msg_iter_do_next_one(muxer_comp, |
1062 | muxer_msg_iter, &msgs[i]); | |
90a9d4fe | 1063 | if (status == BT_SELF_MESSAGE_ITERATOR_STATUS_OK) { |
0d6f8bf6 PP |
1064 | i++; |
1065 | } | |
1066 | } | |
1067 | ||
1068 | if (i > 0) { | |
1069 | /* | |
a940e807 | 1070 | * Even if muxer_msg_iter_do_next_one() returned |
0d6f8bf6 | 1071 | * something else than |
a940e807 PP |
1072 | * BT_MESSAGE_ITERATOR_STATUS_OK, we accumulated |
1073 | * message objects in the output message | |
0d6f8bf6 | 1074 | * array, so we need to return |
a940e807 | 1075 | * BT_MESSAGE_ITERATOR_STATUS_OK so that they are |
0d6f8bf6 | 1076 | * transfered to downstream. This other status occurs |
a940e807 | 1077 | * again the next time muxer_msg_iter_do_next() is |
0d6f8bf6 | 1078 | * called, possibly without any accumulated |
a940e807 | 1079 | * message, in which case we'll return it. |
0d6f8bf6 PP |
1080 | */ |
1081 | *count = i; | |
12fe618d | 1082 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_OK; |
0d6f8bf6 PP |
1083 | } |
1084 | ||
1085 | return status; | |
958f7d11 PP |
1086 | } |
1087 | ||
1088 | static | |
a940e807 | 1089 | void destroy_muxer_msg_iter(struct muxer_msg_iter *muxer_msg_iter) |
ab11110e | 1090 | { |
a940e807 | 1091 | if (!muxer_msg_iter) { |
ab11110e PP |
1092 | return; |
1093 | } | |
1094 | ||
a940e807 PP |
1095 | BT_LOGD("Destroying muxer component's message iterator: " |
1096 | "muxer-msg-iter-addr=%p", muxer_msg_iter); | |
fed72692 | 1097 | |
1dd153f5 PP |
1098 | if (muxer_msg_iter->active_muxer_upstream_msg_iters) { |
1099 | BT_LOGD_STR("Destroying muxer's active upstream message iterator wrappers."); | |
1100 | g_ptr_array_free( | |
1101 | muxer_msg_iter->active_muxer_upstream_msg_iters, TRUE); | |
1102 | } | |
1103 | ||
1104 | if (muxer_msg_iter->ended_muxer_upstream_msg_iters) { | |
1105 | BT_LOGD_STR("Destroying muxer's ended upstream message iterator wrappers."); | |
ab11110e | 1106 | g_ptr_array_free( |
1dd153f5 | 1107 | muxer_msg_iter->ended_muxer_upstream_msg_iters, TRUE); |
ab11110e PP |
1108 | } |
1109 | ||
a940e807 | 1110 | g_free(muxer_msg_iter); |
ab11110e PP |
1111 | } |
1112 | ||
1113 | static | |
c67bbe08 | 1114 | int muxer_msg_iter_init_upstream_iterators(struct muxer_comp *muxer_comp, |
a940e807 | 1115 | struct muxer_msg_iter *muxer_msg_iter) |
958f7d11 | 1116 | { |
544d0515 PP |
1117 | int64_t count; |
1118 | int64_t i; | |
ab11110e | 1119 | int ret = 0; |
958f7d11 | 1120 | |
110ebe2b | 1121 | count = bt_component_filter_get_input_port_count( |
5bb6a6f1 | 1122 | bt_self_component_filter_as_component_filter( |
110ebe2b | 1123 | muxer_comp->self_comp)); |
544d0515 | 1124 | if (count < 0) { |
a940e807 PP |
1125 | BT_LOGD("No input port to initialize for muxer component's message iterator: " |
1126 | "muxer-comp-addr=%p, muxer-msg-iter-addr=%p", | |
1127 | muxer_comp, muxer_msg_iter); | |
ab11110e PP |
1128 | goto end; |
1129 | } | |
958f7d11 PP |
1130 | |
1131 | for (i = 0; i < count; i++) { | |
c67bbe08 PP |
1132 | bt_self_component_port_input_message_iterator *upstream_msg_iter; |
1133 | struct muxer_upstream_msg_iter *muxer_upstream_msg_iter; | |
b727a996 | 1134 | bt_self_component_port_input *self_port = |
110ebe2b PP |
1135 | bt_self_component_filter_borrow_input_port_by_index( |
1136 | muxer_comp->self_comp, i); | |
b727a996 | 1137 | const bt_port *port; |
958f7d11 | 1138 | |
110ebe2b | 1139 | BT_ASSERT(self_port); |
5bb6a6f1 PP |
1140 | port = bt_self_component_port_as_port( |
1141 | bt_self_component_port_input_as_self_component_port( | |
110ebe2b | 1142 | self_port)); |
1d2f35fd | 1143 | BT_ASSERT(port); |
958f7d11 PP |
1144 | |
1145 | if (!bt_port_is_connected(port)) { | |
c67bbe08 | 1146 | /* Skip non-connected port */ |
958f7d11 PP |
1147 | continue; |
1148 | } | |
1149 | ||
c67bbe08 PP |
1150 | upstream_msg_iter = create_msg_iter_on_input_port(self_port); |
1151 | if (!upstream_msg_iter) { | |
1152 | /* create_msg_iter_on_input_port() logs errors */ | |
1153 | BT_ASSERT(!upstream_msg_iter); | |
ab11110e PP |
1154 | ret = -1; |
1155 | goto end; | |
958f7d11 | 1156 | } |
fed72692 | 1157 | |
c67bbe08 PP |
1158 | muxer_upstream_msg_iter = |
1159 | muxer_msg_iter_add_upstream_msg_iter( | |
1160 | muxer_msg_iter, upstream_msg_iter); | |
1161 | bt_self_component_port_input_message_iterator_put_ref( | |
1162 | upstream_msg_iter); | |
1163 | if (!muxer_upstream_msg_iter) { | |
1164 | /* muxer_msg_iter_add_upstream_msg_iter() logs errors */ | |
1165 | ret = -1; | |
1166 | goto end; | |
1167 | } | |
958f7d11 PP |
1168 | } |
1169 | ||
1170 | end: | |
958f7d11 PP |
1171 | return ret; |
1172 | } | |
1173 | ||
958f7d11 | 1174 | BT_HIDDEN |
43f077ae | 1175 | bt_self_message_iterator_status muxer_msg_iter_init( |
a940e807 | 1176 | bt_self_message_iterator *self_msg_iter, |
b727a996 PP |
1177 | bt_self_component_filter *self_comp, |
1178 | bt_self_component_port_output *port) | |
958f7d11 PP |
1179 | { |
1180 | struct muxer_comp *muxer_comp = NULL; | |
a940e807 | 1181 | struct muxer_msg_iter *muxer_msg_iter = NULL; |
43f077ae | 1182 | bt_self_message_iterator_status status = |
12fe618d | 1183 | BT_SELF_MESSAGE_ITERATOR_STATUS_OK; |
958f7d11 PP |
1184 | int ret; |
1185 | ||
110ebe2b | 1186 | muxer_comp = bt_self_component_get_data( |
5bb6a6f1 | 1187 | bt_self_component_filter_as_self_component(self_comp)); |
1d2f35fd | 1188 | BT_ASSERT(muxer_comp); |
a940e807 PP |
1189 | BT_LOGD("Initializing muxer component's message iterator: " |
1190 | "comp-addr=%p, muxer-comp-addr=%p, msg-iter-addr=%p", | |
1191 | self_comp, muxer_comp, self_msg_iter); | |
a09c6b95 | 1192 | |
a940e807 | 1193 | if (muxer_comp->initializing_muxer_msg_iter) { |
a09c6b95 | 1194 | /* |
089717de | 1195 | * Weird, unhandled situation detected: downstream |
a940e807 PP |
1196 | * creates a muxer message iterator while creating |
1197 | * another muxer message iterator (same component). | |
a09c6b95 | 1198 | */ |
a940e807 PP |
1199 | BT_LOGE("Recursive initialization of muxer component's message iterator: " |
1200 | "comp-addr=%p, muxer-comp-addr=%p, msg-iter-addr=%p", | |
1201 | self_comp, muxer_comp, self_msg_iter); | |
958f7d11 PP |
1202 | goto error; |
1203 | } | |
1204 | ||
a940e807 PP |
1205 | muxer_comp->initializing_muxer_msg_iter = true; |
1206 | muxer_msg_iter = g_new0(struct muxer_msg_iter, 1); | |
1207 | if (!muxer_msg_iter) { | |
1208 | BT_LOGE_STR("Failed to allocate one muxer component's message iterator."); | |
ab11110e PP |
1209 | goto error; |
1210 | } | |
1211 | ||
a940e807 | 1212 | muxer_msg_iter->last_returned_ts_ns = INT64_MIN; |
1dd153f5 | 1213 | muxer_msg_iter->active_muxer_upstream_msg_iters = |
958f7d11 | 1214 | g_ptr_array_new_with_free_func( |
a940e807 | 1215 | (GDestroyNotify) destroy_muxer_upstream_msg_iter); |
1dd153f5 PP |
1216 | if (!muxer_msg_iter->active_muxer_upstream_msg_iters) { |
1217 | BT_LOGE_STR("Failed to allocate a GPtrArray."); | |
1218 | goto error; | |
1219 | } | |
1220 | ||
1221 | muxer_msg_iter->ended_muxer_upstream_msg_iters = | |
1222 | g_ptr_array_new_with_free_func( | |
1223 | (GDestroyNotify) destroy_muxer_upstream_msg_iter); | |
1224 | if (!muxer_msg_iter->ended_muxer_upstream_msg_iters) { | |
fed72692 | 1225 | BT_LOGE_STR("Failed to allocate a GPtrArray."); |
958f7d11 PP |
1226 | goto error; |
1227 | } | |
1228 | ||
c67bbe08 | 1229 | ret = muxer_msg_iter_init_upstream_iterators(muxer_comp, |
a940e807 | 1230 | muxer_msg_iter); |
a09c6b95 | 1231 | if (ret) { |
c67bbe08 | 1232 | BT_LOGE("Cannot initialize connected input ports for muxer component's message iterator: " |
fed72692 | 1233 | "comp-addr=%p, muxer-comp-addr=%p, " |
a940e807 PP |
1234 | "muxer-msg-iter-addr=%p, msg-iter-addr=%p, ret=%d", |
1235 | self_comp, muxer_comp, muxer_msg_iter, | |
1236 | self_msg_iter, ret); | |
a09c6b95 PP |
1237 | goto error; |
1238 | } | |
1239 | ||
c67bbe08 | 1240 | bt_self_message_iterator_set_data(self_msg_iter, muxer_msg_iter); |
a940e807 PP |
1241 | BT_LOGD("Initialized muxer component's message iterator: " |
1242 | "comp-addr=%p, muxer-comp-addr=%p, muxer-msg-iter-addr=%p, " | |
1243 | "msg-iter-addr=%p", | |
1244 | self_comp, muxer_comp, muxer_msg_iter, self_msg_iter); | |
958f7d11 PP |
1245 | goto end; |
1246 | ||
1247 | error: | |
a940e807 | 1248 | destroy_muxer_msg_iter(muxer_msg_iter); |
c67bbe08 | 1249 | bt_self_message_iterator_set_data(self_msg_iter, NULL); |
12fe618d | 1250 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR; |
958f7d11 PP |
1251 | |
1252 | end: | |
a940e807 | 1253 | muxer_comp->initializing_muxer_msg_iter = false; |
958f7d11 PP |
1254 | return status; |
1255 | } | |
1256 | ||
1257 | BT_HIDDEN | |
1dd153f5 | 1258 | void muxer_msg_iter_finalize(bt_self_message_iterator *self_msg_iter) |
958f7d11 | 1259 | { |
a940e807 PP |
1260 | struct muxer_msg_iter *muxer_msg_iter = |
1261 | bt_self_message_iterator_get_data(self_msg_iter); | |
b727a996 | 1262 | bt_self_component *self_comp = NULL; |
958f7d11 PP |
1263 | struct muxer_comp *muxer_comp = NULL; |
1264 | ||
a940e807 PP |
1265 | self_comp = bt_self_message_iterator_borrow_component( |
1266 | self_msg_iter); | |
110ebe2b PP |
1267 | BT_ASSERT(self_comp); |
1268 | muxer_comp = bt_self_component_get_data(self_comp); | |
a940e807 PP |
1269 | BT_LOGD("Finalizing muxer component's message iterator: " |
1270 | "comp-addr=%p, muxer-comp-addr=%p, muxer-msg-iter-addr=%p, " | |
1271 | "msg-iter-addr=%p", | |
1272 | self_comp, muxer_comp, muxer_msg_iter, self_msg_iter); | |
958f7d11 | 1273 | |
c67bbe08 | 1274 | if (muxer_msg_iter) { |
a940e807 | 1275 | destroy_muxer_msg_iter(muxer_msg_iter); |
958f7d11 | 1276 | } |
958f7d11 PP |
1277 | } |
1278 | ||
1279 | BT_HIDDEN | |
12fe618d | 1280 | bt_self_message_iterator_status muxer_msg_iter_next( |
a940e807 PP |
1281 | bt_self_message_iterator *self_msg_iter, |
1282 | bt_message_array_const msgs, uint64_t capacity, | |
0d6f8bf6 | 1283 | uint64_t *count) |
958f7d11 | 1284 | { |
12fe618d | 1285 | bt_self_message_iterator_status status; |
a940e807 PP |
1286 | struct muxer_msg_iter *muxer_msg_iter = |
1287 | bt_self_message_iterator_get_data(self_msg_iter); | |
b727a996 | 1288 | bt_self_component *self_comp = NULL; |
958f7d11 | 1289 | struct muxer_comp *muxer_comp = NULL; |
958f7d11 | 1290 | |
a940e807 PP |
1291 | BT_ASSERT(muxer_msg_iter); |
1292 | self_comp = bt_self_message_iterator_borrow_component( | |
1293 | self_msg_iter); | |
110ebe2b PP |
1294 | BT_ASSERT(self_comp); |
1295 | muxer_comp = bt_self_component_get_data(self_comp); | |
1d2f35fd | 1296 | BT_ASSERT(muxer_comp); |
a940e807 PP |
1297 | BT_LOGV("Muxer component's message iterator's \"next\" method called: " |
1298 | "comp-addr=%p, muxer-comp-addr=%p, muxer-msg-iter-addr=%p, " | |
1299 | "msg-iter-addr=%p", | |
1300 | self_comp, muxer_comp, muxer_msg_iter, self_msg_iter); | |
fed72692 | 1301 | |
a940e807 PP |
1302 | status = muxer_msg_iter_do_next(muxer_comp, muxer_msg_iter, |
1303 | msgs, capacity, count); | |
0d6f8bf6 | 1304 | if (status < 0) { |
a940e807 PP |
1305 | BT_LOGE("Cannot get next message: " |
1306 | "comp-addr=%p, muxer-comp-addr=%p, muxer-msg-iter-addr=%p, " | |
1307 | "msg-iter-addr=%p, status=%s", | |
1308 | self_comp, muxer_comp, muxer_msg_iter, self_msg_iter, | |
28b4f657 | 1309 | bt_common_self_message_iterator_status_string(status)); |
fed72692 | 1310 | } else { |
a940e807 | 1311 | BT_LOGV("Returning from muxer component's message iterator's \"next\" method: " |
0d6f8bf6 | 1312 | "status=%s", |
28b4f657 | 1313 | bt_common_self_message_iterator_status_string(status)); |
fed72692 | 1314 | } |
958f7d11 | 1315 | |
0d6f8bf6 | 1316 | return status; |
958f7d11 PP |
1317 | } |
1318 | ||
1319 | BT_HIDDEN | |
43f077ae | 1320 | bt_self_component_status muxer_input_port_connected( |
b727a996 PP |
1321 | bt_self_component_filter *self_comp, |
1322 | bt_self_component_port_input *self_port, | |
1323 | const bt_port_output *other_port) | |
958f7d11 | 1324 | { |
c67bbe08 | 1325 | bt_self_component_status status; |
958f7d11 | 1326 | |
c67bbe08 PP |
1327 | status = add_available_input_port(self_comp); |
1328 | if (status) { | |
06a2cb0d PP |
1329 | /* |
1330 | * Only way to report an error later since this | |
1331 | * method does not return anything. | |
1332 | */ | |
c67bbe08 PP |
1333 | BT_LOGE("Cannot add one muxer component's input port: " |
1334 | "status=%s", | |
1335 | bt_self_component_status_string(status)); | |
06a2cb0d PP |
1336 | goto end; |
1337 | } | |
1338 | ||
958f7d11 | 1339 | end: |
1f40c0ec | 1340 | return status; |
958f7d11 | 1341 | } |
c496a6aa | 1342 | |
1dd153f5 PP |
1343 | static inline |
1344 | bt_bool muxer_upstream_msg_iters_can_all_seek_beginning( | |
1345 | GPtrArray *muxer_upstream_msg_iters) | |
c496a6aa | 1346 | { |
c496a6aa PP |
1347 | uint64_t i; |
1348 | bt_bool ret = BT_TRUE; | |
1349 | ||
1dd153f5 | 1350 | for (i = 0; i < muxer_upstream_msg_iters->len; i++) { |
c496a6aa | 1351 | struct muxer_upstream_msg_iter *upstream_msg_iter = |
1dd153f5 | 1352 | muxer_upstream_msg_iters->pdata[i]; |
c496a6aa PP |
1353 | |
1354 | if (!bt_self_component_port_input_message_iterator_can_seek_beginning( | |
1355 | upstream_msg_iter->msg_iter)) { | |
1356 | ret = BT_FALSE; | |
1357 | goto end; | |
1358 | } | |
1359 | } | |
1360 | ||
1361 | end: | |
1362 | return ret; | |
1363 | } | |
1364 | ||
1dd153f5 PP |
1365 | BT_HIDDEN |
1366 | bt_bool muxer_msg_iter_can_seek_beginning( | |
1367 | bt_self_message_iterator *self_msg_iter) | |
1368 | { | |
1369 | struct muxer_msg_iter *muxer_msg_iter = | |
1370 | bt_self_message_iterator_get_data(self_msg_iter); | |
1371 | bt_bool ret = BT_TRUE; | |
1372 | ||
1373 | if (!muxer_upstream_msg_iters_can_all_seek_beginning( | |
1374 | muxer_msg_iter->active_muxer_upstream_msg_iters)) { | |
1375 | ret = BT_FALSE; | |
1376 | goto end; | |
1377 | } | |
1378 | ||
1379 | if (!muxer_upstream_msg_iters_can_all_seek_beginning( | |
1380 | muxer_msg_iter->ended_muxer_upstream_msg_iters)) { | |
1381 | ret = BT_FALSE; | |
1382 | goto end; | |
1383 | } | |
1384 | ||
1385 | end: | |
1386 | return ret; | |
1387 | } | |
1388 | ||
c496a6aa PP |
1389 | BT_HIDDEN |
1390 | bt_self_message_iterator_status muxer_msg_iter_seek_beginning( | |
1391 | bt_self_message_iterator *self_msg_iter) | |
1392 | { | |
1393 | struct muxer_msg_iter *muxer_msg_iter = | |
1394 | bt_self_message_iterator_get_data(self_msg_iter); | |
1395 | int status; | |
1396 | uint64_t i; | |
1397 | ||
1dd153f5 PP |
1398 | /* Seek all ended upstream iterators first */ |
1399 | for (i = 0; i < muxer_msg_iter->ended_muxer_upstream_msg_iters->len; | |
1400 | i++) { | |
c496a6aa | 1401 | struct muxer_upstream_msg_iter *upstream_msg_iter = |
1dd153f5 | 1402 | muxer_msg_iter->ended_muxer_upstream_msg_iters->pdata[i]; |
c496a6aa PP |
1403 | |
1404 | status = bt_self_component_port_input_message_iterator_seek_beginning( | |
1405 | upstream_msg_iter->msg_iter); | |
1406 | if (status != BT_MESSAGE_ITERATOR_STATUS_OK) { | |
1407 | goto end; | |
1408 | } | |
1dd153f5 PP |
1409 | |
1410 | empty_message_queue(upstream_msg_iter); | |
1411 | } | |
1412 | ||
1413 | /* Seek all previously active upstream iterators */ | |
1414 | for (i = 0; i < muxer_msg_iter->active_muxer_upstream_msg_iters->len; | |
1415 | i++) { | |
1416 | struct muxer_upstream_msg_iter *upstream_msg_iter = | |
1417 | muxer_msg_iter->active_muxer_upstream_msg_iters->pdata[i]; | |
1418 | ||
1419 | status = bt_self_component_port_input_message_iterator_seek_beginning( | |
1420 | upstream_msg_iter->msg_iter); | |
1421 | if (status != BT_MESSAGE_ITERATOR_STATUS_OK) { | |
1422 | goto end; | |
1423 | } | |
1424 | ||
1425 | empty_message_queue(upstream_msg_iter); | |
1426 | } | |
1427 | ||
1428 | /* Make them all active */ | |
1429 | for (i = 0; i < muxer_msg_iter->ended_muxer_upstream_msg_iters->len; | |
1430 | i++) { | |
1431 | struct muxer_upstream_msg_iter *upstream_msg_iter = | |
1432 | muxer_msg_iter->ended_muxer_upstream_msg_iters->pdata[i]; | |
1433 | ||
1434 | g_ptr_array_add(muxer_msg_iter->active_muxer_upstream_msg_iters, | |
1435 | upstream_msg_iter); | |
1436 | muxer_msg_iter->ended_muxer_upstream_msg_iters->pdata[i] = NULL; | |
c496a6aa PP |
1437 | } |
1438 | ||
1dd153f5 PP |
1439 | g_ptr_array_remove_range(muxer_msg_iter->ended_muxer_upstream_msg_iters, |
1440 | 0, muxer_msg_iter->ended_muxer_upstream_msg_iters->len); | |
c496a6aa | 1441 | muxer_msg_iter->last_returned_ts_ns = INT64_MIN; |
1dd153f5 PP |
1442 | muxer_msg_iter->clock_class_expectation = |
1443 | MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ANY; | |
c496a6aa PP |
1444 | |
1445 | end: | |
1446 | return status; | |
1447 | } |