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>
48 /* Not very accurate way of determining cpu clock frequency
49 for unix. Better to use /proc/cpuinfo on linux. */
50 static f64 estimate_clock_frequency (f64 sample_time)
52 /* Round to nearest 100KHz. */
53 const f64 round_to_units = 100e5;
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);
66 ifreq = flt_round_nearest (freq / round_to_units);
67 freq = ifreq * round_to_units;
72 /* Fetch cpu frequency via parseing /proc/cpuinfo.
73 Only works for Linux. */
74 static f64 clock_frequency_from_proc_filesystem (void)
76 f64 cpu_freq=1e9; /* better than 40... */
77 f64 ppc_timebase=0; /* warnings be gone */
79 unformat_input_t input;
82 fd = open ("/proc/cpuinfo", 0);
86 unformat_init_unix_file (&input, fd);
89 while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT)
91 if (unformat (&input, "cpu MHz : %f", &cpu_freq))
93 else if (unformat (&input, "timebase : %f", &ppc_timebase))
96 unformat_skip_line (&input);
99 unformat_free (&input);
103 /* Override CPU frequency with time base for PPC. */
104 if (ppc_timebase != 0)
105 cpu_freq = ppc_timebase;
110 /* Fetch cpu frequency via reading /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq
111 Only works for Linux. */
112 static f64 clock_frequency_from_sys_filesystem (void)
116 unformat_input_t input;
118 /* Time stamp always runs at max frequency. */
120 fd = open ("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq", 0);
124 unformat_init_unix_file (&input, fd);
125 unformat (&input, "%f", &cpu_freq);
126 cpu_freq *= 1e3; /* measured in kHz */
127 unformat_free (&input);
133 f64 os_cpu_clock_frequency (void)
137 /* First try /sys version. */
138 cpu_freq = clock_frequency_from_sys_filesystem ();
142 /* Next try /proc version. */
143 cpu_freq = clock_frequency_from_proc_filesystem ();
147 /* If /proc/cpuinfo fails (e.g. not running on Linux) fall back to
148 gettimeofday based estimated clock frequency. */
149 return estimate_clock_frequency (1e-3);
152 #endif /* CLIB_UNIX */
154 /* Initialize time. */
155 void clib_time_init (clib_time_t * c)
157 memset (c, 0, sizeof (c[0]));
158 c->clocks_per_second = os_cpu_clock_frequency ();
159 c->seconds_per_clock = 1 / c->clocks_per_second;
160 c->log2_clocks_per_second = min_log2_u64 ((u64) c->clocks_per_second);
162 /* Initially verify frequency every sec */
163 c->log2_clocks_per_frequency_verify = c->log2_clocks_per_second;
165 c->last_verify_reference_time = unix_time_now ();
166 c->last_cpu_time = clib_cpu_time_now ();
167 c->init_cpu_time = c->last_verify_cpu_time = c->last_cpu_time;
170 void clib_time_verify_frequency (clib_time_t * c)
172 f64 now_reference = unix_time_now ();
173 f64 dtr = now_reference - c->last_verify_reference_time;
175 u64 dtc = c->last_cpu_time - c->last_verify_cpu_time;
176 f64 round_units = 100e5;
178 c->last_verify_cpu_time = c->last_cpu_time;
179 c->last_verify_reference_time = now_reference;
182 * Is the reported reference interval non-positive,
183 * or off by a factor of two - or 8 seconds - whichever is larger?
184 * Someone reset the clock behind our back.
186 dtr_max = (f64)(2ULL<<c->log2_clocks_per_frequency_verify) /
187 (f64)(1ULL<<c->log2_clocks_per_second);
188 dtr_max = dtr_max > 8.0 ? dtr_max : 8.0;
190 if (dtr <= 0.0 || dtr > dtr_max)
192 c->log2_clocks_per_frequency_verify = c->log2_clocks_per_second;
196 c->clocks_per_second = flt_round_nearest ((f64) dtc / (dtr * round_units)) * round_units;
197 c->seconds_per_clock = 1 / c->clocks_per_second;
199 /* Double time between verifies; max at 64 secs ~ 1 minute. */
200 if (c->log2_clocks_per_frequency_verify < c->log2_clocks_per_second + 6)
201 c->log2_clocks_per_frequency_verify += 1;