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