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