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