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