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