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