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