1 | /*- |
---|
2 | * Copyright (c) 2016 embedded brains GmbH |
---|
3 | * All rights reserved. |
---|
4 | * |
---|
5 | * Redistribution and use in source and binary forms, with or without |
---|
6 | * modification, are permitted provided that the following conditions |
---|
7 | * are met: |
---|
8 | * 1. Redistributions of source code must retain the above copyright |
---|
9 | * notice, this list of conditions and the following disclaimer. |
---|
10 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
11 | * notice, this list of conditions and the following disclaimer in the |
---|
12 | * documentation and/or other materials provided with the distribution. |
---|
13 | * |
---|
14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
---|
15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
---|
18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
24 | * SUCH DAMAGE. |
---|
25 | */ |
---|
26 | |
---|
27 | #if !defined(_SYS_TIME_H_) || !defined(_KERNEL) |
---|
28 | #error "must be included via <sys/time.h> in kernel space" |
---|
29 | #endif |
---|
30 | |
---|
31 | /* Operations on timespecs */ |
---|
32 | #define timespecclear(tvp) ((tvp)->tv_sec = (tvp)->tv_nsec = 0) |
---|
33 | #define timespecisset(tvp) ((tvp)->tv_sec || (tvp)->tv_nsec) |
---|
34 | #define timespeccmp(tvp, uvp, cmp) \ |
---|
35 | (((tvp)->tv_sec == (uvp)->tv_sec) ? \ |
---|
36 | ((tvp)->tv_nsec cmp (uvp)->tv_nsec) : \ |
---|
37 | ((tvp)->tv_sec cmp (uvp)->tv_sec)) |
---|
38 | #define timespecadd(vvp, uvp) \ |
---|
39 | do { \ |
---|
40 | (vvp)->tv_sec += (uvp)->tv_sec; \ |
---|
41 | (vvp)->tv_nsec += (uvp)->tv_nsec; \ |
---|
42 | if ((vvp)->tv_nsec >= 1000000000) { \ |
---|
43 | (vvp)->tv_sec++; \ |
---|
44 | (vvp)->tv_nsec -= 1000000000; \ |
---|
45 | } \ |
---|
46 | } while (0) |
---|
47 | #define timespecsub(vvp, uvp) \ |
---|
48 | do { \ |
---|
49 | (vvp)->tv_sec -= (uvp)->tv_sec; \ |
---|
50 | (vvp)->tv_nsec -= (uvp)->tv_nsec; \ |
---|
51 | if ((vvp)->tv_nsec < 0) { \ |
---|
52 | (vvp)->tv_sec--; \ |
---|
53 | (vvp)->tv_nsec += 1000000000; \ |
---|
54 | } \ |
---|
55 | } while (0) |
---|
56 | |
---|
57 | /* Operations on timevals. */ |
---|
58 | |
---|
59 | #define timevalclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0) |
---|
60 | #define timevalisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec) |
---|
61 | #define timevalcmp(tvp, uvp, cmp) \ |
---|
62 | (((tvp)->tv_sec == (uvp)->tv_sec) ? \ |
---|
63 | ((tvp)->tv_usec cmp (uvp)->tv_usec) : \ |
---|
64 | ((tvp)->tv_sec cmp (uvp)->tv_sec)) |
---|
65 | |
---|
66 | /* timevaladd and timevalsub are not inlined */ |
---|
67 | |
---|
68 | /* |
---|
69 | * Kernel to clock driver interface. |
---|
70 | */ |
---|
71 | void inittodr(time_t base); |
---|
72 | void resettodr(void); |
---|
73 | |
---|
74 | #define time_second _Timecounter_Time_second |
---|
75 | #define time_uptime _Timecounter_Time_uptime |
---|
76 | #define boottimebin _Timecounter_Boottimebin |
---|
77 | extern struct timeval boottime; |
---|
78 | extern struct bintime tc_tick_bt; |
---|
79 | extern sbintime_t tc_tick_sbt; |
---|
80 | extern struct bintime tick_bt; |
---|
81 | extern sbintime_t tick_sbt; |
---|
82 | extern int tc_precexp; |
---|
83 | extern int tc_timepercentage; |
---|
84 | extern struct bintime bt_timethreshold; |
---|
85 | extern struct bintime bt_tickthreshold; |
---|
86 | extern sbintime_t sbt_timethreshold; |
---|
87 | extern sbintime_t sbt_tickthreshold; |
---|
88 | |
---|
89 | /* |
---|
90 | * Functions for looking at our clock: [get]{bin,nano,micro}[up]time() |
---|
91 | * |
---|
92 | * Functions without the "get" prefix returns the best timestamp |
---|
93 | * we can produce in the given format. |
---|
94 | * |
---|
95 | * "bin" == struct bintime == seconds + 64 bit fraction of seconds. |
---|
96 | * "nano" == struct timespec == seconds + nanoseconds. |
---|
97 | * "micro" == struct timeval == seconds + microseconds. |
---|
98 | * |
---|
99 | * Functions containing "up" returns time relative to boot and |
---|
100 | * should be used for calculating time intervals. |
---|
101 | * |
---|
102 | * Functions without "up" returns UTC time. |
---|
103 | * |
---|
104 | * Functions with the "get" prefix returns a less precise result |
---|
105 | * much faster than the functions without "get" prefix and should |
---|
106 | * be used where a precision of 1/hz seconds is acceptable or where |
---|
107 | * performance is priority. (NB: "precision", _not_ "resolution" !) |
---|
108 | */ |
---|
109 | |
---|
110 | #define binuptime(_bt) _Timecounter_Binuptime(_bt) |
---|
111 | #define nanouptime(_tsp) _Timecounter_Nanouptime(_tsp) |
---|
112 | #define microuptime(_tvp) _Timecounter_Microuptime(_tvp) |
---|
113 | |
---|
114 | static __inline sbintime_t |
---|
115 | sbinuptime(void) |
---|
116 | { |
---|
117 | struct bintime _bt; |
---|
118 | |
---|
119 | binuptime(&_bt); |
---|
120 | return (bttosbt(_bt)); |
---|
121 | } |
---|
122 | |
---|
123 | #define bintime(_bt) _Timecounter_Bintime(_bt) |
---|
124 | #define nanotime(_tsp) _Timecounter_Nanotime(_tsp) |
---|
125 | #define microtime(_tvp) _Timecounter_Microtime(_tvp) |
---|
126 | |
---|
127 | #define getbinuptime(_bt) _Timecounter_Getbinuptime(_bt) |
---|
128 | #define getnanouptime(_tsp) _Timecounter_Getnanouptime(_tsp) |
---|
129 | #define getmicrouptime(_tvp) _Timecounter_Getmicrouptime(_tvp) |
---|
130 | |
---|
131 | static __inline sbintime_t |
---|
132 | getsbinuptime(void) |
---|
133 | { |
---|
134 | struct bintime _bt; |
---|
135 | |
---|
136 | getbinuptime(&_bt); |
---|
137 | return (bttosbt(_bt)); |
---|
138 | } |
---|
139 | |
---|
140 | #define getbintime(_bt) _Timecounter_Getbintime(_bt) |
---|
141 | #define getnanotime(_tsp) _Timecounter_Getnanotime(_tsp) |
---|
142 | #define getmicrotime(_tvp) _Timecounter_Getmicrotime(_tvp) |
---|
143 | |
---|
144 | /* Other functions */ |
---|
145 | int itimerdecr(struct itimerval *itp, int usec); |
---|
146 | int itimerfix(struct timeval *tv); |
---|
147 | int ppsratecheck(struct timeval *, int *, int); |
---|
148 | int ratecheck(struct timeval *, const struct timeval *); |
---|
149 | void timevaladd(struct timeval *t1, const struct timeval *t2); |
---|
150 | void timevalsub(struct timeval *t1, const struct timeval *t2); |
---|
151 | int tvtohz(struct timeval *tv); |
---|
152 | |
---|
153 | #define TC_DEFAULTPERC 5 |
---|
154 | |
---|
155 | #define BT2FREQ(bt) \ |
---|
156 | (((uint64_t)0x8000000000000000 + ((bt)->frac >> 2)) / \ |
---|
157 | ((bt)->frac >> 1)) |
---|
158 | |
---|
159 | #define SBT2FREQ(sbt) ((SBT_1S + ((sbt) >> 1)) / (sbt)) |
---|
160 | |
---|
161 | #define FREQ2BT(freq, bt) \ |
---|
162 | { \ |
---|
163 | (bt)->sec = 0; \ |
---|
164 | (bt)->frac = ((uint64_t)0x8000000000000000 / (freq)) << 1; \ |
---|
165 | } |
---|
166 | |
---|
167 | #define TIMESEL(sbt, sbt2) \ |
---|
168 | (((sbt2) >= sbt_timethreshold) ? \ |
---|
169 | ((*(sbt) = getsbinuptime()), 1) : ((*(sbt) = sbinuptime()), 0)) |
---|