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