Commit | Line | Data |
---|---|---|
273b65be | 1 | /* |
de9dd397 | 2 | * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
273b65be JG |
3 | * |
4 | * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
5 | * | |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
22 | * SOFTWARE. | |
23 | */ | |
24 | ||
0f5e83e5 | 25 | #define BT_LOG_TAG "CLOCK-CLASS" |
40547d22 | 26 | #include <babeltrace/lib-logging-internal.h> |
0f5e83e5 | 27 | |
312c056a | 28 | #include <babeltrace/assert-pre-internal.h> |
75c3fca1 | 29 | #include <babeltrace/compat/uuid-internal.h> |
577dc45a PP |
30 | #include <babeltrace/trace-ir/clock-class-internal.h> |
31 | #include <babeltrace/trace-ir/clock-value-internal.h> | |
32 | #include <babeltrace/trace-ir/utils-internal.h> | |
e2317362 | 33 | #include <babeltrace/object.h> |
3d9990ac | 34 | #include <babeltrace/compiler-internal.h> |
c55a9f58 | 35 | #include <babeltrace/types.h> |
ee389f01 | 36 | #include <babeltrace/compat/string-internal.h> |
273b65be | 37 | #include <inttypes.h> |
0f5e83e5 | 38 | #include <babeltrace/object-internal.h> |
f6ccaed9 | 39 | #include <babeltrace/assert-internal.h> |
5134570b | 40 | |
61f59358 PP |
41 | #define BT_ASSERT_PRE_CLOCK_CLASS_HOT(_cc) \ |
42 | BT_ASSERT_PRE_HOT((_cc), "Clock class", ": %!+K", (_cc)) | |
312c056a PP |
43 | |
44 | static | |
61f59358 | 45 | void destroy_clock_class(struct bt_object *obj) |
c06116f3 | 46 | { |
61f59358 | 47 | struct bt_clock_class *clock_class = (void *) obj; |
c06116f3 | 48 | |
61f59358 | 49 | BT_LIB_LOGD("Destroying clock class: %!+K", clock_class); |
273b65be | 50 | |
61f59358 PP |
51 | if (clock_class->name.str) { |
52 | g_string_free(clock_class->name.str, TRUE); | |
53b97e38 PP |
53 | clock_class->name.str = NULL; |
54 | clock_class->name.value = NULL; | |
5134570b PP |
55 | } |
56 | ||
61f59358 PP |
57 | if (clock_class->description.str) { |
58 | g_string_free(clock_class->description.str, TRUE); | |
53b97e38 PP |
59 | clock_class->description.str = NULL; |
60 | clock_class->description.value = NULL; | |
273b65be JG |
61 | } |
62 | ||
61f59358 PP |
63 | bt_object_pool_finalize(&clock_class->cv_pool); |
64 | g_free(clock_class); | |
4c426c17 JG |
65 | } |
66 | ||
f3534905 | 67 | static |
61f59358 PP |
68 | void free_clock_value(struct bt_clock_value *clock_value, |
69 | struct bt_clock_class *clock_class) | |
f3534905 | 70 | { |
61f59358 | 71 | bt_clock_value_destroy(clock_value); |
f3534905 PP |
72 | } |
73 | ||
61f59358 PP |
74 | static inline |
75 | void set_base_offset(struct bt_clock_class *clock_class) | |
312c056a | 76 | { |
61f59358 PP |
77 | uint64_t offset_cycles_ns; |
78 | ||
79 | /* Initialize nanosecond timestamp to clock's offset in seconds */ | |
80 | if (clock_class->offset_seconds <= (INT64_MIN / INT64_C(1000000000) - 1) || | |
81 | clock_class->offset_seconds >= (INT64_MAX / INT64_C(1000000000)) - 1) { | |
82 | /* | |
83 | * Overflow: offset in seconds converted to nanoseconds | |
84 | * is outside the int64_t range. We also subtract 1 here | |
85 | * to leave "space" for the offset in cycles converted | |
86 | * to nanoseconds (which is always less than 1 second by | |
87 | * contract). | |
88 | */ | |
89 | clock_class->base_offset.overflows = true; | |
90 | goto end; | |
91 | } | |
92 | ||
93 | /* Offset (seconds) to nanoseconds */ | |
94 | clock_class->base_offset.value_ns = clock_class->offset_seconds * | |
95 | INT64_C(1000000000); | |
96 | ||
97 | /* Add offset in cycles */ | |
98 | BT_ASSERT(clock_class->offset_cycles < clock_class->frequency); | |
99 | offset_cycles_ns = bt_util_ns_from_value(clock_class->frequency, | |
100 | clock_class->offset_cycles); | |
101 | BT_ASSERT(offset_cycles_ns < 1000000000); | |
102 | clock_class->base_offset.value_ns += (int64_t) offset_cycles_ns; | |
103 | clock_class->base_offset.overflows = false; | |
104 | ||
105 | end: | |
106 | return; | |
312c056a PP |
107 | } |
108 | ||
351946b4 | 109 | struct bt_private_clock_class *bt_private_clock_class_create(void) |
4c426c17 JG |
110 | { |
111 | int ret; | |
50842bdc | 112 | struct bt_clock_class *clock_class = NULL; |
4c426c17 | 113 | |
61f59358 | 114 | BT_LOGD_STR("Creating default clock class object"); |
f3534905 | 115 | |
50842bdc | 116 | clock_class = g_new0(struct bt_clock_class, 1); |
ac0c6bdd | 117 | if (!clock_class) { |
5134570b | 118 | BT_LOGE_STR("Failed to allocate one clock class."); |
4c426c17 JG |
119 | goto error; |
120 | } | |
121 | ||
61f59358 PP |
122 | bt_object_init_shared(&clock_class->base, destroy_clock_class); |
123 | clock_class->name.str = g_string_new(NULL); | |
124 | if (!clock_class->name.str) { | |
125 | BT_LOGE_STR("Failed to allocate a GString."); | |
126 | goto error; | |
127 | } | |
85380e99 | 128 | |
61f59358 PP |
129 | clock_class->description.str = g_string_new(NULL); |
130 | if (!clock_class->description.str) { | |
131 | BT_LOGE_STR("Failed to allocate a GString."); | |
132 | goto error; | |
273b65be JG |
133 | } |
134 | ||
61f59358 PP |
135 | clock_class->frequency = UINT64_C(1000000000); |
136 | clock_class->is_absolute = BT_TRUE; | |
137 | set_base_offset(clock_class); | |
312c056a PP |
138 | ret = bt_object_pool_initialize(&clock_class->cv_pool, |
139 | (bt_object_pool_new_object_func) bt_clock_value_new, | |
140 | (bt_object_pool_destroy_object_func) | |
61f59358 | 141 | free_clock_value, |
312c056a PP |
142 | clock_class); |
143 | if (ret) { | |
144 | BT_LOGE("Failed to initialize clock value pool: ret=%d", | |
145 | ret); | |
146 | goto error; | |
147 | } | |
148 | ||
61f59358 PP |
149 | BT_LIB_LOGD("Created clock class object: %!+K", clock_class); |
150 | goto end; | |
151 | ||
273b65be | 152 | error: |
e2317362 | 153 | BT_OBJECT_PUT_REF_AND_RESET(clock_class); |
87d76bb1 JG |
154 | |
155 | end: | |
351946b4 | 156 | return (void *) clock_class; |
87d76bb1 JG |
157 | } |
158 | ||
61f59358 | 159 | const char *bt_clock_class_get_name( |
50842bdc | 160 | struct bt_clock_class *clock_class) |
87d76bb1 | 161 | { |
61f59358 PP |
162 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); |
163 | return clock_class->name.value; | |
164 | } | |
87d76bb1 | 165 | |
351946b4 PP |
166 | int bt_private_clock_class_set_name( |
167 | struct bt_private_clock_class *priv_clock_class, | |
61f59358 PP |
168 | const char *name) |
169 | { | |
351946b4 PP |
170 | struct bt_clock_class *clock_class = (void *) priv_clock_class; |
171 | ||
61f59358 PP |
172 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); |
173 | BT_ASSERT_PRE_NON_NULL(name, "Name"); | |
174 | BT_ASSERT_PRE_CLOCK_CLASS_HOT(clock_class); | |
175 | g_string_assign(clock_class->name.str, name); | |
176 | clock_class->name.value = clock_class->name.str->str; | |
177 | BT_LIB_LOGV("Set clock class's name: %!+K", clock_class); | |
178 | return 0; | |
179 | } | |
87d76bb1 | 180 | |
61f59358 PP |
181 | const char *bt_clock_class_get_description(struct bt_clock_class *clock_class) |
182 | { | |
183 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); | |
184 | return clock_class->description.value; | |
273b65be JG |
185 | } |
186 | ||
351946b4 PP |
187 | int bt_private_clock_class_set_description( |
188 | struct bt_private_clock_class *priv_clock_class, | |
61f59358 | 189 | const char *descr) |
273b65be | 190 | { |
351946b4 PP |
191 | struct bt_clock_class *clock_class = (void *) priv_clock_class; |
192 | ||
61f59358 PP |
193 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); |
194 | BT_ASSERT_PRE_NON_NULL(descr, "Description"); | |
195 | BT_ASSERT_PRE_CLOCK_CLASS_HOT(clock_class); | |
196 | g_string_assign(clock_class->description.str, descr); | |
197 | clock_class->description.value = clock_class->description.str->str; | |
198 | BT_LIB_LOGV("Set clock class's description: %!+K", | |
199 | clock_class); | |
200 | return 0; | |
273b65be JG |
201 | } |
202 | ||
61f59358 | 203 | uint64_t bt_clock_class_get_frequency(struct bt_clock_class *clock_class) |
87d76bb1 | 204 | { |
61f59358 PP |
205 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); |
206 | return clock_class->frequency; | |
87d76bb1 JG |
207 | } |
208 | ||
c9987c4e | 209 | void bt_private_clock_class_set_frequency( |
351946b4 | 210 | struct bt_private_clock_class *priv_clock_class, |
61f59358 | 211 | uint64_t frequency) |
273b65be | 212 | { |
351946b4 PP |
213 | struct bt_clock_class *clock_class = (void *) priv_clock_class; |
214 | ||
61f59358 PP |
215 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); |
216 | BT_ASSERT_PRE_CLOCK_CLASS_HOT(clock_class); | |
217 | BT_ASSERT_PRE(frequency != UINT64_C(-1) && frequency != 0, | |
218 | "Invalid frequency: %![cc-]+K, new-freq=%" PRIu64, | |
219 | clock_class, frequency); | |
220 | BT_ASSERT_PRE(clock_class->offset_cycles < frequency, | |
221 | "Offset (cycles) is greater than clock class's frequency: " | |
222 | "%![cc-]+K, new-freq=%" PRIu64, clock_class, frequency); | |
223 | clock_class->frequency = frequency; | |
224 | set_base_offset(clock_class); | |
225 | BT_LIB_LOGV("Set clock class's frequency: %!+K", clock_class); | |
273b65be JG |
226 | } |
227 | ||
50842bdc | 228 | uint64_t bt_clock_class_get_precision(struct bt_clock_class *clock_class) |
87d76bb1 | 229 | { |
61f59358 PP |
230 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); |
231 | return clock_class->precision; | |
87d76bb1 JG |
232 | } |
233 | ||
c9987c4e | 234 | void bt_private_clock_class_set_precision( |
351946b4 | 235 | struct bt_private_clock_class *priv_clock_class, |
ac0c6bdd | 236 | uint64_t precision) |
273b65be | 237 | { |
351946b4 PP |
238 | struct bt_clock_class *clock_class = (void *) priv_clock_class; |
239 | ||
61f59358 PP |
240 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); |
241 | BT_ASSERT_PRE_CLOCK_CLASS_HOT(clock_class); | |
242 | BT_ASSERT_PRE(precision != UINT64_C(-1), | |
243 | "Invalid precision: %![cc-]+K, new-precision=%" PRIu64, | |
244 | clock_class, precision); | |
ac0c6bdd | 245 | clock_class->precision = precision; |
61f59358 | 246 | BT_LIB_LOGV("Set clock class's precision: %!+K", clock_class); |
273b65be JG |
247 | } |
248 | ||
61f59358 PP |
249 | void bt_clock_class_get_offset(struct bt_clock_class *clock_class, |
250 | int64_t *seconds, uint64_t *cycles) | |
87d76bb1 | 251 | { |
61f59358 PP |
252 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); |
253 | BT_ASSERT_PRE_NON_NULL(seconds, "Seconds (output)"); | |
254 | BT_ASSERT_PRE_NON_NULL(cycles, "Cycles (output)"); | |
255 | *seconds = clock_class->offset_seconds; | |
256 | *cycles = clock_class->offset_cycles; | |
87d76bb1 JG |
257 | } |
258 | ||
c9987c4e | 259 | void bt_private_clock_class_set_offset( |
351946b4 | 260 | struct bt_private_clock_class *priv_clock_class, |
61f59358 | 261 | int64_t seconds, uint64_t cycles) |
273b65be | 262 | { |
351946b4 PP |
263 | struct bt_clock_class *clock_class = (void *) priv_clock_class; |
264 | ||
61f59358 PP |
265 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); |
266 | BT_ASSERT_PRE_CLOCK_CLASS_HOT(clock_class); | |
267 | BT_ASSERT_PRE(cycles < clock_class->frequency, | |
268 | "Offset (cycles) is greater than clock class's frequency: " | |
269 | "%![cc-]+K, new-offset-cycles=%" PRIu64, clock_class, cycles); | |
270 | clock_class->offset_seconds = seconds; | |
271 | clock_class->offset_cycles = cycles; | |
272 | set_base_offset(clock_class); | |
273 | BT_LIB_LOGV("Set clock class's offset: %!+K", clock_class); | |
273b65be JG |
274 | } |
275 | ||
50842bdc | 276 | bt_bool bt_clock_class_is_absolute(struct bt_clock_class *clock_class) |
87d76bb1 | 277 | { |
61f59358 PP |
278 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); |
279 | return (bool) clock_class->is_absolute; | |
87d76bb1 JG |
280 | } |
281 | ||
c9987c4e | 282 | void bt_private_clock_class_set_is_absolute( |
351946b4 | 283 | struct bt_private_clock_class *priv_clock_class, |
a2b94977 | 284 | bt_bool is_absolute) |
273b65be | 285 | { |
351946b4 PP |
286 | struct bt_clock_class *clock_class = (void *) priv_clock_class; |
287 | ||
61f59358 PP |
288 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); |
289 | BT_ASSERT_PRE_CLOCK_CLASS_HOT(clock_class); | |
290 | clock_class->is_absolute = (bool) is_absolute; | |
291 | BT_LIB_LOGV("Set clock class's absolute property: %!+K", | |
292 | clock_class); | |
273b65be JG |
293 | } |
294 | ||
61f59358 | 295 | bt_uuid bt_clock_class_get_uuid(struct bt_clock_class *clock_class) |
85b743f4 | 296 | { |
61f59358 PP |
297 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); |
298 | return clock_class->uuid.value; | |
85b743f4 JG |
299 | } |
300 | ||
c9987c4e | 301 | void bt_private_clock_class_set_uuid( |
351946b4 | 302 | struct bt_private_clock_class *priv_clock_class, |
61f59358 | 303 | bt_uuid uuid) |
85b743f4 | 304 | { |
351946b4 PP |
305 | struct bt_clock_class *clock_class = (void *) priv_clock_class; |
306 | ||
61f59358 PP |
307 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); |
308 | BT_ASSERT_PRE_NON_NULL(uuid, "UUID"); | |
309 | BT_ASSERT_PRE_CLOCK_CLASS_HOT(clock_class); | |
310 | memcpy(clock_class->uuid.uuid, uuid, BABELTRACE_UUID_LEN); | |
311 | clock_class->uuid.value = clock_class->uuid.uuid; | |
312 | BT_LIB_LOGV("Set clock class's UUID: %!+K", clock_class); | |
312c056a PP |
313 | } |
314 | ||
315 | BT_HIDDEN | |
61f59358 | 316 | void _bt_clock_class_freeze(struct bt_clock_class *clock_class) |
312c056a | 317 | { |
312c056a | 318 | BT_ASSERT(clock_class); |
6f57e458 | 319 | |
61f59358 PP |
320 | if (clock_class->frozen) { |
321 | return; | |
93dda901 PP |
322 | } |
323 | ||
61f59358 PP |
324 | BT_LIB_LOGD("Freezing clock class: %!+K", clock_class); |
325 | clock_class->frozen = 1; | |
93dda901 | 326 | } |
312c056a | 327 | |
61f59358 | 328 | int bt_clock_class_cycles_to_ns_from_origin(struct bt_clock_class *clock_class, |
312c056a PP |
329 | uint64_t cycles, int64_t *ns) |
330 | { | |
331 | int ret; | |
312c056a PP |
332 | |
333 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); | |
61f59358 PP |
334 | BT_ASSERT_PRE_NON_NULL(ns, "Nanoseconds (output)"); |
335 | ret = bt_util_ns_from_origin(clock_class, cycles, ns); | |
312c056a | 336 | if (ret) { |
61f59358 PP |
337 | BT_LIB_LOGW("Cannot convert cycles to nanoseconds " |
338 | "from origin for given clock class: " | |
339 | "value overflows the signed 64-bit integer range: " | |
340 | "%![cc-]+K, cycles=%" PRIu64, | |
341 | clock_class, cycles); | |
312c056a PP |
342 | } |
343 | ||
312c056a PP |
344 | return ret; |
345 | } |