EM-ODP 4.4.0
Event Machine on ODP
Loading...
Searching...
No Matches
env_time.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020-2026, 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#include <stdint.h>
43
44#include <odp_api.h>
45
46#ifdef __cplusplus
47extern "C" {
48#endif
49
50/* type for time stamp */
51typedef odp_time_t env_time_t;
52
53/* Zero time stamp */
54#define ENV_TIME_NULL ODP_TIME_NULL
55
56#define ENV_TIME_MSEC_IN_NS ODP_TIME_MSEC_IN_NS
57
58/**
59 * Returns current local time stamp value.
60 * Local time is thread specific and must not be shared between threads.
61 *
62 * @return Local time stamp
63 */
64static inline env_time_t env_time_local(void)
65{
66 return odp_time_local();
67}
68
69/**
70 * Returns current global time stamp value.
71 * Global time can be shared between threads.
72 *
73 * @return Global time stamp
74 */
75static inline env_time_t env_time_global(void)
76{
77 return odp_time_global();
78}
79
80/**
81 * Returns difference of time stamps (time2 - time1).
82 *
83 * @param time2 Second time stamp
84 * @param time1 First time stamp
85 *
86 * @return Difference between given time stamps
87 */
88static inline env_time_t env_time_diff(env_time_t time2, env_time_t time1)
89{
90 return odp_time_diff(time2, time1);
91}
92
93/**
94 * Returns difference of time stamps (time2 - time1) in nanoseconds.
95 *
96 * @param time2 Second time stamp
97 * @param time1 First time stamp
98 *
99 * @return Difference between given time stamps in nanoseconds
100 */
101static inline uint64_t env_time_diff_ns(env_time_t time2, env_time_t time1)
102{
103 return odp_time_diff_ns(time2, time1);
104}
105
106/**
107 * Sum of time stamps
108 *
109 * @param time1 First time stamp
110 * @param time2 Second time stamp
111 *
112 * @return Sum of given time stamps
113 */
114static inline env_time_t env_time_sum(env_time_t time1, env_time_t time2)
115{
116 return odp_time_sum(time1, time2);
117}
118
119/**
120 * Convert nanoseconds to local time.
121 *
122 * @param ns Time in nanoseconds
123 *
124 * @return Local time stamp
125 */
126static inline env_time_t env_time_local_from_ns(uint64_t ns)
127{
128 return odp_time_local_from_ns(ns);
129}
130
131/**
132 * Convert nanoseconds to global time
133 *
134 * @param ns Time in nanoseconds
135 *
136 * @return Global time stamp
137 */
138static inline env_time_t env_time_global_from_ns(uint64_t ns)
139{
140 return odp_time_global_from_ns(ns);
141}
142
143/**
144 * Compare time stamps.
145 *
146 * @param time2 Second time stamp
147 * @param time1 First time stamp
148 *
149 * @retval <0 when time2 < time1
150 * @retval 0 when time2 == time1
151 * @retval >0 when time2 > time1
152 */
153static inline int env_time_cmp(env_time_t time2, env_time_t time1)
154{
155 return odp_time_cmp(time2, time1);
156}
157
158 /**
159 * Convert time stamp to nanoseconds
160 *
161 * @param time Time stamp
162 *
163 * @return time in nanoseconds
164 */
165static inline uint64_t env_time_to_ns(env_time_t time)
166{
167 return odp_time_to_ns(time);
168}
169
170/**
171 * Convert time stamp to cpu cycles
172 *
173 * @param time Time stamp
174 * @param hz CPU Hz
175 *
176 * @return cpu cycles
177 */
178#ifdef __SIZEOF_INT128__
179
180static inline uint64_t env_time_to_cycles(env_time_t time, uint64_t hz)
181{
182 __uint128_t cycles;
183
184 cycles = (__uint128_t)odp_time_to_ns(time) * hz / 1000000000;
185 return (uint64_t)cycles;
186}
187#else
188static inline uint64_t env_time_to_cycles(env_time_t time, uint64_t hz)
189{
190 return odp_time_to_ns(time) * (hz / 1000000) / 1000;
191}
192#endif
193
194static inline void env_time_wait_ns(uint64_t ns)
195{
196 odp_time_wait_ns(ns);
197}
198
199#ifdef __cplusplus
200}
201#endif
202
203#pragma GCC visibility pop
204#endif /* _ENV_TIME_H_ */