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