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