tailq: remove unneeded inclusions
[dpdk.git] / lib / librte_eal / bsdapp / eal / eal_timer.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 #include <string.h>
34 #include <stdio.h>
35 #include <unistd.h>
36 #include <inttypes.h>
37 #include <sys/types.h>
38 #include <sys/sysctl.h>
39 #include <errno.h>
40
41 #include <rte_common.h>
42 #include <rte_log.h>
43 #include <rte_cycles.h>
44 #include <rte_memory.h>
45 #include <rte_memzone.h>
46 #include <rte_eal.h>
47 #include <rte_debug.h>
48
49 #include "eal_private.h"
50 #include "eal_internal_cfg.h"
51
52 #ifdef RTE_LIBEAL_USE_HPET
53 #warning HPET is not supported in FreeBSD
54 #endif
55
56 enum timer_source eal_timer_source = EAL_TIMER_TSC;
57
58 /* The frequency of the RDTSC timer resolution */
59 static uint64_t eal_tsc_resolution_hz = 0;
60
61 void
62 rte_delay_us(unsigned us)
63 {
64         const uint64_t start = rte_get_timer_cycles();
65         const uint64_t ticks = (uint64_t)us * rte_get_timer_hz() / 1E6;
66         while ((rte_get_timer_cycles() - start) < ticks)
67                 rte_pause();
68 }
69
70 uint64_t
71 rte_get_tsc_hz(void)
72 {
73         return eal_tsc_resolution_hz;
74 }
75
76 static int
77 set_tsc_freq_from_sysctl(void)
78 {
79         size_t sz;
80         int tmp;
81
82         sz = sizeof(tmp);
83         tmp = 0;
84
85         if (sysctlbyname("kern.timecounter.smp_tsc", &tmp, &sz, NULL, 0))
86                 RTE_LOG(WARNING, EAL, "%s\n", strerror(errno));
87         else if (tmp != 1)
88                 RTE_LOG(WARNING, EAL, "TSC is not safe to use in SMP mode\n");
89
90         tmp = 0;
91
92         if (sysctlbyname("kern.timecounter.invariant_tsc", &tmp, &sz, NULL, 0))
93                 RTE_LOG(WARNING, EAL, "%s\n", strerror(errno));
94         else if (tmp != 1)
95                 RTE_LOG(WARNING, EAL, "TSC is not invariant\n");
96
97         sz = sizeof(eal_tsc_resolution_hz);
98         if (sysctlbyname("machdep.tsc_freq", &eal_tsc_resolution_hz, &sz, NULL, 0)) {
99                 RTE_LOG(WARNING, EAL, "%s\n", strerror(errno));
100                 return -1;
101         }
102
103         return 0;
104 }
105
106 static void
107 set_tsc_freq_fallback(void)
108 {
109         RTE_LOG(WARNING, EAL, "WARNING: clock_gettime cannot use "
110                         "CLOCK_MONOTONIC_RAW and HPET is not available"
111                         " - clock timings may be less accurate.\n");
112         /* assume that the sleep(1) will sleep for 1 second */
113         uint64_t start = rte_rdtsc();
114         sleep(1);
115         eal_tsc_resolution_hz = rte_rdtsc() - start;
116 }
117
118 /*
119  * This function measures the TSC frequency. It uses a variety of approaches.
120  *
121  * 1. Read the TSC frequency value provided by the kernel
122  * 2. If above does not work, just sleep for 1 second and tune off that,
123  *    printing a warning about inaccuracy of timing
124  */
125 static void
126 set_tsc_freq(void)
127 {
128         if (set_tsc_freq_from_sysctl() < 0)
129                 set_tsc_freq_fallback();
130
131         RTE_LOG(INFO, EAL, "TSC frequency is ~%"PRIu64" KHz\n",
132                         eal_tsc_resolution_hz/1000);
133 }
134
135 int
136 rte_eal_timer_init(void)
137 {
138         set_tsc_freq();
139         return 0;
140 }