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 #ifndef _SYS_TYPES_H_ 37 #include <sys/types.h> 38 #endif 39 40 #include <sys/_timespec.h> 41 #include <sys/_timeval.h> 42 #include <sys/select.h> 43 44 #define TIMEVAL_TO_TIMESPEC(tv, ts) \ 45 do { \ 46 (ts)->tv_sec = (tv)->tv_sec; \ 47 (ts)->tv_nsec = (tv)->tv_usec * 1000; \ 48 } while (0) 49 #define TIMESPEC_TO_TIMEVAL(tv, ts) \ 50 do { \ 51 (tv)->tv_sec = (ts)->tv_sec; \ 52 (tv)->tv_usec = (ts)->tv_nsec / 1000; \ 53 } while (0) 54 55 struct timezone { 56 int tz_minuteswest; /* minutes west of Greenwich */ 57 int tz_dsttime; /* type of dst correction */ 58 }; 59 #define DST_NONE 0 /* not on dst */ 60 #define DST_USA 1 /* USA style dst */ 61 #define DST_AUST 2 /* Australian style dst */ 62 #define DST_WET 3 /* Western European dst */ 63 #define DST_MET 4 /* Middle European dst */ 64 #define DST_EET 5 /* Eastern European dst */ 65 #define DST_CAN 6 /* Canada */ 66 67 #ifdef _KERNEL 68 69 /* Operations on timespecs */ 70 #define timespecclear(tvp) ((tvp)->tv_sec = (tvp)->tv_nsec = 0) 71 #define timespecisset(tvp) ((tvp)->tv_sec || (tvp)->tv_nsec) 72 #define timespeccmp(tvp, uvp, cmp) \ 73 (((tvp)->tv_sec == (uvp)->tv_sec) ? \ 74 ((tvp)->tv_nsec cmp (uvp)->tv_nsec) : \ 75 ((tvp)->tv_sec cmp (uvp)->tv_sec)) 76 #define timespecadd(vvp, uvp) \ 77 do { \ 78 (vvp)->tv_sec += (uvp)->tv_sec; \ 79 (vvp)->tv_nsec += (uvp)->tv_nsec; \ 80 if ((vvp)->tv_nsec >= 1000000000) { \ 81 (vvp)->tv_sec++; \ 82 (vvp)->tv_nsec -= 1000000000; \ 83 } \ 84 } while (0) 85 #define timespecsub(vvp, uvp) \ 86 do { \ 87 (vvp)->tv_sec -= (uvp)->tv_sec; \ 88 (vvp)->tv_nsec -= (uvp)->tv_nsec; \ 89 if ((vvp)->tv_nsec < 0) { \ 90 (vvp)->tv_sec--; \ 91 (vvp)->tv_nsec += 1000000000; \ 92 } \ 93 } while (0) 94 95 /* Operations on timevals. */ 96 97 #define timevalclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0) 98 #define timevalisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec) 99 #define timevalcmp(tvp, uvp, cmp) \ 100 (((tvp)->tv_sec == (uvp)->tv_sec) ? \ 101 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \ 102 ((tvp)->tv_sec cmp (uvp)->tv_sec)) 103 104 /* timevaladd and timevalsub are not inlined */ 105 106 #endif /* _KERNEL */ 107 108 #ifndef _KERNEL /* NetBSD/OpenBSD compatible interfaces */ 109 110 #define timerclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0) 111 #define timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec) 112 #define timercmp(tvp, uvp, cmp) \ 113 (((tvp)->tv_sec == (uvp)->tv_sec) ? \ 114 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \ 115 ((tvp)->tv_sec cmp (uvp)->tv_sec)) 116 #define timeradd(tvp, uvp, vvp) \ 117 do { \ 118 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \ 119 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \ 120 if ((vvp)->tv_usec >= 1000000) { \ 121 (vvp)->tv_sec++; \ 122 (vvp)->tv_usec -= 1000000; \ 123 } \ 124 } while (0) 125 #define timersub(tvp, uvp, vvp) \ 126 do { \ 127 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \ 128 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \ 129 if ((vvp)->tv_usec < 0) { \ 130 (vvp)->tv_sec--; \ 131 (vvp)->tv_usec += 1000000; \ 132 } \ 133 } while (0) 134 #endif 135 136 /* 137 * Names of the interval timers, and structure 138 * defining a timer setting. 139 */ 140 #define ITIMER_REAL 0 141 #define ITIMER_VIRTUAL 1 142 #define ITIMER_PROF 2 143 144 struct itimerval { 145 struct timeval it_interval; /* timer interval */ 146 struct timeval it_value; /* current value */ 147 }; 148 149 /* 150 * Getkerninfo clock information structure 151 */ 152 struct clockinfo { 153 int hz; /* clock frequency */ 154 int tick; /* micro-seconds per hz tick */ 155 int tickadj; /* clock skew rate for adjtime() */ 156 int stathz; /* statistics clock frequency */ 157 int profhz; /* profiling clock frequency */ 158 }; 159 160 /* CLOCK_REALTIME and TIMER_ABSTIME are supposed to be in time.h */ 161 162 #ifndef CLOCK_REALTIME 163 #define CLOCK_REALTIME 0 164 #endif 165 #define CLOCK_VIRTUAL 1 166 #define CLOCK_PROF 2 167 #define CLOCK_MONOTONIC 4 168 169 #define CLOCK_UPTIME 5 /* from freebsd */ 170 #define CLOCK_UPTIME_PRECISE 7 /* from freebsd */ 171 #define CLOCK_UPTIME_FAST 8 /* from freebsd */ 172 #define CLOCK_REALTIME_PRECISE 9 /* from freebsd */ 173 #define CLOCK_REALTIME_FAST 10 /* from freebsd */ 174 #define CLOCK_MONOTONIC_PRECISE 11 /* from freebsd */ 175 #define CLOCK_MONOTONIC_FAST 12 /* from freebsd */ 176 #define CLOCK_SECOND 13 /* from freebsd */ 177 #define CLOCK_THREAD_CPUTIME_ID 14 178 #define CLOCK_PROCESS_CPUTIME_ID 15 179 180 #define TIMER_RELTIME 0x0 /* relative timer */ 181 #ifndef TIMER_ABSTIME 182 #define TIMER_ABSTIME 0x1 /* absolute timer */ 183 #endif 184 185 #ifdef _KERNEL 186 187 extern time_t time_second; /* simple time_t (can step) */ 188 extern time_t time_uptime; /* monotonic simple uptime / seconds */ 189 extern int64_t ntp_tick_permanent; 190 extern int64_t ntp_tick_acc; 191 extern int64_t ntp_delta; 192 extern int64_t ntp_big_delta; 193 extern int32_t ntp_tick_delta; 194 extern int32_t ntp_default_tick_delta; 195 extern time_t ntp_leap_second; 196 extern int ntp_leap_insert; 197 198 void initclocks_pcpu(void); 199 void getmicrouptime (struct timeval *tv); 200 void getmicrotime (struct timeval *tv); 201 void getnanouptime (struct timespec *tv); 202 void getnanotime (struct timespec *tv); 203 int itimerdecr (struct itimerval *itp, int usec); 204 int itimerfix (struct timeval *tv); 205 int itimespecfix (struct timespec *ts); 206 int ppsratecheck (struct timeval *, int *, int usec); 207 int ratecheck (struct timeval *, const struct timeval *); 208 void microuptime (struct timeval *tv); 209 void microtime (struct timeval *tv); 210 void nanouptime (struct timespec *ts); 211 void nanotime (struct timespec *ts); 212 time_t get_approximate_time_t(void); 213 void set_timeofday(struct timespec *ts); 214 void kern_adjtime(int64_t, int64_t *); 215 void kern_reladjtime(int64_t); 216 void timevaladd (struct timeval *, const struct timeval *); 217 void timevalsub (struct timeval *, const struct timeval *); 218 int tvtohz_high (struct timeval *); 219 int tvtohz_low (struct timeval *); 220 int tstohz_high (struct timespec *); 221 int tstohz_low (struct timespec *); 222 int64_t tsc_get_target(int ns); 223 int tsc_test_target(int64_t target); 224 void tsc_delay(int ns); 225 int nanosleep1(struct timespec *rqt, struct timespec *rmt); 226 227 #else /* !_KERNEL */ 228 229 #include <time.h> 230 #include <sys/cdefs.h> 231 232 #endif /* !_KERNEL */ 233 234 __BEGIN_DECLS 235 236 #if __BSD_VISIBLE 237 int adjtime(const struct timeval *, struct timeval *); 238 int futimes(int, const struct timeval *); 239 int lutimes(const char *, const struct timeval *); 240 int settimeofday(const struct timeval *, const struct timezone *); 241 #endif 242 243 #if __XSI_VISIBLE 244 int getitimer(int, struct itimerval *); 245 int gettimeofday(struct timeval *, struct timezone *); 246 int setitimer(int, const struct itimerval *, struct itimerval *); 247 int utimes(const char *, const struct timeval *); 248 #endif 249 250 __END_DECLS 251 252 #endif /* !_SYS_TIME_H_ */ 253