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