blob: 35852a61cdfe07b2e8d6d55fe18b1951b45f08ee [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15/*
16 Copyright (c) 2005 Eliot Dresselhaus
17
18 Permission is hereby granted, free of charge, to any person obtaining
19 a copy of this software and associated documentation files (the
20 "Software"), to deal in the Software without restriction, including
21 without limitation the rights to use, copy, modify, merge, publish,
22 distribute, sublicense, and/or sell copies of the Software, and to
23 permit persons to whom the Software is furnished to do so, subject to
24 the following conditions:
25
26 The above copyright notice and this permission notice shall be
27 included in all copies or substantial portions of the Software.
28
29 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
33 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
34 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
35 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36*/
37
38#include <vppinfra/os.h>
39#include <vppinfra/time.h>
40#include <vppinfra/format.h>
Damjan Marionc0e939b2016-11-12 11:50:01 +010041#include <vppinfra/cpu.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070042
43#ifdef CLIB_UNIX
44
45#include <math.h>
46#include <sys/time.h>
47#include <fcntl.h>
48
Dave Barachc3799992016-08-15 11:12:27 -040049/* Not very accurate way of determining cpu clock frequency
Ed Warnickecb9cada2015-12-08 15:45:58 -070050 for unix. Better to use /proc/cpuinfo on linux. */
Dave Barachc3799992016-08-15 11:12:27 -040051static f64
52estimate_clock_frequency (f64 sample_time)
Ed Warnickecb9cada2015-12-08 15:45:58 -070053{
54 /* Round to nearest 100KHz. */
55 const f64 round_to_units = 100e5;
56
57 f64 time_now, time_start, time_limit, freq;
58 u64 ifreq, t[2];
59
60 time_start = time_now = unix_time_now ();
61 time_limit = time_now + sample_time;
62 t[0] = clib_cpu_time_now ();
63 while (time_now < time_limit)
64 time_now = unix_time_now ();
65 t[1] = clib_cpu_time_now ();
66
67 freq = (t[1] - t[0]) / (time_now - time_start);
68 ifreq = flt_round_nearest (freq / round_to_units);
69 freq = ifreq * round_to_units;
70
71 return freq;
72}
73
74/* Fetch cpu frequency via parseing /proc/cpuinfo.
Dave Barachc3799992016-08-15 11:12:27 -040075 Only works for Linux. */
76static f64
77clock_frequency_from_proc_filesystem (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -070078{
Dave Barachc3799992016-08-15 11:12:27 -040079 f64 cpu_freq = 1e9; /* better than 40... */
80 f64 ppc_timebase = 0; /* warnings be gone */
Ed Warnickecb9cada2015-12-08 15:45:58 -070081 int fd;
82 unformat_input_t input;
83
Dave Barach61efa142016-01-22 08:23:09 -050084/* $$$$ aarch64 kernel doesn't report "cpu MHz" */
85#if defined(__aarch64__)
86 return 0.0;
87#endif
Dave Barachc3799992016-08-15 11:12:27 -040088
Ed Warnickecb9cada2015-12-08 15:45:58 -070089 cpu_freq = 0;
90 fd = open ("/proc/cpuinfo", 0);
91 if (fd < 0)
92 return cpu_freq;
93
Dave Barach59b25652017-09-10 15:04:27 -040094 unformat_init_clib_file (&input, fd);
Ed Warnickecb9cada2015-12-08 15:45:58 -070095
96 ppc_timebase = 0;
97 while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
98 {
99 if (unformat (&input, "cpu MHz : %f", &cpu_freq))
100 cpu_freq *= 1e6;
101 else if (unformat (&input, "timebase : %f", &ppc_timebase))
102 ;
103 else
104 unformat_skip_line (&input);
105 }
106
107 unformat_free (&input);
108
109 close (fd);
110
111 /* Override CPU frequency with time base for PPC. */
112 if (ppc_timebase != 0)
113 cpu_freq = ppc_timebase;
114
115 return cpu_freq;
116}
117
118/* Fetch cpu frequency via reading /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq
Dave Barachc3799992016-08-15 11:12:27 -0400119 Only works for Linux. */
120static f64
121clock_frequency_from_sys_filesystem (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700122{
Dave Barach96e2d442018-11-14 11:42:03 -0500123 f64 cpu_freq = 0.0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700124 int fd;
125 unformat_input_t input;
126
127 /* Time stamp always runs at max frequency. */
128 cpu_freq = 0;
129 fd = open ("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq", 0);
130 if (fd < 0)
131 goto done;
132
Dave Barach59b25652017-09-10 15:04:27 -0400133 unformat_init_clib_file (&input, fd);
Dave Barach96e2d442018-11-14 11:42:03 -0500134 (void) unformat (&input, "%f", &cpu_freq);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700135 cpu_freq *= 1e3; /* measured in kHz */
136 unformat_free (&input);
137 close (fd);
Dave Barachc3799992016-08-15 11:12:27 -0400138done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700139 return cpu_freq;
140}
141
Dave Barachc3799992016-08-15 11:12:27 -0400142f64
143os_cpu_clock_frequency (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700144{
Brian Brooksc0379ae2018-01-09 16:39:07 -0600145#if defined (__aarch64__)
146 /* The system counter increments at a fixed frequency. It is distributed
147 * to each core which has registers for reading the current counter value
148 * as well as the clock frequency. The system counter is not clocked at
149 * the same frequency as the core. */
Sirshak Das40c6e1d2018-05-29 21:21:02 -0500150 u64 hz;
Brian Brooksc0379ae2018-01-09 16:39:07 -0600151 asm volatile ("mrs %0, cntfrq_el0":"=r" (hz));
152 return (f64) hz;
153#endif
Ed Warnickecb9cada2015-12-08 15:45:58 -0700154 f64 cpu_freq;
155
Damjan Marion12e3e312019-12-18 18:45:19 +0100156#ifdef __x86_64__
157 u32 __clib_unused eax = 0, ebx = 0, ecx = 0, edx = 0;
158 clib_get_cpuid (0x00, &eax, &ebx, &ecx, &edx);
159 if (eax >= 0x15)
160 {
161 u32 max_leaf = eax;
162 /*
163 CPUID Leaf 0x15 - Time Stamp Counter and Nominal Core Crystal Clock Info
164 eax - denominator of the TSC/”core crystal clock” ratio
165 ebx - numerator of the TSC/”core crystal clock” ratio
166 ecx - nominal frequency of the core crystal clock in Hz
167 edx - reseved
168 */
169
170 clib_get_cpuid (0x15, &eax, &ebx, &ecx, &edx);
171 if (ebx && ecx)
172 return ecx * ebx / eax;
173
174 if (max_leaf >= 0x16)
175 {
176 /*
177 CPUID Leaf 0x16 - Processor Frequency Information Leaf
178 eax - Bits 15 - 00: Processor Base Frequency (in MHz).
179 */
180
181 clib_get_cpuid (0x16, &eax, &ebx, &ecx, &edx);
182 if (eax)
183 return 1e6 * (eax & 0xffff);
184 }
185 }
186#endif
187
Damjan Marionc0e939b2016-11-12 11:50:01 +0100188 if (clib_cpu_supports_invariant_tsc ())
189 return estimate_clock_frequency (1e-3);
190
Ed Warnickecb9cada2015-12-08 15:45:58 -0700191 /* First try /sys version. */
192 cpu_freq = clock_frequency_from_sys_filesystem ();
193 if (cpu_freq != 0)
194 return cpu_freq;
195
196 /* Next try /proc version. */
197 cpu_freq = clock_frequency_from_proc_filesystem ();
198 if (cpu_freq != 0)
199 return cpu_freq;
200
201 /* If /proc/cpuinfo fails (e.g. not running on Linux) fall back to
202 gettimeofday based estimated clock frequency. */
203 return estimate_clock_frequency (1e-3);
204}
205
206#endif /* CLIB_UNIX */
207
208/* Initialize time. */
Dave Barachc3799992016-08-15 11:12:27 -0400209void
210clib_time_init (clib_time_t * c)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700211{
Dave Barachb7b92992018-10-17 10:38:51 -0400212 clib_memset (c, 0, sizeof (c[0]));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700213 c->clocks_per_second = os_cpu_clock_frequency ();
214 c->seconds_per_clock = 1 / c->clocks_per_second;
215 c->log2_clocks_per_second = min_log2_u64 ((u64) c->clocks_per_second);
216
217 /* Initially verify frequency every sec */
218 c->log2_clocks_per_frequency_verify = c->log2_clocks_per_second;
219
220 c->last_verify_reference_time = unix_time_now ();
221 c->last_cpu_time = clib_cpu_time_now ();
222 c->init_cpu_time = c->last_verify_cpu_time = c->last_cpu_time;
223}
224
Dave Barachc3799992016-08-15 11:12:27 -0400225void
226clib_time_verify_frequency (clib_time_t * c)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700227{
228 f64 now_reference = unix_time_now ();
229 f64 dtr = now_reference - c->last_verify_reference_time;
230 f64 dtr_max;
231 u64 dtc = c->last_cpu_time - c->last_verify_cpu_time;
Dave Barach36feebb2018-09-07 11:12:27 -0400232 f64 new_clocks_per_second, delta;
Dave Barache52d8d82019-12-01 08:59:03 -0500233 f64 save_total_cpu_time_in_seconds;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700234
235 c->last_verify_cpu_time = c->last_cpu_time;
236 c->last_verify_reference_time = now_reference;
237
Dave Barachc3799992016-08-15 11:12:27 -0400238 /*
239 * Is the reported reference interval non-positive,
240 * or off by a factor of two - or 8 seconds - whichever is larger?
Ed Warnickecb9cada2015-12-08 15:45:58 -0700241 * Someone reset the clock behind our back.
242 */
Dave Barachc3799992016-08-15 11:12:27 -0400243 dtr_max = (f64) (2ULL << c->log2_clocks_per_frequency_verify) /
244 (f64) (1ULL << c->log2_clocks_per_second);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700245 dtr_max = dtr_max > 8.0 ? dtr_max : 8.0;
246
247 if (dtr <= 0.0 || dtr > dtr_max)
248 {
249 c->log2_clocks_per_frequency_verify = c->log2_clocks_per_second;
250 return;
251 }
252
Dave Barachba603ba2018-11-07 17:40:19 -0500253 if (PREDICT_FALSE (c->round_to_units == 0.0))
254 {
255 f64 next_pow10, est_round_to_units;
256 /*
257 * Compute the first power of ten which is greater than
258 * 0.1% of the new clock rate. Save the result, and use it
259 * to round future results, so we don't end up calculating
260 * silly-looking clock rates.
261 */
262 est_round_to_units = ((f64) dtc / dtr) * 0.001;
263 next_pow10 = ceil (log10 (est_round_to_units));
264 c->round_to_units = pow (10.0, next_pow10);
265 }
266
Dave Barach36feebb2018-09-07 11:12:27 -0400267 /*
268 * Reject large frequency changes, another consequence of
269 * system clock changes particularly with old kernels.
270 */
271 new_clocks_per_second =
Dave Barachba603ba2018-11-07 17:40:19 -0500272 flt_round_nearest ((f64) dtc / (dtr * c->round_to_units))
273 * c->round_to_units;
Dave Barach36feebb2018-09-07 11:12:27 -0400274
Dave Barache52d8d82019-12-01 08:59:03 -0500275 /* Compute abs(rate change) */
Dave Barach36feebb2018-09-07 11:12:27 -0400276 delta = new_clocks_per_second - c->clocks_per_second;
277 if (delta < 0.0)
278 delta = -delta;
279
Dave Barache52d8d82019-12-01 08:59:03 -0500280 /* If rate change > 1%, reject it and try again */
Dave Barach36feebb2018-09-07 11:12:27 -0400281 if (PREDICT_FALSE ((delta / c->clocks_per_second) > .01))
282 {
283 clib_warning ("Rejecting large frequency change of %.2f%%",
284 (delta / c->clocks_per_second) * 100.0);
285 c->log2_clocks_per_frequency_verify = c->log2_clocks_per_second;
286 return;
287 }
288
Dave Barache52d8d82019-12-01 08:59:03 -0500289 /* Save total cpu time in seconds */
290 save_total_cpu_time_in_seconds = c->total_cpu_time * c->seconds_per_clock;
291
292 /* Recalculate clock rate */
293 c->clocks_per_second = new_clocks_per_second;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700294 c->seconds_per_clock = 1 / c->clocks_per_second;
295
Dave Barache52d8d82019-12-01 08:59:03 -0500296 /*
297 * Restore total cpu time in seconds. Otherwise, if c->clocks_per_second
298 * has decreased, time may appear to flow backwards.
299 */
300 c->total_cpu_time = save_total_cpu_time_in_seconds * c->clocks_per_second;
301
Ed Warnickecb9cada2015-12-08 15:45:58 -0700302 /* Double time between verifies; max at 64 secs ~ 1 minute. */
303 if (c->log2_clocks_per_frequency_verify < c->log2_clocks_per_second + 6)
304 c->log2_clocks_per_frequency_verify += 1;
305}
Dave Barachc3799992016-08-15 11:12:27 -0400306
307/*
308 * fd.io coding-style-patch-verification: ON
309 *
310 * Local Variables:
311 * eval: (c-set-style "gnu")
312 * End:
313 */