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 | |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 5 | Copyright (C) 2000-2008 Willy Tarreau - w@1wt.eu |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 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 | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 28 | #include <common/standard.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 29 | |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 30 | #define SEC 1 |
| 31 | #define MINUTE (60 * SEC) |
| 32 | #define HOUR (60 * MINUTE) |
| 33 | #define DAY (24 * HOUR) |
| 34 | |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 35 | /* eternity when exprimed in timeval */ |
| 36 | #ifndef TV_ETERNITY |
| 37 | #define TV_ETERNITY (~0UL) |
| 38 | #endif |
| 39 | |
| 40 | /* eternity when exprimed in ms */ |
| 41 | #ifndef TV_ETERNITY_MS |
| 42 | #define TV_ETERNITY_MS (-1) |
| 43 | #endif |
| 44 | |
| 45 | #define TIME_ETERNITY (TV_ETERNITY_MS) |
| 46 | |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 47 | /* we want to be able to detect time jumps. Fix the maximum wait time to a low |
| 48 | * value so that we know the time has changed if we wait longer. |
| 49 | */ |
| 50 | #define MAX_DELAY_MS 1000 |
| 51 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 52 | |
| 53 | /* returns the lowest delay amongst <old> and <new>, and respects TIME_ETERNITY */ |
| 54 | #define MINTIME(old, new) (((new)<0)?(old):(((old)<0||(new)<(old))?(new):(old))) |
| 55 | #define SETNOW(a) (*a=now) |
| 56 | |
Willy Tarreau | e6313a3 | 2008-06-29 13:47:25 +0200 | [diff] [blame] | 57 | extern unsigned int now_ms; /* internal date in milliseconds (may wrap) */ |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 58 | extern struct timeval now; /* internal date is a monotonic function of real clock */ |
| 59 | extern struct timeval date; /* the real current date */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 60 | extern struct timeval start_date; /* the process's start date */ |
| 61 | |
| 62 | |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 63 | /**** exported functions *************************************************/ |
| 64 | |
| 65 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 66 | /* |
| 67 | * adds <ms> ms to <from>, set the result to <tv> and returns a pointer <tv> |
| 68 | */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 69 | REGPRM3 struct timeval *tv_ms_add(struct timeval *tv, const struct timeval *from, int ms); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 70 | |
| 71 | /* |
| 72 | * compares <tv1> and <tv2> modulo 1ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2 |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 73 | * Must not be used when either argument is eternity. Use tv_ms_cmp2() for that. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 74 | */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 75 | REGPRM2 int tv_ms_cmp(const struct timeval *tv1, const struct timeval *tv2); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 76 | |
| 77 | /* |
Willy Tarreau | 8d7d149 | 2007-04-29 10:50:43 +0200 | [diff] [blame] | 78 | * compares <tv1> and <tv2> modulo 1 ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2, |
| 79 | * assuming that TV_ETERNITY is greater than everything. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 80 | */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 81 | REGPRM2 int tv_ms_cmp2(const struct timeval *tv1, const struct timeval *tv2); |
Willy Tarreau | 5e8f066 | 2007-02-12 00:59:08 +0100 | [diff] [blame] | 82 | |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 83 | /**** general purpose functions and macros *******************************/ |
| 84 | |
| 85 | |
| 86 | /* tv_now: sets <tv> to the current time */ |
| 87 | REGPRM1 static inline struct timeval *tv_now(struct timeval *tv) |
| 88 | { |
| 89 | gettimeofday(tv, NULL); |
| 90 | return tv; |
| 91 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 92 | |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 93 | /* tv_udpate_date: sets <date> to system time, and sets <now> to something as |
| 94 | * close as possible to real time, following a monotonic function. The main |
| 95 | * principle consists in detecting backwards and forwards time jumps and adjust |
| 96 | * an offset to correct them. This function should be called only once after |
| 97 | * each poll. The poll's timeout should be passed in <max_wait>, and the return |
| 98 | * value in <interrupted> (a non-zero value means that we have not expired the |
| 99 | * timeout). |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 100 | */ |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 101 | REGPRM2 void tv_update_date(int max_wait, int interrupted); |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 102 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 103 | /* |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 104 | * sets a struct timeval to its highest value so that it can never happen |
| 105 | * note that only tv_usec is necessary to detect it since a tv_usec > 999999 |
| 106 | * is normally not possible. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 107 | */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 108 | REGPRM1 static inline struct timeval *tv_eternity(struct timeval *tv) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 109 | { |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 110 | tv->tv_sec = tv->tv_usec = TV_ETERNITY; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 111 | return tv; |
| 112 | } |
| 113 | |
| 114 | /* |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 115 | * sets a struct timeval to 0 |
| 116 | * |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 117 | */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 118 | REGPRM1 static inline struct timeval *tv_zero(struct timeval *tv) { |
| 119 | tv->tv_sec = tv->tv_usec = 0; |
| 120 | return tv; |
| 121 | } |
| 122 | |
| 123 | /* |
| 124 | * returns non null if tv is [eternity], otherwise 0. |
| 125 | */ |
| 126 | #define tv_iseternity(tv) ((tv)->tv_usec == TV_ETERNITY) |
| 127 | |
| 128 | /* |
Willy Tarreau | 49fa3a1 | 2007-05-12 22:29:44 +0200 | [diff] [blame] | 129 | * returns 0 if tv is [eternity], otherwise non-zero. |
| 130 | */ |
| 131 | #define tv_isset(tv) ((tv)->tv_usec != TV_ETERNITY) |
| 132 | |
| 133 | /* |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 134 | * returns non null if tv is [0], otherwise 0. |
| 135 | */ |
| 136 | #define tv_iszero(tv) (((tv)->tv_sec | (tv)->tv_usec) == 0) |
| 137 | |
Willy Tarreau | 49fa3a1 | 2007-05-12 22:29:44 +0200 | [diff] [blame] | 138 | /* |
| 139 | * Converts a struct timeval to a number of milliseconds. |
| 140 | */ |
| 141 | REGPRM1 static inline unsigned long __tv_to_ms(const struct timeval *tv) |
| 142 | { |
| 143 | unsigned long ret; |
| 144 | |
| 145 | ret = tv->tv_sec * 1000; |
| 146 | ret += tv->tv_usec / 1000; |
| 147 | return ret; |
| 148 | } |
| 149 | |
| 150 | /* |
| 151 | * Converts a struct timeval to a number of milliseconds. |
| 152 | */ |
| 153 | REGPRM2 static inline struct timeval * __tv_from_ms(struct timeval *tv, unsigned long ms) |
| 154 | { |
| 155 | tv->tv_sec = ms / 1000; |
| 156 | tv->tv_usec = (ms % 1000) * 1000; |
| 157 | return tv; |
| 158 | } |
| 159 | |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 160 | |
Willy Tarreau | 49fa3a1 | 2007-05-12 22:29:44 +0200 | [diff] [blame] | 161 | /**** comparison functions and macros ***********************************/ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 162 | |
| 163 | |
| 164 | /* tv_cmp: compares <tv1> and <tv2> : returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2. */ |
| 165 | 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] | 166 | { |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 167 | if ((unsigned)tv1->tv_sec < (unsigned)tv2->tv_sec) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 168 | return -1; |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 169 | else if ((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 170 | return 1; |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 171 | else if ((unsigned)tv1->tv_usec < (unsigned)tv2->tv_usec) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 172 | return -1; |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 173 | else if ((unsigned)tv1->tv_usec > (unsigned)tv2->tv_usec) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 174 | return 1; |
| 175 | else |
| 176 | return 0; |
Willy Tarreau | 5e8f066 | 2007-02-12 00:59:08 +0100 | [diff] [blame] | 177 | } |
| 178 | |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 179 | /* tv_iseq: compares <tv1> and <tv2> : returns 1 if tv1 == tv2, otherwise 0 */ |
Willy Tarreau | 0481c20 | 2007-05-13 16:03:27 +0200 | [diff] [blame] | 180 | #define tv_iseq __tv_iseq |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 181 | REGPRM2 static inline int __tv_iseq(const struct timeval *tv1, const struct timeval *tv2) |
| 182 | { |
Willy Tarreau | 0481c20 | 2007-05-13 16:03:27 +0200 | [diff] [blame] | 183 | return ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) && |
| 184 | ((unsigned)tv1->tv_usec == (unsigned)tv2->tv_usec); |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | /* tv_isgt: compares <tv1> and <tv2> : returns 1 if tv1 > tv2, otherwise 0 */ |
Willy Tarreau | 0481c20 | 2007-05-13 16:03:27 +0200 | [diff] [blame] | 188 | #define tv_isgt _tv_isgt |
| 189 | REGPRM2 int _tv_isgt(const struct timeval *tv1, const struct timeval *tv2); |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 190 | REGPRM2 static inline int __tv_isgt(const struct timeval *tv1, const struct timeval *tv2) |
| 191 | { |
Willy Tarreau | 0481c20 | 2007-05-13 16:03:27 +0200 | [diff] [blame] | 192 | return |
| 193 | ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) ? |
| 194 | ((unsigned)tv1->tv_usec > (unsigned)tv2->tv_usec) : |
| 195 | ((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec); |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | /* tv_isge: compares <tv1> and <tv2> : returns 1 if tv1 >= tv2, otherwise 0 */ |
Willy Tarreau | 0481c20 | 2007-05-13 16:03:27 +0200 | [diff] [blame] | 199 | #define tv_isge __tv_isge |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 200 | REGPRM2 static inline int __tv_isge(const struct timeval *tv1, const struct timeval *tv2) |
| 201 | { |
Willy Tarreau | 0481c20 | 2007-05-13 16:03:27 +0200 | [diff] [blame] | 202 | return |
| 203 | ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) ? |
| 204 | ((unsigned)tv1->tv_usec >= (unsigned)tv2->tv_usec) : |
| 205 | ((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec); |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | /* tv_islt: compares <tv1> and <tv2> : returns 1 if tv1 < tv2, otherwise 0 */ |
Willy Tarreau | 0481c20 | 2007-05-13 16:03:27 +0200 | [diff] [blame] | 209 | #define tv_islt __tv_islt |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 210 | REGPRM2 static inline int __tv_islt(const struct timeval *tv1, const struct timeval *tv2) |
| 211 | { |
Willy Tarreau | 0481c20 | 2007-05-13 16:03:27 +0200 | [diff] [blame] | 212 | return |
| 213 | ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) ? |
| 214 | ((unsigned)tv1->tv_usec < (unsigned)tv2->tv_usec) : |
| 215 | ((unsigned)tv1->tv_sec < (unsigned)tv2->tv_sec); |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | /* tv_isle: compares <tv1> and <tv2> : returns 1 if tv1 <= tv2, otherwise 0 */ |
Willy Tarreau | 0481c20 | 2007-05-13 16:03:27 +0200 | [diff] [blame] | 219 | #define tv_isle _tv_isle |
| 220 | REGPRM2 int _tv_isle(const struct timeval *tv1, const struct timeval *tv2); |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 221 | REGPRM2 static inline int __tv_isle(const struct timeval *tv1, const struct timeval *tv2) |
| 222 | { |
Willy Tarreau | 0481c20 | 2007-05-13 16:03:27 +0200 | [diff] [blame] | 223 | return |
| 224 | ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) ? |
| 225 | ((unsigned)tv1->tv_usec <= (unsigned)tv2->tv_usec) : |
| 226 | ((unsigned)tv1->tv_sec < (unsigned)tv2->tv_sec); |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 227 | } |
| 228 | |
Willy Tarreau | 5e8f066 | 2007-02-12 00:59:08 +0100 | [diff] [blame] | 229 | /* |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 230 | * compares <tv1> and <tv2> modulo 1ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2 |
| 231 | * Must not be used when either argument is eternity. Use tv_ms_cmp2() for that. |
Willy Tarreau | 5e8f066 | 2007-02-12 00:59:08 +0100 | [diff] [blame] | 232 | */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 233 | #define tv_ms_cmp _tv_ms_cmp |
| 234 | REGPRM2 int _tv_ms_cmp(const struct timeval *tv1, const struct timeval *tv2); |
| 235 | REGPRM2 static inline int __tv_ms_cmp(const struct timeval *tv1, const struct timeval *tv2) |
Willy Tarreau | 5e8f066 | 2007-02-12 00:59:08 +0100 | [diff] [blame] | 236 | { |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 237 | if ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) { |
| 238 | if ((unsigned)tv2->tv_usec >= (unsigned)tv1->tv_usec + 1000) |
| 239 | return -1; |
| 240 | else if ((unsigned)tv1->tv_usec >= (unsigned)tv2->tv_usec + 1000) |
| 241 | return 1; |
| 242 | else |
| 243 | return 0; |
| 244 | } |
| 245 | else if (((unsigned)tv2->tv_sec > (unsigned)tv1->tv_sec + 1) || |
| 246 | (((unsigned)tv2->tv_sec == (unsigned)tv1->tv_sec + 1) && |
| 247 | ((unsigned)tv2->tv_usec + 1000000 >= (unsigned)tv1->tv_usec + 1000))) |
| 248 | return -1; |
| 249 | else if (((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec + 1) || |
| 250 | (((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec + 1) && |
| 251 | ((unsigned)tv1->tv_usec + 1000000 >= (unsigned)tv2->tv_usec + 1000))) |
Willy Tarreau | 5e8f066 | 2007-02-12 00:59:08 +0100 | [diff] [blame] | 252 | return 1; |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 253 | else |
Willy Tarreau | 5e8f066 | 2007-02-12 00:59:08 +0100 | [diff] [blame] | 254 | return 0; |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | /* |
| 258 | * compares <tv1> and <tv2> modulo 1 ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2, |
| 259 | * assuming that TV_ETERNITY is greater than everything. |
| 260 | */ |
| 261 | #define tv_ms_cmp2 _tv_ms_cmp2 |
| 262 | REGPRM2 int _tv_ms_cmp2(const struct timeval *tv1, const struct timeval *tv2); |
| 263 | REGPRM2 static inline int __tv_ms_cmp2(const struct timeval *tv1, const struct timeval *tv2) |
| 264 | { |
| 265 | if (tv_iseternity(tv1)) |
| 266 | if (tv_iseternity(tv2)) |
| 267 | return 0; /* same */ |
| 268 | else |
| 269 | return 1; /* tv1 later than tv2 */ |
| 270 | else if (tv_iseternity(tv2)) |
| 271 | return -1; /* tv2 later than tv1 */ |
| 272 | return tv_ms_cmp(tv1, tv2); |
| 273 | } |
| 274 | |
| 275 | /* |
| 276 | * compares <tv1> and <tv2> modulo 1 ms: returns 1 if tv1 <= tv2, 0 if tv1 > tv2, |
| 277 | * assuming that TV_ETERNITY is greater than everything. Returns 0 if tv1 is |
| 278 | * TV_ETERNITY, and always assumes that tv2 != TV_ETERNITY. Designed to replace |
| 279 | * occurrences of (tv_ms_cmp2(tv,now) <= 0). |
| 280 | */ |
| 281 | #define tv_ms_le2 _tv_ms_le2 |
| 282 | REGPRM2 int _tv_ms_le2(const struct timeval *tv1, const struct timeval *tv2); |
| 283 | REGPRM2 static inline int __tv_ms_le2(const struct timeval *tv1, const struct timeval *tv2) |
| 284 | { |
| 285 | if (likely((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec + 1)) |
| 286 | return 0; |
| 287 | |
| 288 | if (likely((unsigned)tv1->tv_sec < (unsigned)tv2->tv_sec)) |
| 289 | return 1; |
| 290 | |
| 291 | if (likely((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec)) { |
| 292 | if ((unsigned)tv2->tv_usec >= (unsigned)tv1->tv_usec + 1000) |
| 293 | return 1; |
| 294 | else |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | if (unlikely(((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec + 1) && |
| 299 | ((unsigned)tv1->tv_usec + 1000000 >= (unsigned)tv2->tv_usec + 1000))) |
| 300 | return 0; |
| 301 | else |
Willy Tarreau | 5e8f066 | 2007-02-12 00:59:08 +0100 | [diff] [blame] | 302 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 303 | } |
| 304 | |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 305 | |
| 306 | /**** operators **********************************************************/ |
| 307 | |
| 308 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 309 | /* |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 310 | * Returns the time in ms elapsed between tv1 and tv2, assuming that tv1<=tv2. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 311 | * Must not be used when either argument is eternity. |
| 312 | */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 313 | #define tv_ms_elapsed __tv_ms_elapsed |
| 314 | REGPRM2 unsigned long _tv_ms_elapsed(const struct timeval *tv1, const struct timeval *tv2); |
| 315 | REGPRM2 static inline unsigned long __tv_ms_elapsed(const struct timeval *tv1, const struct timeval *tv2) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 316 | { |
| 317 | unsigned long ret; |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 318 | |
| 319 | ret = ((signed long)(tv2->tv_sec - tv1->tv_sec)) * 1000; |
| 320 | ret += ((signed long)(tv2->tv_usec - tv1->tv_usec)) / 1000; |
| 321 | return ret; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | /* |
| 325 | * returns the remaining time between tv1=now and event=tv2 |
| 326 | * if tv2 is passed, 0 is returned. |
| 327 | * Must not be used when either argument is eternity. |
| 328 | */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 329 | |
| 330 | #define tv_ms_remain __tv_ms_remain |
| 331 | REGPRM2 unsigned long _tv_ms_remain(const struct timeval *tv1, const struct timeval *tv2); |
| 332 | REGPRM2 static inline unsigned long __tv_ms_remain(const struct timeval *tv1, const struct timeval *tv2) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 333 | { |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 334 | if (tv_ms_cmp(tv1, tv2) >= 0) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 335 | return 0; /* event elapsed */ |
| 336 | |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 337 | return __tv_ms_elapsed(tv1, tv2); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 338 | } |
| 339 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 340 | /* |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 341 | * returns the remaining time between tv1=now and event=tv2 |
| 342 | * if tv2 is passed, 0 is returned. |
| 343 | * Returns TIME_ETERNITY if tv2 is eternity. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 344 | */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 345 | #define tv_ms_remain2 _tv_ms_remain2 |
| 346 | REGPRM2 unsigned long _tv_ms_remain2(const struct timeval *tv1, const struct timeval *tv2); |
| 347 | REGPRM2 static inline unsigned long __tv_ms_remain2(const struct timeval *tv1, const struct timeval *tv2) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 348 | { |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 349 | if (tv_iseternity(tv2)) |
| 350 | return TIME_ETERNITY; |
| 351 | |
| 352 | return tv_ms_remain(tv1, tv2); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 353 | } |
| 354 | |
Willy Tarreau | 49fa3a1 | 2007-05-12 22:29:44 +0200 | [diff] [blame] | 355 | /* |
| 356 | * adds <inc> to <from>, set the result to <tv> and returns a pointer <tv> |
| 357 | */ |
Willy Tarreau | 0481c20 | 2007-05-13 16:03:27 +0200 | [diff] [blame] | 358 | #define tv_add _tv_add |
Willy Tarreau | 49fa3a1 | 2007-05-12 22:29:44 +0200 | [diff] [blame] | 359 | REGPRM3 struct timeval *_tv_add(struct timeval *tv, const struct timeval *from, const struct timeval *inc); |
| 360 | REGPRM3 static inline struct timeval *__tv_add(struct timeval *tv, const struct timeval *from, const struct timeval *inc) |
| 361 | { |
| 362 | tv->tv_usec = from->tv_usec + inc->tv_usec; |
| 363 | tv->tv_sec = from->tv_sec + inc->tv_sec; |
| 364 | if (tv->tv_usec >= 1000000) { |
| 365 | tv->tv_usec -= 1000000; |
| 366 | tv->tv_sec++; |
| 367 | } |
| 368 | return tv; |
| 369 | } |
| 370 | |
| 371 | |
| 372 | /* |
Willy Tarreau | 0481c20 | 2007-05-13 16:03:27 +0200 | [diff] [blame] | 373 | * If <inc> is set, then add it to <from> and set the result to <tv>, then |
| 374 | * return 1, otherwise return 0. It is meant to be used in if conditions. |
| 375 | */ |
| 376 | #define tv_add_ifset _tv_add_ifset |
| 377 | REGPRM3 int _tv_add_ifset(struct timeval *tv, const struct timeval *from, const struct timeval *inc); |
| 378 | REGPRM3 static inline int __tv_add_ifset(struct timeval *tv, const struct timeval *from, const struct timeval *inc) |
| 379 | { |
| 380 | if (tv_iseternity(inc)) |
| 381 | return 0; |
| 382 | tv->tv_usec = from->tv_usec + inc->tv_usec; |
| 383 | tv->tv_sec = from->tv_sec + inc->tv_sec; |
| 384 | if (tv->tv_usec >= 1000000) { |
| 385 | tv->tv_usec -= 1000000; |
| 386 | tv->tv_sec++; |
| 387 | } |
| 388 | return 1; |
| 389 | } |
| 390 | |
| 391 | /* |
Willy Tarreau | 49fa3a1 | 2007-05-12 22:29:44 +0200 | [diff] [blame] | 392 | * adds <inc> to <tv> and returns a pointer <tv> |
| 393 | */ |
| 394 | REGPRM2 static inline struct timeval *__tv_add2(struct timeval *tv, const struct timeval *inc) |
| 395 | { |
| 396 | tv->tv_usec += inc->tv_usec; |
| 397 | tv->tv_sec += inc->tv_sec; |
| 398 | if (tv->tv_usec >= 1000000) { |
| 399 | tv->tv_usec -= 1000000; |
| 400 | tv->tv_sec++; |
| 401 | } |
| 402 | return tv; |
| 403 | } |
| 404 | |
| 405 | |
| 406 | /* |
| 407 | * Computes the remaining time between tv1=now and event=tv2. if tv2 is passed, |
| 408 | * 0 is returned. The result is stored into tv. |
| 409 | */ |
| 410 | #define tv_remain _tv_remain |
| 411 | REGPRM3 struct timeval *_tv_remain(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv); |
| 412 | REGPRM3 static inline struct timeval *__tv_remain(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv) |
| 413 | { |
| 414 | tv->tv_usec = tv2->tv_usec - tv1->tv_usec; |
| 415 | tv->tv_sec = tv2->tv_sec - tv1->tv_sec; |
| 416 | if ((signed)tv->tv_sec > 0) { |
| 417 | if ((signed)tv->tv_usec < 0) { |
| 418 | tv->tv_usec += 1000000; |
| 419 | tv->tv_sec--; |
| 420 | } |
| 421 | } else if (tv->tv_sec == 0) { |
| 422 | if ((signed)tv->tv_usec < 0) |
| 423 | tv->tv_usec = 0; |
| 424 | } else { |
| 425 | tv->tv_sec = 0; |
| 426 | tv->tv_usec = 0; |
| 427 | } |
| 428 | return tv; |
| 429 | } |
| 430 | |
| 431 | |
| 432 | /* |
| 433 | * Computes the remaining time between tv1=now and event=tv2. if tv2 is passed, |
| 434 | * 0 is returned. The result is stored into tv. Returns ETERNITY if tv2 is |
| 435 | * eternity. |
| 436 | */ |
| 437 | #define tv_remain2 _tv_remain2 |
| 438 | REGPRM3 struct timeval *_tv_remain2(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv); |
| 439 | REGPRM3 static inline struct timeval *__tv_remain2(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv) |
| 440 | { |
| 441 | if (tv_iseternity(tv2)) |
| 442 | return tv_eternity(tv); |
| 443 | return __tv_remain(tv1, tv2, tv); |
| 444 | } |
| 445 | |
| 446 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 447 | /* |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 448 | * adds <ms> ms to <from>, set the result to <tv> and returns a pointer <tv> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 449 | */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 450 | #define tv_ms_add _tv_ms_add |
| 451 | REGPRM3 struct timeval *_tv_ms_add(struct timeval *tv, const struct timeval *from, int ms); |
| 452 | REGPRM3 static inline struct timeval *__tv_ms_add(struct timeval *tv, const struct timeval *from, int ms) |
| 453 | { |
| 454 | tv->tv_usec = from->tv_usec + (ms % 1000) * 1000; |
| 455 | tv->tv_sec = from->tv_sec + (ms / 1000); |
| 456 | while (tv->tv_usec >= 1000000) { |
| 457 | tv->tv_usec -= 1000000; |
| 458 | tv->tv_sec++; |
| 459 | } |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 460 | return tv; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 461 | } |
| 462 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 463 | |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 464 | /* |
| 465 | * compares <tv1> and <tv2> : returns 1 if <tv1> is before <tv2>, otherwise 0. |
| 466 | * This should be very fast because it's used in schedulers. |
| 467 | * It has been optimized to return 1 (so call it in a loop which continues |
| 468 | * as long as tv1<=tv2) |
| 469 | */ |
| 470 | |
| 471 | #define tv_isbefore(tv1, tv2) \ |
| 472 | (unlikely((unsigned)(tv1)->tv_sec < (unsigned)(tv2)->tv_sec) ? 1 : \ |
| 473 | (unlikely((unsigned)(tv1)->tv_sec > (unsigned)(tv2)->tv_sec) ? 0 : \ |
| 474 | unlikely((unsigned)(tv1)->tv_usec < (unsigned)(tv2)->tv_usec))) |
| 475 | |
| 476 | /* |
| 477 | * returns the first event between <tv1> and <tv2> into <tvmin>. |
| 478 | * a zero tv is ignored. <tvmin> is returned. If <tvmin> is known |
| 479 | * to be the same as <tv1> or <tv2>, it is recommended to use |
| 480 | * tv_bound instead. |
| 481 | */ |
| 482 | #define tv_min(tvmin, tv1, tv2) ({ \ |
| 483 | if (tv_isbefore(tv1, tv2)) { \ |
| 484 | *tvmin = *tv1; \ |
| 485 | } \ |
| 486 | else { \ |
| 487 | *tvmin = *tv2; \ |
| 488 | } \ |
| 489 | tvmin; \ |
| 490 | }) |
| 491 | |
| 492 | /* |
| 493 | * returns the first event between <tv1> and <tv2> into <tvmin>. |
| 494 | * a zero tv is ignored. <tvmin> is returned. This function has been |
| 495 | * optimized to be called as tv_min(a,a,b) or tv_min(b,a,b). |
| 496 | */ |
| 497 | #define tv_bound(tv1, tv2) ({ \ |
| 498 | if (tv_isbefore(tv2, tv1)) \ |
| 499 | *tv1 = *tv2; \ |
| 500 | tv1; \ |
| 501 | }) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 502 | |
Willy Tarreau | c8f24f8 | 2007-11-30 18:38:35 +0100 | [diff] [blame] | 503 | char *human_time(int t, short hz_div); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 504 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 505 | #endif /* _COMMON_TIME_H */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 506 | |
| 507 | /* |
| 508 | * Local variables: |
| 509 | * c-indent-level: 8 |
| 510 | * c-basic-offset: 8 |
| 511 | * End: |
| 512 | */ |