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