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