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