lib: validate monotonicity of messages from upstream component
[babeltrace.git] / src / lib / graph / iterator.c
1 /*
2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
3 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24 #define BT_LOG_TAG "LIB/MSG-ITER"
25 #include "lib/logging.h"
26
27 #include "compat/compiler.h"
28 #include "compat/glib.h"
29 #include "lib/trace-ir/clock-class.h"
30 #include "lib/trace-ir/clock-snapshot.h"
31 #include <babeltrace2/trace-ir/field.h>
32 #include <babeltrace2/trace-ir/event-const.h>
33 #include "lib/trace-ir/event.h"
34 #include <babeltrace2/trace-ir/packet-const.h>
35 #include "lib/trace-ir/packet.h"
36 #include "lib/trace-ir/stream.h"
37 #include <babeltrace2/graph/connection-const.h>
38 #include <babeltrace2/graph/component-const.h>
39 #include <babeltrace2/graph/component-sink-const.h>
40 #include <babeltrace2/graph/message-const.h>
41 #include <babeltrace2/graph/message-iterator-const.h>
42 #include <babeltrace2/graph/self-component-port-input-message-iterator.h>
43 #include <babeltrace2/graph/port-output-message-iterator.h>
44 #include <babeltrace2/graph/message-event-const.h>
45 #include <babeltrace2/graph/message-message-iterator-inactivity-const.h>
46 #include <babeltrace2/graph/message-packet-beginning.h>
47 #include <babeltrace2/graph/message-packet-beginning-const.h>
48 #include <babeltrace2/graph/message-packet-end-const.h>
49 #include <babeltrace2/graph/message-stream-activity-beginning.h>
50 #include <babeltrace2/graph/message-stream-activity-beginning-const.h>
51 #include <babeltrace2/graph/message-stream-activity-end-const.h>
52 #include <babeltrace2/graph/message-stream-beginning.h>
53 #include <babeltrace2/graph/message-stream-beginning-const.h>
54 #include <babeltrace2/graph/message-stream-end-const.h>
55 #include <babeltrace2/graph/port-const.h>
56 #include <babeltrace2/graph/graph.h>
57 #include <babeltrace2/graph/graph-const.h>
58 #include <babeltrace2/types.h>
59 #include "common/assert.h"
60 #include "lib/assert-pre.h"
61 #include <stdint.h>
62 #include <inttypes.h>
63 #include <stdlib.h>
64
65 #include "component-class.h"
66 #include "component-class-sink-colander.h"
67 #include "component.h"
68 #include "component-sink.h"
69 #include "component-source.h"
70 #include "connection.h"
71 #include "graph.h"
72 #include "message/discarded-items.h"
73 #include "message/event.h"
74 #include "message/iterator.h"
75 #include "message/message.h"
76 #include "message/message-iterator-inactivity.h"
77 #include "message/stream.h"
78 #include "message/packet.h"
79 #include "message/stream-activity.h"
80
81 /*
82 * TODO: Use graph's state (number of active iterators, etc.) and
83 * possibly system specifications to make a better guess than this.
84 */
85 #define MSG_BATCH_SIZE 15
86
87 #define BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(_iter) \
88 BT_ASSERT_PRE((_iter)->state == BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE || \
89 (_iter)->state == BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED || \
90 (_iter)->state == BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN || \
91 (_iter)->state == BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR, \
92 "Message iterator is in the wrong state: %!+i", _iter)
93
94 static inline
95 void set_self_comp_port_input_msg_iterator_state(
96 struct bt_self_component_port_input_message_iterator *iterator,
97 enum bt_self_component_port_input_message_iterator_state state)
98 {
99 BT_ASSERT(iterator);
100 BT_LIB_LOGD("Updating message iterator's state: new-state=%s",
101 bt_self_component_port_input_message_iterator_state_string(state));
102 iterator->state = state;
103 }
104
105 static
106 void destroy_base_message_iterator(struct bt_object *obj)
107 {
108 struct bt_message_iterator *iterator = (void *) obj;
109
110 BT_ASSERT(iterator);
111
112 if (iterator->msgs) {
113 g_ptr_array_free(iterator->msgs, TRUE);
114 iterator->msgs = NULL;
115 }
116
117 g_free(iterator);
118 }
119
120 static
121 void bt_self_component_port_input_message_iterator_destroy(struct bt_object *obj)
122 {
123 struct bt_self_component_port_input_message_iterator *iterator;
124
125 BT_ASSERT(obj);
126
127 /*
128 * The message iterator's reference count is 0 if we're
129 * here. Increment it to avoid a double-destroy (possibly
130 * infinitely recursive). This could happen for example if the
131 * message iterator's finalization function does
132 * bt_object_get_ref() (or anything that causes
133 * bt_object_get_ref() to be called) on itself (ref. count goes
134 * from 0 to 1), and then bt_object_put_ref(): the reference
135 * count would go from 1 to 0 again and this function would be
136 * called again.
137 */
138 obj->ref_count++;
139 iterator = (void *) obj;
140 BT_LIB_LOGI("Destroying self component input port message iterator object: "
141 "%!+i", iterator);
142 bt_self_component_port_input_message_iterator_try_finalize(iterator);
143
144 if (iterator->connection) {
145 /*
146 * Remove ourself from the originating connection so
147 * that it does not try to finalize a dangling pointer
148 * later.
149 */
150 bt_connection_remove_iterator(iterator->connection, iterator);
151 iterator->connection = NULL;
152 }
153
154 if (iterator->auto_seek.msgs) {
155 while (!g_queue_is_empty(iterator->auto_seek.msgs)) {
156 bt_object_put_no_null_check(
157 g_queue_pop_tail(iterator->auto_seek.msgs));
158 }
159
160 g_queue_free(iterator->auto_seek.msgs);
161 iterator->auto_seek.msgs = NULL;
162 }
163
164 destroy_base_message_iterator(obj);
165 }
166
167 BT_HIDDEN
168 void bt_self_component_port_input_message_iterator_try_finalize(
169 struct bt_self_component_port_input_message_iterator *iterator)
170 {
171 typedef void (*method_t)(void *);
172
173 struct bt_component_class *comp_class = NULL;
174 method_t method = NULL;
175
176 BT_ASSERT(iterator);
177
178 switch (iterator->state) {
179 case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED:
180 /* Skip user finalization if user initialization failed */
181 BT_LIB_LOGD("Not finalizing non-initialized message iterator: "
182 "%!+i", iterator);
183 goto end;
184 case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED:
185 /* Already finalized */
186 BT_LIB_LOGD("Not finalizing message iterator: already finalized: "
187 "%!+i", iterator);
188 goto end;
189 case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZING:
190 /* Already finalized */
191 BT_LIB_LOGF("Message iterator is already being finalized: "
192 "%!+i", iterator);
193 abort();
194 default:
195 break;
196 }
197
198 BT_LIB_LOGD("Finalizing message iterator: %!+i", iterator);
199 set_self_comp_port_input_msg_iterator_state(iterator,
200 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZING);
201 BT_ASSERT(iterator->upstream_component);
202 comp_class = iterator->upstream_component->class;
203
204 /* Call user-defined destroy method */
205 switch (comp_class->type) {
206 case BT_COMPONENT_CLASS_TYPE_SOURCE:
207 {
208 struct bt_component_class_source *src_comp_cls =
209 (void *) comp_class;
210
211 method = (method_t) src_comp_cls->methods.msg_iter_finalize;
212 break;
213 }
214 case BT_COMPONENT_CLASS_TYPE_FILTER:
215 {
216 struct bt_component_class_filter *flt_comp_cls =
217 (void *) comp_class;
218
219 method = (method_t) flt_comp_cls->methods.msg_iter_finalize;
220 break;
221 }
222 default:
223 /* Unreachable */
224 abort();
225 }
226
227 if (method) {
228 BT_LIB_LOGD("Calling user's finalization method: %!+i",
229 iterator);
230 method(iterator);
231 }
232
233 iterator->upstream_component = NULL;
234 iterator->upstream_port = NULL;
235 set_self_comp_port_input_msg_iterator_state(iterator,
236 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED);
237 BT_LIB_LOGD("Finalized message iterator: %!+i", iterator);
238
239 end:
240 return;
241 }
242
243 BT_HIDDEN
244 void bt_self_component_port_input_message_iterator_set_connection(
245 struct bt_self_component_port_input_message_iterator *iterator,
246 struct bt_connection *connection)
247 {
248 BT_ASSERT(iterator);
249 iterator->connection = connection;
250 BT_LIB_LOGI("Set message iterator's connection: "
251 "%![iter-]+i, %![conn-]+x", iterator, connection);
252 }
253
254 static
255 int init_message_iterator(struct bt_message_iterator *iterator,
256 enum bt_message_iterator_type type,
257 bt_object_release_func destroy)
258 {
259 int ret = 0;
260
261 bt_object_init_shared(&iterator->base, destroy);
262 iterator->type = type;
263 iterator->msgs = g_ptr_array_new();
264 if (!iterator->msgs) {
265 BT_LOGE_STR("Failed to allocate a GPtrArray.");
266 ret = -1;
267 goto end;
268 }
269
270 g_ptr_array_set_size(iterator->msgs, MSG_BATCH_SIZE);
271
272 end:
273 return ret;
274 }
275
276 static
277 bt_bool can_seek_ns_from_origin_true(
278 struct bt_self_component_port_input_message_iterator *iterator,
279 int64_t ns_from_origin)
280 {
281 return BT_TRUE;
282 }
283
284 static
285 bt_bool can_seek_beginning_true(
286 struct bt_self_component_port_input_message_iterator *iterator)
287 {
288 return BT_TRUE;
289 }
290
291 static
292 struct bt_self_component_port_input_message_iterator *
293 bt_self_component_port_input_message_iterator_create_initial(
294 struct bt_component *upstream_comp,
295 struct bt_port *upstream_port)
296 {
297 int ret;
298 struct bt_self_component_port_input_message_iterator *iterator = NULL;
299
300 BT_ASSERT(upstream_comp);
301 BT_ASSERT(upstream_port);
302 BT_ASSERT(bt_port_is_connected(upstream_port));
303 BT_LIB_LOGI("Creating initial message iterator on self component input port: "
304 "%![up-comp-]+c, %![up-port-]+p", upstream_comp, upstream_port);
305 BT_ASSERT(bt_component_get_class_type(upstream_comp) ==
306 BT_COMPONENT_CLASS_TYPE_SOURCE ||
307 bt_component_get_class_type(upstream_comp) ==
308 BT_COMPONENT_CLASS_TYPE_FILTER);
309 iterator = g_new0(
310 struct bt_self_component_port_input_message_iterator, 1);
311 if (!iterator) {
312 BT_LOGE_STR("Failed to allocate one self component input port "
313 "message iterator.");
314 goto end;
315 }
316
317 ret = init_message_iterator((void *) iterator,
318 BT_MESSAGE_ITERATOR_TYPE_SELF_COMPONENT_PORT_INPUT,
319 bt_self_component_port_input_message_iterator_destroy);
320 if (ret) {
321 /* init_message_iterator() logs errors */
322 BT_OBJECT_PUT_REF_AND_RESET(iterator);
323 goto end;
324 }
325
326 iterator->last_ns_from_origin = INT64_MIN;
327
328 iterator->auto_seek.msgs = g_queue_new();
329 if (!iterator->auto_seek.msgs) {
330 BT_LOGE_STR("Failed to allocate a GQueue.");
331 ret = -1;
332 goto end;
333 }
334
335 iterator->upstream_component = upstream_comp;
336 iterator->upstream_port = upstream_port;
337 iterator->connection = iterator->upstream_port->connection;
338 iterator->graph = bt_component_borrow_graph(upstream_comp);
339 set_self_comp_port_input_msg_iterator_state(iterator,
340 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED);
341
342 switch (iterator->upstream_component->class->type) {
343 case BT_COMPONENT_CLASS_TYPE_SOURCE:
344 {
345 struct bt_component_class_source *src_comp_cls =
346 (void *) iterator->upstream_component->class;
347
348 iterator->methods.next =
349 (bt_self_component_port_input_message_iterator_next_method)
350 src_comp_cls->methods.msg_iter_next;
351 iterator->methods.seek_ns_from_origin =
352 (bt_self_component_port_input_message_iterator_seek_ns_from_origin_method)
353 src_comp_cls->methods.msg_iter_seek_ns_from_origin;
354 iterator->methods.seek_beginning =
355 (bt_self_component_port_input_message_iterator_seek_beginning_method)
356 src_comp_cls->methods.msg_iter_seek_beginning;
357 iterator->methods.can_seek_ns_from_origin =
358 (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method)
359 src_comp_cls->methods.msg_iter_can_seek_ns_from_origin;
360 iterator->methods.can_seek_beginning =
361 (bt_self_component_port_input_message_iterator_can_seek_beginning_method)
362 src_comp_cls->methods.msg_iter_can_seek_beginning;
363 break;
364 }
365 case BT_COMPONENT_CLASS_TYPE_FILTER:
366 {
367 struct bt_component_class_filter *flt_comp_cls =
368 (void *) iterator->upstream_component->class;
369
370 iterator->methods.next =
371 (bt_self_component_port_input_message_iterator_next_method)
372 flt_comp_cls->methods.msg_iter_next;
373 iterator->methods.seek_ns_from_origin =
374 (bt_self_component_port_input_message_iterator_seek_ns_from_origin_method)
375 flt_comp_cls->methods.msg_iter_seek_ns_from_origin;
376 iterator->methods.seek_beginning =
377 (bt_self_component_port_input_message_iterator_seek_beginning_method)
378 flt_comp_cls->methods.msg_iter_seek_beginning;
379 iterator->methods.can_seek_ns_from_origin =
380 (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method)
381 flt_comp_cls->methods.msg_iter_can_seek_ns_from_origin;
382 iterator->methods.can_seek_beginning =
383 (bt_self_component_port_input_message_iterator_can_seek_beginning_method)
384 flt_comp_cls->methods.msg_iter_can_seek_beginning;
385 break;
386 }
387 default:
388 abort();
389 }
390
391 if (iterator->methods.seek_ns_from_origin &&
392 !iterator->methods.can_seek_ns_from_origin) {
393 iterator->methods.can_seek_ns_from_origin =
394 (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method)
395 can_seek_ns_from_origin_true;
396 }
397
398 if (iterator->methods.seek_beginning &&
399 !iterator->methods.can_seek_beginning) {
400 iterator->methods.can_seek_beginning =
401 (bt_self_component_port_input_message_iterator_seek_beginning_method)
402 can_seek_beginning_true;
403 }
404
405 BT_LIB_LOGI("Created initial message iterator on self component input port: "
406 "%![up-port-]+p, %![up-comp-]+c, %![iter-]+i",
407 upstream_port, upstream_comp, iterator);
408
409 end:
410 return iterator;
411 }
412
413 struct bt_self_component_port_input_message_iterator *
414 bt_self_component_port_input_message_iterator_create(
415 struct bt_self_component_port_input *self_port)
416 {
417 typedef enum bt_self_message_iterator_status (*init_method_t)(
418 void *, void *, void *);
419
420 init_method_t init_method = NULL;
421 struct bt_self_component_port_input_message_iterator *iterator =
422 NULL;
423 struct bt_port *port = (void *) self_port;
424 struct bt_port *upstream_port;
425 struct bt_component *comp;
426 struct bt_component *upstream_comp;
427 struct bt_component_class *upstream_comp_cls;
428
429 BT_ASSERT_PRE_NON_NULL(port, "Port");
430 comp = bt_port_borrow_component_inline(port);
431 BT_ASSERT_PRE(bt_port_is_connected(port),
432 "Port is not connected: %![port-]+p", port);
433 BT_ASSERT_PRE(comp, "Port is not part of a component: %![port-]+p",
434 port);
435 BT_ASSERT_PRE(!bt_component_graph_is_canceled(comp),
436 "Port's component's graph is canceled: "
437 "%![port-]+p, %![comp-]+c", port, comp);
438 BT_ASSERT(port->connection);
439 upstream_port = port->connection->upstream_port;
440 BT_ASSERT(upstream_port);
441 upstream_comp = bt_port_borrow_component_inline(upstream_port);
442 BT_ASSERT(upstream_comp);
443 BT_ASSERT_PRE(
444 bt_component_borrow_graph(upstream_comp)->config_state !=
445 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
446 "Graph is not configured: %!+g",
447 bt_component_borrow_graph(upstream_comp));
448 upstream_comp_cls = upstream_comp->class;
449 BT_ASSERT(upstream_comp->class->type ==
450 BT_COMPONENT_CLASS_TYPE_SOURCE ||
451 upstream_comp->class->type ==
452 BT_COMPONENT_CLASS_TYPE_FILTER);
453 iterator = bt_self_component_port_input_message_iterator_create_initial(
454 upstream_comp, upstream_port);
455 if (!iterator) {
456 BT_LOGW_STR("Cannot create self component input port "
457 "message iterator.");
458 goto end;
459 }
460
461 switch (upstream_comp_cls->type) {
462 case BT_COMPONENT_CLASS_TYPE_SOURCE:
463 {
464 struct bt_component_class_source *src_comp_cls =
465 (void *) upstream_comp_cls;
466
467 init_method =
468 (init_method_t) src_comp_cls->methods.msg_iter_init;
469 break;
470 }
471 case BT_COMPONENT_CLASS_TYPE_FILTER:
472 {
473 struct bt_component_class_filter *flt_comp_cls =
474 (void *) upstream_comp_cls;
475
476 init_method =
477 (init_method_t) flt_comp_cls->methods.msg_iter_init;
478 break;
479 }
480 default:
481 /* Unreachable */
482 abort();
483 }
484
485 if (init_method) {
486 int iter_status;
487
488 BT_LIB_LOGD("Calling user's initialization method: %!+i", iterator);
489 iter_status = init_method(iterator, upstream_comp,
490 upstream_port);
491 BT_LOGD("User method returned: status=%s",
492 bt_message_iterator_status_string(iter_status));
493 if (iter_status != BT_MESSAGE_ITERATOR_STATUS_OK) {
494 BT_LOGW_STR("Initialization method failed.");
495 BT_OBJECT_PUT_REF_AND_RESET(iterator);
496 goto end;
497 }
498 }
499
500 set_self_comp_port_input_msg_iterator_state(iterator,
501 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE);
502 g_ptr_array_add(port->connection->iterators, iterator);
503 BT_LIB_LOGI("Created message iterator on self component input port: "
504 "%![up-port-]+p, %![up-comp-]+c, %![iter-]+i",
505 upstream_port, upstream_comp, iterator);
506
507 end:
508 return iterator;
509 }
510
511 void *bt_self_message_iterator_get_data(
512 const struct bt_self_message_iterator *self_iterator)
513 {
514 struct bt_self_component_port_input_message_iterator *iterator =
515 (void *) self_iterator;
516
517 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
518 return iterator->user_data;
519 }
520
521 void bt_self_message_iterator_set_data(
522 struct bt_self_message_iterator *self_iterator, void *data)
523 {
524 struct bt_self_component_port_input_message_iterator *iterator =
525 (void *) self_iterator;
526
527 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
528 iterator->user_data = data;
529 BT_LIB_LOGD("Set message iterator's user data: "
530 "%!+i, user-data-addr=%p", iterator, data);
531 }
532
533 /*
534 * Validate that the default clock snapshot in `msg` doesn't make us go back in
535 * time.
536 */
537
538 BT_ASSERT_PRE_FUNC
539 static
540 bool clock_snapshots_are_monotonic_one(
541 struct bt_self_component_port_input_message_iterator *iterator,
542 const bt_message *msg)
543 {
544 const struct bt_clock_snapshot *clock_snapshot = NULL;
545 bt_message_type message_type = bt_message_get_type(msg);
546 int64_t ns_from_origin;
547 enum bt_clock_snapshot_status clock_snapshot_status;
548
549 /*
550 * The default is true: if we can't figure out the clock snapshot
551 * (or there is none), assume it is fine.
552 */
553 bool result = true;
554
555 switch (message_type) {
556 case BT_MESSAGE_TYPE_EVENT:
557 {
558 struct bt_message_event *event_msg = (struct bt_message_event *) msg;
559 clock_snapshot = event_msg->default_cs;
560 break;
561 }
562 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY:
563 {
564 struct bt_message_message_iterator_inactivity *inactivity_msg =
565 (struct bt_message_message_iterator_inactivity *) msg;
566 clock_snapshot = inactivity_msg->default_cs;
567 break;
568 }
569 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
570 case BT_MESSAGE_TYPE_PACKET_END:
571 {
572 struct bt_message_packet *packet_msg = (struct bt_message_packet *) msg;
573 clock_snapshot = packet_msg->default_cs;
574 break;
575 }
576 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING:
577 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_END:
578 {
579 struct bt_message_stream_activity *str_act_msg =
580 (struct bt_message_stream_activity *) msg;
581
582 if (str_act_msg->default_cs_state == BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_KNOWN) {
583 clock_snapshot = str_act_msg->default_cs;
584 }
585 break;
586 }
587 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
588 case BT_MESSAGE_TYPE_STREAM_END:
589 /* These messages don't have clock snapshots. */
590 goto end;
591 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
592 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
593 {
594 struct bt_message_discarded_items *discarded_msg =
595 (struct bt_message_discarded_items *) msg;
596
597 clock_snapshot = discarded_msg->default_begin_cs;
598 break;
599 }
600 }
601
602 if (!clock_snapshot) {
603 goto end;
604 }
605
606 clock_snapshot_status = bt_clock_snapshot_get_ns_from_origin(clock_snapshot, &ns_from_origin);
607 if (clock_snapshot_status != BT_CLOCK_SNAPSHOT_STATUS_OK) {
608 goto end;
609 }
610
611 result = ns_from_origin >= iterator->last_ns_from_origin;
612 iterator->last_ns_from_origin = ns_from_origin;
613 end:
614 return result;
615 }
616
617 BT_ASSERT_PRE_FUNC
618 static
619 bool clock_snapshots_are_monotonic(
620 struct bt_self_component_port_input_message_iterator *iterator,
621 bt_message_array_const msgs, uint64_t msg_count)
622 {
623 uint64_t i;
624 bool result;
625
626 for (i = 0; i < msg_count; i++) {
627 if (!clock_snapshots_are_monotonic_one(iterator, msgs[i])) {
628 result = false;
629 goto end;
630 }
631 }
632
633 result = true;
634
635 end:
636 return result;
637 }
638
639 /*
640 * When a new stream begins, verify that the clock class tied to this
641 * stream is compatible with what we've seen before.
642 */
643
644 BT_ASSERT_PRE_FUNC
645 static
646 bool clock_classes_are_compatible_one(struct bt_self_component_port_input_message_iterator *iterator,
647 const struct bt_message *msg)
648 {
649 enum bt_message_type message_type = bt_message_get_type(msg);
650 bool result;
651
652 if (message_type == BT_MESSAGE_TYPE_STREAM_BEGINNING) {
653 const struct bt_message_stream *stream_msg = (struct bt_message_stream *) msg;
654 const struct bt_clock_class *clock_class = stream_msg->stream->class->default_clock_class;
655 bt_uuid clock_class_uuid = NULL;
656
657 if (clock_class) {
658 clock_class_uuid = bt_clock_class_get_uuid(clock_class);
659 }
660
661 switch (iterator->clock_expectation.type) {
662 case CLOCK_EXPECTATION_UNSET:
663 /*
664 * This is the first time we see a message with a clock
665 * snapshot: record the properties of that clock, against
666 * which we'll compare the clock properties of the following
667 * messages.
668 */
669
670 if (!clock_class) {
671 iterator->clock_expectation.type = CLOCK_EXPECTATION_NONE;
672 } else if (bt_clock_class_origin_is_unix_epoch(clock_class)) {
673 iterator->clock_expectation.type = CLOCK_EXPECTATION_ORIGIN_UNIX;
674 } else if (clock_class_uuid) {
675 iterator->clock_expectation.type = CLOCK_EXPECTATION_ORIGIN_OTHER_UUID;
676 memcpy(iterator->clock_expectation.uuid, clock_class_uuid, BABELTRACE_UUID_LEN);
677 } else {
678 iterator->clock_expectation.type = CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID;
679 }
680 break;
681
682 case CLOCK_EXPECTATION_NONE:
683 if (clock_class) {
684 BT_ASSERT_PRE_MSG("Expecting no clock class, got one: %![cc-]+K",
685 clock_class);
686 result = false;
687 goto end;
688 }
689
690 break;
691
692 case CLOCK_EXPECTATION_ORIGIN_UNIX:
693 if (!clock_class) {
694 BT_ASSERT_PRE_MSG("Expecting a clock class, got none.");
695 result = false;
696 goto end;
697 }
698
699 if (!bt_clock_class_origin_is_unix_epoch(clock_class)) {
700 BT_ASSERT_PRE_MSG("Expecting a clock class with Unix epoch origin: %![cc-]+K",
701 clock_class);
702 result = false;
703 goto end;
704 }
705 break;
706
707 case CLOCK_EXPECTATION_ORIGIN_OTHER_UUID:
708 if (!clock_class) {
709 BT_ASSERT_PRE_MSG("Expecting a clock class, got none.");
710 result = false;
711 goto end;
712 }
713
714 if (bt_clock_class_origin_is_unix_epoch(clock_class)) {
715 BT_ASSERT_PRE_MSG("Expecting a clock class without Unix epoch origin: %![cc-]+K",
716 clock_class);
717 result = false;
718 goto end;
719 }
720
721 if (!clock_class_uuid) {
722 BT_ASSERT_PRE_MSG("Expecting a clock class with UUID: %![cc-]+K",
723 clock_class);
724 result = false;
725 goto end;
726 }
727
728 if (bt_uuid_compare(iterator->clock_expectation.uuid, clock_class_uuid)) {
729 BT_ASSERT_PRE_MSG("Expecting a clock class with UUID, got one "
730 "with a different UUID: %![cc-]+K, expected-uuid=%!u",
731 clock_class, iterator->clock_expectation.uuid);
732 result = false;
733 goto end;
734 }
735 break;
736
737 case CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID:
738 if (!clock_class) {
739 BT_ASSERT_PRE_MSG("Expecting a clock class, got none.");
740 result = false;
741 goto end;
742 }
743
744 if (bt_clock_class_origin_is_unix_epoch(clock_class)) {
745 BT_ASSERT_PRE_MSG("Expecting a clock class without Unix epoch origin: %![cc-]+K",
746 clock_class);
747 result = false;
748 goto end;
749 }
750
751 if (clock_class_uuid) {
752 BT_ASSERT_PRE_MSG("Expecting a clock class without UUID: %![cc-]+K",
753 clock_class);
754 result = false;
755 goto end;
756 }
757 break;
758 }
759 }
760
761 result = true;
762
763 end:
764 return result;
765 }
766
767 BT_ASSERT_PRE_FUNC
768 static
769 bool clock_classes_are_compatible(
770 struct bt_self_component_port_input_message_iterator *iterator,
771 bt_message_array_const msgs, uint64_t msg_count)
772 {
773 uint64_t i;
774 bool result;
775
776 for (i = 0; i < msg_count; i++) {
777 if (!clock_classes_are_compatible_one(iterator, msgs[i])) {
778 result = false;
779 goto end;
780 }
781 }
782
783 result = true;
784
785 end:
786 return result;
787 }
788
789 /*
790 * Call the `next` method of the iterator. Do some validation on the returned
791 * messages.
792 */
793
794 static
795 bt_message_iterator_status call_iterator_next_method(
796 struct bt_self_component_port_input_message_iterator *iterator,
797 bt_message_array_const msgs, uint64_t capacity, uint64_t *user_count)
798 {
799 bt_message_iterator_status status;
800
801 BT_ASSERT(iterator->methods.next);
802 BT_LOGD_STR("Calling user's \"next\" method.");
803
804 status = iterator->methods.next(iterator, msgs, capacity, user_count);
805
806 if (status == BT_MESSAGE_ITERATOR_STATUS_OK) {
807 BT_ASSERT_PRE(clock_classes_are_compatible(iterator, msgs, *user_count),
808 "Clocks are not compatible");
809 BT_ASSERT_PRE(clock_snapshots_are_monotonic(iterator, msgs, *user_count),
810 "Clock snapshots are not monotonic");
811 }
812
813 return status;
814 }
815
816 enum bt_message_iterator_status
817 bt_self_component_port_input_message_iterator_next(
818 struct bt_self_component_port_input_message_iterator *iterator,
819 bt_message_array_const *msgs, uint64_t *user_count)
820 {
821 int status = BT_MESSAGE_ITERATOR_STATUS_OK;
822
823 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
824 BT_ASSERT_PRE_NON_NULL(msgs, "Message array (output)");
825 BT_ASSERT_PRE_NON_NULL(user_count, "Message count (output)");
826 BT_ASSERT_PRE(iterator->state ==
827 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE,
828 "Message iterator's \"next\" called, but "
829 "message iterator is in the wrong state: %!+i", iterator);
830 BT_ASSERT(iterator->upstream_component);
831 BT_ASSERT(iterator->upstream_component->class);
832 BT_ASSERT_PRE(
833 bt_component_borrow_graph(iterator->upstream_component)->config_state !=
834 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
835 "Graph is not configured: %!+g",
836 bt_component_borrow_graph(iterator->upstream_component));
837 BT_LIB_LOGD("Getting next self component input port "
838 "message iterator's messages: %!+i, batch-size=%u",
839 iterator, MSG_BATCH_SIZE);
840
841 /*
842 * Call the user's "next" method to get the next messages
843 * and status.
844 */
845 *user_count = 0;
846 status = call_iterator_next_method(iterator,
847 (void *) iterator->base.msgs->pdata, MSG_BATCH_SIZE,
848 user_count);
849 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64,
850 bt_message_iterator_status_string(status), *user_count);
851 if (status < 0) {
852 BT_LOGW_STR("User method failed.");
853 goto end;
854 }
855
856 /*
857 * There is no way that this iterator could have been finalized
858 * during its "next" method, as the only way to do this is to
859 * put the last iterator's reference, and this can only be done
860 * by its downstream owner.
861 *
862 * For the same reason, there is no way that this iterator could
863 * have seeked (cannot seek a self message iterator).
864 */
865 BT_ASSERT(iterator->state ==
866 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE);
867
868 switch (status) {
869 case BT_MESSAGE_ITERATOR_STATUS_OK:
870 BT_ASSERT_PRE(*user_count <= MSG_BATCH_SIZE,
871 "Invalid returned message count: greater than "
872 "batch size: count=%" PRIu64 ", batch-size=%u",
873 *user_count, MSG_BATCH_SIZE);
874 *msgs = (void *) iterator->base.msgs->pdata;
875 break;
876 case BT_MESSAGE_ITERATOR_STATUS_AGAIN:
877 goto end;
878 case BT_MESSAGE_ITERATOR_STATUS_END:
879 set_self_comp_port_input_msg_iterator_state(iterator,
880 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED);
881 goto end;
882 default:
883 /* Unknown non-error status */
884 abort();
885 }
886
887 end:
888 return status;
889 }
890
891 enum bt_message_iterator_status bt_port_output_message_iterator_next(
892 struct bt_port_output_message_iterator *iterator,
893 bt_message_array_const *msgs_to_user,
894 uint64_t *count_to_user)
895 {
896 enum bt_message_iterator_status status;
897 enum bt_graph_status graph_status;
898
899 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
900 BT_ASSERT_PRE_NON_NULL(msgs_to_user, "Message array (output)");
901 BT_ASSERT_PRE_NON_NULL(count_to_user, "Message count (output)");
902 BT_LIB_LOGD("Getting next output port message iterator's messages: "
903 "%!+i", iterator);
904 graph_status = bt_graph_consume_sink_no_check(iterator->graph,
905 iterator->colander);
906 switch (graph_status) {
907 case BT_GRAPH_STATUS_CANCELED:
908 case BT_GRAPH_STATUS_AGAIN:
909 case BT_GRAPH_STATUS_END:
910 case BT_GRAPH_STATUS_NOMEM:
911 status = (int) graph_status;
912 break;
913 case BT_GRAPH_STATUS_OK:
914 status = BT_MESSAGE_ITERATOR_STATUS_OK;
915
916 /*
917 * On success, the colander sink moves the messages
918 * to this iterator's array and sets this iterator's
919 * message count: move them to the user.
920 */
921 *msgs_to_user = (void *) iterator->base.msgs->pdata;
922 *count_to_user = iterator->count;
923 break;
924 default:
925 /* Other errors */
926 status = BT_MESSAGE_ITERATOR_STATUS_ERROR;
927 }
928
929 return status;
930 }
931
932 struct bt_component *
933 bt_self_component_port_input_message_iterator_borrow_component(
934 struct bt_self_component_port_input_message_iterator *iterator)
935 {
936 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
937 return iterator->upstream_component;
938 }
939
940 const struct bt_component *
941 bt_self_component_port_input_message_iterator_borrow_component_const(
942 const struct bt_self_component_port_input_message_iterator *iterator)
943 {
944 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
945 return iterator->upstream_component;
946 }
947
948 struct bt_self_component *bt_self_message_iterator_borrow_component(
949 struct bt_self_message_iterator *self_iterator)
950 {
951 struct bt_self_component_port_input_message_iterator *iterator =
952 (void *) self_iterator;
953
954 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
955 return (void *) iterator->upstream_component;
956 }
957
958 struct bt_self_port_output *bt_self_message_iterator_borrow_port(
959 struct bt_self_message_iterator *self_iterator)
960 {
961 struct bt_self_component_port_input_message_iterator *iterator =
962 (void *) self_iterator;
963
964 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
965 return (void *) iterator->upstream_port;
966 }
967
968 static
969 void bt_port_output_message_iterator_destroy(struct bt_object *obj)
970 {
971 struct bt_port_output_message_iterator *iterator = (void *) obj;
972
973 BT_LIB_LOGI("Destroying output port message iterator object: %!+i",
974 iterator);
975 BT_LOGD_STR("Putting graph.");
976 BT_OBJECT_PUT_REF_AND_RESET(iterator->graph);
977 BT_LOGD_STR("Putting colander sink component.");
978 BT_OBJECT_PUT_REF_AND_RESET(iterator->colander);
979 destroy_base_message_iterator(obj);
980 }
981
982 struct bt_port_output_message_iterator *
983 bt_port_output_message_iterator_create(struct bt_graph *graph,
984 const struct bt_port_output *output_port)
985 {
986 struct bt_port_output_message_iterator *iterator = NULL;
987 struct bt_component_class_sink *colander_comp_cls = NULL;
988 struct bt_component *output_port_comp = NULL;
989 struct bt_component_sink *colander_comp;
990 enum bt_graph_status graph_status;
991 struct bt_port_input *colander_in_port = NULL;
992 struct bt_component_class_sink_colander_data colander_data;
993 int ret;
994
995 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
996 BT_ASSERT_PRE_NON_NULL(output_port, "Output port");
997 output_port_comp = bt_port_borrow_component_inline(
998 (const void *) output_port);
999 BT_ASSERT_PRE(output_port_comp,
1000 "Output port has no component: %!+p", output_port);
1001 BT_ASSERT_PRE(bt_component_borrow_graph(output_port_comp) ==
1002 (void *) graph,
1003 "Output port is not part of graph: %![graph-]+g, %![port-]+p",
1004 graph, output_port);
1005 BT_ASSERT_PRE(!graph->has_sink,
1006 "Graph already has a sink component: %![graph-]+g");
1007
1008 /* Create message iterator */
1009 BT_LIB_LOGI("Creating message iterator on output port: "
1010 "%![port-]+p, %![comp-]+c", output_port, output_port_comp);
1011 iterator = g_new0(struct bt_port_output_message_iterator, 1);
1012 if (!iterator) {
1013 BT_LOGE_STR("Failed to allocate one output port message iterator.");
1014 goto error;
1015 }
1016
1017 ret = init_message_iterator((void *) iterator,
1018 BT_MESSAGE_ITERATOR_TYPE_PORT_OUTPUT,
1019 bt_port_output_message_iterator_destroy);
1020 if (ret) {
1021 /* init_message_iterator() logs errors */
1022 BT_OBJECT_PUT_REF_AND_RESET(iterator);
1023 goto end;
1024 }
1025
1026 /* Create colander component */
1027 colander_comp_cls = bt_component_class_sink_colander_get();
1028 if (!colander_comp_cls) {
1029 BT_LOGW("Cannot get colander sink component class.");
1030 goto error;
1031 }
1032
1033 iterator->graph = graph;
1034 bt_object_get_no_null_check(iterator->graph);
1035 colander_data.msgs = (void *) iterator->base.msgs->pdata;
1036 colander_data.count_addr = &iterator->count;
1037
1038 /*
1039 * Hope that nobody uses this very unique name.
1040 *
1041 * We pass `BT_LOGGING_LEVEL_NONE` but the colander component
1042 * class module does not use this level anyway since it belongs
1043 * to the library.
1044 */
1045 graph_status =
1046 bt_graph_add_sink_component_with_init_method_data(
1047 (void *) graph, colander_comp_cls,
1048 "colander-36ac3409-b1a8-4d60-ab1f-4fdf341a8fb1",
1049 NULL, &colander_data, BT_LOGGING_LEVEL_NONE,
1050 (void *) &iterator->colander);
1051 if (graph_status != BT_GRAPH_STATUS_OK) {
1052 BT_LIB_LOGW("Cannot add colander sink component to graph: "
1053 "%1[graph-]+g, status=%s", graph,
1054 bt_graph_status_string(graph_status));
1055 goto error;
1056 }
1057
1058 /*
1059 * Connect provided output port to the colander component's
1060 * input port.
1061 */
1062 colander_in_port =
1063 (void *) bt_component_sink_borrow_input_port_by_index_const(
1064 (void *) iterator->colander, 0);
1065 BT_ASSERT(colander_in_port);
1066 graph_status = bt_graph_connect_ports(graph,
1067 output_port, colander_in_port, NULL);
1068 if (graph_status != BT_GRAPH_STATUS_OK) {
1069 BT_LIB_LOGW("Cannot add colander sink component to graph: "
1070 "%![graph-]+g, %![comp-]+c, status=%s", graph,
1071 iterator->colander,
1072 bt_graph_status_string(graph_status));
1073 goto error;
1074 }
1075
1076 /*
1077 * At this point everything went fine. Make the graph
1078 * nonconsumable forever so that only this message iterator
1079 * can consume (thanks to bt_graph_consume_sink_no_check()).
1080 * This avoids leaking the message created by the colander
1081 * sink and moved to the message iterator's message
1082 * member.
1083 */
1084 bt_graph_set_can_consume(iterator->graph, false);
1085
1086 /* Also set the graph as being configured. */
1087 graph_status = bt_graph_configure(graph);
1088 if (graph_status != BT_GRAPH_STATUS_OK) {
1089 BT_LIB_LOGW("Cannot configure graph after having added colander: "
1090 "%![graph-]+g, status=%s", graph,
1091 bt_graph_status_string(graph_status));
1092 goto error;
1093 }
1094 goto end;
1095
1096 error:
1097 if (iterator && iterator->graph && iterator->colander) {
1098 int ret;
1099
1100 /* Remove created colander component from graph if any */
1101 colander_comp = iterator->colander;
1102 BT_OBJECT_PUT_REF_AND_RESET(iterator->colander);
1103
1104 /*
1105 * At this point the colander component's reference
1106 * count is 0 because iterator->colander was the only
1107 * owner. We also know that it is not connected because
1108 * this is the last operation before this function
1109 * succeeds.
1110 *
1111 * Since we honor the preconditions here,
1112 * bt_graph_remove_unconnected_component() always
1113 * succeeds.
1114 */
1115 ret = bt_graph_remove_unconnected_component(iterator->graph,
1116 (void *) colander_comp);
1117 BT_ASSERT(ret == 0);
1118 }
1119
1120 BT_OBJECT_PUT_REF_AND_RESET(iterator);
1121
1122 end:
1123 bt_object_put_ref(colander_comp_cls);
1124 return (void *) iterator;
1125 }
1126
1127 bt_bool bt_self_component_port_input_message_iterator_can_seek_ns_from_origin(
1128 struct bt_self_component_port_input_message_iterator *iterator,
1129 int64_t ns_from_origin)
1130 {
1131 bt_bool can = BT_FALSE;
1132
1133 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
1134 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator);
1135 BT_ASSERT_PRE(
1136 bt_component_borrow_graph(iterator->upstream_component)->config_state !=
1137 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
1138 "Graph is not configured: %!+g",
1139 bt_component_borrow_graph(iterator->upstream_component));
1140
1141 if (iterator->methods.can_seek_ns_from_origin) {
1142 can = iterator->methods.can_seek_ns_from_origin(iterator,
1143 ns_from_origin);
1144 goto end;
1145 }
1146
1147 /*
1148 * Automatic seeking fall back: if we can seek to the beginning,
1149 * then we can automatically seek to any message.
1150 */
1151 if (iterator->methods.can_seek_beginning) {
1152 can = iterator->methods.can_seek_beginning(iterator);
1153 }
1154
1155 end:
1156 return can;
1157 }
1158
1159 bt_bool bt_self_component_port_input_message_iterator_can_seek_beginning(
1160 struct bt_self_component_port_input_message_iterator *iterator)
1161 {
1162 bt_bool can = BT_FALSE;
1163
1164 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
1165 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator);
1166 BT_ASSERT_PRE(
1167 bt_component_borrow_graph(iterator->upstream_component)->config_state !=
1168 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
1169 "Graph is not configured: %!+g",
1170 bt_component_borrow_graph(iterator->upstream_component));
1171
1172 if (iterator->methods.can_seek_beginning) {
1173 can = iterator->methods.can_seek_beginning(iterator);
1174 }
1175
1176 return can;
1177 }
1178
1179 static inline
1180 void set_iterator_state_after_seeking(
1181 struct bt_self_component_port_input_message_iterator *iterator,
1182 enum bt_message_iterator_status status)
1183 {
1184 enum bt_self_component_port_input_message_iterator_state new_state = 0;
1185
1186 /* Set iterator's state depending on seeking status */
1187 switch (status) {
1188 case BT_MESSAGE_ITERATOR_STATUS_OK:
1189 new_state = BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE;
1190 break;
1191 case BT_MESSAGE_ITERATOR_STATUS_AGAIN:
1192 new_state = BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN;
1193 break;
1194 case BT_MESSAGE_ITERATOR_STATUS_ERROR:
1195 case BT_MESSAGE_ITERATOR_STATUS_NOMEM:
1196 new_state = BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR;
1197 break;
1198 case BT_MESSAGE_ITERATOR_STATUS_END:
1199 new_state = BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED;
1200 break;
1201 default:
1202 abort();
1203 }
1204
1205 set_self_comp_port_input_msg_iterator_state(iterator, new_state);
1206 }
1207
1208 static
1209 void reset_iterator_expectations(
1210 struct bt_self_component_port_input_message_iterator *iterator)
1211 {
1212 iterator->last_ns_from_origin = INT64_MIN;
1213 iterator->clock_expectation.type = CLOCK_EXPECTATION_UNSET;
1214 }
1215
1216 enum bt_message_iterator_status
1217 bt_self_component_port_input_message_iterator_seek_beginning(
1218 struct bt_self_component_port_input_message_iterator *iterator)
1219 {
1220 int status;
1221
1222 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
1223 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator);
1224 BT_ASSERT_PRE(
1225 bt_component_borrow_graph(iterator->upstream_component)->config_state !=
1226 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
1227 "Graph is not configured: %!+g",
1228 bt_component_borrow_graph(iterator->upstream_component));
1229 BT_ASSERT_PRE(
1230 bt_self_component_port_input_message_iterator_can_seek_beginning(
1231 iterator),
1232 "Message iterator cannot seek beginning: %!+i", iterator);
1233
1234 /*
1235 * We are seeking, reset our expectations about how the following
1236 * messages should look like.
1237 */
1238 reset_iterator_expectations(iterator);
1239
1240 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i", iterator);
1241 set_self_comp_port_input_msg_iterator_state(iterator,
1242 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_SEEKING);
1243 status = iterator->methods.seek_beginning(iterator);
1244 BT_LOGD("User method returned: status=%s",
1245 bt_message_iterator_status_string(status));
1246 BT_ASSERT_PRE(status == BT_MESSAGE_ITERATOR_STATUS_OK ||
1247 status == BT_MESSAGE_ITERATOR_STATUS_ERROR ||
1248 status == BT_MESSAGE_ITERATOR_STATUS_NOMEM ||
1249 status == BT_MESSAGE_ITERATOR_STATUS_AGAIN,
1250 "Unexpected status: %![iter-]+i, status=%s",
1251 iterator, bt_common_self_message_iterator_status_string(status));
1252 set_iterator_state_after_seeking(iterator, status);
1253 return status;
1254 }
1255
1256
1257 /*
1258 * Structure used to record the state of a given stream during the fast-forward
1259 * phase of an auto-seek.
1260 */
1261 struct auto_seek_stream_state {
1262 /*
1263 * Value representing which step of this timeline we are at.
1264 *
1265 * time --->
1266 * [SB] 1 [SAB] 2 [PB] 3 [PE] 2 [SAE] 1 [SE]
1267 *
1268 * At each point in the timeline, the messages we need to replicate are:
1269 *
1270 * 1: Stream beginning
1271 * 2: Stream beginning, stream activity beginning
1272 * 3: Stream beginning, stream activity beginning, packet beginning
1273 *
1274 * Before "Stream beginning" and after "Stream end", we don't need to
1275 * replicate anything as the stream doesn't exist.
1276 */
1277 enum {
1278 AUTO_SEEK_STREAM_STATE_STREAM_BEGAN,
1279 AUTO_SEEK_STREAM_STATE_STREAM_ACTIVITY_BEGAN,
1280 AUTO_SEEK_STREAM_STATE_PACKET_BEGAN,
1281 } state;
1282
1283 /*
1284 * If `state` is AUTO_SEEK_STREAM_STATE_PACKET_BEGAN, the packet we are
1285 * in. This is a weak reference, since the packet will always be
1286 * alive by the time we use it.
1287 */
1288 struct bt_packet *packet;
1289 };
1290
1291 static
1292 struct auto_seek_stream_state *create_auto_seek_stream_state(void)
1293 {
1294 return g_new0(struct auto_seek_stream_state, 1);
1295 }
1296
1297 static
1298 void destroy_auto_seek_stream_state(void *ptr)
1299 {
1300 g_free(ptr);
1301 }
1302
1303 static
1304 GHashTable *create_auto_seek_stream_states(void)
1305 {
1306 return g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL,
1307 destroy_auto_seek_stream_state);
1308 }
1309
1310 static
1311 void destroy_auto_seek_stream_states(GHashTable *stream_states)
1312 {
1313 g_hash_table_destroy(stream_states);
1314 }
1315
1316 /*
1317 * Handle one message while we are in the fast-forward phase of an auto-seek.
1318 *
1319 * Sets `*got_first` to true if the message's timestamp is greater or equal to
1320 * `ns_from_origin`. In other words, if this is the first message after our
1321 * seek point.
1322 *
1323 * `stream_states` is an hash table of `bt_stream *` (weak reference) to
1324 * `struct auto_seek_stream_state` used to keep the state of each stream
1325 * during the fast-forward.
1326 */
1327
1328 static inline
1329 enum bt_message_iterator_status auto_seek_handle_message(
1330 struct bt_self_component_port_input_message_iterator *iterator,
1331 int64_t ns_from_origin, const struct bt_message *msg,
1332 bool *got_first, GHashTable *stream_states)
1333 {
1334 enum bt_message_iterator_status status = BT_MESSAGE_ITERATOR_STATUS_OK;
1335 int64_t msg_ns_from_origin;
1336 const struct bt_clock_snapshot *clk_snapshot = NULL;
1337 int ret;
1338
1339 BT_ASSERT(msg);
1340 BT_ASSERT(got_first);
1341
1342 switch (msg->type) {
1343 case BT_MESSAGE_TYPE_EVENT:
1344 {
1345 const struct bt_message_event *event_msg =
1346 (const void *) msg;
1347
1348 clk_snapshot = event_msg->default_cs;
1349 BT_ASSERT_PRE(clk_snapshot,
1350 "Event message has no default clock snapshot: %!+n",
1351 event_msg);
1352 break;
1353 }
1354 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY:
1355 {
1356 const struct bt_message_message_iterator_inactivity *inactivity_msg =
1357 (const void *) msg;
1358
1359 clk_snapshot = inactivity_msg->default_cs;
1360 BT_ASSERT(clk_snapshot);
1361 break;
1362 }
1363 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
1364 case BT_MESSAGE_TYPE_PACKET_END:
1365 {
1366 const struct bt_message_packet *packet_msg =
1367 (const void *) msg;
1368
1369 clk_snapshot = packet_msg->default_cs;
1370 BT_ASSERT_PRE(clk_snapshot,
1371 "Packet message has no default clock snapshot: %!+n",
1372 packet_msg);
1373 break;
1374 }
1375 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
1376 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
1377 {
1378 struct bt_message_discarded_items *msg_disc_items =
1379 (void *) msg;
1380
1381 BT_ASSERT_PRE(msg_disc_items->default_begin_cs &&
1382 msg_disc_items->default_end_cs,
1383 "Discarded events/packets message has no default clock snapshots: %!+n",
1384 msg_disc_items);
1385 ret = bt_clock_snapshot_get_ns_from_origin(
1386 msg_disc_items->default_begin_cs,
1387 &msg_ns_from_origin);
1388 if (ret) {
1389 status = BT_MESSAGE_ITERATOR_STATUS_ERROR;
1390 goto end;
1391 }
1392
1393 if (msg_ns_from_origin >= ns_from_origin) {
1394 *got_first = true;
1395 goto push_msg;
1396 }
1397
1398 ret = bt_clock_snapshot_get_ns_from_origin(
1399 msg_disc_items->default_end_cs,
1400 &msg_ns_from_origin);
1401 if (ret) {
1402 status = BT_MESSAGE_ITERATOR_STATUS_ERROR;
1403 goto end;
1404 }
1405
1406 if (msg_ns_from_origin >= ns_from_origin) {
1407 /*
1408 * The discarded items message's beginning time
1409 * is before the requested seeking time, but its
1410 * end time is after. Modify the message so as
1411 * to set its beginning time to the requested
1412 * seeking time, and make its item count unknown
1413 * as we don't know if items were really
1414 * discarded within the new time range.
1415 */
1416 uint64_t new_begin_raw_value;
1417
1418 ret = bt_clock_class_clock_value_from_ns_from_origin(
1419 msg_disc_items->default_end_cs->clock_class,
1420 ns_from_origin, &new_begin_raw_value);
1421 if (ret) {
1422 status = BT_MESSAGE_ITERATOR_STATUS_ERROR;
1423 goto end;
1424 }
1425
1426 bt_clock_snapshot_set_raw_value(
1427 msg_disc_items->default_begin_cs,
1428 new_begin_raw_value);
1429 msg_disc_items->count.base.avail =
1430 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE;
1431
1432 /*
1433 * It is safe to push it because its beginning
1434 * time is exactly the requested seeking time.
1435 */
1436 goto push_msg;
1437 } else {
1438 goto skip_msg;
1439 }
1440 }
1441 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING:
1442 {
1443 const struct bt_message_stream_activity *stream_act_msg =
1444 (const void *) msg;
1445
1446 switch (stream_act_msg->default_cs_state) {
1447 case BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_UNKNOWN:
1448 case BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_INFINITE:
1449 /*
1450 * -inf is always less than any requested time,
1451 * and we can't assume any specific time for an
1452 * unknown clock snapshot, so skip this.
1453 */
1454 goto skip_msg;
1455 case BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_KNOWN:
1456 clk_snapshot = stream_act_msg->default_cs;
1457 BT_ASSERT(clk_snapshot);
1458 break;
1459 default:
1460 abort();
1461 }
1462
1463 break;
1464 }
1465 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_END:
1466 {
1467 const struct bt_message_stream_activity *stream_act_msg =
1468 (const void *) msg;
1469
1470 switch (stream_act_msg->default_cs_state) {
1471 case BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_UNKNOWN:
1472 /*
1473 * We can't assume any specific time for an
1474 * unknown clock snapshot, so skip this.
1475 */
1476 goto skip_msg;
1477 case BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_INFINITE:
1478 /*
1479 * +inf is always greater than any requested
1480 * time.
1481 */
1482 *got_first = true;
1483 goto push_msg;
1484 case BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_KNOWN:
1485 clk_snapshot = stream_act_msg->default_cs;
1486 BT_ASSERT(clk_snapshot);
1487 break;
1488 default:
1489 abort();
1490 }
1491
1492 break;
1493 }
1494 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
1495 case BT_MESSAGE_TYPE_STREAM_END:
1496 /* Ignore */
1497 goto skip_msg;
1498 default:
1499 abort();
1500 }
1501
1502 BT_ASSERT(clk_snapshot);
1503 ret = bt_clock_snapshot_get_ns_from_origin(clk_snapshot,
1504 &msg_ns_from_origin);
1505 if (ret) {
1506 status = BT_MESSAGE_ITERATOR_STATUS_ERROR;
1507 goto end;
1508 }
1509
1510 if (msg_ns_from_origin >= ns_from_origin) {
1511 *got_first = true;
1512 goto push_msg;
1513 }
1514
1515 skip_msg:
1516 /* This message won't be sent downstream. */
1517 switch (msg->type) {
1518 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
1519 {
1520 const struct bt_message_stream *stream_msg = (const void *) msg;
1521 struct auto_seek_stream_state *stream_state;
1522
1523 /* Update stream's state: stream began. */
1524 stream_state = create_auto_seek_stream_state();
1525 if (!stream_state) {
1526 status = BT_MESSAGE_ITERATOR_STATUS_NOMEM;
1527 goto end;
1528 }
1529
1530 stream_state->state = AUTO_SEEK_STREAM_STATE_STREAM_BEGAN;
1531
1532 BT_ASSERT(!bt_g_hash_table_contains(stream_states, stream_msg->stream));
1533 g_hash_table_insert(stream_states, stream_msg->stream, stream_state);
1534 break;
1535 }
1536 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING:
1537 {
1538 const struct bt_message_stream_activity *stream_act_msg =
1539 (const void *) msg;
1540 struct auto_seek_stream_state *stream_state;
1541
1542 /* Update stream's state: stream activity began. */
1543 stream_state = g_hash_table_lookup(stream_states, stream_act_msg->stream);
1544 BT_ASSERT(stream_state);
1545
1546 BT_ASSERT(stream_state->state == AUTO_SEEK_STREAM_STATE_STREAM_BEGAN);
1547 stream_state->state = AUTO_SEEK_STREAM_STATE_STREAM_ACTIVITY_BEGAN;
1548 BT_ASSERT(!stream_state->packet);
1549 break;
1550 }
1551 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
1552 {
1553 const struct bt_message_packet *packet_msg =
1554 (const void *) msg;
1555 struct auto_seek_stream_state *stream_state;
1556
1557 /* Update stream's state: packet began. */
1558 stream_state = g_hash_table_lookup(stream_states, packet_msg->packet->stream);
1559 BT_ASSERT(stream_state);
1560
1561 BT_ASSERT(stream_state->state == AUTO_SEEK_STREAM_STATE_STREAM_ACTIVITY_BEGAN);
1562 stream_state->state = AUTO_SEEK_STREAM_STATE_PACKET_BEGAN;
1563 BT_ASSERT(!stream_state->packet);
1564 stream_state->packet = packet_msg->packet;
1565 break;
1566 }
1567 case BT_MESSAGE_TYPE_PACKET_END:
1568 {
1569 const struct bt_message_packet *packet_msg =
1570 (const void *) msg;
1571 struct auto_seek_stream_state *stream_state;
1572
1573 /* Update stream's state: packet ended. */
1574 stream_state = g_hash_table_lookup(stream_states, packet_msg->packet->stream);
1575 BT_ASSERT(stream_state);
1576
1577 BT_ASSERT(stream_state->state == AUTO_SEEK_STREAM_STATE_PACKET_BEGAN);
1578 stream_state->state = AUTO_SEEK_STREAM_STATE_STREAM_ACTIVITY_BEGAN;
1579 BT_ASSERT(stream_state->packet);
1580 stream_state->packet = NULL;
1581 break;
1582 }
1583 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_END:
1584 {
1585 const struct bt_message_stream_activity *stream_act_msg =
1586 (const void *) msg;
1587 struct auto_seek_stream_state *stream_state;
1588
1589 /* Update stream's state: stream activity ended. */
1590 stream_state = g_hash_table_lookup(stream_states, stream_act_msg->stream);
1591 BT_ASSERT(stream_state);
1592
1593 BT_ASSERT(stream_state->state == AUTO_SEEK_STREAM_STATE_STREAM_ACTIVITY_BEGAN);
1594 stream_state->state = AUTO_SEEK_STREAM_STATE_STREAM_BEGAN;
1595 BT_ASSERT(!stream_state->packet);
1596 break;
1597 }
1598 case BT_MESSAGE_TYPE_STREAM_END:
1599 {
1600 const struct bt_message_stream *stream_msg = (const void *) msg;
1601 struct auto_seek_stream_state *stream_state;
1602
1603 stream_state = g_hash_table_lookup(stream_states, stream_msg->stream);
1604 BT_ASSERT(stream_state);
1605 BT_ASSERT(stream_state->state == AUTO_SEEK_STREAM_STATE_STREAM_BEGAN);
1606 BT_ASSERT(!stream_state->packet);
1607
1608 /* Update stream's state: this stream doesn't exist anymore. */
1609 g_hash_table_remove(stream_states, stream_msg->stream);
1610 break;
1611 }
1612 default:
1613 break;
1614 }
1615
1616 bt_object_put_no_null_check(msg);
1617 msg = NULL;
1618 goto end;
1619
1620 push_msg:
1621 g_queue_push_tail(iterator->auto_seek.msgs, (void *) msg);
1622 msg = NULL;
1623
1624 end:
1625 BT_ASSERT(!msg || status != BT_MESSAGE_ITERATOR_STATUS_OK);
1626 return status;
1627 }
1628
1629 static
1630 enum bt_message_iterator_status find_message_ge_ns_from_origin(
1631 struct bt_self_component_port_input_message_iterator *iterator,
1632 int64_t ns_from_origin, GHashTable *stream_states)
1633 {
1634 int status;
1635 enum bt_self_component_port_input_message_iterator_state init_state =
1636 iterator->state;
1637 const struct bt_message *messages[MSG_BATCH_SIZE];
1638 uint64_t user_count = 0;
1639 uint64_t i;
1640 bool got_first = false;
1641
1642 BT_ASSERT(iterator);
1643 memset(&messages[0], 0, sizeof(messages[0]) * MSG_BATCH_SIZE);
1644
1645 /*
1646 * Make this iterator temporarily active (not seeking) to call
1647 * the "next" method.
1648 */
1649 set_self_comp_port_input_msg_iterator_state(iterator,
1650 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE);
1651
1652 BT_ASSERT(iterator->methods.next);
1653
1654 while (!got_first) {
1655 /*
1656 * Call the user's "next" method to get the next
1657 * messages and status.
1658 */
1659 status = call_iterator_next_method(iterator,
1660 &messages[0], MSG_BATCH_SIZE, &user_count);
1661 BT_LOGD("User method returned: status=%s",
1662 bt_message_iterator_status_string(status));
1663
1664 /*
1665 * The user's "next" method must not do any action which
1666 * would change the iterator's state.
1667 */
1668 BT_ASSERT(iterator->state ==
1669 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE);
1670
1671 switch (status) {
1672 case BT_MESSAGE_ITERATOR_STATUS_OK:
1673 BT_ASSERT_PRE(user_count <= MSG_BATCH_SIZE,
1674 "Invalid returned message count: greater than "
1675 "batch size: count=%" PRIu64 ", batch-size=%u",
1676 user_count, MSG_BATCH_SIZE);
1677 break;
1678 case BT_MESSAGE_ITERATOR_STATUS_AGAIN:
1679 case BT_MESSAGE_ITERATOR_STATUS_ERROR:
1680 case BT_MESSAGE_ITERATOR_STATUS_NOMEM:
1681 case BT_MESSAGE_ITERATOR_STATUS_END:
1682 goto end;
1683 default:
1684 abort();
1685 }
1686
1687 for (i = 0; i < user_count; i++) {
1688 if (got_first) {
1689 g_queue_push_tail(iterator->auto_seek.msgs,
1690 (void *) messages[i]);
1691 messages[i] = NULL;
1692 continue;
1693 }
1694
1695 status = auto_seek_handle_message(iterator,
1696 ns_from_origin, messages[i], &got_first,
1697 stream_states);
1698 if (status == BT_MESSAGE_ITERATOR_STATUS_OK) {
1699 /* Message was either pushed or moved */
1700 messages[i] = NULL;
1701 } else {
1702 goto end;
1703 }
1704 }
1705 }
1706
1707 end:
1708 for (i = 0; i < user_count; i++) {
1709 if (messages[i]) {
1710 bt_object_put_no_null_check(messages[i]);
1711 }
1712 }
1713
1714 set_self_comp_port_input_msg_iterator_state(iterator, init_state);
1715 return status;
1716 }
1717
1718 /*
1719 * This function is installed as the iterator's next callback after we have
1720 * auto-seeked (seeked to the beginning and fast-forwarded) to send the
1721 * messages saved in iterator->auto_seek.msgs. Once this is done, the original
1722 * next callback is put back.
1723 */
1724
1725 static
1726 enum bt_self_message_iterator_status post_auto_seek_next(
1727 struct bt_self_component_port_input_message_iterator *iterator,
1728 bt_message_array_const msgs, uint64_t capacity,
1729 uint64_t *count)
1730 {
1731 BT_ASSERT(!g_queue_is_empty(iterator->auto_seek.msgs));
1732 *count = 0;
1733
1734 /*
1735 * Move auto-seek messages to the output array (which is this
1736 * iterator's base message array).
1737 */
1738 while (capacity > 0 && !g_queue_is_empty(iterator->auto_seek.msgs)) {
1739 msgs[*count] = g_queue_pop_head(iterator->auto_seek.msgs);
1740 capacity--;
1741 (*count)++;
1742 }
1743
1744 BT_ASSERT(*count > 0);
1745
1746 if (g_queue_is_empty(iterator->auto_seek.msgs)) {
1747 /* No more auto-seek messages, restore user's next callback. */
1748 BT_ASSERT(iterator->auto_seek.original_next_callback);
1749 iterator->methods.next = iterator->auto_seek.original_next_callback;
1750 iterator->auto_seek.original_next_callback = NULL;
1751 }
1752
1753 return BT_SELF_MESSAGE_ITERATOR_STATUS_OK;
1754 }
1755
1756 static inline
1757 int clock_raw_value_from_ns_from_origin(const bt_clock_class *clock_class,
1758 int64_t ns_from_origin, uint64_t *raw_value)
1759 {
1760
1761 int64_t cc_offset_s = clock_class->offset_seconds;
1762 uint64_t cc_offset_cycles = clock_class->offset_cycles;
1763 uint64_t cc_freq = clock_class->frequency;
1764
1765 return bt_common_clock_value_from_ns_from_origin(cc_offset_s,
1766 cc_offset_cycles, cc_freq, ns_from_origin, raw_value);
1767 }
1768
1769
1770 enum bt_message_iterator_status
1771 bt_self_component_port_input_message_iterator_seek_ns_from_origin(
1772 struct bt_self_component_port_input_message_iterator *iterator,
1773 int64_t ns_from_origin)
1774 {
1775 int status;
1776 GHashTable *stream_states = NULL;
1777
1778 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
1779 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator);
1780 BT_ASSERT_PRE(
1781 bt_component_borrow_graph(iterator->upstream_component)->config_state !=
1782 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
1783 "Graph is not configured: %!+g",
1784 bt_component_borrow_graph(iterator->upstream_component));
1785 BT_ASSERT_PRE(
1786 bt_self_component_port_input_message_iterator_can_seek_ns_from_origin(
1787 iterator, ns_from_origin),
1788 "Message iterator cannot seek nanoseconds from origin: %!+i, "
1789 "ns-from-origin=%" PRId64, iterator, ns_from_origin);
1790 set_self_comp_port_input_msg_iterator_state(iterator,
1791 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_SEEKING);
1792
1793 /*
1794 * We are seeking, reset our expectations about how the following
1795 * messages should look like.
1796 */
1797 reset_iterator_expectations(iterator);
1798
1799 if (iterator->methods.seek_ns_from_origin) {
1800 /* The iterator knows how to seek to a particular time, let it handle this. */
1801 BT_LIB_LOGD("Calling user's \"seek nanoseconds from origin\" method: "
1802 "%![iter-]+i, ns=%" PRId64, iterator, ns_from_origin);
1803 status = iterator->methods.seek_ns_from_origin(iterator,
1804 ns_from_origin);
1805 BT_LOGD("User method returned: status=%s",
1806 bt_message_iterator_status_string(status));
1807 BT_ASSERT_PRE(status == BT_MESSAGE_ITERATOR_STATUS_OK ||
1808 status == BT_MESSAGE_ITERATOR_STATUS_ERROR ||
1809 status == BT_MESSAGE_ITERATOR_STATUS_NOMEM ||
1810 status == BT_MESSAGE_ITERATOR_STATUS_AGAIN,
1811 "Unexpected status: %![iter-]+i, status=%s",
1812 iterator,
1813 bt_common_self_message_iterator_status_string(status));
1814 } else {
1815 /*
1816 * The iterator doesn't know how to seek to a particular time. We will
1817 * seek to the beginning and fast forward to the right place.
1818 */
1819 BT_ASSERT(iterator->methods.can_seek_beginning(iterator));
1820 BT_ASSERT(iterator->methods.seek_beginning);
1821 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i",
1822 iterator);
1823 status = iterator->methods.seek_beginning(iterator);
1824 BT_LOGD("User method returned: status=%s",
1825 bt_message_iterator_status_string(status));
1826 BT_ASSERT_PRE(status == BT_MESSAGE_ITERATOR_STATUS_OK ||
1827 status == BT_MESSAGE_ITERATOR_STATUS_ERROR ||
1828 status == BT_MESSAGE_ITERATOR_STATUS_NOMEM ||
1829 status == BT_MESSAGE_ITERATOR_STATUS_AGAIN,
1830 "Unexpected status: %![iter-]+i, status=%s",
1831 iterator,
1832 bt_common_self_message_iterator_status_string(status));
1833 switch (status) {
1834 case BT_MESSAGE_ITERATOR_STATUS_OK:
1835 break;
1836 case BT_MESSAGE_ITERATOR_STATUS_ERROR:
1837 case BT_MESSAGE_ITERATOR_STATUS_NOMEM:
1838 case BT_MESSAGE_ITERATOR_STATUS_AGAIN:
1839 goto end;
1840 default:
1841 abort();
1842 }
1843
1844 /*
1845 * Find the first message which has a default clock
1846 * snapshot greater than or equal to the requested
1847 * seeking time, and move the received messages from
1848 * this point in the batch to this iterator's auto-seek
1849 * message queue.
1850 */
1851 while (!g_queue_is_empty(iterator->auto_seek.msgs)) {
1852 bt_object_put_no_null_check(
1853 g_queue_pop_tail(iterator->auto_seek.msgs));
1854 }
1855
1856 stream_states = create_auto_seek_stream_states();
1857 if (!stream_states) {
1858 BT_LOGE_STR("Failed to allocate one GHashTable.");
1859 status = BT_MESSAGE_ITERATOR_STATUS_NOMEM;
1860 goto end;
1861 }
1862
1863 status = find_message_ge_ns_from_origin(iterator,
1864 ns_from_origin, stream_states);
1865 switch (status) {
1866 case BT_MESSAGE_ITERATOR_STATUS_OK:
1867 case BT_MESSAGE_ITERATOR_STATUS_END:
1868 {
1869 GHashTableIter iter;
1870 gpointer key, value;
1871
1872 /*
1873 * If some streams exist at the seek time, prepend the
1874 * required messages to put those streams in the right
1875 * state.
1876 */
1877 g_hash_table_iter_init(&iter, stream_states);
1878 while (g_hash_table_iter_next (&iter, &key, &value)) {
1879 const bt_stream *stream = key;
1880 struct auto_seek_stream_state *stream_state =
1881 (struct auto_seek_stream_state *) value;
1882 bt_message *msg;
1883 const bt_clock_class *clock_class = bt_stream_class_borrow_default_clock_class_const(
1884 bt_stream_borrow_class_const(stream));
1885 uint64_t raw_value;
1886
1887 if (clock_raw_value_from_ns_from_origin(clock_class, ns_from_origin, &raw_value) != 0) {
1888 BT_LIB_LOGW("Could not convert nanoseconds from origin to clock value: ns-from-origin=%" PRId64 ", %![cc-]+K",
1889 ns_from_origin, clock_class);
1890 status = BT_MESSAGE_ITERATOR_STATUS_ERROR;
1891 goto end;
1892 }
1893
1894 switch (stream_state->state) {
1895 case AUTO_SEEK_STREAM_STATE_PACKET_BEGAN:
1896 BT_ASSERT(stream_state->packet);
1897 BT_LIB_LOGD("Creating packet message: %![packet-]+a", stream_state->packet);
1898 msg = bt_message_packet_beginning_create_with_default_clock_snapshot(
1899 (bt_self_message_iterator *) iterator, stream_state->packet, raw_value);
1900 if (!msg) {
1901 status = BT_MESSAGE_ITERATOR_STATUS_NOMEM;
1902 goto end;
1903 }
1904
1905 g_queue_push_head(iterator->auto_seek.msgs, msg);
1906 msg = NULL;
1907 /* fall-thru */
1908 case AUTO_SEEK_STREAM_STATE_STREAM_ACTIVITY_BEGAN:
1909 msg = bt_message_stream_activity_beginning_create(
1910 (bt_self_message_iterator *) iterator, stream);
1911 if (!msg) {
1912 status = BT_MESSAGE_ITERATOR_STATUS_NOMEM;
1913 goto end;
1914 }
1915
1916 bt_message_stream_activity_beginning_set_default_clock_snapshot(msg, raw_value);
1917
1918 g_queue_push_head(iterator->auto_seek.msgs, msg);
1919 msg = NULL;
1920 /* fall-thru */
1921 case AUTO_SEEK_STREAM_STATE_STREAM_BEGAN:
1922 msg = bt_message_stream_beginning_create(
1923 (bt_self_message_iterator *) iterator, stream);
1924 if (!msg) {
1925 status = BT_MESSAGE_ITERATOR_STATUS_NOMEM;
1926 goto end;
1927 }
1928
1929 g_queue_push_head(iterator->auto_seek.msgs, msg);
1930 msg = NULL;
1931 break;
1932 }
1933 }
1934
1935 /*
1936 * If there are messages in the auto-seek
1937 * message queue, replace the user's "next"
1938 * method with a custom, temporary "next" method
1939 * which returns them.
1940 */
1941 if (!g_queue_is_empty(iterator->auto_seek.msgs)) {
1942 BT_ASSERT(!iterator->auto_seek.original_next_callback);
1943 iterator->auto_seek.original_next_callback = iterator->methods.next;
1944
1945 iterator->methods.next =
1946 (bt_self_component_port_input_message_iterator_next_method)
1947 post_auto_seek_next;
1948 }
1949
1950 /*
1951 * `BT_MESSAGE_ITERATOR_STATUS_END` becomes
1952 * `BT_MESSAGE_ITERATOR_STATUS_OK`: the next
1953 * time this iterator's "next" method is called,
1954 * it will return
1955 * `BT_MESSAGE_ITERATOR_STATUS_END`.
1956 */
1957 status = BT_MESSAGE_ITERATOR_STATUS_OK;
1958 break;
1959 }
1960 case BT_MESSAGE_ITERATOR_STATUS_ERROR:
1961 case BT_MESSAGE_ITERATOR_STATUS_NOMEM:
1962 case BT_MESSAGE_ITERATOR_STATUS_AGAIN:
1963 goto end;
1964 default:
1965 abort();
1966 }
1967 }
1968
1969 /*
1970 * The following messages returned by the next method (including
1971 * post_auto_seek_next) must be after (or at) `ns_from_origin`.
1972 */
1973 iterator->last_ns_from_origin = ns_from_origin;
1974
1975 end:
1976 if (stream_states) {
1977 destroy_auto_seek_stream_states(stream_states);
1978 stream_states = NULL;
1979 }
1980 set_iterator_state_after_seeking(iterator, status);
1981 return status;
1982 }
1983
1984 static inline
1985 bt_self_component_port_input_message_iterator *
1986 borrow_output_port_message_iterator_upstream_iterator(
1987 struct bt_port_output_message_iterator *iterator)
1988 {
1989 struct bt_component_class_sink_colander_priv_data *colander_data;
1990
1991 BT_ASSERT(iterator);
1992 colander_data = (void *) iterator->colander->parent.user_data;
1993 BT_ASSERT(colander_data);
1994 BT_ASSERT(colander_data->msg_iter);
1995 return colander_data->msg_iter;
1996 }
1997
1998 bt_bool bt_port_output_message_iterator_can_seek_ns_from_origin(
1999 struct bt_port_output_message_iterator *iterator,
2000 int64_t ns_from_origin)
2001 {
2002 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
2003 return bt_self_component_port_input_message_iterator_can_seek_ns_from_origin(
2004 borrow_output_port_message_iterator_upstream_iterator(
2005 iterator), ns_from_origin);
2006 }
2007
2008 bt_bool bt_port_output_message_iterator_can_seek_beginning(
2009 struct bt_port_output_message_iterator *iterator)
2010 {
2011 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
2012 return bt_self_component_port_input_message_iterator_can_seek_beginning(
2013 borrow_output_port_message_iterator_upstream_iterator(
2014 iterator));
2015 }
2016
2017 enum bt_message_iterator_status bt_port_output_message_iterator_seek_ns_from_origin(
2018 struct bt_port_output_message_iterator *iterator,
2019 int64_t ns_from_origin)
2020 {
2021 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
2022 return bt_self_component_port_input_message_iterator_seek_ns_from_origin(
2023 borrow_output_port_message_iterator_upstream_iterator(iterator),
2024 ns_from_origin);
2025 }
2026
2027 enum bt_message_iterator_status bt_port_output_message_iterator_seek_beginning(
2028 struct bt_port_output_message_iterator *iterator)
2029 {
2030 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
2031 return bt_self_component_port_input_message_iterator_seek_beginning(
2032 borrow_output_port_message_iterator_upstream_iterator(
2033 iterator));
2034 }
2035
2036 void bt_port_output_message_iterator_get_ref(
2037 const struct bt_port_output_message_iterator *iterator)
2038 {
2039 bt_object_get_ref(iterator);
2040 }
2041
2042 void bt_port_output_message_iterator_put_ref(
2043 const struct bt_port_output_message_iterator *iterator)
2044 {
2045 bt_object_put_ref(iterator);
2046 }
2047
2048 void bt_self_component_port_input_message_iterator_get_ref(
2049 const struct bt_self_component_port_input_message_iterator *iterator)
2050 {
2051 bt_object_get_ref(iterator);
2052 }
2053
2054 void bt_self_component_port_input_message_iterator_put_ref(
2055 const struct bt_self_component_port_input_message_iterator *iterator)
2056 {
2057 bt_object_put_ref(iterator);
2058 }
This page took 0.089837 seconds and 5 git commands to generate.