plugins/common/muxing: split `compare_clock_snapshots_and_clock_classes()`
[babeltrace.git] / src / plugins / common / muxing / muxing.c
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 deterministic way based on their
70 * types.
71 * Returns -1 is left message must go first
72 * Returns 1 is right message 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
165 static
166 int compare_clock_classes(const bt_clock_class *left_cc,
167 const bt_clock_class *right_cc)
168 {
169 int ret;
170 const char *left_clock_class_name, *right_clock_class_name;
171 bt_uuid left_clock_class_uuid, right_clock_class_uuid;
172 uint64_t left_freq, right_freq, left_prec, right_prec;
173
174 left_clock_class_uuid = bt_clock_class_get_uuid(left_cc);
175 right_clock_class_uuid = bt_clock_class_get_uuid(right_cc);
176
177 if (left_clock_class_uuid && !right_clock_class_uuid) {
178 ret = -1;
179 goto end;
180 } else if (!left_clock_class_uuid && right_clock_class_uuid) {
181 ret = 1;
182 goto end;
183 } else if (left_clock_class_uuid && right_clock_class_uuid) {
184 ret = bt_uuid_compare(left_clock_class_uuid,
185 right_clock_class_uuid);
186 if (ret != 0) {
187 goto end;
188 }
189 }
190
191
192 left_clock_class_name = bt_clock_class_get_name(left_cc);
193 right_clock_class_name = bt_clock_class_get_name(right_cc);
194
195 if (left_clock_class_name && !right_clock_class_name) {
196 ret = -1;
197 goto end;
198 } else if (!left_clock_class_name && right_clock_class_name) {
199 ret = 1;
200 goto end;
201 } else if (left_clock_class_name && right_clock_class_name) {
202 ret = strcmp(left_clock_class_name, right_clock_class_name);
203 if (ret != 0) {
204 goto end;
205 }
206 }
207
208 left_freq = bt_clock_class_get_frequency(left_cc);
209 right_freq = bt_clock_class_get_frequency(right_cc);
210
211 ret = right_freq - left_freq;
212 if (ret != 0) {
213 goto end;
214 }
215
216 left_prec = bt_clock_class_get_precision(left_cc);
217 right_prec = bt_clock_class_get_precision(right_cc);
218
219 ret = right_prec - left_prec;
220 if (ret != 0) {
221 goto end;
222 }
223
224 end:
225 return ret;
226 }
227
228 static
229 int compare_streams(const bt_stream *left_stream, const bt_stream *right_stream)
230 {
231 int ret = 0;
232 const char *left_stream_name, *right_stream_name,
233 *left_stream_class_name, *right_stream_class_name;
234 const bt_stream_class *left_stream_class, *right_stream_class;
235
236 /*
237 * No need to compare stream id as it was checked earlier and if we are
238 * here it means they are identical or both absent.
239 */
240 BT_ASSERT_DBG(bt_stream_get_id(left_stream) ==
241 bt_stream_get_id(right_stream));
242
243 /* Compare stream name. */
244 left_stream_name = bt_stream_get_name(left_stream);
245 right_stream_name = bt_stream_get_name(right_stream);
246
247 if (left_stream_name && right_stream_name) {
248 ret = strcmp(left_stream_name, right_stream_name);
249 if (ret != 0) {
250 goto end;
251 }
252 } else if (!left_stream_name && right_stream_name) {
253 ret = -1;
254 goto end;
255 } else if (left_stream_name && !right_stream_name) {
256 ret = 1;
257 goto end;
258 }
259
260 left_stream_class = bt_stream_borrow_class_const(left_stream);
261 right_stream_class = bt_stream_borrow_class_const(right_stream);
262
263 /*
264 * No need to compare stream class id as it was checked earlier and if
265 * we are here it means they are identical.
266 */
267 BT_ASSERT_DBG(bt_stream_class_get_id(left_stream_class) ==
268 bt_stream_class_get_id(right_stream_class));
269
270 /* Compare stream class name. */
271 left_stream_class_name = bt_stream_class_get_name(left_stream_class);
272 right_stream_class_name = bt_stream_class_get_name(right_stream_class);
273
274 if (left_stream_class_name && right_stream_class_name) {
275 ret = strcmp(left_stream_class_name, right_stream_class_name);
276 if (ret != 0) {
277 goto end;
278 }
279 } else if (!left_stream_class_name && right_stream_class_name) {
280 ret = -1;
281 goto end;
282 } else if (left_stream_class_name && !right_stream_class_name) {
283 ret = 1;
284 goto end;
285 }
286
287 /* Compare stream class automatic event class id assignment. */
288 if (bt_stream_class_assigns_automatic_event_class_id(left_stream_class) &&
289 !bt_stream_class_assigns_automatic_event_class_id(right_stream_class)) {
290 ret = 1;
291 goto end;
292 } else if (!bt_stream_class_assigns_automatic_event_class_id(left_stream_class) &&
293 bt_stream_class_assigns_automatic_event_class_id(right_stream_class)) {
294 ret = -1;
295 goto end;
296 }
297
298 /* Compare stream class automatic stream id assignment. */
299 if (bt_stream_class_assigns_automatic_stream_id(left_stream_class) &&
300 !bt_stream_class_assigns_automatic_stream_id(right_stream_class)) {
301 ret = 1;
302 goto end;
303 } else if (!bt_stream_class_assigns_automatic_stream_id(left_stream_class) &&
304 bt_stream_class_assigns_automatic_stream_id(right_stream_class)) {
305 ret = -1;
306 goto end;
307 }
308
309 /* Compare stream class support of discarded events. */
310 if (bt_stream_class_supports_discarded_events(left_stream_class) &&
311 !bt_stream_class_supports_discarded_events(right_stream_class)) {
312 ret = 1;
313 goto end;
314 } else if (!bt_stream_class_supports_discarded_events(left_stream_class) &&
315 bt_stream_class_supports_discarded_events(right_stream_class)) {
316 ret = -1;
317 goto end;
318 }
319
320 /* Compare stream class discarded events default clock snapshot. */
321 if (bt_stream_class_discarded_events_have_default_clock_snapshots(left_stream_class) &&
322 !bt_stream_class_discarded_events_have_default_clock_snapshots(right_stream_class)) {
323 ret = 1;
324 goto end;
325 } else if (!bt_stream_class_discarded_events_have_default_clock_snapshots(left_stream_class) &&
326 bt_stream_class_discarded_events_have_default_clock_snapshots(right_stream_class)) {
327 ret = -1;
328 goto end;
329 }
330
331 /* Compare stream class support of packets. */
332 if (bt_stream_class_supports_packets(left_stream_class) &&
333 !bt_stream_class_supports_packets(right_stream_class)) {
334 ret = 1;
335 goto end;
336 } else if (!bt_stream_class_supports_packets(left_stream_class) &&
337 bt_stream_class_supports_packets(right_stream_class)) {
338 ret = -1;
339 goto end;
340 }
341
342 if (!bt_stream_class_supports_packets(left_stream_class)) {
343 /* Skip all packet related checks. */
344 goto end;
345 }
346
347 /*
348 * Compare stream class presence of discarded packets beginning default
349 * clock snapshot.
350 */
351 if (bt_stream_class_packets_have_beginning_default_clock_snapshot(left_stream_class) &&
352 !bt_stream_class_packets_have_beginning_default_clock_snapshot(right_stream_class)) {
353 ret = 1;
354 goto end;
355 } else if (!bt_stream_class_packets_have_beginning_default_clock_snapshot(left_stream_class) &&
356 bt_stream_class_packets_have_beginning_default_clock_snapshot(right_stream_class)) {
357 ret = -1;
358 goto end;
359 }
360
361 /*
362 * Compare stream class presence of discarded packets end default clock
363 * snapshot.
364 */
365 if (bt_stream_class_packets_have_end_default_clock_snapshot(left_stream_class) &&
366 !bt_stream_class_packets_have_end_default_clock_snapshot(right_stream_class)) {
367 ret = 1;
368 goto end;
369 } else if (!bt_stream_class_packets_have_end_default_clock_snapshot(left_stream_class) &&
370 bt_stream_class_packets_have_end_default_clock_snapshot(right_stream_class)) {
371 ret = -1;
372 goto end;
373 }
374
375 /* Compare stream class support of discarded packets. */
376 if (bt_stream_class_supports_discarded_packets(left_stream_class) &&
377 !bt_stream_class_supports_discarded_packets(right_stream_class)) {
378 ret = 1;
379 goto end;
380 } else if (!bt_stream_class_supports_discarded_packets(left_stream_class) &&
381 bt_stream_class_supports_discarded_packets(right_stream_class)) {
382 ret = -1;
383 goto end;
384 }
385
386 /* Compare stream class discarded packets default clock snapshot. */
387 if (bt_stream_class_discarded_packets_have_default_clock_snapshots(left_stream_class) &&
388 !bt_stream_class_discarded_packets_have_default_clock_snapshots(right_stream_class)) {
389 ret = 1;
390 goto end;
391 } else if (!bt_stream_class_discarded_packets_have_default_clock_snapshots(left_stream_class) &&
392 bt_stream_class_discarded_packets_have_default_clock_snapshots(right_stream_class)) {
393 ret = -1;
394 goto end;
395 }
396
397 end:
398 return ret;
399 }
400
401 static
402 int compare_clock_snapshots(const bt_clock_snapshot *left_cs,
403 const bt_clock_snapshot *right_cs)
404 {
405 uint64_t left_cs_value = bt_clock_snapshot_get_value(left_cs);
406 uint64_t right_cs_value = bt_clock_snapshot_get_value(right_cs);
407
408 return left_cs_value - right_cs_value;
409 }
410
411 static
412 const bt_stream *borrow_stream(const bt_message *msg)
413 {
414 bt_message_type msg_type = bt_message_get_type(msg);
415 const bt_stream *stream = NULL;
416 const bt_packet *packet = NULL;
417 const bt_event *event = NULL;
418
419 switch (msg_type) {
420 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
421 stream = bt_message_stream_beginning_borrow_stream_const(msg);
422 break;
423 case BT_MESSAGE_TYPE_STREAM_END:
424 stream = bt_message_stream_end_borrow_stream_const(msg);
425 break;
426 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
427 packet = bt_message_packet_beginning_borrow_packet_const(msg);
428 stream = bt_packet_borrow_stream_const(packet);
429 break;
430 case BT_MESSAGE_TYPE_PACKET_END:
431 packet = bt_message_packet_end_borrow_packet_const(msg);
432 stream = bt_packet_borrow_stream_const(packet);
433 break;
434 case BT_MESSAGE_TYPE_EVENT:
435 event = bt_message_event_borrow_event_const(msg);
436 stream = bt_event_borrow_stream_const(event);
437 break;
438 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
439 stream = bt_message_discarded_events_borrow_stream_const(msg);
440 break;
441 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
442 stream = bt_message_discarded_packets_borrow_stream_const(msg);
443 break;
444 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY:
445 goto end;
446 default:
447 bt_common_abort();
448 }
449
450 end:
451 return stream;
452 }
453
454 static
455 const bt_trace *borrow_trace(const bt_message *msg)
456 {
457 const bt_trace *trace = NULL;
458 const bt_stream *stream = NULL;
459
460 stream = borrow_stream(msg);
461 if (stream) {
462 trace = bt_stream_borrow_trace_const(stream);
463 }
464
465 return trace;
466 }
467
468 static
469 int compare_messages_by_trace_name(struct messages_to_compare *msgs)
470 {
471 int ret = 0;
472 const char *left_trace_name = NULL, *right_trace_name = NULL;
473
474 if (msgs->left.trace && !msgs->right.trace) {
475 ret = -1;
476 goto end;
477 }
478
479 if (!msgs->left.trace && msgs->right.trace) {
480 ret = 1;
481 goto end;
482 }
483
484 if (!msgs->left.trace && !msgs->right.trace) {
485 ret = 0;
486 goto end;
487 }
488
489 left_trace_name = bt_trace_get_name(msgs->left.trace);
490 right_trace_name = bt_trace_get_name(msgs->right.trace);
491
492 if (left_trace_name && !right_trace_name) {
493 ret = -1;
494 goto end;
495 }
496
497 if (!left_trace_name && right_trace_name) {
498 ret = 1;
499 goto end;
500 }
501
502 if (!left_trace_name && !right_trace_name) {
503 ret = 0;
504 goto end;
505 }
506
507 ret = strcmp(left_trace_name, right_trace_name);
508 end:
509 return ret;
510 }
511
512 static
513 int compare_messages_by_trace_uuid(struct messages_to_compare *msgs)
514 {
515 int ret = 0;
516 bt_uuid left_trace_uuid = NULL, right_trace_uuid = NULL;
517
518 if (msgs->left.trace && !msgs->right.trace) {
519 ret = -1;
520 goto end;
521 }
522
523 if (!msgs->left.trace && msgs->right.trace) {
524 ret = 1;
525 goto end;
526 }
527
528 if (!msgs->left.trace && !msgs->right.trace) {
529 ret = 0;
530 goto end;
531 }
532
533 left_trace_uuid = bt_trace_get_uuid(msgs->left.trace);
534 right_trace_uuid = bt_trace_get_uuid(msgs->right.trace);
535
536 if (left_trace_uuid && !right_trace_uuid) {
537 ret = -1;
538 goto end;
539 }
540
541 if (!left_trace_uuid && right_trace_uuid) {
542 ret = 1;
543 goto end;
544 }
545
546 if (!left_trace_uuid && !right_trace_uuid) {
547 ret = 0;
548 goto end;
549 }
550
551 ret = bt_uuid_compare(left_trace_uuid, right_trace_uuid);
552 end:
553 return ret;
554 }
555
556 static
557 int compare_messages_by_stream_class_id(struct messages_to_compare *msgs)
558 {
559 int ret = 0;
560 uint64_t left_stream_class_id = 0, right_stream_class_id = 0;
561
562 if (msgs->left.stream && !msgs->right.stream) {
563 ret = -1;
564 goto end;
565 }
566
567 if (!msgs->left.stream && msgs->right.stream) {
568 ret = 1;
569 goto end;
570 }
571
572 if (!msgs->left.stream && !msgs->right.stream) {
573 ret = 0;
574 goto end;
575 }
576
577 left_stream_class_id = bt_stream_class_get_id(
578 bt_stream_borrow_class_const(msgs->left.stream));
579
580 right_stream_class_id = bt_stream_class_get_id(
581 bt_stream_borrow_class_const(msgs->right.stream));
582
583 if (left_stream_class_id == right_stream_class_id) {
584 ret = 0;
585 goto end;
586 }
587
588 ret = (left_stream_class_id < right_stream_class_id) ? -1 : 1;
589
590 end:
591 return ret;
592 }
593
594 static
595 int compare_messages_by_stream_id(struct messages_to_compare *msgs)
596 {
597 int ret = 0;
598 uint64_t left_stream_id = 0, right_stream_id = 0;
599
600 if (msgs->left.stream && !msgs->right.stream) {
601 ret = -1;
602 goto end;
603 }
604
605 if (!msgs->left.stream && msgs->right.stream) {
606 ret = 1;
607 goto end;
608 }
609
610 if (!msgs->left.stream && !msgs->right.stream) {
611 ret = 0;
612 goto end;
613 }
614
615 left_stream_id = bt_stream_get_id(msgs->left.stream);
616 right_stream_id = bt_stream_get_id(msgs->right.stream);
617
618 if (left_stream_id == right_stream_id) {
619 ret = 0;
620 goto end;
621 }
622
623 ret = (left_stream_id < right_stream_id) ? -1 : 1;
624
625 end:
626 return ret;
627 }
628
629 static
630 int compare_messages_same_type(struct messages_to_compare *msgs)
631 {
632 int ret = 0;
633
634 /*
635 * Both messages are of the same type, we must compare characterics of
636 * the messages such as the attributes of the event in a event message.
637 */
638 BT_ASSERT_DBG(bt_message_get_type(msgs->left.msg) ==
639 bt_message_get_type(msgs->right.msg));
640
641 switch (bt_message_get_type(msgs->left.msg)) {
642 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
643 /* Fall-through */
644 case BT_MESSAGE_TYPE_STREAM_END:
645 /* Fall-through */
646 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
647 /* Fall-through */
648 case BT_MESSAGE_TYPE_PACKET_END:
649 ret = compare_streams(msgs->left.stream, msgs->right.stream);
650 if (ret) {
651 goto end;
652 }
653
654 break;
655 case BT_MESSAGE_TYPE_EVENT:
656 {
657 const bt_event *left_event, *right_event;
658 left_event = bt_message_event_borrow_event_const(msgs->left.msg);
659 right_event = bt_message_event_borrow_event_const(msgs->right.msg);
660
661 ret = compare_events(left_event, right_event);
662 if (ret) {
663 goto end;
664 }
665
666 ret = compare_streams(msgs->left.stream, msgs->right.stream);
667 if (ret) {
668 goto end;
669 }
670 break;
671 }
672 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
673 {
674 const bt_stream_class *left_stream_class;
675 bt_property_availability left_event_count_avail,
676 right_event_count_avail;
677 uint64_t left_event_count, right_event_count;
678
679 /*
680 * Compare streams first to check if there is a
681 * mismatch about discarded event related configuration
682 * in the stream class.
683 */
684 ret = compare_streams(msgs->left.stream, msgs->right.stream);
685 if (ret) {
686 goto end;
687 }
688
689 left_stream_class = bt_stream_borrow_class_const(msgs->left.stream);
690 if (bt_stream_class_discarded_events_have_default_clock_snapshots(
691 left_stream_class)) {
692 const bt_clock_snapshot *left_beg_cs =
693 bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(msgs->left.msg);
694 const bt_clock_snapshot *right_beg_cs =
695 bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(msgs->right.msg);
696 const bt_clock_snapshot *left_end_cs =
697 bt_message_discarded_events_borrow_end_default_clock_snapshot_const(msgs->left.msg);
698 const bt_clock_snapshot *right_end_cs =
699 bt_message_discarded_events_borrow_end_default_clock_snapshot_const(msgs->right.msg);
700
701 ret = compare_clock_snapshots(left_beg_cs, right_beg_cs);
702 if (ret) {
703 goto end;
704 }
705
706 ret = compare_clock_snapshots(left_end_cs, right_end_cs);
707 if (ret) {
708 goto end;
709 }
710
711 ret = compare_clock_classes(
712 bt_clock_snapshot_borrow_clock_class_const(left_beg_cs),
713 bt_clock_snapshot_borrow_clock_class_const(right_beg_cs));
714 if (ret != 0) {
715 goto end;
716 }
717 }
718
719 left_event_count_avail =
720 bt_message_discarded_events_get_count(
721 msgs->left.msg, &left_event_count);
722 right_event_count_avail =
723 bt_message_discarded_events_get_count(
724 msgs->right.msg, &right_event_count);
725 if (left_event_count_avail == BT_PROPERTY_AVAILABILITY_AVAILABLE &&
726 right_event_count_avail == BT_PROPERTY_AVAILABILITY_AVAILABLE) {
727 ret = left_event_count - right_event_count;
728 if (ret != 0) {
729 goto end;
730 }
731 } else if (left_event_count_avail == BT_PROPERTY_AVAILABILITY_AVAILABLE &&
732 right_event_count_avail == BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE) {
733 ret = -1;
734 goto end;
735 } else if (left_event_count_avail == BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE &&
736 right_event_count_avail == BT_PROPERTY_AVAILABILITY_AVAILABLE) {
737 ret = 1;
738 goto end;
739 }
740
741 break;
742 }
743 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
744 {
745 const bt_stream_class *left_stream_class;
746 bt_property_availability left_packet_count_avail,
747 right_packet_count_avail;
748 uint64_t left_packet_count, right_packet_count;
749
750 /*
751 * Compare streams first to check if there is a
752 * mismatch about discarded packets related
753 * configuration in the stream class.
754 */
755 ret = compare_streams(msgs->left.stream, msgs->right.stream);
756 if (ret) {
757 goto end;
758 }
759
760 left_stream_class = bt_stream_borrow_class_const(msgs->left.stream);
761
762 if (bt_stream_class_discarded_packets_have_default_clock_snapshots(
763 left_stream_class)) {
764 const bt_clock_snapshot *left_beg_cs =
765 bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(msgs->left.msg);
766 const bt_clock_snapshot *right_beg_cs =
767 bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(msgs->right.msg);
768 const bt_clock_snapshot *left_end_cs =
769 bt_message_discarded_packets_borrow_end_default_clock_snapshot_const(msgs->left.msg);
770 const bt_clock_snapshot *right_end_cs =
771 bt_message_discarded_packets_borrow_end_default_clock_snapshot_const(msgs->right.msg);
772
773 ret = compare_clock_snapshots(left_beg_cs, right_beg_cs);
774 if (ret) {
775 goto end;
776 }
777
778 ret = compare_clock_snapshots(left_end_cs, right_end_cs);
779 if (ret) {
780 goto end;
781 }
782
783 ret = compare_clock_classes(
784 bt_clock_snapshot_borrow_clock_class_const(left_beg_cs),
785 bt_clock_snapshot_borrow_clock_class_const(right_beg_cs));
786 if (ret != 0) {
787 goto end;
788 }
789 }
790
791 left_packet_count_avail = bt_message_discarded_packets_get_count(
792 msgs->left.msg, &left_packet_count);
793 right_packet_count_avail = bt_message_discarded_packets_get_count(
794 msgs->right.msg, &right_packet_count);
795 if (left_packet_count_avail == BT_PROPERTY_AVAILABILITY_AVAILABLE &&
796 right_packet_count_avail == BT_PROPERTY_AVAILABILITY_AVAILABLE) {
797 ret = left_packet_count - right_packet_count;
798 if (ret != 0) {
799 goto end;
800 }
801 } else if (left_packet_count_avail == BT_PROPERTY_AVAILABILITY_AVAILABLE &&
802 right_packet_count_avail == BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE) {
803 ret = -1;
804 goto end;
805 } else if (left_packet_count_avail == BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE &&
806 right_packet_count_avail == BT_PROPERTY_AVAILABILITY_AVAILABLE) {
807 ret = 1;
808 goto end;
809 }
810
811 break;
812 }
813 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY:
814 {
815 const bt_clock_snapshot *left_cs =
816 bt_message_message_iterator_inactivity_borrow_clock_snapshot_const(msgs->left.msg);
817 const bt_clock_snapshot *right_cs =
818 bt_message_message_iterator_inactivity_borrow_clock_snapshot_const(msgs->right.msg);
819
820 ret = compare_clock_snapshots(left_cs, right_cs);
821 if (ret != 0) {
822 goto end;
823 }
824
825 ret = compare_clock_classes(
826 bt_clock_snapshot_borrow_clock_class_const(left_cs),
827 bt_clock_snapshot_borrow_clock_class_const(right_cs));
828 if (ret != 0) {
829 goto end;
830 }
831
832 break;
833 }
834 default:
835 bt_common_abort();
836 }
837
838 end:
839 return ret;
840 }
841
842 int common_muxing_compare_messages(const bt_message *left_msg,
843 const bt_message *right_msg)
844 {
845 int ret = 0;
846 struct messages_to_compare msgs;
847
848 BT_ASSERT_DBG(left_msg != right_msg);
849
850 msgs.left.msg = left_msg;
851 msgs.left.trace = borrow_trace(left_msg);
852 msgs.left.stream = borrow_stream(left_msg);
853
854 msgs.right.msg = right_msg;
855 msgs.right.trace = borrow_trace(right_msg);
856 msgs.right.stream = borrow_stream(right_msg);
857
858 /* Same timestamp: compare trace UUIDs. */
859 ret = compare_messages_by_trace_uuid(&msgs);
860 if (ret) {
861 goto end;
862 }
863
864 /* Same timestamp and trace UUID: compare trace names. */
865 ret = compare_messages_by_trace_name(&msgs);
866 if (ret) {
867 goto end;
868 }
869
870 /*
871 * Same timestamp, trace name, and trace UUID: compare stream class
872 * IDs.
873 */
874 ret = compare_messages_by_stream_class_id(&msgs);
875 if (ret) {
876 goto end;
877 }
878
879 /*
880 * Same timestamp, trace name, trace UUID, and stream class ID: compare
881 * stream IDs.
882 */
883 ret = compare_messages_by_stream_id(&msgs);
884 if (ret) {
885 goto end;
886 }
887
888 if (bt_message_get_type(msgs.left.msg) !=
889 bt_message_get_type(msgs.right.msg)) {
890 /*
891 * The messages are of different type, we order (arbitrarily)
892 * in the following way:
893 * SB < PB < EV < DE < MI < PE < DP < SE
894 */
895 ret = compare_messages_by_type(&msgs);
896 if (ret) {
897 goto end;
898 }
899 } else {
900 /* The messages are of the same type. */
901 ret = compare_messages_same_type(&msgs);
902 if (ret) {
903 goto end;
904 }
905 }
906
907 end:
908 return ret;
909 }
This page took 0.048646 seconds and 4 git commands to generate.