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