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