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