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