| 1 | /* |
| 2 | * Copyright 2017 Philippe Proulx <pproulx@efficios.com> |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | * of this software and associated documentation files (the "Software"), to deal |
| 6 | * in the Software without restriction, including without limitation the rights |
| 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | * copies of the Software, and to permit persons to whom the Software is |
| 9 | * furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice shall be included in |
| 12 | * all copies or substantial portions of the Software. |
| 13 | * |
| 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | * SOFTWARE. |
| 21 | */ |
| 22 | |
| 23 | #define BT_LOG_TAG "PLUGIN-UTILS-MUXER-FLT" |
| 24 | #include "logging.h" |
| 25 | |
| 26 | #include <babeltrace/babeltrace-internal.h> |
| 27 | #include <babeltrace/compat/uuid-internal.h> |
| 28 | #include <babeltrace/babeltrace.h> |
| 29 | #include <babeltrace/value-internal.h> |
| 30 | #include <babeltrace/graph/component-internal.h> |
| 31 | #include <babeltrace/graph/message-iterator-internal.h> |
| 32 | #include <babeltrace/graph/connection-internal.h> |
| 33 | #include <plugins-common.h> |
| 34 | #include <glib.h> |
| 35 | #include <stdbool.h> |
| 36 | #include <inttypes.h> |
| 37 | #include <babeltrace/assert-internal.h> |
| 38 | #include <babeltrace/common-internal.h> |
| 39 | #include <stdlib.h> |
| 40 | #include <string.h> |
| 41 | |
| 42 | #include "muxer.h" |
| 43 | |
| 44 | #define ASSUME_ABSOLUTE_CLOCK_CLASSES_PARAM_NAME "assume-absolute-clock-classes" |
| 45 | |
| 46 | struct muxer_comp { |
| 47 | /* |
| 48 | * Array of struct |
| 49 | * bt_self_message_iterator * |
| 50 | * (weak refs) |
| 51 | */ |
| 52 | GPtrArray *muxer_msg_iters; |
| 53 | |
| 54 | /* Weak ref */ |
| 55 | bt_self_component_filter *self_comp; |
| 56 | |
| 57 | unsigned int next_port_num; |
| 58 | size_t available_input_ports; |
| 59 | bool initializing_muxer_msg_iter; |
| 60 | bool assume_absolute_clock_classes; |
| 61 | }; |
| 62 | |
| 63 | struct muxer_upstream_msg_iter { |
| 64 | /* Owned by this, NULL if ended */ |
| 65 | bt_self_component_port_input_message_iterator *msg_iter; |
| 66 | |
| 67 | /* Contains `const bt_message *`, owned by this */ |
| 68 | GQueue *msgs; |
| 69 | }; |
| 70 | |
| 71 | enum muxer_msg_iter_clock_class_expectation { |
| 72 | MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ANY = 0, |
| 73 | MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ABSOLUTE, |
| 74 | MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID, |
| 75 | MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_NO_UUID, |
| 76 | }; |
| 77 | |
| 78 | struct muxer_msg_iter { |
| 79 | /* |
| 80 | * Array of struct muxer_upstream_msg_iter * (owned by this). |
| 81 | * |
| 82 | * NOTE: This array is searched in linearly to find the youngest |
| 83 | * current message. Keep this until benchmarks confirm that |
| 84 | * another data structure is faster than this for our typical |
| 85 | * use cases. |
| 86 | */ |
| 87 | GPtrArray *muxer_upstream_msg_iters; |
| 88 | |
| 89 | /* |
| 90 | * List of "recently" connected input ports (weak) to |
| 91 | * handle by this muxer message iterator. |
| 92 | * muxer_port_connected() adds entries to this list, and the |
| 93 | * entries are removed when a message iterator is created |
| 94 | * on the port's connection and put into |
| 95 | * muxer_upstream_msg_iters above by |
| 96 | * muxer_msg_iter_handle_newly_connected_ports(). |
| 97 | */ |
| 98 | GList *newly_connected_self_ports; |
| 99 | |
| 100 | /* Last time returned in a message */ |
| 101 | int64_t last_returned_ts_ns; |
| 102 | |
| 103 | /* Clock class expectation state */ |
| 104 | enum muxer_msg_iter_clock_class_expectation clock_class_expectation; |
| 105 | |
| 106 | /* |
| 107 | * Expected clock class UUID, only valid when |
| 108 | * clock_class_expectation is |
| 109 | * MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID. |
| 110 | */ |
| 111 | unsigned char expected_clock_class_uuid[BABELTRACE_UUID_LEN]; |
| 112 | }; |
| 113 | |
| 114 | static |
| 115 | void destroy_muxer_upstream_msg_iter( |
| 116 | struct muxer_upstream_msg_iter *muxer_upstream_msg_iter) |
| 117 | { |
| 118 | if (!muxer_upstream_msg_iter) { |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | BT_LOGD("Destroying muxer's upstream message iterator wrapper: " |
| 123 | "addr=%p, msg-iter-addr=%p, queue-len=%u", |
| 124 | muxer_upstream_msg_iter, |
| 125 | muxer_upstream_msg_iter->msg_iter, |
| 126 | muxer_upstream_msg_iter->msgs->length); |
| 127 | bt_self_component_port_input_message_iterator_put_ref(muxer_upstream_msg_iter->msg_iter); |
| 128 | |
| 129 | if (muxer_upstream_msg_iter->msgs) { |
| 130 | const bt_message *msg; |
| 131 | |
| 132 | while ((msg = g_queue_pop_head( |
| 133 | muxer_upstream_msg_iter->msgs))) { |
| 134 | bt_message_put_ref(msg); |
| 135 | } |
| 136 | |
| 137 | g_queue_free(muxer_upstream_msg_iter->msgs); |
| 138 | } |
| 139 | |
| 140 | g_free(muxer_upstream_msg_iter); |
| 141 | } |
| 142 | |
| 143 | static |
| 144 | struct muxer_upstream_msg_iter *muxer_msg_iter_add_upstream_msg_iter( |
| 145 | struct muxer_msg_iter *muxer_msg_iter, |
| 146 | bt_self_component_port_input_message_iterator *self_msg_iter) |
| 147 | { |
| 148 | struct muxer_upstream_msg_iter *muxer_upstream_msg_iter = |
| 149 | g_new0(struct muxer_upstream_msg_iter, 1); |
| 150 | |
| 151 | if (!muxer_upstream_msg_iter) { |
| 152 | BT_LOGE_STR("Failed to allocate one muxer's upstream message iterator wrapper."); |
| 153 | goto end; |
| 154 | } |
| 155 | |
| 156 | muxer_upstream_msg_iter->msg_iter = self_msg_iter; |
| 157 | bt_self_component_port_input_message_iterator_get_ref(muxer_upstream_msg_iter->msg_iter); |
| 158 | muxer_upstream_msg_iter->msgs = g_queue_new(); |
| 159 | if (!muxer_upstream_msg_iter->msgs) { |
| 160 | BT_LOGE_STR("Failed to allocate a GQueue."); |
| 161 | goto end; |
| 162 | } |
| 163 | |
| 164 | g_ptr_array_add(muxer_msg_iter->muxer_upstream_msg_iters, |
| 165 | muxer_upstream_msg_iter); |
| 166 | BT_LOGD("Added muxer's upstream message iterator wrapper: " |
| 167 | "addr=%p, muxer-msg-iter-addr=%p, msg-iter-addr=%p", |
| 168 | muxer_upstream_msg_iter, muxer_msg_iter, |
| 169 | self_msg_iter); |
| 170 | |
| 171 | end: |
| 172 | return muxer_upstream_msg_iter; |
| 173 | } |
| 174 | |
| 175 | static |
| 176 | bt_self_component_status ensure_available_input_port( |
| 177 | bt_self_component_filter *self_comp) |
| 178 | { |
| 179 | struct muxer_comp *muxer_comp = bt_self_component_get_data( |
| 180 | bt_self_component_filter_as_self_component(self_comp)); |
| 181 | bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK; |
| 182 | GString *port_name = NULL; |
| 183 | |
| 184 | BT_ASSERT(muxer_comp); |
| 185 | |
| 186 | if (muxer_comp->available_input_ports >= 1) { |
| 187 | goto end; |
| 188 | } |
| 189 | |
| 190 | port_name = g_string_new("in"); |
| 191 | if (!port_name) { |
| 192 | BT_LOGE_STR("Failed to allocate a GString."); |
| 193 | status = BT_SELF_COMPONENT_STATUS_NOMEM; |
| 194 | goto end; |
| 195 | } |
| 196 | |
| 197 | g_string_append_printf(port_name, "%u", muxer_comp->next_port_num); |
| 198 | status = bt_self_component_filter_add_input_port( |
| 199 | self_comp, port_name->str, NULL, NULL); |
| 200 | if (status != BT_SELF_COMPONENT_STATUS_OK) { |
| 201 | BT_LOGE("Cannot add input port to muxer component: " |
| 202 | "port-name=\"%s\", comp-addr=%p, status=%s", |
| 203 | port_name->str, self_comp, |
| 204 | bt_self_component_status_string(status)); |
| 205 | goto end; |
| 206 | } |
| 207 | |
| 208 | muxer_comp->available_input_ports++; |
| 209 | muxer_comp->next_port_num++; |
| 210 | BT_LOGD("Added one input port to muxer component: " |
| 211 | "port-name=\"%s\", comp-addr=%p", |
| 212 | port_name->str, self_comp); |
| 213 | end: |
| 214 | if (port_name) { |
| 215 | g_string_free(port_name, TRUE); |
| 216 | } |
| 217 | |
| 218 | return status; |
| 219 | } |
| 220 | |
| 221 | static |
| 222 | bt_self_component_status create_output_port( |
| 223 | bt_self_component_filter *self_comp) |
| 224 | { |
| 225 | return bt_self_component_filter_add_output_port( |
| 226 | self_comp, "out", NULL, NULL); |
| 227 | } |
| 228 | |
| 229 | static |
| 230 | void destroy_muxer_comp(struct muxer_comp *muxer_comp) |
| 231 | { |
| 232 | if (!muxer_comp) { |
| 233 | return; |
| 234 | } |
| 235 | |
| 236 | BT_LOGD("Destroying muxer component: muxer-comp-addr=%p, " |
| 237 | "muxer-msg-iter-count=%u", muxer_comp, |
| 238 | muxer_comp->muxer_msg_iters ? |
| 239 | muxer_comp->muxer_msg_iters->len : 0); |
| 240 | |
| 241 | if (muxer_comp->muxer_msg_iters) { |
| 242 | g_ptr_array_free(muxer_comp->muxer_msg_iters, TRUE); |
| 243 | } |
| 244 | |
| 245 | g_free(muxer_comp); |
| 246 | } |
| 247 | |
| 248 | static |
| 249 | bt_value *get_default_params(void) |
| 250 | { |
| 251 | bt_value *params; |
| 252 | int ret; |
| 253 | |
| 254 | params = bt_value_map_create(); |
| 255 | if (!params) { |
| 256 | BT_LOGE_STR("Cannot create a map value object."); |
| 257 | goto error; |
| 258 | } |
| 259 | |
| 260 | ret = bt_value_map_insert_bool_entry(params, |
| 261 | ASSUME_ABSOLUTE_CLOCK_CLASSES_PARAM_NAME, false); |
| 262 | if (ret) { |
| 263 | BT_LOGE_STR("Cannot add boolean value to map value object."); |
| 264 | goto error; |
| 265 | } |
| 266 | |
| 267 | goto end; |
| 268 | |
| 269 | error: |
| 270 | BT_VALUE_PUT_REF_AND_RESET(params); |
| 271 | |
| 272 | end: |
| 273 | return params; |
| 274 | } |
| 275 | |
| 276 | static |
| 277 | int configure_muxer_comp(struct muxer_comp *muxer_comp, |
| 278 | const bt_value *params) |
| 279 | { |
| 280 | bt_value *default_params = NULL; |
| 281 | bt_value *real_params = NULL; |
| 282 | const bt_value *assume_absolute_clock_classes = NULL; |
| 283 | int ret = 0; |
| 284 | bt_bool bool_val; |
| 285 | |
| 286 | default_params = get_default_params(); |
| 287 | if (!default_params) { |
| 288 | BT_LOGE("Cannot get default parameters: " |
| 289 | "muxer-comp-addr=%p", muxer_comp); |
| 290 | goto error; |
| 291 | } |
| 292 | |
| 293 | ret = bt_value_map_extend(default_params, params, &real_params); |
| 294 | if (ret) { |
| 295 | BT_LOGE("Cannot extend default parameters map value: " |
| 296 | "muxer-comp-addr=%p, def-params-addr=%p, " |
| 297 | "params-addr=%p", muxer_comp, default_params, |
| 298 | params); |
| 299 | goto error; |
| 300 | } |
| 301 | |
| 302 | assume_absolute_clock_classes = bt_value_map_borrow_entry_value(real_params, |
| 303 | ASSUME_ABSOLUTE_CLOCK_CLASSES_PARAM_NAME); |
| 304 | if (assume_absolute_clock_classes && |
| 305 | !bt_value_is_bool(assume_absolute_clock_classes)) { |
| 306 | BT_LOGE("Expecting a boolean value for the `%s` parameter: " |
| 307 | "muxer-comp-addr=%p, value-type=%s", |
| 308 | ASSUME_ABSOLUTE_CLOCK_CLASSES_PARAM_NAME, muxer_comp, |
| 309 | bt_common_value_type_string( |
| 310 | bt_value_get_type(assume_absolute_clock_classes))); |
| 311 | goto error; |
| 312 | } |
| 313 | |
| 314 | bool_val = bt_value_bool_get(assume_absolute_clock_classes); |
| 315 | muxer_comp->assume_absolute_clock_classes = (bool) bool_val; |
| 316 | BT_LOGD("Configured muxer component: muxer-comp-addr=%p, " |
| 317 | "assume-absolute-clock-classes=%d", |
| 318 | muxer_comp, muxer_comp->assume_absolute_clock_classes); |
| 319 | goto end; |
| 320 | |
| 321 | error: |
| 322 | ret = -1; |
| 323 | |
| 324 | end: |
| 325 | bt_value_put_ref(default_params); |
| 326 | bt_value_put_ref(real_params); |
| 327 | return ret; |
| 328 | } |
| 329 | |
| 330 | BT_HIDDEN |
| 331 | bt_self_component_status muxer_init( |
| 332 | bt_self_component_filter *self_comp, |
| 333 | const bt_value *params, void *init_data) |
| 334 | |
| 335 | |
| 336 | { |
| 337 | int ret; |
| 338 | bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK; |
| 339 | struct muxer_comp *muxer_comp = g_new0(struct muxer_comp, 1); |
| 340 | |
| 341 | BT_LOGD("Initializing muxer component: " |
| 342 | "comp-addr=%p, params-addr=%p", self_comp, params); |
| 343 | |
| 344 | if (!muxer_comp) { |
| 345 | BT_LOGE_STR("Failed to allocate one muxer component."); |
| 346 | goto error; |
| 347 | } |
| 348 | |
| 349 | ret = configure_muxer_comp(muxer_comp, params); |
| 350 | if (ret) { |
| 351 | BT_LOGE("Cannot configure muxer component: " |
| 352 | "muxer-comp-addr=%p, params-addr=%p", |
| 353 | muxer_comp, params); |
| 354 | goto error; |
| 355 | } |
| 356 | |
| 357 | muxer_comp->muxer_msg_iters = g_ptr_array_new(); |
| 358 | if (!muxer_comp->muxer_msg_iters) { |
| 359 | BT_LOGE_STR("Failed to allocate a GPtrArray."); |
| 360 | goto error; |
| 361 | } |
| 362 | |
| 363 | muxer_comp->self_comp = self_comp; |
| 364 | bt_self_component_set_data( |
| 365 | bt_self_component_filter_as_self_component(self_comp), |
| 366 | muxer_comp); |
| 367 | status = ensure_available_input_port(self_comp); |
| 368 | if (status != BT_SELF_COMPONENT_STATUS_OK) { |
| 369 | BT_LOGE("Cannot ensure that at least one muxer component's input port is available: " |
| 370 | "muxer-comp-addr=%p, status=%s", |
| 371 | muxer_comp, |
| 372 | bt_self_component_status_string(status)); |
| 373 | goto error; |
| 374 | } |
| 375 | |
| 376 | status = create_output_port(self_comp); |
| 377 | if (status) { |
| 378 | BT_LOGE("Cannot create muxer component's output port: " |
| 379 | "muxer-comp-addr=%p, status=%s", |
| 380 | muxer_comp, |
| 381 | bt_self_component_status_string(status)); |
| 382 | goto error; |
| 383 | } |
| 384 | |
| 385 | BT_LOGD("Initialized muxer component: " |
| 386 | "comp-addr=%p, params-addr=%p, muxer-comp-addr=%p", |
| 387 | self_comp, params, muxer_comp); |
| 388 | |
| 389 | goto end; |
| 390 | |
| 391 | error: |
| 392 | destroy_muxer_comp(muxer_comp); |
| 393 | bt_self_component_set_data( |
| 394 | bt_self_component_filter_as_self_component(self_comp), |
| 395 | NULL); |
| 396 | |
| 397 | if (status == BT_SELF_COMPONENT_STATUS_OK) { |
| 398 | status = BT_SELF_COMPONENT_STATUS_ERROR; |
| 399 | } |
| 400 | |
| 401 | end: |
| 402 | return status; |
| 403 | } |
| 404 | |
| 405 | BT_HIDDEN |
| 406 | void muxer_finalize(bt_self_component_filter *self_comp) |
| 407 | { |
| 408 | struct muxer_comp *muxer_comp = bt_self_component_get_data( |
| 409 | bt_self_component_filter_as_self_component(self_comp)); |
| 410 | |
| 411 | BT_LOGD("Finalizing muxer component: comp-addr=%p", |
| 412 | self_comp); |
| 413 | destroy_muxer_comp(muxer_comp); |
| 414 | } |
| 415 | |
| 416 | static |
| 417 | bt_self_component_port_input_message_iterator * |
| 418 | create_msg_iter_on_input_port( |
| 419 | bt_self_component_port_input *self_port, int *ret) |
| 420 | { |
| 421 | const bt_port *port = bt_self_component_port_as_port( |
| 422 | bt_self_component_port_input_as_self_component_port( |
| 423 | self_port)); |
| 424 | bt_self_component_port_input_message_iterator *msg_iter = |
| 425 | NULL; |
| 426 | |
| 427 | BT_ASSERT(ret); |
| 428 | *ret = 0; |
| 429 | BT_ASSERT(port); |
| 430 | BT_ASSERT(bt_port_is_connected(port)); |
| 431 | |
| 432 | // TODO: Advance the iterator to >= the time of the latest |
| 433 | // returned message by the muxer message |
| 434 | // iterator which creates it. |
| 435 | msg_iter = bt_self_component_port_input_message_iterator_create( |
| 436 | self_port); |
| 437 | if (!msg_iter) { |
| 438 | BT_LOGE("Cannot create upstream message iterator on input port: " |
| 439 | "port-addr=%p, port-name=\"%s\"", |
| 440 | port, bt_port_get_name(port)); |
| 441 | *ret = -1; |
| 442 | goto end; |
| 443 | } |
| 444 | |
| 445 | BT_LOGD("Created upstream message iterator on input port: " |
| 446 | "port-addr=%p, port-name=\"%s\", msg-iter-addr=%p", |
| 447 | port, bt_port_get_name(port), msg_iter); |
| 448 | |
| 449 | end: |
| 450 | return msg_iter; |
| 451 | } |
| 452 | |
| 453 | static |
| 454 | bt_self_message_iterator_status muxer_upstream_msg_iter_next( |
| 455 | struct muxer_upstream_msg_iter *muxer_upstream_msg_iter) |
| 456 | { |
| 457 | bt_self_message_iterator_status status; |
| 458 | bt_message_iterator_status input_port_iter_status; |
| 459 | bt_message_array_const msgs; |
| 460 | uint64_t i; |
| 461 | uint64_t count; |
| 462 | |
| 463 | BT_LOGV("Calling upstream message iterator's \"next\" method: " |
| 464 | "muxer-upstream-msg-iter-wrap-addr=%p, msg-iter-addr=%p", |
| 465 | muxer_upstream_msg_iter, |
| 466 | muxer_upstream_msg_iter->msg_iter); |
| 467 | input_port_iter_status = bt_self_component_port_input_message_iterator_next( |
| 468 | muxer_upstream_msg_iter->msg_iter, &msgs, &count); |
| 469 | BT_LOGV("Upstream message iterator's \"next\" method returned: " |
| 470 | "status=%s", bt_message_iterator_status_string(input_port_iter_status)); |
| 471 | |
| 472 | switch (input_port_iter_status) { |
| 473 | case BT_MESSAGE_ITERATOR_STATUS_OK: |
| 474 | /* |
| 475 | * Message iterator's current message is |
| 476 | * valid: it must be considered for muxing operations. |
| 477 | */ |
| 478 | BT_LOGV_STR("Validated upstream message iterator wrapper."); |
| 479 | BT_ASSERT(count > 0); |
| 480 | |
| 481 | /* Move messages to our queue */ |
| 482 | for (i = 0; i < count; i++) { |
| 483 | /* |
| 484 | * Push to tail in order; other side |
| 485 | * (muxer_msg_iter_do_next_one()) consumes |
| 486 | * from the head first. |
| 487 | */ |
| 488 | g_queue_push_tail(muxer_upstream_msg_iter->msgs, |
| 489 | (void *) msgs[i]); |
| 490 | } |
| 491 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_OK; |
| 492 | break; |
| 493 | case BT_MESSAGE_ITERATOR_STATUS_AGAIN: |
| 494 | /* |
| 495 | * Message iterator's current message is not |
| 496 | * valid anymore. Return |
| 497 | * BT_MESSAGE_ITERATOR_STATUS_AGAIN immediately. |
| 498 | */ |
| 499 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_AGAIN; |
| 500 | break; |
| 501 | case BT_MESSAGE_ITERATOR_STATUS_END: /* Fall-through. */ |
| 502 | /* |
| 503 | * Message iterator reached the end: release it. It |
| 504 | * won't be considered again to find the youngest |
| 505 | * message. |
| 506 | */ |
| 507 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_PUT_REF_AND_RESET(muxer_upstream_msg_iter->msg_iter); |
| 508 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_OK; |
| 509 | break; |
| 510 | default: |
| 511 | /* Error or unsupported status code */ |
| 512 | BT_LOGE("Error or unsupported status code: " |
| 513 | "status-code=%d", input_port_iter_status); |
| 514 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR; |
| 515 | break; |
| 516 | } |
| 517 | |
| 518 | return status; |
| 519 | } |
| 520 | |
| 521 | static |
| 522 | int muxer_msg_iter_handle_newly_connected_ports( |
| 523 | struct muxer_msg_iter *muxer_msg_iter) |
| 524 | { |
| 525 | int ret = 0; |
| 526 | |
| 527 | BT_LOGV("Handling newly connected ports: " |
| 528 | "muxer-msg-iter-addr=%p", muxer_msg_iter); |
| 529 | |
| 530 | /* |
| 531 | * Here we create one upstream message iterator for each |
| 532 | * newly connected port. We do NOT perform an initial "next" on |
| 533 | * those new upstream message iterators: they are |
| 534 | * invalidated, to be validated later. The list of newly |
| 535 | * connected ports to handle here is updated by |
| 536 | * muxer_port_connected(). |
| 537 | */ |
| 538 | while (true) { |
| 539 | GList *node = muxer_msg_iter->newly_connected_self_ports; |
| 540 | bt_self_component_port_input *self_port; |
| 541 | const bt_port *port; |
| 542 | bt_self_component_port_input_message_iterator * |
| 543 | upstream_msg_iter = NULL; |
| 544 | struct muxer_upstream_msg_iter *muxer_upstream_msg_iter; |
| 545 | |
| 546 | if (!node) { |
| 547 | break; |
| 548 | } |
| 549 | |
| 550 | self_port = node->data; |
| 551 | port = bt_self_component_port_as_port( |
| 552 | bt_self_component_port_input_as_self_component_port( |
| 553 | (self_port))); |
| 554 | BT_ASSERT(port); |
| 555 | |
| 556 | if (!bt_port_is_connected(port)) { |
| 557 | /* |
| 558 | * Looks like this port is not connected |
| 559 | * anymore: we can't create an upstream |
| 560 | * message iterator on its (non-existing) |
| 561 | * connection in this case. |
| 562 | */ |
| 563 | goto remove_node; |
| 564 | } |
| 565 | |
| 566 | upstream_msg_iter = create_msg_iter_on_input_port( |
| 567 | self_port, &ret); |
| 568 | if (ret) { |
| 569 | /* create_msg_iter_on_input_port() logs errors */ |
| 570 | BT_ASSERT(!upstream_msg_iter); |
| 571 | goto error; |
| 572 | } |
| 573 | |
| 574 | muxer_upstream_msg_iter = |
| 575 | muxer_msg_iter_add_upstream_msg_iter( |
| 576 | muxer_msg_iter, upstream_msg_iter); |
| 577 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_PUT_REF_AND_RESET(upstream_msg_iter); |
| 578 | if (!muxer_upstream_msg_iter) { |
| 579 | /* |
| 580 | * muxer_msg_iter_add_upstream_msg_iter() |
| 581 | * logs errors. |
| 582 | */ |
| 583 | goto error; |
| 584 | } |
| 585 | |
| 586 | remove_node: |
| 587 | bt_self_component_port_input_message_iterator_put_ref(upstream_msg_iter); |
| 588 | muxer_msg_iter->newly_connected_self_ports = |
| 589 | g_list_delete_link( |
| 590 | muxer_msg_iter->newly_connected_self_ports, |
| 591 | node); |
| 592 | } |
| 593 | |
| 594 | goto end; |
| 595 | |
| 596 | error: |
| 597 | if (ret >= 0) { |
| 598 | ret = -1; |
| 599 | } |
| 600 | |
| 601 | end: |
| 602 | return ret; |
| 603 | } |
| 604 | |
| 605 | static |
| 606 | int get_msg_ts_ns(struct muxer_comp *muxer_comp, |
| 607 | struct muxer_msg_iter *muxer_msg_iter, |
| 608 | const bt_message *msg, int64_t last_returned_ts_ns, |
| 609 | int64_t *ts_ns) |
| 610 | { |
| 611 | const bt_clock_class *clock_class = NULL; |
| 612 | const bt_clock_snapshot *clock_snapshot = NULL; |
| 613 | int ret = 0; |
| 614 | const unsigned char *cc_uuid; |
| 615 | const char *cc_name; |
| 616 | bt_clock_snapshot_state cs_state = BT_CLOCK_SNAPSHOT_STATE_KNOWN; |
| 617 | bt_message_stream_activity_clock_snapshot_state sa_cs_state; |
| 618 | |
| 619 | BT_ASSERT(msg); |
| 620 | BT_ASSERT(ts_ns); |
| 621 | |
| 622 | BT_LOGV("Getting message's timestamp: " |
| 623 | "muxer-msg-iter-addr=%p, msg-addr=%p, " |
| 624 | "last-returned-ts=%" PRId64, |
| 625 | muxer_msg_iter, msg, last_returned_ts_ns); |
| 626 | |
| 627 | switch (bt_message_get_type(msg)) { |
| 628 | case BT_MESSAGE_TYPE_EVENT: |
| 629 | clock_class = |
| 630 | bt_message_event_borrow_stream_class_default_clock_class_const( |
| 631 | msg); |
| 632 | if (!clock_class) { |
| 633 | goto no_clock_snapshot; |
| 634 | } |
| 635 | |
| 636 | cs_state = bt_message_event_borrow_default_clock_snapshot_const( |
| 637 | msg, &clock_snapshot); |
| 638 | break; |
| 639 | case BT_MESSAGE_TYPE_PACKET_BEGINNING: |
| 640 | bt_message_packet_beginning_borrow_stream_class_default_clock_class_const( |
| 641 | msg); |
| 642 | if (!clock_class) { |
| 643 | goto no_clock_snapshot; |
| 644 | } |
| 645 | |
| 646 | cs_state = bt_message_packet_beginning_borrow_default_clock_snapshot_const( |
| 647 | msg, &clock_snapshot); |
| 648 | break; |
| 649 | case BT_MESSAGE_TYPE_PACKET_END: |
| 650 | bt_message_packet_end_borrow_stream_class_default_clock_class_const( |
| 651 | msg); |
| 652 | if (!clock_class) { |
| 653 | goto no_clock_snapshot; |
| 654 | } |
| 655 | |
| 656 | cs_state = bt_message_packet_end_borrow_default_clock_snapshot_const( |
| 657 | msg, &clock_snapshot); |
| 658 | break; |
| 659 | case BT_MESSAGE_TYPE_DISCARDED_EVENTS: |
| 660 | bt_message_discarded_events_borrow_stream_class_default_clock_class_const( |
| 661 | msg); |
| 662 | if (!clock_class) { |
| 663 | goto no_clock_snapshot; |
| 664 | } |
| 665 | |
| 666 | cs_state = bt_message_discarded_events_borrow_default_beginning_clock_snapshot_const( |
| 667 | msg, &clock_snapshot); |
| 668 | break; |
| 669 | case BT_MESSAGE_TYPE_DISCARDED_PACKETS: |
| 670 | bt_message_discarded_packets_borrow_stream_class_default_clock_class_const( |
| 671 | msg); |
| 672 | if (!clock_class) { |
| 673 | goto no_clock_snapshot; |
| 674 | } |
| 675 | |
| 676 | cs_state = bt_message_discarded_packets_borrow_default_beginning_clock_snapshot_const( |
| 677 | msg, &clock_snapshot); |
| 678 | break; |
| 679 | case BT_MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING: |
| 680 | bt_message_stream_activity_beginning_borrow_stream_class_default_clock_class_const( |
| 681 | msg); |
| 682 | if (!clock_class) { |
| 683 | goto no_clock_snapshot; |
| 684 | } |
| 685 | |
| 686 | sa_cs_state = bt_message_stream_activity_beginning_borrow_default_clock_snapshot_const( |
| 687 | msg, &clock_snapshot); |
| 688 | if (sa_cs_state != BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_KNOWN) { |
| 689 | goto no_clock_snapshot; |
| 690 | } |
| 691 | |
| 692 | break; |
| 693 | case BT_MESSAGE_TYPE_STREAM_ACTIVITY_END: |
| 694 | bt_message_stream_activity_end_borrow_stream_class_default_clock_class_const( |
| 695 | msg); |
| 696 | if (!clock_class) { |
| 697 | goto no_clock_snapshot; |
| 698 | } |
| 699 | |
| 700 | sa_cs_state = bt_message_stream_activity_end_borrow_default_clock_snapshot_const( |
| 701 | msg, &clock_snapshot); |
| 702 | if (sa_cs_state != BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_KNOWN) { |
| 703 | goto no_clock_snapshot; |
| 704 | } |
| 705 | |
| 706 | break; |
| 707 | case BT_MESSAGE_TYPE_INACTIVITY: |
| 708 | cs_state = |
| 709 | bt_message_inactivity_borrow_default_clock_snapshot_const( |
| 710 | msg, &clock_snapshot); |
| 711 | break; |
| 712 | default: |
| 713 | /* All the other messages have a higher priority */ |
| 714 | BT_LOGV_STR("Message has no timestamp: using the last returned timestamp."); |
| 715 | *ts_ns = last_returned_ts_ns; |
| 716 | goto end; |
| 717 | } |
| 718 | |
| 719 | if (cs_state != BT_CLOCK_SNAPSHOT_STATE_KNOWN) { |
| 720 | BT_LOGE_STR("Unsupported unknown clock snapshot."); |
| 721 | ret = -1; |
| 722 | goto end; |
| 723 | } |
| 724 | |
| 725 | clock_class = bt_clock_snapshot_borrow_clock_class_const(clock_snapshot); |
| 726 | BT_ASSERT(clock_class); |
| 727 | cc_uuid = bt_clock_class_get_uuid(clock_class); |
| 728 | cc_name = bt_clock_class_get_name(clock_class); |
| 729 | |
| 730 | if (muxer_msg_iter->clock_class_expectation == |
| 731 | MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ANY) { |
| 732 | /* |
| 733 | * This is the first clock class that this muxer |
| 734 | * message iterator encounters. Its properties |
| 735 | * determine what to expect for the whole lifetime of |
| 736 | * the iterator without a true |
| 737 | * `assume-absolute-clock-classes` parameter. |
| 738 | */ |
| 739 | if (bt_clock_class_is_absolute(clock_class)) { |
| 740 | /* Expect absolute clock classes */ |
| 741 | muxer_msg_iter->clock_class_expectation = |
| 742 | MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ABSOLUTE; |
| 743 | } else { |
| 744 | if (cc_uuid) { |
| 745 | /* |
| 746 | * Expect non-absolute clock classes |
| 747 | * with a specific UUID. |
| 748 | */ |
| 749 | muxer_msg_iter->clock_class_expectation = |
| 750 | MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID; |
| 751 | memcpy(muxer_msg_iter->expected_clock_class_uuid, |
| 752 | cc_uuid, BABELTRACE_UUID_LEN); |
| 753 | } else { |
| 754 | /* |
| 755 | * Expect non-absolute clock classes |
| 756 | * with no UUID. |
| 757 | */ |
| 758 | muxer_msg_iter->clock_class_expectation = |
| 759 | MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_NO_UUID; |
| 760 | } |
| 761 | } |
| 762 | } |
| 763 | |
| 764 | if (!muxer_comp->assume_absolute_clock_classes) { |
| 765 | switch (muxer_msg_iter->clock_class_expectation) { |
| 766 | case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_ABSOLUTE: |
| 767 | if (!bt_clock_class_is_absolute(clock_class)) { |
| 768 | BT_LOGE("Expecting an absolute clock class, " |
| 769 | "but got a non-absolute one: " |
| 770 | "clock-class-addr=%p, clock-class-name=\"%s\"", |
| 771 | clock_class, cc_name); |
| 772 | goto error; |
| 773 | } |
| 774 | break; |
| 775 | case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_NO_UUID: |
| 776 | if (bt_clock_class_is_absolute(clock_class)) { |
| 777 | BT_LOGE("Expecting a non-absolute clock class with no UUID, " |
| 778 | "but got an absolute one: " |
| 779 | "clock-class-addr=%p, clock-class-name=\"%s\"", |
| 780 | clock_class, cc_name); |
| 781 | goto error; |
| 782 | } |
| 783 | |
| 784 | if (cc_uuid) { |
| 785 | BT_LOGE("Expecting a non-absolute clock class with no UUID, " |
| 786 | "but got one with a UUID: " |
| 787 | "clock-class-addr=%p, clock-class-name=\"%s\", " |
| 788 | "uuid=\"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\"", |
| 789 | clock_class, cc_name, |
| 790 | (unsigned int) cc_uuid[0], |
| 791 | (unsigned int) cc_uuid[1], |
| 792 | (unsigned int) cc_uuid[2], |
| 793 | (unsigned int) cc_uuid[3], |
| 794 | (unsigned int) cc_uuid[4], |
| 795 | (unsigned int) cc_uuid[5], |
| 796 | (unsigned int) cc_uuid[6], |
| 797 | (unsigned int) cc_uuid[7], |
| 798 | (unsigned int) cc_uuid[8], |
| 799 | (unsigned int) cc_uuid[9], |
| 800 | (unsigned int) cc_uuid[10], |
| 801 | (unsigned int) cc_uuid[11], |
| 802 | (unsigned int) cc_uuid[12], |
| 803 | (unsigned int) cc_uuid[13], |
| 804 | (unsigned int) cc_uuid[14], |
| 805 | (unsigned int) cc_uuid[15]); |
| 806 | goto error; |
| 807 | } |
| 808 | break; |
| 809 | case MUXER_MSG_ITER_CLOCK_CLASS_EXPECTATION_NOT_ABS_SPEC_UUID: |
| 810 | if (bt_clock_class_is_absolute(clock_class)) { |
| 811 | BT_LOGE("Expecting a non-absolute clock class with a specific UUID, " |
| 812 | "but got an absolute one: " |
| 813 | "clock-class-addr=%p, clock-class-name=\"%s\"", |
| 814 | clock_class, cc_name); |
| 815 | goto error; |
| 816 | } |
| 817 | |
| 818 | if (!cc_uuid) { |
| 819 | BT_LOGE("Expecting a non-absolute clock class with a specific UUID, " |
| 820 | "but got one with no UUID: " |
| 821 | "clock-class-addr=%p, clock-class-name=\"%s\"", |
| 822 | clock_class, cc_name); |
| 823 | goto error; |
| 824 | } |
| 825 | |
| 826 | if (memcmp(muxer_msg_iter->expected_clock_class_uuid, |
| 827 | cc_uuid, BABELTRACE_UUID_LEN) != 0) { |
| 828 | BT_LOGE("Expecting a non-absolute clock class with a specific UUID, " |
| 829 | "but got one with different UUID: " |
| 830 | "clock-class-addr=%p, clock-class-name=\"%s\", " |
| 831 | "expected-uuid=\"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\", " |
| 832 | "uuid=\"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\"", |
| 833 | clock_class, cc_name, |
| 834 | (unsigned int) muxer_msg_iter->expected_clock_class_uuid[0], |
| 835 | (unsigned int) muxer_msg_iter->expected_clock_class_uuid[1], |
| 836 | (unsigned int) muxer_msg_iter->expected_clock_class_uuid[2], |
| 837 | (unsigned int) muxer_msg_iter->expected_clock_class_uuid[3], |
| 838 | (unsigned int) muxer_msg_iter->expected_clock_class_uuid[4], |
| 839 | (unsigned int) muxer_msg_iter->expected_clock_class_uuid[5], |
| 840 | (unsigned int) muxer_msg_iter->expected_clock_class_uuid[6], |
| 841 | (unsigned int) muxer_msg_iter->expected_clock_class_uuid[7], |
| 842 | (unsigned int) muxer_msg_iter->expected_clock_class_uuid[8], |
| 843 | (unsigned int) muxer_msg_iter->expected_clock_class_uuid[9], |
| 844 | (unsigned int) muxer_msg_iter->expected_clock_class_uuid[10], |
| 845 | (unsigned int) muxer_msg_iter->expected_clock_class_uuid[11], |
| 846 | (unsigned int) muxer_msg_iter->expected_clock_class_uuid[12], |
| 847 | (unsigned int) muxer_msg_iter->expected_clock_class_uuid[13], |
| 848 | (unsigned int) muxer_msg_iter->expected_clock_class_uuid[14], |
| 849 | (unsigned int) muxer_msg_iter->expected_clock_class_uuid[15], |
| 850 | (unsigned int) cc_uuid[0], |
| 851 | (unsigned int) cc_uuid[1], |
| 852 | (unsigned int) cc_uuid[2], |
| 853 | (unsigned int) cc_uuid[3], |
| 854 | (unsigned int) cc_uuid[4], |
| 855 | (unsigned int) cc_uuid[5], |
| 856 | (unsigned int) cc_uuid[6], |
| 857 | (unsigned int) cc_uuid[7], |
| 858 | (unsigned int) cc_uuid[8], |
| 859 | (unsigned int) cc_uuid[9], |
| 860 | (unsigned int) cc_uuid[10], |
| 861 | (unsigned int) cc_uuid[11], |
| 862 | (unsigned int) cc_uuid[12], |
| 863 | (unsigned int) cc_uuid[13], |
| 864 | (unsigned int) cc_uuid[14], |
| 865 | (unsigned int) cc_uuid[15]); |
| 866 | goto error; |
| 867 | } |
| 868 | break; |
| 869 | default: |
| 870 | /* Unexpected */ |
| 871 | BT_LOGF("Unexpected clock class expectation: " |
| 872 | "expectation-code=%d", |
| 873 | muxer_msg_iter->clock_class_expectation); |
| 874 | abort(); |
| 875 | } |
| 876 | } |
| 877 | |
| 878 | ret = bt_clock_snapshot_get_ns_from_origin(clock_snapshot, ts_ns); |
| 879 | if (ret) { |
| 880 | BT_LOGE("Cannot get nanoseconds from Epoch of clock snapshot: " |
| 881 | "clock-snapshot-addr=%p", clock_snapshot); |
| 882 | goto error; |
| 883 | } |
| 884 | |
| 885 | goto end; |
| 886 | |
| 887 | no_clock_snapshot: |
| 888 | BT_LOGV_STR("Message's default clock snapshot is missing: " |
| 889 | "using the last returned timestamp."); |
| 890 | *ts_ns = last_returned_ts_ns; |
| 891 | goto end; |
| 892 | |
| 893 | error: |
| 894 | ret = -1; |
| 895 | |
| 896 | end: |
| 897 | if (ret == 0) { |
| 898 | BT_LOGV("Found message's timestamp: " |
| 899 | "muxer-msg-iter-addr=%p, msg-addr=%p, " |
| 900 | "last-returned-ts=%" PRId64 ", ts=%" PRId64, |
| 901 | muxer_msg_iter, msg, last_returned_ts_ns, |
| 902 | *ts_ns); |
| 903 | } |
| 904 | |
| 905 | return ret; |
| 906 | } |
| 907 | |
| 908 | /* |
| 909 | * This function finds the youngest available message amongst the |
| 910 | * non-ended upstream message iterators and returns the upstream |
| 911 | * message iterator which has it, or |
| 912 | * BT_MESSAGE_ITERATOR_STATUS_END if there's no available |
| 913 | * message. |
| 914 | * |
| 915 | * This function does NOT: |
| 916 | * |
| 917 | * * Update any upstream message iterator. |
| 918 | * * Check for newly connected ports. |
| 919 | * * Check the upstream message iterators to retry. |
| 920 | * |
| 921 | * On sucess, this function sets *muxer_upstream_msg_iter to the |
| 922 | * upstream message iterator of which the current message is |
| 923 | * the youngest, and sets *ts_ns to its time. |
| 924 | */ |
| 925 | static |
| 926 | bt_self_message_iterator_status |
| 927 | muxer_msg_iter_youngest_upstream_msg_iter( |
| 928 | struct muxer_comp *muxer_comp, |
| 929 | struct muxer_msg_iter *muxer_msg_iter, |
| 930 | struct muxer_upstream_msg_iter **muxer_upstream_msg_iter, |
| 931 | int64_t *ts_ns) |
| 932 | { |
| 933 | size_t i; |
| 934 | int ret; |
| 935 | int64_t youngest_ts_ns = INT64_MAX; |
| 936 | bt_self_message_iterator_status status = |
| 937 | BT_SELF_MESSAGE_ITERATOR_STATUS_OK; |
| 938 | |
| 939 | BT_ASSERT(muxer_comp); |
| 940 | BT_ASSERT(muxer_msg_iter); |
| 941 | BT_ASSERT(muxer_upstream_msg_iter); |
| 942 | *muxer_upstream_msg_iter = NULL; |
| 943 | |
| 944 | for (i = 0; i < muxer_msg_iter->muxer_upstream_msg_iters->len; i++) { |
| 945 | const bt_message *msg; |
| 946 | struct muxer_upstream_msg_iter *cur_muxer_upstream_msg_iter = |
| 947 | g_ptr_array_index(muxer_msg_iter->muxer_upstream_msg_iters, i); |
| 948 | int64_t msg_ts_ns; |
| 949 | |
| 950 | if (!cur_muxer_upstream_msg_iter->msg_iter) { |
| 951 | /* This upstream message iterator is ended */ |
| 952 | BT_LOGV("Skipping ended upstream message iterator: " |
| 953 | "muxer-upstream-msg-iter-wrap-addr=%p", |
| 954 | cur_muxer_upstream_msg_iter); |
| 955 | continue; |
| 956 | } |
| 957 | |
| 958 | BT_ASSERT(cur_muxer_upstream_msg_iter->msgs->length > 0); |
| 959 | msg = g_queue_peek_head(cur_muxer_upstream_msg_iter->msgs); |
| 960 | BT_ASSERT(msg); |
| 961 | ret = get_msg_ts_ns(muxer_comp, muxer_msg_iter, msg, |
| 962 | muxer_msg_iter->last_returned_ts_ns, &msg_ts_ns); |
| 963 | if (ret) { |
| 964 | /* get_msg_ts_ns() logs errors */ |
| 965 | *muxer_upstream_msg_iter = NULL; |
| 966 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR; |
| 967 | goto end; |
| 968 | } |
| 969 | |
| 970 | if (msg_ts_ns <= youngest_ts_ns) { |
| 971 | *muxer_upstream_msg_iter = |
| 972 | cur_muxer_upstream_msg_iter; |
| 973 | youngest_ts_ns = msg_ts_ns; |
| 974 | *ts_ns = youngest_ts_ns; |
| 975 | } |
| 976 | } |
| 977 | |
| 978 | if (!*muxer_upstream_msg_iter) { |
| 979 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_END; |
| 980 | *ts_ns = INT64_MIN; |
| 981 | } |
| 982 | |
| 983 | end: |
| 984 | return status; |
| 985 | } |
| 986 | |
| 987 | static |
| 988 | bt_self_message_iterator_status validate_muxer_upstream_msg_iter( |
| 989 | struct muxer_upstream_msg_iter *muxer_upstream_msg_iter) |
| 990 | { |
| 991 | bt_self_message_iterator_status status = |
| 992 | BT_SELF_MESSAGE_ITERATOR_STATUS_OK; |
| 993 | |
| 994 | BT_LOGV("Validating muxer's upstream message iterator wrapper: " |
| 995 | "muxer-upstream-msg-iter-wrap-addr=%p", |
| 996 | muxer_upstream_msg_iter); |
| 997 | |
| 998 | if (muxer_upstream_msg_iter->msgs->length > 0 || |
| 999 | !muxer_upstream_msg_iter->msg_iter) { |
| 1000 | BT_LOGV("Already valid or not considered: " |
| 1001 | "queue-len=%u, upstream-msg-iter-addr=%p", |
| 1002 | muxer_upstream_msg_iter->msgs->length, |
| 1003 | muxer_upstream_msg_iter->msg_iter); |
| 1004 | goto end; |
| 1005 | } |
| 1006 | |
| 1007 | /* muxer_upstream_msg_iter_next() logs details/errors */ |
| 1008 | status = muxer_upstream_msg_iter_next(muxer_upstream_msg_iter); |
| 1009 | |
| 1010 | end: |
| 1011 | return status; |
| 1012 | } |
| 1013 | |
| 1014 | static |
| 1015 | bt_self_message_iterator_status validate_muxer_upstream_msg_iters( |
| 1016 | struct muxer_msg_iter *muxer_msg_iter) |
| 1017 | { |
| 1018 | bt_self_message_iterator_status status = |
| 1019 | BT_SELF_MESSAGE_ITERATOR_STATUS_OK; |
| 1020 | size_t i; |
| 1021 | |
| 1022 | BT_LOGV("Validating muxer's upstream message iterator wrappers: " |
| 1023 | "muxer-msg-iter-addr=%p", muxer_msg_iter); |
| 1024 | |
| 1025 | for (i = 0; i < muxer_msg_iter->muxer_upstream_msg_iters->len; i++) { |
| 1026 | struct muxer_upstream_msg_iter *muxer_upstream_msg_iter = |
| 1027 | g_ptr_array_index( |
| 1028 | muxer_msg_iter->muxer_upstream_msg_iters, |
| 1029 | i); |
| 1030 | |
| 1031 | status = validate_muxer_upstream_msg_iter( |
| 1032 | muxer_upstream_msg_iter); |
| 1033 | if (status != BT_SELF_MESSAGE_ITERATOR_STATUS_OK) { |
| 1034 | if (status < 0) { |
| 1035 | BT_LOGE("Cannot validate muxer's upstream message iterator wrapper: " |
| 1036 | "muxer-msg-iter-addr=%p, " |
| 1037 | "muxer-upstream-msg-iter-wrap-addr=%p", |
| 1038 | muxer_msg_iter, |
| 1039 | muxer_upstream_msg_iter); |
| 1040 | } else { |
| 1041 | BT_LOGV("Cannot validate muxer's upstream message iterator wrapper: " |
| 1042 | "muxer-msg-iter-addr=%p, " |
| 1043 | "muxer-upstream-msg-iter-wrap-addr=%p", |
| 1044 | muxer_msg_iter, |
| 1045 | muxer_upstream_msg_iter); |
| 1046 | } |
| 1047 | |
| 1048 | goto end; |
| 1049 | } |
| 1050 | |
| 1051 | /* |
| 1052 | * Remove this muxer upstream message iterator |
| 1053 | * if it's ended or canceled. |
| 1054 | */ |
| 1055 | if (!muxer_upstream_msg_iter->msg_iter) { |
| 1056 | /* |
| 1057 | * Use g_ptr_array_remove_fast() because the |
| 1058 | * order of those elements is not important. |
| 1059 | */ |
| 1060 | BT_LOGV("Removing muxer's upstream message iterator wrapper: ended or canceled: " |
| 1061 | "muxer-msg-iter-addr=%p, " |
| 1062 | "muxer-upstream-msg-iter-wrap-addr=%p", |
| 1063 | muxer_msg_iter, muxer_upstream_msg_iter); |
| 1064 | g_ptr_array_remove_index_fast( |
| 1065 | muxer_msg_iter->muxer_upstream_msg_iters, |
| 1066 | i); |
| 1067 | i--; |
| 1068 | } |
| 1069 | } |
| 1070 | |
| 1071 | end: |
| 1072 | return status; |
| 1073 | } |
| 1074 | |
| 1075 | static inline |
| 1076 | bt_self_message_iterator_status muxer_msg_iter_do_next_one( |
| 1077 | struct muxer_comp *muxer_comp, |
| 1078 | struct muxer_msg_iter *muxer_msg_iter, |
| 1079 | const bt_message **msg) |
| 1080 | { |
| 1081 | bt_self_message_iterator_status status = |
| 1082 | BT_SELF_MESSAGE_ITERATOR_STATUS_OK; |
| 1083 | struct muxer_upstream_msg_iter *muxer_upstream_msg_iter = NULL; |
| 1084 | int64_t next_return_ts; |
| 1085 | |
| 1086 | while (true) { |
| 1087 | int ret = muxer_msg_iter_handle_newly_connected_ports( |
| 1088 | muxer_msg_iter); |
| 1089 | |
| 1090 | if (ret) { |
| 1091 | BT_LOGE("Cannot handle newly connected input ports for muxer's message iterator: " |
| 1092 | "muxer-comp-addr=%p, muxer-msg-iter-addr=%p, " |
| 1093 | "ret=%d", |
| 1094 | muxer_comp, muxer_msg_iter, ret); |
| 1095 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR; |
| 1096 | goto end; |
| 1097 | } |
| 1098 | |
| 1099 | status = validate_muxer_upstream_msg_iters(muxer_msg_iter); |
| 1100 | if (status != BT_SELF_MESSAGE_ITERATOR_STATUS_OK) { |
| 1101 | /* validate_muxer_upstream_msg_iters() logs details */ |
| 1102 | goto end; |
| 1103 | } |
| 1104 | |
| 1105 | /* |
| 1106 | * At this point, we know that all the existing upstream |
| 1107 | * message iterators are valid. However the |
| 1108 | * operations to validate them (during |
| 1109 | * validate_muxer_upstream_msg_iters()) may have |
| 1110 | * connected new ports. If no ports were connected |
| 1111 | * during this operation, exit the loop. |
| 1112 | */ |
| 1113 | if (!muxer_msg_iter->newly_connected_self_ports) { |
| 1114 | BT_LOGV("Not breaking this loop: muxer's message iterator still has newly connected input ports to handle: " |
| 1115 | "muxer-comp-addr=%p", muxer_comp); |
| 1116 | break; |
| 1117 | } |
| 1118 | } |
| 1119 | |
| 1120 | BT_ASSERT(!muxer_msg_iter->newly_connected_self_ports); |
| 1121 | |
| 1122 | /* |
| 1123 | * At this point we know that all the existing upstream |
| 1124 | * message iterators are valid. We can find the one, |
| 1125 | * amongst those, of which the current message is the |
| 1126 | * youngest. |
| 1127 | */ |
| 1128 | status = muxer_msg_iter_youngest_upstream_msg_iter(muxer_comp, |
| 1129 | muxer_msg_iter, &muxer_upstream_msg_iter, |
| 1130 | &next_return_ts); |
| 1131 | if (status < 0 || status == BT_SELF_MESSAGE_ITERATOR_STATUS_END) { |
| 1132 | if (status < 0) { |
| 1133 | BT_LOGE("Cannot find the youngest upstream message iterator wrapper: " |
| 1134 | "status=%s", |
| 1135 | bt_self_message_iterator_status_string(status)); |
| 1136 | } else { |
| 1137 | BT_LOGV("Cannot find the youngest upstream message iterator wrapper: " |
| 1138 | "status=%s", |
| 1139 | bt_self_message_iterator_status_string(status)); |
| 1140 | } |
| 1141 | |
| 1142 | goto end; |
| 1143 | } |
| 1144 | |
| 1145 | if (next_return_ts < muxer_msg_iter->last_returned_ts_ns) { |
| 1146 | BT_LOGE("Youngest upstream message iterator wrapper's timestamp is less than muxer's message iterator's last returned timestamp: " |
| 1147 | "muxer-msg-iter-addr=%p, ts=%" PRId64 ", " |
| 1148 | "last-returned-ts=%" PRId64, |
| 1149 | muxer_msg_iter, next_return_ts, |
| 1150 | muxer_msg_iter->last_returned_ts_ns); |
| 1151 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR; |
| 1152 | goto end; |
| 1153 | } |
| 1154 | |
| 1155 | BT_LOGV("Found youngest upstream message iterator wrapper: " |
| 1156 | "muxer-msg-iter-addr=%p, " |
| 1157 | "muxer-upstream-msg-iter-wrap-addr=%p, " |
| 1158 | "ts=%" PRId64, |
| 1159 | muxer_msg_iter, muxer_upstream_msg_iter, next_return_ts); |
| 1160 | BT_ASSERT(status == BT_SELF_MESSAGE_ITERATOR_STATUS_OK); |
| 1161 | BT_ASSERT(muxer_upstream_msg_iter); |
| 1162 | |
| 1163 | /* |
| 1164 | * Consume from the queue's head: other side |
| 1165 | * (muxer_upstream_msg_iter_next()) writes to the tail. |
| 1166 | */ |
| 1167 | *msg = g_queue_pop_head(muxer_upstream_msg_iter->msgs); |
| 1168 | BT_ASSERT(*msg); |
| 1169 | muxer_msg_iter->last_returned_ts_ns = next_return_ts; |
| 1170 | |
| 1171 | end: |
| 1172 | return status; |
| 1173 | } |
| 1174 | |
| 1175 | static |
| 1176 | bt_self_message_iterator_status muxer_msg_iter_do_next( |
| 1177 | struct muxer_comp *muxer_comp, |
| 1178 | struct muxer_msg_iter *muxer_msg_iter, |
| 1179 | bt_message_array_const msgs, uint64_t capacity, |
| 1180 | uint64_t *count) |
| 1181 | { |
| 1182 | bt_self_message_iterator_status status = |
| 1183 | BT_SELF_MESSAGE_ITERATOR_STATUS_OK; |
| 1184 | uint64_t i = 0; |
| 1185 | |
| 1186 | while (i < capacity && status == BT_SELF_MESSAGE_ITERATOR_STATUS_OK) { |
| 1187 | status = muxer_msg_iter_do_next_one(muxer_comp, |
| 1188 | muxer_msg_iter, &msgs[i]); |
| 1189 | if (status == BT_SELF_MESSAGE_ITERATOR_STATUS_OK) { |
| 1190 | i++; |
| 1191 | } |
| 1192 | } |
| 1193 | |
| 1194 | if (i > 0) { |
| 1195 | /* |
| 1196 | * Even if muxer_msg_iter_do_next_one() returned |
| 1197 | * something else than |
| 1198 | * BT_MESSAGE_ITERATOR_STATUS_OK, we accumulated |
| 1199 | * message objects in the output message |
| 1200 | * array, so we need to return |
| 1201 | * BT_MESSAGE_ITERATOR_STATUS_OK so that they are |
| 1202 | * transfered to downstream. This other status occurs |
| 1203 | * again the next time muxer_msg_iter_do_next() is |
| 1204 | * called, possibly without any accumulated |
| 1205 | * message, in which case we'll return it. |
| 1206 | */ |
| 1207 | *count = i; |
| 1208 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_OK; |
| 1209 | } |
| 1210 | |
| 1211 | return status; |
| 1212 | } |
| 1213 | |
| 1214 | static |
| 1215 | void destroy_muxer_msg_iter(struct muxer_msg_iter *muxer_msg_iter) |
| 1216 | { |
| 1217 | if (!muxer_msg_iter) { |
| 1218 | return; |
| 1219 | } |
| 1220 | |
| 1221 | BT_LOGD("Destroying muxer component's message iterator: " |
| 1222 | "muxer-msg-iter-addr=%p", muxer_msg_iter); |
| 1223 | |
| 1224 | if (muxer_msg_iter->muxer_upstream_msg_iters) { |
| 1225 | BT_LOGD_STR("Destroying muxer's upstream message iterator wrappers."); |
| 1226 | g_ptr_array_free( |
| 1227 | muxer_msg_iter->muxer_upstream_msg_iters, TRUE); |
| 1228 | } |
| 1229 | |
| 1230 | g_list_free(muxer_msg_iter->newly_connected_self_ports); |
| 1231 | g_free(muxer_msg_iter); |
| 1232 | } |
| 1233 | |
| 1234 | static |
| 1235 | int muxer_msg_iter_init_newly_connected_ports(struct muxer_comp *muxer_comp, |
| 1236 | struct muxer_msg_iter *muxer_msg_iter) |
| 1237 | { |
| 1238 | int64_t count; |
| 1239 | int64_t i; |
| 1240 | int ret = 0; |
| 1241 | |
| 1242 | /* |
| 1243 | * Add the connected input ports to this muxer message |
| 1244 | * iterator's list of newly connected ports. They will be |
| 1245 | * handled by muxer_msg_iter_handle_newly_connected_ports(). |
| 1246 | */ |
| 1247 | count = bt_component_filter_get_input_port_count( |
| 1248 | bt_self_component_filter_as_component_filter( |
| 1249 | muxer_comp->self_comp)); |
| 1250 | if (count < 0) { |
| 1251 | BT_LOGD("No input port to initialize for muxer component's message iterator: " |
| 1252 | "muxer-comp-addr=%p, muxer-msg-iter-addr=%p", |
| 1253 | muxer_comp, muxer_msg_iter); |
| 1254 | goto end; |
| 1255 | } |
| 1256 | |
| 1257 | for (i = 0; i < count; i++) { |
| 1258 | bt_self_component_port_input *self_port = |
| 1259 | bt_self_component_filter_borrow_input_port_by_index( |
| 1260 | muxer_comp->self_comp, i); |
| 1261 | const bt_port *port; |
| 1262 | |
| 1263 | BT_ASSERT(self_port); |
| 1264 | port = bt_self_component_port_as_port( |
| 1265 | bt_self_component_port_input_as_self_component_port( |
| 1266 | self_port)); |
| 1267 | BT_ASSERT(port); |
| 1268 | |
| 1269 | if (!bt_port_is_connected(port)) { |
| 1270 | BT_LOGD("Skipping input port: not connected: " |
| 1271 | "muxer-comp-addr=%p, port-addr=%p, port-name\"%s\"", |
| 1272 | muxer_comp, port, bt_port_get_name(port)); |
| 1273 | continue; |
| 1274 | } |
| 1275 | |
| 1276 | muxer_msg_iter->newly_connected_self_ports = |
| 1277 | g_list_append( |
| 1278 | muxer_msg_iter->newly_connected_self_ports, |
| 1279 | self_port); |
| 1280 | if (!muxer_msg_iter->newly_connected_self_ports) { |
| 1281 | BT_LOGE("Cannot append port to muxer's message iterator list of newly connected input ports: " |
| 1282 | "port-addr=%p, port-name=\"%s\", " |
| 1283 | "muxer-msg-iter-addr=%p", port, |
| 1284 | bt_port_get_name(port), muxer_msg_iter); |
| 1285 | ret = -1; |
| 1286 | goto end; |
| 1287 | } |
| 1288 | |
| 1289 | BT_LOGD("Appended port to muxer's message iterator list of newly connected input ports: " |
| 1290 | "port-addr=%p, port-name=\"%s\", " |
| 1291 | "muxer-msg-iter-addr=%p", port, |
| 1292 | bt_port_get_name(port), muxer_msg_iter); |
| 1293 | } |
| 1294 | |
| 1295 | end: |
| 1296 | return ret; |
| 1297 | } |
| 1298 | |
| 1299 | BT_HIDDEN |
| 1300 | bt_self_message_iterator_status muxer_msg_iter_init( |
| 1301 | bt_self_message_iterator *self_msg_iter, |
| 1302 | bt_self_component_filter *self_comp, |
| 1303 | bt_self_component_port_output *port) |
| 1304 | { |
| 1305 | struct muxer_comp *muxer_comp = NULL; |
| 1306 | struct muxer_msg_iter *muxer_msg_iter = NULL; |
| 1307 | bt_self_message_iterator_status status = |
| 1308 | BT_SELF_MESSAGE_ITERATOR_STATUS_OK; |
| 1309 | int ret; |
| 1310 | |
| 1311 | muxer_comp = bt_self_component_get_data( |
| 1312 | bt_self_component_filter_as_self_component(self_comp)); |
| 1313 | BT_ASSERT(muxer_comp); |
| 1314 | BT_LOGD("Initializing muxer component's message iterator: " |
| 1315 | "comp-addr=%p, muxer-comp-addr=%p, msg-iter-addr=%p", |
| 1316 | self_comp, muxer_comp, self_msg_iter); |
| 1317 | |
| 1318 | if (muxer_comp->initializing_muxer_msg_iter) { |
| 1319 | /* |
| 1320 | * Weird, unhandled situation detected: downstream |
| 1321 | * creates a muxer message iterator while creating |
| 1322 | * another muxer message iterator (same component). |
| 1323 | */ |
| 1324 | BT_LOGE("Recursive initialization of muxer component's message iterator: " |
| 1325 | "comp-addr=%p, muxer-comp-addr=%p, msg-iter-addr=%p", |
| 1326 | self_comp, muxer_comp, self_msg_iter); |
| 1327 | goto error; |
| 1328 | } |
| 1329 | |
| 1330 | muxer_comp->initializing_muxer_msg_iter = true; |
| 1331 | muxer_msg_iter = g_new0(struct muxer_msg_iter, 1); |
| 1332 | if (!muxer_msg_iter) { |
| 1333 | BT_LOGE_STR("Failed to allocate one muxer component's message iterator."); |
| 1334 | goto error; |
| 1335 | } |
| 1336 | |
| 1337 | muxer_msg_iter->last_returned_ts_ns = INT64_MIN; |
| 1338 | muxer_msg_iter->muxer_upstream_msg_iters = |
| 1339 | g_ptr_array_new_with_free_func( |
| 1340 | (GDestroyNotify) destroy_muxer_upstream_msg_iter); |
| 1341 | if (!muxer_msg_iter->muxer_upstream_msg_iters) { |
| 1342 | BT_LOGE_STR("Failed to allocate a GPtrArray."); |
| 1343 | goto error; |
| 1344 | } |
| 1345 | |
| 1346 | /* |
| 1347 | * Add the muxer message iterator to the component's array |
| 1348 | * of muxer message iterators here because |
| 1349 | * muxer_msg_iter_init_newly_connected_ports() can cause |
| 1350 | * muxer_port_connected() to be called, which adds the newly |
| 1351 | * connected port to each muxer message iterator's list of |
| 1352 | * newly connected ports. |
| 1353 | */ |
| 1354 | g_ptr_array_add(muxer_comp->muxer_msg_iters, muxer_msg_iter); |
| 1355 | ret = muxer_msg_iter_init_newly_connected_ports(muxer_comp, |
| 1356 | muxer_msg_iter); |
| 1357 | if (ret) { |
| 1358 | BT_LOGE("Cannot initialize newly connected input ports for muxer component's message iterator: " |
| 1359 | "comp-addr=%p, muxer-comp-addr=%p, " |
| 1360 | "muxer-msg-iter-addr=%p, msg-iter-addr=%p, ret=%d", |
| 1361 | self_comp, muxer_comp, muxer_msg_iter, |
| 1362 | self_msg_iter, ret); |
| 1363 | goto error; |
| 1364 | } |
| 1365 | |
| 1366 | bt_self_message_iterator_set_data(self_msg_iter, |
| 1367 | muxer_msg_iter); |
| 1368 | BT_LOGD("Initialized muxer component's message iterator: " |
| 1369 | "comp-addr=%p, muxer-comp-addr=%p, muxer-msg-iter-addr=%p, " |
| 1370 | "msg-iter-addr=%p", |
| 1371 | self_comp, muxer_comp, muxer_msg_iter, self_msg_iter); |
| 1372 | goto end; |
| 1373 | |
| 1374 | error: |
| 1375 | if (g_ptr_array_index(muxer_comp->muxer_msg_iters, |
| 1376 | muxer_comp->muxer_msg_iters->len - 1) == muxer_msg_iter) { |
| 1377 | g_ptr_array_remove_index(muxer_comp->muxer_msg_iters, |
| 1378 | muxer_comp->muxer_msg_iters->len - 1); |
| 1379 | } |
| 1380 | |
| 1381 | destroy_muxer_msg_iter(muxer_msg_iter); |
| 1382 | bt_self_message_iterator_set_data(self_msg_iter, |
| 1383 | NULL); |
| 1384 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR; |
| 1385 | |
| 1386 | end: |
| 1387 | muxer_comp->initializing_muxer_msg_iter = false; |
| 1388 | return status; |
| 1389 | } |
| 1390 | |
| 1391 | BT_HIDDEN |
| 1392 | void muxer_msg_iter_finalize( |
| 1393 | bt_self_message_iterator *self_msg_iter) |
| 1394 | { |
| 1395 | struct muxer_msg_iter *muxer_msg_iter = |
| 1396 | bt_self_message_iterator_get_data(self_msg_iter); |
| 1397 | bt_self_component *self_comp = NULL; |
| 1398 | struct muxer_comp *muxer_comp = NULL; |
| 1399 | |
| 1400 | self_comp = bt_self_message_iterator_borrow_component( |
| 1401 | self_msg_iter); |
| 1402 | BT_ASSERT(self_comp); |
| 1403 | muxer_comp = bt_self_component_get_data(self_comp); |
| 1404 | BT_LOGD("Finalizing muxer component's message iterator: " |
| 1405 | "comp-addr=%p, muxer-comp-addr=%p, muxer-msg-iter-addr=%p, " |
| 1406 | "msg-iter-addr=%p", |
| 1407 | self_comp, muxer_comp, muxer_msg_iter, self_msg_iter); |
| 1408 | |
| 1409 | if (muxer_comp) { |
| 1410 | (void) g_ptr_array_remove_fast(muxer_comp->muxer_msg_iters, |
| 1411 | muxer_msg_iter); |
| 1412 | destroy_muxer_msg_iter(muxer_msg_iter); |
| 1413 | } |
| 1414 | } |
| 1415 | |
| 1416 | BT_HIDDEN |
| 1417 | bt_self_message_iterator_status muxer_msg_iter_next( |
| 1418 | bt_self_message_iterator *self_msg_iter, |
| 1419 | bt_message_array_const msgs, uint64_t capacity, |
| 1420 | uint64_t *count) |
| 1421 | { |
| 1422 | bt_self_message_iterator_status status; |
| 1423 | struct muxer_msg_iter *muxer_msg_iter = |
| 1424 | bt_self_message_iterator_get_data(self_msg_iter); |
| 1425 | bt_self_component *self_comp = NULL; |
| 1426 | struct muxer_comp *muxer_comp = NULL; |
| 1427 | |
| 1428 | BT_ASSERT(muxer_msg_iter); |
| 1429 | self_comp = bt_self_message_iterator_borrow_component( |
| 1430 | self_msg_iter); |
| 1431 | BT_ASSERT(self_comp); |
| 1432 | muxer_comp = bt_self_component_get_data(self_comp); |
| 1433 | BT_ASSERT(muxer_comp); |
| 1434 | BT_LOGV("Muxer component's message iterator's \"next\" method called: " |
| 1435 | "comp-addr=%p, muxer-comp-addr=%p, muxer-msg-iter-addr=%p, " |
| 1436 | "msg-iter-addr=%p", |
| 1437 | self_comp, muxer_comp, muxer_msg_iter, self_msg_iter); |
| 1438 | |
| 1439 | status = muxer_msg_iter_do_next(muxer_comp, muxer_msg_iter, |
| 1440 | msgs, capacity, count); |
| 1441 | if (status < 0) { |
| 1442 | BT_LOGE("Cannot get next message: " |
| 1443 | "comp-addr=%p, muxer-comp-addr=%p, muxer-msg-iter-addr=%p, " |
| 1444 | "msg-iter-addr=%p, status=%s", |
| 1445 | self_comp, muxer_comp, muxer_msg_iter, self_msg_iter, |
| 1446 | bt_self_message_iterator_status_string(status)); |
| 1447 | } else { |
| 1448 | BT_LOGV("Returning from muxer component's message iterator's \"next\" method: " |
| 1449 | "status=%s", |
| 1450 | bt_self_message_iterator_status_string(status)); |
| 1451 | } |
| 1452 | |
| 1453 | return status; |
| 1454 | } |
| 1455 | |
| 1456 | BT_HIDDEN |
| 1457 | bt_self_component_status muxer_input_port_connected( |
| 1458 | bt_self_component_filter *self_comp, |
| 1459 | bt_self_component_port_input *self_port, |
| 1460 | const bt_port_output *other_port) |
| 1461 | { |
| 1462 | bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK; |
| 1463 | const bt_port *port = bt_self_component_port_as_port( |
| 1464 | bt_self_component_port_input_as_self_component_port( |
| 1465 | self_port)); |
| 1466 | struct muxer_comp *muxer_comp = |
| 1467 | bt_self_component_get_data( |
| 1468 | bt_self_component_filter_as_self_component( |
| 1469 | self_comp)); |
| 1470 | size_t i; |
| 1471 | int ret; |
| 1472 | |
| 1473 | BT_ASSERT(port); |
| 1474 | BT_ASSERT(muxer_comp); |
| 1475 | BT_LOGD("Port connected: " |
| 1476 | "comp-addr=%p, muxer-comp-addr=%p, " |
| 1477 | "port-addr=%p, port-name=\"%s\", " |
| 1478 | "other-port-addr=%p, other-port-name=\"%s\"", |
| 1479 | self_comp, muxer_comp, self_port, bt_port_get_name(port), |
| 1480 | other_port, |
| 1481 | bt_port_get_name(bt_port_output_as_port_const(other_port))); |
| 1482 | |
| 1483 | for (i = 0; i < muxer_comp->muxer_msg_iters->len; i++) { |
| 1484 | struct muxer_msg_iter *muxer_msg_iter = |
| 1485 | g_ptr_array_index(muxer_comp->muxer_msg_iters, i); |
| 1486 | |
| 1487 | /* |
| 1488 | * Add this port to the list of newly connected ports |
| 1489 | * for this muxer message iterator. We append at |
| 1490 | * the end of this list while |
| 1491 | * muxer_msg_iter_handle_newly_connected_ports() |
| 1492 | * removes the nodes from the beginning. |
| 1493 | */ |
| 1494 | muxer_msg_iter->newly_connected_self_ports = |
| 1495 | g_list_append( |
| 1496 | muxer_msg_iter->newly_connected_self_ports, |
| 1497 | self_port); |
| 1498 | if (!muxer_msg_iter->newly_connected_self_ports) { |
| 1499 | BT_LOGE("Cannot append port to muxer's message iterator list of newly connected input ports: " |
| 1500 | "port-addr=%p, port-name=\"%s\", " |
| 1501 | "muxer-msg-iter-addr=%p", self_port, |
| 1502 | bt_port_get_name(port), muxer_msg_iter); |
| 1503 | status = BT_SELF_COMPONENT_STATUS_ERROR; |
| 1504 | goto end; |
| 1505 | } |
| 1506 | |
| 1507 | BT_LOGD("Appended port to muxer's message iterator list of newly connected input ports: " |
| 1508 | "port-addr=%p, port-name=\"%s\", " |
| 1509 | "muxer-msg-iter-addr=%p", self_port, |
| 1510 | bt_port_get_name(port), muxer_msg_iter); |
| 1511 | } |
| 1512 | |
| 1513 | /* One less available input port */ |
| 1514 | muxer_comp->available_input_ports--; |
| 1515 | ret = ensure_available_input_port(self_comp); |
| 1516 | if (ret) { |
| 1517 | /* |
| 1518 | * Only way to report an error later since this |
| 1519 | * method does not return anything. |
| 1520 | */ |
| 1521 | BT_LOGE("Cannot ensure that at least one muxer component's input port is available: " |
| 1522 | "muxer-comp-addr=%p, status=%s", |
| 1523 | muxer_comp, bt_self_component_status_string(ret)); |
| 1524 | status = BT_SELF_COMPONENT_STATUS_ERROR; |
| 1525 | goto end; |
| 1526 | } |
| 1527 | |
| 1528 | end: |
| 1529 | return status; |
| 1530 | } |
| 1531 | |
| 1532 | BT_HIDDEN |
| 1533 | bt_bool muxer_msg_iter_can_seek_beginning( |
| 1534 | bt_self_message_iterator *self_msg_iter) |
| 1535 | { |
| 1536 | struct muxer_msg_iter *muxer_msg_iter = |
| 1537 | bt_self_message_iterator_get_data(self_msg_iter); |
| 1538 | uint64_t i; |
| 1539 | bt_bool ret = BT_TRUE; |
| 1540 | |
| 1541 | for (i = 0; i < muxer_msg_iter->muxer_upstream_msg_iters->len; i++) { |
| 1542 | struct muxer_upstream_msg_iter *upstream_msg_iter = |
| 1543 | muxer_msg_iter->muxer_upstream_msg_iters->pdata[i]; |
| 1544 | |
| 1545 | if (!bt_self_component_port_input_message_iterator_can_seek_beginning( |
| 1546 | upstream_msg_iter->msg_iter)) { |
| 1547 | ret = BT_FALSE; |
| 1548 | goto end; |
| 1549 | } |
| 1550 | } |
| 1551 | |
| 1552 | end: |
| 1553 | return ret; |
| 1554 | } |
| 1555 | |
| 1556 | BT_HIDDEN |
| 1557 | bt_self_message_iterator_status muxer_msg_iter_seek_beginning( |
| 1558 | bt_self_message_iterator *self_msg_iter) |
| 1559 | { |
| 1560 | struct muxer_msg_iter *muxer_msg_iter = |
| 1561 | bt_self_message_iterator_get_data(self_msg_iter); |
| 1562 | int status; |
| 1563 | uint64_t i; |
| 1564 | |
| 1565 | for (i = 0; i < muxer_msg_iter->muxer_upstream_msg_iters->len; i++) { |
| 1566 | struct muxer_upstream_msg_iter *upstream_msg_iter = |
| 1567 | muxer_msg_iter->muxer_upstream_msg_iters->pdata[i]; |
| 1568 | |
| 1569 | status = bt_self_component_port_input_message_iterator_seek_beginning( |
| 1570 | upstream_msg_iter->msg_iter); |
| 1571 | if (status != BT_MESSAGE_ITERATOR_STATUS_OK) { |
| 1572 | goto end; |
| 1573 | } |
| 1574 | } |
| 1575 | |
| 1576 | muxer_msg_iter->last_returned_ts_ns = INT64_MIN; |
| 1577 | |
| 1578 | end: |
| 1579 | return status; |
| 1580 | } |