| 1 | /* |
| 2 | * SPDX-License-Identifier: MIT |
| 3 | * |
| 4 | * Copyright 2019 Francis Deslauriers <francis.deslauriers@efficios.com> |
| 5 | */ |
| 6 | |
| 7 | #include <babeltrace2/babeltrace.h> |
| 8 | |
| 9 | #include <inttypes.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <string.h> |
| 12 | |
| 13 | #include "common/assert.h" |
| 14 | #include "common/common.h" |
| 15 | #include "common/macros.h" |
| 16 | #include "common/uuid.h" |
| 17 | |
| 18 | #include "muxing.h" |
| 19 | |
| 20 | struct message_to_compare { |
| 21 | const bt_message *msg; |
| 22 | const bt_trace *trace; |
| 23 | const bt_stream *stream; |
| 24 | }; |
| 25 | |
| 26 | struct messages_to_compare { |
| 27 | struct message_to_compare left; |
| 28 | struct message_to_compare right; |
| 29 | }; |
| 30 | |
| 31 | static |
| 32 | int message_type_weight(const bt_message_type msg_type) |
| 33 | { |
| 34 | int weight; |
| 35 | |
| 36 | switch (msg_type) { |
| 37 | case BT_MESSAGE_TYPE_STREAM_BEGINNING: |
| 38 | weight = 7; |
| 39 | break; |
| 40 | case BT_MESSAGE_TYPE_PACKET_BEGINNING: |
| 41 | weight = 6; |
| 42 | break; |
| 43 | case BT_MESSAGE_TYPE_EVENT: |
| 44 | weight = 5; |
| 45 | break; |
| 46 | case BT_MESSAGE_TYPE_DISCARDED_EVENTS: |
| 47 | weight = 4; |
| 48 | break; |
| 49 | case BT_MESSAGE_TYPE_PACKET_END: |
| 50 | weight = 3; |
| 51 | break; |
| 52 | case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY: |
| 53 | weight = 2; |
| 54 | break; |
| 55 | case BT_MESSAGE_TYPE_DISCARDED_PACKETS: |
| 56 | weight = 1; |
| 57 | break; |
| 58 | case BT_MESSAGE_TYPE_STREAM_END: |
| 59 | weight = 0; |
| 60 | break; |
| 61 | default: |
| 62 | bt_common_abort(); |
| 63 | } |
| 64 | |
| 65 | return weight; |
| 66 | } |
| 67 | |
| 68 | /* |
| 69 | * Compare 2 messages to order them in a determinitic way based on their |
| 70 | * types. |
| 71 | * Returns -1 is left mesage must go first |
| 72 | * Returns 1 is right mesage must go first |
| 73 | */ |
| 74 | static |
| 75 | int compare_messages_by_type(struct messages_to_compare *msgs) |
| 76 | { |
| 77 | bt_message_type left_msg_type = bt_message_get_type(msgs->left.msg); |
| 78 | bt_message_type right_msg_type = bt_message_get_type(msgs->right.msg); |
| 79 | |
| 80 | return message_type_weight(right_msg_type) - |
| 81 | message_type_weight(left_msg_type); |
| 82 | } |
| 83 | |
| 84 | static |
| 85 | int compare_events(const bt_event *left_event, const bt_event *right_event) |
| 86 | { |
| 87 | int ret = 0; |
| 88 | const bt_event_class *left_event_class, *right_event_class; |
| 89 | uint64_t left_event_class_id, right_event_class_id; |
| 90 | const char *left_event_class_name, *right_event_class_name, |
| 91 | *left_event_class_emf_uri, *right_event_class_emf_uri; |
| 92 | bt_event_class_log_level left_event_class_log_level, right_event_class_log_level; |
| 93 | bt_property_availability left_log_level_avail, right_log_level_avail; |
| 94 | |
| 95 | left_event_class = bt_event_borrow_class_const(left_event); |
| 96 | right_event_class = bt_event_borrow_class_const(right_event); |
| 97 | |
| 98 | left_event_class_id = bt_event_class_get_id(left_event_class); |
| 99 | right_event_class_id = bt_event_class_get_id(right_event_class); |
| 100 | |
| 101 | if (left_event_class_id > right_event_class_id) { |
| 102 | ret = 1; |
| 103 | goto end; |
| 104 | } else if (left_event_class_id < right_event_class_id) { |
| 105 | ret = -1; |
| 106 | goto end; |
| 107 | } |
| 108 | |
| 109 | left_event_class_name = bt_event_class_get_name(left_event_class); |
| 110 | right_event_class_name = bt_event_class_get_name(right_event_class); |
| 111 | if (left_event_class_name && right_event_class_name) { |
| 112 | ret = strcmp(left_event_class_name, right_event_class_name); |
| 113 | if (ret != 0) { |
| 114 | goto end; |
| 115 | } |
| 116 | } else if (!left_event_class_name && right_event_class_name) { |
| 117 | ret = -1; |
| 118 | goto end; |
| 119 | } else if (left_event_class_name && !right_event_class_name) { |
| 120 | ret = 1; |
| 121 | goto end; |
| 122 | } |
| 123 | |
| 124 | left_log_level_avail = bt_event_class_get_log_level(left_event_class, |
| 125 | &left_event_class_log_level); |
| 126 | right_log_level_avail = bt_event_class_get_log_level(right_event_class, |
| 127 | &right_event_class_log_level); |
| 128 | |
| 129 | if (left_log_level_avail == BT_PROPERTY_AVAILABILITY_AVAILABLE && |
| 130 | right_log_level_avail == BT_PROPERTY_AVAILABILITY_AVAILABLE) { |
| 131 | ret = left_event_class_log_level - right_event_class_log_level; |
| 132 | if (ret) { |
| 133 | goto end; |
| 134 | } |
| 135 | } else if (left_log_level_avail == BT_PROPERTY_AVAILABILITY_AVAILABLE && |
| 136 | right_log_level_avail == BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE) { |
| 137 | ret = -1; |
| 138 | goto end; |
| 139 | } else if (left_log_level_avail == BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE && |
| 140 | right_log_level_avail == BT_PROPERTY_AVAILABILITY_AVAILABLE) { |
| 141 | ret = 1; |
| 142 | goto end; |
| 143 | } |
| 144 | |
| 145 | left_event_class_emf_uri = bt_event_class_get_emf_uri(left_event_class); |
| 146 | right_event_class_emf_uri = bt_event_class_get_emf_uri(right_event_class); |
| 147 | if (left_event_class_emf_uri && right_event_class_emf_uri) { |
| 148 | ret = strcmp(left_event_class_emf_uri, right_event_class_emf_uri); |
| 149 | if (ret != 0) { |
| 150 | goto end; |
| 151 | } |
| 152 | } else if (!left_event_class_emf_uri && right_event_class_emf_uri) { |
| 153 | ret = -1; |
| 154 | goto end; |
| 155 | } else if (left_event_class_emf_uri && !right_event_class_emf_uri) { |
| 156 | ret = 1; |
| 157 | goto end; |
| 158 | } |
| 159 | |
| 160 | end: |
| 161 | return ret; |
| 162 | } |
| 163 | |
| 164 | static |
| 165 | int compare_streams(const bt_stream *left_stream, const bt_stream *right_stream) |
| 166 | { |
| 167 | int ret = 0; |
| 168 | const char *left_stream_name, *right_stream_name, |
| 169 | *left_stream_class_name, *right_stream_class_name; |
| 170 | const bt_stream_class *left_stream_class, *right_stream_class; |
| 171 | |
| 172 | /* |
| 173 | * No need to compare stream id as it was checked earlier and if we are |
| 174 | * here it means they are identical or both absent. |
| 175 | */ |
| 176 | BT_ASSERT(bt_stream_get_id(left_stream) == |
| 177 | bt_stream_get_id(right_stream)); |
| 178 | |
| 179 | /* Compare stream name. */ |
| 180 | left_stream_name = bt_stream_get_name(left_stream); |
| 181 | right_stream_name = bt_stream_get_name(right_stream); |
| 182 | |
| 183 | if (left_stream_name && right_stream_name) { |
| 184 | ret = strcmp(left_stream_name, right_stream_name); |
| 185 | if (ret != 0) { |
| 186 | goto end; |
| 187 | } |
| 188 | } else if (!left_stream_name && right_stream_name) { |
| 189 | ret = -1; |
| 190 | goto end; |
| 191 | } else if (left_stream_name && !right_stream_name) { |
| 192 | ret = 1; |
| 193 | goto end; |
| 194 | } |
| 195 | |
| 196 | left_stream_class = bt_stream_borrow_class_const(left_stream); |
| 197 | right_stream_class = bt_stream_borrow_class_const(right_stream); |
| 198 | |
| 199 | /* |
| 200 | * No need to compare stream class id as it was checked earlier and if |
| 201 | * we are here it means they are identical. |
| 202 | */ |
| 203 | BT_ASSERT(bt_stream_class_get_id(left_stream_class) == |
| 204 | bt_stream_class_get_id(right_stream_class)); |
| 205 | |
| 206 | /* Compare stream class name. */ |
| 207 | left_stream_class_name = bt_stream_class_get_name(left_stream_class); |
| 208 | right_stream_class_name = bt_stream_class_get_name(right_stream_class); |
| 209 | |
| 210 | if (left_stream_class_name && right_stream_class_name) { |
| 211 | ret = strcmp(left_stream_class_name, right_stream_class_name); |
| 212 | if (ret != 0) { |
| 213 | goto end; |
| 214 | } |
| 215 | } else if (!left_stream_class_name && right_stream_class_name) { |
| 216 | ret = -1; |
| 217 | goto end; |
| 218 | } else if (left_stream_class_name && !right_stream_class_name) { |
| 219 | ret = 1; |
| 220 | goto end; |
| 221 | } |
| 222 | |
| 223 | /* Compare stream class automatic event class id assignment. */ |
| 224 | if (bt_stream_class_assigns_automatic_event_class_id(left_stream_class) && |
| 225 | !bt_stream_class_assigns_automatic_event_class_id(right_stream_class)) { |
| 226 | ret = 1; |
| 227 | goto end; |
| 228 | } else if (!bt_stream_class_assigns_automatic_event_class_id(left_stream_class) && |
| 229 | bt_stream_class_assigns_automatic_event_class_id(right_stream_class)) { |
| 230 | ret = -1; |
| 231 | goto end; |
| 232 | } |
| 233 | |
| 234 | /* Compare stream class automatic stream id assignment. */ |
| 235 | if (bt_stream_class_assigns_automatic_stream_id(left_stream_class) && |
| 236 | !bt_stream_class_assigns_automatic_stream_id(right_stream_class)) { |
| 237 | ret = 1; |
| 238 | goto end; |
| 239 | } else if (!bt_stream_class_assigns_automatic_stream_id(left_stream_class) && |
| 240 | bt_stream_class_assigns_automatic_stream_id(right_stream_class)) { |
| 241 | ret = -1; |
| 242 | goto end; |
| 243 | } |
| 244 | |
| 245 | /* Compare stream class support of discarded events. */ |
| 246 | if (bt_stream_class_supports_discarded_events(left_stream_class) && |
| 247 | !bt_stream_class_supports_discarded_events(right_stream_class)) { |
| 248 | ret = 1; |
| 249 | goto end; |
| 250 | } else if (!bt_stream_class_supports_discarded_events(left_stream_class) && |
| 251 | bt_stream_class_supports_discarded_events(right_stream_class)) { |
| 252 | ret = -1; |
| 253 | goto end; |
| 254 | } |
| 255 | |
| 256 | /* Compare stream class discarded events default clock snapshot. */ |
| 257 | if (bt_stream_class_discarded_events_have_default_clock_snapshots(left_stream_class) && |
| 258 | !bt_stream_class_discarded_events_have_default_clock_snapshots(right_stream_class)) { |
| 259 | ret = 1; |
| 260 | goto end; |
| 261 | } else if (!bt_stream_class_discarded_events_have_default_clock_snapshots(left_stream_class) && |
| 262 | bt_stream_class_discarded_events_have_default_clock_snapshots(right_stream_class)) { |
| 263 | ret = -1; |
| 264 | goto end; |
| 265 | } |
| 266 | |
| 267 | /* Compare stream class support of packets. */ |
| 268 | if (bt_stream_class_supports_packets(left_stream_class) && |
| 269 | !bt_stream_class_supports_packets(right_stream_class)) { |
| 270 | ret = 1; |
| 271 | goto end; |
| 272 | } else if (!bt_stream_class_supports_packets(left_stream_class) && |
| 273 | bt_stream_class_supports_packets(right_stream_class)) { |
| 274 | ret = -1; |
| 275 | goto end; |
| 276 | } |
| 277 | |
| 278 | if (!bt_stream_class_supports_packets(left_stream_class)) { |
| 279 | /* Skip all packet related checks. */ |
| 280 | goto end; |
| 281 | } |
| 282 | |
| 283 | /* |
| 284 | * Compare stream class presence of discarded packets beginning default |
| 285 | * clock snapshot. |
| 286 | */ |
| 287 | if (bt_stream_class_packets_have_beginning_default_clock_snapshot(left_stream_class) && |
| 288 | !bt_stream_class_packets_have_beginning_default_clock_snapshot(right_stream_class)) { |
| 289 | ret = 1; |
| 290 | goto end; |
| 291 | } else if (!bt_stream_class_packets_have_beginning_default_clock_snapshot(left_stream_class) && |
| 292 | bt_stream_class_packets_have_beginning_default_clock_snapshot(right_stream_class)) { |
| 293 | ret = -1; |
| 294 | goto end; |
| 295 | } |
| 296 | |
| 297 | /* |
| 298 | * Compare stream class presence of discarded packets end default clock |
| 299 | * snapshot. |
| 300 | */ |
| 301 | if (bt_stream_class_packets_have_end_default_clock_snapshot(left_stream_class) && |
| 302 | !bt_stream_class_packets_have_end_default_clock_snapshot(right_stream_class)) { |
| 303 | ret = 1; |
| 304 | goto end; |
| 305 | } else if (!bt_stream_class_packets_have_end_default_clock_snapshot(left_stream_class) && |
| 306 | bt_stream_class_packets_have_end_default_clock_snapshot(right_stream_class)) { |
| 307 | ret = -1; |
| 308 | goto end; |
| 309 | } |
| 310 | |
| 311 | /* Compare stream class support of discarded packets. */ |
| 312 | if (bt_stream_class_supports_discarded_packets(left_stream_class) && |
| 313 | !bt_stream_class_supports_discarded_packets(right_stream_class)) { |
| 314 | ret = 1; |
| 315 | goto end; |
| 316 | } else if (!bt_stream_class_supports_discarded_packets(left_stream_class) && |
| 317 | bt_stream_class_supports_discarded_packets(right_stream_class)) { |
| 318 | ret = -1; |
| 319 | goto end; |
| 320 | } |
| 321 | |
| 322 | /* Compare stream class discarded packets default clock snapshot. */ |
| 323 | if (bt_stream_class_discarded_packets_have_default_clock_snapshots(left_stream_class) && |
| 324 | !bt_stream_class_discarded_packets_have_default_clock_snapshots(right_stream_class)) { |
| 325 | ret = 1; |
| 326 | goto end; |
| 327 | } else if (!bt_stream_class_discarded_packets_have_default_clock_snapshots(left_stream_class) && |
| 328 | bt_stream_class_discarded_packets_have_default_clock_snapshots(right_stream_class)) { |
| 329 | ret = -1; |
| 330 | goto end; |
| 331 | } |
| 332 | |
| 333 | end: |
| 334 | return ret; |
| 335 | } |
| 336 | |
| 337 | static |
| 338 | int compare_clock_snapshots_and_clock_classes(const bt_clock_snapshot *left_cs, |
| 339 | const bt_clock_snapshot *right_cs) |
| 340 | { |
| 341 | int ret; |
| 342 | uint64_t left_freq, right_freq, left_prec, right_prec; |
| 343 | uint64_t left_cs_value, right_cs_value; |
| 344 | const bt_clock_class *left_clock_class, *right_clock_class; |
| 345 | const char *left_clock_class_name, *right_clock_class_name; |
| 346 | left_cs_value = bt_clock_snapshot_get_value(left_cs); |
| 347 | right_cs_value = bt_clock_snapshot_get_value(right_cs); |
| 348 | bt_uuid left_clock_class_uuid, right_clock_class_uuid; |
| 349 | |
| 350 | ret = left_cs_value - right_cs_value; |
| 351 | if (ret != 0) { |
| 352 | goto end; |
| 353 | } |
| 354 | |
| 355 | left_clock_class = bt_clock_snapshot_borrow_clock_class_const(left_cs); |
| 356 | right_clock_class = bt_clock_snapshot_borrow_clock_class_const(right_cs); |
| 357 | |
| 358 | left_clock_class_uuid = bt_clock_class_get_uuid(left_clock_class); |
| 359 | right_clock_class_uuid = bt_clock_class_get_uuid(right_clock_class); |
| 360 | |
| 361 | if (left_clock_class_uuid && !right_clock_class_uuid) { |
| 362 | ret = -1; |
| 363 | goto end; |
| 364 | } else if (!left_clock_class_uuid && right_clock_class_uuid) { |
| 365 | ret = 1; |
| 366 | goto end; |
| 367 | } else if (left_clock_class_uuid && right_clock_class_uuid) { |
| 368 | ret = bt_uuid_compare(left_clock_class_uuid, |
| 369 | right_clock_class_uuid); |
| 370 | if (ret != 0) { |
| 371 | goto end; |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | |
| 376 | left_clock_class_name = bt_clock_class_get_name(left_clock_class); |
| 377 | right_clock_class_name = bt_clock_class_get_name(right_clock_class); |
| 378 | |
| 379 | if (left_clock_class_name && !right_clock_class_name) { |
| 380 | ret = -1; |
| 381 | goto end; |
| 382 | } else if (!left_clock_class_name && right_clock_class_name) { |
| 383 | ret = 1; |
| 384 | goto end; |
| 385 | } else if (left_clock_class_name && right_clock_class_name) { |
| 386 | ret = strcmp(left_clock_class_name, right_clock_class_name); |
| 387 | if (ret != 0) { |
| 388 | goto end; |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | left_freq = bt_clock_class_get_frequency(left_clock_class); |
| 393 | right_freq = bt_clock_class_get_frequency(right_clock_class); |
| 394 | |
| 395 | ret = right_freq - left_freq; |
| 396 | if (ret != 0) { |
| 397 | goto end; |
| 398 | } |
| 399 | |
| 400 | left_prec = bt_clock_class_get_precision(left_clock_class); |
| 401 | right_prec = bt_clock_class_get_precision(right_clock_class); |
| 402 | |
| 403 | ret = right_prec - left_prec; |
| 404 | if (ret != 0) { |
| 405 | goto end; |
| 406 | } |
| 407 | |
| 408 | end: |
| 409 | return ret; |
| 410 | } |
| 411 | |
| 412 | static |
| 413 | const bt_stream *borrow_stream(const bt_message *msg) |
| 414 | { |
| 415 | bt_message_type msg_type = bt_message_get_type(msg); |
| 416 | const bt_stream *stream = NULL; |
| 417 | const bt_packet *packet = NULL; |
| 418 | const bt_event *event = NULL; |
| 419 | |
| 420 | switch (msg_type) { |
| 421 | case BT_MESSAGE_TYPE_STREAM_BEGINNING: |
| 422 | stream = bt_message_stream_beginning_borrow_stream_const(msg); |
| 423 | break; |
| 424 | case BT_MESSAGE_TYPE_STREAM_END: |
| 425 | stream = bt_message_stream_end_borrow_stream_const(msg); |
| 426 | break; |
| 427 | case BT_MESSAGE_TYPE_PACKET_BEGINNING: |
| 428 | packet = bt_message_packet_beginning_borrow_packet_const(msg); |
| 429 | stream = bt_packet_borrow_stream_const(packet); |
| 430 | break; |
| 431 | case BT_MESSAGE_TYPE_PACKET_END: |
| 432 | packet = bt_message_packet_end_borrow_packet_const(msg); |
| 433 | stream = bt_packet_borrow_stream_const(packet); |
| 434 | break; |
| 435 | case BT_MESSAGE_TYPE_EVENT: |
| 436 | event = bt_message_event_borrow_event_const(msg); |
| 437 | stream = bt_event_borrow_stream_const(event); |
| 438 | break; |
| 439 | case BT_MESSAGE_TYPE_DISCARDED_EVENTS: |
| 440 | stream = bt_message_discarded_events_borrow_stream_const(msg); |
| 441 | break; |
| 442 | case BT_MESSAGE_TYPE_DISCARDED_PACKETS: |
| 443 | stream = bt_message_discarded_packets_borrow_stream_const(msg); |
| 444 | break; |
| 445 | case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY: |
| 446 | goto end; |
| 447 | default: |
| 448 | bt_common_abort(); |
| 449 | } |
| 450 | |
| 451 | end: |
| 452 | return stream; |
| 453 | } |
| 454 | |
| 455 | static |
| 456 | const bt_trace *borrow_trace(const bt_message *msg) |
| 457 | { |
| 458 | const bt_trace *trace = NULL; |
| 459 | const bt_stream *stream = NULL; |
| 460 | |
| 461 | stream = borrow_stream(msg); |
| 462 | if (stream) { |
| 463 | trace = bt_stream_borrow_trace_const(stream); |
| 464 | } |
| 465 | |
| 466 | return trace; |
| 467 | } |
| 468 | |
| 469 | static |
| 470 | int compare_messages_by_trace_name(struct messages_to_compare *msgs) |
| 471 | { |
| 472 | int ret = 0; |
| 473 | const char *left_trace_name = NULL, *right_trace_name = NULL; |
| 474 | |
| 475 | if (msgs->left.trace && !msgs->right.trace) { |
| 476 | ret = -1; |
| 477 | goto end; |
| 478 | } |
| 479 | |
| 480 | if (!msgs->left.trace && msgs->right.trace) { |
| 481 | ret = 1; |
| 482 | goto end; |
| 483 | } |
| 484 | |
| 485 | if (!msgs->left.trace && !msgs->right.trace) { |
| 486 | ret = 0; |
| 487 | goto end; |
| 488 | } |
| 489 | |
| 490 | left_trace_name = bt_trace_get_name(msgs->left.trace); |
| 491 | right_trace_name = bt_trace_get_name(msgs->right.trace); |
| 492 | |
| 493 | if (left_trace_name && !right_trace_name) { |
| 494 | ret = -1; |
| 495 | goto end; |
| 496 | } |
| 497 | |
| 498 | if (!left_trace_name && right_trace_name) { |
| 499 | ret = 1; |
| 500 | goto end; |
| 501 | } |
| 502 | |
| 503 | if (!left_trace_name && !right_trace_name) { |
| 504 | ret = 0; |
| 505 | goto end; |
| 506 | } |
| 507 | |
| 508 | ret = strcmp(left_trace_name, right_trace_name); |
| 509 | end: |
| 510 | return ret; |
| 511 | } |
| 512 | |
| 513 | static |
| 514 | int compare_messages_by_trace_uuid(struct messages_to_compare *msgs) |
| 515 | { |
| 516 | int ret = 0; |
| 517 | bt_uuid left_trace_uuid = NULL, right_trace_uuid = NULL; |
| 518 | |
| 519 | if (msgs->left.trace && !msgs->right.trace) { |
| 520 | ret = -1; |
| 521 | goto end; |
| 522 | } |
| 523 | |
| 524 | if (!msgs->left.trace && msgs->right.trace) { |
| 525 | ret = 1; |
| 526 | goto end; |
| 527 | } |
| 528 | |
| 529 | if (!msgs->left.trace && !msgs->right.trace) { |
| 530 | ret = 0; |
| 531 | goto end; |
| 532 | } |
| 533 | |
| 534 | left_trace_uuid = bt_trace_get_uuid(msgs->left.trace); |
| 535 | right_trace_uuid = bt_trace_get_uuid(msgs->right.trace); |
| 536 | |
| 537 | if (left_trace_uuid && !right_trace_uuid) { |
| 538 | ret = -1; |
| 539 | goto end; |
| 540 | } |
| 541 | |
| 542 | if (!left_trace_uuid && right_trace_uuid) { |
| 543 | ret = 1; |
| 544 | goto end; |
| 545 | } |
| 546 | |
| 547 | if (!left_trace_uuid && !right_trace_uuid) { |
| 548 | ret = 0; |
| 549 | goto end; |
| 550 | } |
| 551 | |
| 552 | ret = bt_uuid_compare(left_trace_uuid, right_trace_uuid); |
| 553 | end: |
| 554 | return ret; |
| 555 | } |
| 556 | |
| 557 | static |
| 558 | int compare_messages_by_stream_class_id(struct messages_to_compare *msgs) |
| 559 | { |
| 560 | int ret = 0; |
| 561 | uint64_t left_stream_class_id = 0, right_stream_class_id = 0; |
| 562 | |
| 563 | if (msgs->left.stream && !msgs->right.stream) { |
| 564 | ret = -1; |
| 565 | goto end; |
| 566 | } |
| 567 | |
| 568 | if (!msgs->left.stream && msgs->right.stream) { |
| 569 | ret = 1; |
| 570 | goto end; |
| 571 | } |
| 572 | |
| 573 | if (!msgs->left.stream && !msgs->right.stream) { |
| 574 | ret = 0; |
| 575 | goto end; |
| 576 | } |
| 577 | |
| 578 | left_stream_class_id = bt_stream_class_get_id( |
| 579 | bt_stream_borrow_class_const(msgs->left.stream)); |
| 580 | |
| 581 | right_stream_class_id = bt_stream_class_get_id( |
| 582 | bt_stream_borrow_class_const(msgs->right.stream)); |
| 583 | |
| 584 | if (left_stream_class_id == right_stream_class_id) { |
| 585 | ret = 0; |
| 586 | goto end; |
| 587 | } |
| 588 | |
| 589 | ret = (left_stream_class_id < right_stream_class_id) ? -1 : 1; |
| 590 | |
| 591 | end: |
| 592 | return ret; |
| 593 | } |
| 594 | |
| 595 | static |
| 596 | int compare_messages_by_stream_id(struct messages_to_compare *msgs) |
| 597 | { |
| 598 | int ret = 0; |
| 599 | uint64_t left_stream_id = 0, right_stream_id = 0; |
| 600 | |
| 601 | if (msgs->left.stream && !msgs->right.stream) { |
| 602 | ret = -1; |
| 603 | goto end; |
| 604 | } |
| 605 | |
| 606 | if (!msgs->left.stream && msgs->right.stream) { |
| 607 | ret = 1; |
| 608 | goto end; |
| 609 | } |
| 610 | |
| 611 | if (!msgs->left.stream && !msgs->right.stream) { |
| 612 | ret = 0; |
| 613 | goto end; |
| 614 | } |
| 615 | |
| 616 | left_stream_id = bt_stream_get_id(msgs->left.stream); |
| 617 | right_stream_id = bt_stream_get_id(msgs->right.stream); |
| 618 | |
| 619 | if (left_stream_id == right_stream_id) { |
| 620 | ret = 0; |
| 621 | goto end; |
| 622 | } |
| 623 | |
| 624 | ret = (left_stream_id < right_stream_id) ? -1 : 1; |
| 625 | |
| 626 | end: |
| 627 | return ret; |
| 628 | } |
| 629 | |
| 630 | static |
| 631 | int compare_messages_same_type(struct messages_to_compare *msgs) |
| 632 | { |
| 633 | int ret = 0; |
| 634 | |
| 635 | /* |
| 636 | * Both messages are of the same type, we must compare characterics of |
| 637 | * the messages such as the attributes of the event in a event message. |
| 638 | */ |
| 639 | BT_ASSERT(bt_message_get_type(msgs->left.msg) == |
| 640 | bt_message_get_type(msgs->right.msg)); |
| 641 | |
| 642 | switch (bt_message_get_type(msgs->left.msg)) { |
| 643 | case BT_MESSAGE_TYPE_STREAM_BEGINNING: |
| 644 | /* Fall-through */ |
| 645 | case BT_MESSAGE_TYPE_STREAM_END: |
| 646 | /* Fall-through */ |
| 647 | case BT_MESSAGE_TYPE_PACKET_BEGINNING: |
| 648 | /* Fall-through */ |
| 649 | case BT_MESSAGE_TYPE_PACKET_END: |
| 650 | ret = compare_streams(msgs->left.stream, msgs->right.stream); |
| 651 | if (ret) { |
| 652 | goto end; |
| 653 | } |
| 654 | |
| 655 | break; |
| 656 | case BT_MESSAGE_TYPE_EVENT: |
| 657 | { |
| 658 | const bt_event *left_event, *right_event; |
| 659 | left_event = bt_message_event_borrow_event_const(msgs->left.msg); |
| 660 | right_event = bt_message_event_borrow_event_const(msgs->right.msg); |
| 661 | |
| 662 | ret = compare_events(left_event, right_event); |
| 663 | if (ret) { |
| 664 | goto end; |
| 665 | } |
| 666 | |
| 667 | ret = compare_streams(msgs->left.stream, msgs->right.stream); |
| 668 | if (ret) { |
| 669 | goto end; |
| 670 | } |
| 671 | break; |
| 672 | } |
| 673 | case BT_MESSAGE_TYPE_DISCARDED_EVENTS: |
| 674 | { |
| 675 | const bt_stream_class *left_stream_class; |
| 676 | bt_property_availability left_event_count_avail, |
| 677 | right_event_count_avail; |
| 678 | uint64_t left_event_count, right_event_count; |
| 679 | |
| 680 | /* |
| 681 | * Compare streams first to check if there is a |
| 682 | * mismatch about discarded event related configuration |
| 683 | * in the stream class. |
| 684 | */ |
| 685 | ret = compare_streams(msgs->left.stream, msgs->right.stream); |
| 686 | if (ret) { |
| 687 | goto end; |
| 688 | } |
| 689 | |
| 690 | left_stream_class = bt_stream_borrow_class_const(msgs->left.stream); |
| 691 | if (bt_stream_class_discarded_events_have_default_clock_snapshots( |
| 692 | left_stream_class)) { |
| 693 | const bt_clock_snapshot *left_beg_cs = |
| 694 | bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(msgs->left.msg); |
| 695 | const bt_clock_snapshot *right_beg_cs = |
| 696 | bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(msgs->right.msg); |
| 697 | const bt_clock_snapshot *left_end_cs = |
| 698 | bt_message_discarded_events_borrow_end_default_clock_snapshot_const(msgs->left.msg); |
| 699 | const bt_clock_snapshot *right_end_cs = |
| 700 | bt_message_discarded_events_borrow_end_default_clock_snapshot_const(msgs->right.msg); |
| 701 | |
| 702 | ret = compare_clock_snapshots_and_clock_classes( |
| 703 | left_beg_cs, right_beg_cs); |
| 704 | if (ret) { |
| 705 | goto end; |
| 706 | } |
| 707 | |
| 708 | ret = compare_clock_snapshots_and_clock_classes( |
| 709 | left_end_cs, right_end_cs); |
| 710 | if (ret) { |
| 711 | goto end; |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | left_event_count_avail = |
| 716 | bt_message_discarded_events_get_count( |
| 717 | msgs->left.msg, &left_event_count); |
| 718 | right_event_count_avail = |
| 719 | bt_message_discarded_events_get_count( |
| 720 | msgs->right.msg, &right_event_count); |
| 721 | if (left_event_count_avail == BT_PROPERTY_AVAILABILITY_AVAILABLE && |
| 722 | right_event_count_avail == BT_PROPERTY_AVAILABILITY_AVAILABLE) { |
| 723 | ret = left_event_count - right_event_count; |
| 724 | if (ret != 0) { |
| 725 | goto end; |
| 726 | } |
| 727 | } else if (left_event_count_avail == BT_PROPERTY_AVAILABILITY_AVAILABLE && |
| 728 | right_event_count_avail == BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE) { |
| 729 | ret = -1; |
| 730 | goto end; |
| 731 | } else if (left_event_count_avail == BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE && |
| 732 | right_event_count_avail == BT_PROPERTY_AVAILABILITY_AVAILABLE) { |
| 733 | ret = 1; |
| 734 | goto end; |
| 735 | } |
| 736 | |
| 737 | break; |
| 738 | } |
| 739 | case BT_MESSAGE_TYPE_DISCARDED_PACKETS: |
| 740 | { |
| 741 | const bt_stream_class *left_stream_class; |
| 742 | bt_property_availability left_packet_count_avail, |
| 743 | right_packet_count_avail; |
| 744 | uint64_t left_packet_count, right_packet_count; |
| 745 | |
| 746 | /* |
| 747 | * Compare streams first to check if there is a |
| 748 | * mismatch about discarded packets related |
| 749 | * configuration in the stream class. |
| 750 | */ |
| 751 | ret = compare_streams(msgs->left.stream, msgs->right.stream); |
| 752 | if (ret) { |
| 753 | goto end; |
| 754 | } |
| 755 | |
| 756 | left_stream_class = bt_stream_borrow_class_const(msgs->left.stream); |
| 757 | |
| 758 | if (bt_stream_class_discarded_packets_have_default_clock_snapshots( |
| 759 | left_stream_class)) { |
| 760 | const bt_clock_snapshot *left_beg_cs = |
| 761 | bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(msgs->left.msg); |
| 762 | const bt_clock_snapshot *right_beg_cs = |
| 763 | bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(msgs->right.msg); |
| 764 | const bt_clock_snapshot *left_end_cs = |
| 765 | bt_message_discarded_packets_borrow_end_default_clock_snapshot_const(msgs->left.msg); |
| 766 | const bt_clock_snapshot *right_end_cs = |
| 767 | bt_message_discarded_packets_borrow_end_default_clock_snapshot_const(msgs->right.msg); |
| 768 | |
| 769 | ret = compare_clock_snapshots_and_clock_classes( |
| 770 | left_beg_cs, right_beg_cs); |
| 771 | if (ret) { |
| 772 | goto end; |
| 773 | } |
| 774 | |
| 775 | ret = compare_clock_snapshots_and_clock_classes( |
| 776 | left_end_cs, right_end_cs); |
| 777 | if (ret) { |
| 778 | goto end; |
| 779 | } |
| 780 | } |
| 781 | |
| 782 | left_packet_count_avail = bt_message_discarded_packets_get_count( |
| 783 | msgs->left.msg, &left_packet_count); |
| 784 | right_packet_count_avail = bt_message_discarded_packets_get_count( |
| 785 | msgs->right.msg, &right_packet_count); |
| 786 | if (left_packet_count_avail == BT_PROPERTY_AVAILABILITY_AVAILABLE && |
| 787 | right_packet_count_avail == BT_PROPERTY_AVAILABILITY_AVAILABLE) { |
| 788 | ret = left_packet_count - right_packet_count; |
| 789 | if (ret != 0) { |
| 790 | goto end; |
| 791 | } |
| 792 | } else if (left_packet_count_avail == BT_PROPERTY_AVAILABILITY_AVAILABLE && |
| 793 | right_packet_count_avail == BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE) { |
| 794 | ret = -1; |
| 795 | goto end; |
| 796 | } else if (left_packet_count_avail == BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE && |
| 797 | right_packet_count_avail == BT_PROPERTY_AVAILABILITY_AVAILABLE) { |
| 798 | ret = 1; |
| 799 | goto end; |
| 800 | } |
| 801 | |
| 802 | break; |
| 803 | } |
| 804 | case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY: |
| 805 | { |
| 806 | const bt_clock_snapshot *left_cs = |
| 807 | bt_message_message_iterator_inactivity_borrow_clock_snapshot_const(msgs->left.msg); |
| 808 | const bt_clock_snapshot *right_cs = |
| 809 | bt_message_message_iterator_inactivity_borrow_clock_snapshot_const(msgs->right.msg); |
| 810 | |
| 811 | ret = compare_clock_snapshots_and_clock_classes( |
| 812 | left_cs, right_cs); |
| 813 | if (ret != 0) { |
| 814 | goto end; |
| 815 | } |
| 816 | |
| 817 | break; |
| 818 | } |
| 819 | default: |
| 820 | bt_common_abort(); |
| 821 | } |
| 822 | |
| 823 | end: |
| 824 | return ret; |
| 825 | } |
| 826 | |
| 827 | int common_muxing_compare_messages(const bt_message *left_msg, |
| 828 | const bt_message *right_msg) |
| 829 | { |
| 830 | int ret = 0; |
| 831 | struct messages_to_compare msgs; |
| 832 | |
| 833 | BT_ASSERT(left_msg != right_msg); |
| 834 | |
| 835 | msgs.left.msg = left_msg; |
| 836 | msgs.left.trace = borrow_trace(left_msg); |
| 837 | msgs.left.stream = borrow_stream(left_msg); |
| 838 | |
| 839 | msgs.right.msg = right_msg; |
| 840 | msgs.right.trace = borrow_trace(right_msg); |
| 841 | msgs.right.stream = borrow_stream(right_msg); |
| 842 | |
| 843 | /* Same timestamp: compare trace UUIDs. */ |
| 844 | ret = compare_messages_by_trace_uuid(&msgs); |
| 845 | if (ret) { |
| 846 | goto end; |
| 847 | } |
| 848 | |
| 849 | /* Same timestamp and trace UUID: compare trace names. */ |
| 850 | ret = compare_messages_by_trace_name(&msgs); |
| 851 | if (ret) { |
| 852 | goto end; |
| 853 | } |
| 854 | |
| 855 | /* |
| 856 | * Same timestamp, trace name, and trace UUID: compare stream class |
| 857 | * IDs. |
| 858 | */ |
| 859 | ret = compare_messages_by_stream_class_id(&msgs); |
| 860 | if (ret) { |
| 861 | goto end; |
| 862 | } |
| 863 | |
| 864 | /* |
| 865 | * Same timestamp, trace name, trace UUID, and stream class ID: compare |
| 866 | * stream IDs. |
| 867 | */ |
| 868 | ret = compare_messages_by_stream_id(&msgs); |
| 869 | if (ret) { |
| 870 | goto end; |
| 871 | } |
| 872 | |
| 873 | if (bt_message_get_type(msgs.left.msg) != |
| 874 | bt_message_get_type(msgs.right.msg)) { |
| 875 | /* |
| 876 | * The messages are of different type, we order (arbitrarily) |
| 877 | * in the following way: |
| 878 | * SB < PB < EV < DE < MI < PE < DP < SE |
| 879 | */ |
| 880 | ret = compare_messages_by_type(&msgs); |
| 881 | if (ret) { |
| 882 | goto end; |
| 883 | } |
| 884 | } else { |
| 885 | /* The messages are of the same type. */ |
| 886 | ret = compare_messages_same_type(&msgs); |
| 887 | if (ret) { |
| 888 | goto end; |
| 889 | } |
| 890 | } |
| 891 | |
| 892 | end: |
| 893 | return ret; |
| 894 | } |