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