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