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