Revert "Fix: lib: add missing BT_ASSERT_PRE_NO_ERROR in trace-ir/clock-class.c"
[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("clock-class", (_cc), "Clock class", \
29 ": %!+K", (_cc))
30
31 static
32 void destroy_clock_class(struct bt_object *obj)
33 {
34 struct bt_clock_class *clock_class = (void *) obj;
35
36 BT_LIB_LOGD("Destroying clock class: %!+K", clock_class);
37 BT_OBJECT_PUT_REF_AND_RESET(clock_class->user_attributes);
38
39 if (clock_class->name.str) {
40 g_string_free(clock_class->name.str, TRUE);
41 clock_class->name.str = NULL;
42 clock_class->name.value = NULL;
43 }
44
45 if (clock_class->description.str) {
46 g_string_free(clock_class->description.str, TRUE);
47 clock_class->description.str = NULL;
48 clock_class->description.value = NULL;
49 }
50
51 bt_object_pool_finalize(&clock_class->cs_pool);
52 g_free(clock_class);
53 }
54
55 static
56 void free_clock_snapshot(struct bt_clock_snapshot *clock_snapshot,
57 struct bt_clock_class *clock_class)
58 {
59 bt_clock_snapshot_destroy(clock_snapshot);
60 }
61
62 static inline
63 void set_base_offset(struct bt_clock_class *clock_class)
64 {
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);
68 }
69
70 BT_EXPORT
71 struct bt_clock_class *bt_clock_class_create(bt_self_component *self_comp)
72 {
73 int ret;
74 struct bt_clock_class *clock_class = NULL;
75
76 BT_ASSERT_PRE_NO_ERROR();
77 BT_ASSERT_PRE_COMP_NON_NULL(self_comp);
78 BT_LOGD_STR("Creating default clock class object");
79
80 clock_class = g_new0(struct bt_clock_class, 1);
81 if (!clock_class) {
82 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one clock class.");
83 goto error;
84 }
85
86 bt_object_init_shared(&clock_class->base, destroy_clock_class);
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
95 clock_class->name.str = g_string_new(NULL);
96 if (!clock_class->name.str) {
97 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
98 goto error;
99 }
100
101 clock_class->description.str = g_string_new(NULL);
102 if (!clock_class->description.str) {
103 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
104 goto error;
105 }
106
107 clock_class->frequency = UINT64_C(1000000000);
108 clock_class->origin_is_unix_epoch = BT_TRUE;
109 set_base_offset(clock_class);
110 ret = bt_object_pool_initialize(&clock_class->cs_pool,
111 (bt_object_pool_new_object_func) bt_clock_snapshot_new,
112 (bt_object_pool_destroy_object_func)
113 free_clock_snapshot,
114 clock_class);
115 if (ret) {
116 BT_LIB_LOGE_APPEND_CAUSE(
117 "Failed to initialize clock snapshot pool: ret=%d",
118 ret);
119 goto error;
120 }
121
122 BT_LIB_LOGD("Created clock class object: %!+K", clock_class);
123 goto end;
124
125 error:
126 BT_OBJECT_PUT_REF_AND_RESET(clock_class);
127
128 end:
129 return clock_class;
130 }
131
132 BT_EXPORT
133 const char *bt_clock_class_get_name(const struct bt_clock_class *clock_class)
134 {
135 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class);
136 return clock_class->name.value;
137 }
138
139 BT_EXPORT
140 enum bt_clock_class_set_name_status bt_clock_class_set_name(
141 struct bt_clock_class *clock_class, const char *name)
142 {
143 BT_ASSERT_PRE_NO_ERROR();
144 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class);
145 BT_ASSERT_PRE_NAME_NON_NULL(name);
146 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class);
147 g_string_assign(clock_class->name.str, name);
148 clock_class->name.value = clock_class->name.str->str;
149 BT_LIB_LOGD("Set clock class's name: %!+K", clock_class);
150 return BT_FUNC_STATUS_OK;
151 }
152
153 BT_EXPORT
154 const char *bt_clock_class_get_description(
155 const struct bt_clock_class *clock_class)
156 {
157 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class);
158 return clock_class->description.value;
159 }
160
161 BT_EXPORT
162 enum bt_clock_class_set_description_status bt_clock_class_set_description(
163 struct bt_clock_class *clock_class, const char *descr)
164 {
165 BT_ASSERT_PRE_NO_ERROR();
166 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class);
167 BT_ASSERT_PRE_DESCR_NON_NULL(descr);
168 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class);
169 g_string_assign(clock_class->description.str, descr);
170 clock_class->description.value = clock_class->description.str->str;
171 BT_LIB_LOGD("Set clock class's description: %!+K",
172 clock_class);
173 return BT_FUNC_STATUS_OK;
174 }
175
176 BT_EXPORT
177 uint64_t bt_clock_class_get_frequency(const struct bt_clock_class *clock_class)
178 {
179 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class);
180 return clock_class->frequency;
181 }
182
183 BT_EXPORT
184 void bt_clock_class_set_frequency(struct bt_clock_class *clock_class,
185 uint64_t frequency)
186 {
187 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class);
188 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class);
189 BT_ASSERT_PRE("valid-frequency",
190 frequency != UINT64_C(-1) && frequency != 0,
191 "Invalid frequency: %![cc-]+K, new-freq=%" PRIu64,
192 clock_class, frequency);
193 BT_ASSERT_PRE("offset-cycles-lt-frequency",
194 clock_class->offset_cycles < frequency,
195 "Offset (cycles) is greater than clock class's frequency: "
196 "%![cc-]+K, new-freq=%" PRIu64, clock_class, frequency);
197 clock_class->frequency = frequency;
198 set_base_offset(clock_class);
199 BT_LIB_LOGD("Set clock class's frequency: %!+K", clock_class);
200 }
201
202 BT_EXPORT
203 uint64_t bt_clock_class_get_precision(const struct bt_clock_class *clock_class)
204 {
205 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class);
206 return clock_class->precision;
207 }
208
209 BT_EXPORT
210 void bt_clock_class_set_precision(struct bt_clock_class *clock_class,
211 uint64_t precision)
212 {
213 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class);
214 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class);
215 BT_ASSERT_PRE("valid-precision", precision != UINT64_C(-1),
216 "Invalid precision: %![cc-]+K, new-precision=%" PRIu64,
217 clock_class, precision);
218 clock_class->precision = precision;
219 BT_LIB_LOGD("Set clock class's precision: %!+K", clock_class);
220 }
221
222 BT_EXPORT
223 void bt_clock_class_get_offset(const struct bt_clock_class *clock_class,
224 int64_t *seconds, uint64_t *cycles)
225 {
226 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class);
227 BT_ASSERT_PRE_DEV_NON_NULL("seconds-output", seconds,
228 "Seconds (output)");
229 BT_ASSERT_PRE_DEV_NON_NULL("cycles-output", cycles, "Cycles (output)");
230 *seconds = clock_class->offset_seconds;
231 *cycles = clock_class->offset_cycles;
232 }
233
234 BT_EXPORT
235 void bt_clock_class_set_offset(struct bt_clock_class *clock_class,
236 int64_t seconds, uint64_t cycles)
237 {
238 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class);
239 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class);
240 BT_ASSERT_PRE("offset-cycles-lt-frequency",
241 cycles < clock_class->frequency,
242 "Offset (cycles) is greater than clock class's frequency: "
243 "%![cc-]+K, new-offset-cycles=%" PRIu64, clock_class, cycles);
244 clock_class->offset_seconds = seconds;
245 clock_class->offset_cycles = cycles;
246 set_base_offset(clock_class);
247 BT_LIB_LOGD("Set clock class's offset: %!+K", clock_class);
248 }
249
250 BT_EXPORT
251 bt_bool bt_clock_class_origin_is_unix_epoch(const struct bt_clock_class *clock_class)
252 {
253 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class);
254 return (bool) clock_class->origin_is_unix_epoch;
255 }
256
257 BT_EXPORT
258 void bt_clock_class_set_origin_is_unix_epoch(struct bt_clock_class *clock_class,
259 bt_bool origin_is_unix_epoch)
260 {
261 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class);
262 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class);
263 clock_class->origin_is_unix_epoch = (bool) origin_is_unix_epoch;
264 BT_LIB_LOGD("Set clock class's origin is Unix epoch property: %!+K",
265 clock_class);
266 }
267
268 BT_EXPORT
269 bt_uuid bt_clock_class_get_uuid(const struct bt_clock_class *clock_class)
270 {
271 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class);
272 return clock_class->uuid.value;
273 }
274
275 BT_EXPORT
276 void bt_clock_class_set_uuid(struct bt_clock_class *clock_class,
277 bt_uuid uuid)
278 {
279 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class);
280 BT_ASSERT_PRE_UUID_NON_NULL(uuid);
281 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class);
282 bt_uuid_copy(clock_class->uuid.uuid, uuid);
283 clock_class->uuid.value = clock_class->uuid.uuid;
284 BT_LIB_LOGD("Set clock class's UUID: %!+K", clock_class);
285 }
286
287 void _bt_clock_class_freeze(const struct bt_clock_class *clock_class)
288 {
289 BT_ASSERT(clock_class);
290
291 if (clock_class->frozen) {
292 return;
293 }
294
295 BT_LIB_LOGD("Freezing clock class's user attributes: %!+v",
296 clock_class->user_attributes);
297 bt_value_freeze(clock_class->user_attributes);
298 BT_LIB_LOGD("Freezing clock class: %!+K", clock_class);
299 ((struct bt_clock_class *) clock_class)->frozen = 1;
300 }
301
302 BT_EXPORT
303 enum bt_clock_class_cycles_to_ns_from_origin_status
304 bt_clock_class_cycles_to_ns_from_origin(
305 const struct bt_clock_class *clock_class,
306 uint64_t cycles, int64_t *ns)
307 {
308 int ret;
309
310 BT_ASSERT_PRE_DEV_NO_ERROR();
311 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class);
312 BT_ASSERT_PRE_DEV_NON_NULL("nanoseconds-output", ns,
313 "Nanoseconds (output)");
314 ret = bt_util_ns_from_origin_clock_class(clock_class, cycles, ns);
315 if (ret) {
316 BT_LIB_LOGE_APPEND_CAUSE("Cannot convert cycles to nanoseconds "
317 "from origin for given clock class: "
318 "value overflows the signed 64-bit integer range: "
319 "%![cc-]+K, cycles=%" PRIu64,
320 clock_class, cycles);
321 ret = BT_FUNC_STATUS_OVERFLOW_ERROR;
322 }
323
324 return ret;
325 }
326
327 BT_EXPORT
328 const struct bt_value *bt_clock_class_borrow_user_attributes_const(
329 const struct bt_clock_class *clock_class)
330 {
331 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class);
332 return clock_class->user_attributes;
333 }
334
335 BT_EXPORT
336 struct bt_value *bt_clock_class_borrow_user_attributes(
337 struct bt_clock_class *clock_class)
338 {
339 return (void *) bt_clock_class_borrow_user_attributes_const(
340 (void *) clock_class);
341 }
342
343 BT_EXPORT
344 void bt_clock_class_set_user_attributes(
345 struct bt_clock_class *clock_class,
346 const struct bt_value *user_attributes)
347 {
348 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class);
349 BT_ASSERT_PRE_USER_ATTRS_NON_NULL(user_attributes);
350 BT_ASSERT_PRE_USER_ATTRS_IS_MAP(user_attributes);
351 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class);
352 bt_object_put_ref_no_null_check(clock_class->user_attributes);
353 clock_class->user_attributes = (void *) user_attributes;
354 bt_object_get_ref_no_null_check(clock_class->user_attributes);
355 }
356
357 BT_EXPORT
358 void bt_clock_class_get_ref(const struct bt_clock_class *clock_class)
359 {
360 bt_object_get_ref(clock_class);
361 }
362
363 BT_EXPORT
364 void bt_clock_class_put_ref(const struct bt_clock_class *clock_class)
365 {
366 bt_object_put_ref(clock_class);
367 }
This page took 0.035686 seconds and 4 git commands to generate.