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