4 * Babeltrace CTF writer - 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 "CTF-WRITER-CLOCK-CLASS"
30 #include <babeltrace/lib-logging-internal.h>
32 #include <babeltrace/assert-pre-internal.h>
33 #include <babeltrace/compat/uuid-internal.h>
34 #include <babeltrace/ctf-writer/clock-class-internal.h>
35 #include <babeltrace/ctf-writer/utils.h>
36 #include <babeltrace/ctf-writer/object.h>
37 #include <babeltrace/compiler-internal.h>
38 #include <babeltrace/types.h>
39 #include <babeltrace/compat/string-internal.h>
41 #include <babeltrace/ctf-writer/object-internal.h>
42 #include <babeltrace/assert-internal.h>
45 void bt_ctf_clock_class_destroy(struct bt_ctf_object
*obj
);
48 bt_bool
bt_ctf_clock_class_is_valid(struct bt_ctf_clock_class
*clock_class
)
50 return clock_class
&& clock_class
->name
;
54 int bt_ctf_clock_class_set_name(struct bt_ctf_clock_class
*clock_class
,
60 BT_LOGW_STR("Invalid parameter: clock class is NULL.");
65 if (clock_class
->frozen
) {
66 BT_LOGW("Invalid parameter: clock class is frozen: addr=%p, name=\"%s\"",
67 clock_class
, bt_ctf_clock_class_get_name(clock_class
));
72 if (!bt_ctf_identifier_is_valid(name
)) {
73 BT_LOGW("Clock class's name is not a valid CTF identifier: "
74 "addr=%p, name=\"%s\"",
80 if (clock_class
->name
) {
81 g_string_assign(clock_class
->name
, name
);
83 clock_class
->name
= g_string_new(name
);
84 if (!clock_class
->name
) {
85 BT_LOGE_STR("Failed to allocate a GString.");
91 BT_LOGV("Set clock class's name: addr=%p, name=\"%s\"",
99 bool validate_freq(struct bt_ctf_clock_class
*clock_class
,
100 const char *name
, uint64_t freq
)
102 bool is_valid
= true;
104 if (freq
== -1ULL || freq
== 0) {
105 BT_LOGW("Invalid parameter: frequency is invalid: "
106 "addr=%p, name=\"%s\", freq=%" PRIu64
,
107 clock_class
, name
, freq
);
117 struct bt_ctf_clock_class
*bt_ctf_clock_class_create(const char *name
,
121 struct bt_ctf_clock_class
*clock_class
= NULL
;
123 BT_LOGD("Creating default clock class object: name=\"%s\"",
126 if (!validate_freq(NULL
, name
, freq
)) {
127 /* validate_freq() logs errors */
131 clock_class
= g_new0(struct bt_ctf_clock_class
, 1);
133 BT_LOGE_STR("Failed to allocate one clock class.");
137 clock_class
->precision
= 1;
138 clock_class
->frequency
= freq
;
139 bt_ctf_object_init_shared(&clock_class
->base
, bt_ctf_clock_class_destroy
);
142 ret
= bt_ctf_clock_class_set_name(clock_class
, name
);
144 /* bt_ctf_clock_class_set_name() logs errors */
149 BT_LOGD("Created clock class object: addr=%p, name=\"%s\"",
153 BT_CTF_OBJECT_PUT_REF_AND_RESET(clock_class
);
158 const char *bt_ctf_clock_class_get_name(struct bt_ctf_clock_class
*clock_class
)
160 const char *ret
= NULL
;
163 BT_LOGW_STR("Invalid parameter: clock class is NULL.");
167 if (clock_class
->name
) {
168 ret
= clock_class
->name
->str
;
176 const char *bt_ctf_clock_class_get_description(
177 struct bt_ctf_clock_class
*clock_class
)
179 const char *ret
= NULL
;
182 BT_LOGW_STR("Invalid parameter: clock class is NULL.");
186 if (clock_class
->description
) {
187 ret
= clock_class
->description
->str
;
194 int bt_ctf_clock_class_set_description(struct bt_ctf_clock_class
*clock_class
,
199 if (!clock_class
|| !desc
) {
200 BT_LOGW("Invalid parameter: clock class or description is NULL: "
201 "clock-class-addr=%p, name=\"%s\", desc-addr=%p",
202 clock_class
, bt_ctf_clock_class_get_name(clock_class
),
208 if (clock_class
->frozen
) {
209 BT_LOGW("Invalid parameter: clock class is frozen: addr=%p, name=\"%s\"",
210 clock_class
, bt_ctf_clock_class_get_name(clock_class
));
215 clock_class
->description
= g_string_new(desc
);
216 ret
= clock_class
->description
? 0 : -1;
217 BT_LOGV("Set clock class's description: addr=%p, "
218 "name=\"%s\", desc=\"%s\"",
219 clock_class
, bt_ctf_clock_class_get_name(clock_class
), desc
);
225 uint64_t bt_ctf_clock_class_get_frequency(
226 struct bt_ctf_clock_class
*clock_class
)
228 uint64_t ret
= -1ULL;
231 BT_LOGW_STR("Invalid parameter: clock class is NULL.");
235 ret
= clock_class
->frequency
;
241 int bt_ctf_clock_class_set_frequency(struct bt_ctf_clock_class
*clock_class
,
247 BT_LOGW("Invalid parameter: clock class is NULL or frequency is invalid: "
248 "addr=%p, name=\"%s\"",
249 clock_class
, bt_ctf_clock_class_get_name(clock_class
));
254 if (!validate_freq(clock_class
, bt_ctf_clock_class_get_name(clock_class
),
256 /* validate_freq() logs errors */
260 if (clock_class
->frozen
) {
261 BT_LOGW("Invalid parameter: clock class is frozen: addr=%p, name=\"%s\"",
262 clock_class
, bt_ctf_clock_class_get_name(clock_class
));
267 clock_class
->frequency
= freq
;
268 BT_LOGV("Set clock class's frequency: addr=%p, name=\"%s\", freq=%" PRIu64
,
269 clock_class
, bt_ctf_clock_class_get_name(clock_class
), freq
);
275 uint64_t bt_ctf_clock_class_get_precision(struct bt_ctf_clock_class
*clock_class
)
277 uint64_t ret
= -1ULL;
280 BT_LOGW_STR("Invalid parameter: clock class is NULL.");
284 ret
= clock_class
->precision
;
290 int bt_ctf_clock_class_set_precision(struct bt_ctf_clock_class
*clock_class
,
295 if (!clock_class
|| precision
== -1ULL) {
296 BT_LOGW("Invalid parameter: clock class is NULL or precision is invalid: "
297 "addr=%p, name=\"%s\", precision=%" PRIu64
,
298 clock_class
, bt_ctf_clock_class_get_name(clock_class
),
304 if (clock_class
->frozen
) {
305 BT_LOGW("Invalid parameter: clock class is frozen: addr=%p, name=\"%s\"",
306 clock_class
, bt_ctf_clock_class_get_name(clock_class
));
311 clock_class
->precision
= precision
;
312 BT_LOGV("Set clock class's precision: addr=%p, name=\"%s\", precision=%" PRIu64
,
313 clock_class
, bt_ctf_clock_class_get_name(clock_class
),
320 int bt_ctf_clock_class_get_offset_s(struct bt_ctf_clock_class
*clock_class
,
325 if (!clock_class
|| !offset_s
) {
326 BT_LOGW("Invalid parameter: clock class or offset pointer is NULL: "
327 "clock-class-addr=%p, name=\"%s\", offset-addr=%p",
328 clock_class
, bt_ctf_clock_class_get_name(clock_class
),
334 *offset_s
= clock_class
->offset_s
;
340 int bt_ctf_clock_class_set_offset_s(struct bt_ctf_clock_class
*clock_class
,
346 BT_LOGW_STR("Invalid parameter: clock class is NULL.");
351 if (clock_class
->frozen
) {
352 BT_LOGW("Invalid parameter: clock class is frozen: addr=%p, name=\"%s\"",
353 clock_class
, bt_ctf_clock_class_get_name(clock_class
));
358 clock_class
->offset_s
= offset_s
;
359 BT_LOGV("Set clock class's offset (seconds): "
360 "addr=%p, name=\"%s\", offset-s=%" PRId64
,
361 clock_class
, bt_ctf_clock_class_get_name(clock_class
),
368 int bt_ctf_clock_class_get_offset_cycles(struct bt_ctf_clock_class
*clock_class
,
373 if (!clock_class
|| !offset
) {
374 BT_LOGW("Invalid parameter: clock class or offset pointer is NULL: "
375 "clock-class-addr=%p, name=\"%s\", offset-addr=%p",
376 clock_class
, bt_ctf_clock_class_get_name(clock_class
),
382 *offset
= clock_class
->offset
;
388 int bt_ctf_clock_class_set_offset_cycles(struct bt_ctf_clock_class
*clock_class
,
394 BT_LOGW_STR("Invalid parameter: clock class is NULL.");
399 if (clock_class
->frozen
) {
400 BT_LOGW("Invalid parameter: clock class is frozen: addr=%p, name=\"%s\"",
401 clock_class
, bt_ctf_clock_class_get_name(clock_class
));
406 clock_class
->offset
= offset
;
407 BT_LOGV("Set clock class's offset (cycles): addr=%p, name=\"%s\", offset-cycles=%" PRId64
,
408 clock_class
, bt_ctf_clock_class_get_name(clock_class
), offset
);
414 bt_bool
bt_ctf_clock_class_is_absolute(struct bt_ctf_clock_class
*clock_class
)
419 BT_LOGW_STR("Invalid parameter: clock class is NULL.");
423 ret
= clock_class
->absolute
;
429 int bt_ctf_clock_class_set_is_absolute(struct bt_ctf_clock_class
*clock_class
,
435 BT_LOGW_STR("Invalid parameter: clock class is NULL.");
440 if (clock_class
->frozen
) {
441 BT_LOGW("Invalid parameter: clock class is frozen: addr=%p, name=\"%s\"",
442 clock_class
, bt_ctf_clock_class_get_name(clock_class
));
447 clock_class
->absolute
= !!is_absolute
;
448 BT_LOGV("Set clock class's absolute flag: addr=%p, name=\"%s\", is-absolute=%d",
449 clock_class
, bt_ctf_clock_class_get_name(clock_class
),
456 const unsigned char *bt_ctf_clock_class_get_uuid(
457 struct bt_ctf_clock_class
*clock_class
)
459 const unsigned char *ret
;
462 BT_LOGW_STR("Invalid parameter: clock class is NULL.");
467 if (!clock_class
->uuid_set
) {
468 BT_LOGV("Clock class's UUID is not set: addr=%p, name=\"%s\"",
469 clock_class
, bt_ctf_clock_class_get_name(clock_class
));
474 ret
= clock_class
->uuid
;
480 int bt_ctf_clock_class_set_uuid(struct bt_ctf_clock_class
*clock_class
,
481 const unsigned char *uuid
)
485 if (!clock_class
|| !uuid
) {
486 BT_LOGW("Invalid parameter: clock class or UUID is NULL: "
487 "clock-class-addr=%p, name=\"%s\", uuid-addr=%p",
488 clock_class
, bt_ctf_clock_class_get_name(clock_class
),
494 if (clock_class
->frozen
) {
495 BT_LOGW("Invalid parameter: clock class is frozen: addr=%p, name=\"%s\"",
496 clock_class
, bt_ctf_clock_class_get_name(clock_class
));
501 memcpy(clock_class
->uuid
, uuid
, BABELTRACE_UUID_LEN
);
502 clock_class
->uuid_set
= 1;
503 BT_LOGV("Set clock class's UUID: addr=%p, name=\"%s\", "
504 "uuid=\"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\"",
505 clock_class
, bt_ctf_clock_class_get_name(clock_class
),
506 (unsigned int) uuid
[0],
507 (unsigned int) uuid
[1],
508 (unsigned int) uuid
[2],
509 (unsigned int) uuid
[3],
510 (unsigned int) uuid
[4],
511 (unsigned int) uuid
[5],
512 (unsigned int) uuid
[6],
513 (unsigned int) uuid
[7],
514 (unsigned int) uuid
[8],
515 (unsigned int) uuid
[9],
516 (unsigned int) uuid
[10],
517 (unsigned int) uuid
[11],
518 (unsigned int) uuid
[12],
519 (unsigned int) uuid
[13],
520 (unsigned int) uuid
[14],
521 (unsigned int) uuid
[15]);
527 void bt_ctf_clock_class_freeze(struct bt_ctf_clock_class
*clock_class
)
529 if (!clock_class
|| clock_class
->frozen
) {
533 BT_LOGD("Freezing clock class: addr=%p, name=\"%s\"",
534 clock_class
, bt_ctf_clock_class_get_name(clock_class
));
535 clock_class
->frozen
= 1;
539 void bt_ctf_clock_class_destroy(struct bt_ctf_object
*obj
)
541 struct bt_ctf_clock_class
*clock_class
;
543 clock_class
= container_of(obj
, struct bt_ctf_clock_class
, base
);
544 BT_LOGD("Destroying clock class: addr=%p, name=\"%s\"",
545 obj
, bt_ctf_clock_class_get_name(clock_class
));
547 if (clock_class
->name
) {
548 g_string_free(clock_class
->name
, TRUE
);
551 if (clock_class
->description
) {
552 g_string_free(clock_class
->description
, TRUE
);
559 int bt_ctf_clock_class_compare(struct bt_ctf_clock_class
*clock_class_a
,
560 struct bt_ctf_clock_class
*clock_class_b
)
563 BT_ASSERT(clock_class_a
);
564 BT_ASSERT(clock_class_b
);
567 if (strcmp(clock_class_a
->name
->str
, clock_class_b
->name
->str
) != 0) {
568 BT_LOGV("Clock classes differ: different names: "
569 "cc-a-name=\"%s\", cc-b-name=\"%s\"",
570 clock_class_a
->name
->str
,
571 clock_class_b
->name
->str
);
576 if (clock_class_a
->description
) {
577 if (!clock_class_b
->description
) {
578 BT_LOGV_STR("Clock classes differ: clock class A has a "
579 "description, but clock class B does not.");
583 if (strcmp(clock_class_a
->name
->str
, clock_class_b
->name
->str
)
585 BT_LOGV("Clock classes differ: different descriptions: "
586 "cc-a-descr=\"%s\", cc-b-descr=\"%s\"",
587 clock_class_a
->description
->str
,
588 clock_class_b
->description
->str
);
592 if (clock_class_b
->description
) {
593 BT_LOGV_STR("Clock classes differ: clock class A has "
594 "no description, but clock class B has one.");
600 if (clock_class_a
->frequency
!= clock_class_b
->frequency
) {
601 BT_LOGV("Clock classes differ: different frequencies: "
602 "cc-a-freq=%" PRIu64
", cc-b-freq=%" PRIu64
,
603 clock_class_a
->frequency
,
604 clock_class_b
->frequency
);
609 if (clock_class_a
->precision
!= clock_class_b
->precision
) {
610 BT_LOGV("Clock classes differ: different precisions: "
611 "cc-a-freq=%" PRIu64
", cc-b-freq=%" PRIu64
,
612 clock_class_a
->precision
,
613 clock_class_b
->precision
);
617 /* Offset (seconds) */
618 if (clock_class_a
->offset_s
!= clock_class_b
->offset_s
) {
619 BT_LOGV("Clock classes differ: different offsets (seconds): "
620 "cc-a-offset-s=%" PRId64
", cc-b-offset-s=%" PRId64
,
621 clock_class_a
->offset_s
,
622 clock_class_b
->offset_s
);
626 /* Offset (cycles) */
627 if (clock_class_a
->offset
!= clock_class_b
->offset
) {
628 BT_LOGV("Clock classes differ: different offsets (cycles): "
629 "cc-a-offset-s=%" PRId64
", cc-b-offset-s=%" PRId64
,
630 clock_class_a
->offset
,
631 clock_class_b
->offset
);
636 if (clock_class_a
->uuid_set
) {
637 if (!clock_class_b
->uuid_set
) {
638 BT_LOGV_STR("Clock classes differ: clock class A has a "
639 "UUID, but clock class B does not.");
643 if (memcmp(clock_class_a
->uuid
, clock_class_b
->uuid
,
644 BABELTRACE_UUID_LEN
) != 0) {
645 BT_LOGV("Clock classes differ: different UUIDs: "
646 "cc-a-uuid=\"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\", "
647 "cc-b-uuid=\"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\"",
648 (unsigned int) clock_class_a
->uuid
[0],
649 (unsigned int) clock_class_a
->uuid
[1],
650 (unsigned int) clock_class_a
->uuid
[2],
651 (unsigned int) clock_class_a
->uuid
[3],
652 (unsigned int) clock_class_a
->uuid
[4],
653 (unsigned int) clock_class_a
->uuid
[5],
654 (unsigned int) clock_class_a
->uuid
[6],
655 (unsigned int) clock_class_a
->uuid
[7],
656 (unsigned int) clock_class_a
->uuid
[8],
657 (unsigned int) clock_class_a
->uuid
[9],
658 (unsigned int) clock_class_a
->uuid
[10],
659 (unsigned int) clock_class_a
->uuid
[11],
660 (unsigned int) clock_class_a
->uuid
[12],
661 (unsigned int) clock_class_a
->uuid
[13],
662 (unsigned int) clock_class_a
->uuid
[14],
663 (unsigned int) clock_class_a
->uuid
[15],
664 (unsigned int) clock_class_b
->uuid
[0],
665 (unsigned int) clock_class_b
->uuid
[1],
666 (unsigned int) clock_class_b
->uuid
[2],
667 (unsigned int) clock_class_b
->uuid
[3],
668 (unsigned int) clock_class_b
->uuid
[4],
669 (unsigned int) clock_class_b
->uuid
[5],
670 (unsigned int) clock_class_b
->uuid
[6],
671 (unsigned int) clock_class_b
->uuid
[7],
672 (unsigned int) clock_class_b
->uuid
[8],
673 (unsigned int) clock_class_b
->uuid
[9],
674 (unsigned int) clock_class_b
->uuid
[10],
675 (unsigned int) clock_class_b
->uuid
[11],
676 (unsigned int) clock_class_b
->uuid
[12],
677 (unsigned int) clock_class_b
->uuid
[13],
678 (unsigned int) clock_class_b
->uuid
[14],
679 (unsigned int) clock_class_b
->uuid
[15]);
683 if (clock_class_b
->uuid_set
) {
684 BT_LOGV_STR("Clock classes differ: clock class A has "
685 "no UUID, but clock class B has one.");
691 if (!!clock_class_a
->absolute
!= !!clock_class_b
->absolute
) {
692 BT_LOGV("Clock classes differ: one is absolute, the other "
693 "is not: cc-a-is-absolute=%d, cc-b-is-absolute=%d",
694 !!clock_class_a
->absolute
,
695 !!clock_class_b
->absolute
);