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