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((_cc), "Clock class", ": %!+K", (_cc))
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
)
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
);
69 struct bt_clock_class
*bt_clock_class_create(bt_self_component
*self_comp
)
72 struct bt_clock_class
*clock_class
= NULL
;
74 BT_ASSERT_PRE_NO_ERROR();
75 BT_ASSERT_PRE_NON_NULL(self_comp
, "Self component");
76 BT_LOGD_STR("Creating default clock class object");
78 clock_class
= g_new0(struct bt_clock_class
, 1);
80 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one clock class.");
84 bt_object_init_shared(&clock_class
->base
, destroy_clock_class
);
86 clock_class
->user_attributes
= bt_value_map_create();
87 if (!clock_class
->user_attributes
) {
88 BT_LIB_LOGE_APPEND_CAUSE(
89 "Failed to create a map value object.");
93 clock_class
->name
.str
= g_string_new(NULL
);
94 if (!clock_class
->name
.str
) {
95 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
99 clock_class
->description
.str
= g_string_new(NULL
);
100 if (!clock_class
->description
.str
) {
101 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
105 clock_class
->frequency
= UINT64_C(1000000000);
106 clock_class
->origin_is_unix_epoch
= BT_TRUE
;
107 set_base_offset(clock_class
);
108 ret
= bt_object_pool_initialize(&clock_class
->cs_pool
,
109 (bt_object_pool_new_object_func
) bt_clock_snapshot_new
,
110 (bt_object_pool_destroy_object_func
)
114 BT_LIB_LOGE_APPEND_CAUSE(
115 "Failed to initialize clock snapshot pool: ret=%d",
120 BT_LIB_LOGD("Created clock class object: %!+K", clock_class
);
124 BT_OBJECT_PUT_REF_AND_RESET(clock_class
);
130 const char *bt_clock_class_get_name(const struct bt_clock_class
*clock_class
)
132 BT_ASSERT_PRE_DEV_NON_NULL(clock_class
, "Clock class");
133 return clock_class
->name
.value
;
136 enum bt_clock_class_set_name_status
bt_clock_class_set_name(
137 struct bt_clock_class
*clock_class
, const char *name
)
139 BT_ASSERT_PRE_NO_ERROR();
140 BT_ASSERT_PRE_NON_NULL(clock_class
, "Clock class");
141 BT_ASSERT_PRE_NON_NULL(name
, "Name");
142 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class
);
143 g_string_assign(clock_class
->name
.str
, name
);
144 clock_class
->name
.value
= clock_class
->name
.str
->str
;
145 BT_LIB_LOGD("Set clock class's name: %!+K", clock_class
);
146 return BT_FUNC_STATUS_OK
;
149 const char *bt_clock_class_get_description(
150 const struct bt_clock_class
*clock_class
)
152 BT_ASSERT_PRE_DEV_NON_NULL(clock_class
, "Clock class");
153 return clock_class
->description
.value
;
156 enum bt_clock_class_set_description_status
bt_clock_class_set_description(
157 struct bt_clock_class
*clock_class
, const char *descr
)
159 BT_ASSERT_PRE_NO_ERROR();
160 BT_ASSERT_PRE_NON_NULL(clock_class
, "Clock class");
161 BT_ASSERT_PRE_NON_NULL(descr
, "Description");
162 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class
);
163 g_string_assign(clock_class
->description
.str
, descr
);
164 clock_class
->description
.value
= clock_class
->description
.str
->str
;
165 BT_LIB_LOGD("Set clock class's description: %!+K",
167 return BT_FUNC_STATUS_OK
;
170 uint64_t bt_clock_class_get_frequency(const struct bt_clock_class
*clock_class
)
172 BT_ASSERT_PRE_DEV_NON_NULL(clock_class
, "Clock class");
173 return clock_class
->frequency
;
176 void bt_clock_class_set_frequency(struct bt_clock_class
*clock_class
,
179 BT_ASSERT_PRE_NON_NULL(clock_class
, "Clock class");
180 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class
);
181 BT_ASSERT_PRE(frequency
!= UINT64_C(-1) && frequency
!= 0,
182 "Invalid frequency: %![cc-]+K, new-freq=%" PRIu64
,
183 clock_class
, frequency
);
184 BT_ASSERT_PRE(clock_class
->offset_cycles
< frequency
,
185 "Offset (cycles) is greater than clock class's frequency: "
186 "%![cc-]+K, new-freq=%" PRIu64
, clock_class
, frequency
);
187 clock_class
->frequency
= frequency
;
188 set_base_offset(clock_class
);
189 BT_LIB_LOGD("Set clock class's frequency: %!+K", clock_class
);
192 uint64_t bt_clock_class_get_precision(const struct bt_clock_class
*clock_class
)
194 BT_ASSERT_PRE_DEV_NON_NULL(clock_class
, "Clock class");
195 return clock_class
->precision
;
198 void bt_clock_class_set_precision(struct bt_clock_class
*clock_class
,
201 BT_ASSERT_PRE_NON_NULL(clock_class
, "Clock class");
202 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class
);
203 BT_ASSERT_PRE(precision
!= UINT64_C(-1),
204 "Invalid precision: %![cc-]+K, new-precision=%" PRIu64
,
205 clock_class
, precision
);
206 clock_class
->precision
= precision
;
207 BT_LIB_LOGD("Set clock class's precision: %!+K", clock_class
);
210 void bt_clock_class_get_offset(const struct bt_clock_class
*clock_class
,
211 int64_t *seconds
, uint64_t *cycles
)
213 BT_ASSERT_PRE_DEV_NON_NULL(clock_class
, "Clock class");
214 BT_ASSERT_PRE_DEV_NON_NULL(seconds
, "Seconds (output)");
215 BT_ASSERT_PRE_DEV_NON_NULL(cycles
, "Cycles (output)");
216 *seconds
= clock_class
->offset_seconds
;
217 *cycles
= clock_class
->offset_cycles
;
220 void bt_clock_class_set_offset(struct bt_clock_class
*clock_class
,
221 int64_t seconds
, uint64_t cycles
)
223 BT_ASSERT_PRE_NON_NULL(clock_class
, "Clock class");
224 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class
);
225 BT_ASSERT_PRE(cycles
< clock_class
->frequency
,
226 "Offset (cycles) is greater than clock class's frequency: "
227 "%![cc-]+K, new-offset-cycles=%" PRIu64
, clock_class
, cycles
);
228 clock_class
->offset_seconds
= seconds
;
229 clock_class
->offset_cycles
= cycles
;
230 set_base_offset(clock_class
);
231 BT_LIB_LOGD("Set clock class's offset: %!+K", clock_class
);
234 bt_bool
bt_clock_class_origin_is_unix_epoch(const struct bt_clock_class
*clock_class
)
236 BT_ASSERT_PRE_DEV_NON_NULL(clock_class
, "Clock class");
237 return (bool) clock_class
->origin_is_unix_epoch
;
240 void bt_clock_class_set_origin_is_unix_epoch(struct bt_clock_class
*clock_class
,
241 bt_bool origin_is_unix_epoch
)
243 BT_ASSERT_PRE_NON_NULL(clock_class
, "Clock class");
244 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class
);
245 clock_class
->origin_is_unix_epoch
= (bool) origin_is_unix_epoch
;
246 BT_LIB_LOGD("Set clock class's origin is Unix epoch property: %!+K",
250 bt_uuid
bt_clock_class_get_uuid(const struct bt_clock_class
*clock_class
)
252 BT_ASSERT_PRE_DEV_NON_NULL(clock_class
, "Clock class");
253 return clock_class
->uuid
.value
;
256 void bt_clock_class_set_uuid(struct bt_clock_class
*clock_class
,
259 BT_ASSERT_PRE_NON_NULL(clock_class
, "Clock class");
260 BT_ASSERT_PRE_NON_NULL(uuid
, "UUID");
261 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class
);
262 bt_uuid_copy(clock_class
->uuid
.uuid
, uuid
);
263 clock_class
->uuid
.value
= clock_class
->uuid
.uuid
;
264 BT_LIB_LOGD("Set clock class's UUID: %!+K", clock_class
);
268 void _bt_clock_class_freeze(const struct bt_clock_class
*clock_class
)
270 BT_ASSERT(clock_class
);
272 if (clock_class
->frozen
) {
276 BT_LIB_LOGD("Freezing clock class's user attributes: %!+v",
277 clock_class
->user_attributes
);
278 bt_value_freeze(clock_class
->user_attributes
);
279 BT_LIB_LOGD("Freezing clock class: %!+K", clock_class
);
280 ((struct bt_clock_class
*) clock_class
)->frozen
= 1;
283 enum bt_clock_class_cycles_to_ns_from_origin_status
284 bt_clock_class_cycles_to_ns_from_origin(
285 const struct bt_clock_class
*clock_class
,
286 uint64_t cycles
, int64_t *ns
)
290 BT_ASSERT_PRE_DEV_NO_ERROR();
291 BT_ASSERT_PRE_DEV_NON_NULL(clock_class
, "Clock class");
292 BT_ASSERT_PRE_DEV_NON_NULL(ns
, "Nanoseconds (output)");
293 ret
= bt_util_ns_from_origin_clock_class(clock_class
, cycles
, ns
);
295 BT_LIB_LOGE_APPEND_CAUSE("Cannot convert cycles to nanoseconds "
296 "from origin for given clock class: "
297 "value overflows the signed 64-bit integer range: "
298 "%![cc-]+K, cycles=%" PRIu64
,
299 clock_class
, cycles
);
300 ret
= BT_FUNC_STATUS_OVERFLOW_ERROR
;
306 const struct bt_value
*bt_clock_class_borrow_user_attributes_const(
307 const struct bt_clock_class
*clock_class
)
309 BT_ASSERT_PRE_DEV_NON_NULL(clock_class
, "Clock class");
310 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
);
320 void bt_clock_class_set_user_attributes(
321 struct bt_clock_class
*clock_class
,
322 const struct bt_value
*user_attributes
)
324 BT_ASSERT_PRE_NON_NULL(clock_class
, "Clock class");
325 BT_ASSERT_PRE_NON_NULL(user_attributes
, "User attributes");
326 BT_ASSERT_PRE(user_attributes
->type
== BT_VALUE_TYPE_MAP
,
327 "User attributes object is not a map value object.");
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
);
334 void bt_clock_class_get_ref(const struct bt_clock_class
*clock_class
)
336 bt_object_get_ref(clock_class
);
339 void bt_clock_class_put_ref(const struct bt_clock_class
*clock_class
)
341 bt_object_put_ref(clock_class
);