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