aboutsummaryrefslogtreecommitdiff
path: root/platform/linux-dpdk/include/odp/api/plat/time_inlines.h
blob: c007c5ce965b8c278a17d37f7bdf620be1446ad1 (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
/* Copyright (c) 2018, Linaro Limited
 * Copyright (c) 2020-2021, Nokia
 * All rights reserved.
 *
 * SPDX-License-Identifier:     BSD-3-Clause
 */

#ifndef ODP_PLAT_TIME_INLINES_H_
#define ODP_PLAT_TIME_INLINES_H_

#include <stdint.h>

#include <odp/api/align.h>
#include <odp/api/hints.h>

#include <odp/api/abi/cpu_time.h>

/** @cond _ODP_HIDE_FROM_DOXYGEN_ */

#define _ODP_TIMESPEC_SIZE 16
#define _ODP_TIME_GIGA_HZ  1000000000ULL

typedef odp_time_t (*time_cur_fn)(void);
typedef odp_time_t (*time_cur_strict_fn)(void);
typedef uint64_t (*time_res_fn)(void);

typedef struct time_handler_ {
	time_cur_fn        time_cur;
	time_cur_strict_fn time_cur_strict;
	time_res_fn        time_res;

} time_handler_t;

typedef struct _odp_time_global_t {
	/* Storage space for struct timespec. Posix headers are not included
	 * here to avoid application exposure. */
	uint8_t timespec[_ODP_TIMESPEC_SIZE] ODP_ALIGNED(_ODP_TIMESPEC_SIZE);

	int             use_hw;
	uint64_t        hw_start;
	uint64_t        hw_freq_hz;
	/* DPDK specific */
	time_handler_t  handler;

} _odp_time_global_t;

extern _odp_time_global_t _odp_time_glob;

odp_time_t _odp_timespec_cur(void);

static inline odp_time_t _odp_time_cur_gen(void)
{
	if (_odp_time_glob.use_hw) {
		odp_time_t time;

		time.count = _odp_cpu_global_time() - _odp_time_glob.hw_start;
		return time;
	}

	return _odp_timespec_cur();
}

static inline odp_time_t _odp_time_cur_gen_strict(void)
{
	if (_odp_time_glob.use_hw) {
		odp_time_t time;

		time.count = _odp_cpu_global_time_strict() - _odp_time_glob.hw_start;
		return time;
	}

	return _odp_timespec_cur();
}

static inline odp_time_t _odp_time_cur(void)
{
	return _odp_time_glob.handler.time_cur();
}

static inline odp_time_t _odp_time_cur_strict(void)
{
	return _odp_time_glob.handler.time_cur_strict();
}

static inline uint64_t _odp_time_hw_to_ns(odp_time_t time)
{
	uint64_t nsec;
	uint64_t freq_hz = _odp_time_glob.hw_freq_hz;
	uint64_t count = time.count;
	uint64_t sec = 0;

	if (count >= freq_hz) {
		sec   = count / freq_hz;
		count = count - sec * freq_hz;
	}

	nsec = (_ODP_TIME_GIGA_HZ * count) / freq_hz;

	return (sec * _ODP_TIME_GIGA_HZ) + nsec;
}

static inline uint64_t _odp_time_convert_to_ns(odp_time_t time)
{
	if (_odp_time_glob.use_hw)
		return _odp_time_hw_to_ns(time);

	return time.nsec;
}

#ifndef _ODP_NO_INLINE
	/* Inline functions by default */
	#define _ODP_INLINE static inline
	#define odp_time_local      __odp_time_local
	#define odp_time_global     __odp_time_global
	#define odp_time_to_ns      __odp_time_to_ns
	#define odp_time_local_ns   __odp_time_local_ns
	#define odp_time_global_ns  __odp_time_global_ns

	#define odp_time_local_strict      __odp_time_local_strict
	#define odp_time_global_strict     __odp_time_global_strict
	#define odp_time_local_strict_ns   __odp_time_local_strict_ns
	#define odp_time_global_strict_ns  __odp_time_global_strict_ns

	#define odp_time_cmp        __odp_time_cmp
	#define odp_time_diff       __odp_time_diff
	#define odp_time_sum        __odp_time_sum

#else
	#define _ODP_INLINE
#endif

_ODP_INLINE odp_time_t odp_time_local(void)
{
	return _odp_time_cur();
}

_ODP_INLINE odp_time_t odp_time_global(void)
{
	return _odp_time_cur();
}

_ODP_INLINE odp_time_t odp_time_local_strict(void)
{
	return _odp_time_cur_strict();
}

_ODP_INLINE odp_time_t odp_time_global_strict(void)
{
	return _odp_time_cur_strict();
}

_ODP_INLINE uint64_t odp_time_local_ns(void)
{
	return _odp_time_convert_to_ns(_odp_time_cur());
}

_ODP_INLINE uint64_t odp_time_global_ns(void)
{
	return _odp_time_convert_to_ns(_odp_time_cur());
}

_ODP_INLINE uint64_t odp_time_local_strict_ns(void)
{
	return _odp_time_convert_to_ns(_odp_time_cur_strict());
}

_ODP_INLINE uint64_t odp_time_global_strict_ns(void)
{
	return _odp_time_convert_to_ns(_odp_time_cur_strict());
}

_ODP_INLINE uint64_t odp_time_to_ns(odp_time_t time)
{
	return _odp_time_convert_to_ns(time);
}

_ODP_INLINE int odp_time_cmp(odp_time_t t2, odp_time_t t1)
{
	if (odp_likely(t2.u64 > t1.u64))
		return 1;

	if (t2.u64 < t1.u64)
		return -1;

	return 0;
}

_ODP_INLINE odp_time_t odp_time_diff(odp_time_t t2, odp_time_t t1)
{
	odp_time_t time;

	time.u64 = t2.u64 - t1.u64;

	return time;
}

_ODP_INLINE odp_time_t odp_time_sum(odp_time_t t1, odp_time_t t2)
{
	odp_time_t time;

	time.u64 = t1.u64 + t2.u64;

	return time;
}

/** @endcond */

#endif