2 * SPDX-License-Identifier: MIT
4 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
5 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8 #define BT_LOG_TAG "LIB/CLOCK-CLASS"
9 #include "lib/logging.h"
11 #include "lib/assert-cond.h"
12 #include "common/uuid.h"
13 #include <babeltrace2/trace-ir/clock-class.h>
14 #include "clock-class.h"
15 #include "clock-snapshot.h"
17 #include "compat/compiler.h"
18 #include <babeltrace2/types.h>
19 #include "compat/string.h"
22 #include "lib/object.h"
23 #include "common/assert.h"
24 #include "lib/func-status.h"
25 #include "lib/value.h"
27 #define BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(_cc) \
28 BT_ASSERT_PRE_DEV_HOT("clock-class", (_cc), "Clock class", \
32 void destroy_clock_class(struct bt_object
*obj
)
34 struct bt_clock_class
*clock_class
= (void *) obj
;
36 BT_LIB_LOGD("Destroying clock class: %!+K", clock_class
);
37 BT_OBJECT_PUT_REF_AND_RESET(clock_class
->user_attributes
);
39 if (clock_class
->name
.str
) {
40 g_string_free(clock_class
->name
.str
, TRUE
);
41 clock_class
->name
.str
= NULL
;
42 clock_class
->name
.value
= NULL
;
45 if (clock_class
->description
.str
) {
46 g_string_free(clock_class
->description
.str
, TRUE
);
47 clock_class
->description
.str
= NULL
;
48 clock_class
->description
.value
= NULL
;
51 bt_object_pool_finalize(&clock_class
->cs_pool
);
56 void free_clock_snapshot(struct bt_clock_snapshot
*clock_snapshot
,
57 struct bt_clock_class
*clock_class
)
59 bt_clock_snapshot_destroy(clock_snapshot
);
63 void set_base_offset(struct bt_clock_class
*clock_class
)
65 clock_class
->base_offset
.overflows
= bt_util_get_base_offset_ns(
66 clock_class
->offset_seconds
, clock_class
->offset_cycles
,
67 clock_class
->frequency
, &clock_class
->base_offset
.value_ns
);
71 struct bt_clock_class
*bt_clock_class_create(bt_self_component
*self_comp
)
74 struct bt_clock_class
*clock_class
= NULL
;
76 BT_ASSERT_PRE_NO_ERROR();
77 BT_ASSERT_PRE_COMP_NON_NULL(self_comp
);
78 BT_LOGD_STR("Creating default clock class object");
80 clock_class
= g_new0(struct bt_clock_class
, 1);
82 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one clock class.");
86 bt_object_init_shared(&clock_class
->base
, destroy_clock_class
);
88 clock_class
->user_attributes
= bt_value_map_create();
89 if (!clock_class
->user_attributes
) {
90 BT_LIB_LOGE_APPEND_CAUSE(
91 "Failed to create a map value object.");
95 clock_class
->name
.str
= g_string_new(NULL
);
96 if (!clock_class
->name
.str
) {
97 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
101 clock_class
->description
.str
= g_string_new(NULL
);
102 if (!clock_class
->description
.str
) {
103 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
107 clock_class
->frequency
= UINT64_C(1000000000);
108 clock_class
->origin_is_unix_epoch
= BT_TRUE
;
109 set_base_offset(clock_class
);
110 ret
= bt_object_pool_initialize(&clock_class
->cs_pool
,
111 (bt_object_pool_new_object_func
) bt_clock_snapshot_new
,
112 (bt_object_pool_destroy_object_func
)
116 BT_LIB_LOGE_APPEND_CAUSE(
117 "Failed to initialize clock snapshot pool: ret=%d",
122 BT_LIB_LOGD("Created clock class object: %!+K", clock_class
);
126 BT_OBJECT_PUT_REF_AND_RESET(clock_class
);
133 const char *bt_clock_class_get_name(const struct bt_clock_class
*clock_class
)
135 BT_ASSERT_PRE_NO_ERROR();
136 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class
);
137 return clock_class
->name
.value
;
141 enum bt_clock_class_set_name_status
bt_clock_class_set_name(
142 struct bt_clock_class
*clock_class
, const char *name
)
144 BT_ASSERT_PRE_NO_ERROR();
145 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class
);
146 BT_ASSERT_PRE_NAME_NON_NULL(name
);
147 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class
);
148 g_string_assign(clock_class
->name
.str
, name
);
149 clock_class
->name
.value
= clock_class
->name
.str
->str
;
150 BT_LIB_LOGD("Set clock class's name: %!+K", clock_class
);
151 return BT_FUNC_STATUS_OK
;
155 const char *bt_clock_class_get_description(
156 const struct bt_clock_class
*clock_class
)
158 BT_ASSERT_PRE_NO_ERROR();
159 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class
);
160 return clock_class
->description
.value
;
164 enum bt_clock_class_set_description_status
bt_clock_class_set_description(
165 struct bt_clock_class
*clock_class
, const char *descr
)
167 BT_ASSERT_PRE_NO_ERROR();
168 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class
);
169 BT_ASSERT_PRE_DESCR_NON_NULL(descr
);
170 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class
);
171 g_string_assign(clock_class
->description
.str
, descr
);
172 clock_class
->description
.value
= clock_class
->description
.str
->str
;
173 BT_LIB_LOGD("Set clock class's description: %!+K",
175 return BT_FUNC_STATUS_OK
;
179 uint64_t bt_clock_class_get_frequency(const struct bt_clock_class
*clock_class
)
181 BT_ASSERT_PRE_NO_ERROR();
182 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class
);
183 return clock_class
->frequency
;
187 void bt_clock_class_set_frequency(struct bt_clock_class
*clock_class
,
190 BT_ASSERT_PRE_NO_ERROR();
191 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class
);
192 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class
);
193 BT_ASSERT_PRE("valid-frequency",
194 frequency
!= UINT64_C(-1) && frequency
!= 0,
195 "Invalid frequency: %![cc-]+K, new-freq=%" PRIu64
,
196 clock_class
, frequency
);
197 BT_ASSERT_PRE("offset-cycles-lt-frequency",
198 clock_class
->offset_cycles
< frequency
,
199 "Offset (cycles) is greater than clock class's frequency: "
200 "%![cc-]+K, new-freq=%" PRIu64
, clock_class
, frequency
);
201 clock_class
->frequency
= frequency
;
202 set_base_offset(clock_class
);
203 BT_LIB_LOGD("Set clock class's frequency: %!+K", clock_class
);
207 uint64_t bt_clock_class_get_precision(const struct bt_clock_class
*clock_class
)
209 BT_ASSERT_PRE_NO_ERROR();
210 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class
);
211 return clock_class
->precision
;
215 void bt_clock_class_set_precision(struct bt_clock_class
*clock_class
,
218 BT_ASSERT_PRE_NO_ERROR();
219 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class
);
220 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class
);
221 BT_ASSERT_PRE("valid-precision", precision
!= UINT64_C(-1),
222 "Invalid precision: %![cc-]+K, new-precision=%" PRIu64
,
223 clock_class
, precision
);
224 clock_class
->precision
= precision
;
225 BT_LIB_LOGD("Set clock class's precision: %!+K", clock_class
);
229 void bt_clock_class_get_offset(const struct bt_clock_class
*clock_class
,
230 int64_t *seconds
, uint64_t *cycles
)
232 BT_ASSERT_PRE_NO_ERROR();
233 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class
);
234 BT_ASSERT_PRE_DEV_NON_NULL("seconds-output", seconds
,
236 BT_ASSERT_PRE_DEV_NON_NULL("cycles-output", cycles
, "Cycles (output)");
237 *seconds
= clock_class
->offset_seconds
;
238 *cycles
= clock_class
->offset_cycles
;
242 void bt_clock_class_set_offset(struct bt_clock_class
*clock_class
,
243 int64_t seconds
, uint64_t cycles
)
245 BT_ASSERT_PRE_NO_ERROR();
246 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class
);
247 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class
);
248 BT_ASSERT_PRE("offset-cycles-lt-frequency",
249 cycles
< clock_class
->frequency
,
250 "Offset (cycles) is greater than clock class's frequency: "
251 "%![cc-]+K, new-offset-cycles=%" PRIu64
, clock_class
, cycles
);
252 clock_class
->offset_seconds
= seconds
;
253 clock_class
->offset_cycles
= cycles
;
254 set_base_offset(clock_class
);
255 BT_LIB_LOGD("Set clock class's offset: %!+K", clock_class
);
259 bt_bool
bt_clock_class_origin_is_unix_epoch(const struct bt_clock_class
*clock_class
)
261 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class
);
262 return (bool) clock_class
->origin_is_unix_epoch
;
266 void bt_clock_class_set_origin_is_unix_epoch(struct bt_clock_class
*clock_class
,
267 bt_bool origin_is_unix_epoch
)
269 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class
);
270 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class
);
271 clock_class
->origin_is_unix_epoch
= (bool) origin_is_unix_epoch
;
272 BT_LIB_LOGD("Set clock class's origin is Unix epoch property: %!+K",
277 bt_uuid
bt_clock_class_get_uuid(const struct bt_clock_class
*clock_class
)
279 BT_ASSERT_PRE_NO_ERROR();
280 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class
);
281 return clock_class
->uuid
.value
;
285 void bt_clock_class_set_uuid(struct bt_clock_class
*clock_class
,
288 BT_ASSERT_PRE_NO_ERROR();
289 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class
);
290 BT_ASSERT_PRE_UUID_NON_NULL(uuid
);
291 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class
);
292 bt_uuid_copy(clock_class
->uuid
.uuid
, uuid
);
293 clock_class
->uuid
.value
= clock_class
->uuid
.uuid
;
294 BT_LIB_LOGD("Set clock class's UUID: %!+K", clock_class
);
297 void _bt_clock_class_freeze(const struct bt_clock_class
*clock_class
)
299 BT_ASSERT(clock_class
);
301 if (clock_class
->frozen
) {
305 BT_LIB_LOGD("Freezing clock class's user attributes: %!+v",
306 clock_class
->user_attributes
);
307 bt_value_freeze(clock_class
->user_attributes
);
308 BT_LIB_LOGD("Freezing clock class: %!+K", clock_class
);
309 ((struct bt_clock_class
*) clock_class
)->frozen
= 1;
313 enum bt_clock_class_cycles_to_ns_from_origin_status
314 bt_clock_class_cycles_to_ns_from_origin(
315 const struct bt_clock_class
*clock_class
,
316 uint64_t cycles
, int64_t *ns
)
320 BT_ASSERT_PRE_DEV_NO_ERROR();
321 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class
);
322 BT_ASSERT_PRE_DEV_NON_NULL("nanoseconds-output", ns
,
323 "Nanoseconds (output)");
324 ret
= bt_util_ns_from_origin_clock_class(clock_class
, cycles
, ns
);
326 BT_LIB_LOGE_APPEND_CAUSE("Cannot convert cycles to nanoseconds "
327 "from origin for given clock class: "
328 "value overflows the signed 64-bit integer range: "
329 "%![cc-]+K, cycles=%" PRIu64
,
330 clock_class
, cycles
);
331 ret
= BT_FUNC_STATUS_OVERFLOW_ERROR
;
338 const struct bt_value
*bt_clock_class_borrow_user_attributes_const(
339 const struct bt_clock_class
*clock_class
)
341 BT_ASSERT_PRE_NO_ERROR();
342 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class
);
343 return clock_class
->user_attributes
;
347 struct bt_value
*bt_clock_class_borrow_user_attributes(
348 struct bt_clock_class
*clock_class
)
350 return (void *) bt_clock_class_borrow_user_attributes_const(
351 (void *) clock_class
);
355 void bt_clock_class_set_user_attributes(
356 struct bt_clock_class
*clock_class
,
357 const struct bt_value
*user_attributes
)
359 BT_ASSERT_PRE_NO_ERROR();
360 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class
);
361 BT_ASSERT_PRE_USER_ATTRS_NON_NULL(user_attributes
);
362 BT_ASSERT_PRE_USER_ATTRS_IS_MAP(user_attributes
);
363 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class
);
364 bt_object_put_ref_no_null_check(clock_class
->user_attributes
);
365 clock_class
->user_attributes
= (void *) user_attributes
;
366 bt_object_get_ref_no_null_check(clock_class
->user_attributes
);
370 void bt_clock_class_get_ref(const struct bt_clock_class
*clock_class
)
372 bt_object_get_ref(clock_class
);
376 void bt_clock_class_put_ref(const struct bt_clock_class
*clock_class
)
378 bt_object_put_ref(clock_class
);