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