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 g_free(clock_class
->name
);
39 g_free(clock_class
->description
);
40 bt_object_pool_finalize(&clock_class
->cs_pool
);
45 void free_clock_snapshot(struct bt_clock_snapshot
*clock_snapshot
,
46 struct bt_clock_class
*clock_class
__attribute__((unused
)))
48 bt_clock_snapshot_destroy(clock_snapshot
);
52 void set_base_offset(struct bt_clock_class
*clock_class
)
54 clock_class
->base_offset
.overflows
= bt_util_get_base_offset_ns(
55 clock_class
->offset_seconds
, clock_class
->offset_cycles
,
56 clock_class
->frequency
, &clock_class
->base_offset
.value_ns
);
60 struct bt_clock_class
*bt_clock_class_create(bt_self_component
*self_comp
)
63 struct bt_clock_class
*clock_class
= NULL
;
65 BT_ASSERT_PRE_NO_ERROR();
66 BT_ASSERT_PRE_COMP_NON_NULL(self_comp
);
67 BT_LOGD_STR("Creating default clock class object");
69 clock_class
= g_new0(struct bt_clock_class
, 1);
71 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one clock class.");
75 bt_object_init_shared(&clock_class
->base
, destroy_clock_class
);
77 clock_class
->user_attributes
= bt_value_map_create();
78 if (!clock_class
->user_attributes
) {
79 BT_LIB_LOGE_APPEND_CAUSE(
80 "Failed to create a map value object.");
84 clock_class
->frequency
= UINT64_C(1000000000);
85 clock_class
->origin_is_unix_epoch
= BT_TRUE
;
86 set_base_offset(clock_class
);
87 ret
= bt_object_pool_initialize(&clock_class
->cs_pool
,
88 (bt_object_pool_new_object_func
) bt_clock_snapshot_new
,
89 (bt_object_pool_destroy_object_func
)
93 BT_LIB_LOGE_APPEND_CAUSE(
94 "Failed to initialize clock snapshot pool: ret=%d",
99 BT_LIB_LOGD("Created clock class object: %!+K", clock_class
);
103 BT_OBJECT_PUT_REF_AND_RESET(clock_class
);
110 const char *bt_clock_class_get_name(const struct bt_clock_class
*clock_class
)
112 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class
);
113 return clock_class
->name
;
117 enum bt_clock_class_set_name_status
bt_clock_class_set_name(
118 struct bt_clock_class
*clock_class
, const char *name
)
120 BT_ASSERT_PRE_NO_ERROR();
121 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class
);
122 BT_ASSERT_PRE_NAME_NON_NULL(name
);
123 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class
);
124 g_free(clock_class
->name
);
125 clock_class
->name
= g_strdup(name
);
126 BT_LIB_LOGD("Set clock class's name: %!+K", clock_class
);
127 return BT_FUNC_STATUS_OK
;
131 const char *bt_clock_class_get_description(
132 const struct bt_clock_class
*clock_class
)
134 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class
);
135 return clock_class
->description
;
139 enum bt_clock_class_set_description_status
bt_clock_class_set_description(
140 struct bt_clock_class
*clock_class
, const char *descr
)
142 BT_ASSERT_PRE_NO_ERROR();
143 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class
);
144 BT_ASSERT_PRE_DESCR_NON_NULL(descr
);
145 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class
);
146 g_free(clock_class
->description
);
147 clock_class
->description
= g_strdup(descr
);
148 BT_LIB_LOGD("Set clock class's description: %!+K",
150 return BT_FUNC_STATUS_OK
;
154 uint64_t bt_clock_class_get_frequency(const struct bt_clock_class
*clock_class
)
156 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class
);
157 return clock_class
->frequency
;
161 void bt_clock_class_set_frequency(struct bt_clock_class
*clock_class
,
164 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class
);
165 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class
);
166 BT_ASSERT_PRE("valid-frequency",
167 frequency
!= UINT64_C(-1) && frequency
!= 0,
168 "Invalid frequency: %![cc-]+K, new-freq=%" PRIu64
,
169 clock_class
, frequency
);
170 BT_ASSERT_PRE("offset-cycles-lt-frequency",
171 clock_class
->offset_cycles
< frequency
,
172 "Offset (cycles) is greater than clock class's frequency: "
173 "%![cc-]+K, new-freq=%" PRIu64
, clock_class
, frequency
);
174 clock_class
->frequency
= frequency
;
175 set_base_offset(clock_class
);
176 BT_LIB_LOGD("Set clock class's frequency: %!+K", clock_class
);
180 uint64_t bt_clock_class_get_precision(const struct bt_clock_class
*clock_class
)
182 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class
);
183 return clock_class
->precision
;
187 void bt_clock_class_set_precision(struct bt_clock_class
*clock_class
,
190 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class
);
191 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class
);
192 BT_ASSERT_PRE("valid-precision", precision
!= UINT64_C(-1),
193 "Invalid precision: %![cc-]+K, new-precision=%" PRIu64
,
194 clock_class
, precision
);
195 clock_class
->precision
= precision
;
196 BT_LIB_LOGD("Set clock class's precision: %!+K", clock_class
);
200 void bt_clock_class_get_offset(const struct bt_clock_class
*clock_class
,
201 int64_t *seconds
, uint64_t *cycles
)
203 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class
);
204 BT_ASSERT_PRE_DEV_NON_NULL("seconds-output", seconds
,
206 BT_ASSERT_PRE_DEV_NON_NULL("cycles-output", cycles
, "Cycles (output)");
207 *seconds
= clock_class
->offset_seconds
;
208 *cycles
= clock_class
->offset_cycles
;
212 void bt_clock_class_set_offset(struct bt_clock_class
*clock_class
,
213 int64_t seconds
, uint64_t cycles
)
215 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class
);
216 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class
);
217 BT_ASSERT_PRE("offset-cycles-lt-frequency",
218 cycles
< clock_class
->frequency
,
219 "Offset (cycles) is greater than clock class's frequency: "
220 "%![cc-]+K, new-offset-cycles=%" PRIu64
, clock_class
, cycles
);
221 clock_class
->offset_seconds
= seconds
;
222 clock_class
->offset_cycles
= cycles
;
223 set_base_offset(clock_class
);
224 BT_LIB_LOGD("Set clock class's offset: %!+K", clock_class
);
228 bt_bool
bt_clock_class_origin_is_unix_epoch(const struct bt_clock_class
*clock_class
)
230 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class
);
231 return (bool) clock_class
->origin_is_unix_epoch
;
235 void bt_clock_class_set_origin_is_unix_epoch(struct bt_clock_class
*clock_class
,
236 bt_bool origin_is_unix_epoch
)
238 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class
);
239 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class
);
240 clock_class
->origin_is_unix_epoch
= (bool) origin_is_unix_epoch
;
241 BT_LIB_LOGD("Set clock class's origin is Unix epoch property: %!+K",
246 bt_uuid
bt_clock_class_get_uuid(const struct bt_clock_class
*clock_class
)
248 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class
);
249 return clock_class
->uuid
.value
;
253 void bt_clock_class_set_uuid(struct bt_clock_class
*clock_class
,
256 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class
);
257 BT_ASSERT_PRE_UUID_NON_NULL(uuid
);
258 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class
);
259 bt_uuid_copy(clock_class
->uuid
.uuid
, uuid
);
260 clock_class
->uuid
.value
= clock_class
->uuid
.uuid
;
261 BT_LIB_LOGD("Set clock class's UUID: %!+K", clock_class
);
264 void _bt_clock_class_freeze(const struct bt_clock_class
*clock_class
)
266 BT_ASSERT(clock_class
);
268 if (clock_class
->frozen
) {
272 BT_LIB_LOGD("Freezing clock class's user attributes: %!+v",
273 clock_class
->user_attributes
);
274 bt_value_freeze(clock_class
->user_attributes
);
275 BT_LIB_LOGD("Freezing clock class: %!+K", clock_class
);
276 ((struct bt_clock_class
*) clock_class
)->frozen
= 1;
280 enum bt_clock_class_cycles_to_ns_from_origin_status
281 bt_clock_class_cycles_to_ns_from_origin(
282 const struct bt_clock_class
*clock_class
,
283 uint64_t cycles
, int64_t *ns
)
287 BT_ASSERT_PRE_DEV_NO_ERROR();
288 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class
);
289 BT_ASSERT_PRE_DEV_NON_NULL("nanoseconds-output", ns
,
290 "Nanoseconds (output)");
291 ret
= bt_util_ns_from_origin_clock_class(clock_class
, cycles
, ns
);
293 BT_LIB_LOGE_APPEND_CAUSE("Cannot convert cycles to nanoseconds "
294 "from origin for given clock class: "
295 "value overflows the signed 64-bit integer range: "
296 "%![cc-]+K, cycles=%" PRIu64
,
297 clock_class
, cycles
);
298 ret
= BT_FUNC_STATUS_OVERFLOW_ERROR
;
305 const struct bt_value
*bt_clock_class_borrow_user_attributes_const(
306 const struct bt_clock_class
*clock_class
)
308 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class
);
309 return clock_class
->user_attributes
;
313 struct bt_value
*bt_clock_class_borrow_user_attributes(
314 struct bt_clock_class
*clock_class
)
316 return (void *) bt_clock_class_borrow_user_attributes_const(
317 (void *) clock_class
);
321 void bt_clock_class_set_user_attributes(
322 struct bt_clock_class
*clock_class
,
323 const struct bt_value
*user_attributes
)
325 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class
);
326 BT_ASSERT_PRE_USER_ATTRS_NON_NULL(user_attributes
);
327 BT_ASSERT_PRE_USER_ATTRS_IS_MAP(user_attributes
);
328 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class
);
329 bt_object_put_ref_no_null_check(clock_class
->user_attributes
);
330 clock_class
->user_attributes
= (void *) user_attributes
;
331 bt_object_get_ref_no_null_check(clock_class
->user_attributes
);
335 void bt_clock_class_get_ref(const struct bt_clock_class
*clock_class
)
337 bt_object_get_ref(clock_class
);
341 void bt_clock_class_put_ref(const struct bt_clock_class
*clock_class
)
343 bt_object_put_ref(clock_class
);