lib: notification iterator: transfer a batch of notifications
[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/values-internal.h>
30 #include <babeltrace/graph/component-internal.h>
31 #include <babeltrace/graph/notification-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 <stdlib.h>
39 #include <string.h>
40
41 #define ASSUME_ABSOLUTE_CLOCK_CLASSES_PARAM_NAME "assume-absolute-clock-classes"
42
43 struct muxer_comp {
44 /*
45 * Array of struct
46 * bt_private_connection_private_notification_iterator *
47 * (weak refs)
48 */
49 GPtrArray *muxer_notif_iters;
50
51 /* Weak ref */
52 struct bt_private_component *priv_comp;
53 unsigned int next_port_num;
54 size_t available_input_ports;
55 bool error;
56 bool initializing_muxer_notif_iter;
57 bool assume_absolute_clock_classes;
58 };
59
60 struct muxer_upstream_notif_iter {
61 /* Owned by this, NULL if ended */
62 struct bt_notification_iterator *notif_iter;
63
64 /* Contains `struct bt_notification *`, owned by this */
65 GQueue *notifs;
66 };
67
68 enum muxer_notif_iter_clock_class_expectation {
69 MUXER_NOTIF_ITER_CLOCK_CLASS_EXPECTATION_ANY = 0,
70 MUXER_NOTIF_ITER_CLOCK_CLASS_EXPECTATION_ABSOLUTE,
71 MUXER_NOTIF_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID,
72 MUXER_NOTIF_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_NO_UUID,
73 };
74
75 struct muxer_notif_iter {
76 /*
77 * Array of struct muxer_upstream_notif_iter * (owned by this).
78 *
79 * NOTE: This array is searched in linearly to find the youngest
80 * current notification. Keep this until benchmarks confirm that
81 * another data structure is faster than this for our typical
82 * use cases.
83 */
84 GPtrArray *muxer_upstream_notif_iters;
85
86 /*
87 * List of "recently" connected input ports (weak) to
88 * handle by this muxer notification iterator.
89 * muxer_port_connected() adds entries to this list, and the
90 * entries are removed when a notification iterator is created
91 * on the port's connection and put into
92 * muxer_upstream_notif_iters above by
93 * muxer_notif_iter_handle_newly_connected_ports().
94 */
95 GList *newly_connected_priv_ports;
96
97 /* Last time returned in a notification */
98 int64_t last_returned_ts_ns;
99
100 /* Clock class expectation state */
101 enum muxer_notif_iter_clock_class_expectation clock_class_expectation;
102
103 /*
104 * Expected clock class UUID, only valid when
105 * clock_class_expectation is
106 * MUXER_NOTIF_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID.
107 */
108 unsigned char expected_clock_class_uuid[BABELTRACE_UUID_LEN];
109 };
110
111 static
112 void destroy_muxer_upstream_notif_iter(
113 struct muxer_upstream_notif_iter *muxer_upstream_notif_iter)
114 {
115 if (!muxer_upstream_notif_iter) {
116 return;
117 }
118
119 BT_LOGD("Destroying muxer's upstream notification iterator wrapper: "
120 "addr=%p, notif-iter-addr=%p, queue-len=%u",
121 muxer_upstream_notif_iter,
122 muxer_upstream_notif_iter->notif_iter,
123 muxer_upstream_notif_iter->notifs->length);
124 bt_put(muxer_upstream_notif_iter->notif_iter);
125
126 if (muxer_upstream_notif_iter->notifs) {
127 struct bt_notification *notif;
128
129 while ((notif = g_queue_pop_head(
130 muxer_upstream_notif_iter->notifs))) {
131 bt_put(notif);
132 }
133
134 g_queue_free(muxer_upstream_notif_iter->notifs);
135 }
136
137 g_free(muxer_upstream_notif_iter);
138 }
139
140 static
141 struct muxer_upstream_notif_iter *muxer_notif_iter_add_upstream_notif_iter(
142 struct muxer_notif_iter *muxer_notif_iter,
143 struct bt_notification_iterator *notif_iter,
144 struct bt_private_port *priv_port)
145 {
146 struct muxer_upstream_notif_iter *muxer_upstream_notif_iter =
147 g_new0(struct muxer_upstream_notif_iter, 1);
148
149 if (!muxer_upstream_notif_iter) {
150 BT_LOGE_STR("Failed to allocate one muxer's upstream notification iterator wrapper.");
151 goto end;
152 }
153
154 muxer_upstream_notif_iter->notif_iter = bt_get(notif_iter);
155 muxer_upstream_notif_iter->notifs = g_queue_new();
156 if (!muxer_upstream_notif_iter->notifs) {
157 BT_LOGE_STR("Failed to allocate a GQueue.");
158
159 goto end;
160 }
161
162 g_ptr_array_add(muxer_notif_iter->muxer_upstream_notif_iters,
163 muxer_upstream_notif_iter);
164 BT_LOGD("Added muxer's upstream notification iterator wrapper: "
165 "addr=%p, muxer-notif-iter-addr=%p, notif-iter-addr=%p",
166 muxer_upstream_notif_iter, muxer_notif_iter,
167 notif_iter);
168
169 end:
170 return muxer_upstream_notif_iter;
171 }
172
173 static
174 enum bt_component_status ensure_available_input_port(
175 struct bt_private_component *priv_comp)
176 {
177 struct muxer_comp *muxer_comp =
178 bt_private_component_get_user_data(priv_comp);
179 enum bt_component_status status = BT_COMPONENT_STATUS_OK;
180 GString *port_name = NULL;
181
182 BT_ASSERT(muxer_comp);
183
184 if (muxer_comp->available_input_ports >= 1) {
185 goto end;
186 }
187
188 port_name = g_string_new("in");
189 if (!port_name) {
190 BT_LOGE_STR("Failed to allocate a GString.");
191 status = BT_COMPONENT_STATUS_NOMEM;
192 goto end;
193 }
194
195 g_string_append_printf(port_name, "%u", muxer_comp->next_port_num);
196 status = bt_private_component_filter_add_input_private_port(
197 priv_comp, port_name->str, NULL, NULL);
198 if (status != BT_COMPONENT_STATUS_OK) {
199 BT_LOGE("Cannot add input port to muxer component: "
200 "port-name=\"%s\", comp-addr=%p, status=%s",
201 port_name->str, priv_comp,
202 bt_component_status_string(status));
203 goto end;
204 }
205
206 muxer_comp->available_input_ports++;
207 muxer_comp->next_port_num++;
208 BT_LOGD("Added one input port to muxer component: "
209 "port-name=\"%s\", comp-addr=%p",
210 port_name->str, priv_comp);
211 end:
212 if (port_name) {
213 g_string_free(port_name, TRUE);
214 }
215
216 return status;
217 }
218
219 static
220 enum bt_component_status create_output_port(
221 struct bt_private_component *priv_comp)
222 {
223 return bt_private_component_filter_add_output_private_port(
224 priv_comp, "out", NULL, NULL);
225 }
226
227 static
228 void destroy_muxer_comp(struct muxer_comp *muxer_comp)
229 {
230 if (!muxer_comp) {
231 return;
232 }
233
234 BT_LOGD("Destroying muxer component: muxer-comp-addr=%p, "
235 "muxer-notif-iter-count=%u", muxer_comp,
236 muxer_comp->muxer_notif_iters ?
237 muxer_comp->muxer_notif_iters->len : 0);
238
239 if (muxer_comp->muxer_notif_iters) {
240 g_ptr_array_free(muxer_comp->muxer_notif_iters, TRUE);
241 }
242
243 g_free(muxer_comp);
244 }
245
246 static
247 struct bt_value *get_default_params(void)
248 {
249 struct bt_value *params;
250 int ret;
251
252 params = bt_value_map_create();
253 if (!params) {
254 BT_LOGE_STR("Cannot create a map value object.");
255 goto error;
256 }
257
258 ret = bt_value_map_insert_bool(params,
259 ASSUME_ABSOLUTE_CLOCK_CLASSES_PARAM_NAME, false);
260 if (ret) {
261 BT_LOGE_STR("Cannot add boolean value to map value object.");
262 goto error;
263 }
264
265 goto end;
266
267 error:
268 BT_PUT(params);
269
270 end:
271 return params;
272 }
273
274 static
275 int configure_muxer_comp(struct muxer_comp *muxer_comp, struct bt_value *params)
276 {
277 struct bt_value *default_params = NULL;
278 struct bt_value *real_params = NULL;
279 struct bt_value *assume_absolute_clock_classes = NULL;
280 int ret = 0;
281 bt_bool bool_val;
282
283 default_params = get_default_params();
284 if (!default_params) {
285 BT_LOGE("Cannot get default parameters: "
286 "muxer-comp-addr=%p", muxer_comp);
287 goto error;
288 }
289
290 real_params = bt_value_map_extend(default_params, params);
291 if (!real_params) {
292 BT_LOGE("Cannot extend default parameters map value: "
293 "muxer-comp-addr=%p, def-params-addr=%p, "
294 "params-addr=%p", muxer_comp, default_params,
295 params);
296 goto error;
297 }
298
299 assume_absolute_clock_classes = bt_value_map_borrow(real_params,
300 ASSUME_ABSOLUTE_CLOCK_CLASSES_PARAM_NAME);
301 if (assume_absolute_clock_classes &&
302 !bt_value_is_bool(assume_absolute_clock_classes)) {
303 BT_LOGE("Expecting a boolean value for the `%s` parameter: "
304 "muxer-comp-addr=%p, value-type=%s",
305 ASSUME_ABSOLUTE_CLOCK_CLASSES_PARAM_NAME, muxer_comp,
306 bt_value_type_string(
307 bt_value_get_type(assume_absolute_clock_classes)));
308 goto error;
309 }
310
311 ret = bt_value_bool_get(assume_absolute_clock_classes, &bool_val);
312 BT_ASSERT(ret == 0);
313 muxer_comp->assume_absolute_clock_classes = (bool) bool_val;
314 BT_LOGD("Configured muxer component: muxer-comp-addr=%p, "
315 "assume-absolute-clock-classes=%d",
316 muxer_comp, muxer_comp->assume_absolute_clock_classes);
317 goto end;
318
319 error:
320 ret = -1;
321
322 end:
323 bt_put(default_params);
324 bt_put(real_params);
325 return ret;
326 }
327
328 BT_HIDDEN
329 enum bt_component_status muxer_init(
330 struct bt_private_component *priv_comp,
331 struct bt_value *params, void *init_data)
332 {
333 int ret;
334 enum bt_component_status status = BT_COMPONENT_STATUS_OK;
335 struct muxer_comp *muxer_comp = g_new0(struct muxer_comp, 1);
336
337 BT_LOGD("Initializing muxer component: "
338 "comp-addr=%p, params-addr=%p", priv_comp, params);
339
340 if (!muxer_comp) {
341 BT_LOGE_STR("Failed to allocate one muxer component.");
342 goto error;
343 }
344
345 ret = configure_muxer_comp(muxer_comp, params);
346 if (ret) {
347 BT_LOGE("Cannot configure muxer component: "
348 "muxer-comp-addr=%p, params-addr=%p",
349 muxer_comp, params);
350 goto error;
351 }
352
353 muxer_comp->muxer_notif_iters = g_ptr_array_new();
354 if (!muxer_comp->muxer_notif_iters) {
355 BT_LOGE_STR("Failed to allocate a GPtrArray.");
356 goto error;
357 }
358
359 muxer_comp->priv_comp = priv_comp;
360 ret = bt_private_component_set_user_data(priv_comp, muxer_comp);
361 BT_ASSERT(ret == 0);
362 status = ensure_available_input_port(priv_comp);
363 if (status != BT_COMPONENT_STATUS_OK) {
364 BT_LOGE("Cannot ensure that at least one muxer component's input port is available: "
365 "muxer-comp-addr=%p, status=%s",
366 muxer_comp,
367 bt_component_status_string(status));
368 goto error;
369 }
370
371 status = create_output_port(priv_comp);
372 if (status) {
373 BT_LOGE("Cannot create muxer component's output port: "
374 "muxer-comp-addr=%p, status=%s",
375 muxer_comp,
376 bt_component_status_string(status));
377 goto error;
378 }
379
380 BT_LOGD("Initialized muxer component: "
381 "comp-addr=%p, params-addr=%p, muxer-comp-addr=%p",
382 priv_comp, params, muxer_comp);
383
384 goto end;
385
386 error:
387 destroy_muxer_comp(muxer_comp);
388 ret = bt_private_component_set_user_data(priv_comp, NULL);
389 BT_ASSERT(ret == 0);
390
391 if (status == BT_COMPONENT_STATUS_OK) {
392 status = BT_COMPONENT_STATUS_ERROR;
393 }
394
395 end:
396 return status;
397 }
398
399 BT_HIDDEN
400 void muxer_finalize(struct bt_private_component *priv_comp)
401 {
402 struct muxer_comp *muxer_comp =
403 bt_private_component_get_user_data(priv_comp);
404
405 BT_LOGD("Finalizing muxer component: comp-addr=%p",
406 priv_comp);
407 destroy_muxer_comp(muxer_comp);
408 }
409
410 static
411 struct bt_notification_iterator *create_notif_iter_on_input_port(
412 struct bt_private_port *priv_port, int *ret)
413 {
414 struct bt_port *port = bt_port_borrow_from_private(priv_port);
415 struct bt_notification_iterator *notif_iter = NULL;
416 struct bt_private_connection *priv_conn = NULL;
417 enum bt_connection_status conn_status;
418
419 BT_ASSERT(ret);
420 *ret = 0;
421 BT_ASSERT(port);
422 BT_ASSERT(bt_port_is_connected(port));
423 priv_conn = bt_private_port_get_private_connection(priv_port);
424 BT_ASSERT(priv_conn);
425
426 // TODO: Advance the iterator to >= the time of the latest
427 // returned notification by the muxer notification
428 // iterator which creates it.
429 conn_status = bt_private_connection_create_notification_iterator(
430 priv_conn, &notif_iter);
431 if (conn_status != BT_CONNECTION_STATUS_OK) {
432 BT_LOGE("Cannot create upstream notification iterator on input port's connection: "
433 "port-addr=%p, port-name=\"%s\", conn-addr=%p, "
434 "status=%s",
435 port, bt_port_get_name(port), priv_conn,
436 bt_connection_status_string(conn_status));
437 *ret = -1;
438 goto end;
439 }
440
441 BT_LOGD("Created upstream notification iterator on input port's connection: "
442 "port-addr=%p, port-name=\"%s\", conn-addr=%p, "
443 "notif-iter-addr=%p",
444 port, bt_port_get_name(port), priv_conn, notif_iter);
445
446 end:
447 bt_put(priv_conn);
448 return notif_iter;
449 }
450
451 static
452 enum bt_notification_iterator_status muxer_upstream_notif_iter_next(
453 struct muxer_upstream_notif_iter *muxer_upstream_notif_iter)
454 {
455 enum bt_notification_iterator_status status;
456 bt_notification_array notifs;
457 uint64_t i;
458 uint64_t count;
459
460 BT_LOGV("Calling upstream notification iterator's \"next\" method: "
461 "muxer-upstream-notif-iter-wrap-addr=%p, notif-iter-addr=%p",
462 muxer_upstream_notif_iter,
463 muxer_upstream_notif_iter->notif_iter);
464 status = bt_private_connection_notification_iterator_next(
465 muxer_upstream_notif_iter->notif_iter, &notifs, &count);
466 BT_LOGV("Upstream notification iterator's \"next\" method returned: "
467 "status=%s", bt_notification_iterator_status_string(status));
468
469 switch (status) {
470 case BT_NOTIFICATION_ITERATOR_STATUS_OK:
471 /*
472 * Notification iterator's current notification is
473 * valid: it must be considered for muxing operations.
474 */
475 BT_LOGV_STR("Validated upstream notification iterator wrapper.");
476 BT_ASSERT(count > 0);
477
478 /* Move notifications to our queue */
479 for (i = 0; i < count; i++) {
480 /*
481 * Push to tail in order; other side
482 * (muxer_notif_iter_do_next_one()) consumes
483 * from the head first.
484 */
485 g_queue_push_tail(muxer_upstream_notif_iter->notifs,
486 notifs[i]);
487 }
488 break;
489 case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN:
490 /*
491 * Notification iterator's current notification is not
492 * valid anymore. Return
493 * BT_NOTIFICATION_ITERATOR_STATUS_AGAIN immediately.
494 */
495 break;
496 case BT_NOTIFICATION_ITERATOR_STATUS_END: /* Fall-through. */
497 case BT_NOTIFICATION_ITERATOR_STATUS_CANCELED:
498 /*
499 * Notification iterator reached the end: release it. It
500 * won't be considered again to find the youngest
501 * notification.
502 */
503 BT_PUT(muxer_upstream_notif_iter->notif_iter);
504 status = BT_NOTIFICATION_ITERATOR_STATUS_OK;
505 break;
506 default:
507 /* Error or unsupported status code */
508 BT_LOGE("Error or unsupported status code: "
509 "status-code=%d", status);
510 status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
511 break;
512 }
513
514 return status;
515 }
516
517 static
518 int muxer_notif_iter_handle_newly_connected_ports(
519 struct muxer_notif_iter *muxer_notif_iter)
520 {
521 int ret = 0;
522
523 BT_LOGV("Handling newly connected ports: "
524 "muxer-notif-iter-addr=%p", muxer_notif_iter);
525
526 /*
527 * Here we create one upstream notification iterator for each
528 * newly connected port. We do NOT perform an initial "next" on
529 * those new upstream notification iterators: they are
530 * invalidated, to be validated later. The list of newly
531 * connected ports to handle here is updated by
532 * muxer_port_connected().
533 */
534 while (true) {
535 GList *node = muxer_notif_iter->newly_connected_priv_ports;
536 struct bt_private_port *priv_port;
537 struct bt_port *port;
538 struct bt_notification_iterator *upstream_notif_iter = NULL;
539 struct muxer_upstream_notif_iter *muxer_upstream_notif_iter;
540
541 if (!node) {
542 break;
543 }
544
545 priv_port = node->data;
546 port = bt_port_borrow_from_private(priv_port);
547 BT_ASSERT(port);
548
549 if (!bt_port_is_connected(port)) {
550 /*
551 * Looks like this port is not connected
552 * anymore: we can't create an upstream
553 * notification iterator on its (non-existing)
554 * connection in this case.
555 */
556 goto remove_node;
557 }
558
559 upstream_notif_iter = create_notif_iter_on_input_port(priv_port,
560 &ret);
561 if (ret) {
562 /* create_notif_iter_on_input_port() logs errors */
563 BT_ASSERT(!upstream_notif_iter);
564 goto error;
565 }
566
567 muxer_upstream_notif_iter =
568 muxer_notif_iter_add_upstream_notif_iter(
569 muxer_notif_iter, upstream_notif_iter,
570 priv_port);
571 BT_PUT(upstream_notif_iter);
572 if (!muxer_upstream_notif_iter) {
573 /*
574 * muxer_notif_iter_add_upstream_notif_iter()
575 * logs errors.
576 */
577 goto error;
578 }
579
580 remove_node:
581 bt_put(upstream_notif_iter);
582 muxer_notif_iter->newly_connected_priv_ports =
583 g_list_delete_link(
584 muxer_notif_iter->newly_connected_priv_ports,
585 node);
586 }
587
588 goto end;
589
590 error:
591 if (ret >= 0) {
592 ret = -1;
593 }
594
595 end:
596 return ret;
597 }
598
599 static
600 int get_notif_ts_ns(struct muxer_comp *muxer_comp,
601 struct muxer_notif_iter *muxer_notif_iter,
602 struct bt_notification *notif, int64_t last_returned_ts_ns,
603 int64_t *ts_ns)
604 {
605 struct bt_clock_class_priority_map *cc_prio_map = NULL;
606 struct bt_clock_class *clock_class = NULL;
607 struct bt_clock_value *clock_value = NULL;
608 struct bt_event *event = NULL;
609 int ret = 0;
610 const unsigned char *cc_uuid;
611 const char *cc_name;
612
613 BT_ASSERT(notif);
614 BT_ASSERT(ts_ns);
615
616 BT_LOGV("Getting notification's timestamp: "
617 "muxer-notif-iter-addr=%p, notif-addr=%p, "
618 "last-returned-ts=%" PRId64,
619 muxer_notif_iter, notif, last_returned_ts_ns);
620
621 switch (bt_notification_get_type(notif)) {
622 case BT_NOTIFICATION_TYPE_EVENT:
623 cc_prio_map =
624 bt_notification_event_borrow_clock_class_priority_map(
625 notif);
626 break;
627
628 case BT_NOTIFICATION_TYPE_INACTIVITY:
629 cc_prio_map =
630 bt_notification_inactivity_borrow_clock_class_priority_map(
631 notif);
632 break;
633 default:
634 /* All the other notifications have a higher priority */
635 BT_LOGV_STR("Notification has no timestamp: using the last returned timestamp.");
636 *ts_ns = last_returned_ts_ns;
637 goto end;
638 }
639
640 if (!cc_prio_map) {
641 BT_LOGE("Cannot get notification's clock class priority map: "
642 "notif-addr=%p", notif);
643 goto error;
644 }
645
646 /*
647 * If the clock class priority map is empty, then we consider
648 * that this notification has no time. In this case it's always
649 * the youngest.
650 */
651 if (bt_clock_class_priority_map_get_clock_class_count(cc_prio_map) == 0) {
652 BT_LOGV_STR("Notification's clock class priority map contains 0 clock classes: "
653 "using the last returned timestamp.");
654 *ts_ns = last_returned_ts_ns;
655 goto end;
656 }
657
658 clock_class =
659 bt_clock_class_priority_map_borrow_highest_priority_clock_class(
660 cc_prio_map);
661 if (!clock_class) {
662 BT_LOGE("Cannot get the clock class with the highest priority from clock class priority map: "
663 "cc-prio-map-addr=%p", cc_prio_map);
664 goto error;
665 }
666
667 cc_uuid = bt_clock_class_get_uuid(clock_class);
668 cc_name = bt_clock_class_get_name(clock_class);
669
670 if (muxer_notif_iter->clock_class_expectation ==
671 MUXER_NOTIF_ITER_CLOCK_CLASS_EXPECTATION_ANY) {
672 /*
673 * This is the first clock class that this muxer
674 * notification 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_notif_iter->clock_class_expectation =
682 MUXER_NOTIF_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_notif_iter->clock_class_expectation =
690 MUXER_NOTIF_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID;
691 memcpy(muxer_notif_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_notif_iter->clock_class_expectation =
699 MUXER_NOTIF_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_NO_UUID;
700 }
701 }
702 }
703
704 if (!muxer_comp->assume_absolute_clock_classes) {
705 switch (muxer_notif_iter->clock_class_expectation) {
706 case MUXER_NOTIF_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_NOTIF_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_NOTIF_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_notif_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_notif_iter->expected_clock_class_uuid[0],
775 (unsigned int) muxer_notif_iter->expected_clock_class_uuid[1],
776 (unsigned int) muxer_notif_iter->expected_clock_class_uuid[2],
777 (unsigned int) muxer_notif_iter->expected_clock_class_uuid[3],
778 (unsigned int) muxer_notif_iter->expected_clock_class_uuid[4],
779 (unsigned int) muxer_notif_iter->expected_clock_class_uuid[5],
780 (unsigned int) muxer_notif_iter->expected_clock_class_uuid[6],
781 (unsigned int) muxer_notif_iter->expected_clock_class_uuid[7],
782 (unsigned int) muxer_notif_iter->expected_clock_class_uuid[8],
783 (unsigned int) muxer_notif_iter->expected_clock_class_uuid[9],
784 (unsigned int) muxer_notif_iter->expected_clock_class_uuid[10],
785 (unsigned int) muxer_notif_iter->expected_clock_class_uuid[11],
786 (unsigned int) muxer_notif_iter->expected_clock_class_uuid[12],
787 (unsigned int) muxer_notif_iter->expected_clock_class_uuid[13],
788 (unsigned int) muxer_notif_iter->expected_clock_class_uuid[14],
789 (unsigned int) muxer_notif_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_notif_iter->clock_class_expectation);
814 abort();
815 }
816 }
817
818 switch (bt_notification_get_type(notif)) {
819 case BT_NOTIFICATION_TYPE_EVENT:
820 event = bt_notification_event_borrow_event(notif);
821 BT_ASSERT(event);
822 clock_value = bt_event_borrow_clock_value(event,
823 clock_class);
824 break;
825 case BT_NOTIFICATION_TYPE_INACTIVITY:
826 clock_value = bt_notification_inactivity_borrow_clock_value(
827 notif, clock_class);
828 break;
829 default:
830 BT_LOGF("Unexpected notification type at this point: "
831 "type=%d", bt_notification_get_type(notif));
832 abort();
833 }
834
835 if (!clock_value) {
836 BT_LOGE("Cannot get notification's clock value for clock class: "
837 "clock-class-addr=%p, clock-class-name=\"%s\"",
838 clock_class, cc_name);
839 goto error;
840 }
841
842 ret = bt_clock_value_get_value_ns_from_epoch(clock_value, ts_ns);
843 if (ret) {
844 BT_LOGE("Cannot get nanoseconds from Epoch of clock value: "
845 "clock-value-addr=%p", clock_value);
846 goto error;
847 }
848
849 goto end;
850
851 error:
852 ret = -1;
853
854 end:
855 if (ret == 0) {
856 BT_LOGV("Found notification's timestamp: "
857 "muxer-notif-iter-addr=%p, notif-addr=%p, "
858 "last-returned-ts=%" PRId64 ", ts=%" PRId64,
859 muxer_notif_iter, notif, last_returned_ts_ns,
860 *ts_ns);
861 }
862
863 return ret;
864 }
865
866 /*
867 * This function finds the youngest available notification amongst the
868 * non-ended upstream notification iterators and returns the upstream
869 * notification iterator which has it, or
870 * BT_NOTIFICATION_ITERATOR_STATUS_END if there's no available
871 * notification.
872 *
873 * This function does NOT:
874 *
875 * * Update any upstream notification iterator.
876 * * Check for newly connected ports.
877 * * Check the upstream notification iterators to retry.
878 *
879 * On sucess, this function sets *muxer_upstream_notif_iter to the
880 * upstream notification iterator of which the current notification is
881 * the youngest, and sets *ts_ns to its time.
882 */
883 static
884 enum bt_notification_iterator_status
885 muxer_notif_iter_youngest_upstream_notif_iter(
886 struct muxer_comp *muxer_comp,
887 struct muxer_notif_iter *muxer_notif_iter,
888 struct muxer_upstream_notif_iter **muxer_upstream_notif_iter,
889 int64_t *ts_ns)
890 {
891 size_t i;
892 int ret;
893 int64_t youngest_ts_ns = INT64_MAX;
894 enum bt_notification_iterator_status status =
895 BT_NOTIFICATION_ITERATOR_STATUS_OK;
896
897 BT_ASSERT(muxer_comp);
898 BT_ASSERT(muxer_notif_iter);
899 BT_ASSERT(muxer_upstream_notif_iter);
900 *muxer_upstream_notif_iter = NULL;
901
902 for (i = 0; i < muxer_notif_iter->muxer_upstream_notif_iters->len; i++) {
903 struct bt_notification *notif;
904 struct muxer_upstream_notif_iter *cur_muxer_upstream_notif_iter =
905 g_ptr_array_index(muxer_notif_iter->muxer_upstream_notif_iters, i);
906 int64_t notif_ts_ns;
907
908 if (!cur_muxer_upstream_notif_iter->notif_iter) {
909 /* This upstream notification iterator is ended */
910 BT_LOGV("Skipping ended upstream notification iterator: "
911 "muxer-upstream-notif-iter-wrap-addr=%p",
912 cur_muxer_upstream_notif_iter);
913 continue;
914 }
915
916 BT_ASSERT(cur_muxer_upstream_notif_iter->notifs->length > 0);
917 notif = g_queue_peek_head(cur_muxer_upstream_notif_iter->notifs);
918 BT_ASSERT(notif);
919 ret = get_notif_ts_ns(muxer_comp, muxer_notif_iter, notif,
920 muxer_notif_iter->last_returned_ts_ns, &notif_ts_ns);
921 if (ret) {
922 /* get_notif_ts_ns() logs errors */
923 *muxer_upstream_notif_iter = NULL;
924 status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
925 goto end;
926 }
927
928 if (notif_ts_ns <= youngest_ts_ns) {
929 *muxer_upstream_notif_iter =
930 cur_muxer_upstream_notif_iter;
931 youngest_ts_ns = notif_ts_ns;
932 *ts_ns = youngest_ts_ns;
933 }
934 }
935
936 if (!*muxer_upstream_notif_iter) {
937 status = BT_NOTIFICATION_ITERATOR_STATUS_END;
938 *ts_ns = INT64_MIN;
939 }
940
941 end:
942 return status;
943 }
944
945 static
946 enum bt_notification_iterator_status validate_muxer_upstream_notif_iter(
947 struct muxer_upstream_notif_iter *muxer_upstream_notif_iter)
948 {
949 enum bt_notification_iterator_status status =
950 BT_NOTIFICATION_ITERATOR_STATUS_OK;
951
952 BT_LOGV("Validating muxer's upstream notification iterator wrapper: "
953 "muxer-upstream-notif-iter-wrap-addr=%p",
954 muxer_upstream_notif_iter);
955
956 if (muxer_upstream_notif_iter->notifs->length > 0 ||
957 !muxer_upstream_notif_iter->notif_iter) {
958 BT_LOGV("Already valid or not considered: "
959 "queue-len=%u, upstream-notif-iter-addr=%p",
960 muxer_upstream_notif_iter->notifs->length,
961 muxer_upstream_notif_iter->notif_iter);
962 goto end;
963 }
964
965 /* muxer_upstream_notif_iter_next() logs details/errors */
966 status = muxer_upstream_notif_iter_next(muxer_upstream_notif_iter);
967
968 end:
969 return status;
970 }
971
972 static
973 enum bt_notification_iterator_status validate_muxer_upstream_notif_iters(
974 struct muxer_notif_iter *muxer_notif_iter)
975 {
976 enum bt_notification_iterator_status status =
977 BT_NOTIFICATION_ITERATOR_STATUS_OK;
978 size_t i;
979
980 BT_LOGV("Validating muxer's upstream notification iterator wrappers: "
981 "muxer-notif-iter-addr=%p", muxer_notif_iter);
982
983 for (i = 0; i < muxer_notif_iter->muxer_upstream_notif_iters->len; i++) {
984 struct muxer_upstream_notif_iter *muxer_upstream_notif_iter =
985 g_ptr_array_index(
986 muxer_notif_iter->muxer_upstream_notif_iters,
987 i);
988
989 status = validate_muxer_upstream_notif_iter(
990 muxer_upstream_notif_iter);
991 if (status != BT_NOTIFICATION_ITERATOR_STATUS_OK) {
992 if (status < 0) {
993 BT_LOGE("Cannot validate muxer's upstream notification iterator wrapper: "
994 "muxer-notif-iter-addr=%p, "
995 "muxer-upstream-notif-iter-wrap-addr=%p",
996 muxer_notif_iter,
997 muxer_upstream_notif_iter);
998 } else {
999 BT_LOGV("Cannot validate muxer's upstream notification iterator wrapper: "
1000 "muxer-notif-iter-addr=%p, "
1001 "muxer-upstream-notif-iter-wrap-addr=%p",
1002 muxer_notif_iter,
1003 muxer_upstream_notif_iter);
1004 }
1005
1006 goto end;
1007 }
1008
1009 /*
1010 * Remove this muxer upstream notification iterator
1011 * if it's ended or canceled.
1012 */
1013 if (!muxer_upstream_notif_iter->notif_iter) {
1014 /*
1015 * Use g_ptr_array_remove_fast() because the
1016 * order of those elements is not important.
1017 */
1018 BT_LOGV("Removing muxer's upstream notification iterator wrapper: ended or canceled: "
1019 "muxer-notif-iter-addr=%p, "
1020 "muxer-upstream-notif-iter-wrap-addr=%p",
1021 muxer_notif_iter, muxer_upstream_notif_iter);
1022 g_ptr_array_remove_index_fast(
1023 muxer_notif_iter->muxer_upstream_notif_iters,
1024 i);
1025 i--;
1026 }
1027 }
1028
1029 end:
1030 return status;
1031 }
1032
1033 static inline
1034 enum bt_notification_iterator_status muxer_notif_iter_do_next_one(
1035 struct muxer_comp *muxer_comp,
1036 struct muxer_notif_iter *muxer_notif_iter,
1037 struct bt_notification **notif)
1038 {
1039 enum bt_notification_iterator_status status =
1040 BT_NOTIFICATION_ITERATOR_STATUS_OK;
1041 struct muxer_upstream_notif_iter *muxer_upstream_notif_iter = NULL;
1042 int64_t next_return_ts;
1043
1044 while (true) {
1045 int ret = muxer_notif_iter_handle_newly_connected_ports(
1046 muxer_notif_iter);
1047
1048 if (ret) {
1049 BT_LOGE("Cannot handle newly connected input ports for muxer's notification iterator: "
1050 "muxer-comp-addr=%p, muxer-notif-iter-addr=%p, "
1051 "ret=%d",
1052 muxer_comp, muxer_notif_iter, ret);
1053 status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
1054 goto end;
1055 }
1056
1057 status = validate_muxer_upstream_notif_iters(muxer_notif_iter);
1058 if (status != BT_NOTIFICATION_ITERATOR_STATUS_OK) {
1059 /* validate_muxer_upstream_notif_iters() logs details */
1060 goto end;
1061 }
1062
1063 /*
1064 * At this point, we know that all the existing upstream
1065 * notification iterators are valid. However the
1066 * operations to validate them (during
1067 * validate_muxer_upstream_notif_iters()) may have
1068 * connected new ports. If no ports were connected
1069 * during this operation, exit the loop.
1070 */
1071 if (!muxer_notif_iter->newly_connected_priv_ports) {
1072 BT_LOGV("Not breaking this loop: muxer's notification iterator still has newly connected input ports to handle: "
1073 "muxer-comp-addr=%p", muxer_comp);
1074 break;
1075 }
1076 }
1077
1078 BT_ASSERT(!muxer_notif_iter->newly_connected_priv_ports);
1079
1080 /*
1081 * At this point we know that all the existing upstream
1082 * notification iterators are valid. We can find the one,
1083 * amongst those, of which the current notification is the
1084 * youngest.
1085 */
1086 status = muxer_notif_iter_youngest_upstream_notif_iter(muxer_comp,
1087 muxer_notif_iter, &muxer_upstream_notif_iter,
1088 &next_return_ts);
1089 if (status < 0 || status == BT_NOTIFICATION_ITERATOR_STATUS_END ||
1090 status == BT_NOTIFICATION_ITERATOR_STATUS_CANCELED) {
1091 if (status < 0) {
1092 BT_LOGE("Cannot find the youngest upstream notification iterator wrapper: "
1093 "status=%s",
1094 bt_notification_iterator_status_string(status));
1095 } else {
1096 BT_LOGV("Cannot find the youngest upstream notification iterator wrapper: "
1097 "status=%s",
1098 bt_notification_iterator_status_string(status));
1099 }
1100
1101 goto end;
1102 }
1103
1104 if (next_return_ts < muxer_notif_iter->last_returned_ts_ns) {
1105 BT_LOGE("Youngest upstream notification iterator wrapper's timestamp is less than muxer's notification iterator's last returned timestamp: "
1106 "muxer-notif-iter-addr=%p, ts=%" PRId64 ", "
1107 "last-returned-ts=%" PRId64,
1108 muxer_notif_iter, next_return_ts,
1109 muxer_notif_iter->last_returned_ts_ns);
1110 status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
1111 goto end;
1112 }
1113
1114 BT_LOGV("Found youngest upstream notification iterator wrapper: "
1115 "muxer-notif-iter-addr=%p, "
1116 "muxer-upstream-notif-iter-wrap-addr=%p, "
1117 "ts=%" PRId64,
1118 muxer_notif_iter, muxer_upstream_notif_iter, next_return_ts);
1119 BT_ASSERT(status == BT_NOTIFICATION_ITERATOR_STATUS_OK);
1120 BT_ASSERT(muxer_upstream_notif_iter);
1121
1122 /*
1123 * Consume from the queue's head: other side
1124 * (muxer_upstream_notif_iter_next()) writes to the tail.
1125 */
1126 *notif = g_queue_pop_head(muxer_upstream_notif_iter->notifs);
1127 BT_ASSERT(*notif);
1128 muxer_notif_iter->last_returned_ts_ns = next_return_ts;
1129
1130 end:
1131 return status;
1132 }
1133
1134 static
1135 enum bt_notification_iterator_status muxer_notif_iter_do_next(
1136 struct muxer_comp *muxer_comp,
1137 struct muxer_notif_iter *muxer_notif_iter,
1138 bt_notification_array notifs, uint64_t capacity,
1139 uint64_t *count)
1140 {
1141 enum bt_notification_iterator_status status =
1142 BT_NOTIFICATION_ITERATOR_STATUS_OK;
1143 uint64_t i = 0;
1144
1145 while (i < capacity && status == BT_NOTIFICATION_ITERATOR_STATUS_OK) {
1146 status = muxer_notif_iter_do_next_one(muxer_comp,
1147 muxer_notif_iter, &notifs[i]);
1148 if (status == BT_NOTIFICATION_ITERATOR_STATUS_OK) {
1149 i++;
1150 }
1151 }
1152
1153 if (i > 0) {
1154 /*
1155 * Even if muxer_notif_iter_do_next_one() returned
1156 * something else than
1157 * BT_NOTIFICATION_ITERATOR_STATUS_OK, we accumulated
1158 * notification objects in the output notification
1159 * array, so we need to return
1160 * BT_NOTIFICATION_ITERATOR_STATUS_OK so that they are
1161 * transfered to downstream. This other status occurs
1162 * again the next time muxer_notif_iter_do_next() is
1163 * called, possibly without any accumulated
1164 * notification, in which case we'll return it.
1165 */
1166 *count = i;
1167 status = BT_NOTIFICATION_ITERATOR_STATUS_OK;
1168 }
1169
1170 return status;
1171 }
1172
1173 static
1174 void destroy_muxer_notif_iter(struct muxer_notif_iter *muxer_notif_iter)
1175 {
1176 if (!muxer_notif_iter) {
1177 return;
1178 }
1179
1180 BT_LOGD("Destroying muxer component's notification iterator: "
1181 "muxer-notif-iter-addr=%p", muxer_notif_iter);
1182
1183 if (muxer_notif_iter->muxer_upstream_notif_iters) {
1184 BT_LOGD_STR("Destroying muxer's upstream notification iterator wrappers.");
1185 g_ptr_array_free(
1186 muxer_notif_iter->muxer_upstream_notif_iters, TRUE);
1187 }
1188
1189 g_list_free(muxer_notif_iter->newly_connected_priv_ports);
1190 g_free(muxer_notif_iter);
1191 }
1192
1193 static
1194 int muxer_notif_iter_init_newly_connected_ports(struct muxer_comp *muxer_comp,
1195 struct muxer_notif_iter *muxer_notif_iter)
1196 {
1197 struct bt_component *comp;
1198 int64_t count;
1199 int64_t i;
1200 int ret = 0;
1201
1202 /*
1203 * Add the connected input ports to this muxer notification
1204 * iterator's list of newly connected ports. They will be
1205 * handled by muxer_notif_iter_handle_newly_connected_ports().
1206 */
1207 comp = bt_component_borrow_from_private(muxer_comp->priv_comp);
1208 BT_ASSERT(comp);
1209 count = bt_component_filter_get_input_port_count(comp);
1210 if (count < 0) {
1211 BT_LOGD("No input port to initialize for muxer component's notification iterator: "
1212 "muxer-comp-addr=%p, muxer-notif-iter-addr=%p",
1213 muxer_comp, muxer_notif_iter);
1214 goto end;
1215 }
1216
1217 for (i = 0; i < count; i++) {
1218 struct bt_private_port *priv_port =
1219 bt_private_component_filter_get_input_private_port_by_index(
1220 muxer_comp->priv_comp, i);
1221 struct bt_port *port;
1222
1223 BT_ASSERT(priv_port);
1224 port = bt_port_borrow_from_private(priv_port);
1225 BT_ASSERT(port);
1226
1227 if (!bt_port_is_connected(port)) {
1228 BT_LOGD("Skipping input port: not connected: "
1229 "muxer-comp-addr=%p, port-addr=%p, port-name\"%s\"",
1230 muxer_comp, port, bt_port_get_name(port));
1231 bt_put(priv_port);
1232 continue;
1233 }
1234
1235 bt_put(priv_port);
1236 muxer_notif_iter->newly_connected_priv_ports =
1237 g_list_append(
1238 muxer_notif_iter->newly_connected_priv_ports,
1239 priv_port);
1240 if (!muxer_notif_iter->newly_connected_priv_ports) {
1241 BT_LOGE("Cannot append port to muxer's notification iterator list of newly connected input ports: "
1242 "port-addr=%p, port-name=\"%s\", "
1243 "muxer-notif-iter-addr=%p", port,
1244 bt_port_get_name(port), muxer_notif_iter);
1245 ret = -1;
1246 goto end;
1247 }
1248
1249 BT_LOGD("Appended port to muxer's notification iterator list of newly connected input ports: "
1250 "port-addr=%p, port-name=\"%s\", "
1251 "muxer-notif-iter-addr=%p", port,
1252 bt_port_get_name(port), muxer_notif_iter);
1253 }
1254
1255 end:
1256 return ret;
1257 }
1258
1259 BT_HIDDEN
1260 enum bt_notification_iterator_status muxer_notif_iter_init(
1261 struct bt_private_connection_private_notification_iterator *priv_notif_iter,
1262 struct bt_private_port *output_priv_port)
1263 {
1264 struct muxer_comp *muxer_comp = NULL;
1265 struct muxer_notif_iter *muxer_notif_iter = NULL;
1266 struct bt_private_component *priv_comp = NULL;
1267 enum bt_notification_iterator_status status =
1268 BT_NOTIFICATION_ITERATOR_STATUS_OK;
1269 int ret;
1270
1271 priv_comp = bt_private_connection_private_notification_iterator_get_private_component(
1272 priv_notif_iter);
1273 BT_ASSERT(priv_comp);
1274 muxer_comp = bt_private_component_get_user_data(priv_comp);
1275 BT_ASSERT(muxer_comp);
1276 BT_LOGD("Initializing muxer component's notification iterator: "
1277 "comp-addr=%p, muxer-comp-addr=%p, notif-iter-addr=%p",
1278 priv_comp, muxer_comp, priv_notif_iter);
1279
1280 if (muxer_comp->initializing_muxer_notif_iter) {
1281 /*
1282 * Weird, unhandled situation detected: downstream
1283 * creates a muxer notification iterator while creating
1284 * another muxer notification iterator (same component).
1285 */
1286 BT_LOGE("Recursive initialization of muxer component's notification iterator: "
1287 "comp-addr=%p, muxer-comp-addr=%p, notif-iter-addr=%p",
1288 priv_comp, muxer_comp, priv_notif_iter);
1289 goto error;
1290 }
1291
1292 muxer_comp->initializing_muxer_notif_iter = true;
1293 muxer_notif_iter = g_new0(struct muxer_notif_iter, 1);
1294 if (!muxer_notif_iter) {
1295 BT_LOGE_STR("Failed to allocate one muxer component's notification iterator.");
1296 goto error;
1297 }
1298
1299 muxer_notif_iter->last_returned_ts_ns = INT64_MIN;
1300 muxer_notif_iter->muxer_upstream_notif_iters =
1301 g_ptr_array_new_with_free_func(
1302 (GDestroyNotify) destroy_muxer_upstream_notif_iter);
1303 if (!muxer_notif_iter->muxer_upstream_notif_iters) {
1304 BT_LOGE_STR("Failed to allocate a GPtrArray.");
1305 goto error;
1306 }
1307
1308 /*
1309 * Add the muxer notification iterator to the component's array
1310 * of muxer notification iterators here because
1311 * muxer_notif_iter_init_newly_connected_ports() can cause
1312 * muxer_port_connected() to be called, which adds the newly
1313 * connected port to each muxer notification iterator's list of
1314 * newly connected ports.
1315 */
1316 g_ptr_array_add(muxer_comp->muxer_notif_iters, muxer_notif_iter);
1317 ret = muxer_notif_iter_init_newly_connected_ports(muxer_comp,
1318 muxer_notif_iter);
1319 if (ret) {
1320 BT_LOGE("Cannot initialize newly connected input ports for muxer component's notification iterator: "
1321 "comp-addr=%p, muxer-comp-addr=%p, "
1322 "muxer-notif-iter-addr=%p, notif-iter-addr=%p, ret=%d",
1323 priv_comp, muxer_comp, muxer_notif_iter,
1324 priv_notif_iter, ret);
1325 goto error;
1326 }
1327
1328 ret = bt_private_connection_private_notification_iterator_set_user_data(priv_notif_iter,
1329 muxer_notif_iter);
1330 BT_ASSERT(ret == 0);
1331 BT_LOGD("Initialized muxer component's notification iterator: "
1332 "comp-addr=%p, muxer-comp-addr=%p, muxer-notif-iter-addr=%p, "
1333 "notif-iter-addr=%p",
1334 priv_comp, muxer_comp, muxer_notif_iter, priv_notif_iter);
1335 goto end;
1336
1337 error:
1338 if (g_ptr_array_index(muxer_comp->muxer_notif_iters,
1339 muxer_comp->muxer_notif_iters->len - 1) == muxer_notif_iter) {
1340 g_ptr_array_remove_index(muxer_comp->muxer_notif_iters,
1341 muxer_comp->muxer_notif_iters->len - 1);
1342 }
1343
1344 destroy_muxer_notif_iter(muxer_notif_iter);
1345 ret = bt_private_connection_private_notification_iterator_set_user_data(priv_notif_iter,
1346 NULL);
1347 BT_ASSERT(ret == 0);
1348 status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
1349
1350 end:
1351 muxer_comp->initializing_muxer_notif_iter = false;
1352 bt_put(priv_comp);
1353 return status;
1354 }
1355
1356 BT_HIDDEN
1357 void muxer_notif_iter_finalize(
1358 struct bt_private_connection_private_notification_iterator *priv_notif_iter)
1359 {
1360 struct muxer_notif_iter *muxer_notif_iter =
1361 bt_private_connection_private_notification_iterator_get_user_data(priv_notif_iter);
1362 struct bt_private_component *priv_comp = NULL;
1363 struct muxer_comp *muxer_comp = NULL;
1364
1365 priv_comp = bt_private_connection_private_notification_iterator_get_private_component(
1366 priv_notif_iter);
1367 BT_ASSERT(priv_comp);
1368 muxer_comp = bt_private_component_get_user_data(priv_comp);
1369 BT_LOGD("Finalizing muxer component's notification iterator: "
1370 "comp-addr=%p, muxer-comp-addr=%p, muxer-notif-iter-addr=%p, "
1371 "notif-iter-addr=%p",
1372 priv_comp, muxer_comp, muxer_notif_iter, priv_notif_iter);
1373
1374 if (muxer_comp) {
1375 (void) g_ptr_array_remove_fast(muxer_comp->muxer_notif_iters,
1376 muxer_notif_iter);
1377 destroy_muxer_notif_iter(muxer_notif_iter);
1378 }
1379
1380 bt_put(priv_comp);
1381 }
1382
1383 BT_HIDDEN
1384 enum bt_notification_iterator_status muxer_notif_iter_next(
1385 struct bt_private_connection_private_notification_iterator *priv_notif_iter,
1386 bt_notification_array notifs, uint64_t capacity,
1387 uint64_t *count)
1388 {
1389 enum bt_notification_iterator_status status;
1390 struct muxer_notif_iter *muxer_notif_iter =
1391 bt_private_connection_private_notification_iterator_get_user_data(priv_notif_iter);
1392 struct bt_private_component *priv_comp = NULL;
1393 struct muxer_comp *muxer_comp = NULL;
1394
1395 BT_ASSERT(muxer_notif_iter);
1396 priv_comp = bt_private_connection_private_notification_iterator_get_private_component(
1397 priv_notif_iter);
1398 BT_ASSERT(priv_comp);
1399 muxer_comp = bt_private_component_get_user_data(priv_comp);
1400 BT_ASSERT(muxer_comp);
1401 BT_LOGV("Muxer component's notification iterator's \"next\" method called: "
1402 "comp-addr=%p, muxer-comp-addr=%p, muxer-notif-iter-addr=%p, "
1403 "notif-iter-addr=%p",
1404 priv_comp, muxer_comp, muxer_notif_iter, priv_notif_iter);
1405
1406 /* Are we in an error state set elsewhere? */
1407 if (unlikely(muxer_comp->error)) {
1408 BT_LOGE("Muxer component is already in an error state: returning BT_NOTIFICATION_ITERATOR_STATUS_ERROR: "
1409 "comp-addr=%p, muxer-comp-addr=%p, muxer-notif-iter-addr=%p, "
1410 "notif-iter-addr=%p",
1411 priv_comp, muxer_comp, muxer_notif_iter, priv_notif_iter);
1412 status = BT_NOTIFICATION_ITERATOR_STATUS_ERROR;
1413 goto end;
1414 }
1415
1416 status = muxer_notif_iter_do_next(muxer_comp, muxer_notif_iter,
1417 notifs, capacity, count);
1418 if (status < 0) {
1419 BT_LOGE("Cannot get next notification: "
1420 "comp-addr=%p, muxer-comp-addr=%p, muxer-notif-iter-addr=%p, "
1421 "notif-iter-addr=%p, status=%s",
1422 priv_comp, muxer_comp, muxer_notif_iter, priv_notif_iter,
1423 bt_notification_iterator_status_string(status));
1424 } else {
1425 BT_LOGV("Returning from muxer component's notification iterator's \"next\" method: "
1426 "status=%s",
1427 bt_notification_iterator_status_string(status));
1428 }
1429
1430 end:
1431 bt_put(priv_comp);
1432 return status;
1433 }
1434
1435 BT_HIDDEN
1436 void muxer_port_connected(
1437 struct bt_private_component *priv_comp,
1438 struct bt_private_port *self_private_port,
1439 struct bt_port *other_port)
1440 {
1441 struct bt_port *self_port =
1442 bt_port_borrow_from_private(self_private_port);
1443 struct muxer_comp *muxer_comp =
1444 bt_private_component_get_user_data(priv_comp);
1445 size_t i;
1446 int ret;
1447
1448 BT_ASSERT(self_port);
1449 BT_ASSERT(muxer_comp);
1450 BT_LOGD("Port connected: "
1451 "comp-addr=%p, muxer-comp-addr=%p, "
1452 "port-addr=%p, port-name=\"%s\", "
1453 "other-port-addr=%p, other-port-name=\"%s\"",
1454 priv_comp, muxer_comp, self_port, bt_port_get_name(self_port),
1455 other_port, bt_port_get_name(other_port));
1456
1457 if (bt_port_get_type(self_port) == BT_PORT_TYPE_OUTPUT) {
1458 goto end;
1459 }
1460
1461 for (i = 0; i < muxer_comp->muxer_notif_iters->len; i++) {
1462 struct muxer_notif_iter *muxer_notif_iter =
1463 g_ptr_array_index(muxer_comp->muxer_notif_iters, i);
1464
1465 /*
1466 * Add this port to the list of newly connected ports
1467 * for this muxer notification iterator. We append at
1468 * the end of this list while
1469 * muxer_notif_iter_handle_newly_connected_ports()
1470 * removes the nodes from the beginning.
1471 */
1472 muxer_notif_iter->newly_connected_priv_ports =
1473 g_list_append(
1474 muxer_notif_iter->newly_connected_priv_ports,
1475 self_private_port);
1476 if (!muxer_notif_iter->newly_connected_priv_ports) {
1477 BT_LOGE("Cannot append port to muxer's notification iterator list of newly connected input ports: "
1478 "port-addr=%p, port-name=\"%s\", "
1479 "muxer-notif-iter-addr=%p", self_port,
1480 bt_port_get_name(self_port), muxer_notif_iter);
1481 muxer_comp->error = true;
1482 goto end;
1483 }
1484
1485 BT_LOGD("Appended port to muxer's notification iterator list of newly connected input ports: "
1486 "port-addr=%p, port-name=\"%s\", "
1487 "muxer-notif-iter-addr=%p", self_port,
1488 bt_port_get_name(self_port), muxer_notif_iter);
1489 }
1490
1491 /* One less available input port */
1492 muxer_comp->available_input_ports--;
1493 ret = ensure_available_input_port(priv_comp);
1494 if (ret) {
1495 /*
1496 * Only way to report an error later since this
1497 * method does not return anything.
1498 */
1499 BT_LOGE("Cannot ensure that at least one muxer component's input port is available: "
1500 "muxer-comp-addr=%p, status=%s",
1501 muxer_comp, bt_component_status_string(ret));
1502 muxer_comp->error = true;
1503 goto end;
1504 }
1505
1506 end:
1507 return;
1508 }
1509
1510 BT_HIDDEN
1511 void muxer_port_disconnected(struct bt_private_component *priv_comp,
1512 struct bt_private_port *priv_port)
1513 {
1514 struct bt_port *port = bt_port_borrow_from_private(priv_port);
1515 struct muxer_comp *muxer_comp =
1516 bt_private_component_get_user_data(priv_comp);
1517
1518 BT_ASSERT(port);
1519 BT_ASSERT(muxer_comp);
1520 BT_LOGD("Port disconnected: "
1521 "comp-addr=%p, muxer-comp-addr=%p, port-addr=%p, "
1522 "port-name=\"%s\"", priv_comp, muxer_comp,
1523 port, bt_port_get_name(port));
1524
1525 /*
1526 * There's nothing special to do when a port is disconnected
1527 * because this component deals with upstream notification
1528 * iterators which were already created thanks to connected
1529 * ports. The fact that the port is disconnected does not cancel
1530 * the upstream notification iterators created using its
1531 * connection: they still exist, even if the connection is dead.
1532 * The only way to remove an upstream notification iterator is
1533 * for its "next" operation to return
1534 * BT_NOTIFICATION_ITERATOR_STATUS_END.
1535 */
1536 if (bt_port_get_type(port) == BT_PORT_TYPE_INPUT) {
1537 /* One more available input port */
1538 muxer_comp->available_input_ports++;
1539 BT_LOGD("Leaving disconnected input port available for future connections: "
1540 "comp-addr=%p, muxer-comp-addr=%p, port-addr=%p, "
1541 "port-name=\"%s\", avail-input-port-count=%zu",
1542 priv_comp, muxer_comp, port, bt_port_get_name(port),
1543 muxer_comp->available_input_ports);
1544 }
1545 }
This page took 0.068961 seconds and 4 git commands to generate.