Replace libuuid with internal implementation
[babeltrace.git] / src / lib / trace-ir / clock-class.c
CommitLineData
273b65be 1/*
f2b0325d 2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
de9dd397 3 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
273b65be
JG
4 *
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:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
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
21 * SOFTWARE.
22 */
23
b03487ab 24#define BT_LOG_TAG "LIB/CLOCK-CLASS"
1633ef46 25#include "lib/logging.h"
0f5e83e5 26
57952005 27#include "lib/assert-pre.h"
d126826c 28#include "common/uuid.h"
71c5da58
MJ
29#include <babeltrace2/trace-ir/clock-class-const.h>
30#include <babeltrace2/trace-ir/clock-class.h>
57952005
MJ
31#include "clock-class.h"
32#include "clock-snapshot.h"
33#include "utils.h"
34#include "compat/compiler.h"
71c5da58 35#include <babeltrace2/types.h>
57952005 36#include "compat/string.h"
273b65be 37#include <inttypes.h>
57952005
MJ
38#include "lib/object.h"
39#include "common/assert.h"
fb25b9e3 40#include "lib/func-status.h"
5134570b 41
7b33a0e0
PP
42#define BT_ASSERT_PRE_CLOCK_CLASS_HOT(_cc) \
43 BT_ASSERT_PRE_HOT((_cc), "Clock class", ": %!+K", (_cc))
a6918753
PP
44
45static
7b33a0e0 46void destroy_clock_class(struct bt_object *obj)
c06116f3 47{
7b33a0e0 48 struct bt_clock_class *clock_class = (void *) obj;
c06116f3 49
7b33a0e0 50 BT_LIB_LOGD("Destroying clock class: %!+K", clock_class);
273b65be 51
7b33a0e0
PP
52 if (clock_class->name.str) {
53 g_string_free(clock_class->name.str, TRUE);
1248f5ea
PP
54 clock_class->name.str = NULL;
55 clock_class->name.value = NULL;
5134570b
PP
56 }
57
7b33a0e0
PP
58 if (clock_class->description.str) {
59 g_string_free(clock_class->description.str, TRUE);
1248f5ea
PP
60 clock_class->description.str = NULL;
61 clock_class->description.value = NULL;
273b65be
JG
62 }
63
ecbb78c0 64 bt_object_pool_finalize(&clock_class->cs_pool);
7b33a0e0 65 g_free(clock_class);
4c426c17
JG
66}
67
15260cc8 68static
ecbb78c0 69void free_clock_snapshot(struct bt_clock_snapshot *clock_snapshot,
7b33a0e0 70 struct bt_clock_class *clock_class)
15260cc8 71{
ecbb78c0 72 bt_clock_snapshot_destroy(clock_snapshot);
15260cc8
PP
73}
74
7b33a0e0
PP
75static inline
76void set_base_offset(struct bt_clock_class *clock_class)
a6918753 77{
35fa110e
PP
78 clock_class->base_offset.overflows = bt_util_get_base_offset_ns(
79 clock_class->offset_seconds, clock_class->offset_cycles,
80 clock_class->frequency, &clock_class->base_offset.value_ns);
a6918753
PP
81}
82
c0c2ed34 83struct bt_clock_class *bt_clock_class_create(bt_self_component *self_comp)
4c426c17
JG
84{
85 int ret;
839d52a5 86 struct bt_clock_class *clock_class = NULL;
4c426c17 87
c0c2ed34 88 BT_ASSERT_PRE_NON_NULL(self_comp, "Self component");
7b33a0e0 89 BT_LOGD_STR("Creating default clock class object");
15260cc8 90
839d52a5 91 clock_class = g_new0(struct bt_clock_class, 1);
ac0c6bdd 92 if (!clock_class) {
a8f90e5d 93 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one clock class.");
4c426c17
JG
94 goto error;
95 }
96
7b33a0e0
PP
97 bt_object_init_shared(&clock_class->base, destroy_clock_class);
98 clock_class->name.str = g_string_new(NULL);
99 if (!clock_class->name.str) {
a8f90e5d 100 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
7b33a0e0
PP
101 goto error;
102 }
85380e99 103
7b33a0e0
PP
104 clock_class->description.str = g_string_new(NULL);
105 if (!clock_class->description.str) {
a8f90e5d 106 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
7b33a0e0 107 goto error;
273b65be
JG
108 }
109
7b33a0e0 110 clock_class->frequency = UINT64_C(1000000000);
d608d675 111 clock_class->origin_is_unix_epoch = BT_TRUE;
7b33a0e0 112 set_base_offset(clock_class);
ecbb78c0
PP
113 ret = bt_object_pool_initialize(&clock_class->cs_pool,
114 (bt_object_pool_new_object_func) bt_clock_snapshot_new,
a6918753 115 (bt_object_pool_destroy_object_func)
ecbb78c0 116 free_clock_snapshot,
a6918753
PP
117 clock_class);
118 if (ret) {
a8f90e5d
PP
119 BT_LIB_LOGE_APPEND_CAUSE(
120 "Failed to initialize clock snapshot pool: ret=%d",
a6918753
PP
121 ret);
122 goto error;
123 }
124
7b33a0e0
PP
125 BT_LIB_LOGD("Created clock class object: %!+K", clock_class);
126 goto end;
127
273b65be 128error:
8138bfe1 129 BT_OBJECT_PUT_REF_AND_RESET(clock_class);
87d76bb1
JG
130
131end:
78cf9df6 132 return clock_class;
87d76bb1
JG
133}
134
78cf9df6 135const char *bt_clock_class_get_name(const struct bt_clock_class *clock_class)
87d76bb1 136{
7b33a0e0
PP
137 BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class");
138 return clock_class->name.value;
139}
87d76bb1 140
fb25b9e3 141enum bt_clock_class_set_name_status bt_clock_class_set_name(
d10c1e1a 142 struct bt_clock_class *clock_class, const char *name)
7b33a0e0
PP
143{
144 BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class");
145 BT_ASSERT_PRE_NON_NULL(name, "Name");
146 BT_ASSERT_PRE_CLOCK_CLASS_HOT(clock_class);
147 g_string_assign(clock_class->name.str, name);
148 clock_class->name.value = clock_class->name.str->str;
a684a357 149 BT_LIB_LOGD("Set clock class's name: %!+K", clock_class);
fb25b9e3 150 return BT_FUNC_STATUS_OK;
7b33a0e0 151}
87d76bb1 152
78cf9df6
PP
153const char *bt_clock_class_get_description(
154 const struct bt_clock_class *clock_class)
7b33a0e0
PP
155{
156 BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class");
157 return clock_class->description.value;
273b65be
JG
158}
159
fb25b9e3 160enum bt_clock_class_set_description_status bt_clock_class_set_description(
d10c1e1a 161 struct bt_clock_class *clock_class, const char *descr)
273b65be 162{
7b33a0e0
PP
163 BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class");
164 BT_ASSERT_PRE_NON_NULL(descr, "Description");
165 BT_ASSERT_PRE_CLOCK_CLASS_HOT(clock_class);
166 g_string_assign(clock_class->description.str, descr);
167 clock_class->description.value = clock_class->description.str->str;
a684a357 168 BT_LIB_LOGD("Set clock class's description: %!+K",
7b33a0e0 169 clock_class);
fb25b9e3 170 return BT_FUNC_STATUS_OK;
273b65be
JG
171}
172
78cf9df6 173uint64_t bt_clock_class_get_frequency(const struct bt_clock_class *clock_class)
87d76bb1 174{
7b33a0e0
PP
175 BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class");
176 return clock_class->frequency;
87d76bb1
JG
177}
178
78cf9df6 179void bt_clock_class_set_frequency(struct bt_clock_class *clock_class,
7b33a0e0 180 uint64_t frequency)
273b65be 181{
7b33a0e0
PP
182 BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class");
183 BT_ASSERT_PRE_CLOCK_CLASS_HOT(clock_class);
184 BT_ASSERT_PRE(frequency != UINT64_C(-1) && frequency != 0,
185 "Invalid frequency: %![cc-]+K, new-freq=%" PRIu64,
186 clock_class, frequency);
187 BT_ASSERT_PRE(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);
a684a357 192 BT_LIB_LOGD("Set clock class's frequency: %!+K", clock_class);
273b65be
JG
193}
194
78cf9df6 195uint64_t bt_clock_class_get_precision(const struct bt_clock_class *clock_class)
87d76bb1 196{
7b33a0e0
PP
197 BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class");
198 return clock_class->precision;
87d76bb1
JG
199}
200
78cf9df6 201void bt_clock_class_set_precision(struct bt_clock_class *clock_class,
ac0c6bdd 202 uint64_t precision)
273b65be 203{
7b33a0e0
PP
204 BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class");
205 BT_ASSERT_PRE_CLOCK_CLASS_HOT(clock_class);
206 BT_ASSERT_PRE(precision != UINT64_C(-1),
207 "Invalid precision: %![cc-]+K, new-precision=%" PRIu64,
208 clock_class, precision);
ac0c6bdd 209 clock_class->precision = precision;
a684a357 210 BT_LIB_LOGD("Set clock class's precision: %!+K", clock_class);
273b65be
JG
211}
212
78cf9df6 213void bt_clock_class_get_offset(const struct bt_clock_class *clock_class,
7b33a0e0 214 int64_t *seconds, uint64_t *cycles)
87d76bb1 215{
7b33a0e0
PP
216 BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class");
217 BT_ASSERT_PRE_NON_NULL(seconds, "Seconds (output)");
218 BT_ASSERT_PRE_NON_NULL(cycles, "Cycles (output)");
219 *seconds = clock_class->offset_seconds;
220 *cycles = clock_class->offset_cycles;
87d76bb1
JG
221}
222
78cf9df6 223void bt_clock_class_set_offset(struct bt_clock_class *clock_class,
7b33a0e0 224 int64_t seconds, uint64_t cycles)
273b65be 225{
7b33a0e0
PP
226 BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class");
227 BT_ASSERT_PRE_CLOCK_CLASS_HOT(clock_class);
228 BT_ASSERT_PRE(cycles < clock_class->frequency,
229 "Offset (cycles) is greater than clock class's frequency: "
230 "%![cc-]+K, new-offset-cycles=%" PRIu64, clock_class, cycles);
231 clock_class->offset_seconds = seconds;
232 clock_class->offset_cycles = cycles;
233 set_base_offset(clock_class);
a684a357 234 BT_LIB_LOGD("Set clock class's offset: %!+K", clock_class);
273b65be
JG
235}
236
d608d675 237bt_bool bt_clock_class_origin_is_unix_epoch(const struct bt_clock_class *clock_class)
87d76bb1 238{
7b33a0e0 239 BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class");
d608d675 240 return (bool) clock_class->origin_is_unix_epoch;
87d76bb1
JG
241}
242
d608d675
PP
243void bt_clock_class_set_origin_is_unix_epoch(struct bt_clock_class *clock_class,
244 bt_bool origin_is_unix_epoch)
273b65be 245{
7b33a0e0
PP
246 BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class");
247 BT_ASSERT_PRE_CLOCK_CLASS_HOT(clock_class);
d608d675 248 clock_class->origin_is_unix_epoch = (bool) origin_is_unix_epoch;
a684a357 249 BT_LIB_LOGD("Set clock class's origin is Unix epoch property: %!+K",
7b33a0e0 250 clock_class);
273b65be
JG
251}
252
78cf9df6 253bt_uuid bt_clock_class_get_uuid(const struct bt_clock_class *clock_class)
85b743f4 254{
7b33a0e0
PP
255 BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class");
256 return clock_class->uuid.value;
85b743f4
JG
257}
258
78cf9df6 259void bt_clock_class_set_uuid(struct bt_clock_class *clock_class,
7b33a0e0 260 bt_uuid uuid)
85b743f4 261{
7b33a0e0
PP
262 BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class");
263 BT_ASSERT_PRE_NON_NULL(uuid, "UUID");
264 BT_ASSERT_PRE_CLOCK_CLASS_HOT(clock_class);
d126826c 265 bt_uuid_copy(clock_class->uuid.uuid, uuid);
7b33a0e0 266 clock_class->uuid.value = clock_class->uuid.uuid;
a684a357 267 BT_LIB_LOGD("Set clock class's UUID: %!+K", clock_class);
a6918753
PP
268}
269
270BT_HIDDEN
78cf9df6 271void _bt_clock_class_freeze(const struct bt_clock_class *clock_class)
a6918753 272{
a6918753 273 BT_ASSERT(clock_class);
6f57e458 274
7b33a0e0
PP
275 if (clock_class->frozen) {
276 return;
db3347ac
PP
277 }
278
7b33a0e0 279 BT_LIB_LOGD("Freezing clock class: %!+K", clock_class);
78cf9df6 280 ((struct bt_clock_class *) clock_class)->frozen = 1;
db3347ac 281}
a6918753 282
fb25b9e3
PP
283enum bt_clock_class_cycles_to_ns_from_origin_status
284bt_clock_class_cycles_to_ns_from_origin(
78cf9df6 285 const struct bt_clock_class *clock_class,
a6918753
PP
286 uint64_t cycles, int64_t *ns)
287{
288 int ret;
a6918753
PP
289
290 BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class");
7b33a0e0 291 BT_ASSERT_PRE_NON_NULL(ns, "Nanoseconds (output)");
35fa110e 292 ret = bt_util_ns_from_origin_clock_class(clock_class, cycles, ns);
a6918753 293 if (ret) {
fb25b9e3 294 ret = BT_FUNC_STATUS_OVERFLOW;
a684a357 295 BT_LIB_LOGD("Cannot convert cycles to nanoseconds "
7b33a0e0
PP
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);
a6918753
PP
300 }
301
a6918753
PP
302 return ret;
303}
8c6884d9
PP
304
305void bt_clock_class_get_ref(const struct bt_clock_class *clock_class)
306{
307 bt_object_get_ref(clock_class);
308}
309
310void bt_clock_class_put_ref(const struct bt_clock_class *clock_class)
311{
312 bt_object_put_ref(clock_class);
313}
This page took 0.083367 seconds and 4 git commands to generate.