Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 2 | include/common/time.h |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3 | Time calculation functions and macros. |
| 4 | |
| 5 | Copyright (C) 2000-2006 Willy Tarreau - w@1wt.eu |
| 6 | |
| 7 | This library is free software; you can redistribute it and/or |
| 8 | modify it under the terms of the GNU Lesser General Public |
| 9 | License as published by the Free Software Foundation, version 2.1 |
| 10 | exclusively. |
| 11 | |
| 12 | This library is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | Lesser General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU Lesser General Public |
| 18 | License along with this library; if not, write to the Free Software |
| 19 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 22 | #ifndef _COMMON_TIME_H |
| 23 | #define _COMMON_TIME_H |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 24 | |
| 25 | #include <stdlib.h> |
| 26 | #include <sys/time.h> |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 27 | #include <common/config.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 28 | |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 29 | /* eternity when exprimed in timeval */ |
| 30 | #ifndef TV_ETERNITY |
| 31 | #define TV_ETERNITY (~0UL) |
| 32 | #endif |
| 33 | |
| 34 | /* eternity when exprimed in ms */ |
| 35 | #ifndef TV_ETERNITY_MS |
| 36 | #define TV_ETERNITY_MS (-1) |
| 37 | #endif |
| 38 | |
| 39 | #define TIME_ETERNITY (TV_ETERNITY_MS) |
| 40 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 41 | |
| 42 | /* returns the lowest delay amongst <old> and <new>, and respects TIME_ETERNITY */ |
| 43 | #define MINTIME(old, new) (((new)<0)?(old):(((old)<0||(new)<(old))?(new):(old))) |
| 44 | #define SETNOW(a) (*a=now) |
| 45 | |
| 46 | extern struct timeval now; /* the current date at any moment */ |
| 47 | extern struct timeval start_date; /* the process's start date */ |
| 48 | |
| 49 | |
| 50 | /* |
| 51 | * adds <ms> ms to <from>, set the result to <tv> and returns a pointer <tv> |
| 52 | */ |
Willy Tarreau | fb27867 | 2006-10-15 15:38:50 +0200 | [diff] [blame] | 53 | REGPRM3 struct timeval *tv_delayfrom(struct timeval *tv, const struct timeval *from, int ms); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 54 | |
| 55 | /* |
| 56 | * compares <tv1> and <tv2> modulo 1ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2 |
| 57 | * Must not be used when either argument is eternity. Use tv_cmp2_ms() for that. |
| 58 | */ |
Willy Tarreau | fb27867 | 2006-10-15 15:38:50 +0200 | [diff] [blame] | 59 | REGPRM2 int tv_cmp_ms(const struct timeval *tv1, const struct timeval *tv2); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 60 | |
| 61 | /* |
Willy Tarreau | 8d7d149 | 2007-04-29 10:50:43 +0200 | [diff] [blame^] | 62 | * compares <tv1> and <tv2> modulo 1 ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2, |
| 63 | * assuming that TV_ETERNITY is greater than everything. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 64 | */ |
Willy Tarreau | 8d7d149 | 2007-04-29 10:50:43 +0200 | [diff] [blame^] | 65 | REGPRM2 int tv_cmp2_ms(const struct timeval *tv1, const struct timeval *tv2); |
Willy Tarreau | 5e8f066 | 2007-02-12 00:59:08 +0100 | [diff] [blame] | 66 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 67 | /* |
Willy Tarreau | 8d7d149 | 2007-04-29 10:50:43 +0200 | [diff] [blame^] | 68 | * compares <tv1> and <tv2> modulo 1 ms: returns 1 if tv1 <= tv2, 0 if tv1 > tv2, |
| 69 | * assuming that TV_ETERNITY is greater than everything. Returns 0 if tv1 is |
| 70 | * TV_ETERNITY, and always assumes that tv2 != TV_ETERNITY. Designed to replace |
| 71 | * occurrences of (tv_cmp2_ms(tv,now) <= 0). |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 72 | */ |
Willy Tarreau | 8d7d149 | 2007-04-29 10:50:43 +0200 | [diff] [blame^] | 73 | REGPRM2 int tv_cmp2_le(const struct timeval *tv1, const struct timeval *tv2); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 74 | |
| 75 | /* |
| 76 | * returns the remaining time between tv1=now and event=tv2 |
| 77 | * if tv2 is passed, 0 is returned. |
| 78 | * Returns TIME_ETERNITY if tv2 is eternity. |
| 79 | */ |
Willy Tarreau | fb27867 | 2006-10-15 15:38:50 +0200 | [diff] [blame] | 80 | REGPRM2 unsigned long tv_remain2(const struct timeval *tv1, const struct timeval *tv2); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 81 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 82 | /* sets <tv> to the current time */ |
Willy Tarreau | fb27867 | 2006-10-15 15:38:50 +0200 | [diff] [blame] | 83 | REGPRM1 static inline struct timeval *tv_now(struct timeval *tv) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 84 | { |
| 85 | if (tv) |
| 86 | gettimeofday(tv, NULL); |
| 87 | return tv; |
| 88 | } |
| 89 | |
| 90 | /* |
| 91 | * compares <tv1> and <tv2> : returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2 |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 92 | */ |
Willy Tarreau | fb27867 | 2006-10-15 15:38:50 +0200 | [diff] [blame] | 93 | REGPRM2 static inline int tv_cmp(const struct timeval *tv1, const struct timeval *tv2) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 94 | { |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 95 | if ((unsigned)tv1->tv_sec < (unsigned)tv2->tv_sec) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 96 | return -1; |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 97 | else if ((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 98 | return 1; |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 99 | else if ((unsigned)tv1->tv_usec < (unsigned)tv2->tv_usec) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 100 | return -1; |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 101 | else if ((unsigned)tv1->tv_usec > (unsigned)tv2->tv_usec) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 102 | return 1; |
| 103 | else |
| 104 | return 0; |
Willy Tarreau | 5e8f066 | 2007-02-12 00:59:08 +0100 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /* |
| 108 | * compares <tv1> and <tv2> : returns 0 if tv1 < tv2, 1 if tv1 >= tv2 |
| 109 | */ |
| 110 | REGPRM2 static inline int tv_cmp_ge(const struct timeval *tv1, const struct timeval *tv2) |
| 111 | { |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 112 | if ((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec) |
Willy Tarreau | 5e8f066 | 2007-02-12 00:59:08 +0100 | [diff] [blame] | 113 | return 1; |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 114 | if ((unsigned)tv1->tv_sec < (unsigned)tv2->tv_sec) |
Willy Tarreau | 5e8f066 | 2007-02-12 00:59:08 +0100 | [diff] [blame] | 115 | return 0; |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 116 | if ((unsigned)tv1->tv_usec >= (unsigned)tv2->tv_usec) |
Willy Tarreau | 5e8f066 | 2007-02-12 00:59:08 +0100 | [diff] [blame] | 117 | return 1; |
| 118 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | /* |
| 122 | * returns the difference, in ms, between tv1 and tv2 |
| 123 | * Must not be used when either argument is eternity. |
| 124 | */ |
Willy Tarreau | fb27867 | 2006-10-15 15:38:50 +0200 | [diff] [blame] | 125 | REGPRM2 static inline unsigned long tv_diff(const struct timeval *tv1, const struct timeval *tv2) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 126 | { |
| 127 | unsigned long ret; |
| 128 | |
| 129 | ret = (tv2->tv_sec - tv1->tv_sec) * 1000; |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 130 | if ((unsigned)tv2->tv_usec > (unsigned)tv1->tv_usec) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 131 | ret += (tv2->tv_usec - tv1->tv_usec) / 1000; |
| 132 | else |
| 133 | ret -= (tv1->tv_usec - tv2->tv_usec) / 1000; |
| 134 | return (unsigned long) ret; |
| 135 | } |
| 136 | |
| 137 | /* |
| 138 | * returns the remaining time between tv1=now and event=tv2 |
| 139 | * if tv2 is passed, 0 is returned. |
| 140 | * Must not be used when either argument is eternity. |
| 141 | */ |
Willy Tarreau | fb27867 | 2006-10-15 15:38:50 +0200 | [diff] [blame] | 142 | REGPRM2 static inline unsigned long tv_remain(const struct timeval *tv1, const struct timeval *tv2) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 143 | { |
| 144 | unsigned long ret; |
| 145 | |
| 146 | if (tv_cmp_ms(tv1, tv2) >= 0) |
| 147 | return 0; /* event elapsed */ |
| 148 | |
| 149 | ret = (tv2->tv_sec - tv1->tv_sec) * 1000; |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 150 | if ((unsigned)tv2->tv_usec > (unsigned)tv1->tv_usec) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 151 | ret += (tv2->tv_usec - tv1->tv_usec) / 1000; |
| 152 | else |
| 153 | ret -= (tv1->tv_usec - tv2->tv_usec) / 1000; |
| 154 | return (unsigned long) ret; |
| 155 | } |
| 156 | |
| 157 | |
| 158 | /* |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 159 | * sets a struct timeval to its highest value so that it can never happen |
| 160 | * note that only tv_usec is necessary to detect it since a tv_usec > 999999 |
| 161 | * is normally not possible. |
| 162 | * |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 163 | */ |
| 164 | |
Willy Tarreau | fb27867 | 2006-10-15 15:38:50 +0200 | [diff] [blame] | 165 | REGPRM1 static inline struct timeval *tv_eternity(struct timeval *tv) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 166 | { |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 167 | tv->tv_sec = tv->tv_usec = TV_ETERNITY; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 168 | return tv; |
| 169 | } |
| 170 | |
| 171 | /* |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 172 | * sets a struct timeval to 0 |
| 173 | * |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 174 | */ |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 175 | REGPRM1 static inline struct timeval *tv_zero(struct timeval *tv) { |
| 176 | tv->tv_sec = tv->tv_usec = 0; |
| 177 | return tv; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | /* |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 181 | * returns non null if tv is [eternity], otherwise 0. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 182 | */ |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 183 | #define tv_iseternity(tv) ((tv)->tv_usec == TV_ETERNITY) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 184 | |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 185 | /* |
| 186 | * returns non null if tv is [0], otherwise 0. |
| 187 | */ |
| 188 | #define tv_iszero(tv) (((tv)->tv_sec | (tv)->tv_usec) == 0) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 189 | |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 190 | /* |
| 191 | * compares <tv1> and <tv2> : returns 1 if <tv1> is before <tv2>, otherwise 0. |
| 192 | * This should be very fast because it's used in schedulers. |
| 193 | * It has been optimized to return 1 (so call it in a loop which continues |
| 194 | * as long as tv1<=tv2) |
| 195 | */ |
| 196 | |
| 197 | #define tv_isbefore(tv1, tv2) \ |
| 198 | (unlikely((unsigned)(tv1)->tv_sec < (unsigned)(tv2)->tv_sec) ? 1 : \ |
| 199 | (unlikely((unsigned)(tv1)->tv_sec > (unsigned)(tv2)->tv_sec) ? 0 : \ |
| 200 | unlikely((unsigned)(tv1)->tv_usec < (unsigned)(tv2)->tv_usec))) |
| 201 | |
| 202 | /* |
| 203 | * returns the first event between <tv1> and <tv2> into <tvmin>. |
| 204 | * a zero tv is ignored. <tvmin> is returned. If <tvmin> is known |
| 205 | * to be the same as <tv1> or <tv2>, it is recommended to use |
| 206 | * tv_bound instead. |
| 207 | */ |
| 208 | #define tv_min(tvmin, tv1, tv2) ({ \ |
| 209 | if (tv_isbefore(tv1, tv2)) { \ |
| 210 | *tvmin = *tv1; \ |
| 211 | } \ |
| 212 | else { \ |
| 213 | *tvmin = *tv2; \ |
| 214 | } \ |
| 215 | tvmin; \ |
| 216 | }) |
| 217 | |
| 218 | /* |
| 219 | * returns the first event between <tv1> and <tv2> into <tvmin>. |
| 220 | * a zero tv is ignored. <tvmin> is returned. This function has been |
| 221 | * optimized to be called as tv_min(a,a,b) or tv_min(b,a,b). |
| 222 | */ |
| 223 | #define tv_bound(tv1, tv2) ({ \ |
| 224 | if (tv_isbefore(tv2, tv1)) \ |
| 225 | *tv1 = *tv2; \ |
| 226 | tv1; \ |
| 227 | }) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 228 | |
| 229 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 230 | #endif /* _COMMON_TIME_H */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 231 | |
| 232 | /* |
| 233 | * Local variables: |
| 234 | * c-indent-level: 8 |
| 235 | * c-basic-offset: 8 |
| 236 | * End: |
| 237 | */ |