Commit | Line | Data |
---|---|---|
0f2d58c9 | 1 | /* |
0235b0db | 2 | * SPDX-License-Identifier: MIT |
0f2d58c9 | 3 | * |
0235b0db | 4 | * Copyright (c) 2015-2018 Philippe Proulx <pproulx@efficios.com> |
0f2d58c9 PP |
5 | */ |
6 | ||
350ad6c1 | 7 | #define BT_LOG_TAG "LIB/UTIL" |
c2d9d9cf | 8 | #include "lib/logging.h" |
0f2d58c9 | 9 | |
d98421f2 | 10 | #include "lib/assert-cond.h" |
c4f23e30 | 11 | #include <stdbool.h> |
0f2d58c9 PP |
12 | #include <stdlib.h> |
13 | #include <string.h> | |
0f2d58c9 | 14 | #include <inttypes.h> |
4fa90f32 | 15 | #include <babeltrace2/babeltrace.h> |
578e048b | 16 | #include "lib/trace-ir/utils.h" |
0f2d58c9 | 17 | |
c4c4586d PP |
18 | bt_util_clock_cycles_to_ns_from_origin_status |
19 | bt_util_clock_cycles_to_ns_from_origin(uint64_t cycles, | |
0f2d58c9 PP |
20 | uint64_t frequency, int64_t offset_seconds, |
21 | uint64_t offset_cycles, int64_t *ns) | |
22 | { | |
23 | bool overflows; | |
24 | int64_t base_offset_ns; | |
c4c4586d PP |
25 | bt_util_clock_cycles_to_ns_from_origin_status status = |
26 | BT_FUNC_STATUS_OK; | |
0f2d58c9 PP |
27 | int ret; |
28 | ||
17f3083a | 29 | BT_ASSERT_PRE_NO_ERROR(); |
0f2d58c9 PP |
30 | BT_ASSERT_PRE_NON_NULL(ns, "Nanoseconds (output)"); |
31 | BT_ASSERT_PRE(frequency != UINT64_C(-1) && frequency != 0, | |
32 | "Invalid frequency: freq=%" PRIu64, frequency); | |
33 | BT_ASSERT_PRE(offset_cycles < frequency, | |
34 | "Offset (cycles) is greater than frequency: " | |
35 | "offset-cycles=%" PRIu64 ", freq=%" PRIu64, | |
36 | offset_cycles, frequency); | |
37 | ||
38 | overflows = bt_util_get_base_offset_ns(offset_seconds, offset_cycles, | |
39 | frequency, &base_offset_ns); | |
40 | if (overflows) { | |
c4c4586d | 41 | status = BT_FUNC_STATUS_OVERFLOW_ERROR; |
0f2d58c9 PP |
42 | goto end; |
43 | } | |
44 | ||
45 | ret = bt_util_ns_from_origin_inline(base_offset_ns, | |
46 | offset_seconds, offset_cycles, | |
47 | frequency, cycles, ns); | |
48 | if (ret) { | |
c4c4586d | 49 | status = BT_FUNC_STATUS_OVERFLOW_ERROR; |
0f2d58c9 PP |
50 | } |
51 | ||
52 | end: | |
53 | return status; | |
54 | } |