Fix: lib: add missing BT_ASSERT_PRE_NO_ERROR in trace-ir/clock-class.c
[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
1353b066 70BT_EXPORT
7fcdb0a9 71struct bt_clock_class *bt_clock_class_create(bt_self_component *self_comp)
4c426c17
JG
72{
73 int ret;
50842bdc 74 struct bt_clock_class *clock_class = NULL;
4c426c17 75
17f3083a 76 BT_ASSERT_PRE_NO_ERROR();
d5b13b9b 77 BT_ASSERT_PRE_COMP_NON_NULL(self_comp);
44c440bc 78 BT_LOGD_STR("Creating default clock class object");
f3534905 79
50842bdc 80 clock_class = g_new0(struct bt_clock_class, 1);
ac0c6bdd 81 if (!clock_class) {
870631a2 82 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one clock class.");
4c426c17
JG
83 goto error;
84 }
85
44c440bc 86 bt_object_init_shared(&clock_class->base, destroy_clock_class);
c6962c96
PP
87
88 clock_class->user_attributes = bt_value_map_create();
89 if (!clock_class->user_attributes) {
90 BT_LIB_LOGE_APPEND_CAUSE(
91 "Failed to create a map value object.");
92 goto error;
93 }
94
44c440bc
PP
95 clock_class->name.str = g_string_new(NULL);
96 if (!clock_class->name.str) {
870631a2 97 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
44c440bc
PP
98 goto error;
99 }
85380e99 100
44c440bc
PP
101 clock_class->description.str = g_string_new(NULL);
102 if (!clock_class->description.str) {
870631a2 103 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
44c440bc 104 goto error;
273b65be
JG
105 }
106
44c440bc 107 clock_class->frequency = UINT64_C(1000000000);
5552377a 108 clock_class->origin_is_unix_epoch = BT_TRUE;
44c440bc 109 set_base_offset(clock_class);
605e1019
PP
110 ret = bt_object_pool_initialize(&clock_class->cs_pool,
111 (bt_object_pool_new_object_func) bt_clock_snapshot_new,
312c056a 112 (bt_object_pool_destroy_object_func)
605e1019 113 free_clock_snapshot,
312c056a
PP
114 clock_class);
115 if (ret) {
870631a2
PP
116 BT_LIB_LOGE_APPEND_CAUSE(
117 "Failed to initialize clock snapshot pool: ret=%d",
312c056a
PP
118 ret);
119 goto error;
120 }
121
44c440bc
PP
122 BT_LIB_LOGD("Created clock class object: %!+K", clock_class);
123 goto end;
124
273b65be 125error:
65300d60 126 BT_OBJECT_PUT_REF_AND_RESET(clock_class);
87d76bb1
JG
127
128end:
40f4ba76 129 return clock_class;
87d76bb1
JG
130}
131
1353b066 132BT_EXPORT
40f4ba76 133const char *bt_clock_class_get_name(const struct bt_clock_class *clock_class)
87d76bb1 134{
bbc5046d 135 BT_ASSERT_PRE_NO_ERROR();
d5b13b9b 136 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class);
44c440bc
PP
137 return clock_class->name.value;
138}
87d76bb1 139
1353b066 140BT_EXPORT
d24d5663 141enum bt_clock_class_set_name_status bt_clock_class_set_name(
8c41fd73 142 struct bt_clock_class *clock_class, const char *name)
44c440bc 143{
17f3083a 144 BT_ASSERT_PRE_NO_ERROR();
d5b13b9b
PP
145 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class);
146 BT_ASSERT_PRE_NAME_NON_NULL(name);
bdb288b3 147 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class);
44c440bc
PP
148 g_string_assign(clock_class->name.str, name);
149 clock_class->name.value = clock_class->name.str->str;
3f7d4d90 150 BT_LIB_LOGD("Set clock class's name: %!+K", clock_class);
d24d5663 151 return BT_FUNC_STATUS_OK;
44c440bc 152}
87d76bb1 153
1353b066 154BT_EXPORT
40f4ba76
PP
155const char *bt_clock_class_get_description(
156 const struct bt_clock_class *clock_class)
44c440bc 157{
bbc5046d 158 BT_ASSERT_PRE_NO_ERROR();
d5b13b9b 159 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class);
44c440bc 160 return clock_class->description.value;
273b65be
JG
161}
162
1353b066 163BT_EXPORT
d24d5663 164enum bt_clock_class_set_description_status bt_clock_class_set_description(
8c41fd73 165 struct bt_clock_class *clock_class, const char *descr)
273b65be 166{
17f3083a 167 BT_ASSERT_PRE_NO_ERROR();
d5b13b9b
PP
168 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class);
169 BT_ASSERT_PRE_DESCR_NON_NULL(descr);
bdb288b3 170 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class);
44c440bc
PP
171 g_string_assign(clock_class->description.str, descr);
172 clock_class->description.value = clock_class->description.str->str;
3f7d4d90 173 BT_LIB_LOGD("Set clock class's description: %!+K",
44c440bc 174 clock_class);
d24d5663 175 return BT_FUNC_STATUS_OK;
273b65be
JG
176}
177
1353b066 178BT_EXPORT
40f4ba76 179uint64_t bt_clock_class_get_frequency(const struct bt_clock_class *clock_class)
87d76bb1 180{
bbc5046d 181 BT_ASSERT_PRE_NO_ERROR();
d5b13b9b 182 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class);
44c440bc 183 return clock_class->frequency;
87d76bb1
JG
184}
185
1353b066 186BT_EXPORT
40f4ba76 187void bt_clock_class_set_frequency(struct bt_clock_class *clock_class,
44c440bc 188 uint64_t frequency)
273b65be 189{
bbc5046d 190 BT_ASSERT_PRE_NO_ERROR();
d5b13b9b 191 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class);
bdb288b3 192 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class);
1778c2a4
PP
193 BT_ASSERT_PRE("valid-frequency",
194 frequency != UINT64_C(-1) && frequency != 0,
44c440bc
PP
195 "Invalid frequency: %![cc-]+K, new-freq=%" PRIu64,
196 clock_class, frequency);
1778c2a4
PP
197 BT_ASSERT_PRE("offset-cycles-lt-frequency",
198 clock_class->offset_cycles < frequency,
44c440bc
PP
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);
3f7d4d90 203 BT_LIB_LOGD("Set clock class's frequency: %!+K", clock_class);
273b65be
JG
204}
205
1353b066 206BT_EXPORT
40f4ba76 207uint64_t bt_clock_class_get_precision(const struct bt_clock_class *clock_class)
87d76bb1 208{
bbc5046d 209 BT_ASSERT_PRE_NO_ERROR();
d5b13b9b 210 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class);
44c440bc 211 return clock_class->precision;
87d76bb1
JG
212}
213
1353b066 214BT_EXPORT
40f4ba76 215void bt_clock_class_set_precision(struct bt_clock_class *clock_class,
ac0c6bdd 216 uint64_t precision)
273b65be 217{
bbc5046d 218 BT_ASSERT_PRE_NO_ERROR();
d5b13b9b 219 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class);
bdb288b3 220 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class);
1778c2a4 221 BT_ASSERT_PRE("valid-precision", precision != UINT64_C(-1),
44c440bc
PP
222 "Invalid precision: %![cc-]+K, new-precision=%" PRIu64,
223 clock_class, precision);
ac0c6bdd 224 clock_class->precision = precision;
3f7d4d90 225 BT_LIB_LOGD("Set clock class's precision: %!+K", clock_class);
273b65be
JG
226}
227
1353b066 228BT_EXPORT
40f4ba76 229void bt_clock_class_get_offset(const struct bt_clock_class *clock_class,
44c440bc 230 int64_t *seconds, uint64_t *cycles)
87d76bb1 231{
bbc5046d 232 BT_ASSERT_PRE_NO_ERROR();
d5b13b9b 233 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class);
1778c2a4
PP
234 BT_ASSERT_PRE_DEV_NON_NULL("seconds-output", seconds,
235 "Seconds (output)");
236 BT_ASSERT_PRE_DEV_NON_NULL("cycles-output", cycles, "Cycles (output)");
44c440bc
PP
237 *seconds = clock_class->offset_seconds;
238 *cycles = clock_class->offset_cycles;
87d76bb1
JG
239}
240
1353b066 241BT_EXPORT
40f4ba76 242void bt_clock_class_set_offset(struct bt_clock_class *clock_class,
44c440bc 243 int64_t seconds, uint64_t cycles)
273b65be 244{
bbc5046d 245 BT_ASSERT_PRE_NO_ERROR();
d5b13b9b 246 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class);
bdb288b3 247 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class);
1778c2a4
PP
248 BT_ASSERT_PRE("offset-cycles-lt-frequency",
249 cycles < clock_class->frequency,
44c440bc
PP
250 "Offset (cycles) is greater than clock class's frequency: "
251 "%![cc-]+K, new-offset-cycles=%" PRIu64, clock_class, cycles);
252 clock_class->offset_seconds = seconds;
253 clock_class->offset_cycles = cycles;
254 set_base_offset(clock_class);
3f7d4d90 255 BT_LIB_LOGD("Set clock class's offset: %!+K", clock_class);
273b65be
JG
256}
257
1353b066 258BT_EXPORT
5552377a 259bt_bool bt_clock_class_origin_is_unix_epoch(const struct bt_clock_class *clock_class)
87d76bb1 260{
d5b13b9b 261 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class);
5552377a 262 return (bool) clock_class->origin_is_unix_epoch;
87d76bb1
JG
263}
264
1353b066 265BT_EXPORT
5552377a
PP
266void bt_clock_class_set_origin_is_unix_epoch(struct bt_clock_class *clock_class,
267 bt_bool origin_is_unix_epoch)
273b65be 268{
d5b13b9b 269 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class);
bdb288b3 270 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class);
5552377a 271 clock_class->origin_is_unix_epoch = (bool) origin_is_unix_epoch;
3f7d4d90 272 BT_LIB_LOGD("Set clock class's origin is Unix epoch property: %!+K",
44c440bc 273 clock_class);
273b65be
JG
274}
275
1353b066 276BT_EXPORT
40f4ba76 277bt_uuid bt_clock_class_get_uuid(const struct bt_clock_class *clock_class)
85b743f4 278{
bbc5046d 279 BT_ASSERT_PRE_NO_ERROR();
d5b13b9b 280 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class);
44c440bc 281 return clock_class->uuid.value;
85b743f4
JG
282}
283
1353b066 284BT_EXPORT
40f4ba76 285void bt_clock_class_set_uuid(struct bt_clock_class *clock_class,
44c440bc 286 bt_uuid uuid)
85b743f4 287{
bbc5046d 288 BT_ASSERT_PRE_NO_ERROR();
d5b13b9b
PP
289 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class);
290 BT_ASSERT_PRE_UUID_NON_NULL(uuid);
bdb288b3 291 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class);
6162e6b7 292 bt_uuid_copy(clock_class->uuid.uuid, uuid);
44c440bc 293 clock_class->uuid.value = clock_class->uuid.uuid;
3f7d4d90 294 BT_LIB_LOGD("Set clock class's UUID: %!+K", clock_class);
312c056a
PP
295}
296
40f4ba76 297void _bt_clock_class_freeze(const struct bt_clock_class *clock_class)
312c056a 298{
312c056a 299 BT_ASSERT(clock_class);
6f57e458 300
44c440bc
PP
301 if (clock_class->frozen) {
302 return;
93dda901
PP
303 }
304
c6962c96
PP
305 BT_LIB_LOGD("Freezing clock class's user attributes: %!+v",
306 clock_class->user_attributes);
307 bt_value_freeze(clock_class->user_attributes);
44c440bc 308 BT_LIB_LOGD("Freezing clock class: %!+K", clock_class);
40f4ba76 309 ((struct bt_clock_class *) clock_class)->frozen = 1;
93dda901 310}
312c056a 311
1353b066 312BT_EXPORT
d24d5663
PP
313enum bt_clock_class_cycles_to_ns_from_origin_status
314bt_clock_class_cycles_to_ns_from_origin(
40f4ba76 315 const struct bt_clock_class *clock_class,
312c056a
PP
316 uint64_t cycles, int64_t *ns)
317{
318 int ret;
312c056a 319
17f3083a 320 BT_ASSERT_PRE_DEV_NO_ERROR();
d5b13b9b 321 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class);
1778c2a4
PP
322 BT_ASSERT_PRE_DEV_NON_NULL("nanoseconds-output", ns,
323 "Nanoseconds (output)");
0f2d58c9 324 ret = bt_util_ns_from_origin_clock_class(clock_class, cycles, ns);
312c056a 325 if (ret) {
bc3d9692 326 BT_LIB_LOGE_APPEND_CAUSE("Cannot convert cycles to nanoseconds "
44c440bc
PP
327 "from origin for given clock class: "
328 "value overflows the signed 64-bit integer range: "
329 "%![cc-]+K, cycles=%" PRIu64,
330 clock_class, cycles);
520cdc82 331 ret = BT_FUNC_STATUS_OVERFLOW_ERROR;
312c056a
PP
332 }
333
312c056a
PP
334 return ret;
335}
c5b9b441 336
1353b066 337BT_EXPORT
c6962c96
PP
338const struct bt_value *bt_clock_class_borrow_user_attributes_const(
339 const struct bt_clock_class *clock_class)
340{
bbc5046d 341 BT_ASSERT_PRE_NO_ERROR();
d5b13b9b 342 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class);
c6962c96
PP
343 return clock_class->user_attributes;
344}
345
1353b066 346BT_EXPORT
c6962c96
PP
347struct bt_value *bt_clock_class_borrow_user_attributes(
348 struct bt_clock_class *clock_class)
349{
350 return (void *) bt_clock_class_borrow_user_attributes_const(
351 (void *) clock_class);
352}
353
1353b066 354BT_EXPORT
c6962c96
PP
355void bt_clock_class_set_user_attributes(
356 struct bt_clock_class *clock_class,
357 const struct bt_value *user_attributes)
358{
bbc5046d 359 BT_ASSERT_PRE_NO_ERROR();
d5b13b9b
PP
360 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class);
361 BT_ASSERT_PRE_USER_ATTRS_NON_NULL(user_attributes);
362 BT_ASSERT_PRE_USER_ATTRS_IS_MAP(user_attributes);
c6962c96 363 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class);
6871026b 364 bt_object_put_ref_no_null_check(clock_class->user_attributes);
c6962c96 365 clock_class->user_attributes = (void *) user_attributes;
6871026b 366 bt_object_get_ref_no_null_check(clock_class->user_attributes);
c6962c96
PP
367}
368
1353b066 369BT_EXPORT
c5b9b441
PP
370void bt_clock_class_get_ref(const struct bt_clock_class *clock_class)
371{
372 bt_object_get_ref(clock_class);
373}
374
1353b066 375BT_EXPORT
c5b9b441
PP
376void bt_clock_class_put_ref(const struct bt_clock_class *clock_class)
377{
378 bt_object_put_ref(clock_class);
379}
This page took 0.117859 seconds and 4 git commands to generate.