blob: 5a6aaf182e4c617c6e357137f83b1067e2baa14c [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>
Dave Barachc25048b2020-01-29 18:05:24 -050042#include <math.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070043
44#ifdef CLIB_UNIX
45
46#include <math.h>
47#include <sys/time.h>
48#include <fcntl.h>
49
Dave Barachc3799992016-08-15 11:12:27 -040050/* Not very accurate way of determining cpu clock frequency
Ed Warnickecb9cada2015-12-08 15:45:58 -070051 for unix. Better to use /proc/cpuinfo on linux. */
Dave Barachc3799992016-08-15 11:12:27 -040052static f64
53estimate_clock_frequency (f64 sample_time)
Ed Warnickecb9cada2015-12-08 15:45:58 -070054{
Ed Warnickecb9cada2015-12-08 15:45:58 -070055 f64 time_now, time_start, time_limit, freq;
Dave Barachc25048b2020-01-29 18:05:24 -050056 u64 t[2];
Ed Warnickecb9cada2015-12-08 15:45:58 -070057
58 time_start = time_now = unix_time_now ();
59 time_limit = time_now + sample_time;
60 t[0] = clib_cpu_time_now ();
61 while (time_now < time_limit)
62 time_now = unix_time_now ();
63 t[1] = clib_cpu_time_now ();
64
65 freq = (t[1] - t[0]) / (time_now - time_start);
Ed Warnickecb9cada2015-12-08 15:45:58 -070066
67 return freq;
68}
69
70/* Fetch cpu frequency via parseing /proc/cpuinfo.
Dave Barachc3799992016-08-15 11:12:27 -040071 Only works for Linux. */
72static f64
73clock_frequency_from_proc_filesystem (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -070074{
Dave Barachc3799992016-08-15 11:12:27 -040075 f64 cpu_freq = 1e9; /* better than 40... */
76 f64 ppc_timebase = 0; /* warnings be gone */
Ed Warnickecb9cada2015-12-08 15:45:58 -070077 unformat_input_t input;
78
Dave Barach61efa142016-01-22 08:23:09 -050079/* $$$$ aarch64 kernel doesn't report "cpu MHz" */
80#if defined(__aarch64__)
81 return 0.0;
82#endif
Dave Barachc3799992016-08-15 11:12:27 -040083
Ed Warnickecb9cada2015-12-08 15:45:58 -070084 cpu_freq = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -070085
86 ppc_timebase = 0;
Damjan Marion8d0c0c62023-08-06 20:39:38 +020087 if (unformat_init_file (&input, "/proc/cpuinfo"))
Ed Warnickecb9cada2015-12-08 15:45:58 -070088 {
Damjan Marion8d0c0c62023-08-06 20:39:38 +020089 while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
90 {
91 if (unformat (&input, "cpu MHz : %f", &cpu_freq))
92 cpu_freq *= 1e6;
93 else if (unformat (&input, "timebase : %f", &ppc_timebase))
94 ;
95 else
96 unformat_skip_line (&input);
97 }
98
99 unformat_free (&input);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700100 }
Damjan Marion8d0c0c62023-08-06 20:39:38 +0200101 else
102 return cpu_freq;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700103
104 /* Override CPU frequency with time base for PPC. */
105 if (ppc_timebase != 0)
106 cpu_freq = ppc_timebase;
107
108 return cpu_freq;
109}
110
111/* Fetch cpu frequency via reading /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq
Dave Barachc3799992016-08-15 11:12:27 -0400112 Only works for Linux. */
113static f64
114clock_frequency_from_sys_filesystem (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700115{
Dave Barach96e2d442018-11-14 11:42:03 -0500116 f64 cpu_freq = 0.0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700117 unformat_input_t input;
118
119 /* Time stamp always runs at max frequency. */
120 cpu_freq = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700121
Damjan Marion8d0c0c62023-08-06 20:39:38 +0200122 if (unformat_init_file (
123 &input, "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq"))
124 {
125 if (unformat (&input, "%f", &cpu_freq))
126 cpu_freq *= 1e3; /* measured in kHz */
127 unformat_free (&input);
128 }
129
Ed Warnickecb9cada2015-12-08 15:45:58 -0700130 return cpu_freq;
131}
132
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200133__clib_export f64
Dave Barachc3799992016-08-15 11:12:27 -0400134os_cpu_clock_frequency (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700135{
Brian Brooksc0379ae2018-01-09 16:39:07 -0600136#if defined (__aarch64__)
137 /* The system counter increments at a fixed frequency. It is distributed
138 * to each core which has registers for reading the current counter value
139 * as well as the clock frequency. The system counter is not clocked at
140 * the same frequency as the core. */
Sirshak Das40c6e1d2018-05-29 21:21:02 -0500141 u64 hz;
Brian Brooksc0379ae2018-01-09 16:39:07 -0600142 asm volatile ("mrs %0, cntfrq_el0":"=r" (hz));
143 return (f64) hz;
144#endif
Ed Warnickecb9cada2015-12-08 15:45:58 -0700145 f64 cpu_freq;
146
Damjan Marion12e3e312019-12-18 18:45:19 +0100147#ifdef __x86_64__
148 u32 __clib_unused eax = 0, ebx = 0, ecx = 0, edx = 0;
149 clib_get_cpuid (0x00, &eax, &ebx, &ecx, &edx);
150 if (eax >= 0x15)
151 {
152 u32 max_leaf = eax;
153 /*
154 CPUID Leaf 0x15 - Time Stamp Counter and Nominal Core Crystal Clock Info
155 eax - denominator of the TSC/”core crystal clock” ratio
156 ebx - numerator of the TSC/”core crystal clock” ratio
157 ecx - nominal frequency of the core crystal clock in Hz
158 edx - reseved
159 */
160
161 clib_get_cpuid (0x15, &eax, &ebx, &ecx, &edx);
162 if (ebx && ecx)
Damjan Marionfa958422020-03-04 20:06:06 +0100163 return (u64) ecx *ebx / eax;
Damjan Marion12e3e312019-12-18 18:45:19 +0100164
165 if (max_leaf >= 0x16)
166 {
167 /*
168 CPUID Leaf 0x16 - Processor Frequency Information Leaf
169 eax - Bits 15 - 00: Processor Base Frequency (in MHz).
170 */
171
172 clib_get_cpuid (0x16, &eax, &ebx, &ecx, &edx);
173 if (eax)
174 return 1e6 * (eax & 0xffff);
175 }
176 }
177#endif
178
Dave Barachc25048b2020-01-29 18:05:24 -0500179 /* If we have an invariant TSC, use it to estimate the clock frequency */
Damjan Marionc0e939b2016-11-12 11:50:01 +0100180 if (clib_cpu_supports_invariant_tsc ())
181 return estimate_clock_frequency (1e-3);
182
Dave Barachc25048b2020-01-29 18:05:24 -0500183 /* Next, try /sys version. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700184 cpu_freq = clock_frequency_from_sys_filesystem ();
185 if (cpu_freq != 0)
186 return cpu_freq;
187
188 /* Next try /proc version. */
189 cpu_freq = clock_frequency_from_proc_filesystem ();
190 if (cpu_freq != 0)
191 return cpu_freq;
192
193 /* If /proc/cpuinfo fails (e.g. not running on Linux) fall back to
194 gettimeofday based estimated clock frequency. */
195 return estimate_clock_frequency (1e-3);
196}
197
198#endif /* CLIB_UNIX */
199
200/* Initialize time. */
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200201__clib_export void
Dave Barachc3799992016-08-15 11:12:27 -0400202clib_time_init (clib_time_t * c)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700203{
Dave Barachb7b92992018-10-17 10:38:51 -0400204 clib_memset (c, 0, sizeof (c[0]));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700205 c->clocks_per_second = os_cpu_clock_frequency ();
Dave Barach51cabf22020-02-04 16:10:17 -0500206 /*
207 * Sporadic reports of os_cpu_clock_frequency() returning 0.0
208 * in highly parallel container environments.
209 * To avoid immediate division by zero:
210 * Step 1: try estimate_clock_frequency().
211 * Step 2: give up. Pretend we have a 2gHz clock.
212 */
213 if (PREDICT_FALSE (c->clocks_per_second == 0.0))
214 {
215 c->clocks_per_second = estimate_clock_frequency (1e-3);
216 if (c->clocks_per_second == 0.0)
217 {
218 clib_warning ("os_cpu_clock_frequency() returned 0.0, use 2e9...");
219 c->clocks_per_second = 2e9;
220 }
221 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700222 c->seconds_per_clock = 1 / c->clocks_per_second;
223 c->log2_clocks_per_second = min_log2_u64 ((u64) c->clocks_per_second);
224
Dave Barachc25048b2020-01-29 18:05:24 -0500225 /* Verify frequency every 16 sec */
226 c->log2_clocks_per_frequency_verify = c->log2_clocks_per_second + 4;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700227
228 c->last_verify_reference_time = unix_time_now ();
Dave Barachc25048b2020-01-29 18:05:24 -0500229 c->init_reference_time = c->last_verify_reference_time;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700230 c->last_cpu_time = clib_cpu_time_now ();
231 c->init_cpu_time = c->last_verify_cpu_time = c->last_cpu_time;
Dave Barachc25048b2020-01-29 18:05:24 -0500232 c->total_cpu_time = 0ULL;
233
234 /*
235 * Use exponential smoothing, with a half-life of 1 minute
236 * reported_rate(t) = reported_rate(t-1) * K + rate(t)*(1-K)
237 * where K = e**(-1.0/3.75);
238 * 15 samples in 4 minutes
239 * 7.5 samples in 2 minutes,
240 * 3.75 samples in 1 minute, etc.
241 */
242 c->damping_constant = exp (-1.0 / 3.75);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700243}
244
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200245__clib_export void
Dave Barachc3799992016-08-15 11:12:27 -0400246clib_time_verify_frequency (clib_time_t * c)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700247{
Dave Barachc25048b2020-01-29 18:05:24 -0500248 f64 now_reference, delta_reference, delta_reference_max;
Dave Barach66441c42020-03-10 09:01:02 -0400249 f64 delta_clock_in_seconds;
250 u64 now_clock, delta_clock;
Dave Barach36feebb2018-09-07 11:12:27 -0400251 f64 new_clocks_per_second, delta;
Dave Barachc25048b2020-01-29 18:05:24 -0500252
253 /* Ask the kernel and the CPU what time it is... */
254 now_reference = unix_time_now ();
Dave Barach66441c42020-03-10 09:01:02 -0400255 now_clock = clib_cpu_time_now ();
256
257 /* Compute change in the reference clock */
258 delta_reference = now_reference - c->last_verify_reference_time;
259
260 /* And change in the CPU clock */
261 delta_clock_in_seconds = (f64) (now_clock - c->last_verify_cpu_time) *
262 c->seconds_per_clock;
263
264 /*
265 * Recompute vpp start time reference, and total clocks
266 * using the current clock rate
267 */
268 c->init_reference_time += (delta_reference - delta_clock_in_seconds);
269 c->total_cpu_time = (now_reference - c->init_reference_time)
270 * c->clocks_per_second;
271
272 c->last_cpu_time = now_clock;
Dave Barachc25048b2020-01-29 18:05:24 -0500273
274 /* Calculate a new clock rate sample */
Dave Barachc25048b2020-01-29 18:05:24 -0500275 delta_clock = c->last_cpu_time - c->last_verify_cpu_time;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700276
277 c->last_verify_cpu_time = c->last_cpu_time;
278 c->last_verify_reference_time = now_reference;
279
Dave Barachc3799992016-08-15 11:12:27 -0400280 /*
281 * Is the reported reference interval non-positive,
282 * or off by a factor of two - or 8 seconds - whichever is larger?
Ed Warnickecb9cada2015-12-08 15:45:58 -0700283 * Someone reset the clock behind our back.
284 */
Dave Barachc25048b2020-01-29 18:05:24 -0500285 delta_reference_max = (f64) (2ULL << c->log2_clocks_per_frequency_verify) /
Dave Barachc3799992016-08-15 11:12:27 -0400286 (f64) (1ULL << c->log2_clocks_per_second);
Dave Barachc25048b2020-01-29 18:05:24 -0500287 delta_reference_max = delta_reference_max > 8.0 ? delta_reference_max : 8.0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700288
Dave Barachc25048b2020-01-29 18:05:24 -0500289 /* Ignore this sample */
290 if (delta_reference <= 0.0 || delta_reference > delta_reference_max)
291 return;
Dave Barachba603ba2018-11-07 17:40:19 -0500292
Dave Barach36feebb2018-09-07 11:12:27 -0400293 /*
294 * Reject large frequency changes, another consequence of
295 * system clock changes particularly with old kernels.
296 */
Dave Barachc25048b2020-01-29 18:05:24 -0500297 new_clocks_per_second = ((f64) delta_clock) / delta_reference;
Dave Barach36feebb2018-09-07 11:12:27 -0400298
Dave Barache52d8d82019-12-01 08:59:03 -0500299 /* Compute abs(rate change) */
Dave Barach36feebb2018-09-07 11:12:27 -0400300 delta = new_clocks_per_second - c->clocks_per_second;
301 if (delta < 0.0)
302 delta = -delta;
303
Dave Barachc25048b2020-01-29 18:05:24 -0500304 /* If rate change > 1%, reject this sample */
Dave Barach36feebb2018-09-07 11:12:27 -0400305 if (PREDICT_FALSE ((delta / c->clocks_per_second) > .01))
306 {
307 clib_warning ("Rejecting large frequency change of %.2f%%",
308 (delta / c->clocks_per_second) * 100.0);
Dave Barach36feebb2018-09-07 11:12:27 -0400309 return;
310 }
311
Dave Barachc25048b2020-01-29 18:05:24 -0500312 /* Add sample to the exponentially-smoothed rate */
313 c->clocks_per_second = c->clocks_per_second * c->damping_constant +
314 (1.0 - c->damping_constant) * new_clocks_per_second;
315 c->seconds_per_clock = 1.0 / c->clocks_per_second;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700316
Dave Barache52d8d82019-12-01 08:59:03 -0500317 /*
Dave Barachc25048b2020-01-29 18:05:24 -0500318 * Recalculate total_cpu_time based on the kernel timebase, and
319 * the calculated clock rate
Dave Barache52d8d82019-12-01 08:59:03 -0500320 */
Dave Barachc25048b2020-01-29 18:05:24 -0500321 c->total_cpu_time =
322 (now_reference - c->init_reference_time) * c->clocks_per_second;
323}
Dave Barache52d8d82019-12-01 08:59:03 -0500324
Dave Barachc25048b2020-01-29 18:05:24 -0500325
Damjan Mariondae1c7e2020-10-17 13:32:25 +0200326__clib_export u8 *
Dave Barachc25048b2020-01-29 18:05:24 -0500327format_clib_time (u8 * s, va_list * args)
328{
329 clib_time_t *c = va_arg (*args, clib_time_t *);
330 int verbose = va_arg (*args, int);
331 f64 now, reftime, delta_reftime_in_seconds, error;
332
333 /* Compute vpp elapsed time from the CPU clock */
334 reftime = unix_time_now ();
335 now = clib_time_now (c);
336
337 s = format (s, "Time now %.6f", now);
338 if (verbose == 0)
339 return s;
340
341 /* And also from the kernel */
342 delta_reftime_in_seconds = reftime - c->init_reference_time;
343
344 error = now - delta_reftime_in_seconds;
345
346 s = format (s, ", reftime %.6f, error %.6f, clocks/sec %.6f",
347 delta_reftime_in_seconds, error, c->clocks_per_second);
348 return (s);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700349}
Dave Barachc3799992016-08-15 11:12:27 -0400350
351/*
352 * fd.io coding-style-patch-verification: ON
353 *
354 * Local Variables:
355 * eval: (c-set-style "gnu")
356 * End:
357 */