Commit | Line | Data |
---|---|---|
47e5a032 | 1 | /* |
e2f7325d | 2 | * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com> |
47e5a032 | 3 | * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
47e5a032 JG |
4 | * |
5 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
6 | * of this software and associated documentation files (the "Software"), to deal | |
7 | * in the Software without restriction, including without limitation the rights | |
8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
9 | * copies of the Software, and to permit persons to whom the Software is | |
10 | * furnished to do so, subject to the following conditions: | |
11 | * | |
12 | * The above copyright notice and this permission notice shall be included in | |
13 | * all copies or substantial portions of the Software. | |
14 | * | |
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
21 | * SOFTWARE. | |
22 | */ | |
23 | ||
350ad6c1 | 24 | #define BT_LOG_TAG "LIB/MSG-ITER" |
c2d9d9cf | 25 | #include "lib/logging.h" |
5af447e5 | 26 | |
578e048b | 27 | #include "compat/compiler.h" |
e74dbb33 | 28 | #include "compat/glib.h" |
578e048b MJ |
29 | #include "lib/trace-ir/clock-class.h" |
30 | #include "lib/trace-ir/clock-snapshot.h" | |
3fadfbc0 | 31 | #include <babeltrace2/trace-ir/field.h> |
43c59509 | 32 | #include <babeltrace2/trace-ir/event.h> |
578e048b | 33 | #include "lib/trace-ir/event.h" |
43c59509 | 34 | #include <babeltrace2/trace-ir/packet.h> |
578e048b MJ |
35 | #include "lib/trace-ir/packet.h" |
36 | #include "lib/trace-ir/stream.h" | |
43c59509 PP |
37 | #include <babeltrace2/trace-ir/clock-class.h> |
38 | #include <babeltrace2/trace-ir/stream-class.h> | |
39 | #include <babeltrace2/trace-ir/stream.h> | |
40 | #include <babeltrace2/graph/connection.h> | |
41 | #include <babeltrace2/graph/component.h> | |
42 | #include <babeltrace2/graph/message.h> | |
43 | #include <babeltrace2/graph/self-component.h> | |
44 | #include <babeltrace2/graph/port.h> | |
3fadfbc0 | 45 | #include <babeltrace2/graph/graph.h> |
43c59509 | 46 | #include <babeltrace2/graph/message-iterator.h> |
3fadfbc0 | 47 | #include <babeltrace2/types.h> |
578e048b MJ |
48 | #include "common/assert.h" |
49 | #include "lib/assert-pre.h" | |
f6f301d7 | 50 | #include "lib/assert-post.h" |
fa054faf | 51 | #include <stdint.h> |
2ec84d26 | 52 | #include <inttypes.h> |
c4f23e30 | 53 | #include <stdbool.h> |
0fbb9a9f | 54 | #include <stdlib.h> |
3230ee6b | 55 | |
578e048b | 56 | #include "component-class.h" |
578e048b MJ |
57 | #include "component.h" |
58 | #include "component-sink.h" | |
59 | #include "component-source.h" | |
60 | #include "connection.h" | |
61 | #include "graph.h" | |
a3f0c7db | 62 | #include "message-iterator-class.h" |
578e048b MJ |
63 | #include "message/discarded-items.h" |
64 | #include "message/event.h" | |
65 | #include "message/iterator.h" | |
66 | #include "message/message.h" | |
67 | #include "message/message-iterator-inactivity.h" | |
68 | #include "message/stream.h" | |
69 | #include "message/packet.h" | |
d24d5663 | 70 | #include "lib/func-status.h" |
578e048b | 71 | |
d4393e08 PP |
72 | /* |
73 | * TODO: Use graph's state (number of active iterators, etc.) and | |
74 | * possibly system specifications to make a better guess than this. | |
75 | */ | |
d6e69534 | 76 | #define MSG_BATCH_SIZE 15 |
d4393e08 | 77 | |
7474e7d3 | 78 | #define BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(_iter) \ |
9a2c8b8e PP |
79 | BT_ASSERT_PRE((_iter)->state == BT_MESSAGE_ITERATOR_STATE_ACTIVE || \ |
80 | (_iter)->state == BT_MESSAGE_ITERATOR_STATE_ENDED || \ | |
81 | (_iter)->state == BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN || \ | |
82 | (_iter)->state == BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR, \ | |
7474e7d3 | 83 | "Message iterator is in the wrong state: %!+i", _iter) |
47e5a032 | 84 | |
d0fea130 | 85 | static inline |
9a2c8b8e PP |
86 | void set_msg_iterator_state(struct bt_message_iterator *iterator, |
87 | enum bt_message_iterator_state state) | |
d0fea130 | 88 | { |
98b15851 | 89 | BT_ASSERT_DBG(iterator); |
7474e7d3 | 90 | BT_LIB_LOGD("Updating message iterator's state: new-state=%s", |
9a2c8b8e | 91 | bt_message_iterator_state_string(state)); |
d0fea130 PP |
92 | iterator->state = state; |
93 | } | |
94 | ||
47e5a032 | 95 | static |
9a2c8b8e | 96 | void bt_message_iterator_destroy(struct bt_object *obj) |
47e5a032 | 97 | { |
9a2c8b8e | 98 | struct bt_message_iterator *iterator; |
8738a040 | 99 | |
f6ccaed9 | 100 | BT_ASSERT(obj); |
d3eb6e8f | 101 | |
bd14d768 | 102 | /* |
d6e69534 | 103 | * The message iterator's reference count is 0 if we're |
bd14d768 PP |
104 | * here. Increment it to avoid a double-destroy (possibly |
105 | * infinitely recursive). This could happen for example if the | |
d6e69534 | 106 | * message iterator's finalization function does |
d94d92ac PP |
107 | * bt_object_get_ref() (or anything that causes |
108 | * bt_object_get_ref() to be called) on itself (ref. count goes | |
109 | * from 0 to 1), and then bt_object_put_ref(): the reference | |
110 | * count would go from 1 to 0 again and this function would be | |
111 | * called again. | |
bd14d768 | 112 | */ |
3fea54f6 | 113 | obj->ref_count++; |
07245ac2 | 114 | iterator = (void *) obj; |
3f7d4d90 | 115 | BT_LIB_LOGI("Destroying self component input port message iterator object: " |
d94d92ac | 116 | "%!+i", iterator); |
9a2c8b8e | 117 | bt_message_iterator_try_finalize(iterator); |
d3eb6e8f | 118 | |
bd14d768 PP |
119 | if (iterator->connection) { |
120 | /* | |
121 | * Remove ourself from the originating connection so | |
122 | * that it does not try to finalize a dangling pointer | |
123 | * later. | |
124 | */ | |
125 | bt_connection_remove_iterator(iterator->connection, iterator); | |
d94d92ac | 126 | iterator->connection = NULL; |
bd14d768 PP |
127 | } |
128 | ||
da9c4c52 SM |
129 | if (iterator->auto_seek.msgs) { |
130 | while (!g_queue_is_empty(iterator->auto_seek.msgs)) { | |
6871026b | 131 | bt_object_put_ref_no_null_check( |
da9c4c52 | 132 | g_queue_pop_tail(iterator->auto_seek.msgs)); |
7474e7d3 PP |
133 | } |
134 | ||
da9c4c52 SM |
135 | g_queue_free(iterator->auto_seek.msgs); |
136 | iterator->auto_seek.msgs = NULL; | |
7474e7d3 PP |
137 | } |
138 | ||
ca02df0a PP |
139 | if (iterator->upstream_msg_iters) { |
140 | /* | |
141 | * At this point the message iterator is finalized, so | |
142 | * it's detached from any upstream message iterator. | |
143 | */ | |
144 | BT_ASSERT(iterator->upstream_msg_iters->len == 0); | |
145 | g_ptr_array_free(iterator->upstream_msg_iters, TRUE); | |
146 | iterator->upstream_msg_iters = NULL; | |
147 | } | |
148 | ||
6c373cc9 PP |
149 | if (iterator->msgs) { |
150 | g_ptr_array_free(iterator->msgs, TRUE); | |
151 | iterator->msgs = NULL; | |
152 | } | |
153 | ||
154 | g_free(iterator); | |
47e5a032 JG |
155 | } |
156 | ||
bd14d768 | 157 | BT_HIDDEN |
9a2c8b8e PP |
158 | void bt_message_iterator_try_finalize( |
159 | struct bt_message_iterator *iterator) | |
bd14d768 | 160 | { |
ca02df0a | 161 | uint64_t i; |
fca28f75 | 162 | bool call_user_finalize = true; |
bd14d768 | 163 | |
f6ccaed9 | 164 | BT_ASSERT(iterator); |
bd14d768 PP |
165 | |
166 | switch (iterator->state) { | |
9a2c8b8e | 167 | case BT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED: |
fca28f75 SM |
168 | /* |
169 | * If this function is called while the iterator is in the | |
170 | * NON_INITIALIZED state, it means the user initialization | |
171 | * method has either not been called, or has failed. We | |
172 | * therefore don't want to call the user finalization method. | |
173 | * However, the initialization method might have created some | |
174 | * upstream message iterators before failing, so we want to | |
175 | * execute the rest of this function, which unlinks the related | |
176 | * iterators. | |
177 | */ | |
178 | call_user_finalize = false; | |
179 | break; | |
9a2c8b8e | 180 | case BT_MESSAGE_ITERATOR_STATE_FINALIZED: |
bd14d768 | 181 | /* Already finalized */ |
d6e69534 | 182 | BT_LIB_LOGD("Not finalizing message iterator: already finalized: " |
d94d92ac | 183 | "%!+i", iterator); |
d0fea130 | 184 | goto end; |
9a2c8b8e | 185 | case BT_MESSAGE_ITERATOR_STATE_FINALIZING: |
870631a2 | 186 | /* Finalizing */ |
d0fea130 PP |
187 | BT_LIB_LOGF("Message iterator is already being finalized: " |
188 | "%!+i", iterator); | |
498e7994 | 189 | bt_common_abort(); |
bd14d768 PP |
190 | default: |
191 | break; | |
192 | } | |
193 | ||
d6e69534 | 194 | BT_LIB_LOGD("Finalizing message iterator: %!+i", iterator); |
9a2c8b8e PP |
195 | set_msg_iterator_state(iterator, |
196 | BT_MESSAGE_ITERATOR_STATE_FINALIZING); | |
f6ccaed9 | 197 | BT_ASSERT(iterator->upstream_component); |
bd14d768 PP |
198 | |
199 | /* Call user-defined destroy method */ | |
fca28f75 | 200 | if (call_user_finalize) { |
a3f0c7db | 201 | typedef void (*method_t)(void *); |
41a3efcd | 202 | method_t method; |
fca28f75 SM |
203 | struct bt_component_class *comp_class = |
204 | iterator->upstream_component->class; | |
41a3efcd | 205 | struct bt_component_class_with_iterator_class *class_with_iter_class; |
fca28f75 | 206 | |
41a3efcd SM |
207 | BT_ASSERT(bt_component_class_has_message_iterator_class(comp_class)); |
208 | class_with_iter_class = container_of(comp_class, | |
209 | struct bt_component_class_with_iterator_class, parent); | |
210 | method = (method_t) class_with_iter_class->msg_iter_cls->methods.finalize; | |
bd14d768 | 211 | |
fca28f75 SM |
212 | if (method) { |
213 | const bt_error *saved_error; | |
42a63165 | 214 | |
fca28f75 | 215 | saved_error = bt_current_thread_take_error(); |
42a63165 | 216 | |
fca28f75 SM |
217 | BT_LIB_LOGD("Calling user's finalization method: %!+i", |
218 | iterator); | |
219 | method(iterator); | |
42a63165 | 220 | |
fca28f75 SM |
221 | if (saved_error) { |
222 | BT_CURRENT_THREAD_MOVE_ERROR_AND_RESET(saved_error); | |
223 | } | |
42a63165 | 224 | } |
bd14d768 PP |
225 | } |
226 | ||
ca02df0a PP |
227 | /* Detach upstream message iterators */ |
228 | for (i = 0; i < iterator->upstream_msg_iters->len; i++) { | |
9a2c8b8e | 229 | struct bt_message_iterator *upstream_msg_iter = |
ca02df0a PP |
230 | iterator->upstream_msg_iters->pdata[i]; |
231 | ||
232 | upstream_msg_iter->downstream_msg_iter = NULL; | |
233 | } | |
234 | ||
235 | g_ptr_array_set_size(iterator->upstream_msg_iters, 0); | |
236 | ||
237 | /* Detach downstream message iterator */ | |
238 | if (iterator->downstream_msg_iter) { | |
239 | gboolean existed; | |
240 | ||
241 | BT_ASSERT(iterator->downstream_msg_iter->upstream_msg_iters); | |
242 | existed = g_ptr_array_remove_fast( | |
243 | iterator->downstream_msg_iter->upstream_msg_iters, | |
244 | iterator); | |
245 | BT_ASSERT(existed); | |
246 | } | |
247 | ||
bd14d768 PP |
248 | iterator->upstream_component = NULL; |
249 | iterator->upstream_port = NULL; | |
9a2c8b8e PP |
250 | set_msg_iterator_state(iterator, |
251 | BT_MESSAGE_ITERATOR_STATE_FINALIZED); | |
d6e69534 | 252 | BT_LIB_LOGD("Finalized message iterator: %!+i", iterator); |
d0fea130 PP |
253 | |
254 | end: | |
255 | return; | |
bd14d768 PP |
256 | } |
257 | ||
258 | BT_HIDDEN | |
9a2c8b8e PP |
259 | void bt_message_iterator_set_connection( |
260 | struct bt_message_iterator *iterator, | |
bd14d768 PP |
261 | struct bt_connection *connection) |
262 | { | |
f6ccaed9 | 263 | BT_ASSERT(iterator); |
bd14d768 | 264 | iterator->connection = connection; |
3f7d4d90 | 265 | BT_LIB_LOGI("Set message iterator's connection: " |
d94d92ac | 266 | "%![iter-]+i, %![conn-]+x", iterator, connection); |
bd14d768 PP |
267 | } |
268 | ||
7474e7d3 | 269 | static |
f2fb1b32 | 270 | enum bt_message_iterator_can_seek_beginning_status can_seek_ns_from_origin_true( |
9a2c8b8e | 271 | struct bt_message_iterator *iterator, |
f2fb1b32 | 272 | int64_t ns_from_origin, bt_bool *can_seek) |
7474e7d3 | 273 | { |
f2fb1b32 SM |
274 | *can_seek = BT_TRUE; |
275 | ||
276 | return BT_FUNC_STATUS_OK; | |
7474e7d3 PP |
277 | } |
278 | ||
279 | static | |
f2fb1b32 | 280 | enum bt_message_iterator_can_seek_beginning_status can_seek_beginning_true( |
9a2c8b8e | 281 | struct bt_message_iterator *iterator, |
f2fb1b32 | 282 | bt_bool *can_seek) |
7474e7d3 | 283 | { |
f2fb1b32 SM |
284 | *can_seek = BT_TRUE; |
285 | ||
286 | return BT_FUNC_STATUS_OK; | |
7474e7d3 PP |
287 | } |
288 | ||
d94d92ac | 289 | static |
e803df70 | 290 | int create_self_component_input_port_message_iterator( |
ca02df0a | 291 | struct bt_self_message_iterator *self_downstream_msg_iter, |
e803df70 | 292 | struct bt_self_component_port_input *self_port, |
9a2c8b8e | 293 | struct bt_message_iterator **message_iterator) |
47e5a032 | 294 | { |
a3f0c7db | 295 | bt_message_iterator_class_initialize_method init_method = NULL; |
9a2c8b8e | 296 | struct bt_message_iterator *iterator = |
ca02df0a | 297 | NULL; |
9a2c8b8e | 298 | struct bt_message_iterator *downstream_msg_iter = |
ca02df0a PP |
299 | (void *) self_downstream_msg_iter; |
300 | struct bt_port *port = (void *) self_port; | |
301 | struct bt_port *upstream_port; | |
302 | struct bt_component *comp; | |
303 | struct bt_component *upstream_comp; | |
304 | struct bt_component_class *upstream_comp_cls; | |
41a3efcd | 305 | struct bt_component_class_with_iterator_class *upstream_comp_cls_with_iter_cls; |
e803df70 | 306 | int status; |
47e5a032 | 307 | |
e803df70 | 308 | BT_ASSERT_PRE_NON_NULL(message_iterator, "Created message iterator"); |
ca02df0a PP |
309 | BT_ASSERT_PRE_NON_NULL(port, "Input port"); |
310 | comp = bt_port_borrow_component_inline(port); | |
311 | BT_ASSERT_PRE(bt_port_is_connected(port), | |
312 | "Input port is not connected: %![port-]+p", port); | |
313 | BT_ASSERT_PRE(comp, "Input port is not part of a component: %![port-]+p", | |
314 | port); | |
ca02df0a PP |
315 | BT_ASSERT(port->connection); |
316 | upstream_port = port->connection->upstream_port; | |
f6ccaed9 | 317 | BT_ASSERT(upstream_port); |
ca02df0a PP |
318 | upstream_comp = bt_port_borrow_component_inline(upstream_port); |
319 | BT_ASSERT(upstream_comp); | |
320 | BT_ASSERT_PRE( | |
9b4f9b42 PP |
321 | bt_component_borrow_graph(upstream_comp)->config_state == |
322 | BT_GRAPH_CONFIGURATION_STATE_PARTIALLY_CONFIGURED || | |
323 | bt_component_borrow_graph(upstream_comp)->config_state == | |
324 | BT_GRAPH_CONFIGURATION_STATE_CONFIGURED, | |
ca02df0a PP |
325 | "Graph is not configured: %!+g", |
326 | bt_component_borrow_graph(upstream_comp)); | |
327 | upstream_comp_cls = upstream_comp->class; | |
328 | BT_ASSERT(upstream_comp->class->type == | |
d94d92ac | 329 | BT_COMPONENT_CLASS_TYPE_SOURCE || |
ca02df0a | 330 | upstream_comp->class->type == |
d94d92ac | 331 | BT_COMPONENT_CLASS_TYPE_FILTER); |
ca02df0a PP |
332 | BT_LIB_LOGI("Creating message iterator on self component input port: " |
333 | "%![up-comp-]+c, %![up-port-]+p", upstream_comp, upstream_port); | |
d94d92ac | 334 | iterator = g_new0( |
9a2c8b8e | 335 | struct bt_message_iterator, 1); |
47e5a032 | 336 | if (!iterator) { |
870631a2 PP |
337 | BT_LIB_LOGE_APPEND_CAUSE( |
338 | "Failed to allocate one self component input port " | |
d6e69534 | 339 | "message iterator."); |
e803df70 | 340 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
870631a2 | 341 | goto error; |
47e5a032 JG |
342 | } |
343 | ||
6c373cc9 | 344 | bt_object_init_shared(&iterator->base, |
9a2c8b8e | 345 | bt_message_iterator_destroy); |
6c373cc9 PP |
346 | iterator->msgs = g_ptr_array_new(); |
347 | if (!iterator->msgs) { | |
348 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray."); | |
e803df70 | 349 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
870631a2 | 350 | goto error; |
d4393e08 | 351 | } |
3230ee6b | 352 | |
6c373cc9 | 353 | g_ptr_array_set_size(iterator->msgs, MSG_BATCH_SIZE); |
54b135a0 | 354 | iterator->last_ns_from_origin = INT64_MIN; |
da9c4c52 SM |
355 | iterator->auto_seek.msgs = g_queue_new(); |
356 | if (!iterator->auto_seek.msgs) { | |
870631a2 | 357 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GQueue."); |
e803df70 | 358 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
ca02df0a PP |
359 | goto error; |
360 | } | |
361 | ||
362 | iterator->upstream_msg_iters = g_ptr_array_new(); | |
363 | if (!iterator->upstream_msg_iters) { | |
364 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GPtrArray."); | |
e803df70 | 365 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
ca02df0a | 366 | goto error; |
3230ee6b PP |
367 | } |
368 | ||
bd14d768 PP |
369 | iterator->upstream_component = upstream_comp; |
370 | iterator->upstream_port = upstream_port; | |
d94d92ac | 371 | iterator->connection = iterator->upstream_port->connection; |
5c563278 | 372 | iterator->graph = bt_component_borrow_graph(upstream_comp); |
9a2c8b8e PP |
373 | set_msg_iterator_state(iterator, |
374 | BT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED); | |
7474e7d3 | 375 | |
41a3efcd SM |
376 | /* Copy methods from the message iterator class to the message iterator. */ |
377 | BT_ASSERT(bt_component_class_has_message_iterator_class(upstream_comp_cls)); | |
378 | upstream_comp_cls_with_iter_cls = container_of(upstream_comp_cls, | |
379 | struct bt_component_class_with_iterator_class, parent); | |
380 | ||
381 | iterator->methods.next = | |
9a2c8b8e | 382 | (bt_message_iterator_next_method) |
41a3efcd SM |
383 | upstream_comp_cls_with_iter_cls->msg_iter_cls->methods.next; |
384 | iterator->methods.seek_ns_from_origin = | |
9a2c8b8e | 385 | (bt_message_iterator_seek_ns_from_origin_method) |
41a3efcd SM |
386 | upstream_comp_cls_with_iter_cls->msg_iter_cls->methods.seek_ns_from_origin; |
387 | iterator->methods.seek_beginning = | |
9a2c8b8e | 388 | (bt_message_iterator_seek_beginning_method) |
41a3efcd SM |
389 | upstream_comp_cls_with_iter_cls->msg_iter_cls->methods.seek_beginning; |
390 | iterator->methods.can_seek_ns_from_origin = | |
9a2c8b8e | 391 | (bt_message_iterator_can_seek_ns_from_origin_method) |
41a3efcd SM |
392 | upstream_comp_cls_with_iter_cls->msg_iter_cls->methods.can_seek_ns_from_origin; |
393 | iterator->methods.can_seek_beginning = | |
9a2c8b8e | 394 | (bt_message_iterator_can_seek_beginning_method) |
41a3efcd | 395 | upstream_comp_cls_with_iter_cls->msg_iter_cls->methods.can_seek_beginning; |
7474e7d3 PP |
396 | |
397 | if (iterator->methods.seek_ns_from_origin && | |
398 | !iterator->methods.can_seek_ns_from_origin) { | |
399 | iterator->methods.can_seek_ns_from_origin = | |
9a2c8b8e | 400 | (bt_message_iterator_can_seek_ns_from_origin_method) |
7474e7d3 PP |
401 | can_seek_ns_from_origin_true; |
402 | } | |
403 | ||
404 | if (iterator->methods.seek_beginning && | |
405 | !iterator->methods.can_seek_beginning) { | |
406 | iterator->methods.can_seek_beginning = | |
9a2c8b8e | 407 | (bt_message_iterator_can_seek_beginning_method) |
7474e7d3 PP |
408 | can_seek_beginning_true; |
409 | } | |
410 | ||
41a3efcd SM |
411 | /* Call iterator's init method. */ |
412 | init_method = upstream_comp_cls_with_iter_cls->msg_iter_cls->methods.initialize; | |
d94d92ac PP |
413 | |
414 | if (init_method) { | |
a3f0c7db | 415 | enum bt_message_iterator_class_initialize_method_status iter_status; |
d94d92ac PP |
416 | |
417 | BT_LIB_LOGD("Calling user's initialization method: %!+i", iterator); | |
a3f0c7db SM |
418 | iter_status = init_method( |
419 | (struct bt_self_message_iterator *) iterator, | |
420 | &iterator->config, | |
a3f0c7db | 421 | (struct bt_self_component_port_output *) upstream_port); |
d94d92ac | 422 | BT_LOGD("User method returned: status=%s", |
d24d5663 | 423 | bt_common_func_status_string(iter_status)); |
6ecdcca3 | 424 | BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(iter_status); |
d24d5663 | 425 | if (iter_status != BT_FUNC_STATUS_OK) { |
870631a2 PP |
426 | BT_LIB_LOGW_APPEND_CAUSE( |
427 | "Component input port message iterator initialization method failed: " | |
428 | "%![iter-]+i, status=%s", | |
429 | iterator, | |
430 | bt_common_func_status_string(iter_status)); | |
e803df70 | 431 | status = iter_status; |
870631a2 | 432 | goto error; |
d94d92ac | 433 | } |
8d8b141d SM |
434 | |
435 | iterator->config.frozen = true; | |
d94d92ac PP |
436 | } |
437 | ||
ca02df0a PP |
438 | if (downstream_msg_iter) { |
439 | /* Set this message iterator's downstream message iterator */ | |
440 | iterator->downstream_msg_iter = downstream_msg_iter; | |
441 | ||
442 | /* | |
443 | * Add this message iterator to the downstream message | |
444 | * iterator's array of upstream message iterators. | |
445 | */ | |
446 | g_ptr_array_add(downstream_msg_iter->upstream_msg_iters, | |
447 | iterator); | |
448 | } | |
449 | ||
9a2c8b8e PP |
450 | set_msg_iterator_state(iterator, |
451 | BT_MESSAGE_ITERATOR_STATE_ACTIVE); | |
d94d92ac | 452 | g_ptr_array_add(port->connection->iterators, iterator); |
3f7d4d90 | 453 | BT_LIB_LOGI("Created message iterator on self component input port: " |
d94d92ac PP |
454 | "%![up-port-]+p, %![up-comp-]+c, %![iter-]+i", |
455 | upstream_port, upstream_comp, iterator); | |
e803df70 SM |
456 | |
457 | *message_iterator = iterator; | |
458 | status = BT_FUNC_STATUS_OK; | |
870631a2 PP |
459 | goto end; |
460 | ||
461 | error: | |
462 | BT_OBJECT_PUT_REF_AND_RESET(iterator); | |
d94d92ac PP |
463 | |
464 | end: | |
e803df70 | 465 | return status; |
ea8d3e58 JG |
466 | } |
467 | ||
9a2c8b8e PP |
468 | bt_message_iterator_create_from_message_iterator_status |
469 | bt_message_iterator_create_from_message_iterator( | |
ca02df0a | 470 | struct bt_self_message_iterator *self_msg_iter, |
e803df70 | 471 | struct bt_self_component_port_input *input_port, |
9a2c8b8e | 472 | struct bt_message_iterator **message_iterator) |
ca02df0a | 473 | { |
17f3083a | 474 | BT_ASSERT_PRE_NO_ERROR(); |
ca02df0a PP |
475 | BT_ASSERT_PRE_NON_NULL(self_msg_iter, "Message iterator"); |
476 | return create_self_component_input_port_message_iterator(self_msg_iter, | |
e803df70 | 477 | input_port, message_iterator); |
ca02df0a PP |
478 | } |
479 | ||
9a2c8b8e PP |
480 | bt_message_iterator_create_from_sink_component_status |
481 | bt_message_iterator_create_from_sink_component( | |
ca02df0a | 482 | struct bt_self_component_sink *self_comp, |
e803df70 | 483 | struct bt_self_component_port_input *input_port, |
9a2c8b8e | 484 | struct bt_message_iterator **message_iterator) |
ca02df0a | 485 | { |
17f3083a | 486 | BT_ASSERT_PRE_NO_ERROR(); |
ca02df0a PP |
487 | BT_ASSERT_PRE_NON_NULL(self_comp, "Sink component"); |
488 | return create_self_component_input_port_message_iterator(NULL, | |
e803df70 | 489 | input_port, message_iterator); |
ca02df0a PP |
490 | } |
491 | ||
d6e69534 PP |
492 | void *bt_self_message_iterator_get_data( |
493 | const struct bt_self_message_iterator *self_iterator) | |
ea8d3e58 | 494 | { |
9a2c8b8e | 495 | struct bt_message_iterator *iterator = |
d94d92ac | 496 | (void *) self_iterator; |
ea8d3e58 | 497 | |
bdb288b3 | 498 | BT_ASSERT_PRE_DEV_NON_NULL(iterator, "Message iterator"); |
d94d92ac | 499 | return iterator->user_data; |
8738a040 | 500 | } |
413bc2c4 | 501 | |
d6e69534 PP |
502 | void bt_self_message_iterator_set_data( |
503 | struct bt_self_message_iterator *self_iterator, void *data) | |
5c563278 | 504 | { |
9a2c8b8e | 505 | struct bt_message_iterator *iterator = |
d94d92ac | 506 | (void *) self_iterator; |
5c563278 | 507 | |
bdb288b3 | 508 | BT_ASSERT_PRE_DEV_NON_NULL(iterator, "Message iterator"); |
d94d92ac | 509 | iterator->user_data = data; |
3f7d4d90 | 510 | BT_LIB_LOGD("Set message iterator's user data: " |
d94d92ac | 511 | "%!+i, user-data-addr=%p", iterator, data); |
5c563278 PP |
512 | } |
513 | ||
8d8b141d SM |
514 | void bt_self_message_iterator_configuration_set_can_seek_forward( |
515 | bt_self_message_iterator_configuration *config, | |
516 | bt_bool can_seek_forward) | |
517 | { | |
518 | BT_ASSERT_PRE_NON_NULL(config, "Message iterator configuration"); | |
519 | BT_ASSERT_PRE_DEV_HOT(config, "Message iterator configuration", ""); | |
520 | ||
521 | config->can_seek_forward = can_seek_forward; | |
522 | } | |
523 | ||
54b135a0 SM |
524 | /* |
525 | * Validate that the default clock snapshot in `msg` doesn't make us go back in | |
526 | * time. | |
527 | */ | |
528 | ||
bdb288b3 | 529 | BT_ASSERT_POST_DEV_FUNC |
54b135a0 SM |
530 | static |
531 | bool clock_snapshots_are_monotonic_one( | |
9a2c8b8e | 532 | struct bt_message_iterator *iterator, |
54b135a0 SM |
533 | const bt_message *msg) |
534 | { | |
535 | const struct bt_clock_snapshot *clock_snapshot = NULL; | |
536 | bt_message_type message_type = bt_message_get_type(msg); | |
537 | int64_t ns_from_origin; | |
d24d5663 | 538 | enum bt_clock_snapshot_get_ns_from_origin_status clock_snapshot_status; |
54b135a0 SM |
539 | |
540 | /* | |
541 | * The default is true: if we can't figure out the clock snapshot | |
542 | * (or there is none), assume it is fine. | |
543 | */ | |
544 | bool result = true; | |
545 | ||
546 | switch (message_type) { | |
547 | case BT_MESSAGE_TYPE_EVENT: | |
548 | { | |
549 | struct bt_message_event *event_msg = (struct bt_message_event *) msg; | |
550 | clock_snapshot = event_msg->default_cs; | |
551 | break; | |
552 | } | |
553 | case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY: | |
554 | { | |
555 | struct bt_message_message_iterator_inactivity *inactivity_msg = | |
556 | (struct bt_message_message_iterator_inactivity *) msg; | |
60d02328 | 557 | clock_snapshot = inactivity_msg->cs; |
54b135a0 SM |
558 | break; |
559 | } | |
560 | case BT_MESSAGE_TYPE_PACKET_BEGINNING: | |
561 | case BT_MESSAGE_TYPE_PACKET_END: | |
562 | { | |
563 | struct bt_message_packet *packet_msg = (struct bt_message_packet *) msg; | |
564 | clock_snapshot = packet_msg->default_cs; | |
565 | break; | |
566 | } | |
188edac1 SM |
567 | case BT_MESSAGE_TYPE_STREAM_BEGINNING: |
568 | case BT_MESSAGE_TYPE_STREAM_END: | |
54b135a0 | 569 | { |
188edac1 SM |
570 | struct bt_message_stream *stream_msg = (struct bt_message_stream *) msg; |
571 | if (stream_msg->default_cs_state != BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN) { | |
572 | goto end; | |
54b135a0 | 573 | } |
188edac1 SM |
574 | |
575 | clock_snapshot = stream_msg->default_cs; | |
54b135a0 SM |
576 | break; |
577 | } | |
54b135a0 SM |
578 | case BT_MESSAGE_TYPE_DISCARDED_EVENTS: |
579 | case BT_MESSAGE_TYPE_DISCARDED_PACKETS: | |
580 | { | |
581 | struct bt_message_discarded_items *discarded_msg = | |
582 | (struct bt_message_discarded_items *) msg; | |
583 | ||
584 | clock_snapshot = discarded_msg->default_begin_cs; | |
585 | break; | |
586 | } | |
587 | } | |
588 | ||
589 | if (!clock_snapshot) { | |
590 | goto end; | |
591 | } | |
592 | ||
d747e85f SM |
593 | clock_snapshot_status = bt_clock_snapshot_get_ns_from_origin( |
594 | clock_snapshot, &ns_from_origin); | |
d24d5663 | 595 | if (clock_snapshot_status != BT_FUNC_STATUS_OK) { |
d747e85f SM |
596 | /* |
597 | * bt_clock_snapshot_get_ns_from_origin can return | |
598 | * OVERFLOW_ERROR. We don't really want to report an error to | |
599 | * our caller, so just clear it. | |
600 | */ | |
601 | bt_current_thread_clear_error(); | |
54b135a0 SM |
602 | goto end; |
603 | } | |
604 | ||
605 | result = ns_from_origin >= iterator->last_ns_from_origin; | |
606 | iterator->last_ns_from_origin = ns_from_origin; | |
607 | end: | |
608 | return result; | |
609 | } | |
610 | ||
bdb288b3 | 611 | BT_ASSERT_POST_DEV_FUNC |
54b135a0 SM |
612 | static |
613 | bool clock_snapshots_are_monotonic( | |
9a2c8b8e | 614 | struct bt_message_iterator *iterator, |
54b135a0 SM |
615 | bt_message_array_const msgs, uint64_t msg_count) |
616 | { | |
617 | uint64_t i; | |
618 | bool result; | |
619 | ||
620 | for (i = 0; i < msg_count; i++) { | |
621 | if (!clock_snapshots_are_monotonic_one(iterator, msgs[i])) { | |
622 | result = false; | |
623 | goto end; | |
624 | } | |
625 | } | |
626 | ||
627 | result = true; | |
628 | ||
629 | end: | |
630 | return result; | |
631 | } | |
632 | ||
633 | /* | |
634 | * When a new stream begins, verify that the clock class tied to this | |
635 | * stream is compatible with what we've seen before. | |
636 | */ | |
637 | ||
bdb288b3 | 638 | BT_ASSERT_POST_DEV_FUNC |
54b135a0 | 639 | static |
9a2c8b8e | 640 | bool clock_classes_are_compatible_one(struct bt_message_iterator *iterator, |
54b135a0 SM |
641 | const struct bt_message *msg) |
642 | { | |
643 | enum bt_message_type message_type = bt_message_get_type(msg); | |
644 | bool result; | |
645 | ||
646 | if (message_type == BT_MESSAGE_TYPE_STREAM_BEGINNING) { | |
647 | const struct bt_message_stream *stream_msg = (struct bt_message_stream *) msg; | |
648 | const struct bt_clock_class *clock_class = stream_msg->stream->class->default_clock_class; | |
649 | bt_uuid clock_class_uuid = NULL; | |
650 | ||
651 | if (clock_class) { | |
652 | clock_class_uuid = bt_clock_class_get_uuid(clock_class); | |
653 | } | |
654 | ||
655 | switch (iterator->clock_expectation.type) { | |
656 | case CLOCK_EXPECTATION_UNSET: | |
657 | /* | |
658 | * This is the first time we see a message with a clock | |
659 | * snapshot: record the properties of that clock, against | |
660 | * which we'll compare the clock properties of the following | |
661 | * messages. | |
662 | */ | |
663 | ||
664 | if (!clock_class) { | |
665 | iterator->clock_expectation.type = CLOCK_EXPECTATION_NONE; | |
666 | } else if (bt_clock_class_origin_is_unix_epoch(clock_class)) { | |
667 | iterator->clock_expectation.type = CLOCK_EXPECTATION_ORIGIN_UNIX; | |
668 | } else if (clock_class_uuid) { | |
669 | iterator->clock_expectation.type = CLOCK_EXPECTATION_ORIGIN_OTHER_UUID; | |
6162e6b7 | 670 | bt_uuid_copy(iterator->clock_expectation.uuid, clock_class_uuid); |
54b135a0 SM |
671 | } else { |
672 | iterator->clock_expectation.type = CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID; | |
673 | } | |
674 | break; | |
675 | ||
676 | case CLOCK_EXPECTATION_NONE: | |
677 | if (clock_class) { | |
bdb288b3 PP |
678 | BT_ASSERT_POST_DEV_MSG( |
679 | "Expecting no clock class, got one: %![cc-]+K", | |
54b135a0 SM |
680 | clock_class); |
681 | result = false; | |
682 | goto end; | |
683 | } | |
684 | ||
685 | break; | |
686 | ||
687 | case CLOCK_EXPECTATION_ORIGIN_UNIX: | |
688 | if (!clock_class) { | |
bdb288b3 PP |
689 | BT_ASSERT_POST_DEV_MSG( |
690 | "Expecting a clock class, got none."); | |
54b135a0 SM |
691 | result = false; |
692 | goto end; | |
693 | } | |
694 | ||
695 | if (!bt_clock_class_origin_is_unix_epoch(clock_class)) { | |
bdb288b3 PP |
696 | BT_ASSERT_POST_DEV_MSG( |
697 | "Expecting a clock class with Unix epoch origin: %![cc-]+K", | |
54b135a0 SM |
698 | clock_class); |
699 | result = false; | |
700 | goto end; | |
701 | } | |
702 | break; | |
703 | ||
704 | case CLOCK_EXPECTATION_ORIGIN_OTHER_UUID: | |
705 | if (!clock_class) { | |
bdb288b3 PP |
706 | BT_ASSERT_POST_DEV_MSG( |
707 | "Expecting a clock class, got none."); | |
54b135a0 SM |
708 | result = false; |
709 | goto end; | |
710 | } | |
711 | ||
712 | if (bt_clock_class_origin_is_unix_epoch(clock_class)) { | |
bdb288b3 PP |
713 | BT_ASSERT_POST_DEV_MSG( |
714 | "Expecting a clock class without Unix epoch origin: %![cc-]+K", | |
54b135a0 SM |
715 | clock_class); |
716 | result = false; | |
717 | goto end; | |
718 | } | |
719 | ||
720 | if (!clock_class_uuid) { | |
bdb288b3 PP |
721 | BT_ASSERT_POST_DEV_MSG( |
722 | "Expecting a clock class with UUID: %![cc-]+K", | |
54b135a0 SM |
723 | clock_class); |
724 | result = false; | |
725 | goto end; | |
726 | } | |
727 | ||
728 | if (bt_uuid_compare(iterator->clock_expectation.uuid, clock_class_uuid)) { | |
bdb288b3 PP |
729 | BT_ASSERT_POST_DEV_MSG( |
730 | "Expecting a clock class with UUID, got one " | |
54b135a0 SM |
731 | "with a different UUID: %![cc-]+K, expected-uuid=%!u", |
732 | clock_class, iterator->clock_expectation.uuid); | |
733 | result = false; | |
734 | goto end; | |
735 | } | |
736 | break; | |
737 | ||
738 | case CLOCK_EXPECTATION_ORIGIN_OTHER_NO_UUID: | |
739 | if (!clock_class) { | |
bdb288b3 PP |
740 | BT_ASSERT_POST_DEV_MSG( |
741 | "Expecting a clock class, got none."); | |
54b135a0 SM |
742 | result = false; |
743 | goto end; | |
744 | } | |
745 | ||
746 | if (bt_clock_class_origin_is_unix_epoch(clock_class)) { | |
bdb288b3 PP |
747 | BT_ASSERT_POST_DEV_MSG( |
748 | "Expecting a clock class without Unix epoch origin: %![cc-]+K", | |
54b135a0 SM |
749 | clock_class); |
750 | result = false; | |
751 | goto end; | |
752 | } | |
753 | ||
754 | if (clock_class_uuid) { | |
bdb288b3 PP |
755 | BT_ASSERT_POST_DEV_MSG( |
756 | "Expecting a clock class without UUID: %![cc-]+K", | |
54b135a0 SM |
757 | clock_class); |
758 | result = false; | |
759 | goto end; | |
760 | } | |
761 | break; | |
762 | } | |
763 | } | |
764 | ||
765 | result = true; | |
766 | ||
767 | end: | |
768 | return result; | |
769 | } | |
770 | ||
bdb288b3 | 771 | BT_ASSERT_POST_DEV_FUNC |
54b135a0 SM |
772 | static |
773 | bool clock_classes_are_compatible( | |
9a2c8b8e | 774 | struct bt_message_iterator *iterator, |
54b135a0 SM |
775 | bt_message_array_const msgs, uint64_t msg_count) |
776 | { | |
777 | uint64_t i; | |
778 | bool result; | |
779 | ||
780 | for (i = 0; i < msg_count; i++) { | |
781 | if (!clock_classes_are_compatible_one(iterator, msgs[i])) { | |
782 | result = false; | |
783 | goto end; | |
784 | } | |
785 | } | |
786 | ||
787 | result = true; | |
788 | ||
789 | end: | |
790 | return result; | |
791 | } | |
792 | ||
793 | /* | |
794 | * Call the `next` method of the iterator. Do some validation on the returned | |
795 | * messages. | |
796 | */ | |
797 | ||
798 | static | |
a3f0c7db | 799 | enum bt_message_iterator_class_next_method_status |
d24d5663 | 800 | call_iterator_next_method( |
9a2c8b8e | 801 | struct bt_message_iterator *iterator, |
54b135a0 SM |
802 | bt_message_array_const msgs, uint64_t capacity, uint64_t *user_count) |
803 | { | |
a3f0c7db | 804 | enum bt_message_iterator_class_next_method_status status; |
54b135a0 | 805 | |
98b15851 | 806 | BT_ASSERT_DBG(iterator->methods.next); |
54b135a0 | 807 | BT_LOGD_STR("Calling user's \"next\" method."); |
54b135a0 | 808 | status = iterator->methods.next(iterator, msgs, capacity, user_count); |
f6f301d7 | 809 | BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64, |
d24d5663 | 810 | bt_common_func_status_string(status), *user_count); |
54b135a0 | 811 | |
d24d5663 | 812 | if (status == BT_FUNC_STATUS_OK) { |
bdb288b3 | 813 | BT_ASSERT_POST_DEV(clock_classes_are_compatible(iterator, msgs, *user_count), |
54b135a0 | 814 | "Clocks are not compatible"); |
bdb288b3 | 815 | BT_ASSERT_POST_DEV(clock_snapshots_are_monotonic(iterator, msgs, *user_count), |
54b135a0 SM |
816 | "Clock snapshots are not monotonic"); |
817 | } | |
818 | ||
6ecdcca3 SM |
819 | BT_ASSERT_POST_DEV_NO_ERROR_IF_NO_ERROR_STATUS(status); |
820 | ||
54b135a0 SM |
821 | return status; |
822 | } | |
823 | ||
d24d5663 | 824 | enum bt_message_iterator_next_status |
9a2c8b8e PP |
825 | bt_message_iterator_next( |
826 | struct bt_message_iterator *iterator, | |
d6e69534 | 827 | bt_message_array_const *msgs, uint64_t *user_count) |
3230ee6b | 828 | { |
d24d5663 | 829 | enum bt_message_iterator_next_status status = BT_FUNC_STATUS_OK; |
d94d92ac | 830 | |
17f3083a | 831 | BT_ASSERT_PRE_DEV_NO_ERROR(); |
bdb288b3 PP |
832 | BT_ASSERT_PRE_DEV_NON_NULL(iterator, "Message iterator"); |
833 | BT_ASSERT_PRE_DEV_NON_NULL(msgs, "Message array (output)"); | |
834 | BT_ASSERT_PRE_DEV_NON_NULL(user_count, "Message count (output)"); | |
835 | BT_ASSERT_PRE_DEV(iterator->state == | |
9a2c8b8e | 836 | BT_MESSAGE_ITERATOR_STATE_ACTIVE, |
d6e69534 | 837 | "Message iterator's \"next\" called, but " |
7474e7d3 | 838 | "message iterator is in the wrong state: %!+i", iterator); |
98b15851 PP |
839 | BT_ASSERT_DBG(iterator->upstream_component); |
840 | BT_ASSERT_DBG(iterator->upstream_component->class); | |
bdb288b3 | 841 | BT_ASSERT_PRE_DEV( |
5badd463 PP |
842 | bt_component_borrow_graph(iterator->upstream_component)->config_state != |
843 | BT_GRAPH_CONFIGURATION_STATE_CONFIGURING, | |
4725a201 PP |
844 | "Graph is not configured: %!+g", |
845 | bt_component_borrow_graph(iterator->upstream_component)); | |
d94d92ac | 846 | BT_LIB_LOGD("Getting next self component input port " |
3f7d4d90 PP |
847 | "message iterator's messages: %!+i, batch-size=%u", |
848 | iterator, MSG_BATCH_SIZE); | |
d3eb6e8f | 849 | |
3230ee6b | 850 | /* |
d6e69534 | 851 | * Call the user's "next" method to get the next messages |
fa054faf | 852 | * and status. |
3230ee6b | 853 | */ |
3f7d4d90 | 854 | *user_count = 0; |
d24d5663 | 855 | status = (int) call_iterator_next_method(iterator, |
6c373cc9 | 856 | (void *) iterator->msgs->pdata, MSG_BATCH_SIZE, |
7474e7d3 | 857 | user_count); |
870631a2 PP |
858 | BT_LOGD("User method returned: status=%s, msg-count=%" PRIu64, |
859 | bt_common_func_status_string(status), *user_count); | |
d4393e08 | 860 | if (status < 0) { |
870631a2 PP |
861 | BT_LIB_LOGW_APPEND_CAUSE( |
862 | "Component input port message iterator's \"next\" method failed: " | |
863 | "%![iter-]+i, status=%s", | |
864 | iterator, bt_common_func_status_string(status)); | |
f42867e2 PP |
865 | goto end; |
866 | } | |
3230ee6b | 867 | |
d0fea130 PP |
868 | /* |
869 | * There is no way that this iterator could have been finalized | |
870 | * during its "next" method, as the only way to do this is to | |
871 | * put the last iterator's reference, and this can only be done | |
872 | * by its downstream owner. | |
7474e7d3 PP |
873 | * |
874 | * For the same reason, there is no way that this iterator could | |
875 | * have seeked (cannot seek a self message iterator). | |
d0fea130 | 876 | */ |
98b15851 | 877 | BT_ASSERT_DBG(iterator->state == |
9a2c8b8e | 878 | BT_MESSAGE_ITERATOR_STATE_ACTIVE); |
8cf27cc5 | 879 | |
d4393e08 | 880 | switch (status) { |
d24d5663 | 881 | case BT_FUNC_STATUS_OK: |
bdb288b3 | 882 | BT_ASSERT_POST_DEV(*user_count <= MSG_BATCH_SIZE, |
7474e7d3 PP |
883 | "Invalid returned message count: greater than " |
884 | "batch size: count=%" PRIu64 ", batch-size=%u", | |
885 | *user_count, MSG_BATCH_SIZE); | |
6c373cc9 | 886 | *msgs = (void *) iterator->msgs->pdata; |
d4393e08 | 887 | break; |
d24d5663 | 888 | case BT_FUNC_STATUS_AGAIN: |
d4393e08 | 889 | goto end; |
d24d5663 | 890 | case BT_FUNC_STATUS_END: |
9a2c8b8e PP |
891 | set_msg_iterator_state(iterator, |
892 | BT_MESSAGE_ITERATOR_STATE_ENDED); | |
f42867e2 | 893 | goto end; |
f42867e2 PP |
894 | default: |
895 | /* Unknown non-error status */ | |
498e7994 | 896 | bt_common_abort(); |
41a2b7ae PP |
897 | } |
898 | ||
899 | end: | |
3230ee6b PP |
900 | return status; |
901 | } | |
902 | ||
7474e7d3 | 903 | struct bt_component * |
9a2c8b8e PP |
904 | bt_message_iterator_borrow_component( |
905 | struct bt_message_iterator *iterator) | |
d94d92ac | 906 | { |
bdb288b3 | 907 | BT_ASSERT_PRE_DEV_NON_NULL(iterator, "Message iterator"); |
d94d92ac PP |
908 | return iterator->upstream_component; |
909 | } | |
910 | ||
d6e69534 PP |
911 | struct bt_self_component *bt_self_message_iterator_borrow_component( |
912 | struct bt_self_message_iterator *self_iterator) | |
413bc2c4 | 913 | { |
9a2c8b8e | 914 | struct bt_message_iterator *iterator = |
d94d92ac | 915 | (void *) self_iterator; |
90157d89 | 916 | |
bdb288b3 | 917 | BT_ASSERT_PRE_DEV_NON_NULL(iterator, "Message iterator"); |
d94d92ac | 918 | return (void *) iterator->upstream_component; |
413bc2c4 JG |
919 | } |
920 | ||
50e763f6 | 921 | struct bt_self_component_port_output *bt_self_message_iterator_borrow_port( |
d6e69534 | 922 | struct bt_self_message_iterator *self_iterator) |
91457551 | 923 | { |
9a2c8b8e | 924 | struct bt_message_iterator *iterator = |
d94d92ac PP |
925 | (void *) self_iterator; |
926 | ||
bdb288b3 | 927 | BT_ASSERT_PRE_DEV_NON_NULL(iterator, "Message iterator"); |
d94d92ac | 928 | return (void *) iterator->upstream_port; |
91457551 | 929 | } |
8ed535b5 | 930 | |
f2fb1b32 | 931 | enum bt_message_iterator_can_seek_ns_from_origin_status |
9a2c8b8e PP |
932 | bt_message_iterator_can_seek_ns_from_origin( |
933 | struct bt_message_iterator *iterator, | |
f2fb1b32 | 934 | int64_t ns_from_origin, bt_bool *can_seek) |
7474e7d3 | 935 | { |
f2fb1b32 | 936 | enum bt_message_iterator_can_seek_ns_from_origin_status status; |
7474e7d3 | 937 | |
17f3083a | 938 | BT_ASSERT_PRE_NO_ERROR(); |
7474e7d3 | 939 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); |
f2fb1b32 | 940 | BT_ASSERT_PRE_NON_NULL(can_seek, "Result (output)"); |
7474e7d3 | 941 | BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator); |
5badd463 PP |
942 | BT_ASSERT_PRE( |
943 | bt_component_borrow_graph(iterator->upstream_component)->config_state != | |
944 | BT_GRAPH_CONFIGURATION_STATE_CONFIGURING, | |
7474e7d3 PP |
945 | "Graph is not configured: %!+g", |
946 | bt_component_borrow_graph(iterator->upstream_component)); | |
947 | ||
948 | if (iterator->methods.can_seek_ns_from_origin) { | |
f2fb1b32 SM |
949 | /* |
950 | * Initialize to an invalid value, so we can post-assert that | |
951 | * the method returned a valid value. | |
952 | */ | |
953 | *can_seek = -1; | |
954 | ||
c0e46a7c SM |
955 | BT_LIB_LOGD("Calling user's \"can seek nanoseconds from origin\" method: %!+i", |
956 | iterator); | |
957 | ||
f2fb1b32 SM |
958 | status = (int) iterator->methods.can_seek_ns_from_origin(iterator, |
959 | ns_from_origin, can_seek); | |
960 | ||
6ecdcca3 SM |
961 | BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status); |
962 | ||
c0e46a7c SM |
963 | if (status != BT_FUNC_STATUS_OK) { |
964 | BT_LIB_LOGW_APPEND_CAUSE( | |
965 | "Component input port message iterator's \"can seek nanoseconds from origin\" method failed: " | |
966 | "%![iter-]+i, status=%s", | |
967 | iterator, bt_common_func_status_string(status)); | |
968 | goto end; | |
969 | } | |
970 | ||
971 | BT_ASSERT_POST(*can_seek == BT_TRUE || *can_seek == BT_FALSE, | |
f2fb1b32 SM |
972 | "Unexpected boolean value returned from user's \"can seek ns from origin\" method: val=%d, %![iter-]+i", |
973 | *can_seek, iterator); | |
974 | ||
c0e46a7c SM |
975 | BT_LIB_LOGD( |
976 | "User's \"can seek nanoseconds from origin\" returned successfully: " | |
977 | "%![iter-]+i, can-seek=%d", | |
978 | iterator, *can_seek); | |
979 | ||
980 | if (*can_seek) { | |
981 | goto end; | |
982 | } | |
7474e7d3 PP |
983 | } |
984 | ||
985 | /* | |
c0e46a7c SM |
986 | * Automatic seeking fall back: if we can seek to the beginning and the |
987 | * iterator supports forward seeking then we can automatically seek to | |
988 | * any timestamp. | |
7474e7d3 | 989 | */ |
9a2c8b8e | 990 | status = (int) bt_message_iterator_can_seek_beginning( |
f2fb1b32 | 991 | iterator, can_seek); |
c0e46a7c SM |
992 | if (status != BT_FUNC_STATUS_OK) { |
993 | goto end; | |
994 | } | |
995 | ||
996 | *can_seek = *can_seek && iterator->config.can_seek_forward; | |
7474e7d3 PP |
997 | |
998 | end: | |
f2fb1b32 | 999 | return status; |
7474e7d3 PP |
1000 | } |
1001 | ||
f2fb1b32 | 1002 | enum bt_message_iterator_can_seek_beginning_status |
9a2c8b8e PP |
1003 | bt_message_iterator_can_seek_beginning( |
1004 | struct bt_message_iterator *iterator, | |
f2fb1b32 | 1005 | bt_bool *can_seek) |
7474e7d3 | 1006 | { |
f2fb1b32 | 1007 | enum bt_message_iterator_can_seek_beginning_status status; |
7474e7d3 | 1008 | |
17f3083a | 1009 | BT_ASSERT_PRE_NO_ERROR(); |
7474e7d3 | 1010 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); |
f2fb1b32 | 1011 | BT_ASSERT_PRE_NON_NULL(can_seek, "Result (output)"); |
7474e7d3 | 1012 | BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator); |
5badd463 PP |
1013 | BT_ASSERT_PRE( |
1014 | bt_component_borrow_graph(iterator->upstream_component)->config_state != | |
1015 | BT_GRAPH_CONFIGURATION_STATE_CONFIGURING, | |
7474e7d3 PP |
1016 | "Graph is not configured: %!+g", |
1017 | bt_component_borrow_graph(iterator->upstream_component)); | |
1018 | ||
1019 | if (iterator->methods.can_seek_beginning) { | |
f2fb1b32 SM |
1020 | /* |
1021 | * Initialize to an invalid value, so we can post-assert that | |
1022 | * the method returned a valid value. | |
1023 | */ | |
1024 | *can_seek = -1; | |
1025 | ||
1026 | status = (int) iterator->methods.can_seek_beginning(iterator, can_seek); | |
1027 | ||
1028 | BT_ASSERT_POST( | |
1029 | status != BT_FUNC_STATUS_OK || | |
1030 | *can_seek == BT_TRUE || | |
1031 | *can_seek == BT_FALSE, | |
1032 | "Unexpected boolean value returned from user's \"can seek beginning\" method: val=%d, %![iter-]+i", | |
1033 | *can_seek, iterator); | |
6ecdcca3 | 1034 | BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status); |
f2fb1b32 SM |
1035 | } else { |
1036 | *can_seek = BT_FALSE; | |
1037 | status = BT_FUNC_STATUS_OK; | |
7474e7d3 PP |
1038 | } |
1039 | ||
f2fb1b32 | 1040 | return status; |
7474e7d3 PP |
1041 | } |
1042 | ||
1043 | static inline | |
003e713f | 1044 | void set_iterator_state_after_seeking( |
9a2c8b8e | 1045 | struct bt_message_iterator *iterator, |
d24d5663 | 1046 | int status) |
7474e7d3 | 1047 | { |
9a2c8b8e | 1048 | enum bt_message_iterator_state new_state = 0; |
7474e7d3 PP |
1049 | |
1050 | /* Set iterator's state depending on seeking status */ | |
1051 | switch (status) { | |
d24d5663 | 1052 | case BT_FUNC_STATUS_OK: |
9a2c8b8e | 1053 | new_state = BT_MESSAGE_ITERATOR_STATE_ACTIVE; |
7474e7d3 | 1054 | break; |
d24d5663 | 1055 | case BT_FUNC_STATUS_AGAIN: |
9a2c8b8e | 1056 | new_state = BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN; |
7474e7d3 | 1057 | break; |
d24d5663 PP |
1058 | case BT_FUNC_STATUS_ERROR: |
1059 | case BT_FUNC_STATUS_MEMORY_ERROR: | |
9a2c8b8e | 1060 | new_state = BT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR; |
7474e7d3 | 1061 | break; |
d24d5663 | 1062 | case BT_FUNC_STATUS_END: |
9a2c8b8e | 1063 | new_state = BT_MESSAGE_ITERATOR_STATE_ENDED; |
7474e7d3 PP |
1064 | break; |
1065 | default: | |
498e7994 | 1066 | bt_common_abort(); |
7474e7d3 PP |
1067 | } |
1068 | ||
9a2c8b8e | 1069 | set_msg_iterator_state(iterator, new_state); |
7474e7d3 PP |
1070 | } |
1071 | ||
54b135a0 SM |
1072 | static |
1073 | void reset_iterator_expectations( | |
9a2c8b8e | 1074 | struct bt_message_iterator *iterator) |
54b135a0 SM |
1075 | { |
1076 | iterator->last_ns_from_origin = INT64_MIN; | |
1077 | iterator->clock_expectation.type = CLOCK_EXPECTATION_UNSET; | |
1078 | } | |
1079 | ||
f2fb1b32 SM |
1080 | static |
1081 | bool message_iterator_can_seek_beginning( | |
9a2c8b8e | 1082 | struct bt_message_iterator *iterator) |
f2fb1b32 SM |
1083 | { |
1084 | enum bt_message_iterator_can_seek_beginning_status status; | |
1085 | bt_bool can_seek; | |
1086 | ||
9a2c8b8e | 1087 | status = bt_message_iterator_can_seek_beginning( |
f2fb1b32 SM |
1088 | iterator, &can_seek); |
1089 | if (status != BT_FUNC_STATUS_OK) { | |
1090 | can_seek = BT_FALSE; | |
1091 | } | |
1092 | ||
1093 | return can_seek; | |
1094 | } | |
1095 | ||
d24d5663 | 1096 | enum bt_message_iterator_seek_beginning_status |
9a2c8b8e PP |
1097 | bt_message_iterator_seek_beginning( |
1098 | struct bt_message_iterator *iterator) | |
7474e7d3 | 1099 | { |
9275bef4 | 1100 | int status; |
7474e7d3 | 1101 | |
17f3083a | 1102 | BT_ASSERT_PRE_NO_ERROR(); |
7474e7d3 PP |
1103 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); |
1104 | BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator); | |
5badd463 PP |
1105 | BT_ASSERT_PRE( |
1106 | bt_component_borrow_graph(iterator->upstream_component)->config_state != | |
1107 | BT_GRAPH_CONFIGURATION_STATE_CONFIGURING, | |
7474e7d3 PP |
1108 | "Graph is not configured: %!+g", |
1109 | bt_component_borrow_graph(iterator->upstream_component)); | |
f2fb1b32 | 1110 | BT_ASSERT_PRE(message_iterator_can_seek_beginning(iterator), |
7474e7d3 | 1111 | "Message iterator cannot seek beginning: %!+i", iterator); |
54b135a0 SM |
1112 | |
1113 | /* | |
1114 | * We are seeking, reset our expectations about how the following | |
1115 | * messages should look like. | |
1116 | */ | |
1117 | reset_iterator_expectations(iterator); | |
1118 | ||
7474e7d3 | 1119 | BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i", iterator); |
9a2c8b8e PP |
1120 | set_msg_iterator_state(iterator, |
1121 | BT_MESSAGE_ITERATOR_STATE_SEEKING); | |
7474e7d3 PP |
1122 | status = iterator->methods.seek_beginning(iterator); |
1123 | BT_LOGD("User method returned: status=%s", | |
d24d5663 PP |
1124 | bt_common_func_status_string(status)); |
1125 | BT_ASSERT_POST(status == BT_FUNC_STATUS_OK || | |
1126 | status == BT_FUNC_STATUS_ERROR || | |
1127 | status == BT_FUNC_STATUS_MEMORY_ERROR || | |
1128 | status == BT_FUNC_STATUS_AGAIN, | |
7474e7d3 | 1129 | "Unexpected status: %![iter-]+i, status=%s", |
d24d5663 | 1130 | iterator, bt_common_func_status_string(status)); |
6ecdcca3 | 1131 | BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status); |
870631a2 PP |
1132 | if (status < 0) { |
1133 | BT_LIB_LOGW_APPEND_CAUSE( | |
1134 | "Component input port message iterator's \"seek beginning\" method failed: " | |
1135 | "%![iter-]+i, status=%s", | |
1136 | iterator, bt_common_func_status_string(status)); | |
1137 | } | |
1138 | ||
7474e7d3 PP |
1139 | set_iterator_state_after_seeking(iterator, status); |
1140 | return status; | |
1141 | } | |
1142 | ||
8d8b141d | 1143 | bt_bool |
9a2c8b8e PP |
1144 | bt_message_iterator_can_seek_forward( |
1145 | bt_message_iterator *iterator) | |
8d8b141d SM |
1146 | { |
1147 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); | |
1148 | ||
1149 | return iterator->config.can_seek_forward; | |
1150 | } | |
1151 | ||
5b7b55be SM |
1152 | /* |
1153 | * Structure used to record the state of a given stream during the fast-forward | |
1154 | * phase of an auto-seek. | |
1155 | */ | |
1156 | struct auto_seek_stream_state { | |
1157 | /* | |
1158 | * Value representing which step of this timeline we are at. | |
1159 | * | |
1160 | * time ---> | |
188edac1 | 1161 | * [SB] 1 [PB] 2 [PE] 1 [SE] |
5b7b55be SM |
1162 | * |
1163 | * At each point in the timeline, the messages we need to replicate are: | |
1164 | * | |
1165 | * 1: Stream beginning | |
188edac1 | 1166 | * 2: Stream beginning, packet beginning |
5b7b55be SM |
1167 | * |
1168 | * Before "Stream beginning" and after "Stream end", we don't need to | |
1169 | * replicate anything as the stream doesn't exist. | |
1170 | */ | |
1171 | enum { | |
1172 | AUTO_SEEK_STREAM_STATE_STREAM_BEGAN, | |
5b7b55be SM |
1173 | AUTO_SEEK_STREAM_STATE_PACKET_BEGAN, |
1174 | } state; | |
1175 | ||
1176 | /* | |
1177 | * If `state` is AUTO_SEEK_STREAM_STATE_PACKET_BEGAN, the packet we are | |
1178 | * in. This is a weak reference, since the packet will always be | |
1179 | * alive by the time we use it. | |
1180 | */ | |
1181 | struct bt_packet *packet; | |
188edac1 SM |
1182 | |
1183 | /* Have we see a message with a clock snapshot yet? */ | |
1184 | bool seen_clock_snapshot; | |
5b7b55be SM |
1185 | }; |
1186 | ||
1187 | static | |
1188 | struct auto_seek_stream_state *create_auto_seek_stream_state(void) | |
1189 | { | |
1190 | return g_new0(struct auto_seek_stream_state, 1); | |
1191 | } | |
1192 | ||
1193 | static | |
1194 | void destroy_auto_seek_stream_state(void *ptr) | |
1195 | { | |
1196 | g_free(ptr); | |
1197 | } | |
1198 | ||
1199 | static | |
1200 | GHashTable *create_auto_seek_stream_states(void) | |
1201 | { | |
1202 | return g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, | |
1203 | destroy_auto_seek_stream_state); | |
1204 | } | |
1205 | ||
1206 | static | |
1207 | void destroy_auto_seek_stream_states(GHashTable *stream_states) | |
1208 | { | |
1209 | g_hash_table_destroy(stream_states); | |
1210 | } | |
1211 | ||
1212 | /* | |
1213 | * Handle one message while we are in the fast-forward phase of an auto-seek. | |
1214 | * | |
1215 | * Sets `*got_first` to true if the message's timestamp is greater or equal to | |
1216 | * `ns_from_origin`. In other words, if this is the first message after our | |
1217 | * seek point. | |
1218 | * | |
1219 | * `stream_states` is an hash table of `bt_stream *` (weak reference) to | |
1220 | * `struct auto_seek_stream_state` used to keep the state of each stream | |
1221 | * during the fast-forward. | |
1222 | */ | |
1223 | ||
7474e7d3 | 1224 | static inline |
d24d5663 | 1225 | int auto_seek_handle_message( |
9a2c8b8e | 1226 | struct bt_message_iterator *iterator, |
5b9e151d | 1227 | int64_t ns_from_origin, const struct bt_message *msg, |
5b7b55be | 1228 | bool *got_first, GHashTable *stream_states) |
7474e7d3 | 1229 | { |
d24d5663 | 1230 | int status = BT_FUNC_STATUS_OK; |
5b9e151d | 1231 | int64_t msg_ns_from_origin; |
7474e7d3 | 1232 | const struct bt_clock_snapshot *clk_snapshot = NULL; |
5b9e151d PP |
1233 | int ret; |
1234 | ||
98b15851 PP |
1235 | BT_ASSERT_DBG(msg); |
1236 | BT_ASSERT_DBG(got_first); | |
7474e7d3 PP |
1237 | |
1238 | switch (msg->type) { | |
1239 | case BT_MESSAGE_TYPE_EVENT: | |
1240 | { | |
1241 | const struct bt_message_event *event_msg = | |
1242 | (const void *) msg; | |
1243 | ||
2c091c04 | 1244 | clk_snapshot = event_msg->default_cs; |
bdb288b3 | 1245 | BT_ASSERT_POST_DEV(clk_snapshot, |
c7072d5a PP |
1246 | "Event message has no default clock snapshot: %!+n", |
1247 | event_msg); | |
7474e7d3 PP |
1248 | break; |
1249 | } | |
b9fd9cbb | 1250 | case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY: |
7474e7d3 | 1251 | { |
b9fd9cbb | 1252 | const struct bt_message_message_iterator_inactivity *inactivity_msg = |
7474e7d3 PP |
1253 | (const void *) msg; |
1254 | ||
60d02328 | 1255 | clk_snapshot = inactivity_msg->cs; |
98b15851 | 1256 | BT_ASSERT_DBG(clk_snapshot); |
7474e7d3 PP |
1257 | break; |
1258 | } | |
16663a5e PP |
1259 | case BT_MESSAGE_TYPE_PACKET_BEGINNING: |
1260 | case BT_MESSAGE_TYPE_PACKET_END: | |
c7072d5a PP |
1261 | { |
1262 | const struct bt_message_packet *packet_msg = | |
1263 | (const void *) msg; | |
1264 | ||
1265 | clk_snapshot = packet_msg->default_cs; | |
bdb288b3 | 1266 | BT_ASSERT_POST_DEV(clk_snapshot, |
c7072d5a PP |
1267 | "Packet message has no default clock snapshot: %!+n", |
1268 | packet_msg); | |
1269 | break; | |
1270 | } | |
16663a5e PP |
1271 | case BT_MESSAGE_TYPE_DISCARDED_EVENTS: |
1272 | case BT_MESSAGE_TYPE_DISCARDED_PACKETS: | |
7474e7d3 | 1273 | { |
5b9e151d PP |
1274 | struct bt_message_discarded_items *msg_disc_items = |
1275 | (void *) msg; | |
1276 | ||
bdb288b3 | 1277 | BT_ASSERT_POST_DEV(msg_disc_items->default_begin_cs && |
5b9e151d PP |
1278 | msg_disc_items->default_end_cs, |
1279 | "Discarded events/packets message has no default clock snapshots: %!+n", | |
1280 | msg_disc_items); | |
1281 | ret = bt_clock_snapshot_get_ns_from_origin( | |
1282 | msg_disc_items->default_begin_cs, | |
1283 | &msg_ns_from_origin); | |
1284 | if (ret) { | |
d24d5663 | 1285 | status = BT_FUNC_STATUS_ERROR; |
5b9e151d PP |
1286 | goto end; |
1287 | } | |
7474e7d3 | 1288 | |
5b9e151d PP |
1289 | if (msg_ns_from_origin >= ns_from_origin) { |
1290 | *got_first = true; | |
1291 | goto push_msg; | |
1292 | } | |
1293 | ||
1294 | ret = bt_clock_snapshot_get_ns_from_origin( | |
1295 | msg_disc_items->default_end_cs, | |
1296 | &msg_ns_from_origin); | |
1297 | if (ret) { | |
d24d5663 | 1298 | status = BT_FUNC_STATUS_ERROR; |
5b9e151d PP |
1299 | goto end; |
1300 | } | |
1301 | ||
1302 | if (msg_ns_from_origin >= ns_from_origin) { | |
1303 | /* | |
1304 | * The discarded items message's beginning time | |
1305 | * is before the requested seeking time, but its | |
1306 | * end time is after. Modify the message so as | |
1307 | * to set its beginning time to the requested | |
1308 | * seeking time, and make its item count unknown | |
1309 | * as we don't know if items were really | |
1310 | * discarded within the new time range. | |
1311 | */ | |
4af85094 | 1312 | uint64_t new_begin_raw_value = 0; |
5b9e151d PP |
1313 | |
1314 | ret = bt_clock_class_clock_value_from_ns_from_origin( | |
1315 | msg_disc_items->default_end_cs->clock_class, | |
1316 | ns_from_origin, &new_begin_raw_value); | |
1317 | if (ret) { | |
d24d5663 | 1318 | status = BT_FUNC_STATUS_ERROR; |
5b9e151d PP |
1319 | goto end; |
1320 | } | |
1321 | ||
1322 | bt_clock_snapshot_set_raw_value( | |
1323 | msg_disc_items->default_begin_cs, | |
1324 | new_begin_raw_value); | |
1325 | msg_disc_items->count.base.avail = | |
1326 | BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE; | |
1327 | ||
1328 | /* | |
1329 | * It is safe to push it because its beginning | |
1330 | * time is exactly the requested seeking time. | |
1331 | */ | |
1332 | goto push_msg; | |
1333 | } else { | |
1334 | goto skip_msg; | |
1335 | } | |
7474e7d3 | 1336 | } |
188edac1 SM |
1337 | case BT_MESSAGE_TYPE_STREAM_BEGINNING: |
1338 | case BT_MESSAGE_TYPE_STREAM_END: | |
7474e7d3 | 1339 | { |
188edac1 SM |
1340 | struct bt_message_stream *stream_msg = |
1341 | (struct bt_message_stream *) msg; | |
7474e7d3 | 1342 | |
188edac1 SM |
1343 | if (stream_msg->default_cs_state != BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN) { |
1344 | /* Ignore */ | |
5b9e151d | 1345 | goto skip_msg; |
16663a5e PP |
1346 | } |
1347 | ||
188edac1 | 1348 | clk_snapshot = stream_msg->default_cs; |
7474e7d3 PP |
1349 | break; |
1350 | } | |
1351 | default: | |
498e7994 | 1352 | bt_common_abort(); |
7474e7d3 PP |
1353 | } |
1354 | ||
98b15851 | 1355 | BT_ASSERT_DBG(clk_snapshot); |
5b9e151d PP |
1356 | ret = bt_clock_snapshot_get_ns_from_origin(clk_snapshot, |
1357 | &msg_ns_from_origin); | |
1358 | if (ret) { | |
d24d5663 | 1359 | status = BT_FUNC_STATUS_ERROR; |
7474e7d3 PP |
1360 | goto end; |
1361 | } | |
1362 | ||
5b9e151d PP |
1363 | if (msg_ns_from_origin >= ns_from_origin) { |
1364 | *got_first = true; | |
1365 | goto push_msg; | |
1366 | } | |
1367 | ||
1368 | skip_msg: | |
5b7b55be SM |
1369 | /* This message won't be sent downstream. */ |
1370 | switch (msg->type) { | |
1371 | case BT_MESSAGE_TYPE_STREAM_BEGINNING: | |
1372 | { | |
1373 | const struct bt_message_stream *stream_msg = (const void *) msg; | |
1374 | struct auto_seek_stream_state *stream_state; | |
5b7b55be SM |
1375 | |
1376 | /* Update stream's state: stream began. */ | |
1377 | stream_state = create_auto_seek_stream_state(); | |
1378 | if (!stream_state) { | |
d24d5663 | 1379 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
5b7b55be SM |
1380 | goto end; |
1381 | } | |
1382 | ||
1383 | stream_state->state = AUTO_SEEK_STREAM_STATE_STREAM_BEGAN; | |
e74dbb33 | 1384 | |
188edac1 SM |
1385 | if (stream_msg->default_cs_state == BT_MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_KNOWN) { |
1386 | stream_state->seen_clock_snapshot = true; | |
1387 | } | |
1388 | ||
98b15851 | 1389 | BT_ASSERT_DBG(!bt_g_hash_table_contains(stream_states, stream_msg->stream)); |
e74dbb33 | 1390 | g_hash_table_insert(stream_states, stream_msg->stream, stream_state); |
5b7b55be SM |
1391 | break; |
1392 | } | |
188edac1 | 1393 | case BT_MESSAGE_TYPE_PACKET_BEGINNING: |
5b7b55be | 1394 | { |
188edac1 | 1395 | const struct bt_message_packet *packet_msg = |
5b7b55be SM |
1396 | (const void *) msg; |
1397 | struct auto_seek_stream_state *stream_state; | |
1398 | ||
188edac1 SM |
1399 | /* Update stream's state: packet began. */ |
1400 | stream_state = g_hash_table_lookup(stream_states, packet_msg->packet->stream); | |
98b15851 PP |
1401 | BT_ASSERT_DBG(stream_state); |
1402 | BT_ASSERT_DBG(stream_state->state == AUTO_SEEK_STREAM_STATE_STREAM_BEGAN); | |
188edac1 | 1403 | stream_state->state = AUTO_SEEK_STREAM_STATE_PACKET_BEGAN; |
98b15851 | 1404 | BT_ASSERT_DBG(!stream_state->packet); |
188edac1 SM |
1405 | stream_state->packet = packet_msg->packet; |
1406 | ||
1407 | if (packet_msg->packet->stream->class->packets_have_beginning_default_clock_snapshot) { | |
1408 | stream_state->seen_clock_snapshot = true; | |
1409 | } | |
1410 | ||
5b7b55be SM |
1411 | break; |
1412 | } | |
188edac1 | 1413 | case BT_MESSAGE_TYPE_EVENT: |
5b7b55be | 1414 | { |
188edac1 | 1415 | const struct bt_message_event *event_msg = (const void *) msg; |
5b7b55be SM |
1416 | struct auto_seek_stream_state *stream_state; |
1417 | ||
188edac1 SM |
1418 | stream_state = g_hash_table_lookup(stream_states, |
1419 | event_msg->event->packet->stream); | |
98b15851 | 1420 | BT_ASSERT_DBG(stream_state); |
5b7b55be | 1421 | |
188edac1 SM |
1422 | // HELPME: are we sure that event messages have clock snapshots at this point? |
1423 | stream_state->seen_clock_snapshot = true; | |
1424 | ||
5b7b55be SM |
1425 | break; |
1426 | } | |
1427 | case BT_MESSAGE_TYPE_PACKET_END: | |
1428 | { | |
1429 | const struct bt_message_packet *packet_msg = | |
1430 | (const void *) msg; | |
1431 | struct auto_seek_stream_state *stream_state; | |
1432 | ||
1433 | /* Update stream's state: packet ended. */ | |
1434 | stream_state = g_hash_table_lookup(stream_states, packet_msg->packet->stream); | |
98b15851 PP |
1435 | BT_ASSERT_DBG(stream_state); |
1436 | BT_ASSERT_DBG(stream_state->state == AUTO_SEEK_STREAM_STATE_PACKET_BEGAN); | |
188edac1 | 1437 | stream_state->state = AUTO_SEEK_STREAM_STATE_STREAM_BEGAN; |
98b15851 | 1438 | BT_ASSERT_DBG(stream_state->packet); |
5b7b55be | 1439 | stream_state->packet = NULL; |
5b7b55be | 1440 | |
188edac1 SM |
1441 | if (packet_msg->packet->stream->class->packets_have_end_default_clock_snapshot) { |
1442 | stream_state->seen_clock_snapshot = true; | |
1443 | } | |
5b7b55be | 1444 | |
5b7b55be SM |
1445 | break; |
1446 | } | |
1447 | case BT_MESSAGE_TYPE_STREAM_END: | |
1448 | { | |
1449 | const struct bt_message_stream *stream_msg = (const void *) msg; | |
1450 | struct auto_seek_stream_state *stream_state; | |
1451 | ||
1452 | stream_state = g_hash_table_lookup(stream_states, stream_msg->stream); | |
98b15851 PP |
1453 | BT_ASSERT_DBG(stream_state); |
1454 | BT_ASSERT_DBG(stream_state->state == AUTO_SEEK_STREAM_STATE_STREAM_BEGAN); | |
1455 | BT_ASSERT_DBG(!stream_state->packet); | |
5b7b55be SM |
1456 | |
1457 | /* Update stream's state: this stream doesn't exist anymore. */ | |
1458 | g_hash_table_remove(stream_states, stream_msg->stream); | |
1459 | break; | |
1460 | } | |
188edac1 SM |
1461 | case BT_MESSAGE_TYPE_DISCARDED_EVENTS: |
1462 | case BT_MESSAGE_TYPE_DISCARDED_PACKETS: | |
1463 | { | |
1464 | const struct bt_message_discarded_items *discarded_msg = | |
1465 | (const void *) msg; | |
1466 | struct auto_seek_stream_state *stream_state; | |
1467 | ||
1468 | stream_state = g_hash_table_lookup(stream_states, discarded_msg->stream); | |
98b15851 | 1469 | BT_ASSERT_DBG(stream_state); |
188edac1 SM |
1470 | |
1471 | if ((msg->type == BT_MESSAGE_TYPE_DISCARDED_EVENTS && discarded_msg->stream->class->discarded_events_have_default_clock_snapshots) || | |
1472 | (msg->type == BT_MESSAGE_TYPE_DISCARDED_PACKETS && discarded_msg->stream->class->discarded_packets_have_default_clock_snapshots)) { | |
1473 | stream_state->seen_clock_snapshot = true; | |
1474 | } | |
1475 | ||
1476 | break; | |
1477 | } | |
5b7b55be SM |
1478 | default: |
1479 | break; | |
1480 | } | |
1481 | ||
6871026b | 1482 | bt_object_put_ref_no_null_check(msg); |
04c0cec6 | 1483 | msg = NULL; |
5b9e151d PP |
1484 | goto end; |
1485 | ||
1486 | push_msg: | |
da9c4c52 | 1487 | g_queue_push_tail(iterator->auto_seek.msgs, (void *) msg); |
5b9e151d | 1488 | msg = NULL; |
7474e7d3 PP |
1489 | |
1490 | end: | |
98b15851 | 1491 | BT_ASSERT_DBG(!msg || status != BT_FUNC_STATUS_OK); |
5b9e151d | 1492 | return status; |
7474e7d3 PP |
1493 | } |
1494 | ||
1495 | static | |
d24d5663 | 1496 | int find_message_ge_ns_from_origin( |
9a2c8b8e | 1497 | struct bt_message_iterator *iterator, |
5b7b55be | 1498 | int64_t ns_from_origin, GHashTable *stream_states) |
7474e7d3 | 1499 | { |
c70a96a6 | 1500 | int status = BT_FUNC_STATUS_OK; |
9a2c8b8e | 1501 | enum bt_message_iterator_state init_state = |
7474e7d3 PP |
1502 | iterator->state; |
1503 | const struct bt_message *messages[MSG_BATCH_SIZE]; | |
1504 | uint64_t user_count = 0; | |
1505 | uint64_t i; | |
5b9e151d | 1506 | bool got_first = false; |
7474e7d3 | 1507 | |
98b15851 | 1508 | BT_ASSERT_DBG(iterator); |
7474e7d3 PP |
1509 | memset(&messages[0], 0, sizeof(messages[0]) * MSG_BATCH_SIZE); |
1510 | ||
1511 | /* | |
1512 | * Make this iterator temporarily active (not seeking) to call | |
1513 | * the "next" method. | |
1514 | */ | |
9a2c8b8e PP |
1515 | set_msg_iterator_state(iterator, |
1516 | BT_MESSAGE_ITERATOR_STATE_ACTIVE); | |
7474e7d3 | 1517 | |
98b15851 | 1518 | BT_ASSERT_DBG(iterator->methods.next); |
7474e7d3 | 1519 | |
e0dade92 | 1520 | while (!got_first) { |
7474e7d3 PP |
1521 | /* |
1522 | * Call the user's "next" method to get the next | |
1523 | * messages and status. | |
1524 | */ | |
54b135a0 | 1525 | status = call_iterator_next_method(iterator, |
7474e7d3 | 1526 | &messages[0], MSG_BATCH_SIZE, &user_count); |
870631a2 PP |
1527 | BT_LOGD("User method returned: status=%s", |
1528 | bt_common_func_status_string(status)); | |
1529 | if (status < 0) { | |
1530 | BT_LIB_LOGW_APPEND_CAUSE( | |
1531 | "Component input port message iterator's \"next\" method failed: " | |
1532 | "%![iter-]+i, status=%s", | |
1533 | iterator, bt_common_func_status_string(status)); | |
1534 | } | |
7474e7d3 | 1535 | |
7474e7d3 PP |
1536 | /* |
1537 | * The user's "next" method must not do any action which | |
1538 | * would change the iterator's state. | |
1539 | */ | |
98b15851 | 1540 | BT_ASSERT_DBG(iterator->state == |
9a2c8b8e | 1541 | BT_MESSAGE_ITERATOR_STATE_ACTIVE); |
7474e7d3 PP |
1542 | |
1543 | switch (status) { | |
d24d5663 | 1544 | case BT_FUNC_STATUS_OK: |
bdb288b3 | 1545 | BT_ASSERT_POST_DEV(user_count <= MSG_BATCH_SIZE, |
7474e7d3 PP |
1546 | "Invalid returned message count: greater than " |
1547 | "batch size: count=%" PRIu64 ", batch-size=%u", | |
1548 | user_count, MSG_BATCH_SIZE); | |
1549 | break; | |
d24d5663 PP |
1550 | case BT_FUNC_STATUS_AGAIN: |
1551 | case BT_FUNC_STATUS_ERROR: | |
1552 | case BT_FUNC_STATUS_MEMORY_ERROR: | |
1553 | case BT_FUNC_STATUS_END: | |
7474e7d3 PP |
1554 | goto end; |
1555 | default: | |
498e7994 | 1556 | bt_common_abort(); |
7474e7d3 PP |
1557 | } |
1558 | ||
7474e7d3 | 1559 | for (i = 0; i < user_count; i++) { |
5b9e151d | 1560 | if (got_first) { |
da9c4c52 | 1561 | g_queue_push_tail(iterator->auto_seek.msgs, |
5b9e151d PP |
1562 | (void *) messages[i]); |
1563 | messages[i] = NULL; | |
7474e7d3 PP |
1564 | continue; |
1565 | } | |
1566 | ||
5b9e151d | 1567 | status = auto_seek_handle_message(iterator, |
5b7b55be SM |
1568 | ns_from_origin, messages[i], &got_first, |
1569 | stream_states); | |
d24d5663 | 1570 | if (status == BT_FUNC_STATUS_OK) { |
e0dade92 | 1571 | /* Message was either pushed or moved */ |
5b9e151d PP |
1572 | messages[i] = NULL; |
1573 | } else { | |
7474e7d3 PP |
1574 | goto end; |
1575 | } | |
7474e7d3 PP |
1576 | } |
1577 | } | |
1578 | ||
1579 | end: | |
1580 | for (i = 0; i < user_count; i++) { | |
1581 | if (messages[i]) { | |
6871026b | 1582 | bt_object_put_ref_no_null_check(messages[i]); |
7474e7d3 PP |
1583 | } |
1584 | } | |
1585 | ||
9a2c8b8e | 1586 | set_msg_iterator_state(iterator, init_state); |
7474e7d3 PP |
1587 | return status; |
1588 | } | |
1589 | ||
5b7b55be SM |
1590 | /* |
1591 | * This function is installed as the iterator's next callback after we have | |
1592 | * auto-seeked (seeked to the beginning and fast-forwarded) to send the | |
1593 | * messages saved in iterator->auto_seek.msgs. Once this is done, the original | |
1594 | * next callback is put back. | |
1595 | */ | |
1596 | ||
7474e7d3 | 1597 | static |
a3f0c7db | 1598 | enum bt_message_iterator_class_next_method_status post_auto_seek_next( |
9a2c8b8e | 1599 | struct bt_message_iterator *iterator, |
7474e7d3 PP |
1600 | bt_message_array_const msgs, uint64_t capacity, |
1601 | uint64_t *count) | |
1602 | { | |
da9c4c52 | 1603 | BT_ASSERT(!g_queue_is_empty(iterator->auto_seek.msgs)); |
5b9e151d | 1604 | *count = 0; |
7474e7d3 PP |
1605 | |
1606 | /* | |
1607 | * Move auto-seek messages to the output array (which is this | |
5b9e151d | 1608 | * iterator's base message array). |
7474e7d3 | 1609 | */ |
da9c4c52 SM |
1610 | while (capacity > 0 && !g_queue_is_empty(iterator->auto_seek.msgs)) { |
1611 | msgs[*count] = g_queue_pop_head(iterator->auto_seek.msgs); | |
5b9e151d PP |
1612 | capacity--; |
1613 | (*count)++; | |
7474e7d3 | 1614 | } |
7474e7d3 | 1615 | |
5b9e151d PP |
1616 | BT_ASSERT(*count > 0); |
1617 | ||
da9c4c52 | 1618 | if (g_queue_is_empty(iterator->auto_seek.msgs)) { |
572075a8 SM |
1619 | /* No more auto-seek messages, restore user's next callback. */ |
1620 | BT_ASSERT(iterator->auto_seek.original_next_callback); | |
1621 | iterator->methods.next = iterator->auto_seek.original_next_callback; | |
1622 | iterator->auto_seek.original_next_callback = NULL; | |
7474e7d3 PP |
1623 | } |
1624 | ||
d24d5663 | 1625 | return BT_FUNC_STATUS_OK; |
7474e7d3 PP |
1626 | } |
1627 | ||
5b7b55be SM |
1628 | static inline |
1629 | int clock_raw_value_from_ns_from_origin(const bt_clock_class *clock_class, | |
1630 | int64_t ns_from_origin, uint64_t *raw_value) | |
1631 | { | |
1632 | ||
1633 | int64_t cc_offset_s = clock_class->offset_seconds; | |
1634 | uint64_t cc_offset_cycles = clock_class->offset_cycles; | |
1635 | uint64_t cc_freq = clock_class->frequency; | |
1636 | ||
1637 | return bt_common_clock_value_from_ns_from_origin(cc_offset_s, | |
1638 | cc_offset_cycles, cc_freq, ns_from_origin, raw_value); | |
1639 | } | |
1640 | ||
f2fb1b32 SM |
1641 | static |
1642 | bool message_iterator_can_seek_ns_from_origin( | |
9a2c8b8e | 1643 | struct bt_message_iterator *iterator, |
f2fb1b32 SM |
1644 | int64_t ns_from_origin) |
1645 | { | |
1646 | enum bt_message_iterator_can_seek_ns_from_origin_status status; | |
1647 | bt_bool can_seek; | |
1648 | ||
9a2c8b8e | 1649 | status = bt_message_iterator_can_seek_ns_from_origin( |
f2fb1b32 SM |
1650 | iterator, ns_from_origin, &can_seek); |
1651 | if (status != BT_FUNC_STATUS_OK) { | |
1652 | can_seek = BT_FALSE; | |
1653 | } | |
1654 | ||
1655 | return can_seek; | |
1656 | } | |
5b7b55be | 1657 | |
d24d5663 | 1658 | enum bt_message_iterator_seek_ns_from_origin_status |
9a2c8b8e PP |
1659 | bt_message_iterator_seek_ns_from_origin( |
1660 | struct bt_message_iterator *iterator, | |
7474e7d3 PP |
1661 | int64_t ns_from_origin) |
1662 | { | |
1663 | int status; | |
5b7b55be | 1664 | GHashTable *stream_states = NULL; |
c0e46a7c | 1665 | bt_bool can_seek_by_itself; |
7474e7d3 | 1666 | |
17f3083a | 1667 | BT_ASSERT_PRE_NO_ERROR(); |
7474e7d3 PP |
1668 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); |
1669 | BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator); | |
5badd463 PP |
1670 | BT_ASSERT_PRE( |
1671 | bt_component_borrow_graph(iterator->upstream_component)->config_state != | |
1672 | BT_GRAPH_CONFIGURATION_STATE_CONFIGURING, | |
7474e7d3 PP |
1673 | "Graph is not configured: %!+g", |
1674 | bt_component_borrow_graph(iterator->upstream_component)); | |
c0e46a7c | 1675 | /* The iterator must be able to seek ns from origin one way or another. */ |
7474e7d3 | 1676 | BT_ASSERT_PRE( |
f2fb1b32 | 1677 | message_iterator_can_seek_ns_from_origin(iterator, ns_from_origin), |
7474e7d3 PP |
1678 | "Message iterator cannot seek nanoseconds from origin: %!+i, " |
1679 | "ns-from-origin=%" PRId64, iterator, ns_from_origin); | |
9a2c8b8e PP |
1680 | set_msg_iterator_state(iterator, |
1681 | BT_MESSAGE_ITERATOR_STATE_SEEKING); | |
7474e7d3 | 1682 | |
54b135a0 SM |
1683 | /* |
1684 | * We are seeking, reset our expectations about how the following | |
1685 | * messages should look like. | |
1686 | */ | |
1687 | reset_iterator_expectations(iterator); | |
1688 | ||
c0e46a7c SM |
1689 | /* Check if the iterator can seek by itself. If not we'll use autoseek. */ |
1690 | if (iterator->methods.can_seek_ns_from_origin) { | |
a3f0c7db | 1691 | bt_message_iterator_class_can_seek_ns_from_origin_method_status |
c0e46a7c SM |
1692 | can_seek_status; |
1693 | ||
1694 | can_seek_status = | |
1695 | iterator->methods.can_seek_ns_from_origin( | |
1696 | iterator, ns_from_origin, &can_seek_by_itself); | |
1697 | if (can_seek_status != BT_FUNC_STATUS_OK) { | |
1698 | status = can_seek_status; | |
1699 | goto end; | |
1700 | } | |
1701 | } else { | |
1702 | can_seek_by_itself = false; | |
1703 | } | |
1704 | ||
1705 | if (can_seek_by_itself) { | |
5b7b55be | 1706 | /* The iterator knows how to seek to a particular time, let it handle this. */ |
2e1b5615 | 1707 | BT_ASSERT(iterator->methods.seek_ns_from_origin); |
7474e7d3 PP |
1708 | BT_LIB_LOGD("Calling user's \"seek nanoseconds from origin\" method: " |
1709 | "%![iter-]+i, ns=%" PRId64, iterator, ns_from_origin); | |
1710 | status = iterator->methods.seek_ns_from_origin(iterator, | |
1711 | ns_from_origin); | |
1712 | BT_LOGD("User method returned: status=%s", | |
d24d5663 PP |
1713 | bt_common_func_status_string(status)); |
1714 | BT_ASSERT_POST(status == BT_FUNC_STATUS_OK || | |
1715 | status == BT_FUNC_STATUS_ERROR || | |
1716 | status == BT_FUNC_STATUS_MEMORY_ERROR || | |
1717 | status == BT_FUNC_STATUS_AGAIN, | |
7474e7d3 | 1718 | "Unexpected status: %![iter-]+i, status=%s", |
d24d5663 | 1719 | iterator, bt_common_func_status_string(status)); |
6ecdcca3 | 1720 | BT_ASSERT_POST_NO_ERROR_IF_NO_ERROR_STATUS(status); |
870631a2 PP |
1721 | if (status < 0) { |
1722 | BT_LIB_LOGW_APPEND_CAUSE( | |
1723 | "Component input port message iterator's \"seek nanoseconds from origin\" method failed: " | |
1724 | "%![iter-]+i, status=%s", | |
1725 | iterator, bt_common_func_status_string(status)); | |
1726 | } | |
7474e7d3 | 1727 | } else { |
5b7b55be | 1728 | /* |
c0e46a7c SM |
1729 | * The iterator doesn't know how to seek by itself to a |
1730 | * particular time. We will seek to the beginning and fast | |
1731 | * forward to the right place. | |
5b7b55be | 1732 | */ |
a3f0c7db | 1733 | enum bt_message_iterator_class_can_seek_beginning_method_status can_seek_status; |
f2fb1b32 SM |
1734 | bt_bool can_seek_beginning; |
1735 | ||
1736 | can_seek_status = iterator->methods.can_seek_beginning(iterator, | |
1737 | &can_seek_beginning); | |
1738 | BT_ASSERT(can_seek_status == BT_FUNC_STATUS_OK); | |
1739 | BT_ASSERT(can_seek_beginning); | |
7474e7d3 PP |
1740 | BT_ASSERT(iterator->methods.seek_beginning); |
1741 | BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i", | |
1742 | iterator); | |
1743 | status = iterator->methods.seek_beginning(iterator); | |
1744 | BT_LOGD("User method returned: status=%s", | |
d24d5663 PP |
1745 | bt_common_func_status_string(status)); |
1746 | BT_ASSERT_POST(status == BT_FUNC_STATUS_OK || | |
1747 | status == BT_FUNC_STATUS_ERROR || | |
1748 | status == BT_FUNC_STATUS_MEMORY_ERROR || | |
1749 | status == BT_FUNC_STATUS_AGAIN, | |
7474e7d3 | 1750 | "Unexpected status: %![iter-]+i, status=%s", |
d24d5663 | 1751 | iterator, bt_common_func_status_string(status)); |
870631a2 PP |
1752 | if (status < 0) { |
1753 | BT_LIB_LOGW_APPEND_CAUSE( | |
1754 | "Component input port message iterator's \"seek beginning\" method failed: " | |
1755 | "%![iter-]+i, status=%s", | |
1756 | iterator, bt_common_func_status_string(status)); | |
1757 | } | |
1758 | ||
7474e7d3 | 1759 | switch (status) { |
d24d5663 | 1760 | case BT_FUNC_STATUS_OK: |
7474e7d3 | 1761 | break; |
d24d5663 PP |
1762 | case BT_FUNC_STATUS_ERROR: |
1763 | case BT_FUNC_STATUS_MEMORY_ERROR: | |
1764 | case BT_FUNC_STATUS_AGAIN: | |
7474e7d3 PP |
1765 | goto end; |
1766 | default: | |
498e7994 | 1767 | bt_common_abort(); |
7474e7d3 PP |
1768 | } |
1769 | ||
1770 | /* | |
1771 | * Find the first message which has a default clock | |
1772 | * snapshot greater than or equal to the requested | |
5b9e151d PP |
1773 | * seeking time, and move the received messages from |
1774 | * this point in the batch to this iterator's auto-seek | |
1775 | * message queue. | |
7474e7d3 | 1776 | */ |
da9c4c52 | 1777 | while (!g_queue_is_empty(iterator->auto_seek.msgs)) { |
6871026b | 1778 | bt_object_put_ref_no_null_check( |
da9c4c52 | 1779 | g_queue_pop_tail(iterator->auto_seek.msgs)); |
5b9e151d PP |
1780 | } |
1781 | ||
5b7b55be SM |
1782 | stream_states = create_auto_seek_stream_states(); |
1783 | if (!stream_states) { | |
870631a2 PP |
1784 | BT_LIB_LOGE_APPEND_CAUSE( |
1785 | "Failed to allocate one GHashTable."); | |
d24d5663 | 1786 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
5b7b55be SM |
1787 | goto end; |
1788 | } | |
1789 | ||
7474e7d3 | 1790 | status = find_message_ge_ns_from_origin(iterator, |
5b7b55be | 1791 | ns_from_origin, stream_states); |
7474e7d3 | 1792 | switch (status) { |
d24d5663 PP |
1793 | case BT_FUNC_STATUS_OK: |
1794 | case BT_FUNC_STATUS_END: | |
5b7b55be SM |
1795 | { |
1796 | GHashTableIter iter; | |
1797 | gpointer key, value; | |
1798 | ||
1799 | /* | |
1800 | * If some streams exist at the seek time, prepend the | |
1801 | * required messages to put those streams in the right | |
1802 | * state. | |
1803 | */ | |
1804 | g_hash_table_iter_init(&iter, stream_states); | |
1805 | while (g_hash_table_iter_next (&iter, &key, &value)) { | |
1806 | const bt_stream *stream = key; | |
1807 | struct auto_seek_stream_state *stream_state = | |
1808 | (struct auto_seek_stream_state *) value; | |
1809 | bt_message *msg; | |
1810 | const bt_clock_class *clock_class = bt_stream_class_borrow_default_clock_class_const( | |
1811 | bt_stream_borrow_class_const(stream)); | |
188edac1 SM |
1812 | /* Initialize to silence maybe-uninitialized warning. */ |
1813 | uint64_t raw_value = 0; | |
1814 | ||
1815 | /* | |
1816 | * If we haven't seen a message with a clock snapshot, we don't know if our seek time is within | |
1817 | * the clock's range, so it wouldn't be safe to try to convert ns_from_origin to a clock value. | |
1818 | * | |
1819 | * Also, it would be a bit of a lie to generate a stream begin message with the seek time as its | |
1820 | * clock snapshot, because we don't really know if the stream existed at that time. If we have | |
1821 | * seen a message with a clock snapshot in our seeking, then we are sure that the | |
1822 | * seek time is not below the clock range, and we know the stream was active at that | |
1823 | * time (and that we cut it short). | |
1824 | */ | |
1825 | if (stream_state->seen_clock_snapshot) { | |
1826 | if (clock_raw_value_from_ns_from_origin(clock_class, ns_from_origin, &raw_value) != 0) { | |
1827 | BT_LIB_LOGW("Could not convert nanoseconds from origin to clock value: ns-from-origin=%" PRId64 ", %![cc-]+K", | |
1828 | ns_from_origin, clock_class); | |
1829 | status = BT_FUNC_STATUS_ERROR; | |
1830 | goto end; | |
1831 | } | |
5b7b55be SM |
1832 | } |
1833 | ||
1834 | switch (stream_state->state) { | |
1835 | case AUTO_SEEK_STREAM_STATE_PACKET_BEGAN: | |
1836 | BT_ASSERT(stream_state->packet); | |
1837 | BT_LIB_LOGD("Creating packet message: %![packet-]+a", stream_state->packet); | |
188edac1 SM |
1838 | |
1839 | if (stream->class->packets_have_beginning_default_clock_snapshot) { | |
1840 | /* | |
1841 | * If we are in the PACKET_BEGAN state, it means we have seen a "packet beginning" | |
1842 | * message. If "packet beginning" packets have clock snapshots, then we must have | |
1843 | * seen a clock snapshot. | |
1844 | */ | |
1845 | BT_ASSERT(stream_state->seen_clock_snapshot); | |
1846 | ||
1847 | msg = bt_message_packet_beginning_create_with_default_clock_snapshot( | |
1848 | (bt_self_message_iterator *) iterator, stream_state->packet, raw_value); | |
1849 | } else { | |
1850 | msg = bt_message_packet_beginning_create((bt_self_message_iterator *) iterator, | |
1851 | stream_state->packet); | |
5b7b55be SM |
1852 | } |
1853 | ||
5b7b55be | 1854 | if (!msg) { |
d24d5663 | 1855 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
5b7b55be SM |
1856 | goto end; |
1857 | } | |
1858 | ||
5b7b55be SM |
1859 | g_queue_push_head(iterator->auto_seek.msgs, msg); |
1860 | msg = NULL; | |
1861 | /* fall-thru */ | |
188edac1 | 1862 | |
5b7b55be SM |
1863 | case AUTO_SEEK_STREAM_STATE_STREAM_BEGAN: |
1864 | msg = bt_message_stream_beginning_create( | |
1865 | (bt_self_message_iterator *) iterator, stream); | |
1866 | if (!msg) { | |
d24d5663 | 1867 | status = BT_FUNC_STATUS_MEMORY_ERROR; |
5b7b55be SM |
1868 | goto end; |
1869 | } | |
1870 | ||
188edac1 SM |
1871 | if (stream_state->seen_clock_snapshot) { |
1872 | bt_message_stream_beginning_set_default_clock_snapshot(msg, raw_value); | |
1873 | } | |
1874 | ||
5b7b55be SM |
1875 | g_queue_push_head(iterator->auto_seek.msgs, msg); |
1876 | msg = NULL; | |
1877 | break; | |
1878 | } | |
1879 | } | |
1880 | ||
7474e7d3 | 1881 | /* |
5b9e151d PP |
1882 | * If there are messages in the auto-seek |
1883 | * message queue, replace the user's "next" | |
1884 | * method with a custom, temporary "next" method | |
1885 | * which returns them. | |
7474e7d3 | 1886 | */ |
da9c4c52 | 1887 | if (!g_queue_is_empty(iterator->auto_seek.msgs)) { |
572075a8 SM |
1888 | BT_ASSERT(!iterator->auto_seek.original_next_callback); |
1889 | iterator->auto_seek.original_next_callback = iterator->methods.next; | |
1890 | ||
5b9e151d | 1891 | iterator->methods.next = |
9a2c8b8e | 1892 | (bt_message_iterator_next_method) |
5b9e151d PP |
1893 | post_auto_seek_next; |
1894 | } | |
1895 | ||
1896 | /* | |
d24d5663 PP |
1897 | * `BT_FUNC_STATUS_END` becomes |
1898 | * `BT_FUNC_STATUS_OK`: the next | |
5b9e151d PP |
1899 | * time this iterator's "next" method is called, |
1900 | * it will return | |
d24d5663 | 1901 | * `BT_FUNC_STATUS_END`. |
5b9e151d | 1902 | */ |
d24d5663 | 1903 | status = BT_FUNC_STATUS_OK; |
7474e7d3 | 1904 | break; |
5b7b55be | 1905 | } |
d24d5663 PP |
1906 | case BT_FUNC_STATUS_ERROR: |
1907 | case BT_FUNC_STATUS_MEMORY_ERROR: | |
1908 | case BT_FUNC_STATUS_AGAIN: | |
7474e7d3 | 1909 | goto end; |
7474e7d3 | 1910 | default: |
498e7994 | 1911 | bt_common_abort(); |
7474e7d3 PP |
1912 | } |
1913 | } | |
1914 | ||
54b135a0 SM |
1915 | /* |
1916 | * The following messages returned by the next method (including | |
1917 | * post_auto_seek_next) must be after (or at) `ns_from_origin`. | |
1918 | */ | |
1919 | iterator->last_ns_from_origin = ns_from_origin; | |
1920 | ||
7474e7d3 | 1921 | end: |
5b7b55be SM |
1922 | if (stream_states) { |
1923 | destroy_auto_seek_stream_states(stream_states); | |
1924 | stream_states = NULL; | |
1925 | } | |
870631a2 | 1926 | |
7474e7d3 | 1927 | set_iterator_state_after_seeking(iterator, status); |
7474e7d3 PP |
1928 | return status; |
1929 | } | |
1930 | ||
9b4f9b42 PP |
1931 | bt_bool bt_self_message_iterator_is_interrupted( |
1932 | const struct bt_self_message_iterator *self_msg_iter) | |
1933 | { | |
9a2c8b8e | 1934 | const struct bt_message_iterator *iterator = |
9b4f9b42 PP |
1935 | (const void *) self_msg_iter; |
1936 | ||
1937 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); | |
1938 | return (bt_bool) bt_graph_is_interrupted(iterator->graph); | |
1939 | } | |
1940 | ||
9a2c8b8e PP |
1941 | void bt_message_iterator_get_ref( |
1942 | const struct bt_message_iterator *iterator) | |
c5b9b441 PP |
1943 | { |
1944 | bt_object_get_ref(iterator); | |
1945 | } | |
1946 | ||
9a2c8b8e PP |
1947 | void bt_message_iterator_put_ref( |
1948 | const struct bt_message_iterator *iterator) | |
c5b9b441 PP |
1949 | { |
1950 | bt_object_put_ref(iterator); | |
1951 | } |