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