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