aboutsummaryrefslogtreecommitdiff
path: root/platform/linux-dpdk/odp_time.c
blob: 3107d24bfebde39d918c6aa0ef8a0cd7888c6327 (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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/* Copyright (c) 2013-2018, Linaro Limited
 * All rights reserved.
 *
 * SPDX-License-Identifier:     BSD-3-Clause
 */

#include <odp_posix_extensions.h>

#include <time.h>
#include <odp/api/time.h>
#include <odp/api/hints.h>
#include <odp_debug_internal.h>
#include <odp_arch_time_internal.h>
#include <rte_cycles.h>
#include <string.h>
#include <inttypes.h>

typedef uint64_t (*time_to_ns_fn) (odp_time_t time);
typedef odp_time_t (*time_cur_fn)(void);
typedef odp_time_t (*time_from_ns_fn) (uint64_t ns);
typedef uint64_t (*time_res_fn)(void);

typedef struct time_handler_ {
	time_cur_fn    time_cur;
	time_res_fn     time_res;
} time_handler_t;

typedef struct time_global_t {
	struct timespec spec_start;
	int             use_hw;
	uint64_t        hw_start;
	uint64_t        hw_freq_hz;
	/* DPDK specific */
	time_handler_t  handler;
} time_global_t;

static time_global_t global;

/*
 * Posix timespec based functions
 */

static inline uint64_t time_spec_diff_nsec(struct timespec *t2,
					   struct timespec *t1)
{
	struct timespec diff;
	uint64_t nsec;

	diff.tv_sec  = t2->tv_sec  - t1->tv_sec;
	diff.tv_nsec = t2->tv_nsec - t1->tv_nsec;

	if (diff.tv_nsec < 0) {
		diff.tv_nsec += ODP_TIME_SEC_IN_NS;
		diff.tv_sec  -= 1;
	}

	nsec = (diff.tv_sec * ODP_TIME_SEC_IN_NS) + diff.tv_nsec;

	return nsec;
}

static inline odp_time_t time_spec_cur(void)
{
	int ret;
	odp_time_t time;
	struct timespec sys_time;

	ret = clock_gettime(CLOCK_MONOTONIC_RAW, &sys_time);
	if (odp_unlikely(ret != 0))
		ODP_ABORT("clock_gettime failed\n");

	time.nsec = time_spec_diff_nsec(&sys_time, &global.spec_start);

	return time;
}

static inline uint64_t time_spec_res(void)
{
	int ret;
	struct timespec tres;

	ret = clock_getres(CLOCK_MONOTONIC_RAW, &tres);
	if (odp_unlikely(ret != 0))
		ODP_ABORT("clock_getres failed\n");

	return ODP_TIME_SEC_IN_NS / (uint64_t)tres.tv_nsec;
}

static inline uint64_t time_spec_to_ns(odp_time_t time)
{
	return time.nsec;
}

static inline odp_time_t time_spec_from_ns(uint64_t ns)
{
	odp_time_t time;

	time.nsec = ns;

	return time;
}

/*
 * HW time counter based functions
 */

static inline odp_time_t time_hw_cur(void)
{
	odp_time_t time;

	time.count = cpu_global_time() - global.hw_start;

	return time;
}

static inline uint64_t time_hw_res(void)
{
	return global.hw_freq_hz;
}

static inline uint64_t time_hw_to_ns(odp_time_t time)
{
	uint64_t nsec;
	uint64_t freq_hz = global.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_SEC_IN_NS * count) / freq_hz;

	return (sec * ODP_TIME_SEC_IN_NS) + nsec;
}

static inline odp_time_t time_hw_from_ns(uint64_t ns)
{
	odp_time_t time;
	uint64_t count;
	uint64_t freq_hz = global.hw_freq_hz;
	uint64_t sec = 0;

	if (ns >= ODP_TIME_SEC_IN_NS) {
		sec = ns / ODP_TIME_SEC_IN_NS;
		ns  = ns - sec * ODP_TIME_SEC_IN_NS;
	}

	count  = sec * freq_hz;
	count += (ns * freq_hz) / ODP_TIME_SEC_IN_NS;

	time.count = count;

	return time;
}

/*
 * Common functions
 */

static inline odp_time_t time_cur(void)
{
	if (global.use_hw)
		return time_hw_cur();

	return time_spec_cur();
}

static inline uint64_t time_res(void)
{
	if (global.use_hw)
		return time_hw_res();

	return time_spec_res();
}

static inline int 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;
}

static inline odp_time_t time_sum(odp_time_t t1, odp_time_t t2)
{
	odp_time_t time;

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

	return time;
}

static inline uint64_t time_to_ns(odp_time_t time)
{
	if (global.use_hw)
		return time_hw_to_ns(time);

	return time_spec_to_ns(time);
}

static inline odp_time_t time_from_ns(uint64_t ns)
{
	if (global.use_hw)
		return time_hw_from_ns(ns);

	return time_spec_from_ns(ns);
}

static inline uint64_t time_res_dpdk(void)
{
	return rte_get_timer_hz();
}

static inline odp_time_t time_cur_dpdk(void)
{
	odp_time_t time;

	time.u64 = rte_get_timer_cycles() - global.hw_start;

	return time;
}

static inline odp_time_t time_local(void)
{
	return global.handler.time_cur();
}

static inline void time_wait_until(odp_time_t time)
{
	odp_time_t cur;

	do {
		cur = time_local();
	} while (time_cmp(time, cur) > 0);
}

odp_time_t odp_time_local(void)
{
	return time_local();
}

odp_time_t odp_time_global(void)
{
	return time_local();
}

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;
}

uint64_t odp_time_diff_ns(odp_time_t t2, odp_time_t t1)
{
	odp_time_t time;

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

	return time_to_ns(time);
}

uint64_t odp_time_to_ns(odp_time_t time)
{
	return time_to_ns(time);
}

odp_time_t odp_time_local_from_ns(uint64_t ns)
{
	return time_from_ns(ns);
}

odp_time_t odp_time_global_from_ns(uint64_t ns)
{
	return time_from_ns(ns);
}

int odp_time_cmp(odp_time_t t2, odp_time_t t1)
{
	return time_cmp(t2, t1);
}

odp_time_t odp_time_sum(odp_time_t t1, odp_time_t t2)
{
	return time_sum(t1, t2);
}

uint64_t odp_time_local_res(void)
{
	return global.handler.time_res();
}

uint64_t odp_time_global_res(void)
{
	return global.handler.time_res();
}

void odp_time_wait_ns(uint64_t ns)
{
	odp_time_t cur = time_local();
	odp_time_t wait = time_from_ns(ns);
	odp_time_t end_time = time_sum(cur, wait);

	time_wait_until(end_time);
}

void odp_time_wait_until(odp_time_t time)
{
	return time_wait_until(time);
}

static odp_bool_t is_invariant_tsc_supported(void)
{
	FILE  *file;
	char  *line = NULL;
	size_t len = 0;
	odp_bool_t nonstop_tsc  = false;
	odp_bool_t constant_tsc = false;
	odp_bool_t ret = false;

	file = fopen("/proc/cpuinfo", "rt");
	while (getline(&line, &len, file) != -1) {
		if (strstr(line, "flags") != NULL) {
			if (strstr(line, "constant_tsc") != NULL)
				constant_tsc = true;
			if (strstr(line, "nonstop_tsc") != NULL)
				nonstop_tsc = true;

			if (constant_tsc && nonstop_tsc)
				ret = true;
			else
				ret = false;

			free(line);
			fclose(file);
			return ret;
		}
	}
	free(line);
	fclose(file);
	return false;
}

static inline odp_bool_t is_dpdk_timer_cycles_support(void)
{
	if (is_invariant_tsc_supported() == true)
		return true;

#ifdef RTE_LIBEAL_USE_HPET
	if (rte_eal_hpet_init(1) == 0)
		return true;
#endif
	return false;
}

int odp_time_init_global(void)
{
	int ret = 0;

	memset(&global, 0, sizeof(time_global_t));

	if (is_dpdk_timer_cycles_support()) {
		global.handler.time_cur     = time_cur_dpdk;
		global.handler.time_res     = time_res_dpdk;
		global.hw_freq_hz  = time_res_dpdk();
		global.use_hw = 1;
		global.hw_start = rte_get_timer_cycles();
		if (global.hw_start == 0)
				return -1;
			else
				return 0;
	}

	global.handler.time_cur     = time_cur;
	global.handler.time_res     = time_res;

	if (cpu_has_global_time()) {
		global.use_hw = 1;
		global.hw_freq_hz  = cpu_global_time_freq();

		if (global.hw_freq_hz == 0)
			return -1;

		printf("HW time counter freq: %" PRIu64 " hz\n\n",
		       global.hw_freq_hz);

		global.hw_start = cpu_global_time();
		return 0;
	}

	global.spec_start.tv_sec  = 0;
	global.spec_start.tv_nsec = 0;

	ret = clock_gettime(CLOCK_MONOTONIC_RAW, &global.spec_start);

	return ret;
}

int odp_time_term_global(void)
{
	return 0;
}