aboutsummaryrefslogtreecommitdiff
path: root/include/odp/api/spec/time.h
blob: f7e20a6f4f006eafb729e675ab8a35a93d5a1a06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright (c) 2013-2018 Linaro Limited
 * Copyright (c) 2020-2023 Nokia
 */

/**
 * @file
 *
 * ODP time
 */

#ifndef ODP_API_SPEC_TIME_H_
#define ODP_API_SPEC_TIME_H_
#include <odp/visibility_begin.h>

#ifdef __cplusplus
extern "C" {
#endif

#include <odp/api/std_types.h>
#include <odp/api/time_types.h>

/** @addtogroup odp_time
 *  SoC global and CPU local wall clock time
 *
 *  @{
 */

/**
 * Current local time
 *
 * Returns current CPU local time stamp value. The used time source is specific to the calling
 * thread and the CPU it is running on during the call. Time stamp values from different
 * time sources cannot be compared or otherwise mixed.
 *
 * Local time stamp value advances with a constant rate defined by odp_time_local_res(). The rate
 * remains constant even during dynamic CPU frequency scaling. Local time stamp and related
 * nanosecond values may not start from zero, but are guaranteed not to wrap around in at least
 * 10 years from the ODP instance startup.
 *
 * @return CPU local time stamp value
 */
odp_time_t odp_time_local(void);

/**
 * Current local time in nanoseconds
 *
 * Like odp_time_local(), but the time stamp value is converted into nanoseconds.
 *
 * @return Local time stamp in nanoseconds
 */
uint64_t odp_time_local_ns(void);

/**
 * Current local time (strict)
 *
 * Like odp_time_local(), but reads the time stamp value more strictly in the program order.
 * The function may decrease CPU performance around the call, as it may include additional
 * barrier instructions or otherwise limit out-of-order execution.
 *
 * @return Local time stamp
 */
odp_time_t odp_time_local_strict(void);

/**
 * Current local time in nanoseconds (strict)
 *
 * Like odp_time_local_strict(), but the time stamp value is converted into nanoseconds.
 *
 * @return Local time stamp in nanoseconds
 */
uint64_t odp_time_local_strict_ns(void);

/**
 * Current global time
 *
 * Returns current SoC global time stamp value. Global time stamp values read by different threads
 * (or CPUs) may be compared or otherwise mixed as those come from the same time source.
 *
 * Global time stamp value advances with a constant rate defined by odp_time_global_res(). The rate
 * remains constant even during dynamic CPU frequency scaling. Global time stamp and related
 * nanosecond values may not start from zero, but are guaranteed not to wrap around in at least
 * 10 years from the ODP instance startup.
 *
 * @return SoC global time stamp value
 */
odp_time_t odp_time_global(void);

/**
 * Current global time in nanoseconds
 *
 * Like odp_time_global(), but the time stamp value is converted into nanoseconds.
 *
 * @return Global time stamp in nanoseconds
 */
uint64_t odp_time_global_ns(void);

/**
 * Current global time (strict)
 *
 * Like odp_time_global(), but reads the time stamp value more strictly (see
 * odp_time_local_strict() documentation) in the program order.
 *
 * @return Global time stamp
 */
odp_time_t odp_time_global_strict(void);

/**
 * Current global time in nanoseconds (strict)
 *
 * Like odp_time_global_strict(), but the time stamp value is converted into nanoseconds.
 *
 * @return Global time stamp in nanoseconds
 */
uint64_t odp_time_global_strict_ns(void);

/**
 * Time difference
 *
 * @param t2    Second time stamp
 * @param t1    First time stamp
 *
 * @return Difference of time stamps
 */
odp_time_t odp_time_diff(odp_time_t t2, odp_time_t t1);

/**
 * Time difference in nanoseconds
 *
 * @param t2    Second time stamp
 * @param t1    First time stamp
 *
 * @return Difference of time stamps (t2 - t1) in nanoseconds
 */
uint64_t odp_time_diff_ns(odp_time_t t2, odp_time_t t1);

/**
 * Add nanoseconds into time
 *
 * Adds 'ns' nanoseconds into the time stamp value. The resulting time may wrap around, if
 * the sum of 'time' and 'ns' is more than 10 years from the ODP instance startup.
 *
 * @param time  Time stamp
 * @param ns    Nanoseconds to be added
 *
 * @return Time stamp incremented by 'ns' nanoseconds
 */
odp_time_t odp_time_add_ns(odp_time_t time, uint64_t ns);

/**
 * Time sum
 *
 * Returns the sum of time stamp values. Time stamps must be from the same time source (global or
 * local). The resulting time may wrap around, if the sum exceeds 10 years from the ODP instance
 * startup.
 *
 * @param t1    Time stamp
 * @param t2    Time stamp
 *
 * @return Sum of time stamps
 */
odp_time_t odp_time_sum(odp_time_t t1, odp_time_t t2);

/**
 * Convert time to nanoseconds
 *
 * @param time  Time
 *
 * @return Time in nanoseconds
 */
uint64_t odp_time_to_ns(odp_time_t time);

/**
 * Convert nanoseconds to local time
 *
 * @param ns    Time in nanoseconds
 *
 * @return Local time stamp
 */
odp_time_t odp_time_local_from_ns(uint64_t ns);

/**
 * Convert nanoseconds to global time
 *
 * @param ns    Time in nanoseconds
 *
 * @return Global time stamp
 */
odp_time_t odp_time_global_from_ns(uint64_t ns);

/**
 * Compare two times
 *
 * @param t2    Second time
 * @param t1    First time
 *
 * @retval <0 when t2 < t1
 * @retval  0 when t2 == t1
 * @retval >0 when t2 > t1
 */
int odp_time_cmp(odp_time_t t2, odp_time_t t1);

/**
 * Local time resolution in hertz
 *
 * @return      Local time resolution in hertz
 */
uint64_t odp_time_local_res(void);

/**
 * Global time resolution in hertz
 *
 * @return      Global time resolution in hertz
 */
uint64_t odp_time_global_res(void);

/**
 * Wait until the specified (wall clock) time has been reached
 *
 * The thread potentially busy loop the entire wait time.
 *
 * @param time  Time to reach before continue
 */
void odp_time_wait_until(odp_time_t time);

/**
 * Wait the specified number of nanoseconds
 *
 * The thread potentially busy loop the entire wait time.
 *
 * @param ns    Time in nanoseconds to wait
 */
void odp_time_wait_ns(uint64_t ns);

/**
 * Get ODP instance startup time
 *
 * Outputs time stamp values captured at ODP instance startup. Application may use those
 * to calculate time stamp values relative to ODP startup time.
 *
 * @param[out] startup  Startup time structure for output
 */
void odp_time_startup(odp_time_startup_t *startup);

/**
 * @}
 */

#ifdef __cplusplus
}
#endif

#include <odp/visibility_end.h>
#endif