0b449e277ad2e4b8f5911373de10a55c822d60ff
[babeltrace.git] / src / lib / trace-ir / clock-class.c
1 /*
2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
3 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24 #define BT_LOG_TAG "LIB/CLOCK-CLASS"
25 #include "lib/logging.h"
26
27 #include "lib/assert-pre.h"
28 #include "common/uuid.h"
29 #include <babeltrace2/trace-ir/clock-class-const.h>
30 #include <babeltrace2/trace-ir/clock-class.h>
31 #include "clock-class.h"
32 #include "clock-snapshot.h"
33 #include "utils.h"
34 #include "compat/compiler.h"
35 #include <babeltrace2/types.h>
36 #include "compat/string.h"
37 #include <inttypes.h>
38 #include <stdbool.h>
39 #include "lib/object.h"
40 #include "common/assert.h"
41 #include "lib/func-status.h"
42 #include "lib/value.h"
43
44 #define BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(_cc) \
45 BT_ASSERT_PRE_DEV_HOT((_cc), "Clock class", ": %!+K", (_cc))
46
47 static
48 void destroy_clock_class(struct bt_object *obj)
49 {
50 struct bt_clock_class *clock_class = (void *) obj;
51
52 BT_LIB_LOGD("Destroying clock class: %!+K", clock_class);
53 BT_OBJECT_PUT_REF_AND_RESET(clock_class->user_attributes);
54
55 if (clock_class->name.str) {
56 g_string_free(clock_class->name.str, TRUE);
57 clock_class->name.str = NULL;
58 clock_class->name.value = NULL;
59 }
60
61 if (clock_class->description.str) {
62 g_string_free(clock_class->description.str, TRUE);
63 clock_class->description.str = NULL;
64 clock_class->description.value = NULL;
65 }
66
67 bt_object_pool_finalize(&clock_class->cs_pool);
68 g_free(clock_class);
69 }
70
71 static
72 void free_clock_snapshot(struct bt_clock_snapshot *clock_snapshot,
73 struct bt_clock_class *clock_class)
74 {
75 bt_clock_snapshot_destroy(clock_snapshot);
76 }
77
78 static inline
79 void set_base_offset(struct bt_clock_class *clock_class)
80 {
81 clock_class->base_offset.overflows = bt_util_get_base_offset_ns(
82 clock_class->offset_seconds, clock_class->offset_cycles,
83 clock_class->frequency, &clock_class->base_offset.value_ns);
84 }
85
86 struct bt_clock_class *bt_clock_class_create(bt_self_component *self_comp)
87 {
88 int ret;
89 struct bt_clock_class *clock_class = NULL;
90
91 BT_ASSERT_PRE_NON_NULL(self_comp, "Self component");
92 BT_LOGD_STR("Creating default clock class object");
93
94 clock_class = g_new0(struct bt_clock_class, 1);
95 if (!clock_class) {
96 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one clock class.");
97 goto error;
98 }
99
100 bt_object_init_shared(&clock_class->base, destroy_clock_class);
101
102 clock_class->user_attributes = bt_value_map_create();
103 if (!clock_class->user_attributes) {
104 BT_LIB_LOGE_APPEND_CAUSE(
105 "Failed to create a map value object.");
106 goto error;
107 }
108
109 clock_class->name.str = g_string_new(NULL);
110 if (!clock_class->name.str) {
111 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
112 goto error;
113 }
114
115 clock_class->description.str = g_string_new(NULL);
116 if (!clock_class->description.str) {
117 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString.");
118 goto error;
119 }
120
121 clock_class->frequency = UINT64_C(1000000000);
122 clock_class->origin_is_unix_epoch = BT_TRUE;
123 set_base_offset(clock_class);
124 ret = bt_object_pool_initialize(&clock_class->cs_pool,
125 (bt_object_pool_new_object_func) bt_clock_snapshot_new,
126 (bt_object_pool_destroy_object_func)
127 free_clock_snapshot,
128 clock_class);
129 if (ret) {
130 BT_LIB_LOGE_APPEND_CAUSE(
131 "Failed to initialize clock snapshot pool: ret=%d",
132 ret);
133 goto error;
134 }
135
136 BT_LIB_LOGD("Created clock class object: %!+K", clock_class);
137 goto end;
138
139 error:
140 BT_OBJECT_PUT_REF_AND_RESET(clock_class);
141
142 end:
143 return clock_class;
144 }
145
146 const char *bt_clock_class_get_name(const struct bt_clock_class *clock_class)
147 {
148 BT_ASSERT_PRE_DEV_NON_NULL(clock_class, "Clock class");
149 return clock_class->name.value;
150 }
151
152 enum bt_clock_class_set_name_status bt_clock_class_set_name(
153 struct bt_clock_class *clock_class, const char *name)
154 {
155 BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class");
156 BT_ASSERT_PRE_NON_NULL(name, "Name");
157 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class);
158 g_string_assign(clock_class->name.str, name);
159 clock_class->name.value = clock_class->name.str->str;
160 BT_LIB_LOGD("Set clock class's name: %!+K", clock_class);
161 return BT_FUNC_STATUS_OK;
162 }
163
164 const char *bt_clock_class_get_description(
165 const struct bt_clock_class *clock_class)
166 {
167 BT_ASSERT_PRE_DEV_NON_NULL(clock_class, "Clock class");
168 return clock_class->description.value;
169 }
170
171 enum bt_clock_class_set_description_status bt_clock_class_set_description(
172 struct bt_clock_class *clock_class, const char *descr)
173 {
174 BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class");
175 BT_ASSERT_PRE_NON_NULL(descr, "Description");
176 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class);
177 g_string_assign(clock_class->description.str, descr);
178 clock_class->description.value = clock_class->description.str->str;
179 BT_LIB_LOGD("Set clock class's description: %!+K",
180 clock_class);
181 return BT_FUNC_STATUS_OK;
182 }
183
184 uint64_t bt_clock_class_get_frequency(const struct bt_clock_class *clock_class)
185 {
186 BT_ASSERT_PRE_DEV_NON_NULL(clock_class, "Clock class");
187 return clock_class->frequency;
188 }
189
190 void bt_clock_class_set_frequency(struct bt_clock_class *clock_class,
191 uint64_t frequency)
192 {
193 BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class");
194 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class);
195 BT_ASSERT_PRE(frequency != UINT64_C(-1) && frequency != 0,
196 "Invalid frequency: %![cc-]+K, new-freq=%" PRIu64,
197 clock_class, frequency);
198 BT_ASSERT_PRE(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 uint64_t bt_clock_class_get_precision(const struct bt_clock_class *clock_class)
207 {
208 BT_ASSERT_PRE_DEV_NON_NULL(clock_class, "Clock class");
209 return clock_class->precision;
210 }
211
212 void bt_clock_class_set_precision(struct bt_clock_class *clock_class,
213 uint64_t precision)
214 {
215 BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class");
216 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class);
217 BT_ASSERT_PRE(precision != UINT64_C(-1),
218 "Invalid precision: %![cc-]+K, new-precision=%" PRIu64,
219 clock_class, precision);
220 clock_class->precision = precision;
221 BT_LIB_LOGD("Set clock class's precision: %!+K", clock_class);
222 }
223
224 void bt_clock_class_get_offset(const struct bt_clock_class *clock_class,
225 int64_t *seconds, uint64_t *cycles)
226 {
227 BT_ASSERT_PRE_DEV_NON_NULL(clock_class, "Clock class");
228 BT_ASSERT_PRE_DEV_NON_NULL(seconds, "Seconds (output)");
229 BT_ASSERT_PRE_DEV_NON_NULL(cycles, "Cycles (output)");
230 *seconds = clock_class->offset_seconds;
231 *cycles = clock_class->offset_cycles;
232 }
233
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_NON_NULL(clock_class, "Clock class");
238 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class);
239 BT_ASSERT_PRE(cycles < clock_class->frequency,
240 "Offset (cycles) is greater than clock class's frequency: "
241 "%![cc-]+K, new-offset-cycles=%" PRIu64, clock_class, cycles);
242 clock_class->offset_seconds = seconds;
243 clock_class->offset_cycles = cycles;
244 set_base_offset(clock_class);
245 BT_LIB_LOGD("Set clock class's offset: %!+K", clock_class);
246 }
247
248 bt_bool bt_clock_class_origin_is_unix_epoch(const struct bt_clock_class *clock_class)
249 {
250 BT_ASSERT_PRE_DEV_NON_NULL(clock_class, "Clock class");
251 return (bool) clock_class->origin_is_unix_epoch;
252 }
253
254 void bt_clock_class_set_origin_is_unix_epoch(struct bt_clock_class *clock_class,
255 bt_bool origin_is_unix_epoch)
256 {
257 BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class");
258 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class);
259 clock_class->origin_is_unix_epoch = (bool) origin_is_unix_epoch;
260 BT_LIB_LOGD("Set clock class's origin is Unix epoch property: %!+K",
261 clock_class);
262 }
263
264 bt_uuid bt_clock_class_get_uuid(const struct bt_clock_class *clock_class)
265 {
266 BT_ASSERT_PRE_DEV_NON_NULL(clock_class, "Clock class");
267 return clock_class->uuid.value;
268 }
269
270 void bt_clock_class_set_uuid(struct bt_clock_class *clock_class,
271 bt_uuid uuid)
272 {
273 BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class");
274 BT_ASSERT_PRE_NON_NULL(uuid, "UUID");
275 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class);
276 bt_uuid_copy(clock_class->uuid.uuid, uuid);
277 clock_class->uuid.value = clock_class->uuid.uuid;
278 BT_LIB_LOGD("Set clock class's UUID: %!+K", clock_class);
279 }
280
281 BT_HIDDEN
282 void _bt_clock_class_freeze(const struct bt_clock_class *clock_class)
283 {
284 BT_ASSERT(clock_class);
285
286 if (clock_class->frozen) {
287 return;
288 }
289
290 BT_LIB_LOGD("Freezing clock class's user attributes: %!+v",
291 clock_class->user_attributes);
292 bt_value_freeze(clock_class->user_attributes);
293 BT_LIB_LOGD("Freezing clock class: %!+K", clock_class);
294 ((struct bt_clock_class *) clock_class)->frozen = 1;
295 }
296
297 enum bt_clock_class_cycles_to_ns_from_origin_status
298 bt_clock_class_cycles_to_ns_from_origin(
299 const struct bt_clock_class *clock_class,
300 uint64_t cycles, int64_t *ns)
301 {
302 int ret;
303
304 BT_ASSERT_PRE_DEV_NON_NULL(clock_class, "Clock class");
305 BT_ASSERT_PRE_DEV_NON_NULL(ns, "Nanoseconds (output)");
306 ret = bt_util_ns_from_origin_clock_class(clock_class, cycles, ns);
307 if (ret) {
308 BT_LIB_LOGE_APPEND_CAUSE("Cannot convert cycles to nanoseconds "
309 "from origin for given clock class: "
310 "value overflows the signed 64-bit integer range: "
311 "%![cc-]+K, cycles=%" PRIu64,
312 clock_class, cycles);
313 ret = BT_FUNC_STATUS_OVERFLOW_ERROR;
314 }
315
316 return ret;
317 }
318
319 const struct bt_value *bt_clock_class_borrow_user_attributes_const(
320 const struct bt_clock_class *clock_class)
321 {
322 BT_ASSERT_PRE_DEV_NON_NULL(clock_class, "Clock class");
323 return clock_class->user_attributes;
324 }
325
326 struct bt_value *bt_clock_class_borrow_user_attributes(
327 struct bt_clock_class *clock_class)
328 {
329 return (void *) bt_clock_class_borrow_user_attributes_const(
330 (void *) clock_class);
331 }
332
333 void bt_clock_class_set_user_attributes(
334 struct bt_clock_class *clock_class,
335 const struct bt_value *user_attributes)
336 {
337 BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class");
338 BT_ASSERT_PRE_NON_NULL(user_attributes, "User attributes");
339 BT_ASSERT_PRE(user_attributes->type == BT_VALUE_TYPE_MAP,
340 "User attributes object is not a map value object.");
341 BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class);
342 bt_object_put_ref_no_null_check(clock_class->user_attributes);
343 clock_class->user_attributes = (void *) user_attributes;
344 bt_object_get_ref_no_null_check(clock_class->user_attributes);
345 }
346
347 void bt_clock_class_get_ref(const struct bt_clock_class *clock_class)
348 {
349 bt_object_get_ref(clock_class);
350 }
351
352 void bt_clock_class_put_ref(const struct bt_clock_class *clock_class)
353 {
354 bt_object_put_ref(clock_class);
355 }
This page took 0.035671 seconds and 3 git commands to generate.