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