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