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) 2001, 2002, 2003 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 #ifndef included_time_h
39 #define included_time_h
41 #include <vppinfra/clib.h>
42 #include <vppinfra/format.h>
46 /* Total run time in clock cycles
47 since clib_time_init call. */
50 /* Last recorded time stamp. */
53 /* CPU clock frequency. */
54 f64 clocks_per_second;
56 /* 1 / cpu clock frequency: conversion factor
57 from clock cycles into seconds. */
58 f64 seconds_per_clock;
60 /* Time stamp of call to clib_time_init call. */
62 f64 init_reference_time;
64 u64 last_verify_cpu_time;
66 /* Same but for reference time (if present). */
67 f64 last_verify_reference_time;
69 u32 log2_clocks_per_second, log2_clocks_per_frequency_verify;
71 /* Damping constant */
76 format_function_t format_clib_time;
78 /* Return CPU time stamp as 64bit number. */
79 #if defined(__x86_64__) || defined(i386)
81 clib_cpu_time_now (void)
84 asm volatile ("rdtsc":"=a" (a), "=d" (d));
85 return (u64) a + ((u64) d << (u64) 32);
88 #elif defined (__powerpc64__)
91 clib_cpu_time_now (void)
94 asm volatile ("mftb %0":"=r" (t));
98 #elif defined (__SPU__)
101 clib_cpu_time_now (void)
104 return spu_rdch (0x8);
106 return 0 /* __builtin_si_rdch (0x8) FIXME */ ;
110 #elif defined (__powerpc__)
113 clib_cpu_time_now (void)
120 "cmpw %[hi1],%[hi2]\n"
121 "bne 1b\n":[hi1] "=r" (hi1),[hi2] "=r" (hi2),[lo] "=r" (lo));
122 return (u64) lo + ((u64) hi2 << (u64) 32);
125 #elif defined (__aarch64__)
127 clib_cpu_time_now (void)
130 /* User access to cntvct_el0 is enabled in Linux kernel since 3.12. */
131 asm volatile ("mrs %0, cntvct_el0":"=r" (vct));
135 #elif defined (__arm__)
136 #if defined(__ARM_ARCH_8A__)
138 clib_cpu_time_now (void) /* We may run arm64 in aarch32 mode, to leverage 64bit counter */
141 asm volatile ("mrrc p15, 0, %Q0, %R0, c9":"=r" (tsc));
144 #elif defined(__ARM_ARCH_7A__)
146 clib_cpu_time_now (void)
149 asm volatile ("mrc p15, 0, %0, c9, c13, 0":"=r" (tsc));
154 clib_cpu_time_now (void)
157 asm volatile ("mrc p15, 0, %[lo], c15, c12, 1":[lo] "=r" (lo));
162 #elif defined (__xtensa__)
166 clib_cpu_time_now (void)
171 #elif defined (__TMS320C6X__)
174 clib_cpu_time_now (void)
178 asm volatile (" dint\n"
180 " mvc .s2 TSCH,%1\n" " rint\n":"=b" (l), "=b" (h));
182 return ((u64) h << 32) | l;
185 #elif defined(_mips) && __mips == 64
188 clib_cpu_time_now (void)
191 asm volatile ("rdhwr %0,$31\n":"=r" (result));
195 #elif defined(__riscv)
198 clib_cpu_time_now (void)
201 asm volatile("rdcycle %0\n" : "=r"(result));
205 #error "don't know how to read CPU time stamp"
209 void clib_time_verify_frequency (clib_time_t * c);
211 /* Define it as the type returned by clib_time_now */
212 typedef f64 clib_time_type_t;
213 typedef u64 clib_us_time_t;
215 #define CLIB_US_TIME_PERIOD (1e-6)
216 #define CLIB_US_TIME_FREQ (1.0/CLIB_US_TIME_PERIOD)
219 clib_time_now_internal (clib_time_t * c, u64 n)
221 u64 l = c->last_cpu_time;
222 u64 t = c->total_cpu_time;
225 c->total_cpu_time = t;
226 c->last_cpu_time = n;
227 rv = t * c->seconds_per_clock;
230 c->last_verify_cpu_time) >> c->log2_clocks_per_frequency_verify))
231 clib_time_verify_frequency (c);
235 /* Maximum f64 value as max clib_time */
236 #define CLIB_TIME_MAX (1.7976931348623157e+308)
239 clib_time_now (clib_time_t * c)
241 return clib_time_now_internal (c, clib_cpu_time_now ());
245 clib_cpu_time_wait (u64 dt)
247 u64 t_end = clib_cpu_time_now () + dt;
248 while (clib_cpu_time_now () < t_end)
252 void clib_time_init (clib_time_t * c);
257 #include <sys/time.h>
258 #include <sys/resource.h>
260 #include <sys/syscall.h>
262 /* Use 64bit floating point to represent time offset from epoch. */
268 clock_gettime (CLOCK_REALTIME, &ts);
270 /* clock_gettime without indirect syscall uses GLIBC wrappers which
271 we don't want. Just the bare metal, please. */
272 syscall (SYS_clock_gettime, CLOCK_REALTIME, &ts);
274 return ts.tv_sec + 1e-9 * ts.tv_nsec;
277 /* As above but integer number of nano-seconds. */
279 unix_time_now_nsec (void)
283 clock_gettime (CLOCK_REALTIME, &ts);
285 syscall (SYS_clock_gettime, CLOCK_REALTIME, &ts);
287 return 1e9 * ts.tv_sec + ts.tv_nsec;
291 unix_time_now_nsec_fraction (u32 * sec, u32 * nsec)
295 clock_gettime (CLOCK_REALTIME, &ts);
297 syscall (SYS_clock_gettime, CLOCK_REALTIME, &ts);
304 unix_usage_now (void)
307 getrusage (RUSAGE_SELF, &u);
308 return u.ru_utime.tv_sec + 1e-6 * u.ru_utime.tv_usec
309 + u.ru_stime.tv_sec + 1e-6 * u.ru_stime.tv_usec;
315 struct timespec ts, tsrem;
317 ts.tv_nsec = 1e9 * (dt - (f64) ts.tv_sec);
319 while (nanosleep (&ts, &tsrem) < 0)
323 #else /* ! CLIB_UNIX */
332 unix_time_now_nsec (void)
338 unix_time_now_nsec_fraction (u32 * sec, u32 * nsec)
343 unix_usage_now (void)
355 #endif /* included_time_h */
358 * fd.io coding-style-patch-verification: ON
361 * eval: (c-set-style "gnu")