FD.io VPP  v19.01.3-6-g70449b9b9
Vector Packet Processing
time.h
Go to the documentation of this file.
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 
43 typedef struct
44 {
45  /* Total run time in clock cycles
46  since clib_time_init call. */
48 
49  /* Last recorded time stamp. */
51 
52  /* CPU clock frequency. */
54 
55  /* 1 / cpu clock frequency: conversion factor
56  from clock cycles into seconds. */
58 
60 
61  /* Time stamp of call to clib_time_init call. */
63 
65 
66  /* Same but for reference time (if present). */
68 
69  u32 log2_clocks_per_second, log2_clocks_per_frequency_verify;
70 } clib_time_t;
71 
72 /* Return CPU time stamp as 64bit number. */
73 #if defined(__x86_64__) || defined(i386)
76 {
77  u32 a, d;
78  asm volatile ("rdtsc":"=a" (a), "=d" (d));
79  return (u64) a + ((u64) d << (u64) 32);
80 }
81 
82 #elif defined (__powerpc64__)
83 
85 clib_cpu_time_now (void)
86 {
87  u64 t;
88  asm volatile ("mftb %0":"=r" (t));
89  return t;
90 }
91 
92 #elif defined (__SPU__)
93 
95 clib_cpu_time_now (void)
96 {
97 #ifdef _XLC
98  return spu_rdch (0x8);
99 #else
100  return 0 /* __builtin_si_rdch (0x8) FIXME */ ;
101 #endif
102 }
103 
104 #elif defined (__powerpc__)
105 
107 clib_cpu_time_now (void)
108 {
109  u32 hi1, hi2, lo;
110  asm volatile ("1:\n"
111  "mftbu %[hi1]\n"
112  "mftb %[lo]\n"
113  "mftbu %[hi2]\n"
114  "cmpw %[hi1],%[hi2]\n"
115  "bne 1b\n":[hi1] "=r" (hi1),[hi2] "=r" (hi2),[lo] "=r" (lo));
116  return (u64) lo + ((u64) hi2 << (u64) 32);
117 }
118 
119 #elif defined (__aarch64__)
121 clib_cpu_time_now (void)
122 {
123  u64 vct;
124  /* User access to cntvct_el0 is enabled in Linux kernel since 3.12. */
125  asm volatile ("mrs %0, cntvct_el0":"=r" (vct));
126  return vct;
127 }
128 
129 #elif defined (__arm__)
130 #if defined(__ARM_ARCH_8A__)
132 clib_cpu_time_now (void) /* We may run arm64 in aarch32 mode, to leverage 64bit counter */
133 {
134  u64 tsc;
135  asm volatile ("mrrc p15, 0, %Q0, %R0, c9":"=r" (tsc));
136  return tsc;
137 }
138 #elif defined(__ARM_ARCH_7A__)
140 clib_cpu_time_now (void)
141 {
142  u32 tsc;
143  asm volatile ("mrc p15, 0, %0, c9, c13, 0":"=r" (tsc));
144  return (u64) tsc;
145 }
146 #else
148 clib_cpu_time_now (void)
149 {
150  u32 lo;
151  asm volatile ("mrc p15, 0, %[lo], c15, c12, 1":[lo] "=r" (lo));
152  return (u64) lo;
153 }
154 #endif
155 
156 #elif defined (__xtensa__)
157 
158 /* Stub for now. */
160 clib_cpu_time_now (void)
161 {
162  return 0;
163 }
164 
165 #elif defined (__TMS320C6X__)
166 
168 clib_cpu_time_now (void)
169 {
170  u32 l, h;
171 
172  asm volatile (" dint\n"
173  " mvc .s2 TSCL,%0\n"
174  " mvc .s2 TSCH,%1\n" " rint\n":"=b" (l), "=b" (h));
175 
176  return ((u64) h << 32) | l;
177 }
178 
179 #elif defined(_mips) && __mips == 64
180 
182 clib_cpu_time_now (void)
183 {
184  u64 result;
185  asm volatile ("rdhwr %0,$31\n":"=r" (result));
186  return result;
187 }
188 
189 #else
190 #error "don't know how to read CPU time stamp"
191 
192 #endif
193 
195 
198 {
199  u64 l = c->last_cpu_time;
200  u64 t = c->total_cpu_time;
201  t += n - l;
202  c->total_cpu_time = t;
203  c->last_cpu_time = n;
204  if (PREDICT_FALSE
205  ((c->last_cpu_time -
208  return t * c->seconds_per_clock;
209 }
210 
211 /* Maximum f64 value as max clib_time */
212 #define CLIB_TIME_MAX (1.7976931348623157e+308)
213 
216 {
218 }
219 
220 always_inline void
222 {
223  u64 t_end = clib_cpu_time_now () + dt;
224  while (clib_cpu_time_now () < t_end)
225  ;
226 }
227 
228 void clib_time_init (clib_time_t * c);
229 
230 #ifdef CLIB_UNIX
231 
232 #include <time.h>
233 #include <sys/time.h>
234 #include <sys/resource.h>
235 #include <unistd.h>
236 #include <sys/syscall.h>
237 
238 /* Use 64bit floating point to represent time offset from epoch. */
241 {
242  struct timespec ts;
243 #ifdef __MACH__
244  clock_gettime (CLOCK_REALTIME, &ts);
245 #else
246  /* clock_gettime without indirect syscall uses GLIBC wrappers which
247  we don't want. Just the bare metal, please. */
248  syscall (SYS_clock_gettime, CLOCK_REALTIME, &ts);
249 #endif
250  return ts.tv_sec + 1e-9 * ts.tv_nsec;
251 }
252 
253 /* As above but integer number of nano-seconds. */
256 {
257  struct timespec ts;
258 #ifdef __MACH__
259  clock_gettime (CLOCK_REALTIME, &ts);
260 #else
261  syscall (SYS_clock_gettime, CLOCK_REALTIME, &ts);
262 #endif
263  return 1e9 * ts.tv_sec + ts.tv_nsec;
264 }
265 
266 always_inline void
268 {
269  struct timespec ts;
270 #ifdef __MACH__
271  clock_gettime (CLOCK_REALTIME, &ts);
272 #else
273  syscall (SYS_clock_gettime, CLOCK_REALTIME, &ts);
274 #endif
275  *sec = ts.tv_sec;
276  *nsec = ts.tv_nsec;
277 }
278 
281 {
282  struct rusage u;
283  getrusage (RUSAGE_SELF, &u);
284  return u.ru_utime.tv_sec + 1e-6 * u.ru_utime.tv_usec
285  + u.ru_stime.tv_sec + 1e-6 * u.ru_stime.tv_usec;
286 }
287 
288 always_inline void
290 {
291  struct timespec ts, tsrem;
292  ts.tv_sec = dt;
293  ts.tv_nsec = 1e9 * (dt - (f64) ts.tv_sec);
294 
295  while (nanosleep (&ts, &tsrem) < 0)
296  ts = tsrem;
297 }
298 
299 #else /* ! CLIB_UNIX */
300 
302 unix_time_now (void)
303 {
304  return 0;
305 }
306 
308 unix_time_now_nsec (void)
309 {
310  return 0;
311 }
312 
313 always_inline void
314 unix_time_now_nsec_fraction (u32 * sec, u32 * nsec)
315 {
316 }
317 
319 unix_usage_now (void)
320 {
321  return 0;
322 }
323 
324 always_inline void
325 unix_sleep (f64 dt)
326 {
327 }
328 
329 #endif
330 
331 #endif /* included_time_h */
332 
333 /*
334  * fd.io coding-style-patch-verification: ON
335  *
336  * Local Variables:
337  * eval: (c-set-style "gnu")
338  * End:
339  */
a
Definition: bitmap.h:538
static void clib_cpu_time_wait(u64 dt)
Definition: time.h:221
unsigned long u64
Definition: types.h:89
void clib_time_verify_frequency(clib_time_t *c)
Definition: time.c:194
f64 clocks_per_second
Definition: time.h:53
static f64 clib_time_now(clib_time_t *c)
Definition: time.h:215
static u64 clib_cpu_time_now(void)
Definition: time.h:75
u32 log2_clocks_per_second
Definition: time.h:69
u64 last_verify_cpu_time
Definition: time.h:64
double f64
Definition: types.h:142
static void unix_sleep(f64 dt)
Definition: time.h:289
#define always_inline
Definition: clib.h:98
static f64 unix_time_now(void)
Definition: time.h:240
unsigned int u32
Definition: types.h:88
static void unix_time_now_nsec_fraction(u32 *sec, u32 *nsec)
Definition: time.h:267
lo
u32 log2_clocks_per_frequency_verify
Definition: time.h:69
#define PREDICT_FALSE(x)
Definition: clib.h:111
f64 seconds_per_clock
Definition: time.h:57
u64 total_cpu_time
Definition: time.h:47
static u64 unix_time_now_nsec(void)
Definition: time.h:255
static f64 unix_usage_now(void)
Definition: time.h:280
u64 last_cpu_time
Definition: time.h:50
f64 last_verify_reference_time
Definition: time.h:67
static f64 clib_time_now_internal(clib_time_t *c, u64 n)
Definition: time.h:197
u64 init_cpu_time
Definition: time.h:62
#define SYS_clock_gettime
void clib_time_init(clib_time_t *c)
Definition: time.c:178
f64 round_to_units
Definition: time.h:59