DPDK  18.05.1
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 #include <stdint.h>
9 #include <time.h>
10 
11 #define NSEC_PER_SEC 1000000000L
12 
19  uint64_t cycle_last;
21  uint64_t nsec;
23  uint64_t nsec_mask;
25  uint64_t nsec_frac;
27  uint64_t cc_mask;
29  uint32_t cc_shift;
30 };
31 
35 static inline uint64_t
36 rte_cyclecounter_cycles_to_ns(struct rte_timecounter *tc, uint64_t cycles)
37 {
38  uint64_t ns;
39 
40  /* Add fractional nanoseconds. */
41  ns = cycles + tc->nsec_frac;
42  tc->nsec_frac = ns & tc->nsec_mask;
43 
44  /* Shift to get only nanoseconds. */
45  return ns >> tc->cc_shift;
46 }
47 
51 static inline uint64_t
52 rte_timecounter_update(struct rte_timecounter *tc, uint64_t cycle_now)
53 {
54  uint64_t cycle_delta, ns_offset;
55 
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;
59  else
60  /* Handle cycle counts that have wrapped around . */
61  cycle_delta = (~(tc->cycle_last - cycle_now) & tc->cc_mask) + 1;
62 
63  /* Convert to nanoseconds. */
64  ns_offset = rte_cyclecounter_cycles_to_ns(tc, cycle_delta);
65 
66  /* Store current cycle counter for next call. */
67  tc->cycle_last = cycle_now;
68 
69  /* Update the nanosecond count. */
70  tc->nsec += ns_offset;
71 
72  return tc->nsec;
73 }
74 
78 static inline uint64_t
79 rte_timespec_to_ns(const struct timespec *ts)
80 {
81  return ((uint64_t) ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec;
82 }
83 
87 static inline struct timespec
88 rte_ns_to_timespec(uint64_t nsec)
89 {
90  struct timespec ts = {0, 0};
91 
92  if (nsec == 0)
93  return ts;
94 
95  ts.tv_sec = nsec / NSEC_PER_SEC;
96  ts.tv_nsec = nsec % NSEC_PER_SEC;
97 
98  return ts;
99 }
100 
101 #endif /* _RTE_TIME_H_ */