Split CTF IR and CTF writer APIs and implementations
[babeltrace.git] / lib / ctf-ir / clock-class.c
1 /*
2 * clock-class.c
3 *
4 * Babeltrace CTF IR - Clock class
5 *
6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
7 *
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 */
28
29 #define BT_LOG_TAG "CLOCK-CLASS"
30 #include <babeltrace/lib-logging-internal.h>
31
32 #include <babeltrace/compat/uuid-internal.h>
33 #include <babeltrace/ctf-ir/clock-class-internal.h>
34 #include <babeltrace/ctf-ir/clock-value-internal.h>
35 #include <babeltrace/ctf-ir/utils.h>
36 #include <babeltrace/ref.h>
37 #include <babeltrace/compiler-internal.h>
38 #include <babeltrace/types.h>
39 #include <babeltrace/compat/string-internal.h>
40 #include <inttypes.h>
41 #include <babeltrace/object-internal.h>
42 #include <babeltrace/assert-internal.h>
43
44 static
45 void bt_clock_class_destroy(struct bt_object *obj);
46
47 BT_HIDDEN
48 bt_bool bt_clock_class_is_valid(struct bt_clock_class *clock_class)
49 {
50 return clock_class && clock_class->name;
51 }
52
53 int bt_clock_class_set_name(struct bt_clock_class *clock_class,
54 const char *name)
55 {
56 int ret = 0;
57
58 if (!clock_class) {
59 BT_LOGW_STR("Invalid parameter: clock class is NULL.");
60 ret = -1;
61 goto end;
62 }
63
64 if (clock_class->frozen) {
65 BT_LOGW("Invalid parameter: clock class is frozen: addr=%p, name=\"%s\"",
66 clock_class, bt_clock_class_get_name(clock_class));
67 ret = -1;
68 goto end;
69 }
70
71 if (!bt_identifier_is_valid(name)) {
72 BT_LOGW("Clock class's name is not a valid CTF identifier: "
73 "addr=%p, name=\"%s\"",
74 clock_class, name);
75 ret = -1;
76 goto end;
77 }
78
79 if (clock_class->name) {
80 g_string_assign(clock_class->name, name);
81 } else {
82 clock_class->name = g_string_new(name);
83 if (!clock_class->name) {
84 BT_LOGE_STR("Failed to allocate a GString.");
85 ret = -1;
86 goto end;
87 }
88 }
89
90 BT_LOGV("Set clock class's name: addr=%p, name=\"%s\"",
91 clock_class, name);
92
93 end:
94 return ret;
95 }
96
97 static
98 bool validate_freq(struct bt_clock_class *clock_class,
99 const char *name, uint64_t freq)
100 {
101 bool is_valid = true;
102
103 if (freq == -1ULL || freq == 0) {
104 BT_LOGW("Invalid parameter: frequency is invalid: "
105 "addr=%p, name=\"%s\", freq=%" PRIu64,
106 clock_class, name, freq);
107 is_valid = false;
108 goto end;
109 }
110
111 end:
112 return is_valid;
113 }
114
115 struct bt_clock_class *bt_clock_class_create(const char *name,
116 uint64_t freq)
117 {
118 int ret;
119 struct bt_clock_class *clock_class = NULL;
120
121 BT_LOGD("Creating default clock class object: name=\"%s\"",
122 name);
123
124 if (!validate_freq(NULL, name, freq)) {
125 /* validate_freq() logs errors */
126 goto error;
127 }
128
129 clock_class = g_new0(struct bt_clock_class, 1);
130 if (!clock_class) {
131 BT_LOGE_STR("Failed to allocate one clock class.");
132 goto error;
133 }
134
135 clock_class->precision = 1;
136 clock_class->frequency = freq;
137 bt_object_init(clock_class, bt_clock_class_destroy);
138
139 if (name) {
140 ret = bt_clock_class_set_name(clock_class, name);
141 if (ret) {
142 /* bt_clock_class_set_name() logs errors */
143 goto error;
144 }
145 }
146
147 BT_LOGD("Created clock class object: addr=%p, name=\"%s\"",
148 clock_class, name);
149 return clock_class;
150 error:
151 BT_PUT(clock_class);
152 return clock_class;
153 }
154
155 const char *bt_clock_class_get_name(struct bt_clock_class *clock_class)
156 {
157 const char *ret = NULL;
158
159 if (!clock_class) {
160 BT_LOGW_STR("Invalid parameter: clock class is NULL.");
161 goto end;
162 }
163
164 if (clock_class->name) {
165 ret = clock_class->name->str;
166 }
167
168 end:
169 return ret;
170 }
171
172 const char *bt_clock_class_get_description(
173 struct bt_clock_class *clock_class)
174 {
175 const char *ret = NULL;
176
177 if (!clock_class) {
178 BT_LOGW_STR("Invalid parameter: clock class is NULL.");
179 goto end;
180 }
181
182 if (clock_class->description) {
183 ret = clock_class->description->str;
184 }
185 end:
186 return ret;
187 }
188
189 int bt_clock_class_set_description(struct bt_clock_class *clock_class,
190 const char *desc)
191 {
192 int ret = 0;
193
194 if (!clock_class || !desc) {
195 BT_LOGW("Invalid parameter: clock class or description is NULL: "
196 "clock-class-addr=%p, name=\"%s\", desc-addr=%p",
197 clock_class, bt_clock_class_get_name(clock_class),
198 desc);
199 ret = -1;
200 goto end;
201 }
202
203 if (clock_class->frozen) {
204 BT_LOGW("Invalid parameter: clock class is frozen: addr=%p, name=\"%s\"",
205 clock_class, bt_clock_class_get_name(clock_class));
206 ret = -1;
207 goto end;
208 }
209
210 clock_class->description = g_string_new(desc);
211 ret = clock_class->description ? 0 : -1;
212 BT_LOGV("Set clock class's description: addr=%p, "
213 "name=\"%s\", desc=\"%s\"",
214 clock_class, bt_clock_class_get_name(clock_class), desc);
215 end:
216 return ret;
217 }
218
219 uint64_t bt_clock_class_get_frequency(
220 struct bt_clock_class *clock_class)
221 {
222 uint64_t ret = -1ULL;
223
224 if (!clock_class) {
225 BT_LOGW_STR("Invalid parameter: clock class is NULL.");
226 goto end;
227 }
228
229 ret = clock_class->frequency;
230 end:
231 return ret;
232 }
233
234 int bt_clock_class_set_frequency(struct bt_clock_class *clock_class,
235 uint64_t freq)
236 {
237 int ret = 0;
238
239 if (!clock_class) {
240 BT_LOGW("Invalid parameter: clock class is NULL or frequency is invalid: "
241 "addr=%p, name=\"%s\"",
242 clock_class, bt_clock_class_get_name(clock_class));
243 ret = -1;
244 goto end;
245 }
246
247 if (!validate_freq(clock_class, bt_clock_class_get_name(clock_class),
248 freq)) {
249 /* validate_freq() logs errors */
250 goto end;
251 }
252
253 if (clock_class->frozen) {
254 BT_LOGW("Invalid parameter: clock class is frozen: addr=%p, name=\"%s\"",
255 clock_class, bt_clock_class_get_name(clock_class));
256 ret = -1;
257 goto end;
258 }
259
260 clock_class->frequency = freq;
261 BT_LOGV("Set clock class's frequency: addr=%p, name=\"%s\", freq=%" PRIu64,
262 clock_class, bt_clock_class_get_name(clock_class), freq);
263 end:
264 return ret;
265 }
266
267 uint64_t bt_clock_class_get_precision(struct bt_clock_class *clock_class)
268 {
269 uint64_t ret = -1ULL;
270
271 if (!clock_class) {
272 BT_LOGW_STR("Invalid parameter: clock class is NULL.");
273 goto end;
274 }
275
276 ret = clock_class->precision;
277 end:
278 return ret;
279 }
280
281 int bt_clock_class_set_precision(struct bt_clock_class *clock_class,
282 uint64_t precision)
283 {
284 int ret = 0;
285
286 if (!clock_class || precision == -1ULL) {
287 BT_LOGW("Invalid parameter: clock class is NULL or precision is invalid: "
288 "addr=%p, name=\"%s\", precision=%" PRIu64,
289 clock_class, bt_clock_class_get_name(clock_class),
290 precision);
291 ret = -1;
292 goto end;
293 }
294
295 if (clock_class->frozen) {
296 BT_LOGW("Invalid parameter: clock class is frozen: addr=%p, name=\"%s\"",
297 clock_class, bt_clock_class_get_name(clock_class));
298 ret = -1;
299 goto end;
300 }
301
302 clock_class->precision = precision;
303 BT_LOGV("Set clock class's precision: addr=%p, name=\"%s\", precision=%" PRIu64,
304 clock_class, bt_clock_class_get_name(clock_class),
305 precision);
306 end:
307 return ret;
308 }
309
310 int bt_clock_class_get_offset_s(struct bt_clock_class *clock_class,
311 int64_t *offset_s)
312 {
313 int ret = 0;
314
315 if (!clock_class || !offset_s) {
316 BT_LOGW("Invalid parameter: clock class or offset pointer is NULL: "
317 "clock-class-addr=%p, name=\"%s\", offset-addr=%p",
318 clock_class, bt_clock_class_get_name(clock_class),
319 offset_s);
320 ret = -1;
321 goto end;
322 }
323
324 *offset_s = clock_class->offset_s;
325 end:
326 return ret;
327 }
328
329 int bt_clock_class_set_offset_s(struct bt_clock_class *clock_class,
330 int64_t offset_s)
331 {
332 int ret = 0;
333
334 if (!clock_class) {
335 BT_LOGW_STR("Invalid parameter: clock class is NULL.");
336 ret = -1;
337 goto end;
338 }
339
340 if (clock_class->frozen) {
341 BT_LOGW("Invalid parameter: clock class is frozen: addr=%p, name=\"%s\"",
342 clock_class, bt_clock_class_get_name(clock_class));
343 ret = -1;
344 goto end;
345 }
346
347 clock_class->offset_s = offset_s;
348 BT_LOGV("Set clock class's offset (seconds): "
349 "addr=%p, name=\"%s\", offset-s=%" PRId64,
350 clock_class, bt_clock_class_get_name(clock_class),
351 offset_s);
352 end:
353 return ret;
354 }
355
356 int bt_clock_class_get_offset_cycles(struct bt_clock_class *clock_class,
357 int64_t *offset)
358 {
359 int ret = 0;
360
361 if (!clock_class || !offset) {
362 BT_LOGW("Invalid parameter: clock class or offset pointer is NULL: "
363 "clock-class-addr=%p, name=\"%s\", offset-addr=%p",
364 clock_class, bt_clock_class_get_name(clock_class),
365 offset);
366 ret = -1;
367 goto end;
368 }
369
370 *offset = clock_class->offset;
371 end:
372 return ret;
373 }
374
375 int bt_clock_class_set_offset_cycles(struct bt_clock_class *clock_class,
376 int64_t offset)
377 {
378 int ret = 0;
379
380 if (!clock_class) {
381 BT_LOGW_STR("Invalid parameter: clock class is NULL.");
382 ret = -1;
383 goto end;
384 }
385
386 if (clock_class->frozen) {
387 BT_LOGW("Invalid parameter: clock class is frozen: addr=%p, name=\"%s\"",
388 clock_class, bt_clock_class_get_name(clock_class));
389 ret = -1;
390 goto end;
391 }
392
393 clock_class->offset = offset;
394 BT_LOGV("Set clock class's offset (cycles): addr=%p, name=\"%s\", offset-cycles=%" PRId64,
395 clock_class, bt_clock_class_get_name(clock_class), offset);
396 end:
397 return ret;
398 }
399
400 bt_bool bt_clock_class_is_absolute(struct bt_clock_class *clock_class)
401 {
402 int ret = -1;
403
404 if (!clock_class) {
405 BT_LOGW_STR("Invalid parameter: clock class is NULL.");
406 goto end;
407 }
408
409 ret = clock_class->absolute;
410 end:
411 return ret;
412 }
413
414 int bt_clock_class_set_is_absolute(struct bt_clock_class *clock_class,
415 bt_bool is_absolute)
416 {
417 int ret = 0;
418
419 if (!clock_class) {
420 BT_LOGW_STR("Invalid parameter: clock class is NULL.");
421 ret = -1;
422 goto end;
423 }
424
425 if (clock_class->frozen) {
426 BT_LOGW("Invalid parameter: clock class is frozen: addr=%p, name=\"%s\"",
427 clock_class, bt_clock_class_get_name(clock_class));
428 ret = -1;
429 goto end;
430 }
431
432 clock_class->absolute = !!is_absolute;
433 BT_LOGV("Set clock class's absolute flag: addr=%p, name=\"%s\", is-absolute=%d",
434 clock_class, bt_clock_class_get_name(clock_class),
435 is_absolute);
436 end:
437 return ret;
438 }
439
440 const unsigned char *bt_clock_class_get_uuid(
441 struct bt_clock_class *clock_class)
442 {
443 const unsigned char *ret;
444
445 if (!clock_class) {
446 BT_LOGW_STR("Invalid parameter: clock class is NULL.");
447 ret = NULL;
448 goto end;
449 }
450
451 if (!clock_class->uuid_set) {
452 BT_LOGV("Clock class's UUID is not set: addr=%p, name=\"%s\"",
453 clock_class, bt_clock_class_get_name(clock_class));
454 ret = NULL;
455 goto end;
456 }
457
458 ret = clock_class->uuid;
459 end:
460 return ret;
461 }
462
463 int bt_clock_class_set_uuid(struct bt_clock_class *clock_class,
464 const unsigned char *uuid)
465 {
466 int ret = 0;
467
468 if (!clock_class || !uuid) {
469 BT_LOGW("Invalid parameter: clock class or UUID is NULL: "
470 "clock-class-addr=%p, name=\"%s\", uuid-addr=%p",
471 clock_class, bt_clock_class_get_name(clock_class),
472 uuid);
473 ret = -1;
474 goto end;
475 }
476
477 if (clock_class->frozen) {
478 BT_LOGW("Invalid parameter: clock class is frozen: addr=%p, name=\"%s\"",
479 clock_class, bt_clock_class_get_name(clock_class));
480 ret = -1;
481 goto end;
482 }
483
484 memcpy(clock_class->uuid, uuid, BABELTRACE_UUID_LEN);
485 clock_class->uuid_set = 1;
486 BT_LOGV("Set clock class's UUID: addr=%p, name=\"%s\", "
487 "uuid=\"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\"",
488 clock_class, bt_clock_class_get_name(clock_class),
489 (unsigned int) uuid[0],
490 (unsigned int) uuid[1],
491 (unsigned int) uuid[2],
492 (unsigned int) uuid[3],
493 (unsigned int) uuid[4],
494 (unsigned int) uuid[5],
495 (unsigned int) uuid[6],
496 (unsigned int) uuid[7],
497 (unsigned int) uuid[8],
498 (unsigned int) uuid[9],
499 (unsigned int) uuid[10],
500 (unsigned int) uuid[11],
501 (unsigned int) uuid[12],
502 (unsigned int) uuid[13],
503 (unsigned int) uuid[14],
504 (unsigned int) uuid[15]);
505 end:
506 return ret;
507 }
508
509 static uint64_t ns_from_value(uint64_t frequency, uint64_t value)
510 {
511 uint64_t ns;
512
513 if (frequency == 1000000000) {
514 ns = value;
515 } else {
516 double dblres = ((1e9 * (double) value) / (double) frequency);
517
518 if (dblres >= (double) UINT64_MAX) {
519 /* Overflows uint64_t */
520 ns = -1ULL;
521 } else {
522 ns = (uint64_t) dblres;
523 }
524 }
525
526 return ns;
527 }
528
529 BT_HIDDEN
530 void bt_clock_class_freeze(struct bt_clock_class *clock_class)
531 {
532 if (!clock_class || clock_class->frozen) {
533 return;
534 }
535
536 BT_LOGD("Freezing clock class: addr=%p, name=\"%s\"",
537 clock_class, bt_clock_class_get_name(clock_class));
538 clock_class->frozen = 1;
539 }
540
541 static
542 void bt_clock_class_destroy(struct bt_object *obj)
543 {
544 struct bt_clock_class *clock_class;
545
546 clock_class = container_of(obj, struct bt_clock_class, base);
547 BT_LOGD("Destroying clock class: addr=%p, name=\"%s\"",
548 obj, bt_clock_class_get_name(clock_class));
549 if (clock_class->name) {
550 g_string_free(clock_class->name, TRUE);
551 }
552 if (clock_class->description) {
553 g_string_free(clock_class->description, TRUE);
554 }
555
556 g_free(clock_class);
557 }
558
559 static
560 void bt_clock_value_destroy(struct bt_object *obj)
561 {
562 struct bt_clock_value *value;
563
564 if (!obj) {
565 return;
566 }
567
568 value = container_of(obj, struct bt_clock_value, base);
569 BT_LOGD("Destroying clock value: addr=%p, clock-class-addr=%p, "
570 "clock-class-name=\"%s\"", obj, value->clock_class,
571 bt_clock_class_get_name(value->clock_class));
572 bt_put(value->clock_class);
573 g_free(value);
574 }
575
576 static
577 void set_ns_from_epoch(struct bt_clock_value *clock_value)
578 {
579 struct bt_clock_class *clock_class = clock_value->clock_class;
580 int64_t diff;
581 int64_t s_ns;
582 uint64_t u_ns;
583 uint64_t cycles;
584
585 /* Initialize nanosecond timestamp to clock's offset in seconds */
586 if (clock_class->offset_s <= (INT64_MIN / 1000000000) ||
587 clock_class->offset_s >= (INT64_MAX / 1000000000)) {
588 /*
589 * Overflow: offset in seconds converted to nanoseconds
590 * is outside the int64_t range.
591 */
592 clock_value->ns_from_epoch_overflows = true;
593 goto end;
594 }
595
596 clock_value->ns_from_epoch = clock_class->offset_s * (int64_t) 1000000000;
597
598 /* Add offset in cycles */
599 if (clock_class->offset < 0) {
600 cycles = (uint64_t) (-clock_class->offset);
601 } else {
602 cycles = (uint64_t) clock_class->offset;
603 }
604
605 u_ns = ns_from_value(clock_class->frequency, cycles);
606
607 if (u_ns == -1ULL || u_ns >= INT64_MAX) {
608 /*
609 * Overflow: offset in cycles converted to nanoseconds
610 * is outside the int64_t range.
611 */
612 clock_value->ns_from_epoch_overflows = true;
613 goto end;
614 }
615
616 s_ns = (int64_t) u_ns;
617 BT_ASSERT(s_ns >= 0);
618
619 if (clock_class->offset < 0) {
620 if (clock_value->ns_from_epoch >= 0) {
621 /*
622 * Offset in cycles is negative so it must also
623 * be negative once converted to nanoseconds.
624 */
625 s_ns = -s_ns;
626 goto offset_ok;
627 }
628
629 diff = clock_value->ns_from_epoch - INT64_MIN;
630
631 if (s_ns >= diff) {
632 /*
633 * Overflow: current timestamp in nanoseconds
634 * plus the offset in cycles converted to
635 * nanoseconds is outside the int64_t range.
636 */
637 clock_value->ns_from_epoch_overflows = true;
638 goto end;
639 }
640
641 /*
642 * Offset in cycles is negative so it must also be
643 * negative once converted to nanoseconds.
644 */
645 s_ns = -s_ns;
646 } else {
647 if (clock_value->ns_from_epoch <= 0) {
648 goto offset_ok;
649 }
650
651 diff = INT64_MAX - clock_value->ns_from_epoch;
652
653 if (s_ns >= diff) {
654 /*
655 * Overflow: current timestamp in nanoseconds
656 * plus the offset in cycles converted to
657 * nanoseconds is outside the int64_t range.
658 */
659 clock_value->ns_from_epoch_overflows = true;
660 goto end;
661 }
662 }
663
664 offset_ok:
665 clock_value->ns_from_epoch += s_ns;
666
667 /* Add clock value (cycles) */
668 u_ns = ns_from_value(clock_class->frequency, clock_value->value);
669
670 if (u_ns == -1ULL || u_ns >= INT64_MAX) {
671 /*
672 * Overflow: value converted to nanoseconds is outside
673 * the int64_t range.
674 */
675 clock_value->ns_from_epoch_overflows = true;
676 goto end;
677 }
678
679 s_ns = (int64_t) u_ns;
680 BT_ASSERT(s_ns >= 0);
681
682 /* Clock value (cycles) is always positive */
683 if (clock_value->ns_from_epoch <= 0) {
684 goto value_ok;
685 }
686
687 diff = INT64_MAX - clock_value->ns_from_epoch;
688
689 if (s_ns >= diff) {
690 /*
691 * Overflow: current timestamp in nanoseconds plus the
692 * clock value converted to nanoseconds is outside the
693 * int64_t range.
694 */
695 clock_value->ns_from_epoch_overflows = true;
696 goto end;
697 }
698
699 value_ok:
700 clock_value->ns_from_epoch += s_ns;
701
702 end:
703 if (clock_value->ns_from_epoch_overflows) {
704 clock_value->ns_from_epoch = 0;
705 }
706 }
707
708 struct bt_clock_value *bt_clock_value_create(
709 struct bt_clock_class *clock_class, uint64_t value)
710 {
711 struct bt_clock_value *ret = NULL;
712
713 BT_LOGD("Creating clock value object: clock-class-addr=%p, "
714 "clock-class-name=\"%s\", value=%" PRIu64, clock_class,
715 bt_clock_class_get_name(clock_class), value);
716
717 if (!clock_class) {
718 BT_LOGW_STR("Invalid parameter: clock class is NULL.");
719 goto end;
720 }
721
722 ret = g_new0(struct bt_clock_value, 1);
723 if (!ret) {
724 BT_LOGE_STR("Failed to allocate one clock value.");
725 goto end;
726 }
727
728 bt_object_init(ret, bt_clock_value_destroy);
729 ret->clock_class = bt_get(clock_class);
730 ret->value = value;
731 set_ns_from_epoch(ret);
732 bt_clock_class_freeze(clock_class);
733 BT_LOGD("Created clock value object: clock-value-addr=%p, "
734 "clock-class-addr=%p, clock-class-name=\"%s\", "
735 "ns-from-epoch=%" PRId64 ", ns-from-epoch-overflows=%d",
736 ret, clock_class, bt_clock_class_get_name(clock_class),
737 ret->ns_from_epoch, ret->ns_from_epoch_overflows);
738
739 end:
740 return ret;
741 }
742
743 int bt_clock_value_get_value(
744 struct bt_clock_value *clock_value, uint64_t *raw_value)
745 {
746 int ret = 0;
747
748 if (!clock_value || !raw_value) {
749 BT_LOGW("Invalid parameter: clock value or raw value is NULL: "
750 "clock-value-addr=%p, raw-value-addr=%p",
751 clock_value, raw_value);
752 ret = -1;
753 goto end;
754 }
755
756 *raw_value = clock_value->value;
757 end:
758 return ret;
759 }
760
761 int bt_clock_value_get_value_ns_from_epoch(struct bt_clock_value *value,
762 int64_t *ret_value_ns)
763 {
764 int ret = 0;
765
766 if (!value || !ret_value_ns) {
767 BT_LOGW("Invalid parameter: clock value or return value pointer is NULL: "
768 "clock-value-addr=%p, ret-value-addr=%p",
769 value, ret_value_ns);
770 ret = -1;
771 goto end;
772 }
773
774 if (value->ns_from_epoch_overflows) {
775 BT_LOGW("Clock value converted to nanoseconds from Epoch overflows the signed 64-bit integer range: "
776 "clock-value-addr=%p, "
777 "clock-class-offset-s=%" PRId64 ", "
778 "clock-class-offset-cycles=%" PRId64 ", "
779 "value=%" PRIu64,
780 value, value->clock_class->offset_s,
781 value->clock_class->offset,
782 value->value);
783 ret = -1;
784 goto end;
785 }
786
787 *ret_value_ns = value->ns_from_epoch;
788
789 end:
790 return ret;
791 }
792
793 struct bt_clock_class *bt_clock_value_get_class(
794 struct bt_clock_value *clock_value)
795 {
796 struct bt_clock_class *clock_class = NULL;
797
798 if (!clock_value) {
799 BT_LOGW_STR("Invalid parameter: clock value is NULL.");
800 goto end;
801 }
802
803 clock_class = bt_get(clock_value->clock_class);
804
805 end:
806 return clock_class;
807 }
808
809 BT_HIDDEN
810 int bt_clock_class_compare(struct bt_clock_class *clock_class_a,
811 struct bt_clock_class *clock_class_b)
812 {
813 int ret = 1;
814 BT_ASSERT(clock_class_a);
815 BT_ASSERT(clock_class_b);
816
817 /* Name */
818 if (strcmp(clock_class_a->name->str, clock_class_b->name->str) != 0) {
819 BT_LOGV("Clock classes differ: different names: "
820 "cc-a-name=\"%s\", cc-b-name=\"%s\"",
821 clock_class_a->name->str,
822 clock_class_b->name->str);
823 goto end;
824 }
825
826 /* Description */
827 if (clock_class_a->description) {
828 if (!clock_class_b->description) {
829 BT_LOGV_STR("Clock classes differ: clock class A has a "
830 "description, but clock class B does not.");
831 goto end;
832 }
833
834 if (strcmp(clock_class_a->name->str, clock_class_b->name->str)
835 != 0) {
836 BT_LOGV("Clock classes differ: different descriptions: "
837 "cc-a-descr=\"%s\", cc-b-descr=\"%s\"",
838 clock_class_a->description->str,
839 clock_class_b->description->str);
840 goto end;
841 }
842 } else {
843 if (clock_class_b->description) {
844 BT_LOGV_STR("Clock classes differ: clock class A has "
845 "no description, but clock class B has one.");
846 goto end;
847 }
848 }
849
850 /* Frequency */
851 if (clock_class_a->frequency != clock_class_b->frequency) {
852 BT_LOGV("Clock classes differ: different frequencies: "
853 "cc-a-freq=%" PRIu64 ", cc-b-freq=%" PRIu64,
854 clock_class_a->frequency,
855 clock_class_b->frequency);
856 goto end;
857 }
858
859 /* Precision */
860 if (clock_class_a->precision != clock_class_b->precision) {
861 BT_LOGV("Clock classes differ: different precisions: "
862 "cc-a-freq=%" PRIu64 ", cc-b-freq=%" PRIu64,
863 clock_class_a->precision,
864 clock_class_b->precision);
865 goto end;
866 }
867
868 /* Offset (seconds) */
869 if (clock_class_a->offset_s != clock_class_b->offset_s) {
870 BT_LOGV("Clock classes differ: different offsets (seconds): "
871 "cc-a-offset-s=%" PRId64 ", cc-b-offset-s=%" PRId64,
872 clock_class_a->offset_s,
873 clock_class_b->offset_s);
874 goto end;
875 }
876
877 /* Offset (cycles) */
878 if (clock_class_a->offset != clock_class_b->offset) {
879 BT_LOGV("Clock classes differ: different offsets (cycles): "
880 "cc-a-offset-s=%" PRId64 ", cc-b-offset-s=%" PRId64,
881 clock_class_a->offset,
882 clock_class_b->offset);
883 goto end;
884 }
885
886 /* UUIDs */
887 if (clock_class_a->uuid_set) {
888 if (!clock_class_b->uuid_set) {
889 BT_LOGV_STR("Clock classes differ: clock class A has a "
890 "UUID, but clock class B does not.");
891 goto end;
892 }
893
894 if (memcmp(clock_class_a->uuid, clock_class_b->uuid,
895 BABELTRACE_UUID_LEN) != 0) {
896 BT_LOGV("Clock classes differ: different UUIDs: "
897 "cc-a-uuid=\"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\", "
898 "cc-b-uuid=\"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\"",
899 (unsigned int) clock_class_a->uuid[0],
900 (unsigned int) clock_class_a->uuid[1],
901 (unsigned int) clock_class_a->uuid[2],
902 (unsigned int) clock_class_a->uuid[3],
903 (unsigned int) clock_class_a->uuid[4],
904 (unsigned int) clock_class_a->uuid[5],
905 (unsigned int) clock_class_a->uuid[6],
906 (unsigned int) clock_class_a->uuid[7],
907 (unsigned int) clock_class_a->uuid[8],
908 (unsigned int) clock_class_a->uuid[9],
909 (unsigned int) clock_class_a->uuid[10],
910 (unsigned int) clock_class_a->uuid[11],
911 (unsigned int) clock_class_a->uuid[12],
912 (unsigned int) clock_class_a->uuid[13],
913 (unsigned int) clock_class_a->uuid[14],
914 (unsigned int) clock_class_a->uuid[15],
915 (unsigned int) clock_class_b->uuid[0],
916 (unsigned int) clock_class_b->uuid[1],
917 (unsigned int) clock_class_b->uuid[2],
918 (unsigned int) clock_class_b->uuid[3],
919 (unsigned int) clock_class_b->uuid[4],
920 (unsigned int) clock_class_b->uuid[5],
921 (unsigned int) clock_class_b->uuid[6],
922 (unsigned int) clock_class_b->uuid[7],
923 (unsigned int) clock_class_b->uuid[8],
924 (unsigned int) clock_class_b->uuid[9],
925 (unsigned int) clock_class_b->uuid[10],
926 (unsigned int) clock_class_b->uuid[11],
927 (unsigned int) clock_class_b->uuid[12],
928 (unsigned int) clock_class_b->uuid[13],
929 (unsigned int) clock_class_b->uuid[14],
930 (unsigned int) clock_class_b->uuid[15]);
931 goto end;
932 }
933 } else {
934 if (clock_class_b->uuid_set) {
935 BT_LOGV_STR("Clock classes differ: clock class A has "
936 "no UUID, but clock class B has one.");
937 goto end;
938 }
939 }
940
941 /* Absolute */
942 if (!!clock_class_a->absolute != !!clock_class_b->absolute) {
943 BT_LOGV("Clock classes differ: one is absolute, the other "
944 "is not: cc-a-is-absolute=%d, cc-b-is-absolute=%d",
945 !!clock_class_a->absolute,
946 !!clock_class_b->absolute);
947 goto end;
948 }
949
950 /* Equal */
951 ret = 0;
952
953 end:
954 return ret;
955 }
This page took 0.047812 seconds and 5 git commands to generate.