2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
3 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 #define BT_LOG_TAG "LIB/CLOCK-CLASS"
25 #include "lib/logging.h"
27 #include "lib/assert-pre.h"
28 #include "common/uuid.h"
29 #include <babeltrace2/trace-ir/clock-class.h>
30 #include "clock-class.h"
31 #include "clock-snapshot.h"
33 #include "compat/compiler.h"
34 #include <babeltrace2/types.h>
35 #include "compat/string.h"
38 #include "lib/object.h"
39 #include "common/assert.h"
40 #include "lib/func-status.h"
41 #include "lib/value.h"
43 #define BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(_cc) \
44 BT_ASSERT_PRE_DEV_HOT((_cc), "Clock class", ": %!+K", (_cc))
47 void destroy_clock_class(struct bt_object
*obj
)
49 struct bt_clock_class
*clock_class
= (void *) obj
;
51 BT_LIB_LOGD("Destroying clock class: %!+K", clock_class
);
52 BT_OBJECT_PUT_REF_AND_RESET(clock_class
->user_attributes
);
54 if (clock_class
->name
.str
) {
55 g_string_free(clock_class
->name
.str
, TRUE
);
56 clock_class
->name
.str
= NULL
;
57 clock_class
->name
.value
= NULL
;
60 if (clock_class
->description
.str
) {
61 g_string_free(clock_class
->description
.str
, TRUE
);
62 clock_class
->description
.str
= NULL
;
63 clock_class
->description
.value
= NULL
;
66 bt_object_pool_finalize(&clock_class
->cs_pool
);
71 void free_clock_snapshot(struct bt_clock_snapshot
*clock_snapshot
,
72 struct bt_clock_class
*clock_class
)
74 bt_clock_snapshot_destroy(clock_snapshot
);
78 void set_base_offset(struct bt_clock_class
*clock_class
)
80 clock_class
->base_offset
.overflows
= bt_util_get_base_offset_ns(
81 clock_class
->offset_seconds
, clock_class
->offset_cycles
,
82 clock_class
->frequency
, &clock_class
->base_offset
.value_ns
);
85 struct bt_clock_class
*bt_clock_class_create(bt_self_component
*self_comp
)
88 struct bt_clock_class
*clock_class
= NULL
;
90 BT_ASSERT_PRE_NO_ERROR();
91 BT_ASSERT_PRE_NON_NULL(self_comp
, "Self component");
92 BT_LOGD_STR("Creating default clock class object");
94 clock_class
= g_new0(struct bt_clock_class
, 1);
96 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one clock class.");
100 bt_object_init_shared(&clock_class
->base
, destroy_clock_class
);
102 clock_class
->user_attributes
= bt_value_map_create();
103 if (!clock_class
->user_attributes
) {
104 BT_LIB_LOGE_APPEND_CAUSE(
105 "Failed to create a map value object.");
109 clock_class
->name
.str
= g_string_new(NULL
);
110 if (!clock_class
->name
.str
) {
111 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
115 clock_class
->description
.str
= g_string_new(NULL
);
116 if (!clock_class
->description
.str
) {
117 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
121 clock_class
->frequency
= UINT64_C(1000000000);
122 clock_class
->origin_is_unix_epoch
= BT_TRUE
;
123 set_base_offset(clock_class
);
124 ret
= bt_object_pool_initialize(&clock_class
->cs_pool
,
125 (bt_object_pool_new_object_func
) bt_clock_snapshot_new
,
126 (bt_object_pool_destroy_object_func
)
130 BT_LIB_LOGE_APPEND_CAUSE(
131 "Failed to initialize clock snapshot pool: ret=%d",
136 BT_LIB_LOGD("Created clock class object: %!+K", clock_class
);
140 BT_OBJECT_PUT_REF_AND_RESET(clock_class
);
146 const char *bt_clock_class_get_name(const struct bt_clock_class
*clock_class
)
148 BT_ASSERT_PRE_DEV_NON_NULL(clock_class
, "Clock class");
149 return clock_class
->name
.value
;
152 enum bt_clock_class_set_name_status
bt_clock_class_set_name(
153 struct bt_clock_class
*clock_class
, const char *name
)
155 BT_ASSERT_PRE_NO_ERROR();
156 BT_ASSERT_PRE_NON_NULL(clock_class
, "Clock class");
157 BT_ASSERT_PRE_NON_NULL(name
, "Name");
158 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class
);
159 g_string_assign(clock_class
->name
.str
, name
);
160 clock_class
->name
.value
= clock_class
->name
.str
->str
;
161 BT_LIB_LOGD("Set clock class's name: %!+K", clock_class
);
162 return BT_FUNC_STATUS_OK
;
165 const char *bt_clock_class_get_description(
166 const struct bt_clock_class
*clock_class
)
168 BT_ASSERT_PRE_DEV_NON_NULL(clock_class
, "Clock class");
169 return clock_class
->description
.value
;
172 enum bt_clock_class_set_description_status
bt_clock_class_set_description(
173 struct bt_clock_class
*clock_class
, const char *descr
)
175 BT_ASSERT_PRE_NO_ERROR();
176 BT_ASSERT_PRE_NON_NULL(clock_class
, "Clock class");
177 BT_ASSERT_PRE_NON_NULL(descr
, "Description");
178 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class
);
179 g_string_assign(clock_class
->description
.str
, descr
);
180 clock_class
->description
.value
= clock_class
->description
.str
->str
;
181 BT_LIB_LOGD("Set clock class's description: %!+K",
183 return BT_FUNC_STATUS_OK
;
186 uint64_t bt_clock_class_get_frequency(const struct bt_clock_class
*clock_class
)
188 BT_ASSERT_PRE_DEV_NON_NULL(clock_class
, "Clock class");
189 return clock_class
->frequency
;
192 void bt_clock_class_set_frequency(struct bt_clock_class
*clock_class
,
195 BT_ASSERT_PRE_NON_NULL(clock_class
, "Clock class");
196 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class
);
197 BT_ASSERT_PRE(frequency
!= UINT64_C(-1) && frequency
!= 0,
198 "Invalid frequency: %![cc-]+K, new-freq=%" PRIu64
,
199 clock_class
, frequency
);
200 BT_ASSERT_PRE(clock_class
->offset_cycles
< frequency
,
201 "Offset (cycles) is greater than clock class's frequency: "
202 "%![cc-]+K, new-freq=%" PRIu64
, clock_class
, frequency
);
203 clock_class
->frequency
= frequency
;
204 set_base_offset(clock_class
);
205 BT_LIB_LOGD("Set clock class's frequency: %!+K", clock_class
);
208 uint64_t bt_clock_class_get_precision(const struct bt_clock_class
*clock_class
)
210 BT_ASSERT_PRE_DEV_NON_NULL(clock_class
, "Clock class");
211 return clock_class
->precision
;
214 void bt_clock_class_set_precision(struct bt_clock_class
*clock_class
,
217 BT_ASSERT_PRE_NON_NULL(clock_class
, "Clock class");
218 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class
);
219 BT_ASSERT_PRE(precision
!= UINT64_C(-1),
220 "Invalid precision: %![cc-]+K, new-precision=%" PRIu64
,
221 clock_class
, precision
);
222 clock_class
->precision
= precision
;
223 BT_LIB_LOGD("Set clock class's precision: %!+K", clock_class
);
226 void bt_clock_class_get_offset(const struct bt_clock_class
*clock_class
,
227 int64_t *seconds
, uint64_t *cycles
)
229 BT_ASSERT_PRE_DEV_NON_NULL(clock_class
, "Clock class");
230 BT_ASSERT_PRE_DEV_NON_NULL(seconds
, "Seconds (output)");
231 BT_ASSERT_PRE_DEV_NON_NULL(cycles
, "Cycles (output)");
232 *seconds
= clock_class
->offset_seconds
;
233 *cycles
= clock_class
->offset_cycles
;
236 void bt_clock_class_set_offset(struct bt_clock_class
*clock_class
,
237 int64_t seconds
, uint64_t cycles
)
239 BT_ASSERT_PRE_NON_NULL(clock_class
, "Clock class");
240 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class
);
241 BT_ASSERT_PRE(cycles
< clock_class
->frequency
,
242 "Offset (cycles) is greater than clock class's frequency: "
243 "%![cc-]+K, new-offset-cycles=%" PRIu64
, clock_class
, cycles
);
244 clock_class
->offset_seconds
= seconds
;
245 clock_class
->offset_cycles
= cycles
;
246 set_base_offset(clock_class
);
247 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_NON_NULL(clock_class
, "Clock class");
253 return (bool) clock_class
->origin_is_unix_epoch
;
256 void bt_clock_class_set_origin_is_unix_epoch(struct bt_clock_class
*clock_class
,
257 bt_bool origin_is_unix_epoch
)
259 BT_ASSERT_PRE_NON_NULL(clock_class
, "Clock class");
260 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class
);
261 clock_class
->origin_is_unix_epoch
= (bool) origin_is_unix_epoch
;
262 BT_LIB_LOGD("Set clock class's origin is Unix epoch property: %!+K",
266 bt_uuid
bt_clock_class_get_uuid(const struct bt_clock_class
*clock_class
)
268 BT_ASSERT_PRE_DEV_NON_NULL(clock_class
, "Clock class");
269 return clock_class
->uuid
.value
;
272 void bt_clock_class_set_uuid(struct bt_clock_class
*clock_class
,
275 BT_ASSERT_PRE_NON_NULL(clock_class
, "Clock class");
276 BT_ASSERT_PRE_NON_NULL(uuid
, "UUID");
277 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class
);
278 bt_uuid_copy(clock_class
->uuid
.uuid
, uuid
);
279 clock_class
->uuid
.value
= clock_class
->uuid
.uuid
;
280 BT_LIB_LOGD("Set clock class's UUID: %!+K", clock_class
);
284 void _bt_clock_class_freeze(const struct bt_clock_class
*clock_class
)
286 BT_ASSERT(clock_class
);
288 if (clock_class
->frozen
) {
292 BT_LIB_LOGD("Freezing clock class's user attributes: %!+v",
293 clock_class
->user_attributes
);
294 bt_value_freeze(clock_class
->user_attributes
);
295 BT_LIB_LOGD("Freezing clock class: %!+K", clock_class
);
296 ((struct bt_clock_class
*) clock_class
)->frozen
= 1;
299 enum bt_clock_class_cycles_to_ns_from_origin_status
300 bt_clock_class_cycles_to_ns_from_origin(
301 const struct bt_clock_class
*clock_class
,
302 uint64_t cycles
, int64_t *ns
)
306 BT_ASSERT_PRE_DEV_NO_ERROR();
307 BT_ASSERT_PRE_DEV_NON_NULL(clock_class
, "Clock class");
308 BT_ASSERT_PRE_DEV_NON_NULL(ns
, "Nanoseconds (output)");
309 ret
= bt_util_ns_from_origin_clock_class(clock_class
, cycles
, ns
);
311 BT_LIB_LOGE_APPEND_CAUSE("Cannot convert cycles to nanoseconds "
312 "from origin for given clock class: "
313 "value overflows the signed 64-bit integer range: "
314 "%![cc-]+K, cycles=%" PRIu64
,
315 clock_class
, cycles
);
316 ret
= BT_FUNC_STATUS_OVERFLOW_ERROR
;
322 const struct bt_value
*bt_clock_class_borrow_user_attributes_const(
323 const struct bt_clock_class
*clock_class
)
325 BT_ASSERT_PRE_DEV_NON_NULL(clock_class
, "Clock class");
326 return clock_class
->user_attributes
;
329 struct bt_value
*bt_clock_class_borrow_user_attributes(
330 struct bt_clock_class
*clock_class
)
332 return (void *) bt_clock_class_borrow_user_attributes_const(
333 (void *) clock_class
);
336 void bt_clock_class_set_user_attributes(
337 struct bt_clock_class
*clock_class
,
338 const struct bt_value
*user_attributes
)
340 BT_ASSERT_PRE_NON_NULL(clock_class
, "Clock class");
341 BT_ASSERT_PRE_NON_NULL(user_attributes
, "User attributes");
342 BT_ASSERT_PRE(user_attributes
->type
== BT_VALUE_TYPE_MAP
,
343 "User attributes object is not a map value object.");
344 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class
);
345 bt_object_put_ref_no_null_check(clock_class
->user_attributes
);
346 clock_class
->user_attributes
= (void *) user_attributes
;
347 bt_object_get_ref_no_null_check(clock_class
->user_attributes
);
350 void bt_clock_class_get_ref(const struct bt_clock_class
*clock_class
)
352 bt_object_get_ref(clock_class
);
355 void bt_clock_class_put_ref(const struct bt_clock_class
*clock_class
)
357 bt_object_put_ref(clock_class
);