blob: 0327ce0fd05b5bdd46d27b4cfe30320c664b610d [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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
Dave Barachc3799992016-08-15 11:12:27 -040043typedef struct
44{
Ed Warnickecb9cada2015-12-08 15:45:58 -070045 /* Total run time in clock cycles
46 since clib_time_init call. */
47 u64 total_cpu_time;
48
49 /* Last recorded time stamp. */
50 u64 last_cpu_time;
51
52 /* CPU clock frequency. */
53 f64 clocks_per_second;
54
55 /* 1 / cpu clock frequency: conversion factor
56 from clock cycles into seconds. */
57 f64 seconds_per_clock;
58
Dave Barachba603ba2018-11-07 17:40:19 -050059 f64 round_to_units;
60
Ed Warnickecb9cada2015-12-08 15:45:58 -070061 /* Time stamp of call to clib_time_init call. */
62 u64 init_cpu_time;
63
64 u64 last_verify_cpu_time;
65
66 /* Same but for reference time (if present). */
67 f64 last_verify_reference_time;
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)
Dave Barachc3799992016-08-15 11:12:27 -040074always_inline u64
75clib_cpu_time_now (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -070076{
77 u32 a, d;
Dave Barachc3799992016-08-15 11:12:27 -040078 asm volatile ("rdtsc":"=a" (a), "=d" (d));
Ed Warnickecb9cada2015-12-08 15:45:58 -070079 return (u64) a + ((u64) d << (u64) 32);
80}
81
82#elif defined (__powerpc64__)
83
Dave Barachc3799992016-08-15 11:12:27 -040084always_inline u64
85clib_cpu_time_now (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -070086{
87 u64 t;
Dave Barachc3799992016-08-15 11:12:27 -040088 asm volatile ("mftb %0":"=r" (t));
Ed Warnickecb9cada2015-12-08 15:45:58 -070089 return t;
90}
91
92#elif defined (__SPU__)
93
Dave Barachc3799992016-08-15 11:12:27 -040094always_inline u64
95clib_cpu_time_now (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -070096{
97#ifdef _XLC
98 return spu_rdch (0x8);
99#else
Dave Barachc3799992016-08-15 11:12:27 -0400100 return 0 /* __builtin_si_rdch (0x8) FIXME */ ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700101#endif
102}
103
104#elif defined (__powerpc__)
105
Dave Barachc3799992016-08-15 11:12:27 -0400106always_inline u64
107clib_cpu_time_now (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700108{
109 u32 hi1, hi2, lo;
Dave Barachc3799992016-08-15 11:12:27 -0400110 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));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700116 return (u64) lo + ((u64) hi2 << (u64) 32);
117}
118
Brian Brooksc0379ae2018-01-09 16:39:07 -0600119#elif defined (__aarch64__)
120always_inline u64
121clib_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
Ed Warnickecb9cada2015-12-08 15:45:58 -0700129#elif defined (__arm__)
Christophe Fontaine9341a1f2016-05-13 07:07:28 +0000130#if defined(__ARM_ARCH_8A__)
Dave Barachc3799992016-08-15 11:12:27 -0400131always_inline u64
132clib_cpu_time_now (void) /* We may run arm64 in aarch32 mode, to leverage 64bit counter */
Christophe Fontainefef15b42016-04-09 12:38:49 +0900133{
134 u64 tsc;
Dave Barachc3799992016-08-15 11:12:27 -0400135 asm volatile ("mrrc p15, 0, %Q0, %R0, c9":"=r" (tsc));
Christophe Fontainefef15b42016-04-09 12:38:49 +0900136 return tsc;
137}
Christophe Fontaine9341a1f2016-05-13 07:07:28 +0000138#elif defined(__ARM_ARCH_7A__)
Dave Barachc3799992016-08-15 11:12:27 -0400139always_inline u64
140clib_cpu_time_now (void)
Christophe Fontaine9341a1f2016-05-13 07:07:28 +0000141{
142 u32 tsc;
Dave Barachc3799992016-08-15 11:12:27 -0400143 asm volatile ("mrc p15, 0, %0, c9, c13, 0":"=r" (tsc));
144 return (u64) tsc;
Christophe Fontaine9341a1f2016-05-13 07:07:28 +0000145}
Christophe Fontainefef15b42016-04-09 12:38:49 +0900146#else
Dave Barachc3799992016-08-15 11:12:27 -0400147always_inline u64
148clib_cpu_time_now (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700149{
150 u32 lo;
Dave Barachc3799992016-08-15 11:12:27 -0400151 asm volatile ("mrc p15, 0, %[lo], c15, c12, 1":[lo] "=r" (lo));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700152 return (u64) lo;
153}
Christophe Fontainefef15b42016-04-09 12:38:49 +0900154#endif
Ed Warnickecb9cada2015-12-08 15:45:58 -0700155
156#elif defined (__xtensa__)
157
158/* Stub for now. */
Dave Barachc3799992016-08-15 11:12:27 -0400159always_inline u64
160clib_cpu_time_now (void)
161{
162 return 0;
163}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700164
165#elif defined (__TMS320C6X__)
166
Dave Barachc3799992016-08-15 11:12:27 -0400167always_inline u64
168clib_cpu_time_now (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700169{
170 u32 l, h;
171
172 asm volatile (" dint\n"
173 " mvc .s2 TSCL,%0\n"
Dave Barachc3799992016-08-15 11:12:27 -0400174 " mvc .s2 TSCH,%1\n" " rint\n":"=b" (l), "=b" (h));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700175
Dave Barachc3799992016-08-15 11:12:27 -0400176 return ((u64) h << 32) | l;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700177}
178
Carl Smith28d42712018-07-26 15:45:28 +1200179#elif defined(_mips) && __mips == 64
180
181always_inline u64
182clib_cpu_time_now (void)
183{
184 u64 result;
185 asm volatile ("rdhwr %0,$31\n":"=r" (result));
186 return result;
187}
188
Dave Barach61efa142016-01-22 08:23:09 -0500189#else
Ed Warnickecb9cada2015-12-08 15:45:58 -0700190#error "don't know how to read CPU time stamp"
191
192#endif
193
194void clib_time_verify_frequency (clib_time_t * c);
195
Dave Barachc3799992016-08-15 11:12:27 -0400196always_inline f64
197clib_time_now_internal (clib_time_t * c, u64 n)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700198{
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;
Dave Barachc3799992016-08-15 11:12:27 -0400204 if (PREDICT_FALSE
205 ((c->last_cpu_time -
206 c->last_verify_cpu_time) >> c->log2_clocks_per_frequency_verify))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700207 clib_time_verify_frequency (c);
208 return t * c->seconds_per_clock;
209}
210
John Lo7f358b32018-04-28 01:19:24 -0400211/* Maximum f64 value as max clib_time */
212#define CLIB_TIME_MAX (1.7976931348623157e+308)
213
Ed Warnickecb9cada2015-12-08 15:45:58 -0700214always_inline f64
215clib_time_now (clib_time_t * c)
216{
Dave Barachc3799992016-08-15 11:12:27 -0400217 return clib_time_now_internal (c, clib_cpu_time_now ());
Ed Warnickecb9cada2015-12-08 15:45:58 -0700218}
219
Dave Barachc3799992016-08-15 11:12:27 -0400220always_inline void
221clib_cpu_time_wait (u64 dt)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700222{
223 u64 t_end = clib_cpu_time_now () + dt;
224 while (clib_cpu_time_now () < t_end)
225 ;
226}
227
228void 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. */
Dave Barachc3799992016-08-15 11:12:27 -0400239always_inline f64
240unix_time_now (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700241{
Damjan Marion4dffd1c2018-09-03 12:30:36 +0200242 struct timespec ts;
243#ifdef __MACH__
244 clock_gettime (CLOCK_REALTIME, &ts);
245#else
Ed Warnickecb9cada2015-12-08 15:45:58 -0700246 /* clock_gettime without indirect syscall uses GLIBC wrappers which
247 we don't want. Just the bare metal, please. */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700248 syscall (SYS_clock_gettime, CLOCK_REALTIME, &ts);
Damjan Marion4dffd1c2018-09-03 12:30:36 +0200249#endif
Dave Barachc3799992016-08-15 11:12:27 -0400250 return ts.tv_sec + 1e-9 * ts.tv_nsec;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700251}
252
253/* As above but integer number of nano-seconds. */
Dave Barachc3799992016-08-15 11:12:27 -0400254always_inline u64
255unix_time_now_nsec (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700256{
257 struct timespec ts;
Damjan Marion4dffd1c2018-09-03 12:30:36 +0200258#ifdef __MACH__
259 clock_gettime (CLOCK_REALTIME, &ts);
260#else
Ed Warnickecb9cada2015-12-08 15:45:58 -0700261 syscall (SYS_clock_gettime, CLOCK_REALTIME, &ts);
Damjan Marion4dffd1c2018-09-03 12:30:36 +0200262#endif
Dave Barachc3799992016-08-15 11:12:27 -0400263 return 1e9 * ts.tv_sec + ts.tv_nsec;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700264}
265
Ole Troaned929252017-06-13 21:15:40 +0200266always_inline void
267unix_time_now_nsec_fraction (u32 * sec, u32 * nsec)
268{
269 struct timespec ts;
Damjan Marion4dffd1c2018-09-03 12:30:36 +0200270#ifdef __MACH__
271 clock_gettime (CLOCK_REALTIME, &ts);
272#else
Ole Troaned929252017-06-13 21:15:40 +0200273 syscall (SYS_clock_gettime, CLOCK_REALTIME, &ts);
Damjan Marion4dffd1c2018-09-03 12:30:36 +0200274#endif
Ole Troaned929252017-06-13 21:15:40 +0200275 *sec = ts.tv_sec;
276 *nsec = ts.tv_nsec;
277}
278
Dave Barachc3799992016-08-15 11:12:27 -0400279always_inline f64
280unix_usage_now (void)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700281{
282 struct rusage u;
283 getrusage (RUSAGE_SELF, &u);
Dave Barachc3799992016-08-15 11:12:27 -0400284 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;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700286}
287
Dave Barachc3799992016-08-15 11:12:27 -0400288always_inline void
289unix_sleep (f64 dt)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700290{
Dave Barachb9f2cf02017-10-17 13:13:42 -0400291 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;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700297}
298
Dave Barachc3799992016-08-15 11:12:27 -0400299#else /* ! CLIB_UNIX */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700300
Dave Barachc3799992016-08-15 11:12:27 -0400301always_inline f64
302unix_time_now (void)
303{
304 return 0;
305}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700306
Dave Barachc3799992016-08-15 11:12:27 -0400307always_inline u64
308unix_time_now_nsec (void)
309{
310 return 0;
311}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700312
Ole Troaned929252017-06-13 21:15:40 +0200313always_inline void
314unix_time_now_nsec_fraction (u32 * sec, u32 * nsec)
315{
316}
317
Dave Barachc3799992016-08-15 11:12:27 -0400318always_inline f64
319unix_usage_now (void)
320{
321 return 0;
322}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700323
Dave Barachc3799992016-08-15 11:12:27 -0400324always_inline void
325unix_sleep (f64 dt)
326{
327}
Ed Warnickecb9cada2015-12-08 15:45:58 -0700328
329#endif
330
331#endif /* included_time_h */
Dave Barachc3799992016-08-15 11:12:27 -0400332
333/*
334 * fd.io coding-style-patch-verification: ON
335 *
336 * Local Variables:
337 * eval: (c-set-style "gnu")
338 * End:
339 */