aboutsummaryrefslogtreecommitdiff
path: root/arch/linux-generic/source/odp_time.c
blob: 38c1bdf027828134535c10fb6cae1be843eefbb6 (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
/* Copyright (c) 2013, Linaro Limited
 * All rights reserved.
 *
 * SPDX-License-Identifier:     BSD-3-Clause
 */

#include <odp_time.h>
#include <odp_hints.h>
#include <odp_system_info.h>
#include <odp_debug.h>

#if defined __x86_64__ || defined __i386__

uint64_t odp_time_get_cycles(void)
{
	union {
		uint64_t tsc_64;
		struct {
			uint32_t lo_32;
			uint32_t hi_32;
		};
	} tsc;

	asm volatile("rdtsc" :
		     "=a" (tsc.lo_32),
		     "=d" (tsc.hi_32) : : "memory");

	return tsc.tsc_64;
}


#elif defined __OCTEON__

uint64_t odp_time_get_cycles(void)
{
	#define CVMX_TMP_STR(x) CVMX_TMP_STR2(x)
	#define CVMX_TMP_STR2(x) #x
	uint64_t cycle;

	asm __volatile__ ("rdhwr %[rt],$" CVMX_TMP_STR(31) :
			   [rt] "=d" (cycle) : : "memory");

	return cycle;
}

#else

#include <time.h>
#include <stdlib.h>

uint64_t odp_time_get_cycles(void)
{
	struct timespec time;
	uint64_t sec, ns, hz, cycles;
	int ret;

	ret = clock_gettime(CLOCK_MONOTONIC_RAW, &time);

	if (ret != 0) {
		ODP_ERR("clock_gettime failed\n");
		exit(EXIT_FAILURE);
	}

	hz  = odp_sys_cpu_hz();
	sec = (uint64_t) time.tv_sec;
	ns  = (uint64_t) time.tv_nsec;

	cycles  = sec * hz;
	cycles += (ns * hz) / 1000000000;

	return cycles;
}

#endif

uint64_t odp_time_diff_cycles(uint64_t t1, uint64_t t2)
{
	if (odp_likely(t2 > t1))
		return t2 - t1;

	return t2 + (UINT64_MAX - t1);
}

uint64_t odp_time_cycles_to_ns(uint64_t cycles)
{
	uint64_t hz = odp_sys_cpu_hz();

	if (cycles > hz)
		return 1000000000*(cycles/hz);

	return (1000000000*cycles)/hz;
}