Cleanup: add `#include <stdbool.h>` whenever `bool` type is used
[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>
969c1d8a 38#include <stdbool.h>
57952005
MJ
39#include "lib/object.h"
40#include "common/assert.h"
fb25b9e3 41#include "lib/func-status.h"
af90138b 42#include "lib/value.h"
5134570b 43
fa6cfec3
PP
44#define BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(_cc) \
45 BT_ASSERT_PRE_DEV_HOT((_cc), "Clock class", ": %!+K", (_cc))
a6918753
PP
46
47static
7b33a0e0 48void destroy_clock_class(struct bt_object *obj)
c06116f3 49{
7b33a0e0 50 struct bt_clock_class *clock_class = (void *) obj;
c06116f3 51
7b33a0e0 52 BT_LIB_LOGD("Destroying clock class: %!+K", clock_class);
af90138b 53 BT_OBJECT_PUT_REF_AND_RESET(clock_class->user_attributes);
273b65be 54
7b33a0e0
PP
55 if (clock_class->name.str) {
56 g_string_free(clock_class->name.str, TRUE);
1248f5ea
PP
57 clock_class->name.str = NULL;
58 clock_class->name.value = NULL;
5134570b
PP
59 }
60
7b33a0e0
PP
61 if (clock_class->description.str) {
62 g_string_free(clock_class->description.str, TRUE);
1248f5ea
PP
63 clock_class->description.str = NULL;
64 clock_class->description.value = NULL;
273b65be
JG
65 }
66
ecbb78c0 67 bt_object_pool_finalize(&clock_class->cs_pool);
7b33a0e0 68 g_free(clock_class);
4c426c17
JG
69}
70
15260cc8 71static
ecbb78c0 72void free_clock_snapshot(struct bt_clock_snapshot *clock_snapshot,
7b33a0e0 73 struct bt_clock_class *clock_class)
15260cc8 74{
ecbb78c0 75 bt_clock_snapshot_destroy(clock_snapshot);
15260cc8
PP
76}
77
7b33a0e0
PP
78static inline
79void set_base_offset(struct bt_clock_class *clock_class)
a6918753 80{
35fa110e
PP
81 clock_class->base_offset.overflows = bt_util_get_base_offset_ns(
82 clock_class->offset_seconds, clock_class->offset_cycles,
83 clock_class->frequency, &clock_class->base_offset.value_ns);
a6918753
PP
84}
85
c0c2ed34 86struct bt_clock_class *bt_clock_class_create(bt_self_component *self_comp)
4c426c17
JG
87{
88 int ret;
839d52a5 89 struct bt_clock_class *clock_class = NULL;
4c426c17 90
c0c2ed34 91 BT_ASSERT_PRE_NON_NULL(self_comp, "Self component");
7b33a0e0 92 BT_LOGD_STR("Creating default clock class object");
15260cc8 93
839d52a5 94 clock_class = g_new0(struct bt_clock_class, 1);
ac0c6bdd 95 if (!clock_class) {
a8f90e5d 96 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one clock class.");
4c426c17
JG
97 goto error;
98 }
99
7b33a0e0 100 bt_object_init_shared(&clock_class->base, destroy_clock_class);
af90138b
PP
101
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.");
106 goto error;
107 }
108
7b33a0e0
PP
109 clock_class->name.str = g_string_new(NULL);
110 if (!clock_class->name.str) {
a8f90e5d 111 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
7b33a0e0
PP
112 goto error;
113 }
85380e99 114
7b33a0e0
PP
115 clock_class->description.str = g_string_new(NULL);
116 if (!clock_class->description.str) {
a8f90e5d 117 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
7b33a0e0 118 goto error;
273b65be
JG
119 }
120
7b33a0e0 121 clock_class->frequency = UINT64_C(1000000000);
d608d675 122 clock_class->origin_is_unix_epoch = BT_TRUE;
7b33a0e0 123 set_base_offset(clock_class);
ecbb78c0
PP
124 ret = bt_object_pool_initialize(&clock_class->cs_pool,
125 (bt_object_pool_new_object_func) bt_clock_snapshot_new,
a6918753 126 (bt_object_pool_destroy_object_func)
ecbb78c0 127 free_clock_snapshot,
a6918753
PP
128 clock_class);
129 if (ret) {
a8f90e5d
PP
130 BT_LIB_LOGE_APPEND_CAUSE(
131 "Failed to initialize clock snapshot pool: ret=%d",
a6918753
PP
132 ret);
133 goto error;
134 }
135
7b33a0e0
PP
136 BT_LIB_LOGD("Created clock class object: %!+K", clock_class);
137 goto end;
138
273b65be 139error:
8138bfe1 140 BT_OBJECT_PUT_REF_AND_RESET(clock_class);
87d76bb1
JG
141
142end:
78cf9df6 143 return clock_class;
87d76bb1
JG
144}
145
78cf9df6 146const char *bt_clock_class_get_name(const struct bt_clock_class *clock_class)
87d76bb1 147{
fa6cfec3 148 BT_ASSERT_PRE_DEV_NON_NULL(clock_class, "Clock class");
7b33a0e0
PP
149 return clock_class->name.value;
150}
87d76bb1 151
fb25b9e3 152enum bt_clock_class_set_name_status bt_clock_class_set_name(
d10c1e1a 153 struct bt_clock_class *clock_class, const char *name)
7b33a0e0
PP
154{
155 BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class");
156 BT_ASSERT_PRE_NON_NULL(name, "Name");
fa6cfec3 157 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class);
7b33a0e0
PP
158 g_string_assign(clock_class->name.str, name);
159 clock_class->name.value = clock_class->name.str->str;
a684a357 160 BT_LIB_LOGD("Set clock class's name: %!+K", clock_class);
fb25b9e3 161 return BT_FUNC_STATUS_OK;
7b33a0e0 162}
87d76bb1 163
78cf9df6
PP
164const char *bt_clock_class_get_description(
165 const struct bt_clock_class *clock_class)
7b33a0e0 166{
fa6cfec3 167 BT_ASSERT_PRE_DEV_NON_NULL(clock_class, "Clock class");
7b33a0e0 168 return clock_class->description.value;
273b65be
JG
169}
170
fb25b9e3 171enum bt_clock_class_set_description_status bt_clock_class_set_description(
d10c1e1a 172 struct bt_clock_class *clock_class, const char *descr)
273b65be 173{
7b33a0e0
PP
174 BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class");
175 BT_ASSERT_PRE_NON_NULL(descr, "Description");
fa6cfec3 176 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class);
7b33a0e0
PP
177 g_string_assign(clock_class->description.str, descr);
178 clock_class->description.value = clock_class->description.str->str;
a684a357 179 BT_LIB_LOGD("Set clock class's description: %!+K",
7b33a0e0 180 clock_class);
fb25b9e3 181 return BT_FUNC_STATUS_OK;
273b65be
JG
182}
183
78cf9df6 184uint64_t bt_clock_class_get_frequency(const struct bt_clock_class *clock_class)
87d76bb1 185{
fa6cfec3 186 BT_ASSERT_PRE_DEV_NON_NULL(clock_class, "Clock class");
7b33a0e0 187 return clock_class->frequency;
87d76bb1
JG
188}
189
78cf9df6 190void bt_clock_class_set_frequency(struct bt_clock_class *clock_class,
7b33a0e0 191 uint64_t frequency)
273b65be 192{
7b33a0e0 193 BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class");
fa6cfec3 194 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class);
7b33a0e0
PP
195 BT_ASSERT_PRE(frequency != UINT64_C(-1) && frequency != 0,
196 "Invalid frequency: %![cc-]+K, new-freq=%" PRIu64,
197 clock_class, frequency);
198 BT_ASSERT_PRE(clock_class->offset_cycles < frequency,
199 "Offset (cycles) is greater than clock class's frequency: "
200 "%![cc-]+K, new-freq=%" PRIu64, clock_class, frequency);
201 clock_class->frequency = frequency;
202 set_base_offset(clock_class);
a684a357 203 BT_LIB_LOGD("Set clock class's frequency: %!+K", clock_class);
273b65be
JG
204}
205
78cf9df6 206uint64_t bt_clock_class_get_precision(const struct bt_clock_class *clock_class)
87d76bb1 207{
fa6cfec3 208 BT_ASSERT_PRE_DEV_NON_NULL(clock_class, "Clock class");
7b33a0e0 209 return clock_class->precision;
87d76bb1
JG
210}
211
78cf9df6 212void bt_clock_class_set_precision(struct bt_clock_class *clock_class,
ac0c6bdd 213 uint64_t precision)
273b65be 214{
7b33a0e0 215 BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class");
fa6cfec3 216 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class);
7b33a0e0
PP
217 BT_ASSERT_PRE(precision != UINT64_C(-1),
218 "Invalid precision: %![cc-]+K, new-precision=%" PRIu64,
219 clock_class, precision);
ac0c6bdd 220 clock_class->precision = precision;
a684a357 221 BT_LIB_LOGD("Set clock class's precision: %!+K", clock_class);
273b65be
JG
222}
223
78cf9df6 224void bt_clock_class_get_offset(const struct bt_clock_class *clock_class,
7b33a0e0 225 int64_t *seconds, uint64_t *cycles)
87d76bb1 226{
fa6cfec3
PP
227 BT_ASSERT_PRE_DEV_NON_NULL(clock_class, "Clock class");
228 BT_ASSERT_PRE_DEV_NON_NULL(seconds, "Seconds (output)");
229 BT_ASSERT_PRE_DEV_NON_NULL(cycles, "Cycles (output)");
7b33a0e0
PP
230 *seconds = clock_class->offset_seconds;
231 *cycles = clock_class->offset_cycles;
87d76bb1
JG
232}
233
78cf9df6 234void bt_clock_class_set_offset(struct bt_clock_class *clock_class,
7b33a0e0 235 int64_t seconds, uint64_t cycles)
273b65be 236{
7b33a0e0 237 BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class");
fa6cfec3 238 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class);
7b33a0e0
PP
239 BT_ASSERT_PRE(cycles < clock_class->frequency,
240 "Offset (cycles) is greater than clock class's frequency: "
241 "%![cc-]+K, new-offset-cycles=%" PRIu64, clock_class, cycles);
242 clock_class->offset_seconds = seconds;
243 clock_class->offset_cycles = cycles;
244 set_base_offset(clock_class);
a684a357 245 BT_LIB_LOGD("Set clock class's offset: %!+K", clock_class);
273b65be
JG
246}
247
d608d675 248bt_bool bt_clock_class_origin_is_unix_epoch(const struct bt_clock_class *clock_class)
87d76bb1 249{
fa6cfec3 250 BT_ASSERT_PRE_DEV_NON_NULL(clock_class, "Clock class");
d608d675 251 return (bool) clock_class->origin_is_unix_epoch;
87d76bb1
JG
252}
253
d608d675
PP
254void bt_clock_class_set_origin_is_unix_epoch(struct bt_clock_class *clock_class,
255 bt_bool origin_is_unix_epoch)
273b65be 256{
7b33a0e0 257 BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class");
fa6cfec3 258 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class);
d608d675 259 clock_class->origin_is_unix_epoch = (bool) origin_is_unix_epoch;
a684a357 260 BT_LIB_LOGD("Set clock class's origin is Unix epoch property: %!+K",
7b33a0e0 261 clock_class);
273b65be
JG
262}
263
78cf9df6 264bt_uuid bt_clock_class_get_uuid(const struct bt_clock_class *clock_class)
85b743f4 265{
fa6cfec3 266 BT_ASSERT_PRE_DEV_NON_NULL(clock_class, "Clock class");
7b33a0e0 267 return clock_class->uuid.value;
85b743f4
JG
268}
269
78cf9df6 270void bt_clock_class_set_uuid(struct bt_clock_class *clock_class,
7b33a0e0 271 bt_uuid uuid)
85b743f4 272{
7b33a0e0
PP
273 BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class");
274 BT_ASSERT_PRE_NON_NULL(uuid, "UUID");
fa6cfec3 275 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class);
d126826c 276 bt_uuid_copy(clock_class->uuid.uuid, uuid);
7b33a0e0 277 clock_class->uuid.value = clock_class->uuid.uuid;
a684a357 278 BT_LIB_LOGD("Set clock class's UUID: %!+K", clock_class);
a6918753
PP
279}
280
281BT_HIDDEN
78cf9df6 282void _bt_clock_class_freeze(const struct bt_clock_class *clock_class)
a6918753 283{
a6918753 284 BT_ASSERT(clock_class);
6f57e458 285
7b33a0e0
PP
286 if (clock_class->frozen) {
287 return;
db3347ac
PP
288 }
289
af90138b
PP
290 BT_LIB_LOGD("Freezing clock class's user attributes: %!+v",
291 clock_class->user_attributes);
292 bt_value_freeze(clock_class->user_attributes);
7b33a0e0 293 BT_LIB_LOGD("Freezing clock class: %!+K", clock_class);
78cf9df6 294 ((struct bt_clock_class *) clock_class)->frozen = 1;
db3347ac 295}
a6918753 296
fb25b9e3
PP
297enum bt_clock_class_cycles_to_ns_from_origin_status
298bt_clock_class_cycles_to_ns_from_origin(
78cf9df6 299 const struct bt_clock_class *clock_class,
a6918753
PP
300 uint64_t cycles, int64_t *ns)
301{
302 int ret;
a6918753 303
fa6cfec3
PP
304 BT_ASSERT_PRE_DEV_NON_NULL(clock_class, "Clock class");
305 BT_ASSERT_PRE_DEV_NON_NULL(ns, "Nanoseconds (output)");
35fa110e 306 ret = bt_util_ns_from_origin_clock_class(clock_class, cycles, ns);
a6918753 307 if (ret) {
790240eb 308 BT_LIB_LOGE_APPEND_CAUSE("Cannot convert cycles to nanoseconds "
7b33a0e0
PP
309 "from origin for given clock class: "
310 "value overflows the signed 64-bit integer range: "
311 "%![cc-]+K, cycles=%" PRIu64,
312 clock_class, cycles);
f67e22d1 313 ret = BT_FUNC_STATUS_OVERFLOW_ERROR;
a6918753
PP
314 }
315
a6918753
PP
316 return ret;
317}
8c6884d9 318
af90138b
PP
319const struct bt_value *bt_clock_class_borrow_user_attributes_const(
320 const struct bt_clock_class *clock_class)
321{
322 BT_ASSERT_PRE_DEV_NON_NULL(clock_class, "Clock class");
323 return clock_class->user_attributes;
324}
325
326struct bt_value *bt_clock_class_borrow_user_attributes(
327 struct bt_clock_class *clock_class)
328{
329 return (void *) bt_clock_class_borrow_user_attributes_const(
330 (void *) clock_class);
331}
332
333void bt_clock_class_set_user_attributes(
334 struct bt_clock_class *clock_class,
335 const struct bt_value *user_attributes)
336{
337 BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class");
338 BT_ASSERT_PRE_NON_NULL(user_attributes, "User attributes");
339 BT_ASSERT_PRE(user_attributes->type == BT_VALUE_TYPE_MAP,
340 "User attributes object is not a map value object.");
341 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class);
864aa43f 342 bt_object_put_ref_no_null_check(clock_class->user_attributes);
af90138b 343 clock_class->user_attributes = (void *) user_attributes;
864aa43f 344 bt_object_get_ref_no_null_check(clock_class->user_attributes);
af90138b
PP
345}
346
8c6884d9
PP
347void bt_clock_class_get_ref(const struct bt_clock_class *clock_class)
348{
349 bt_object_get_ref(clock_class);
350}
351
352void bt_clock_class_put_ref(const struct bt_clock_class *clock_class)
353{
354 bt_object_put_ref(clock_class);
355}
This page took 0.097507 seconds and 4 git commands to generate.