1*199767f8SToomas Soome /*-
2*199767f8SToomas Soome * Copyright (c) 1982, 1986, 1993
3*199767f8SToomas Soome * The Regents of the University of California. All rights reserved.
4*199767f8SToomas Soome *
5*199767f8SToomas Soome * Redistribution and use in source and binary forms, with or without
6*199767f8SToomas Soome * modification, are permitted provided that the following conditions
7*199767f8SToomas Soome * are met:
8*199767f8SToomas Soome * 1. Redistributions of source code must retain the above copyright
9*199767f8SToomas Soome * notice, this list of conditions and the following disclaimer.
10*199767f8SToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
11*199767f8SToomas Soome * notice, this list of conditions and the following disclaimer in the
12*199767f8SToomas Soome * documentation and/or other materials provided with the distribution.
13*199767f8SToomas Soome * 4. Neither the name of the University nor the names of its contributors
14*199767f8SToomas Soome * may be used to endorse or promote products derived from this software
15*199767f8SToomas Soome * without specific prior written permission.
16*199767f8SToomas Soome *
17*199767f8SToomas Soome * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18*199767f8SToomas Soome * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*199767f8SToomas Soome * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*199767f8SToomas Soome * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21*199767f8SToomas Soome * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*199767f8SToomas Soome * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23*199767f8SToomas Soome * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24*199767f8SToomas Soome * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25*199767f8SToomas Soome * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26*199767f8SToomas Soome * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27*199767f8SToomas Soome * SUCH DAMAGE.
28*199767f8SToomas Soome *
29*199767f8SToomas Soome * @(#)time.h 8.5 (Berkeley) 5/4/95
30*199767f8SToomas Soome * $FreeBSD$
31*199767f8SToomas Soome */
32*199767f8SToomas Soome
33*199767f8SToomas Soome #ifndef _SYS_TIME_H_
34*199767f8SToomas Soome #define _SYS_TIME_H_
35*199767f8SToomas Soome
36*199767f8SToomas Soome #include <sys/_timeval.h>
37*199767f8SToomas Soome #include <sys/types.h>
38*199767f8SToomas Soome #include <sys/timespec.h>
39*199767f8SToomas Soome
40*199767f8SToomas Soome struct timezone {
41*199767f8SToomas Soome int tz_minuteswest; /* minutes west of Greenwich */
42*199767f8SToomas Soome int tz_dsttime; /* type of dst correction */
43*199767f8SToomas Soome };
44*199767f8SToomas Soome #define DST_NONE 0 /* not on dst */
45*199767f8SToomas Soome #define DST_USA 1 /* USA style dst */
46*199767f8SToomas Soome #define DST_AUST 2 /* Australian style dst */
47*199767f8SToomas Soome #define DST_WET 3 /* Western European dst */
48*199767f8SToomas Soome #define DST_MET 4 /* Middle European dst */
49*199767f8SToomas Soome #define DST_EET 5 /* Eastern European dst */
50*199767f8SToomas Soome #define DST_CAN 6 /* Canada */
51*199767f8SToomas Soome
52*199767f8SToomas Soome #if __BSD_VISIBLE
53*199767f8SToomas Soome struct bintime {
54*199767f8SToomas Soome time_t sec;
55*199767f8SToomas Soome uint64_t frac;
56*199767f8SToomas Soome };
57*199767f8SToomas Soome
58*199767f8SToomas Soome static __inline void
bintime_addx(struct bintime * _bt,uint64_t _x)59*199767f8SToomas Soome bintime_addx(struct bintime *_bt, uint64_t _x)
60*199767f8SToomas Soome {
61*199767f8SToomas Soome uint64_t _u;
62*199767f8SToomas Soome
63*199767f8SToomas Soome _u = _bt->frac;
64*199767f8SToomas Soome _bt->frac += _x;
65*199767f8SToomas Soome if (_u > _bt->frac)
66*199767f8SToomas Soome _bt->sec++;
67*199767f8SToomas Soome }
68*199767f8SToomas Soome
69*199767f8SToomas Soome static __inline void
bintime_add(struct bintime * _bt,const struct bintime * _bt2)70*199767f8SToomas Soome bintime_add(struct bintime *_bt, const struct bintime *_bt2)
71*199767f8SToomas Soome {
72*199767f8SToomas Soome uint64_t _u;
73*199767f8SToomas Soome
74*199767f8SToomas Soome _u = _bt->frac;
75*199767f8SToomas Soome _bt->frac += _bt2->frac;
76*199767f8SToomas Soome if (_u > _bt->frac)
77*199767f8SToomas Soome _bt->sec++;
78*199767f8SToomas Soome _bt->sec += _bt2->sec;
79*199767f8SToomas Soome }
80*199767f8SToomas Soome
81*199767f8SToomas Soome static __inline void
bintime_sub(struct bintime * _bt,const struct bintime * _bt2)82*199767f8SToomas Soome bintime_sub(struct bintime *_bt, const struct bintime *_bt2)
83*199767f8SToomas Soome {
84*199767f8SToomas Soome uint64_t _u;
85*199767f8SToomas Soome
86*199767f8SToomas Soome _u = _bt->frac;
87*199767f8SToomas Soome _bt->frac -= _bt2->frac;
88*199767f8SToomas Soome if (_u < _bt->frac)
89*199767f8SToomas Soome _bt->sec--;
90*199767f8SToomas Soome _bt->sec -= _bt2->sec;
91*199767f8SToomas Soome }
92*199767f8SToomas Soome
93*199767f8SToomas Soome static __inline void
bintime_mul(struct bintime * _bt,u_int _x)94*199767f8SToomas Soome bintime_mul(struct bintime *_bt, u_int _x)
95*199767f8SToomas Soome {
96*199767f8SToomas Soome uint64_t _p1, _p2;
97*199767f8SToomas Soome
98*199767f8SToomas Soome _p1 = (_bt->frac & 0xffffffffull) * _x;
99*199767f8SToomas Soome _p2 = (_bt->frac >> 32) * _x + (_p1 >> 32);
100*199767f8SToomas Soome _bt->sec *= _x;
101*199767f8SToomas Soome _bt->sec += (_p2 >> 32);
102*199767f8SToomas Soome _bt->frac = (_p2 << 32) | (_p1 & 0xffffffffull);
103*199767f8SToomas Soome }
104*199767f8SToomas Soome
105*199767f8SToomas Soome static __inline void
bintime_shift(struct bintime * _bt,int _exp)106*199767f8SToomas Soome bintime_shift(struct bintime *_bt, int _exp)
107*199767f8SToomas Soome {
108*199767f8SToomas Soome
109*199767f8SToomas Soome if (_exp > 0) {
110*199767f8SToomas Soome _bt->sec <<= _exp;
111*199767f8SToomas Soome _bt->sec |= _bt->frac >> (64 - _exp);
112*199767f8SToomas Soome _bt->frac <<= _exp;
113*199767f8SToomas Soome } else if (_exp < 0) {
114*199767f8SToomas Soome _bt->frac >>= -_exp;
115*199767f8SToomas Soome _bt->frac |= (uint64_t)_bt->sec << (64 + _exp);
116*199767f8SToomas Soome _bt->sec >>= -_exp;
117*199767f8SToomas Soome }
118*199767f8SToomas Soome }
119*199767f8SToomas Soome
120*199767f8SToomas Soome #define bintime_clear(a) ((a)->sec = (a)->frac = 0)
121*199767f8SToomas Soome #define bintime_isset(a) ((a)->sec || (a)->frac)
122*199767f8SToomas Soome #define bintime_cmp(a, b, cmp) \
123*199767f8SToomas Soome (((a)->sec == (b)->sec) ? \
124*199767f8SToomas Soome ((a)->frac cmp (b)->frac) : \
125*199767f8SToomas Soome ((a)->sec cmp (b)->sec))
126*199767f8SToomas Soome
127*199767f8SToomas Soome #define SBT_1S ((sbintime_t)1 << 32)
128*199767f8SToomas Soome #define SBT_1M (SBT_1S * 60)
129*199767f8SToomas Soome #define SBT_1MS (SBT_1S / 1000)
130*199767f8SToomas Soome #define SBT_1US (SBT_1S / 1000000)
131*199767f8SToomas Soome #define SBT_1NS (SBT_1S / 1000000000)
132*199767f8SToomas Soome #define SBT_MAX 0x7fffffffffffffffLL
133*199767f8SToomas Soome
134*199767f8SToomas Soome static __inline int
sbintime_getsec(sbintime_t _sbt)135*199767f8SToomas Soome sbintime_getsec(sbintime_t _sbt)
136*199767f8SToomas Soome {
137*199767f8SToomas Soome
138*199767f8SToomas Soome return (_sbt >> 32);
139*199767f8SToomas Soome }
140*199767f8SToomas Soome
141*199767f8SToomas Soome static __inline sbintime_t
bttosbt(const struct bintime _bt)142*199767f8SToomas Soome bttosbt(const struct bintime _bt)
143*199767f8SToomas Soome {
144*199767f8SToomas Soome
145*199767f8SToomas Soome return (((sbintime_t)_bt.sec << 32) + (_bt.frac >> 32));
146*199767f8SToomas Soome }
147*199767f8SToomas Soome
148*199767f8SToomas Soome static __inline struct bintime
sbttobt(sbintime_t _sbt)149*199767f8SToomas Soome sbttobt(sbintime_t _sbt)
150*199767f8SToomas Soome {
151*199767f8SToomas Soome struct bintime _bt;
152*199767f8SToomas Soome
153*199767f8SToomas Soome _bt.sec = _sbt >> 32;
154*199767f8SToomas Soome _bt.frac = _sbt << 32;
155*199767f8SToomas Soome return (_bt);
156*199767f8SToomas Soome }
157*199767f8SToomas Soome
158*199767f8SToomas Soome /*-
159*199767f8SToomas Soome * Background information:
160*199767f8SToomas Soome *
161*199767f8SToomas Soome * When converting between timestamps on parallel timescales of differing
162*199767f8SToomas Soome * resolutions it is historical and scientific practice to round down rather
163*199767f8SToomas Soome * than doing 4/5 rounding.
164*199767f8SToomas Soome *
165*199767f8SToomas Soome * The date changes at midnight, not at noon.
166*199767f8SToomas Soome *
167*199767f8SToomas Soome * Even at 15:59:59.999999999 it's not four'o'clock.
168*199767f8SToomas Soome *
169*199767f8SToomas Soome * time_second ticks after N.999999999 not after N.4999999999
170*199767f8SToomas Soome */
171*199767f8SToomas Soome
172*199767f8SToomas Soome static __inline void
bintime2timespec(const struct bintime * _bt,struct timespec * _ts)173*199767f8SToomas Soome bintime2timespec(const struct bintime *_bt, struct timespec *_ts)
174*199767f8SToomas Soome {
175*199767f8SToomas Soome
176*199767f8SToomas Soome _ts->tv_sec = _bt->sec;
177*199767f8SToomas Soome _ts->tv_nsec = ((uint64_t)1000000000 *
178*199767f8SToomas Soome (uint32_t)(_bt->frac >> 32)) >> 32;
179*199767f8SToomas Soome }
180*199767f8SToomas Soome
181*199767f8SToomas Soome static __inline void
timespec2bintime(const struct timespec * _ts,struct bintime * _bt)182*199767f8SToomas Soome timespec2bintime(const struct timespec *_ts, struct bintime *_bt)
183*199767f8SToomas Soome {
184*199767f8SToomas Soome
185*199767f8SToomas Soome _bt->sec = _ts->tv_sec;
186*199767f8SToomas Soome /* 18446744073 = int(2^64 / 1000000000) */
187*199767f8SToomas Soome _bt->frac = _ts->tv_nsec * (uint64_t)18446744073LL;
188*199767f8SToomas Soome }
189*199767f8SToomas Soome
190*199767f8SToomas Soome static __inline void
bintime2timeval(const struct bintime * _bt,struct timeval * _tv)191*199767f8SToomas Soome bintime2timeval(const struct bintime *_bt, struct timeval *_tv)
192*199767f8SToomas Soome {
193*199767f8SToomas Soome
194*199767f8SToomas Soome _tv->tv_sec = _bt->sec;
195*199767f8SToomas Soome _tv->tv_usec = ((uint64_t)1000000 * (uint32_t)(_bt->frac >> 32)) >> 32;
196*199767f8SToomas Soome }
197*199767f8SToomas Soome
198*199767f8SToomas Soome static __inline void
timeval2bintime(const struct timeval * _tv,struct bintime * _bt)199*199767f8SToomas Soome timeval2bintime(const struct timeval *_tv, struct bintime *_bt)
200*199767f8SToomas Soome {
201*199767f8SToomas Soome
202*199767f8SToomas Soome _bt->sec = _tv->tv_sec;
203*199767f8SToomas Soome /* 18446744073709 = int(2^64 / 1000000) */
204*199767f8SToomas Soome _bt->frac = _tv->tv_usec * (uint64_t)18446744073709LL;
205*199767f8SToomas Soome }
206*199767f8SToomas Soome
207*199767f8SToomas Soome static __inline struct timespec
sbttots(sbintime_t _sbt)208*199767f8SToomas Soome sbttots(sbintime_t _sbt)
209*199767f8SToomas Soome {
210*199767f8SToomas Soome struct timespec _ts;
211*199767f8SToomas Soome
212*199767f8SToomas Soome _ts.tv_sec = _sbt >> 32;
213*199767f8SToomas Soome _ts.tv_nsec = ((uint64_t)1000000000 * (uint32_t)_sbt) >> 32;
214*199767f8SToomas Soome return (_ts);
215*199767f8SToomas Soome }
216*199767f8SToomas Soome
217*199767f8SToomas Soome static __inline sbintime_t
tstosbt(struct timespec _ts)218*199767f8SToomas Soome tstosbt(struct timespec _ts)
219*199767f8SToomas Soome {
220*199767f8SToomas Soome
221*199767f8SToomas Soome return (((sbintime_t)_ts.tv_sec << 32) +
222*199767f8SToomas Soome (_ts.tv_nsec * (((uint64_t)1 << 63) / 500000000) >> 32));
223*199767f8SToomas Soome }
224*199767f8SToomas Soome
225*199767f8SToomas Soome static __inline struct timeval
sbttotv(sbintime_t _sbt)226*199767f8SToomas Soome sbttotv(sbintime_t _sbt)
227*199767f8SToomas Soome {
228*199767f8SToomas Soome struct timeval _tv;
229*199767f8SToomas Soome
230*199767f8SToomas Soome _tv.tv_sec = _sbt >> 32;
231*199767f8SToomas Soome _tv.tv_usec = ((uint64_t)1000000 * (uint32_t)_sbt) >> 32;
232*199767f8SToomas Soome return (_tv);
233*199767f8SToomas Soome }
234*199767f8SToomas Soome
235*199767f8SToomas Soome static __inline sbintime_t
tvtosbt(struct timeval _tv)236*199767f8SToomas Soome tvtosbt(struct timeval _tv)
237*199767f8SToomas Soome {
238*199767f8SToomas Soome
239*199767f8SToomas Soome return (((sbintime_t)_tv.tv_sec << 32) +
240*199767f8SToomas Soome (_tv.tv_usec * (((uint64_t)1 << 63) / 500000) >> 32));
241*199767f8SToomas Soome }
242*199767f8SToomas Soome #endif /* __BSD_VISIBLE */
243*199767f8SToomas Soome
244*199767f8SToomas Soome #ifdef _KERNEL
245*199767f8SToomas Soome
246*199767f8SToomas Soome /* Operations on timespecs */
247*199767f8SToomas Soome #define timespecclear(tvp) ((tvp)->tv_sec = (tvp)->tv_nsec = 0)
248*199767f8SToomas Soome #define timespecisset(tvp) ((tvp)->tv_sec || (tvp)->tv_nsec)
249*199767f8SToomas Soome #define timespeccmp(tvp, uvp, cmp) \
250*199767f8SToomas Soome (((tvp)->tv_sec == (uvp)->tv_sec) ? \
251*199767f8SToomas Soome ((tvp)->tv_nsec cmp (uvp)->tv_nsec) : \
252*199767f8SToomas Soome ((tvp)->tv_sec cmp (uvp)->tv_sec))
253*199767f8SToomas Soome #define timespecadd(vvp, uvp) \
254*199767f8SToomas Soome do { \
255*199767f8SToomas Soome (vvp)->tv_sec += (uvp)->tv_sec; \
256*199767f8SToomas Soome (vvp)->tv_nsec += (uvp)->tv_nsec; \
257*199767f8SToomas Soome if ((vvp)->tv_nsec >= 1000000000) { \
258*199767f8SToomas Soome (vvp)->tv_sec++; \
259*199767f8SToomas Soome (vvp)->tv_nsec -= 1000000000; \
260*199767f8SToomas Soome } \
261*199767f8SToomas Soome } while (0)
262*199767f8SToomas Soome #define timespecsub(vvp, uvp) \
263*199767f8SToomas Soome do { \
264*199767f8SToomas Soome (vvp)->tv_sec -= (uvp)->tv_sec; \
265*199767f8SToomas Soome (vvp)->tv_nsec -= (uvp)->tv_nsec; \
266*199767f8SToomas Soome if ((vvp)->tv_nsec < 0) { \
267*199767f8SToomas Soome (vvp)->tv_sec--; \
268*199767f8SToomas Soome (vvp)->tv_nsec += 1000000000; \
269*199767f8SToomas Soome } \
270*199767f8SToomas Soome } while (0)
271*199767f8SToomas Soome
272*199767f8SToomas Soome /* Operations on timevals. */
273*199767f8SToomas Soome
274*199767f8SToomas Soome #define timevalclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0)
275*199767f8SToomas Soome #define timevalisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
276*199767f8SToomas Soome #define timevalcmp(tvp, uvp, cmp) \
277*199767f8SToomas Soome (((tvp)->tv_sec == (uvp)->tv_sec) ? \
278*199767f8SToomas Soome ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
279*199767f8SToomas Soome ((tvp)->tv_sec cmp (uvp)->tv_sec))
280*199767f8SToomas Soome
281*199767f8SToomas Soome /* timevaladd and timevalsub are not inlined */
282*199767f8SToomas Soome
283*199767f8SToomas Soome #endif /* _KERNEL */
284*199767f8SToomas Soome
285*199767f8SToomas Soome #ifndef _KERNEL /* NetBSD/OpenBSD compatible interfaces */
286*199767f8SToomas Soome
287*199767f8SToomas Soome #define timerclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0)
288*199767f8SToomas Soome #define timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec)
289*199767f8SToomas Soome #define timercmp(tvp, uvp, cmp) \
290*199767f8SToomas Soome (((tvp)->tv_sec == (uvp)->tv_sec) ? \
291*199767f8SToomas Soome ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
292*199767f8SToomas Soome ((tvp)->tv_sec cmp (uvp)->tv_sec))
293*199767f8SToomas Soome #define timeradd(tvp, uvp, vvp) \
294*199767f8SToomas Soome do { \
295*199767f8SToomas Soome (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
296*199767f8SToomas Soome (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
297*199767f8SToomas Soome if ((vvp)->tv_usec >= 1000000) { \
298*199767f8SToomas Soome (vvp)->tv_sec++; \
299*199767f8SToomas Soome (vvp)->tv_usec -= 1000000; \
300*199767f8SToomas Soome } \
301*199767f8SToomas Soome } while (0)
302*199767f8SToomas Soome #define timersub(tvp, uvp, vvp) \
303*199767f8SToomas Soome do { \
304*199767f8SToomas Soome (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
305*199767f8SToomas Soome (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
306*199767f8SToomas Soome if ((vvp)->tv_usec < 0) { \
307*199767f8SToomas Soome (vvp)->tv_sec--; \
308*199767f8SToomas Soome (vvp)->tv_usec += 1000000; \
309*199767f8SToomas Soome } \
310*199767f8SToomas Soome } while (0)
311*199767f8SToomas Soome #endif
312*199767f8SToomas Soome
313*199767f8SToomas Soome /*
314*199767f8SToomas Soome * Names of the interval timers, and structure
315*199767f8SToomas Soome * defining a timer setting.
316*199767f8SToomas Soome */
317*199767f8SToomas Soome #define ITIMER_REAL 0
318*199767f8SToomas Soome #define ITIMER_VIRTUAL 1
319*199767f8SToomas Soome #define ITIMER_PROF 2
320*199767f8SToomas Soome
321*199767f8SToomas Soome struct itimerval {
322*199767f8SToomas Soome struct timeval it_interval; /* timer interval */
323*199767f8SToomas Soome struct timeval it_value; /* current value */
324*199767f8SToomas Soome };
325*199767f8SToomas Soome
326*199767f8SToomas Soome /*
327*199767f8SToomas Soome * Getkerninfo clock information structure
328*199767f8SToomas Soome */
329*199767f8SToomas Soome struct clockinfo {
330*199767f8SToomas Soome int hz; /* clock frequency */
331*199767f8SToomas Soome int tick; /* micro-seconds per hz tick */
332*199767f8SToomas Soome int spare;
333*199767f8SToomas Soome int stathz; /* statistics clock frequency */
334*199767f8SToomas Soome int profhz; /* profiling clock frequency */
335*199767f8SToomas Soome };
336*199767f8SToomas Soome
337*199767f8SToomas Soome /* These macros are also in time.h. */
338*199767f8SToomas Soome #ifndef CLOCK_REALTIME
339*199767f8SToomas Soome #define CLOCK_REALTIME 0
340*199767f8SToomas Soome #define CLOCK_VIRTUAL 1
341*199767f8SToomas Soome #define CLOCK_PROF 2
342*199767f8SToomas Soome #define CLOCK_MONOTONIC 4
343*199767f8SToomas Soome #define CLOCK_UPTIME 5 /* FreeBSD-specific. */
344*199767f8SToomas Soome #define CLOCK_UPTIME_PRECISE 7 /* FreeBSD-specific. */
345*199767f8SToomas Soome #define CLOCK_UPTIME_FAST 8 /* FreeBSD-specific. */
346*199767f8SToomas Soome #define CLOCK_REALTIME_PRECISE 9 /* FreeBSD-specific. */
347*199767f8SToomas Soome #define CLOCK_REALTIME_FAST 10 /* FreeBSD-specific. */
348*199767f8SToomas Soome #define CLOCK_MONOTONIC_PRECISE 11 /* FreeBSD-specific. */
349*199767f8SToomas Soome #define CLOCK_MONOTONIC_FAST 12 /* FreeBSD-specific. */
350*199767f8SToomas Soome #define CLOCK_SECOND 13 /* FreeBSD-specific. */
351*199767f8SToomas Soome #define CLOCK_THREAD_CPUTIME_ID 14
352*199767f8SToomas Soome #define CLOCK_PROCESS_CPUTIME_ID 15
353*199767f8SToomas Soome #endif
354*199767f8SToomas Soome
355*199767f8SToomas Soome #ifndef TIMER_ABSTIME
356*199767f8SToomas Soome #define TIMER_RELTIME 0x0 /* relative timer */
357*199767f8SToomas Soome #define TIMER_ABSTIME 0x1 /* absolute timer */
358*199767f8SToomas Soome #endif
359*199767f8SToomas Soome
360*199767f8SToomas Soome #if __BSD_VISIBLE
361*199767f8SToomas Soome #define CPUCLOCK_WHICH_PID 0
362*199767f8SToomas Soome #define CPUCLOCK_WHICH_TID 1
363*199767f8SToomas Soome #endif
364*199767f8SToomas Soome
365*199767f8SToomas Soome #ifdef _KERNEL
366*199767f8SToomas Soome
367*199767f8SToomas Soome /*
368*199767f8SToomas Soome * Kernel to clock driver interface.
369*199767f8SToomas Soome */
370*199767f8SToomas Soome void inittodr(time_t base);
371*199767f8SToomas Soome void resettodr(void);
372*199767f8SToomas Soome
373*199767f8SToomas Soome extern volatile time_t time_second;
374*199767f8SToomas Soome extern volatile time_t time_uptime;
375*199767f8SToomas Soome extern struct bintime boottimebin;
376*199767f8SToomas Soome extern struct timeval boottime;
377*199767f8SToomas Soome extern struct bintime tc_tick_bt;
378*199767f8SToomas Soome extern sbintime_t tc_tick_sbt;
379*199767f8SToomas Soome extern struct bintime tick_bt;
380*199767f8SToomas Soome extern sbintime_t tick_sbt;
381*199767f8SToomas Soome extern int tc_precexp;
382*199767f8SToomas Soome extern int tc_timepercentage;
383*199767f8SToomas Soome extern struct bintime bt_timethreshold;
384*199767f8SToomas Soome extern struct bintime bt_tickthreshold;
385*199767f8SToomas Soome extern sbintime_t sbt_timethreshold;
386*199767f8SToomas Soome extern sbintime_t sbt_tickthreshold;
387*199767f8SToomas Soome
388*199767f8SToomas Soome /*
389*199767f8SToomas Soome * Functions for looking at our clock: [get]{bin,nano,micro}[up]time()
390*199767f8SToomas Soome *
391*199767f8SToomas Soome * Functions without the "get" prefix returns the best timestamp
392*199767f8SToomas Soome * we can produce in the given format.
393*199767f8SToomas Soome *
394*199767f8SToomas Soome * "bin" == struct bintime == seconds + 64 bit fraction of seconds.
395*199767f8SToomas Soome * "nano" == struct timespec == seconds + nanoseconds.
396*199767f8SToomas Soome * "micro" == struct timeval == seconds + microseconds.
397*199767f8SToomas Soome *
398*199767f8SToomas Soome * Functions containing "up" returns time relative to boot and
399*199767f8SToomas Soome * should be used for calculating time intervals.
400*199767f8SToomas Soome *
401*199767f8SToomas Soome * Functions without "up" returns UTC time.
402*199767f8SToomas Soome *
403*199767f8SToomas Soome * Functions with the "get" prefix returns a less precise result
404*199767f8SToomas Soome * much faster than the functions without "get" prefix and should
405*199767f8SToomas Soome * be used where a precision of 1/hz seconds is acceptable or where
406*199767f8SToomas Soome * performance is priority. (NB: "precision", _not_ "resolution" !)
407*199767f8SToomas Soome */
408*199767f8SToomas Soome
409*199767f8SToomas Soome void binuptime(struct bintime *bt);
410*199767f8SToomas Soome void nanouptime(struct timespec *tsp);
411*199767f8SToomas Soome void microuptime(struct timeval *tvp);
412*199767f8SToomas Soome
413*199767f8SToomas Soome static __inline sbintime_t
sbinuptime(void)414*199767f8SToomas Soome sbinuptime(void)
415*199767f8SToomas Soome {
416*199767f8SToomas Soome struct bintime _bt;
417*199767f8SToomas Soome
418*199767f8SToomas Soome binuptime(&_bt);
419*199767f8SToomas Soome return (bttosbt(_bt));
420*199767f8SToomas Soome }
421*199767f8SToomas Soome
422*199767f8SToomas Soome void bintime(struct bintime *bt);
423*199767f8SToomas Soome void nanotime(struct timespec *tsp);
424*199767f8SToomas Soome void microtime(struct timeval *tvp);
425*199767f8SToomas Soome
426*199767f8SToomas Soome void getbinuptime(struct bintime *bt);
427*199767f8SToomas Soome void getnanouptime(struct timespec *tsp);
428*199767f8SToomas Soome void getmicrouptime(struct timeval *tvp);
429*199767f8SToomas Soome
430*199767f8SToomas Soome static __inline sbintime_t
getsbinuptime(void)431*199767f8SToomas Soome getsbinuptime(void)
432*199767f8SToomas Soome {
433*199767f8SToomas Soome struct bintime _bt;
434*199767f8SToomas Soome
435*199767f8SToomas Soome getbinuptime(&_bt);
436*199767f8SToomas Soome return (bttosbt(_bt));
437*199767f8SToomas Soome }
438*199767f8SToomas Soome
439*199767f8SToomas Soome void getbintime(struct bintime *bt);
440*199767f8SToomas Soome void getnanotime(struct timespec *tsp);
441*199767f8SToomas Soome void getmicrotime(struct timeval *tvp);
442*199767f8SToomas Soome
443*199767f8SToomas Soome /* Other functions */
444*199767f8SToomas Soome int itimerdecr(struct itimerval *itp, int usec);
445*199767f8SToomas Soome int itimerfix(struct timeval *tv);
446*199767f8SToomas Soome int ppsratecheck(struct timeval *, int *, int);
447*199767f8SToomas Soome int ratecheck(struct timeval *, const struct timeval *);
448*199767f8SToomas Soome void timevaladd(struct timeval *t1, const struct timeval *t2);
449*199767f8SToomas Soome void timevalsub(struct timeval *t1, const struct timeval *t2);
450*199767f8SToomas Soome int tvtohz(struct timeval *tv);
451*199767f8SToomas Soome
452*199767f8SToomas Soome #define TC_DEFAULTPERC 5
453*199767f8SToomas Soome
454*199767f8SToomas Soome #define BT2FREQ(bt) \
455*199767f8SToomas Soome (((uint64_t)0x8000000000000000 + ((bt)->frac >> 2)) / \
456*199767f8SToomas Soome ((bt)->frac >> 1))
457*199767f8SToomas Soome
458*199767f8SToomas Soome #define SBT2FREQ(sbt) ((SBT_1S + ((sbt) >> 1)) / (sbt))
459*199767f8SToomas Soome
460*199767f8SToomas Soome #define FREQ2BT(freq, bt) \
461*199767f8SToomas Soome { \
462*199767f8SToomas Soome (bt)->sec = 0; \
463*199767f8SToomas Soome (bt)->frac = ((uint64_t)0x8000000000000000 / (freq)) << 1; \
464*199767f8SToomas Soome }
465*199767f8SToomas Soome
466*199767f8SToomas Soome #define TIMESEL(sbt, sbt2) \
467*199767f8SToomas Soome (((sbt2) >= sbt_timethreshold) ? \
468*199767f8SToomas Soome ((*(sbt) = getsbinuptime()), 1) : ((*(sbt) = sbinuptime()), 0))
469*199767f8SToomas Soome
470*199767f8SToomas Soome #else /* !_KERNEL */
471*199767f8SToomas Soome #include <time.h>
472*199767f8SToomas Soome
473*199767f8SToomas Soome #include <sys/cdefs.h>
474*199767f8SToomas Soome #include <sys/select.h>
475*199767f8SToomas Soome
476*199767f8SToomas Soome __BEGIN_DECLS
477*199767f8SToomas Soome int setitimer(int, const struct itimerval *, struct itimerval *);
478*199767f8SToomas Soome int utimes(const char *, const struct timeval *);
479*199767f8SToomas Soome
480*199767f8SToomas Soome #if __BSD_VISIBLE
481*199767f8SToomas Soome int adjtime(const struct timeval *, struct timeval *);
482*199767f8SToomas Soome int clock_getcpuclockid2(id_t, int, clockid_t *);
483*199767f8SToomas Soome int futimes(int, const struct timeval *);
484*199767f8SToomas Soome int futimesat(int, const char *, const struct timeval [2]);
485*199767f8SToomas Soome int lutimes(const char *, const struct timeval *);
486*199767f8SToomas Soome int settimeofday(const struct timeval *, const struct timezone *);
487*199767f8SToomas Soome #endif
488*199767f8SToomas Soome
489*199767f8SToomas Soome #if __XSI_VISIBLE
490*199767f8SToomas Soome int getitimer(int, struct itimerval *);
491*199767f8SToomas Soome int gettimeofday(struct timeval *, struct timezone *);
492*199767f8SToomas Soome #endif
493*199767f8SToomas Soome
494*199767f8SToomas Soome __END_DECLS
495*199767f8SToomas Soome
496*199767f8SToomas Soome #endif /* !_KERNEL */
497*199767f8SToomas Soome
498*199767f8SToomas Soome #endif /* !_SYS_TIME_H_ */
499