lib: update and simplify the `bt_object` API
[babeltrace.git] / lib / ctf-ir / clock-class.c
CommitLineData
273b65be 1/*
ac0c6bdd 2 * clock-class.c
273b65be 3 *
ac0c6bdd 4 * Babeltrace CTF IR - Clock class
273b65be 5 *
de9dd397 6 * Copyright 2013, 2014 Jérémie Galarneau <jeremie.galarneau@efficios.com>
273b65be
JG
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
0f5e83e5 29#define BT_LOG_TAG "CLOCK-CLASS"
40547d22 30#include <babeltrace/lib-logging-internal.h>
0f5e83e5 31
312c056a 32#include <babeltrace/assert-pre-internal.h>
75c3fca1 33#include <babeltrace/compat/uuid-internal.h>
ac0c6bdd 34#include <babeltrace/ctf-ir/clock-class-internal.h>
c057dea0 35#include <babeltrace/ctf-ir/clock-value-internal.h>
654c1444 36#include <babeltrace/ctf-ir/utils.h>
83509119 37#include <babeltrace/ref.h>
3d9990ac 38#include <babeltrace/compiler-internal.h>
c55a9f58 39#include <babeltrace/types.h>
ee389f01 40#include <babeltrace/compat/string-internal.h>
273b65be 41#include <inttypes.h>
0f5e83e5 42#include <babeltrace/object-internal.h>
f6ccaed9 43#include <babeltrace/assert-internal.h>
5134570b 44
273b65be 45static
50842bdc 46void bt_clock_class_destroy(struct bt_object *obj);
273b65be 47
312c056a
PP
48static
49struct bt_clock_value *bt_clock_value_new(struct bt_clock_class *clock_class);
50
51static
52void bt_clock_value_destroy(struct bt_clock_value *clock_value);
53
4c426c17 54BT_HIDDEN
50842bdc 55bt_bool bt_clock_class_is_valid(struct bt_clock_class *clock_class)
c06116f3 56{
ac0c6bdd 57 return clock_class && clock_class->name;
c06116f3
PP
58}
59
50842bdc 60int bt_clock_class_set_name(struct bt_clock_class *clock_class,
4c426c17
JG
61 const char *name)
62{
63 int ret = 0;
273b65be 64
5134570b
PP
65 if (!clock_class) {
66 BT_LOGW_STR("Invalid parameter: clock class is NULL.");
67 ret = -1;
68 goto end;
69 }
70
71 if (clock_class->frozen) {
2cf0acd7 72 BT_LOGW("Invalid parameter: clock class is frozen: addr=%p, name=\"%s\"",
50842bdc 73 clock_class, bt_clock_class_get_name(clock_class));
c06116f3
PP
74 ret = -1;
75 goto end;
76 }
77
f4cc9a21 78 if (!bt_identifier_is_valid(name)) {
3dca2276 79 BT_LOGW("Clock class's name is not a valid CTF identifier: "
2cf0acd7 80 "addr=%p, name=\"%s\"",
5134570b 81 clock_class, name);
4c426c17
JG
82 ret = -1;
83 goto end;
273b65be
JG
84 }
85
ac0c6bdd
PP
86 if (clock_class->name) {
87 g_string_assign(clock_class->name, name);
e1ae7645 88 } else {
ac0c6bdd
PP
89 clock_class->name = g_string_new(name);
90 if (!clock_class->name) {
5134570b 91 BT_LOGE_STR("Failed to allocate a GString.");
e1ae7645
JG
92 ret = -1;
93 goto end;
94 }
273b65be
JG
95 }
96
2cf0acd7 97 BT_LOGV("Set clock class's name: addr=%p, name=\"%s\"",
5134570b
PP
98 clock_class, name);
99
4c426c17
JG
100end:
101 return ret;
102}
103
f3534905 104static
50842bdc 105bool validate_freq(struct bt_clock_class *clock_class,
f3534905
PP
106 const char *name, uint64_t freq)
107{
108 bool is_valid = true;
109
110 if (freq == -1ULL || freq == 0) {
111 BT_LOGW("Invalid parameter: frequency is invalid: "
112 "addr=%p, name=\"%s\", freq=%" PRIu64,
113 clock_class, name, freq);
114 is_valid = false;
115 goto end;
116 }
117
118end:
119 return is_valid;
120}
121
312c056a
PP
122static
123void bt_clock_class_free_clock_value(struct bt_clock_value *clock_value,
124 struct bt_clock_class *clock_class)
125{
126 bt_clock_value_destroy(clock_value);
127}
128
50842bdc 129struct bt_clock_class *bt_clock_class_create(const char *name,
f3534905 130 uint64_t freq)
4c426c17
JG
131{
132 int ret;
50842bdc 133 struct bt_clock_class *clock_class = NULL;
4c426c17 134
5134570b
PP
135 BT_LOGD("Creating default clock class object: name=\"%s\"",
136 name);
f3534905
PP
137
138 if (!validate_freq(NULL, name, freq)) {
139 /* validate_freq() logs errors */
140 goto error;
141 }
142
50842bdc 143 clock_class = g_new0(struct bt_clock_class, 1);
ac0c6bdd 144 if (!clock_class) {
5134570b 145 BT_LOGE_STR("Failed to allocate one clock class.");
4c426c17
JG
146 goto error;
147 }
148
ac0c6bdd 149 clock_class->precision = 1;
f3534905 150 clock_class->frequency = freq;
3fea54f6 151 bt_object_init_shared(&clock_class->base, bt_clock_class_destroy);
85380e99
JG
152
153 if (name) {
50842bdc 154 ret = bt_clock_class_set_name(clock_class, name);
85380e99 155 if (ret) {
3dca2276 156 /* bt_clock_class_set_name() logs errors */
85380e99
JG
157 goto error;
158 }
273b65be
JG
159 }
160
312c056a
PP
161 ret = bt_object_pool_initialize(&clock_class->cv_pool,
162 (bt_object_pool_new_object_func) bt_clock_value_new,
163 (bt_object_pool_destroy_object_func)
164 bt_clock_class_free_clock_value,
165 clock_class);
166 if (ret) {
167 BT_LOGE("Failed to initialize clock value pool: ret=%d",
168 ret);
169 goto error;
170 }
171
2cf0acd7
PP
172 BT_LOGD("Created clock class object: addr=%p, name=\"%s\"",
173 clock_class, name);
ac0c6bdd 174 return clock_class;
273b65be 175error:
ac0c6bdd
PP
176 BT_PUT(clock_class);
177 return clock_class;
87d76bb1
JG
178}
179
50842bdc 180const char *bt_clock_class_get_name(struct bt_clock_class *clock_class)
87d76bb1
JG
181{
182 const char *ret = NULL;
183
ac0c6bdd 184 if (!clock_class) {
5134570b 185 BT_LOGW_STR("Invalid parameter: clock class is NULL.");
87d76bb1
JG
186 goto end;
187 }
188
ac0c6bdd
PP
189 if (clock_class->name) {
190 ret = clock_class->name->str;
87d76bb1
JG
191 }
192
193end:
194 return ret;
195}
196
50842bdc
PP
197const char *bt_clock_class_get_description(
198 struct bt_clock_class *clock_class)
87d76bb1
JG
199{
200 const char *ret = NULL;
201
ac0c6bdd 202 if (!clock_class) {
5134570b 203 BT_LOGW_STR("Invalid parameter: clock class is NULL.");
87d76bb1
JG
204 goto end;
205 }
206
ac0c6bdd
PP
207 if (clock_class->description) {
208 ret = clock_class->description->str;
87d76bb1
JG
209 }
210end:
211 return ret;
273b65be
JG
212}
213
50842bdc 214int bt_clock_class_set_description(struct bt_clock_class *clock_class,
ac0c6bdd 215 const char *desc)
273b65be
JG
216{
217 int ret = 0;
218
5134570b
PP
219 if (!clock_class || !desc) {
220 BT_LOGW("Invalid parameter: clock class or description is NULL: "
2cf0acd7 221 "clock-class-addr=%p, name=\"%s\", desc-addr=%p",
50842bdc 222 clock_class, bt_clock_class_get_name(clock_class),
2cf0acd7 223 desc);
5134570b
PP
224 ret = -1;
225 goto end;
226 }
227
228 if (clock_class->frozen) {
2cf0acd7 229 BT_LOGW("Invalid parameter: clock class is frozen: addr=%p, name=\"%s\"",
50842bdc 230 clock_class, bt_clock_class_get_name(clock_class));
273b65be
JG
231 ret = -1;
232 goto end;
233 }
234
ac0c6bdd
PP
235 clock_class->description = g_string_new(desc);
236 ret = clock_class->description ? 0 : -1;
2cf0acd7
PP
237 BT_LOGV("Set clock class's description: addr=%p, "
238 "name=\"%s\", desc=\"%s\"",
50842bdc 239 clock_class, bt_clock_class_get_name(clock_class), desc);
273b65be
JG
240end:
241 return ret;
242}
243
50842bdc
PP
244uint64_t bt_clock_class_get_frequency(
245 struct bt_clock_class *clock_class)
87d76bb1
JG
246{
247 uint64_t ret = -1ULL;
248
ac0c6bdd 249 if (!clock_class) {
5134570b 250 BT_LOGW_STR("Invalid parameter: clock class is NULL.");
87d76bb1
JG
251 goto end;
252 }
253
ac0c6bdd 254 ret = clock_class->frequency;
87d76bb1
JG
255end:
256 return ret;
257}
258
50842bdc 259int bt_clock_class_set_frequency(struct bt_clock_class *clock_class,
ac0c6bdd 260 uint64_t freq)
273b65be
JG
261{
262 int ret = 0;
263
f3534905 264 if (!clock_class) {
5134570b 265 BT_LOGW("Invalid parameter: clock class is NULL or frequency is invalid: "
f3534905 266 "addr=%p, name=\"%s\"",
50842bdc 267 clock_class, bt_clock_class_get_name(clock_class));
5134570b
PP
268 ret = -1;
269 goto end;
270 }
271
50842bdc 272 if (!validate_freq(clock_class, bt_clock_class_get_name(clock_class),
f3534905
PP
273 freq)) {
274 /* validate_freq() logs errors */
275 goto end;
276 }
277
5134570b 278 if (clock_class->frozen) {
2cf0acd7 279 BT_LOGW("Invalid parameter: clock class is frozen: addr=%p, name=\"%s\"",
50842bdc 280 clock_class, bt_clock_class_get_name(clock_class));
273b65be
JG
281 ret = -1;
282 goto end;
283 }
284
ac0c6bdd 285 clock_class->frequency = freq;
2cf0acd7 286 BT_LOGV("Set clock class's frequency: addr=%p, name=\"%s\", freq=%" PRIu64,
50842bdc 287 clock_class, bt_clock_class_get_name(clock_class), freq);
273b65be
JG
288end:
289 return ret;
290}
291
50842bdc 292uint64_t bt_clock_class_get_precision(struct bt_clock_class *clock_class)
87d76bb1
JG
293{
294 uint64_t ret = -1ULL;
295
ac0c6bdd 296 if (!clock_class) {
5134570b 297 BT_LOGW_STR("Invalid parameter: clock class is NULL.");
87d76bb1
JG
298 goto end;
299 }
300
ac0c6bdd 301 ret = clock_class->precision;
87d76bb1
JG
302end:
303 return ret;
304}
305
50842bdc 306int bt_clock_class_set_precision(struct bt_clock_class *clock_class,
ac0c6bdd 307 uint64_t precision)
273b65be
JG
308{
309 int ret = 0;
310
5134570b
PP
311 if (!clock_class || precision == -1ULL) {
312 BT_LOGW("Invalid parameter: clock class is NULL or precision is invalid: "
2cf0acd7 313 "addr=%p, name=\"%s\", precision=%" PRIu64,
50842bdc 314 clock_class, bt_clock_class_get_name(clock_class),
2cf0acd7 315 precision);
5134570b
PP
316 ret = -1;
317 goto end;
318 }
319
320 if (clock_class->frozen) {
2cf0acd7 321 BT_LOGW("Invalid parameter: clock class is frozen: addr=%p, name=\"%s\"",
50842bdc 322 clock_class, bt_clock_class_get_name(clock_class));
273b65be
JG
323 ret = -1;
324 goto end;
325 }
326
ac0c6bdd 327 clock_class->precision = precision;
2cf0acd7 328 BT_LOGV("Set clock class's precision: addr=%p, name=\"%s\", precision=%" PRIu64,
50842bdc 329 clock_class, bt_clock_class_get_name(clock_class),
2cf0acd7 330 precision);
273b65be
JG
331end:
332 return ret;
333}
334
50842bdc 335int bt_clock_class_get_offset_s(struct bt_clock_class *clock_class,
ac0c6bdd 336 int64_t *offset_s)
87d76bb1 337{
61cf588b 338 int ret = 0;
87d76bb1 339
ac0c6bdd 340 if (!clock_class || !offset_s) {
5134570b 341 BT_LOGW("Invalid parameter: clock class or offset pointer is NULL: "
2cf0acd7 342 "clock-class-addr=%p, name=\"%s\", offset-addr=%p",
50842bdc 343 clock_class, bt_clock_class_get_name(clock_class),
2cf0acd7 344 offset_s);
61cf588b 345 ret = -1;
87d76bb1
JG
346 goto end;
347 }
348
ac0c6bdd 349 *offset_s = clock_class->offset_s;
87d76bb1
JG
350end:
351 return ret;
352}
353
50842bdc 354int bt_clock_class_set_offset_s(struct bt_clock_class *clock_class,
ac0c6bdd 355 int64_t offset_s)
273b65be
JG
356{
357 int ret = 0;
358
5134570b
PP
359 if (!clock_class) {
360 BT_LOGW_STR("Invalid parameter: clock class is NULL.");
361 ret = -1;
362 goto end;
363 }
364
365 if (clock_class->frozen) {
2cf0acd7 366 BT_LOGW("Invalid parameter: clock class is frozen: addr=%p, name=\"%s\"",
50842bdc 367 clock_class, bt_clock_class_get_name(clock_class));
273b65be
JG
368 ret = -1;
369 goto end;
370 }
371
ac0c6bdd 372 clock_class->offset_s = offset_s;
2cf0acd7
PP
373 BT_LOGV("Set clock class's offset (seconds): "
374 "addr=%p, name=\"%s\", offset-s=%" PRId64,
50842bdc 375 clock_class, bt_clock_class_get_name(clock_class),
2cf0acd7 376 offset_s);
273b65be
JG
377end:
378 return ret;
379}
380
50842bdc 381int bt_clock_class_get_offset_cycles(struct bt_clock_class *clock_class,
ac0c6bdd 382 int64_t *offset)
87d76bb1 383{
61cf588b 384 int ret = 0;
87d76bb1 385
ac0c6bdd 386 if (!clock_class || !offset) {
5134570b 387 BT_LOGW("Invalid parameter: clock class or offset pointer is NULL: "
2cf0acd7 388 "clock-class-addr=%p, name=\"%s\", offset-addr=%p",
50842bdc 389 clock_class, bt_clock_class_get_name(clock_class),
2cf0acd7 390 offset);
61cf588b 391 ret = -1;
87d76bb1
JG
392 goto end;
393 }
394
ac0c6bdd 395 *offset = clock_class->offset;
87d76bb1
JG
396end:
397 return ret;
398}
399
50842bdc 400int bt_clock_class_set_offset_cycles(struct bt_clock_class *clock_class,
ac0c6bdd 401 int64_t offset)
273b65be
JG
402{
403 int ret = 0;
404
5134570b
PP
405 if (!clock_class) {
406 BT_LOGW_STR("Invalid parameter: clock class is NULL.");
407 ret = -1;
408 goto end;
409 }
410
411 if (clock_class->frozen) {
2cf0acd7 412 BT_LOGW("Invalid parameter: clock class is frozen: addr=%p, name=\"%s\"",
50842bdc 413 clock_class, bt_clock_class_get_name(clock_class));
273b65be
JG
414 ret = -1;
415 goto end;
416 }
417
ac0c6bdd 418 clock_class->offset = offset;
2cf0acd7 419 BT_LOGV("Set clock class's offset (cycles): addr=%p, name=\"%s\", offset-cycles=%" PRId64,
50842bdc 420 clock_class, bt_clock_class_get_name(clock_class), offset);
273b65be
JG
421end:
422 return ret;
423}
424
50842bdc 425bt_bool bt_clock_class_is_absolute(struct bt_clock_class *clock_class)
87d76bb1
JG
426{
427 int ret = -1;
428
ac0c6bdd 429 if (!clock_class) {
5134570b 430 BT_LOGW_STR("Invalid parameter: clock class is NULL.");
87d76bb1
JG
431 goto end;
432 }
433
ac0c6bdd 434 ret = clock_class->absolute;
87d76bb1
JG
435end:
436 return ret;
437}
438
50842bdc 439int bt_clock_class_set_is_absolute(struct bt_clock_class *clock_class,
a2b94977 440 bt_bool is_absolute)
273b65be
JG
441{
442 int ret = 0;
443
5134570b
PP
444 if (!clock_class) {
445 BT_LOGW_STR("Invalid parameter: clock class is NULL.");
446 ret = -1;
447 goto end;
448 }
449
450 if (clock_class->frozen) {
2cf0acd7 451 BT_LOGW("Invalid parameter: clock class is frozen: addr=%p, name=\"%s\"",
50842bdc 452 clock_class, bt_clock_class_get_name(clock_class));
273b65be
JG
453 ret = -1;
454 goto end;
455 }
456
ac0c6bdd 457 clock_class->absolute = !!is_absolute;
2cf0acd7 458 BT_LOGV("Set clock class's absolute flag: addr=%p, name=\"%s\", is-absolute=%d",
50842bdc 459 clock_class, bt_clock_class_get_name(clock_class),
2cf0acd7 460 is_absolute);
273b65be
JG
461end:
462 return ret;
463}
464
50842bdc
PP
465const unsigned char *bt_clock_class_get_uuid(
466 struct bt_clock_class *clock_class)
85b743f4
JG
467{
468 const unsigned char *ret;
469
5134570b
PP
470 if (!clock_class) {
471 BT_LOGW_STR("Invalid parameter: clock class is NULL.");
472 ret = NULL;
473 goto end;
474 }
475
476 if (!clock_class->uuid_set) {
2cf0acd7 477 BT_LOGV("Clock class's UUID is not set: addr=%p, name=\"%s\"",
50842bdc 478 clock_class, bt_clock_class_get_name(clock_class));
85b743f4
JG
479 ret = NULL;
480 goto end;
481 }
482
ac0c6bdd 483 ret = clock_class->uuid;
85b743f4
JG
484end:
485 return ret;
486}
487
50842bdc 488int bt_clock_class_set_uuid(struct bt_clock_class *clock_class,
ac0c6bdd 489 const unsigned char *uuid)
85b743f4
JG
490{
491 int ret = 0;
492
5134570b
PP
493 if (!clock_class || !uuid) {
494 BT_LOGW("Invalid parameter: clock class or UUID is NULL: "
2cf0acd7 495 "clock-class-addr=%p, name=\"%s\", uuid-addr=%p",
50842bdc 496 clock_class, bt_clock_class_get_name(clock_class),
2cf0acd7 497 uuid);
5134570b
PP
498 ret = -1;
499 goto end;
500 }
501
502 if (clock_class->frozen) {
2cf0acd7 503 BT_LOGW("Invalid parameter: clock class is frozen: addr=%p, name=\"%s\"",
50842bdc 504 clock_class, bt_clock_class_get_name(clock_class));
85b743f4
JG
505 ret = -1;
506 goto end;
507 }
508
20eee76e 509 memcpy(clock_class->uuid, uuid, BABELTRACE_UUID_LEN);
ac0c6bdd 510 clock_class->uuid_set = 1;
2cf0acd7 511 BT_LOGV("Set clock class's UUID: addr=%p, name=\"%s\", "
5134570b 512 "uuid=\"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\"",
50842bdc 513 clock_class, bt_clock_class_get_name(clock_class),
5134570b
PP
514 (unsigned int) uuid[0],
515 (unsigned int) uuid[1],
516 (unsigned int) uuid[2],
517 (unsigned int) uuid[3],
518 (unsigned int) uuid[4],
519 (unsigned int) uuid[5],
520 (unsigned int) uuid[6],
521 (unsigned int) uuid[7],
522 (unsigned int) uuid[8],
523 (unsigned int) uuid[9],
524 (unsigned int) uuid[10],
525 (unsigned int) uuid[11],
526 (unsigned int) uuid[12],
527 (unsigned int) uuid[13],
528 (unsigned int) uuid[14],
529 (unsigned int) uuid[15]);
85b743f4
JG
530end:
531 return ret;
532}
533
312c056a
PP
534static inline
535uint64_t ns_from_value(uint64_t frequency, uint64_t value)
4ef18cab
PP
536{
537 uint64_t ns;
538
312c056a 539 if (frequency == UINT64_C(1000000000)) {
4ef18cab
PP
540 ns = value;
541 } else {
036a7cac
PP
542 double dblres = ((1e9 * (double) value) / (double) frequency);
543
544 if (dblres >= (double) UINT64_MAX) {
545 /* Overflows uint64_t */
546 ns = -1ULL;
547 } else {
548 ns = (uint64_t) dblres;
549 }
4ef18cab
PP
550 }
551
552 return ns;
553}
554
273b65be 555BT_HIDDEN
50842bdc 556void bt_clock_class_freeze(struct bt_clock_class *clock_class)
273b65be 557{
3dca2276 558 if (!clock_class || clock_class->frozen) {
273b65be
JG
559 return;
560 }
561
3dca2276
PP
562 BT_LOGD("Freezing clock class: addr=%p, name=\"%s\"",
563 clock_class, bt_clock_class_get_name(clock_class));
564 clock_class->frozen = 1;
273b65be
JG
565}
566
273b65be 567static
50842bdc 568void bt_clock_class_destroy(struct bt_object *obj)
273b65be 569{
50842bdc 570 struct bt_clock_class *clock_class;
273b65be 571
50842bdc 572 clock_class = container_of(obj, struct bt_clock_class, base);
2cf0acd7 573 BT_LOGD("Destroying clock class: addr=%p, name=\"%s\"",
50842bdc 574 obj, bt_clock_class_get_name(clock_class));
312c056a 575
ac0c6bdd
PP
576 if (clock_class->name) {
577 g_string_free(clock_class->name, TRUE);
273b65be 578 }
312c056a 579
ac0c6bdd
PP
580 if (clock_class->description) {
581 g_string_free(clock_class->description, TRUE);
273b65be
JG
582 }
583
312c056a 584 bt_object_pool_finalize(&clock_class->cv_pool);
ac0c6bdd 585 g_free(clock_class);
273b65be 586}
4ef18cab 587
61ec14e6 588static
312c056a 589void bt_clock_value_destroy(struct bt_clock_value *clock_value)
61ec14e6 590{
2cf0acd7 591 BT_LOGD("Destroying clock value: addr=%p, clock-class-addr=%p, "
312c056a
PP
592 "clock-class-name=\"%s\"", clock_value,
593 clock_value->clock_class,
594 bt_clock_class_get_name(clock_value->clock_class));
595 bt_put(clock_value->clock_class);
596 g_free(clock_value);
61ec14e6
JG
597}
598
312c056a
PP
599static inline
600int ns_from_epoch(struct bt_clock_class *clock_class, uint64_t value,
601 int64_t *ns_from_epoch, bool *overflows)
036a7cac 602{
312c056a 603 int ret = 0;
036a7cac
PP
604 int64_t diff;
605 int64_t s_ns;
606 uint64_t u_ns;
607 uint64_t cycles;
608
312c056a
PP
609 *overflows = false;
610
036a7cac 611 /* Initialize nanosecond timestamp to clock's offset in seconds */
312c056a
PP
612 if (clock_class->offset_s <= (INT64_MIN / INT64_C(1000000000)) ||
613 clock_class->offset_s >= (INT64_MAX / INT64_C(1000000000))) {
036a7cac
PP
614 /*
615 * Overflow: offset in seconds converted to nanoseconds
616 * is outside the int64_t range.
617 */
312c056a 618 *overflows = true;
036a7cac
PP
619 goto end;
620 }
621
312c056a 622 *ns_from_epoch = clock_class->offset_s * INT64_C(1000000000);
036a7cac
PP
623
624 /* Add offset in cycles */
625 if (clock_class->offset < 0) {
312c056a 626 cycles = (uint64_t) -clock_class->offset;
036a7cac
PP
627 } else {
628 cycles = (uint64_t) clock_class->offset;
629 }
630
631 u_ns = ns_from_value(clock_class->frequency, cycles);
632
312c056a 633 if (u_ns == UINT64_C(-1) || u_ns >= INT64_MAX) {
036a7cac
PP
634 /*
635 * Overflow: offset in cycles converted to nanoseconds
636 * is outside the int64_t range.
637 */
312c056a 638 *overflows = true;
036a7cac
PP
639 goto end;
640 }
641
642 s_ns = (int64_t) u_ns;
f6ccaed9 643 BT_ASSERT(s_ns >= 0);
036a7cac
PP
644
645 if (clock_class->offset < 0) {
312c056a 646 if (*ns_from_epoch >= 0) {
036a7cac
PP
647 /*
648 * Offset in cycles is negative so it must also
649 * be negative once converted to nanoseconds.
650 */
651 s_ns = -s_ns;
652 goto offset_ok;
653 }
654
312c056a 655 diff = *ns_from_epoch - INT64_MIN;
036a7cac
PP
656
657 if (s_ns >= diff) {
658 /*
659 * Overflow: current timestamp in nanoseconds
660 * plus the offset in cycles converted to
661 * nanoseconds is outside the int64_t range.
662 */
312c056a 663 *overflows = true;
036a7cac
PP
664 goto end;
665 }
666
667 /*
668 * Offset in cycles is negative so it must also be
669 * negative once converted to nanoseconds.
670 */
671 s_ns = -s_ns;
672 } else {
312c056a 673 if (*ns_from_epoch <= 0) {
036a7cac
PP
674 goto offset_ok;
675 }
676
312c056a 677 diff = INT64_MAX - *ns_from_epoch;
036a7cac
PP
678
679 if (s_ns >= diff) {
680 /*
681 * Overflow: current timestamp in nanoseconds
682 * plus the offset in cycles converted to
683 * nanoseconds is outside the int64_t range.
684 */
312c056a 685 *overflows = true;
036a7cac
PP
686 goto end;
687 }
688 }
689
690offset_ok:
312c056a 691 *ns_from_epoch += s_ns;
036a7cac
PP
692
693 /* Add clock value (cycles) */
312c056a 694 u_ns = ns_from_value(clock_class->frequency, value);
036a7cac
PP
695
696 if (u_ns == -1ULL || u_ns >= INT64_MAX) {
697 /*
698 * Overflow: value converted to nanoseconds is outside
699 * the int64_t range.
700 */
312c056a 701 *overflows = true;
036a7cac
PP
702 goto end;
703 }
704
705 s_ns = (int64_t) u_ns;
f6ccaed9 706 BT_ASSERT(s_ns >= 0);
036a7cac
PP
707
708 /* Clock value (cycles) is always positive */
312c056a 709 if (*ns_from_epoch <= 0) {
036a7cac
PP
710 goto value_ok;
711 }
712
312c056a 713 diff = INT64_MAX - *ns_from_epoch;
036a7cac
PP
714
715 if (s_ns >= diff) {
716 /*
717 * Overflow: current timestamp in nanoseconds plus the
718 * clock value converted to nanoseconds is outside the
719 * int64_t range.
720 */
312c056a 721 *overflows = true;
036a7cac
PP
722 goto end;
723 }
724
725value_ok:
312c056a 726 *ns_from_epoch += s_ns;
036a7cac
PP
727
728end:
312c056a
PP
729 if (*overflows) {
730 *ns_from_epoch = 0;
731 ret = -1;
036a7cac 732 }
312c056a
PP
733
734 return ret;
036a7cac
PP
735}
736
312c056a
PP
737static
738void set_ns_from_epoch(struct bt_clock_value *clock_value)
739{
740 (void) ns_from_epoch(clock_value->clock_class,
741 clock_value->value, &clock_value->ns_from_epoch,
742 &clock_value->ns_from_epoch_overflows);
743}
744
745static
746struct bt_clock_value *bt_clock_value_new(struct bt_clock_class *clock_class)
4ef18cab 747{
50842bdc 748 struct bt_clock_value *ret = NULL;
4ef18cab 749
5134570b 750 BT_LOGD("Creating clock value object: clock-class-addr=%p, "
312c056a
PP
751 "clock-class-name=\"%s\"", clock_class,
752 bt_clock_class_get_name(clock_class));
5134570b 753
ac0c6bdd 754 if (!clock_class) {
5134570b 755 BT_LOGW_STR("Invalid parameter: clock class is NULL.");
4ef18cab
PP
756 goto end;
757 }
758
50842bdc 759 ret = g_new0(struct bt_clock_value, 1);
61ec14e6 760 if (!ret) {
5134570b 761 BT_LOGE_STR("Failed to allocate one clock value.");
61ec14e6
JG
762 goto end;
763 }
764
3fea54f6 765 bt_object_init_unique(&ret->base);
ac0c6bdd 766 ret->clock_class = bt_get(clock_class);
50842bdc 767 bt_clock_class_freeze(clock_class);
2cf0acd7 768 BT_LOGD("Created clock value object: clock-value-addr=%p, "
036a7cac
PP
769 "clock-class-addr=%p, clock-class-name=\"%s\", "
770 "ns-from-epoch=%" PRId64 ", ns-from-epoch-overflows=%d",
50842bdc 771 ret, clock_class, bt_clock_class_get_name(clock_class),
036a7cac
PP
772 ret->ns_from_epoch, ret->ns_from_epoch_overflows);
773
61ec14e6
JG
774end:
775 return ret;
776}
4ef18cab 777
312c056a
PP
778BT_HIDDEN
779struct bt_clock_value *bt_clock_value_create(struct bt_clock_class *clock_class)
780{
781 struct bt_clock_value *clock_value = NULL;
782
783 BT_ASSERT(clock_class);
784 clock_value = bt_object_pool_create_object(&clock_class->cv_pool);
785 if (!clock_value) {
786 BT_LIB_LOGE("Cannot allocate one clock value from clock class's clock value pool: "
787 "%![cc-]+K", clock_class);
788 goto error;
789 }
790
791 if (!clock_value->clock_class) {
792 clock_value->clock_class = bt_get(clock_class);
793 }
794
795 goto end;
796
797error:
798 if (clock_value) {
799 bt_clock_value_recycle(clock_value);
800 clock_value = NULL;
801 }
802
803end:
804 return clock_value;
805}
806
807BT_HIDDEN
808void bt_clock_value_recycle(struct bt_clock_value *clock_value)
809{
810 struct bt_clock_class *clock_class;
811
812 BT_ASSERT(clock_value);
813 BT_LIB_LOGD("Recycling clock value: %!+k", clock_value);
814
815 /*
816 * Those are the important ordered steps:
817 *
818 * 1. Reset the clock value object, but do NOT put its clock
819 * class's reference. This clock class contains the pool to
820 * which we're about to recycle this clock value object, so
821 * we must guarantee its existence thanks to this existing
822 * reference.
823 *
824 * 2. Move the clock class reference to our `clock_class`
825 * variable so that we can set the clock value's clock class
826 * member to NULL before recycling it. We CANNOT do this
827 * after we put the clock class reference because this
828 * bt_put() could destroy the clock class, also destroying
829 * its clock value pool, thus also destroying our clock value
830 * object (this would result in an invalid write access).
831 *
832 * 3. Recycle the clock value object.
833 *
834 * 4. Put our clock class reference.
835 */
836 bt_clock_value_reset(clock_value);
837 bt_clock_value_set_is_frozen(clock_value, false);
838 clock_class = clock_value->clock_class;
839 BT_ASSERT(clock_class);
840 clock_value->clock_class = NULL;
841 bt_object_pool_recycle_object(&clock_class->cv_pool, clock_value);
842 bt_put(clock_class);
843}
844
845BT_HIDDEN
846void bt_clock_value_set_raw_value(struct bt_clock_value *clock_value,
847 uint64_t cycles)
848{
849 BT_ASSERT(clock_value);
850
851 clock_value->value = cycles;
852 set_ns_from_epoch(clock_value);
853 bt_clock_value_set(clock_value);
854}
855
856int bt_clock_value_set_value(struct bt_clock_value *clock_value,
857 uint64_t raw_value)
858{
859 BT_ASSERT_PRE_NON_NULL(clock_value, "Clock value");
860 BT_ASSERT_PRE_HOT(clock_value, "Clock value", ": %!+k", clock_value);
861 bt_clock_value_set_raw_value(clock_value, raw_value);
862 return 0;
863}
864
865int bt_clock_value_get_value(struct bt_clock_value *clock_value,
866 uint64_t *raw_value)
61ec14e6
JG
867{
868 int ret = 0;
4ef18cab 869
61ec14e6 870 if (!clock_value || !raw_value) {
5134570b
PP
871 BT_LOGW("Invalid parameter: clock value or raw value is NULL: "
872 "clock-value-addr=%p, raw-value-addr=%p",
873 clock_value, raw_value);
61ec14e6
JG
874 ret = -1;
875 goto end;
876 }
4ef18cab 877
61ec14e6 878 *raw_value = clock_value->value;
4ef18cab 879end:
61ec14e6
JG
880 return ret;
881}
882
50842bdc 883int bt_clock_value_get_value_ns_from_epoch(struct bt_clock_value *value,
61ec14e6
JG
884 int64_t *ret_value_ns)
885{
886 int ret = 0;
61ec14e6
JG
887
888 if (!value || !ret_value_ns) {
5134570b
PP
889 BT_LOGW("Invalid parameter: clock value or return value pointer is NULL: "
890 "clock-value-addr=%p, ret-value-addr=%p",
891 value, ret_value_ns);
61ec14e6
JG
892 ret = -1;
893 goto end;
894 }
895
036a7cac
PP
896 if (value->ns_from_epoch_overflows) {
897 BT_LOGW("Clock value converted to nanoseconds from Epoch overflows the signed 64-bit integer range: "
898 "clock-value-addr=%p, "
899 "clock-class-offset-s=%" PRId64 ", "
900 "clock-class-offset-cycles=%" PRId64 ", "
901 "value=%" PRIu64,
902 value, value->clock_class->offset_s,
903 value->clock_class->offset,
904 value->value);
905 ret = -1;
906 goto end;
907 }
61ec14e6 908
036a7cac 909 *ret_value_ns = value->ns_from_epoch;
61ec14e6 910
61ec14e6
JG
911end:
912 return ret;
4ef18cab 913}
6f57e458 914
094ff7c0 915struct bt_clock_class *bt_clock_value_borrow_class(
50842bdc 916 struct bt_clock_value *clock_value)
6f57e458 917{
50842bdc 918 struct bt_clock_class *clock_class = NULL;
6f57e458
PP
919
920 if (!clock_value) {
5134570b 921 BT_LOGW_STR("Invalid parameter: clock value is NULL.");
6f57e458
PP
922 goto end;
923 }
924
094ff7c0 925 clock_class = clock_value->clock_class;
6f57e458
PP
926
927end:
928 return clock_class;
929}
93dda901
PP
930
931BT_HIDDEN
932int bt_clock_class_compare(struct bt_clock_class *clock_class_a,
933 struct bt_clock_class *clock_class_b)
934{
935 int ret = 1;
f6ccaed9
PP
936 BT_ASSERT(clock_class_a);
937 BT_ASSERT(clock_class_b);
93dda901
PP
938
939 /* Name */
940 if (strcmp(clock_class_a->name->str, clock_class_b->name->str) != 0) {
941 BT_LOGV("Clock classes differ: different names: "
942 "cc-a-name=\"%s\", cc-b-name=\"%s\"",
943 clock_class_a->name->str,
944 clock_class_b->name->str);
945 goto end;
946 }
947
948 /* Description */
949 if (clock_class_a->description) {
950 if (!clock_class_b->description) {
951 BT_LOGV_STR("Clock classes differ: clock class A has a "
952 "description, but clock class B does not.");
953 goto end;
954 }
955
956 if (strcmp(clock_class_a->name->str, clock_class_b->name->str)
957 != 0) {
958 BT_LOGV("Clock classes differ: different descriptions: "
959 "cc-a-descr=\"%s\", cc-b-descr=\"%s\"",
960 clock_class_a->description->str,
961 clock_class_b->description->str);
962 goto end;
963 }
964 } else {
965 if (clock_class_b->description) {
966 BT_LOGV_STR("Clock classes differ: clock class A has "
967 "no description, but clock class B has one.");
968 goto end;
969 }
970 }
971
972 /* Frequency */
973 if (clock_class_a->frequency != clock_class_b->frequency) {
974 BT_LOGV("Clock classes differ: different frequencies: "
975 "cc-a-freq=%" PRIu64 ", cc-b-freq=%" PRIu64,
976 clock_class_a->frequency,
977 clock_class_b->frequency);
978 goto end;
979 }
980
981 /* Precision */
982 if (clock_class_a->precision != clock_class_b->precision) {
983 BT_LOGV("Clock classes differ: different precisions: "
984 "cc-a-freq=%" PRIu64 ", cc-b-freq=%" PRIu64,
985 clock_class_a->precision,
986 clock_class_b->precision);
987 goto end;
988 }
989
990 /* Offset (seconds) */
991 if (clock_class_a->offset_s != clock_class_b->offset_s) {
992 BT_LOGV("Clock classes differ: different offsets (seconds): "
993 "cc-a-offset-s=%" PRId64 ", cc-b-offset-s=%" PRId64,
994 clock_class_a->offset_s,
995 clock_class_b->offset_s);
996 goto end;
997 }
998
999 /* Offset (cycles) */
1000 if (clock_class_a->offset != clock_class_b->offset) {
1001 BT_LOGV("Clock classes differ: different offsets (cycles): "
1002 "cc-a-offset-s=%" PRId64 ", cc-b-offset-s=%" PRId64,
1003 clock_class_a->offset,
1004 clock_class_b->offset);
1005 goto end;
1006 }
1007
1008 /* UUIDs */
1009 if (clock_class_a->uuid_set) {
1010 if (!clock_class_b->uuid_set) {
1011 BT_LOGV_STR("Clock classes differ: clock class A has a "
1012 "UUID, but clock class B does not.");
1013 goto end;
1014 }
1015
1016 if (memcmp(clock_class_a->uuid, clock_class_b->uuid,
1017 BABELTRACE_UUID_LEN) != 0) {
1018 BT_LOGV("Clock classes differ: different UUIDs: "
1019 "cc-a-uuid=\"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\", "
1020 "cc-b-uuid=\"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\"",
1021 (unsigned int) clock_class_a->uuid[0],
1022 (unsigned int) clock_class_a->uuid[1],
1023 (unsigned int) clock_class_a->uuid[2],
1024 (unsigned int) clock_class_a->uuid[3],
1025 (unsigned int) clock_class_a->uuid[4],
1026 (unsigned int) clock_class_a->uuid[5],
1027 (unsigned int) clock_class_a->uuid[6],
1028 (unsigned int) clock_class_a->uuid[7],
1029 (unsigned int) clock_class_a->uuid[8],
1030 (unsigned int) clock_class_a->uuid[9],
1031 (unsigned int) clock_class_a->uuid[10],
1032 (unsigned int) clock_class_a->uuid[11],
1033 (unsigned int) clock_class_a->uuid[12],
1034 (unsigned int) clock_class_a->uuid[13],
1035 (unsigned int) clock_class_a->uuid[14],
1036 (unsigned int) clock_class_a->uuid[15],
1037 (unsigned int) clock_class_b->uuid[0],
1038 (unsigned int) clock_class_b->uuid[1],
1039 (unsigned int) clock_class_b->uuid[2],
1040 (unsigned int) clock_class_b->uuid[3],
1041 (unsigned int) clock_class_b->uuid[4],
1042 (unsigned int) clock_class_b->uuid[5],
1043 (unsigned int) clock_class_b->uuid[6],
1044 (unsigned int) clock_class_b->uuid[7],
1045 (unsigned int) clock_class_b->uuid[8],
1046 (unsigned int) clock_class_b->uuid[9],
1047 (unsigned int) clock_class_b->uuid[10],
1048 (unsigned int) clock_class_b->uuid[11],
1049 (unsigned int) clock_class_b->uuid[12],
1050 (unsigned int) clock_class_b->uuid[13],
1051 (unsigned int) clock_class_b->uuid[14],
1052 (unsigned int) clock_class_b->uuid[15]);
1053 goto end;
1054 }
1055 } else {
1056 if (clock_class_b->uuid_set) {
1057 BT_LOGV_STR("Clock classes differ: clock class A has "
1058 "no UUID, but clock class B has one.");
1059 goto end;
1060 }
1061 }
1062
1063 /* Absolute */
1064 if (!!clock_class_a->absolute != !!clock_class_b->absolute) {
1065 BT_LOGV("Clock classes differ: one is absolute, the other "
1066 "is not: cc-a-is-absolute=%d, cc-b-is-absolute=%d",
1067 !!clock_class_a->absolute,
1068 !!clock_class_b->absolute);
1069 goto end;
1070 }
1071
1072 /* Equal */
1073 ret = 0;
1074
1075end:
1076 return ret;
1077}
312c056a
PP
1078
1079int bt_clock_class_cycles_to_ns(struct bt_clock_class *clock_class,
1080 uint64_t cycles, int64_t *ns)
1081{
1082 int ret;
1083 bool overflows;
1084
1085 BT_ASSERT_PRE_NON_NULL(clock_class, "Clock class");
1086 BT_ASSERT_PRE_NON_NULL(ns, "Nanoseconds");
1087
1088 ret = ns_from_epoch(clock_class, cycles, ns, &overflows);
1089 if (ret) {
1090 if (overflows) {
1091 BT_LIB_LOGW("Cannot convert cycles to nanoseconds "
1092 "from Epoch for given clock class: "
1093 "value overflow: %![cc-]+K, cycles=%" PRIu64,
1094 clock_class, cycles);
1095 } else {
1096 BT_LIB_LOGW("Cannot convert cycles to nanoseconds "
1097 "from Epoch for given clock class: "
1098 "%![cc-]+K, cycles=%" PRIu64,
1099 clock_class, cycles);
1100 }
1101
1102 goto end;
1103 }
1104
1105end:
1106 return ret;
1107}
This page took 0.096229 seconds and 4 git commands to generate.