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