Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 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 Marion | c0e939b | 2016-11-12 11:50:01 +0100 | [diff] [blame] | 41 | #include <vppinfra/cpu.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 42 | |
| 43 | #ifdef CLIB_UNIX |
| 44 | |
| 45 | #include <math.h> |
| 46 | #include <sys/time.h> |
| 47 | #include <fcntl.h> |
| 48 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 49 | /* Not very accurate way of determining cpu clock frequency |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 50 | for unix. Better to use /proc/cpuinfo on linux. */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 51 | static f64 |
| 52 | estimate_clock_frequency (f64 sample_time) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 53 | { |
| 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 Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 75 | Only works for Linux. */ |
| 76 | static f64 |
| 77 | clock_frequency_from_proc_filesystem (void) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 78 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 79 | f64 cpu_freq = 1e9; /* better than 40... */ |
| 80 | f64 ppc_timebase = 0; /* warnings be gone */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 81 | int fd; |
| 82 | unformat_input_t input; |
| 83 | |
Dave Barach | 61efa14 | 2016-01-22 08:23:09 -0500 | [diff] [blame] | 84 | /* $$$$ aarch64 kernel doesn't report "cpu MHz" */ |
| 85 | #if defined(__aarch64__) |
| 86 | return 0.0; |
| 87 | #endif |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 88 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 89 | cpu_freq = 0; |
| 90 | fd = open ("/proc/cpuinfo", 0); |
| 91 | if (fd < 0) |
| 92 | return cpu_freq; |
| 93 | |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 94 | unformat_init_clib_file (&input, fd); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 95 | |
| 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 Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 119 | Only works for Linux. */ |
| 120 | static f64 |
| 121 | clock_frequency_from_sys_filesystem (void) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 122 | { |
| 123 | f64 cpu_freq; |
| 124 | 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 Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 133 | unformat_init_clib_file (&input, fd); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 134 | unformat (&input, "%f", &cpu_freq); |
| 135 | cpu_freq *= 1e3; /* measured in kHz */ |
| 136 | unformat_free (&input); |
| 137 | close (fd); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 138 | done: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 139 | return cpu_freq; |
| 140 | } |
| 141 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 142 | f64 |
| 143 | os_cpu_clock_frequency (void) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 144 | { |
Brian Brooks | c0379ae | 2018-01-09 16:39:07 -0600 | [diff] [blame] | 145 | #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 Das | 40c6e1d | 2018-05-29 21:21:02 -0500 | [diff] [blame] | 150 | u64 hz; |
Brian Brooks | c0379ae | 2018-01-09 16:39:07 -0600 | [diff] [blame] | 151 | asm volatile ("mrs %0, cntfrq_el0":"=r" (hz)); |
| 152 | return (f64) hz; |
| 153 | #endif |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 154 | f64 cpu_freq; |
| 155 | |
Damjan Marion | c0e939b | 2016-11-12 11:50:01 +0100 | [diff] [blame] | 156 | if (clib_cpu_supports_invariant_tsc ()) |
| 157 | return estimate_clock_frequency (1e-3); |
| 158 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 159 | /* First try /sys version. */ |
| 160 | cpu_freq = clock_frequency_from_sys_filesystem (); |
| 161 | if (cpu_freq != 0) |
| 162 | return cpu_freq; |
| 163 | |
| 164 | /* Next try /proc version. */ |
| 165 | cpu_freq = clock_frequency_from_proc_filesystem (); |
| 166 | if (cpu_freq != 0) |
| 167 | return cpu_freq; |
| 168 | |
| 169 | /* If /proc/cpuinfo fails (e.g. not running on Linux) fall back to |
| 170 | gettimeofday based estimated clock frequency. */ |
| 171 | return estimate_clock_frequency (1e-3); |
| 172 | } |
| 173 | |
| 174 | #endif /* CLIB_UNIX */ |
| 175 | |
| 176 | /* Initialize time. */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 177 | void |
| 178 | clib_time_init (clib_time_t * c) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 179 | { |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame^] | 180 | clib_memset (c, 0, sizeof (c[0])); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 181 | c->clocks_per_second = os_cpu_clock_frequency (); |
| 182 | c->seconds_per_clock = 1 / c->clocks_per_second; |
| 183 | c->log2_clocks_per_second = min_log2_u64 ((u64) c->clocks_per_second); |
| 184 | |
| 185 | /* Initially verify frequency every sec */ |
| 186 | c->log2_clocks_per_frequency_verify = c->log2_clocks_per_second; |
| 187 | |
| 188 | c->last_verify_reference_time = unix_time_now (); |
| 189 | c->last_cpu_time = clib_cpu_time_now (); |
| 190 | c->init_cpu_time = c->last_verify_cpu_time = c->last_cpu_time; |
| 191 | } |
| 192 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 193 | void |
| 194 | clib_time_verify_frequency (clib_time_t * c) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 195 | { |
| 196 | f64 now_reference = unix_time_now (); |
| 197 | f64 dtr = now_reference - c->last_verify_reference_time; |
| 198 | f64 dtr_max; |
| 199 | u64 dtc = c->last_cpu_time - c->last_verify_cpu_time; |
Dave Barach | 36feebb | 2018-09-07 11:12:27 -0400 | [diff] [blame] | 200 | f64 new_clocks_per_second, delta; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 201 | f64 round_units = 100e5; |
| 202 | |
| 203 | c->last_verify_cpu_time = c->last_cpu_time; |
| 204 | c->last_verify_reference_time = now_reference; |
| 205 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 206 | /* |
| 207 | * Is the reported reference interval non-positive, |
| 208 | * or off by a factor of two - or 8 seconds - whichever is larger? |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 209 | * Someone reset the clock behind our back. |
| 210 | */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 211 | dtr_max = (f64) (2ULL << c->log2_clocks_per_frequency_verify) / |
| 212 | (f64) (1ULL << c->log2_clocks_per_second); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 213 | dtr_max = dtr_max > 8.0 ? dtr_max : 8.0; |
| 214 | |
| 215 | if (dtr <= 0.0 || dtr > dtr_max) |
| 216 | { |
| 217 | c->log2_clocks_per_frequency_verify = c->log2_clocks_per_second; |
| 218 | return; |
| 219 | } |
| 220 | |
Dave Barach | 36feebb | 2018-09-07 11:12:27 -0400 | [diff] [blame] | 221 | /* |
| 222 | * Reject large frequency changes, another consequence of |
| 223 | * system clock changes particularly with old kernels. |
| 224 | */ |
| 225 | new_clocks_per_second = |
| 226 | flt_round_nearest ((f64) dtc / (dtr * round_units)) * round_units; |
| 227 | |
| 228 | delta = new_clocks_per_second - c->clocks_per_second; |
| 229 | if (delta < 0.0) |
| 230 | delta = -delta; |
| 231 | |
| 232 | if (PREDICT_FALSE ((delta / c->clocks_per_second) > .01)) |
| 233 | { |
| 234 | clib_warning ("Rejecting large frequency change of %.2f%%", |
| 235 | (delta / c->clocks_per_second) * 100.0); |
| 236 | c->log2_clocks_per_frequency_verify = c->log2_clocks_per_second; |
| 237 | return; |
| 238 | } |
| 239 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 240 | c->clocks_per_second = |
| 241 | flt_round_nearest ((f64) dtc / (dtr * round_units)) * round_units; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 242 | c->seconds_per_clock = 1 / c->clocks_per_second; |
| 243 | |
| 244 | /* Double time between verifies; max at 64 secs ~ 1 minute. */ |
| 245 | if (c->log2_clocks_per_frequency_verify < c->log2_clocks_per_second + 6) |
| 246 | c->log2_clocks_per_frequency_verify += 1; |
| 247 | } |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 248 | |
| 249 | /* |
| 250 | * fd.io coding-style-patch-verification: ON |
| 251 | * |
| 252 | * Local Variables: |
| 253 | * eval: (c-set-style "gnu") |
| 254 | * End: |
| 255 | */ |