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