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