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