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