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