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