9191fa1a64bdd65fd6acf2f283ba04b91ef6fcbc
[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_NO_ERROR();
136 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class);
137 return clock_class->name.value;
138 }
139
140 BT_EXPORT
141 enum bt_clock_class_set_name_status bt_clock_class_set_name(
142 struct bt_clock_class *clock_class, const char *name)
143 {
144 BT_ASSERT_PRE_NO_ERROR();
145 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class);
146 BT_ASSERT_PRE_NAME_NON_NULL(name);
147 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class);
148 g_string_assign(clock_class->name.str, name);
149 clock_class->name.value = clock_class->name.str->str;
150 BT_LIB_LOGD("Set clock class's name: %!+K", clock_class);
151 return BT_FUNC_STATUS_OK;
152 }
153
154 BT_EXPORT
155 const char *bt_clock_class_get_description(
156 const struct bt_clock_class *clock_class)
157 {
158 BT_ASSERT_PRE_NO_ERROR();
159 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class);
160 return clock_class->description.value;
161 }
162
163 BT_EXPORT
164 enum bt_clock_class_set_description_status bt_clock_class_set_description(
165 struct bt_clock_class *clock_class, const char *descr)
166 {
167 BT_ASSERT_PRE_NO_ERROR();
168 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class);
169 BT_ASSERT_PRE_DESCR_NON_NULL(descr);
170 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class);
171 g_string_assign(clock_class->description.str, descr);
172 clock_class->description.value = clock_class->description.str->str;
173 BT_LIB_LOGD("Set clock class's description: %!+K",
174 clock_class);
175 return BT_FUNC_STATUS_OK;
176 }
177
178 BT_EXPORT
179 uint64_t bt_clock_class_get_frequency(const struct bt_clock_class *clock_class)
180 {
181 BT_ASSERT_PRE_NO_ERROR();
182 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class);
183 return clock_class->frequency;
184 }
185
186 BT_EXPORT
187 void bt_clock_class_set_frequency(struct bt_clock_class *clock_class,
188 uint64_t frequency)
189 {
190 BT_ASSERT_PRE_NO_ERROR();
191 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class);
192 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class);
193 BT_ASSERT_PRE("valid-frequency",
194 frequency != UINT64_C(-1) && frequency != 0,
195 "Invalid frequency: %![cc-]+K, new-freq=%" PRIu64,
196 clock_class, frequency);
197 BT_ASSERT_PRE("offset-cycles-lt-frequency",
198 clock_class->offset_cycles < frequency,
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);
203 BT_LIB_LOGD("Set clock class's frequency: %!+K", clock_class);
204 }
205
206 BT_EXPORT
207 uint64_t bt_clock_class_get_precision(const struct bt_clock_class *clock_class)
208 {
209 BT_ASSERT_PRE_NO_ERROR();
210 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class);
211 return clock_class->precision;
212 }
213
214 BT_EXPORT
215 void bt_clock_class_set_precision(struct bt_clock_class *clock_class,
216 uint64_t precision)
217 {
218 BT_ASSERT_PRE_NO_ERROR();
219 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class);
220 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class);
221 BT_ASSERT_PRE("valid-precision", precision != UINT64_C(-1),
222 "Invalid precision: %![cc-]+K, new-precision=%" PRIu64,
223 clock_class, precision);
224 clock_class->precision = precision;
225 BT_LIB_LOGD("Set clock class's precision: %!+K", clock_class);
226 }
227
228 BT_EXPORT
229 void bt_clock_class_get_offset(const struct bt_clock_class *clock_class,
230 int64_t *seconds, uint64_t *cycles)
231 {
232 BT_ASSERT_PRE_NO_ERROR();
233 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class);
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)");
237 *seconds = clock_class->offset_seconds;
238 *cycles = clock_class->offset_cycles;
239 }
240
241 BT_EXPORT
242 void bt_clock_class_set_offset(struct bt_clock_class *clock_class,
243 int64_t seconds, uint64_t cycles)
244 {
245 BT_ASSERT_PRE_NO_ERROR();
246 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class);
247 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class);
248 BT_ASSERT_PRE("offset-cycles-lt-frequency",
249 cycles < clock_class->frequency,
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);
255 BT_LIB_LOGD("Set clock class's offset: %!+K", clock_class);
256 }
257
258 BT_EXPORT
259 bt_bool bt_clock_class_origin_is_unix_epoch(const struct bt_clock_class *clock_class)
260 {
261 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class);
262 return (bool) clock_class->origin_is_unix_epoch;
263 }
264
265 BT_EXPORT
266 void bt_clock_class_set_origin_is_unix_epoch(struct bt_clock_class *clock_class,
267 bt_bool origin_is_unix_epoch)
268 {
269 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class);
270 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class);
271 clock_class->origin_is_unix_epoch = (bool) origin_is_unix_epoch;
272 BT_LIB_LOGD("Set clock class's origin is Unix epoch property: %!+K",
273 clock_class);
274 }
275
276 BT_EXPORT
277 bt_uuid bt_clock_class_get_uuid(const struct bt_clock_class *clock_class)
278 {
279 BT_ASSERT_PRE_NO_ERROR();
280 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class);
281 return clock_class->uuid.value;
282 }
283
284 BT_EXPORT
285 void bt_clock_class_set_uuid(struct bt_clock_class *clock_class,
286 bt_uuid uuid)
287 {
288 BT_ASSERT_PRE_NO_ERROR();
289 BT_ASSERT_PRE_CLK_CLS_NON_NULL(clock_class);
290 BT_ASSERT_PRE_UUID_NON_NULL(uuid);
291 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class);
292 bt_uuid_copy(clock_class->uuid.uuid, uuid);
293 clock_class->uuid.value = clock_class->uuid.uuid;
294 BT_LIB_LOGD("Set clock class's UUID: %!+K", clock_class);
295 }
296
297 void _bt_clock_class_freeze(const struct bt_clock_class *clock_class)
298 {
299 BT_ASSERT(clock_class);
300
301 if (clock_class->frozen) {
302 return;
303 }
304
305 BT_LIB_LOGD("Freezing clock class's user attributes: %!+v",
306 clock_class->user_attributes);
307 bt_value_freeze(clock_class->user_attributes);
308 BT_LIB_LOGD("Freezing clock class: %!+K", clock_class);
309 ((struct bt_clock_class *) clock_class)->frozen = 1;
310 }
311
312 BT_EXPORT
313 enum bt_clock_class_cycles_to_ns_from_origin_status
314 bt_clock_class_cycles_to_ns_from_origin(
315 const struct bt_clock_class *clock_class,
316 uint64_t cycles, int64_t *ns)
317 {
318 int ret;
319
320 BT_ASSERT_PRE_DEV_NO_ERROR();
321 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class);
322 BT_ASSERT_PRE_DEV_NON_NULL("nanoseconds-output", ns,
323 "Nanoseconds (output)");
324 ret = bt_util_ns_from_origin_clock_class(clock_class, cycles, ns);
325 if (ret) {
326 BT_LIB_LOGE_APPEND_CAUSE("Cannot convert cycles to nanoseconds "
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);
331 ret = BT_FUNC_STATUS_OVERFLOW_ERROR;
332 }
333
334 return ret;
335 }
336
337 BT_EXPORT
338 const struct bt_value *bt_clock_class_borrow_user_attributes_const(
339 const struct bt_clock_class *clock_class)
340 {
341 BT_ASSERT_PRE_NO_ERROR();
342 BT_ASSERT_PRE_DEV_CLK_CLS_NON_NULL(clock_class);
343 return clock_class->user_attributes;
344 }
345
346 BT_EXPORT
347 struct 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
354 BT_EXPORT
355 void bt_clock_class_set_user_attributes(
356 struct bt_clock_class *clock_class,
357 const struct bt_value *user_attributes)
358 {
359 BT_ASSERT_PRE_NO_ERROR();
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);
363 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class);
364 bt_object_put_ref_no_null_check(clock_class->user_attributes);
365 clock_class->user_attributes = (void *) user_attributes;
366 bt_object_get_ref_no_null_check(clock_class->user_attributes);
367 }
368
369 BT_EXPORT
370 void bt_clock_class_get_ref(const struct bt_clock_class *clock_class)
371 {
372 bt_object_get_ref(clock_class);
373 }
374
375 BT_EXPORT
376 void 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.035354 seconds and 3 git commands to generate.