lib/graph/iterator.c: use `BT_IF_DEV_MODE` at a few places
[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{
680 enum bt_message_type message_type = bt_message_get_type(msg);
54b135a0
SM
681
682 if (message_type == BT_MESSAGE_TYPE_STREAM_BEGINNING) {
683 const struct bt_message_stream *stream_msg = (struct bt_message_stream *) msg;
684 const struct bt_clock_class *clock_class = stream_msg->stream->class->default_clock_class;
685 bt_uuid clock_class_uuid = NULL;
686
687 if (clock_class) {
688 clock_class_uuid = bt_clock_class_get_uuid(clock_class);
689 }
690
691 switch (iterator->clock_expectation.type) {
692 case CLOCK_EXPECTATION_UNSET:
693 /*
694 * This is the first time we see a message with a clock
695 * snapshot: record the properties of that clock, against
696 * which we'll compare the clock properties of the following
697 * messages.
698 */
699
700 if (!clock_class) {
701 iterator->clock_expectation.type = CLOCK_EXPECTATION_NONE;
702 } else if (bt_clock_class_origin_is_unix_epoch(clock_class)) {
703 iterator->clock_expectation.type = CLOCK_EXPECTATION_ORIGIN_UNIX;
704 } else if (clock_class_uuid) {
705 iterator->clock_expectation.type = CLOCK_EXPECTATION_ORIGIN_OTHER_UUID;
6162e6b7 706 bt_uuid_copy(iterator->clock_expectation.uuid, clock_class_uuid);
54b135a0
SM
707 } else {
708 iterator->clock_expectation.type = CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID;
29e191fc
SM
709 iterator->clock_expectation.clock_class = clock_class;
710 bt_clock_class_get_ref(iterator->clock_expectation.clock_class);
54b135a0
SM
711 }
712 break;
713
714 case CLOCK_EXPECTATION_NONE:
217fd08c
SM
715 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME,
716 "stream-class-has-no-clock-class", !clock_class,
717 "Expecting no clock class, got one: %![cc-]+K",
718 clock_class);
54b135a0
SM
719 break;
720
721 case CLOCK_EXPECTATION_ORIGIN_UNIX:
217fd08c
SM
722 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME,
723 "stream-class-has-clock-class-with-unix-epoch-origin", clock_class,
724 "Expecting a clock class with Unix epoch origin, got none.");
54b135a0 725
217fd08c
SM
726 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME,
727 "clock-class-has-unix-epoch-origin",
728 bt_clock_class_origin_is_unix_epoch(clock_class),
729 "Expecting a clock class with Unix epoch origin: %![cc-]+K",
730 clock_class);
54b135a0
SM
731 break;
732
733 case CLOCK_EXPECTATION_ORIGIN_OTHER_UUID:
217fd08c
SM
734 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME,
735 "stream-class-has-clock-class-with-uuid", clock_class,
736 "Expecting a clock class with UUID, got none.");
54b135a0 737
217fd08c
SM
738 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME,
739 "clock-class-has-non-unix-epoch-origin",
740 !bt_clock_class_origin_is_unix_epoch(clock_class),
741 "Expecting a clock class without Unix epoch origin: %![cc-]+K",
742 clock_class);
54b135a0 743
217fd08c
SM
744 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME,
745 "clock-class-has-uuid",
746 clock_class_uuid,
747 "Expecting a clock class with UUID, got one without UUID: %![cc-]+K",
748 clock_class);
54b135a0 749
217fd08c
SM
750 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME,
751 "clock-class-has-expected-uuid",
752 !bt_uuid_compare(iterator->clock_expectation.uuid, clock_class_uuid),
753 "Expecting a clock class with UUID, got one "
54b135a0
SM
754 "with a different UUID: %![cc-]+K, expected-uuid=%!u",
755 clock_class, iterator->clock_expectation.uuid);
54b135a0
SM
756 break;
757
758 case CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID:
217fd08c
SM
759 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME,
760 "stream-class-has-clock-class", clock_class,
761 "Expecting a clock class, got none.");
54b135a0 762
217fd08c
SM
763 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME,
764 "clock-class-is-expected",
765 clock_class == iterator->clock_expectation.clock_class,
766 "Expecting clock class %![cc-]+K, got %![cc-]+K.",
767 iterator->clock_expectation.clock_class,
768 clock_class);
54b135a0
SM
769 break;
770 }
771 }
54b135a0
SM
772}
773
54b135a0 774static
217fd08c 775void assert_post_dev_clock_classes_are_compatible(
9a2c8b8e 776 struct bt_message_iterator *iterator,
54b135a0
SM
777 bt_message_array_const msgs, uint64_t msg_count)
778{
779 uint64_t i;
54b135a0
SM
780
781 for (i = 0; i < msg_count; i++) {
217fd08c 782 assert_post_dev_clock_classes_are_compatible_one(iterator, msgs[i]);
54b135a0 783 }
54b135a0
SM
784}
785
9340eff9
SM
786static
787const bt_stream *get_stream_from_msg(const struct bt_message *msg)
788{
789 struct bt_stream *stream;
790
791 switch (msg->type) {
792 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
793 case BT_MESSAGE_TYPE_STREAM_END:
794 {
795 struct bt_message_stream *msg_stream =
796 (struct bt_message_stream *) msg;
797 stream = msg_stream->stream;
798 break;
799 }
800 case BT_MESSAGE_TYPE_EVENT:
801 {
802 struct bt_message_event *msg_event =
803 (struct bt_message_event *) msg;
804 stream = msg_event->event->stream;
805 break;
806 }
807 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
808 case BT_MESSAGE_TYPE_PACKET_END:
809 {
810 struct bt_message_packet *msg_packet =
811 (struct bt_message_packet *) msg;
812 stream = msg_packet->packet->stream;
813 break;
814 }
815 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
816 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
817 {
818 struct bt_message_discarded_items *msg_discarded =
819 (struct bt_message_discarded_items *) msg;
820 stream = msg_discarded->stream;
821 break;
822 }
823 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY:
824 stream = NULL;
825 break;
826 default:
827 bt_common_abort();
828 }
829
830 return stream;
831}
832
0121936a
SM
833static
834GString *message_types_to_string(guint msg_types)
835{
836 GString *str = g_string_new("");
837
838 for (int msg_type = 1; msg_type <= BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY;
839 msg_type <<= 1) {
840 if (msg_type & msg_types) {
841 if (str->len > 0) {
842 g_string_append_c(str, '|');
843 }
844
845 g_string_append(str,
846 bt_common_message_type_string(msg_type));
847 }
848 }
849
850 return str;
851}
852
853static
854void update_expected_msg_type(const struct bt_stream *stream,
855 struct per_stream_state *state,
856 const struct bt_message *msg)
857{
858 switch (msg->type) {
859 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
860 state->expected_msg_types = BT_MESSAGE_TYPE_STREAM_END;
861
862 if (stream->class->supports_packets) {
863 state->expected_msg_types |=
864 BT_MESSAGE_TYPE_PACKET_BEGINNING;
865
866 if (stream->class->supports_discarded_packets) {
867 state->expected_msg_types |=
868 BT_MESSAGE_TYPE_DISCARDED_PACKETS;
869 }
870 } else {
871 state->expected_msg_types |= BT_MESSAGE_TYPE_EVENT;
872 }
873
874 if (stream->class->supports_discarded_events) {
875 state->expected_msg_types |=
876 BT_MESSAGE_TYPE_DISCARDED_EVENTS;
877 }
878
879 break;
880 case BT_MESSAGE_TYPE_STREAM_END:
881 state->expected_msg_types = 0;
882 break;
883 case BT_MESSAGE_TYPE_EVENT:
884 {
885 state->expected_msg_types = BT_MESSAGE_TYPE_EVENT;
886
887 if (stream->class->supports_packets) {
888 state->expected_msg_types |= BT_MESSAGE_TYPE_PACKET_END;
889 } else {
890 state->expected_msg_types |= BT_MESSAGE_TYPE_STREAM_END;
891 }
892
893 if (stream->class->supports_discarded_events) {
894 state->expected_msg_types |=
895 BT_MESSAGE_TYPE_DISCARDED_EVENTS;
896 }
897
898 break;
899 }
900 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
901 {
902 state->expected_msg_types = BT_MESSAGE_TYPE_EVENT |
903 BT_MESSAGE_TYPE_PACKET_END;
904
905 if (stream->class->supports_discarded_events) {
906 state->expected_msg_types |=
907 BT_MESSAGE_TYPE_DISCARDED_EVENTS;
908 }
909
910 break;
911 }
912 case BT_MESSAGE_TYPE_PACKET_END:
913 {
914 state->expected_msg_types = BT_MESSAGE_TYPE_PACKET_BEGINNING |
915 BT_MESSAGE_TYPE_STREAM_END;
916
917 if (stream->class->supports_discarded_events) {
918 state->expected_msg_types |=
919 BT_MESSAGE_TYPE_DISCARDED_EVENTS;
920 }
921
922 if (stream->class->supports_discarded_packets) {
923 state->expected_msg_types |=
924 BT_MESSAGE_TYPE_DISCARDED_PACKETS;
925 }
926
927 break;
928 }
929 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
930 state->expected_msg_types = BT_MESSAGE_TYPE_DISCARDED_EVENTS;
931
932 if (state->cur_packet) {
933 state->expected_msg_types |= BT_MESSAGE_TYPE_EVENT |
934 BT_MESSAGE_TYPE_PACKET_END;
935 } else {
936 state->expected_msg_types |= BT_MESSAGE_TYPE_STREAM_END;
937
938 if (stream->class->supports_packets) {
939 state->expected_msg_types |=
940 BT_MESSAGE_TYPE_PACKET_BEGINNING;
941
942 if (stream->class->supports_discarded_packets) {
943 state->expected_msg_types |=
944 BT_MESSAGE_TYPE_DISCARDED_PACKETS;
945 }
946 } else {
947 state->expected_msg_types |=
948 BT_MESSAGE_TYPE_EVENT;
949 }
950 }
951
952 break;
953 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
954 state->expected_msg_types = BT_MESSAGE_TYPE_DISCARDED_PACKETS |
955 BT_MESSAGE_TYPE_PACKET_BEGINNING |
956 BT_MESSAGE_TYPE_STREAM_END;
957
958 if (stream->class->supports_discarded_events) {
959 state->expected_msg_types |=
960 BT_MESSAGE_TYPE_DISCARDED_EVENTS;
961 }
962 break;
963 default:
964 /*
965 * Other message types are not associated to a stream, so we
966 * should not get them here.
967 */
968 bt_common_abort();
969 }
970}
971
9340eff9
SM
972static
973struct per_stream_state *get_per_stream_state(
974 struct bt_message_iterator *iterator,
975 const struct bt_stream *stream)
976{
977 struct per_stream_state *state = g_hash_table_lookup(
978 iterator->per_stream_state, stream);
979
980 if (!state) {
981 state = g_new0(struct per_stream_state, 1);
0121936a 982 state->expected_msg_types = BT_MESSAGE_TYPE_STREAM_BEGINNING;
9340eff9
SM
983 g_hash_table_insert(iterator->per_stream_state,
984 (gpointer) stream, state);
985 }
986
987 return state;
988}
9340eff9 989
0121936a
SM
990static
991void assert_post_dev_expected_sequence(struct bt_message_iterator *iterator,
992 const struct bt_message *msg)
993{
994 const bt_stream *stream = get_stream_from_msg(msg);
995 struct per_stream_state *state;
996
997 if (!stream) {
998 goto end;
999 }
1000
1001 state = get_per_stream_state(iterator, stream);
1002
1003 /*
1004 * We don't free the return value of message_types_to_string(), but
1005 * that's because we know the program is going to abort anyway, and
1006 * we don't want to call it if the assertion holds.
1007 */
1008 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME,
1009 "message-type-is-expected",
1010 msg->type & state->expected_msg_types,
1011 "Unexpected message type: %![stream-]s, %![iterator-]i, "
1012 "%![message-]n, expected-msg-types=%s",
1013 stream, iterator, msg,
1014 message_types_to_string(state->expected_msg_types)->str);
1015
1016 update_expected_msg_type(stream, state, msg);
1017
1018end:
1019 return;
1020}
1021
9340eff9
SM
1022static
1023void assert_post_dev_expected_packet(struct bt_message_iterator *iterator,
1024 const struct bt_message *msg)
1025{
1026 const bt_stream *stream = get_stream_from_msg(msg);
1027 struct per_stream_state *state;
1028 const bt_packet *actual_packet = NULL;
1029 const bt_packet *expected_packet = NULL;
1030
1031 if (!stream) {
1032 goto end;
1033 }
1034
1035 state = get_per_stream_state(iterator, stream);
1036
1037 switch (msg->type) {
1038 case BT_MESSAGE_TYPE_EVENT:
1039 {
1040 const struct bt_message_event *msg_event =
1041 (const struct bt_message_event *) msg;
1042
1043 actual_packet = msg_event->event->packet;
1044 expected_packet = state->cur_packet;
1045 break;
1046 }
1047 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
1048 {
1049 const struct bt_message_packet *msg_packet =
1050 (const struct bt_message_packet *) msg;
1051
1052 BT_ASSERT(!state->cur_packet);
1053 state->cur_packet = msg_packet->packet;
1054 break;
1055 }
1056 case BT_MESSAGE_TYPE_PACKET_END:
1057 {
1058 const struct bt_message_packet *msg_packet =
1059 (const struct bt_message_packet *) msg;
1060
1061 actual_packet = msg_packet->packet;
1062 expected_packet = state->cur_packet;
1063 BT_ASSERT(state->cur_packet);
1064 state->cur_packet = NULL;
1065 break;
1066 }
1067 default:
1068 break;
1069 }
1070
1071 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME,
1072 "message-packet-is-expected",
1073 actual_packet == expected_packet,
1074 "Message's packet is not expected: %![stream-]s, %![iterator-]i, "
1075 "%![message-]n, %![received-packet-]a, %![expected-packet-]a",
1076 stream, iterator, msg, actual_packet, expected_packet);
1077
1078end:
1079 return;
1080}
1081
1082static
1083void assert_post_dev_next(
1084 struct bt_message_iterator *iterator,
1085 bt_message_iterator_class_next_method_status status,
1086 bt_message_array_const msgs, uint64_t msg_count)
1087{
1088 if (status == BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_OK) {
1089 uint64_t i;
1090
1091 for (i = 0; i < msg_count; i++) {
0121936a 1092 assert_post_dev_expected_sequence(iterator, msgs[i]);
9340eff9
SM
1093 assert_post_dev_expected_packet(iterator, msgs[i]);
1094 }
0121936a
SM
1095 } else if (status == BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END) {
1096 GHashTableIter iter;
1097
1098 gpointer stream_v, stream_state_v;
1099
1100 g_hash_table_iter_init(&iter, iterator->per_stream_state);
1101 while (g_hash_table_iter_next(&iter, &stream_v,
1102 &stream_state_v)) {
1103 struct bt_stream *stream = stream_v;
1104 struct per_stream_state *stream_state = stream_state_v;
1105
1106 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME,
1107 "stream-is-ended",
1108 stream_state->expected_msg_types == 0,
1109 "Stream is not ended: %![stream-]s, "
1110 "%![iterator-]i, expected-msg-types=%s",
1111 stream, iterator,
1112 message_types_to_string(
1113 stream_state->expected_msg_types)->str);
1114 }
1115
9340eff9
SM
1116 }
1117}
1118#endif
1119
54b135a0
SM
1120/*
1121 * Call the `next` method of the iterator. Do some validation on the returned
1122 * messages.
1123 */
1124
1125static
a3f0c7db 1126enum bt_message_iterator_class_next_method_status
d24d5663 1127call_iterator_next_method(
9a2c8b8e 1128 struct bt_message_iterator *iterator,
54b135a0
SM
1129 bt_message_array_const msgs, uint64_t capacity, uint64_t *user_count)
1130{
a3f0c7db 1131 enum bt_message_iterator_class_next_method_status status;
54b135a0 1132
98b15851 1133 BT_ASSERT_DBG(iterator->methods.next);
54b135a0 1134 BT_LOGD_STR("Calling user's \"next\" method.");
54b135a0 1135 status = iterator->methods.next(iterator, msgs, capacity, user_count);
f6f301d7 1136 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64,
d24d5663 1137 bt_common_func_status_string(status), *user_count);
54b135a0 1138
d24d5663 1139 if (status == BT_FUNC_STATUS_OK) {
217fd08c
SM
1140 BT_IF_DEV_MODE(assert_post_dev_clock_classes_are_compatible(
1141 iterator, msgs, *user_count));
1142
1778c2a4
PP
1143 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME,
1144 "message-clock-snapshots-are-monotonic",
1145 clock_snapshots_are_monotonic(iterator, msgs,
1146 *user_count),
54b135a0
SM
1147 "Clock snapshots are not monotonic");
1148 }
1149
d8451d46
SM
1150 BT_IF_DEV_MODE(assert_post_dev_next(iterator, status, msgs,
1151 *user_count));
9340eff9 1152
1778c2a4
PP
1153 BT_ASSERT_POST_DEV_NO_ERROR_IF_NO_ERROR_STATUS(NEXT_METHOD_NAME,
1154 status);
6ecdcca3 1155
54b135a0
SM
1156 return status;
1157}
1158
1353b066 1159BT_EXPORT
d24d5663 1160enum bt_message_iterator_next_status
9a2c8b8e
PP
1161bt_message_iterator_next(
1162 struct bt_message_iterator *iterator,
d6e69534 1163 bt_message_array_const *msgs, uint64_t *user_count)
3230ee6b 1164{
d24d5663 1165 enum bt_message_iterator_next_status status = BT_FUNC_STATUS_OK;
d94d92ac 1166
17f3083a 1167 BT_ASSERT_PRE_DEV_NO_ERROR();
d5b13b9b 1168 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator);
1778c2a4
PP
1169 BT_ASSERT_PRE_DEV_NON_NULL("message-array-output", msgs,
1170 "Message array (output)");
1171 BT_ASSERT_PRE_DEV_NON_NULL("user-count-output", user_count,
1172 "Message count (output)");
1173 BT_ASSERT_PRE_DEV("message-iterator-is-active",
1174 iterator->state == BT_MESSAGE_ITERATOR_STATE_ACTIVE,
d6e69534 1175 "Message iterator's \"next\" called, but "
7474e7d3 1176 "message iterator is in the wrong state: %!+i", iterator);
98b15851
PP
1177 BT_ASSERT_DBG(iterator->upstream_component);
1178 BT_ASSERT_DBG(iterator->upstream_component->class);
1778c2a4 1179 BT_ASSERT_PRE_DEV("graph-is-configured",
5badd463
PP
1180 bt_component_borrow_graph(iterator->upstream_component)->config_state !=
1181 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
4725a201
PP
1182 "Graph is not configured: %!+g",
1183 bt_component_borrow_graph(iterator->upstream_component));
d94d92ac 1184 BT_LIB_LOGD("Getting next self component input port "
3f7d4d90
PP
1185 "message iterator's messages: %!+i, batch-size=%u",
1186 iterator, MSG_BATCH_SIZE);
d3eb6e8f 1187
3230ee6b 1188 /*
d6e69534 1189 * Call the user's "next" method to get the next messages
fa054faf 1190 * and status.
3230ee6b 1191 */
3f7d4d90 1192 *user_count = 0;
d24d5663 1193 status = (int) call_iterator_next_method(iterator,
6c373cc9 1194 (void *) iterator->msgs->pdata, MSG_BATCH_SIZE,
7474e7d3 1195 user_count);
870631a2
PP
1196 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64,
1197 bt_common_func_status_string(status), *user_count);
d4393e08 1198 if (status < 0) {
870631a2
PP
1199 BT_LIB_LOGW_APPEND_CAUSE(
1200 "Component input port message iterator's \"next\" method failed: "
1201 "%![iter-]+i, status=%s",
1202 iterator, bt_common_func_status_string(status));
f42867e2
PP
1203 goto end;
1204 }
3230ee6b 1205
d0fea130
PP
1206 /*
1207 * There is no way that this iterator could have been finalized
1208 * during its "next" method, as the only way to do this is to
1209 * put the last iterator's reference, and this can only be done
1210 * by its downstream owner.
7474e7d3
PP
1211 *
1212 * For the same reason, there is no way that this iterator could
e7401568 1213 * have sought (cannot seek a self message iterator).
d0fea130 1214 */
98b15851 1215 BT_ASSERT_DBG(iterator->state ==
9a2c8b8e 1216 BT_MESSAGE_ITERATOR_STATE_ACTIVE);
8cf27cc5 1217
d4393e08 1218 switch (status) {
d24d5663 1219 case BT_FUNC_STATUS_OK:
1778c2a4
PP
1220 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME, "count-lteq-capacity",
1221 *user_count <= MSG_BATCH_SIZE,
7474e7d3
PP
1222 "Invalid returned message count: greater than "
1223 "batch size: count=%" PRIu64 ", batch-size=%u",
1224 *user_count, MSG_BATCH_SIZE);
6c373cc9 1225 *msgs = (void *) iterator->msgs->pdata;
d4393e08 1226 break;
d24d5663 1227 case BT_FUNC_STATUS_AGAIN:
d4393e08 1228 goto end;
d24d5663 1229 case BT_FUNC_STATUS_END:
9a2c8b8e
PP
1230 set_msg_iterator_state(iterator,
1231 BT_MESSAGE_ITERATOR_STATE_ENDED);
f42867e2 1232 goto end;
f42867e2
PP
1233 default:
1234 /* Unknown non-error status */
498e7994 1235 bt_common_abort();
41a2b7ae
PP
1236 }
1237
1238end:
3230ee6b
PP
1239 return status;
1240}
1241
1353b066 1242BT_EXPORT
7474e7d3 1243struct bt_component *
9a2c8b8e
PP
1244bt_message_iterator_borrow_component(
1245 struct bt_message_iterator *iterator)
d94d92ac 1246{
d5b13b9b 1247 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator);
d94d92ac
PP
1248 return iterator->upstream_component;
1249}
1250
1353b066 1251BT_EXPORT
d6e69534
PP
1252struct bt_self_component *bt_self_message_iterator_borrow_component(
1253 struct bt_self_message_iterator *self_iterator)
413bc2c4 1254{
9a2c8b8e 1255 struct bt_message_iterator *iterator =
d94d92ac 1256 (void *) self_iterator;
90157d89 1257
d5b13b9b 1258 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator);
d94d92ac 1259 return (void *) iterator->upstream_component;
413bc2c4
JG
1260}
1261
1353b066 1262BT_EXPORT
50e763f6 1263struct bt_self_component_port_output *bt_self_message_iterator_borrow_port(
d6e69534 1264 struct bt_self_message_iterator *self_iterator)
91457551 1265{
9a2c8b8e 1266 struct bt_message_iterator *iterator =
d94d92ac
PP
1267 (void *) self_iterator;
1268
d5b13b9b 1269 BT_ASSERT_PRE_DEV_MSG_ITER_NON_NULL(iterator);
d94d92ac 1270 return (void *) iterator->upstream_port;
91457551 1271}
8ed535b5 1272
1778c2a4
PP
1273#define CAN_SEEK_NS_FROM_ORIGIN_METHOD_NAME \
1274 "bt_message_iterator_class_can_seek_ns_from_origin_method"
1275
1353b066 1276BT_EXPORT
f2fb1b32 1277enum bt_message_iterator_can_seek_ns_from_origin_status
9a2c8b8e
PP
1278bt_message_iterator_can_seek_ns_from_origin(
1279 struct bt_message_iterator *iterator,
f2fb1b32 1280 int64_t ns_from_origin, bt_bool *can_seek)
7474e7d3 1281{
f2fb1b32 1282 enum bt_message_iterator_can_seek_ns_from_origin_status status;
7474e7d3 1283
17f3083a 1284 BT_ASSERT_PRE_NO_ERROR();
d5b13b9b
PP
1285 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator);
1286 BT_ASSERT_PRE_RES_OUT_NON_NULL(can_seek);
7474e7d3 1287 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator);
1778c2a4 1288 BT_ASSERT_PRE("graph-is-configured",
5badd463
PP
1289 bt_component_borrow_graph(iterator->upstream_component)->config_state !=
1290 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
7474e7d3
PP
1291 "Graph is not configured: %!+g",
1292 bt_component_borrow_graph(iterator->upstream_component));
1293
1294 if (iterator->methods.can_seek_ns_from_origin) {
f2fb1b32
SM
1295 /*
1296 * Initialize to an invalid value, so we can post-assert that
1297 * the method returned a valid value.
1298 */
1299 *can_seek = -1;
1300
c0e46a7c
SM
1301 BT_LIB_LOGD("Calling user's \"can seek nanoseconds from origin\" method: %!+i",
1302 iterator);
1303
f2fb1b32
SM
1304 status = (int) iterator->methods.can_seek_ns_from_origin(iterator,
1305 ns_from_origin, can_seek);
1306
1778c2a4
PP
1307 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(
1308 CAN_SEEK_NS_FROM_ORIGIN_METHOD_NAME, status);
6ecdcca3 1309
c0e46a7c
SM
1310 if (status != BT_FUNC_STATUS_OK) {
1311 BT_LIB_LOGW_APPEND_CAUSE(
1312 "Component input port message iterator's \"can seek nanoseconds from origin\" method failed: "
1313 "%![iter-]+i, status=%s",
1314 iterator, bt_common_func_status_string(status));
1315 goto end;
1316 }
1317
1778c2a4
PP
1318 BT_ASSERT_POST(CAN_SEEK_NS_FROM_ORIGIN_METHOD_NAME,
1319 "valid-return-value",
1320 *can_seek == BT_TRUE || *can_seek == BT_FALSE,
f2fb1b32
SM
1321 "Unexpected boolean value returned from user's \"can seek ns from origin\" method: val=%d, %![iter-]+i",
1322 *can_seek, iterator);
1323
c0e46a7c
SM
1324 BT_LIB_LOGD(
1325 "User's \"can seek nanoseconds from origin\" returned successfully: "
1326 "%![iter-]+i, can-seek=%d",
1327 iterator, *can_seek);
1328
1329 if (*can_seek) {
1330 goto end;
1331 }
7474e7d3
PP
1332 }
1333
1334 /*
c0e46a7c
SM
1335 * Automatic seeking fall back: if we can seek to the beginning and the
1336 * iterator supports forward seeking then we can automatically seek to
1337 * any timestamp.
7474e7d3 1338 */
9a2c8b8e 1339 status = (int) bt_message_iterator_can_seek_beginning(
f2fb1b32 1340 iterator, can_seek);
c0e46a7c
SM
1341 if (status != BT_FUNC_STATUS_OK) {
1342 goto end;
1343 }
1344
1345 *can_seek = *can_seek && iterator->config.can_seek_forward;
7474e7d3
PP
1346
1347end:
f2fb1b32 1348 return status;
7474e7d3
PP
1349}
1350
1778c2a4
PP
1351#define CAN_SEEK_BEGINNING_METHOD_NAME \
1352 "bt_message_iterator_class_can_seek_beginning"
1353
1353b066 1354BT_EXPORT
f2fb1b32 1355enum bt_message_iterator_can_seek_beginning_status
9a2c8b8e
PP
1356bt_message_iterator_can_seek_beginning(
1357 struct bt_message_iterator *iterator,
f2fb1b32 1358 bt_bool *can_seek)
7474e7d3 1359{
f2fb1b32 1360 enum bt_message_iterator_can_seek_beginning_status status;
7474e7d3 1361
17f3083a 1362 BT_ASSERT_PRE_NO_ERROR();
d5b13b9b
PP
1363 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator);
1364 BT_ASSERT_PRE_RES_OUT_NON_NULL(can_seek);
7474e7d3 1365 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator);
1778c2a4 1366 BT_ASSERT_PRE("graph-is-configured",
5badd463
PP
1367 bt_component_borrow_graph(iterator->upstream_component)->config_state !=
1368 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
7474e7d3
PP
1369 "Graph is not configured: %!+g",
1370 bt_component_borrow_graph(iterator->upstream_component));
1371
1372 if (iterator->methods.can_seek_beginning) {
f2fb1b32
SM
1373 /*
1374 * Initialize to an invalid value, so we can post-assert that
1375 * the method returned a valid value.
1376 */
1377 *can_seek = -1;
1378
1379 status = (int) iterator->methods.can_seek_beginning(iterator, can_seek);
1380
1778c2a4
PP
1381 BT_ASSERT_POST(CAN_SEEK_BEGINNING_METHOD_NAME,
1382 "valid-return-value",
f2fb1b32
SM
1383 status != BT_FUNC_STATUS_OK ||
1384 *can_seek == BT_TRUE ||
1385 *can_seek == BT_FALSE,
1386 "Unexpected boolean value returned from user's \"can seek beginning\" method: val=%d, %![iter-]+i",
1387 *can_seek, iterator);
1778c2a4
PP
1388 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(
1389 CAN_SEEK_BEGINNING_METHOD_NAME, status);
f2fb1b32
SM
1390 } else {
1391 *can_seek = BT_FALSE;
1392 status = BT_FUNC_STATUS_OK;
7474e7d3
PP
1393 }
1394
f2fb1b32 1395 return status;
7474e7d3
PP
1396}
1397
1398static inline
003e713f 1399void set_iterator_state_after_seeking(
9a2c8b8e 1400 struct bt_message_iterator *iterator,
d24d5663 1401 int status)
7474e7d3 1402{
9a2c8b8e 1403 enum bt_message_iterator_state new_state = 0;
7474e7d3
PP
1404
1405 /* Set iterator's state depending on seeking status */
1406 switch (status) {
d24d5663 1407 case BT_FUNC_STATUS_OK:
9a2c8b8e 1408 new_state = BT_MESSAGE_ITERATOR_STATE_ACTIVE;
7474e7d3 1409 break;
d24d5663 1410 case BT_FUNC_STATUS_AGAIN:
9a2c8b8e 1411 new_state = BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN;
7474e7d3 1412 break;
d24d5663
PP
1413 case BT_FUNC_STATUS_ERROR:
1414 case BT_FUNC_STATUS_MEMORY_ERROR:
9a2c8b8e 1415 new_state = BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR;
7474e7d3 1416 break;
d24d5663 1417 case BT_FUNC_STATUS_END:
9a2c8b8e 1418 new_state = BT_MESSAGE_ITERATOR_STATE_ENDED;
7474e7d3
PP
1419 break;
1420 default:
498e7994 1421 bt_common_abort();
7474e7d3
PP
1422 }
1423
9a2c8b8e 1424 set_msg_iterator_state(iterator, new_state);
7474e7d3
PP
1425}
1426
54b135a0
SM
1427static
1428void reset_iterator_expectations(
9a2c8b8e 1429 struct bt_message_iterator *iterator)
54b135a0
SM
1430{
1431 iterator->last_ns_from_origin = INT64_MIN;
1432 iterator->clock_expectation.type = CLOCK_EXPECTATION_UNSET;
1433}
1434
f2fb1b32
SM
1435static
1436bool message_iterator_can_seek_beginning(
9a2c8b8e 1437 struct bt_message_iterator *iterator)
f2fb1b32
SM
1438{
1439 enum bt_message_iterator_can_seek_beginning_status status;
1440 bt_bool can_seek;
1441
9a2c8b8e 1442 status = bt_message_iterator_can_seek_beginning(
f2fb1b32
SM
1443 iterator, &can_seek);
1444 if (status != BT_FUNC_STATUS_OK) {
1445 can_seek = BT_FALSE;
1446 }
1447
1448 return can_seek;
1449}
1450
1778c2a4
PP
1451#define SEEK_BEGINNING_METHOD_NAME \
1452 "bt_message_iterator_class_seek_beginning_method"
1453
1353b066 1454BT_EXPORT
d24d5663 1455enum bt_message_iterator_seek_beginning_status
1778c2a4 1456bt_message_iterator_seek_beginning(struct bt_message_iterator *iterator)
7474e7d3 1457{
9275bef4 1458 int status;
7474e7d3 1459
17f3083a 1460 BT_ASSERT_PRE_NO_ERROR();
d5b13b9b 1461 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator);
7474e7d3 1462 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator);
1778c2a4 1463 BT_ASSERT_PRE("graph-is-configured",
5badd463
PP
1464 bt_component_borrow_graph(iterator->upstream_component)->config_state !=
1465 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
7474e7d3
PP
1466 "Graph is not configured: %!+g",
1467 bt_component_borrow_graph(iterator->upstream_component));
1778c2a4
PP
1468 BT_ASSERT_PRE("can-seek-beginning",
1469 message_iterator_can_seek_beginning(iterator),
7474e7d3 1470 "Message iterator cannot seek beginning: %!+i", iterator);
54b135a0
SM
1471
1472 /*
1473 * We are seeking, reset our expectations about how the following
1474 * messages should look like.
1475 */
1476 reset_iterator_expectations(iterator);
1477
7474e7d3 1478 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i", iterator);
9a2c8b8e
PP
1479 set_msg_iterator_state(iterator,
1480 BT_MESSAGE_ITERATOR_STATE_SEEKING);
7474e7d3
PP
1481 status = iterator->methods.seek_beginning(iterator);
1482 BT_LOGD("User method returned: status=%s",
d24d5663 1483 bt_common_func_status_string(status));
1778c2a4
PP
1484 BT_ASSERT_POST(SEEK_BEGINNING_METHOD_NAME, "valid-status",
1485 status == BT_FUNC_STATUS_OK ||
d24d5663
PP
1486 status == BT_FUNC_STATUS_ERROR ||
1487 status == BT_FUNC_STATUS_MEMORY_ERROR ||
1488 status == BT_FUNC_STATUS_AGAIN,
7474e7d3 1489 "Unexpected status: %![iter-]+i, status=%s",
d24d5663 1490 iterator, bt_common_func_status_string(status));
1778c2a4
PP
1491 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(SEEK_BEGINNING_METHOD_NAME,
1492 status);
870631a2
PP
1493 if (status < 0) {
1494 BT_LIB_LOGW_APPEND_CAUSE(
1495 "Component input port message iterator's \"seek beginning\" method failed: "
1496 "%![iter-]+i, status=%s",
1497 iterator, bt_common_func_status_string(status));
1498 }
1499
9340eff9
SM
1500 clear_per_stream_state(iterator);
1501
7474e7d3
PP
1502 set_iterator_state_after_seeking(iterator, status);
1503 return status;
1504}
1505
1353b066 1506BT_EXPORT
8d8b141d 1507bt_bool
9a2c8b8e
PP
1508bt_message_iterator_can_seek_forward(
1509 bt_message_iterator *iterator)
8d8b141d 1510{
d5b13b9b 1511 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator);
8d8b141d
SM
1512
1513 return iterator->config.can_seek_forward;
1514}
1515
5b7b55be
SM
1516/*
1517 * Structure used to record the state of a given stream during the fast-forward
1518 * phase of an auto-seek.
1519 */
1520struct auto_seek_stream_state {
1521 /*
1522 * Value representing which step of this timeline we are at.
1523 *
1524 * time --->
188edac1 1525 * [SB] 1 [PB] 2 [PE] 1 [SE]
5b7b55be
SM
1526 *
1527 * At each point in the timeline, the messages we need to replicate are:
1528 *
1529 * 1: Stream beginning
188edac1 1530 * 2: Stream beginning, packet beginning
5b7b55be
SM
1531 *
1532 * Before "Stream beginning" and after "Stream end", we don't need to
1533 * replicate anything as the stream doesn't exist.
1534 */
1535 enum {
1536 AUTO_SEEK_STREAM_STATE_STREAM_BEGAN,
5b7b55be
SM
1537 AUTO_SEEK_STREAM_STATE_PACKET_BEGAN,
1538 } state;
1539
1540 /*
1541 * If `state` is AUTO_SEEK_STREAM_STATE_PACKET_BEGAN, the packet we are
1542 * in. This is a weak reference, since the packet will always be
1543 * alive by the time we use it.
1544 */
1545 struct bt_packet *packet;
188edac1
SM
1546
1547 /* Have we see a message with a clock snapshot yet? */
1548 bool seen_clock_snapshot;
5b7b55be
SM
1549};
1550
1551static
1552struct auto_seek_stream_state *create_auto_seek_stream_state(void)
1553{
1554 return g_new0(struct auto_seek_stream_state, 1);
1555}
1556
1557static
1558void destroy_auto_seek_stream_state(void *ptr)
1559{
1560 g_free(ptr);
1561}
1562
1563static
1564GHashTable *create_auto_seek_stream_states(void)
1565{
1566 return g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL,
1567 destroy_auto_seek_stream_state);
1568}
1569
1570static
1571void destroy_auto_seek_stream_states(GHashTable *stream_states)
1572{
1573 g_hash_table_destroy(stream_states);
1574}
1575
1576/*
1577 * Handle one message while we are in the fast-forward phase of an auto-seek.
1578 *
1579 * Sets `*got_first` to true if the message's timestamp is greater or equal to
1580 * `ns_from_origin`. In other words, if this is the first message after our
1581 * seek point.
1582 *
1583 * `stream_states` is an hash table of `bt_stream *` (weak reference) to
1584 * `struct auto_seek_stream_state` used to keep the state of each stream
1585 * during the fast-forward.
1586 */
1587
7474e7d3 1588static inline
d24d5663 1589int auto_seek_handle_message(
9a2c8b8e 1590 struct bt_message_iterator *iterator,
5b9e151d 1591 int64_t ns_from_origin, const struct bt_message *msg,
5b7b55be 1592 bool *got_first, GHashTable *stream_states)
7474e7d3 1593{
d24d5663 1594 int status = BT_FUNC_STATUS_OK;
5b9e151d 1595 int64_t msg_ns_from_origin;
7474e7d3 1596 const struct bt_clock_snapshot *clk_snapshot = NULL;
5b9e151d
PP
1597 int ret;
1598
98b15851
PP
1599 BT_ASSERT_DBG(msg);
1600 BT_ASSERT_DBG(got_first);
7474e7d3
PP
1601
1602 switch (msg->type) {
1603 case BT_MESSAGE_TYPE_EVENT:
1604 {
1605 const struct bt_message_event *event_msg =
1606 (const void *) msg;
1607
2c091c04 1608 clk_snapshot = event_msg->default_cs;
1778c2a4
PP
1609 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME,
1610 "event-message-has-default-clock-snapshot",
1611 clk_snapshot,
c7072d5a
PP
1612 "Event message has no default clock snapshot: %!+n",
1613 event_msg);
7474e7d3
PP
1614 break;
1615 }
b9fd9cbb 1616 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY:
7474e7d3 1617 {
b9fd9cbb 1618 const struct bt_message_message_iterator_inactivity *inactivity_msg =
7474e7d3
PP
1619 (const void *) msg;
1620
60d02328 1621 clk_snapshot = inactivity_msg->cs;
98b15851 1622 BT_ASSERT_DBG(clk_snapshot);
7474e7d3
PP
1623 break;
1624 }
16663a5e
PP
1625 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
1626 case BT_MESSAGE_TYPE_PACKET_END:
c7072d5a
PP
1627 {
1628 const struct bt_message_packet *packet_msg =
1629 (const void *) msg;
1630
ed3039e4
SM
1631 if (msg->type == BT_MESSAGE_TYPE_PACKET_BEGINNING
1632 && !packet_msg->packet->stream->class->packets_have_beginning_default_clock_snapshot) {
1633 goto skip_msg;
1634 }
1635
1636 if (msg->type == BT_MESSAGE_TYPE_PACKET_END
1637 && !packet_msg->packet->stream->class->packets_have_end_default_clock_snapshot) {
1638 goto skip_msg;
1639 }
1640
c7072d5a 1641 clk_snapshot = packet_msg->default_cs;
1778c2a4
PP
1642 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME,
1643 "packet-message-has-default-clock-snapshot",
1644 clk_snapshot,
c7072d5a
PP
1645 "Packet message has no default clock snapshot: %!+n",
1646 packet_msg);
1647 break;
1648 }
16663a5e
PP
1649 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
1650 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
7474e7d3 1651 {
5b9e151d
PP
1652 struct bt_message_discarded_items *msg_disc_items =
1653 (void *) msg;
1654
ed3039e4
SM
1655 if (msg->type == BT_MESSAGE_TYPE_DISCARDED_EVENTS &&
1656 !msg_disc_items->stream->class->discarded_events_have_default_clock_snapshots) {
1657 goto skip_msg;
1658 }
1659
1660 if (msg->type == BT_MESSAGE_TYPE_DISCARDED_PACKETS &&
1661 !msg_disc_items->stream->class->discarded_packets_have_default_clock_snapshots) {
1662 goto skip_msg;
1663 }
1664
1778c2a4
PP
1665 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME,
1666 "discarded-events-packets-message-has-default-clock-snapshot",
1667 msg_disc_items->default_begin_cs &&
1668 msg_disc_items->default_end_cs,
5b9e151d
PP
1669 "Discarded events/packets message has no default clock snapshots: %!+n",
1670 msg_disc_items);
1671 ret = bt_clock_snapshot_get_ns_from_origin(
1672 msg_disc_items->default_begin_cs,
1673 &msg_ns_from_origin);
1674 if (ret) {
d24d5663 1675 status = BT_FUNC_STATUS_ERROR;
5b9e151d
PP
1676 goto end;
1677 }
7474e7d3 1678
5b9e151d
PP
1679 if (msg_ns_from_origin >= ns_from_origin) {
1680 *got_first = true;
1681 goto push_msg;
1682 }
1683
1684 ret = bt_clock_snapshot_get_ns_from_origin(
1685 msg_disc_items->default_end_cs,
1686 &msg_ns_from_origin);
1687 if (ret) {
d24d5663 1688 status = BT_FUNC_STATUS_ERROR;
5b9e151d
PP
1689 goto end;
1690 }
1691
1692 if (msg_ns_from_origin >= ns_from_origin) {
1693 /*
1694 * The discarded items message's beginning time
1695 * is before the requested seeking time, but its
1696 * end time is after. Modify the message so as
1697 * to set its beginning time to the requested
1698 * seeking time, and make its item count unknown
1699 * as we don't know if items were really
1700 * discarded within the new time range.
1701 */
4af85094 1702 uint64_t new_begin_raw_value = 0;
5b9e151d
PP
1703
1704 ret = bt_clock_class_clock_value_from_ns_from_origin(
1705 msg_disc_items->default_end_cs->clock_class,
1706 ns_from_origin, &new_begin_raw_value);
1707 if (ret) {
d24d5663 1708 status = BT_FUNC_STATUS_ERROR;
5b9e151d
PP
1709 goto end;
1710 }
1711
1712 bt_clock_snapshot_set_raw_value(
1713 msg_disc_items->default_begin_cs,
1714 new_begin_raw_value);
1715 msg_disc_items->count.base.avail =
1716 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE;
1717
1718 /*
1719 * It is safe to push it because its beginning
1720 * time is exactly the requested seeking time.
1721 */
1722 goto push_msg;
1723 } else {
1724 goto skip_msg;
1725 }
7474e7d3 1726 }
188edac1
SM
1727 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
1728 case BT_MESSAGE_TYPE_STREAM_END:
7474e7d3 1729 {
188edac1
SM
1730 struct bt_message_stream *stream_msg =
1731 (struct bt_message_stream *) msg;
7474e7d3 1732
188edac1
SM
1733 if (stream_msg->default_cs_state != BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN) {
1734 /* Ignore */
5b9e151d 1735 goto skip_msg;
16663a5e
PP
1736 }
1737
188edac1 1738 clk_snapshot = stream_msg->default_cs;
7474e7d3
PP
1739 break;
1740 }
1741 default:
498e7994 1742 bt_common_abort();
7474e7d3
PP
1743 }
1744
98b15851 1745 BT_ASSERT_DBG(clk_snapshot);
5b9e151d
PP
1746 ret = bt_clock_snapshot_get_ns_from_origin(clk_snapshot,
1747 &msg_ns_from_origin);
1748 if (ret) {
d24d5663 1749 status = BT_FUNC_STATUS_ERROR;
7474e7d3
PP
1750 goto end;
1751 }
1752
5b9e151d
PP
1753 if (msg_ns_from_origin >= ns_from_origin) {
1754 *got_first = true;
1755 goto push_msg;
1756 }
1757
1758skip_msg:
5b7b55be
SM
1759 /* This message won't be sent downstream. */
1760 switch (msg->type) {
1761 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
1762 {
1763 const struct bt_message_stream *stream_msg = (const void *) msg;
1764 struct auto_seek_stream_state *stream_state;
5b7b55be
SM
1765
1766 /* Update stream's state: stream began. */
1767 stream_state = create_auto_seek_stream_state();
1768 if (!stream_state) {
d24d5663 1769 status = BT_FUNC_STATUS_MEMORY_ERROR;
5b7b55be
SM
1770 goto end;
1771 }
1772
1773 stream_state->state = AUTO_SEEK_STREAM_STATE_STREAM_BEGAN;
e74dbb33 1774
188edac1
SM
1775 if (stream_msg->default_cs_state == BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN) {
1776 stream_state->seen_clock_snapshot = true;
1777 }
1778
98b15851 1779 BT_ASSERT_DBG(!bt_g_hash_table_contains(stream_states, stream_msg->stream));
e74dbb33 1780 g_hash_table_insert(stream_states, stream_msg->stream, stream_state);
5b7b55be
SM
1781 break;
1782 }
188edac1 1783 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
5b7b55be 1784 {
188edac1 1785 const struct bt_message_packet *packet_msg =
5b7b55be
SM
1786 (const void *) msg;
1787 struct auto_seek_stream_state *stream_state;
1788
188edac1
SM
1789 /* Update stream's state: packet began. */
1790 stream_state = g_hash_table_lookup(stream_states, packet_msg->packet->stream);
98b15851
PP
1791 BT_ASSERT_DBG(stream_state);
1792 BT_ASSERT_DBG(stream_state->state == AUTO_SEEK_STREAM_STATE_STREAM_BEGAN);
188edac1 1793 stream_state->state = AUTO_SEEK_STREAM_STATE_PACKET_BEGAN;
98b15851 1794 BT_ASSERT_DBG(!stream_state->packet);
188edac1
SM
1795 stream_state->packet = packet_msg->packet;
1796
1797 if (packet_msg->packet->stream->class->packets_have_beginning_default_clock_snapshot) {
1798 stream_state->seen_clock_snapshot = true;
1799 }
1800
5b7b55be
SM
1801 break;
1802 }
188edac1 1803 case BT_MESSAGE_TYPE_EVENT:
5b7b55be 1804 {
188edac1 1805 const struct bt_message_event *event_msg = (const void *) msg;
5b7b55be
SM
1806 struct auto_seek_stream_state *stream_state;
1807
188edac1 1808 stream_state = g_hash_table_lookup(stream_states,
03cc4222 1809 event_msg->event->stream);
98b15851 1810 BT_ASSERT_DBG(stream_state);
5b7b55be 1811
188edac1
SM
1812 // HELPME: are we sure that event messages have clock snapshots at this point?
1813 stream_state->seen_clock_snapshot = true;
1814
5b7b55be
SM
1815 break;
1816 }
1817 case BT_MESSAGE_TYPE_PACKET_END:
1818 {
1819 const struct bt_message_packet *packet_msg =
1820 (const void *) msg;
1821 struct auto_seek_stream_state *stream_state;
1822
1823 /* Update stream's state: packet ended. */
1824 stream_state = g_hash_table_lookup(stream_states, packet_msg->packet->stream);
98b15851
PP
1825 BT_ASSERT_DBG(stream_state);
1826 BT_ASSERT_DBG(stream_state->state == AUTO_SEEK_STREAM_STATE_PACKET_BEGAN);
188edac1 1827 stream_state->state = AUTO_SEEK_STREAM_STATE_STREAM_BEGAN;
98b15851 1828 BT_ASSERT_DBG(stream_state->packet);
5b7b55be 1829 stream_state->packet = NULL;
5b7b55be 1830
188edac1
SM
1831 if (packet_msg->packet->stream->class->packets_have_end_default_clock_snapshot) {
1832 stream_state->seen_clock_snapshot = true;
1833 }
5b7b55be 1834
5b7b55be
SM
1835 break;
1836 }
1837 case BT_MESSAGE_TYPE_STREAM_END:
1838 {
1839 const struct bt_message_stream *stream_msg = (const void *) msg;
1840 struct auto_seek_stream_state *stream_state;
1841
1842 stream_state = g_hash_table_lookup(stream_states, stream_msg->stream);
98b15851
PP
1843 BT_ASSERT_DBG(stream_state);
1844 BT_ASSERT_DBG(stream_state->state == AUTO_SEEK_STREAM_STATE_STREAM_BEGAN);
1845 BT_ASSERT_DBG(!stream_state->packet);
5b7b55be
SM
1846
1847 /* Update stream's state: this stream doesn't exist anymore. */
1848 g_hash_table_remove(stream_states, stream_msg->stream);
1849 break;
1850 }
188edac1
SM
1851 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
1852 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
1853 {
1854 const struct bt_message_discarded_items *discarded_msg =
1855 (const void *) msg;
1856 struct auto_seek_stream_state *stream_state;
1857
1858 stream_state = g_hash_table_lookup(stream_states, discarded_msg->stream);
98b15851 1859 BT_ASSERT_DBG(stream_state);
188edac1
SM
1860
1861 if ((msg->type == BT_MESSAGE_TYPE_DISCARDED_EVENTS && discarded_msg->stream->class->discarded_events_have_default_clock_snapshots) ||
1862 (msg->type == BT_MESSAGE_TYPE_DISCARDED_PACKETS && discarded_msg->stream->class->discarded_packets_have_default_clock_snapshots)) {
1863 stream_state->seen_clock_snapshot = true;
1864 }
1865
1866 break;
1867 }
5b7b55be
SM
1868 default:
1869 break;
1870 }
1871
6871026b 1872 bt_object_put_ref_no_null_check(msg);
04c0cec6 1873 msg = NULL;
5b9e151d
PP
1874 goto end;
1875
1876push_msg:
da9c4c52 1877 g_queue_push_tail(iterator->auto_seek.msgs, (void *) msg);
5b9e151d 1878 msg = NULL;
7474e7d3
PP
1879
1880end:
98b15851 1881 BT_ASSERT_DBG(!msg || status != BT_FUNC_STATUS_OK);
5b9e151d 1882 return status;
7474e7d3
PP
1883}
1884
1885static
d24d5663 1886int find_message_ge_ns_from_origin(
9a2c8b8e 1887 struct bt_message_iterator *iterator,
5b7b55be 1888 int64_t ns_from_origin, GHashTable *stream_states)
7474e7d3 1889{
c70a96a6 1890 int status = BT_FUNC_STATUS_OK;
9a2c8b8e 1891 enum bt_message_iterator_state init_state =
7474e7d3
PP
1892 iterator->state;
1893 const struct bt_message *messages[MSG_BATCH_SIZE];
1894 uint64_t user_count = 0;
1895 uint64_t i;
5b9e151d 1896 bool got_first = false;
7474e7d3 1897
98b15851 1898 BT_ASSERT_DBG(iterator);
7474e7d3
PP
1899 memset(&messages[0], 0, sizeof(messages[0]) * MSG_BATCH_SIZE);
1900
1901 /*
1902 * Make this iterator temporarily active (not seeking) to call
1903 * the "next" method.
1904 */
9a2c8b8e
PP
1905 set_msg_iterator_state(iterator,
1906 BT_MESSAGE_ITERATOR_STATE_ACTIVE);
7474e7d3 1907
98b15851 1908 BT_ASSERT_DBG(iterator->methods.next);
7474e7d3 1909
e0dade92 1910 while (!got_first) {
7474e7d3
PP
1911 /*
1912 * Call the user's "next" method to get the next
1913 * messages and status.
1914 */
54b135a0 1915 status = call_iterator_next_method(iterator,
7474e7d3 1916 &messages[0], MSG_BATCH_SIZE, &user_count);
870631a2
PP
1917 BT_LOGD("User method returned: status=%s",
1918 bt_common_func_status_string(status));
1919 if (status < 0) {
1920 BT_LIB_LOGW_APPEND_CAUSE(
1921 "Component input port message iterator's \"next\" method failed: "
1922 "%![iter-]+i, status=%s",
1923 iterator, bt_common_func_status_string(status));
1924 }
7474e7d3 1925
7474e7d3
PP
1926 /*
1927 * The user's "next" method must not do any action which
1928 * would change the iterator's state.
1929 */
98b15851 1930 BT_ASSERT_DBG(iterator->state ==
9a2c8b8e 1931 BT_MESSAGE_ITERATOR_STATE_ACTIVE);
7474e7d3
PP
1932
1933 switch (status) {
d24d5663 1934 case BT_FUNC_STATUS_OK:
1778c2a4
PP
1935 BT_ASSERT_POST_DEV(NEXT_METHOD_NAME,
1936 "count-lteq-capacity",
1937 user_count <= MSG_BATCH_SIZE,
7474e7d3
PP
1938 "Invalid returned message count: greater than "
1939 "batch size: count=%" PRIu64 ", batch-size=%u",
1940 user_count, MSG_BATCH_SIZE);
1941 break;
d24d5663
PP
1942 case BT_FUNC_STATUS_AGAIN:
1943 case BT_FUNC_STATUS_ERROR:
1944 case BT_FUNC_STATUS_MEMORY_ERROR:
1945 case BT_FUNC_STATUS_END:
7474e7d3
PP
1946 goto end;
1947 default:
498e7994 1948 bt_common_abort();
7474e7d3
PP
1949 }
1950
7474e7d3 1951 for (i = 0; i < user_count; i++) {
5b9e151d 1952 if (got_first) {
da9c4c52 1953 g_queue_push_tail(iterator->auto_seek.msgs,
5b9e151d
PP
1954 (void *) messages[i]);
1955 messages[i] = NULL;
7474e7d3
PP
1956 continue;
1957 }
1958
5b9e151d 1959 status = auto_seek_handle_message(iterator,
5b7b55be
SM
1960 ns_from_origin, messages[i], &got_first,
1961 stream_states);
d24d5663 1962 if (status == BT_FUNC_STATUS_OK) {
e0dade92 1963 /* Message was either pushed or moved */
5b9e151d
PP
1964 messages[i] = NULL;
1965 } else {
7474e7d3
PP
1966 goto end;
1967 }
7474e7d3
PP
1968 }
1969 }
1970
1971end:
1972 for (i = 0; i < user_count; i++) {
1973 if (messages[i]) {
6871026b 1974 bt_object_put_ref_no_null_check(messages[i]);
7474e7d3
PP
1975 }
1976 }
1977
9a2c8b8e 1978 set_msg_iterator_state(iterator, init_state);
7474e7d3
PP
1979 return status;
1980}
1981
5b7b55be
SM
1982/*
1983 * This function is installed as the iterator's next callback after we have
e7401568 1984 * auto-sought (sought to the beginning and fast-forwarded) to send the
5b7b55be
SM
1985 * messages saved in iterator->auto_seek.msgs. Once this is done, the original
1986 * next callback is put back.
1987 */
1988
7474e7d3 1989static
a3f0c7db 1990enum bt_message_iterator_class_next_method_status post_auto_seek_next(
9a2c8b8e 1991 struct bt_message_iterator *iterator,
7474e7d3
PP
1992 bt_message_array_const msgs, uint64_t capacity,
1993 uint64_t *count)
1994{
da9c4c52 1995 BT_ASSERT(!g_queue_is_empty(iterator->auto_seek.msgs));
5b9e151d 1996 *count = 0;
7474e7d3
PP
1997
1998 /*
1999 * Move auto-seek messages to the output array (which is this
5b9e151d 2000 * iterator's base message array).
7474e7d3 2001 */
da9c4c52
SM
2002 while (capacity > 0 && !g_queue_is_empty(iterator->auto_seek.msgs)) {
2003 msgs[*count] = g_queue_pop_head(iterator->auto_seek.msgs);
5b9e151d
PP
2004 capacity--;
2005 (*count)++;
7474e7d3 2006 }
7474e7d3 2007
5b9e151d
PP
2008 BT_ASSERT(*count > 0);
2009
da9c4c52 2010 if (g_queue_is_empty(iterator->auto_seek.msgs)) {
572075a8
SM
2011 /* No more auto-seek messages, restore user's next callback. */
2012 BT_ASSERT(iterator->auto_seek.original_next_callback);
2013 iterator->methods.next = iterator->auto_seek.original_next_callback;
2014 iterator->auto_seek.original_next_callback = NULL;
7474e7d3
PP
2015 }
2016
d24d5663 2017 return BT_FUNC_STATUS_OK;
7474e7d3
PP
2018}
2019
5b7b55be
SM
2020static inline
2021int clock_raw_value_from_ns_from_origin(const bt_clock_class *clock_class,
2022 int64_t ns_from_origin, uint64_t *raw_value)
2023{
2024
2025 int64_t cc_offset_s = clock_class->offset_seconds;
2026 uint64_t cc_offset_cycles = clock_class->offset_cycles;
2027 uint64_t cc_freq = clock_class->frequency;
2028
2029 return bt_common_clock_value_from_ns_from_origin(cc_offset_s,
2030 cc_offset_cycles, cc_freq, ns_from_origin, raw_value);
2031}
2032
f2fb1b32
SM
2033static
2034bool message_iterator_can_seek_ns_from_origin(
9a2c8b8e 2035 struct bt_message_iterator *iterator,
f2fb1b32
SM
2036 int64_t ns_from_origin)
2037{
2038 enum bt_message_iterator_can_seek_ns_from_origin_status status;
2039 bt_bool can_seek;
2040
9a2c8b8e 2041 status = bt_message_iterator_can_seek_ns_from_origin(
f2fb1b32
SM
2042 iterator, ns_from_origin, &can_seek);
2043 if (status != BT_FUNC_STATUS_OK) {
2044 can_seek = BT_FALSE;
2045 }
2046
2047 return can_seek;
2048}
5b7b55be 2049
1778c2a4
PP
2050#define SEEK_NS_FROM_ORIGIN_METHOD_NAME \
2051 "bt_message_iterator_class_seek_ns_from_origin_method"
2052
2053
1353b066 2054BT_EXPORT
d24d5663 2055enum bt_message_iterator_seek_ns_from_origin_status
9a2c8b8e
PP
2056bt_message_iterator_seek_ns_from_origin(
2057 struct bt_message_iterator *iterator,
7474e7d3
PP
2058 int64_t ns_from_origin)
2059{
2060 int status;
5b7b55be 2061 GHashTable *stream_states = NULL;
c0e46a7c 2062 bt_bool can_seek_by_itself;
7474e7d3 2063
17f3083a 2064 BT_ASSERT_PRE_NO_ERROR();
d5b13b9b 2065 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator);
7474e7d3 2066 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator);
1778c2a4 2067 BT_ASSERT_PRE("graph-is-configured",
5badd463
PP
2068 bt_component_borrow_graph(iterator->upstream_component)->config_state !=
2069 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
7474e7d3
PP
2070 "Graph is not configured: %!+g",
2071 bt_component_borrow_graph(iterator->upstream_component));
c0e46a7c 2072 /* The iterator must be able to seek ns from origin one way or another. */
1778c2a4 2073 BT_ASSERT_PRE("can-seek-ns-from-origin",
f2fb1b32 2074 message_iterator_can_seek_ns_from_origin(iterator, ns_from_origin),
7474e7d3
PP
2075 "Message iterator cannot seek nanoseconds from origin: %!+i, "
2076 "ns-from-origin=%" PRId64, iterator, ns_from_origin);
9a2c8b8e
PP
2077 set_msg_iterator_state(iterator,
2078 BT_MESSAGE_ITERATOR_STATE_SEEKING);
7474e7d3 2079
54b135a0
SM
2080 /*
2081 * We are seeking, reset our expectations about how the following
2082 * messages should look like.
2083 */
2084 reset_iterator_expectations(iterator);
2085
c0e46a7c
SM
2086 /* Check if the iterator can seek by itself. If not we'll use autoseek. */
2087 if (iterator->methods.can_seek_ns_from_origin) {
a3f0c7db 2088 bt_message_iterator_class_can_seek_ns_from_origin_method_status
c0e46a7c
SM
2089 can_seek_status;
2090
2091 can_seek_status =
2092 iterator->methods.can_seek_ns_from_origin(
2093 iterator, ns_from_origin, &can_seek_by_itself);
2094 if (can_seek_status != BT_FUNC_STATUS_OK) {
2095 status = can_seek_status;
2096 goto end;
2097 }
2098 } else {
2099 can_seek_by_itself = false;
2100 }
2101
2102 if (can_seek_by_itself) {
5b7b55be 2103 /* The iterator knows how to seek to a particular time, let it handle this. */
2e1b5615 2104 BT_ASSERT(iterator->methods.seek_ns_from_origin);
7474e7d3
PP
2105 BT_LIB_LOGD("Calling user's \"seek nanoseconds from origin\" method: "
2106 "%![iter-]+i, ns=%" PRId64, iterator, ns_from_origin);
2107 status = iterator->methods.seek_ns_from_origin(iterator,
2108 ns_from_origin);
2109 BT_LOGD("User method returned: status=%s",
d24d5663 2110 bt_common_func_status_string(status));
1778c2a4
PP
2111 BT_ASSERT_POST(SEEK_NS_FROM_ORIGIN_METHOD_NAME, "valid-status",
2112 status == BT_FUNC_STATUS_OK ||
d24d5663
PP
2113 status == BT_FUNC_STATUS_ERROR ||
2114 status == BT_FUNC_STATUS_MEMORY_ERROR ||
2115 status == BT_FUNC_STATUS_AGAIN,
7474e7d3 2116 "Unexpected status: %![iter-]+i, status=%s",
d24d5663 2117 iterator, bt_common_func_status_string(status));
1778c2a4
PP
2118 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(
2119 SEEK_NS_FROM_ORIGIN_METHOD_NAME, status);
870631a2
PP
2120 if (status < 0) {
2121 BT_LIB_LOGW_APPEND_CAUSE(
2122 "Component input port message iterator's \"seek nanoseconds from origin\" method failed: "
2123 "%![iter-]+i, status=%s",
2124 iterator, bt_common_func_status_string(status));
2125 }
7474e7d3 2126 } else {
5b7b55be 2127 /*
c0e46a7c
SM
2128 * The iterator doesn't know how to seek by itself to a
2129 * particular time. We will seek to the beginning and fast
2130 * forward to the right place.
5b7b55be 2131 */
a3f0c7db 2132 enum bt_message_iterator_class_can_seek_beginning_method_status can_seek_status;
f2fb1b32
SM
2133 bt_bool can_seek_beginning;
2134
2135 can_seek_status = iterator->methods.can_seek_beginning(iterator,
2136 &can_seek_beginning);
2137 BT_ASSERT(can_seek_status == BT_FUNC_STATUS_OK);
2138 BT_ASSERT(can_seek_beginning);
7474e7d3
PP
2139 BT_ASSERT(iterator->methods.seek_beginning);
2140 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i",
2141 iterator);
2142 status = iterator->methods.seek_beginning(iterator);
2143 BT_LOGD("User method returned: status=%s",
d24d5663 2144 bt_common_func_status_string(status));
1778c2a4
PP
2145 BT_ASSERT_POST(SEEK_BEGINNING_METHOD_NAME, "valid-status",
2146 status == BT_FUNC_STATUS_OK ||
d24d5663
PP
2147 status == BT_FUNC_STATUS_ERROR ||
2148 status == BT_FUNC_STATUS_MEMORY_ERROR ||
2149 status == BT_FUNC_STATUS_AGAIN,
7474e7d3 2150 "Unexpected status: %![iter-]+i, status=%s",
d24d5663 2151 iterator, bt_common_func_status_string(status));
870631a2
PP
2152 if (status < 0) {
2153 BT_LIB_LOGW_APPEND_CAUSE(
2154 "Component input port message iterator's \"seek beginning\" method failed: "
2155 "%![iter-]+i, status=%s",
2156 iterator, bt_common_func_status_string(status));
2157 }
2158
9340eff9
SM
2159 clear_per_stream_state(iterator);
2160
7474e7d3 2161 switch (status) {
d24d5663 2162 case BT_FUNC_STATUS_OK:
7474e7d3 2163 break;
d24d5663
PP
2164 case BT_FUNC_STATUS_ERROR:
2165 case BT_FUNC_STATUS_MEMORY_ERROR:
2166 case BT_FUNC_STATUS_AGAIN:
7474e7d3
PP
2167 goto end;
2168 default:
498e7994 2169 bt_common_abort();
7474e7d3
PP
2170 }
2171
2172 /*
2173 * Find the first message which has a default clock
2174 * snapshot greater than or equal to the requested
5b9e151d
PP
2175 * seeking time, and move the received messages from
2176 * this point in the batch to this iterator's auto-seek
2177 * message queue.
7474e7d3 2178 */
da9c4c52 2179 while (!g_queue_is_empty(iterator->auto_seek.msgs)) {
6871026b 2180 bt_object_put_ref_no_null_check(
da9c4c52 2181 g_queue_pop_tail(iterator->auto_seek.msgs));
5b9e151d
PP
2182 }
2183
5b7b55be
SM
2184 stream_states = create_auto_seek_stream_states();
2185 if (!stream_states) {
870631a2
PP
2186 BT_LIB_LOGE_APPEND_CAUSE(
2187 "Failed to allocate one GHashTable.");
d24d5663 2188 status = BT_FUNC_STATUS_MEMORY_ERROR;
5b7b55be
SM
2189 goto end;
2190 }
2191
7474e7d3 2192 status = find_message_ge_ns_from_origin(iterator,
5b7b55be 2193 ns_from_origin, stream_states);
7474e7d3 2194 switch (status) {
d24d5663
PP
2195 case BT_FUNC_STATUS_OK:
2196 case BT_FUNC_STATUS_END:
5b7b55be
SM
2197 {
2198 GHashTableIter iter;
2199 gpointer key, value;
2200
2201 /*
2202 * If some streams exist at the seek time, prepend the
2203 * required messages to put those streams in the right
2204 * state.
2205 */
2206 g_hash_table_iter_init(&iter, stream_states);
2207 while (g_hash_table_iter_next (&iter, &key, &value)) {
2208 const bt_stream *stream = key;
2209 struct auto_seek_stream_state *stream_state =
2210 (struct auto_seek_stream_state *) value;
2211 bt_message *msg;
2212 const bt_clock_class *clock_class = bt_stream_class_borrow_default_clock_class_const(
2213 bt_stream_borrow_class_const(stream));
188edac1
SM
2214 /* Initialize to silence maybe-uninitialized warning. */
2215 uint64_t raw_value = 0;
2216
2217 /*
2218 * If we haven't seen a message with a clock snapshot, we don't know if our seek time is within
2219 * the clock's range, so it wouldn't be safe to try to convert ns_from_origin to a clock value.
2220 *
2221 * Also, it would be a bit of a lie to generate a stream begin message with the seek time as its
2222 * clock snapshot, because we don't really know if the stream existed at that time. If we have
2223 * seen a message with a clock snapshot in our seeking, then we are sure that the
2224 * seek time is not below the clock range, and we know the stream was active at that
2225 * time (and that we cut it short).
2226 */
2227 if (stream_state->seen_clock_snapshot) {
2228 if (clock_raw_value_from_ns_from_origin(clock_class, ns_from_origin, &raw_value) != 0) {
2229 BT_LIB_LOGW("Could not convert nanoseconds from origin to clock value: ns-from-origin=%" PRId64 ", %![cc-]+K",
2230 ns_from_origin, clock_class);
2231 status = BT_FUNC_STATUS_ERROR;
2232 goto end;
2233 }
5b7b55be
SM
2234 }
2235
2236 switch (stream_state->state) {
2237 case AUTO_SEEK_STREAM_STATE_PACKET_BEGAN:
2238 BT_ASSERT(stream_state->packet);
2239 BT_LIB_LOGD("Creating packet message: %![packet-]+a", stream_state->packet);
188edac1
SM
2240
2241 if (stream->class->packets_have_beginning_default_clock_snapshot) {
2242 /*
2243 * If we are in the PACKET_BEGAN state, it means we have seen a "packet beginning"
2244 * message. If "packet beginning" packets have clock snapshots, then we must have
2245 * seen a clock snapshot.
2246 */
2247 BT_ASSERT(stream_state->seen_clock_snapshot);
2248
2249 msg = bt_message_packet_beginning_create_with_default_clock_snapshot(
2250 (bt_self_message_iterator *) iterator, stream_state->packet, raw_value);
2251 } else {
2252 msg = bt_message_packet_beginning_create((bt_self_message_iterator *) iterator,
2253 stream_state->packet);
5b7b55be
SM
2254 }
2255
5b7b55be 2256 if (!msg) {
d24d5663 2257 status = BT_FUNC_STATUS_MEMORY_ERROR;
5b7b55be
SM
2258 goto end;
2259 }
2260
5b7b55be
SM
2261 g_queue_push_head(iterator->auto_seek.msgs, msg);
2262 msg = NULL;
2263 /* fall-thru */
188edac1 2264
5b7b55be
SM
2265 case AUTO_SEEK_STREAM_STATE_STREAM_BEGAN:
2266 msg = bt_message_stream_beginning_create(
2267 (bt_self_message_iterator *) iterator, stream);
2268 if (!msg) {
d24d5663 2269 status = BT_FUNC_STATUS_MEMORY_ERROR;
5b7b55be
SM
2270 goto end;
2271 }
2272
188edac1
SM
2273 if (stream_state->seen_clock_snapshot) {
2274 bt_message_stream_beginning_set_default_clock_snapshot(msg, raw_value);
2275 }
2276
5b7b55be
SM
2277 g_queue_push_head(iterator->auto_seek.msgs, msg);
2278 msg = NULL;
2279 break;
2280 }
2281 }
2282
7474e7d3 2283 /*
5b9e151d
PP
2284 * If there are messages in the auto-seek
2285 * message queue, replace the user's "next"
2286 * method with a custom, temporary "next" method
2287 * which returns them.
7474e7d3 2288 */
da9c4c52 2289 if (!g_queue_is_empty(iterator->auto_seek.msgs)) {
572075a8
SM
2290 BT_ASSERT(!iterator->auto_seek.original_next_callback);
2291 iterator->auto_seek.original_next_callback = iterator->methods.next;
2292
5b9e151d 2293 iterator->methods.next =
9a2c8b8e 2294 (bt_message_iterator_next_method)
5b9e151d
PP
2295 post_auto_seek_next;
2296 }
2297
2298 /*
d24d5663
PP
2299 * `BT_FUNC_STATUS_END` becomes
2300 * `BT_FUNC_STATUS_OK`: the next
5b9e151d
PP
2301 * time this iterator's "next" method is called,
2302 * it will return
d24d5663 2303 * `BT_FUNC_STATUS_END`.
5b9e151d 2304 */
d24d5663 2305 status = BT_FUNC_STATUS_OK;
7474e7d3 2306 break;
5b7b55be 2307 }
d24d5663
PP
2308 case BT_FUNC_STATUS_ERROR:
2309 case BT_FUNC_STATUS_MEMORY_ERROR:
2310 case BT_FUNC_STATUS_AGAIN:
7474e7d3 2311 goto end;
7474e7d3 2312 default:
498e7994 2313 bt_common_abort();
7474e7d3
PP
2314 }
2315 }
2316
9340eff9
SM
2317 clear_per_stream_state(iterator);
2318
54b135a0
SM
2319 /*
2320 * The following messages returned by the next method (including
2321 * post_auto_seek_next) must be after (or at) `ns_from_origin`.
2322 */
2323 iterator->last_ns_from_origin = ns_from_origin;
2324
7474e7d3 2325end:
5b7b55be
SM
2326 if (stream_states) {
2327 destroy_auto_seek_stream_states(stream_states);
2328 stream_states = NULL;
2329 }
870631a2 2330
7474e7d3 2331 set_iterator_state_after_seeking(iterator, status);
7474e7d3
PP
2332 return status;
2333}
2334
1353b066 2335BT_EXPORT
9b4f9b42
PP
2336bt_bool bt_self_message_iterator_is_interrupted(
2337 const struct bt_self_message_iterator *self_msg_iter)
2338{
9a2c8b8e 2339 const struct bt_message_iterator *iterator =
9b4f9b42
PP
2340 (const void *) self_msg_iter;
2341
d5b13b9b 2342 BT_ASSERT_PRE_MSG_ITER_NON_NULL(iterator);
9b4f9b42
PP
2343 return (bt_bool) bt_graph_is_interrupted(iterator->graph);
2344}
2345
1353b066 2346BT_EXPORT
9a2c8b8e
PP
2347void bt_message_iterator_get_ref(
2348 const struct bt_message_iterator *iterator)
c5b9b441
PP
2349{
2350 bt_object_get_ref(iterator);
2351}
2352
1353b066 2353BT_EXPORT
9a2c8b8e
PP
2354void bt_message_iterator_put_ref(
2355 const struct bt_message_iterator *iterator)
c5b9b441
PP
2356{
2357 bt_object_put_ref(iterator);
2358}
This page took 0.267354 seconds and 4 git commands to generate.