Commit | Line | Data |
---|---|---|
273b65be | 1 | /* |
f2b0325d | 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 | ||
0f5e83e5 | 24 | #define BT_LOG_TAG "CLOCK-CLASS" |
40547d22 | 25 | #include <babeltrace/lib-logging-internal.h> |
0f5e83e5 | 26 | |
a6918753 | 27 | #include <babeltrace/assert-pre-internal.h> |
75c3fca1 | 28 | #include <babeltrace/compat/uuid-internal.h> |
108b91d0 PP |
29 | #include <babeltrace/trace-ir/clock-class-internal.h> |
30 | #include <babeltrace/trace-ir/clock-value-internal.h> | |
31 | #include <babeltrace/trace-ir/utils-internal.h> | |
3d9990ac | 32 | #include <babeltrace/compiler-internal.h> |
c55a9f58 | 33 | #include <babeltrace/types.h> |
ee389f01 | 34 | #include <babeltrace/compat/string-internal.h> |
273b65be | 35 | #include <inttypes.h> |
0f5e83e5 | 36 | #include <babeltrace/object-internal.h> |
8b45963b | 37 | #include <babeltrace/assert-internal.h> |
5134570b | 38 | |
7b33a0e0 PP |
39 | #define BT_ASSERT_PRE_CLOCK_CLASS_HOT(_cc) \ |
40 | BT_ASSERT_PRE_HOT((_cc), "Clock class", ": %!+K", (_cc)) | |
a6918753 PP |
41 | |
42 | static | |
7b33a0e0 | 43 | void destroy_clock_class(struct bt_object *obj) |
c06116f3 | 44 | { |
7b33a0e0 | 45 | struct bt_clock_class *clock_class = (void *) obj; |
c06116f3 | 46 | |
7b33a0e0 | 47 | BT_LIB_LOGD("Destroying clock class: %!+K", clock_class); |
273b65be | 48 | |
7b33a0e0 PP |
49 | if (clock_class->name.str) { |
50 | g_string_free(clock_class->name.str, TRUE); | |
1248f5ea PP |
51 | clock_class->name.str = NULL; |
52 | clock_class->name.value = NULL; | |
5134570b PP |
53 | } |
54 | ||
7b33a0e0 PP |
55 | if (clock_class->description.str) { |
56 | g_string_free(clock_class->description.str, TRUE); | |
1248f5ea PP |
57 | clock_class->description.str = NULL; |
58 | clock_class->description.value = NULL; | |
273b65be JG |
59 | } |
60 | ||
7b33a0e0 PP |
61 | bt_object_pool_finalize(&clock_class->cv_pool); |
62 | g_free(clock_class); | |
4c426c17 JG |
63 | } |
64 | ||
15260cc8 | 65 | static |
7b33a0e0 PP |
66 | void free_clock_value(struct bt_clock_value *clock_value, |
67 | struct bt_clock_class *clock_class) | |
15260cc8 | 68 | { |
7b33a0e0 | 69 | bt_clock_value_destroy(clock_value); |
15260cc8 PP |
70 | } |
71 | ||
7b33a0e0 PP |
72 | static inline |
73 | void set_base_offset(struct bt_clock_class *clock_class) | |
a6918753 | 74 | { |
7b33a0e0 PP |
75 | uint64_t offset_cycles_ns; |
76 | ||
77 | /* Initialize nanosecond timestamp to clock's offset in seconds */ | |
78 | if (clock_class->offset_seconds <= (INT64_MIN / INT64_C(1000000000) - 1) || | |
79 | clock_class->offset_seconds >= (INT64_MAX / INT64_C(1000000000)) - 1) { | |
80 | /* | |
81 | * Overflow: offset in seconds converted to nanoseconds | |
82 | * is outside the int64_t range. We also subtract 1 here | |
83 | * to leave "space" for the offset in cycles converted | |
84 | * to nanoseconds (which is always less than 1 second by | |
85 | * contract). | |
86 | */ | |
87 | clock_class->base_offset.overflows = true; | |
88 | goto end; | |
89 | } | |
90 | ||
91 | /* Offset (seconds) to nanoseconds */ | |
92 | clock_class->base_offset.value_ns = clock_class->offset_seconds * | |
93 | INT64_C(1000000000); | |
94 | ||
95 | /* Add offset in cycles */ | |
96 | BT_ASSERT(clock_class->offset_cycles < clock_class->frequency); | |
97 | offset_cycles_ns = bt_util_ns_from_value(clock_class->frequency, | |
98 | clock_class->offset_cycles); | |
99 | BT_ASSERT(offset_cycles_ns < 1000000000); | |
100 | clock_class->base_offset.value_ns += (int64_t) offset_cycles_ns; | |
101 | clock_class->base_offset.overflows = false; | |
102 | ||
103 | end: | |
104 | return; | |
a6918753 PP |
105 | } |
106 | ||
78cf9df6 | 107 | struct bt_clock_class *bt_clock_class_create(void) |
4c426c17 JG |
108 | { |
109 | int ret; | |
839d52a5 | 110 | struct bt_clock_class *clock_class = NULL; |
4c426c17 | 111 | |
7b33a0e0 | 112 | BT_LOGD_STR("Creating default clock class object"); |
15260cc8 | 113 | |
839d52a5 | 114 | clock_class = g_new0(struct bt_clock_class, 1); |
ac0c6bdd | 115 | if (!clock_class) { |
5134570b | 116 | BT_LOGE_STR("Failed to allocate one clock class."); |
4c426c17 JG |
117 | goto error; |
118 | } | |
119 | ||
7b33a0e0 PP |
120 | bt_object_init_shared(&clock_class->base, destroy_clock_class); |
121 | clock_class->name.str = g_string_new(NULL); | |
122 | if (!clock_class->name.str) { | |
123 | BT_LOGE_STR("Failed to allocate a GString."); | |
124 | goto error; | |
125 | } | |
85380e99 | 126 | |
7b33a0e0 PP |
127 | clock_class->description.str = g_string_new(NULL); |
128 | if (!clock_class->description.str) { | |
129 | BT_LOGE_STR("Failed to allocate a GString."); | |
130 | goto error; | |
273b65be JG |
131 | } |
132 | ||
7b33a0e0 PP |
133 | clock_class->frequency = UINT64_C(1000000000); |
134 | clock_class->is_absolute = BT_TRUE; | |
135 | set_base_offset(clock_class); | |
a6918753 PP |
136 | ret = bt_object_pool_initialize(&clock_class->cv_pool, |
137 | (bt_object_pool_new_object_func) bt_clock_value_new, | |
138 | (bt_object_pool_destroy_object_func) | |
7b33a0e0 | 139 | free_clock_value, |
a6918753 PP |
140 | clock_class); |
141 | if (ret) { | |
142 | BT_LOGE("Failed to initialize clock value pool: ret=%d", | |
143 | ret); | |
144 | goto error; | |
145 | } | |
146 | ||
7b33a0e0 PP |
147 | BT_LIB_LOGD("Created clock class object: %!+K", clock_class); |
148 | goto end; | |
149 | ||
273b65be | 150 | error: |
8138bfe1 | 151 | BT_OBJECT_PUT_REF_AND_RESET(clock_class); |
87d76bb1 JG |
152 | |
153 | end: | |
78cf9df6 | 154 | return clock_class; |
87d76bb1 JG |
155 | } |
156 | ||
78cf9df6 | 157 | const char *bt_clock_class_get_name(const struct bt_clock_class *clock_class) |
87d76bb1 | 158 | { |
7b33a0e0 PP |
159 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); |
160 | return clock_class->name.value; | |
161 | } | |
87d76bb1 | 162 | |
78cf9df6 | 163 | int bt_clock_class_set_name(struct bt_clock_class *clock_class, |
7b33a0e0 PP |
164 | const char *name) |
165 | { | |
166 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); | |
167 | BT_ASSERT_PRE_NON_NULL(name, "Name"); | |
168 | BT_ASSERT_PRE_CLOCK_CLASS_HOT(clock_class); | |
169 | g_string_assign(clock_class->name.str, name); | |
170 | clock_class->name.value = clock_class->name.str->str; | |
171 | BT_LIB_LOGV("Set clock class's name: %!+K", clock_class); | |
172 | return 0; | |
173 | } | |
87d76bb1 | 174 | |
78cf9df6 PP |
175 | const char *bt_clock_class_get_description( |
176 | const struct bt_clock_class *clock_class) | |
7b33a0e0 PP |
177 | { |
178 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); | |
179 | return clock_class->description.value; | |
273b65be JG |
180 | } |
181 | ||
78cf9df6 | 182 | int bt_clock_class_set_description(struct bt_clock_class *clock_class, |
7b33a0e0 | 183 | const char *descr) |
273b65be | 184 | { |
7b33a0e0 PP |
185 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); |
186 | BT_ASSERT_PRE_NON_NULL(descr, "Description"); | |
187 | BT_ASSERT_PRE_CLOCK_CLASS_HOT(clock_class); | |
188 | g_string_assign(clock_class->description.str, descr); | |
189 | clock_class->description.value = clock_class->description.str->str; | |
190 | BT_LIB_LOGV("Set clock class's description: %!+K", | |
191 | clock_class); | |
192 | return 0; | |
273b65be JG |
193 | } |
194 | ||
78cf9df6 | 195 | uint64_t bt_clock_class_get_frequency(const struct bt_clock_class *clock_class) |
87d76bb1 | 196 | { |
7b33a0e0 PP |
197 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); |
198 | return clock_class->frequency; | |
87d76bb1 JG |
199 | } |
200 | ||
78cf9df6 | 201 | void bt_clock_class_set_frequency(struct bt_clock_class *clock_class, |
7b33a0e0 | 202 | uint64_t frequency) |
273b65be | 203 | { |
7b33a0e0 PP |
204 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); |
205 | BT_ASSERT_PRE_CLOCK_CLASS_HOT(clock_class); | |
206 | BT_ASSERT_PRE(frequency != UINT64_C(-1) && frequency != 0, | |
207 | "Invalid frequency: %![cc-]+K, new-freq=%" PRIu64, | |
208 | clock_class, frequency); | |
209 | BT_ASSERT_PRE(clock_class->offset_cycles < frequency, | |
210 | "Offset (cycles) is greater than clock class's frequency: " | |
211 | "%![cc-]+K, new-freq=%" PRIu64, clock_class, frequency); | |
212 | clock_class->frequency = frequency; | |
213 | set_base_offset(clock_class); | |
214 | BT_LIB_LOGV("Set clock class's frequency: %!+K", clock_class); | |
273b65be JG |
215 | } |
216 | ||
78cf9df6 | 217 | uint64_t bt_clock_class_get_precision(const struct bt_clock_class *clock_class) |
87d76bb1 | 218 | { |
7b33a0e0 PP |
219 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); |
220 | return clock_class->precision; | |
87d76bb1 JG |
221 | } |
222 | ||
78cf9df6 | 223 | void bt_clock_class_set_precision(struct bt_clock_class *clock_class, |
ac0c6bdd | 224 | uint64_t precision) |
273b65be | 225 | { |
7b33a0e0 PP |
226 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); |
227 | BT_ASSERT_PRE_CLOCK_CLASS_HOT(clock_class); | |
228 | BT_ASSERT_PRE(precision != UINT64_C(-1), | |
229 | "Invalid precision: %![cc-]+K, new-precision=%" PRIu64, | |
230 | clock_class, precision); | |
ac0c6bdd | 231 | clock_class->precision = precision; |
7b33a0e0 | 232 | BT_LIB_LOGV("Set clock class's precision: %!+K", clock_class); |
273b65be JG |
233 | } |
234 | ||
78cf9df6 | 235 | void bt_clock_class_get_offset(const struct bt_clock_class *clock_class, |
7b33a0e0 | 236 | int64_t *seconds, uint64_t *cycles) |
87d76bb1 | 237 | { |
7b33a0e0 PP |
238 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); |
239 | BT_ASSERT_PRE_NON_NULL(seconds, "Seconds (output)"); | |
240 | BT_ASSERT_PRE_NON_NULL(cycles, "Cycles (output)"); | |
241 | *seconds = clock_class->offset_seconds; | |
242 | *cycles = clock_class->offset_cycles; | |
87d76bb1 JG |
243 | } |
244 | ||
78cf9df6 | 245 | void bt_clock_class_set_offset(struct bt_clock_class *clock_class, |
7b33a0e0 | 246 | int64_t seconds, uint64_t cycles) |
273b65be | 247 | { |
7b33a0e0 PP |
248 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); |
249 | BT_ASSERT_PRE_CLOCK_CLASS_HOT(clock_class); | |
250 | BT_ASSERT_PRE(cycles < clock_class->frequency, | |
251 | "Offset (cycles) is greater than clock class's frequency: " | |
252 | "%![cc-]+K, new-offset-cycles=%" PRIu64, clock_class, cycles); | |
253 | clock_class->offset_seconds = seconds; | |
254 | clock_class->offset_cycles = cycles; | |
255 | set_base_offset(clock_class); | |
256 | BT_LIB_LOGV("Set clock class's offset: %!+K", clock_class); | |
273b65be JG |
257 | } |
258 | ||
78cf9df6 | 259 | bt_bool bt_clock_class_is_absolute(const struct bt_clock_class *clock_class) |
87d76bb1 | 260 | { |
7b33a0e0 PP |
261 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); |
262 | return (bool) clock_class->is_absolute; | |
87d76bb1 JG |
263 | } |
264 | ||
78cf9df6 | 265 | void bt_clock_class_set_is_absolute(struct bt_clock_class *clock_class, |
a2b94977 | 266 | bt_bool is_absolute) |
273b65be | 267 | { |
7b33a0e0 PP |
268 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); |
269 | BT_ASSERT_PRE_CLOCK_CLASS_HOT(clock_class); | |
270 | clock_class->is_absolute = (bool) is_absolute; | |
271 | BT_LIB_LOGV("Set clock class's absolute property: %!+K", | |
272 | clock_class); | |
273b65be JG |
273 | } |
274 | ||
78cf9df6 | 275 | bt_uuid bt_clock_class_get_uuid(const struct bt_clock_class *clock_class) |
85b743f4 | 276 | { |
7b33a0e0 PP |
277 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); |
278 | return clock_class->uuid.value; | |
85b743f4 JG |
279 | } |
280 | ||
78cf9df6 | 281 | void bt_clock_class_set_uuid(struct bt_clock_class *clock_class, |
7b33a0e0 | 282 | bt_uuid uuid) |
85b743f4 | 283 | { |
7b33a0e0 PP |
284 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); |
285 | BT_ASSERT_PRE_NON_NULL(uuid, "UUID"); | |
286 | BT_ASSERT_PRE_CLOCK_CLASS_HOT(clock_class); | |
287 | memcpy(clock_class->uuid.uuid, uuid, BABELTRACE_UUID_LEN); | |
288 | clock_class->uuid.value = clock_class->uuid.uuid; | |
289 | BT_LIB_LOGV("Set clock class's UUID: %!+K", clock_class); | |
a6918753 PP |
290 | } |
291 | ||
292 | BT_HIDDEN | |
78cf9df6 | 293 | void _bt_clock_class_freeze(const struct bt_clock_class *clock_class) |
a6918753 | 294 | { |
a6918753 | 295 | BT_ASSERT(clock_class); |
6f57e458 | 296 | |
7b33a0e0 PP |
297 | if (clock_class->frozen) { |
298 | return; | |
db3347ac PP |
299 | } |
300 | ||
7b33a0e0 | 301 | BT_LIB_LOGD("Freezing clock class: %!+K", clock_class); |
78cf9df6 | 302 | ((struct bt_clock_class *) clock_class)->frozen = 1; |
db3347ac | 303 | } |
a6918753 | 304 | |
78cf9df6 PP |
305 | int bt_clock_class_cycles_to_ns_from_origin( |
306 | const struct bt_clock_class *clock_class, | |
a6918753 PP |
307 | uint64_t cycles, int64_t *ns) |
308 | { | |
309 | int ret; | |
a6918753 PP |
310 | |
311 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); | |
7b33a0e0 PP |
312 | BT_ASSERT_PRE_NON_NULL(ns, "Nanoseconds (output)"); |
313 | ret = bt_util_ns_from_origin(clock_class, cycles, ns); | |
a6918753 | 314 | if (ret) { |
7b33a0e0 PP |
315 | BT_LIB_LOGW("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); | |
a6918753 PP |
320 | } |
321 | ||
a6918753 PP |
322 | return ret; |
323 | } | |
8c6884d9 PP |
324 | |
325 | void bt_clock_class_get_ref(const struct bt_clock_class *clock_class) | |
326 | { | |
327 | bt_object_get_ref(clock_class); | |
328 | } | |
329 | ||
330 | void bt_clock_class_put_ref(const struct bt_clock_class *clock_class) | |
331 | { | |
332 | bt_object_put_ref(clock_class); | |
333 | } |