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