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) 2001, 2002, 2003 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 | #ifndef included_time_h |
| 39 | #define included_time_h |
| 40 | |
| 41 | #include <vppinfra/clib.h> |
| 42 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 43 | typedef struct |
| 44 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 45 | /* Total run time in clock cycles |
| 46 | since clib_time_init call. */ |
| 47 | u64 total_cpu_time; |
| 48 | |
| 49 | /* Last recorded time stamp. */ |
| 50 | u64 last_cpu_time; |
| 51 | |
| 52 | /* CPU clock frequency. */ |
| 53 | f64 clocks_per_second; |
| 54 | |
| 55 | /* 1 / cpu clock frequency: conversion factor |
| 56 | from clock cycles into seconds. */ |
| 57 | f64 seconds_per_clock; |
| 58 | |
| 59 | /* Time stamp of call to clib_time_init call. */ |
| 60 | u64 init_cpu_time; |
| 61 | |
| 62 | u64 last_verify_cpu_time; |
| 63 | |
| 64 | /* Same but for reference time (if present). */ |
| 65 | f64 last_verify_reference_time; |
| 66 | |
| 67 | u32 log2_clocks_per_second, log2_clocks_per_frequency_verify; |
| 68 | } clib_time_t; |
| 69 | |
| 70 | /* Return CPU time stamp as 64bit number. */ |
| 71 | #if defined(__x86_64__) || defined(i386) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 72 | always_inline u64 |
| 73 | clib_cpu_time_now (void) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 74 | { |
| 75 | u32 a, d; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 76 | asm volatile ("rdtsc":"=a" (a), "=d" (d)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 77 | return (u64) a + ((u64) d << (u64) 32); |
| 78 | } |
| 79 | |
| 80 | #elif defined (__powerpc64__) |
| 81 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 82 | always_inline u64 |
| 83 | clib_cpu_time_now (void) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 84 | { |
| 85 | u64 t; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 86 | asm volatile ("mftb %0":"=r" (t)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 87 | return t; |
| 88 | } |
| 89 | |
| 90 | #elif defined (__SPU__) |
| 91 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 92 | always_inline u64 |
| 93 | clib_cpu_time_now (void) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 94 | { |
| 95 | #ifdef _XLC |
| 96 | return spu_rdch (0x8); |
| 97 | #else |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 98 | return 0 /* __builtin_si_rdch (0x8) FIXME */ ; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 99 | #endif |
| 100 | } |
| 101 | |
| 102 | #elif defined (__powerpc__) |
| 103 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 104 | always_inline u64 |
| 105 | clib_cpu_time_now (void) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 106 | { |
| 107 | u32 hi1, hi2, lo; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 108 | asm volatile ("1:\n" |
| 109 | "mftbu %[hi1]\n" |
| 110 | "mftb %[lo]\n" |
| 111 | "mftbu %[hi2]\n" |
| 112 | "cmpw %[hi1],%[hi2]\n" |
| 113 | "bne 1b\n":[hi1] "=r" (hi1),[hi2] "=r" (hi2),[lo] "=r" (lo)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 114 | return (u64) lo + ((u64) hi2 << (u64) 32); |
| 115 | } |
| 116 | |
| 117 | #elif defined (__arm__) |
Christophe Fontaine | 9341a1f | 2016-05-13 07:07:28 +0000 | [diff] [blame] | 118 | #if defined(__ARM_ARCH_8A__) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 119 | always_inline u64 |
| 120 | clib_cpu_time_now (void) /* We may run arm64 in aarch32 mode, to leverage 64bit counter */ |
Christophe Fontaine | fef15b4 | 2016-04-09 12:38:49 +0900 | [diff] [blame] | 121 | { |
| 122 | u64 tsc; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 123 | asm volatile ("mrrc p15, 0, %Q0, %R0, c9":"=r" (tsc)); |
Christophe Fontaine | fef15b4 | 2016-04-09 12:38:49 +0900 | [diff] [blame] | 124 | return tsc; |
| 125 | } |
Christophe Fontaine | 9341a1f | 2016-05-13 07:07:28 +0000 | [diff] [blame] | 126 | #elif defined(__ARM_ARCH_7A__) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 127 | always_inline u64 |
| 128 | clib_cpu_time_now (void) |
Christophe Fontaine | 9341a1f | 2016-05-13 07:07:28 +0000 | [diff] [blame] | 129 | { |
| 130 | u32 tsc; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 131 | asm volatile ("mrc p15, 0, %0, c9, c13, 0":"=r" (tsc)); |
| 132 | return (u64) tsc; |
Christophe Fontaine | 9341a1f | 2016-05-13 07:07:28 +0000 | [diff] [blame] | 133 | } |
Christophe Fontaine | fef15b4 | 2016-04-09 12:38:49 +0900 | [diff] [blame] | 134 | #else |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 135 | always_inline u64 |
| 136 | clib_cpu_time_now (void) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 137 | { |
| 138 | u32 lo; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 139 | asm volatile ("mrc p15, 0, %[lo], c15, c12, 1":[lo] "=r" (lo)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 140 | return (u64) lo; |
| 141 | } |
Christophe Fontaine | fef15b4 | 2016-04-09 12:38:49 +0900 | [diff] [blame] | 142 | #endif |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 143 | |
| 144 | #elif defined (__xtensa__) |
| 145 | |
| 146 | /* Stub for now. */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 147 | always_inline u64 |
| 148 | clib_cpu_time_now (void) |
| 149 | { |
| 150 | return 0; |
| 151 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 152 | |
| 153 | #elif defined (__TMS320C6X__) |
| 154 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 155 | always_inline u64 |
| 156 | clib_cpu_time_now (void) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 157 | { |
| 158 | u32 l, h; |
| 159 | |
| 160 | asm volatile (" dint\n" |
| 161 | " mvc .s2 TSCL,%0\n" |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 162 | " mvc .s2 TSCH,%1\n" " rint\n":"=b" (l), "=b" (h)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 163 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 164 | return ((u64) h << 32) | l; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Dave Barach | 61efa14 | 2016-01-22 08:23:09 -0500 | [diff] [blame] | 167 | #elif defined (__aarch64__) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 168 | always_inline u64 |
| 169 | clib_cpu_time_now (void) |
Dave Barach | 61efa14 | 2016-01-22 08:23:09 -0500 | [diff] [blame] | 170 | { |
| 171 | u64 tsc; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 172 | |
Dave Barach | 61efa14 | 2016-01-22 08:23:09 -0500 | [diff] [blame] | 173 | /* Works on Cavium ThunderX. Other platforms: YMMV */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 174 | asm volatile ("mrs %0, cntvct_el0":"=r" (tsc)); |
Dave Barach | 61efa14 | 2016-01-22 08:23:09 -0500 | [diff] [blame] | 175 | |
| 176 | return tsc; |
| 177 | } |
| 178 | |
| 179 | #else |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 180 | #error "don't know how to read CPU time stamp" |
| 181 | |
| 182 | #endif |
| 183 | |
| 184 | void clib_time_verify_frequency (clib_time_t * c); |
| 185 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 186 | always_inline f64 |
| 187 | clib_time_now_internal (clib_time_t * c, u64 n) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 188 | { |
| 189 | u64 l = c->last_cpu_time; |
| 190 | u64 t = c->total_cpu_time; |
| 191 | t += n - l; |
| 192 | c->total_cpu_time = t; |
| 193 | c->last_cpu_time = n; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 194 | if (PREDICT_FALSE |
| 195 | ((c->last_cpu_time - |
| 196 | c->last_verify_cpu_time) >> c->log2_clocks_per_frequency_verify)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 197 | clib_time_verify_frequency (c); |
| 198 | return t * c->seconds_per_clock; |
| 199 | } |
| 200 | |
| 201 | always_inline f64 |
| 202 | clib_time_now (clib_time_t * c) |
| 203 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 204 | return clib_time_now_internal (c, clib_cpu_time_now ()); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 207 | always_inline void |
| 208 | clib_cpu_time_wait (u64 dt) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 209 | { |
| 210 | u64 t_end = clib_cpu_time_now () + dt; |
| 211 | while (clib_cpu_time_now () < t_end) |
| 212 | ; |
| 213 | } |
| 214 | |
| 215 | void clib_time_init (clib_time_t * c); |
| 216 | |
| 217 | #ifdef CLIB_UNIX |
| 218 | |
| 219 | #include <time.h> |
| 220 | #include <sys/time.h> |
| 221 | #include <sys/resource.h> |
| 222 | #include <unistd.h> |
| 223 | #include <sys/syscall.h> |
| 224 | |
| 225 | /* Use 64bit floating point to represent time offset from epoch. */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 226 | always_inline f64 |
| 227 | unix_time_now (void) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 228 | { |
| 229 | /* clock_gettime without indirect syscall uses GLIBC wrappers which |
| 230 | we don't want. Just the bare metal, please. */ |
| 231 | struct timespec ts; |
| 232 | syscall (SYS_clock_gettime, CLOCK_REALTIME, &ts); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 233 | return ts.tv_sec + 1e-9 * ts.tv_nsec; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | /* As above but integer number of nano-seconds. */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 237 | always_inline u64 |
| 238 | unix_time_now_nsec (void) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 239 | { |
| 240 | struct timespec ts; |
| 241 | syscall (SYS_clock_gettime, CLOCK_REALTIME, &ts); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 242 | return 1e9 * ts.tv_sec + ts.tv_nsec; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 243 | } |
| 244 | |
Ole Troan | ed92925 | 2017-06-13 21:15:40 +0200 | [diff] [blame] | 245 | always_inline void |
| 246 | unix_time_now_nsec_fraction (u32 * sec, u32 * nsec) |
| 247 | { |
| 248 | struct timespec ts; |
| 249 | syscall (SYS_clock_gettime, CLOCK_REALTIME, &ts); |
| 250 | *sec = ts.tv_sec; |
| 251 | *nsec = ts.tv_nsec; |
| 252 | } |
| 253 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 254 | always_inline f64 |
| 255 | unix_usage_now (void) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 256 | { |
| 257 | struct rusage u; |
| 258 | getrusage (RUSAGE_SELF, &u); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 259 | return u.ru_utime.tv_sec + 1e-6 * u.ru_utime.tv_usec |
| 260 | + u.ru_stime.tv_sec + 1e-6 * u.ru_stime.tv_usec; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 263 | always_inline void |
| 264 | unix_sleep (f64 dt) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 265 | { |
| 266 | struct timespec t; |
| 267 | t.tv_sec = dt; |
| 268 | t.tv_nsec = 1e9 * dt; |
| 269 | nanosleep (&t, 0); |
| 270 | } |
| 271 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 272 | #else /* ! CLIB_UNIX */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 273 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 274 | always_inline f64 |
| 275 | unix_time_now (void) |
| 276 | { |
| 277 | return 0; |
| 278 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 279 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 280 | always_inline u64 |
| 281 | unix_time_now_nsec (void) |
| 282 | { |
| 283 | return 0; |
| 284 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 285 | |
Ole Troan | ed92925 | 2017-06-13 21:15:40 +0200 | [diff] [blame] | 286 | always_inline void |
| 287 | unix_time_now_nsec_fraction (u32 * sec, u32 * nsec) |
| 288 | { |
| 289 | } |
| 290 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 291 | always_inline f64 |
| 292 | unix_usage_now (void) |
| 293 | { |
| 294 | return 0; |
| 295 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 296 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 297 | always_inline void |
| 298 | unix_sleep (f64 dt) |
| 299 | { |
| 300 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 301 | |
| 302 | #endif |
| 303 | |
| 304 | #endif /* included_time_h */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 305 | |
| 306 | /* |
| 307 | * fd.io coding-style-patch-verification: ON |
| 308 | * |
| 309 | * Local Variables: |
| 310 | * eval: (c-set-style "gnu") |
| 311 | * End: |
| 312 | */ |