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