EM-ODP  3.7.0
Event Machine on ODP
env_time.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020, Nokia Solutions and Networks
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * * Neither the name of the copyright holder nor the names of its
15  * contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 /**
32  * @file
33  * Env time functions - don't include this file directly,
34  * instead include "environment.h"
35  */
36 
37 #ifndef _ENV_TIME_H_
38 #define _ENV_TIME_H_
39 
40 #pragma GCC visibility push(default)
41 
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45 
46 /* type for time stamp */
47 typedef odp_time_t env_time_t;
48 
49 /* Zero time stamp */
50 #define ENV_TIME_NULL ODP_TIME_NULL
51 
52 #define ENV_TIME_MSEC_IN_NS ODP_TIME_MSEC_IN_NS
53 
54 /**
55  * Returns current local time stamp value.
56  * Local time is thread specific and must not be shared between threads.
57  *
58  * @return Local time stamp
59  */
60 static inline env_time_t env_time_local(void)
61 {
62  return odp_time_local();
63 }
64 
65 /**
66  * Returns current global time stamp value.
67  * Global time can be shared between threads.
68  *
69  * @return Global time stamp
70  */
71 static inline env_time_t env_time_global(void)
72 {
73  return odp_time_global();
74 }
75 
76 /**
77  * Returns difference of time stamps (time2 - time1).
78  *
79  * @param time2 Second time stamp
80  * @param time1 First time stamp
81  *
82  * @return Difference between given time stamps
83  */
84 static inline env_time_t env_time_diff(env_time_t time2, env_time_t time1)
85 {
86  return odp_time_diff(time2, time1);
87 }
88 
89 /**
90  * Returns difference of time stamps (time2 - time1) in nanoseconds.
91  *
92  * @param time2 Second time stamp
93  * @param time1 First time stamp
94  *
95  * @return Difference between given time stamps in nanoseconds
96  */
97 static inline uint64_t env_time_diff_ns(env_time_t time2, env_time_t time1)
98 {
99  return odp_time_diff_ns(time2, time1);
100 }
101 
102 /**
103  * Sum of time stamps
104  *
105  * @param time1 First time stamp
106  * @param time2 Second time stamp
107  *
108  * @return Sum of given time stamps
109  */
110 static inline env_time_t env_time_sum(env_time_t time1, env_time_t time2)
111 {
112  return odp_time_sum(time1, time2);
113 }
114 
115 /**
116  * Convert nanoseconds to local time.
117  *
118  * @param ns Time in nanoseconds
119  *
120  * @return Local time stamp
121  */
122 static inline env_time_t env_time_local_from_ns(uint64_t ns)
123 {
124  return odp_time_local_from_ns(ns);
125 }
126 
127 /**
128  * Convert nanoseconds to global time
129  *
130  * @param ns Time in nanoseconds
131  *
132  * @return Global time stamp
133  */
134 static inline env_time_t env_time_global_from_ns(uint64_t ns)
135 {
136  return odp_time_global_from_ns(ns);
137 }
138 
139 /**
140  * Compare time stamps.
141  *
142  * @param time2 Second time stamp
143  * @param time1 First time stamp
144  *
145  * @retval <0 when time2 < time1
146  * @retval 0 when time2 == time1
147  * @retval >0 when time2 > time1
148  */
149 static inline int env_time_cmp(env_time_t time2, env_time_t time1)
150 {
151  return odp_time_cmp(time2, time1);
152 }
153 
154  /**
155  * Convert time stamp to nanoseconds
156  *
157  * @param time Time stamp
158  *
159  * @return time in nanoseconds
160  */
161 static inline uint64_t env_time_to_ns(env_time_t time)
162 {
163  return odp_time_to_ns(time);
164 }
165 
166 /**
167  * Convert time stamp to cpu cycles
168  *
169  * @param time Time stamp
170  * @param hz CPU Hz
171  *
172  * @return cpu cycles
173  */
174 #ifdef __SIZEOF_INT128__
175 
176 static inline uint64_t env_time_to_cycles(env_time_t time, uint64_t hz)
177 {
178  __uint128_t cycles;
179 
180  cycles = (__uint128_t)odp_time_to_ns(time) * hz / 1000000000;
181  return (uint64_t)cycles;
182 }
183 #else
184 static inline uint64_t env_time_to_cycles(env_time_t time, uint64_t hz)
185 {
186  return odp_time_to_ns(time) * (hz / 1000000) / 1000;
187 }
188 #endif
189 
190 static inline void env_time_wait_ns(uint64_t ns)
191 {
192  odp_time_wait_ns(ns);
193 }
194 
195 #ifdef __cplusplus
196 }
197 #endif
198 
199 #pragma GCC visibility pop
200 #endif /* _ENV_TIME_H_ */