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