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>
21 #include "lib/object.h"
22 #include "common/assert.h"
23 #include "lib/func-status.h"
24 #include "lib/value.h"
26 #define BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(_cc) \
27 BT_ASSERT_PRE_DEV_HOT("clock-class", (_cc), "Clock class", \
31 void destroy_clock_class(struct bt_object
*obj
)
33 struct bt_clock_class
*clock_class
= (void *) obj
;
35 BT_LIB_LOGD("Destroying clock class: %!+K", clock_class
);
36 BT_OBJECT_PUT_REF_AND_RESET(clock_class
->user_attributes
);
38 if (clock_class
->name
.str
) {
39 g_string_free(clock_class
->name
.str
, TRUE
);
40 clock_class
->name
.str
= NULL
;
41 clock_class
->name
.value
= NULL
;
44 if (clock_class
->description
.str
) {
45 g_string_free(clock_class
->description
.str
, TRUE
);
46 clock_class
->description
.str
= NULL
;
47 clock_class
->description
.value
= NULL
;
50 bt_object_pool_finalize(&clock_class
->cs_pool
);
55 void free_clock_snapshot(struct bt_clock_snapshot
*clock_snapshot
,
56 struct bt_clock_class
*clock_class
__attribute__((unused
)))
58 bt_clock_snapshot_destroy(clock_snapshot
);
62 void set_base_offset(struct bt_clock_class
*clock_class
)
64 clock_class
->base_offset
.overflows
= bt_util_get_base_offset_ns(
65 clock_class
->offset_seconds
, clock_class
->offset_cycles
,
66 clock_class
->frequency
, &clock_class
->base_offset
.value_ns
);
70 struct bt_clock_class
*bt_clock_class_create(bt_self_component
*self_comp
)
73 struct bt_clock_class
*clock_class
= NULL
;
75 BT_ASSERT_PRE_NO_ERROR();
76 BT_ASSERT_PRE_COMP_NON_NULL(self_comp
);
77 BT_LOGD_STR("Creating default clock class object");
79 clock_class
= g_new0(struct bt_clock_class
, 1);
81 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one clock class.");
85 bt_object_init_shared(&clock_class
->base
, destroy_clock_class
);
87 clock_class
->user_attributes
= bt_value_map_create();
88 if (!clock_class
->user_attributes
) {
89 BT_LIB_LOGE_APPEND_CAUSE(
90 "Failed to create a map value object.");
94 clock_class
->name
.str
= g_string_new(NULL
);
95 if (!clock_class
->name
.str
) {
96 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
100 clock_class
->description
.str
= g_string_new(NULL
);
101 if (!clock_class
->description
.str
) {
102 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
106 clock_class
->frequency
= UINT64_C(1000000000);
107 clock_class
->origin_is_unix_epoch
= BT_TRUE
;
108 set_base_offset(clock_class
);
109 ret
= bt_object_pool_initialize(&clock_class
->cs_pool
,
110 (bt_object_pool_new_object_func
) bt_clock_snapshot_new
,
111 (bt_object_pool_destroy_object_func
)
115 BT_LIB_LOGE_APPEND_CAUSE(
116 "Failed to initialize clock snapshot pool: ret=%d",
121 BT_LIB_LOGD("Created clock class object: %!+K", clock_class
);
125 BT_OBJECT_PUT_REF_AND_RESET(clock_class
);
132 const char *bt_clock_class_get_name(const struct bt_clock_class
*clock_class
)
134 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class
);
135 return clock_class
->name
.value
;
139 enum bt_clock_class_set_name_status
bt_clock_class_set_name(
140 struct bt_clock_class
*clock_class
, const char *name
)
142 BT_ASSERT_PRE_NO_ERROR();
143 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class
);
144 BT_ASSERT_PRE_NAME_NON_NULL(name
);
145 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class
);
146 g_string_assign(clock_class
->name
.str
, name
);
147 clock_class
->name
.value
= clock_class
->name
.str
->str
;
148 BT_LIB_LOGD("Set clock class's name: %!+K", clock_class
);
149 return BT_FUNC_STATUS_OK
;
153 const char *bt_clock_class_get_description(
154 const struct bt_clock_class
*clock_class
)
156 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class
);
157 return clock_class
->description
.value
;
161 enum bt_clock_class_set_description_status
bt_clock_class_set_description(
162 struct bt_clock_class
*clock_class
, const char *descr
)
164 BT_ASSERT_PRE_NO_ERROR();
165 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class
);
166 BT_ASSERT_PRE_DESCR_NON_NULL(descr
);
167 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class
);
168 g_string_assign(clock_class
->description
.str
, descr
);
169 clock_class
->description
.value
= clock_class
->description
.str
->str
;
170 BT_LIB_LOGD("Set clock class's description: %!+K",
172 return BT_FUNC_STATUS_OK
;
176 uint64_t bt_clock_class_get_frequency(const struct bt_clock_class
*clock_class
)
178 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class
);
179 return clock_class
->frequency
;
183 void bt_clock_class_set_frequency(struct bt_clock_class
*clock_class
,
186 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class
);
187 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class
);
188 BT_ASSERT_PRE("valid-frequency",
189 frequency
!= UINT64_C(-1) && frequency
!= 0,
190 "Invalid frequency: %![cc-]+K, new-freq=%" PRIu64
,
191 clock_class
, frequency
);
192 BT_ASSERT_PRE("offset-cycles-lt-frequency",
193 clock_class
->offset_cycles
< frequency
,
194 "Offset (cycles) is greater than clock class's frequency: "
195 "%![cc-]+K, new-freq=%" PRIu64
, clock_class
, frequency
);
196 clock_class
->frequency
= frequency
;
197 set_base_offset(clock_class
);
198 BT_LIB_LOGD("Set clock class's frequency: %!+K", clock_class
);
202 uint64_t bt_clock_class_get_precision(const struct bt_clock_class
*clock_class
)
204 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class
);
205 return clock_class
->precision
;
209 void bt_clock_class_set_precision(struct bt_clock_class
*clock_class
,
212 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class
);
213 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class
);
214 BT_ASSERT_PRE("valid-precision", precision
!= UINT64_C(-1),
215 "Invalid precision: %![cc-]+K, new-precision=%" PRIu64
,
216 clock_class
, precision
);
217 clock_class
->precision
= precision
;
218 BT_LIB_LOGD("Set clock class's precision: %!+K", clock_class
);
222 void bt_clock_class_get_offset(const struct bt_clock_class
*clock_class
,
223 int64_t *seconds
, uint64_t *cycles
)
225 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class
);
226 BT_ASSERT_PRE_DEV_NON_NULL("seconds-output", seconds
,
228 BT_ASSERT_PRE_DEV_NON_NULL("cycles-output", cycles
, "Cycles (output)");
229 *seconds
= clock_class
->offset_seconds
;
230 *cycles
= clock_class
->offset_cycles
;
234 void bt_clock_class_set_offset(struct bt_clock_class
*clock_class
,
235 int64_t seconds
, uint64_t cycles
)
237 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class
);
238 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class
);
239 BT_ASSERT_PRE("offset-cycles-lt-frequency",
240 cycles
< clock_class
->frequency
,
241 "Offset (cycles) is greater than clock class's frequency: "
242 "%![cc-]+K, new-offset-cycles=%" PRIu64
, clock_class
, cycles
);
243 clock_class
->offset_seconds
= seconds
;
244 clock_class
->offset_cycles
= cycles
;
245 set_base_offset(clock_class
);
246 BT_LIB_LOGD("Set clock class's offset: %!+K", clock_class
);
250 bt_bool
bt_clock_class_origin_is_unix_epoch(const struct bt_clock_class
*clock_class
)
252 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class
);
253 return (bool) clock_class
->origin_is_unix_epoch
;
257 void bt_clock_class_set_origin_is_unix_epoch(struct bt_clock_class
*clock_class
,
258 bt_bool origin_is_unix_epoch
)
260 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class
);
261 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class
);
262 clock_class
->origin_is_unix_epoch
= (bool) origin_is_unix_epoch
;
263 BT_LIB_LOGD("Set clock class's origin is Unix epoch property: %!+K",
268 bt_uuid
bt_clock_class_get_uuid(const struct bt_clock_class
*clock_class
)
270 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class
);
271 return clock_class
->uuid
.value
;
275 void bt_clock_class_set_uuid(struct bt_clock_class
*clock_class
,
278 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class
);
279 BT_ASSERT_PRE_UUID_NON_NULL(uuid
);
280 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class
);
281 bt_uuid_copy(clock_class
->uuid
.uuid
, uuid
);
282 clock_class
->uuid
.value
= clock_class
->uuid
.uuid
;
283 BT_LIB_LOGD("Set clock class's UUID: %!+K", clock_class
);
286 void _bt_clock_class_freeze(const struct bt_clock_class
*clock_class
)
288 BT_ASSERT(clock_class
);
290 if (clock_class
->frozen
) {
294 BT_LIB_LOGD("Freezing clock class's user attributes: %!+v",
295 clock_class
->user_attributes
);
296 bt_value_freeze(clock_class
->user_attributes
);
297 BT_LIB_LOGD("Freezing clock class: %!+K", clock_class
);
298 ((struct bt_clock_class
*) clock_class
)->frozen
= 1;
302 enum bt_clock_class_cycles_to_ns_from_origin_status
303 bt_clock_class_cycles_to_ns_from_origin(
304 const struct bt_clock_class
*clock_class
,
305 uint64_t cycles
, int64_t *ns
)
309 BT_ASSERT_PRE_DEV_NO_ERROR();
310 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class
);
311 BT_ASSERT_PRE_DEV_NON_NULL("nanoseconds-output", ns
,
312 "Nanoseconds (output)");
313 ret
= bt_util_ns_from_origin_clock_class(clock_class
, cycles
, ns
);
315 BT_LIB_LOGE_APPEND_CAUSE("Cannot convert cycles to nanoseconds "
316 "from origin for given clock class: "
317 "value overflows the signed 64-bit integer range: "
318 "%![cc-]+K, cycles=%" PRIu64
,
319 clock_class
, cycles
);
320 ret
= BT_FUNC_STATUS_OVERFLOW_ERROR
;
327 const struct bt_value
*bt_clock_class_borrow_user_attributes_const(
328 const struct bt_clock_class
*clock_class
)
330 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class
);
331 return clock_class
->user_attributes
;
335 struct bt_value
*bt_clock_class_borrow_user_attributes(
336 struct bt_clock_class
*clock_class
)
338 return (void *) bt_clock_class_borrow_user_attributes_const(
339 (void *) clock_class
);
343 void bt_clock_class_set_user_attributes(
344 struct bt_clock_class
*clock_class
,
345 const struct bt_value
*user_attributes
)
347 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class
);
348 BT_ASSERT_PRE_USER_ATTRS_NON_NULL(user_attributes
);
349 BT_ASSERT_PRE_USER_ATTRS_IS_MAP(user_attributes
);
350 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class
);
351 bt_object_put_ref_no_null_check(clock_class
->user_attributes
);
352 clock_class
->user_attributes
= (void *) user_attributes
;
353 bt_object_get_ref_no_null_check(clock_class
->user_attributes
);
357 void bt_clock_class_get_ref(const struct bt_clock_class
*clock_class
)
359 bt_object_get_ref(clock_class
);
363 void bt_clock_class_put_ref(const struct bt_clock_class
*clock_class
)
365 bt_object_put_ref(clock_class
);