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