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