xref: /dragonfly/sys/sys/time.h (revision 5af8357c50765945141a412d2cc5be992ebf8848)
1 /*
2  * Copyright (c) 1982, 1986, 1993
3  *        The Regents of the University of California.  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  * 3. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *        @(#)time.h          8.5 (Berkeley) 5/4/95
30  * $FreeBSD: src/sys/sys/time.h,v 1.42 1999/12/29 04:24:48 peter Exp $
31  */
32 
33 #ifndef _SYS_TIME_H_
34 #define _SYS_TIME_H_
35 
36 #ifdef _KERNEL
37 #include <sys/types.h>
38 #include <machine/clock.h>
39 #else
40 #include <machine/stdint.h>
41 #endif
42 
43 #include <sys/_timespec.h>
44 #include <sys/_timeval.h>
45 #include <sys/select.h>
46 
47 #define   TIMEVAL_TO_TIMESPEC(tv, ts)                                           \
48           do {                                                                            \
49                     (ts)->tv_sec = (tv)->tv_sec;                                \
50                     (ts)->tv_nsec = (tv)->tv_usec * 1000;                       \
51           } while (0)
52 #define   TIMESPEC_TO_TIMEVAL(tv, ts)                                           \
53           do {                                                                            \
54                     (tv)->tv_sec = (ts)->tv_sec;                                \
55                     (tv)->tv_usec = (ts)->tv_nsec / 1000;                       \
56           } while (0)
57 
58 struct timezone {
59           int       tz_minuteswest;     /* minutes west of Greenwich */
60           int       tz_dsttime;         /* type of dst correction */
61 };
62 #define   DST_NONE  0         /* not on dst */
63 #define   DST_USA             1         /* USA style dst */
64 #define   DST_AUST  2         /* Australian style dst */
65 #define   DST_WET             3         /* Western European dst */
66 #define   DST_MET             4         /* Middle European dst */
67 #define   DST_EET             5         /* Eastern European dst */
68 #define   DST_CAN             6         /* Canada */
69 
70 /* Operations on timespecs */
71 #define   timespecclear(tvp)  ((tvp)->tv_sec = (tvp)->tv_nsec = 0)
72 #define   timespecisset(tvp)  ((tvp)->tv_sec || (tvp)->tv_nsec)
73 #define   timespeccmp(tvp, uvp, cmp)                                            \
74           (((tvp)->tv_sec == (uvp)->tv_sec) ?                                   \
75               ((tvp)->tv_nsec cmp (uvp)->tv_nsec) :                             \
76               ((tvp)->tv_sec cmp (uvp)->tv_sec))
77 #define   timespecadd(tsp, usp, vsp)                                            \
78           do {                                                                            \
79                     (vsp)->tv_sec = (tsp)->tv_sec + (usp)->tv_sec;              \
80                     (vsp)->tv_nsec = (tsp)->tv_nsec + (usp)->tv_nsec; \
81                     if ((vsp)->tv_nsec >= 1000000000L) {                        \
82                               (vsp)->tv_sec++;                                  \
83                               (vsp)->tv_nsec -= 1000000000L;                              \
84                     }                                                                     \
85           } while (0)
86 #define   timespecsub(tsp, usp, vsp)                                            \
87           do {                                                                            \
88                     (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec;              \
89                     (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \
90                     if ((vsp)->tv_nsec < 0) {                                   \
91                               (vsp)->tv_sec--;                                  \
92                               (vsp)->tv_nsec += 1000000000L;                              \
93                     }                                                                     \
94           } while (0)
95 
96 /* Operations on timevals. */
97 
98 #define   timevalclear(tvp)             ((tvp)->tv_sec = (tvp)->tv_usec = 0)
99 #define   timevalisset(tvp)             ((tvp)->tv_sec || (tvp)->tv_usec)
100 #define   timevalcmp(tvp, uvp, cmp)                                             \
101           (((tvp)->tv_sec == (uvp)->tv_sec) ?                                   \
102               ((tvp)->tv_usec cmp (uvp)->tv_usec) :                             \
103               ((tvp)->tv_sec cmp (uvp)->tv_sec))
104 
105 /* timevaladd and timevalsub are not inlined */
106 
107 #ifndef _KERNEL                         /* NetBSD/OpenBSD compatible interfaces */
108 
109 #define   timerclear(tvp)               ((tvp)->tv_sec = (tvp)->tv_usec = 0)
110 #define   timerisset(tvp)               ((tvp)->tv_sec || (tvp)->tv_usec)
111 #define   timercmp(tvp, uvp, cmp)                                               \
112           (((tvp)->tv_sec == (uvp)->tv_sec) ?                                   \
113               ((tvp)->tv_usec cmp (uvp)->tv_usec) :                             \
114               ((tvp)->tv_sec cmp (uvp)->tv_sec))
115 #define timeradd(tvp, uvp, vvp)                                                           \
116           do {                                                                            \
117                     (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec;              \
118                     (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
119                     if ((vvp)->tv_usec >= 1000000) {                            \
120                               (vvp)->tv_sec++;                                  \
121                               (vvp)->tv_usec -= 1000000;                        \
122                     }                                                                     \
123           } while (0)
124 #define timersub(tvp, uvp, vvp)                                                           \
125           do {                                                                            \
126                     (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;              \
127                     (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
128                     if ((vvp)->tv_usec < 0) {                                   \
129                               (vvp)->tv_sec--;                                  \
130                               (vvp)->tv_usec += 1000000;                        \
131                     }                                                                     \
132           } while (0)
133 #endif
134 
135 /*
136  * Names of the interval timers, and structure
137  * defining a timer setting.
138  */
139 #define   ITIMER_REAL         0
140 #define   ITIMER_VIRTUAL      1
141 #define   ITIMER_PROF         2
142 
143 struct    itimerval {
144           struct    timeval it_interval;          /* timer interval */
145           struct    timeval it_value;   /* current value */
146 };
147 
148 /*
149  * Getkerninfo clock information structure
150  */
151 struct clockinfo {
152           int       hz;                 /* clock frequency */
153           int       tick;               /* micro-seconds per hz tick */
154           int       tickadj;  /* clock skew rate for adjtime() */
155           int       stathz;             /* statistics clock frequency */
156           int       profhz;             /* profiling clock frequency */
157 };
158 
159 /* CLOCK_REALTIME and TIMER_ABSTIME are supposed to be in time.h */
160 
161 #ifndef CLOCK_REALTIME
162 #define CLOCK_REALTIME                  0
163 #endif
164 #define CLOCK_VIRTUAL                   1
165 #define CLOCK_PROF            2
166 #define CLOCK_MONOTONIC                 4
167 
168 #define CLOCK_UPTIME                    5         /* from freebsd */
169 #define CLOCK_UPTIME_PRECISE  7         /* from freebsd */
170 #define CLOCK_UPTIME_FAST     8         /* from freebsd */
171 #define CLOCK_REALTIME_PRECISE          9         /* from freebsd */
172 #define CLOCK_REALTIME_FAST   10        /* from freebsd */
173 #define CLOCK_MONOTONIC_PRECISE         11        /* from freebsd */
174 #define CLOCK_MONOTONIC_FAST  12        /* from freebsd */
175 #define CLOCK_SECOND                    13        /* from freebsd */
176 #define CLOCK_THREAD_CPUTIME_ID                   14
177 #define CLOCK_PROCESS_CPUTIME_ID        15
178 
179 #define TIMER_RELTIME         0x0       /* relative timer */
180 #ifndef TIMER_ABSTIME
181 #define TIMER_ABSTIME         0x1       /* absolute timer */
182 #endif
183 
184 #ifdef _KERNEL
185 
186 extern time_t       time_second;                  /* simple time_t (can step) */
187 extern time_t       time_uptime;                  /* monotonic simple uptime / seconds */
188 extern int64_t      ntp_tick_permanent;
189 extern int64_t      ntp_tick_acc;
190 extern int64_t      ntp_delta;
191 extern int64_t      ntp_big_delta;
192 extern int32_t      ntp_tick_delta;
193 extern int32_t      ntp_default_tick_delta;
194 extern time_t       ntp_leap_second;
195 extern int          ntp_leap_insert;
196 
197 void      initclocks_pcpu(void);
198 void      getmicrouptime(struct timeval *tv);
199 void      getmicrotime(struct timeval *tv);
200 void      getnanouptime(struct timespec *tv);
201 void      getnanotime(struct timespec *tv);
202 int       itimerdecr(struct itimerval *itp, int usec);
203 int       itimerfix(struct timeval *tv);
204 int       itimespecfix(struct timespec *ts);
205 int       ppsratecheck(struct timeval *, int *, int usec);
206 int       ratecheck(struct timeval *, const struct timeval *);
207 void      microuptime(struct timeval *tv);
208 void      microtime(struct timeval *tv);
209 void      nanouptime(struct timespec *ts);
210 void      nanotime(struct timespec *ts);
211 time_t    get_approximate_time_t(void);
212 void      set_timeofday(struct timespec *ts);
213 void      kern_adjtime(int64_t, int64_t *);
214 void      kern_reladjtime(int64_t);
215 void      timevaladd(struct timeval *, const struct timeval *);
216 void      timevalsub(struct timeval *, const struct timeval *);
217 int       tvtohz_high(struct timeval *);
218 int       tvtohz_low(struct timeval *);
219 int       tstohz_high(struct timespec *);
220 int       tstohz_low(struct timespec *);
221 int       tsc_test_target(int64_t target);
222 void      tsc_delay(int ns);
223 int       clock_nanosleep1(clockid_t clock_id, int flags,
224                     struct timespec *rqt, struct timespec *rmt);
225 int       nanosleep1(struct timespec *rqt, struct timespec *rmt);
226 
227 void      timespec2fattime(const struct timespec *tsp, int utc, uint16_t *ddp,
228                     uint16_t *dtp, uint8_t *dhp);
229 void      fattime2timespec(unsigned dd, unsigned dt, unsigned dh, int utc,
230                     struct timespec *tsp);
231 
232 tsc_uclock_t tsc_get_target(int ns);
233 
234 #else /* !_KERNEL */
235 
236 #include <time.h>
237 #include <sys/cdefs.h>
238 
239 #endif /* _KERNEL */
240 
241 __BEGIN_DECLS
242 
243 #if __BSD_VISIBLE
244 int       adjtime(const struct timeval *, struct timeval *);
245 int       futimes(int, const struct timeval *);
246 int       futimesat(int fd, const char *path, const struct timeval times[2]);
247 int       lutimes(const char *, const struct timeval *);
248 int       settimeofday(const struct timeval *, const struct timezone *);
249 
250 #define HAVE_FUTIMESAT        1
251 
252 #endif
253 
254 #if __XSI_VISIBLE
255 int       getitimer(int, struct itimerval *);
256 int       gettimeofday(struct timeval * __restrict, struct timezone * __restrict);
257 int       setitimer(int, const struct itimerval * __restrict,
258               struct itimerval * __restrict);
259 int       utimes(const char *, const struct timeval *);
260 #endif
261 
262 __END_DECLS
263 
264 #endif /* !_SYS_TIME_H_ */
265