2 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 #define BT_LOG_TAG "CLOCK-CLASS"
26 #include <babeltrace/lib-logging-internal.h>
28 #include <babeltrace/assert-pre-internal.h>
29 #include <babeltrace/compat/uuid-internal.h>
30 #include <babeltrace/trace-ir/clock-class-internal.h>
31 #include <babeltrace/trace-ir/clock-value-internal.h>
32 #include <babeltrace/trace-ir/utils-internal.h>
33 #include <babeltrace/object.h>
34 #include <babeltrace/compiler-internal.h>
35 #include <babeltrace/types.h>
36 #include <babeltrace/compat/string-internal.h>
38 #include <babeltrace/object-internal.h>
39 #include <babeltrace/assert-internal.h>
41 #define BT_ASSERT_PRE_CLOCK_CLASS_HOT(_cc) \
42 BT_ASSERT_PRE_HOT((_cc), "Clock class", ": %!+K", (_cc))
45 void destroy_clock_class(struct bt_object
*obj
)
47 struct bt_clock_class
*clock_class
= (void *) obj
;
49 BT_LIB_LOGD("Destroying clock class: %!+K", clock_class
);
51 if (clock_class
->name
.str
) {
52 g_string_free(clock_class
->name
.str
, TRUE
);
55 if (clock_class
->description
.str
) {
56 g_string_free(clock_class
->description
.str
, TRUE
);
59 bt_object_pool_finalize(&clock_class
->cv_pool
);
64 void free_clock_value(struct bt_clock_value
*clock_value
,
65 struct bt_clock_class
*clock_class
)
67 bt_clock_value_destroy(clock_value
);
71 void set_base_offset(struct bt_clock_class
*clock_class
)
73 uint64_t offset_cycles_ns
;
75 /* Initialize nanosecond timestamp to clock's offset in seconds */
76 if (clock_class
->offset_seconds
<= (INT64_MIN
/ INT64_C(1000000000) - 1) ||
77 clock_class
->offset_seconds
>= (INT64_MAX
/ INT64_C(1000000000)) - 1) {
79 * Overflow: offset in seconds converted to nanoseconds
80 * is outside the int64_t range. We also subtract 1 here
81 * to leave "space" for the offset in cycles converted
82 * to nanoseconds (which is always less than 1 second by
85 clock_class
->base_offset
.overflows
= true;
89 /* Offset (seconds) to nanoseconds */
90 clock_class
->base_offset
.value_ns
= clock_class
->offset_seconds
*
93 /* Add offset in cycles */
94 BT_ASSERT(clock_class
->offset_cycles
< clock_class
->frequency
);
95 offset_cycles_ns
= bt_util_ns_from_value(clock_class
->frequency
,
96 clock_class
->offset_cycles
);
97 BT_ASSERT(offset_cycles_ns
< 1000000000);
98 clock_class
->base_offset
.value_ns
+= (int64_t) offset_cycles_ns
;
99 clock_class
->base_offset
.overflows
= false;
105 struct bt_private_clock_class
*bt_private_clock_class_create(void)
108 struct bt_clock_class
*clock_class
= NULL
;
110 BT_LOGD_STR("Creating default clock class object");
112 clock_class
= g_new0(struct bt_clock_class
, 1);
114 BT_LOGE_STR("Failed to allocate one clock class.");
118 bt_object_init_shared(&clock_class
->base
, destroy_clock_class
);
119 clock_class
->name
.str
= g_string_new(NULL
);
120 if (!clock_class
->name
.str
) {
121 BT_LOGE_STR("Failed to allocate a GString.");
125 clock_class
->description
.str
= g_string_new(NULL
);
126 if (!clock_class
->description
.str
) {
127 BT_LOGE_STR("Failed to allocate a GString.");
131 clock_class
->frequency
= UINT64_C(1000000000);
132 clock_class
->is_absolute
= BT_TRUE
;
133 set_base_offset(clock_class
);
134 ret
= bt_object_pool_initialize(&clock_class
->cv_pool
,
135 (bt_object_pool_new_object_func
) bt_clock_value_new
,
136 (bt_object_pool_destroy_object_func
)
140 BT_LOGE("Failed to initialize clock value pool: ret=%d",
145 BT_LIB_LOGD("Created clock class object: %!+K", clock_class
);
149 BT_OBJECT_PUT_REF_AND_RESET(clock_class
);
152 return (void *) clock_class
;
155 const char *bt_clock_class_get_name(
156 struct bt_clock_class
*clock_class
)
158 BT_ASSERT_PRE_NON_NULL(clock_class
, "Clock class");
159 return clock_class
->name
.value
;
162 int bt_private_clock_class_set_name(
163 struct bt_private_clock_class
*priv_clock_class
,
166 struct bt_clock_class
*clock_class
= (void *) priv_clock_class
;
168 BT_ASSERT_PRE_NON_NULL(clock_class
, "Clock class");
169 BT_ASSERT_PRE_NON_NULL(name
, "Name");
170 BT_ASSERT_PRE_CLOCK_CLASS_HOT(clock_class
);
171 g_string_assign(clock_class
->name
.str
, name
);
172 clock_class
->name
.value
= clock_class
->name
.str
->str
;
173 BT_LIB_LOGV("Set clock class's name: %!+K", clock_class
);
177 const char *bt_clock_class_get_description(struct bt_clock_class
*clock_class
)
179 BT_ASSERT_PRE_NON_NULL(clock_class
, "Clock class");
180 return clock_class
->description
.value
;
183 int bt_private_clock_class_set_description(
184 struct bt_private_clock_class
*priv_clock_class
,
187 struct bt_clock_class
*clock_class
= (void *) priv_clock_class
;
189 BT_ASSERT_PRE_NON_NULL(clock_class
, "Clock class");
190 BT_ASSERT_PRE_NON_NULL(descr
, "Description");
191 BT_ASSERT_PRE_CLOCK_CLASS_HOT(clock_class
);
192 g_string_assign(clock_class
->description
.str
, descr
);
193 clock_class
->description
.value
= clock_class
->description
.str
->str
;
194 BT_LIB_LOGV("Set clock class's description: %!+K",
199 uint64_t bt_clock_class_get_frequency(struct bt_clock_class
*clock_class
)
201 BT_ASSERT_PRE_NON_NULL(clock_class
, "Clock class");
202 return clock_class
->frequency
;
205 int bt_private_clock_class_set_frequency(
206 struct bt_private_clock_class
*priv_clock_class
,
209 struct bt_clock_class
*clock_class
= (void *) priv_clock_class
;
211 BT_ASSERT_PRE_NON_NULL(clock_class
, "Clock class");
212 BT_ASSERT_PRE_CLOCK_CLASS_HOT(clock_class
);
213 BT_ASSERT_PRE(frequency
!= UINT64_C(-1) && frequency
!= 0,
214 "Invalid frequency: %![cc-]+K, new-freq=%" PRIu64
,
215 clock_class
, frequency
);
216 BT_ASSERT_PRE(clock_class
->offset_cycles
< frequency
,
217 "Offset (cycles) is greater than clock class's frequency: "
218 "%![cc-]+K, new-freq=%" PRIu64
, clock_class
, frequency
);
219 clock_class
->frequency
= frequency
;
220 set_base_offset(clock_class
);
221 BT_LIB_LOGV("Set clock class's frequency: %!+K", clock_class
);
225 uint64_t bt_clock_class_get_precision(struct bt_clock_class
*clock_class
)
227 BT_ASSERT_PRE_NON_NULL(clock_class
, "Clock class");
228 return clock_class
->precision
;
231 int bt_private_clock_class_set_precision(
232 struct bt_private_clock_class
*priv_clock_class
,
235 struct bt_clock_class
*clock_class
= (void *) priv_clock_class
;
237 BT_ASSERT_PRE_NON_NULL(clock_class
, "Clock class");
238 BT_ASSERT_PRE_CLOCK_CLASS_HOT(clock_class
);
239 BT_ASSERT_PRE(precision
!= UINT64_C(-1),
240 "Invalid precision: %![cc-]+K, new-precision=%" PRIu64
,
241 clock_class
, precision
);
242 clock_class
->precision
= precision
;
243 BT_LIB_LOGV("Set clock class's precision: %!+K", clock_class
);
247 void bt_clock_class_get_offset(struct bt_clock_class
*clock_class
,
248 int64_t *seconds
, uint64_t *cycles
)
250 BT_ASSERT_PRE_NON_NULL(clock_class
, "Clock class");
251 BT_ASSERT_PRE_NON_NULL(seconds
, "Seconds (output)");
252 BT_ASSERT_PRE_NON_NULL(cycles
, "Cycles (output)");
253 *seconds
= clock_class
->offset_seconds
;
254 *cycles
= clock_class
->offset_cycles
;
257 int bt_private_clock_class_set_offset(
258 struct bt_private_clock_class
*priv_clock_class
,
259 int64_t seconds
, uint64_t cycles
)
261 struct bt_clock_class
*clock_class
= (void *) priv_clock_class
;
263 BT_ASSERT_PRE_NON_NULL(clock_class
, "Clock class");
264 BT_ASSERT_PRE_CLOCK_CLASS_HOT(clock_class
);
265 BT_ASSERT_PRE(cycles
< clock_class
->frequency
,
266 "Offset (cycles) is greater than clock class's frequency: "
267 "%![cc-]+K, new-offset-cycles=%" PRIu64
, clock_class
, cycles
);
268 clock_class
->offset_seconds
= seconds
;
269 clock_class
->offset_cycles
= cycles
;
270 set_base_offset(clock_class
);
271 BT_LIB_LOGV("Set clock class's offset: %!+K", clock_class
);
275 bt_bool
bt_clock_class_is_absolute(struct bt_clock_class
*clock_class
)
277 BT_ASSERT_PRE_NON_NULL(clock_class
, "Clock class");
278 return (bool) clock_class
->is_absolute
;
281 int bt_private_clock_class_set_is_absolute(
282 struct bt_private_clock_class
*priv_clock_class
,
285 struct bt_clock_class
*clock_class
= (void *) priv_clock_class
;
287 BT_ASSERT_PRE_NON_NULL(clock_class
, "Clock class");
288 BT_ASSERT_PRE_CLOCK_CLASS_HOT(clock_class
);
289 clock_class
->is_absolute
= (bool) is_absolute
;
290 BT_LIB_LOGV("Set clock class's absolute property: %!+K",
295 bt_uuid
bt_clock_class_get_uuid(struct bt_clock_class
*clock_class
)
297 BT_ASSERT_PRE_NON_NULL(clock_class
, "Clock class");
298 return clock_class
->uuid
.value
;
301 int bt_private_clock_class_set_uuid(
302 struct bt_private_clock_class
*priv_clock_class
,
305 struct bt_clock_class
*clock_class
= (void *) priv_clock_class
;
307 BT_ASSERT_PRE_NON_NULL(clock_class
, "Clock class");
308 BT_ASSERT_PRE_NON_NULL(uuid
, "UUID");
309 BT_ASSERT_PRE_CLOCK_CLASS_HOT(clock_class
);
310 memcpy(clock_class
->uuid
.uuid
, uuid
, BABELTRACE_UUID_LEN
);
311 clock_class
->uuid
.value
= clock_class
->uuid
.uuid
;
312 BT_LIB_LOGV("Set clock class's UUID: %!+K", clock_class
);
317 void _bt_clock_class_freeze(struct bt_clock_class
*clock_class
)
319 BT_ASSERT(clock_class
);
321 if (clock_class
->frozen
) {
325 BT_LIB_LOGD("Freezing clock class: %!+K", clock_class
);
326 clock_class
->frozen
= 1;
329 int bt_clock_class_cycles_to_ns_from_origin(struct bt_clock_class
*clock_class
,
330 uint64_t cycles
, int64_t *ns
)
334 BT_ASSERT_PRE_NON_NULL(clock_class
, "Clock class");
335 BT_ASSERT_PRE_NON_NULL(ns
, "Nanoseconds (output)");
336 ret
= bt_util_ns_from_origin(clock_class
, cycles
, ns
);
338 BT_LIB_LOGW("Cannot convert cycles to nanoseconds "
339 "from origin for given clock class: "
340 "value overflows the signed 64-bit integer range: "
341 "%![cc-]+K, cycles=%" PRIu64
,
342 clock_class
, cycles
);