bt_port_output_message_iterator_create(): check that graph has no sinks
[babeltrace.git] / lib / graph / iterator.c
CommitLineData
47e5a032 1/*
f2b0325d 2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
47e5a032 3 * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
47e5a032
JG
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
b09a5592 24#define BT_LOG_TAG "MSG-ITER"
5af447e5
PP
25#include <babeltrace/lib-logging-internal.h>
26
3d9990ac 27#include <babeltrace/compiler-internal.h>
3d902f17
PP
28#include <babeltrace/trace-ir/clock-class-internal.h>
29#include <babeltrace/trace-ir/clock-snapshot-internal.h>
0f15f666 30#include <babeltrace/trace-ir/field.h>
78cf9df6 31#include <babeltrace/trace-ir/event-const.h>
108b91d0 32#include <babeltrace/trace-ir/event-internal.h>
78cf9df6 33#include <babeltrace/trace-ir/packet-const.h>
108b91d0
PP
34#include <babeltrace/trace-ir/packet-internal.h>
35#include <babeltrace/trace-ir/stream-internal.h>
7b53201c 36#include <babeltrace/graph/connection-const.h>
bd14d768 37#include <babeltrace/graph/connection-internal.h>
7b53201c 38#include <babeltrace/graph/component-const.h>
9e550e5f 39#include <babeltrace/graph/component-internal.h>
b2e0c907
PP
40#include <babeltrace/graph/component-source-internal.h>
41#include <babeltrace/graph/component-class-internal.h>
c3ac0193 42#include <babeltrace/graph/component-class-sink-colander-internal.h>
7b53201c 43#include <babeltrace/graph/component-sink-const.h>
15a52f66 44#include <babeltrace/graph/component-sink-internal.h>
b09a5592 45#include <babeltrace/graph/message-const.h>
15a52f66 46#include <babeltrace/graph/message-iterator-const.h>
b09a5592
PP
47#include <babeltrace/graph/message-iterator-internal.h>
48#include <babeltrace/graph/self-component-port-input-message-iterator.h>
49#include <babeltrace/graph/port-output-message-iterator.h>
50#include <babeltrace/graph/message-internal.h>
51#include <babeltrace/graph/message-event-const.h>
52#include <babeltrace/graph/message-event-internal.h>
a1f053a9
PP
53#include <babeltrace/graph/message-packet-beginning-const.h>
54#include <babeltrace/graph/message-packet-end-const.h>
b09a5592 55#include <babeltrace/graph/message-packet-internal.h>
a1f053a9
PP
56#include <babeltrace/graph/message-stream-beginning-const.h>
57#include <babeltrace/graph/message-stream-end-const.h>
b09a5592 58#include <babeltrace/graph/message-stream-internal.h>
40bf6fd0 59#include <babeltrace/graph/message-message-iterator-inactivity-internal.h>
f629b891
PP
60#include <babeltrace/graph/message-discarded-items-internal.h>
61#include <babeltrace/graph/message-stream-activity-internal.h>
7b53201c
PP
62#include <babeltrace/graph/port-const.h>
63#include <babeltrace/graph/graph.h>
64#include <babeltrace/graph/graph-const.h>
c3ac0193 65#include <babeltrace/graph/graph-internal.h>
c55a9f58 66#include <babeltrace/types.h>
8b45963b 67#include <babeltrace/assert-internal.h>
6ff151ad 68#include <babeltrace/assert-pre-internal.h>
fa054faf 69#include <stdint.h>
b04a393b 70#include <inttypes.h>
0fbb9a9f 71#include <stdlib.h>
3230ee6b 72
3fd7b79d
PP
73/*
74 * TODO: Use graph's state (number of active iterators, etc.) and
75 * possibly system specifications to make a better guess than this.
76 */
b09a5592 77#define MSG_BATCH_SIZE 15
3fd7b79d 78
15a52f66
PP
79#define BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(_iter) \
80 BT_ASSERT_PRE((_iter)->state == BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE || \
81 (_iter)->state == BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED || \
82 (_iter)->state == BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN || \
83 (_iter)->state == BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR, \
84 "Message iterator is in the wrong state: %!+i", _iter)
47e5a032 85
904860cb
PP
86static inline
87void _set_self_comp_port_input_msg_iterator_state(
88 struct bt_self_component_port_input_message_iterator *iterator,
89 enum bt_self_component_port_input_message_iterator_state state)
90{
91 BT_ASSERT(iterator);
15a52f66 92 BT_LIB_LOGD("Updating message iterator's state: new-state=%s",
904860cb
PP
93 bt_self_component_port_input_message_iterator_state_string(state));
94 iterator->state = state;
95}
96
97#ifdef BT_DEV_MODE
98# define set_self_comp_port_input_msg_iterator_state _set_self_comp_port_input_msg_iterator_state
99#else
100# define set_self_comp_port_input_msg_iterator_state(_a, _b)
101#endif
102
c3ac0193 103static
b09a5592 104void destroy_base_message_iterator(struct bt_object *obj)
c3ac0193 105{
b09a5592 106 struct bt_message_iterator *iterator = (void *) obj;
3fd7b79d
PP
107
108 BT_ASSERT(iterator);
109
b09a5592
PP
110 if (iterator->msgs) {
111 g_ptr_array_free(iterator->msgs, TRUE);
112 iterator->msgs = NULL;
3fd7b79d
PP
113 }
114
115 g_free(iterator);
c3ac0193
PP
116}
117
47e5a032 118static
b09a5592 119void bt_self_component_port_input_message_iterator_destroy(struct bt_object *obj)
47e5a032 120{
b09a5592 121 struct bt_self_component_port_input_message_iterator *iterator;
8738a040 122
8b45963b 123 BT_ASSERT(obj);
d3eb6e8f 124
bd14d768 125 /*
b09a5592 126 * The message iterator's reference count is 0 if we're
bd14d768
PP
127 * here. Increment it to avoid a double-destroy (possibly
128 * infinitely recursive). This could happen for example if the
b09a5592 129 * message iterator's finalization function does
834e9996
PP
130 * bt_object_get_ref() (or anything that causes
131 * bt_object_get_ref() to be called) on itself (ref. count goes
132 * from 0 to 1), and then bt_object_put_ref(): the reference
133 * count would go from 1 to 0 again and this function would be
134 * called again.
bd14d768 135 */
1d7bf349 136 obj->ref_count++;
94a96686 137 iterator = (void *) obj;
b09a5592 138 BT_LIB_LOGD("Destroying self component input port message iterator object: "
834e9996 139 "%!+i", iterator);
904860cb 140 bt_self_component_port_input_message_iterator_try_finalize(iterator);
d3eb6e8f 141
bd14d768
PP
142 if (iterator->connection) {
143 /*
144 * Remove ourself from the originating connection so
145 * that it does not try to finalize a dangling pointer
146 * later.
147 */
148 bt_connection_remove_iterator(iterator->connection, iterator);
834e9996 149 iterator->connection = NULL;
bd14d768
PP
150 }
151
15a52f66 152 if (iterator->auto_seek_msgs) {
3d902f17
PP
153 while (!g_queue_is_empty(iterator->auto_seek_msgs)) {
154 bt_object_put_no_null_check(
155 g_queue_pop_tail(iterator->auto_seek_msgs));
15a52f66
PP
156 }
157
3d902f17 158 g_queue_free(iterator->auto_seek_msgs);
15a52f66
PP
159 iterator->auto_seek_msgs = NULL;
160 }
161
b09a5592 162 destroy_base_message_iterator(obj);
47e5a032
JG
163}
164
bd14d768 165BT_HIDDEN
904860cb 166void bt_self_component_port_input_message_iterator_try_finalize(
b09a5592 167 struct bt_self_component_port_input_message_iterator *iterator)
bd14d768 168{
834e9996
PP
169 typedef void (*method_t)(void *);
170
bd14d768 171 struct bt_component_class *comp_class = NULL;
834e9996 172 method_t method = NULL;
bd14d768 173
8b45963b 174 BT_ASSERT(iterator);
bd14d768
PP
175
176 switch (iterator->state) {
b09a5592 177 case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED:
dba1e5d8 178 /* Skip user finalization if user initialization failed */
b09a5592 179 BT_LIB_LOGD("Not finalizing non-initialized message iterator: "
834e9996 180 "%!+i", iterator);
904860cb 181 goto end;
b09a5592 182 case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED:
bd14d768 183 /* Already finalized */
b09a5592 184 BT_LIB_LOGD("Not finalizing message iterator: already finalized: "
834e9996 185 "%!+i", iterator);
904860cb
PP
186 goto end;
187 case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZING:
188 /* Already finalized */
189 BT_LIB_LOGF("Message iterator is already being finalized: "
190 "%!+i", iterator);
191 abort();
bd14d768
PP
192 default:
193 break;
194 }
195
b09a5592 196 BT_LIB_LOGD("Finalizing message iterator: %!+i", iterator);
904860cb
PP
197 set_self_comp_port_input_msg_iterator_state(iterator,
198 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZING);
8b45963b 199 BT_ASSERT(iterator->upstream_component);
bd14d768
PP
200 comp_class = iterator->upstream_component->class;
201
202 /* Call user-defined destroy method */
203 switch (comp_class->type) {
204 case BT_COMPONENT_CLASS_TYPE_SOURCE:
205 {
834e9996
PP
206 struct bt_component_class_source *src_comp_cls =
207 (void *) comp_class;
bd14d768 208
b09a5592 209 method = (method_t) src_comp_cls->methods.msg_iter_finalize;
bd14d768
PP
210 break;
211 }
212 case BT_COMPONENT_CLASS_TYPE_FILTER:
213 {
834e9996
PP
214 struct bt_component_class_filter *flt_comp_cls =
215 (void *) comp_class;
bd14d768 216
b09a5592 217 method = (method_t) flt_comp_cls->methods.msg_iter_finalize;
bd14d768
PP
218 break;
219 }
220 default:
221 /* Unreachable */
0fbb9a9f 222 abort();
bd14d768
PP
223 }
224
834e9996
PP
225 if (method) {
226 BT_LIB_LOGD("Calling user's finalization method: %!+i",
5af447e5 227 iterator);
834e9996 228 method(iterator);
bd14d768
PP
229 }
230
bd14d768
PP
231 iterator->upstream_component = NULL;
232 iterator->upstream_port = NULL;
904860cb
PP
233 set_self_comp_port_input_msg_iterator_state(iterator,
234 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED);
b09a5592 235 BT_LIB_LOGD("Finalized message iterator: %!+i", iterator);
904860cb
PP
236
237end:
238 return;
bd14d768
PP
239}
240
241BT_HIDDEN
b09a5592
PP
242void bt_self_component_port_input_message_iterator_set_connection(
243 struct bt_self_component_port_input_message_iterator *iterator,
bd14d768
PP
244 struct bt_connection *connection)
245{
8b45963b 246 BT_ASSERT(iterator);
bd14d768 247 iterator->connection = connection;
b09a5592 248 BT_LIB_LOGV("Set message iterator's connection: "
834e9996 249 "%![iter-]+i, %![conn-]+x", iterator, connection);
bd14d768
PP
250}
251
fe7265b5 252static
b09a5592
PP
253int init_message_iterator(struct bt_message_iterator *iterator,
254 enum bt_message_iterator_type type,
fe7265b5
PP
255 bt_object_release_func destroy)
256{
3fd7b79d
PP
257 int ret = 0;
258
1d7bf349 259 bt_object_init_shared(&iterator->base, destroy);
fe7265b5 260 iterator->type = type;
b09a5592
PP
261 iterator->msgs = g_ptr_array_new();
262 if (!iterator->msgs) {
3fd7b79d
PP
263 BT_LOGE_STR("Failed to allocate a GPtrArray.");
264 ret = -1;
265 goto end;
266 }
267
b09a5592 268 g_ptr_array_set_size(iterator->msgs, MSG_BATCH_SIZE);
3fd7b79d
PP
269
270end:
271 return ret;
fe7265b5
PP
272}
273
15a52f66
PP
274static
275bt_bool can_seek_ns_from_origin_true(
276 struct bt_self_component_port_input_message_iterator *iterator,
277 int64_t ns_from_origin)
278{
279 return BT_TRUE;
280}
281
282static
283bt_bool can_seek_beginning_true(
284 struct bt_self_component_port_input_message_iterator *iterator)
285{
286 return BT_TRUE;
287}
288
834e9996 289static
b09a5592
PP
290struct bt_self_component_port_input_message_iterator *
291bt_self_component_port_input_message_iterator_create_initial(
3230ee6b 292 struct bt_component *upstream_comp,
834e9996 293 struct bt_port *upstream_port)
47e5a032 294{
3fd7b79d 295 int ret;
b09a5592 296 struct bt_self_component_port_input_message_iterator *iterator = NULL;
47e5a032 297
8b45963b
PP
298 BT_ASSERT(upstream_comp);
299 BT_ASSERT(upstream_port);
8b45963b 300 BT_ASSERT(bt_port_is_connected(upstream_port));
b09a5592 301 BT_LIB_LOGD("Creating initial message iterator on self component input port: "
834e9996
PP
302 "%![up-comp-]+c, %![up-port-]+p", upstream_comp, upstream_port);
303 BT_ASSERT(bt_component_get_class_type(upstream_comp) ==
304 BT_COMPONENT_CLASS_TYPE_SOURCE ||
305 bt_component_get_class_type(upstream_comp) ==
306 BT_COMPONENT_CLASS_TYPE_FILTER);
307 iterator = g_new0(
b09a5592 308 struct bt_self_component_port_input_message_iterator, 1);
47e5a032 309 if (!iterator) {
834e9996 310 BT_LOGE_STR("Failed to allocate one self component input port "
b09a5592 311 "message iterator.");
73d5c1ad 312 goto end;
47e5a032
JG
313 }
314
b09a5592
PP
315 ret = init_message_iterator((void *) iterator,
316 BT_MESSAGE_ITERATOR_TYPE_SELF_COMPONENT_PORT_INPUT,
317 bt_self_component_port_input_message_iterator_destroy);
3fd7b79d 318 if (ret) {
b09a5592 319 /* init_message_iterator() logs errors */
834e9996 320 BT_OBJECT_PUT_REF_AND_RESET(iterator);
3fd7b79d
PP
321 goto end;
322 }
3230ee6b 323
3d902f17 324 iterator->auto_seek_msgs = g_queue_new();
15a52f66 325 if (!iterator->auto_seek_msgs) {
3d902f17 326 BT_LOGE_STR("Failed to allocate a GQueue.");
15a52f66 327 ret = -1;
73d5c1ad 328 goto end;
3230ee6b
PP
329 }
330
bd14d768
PP
331 iterator->upstream_component = upstream_comp;
332 iterator->upstream_port = upstream_port;
834e9996 333 iterator->connection = iterator->upstream_port->connection;
f7c3ac09 334 iterator->graph = bt_component_borrow_graph(upstream_comp);
904860cb
PP
335 set_self_comp_port_input_msg_iterator_state(iterator,
336 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED);
15a52f66
PP
337
338 switch (iterator->upstream_component->class->type) {
339 case BT_COMPONENT_CLASS_TYPE_SOURCE:
340 {
341 struct bt_component_class_source *src_comp_cls =
342 (void *) iterator->upstream_component->class;
343
344 iterator->methods.next =
345 (bt_self_component_port_input_message_iterator_next_method)
346 src_comp_cls->methods.msg_iter_next;
347 iterator->methods.seek_ns_from_origin =
348 (bt_self_component_port_input_message_iterator_seek_ns_from_origin_method)
349 src_comp_cls->methods.msg_iter_seek_ns_from_origin;
350 iterator->methods.seek_beginning =
351 (bt_self_component_port_input_message_iterator_seek_beginning_method)
352 src_comp_cls->methods.msg_iter_seek_beginning;
353 iterator->methods.can_seek_ns_from_origin =
354 (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method)
355 src_comp_cls->methods.msg_iter_can_seek_ns_from_origin;
356 iterator->methods.can_seek_beginning =
357 (bt_self_component_port_input_message_iterator_can_seek_beginning_method)
358 src_comp_cls->methods.msg_iter_can_seek_beginning;
359 break;
360 }
361 case BT_COMPONENT_CLASS_TYPE_FILTER:
362 {
363 struct bt_component_class_filter *flt_comp_cls =
364 (void *) iterator->upstream_component->class;
365
366 iterator->methods.next =
367 (bt_self_component_port_input_message_iterator_next_method)
368 flt_comp_cls->methods.msg_iter_next;
369 iterator->methods.seek_ns_from_origin =
370 (bt_self_component_port_input_message_iterator_seek_ns_from_origin_method)
371 flt_comp_cls->methods.msg_iter_seek_ns_from_origin;
372 iterator->methods.seek_beginning =
373 (bt_self_component_port_input_message_iterator_seek_beginning_method)
374 flt_comp_cls->methods.msg_iter_seek_beginning;
375 iterator->methods.can_seek_ns_from_origin =
376 (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method)
377 flt_comp_cls->methods.msg_iter_can_seek_ns_from_origin;
378 iterator->methods.can_seek_beginning =
379 (bt_self_component_port_input_message_iterator_can_seek_beginning_method)
380 flt_comp_cls->methods.msg_iter_can_seek_beginning;
381 break;
382 }
383 default:
384 abort();
385 }
386
387 if (iterator->methods.seek_ns_from_origin &&
388 !iterator->methods.can_seek_ns_from_origin) {
389 iterator->methods.can_seek_ns_from_origin =
390 (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method)
391 can_seek_ns_from_origin_true;
392 }
393
394 if (iterator->methods.seek_beginning &&
395 !iterator->methods.can_seek_beginning) {
396 iterator->methods.can_seek_beginning =
397 (bt_self_component_port_input_message_iterator_seek_beginning_method)
398 can_seek_beginning_true;
399 }
400
b09a5592 401 BT_LIB_LOGD("Created initial message iterator on self component input port: "
834e9996
PP
402 "%![up-port-]+p, %![up-comp-]+c, %![iter-]+i",
403 upstream_port, upstream_comp, iterator);
3230ee6b 404
47e5a032 405end:
834e9996 406 return iterator;
47e5a032
JG
407}
408
b09a5592
PP
409struct bt_self_component_port_input_message_iterator *
410bt_self_component_port_input_message_iterator_create(
834e9996 411 struct bt_self_component_port_input *self_port)
ea8d3e58 412{
b09a5592 413 typedef enum bt_self_message_iterator_status (*init_method_t)(
834e9996
PP
414 void *, void *, void *);
415
416 init_method_t init_method = NULL;
b09a5592 417 struct bt_self_component_port_input_message_iterator *iterator =
834e9996
PP
418 NULL;
419 struct bt_port *port = (void *) self_port;
420 struct bt_port *upstream_port;
421 struct bt_component *comp;
422 struct bt_component *upstream_comp;
423 struct bt_component_class *upstream_comp_cls;
424
425 BT_ASSERT_PRE_NON_NULL(port, "Port");
7b53201c 426 comp = bt_port_borrow_component_inline(port);
834e9996
PP
427 BT_ASSERT_PRE(bt_port_is_connected(port),
428 "Port is not connected: %![port-]+p", port);
429 BT_ASSERT_PRE(comp, "Port is not part of a component: %![port-]+p",
430 port);
431 BT_ASSERT_PRE(!bt_component_graph_is_canceled(comp),
432 "Port's component's graph is canceled: "
433 "%![port-]+p, %![comp-]+c", port, comp);
434 BT_ASSERT(port->connection);
435 upstream_port = port->connection->upstream_port;
436 BT_ASSERT(upstream_port);
7b53201c 437 upstream_comp = bt_port_borrow_component_inline(upstream_port);
834e9996 438 BT_ASSERT(upstream_comp);
1043fdea
PP
439 BT_ASSERT_PRE(
440 bt_component_borrow_graph(upstream_comp)->config_state !=
441 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
442 "Graph is not configured: %!+g",
443 bt_component_borrow_graph(upstream_comp));
834e9996
PP
444 upstream_comp_cls = upstream_comp->class;
445 BT_ASSERT(upstream_comp->class->type ==
446 BT_COMPONENT_CLASS_TYPE_SOURCE ||
447 upstream_comp->class->type ==
448 BT_COMPONENT_CLASS_TYPE_FILTER);
b09a5592 449 iterator = bt_self_component_port_input_message_iterator_create_initial(
834e9996
PP
450 upstream_comp, upstream_port);
451 if (!iterator) {
452 BT_LOGW_STR("Cannot create self component input port "
b09a5592 453 "message iterator.");
834e9996
PP
454 goto end;
455 }
890882ef 456
834e9996
PP
457 switch (upstream_comp_cls->type) {
458 case BT_COMPONENT_CLASS_TYPE_SOURCE:
459 {
460 struct bt_component_class_source *src_comp_cls =
461 (void *) upstream_comp_cls;
462
463 init_method =
b09a5592 464 (init_method_t) src_comp_cls->methods.msg_iter_init;
834e9996
PP
465 break;
466 }
467 case BT_COMPONENT_CLASS_TYPE_FILTER:
468 {
469 struct bt_component_class_filter *flt_comp_cls =
470 (void *) upstream_comp_cls;
471
472 init_method =
b09a5592 473 (init_method_t) flt_comp_cls->methods.msg_iter_init;
834e9996
PP
474 break;
475 }
476 default:
477 /* Unreachable */
478 abort();
479 }
480
481 if (init_method) {
482 int iter_status;
483
484 BT_LIB_LOGD("Calling user's initialization method: %!+i", iterator);
485 iter_status = init_method(iterator, upstream_comp,
486 upstream_port);
487 BT_LOGD("User method returned: status=%s",
b09a5592
PP
488 bt_message_iterator_status_string(iter_status));
489 if (iter_status != BT_MESSAGE_ITERATOR_STATUS_OK) {
834e9996 490 BT_LOGW_STR("Initialization method failed.");
904860cb 491 BT_OBJECT_PUT_REF_AND_RESET(iterator);
834e9996
PP
492 goto end;
493 }
494 }
495
904860cb
PP
496 set_self_comp_port_input_msg_iterator_state(iterator,
497 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE);
834e9996 498 g_ptr_array_add(port->connection->iterators, iterator);
b09a5592 499 BT_LIB_LOGD("Created message iterator on self component input port: "
834e9996
PP
500 "%![up-port-]+p, %![up-comp-]+c, %![iter-]+i",
501 upstream_port, upstream_comp, iterator);
502
503end:
504 return iterator;
ea8d3e58
JG
505}
506
b09a5592
PP
507void *bt_self_message_iterator_get_data(
508 const struct bt_self_message_iterator *self_iterator)
ea8d3e58 509{
b09a5592 510 struct bt_self_component_port_input_message_iterator *iterator =
834e9996 511 (void *) self_iterator;
ea8d3e58 512
b09a5592 513 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
834e9996 514 return iterator->user_data;
8738a040 515}
413bc2c4 516
b09a5592
PP
517void bt_self_message_iterator_set_data(
518 struct bt_self_message_iterator *self_iterator, void *data)
f7c3ac09 519{
b09a5592 520 struct bt_self_component_port_input_message_iterator *iterator =
834e9996 521 (void *) self_iterator;
f7c3ac09 522
b09a5592 523 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
834e9996 524 iterator->user_data = data;
b09a5592 525 BT_LIB_LOGV("Set message iterator's user data: "
834e9996 526 "%!+i, user-data-addr=%p", iterator, data);
f7c3ac09
PP
527}
528
6ff151ad
PP
529BT_ASSERT_PRE_FUNC
530static inline
b09a5592 531void bt_message_borrow_packet_stream(const struct bt_message *msg,
78cf9df6
PP
532 const struct bt_stream **stream,
533 const struct bt_packet **packet)
fa054faf 534{
b09a5592 535 BT_ASSERT(msg);
fa054faf 536
b09a5592
PP
537 switch (msg->type) {
538 case BT_MESSAGE_TYPE_EVENT:
78cf9df6 539 *packet = bt_event_borrow_packet_const(
b09a5592 540 bt_message_event_borrow_event_const(msg));
78cf9df6 541 *stream = bt_packet_borrow_stream_const(*packet);
fa054faf 542 break;
b09a5592
PP
543 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
544 *stream = bt_message_stream_beginning_borrow_stream_const(msg);
fa054faf 545 break;
b09a5592
PP
546 case BT_MESSAGE_TYPE_STREAM_END:
547 *stream = bt_message_stream_end_borrow_stream_const(msg);
fa054faf 548 break;
b09a5592
PP
549 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
550 *packet = bt_message_packet_beginning_borrow_packet_const(msg);
78cf9df6 551 *stream = bt_packet_borrow_stream_const(*packet);
fa054faf 552 break;
b09a5592
PP
553 case BT_MESSAGE_TYPE_PACKET_END:
554 *packet = bt_message_packet_end_borrow_packet_const(msg);
78cf9df6 555 *stream = bt_packet_borrow_stream_const(*packet);
b04a393b 556 break;
fa054faf 557 default:
6ff151ad 558 break;
fa054faf 559 }
fa054faf
PP
560}
561
b09a5592
PP
562enum bt_message_iterator_status
563bt_self_component_port_input_message_iterator_next(
564 struct bt_self_component_port_input_message_iterator *iterator,
565 bt_message_array_const *msgs, uint64_t *user_count)
3230ee6b 566{
b09a5592 567 int status = BT_MESSAGE_ITERATOR_STATUS_OK;
834e9996 568
b09a5592
PP
569 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
570 BT_ASSERT_PRE_NON_NULL(msgs, "Message array (output)");
571 BT_ASSERT_PRE_NON_NULL(user_count, "Message count (output)");
6ff151ad 572 BT_ASSERT_PRE(iterator->state ==
b09a5592
PP
573 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE,
574 "Message iterator's \"next\" called, but "
15a52f66 575 "message iterator is in the wrong state: %!+i", iterator);
6ff151ad
PP
576 BT_ASSERT(iterator->upstream_component);
577 BT_ASSERT(iterator->upstream_component->class);
1043fdea
PP
578 BT_ASSERT_PRE(
579 bt_component_borrow_graph(iterator->upstream_component)->config_state !=
580 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
9cce94e4
PP
581 "Graph is not configured: %!+g",
582 bt_component_borrow_graph(iterator->upstream_component));
834e9996 583 BT_LIB_LOGD("Getting next self component input port "
b09a5592 584 "message iterator's messages: %!+i", iterator);
d3eb6e8f 585
3230ee6b 586 /*
b09a5592 587 * Call the user's "next" method to get the next messages
fa054faf 588 * and status.
3230ee6b 589 */
15a52f66 590 BT_ASSERT(iterator->methods.next);
6ff151ad 591 BT_LOGD_STR("Calling user's \"next\" method.");
15a52f66
PP
592 status = iterator->methods.next(iterator,
593 (void *) iterator->base.msgs->pdata, MSG_BATCH_SIZE,
594 user_count);
6ff151ad 595 BT_LOGD("User method returned: status=%s",
b09a5592 596 bt_message_iterator_status_string(status));
3fd7b79d 597 if (status < 0) {
6ff151ad 598 BT_LOGW_STR("User method failed.");
6ff151ad
PP
599 goto end;
600 }
3230ee6b 601
904860cb
PP
602#ifdef BT_DEV_MODE
603 /*
604 * There is no way that this iterator could have been finalized
605 * during its "next" method, as the only way to do this is to
606 * put the last iterator's reference, and this can only be done
607 * by its downstream owner.
15a52f66
PP
608 *
609 * For the same reason, there is no way that this iterator could
610 * have seeked (cannot seek a self message iterator).
904860cb
PP
611 */
612 BT_ASSERT(iterator->state ==
613 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE);
614#endif
8cf27cc5 615
3fd7b79d 616 switch (status) {
b09a5592 617 case BT_MESSAGE_ITERATOR_STATUS_OK:
15a52f66
PP
618 BT_ASSERT_PRE(*user_count <= MSG_BATCH_SIZE,
619 "Invalid returned message count: greater than "
620 "batch size: count=%" PRIu64 ", batch-size=%u",
621 *user_count, MSG_BATCH_SIZE);
b09a5592 622 *msgs = (void *) iterator->base.msgs->pdata;
3fd7b79d 623 break;
b09a5592 624 case BT_MESSAGE_ITERATOR_STATUS_AGAIN:
3fd7b79d 625 goto end;
b09a5592 626 case BT_MESSAGE_ITERATOR_STATUS_END:
904860cb
PP
627 set_self_comp_port_input_msg_iterator_state(iterator,
628 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED);
6ff151ad 629 goto end;
6ff151ad
PP
630 default:
631 /* Unknown non-error status */
632 abort();
41a2b7ae
PP
633 }
634
635end:
3230ee6b
PP
636 return status;
637}
638
904860cb 639enum bt_message_iterator_status bt_port_output_message_iterator_next(
b09a5592
PP
640 struct bt_port_output_message_iterator *iterator,
641 bt_message_array_const *msgs_to_user,
3fd7b79d 642 uint64_t *count_to_user)
3230ee6b 643{
b09a5592 644 enum bt_message_iterator_status status;
94a96686 645 enum bt_graph_status graph_status;
3230ee6b 646
b09a5592
PP
647 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
648 BT_ASSERT_PRE_NON_NULL(msgs_to_user, "Message array (output)");
649 BT_ASSERT_PRE_NON_NULL(count_to_user, "Message count (output)");
650 BT_LIB_LOGD("Getting next output port message iterator's messages: "
94a96686 651 "%!+i", iterator);
834e9996
PP
652 graph_status = bt_graph_consume_sink_no_check(iterator->graph,
653 iterator->colander);
94a96686
PP
654 switch (graph_status) {
655 case BT_GRAPH_STATUS_CANCELED:
94a96686 656 case BT_GRAPH_STATUS_AGAIN:
94a96686 657 case BT_GRAPH_STATUS_END:
94a96686 658 case BT_GRAPH_STATUS_NOMEM:
834e9996 659 status = (int) graph_status;
94a96686
PP
660 break;
661 case BT_GRAPH_STATUS_OK:
b09a5592 662 status = BT_MESSAGE_ITERATOR_STATUS_OK;
3fd7b79d
PP
663
664 /*
b09a5592 665 * On success, the colander sink moves the messages
3fd7b79d 666 * to this iterator's array and sets this iterator's
b09a5592 667 * message count: move them to the user.
3fd7b79d 668 */
b09a5592 669 *msgs_to_user = (void *) iterator->base.msgs->pdata;
834e9996 670 *count_to_user = iterator->count;
fe7265b5 671 break;
fe7265b5 672 default:
94a96686 673 /* Other errors */
b09a5592 674 status = BT_MESSAGE_ITERATOR_STATUS_ERROR;
fe7265b5 675 }
3230ee6b 676
3230ee6b 677 return status;
53d45b87
JG
678}
679
15a52f66
PP
680struct bt_component *
681bt_self_component_port_input_message_iterator_borrow_component(
b09a5592 682 struct bt_self_component_port_input_message_iterator *iterator)
834e9996 683{
b09a5592 684 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
834e9996
PP
685 return iterator->upstream_component;
686}
687
15a52f66
PP
688const struct bt_component *
689bt_self_component_port_input_message_iterator_borrow_component_const(
690 const struct bt_self_component_port_input_message_iterator *iterator)
691{
692 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
693 return iterator->upstream_component;
694}
695
b09a5592
PP
696struct bt_self_component *bt_self_message_iterator_borrow_component(
697 struct bt_self_message_iterator *self_iterator)
413bc2c4 698{
b09a5592 699 struct bt_self_component_port_input_message_iterator *iterator =
834e9996 700 (void *) self_iterator;
fe7265b5 701
b09a5592 702 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
834e9996 703 return (void *) iterator->upstream_component;
413bc2c4
JG
704}
705
b09a5592
PP
706struct bt_self_port_output *bt_self_message_iterator_borrow_port(
707 struct bt_self_message_iterator *self_iterator)
91457551 708{
b09a5592 709 struct bt_self_component_port_input_message_iterator *iterator =
834e9996
PP
710 (void *) self_iterator;
711
b09a5592 712 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
834e9996 713 return (void *) iterator->upstream_port;
91457551 714}
c3ac0193
PP
715
716static
b09a5592 717void bt_port_output_message_iterator_destroy(struct bt_object *obj)
c3ac0193 718{
b09a5592 719 struct bt_port_output_message_iterator *iterator = (void *) obj;
c3ac0193 720
b09a5592 721 BT_LIB_LOGD("Destroying output port message iterator object: %!+i",
c3ac0193
PP
722 iterator);
723 BT_LOGD_STR("Putting graph.");
834e9996 724 BT_OBJECT_PUT_REF_AND_RESET(iterator->graph);
c3ac0193 725 BT_LOGD_STR("Putting colander sink component.");
834e9996 726 BT_OBJECT_PUT_REF_AND_RESET(iterator->colander);
b09a5592 727 destroy_base_message_iterator(obj);
c3ac0193
PP
728}
729
b09a5592 730struct bt_port_output_message_iterator *
15a52f66 731bt_port_output_message_iterator_create(struct bt_graph *graph,
7b53201c 732 const struct bt_port_output *output_port)
c3ac0193 733{
b09a5592 734 struct bt_port_output_message_iterator *iterator = NULL;
834e9996 735 struct bt_component_class_sink *colander_comp_cls = NULL;
c3ac0193 736 struct bt_component *output_port_comp = NULL;
834e9996 737 struct bt_component_sink *colander_comp;
c3ac0193 738 enum bt_graph_status graph_status;
834e9996 739 struct bt_port_input *colander_in_port = NULL;
c3ac0193 740 struct bt_component_class_sink_colander_data colander_data;
3fd7b79d 741 int ret;
c3ac0193 742
834e9996 743 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
6ff151ad 744 BT_ASSERT_PRE_NON_NULL(output_port, "Output port");
7b53201c
PP
745 output_port_comp = bt_port_borrow_component_inline(
746 (const void *) output_port);
6ff151ad
PP
747 BT_ASSERT_PRE(output_port_comp,
748 "Output port has no component: %!+p", output_port);
834e9996
PP
749 BT_ASSERT_PRE(bt_component_borrow_graph(output_port_comp) ==
750 (void *) graph,
751 "Output port is not part of graph: %![graph-]+g, %![port-]+p",
752 graph, output_port);
12c700be
PP
753 BT_ASSERT_PRE(!graph->has_sink,
754 "Graph already has a sink component: %![graph-]+g");
c3ac0193 755
b09a5592
PP
756 /* Create message iterator */
757 BT_LIB_LOGD("Creating message iterator on output port: "
834e9996 758 "%![port-]+p, %![comp-]+c", output_port, output_port_comp);
b09a5592 759 iterator = g_new0(struct bt_port_output_message_iterator, 1);
c3ac0193 760 if (!iterator) {
b09a5592 761 BT_LOGE_STR("Failed to allocate one output port message iterator.");
c3ac0193
PP
762 goto error;
763 }
764
b09a5592
PP
765 ret = init_message_iterator((void *) iterator,
766 BT_MESSAGE_ITERATOR_TYPE_PORT_OUTPUT,
767 bt_port_output_message_iterator_destroy);
3fd7b79d 768 if (ret) {
b09a5592 769 /* init_message_iterator() logs errors */
8138bfe1 770 BT_OBJECT_PUT_REF_AND_RESET(iterator);
3fd7b79d
PP
771 goto end;
772 }
c3ac0193
PP
773
774 /* Create colander component */
775 colander_comp_cls = bt_component_class_sink_colander_get();
776 if (!colander_comp_cls) {
777 BT_LOGW("Cannot get colander sink component class.");
778 goto error;
779 }
780
4b70020d
PP
781 iterator->graph = graph;
782 bt_object_get_no_null_check(iterator->graph);
b09a5592 783 colander_data.msgs = (void *) iterator->base.msgs->pdata;
3fd7b79d 784 colander_data.count_addr = &iterator->count;
a9de0e7a
PP
785
786 /* Hope that nobody uses this very unique name */
834e9996 787 graph_status =
7b53201c 788 bt_graph_add_sink_component_with_init_method_data(
a9de0e7a
PP
789 (void *) graph, colander_comp_cls,
790 "colander-36ac3409-b1a8-4d60-ab1f-4fdf341a8fb1",
7b53201c 791 NULL, &colander_data, (void *) &iterator->colander);
c3ac0193 792 if (graph_status != BT_GRAPH_STATUS_OK) {
834e9996
PP
793 BT_LIB_LOGW("Cannot add colander sink component to graph: "
794 "%1[graph-]+g, status=%s", graph,
c3ac0193
PP
795 bt_graph_status_string(graph_status));
796 goto error;
797 }
798
799 /*
800 * Connect provided output port to the colander component's
801 * input port.
802 */
7b53201c
PP
803 colander_in_port =
804 (void *) bt_component_sink_borrow_input_port_by_index_const(
805 (void *) iterator->colander, 0);
8b45963b 806 BT_ASSERT(colander_in_port);
7b53201c 807 graph_status = bt_graph_connect_ports(graph,
c3ac0193
PP
808 output_port, colander_in_port, NULL);
809 if (graph_status != BT_GRAPH_STATUS_OK) {
834e9996
PP
810 BT_LIB_LOGW("Cannot add colander sink component to graph: "
811 "%![graph-]+g, %![comp-]+c, status=%s", graph,
812 iterator->colander,
c3ac0193
PP
813 bt_graph_status_string(graph_status));
814 goto error;
815 }
816
817 /*
818 * At this point everything went fine. Make the graph
b09a5592 819 * nonconsumable forever so that only this message iterator
c3ac0193 820 * can consume (thanks to bt_graph_consume_sink_no_check()).
b09a5592
PP
821 * This avoids leaking the message created by the colander
822 * sink and moved to the message iterator's message
94a96686 823 * member.
c3ac0193 824 */
834e9996 825 bt_graph_set_can_consume(iterator->graph, false);
1043fdea
PP
826
827 /*
828 * Also set the graph as being configured: it has no active sink
829 * anyway, so we don't need to call bt_graph_configure().
830 */
831 graph->config_state = BT_GRAPH_CONFIGURATION_STATE_CONFIGURED;
c3ac0193
PP
832 goto end;
833
834error:
835 if (iterator && iterator->graph && iterator->colander) {
836 int ret;
837
838 /* Remove created colander component from graph if any */
839 colander_comp = iterator->colander;
8138bfe1 840 BT_OBJECT_PUT_REF_AND_RESET(iterator->colander);
c3ac0193
PP
841
842 /*
843 * At this point the colander component's reference
844 * count is 0 because iterator->colander was the only
845 * owner. We also know that it is not connected because
846 * this is the last operation before this function
847 * succeeds.
848 *
849 * Since we honor the preconditions here,
850 * bt_graph_remove_unconnected_component() always
851 * succeeds.
852 */
853 ret = bt_graph_remove_unconnected_component(iterator->graph,
834e9996 854 (void *) colander_comp);
8b45963b 855 BT_ASSERT(ret == 0);
c3ac0193
PP
856 }
857
8138bfe1 858 BT_OBJECT_PUT_REF_AND_RESET(iterator);
c3ac0193
PP
859
860end:
8138bfe1 861 bt_object_put_ref(colander_comp_cls);
c3ac0193
PP
862 return (void *) iterator;
863}
8c6884d9 864
15a52f66
PP
865bt_bool bt_self_component_port_input_message_iterator_can_seek_ns_from_origin(
866 struct bt_self_component_port_input_message_iterator *iterator,
867 int64_t ns_from_origin)
868{
869 bt_bool can = BT_FALSE;
870
871 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
872 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator);
1043fdea
PP
873 BT_ASSERT_PRE(
874 bt_component_borrow_graph(iterator->upstream_component)->config_state !=
875 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
15a52f66
PP
876 "Graph is not configured: %!+g",
877 bt_component_borrow_graph(iterator->upstream_component));
878
879 if (iterator->methods.can_seek_ns_from_origin) {
880 can = iterator->methods.can_seek_ns_from_origin(iterator,
881 ns_from_origin);
882 goto end;
883 }
884
885 /*
886 * Automatic seeking fall back: if we can seek to the beginning,
887 * then we can automatically seek to any message.
888 */
889 if (iterator->methods.can_seek_beginning) {
890 can = iterator->methods.can_seek_beginning(iterator);
891 }
892
893end:
894 return can;
895}
896
897bt_bool bt_self_component_port_input_message_iterator_can_seek_beginning(
898 struct bt_self_component_port_input_message_iterator *iterator)
899{
900 bt_bool can = BT_FALSE;
901
902 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
903 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator);
1043fdea
PP
904 BT_ASSERT_PRE(
905 bt_component_borrow_graph(iterator->upstream_component)->config_state !=
906 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
15a52f66
PP
907 "Graph is not configured: %!+g",
908 bt_component_borrow_graph(iterator->upstream_component));
909
910 if (iterator->methods.can_seek_beginning) {
911 can = iterator->methods.can_seek_beginning(iterator);
912 }
913
914 return can;
915}
916
917static inline
918void _set_iterator_state_after_seeking(
919 struct bt_self_component_port_input_message_iterator *iterator,
920 enum bt_message_iterator_status status)
921{
922 enum bt_self_component_port_input_message_iterator_state new_state = 0;
923
924 /* Set iterator's state depending on seeking status */
925 switch (status) {
926 case BT_MESSAGE_ITERATOR_STATUS_OK:
927 new_state = BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE;
928 break;
929 case BT_MESSAGE_ITERATOR_STATUS_AGAIN:
930 new_state = BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN;
931 break;
932 case BT_MESSAGE_ITERATOR_STATUS_ERROR:
933 case BT_MESSAGE_ITERATOR_STATUS_NOMEM:
934 new_state = BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR;
935 break;
936 case BT_MESSAGE_ITERATOR_STATUS_END:
937 new_state = BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED;
938 break;
939 default:
940 abort();
941 }
942
943 set_self_comp_port_input_msg_iterator_state(iterator, new_state);
944}
945
946#ifdef BT_DEV_MODE
947# define set_iterator_state_after_seeking _set_iterator_state_after_seeking
948#else
949# define set_iterator_state_after_seeking(_iter, _status)
950#endif
951
952enum bt_message_iterator_status
953bt_self_component_port_input_message_iterator_seek_beginning(
954 struct bt_self_component_port_input_message_iterator *iterator)
955{
956 int status;
957
958 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
959 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator);
1043fdea
PP
960 BT_ASSERT_PRE(
961 bt_component_borrow_graph(iterator->upstream_component)->config_state !=
962 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
15a52f66
PP
963 "Graph is not configured: %!+g",
964 bt_component_borrow_graph(iterator->upstream_component));
965 BT_ASSERT_PRE(
966 bt_self_component_port_input_message_iterator_can_seek_beginning(
967 iterator),
968 "Message iterator cannot seek beginning: %!+i", iterator);
969 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i", iterator);
970 set_self_comp_port_input_msg_iterator_state(iterator,
971 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_SEEKING);
972 status = iterator->methods.seek_beginning(iterator);
973 BT_LOGD("User method returned: status=%s",
974 bt_message_iterator_status_string(status));
975 BT_ASSERT_PRE(status == BT_MESSAGE_ITERATOR_STATUS_OK ||
976 status == BT_MESSAGE_ITERATOR_STATUS_ERROR ||
977 status == BT_MESSAGE_ITERATOR_STATUS_NOMEM ||
978 status == BT_MESSAGE_ITERATOR_STATUS_AGAIN,
979 "Unexpected status: %![iter-]+i, status=%s",
d47c10dc 980 iterator, bt_common_self_message_iterator_status_string(status));
15a52f66
PP
981 set_iterator_state_after_seeking(iterator, status);
982 return status;
983}
984
985static inline
3d902f17
PP
986enum bt_message_iterator_status auto_seek_handle_message(
987 struct bt_self_component_port_input_message_iterator *iterator,
988 int64_t ns_from_origin, const struct bt_message *msg,
989 bool *got_first)
15a52f66 990{
3d902f17
PP
991 enum bt_message_iterator_status status = BT_MESSAGE_ITERATOR_STATUS_OK;
992 int64_t msg_ns_from_origin;
15a52f66 993 const struct bt_clock_snapshot *clk_snapshot = NULL;
3d902f17
PP
994 int ret;
995
996 BT_ASSERT(msg);
997 BT_ASSERT(got_first);
15a52f66
PP
998
999 switch (msg->type) {
1000 case BT_MESSAGE_TYPE_EVENT:
1001 {
1002 const struct bt_message_event *event_msg =
1003 (const void *) msg;
1004
9c04b633 1005 clk_snapshot = event_msg->default_cs;
15a52f66 1006 BT_ASSERT_PRE(clk_snapshot,
956dcc1e
PP
1007 "Event message has no default clock snapshot: %!+n",
1008 event_msg);
15a52f66
PP
1009 break;
1010 }
40bf6fd0 1011 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY:
15a52f66 1012 {
40bf6fd0 1013 const struct bt_message_message_iterator_inactivity *inactivity_msg =
15a52f66
PP
1014 (const void *) msg;
1015
15a52f66 1016 clk_snapshot = inactivity_msg->default_cs;
f629b891 1017 BT_ASSERT(clk_snapshot);
15a52f66
PP
1018 break;
1019 }
f629b891
PP
1020 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
1021 case BT_MESSAGE_TYPE_PACKET_END:
956dcc1e
PP
1022 {
1023 const struct bt_message_packet *packet_msg =
1024 (const void *) msg;
1025
1026 clk_snapshot = packet_msg->default_cs;
1027 BT_ASSERT_PRE(clk_snapshot,
1028 "Packet message has no default clock snapshot: %!+n",
1029 packet_msg);
1030 break;
1031 }
f629b891
PP
1032 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
1033 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
15a52f66 1034 {
3d902f17
PP
1035 struct bt_message_discarded_items *msg_disc_items =
1036 (void *) msg;
1037
1038 BT_ASSERT_PRE(msg_disc_items->default_begin_cs &&
1039 msg_disc_items->default_end_cs,
1040 "Discarded events/packets message has no default clock snapshots: %!+n",
1041 msg_disc_items);
1042 ret = bt_clock_snapshot_get_ns_from_origin(
1043 msg_disc_items->default_begin_cs,
1044 &msg_ns_from_origin);
1045 if (ret) {
1046 status = BT_MESSAGE_ITERATOR_STATUS_ERROR;
1047 goto end;
1048 }
15a52f66 1049
3d902f17
PP
1050 if (msg_ns_from_origin >= ns_from_origin) {
1051 *got_first = true;
1052 goto push_msg;
1053 }
1054
1055 ret = bt_clock_snapshot_get_ns_from_origin(
1056 msg_disc_items->default_end_cs,
1057 &msg_ns_from_origin);
1058 if (ret) {
1059 status = BT_MESSAGE_ITERATOR_STATUS_ERROR;
1060 goto end;
1061 }
1062
1063 if (msg_ns_from_origin >= ns_from_origin) {
1064 /*
1065 * The discarded items message's beginning time
1066 * is before the requested seeking time, but its
1067 * end time is after. Modify the message so as
1068 * to set its beginning time to the requested
1069 * seeking time, and make its item count unknown
1070 * as we don't know if items were really
1071 * discarded within the new time range.
1072 */
1073 uint64_t new_begin_raw_value;
1074
1075 ret = bt_clock_class_clock_value_from_ns_from_origin(
1076 msg_disc_items->default_end_cs->clock_class,
1077 ns_from_origin, &new_begin_raw_value);
1078 if (ret) {
1079 status = BT_MESSAGE_ITERATOR_STATUS_ERROR;
1080 goto end;
1081 }
1082
1083 bt_clock_snapshot_set_raw_value(
1084 msg_disc_items->default_begin_cs,
1085 new_begin_raw_value);
1086 msg_disc_items->count.base.avail =
1087 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE;
1088
1089 /*
1090 * It is safe to push it because its beginning
1091 * time is exactly the requested seeking time.
1092 */
1093 goto push_msg;
1094 } else {
1095 goto skip_msg;
1096 }
15a52f66 1097 }
f629b891 1098 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING:
15a52f66 1099 {
f629b891 1100 const struct bt_message_stream_activity *stream_act_msg =
15a52f66
PP
1101 (const void *) msg;
1102
f629b891
PP
1103 switch (stream_act_msg->default_cs_state) {
1104 case BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_UNKNOWN:
1105 case BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_INFINITE:
1106 /*
1107 * -inf is always less than any requested time,
1108 * and we can't assume any specific time for an
1109 * unknown clock snapshot, so skip this.
1110 */
3d902f17 1111 goto skip_msg;
f629b891
PP
1112 case BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_KNOWN:
1113 clk_snapshot = stream_act_msg->default_cs;
1114 BT_ASSERT(clk_snapshot);
1115 break;
1116 default:
1117 abort();
1118 }
1119
15a52f66
PP
1120 break;
1121 }
f629b891
PP
1122 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_END:
1123 {
1124 const struct bt_message_stream_activity *stream_act_msg =
1125 (const void *) msg;
1126
1127 switch (stream_act_msg->default_cs_state) {
1128 case BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_UNKNOWN:
1129 /*
1130 * We can't assume any specific time for an
1131 * unknown clock snapshot, so skip this.
1132 */
3d902f17 1133 goto skip_msg;
f629b891
PP
1134 case BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_INFINITE:
1135 /*
1136 * +inf is always greater than any requested
1137 * time.
1138 */
3d902f17
PP
1139 *got_first = true;
1140 goto push_msg;
f629b891
PP
1141 case BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_KNOWN:
1142 clk_snapshot = stream_act_msg->default_cs;
1143 BT_ASSERT(clk_snapshot);
1144 break;
1145 default:
1146 abort();
1147 }
1148
1149 break;
1150 }
1151 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
1152 case BT_MESSAGE_TYPE_STREAM_END:
1153 /* Ignore */
3d902f17 1154 goto skip_msg;
15a52f66
PP
1155 default:
1156 abort();
1157 }
1158
3d902f17
PP
1159 BT_ASSERT(clk_snapshot);
1160 ret = bt_clock_snapshot_get_ns_from_origin(clk_snapshot,
1161 &msg_ns_from_origin);
1162 if (ret) {
1163 status = BT_MESSAGE_ITERATOR_STATUS_ERROR;
15a52f66
PP
1164 goto end;
1165 }
1166
3d902f17
PP
1167 if (msg_ns_from_origin >= ns_from_origin) {
1168 *got_first = true;
1169 goto push_msg;
1170 }
1171
1172skip_msg:
1173 bt_object_put_no_null_check(msg);
27afdfda 1174 msg = NULL;
3d902f17
PP
1175 goto end;
1176
1177push_msg:
1178 g_queue_push_head(iterator->auto_seek_msgs, (void *) msg);
1179 msg = NULL;
15a52f66
PP
1180
1181end:
3d902f17
PP
1182 BT_ASSERT(!msg || status != BT_MESSAGE_ITERATOR_STATUS_OK);
1183 return status;
15a52f66
PP
1184}
1185
1186static
1187enum bt_message_iterator_status find_message_ge_ns_from_origin(
1188 struct bt_self_component_port_input_message_iterator *iterator,
1189 int64_t ns_from_origin)
1190{
1191 int status;
1192 enum bt_self_component_port_input_message_iterator_state init_state =
1193 iterator->state;
1194 const struct bt_message *messages[MSG_BATCH_SIZE];
1195 uint64_t user_count = 0;
1196 uint64_t i;
3d902f17 1197 bool got_first = false;
15a52f66
PP
1198
1199 BT_ASSERT(iterator);
1200 memset(&messages[0], 0, sizeof(messages[0]) * MSG_BATCH_SIZE);
1201
1202 /*
1203 * Make this iterator temporarily active (not seeking) to call
1204 * the "next" method.
1205 */
1206 set_self_comp_port_input_msg_iterator_state(iterator,
1207 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE);
1208
1209 BT_ASSERT(iterator->methods.next);
1210
c980433a 1211 while (!got_first) {
15a52f66
PP
1212 /*
1213 * Call the user's "next" method to get the next
1214 * messages and status.
1215 */
1216 BT_LOGD_STR("Calling user's \"next\" method.");
1217 status = iterator->methods.next(iterator,
1218 &messages[0], MSG_BATCH_SIZE, &user_count);
1219 BT_LOGD("User method returned: status=%s",
1220 bt_message_iterator_status_string(status));
1221
1222#ifdef BT_DEV_MODE
1223 /*
1224 * The user's "next" method must not do any action which
1225 * would change the iterator's state.
1226 */
1227 BT_ASSERT(iterator->state ==
1228 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE);
1229#endif
1230
1231 switch (status) {
1232 case BT_MESSAGE_ITERATOR_STATUS_OK:
1233 BT_ASSERT_PRE(user_count <= MSG_BATCH_SIZE,
1234 "Invalid returned message count: greater than "
1235 "batch size: count=%" PRIu64 ", batch-size=%u",
1236 user_count, MSG_BATCH_SIZE);
1237 break;
1238 case BT_MESSAGE_ITERATOR_STATUS_AGAIN:
1239 case BT_MESSAGE_ITERATOR_STATUS_ERROR:
1240 case BT_MESSAGE_ITERATOR_STATUS_NOMEM:
1241 case BT_MESSAGE_ITERATOR_STATUS_END:
1242 goto end;
1243 default:
1244 abort();
1245 }
1246
15a52f66 1247 for (i = 0; i < user_count; i++) {
3d902f17
PP
1248 if (got_first) {
1249 g_queue_push_head(iterator->auto_seek_msgs,
1250 (void *) messages[i]);
1251 messages[i] = NULL;
15a52f66
PP
1252 continue;
1253 }
1254
3d902f17
PP
1255 status = auto_seek_handle_message(iterator,
1256 ns_from_origin, messages[i], &got_first);
1257 if (status == BT_MESSAGE_ITERATOR_STATUS_OK) {
c980433a 1258 /* Message was either pushed or moved */
3d902f17
PP
1259 messages[i] = NULL;
1260 } else {
15a52f66
PP
1261 goto end;
1262 }
15a52f66
PP
1263 }
1264 }
1265
1266end:
1267 for (i = 0; i < user_count; i++) {
1268 if (messages[i]) {
1269 bt_object_put_no_null_check(messages[i]);
1270 }
1271 }
1272
1273 set_self_comp_port_input_msg_iterator_state(iterator, init_state);
1274 return status;
1275}
1276
1277static
1278enum bt_self_message_iterator_status post_auto_seek_next(
1279 struct bt_self_component_port_input_message_iterator *iterator,
1280 bt_message_array_const msgs, uint64_t capacity,
1281 uint64_t *count)
1282{
3d902f17
PP
1283 BT_ASSERT(!g_queue_is_empty(iterator->auto_seek_msgs));
1284 *count = 0;
15a52f66
PP
1285
1286 /*
1287 * Move auto-seek messages to the output array (which is this
3d902f17 1288 * iterator's base message array).
15a52f66 1289 */
3d902f17
PP
1290 while (capacity > 0 && !g_queue_is_empty(iterator->auto_seek_msgs)) {
1291 msgs[*count] = g_queue_pop_tail(iterator->auto_seek_msgs);
1292 capacity--;
1293 (*count)++;
15a52f66 1294 }
15a52f66 1295
3d902f17
PP
1296 BT_ASSERT(*count > 0);
1297
1298 if (g_queue_is_empty(iterator->auto_seek_msgs)) {
1299 /* No more auto-seek messages */
1300 switch (iterator->upstream_component->class->type) {
1301 case BT_COMPONENT_CLASS_TYPE_SOURCE:
1302 {
1303 struct bt_component_class_source *src_comp_cls =
1304 (void *) iterator->upstream_component->class;
1305
1306 iterator->methods.next =
1307 (bt_self_component_port_input_message_iterator_next_method)
1308 src_comp_cls->methods.msg_iter_next;
1309 break;
1310 }
1311 case BT_COMPONENT_CLASS_TYPE_FILTER:
1312 {
1313 struct bt_component_class_filter *flt_comp_cls =
1314 (void *) iterator->upstream_component->class;
1315
1316 iterator->methods.next =
1317 (bt_self_component_port_input_message_iterator_next_method)
1318 flt_comp_cls->methods.msg_iter_next;
1319 break;
1320 }
1321 default:
1322 abort();
1323 }
15a52f66
PP
1324 }
1325
1326 return BT_SELF_MESSAGE_ITERATOR_STATUS_OK;
1327}
1328
1329enum bt_message_iterator_status
1330bt_self_component_port_input_message_iterator_seek_ns_from_origin(
1331 struct bt_self_component_port_input_message_iterator *iterator,
1332 int64_t ns_from_origin)
1333{
1334 int status;
1335
1336 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
1337 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator);
1043fdea
PP
1338 BT_ASSERT_PRE(
1339 bt_component_borrow_graph(iterator->upstream_component)->config_state !=
1340 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
15a52f66
PP
1341 "Graph is not configured: %!+g",
1342 bt_component_borrow_graph(iterator->upstream_component));
1343 BT_ASSERT_PRE(
1344 bt_self_component_port_input_message_iterator_can_seek_ns_from_origin(
1345 iterator, ns_from_origin),
1346 "Message iterator cannot seek nanoseconds from origin: %!+i, "
1347 "ns-from-origin=%" PRId64, iterator, ns_from_origin);
1348 set_self_comp_port_input_msg_iterator_state(iterator,
1349 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_SEEKING);
1350
1351 if (iterator->methods.seek_ns_from_origin) {
1352 BT_LIB_LOGD("Calling user's \"seek nanoseconds from origin\" method: "
1353 "%![iter-]+i, ns=%" PRId64, iterator, ns_from_origin);
1354 status = iterator->methods.seek_ns_from_origin(iterator,
1355 ns_from_origin);
1356 BT_LOGD("User method returned: status=%s",
1357 bt_message_iterator_status_string(status));
1358 BT_ASSERT_PRE(status == BT_MESSAGE_ITERATOR_STATUS_OK ||
1359 status == BT_MESSAGE_ITERATOR_STATUS_ERROR ||
1360 status == BT_MESSAGE_ITERATOR_STATUS_NOMEM ||
1361 status == BT_MESSAGE_ITERATOR_STATUS_AGAIN,
1362 "Unexpected status: %![iter-]+i, status=%s",
1363 iterator,
d47c10dc 1364 bt_common_self_message_iterator_status_string(status));
15a52f66
PP
1365 } else {
1366 /* Start automatic seeking: seek beginning first */
1367 BT_ASSERT(iterator->methods.can_seek_beginning(iterator));
1368 BT_ASSERT(iterator->methods.seek_beginning);
1369 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i",
1370 iterator);
1371 status = iterator->methods.seek_beginning(iterator);
1372 BT_LOGD("User method returned: status=%s",
1373 bt_message_iterator_status_string(status));
1374 BT_ASSERT_PRE(status == BT_MESSAGE_ITERATOR_STATUS_OK ||
1375 status == BT_MESSAGE_ITERATOR_STATUS_ERROR ||
1376 status == BT_MESSAGE_ITERATOR_STATUS_NOMEM ||
1377 status == BT_MESSAGE_ITERATOR_STATUS_AGAIN,
1378 "Unexpected status: %![iter-]+i, status=%s",
1379 iterator,
d47c10dc 1380 bt_common_self_message_iterator_status_string(status));
15a52f66
PP
1381 switch (status) {
1382 case BT_MESSAGE_ITERATOR_STATUS_OK:
1383 break;
1384 case BT_MESSAGE_ITERATOR_STATUS_ERROR:
1385 case BT_MESSAGE_ITERATOR_STATUS_NOMEM:
1386 case BT_MESSAGE_ITERATOR_STATUS_AGAIN:
1387 goto end;
1388 default:
1389 abort();
1390 }
1391
1392 /*
1393 * Find the first message which has a default clock
1394 * snapshot greater than or equal to the requested
3d902f17
PP
1395 * seeking time, and move the received messages from
1396 * this point in the batch to this iterator's auto-seek
1397 * message queue.
15a52f66 1398 */
3d902f17
PP
1399 while (!g_queue_is_empty(iterator->auto_seek_msgs)) {
1400 bt_object_put_no_null_check(
1401 g_queue_pop_tail(iterator->auto_seek_msgs));
1402 }
1403
15a52f66
PP
1404 status = find_message_ge_ns_from_origin(iterator,
1405 ns_from_origin);
1406 switch (status) {
1407 case BT_MESSAGE_ITERATOR_STATUS_OK:
3d902f17 1408 case BT_MESSAGE_ITERATOR_STATUS_END:
15a52f66 1409 /*
3d902f17
PP
1410 * If there are messages in the auto-seek
1411 * message queue, replace the user's "next"
1412 * method with a custom, temporary "next" method
1413 * which returns them.
15a52f66 1414 */
3d902f17
PP
1415 if (!g_queue_is_empty(iterator->auto_seek_msgs)) {
1416 iterator->methods.next =
1417 (bt_self_component_port_input_message_iterator_next_method)
1418 post_auto_seek_next;
1419 }
1420
1421 /*
1422 * `BT_MESSAGE_ITERATOR_STATUS_END` becomes
1423 * `BT_MESSAGE_ITERATOR_STATUS_OK`: the next
1424 * time this iterator's "next" method is called,
1425 * it will return
1426 * `BT_MESSAGE_ITERATOR_STATUS_END`.
1427 */
1428 status = BT_MESSAGE_ITERATOR_STATUS_OK;
15a52f66
PP
1429 break;
1430 case BT_MESSAGE_ITERATOR_STATUS_ERROR:
1431 case BT_MESSAGE_ITERATOR_STATUS_NOMEM:
1432 case BT_MESSAGE_ITERATOR_STATUS_AGAIN:
1433 goto end;
15a52f66
PP
1434 default:
1435 abort();
1436 }
1437 }
1438
1439end:
1440 set_iterator_state_after_seeking(iterator, status);
15a52f66
PP
1441 return status;
1442}
1443
1444static inline
1445bt_self_component_port_input_message_iterator *
1446borrow_output_port_message_iterator_upstream_iterator(
1447 struct bt_port_output_message_iterator *iterator)
1448{
1449 struct bt_component_class_sink_colander_priv_data *colander_data;
1450
1451 BT_ASSERT(iterator);
1452 colander_data = (void *) iterator->colander->parent.user_data;
1453 BT_ASSERT(colander_data);
1454 BT_ASSERT(colander_data->msg_iter);
1455 return colander_data->msg_iter;
1456}
1457
1458bt_bool bt_port_output_message_iterator_can_seek_ns_from_origin(
1459 struct bt_port_output_message_iterator *iterator,
1460 int64_t ns_from_origin)
1461{
1462 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
1463 return bt_self_component_port_input_message_iterator_can_seek_ns_from_origin(
1464 borrow_output_port_message_iterator_upstream_iterator(
1465 iterator), ns_from_origin);
1466}
1467
1468bt_bool bt_port_output_message_iterator_can_seek_beginning(
1469 struct bt_port_output_message_iterator *iterator)
1470{
1471 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
1472 return bt_self_component_port_input_message_iterator_can_seek_beginning(
1473 borrow_output_port_message_iterator_upstream_iterator(
1474 iterator));
1475}
1476
1477enum bt_message_iterator_status bt_port_output_message_iterator_seek_ns_from_origin(
1478 struct bt_port_output_message_iterator *iterator,
1479 int64_t ns_from_origin)
1480{
1481 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
1482 return bt_self_component_port_input_message_iterator_seek_ns_from_origin(
1483 borrow_output_port_message_iterator_upstream_iterator(iterator),
1484 ns_from_origin);
1485}
1486
1487enum bt_message_iterator_status bt_port_output_message_iterator_seek_beginning(
1488 struct bt_port_output_message_iterator *iterator)
1489{
1490 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
1491 return bt_self_component_port_input_message_iterator_seek_beginning(
1492 borrow_output_port_message_iterator_upstream_iterator(
1493 iterator));
1494}
1495
b09a5592
PP
1496void bt_port_output_message_iterator_get_ref(
1497 const struct bt_port_output_message_iterator *iterator)
8c6884d9
PP
1498{
1499 bt_object_get_ref(iterator);
1500}
1501
b09a5592
PP
1502void bt_port_output_message_iterator_put_ref(
1503 const struct bt_port_output_message_iterator *iterator)
8c6884d9
PP
1504{
1505 bt_object_put_ref(iterator);
1506}
1507
b09a5592
PP
1508void bt_self_component_port_input_message_iterator_get_ref(
1509 const struct bt_self_component_port_input_message_iterator *iterator)
8c6884d9
PP
1510{
1511 bt_object_get_ref(iterator);
1512}
1513
b09a5592
PP
1514void bt_self_component_port_input_message_iterator_put_ref(
1515 const struct bt_self_component_port_input_message_iterator *iterator)
8c6884d9
PP
1516{
1517 bt_object_put_ref(iterator);
1518}
This page took 0.120878 seconds and 4 git commands to generate.