Commit | Line | Data |
---|---|---|
0f2d58c9 PP |
1 | /* |
2 | * Copyright (c) 2015-2018 Philippe Proulx <pproulx@efficios.com> | |
3 | * | |
4 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
5 | * of this software and associated documentation files (the "Software"), to deal | |
6 | * in the Software without restriction, including without limitation the rights | |
7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
8 | * copies of the Software, and to permit persons to whom the Software is | |
9 | * furnished to do so, subject to the following conditions: | |
10 | * | |
11 | * The above copyright notice and this permission notice shall be included in | |
12 | * all copies or substantial portions of the Software. | |
13 | * | |
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
20 | * SOFTWARE. | |
21 | */ | |
22 | ||
350ad6c1 | 23 | #define BT_LOG_TAG "LIB/UTIL" |
c2d9d9cf | 24 | #include "lib/logging.h" |
0f2d58c9 | 25 | |
578e048b | 26 | #include "lib/assert-pre.h" |
c4f23e30 | 27 | #include <stdbool.h> |
0f2d58c9 PP |
28 | #include <stdlib.h> |
29 | #include <string.h> | |
0f2d58c9 | 30 | #include <inttypes.h> |
4fa90f32 | 31 | #include <babeltrace2/babeltrace.h> |
578e048b | 32 | #include "lib/trace-ir/utils.h" |
0f2d58c9 | 33 | |
c4c4586d PP |
34 | bt_util_clock_cycles_to_ns_from_origin_status |
35 | bt_util_clock_cycles_to_ns_from_origin(uint64_t cycles, | |
0f2d58c9 PP |
36 | uint64_t frequency, int64_t offset_seconds, |
37 | uint64_t offset_cycles, int64_t *ns) | |
38 | { | |
39 | bool overflows; | |
40 | int64_t base_offset_ns; | |
c4c4586d PP |
41 | bt_util_clock_cycles_to_ns_from_origin_status status = |
42 | BT_FUNC_STATUS_OK; | |
0f2d58c9 PP |
43 | int ret; |
44 | ||
45 | BT_ASSERT_PRE_NON_NULL(ns, "Nanoseconds (output)"); | |
46 | BT_ASSERT_PRE(frequency != UINT64_C(-1) && frequency != 0, | |
47 | "Invalid frequency: freq=%" PRIu64, frequency); | |
48 | BT_ASSERT_PRE(offset_cycles < frequency, | |
49 | "Offset (cycles) is greater than frequency: " | |
50 | "offset-cycles=%" PRIu64 ", freq=%" PRIu64, | |
51 | offset_cycles, frequency); | |
52 | ||
53 | overflows = bt_util_get_base_offset_ns(offset_seconds, offset_cycles, | |
54 | frequency, &base_offset_ns); | |
55 | if (overflows) { | |
c4c4586d | 56 | status = BT_FUNC_STATUS_OVERFLOW_ERROR; |
0f2d58c9 PP |
57 | goto end; |
58 | } | |
59 | ||
60 | ret = bt_util_ns_from_origin_inline(base_offset_ns, | |
61 | offset_seconds, offset_cycles, | |
62 | frequency, cycles, ns); | |
63 | if (ret) { | |
c4c4586d | 64 | status = BT_FUNC_STATUS_OVERFLOW_ERROR; |
0f2d58c9 PP |
65 | } |
66 | ||
67 | end: | |
68 | return status; | |
69 | } |