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