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 | ||
d6e69534 | 24 | #define BT_LOG_TAG "MSG-ITER" |
5af447e5 PP |
25 | #include <babeltrace/lib-logging-internal.h> |
26 | ||
3d9990ac | 27 | #include <babeltrace/compiler-internal.h> |
5b9e151d PP |
28 | #include <babeltrace/trace-ir/clock-class-internal.h> |
29 | #include <babeltrace/trace-ir/clock-snapshot-internal.h> | |
c6bd8523 | 30 | #include <babeltrace/trace-ir/field.h> |
40f4ba76 | 31 | #include <babeltrace/trace-ir/event-const.h> |
56e18c4c | 32 | #include <babeltrace/trace-ir/event-internal.h> |
40f4ba76 | 33 | #include <babeltrace/trace-ir/packet-const.h> |
56e18c4c PP |
34 | #include <babeltrace/trace-ir/packet-internal.h> |
35 | #include <babeltrace/trace-ir/stream-internal.h> | |
0d72b8c3 | 36 | #include <babeltrace/graph/connection-const.h> |
bd14d768 | 37 | #include <babeltrace/graph/connection-internal.h> |
0d72b8c3 | 38 | #include <babeltrace/graph/component-const.h> |
e5be10ef | 39 | #include <babeltrace/graph/component-internal.h> |
b2e0c907 PP |
40 | #include <babeltrace/graph/component-source-internal.h> |
41 | #include <babeltrace/graph/component-class-internal.h> | |
8ed535b5 | 42 | #include <babeltrace/graph/component-class-sink-colander-internal.h> |
0d72b8c3 | 43 | #include <babeltrace/graph/component-sink-const.h> |
7474e7d3 | 44 | #include <babeltrace/graph/component-sink-internal.h> |
d6e69534 | 45 | #include <babeltrace/graph/message-const.h> |
7474e7d3 | 46 | #include <babeltrace/graph/message-iterator-const.h> |
d6e69534 PP |
47 | #include <babeltrace/graph/message-iterator-internal.h> |
48 | #include <babeltrace/graph/self-component-port-input-message-iterator.h> | |
49 | #include <babeltrace/graph/port-output-message-iterator.h> | |
50 | #include <babeltrace/graph/message-internal.h> | |
51 | #include <babeltrace/graph/message-event-const.h> | |
52 | #include <babeltrace/graph/message-event-internal.h> | |
5df26c89 PP |
53 | #include <babeltrace/graph/message-packet-beginning-const.h> |
54 | #include <babeltrace/graph/message-packet-end-const.h> | |
d6e69534 | 55 | #include <babeltrace/graph/message-packet-internal.h> |
5df26c89 PP |
56 | #include <babeltrace/graph/message-stream-beginning-const.h> |
57 | #include <babeltrace/graph/message-stream-end-const.h> | |
d6e69534 | 58 | #include <babeltrace/graph/message-stream-internal.h> |
b9fd9cbb | 59 | #include <babeltrace/graph/message-message-iterator-inactivity-internal.h> |
16663a5e PP |
60 | #include <babeltrace/graph/message-discarded-items-internal.h> |
61 | #include <babeltrace/graph/message-stream-activity-internal.h> | |
0d72b8c3 PP |
62 | #include <babeltrace/graph/port-const.h> |
63 | #include <babeltrace/graph/graph.h> | |
64 | #include <babeltrace/graph/graph-const.h> | |
8ed535b5 | 65 | #include <babeltrace/graph/graph-internal.h> |
c55a9f58 | 66 | #include <babeltrace/types.h> |
f6ccaed9 | 67 | #include <babeltrace/assert-internal.h> |
f42867e2 | 68 | #include <babeltrace/assert-pre-internal.h> |
fa054faf | 69 | #include <stdint.h> |
2ec84d26 | 70 | #include <inttypes.h> |
0fbb9a9f | 71 | #include <stdlib.h> |
3230ee6b | 72 | |
d4393e08 PP |
73 | /* |
74 | * TODO: Use graph's state (number of active iterators, etc.) and | |
75 | * possibly system specifications to make a better guess than this. | |
76 | */ | |
d6e69534 | 77 | #define MSG_BATCH_SIZE 15 |
d4393e08 | 78 | |
7474e7d3 PP |
79 | #define BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(_iter) \ |
80 | BT_ASSERT_PRE((_iter)->state == BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE || \ | |
81 | (_iter)->state == BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED || \ | |
82 | (_iter)->state == BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN || \ | |
83 | (_iter)->state == BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR, \ | |
84 | "Message iterator is in the wrong state: %!+i", _iter) | |
47e5a032 | 85 | |
0952128e | 86 | BT_ASSERT_PRE_FUNC |
d0fea130 PP |
87 | static inline |
88 | void _set_self_comp_port_input_msg_iterator_state( | |
89 | struct bt_self_component_port_input_message_iterator *iterator, | |
90 | enum bt_self_component_port_input_message_iterator_state state) | |
91 | { | |
92 | BT_ASSERT(iterator); | |
7474e7d3 | 93 | BT_LIB_LOGD("Updating message iterator's state: new-state=%s", |
d0fea130 PP |
94 | bt_self_component_port_input_message_iterator_state_string(state)); |
95 | iterator->state = state; | |
96 | } | |
97 | ||
98 | #ifdef BT_DEV_MODE | |
99 | # define set_self_comp_port_input_msg_iterator_state _set_self_comp_port_input_msg_iterator_state | |
100 | #else | |
c411e134 | 101 | # define set_self_comp_port_input_msg_iterator_state(_a, _b) ((void) _a); ((void) _b); |
d0fea130 PP |
102 | #endif |
103 | ||
8ed535b5 | 104 | static |
d6e69534 | 105 | void destroy_base_message_iterator(struct bt_object *obj) |
8ed535b5 | 106 | { |
d6e69534 | 107 | struct bt_message_iterator *iterator = (void *) obj; |
d4393e08 PP |
108 | |
109 | BT_ASSERT(iterator); | |
110 | ||
d6e69534 PP |
111 | if (iterator->msgs) { |
112 | g_ptr_array_free(iterator->msgs, TRUE); | |
113 | iterator->msgs = NULL; | |
d4393e08 PP |
114 | } |
115 | ||
116 | g_free(iterator); | |
8ed535b5 PP |
117 | } |
118 | ||
47e5a032 | 119 | static |
d6e69534 | 120 | void bt_self_component_port_input_message_iterator_destroy(struct bt_object *obj) |
47e5a032 | 121 | { |
d6e69534 | 122 | struct bt_self_component_port_input_message_iterator *iterator; |
8738a040 | 123 | |
f6ccaed9 | 124 | BT_ASSERT(obj); |
d3eb6e8f | 125 | |
bd14d768 | 126 | /* |
d6e69534 | 127 | * The message iterator's reference count is 0 if we're |
bd14d768 PP |
128 | * here. Increment it to avoid a double-destroy (possibly |
129 | * infinitely recursive). This could happen for example if the | |
d6e69534 | 130 | * message iterator's finalization function does |
d94d92ac PP |
131 | * bt_object_get_ref() (or anything that causes |
132 | * bt_object_get_ref() to be called) on itself (ref. count goes | |
133 | * from 0 to 1), and then bt_object_put_ref(): the reference | |
134 | * count would go from 1 to 0 again and this function would be | |
135 | * called again. | |
bd14d768 | 136 | */ |
3fea54f6 | 137 | obj->ref_count++; |
07245ac2 | 138 | iterator = (void *) obj; |
d6e69534 | 139 | BT_LIB_LOGD("Destroying self component input port message iterator object: " |
d94d92ac | 140 | "%!+i", iterator); |
d0fea130 | 141 | bt_self_component_port_input_message_iterator_try_finalize(iterator); |
d3eb6e8f | 142 | |
bd14d768 PP |
143 | if (iterator->connection) { |
144 | /* | |
145 | * Remove ourself from the originating connection so | |
146 | * that it does not try to finalize a dangling pointer | |
147 | * later. | |
148 | */ | |
149 | bt_connection_remove_iterator(iterator->connection, iterator); | |
d94d92ac | 150 | iterator->connection = NULL; |
bd14d768 PP |
151 | } |
152 | ||
7474e7d3 | 153 | if (iterator->auto_seek_msgs) { |
5b9e151d PP |
154 | while (!g_queue_is_empty(iterator->auto_seek_msgs)) { |
155 | bt_object_put_no_null_check( | |
156 | g_queue_pop_tail(iterator->auto_seek_msgs)); | |
7474e7d3 PP |
157 | } |
158 | ||
5b9e151d | 159 | g_queue_free(iterator->auto_seek_msgs); |
7474e7d3 PP |
160 | iterator->auto_seek_msgs = NULL; |
161 | } | |
162 | ||
d6e69534 | 163 | destroy_base_message_iterator(obj); |
47e5a032 JG |
164 | } |
165 | ||
bd14d768 | 166 | BT_HIDDEN |
d0fea130 | 167 | void bt_self_component_port_input_message_iterator_try_finalize( |
d6e69534 | 168 | struct bt_self_component_port_input_message_iterator *iterator) |
bd14d768 | 169 | { |
d94d92ac PP |
170 | typedef void (*method_t)(void *); |
171 | ||
bd14d768 | 172 | struct bt_component_class *comp_class = NULL; |
d94d92ac | 173 | method_t method = NULL; |
bd14d768 | 174 | |
f6ccaed9 | 175 | BT_ASSERT(iterator); |
bd14d768 PP |
176 | |
177 | switch (iterator->state) { | |
d6e69534 | 178 | case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED: |
088d4023 | 179 | /* Skip user finalization if user initialization failed */ |
d6e69534 | 180 | BT_LIB_LOGD("Not finalizing non-initialized message iterator: " |
d94d92ac | 181 | "%!+i", iterator); |
d0fea130 | 182 | goto end; |
d6e69534 | 183 | case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED: |
bd14d768 | 184 | /* Already finalized */ |
d6e69534 | 185 | BT_LIB_LOGD("Not finalizing message iterator: already finalized: " |
d94d92ac | 186 | "%!+i", iterator); |
d0fea130 PP |
187 | goto end; |
188 | case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZING: | |
189 | /* Already finalized */ | |
190 | BT_LIB_LOGF("Message iterator is already being finalized: " | |
191 | "%!+i", iterator); | |
192 | abort(); | |
bd14d768 PP |
193 | default: |
194 | break; | |
195 | } | |
196 | ||
d6e69534 | 197 | BT_LIB_LOGD("Finalizing message iterator: %!+i", iterator); |
d0fea130 PP |
198 | set_self_comp_port_input_msg_iterator_state(iterator, |
199 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZING); | |
f6ccaed9 | 200 | BT_ASSERT(iterator->upstream_component); |
bd14d768 PP |
201 | comp_class = iterator->upstream_component->class; |
202 | ||
203 | /* Call user-defined destroy method */ | |
204 | switch (comp_class->type) { | |
205 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
206 | { | |
d94d92ac PP |
207 | struct bt_component_class_source *src_comp_cls = |
208 | (void *) comp_class; | |
bd14d768 | 209 | |
d6e69534 | 210 | method = (method_t) src_comp_cls->methods.msg_iter_finalize; |
bd14d768 PP |
211 | break; |
212 | } | |
213 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
214 | { | |
d94d92ac PP |
215 | struct bt_component_class_filter *flt_comp_cls = |
216 | (void *) comp_class; | |
bd14d768 | 217 | |
d6e69534 | 218 | method = (method_t) flt_comp_cls->methods.msg_iter_finalize; |
bd14d768 PP |
219 | break; |
220 | } | |
221 | default: | |
222 | /* Unreachable */ | |
0fbb9a9f | 223 | abort(); |
bd14d768 PP |
224 | } |
225 | ||
d94d92ac PP |
226 | if (method) { |
227 | BT_LIB_LOGD("Calling user's finalization method: %!+i", | |
5af447e5 | 228 | iterator); |
d94d92ac | 229 | method(iterator); |
bd14d768 PP |
230 | } |
231 | ||
bd14d768 PP |
232 | iterator->upstream_component = NULL; |
233 | iterator->upstream_port = NULL; | |
d0fea130 PP |
234 | set_self_comp_port_input_msg_iterator_state(iterator, |
235 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED); | |
d6e69534 | 236 | BT_LIB_LOGD("Finalized message iterator: %!+i", iterator); |
d0fea130 PP |
237 | |
238 | end: | |
239 | return; | |
bd14d768 PP |
240 | } |
241 | ||
242 | BT_HIDDEN | |
d6e69534 PP |
243 | void bt_self_component_port_input_message_iterator_set_connection( |
244 | struct bt_self_component_port_input_message_iterator *iterator, | |
bd14d768 PP |
245 | struct bt_connection *connection) |
246 | { | |
f6ccaed9 | 247 | BT_ASSERT(iterator); |
bd14d768 | 248 | iterator->connection = connection; |
d6e69534 | 249 | BT_LIB_LOGV("Set message iterator's connection: " |
d94d92ac | 250 | "%![iter-]+i, %![conn-]+x", iterator, connection); |
bd14d768 PP |
251 | } |
252 | ||
90157d89 | 253 | static |
d6e69534 PP |
254 | int init_message_iterator(struct bt_message_iterator *iterator, |
255 | enum bt_message_iterator_type type, | |
90157d89 PP |
256 | bt_object_release_func destroy) |
257 | { | |
d4393e08 PP |
258 | int ret = 0; |
259 | ||
3fea54f6 | 260 | bt_object_init_shared(&iterator->base, destroy); |
90157d89 | 261 | iterator->type = type; |
d6e69534 PP |
262 | iterator->msgs = g_ptr_array_new(); |
263 | if (!iterator->msgs) { | |
d4393e08 PP |
264 | BT_LOGE_STR("Failed to allocate a GPtrArray."); |
265 | ret = -1; | |
266 | goto end; | |
267 | } | |
268 | ||
d6e69534 | 269 | g_ptr_array_set_size(iterator->msgs, MSG_BATCH_SIZE); |
d4393e08 PP |
270 | |
271 | end: | |
272 | return ret; | |
90157d89 PP |
273 | } |
274 | ||
7474e7d3 PP |
275 | static |
276 | bt_bool can_seek_ns_from_origin_true( | |
277 | struct bt_self_component_port_input_message_iterator *iterator, | |
278 | int64_t ns_from_origin) | |
279 | { | |
280 | return BT_TRUE; | |
281 | } | |
282 | ||
283 | static | |
284 | bt_bool can_seek_beginning_true( | |
285 | struct bt_self_component_port_input_message_iterator *iterator) | |
286 | { | |
287 | return BT_TRUE; | |
288 | } | |
289 | ||
d94d92ac | 290 | static |
d6e69534 PP |
291 | struct bt_self_component_port_input_message_iterator * |
292 | bt_self_component_port_input_message_iterator_create_initial( | |
3230ee6b | 293 | struct bt_component *upstream_comp, |
d94d92ac | 294 | struct bt_port *upstream_port) |
47e5a032 | 295 | { |
d4393e08 | 296 | int ret; |
d6e69534 | 297 | struct bt_self_component_port_input_message_iterator *iterator = NULL; |
47e5a032 | 298 | |
f6ccaed9 PP |
299 | BT_ASSERT(upstream_comp); |
300 | BT_ASSERT(upstream_port); | |
f6ccaed9 | 301 | BT_ASSERT(bt_port_is_connected(upstream_port)); |
d6e69534 | 302 | BT_LIB_LOGD("Creating initial message iterator on self component input port: " |
d94d92ac PP |
303 | "%![up-comp-]+c, %![up-port-]+p", upstream_comp, upstream_port); |
304 | BT_ASSERT(bt_component_get_class_type(upstream_comp) == | |
305 | BT_COMPONENT_CLASS_TYPE_SOURCE || | |
306 | bt_component_get_class_type(upstream_comp) == | |
307 | BT_COMPONENT_CLASS_TYPE_FILTER); | |
308 | iterator = g_new0( | |
d6e69534 | 309 | struct bt_self_component_port_input_message_iterator, 1); |
47e5a032 | 310 | if (!iterator) { |
d94d92ac | 311 | BT_LOGE_STR("Failed to allocate one self component input port " |
d6e69534 | 312 | "message iterator."); |
73d5c1ad | 313 | goto end; |
47e5a032 JG |
314 | } |
315 | ||
d6e69534 PP |
316 | ret = init_message_iterator((void *) iterator, |
317 | BT_MESSAGE_ITERATOR_TYPE_SELF_COMPONENT_PORT_INPUT, | |
318 | bt_self_component_port_input_message_iterator_destroy); | |
d4393e08 | 319 | if (ret) { |
d6e69534 | 320 | /* init_message_iterator() logs errors */ |
d94d92ac | 321 | BT_OBJECT_PUT_REF_AND_RESET(iterator); |
d4393e08 PP |
322 | goto end; |
323 | } | |
3230ee6b | 324 | |
5b9e151d | 325 | iterator->auto_seek_msgs = g_queue_new(); |
7474e7d3 | 326 | if (!iterator->auto_seek_msgs) { |
5b9e151d | 327 | BT_LOGE_STR("Failed to allocate a GQueue."); |
7474e7d3 | 328 | ret = -1; |
73d5c1ad | 329 | goto end; |
3230ee6b PP |
330 | } |
331 | ||
bd14d768 PP |
332 | iterator->upstream_component = upstream_comp; |
333 | iterator->upstream_port = upstream_port; | |
d94d92ac | 334 | iterator->connection = iterator->upstream_port->connection; |
5c563278 | 335 | iterator->graph = bt_component_borrow_graph(upstream_comp); |
d0fea130 PP |
336 | set_self_comp_port_input_msg_iterator_state(iterator, |
337 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED); | |
7474e7d3 PP |
338 | |
339 | switch (iterator->upstream_component->class->type) { | |
340 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
341 | { | |
342 | struct bt_component_class_source *src_comp_cls = | |
343 | (void *) iterator->upstream_component->class; | |
344 | ||
345 | iterator->methods.next = | |
346 | (bt_self_component_port_input_message_iterator_next_method) | |
347 | src_comp_cls->methods.msg_iter_next; | |
348 | iterator->methods.seek_ns_from_origin = | |
349 | (bt_self_component_port_input_message_iterator_seek_ns_from_origin_method) | |
350 | src_comp_cls->methods.msg_iter_seek_ns_from_origin; | |
351 | iterator->methods.seek_beginning = | |
352 | (bt_self_component_port_input_message_iterator_seek_beginning_method) | |
353 | src_comp_cls->methods.msg_iter_seek_beginning; | |
354 | iterator->methods.can_seek_ns_from_origin = | |
355 | (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method) | |
356 | src_comp_cls->methods.msg_iter_can_seek_ns_from_origin; | |
357 | iterator->methods.can_seek_beginning = | |
358 | (bt_self_component_port_input_message_iterator_can_seek_beginning_method) | |
359 | src_comp_cls->methods.msg_iter_can_seek_beginning; | |
360 | break; | |
361 | } | |
362 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
363 | { | |
364 | struct bt_component_class_filter *flt_comp_cls = | |
365 | (void *) iterator->upstream_component->class; | |
366 | ||
367 | iterator->methods.next = | |
368 | (bt_self_component_port_input_message_iterator_next_method) | |
369 | flt_comp_cls->methods.msg_iter_next; | |
370 | iterator->methods.seek_ns_from_origin = | |
371 | (bt_self_component_port_input_message_iterator_seek_ns_from_origin_method) | |
372 | flt_comp_cls->methods.msg_iter_seek_ns_from_origin; | |
373 | iterator->methods.seek_beginning = | |
374 | (bt_self_component_port_input_message_iterator_seek_beginning_method) | |
375 | flt_comp_cls->methods.msg_iter_seek_beginning; | |
376 | iterator->methods.can_seek_ns_from_origin = | |
377 | (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method) | |
378 | flt_comp_cls->methods.msg_iter_can_seek_ns_from_origin; | |
379 | iterator->methods.can_seek_beginning = | |
380 | (bt_self_component_port_input_message_iterator_can_seek_beginning_method) | |
381 | flt_comp_cls->methods.msg_iter_can_seek_beginning; | |
382 | break; | |
383 | } | |
384 | default: | |
385 | abort(); | |
386 | } | |
387 | ||
388 | if (iterator->methods.seek_ns_from_origin && | |
389 | !iterator->methods.can_seek_ns_from_origin) { | |
390 | iterator->methods.can_seek_ns_from_origin = | |
391 | (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method) | |
392 | can_seek_ns_from_origin_true; | |
393 | } | |
394 | ||
395 | if (iterator->methods.seek_beginning && | |
396 | !iterator->methods.can_seek_beginning) { | |
397 | iterator->methods.can_seek_beginning = | |
398 | (bt_self_component_port_input_message_iterator_seek_beginning_method) | |
399 | can_seek_beginning_true; | |
400 | } | |
401 | ||
d6e69534 | 402 | BT_LIB_LOGD("Created initial message iterator on self component input port: " |
d94d92ac PP |
403 | "%![up-port-]+p, %![up-comp-]+c, %![iter-]+i", |
404 | upstream_port, upstream_comp, iterator); | |
3230ee6b | 405 | |
47e5a032 | 406 | end: |
d94d92ac | 407 | return iterator; |
47e5a032 JG |
408 | } |
409 | ||
d6e69534 PP |
410 | struct bt_self_component_port_input_message_iterator * |
411 | bt_self_component_port_input_message_iterator_create( | |
d94d92ac | 412 | struct bt_self_component_port_input *self_port) |
ea8d3e58 | 413 | { |
d6e69534 | 414 | typedef enum bt_self_message_iterator_status (*init_method_t)( |
d94d92ac PP |
415 | void *, void *, void *); |
416 | ||
417 | init_method_t init_method = NULL; | |
d6e69534 | 418 | struct bt_self_component_port_input_message_iterator *iterator = |
d94d92ac PP |
419 | NULL; |
420 | struct bt_port *port = (void *) self_port; | |
421 | struct bt_port *upstream_port; | |
422 | struct bt_component *comp; | |
423 | struct bt_component *upstream_comp; | |
424 | struct bt_component_class *upstream_comp_cls; | |
425 | ||
426 | BT_ASSERT_PRE_NON_NULL(port, "Port"); | |
0d72b8c3 | 427 | comp = bt_port_borrow_component_inline(port); |
d94d92ac PP |
428 | BT_ASSERT_PRE(bt_port_is_connected(port), |
429 | "Port is not connected: %![port-]+p", port); | |
430 | BT_ASSERT_PRE(comp, "Port is not part of a component: %![port-]+p", | |
431 | port); | |
432 | BT_ASSERT_PRE(!bt_component_graph_is_canceled(comp), | |
433 | "Port's component's graph is canceled: " | |
434 | "%![port-]+p, %![comp-]+c", port, comp); | |
435 | BT_ASSERT(port->connection); | |
436 | upstream_port = port->connection->upstream_port; | |
437 | BT_ASSERT(upstream_port); | |
0d72b8c3 | 438 | upstream_comp = bt_port_borrow_component_inline(upstream_port); |
d94d92ac | 439 | BT_ASSERT(upstream_comp); |
5badd463 PP |
440 | BT_ASSERT_PRE( |
441 | bt_component_borrow_graph(upstream_comp)->config_state != | |
442 | BT_GRAPH_CONFIGURATION_STATE_CONFIGURING, | |
443 | "Graph is not configured: %!+g", | |
444 | bt_component_borrow_graph(upstream_comp)); | |
d94d92ac PP |
445 | upstream_comp_cls = upstream_comp->class; |
446 | BT_ASSERT(upstream_comp->class->type == | |
447 | BT_COMPONENT_CLASS_TYPE_SOURCE || | |
448 | upstream_comp->class->type == | |
449 | BT_COMPONENT_CLASS_TYPE_FILTER); | |
d6e69534 | 450 | iterator = bt_self_component_port_input_message_iterator_create_initial( |
d94d92ac PP |
451 | upstream_comp, upstream_port); |
452 | if (!iterator) { | |
453 | BT_LOGW_STR("Cannot create self component input port " | |
d6e69534 | 454 | "message iterator."); |
d94d92ac PP |
455 | goto end; |
456 | } | |
890882ef | 457 | |
d94d92ac PP |
458 | switch (upstream_comp_cls->type) { |
459 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
460 | { | |
461 | struct bt_component_class_source *src_comp_cls = | |
462 | (void *) upstream_comp_cls; | |
463 | ||
464 | init_method = | |
d6e69534 | 465 | (init_method_t) src_comp_cls->methods.msg_iter_init; |
d94d92ac PP |
466 | break; |
467 | } | |
468 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
469 | { | |
470 | struct bt_component_class_filter *flt_comp_cls = | |
471 | (void *) upstream_comp_cls; | |
472 | ||
473 | init_method = | |
d6e69534 | 474 | (init_method_t) flt_comp_cls->methods.msg_iter_init; |
d94d92ac PP |
475 | break; |
476 | } | |
477 | default: | |
478 | /* Unreachable */ | |
479 | abort(); | |
480 | } | |
481 | ||
482 | if (init_method) { | |
483 | int iter_status; | |
484 | ||
485 | BT_LIB_LOGD("Calling user's initialization method: %!+i", iterator); | |
486 | iter_status = init_method(iterator, upstream_comp, | |
487 | upstream_port); | |
488 | BT_LOGD("User method returned: status=%s", | |
d6e69534 PP |
489 | bt_message_iterator_status_string(iter_status)); |
490 | if (iter_status != BT_MESSAGE_ITERATOR_STATUS_OK) { | |
d94d92ac | 491 | BT_LOGW_STR("Initialization method failed."); |
d0fea130 | 492 | BT_OBJECT_PUT_REF_AND_RESET(iterator); |
d94d92ac PP |
493 | goto end; |
494 | } | |
495 | } | |
496 | ||
d0fea130 PP |
497 | set_self_comp_port_input_msg_iterator_state(iterator, |
498 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE); | |
d94d92ac | 499 | g_ptr_array_add(port->connection->iterators, iterator); |
d6e69534 | 500 | BT_LIB_LOGD("Created message iterator on self component input port: " |
d94d92ac PP |
501 | "%![up-port-]+p, %![up-comp-]+c, %![iter-]+i", |
502 | upstream_port, upstream_comp, iterator); | |
503 | ||
504 | end: | |
505 | return iterator; | |
ea8d3e58 JG |
506 | } |
507 | ||
d6e69534 PP |
508 | void *bt_self_message_iterator_get_data( |
509 | const struct bt_self_message_iterator *self_iterator) | |
ea8d3e58 | 510 | { |
d6e69534 | 511 | struct bt_self_component_port_input_message_iterator *iterator = |
d94d92ac | 512 | (void *) self_iterator; |
ea8d3e58 | 513 | |
d6e69534 | 514 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); |
d94d92ac | 515 | return iterator->user_data; |
8738a040 | 516 | } |
413bc2c4 | 517 | |
d6e69534 PP |
518 | void bt_self_message_iterator_set_data( |
519 | struct bt_self_message_iterator *self_iterator, void *data) | |
5c563278 | 520 | { |
d6e69534 | 521 | struct bt_self_component_port_input_message_iterator *iterator = |
d94d92ac | 522 | (void *) self_iterator; |
5c563278 | 523 | |
d6e69534 | 524 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); |
d94d92ac | 525 | iterator->user_data = data; |
d6e69534 | 526 | BT_LIB_LOGV("Set message iterator's user data: " |
d94d92ac | 527 | "%!+i, user-data-addr=%p", iterator, data); |
5c563278 PP |
528 | } |
529 | ||
d6e69534 PP |
530 | enum bt_message_iterator_status |
531 | bt_self_component_port_input_message_iterator_next( | |
532 | struct bt_self_component_port_input_message_iterator *iterator, | |
533 | bt_message_array_const *msgs, uint64_t *user_count) | |
3230ee6b | 534 | { |
d6e69534 | 535 | int status = BT_MESSAGE_ITERATOR_STATUS_OK; |
d94d92ac | 536 | |
d6e69534 PP |
537 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); |
538 | BT_ASSERT_PRE_NON_NULL(msgs, "Message array (output)"); | |
539 | BT_ASSERT_PRE_NON_NULL(user_count, "Message count (output)"); | |
f42867e2 | 540 | BT_ASSERT_PRE(iterator->state == |
d6e69534 PP |
541 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE, |
542 | "Message iterator's \"next\" called, but " | |
7474e7d3 | 543 | "message iterator is in the wrong state: %!+i", iterator); |
f42867e2 PP |
544 | BT_ASSERT(iterator->upstream_component); |
545 | BT_ASSERT(iterator->upstream_component->class); | |
5badd463 PP |
546 | BT_ASSERT_PRE( |
547 | bt_component_borrow_graph(iterator->upstream_component)->config_state != | |
548 | BT_GRAPH_CONFIGURATION_STATE_CONFIGURING, | |
4725a201 PP |
549 | "Graph is not configured: %!+g", |
550 | bt_component_borrow_graph(iterator->upstream_component)); | |
d94d92ac | 551 | BT_LIB_LOGD("Getting next self component input port " |
d6e69534 | 552 | "message iterator's messages: %!+i", iterator); |
d3eb6e8f | 553 | |
3230ee6b | 554 | /* |
d6e69534 | 555 | * Call the user's "next" method to get the next messages |
fa054faf | 556 | * and status. |
3230ee6b | 557 | */ |
7474e7d3 | 558 | BT_ASSERT(iterator->methods.next); |
f42867e2 | 559 | BT_LOGD_STR("Calling user's \"next\" method."); |
7474e7d3 PP |
560 | status = iterator->methods.next(iterator, |
561 | (void *) iterator->base.msgs->pdata, MSG_BATCH_SIZE, | |
562 | user_count); | |
f42867e2 | 563 | BT_LOGD("User method returned: status=%s", |
d6e69534 | 564 | bt_message_iterator_status_string(status)); |
d4393e08 | 565 | if (status < 0) { |
f42867e2 | 566 | BT_LOGW_STR("User method failed."); |
f42867e2 PP |
567 | goto end; |
568 | } | |
3230ee6b | 569 | |
d0fea130 PP |
570 | #ifdef BT_DEV_MODE |
571 | /* | |
572 | * There is no way that this iterator could have been finalized | |
573 | * during its "next" method, as the only way to do this is to | |
574 | * put the last iterator's reference, and this can only be done | |
575 | * by its downstream owner. | |
7474e7d3 PP |
576 | * |
577 | * For the same reason, there is no way that this iterator could | |
578 | * have seeked (cannot seek a self message iterator). | |
d0fea130 PP |
579 | */ |
580 | BT_ASSERT(iterator->state == | |
581 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE); | |
582 | #endif | |
8cf27cc5 | 583 | |
d4393e08 | 584 | switch (status) { |
d6e69534 | 585 | case BT_MESSAGE_ITERATOR_STATUS_OK: |
7474e7d3 PP |
586 | BT_ASSERT_PRE(*user_count <= MSG_BATCH_SIZE, |
587 | "Invalid returned message count: greater than " | |
588 | "batch size: count=%" PRIu64 ", batch-size=%u", | |
589 | *user_count, MSG_BATCH_SIZE); | |
d6e69534 | 590 | *msgs = (void *) iterator->base.msgs->pdata; |
d4393e08 | 591 | break; |
d6e69534 | 592 | case BT_MESSAGE_ITERATOR_STATUS_AGAIN: |
d4393e08 | 593 | goto end; |
d6e69534 | 594 | case BT_MESSAGE_ITERATOR_STATUS_END: |
d0fea130 PP |
595 | set_self_comp_port_input_msg_iterator_state(iterator, |
596 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED); | |
f42867e2 | 597 | goto end; |
f42867e2 PP |
598 | default: |
599 | /* Unknown non-error status */ | |
600 | abort(); | |
41a2b7ae PP |
601 | } |
602 | ||
603 | end: | |
3230ee6b PP |
604 | return status; |
605 | } | |
606 | ||
d0fea130 | 607 | enum bt_message_iterator_status bt_port_output_message_iterator_next( |
d6e69534 PP |
608 | struct bt_port_output_message_iterator *iterator, |
609 | bt_message_array_const *msgs_to_user, | |
d4393e08 | 610 | uint64_t *count_to_user) |
3230ee6b | 611 | { |
d6e69534 | 612 | enum bt_message_iterator_status status; |
07245ac2 | 613 | enum bt_graph_status graph_status; |
3230ee6b | 614 | |
d6e69534 PP |
615 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); |
616 | BT_ASSERT_PRE_NON_NULL(msgs_to_user, "Message array (output)"); | |
617 | BT_ASSERT_PRE_NON_NULL(count_to_user, "Message count (output)"); | |
618 | BT_LIB_LOGD("Getting next output port message iterator's messages: " | |
07245ac2 | 619 | "%!+i", iterator); |
d94d92ac PP |
620 | graph_status = bt_graph_consume_sink_no_check(iterator->graph, |
621 | iterator->colander); | |
07245ac2 PP |
622 | switch (graph_status) { |
623 | case BT_GRAPH_STATUS_CANCELED: | |
07245ac2 | 624 | case BT_GRAPH_STATUS_AGAIN: |
07245ac2 | 625 | case BT_GRAPH_STATUS_END: |
07245ac2 | 626 | case BT_GRAPH_STATUS_NOMEM: |
d94d92ac | 627 | status = (int) graph_status; |
07245ac2 PP |
628 | break; |
629 | case BT_GRAPH_STATUS_OK: | |
d6e69534 | 630 | status = BT_MESSAGE_ITERATOR_STATUS_OK; |
d4393e08 PP |
631 | |
632 | /* | |
d6e69534 | 633 | * On success, the colander sink moves the messages |
d4393e08 | 634 | * to this iterator's array and sets this iterator's |
d6e69534 | 635 | * message count: move them to the user. |
d4393e08 | 636 | */ |
d6e69534 | 637 | *msgs_to_user = (void *) iterator->base.msgs->pdata; |
d94d92ac | 638 | *count_to_user = iterator->count; |
90157d89 | 639 | break; |
90157d89 | 640 | default: |
07245ac2 | 641 | /* Other errors */ |
d6e69534 | 642 | status = BT_MESSAGE_ITERATOR_STATUS_ERROR; |
90157d89 | 643 | } |
3230ee6b | 644 | |
3230ee6b | 645 | return status; |
53d45b87 JG |
646 | } |
647 | ||
7474e7d3 PP |
648 | struct bt_component * |
649 | bt_self_component_port_input_message_iterator_borrow_component( | |
d6e69534 | 650 | struct bt_self_component_port_input_message_iterator *iterator) |
d94d92ac | 651 | { |
d6e69534 | 652 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); |
d94d92ac PP |
653 | return iterator->upstream_component; |
654 | } | |
655 | ||
7474e7d3 PP |
656 | const struct bt_component * |
657 | bt_self_component_port_input_message_iterator_borrow_component_const( | |
658 | const struct bt_self_component_port_input_message_iterator *iterator) | |
659 | { | |
660 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); | |
661 | return iterator->upstream_component; | |
662 | } | |
663 | ||
d6e69534 PP |
664 | struct bt_self_component *bt_self_message_iterator_borrow_component( |
665 | struct bt_self_message_iterator *self_iterator) | |
413bc2c4 | 666 | { |
d6e69534 | 667 | struct bt_self_component_port_input_message_iterator *iterator = |
d94d92ac | 668 | (void *) self_iterator; |
90157d89 | 669 | |
d6e69534 | 670 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); |
d94d92ac | 671 | return (void *) iterator->upstream_component; |
413bc2c4 JG |
672 | } |
673 | ||
d6e69534 PP |
674 | struct bt_self_port_output *bt_self_message_iterator_borrow_port( |
675 | struct bt_self_message_iterator *self_iterator) | |
91457551 | 676 | { |
d6e69534 | 677 | struct bt_self_component_port_input_message_iterator *iterator = |
d94d92ac PP |
678 | (void *) self_iterator; |
679 | ||
d6e69534 | 680 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); |
d94d92ac | 681 | return (void *) iterator->upstream_port; |
91457551 | 682 | } |
8ed535b5 PP |
683 | |
684 | static | |
d6e69534 | 685 | void bt_port_output_message_iterator_destroy(struct bt_object *obj) |
8ed535b5 | 686 | { |
d6e69534 | 687 | struct bt_port_output_message_iterator *iterator = (void *) obj; |
8ed535b5 | 688 | |
d6e69534 | 689 | BT_LIB_LOGD("Destroying output port message iterator object: %!+i", |
8ed535b5 PP |
690 | iterator); |
691 | BT_LOGD_STR("Putting graph."); | |
d94d92ac | 692 | BT_OBJECT_PUT_REF_AND_RESET(iterator->graph); |
8ed535b5 | 693 | BT_LOGD_STR("Putting colander sink component."); |
d94d92ac | 694 | BT_OBJECT_PUT_REF_AND_RESET(iterator->colander); |
d6e69534 | 695 | destroy_base_message_iterator(obj); |
8ed535b5 PP |
696 | } |
697 | ||
d6e69534 | 698 | struct bt_port_output_message_iterator * |
7474e7d3 | 699 | bt_port_output_message_iterator_create(struct bt_graph *graph, |
0d72b8c3 | 700 | const struct bt_port_output *output_port) |
8ed535b5 | 701 | { |
d6e69534 | 702 | struct bt_port_output_message_iterator *iterator = NULL; |
d94d92ac | 703 | struct bt_component_class_sink *colander_comp_cls = NULL; |
8ed535b5 | 704 | struct bt_component *output_port_comp = NULL; |
d94d92ac | 705 | struct bt_component_sink *colander_comp; |
8ed535b5 | 706 | enum bt_graph_status graph_status; |
d94d92ac | 707 | struct bt_port_input *colander_in_port = NULL; |
8ed535b5 | 708 | struct bt_component_class_sink_colander_data colander_data; |
d4393e08 | 709 | int ret; |
8ed535b5 | 710 | |
d94d92ac | 711 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
f42867e2 | 712 | BT_ASSERT_PRE_NON_NULL(output_port, "Output port"); |
0d72b8c3 PP |
713 | output_port_comp = bt_port_borrow_component_inline( |
714 | (const void *) output_port); | |
f42867e2 PP |
715 | BT_ASSERT_PRE(output_port_comp, |
716 | "Output port has no component: %!+p", output_port); | |
d94d92ac PP |
717 | BT_ASSERT_PRE(bt_component_borrow_graph(output_port_comp) == |
718 | (void *) graph, | |
719 | "Output port is not part of graph: %![graph-]+g, %![port-]+p", | |
720 | graph, output_port); | |
2bcba397 PP |
721 | BT_ASSERT_PRE(!graph->has_sink, |
722 | "Graph already has a sink component: %![graph-]+g"); | |
8ed535b5 | 723 | |
d6e69534 PP |
724 | /* Create message iterator */ |
725 | BT_LIB_LOGD("Creating message iterator on output port: " | |
d94d92ac | 726 | "%![port-]+p, %![comp-]+c", output_port, output_port_comp); |
d6e69534 | 727 | iterator = g_new0(struct bt_port_output_message_iterator, 1); |
8ed535b5 | 728 | if (!iterator) { |
d6e69534 | 729 | BT_LOGE_STR("Failed to allocate one output port message iterator."); |
8ed535b5 PP |
730 | goto error; |
731 | } | |
732 | ||
d6e69534 PP |
733 | ret = init_message_iterator((void *) iterator, |
734 | BT_MESSAGE_ITERATOR_TYPE_PORT_OUTPUT, | |
735 | bt_port_output_message_iterator_destroy); | |
d4393e08 | 736 | if (ret) { |
d6e69534 | 737 | /* init_message_iterator() logs errors */ |
65300d60 | 738 | BT_OBJECT_PUT_REF_AND_RESET(iterator); |
d4393e08 PP |
739 | goto end; |
740 | } | |
8ed535b5 PP |
741 | |
742 | /* Create colander component */ | |
743 | colander_comp_cls = bt_component_class_sink_colander_get(); | |
744 | if (!colander_comp_cls) { | |
745 | BT_LOGW("Cannot get colander sink component class."); | |
746 | goto error; | |
747 | } | |
748 | ||
398454ed PP |
749 | iterator->graph = graph; |
750 | bt_object_get_no_null_check(iterator->graph); | |
d6e69534 | 751 | colander_data.msgs = (void *) iterator->base.msgs->pdata; |
d4393e08 | 752 | colander_data.count_addr = &iterator->count; |
5fd91d88 PP |
753 | |
754 | /* Hope that nobody uses this very unique name */ | |
d94d92ac | 755 | graph_status = |
0d72b8c3 | 756 | bt_graph_add_sink_component_with_init_method_data( |
5fd91d88 PP |
757 | (void *) graph, colander_comp_cls, |
758 | "colander-36ac3409-b1a8-4d60-ab1f-4fdf341a8fb1", | |
0d72b8c3 | 759 | NULL, &colander_data, (void *) &iterator->colander); |
8ed535b5 | 760 | if (graph_status != BT_GRAPH_STATUS_OK) { |
d94d92ac PP |
761 | BT_LIB_LOGW("Cannot add colander sink component to graph: " |
762 | "%1[graph-]+g, status=%s", graph, | |
8ed535b5 PP |
763 | bt_graph_status_string(graph_status)); |
764 | goto error; | |
765 | } | |
766 | ||
767 | /* | |
768 | * Connect provided output port to the colander component's | |
769 | * input port. | |
770 | */ | |
0d72b8c3 PP |
771 | colander_in_port = |
772 | (void *) bt_component_sink_borrow_input_port_by_index_const( | |
773 | (void *) iterator->colander, 0); | |
f6ccaed9 | 774 | BT_ASSERT(colander_in_port); |
0d72b8c3 | 775 | graph_status = bt_graph_connect_ports(graph, |
8ed535b5 PP |
776 | output_port, colander_in_port, NULL); |
777 | if (graph_status != BT_GRAPH_STATUS_OK) { | |
d94d92ac PP |
778 | BT_LIB_LOGW("Cannot add colander sink component to graph: " |
779 | "%![graph-]+g, %![comp-]+c, status=%s", graph, | |
780 | iterator->colander, | |
8ed535b5 PP |
781 | bt_graph_status_string(graph_status)); |
782 | goto error; | |
783 | } | |
784 | ||
785 | /* | |
786 | * At this point everything went fine. Make the graph | |
d6e69534 | 787 | * nonconsumable forever so that only this message iterator |
8ed535b5 | 788 | * can consume (thanks to bt_graph_consume_sink_no_check()). |
d6e69534 PP |
789 | * This avoids leaking the message created by the colander |
790 | * sink and moved to the message iterator's message | |
07245ac2 | 791 | * member. |
8ed535b5 | 792 | */ |
d94d92ac | 793 | bt_graph_set_can_consume(iterator->graph, false); |
5badd463 PP |
794 | |
795 | /* | |
796 | * Also set the graph as being configured: it has no active sink | |
797 | * anyway, so we don't need to call bt_graph_configure(). | |
798 | */ | |
799 | graph->config_state = BT_GRAPH_CONFIGURATION_STATE_CONFIGURED; | |
8ed535b5 PP |
800 | goto end; |
801 | ||
802 | error: | |
803 | if (iterator && iterator->graph && iterator->colander) { | |
804 | int ret; | |
805 | ||
806 | /* Remove created colander component from graph if any */ | |
807 | colander_comp = iterator->colander; | |
65300d60 | 808 | BT_OBJECT_PUT_REF_AND_RESET(iterator->colander); |
8ed535b5 PP |
809 | |
810 | /* | |
811 | * At this point the colander component's reference | |
812 | * count is 0 because iterator->colander was the only | |
813 | * owner. We also know that it is not connected because | |
814 | * this is the last operation before this function | |
815 | * succeeds. | |
816 | * | |
817 | * Since we honor the preconditions here, | |
818 | * bt_graph_remove_unconnected_component() always | |
819 | * succeeds. | |
820 | */ | |
821 | ret = bt_graph_remove_unconnected_component(iterator->graph, | |
d94d92ac | 822 | (void *) colander_comp); |
f6ccaed9 | 823 | BT_ASSERT(ret == 0); |
8ed535b5 PP |
824 | } |
825 | ||
65300d60 | 826 | BT_OBJECT_PUT_REF_AND_RESET(iterator); |
8ed535b5 PP |
827 | |
828 | end: | |
65300d60 | 829 | bt_object_put_ref(colander_comp_cls); |
8ed535b5 PP |
830 | return (void *) iterator; |
831 | } | |
c5b9b441 | 832 | |
7474e7d3 PP |
833 | bt_bool bt_self_component_port_input_message_iterator_can_seek_ns_from_origin( |
834 | struct bt_self_component_port_input_message_iterator *iterator, | |
835 | int64_t ns_from_origin) | |
836 | { | |
837 | bt_bool can = BT_FALSE; | |
838 | ||
839 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); | |
840 | BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator); | |
5badd463 PP |
841 | BT_ASSERT_PRE( |
842 | bt_component_borrow_graph(iterator->upstream_component)->config_state != | |
843 | BT_GRAPH_CONFIGURATION_STATE_CONFIGURING, | |
7474e7d3 PP |
844 | "Graph is not configured: %!+g", |
845 | bt_component_borrow_graph(iterator->upstream_component)); | |
846 | ||
847 | if (iterator->methods.can_seek_ns_from_origin) { | |
848 | can = iterator->methods.can_seek_ns_from_origin(iterator, | |
849 | ns_from_origin); | |
850 | goto end; | |
851 | } | |
852 | ||
853 | /* | |
854 | * Automatic seeking fall back: if we can seek to the beginning, | |
855 | * then we can automatically seek to any message. | |
856 | */ | |
857 | if (iterator->methods.can_seek_beginning) { | |
858 | can = iterator->methods.can_seek_beginning(iterator); | |
859 | } | |
860 | ||
861 | end: | |
862 | return can; | |
863 | } | |
864 | ||
865 | bt_bool bt_self_component_port_input_message_iterator_can_seek_beginning( | |
866 | struct bt_self_component_port_input_message_iterator *iterator) | |
867 | { | |
868 | bt_bool can = BT_FALSE; | |
869 | ||
870 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); | |
871 | BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator); | |
5badd463 PP |
872 | BT_ASSERT_PRE( |
873 | bt_component_borrow_graph(iterator->upstream_component)->config_state != | |
874 | BT_GRAPH_CONFIGURATION_STATE_CONFIGURING, | |
7474e7d3 PP |
875 | "Graph is not configured: %!+g", |
876 | bt_component_borrow_graph(iterator->upstream_component)); | |
877 | ||
878 | if (iterator->methods.can_seek_beginning) { | |
879 | can = iterator->methods.can_seek_beginning(iterator); | |
880 | } | |
881 | ||
882 | return can; | |
883 | } | |
884 | ||
0952128e | 885 | BT_ASSERT_PRE_FUNC |
7474e7d3 PP |
886 | static inline |
887 | void _set_iterator_state_after_seeking( | |
888 | struct bt_self_component_port_input_message_iterator *iterator, | |
889 | enum bt_message_iterator_status status) | |
890 | { | |
891 | enum bt_self_component_port_input_message_iterator_state new_state = 0; | |
892 | ||
893 | /* Set iterator's state depending on seeking status */ | |
894 | switch (status) { | |
895 | case BT_MESSAGE_ITERATOR_STATUS_OK: | |
896 | new_state = BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE; | |
897 | break; | |
898 | case BT_MESSAGE_ITERATOR_STATUS_AGAIN: | |
899 | new_state = BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN; | |
900 | break; | |
901 | case BT_MESSAGE_ITERATOR_STATUS_ERROR: | |
902 | case BT_MESSAGE_ITERATOR_STATUS_NOMEM: | |
903 | new_state = BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR; | |
904 | break; | |
905 | case BT_MESSAGE_ITERATOR_STATUS_END: | |
906 | new_state = BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED; | |
907 | break; | |
908 | default: | |
909 | abort(); | |
910 | } | |
911 | ||
912 | set_self_comp_port_input_msg_iterator_state(iterator, new_state); | |
913 | } | |
914 | ||
915 | #ifdef BT_DEV_MODE | |
916 | # define set_iterator_state_after_seeking _set_iterator_state_after_seeking | |
917 | #else | |
c411e134 | 918 | # define set_iterator_state_after_seeking(_iter, _status) ((void) _iter); ((void) _status); |
7474e7d3 PP |
919 | #endif |
920 | ||
921 | enum bt_message_iterator_status | |
922 | bt_self_component_port_input_message_iterator_seek_beginning( | |
923 | struct bt_self_component_port_input_message_iterator *iterator) | |
924 | { | |
925 | int status; | |
926 | ||
927 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); | |
928 | BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator); | |
5badd463 PP |
929 | BT_ASSERT_PRE( |
930 | bt_component_borrow_graph(iterator->upstream_component)->config_state != | |
931 | BT_GRAPH_CONFIGURATION_STATE_CONFIGURING, | |
7474e7d3 PP |
932 | "Graph is not configured: %!+g", |
933 | bt_component_borrow_graph(iterator->upstream_component)); | |
934 | BT_ASSERT_PRE( | |
935 | bt_self_component_port_input_message_iterator_can_seek_beginning( | |
936 | iterator), | |
937 | "Message iterator cannot seek beginning: %!+i", iterator); | |
938 | BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i", iterator); | |
939 | set_self_comp_port_input_msg_iterator_state(iterator, | |
940 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_SEEKING); | |
941 | status = iterator->methods.seek_beginning(iterator); | |
942 | BT_LOGD("User method returned: status=%s", | |
943 | bt_message_iterator_status_string(status)); | |
944 | BT_ASSERT_PRE(status == BT_MESSAGE_ITERATOR_STATUS_OK || | |
945 | status == BT_MESSAGE_ITERATOR_STATUS_ERROR || | |
946 | status == BT_MESSAGE_ITERATOR_STATUS_NOMEM || | |
947 | status == BT_MESSAGE_ITERATOR_STATUS_AGAIN, | |
948 | "Unexpected status: %![iter-]+i, status=%s", | |
bd1a54fe | 949 | iterator, bt_common_self_message_iterator_status_string(status)); |
7474e7d3 PP |
950 | set_iterator_state_after_seeking(iterator, status); |
951 | return status; | |
952 | } | |
953 | ||
954 | static inline | |
5b9e151d PP |
955 | enum bt_message_iterator_status auto_seek_handle_message( |
956 | struct bt_self_component_port_input_message_iterator *iterator, | |
957 | int64_t ns_from_origin, const struct bt_message *msg, | |
958 | bool *got_first) | |
7474e7d3 | 959 | { |
5b9e151d PP |
960 | enum bt_message_iterator_status status = BT_MESSAGE_ITERATOR_STATUS_OK; |
961 | int64_t msg_ns_from_origin; | |
7474e7d3 | 962 | const struct bt_clock_snapshot *clk_snapshot = NULL; |
5b9e151d PP |
963 | int ret; |
964 | ||
965 | BT_ASSERT(msg); | |
966 | BT_ASSERT(got_first); | |
7474e7d3 PP |
967 | |
968 | switch (msg->type) { | |
969 | case BT_MESSAGE_TYPE_EVENT: | |
970 | { | |
971 | const struct bt_message_event *event_msg = | |
972 | (const void *) msg; | |
973 | ||
2c091c04 | 974 | clk_snapshot = event_msg->default_cs; |
7474e7d3 | 975 | BT_ASSERT_PRE(clk_snapshot, |
c7072d5a PP |
976 | "Event message has no default clock snapshot: %!+n", |
977 | event_msg); | |
7474e7d3 PP |
978 | break; |
979 | } | |
b9fd9cbb | 980 | case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY: |
7474e7d3 | 981 | { |
b9fd9cbb | 982 | const struct bt_message_message_iterator_inactivity *inactivity_msg = |
7474e7d3 PP |
983 | (const void *) msg; |
984 | ||
7474e7d3 | 985 | clk_snapshot = inactivity_msg->default_cs; |
16663a5e | 986 | BT_ASSERT(clk_snapshot); |
7474e7d3 PP |
987 | break; |
988 | } | |
16663a5e PP |
989 | case BT_MESSAGE_TYPE_PACKET_BEGINNING: |
990 | case BT_MESSAGE_TYPE_PACKET_END: | |
c7072d5a PP |
991 | { |
992 | const struct bt_message_packet *packet_msg = | |
993 | (const void *) msg; | |
994 | ||
995 | clk_snapshot = packet_msg->default_cs; | |
996 | BT_ASSERT_PRE(clk_snapshot, | |
997 | "Packet message has no default clock snapshot: %!+n", | |
998 | packet_msg); | |
999 | break; | |
1000 | } | |
16663a5e PP |
1001 | case BT_MESSAGE_TYPE_DISCARDED_EVENTS: |
1002 | case BT_MESSAGE_TYPE_DISCARDED_PACKETS: | |
7474e7d3 | 1003 | { |
5b9e151d PP |
1004 | struct bt_message_discarded_items *msg_disc_items = |
1005 | (void *) msg; | |
1006 | ||
1007 | BT_ASSERT_PRE(msg_disc_items->default_begin_cs && | |
1008 | msg_disc_items->default_end_cs, | |
1009 | "Discarded events/packets message has no default clock snapshots: %!+n", | |
1010 | msg_disc_items); | |
1011 | ret = bt_clock_snapshot_get_ns_from_origin( | |
1012 | msg_disc_items->default_begin_cs, | |
1013 | &msg_ns_from_origin); | |
1014 | if (ret) { | |
1015 | status = BT_MESSAGE_ITERATOR_STATUS_ERROR; | |
1016 | goto end; | |
1017 | } | |
7474e7d3 | 1018 | |
5b9e151d PP |
1019 | if (msg_ns_from_origin >= ns_from_origin) { |
1020 | *got_first = true; | |
1021 | goto push_msg; | |
1022 | } | |
1023 | ||
1024 | ret = bt_clock_snapshot_get_ns_from_origin( | |
1025 | msg_disc_items->default_end_cs, | |
1026 | &msg_ns_from_origin); | |
1027 | if (ret) { | |
1028 | status = BT_MESSAGE_ITERATOR_STATUS_ERROR; | |
1029 | goto end; | |
1030 | } | |
1031 | ||
1032 | if (msg_ns_from_origin >= ns_from_origin) { | |
1033 | /* | |
1034 | * The discarded items message's beginning time | |
1035 | * is before the requested seeking time, but its | |
1036 | * end time is after. Modify the message so as | |
1037 | * to set its beginning time to the requested | |
1038 | * seeking time, and make its item count unknown | |
1039 | * as we don't know if items were really | |
1040 | * discarded within the new time range. | |
1041 | */ | |
1042 | uint64_t new_begin_raw_value; | |
1043 | ||
1044 | ret = bt_clock_class_clock_value_from_ns_from_origin( | |
1045 | msg_disc_items->default_end_cs->clock_class, | |
1046 | ns_from_origin, &new_begin_raw_value); | |
1047 | if (ret) { | |
1048 | status = BT_MESSAGE_ITERATOR_STATUS_ERROR; | |
1049 | goto end; | |
1050 | } | |
1051 | ||
1052 | bt_clock_snapshot_set_raw_value( | |
1053 | msg_disc_items->default_begin_cs, | |
1054 | new_begin_raw_value); | |
1055 | msg_disc_items->count.base.avail = | |
1056 | BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE; | |
1057 | ||
1058 | /* | |
1059 | * It is safe to push it because its beginning | |
1060 | * time is exactly the requested seeking time. | |
1061 | */ | |
1062 | goto push_msg; | |
1063 | } else { | |
1064 | goto skip_msg; | |
1065 | } | |
7474e7d3 | 1066 | } |
16663a5e | 1067 | case BT_MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING: |
7474e7d3 | 1068 | { |
16663a5e | 1069 | const struct bt_message_stream_activity *stream_act_msg = |
7474e7d3 PP |
1070 | (const void *) msg; |
1071 | ||
16663a5e PP |
1072 | switch (stream_act_msg->default_cs_state) { |
1073 | case BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_UNKNOWN: | |
1074 | case BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_INFINITE: | |
1075 | /* | |
1076 | * -inf is always less than any requested time, | |
1077 | * and we can't assume any specific time for an | |
1078 | * unknown clock snapshot, so skip this. | |
1079 | */ | |
5b9e151d | 1080 | goto skip_msg; |
16663a5e PP |
1081 | case BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_KNOWN: |
1082 | clk_snapshot = stream_act_msg->default_cs; | |
1083 | BT_ASSERT(clk_snapshot); | |
1084 | break; | |
1085 | default: | |
1086 | abort(); | |
1087 | } | |
1088 | ||
7474e7d3 PP |
1089 | break; |
1090 | } | |
16663a5e PP |
1091 | case BT_MESSAGE_TYPE_STREAM_ACTIVITY_END: |
1092 | { | |
1093 | const struct bt_message_stream_activity *stream_act_msg = | |
1094 | (const void *) msg; | |
1095 | ||
1096 | switch (stream_act_msg->default_cs_state) { | |
1097 | case BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_UNKNOWN: | |
1098 | /* | |
1099 | * We can't assume any specific time for an | |
1100 | * unknown clock snapshot, so skip this. | |
1101 | */ | |
5b9e151d | 1102 | goto skip_msg; |
16663a5e PP |
1103 | case BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_INFINITE: |
1104 | /* | |
1105 | * +inf is always greater than any requested | |
1106 | * time. | |
1107 | */ | |
5b9e151d PP |
1108 | *got_first = true; |
1109 | goto push_msg; | |
16663a5e PP |
1110 | case BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_KNOWN: |
1111 | clk_snapshot = stream_act_msg->default_cs; | |
1112 | BT_ASSERT(clk_snapshot); | |
1113 | break; | |
1114 | default: | |
1115 | abort(); | |
1116 | } | |
1117 | ||
1118 | break; | |
1119 | } | |
1120 | case BT_MESSAGE_TYPE_STREAM_BEGINNING: | |
1121 | case BT_MESSAGE_TYPE_STREAM_END: | |
1122 | /* Ignore */ | |
5b9e151d | 1123 | goto skip_msg; |
7474e7d3 PP |
1124 | default: |
1125 | abort(); | |
1126 | } | |
1127 | ||
5b9e151d PP |
1128 | BT_ASSERT(clk_snapshot); |
1129 | ret = bt_clock_snapshot_get_ns_from_origin(clk_snapshot, | |
1130 | &msg_ns_from_origin); | |
1131 | if (ret) { | |
1132 | status = BT_MESSAGE_ITERATOR_STATUS_ERROR; | |
7474e7d3 PP |
1133 | goto end; |
1134 | } | |
1135 | ||
5b9e151d PP |
1136 | if (msg_ns_from_origin >= ns_from_origin) { |
1137 | *got_first = true; | |
1138 | goto push_msg; | |
1139 | } | |
1140 | ||
1141 | skip_msg: | |
1142 | bt_object_put_no_null_check(msg); | |
04c0cec6 | 1143 | msg = NULL; |
5b9e151d PP |
1144 | goto end; |
1145 | ||
1146 | push_msg: | |
1147 | g_queue_push_head(iterator->auto_seek_msgs, (void *) msg); | |
1148 | msg = NULL; | |
7474e7d3 PP |
1149 | |
1150 | end: | |
5b9e151d PP |
1151 | BT_ASSERT(!msg || status != BT_MESSAGE_ITERATOR_STATUS_OK); |
1152 | return status; | |
7474e7d3 PP |
1153 | } |
1154 | ||
1155 | static | |
1156 | enum bt_message_iterator_status find_message_ge_ns_from_origin( | |
1157 | struct bt_self_component_port_input_message_iterator *iterator, | |
1158 | int64_t ns_from_origin) | |
1159 | { | |
1160 | int status; | |
1161 | enum bt_self_component_port_input_message_iterator_state init_state = | |
1162 | iterator->state; | |
1163 | const struct bt_message *messages[MSG_BATCH_SIZE]; | |
1164 | uint64_t user_count = 0; | |
1165 | uint64_t i; | |
5b9e151d | 1166 | bool got_first = false; |
7474e7d3 PP |
1167 | |
1168 | BT_ASSERT(iterator); | |
1169 | memset(&messages[0], 0, sizeof(messages[0]) * MSG_BATCH_SIZE); | |
1170 | ||
1171 | /* | |
1172 | * Make this iterator temporarily active (not seeking) to call | |
1173 | * the "next" method. | |
1174 | */ | |
1175 | set_self_comp_port_input_msg_iterator_state(iterator, | |
1176 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE); | |
1177 | ||
1178 | BT_ASSERT(iterator->methods.next); | |
1179 | ||
e0dade92 | 1180 | while (!got_first) { |
7474e7d3 PP |
1181 | /* |
1182 | * Call the user's "next" method to get the next | |
1183 | * messages and status. | |
1184 | */ | |
1185 | BT_LOGD_STR("Calling user's \"next\" method."); | |
1186 | status = iterator->methods.next(iterator, | |
1187 | &messages[0], MSG_BATCH_SIZE, &user_count); | |
1188 | BT_LOGD("User method returned: status=%s", | |
1189 | bt_message_iterator_status_string(status)); | |
1190 | ||
1191 | #ifdef BT_DEV_MODE | |
1192 | /* | |
1193 | * The user's "next" method must not do any action which | |
1194 | * would change the iterator's state. | |
1195 | */ | |
1196 | BT_ASSERT(iterator->state == | |
1197 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE); | |
1198 | #endif | |
1199 | ||
1200 | switch (status) { | |
1201 | case BT_MESSAGE_ITERATOR_STATUS_OK: | |
1202 | BT_ASSERT_PRE(user_count <= MSG_BATCH_SIZE, | |
1203 | "Invalid returned message count: greater than " | |
1204 | "batch size: count=%" PRIu64 ", batch-size=%u", | |
1205 | user_count, MSG_BATCH_SIZE); | |
1206 | break; | |
1207 | case BT_MESSAGE_ITERATOR_STATUS_AGAIN: | |
1208 | case BT_MESSAGE_ITERATOR_STATUS_ERROR: | |
1209 | case BT_MESSAGE_ITERATOR_STATUS_NOMEM: | |
1210 | case BT_MESSAGE_ITERATOR_STATUS_END: | |
1211 | goto end; | |
1212 | default: | |
1213 | abort(); | |
1214 | } | |
1215 | ||
7474e7d3 | 1216 | for (i = 0; i < user_count; i++) { |
5b9e151d PP |
1217 | if (got_first) { |
1218 | g_queue_push_head(iterator->auto_seek_msgs, | |
1219 | (void *) messages[i]); | |
1220 | messages[i] = NULL; | |
7474e7d3 PP |
1221 | continue; |
1222 | } | |
1223 | ||
5b9e151d PP |
1224 | status = auto_seek_handle_message(iterator, |
1225 | ns_from_origin, messages[i], &got_first); | |
1226 | if (status == BT_MESSAGE_ITERATOR_STATUS_OK) { | |
e0dade92 | 1227 | /* Message was either pushed or moved */ |
5b9e151d PP |
1228 | messages[i] = NULL; |
1229 | } else { | |
7474e7d3 PP |
1230 | goto end; |
1231 | } | |
7474e7d3 PP |
1232 | } |
1233 | } | |
1234 | ||
1235 | end: | |
1236 | for (i = 0; i < user_count; i++) { | |
1237 | if (messages[i]) { | |
1238 | bt_object_put_no_null_check(messages[i]); | |
1239 | } | |
1240 | } | |
1241 | ||
1242 | set_self_comp_port_input_msg_iterator_state(iterator, init_state); | |
1243 | return status; | |
1244 | } | |
1245 | ||
1246 | static | |
1247 | enum bt_self_message_iterator_status post_auto_seek_next( | |
1248 | struct bt_self_component_port_input_message_iterator *iterator, | |
1249 | bt_message_array_const msgs, uint64_t capacity, | |
1250 | uint64_t *count) | |
1251 | { | |
5b9e151d PP |
1252 | BT_ASSERT(!g_queue_is_empty(iterator->auto_seek_msgs)); |
1253 | *count = 0; | |
7474e7d3 PP |
1254 | |
1255 | /* | |
1256 | * Move auto-seek messages to the output array (which is this | |
5b9e151d | 1257 | * iterator's base message array). |
7474e7d3 | 1258 | */ |
5b9e151d PP |
1259 | while (capacity > 0 && !g_queue_is_empty(iterator->auto_seek_msgs)) { |
1260 | msgs[*count] = g_queue_pop_tail(iterator->auto_seek_msgs); | |
1261 | capacity--; | |
1262 | (*count)++; | |
7474e7d3 | 1263 | } |
7474e7d3 | 1264 | |
5b9e151d PP |
1265 | BT_ASSERT(*count > 0); |
1266 | ||
1267 | if (g_queue_is_empty(iterator->auto_seek_msgs)) { | |
1268 | /* No more auto-seek messages */ | |
1269 | switch (iterator->upstream_component->class->type) { | |
1270 | case BT_COMPONENT_CLASS_TYPE_SOURCE: | |
1271 | { | |
1272 | struct bt_component_class_source *src_comp_cls = | |
1273 | (void *) iterator->upstream_component->class; | |
1274 | ||
1275 | iterator->methods.next = | |
1276 | (bt_self_component_port_input_message_iterator_next_method) | |
1277 | src_comp_cls->methods.msg_iter_next; | |
1278 | break; | |
1279 | } | |
1280 | case BT_COMPONENT_CLASS_TYPE_FILTER: | |
1281 | { | |
1282 | struct bt_component_class_filter *flt_comp_cls = | |
1283 | (void *) iterator->upstream_component->class; | |
1284 | ||
1285 | iterator->methods.next = | |
1286 | (bt_self_component_port_input_message_iterator_next_method) | |
1287 | flt_comp_cls->methods.msg_iter_next; | |
1288 | break; | |
1289 | } | |
1290 | default: | |
1291 | abort(); | |
1292 | } | |
7474e7d3 PP |
1293 | } |
1294 | ||
1295 | return BT_SELF_MESSAGE_ITERATOR_STATUS_OK; | |
1296 | } | |
1297 | ||
1298 | enum bt_message_iterator_status | |
1299 | bt_self_component_port_input_message_iterator_seek_ns_from_origin( | |
1300 | struct bt_self_component_port_input_message_iterator *iterator, | |
1301 | int64_t ns_from_origin) | |
1302 | { | |
1303 | int status; | |
1304 | ||
1305 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); | |
1306 | BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator); | |
5badd463 PP |
1307 | BT_ASSERT_PRE( |
1308 | bt_component_borrow_graph(iterator->upstream_component)->config_state != | |
1309 | BT_GRAPH_CONFIGURATION_STATE_CONFIGURING, | |
7474e7d3 PP |
1310 | "Graph is not configured: %!+g", |
1311 | bt_component_borrow_graph(iterator->upstream_component)); | |
1312 | BT_ASSERT_PRE( | |
1313 | bt_self_component_port_input_message_iterator_can_seek_ns_from_origin( | |
1314 | iterator, ns_from_origin), | |
1315 | "Message iterator cannot seek nanoseconds from origin: %!+i, " | |
1316 | "ns-from-origin=%" PRId64, iterator, ns_from_origin); | |
1317 | set_self_comp_port_input_msg_iterator_state(iterator, | |
1318 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_SEEKING); | |
1319 | ||
1320 | if (iterator->methods.seek_ns_from_origin) { | |
1321 | BT_LIB_LOGD("Calling user's \"seek nanoseconds from origin\" method: " | |
1322 | "%![iter-]+i, ns=%" PRId64, iterator, ns_from_origin); | |
1323 | status = iterator->methods.seek_ns_from_origin(iterator, | |
1324 | ns_from_origin); | |
1325 | BT_LOGD("User method returned: status=%s", | |
1326 | bt_message_iterator_status_string(status)); | |
1327 | BT_ASSERT_PRE(status == BT_MESSAGE_ITERATOR_STATUS_OK || | |
1328 | status == BT_MESSAGE_ITERATOR_STATUS_ERROR || | |
1329 | status == BT_MESSAGE_ITERATOR_STATUS_NOMEM || | |
1330 | status == BT_MESSAGE_ITERATOR_STATUS_AGAIN, | |
1331 | "Unexpected status: %![iter-]+i, status=%s", | |
1332 | iterator, | |
bd1a54fe | 1333 | bt_common_self_message_iterator_status_string(status)); |
7474e7d3 PP |
1334 | } else { |
1335 | /* Start automatic seeking: seek beginning first */ | |
1336 | BT_ASSERT(iterator->methods.can_seek_beginning(iterator)); | |
1337 | BT_ASSERT(iterator->methods.seek_beginning); | |
1338 | BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i", | |
1339 | iterator); | |
1340 | status = iterator->methods.seek_beginning(iterator); | |
1341 | BT_LOGD("User method returned: status=%s", | |
1342 | bt_message_iterator_status_string(status)); | |
1343 | BT_ASSERT_PRE(status == BT_MESSAGE_ITERATOR_STATUS_OK || | |
1344 | status == BT_MESSAGE_ITERATOR_STATUS_ERROR || | |
1345 | status == BT_MESSAGE_ITERATOR_STATUS_NOMEM || | |
1346 | status == BT_MESSAGE_ITERATOR_STATUS_AGAIN, | |
1347 | "Unexpected status: %![iter-]+i, status=%s", | |
1348 | iterator, | |
bd1a54fe | 1349 | bt_common_self_message_iterator_status_string(status)); |
7474e7d3 PP |
1350 | switch (status) { |
1351 | case BT_MESSAGE_ITERATOR_STATUS_OK: | |
1352 | break; | |
1353 | case BT_MESSAGE_ITERATOR_STATUS_ERROR: | |
1354 | case BT_MESSAGE_ITERATOR_STATUS_NOMEM: | |
1355 | case BT_MESSAGE_ITERATOR_STATUS_AGAIN: | |
1356 | goto end; | |
1357 | default: | |
1358 | abort(); | |
1359 | } | |
1360 | ||
1361 | /* | |
1362 | * Find the first message which has a default clock | |
1363 | * snapshot greater than or equal to the requested | |
5b9e151d PP |
1364 | * seeking time, and move the received messages from |
1365 | * this point in the batch to this iterator's auto-seek | |
1366 | * message queue. | |
7474e7d3 | 1367 | */ |
5b9e151d PP |
1368 | while (!g_queue_is_empty(iterator->auto_seek_msgs)) { |
1369 | bt_object_put_no_null_check( | |
1370 | g_queue_pop_tail(iterator->auto_seek_msgs)); | |
1371 | } | |
1372 | ||
7474e7d3 PP |
1373 | status = find_message_ge_ns_from_origin(iterator, |
1374 | ns_from_origin); | |
1375 | switch (status) { | |
1376 | case BT_MESSAGE_ITERATOR_STATUS_OK: | |
5b9e151d | 1377 | case BT_MESSAGE_ITERATOR_STATUS_END: |
7474e7d3 | 1378 | /* |
5b9e151d PP |
1379 | * If there are messages in the auto-seek |
1380 | * message queue, replace the user's "next" | |
1381 | * method with a custom, temporary "next" method | |
1382 | * which returns them. | |
7474e7d3 | 1383 | */ |
5b9e151d PP |
1384 | if (!g_queue_is_empty(iterator->auto_seek_msgs)) { |
1385 | iterator->methods.next = | |
1386 | (bt_self_component_port_input_message_iterator_next_method) | |
1387 | post_auto_seek_next; | |
1388 | } | |
1389 | ||
1390 | /* | |
1391 | * `BT_MESSAGE_ITERATOR_STATUS_END` becomes | |
1392 | * `BT_MESSAGE_ITERATOR_STATUS_OK`: the next | |
1393 | * time this iterator's "next" method is called, | |
1394 | * it will return | |
1395 | * `BT_MESSAGE_ITERATOR_STATUS_END`. | |
1396 | */ | |
1397 | status = BT_MESSAGE_ITERATOR_STATUS_OK; | |
7474e7d3 PP |
1398 | break; |
1399 | case BT_MESSAGE_ITERATOR_STATUS_ERROR: | |
1400 | case BT_MESSAGE_ITERATOR_STATUS_NOMEM: | |
1401 | case BT_MESSAGE_ITERATOR_STATUS_AGAIN: | |
1402 | goto end; | |
7474e7d3 PP |
1403 | default: |
1404 | abort(); | |
1405 | } | |
1406 | } | |
1407 | ||
1408 | end: | |
1409 | set_iterator_state_after_seeking(iterator, status); | |
7474e7d3 PP |
1410 | return status; |
1411 | } | |
1412 | ||
1413 | static inline | |
1414 | bt_self_component_port_input_message_iterator * | |
1415 | borrow_output_port_message_iterator_upstream_iterator( | |
1416 | struct bt_port_output_message_iterator *iterator) | |
1417 | { | |
1418 | struct bt_component_class_sink_colander_priv_data *colander_data; | |
1419 | ||
1420 | BT_ASSERT(iterator); | |
1421 | colander_data = (void *) iterator->colander->parent.user_data; | |
1422 | BT_ASSERT(colander_data); | |
1423 | BT_ASSERT(colander_data->msg_iter); | |
1424 | return colander_data->msg_iter; | |
1425 | } | |
1426 | ||
1427 | bt_bool bt_port_output_message_iterator_can_seek_ns_from_origin( | |
1428 | struct bt_port_output_message_iterator *iterator, | |
1429 | int64_t ns_from_origin) | |
1430 | { | |
1431 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); | |
1432 | return bt_self_component_port_input_message_iterator_can_seek_ns_from_origin( | |
1433 | borrow_output_port_message_iterator_upstream_iterator( | |
1434 | iterator), ns_from_origin); | |
1435 | } | |
1436 | ||
1437 | bt_bool bt_port_output_message_iterator_can_seek_beginning( | |
1438 | struct bt_port_output_message_iterator *iterator) | |
1439 | { | |
1440 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); | |
1441 | return bt_self_component_port_input_message_iterator_can_seek_beginning( | |
1442 | borrow_output_port_message_iterator_upstream_iterator( | |
1443 | iterator)); | |
1444 | } | |
1445 | ||
1446 | enum bt_message_iterator_status bt_port_output_message_iterator_seek_ns_from_origin( | |
1447 | struct bt_port_output_message_iterator *iterator, | |
1448 | int64_t ns_from_origin) | |
1449 | { | |
1450 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); | |
1451 | return bt_self_component_port_input_message_iterator_seek_ns_from_origin( | |
1452 | borrow_output_port_message_iterator_upstream_iterator(iterator), | |
1453 | ns_from_origin); | |
1454 | } | |
1455 | ||
1456 | enum bt_message_iterator_status bt_port_output_message_iterator_seek_beginning( | |
1457 | struct bt_port_output_message_iterator *iterator) | |
1458 | { | |
1459 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); | |
1460 | return bt_self_component_port_input_message_iterator_seek_beginning( | |
1461 | borrow_output_port_message_iterator_upstream_iterator( | |
1462 | iterator)); | |
1463 | } | |
1464 | ||
d6e69534 PP |
1465 | void bt_port_output_message_iterator_get_ref( |
1466 | const struct bt_port_output_message_iterator *iterator) | |
c5b9b441 PP |
1467 | { |
1468 | bt_object_get_ref(iterator); | |
1469 | } | |
1470 | ||
d6e69534 PP |
1471 | void bt_port_output_message_iterator_put_ref( |
1472 | const struct bt_port_output_message_iterator *iterator) | |
c5b9b441 PP |
1473 | { |
1474 | bt_object_put_ref(iterator); | |
1475 | } | |
1476 | ||
d6e69534 PP |
1477 | void bt_self_component_port_input_message_iterator_get_ref( |
1478 | const struct bt_self_component_port_input_message_iterator *iterator) | |
c5b9b441 PP |
1479 | { |
1480 | bt_object_get_ref(iterator); | |
1481 | } | |
1482 | ||
d6e69534 PP |
1483 | void bt_self_component_port_input_message_iterator_put_ref( |
1484 | const struct bt_self_component_port_input_message_iterator *iterator) | |
c5b9b441 PP |
1485 | { |
1486 | bt_object_put_ref(iterator); | |
1487 | } |