Commit | Line | Data |
---|---|---|
273b65be | 1 | /* |
e2f7325d | 2 | * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com> |
de9dd397 | 3 | * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
273b65be JG |
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 | ||
350ad6c1 | 24 | #define BT_LOG_TAG "LIB/CLOCK-CLASS" |
c2d9d9cf | 25 | #include "lib/logging.h" |
0f5e83e5 | 26 | |
578e048b | 27 | #include "lib/assert-pre.h" |
6162e6b7 | 28 | #include "common/uuid.h" |
3fadfbc0 MJ |
29 | #include <babeltrace2/trace-ir/clock-class-const.h> |
30 | #include <babeltrace2/trace-ir/clock-class.h> | |
578e048b MJ |
31 | #include "clock-class.h" |
32 | #include "clock-snapshot.h" | |
33 | #include "utils.h" | |
34 | #include "compat/compiler.h" | |
3fadfbc0 | 35 | #include <babeltrace2/types.h> |
578e048b | 36 | #include "compat/string.h" |
273b65be | 37 | #include <inttypes.h> |
578e048b MJ |
38 | #include "lib/object.h" |
39 | #include "common/assert.h" | |
d24d5663 | 40 | #include "lib/func-status.h" |
c6962c96 | 41 | #include "lib/value.h" |
5134570b | 42 | |
bdb288b3 PP |
43 | #define BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(_cc) \ |
44 | BT_ASSERT_PRE_DEV_HOT((_cc), "Clock class", ": %!+K", (_cc)) | |
312c056a PP |
45 | |
46 | static | |
44c440bc | 47 | void destroy_clock_class(struct bt_object *obj) |
c06116f3 | 48 | { |
44c440bc | 49 | struct bt_clock_class *clock_class = (void *) obj; |
c06116f3 | 50 | |
44c440bc | 51 | BT_LIB_LOGD("Destroying clock class: %!+K", clock_class); |
c6962c96 | 52 | BT_OBJECT_PUT_REF_AND_RESET(clock_class->user_attributes); |
273b65be | 53 | |
44c440bc PP |
54 | if (clock_class->name.str) { |
55 | g_string_free(clock_class->name.str, TRUE); | |
238b7404 PP |
56 | clock_class->name.str = NULL; |
57 | clock_class->name.value = NULL; | |
5134570b PP |
58 | } |
59 | ||
44c440bc PP |
60 | if (clock_class->description.str) { |
61 | g_string_free(clock_class->description.str, TRUE); | |
238b7404 PP |
62 | clock_class->description.str = NULL; |
63 | clock_class->description.value = NULL; | |
273b65be JG |
64 | } |
65 | ||
605e1019 | 66 | bt_object_pool_finalize(&clock_class->cs_pool); |
44c440bc | 67 | g_free(clock_class); |
4c426c17 JG |
68 | } |
69 | ||
f3534905 | 70 | static |
605e1019 | 71 | void free_clock_snapshot(struct bt_clock_snapshot *clock_snapshot, |
44c440bc | 72 | struct bt_clock_class *clock_class) |
f3534905 | 73 | { |
605e1019 | 74 | bt_clock_snapshot_destroy(clock_snapshot); |
f3534905 PP |
75 | } |
76 | ||
44c440bc PP |
77 | static inline |
78 | void set_base_offset(struct bt_clock_class *clock_class) | |
312c056a | 79 | { |
0f2d58c9 PP |
80 | clock_class->base_offset.overflows = bt_util_get_base_offset_ns( |
81 | clock_class->offset_seconds, clock_class->offset_cycles, | |
82 | clock_class->frequency, &clock_class->base_offset.value_ns); | |
312c056a PP |
83 | } |
84 | ||
7fcdb0a9 | 85 | struct bt_clock_class *bt_clock_class_create(bt_self_component *self_comp) |
4c426c17 JG |
86 | { |
87 | int ret; | |
50842bdc | 88 | struct bt_clock_class *clock_class = NULL; |
4c426c17 | 89 | |
7fcdb0a9 | 90 | BT_ASSERT_PRE_NON_NULL(self_comp, "Self component"); |
44c440bc | 91 | BT_LOGD_STR("Creating default clock class object"); |
f3534905 | 92 | |
50842bdc | 93 | clock_class = g_new0(struct bt_clock_class, 1); |
ac0c6bdd | 94 | if (!clock_class) { |
870631a2 | 95 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one clock class."); |
4c426c17 JG |
96 | goto error; |
97 | } | |
98 | ||
44c440bc | 99 | bt_object_init_shared(&clock_class->base, destroy_clock_class); |
c6962c96 PP |
100 | |
101 | clock_class->user_attributes = bt_value_map_create(); | |
102 | if (!clock_class->user_attributes) { | |
103 | BT_LIB_LOGE_APPEND_CAUSE( | |
104 | "Failed to create a map value object."); | |
105 | goto error; | |
106 | } | |
107 | ||
44c440bc PP |
108 | clock_class->name.str = g_string_new(NULL); |
109 | if (!clock_class->name.str) { | |
870631a2 | 110 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString."); |
44c440bc PP |
111 | goto error; |
112 | } | |
85380e99 | 113 | |
44c440bc PP |
114 | clock_class->description.str = g_string_new(NULL); |
115 | if (!clock_class->description.str) { | |
870631a2 | 116 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate a GString."); |
44c440bc | 117 | goto error; |
273b65be JG |
118 | } |
119 | ||
44c440bc | 120 | clock_class->frequency = UINT64_C(1000000000); |
5552377a | 121 | clock_class->origin_is_unix_epoch = BT_TRUE; |
44c440bc | 122 | set_base_offset(clock_class); |
605e1019 PP |
123 | ret = bt_object_pool_initialize(&clock_class->cs_pool, |
124 | (bt_object_pool_new_object_func) bt_clock_snapshot_new, | |
312c056a | 125 | (bt_object_pool_destroy_object_func) |
605e1019 | 126 | free_clock_snapshot, |
312c056a PP |
127 | clock_class); |
128 | if (ret) { | |
870631a2 PP |
129 | BT_LIB_LOGE_APPEND_CAUSE( |
130 | "Failed to initialize clock snapshot pool: ret=%d", | |
312c056a PP |
131 | ret); |
132 | goto error; | |
133 | } | |
134 | ||
44c440bc PP |
135 | BT_LIB_LOGD("Created clock class object: %!+K", clock_class); |
136 | goto end; | |
137 | ||
273b65be | 138 | error: |
65300d60 | 139 | BT_OBJECT_PUT_REF_AND_RESET(clock_class); |
87d76bb1 JG |
140 | |
141 | end: | |
40f4ba76 | 142 | return clock_class; |
87d76bb1 JG |
143 | } |
144 | ||
40f4ba76 | 145 | const char *bt_clock_class_get_name(const struct bt_clock_class *clock_class) |
87d76bb1 | 146 | { |
bdb288b3 | 147 | BT_ASSERT_PRE_DEV_NON_NULL(clock_class, "Clock class"); |
44c440bc PP |
148 | return clock_class->name.value; |
149 | } | |
87d76bb1 | 150 | |
d24d5663 | 151 | enum bt_clock_class_set_name_status bt_clock_class_set_name( |
8c41fd73 | 152 | struct bt_clock_class *clock_class, const char *name) |
44c440bc PP |
153 | { |
154 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); | |
155 | BT_ASSERT_PRE_NON_NULL(name, "Name"); | |
bdb288b3 | 156 | BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class); |
44c440bc PP |
157 | g_string_assign(clock_class->name.str, name); |
158 | clock_class->name.value = clock_class->name.str->str; | |
3f7d4d90 | 159 | BT_LIB_LOGD("Set clock class's name: %!+K", clock_class); |
d24d5663 | 160 | return BT_FUNC_STATUS_OK; |
44c440bc | 161 | } |
87d76bb1 | 162 | |
40f4ba76 PP |
163 | const char *bt_clock_class_get_description( |
164 | const struct bt_clock_class *clock_class) | |
44c440bc | 165 | { |
bdb288b3 | 166 | BT_ASSERT_PRE_DEV_NON_NULL(clock_class, "Clock class"); |
44c440bc | 167 | return clock_class->description.value; |
273b65be JG |
168 | } |
169 | ||
d24d5663 | 170 | enum bt_clock_class_set_description_status bt_clock_class_set_description( |
8c41fd73 | 171 | struct bt_clock_class *clock_class, const char *descr) |
273b65be | 172 | { |
44c440bc PP |
173 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); |
174 | BT_ASSERT_PRE_NON_NULL(descr, "Description"); | |
bdb288b3 | 175 | BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class); |
44c440bc PP |
176 | g_string_assign(clock_class->description.str, descr); |
177 | clock_class->description.value = clock_class->description.str->str; | |
3f7d4d90 | 178 | BT_LIB_LOGD("Set clock class's description: %!+K", |
44c440bc | 179 | clock_class); |
d24d5663 | 180 | return BT_FUNC_STATUS_OK; |
273b65be JG |
181 | } |
182 | ||
40f4ba76 | 183 | uint64_t bt_clock_class_get_frequency(const struct bt_clock_class *clock_class) |
87d76bb1 | 184 | { |
bdb288b3 | 185 | BT_ASSERT_PRE_DEV_NON_NULL(clock_class, "Clock class"); |
44c440bc | 186 | return clock_class->frequency; |
87d76bb1 JG |
187 | } |
188 | ||
40f4ba76 | 189 | void bt_clock_class_set_frequency(struct bt_clock_class *clock_class, |
44c440bc | 190 | uint64_t frequency) |
273b65be | 191 | { |
44c440bc | 192 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); |
bdb288b3 | 193 | BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class); |
44c440bc PP |
194 | BT_ASSERT_PRE(frequency != UINT64_C(-1) && frequency != 0, |
195 | "Invalid frequency: %![cc-]+K, new-freq=%" PRIu64, | |
196 | clock_class, frequency); | |
197 | BT_ASSERT_PRE(clock_class->offset_cycles < frequency, | |
198 | "Offset (cycles) is greater than clock class's frequency: " | |
199 | "%![cc-]+K, new-freq=%" PRIu64, clock_class, frequency); | |
200 | clock_class->frequency = frequency; | |
201 | set_base_offset(clock_class); | |
3f7d4d90 | 202 | BT_LIB_LOGD("Set clock class's frequency: %!+K", clock_class); |
273b65be JG |
203 | } |
204 | ||
40f4ba76 | 205 | uint64_t bt_clock_class_get_precision(const struct bt_clock_class *clock_class) |
87d76bb1 | 206 | { |
bdb288b3 | 207 | BT_ASSERT_PRE_DEV_NON_NULL(clock_class, "Clock class"); |
44c440bc | 208 | return clock_class->precision; |
87d76bb1 JG |
209 | } |
210 | ||
40f4ba76 | 211 | void bt_clock_class_set_precision(struct bt_clock_class *clock_class, |
ac0c6bdd | 212 | uint64_t precision) |
273b65be | 213 | { |
44c440bc | 214 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); |
bdb288b3 | 215 | BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class); |
44c440bc PP |
216 | BT_ASSERT_PRE(precision != UINT64_C(-1), |
217 | "Invalid precision: %![cc-]+K, new-precision=%" PRIu64, | |
218 | clock_class, precision); | |
ac0c6bdd | 219 | clock_class->precision = precision; |
3f7d4d90 | 220 | BT_LIB_LOGD("Set clock class's precision: %!+K", clock_class); |
273b65be JG |
221 | } |
222 | ||
40f4ba76 | 223 | void bt_clock_class_get_offset(const struct bt_clock_class *clock_class, |
44c440bc | 224 | int64_t *seconds, uint64_t *cycles) |
87d76bb1 | 225 | { |
bdb288b3 PP |
226 | BT_ASSERT_PRE_DEV_NON_NULL(clock_class, "Clock class"); |
227 | BT_ASSERT_PRE_DEV_NON_NULL(seconds, "Seconds (output)"); | |
228 | BT_ASSERT_PRE_DEV_NON_NULL(cycles, "Cycles (output)"); | |
44c440bc PP |
229 | *seconds = clock_class->offset_seconds; |
230 | *cycles = clock_class->offset_cycles; | |
87d76bb1 JG |
231 | } |
232 | ||
40f4ba76 | 233 | void bt_clock_class_set_offset(struct bt_clock_class *clock_class, |
44c440bc | 234 | int64_t seconds, uint64_t cycles) |
273b65be | 235 | { |
44c440bc | 236 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); |
bdb288b3 | 237 | BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class); |
44c440bc PP |
238 | BT_ASSERT_PRE(cycles < clock_class->frequency, |
239 | "Offset (cycles) is greater than clock class's frequency: " | |
240 | "%![cc-]+K, new-offset-cycles=%" PRIu64, clock_class, cycles); | |
241 | clock_class->offset_seconds = seconds; | |
242 | clock_class->offset_cycles = cycles; | |
243 | set_base_offset(clock_class); | |
3f7d4d90 | 244 | BT_LIB_LOGD("Set clock class's offset: %!+K", clock_class); |
273b65be JG |
245 | } |
246 | ||
5552377a | 247 | bt_bool bt_clock_class_origin_is_unix_epoch(const struct bt_clock_class *clock_class) |
87d76bb1 | 248 | { |
bdb288b3 | 249 | BT_ASSERT_PRE_DEV_NON_NULL(clock_class, "Clock class"); |
5552377a | 250 | return (bool) clock_class->origin_is_unix_epoch; |
87d76bb1 JG |
251 | } |
252 | ||
5552377a PP |
253 | void bt_clock_class_set_origin_is_unix_epoch(struct bt_clock_class *clock_class, |
254 | bt_bool origin_is_unix_epoch) | |
273b65be | 255 | { |
44c440bc | 256 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); |
bdb288b3 | 257 | BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class); |
5552377a | 258 | clock_class->origin_is_unix_epoch = (bool) origin_is_unix_epoch; |
3f7d4d90 | 259 | BT_LIB_LOGD("Set clock class's origin is Unix epoch property: %!+K", |
44c440bc | 260 | clock_class); |
273b65be JG |
261 | } |
262 | ||
40f4ba76 | 263 | bt_uuid bt_clock_class_get_uuid(const struct bt_clock_class *clock_class) |
85b743f4 | 264 | { |
bdb288b3 | 265 | BT_ASSERT_PRE_DEV_NON_NULL(clock_class, "Clock class"); |
44c440bc | 266 | return clock_class->uuid.value; |
85b743f4 JG |
267 | } |
268 | ||
40f4ba76 | 269 | void bt_clock_class_set_uuid(struct bt_clock_class *clock_class, |
44c440bc | 270 | bt_uuid uuid) |
85b743f4 | 271 | { |
44c440bc PP |
272 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); |
273 | BT_ASSERT_PRE_NON_NULL(uuid, "UUID"); | |
bdb288b3 | 274 | BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class); |
6162e6b7 | 275 | bt_uuid_copy(clock_class->uuid.uuid, uuid); |
44c440bc | 276 | clock_class->uuid.value = clock_class->uuid.uuid; |
3f7d4d90 | 277 | BT_LIB_LOGD("Set clock class's UUID: %!+K", clock_class); |
312c056a PP |
278 | } |
279 | ||
280 | BT_HIDDEN | |
40f4ba76 | 281 | void _bt_clock_class_freeze(const struct bt_clock_class *clock_class) |
312c056a | 282 | { |
312c056a | 283 | BT_ASSERT(clock_class); |
6f57e458 | 284 | |
44c440bc PP |
285 | if (clock_class->frozen) { |
286 | return; | |
93dda901 PP |
287 | } |
288 | ||
c6962c96 PP |
289 | BT_LIB_LOGD("Freezing clock class's user attributes: %!+v", |
290 | clock_class->user_attributes); | |
291 | bt_value_freeze(clock_class->user_attributes); | |
44c440bc | 292 | BT_LIB_LOGD("Freezing clock class: %!+K", clock_class); |
40f4ba76 | 293 | ((struct bt_clock_class *) clock_class)->frozen = 1; |
93dda901 | 294 | } |
312c056a | 295 | |
d24d5663 PP |
296 | enum bt_clock_class_cycles_to_ns_from_origin_status |
297 | bt_clock_class_cycles_to_ns_from_origin( | |
40f4ba76 | 298 | const struct bt_clock_class *clock_class, |
312c056a PP |
299 | uint64_t cycles, int64_t *ns) |
300 | { | |
301 | int ret; | |
312c056a | 302 | |
bdb288b3 PP |
303 | BT_ASSERT_PRE_DEV_NON_NULL(clock_class, "Clock class"); |
304 | BT_ASSERT_PRE_DEV_NON_NULL(ns, "Nanoseconds (output)"); | |
0f2d58c9 | 305 | ret = bt_util_ns_from_origin_clock_class(clock_class, cycles, ns); |
312c056a | 306 | if (ret) { |
bc3d9692 | 307 | BT_LIB_LOGE_APPEND_CAUSE("Cannot convert cycles to nanoseconds " |
44c440bc PP |
308 | "from origin for given clock class: " |
309 | "value overflows the signed 64-bit integer range: " | |
310 | "%![cc-]+K, cycles=%" PRIu64, | |
311 | clock_class, cycles); | |
520cdc82 | 312 | ret = BT_FUNC_STATUS_OVERFLOW_ERROR; |
312c056a PP |
313 | } |
314 | ||
312c056a PP |
315 | return ret; |
316 | } | |
c5b9b441 | 317 | |
c6962c96 PP |
318 | const struct bt_value *bt_clock_class_borrow_user_attributes_const( |
319 | const struct bt_clock_class *clock_class) | |
320 | { | |
321 | BT_ASSERT_PRE_DEV_NON_NULL(clock_class, "Clock class"); | |
322 | return clock_class->user_attributes; | |
323 | } | |
324 | ||
325 | struct bt_value *bt_clock_class_borrow_user_attributes( | |
326 | struct bt_clock_class *clock_class) | |
327 | { | |
328 | return (void *) bt_clock_class_borrow_user_attributes_const( | |
329 | (void *) clock_class); | |
330 | } | |
331 | ||
332 | void bt_clock_class_set_user_attributes( | |
333 | struct bt_clock_class *clock_class, | |
334 | const struct bt_value *user_attributes) | |
335 | { | |
336 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); | |
337 | BT_ASSERT_PRE_NON_NULL(user_attributes, "User attributes"); | |
338 | BT_ASSERT_PRE(user_attributes->type == BT_VALUE_TYPE_MAP, | |
339 | "User attributes object is not a map value object."); | |
340 | BT_ASSERT_PRE_DEV_CLOCK_CLASS_HOT(clock_class); | |
6871026b | 341 | bt_object_put_ref_no_null_check(clock_class->user_attributes); |
c6962c96 | 342 | clock_class->user_attributes = (void *) user_attributes; |
6871026b | 343 | bt_object_get_ref_no_null_check(clock_class->user_attributes); |
c6962c96 PP |
344 | } |
345 | ||
c5b9b441 PP |
346 | void bt_clock_class_get_ref(const struct bt_clock_class *clock_class) |
347 | { | |
348 | bt_object_get_ref(clock_class); | |
349 | } | |
350 | ||
351 | void bt_clock_class_put_ref(const struct bt_clock_class *clock_class) | |
352 | { | |
353 | bt_object_put_ref(clock_class); | |
354 | } |