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