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