xref: /dpdk/lib/eal/windows/eal_timer.c (revision 99a2dd95)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2020 Mellanox Technologies, Ltd
3  */
4 
5 #include <inttypes.h>
6 
7 #include <rte_windows.h>
8 #include <rte_common.h>
9 #include <rte_cycles.h>
10 #include <rte_eal.h>
11 #include <rte_errno.h>
12 #include "eal_private.h"
13 
14 #define US_PER_SEC 1E6
15 #define CYC_PER_10MHZ 1E7
16 
17 void
rte_delay_us_sleep(unsigned int us)18 rte_delay_us_sleep(unsigned int us)
19 {
20 	HANDLE timer;
21 	LARGE_INTEGER due_time;
22 
23 	/* create waitable timer */
24 	timer = CreateWaitableTimer(NULL, TRUE, NULL);
25 	if (!timer) {
26 		RTE_LOG_WIN32_ERR("CreateWaitableTimer()");
27 		rte_errno = ENOMEM;
28 		return;
29 	}
30 
31 	/*
32 	 * due_time's uom is 100 ns, multiply by 10 to convert to microseconds
33 	 * set us microseconds time for timer
34 	 */
35 	due_time.QuadPart = -((int64_t)us * 10);
36 	if (!SetWaitableTimer(timer, &due_time, 0, NULL, NULL, FALSE)) {
37 		RTE_LOG_WIN32_ERR("SetWaitableTimer()");
38 		rte_errno = EINVAL;
39 		goto end;
40 	}
41 	/* start wait for timer for us microseconds */
42 	if (WaitForSingleObject(timer, INFINITE) == WAIT_FAILED) {
43 		RTE_LOG_WIN32_ERR("WaitForSingleObject()");
44 		rte_errno = EINVAL;
45 	}
46 
47 end:
48 	CloseHandle(timer);
49 }
50 
51 uint64_t
get_tsc_freq(void)52 get_tsc_freq(void)
53 {
54 	LARGE_INTEGER t_start, t_end, elapsed_us;
55 	LARGE_INTEGER frequency;
56 	uint64_t tsc_hz;
57 	uint64_t end, start;
58 
59 	QueryPerformanceFrequency(&frequency);
60 
61 	QueryPerformanceCounter(&t_start);
62 	start = rte_get_tsc_cycles();
63 
64 	rte_delay_us_sleep(US_PER_SEC / 10); /* 1/10 second */
65 
66 	if (rte_errno != 0)
67 		return 0;
68 
69 	QueryPerformanceCounter(&t_end);
70 	end = rte_get_tsc_cycles();
71 
72 	elapsed_us.QuadPart = t_end.QuadPart - t_start.QuadPart;
73 
74 	/*
75 	 * To guard against loss-of-precision, convert to microseconds
76 	 * *before* dividing by ticks-per-second.
77 	 */
78 	elapsed_us.QuadPart *= US_PER_SEC;
79 	elapsed_us.QuadPart /= frequency.QuadPart;
80 
81 	double secs = ((double)elapsed_us.QuadPart)/US_PER_SEC;
82 	tsc_hz = (uint64_t)((end - start)/secs);
83 
84 	/* Round up to 10Mhz. 1E7 ~ 10Mhz */
85 	return RTE_ALIGN_MUL_NEAR(tsc_hz, CYC_PER_10MHZ);
86 }
87 
88 
89 int
rte_eal_timer_init(void)90 rte_eal_timer_init(void)
91 {
92 	set_tsc_freq();
93 	return 0;
94 }
95