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