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