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