4 * Babeltrace CTF IR - Clock class
6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
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:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
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
29 #define BT_LOG_TAG "CLOCK-CLASS"
30 #include <babeltrace/lib-logging-internal.h>
32 #include <babeltrace/compat/uuid-internal.h>
33 #include <babeltrace/ctf-ir/clock-class-internal.h>
34 #include <babeltrace/ctf-ir/utils.h>
35 #include <babeltrace/ref.h>
36 #include <babeltrace/compiler-internal.h>
37 #include <babeltrace/types.h>
38 #include <babeltrace/compat/string-internal.h>
40 #include <babeltrace/object-internal.h>
43 void bt_ctf_clock_class_destroy(struct bt_object
*obj
);
46 bt_bool
bt_ctf_clock_class_is_valid(struct bt_ctf_clock_class
*clock_class
)
48 return clock_class
&& clock_class
->name
;
51 int bt_ctf_clock_class_set_name(struct bt_ctf_clock_class
*clock_class
,
57 BT_LOGW_STR("Invalid parameter: clock class is NULL.");
62 if (clock_class
->frozen
) {
63 BT_LOGW("Invalid parameter: clock class is frozen: addr=%p, name=\"%s\"",
64 clock_class
, bt_ctf_clock_class_get_name(clock_class
));
69 if (bt_ctf_validate_identifier(name
)) {
70 BT_LOGE("Clock class's name is not a valid CTF identifier: "
71 "addr=%p, name=\"%s\"",
77 if (clock_class
->name
) {
78 g_string_assign(clock_class
->name
, name
);
80 clock_class
->name
= g_string_new(name
);
81 if (!clock_class
->name
) {
82 BT_LOGE_STR("Failed to allocate a GString.");
88 BT_LOGV("Set clock class's name: addr=%p, name=\"%s\"",
96 bool validate_freq(struct bt_ctf_clock_class
*clock_class
,
97 const char *name
, uint64_t freq
)
101 if (freq
== -1ULL || freq
== 0) {
102 BT_LOGW("Invalid parameter: frequency is invalid: "
103 "addr=%p, name=\"%s\", freq=%" PRIu64
,
104 clock_class
, name
, freq
);
113 struct bt_ctf_clock_class
*bt_ctf_clock_class_create(const char *name
,
117 struct bt_ctf_clock_class
*clock_class
= NULL
;
119 BT_LOGD("Creating default clock class object: name=\"%s\"",
122 if (!validate_freq(NULL
, name
, freq
)) {
123 /* validate_freq() logs errors */
127 clock_class
= g_new0(struct bt_ctf_clock_class
, 1);
129 BT_LOGE_STR("Failed to allocate one clock class.");
133 clock_class
->precision
= 1;
134 clock_class
->frequency
= freq
;
135 bt_object_init(clock_class
, bt_ctf_clock_class_destroy
);
138 ret
= bt_ctf_clock_class_set_name(clock_class
, name
);
140 BT_LOGE("Cannot set clock class's name: "
141 "addr=%p, name=\"%s\"",
147 BT_LOGD("Created clock class object: addr=%p, name=\"%s\"",
155 const char *bt_ctf_clock_class_get_name(struct bt_ctf_clock_class
*clock_class
)
157 const char *ret
= NULL
;
160 BT_LOGW_STR("Invalid parameter: clock class is NULL.");
164 if (clock_class
->name
) {
165 ret
= clock_class
->name
->str
;
172 const char *bt_ctf_clock_class_get_description(
173 struct bt_ctf_clock_class
*clock_class
)
175 const char *ret
= NULL
;
178 BT_LOGW_STR("Invalid parameter: clock class is NULL.");
182 if (clock_class
->description
) {
183 ret
= clock_class
->description
->str
;
189 int bt_ctf_clock_class_set_description(struct bt_ctf_clock_class
*clock_class
,
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_ctf_clock_class_get_name(clock_class
),
203 if (clock_class
->frozen
) {
204 BT_LOGW("Invalid parameter: clock class is frozen: addr=%p, name=\"%s\"",
205 clock_class
, bt_ctf_clock_class_get_name(clock_class
));
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_ctf_clock_class_get_name(clock_class
), desc
);
219 uint64_t bt_ctf_clock_class_get_frequency(
220 struct bt_ctf_clock_class
*clock_class
)
222 uint64_t ret
= -1ULL;
225 BT_LOGW_STR("Invalid parameter: clock class is NULL.");
229 ret
= clock_class
->frequency
;
234 int bt_ctf_clock_class_set_frequency(struct bt_ctf_clock_class
*clock_class
,
240 BT_LOGW("Invalid parameter: clock class is NULL or frequency is invalid: "
241 "addr=%p, name=\"%s\"",
242 clock_class
, bt_ctf_clock_class_get_name(clock_class
));
247 if (!validate_freq(clock_class
, bt_ctf_clock_class_get_name(clock_class
),
249 /* validate_freq() logs errors */
253 if (clock_class
->frozen
) {
254 BT_LOGW("Invalid parameter: clock class is frozen: addr=%p, name=\"%s\"",
255 clock_class
, bt_ctf_clock_class_get_name(clock_class
));
260 clock_class
->frequency
= freq
;
261 BT_LOGV("Set clock class's frequency: addr=%p, name=\"%s\", freq=%" PRIu64
,
262 clock_class
, bt_ctf_clock_class_get_name(clock_class
), freq
);
267 uint64_t bt_ctf_clock_class_get_precision(struct bt_ctf_clock_class
*clock_class
)
269 uint64_t ret
= -1ULL;
272 BT_LOGW_STR("Invalid parameter: clock class is NULL.");
276 ret
= clock_class
->precision
;
281 int bt_ctf_clock_class_set_precision(struct bt_ctf_clock_class
*clock_class
,
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_ctf_clock_class_get_name(clock_class
),
295 if (clock_class
->frozen
) {
296 BT_LOGW("Invalid parameter: clock class is frozen: addr=%p, name=\"%s\"",
297 clock_class
, bt_ctf_clock_class_get_name(clock_class
));
302 clock_class
->precision
= precision
;
303 BT_LOGV("Set clock class's precision: addr=%p, name=\"%s\", precision=%" PRIu64
,
304 clock_class
, bt_ctf_clock_class_get_name(clock_class
),
310 int bt_ctf_clock_class_get_offset_s(struct bt_ctf_clock_class
*clock_class
,
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_ctf_clock_class_get_name(clock_class
),
324 *offset_s
= clock_class
->offset_s
;
329 int bt_ctf_clock_class_set_offset_s(struct bt_ctf_clock_class
*clock_class
,
335 BT_LOGW_STR("Invalid parameter: clock class is NULL.");
340 if (clock_class
->frozen
) {
341 BT_LOGW("Invalid parameter: clock class is frozen: addr=%p, name=\"%s\"",
342 clock_class
, bt_ctf_clock_class_get_name(clock_class
));
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_ctf_clock_class_get_name(clock_class
),
356 int bt_ctf_clock_class_get_offset_cycles(struct bt_ctf_clock_class
*clock_class
,
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_ctf_clock_class_get_name(clock_class
),
370 *offset
= clock_class
->offset
;
375 int bt_ctf_clock_class_set_offset_cycles(struct bt_ctf_clock_class
*clock_class
,
381 BT_LOGW_STR("Invalid parameter: clock class is NULL.");
386 if (clock_class
->frozen
) {
387 BT_LOGW("Invalid parameter: clock class is frozen: addr=%p, name=\"%s\"",
388 clock_class
, bt_ctf_clock_class_get_name(clock_class
));
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_ctf_clock_class_get_name(clock_class
), offset
);
400 bt_bool
bt_ctf_clock_class_is_absolute(struct bt_ctf_clock_class
*clock_class
)
405 BT_LOGW_STR("Invalid parameter: clock class is NULL.");
409 ret
= clock_class
->absolute
;
414 int bt_ctf_clock_class_set_is_absolute(struct bt_ctf_clock_class
*clock_class
,
420 BT_LOGW_STR("Invalid parameter: clock class is NULL.");
425 if (clock_class
->frozen
) {
426 BT_LOGW("Invalid parameter: clock class is frozen: addr=%p, name=\"%s\"",
427 clock_class
, bt_ctf_clock_class_get_name(clock_class
));
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_ctf_clock_class_get_name(clock_class
),
440 const unsigned char *bt_ctf_clock_class_get_uuid(
441 struct bt_ctf_clock_class
*clock_class
)
443 const unsigned char *ret
;
446 BT_LOGW_STR("Invalid parameter: clock class is NULL.");
451 if (!clock_class
->uuid_set
) {
452 BT_LOGV("Clock class's UUID is not set: addr=%p, name=\"%s\"",
453 clock_class
, bt_ctf_clock_class_get_name(clock_class
));
458 ret
= clock_class
->uuid
;
463 int bt_ctf_clock_class_set_uuid(struct bt_ctf_clock_class
*clock_class
,
464 const unsigned char *uuid
)
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_ctf_clock_class_get_name(clock_class
),
477 if (clock_class
->frozen
) {
478 BT_LOGW("Invalid parameter: clock class is frozen: addr=%p, name=\"%s\"",
479 clock_class
, bt_ctf_clock_class_get_name(clock_class
));
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_ctf_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]);
509 static uint64_t ns_from_value(uint64_t frequency
, uint64_t value
)
513 if (frequency
== 1000000000) {
516 double dblres
= ((1e9
* (double) value
) / (double) frequency
);
518 if (dblres
>= (double) UINT64_MAX
) {
519 /* Overflows uint64_t */
522 ns
= (uint64_t) dblres
;
530 void bt_ctf_clock_class_freeze(struct bt_ctf_clock_class
*clock_class
)
533 BT_LOGW_STR("Invalid parameter: clock class is NULL.");
537 if (!clock_class
->frozen
) {
538 BT_LOGD("Freezing clock class: addr=%p, name=\"%s\"",
539 clock_class
, bt_ctf_clock_class_get_name(clock_class
));
540 clock_class
->frozen
= 1;
545 void bt_ctf_clock_class_serialize(struct bt_ctf_clock_class
*clock_class
,
546 struct metadata_context
*context
)
550 BT_LOGD("Serializing clock class's metadata: clock-class-addr=%p, "
551 "name=\"%s\", metadata-context-addr=%p", clock_class
,
552 bt_ctf_clock_class_get_name(clock_class
), context
);
554 if (!clock_class
|| !context
) {
555 BT_LOGW("Invalid parameter: clock class or metadata context is NULL: "
556 "clock-class-addr=%p, name=\"%s\", metadata-context-addr=%p",
557 clock_class
, bt_ctf_clock_class_get_name(clock_class
),
562 uuid
= clock_class
->uuid
;
563 g_string_append(context
->string
, "clock {\n");
564 g_string_append_printf(context
->string
, "\tname = %s;\n",
565 clock_class
->name
->str
);
567 if (clock_class
->uuid_set
) {
568 g_string_append_printf(context
->string
,
569 "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n",
570 uuid
[0], uuid
[1], uuid
[2], uuid
[3],
571 uuid
[4], uuid
[5], uuid
[6], uuid
[7],
572 uuid
[8], uuid
[9], uuid
[10], uuid
[11],
573 uuid
[12], uuid
[13], uuid
[14], uuid
[15]);
576 if (clock_class
->description
) {
577 g_string_append_printf(context
->string
, "\tdescription = \"%s\";\n",
578 clock_class
->description
->str
);
581 g_string_append_printf(context
->string
, "\tfreq = %" PRIu64
";\n",
582 clock_class
->frequency
);
583 g_string_append_printf(context
->string
, "\tprecision = %" PRIu64
";\n",
584 clock_class
->precision
);
585 g_string_append_printf(context
->string
, "\toffset_s = %" PRIu64
";\n",
586 clock_class
->offset_s
);
587 g_string_append_printf(context
->string
, "\toffset = %" PRIu64
";\n",
588 clock_class
->offset
);
589 g_string_append_printf(context
->string
, "\tabsolute = %s;\n",
590 clock_class
->absolute
? "true" : "false");
591 g_string_append(context
->string
, "};\n\n");
595 void bt_ctf_clock_class_destroy(struct bt_object
*obj
)
597 struct bt_ctf_clock_class
*clock_class
;
599 clock_class
= container_of(obj
, struct bt_ctf_clock_class
, base
);
600 BT_LOGD("Destroying clock class: addr=%p, name=\"%s\"",
601 obj
, bt_ctf_clock_class_get_name(clock_class
));
602 if (clock_class
->name
) {
603 g_string_free(clock_class
->name
, TRUE
);
605 if (clock_class
->description
) {
606 g_string_free(clock_class
->description
, TRUE
);
613 void bt_ctf_clock_value_destroy(struct bt_object
*obj
)
615 struct bt_ctf_clock_value
*value
;
621 value
= container_of(obj
, struct bt_ctf_clock_value
, base
);
622 BT_LOGD("Destroying clock value: addr=%p, clock-class-addr=%p, "
623 "clock-class-name=\"%s\"", obj
, value
->clock_class
,
624 bt_ctf_clock_class_get_name(value
->clock_class
));
625 bt_put(value
->clock_class
);
630 void set_ns_from_epoch(struct bt_ctf_clock_value
*clock_value
)
632 struct bt_ctf_clock_class
*clock_class
= clock_value
->clock_class
;
638 /* Initialize nanosecond timestamp to clock's offset in seconds */
639 if (clock_class
->offset_s
<= (INT64_MIN
/ 1000000000) ||
640 clock_class
->offset_s
>= (INT64_MAX
/ 1000000000)) {
642 * Overflow: offset in seconds converted to nanoseconds
643 * is outside the int64_t range.
645 clock_value
->ns_from_epoch_overflows
= true;
649 clock_value
->ns_from_epoch
= clock_class
->offset_s
* (int64_t) 1000000000;
651 /* Add offset in cycles */
652 if (clock_class
->offset
< 0) {
653 cycles
= (uint64_t) (-clock_class
->offset
);
655 cycles
= (uint64_t) clock_class
->offset
;
658 u_ns
= ns_from_value(clock_class
->frequency
, cycles
);
660 if (u_ns
== -1ULL || u_ns
>= INT64_MAX
) {
662 * Overflow: offset in cycles converted to nanoseconds
663 * is outside the int64_t range.
665 clock_value
->ns_from_epoch_overflows
= true;
669 s_ns
= (int64_t) u_ns
;
672 if (clock_class
->offset
< 0) {
673 if (clock_value
->ns_from_epoch
>= 0) {
675 * Offset in cycles is negative so it must also
676 * be negative once converted to nanoseconds.
682 diff
= clock_value
->ns_from_epoch
- INT64_MIN
;
686 * Overflow: current timestamp in nanoseconds
687 * plus the offset in cycles converted to
688 * nanoseconds is outside the int64_t range.
690 clock_value
->ns_from_epoch_overflows
= true;
695 * Offset in cycles is negative so it must also be
696 * negative once converted to nanoseconds.
700 if (clock_value
->ns_from_epoch
<= 0) {
704 diff
= INT64_MAX
- clock_value
->ns_from_epoch
;
708 * Overflow: current timestamp in nanoseconds
709 * plus the offset in cycles converted to
710 * nanoseconds is outside the int64_t range.
712 clock_value
->ns_from_epoch_overflows
= true;
718 clock_value
->ns_from_epoch
+= s_ns
;
720 /* Add clock value (cycles) */
721 u_ns
= ns_from_value(clock_class
->frequency
, clock_value
->value
);
723 if (u_ns
== -1ULL || u_ns
>= INT64_MAX
) {
725 * Overflow: value converted to nanoseconds is outside
728 clock_value
->ns_from_epoch_overflows
= true;
732 s_ns
= (int64_t) u_ns
;
735 /* Clock value (cycles) is always positive */
736 if (clock_value
->ns_from_epoch
<= 0) {
740 diff
= INT64_MAX
- clock_value
->ns_from_epoch
;
744 * Overflow: current timestamp in nanoseconds plus the
745 * clock value converted to nanoseconds is outside the
748 clock_value
->ns_from_epoch_overflows
= true;
753 clock_value
->ns_from_epoch
+= s_ns
;
756 if (clock_value
->ns_from_epoch_overflows
) {
757 clock_value
->ns_from_epoch
= 0;
761 struct bt_ctf_clock_value
*bt_ctf_clock_value_create(
762 struct bt_ctf_clock_class
*clock_class
, uint64_t value
)
764 struct bt_ctf_clock_value
*ret
= NULL
;
766 BT_LOGD("Creating clock value object: clock-class-addr=%p, "
767 "clock-class-name=\"%s\", value=%" PRIu64
, clock_class
,
768 bt_ctf_clock_class_get_name(clock_class
), value
);
771 BT_LOGW_STR("Invalid parameter: clock class is NULL.");
775 ret
= g_new0(struct bt_ctf_clock_value
, 1);
777 BT_LOGE_STR("Failed to allocate one clock value.");
781 bt_object_init(ret
, bt_ctf_clock_value_destroy
);
782 ret
->clock_class
= bt_get(clock_class
);
784 set_ns_from_epoch(ret
);
785 BT_LOGD("Created clock value object: clock-value-addr=%p, "
786 "clock-class-addr=%p, clock-class-name=\"%s\", "
787 "ns-from-epoch=%" PRId64
", ns-from-epoch-overflows=%d",
788 ret
, clock_class
, bt_ctf_clock_class_get_name(clock_class
),
789 ret
->ns_from_epoch
, ret
->ns_from_epoch_overflows
);
795 int bt_ctf_clock_value_get_value(
796 struct bt_ctf_clock_value
*clock_value
, uint64_t *raw_value
)
800 if (!clock_value
|| !raw_value
) {
801 BT_LOGW("Invalid parameter: clock value or raw value is NULL: "
802 "clock-value-addr=%p, raw-value-addr=%p",
803 clock_value
, raw_value
);
808 *raw_value
= clock_value
->value
;
813 int bt_ctf_clock_value_get_value_ns_from_epoch(struct bt_ctf_clock_value
*value
,
814 int64_t *ret_value_ns
)
818 if (!value
|| !ret_value_ns
) {
819 BT_LOGW("Invalid parameter: clock value or return value pointer is NULL: "
820 "clock-value-addr=%p, ret-value-addr=%p",
821 value
, ret_value_ns
);
826 if (value
->ns_from_epoch_overflows
) {
827 BT_LOGW("Clock value converted to nanoseconds from Epoch overflows the signed 64-bit integer range: "
828 "clock-value-addr=%p, "
829 "clock-class-offset-s=%" PRId64
", "
830 "clock-class-offset-cycles=%" PRId64
", "
832 value
, value
->clock_class
->offset_s
,
833 value
->clock_class
->offset
,
839 *ret_value_ns
= value
->ns_from_epoch
;
845 struct bt_ctf_clock_class
*bt_ctf_clock_value_get_class(
846 struct bt_ctf_clock_value
*clock_value
)
848 struct bt_ctf_clock_class
*clock_class
= NULL
;
851 BT_LOGW_STR("Invalid parameter: clock value is NULL.");
855 clock_class
= bt_get(clock_value
->clock_class
);