lib: add bt_{graph,query_executor}_add_interrupter()
[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 "LIB/MSG-ITER"
25 #include "lib/logging.h"
26
27 #include "compat/compiler.h"
28 #include "compat/glib.h"
29 #include "lib/trace-ir/clock-class.h"
30 #include "lib/trace-ir/clock-snapshot.h"
31 #include <babeltrace2/trace-ir/field.h>
32 #include <babeltrace2/trace-ir/event-const.h>
33 #include "lib/trace-ir/event.h"
34 #include <babeltrace2/trace-ir/packet-const.h>
35 #include "lib/trace-ir/packet.h"
36 #include "lib/trace-ir/stream.h"
37 #include <babeltrace2/trace-ir/clock-class-const.h>
38 #include <babeltrace2/trace-ir/stream-class-const.h>
39 #include <babeltrace2/trace-ir/stream-const.h>
40 #include <babeltrace2/graph/connection-const.h>
41 #include <babeltrace2/graph/component-const.h>
42 #include <babeltrace2/graph/component-sink-const.h>
43 #include <babeltrace2/graph/message-const.h>
44 #include <babeltrace2/graph/message-iterator.h>
45 #include <babeltrace2/graph/self-component-port-input-message-iterator.h>
46 #include <babeltrace2/graph/port-output-message-iterator.h>
47 #include <babeltrace2/graph/message-event-const.h>
48 #include <babeltrace2/graph/message-message-iterator-inactivity-const.h>
49 #include <babeltrace2/graph/message-packet-beginning.h>
50 #include <babeltrace2/graph/message-packet-beginning-const.h>
51 #include <babeltrace2/graph/message-packet-end-const.h>
52 #include <babeltrace2/graph/message-stream-beginning.h>
53 #include <babeltrace2/graph/message-stream-beginning-const.h>
54 #include <babeltrace2/graph/message-stream-end-const.h>
55 #include <babeltrace2/graph/port-const.h>
56 #include <babeltrace2/graph/graph.h>
57 #include <babeltrace2/graph/graph-const.h>
58 #include <babeltrace2/types.h>
59 #include "common/assert.h"
60 #include "lib/assert-pre.h"
61 #include "lib/assert-post.h"
62 #include <stdint.h>
63 #include <inttypes.h>
64 #include <stdlib.h>
65
66 #include "component-class.h"
67 #include "component-class-sink-colander.h"
68 #include "component.h"
69 #include "component-sink.h"
70 #include "component-source.h"
71 #include "connection.h"
72 #include "graph.h"
73 #include "message/discarded-items.h"
74 #include "message/event.h"
75 #include "message/iterator.h"
76 #include "message/message.h"
77 #include "message/message-iterator-inactivity.h"
78 #include "message/stream.h"
79 #include "message/packet.h"
80 #include "lib/func-status.h"
81
82 /*
83 * TODO: Use graph's state (number of active iterators, etc.) and
84 * possibly system specifications to make a better guess than this.
85 */
86 #define MSG_BATCH_SIZE 15
87
88 #define BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(_iter) \
89 BT_ASSERT_PRE((_iter)->state == BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE || \
90 (_iter)->state == BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED || \
91 (_iter)->state == BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN || \
92 (_iter)->state == BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR, \
93 "Message iterator is in the wrong state: %!+i", _iter)
94
95 static inline
96 void set_self_comp_port_input_msg_iterator_state(
97 struct bt_self_component_port_input_message_iterator *iterator,
98 enum bt_self_component_port_input_message_iterator_state state)
99 {
100 BT_ASSERT(iterator);
101 BT_LIB_LOGD("Updating message iterator's state: new-state=%s",
102 bt_self_component_port_input_message_iterator_state_string(state));
103 iterator->state = state;
104 }
105
106 static
107 void destroy_base_message_iterator(struct bt_object *obj)
108 {
109 struct bt_message_iterator *iterator = (void *) obj;
110
111 BT_ASSERT(iterator);
112
113 if (iterator->msgs) {
114 g_ptr_array_free(iterator->msgs, TRUE);
115 iterator->msgs = NULL;
116 }
117
118 g_free(iterator);
119 }
120
121 static
122 void bt_self_component_port_input_message_iterator_destroy(struct bt_object *obj)
123 {
124 struct bt_self_component_port_input_message_iterator *iterator;
125
126 BT_ASSERT(obj);
127
128 /*
129 * The message iterator's reference count is 0 if we're
130 * here. Increment it to avoid a double-destroy (possibly
131 * infinitely recursive). This could happen for example if the
132 * message iterator's finalization function does
133 * bt_object_get_ref() (or anything that causes
134 * bt_object_get_ref() to be called) on itself (ref. count goes
135 * from 0 to 1), and then bt_object_put_ref(): the reference
136 * count would go from 1 to 0 again and this function would be
137 * called again.
138 */
139 obj->ref_count++;
140 iterator = (void *) obj;
141 BT_LIB_LOGI("Destroying self component input port message iterator object: "
142 "%!+i", iterator);
143 bt_self_component_port_input_message_iterator_try_finalize(iterator);
144
145 if (iterator->connection) {
146 /*
147 * Remove ourself from the originating connection so
148 * that it does not try to finalize a dangling pointer
149 * later.
150 */
151 bt_connection_remove_iterator(iterator->connection, iterator);
152 iterator->connection = NULL;
153 }
154
155 if (iterator->auto_seek.msgs) {
156 while (!g_queue_is_empty(iterator->auto_seek.msgs)) {
157 bt_object_put_no_null_check(
158 g_queue_pop_tail(iterator->auto_seek.msgs));
159 }
160
161 g_queue_free(iterator->auto_seek.msgs);
162 iterator->auto_seek.msgs = NULL;
163 }
164
165 if (iterator->upstream_msg_iters) {
166 /*
167 * At this point the message iterator is finalized, so
168 * it's detached from any upstream message iterator.
169 */
170 BT_ASSERT(iterator->upstream_msg_iters->len == 0);
171 g_ptr_array_free(iterator->upstream_msg_iters, TRUE);
172 iterator->upstream_msg_iters = NULL;
173 }
174
175 destroy_base_message_iterator(obj);
176 }
177
178 BT_HIDDEN
179 void bt_self_component_port_input_message_iterator_try_finalize(
180 struct bt_self_component_port_input_message_iterator *iterator)
181 {
182 uint64_t i;
183 typedef void (*method_t)(void *);
184
185 struct bt_component_class *comp_class = NULL;
186 method_t method = NULL;
187
188 BT_ASSERT(iterator);
189
190 switch (iterator->state) {
191 case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED:
192 /* Skip user finalization if user initialization failed */
193 BT_LIB_LOGD("Not finalizing non-initialized message iterator: "
194 "%!+i", iterator);
195 goto end;
196 case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED:
197 /* Already finalized */
198 BT_LIB_LOGD("Not finalizing message iterator: already finalized: "
199 "%!+i", iterator);
200 goto end;
201 case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZING:
202 /* Finalizing */
203 BT_LIB_LOGF("Message iterator is already being finalized: "
204 "%!+i", iterator);
205 abort();
206 default:
207 break;
208 }
209
210 BT_LIB_LOGD("Finalizing message iterator: %!+i", iterator);
211 set_self_comp_port_input_msg_iterator_state(iterator,
212 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZING);
213 BT_ASSERT(iterator->upstream_component);
214 comp_class = iterator->upstream_component->class;
215
216 /* Call user-defined destroy method */
217 switch (comp_class->type) {
218 case BT_COMPONENT_CLASS_TYPE_SOURCE:
219 {
220 struct bt_component_class_source *src_comp_cls =
221 (void *) comp_class;
222
223 method = (method_t) src_comp_cls->methods.msg_iter_finalize;
224 break;
225 }
226 case BT_COMPONENT_CLASS_TYPE_FILTER:
227 {
228 struct bt_component_class_filter *flt_comp_cls =
229 (void *) comp_class;
230
231 method = (method_t) flt_comp_cls->methods.msg_iter_finalize;
232 break;
233 }
234 default:
235 /* Unreachable */
236 abort();
237 }
238
239 if (method) {
240 BT_LIB_LOGD("Calling user's finalization method: %!+i",
241 iterator);
242 method(iterator);
243 }
244
245 /* Detach upstream message iterators */
246 for (i = 0; i < iterator->upstream_msg_iters->len; i++) {
247 struct bt_self_component_port_input_message_iterator *upstream_msg_iter =
248 iterator->upstream_msg_iters->pdata[i];
249
250 upstream_msg_iter->downstream_msg_iter = NULL;
251 }
252
253 g_ptr_array_set_size(iterator->upstream_msg_iters, 0);
254
255 /* Detach downstream message iterator */
256 if (iterator->downstream_msg_iter) {
257 gboolean existed;
258
259 BT_ASSERT(iterator->downstream_msg_iter->upstream_msg_iters);
260 existed = g_ptr_array_remove_fast(
261 iterator->downstream_msg_iter->upstream_msg_iters,
262 iterator);
263 BT_ASSERT(existed);
264 }
265
266 iterator->upstream_component = NULL;
267 iterator->upstream_port = NULL;
268 set_self_comp_port_input_msg_iterator_state(iterator,
269 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED);
270 BT_LIB_LOGD("Finalized message iterator: %!+i", iterator);
271
272 end:
273 return;
274 }
275
276 BT_HIDDEN
277 void bt_self_component_port_input_message_iterator_set_connection(
278 struct bt_self_component_port_input_message_iterator *iterator,
279 struct bt_connection *connection)
280 {
281 BT_ASSERT(iterator);
282 iterator->connection = connection;
283 BT_LIB_LOGI("Set message iterator's connection: "
284 "%![iter-]+i, %![conn-]+x", iterator, connection);
285 }
286
287 static
288 int init_message_iterator(struct bt_message_iterator *iterator,
289 enum bt_message_iterator_type type,
290 bt_object_release_func destroy)
291 {
292 int ret = 0;
293
294 bt_object_init_shared(&iterator->base, destroy);
295 iterator->type = type;
296 iterator->msgs = g_ptr_array_new();
297 if (!iterator->msgs) {
298 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
299 ret = -1;
300 goto end;
301 }
302
303 g_ptr_array_set_size(iterator->msgs, MSG_BATCH_SIZE);
304
305 end:
306 return ret;
307 }
308
309 static
310 bt_bool can_seek_ns_from_origin_true(
311 struct bt_self_component_port_input_message_iterator *iterator,
312 int64_t ns_from_origin)
313 {
314 return BT_TRUE;
315 }
316
317 static
318 bt_bool can_seek_beginning_true(
319 struct bt_self_component_port_input_message_iterator *iterator)
320 {
321 return BT_TRUE;
322 }
323
324 static
325 struct bt_self_component_port_input_message_iterator *
326 create_self_component_input_port_message_iterator(
327 struct bt_self_message_iterator *self_downstream_msg_iter,
328 struct bt_self_component_port_input *self_port)
329 {
330 typedef enum bt_component_class_message_iterator_init_method_status (*init_method_t)(
331 void *, void *, void *);
332
333 int ret;
334 init_method_t init_method = NULL;
335 struct bt_self_component_port_input_message_iterator *iterator =
336 NULL;
337 struct bt_self_component_port_input_message_iterator *downstream_msg_iter =
338 (void *) self_downstream_msg_iter;
339 struct bt_port *port = (void *) self_port;
340 struct bt_port *upstream_port;
341 struct bt_component *comp;
342 struct bt_component *upstream_comp;
343 struct bt_component_class *upstream_comp_cls;
344
345 BT_ASSERT_PRE_NON_NULL(port, "Input port");
346 comp = bt_port_borrow_component_inline(port);
347 BT_ASSERT_PRE(bt_port_is_connected(port),
348 "Input port is not connected: %![port-]+p", port);
349 BT_ASSERT_PRE(comp, "Input port is not part of a component: %![port-]+p",
350 port);
351 BT_ASSERT(port->connection);
352 upstream_port = port->connection->upstream_port;
353 BT_ASSERT(upstream_port);
354 upstream_comp = bt_port_borrow_component_inline(upstream_port);
355 BT_ASSERT(upstream_comp);
356 BT_ASSERT_PRE(
357 bt_component_borrow_graph(upstream_comp)->config_state ==
358 BT_GRAPH_CONFIGURATION_STATE_PARTIALLY_CONFIGURED ||
359 bt_component_borrow_graph(upstream_comp)->config_state ==
360 BT_GRAPH_CONFIGURATION_STATE_CONFIGURED,
361 "Graph is not configured: %!+g",
362 bt_component_borrow_graph(upstream_comp));
363 upstream_comp_cls = upstream_comp->class;
364 BT_ASSERT(upstream_comp->class->type ==
365 BT_COMPONENT_CLASS_TYPE_SOURCE ||
366 upstream_comp->class->type ==
367 BT_COMPONENT_CLASS_TYPE_FILTER);
368 BT_LIB_LOGI("Creating message iterator on self component input port: "
369 "%![up-comp-]+c, %![up-port-]+p", upstream_comp, upstream_port);
370 iterator = g_new0(
371 struct bt_self_component_port_input_message_iterator, 1);
372 if (!iterator) {
373 BT_LIB_LOGE_APPEND_CAUSE(
374 "Failed to allocate one self component input port "
375 "message iterator.");
376 goto error;
377 }
378
379 ret = init_message_iterator((void *) iterator,
380 BT_MESSAGE_ITERATOR_TYPE_SELF_COMPONENT_PORT_INPUT,
381 bt_self_component_port_input_message_iterator_destroy);
382 if (ret) {
383 /* init_message_iterator() logs errors */
384 goto error;
385 }
386
387 iterator->last_ns_from_origin = INT64_MIN;
388 iterator->auto_seek.msgs = g_queue_new();
389 if (!iterator->auto_seek.msgs) {
390 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GQueue.");
391 goto error;
392 }
393
394 iterator->upstream_msg_iters = g_ptr_array_new();
395 if (!iterator->upstream_msg_iters) {
396 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray.");
397 goto error;
398 }
399
400 iterator->upstream_component = upstream_comp;
401 iterator->upstream_port = upstream_port;
402 iterator->connection = iterator->upstream_port->connection;
403 iterator->graph = bt_component_borrow_graph(upstream_comp);
404 set_self_comp_port_input_msg_iterator_state(iterator,
405 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED);
406
407 switch (iterator->upstream_component->class->type) {
408 case BT_COMPONENT_CLASS_TYPE_SOURCE:
409 {
410 struct bt_component_class_source *src_comp_cls =
411 (void *) iterator->upstream_component->class;
412
413 iterator->methods.next =
414 (bt_self_component_port_input_message_iterator_next_method)
415 src_comp_cls->methods.msg_iter_next;
416 iterator->methods.seek_ns_from_origin =
417 (bt_self_component_port_input_message_iterator_seek_ns_from_origin_method)
418 src_comp_cls->methods.msg_iter_seek_ns_from_origin;
419 iterator->methods.seek_beginning =
420 (bt_self_component_port_input_message_iterator_seek_beginning_method)
421 src_comp_cls->methods.msg_iter_seek_beginning;
422 iterator->methods.can_seek_ns_from_origin =
423 (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method)
424 src_comp_cls->methods.msg_iter_can_seek_ns_from_origin;
425 iterator->methods.can_seek_beginning =
426 (bt_self_component_port_input_message_iterator_can_seek_beginning_method)
427 src_comp_cls->methods.msg_iter_can_seek_beginning;
428 break;
429 }
430 case BT_COMPONENT_CLASS_TYPE_FILTER:
431 {
432 struct bt_component_class_filter *flt_comp_cls =
433 (void *) iterator->upstream_component->class;
434
435 iterator->methods.next =
436 (bt_self_component_port_input_message_iterator_next_method)
437 flt_comp_cls->methods.msg_iter_next;
438 iterator->methods.seek_ns_from_origin =
439 (bt_self_component_port_input_message_iterator_seek_ns_from_origin_method)
440 flt_comp_cls->methods.msg_iter_seek_ns_from_origin;
441 iterator->methods.seek_beginning =
442 (bt_self_component_port_input_message_iterator_seek_beginning_method)
443 flt_comp_cls->methods.msg_iter_seek_beginning;
444 iterator->methods.can_seek_ns_from_origin =
445 (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method)
446 flt_comp_cls->methods.msg_iter_can_seek_ns_from_origin;
447 iterator->methods.can_seek_beginning =
448 (bt_self_component_port_input_message_iterator_can_seek_beginning_method)
449 flt_comp_cls->methods.msg_iter_can_seek_beginning;
450 break;
451 }
452 default:
453 abort();
454 }
455
456 if (iterator->methods.seek_ns_from_origin &&
457 !iterator->methods.can_seek_ns_from_origin) {
458 iterator->methods.can_seek_ns_from_origin =
459 (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method)
460 can_seek_ns_from_origin_true;
461 }
462
463 if (iterator->methods.seek_beginning &&
464 !iterator->methods.can_seek_beginning) {
465 iterator->methods.can_seek_beginning =
466 (bt_self_component_port_input_message_iterator_seek_beginning_method)
467 can_seek_beginning_true;
468 }
469
470 switch (upstream_comp_cls->type) {
471 case BT_COMPONENT_CLASS_TYPE_SOURCE:
472 {
473 struct bt_component_class_source *src_comp_cls =
474 (void *) upstream_comp_cls;
475
476 init_method =
477 (init_method_t) src_comp_cls->methods.msg_iter_init;
478 break;
479 }
480 case BT_COMPONENT_CLASS_TYPE_FILTER:
481 {
482 struct bt_component_class_filter *flt_comp_cls =
483 (void *) upstream_comp_cls;
484
485 init_method =
486 (init_method_t) flt_comp_cls->methods.msg_iter_init;
487 break;
488 }
489 default:
490 /* Unreachable */
491 abort();
492 }
493
494 if (init_method) {
495 enum bt_component_class_message_iterator_init_method_status iter_status;
496
497 BT_LIB_LOGD("Calling user's initialization method: %!+i", iterator);
498 iter_status = init_method(iterator, upstream_comp,
499 upstream_port);
500 BT_LOGD("User method returned: status=%s",
501 bt_common_func_status_string(iter_status));
502 if (iter_status != BT_FUNC_STATUS_OK) {
503 BT_LIB_LOGW_APPEND_CAUSE(
504 "Component input port message iterator initialization method failed: "
505 "%![iter-]+i, status=%s",
506 iterator,
507 bt_common_func_status_string(iter_status));
508 goto error;
509 }
510 }
511
512 if (downstream_msg_iter) {
513 /* Set this message iterator's downstream message iterator */
514 iterator->downstream_msg_iter = downstream_msg_iter;
515
516 /*
517 * Add this message iterator to the downstream message
518 * iterator's array of upstream message iterators.
519 */
520 g_ptr_array_add(downstream_msg_iter->upstream_msg_iters,
521 iterator);
522 }
523
524 set_self_comp_port_input_msg_iterator_state(iterator,
525 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE);
526 g_ptr_array_add(port->connection->iterators, iterator);
527 BT_LIB_LOGI("Created message iterator on self component input port: "
528 "%![up-port-]+p, %![up-comp-]+c, %![iter-]+i",
529 upstream_port, upstream_comp, iterator);
530 goto end;
531
532 error:
533 BT_OBJECT_PUT_REF_AND_RESET(iterator);
534
535 end:
536 return iterator;
537 }
538
539 struct bt_self_component_port_input_message_iterator *
540 bt_self_component_port_input_message_iterator_create_from_message_iterator(
541 struct bt_self_message_iterator *self_msg_iter,
542 struct bt_self_component_port_input *input_port)
543 {
544 BT_ASSERT_PRE_NON_NULL(self_msg_iter, "Message iterator");
545 return create_self_component_input_port_message_iterator(self_msg_iter,
546 input_port);
547 }
548
549 struct bt_self_component_port_input_message_iterator *
550 bt_self_component_port_input_message_iterator_create_from_sink_component(
551 struct bt_self_component_sink *self_comp,
552 struct bt_self_component_port_input *input_port)
553 {
554 BT_ASSERT_PRE_NON_NULL(self_comp, "Sink component");
555 return create_self_component_input_port_message_iterator(NULL,
556 input_port);
557 }
558
559 void *bt_self_message_iterator_get_data(
560 const struct bt_self_message_iterator *self_iterator)
561 {
562 struct bt_self_component_port_input_message_iterator *iterator =
563 (void *) self_iterator;
564
565 BT_ASSERT_PRE_DEV_NON_NULL(iterator, "Message iterator");
566 return iterator->user_data;
567 }
568
569 void bt_self_message_iterator_set_data(
570 struct bt_self_message_iterator *self_iterator, void *data)
571 {
572 struct bt_self_component_port_input_message_iterator *iterator =
573 (void *) self_iterator;
574
575 BT_ASSERT_PRE_DEV_NON_NULL(iterator, "Message iterator");
576 iterator->user_data = data;
577 BT_LIB_LOGD("Set message iterator's user data: "
578 "%!+i, user-data-addr=%p", iterator, data);
579 }
580
581 /*
582 * Validate that the default clock snapshot in `msg` doesn't make us go back in
583 * time.
584 */
585
586 BT_ASSERT_POST_DEV_FUNC
587 static
588 bool clock_snapshots_are_monotonic_one(
589 struct bt_self_component_port_input_message_iterator *iterator,
590 const bt_message *msg)
591 {
592 const struct bt_clock_snapshot *clock_snapshot = NULL;
593 bt_message_type message_type = bt_message_get_type(msg);
594 int64_t ns_from_origin;
595 enum bt_clock_snapshot_get_ns_from_origin_status clock_snapshot_status;
596
597 /*
598 * The default is true: if we can't figure out the clock snapshot
599 * (or there is none), assume it is fine.
600 */
601 bool result = true;
602
603 switch (message_type) {
604 case BT_MESSAGE_TYPE_EVENT:
605 {
606 struct bt_message_event *event_msg = (struct bt_message_event *) msg;
607 clock_snapshot = event_msg->default_cs;
608 break;
609 }
610 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY:
611 {
612 struct bt_message_message_iterator_inactivity *inactivity_msg =
613 (struct bt_message_message_iterator_inactivity *) msg;
614 clock_snapshot = inactivity_msg->default_cs;
615 break;
616 }
617 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
618 case BT_MESSAGE_TYPE_PACKET_END:
619 {
620 struct bt_message_packet *packet_msg = (struct bt_message_packet *) msg;
621 clock_snapshot = packet_msg->default_cs;
622 break;
623 }
624 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
625 case BT_MESSAGE_TYPE_STREAM_END:
626 {
627 struct bt_message_stream *stream_msg = (struct bt_message_stream *) msg;
628 if (stream_msg->default_cs_state != BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN) {
629 goto end;
630 }
631
632 clock_snapshot = stream_msg->default_cs;
633 break;
634 }
635 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
636 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
637 {
638 struct bt_message_discarded_items *discarded_msg =
639 (struct bt_message_discarded_items *) msg;
640
641 clock_snapshot = discarded_msg->default_begin_cs;
642 break;
643 }
644 }
645
646 if (!clock_snapshot) {
647 goto end;
648 }
649
650 clock_snapshot_status = bt_clock_snapshot_get_ns_from_origin(clock_snapshot, &ns_from_origin);
651 if (clock_snapshot_status != BT_FUNC_STATUS_OK) {
652 goto end;
653 }
654
655 result = ns_from_origin >= iterator->last_ns_from_origin;
656 iterator->last_ns_from_origin = ns_from_origin;
657 end:
658 return result;
659 }
660
661 BT_ASSERT_POST_DEV_FUNC
662 static
663 bool clock_snapshots_are_monotonic(
664 struct bt_self_component_port_input_message_iterator *iterator,
665 bt_message_array_const msgs, uint64_t msg_count)
666 {
667 uint64_t i;
668 bool result;
669
670 for (i = 0; i < msg_count; i++) {
671 if (!clock_snapshots_are_monotonic_one(iterator, msgs[i])) {
672 result = false;
673 goto end;
674 }
675 }
676
677 result = true;
678
679 end:
680 return result;
681 }
682
683 /*
684 * When a new stream begins, verify that the clock class tied to this
685 * stream is compatible with what we've seen before.
686 */
687
688 BT_ASSERT_POST_DEV_FUNC
689 static
690 bool clock_classes_are_compatible_one(struct bt_self_component_port_input_message_iterator *iterator,
691 const struct bt_message *msg)
692 {
693 enum bt_message_type message_type = bt_message_get_type(msg);
694 bool result;
695
696 if (message_type == BT_MESSAGE_TYPE_STREAM_BEGINNING) {
697 const struct bt_message_stream *stream_msg = (struct bt_message_stream *) msg;
698 const struct bt_clock_class *clock_class = stream_msg->stream->class->default_clock_class;
699 bt_uuid clock_class_uuid = NULL;
700
701 if (clock_class) {
702 clock_class_uuid = bt_clock_class_get_uuid(clock_class);
703 }
704
705 switch (iterator->clock_expectation.type) {
706 case CLOCK_EXPECTATION_UNSET:
707 /*
708 * This is the first time we see a message with a clock
709 * snapshot: record the properties of that clock, against
710 * which we'll compare the clock properties of the following
711 * messages.
712 */
713
714 if (!clock_class) {
715 iterator->clock_expectation.type = CLOCK_EXPECTATION_NONE;
716 } else if (bt_clock_class_origin_is_unix_epoch(clock_class)) {
717 iterator->clock_expectation.type = CLOCK_EXPECTATION_ORIGIN_UNIX;
718 } else if (clock_class_uuid) {
719 iterator->clock_expectation.type = CLOCK_EXPECTATION_ORIGIN_OTHER_UUID;
720 bt_uuid_copy(iterator->clock_expectation.uuid, clock_class_uuid);
721 } else {
722 iterator->clock_expectation.type = CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID;
723 }
724 break;
725
726 case CLOCK_EXPECTATION_NONE:
727 if (clock_class) {
728 BT_ASSERT_POST_DEV_MSG(
729 "Expecting no clock class, got one: %![cc-]+K",
730 clock_class);
731 result = false;
732 goto end;
733 }
734
735 break;
736
737 case CLOCK_EXPECTATION_ORIGIN_UNIX:
738 if (!clock_class) {
739 BT_ASSERT_POST_DEV_MSG(
740 "Expecting a clock class, got none.");
741 result = false;
742 goto end;
743 }
744
745 if (!bt_clock_class_origin_is_unix_epoch(clock_class)) {
746 BT_ASSERT_POST_DEV_MSG(
747 "Expecting a clock class with Unix epoch origin: %![cc-]+K",
748 clock_class);
749 result = false;
750 goto end;
751 }
752 break;
753
754 case CLOCK_EXPECTATION_ORIGIN_OTHER_UUID:
755 if (!clock_class) {
756 BT_ASSERT_POST_DEV_MSG(
757 "Expecting a clock class, got none.");
758 result = false;
759 goto end;
760 }
761
762 if (bt_clock_class_origin_is_unix_epoch(clock_class)) {
763 BT_ASSERT_POST_DEV_MSG(
764 "Expecting a clock class without Unix epoch origin: %![cc-]+K",
765 clock_class);
766 result = false;
767 goto end;
768 }
769
770 if (!clock_class_uuid) {
771 BT_ASSERT_POST_DEV_MSG(
772 "Expecting a clock class with UUID: %![cc-]+K",
773 clock_class);
774 result = false;
775 goto end;
776 }
777
778 if (bt_uuid_compare(iterator->clock_expectation.uuid, clock_class_uuid)) {
779 BT_ASSERT_POST_DEV_MSG(
780 "Expecting a clock class with UUID, got one "
781 "with a different UUID: %![cc-]+K, expected-uuid=%!u",
782 clock_class, iterator->clock_expectation.uuid);
783 result = false;
784 goto end;
785 }
786 break;
787
788 case CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID:
789 if (!clock_class) {
790 BT_ASSERT_POST_DEV_MSG(
791 "Expecting a clock class, got none.");
792 result = false;
793 goto end;
794 }
795
796 if (bt_clock_class_origin_is_unix_epoch(clock_class)) {
797 BT_ASSERT_POST_DEV_MSG(
798 "Expecting a clock class without Unix epoch origin: %![cc-]+K",
799 clock_class);
800 result = false;
801 goto end;
802 }
803
804 if (clock_class_uuid) {
805 BT_ASSERT_POST_DEV_MSG(
806 "Expecting a clock class without UUID: %![cc-]+K",
807 clock_class);
808 result = false;
809 goto end;
810 }
811 break;
812 }
813 }
814
815 result = true;
816
817 end:
818 return result;
819 }
820
821 BT_ASSERT_POST_DEV_FUNC
822 static
823 bool clock_classes_are_compatible(
824 struct bt_self_component_port_input_message_iterator *iterator,
825 bt_message_array_const msgs, uint64_t msg_count)
826 {
827 uint64_t i;
828 bool result;
829
830 for (i = 0; i < msg_count; i++) {
831 if (!clock_classes_are_compatible_one(iterator, msgs[i])) {
832 result = false;
833 goto end;
834 }
835 }
836
837 result = true;
838
839 end:
840 return result;
841 }
842
843 /*
844 * Call the `next` method of the iterator. Do some validation on the returned
845 * messages.
846 */
847
848 static
849 enum bt_component_class_message_iterator_next_method_status
850 call_iterator_next_method(
851 struct bt_self_component_port_input_message_iterator *iterator,
852 bt_message_array_const msgs, uint64_t capacity, uint64_t *user_count)
853 {
854 enum bt_component_class_message_iterator_next_method_status status;
855
856 BT_ASSERT(iterator->methods.next);
857 BT_LOGD_STR("Calling user's \"next\" method.");
858 status = iterator->methods.next(iterator, msgs, capacity, user_count);
859 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64,
860 bt_common_func_status_string(status), *user_count);
861
862 if (status == BT_FUNC_STATUS_OK) {
863 BT_ASSERT_POST_DEV(clock_classes_are_compatible(iterator, msgs, *user_count),
864 "Clocks are not compatible");
865 BT_ASSERT_POST_DEV(clock_snapshots_are_monotonic(iterator, msgs, *user_count),
866 "Clock snapshots are not monotonic");
867 }
868
869 return status;
870 }
871
872 enum bt_message_iterator_next_status
873 bt_self_component_port_input_message_iterator_next(
874 struct bt_self_component_port_input_message_iterator *iterator,
875 bt_message_array_const *msgs, uint64_t *user_count)
876 {
877 enum bt_message_iterator_next_status status = BT_FUNC_STATUS_OK;
878
879 BT_ASSERT_PRE_DEV_NON_NULL(iterator, "Message iterator");
880 BT_ASSERT_PRE_DEV_NON_NULL(msgs, "Message array (output)");
881 BT_ASSERT_PRE_DEV_NON_NULL(user_count, "Message count (output)");
882 BT_ASSERT_PRE_DEV(iterator->state ==
883 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE,
884 "Message iterator's \"next\" called, but "
885 "message iterator is in the wrong state: %!+i", iterator);
886 BT_ASSERT(iterator->upstream_component);
887 BT_ASSERT(iterator->upstream_component->class);
888 BT_ASSERT_PRE_DEV(
889 bt_component_borrow_graph(iterator->upstream_component)->config_state !=
890 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
891 "Graph is not configured: %!+g",
892 bt_component_borrow_graph(iterator->upstream_component));
893 BT_LIB_LOGD("Getting next self component input port "
894 "message iterator's messages: %!+i, batch-size=%u",
895 iterator, MSG_BATCH_SIZE);
896
897 /*
898 * Call the user's "next" method to get the next messages
899 * and status.
900 */
901 *user_count = 0;
902 status = (int) call_iterator_next_method(iterator,
903 (void *) iterator->base.msgs->pdata, MSG_BATCH_SIZE,
904 user_count);
905 BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64,
906 bt_common_func_status_string(status), *user_count);
907 if (status < 0) {
908 BT_LIB_LOGW_APPEND_CAUSE(
909 "Component input port message iterator's \"next\" method failed: "
910 "%![iter-]+i, status=%s",
911 iterator, bt_common_func_status_string(status));
912 goto end;
913 }
914
915 /*
916 * There is no way that this iterator could have been finalized
917 * during its "next" method, as the only way to do this is to
918 * put the last iterator's reference, and this can only be done
919 * by its downstream owner.
920 *
921 * For the same reason, there is no way that this iterator could
922 * have seeked (cannot seek a self message iterator).
923 */
924 BT_ASSERT(iterator->state ==
925 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE);
926
927 switch (status) {
928 case BT_FUNC_STATUS_OK:
929 BT_ASSERT_POST_DEV(*user_count <= MSG_BATCH_SIZE,
930 "Invalid returned message count: greater than "
931 "batch size: count=%" PRIu64 ", batch-size=%u",
932 *user_count, MSG_BATCH_SIZE);
933 *msgs = (void *) iterator->base.msgs->pdata;
934 break;
935 case BT_FUNC_STATUS_AGAIN:
936 goto end;
937 case BT_FUNC_STATUS_END:
938 set_self_comp_port_input_msg_iterator_state(iterator,
939 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED);
940 goto end;
941 default:
942 /* Unknown non-error status */
943 abort();
944 }
945
946 end:
947 return status;
948 }
949
950 enum bt_message_iterator_next_status bt_port_output_message_iterator_next(
951 struct bt_port_output_message_iterator *iterator,
952 bt_message_array_const *msgs_to_user,
953 uint64_t *count_to_user)
954 {
955 enum bt_message_iterator_next_status status;
956 int graph_status;
957
958 BT_ASSERT_PRE_DEV_NON_NULL(iterator, "Message iterator");
959 BT_ASSERT_PRE_DEV_NON_NULL(msgs_to_user, "Message array (output)");
960 BT_ASSERT_PRE_DEV_NON_NULL(count_to_user, "Message count (output)");
961 BT_LIB_LOGD("Getting next output port message iterator's messages: "
962 "%!+i", iterator);
963 graph_status = bt_graph_consume_sink_no_check(iterator->graph,
964 iterator->colander);
965 switch (graph_status) {
966 case BT_FUNC_STATUS_AGAIN:
967 case BT_FUNC_STATUS_END:
968 case BT_FUNC_STATUS_MEMORY_ERROR:
969 status = (int) graph_status;
970 break;
971 case BT_FUNC_STATUS_OK:
972 status = BT_FUNC_STATUS_OK;
973
974 /*
975 * On success, the colander sink moves the messages
976 * to this iterator's array and sets this iterator's
977 * message count: move them to the user.
978 */
979 *msgs_to_user = (void *) iterator->base.msgs->pdata;
980 *count_to_user = iterator->count;
981 break;
982 default:
983 /* Other errors */
984 status = BT_FUNC_STATUS_ERROR;
985 }
986
987 return status;
988 }
989
990 struct bt_component *
991 bt_self_component_port_input_message_iterator_borrow_component(
992 struct bt_self_component_port_input_message_iterator *iterator)
993 {
994 BT_ASSERT_PRE_DEV_NON_NULL(iterator, "Message iterator");
995 return iterator->upstream_component;
996 }
997
998 const struct bt_component *
999 bt_self_component_port_input_message_iterator_borrow_component_const(
1000 const struct bt_self_component_port_input_message_iterator *iterator)
1001 {
1002 BT_ASSERT_PRE_DEV_NON_NULL(iterator, "Message iterator");
1003 return iterator->upstream_component;
1004 }
1005
1006 struct bt_self_component *bt_self_message_iterator_borrow_component(
1007 struct bt_self_message_iterator *self_iterator)
1008 {
1009 struct bt_self_component_port_input_message_iterator *iterator =
1010 (void *) self_iterator;
1011
1012 BT_ASSERT_PRE_DEV_NON_NULL(iterator, "Message iterator");
1013 return (void *) iterator->upstream_component;
1014 }
1015
1016 struct bt_self_port_output *bt_self_message_iterator_borrow_port(
1017 struct bt_self_message_iterator *self_iterator)
1018 {
1019 struct bt_self_component_port_input_message_iterator *iterator =
1020 (void *) self_iterator;
1021
1022 BT_ASSERT_PRE_DEV_NON_NULL(iterator, "Message iterator");
1023 return (void *) iterator->upstream_port;
1024 }
1025
1026 static
1027 void bt_port_output_message_iterator_destroy(struct bt_object *obj)
1028 {
1029 struct bt_port_output_message_iterator *iterator = (void *) obj;
1030
1031 BT_LIB_LOGI("Destroying output port message iterator object: %!+i",
1032 iterator);
1033 BT_LOGD_STR("Putting graph.");
1034 BT_OBJECT_PUT_REF_AND_RESET(iterator->graph);
1035 BT_LOGD_STR("Putting colander sink component.");
1036 BT_OBJECT_PUT_REF_AND_RESET(iterator->colander);
1037 destroy_base_message_iterator(obj);
1038 }
1039
1040 struct bt_port_output_message_iterator *
1041 bt_port_output_message_iterator_create(struct bt_graph *graph,
1042 const struct bt_port_output *output_port)
1043 {
1044 struct bt_port_output_message_iterator *iterator = NULL;
1045 struct bt_component_class_sink *colander_comp_cls = NULL;
1046 struct bt_component *output_port_comp = NULL;
1047 struct bt_component_sink *colander_comp;
1048 int graph_status;
1049 struct bt_port_input *colander_in_port = NULL;
1050 struct bt_component_class_sink_colander_data colander_data;
1051 int ret;
1052
1053 BT_ASSERT_PRE_NON_NULL(graph, "Graph");
1054 BT_ASSERT_PRE_NON_NULL(output_port, "Output port");
1055 output_port_comp = bt_port_borrow_component_inline(
1056 (const void *) output_port);
1057 BT_ASSERT_PRE(output_port_comp,
1058 "Output port has no component: %!+p", output_port);
1059 BT_ASSERT_PRE(bt_component_borrow_graph(output_port_comp) ==
1060 (void *) graph,
1061 "Output port is not part of graph: %![graph-]+g, %![port-]+p",
1062 graph, output_port);
1063 BT_ASSERT_PRE(!graph->has_sink,
1064 "Graph already has a sink component: %![graph-]+g");
1065
1066 /* Create message iterator */
1067 BT_LIB_LOGI("Creating message iterator on output port: "
1068 "%![port-]+p, %![comp-]+c", output_port, output_port_comp);
1069 iterator = g_new0(struct bt_port_output_message_iterator, 1);
1070 if (!iterator) {
1071 BT_LIB_LOGE_APPEND_CAUSE(
1072 "Failed to allocate one output port message iterator.");
1073 goto error;
1074 }
1075
1076 ret = init_message_iterator((void *) iterator,
1077 BT_MESSAGE_ITERATOR_TYPE_PORT_OUTPUT,
1078 bt_port_output_message_iterator_destroy);
1079 if (ret) {
1080 /* init_message_iterator() logs errors */
1081 BT_OBJECT_PUT_REF_AND_RESET(iterator);
1082 goto end;
1083 }
1084
1085 /* Create colander component */
1086 colander_comp_cls = bt_component_class_sink_colander_get();
1087 if (!colander_comp_cls) {
1088 /* bt_component_class_sink_colander_get() logs errors */
1089 BT_LIB_LOGE_APPEND_CAUSE(
1090 "Cannot get colander sink component class.");
1091 goto error;
1092 }
1093
1094 iterator->graph = graph;
1095 bt_object_get_no_null_check(iterator->graph);
1096 colander_data.msgs = (void *) iterator->base.msgs->pdata;
1097 colander_data.count_addr = &iterator->count;
1098
1099 /*
1100 * Hope that nobody uses this very unique name.
1101 *
1102 * We pass `BT_LOGGING_LEVEL_NONE` but the colander component
1103 * class module does not use this level anyway since it belongs
1104 * to the library.
1105 */
1106 graph_status = bt_graph_add_sink_component_with_init_method_data(
1107 (void *) graph, colander_comp_cls,
1108 "colander-36ac3409-b1a8-4d60-ab1f-4fdf341a8fb1",
1109 NULL, &colander_data, BT_LOGGING_LEVEL_NONE,
1110 (void *) &iterator->colander);
1111 if (graph_status != BT_FUNC_STATUS_OK) {
1112 BT_LIB_LOGE_APPEND_CAUSE(
1113 "Cannot add colander sink component to graph: "
1114 "%![graph-]+g, status=%s", graph,
1115 bt_common_func_status_string(graph_status));
1116 goto error;
1117 }
1118
1119 /*
1120 * Connect provided output port to the colander component's
1121 * input port.
1122 */
1123 colander_in_port =
1124 (void *) bt_component_sink_borrow_input_port_by_index_const(
1125 (void *) iterator->colander, 0);
1126 BT_ASSERT(colander_in_port);
1127 graph_status = bt_graph_connect_ports(graph,
1128 output_port, colander_in_port, NULL);
1129 if (graph_status != BT_FUNC_STATUS_OK) {
1130 BT_LIB_LOGW_APPEND_CAUSE(
1131 "Cannot connect colander sink's port: "
1132 "%![graph-]+g, %![comp-]+c, status=%s", graph,
1133 iterator->colander,
1134 bt_common_func_status_string(graph_status));
1135 goto error;
1136 }
1137
1138 /*
1139 * At this point everything went fine. Make the graph
1140 * nonconsumable forever so that only this message iterator
1141 * can consume (thanks to bt_graph_consume_sink_no_check()).
1142 * This avoids leaking the message created by the colander
1143 * sink and moved to the message iterator's message
1144 * member.
1145 */
1146 bt_graph_set_can_consume(iterator->graph, false);
1147
1148 /* Also set the graph as being configured. */
1149 graph_status = bt_graph_configure(graph);
1150 if (graph_status != BT_FUNC_STATUS_OK) {
1151 BT_LIB_LOGW_APPEND_CAUSE(
1152 "Cannot configure graph after having "
1153 "added and connected colander sink: "
1154 "%![graph-]+g, %![comp-]+c, status=%s", graph,
1155 iterator->colander,
1156 bt_common_func_status_string(graph_status));
1157 goto error;
1158 }
1159 goto end;
1160
1161 error:
1162 if (iterator && iterator->graph && iterator->colander) {
1163 int ret;
1164
1165 /* Remove created colander component from graph if any */
1166 colander_comp = iterator->colander;
1167 BT_OBJECT_PUT_REF_AND_RESET(iterator->colander);
1168
1169 /*
1170 * At this point the colander component's reference
1171 * count is 0 because iterator->colander was the only
1172 * owner. We also know that it is not connected because
1173 * this is the last operation before this function
1174 * succeeds.
1175 *
1176 * Since we honor the preconditions here,
1177 * bt_graph_remove_unconnected_component() always
1178 * succeeds.
1179 */
1180 ret = bt_graph_remove_unconnected_component(iterator->graph,
1181 (void *) colander_comp);
1182 BT_ASSERT(ret == 0);
1183 }
1184
1185 BT_OBJECT_PUT_REF_AND_RESET(iterator);
1186
1187 end:
1188 bt_object_put_ref(colander_comp_cls);
1189 return (void *) iterator;
1190 }
1191
1192 bt_bool bt_self_component_port_input_message_iterator_can_seek_ns_from_origin(
1193 struct bt_self_component_port_input_message_iterator *iterator,
1194 int64_t ns_from_origin)
1195 {
1196 bt_bool can = BT_FALSE;
1197
1198 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
1199 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator);
1200 BT_ASSERT_PRE(
1201 bt_component_borrow_graph(iterator->upstream_component)->config_state !=
1202 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
1203 "Graph is not configured: %!+g",
1204 bt_component_borrow_graph(iterator->upstream_component));
1205
1206 if (iterator->methods.can_seek_ns_from_origin) {
1207 can = iterator->methods.can_seek_ns_from_origin(iterator,
1208 ns_from_origin);
1209 goto end;
1210 }
1211
1212 /*
1213 * Automatic seeking fall back: if we can seek to the beginning,
1214 * then we can automatically seek to any message.
1215 */
1216 if (iterator->methods.can_seek_beginning) {
1217 can = iterator->methods.can_seek_beginning(iterator);
1218 }
1219
1220 end:
1221 return can;
1222 }
1223
1224 bt_bool bt_self_component_port_input_message_iterator_can_seek_beginning(
1225 struct bt_self_component_port_input_message_iterator *iterator)
1226 {
1227 bt_bool can = BT_FALSE;
1228
1229 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
1230 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator);
1231 BT_ASSERT_PRE(
1232 bt_component_borrow_graph(iterator->upstream_component)->config_state !=
1233 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
1234 "Graph is not configured: %!+g",
1235 bt_component_borrow_graph(iterator->upstream_component));
1236
1237 if (iterator->methods.can_seek_beginning) {
1238 can = iterator->methods.can_seek_beginning(iterator);
1239 }
1240
1241 return can;
1242 }
1243
1244 static inline
1245 void set_iterator_state_after_seeking(
1246 struct bt_self_component_port_input_message_iterator *iterator,
1247 int status)
1248 {
1249 enum bt_self_component_port_input_message_iterator_state new_state = 0;
1250
1251 /* Set iterator's state depending on seeking status */
1252 switch (status) {
1253 case BT_FUNC_STATUS_OK:
1254 new_state = BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE;
1255 break;
1256 case BT_FUNC_STATUS_AGAIN:
1257 new_state = BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN;
1258 break;
1259 case BT_FUNC_STATUS_ERROR:
1260 case BT_FUNC_STATUS_MEMORY_ERROR:
1261 new_state = BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR;
1262 break;
1263 case BT_FUNC_STATUS_END:
1264 new_state = BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED;
1265 break;
1266 default:
1267 abort();
1268 }
1269
1270 set_self_comp_port_input_msg_iterator_state(iterator, new_state);
1271 }
1272
1273 static
1274 void reset_iterator_expectations(
1275 struct bt_self_component_port_input_message_iterator *iterator)
1276 {
1277 iterator->last_ns_from_origin = INT64_MIN;
1278 iterator->clock_expectation.type = CLOCK_EXPECTATION_UNSET;
1279 }
1280
1281 enum bt_message_iterator_seek_beginning_status
1282 bt_self_component_port_input_message_iterator_seek_beginning(
1283 struct bt_self_component_port_input_message_iterator *iterator)
1284 {
1285 int status;
1286
1287 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
1288 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator);
1289 BT_ASSERT_PRE(
1290 bt_component_borrow_graph(iterator->upstream_component)->config_state !=
1291 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
1292 "Graph is not configured: %!+g",
1293 bt_component_borrow_graph(iterator->upstream_component));
1294 BT_ASSERT_PRE(
1295 bt_self_component_port_input_message_iterator_can_seek_beginning(
1296 iterator),
1297 "Message iterator cannot seek beginning: %!+i", iterator);
1298
1299 /*
1300 * We are seeking, reset our expectations about how the following
1301 * messages should look like.
1302 */
1303 reset_iterator_expectations(iterator);
1304
1305 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i", iterator);
1306 set_self_comp_port_input_msg_iterator_state(iterator,
1307 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_SEEKING);
1308 status = iterator->methods.seek_beginning(iterator);
1309 BT_LOGD("User method returned: status=%s",
1310 bt_common_func_status_string(status));
1311 BT_ASSERT_POST(status == BT_FUNC_STATUS_OK ||
1312 status == BT_FUNC_STATUS_ERROR ||
1313 status == BT_FUNC_STATUS_MEMORY_ERROR ||
1314 status == BT_FUNC_STATUS_AGAIN,
1315 "Unexpected status: %![iter-]+i, status=%s",
1316 iterator, bt_common_func_status_string(status));
1317 if (status < 0) {
1318 BT_LIB_LOGW_APPEND_CAUSE(
1319 "Component input port message iterator's \"seek beginning\" method failed: "
1320 "%![iter-]+i, status=%s",
1321 iterator, bt_common_func_status_string(status));
1322 }
1323
1324 set_iterator_state_after_seeking(iterator, status);
1325 return status;
1326 }
1327
1328 /*
1329 * Structure used to record the state of a given stream during the fast-forward
1330 * phase of an auto-seek.
1331 */
1332 struct auto_seek_stream_state {
1333 /*
1334 * Value representing which step of this timeline we are at.
1335 *
1336 * time --->
1337 * [SB] 1 [PB] 2 [PE] 1 [SE]
1338 *
1339 * At each point in the timeline, the messages we need to replicate are:
1340 *
1341 * 1: Stream beginning
1342 * 2: Stream beginning, packet beginning
1343 *
1344 * Before "Stream beginning" and after "Stream end", we don't need to
1345 * replicate anything as the stream doesn't exist.
1346 */
1347 enum {
1348 AUTO_SEEK_STREAM_STATE_STREAM_BEGAN,
1349 AUTO_SEEK_STREAM_STATE_PACKET_BEGAN,
1350 } state;
1351
1352 /*
1353 * If `state` is AUTO_SEEK_STREAM_STATE_PACKET_BEGAN, the packet we are
1354 * in. This is a weak reference, since the packet will always be
1355 * alive by the time we use it.
1356 */
1357 struct bt_packet *packet;
1358
1359 /* Have we see a message with a clock snapshot yet? */
1360 bool seen_clock_snapshot;
1361 };
1362
1363 static
1364 struct auto_seek_stream_state *create_auto_seek_stream_state(void)
1365 {
1366 return g_new0(struct auto_seek_stream_state, 1);
1367 }
1368
1369 static
1370 void destroy_auto_seek_stream_state(void *ptr)
1371 {
1372 g_free(ptr);
1373 }
1374
1375 static
1376 GHashTable *create_auto_seek_stream_states(void)
1377 {
1378 return g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL,
1379 destroy_auto_seek_stream_state);
1380 }
1381
1382 static
1383 void destroy_auto_seek_stream_states(GHashTable *stream_states)
1384 {
1385 g_hash_table_destroy(stream_states);
1386 }
1387
1388 /*
1389 * Handle one message while we are in the fast-forward phase of an auto-seek.
1390 *
1391 * Sets `*got_first` to true if the message's timestamp is greater or equal to
1392 * `ns_from_origin`. In other words, if this is the first message after our
1393 * seek point.
1394 *
1395 * `stream_states` is an hash table of `bt_stream *` (weak reference) to
1396 * `struct auto_seek_stream_state` used to keep the state of each stream
1397 * during the fast-forward.
1398 */
1399
1400 static inline
1401 int auto_seek_handle_message(
1402 struct bt_self_component_port_input_message_iterator *iterator,
1403 int64_t ns_from_origin, const struct bt_message *msg,
1404 bool *got_first, GHashTable *stream_states)
1405 {
1406 int status = BT_FUNC_STATUS_OK;
1407 int64_t msg_ns_from_origin;
1408 const struct bt_clock_snapshot *clk_snapshot = NULL;
1409 int ret;
1410
1411 BT_ASSERT(msg);
1412 BT_ASSERT(got_first);
1413
1414 switch (msg->type) {
1415 case BT_MESSAGE_TYPE_EVENT:
1416 {
1417 const struct bt_message_event *event_msg =
1418 (const void *) msg;
1419
1420 clk_snapshot = event_msg->default_cs;
1421 BT_ASSERT_POST_DEV(clk_snapshot,
1422 "Event message has no default clock snapshot: %!+n",
1423 event_msg);
1424 break;
1425 }
1426 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY:
1427 {
1428 const struct bt_message_message_iterator_inactivity *inactivity_msg =
1429 (const void *) msg;
1430
1431 clk_snapshot = inactivity_msg->default_cs;
1432 BT_ASSERT(clk_snapshot);
1433 break;
1434 }
1435 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
1436 case BT_MESSAGE_TYPE_PACKET_END:
1437 {
1438 const struct bt_message_packet *packet_msg =
1439 (const void *) msg;
1440
1441 clk_snapshot = packet_msg->default_cs;
1442 BT_ASSERT_POST_DEV(clk_snapshot,
1443 "Packet message has no default clock snapshot: %!+n",
1444 packet_msg);
1445 break;
1446 }
1447 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
1448 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
1449 {
1450 struct bt_message_discarded_items *msg_disc_items =
1451 (void *) msg;
1452
1453 BT_ASSERT_POST_DEV(msg_disc_items->default_begin_cs &&
1454 msg_disc_items->default_end_cs,
1455 "Discarded events/packets message has no default clock snapshots: %!+n",
1456 msg_disc_items);
1457 ret = bt_clock_snapshot_get_ns_from_origin(
1458 msg_disc_items->default_begin_cs,
1459 &msg_ns_from_origin);
1460 if (ret) {
1461 status = BT_FUNC_STATUS_ERROR;
1462 goto end;
1463 }
1464
1465 if (msg_ns_from_origin >= ns_from_origin) {
1466 *got_first = true;
1467 goto push_msg;
1468 }
1469
1470 ret = bt_clock_snapshot_get_ns_from_origin(
1471 msg_disc_items->default_end_cs,
1472 &msg_ns_from_origin);
1473 if (ret) {
1474 status = BT_FUNC_STATUS_ERROR;
1475 goto end;
1476 }
1477
1478 if (msg_ns_from_origin >= ns_from_origin) {
1479 /*
1480 * The discarded items message's beginning time
1481 * is before the requested seeking time, but its
1482 * end time is after. Modify the message so as
1483 * to set its beginning time to the requested
1484 * seeking time, and make its item count unknown
1485 * as we don't know if items were really
1486 * discarded within the new time range.
1487 */
1488 uint64_t new_begin_raw_value = 0;
1489
1490 ret = bt_clock_class_clock_value_from_ns_from_origin(
1491 msg_disc_items->default_end_cs->clock_class,
1492 ns_from_origin, &new_begin_raw_value);
1493 if (ret) {
1494 status = BT_FUNC_STATUS_ERROR;
1495 goto end;
1496 }
1497
1498 bt_clock_snapshot_set_raw_value(
1499 msg_disc_items->default_begin_cs,
1500 new_begin_raw_value);
1501 msg_disc_items->count.base.avail =
1502 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE;
1503
1504 /*
1505 * It is safe to push it because its beginning
1506 * time is exactly the requested seeking time.
1507 */
1508 goto push_msg;
1509 } else {
1510 goto skip_msg;
1511 }
1512 }
1513 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
1514 case BT_MESSAGE_TYPE_STREAM_END:
1515 {
1516 struct bt_message_stream *stream_msg =
1517 (struct bt_message_stream *) msg;
1518
1519 if (stream_msg->default_cs_state != BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN) {
1520 /* Ignore */
1521 goto skip_msg;
1522 }
1523
1524 clk_snapshot = stream_msg->default_cs;
1525 break;
1526 }
1527 default:
1528 abort();
1529 }
1530
1531 BT_ASSERT(clk_snapshot);
1532 ret = bt_clock_snapshot_get_ns_from_origin(clk_snapshot,
1533 &msg_ns_from_origin);
1534 if (ret) {
1535 status = BT_FUNC_STATUS_ERROR;
1536 goto end;
1537 }
1538
1539 if (msg_ns_from_origin >= ns_from_origin) {
1540 *got_first = true;
1541 goto push_msg;
1542 }
1543
1544 skip_msg:
1545 /* This message won't be sent downstream. */
1546 switch (msg->type) {
1547 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
1548 {
1549 const struct bt_message_stream *stream_msg = (const void *) msg;
1550 struct auto_seek_stream_state *stream_state;
1551
1552 /* Update stream's state: stream began. */
1553 stream_state = create_auto_seek_stream_state();
1554 if (!stream_state) {
1555 status = BT_FUNC_STATUS_MEMORY_ERROR;
1556 goto end;
1557 }
1558
1559 stream_state->state = AUTO_SEEK_STREAM_STATE_STREAM_BEGAN;
1560
1561 if (stream_msg->default_cs_state == BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN) {
1562 stream_state->seen_clock_snapshot = true;
1563 }
1564
1565 BT_ASSERT(!bt_g_hash_table_contains(stream_states, stream_msg->stream));
1566 g_hash_table_insert(stream_states, stream_msg->stream, stream_state);
1567 break;
1568 }
1569 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
1570 {
1571 const struct bt_message_packet *packet_msg =
1572 (const void *) msg;
1573 struct auto_seek_stream_state *stream_state;
1574
1575 /* Update stream's state: packet began. */
1576 stream_state = g_hash_table_lookup(stream_states, packet_msg->packet->stream);
1577 BT_ASSERT(stream_state);
1578
1579 BT_ASSERT(stream_state->state == AUTO_SEEK_STREAM_STATE_STREAM_BEGAN);
1580 stream_state->state = AUTO_SEEK_STREAM_STATE_PACKET_BEGAN;
1581 BT_ASSERT(!stream_state->packet);
1582 stream_state->packet = packet_msg->packet;
1583
1584 if (packet_msg->packet->stream->class->packets_have_beginning_default_clock_snapshot) {
1585 stream_state->seen_clock_snapshot = true;
1586 }
1587
1588 break;
1589 }
1590 case BT_MESSAGE_TYPE_EVENT:
1591 {
1592 const struct bt_message_event *event_msg = (const void *) msg;
1593 struct auto_seek_stream_state *stream_state;
1594
1595 stream_state = g_hash_table_lookup(stream_states,
1596 event_msg->event->packet->stream);
1597 BT_ASSERT(stream_state);
1598
1599 // HELPME: are we sure that event messages have clock snapshots at this point?
1600 stream_state->seen_clock_snapshot = true;
1601
1602 break;
1603 }
1604 case BT_MESSAGE_TYPE_PACKET_END:
1605 {
1606 const struct bt_message_packet *packet_msg =
1607 (const void *) msg;
1608 struct auto_seek_stream_state *stream_state;
1609
1610 /* Update stream's state: packet ended. */
1611 stream_state = g_hash_table_lookup(stream_states, packet_msg->packet->stream);
1612 BT_ASSERT(stream_state);
1613
1614 BT_ASSERT(stream_state->state == AUTO_SEEK_STREAM_STATE_PACKET_BEGAN);
1615 stream_state->state = AUTO_SEEK_STREAM_STATE_STREAM_BEGAN;
1616 BT_ASSERT(stream_state->packet);
1617 stream_state->packet = NULL;
1618
1619 if (packet_msg->packet->stream->class->packets_have_end_default_clock_snapshot) {
1620 stream_state->seen_clock_snapshot = true;
1621 }
1622
1623 break;
1624 }
1625 case BT_MESSAGE_TYPE_STREAM_END:
1626 {
1627 const struct bt_message_stream *stream_msg = (const void *) msg;
1628 struct auto_seek_stream_state *stream_state;
1629
1630 stream_state = g_hash_table_lookup(stream_states, stream_msg->stream);
1631 BT_ASSERT(stream_state);
1632 BT_ASSERT(stream_state->state == AUTO_SEEK_STREAM_STATE_STREAM_BEGAN);
1633 BT_ASSERT(!stream_state->packet);
1634
1635 /* Update stream's state: this stream doesn't exist anymore. */
1636 g_hash_table_remove(stream_states, stream_msg->stream);
1637 break;
1638 }
1639 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
1640 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
1641 {
1642 const struct bt_message_discarded_items *discarded_msg =
1643 (const void *) msg;
1644 struct auto_seek_stream_state *stream_state;
1645
1646 stream_state = g_hash_table_lookup(stream_states, discarded_msg->stream);
1647 BT_ASSERT(stream_state);
1648
1649 if ((msg->type == BT_MESSAGE_TYPE_DISCARDED_EVENTS && discarded_msg->stream->class->discarded_events_have_default_clock_snapshots) ||
1650 (msg->type == BT_MESSAGE_TYPE_DISCARDED_PACKETS && discarded_msg->stream->class->discarded_packets_have_default_clock_snapshots)) {
1651 stream_state->seen_clock_snapshot = true;
1652 }
1653
1654 break;
1655 }
1656 default:
1657 break;
1658 }
1659
1660 bt_object_put_no_null_check(msg);
1661 msg = NULL;
1662 goto end;
1663
1664 push_msg:
1665 g_queue_push_tail(iterator->auto_seek.msgs, (void *) msg);
1666 msg = NULL;
1667
1668 end:
1669 BT_ASSERT(!msg || status != BT_FUNC_STATUS_OK);
1670 return status;
1671 }
1672
1673 static
1674 int find_message_ge_ns_from_origin(
1675 struct bt_self_component_port_input_message_iterator *iterator,
1676 int64_t ns_from_origin, GHashTable *stream_states)
1677 {
1678 int status;
1679 enum bt_self_component_port_input_message_iterator_state init_state =
1680 iterator->state;
1681 const struct bt_message *messages[MSG_BATCH_SIZE];
1682 uint64_t user_count = 0;
1683 uint64_t i;
1684 bool got_first = false;
1685
1686 BT_ASSERT(iterator);
1687 memset(&messages[0], 0, sizeof(messages[0]) * MSG_BATCH_SIZE);
1688
1689 /*
1690 * Make this iterator temporarily active (not seeking) to call
1691 * the "next" method.
1692 */
1693 set_self_comp_port_input_msg_iterator_state(iterator,
1694 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE);
1695
1696 BT_ASSERT(iterator->methods.next);
1697
1698 while (!got_first) {
1699 /*
1700 * Call the user's "next" method to get the next
1701 * messages and status.
1702 */
1703 status = call_iterator_next_method(iterator,
1704 &messages[0], MSG_BATCH_SIZE, &user_count);
1705 BT_LOGD("User method returned: status=%s",
1706 bt_common_func_status_string(status));
1707 if (status < 0) {
1708 BT_LIB_LOGW_APPEND_CAUSE(
1709 "Component input port message iterator's \"next\" method failed: "
1710 "%![iter-]+i, status=%s",
1711 iterator, bt_common_func_status_string(status));
1712 }
1713
1714 /*
1715 * The user's "next" method must not do any action which
1716 * would change the iterator's state.
1717 */
1718 BT_ASSERT(iterator->state ==
1719 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE);
1720
1721 switch (status) {
1722 case BT_FUNC_STATUS_OK:
1723 BT_ASSERT_POST_DEV(user_count <= MSG_BATCH_SIZE,
1724 "Invalid returned message count: greater than "
1725 "batch size: count=%" PRIu64 ", batch-size=%u",
1726 user_count, MSG_BATCH_SIZE);
1727 break;
1728 case BT_FUNC_STATUS_AGAIN:
1729 case BT_FUNC_STATUS_ERROR:
1730 case BT_FUNC_STATUS_MEMORY_ERROR:
1731 case BT_FUNC_STATUS_END:
1732 goto end;
1733 default:
1734 abort();
1735 }
1736
1737 for (i = 0; i < user_count; i++) {
1738 if (got_first) {
1739 g_queue_push_tail(iterator->auto_seek.msgs,
1740 (void *) messages[i]);
1741 messages[i] = NULL;
1742 continue;
1743 }
1744
1745 status = auto_seek_handle_message(iterator,
1746 ns_from_origin, messages[i], &got_first,
1747 stream_states);
1748 if (status == BT_FUNC_STATUS_OK) {
1749 /* Message was either pushed or moved */
1750 messages[i] = NULL;
1751 } else {
1752 goto end;
1753 }
1754 }
1755 }
1756
1757 end:
1758 for (i = 0; i < user_count; i++) {
1759 if (messages[i]) {
1760 bt_object_put_no_null_check(messages[i]);
1761 }
1762 }
1763
1764 set_self_comp_port_input_msg_iterator_state(iterator, init_state);
1765 return status;
1766 }
1767
1768 /*
1769 * This function is installed as the iterator's next callback after we have
1770 * auto-seeked (seeked to the beginning and fast-forwarded) to send the
1771 * messages saved in iterator->auto_seek.msgs. Once this is done, the original
1772 * next callback is put back.
1773 */
1774
1775 static
1776 enum bt_component_class_message_iterator_next_method_status post_auto_seek_next(
1777 struct bt_self_component_port_input_message_iterator *iterator,
1778 bt_message_array_const msgs, uint64_t capacity,
1779 uint64_t *count)
1780 {
1781 BT_ASSERT(!g_queue_is_empty(iterator->auto_seek.msgs));
1782 *count = 0;
1783
1784 /*
1785 * Move auto-seek messages to the output array (which is this
1786 * iterator's base message array).
1787 */
1788 while (capacity > 0 && !g_queue_is_empty(iterator->auto_seek.msgs)) {
1789 msgs[*count] = g_queue_pop_head(iterator->auto_seek.msgs);
1790 capacity--;
1791 (*count)++;
1792 }
1793
1794 BT_ASSERT(*count > 0);
1795
1796 if (g_queue_is_empty(iterator->auto_seek.msgs)) {
1797 /* No more auto-seek messages, restore user's next callback. */
1798 BT_ASSERT(iterator->auto_seek.original_next_callback);
1799 iterator->methods.next = iterator->auto_seek.original_next_callback;
1800 iterator->auto_seek.original_next_callback = NULL;
1801 }
1802
1803 return BT_FUNC_STATUS_OK;
1804 }
1805
1806 static inline
1807 int clock_raw_value_from_ns_from_origin(const bt_clock_class *clock_class,
1808 int64_t ns_from_origin, uint64_t *raw_value)
1809 {
1810
1811 int64_t cc_offset_s = clock_class->offset_seconds;
1812 uint64_t cc_offset_cycles = clock_class->offset_cycles;
1813 uint64_t cc_freq = clock_class->frequency;
1814
1815 return bt_common_clock_value_from_ns_from_origin(cc_offset_s,
1816 cc_offset_cycles, cc_freq, ns_from_origin, raw_value);
1817 }
1818
1819
1820 enum bt_message_iterator_seek_ns_from_origin_status
1821 bt_self_component_port_input_message_iterator_seek_ns_from_origin(
1822 struct bt_self_component_port_input_message_iterator *iterator,
1823 int64_t ns_from_origin)
1824 {
1825 int status;
1826 GHashTable *stream_states = NULL;
1827
1828 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
1829 BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator);
1830 BT_ASSERT_PRE(
1831 bt_component_borrow_graph(iterator->upstream_component)->config_state !=
1832 BT_GRAPH_CONFIGURATION_STATE_CONFIGURING,
1833 "Graph is not configured: %!+g",
1834 bt_component_borrow_graph(iterator->upstream_component));
1835 BT_ASSERT_PRE(
1836 bt_self_component_port_input_message_iterator_can_seek_ns_from_origin(
1837 iterator, ns_from_origin),
1838 "Message iterator cannot seek nanoseconds from origin: %!+i, "
1839 "ns-from-origin=%" PRId64, iterator, ns_from_origin);
1840 set_self_comp_port_input_msg_iterator_state(iterator,
1841 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_SEEKING);
1842
1843 /*
1844 * We are seeking, reset our expectations about how the following
1845 * messages should look like.
1846 */
1847 reset_iterator_expectations(iterator);
1848
1849 if (iterator->methods.seek_ns_from_origin) {
1850 /* The iterator knows how to seek to a particular time, let it handle this. */
1851 BT_LIB_LOGD("Calling user's \"seek nanoseconds from origin\" method: "
1852 "%![iter-]+i, ns=%" PRId64, iterator, ns_from_origin);
1853 status = iterator->methods.seek_ns_from_origin(iterator,
1854 ns_from_origin);
1855 BT_LOGD("User method returned: status=%s",
1856 bt_common_func_status_string(status));
1857 BT_ASSERT_POST(status == BT_FUNC_STATUS_OK ||
1858 status == BT_FUNC_STATUS_ERROR ||
1859 status == BT_FUNC_STATUS_MEMORY_ERROR ||
1860 status == BT_FUNC_STATUS_AGAIN,
1861 "Unexpected status: %![iter-]+i, status=%s",
1862 iterator, bt_common_func_status_string(status));
1863 if (status < 0) {
1864 BT_LIB_LOGW_APPEND_CAUSE(
1865 "Component input port message iterator's \"seek nanoseconds from origin\" method failed: "
1866 "%![iter-]+i, status=%s",
1867 iterator, bt_common_func_status_string(status));
1868 }
1869 } else {
1870 /*
1871 * The iterator doesn't know how to seek to a particular time. We will
1872 * seek to the beginning and fast forward to the right place.
1873 */
1874 BT_ASSERT(iterator->methods.can_seek_beginning(iterator));
1875 BT_ASSERT(iterator->methods.seek_beginning);
1876 BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i",
1877 iterator);
1878 status = iterator->methods.seek_beginning(iterator);
1879 BT_LOGD("User method returned: status=%s",
1880 bt_common_func_status_string(status));
1881 BT_ASSERT_POST(status == BT_FUNC_STATUS_OK ||
1882 status == BT_FUNC_STATUS_ERROR ||
1883 status == BT_FUNC_STATUS_MEMORY_ERROR ||
1884 status == BT_FUNC_STATUS_AGAIN,
1885 "Unexpected status: %![iter-]+i, status=%s",
1886 iterator, bt_common_func_status_string(status));
1887 if (status < 0) {
1888 BT_LIB_LOGW_APPEND_CAUSE(
1889 "Component input port message iterator's \"seek beginning\" method failed: "
1890 "%![iter-]+i, status=%s",
1891 iterator, bt_common_func_status_string(status));
1892 }
1893
1894 switch (status) {
1895 case BT_FUNC_STATUS_OK:
1896 break;
1897 case BT_FUNC_STATUS_ERROR:
1898 case BT_FUNC_STATUS_MEMORY_ERROR:
1899 case BT_FUNC_STATUS_AGAIN:
1900 goto end;
1901 default:
1902 abort();
1903 }
1904
1905 /*
1906 * Find the first message which has a default clock
1907 * snapshot greater than or equal to the requested
1908 * seeking time, and move the received messages from
1909 * this point in the batch to this iterator's auto-seek
1910 * message queue.
1911 */
1912 while (!g_queue_is_empty(iterator->auto_seek.msgs)) {
1913 bt_object_put_no_null_check(
1914 g_queue_pop_tail(iterator->auto_seek.msgs));
1915 }
1916
1917 stream_states = create_auto_seek_stream_states();
1918 if (!stream_states) {
1919 BT_LIB_LOGE_APPEND_CAUSE(
1920 "Failed to allocate one GHashTable.");
1921 status = BT_FUNC_STATUS_MEMORY_ERROR;
1922 goto end;
1923 }
1924
1925 status = find_message_ge_ns_from_origin(iterator,
1926 ns_from_origin, stream_states);
1927 switch (status) {
1928 case BT_FUNC_STATUS_OK:
1929 case BT_FUNC_STATUS_END:
1930 {
1931 GHashTableIter iter;
1932 gpointer key, value;
1933
1934 /*
1935 * If some streams exist at the seek time, prepend the
1936 * required messages to put those streams in the right
1937 * state.
1938 */
1939 g_hash_table_iter_init(&iter, stream_states);
1940 while (g_hash_table_iter_next (&iter, &key, &value)) {
1941 const bt_stream *stream = key;
1942 struct auto_seek_stream_state *stream_state =
1943 (struct auto_seek_stream_state *) value;
1944 bt_message *msg;
1945 const bt_clock_class *clock_class = bt_stream_class_borrow_default_clock_class_const(
1946 bt_stream_borrow_class_const(stream));
1947 /* Initialize to silence maybe-uninitialized warning. */
1948 uint64_t raw_value = 0;
1949
1950 /*
1951 * If we haven't seen a message with a clock snapshot, we don't know if our seek time is within
1952 * the clock's range, so it wouldn't be safe to try to convert ns_from_origin to a clock value.
1953 *
1954 * Also, it would be a bit of a lie to generate a stream begin message with the seek time as its
1955 * clock snapshot, because we don't really know if the stream existed at that time. If we have
1956 * seen a message with a clock snapshot in our seeking, then we are sure that the
1957 * seek time is not below the clock range, and we know the stream was active at that
1958 * time (and that we cut it short).
1959 */
1960 if (stream_state->seen_clock_snapshot) {
1961 if (clock_raw_value_from_ns_from_origin(clock_class, ns_from_origin, &raw_value) != 0) {
1962 BT_LIB_LOGW("Could not convert nanoseconds from origin to clock value: ns-from-origin=%" PRId64 ", %![cc-]+K",
1963 ns_from_origin, clock_class);
1964 status = BT_FUNC_STATUS_ERROR;
1965 goto end;
1966 }
1967 }
1968
1969 switch (stream_state->state) {
1970 case AUTO_SEEK_STREAM_STATE_PACKET_BEGAN:
1971 BT_ASSERT(stream_state->packet);
1972 BT_LIB_LOGD("Creating packet message: %![packet-]+a", stream_state->packet);
1973
1974 if (stream->class->packets_have_beginning_default_clock_snapshot) {
1975 /*
1976 * If we are in the PACKET_BEGAN state, it means we have seen a "packet beginning"
1977 * message. If "packet beginning" packets have clock snapshots, then we must have
1978 * seen a clock snapshot.
1979 */
1980 BT_ASSERT(stream_state->seen_clock_snapshot);
1981
1982 msg = bt_message_packet_beginning_create_with_default_clock_snapshot(
1983 (bt_self_message_iterator *) iterator, stream_state->packet, raw_value);
1984 } else {
1985 msg = bt_message_packet_beginning_create((bt_self_message_iterator *) iterator,
1986 stream_state->packet);
1987 }
1988
1989 if (!msg) {
1990 status = BT_FUNC_STATUS_MEMORY_ERROR;
1991 goto end;
1992 }
1993
1994 g_queue_push_head(iterator->auto_seek.msgs, msg);
1995 msg = NULL;
1996 /* fall-thru */
1997
1998 case AUTO_SEEK_STREAM_STATE_STREAM_BEGAN:
1999 msg = bt_message_stream_beginning_create(
2000 (bt_self_message_iterator *) iterator, stream);
2001 if (!msg) {
2002 status = BT_FUNC_STATUS_MEMORY_ERROR;
2003 goto end;
2004 }
2005
2006 if (stream_state->seen_clock_snapshot) {
2007 bt_message_stream_beginning_set_default_clock_snapshot(msg, raw_value);
2008 }
2009
2010 g_queue_push_head(iterator->auto_seek.msgs, msg);
2011 msg = NULL;
2012 break;
2013 }
2014 }
2015
2016 /*
2017 * If there are messages in the auto-seek
2018 * message queue, replace the user's "next"
2019 * method with a custom, temporary "next" method
2020 * which returns them.
2021 */
2022 if (!g_queue_is_empty(iterator->auto_seek.msgs)) {
2023 BT_ASSERT(!iterator->auto_seek.original_next_callback);
2024 iterator->auto_seek.original_next_callback = iterator->methods.next;
2025
2026 iterator->methods.next =
2027 (bt_self_component_port_input_message_iterator_next_method)
2028 post_auto_seek_next;
2029 }
2030
2031 /*
2032 * `BT_FUNC_STATUS_END` becomes
2033 * `BT_FUNC_STATUS_OK`: the next
2034 * time this iterator's "next" method is called,
2035 * it will return
2036 * `BT_FUNC_STATUS_END`.
2037 */
2038 status = BT_FUNC_STATUS_OK;
2039 break;
2040 }
2041 case BT_FUNC_STATUS_ERROR:
2042 case BT_FUNC_STATUS_MEMORY_ERROR:
2043 case BT_FUNC_STATUS_AGAIN:
2044 goto end;
2045 default:
2046 abort();
2047 }
2048 }
2049
2050 /*
2051 * The following messages returned by the next method (including
2052 * post_auto_seek_next) must be after (or at) `ns_from_origin`.
2053 */
2054 iterator->last_ns_from_origin = ns_from_origin;
2055
2056 end:
2057 if (stream_states) {
2058 destroy_auto_seek_stream_states(stream_states);
2059 stream_states = NULL;
2060 }
2061
2062 set_iterator_state_after_seeking(iterator, status);
2063 return status;
2064 }
2065
2066 static inline
2067 bt_self_component_port_input_message_iterator *
2068 borrow_output_port_message_iterator_upstream_iterator(
2069 struct bt_port_output_message_iterator *iterator)
2070 {
2071 struct bt_component_class_sink_colander_priv_data *colander_data;
2072
2073 BT_ASSERT(iterator);
2074 colander_data = (void *) iterator->colander->parent.user_data;
2075 BT_ASSERT(colander_data);
2076 BT_ASSERT(colander_data->msg_iter);
2077 return colander_data->msg_iter;
2078 }
2079
2080 bt_bool bt_port_output_message_iterator_can_seek_ns_from_origin(
2081 struct bt_port_output_message_iterator *iterator,
2082 int64_t ns_from_origin)
2083 {
2084 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
2085 return bt_self_component_port_input_message_iterator_can_seek_ns_from_origin(
2086 borrow_output_port_message_iterator_upstream_iterator(
2087 iterator), ns_from_origin);
2088 }
2089
2090 bt_bool bt_port_output_message_iterator_can_seek_beginning(
2091 struct bt_port_output_message_iterator *iterator)
2092 {
2093 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
2094 return bt_self_component_port_input_message_iterator_can_seek_beginning(
2095 borrow_output_port_message_iterator_upstream_iterator(
2096 iterator));
2097 }
2098
2099 enum bt_message_iterator_seek_ns_from_origin_status
2100 bt_port_output_message_iterator_seek_ns_from_origin(
2101 struct bt_port_output_message_iterator *iterator,
2102 int64_t ns_from_origin)
2103 {
2104 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
2105 return bt_self_component_port_input_message_iterator_seek_ns_from_origin(
2106 borrow_output_port_message_iterator_upstream_iterator(iterator),
2107 ns_from_origin);
2108 }
2109
2110 enum bt_message_iterator_seek_beginning_status
2111 bt_port_output_message_iterator_seek_beginning(
2112 struct bt_port_output_message_iterator *iterator)
2113 {
2114 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
2115 return bt_self_component_port_input_message_iterator_seek_beginning(
2116 borrow_output_port_message_iterator_upstream_iterator(
2117 iterator));
2118 }
2119
2120 bt_bool bt_self_message_iterator_is_interrupted(
2121 const struct bt_self_message_iterator *self_msg_iter)
2122 {
2123 const struct bt_self_component_port_input_message_iterator *iterator =
2124 (const void *) self_msg_iter;
2125
2126 BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator");
2127 return (bt_bool) bt_graph_is_interrupted(iterator->graph);
2128 }
2129
2130 void bt_port_output_message_iterator_get_ref(
2131 const struct bt_port_output_message_iterator *iterator)
2132 {
2133 bt_object_get_ref(iterator);
2134 }
2135
2136 void bt_port_output_message_iterator_put_ref(
2137 const struct bt_port_output_message_iterator *iterator)
2138 {
2139 bt_object_put_ref(iterator);
2140 }
2141
2142 void bt_self_component_port_input_message_iterator_get_ref(
2143 const struct bt_self_component_port_input_message_iterator *iterator)
2144 {
2145 bt_object_get_ref(iterator);
2146 }
2147
2148 void bt_self_component_port_input_message_iterator_put_ref(
2149 const struct bt_self_component_port_input_message_iterator *iterator)
2150 {
2151 bt_object_put_ref(iterator);
2152 }
This page took 0.129773 seconds and 4 git commands to generate.