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