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