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