1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2015 Intel Corporation
11 #define NSEC_PER_SEC 1000000000L
14 * Structure to hold the parameters of a running cycle counter to assist
15 * in converting cycles to nanoseconds.
17 struct rte_timecounter {
18 /** Last cycle counter value read. */
20 /** Nanoseconds count. */
22 /** Bitmask separating nanosecond and sub-nanoseconds. */
24 /** Sub-nanoseconds count. */
26 /** Bitmask for two's complement subtraction of non-64 bit counters. */
28 /** Cycle to nanosecond divisor (power of two). */
33 * Converts cyclecounter cycles to nanoseconds.
35 static inline uint64_t
36 rte_cyclecounter_cycles_to_ns(struct rte_timecounter *tc, uint64_t cycles)
40 /* Add fractional nanoseconds. */
41 ns = cycles + tc->nsec_frac;
42 tc->nsec_frac = ns & tc->nsec_mask;
44 /* Shift to get only nanoseconds. */
45 return ns >> tc->cc_shift;
49 * Update the internal nanosecond count in the structure.
51 static inline uint64_t
52 rte_timecounter_update(struct rte_timecounter *tc, uint64_t cycle_now)
54 uint64_t cycle_delta, ns_offset;
56 /* Calculate the delta since the last call. */
57 if (tc->cycle_last <= cycle_now)
58 cycle_delta = (cycle_now - tc->cycle_last) & tc->cc_mask;
60 /* Handle cycle counts that have wrapped around . */
61 cycle_delta = (~(tc->cycle_last - cycle_now) & tc->cc_mask) + 1;
63 /* Convert to nanoseconds. */
64 ns_offset = rte_cyclecounter_cycles_to_ns(tc, cycle_delta);
66 /* Store current cycle counter for next call. */
67 tc->cycle_last = cycle_now;
69 /* Update the nanosecond count. */
70 tc->nsec += ns_offset;
76 * Convert from timespec structure into nanosecond units.
78 static inline uint64_t
79 rte_timespec_to_ns(const struct timespec *ts)
81 return ((uint64_t) ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec;
85 * Convert from nanosecond units into timespec structure.
87 static inline struct timespec
88 rte_ns_to_timespec(uint64_t nsec)
90 struct timespec ts = {0, 0};
95 ts.tv_sec = nsec / NSEC_PER_SEC;
96 ts.tv_nsec = nsec % NSEC_PER_SEC;
101 #endif /* _RTE_TIME_H_ */