Commit | Line | Data |
---|---|---|
273b65be | 1 | /* |
ac0c6bdd | 2 | * clock-class.c |
273b65be | 3 | * |
108b91d0 | 4 | * Babeltrace trace IR - Clock class |
273b65be | 5 | * |
de9dd397 | 6 | * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
273b65be JG |
7 | * |
8 | * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
9 | * | |
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
11 | * of this software and associated documentation files (the "Software"), to deal | |
12 | * in the Software without restriction, including without limitation the rights | |
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
14 | * copies of the Software, and to permit persons to whom the Software is | |
15 | * furnished to do so, subject to the following conditions: | |
16 | * | |
17 | * The above copyright notice and this permission notice shall be included in | |
18 | * all copies or substantial portions of the Software. | |
19 | * | |
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
26 | * SOFTWARE. | |
27 | */ | |
28 | ||
0f5e83e5 | 29 | #define BT_LOG_TAG "CLOCK-CLASS" |
40547d22 | 30 | #include <babeltrace/lib-logging-internal.h> |
0f5e83e5 | 31 | |
a6918753 | 32 | #include <babeltrace/assert-pre-internal.h> |
75c3fca1 | 33 | #include <babeltrace/compat/uuid-internal.h> |
108b91d0 PP |
34 | #include <babeltrace/trace-ir/clock-class-internal.h> |
35 | #include <babeltrace/trace-ir/clock-value-internal.h> | |
36 | #include <babeltrace/trace-ir/utils-internal.h> | |
8138bfe1 | 37 | #include <babeltrace/object.h> |
3d9990ac | 38 | #include <babeltrace/compiler-internal.h> |
c55a9f58 | 39 | #include <babeltrace/types.h> |
ee389f01 | 40 | #include <babeltrace/compat/string-internal.h> |
273b65be | 41 | #include <inttypes.h> |
0f5e83e5 | 42 | #include <babeltrace/object-internal.h> |
8b45963b | 43 | #include <babeltrace/assert-internal.h> |
5134570b | 44 | |
7b33a0e0 PP |
45 | #define BT_ASSERT_PRE_CLOCK_CLASS_HOT(_cc) \ |
46 | BT_ASSERT_PRE_HOT((_cc), "Clock class", ": %!+K", (_cc)) | |
a6918753 PP |
47 | |
48 | static | |
7b33a0e0 | 49 | void destroy_clock_class(struct bt_object *obj) |
c06116f3 | 50 | { |
7b33a0e0 | 51 | struct bt_clock_class *clock_class = (void *) obj; |
c06116f3 | 52 | |
7b33a0e0 | 53 | BT_LIB_LOGD("Destroying clock class: %!+K", clock_class); |
273b65be | 54 | |
7b33a0e0 PP |
55 | if (clock_class->name.str) { |
56 | g_string_free(clock_class->name.str, TRUE); | |
5134570b PP |
57 | } |
58 | ||
7b33a0e0 PP |
59 | if (clock_class->description.str) { |
60 | g_string_free(clock_class->description.str, TRUE); | |
273b65be JG |
61 | } |
62 | ||
7b33a0e0 PP |
63 | bt_object_pool_finalize(&clock_class->cv_pool); |
64 | g_free(clock_class); | |
4c426c17 JG |
65 | } |
66 | ||
15260cc8 | 67 | static |
7b33a0e0 PP |
68 | void free_clock_value(struct bt_clock_value *clock_value, |
69 | struct bt_clock_class *clock_class) | |
15260cc8 | 70 | { |
7b33a0e0 | 71 | bt_clock_value_destroy(clock_value); |
15260cc8 PP |
72 | } |
73 | ||
7b33a0e0 PP |
74 | static inline |
75 | void set_base_offset(struct bt_clock_class *clock_class) | |
a6918753 | 76 | { |
7b33a0e0 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; | |
a6918753 PP |
107 | } |
108 | ||
7b33a0e0 | 109 | struct bt_clock_class *bt_clock_class_create(void) |
4c426c17 JG |
110 | { |
111 | int ret; | |
839d52a5 | 112 | struct bt_clock_class *clock_class = NULL; |
4c426c17 | 113 | |
7b33a0e0 | 114 | BT_LOGD_STR("Creating default clock class object"); |
15260cc8 | 115 | |
839d52a5 | 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 | ||
7b33a0e0 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 | |
7b33a0e0 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 | ||
7b33a0e0 PP |
135 | clock_class->frequency = UINT64_C(1000000000); |
136 | clock_class->is_absolute = BT_TRUE; | |
137 | set_base_offset(clock_class); | |
a6918753 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) | |
7b33a0e0 | 141 | free_clock_value, |
a6918753 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 | ||
7b33a0e0 PP |
149 | BT_LIB_LOGD("Created clock class object: %!+K", clock_class); |
150 | goto end; | |
151 | ||
273b65be | 152 | error: |
8138bfe1 | 153 | BT_OBJECT_PUT_REF_AND_RESET(clock_class); |
87d76bb1 JG |
154 | |
155 | end: | |
7b33a0e0 | 156 | return clock_class; |
87d76bb1 JG |
157 | } |
158 | ||
7b33a0e0 | 159 | const char *bt_clock_class_get_name( |
839d52a5 | 160 | struct bt_clock_class *clock_class) |
87d76bb1 | 161 | { |
7b33a0e0 PP |
162 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); |
163 | return clock_class->name.value; | |
164 | } | |
87d76bb1 | 165 | |
7b33a0e0 PP |
166 | int bt_clock_class_set_name(struct bt_clock_class *clock_class, |
167 | const char *name) | |
168 | { | |
169 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); | |
170 | BT_ASSERT_PRE_NON_NULL(name, "Name"); | |
171 | BT_ASSERT_PRE_CLOCK_CLASS_HOT(clock_class); | |
172 | g_string_assign(clock_class->name.str, name); | |
173 | clock_class->name.value = clock_class->name.str->str; | |
174 | BT_LIB_LOGV("Set clock class's name: %!+K", clock_class); | |
175 | return 0; | |
176 | } | |
87d76bb1 | 177 | |
7b33a0e0 PP |
178 | const char *bt_clock_class_get_description(struct bt_clock_class *clock_class) |
179 | { | |
180 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); | |
181 | return clock_class->description.value; | |
273b65be JG |
182 | } |
183 | ||
839d52a5 | 184 | int bt_clock_class_set_description(struct bt_clock_class *clock_class, |
7b33a0e0 | 185 | const char *descr) |
273b65be | 186 | { |
7b33a0e0 PP |
187 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); |
188 | BT_ASSERT_PRE_NON_NULL(descr, "Description"); | |
189 | BT_ASSERT_PRE_CLOCK_CLASS_HOT(clock_class); | |
190 | g_string_assign(clock_class->description.str, descr); | |
191 | clock_class->description.value = clock_class->description.str->str; | |
192 | BT_LIB_LOGV("Set clock class's description: %!+K", | |
193 | clock_class); | |
194 | return 0; | |
273b65be JG |
195 | } |
196 | ||
7b33a0e0 | 197 | uint64_t bt_clock_class_get_frequency(struct bt_clock_class *clock_class) |
87d76bb1 | 198 | { |
7b33a0e0 PP |
199 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); |
200 | return clock_class->frequency; | |
87d76bb1 JG |
201 | } |
202 | ||
839d52a5 | 203 | int bt_clock_class_set_frequency(struct bt_clock_class *clock_class, |
7b33a0e0 | 204 | uint64_t frequency) |
273b65be | 205 | { |
7b33a0e0 PP |
206 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); |
207 | BT_ASSERT_PRE_CLOCK_CLASS_HOT(clock_class); | |
208 | BT_ASSERT_PRE(frequency != UINT64_C(-1) && frequency != 0, | |
209 | "Invalid frequency: %![cc-]+K, new-freq=%" PRIu64, | |
210 | clock_class, frequency); | |
211 | BT_ASSERT_PRE(clock_class->offset_cycles < frequency, | |
212 | "Offset (cycles) is greater than clock class's frequency: " | |
213 | "%![cc-]+K, new-freq=%" PRIu64, clock_class, frequency); | |
214 | clock_class->frequency = frequency; | |
215 | set_base_offset(clock_class); | |
216 | BT_LIB_LOGV("Set clock class's frequency: %!+K", clock_class); | |
217 | return 0; | |
273b65be JG |
218 | } |
219 | ||
839d52a5 | 220 | uint64_t bt_clock_class_get_precision(struct bt_clock_class *clock_class) |
87d76bb1 | 221 | { |
7b33a0e0 PP |
222 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); |
223 | return clock_class->precision; | |
87d76bb1 JG |
224 | } |
225 | ||
839d52a5 | 226 | int bt_clock_class_set_precision(struct bt_clock_class *clock_class, |
ac0c6bdd | 227 | uint64_t precision) |
273b65be | 228 | { |
7b33a0e0 PP |
229 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); |
230 | BT_ASSERT_PRE_CLOCK_CLASS_HOT(clock_class); | |
231 | BT_ASSERT_PRE(precision != UINT64_C(-1), | |
232 | "Invalid precision: %![cc-]+K, new-precision=%" PRIu64, | |
233 | clock_class, precision); | |
ac0c6bdd | 234 | clock_class->precision = precision; |
7b33a0e0 PP |
235 | BT_LIB_LOGV("Set clock class's precision: %!+K", clock_class); |
236 | return 0; | |
273b65be JG |
237 | } |
238 | ||
7b33a0e0 PP |
239 | void bt_clock_class_get_offset(struct bt_clock_class *clock_class, |
240 | int64_t *seconds, uint64_t *cycles) | |
87d76bb1 | 241 | { |
7b33a0e0 PP |
242 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); |
243 | BT_ASSERT_PRE_NON_NULL(seconds, "Seconds (output)"); | |
244 | BT_ASSERT_PRE_NON_NULL(cycles, "Cycles (output)"); | |
245 | *seconds = clock_class->offset_seconds; | |
246 | *cycles = clock_class->offset_cycles; | |
87d76bb1 JG |
247 | } |
248 | ||
7b33a0e0 PP |
249 | int bt_clock_class_set_offset(struct bt_clock_class *clock_class, |
250 | int64_t seconds, uint64_t cycles) | |
273b65be | 251 | { |
7b33a0e0 PP |
252 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); |
253 | BT_ASSERT_PRE_CLOCK_CLASS_HOT(clock_class); | |
254 | BT_ASSERT_PRE(cycles < clock_class->frequency, | |
255 | "Offset (cycles) is greater than clock class's frequency: " | |
256 | "%![cc-]+K, new-offset-cycles=%" PRIu64, clock_class, cycles); | |
257 | clock_class->offset_seconds = seconds; | |
258 | clock_class->offset_cycles = cycles; | |
259 | set_base_offset(clock_class); | |
260 | BT_LIB_LOGV("Set clock class's offset: %!+K", clock_class); | |
261 | return 0; | |
273b65be JG |
262 | } |
263 | ||
839d52a5 | 264 | bt_bool bt_clock_class_is_absolute(struct bt_clock_class *clock_class) |
87d76bb1 | 265 | { |
7b33a0e0 PP |
266 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); |
267 | return (bool) clock_class->is_absolute; | |
87d76bb1 JG |
268 | } |
269 | ||
839d52a5 | 270 | int bt_clock_class_set_is_absolute(struct bt_clock_class *clock_class, |
a2b94977 | 271 | bt_bool is_absolute) |
273b65be | 272 | { |
7b33a0e0 PP |
273 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); |
274 | BT_ASSERT_PRE_CLOCK_CLASS_HOT(clock_class); | |
275 | clock_class->is_absolute = (bool) is_absolute; | |
276 | BT_LIB_LOGV("Set clock class's absolute property: %!+K", | |
277 | clock_class); | |
278 | return 0; | |
273b65be JG |
279 | } |
280 | ||
7b33a0e0 | 281 | bt_uuid bt_clock_class_get_uuid(struct bt_clock_class *clock_class) |
85b743f4 | 282 | { |
7b33a0e0 PP |
283 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); |
284 | return clock_class->uuid.value; | |
85b743f4 JG |
285 | } |
286 | ||
839d52a5 | 287 | int bt_clock_class_set_uuid(struct bt_clock_class *clock_class, |
7b33a0e0 | 288 | bt_uuid uuid) |
85b743f4 | 289 | { |
7b33a0e0 PP |
290 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); |
291 | BT_ASSERT_PRE_NON_NULL(uuid, "UUID"); | |
292 | BT_ASSERT_PRE_CLOCK_CLASS_HOT(clock_class); | |
293 | memcpy(clock_class->uuid.uuid, uuid, BABELTRACE_UUID_LEN); | |
294 | clock_class->uuid.value = clock_class->uuid.uuid; | |
295 | BT_LIB_LOGV("Set clock class's UUID: %!+K", clock_class); | |
296 | return 0; | |
a6918753 PP |
297 | } |
298 | ||
299 | BT_HIDDEN | |
7b33a0e0 | 300 | void _bt_clock_class_freeze(struct bt_clock_class *clock_class) |
a6918753 | 301 | { |
a6918753 | 302 | BT_ASSERT(clock_class); |
6f57e458 | 303 | |
7b33a0e0 PP |
304 | if (clock_class->frozen) { |
305 | return; | |
db3347ac PP |
306 | } |
307 | ||
7b33a0e0 PP |
308 | BT_LIB_LOGD("Freezing clock class: %!+K", clock_class); |
309 | clock_class->frozen = 1; | |
db3347ac | 310 | } |
a6918753 | 311 | |
7b33a0e0 | 312 | int bt_clock_class_cycles_to_ns_from_origin(struct bt_clock_class *clock_class, |
a6918753 PP |
313 | uint64_t cycles, int64_t *ns) |
314 | { | |
315 | int ret; | |
a6918753 PP |
316 | |
317 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); | |
7b33a0e0 PP |
318 | BT_ASSERT_PRE_NON_NULL(ns, "Nanoseconds (output)"); |
319 | ret = bt_util_ns_from_origin(clock_class, cycles, ns); | |
a6918753 | 320 | if (ret) { |
7b33a0e0 PP |
321 | BT_LIB_LOGW("Cannot convert cycles to nanoseconds " |
322 | "from origin for given clock class: " | |
323 | "value overflows the signed 64-bit integer range: " | |
324 | "%![cc-]+K, cycles=%" PRIu64, | |
325 | clock_class, cycles); | |
a6918753 PP |
326 | } |
327 | ||
a6918753 PP |
328 | return ret; |
329 | } |