2 * SPDX-License-Identifier: MIT
4 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * Babeltrace CTF writer - Clock class
9 #define BT_LOG_TAG "CTF-WRITER/CLOCK-CLASS"
12 #include "common/uuid.h"
13 #include <babeltrace2-ctf-writer/utils.h>
14 #include <babeltrace2-ctf-writer/object.h>
15 #include "compat/compiler.h"
16 #include <babeltrace2/types.h>
17 #include "compat/string.h"
20 #include "common/assert.h"
22 #include "assert-pre.h"
23 #include "clock-class.h"
27 void bt_ctf_clock_class_destroy(struct bt_ctf_object
*obj
);
30 bt_ctf_bool
bt_ctf_clock_class_is_valid(struct bt_ctf_clock_class
*clock_class
)
32 return clock_class
&& clock_class
->name
;
36 int bt_ctf_clock_class_set_name(struct bt_ctf_clock_class
*clock_class
,
42 BT_LOGW_STR("Invalid parameter: clock class is NULL.");
47 if (clock_class
->frozen
) {
48 BT_LOGW("Invalid parameter: clock class is frozen: addr=%p, name=\"%s\"",
49 clock_class
, bt_ctf_clock_class_get_name(clock_class
));
54 if (!bt_ctf_identifier_is_valid(name
)) {
55 BT_LOGW("Clock class's name is not a valid CTF identifier: "
56 "addr=%p, name=\"%s\"",
62 if (clock_class
->name
) {
63 g_string_assign(clock_class
->name
, name
);
65 clock_class
->name
= g_string_new(name
);
66 if (!clock_class
->name
) {
67 BT_LOGE_STR("Failed to allocate a GString.");
73 BT_LOGT("Set clock class's name: addr=%p, name=\"%s\"",
81 bool validate_freq(struct bt_ctf_clock_class
*clock_class
,
82 const char *name
, uint64_t freq
)
86 if (freq
== -1ULL || freq
== 0) {
87 BT_LOGW("Invalid parameter: frequency is invalid: "
88 "addr=%p, name=\"%s\", freq=%" PRIu64
,
89 clock_class
, name
, freq
);
99 struct bt_ctf_clock_class
*bt_ctf_clock_class_create(const char *name
,
103 struct bt_ctf_clock_class
*clock_class
= NULL
;
105 BT_LOGD("Creating default clock class object: name=\"%s\"",
108 if (!validate_freq(NULL
, name
, freq
)) {
109 /* validate_freq() logs errors */
113 clock_class
= g_new0(struct bt_ctf_clock_class
, 1);
115 BT_LOGE_STR("Failed to allocate one clock class.");
119 clock_class
->precision
= 1;
120 clock_class
->frequency
= freq
;
121 bt_ctf_object_init_shared(&clock_class
->base
, bt_ctf_clock_class_destroy
);
124 ret
= bt_ctf_clock_class_set_name(clock_class
, name
);
126 /* bt_ctf_clock_class_set_name() logs errors */
131 BT_LOGD("Created clock class object: addr=%p, name=\"%s\"",
135 BT_CTF_OBJECT_PUT_REF_AND_RESET(clock_class
);
140 const char *bt_ctf_clock_class_get_name(struct bt_ctf_clock_class
*clock_class
)
142 const char *ret
= NULL
;
145 BT_LOGW_STR("Invalid parameter: clock class is NULL.");
149 if (clock_class
->name
) {
150 ret
= clock_class
->name
->str
;
158 const char *bt_ctf_clock_class_get_description(
159 struct bt_ctf_clock_class
*clock_class
)
161 const char *ret
= NULL
;
164 BT_LOGW_STR("Invalid parameter: clock class is NULL.");
168 if (clock_class
->description
) {
169 ret
= clock_class
->description
->str
;
176 int bt_ctf_clock_class_set_description(struct bt_ctf_clock_class
*clock_class
,
181 if (!clock_class
|| !desc
) {
182 BT_LOGW("Invalid parameter: clock class or description is NULL: "
183 "clock-class-addr=%p, name=\"%s\", desc-addr=%p",
184 clock_class
, bt_ctf_clock_class_get_name(clock_class
),
190 if (clock_class
->frozen
) {
191 BT_LOGW("Invalid parameter: clock class is frozen: addr=%p, name=\"%s\"",
192 clock_class
, bt_ctf_clock_class_get_name(clock_class
));
197 clock_class
->description
= g_string_new(desc
);
198 ret
= clock_class
->description
? 0 : -1;
199 BT_LOGT("Set clock class's description: addr=%p, "
200 "name=\"%s\", desc=\"%s\"",
201 clock_class
, bt_ctf_clock_class_get_name(clock_class
), desc
);
207 uint64_t bt_ctf_clock_class_get_frequency(
208 struct bt_ctf_clock_class
*clock_class
)
210 uint64_t ret
= -1ULL;
213 BT_LOGW_STR("Invalid parameter: clock class is NULL.");
217 ret
= clock_class
->frequency
;
223 int bt_ctf_clock_class_set_frequency(struct bt_ctf_clock_class
*clock_class
,
229 BT_LOGW("Invalid parameter: clock class is NULL or frequency is invalid: "
230 "addr=%p, name=\"%s\"",
231 clock_class
, bt_ctf_clock_class_get_name(clock_class
));
236 if (!validate_freq(clock_class
, bt_ctf_clock_class_get_name(clock_class
),
238 /* validate_freq() logs errors */
242 if (clock_class
->frozen
) {
243 BT_LOGW("Invalid parameter: clock class is frozen: addr=%p, name=\"%s\"",
244 clock_class
, bt_ctf_clock_class_get_name(clock_class
));
249 clock_class
->frequency
= freq
;
250 BT_LOGT("Set clock class's frequency: addr=%p, name=\"%s\", freq=%" PRIu64
,
251 clock_class
, bt_ctf_clock_class_get_name(clock_class
), freq
);
257 uint64_t bt_ctf_clock_class_get_precision(struct bt_ctf_clock_class
*clock_class
)
259 uint64_t ret
= -1ULL;
262 BT_LOGW_STR("Invalid parameter: clock class is NULL.");
266 ret
= clock_class
->precision
;
272 int bt_ctf_clock_class_set_precision(struct bt_ctf_clock_class
*clock_class
,
277 if (!clock_class
|| precision
== -1ULL) {
278 BT_LOGW("Invalid parameter: clock class is NULL or precision is invalid: "
279 "addr=%p, name=\"%s\", precision=%" PRIu64
,
280 clock_class
, bt_ctf_clock_class_get_name(clock_class
),
286 if (clock_class
->frozen
) {
287 BT_LOGW("Invalid parameter: clock class is frozen: addr=%p, name=\"%s\"",
288 clock_class
, bt_ctf_clock_class_get_name(clock_class
));
293 clock_class
->precision
= precision
;
294 BT_LOGT("Set clock class's precision: addr=%p, name=\"%s\", precision=%" PRIu64
,
295 clock_class
, bt_ctf_clock_class_get_name(clock_class
),
302 int bt_ctf_clock_class_get_offset_s(struct bt_ctf_clock_class
*clock_class
,
307 if (!clock_class
|| !offset_s
) {
308 BT_LOGW("Invalid parameter: clock class or offset pointer is NULL: "
309 "clock-class-addr=%p, name=\"%s\", offset-addr=%p",
310 clock_class
, bt_ctf_clock_class_get_name(clock_class
),
316 *offset_s
= clock_class
->offset_s
;
322 int bt_ctf_clock_class_set_offset_s(struct bt_ctf_clock_class
*clock_class
,
328 BT_LOGW_STR("Invalid parameter: clock class is NULL.");
333 if (clock_class
->frozen
) {
334 BT_LOGW("Invalid parameter: clock class is frozen: addr=%p, name=\"%s\"",
335 clock_class
, bt_ctf_clock_class_get_name(clock_class
));
340 clock_class
->offset_s
= offset_s
;
341 BT_LOGT("Set clock class's offset (seconds): "
342 "addr=%p, name=\"%s\", offset-s=%" PRId64
,
343 clock_class
, bt_ctf_clock_class_get_name(clock_class
),
350 int bt_ctf_clock_class_get_offset_cycles(struct bt_ctf_clock_class
*clock_class
,
355 if (!clock_class
|| !offset
) {
356 BT_LOGW("Invalid parameter: clock class or offset pointer is NULL: "
357 "clock-class-addr=%p, name=\"%s\", offset-addr=%p",
358 clock_class
, bt_ctf_clock_class_get_name(clock_class
),
364 *offset
= clock_class
->offset
;
370 int bt_ctf_clock_class_set_offset_cycles(struct bt_ctf_clock_class
*clock_class
,
376 BT_LOGW_STR("Invalid parameter: clock class is NULL.");
381 if (clock_class
->frozen
) {
382 BT_LOGW("Invalid parameter: clock class is frozen: addr=%p, name=\"%s\"",
383 clock_class
, bt_ctf_clock_class_get_name(clock_class
));
388 clock_class
->offset
= offset
;
389 BT_LOGT("Set clock class's offset (cycles): addr=%p, name=\"%s\", offset-cycles=%" PRId64
,
390 clock_class
, bt_ctf_clock_class_get_name(clock_class
), offset
);
396 bt_ctf_bool
bt_ctf_clock_class_is_absolute(struct bt_ctf_clock_class
*clock_class
)
401 BT_LOGW_STR("Invalid parameter: clock class is NULL.");
405 ret
= clock_class
->absolute
;
411 int bt_ctf_clock_class_set_is_absolute(struct bt_ctf_clock_class
*clock_class
,
412 bt_ctf_bool is_absolute
)
417 BT_LOGW_STR("Invalid parameter: clock class is NULL.");
422 if (clock_class
->frozen
) {
423 BT_LOGW("Invalid parameter: clock class is frozen: addr=%p, name=\"%s\"",
424 clock_class
, bt_ctf_clock_class_get_name(clock_class
));
429 clock_class
->absolute
= !!is_absolute
;
430 BT_LOGT("Set clock class's absolute flag: addr=%p, name=\"%s\", is-absolute=%d",
431 clock_class
, bt_ctf_clock_class_get_name(clock_class
),
438 const uint8_t *bt_ctf_clock_class_get_uuid(
439 struct bt_ctf_clock_class
*clock_class
)
444 BT_LOGW_STR("Invalid parameter: clock class is NULL.");
449 if (!clock_class
->uuid_set
) {
450 BT_LOGT("Clock class's UUID is not set: addr=%p, name=\"%s\"",
451 clock_class
, bt_ctf_clock_class_get_name(clock_class
));
456 ret
= clock_class
->uuid
;
462 int bt_ctf_clock_class_set_uuid(struct bt_ctf_clock_class
*clock_class
,
467 if (!clock_class
|| !uuid
) {
468 BT_LOGW("Invalid parameter: clock class or UUID is NULL: "
469 "clock-class-addr=%p, name=\"%s\", uuid-addr=%p",
470 clock_class
, bt_ctf_clock_class_get_name(clock_class
),
476 if (clock_class
->frozen
) {
477 BT_LOGW("Invalid parameter: clock class is frozen: addr=%p, name=\"%s\"",
478 clock_class
, bt_ctf_clock_class_get_name(clock_class
));
483 bt_uuid_copy(clock_class
->uuid
, uuid
);
484 clock_class
->uuid_set
= 1;
485 BT_LOGT("Set clock class's UUID: addr=%p, name=\"%s\", uuid=\"" BT_UUID_FMT
"\"",
486 clock_class
, bt_ctf_clock_class_get_name(clock_class
),
487 BT_UUID_FMT_VALUES(uuid
));
493 void bt_ctf_clock_class_freeze(struct bt_ctf_clock_class
*clock_class
)
495 if (!clock_class
|| clock_class
->frozen
) {
499 BT_LOGD("Freezing clock class: addr=%p, name=\"%s\"",
500 clock_class
, bt_ctf_clock_class_get_name(clock_class
));
501 clock_class
->frozen
= 1;
505 void bt_ctf_clock_class_destroy(struct bt_ctf_object
*obj
)
507 struct bt_ctf_clock_class
*clock_class
;
509 clock_class
= container_of(obj
, struct bt_ctf_clock_class
, base
);
510 BT_LOGD("Destroying clock class: addr=%p, name=\"%s\"",
511 obj
, bt_ctf_clock_class_get_name(clock_class
));
513 if (clock_class
->name
) {
514 g_string_free(clock_class
->name
, TRUE
);
517 if (clock_class
->description
) {
518 g_string_free(clock_class
->description
, TRUE
);
525 int bt_ctf_clock_class_compare(struct bt_ctf_clock_class
*clock_class_a
,
526 struct bt_ctf_clock_class
*clock_class_b
)
529 BT_ASSERT_DBG(clock_class_a
);
530 BT_ASSERT_DBG(clock_class_b
);
533 if (strcmp(clock_class_a
->name
->str
, clock_class_b
->name
->str
) != 0) {
534 BT_LOGT("Clock classes differ: different names: "
535 "cc-a-name=\"%s\", cc-b-name=\"%s\"",
536 clock_class_a
->name
->str
,
537 clock_class_b
->name
->str
);
542 if (clock_class_a
->description
) {
543 if (!clock_class_b
->description
) {
544 BT_LOGT_STR("Clock classes differ: clock class A has a "
545 "description, but clock class B does not.");
549 if (strcmp(clock_class_a
->name
->str
, clock_class_b
->name
->str
)
551 BT_LOGT("Clock classes differ: different descriptions: "
552 "cc-a-descr=\"%s\", cc-b-descr=\"%s\"",
553 clock_class_a
->description
->str
,
554 clock_class_b
->description
->str
);
558 if (clock_class_b
->description
) {
559 BT_LOGT_STR("Clock classes differ: clock class A has "
560 "no description, but clock class B has one.");
566 if (clock_class_a
->frequency
!= clock_class_b
->frequency
) {
567 BT_LOGT("Clock classes differ: different frequencies: "
568 "cc-a-freq=%" PRIu64
", cc-b-freq=%" PRIu64
,
569 clock_class_a
->frequency
,
570 clock_class_b
->frequency
);
575 if (clock_class_a
->precision
!= clock_class_b
->precision
) {
576 BT_LOGT("Clock classes differ: different precisions: "
577 "cc-a-freq=%" PRIu64
", cc-b-freq=%" PRIu64
,
578 clock_class_a
->precision
,
579 clock_class_b
->precision
);
583 /* Offset (seconds) */
584 if (clock_class_a
->offset_s
!= clock_class_b
->offset_s
) {
585 BT_LOGT("Clock classes differ: different offsets (seconds): "
586 "cc-a-offset-s=%" PRId64
", cc-b-offset-s=%" PRId64
,
587 clock_class_a
->offset_s
,
588 clock_class_b
->offset_s
);
592 /* Offset (cycles) */
593 if (clock_class_a
->offset
!= clock_class_b
->offset
) {
594 BT_LOGT("Clock classes differ: different offsets (cycles): "
595 "cc-a-offset-s=%" PRId64
", cc-b-offset-s=%" PRId64
,
596 clock_class_a
->offset
,
597 clock_class_b
->offset
);
602 if (clock_class_a
->uuid_set
) {
603 if (!clock_class_b
->uuid_set
) {
604 BT_LOGT_STR("Clock classes differ: clock class A has a "
605 "UUID, but clock class B does not.");
609 if (bt_uuid_compare(clock_class_a
->uuid
, clock_class_b
->uuid
) != 0) {
610 BT_LOGT("Clock classes differ: different UUIDs: "
611 "cc-a-uuid=\"" BT_UUID_FMT
"\", "
612 "cc-b-uuid=\"" BT_UUID_FMT
"\"",
613 BT_UUID_FMT_VALUES(clock_class_a
->uuid
),
614 BT_UUID_FMT_VALUES(clock_class_b
->uuid
));
618 if (clock_class_b
->uuid_set
) {
619 BT_LOGT_STR("Clock classes differ: clock class A has "
620 "no UUID, but clock class B has one.");
626 if (!!clock_class_a
->absolute
!= !!clock_class_b
->absolute
) {
627 BT_LOGT("Clock classes differ: one is absolute, the other "
628 "is not: cc-a-is-absolute=%d, cc-b-is-absolute=%d",
629 !!clock_class_a
->absolute
,
630 !!clock_class_b
->absolute
);