e52a956c94b7b99cf646b913127f0e7fa7d824f5
[babeltrace.git] / src / lib / util.c
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright (c) 2015-2018 Philippe Proulx <pproulx@efficios.com>
5 */
6
7 #define BT_LOG_TAG "LIB/UTIL"
8 #include "lib/logging.h"
9
10 #include "lib/assert-pre.h"
11 #include <stdbool.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <inttypes.h>
15 #include <babeltrace2/babeltrace.h>
16 #include "lib/trace-ir/utils.h"
17
18 bt_util_clock_cycles_to_ns_from_origin_status
19 bt_util_clock_cycles_to_ns_from_origin(uint64_t cycles,
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;
25 bt_util_clock_cycles_to_ns_from_origin_status status =
26 BT_FUNC_STATUS_OK;
27 int ret;
28
29 BT_ASSERT_PRE_NO_ERROR();
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) {
41 status = BT_FUNC_STATUS_OVERFLOW_ERROR;
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) {
49 status = BT_FUNC_STATUS_OVERFLOW_ERROR;
50 }
51
52 end:
53 return status;
54 }
This page took 0.030337 seconds and 3 git commands to generate.