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