| 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 | |
| 23 | #define BT_LOG_TAG "LIB/UTIL" |
| 24 | #include "lib/logging.h" |
| 25 | |
| 26 | #include "lib/assert-pre.h" |
| 27 | #include <stdbool.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
| 30 | #include <inttypes.h> |
| 31 | #include <babeltrace2/babeltrace.h> |
| 32 | #include "lib/trace-ir/utils.h" |
| 33 | |
| 34 | bt_util_clock_cycles_to_ns_from_origin_status |
| 35 | bt_util_clock_cycles_to_ns_from_origin(uint64_t cycles, |
| 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; |
| 41 | bt_util_clock_cycles_to_ns_from_origin_status status = |
| 42 | BT_FUNC_STATUS_OK; |
| 43 | int ret; |
| 44 | |
| 45 | BT_ASSERT_PRE_NO_ERROR(); |
| 46 | BT_ASSERT_PRE_NON_NULL(ns, "Nanoseconds (output)"); |
| 47 | BT_ASSERT_PRE(frequency != UINT64_C(-1) && frequency != 0, |
| 48 | "Invalid frequency: freq=%" PRIu64, frequency); |
| 49 | BT_ASSERT_PRE(offset_cycles < frequency, |
| 50 | "Offset (cycles) is greater than frequency: " |
| 51 | "offset-cycles=%" PRIu64 ", freq=%" PRIu64, |
| 52 | offset_cycles, frequency); |
| 53 | |
| 54 | overflows = bt_util_get_base_offset_ns(offset_seconds, offset_cycles, |
| 55 | frequency, &base_offset_ns); |
| 56 | if (overflows) { |
| 57 | status = BT_FUNC_STATUS_OVERFLOW_ERROR; |
| 58 | goto end; |
| 59 | } |
| 60 | |
| 61 | ret = bt_util_ns_from_origin_inline(base_offset_ns, |
| 62 | offset_seconds, offset_cycles, |
| 63 | frequency, cycles, ns); |
| 64 | if (ret) { |
| 65 | status = BT_FUNC_STATUS_OVERFLOW_ERROR; |
| 66 | } |
| 67 | |
| 68 | end: |
| 69 | return status; |
| 70 | } |