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