lib: run most of bt_self_component_port_input_message_iterator_try_finalize when...
[babeltrace.git] / src / lib / graph / iterator.c
CommitLineData
47e5a032 1/*
e2f7325d 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
350ad6c1 24#define BT_LOG_TAG "LIB/MSG-ITER"
c2d9d9cf 25#include "lib/logging.h"
5af447e5 26
578e048b 27#include "compat/compiler.h"
e74dbb33 28#include "compat/glib.h"
578e048b
MJ
29#include "lib/trace-ir/clock-class.h"
30#include "lib/trace-ir/clock-snapshot.h"
3fadfbc0
MJ
31#include <babeltrace2/trace-ir/field.h>
32#include <babeltrace2/trace-ir/event-const.h>
578e048b 33#include "lib/trace-ir/event.h"
3fadfbc0 34#include <babeltrace2/trace-ir/packet-const.h>
578e048b
MJ
35#include "lib/trace-ir/packet.h"
36#include "lib/trace-ir/stream.h"
d24d5663
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>
3fadfbc0 40#include <babeltrace2/graph/connection-const.h>
3fadfbc0 41#include <babeltrace2/graph/component-const.h>
3fadfbc0 42#include <babeltrace2/graph/component-sink-const.h>
3fadfbc0 43#include <babeltrace2/graph/message-const.h>
d24d5663 44#include <babeltrace2/graph/message-iterator.h>
3fadfbc0 45#include <babeltrace2/graph/self-component-port-input-message-iterator.h>
3fadfbc0 46#include <babeltrace2/graph/message-event-const.h>
5b7b55be
SM
47#include <babeltrace2/graph/message-message-iterator-inactivity-const.h>
48#include <babeltrace2/graph/message-packet-beginning.h>
3fadfbc0
MJ
49#include <babeltrace2/graph/message-packet-beginning-const.h>
50#include <babeltrace2/graph/message-packet-end-const.h>
5b7b55be 51#include <babeltrace2/graph/message-stream-beginning.h>
3fadfbc0
MJ
52#include <babeltrace2/graph/message-stream-beginning-const.h>
53#include <babeltrace2/graph/message-stream-end-const.h>
3fadfbc0
MJ
54#include <babeltrace2/graph/port-const.h>
55#include <babeltrace2/graph/graph.h>
56#include <babeltrace2/graph/graph-const.h>
3fadfbc0 57#include <babeltrace2/types.h>
578e048b
MJ
58#include "common/assert.h"
59#include "lib/assert-pre.h"
f6f301d7 60#include "lib/assert-post.h"
fa054faf 61#include <stdint.h>
2ec84d26 62#include <inttypes.h>
c4f23e30 63#include <stdbool.h>
0fbb9a9f 64#include <stdlib.h>
3230ee6b 65
578e048b 66#include "component-class.h"
578e048b
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"
d24d5663 79#include "lib/func-status.h"
578e048b 80
d4393e08
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 */
d6e69534 85#define MSG_BATCH_SIZE 15
d4393e08 86
7474e7d3
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
d0fea130 94static inline
003e713f 95void set_self_comp_port_input_msg_iterator_state(
d0fea130
PP
96 struct bt_self_component_port_input_message_iterator *iterator,
97 enum bt_self_component_port_input_message_iterator_state state)
98{
98b15851 99 BT_ASSERT_DBG(iterator);
7474e7d3 100 BT_LIB_LOGD("Updating message iterator's state: new-state=%s",
d0fea130
PP
101 bt_self_component_port_input_message_iterator_state_string(state));
102 iterator->state = state;
103}
104
47e5a032 105static
d6e69534 106void bt_self_component_port_input_message_iterator_destroy(struct bt_object *obj)
47e5a032 107{
d6e69534 108 struct bt_self_component_port_input_message_iterator *iterator;
8738a040 109
f6ccaed9 110 BT_ASSERT(obj);
d3eb6e8f 111
bd14d768 112 /*
d6e69534 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
d6e69534 116 * message iterator's finalization function does
d94d92ac
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 */
3fea54f6 123 obj->ref_count++;
07245ac2 124 iterator = (void *) obj;
3f7d4d90 125 BT_LIB_LOGI("Destroying self component input port message iterator object: "
d94d92ac 126 "%!+i", iterator);
d0fea130 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);
d94d92ac 136 iterator->connection = NULL;
bd14d768
PP
137 }
138
da9c4c52
SM
139 if (iterator->auto_seek.msgs) {
140 while (!g_queue_is_empty(iterator->auto_seek.msgs)) {
6871026b 141 bt_object_put_ref_no_null_check(
da9c4c52 142 g_queue_pop_tail(iterator->auto_seek.msgs));
7474e7d3
PP
143 }
144
da9c4c52
SM
145 g_queue_free(iterator->auto_seek.msgs);
146 iterator->auto_seek.msgs = NULL;
7474e7d3
PP
147 }
148
ca02df0a
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
6c373cc9
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
d0fea130 168void bt_self_component_port_input_message_iterator_try_finalize(
d6e69534 169 struct bt_self_component_port_input_message_iterator *iterator)
bd14d768 170{
ca02df0a 171 uint64_t i;
d94d92ac 172 typedef void (*method_t)(void *);
fca28f75 173 bool call_user_finalize = true;
bd14d768 174
f6ccaed9 175 BT_ASSERT(iterator);
bd14d768
PP
176
177 switch (iterator->state) {
d6e69534 178 case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED:
fca28f75
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;
d6e69534 191 case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED:
bd14d768 192 /* Already finalized */
d6e69534 193 BT_LIB_LOGD("Not finalizing message iterator: already finalized: "
d94d92ac 194 "%!+i", iterator);
d0fea130
PP
195 goto end;
196 case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZING:
870631a2 197 /* Finalizing */
d0fea130
PP
198 BT_LIB_LOGF("Message iterator is already being finalized: "
199 "%!+i", iterator);
498e7994 200 bt_common_abort();
bd14d768
PP
201 default:
202 break;
203 }
204
d6e69534 205 BT_LIB_LOGD("Finalizing message iterator: %!+i", iterator);
d0fea130
PP
206 set_self_comp_port_input_msg_iterator_state(iterator,
207 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZING);
f6ccaed9 208 BT_ASSERT(iterator->upstream_component);
bd14d768
PP
209
210 /* Call user-defined destroy method */
fca28f75
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
fca28f75
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
fca28f75
SM
238 if (method) {
239 const bt_error *saved_error;
42a63165 240
fca28f75 241 saved_error = bt_current_thread_take_error();
42a63165 242
fca28f75
SM
243 BT_LIB_LOGD("Calling user's finalization method: %!+i",
244 iterator);
245 method(iterator);
42a63165 246
fca28f75
SM
247 if (saved_error) {
248 BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(saved_error);
249 }
42a63165 250 }
bd14d768
PP
251 }
252
ca02df0a
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;
d0fea130
PP
276 set_self_comp_port_input_msg_iterator_state(iterator,
277 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED);
d6e69534 278 BT_LIB_LOGD("Finalized message iterator: %!+i", iterator);
d0fea130
PP
279
280end:
281 return;
bd14d768
PP
282}
283
284BT_HIDDEN
d6e69534
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{
f6ccaed9 289 BT_ASSERT(iterator);
bd14d768 290 iterator->connection = connection;
3f7d4d90 291 BT_LIB_LOGI("Set message iterator's connection: "
d94d92ac 292 "%![iter-]+i, %![conn-]+x", iterator, connection);
bd14d768
PP
293}
294
7474e7d3 295static
f2fb1b32 296enum bt_message_iterator_can_seek_beginning_status can_seek_ns_from_origin_true(
7474e7d3 297 struct bt_self_component_port_input_message_iterator *iterator,
f2fb1b32 298 int64_t ns_from_origin, bt_bool *can_seek)
7474e7d3 299{
f2fb1b32
SM
300 *can_seek = BT_TRUE;
301
302 return BT_FUNC_STATUS_OK;
7474e7d3
PP
303}
304
305static
f2fb1b32
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)
7474e7d3 309{
f2fb1b32
SM
310 *can_seek = BT_TRUE;
311
312 return BT_FUNC_STATUS_OK;
7474e7d3
PP
313}
314
d94d92ac 315static
e803df70 316int create_self_component_input_port_message_iterator(
ca02df0a 317 struct bt_self_message_iterator *self_downstream_msg_iter,
e803df70
SM
318 struct bt_self_component_port_input *self_port,
319 struct bt_self_component_port_input_message_iterator **message_iterator)
47e5a032 320{
21a9f056 321 typedef enum bt_component_class_message_iterator_initialize_method_status (*init_method_t)(
8d8b141d 322 void *, void *, void *, void *);
ca02df0a 323
ca02df0a
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;
e803df70 334 int status;
47e5a032 335
e803df70 336 BT_ASSERT_PRE_NON_NULL(message_iterator, "Created message iterator");
ca02df0a
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);
ca02df0a
PP
343 BT_ASSERT(port->connection);
344 upstream_port = port->connection->upstream_port;
f6ccaed9 345 BT_ASSERT(upstream_port);
ca02df0a
PP
346 upstream_comp = bt_port_borrow_component_inline(upstream_port);
347 BT_ASSERT(upstream_comp);
348 BT_ASSERT_PRE(
9b4f9b42
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,
ca02df0a
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 ==
d94d92ac 357 BT_COMPONENT_CLASS_TYPE_SOURCE ||
ca02df0a 358 upstream_comp->class->type ==
d94d92ac 359 BT_COMPONENT_CLASS_TYPE_FILTER);
ca02df0a
PP
360 BT_LIB_LOGI("Creating message iterator on self component input port: "
361 "%![up-comp-]+c, %![up-port-]+p", upstream_comp, upstream_port);
d94d92ac 362 iterator = g_new0(
d6e69534 363 struct bt_self_component_port_input_message_iterator, 1);
47e5a032 364 if (!iterator) {
870631a2
PP
365 BT_LIB_LOGE_APPEND_CAUSE(
366 "Failed to allocate one self component input port "
d6e69534 367 "message iterator.");
e803df70 368 status = BT_FUNC_STATUS_MEMORY_ERROR;
870631a2 369 goto error;
47e5a032
JG
370 }
371
6c373cc9 372 bt_object_init_shared(&iterator->base,
d6e69534 373 bt_self_component_port_input_message_iterator_destroy);
6c373cc9
PP
374 iterator->msgs = g_ptr_array_new();
375 if (!iterator->msgs) {
376 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
e803df70 377 status = BT_FUNC_STATUS_MEMORY_ERROR;
870631a2 378 goto error;
d4393e08 379 }
3230ee6b 380
6c373cc9 381 g_ptr_array_set_size(iterator->msgs, MSG_BATCH_SIZE);
54b135a0 382 iterator->last_ns_from_origin = INT64_MIN;
da9c4c52
SM
383 iterator->auto_seek.msgs = g_queue_new();
384 if (!iterator->auto_seek.msgs) {
870631a2 385 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GQueue.");
e803df70 386 status = BT_FUNC_STATUS_MEMORY_ERROR;
ca02df0a
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.");
e803df70 393 status = BT_FUNC_STATUS_MEMORY_ERROR;
ca02df0a 394 goto error;
3230ee6b
PP
395 }
396
bd14d768
PP
397 iterator->upstream_component = upstream_comp;
398 iterator->upstream_port = upstream_port;
d94d92ac 399 iterator->connection = iterator->upstream_port->connection;
5c563278 400 iterator->graph = bt_component_borrow_graph(upstream_comp);
d0fea130
PP
401 set_self_comp_port_input_msg_iterator_state(iterator,
402 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED);
7474e7d3
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:
498e7994 450 bt_common_abort();
7474e7d3
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 =
86751779 463 (bt_self_component_port_input_message_iterator_can_seek_beginning_method)
7474e7d3
PP
464 can_seek_beginning_true;
465 }
466
d94d92ac
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 =
21a9f056 474 (init_method_t) src_comp_cls->methods.msg_iter_initialize;
d94d92ac
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 =
21a9f056 483 (init_method_t) flt_comp_cls->methods.msg_iter_initialize;
d94d92ac
PP
484 break;
485 }
486 default:
487 /* Unreachable */
498e7994 488 bt_common_abort();
d94d92ac
PP
489 }
490
491 if (init_method) {
21a9f056 492 enum bt_component_class_message_iterator_initialize_method_status iter_status;
d94d92ac
PP
493
494 BT_LIB_LOGD("Calling user's initialization method: %!+i", iterator);
8d8b141d 495 iter_status = init_method(iterator, &iterator->config, upstream_comp,
d94d92ac
PP
496 upstream_port);
497 BT_LOGD("User method returned: status=%s",
d24d5663 498 bt_common_func_status_string(iter_status));
6ecdcca3 499 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(iter_status);
d24d5663 500 if (iter_status != BT_FUNC_STATUS_OK) {
870631a2
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));
e803df70 506 status = iter_status;
870631a2 507 goto error;
d94d92ac 508 }
8d8b141d
SM
509
510 iterator->config.frozen = true;
d94d92ac
PP
511 }
512
ca02df0a
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
d0fea130
PP
525 set_self_comp_port_input_msg_iterator_state(iterator,
526 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE);
d94d92ac 527 g_ptr_array_add(port->connection->iterators, iterator);
3f7d4d90 528 BT_LIB_LOGI("Created message iterator on self component input port: "
d94d92ac
PP
529 "%![up-port-]+p, %![up-comp-]+c, %![iter-]+i",
530 upstream_port, upstream_comp, iterator);
e803df70
SM
531
532 *message_iterator = iterator;
533 status = BT_FUNC_STATUS_OK;
870631a2
PP
534 goto end;
535
536error:
537 BT_OBJECT_PUT_REF_AND_RESET(iterator);
d94d92ac
PP
538
539end:
e803df70 540 return status;
ea8d3e58
JG
541}
542
e803df70 543bt_self_component_port_input_message_iterator_create_from_message_iterator_status
ca02df0a
PP
544bt_self_component_port_input_message_iterator_create_from_message_iterator(
545 struct bt_self_message_iterator *self_msg_iter,
e803df70
SM
546 struct bt_self_component_port_input *input_port,
547 struct bt_self_component_port_input_message_iterator **message_iterator)
ca02df0a 548{
17f3083a 549 BT_ASSERT_PRE_NO_ERROR();
ca02df0a
PP
550 BT_ASSERT_PRE_NON_NULL(self_msg_iter, "Message iterator");
551 return create_self_component_input_port_message_iterator(self_msg_iter,
e803df70 552 input_port, message_iterator);
ca02df0a
PP
553}
554
e803df70 555bt_self_component_port_input_message_iterator_create_from_sink_component_status
ca02df0a
PP
556bt_self_component_port_input_message_iterator_create_from_sink_component(
557 struct bt_self_component_sink *self_comp,
e803df70
SM
558 struct bt_self_component_port_input *input_port,
559 struct bt_self_component_port_input_message_iterator **message_iterator)
ca02df0a 560{
17f3083a 561 BT_ASSERT_PRE_NO_ERROR();
ca02df0a
PP
562 BT_ASSERT_PRE_NON_NULL(self_comp, "Sink component");
563 return create_self_component_input_port_message_iterator(NULL,
e803df70 564 input_port, message_iterator);
ca02df0a
PP
565}
566
d6e69534
PP
567void *bt_self_message_iterator_get_data(
568 const struct bt_self_message_iterator *self_iterator)
ea8d3e58 569{
d6e69534 570 struct bt_self_component_port_input_message_iterator *iterator =
d94d92ac 571 (void *) self_iterator;
ea8d3e58 572
bdb288b3 573 BT_ASSERT_PRE_DEV_NON_NULL(iterator, "Message iterator");
d94d92ac 574 return iterator->user_data;
8738a040 575}
413bc2c4 576
d6e69534
PP
577void bt_self_message_iterator_set_data(
578 struct bt_self_message_iterator *self_iterator, void *data)
5c563278 579{
d6e69534 580 struct bt_self_component_port_input_message_iterator *iterator =
d94d92ac 581 (void *) self_iterator;
5c563278 582
bdb288b3 583 BT_ASSERT_PRE_DEV_NON_NULL(iterator, "Message iterator");
d94d92ac 584 iterator->user_data = data;
3f7d4d90 585 BT_LIB_LOGD("Set message iterator's user data: "
d94d92ac 586 "%!+i, user-data-addr=%p", iterator, data);
5c563278
PP
587}
588
8d8b141d
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
54b135a0
SM
599/*
600 * Validate that the default clock snapshot in `msg` doesn't make us go back in
601 * time.
602 */
603
bdb288b3 604BT_ASSERT_POST_DEV_FUNC
54b135a0
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;
d24d5663 613 enum bt_clock_snapshot_get_ns_from_origin_status clock_snapshot_status;
54b135a0
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 }
188edac1
SM
642 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
643 case BT_MESSAGE_TYPE_STREAM_END:
54b135a0 644 {
188edac1
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;
54b135a0 648 }
188edac1
SM
649
650 clock_snapshot = stream_msg->default_cs;
54b135a0
SM
651 break;
652 }
54b135a0
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
d747e85f
SM
668 clock_snapshot_status = bt_clock_snapshot_get_ns_from_origin(
669 clock_snapshot, &ns_from_origin);
d24d5663 670 if (clock_snapshot_status != BT_FUNC_STATUS_OK) {
d747e85f
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();
54b135a0
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
bdb288b3 686BT_ASSERT_POST_DEV_FUNC
54b135a0
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
bdb288b3 713BT_ASSERT_POST_DEV_FUNC
54b135a0
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;
6162e6b7 745 bt_uuid_copy(iterator->clock_expectation.uuid, clock_class_uuid);
54b135a0
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) {
bdb288b3
PP
753 BT_ASSERT_POST_DEV_MSG(
754 "Expecting no clock class, got one: %![cc-]+K",
54b135a0
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) {
bdb288b3
PP
764 BT_ASSERT_POST_DEV_MSG(
765 "Expecting a clock class, got none.");
54b135a0
SM
766 result = false;
767 goto end;
768 }
769
770 if (!bt_clock_class_origin_is_unix_epoch(clock_class)) {
bdb288b3
PP
771 BT_ASSERT_POST_DEV_MSG(
772 "Expecting a clock class with Unix epoch origin: %![cc-]+K",
54b135a0
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) {
bdb288b3
PP
781 BT_ASSERT_POST_DEV_MSG(
782 "Expecting a clock class, got none.");
54b135a0
SM
783 result = false;
784 goto end;
785 }
786
787 if (bt_clock_class_origin_is_unix_epoch(clock_class)) {
bdb288b3
PP
788 BT_ASSERT_POST_DEV_MSG(
789 "Expecting a clock class without Unix epoch origin: %![cc-]+K",
54b135a0
SM
790 clock_class);
791 result = false;
792 goto end;
793 }
794
795 if (!clock_class_uuid) {
bdb288b3
PP
796 BT_ASSERT_POST_DEV_MSG(
797 "Expecting a clock class with UUID: %![cc-]+K",
54b135a0
SM
798 clock_class);
799 result = false;
800 goto end;
801 }
802
803 if (bt_uuid_compare(iterator->clock_expectation.uuid, clock_class_uuid)) {
bdb288b3
PP
804 BT_ASSERT_POST_DEV_MSG(
805 "Expecting a clock class with UUID, got one "
54b135a0
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) {
bdb288b3
PP
815 BT_ASSERT_POST_DEV_MSG(
816 "Expecting a clock class, got none.");
54b135a0
SM
817 result = false;
818 goto end;
819 }
820
821 if (bt_clock_class_origin_is_unix_epoch(clock_class)) {
bdb288b3
PP
822 BT_ASSERT_POST_DEV_MSG(
823 "Expecting a clock class without Unix epoch origin: %![cc-]+K",
54b135a0
SM
824 clock_class);
825 result = false;
826 goto end;
827 }
828
829 if (clock_class_uuid) {
bdb288b3
PP
830 BT_ASSERT_POST_DEV_MSG(
831 "Expecting a clock class without UUID: %![cc-]+K",
54b135a0
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
bdb288b3 846BT_ASSERT_POST_DEV_FUNC
54b135a0
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
d24d5663
PP
874enum bt_component_class_message_iterator_next_method_status
875call_iterator_next_method(
54b135a0
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{
d24d5663 879 enum bt_component_class_message_iterator_next_method_status status;
54b135a0 880
98b15851 881 BT_ASSERT_DBG(iterator->methods.next);
54b135a0 882 BT_LOGD_STR("Calling user's \"next\" method.");
54b135a0 883 status = iterator->methods.next(iterator, msgs, capacity, user_count);
f6f301d7 884 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64,
d24d5663 885 bt_common_func_status_string(status), *user_count);
54b135a0 886
d24d5663 887 if (status == BT_FUNC_STATUS_OK) {
bdb288b3 888 BT_ASSERT_POST_DEV(clock_classes_are_compatible(iterator, msgs, *user_count),
54b135a0 889 "Clocks are not compatible");
bdb288b3 890 BT_ASSERT_POST_DEV(clock_snapshots_are_monotonic(iterator, msgs, *user_count),
54b135a0
SM
891 "Clock snapshots are not monotonic");
892 }
893
6ecdcca3
SM
894 BT_ASSERT_POST_DEV_NO_ERROR_IF_NO_ERROR_STATUS(status);
895
54b135a0
SM
896 return status;
897}
898
d24d5663 899enum bt_message_iterator_next_status
d6e69534
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{
d24d5663 904 enum bt_message_iterator_next_status status = BT_FUNC_STATUS_OK;
d94d92ac 905
17f3083a 906 BT_ASSERT_PRE_DEV_NO_ERROR();
bdb288b3
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 ==
d6e69534
PP
911 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE,
912 "Message iterator's \"next\" called, but "
7474e7d3 913 "message iterator is in the wrong state: %!+i", iterator);
98b15851
PP
914 BT_ASSERT_DBG(iterator->upstream_component);
915 BT_ASSERT_DBG(iterator->upstream_component->class);
bdb288b3 916 BT_ASSERT_PRE_DEV(
5badd463
PP
917 bt_component_borrow_graph(iterator->upstream_component)->config_state !=
918 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
4725a201
PP
919 "Graph is not configured: %!+g",
920 bt_component_borrow_graph(iterator->upstream_component));
d94d92ac 921 BT_LIB_LOGD("Getting next self component input port "
3f7d4d90
PP
922 "message iterator's messages: %!+i, batch-size=%u",
923 iterator, MSG_BATCH_SIZE);
d3eb6e8f 924
3230ee6b 925 /*
d6e69534 926 * Call the user's "next" method to get the next messages
fa054faf 927 * and status.
3230ee6b 928 */
3f7d4d90 929 *user_count = 0;
d24d5663 930 status = (int) call_iterator_next_method(iterator,
6c373cc9 931 (void *) iterator->msgs->pdata, MSG_BATCH_SIZE,
7474e7d3 932 user_count);
870631a2
PP
933 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64,
934 bt_common_func_status_string(status), *user_count);
d4393e08 935 if (status < 0) {
870631a2
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));
f42867e2
PP
940 goto end;
941 }
3230ee6b 942
d0fea130
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.
7474e7d3
PP
948 *
949 * For the same reason, there is no way that this iterator could
950 * have seeked (cannot seek a self message iterator).
d0fea130 951 */
98b15851 952 BT_ASSERT_DBG(iterator->state ==
d0fea130 953 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE);
8cf27cc5 954
d4393e08 955 switch (status) {
d24d5663 956 case BT_FUNC_STATUS_OK:
bdb288b3 957 BT_ASSERT_POST_DEV(*user_count <= MSG_BATCH_SIZE,
7474e7d3
PP
958 "Invalid returned message count: greater than "
959 "batch size: count=%" PRIu64 ", batch-size=%u",
960 *user_count, MSG_BATCH_SIZE);
6c373cc9 961 *msgs = (void *) iterator->msgs->pdata;
d4393e08 962 break;
d24d5663 963 case BT_FUNC_STATUS_AGAIN:
d4393e08 964 goto end;
d24d5663 965 case BT_FUNC_STATUS_END:
d0fea130
PP
966 set_self_comp_port_input_msg_iterator_state(iterator,
967 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED);
f42867e2 968 goto end;
f42867e2
PP
969 default:
970 /* Unknown non-error status */
498e7994 971 bt_common_abort();
41a2b7ae
PP
972 }
973
974end:
3230ee6b
PP
975 return status;
976}
977
7474e7d3
PP
978struct bt_component *
979bt_self_component_port_input_message_iterator_borrow_component(
d6e69534 980 struct bt_self_component_port_input_message_iterator *iterator)
d94d92ac 981{
bdb288b3 982 BT_ASSERT_PRE_DEV_NON_NULL(iterator, "Message iterator");
d94d92ac
PP
983 return iterator->upstream_component;
984}
985
d6e69534
PP
986struct bt_self_component *bt_self_message_iterator_borrow_component(
987 struct bt_self_message_iterator *self_iterator)
413bc2c4 988{
d6e69534 989 struct bt_self_component_port_input_message_iterator *iterator =
d94d92ac 990 (void *) self_iterator;
90157d89 991
bdb288b3 992 BT_ASSERT_PRE_DEV_NON_NULL(iterator, "Message iterator");
d94d92ac 993 return (void *) iterator->upstream_component;
413bc2c4
JG
994}
995
50e763f6 996struct bt_self_component_port_output *bt_self_message_iterator_borrow_port(
d6e69534 997 struct bt_self_message_iterator *self_iterator)
91457551 998{
d6e69534 999 struct bt_self_component_port_input_message_iterator *iterator =
d94d92ac
PP
1000 (void *) self_iterator;
1001
bdb288b3 1002 BT_ASSERT_PRE_DEV_NON_NULL(iterator, "Message iterator");
d94d92ac 1003 return (void *) iterator->upstream_port;
91457551 1004}
8ed535b5 1005
f2fb1b32
SM
1006enum bt_message_iterator_can_seek_ns_from_origin_status
1007bt_self_component_port_input_message_iterator_can_seek_ns_from_origin(
7474e7d3 1008 struct bt_self_component_port_input_message_iterator *iterator,
f2fb1b32 1009 int64_t ns_from_origin, bt_bool *can_seek)
7474e7d3 1010{
f2fb1b32 1011 enum bt_message_iterator_can_seek_ns_from_origin_status status;
7474e7d3 1012
17f3083a 1013 BT_ASSERT_PRE_NO_ERROR();
7474e7d3 1014 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
f2fb1b32 1015 BT_ASSERT_PRE_NON_NULL(can_seek, "Result (output)");
7474e7d3 1016 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator);
5badd463
PP
1017 BT_ASSERT_PRE(
1018 bt_component_borrow_graph(iterator->upstream_component)->config_state !=
1019 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
7474e7d3
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) {
f2fb1b32
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
c0e46a7c
SM
1030 BT_LIB_LOGD("Calling user's \"can seek nanoseconds from origin\" method: %!+i",
1031 iterator);
1032
f2fb1b32
SM
1033 status = (int) iterator->methods.can_seek_ns_from_origin(iterator,
1034 ns_from_origin, can_seek);
1035
6ecdcca3
SM
1036 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status);
1037
c0e46a7c
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,
f2fb1b32
SM
1047 "Unexpected boolean value returned from user's \"can seek ns from origin\" method: val=%d, %![iter-]+i",
1048 *can_seek, iterator);
1049
c0e46a7c
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 }
7474e7d3
PP
1058 }
1059
1060 /*
c0e46a7c
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.
7474e7d3 1064 */
f2fb1b32
SM
1065 status = (int) bt_self_component_port_input_message_iterator_can_seek_beginning(
1066 iterator, can_seek);
c0e46a7c
SM
1067 if (status != BT_FUNC_STATUS_OK) {
1068 goto end;
1069 }
1070
1071 *can_seek = *can_seek && iterator->config.can_seek_forward;
7474e7d3
PP
1072
1073end:
f2fb1b32 1074 return status;
7474e7d3
PP
1075}
1076
f2fb1b32
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)
7474e7d3 1081{
f2fb1b32 1082 enum bt_message_iterator_can_seek_beginning_status status;
7474e7d3 1083
17f3083a 1084 BT_ASSERT_PRE_NO_ERROR();
7474e7d3 1085 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
f2fb1b32 1086 BT_ASSERT_PRE_NON_NULL(can_seek, "Result (output)");
7474e7d3 1087 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator);
5badd463
PP
1088 BT_ASSERT_PRE(
1089 bt_component_borrow_graph(iterator->upstream_component)->config_state !=
1090 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
7474e7d3
PP
1091 "Graph is not configured: %!+g",
1092 bt_component_borrow_graph(iterator->upstream_component));
1093
1094 if (iterator->methods.can_seek_beginning) {
f2fb1b32
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);
6ecdcca3 1109 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status);
f2fb1b32
SM
1110 } else {
1111 *can_seek = BT_FALSE;
1112 status = BT_FUNC_STATUS_OK;
7474e7d3
PP
1113 }
1114
f2fb1b32 1115 return status;
7474e7d3
PP
1116}
1117
1118static inline
003e713f 1119void set_iterator_state_after_seeking(
7474e7d3 1120 struct bt_self_component_port_input_message_iterator *iterator,
d24d5663 1121 int status)
7474e7d3
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) {
d24d5663 1127 case BT_FUNC_STATUS_OK:
7474e7d3
PP
1128 new_state = BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE;
1129 break;
d24d5663 1130 case BT_FUNC_STATUS_AGAIN:
7474e7d3
PP
1131 new_state = BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN;
1132 break;
d24d5663
PP
1133 case BT_FUNC_STATUS_ERROR:
1134 case BT_FUNC_STATUS_MEMORY_ERROR:
7474e7d3
PP
1135 new_state = BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR;
1136 break;
d24d5663 1137 case BT_FUNC_STATUS_END:
7474e7d3
PP
1138 new_state = BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED;
1139 break;
1140 default:
498e7994 1141 bt_common_abort();
7474e7d3
PP
1142 }
1143
1144 set_self_comp_port_input_msg_iterator_state(iterator, new_state);
1145}
1146
54b135a0
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
f2fb1b32
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
d24d5663 1171enum bt_message_iterator_seek_beginning_status
7474e7d3
PP
1172bt_self_component_port_input_message_iterator_seek_beginning(
1173 struct bt_self_component_port_input_message_iterator *iterator)
1174{
9275bef4 1175 int status;
7474e7d3 1176
17f3083a 1177 BT_ASSERT_PRE_NO_ERROR();
7474e7d3
PP
1178 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
1179 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator);
5badd463
PP
1180 BT_ASSERT_PRE(
1181 bt_component_borrow_graph(iterator->upstream_component)->config_state !=
1182 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
7474e7d3
PP
1183 "Graph is not configured: %!+g",
1184 bt_component_borrow_graph(iterator->upstream_component));
f2fb1b32 1185 BT_ASSERT_PRE(message_iterator_can_seek_beginning(iterator),
7474e7d3 1186 "Message iterator cannot seek beginning: %!+i", iterator);
54b135a0
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
7474e7d3
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",
d24d5663
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,
7474e7d3 1204 "Unexpected status: %![iter-]+i, status=%s",
d24d5663 1205 iterator, bt_common_func_status_string(status));
6ecdcca3 1206 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status);
870631a2
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
7474e7d3
PP
1214 set_iterator_state_after_seeking(iterator, status);
1215 return status;
1216}
1217
8d8b141d
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
5b7b55be
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 --->
188edac1 1236 * [SB] 1 [PB] 2 [PE] 1 [SE]
5b7b55be
SM
1237 *
1238 * At each point in the timeline, the messages we need to replicate are:
1239 *
1240 * 1: Stream beginning
188edac1 1241 * 2: Stream beginning, packet beginning
5b7b55be
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,
5b7b55be
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;
188edac1
SM
1257
1258 /* Have we see a message with a clock snapshot yet? */
1259 bool seen_clock_snapshot;
5b7b55be
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
7474e7d3 1299static inline
d24d5663 1300int auto_seek_handle_message(
5b9e151d
PP
1301 struct bt_self_component_port_input_message_iterator *iterator,
1302 int64_t ns_from_origin, const struct bt_message *msg,
5b7b55be 1303 bool *got_first, GHashTable *stream_states)
7474e7d3 1304{
d24d5663 1305 int status = BT_FUNC_STATUS_OK;
5b9e151d 1306 int64_t msg_ns_from_origin;
7474e7d3 1307 const struct bt_clock_snapshot *clk_snapshot = NULL;
5b9e151d
PP
1308 int ret;
1309
98b15851
PP
1310 BT_ASSERT_DBG(msg);
1311 BT_ASSERT_DBG(got_first);
7474e7d3
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
2c091c04 1319 clk_snapshot = event_msg->default_cs;
bdb288b3 1320 BT_ASSERT_POST_DEV(clk_snapshot,
c7072d5a
PP
1321 "Event message has no default clock snapshot: %!+n",
1322 event_msg);
7474e7d3
PP
1323 break;
1324 }
b9fd9cbb 1325 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY:
7474e7d3 1326 {
b9fd9cbb 1327 const struct bt_message_message_iterator_inactivity *inactivity_msg =
7474e7d3
PP
1328 (const void *) msg;
1329
7474e7d3 1330 clk_snapshot = inactivity_msg->default_cs;
98b15851 1331 BT_ASSERT_DBG(clk_snapshot);
7474e7d3
PP
1332 break;
1333 }
16663a5e
PP
1334 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
1335 case BT_MESSAGE_TYPE_PACKET_END:
c7072d5a
PP
1336 {
1337 const struct bt_message_packet *packet_msg =
1338 (const void *) msg;
1339
1340 clk_snapshot = packet_msg->default_cs;
bdb288b3 1341 BT_ASSERT_POST_DEV(clk_snapshot,
c7072d5a
PP
1342 "Packet message has no default clock snapshot: %!+n",
1343 packet_msg);
1344 break;
1345 }
16663a5e
PP
1346 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
1347 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
7474e7d3 1348 {
5b9e151d
PP
1349 struct bt_message_discarded_items *msg_disc_items =
1350 (void *) msg;
1351
bdb288b3 1352 BT_ASSERT_POST_DEV(msg_disc_items->default_begin_cs &&
5b9e151d
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) {
d24d5663 1360 status = BT_FUNC_STATUS_ERROR;
5b9e151d
PP
1361 goto end;
1362 }
7474e7d3 1363
5b9e151d
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) {
d24d5663 1373 status = BT_FUNC_STATUS_ERROR;
5b9e151d
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 */
4af85094 1387 uint64_t new_begin_raw_value = 0;
5b9e151d
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) {
d24d5663 1393 status = BT_FUNC_STATUS_ERROR;
5b9e151d
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 }
7474e7d3 1411 }
188edac1
SM
1412 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
1413 case BT_MESSAGE_TYPE_STREAM_END:
7474e7d3 1414 {
188edac1
SM
1415 struct bt_message_stream *stream_msg =
1416 (struct bt_message_stream *) msg;
7474e7d3 1417
188edac1
SM
1418 if (stream_msg->default_cs_state != BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN) {
1419 /* Ignore */
5b9e151d 1420 goto skip_msg;
16663a5e
PP
1421 }
1422
188edac1 1423 clk_snapshot = stream_msg->default_cs;
7474e7d3
PP
1424 break;
1425 }
1426 default:
498e7994 1427 bt_common_abort();
7474e7d3
PP
1428 }
1429
98b15851 1430 BT_ASSERT_DBG(clk_snapshot);
5b9e151d
PP
1431 ret = bt_clock_snapshot_get_ns_from_origin(clk_snapshot,
1432 &msg_ns_from_origin);
1433 if (ret) {
d24d5663 1434 status = BT_FUNC_STATUS_ERROR;
7474e7d3
PP
1435 goto end;
1436 }
1437
5b9e151d
PP
1438 if (msg_ns_from_origin >= ns_from_origin) {
1439 *got_first = true;
1440 goto push_msg;
1441 }
1442
1443skip_msg:
5b7b55be
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;
5b7b55be
SM
1450
1451 /* Update stream's state: stream began. */
1452 stream_state = create_auto_seek_stream_state();
1453 if (!stream_state) {
d24d5663 1454 status = BT_FUNC_STATUS_MEMORY_ERROR;
5b7b55be
SM
1455 goto end;
1456 }
1457
1458 stream_state->state = AUTO_SEEK_STREAM_STATE_STREAM_BEGAN;
e74dbb33 1459
188edac1
SM
1460 if (stream_msg->default_cs_state == BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN) {
1461 stream_state->seen_clock_snapshot = true;
1462 }
1463
98b15851 1464 BT_ASSERT_DBG(!bt_g_hash_table_contains(stream_states, stream_msg->stream));
e74dbb33 1465 g_hash_table_insert(stream_states, stream_msg->stream, stream_state);
5b7b55be
SM
1466 break;
1467 }
188edac1 1468 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
5b7b55be 1469 {
188edac1 1470 const struct bt_message_packet *packet_msg =
5b7b55be
SM
1471 (const void *) msg;
1472 struct auto_seek_stream_state *stream_state;
1473
188edac1
SM
1474 /* Update stream's state: packet began. */
1475 stream_state = g_hash_table_lookup(stream_states, packet_msg->packet->stream);
98b15851
PP
1476 BT_ASSERT_DBG(stream_state);
1477 BT_ASSERT_DBG(stream_state->state == AUTO_SEEK_STREAM_STATE_STREAM_BEGAN);
188edac1 1478 stream_state->state = AUTO_SEEK_STREAM_STATE_PACKET_BEGAN;
98b15851 1479 BT_ASSERT_DBG(!stream_state->packet);
188edac1
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
5b7b55be
SM
1486 break;
1487 }
188edac1 1488 case BT_MESSAGE_TYPE_EVENT:
5b7b55be 1489 {
188edac1 1490 const struct bt_message_event *event_msg = (const void *) msg;
5b7b55be
SM
1491 struct auto_seek_stream_state *stream_state;
1492
188edac1
SM
1493 stream_state = g_hash_table_lookup(stream_states,
1494 event_msg->event->packet->stream);
98b15851 1495 BT_ASSERT_DBG(stream_state);
5b7b55be 1496
188edac1
SM
1497 // HELPME: are we sure that event messages have clock snapshots at this point?
1498 stream_state->seen_clock_snapshot = true;
1499
5b7b55be
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);
98b15851
PP
1510 BT_ASSERT_DBG(stream_state);
1511 BT_ASSERT_DBG(stream_state->state == AUTO_SEEK_STREAM_STATE_PACKET_BEGAN);
188edac1 1512 stream_state->state = AUTO_SEEK_STREAM_STATE_STREAM_BEGAN;
98b15851 1513 BT_ASSERT_DBG(stream_state->packet);
5b7b55be 1514 stream_state->packet = NULL;
5b7b55be 1515
188edac1
SM
1516 if (packet_msg->packet->stream->class->packets_have_end_default_clock_snapshot) {
1517 stream_state->seen_clock_snapshot = true;
1518 }
5b7b55be 1519
5b7b55be
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);
98b15851
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);
5b7b55be
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 }
188edac1
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);
98b15851 1544 BT_ASSERT_DBG(stream_state);
188edac1
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 }
5b7b55be
SM
1553 default:
1554 break;
1555 }
1556
6871026b 1557 bt_object_put_ref_no_null_check(msg);
04c0cec6 1558 msg = NULL;
5b9e151d
PP
1559 goto end;
1560
1561push_msg:
da9c4c52 1562 g_queue_push_tail(iterator->auto_seek.msgs, (void *) msg);
5b9e151d 1563 msg = NULL;
7474e7d3
PP
1564
1565end:
98b15851 1566 BT_ASSERT_DBG(!msg || status != BT_FUNC_STATUS_OK);
5b9e151d 1567 return status;
7474e7d3
PP
1568}
1569
1570static
d24d5663 1571int find_message_ge_ns_from_origin(
7474e7d3 1572 struct bt_self_component_port_input_message_iterator *iterator,
5b7b55be 1573 int64_t ns_from_origin, GHashTable *stream_states)
7474e7d3 1574{
c70a96a6 1575 int status = BT_FUNC_STATUS_OK;
7474e7d3
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;
5b9e151d 1581 bool got_first = false;
7474e7d3 1582
98b15851 1583 BT_ASSERT_DBG(iterator);
7474e7d3
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
98b15851 1593 BT_ASSERT_DBG(iterator->methods.next);
7474e7d3 1594
e0dade92 1595 while (!got_first) {
7474e7d3
PP
1596 /*
1597 * Call the user's "next" method to get the next
1598 * messages and status.
1599 */
54b135a0 1600 status = call_iterator_next_method(iterator,
7474e7d3 1601 &messages[0], MSG_BATCH_SIZE, &user_count);
870631a2
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 }
7474e7d3 1610
7474e7d3
PP
1611 /*
1612 * The user's "next" method must not do any action which
1613 * would change the iterator's state.
1614 */
98b15851 1615 BT_ASSERT_DBG(iterator->state ==
7474e7d3 1616 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE);
7474e7d3
PP
1617
1618 switch (status) {
d24d5663 1619 case BT_FUNC_STATUS_OK:
bdb288b3 1620 BT_ASSERT_POST_DEV(user_count <= MSG_BATCH_SIZE,
7474e7d3
PP
1621 "Invalid returned message count: greater than "
1622 "batch size: count=%" PRIu64 ", batch-size=%u",
1623 user_count, MSG_BATCH_SIZE);
1624 break;
d24d5663
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:
7474e7d3
PP
1629 goto end;
1630 default:
498e7994 1631 bt_common_abort();
7474e7d3
PP
1632 }
1633
7474e7d3 1634 for (i = 0; i < user_count; i++) {
5b9e151d 1635 if (got_first) {
da9c4c52 1636 g_queue_push_tail(iterator->auto_seek.msgs,
5b9e151d
PP
1637 (void *) messages[i]);
1638 messages[i] = NULL;
7474e7d3
PP
1639 continue;
1640 }
1641
5b9e151d 1642 status = auto_seek_handle_message(iterator,
5b7b55be
SM
1643 ns_from_origin, messages[i], &got_first,
1644 stream_states);
d24d5663 1645 if (status == BT_FUNC_STATUS_OK) {
e0dade92 1646 /* Message was either pushed or moved */
5b9e151d
PP
1647 messages[i] = NULL;
1648 } else {
7474e7d3
PP
1649 goto end;
1650 }
7474e7d3
PP
1651 }
1652 }
1653
1654end:
1655 for (i = 0; i < user_count; i++) {
1656 if (messages[i]) {
6871026b 1657 bt_object_put_ref_no_null_check(messages[i]);
7474e7d3
PP
1658 }
1659 }
1660
1661 set_self_comp_port_input_msg_iterator_state(iterator, init_state);
1662 return status;
1663}
1664
5b7b55be
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
7474e7d3 1672static
d24d5663 1673enum bt_component_class_message_iterator_next_method_status post_auto_seek_next(
7474e7d3
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{
da9c4c52 1678 BT_ASSERT(!g_queue_is_empty(iterator->auto_seek.msgs));
5b9e151d 1679 *count = 0;
7474e7d3
PP
1680
1681 /*
1682 * Move auto-seek messages to the output array (which is this
5b9e151d 1683 * iterator's base message array).
7474e7d3 1684 */
da9c4c52
SM
1685 while (capacity > 0 && !g_queue_is_empty(iterator->auto_seek.msgs)) {
1686 msgs[*count] = g_queue_pop_head(iterator->auto_seek.msgs);
5b9e151d
PP
1687 capacity--;
1688 (*count)++;
7474e7d3 1689 }
7474e7d3 1690
5b9e151d
PP
1691 BT_ASSERT(*count > 0);
1692
da9c4c52 1693 if (g_queue_is_empty(iterator->auto_seek.msgs)) {
572075a8
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;
7474e7d3
PP
1698 }
1699
d24d5663 1700 return BT_FUNC_STATUS_OK;
7474e7d3
PP
1701}
1702
5b7b55be
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
f2fb1b32
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}
5b7b55be 1732
d24d5663 1733enum bt_message_iterator_seek_ns_from_origin_status
7474e7d3
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;
5b7b55be 1739 GHashTable *stream_states = NULL;
c0e46a7c 1740 bt_bool can_seek_by_itself;
7474e7d3 1741
17f3083a 1742 BT_ASSERT_PRE_NO_ERROR();
7474e7d3
PP
1743 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
1744 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator);
5badd463
PP
1745 BT_ASSERT_PRE(
1746 bt_component_borrow_graph(iterator->upstream_component)->config_state !=
1747 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
7474e7d3
PP
1748 "Graph is not configured: %!+g",
1749 bt_component_borrow_graph(iterator->upstream_component));
c0e46a7c 1750 /* The iterator must be able to seek ns from origin one way or another. */
7474e7d3 1751 BT_ASSERT_PRE(
f2fb1b32 1752 message_iterator_can_seek_ns_from_origin(iterator, ns_from_origin),
7474e7d3
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
54b135a0
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
c0e46a7c
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) {
5b7b55be 1781 /* The iterator knows how to seek to a particular time, let it handle this. */
2e1b5615 1782 BT_ASSERT(iterator->methods.seek_ns_from_origin);
7474e7d3
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",
d24d5663
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,
7474e7d3 1793 "Unexpected status: %![iter-]+i, status=%s",
d24d5663 1794 iterator, bt_common_func_status_string(status));
6ecdcca3 1795 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status);
870631a2
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 }
7474e7d3 1802 } else {
5b7b55be 1803 /*
c0e46a7c
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.
5b7b55be 1807 */
f2fb1b32
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);
7474e7d3
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",
d24d5663
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,
7474e7d3 1826 "Unexpected status: %![iter-]+i, status=%s",
d24d5663 1827 iterator, bt_common_func_status_string(status));
870631a2
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
7474e7d3 1835 switch (status) {
d24d5663 1836 case BT_FUNC_STATUS_OK:
7474e7d3 1837 break;
d24d5663
PP
1838 case BT_FUNC_STATUS_ERROR:
1839 case BT_FUNC_STATUS_MEMORY_ERROR:
1840 case BT_FUNC_STATUS_AGAIN:
7474e7d3
PP
1841 goto end;
1842 default:
498e7994 1843 bt_common_abort();
7474e7d3
PP
1844 }
1845
1846 /*
1847 * Find the first message which has a default clock
1848 * snapshot greater than or equal to the requested
5b9e151d
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.
7474e7d3 1852 */
da9c4c52 1853 while (!g_queue_is_empty(iterator->auto_seek.msgs)) {
6871026b 1854 bt_object_put_ref_no_null_check(
da9c4c52 1855 g_queue_pop_tail(iterator->auto_seek.msgs));
5b9e151d
PP
1856 }
1857
5b7b55be
SM
1858 stream_states = create_auto_seek_stream_states();
1859 if (!stream_states) {
870631a2
PP
1860 BT_LIB_LOGE_APPEND_CAUSE(
1861 "Failed to allocate one GHashTable.");
d24d5663 1862 status = BT_FUNC_STATUS_MEMORY_ERROR;
5b7b55be
SM
1863 goto end;
1864 }
1865
7474e7d3 1866 status = find_message_ge_ns_from_origin(iterator,
5b7b55be 1867 ns_from_origin, stream_states);
7474e7d3 1868 switch (status) {
d24d5663
PP
1869 case BT_FUNC_STATUS_OK:
1870 case BT_FUNC_STATUS_END:
5b7b55be
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));
188edac1
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 }
5b7b55be
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);
188edac1
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);
5b7b55be
SM
1928 }
1929
5b7b55be 1930 if (!msg) {
d24d5663 1931 status = BT_FUNC_STATUS_MEMORY_ERROR;
5b7b55be
SM
1932 goto end;
1933 }
1934
5b7b55be
SM
1935 g_queue_push_head(iterator->auto_seek.msgs, msg);
1936 msg = NULL;
1937 /* fall-thru */
188edac1 1938
5b7b55be
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) {
d24d5663 1943 status = BT_FUNC_STATUS_MEMORY_ERROR;
5b7b55be
SM
1944 goto end;
1945 }
1946
188edac1
SM
1947 if (stream_state->seen_clock_snapshot) {
1948 bt_message_stream_beginning_set_default_clock_snapshot(msg, raw_value);
1949 }
1950
5b7b55be
SM
1951 g_queue_push_head(iterator->auto_seek.msgs, msg);
1952 msg = NULL;
1953 break;
1954 }
1955 }
1956
7474e7d3 1957 /*
5b9e151d
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.
7474e7d3 1962 */
da9c4c52 1963 if (!g_queue_is_empty(iterator->auto_seek.msgs)) {
572075a8
SM
1964 BT_ASSERT(!iterator->auto_seek.original_next_callback);
1965 iterator->auto_seek.original_next_callback = iterator->methods.next;
1966
5b9e151d
PP
1967 iterator->methods.next =
1968 (bt_self_component_port_input_message_iterator_next_method)
1969 post_auto_seek_next;
1970 }
1971
1972 /*
d24d5663
PP
1973 * `BT_FUNC_STATUS_END` becomes
1974 * `BT_FUNC_STATUS_OK`: the next
5b9e151d
PP
1975 * time this iterator's "next" method is called,
1976 * it will return
d24d5663 1977 * `BT_FUNC_STATUS_END`.
5b9e151d 1978 */
d24d5663 1979 status = BT_FUNC_STATUS_OK;
7474e7d3 1980 break;
5b7b55be 1981 }
d24d5663
PP
1982 case BT_FUNC_STATUS_ERROR:
1983 case BT_FUNC_STATUS_MEMORY_ERROR:
1984 case BT_FUNC_STATUS_AGAIN:
7474e7d3 1985 goto end;
7474e7d3 1986 default:
498e7994 1987 bt_common_abort();
7474e7d3
PP
1988 }
1989 }
1990
54b135a0
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
7474e7d3 1997end:
5b7b55be
SM
1998 if (stream_states) {
1999 destroy_auto_seek_stream_states(stream_states);
2000 stream_states = NULL;
2001 }
870631a2 2002
7474e7d3 2003 set_iterator_state_after_seeking(iterator, status);
7474e7d3
PP
2004 return status;
2005}
2006
9b4f9b42
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
d6e69534
PP
2017void bt_self_component_port_input_message_iterator_get_ref(
2018 const struct bt_self_component_port_input_message_iterator *iterator)
c5b9b441
PP
2019{
2020 bt_object_get_ref(iterator);
2021}
2022
d6e69534
PP
2023void bt_self_component_port_input_message_iterator_put_ref(
2024 const struct bt_self_component_port_input_message_iterator *iterator)
c5b9b441
PP
2025{
2026 bt_object_put_ref(iterator);
2027}
This page took 0.176996 seconds and 4 git commands to generate.