FD.io VPP  v16.06
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  /* Total run time in clock cycles
45  since clib_time_init call. */
47 
48  /* Last recorded time stamp. */
50 
51  /* CPU clock frequency. */
53 
54  /* 1 / cpu clock frequency: conversion factor
55  from clock cycles into seconds. */
57 
58  /* Time stamp of call to clib_time_init call. */
60 
62 
63  /* Same but for reference time (if present). */
65 
66  u32 log2_clocks_per_second, log2_clocks_per_frequency_verify;
67 } clib_time_t;
68 
69 /* Return CPU time stamp as 64bit number. */
70 #if defined(__x86_64__) || defined(i386)
72 {
73  u32 a, d;
74  asm volatile ("rdtsc"
75  : "=a" (a), "=d" (d));
76  return (u64) a + ((u64) d << (u64) 32);
77 }
78 
79 #elif defined (__powerpc64__)
80 
82 {
83  u64 t;
84  asm volatile ("mftb %0" : "=r" (t));
85  return t;
86 }
87 
88 #elif defined (__SPU__)
89 
91 {
92 #ifdef _XLC
93  return spu_rdch (0x8);
94 #else
95  return 0 /* __builtin_si_rdch (0x8) FIXME */;
96 #endif
97 }
98 
99 #elif defined (__powerpc__)
100 
102 {
103  u32 hi1, hi2, lo;
104  asm volatile (
105  "1:\n"
106  "mftbu %[hi1]\n"
107  "mftb %[lo]\n"
108  "mftbu %[hi2]\n"
109  "cmpw %[hi1],%[hi2]\n"
110  "bne 1b\n"
111  : [hi1] "=r" (hi1), [hi2] "=r" (hi2), [lo] "=r" (lo));
112  return (u64) lo + ((u64) hi2 << (u64) 32);
113 }
114 
115 #elif defined (__arm__)
116 #if defined(__ARM_ARCH_7A__)
118 {
119  u64 tsc;
120  asm volatile("mrrc p15, 0, %Q0, %R0, c9" : "=r" (tsc));
121  return tsc;
122 }
123 #else
125 {
126  u32 lo;
127  asm volatile ("mrc p15, 0, %[lo], c15, c12, 1"
128  : [lo] "=r" (lo));
129  return (u64) lo;
130 }
131 #endif
132 
133 #elif defined (__xtensa__)
134 
135 /* Stub for now. */
137 { return 0; }
138 
139 #elif defined (__TMS320C6X__)
140 
142 {
143  u32 l, h;
144 
145  asm volatile (" dint\n"
146  " mvc .s2 TSCL,%0\n"
147  " mvc .s2 TSCH,%1\n"
148  " rint\n"
149  : "=b" (l), "=b" (h));
150 
151  return ((u64)h << 32) | l;
152 }
153 
154 #elif defined (__aarch64__)
156 {
157  u64 tsc;
158 
159  /* Works on Cavium ThunderX. Other platforms: YMMV */
160  asm volatile("mrs %0, cntvct_el0" : "=r" (tsc));
161 
162  return tsc;
163 }
164 
165 #else
166 #error "don't know how to read CPU time stamp"
167 
168 #endif
169 
171 
173 {
174  u64 l = c->last_cpu_time;
175  u64 t = c->total_cpu_time;
176  t += n - l;
177  c->total_cpu_time = t;
178  c->last_cpu_time = n;
181  return t * c->seconds_per_clock;
182 }
183 
186 {
188 }
189 
191 {
192  u64 t_end = clib_cpu_time_now () + dt;
193  while (clib_cpu_time_now () < t_end)
194  ;
195 }
196 
197 void clib_time_init (clib_time_t * c);
198 
199 #ifdef CLIB_UNIX
200 
201 #include <time.h>
202 #include <sys/time.h>
203 #include <sys/resource.h>
204 #include <unistd.h>
205 #include <sys/syscall.h>
206 
207 /* Use 64bit floating point to represent time offset from epoch. */
209 {
210  /* clock_gettime without indirect syscall uses GLIBC wrappers which
211  we don't want. Just the bare metal, please. */
212  struct timespec ts;
213  syscall (SYS_clock_gettime, CLOCK_REALTIME, &ts);
214  return ts.tv_sec + 1e-9*ts.tv_nsec;
215 }
216 
217 /* As above but integer number of nano-seconds. */
219 {
220  struct timespec ts;
221  syscall (SYS_clock_gettime, CLOCK_REALTIME, &ts);
222  return 1e9*ts.tv_sec + ts.tv_nsec;
223 }
224 
226 {
227  struct rusage u;
228  getrusage (RUSAGE_SELF, &u);
229  return u.ru_utime.tv_sec + 1e-6*u.ru_utime.tv_usec
230  + u.ru_stime.tv_sec + 1e-6*u.ru_stime.tv_usec;
231 }
232 
234 {
235  struct timespec t;
236  t.tv_sec = dt;
237  t.tv_nsec = 1e9 * dt;
238  nanosleep (&t, 0);
239 }
240 
241 #else /* ! CLIB_UNIX */
242 
244 { return 0; }
245 
247 { return 0; }
248 
250 { return 0; }
251 
252 always_inline void unix_sleep (f64 dt)
253 { }
254 
255 #endif
256 
257 #endif /* included_time_h */
always_inline f64 unix_time_now(void)
Definition: time.h:208
a
Definition: bitmap.h:393
always_inline f64 unix_usage_now(void)
Definition: time.h:225
always_inline f64 clib_time_now_internal(clib_time_t *c, u64 n)
Definition: time.h:172
void clib_time_verify_frequency(clib_time_t *c)
Definition: time.c:175
f64 clocks_per_second
Definition: time.h:52
u32 log2_clocks_per_second
Definition: time.h:66
u64 last_verify_cpu_time
Definition: time.h:61
#define always_inline
Definition: clib.h:84
unsigned long u64
Definition: types.h:89
always_inline f64 clib_time_now(clib_time_t *c)
Definition: time.h:185
u32 log2_clocks_per_frequency_verify
Definition: time.h:66
always_inline void unix_sleep(f64 dt)
Definition: time.h:233
#define PREDICT_FALSE(x)
Definition: clib.h:97
f64 seconds_per_clock
Definition: time.h:56
u64 total_cpu_time
Definition: time.h:46
u64 last_cpu_time
Definition: time.h:49
unsigned int u32
Definition: types.h:88
f64 last_verify_reference_time
Definition: time.h:64
always_inline void clib_cpu_time_wait(u64 dt)
Definition: time.h:190
double f64
Definition: types.h:140
u64 init_cpu_time
Definition: time.h:59
void clib_time_init(clib_time_t *c)
Definition: time.c:160
always_inline u64 clib_cpu_time_now(void)
Definition: time.h:71
always_inline u64 unix_time_now_nsec(void)
Definition: time.h:218