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 | ||
0f5e83e5 | 24 | #define BT_LOG_TAG "CLOCK-CLASS" |
40547d22 | 25 | #include <babeltrace/lib-logging-internal.h> |
0f5e83e5 | 26 | |
312c056a | 27 | #include <babeltrace/assert-pre-internal.h> |
75c3fca1 | 28 | #include <babeltrace/compat/uuid-internal.h> |
8c41fd73 PP |
29 | #include <babeltrace/trace-ir/clock-class-const.h> |
30 | #include <babeltrace/trace-ir/clock-class.h> | |
56e18c4c | 31 | #include <babeltrace/trace-ir/clock-class-internal.h> |
605e1019 | 32 | #include <babeltrace/trace-ir/clock-snapshot-internal.h> |
56e18c4c | 33 | #include <babeltrace/trace-ir/utils-internal.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 | |
44c440bc 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 | |
44c440bc | 45 | void destroy_clock_class(struct bt_object *obj) |
c06116f3 | 46 | { |
44c440bc | 47 | struct bt_clock_class *clock_class = (void *) obj; |
c06116f3 | 48 | |
44c440bc | 49 | BT_LIB_LOGD("Destroying clock class: %!+K", clock_class); |
273b65be | 50 | |
44c440bc PP |
51 | if (clock_class->name.str) { |
52 | g_string_free(clock_class->name.str, TRUE); | |
238b7404 PP |
53 | clock_class->name.str = NULL; |
54 | clock_class->name.value = NULL; | |
5134570b PP |
55 | } |
56 | ||
44c440bc PP |
57 | if (clock_class->description.str) { |
58 | g_string_free(clock_class->description.str, TRUE); | |
238b7404 PP |
59 | clock_class->description.str = NULL; |
60 | clock_class->description.value = NULL; | |
273b65be JG |
61 | } |
62 | ||
605e1019 | 63 | bt_object_pool_finalize(&clock_class->cs_pool); |
44c440bc | 64 | g_free(clock_class); |
4c426c17 JG |
65 | } |
66 | ||
f3534905 | 67 | static |
605e1019 | 68 | void free_clock_snapshot(struct bt_clock_snapshot *clock_snapshot, |
44c440bc | 69 | struct bt_clock_class *clock_class) |
f3534905 | 70 | { |
605e1019 | 71 | bt_clock_snapshot_destroy(clock_snapshot); |
f3534905 PP |
72 | } |
73 | ||
44c440bc PP |
74 | static inline |
75 | void set_base_offset(struct bt_clock_class *clock_class) | |
312c056a | 76 | { |
44c440bc 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 | ||
40f4ba76 | 109 | struct bt_clock_class *bt_clock_class_create(void) |
4c426c17 JG |
110 | { |
111 | int ret; | |
50842bdc | 112 | struct bt_clock_class *clock_class = NULL; |
4c426c17 | 113 | |
44c440bc | 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 | ||
44c440bc 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 | |
44c440bc 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 | ||
44c440bc PP |
135 | clock_class->frequency = UINT64_C(1000000000); |
136 | clock_class->is_absolute = BT_TRUE; | |
137 | set_base_offset(clock_class); | |
605e1019 PP |
138 | ret = bt_object_pool_initialize(&clock_class->cs_pool, |
139 | (bt_object_pool_new_object_func) bt_clock_snapshot_new, | |
312c056a | 140 | (bt_object_pool_destroy_object_func) |
605e1019 | 141 | free_clock_snapshot, |
312c056a PP |
142 | clock_class); |
143 | if (ret) { | |
605e1019 | 144 | BT_LOGE("Failed to initialize clock snapshot pool: ret=%d", |
312c056a PP |
145 | ret); |
146 | goto error; | |
147 | } | |
148 | ||
44c440bc PP |
149 | BT_LIB_LOGD("Created clock class object: %!+K", clock_class); |
150 | goto end; | |
151 | ||
273b65be | 152 | error: |
65300d60 | 153 | BT_OBJECT_PUT_REF_AND_RESET(clock_class); |
87d76bb1 JG |
154 | |
155 | end: | |
40f4ba76 | 156 | return clock_class; |
87d76bb1 JG |
157 | } |
158 | ||
40f4ba76 | 159 | const char *bt_clock_class_get_name(const struct bt_clock_class *clock_class) |
87d76bb1 | 160 | { |
44c440bc PP |
161 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); |
162 | return clock_class->name.value; | |
163 | } | |
87d76bb1 | 164 | |
8c41fd73 PP |
165 | enum bt_clock_class_status bt_clock_class_set_name( |
166 | struct bt_clock_class *clock_class, const char *name) | |
44c440bc PP |
167 | { |
168 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); | |
169 | BT_ASSERT_PRE_NON_NULL(name, "Name"); | |
170 | BT_ASSERT_PRE_CLOCK_CLASS_HOT(clock_class); | |
171 | g_string_assign(clock_class->name.str, name); | |
172 | clock_class->name.value = clock_class->name.str->str; | |
173 | BT_LIB_LOGV("Set clock class's name: %!+K", clock_class); | |
8c41fd73 | 174 | return BT_CLOCK_CLASS_STATUS_OK; |
44c440bc | 175 | } |
87d76bb1 | 176 | |
40f4ba76 PP |
177 | const char *bt_clock_class_get_description( |
178 | const struct bt_clock_class *clock_class) | |
44c440bc PP |
179 | { |
180 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); | |
181 | return clock_class->description.value; | |
273b65be JG |
182 | } |
183 | ||
8c41fd73 PP |
184 | enum bt_clock_class_status bt_clock_class_set_description( |
185 | struct bt_clock_class *clock_class, const char *descr) | |
273b65be | 186 | { |
44c440bc 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); | |
8c41fd73 | 194 | return BT_CLOCK_CLASS_STATUS_OK; |
273b65be JG |
195 | } |
196 | ||
40f4ba76 | 197 | uint64_t bt_clock_class_get_frequency(const struct bt_clock_class *clock_class) |
87d76bb1 | 198 | { |
44c440bc PP |
199 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); |
200 | return clock_class->frequency; | |
87d76bb1 JG |
201 | } |
202 | ||
40f4ba76 | 203 | void bt_clock_class_set_frequency(struct bt_clock_class *clock_class, |
44c440bc | 204 | uint64_t frequency) |
273b65be | 205 | { |
44c440bc 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); | |
273b65be JG |
217 | } |
218 | ||
40f4ba76 | 219 | uint64_t bt_clock_class_get_precision(const struct bt_clock_class *clock_class) |
87d76bb1 | 220 | { |
44c440bc PP |
221 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); |
222 | return clock_class->precision; | |
87d76bb1 JG |
223 | } |
224 | ||
40f4ba76 | 225 | void bt_clock_class_set_precision(struct bt_clock_class *clock_class, |
ac0c6bdd | 226 | uint64_t precision) |
273b65be | 227 | { |
44c440bc PP |
228 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); |
229 | BT_ASSERT_PRE_CLOCK_CLASS_HOT(clock_class); | |
230 | BT_ASSERT_PRE(precision != UINT64_C(-1), | |
231 | "Invalid precision: %![cc-]+K, new-precision=%" PRIu64, | |
232 | clock_class, precision); | |
ac0c6bdd | 233 | clock_class->precision = precision; |
44c440bc | 234 | BT_LIB_LOGV("Set clock class's precision: %!+K", clock_class); |
273b65be JG |
235 | } |
236 | ||
40f4ba76 | 237 | void bt_clock_class_get_offset(const struct bt_clock_class *clock_class, |
44c440bc | 238 | int64_t *seconds, uint64_t *cycles) |
87d76bb1 | 239 | { |
44c440bc PP |
240 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); |
241 | BT_ASSERT_PRE_NON_NULL(seconds, "Seconds (output)"); | |
242 | BT_ASSERT_PRE_NON_NULL(cycles, "Cycles (output)"); | |
243 | *seconds = clock_class->offset_seconds; | |
244 | *cycles = clock_class->offset_cycles; | |
87d76bb1 JG |
245 | } |
246 | ||
40f4ba76 | 247 | void bt_clock_class_set_offset(struct bt_clock_class *clock_class, |
44c440bc | 248 | int64_t seconds, uint64_t cycles) |
273b65be | 249 | { |
44c440bc PP |
250 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); |
251 | BT_ASSERT_PRE_CLOCK_CLASS_HOT(clock_class); | |
252 | BT_ASSERT_PRE(cycles < clock_class->frequency, | |
253 | "Offset (cycles) is greater than clock class's frequency: " | |
254 | "%![cc-]+K, new-offset-cycles=%" PRIu64, clock_class, cycles); | |
255 | clock_class->offset_seconds = seconds; | |
256 | clock_class->offset_cycles = cycles; | |
257 | set_base_offset(clock_class); | |
258 | BT_LIB_LOGV("Set clock class's offset: %!+K", clock_class); | |
273b65be JG |
259 | } |
260 | ||
40f4ba76 | 261 | bt_bool bt_clock_class_is_absolute(const struct bt_clock_class *clock_class) |
87d76bb1 | 262 | { |
44c440bc PP |
263 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); |
264 | return (bool) clock_class->is_absolute; | |
87d76bb1 JG |
265 | } |
266 | ||
40f4ba76 | 267 | void bt_clock_class_set_is_absolute(struct bt_clock_class *clock_class, |
a2b94977 | 268 | bt_bool is_absolute) |
273b65be | 269 | { |
44c440bc PP |
270 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); |
271 | BT_ASSERT_PRE_CLOCK_CLASS_HOT(clock_class); | |
272 | clock_class->is_absolute = (bool) is_absolute; | |
273 | BT_LIB_LOGV("Set clock class's absolute property: %!+K", | |
274 | clock_class); | |
273b65be JG |
275 | } |
276 | ||
40f4ba76 | 277 | bt_uuid bt_clock_class_get_uuid(const struct bt_clock_class *clock_class) |
85b743f4 | 278 | { |
44c440bc PP |
279 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); |
280 | return clock_class->uuid.value; | |
85b743f4 JG |
281 | } |
282 | ||
40f4ba76 | 283 | void bt_clock_class_set_uuid(struct bt_clock_class *clock_class, |
44c440bc | 284 | bt_uuid uuid) |
85b743f4 | 285 | { |
44c440bc PP |
286 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); |
287 | BT_ASSERT_PRE_NON_NULL(uuid, "UUID"); | |
288 | BT_ASSERT_PRE_CLOCK_CLASS_HOT(clock_class); | |
289 | memcpy(clock_class->uuid.uuid, uuid, BABELTRACE_UUID_LEN); | |
290 | clock_class->uuid.value = clock_class->uuid.uuid; | |
291 | BT_LIB_LOGV("Set clock class's UUID: %!+K", clock_class); | |
312c056a PP |
292 | } |
293 | ||
294 | BT_HIDDEN | |
40f4ba76 | 295 | void _bt_clock_class_freeze(const struct bt_clock_class *clock_class) |
312c056a | 296 | { |
312c056a | 297 | BT_ASSERT(clock_class); |
6f57e458 | 298 | |
44c440bc PP |
299 | if (clock_class->frozen) { |
300 | return; | |
93dda901 PP |
301 | } |
302 | ||
44c440bc | 303 | BT_LIB_LOGD("Freezing clock class: %!+K", clock_class); |
40f4ba76 | 304 | ((struct bt_clock_class *) clock_class)->frozen = 1; |
93dda901 | 305 | } |
312c056a | 306 | |
8c41fd73 | 307 | enum bt_clock_class_status bt_clock_class_cycles_to_ns_from_origin( |
40f4ba76 | 308 | const struct bt_clock_class *clock_class, |
312c056a PP |
309 | uint64_t cycles, int64_t *ns) |
310 | { | |
311 | int ret; | |
312c056a PP |
312 | |
313 | BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class"); | |
44c440bc PP |
314 | BT_ASSERT_PRE_NON_NULL(ns, "Nanoseconds (output)"); |
315 | ret = bt_util_ns_from_origin(clock_class, cycles, ns); | |
312c056a | 316 | if (ret) { |
8c41fd73 | 317 | ret = BT_CLOCK_CLASS_STATUS_OVERFLOW; |
44c440bc PP |
318 | BT_LIB_LOGW("Cannot convert cycles to nanoseconds " |
319 | "from origin for given clock class: " | |
320 | "value overflows the signed 64-bit integer range: " | |
321 | "%![cc-]+K, cycles=%" PRIu64, | |
322 | clock_class, cycles); | |
312c056a PP |
323 | } |
324 | ||
312c056a PP |
325 | return ret; |
326 | } | |
c5b9b441 PP |
327 | |
328 | void bt_clock_class_get_ref(const struct bt_clock_class *clock_class) | |
329 | { | |
330 | bt_object_get_ref(clock_class); | |
331 | } | |
332 | ||
333 | void bt_clock_class_put_ref(const struct bt_clock_class *clock_class) | |
334 | { | |
335 | bt_object_put_ref(clock_class); | |
336 | } |