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