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