RTEMS
_kernel_time.h
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 #include <machine/_timecounter.h>
32 
33 /* Operations on timespecs */
34 #ifndef timespecclear
35 #define timespecclear(tvp) ((tvp)->tv_sec = (tvp)->tv_nsec = 0)
36 #endif
37 #ifndef timespecisset
38 #define timespecisset(tvp) ((tvp)->tv_sec || (tvp)->tv_nsec)
39 #endif
40 #ifndef timespeccmp
41 #define timespeccmp(tvp, uvp, cmp) \
42  (((tvp)->tv_sec == (uvp)->tv_sec) ? \
43  ((tvp)->tv_nsec cmp (uvp)->tv_nsec) : \
44  ((tvp)->tv_sec cmp (uvp)->tv_sec))
45 #endif
46 
47 #ifndef timespecadd
48 #define timespecadd(tsp, usp, vsp) \
49  do { \
50  (vsp)->tv_sec = (tsp)->tv_sec + (usp)->tv_sec; \
51  (vsp)->tv_nsec = (tsp)->tv_nsec + (usp)->tv_nsec; \
52  if ((vsp)->tv_nsec >= 1000000000L) { \
53  (vsp)->tv_sec++; \
54  (vsp)->tv_nsec -= 1000000000L; \
55  } \
56  } while (0)
57 #endif
58 #ifndef timespecsub
59 #define timespecsub(tsp, usp, vsp) \
60  do { \
61  (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \
62  (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \
63  if ((vsp)->tv_nsec < 0) { \
64  (vsp)->tv_sec--; \
65  (vsp)->tv_nsec += 1000000000L; \
66  } \
67  } while (0)
68 #endif
69 
70 /*
71  * Simple macros to convert ticks to milliseconds
72  * or microseconds and vice-versa. The answer
73  * will always be at least 1. Note the return
74  * value is a uint32_t however we step up the
75  * operations to 64 bit to avoid any overflow/underflow
76  * problems.
77  */
78 #define TICKS_2_MSEC(t) max(1, (uint32_t)(hz == 1000) ? \
79  (t) : (((uint64_t)(t) * (uint64_t)1000)/(uint64_t)hz))
80 #define TICKS_2_USEC(t) max(1, (uint32_t)(hz == 1000) ? \
81  ((t) * 1000) : (((uint64_t)(t) * (uint64_t)1000000)/(uint64_t)hz))
82 #define MSEC_2_TICKS(m) max(1, (uint32_t)((hz == 1000) ? \
83  (m) : ((uint64_t)(m) * (uint64_t)hz)/(uint64_t)1000))
84 #define USEC_2_TICKS(u) max(1, (uint32_t)((hz == 1000) ? \
85  ((u) / 1000) : ((uint64_t)(u) * (uint64_t)hz)/(uint64_t)1000000))
86 
87 /* Operations on timevals. */
88 
89 #define timevalclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0)
90 #define timevalisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
91 #define timevalcmp(tvp, uvp, cmp) \
92  (((tvp)->tv_sec == (uvp)->tv_sec) ? \
93  ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
94  ((tvp)->tv_sec cmp (uvp)->tv_sec))
95 
96 /* timevaladd and timevalsub are not inlined */
97 
98 /*
99  * Kernel to clock driver interface.
100  */
101 void inittodr(time_t base);
102 void resettodr(void);
103 
104 #define time_second _Timecounter_Time_second
105 #define time_uptime _Timecounter_Time_uptime
106 extern struct timeval boottime;
107 extern struct bintime tc_tick_bt;
108 extern sbintime_t tc_tick_sbt;
109 extern struct bintime tick_bt;
110 extern sbintime_t tick_sbt;
111 extern int tc_precexp;
112 extern int tc_timepercentage;
113 extern struct bintime bt_timethreshold;
114 extern struct bintime bt_tickthreshold;
115 extern sbintime_t sbt_timethreshold;
116 extern sbintime_t sbt_tickthreshold;
117 
118 /*
119  * Functions for looking at our clock: [get]{bin,nano,micro}[up]time()
120  *
121  * Functions without the "get" prefix returns the best timestamp
122  * we can produce in the given format.
123  *
124  * "bin" == struct bintime == seconds + 64 bit fraction of seconds.
125  * "nano" == struct timespec == seconds + nanoseconds.
126  * "micro" == struct timeval == seconds + microseconds.
127  *
128  * Functions containing "up" returns time relative to boot and
129  * should be used for calculating time intervals.
130  *
131  * Functions without "up" returns UTC time.
132  *
133  * Functions with the "get" prefix returns a less precise result
134  * much faster than the functions without "get" prefix and should
135  * be used where a precision of 1/hz seconds is acceptable or where
136  * performance is priority. (NB: "precision", _not_ "resolution" !)
137  */
138 
139 #define binuptime(_bt) _Timecounter_Binuptime(_bt)
140 #define nanouptime(_tsp) _Timecounter_Nanouptime(_tsp)
141 #define microuptime(_tvp) _Timecounter_Microuptime(_tvp)
142 
143 static __inline sbintime_t
144 sbinuptime(void)
145 {
146  struct bintime _bt;
147 
148  binuptime(&_bt);
149  return (bttosbt(_bt));
150 }
151 
152 #define bintime(_bt) _Timecounter_Bintime(_bt)
153 #define nanotime(_tsp) _Timecounter_Nanotime(_tsp)
154 #define microtime(_tvp) _Timecounter_Microtime(_tvp)
155 
156 #define getbinuptime(_bt) _Timecounter_Getbinuptime(_bt)
157 #define getnanouptime(_tsp) _Timecounter_Getnanouptime(_tsp)
158 #define getmicrouptime(_tvp) _Timecounter_Getmicrouptime(_tvp)
159 
160 static __inline sbintime_t
161 getsbinuptime(void)
162 {
163  struct bintime _bt;
164 
165  getbinuptime(&_bt);
166  return (bttosbt(_bt));
167 }
168 
169 #define getbintime(_bt) _Timecounter_Getbintime(_bt)
170 #define getnanotime(_tsp) _Timecounter_Getnanotime(_tsp)
171 #define getmicrotime(_tvp) _Timecounter_Getmicrotime(_tvp)
172 
173 #define getboottime(_tvp) _Timecounter_Getboottime(_tvp)
174 #define getboottimebin(_bt) _Timecounter_Getboottimebin(_bt)
175 
176 /* Other functions */
177 int itimerdecr(struct itimerval *itp, int usec);
178 int itimerfix(struct timeval *tv);
179 int ppsratecheck(struct timeval *, int *, int);
180 int ratecheck(struct timeval *, const struct timeval *);
181 void timevaladd(struct timeval *t1, const struct timeval *t2);
182 void timevalsub(struct timeval *t1, const struct timeval *t2);
183 int tvtohz(struct timeval *tv);
184 
185 #define TC_DEFAULTPERC 5
186 
187 #define BT2FREQ(bt) \
188  (((uint64_t)0x8000000000000000 + ((bt)->frac >> 2)) / \
189  ((bt)->frac >> 1))
190 
191 #define SBT2FREQ(sbt) ((SBT_1S + ((sbt) >> 1)) / (sbt))
192 
193 #define FREQ2BT(freq, bt) \
194 { \
195  (bt)->sec = 0; \
196  (bt)->frac = ((uint64_t)0x8000000000000000 / (freq)) << 1; \
197 }
198 
199 #define TIMESEL(sbt, sbt2) \
200  (((sbt2) >= sbt_timethreshold) ? \
201  ((*(sbt) = getsbinuptime()), 1) : ((*(sbt) = sbinuptime()), 0))