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