1a9643ea8Slogwang /*-
2a9643ea8Slogwang  *   BSD LICENSE
3a9643ea8Slogwang  *
4a9643ea8Slogwang  *   Copyright(c) 2015 Intel Corporation. All rights reserved.
5a9643ea8Slogwang  *   All rights reserved.
6a9643ea8Slogwang  *
7a9643ea8Slogwang  *   Redistribution and use in source and binary forms, with or without
8a9643ea8Slogwang  *   modification, are permitted provided that the following conditions
9a9643ea8Slogwang  *   are met:
10a9643ea8Slogwang  *
11a9643ea8Slogwang  *     * Redistributions of source code must retain the above copyright
12a9643ea8Slogwang  *       notice, this list of conditions and the following disclaimer.
13a9643ea8Slogwang  *     * Redistributions in binary form must reproduce the above copyright
14a9643ea8Slogwang  *       notice, this list of conditions and the following disclaimer in
15a9643ea8Slogwang  *       the documentation and/or other materials provided with the
16a9643ea8Slogwang  *       distribution.
17a9643ea8Slogwang  *     * Neither the name of Intel Corporation nor the names of its
18a9643ea8Slogwang  *       contributors may be used to endorse or promote products derived
19a9643ea8Slogwang  *       from this software without specific prior written permission.
20a9643ea8Slogwang  *
21a9643ea8Slogwang  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22a9643ea8Slogwang  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23a9643ea8Slogwang  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24a9643ea8Slogwang  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25a9643ea8Slogwang  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26a9643ea8Slogwang  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27a9643ea8Slogwang  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28a9643ea8Slogwang  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29a9643ea8Slogwang  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30a9643ea8Slogwang  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31a9643ea8Slogwang  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32a9643ea8Slogwang  */
33a9643ea8Slogwang 
34a9643ea8Slogwang 
35a9643ea8Slogwang #ifndef LTHREAD_TIMER_H_
36a9643ea8Slogwang #define LTHREAD_TIMER_H_
37a9643ea8Slogwang 
38*2bfe3f2eSlogwang #ifdef __cplusplus
39*2bfe3f2eSlogwang extern "C" {
40*2bfe3f2eSlogwang #endif
41*2bfe3f2eSlogwang 
42a9643ea8Slogwang #include "lthread_int.h"
43a9643ea8Slogwang #include "lthread_sched.h"
44a9643ea8Slogwang 
45a9643ea8Slogwang 
46a9643ea8Slogwang static inline uint64_t
47a9643ea8Slogwang _ns_to_clks(uint64_t ns)
48a9643ea8Slogwang {
49*2bfe3f2eSlogwang 	/*
50*2bfe3f2eSlogwang 	 * clkns needs to be divided by 1E9 to get ns clocks. However,
51*2bfe3f2eSlogwang 	 * dividing by this first would lose a lot of accuracy.
52*2bfe3f2eSlogwang 	 * Dividing after a multiply by ns, could cause overflow of
53*2bfe3f2eSlogwang 	 * uint64_t if ns is about 5 seconds [if we assume a max tsc
54*2bfe3f2eSlogwang 	 * rate of 4GHz]. Therefore we first divide by 1E4, then
55*2bfe3f2eSlogwang 	 * multiply and finally divide by 1E5. This allows ns to be
56*2bfe3f2eSlogwang 	 * values many hours long, without overflow, while still keeping
57*2bfe3f2eSlogwang 	 * reasonable accuracy.
58*2bfe3f2eSlogwang 	 */
59*2bfe3f2eSlogwang 	uint64_t clkns = rte_get_tsc_hz() / 1e4;
60a9643ea8Slogwang 
61a9643ea8Slogwang 	clkns *= ns;
62*2bfe3f2eSlogwang 	clkns /= 1e5;
63*2bfe3f2eSlogwang 
64*2bfe3f2eSlogwang 	return clkns;
65a9643ea8Slogwang }
66a9643ea8Slogwang 
67a9643ea8Slogwang 
68a9643ea8Slogwang static inline void
69a9643ea8Slogwang _timer_start(struct lthread *lt, uint64_t clks)
70a9643ea8Slogwang {
71a9643ea8Slogwang 	if (clks > 0) {
72a9643ea8Slogwang 		DIAG_EVENT(lt, LT_DIAG_LTHREAD_TMR_START, &lt->tim, clks);
73a9643ea8Slogwang 		rte_timer_init(&lt->tim);
74a9643ea8Slogwang 		rte_timer_reset(&lt->tim,
75a9643ea8Slogwang 				clks,
76a9643ea8Slogwang 				SINGLE,
77a9643ea8Slogwang 				rte_lcore_id(),
78a9643ea8Slogwang 				_sched_timer_cb,
79a9643ea8Slogwang 				(void *)lt);
80a9643ea8Slogwang 	}
81a9643ea8Slogwang }
82a9643ea8Slogwang 
83a9643ea8Slogwang 
84a9643ea8Slogwang static inline void
85a9643ea8Slogwang _timer_stop(struct lthread *lt)
86a9643ea8Slogwang {
87a9643ea8Slogwang 	if (lt != NULL) {
88a9643ea8Slogwang 		DIAG_EVENT(lt, LT_DIAG_LTHREAD_TMR_DELETE, &lt->tim, 0);
89a9643ea8Slogwang 		rte_timer_stop(&lt->tim);
90a9643ea8Slogwang 	}
91a9643ea8Slogwang }
92a9643ea8Slogwang 
93*2bfe3f2eSlogwang #ifdef __cplusplus
94*2bfe3f2eSlogwang }
95*2bfe3f2eSlogwang #endif
96a9643ea8Slogwang 
97a9643ea8Slogwang #endif /* LTHREAD_TIMER_H_ */
98