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