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