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