bt2: fix some whitespace issues
[babeltrace.git] / src / lib / graph / iterator.c
CommitLineData
47e5a032 1/*
e2f7325d 2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
47e5a032 3 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
47e5a032
JG
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
350ad6c1 24#define BT_LOG_TAG "LIB/MSG-ITER"
c2d9d9cf 25#include "lib/logging.h"
5af447e5 26
578e048b 27#include "compat/compiler.h"
e74dbb33 28#include "compat/glib.h"
578e048b
MJ
29#include "lib/trace-ir/clock-class.h"
30#include "lib/trace-ir/clock-snapshot.h"
3fadfbc0
MJ
31#include <babeltrace2/trace-ir/field.h>
32#include <babeltrace2/trace-ir/event-const.h>
578e048b 33#include "lib/trace-ir/event.h"
3fadfbc0 34#include <babeltrace2/trace-ir/packet-const.h>
578e048b
MJ
35#include "lib/trace-ir/packet.h"
36#include "lib/trace-ir/stream.h"
d24d5663
PP
37#include <babeltrace2/trace-ir/clock-class-const.h>
38#include <babeltrace2/trace-ir/stream-class-const.h>
39#include <babeltrace2/trace-ir/stream-const.h>
3fadfbc0 40#include <babeltrace2/graph/connection-const.h>
3fadfbc0 41#include <babeltrace2/graph/component-const.h>
3fadfbc0 42#include <babeltrace2/graph/component-sink-const.h>
3fadfbc0 43#include <babeltrace2/graph/message-const.h>
d24d5663 44#include <babeltrace2/graph/message-iterator.h>
3fadfbc0
MJ
45#include <babeltrace2/graph/self-component-port-input-message-iterator.h>
46#include <babeltrace2/graph/port-output-message-iterator.h>
3fadfbc0 47#include <babeltrace2/graph/message-event-const.h>
5b7b55be
SM
48#include <babeltrace2/graph/message-message-iterator-inactivity-const.h>
49#include <babeltrace2/graph/message-packet-beginning.h>
3fadfbc0
MJ
50#include <babeltrace2/graph/message-packet-beginning-const.h>
51#include <babeltrace2/graph/message-packet-end-const.h>
5b7b55be 52#include <babeltrace2/graph/message-stream-beginning.h>
3fadfbc0
MJ
53#include <babeltrace2/graph/message-stream-beginning-const.h>
54#include <babeltrace2/graph/message-stream-end-const.h>
3fadfbc0
MJ
55#include <babeltrace2/graph/port-const.h>
56#include <babeltrace2/graph/graph.h>
57#include <babeltrace2/graph/graph-const.h>
3fadfbc0 58#include <babeltrace2/types.h>
578e048b
MJ
59#include "common/assert.h"
60#include "lib/assert-pre.h"
f6f301d7 61#include "lib/assert-post.h"
fa054faf 62#include <stdint.h>
2ec84d26 63#include <inttypes.h>
0fbb9a9f 64#include <stdlib.h>
3230ee6b 65
578e048b
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"
d24d5663 80#include "lib/func-status.h"
578e048b 81
d4393e08
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 */
d6e69534 86#define MSG_BATCH_SIZE 15
d4393e08 87
7474e7d3
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
d0fea130 95static inline
003e713f 96void set_self_comp_port_input_msg_iterator_state(
d0fea130
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);
7474e7d3 101 BT_LIB_LOGD("Updating message iterator's state: new-state=%s",
d0fea130
PP
102 bt_self_component_port_input_message_iterator_state_string(state));
103 iterator->state = state;
104}
105
8ed535b5 106static
d6e69534 107void destroy_base_message_iterator(struct bt_object *obj)
8ed535b5 108{
d6e69534 109 struct bt_message_iterator *iterator = (void *) obj;
d4393e08
PP
110
111 BT_ASSERT(iterator);
112
d6e69534
PP
113 if (iterator->msgs) {
114 g_ptr_array_free(iterator->msgs, TRUE);
115 iterator->msgs = NULL;
d4393e08
PP
116 }
117
118 g_free(iterator);
8ed535b5
PP
119}
120
47e5a032 121static
d6e69534 122void bt_self_component_port_input_message_iterator_destroy(struct bt_object *obj)
47e5a032 123{
d6e69534 124 struct bt_self_component_port_input_message_iterator *iterator;
8738a040 125
f6ccaed9 126 BT_ASSERT(obj);
d3eb6e8f 127
bd14d768 128 /*
d6e69534 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
d6e69534 132 * message iterator's finalization function does
d94d92ac
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 */
3fea54f6 139 obj->ref_count++;
07245ac2 140 iterator = (void *) obj;
3f7d4d90 141 BT_LIB_LOGI("Destroying self component input port message iterator object: "
d94d92ac 142 "%!+i", iterator);
d0fea130 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);
d94d92ac 152 iterator->connection = NULL;
bd14d768
PP
153 }
154
da9c4c52
SM
155 if (iterator->auto_seek.msgs) {
156 while (!g_queue_is_empty(iterator->auto_seek.msgs)) {
5b9e151d 157 bt_object_put_no_null_check(
da9c4c52 158 g_queue_pop_tail(iterator->auto_seek.msgs));
7474e7d3
PP
159 }
160
da9c4c52
SM
161 g_queue_free(iterator->auto_seek.msgs);
162 iterator->auto_seek.msgs = NULL;
7474e7d3
PP
163 }
164
d6e69534 165 destroy_base_message_iterator(obj);
47e5a032
JG
166}
167
bd14d768 168BT_HIDDEN
d0fea130 169void bt_self_component_port_input_message_iterator_try_finalize(
d6e69534 170 struct bt_self_component_port_input_message_iterator *iterator)
bd14d768 171{
d94d92ac
PP
172 typedef void (*method_t)(void *);
173
bd14d768 174 struct bt_component_class *comp_class = NULL;
d94d92ac 175 method_t method = NULL;
bd14d768 176
f6ccaed9 177 BT_ASSERT(iterator);
bd14d768
PP
178
179 switch (iterator->state) {
d6e69534 180 case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED:
088d4023 181 /* Skip user finalization if user initialization failed */
d6e69534 182 BT_LIB_LOGD("Not finalizing non-initialized message iterator: "
d94d92ac 183 "%!+i", iterator);
d0fea130 184 goto end;
d6e69534 185 case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED:
bd14d768 186 /* Already finalized */
d6e69534 187 BT_LIB_LOGD("Not finalizing message iterator: already finalized: "
d94d92ac 188 "%!+i", iterator);
d0fea130
PP
189 goto end;
190 case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZING:
870631a2 191 /* Finalizing */
d0fea130
PP
192 BT_LIB_LOGF("Message iterator is already being finalized: "
193 "%!+i", iterator);
194 abort();
bd14d768
PP
195 default:
196 break;
197 }
198
d6e69534 199 BT_LIB_LOGD("Finalizing message iterator: %!+i", iterator);
d0fea130
PP
200 set_self_comp_port_input_msg_iterator_state(iterator,
201 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZING);
f6ccaed9 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 {
d94d92ac
PP
209 struct bt_component_class_source *src_comp_cls =
210 (void *) comp_class;
bd14d768 211
d6e69534 212 method = (method_t) src_comp_cls->methods.msg_iter_finalize;
bd14d768
PP
213 break;
214 }
215 case BT_COMPONENT_CLASS_TYPE_FILTER:
216 {
d94d92ac
PP
217 struct bt_component_class_filter *flt_comp_cls =
218 (void *) comp_class;
bd14d768 219
d6e69534 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
d94d92ac
PP
228 if (method) {
229 BT_LIB_LOGD("Calling user's finalization method: %!+i",
5af447e5 230 iterator);
d94d92ac 231 method(iterator);
bd14d768
PP
232 }
233
bd14d768
PP
234 iterator->upstream_component = NULL;
235 iterator->upstream_port = NULL;
d0fea130
PP
236 set_self_comp_port_input_msg_iterator_state(iterator,
237 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED);
d6e69534 238 BT_LIB_LOGD("Finalized message iterator: %!+i", iterator);
d0fea130
PP
239
240end:
241 return;
bd14d768
PP
242}
243
244BT_HIDDEN
d6e69534
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{
f6ccaed9 249 BT_ASSERT(iterator);
bd14d768 250 iterator->connection = connection;
3f7d4d90 251 BT_LIB_LOGI("Set message iterator's connection: "
d94d92ac 252 "%![iter-]+i, %![conn-]+x", iterator, connection);
bd14d768
PP
253}
254
90157d89 255static
d6e69534
PP
256int init_message_iterator(struct bt_message_iterator *iterator,
257 enum bt_message_iterator_type type,
90157d89
PP
258 bt_object_release_func destroy)
259{
d4393e08
PP
260 int ret = 0;
261
3fea54f6 262 bt_object_init_shared(&iterator->base, destroy);
90157d89 263 iterator->type = type;
d6e69534
PP
264 iterator->msgs = g_ptr_array_new();
265 if (!iterator->msgs) {
870631a2 266 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
d4393e08
PP
267 ret = -1;
268 goto end;
269 }
270
d6e69534 271 g_ptr_array_set_size(iterator->msgs, MSG_BATCH_SIZE);
d4393e08
PP
272
273end:
274 return ret;
90157d89
PP
275}
276
7474e7d3
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
d94d92ac 292static
d6e69534
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,
d94d92ac 296 struct bt_port *upstream_port)
47e5a032 297{
d4393e08 298 int ret;
d6e69534 299 struct bt_self_component_port_input_message_iterator *iterator = NULL;
47e5a032 300
f6ccaed9
PP
301 BT_ASSERT(upstream_comp);
302 BT_ASSERT(upstream_port);
f6ccaed9 303 BT_ASSERT(bt_port_is_connected(upstream_port));
3f7d4d90 304 BT_LIB_LOGI("Creating initial message iterator on self component input port: "
d94d92ac
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(
d6e69534 311 struct bt_self_component_port_input_message_iterator, 1);
47e5a032 312 if (!iterator) {
870631a2
PP
313 BT_LIB_LOGE_APPEND_CAUSE(
314 "Failed to allocate one self component input port "
d6e69534 315 "message iterator.");
870631a2 316 goto error;
47e5a032
JG
317 }
318
d6e69534
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);
d4393e08 322 if (ret) {
d6e69534 323 /* init_message_iterator() logs errors */
870631a2 324 goto error;
d4393e08 325 }
3230ee6b 326
54b135a0
SM
327 iterator->last_ns_from_origin = INT64_MIN;
328
da9c4c52
SM
329 iterator->auto_seek.msgs = g_queue_new();
330 if (!iterator->auto_seek.msgs) {
870631a2 331 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GQueue.");
7474e7d3 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;
d94d92ac 338 iterator->connection = iterator->upstream_port->connection;
5c563278 339 iterator->graph = bt_component_borrow_graph(upstream_comp);
d0fea130
PP
340 set_self_comp_port_input_msg_iterator_state(iterator,
341 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED);
7474e7d3
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
3f7d4d90 406 BT_LIB_LOGI("Created initial message iterator on self component input port: "
d94d92ac
PP
407 "%![up-port-]+p, %![up-comp-]+c, %![iter-]+i",
408 upstream_port, upstream_comp, iterator);
870631a2
PP
409 goto end;
410
411error:
412 BT_OBJECT_PUT_REF_AND_RESET(iterator);
3230ee6b 413
47e5a032 414end:
d94d92ac 415 return iterator;
47e5a032
JG
416}
417
d6e69534
PP
418struct bt_self_component_port_input_message_iterator *
419bt_self_component_port_input_message_iterator_create(
d94d92ac 420 struct bt_self_component_port_input *self_port)
ea8d3e58 421{
d24d5663 422 typedef enum bt_component_class_message_iterator_init_method_status (*init_method_t)(
d94d92ac
PP
423 void *, void *, void *);
424
425 init_method_t init_method = NULL;
d6e69534 426 struct bt_self_component_port_input_message_iterator *iterator =
d94d92ac
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");
0d72b8c3 435 comp = bt_port_borrow_component_inline(port);
d94d92ac
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);
0d72b8c3 446 upstream_comp = bt_port_borrow_component_inline(upstream_port);
d94d92ac 447 BT_ASSERT(upstream_comp);
5badd463
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));
d94d92ac
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);
d6e69534 458 iterator = bt_self_component_port_input_message_iterator_create_initial(
d94d92ac
PP
459 upstream_comp, upstream_port);
460 if (!iterator) {
870631a2
PP
461 BT_LIB_LOGE_APPEND_CAUSE(
462 "Cannot create self component input port message iterator.");
463 goto error;
d94d92ac 464 }
890882ef 465
d94d92ac
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 =
d6e69534 473 (init_method_t) src_comp_cls->methods.msg_iter_init;
d94d92ac
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 =
d6e69534 482 (init_method_t) flt_comp_cls->methods.msg_iter_init;
d94d92ac
PP
483 break;
484 }
485 default:
486 /* Unreachable */
487 abort();
488 }
489
490 if (init_method) {
d24d5663 491 enum bt_component_class_message_iterator_init_method_status iter_status;
d94d92ac
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",
d24d5663
PP
497 bt_common_func_status_string(iter_status));
498 if (iter_status != BT_FUNC_STATUS_OK) {
870631a2
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;
d94d92ac
PP
505 }
506 }
507
d0fea130
PP
508 set_self_comp_port_input_msg_iterator_state(iterator,
509 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE);
d94d92ac 510 g_ptr_array_add(port->connection->iterators, iterator);
3f7d4d90 511 BT_LIB_LOGI("Created message iterator on self component input port: "
d94d92ac
PP
512 "%![up-port-]+p, %![up-comp-]+c, %![iter-]+i",
513 upstream_port, upstream_comp, iterator);
870631a2
PP
514 goto end;
515
516error:
517 BT_OBJECT_PUT_REF_AND_RESET(iterator);
d94d92ac
PP
518
519end:
520 return iterator;
ea8d3e58
JG
521}
522
d6e69534
PP
523void *bt_self_message_iterator_get_data(
524 const struct bt_self_message_iterator *self_iterator)
ea8d3e58 525{
d6e69534 526 struct bt_self_component_port_input_message_iterator *iterator =
d94d92ac 527 (void *) self_iterator;
ea8d3e58 528
bdb288b3 529 BT_ASSERT_PRE_DEV_NON_NULL(iterator, "Message iterator");
d94d92ac 530 return iterator->user_data;
8738a040 531}
413bc2c4 532
d6e69534
PP
533void bt_self_message_iterator_set_data(
534 struct bt_self_message_iterator *self_iterator, void *data)
5c563278 535{
d6e69534 536 struct bt_self_component_port_input_message_iterator *iterator =
d94d92ac 537 (void *) self_iterator;
5c563278 538
bdb288b3 539 BT_ASSERT_PRE_DEV_NON_NULL(iterator, "Message iterator");
d94d92ac 540 iterator->user_data = data;
3f7d4d90 541 BT_LIB_LOGD("Set message iterator's user data: "
d94d92ac 542 "%!+i, user-data-addr=%p", iterator, data);
5c563278
PP
543}
544
54b135a0
SM
545/*
546 * Validate that the default clock snapshot in `msg` doesn't make us go back in
547 * time.
548 */
549
bdb288b3 550BT_ASSERT_POST_DEV_FUNC
54b135a0
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;
d24d5663 559 enum bt_clock_snapshot_get_ns_from_origin_status clock_snapshot_status;
54b135a0
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 }
188edac1
SM
588 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
589 case BT_MESSAGE_TYPE_STREAM_END:
54b135a0 590 {
188edac1
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;
54b135a0 594 }
188edac1
SM
595
596 clock_snapshot = stream_msg->default_cs;
54b135a0
SM
597 break;
598 }
54b135a0
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);
d24d5663 615 if (clock_snapshot_status != BT_FUNC_STATUS_OK) {
54b135a0
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
bdb288b3 625BT_ASSERT_POST_DEV_FUNC
54b135a0
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
bdb288b3 652BT_ASSERT_POST_DEV_FUNC
54b135a0
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;
6162e6b7 684 bt_uuid_copy(iterator->clock_expectation.uuid, clock_class_uuid);
54b135a0
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) {
bdb288b3
PP
692 BT_ASSERT_POST_DEV_MSG(
693 "Expecting no clock class, got one: %![cc-]+K",
54b135a0
SM
694 clock_class);
695 result = false;
696 goto end;
697 }
698
699 break;
700
701 case CLOCK_EXPECTATION_ORIGIN_UNIX:
702 if (!clock_class) {
bdb288b3
PP
703 BT_ASSERT_POST_DEV_MSG(
704 "Expecting a clock class, got none.");
54b135a0
SM
705 result = false;
706 goto end;
707 }
708
709 if (!bt_clock_class_origin_is_unix_epoch(clock_class)) {
bdb288b3
PP
710 BT_ASSERT_POST_DEV_MSG(
711 "Expecting a clock class with Unix epoch origin: %![cc-]+K",
54b135a0
SM
712 clock_class);
713 result = false;
714 goto end;
715 }
716 break;
717
718 case CLOCK_EXPECTATION_ORIGIN_OTHER_UUID:
719 if (!clock_class) {
bdb288b3
PP
720 BT_ASSERT_POST_DEV_MSG(
721 "Expecting a clock class, got none.");
54b135a0
SM
722 result = false;
723 goto end;
724 }
725
726 if (bt_clock_class_origin_is_unix_epoch(clock_class)) {
bdb288b3
PP
727 BT_ASSERT_POST_DEV_MSG(
728 "Expecting a clock class without Unix epoch origin: %![cc-]+K",
54b135a0
SM
729 clock_class);
730 result = false;
731 goto end;
732 }
733
734 if (!clock_class_uuid) {
bdb288b3
PP
735 BT_ASSERT_POST_DEV_MSG(
736 "Expecting a clock class with UUID: %![cc-]+K",
54b135a0
SM
737 clock_class);
738 result = false;
739 goto end;
740 }
741
742 if (bt_uuid_compare(iterator->clock_expectation.uuid, clock_class_uuid)) {
bdb288b3
PP
743 BT_ASSERT_POST_DEV_MSG(
744 "Expecting a clock class with UUID, got one "
54b135a0
SM
745 "with a different UUID: %![cc-]+K, expected-uuid=%!u",
746 clock_class, iterator->clock_expectation.uuid);
747 result = false;
748 goto end;
749 }
750 break;
751
752 case CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID:
753 if (!clock_class) {
bdb288b3
PP
754 BT_ASSERT_POST_DEV_MSG(
755 "Expecting a clock class, got none.");
54b135a0
SM
756 result = false;
757 goto end;
758 }
759
760 if (bt_clock_class_origin_is_unix_epoch(clock_class)) {
bdb288b3
PP
761 BT_ASSERT_POST_DEV_MSG(
762 "Expecting a clock class without Unix epoch origin: %![cc-]+K",
54b135a0
SM
763 clock_class);
764 result = false;
765 goto end;
766 }
767
768 if (clock_class_uuid) {
bdb288b3
PP
769 BT_ASSERT_POST_DEV_MSG(
770 "Expecting a clock class without UUID: %![cc-]+K",
54b135a0
SM
771 clock_class);
772 result = false;
773 goto end;
774 }
775 break;
776 }
777 }
778
779 result = true;
780
781end:
782 return result;
783}
784
bdb288b3 785BT_ASSERT_POST_DEV_FUNC
54b135a0
SM
786static
787bool clock_classes_are_compatible(
788 struct bt_self_component_port_input_message_iterator *iterator,
789 bt_message_array_const msgs, uint64_t msg_count)
790{
791 uint64_t i;
792 bool result;
793
794 for (i = 0; i < msg_count; i++) {
795 if (!clock_classes_are_compatible_one(iterator, msgs[i])) {
796 result = false;
797 goto end;
798 }
799 }
800
801 result = true;
802
803end:
804 return result;
805}
806
807/*
808 * Call the `next` method of the iterator. Do some validation on the returned
809 * messages.
810 */
811
812static
d24d5663
PP
813enum bt_component_class_message_iterator_next_method_status
814call_iterator_next_method(
54b135a0
SM
815 struct bt_self_component_port_input_message_iterator *iterator,
816 bt_message_array_const msgs, uint64_t capacity, uint64_t *user_count)
817{
d24d5663 818 enum bt_component_class_message_iterator_next_method_status status;
54b135a0
SM
819
820 BT_ASSERT(iterator->methods.next);
821 BT_LOGD_STR("Calling user's \"next\" method.");
54b135a0 822 status = iterator->methods.next(iterator, msgs, capacity, user_count);
f6f301d7 823 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64,
d24d5663 824 bt_common_func_status_string(status), *user_count);
54b135a0 825
d24d5663 826 if (status == BT_FUNC_STATUS_OK) {
bdb288b3 827 BT_ASSERT_POST_DEV(clock_classes_are_compatible(iterator, msgs, *user_count),
54b135a0 828 "Clocks are not compatible");
bdb288b3 829 BT_ASSERT_POST_DEV(clock_snapshots_are_monotonic(iterator, msgs, *user_count),
54b135a0
SM
830 "Clock snapshots are not monotonic");
831 }
832
833 return status;
834}
835
d24d5663 836enum bt_message_iterator_next_status
d6e69534
PP
837bt_self_component_port_input_message_iterator_next(
838 struct bt_self_component_port_input_message_iterator *iterator,
839 bt_message_array_const *msgs, uint64_t *user_count)
3230ee6b 840{
d24d5663 841 enum bt_message_iterator_next_status status = BT_FUNC_STATUS_OK;
d94d92ac 842
bdb288b3
PP
843 BT_ASSERT_PRE_DEV_NON_NULL(iterator, "Message iterator");
844 BT_ASSERT_PRE_DEV_NON_NULL(msgs, "Message array (output)");
845 BT_ASSERT_PRE_DEV_NON_NULL(user_count, "Message count (output)");
846 BT_ASSERT_PRE_DEV(iterator->state ==
d6e69534
PP
847 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE,
848 "Message iterator's \"next\" called, but "
7474e7d3 849 "message iterator is in the wrong state: %!+i", iterator);
f42867e2
PP
850 BT_ASSERT(iterator->upstream_component);
851 BT_ASSERT(iterator->upstream_component->class);
bdb288b3 852 BT_ASSERT_PRE_DEV(
5badd463
PP
853 bt_component_borrow_graph(iterator->upstream_component)->config_state !=
854 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
4725a201
PP
855 "Graph is not configured: %!+g",
856 bt_component_borrow_graph(iterator->upstream_component));
d94d92ac 857 BT_LIB_LOGD("Getting next self component input port "
3f7d4d90
PP
858 "message iterator's messages: %!+i, batch-size=%u",
859 iterator, MSG_BATCH_SIZE);
d3eb6e8f 860
3230ee6b 861 /*
d6e69534 862 * Call the user's "next" method to get the next messages
fa054faf 863 * and status.
3230ee6b 864 */
3f7d4d90 865 *user_count = 0;
d24d5663 866 status = (int) call_iterator_next_method(iterator,
7474e7d3
PP
867 (void *) iterator->base.msgs->pdata, MSG_BATCH_SIZE,
868 user_count);
870631a2
PP
869 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64,
870 bt_common_func_status_string(status), *user_count);
d4393e08 871 if (status < 0) {
870631a2
PP
872 BT_LIB_LOGW_APPEND_CAUSE(
873 "Component input port message iterator's \"next\" method failed: "
874 "%![iter-]+i, status=%s",
875 iterator, bt_common_func_status_string(status));
f42867e2
PP
876 goto end;
877 }
3230ee6b 878
d0fea130
PP
879 /*
880 * There is no way that this iterator could have been finalized
881 * during its "next" method, as the only way to do this is to
882 * put the last iterator's reference, and this can only be done
883 * by its downstream owner.
7474e7d3
PP
884 *
885 * For the same reason, there is no way that this iterator could
886 * have seeked (cannot seek a self message iterator).
d0fea130
PP
887 */
888 BT_ASSERT(iterator->state ==
889 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE);
8cf27cc5 890
d4393e08 891 switch (status) {
d24d5663 892 case BT_FUNC_STATUS_OK:
bdb288b3 893 BT_ASSERT_POST_DEV(*user_count <= MSG_BATCH_SIZE,
7474e7d3
PP
894 "Invalid returned message count: greater than "
895 "batch size: count=%" PRIu64 ", batch-size=%u",
896 *user_count, MSG_BATCH_SIZE);
d6e69534 897 *msgs = (void *) iterator->base.msgs->pdata;
d4393e08 898 break;
d24d5663 899 case BT_FUNC_STATUS_AGAIN:
d4393e08 900 goto end;
d24d5663 901 case BT_FUNC_STATUS_END:
d0fea130
PP
902 set_self_comp_port_input_msg_iterator_state(iterator,
903 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED);
f42867e2 904 goto end;
f42867e2
PP
905 default:
906 /* Unknown non-error status */
907 abort();
41a2b7ae
PP
908 }
909
910end:
3230ee6b
PP
911 return status;
912}
913
d24d5663 914enum bt_message_iterator_next_status bt_port_output_message_iterator_next(
d6e69534
PP
915 struct bt_port_output_message_iterator *iterator,
916 bt_message_array_const *msgs_to_user,
d4393e08 917 uint64_t *count_to_user)
3230ee6b 918{
d24d5663
PP
919 enum bt_message_iterator_next_status status;
920 int graph_status;
3230ee6b 921
bdb288b3
PP
922 BT_ASSERT_PRE_DEV_NON_NULL(iterator, "Message iterator");
923 BT_ASSERT_PRE_DEV_NON_NULL(msgs_to_user, "Message array (output)");
924 BT_ASSERT_PRE_DEV_NON_NULL(count_to_user, "Message count (output)");
d6e69534 925 BT_LIB_LOGD("Getting next output port message iterator's messages: "
07245ac2 926 "%!+i", iterator);
d94d92ac
PP
927 graph_status = bt_graph_consume_sink_no_check(iterator->graph,
928 iterator->colander);
07245ac2 929 switch (graph_status) {
d24d5663
PP
930 case BT_FUNC_STATUS_CANCELED:
931 case BT_FUNC_STATUS_AGAIN:
932 case BT_FUNC_STATUS_END:
933 case BT_FUNC_STATUS_MEMORY_ERROR:
d94d92ac 934 status = (int) graph_status;
07245ac2 935 break;
d24d5663
PP
936 case BT_FUNC_STATUS_OK:
937 status = BT_FUNC_STATUS_OK;
d4393e08
PP
938
939 /*
d6e69534 940 * On success, the colander sink moves the messages
d4393e08 941 * to this iterator's array and sets this iterator's
d6e69534 942 * message count: move them to the user.
d4393e08 943 */
d6e69534 944 *msgs_to_user = (void *) iterator->base.msgs->pdata;
d94d92ac 945 *count_to_user = iterator->count;
90157d89 946 break;
90157d89 947 default:
07245ac2 948 /* Other errors */
d24d5663 949 status = BT_FUNC_STATUS_ERROR;
90157d89 950 }
3230ee6b 951
3230ee6b 952 return status;
53d45b87
JG
953}
954
7474e7d3
PP
955struct bt_component *
956bt_self_component_port_input_message_iterator_borrow_component(
d6e69534 957 struct bt_self_component_port_input_message_iterator *iterator)
d94d92ac 958{
bdb288b3 959 BT_ASSERT_PRE_DEV_NON_NULL(iterator, "Message iterator");
d94d92ac
PP
960 return iterator->upstream_component;
961}
962
7474e7d3
PP
963const struct bt_component *
964bt_self_component_port_input_message_iterator_borrow_component_const(
965 const struct bt_self_component_port_input_message_iterator *iterator)
966{
bdb288b3 967 BT_ASSERT_PRE_DEV_NON_NULL(iterator, "Message iterator");
7474e7d3
PP
968 return iterator->upstream_component;
969}
970
d6e69534
PP
971struct bt_self_component *bt_self_message_iterator_borrow_component(
972 struct bt_self_message_iterator *self_iterator)
413bc2c4 973{
d6e69534 974 struct bt_self_component_port_input_message_iterator *iterator =
d94d92ac 975 (void *) self_iterator;
90157d89 976
bdb288b3 977 BT_ASSERT_PRE_DEV_NON_NULL(iterator, "Message iterator");
d94d92ac 978 return (void *) iterator->upstream_component;
413bc2c4
JG
979}
980
d6e69534
PP
981struct bt_self_port_output *bt_self_message_iterator_borrow_port(
982 struct bt_self_message_iterator *self_iterator)
91457551 983{
d6e69534 984 struct bt_self_component_port_input_message_iterator *iterator =
d94d92ac
PP
985 (void *) self_iterator;
986
bdb288b3 987 BT_ASSERT_PRE_DEV_NON_NULL(iterator, "Message iterator");
d94d92ac 988 return (void *) iterator->upstream_port;
91457551 989}
8ed535b5
PP
990
991static
d6e69534 992void bt_port_output_message_iterator_destroy(struct bt_object *obj)
8ed535b5 993{
d6e69534 994 struct bt_port_output_message_iterator *iterator = (void *) obj;
8ed535b5 995
3f7d4d90 996 BT_LIB_LOGI("Destroying output port message iterator object: %!+i",
8ed535b5
PP
997 iterator);
998 BT_LOGD_STR("Putting graph.");
d94d92ac 999 BT_OBJECT_PUT_REF_AND_RESET(iterator->graph);
8ed535b5 1000 BT_LOGD_STR("Putting colander sink component.");
d94d92ac 1001 BT_OBJECT_PUT_REF_AND_RESET(iterator->colander);
d6e69534 1002 destroy_base_message_iterator(obj);
8ed535b5
PP
1003}
1004
d6e69534 1005struct bt_port_output_message_iterator *
7474e7d3 1006bt_port_output_message_iterator_create(struct bt_graph *graph,
0d72b8c3 1007 const struct bt_port_output *output_port)
8ed535b5 1008{
d6e69534 1009 struct bt_port_output_message_iterator *iterator = NULL;
d94d92ac 1010 struct bt_component_class_sink *colander_comp_cls = NULL;
8ed535b5 1011 struct bt_component *output_port_comp = NULL;
d94d92ac 1012 struct bt_component_sink *colander_comp;
d24d5663 1013 int graph_status;
d94d92ac 1014 struct bt_port_input *colander_in_port = NULL;
8ed535b5 1015 struct bt_component_class_sink_colander_data colander_data;
d4393e08 1016 int ret;
8ed535b5 1017
d94d92ac 1018 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
f42867e2 1019 BT_ASSERT_PRE_NON_NULL(output_port, "Output port");
0d72b8c3
PP
1020 output_port_comp = bt_port_borrow_component_inline(
1021 (const void *) output_port);
f42867e2
PP
1022 BT_ASSERT_PRE(output_port_comp,
1023 "Output port has no component: %!+p", output_port);
d94d92ac
PP
1024 BT_ASSERT_PRE(bt_component_borrow_graph(output_port_comp) ==
1025 (void *) graph,
1026 "Output port is not part of graph: %![graph-]+g, %![port-]+p",
1027 graph, output_port);
2bcba397
PP
1028 BT_ASSERT_PRE(!graph->has_sink,
1029 "Graph already has a sink component: %![graph-]+g");
8ed535b5 1030
d6e69534 1031 /* Create message iterator */
3f7d4d90 1032 BT_LIB_LOGI("Creating message iterator on output port: "
d94d92ac 1033 "%![port-]+p, %![comp-]+c", output_port, output_port_comp);
d6e69534 1034 iterator = g_new0(struct bt_port_output_message_iterator, 1);
8ed535b5 1035 if (!iterator) {
870631a2
PP
1036 BT_LIB_LOGE_APPEND_CAUSE(
1037 "Failed to allocate one output port message iterator.");
8ed535b5
PP
1038 goto error;
1039 }
1040
d6e69534
PP
1041 ret = init_message_iterator((void *) iterator,
1042 BT_MESSAGE_ITERATOR_TYPE_PORT_OUTPUT,
1043 bt_port_output_message_iterator_destroy);
d4393e08 1044 if (ret) {
d6e69534 1045 /* init_message_iterator() logs errors */
65300d60 1046 BT_OBJECT_PUT_REF_AND_RESET(iterator);
d4393e08
PP
1047 goto end;
1048 }
8ed535b5
PP
1049
1050 /* Create colander component */
1051 colander_comp_cls = bt_component_class_sink_colander_get();
1052 if (!colander_comp_cls) {
870631a2
PP
1053 /* bt_component_class_sink_colander_get() logs errors */
1054 BT_LIB_LOGE_APPEND_CAUSE(
1055 "Cannot get colander sink component class.");
8ed535b5
PP
1056 goto error;
1057 }
1058
398454ed
PP
1059 iterator->graph = graph;
1060 bt_object_get_no_null_check(iterator->graph);
d6e69534 1061 colander_data.msgs = (void *) iterator->base.msgs->pdata;
d4393e08 1062 colander_data.count_addr = &iterator->count;
5fd91d88 1063
e874da19
PP
1064 /*
1065 * Hope that nobody uses this very unique name.
1066 *
1067 * We pass `BT_LOGGING_LEVEL_NONE` but the colander component
1068 * class module does not use this level anyway since it belongs
1069 * to the library.
1070 */
d24d5663 1071 graph_status = bt_graph_add_sink_component_with_init_method_data(
5fd91d88
PP
1072 (void *) graph, colander_comp_cls,
1073 "colander-36ac3409-b1a8-4d60-ab1f-4fdf341a8fb1",
e874da19
PP
1074 NULL, &colander_data, BT_LOGGING_LEVEL_NONE,
1075 (void *) &iterator->colander);
d24d5663 1076 if (graph_status != BT_FUNC_STATUS_OK) {
870631a2
PP
1077 BT_LIB_LOGE_APPEND_CAUSE(
1078 "Cannot add colander sink component to graph: "
1079 "%![graph-]+g, status=%s", graph,
d24d5663 1080 bt_common_func_status_string(graph_status));
8ed535b5
PP
1081 goto error;
1082 }
1083
1084 /*
1085 * Connect provided output port to the colander component's
1086 * input port.
1087 */
0d72b8c3
PP
1088 colander_in_port =
1089 (void *) bt_component_sink_borrow_input_port_by_index_const(
1090 (void *) iterator->colander, 0);
f6ccaed9 1091 BT_ASSERT(colander_in_port);
0d72b8c3 1092 graph_status = bt_graph_connect_ports(graph,
8ed535b5 1093 output_port, colander_in_port, NULL);
d24d5663 1094 if (graph_status != BT_FUNC_STATUS_OK) {
870631a2
PP
1095 BT_LIB_LOGW_APPEND_CAUSE(
1096 "Cannot connect colander sink's port: "
d94d92ac
PP
1097 "%![graph-]+g, %![comp-]+c, status=%s", graph,
1098 iterator->colander,
d24d5663 1099 bt_common_func_status_string(graph_status));
8ed535b5
PP
1100 goto error;
1101 }
1102
1103 /*
1104 * At this point everything went fine. Make the graph
d6e69534 1105 * nonconsumable forever so that only this message iterator
8ed535b5 1106 * can consume (thanks to bt_graph_consume_sink_no_check()).
d6e69534
PP
1107 * This avoids leaking the message created by the colander
1108 * sink and moved to the message iterator's message
07245ac2 1109 * member.
8ed535b5 1110 */
d94d92ac 1111 bt_graph_set_can_consume(iterator->graph, false);
5badd463 1112
36d1acad
SM
1113 /* Also set the graph as being configured. */
1114 graph_status = bt_graph_configure(graph);
d24d5663 1115 if (graph_status != BT_FUNC_STATUS_OK) {
870631a2
PP
1116 BT_LIB_LOGW_APPEND_CAUSE(
1117 "Cannot configure graph after having "
1118 "added and connected colander sink: "
1119 "%![graph-]+g, %![comp-]+c, status=%s", graph,
1120 iterator->colander,
d24d5663 1121 bt_common_func_status_string(graph_status));
36d1acad
SM
1122 goto error;
1123 }
8ed535b5
PP
1124 goto end;
1125
1126error:
1127 if (iterator && iterator->graph && iterator->colander) {
1128 int ret;
1129
1130 /* Remove created colander component from graph if any */
1131 colander_comp = iterator->colander;
65300d60 1132 BT_OBJECT_PUT_REF_AND_RESET(iterator->colander);
8ed535b5
PP
1133
1134 /*
1135 * At this point the colander component's reference
1136 * count is 0 because iterator->colander was the only
1137 * owner. We also know that it is not connected because
1138 * this is the last operation before this function
1139 * succeeds.
1140 *
1141 * Since we honor the preconditions here,
1142 * bt_graph_remove_unconnected_component() always
1143 * succeeds.
1144 */
1145 ret = bt_graph_remove_unconnected_component(iterator->graph,
d94d92ac 1146 (void *) colander_comp);
f6ccaed9 1147 BT_ASSERT(ret == 0);
8ed535b5
PP
1148 }
1149
65300d60 1150 BT_OBJECT_PUT_REF_AND_RESET(iterator);
8ed535b5
PP
1151
1152end:
65300d60 1153 bt_object_put_ref(colander_comp_cls);
8ed535b5
PP
1154 return (void *) iterator;
1155}
c5b9b441 1156
7474e7d3
PP
1157bt_bool bt_self_component_port_input_message_iterator_can_seek_ns_from_origin(
1158 struct bt_self_component_port_input_message_iterator *iterator,
1159 int64_t ns_from_origin)
1160{
1161 bt_bool can = BT_FALSE;
1162
1163 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
1164 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator);
5badd463
PP
1165 BT_ASSERT_PRE(
1166 bt_component_borrow_graph(iterator->upstream_component)->config_state !=
1167 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
7474e7d3
PP
1168 "Graph is not configured: %!+g",
1169 bt_component_borrow_graph(iterator->upstream_component));
1170
1171 if (iterator->methods.can_seek_ns_from_origin) {
1172 can = iterator->methods.can_seek_ns_from_origin(iterator,
1173 ns_from_origin);
1174 goto end;
1175 }
1176
1177 /*
1178 * Automatic seeking fall back: if we can seek to the beginning,
1179 * then we can automatically seek to any message.
1180 */
1181 if (iterator->methods.can_seek_beginning) {
1182 can = iterator->methods.can_seek_beginning(iterator);
1183 }
1184
1185end:
1186 return can;
1187}
1188
1189bt_bool bt_self_component_port_input_message_iterator_can_seek_beginning(
1190 struct bt_self_component_port_input_message_iterator *iterator)
1191{
1192 bt_bool can = BT_FALSE;
1193
1194 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
1195 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator);
5badd463
PP
1196 BT_ASSERT_PRE(
1197 bt_component_borrow_graph(iterator->upstream_component)->config_state !=
1198 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
7474e7d3
PP
1199 "Graph is not configured: %!+g",
1200 bt_component_borrow_graph(iterator->upstream_component));
1201
1202 if (iterator->methods.can_seek_beginning) {
1203 can = iterator->methods.can_seek_beginning(iterator);
1204 }
1205
1206 return can;
1207}
1208
1209static inline
003e713f 1210void set_iterator_state_after_seeking(
7474e7d3 1211 struct bt_self_component_port_input_message_iterator *iterator,
d24d5663 1212 int status)
7474e7d3
PP
1213{
1214 enum bt_self_component_port_input_message_iterator_state new_state = 0;
1215
1216 /* Set iterator's state depending on seeking status */
1217 switch (status) {
d24d5663 1218 case BT_FUNC_STATUS_OK:
7474e7d3
PP
1219 new_state = BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE;
1220 break;
d24d5663 1221 case BT_FUNC_STATUS_AGAIN:
7474e7d3
PP
1222 new_state = BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN;
1223 break;
d24d5663
PP
1224 case BT_FUNC_STATUS_ERROR:
1225 case BT_FUNC_STATUS_MEMORY_ERROR:
7474e7d3
PP
1226 new_state = BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR;
1227 break;
d24d5663 1228 case BT_FUNC_STATUS_END:
7474e7d3
PP
1229 new_state = BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED;
1230 break;
1231 default:
1232 abort();
1233 }
1234
1235 set_self_comp_port_input_msg_iterator_state(iterator, new_state);
1236}
1237
54b135a0
SM
1238static
1239void reset_iterator_expectations(
1240 struct bt_self_component_port_input_message_iterator *iterator)
1241{
1242 iterator->last_ns_from_origin = INT64_MIN;
1243 iterator->clock_expectation.type = CLOCK_EXPECTATION_UNSET;
1244}
1245
d24d5663 1246enum bt_message_iterator_seek_beginning_status
7474e7d3
PP
1247bt_self_component_port_input_message_iterator_seek_beginning(
1248 struct bt_self_component_port_input_message_iterator *iterator)
1249{
9275bef4 1250 int status;
7474e7d3
PP
1251
1252 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
1253 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator);
5badd463
PP
1254 BT_ASSERT_PRE(
1255 bt_component_borrow_graph(iterator->upstream_component)->config_state !=
1256 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
7474e7d3
PP
1257 "Graph is not configured: %!+g",
1258 bt_component_borrow_graph(iterator->upstream_component));
1259 BT_ASSERT_PRE(
1260 bt_self_component_port_input_message_iterator_can_seek_beginning(
1261 iterator),
1262 "Message iterator cannot seek beginning: %!+i", iterator);
54b135a0
SM
1263
1264 /*
1265 * We are seeking, reset our expectations about how the following
1266 * messages should look like.
1267 */
1268 reset_iterator_expectations(iterator);
1269
7474e7d3
PP
1270 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i", iterator);
1271 set_self_comp_port_input_msg_iterator_state(iterator,
1272 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_SEEKING);
1273 status = iterator->methods.seek_beginning(iterator);
1274 BT_LOGD("User method returned: status=%s",
d24d5663
PP
1275 bt_common_func_status_string(status));
1276 BT_ASSERT_POST(status == BT_FUNC_STATUS_OK ||
1277 status == BT_FUNC_STATUS_ERROR ||
1278 status == BT_FUNC_STATUS_MEMORY_ERROR ||
1279 status == BT_FUNC_STATUS_AGAIN,
7474e7d3 1280 "Unexpected status: %![iter-]+i, status=%s",
d24d5663 1281 iterator, bt_common_func_status_string(status));
870631a2
PP
1282 if (status < 0) {
1283 BT_LIB_LOGW_APPEND_CAUSE(
1284 "Component input port message iterator's \"seek beginning\" method failed: "
1285 "%![iter-]+i, status=%s",
1286 iterator, bt_common_func_status_string(status));
1287 }
1288
7474e7d3
PP
1289 set_iterator_state_after_seeking(iterator, status);
1290 return status;
1291}
1292
5b7b55be
SM
1293/*
1294 * Structure used to record the state of a given stream during the fast-forward
1295 * phase of an auto-seek.
1296 */
1297struct auto_seek_stream_state {
1298 /*
1299 * Value representing which step of this timeline we are at.
1300 *
1301 * time --->
188edac1 1302 * [SB] 1 [PB] 2 [PE] 1 [SE]
5b7b55be
SM
1303 *
1304 * At each point in the timeline, the messages we need to replicate are:
1305 *
1306 * 1: Stream beginning
188edac1 1307 * 2: Stream beginning, packet beginning
5b7b55be
SM
1308 *
1309 * Before "Stream beginning" and after "Stream end", we don't need to
1310 * replicate anything as the stream doesn't exist.
1311 */
1312 enum {
1313 AUTO_SEEK_STREAM_STATE_STREAM_BEGAN,
5b7b55be
SM
1314 AUTO_SEEK_STREAM_STATE_PACKET_BEGAN,
1315 } state;
1316
1317 /*
1318 * If `state` is AUTO_SEEK_STREAM_STATE_PACKET_BEGAN, the packet we are
1319 * in. This is a weak reference, since the packet will always be
1320 * alive by the time we use it.
1321 */
1322 struct bt_packet *packet;
188edac1
SM
1323
1324 /* Have we see a message with a clock snapshot yet? */
1325 bool seen_clock_snapshot;
5b7b55be
SM
1326};
1327
1328static
1329struct auto_seek_stream_state *create_auto_seek_stream_state(void)
1330{
1331 return g_new0(struct auto_seek_stream_state, 1);
1332}
1333
1334static
1335void destroy_auto_seek_stream_state(void *ptr)
1336{
1337 g_free(ptr);
1338}
1339
1340static
1341GHashTable *create_auto_seek_stream_states(void)
1342{
1343 return g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL,
1344 destroy_auto_seek_stream_state);
1345}
1346
1347static
1348void destroy_auto_seek_stream_states(GHashTable *stream_states)
1349{
1350 g_hash_table_destroy(stream_states);
1351}
1352
1353/*
1354 * Handle one message while we are in the fast-forward phase of an auto-seek.
1355 *
1356 * Sets `*got_first` to true if the message's timestamp is greater or equal to
1357 * `ns_from_origin`. In other words, if this is the first message after our
1358 * seek point.
1359 *
1360 * `stream_states` is an hash table of `bt_stream *` (weak reference) to
1361 * `struct auto_seek_stream_state` used to keep the state of each stream
1362 * during the fast-forward.
1363 */
1364
7474e7d3 1365static inline
d24d5663 1366int auto_seek_handle_message(
5b9e151d
PP
1367 struct bt_self_component_port_input_message_iterator *iterator,
1368 int64_t ns_from_origin, const struct bt_message *msg,
5b7b55be 1369 bool *got_first, GHashTable *stream_states)
7474e7d3 1370{
d24d5663 1371 int status = BT_FUNC_STATUS_OK;
5b9e151d 1372 int64_t msg_ns_from_origin;
7474e7d3 1373 const struct bt_clock_snapshot *clk_snapshot = NULL;
5b9e151d
PP
1374 int ret;
1375
1376 BT_ASSERT(msg);
1377 BT_ASSERT(got_first);
7474e7d3
PP
1378
1379 switch (msg->type) {
1380 case BT_MESSAGE_TYPE_EVENT:
1381 {
1382 const struct bt_message_event *event_msg =
1383 (const void *) msg;
1384
2c091c04 1385 clk_snapshot = event_msg->default_cs;
bdb288b3 1386 BT_ASSERT_POST_DEV(clk_snapshot,
c7072d5a
PP
1387 "Event message has no default clock snapshot: %!+n",
1388 event_msg);
7474e7d3
PP
1389 break;
1390 }
b9fd9cbb 1391 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY:
7474e7d3 1392 {
b9fd9cbb 1393 const struct bt_message_message_iterator_inactivity *inactivity_msg =
7474e7d3
PP
1394 (const void *) msg;
1395
7474e7d3 1396 clk_snapshot = inactivity_msg->default_cs;
16663a5e 1397 BT_ASSERT(clk_snapshot);
7474e7d3
PP
1398 break;
1399 }
16663a5e
PP
1400 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
1401 case BT_MESSAGE_TYPE_PACKET_END:
c7072d5a
PP
1402 {
1403 const struct bt_message_packet *packet_msg =
1404 (const void *) msg;
1405
1406 clk_snapshot = packet_msg->default_cs;
bdb288b3 1407 BT_ASSERT_POST_DEV(clk_snapshot,
c7072d5a
PP
1408 "Packet message has no default clock snapshot: %!+n",
1409 packet_msg);
1410 break;
1411 }
16663a5e
PP
1412 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
1413 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
7474e7d3 1414 {
5b9e151d
PP
1415 struct bt_message_discarded_items *msg_disc_items =
1416 (void *) msg;
1417
bdb288b3 1418 BT_ASSERT_POST_DEV(msg_disc_items->default_begin_cs &&
5b9e151d
PP
1419 msg_disc_items->default_end_cs,
1420 "Discarded events/packets message has no default clock snapshots: %!+n",
1421 msg_disc_items);
1422 ret = bt_clock_snapshot_get_ns_from_origin(
1423 msg_disc_items->default_begin_cs,
1424 &msg_ns_from_origin);
1425 if (ret) {
d24d5663 1426 status = BT_FUNC_STATUS_ERROR;
5b9e151d
PP
1427 goto end;
1428 }
7474e7d3 1429
5b9e151d
PP
1430 if (msg_ns_from_origin >= ns_from_origin) {
1431 *got_first = true;
1432 goto push_msg;
1433 }
1434
1435 ret = bt_clock_snapshot_get_ns_from_origin(
1436 msg_disc_items->default_end_cs,
1437 &msg_ns_from_origin);
1438 if (ret) {
d24d5663 1439 status = BT_FUNC_STATUS_ERROR;
5b9e151d
PP
1440 goto end;
1441 }
1442
1443 if (msg_ns_from_origin >= ns_from_origin) {
1444 /*
1445 * The discarded items message's beginning time
1446 * is before the requested seeking time, but its
1447 * end time is after. Modify the message so as
1448 * to set its beginning time to the requested
1449 * seeking time, and make its item count unknown
1450 * as we don't know if items were really
1451 * discarded within the new time range.
1452 */
4af85094 1453 uint64_t new_begin_raw_value = 0;
5b9e151d
PP
1454
1455 ret = bt_clock_class_clock_value_from_ns_from_origin(
1456 msg_disc_items->default_end_cs->clock_class,
1457 ns_from_origin, &new_begin_raw_value);
1458 if (ret) {
d24d5663 1459 status = BT_FUNC_STATUS_ERROR;
5b9e151d
PP
1460 goto end;
1461 }
1462
1463 bt_clock_snapshot_set_raw_value(
1464 msg_disc_items->default_begin_cs,
1465 new_begin_raw_value);
1466 msg_disc_items->count.base.avail =
1467 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE;
1468
1469 /*
1470 * It is safe to push it because its beginning
1471 * time is exactly the requested seeking time.
1472 */
1473 goto push_msg;
1474 } else {
1475 goto skip_msg;
1476 }
7474e7d3 1477 }
188edac1
SM
1478 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
1479 case BT_MESSAGE_TYPE_STREAM_END:
7474e7d3 1480 {
188edac1
SM
1481 struct bt_message_stream *stream_msg =
1482 (struct bt_message_stream *) msg;
7474e7d3 1483
188edac1
SM
1484 if (stream_msg->default_cs_state != BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN) {
1485 /* Ignore */
5b9e151d 1486 goto skip_msg;
16663a5e
PP
1487 }
1488
188edac1 1489 clk_snapshot = stream_msg->default_cs;
7474e7d3
PP
1490 break;
1491 }
1492 default:
1493 abort();
1494 }
1495
5b9e151d
PP
1496 BT_ASSERT(clk_snapshot);
1497 ret = bt_clock_snapshot_get_ns_from_origin(clk_snapshot,
1498 &msg_ns_from_origin);
1499 if (ret) {
d24d5663 1500 status = BT_FUNC_STATUS_ERROR;
7474e7d3
PP
1501 goto end;
1502 }
1503
5b9e151d
PP
1504 if (msg_ns_from_origin >= ns_from_origin) {
1505 *got_first = true;
1506 goto push_msg;
1507 }
1508
1509skip_msg:
5b7b55be
SM
1510 /* This message won't be sent downstream. */
1511 switch (msg->type) {
1512 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
1513 {
1514 const struct bt_message_stream *stream_msg = (const void *) msg;
1515 struct auto_seek_stream_state *stream_state;
5b7b55be
SM
1516
1517 /* Update stream's state: stream began. */
1518 stream_state = create_auto_seek_stream_state();
1519 if (!stream_state) {
d24d5663 1520 status = BT_FUNC_STATUS_MEMORY_ERROR;
5b7b55be
SM
1521 goto end;
1522 }
1523
1524 stream_state->state = AUTO_SEEK_STREAM_STATE_STREAM_BEGAN;
e74dbb33 1525
188edac1
SM
1526 if (stream_msg->default_cs_state == BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN) {
1527 stream_state->seen_clock_snapshot = true;
1528 }
1529
e74dbb33
SM
1530 BT_ASSERT(!bt_g_hash_table_contains(stream_states, stream_msg->stream));
1531 g_hash_table_insert(stream_states, stream_msg->stream, stream_state);
5b7b55be
SM
1532 break;
1533 }
188edac1 1534 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
5b7b55be 1535 {
188edac1 1536 const struct bt_message_packet *packet_msg =
5b7b55be
SM
1537 (const void *) msg;
1538 struct auto_seek_stream_state *stream_state;
1539
188edac1
SM
1540 /* Update stream's state: packet began. */
1541 stream_state = g_hash_table_lookup(stream_states, packet_msg->packet->stream);
5b7b55be
SM
1542 BT_ASSERT(stream_state);
1543
1544 BT_ASSERT(stream_state->state == AUTO_SEEK_STREAM_STATE_STREAM_BEGAN);
188edac1 1545 stream_state->state = AUTO_SEEK_STREAM_STATE_PACKET_BEGAN;
5b7b55be 1546 BT_ASSERT(!stream_state->packet);
188edac1
SM
1547 stream_state->packet = packet_msg->packet;
1548
1549 if (packet_msg->packet->stream->class->packets_have_beginning_default_clock_snapshot) {
1550 stream_state->seen_clock_snapshot = true;
1551 }
1552
5b7b55be
SM
1553 break;
1554 }
188edac1 1555 case BT_MESSAGE_TYPE_EVENT:
5b7b55be 1556 {
188edac1 1557 const struct bt_message_event *event_msg = (const void *) msg;
5b7b55be
SM
1558 struct auto_seek_stream_state *stream_state;
1559
188edac1
SM
1560 stream_state = g_hash_table_lookup(stream_states,
1561 event_msg->event->packet->stream);
5b7b55be
SM
1562 BT_ASSERT(stream_state);
1563
188edac1
SM
1564 // HELPME: are we sure that event messages have clock snapshots at this point?
1565 stream_state->seen_clock_snapshot = true;
1566
5b7b55be
SM
1567 break;
1568 }
1569 case BT_MESSAGE_TYPE_PACKET_END:
1570 {
1571 const struct bt_message_packet *packet_msg =
1572 (const void *) msg;
1573 struct auto_seek_stream_state *stream_state;
1574
1575 /* Update stream's state: packet ended. */
1576 stream_state = g_hash_table_lookup(stream_states, packet_msg->packet->stream);
1577 BT_ASSERT(stream_state);
1578
1579 BT_ASSERT(stream_state->state == AUTO_SEEK_STREAM_STATE_PACKET_BEGAN);
188edac1 1580 stream_state->state = AUTO_SEEK_STREAM_STATE_STREAM_BEGAN;
5b7b55be
SM
1581 BT_ASSERT(stream_state->packet);
1582 stream_state->packet = NULL;
5b7b55be 1583
188edac1
SM
1584 if (packet_msg->packet->stream->class->packets_have_end_default_clock_snapshot) {
1585 stream_state->seen_clock_snapshot = true;
1586 }
5b7b55be 1587
5b7b55be
SM
1588 break;
1589 }
1590 case BT_MESSAGE_TYPE_STREAM_END:
1591 {
1592 const struct bt_message_stream *stream_msg = (const void *) msg;
1593 struct auto_seek_stream_state *stream_state;
1594
1595 stream_state = g_hash_table_lookup(stream_states, stream_msg->stream);
1596 BT_ASSERT(stream_state);
1597 BT_ASSERT(stream_state->state == AUTO_SEEK_STREAM_STATE_STREAM_BEGAN);
1598 BT_ASSERT(!stream_state->packet);
1599
1600 /* Update stream's state: this stream doesn't exist anymore. */
1601 g_hash_table_remove(stream_states, stream_msg->stream);
1602 break;
1603 }
188edac1
SM
1604 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
1605 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
1606 {
1607 const struct bt_message_discarded_items *discarded_msg =
1608 (const void *) msg;
1609 struct auto_seek_stream_state *stream_state;
1610
1611 stream_state = g_hash_table_lookup(stream_states, discarded_msg->stream);
1612 BT_ASSERT(stream_state);
1613
1614 if ((msg->type == BT_MESSAGE_TYPE_DISCARDED_EVENTS && discarded_msg->stream->class->discarded_events_have_default_clock_snapshots) ||
1615 (msg->type == BT_MESSAGE_TYPE_DISCARDED_PACKETS && discarded_msg->stream->class->discarded_packets_have_default_clock_snapshots)) {
1616 stream_state->seen_clock_snapshot = true;
1617 }
1618
1619 break;
1620 }
5b7b55be
SM
1621 default:
1622 break;
1623 }
1624
5b9e151d 1625 bt_object_put_no_null_check(msg);
04c0cec6 1626 msg = NULL;
5b9e151d
PP
1627 goto end;
1628
1629push_msg:
da9c4c52 1630 g_queue_push_tail(iterator->auto_seek.msgs, (void *) msg);
5b9e151d 1631 msg = NULL;
7474e7d3
PP
1632
1633end:
d24d5663 1634 BT_ASSERT(!msg || status != BT_FUNC_STATUS_OK);
5b9e151d 1635 return status;
7474e7d3
PP
1636}
1637
1638static
d24d5663 1639int find_message_ge_ns_from_origin(
7474e7d3 1640 struct bt_self_component_port_input_message_iterator *iterator,
5b7b55be 1641 int64_t ns_from_origin, GHashTable *stream_states)
7474e7d3
PP
1642{
1643 int status;
1644 enum bt_self_component_port_input_message_iterator_state init_state =
1645 iterator->state;
1646 const struct bt_message *messages[MSG_BATCH_SIZE];
1647 uint64_t user_count = 0;
1648 uint64_t i;
5b9e151d 1649 bool got_first = false;
7474e7d3
PP
1650
1651 BT_ASSERT(iterator);
1652 memset(&messages[0], 0, sizeof(messages[0]) * MSG_BATCH_SIZE);
1653
1654 /*
1655 * Make this iterator temporarily active (not seeking) to call
1656 * the "next" method.
1657 */
1658 set_self_comp_port_input_msg_iterator_state(iterator,
1659 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE);
1660
1661 BT_ASSERT(iterator->methods.next);
1662
e0dade92 1663 while (!got_first) {
7474e7d3
PP
1664 /*
1665 * Call the user's "next" method to get the next
1666 * messages and status.
1667 */
54b135a0 1668 status = call_iterator_next_method(iterator,
7474e7d3 1669 &messages[0], MSG_BATCH_SIZE, &user_count);
870631a2
PP
1670 BT_LOGD("User method returned: status=%s",
1671 bt_common_func_status_string(status));
1672 if (status < 0) {
1673 BT_LIB_LOGW_APPEND_CAUSE(
1674 "Component input port message iterator's \"next\" method failed: "
1675 "%![iter-]+i, status=%s",
1676 iterator, bt_common_func_status_string(status));
1677 }
7474e7d3 1678
7474e7d3
PP
1679 /*
1680 * The user's "next" method must not do any action which
1681 * would change the iterator's state.
1682 */
1683 BT_ASSERT(iterator->state ==
1684 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE);
7474e7d3
PP
1685
1686 switch (status) {
d24d5663 1687 case BT_FUNC_STATUS_OK:
bdb288b3 1688 BT_ASSERT_POST_DEV(user_count <= MSG_BATCH_SIZE,
7474e7d3
PP
1689 "Invalid returned message count: greater than "
1690 "batch size: count=%" PRIu64 ", batch-size=%u",
1691 user_count, MSG_BATCH_SIZE);
1692 break;
d24d5663
PP
1693 case BT_FUNC_STATUS_AGAIN:
1694 case BT_FUNC_STATUS_ERROR:
1695 case BT_FUNC_STATUS_MEMORY_ERROR:
1696 case BT_FUNC_STATUS_END:
7474e7d3
PP
1697 goto end;
1698 default:
1699 abort();
1700 }
1701
7474e7d3 1702 for (i = 0; i < user_count; i++) {
5b9e151d 1703 if (got_first) {
da9c4c52 1704 g_queue_push_tail(iterator->auto_seek.msgs,
5b9e151d
PP
1705 (void *) messages[i]);
1706 messages[i] = NULL;
7474e7d3
PP
1707 continue;
1708 }
1709
5b9e151d 1710 status = auto_seek_handle_message(iterator,
5b7b55be
SM
1711 ns_from_origin, messages[i], &got_first,
1712 stream_states);
d24d5663 1713 if (status == BT_FUNC_STATUS_OK) {
e0dade92 1714 /* Message was either pushed or moved */
5b9e151d
PP
1715 messages[i] = NULL;
1716 } else {
7474e7d3
PP
1717 goto end;
1718 }
7474e7d3
PP
1719 }
1720 }
1721
1722end:
1723 for (i = 0; i < user_count; i++) {
1724 if (messages[i]) {
1725 bt_object_put_no_null_check(messages[i]);
1726 }
1727 }
1728
1729 set_self_comp_port_input_msg_iterator_state(iterator, init_state);
1730 return status;
1731}
1732
5b7b55be
SM
1733/*
1734 * This function is installed as the iterator's next callback after we have
1735 * auto-seeked (seeked to the beginning and fast-forwarded) to send the
1736 * messages saved in iterator->auto_seek.msgs. Once this is done, the original
1737 * next callback is put back.
1738 */
1739
7474e7d3 1740static
d24d5663 1741enum bt_component_class_message_iterator_next_method_status post_auto_seek_next(
7474e7d3
PP
1742 struct bt_self_component_port_input_message_iterator *iterator,
1743 bt_message_array_const msgs, uint64_t capacity,
1744 uint64_t *count)
1745{
da9c4c52 1746 BT_ASSERT(!g_queue_is_empty(iterator->auto_seek.msgs));
5b9e151d 1747 *count = 0;
7474e7d3
PP
1748
1749 /*
1750 * Move auto-seek messages to the output array (which is this
5b9e151d 1751 * iterator's base message array).
7474e7d3 1752 */
da9c4c52
SM
1753 while (capacity > 0 && !g_queue_is_empty(iterator->auto_seek.msgs)) {
1754 msgs[*count] = g_queue_pop_head(iterator->auto_seek.msgs);
5b9e151d
PP
1755 capacity--;
1756 (*count)++;
7474e7d3 1757 }
7474e7d3 1758
5b9e151d
PP
1759 BT_ASSERT(*count > 0);
1760
da9c4c52 1761 if (g_queue_is_empty(iterator->auto_seek.msgs)) {
572075a8
SM
1762 /* No more auto-seek messages, restore user's next callback. */
1763 BT_ASSERT(iterator->auto_seek.original_next_callback);
1764 iterator->methods.next = iterator->auto_seek.original_next_callback;
1765 iterator->auto_seek.original_next_callback = NULL;
7474e7d3
PP
1766 }
1767
d24d5663 1768 return BT_FUNC_STATUS_OK;
7474e7d3
PP
1769}
1770
5b7b55be
SM
1771static inline
1772int clock_raw_value_from_ns_from_origin(const bt_clock_class *clock_class,
1773 int64_t ns_from_origin, uint64_t *raw_value)
1774{
1775
1776 int64_t cc_offset_s = clock_class->offset_seconds;
1777 uint64_t cc_offset_cycles = clock_class->offset_cycles;
1778 uint64_t cc_freq = clock_class->frequency;
1779
1780 return bt_common_clock_value_from_ns_from_origin(cc_offset_s,
1781 cc_offset_cycles, cc_freq, ns_from_origin, raw_value);
1782}
1783
1784
d24d5663 1785enum bt_message_iterator_seek_ns_from_origin_status
7474e7d3
PP
1786bt_self_component_port_input_message_iterator_seek_ns_from_origin(
1787 struct bt_self_component_port_input_message_iterator *iterator,
1788 int64_t ns_from_origin)
1789{
1790 int status;
5b7b55be 1791 GHashTable *stream_states = NULL;
7474e7d3
PP
1792
1793 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
1794 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator);
5badd463
PP
1795 BT_ASSERT_PRE(
1796 bt_component_borrow_graph(iterator->upstream_component)->config_state !=
1797 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
7474e7d3
PP
1798 "Graph is not configured: %!+g",
1799 bt_component_borrow_graph(iterator->upstream_component));
1800 BT_ASSERT_PRE(
1801 bt_self_component_port_input_message_iterator_can_seek_ns_from_origin(
1802 iterator, ns_from_origin),
1803 "Message iterator cannot seek nanoseconds from origin: %!+i, "
1804 "ns-from-origin=%" PRId64, iterator, ns_from_origin);
1805 set_self_comp_port_input_msg_iterator_state(iterator,
1806 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_SEEKING);
1807
54b135a0
SM
1808 /*
1809 * We are seeking, reset our expectations about how the following
1810 * messages should look like.
1811 */
1812 reset_iterator_expectations(iterator);
1813
7474e7d3 1814 if (iterator->methods.seek_ns_from_origin) {
5b7b55be 1815 /* The iterator knows how to seek to a particular time, let it handle this. */
7474e7d3
PP
1816 BT_LIB_LOGD("Calling user's \"seek nanoseconds from origin\" method: "
1817 "%![iter-]+i, ns=%" PRId64, iterator, ns_from_origin);
1818 status = iterator->methods.seek_ns_from_origin(iterator,
1819 ns_from_origin);
1820 BT_LOGD("User method returned: status=%s",
d24d5663
PP
1821 bt_common_func_status_string(status));
1822 BT_ASSERT_POST(status == BT_FUNC_STATUS_OK ||
1823 status == BT_FUNC_STATUS_ERROR ||
1824 status == BT_FUNC_STATUS_MEMORY_ERROR ||
1825 status == BT_FUNC_STATUS_AGAIN,
7474e7d3 1826 "Unexpected status: %![iter-]+i, status=%s",
d24d5663 1827 iterator, bt_common_func_status_string(status));
870631a2
PP
1828 if (status < 0) {
1829 BT_LIB_LOGW_APPEND_CAUSE(
1830 "Component input port message iterator's \"seek nanoseconds from origin\" method failed: "
1831 "%![iter-]+i, status=%s",
1832 iterator, bt_common_func_status_string(status));
1833 }
7474e7d3 1834 } else {
5b7b55be
SM
1835 /*
1836 * The iterator doesn't know how to seek to a particular time. We will
1837 * seek to the beginning and fast forward to the right place.
1838 */
7474e7d3
PP
1839 BT_ASSERT(iterator->methods.can_seek_beginning(iterator));
1840 BT_ASSERT(iterator->methods.seek_beginning);
1841 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i",
1842 iterator);
1843 status = iterator->methods.seek_beginning(iterator);
1844 BT_LOGD("User method returned: status=%s",
d24d5663
PP
1845 bt_common_func_status_string(status));
1846 BT_ASSERT_POST(status == BT_FUNC_STATUS_OK ||
1847 status == BT_FUNC_STATUS_ERROR ||
1848 status == BT_FUNC_STATUS_MEMORY_ERROR ||
1849 status == BT_FUNC_STATUS_AGAIN,
7474e7d3 1850 "Unexpected status: %![iter-]+i, status=%s",
d24d5663 1851 iterator, bt_common_func_status_string(status));
870631a2
PP
1852 if (status < 0) {
1853 BT_LIB_LOGW_APPEND_CAUSE(
1854 "Component input port message iterator's \"seek beginning\" method failed: "
1855 "%![iter-]+i, status=%s",
1856 iterator, bt_common_func_status_string(status));
1857 }
1858
7474e7d3 1859 switch (status) {
d24d5663 1860 case BT_FUNC_STATUS_OK:
7474e7d3 1861 break;
d24d5663
PP
1862 case BT_FUNC_STATUS_ERROR:
1863 case BT_FUNC_STATUS_MEMORY_ERROR:
1864 case BT_FUNC_STATUS_AGAIN:
7474e7d3
PP
1865 goto end;
1866 default:
1867 abort();
1868 }
1869
1870 /*
1871 * Find the first message which has a default clock
1872 * snapshot greater than or equal to the requested
5b9e151d
PP
1873 * seeking time, and move the received messages from
1874 * this point in the batch to this iterator's auto-seek
1875 * message queue.
7474e7d3 1876 */
da9c4c52 1877 while (!g_queue_is_empty(iterator->auto_seek.msgs)) {
5b9e151d 1878 bt_object_put_no_null_check(
da9c4c52 1879 g_queue_pop_tail(iterator->auto_seek.msgs));
5b9e151d
PP
1880 }
1881
5b7b55be
SM
1882 stream_states = create_auto_seek_stream_states();
1883 if (!stream_states) {
870631a2
PP
1884 BT_LIB_LOGE_APPEND_CAUSE(
1885 "Failed to allocate one GHashTable.");
d24d5663 1886 status = BT_FUNC_STATUS_MEMORY_ERROR;
5b7b55be
SM
1887 goto end;
1888 }
1889
7474e7d3 1890 status = find_message_ge_ns_from_origin(iterator,
5b7b55be 1891 ns_from_origin, stream_states);
7474e7d3 1892 switch (status) {
d24d5663
PP
1893 case BT_FUNC_STATUS_OK:
1894 case BT_FUNC_STATUS_END:
5b7b55be
SM
1895 {
1896 GHashTableIter iter;
1897 gpointer key, value;
1898
1899 /*
1900 * If some streams exist at the seek time, prepend the
1901 * required messages to put those streams in the right
1902 * state.
1903 */
1904 g_hash_table_iter_init(&iter, stream_states);
1905 while (g_hash_table_iter_next (&iter, &key, &value)) {
1906 const bt_stream *stream = key;
1907 struct auto_seek_stream_state *stream_state =
1908 (struct auto_seek_stream_state *) value;
1909 bt_message *msg;
1910 const bt_clock_class *clock_class = bt_stream_class_borrow_default_clock_class_const(
1911 bt_stream_borrow_class_const(stream));
188edac1
SM
1912 /* Initialize to silence maybe-uninitialized warning. */
1913 uint64_t raw_value = 0;
1914
1915 /*
1916 * If we haven't seen a message with a clock snapshot, we don't know if our seek time is within
1917 * the clock's range, so it wouldn't be safe to try to convert ns_from_origin to a clock value.
1918 *
1919 * Also, it would be a bit of a lie to generate a stream begin message with the seek time as its
1920 * clock snapshot, because we don't really know if the stream existed at that time. If we have
1921 * seen a message with a clock snapshot in our seeking, then we are sure that the
1922 * seek time is not below the clock range, and we know the stream was active at that
1923 * time (and that we cut it short).
1924 */
1925 if (stream_state->seen_clock_snapshot) {
1926 if (clock_raw_value_from_ns_from_origin(clock_class, ns_from_origin, &raw_value) != 0) {
1927 BT_LIB_LOGW("Could not convert nanoseconds from origin to clock value: ns-from-origin=%" PRId64 ", %![cc-]+K",
1928 ns_from_origin, clock_class);
1929 status = BT_FUNC_STATUS_ERROR;
1930 goto end;
1931 }
5b7b55be
SM
1932 }
1933
1934 switch (stream_state->state) {
1935 case AUTO_SEEK_STREAM_STATE_PACKET_BEGAN:
1936 BT_ASSERT(stream_state->packet);
1937 BT_LIB_LOGD("Creating packet message: %![packet-]+a", stream_state->packet);
188edac1
SM
1938
1939 if (stream->class->packets_have_beginning_default_clock_snapshot) {
1940 /*
1941 * If we are in the PACKET_BEGAN state, it means we have seen a "packet beginning"
1942 * message. If "packet beginning" packets have clock snapshots, then we must have
1943 * seen a clock snapshot.
1944 */
1945 BT_ASSERT(stream_state->seen_clock_snapshot);
1946
1947 msg = bt_message_packet_beginning_create_with_default_clock_snapshot(
1948 (bt_self_message_iterator *) iterator, stream_state->packet, raw_value);
1949 } else {
1950 msg = bt_message_packet_beginning_create((bt_self_message_iterator *) iterator,
1951 stream_state->packet);
5b7b55be
SM
1952 }
1953
5b7b55be 1954 if (!msg) {
d24d5663 1955 status = BT_FUNC_STATUS_MEMORY_ERROR;
5b7b55be
SM
1956 goto end;
1957 }
1958
5b7b55be
SM
1959 g_queue_push_head(iterator->auto_seek.msgs, msg);
1960 msg = NULL;
1961 /* fall-thru */
188edac1 1962
5b7b55be
SM
1963 case AUTO_SEEK_STREAM_STATE_STREAM_BEGAN:
1964 msg = bt_message_stream_beginning_create(
1965 (bt_self_message_iterator *) iterator, stream);
1966 if (!msg) {
d24d5663 1967 status = BT_FUNC_STATUS_MEMORY_ERROR;
5b7b55be
SM
1968 goto end;
1969 }
1970
188edac1
SM
1971 if (stream_state->seen_clock_snapshot) {
1972 bt_message_stream_beginning_set_default_clock_snapshot(msg, raw_value);
1973 }
1974
5b7b55be
SM
1975 g_queue_push_head(iterator->auto_seek.msgs, msg);
1976 msg = NULL;
1977 break;
1978 }
1979 }
1980
7474e7d3 1981 /*
5b9e151d
PP
1982 * If there are messages in the auto-seek
1983 * message queue, replace the user's "next"
1984 * method with a custom, temporary "next" method
1985 * which returns them.
7474e7d3 1986 */
da9c4c52 1987 if (!g_queue_is_empty(iterator->auto_seek.msgs)) {
572075a8
SM
1988 BT_ASSERT(!iterator->auto_seek.original_next_callback);
1989 iterator->auto_seek.original_next_callback = iterator->methods.next;
1990
5b9e151d
PP
1991 iterator->methods.next =
1992 (bt_self_component_port_input_message_iterator_next_method)
1993 post_auto_seek_next;
1994 }
1995
1996 /*
d24d5663
PP
1997 * `BT_FUNC_STATUS_END` becomes
1998 * `BT_FUNC_STATUS_OK`: the next
5b9e151d
PP
1999 * time this iterator's "next" method is called,
2000 * it will return
d24d5663 2001 * `BT_FUNC_STATUS_END`.
5b9e151d 2002 */
d24d5663 2003 status = BT_FUNC_STATUS_OK;
7474e7d3 2004 break;
5b7b55be 2005 }
d24d5663
PP
2006 case BT_FUNC_STATUS_ERROR:
2007 case BT_FUNC_STATUS_MEMORY_ERROR:
2008 case BT_FUNC_STATUS_AGAIN:
7474e7d3 2009 goto end;
7474e7d3
PP
2010 default:
2011 abort();
2012 }
2013 }
2014
54b135a0
SM
2015 /*
2016 * The following messages returned by the next method (including
2017 * post_auto_seek_next) must be after (or at) `ns_from_origin`.
2018 */
2019 iterator->last_ns_from_origin = ns_from_origin;
2020
7474e7d3 2021end:
5b7b55be
SM
2022 if (stream_states) {
2023 destroy_auto_seek_stream_states(stream_states);
2024 stream_states = NULL;
2025 }
870631a2 2026
7474e7d3 2027 set_iterator_state_after_seeking(iterator, status);
7474e7d3
PP
2028 return status;
2029}
2030
2031static inline
2032bt_self_component_port_input_message_iterator *
2033borrow_output_port_message_iterator_upstream_iterator(
2034 struct bt_port_output_message_iterator *iterator)
2035{
2036 struct bt_component_class_sink_colander_priv_data *colander_data;
2037
2038 BT_ASSERT(iterator);
2039 colander_data = (void *) iterator->colander->parent.user_data;
2040 BT_ASSERT(colander_data);
2041 BT_ASSERT(colander_data->msg_iter);
2042 return colander_data->msg_iter;
2043}
2044
2045bt_bool bt_port_output_message_iterator_can_seek_ns_from_origin(
2046 struct bt_port_output_message_iterator *iterator,
2047 int64_t ns_from_origin)
2048{
2049 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
2050 return bt_self_component_port_input_message_iterator_can_seek_ns_from_origin(
2051 borrow_output_port_message_iterator_upstream_iterator(
2052 iterator), ns_from_origin);
2053}
2054
2055bt_bool bt_port_output_message_iterator_can_seek_beginning(
2056 struct bt_port_output_message_iterator *iterator)
2057{
2058 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
2059 return bt_self_component_port_input_message_iterator_can_seek_beginning(
2060 borrow_output_port_message_iterator_upstream_iterator(
2061 iterator));
2062}
2063
d24d5663
PP
2064enum bt_message_iterator_seek_ns_from_origin_status
2065bt_port_output_message_iterator_seek_ns_from_origin(
7474e7d3
PP
2066 struct bt_port_output_message_iterator *iterator,
2067 int64_t ns_from_origin)
2068{
2069 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
2070 return bt_self_component_port_input_message_iterator_seek_ns_from_origin(
2071 borrow_output_port_message_iterator_upstream_iterator(iterator),
2072 ns_from_origin);
2073}
2074
d24d5663
PP
2075enum bt_message_iterator_seek_beginning_status
2076bt_port_output_message_iterator_seek_beginning(
7474e7d3
PP
2077 struct bt_port_output_message_iterator *iterator)
2078{
2079 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
2080 return bt_self_component_port_input_message_iterator_seek_beginning(
2081 borrow_output_port_message_iterator_upstream_iterator(
2082 iterator));
2083}
2084
d6e69534
PP
2085void bt_port_output_message_iterator_get_ref(
2086 const struct bt_port_output_message_iterator *iterator)
c5b9b441
PP
2087{
2088 bt_object_get_ref(iterator);
2089}
2090
d6e69534
PP
2091void bt_port_output_message_iterator_put_ref(
2092 const struct bt_port_output_message_iterator *iterator)
c5b9b441
PP
2093{
2094 bt_object_put_ref(iterator);
2095}
2096
d6e69534
PP
2097void bt_self_component_port_input_message_iterator_get_ref(
2098 const struct bt_self_component_port_input_message_iterator *iterator)
c5b9b441
PP
2099{
2100 bt_object_get_ref(iterator);
2101}
2102
d6e69534
PP
2103void bt_self_component_port_input_message_iterator_put_ref(
2104 const struct bt_self_component_port_input_message_iterator *iterator)
c5b9b441
PP
2105{
2106 bt_object_put_ref(iterator);
2107}
This page took 0.159457 seconds and 4 git commands to generate.