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