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