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