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> |
Dave Barach | c25048b | 2020-01-29 18:05:24 -0500 | [diff] [blame] | 42 | #include <math.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 43 | |
| 44 | #ifdef CLIB_UNIX |
| 45 | |
| 46 | #include <math.h> |
| 47 | #include <sys/time.h> |
| 48 | #include <fcntl.h> |
| 49 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 50 | /* Not very accurate way of determining cpu clock frequency |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 51 | for unix. Better to use /proc/cpuinfo on linux. */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 52 | static f64 |
| 53 | estimate_clock_frequency (f64 sample_time) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 54 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 55 | f64 time_now, time_start, time_limit, freq; |
Dave Barach | c25048b | 2020-01-29 18:05:24 -0500 | [diff] [blame] | 56 | u64 t[2]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 57 | |
| 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 66 | |
| 67 | return freq; |
| 68 | } |
| 69 | |
| 70 | /* Fetch cpu frequency via parseing /proc/cpuinfo. |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 71 | Only works for Linux. */ |
| 72 | static f64 |
| 73 | clock_frequency_from_proc_filesystem (void) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 74 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 75 | f64 cpu_freq = 1e9; /* better than 40... */ |
| 76 | f64 ppc_timebase = 0; /* warnings be gone */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 77 | unformat_input_t input; |
| 78 | |
Xiaoming Jiang | 04da324 | 2024-02-22 21:24:20 +0800 | [diff] [blame] | 79 | #if defined(__x86_64__) |
| 80 | if (clib_cpu_supports_aperfmperf ()) |
| 81 | return 0.0; |
| 82 | #elif defined(__aarch64__) |
| 83 | /* $$$$ aarch64 kernel doesn't report "cpu MHz" */ |
Dave Barach | 61efa14 | 2016-01-22 08:23:09 -0500 | [diff] [blame] | 84 | return 0.0; |
| 85 | #endif |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 86 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 87 | cpu_freq = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 88 | |
| 89 | ppc_timebase = 0; |
Damjan Marion | 8d0c0c6 | 2023-08-06 20:39:38 +0200 | [diff] [blame] | 90 | if (unformat_init_file (&input, "/proc/cpuinfo")) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 91 | { |
Damjan Marion | 8d0c0c6 | 2023-08-06 20:39:38 +0200 | [diff] [blame] | 92 | while (unformat_check_input (&input) != UNFORMAT_END_OF_INPUT) |
| 93 | { |
| 94 | if (unformat (&input, "cpu MHz : %f", &cpu_freq)) |
| 95 | cpu_freq *= 1e6; |
| 96 | else if (unformat (&input, "timebase : %f", &ppc_timebase)) |
| 97 | ; |
| 98 | else |
| 99 | unformat_skip_line (&input); |
| 100 | } |
| 101 | |
| 102 | unformat_free (&input); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 103 | } |
Damjan Marion | 8d0c0c6 | 2023-08-06 20:39:38 +0200 | [diff] [blame] | 104 | else |
| 105 | return cpu_freq; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 106 | |
| 107 | /* Override CPU frequency with time base for PPC. */ |
| 108 | if (ppc_timebase != 0) |
| 109 | cpu_freq = ppc_timebase; |
| 110 | |
| 111 | return cpu_freq; |
| 112 | } |
| 113 | |
| 114 | /* 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] | 115 | Only works for Linux. */ |
| 116 | static f64 |
| 117 | clock_frequency_from_sys_filesystem (void) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 118 | { |
Dave Barach | 96e2d44 | 2018-11-14 11:42:03 -0500 | [diff] [blame] | 119 | f64 cpu_freq = 0.0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 120 | unformat_input_t input; |
| 121 | |
| 122 | /* Time stamp always runs at max frequency. */ |
| 123 | cpu_freq = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 124 | |
Damjan Marion | 8d0c0c6 | 2023-08-06 20:39:38 +0200 | [diff] [blame] | 125 | if (unformat_init_file ( |
| 126 | &input, "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq")) |
| 127 | { |
| 128 | if (unformat (&input, "%f", &cpu_freq)) |
| 129 | cpu_freq *= 1e3; /* measured in kHz */ |
| 130 | unformat_free (&input); |
| 131 | } |
| 132 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 133 | return cpu_freq; |
| 134 | } |
| 135 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 136 | __clib_export f64 |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 137 | os_cpu_clock_frequency (void) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 138 | { |
Brian Brooks | c0379ae | 2018-01-09 16:39:07 -0600 | [diff] [blame] | 139 | #if defined (__aarch64__) |
| 140 | /* The system counter increments at a fixed frequency. It is distributed |
| 141 | * to each core which has registers for reading the current counter value |
| 142 | * as well as the clock frequency. The system counter is not clocked at |
| 143 | * the same frequency as the core. */ |
Sirshak Das | 40c6e1d | 2018-05-29 21:21:02 -0500 | [diff] [blame] | 144 | u64 hz; |
Brian Brooks | c0379ae | 2018-01-09 16:39:07 -0600 | [diff] [blame] | 145 | asm volatile ("mrs %0, cntfrq_el0":"=r" (hz)); |
| 146 | return (f64) hz; |
| 147 | #endif |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 148 | f64 cpu_freq; |
| 149 | |
Damjan Marion | 12e3e31 | 2019-12-18 18:45:19 +0100 | [diff] [blame] | 150 | #ifdef __x86_64__ |
| 151 | u32 __clib_unused eax = 0, ebx = 0, ecx = 0, edx = 0; |
| 152 | clib_get_cpuid (0x00, &eax, &ebx, &ecx, &edx); |
| 153 | if (eax >= 0x15) |
| 154 | { |
| 155 | u32 max_leaf = eax; |
| 156 | /* |
| 157 | CPUID Leaf 0x15 - Time Stamp Counter and Nominal Core Crystal Clock Info |
| 158 | eax - denominator of the TSC/”core crystal clock” ratio |
| 159 | ebx - numerator of the TSC/”core crystal clock” ratio |
| 160 | ecx - nominal frequency of the core crystal clock in Hz |
| 161 | edx - reseved |
| 162 | */ |
| 163 | |
| 164 | clib_get_cpuid (0x15, &eax, &ebx, &ecx, &edx); |
| 165 | if (ebx && ecx) |
Damjan Marion | fa95842 | 2020-03-04 20:06:06 +0100 | [diff] [blame] | 166 | return (u64) ecx *ebx / eax; |
Damjan Marion | 12e3e31 | 2019-12-18 18:45:19 +0100 | [diff] [blame] | 167 | |
| 168 | if (max_leaf >= 0x16) |
| 169 | { |
| 170 | /* |
| 171 | CPUID Leaf 0x16 - Processor Frequency Information Leaf |
| 172 | eax - Bits 15 - 00: Processor Base Frequency (in MHz). |
| 173 | */ |
| 174 | |
| 175 | clib_get_cpuid (0x16, &eax, &ebx, &ecx, &edx); |
| 176 | if (eax) |
| 177 | return 1e6 * (eax & 0xffff); |
| 178 | } |
| 179 | } |
| 180 | #endif |
| 181 | |
Dave Barach | c25048b | 2020-01-29 18:05:24 -0500 | [diff] [blame] | 182 | /* If we have an invariant TSC, use it to estimate the clock frequency */ |
Damjan Marion | c0e939b | 2016-11-12 11:50:01 +0100 | [diff] [blame] | 183 | if (clib_cpu_supports_invariant_tsc ()) |
| 184 | return estimate_clock_frequency (1e-3); |
| 185 | |
Dave Barach | c25048b | 2020-01-29 18:05:24 -0500 | [diff] [blame] | 186 | /* Next, try /sys version. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 187 | cpu_freq = clock_frequency_from_sys_filesystem (); |
| 188 | if (cpu_freq != 0) |
| 189 | return cpu_freq; |
| 190 | |
| 191 | /* Next try /proc version. */ |
| 192 | cpu_freq = clock_frequency_from_proc_filesystem (); |
| 193 | if (cpu_freq != 0) |
| 194 | return cpu_freq; |
| 195 | |
| 196 | /* If /proc/cpuinfo fails (e.g. not running on Linux) fall back to |
| 197 | gettimeofday based estimated clock frequency. */ |
| 198 | return estimate_clock_frequency (1e-3); |
| 199 | } |
| 200 | |
| 201 | #endif /* CLIB_UNIX */ |
| 202 | |
| 203 | /* Initialize time. */ |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 204 | __clib_export void |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 205 | clib_time_init (clib_time_t * c) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 206 | { |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 207 | clib_memset (c, 0, sizeof (c[0])); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 208 | c->clocks_per_second = os_cpu_clock_frequency (); |
Dave Barach | 51cabf2 | 2020-02-04 16:10:17 -0500 | [diff] [blame] | 209 | /* |
| 210 | * Sporadic reports of os_cpu_clock_frequency() returning 0.0 |
| 211 | * in highly parallel container environments. |
| 212 | * To avoid immediate division by zero: |
| 213 | * Step 1: try estimate_clock_frequency(). |
| 214 | * Step 2: give up. Pretend we have a 2gHz clock. |
| 215 | */ |
| 216 | if (PREDICT_FALSE (c->clocks_per_second == 0.0)) |
| 217 | { |
| 218 | c->clocks_per_second = estimate_clock_frequency (1e-3); |
| 219 | if (c->clocks_per_second == 0.0) |
| 220 | { |
| 221 | clib_warning ("os_cpu_clock_frequency() returned 0.0, use 2e9..."); |
| 222 | c->clocks_per_second = 2e9; |
| 223 | } |
| 224 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 225 | c->seconds_per_clock = 1 / c->clocks_per_second; |
| 226 | c->log2_clocks_per_second = min_log2_u64 ((u64) c->clocks_per_second); |
| 227 | |
Dave Barach | c25048b | 2020-01-29 18:05:24 -0500 | [diff] [blame] | 228 | /* Verify frequency every 16 sec */ |
| 229 | c->log2_clocks_per_frequency_verify = c->log2_clocks_per_second + 4; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 230 | |
| 231 | c->last_verify_reference_time = unix_time_now (); |
Dave Barach | c25048b | 2020-01-29 18:05:24 -0500 | [diff] [blame] | 232 | c->init_reference_time = c->last_verify_reference_time; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 233 | c->last_cpu_time = clib_cpu_time_now (); |
| 234 | c->init_cpu_time = c->last_verify_cpu_time = c->last_cpu_time; |
Dave Barach | c25048b | 2020-01-29 18:05:24 -0500 | [diff] [blame] | 235 | c->total_cpu_time = 0ULL; |
| 236 | |
| 237 | /* |
| 238 | * Use exponential smoothing, with a half-life of 1 minute |
| 239 | * reported_rate(t) = reported_rate(t-1) * K + rate(t)*(1-K) |
| 240 | * where K = e**(-1.0/3.75); |
| 241 | * 15 samples in 4 minutes |
| 242 | * 7.5 samples in 2 minutes, |
| 243 | * 3.75 samples in 1 minute, etc. |
| 244 | */ |
| 245 | c->damping_constant = exp (-1.0 / 3.75); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 248 | __clib_export void |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 249 | clib_time_verify_frequency (clib_time_t * c) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 250 | { |
Dave Barach | c25048b | 2020-01-29 18:05:24 -0500 | [diff] [blame] | 251 | f64 now_reference, delta_reference, delta_reference_max; |
Dave Barach | 66441c4 | 2020-03-10 09:01:02 -0400 | [diff] [blame] | 252 | f64 delta_clock_in_seconds; |
| 253 | u64 now_clock, delta_clock; |
Dave Barach | 36feebb | 2018-09-07 11:12:27 -0400 | [diff] [blame] | 254 | f64 new_clocks_per_second, delta; |
Dave Barach | c25048b | 2020-01-29 18:05:24 -0500 | [diff] [blame] | 255 | |
| 256 | /* Ask the kernel and the CPU what time it is... */ |
| 257 | now_reference = unix_time_now (); |
Dave Barach | 66441c4 | 2020-03-10 09:01:02 -0400 | [diff] [blame] | 258 | now_clock = clib_cpu_time_now (); |
| 259 | |
| 260 | /* Compute change in the reference clock */ |
| 261 | delta_reference = now_reference - c->last_verify_reference_time; |
| 262 | |
| 263 | /* And change in the CPU clock */ |
| 264 | delta_clock_in_seconds = (f64) (now_clock - c->last_verify_cpu_time) * |
| 265 | c->seconds_per_clock; |
| 266 | |
| 267 | /* |
| 268 | * Recompute vpp start time reference, and total clocks |
| 269 | * using the current clock rate |
| 270 | */ |
| 271 | c->init_reference_time += (delta_reference - delta_clock_in_seconds); |
| 272 | c->total_cpu_time = (now_reference - c->init_reference_time) |
| 273 | * c->clocks_per_second; |
| 274 | |
| 275 | c->last_cpu_time = now_clock; |
Dave Barach | c25048b | 2020-01-29 18:05:24 -0500 | [diff] [blame] | 276 | |
| 277 | /* Calculate a new clock rate sample */ |
Dave Barach | c25048b | 2020-01-29 18:05:24 -0500 | [diff] [blame] | 278 | delta_clock = c->last_cpu_time - c->last_verify_cpu_time; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 279 | |
| 280 | c->last_verify_cpu_time = c->last_cpu_time; |
| 281 | c->last_verify_reference_time = now_reference; |
| 282 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 283 | /* |
| 284 | * Is the reported reference interval non-positive, |
| 285 | * 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] | 286 | * Someone reset the clock behind our back. |
| 287 | */ |
Dave Barach | c25048b | 2020-01-29 18:05:24 -0500 | [diff] [blame] | 288 | delta_reference_max = (f64) (2ULL << c->log2_clocks_per_frequency_verify) / |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 289 | (f64) (1ULL << c->log2_clocks_per_second); |
Dave Barach | c25048b | 2020-01-29 18:05:24 -0500 | [diff] [blame] | 290 | delta_reference_max = delta_reference_max > 8.0 ? delta_reference_max : 8.0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 291 | |
Dave Barach | c25048b | 2020-01-29 18:05:24 -0500 | [diff] [blame] | 292 | /* Ignore this sample */ |
| 293 | if (delta_reference <= 0.0 || delta_reference > delta_reference_max) |
| 294 | return; |
Dave Barach | ba603ba | 2018-11-07 17:40:19 -0500 | [diff] [blame] | 295 | |
Dave Barach | 36feebb | 2018-09-07 11:12:27 -0400 | [diff] [blame] | 296 | /* |
| 297 | * Reject large frequency changes, another consequence of |
| 298 | * system clock changes particularly with old kernels. |
| 299 | */ |
Dave Barach | c25048b | 2020-01-29 18:05:24 -0500 | [diff] [blame] | 300 | new_clocks_per_second = ((f64) delta_clock) / delta_reference; |
Dave Barach | 36feebb | 2018-09-07 11:12:27 -0400 | [diff] [blame] | 301 | |
Dave Barach | e52d8d8 | 2019-12-01 08:59:03 -0500 | [diff] [blame] | 302 | /* Compute abs(rate change) */ |
Dave Barach | 36feebb | 2018-09-07 11:12:27 -0400 | [diff] [blame] | 303 | delta = new_clocks_per_second - c->clocks_per_second; |
| 304 | if (delta < 0.0) |
| 305 | delta = -delta; |
| 306 | |
Dave Barach | c25048b | 2020-01-29 18:05:24 -0500 | [diff] [blame] | 307 | /* If rate change > 1%, reject this sample */ |
Dave Barach | 36feebb | 2018-09-07 11:12:27 -0400 | [diff] [blame] | 308 | if (PREDICT_FALSE ((delta / c->clocks_per_second) > .01)) |
| 309 | { |
| 310 | clib_warning ("Rejecting large frequency change of %.2f%%", |
| 311 | (delta / c->clocks_per_second) * 100.0); |
Dave Barach | 36feebb | 2018-09-07 11:12:27 -0400 | [diff] [blame] | 312 | return; |
| 313 | } |
| 314 | |
Dave Barach | c25048b | 2020-01-29 18:05:24 -0500 | [diff] [blame] | 315 | /* Add sample to the exponentially-smoothed rate */ |
| 316 | c->clocks_per_second = c->clocks_per_second * c->damping_constant + |
| 317 | (1.0 - c->damping_constant) * new_clocks_per_second; |
| 318 | c->seconds_per_clock = 1.0 / c->clocks_per_second; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 319 | |
Dave Barach | e52d8d8 | 2019-12-01 08:59:03 -0500 | [diff] [blame] | 320 | /* |
Dave Barach | c25048b | 2020-01-29 18:05:24 -0500 | [diff] [blame] | 321 | * Recalculate total_cpu_time based on the kernel timebase, and |
| 322 | * the calculated clock rate |
Dave Barach | e52d8d8 | 2019-12-01 08:59:03 -0500 | [diff] [blame] | 323 | */ |
Dave Barach | c25048b | 2020-01-29 18:05:24 -0500 | [diff] [blame] | 324 | c->total_cpu_time = |
| 325 | (now_reference - c->init_reference_time) * c->clocks_per_second; |
| 326 | } |
Dave Barach | e52d8d8 | 2019-12-01 08:59:03 -0500 | [diff] [blame] | 327 | |
Dave Barach | c25048b | 2020-01-29 18:05:24 -0500 | [diff] [blame] | 328 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 329 | __clib_export u8 * |
Dave Barach | c25048b | 2020-01-29 18:05:24 -0500 | [diff] [blame] | 330 | format_clib_time (u8 * s, va_list * args) |
| 331 | { |
| 332 | clib_time_t *c = va_arg (*args, clib_time_t *); |
| 333 | int verbose = va_arg (*args, int); |
| 334 | f64 now, reftime, delta_reftime_in_seconds, error; |
| 335 | |
| 336 | /* Compute vpp elapsed time from the CPU clock */ |
| 337 | reftime = unix_time_now (); |
| 338 | now = clib_time_now (c); |
| 339 | |
| 340 | s = format (s, "Time now %.6f", now); |
| 341 | if (verbose == 0) |
| 342 | return s; |
| 343 | |
| 344 | /* And also from the kernel */ |
| 345 | delta_reftime_in_seconds = reftime - c->init_reference_time; |
| 346 | |
| 347 | error = now - delta_reftime_in_seconds; |
| 348 | |
| 349 | s = format (s, ", reftime %.6f, error %.6f, clocks/sec %.6f", |
| 350 | delta_reftime_in_seconds, error, c->clocks_per_second); |
| 351 | return (s); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 352 | } |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 353 | |
| 354 | /* |
| 355 | * fd.io coding-style-patch-verification: ON |
| 356 | * |
| 357 | * Local Variables: |
| 358 | * eval: (c-set-style "gnu") |
| 359 | * End: |
| 360 | */ |