| 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 "MSG-ITER" |
| 25 | #include <babeltrace/lib-logging-internal.h> |
| 26 | |
| 27 | #include <babeltrace/compiler-internal.h> |
| 28 | #include <babeltrace/trace-ir/field.h> |
| 29 | #include <babeltrace/trace-ir/event-const.h> |
| 30 | #include <babeltrace/trace-ir/event-internal.h> |
| 31 | #include <babeltrace/trace-ir/packet-const.h> |
| 32 | #include <babeltrace/trace-ir/packet-internal.h> |
| 33 | #include <babeltrace/trace-ir/stream-internal.h> |
| 34 | #include <babeltrace/graph/connection-const.h> |
| 35 | #include <babeltrace/graph/connection-internal.h> |
| 36 | #include <babeltrace/graph/component-const.h> |
| 37 | #include <babeltrace/graph/component-internal.h> |
| 38 | #include <babeltrace/graph/component-source-internal.h> |
| 39 | #include <babeltrace/graph/component-class-internal.h> |
| 40 | #include <babeltrace/graph/component-class-sink-colander-internal.h> |
| 41 | #include <babeltrace/graph/component-sink-const.h> |
| 42 | #include <babeltrace/graph/component-sink-internal.h> |
| 43 | #include <babeltrace/graph/message-const.h> |
| 44 | #include <babeltrace/graph/message-iterator-const.h> |
| 45 | #include <babeltrace/graph/message-iterator-internal.h> |
| 46 | #include <babeltrace/graph/self-component-port-input-message-iterator.h> |
| 47 | #include <babeltrace/graph/port-output-message-iterator.h> |
| 48 | #include <babeltrace/graph/message-internal.h> |
| 49 | #include <babeltrace/graph/message-event-const.h> |
| 50 | #include <babeltrace/graph/message-event-internal.h> |
| 51 | #include <babeltrace/graph/message-packet-const.h> |
| 52 | #include <babeltrace/graph/message-packet-internal.h> |
| 53 | #include <babeltrace/graph/message-stream-const.h> |
| 54 | #include <babeltrace/graph/message-stream-internal.h> |
| 55 | #include <babeltrace/graph/message-inactivity-internal.h> |
| 56 | #include <babeltrace/graph/port-const.h> |
| 57 | #include <babeltrace/graph/graph.h> |
| 58 | #include <babeltrace/graph/graph-const.h> |
| 59 | #include <babeltrace/graph/graph-internal.h> |
| 60 | #include <babeltrace/types.h> |
| 61 | #include <babeltrace/assert-internal.h> |
| 62 | #include <babeltrace/assert-pre-internal.h> |
| 63 | #include <stdint.h> |
| 64 | #include <inttypes.h> |
| 65 | #include <stdlib.h> |
| 66 | |
| 67 | /* |
| 68 | * TODO: Use graph's state (number of active iterators, etc.) and |
| 69 | * possibly system specifications to make a better guess than this. |
| 70 | */ |
| 71 | #define MSG_BATCH_SIZE 15 |
| 72 | |
| 73 | #define BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(_iter) \ |
| 74 | BT_ASSERT_PRE((_iter)->state == BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE || \ |
| 75 | (_iter)->state == BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED || \ |
| 76 | (_iter)->state == BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN || \ |
| 77 | (_iter)->state == BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR, \ |
| 78 | "Message iterator is in the wrong state: %!+i", _iter) |
| 79 | |
| 80 | static inline |
| 81 | void _set_self_comp_port_input_msg_iterator_state( |
| 82 | struct bt_self_component_port_input_message_iterator *iterator, |
| 83 | enum bt_self_component_port_input_message_iterator_state state) |
| 84 | { |
| 85 | BT_ASSERT(iterator); |
| 86 | BT_LIB_LOGD("Updating message iterator's state: new-state=%s", |
| 87 | bt_self_component_port_input_message_iterator_state_string(state)); |
| 88 | iterator->state = state; |
| 89 | } |
| 90 | |
| 91 | #ifdef BT_DEV_MODE |
| 92 | # define set_self_comp_port_input_msg_iterator_state _set_self_comp_port_input_msg_iterator_state |
| 93 | #else |
| 94 | # define set_self_comp_port_input_msg_iterator_state(_a, _b) |
| 95 | #endif |
| 96 | |
| 97 | static |
| 98 | void destroy_base_message_iterator(struct bt_object *obj) |
| 99 | { |
| 100 | struct bt_message_iterator *iterator = (void *) obj; |
| 101 | |
| 102 | BT_ASSERT(iterator); |
| 103 | |
| 104 | if (iterator->msgs) { |
| 105 | g_ptr_array_free(iterator->msgs, TRUE); |
| 106 | iterator->msgs = NULL; |
| 107 | } |
| 108 | |
| 109 | g_free(iterator); |
| 110 | } |
| 111 | |
| 112 | static |
| 113 | void bt_self_component_port_input_message_iterator_destroy(struct bt_object *obj) |
| 114 | { |
| 115 | struct bt_self_component_port_input_message_iterator *iterator; |
| 116 | |
| 117 | BT_ASSERT(obj); |
| 118 | |
| 119 | /* |
| 120 | * The message iterator's reference count is 0 if we're |
| 121 | * here. Increment it to avoid a double-destroy (possibly |
| 122 | * infinitely recursive). This could happen for example if the |
| 123 | * message iterator's finalization function does |
| 124 | * bt_object_get_ref() (or anything that causes |
| 125 | * bt_object_get_ref() to be called) on itself (ref. count goes |
| 126 | * from 0 to 1), and then bt_object_put_ref(): the reference |
| 127 | * count would go from 1 to 0 again and this function would be |
| 128 | * called again. |
| 129 | */ |
| 130 | obj->ref_count++; |
| 131 | iterator = (void *) obj; |
| 132 | BT_LIB_LOGD("Destroying self component input port message iterator object: " |
| 133 | "%!+i", iterator); |
| 134 | bt_self_component_port_input_message_iterator_try_finalize(iterator); |
| 135 | |
| 136 | if (iterator->connection) { |
| 137 | /* |
| 138 | * Remove ourself from the originating connection so |
| 139 | * that it does not try to finalize a dangling pointer |
| 140 | * later. |
| 141 | */ |
| 142 | bt_connection_remove_iterator(iterator->connection, iterator); |
| 143 | iterator->connection = NULL; |
| 144 | } |
| 145 | |
| 146 | if (iterator->auto_seek_msgs) { |
| 147 | uint64_t i; |
| 148 | |
| 149 | /* Put any remaining message in the auto-seek array */ |
| 150 | for (i = 0; i < iterator->auto_seek_msgs->len; i++) { |
| 151 | if (iterator->auto_seek_msgs->pdata[i]) { |
| 152 | bt_object_put_no_null_check( |
| 153 | iterator->auto_seek_msgs->pdata[i]); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | g_ptr_array_free(iterator->auto_seek_msgs, TRUE); |
| 158 | iterator->auto_seek_msgs = NULL; |
| 159 | } |
| 160 | |
| 161 | destroy_base_message_iterator(obj); |
| 162 | } |
| 163 | |
| 164 | BT_HIDDEN |
| 165 | void bt_self_component_port_input_message_iterator_try_finalize( |
| 166 | struct bt_self_component_port_input_message_iterator *iterator) |
| 167 | { |
| 168 | typedef void (*method_t)(void *); |
| 169 | |
| 170 | struct bt_component_class *comp_class = NULL; |
| 171 | method_t method = NULL; |
| 172 | |
| 173 | BT_ASSERT(iterator); |
| 174 | |
| 175 | switch (iterator->state) { |
| 176 | case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED: |
| 177 | /* Skip user finalization if user initialization failed */ |
| 178 | BT_LIB_LOGD("Not finalizing non-initialized message iterator: " |
| 179 | "%!+i", iterator); |
| 180 | goto end; |
| 181 | case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED: |
| 182 | /* Already finalized */ |
| 183 | BT_LIB_LOGD("Not finalizing message iterator: already finalized: " |
| 184 | "%!+i", iterator); |
| 185 | goto end; |
| 186 | case BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZING: |
| 187 | /* Already finalized */ |
| 188 | BT_LIB_LOGF("Message iterator is already being finalized: " |
| 189 | "%!+i", iterator); |
| 190 | abort(); |
| 191 | default: |
| 192 | break; |
| 193 | } |
| 194 | |
| 195 | BT_LIB_LOGD("Finalizing message iterator: %!+i", iterator); |
| 196 | set_self_comp_port_input_msg_iterator_state(iterator, |
| 197 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZING); |
| 198 | BT_ASSERT(iterator->upstream_component); |
| 199 | comp_class = iterator->upstream_component->class; |
| 200 | |
| 201 | /* Call user-defined destroy method */ |
| 202 | switch (comp_class->type) { |
| 203 | case BT_COMPONENT_CLASS_TYPE_SOURCE: |
| 204 | { |
| 205 | struct bt_component_class_source *src_comp_cls = |
| 206 | (void *) comp_class; |
| 207 | |
| 208 | method = (method_t) src_comp_cls->methods.msg_iter_finalize; |
| 209 | break; |
| 210 | } |
| 211 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
| 212 | { |
| 213 | struct bt_component_class_filter *flt_comp_cls = |
| 214 | (void *) comp_class; |
| 215 | |
| 216 | method = (method_t) flt_comp_cls->methods.msg_iter_finalize; |
| 217 | break; |
| 218 | } |
| 219 | default: |
| 220 | /* Unreachable */ |
| 221 | abort(); |
| 222 | } |
| 223 | |
| 224 | if (method) { |
| 225 | BT_LIB_LOGD("Calling user's finalization method: %!+i", |
| 226 | iterator); |
| 227 | method(iterator); |
| 228 | } |
| 229 | |
| 230 | iterator->upstream_component = NULL; |
| 231 | iterator->upstream_port = NULL; |
| 232 | set_self_comp_port_input_msg_iterator_state(iterator, |
| 233 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_FINALIZED); |
| 234 | BT_LIB_LOGD("Finalized message iterator: %!+i", iterator); |
| 235 | |
| 236 | end: |
| 237 | return; |
| 238 | } |
| 239 | |
| 240 | BT_HIDDEN |
| 241 | void bt_self_component_port_input_message_iterator_set_connection( |
| 242 | struct bt_self_component_port_input_message_iterator *iterator, |
| 243 | struct bt_connection *connection) |
| 244 | { |
| 245 | BT_ASSERT(iterator); |
| 246 | iterator->connection = connection; |
| 247 | BT_LIB_LOGV("Set message iterator's connection: " |
| 248 | "%![iter-]+i, %![conn-]+x", iterator, connection); |
| 249 | } |
| 250 | |
| 251 | static |
| 252 | int init_message_iterator(struct bt_message_iterator *iterator, |
| 253 | enum bt_message_iterator_type type, |
| 254 | bt_object_release_func destroy) |
| 255 | { |
| 256 | int ret = 0; |
| 257 | |
| 258 | bt_object_init_shared(&iterator->base, destroy); |
| 259 | iterator->type = type; |
| 260 | iterator->msgs = g_ptr_array_new(); |
| 261 | if (!iterator->msgs) { |
| 262 | BT_LOGE_STR("Failed to allocate a GPtrArray."); |
| 263 | ret = -1; |
| 264 | goto end; |
| 265 | } |
| 266 | |
| 267 | g_ptr_array_set_size(iterator->msgs, MSG_BATCH_SIZE); |
| 268 | |
| 269 | end: |
| 270 | return ret; |
| 271 | } |
| 272 | |
| 273 | static |
| 274 | bt_bool can_seek_ns_from_origin_true( |
| 275 | struct bt_self_component_port_input_message_iterator *iterator, |
| 276 | int64_t ns_from_origin) |
| 277 | { |
| 278 | return BT_TRUE; |
| 279 | } |
| 280 | |
| 281 | static |
| 282 | bt_bool can_seek_beginning_true( |
| 283 | struct bt_self_component_port_input_message_iterator *iterator) |
| 284 | { |
| 285 | return BT_TRUE; |
| 286 | } |
| 287 | |
| 288 | static |
| 289 | struct bt_self_component_port_input_message_iterator * |
| 290 | bt_self_component_port_input_message_iterator_create_initial( |
| 291 | struct bt_component *upstream_comp, |
| 292 | struct bt_port *upstream_port) |
| 293 | { |
| 294 | int ret; |
| 295 | struct bt_self_component_port_input_message_iterator *iterator = NULL; |
| 296 | |
| 297 | BT_ASSERT(upstream_comp); |
| 298 | BT_ASSERT(upstream_port); |
| 299 | BT_ASSERT(bt_port_is_connected(upstream_port)); |
| 300 | BT_LIB_LOGD("Creating initial message iterator on self component input port: " |
| 301 | "%![up-comp-]+c, %![up-port-]+p", upstream_comp, upstream_port); |
| 302 | BT_ASSERT(bt_component_get_class_type(upstream_comp) == |
| 303 | BT_COMPONENT_CLASS_TYPE_SOURCE || |
| 304 | bt_component_get_class_type(upstream_comp) == |
| 305 | BT_COMPONENT_CLASS_TYPE_FILTER); |
| 306 | iterator = g_new0( |
| 307 | struct bt_self_component_port_input_message_iterator, 1); |
| 308 | if (!iterator) { |
| 309 | BT_LOGE_STR("Failed to allocate one self component input port " |
| 310 | "message iterator."); |
| 311 | goto end; |
| 312 | } |
| 313 | |
| 314 | ret = init_message_iterator((void *) iterator, |
| 315 | BT_MESSAGE_ITERATOR_TYPE_SELF_COMPONENT_PORT_INPUT, |
| 316 | bt_self_component_port_input_message_iterator_destroy); |
| 317 | if (ret) { |
| 318 | /* init_message_iterator() logs errors */ |
| 319 | BT_OBJECT_PUT_REF_AND_RESET(iterator); |
| 320 | goto end; |
| 321 | } |
| 322 | |
| 323 | iterator->auto_seek_msgs = g_ptr_array_new(); |
| 324 | if (!iterator->auto_seek_msgs) { |
| 325 | BT_LOGE_STR("Failed to allocate a GPtrArray."); |
| 326 | ret = -1; |
| 327 | goto end; |
| 328 | } |
| 329 | |
| 330 | g_ptr_array_set_size(iterator->auto_seek_msgs, MSG_BATCH_SIZE); |
| 331 | iterator->upstream_component = upstream_comp; |
| 332 | iterator->upstream_port = upstream_port; |
| 333 | iterator->connection = iterator->upstream_port->connection; |
| 334 | iterator->graph = bt_component_borrow_graph(upstream_comp); |
| 335 | set_self_comp_port_input_msg_iterator_state(iterator, |
| 336 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_NON_INITIALIZED); |
| 337 | |
| 338 | switch (iterator->upstream_component->class->type) { |
| 339 | case BT_COMPONENT_CLASS_TYPE_SOURCE: |
| 340 | { |
| 341 | struct bt_component_class_source *src_comp_cls = |
| 342 | (void *) iterator->upstream_component->class; |
| 343 | |
| 344 | iterator->methods.next = |
| 345 | (bt_self_component_port_input_message_iterator_next_method) |
| 346 | src_comp_cls->methods.msg_iter_next; |
| 347 | iterator->methods.seek_ns_from_origin = |
| 348 | (bt_self_component_port_input_message_iterator_seek_ns_from_origin_method) |
| 349 | src_comp_cls->methods.msg_iter_seek_ns_from_origin; |
| 350 | iterator->methods.seek_beginning = |
| 351 | (bt_self_component_port_input_message_iterator_seek_beginning_method) |
| 352 | src_comp_cls->methods.msg_iter_seek_beginning; |
| 353 | iterator->methods.can_seek_ns_from_origin = |
| 354 | (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method) |
| 355 | src_comp_cls->methods.msg_iter_can_seek_ns_from_origin; |
| 356 | iterator->methods.can_seek_beginning = |
| 357 | (bt_self_component_port_input_message_iterator_can_seek_beginning_method) |
| 358 | src_comp_cls->methods.msg_iter_can_seek_beginning; |
| 359 | break; |
| 360 | } |
| 361 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
| 362 | { |
| 363 | struct bt_component_class_filter *flt_comp_cls = |
| 364 | (void *) iterator->upstream_component->class; |
| 365 | |
| 366 | iterator->methods.next = |
| 367 | (bt_self_component_port_input_message_iterator_next_method) |
| 368 | flt_comp_cls->methods.msg_iter_next; |
| 369 | iterator->methods.seek_ns_from_origin = |
| 370 | (bt_self_component_port_input_message_iterator_seek_ns_from_origin_method) |
| 371 | flt_comp_cls->methods.msg_iter_seek_ns_from_origin; |
| 372 | iterator->methods.seek_beginning = |
| 373 | (bt_self_component_port_input_message_iterator_seek_beginning_method) |
| 374 | flt_comp_cls->methods.msg_iter_seek_beginning; |
| 375 | iterator->methods.can_seek_ns_from_origin = |
| 376 | (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method) |
| 377 | flt_comp_cls->methods.msg_iter_can_seek_ns_from_origin; |
| 378 | iterator->methods.can_seek_beginning = |
| 379 | (bt_self_component_port_input_message_iterator_can_seek_beginning_method) |
| 380 | flt_comp_cls->methods.msg_iter_can_seek_beginning; |
| 381 | break; |
| 382 | } |
| 383 | default: |
| 384 | abort(); |
| 385 | } |
| 386 | |
| 387 | if (iterator->methods.seek_ns_from_origin && |
| 388 | !iterator->methods.can_seek_ns_from_origin) { |
| 389 | iterator->methods.can_seek_ns_from_origin = |
| 390 | (bt_self_component_port_input_message_iterator_can_seek_ns_from_origin_method) |
| 391 | can_seek_ns_from_origin_true; |
| 392 | } |
| 393 | |
| 394 | if (iterator->methods.seek_beginning && |
| 395 | !iterator->methods.can_seek_beginning) { |
| 396 | iterator->methods.can_seek_beginning = |
| 397 | (bt_self_component_port_input_message_iterator_seek_beginning_method) |
| 398 | can_seek_beginning_true; |
| 399 | } |
| 400 | |
| 401 | BT_LIB_LOGD("Created initial message iterator on self component input port: " |
| 402 | "%![up-port-]+p, %![up-comp-]+c, %![iter-]+i", |
| 403 | upstream_port, upstream_comp, iterator); |
| 404 | |
| 405 | end: |
| 406 | return iterator; |
| 407 | } |
| 408 | |
| 409 | struct bt_self_component_port_input_message_iterator * |
| 410 | bt_self_component_port_input_message_iterator_create( |
| 411 | struct bt_self_component_port_input *self_port) |
| 412 | { |
| 413 | typedef enum bt_self_message_iterator_status (*init_method_t)( |
| 414 | void *, void *, void *); |
| 415 | |
| 416 | init_method_t init_method = NULL; |
| 417 | struct bt_self_component_port_input_message_iterator *iterator = |
| 418 | NULL; |
| 419 | struct bt_port *port = (void *) self_port; |
| 420 | struct bt_port *upstream_port; |
| 421 | struct bt_component *comp; |
| 422 | struct bt_component *upstream_comp; |
| 423 | struct bt_component_class *upstream_comp_cls; |
| 424 | |
| 425 | BT_ASSERT_PRE_NON_NULL(port, "Port"); |
| 426 | comp = bt_port_borrow_component_inline(port); |
| 427 | BT_ASSERT_PRE(bt_port_is_connected(port), |
| 428 | "Port is not connected: %![port-]+p", port); |
| 429 | BT_ASSERT_PRE(comp, "Port is not part of a component: %![port-]+p", |
| 430 | port); |
| 431 | BT_ASSERT_PRE(!bt_component_graph_is_canceled(comp), |
| 432 | "Port's component's graph is canceled: " |
| 433 | "%![port-]+p, %![comp-]+c", port, comp); |
| 434 | BT_ASSERT(port->connection); |
| 435 | upstream_port = port->connection->upstream_port; |
| 436 | BT_ASSERT(upstream_port); |
| 437 | upstream_comp = bt_port_borrow_component_inline(upstream_port); |
| 438 | BT_ASSERT(upstream_comp); |
| 439 | upstream_comp_cls = upstream_comp->class; |
| 440 | BT_ASSERT(upstream_comp->class->type == |
| 441 | BT_COMPONENT_CLASS_TYPE_SOURCE || |
| 442 | upstream_comp->class->type == |
| 443 | BT_COMPONENT_CLASS_TYPE_FILTER); |
| 444 | iterator = bt_self_component_port_input_message_iterator_create_initial( |
| 445 | upstream_comp, upstream_port); |
| 446 | if (!iterator) { |
| 447 | BT_LOGW_STR("Cannot create self component input port " |
| 448 | "message iterator."); |
| 449 | goto end; |
| 450 | } |
| 451 | |
| 452 | switch (upstream_comp_cls->type) { |
| 453 | case BT_COMPONENT_CLASS_TYPE_SOURCE: |
| 454 | { |
| 455 | struct bt_component_class_source *src_comp_cls = |
| 456 | (void *) upstream_comp_cls; |
| 457 | |
| 458 | init_method = |
| 459 | (init_method_t) src_comp_cls->methods.msg_iter_init; |
| 460 | break; |
| 461 | } |
| 462 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
| 463 | { |
| 464 | struct bt_component_class_filter *flt_comp_cls = |
| 465 | (void *) upstream_comp_cls; |
| 466 | |
| 467 | init_method = |
| 468 | (init_method_t) flt_comp_cls->methods.msg_iter_init; |
| 469 | break; |
| 470 | } |
| 471 | default: |
| 472 | /* Unreachable */ |
| 473 | abort(); |
| 474 | } |
| 475 | |
| 476 | if (init_method) { |
| 477 | int iter_status; |
| 478 | |
| 479 | BT_LIB_LOGD("Calling user's initialization method: %!+i", iterator); |
| 480 | iter_status = init_method(iterator, upstream_comp, |
| 481 | upstream_port); |
| 482 | BT_LOGD("User method returned: status=%s", |
| 483 | bt_message_iterator_status_string(iter_status)); |
| 484 | if (iter_status != BT_MESSAGE_ITERATOR_STATUS_OK) { |
| 485 | BT_LOGW_STR("Initialization method failed."); |
| 486 | BT_OBJECT_PUT_REF_AND_RESET(iterator); |
| 487 | goto end; |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | set_self_comp_port_input_msg_iterator_state(iterator, |
| 492 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE); |
| 493 | g_ptr_array_add(port->connection->iterators, iterator); |
| 494 | BT_LIB_LOGD("Created message iterator on self component input port: " |
| 495 | "%![up-port-]+p, %![up-comp-]+c, %![iter-]+i", |
| 496 | upstream_port, upstream_comp, iterator); |
| 497 | |
| 498 | end: |
| 499 | return iterator; |
| 500 | } |
| 501 | |
| 502 | void *bt_self_message_iterator_get_data( |
| 503 | const struct bt_self_message_iterator *self_iterator) |
| 504 | { |
| 505 | struct bt_self_component_port_input_message_iterator *iterator = |
| 506 | (void *) self_iterator; |
| 507 | |
| 508 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); |
| 509 | return iterator->user_data; |
| 510 | } |
| 511 | |
| 512 | void bt_self_message_iterator_set_data( |
| 513 | struct bt_self_message_iterator *self_iterator, void *data) |
| 514 | { |
| 515 | struct bt_self_component_port_input_message_iterator *iterator = |
| 516 | (void *) self_iterator; |
| 517 | |
| 518 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); |
| 519 | iterator->user_data = data; |
| 520 | BT_LIB_LOGV("Set message iterator's user data: " |
| 521 | "%!+i, user-data-addr=%p", iterator, data); |
| 522 | } |
| 523 | |
| 524 | BT_ASSERT_PRE_FUNC |
| 525 | static inline |
| 526 | void bt_message_borrow_packet_stream(const struct bt_message *msg, |
| 527 | const struct bt_stream **stream, |
| 528 | const struct bt_packet **packet) |
| 529 | { |
| 530 | BT_ASSERT(msg); |
| 531 | |
| 532 | switch (msg->type) { |
| 533 | case BT_MESSAGE_TYPE_EVENT: |
| 534 | *packet = bt_event_borrow_packet_const( |
| 535 | bt_message_event_borrow_event_const(msg)); |
| 536 | *stream = bt_packet_borrow_stream_const(*packet); |
| 537 | break; |
| 538 | case BT_MESSAGE_TYPE_STREAM_BEGINNING: |
| 539 | *stream = bt_message_stream_beginning_borrow_stream_const(msg); |
| 540 | break; |
| 541 | case BT_MESSAGE_TYPE_STREAM_END: |
| 542 | *stream = bt_message_stream_end_borrow_stream_const(msg); |
| 543 | break; |
| 544 | case BT_MESSAGE_TYPE_PACKET_BEGINNING: |
| 545 | *packet = bt_message_packet_beginning_borrow_packet_const(msg); |
| 546 | *stream = bt_packet_borrow_stream_const(*packet); |
| 547 | break; |
| 548 | case BT_MESSAGE_TYPE_PACKET_END: |
| 549 | *packet = bt_message_packet_end_borrow_packet_const(msg); |
| 550 | *stream = bt_packet_borrow_stream_const(*packet); |
| 551 | break; |
| 552 | default: |
| 553 | break; |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | enum bt_message_iterator_status |
| 558 | bt_self_component_port_input_message_iterator_next( |
| 559 | struct bt_self_component_port_input_message_iterator *iterator, |
| 560 | bt_message_array_const *msgs, uint64_t *user_count) |
| 561 | { |
| 562 | int status = BT_MESSAGE_ITERATOR_STATUS_OK; |
| 563 | |
| 564 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); |
| 565 | BT_ASSERT_PRE_NON_NULL(msgs, "Message array (output)"); |
| 566 | BT_ASSERT_PRE_NON_NULL(user_count, "Message count (output)"); |
| 567 | BT_ASSERT_PRE(iterator->state == |
| 568 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE, |
| 569 | "Message iterator's \"next\" called, but " |
| 570 | "message iterator is in the wrong state: %!+i", iterator); |
| 571 | BT_ASSERT(iterator->upstream_component); |
| 572 | BT_ASSERT(iterator->upstream_component->class); |
| 573 | BT_ASSERT_PRE(bt_component_borrow_graph(iterator->upstream_component)->is_configured, |
| 574 | "Graph is not configured: %!+g", |
| 575 | bt_component_borrow_graph(iterator->upstream_component)); |
| 576 | BT_LIB_LOGD("Getting next self component input port " |
| 577 | "message iterator's messages: %!+i", iterator); |
| 578 | |
| 579 | /* |
| 580 | * Call the user's "next" method to get the next messages |
| 581 | * and status. |
| 582 | */ |
| 583 | BT_ASSERT(iterator->methods.next); |
| 584 | BT_LOGD_STR("Calling user's \"next\" method."); |
| 585 | status = iterator->methods.next(iterator, |
| 586 | (void *) iterator->base.msgs->pdata, MSG_BATCH_SIZE, |
| 587 | user_count); |
| 588 | BT_LOGD("User method returned: status=%s", |
| 589 | bt_message_iterator_status_string(status)); |
| 590 | if (status < 0) { |
| 591 | BT_LOGW_STR("User method failed."); |
| 592 | goto end; |
| 593 | } |
| 594 | |
| 595 | #ifdef BT_DEV_MODE |
| 596 | /* |
| 597 | * There is no way that this iterator could have been finalized |
| 598 | * during its "next" method, as the only way to do this is to |
| 599 | * put the last iterator's reference, and this can only be done |
| 600 | * by its downstream owner. |
| 601 | * |
| 602 | * For the same reason, there is no way that this iterator could |
| 603 | * have seeked (cannot seek a self message iterator). |
| 604 | */ |
| 605 | BT_ASSERT(iterator->state == |
| 606 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE); |
| 607 | #endif |
| 608 | |
| 609 | switch (status) { |
| 610 | case BT_MESSAGE_ITERATOR_STATUS_OK: |
| 611 | BT_ASSERT_PRE(*user_count <= MSG_BATCH_SIZE, |
| 612 | "Invalid returned message count: greater than " |
| 613 | "batch size: count=%" PRIu64 ", batch-size=%u", |
| 614 | *user_count, MSG_BATCH_SIZE); |
| 615 | *msgs = (void *) iterator->base.msgs->pdata; |
| 616 | break; |
| 617 | case BT_MESSAGE_ITERATOR_STATUS_AGAIN: |
| 618 | goto end; |
| 619 | case BT_MESSAGE_ITERATOR_STATUS_END: |
| 620 | set_self_comp_port_input_msg_iterator_state(iterator, |
| 621 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED); |
| 622 | goto end; |
| 623 | default: |
| 624 | /* Unknown non-error status */ |
| 625 | abort(); |
| 626 | } |
| 627 | |
| 628 | end: |
| 629 | return status; |
| 630 | } |
| 631 | |
| 632 | enum bt_message_iterator_status bt_port_output_message_iterator_next( |
| 633 | struct bt_port_output_message_iterator *iterator, |
| 634 | bt_message_array_const *msgs_to_user, |
| 635 | uint64_t *count_to_user) |
| 636 | { |
| 637 | enum bt_message_iterator_status status; |
| 638 | enum bt_graph_status graph_status; |
| 639 | |
| 640 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); |
| 641 | BT_ASSERT_PRE_NON_NULL(msgs_to_user, "Message array (output)"); |
| 642 | BT_ASSERT_PRE_NON_NULL(count_to_user, "Message count (output)"); |
| 643 | BT_LIB_LOGD("Getting next output port message iterator's messages: " |
| 644 | "%!+i", iterator); |
| 645 | |
| 646 | /* |
| 647 | * As soon as the user calls this function, we mark the graph as |
| 648 | * being definitely configured. |
| 649 | */ |
| 650 | bt_graph_set_is_configured(iterator->graph, true); |
| 651 | |
| 652 | graph_status = bt_graph_consume_sink_no_check(iterator->graph, |
| 653 | iterator->colander); |
| 654 | switch (graph_status) { |
| 655 | case BT_GRAPH_STATUS_CANCELED: |
| 656 | case BT_GRAPH_STATUS_AGAIN: |
| 657 | case BT_GRAPH_STATUS_END: |
| 658 | case BT_GRAPH_STATUS_NOMEM: |
| 659 | status = (int) graph_status; |
| 660 | break; |
| 661 | case BT_GRAPH_STATUS_OK: |
| 662 | status = BT_MESSAGE_ITERATOR_STATUS_OK; |
| 663 | |
| 664 | /* |
| 665 | * On success, the colander sink moves the messages |
| 666 | * to this iterator's array and sets this iterator's |
| 667 | * message count: move them to the user. |
| 668 | */ |
| 669 | *msgs_to_user = (void *) iterator->base.msgs->pdata; |
| 670 | *count_to_user = iterator->count; |
| 671 | break; |
| 672 | default: |
| 673 | /* Other errors */ |
| 674 | status = BT_MESSAGE_ITERATOR_STATUS_ERROR; |
| 675 | } |
| 676 | |
| 677 | return status; |
| 678 | } |
| 679 | |
| 680 | struct bt_component * |
| 681 | bt_self_component_port_input_message_iterator_borrow_component( |
| 682 | struct bt_self_component_port_input_message_iterator *iterator) |
| 683 | { |
| 684 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); |
| 685 | return iterator->upstream_component; |
| 686 | } |
| 687 | |
| 688 | const struct bt_component * |
| 689 | bt_self_component_port_input_message_iterator_borrow_component_const( |
| 690 | const struct bt_self_component_port_input_message_iterator *iterator) |
| 691 | { |
| 692 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); |
| 693 | return iterator->upstream_component; |
| 694 | } |
| 695 | |
| 696 | struct bt_self_component *bt_self_message_iterator_borrow_component( |
| 697 | struct bt_self_message_iterator *self_iterator) |
| 698 | { |
| 699 | struct bt_self_component_port_input_message_iterator *iterator = |
| 700 | (void *) self_iterator; |
| 701 | |
| 702 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); |
| 703 | return (void *) iterator->upstream_component; |
| 704 | } |
| 705 | |
| 706 | struct bt_self_port_output *bt_self_message_iterator_borrow_port( |
| 707 | struct bt_self_message_iterator *self_iterator) |
| 708 | { |
| 709 | struct bt_self_component_port_input_message_iterator *iterator = |
| 710 | (void *) self_iterator; |
| 711 | |
| 712 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); |
| 713 | return (void *) iterator->upstream_port; |
| 714 | } |
| 715 | |
| 716 | static |
| 717 | void bt_port_output_message_iterator_destroy(struct bt_object *obj) |
| 718 | { |
| 719 | struct bt_port_output_message_iterator *iterator = (void *) obj; |
| 720 | |
| 721 | BT_LIB_LOGD("Destroying output port message iterator object: %!+i", |
| 722 | iterator); |
| 723 | BT_LOGD_STR("Putting graph."); |
| 724 | BT_OBJECT_PUT_REF_AND_RESET(iterator->graph); |
| 725 | BT_LOGD_STR("Putting colander sink component."); |
| 726 | BT_OBJECT_PUT_REF_AND_RESET(iterator->colander); |
| 727 | destroy_base_message_iterator(obj); |
| 728 | } |
| 729 | |
| 730 | struct bt_port_output_message_iterator * |
| 731 | bt_port_output_message_iterator_create(struct bt_graph *graph, |
| 732 | const struct bt_port_output *output_port) |
| 733 | { |
| 734 | struct bt_port_output_message_iterator *iterator = NULL; |
| 735 | struct bt_component_class_sink *colander_comp_cls = NULL; |
| 736 | struct bt_component *output_port_comp = NULL; |
| 737 | struct bt_component_sink *colander_comp; |
| 738 | enum bt_graph_status graph_status; |
| 739 | struct bt_port_input *colander_in_port = NULL; |
| 740 | struct bt_component_class_sink_colander_data colander_data; |
| 741 | int ret; |
| 742 | |
| 743 | BT_ASSERT_PRE_NON_NULL(graph, "Graph"); |
| 744 | BT_ASSERT_PRE_NON_NULL(output_port, "Output port"); |
| 745 | output_port_comp = bt_port_borrow_component_inline( |
| 746 | (const void *) output_port); |
| 747 | BT_ASSERT_PRE(output_port_comp, |
| 748 | "Output port has no component: %!+p", output_port); |
| 749 | BT_ASSERT_PRE(bt_component_borrow_graph(output_port_comp) == |
| 750 | (void *) graph, |
| 751 | "Output port is not part of graph: %![graph-]+g, %![port-]+p", |
| 752 | graph, output_port); |
| 753 | |
| 754 | /* Create message iterator */ |
| 755 | BT_LIB_LOGD("Creating message iterator on output port: " |
| 756 | "%![port-]+p, %![comp-]+c", output_port, output_port_comp); |
| 757 | iterator = g_new0(struct bt_port_output_message_iterator, 1); |
| 758 | if (!iterator) { |
| 759 | BT_LOGE_STR("Failed to allocate one output port message iterator."); |
| 760 | goto error; |
| 761 | } |
| 762 | |
| 763 | ret = init_message_iterator((void *) iterator, |
| 764 | BT_MESSAGE_ITERATOR_TYPE_PORT_OUTPUT, |
| 765 | bt_port_output_message_iterator_destroy); |
| 766 | if (ret) { |
| 767 | /* init_message_iterator() logs errors */ |
| 768 | BT_OBJECT_PUT_REF_AND_RESET(iterator); |
| 769 | goto end; |
| 770 | } |
| 771 | |
| 772 | /* Create colander component */ |
| 773 | colander_comp_cls = bt_component_class_sink_colander_get(); |
| 774 | if (!colander_comp_cls) { |
| 775 | BT_LOGW("Cannot get colander sink component class."); |
| 776 | goto error; |
| 777 | } |
| 778 | |
| 779 | iterator->graph = graph; |
| 780 | bt_object_get_no_null_check(iterator->graph); |
| 781 | colander_data.msgs = (void *) iterator->base.msgs->pdata; |
| 782 | colander_data.count_addr = &iterator->count; |
| 783 | |
| 784 | /* Hope that nobody uses this very unique name */ |
| 785 | graph_status = |
| 786 | bt_graph_add_sink_component_with_init_method_data( |
| 787 | (void *) graph, colander_comp_cls, |
| 788 | "colander-36ac3409-b1a8-4d60-ab1f-4fdf341a8fb1", |
| 789 | NULL, &colander_data, (void *) &iterator->colander); |
| 790 | if (graph_status != BT_GRAPH_STATUS_OK) { |
| 791 | BT_LIB_LOGW("Cannot add colander sink component to graph: " |
| 792 | "%1[graph-]+g, status=%s", graph, |
| 793 | bt_graph_status_string(graph_status)); |
| 794 | goto error; |
| 795 | } |
| 796 | |
| 797 | /* |
| 798 | * Connect provided output port to the colander component's |
| 799 | * input port. |
| 800 | */ |
| 801 | colander_in_port = |
| 802 | (void *) bt_component_sink_borrow_input_port_by_index_const( |
| 803 | (void *) iterator->colander, 0); |
| 804 | BT_ASSERT(colander_in_port); |
| 805 | graph_status = bt_graph_connect_ports(graph, |
| 806 | output_port, colander_in_port, NULL); |
| 807 | if (graph_status != BT_GRAPH_STATUS_OK) { |
| 808 | BT_LIB_LOGW("Cannot add colander sink component to graph: " |
| 809 | "%![graph-]+g, %![comp-]+c, status=%s", graph, |
| 810 | iterator->colander, |
| 811 | bt_graph_status_string(graph_status)); |
| 812 | goto error; |
| 813 | } |
| 814 | |
| 815 | /* |
| 816 | * At this point everything went fine. Make the graph |
| 817 | * nonconsumable forever so that only this message iterator |
| 818 | * can consume (thanks to bt_graph_consume_sink_no_check()). |
| 819 | * This avoids leaking the message created by the colander |
| 820 | * sink and moved to the message iterator's message |
| 821 | * member. |
| 822 | */ |
| 823 | bt_graph_set_can_consume(iterator->graph, false); |
| 824 | goto end; |
| 825 | |
| 826 | error: |
| 827 | if (iterator && iterator->graph && iterator->colander) { |
| 828 | int ret; |
| 829 | |
| 830 | /* Remove created colander component from graph if any */ |
| 831 | colander_comp = iterator->colander; |
| 832 | BT_OBJECT_PUT_REF_AND_RESET(iterator->colander); |
| 833 | |
| 834 | /* |
| 835 | * At this point the colander component's reference |
| 836 | * count is 0 because iterator->colander was the only |
| 837 | * owner. We also know that it is not connected because |
| 838 | * this is the last operation before this function |
| 839 | * succeeds. |
| 840 | * |
| 841 | * Since we honor the preconditions here, |
| 842 | * bt_graph_remove_unconnected_component() always |
| 843 | * succeeds. |
| 844 | */ |
| 845 | ret = bt_graph_remove_unconnected_component(iterator->graph, |
| 846 | (void *) colander_comp); |
| 847 | BT_ASSERT(ret == 0); |
| 848 | } |
| 849 | |
| 850 | BT_OBJECT_PUT_REF_AND_RESET(iterator); |
| 851 | |
| 852 | end: |
| 853 | bt_object_put_ref(colander_comp_cls); |
| 854 | return (void *) iterator; |
| 855 | } |
| 856 | |
| 857 | bt_bool bt_self_component_port_input_message_iterator_can_seek_ns_from_origin( |
| 858 | struct bt_self_component_port_input_message_iterator *iterator, |
| 859 | int64_t ns_from_origin) |
| 860 | { |
| 861 | bt_bool can = BT_FALSE; |
| 862 | |
| 863 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); |
| 864 | BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator); |
| 865 | BT_ASSERT_PRE(bt_component_borrow_graph(iterator->upstream_component)->is_configured, |
| 866 | "Graph is not configured: %!+g", |
| 867 | bt_component_borrow_graph(iterator->upstream_component)); |
| 868 | |
| 869 | if (iterator->methods.can_seek_ns_from_origin) { |
| 870 | can = iterator->methods.can_seek_ns_from_origin(iterator, |
| 871 | ns_from_origin); |
| 872 | goto end; |
| 873 | } |
| 874 | |
| 875 | /* |
| 876 | * Automatic seeking fall back: if we can seek to the beginning, |
| 877 | * then we can automatically seek to any message. |
| 878 | */ |
| 879 | if (iterator->methods.can_seek_beginning) { |
| 880 | can = iterator->methods.can_seek_beginning(iterator); |
| 881 | } |
| 882 | |
| 883 | end: |
| 884 | return can; |
| 885 | } |
| 886 | |
| 887 | bt_bool bt_self_component_port_input_message_iterator_can_seek_beginning( |
| 888 | struct bt_self_component_port_input_message_iterator *iterator) |
| 889 | { |
| 890 | bt_bool can = BT_FALSE; |
| 891 | |
| 892 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); |
| 893 | BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator); |
| 894 | BT_ASSERT_PRE(bt_component_borrow_graph(iterator->upstream_component)->is_configured, |
| 895 | "Graph is not configured: %!+g", |
| 896 | bt_component_borrow_graph(iterator->upstream_component)); |
| 897 | |
| 898 | if (iterator->methods.can_seek_beginning) { |
| 899 | can = iterator->methods.can_seek_beginning(iterator); |
| 900 | } |
| 901 | |
| 902 | return can; |
| 903 | } |
| 904 | |
| 905 | static inline |
| 906 | void _set_iterator_state_after_seeking( |
| 907 | struct bt_self_component_port_input_message_iterator *iterator, |
| 908 | enum bt_message_iterator_status status) |
| 909 | { |
| 910 | enum bt_self_component_port_input_message_iterator_state new_state = 0; |
| 911 | |
| 912 | /* Set iterator's state depending on seeking status */ |
| 913 | switch (status) { |
| 914 | case BT_MESSAGE_ITERATOR_STATUS_OK: |
| 915 | new_state = BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE; |
| 916 | break; |
| 917 | case BT_MESSAGE_ITERATOR_STATUS_AGAIN: |
| 918 | new_state = BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_AGAIN; |
| 919 | break; |
| 920 | case BT_MESSAGE_ITERATOR_STATUS_ERROR: |
| 921 | case BT_MESSAGE_ITERATOR_STATUS_NOMEM: |
| 922 | new_state = BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_LAST_SEEKING_RETURNED_ERROR; |
| 923 | break; |
| 924 | case BT_MESSAGE_ITERATOR_STATUS_END: |
| 925 | new_state = BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ENDED; |
| 926 | break; |
| 927 | default: |
| 928 | abort(); |
| 929 | } |
| 930 | |
| 931 | set_self_comp_port_input_msg_iterator_state(iterator, new_state); |
| 932 | } |
| 933 | |
| 934 | #ifdef BT_DEV_MODE |
| 935 | # define set_iterator_state_after_seeking _set_iterator_state_after_seeking |
| 936 | #else |
| 937 | # define set_iterator_state_after_seeking(_iter, _status) |
| 938 | #endif |
| 939 | |
| 940 | enum bt_message_iterator_status |
| 941 | bt_self_component_port_input_message_iterator_seek_beginning( |
| 942 | struct bt_self_component_port_input_message_iterator *iterator) |
| 943 | { |
| 944 | int status; |
| 945 | |
| 946 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); |
| 947 | BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator); |
| 948 | BT_ASSERT_PRE(bt_component_borrow_graph(iterator->upstream_component)->is_configured, |
| 949 | "Graph is not configured: %!+g", |
| 950 | bt_component_borrow_graph(iterator->upstream_component)); |
| 951 | BT_ASSERT_PRE( |
| 952 | bt_self_component_port_input_message_iterator_can_seek_beginning( |
| 953 | iterator), |
| 954 | "Message iterator cannot seek beginning: %!+i", iterator); |
| 955 | BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i", iterator); |
| 956 | set_self_comp_port_input_msg_iterator_state(iterator, |
| 957 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_SEEKING); |
| 958 | status = iterator->methods.seek_beginning(iterator); |
| 959 | BT_LOGD("User method returned: status=%s", |
| 960 | bt_message_iterator_status_string(status)); |
| 961 | BT_ASSERT_PRE(status == BT_MESSAGE_ITERATOR_STATUS_OK || |
| 962 | status == BT_MESSAGE_ITERATOR_STATUS_ERROR || |
| 963 | status == BT_MESSAGE_ITERATOR_STATUS_NOMEM || |
| 964 | status == BT_MESSAGE_ITERATOR_STATUS_AGAIN, |
| 965 | "Unexpected status: %![iter-]+i, status=%s", |
| 966 | iterator, bt_self_message_iterator_status_string(status)); |
| 967 | set_iterator_state_after_seeking(iterator, status); |
| 968 | return status; |
| 969 | } |
| 970 | |
| 971 | static inline |
| 972 | int get_message_ns_from_origin(const struct bt_message *msg, |
| 973 | int64_t *ns_from_origin, bool *ignore) |
| 974 | { |
| 975 | const struct bt_clock_snapshot *clk_snapshot = NULL; |
| 976 | int ret = 0; |
| 977 | |
| 978 | switch (msg->type) { |
| 979 | case BT_MESSAGE_TYPE_EVENT: |
| 980 | { |
| 981 | const struct bt_message_event *event_msg = |
| 982 | (const void *) msg; |
| 983 | |
| 984 | clk_snapshot = event_msg->event->default_cs; |
| 985 | BT_ASSERT_PRE(clk_snapshot, |
| 986 | "Event has no default clock snapshot: %!+e", |
| 987 | event_msg->event); |
| 988 | break; |
| 989 | } |
| 990 | case BT_MESSAGE_TYPE_INACTIVITY: |
| 991 | { |
| 992 | const struct bt_message_inactivity *inactivity_msg = |
| 993 | (const void *) msg; |
| 994 | |
| 995 | BT_ASSERT(inactivity_msg->default_cs); |
| 996 | clk_snapshot = inactivity_msg->default_cs; |
| 997 | break; |
| 998 | } |
| 999 | case BT_MESSAGE_TYPE_STREAM_BEGINNING: |
| 1000 | case BT_MESSAGE_TYPE_STREAM_END: |
| 1001 | /* Ignore */ |
| 1002 | goto end; |
| 1003 | case BT_MESSAGE_TYPE_PACKET_BEGINNING: |
| 1004 | { |
| 1005 | const struct bt_message_packet_beginning *pkt_msg = |
| 1006 | (const void *) msg; |
| 1007 | |
| 1008 | clk_snapshot = pkt_msg->packet->default_beginning_cs; |
| 1009 | break; |
| 1010 | } |
| 1011 | case BT_MESSAGE_TYPE_PACKET_END: |
| 1012 | { |
| 1013 | const struct bt_message_packet_end *pkt_msg = |
| 1014 | (const void *) msg; |
| 1015 | |
| 1016 | clk_snapshot = pkt_msg->packet->default_end_cs; |
| 1017 | break; |
| 1018 | } |
| 1019 | default: |
| 1020 | abort(); |
| 1021 | } |
| 1022 | |
| 1023 | if (!clk_snapshot) { |
| 1024 | *ignore = true; |
| 1025 | goto end; |
| 1026 | } |
| 1027 | |
| 1028 | ret = bt_clock_snapshot_get_ns_from_origin(clk_snapshot, |
| 1029 | ns_from_origin); |
| 1030 | |
| 1031 | end: |
| 1032 | return ret; |
| 1033 | } |
| 1034 | |
| 1035 | static |
| 1036 | enum bt_message_iterator_status find_message_ge_ns_from_origin( |
| 1037 | struct bt_self_component_port_input_message_iterator *iterator, |
| 1038 | int64_t ns_from_origin) |
| 1039 | { |
| 1040 | int status; |
| 1041 | enum bt_self_component_port_input_message_iterator_state init_state = |
| 1042 | iterator->state; |
| 1043 | const struct bt_message *messages[MSG_BATCH_SIZE]; |
| 1044 | uint64_t user_count = 0; |
| 1045 | uint64_t i; |
| 1046 | |
| 1047 | BT_ASSERT(iterator); |
| 1048 | memset(&messages[0], 0, sizeof(messages[0]) * MSG_BATCH_SIZE); |
| 1049 | |
| 1050 | /* |
| 1051 | * Make this iterator temporarily active (not seeking) to call |
| 1052 | * the "next" method. |
| 1053 | */ |
| 1054 | set_self_comp_port_input_msg_iterator_state(iterator, |
| 1055 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE); |
| 1056 | |
| 1057 | BT_ASSERT(iterator->methods.next); |
| 1058 | |
| 1059 | while (true) { |
| 1060 | /* |
| 1061 | * Call the user's "next" method to get the next |
| 1062 | * messages and status. |
| 1063 | */ |
| 1064 | BT_LOGD_STR("Calling user's \"next\" method."); |
| 1065 | status = iterator->methods.next(iterator, |
| 1066 | &messages[0], MSG_BATCH_SIZE, &user_count); |
| 1067 | BT_LOGD("User method returned: status=%s", |
| 1068 | bt_message_iterator_status_string(status)); |
| 1069 | |
| 1070 | #ifdef BT_DEV_MODE |
| 1071 | /* |
| 1072 | * The user's "next" method must not do any action which |
| 1073 | * would change the iterator's state. |
| 1074 | */ |
| 1075 | BT_ASSERT(iterator->state == |
| 1076 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_ACTIVE); |
| 1077 | #endif |
| 1078 | |
| 1079 | switch (status) { |
| 1080 | case BT_MESSAGE_ITERATOR_STATUS_OK: |
| 1081 | BT_ASSERT_PRE(user_count <= MSG_BATCH_SIZE, |
| 1082 | "Invalid returned message count: greater than " |
| 1083 | "batch size: count=%" PRIu64 ", batch-size=%u", |
| 1084 | user_count, MSG_BATCH_SIZE); |
| 1085 | break; |
| 1086 | case BT_MESSAGE_ITERATOR_STATUS_AGAIN: |
| 1087 | case BT_MESSAGE_ITERATOR_STATUS_ERROR: |
| 1088 | case BT_MESSAGE_ITERATOR_STATUS_NOMEM: |
| 1089 | case BT_MESSAGE_ITERATOR_STATUS_END: |
| 1090 | goto end; |
| 1091 | default: |
| 1092 | abort(); |
| 1093 | } |
| 1094 | |
| 1095 | /* |
| 1096 | * Find first message which has a default clock snapshot |
| 1097 | * that is greater than or equal to the requested value. |
| 1098 | * |
| 1099 | * For event and inactivity messages, compare with the |
| 1100 | * default clock snapshot. |
| 1101 | * |
| 1102 | * For packet beginning messages, compare with the |
| 1103 | * default beginning clock snapshot, if any. |
| 1104 | * |
| 1105 | * For packet end messages, compare with the default end |
| 1106 | * clock snapshot, if any. |
| 1107 | * |
| 1108 | * For stream beginning, stream end, ignore. |
| 1109 | */ |
| 1110 | for (i = 0; i < user_count; i++) { |
| 1111 | const struct bt_message *msg = messages[i]; |
| 1112 | int64_t msg_ns_from_origin; |
| 1113 | bool ignore = false; |
| 1114 | int ret; |
| 1115 | |
| 1116 | BT_ASSERT(msg); |
| 1117 | ret = get_message_ns_from_origin(msg, &msg_ns_from_origin, |
| 1118 | &ignore); |
| 1119 | if (ret) { |
| 1120 | status = BT_MESSAGE_ITERATOR_STATUS_ERROR; |
| 1121 | goto end; |
| 1122 | } |
| 1123 | |
| 1124 | if (ignore) { |
| 1125 | /* Skip message without a clock snapshot */ |
| 1126 | continue; |
| 1127 | } |
| 1128 | |
| 1129 | if (msg_ns_from_origin >= ns_from_origin) { |
| 1130 | /* |
| 1131 | * We found it: move this message and |
| 1132 | * the following ones to the iterator's |
| 1133 | * auto-seek message array. |
| 1134 | */ |
| 1135 | uint64_t j; |
| 1136 | |
| 1137 | for (j = i; j < user_count; j++) { |
| 1138 | iterator->auto_seek_msgs->pdata[j - i] = |
| 1139 | (void *) messages[j]; |
| 1140 | messages[j] = NULL; |
| 1141 | } |
| 1142 | |
| 1143 | iterator->auto_seek_msg_count = user_count - i; |
| 1144 | goto end; |
| 1145 | } |
| 1146 | |
| 1147 | bt_object_put_no_null_check(msg); |
| 1148 | messages[i] = NULL; |
| 1149 | } |
| 1150 | } |
| 1151 | |
| 1152 | end: |
| 1153 | for (i = 0; i < user_count; i++) { |
| 1154 | if (messages[i]) { |
| 1155 | bt_object_put_no_null_check(messages[i]); |
| 1156 | } |
| 1157 | } |
| 1158 | |
| 1159 | set_self_comp_port_input_msg_iterator_state(iterator, init_state); |
| 1160 | return status; |
| 1161 | } |
| 1162 | |
| 1163 | static |
| 1164 | enum bt_self_message_iterator_status post_auto_seek_next( |
| 1165 | struct bt_self_component_port_input_message_iterator *iterator, |
| 1166 | bt_message_array_const msgs, uint64_t capacity, |
| 1167 | uint64_t *count) |
| 1168 | { |
| 1169 | BT_ASSERT(iterator->auto_seek_msg_count <= capacity); |
| 1170 | BT_ASSERT(iterator->auto_seek_msg_count > 0); |
| 1171 | |
| 1172 | /* |
| 1173 | * Move auto-seek messages to the output array (which is this |
| 1174 | * iterator's base message array. |
| 1175 | */ |
| 1176 | memcpy(&msgs[0], &iterator->auto_seek_msgs->pdata[0], |
| 1177 | sizeof(msgs[0]) * iterator->auto_seek_msg_count); |
| 1178 | memset(&iterator->auto_seek_msgs->pdata[0], 0, |
| 1179 | sizeof(iterator->auto_seek_msgs->pdata[0]) * |
| 1180 | iterator->auto_seek_msg_count); |
| 1181 | *count = iterator->auto_seek_msg_count; |
| 1182 | |
| 1183 | /* Restore real user's "next" method */ |
| 1184 | switch (iterator->upstream_component->class->type) { |
| 1185 | case BT_COMPONENT_CLASS_TYPE_SOURCE: |
| 1186 | { |
| 1187 | struct bt_component_class_source *src_comp_cls = |
| 1188 | (void *) iterator->upstream_component->class; |
| 1189 | |
| 1190 | iterator->methods.next = |
| 1191 | (bt_self_component_port_input_message_iterator_next_method) |
| 1192 | src_comp_cls->methods.msg_iter_next; |
| 1193 | break; |
| 1194 | } |
| 1195 | case BT_COMPONENT_CLASS_TYPE_FILTER: |
| 1196 | { |
| 1197 | struct bt_component_class_filter *flt_comp_cls = |
| 1198 | (void *) iterator->upstream_component->class; |
| 1199 | |
| 1200 | iterator->methods.next = |
| 1201 | (bt_self_component_port_input_message_iterator_next_method) |
| 1202 | flt_comp_cls->methods.msg_iter_next; |
| 1203 | break; |
| 1204 | } |
| 1205 | default: |
| 1206 | abort(); |
| 1207 | } |
| 1208 | |
| 1209 | return BT_SELF_MESSAGE_ITERATOR_STATUS_OK; |
| 1210 | } |
| 1211 | |
| 1212 | enum bt_message_iterator_status |
| 1213 | bt_self_component_port_input_message_iterator_seek_ns_from_origin( |
| 1214 | struct bt_self_component_port_input_message_iterator *iterator, |
| 1215 | int64_t ns_from_origin) |
| 1216 | { |
| 1217 | int status; |
| 1218 | |
| 1219 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); |
| 1220 | BT_ASSERT_PRE_ITER_HAS_STATE_TO_SEEK(iterator); |
| 1221 | BT_ASSERT_PRE(bt_component_borrow_graph(iterator->upstream_component)->is_configured, |
| 1222 | "Graph is not configured: %!+g", |
| 1223 | bt_component_borrow_graph(iterator->upstream_component)); |
| 1224 | BT_ASSERT_PRE( |
| 1225 | bt_self_component_port_input_message_iterator_can_seek_ns_from_origin( |
| 1226 | iterator, ns_from_origin), |
| 1227 | "Message iterator cannot seek nanoseconds from origin: %!+i, " |
| 1228 | "ns-from-origin=%" PRId64, iterator, ns_from_origin); |
| 1229 | set_self_comp_port_input_msg_iterator_state(iterator, |
| 1230 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_STATE_SEEKING); |
| 1231 | |
| 1232 | if (iterator->methods.seek_ns_from_origin) { |
| 1233 | BT_LIB_LOGD("Calling user's \"seek nanoseconds from origin\" method: " |
| 1234 | "%![iter-]+i, ns=%" PRId64, iterator, ns_from_origin); |
| 1235 | status = iterator->methods.seek_ns_from_origin(iterator, |
| 1236 | ns_from_origin); |
| 1237 | BT_LOGD("User method returned: status=%s", |
| 1238 | bt_message_iterator_status_string(status)); |
| 1239 | BT_ASSERT_PRE(status == BT_MESSAGE_ITERATOR_STATUS_OK || |
| 1240 | status == BT_MESSAGE_ITERATOR_STATUS_ERROR || |
| 1241 | status == BT_MESSAGE_ITERATOR_STATUS_NOMEM || |
| 1242 | status == BT_MESSAGE_ITERATOR_STATUS_AGAIN, |
| 1243 | "Unexpected status: %![iter-]+i, status=%s", |
| 1244 | iterator, |
| 1245 | bt_self_message_iterator_status_string(status)); |
| 1246 | } else { |
| 1247 | /* Start automatic seeking: seek beginning first */ |
| 1248 | BT_ASSERT(iterator->methods.can_seek_beginning(iterator)); |
| 1249 | BT_ASSERT(iterator->methods.seek_beginning); |
| 1250 | BT_LIB_LOGD("Calling user's \"seek beginning\" method: %!+i", |
| 1251 | iterator); |
| 1252 | status = iterator->methods.seek_beginning(iterator); |
| 1253 | BT_LOGD("User method returned: status=%s", |
| 1254 | bt_message_iterator_status_string(status)); |
| 1255 | BT_ASSERT_PRE(status == BT_MESSAGE_ITERATOR_STATUS_OK || |
| 1256 | status == BT_MESSAGE_ITERATOR_STATUS_ERROR || |
| 1257 | status == BT_MESSAGE_ITERATOR_STATUS_NOMEM || |
| 1258 | status == BT_MESSAGE_ITERATOR_STATUS_AGAIN, |
| 1259 | "Unexpected status: %![iter-]+i, status=%s", |
| 1260 | iterator, |
| 1261 | bt_self_message_iterator_status_string(status)); |
| 1262 | switch (status) { |
| 1263 | case BT_MESSAGE_ITERATOR_STATUS_OK: |
| 1264 | break; |
| 1265 | case BT_MESSAGE_ITERATOR_STATUS_ERROR: |
| 1266 | case BT_MESSAGE_ITERATOR_STATUS_NOMEM: |
| 1267 | case BT_MESSAGE_ITERATOR_STATUS_AGAIN: |
| 1268 | goto end; |
| 1269 | default: |
| 1270 | abort(); |
| 1271 | } |
| 1272 | |
| 1273 | /* |
| 1274 | * Find the first message which has a default clock |
| 1275 | * snapshot greater than or equal to the requested |
| 1276 | * nanoseconds from origin, and move the received |
| 1277 | * messages from this point in the batch to this |
| 1278 | * iterator's auto-seek message array. |
| 1279 | */ |
| 1280 | status = find_message_ge_ns_from_origin(iterator, |
| 1281 | ns_from_origin); |
| 1282 | switch (status) { |
| 1283 | case BT_MESSAGE_ITERATOR_STATUS_OK: |
| 1284 | /* |
| 1285 | * Replace the user's "next" method with a |
| 1286 | * custom, temporary "next" method which returns |
| 1287 | * the messages in the iterator's message array. |
| 1288 | */ |
| 1289 | iterator->methods.next = |
| 1290 | (bt_self_component_port_input_message_iterator_next_method) |
| 1291 | post_auto_seek_next; |
| 1292 | break; |
| 1293 | case BT_MESSAGE_ITERATOR_STATUS_ERROR: |
| 1294 | case BT_MESSAGE_ITERATOR_STATUS_NOMEM: |
| 1295 | case BT_MESSAGE_ITERATOR_STATUS_AGAIN: |
| 1296 | goto end; |
| 1297 | case BT_MESSAGE_ITERATOR_STATUS_END: |
| 1298 | /* |
| 1299 | * The iterator reached the end: just return |
| 1300 | * `BT_MESSAGE_ITERATOR_STATUS_OK` here, as if |
| 1301 | * the seeking operation occured: the next |
| 1302 | * "next" method will return |
| 1303 | * `BT_MESSAGE_ITERATOR_STATUS_END` itself. |
| 1304 | */ |
| 1305 | break; |
| 1306 | default: |
| 1307 | abort(); |
| 1308 | } |
| 1309 | } |
| 1310 | |
| 1311 | end: |
| 1312 | set_iterator_state_after_seeking(iterator, status); |
| 1313 | |
| 1314 | if (status == BT_MESSAGE_ITERATOR_STATUS_END) { |
| 1315 | status = BT_MESSAGE_ITERATOR_STATUS_OK; |
| 1316 | } |
| 1317 | |
| 1318 | return status; |
| 1319 | } |
| 1320 | |
| 1321 | static inline |
| 1322 | bt_self_component_port_input_message_iterator * |
| 1323 | borrow_output_port_message_iterator_upstream_iterator( |
| 1324 | struct bt_port_output_message_iterator *iterator) |
| 1325 | { |
| 1326 | struct bt_component_class_sink_colander_priv_data *colander_data; |
| 1327 | |
| 1328 | BT_ASSERT(iterator); |
| 1329 | colander_data = (void *) iterator->colander->parent.user_data; |
| 1330 | BT_ASSERT(colander_data); |
| 1331 | BT_ASSERT(colander_data->msg_iter); |
| 1332 | return colander_data->msg_iter; |
| 1333 | } |
| 1334 | |
| 1335 | bt_bool bt_port_output_message_iterator_can_seek_ns_from_origin( |
| 1336 | struct bt_port_output_message_iterator *iterator, |
| 1337 | int64_t ns_from_origin) |
| 1338 | { |
| 1339 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); |
| 1340 | return bt_self_component_port_input_message_iterator_can_seek_ns_from_origin( |
| 1341 | borrow_output_port_message_iterator_upstream_iterator( |
| 1342 | iterator), ns_from_origin); |
| 1343 | } |
| 1344 | |
| 1345 | bt_bool bt_port_output_message_iterator_can_seek_beginning( |
| 1346 | struct bt_port_output_message_iterator *iterator) |
| 1347 | { |
| 1348 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); |
| 1349 | return bt_self_component_port_input_message_iterator_can_seek_beginning( |
| 1350 | borrow_output_port_message_iterator_upstream_iterator( |
| 1351 | iterator)); |
| 1352 | } |
| 1353 | |
| 1354 | enum bt_message_iterator_status bt_port_output_message_iterator_seek_ns_from_origin( |
| 1355 | struct bt_port_output_message_iterator *iterator, |
| 1356 | int64_t ns_from_origin) |
| 1357 | { |
| 1358 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); |
| 1359 | return bt_self_component_port_input_message_iterator_seek_ns_from_origin( |
| 1360 | borrow_output_port_message_iterator_upstream_iterator(iterator), |
| 1361 | ns_from_origin); |
| 1362 | } |
| 1363 | |
| 1364 | enum bt_message_iterator_status bt_port_output_message_iterator_seek_beginning( |
| 1365 | struct bt_port_output_message_iterator *iterator) |
| 1366 | { |
| 1367 | BT_ASSERT_PRE_NON_NULL(iterator, "Message iterator"); |
| 1368 | return bt_self_component_port_input_message_iterator_seek_beginning( |
| 1369 | borrow_output_port_message_iterator_upstream_iterator( |
| 1370 | iterator)); |
| 1371 | } |
| 1372 | |
| 1373 | void bt_port_output_message_iterator_get_ref( |
| 1374 | const struct bt_port_output_message_iterator *iterator) |
| 1375 | { |
| 1376 | bt_object_get_ref(iterator); |
| 1377 | } |
| 1378 | |
| 1379 | void bt_port_output_message_iterator_put_ref( |
| 1380 | const struct bt_port_output_message_iterator *iterator) |
| 1381 | { |
| 1382 | bt_object_put_ref(iterator); |
| 1383 | } |
| 1384 | |
| 1385 | void bt_self_component_port_input_message_iterator_get_ref( |
| 1386 | const struct bt_self_component_port_input_message_iterator *iterator) |
| 1387 | { |
| 1388 | bt_object_get_ref(iterator); |
| 1389 | } |
| 1390 | |
| 1391 | void bt_self_component_port_input_message_iterator_put_ref( |
| 1392 | const struct bt_self_component_port_input_message_iterator *iterator) |
| 1393 | { |
| 1394 | bt_object_put_ref(iterator); |
| 1395 | } |