lib: remove self component param. from msg. iterator init. method
[babeltrace.git] / src / lib / graph / iterator.c
CommitLineData
47e5a032 1/*
f2b0325d 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
b03487ab 24#define BT_LOG_TAG "LIB/MSG-ITER"
1633ef46 25#include "lib/logging.h"
5af447e5 26
57952005 27#include "compat/compiler.h"
c3df794d 28#include "compat/glib.h"
57952005
MJ
29#include "lib/trace-ir/clock-class.h"
30#include "lib/trace-ir/clock-snapshot.h"
71c5da58
MJ
31#include <babeltrace2/trace-ir/field.h>
32#include <babeltrace2/trace-ir/event-const.h>
57952005 33#include "lib/trace-ir/event.h"
71c5da58 34#include <babeltrace2/trace-ir/packet-const.h>
57952005
MJ
35#include "lib/trace-ir/packet.h"
36#include "lib/trace-ir/stream.h"
fb25b9e3
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>
71c5da58 40#include <babeltrace2/graph/connection-const.h>
71c5da58 41#include <babeltrace2/graph/component-const.h>
71c5da58 42#include <babeltrace2/graph/component-sink-const.h>
71c5da58 43#include <babeltrace2/graph/message-const.h>
fb25b9e3 44#include <babeltrace2/graph/message-iterator.h>
71c5da58 45#include <babeltrace2/graph/message-event-const.h>
143feff5
SM
46#include <babeltrace2/graph/message-message-iterator-inactivity-const.h>
47#include <babeltrace2/graph/message-packet-beginning.h>
71c5da58
MJ
48#include <babeltrace2/graph/message-packet-beginning-const.h>
49#include <babeltrace2/graph/message-packet-end-const.h>
143feff5 50#include <babeltrace2/graph/message-stream-beginning.h>
71c5da58
MJ
51#include <babeltrace2/graph/message-stream-beginning-const.h>
52#include <babeltrace2/graph/message-stream-end-const.h>
71c5da58
MJ
53#include <babeltrace2/graph/port-const.h>
54#include <babeltrace2/graph/graph.h>
55#include <babeltrace2/graph/graph-const.h>
71c5da58 56#include <babeltrace2/types.h>
57952005
MJ
57#include "common/assert.h"
58#include "lib/assert-pre.h"
3b7d3ccc 59#include "lib/assert-post.h"
fa054faf 60#include <stdint.h>
b04a393b 61#include <inttypes.h>
969c1d8a 62#include <stdbool.h>
0fbb9a9f 63#include <stdlib.h>
3230ee6b 64
57952005 65#include "component-class.h"
57952005
MJ
66#include "component.h"
67#include "component-sink.h"
68#include "component-source.h"
69#include "connection.h"
70#include "graph.h"
68e2deed 71#include "message-iterator-class.h"
57952005
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"
fb25b9e3 79#include "lib/func-status.h"
57952005 80
3fd7b79d
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 */
b09a5592 85#define MSG_BATCH_SIZE 15
3fd7b79d 86
15a52f66 87#define BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(_iter) \
fbd8a4e0
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, \
15a52f66 92 "Message iterator is in the wrong state: %!+i", _iter)
47e5a032 93
904860cb 94static inline
fbd8a4e0
PP
95void set_msg_iterator_state(struct bt_message_iterator *iterator,
96 enum bt_message_iterator_state state)
904860cb 97{
ec4a3354 98 BT_ASSERT_DBG(iterator);
15a52f66 99 BT_LIB_LOGD("Updating message iterator's state: new-state=%s",
fbd8a4e0 100 bt_message_iterator_state_string(state));
904860cb
PP
101 iterator->state = state;
102}
103
47e5a032 104static
fbd8a4e0 105void bt_message_iterator_destroy(struct bt_object *obj)
47e5a032 106{
fbd8a4e0 107 struct bt_message_iterator *iterator;
8738a040 108
8b45963b 109 BT_ASSERT(obj);
d3eb6e8f 110
bd14d768 111 /*
b09a5592 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
b09a5592 115 * message iterator's finalization function does
834e9996
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 */
1d7bf349 122 obj->ref_count++;
94a96686 123 iterator = (void *) obj;
a684a357 124 BT_LIB_LOGI("Destroying self component input port message iterator object: "
834e9996 125 "%!+i", iterator);
fbd8a4e0 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);
834e9996 135 iterator->connection = NULL;
bd14d768
PP
136 }
137
1b3630b6
SM
138 if (iterator->auto_seek.msgs) {
139 while (!g_queue_is_empty(iterator->auto_seek.msgs)) {
864aa43f 140 bt_object_put_ref_no_null_check(
1b3630b6 141 g_queue_pop_tail(iterator->auto_seek.msgs));
15a52f66
PP
142 }
143
1b3630b6
SM
144 g_queue_free(iterator->auto_seek.msgs);
145 iterator->auto_seek.msgs = NULL;
15a52f66
PP
146 }
147
692f1a01
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
fac7b25a
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
fbd8a4e0
PP
167void bt_message_iterator_try_finalize(
168 struct bt_message_iterator *iterator)
bd14d768 169{
692f1a01 170 uint64_t i;
ec60bdaa 171 bool call_user_finalize = true;
bd14d768 172
8b45963b 173 BT_ASSERT(iterator);
bd14d768
PP
174
175 switch (iterator->state) {
fbd8a4e0 176 case BT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED:
ec60bdaa
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;
fbd8a4e0 189 case BT_MESSAGE_ITERATOR_STATE_FINALIZED:
bd14d768 190 /* Already finalized */
b09a5592 191 BT_LIB_LOGD("Not finalizing message iterator: already finalized: "
834e9996 192 "%!+i", iterator);
904860cb 193 goto end;
fbd8a4e0 194 case BT_MESSAGE_ITERATOR_STATE_FINALIZING:
a8f90e5d 195 /* Finalizing */
904860cb
PP
196 BT_LIB_LOGF("Message iterator is already being finalized: "
197 "%!+i", iterator);
24847fc7 198 bt_common_abort();
bd14d768
PP
199 default:
200 break;
201 }
202
b09a5592 203 BT_LIB_LOGD("Finalizing message iterator: %!+i", iterator);
fbd8a4e0
PP
204 set_msg_iterator_state(iterator,
205 BT_MESSAGE_ITERATOR_STATE_FINALIZING);
8b45963b 206 BT_ASSERT(iterator->upstream_component);
bd14d768
PP
207
208 /* Call user-defined destroy method */
ec60bdaa 209 if (call_user_finalize) {
68e2deed 210 typedef void (*method_t)(void *);
4ba42e00 211 method_t method;
ec60bdaa
SM
212 struct bt_component_class *comp_class =
213 iterator->upstream_component->class;
4ba42e00 214 struct bt_component_class_with_iterator_class *class_with_iter_class;
ec60bdaa 215
4ba42e00
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
ec60bdaa
SM
221 if (method) {
222 const bt_error *saved_error;
7c4a2fba 223
ec60bdaa 224 saved_error = bt_current_thread_take_error();
7c4a2fba 225
ec60bdaa
SM
226 BT_LIB_LOGD("Calling user's finalization method: %!+i",
227 iterator);
228 method(iterator);
7c4a2fba 229
ec60bdaa
SM
230 if (saved_error) {
231 BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(saved_error);
232 }
7c4a2fba 233 }
bd14d768
PP
234 }
235
692f1a01
PP
236 /* Detach upstream message iterators */
237 for (i = 0; i < iterator->upstream_msg_iters->len; i++) {
fbd8a4e0 238 struct bt_message_iterator *upstream_msg_iter =
692f1a01
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;
fbd8a4e0
PP
259 set_msg_iterator_state(iterator,
260 BT_MESSAGE_ITERATOR_STATE_FINALIZED);
b09a5592 261 BT_LIB_LOGD("Finalized message iterator: %!+i", iterator);
904860cb
PP
262
263end:
264 return;
bd14d768
PP
265}
266
267BT_HIDDEN
fbd8a4e0
PP
268void bt_message_iterator_set_connection(
269 struct bt_message_iterator *iterator,
bd14d768
PP
270 struct bt_connection *connection)
271{
8b45963b 272 BT_ASSERT(iterator);
bd14d768 273 iterator->connection = connection;
a684a357 274 BT_LIB_LOGI("Set message iterator's connection: "
834e9996 275 "%![iter-]+i, %![conn-]+x", iterator, connection);
bd14d768
PP
276}
277
15a52f66 278static
9e8e8b43 279enum bt_message_iterator_can_seek_beginning_status can_seek_ns_from_origin_true(
fbd8a4e0 280 struct bt_message_iterator *iterator,
9e8e8b43 281 int64_t ns_from_origin, bt_bool *can_seek)
15a52f66 282{
9e8e8b43
SM
283 *can_seek = BT_TRUE;
284
285 return BT_FUNC_STATUS_OK;
15a52f66
PP
286}
287
288static
9e8e8b43 289enum bt_message_iterator_can_seek_beginning_status can_seek_beginning_true(
fbd8a4e0 290 struct bt_message_iterator *iterator,
9e8e8b43 291 bt_bool *can_seek)
15a52f66 292{
9e8e8b43
SM
293 *can_seek = BT_TRUE;
294
295 return BT_FUNC_STATUS_OK;
15a52f66
PP
296}
297
834e9996 298static
ab8b2b1b 299int create_self_component_input_port_message_iterator(
692f1a01 300 struct bt_self_message_iterator *self_downstream_msg_iter,
ab8b2b1b 301 struct bt_self_component_port_input *self_port,
fbd8a4e0 302 struct bt_message_iterator **message_iterator)
47e5a032 303{
68e2deed 304 bt_message_iterator_class_initialize_method init_method = NULL;
fbd8a4e0 305 struct bt_message_iterator *iterator =
692f1a01 306 NULL;
fbd8a4e0 307 struct bt_message_iterator *downstream_msg_iter =
692f1a01
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;
4ba42e00 314 struct bt_component_class_with_iterator_class *upstream_comp_cls_with_iter_cls;
ab8b2b1b 315 int status;
47e5a032 316
ab8b2b1b 317 BT_ASSERT_PRE_NON_NULL(message_iterator, "Created message iterator");
692f1a01
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);
692f1a01
PP
324 BT_ASSERT(port->connection);
325 upstream_port = port->connection->upstream_port;
8b45963b 326 BT_ASSERT(upstream_port);
692f1a01
PP
327 upstream_comp = bt_port_borrow_component_inline(upstream_port);
328 BT_ASSERT(upstream_comp);
329 BT_ASSERT_PRE(
d73bb381
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,
692f1a01
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 ==
834e9996 338 BT_COMPONENT_CLASS_TYPE_SOURCE ||
692f1a01 339 upstream_comp->class->type ==
834e9996 340 BT_COMPONENT_CLASS_TYPE_FILTER);
692f1a01
PP
341 BT_LIB_LOGI("Creating message iterator on self component input port: "
342 "%![up-comp-]+c, %![up-port-]+p", upstream_comp, upstream_port);
834e9996 343 iterator = g_new0(
fbd8a4e0 344 struct bt_message_iterator, 1);
47e5a032 345 if (!iterator) {
a8f90e5d
PP
346 BT_LIB_LOGE_APPEND_CAUSE(
347 "Failed to allocate one self component input port "
b09a5592 348 "message iterator.");
ab8b2b1b 349 status = BT_FUNC_STATUS_MEMORY_ERROR;
a8f90e5d 350 goto error;
47e5a032
JG
351 }
352
fac7b25a 353 bt_object_init_shared(&iterator->base,
fbd8a4e0 354 bt_message_iterator_destroy);
fac7b25a
PP
355 iterator->msgs = g_ptr_array_new();
356 if (!iterator->msgs) {
357 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
ab8b2b1b 358 status = BT_FUNC_STATUS_MEMORY_ERROR;
a8f90e5d 359 goto error;
3fd7b79d 360 }
3230ee6b 361
fac7b25a 362 g_ptr_array_set_size(iterator->msgs, MSG_BATCH_SIZE);
e253ad8d 363 iterator->last_ns_from_origin = INT64_MIN;
1b3630b6
SM
364 iterator->auto_seek.msgs = g_queue_new();
365 if (!iterator->auto_seek.msgs) {
a8f90e5d 366 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GQueue.");
ab8b2b1b 367 status = BT_FUNC_STATUS_MEMORY_ERROR;
692f1a01
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.");
ab8b2b1b 374 status = BT_FUNC_STATUS_MEMORY_ERROR;
692f1a01 375 goto error;
3230ee6b
PP
376 }
377
bd14d768
PP
378 iterator->upstream_component = upstream_comp;
379 iterator->upstream_port = upstream_port;
834e9996 380 iterator->connection = iterator->upstream_port->connection;
f7c3ac09 381 iterator->graph = bt_component_borrow_graph(upstream_comp);
fbd8a4e0
PP
382 set_msg_iterator_state(iterator,
383 BT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED);
15a52f66 384
4ba42e00
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 =
fbd8a4e0 391 (bt_message_iterator_next_method)
4ba42e00
SM
392 upstream_comp_cls_with_iter_cls->msg_iter_cls->methods.next;
393 iterator->methods.seek_ns_from_origin =
fbd8a4e0 394 (bt_message_iterator_seek_ns_from_origin_method)
4ba42e00
SM
395 upstream_comp_cls_with_iter_cls->msg_iter_cls->methods.seek_ns_from_origin;
396 iterator->methods.seek_beginning =
fbd8a4e0 397 (bt_message_iterator_seek_beginning_method)
4ba42e00
SM
398 upstream_comp_cls_with_iter_cls->msg_iter_cls->methods.seek_beginning;
399 iterator->methods.can_seek_ns_from_origin =
fbd8a4e0 400 (bt_message_iterator_can_seek_ns_from_origin_method)
4ba42e00
SM
401 upstream_comp_cls_with_iter_cls->msg_iter_cls->methods.can_seek_ns_from_origin;
402 iterator->methods.can_seek_beginning =
fbd8a4e0 403 (bt_message_iterator_can_seek_beginning_method)
4ba42e00 404 upstream_comp_cls_with_iter_cls->msg_iter_cls->methods.can_seek_beginning;
15a52f66
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 =
fbd8a4e0 409 (bt_message_iterator_can_seek_ns_from_origin_method)
15a52f66
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 =
fbd8a4e0 416 (bt_message_iterator_can_seek_beginning_method)
15a52f66
PP
417 can_seek_beginning_true;
418 }
419
4ba42e00
SM
420 /* Call iterator's init method. */
421 init_method = upstream_comp_cls_with_iter_cls->msg_iter_cls->methods.initialize;
834e9996
PP
422
423 if (init_method) {
68e2deed 424 enum bt_message_iterator_class_initialize_method_status iter_status;
834e9996
PP
425
426 BT_LIB_LOGD("Calling user's initialization method: %!+i", iterator);
68e2deed
SM
427 iter_status = init_method(
428 (struct bt_self_message_iterator *) iterator,
429 &iterator->config,
68e2deed 430 (struct bt_self_component_port_output *) upstream_port);
834e9996 431 BT_LOGD("User method returned: status=%s",
fb25b9e3 432 bt_common_func_status_string(iter_status));
d6f6a5aa 433 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(iter_status);
fb25b9e3 434 if (iter_status != BT_FUNC_STATUS_OK) {
a8f90e5d
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));
ab8b2b1b 440 status = iter_status;
a8f90e5d 441 goto error;
834e9996 442 }
9415de1c
SM
443
444 iterator->config.frozen = true;
834e9996
PP
445 }
446
692f1a01
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
fbd8a4e0
PP
459 set_msg_iterator_state(iterator,
460 BT_MESSAGE_ITERATOR_STATE_ACTIVE);
834e9996 461 g_ptr_array_add(port->connection->iterators, iterator);
a684a357 462 BT_LIB_LOGI("Created message iterator on self component input port: "
834e9996
PP
463 "%![up-port-]+p, %![up-comp-]+c, %![iter-]+i",
464 upstream_port, upstream_comp, iterator);
ab8b2b1b
SM
465
466 *message_iterator = iterator;
467 status = BT_FUNC_STATUS_OK;
a8f90e5d
PP
468 goto end;
469
470error:
471 BT_OBJECT_PUT_REF_AND_RESET(iterator);
834e9996
PP
472
473end:
ab8b2b1b 474 return status;
ea8d3e58
JG
475}
476
fbd8a4e0
PP
477bt_message_iterator_create_from_message_iterator_status
478bt_message_iterator_create_from_message_iterator(
692f1a01 479 struct bt_self_message_iterator *self_msg_iter,
ab8b2b1b 480 struct bt_self_component_port_input *input_port,
fbd8a4e0 481 struct bt_message_iterator **message_iterator)
692f1a01 482{
7c7324d3 483 BT_ASSERT_PRE_NO_ERROR();
692f1a01
PP
484 BT_ASSERT_PRE_NON_NULL(self_msg_iter, "Message iterator");
485 return create_self_component_input_port_message_iterator(self_msg_iter,
ab8b2b1b 486 input_port, message_iterator);
692f1a01
PP
487}
488
fbd8a4e0
PP
489bt_message_iterator_create_from_sink_component_status
490bt_message_iterator_create_from_sink_component(
692f1a01 491 struct bt_self_component_sink *self_comp,
ab8b2b1b 492 struct bt_self_component_port_input *input_port,
fbd8a4e0 493 struct bt_message_iterator **message_iterator)
692f1a01 494{
7c7324d3 495 BT_ASSERT_PRE_NO_ERROR();
692f1a01
PP
496 BT_ASSERT_PRE_NON_NULL(self_comp, "Sink component");
497 return create_self_component_input_port_message_iterator(NULL,
ab8b2b1b 498 input_port, message_iterator);
692f1a01
PP
499}
500
b09a5592
PP
501void *bt_self_message_iterator_get_data(
502 const struct bt_self_message_iterator *self_iterator)
ea8d3e58 503{
fbd8a4e0 504 struct bt_message_iterator *iterator =
834e9996 505 (void *) self_iterator;
ea8d3e58 506
fa6cfec3 507 BT_ASSERT_PRE_DEV_NON_NULL(iterator, "Message iterator");
834e9996 508 return iterator->user_data;
8738a040 509}
413bc2c4 510
b09a5592
PP
511void bt_self_message_iterator_set_data(
512 struct bt_self_message_iterator *self_iterator, void *data)
f7c3ac09 513{
fbd8a4e0 514 struct bt_message_iterator *iterator =
834e9996 515 (void *) self_iterator;
f7c3ac09 516
fa6cfec3 517 BT_ASSERT_PRE_DEV_NON_NULL(iterator, "Message iterator");
834e9996 518 iterator->user_data = data;
a684a357 519 BT_LIB_LOGD("Set message iterator's user data: "
834e9996 520 "%!+i, user-data-addr=%p", iterator, data);
f7c3ac09
PP
521}
522
9415de1c
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
e253ad8d
SM
533/*
534 * Validate that the default clock snapshot in `msg` doesn't make us go back in
535 * time.
536 */
537
fa6cfec3 538BT_ASSERT_POST_DEV_FUNC
e253ad8d
SM
539static
540bool clock_snapshots_are_monotonic_one(
fbd8a4e0 541 struct bt_message_iterator *iterator,
e253ad8d
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;
fb25b9e3 547 enum bt_clock_snapshot_get_ns_from_origin_status clock_snapshot_status;
e253ad8d
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;
566 clock_snapshot = inactivity_msg->default_cs;
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 }
b7cbc799
SM
576 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
577 case BT_MESSAGE_TYPE_STREAM_END:
e253ad8d 578 {
b7cbc799
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;
e253ad8d 582 }
b7cbc799
SM
583
584 clock_snapshot = stream_msg->default_cs;
e253ad8d
SM
585 break;
586 }
e253ad8d
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
80b0f1c7
SM
602 clock_snapshot_status = bt_clock_snapshot_get_ns_from_origin(
603 clock_snapshot, &ns_from_origin);
fb25b9e3 604 if (clock_snapshot_status != BT_FUNC_STATUS_OK) {
80b0f1c7
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();
e253ad8d
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
fa6cfec3 620BT_ASSERT_POST_DEV_FUNC
e253ad8d
SM
621static
622bool clock_snapshots_are_monotonic(
fbd8a4e0 623 struct bt_message_iterator *iterator,
e253ad8d
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
fa6cfec3 647BT_ASSERT_POST_DEV_FUNC
e253ad8d 648static
fbd8a4e0 649bool clock_classes_are_compatible_one(struct bt_message_iterator *iterator,
e253ad8d
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;
d126826c 679 bt_uuid_copy(iterator->clock_expectation.uuid, clock_class_uuid);
e253ad8d
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) {
fa6cfec3
PP
687 BT_ASSERT_POST_DEV_MSG(
688 "Expecting no clock class, got one: %![cc-]+K",
e253ad8d
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) {
fa6cfec3
PP
698 BT_ASSERT_POST_DEV_MSG(
699 "Expecting a clock class, got none.");
e253ad8d
SM
700 result = false;
701 goto end;
702 }
703
704 if (!bt_clock_class_origin_is_unix_epoch(clock_class)) {
fa6cfec3
PP
705 BT_ASSERT_POST_DEV_MSG(
706 "Expecting a clock class with Unix epoch origin: %![cc-]+K",
e253ad8d
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) {
fa6cfec3
PP
715 BT_ASSERT_POST_DEV_MSG(
716 "Expecting a clock class, got none.");
e253ad8d
SM
717 result = false;
718 goto end;
719 }
720
721 if (bt_clock_class_origin_is_unix_epoch(clock_class)) {
fa6cfec3
PP
722 BT_ASSERT_POST_DEV_MSG(
723 "Expecting a clock class without Unix epoch origin: %![cc-]+K",
e253ad8d
SM
724 clock_class);
725 result = false;
726 goto end;
727 }
728
729 if (!clock_class_uuid) {
fa6cfec3
PP
730 BT_ASSERT_POST_DEV_MSG(
731 "Expecting a clock class with UUID: %![cc-]+K",
e253ad8d
SM
732 clock_class);
733 result = false;
734 goto end;
735 }
736
737 if (bt_uuid_compare(iterator->clock_expectation.uuid, clock_class_uuid)) {
fa6cfec3
PP
738 BT_ASSERT_POST_DEV_MSG(
739 "Expecting a clock class with UUID, got one "
e253ad8d
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) {
fa6cfec3
PP
749 BT_ASSERT_POST_DEV_MSG(
750 "Expecting a clock class, got none.");
e253ad8d
SM
751 result = false;
752 goto end;
753 }
754
755 if (bt_clock_class_origin_is_unix_epoch(clock_class)) {
fa6cfec3
PP
756 BT_ASSERT_POST_DEV_MSG(
757 "Expecting a clock class without Unix epoch origin: %![cc-]+K",
e253ad8d
SM
758 clock_class);
759 result = false;
760 goto end;
761 }
762
763 if (clock_class_uuid) {
fa6cfec3
PP
764 BT_ASSERT_POST_DEV_MSG(
765 "Expecting a clock class without UUID: %![cc-]+K",
e253ad8d
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
fa6cfec3 780BT_ASSERT_POST_DEV_FUNC
e253ad8d
SM
781static
782bool clock_classes_are_compatible(
fbd8a4e0 783 struct bt_message_iterator *iterator,
e253ad8d
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
68e2deed 808enum bt_message_iterator_class_next_method_status
fb25b9e3 809call_iterator_next_method(
fbd8a4e0 810 struct bt_message_iterator *iterator,
e253ad8d
SM
811 bt_message_array_const msgs, uint64_t capacity, uint64_t *user_count)
812{
68e2deed 813 enum bt_message_iterator_class_next_method_status status;
e253ad8d 814
ec4a3354 815 BT_ASSERT_DBG(iterator->methods.next);
e253ad8d 816 BT_LOGD_STR("Calling user's \"next\" method.");
e253ad8d 817 status = iterator->methods.next(iterator, msgs, capacity, user_count);
3b7d3ccc 818 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64,
fb25b9e3 819 bt_common_func_status_string(status), *user_count);
e253ad8d 820
fb25b9e3 821 if (status == BT_FUNC_STATUS_OK) {
fa6cfec3 822 BT_ASSERT_POST_DEV(clock_classes_are_compatible(iterator, msgs, *user_count),
e253ad8d 823 "Clocks are not compatible");
fa6cfec3 824 BT_ASSERT_POST_DEV(clock_snapshots_are_monotonic(iterator, msgs, *user_count),
e253ad8d
SM
825 "Clock snapshots are not monotonic");
826 }
827
d6f6a5aa
SM
828 BT_ASSERT_POST_DEV_NO_ERROR_IF_NO_ERROR_STATUS(status);
829
e253ad8d
SM
830 return status;
831}
832
fb25b9e3 833enum bt_message_iterator_next_status
fbd8a4e0
PP
834bt_message_iterator_next(
835 struct bt_message_iterator *iterator,
b09a5592 836 bt_message_array_const *msgs, uint64_t *user_count)
3230ee6b 837{
fb25b9e3 838 enum bt_message_iterator_next_status status = BT_FUNC_STATUS_OK;
834e9996 839
7c7324d3 840 BT_ASSERT_PRE_DEV_NO_ERROR();
fa6cfec3
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 ==
fbd8a4e0 845 BT_MESSAGE_ITERATOR_STATE_ACTIVE,
b09a5592 846 "Message iterator's \"next\" called, but "
15a52f66 847 "message iterator is in the wrong state: %!+i", iterator);
ec4a3354
PP
848 BT_ASSERT_DBG(iterator->upstream_component);
849 BT_ASSERT_DBG(iterator->upstream_component->class);
fa6cfec3 850 BT_ASSERT_PRE_DEV(
1043fdea
PP
851 bt_component_borrow_graph(iterator->upstream_component)->config_state !=
852 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
9cce94e4
PP
853 "Graph is not configured: %!+g",
854 bt_component_borrow_graph(iterator->upstream_component));
834e9996 855 BT_LIB_LOGD("Getting next self component input port "
a684a357
PP
856 "message iterator's messages: %!+i, batch-size=%u",
857 iterator, MSG_BATCH_SIZE);
d3eb6e8f 858
3230ee6b 859 /*
b09a5592 860 * Call the user's "next" method to get the next messages
fa054faf 861 * and status.
3230ee6b 862 */
a684a357 863 *user_count = 0;
fb25b9e3 864 status = (int) call_iterator_next_method(iterator,
fac7b25a 865 (void *) iterator->msgs->pdata, MSG_BATCH_SIZE,
15a52f66 866 user_count);
a8f90e5d
PP
867 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64,
868 bt_common_func_status_string(status), *user_count);
3fd7b79d 869 if (status < 0) {
a8f90e5d
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));
6ff151ad
PP
874 goto end;
875 }
3230ee6b 876
904860cb
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.
15a52f66
PP
882 *
883 * For the same reason, there is no way that this iterator could
884 * have seeked (cannot seek a self message iterator).
904860cb 885 */
ec4a3354 886 BT_ASSERT_DBG(iterator->state ==
fbd8a4e0 887 BT_MESSAGE_ITERATOR_STATE_ACTIVE);
8cf27cc5 888
3fd7b79d 889 switch (status) {
fb25b9e3 890 case BT_FUNC_STATUS_OK:
fa6cfec3 891 BT_ASSERT_POST_DEV(*user_count <= MSG_BATCH_SIZE,
15a52f66
PP
892 "Invalid returned message count: greater than "
893 "batch size: count=%" PRIu64 ", batch-size=%u",
894 *user_count, MSG_BATCH_SIZE);
fac7b25a 895 *msgs = (void *) iterator->msgs->pdata;
3fd7b79d 896 break;
fb25b9e3 897 case BT_FUNC_STATUS_AGAIN:
3fd7b79d 898 goto end;
fb25b9e3 899 case BT_FUNC_STATUS_END:
fbd8a4e0
PP
900 set_msg_iterator_state(iterator,
901 BT_MESSAGE_ITERATOR_STATE_ENDED);
6ff151ad 902 goto end;
6ff151ad
PP
903 default:
904 /* Unknown non-error status */
24847fc7 905 bt_common_abort();
41a2b7ae
PP
906 }
907
908end:
3230ee6b
PP
909 return status;
910}
911
15a52f66 912struct bt_component *
fbd8a4e0
PP
913bt_message_iterator_borrow_component(
914 struct bt_message_iterator *iterator)
834e9996 915{
fa6cfec3 916 BT_ASSERT_PRE_DEV_NON_NULL(iterator, "Message iterator");
834e9996
PP
917 return iterator->upstream_component;
918}
919
b09a5592
PP
920struct bt_self_component *bt_self_message_iterator_borrow_component(
921 struct bt_self_message_iterator *self_iterator)
413bc2c4 922{
fbd8a4e0 923 struct bt_message_iterator *iterator =
834e9996 924 (void *) self_iterator;
fe7265b5 925
fa6cfec3 926 BT_ASSERT_PRE_DEV_NON_NULL(iterator, "Message iterator");
834e9996 927 return (void *) iterator->upstream_component;
413bc2c4
JG
928}
929
149c1f3a 930struct bt_self_component_port_output *bt_self_message_iterator_borrow_port(
b09a5592 931 struct bt_self_message_iterator *self_iterator)
91457551 932{
fbd8a4e0 933 struct bt_message_iterator *iterator =
834e9996
PP
934 (void *) self_iterator;
935
fa6cfec3 936 BT_ASSERT_PRE_DEV_NON_NULL(iterator, "Message iterator");
834e9996 937 return (void *) iterator->upstream_port;
91457551 938}
c3ac0193 939
9e8e8b43 940enum bt_message_iterator_can_seek_ns_from_origin_status
fbd8a4e0
PP
941bt_message_iterator_can_seek_ns_from_origin(
942 struct bt_message_iterator *iterator,
9e8e8b43 943 int64_t ns_from_origin, bt_bool *can_seek)
15a52f66 944{
9e8e8b43 945 enum bt_message_iterator_can_seek_ns_from_origin_status status;
15a52f66 946
7c7324d3 947 BT_ASSERT_PRE_NO_ERROR();
15a52f66 948 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
9e8e8b43 949 BT_ASSERT_PRE_NON_NULL(can_seek, "Result (output)");
15a52f66 950 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator);
1043fdea
PP
951 BT_ASSERT_PRE(
952 bt_component_borrow_graph(iterator->upstream_component)->config_state !=
953 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
15a52f66
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) {
9e8e8b43
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
c49bf79b
SM
964 BT_LIB_LOGD("Calling user's \"can seek nanoseconds from origin\" method: %!+i",
965 iterator);
966
9e8e8b43
SM
967 status = (int) iterator->methods.can_seek_ns_from_origin(iterator,
968 ns_from_origin, can_seek);
969
d6f6a5aa
SM
970 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status);
971
c49bf79b
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,
9e8e8b43
SM
981 "Unexpected boolean value returned from user's \"can seek ns from origin\" method: val=%d, %![iter-]+i",
982 *can_seek, iterator);
983
c49bf79b
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 }
15a52f66
PP
992 }
993
994 /*
c49bf79b
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.
15a52f66 998 */
fbd8a4e0 999 status = (int) bt_message_iterator_can_seek_beginning(
9e8e8b43 1000 iterator, can_seek);
c49bf79b
SM
1001 if (status != BT_FUNC_STATUS_OK) {
1002 goto end;
1003 }
1004
1005 *can_seek = *can_seek && iterator->config.can_seek_forward;
15a52f66
PP
1006
1007end:
9e8e8b43 1008 return status;
15a52f66
PP
1009}
1010
9e8e8b43 1011enum bt_message_iterator_can_seek_beginning_status
fbd8a4e0
PP
1012bt_message_iterator_can_seek_beginning(
1013 struct bt_message_iterator *iterator,
9e8e8b43 1014 bt_bool *can_seek)
15a52f66 1015{
9e8e8b43 1016 enum bt_message_iterator_can_seek_beginning_status status;
15a52f66 1017
7c7324d3 1018 BT_ASSERT_PRE_NO_ERROR();
15a52f66 1019 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
9e8e8b43 1020 BT_ASSERT_PRE_NON_NULL(can_seek, "Result (output)");
15a52f66 1021 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator);
1043fdea
PP
1022 BT_ASSERT_PRE(
1023 bt_component_borrow_graph(iterator->upstream_component)->config_state !=
1024 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
15a52f66
PP
1025 "Graph is not configured: %!+g",
1026 bt_component_borrow_graph(iterator->upstream_component));
1027
1028 if (iterator->methods.can_seek_beginning) {
9e8e8b43
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);
d6f6a5aa 1043 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status);
9e8e8b43
SM
1044 } else {
1045 *can_seek = BT_FALSE;
1046 status = BT_FUNC_STATUS_OK;
15a52f66
PP
1047 }
1048
9e8e8b43 1049 return status;
15a52f66
PP
1050}
1051
1052static inline
1d55aa9a 1053void set_iterator_state_after_seeking(
fbd8a4e0 1054 struct bt_message_iterator *iterator,
fb25b9e3 1055 int status)
15a52f66 1056{
fbd8a4e0 1057 enum bt_message_iterator_state new_state = 0;
15a52f66
PP
1058
1059 /* Set iterator's state depending on seeking status */
1060 switch (status) {
fb25b9e3 1061 case BT_FUNC_STATUS_OK:
fbd8a4e0 1062 new_state = BT_MESSAGE_ITERATOR_STATE_ACTIVE;
15a52f66 1063 break;
fb25b9e3 1064 case BT_FUNC_STATUS_AGAIN:
fbd8a4e0 1065 new_state = BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN;
15a52f66 1066 break;
fb25b9e3
PP
1067 case BT_FUNC_STATUS_ERROR:
1068 case BT_FUNC_STATUS_MEMORY_ERROR:
fbd8a4e0 1069 new_state = BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR;
15a52f66 1070 break;
fb25b9e3 1071 case BT_FUNC_STATUS_END:
fbd8a4e0 1072 new_state = BT_MESSAGE_ITERATOR_STATE_ENDED;
15a52f66
PP
1073 break;
1074 default:
24847fc7 1075 bt_common_abort();
15a52f66
PP
1076 }
1077
fbd8a4e0 1078 set_msg_iterator_state(iterator, new_state);
15a52f66
PP
1079}
1080
e253ad8d
SM
1081static
1082void reset_iterator_expectations(
fbd8a4e0 1083 struct bt_message_iterator *iterator)
e253ad8d
SM
1084{
1085 iterator->last_ns_from_origin = INT64_MIN;
1086 iterator->clock_expectation.type = CLOCK_EXPECTATION_UNSET;
1087}
1088
9e8e8b43
SM
1089static
1090bool message_iterator_can_seek_beginning(
fbd8a4e0 1091 struct bt_message_iterator *iterator)
9e8e8b43
SM
1092{
1093 enum bt_message_iterator_can_seek_beginning_status status;
1094 bt_bool can_seek;
1095
fbd8a4e0 1096 status = bt_message_iterator_can_seek_beginning(
9e8e8b43
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
fb25b9e3 1105enum bt_message_iterator_seek_beginning_status
fbd8a4e0
PP
1106bt_message_iterator_seek_beginning(
1107 struct bt_message_iterator *iterator)
15a52f66 1108{
621b4e7e 1109 int status;
15a52f66 1110
7c7324d3 1111 BT_ASSERT_PRE_NO_ERROR();
15a52f66
PP
1112 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
1113 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator);
1043fdea
PP
1114 BT_ASSERT_PRE(
1115 bt_component_borrow_graph(iterator->upstream_component)->config_state !=
1116 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
15a52f66
PP
1117 "Graph is not configured: %!+g",
1118 bt_component_borrow_graph(iterator->upstream_component));
9e8e8b43 1119 BT_ASSERT_PRE(message_iterator_can_seek_beginning(iterator),
15a52f66 1120 "Message iterator cannot seek beginning: %!+i", iterator);
e253ad8d
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
15a52f66 1128 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i", iterator);
fbd8a4e0
PP
1129 set_msg_iterator_state(iterator,
1130 BT_MESSAGE_ITERATOR_STATE_SEEKING);
15a52f66
PP
1131 status = iterator->methods.seek_beginning(iterator);
1132 BT_LOGD("User method returned: status=%s",
fb25b9e3
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,
15a52f66 1138 "Unexpected status: %![iter-]+i, status=%s",
fb25b9e3 1139 iterator, bt_common_func_status_string(status));
d6f6a5aa 1140 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status);
a8f90e5d
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
15a52f66
PP
1148 set_iterator_state_after_seeking(iterator, status);
1149 return status;
1150}
1151
9415de1c 1152bt_bool
fbd8a4e0
PP
1153bt_message_iterator_can_seek_forward(
1154 bt_message_iterator *iterator)
9415de1c
SM
1155{
1156 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
1157
1158 return iterator->config.can_seek_forward;
1159}
1160
143feff5
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 --->
b7cbc799 1170 * [SB] 1 [PB] 2 [PE] 1 [SE]
143feff5
SM
1171 *
1172 * At each point in the timeline, the messages we need to replicate are:
1173 *
1174 * 1: Stream beginning
b7cbc799 1175 * 2: Stream beginning, packet beginning
143feff5
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,
143feff5
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;
b7cbc799
SM
1191
1192 /* Have we see a message with a clock snapshot yet? */
1193 bool seen_clock_snapshot;
143feff5
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
15a52f66 1233static inline
fb25b9e3 1234int auto_seek_handle_message(
fbd8a4e0 1235 struct bt_message_iterator *iterator,
3d902f17 1236 int64_t ns_from_origin, const struct bt_message *msg,
143feff5 1237 bool *got_first, GHashTable *stream_states)
15a52f66 1238{
fb25b9e3 1239 int status = BT_FUNC_STATUS_OK;
3d902f17 1240 int64_t msg_ns_from_origin;
15a52f66 1241 const struct bt_clock_snapshot *clk_snapshot = NULL;
3d902f17
PP
1242 int ret;
1243
ec4a3354
PP
1244 BT_ASSERT_DBG(msg);
1245 BT_ASSERT_DBG(got_first);
15a52f66
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
9c04b633 1253 clk_snapshot = event_msg->default_cs;
fa6cfec3 1254 BT_ASSERT_POST_DEV(clk_snapshot,
956dcc1e
PP
1255 "Event message has no default clock snapshot: %!+n",
1256 event_msg);
15a52f66
PP
1257 break;
1258 }
40bf6fd0 1259 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY:
15a52f66 1260 {
40bf6fd0 1261 const struct bt_message_message_iterator_inactivity *inactivity_msg =
15a52f66
PP
1262 (const void *) msg;
1263
15a52f66 1264 clk_snapshot = inactivity_msg->default_cs;
ec4a3354 1265 BT_ASSERT_DBG(clk_snapshot);
15a52f66
PP
1266 break;
1267 }
f629b891
PP
1268 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
1269 case BT_MESSAGE_TYPE_PACKET_END:
956dcc1e
PP
1270 {
1271 const struct bt_message_packet *packet_msg =
1272 (const void *) msg;
1273
1274 clk_snapshot = packet_msg->default_cs;
fa6cfec3 1275 BT_ASSERT_POST_DEV(clk_snapshot,
956dcc1e
PP
1276 "Packet message has no default clock snapshot: %!+n",
1277 packet_msg);
1278 break;
1279 }
f629b891
PP
1280 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
1281 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
15a52f66 1282 {
3d902f17
PP
1283 struct bt_message_discarded_items *msg_disc_items =
1284 (void *) msg;
1285
fa6cfec3 1286 BT_ASSERT_POST_DEV(msg_disc_items->default_begin_cs &&
3d902f17
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) {
fb25b9e3 1294 status = BT_FUNC_STATUS_ERROR;
3d902f17
PP
1295 goto end;
1296 }
15a52f66 1297
3d902f17
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) {
fb25b9e3 1307 status = BT_FUNC_STATUS_ERROR;
3d902f17
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 */
1564e3dc 1321 uint64_t new_begin_raw_value = 0;
3d902f17
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) {
fb25b9e3 1327 status = BT_FUNC_STATUS_ERROR;
3d902f17
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 }
15a52f66 1345 }
b7cbc799
SM
1346 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
1347 case BT_MESSAGE_TYPE_STREAM_END:
15a52f66 1348 {
b7cbc799
SM
1349 struct bt_message_stream *stream_msg =
1350 (struct bt_message_stream *) msg;
15a52f66 1351
b7cbc799
SM
1352 if (stream_msg->default_cs_state != BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN) {
1353 /* Ignore */
3d902f17 1354 goto skip_msg;
f629b891
PP
1355 }
1356
b7cbc799 1357 clk_snapshot = stream_msg->default_cs;
15a52f66
PP
1358 break;
1359 }
1360 default:
24847fc7 1361 bt_common_abort();
15a52f66
PP
1362 }
1363
ec4a3354 1364 BT_ASSERT_DBG(clk_snapshot);
3d902f17
PP
1365 ret = bt_clock_snapshot_get_ns_from_origin(clk_snapshot,
1366 &msg_ns_from_origin);
1367 if (ret) {
fb25b9e3 1368 status = BT_FUNC_STATUS_ERROR;
15a52f66
PP
1369 goto end;
1370 }
1371
3d902f17
PP
1372 if (msg_ns_from_origin >= ns_from_origin) {
1373 *got_first = true;
1374 goto push_msg;
1375 }
1376
1377skip_msg:
143feff5
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;
143feff5
SM
1384
1385 /* Update stream's state: stream began. */
1386 stream_state = create_auto_seek_stream_state();
1387 if (!stream_state) {
fb25b9e3 1388 status = BT_FUNC_STATUS_MEMORY_ERROR;
143feff5
SM
1389 goto end;
1390 }
1391
1392 stream_state->state = AUTO_SEEK_STREAM_STATE_STREAM_BEGAN;
c3df794d 1393
b7cbc799
SM
1394 if (stream_msg->default_cs_state == BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN) {
1395 stream_state->seen_clock_snapshot = true;
1396 }
1397
ec4a3354 1398 BT_ASSERT_DBG(!bt_g_hash_table_contains(stream_states, stream_msg->stream));
c3df794d 1399 g_hash_table_insert(stream_states, stream_msg->stream, stream_state);
143feff5
SM
1400 break;
1401 }
b7cbc799 1402 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
143feff5 1403 {
b7cbc799 1404 const struct bt_message_packet *packet_msg =
143feff5
SM
1405 (const void *) msg;
1406 struct auto_seek_stream_state *stream_state;
1407
b7cbc799
SM
1408 /* Update stream's state: packet began. */
1409 stream_state = g_hash_table_lookup(stream_states, packet_msg->packet->stream);
ec4a3354
PP
1410 BT_ASSERT_DBG(stream_state);
1411 BT_ASSERT_DBG(stream_state->state == AUTO_SEEK_STREAM_STATE_STREAM_BEGAN);
b7cbc799 1412 stream_state->state = AUTO_SEEK_STREAM_STATE_PACKET_BEGAN;
ec4a3354 1413 BT_ASSERT_DBG(!stream_state->packet);
b7cbc799
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
143feff5
SM
1420 break;
1421 }
b7cbc799 1422 case BT_MESSAGE_TYPE_EVENT:
143feff5 1423 {
b7cbc799 1424 const struct bt_message_event *event_msg = (const void *) msg;
143feff5
SM
1425 struct auto_seek_stream_state *stream_state;
1426
b7cbc799
SM
1427 stream_state = g_hash_table_lookup(stream_states,
1428 event_msg->event->packet->stream);
ec4a3354 1429 BT_ASSERT_DBG(stream_state);
143feff5 1430
b7cbc799
SM
1431 // HELPME: are we sure that event messages have clock snapshots at this point?
1432 stream_state->seen_clock_snapshot = true;
1433
143feff5
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);
ec4a3354
PP
1444 BT_ASSERT_DBG(stream_state);
1445 BT_ASSERT_DBG(stream_state->state == AUTO_SEEK_STREAM_STATE_PACKET_BEGAN);
b7cbc799 1446 stream_state->state = AUTO_SEEK_STREAM_STATE_STREAM_BEGAN;
ec4a3354 1447 BT_ASSERT_DBG(stream_state->packet);
143feff5 1448 stream_state->packet = NULL;
143feff5 1449
b7cbc799
SM
1450 if (packet_msg->packet->stream->class->packets_have_end_default_clock_snapshot) {
1451 stream_state->seen_clock_snapshot = true;
1452 }
143feff5 1453
143feff5
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);
ec4a3354
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);
143feff5
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 }
b7cbc799
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);
ec4a3354 1478 BT_ASSERT_DBG(stream_state);
b7cbc799
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 }
143feff5
SM
1487 default:
1488 break;
1489 }
1490
864aa43f 1491 bt_object_put_ref_no_null_check(msg);
27afdfda 1492 msg = NULL;
3d902f17
PP
1493 goto end;
1494
1495push_msg:
1b3630b6 1496 g_queue_push_tail(iterator->auto_seek.msgs, (void *) msg);
3d902f17 1497 msg = NULL;
15a52f66
PP
1498
1499end:
ec4a3354 1500 BT_ASSERT_DBG(!msg || status != BT_FUNC_STATUS_OK);
3d902f17 1501 return status;
15a52f66
PP
1502}
1503
1504static
fb25b9e3 1505int find_message_ge_ns_from_origin(
fbd8a4e0 1506 struct bt_message_iterator *iterator,
143feff5 1507 int64_t ns_from_origin, GHashTable *stream_states)
15a52f66 1508{
5999d54a 1509 int status = BT_FUNC_STATUS_OK;
fbd8a4e0 1510 enum bt_message_iterator_state init_state =
15a52f66
PP
1511 iterator->state;
1512 const struct bt_message *messages[MSG_BATCH_SIZE];
1513 uint64_t user_count = 0;
1514 uint64_t i;
3d902f17 1515 bool got_first = false;
15a52f66 1516
ec4a3354 1517 BT_ASSERT_DBG(iterator);
15a52f66
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 */
fbd8a4e0
PP
1524 set_msg_iterator_state(iterator,
1525 BT_MESSAGE_ITERATOR_STATE_ACTIVE);
15a52f66 1526
ec4a3354 1527 BT_ASSERT_DBG(iterator->methods.next);
15a52f66 1528
c980433a 1529 while (!got_first) {
15a52f66
PP
1530 /*
1531 * Call the user's "next" method to get the next
1532 * messages and status.
1533 */
e253ad8d 1534 status = call_iterator_next_method(iterator,
15a52f66 1535 &messages[0], MSG_BATCH_SIZE, &user_count);
a8f90e5d
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 }
15a52f66 1544
15a52f66
PP
1545 /*
1546 * The user's "next" method must not do any action which
1547 * would change the iterator's state.
1548 */
ec4a3354 1549 BT_ASSERT_DBG(iterator->state ==
fbd8a4e0 1550 BT_MESSAGE_ITERATOR_STATE_ACTIVE);
15a52f66
PP
1551
1552 switch (status) {
fb25b9e3 1553 case BT_FUNC_STATUS_OK:
fa6cfec3 1554 BT_ASSERT_POST_DEV(user_count <= MSG_BATCH_SIZE,
15a52f66
PP
1555 "Invalid returned message count: greater than "
1556 "batch size: count=%" PRIu64 ", batch-size=%u",
1557 user_count, MSG_BATCH_SIZE);
1558 break;
fb25b9e3
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:
15a52f66
PP
1563 goto end;
1564 default:
24847fc7 1565 bt_common_abort();
15a52f66
PP
1566 }
1567
15a52f66 1568 for (i = 0; i < user_count; i++) {
3d902f17 1569 if (got_first) {
1b3630b6 1570 g_queue_push_tail(iterator->auto_seek.msgs,
3d902f17
PP
1571 (void *) messages[i]);
1572 messages[i] = NULL;
15a52f66
PP
1573 continue;
1574 }
1575
3d902f17 1576 status = auto_seek_handle_message(iterator,
143feff5
SM
1577 ns_from_origin, messages[i], &got_first,
1578 stream_states);
fb25b9e3 1579 if (status == BT_FUNC_STATUS_OK) {
c980433a 1580 /* Message was either pushed or moved */
3d902f17
PP
1581 messages[i] = NULL;
1582 } else {
15a52f66
PP
1583 goto end;
1584 }
15a52f66
PP
1585 }
1586 }
1587
1588end:
1589 for (i = 0; i < user_count; i++) {
1590 if (messages[i]) {
864aa43f 1591 bt_object_put_ref_no_null_check(messages[i]);
15a52f66
PP
1592 }
1593 }
1594
fbd8a4e0 1595 set_msg_iterator_state(iterator, init_state);
15a52f66
PP
1596 return status;
1597}
1598
143feff5
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
15a52f66 1606static
68e2deed 1607enum bt_message_iterator_class_next_method_status post_auto_seek_next(
fbd8a4e0 1608 struct bt_message_iterator *iterator,
15a52f66
PP
1609 bt_message_array_const msgs, uint64_t capacity,
1610 uint64_t *count)
1611{
1b3630b6 1612 BT_ASSERT(!g_queue_is_empty(iterator->auto_seek.msgs));
3d902f17 1613 *count = 0;
15a52f66
PP
1614
1615 /*
1616 * Move auto-seek messages to the output array (which is this
3d902f17 1617 * iterator's base message array).
15a52f66 1618 */
1b3630b6
SM
1619 while (capacity > 0 && !g_queue_is_empty(iterator->auto_seek.msgs)) {
1620 msgs[*count] = g_queue_pop_head(iterator->auto_seek.msgs);
3d902f17
PP
1621 capacity--;
1622 (*count)++;
15a52f66 1623 }
15a52f66 1624
3d902f17
PP
1625 BT_ASSERT(*count > 0);
1626
1b3630b6 1627 if (g_queue_is_empty(iterator->auto_seek.msgs)) {
1d801aab
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;
15a52f66
PP
1632 }
1633
fb25b9e3 1634 return BT_FUNC_STATUS_OK;
15a52f66
PP
1635}
1636
143feff5
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
9e8e8b43
SM
1650static
1651bool message_iterator_can_seek_ns_from_origin(
fbd8a4e0 1652 struct bt_message_iterator *iterator,
9e8e8b43
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
fbd8a4e0 1658 status = bt_message_iterator_can_seek_ns_from_origin(
9e8e8b43
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}
143feff5 1666
fb25b9e3 1667enum bt_message_iterator_seek_ns_from_origin_status
fbd8a4e0
PP
1668bt_message_iterator_seek_ns_from_origin(
1669 struct bt_message_iterator *iterator,
15a52f66
PP
1670 int64_t ns_from_origin)
1671{
1672 int status;
143feff5 1673 GHashTable *stream_states = NULL;
c49bf79b 1674 bt_bool can_seek_by_itself;
15a52f66 1675
7c7324d3 1676 BT_ASSERT_PRE_NO_ERROR();
15a52f66
PP
1677 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
1678 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator);
1043fdea
PP
1679 BT_ASSERT_PRE(
1680 bt_component_borrow_graph(iterator->upstream_component)->config_state !=
1681 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
15a52f66
PP
1682 "Graph is not configured: %!+g",
1683 bt_component_borrow_graph(iterator->upstream_component));
c49bf79b 1684 /* The iterator must be able to seek ns from origin one way or another. */
15a52f66 1685 BT_ASSERT_PRE(
9e8e8b43 1686 message_iterator_can_seek_ns_from_origin(iterator, ns_from_origin),
15a52f66
PP
1687 "Message iterator cannot seek nanoseconds from origin: %!+i, "
1688 "ns-from-origin=%" PRId64, iterator, ns_from_origin);
fbd8a4e0
PP
1689 set_msg_iterator_state(iterator,
1690 BT_MESSAGE_ITERATOR_STATE_SEEKING);
15a52f66 1691
e253ad8d
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
c49bf79b
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) {
68e2deed 1700 bt_message_iterator_class_can_seek_ns_from_origin_method_status
c49bf79b
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) {
143feff5 1715 /* The iterator knows how to seek to a particular time, let it handle this. */
7f8e969d 1716 BT_ASSERT(iterator->methods.seek_ns_from_origin);
15a52f66
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",
fb25b9e3
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,
15a52f66 1727 "Unexpected status: %![iter-]+i, status=%s",
fb25b9e3 1728 iterator, bt_common_func_status_string(status));
d6f6a5aa 1729 BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status);
a8f90e5d
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 }
15a52f66 1736 } else {
143feff5 1737 /*
c49bf79b
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.
143feff5 1741 */
68e2deed 1742 enum bt_message_iterator_class_can_seek_beginning_method_status can_seek_status;
9e8e8b43
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);
15a52f66
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",
fb25b9e3
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,
15a52f66 1759 "Unexpected status: %![iter-]+i, status=%s",
fb25b9e3 1760 iterator, bt_common_func_status_string(status));
a8f90e5d
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
15a52f66 1768 switch (status) {
fb25b9e3 1769 case BT_FUNC_STATUS_OK:
15a52f66 1770 break;
fb25b9e3
PP
1771 case BT_FUNC_STATUS_ERROR:
1772 case BT_FUNC_STATUS_MEMORY_ERROR:
1773 case BT_FUNC_STATUS_AGAIN:
15a52f66
PP
1774 goto end;
1775 default:
24847fc7 1776 bt_common_abort();
15a52f66
PP
1777 }
1778
1779 /*
1780 * Find the first message which has a default clock
1781 * snapshot greater than or equal to the requested
3d902f17
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.
15a52f66 1785 */
1b3630b6 1786 while (!g_queue_is_empty(iterator->auto_seek.msgs)) {
864aa43f 1787 bt_object_put_ref_no_null_check(
1b3630b6 1788 g_queue_pop_tail(iterator->auto_seek.msgs));
3d902f17
PP
1789 }
1790
143feff5
SM
1791 stream_states = create_auto_seek_stream_states();
1792 if (!stream_states) {
a8f90e5d
PP
1793 BT_LIB_LOGE_APPEND_CAUSE(
1794 "Failed to allocate one GHashTable.");
fb25b9e3 1795 status = BT_FUNC_STATUS_MEMORY_ERROR;
143feff5
SM
1796 goto end;
1797 }
1798
15a52f66 1799 status = find_message_ge_ns_from_origin(iterator,
143feff5 1800 ns_from_origin, stream_states);
15a52f66 1801 switch (status) {
fb25b9e3
PP
1802 case BT_FUNC_STATUS_OK:
1803 case BT_FUNC_STATUS_END:
143feff5
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));
b7cbc799
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 }
143feff5
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);
b7cbc799
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);
143feff5
SM
1861 }
1862
143feff5 1863 if (!msg) {
fb25b9e3 1864 status = BT_FUNC_STATUS_MEMORY_ERROR;
143feff5
SM
1865 goto end;
1866 }
1867
143feff5
SM
1868 g_queue_push_head(iterator->auto_seek.msgs, msg);
1869 msg = NULL;
1870 /* fall-thru */
b7cbc799 1871
143feff5
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) {
fb25b9e3 1876 status = BT_FUNC_STATUS_MEMORY_ERROR;
143feff5
SM
1877 goto end;
1878 }
1879
b7cbc799
SM
1880 if (stream_state->seen_clock_snapshot) {
1881 bt_message_stream_beginning_set_default_clock_snapshot(msg, raw_value);
1882 }
1883
143feff5
SM
1884 g_queue_push_head(iterator->auto_seek.msgs, msg);
1885 msg = NULL;
1886 break;
1887 }
1888 }
1889
15a52f66 1890 /*
3d902f17
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.
15a52f66 1895 */
1b3630b6 1896 if (!g_queue_is_empty(iterator->auto_seek.msgs)) {
1d801aab
SM
1897 BT_ASSERT(!iterator->auto_seek.original_next_callback);
1898 iterator->auto_seek.original_next_callback = iterator->methods.next;
1899
3d902f17 1900 iterator->methods.next =
fbd8a4e0 1901 (bt_message_iterator_next_method)
3d902f17
PP
1902 post_auto_seek_next;
1903 }
1904
1905 /*
fb25b9e3
PP
1906 * `BT_FUNC_STATUS_END` becomes
1907 * `BT_FUNC_STATUS_OK`: the next
3d902f17
PP
1908 * time this iterator's "next" method is called,
1909 * it will return
fb25b9e3 1910 * `BT_FUNC_STATUS_END`.
3d902f17 1911 */
fb25b9e3 1912 status = BT_FUNC_STATUS_OK;
15a52f66 1913 break;
143feff5 1914 }
fb25b9e3
PP
1915 case BT_FUNC_STATUS_ERROR:
1916 case BT_FUNC_STATUS_MEMORY_ERROR:
1917 case BT_FUNC_STATUS_AGAIN:
15a52f66 1918 goto end;
15a52f66 1919 default:
24847fc7 1920 bt_common_abort();
15a52f66
PP
1921 }
1922 }
1923
e253ad8d
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
15a52f66 1930end:
143feff5
SM
1931 if (stream_states) {
1932 destroy_auto_seek_stream_states(stream_states);
1933 stream_states = NULL;
1934 }
a8f90e5d 1935
15a52f66 1936 set_iterator_state_after_seeking(iterator, status);
15a52f66
PP
1937 return status;
1938}
1939
d73bb381
PP
1940bt_bool bt_self_message_iterator_is_interrupted(
1941 const struct bt_self_message_iterator *self_msg_iter)
1942{
fbd8a4e0 1943 const struct bt_message_iterator *iterator =
d73bb381
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
fbd8a4e0
PP
1950void bt_message_iterator_get_ref(
1951 const struct bt_message_iterator *iterator)
8c6884d9
PP
1952{
1953 bt_object_get_ref(iterator);
1954}
1955
fbd8a4e0
PP
1956void bt_message_iterator_put_ref(
1957 const struct bt_message_iterator *iterator)
8c6884d9
PP
1958{
1959 bt_object_put_ref(iterator);
1960}
This page took 0.234252 seconds and 4 git commands to generate.