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