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
);
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
);
131 const char *bt_clock_class_get_name(const struct bt_clock_class
*clock_class
)
133 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class
);
134 return clock_class
->name
.value
;
137 enum bt_clock_class_set_name_status
bt_clock_class_set_name(
138 struct bt_clock_class
*clock_class
, const char *name
)
140 BT_ASSERT_PRE_NO_ERROR();
141 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class
);
142 BT_ASSERT_PRE_NAME_NON_NULL(name
);
143 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class
);
144 g_string_assign(clock_class
->name
.str
, name
);
145 clock_class
->name
.value
= clock_class
->name
.str
->str
;
146 BT_LIB_LOGD("Set clock class's name: %!+K", clock_class
);
147 return BT_FUNC_STATUS_OK
;
150 const char *bt_clock_class_get_description(
151 const struct bt_clock_class
*clock_class
)
153 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class
);
154 return clock_class
->description
.value
;
157 enum bt_clock_class_set_description_status
bt_clock_class_set_description(
158 struct bt_clock_class
*clock_class
, const char *descr
)
160 BT_ASSERT_PRE_NO_ERROR();
161 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class
);
162 BT_ASSERT_PRE_DESCR_NON_NULL(descr
);
163 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class
);
164 g_string_assign(clock_class
->description
.str
, descr
);
165 clock_class
->description
.value
= clock_class
->description
.str
->str
;
166 BT_LIB_LOGD("Set clock class's description: %!+K",
168 return BT_FUNC_STATUS_OK
;
171 uint64_t bt_clock_class_get_frequency(const struct bt_clock_class
*clock_class
)
173 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class
);
174 return clock_class
->frequency
;
177 void bt_clock_class_set_frequency(struct bt_clock_class
*clock_class
,
180 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class
);
181 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class
);
182 BT_ASSERT_PRE("valid-frequency",
183 frequency
!= UINT64_C(-1) && frequency
!= 0,
184 "Invalid frequency: %![cc-]+K, new-freq=%" PRIu64
,
185 clock_class
, frequency
);
186 BT_ASSERT_PRE("offset-cycles-lt-frequency",
187 clock_class
->offset_cycles
< frequency
,
188 "Offset (cycles) is greater than clock class's frequency: "
189 "%![cc-]+K, new-freq=%" PRIu64
, clock_class
, frequency
);
190 clock_class
->frequency
= frequency
;
191 set_base_offset(clock_class
);
192 BT_LIB_LOGD("Set clock class's frequency: %!+K", clock_class
);
195 uint64_t bt_clock_class_get_precision(const struct bt_clock_class
*clock_class
)
197 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class
);
198 return clock_class
->precision
;
201 void bt_clock_class_set_precision(struct bt_clock_class
*clock_class
,
204 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class
);
205 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class
);
206 BT_ASSERT_PRE("valid-precision", precision
!= UINT64_C(-1),
207 "Invalid precision: %![cc-]+K, new-precision=%" PRIu64
,
208 clock_class
, precision
);
209 clock_class
->precision
= precision
;
210 BT_LIB_LOGD("Set clock class's precision: %!+K", clock_class
);
213 void bt_clock_class_get_offset(const struct bt_clock_class
*clock_class
,
214 int64_t *seconds
, uint64_t *cycles
)
216 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class
);
217 BT_ASSERT_PRE_DEV_NON_NULL("seconds-output", seconds
,
219 BT_ASSERT_PRE_DEV_NON_NULL("cycles-output", cycles
, "Cycles (output)");
220 *seconds
= clock_class
->offset_seconds
;
221 *cycles
= clock_class
->offset_cycles
;
224 void bt_clock_class_set_offset(struct bt_clock_class
*clock_class
,
225 int64_t seconds
, uint64_t cycles
)
227 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class
);
228 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class
);
229 BT_ASSERT_PRE("offset-cycles-lt-frequency",
230 cycles
< clock_class
->frequency
,
231 "Offset (cycles) is greater than clock class's frequency: "
232 "%![cc-]+K, new-offset-cycles=%" PRIu64
, clock_class
, cycles
);
233 clock_class
->offset_seconds
= seconds
;
234 clock_class
->offset_cycles
= cycles
;
235 set_base_offset(clock_class
);
236 BT_LIB_LOGD("Set clock class's offset: %!+K", clock_class
);
239 bt_bool
bt_clock_class_origin_is_unix_epoch(const struct bt_clock_class
*clock_class
)
241 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class
);
242 return (bool) clock_class
->origin_is_unix_epoch
;
245 void bt_clock_class_set_origin_is_unix_epoch(struct bt_clock_class
*clock_class
,
246 bt_bool origin_is_unix_epoch
)
248 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class
);
249 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class
);
250 clock_class
->origin_is_unix_epoch
= (bool) origin_is_unix_epoch
;
251 BT_LIB_LOGD("Set clock class's origin is Unix epoch property: %!+K",
255 bt_uuid
bt_clock_class_get_uuid(const struct bt_clock_class
*clock_class
)
257 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class
);
258 return clock_class
->uuid
.value
;
261 void bt_clock_class_set_uuid(struct bt_clock_class
*clock_class
,
264 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class
);
265 BT_ASSERT_PRE_UUID_NON_NULL(uuid
);
266 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class
);
267 bt_uuid_copy(clock_class
->uuid
.uuid
, uuid
);
268 clock_class
->uuid
.value
= clock_class
->uuid
.uuid
;
269 BT_LIB_LOGD("Set clock class's UUID: %!+K", clock_class
);
273 void _bt_clock_class_freeze(const struct bt_clock_class
*clock_class
)
275 BT_ASSERT(clock_class
);
277 if (clock_class
->frozen
) {
281 BT_LIB_LOGD("Freezing clock class's user attributes: %!+v",
282 clock_class
->user_attributes
);
283 bt_value_freeze(clock_class
->user_attributes
);
284 BT_LIB_LOGD("Freezing clock class: %!+K", clock_class
);
285 ((struct bt_clock_class
*) clock_class
)->frozen
= 1;
288 enum bt_clock_class_cycles_to_ns_from_origin_status
289 bt_clock_class_cycles_to_ns_from_origin(
290 const struct bt_clock_class
*clock_class
,
291 uint64_t cycles
, int64_t *ns
)
295 BT_ASSERT_PRE_DEV_NO_ERROR();
296 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class
);
297 BT_ASSERT_PRE_DEV_NON_NULL("nanoseconds-output", ns
,
298 "Nanoseconds (output)");
299 ret
= bt_util_ns_from_origin_clock_class(clock_class
, cycles
, ns
);
301 BT_LIB_LOGE_APPEND_CAUSE("Cannot convert cycles to nanoseconds "
302 "from origin for given clock class: "
303 "value overflows the signed 64-bit integer range: "
304 "%![cc-]+K, cycles=%" PRIu64
,
305 clock_class
, cycles
);
306 ret
= BT_FUNC_STATUS_OVERFLOW_ERROR
;
312 const struct bt_value
*bt_clock_class_borrow_user_attributes_const(
313 const struct bt_clock_class
*clock_class
)
315 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class
);
316 return clock_class
->user_attributes
;
319 struct bt_value
*bt_clock_class_borrow_user_attributes(
320 struct bt_clock_class
*clock_class
)
322 return (void *) bt_clock_class_borrow_user_attributes_const(
323 (void *) clock_class
);
326 void bt_clock_class_set_user_attributes(
327 struct bt_clock_class
*clock_class
,
328 const struct bt_value
*user_attributes
)
330 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class
);
331 BT_ASSERT_PRE_USER_ATTRS_NON_NULL(user_attributes
);
332 BT_ASSERT_PRE_USER_ATTRS_IS_MAP(user_attributes
);
333 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class
);
334 bt_object_put_ref_no_null_check(clock_class
->user_attributes
);
335 clock_class
->user_attributes
= (void *) user_attributes
;
336 bt_object_get_ref_no_null_check(clock_class
->user_attributes
);
339 void bt_clock_class_get_ref(const struct bt_clock_class
*clock_class
)
341 bt_object_get_ref(clock_class
);
344 void bt_clock_class_put_ref(const struct bt_clock_class
*clock_class
)
346 bt_object_put_ref(clock_class
);