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