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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 Copyright (c) 2005 Eliot Dresselhaus
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:
26 The above copyright notice and this permission notice shall be
27 included in all copies or substantial portions of the Software.
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.
38 #include <vppinfra/os.h>
39 #include <vppinfra/time.h>
40 #include <vppinfra/format.h>
41 #include <vppinfra/cpu.h>
50 /* Not very accurate way of determining cpu clock frequency
51 for unix. Better to use /proc/cpuinfo on linux. */
53 estimate_clock_frequency (f64 sample_time)
55 f64 time_now, time_start, time_limit, freq;
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 ();
65 freq = (t[1] - t[0]) / (time_now - time_start);
70 /* Fetch cpu frequency via parseing /proc/cpuinfo.
71 Only works for Linux. */
73 clock_frequency_from_proc_filesystem (void)
75 f64 cpu_freq = 1e9; /* better than 40... */
76 f64 ppc_timebase = 0; /* warnings be gone */
78 unformat_input_t input;
80 /* $$$$ aarch64 kernel doesn't report "cpu MHz" */
81 #if defined(__aarch64__)
86 fd = open ("/proc/cpuinfo", 0);
90 unformat_init_clib_file (&input, fd);
93 while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
95 if (unformat (&input, "cpu MHz : %f", &cpu_freq))
97 else if (unformat (&input, "timebase : %f", &ppc_timebase))
100 unformat_skip_line (&input);
103 unformat_free (&input);
107 /* Override CPU frequency with time base for PPC. */
108 if (ppc_timebase != 0)
109 cpu_freq = ppc_timebase;
114 /* Fetch cpu frequency via reading /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq
115 Only works for Linux. */
117 clock_frequency_from_sys_filesystem (void)
121 unformat_input_t input;
123 /* Time stamp always runs at max frequency. */
125 fd = open ("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq", 0);
129 unformat_init_clib_file (&input, fd);
130 (void) unformat (&input, "%f", &cpu_freq);
131 cpu_freq *= 1e3; /* measured in kHz */
132 unformat_free (&input);
139 os_cpu_clock_frequency (void)
141 #if defined (__aarch64__)
142 /* The system counter increments at a fixed frequency. It is distributed
143 * to each core which has registers for reading the current counter value
144 * as well as the clock frequency. The system counter is not clocked at
145 * the same frequency as the core. */
147 asm volatile ("mrs %0, cntfrq_el0":"=r" (hz));
153 u32 __clib_unused eax = 0, ebx = 0, ecx = 0, edx = 0;
154 clib_get_cpuid (0x00, &eax, &ebx, &ecx, &edx);
159 CPUID Leaf 0x15 - Time Stamp Counter and Nominal Core Crystal Clock Info
160 eax - denominator of the TSC/”core crystal clock” ratio
161 ebx - numerator of the TSC/”core crystal clock” ratio
162 ecx - nominal frequency of the core crystal clock in Hz
166 clib_get_cpuid (0x15, &eax, &ebx, &ecx, &edx);
168 return ecx * ebx / eax;
170 if (max_leaf >= 0x16)
173 CPUID Leaf 0x16 - Processor Frequency Information Leaf
174 eax - Bits 15 - 00: Processor Base Frequency (in MHz).
177 clib_get_cpuid (0x16, &eax, &ebx, &ecx, &edx);
179 return 1e6 * (eax & 0xffff);
184 /* If we have an invariant TSC, use it to estimate the clock frequency */
185 if (clib_cpu_supports_invariant_tsc ())
186 return estimate_clock_frequency (1e-3);
188 /* Next, try /sys version. */
189 cpu_freq = clock_frequency_from_sys_filesystem ();
193 /* Next try /proc version. */
194 cpu_freq = clock_frequency_from_proc_filesystem ();
198 /* If /proc/cpuinfo fails (e.g. not running on Linux) fall back to
199 gettimeofday based estimated clock frequency. */
200 return estimate_clock_frequency (1e-3);
203 #endif /* CLIB_UNIX */
205 /* Initialize time. */
207 clib_time_init (clib_time_t * c)
209 clib_memset (c, 0, sizeof (c[0]));
210 c->clocks_per_second = os_cpu_clock_frequency ();
211 c->seconds_per_clock = 1 / c->clocks_per_second;
212 c->log2_clocks_per_second = min_log2_u64 ((u64) c->clocks_per_second);
214 /* Verify frequency every 16 sec */
215 c->log2_clocks_per_frequency_verify = c->log2_clocks_per_second + 4;
217 c->last_verify_reference_time = unix_time_now ();
218 c->init_reference_time = c->last_verify_reference_time;
219 c->last_cpu_time = clib_cpu_time_now ();
220 c->init_cpu_time = c->last_verify_cpu_time = c->last_cpu_time;
221 c->total_cpu_time = 0ULL;
224 * Use exponential smoothing, with a half-life of 1 minute
225 * reported_rate(t) = reported_rate(t-1) * K + rate(t)*(1-K)
226 * where K = e**(-1.0/3.75);
227 * 15 samples in 4 minutes
228 * 7.5 samples in 2 minutes,
229 * 3.75 samples in 1 minute, etc.
231 c->damping_constant = exp (-1.0 / 3.75);
235 clib_time_verify_frequency (clib_time_t * c)
237 f64 now_reference, delta_reference, delta_reference_max;
239 f64 new_clocks_per_second, delta;
241 /* Ask the kernel and the CPU what time it is... */
242 now_reference = unix_time_now ();
243 c->last_cpu_time = clib_cpu_time_now ();
245 /* Calculate a new clock rate sample */
246 delta_reference = now_reference - c->last_verify_reference_time;
247 delta_clock = c->last_cpu_time - c->last_verify_cpu_time;
249 c->last_verify_cpu_time = c->last_cpu_time;
250 c->last_verify_reference_time = now_reference;
253 * Is the reported reference interval non-positive,
254 * or off by a factor of two - or 8 seconds - whichever is larger?
255 * Someone reset the clock behind our back.
257 delta_reference_max = (f64) (2ULL << c->log2_clocks_per_frequency_verify) /
258 (f64) (1ULL << c->log2_clocks_per_second);
259 delta_reference_max = delta_reference_max > 8.0 ? delta_reference_max : 8.0;
261 /* Ignore this sample */
262 if (delta_reference <= 0.0 || delta_reference > delta_reference_max)
266 * Reject large frequency changes, another consequence of
267 * system clock changes particularly with old kernels.
269 new_clocks_per_second = ((f64) delta_clock) / delta_reference;
271 /* Compute abs(rate change) */
272 delta = new_clocks_per_second - c->clocks_per_second;
276 /* If rate change > 1%, reject this sample */
277 if (PREDICT_FALSE ((delta / c->clocks_per_second) > .01))
279 clib_warning ("Rejecting large frequency change of %.2f%%",
280 (delta / c->clocks_per_second) * 100.0);
284 /* Add sample to the exponentially-smoothed rate */
285 c->clocks_per_second = c->clocks_per_second * c->damping_constant +
286 (1.0 - c->damping_constant) * new_clocks_per_second;
287 c->seconds_per_clock = 1.0 / c->clocks_per_second;
290 * Recalculate total_cpu_time based on the kernel timebase, and
291 * the calculated clock rate
294 (now_reference - c->init_reference_time) * c->clocks_per_second;
299 format_clib_time (u8 * s, va_list * args)
301 clib_time_t *c = va_arg (*args, clib_time_t *);
302 int verbose = va_arg (*args, int);
303 f64 now, reftime, delta_reftime_in_seconds, error;
305 /* Compute vpp elapsed time from the CPU clock */
306 reftime = unix_time_now ();
307 now = clib_time_now (c);
309 s = format (s, "Time now %.6f", now);
313 /* And also from the kernel */
314 delta_reftime_in_seconds = reftime - c->init_reference_time;
316 error = now - delta_reftime_in_seconds;
318 s = format (s, ", reftime %.6f, error %.6f, clocks/sec %.6f",
319 delta_reftime_in_seconds, error, c->clocks_per_second);
324 * fd.io coding-style-patch-verification: ON
327 * eval: (c-set-style "gnu")