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