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