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