lib: assign a unique ID to each pre/postcond. and report it on failure
[babeltrace.git] / src / lib / trace-ir / clock-class.c
CommitLineData
273b65be 1/*
0235b0db
MJ
2 * SPDX-License-Identifier: MIT
3 *
e2f7325d 4 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
de9dd397 5 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
273b65be
JG
6 */
7
350ad6c1 8#define BT_LOG_TAG "LIB/CLOCK-CLASS"
c2d9d9cf 9#include "lib/logging.h"
0f5e83e5 10
d98421f2 11#include "lib/assert-cond.h"
6162e6b7 12#include "common/uuid.h"
3fadfbc0 13#include <babeltrace2/trace-ir/clock-class.h>
578e048b
MJ
14#include "clock-class.h"
15#include "clock-snapshot.h"
16#include "utils.h"
17#include "compat/compiler.h"
3fadfbc0 18#include <babeltrace2/types.h>
578e048b 19#include "compat/string.h"
273b65be 20#include <inttypes.h>
c4f23e30 21#include <stdbool.h>
578e048b
MJ
22#include "lib/object.h"
23#include "common/assert.h"
d24d5663 24#include "lib/func-status.h"
c6962c96 25#include "lib/value.h"
5134570b 26
d5b13b9b 27#define BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(_cc) \
1778c2a4
PP
28 BT_ASSERT_PRE_DEV_HOT("clock-class", (_cc), "Clock class", \
29 ": %!+K", (_cc))
312c056a
PP
30
31static
44c440bc 32void destroy_clock_class(struct bt_object *obj)
c06116f3 33{
44c440bc 34 struct bt_clock_class *clock_class = (void *) obj;
c06116f3 35
44c440bc 36 BT_LIB_LOGD("Destroying clock class: %!+K", clock_class);
c6962c96 37 BT_OBJECT_PUT_REF_AND_RESET(clock_class->user_attributes);
273b65be 38
44c440bc
PP
39 if (clock_class->name.str) {
40 g_string_free(clock_class->name.str, TRUE);
238b7404
PP
41 clock_class->name.str = NULL;
42 clock_class->name.value = NULL;
5134570b
PP
43 }
44
44c440bc
PP
45 if (clock_class->description.str) {
46 g_string_free(clock_class->description.str, TRUE);
238b7404
PP
47 clock_class->description.str = NULL;
48 clock_class->description.value = NULL;
273b65be
JG
49 }
50
605e1019 51 bt_object_pool_finalize(&clock_class->cs_pool);
44c440bc 52 g_free(clock_class);
4c426c17
JG
53}
54
f3534905 55static
605e1019 56void free_clock_snapshot(struct bt_clock_snapshot *clock_snapshot,
44c440bc 57 struct bt_clock_class *clock_class)
f3534905 58{
605e1019 59 bt_clock_snapshot_destroy(clock_snapshot);
f3534905
PP
60}
61
44c440bc
PP
62static inline
63void set_base_offset(struct bt_clock_class *clock_class)
312c056a 64{
0f2d58c9
PP
65 clock_class->base_offset.overflows = bt_util_get_base_offset_ns(
66 clock_class->offset_seconds, clock_class->offset_cycles,
67 clock_class->frequency, &clock_class->base_offset.value_ns);
312c056a
PP
68}
69
7fcdb0a9 70struct bt_clock_class *bt_clock_class_create(bt_self_component *self_comp)
4c426c17
JG
71{
72 int ret;
50842bdc 73 struct bt_clock_class *clock_class = NULL;
4c426c17 74
17f3083a 75 BT_ASSERT_PRE_NO_ERROR();
d5b13b9b 76 BT_ASSERT_PRE_COMP_NON_NULL(self_comp);
44c440bc 77 BT_LOGD_STR("Creating default clock class object");
f3534905 78
50842bdc 79 clock_class = g_new0(struct bt_clock_class, 1);
ac0c6bdd 80 if (!clock_class) {
870631a2 81 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one clock class.");
4c426c17
JG
82 goto error;
83 }
84
44c440bc 85 bt_object_init_shared(&clock_class->base, destroy_clock_class);
c6962c96
PP
86
87 clock_class->user_attributes = bt_value_map_create();
88 if (!clock_class->user_attributes) {
89 BT_LIB_LOGE_APPEND_CAUSE(
90 "Failed to create a map value object.");
91 goto error;
92 }
93
44c440bc
PP
94 clock_class->name.str = g_string_new(NULL);
95 if (!clock_class->name.str) {
870631a2 96 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
44c440bc
PP
97 goto error;
98 }
85380e99 99
44c440bc
PP
100 clock_class->description.str = g_string_new(NULL);
101 if (!clock_class->description.str) {
870631a2 102 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
44c440bc 103 goto error;
273b65be
JG
104 }
105
44c440bc 106 clock_class->frequency = UINT64_C(1000000000);
5552377a 107 clock_class->origin_is_unix_epoch = BT_TRUE;
44c440bc 108 set_base_offset(clock_class);
605e1019
PP
109 ret = bt_object_pool_initialize(&clock_class->cs_pool,
110 (bt_object_pool_new_object_func) bt_clock_snapshot_new,
312c056a 111 (bt_object_pool_destroy_object_func)
605e1019 112 free_clock_snapshot,
312c056a
PP
113 clock_class);
114 if (ret) {
870631a2
PP
115 BT_LIB_LOGE_APPEND_CAUSE(
116 "Failed to initialize clock snapshot pool: ret=%d",
312c056a
PP
117 ret);
118 goto error;
119 }
120
44c440bc
PP
121 BT_LIB_LOGD("Created clock class object: %!+K", clock_class);
122 goto end;
123
273b65be 124error:
65300d60 125 BT_OBJECT_PUT_REF_AND_RESET(clock_class);
87d76bb1
JG
126
127end:
40f4ba76 128 return clock_class;
87d76bb1
JG
129}
130
40f4ba76 131const char *bt_clock_class_get_name(const struct bt_clock_class *clock_class)
87d76bb1 132{
d5b13b9b 133 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class);
44c440bc
PP
134 return clock_class->name.value;
135}
87d76bb1 136
d24d5663 137enum bt_clock_class_set_name_status bt_clock_class_set_name(
8c41fd73 138 struct bt_clock_class *clock_class, const char *name)
44c440bc 139{
17f3083a 140 BT_ASSERT_PRE_NO_ERROR();
d5b13b9b
PP
141 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class);
142 BT_ASSERT_PRE_NAME_NON_NULL(name);
bdb288b3 143 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class);
44c440bc
PP
144 g_string_assign(clock_class->name.str, name);
145 clock_class->name.value = clock_class->name.str->str;
3f7d4d90 146 BT_LIB_LOGD("Set clock class's name: %!+K", clock_class);
d24d5663 147 return BT_FUNC_STATUS_OK;
44c440bc 148}
87d76bb1 149
40f4ba76
PP
150const char *bt_clock_class_get_description(
151 const struct bt_clock_class *clock_class)
44c440bc 152{
d5b13b9b 153 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class);
44c440bc 154 return clock_class->description.value;
273b65be
JG
155}
156
d24d5663 157enum bt_clock_class_set_description_status bt_clock_class_set_description(
8c41fd73 158 struct bt_clock_class *clock_class, const char *descr)
273b65be 159{
17f3083a 160 BT_ASSERT_PRE_NO_ERROR();
d5b13b9b
PP
161 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class);
162 BT_ASSERT_PRE_DESCR_NON_NULL(descr);
bdb288b3 163 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class);
44c440bc
PP
164 g_string_assign(clock_class->description.str, descr);
165 clock_class->description.value = clock_class->description.str->str;
3f7d4d90 166 BT_LIB_LOGD("Set clock class's description: %!+K",
44c440bc 167 clock_class);
d24d5663 168 return BT_FUNC_STATUS_OK;
273b65be
JG
169}
170
40f4ba76 171uint64_t bt_clock_class_get_frequency(const struct bt_clock_class *clock_class)
87d76bb1 172{
d5b13b9b 173 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class);
44c440bc 174 return clock_class->frequency;
87d76bb1
JG
175}
176
40f4ba76 177void bt_clock_class_set_frequency(struct bt_clock_class *clock_class,
44c440bc 178 uint64_t frequency)
273b65be 179{
d5b13b9b 180 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class);
bdb288b3 181 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class);
1778c2a4
PP
182 BT_ASSERT_PRE("valid-frequency",
183 frequency != UINT64_C(-1) && frequency != 0,
44c440bc
PP
184 "Invalid frequency: %![cc-]+K, new-freq=%" PRIu64,
185 clock_class, frequency);
1778c2a4
PP
186 BT_ASSERT_PRE("offset-cycles-lt-frequency",
187 clock_class->offset_cycles < frequency,
44c440bc
PP
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);
3f7d4d90 192 BT_LIB_LOGD("Set clock class's frequency: %!+K", clock_class);
273b65be
JG
193}
194
40f4ba76 195uint64_t bt_clock_class_get_precision(const struct bt_clock_class *clock_class)
87d76bb1 196{
d5b13b9b 197 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class);
44c440bc 198 return clock_class->precision;
87d76bb1
JG
199}
200
40f4ba76 201void bt_clock_class_set_precision(struct bt_clock_class *clock_class,
ac0c6bdd 202 uint64_t precision)
273b65be 203{
d5b13b9b 204 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class);
bdb288b3 205 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class);
1778c2a4 206 BT_ASSERT_PRE("valid-precision", precision != UINT64_C(-1),
44c440bc
PP
207 "Invalid precision: %![cc-]+K, new-precision=%" PRIu64,
208 clock_class, precision);
ac0c6bdd 209 clock_class->precision = precision;
3f7d4d90 210 BT_LIB_LOGD("Set clock class's precision: %!+K", clock_class);
273b65be
JG
211}
212
40f4ba76 213void bt_clock_class_get_offset(const struct bt_clock_class *clock_class,
44c440bc 214 int64_t *seconds, uint64_t *cycles)
87d76bb1 215{
d5b13b9b 216 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class);
1778c2a4
PP
217 BT_ASSERT_PRE_DEV_NON_NULL("seconds-output", seconds,
218 "Seconds (output)");
219 BT_ASSERT_PRE_DEV_NON_NULL("cycles-output", cycles, "Cycles (output)");
44c440bc
PP
220 *seconds = clock_class->offset_seconds;
221 *cycles = clock_class->offset_cycles;
87d76bb1
JG
222}
223
40f4ba76 224void bt_clock_class_set_offset(struct bt_clock_class *clock_class,
44c440bc 225 int64_t seconds, uint64_t cycles)
273b65be 226{
d5b13b9b 227 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class);
bdb288b3 228 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class);
1778c2a4
PP
229 BT_ASSERT_PRE("offset-cycles-lt-frequency",
230 cycles < clock_class->frequency,
44c440bc
PP
231 "Offset (cycles) is greater than clock class's frequency: "
232 "%![cc-]+K, new-offset-cycles=%" PRIu64, clock_class, cycles);
233 clock_class->offset_seconds = seconds;
234 clock_class->offset_cycles = cycles;
235 set_base_offset(clock_class);
3f7d4d90 236 BT_LIB_LOGD("Set clock class's offset: %!+K", clock_class);
273b65be
JG
237}
238
5552377a 239bt_bool bt_clock_class_origin_is_unix_epoch(const struct bt_clock_class *clock_class)
87d76bb1 240{
d5b13b9b 241 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class);
5552377a 242 return (bool) clock_class->origin_is_unix_epoch;
87d76bb1
JG
243}
244
5552377a
PP
245void bt_clock_class_set_origin_is_unix_epoch(struct bt_clock_class *clock_class,
246 bt_bool origin_is_unix_epoch)
273b65be 247{
d5b13b9b 248 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class);
bdb288b3 249 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class);
5552377a 250 clock_class->origin_is_unix_epoch = (bool) origin_is_unix_epoch;
3f7d4d90 251 BT_LIB_LOGD("Set clock class's origin is Unix epoch property: %!+K",
44c440bc 252 clock_class);
273b65be
JG
253}
254
40f4ba76 255bt_uuid bt_clock_class_get_uuid(const struct bt_clock_class *clock_class)
85b743f4 256{
d5b13b9b 257 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class);
44c440bc 258 return clock_class->uuid.value;
85b743f4
JG
259}
260
40f4ba76 261void bt_clock_class_set_uuid(struct bt_clock_class *clock_class,
44c440bc 262 bt_uuid uuid)
85b743f4 263{
d5b13b9b
PP
264 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class);
265 BT_ASSERT_PRE_UUID_NON_NULL(uuid);
bdb288b3 266 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class);
6162e6b7 267 bt_uuid_copy(clock_class->uuid.uuid, uuid);
44c440bc 268 clock_class->uuid.value = clock_class->uuid.uuid;
3f7d4d90 269 BT_LIB_LOGD("Set clock class's UUID: %!+K", clock_class);
312c056a
PP
270}
271
272BT_HIDDEN
40f4ba76 273void _bt_clock_class_freeze(const struct bt_clock_class *clock_class)
312c056a 274{
312c056a 275 BT_ASSERT(clock_class);
6f57e458 276
44c440bc
PP
277 if (clock_class->frozen) {
278 return;
93dda901
PP
279 }
280
c6962c96
PP
281 BT_LIB_LOGD("Freezing clock class's user attributes: %!+v",
282 clock_class->user_attributes);
283 bt_value_freeze(clock_class->user_attributes);
44c440bc 284 BT_LIB_LOGD("Freezing clock class: %!+K", clock_class);
40f4ba76 285 ((struct bt_clock_class *) clock_class)->frozen = 1;
93dda901 286}
312c056a 287
d24d5663
PP
288enum bt_clock_class_cycles_to_ns_from_origin_status
289bt_clock_class_cycles_to_ns_from_origin(
40f4ba76 290 const struct bt_clock_class *clock_class,
312c056a
PP
291 uint64_t cycles, int64_t *ns)
292{
293 int ret;
312c056a 294
17f3083a 295 BT_ASSERT_PRE_DEV_NO_ERROR();
d5b13b9b 296 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class);
1778c2a4
PP
297 BT_ASSERT_PRE_DEV_NON_NULL("nanoseconds-output", ns,
298 "Nanoseconds (output)");
0f2d58c9 299 ret = bt_util_ns_from_origin_clock_class(clock_class, cycles, ns);
312c056a 300 if (ret) {
bc3d9692 301 BT_LIB_LOGE_APPEND_CAUSE("Cannot convert cycles to nanoseconds "
44c440bc
PP
302 "from origin for given clock class: "
303 "value overflows the signed 64-bit integer range: "
304 "%![cc-]+K, cycles=%" PRIu64,
305 clock_class, cycles);
520cdc82 306 ret = BT_FUNC_STATUS_OVERFLOW_ERROR;
312c056a
PP
307 }
308
312c056a
PP
309 return ret;
310}
c5b9b441 311
c6962c96
PP
312const struct bt_value *bt_clock_class_borrow_user_attributes_const(
313 const struct bt_clock_class *clock_class)
314{
d5b13b9b 315 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class);
c6962c96
PP
316 return clock_class->user_attributes;
317}
318
319struct bt_value *bt_clock_class_borrow_user_attributes(
320 struct bt_clock_class *clock_class)
321{
322 return (void *) bt_clock_class_borrow_user_attributes_const(
323 (void *) clock_class);
324}
325
326void bt_clock_class_set_user_attributes(
327 struct bt_clock_class *clock_class,
328 const struct bt_value *user_attributes)
329{
d5b13b9b
PP
330 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class);
331 BT_ASSERT_PRE_USER_ATTRS_NON_NULL(user_attributes);
332 BT_ASSERT_PRE_USER_ATTRS_IS_MAP(user_attributes);
c6962c96 333 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class);
6871026b 334 bt_object_put_ref_no_null_check(clock_class->user_attributes);
c6962c96 335 clock_class->user_attributes = (void *) user_attributes;
6871026b 336 bt_object_get_ref_no_null_check(clock_class->user_attributes);
c6962c96
PP
337}
338
c5b9b441
PP
339void bt_clock_class_get_ref(const struct bt_clock_class *clock_class)
340{
341 bt_object_get_ref(clock_class);
342}
343
344void bt_clock_class_put_ref(const struct bt_clock_class *clock_class)
345{
346 bt_object_put_ref(clock_class);
347}
This page took 0.125311 seconds and 4 git commands to generate.