DPDK 22.11.5
rte_time.h
1/* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2015 Intel Corporation
3 */
4
5#ifndef _RTE_TIME_H_
6#define _RTE_TIME_H_
7
8#ifdef __cplusplus
9extern "C" {
10#endif
11
12#include <stdint.h>
13#include <time.h>
14
15#define NSEC_PER_SEC 1000000000L
16
23 uint64_t cycle_last;
25 uint64_t nsec;
27 uint64_t nsec_mask;
29 uint64_t nsec_frac;
31 uint64_t cc_mask;
33 uint32_t cc_shift;
34};
35
39static inline uint64_t
40rte_cyclecounter_cycles_to_ns(struct rte_timecounter *tc, uint64_t cycles)
41{
42 uint64_t ns;
43
44 /* Add fractional nanoseconds. */
45 ns = cycles + tc->nsec_frac;
46 tc->nsec_frac = ns & tc->nsec_mask;
47
48 /* Shift to get only nanoseconds. */
49 return ns >> tc->cc_shift;
50}
51
55static inline uint64_t
56rte_timecounter_update(struct rte_timecounter *tc, uint64_t cycle_now)
57{
58 uint64_t cycle_delta, ns_offset;
59
60 /* Calculate the delta since the last call. */
61 if (tc->cycle_last <= cycle_now)
62 cycle_delta = (cycle_now - tc->cycle_last) & tc->cc_mask;
63 else
64 /* Handle cycle counts that have wrapped around . */
65 cycle_delta = (~(tc->cycle_last - cycle_now) & tc->cc_mask) + 1;
66
67 /* Convert to nanoseconds. */
68 ns_offset = rte_cyclecounter_cycles_to_ns(tc, cycle_delta);
69
70 /* Store current cycle counter for next call. */
71 tc->cycle_last = cycle_now;
72
73 /* Update the nanosecond count. */
74 tc->nsec += ns_offset;
75
76 return tc->nsec;
77}
78
82static inline uint64_t
83rte_timespec_to_ns(const struct timespec *ts)
84{
85 return ((uint64_t) ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec;
86}
87
91static inline struct timespec
92rte_ns_to_timespec(uint64_t nsec)
93{
94 struct timespec ts = {0, 0};
95
96 if (nsec == 0)
97 return ts;
98
99 ts.tv_sec = nsec / NSEC_PER_SEC;
100 ts.tv_nsec = nsec % NSEC_PER_SEC;
101
102 return ts;
103}
104
105#ifdef __cplusplus
106}
107#endif
108
109#endif /* _RTE_TIME_H_ */
uint64_t cycle_last
Definition: rte_time.h:23
uint64_t nsec_mask
Definition: rte_time.h:27
uint32_t cc_shift
Definition: rte_time.h:33
uint64_t nsec_frac
Definition: rte_time.h:29
uint64_t nsec
Definition: rte_time.h:25
uint64_t cc_mask
Definition: rte_time.h:31