lib: return `void` when setting a simple value with no side effects
[babeltrace.git] / lib / trace-ir / clock-class.c
CommitLineData
273b65be 1/*
de9dd397 2 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
273b65be
JG
3 *
4 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
0f5e83e5 25#define BT_LOG_TAG "CLOCK-CLASS"
40547d22 26#include <babeltrace/lib-logging-internal.h>
0f5e83e5 27
a6918753 28#include <babeltrace/assert-pre-internal.h>
75c3fca1 29#include <babeltrace/compat/uuid-internal.h>
108b91d0
PP
30#include <babeltrace/trace-ir/clock-class-internal.h>
31#include <babeltrace/trace-ir/clock-value-internal.h>
32#include <babeltrace/trace-ir/utils-internal.h>
8138bfe1 33#include <babeltrace/object.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
44static
7b33a0e0 45void 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);
5134570b
PP
53 }
54
7b33a0e0
PP
55 if (clock_class->description.str) {
56 g_string_free(clock_class->description.str, TRUE);
273b65be
JG
57 }
58
7b33a0e0
PP
59 bt_object_pool_finalize(&clock_class->cv_pool);
60 g_free(clock_class);
4c426c17
JG
61}
62
15260cc8 63static
7b33a0e0
PP
64void free_clock_value(struct bt_clock_value *clock_value,
65 struct bt_clock_class *clock_class)
15260cc8 66{
7b33a0e0 67 bt_clock_value_destroy(clock_value);
15260cc8
PP
68}
69
7b33a0e0
PP
70static inline
71void set_base_offset(struct bt_clock_class *clock_class)
a6918753 72{
7b33a0e0
PP
73 uint64_t offset_cycles_ns;
74
75 /* Initialize nanosecond timestamp to clock's offset in seconds */
76 if (clock_class->offset_seconds <= (INT64_MIN / INT64_C(1000000000) - 1) ||
77 clock_class->offset_seconds >= (INT64_MAX / INT64_C(1000000000)) - 1) {
78 /*
79 * Overflow: offset in seconds converted to nanoseconds
80 * is outside the int64_t range. We also subtract 1 here
81 * to leave "space" for the offset in cycles converted
82 * to nanoseconds (which is always less than 1 second by
83 * contract).
84 */
85 clock_class->base_offset.overflows = true;
86 goto end;
87 }
88
89 /* Offset (seconds) to nanoseconds */
90 clock_class->base_offset.value_ns = clock_class->offset_seconds *
91 INT64_C(1000000000);
92
93 /* Add offset in cycles */
94 BT_ASSERT(clock_class->offset_cycles < clock_class->frequency);
95 offset_cycles_ns = bt_util_ns_from_value(clock_class->frequency,
96 clock_class->offset_cycles);
97 BT_ASSERT(offset_cycles_ns < 1000000000);
98 clock_class->base_offset.value_ns += (int64_t) offset_cycles_ns;
99 clock_class->base_offset.overflows = false;
100
101end:
102 return;
a6918753
PP
103}
104
9e550e5f 105struct bt_private_clock_class *bt_private_clock_class_create(void)
4c426c17
JG
106{
107 int ret;
839d52a5 108 struct bt_clock_class *clock_class = NULL;
4c426c17 109
7b33a0e0 110 BT_LOGD_STR("Creating default clock class object");
15260cc8 111
839d52a5 112 clock_class = g_new0(struct bt_clock_class, 1);
ac0c6bdd 113 if (!clock_class) {
5134570b 114 BT_LOGE_STR("Failed to allocate one clock class.");
4c426c17
JG
115 goto error;
116 }
117
7b33a0e0
PP
118 bt_object_init_shared(&clock_class->base, destroy_clock_class);
119 clock_class->name.str = g_string_new(NULL);
120 if (!clock_class->name.str) {
121 BT_LOGE_STR("Failed to allocate a GString.");
122 goto error;
123 }
85380e99 124
7b33a0e0
PP
125 clock_class->description.str = g_string_new(NULL);
126 if (!clock_class->description.str) {
127 BT_LOGE_STR("Failed to allocate a GString.");
128 goto error;
273b65be
JG
129 }
130
7b33a0e0
PP
131 clock_class->frequency = UINT64_C(1000000000);
132 clock_class->is_absolute = BT_TRUE;
133 set_base_offset(clock_class);
a6918753
PP
134 ret = bt_object_pool_initialize(&clock_class->cv_pool,
135 (bt_object_pool_new_object_func) bt_clock_value_new,
136 (bt_object_pool_destroy_object_func)
7b33a0e0 137 free_clock_value,
a6918753
PP
138 clock_class);
139 if (ret) {
140 BT_LOGE("Failed to initialize clock value pool: ret=%d",
141 ret);
142 goto error;
143 }
144
7b33a0e0
PP
145 BT_LIB_LOGD("Created clock class object: %!+K", clock_class);
146 goto end;
147
273b65be 148error:
8138bfe1 149 BT_OBJECT_PUT_REF_AND_RESET(clock_class);
87d76bb1
JG
150
151end:
9e550e5f 152 return (void *) clock_class;
87d76bb1
JG
153}
154
7b33a0e0 155const char *bt_clock_class_get_name(
839d52a5 156 struct bt_clock_class *clock_class)
87d76bb1 157{
7b33a0e0
PP
158 BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class");
159 return clock_class->name.value;
160}
87d76bb1 161
9e550e5f
PP
162int bt_private_clock_class_set_name(
163 struct bt_private_clock_class *priv_clock_class,
7b33a0e0
PP
164 const char *name)
165{
9e550e5f
PP
166 struct bt_clock_class *clock_class = (void *) priv_clock_class;
167
7b33a0e0
PP
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);
174 return 0;
175}
87d76bb1 176
7b33a0e0
PP
177const char *bt_clock_class_get_description(struct bt_clock_class *clock_class)
178{
179 BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class");
180 return clock_class->description.value;
273b65be
JG
181}
182
9e550e5f
PP
183int bt_private_clock_class_set_description(
184 struct bt_private_clock_class *priv_clock_class,
7b33a0e0 185 const char *descr)
273b65be 186{
9e550e5f
PP
187 struct bt_clock_class *clock_class = (void *) priv_clock_class;
188
7b33a0e0
PP
189 BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class");
190 BT_ASSERT_PRE_NON_NULL(descr, "Description");
191 BT_ASSERT_PRE_CLOCK_CLASS_HOT(clock_class);
192 g_string_assign(clock_class->description.str, descr);
193 clock_class->description.value = clock_class->description.str->str;
194 BT_LIB_LOGV("Set clock class's description: %!+K",
195 clock_class);
196 return 0;
273b65be
JG
197}
198
7b33a0e0 199uint64_t bt_clock_class_get_frequency(struct bt_clock_class *clock_class)
87d76bb1 200{
7b33a0e0
PP
201 BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class");
202 return clock_class->frequency;
87d76bb1
JG
203}
204
c8bbf821 205void bt_private_clock_class_set_frequency(
9e550e5f 206 struct bt_private_clock_class *priv_clock_class,
7b33a0e0 207 uint64_t frequency)
273b65be 208{
9e550e5f
PP
209 struct bt_clock_class *clock_class = (void *) priv_clock_class;
210
7b33a0e0
PP
211 BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class");
212 BT_ASSERT_PRE_CLOCK_CLASS_HOT(clock_class);
213 BT_ASSERT_PRE(frequency != UINT64_C(-1) && frequency != 0,
214 "Invalid frequency: %![cc-]+K, new-freq=%" PRIu64,
215 clock_class, frequency);
216 BT_ASSERT_PRE(clock_class->offset_cycles < frequency,
217 "Offset (cycles) is greater than clock class's frequency: "
218 "%![cc-]+K, new-freq=%" PRIu64, clock_class, frequency);
219 clock_class->frequency = frequency;
220 set_base_offset(clock_class);
221 BT_LIB_LOGV("Set clock class's frequency: %!+K", clock_class);
273b65be
JG
222}
223
839d52a5 224uint64_t bt_clock_class_get_precision(struct bt_clock_class *clock_class)
87d76bb1 225{
7b33a0e0
PP
226 BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class");
227 return clock_class->precision;
87d76bb1
JG
228}
229
c8bbf821 230void bt_private_clock_class_set_precision(
9e550e5f 231 struct bt_private_clock_class *priv_clock_class,
ac0c6bdd 232 uint64_t precision)
273b65be 233{
9e550e5f
PP
234 struct bt_clock_class *clock_class = (void *) priv_clock_class;
235
7b33a0e0
PP
236 BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class");
237 BT_ASSERT_PRE_CLOCK_CLASS_HOT(clock_class);
238 BT_ASSERT_PRE(precision != UINT64_C(-1),
239 "Invalid precision: %![cc-]+K, new-precision=%" PRIu64,
240 clock_class, precision);
ac0c6bdd 241 clock_class->precision = precision;
7b33a0e0 242 BT_LIB_LOGV("Set clock class's precision: %!+K", clock_class);
273b65be
JG
243}
244
7b33a0e0
PP
245void bt_clock_class_get_offset(struct bt_clock_class *clock_class,
246 int64_t *seconds, uint64_t *cycles)
87d76bb1 247{
7b33a0e0
PP
248 BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class");
249 BT_ASSERT_PRE_NON_NULL(seconds, "Seconds (output)");
250 BT_ASSERT_PRE_NON_NULL(cycles, "Cycles (output)");
251 *seconds = clock_class->offset_seconds;
252 *cycles = clock_class->offset_cycles;
87d76bb1
JG
253}
254
c8bbf821 255void bt_private_clock_class_set_offset(
9e550e5f 256 struct bt_private_clock_class *priv_clock_class,
7b33a0e0 257 int64_t seconds, uint64_t cycles)
273b65be 258{
9e550e5f
PP
259 struct bt_clock_class *clock_class = (void *) priv_clock_class;
260
7b33a0e0
PP
261 BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class");
262 BT_ASSERT_PRE_CLOCK_CLASS_HOT(clock_class);
263 BT_ASSERT_PRE(cycles < clock_class->frequency,
264 "Offset (cycles) is greater than clock class's frequency: "
265 "%![cc-]+K, new-offset-cycles=%" PRIu64, clock_class, cycles);
266 clock_class->offset_seconds = seconds;
267 clock_class->offset_cycles = cycles;
268 set_base_offset(clock_class);
269 BT_LIB_LOGV("Set clock class's offset: %!+K", clock_class);
273b65be
JG
270}
271
839d52a5 272bt_bool bt_clock_class_is_absolute(struct bt_clock_class *clock_class)
87d76bb1 273{
7b33a0e0
PP
274 BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class");
275 return (bool) clock_class->is_absolute;
87d76bb1
JG
276}
277
c8bbf821 278void bt_private_clock_class_set_is_absolute(
9e550e5f 279 struct bt_private_clock_class *priv_clock_class,
a2b94977 280 bt_bool is_absolute)
273b65be 281{
9e550e5f
PP
282 struct bt_clock_class *clock_class = (void *) priv_clock_class;
283
7b33a0e0
PP
284 BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class");
285 BT_ASSERT_PRE_CLOCK_CLASS_HOT(clock_class);
286 clock_class->is_absolute = (bool) is_absolute;
287 BT_LIB_LOGV("Set clock class's absolute property: %!+K",
288 clock_class);
273b65be
JG
289}
290
7b33a0e0 291bt_uuid bt_clock_class_get_uuid(struct bt_clock_class *clock_class)
85b743f4 292{
7b33a0e0
PP
293 BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class");
294 return clock_class->uuid.value;
85b743f4
JG
295}
296
c8bbf821 297void bt_private_clock_class_set_uuid(
9e550e5f 298 struct bt_private_clock_class *priv_clock_class,
7b33a0e0 299 bt_uuid uuid)
85b743f4 300{
9e550e5f
PP
301 struct bt_clock_class *clock_class = (void *) priv_clock_class;
302
7b33a0e0
PP
303 BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class");
304 BT_ASSERT_PRE_NON_NULL(uuid, "UUID");
305 BT_ASSERT_PRE_CLOCK_CLASS_HOT(clock_class);
306 memcpy(clock_class->uuid.uuid, uuid, BABELTRACE_UUID_LEN);
307 clock_class->uuid.value = clock_class->uuid.uuid;
308 BT_LIB_LOGV("Set clock class's UUID: %!+K", clock_class);
a6918753
PP
309}
310
311BT_HIDDEN
7b33a0e0 312void _bt_clock_class_freeze(struct bt_clock_class *clock_class)
a6918753 313{
a6918753 314 BT_ASSERT(clock_class);
6f57e458 315
7b33a0e0
PP
316 if (clock_class->frozen) {
317 return;
db3347ac
PP
318 }
319
7b33a0e0
PP
320 BT_LIB_LOGD("Freezing clock class: %!+K", clock_class);
321 clock_class->frozen = 1;
db3347ac 322}
a6918753 323
7b33a0e0 324int bt_clock_class_cycles_to_ns_from_origin(struct bt_clock_class *clock_class,
a6918753
PP
325 uint64_t cycles, int64_t *ns)
326{
327 int ret;
a6918753
PP
328
329 BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class");
7b33a0e0
PP
330 BT_ASSERT_PRE_NON_NULL(ns, "Nanoseconds (output)");
331 ret = bt_util_ns_from_origin(clock_class, cycles, ns);
a6918753 332 if (ret) {
7b33a0e0
PP
333 BT_LIB_LOGW("Cannot convert cycles to nanoseconds "
334 "from origin for given clock class: "
335 "value overflows the signed 64-bit integer range: "
336 "%![cc-]+K, cycles=%" PRIu64,
337 clock_class, cycles);
a6918753
PP
338 }
339
a6918753
PP
340 return ret;
341}
This page took 0.089202 seconds and 4 git commands to generate.