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>
44 /* Total run time in clock cycles
45 since clib_time_init call. */
48 /* Last recorded time stamp. */
51 /* CPU clock frequency. */
52 f64 clocks_per_second;
54 /* 1 / cpu clock frequency: conversion factor
55 from clock cycles into seconds. */
56 f64 seconds_per_clock;
58 /* Time stamp of call to clib_time_init call. */
61 u64 last_verify_cpu_time;
63 /* Same but for reference time (if present). */
64 f64 last_verify_reference_time;
66 u32 log2_clocks_per_second, log2_clocks_per_frequency_verify;
69 /* Return CPU time stamp as 64bit number. */
70 #if defined(__x86_64__) || defined(i386)
71 always_inline u64 clib_cpu_time_now (void)
75 : "=a" (a), "=d" (d));
76 return (u64) a + ((u64) d << (u64) 32);
79 #elif defined (__powerpc64__)
81 always_inline u64 clib_cpu_time_now (void)
84 asm volatile ("mftb %0" : "=r" (t));
88 #elif defined (__SPU__)
90 always_inline u64 clib_cpu_time_now (void)
93 return spu_rdch (0x8);
95 return 0 /* __builtin_si_rdch (0x8) FIXME */;
99 #elif defined (__powerpc__)
101 always_inline u64 clib_cpu_time_now (void)
109 "cmpw %[hi1],%[hi2]\n"
111 : [hi1] "=r" (hi1), [hi2] "=r" (hi2), [lo] "=r" (lo));
112 return (u64) lo + ((u64) hi2 << (u64) 32);
115 #elif defined (__arm__)
117 always_inline u64 clib_cpu_time_now (void)
120 asm volatile ("mrc p15, 0, %[lo], c15, c12, 1"
125 #elif defined (__xtensa__)
128 always_inline u64 clib_cpu_time_now (void)
131 #elif defined (__TMS320C6X__)
133 always_inline u64 clib_cpu_time_now (void)
137 asm volatile (" dint\n"
141 : "=b" (l), "=b" (h));
143 return ((u64)h << 32) | l;
148 #error "don't know how to read CPU time stamp"
152 void clib_time_verify_frequency (clib_time_t * c);
154 always_inline f64 clib_time_now_internal (clib_time_t * c, u64 n)
156 u64 l = c->last_cpu_time;
157 u64 t = c->total_cpu_time;
159 c->total_cpu_time = t;
160 c->last_cpu_time = n;
161 if (PREDICT_FALSE ((c->last_cpu_time - c->last_verify_cpu_time) >> c->log2_clocks_per_frequency_verify))
162 clib_time_verify_frequency (c);
163 return t * c->seconds_per_clock;
167 clib_time_now (clib_time_t * c)
169 return clib_time_now_internal (c, clib_cpu_time_now());
172 always_inline void clib_cpu_time_wait (u64 dt)
174 u64 t_end = clib_cpu_time_now () + dt;
175 while (clib_cpu_time_now () < t_end)
179 void clib_time_init (clib_time_t * c);
184 #include <sys/time.h>
185 #include <sys/resource.h>
187 #include <sys/syscall.h>
189 /* Use 64bit floating point to represent time offset from epoch. */
190 always_inline f64 unix_time_now (void)
192 /* clock_gettime without indirect syscall uses GLIBC wrappers which
193 we don't want. Just the bare metal, please. */
195 syscall (SYS_clock_gettime, CLOCK_REALTIME, &ts);
196 return ts.tv_sec + 1e-9*ts.tv_nsec;
199 /* As above but integer number of nano-seconds. */
200 always_inline u64 unix_time_now_nsec (void)
203 syscall (SYS_clock_gettime, CLOCK_REALTIME, &ts);
204 return 1e9*ts.tv_sec + ts.tv_nsec;
207 always_inline f64 unix_usage_now (void)
210 getrusage (RUSAGE_SELF, &u);
211 return u.ru_utime.tv_sec + 1e-6*u.ru_utime.tv_usec
212 + u.ru_stime.tv_sec + 1e-6*u.ru_stime.tv_usec;
215 always_inline void unix_sleep (f64 dt)
219 t.tv_nsec = 1e9 * dt;
223 #else /* ! CLIB_UNIX */
225 always_inline f64 unix_time_now (void)
228 always_inline u64 unix_time_now_nsec (void)
231 always_inline f64 unix_usage_now (void)
234 always_inline void unix_sleep (f64 dt)
239 #endif /* included_time_h */