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