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