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 | 5e8f066 | 2007-02-12 00:59:08 +0100 | [diff] [blame] | 62 | * compares <tv1> and <tv2> : returns 0 if tv1 < tv2, 1 if tv1 >= tv2, |
| 63 | * considering that 0 is the eternity. |
| 64 | */ |
| 65 | REGPRM2 int tv_cmp_ge2(const struct timeval *tv1, const struct timeval *tv2); |
| 66 | |
| 67 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 68 | * compares <tv1> and <tv2> : returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2, |
| 69 | * considering that 0 is the eternity. |
| 70 | */ |
Willy Tarreau | fb27867 | 2006-10-15 15:38:50 +0200 | [diff] [blame] | 71 | REGPRM2 int tv_cmp2(const struct timeval *tv1, const struct timeval *tv2); |
Willy Tarreau | 5e8f066 | 2007-02-12 00:59:08 +0100 | [diff] [blame] | 72 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 73 | /* |
| 74 | * compares <tv1> and <tv2> modulo 1 ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2, |
| 75 | * considering that 0 is the eternity. |
| 76 | */ |
Willy Tarreau | fb27867 | 2006-10-15 15:38:50 +0200 | [diff] [blame] | 77 | REGPRM2 int tv_cmp2_ms(const struct timeval *tv1, const struct timeval *tv2); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 78 | |
| 79 | /* |
| 80 | * returns the remaining time between tv1=now and event=tv2 |
| 81 | * if tv2 is passed, 0 is returned. |
| 82 | * Returns TIME_ETERNITY if tv2 is eternity. |
| 83 | */ |
Willy Tarreau | fb27867 | 2006-10-15 15:38:50 +0200 | [diff] [blame] | 84 | 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] | 85 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 86 | /* sets <tv> to the current time */ |
Willy Tarreau | fb27867 | 2006-10-15 15:38:50 +0200 | [diff] [blame] | 87 | REGPRM1 static inline struct timeval *tv_now(struct timeval *tv) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 88 | { |
| 89 | if (tv) |
| 90 | gettimeofday(tv, NULL); |
| 91 | return tv; |
| 92 | } |
| 93 | |
| 94 | /* |
| 95 | * compares <tv1> and <tv2> : returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2 |
| 96 | * Must not be used when either argument is eternity. Use tv_cmp2() for that. |
| 97 | */ |
Willy Tarreau | fb27867 | 2006-10-15 15:38:50 +0200 | [diff] [blame] | 98 | 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] | 99 | { |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame^] | 100 | if ((unsigned)tv1->tv_sec < (unsigned)tv2->tv_sec) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 101 | return -1; |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame^] | 102 | else if ((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 103 | return 1; |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame^] | 104 | else if ((unsigned)tv1->tv_usec < (unsigned)tv2->tv_usec) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 105 | return -1; |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame^] | 106 | else if ((unsigned)tv1->tv_usec > (unsigned)tv2->tv_usec) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 107 | return 1; |
| 108 | else |
| 109 | return 0; |
Willy Tarreau | 5e8f066 | 2007-02-12 00:59:08 +0100 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /* |
| 113 | * compares <tv1> and <tv2> : returns 0 if tv1 < tv2, 1 if tv1 >= tv2 |
| 114 | */ |
| 115 | REGPRM2 static inline int tv_cmp_ge(const struct timeval *tv1, const struct timeval *tv2) |
| 116 | { |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame^] | 117 | if ((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec) |
Willy Tarreau | 5e8f066 | 2007-02-12 00:59:08 +0100 | [diff] [blame] | 118 | return 1; |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame^] | 119 | if ((unsigned)tv1->tv_sec < (unsigned)tv2->tv_sec) |
Willy Tarreau | 5e8f066 | 2007-02-12 00:59:08 +0100 | [diff] [blame] | 120 | return 0; |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame^] | 121 | if ((unsigned)tv1->tv_usec >= (unsigned)tv2->tv_usec) |
Willy Tarreau | 5e8f066 | 2007-02-12 00:59:08 +0100 | [diff] [blame] | 122 | return 1; |
| 123 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | /* |
| 127 | * returns the difference, in ms, between tv1 and tv2 |
| 128 | * Must not be used when either argument is eternity. |
| 129 | */ |
Willy Tarreau | fb27867 | 2006-10-15 15:38:50 +0200 | [diff] [blame] | 130 | 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] | 131 | { |
| 132 | unsigned long ret; |
| 133 | |
| 134 | ret = (tv2->tv_sec - tv1->tv_sec) * 1000; |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame^] | 135 | if ((unsigned)tv2->tv_usec > (unsigned)tv1->tv_usec) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 136 | ret += (tv2->tv_usec - tv1->tv_usec) / 1000; |
| 137 | else |
| 138 | ret -= (tv1->tv_usec - tv2->tv_usec) / 1000; |
| 139 | return (unsigned long) ret; |
| 140 | } |
| 141 | |
| 142 | /* |
| 143 | * returns the remaining time between tv1=now and event=tv2 |
| 144 | * if tv2 is passed, 0 is returned. |
| 145 | * Must not be used when either argument is eternity. |
| 146 | */ |
Willy Tarreau | fb27867 | 2006-10-15 15:38:50 +0200 | [diff] [blame] | 147 | 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] | 148 | { |
| 149 | unsigned long ret; |
| 150 | |
| 151 | if (tv_cmp_ms(tv1, tv2) >= 0) |
| 152 | return 0; /* event elapsed */ |
| 153 | |
| 154 | ret = (tv2->tv_sec - tv1->tv_sec) * 1000; |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame^] | 155 | if ((unsigned)tv2->tv_usec > (unsigned)tv1->tv_usec) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 156 | ret += (tv2->tv_usec - tv1->tv_usec) / 1000; |
| 157 | else |
| 158 | ret -= (tv1->tv_usec - tv2->tv_usec) / 1000; |
| 159 | return (unsigned long) ret; |
| 160 | } |
| 161 | |
| 162 | |
| 163 | /* |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame^] | 164 | * sets a struct timeval to its highest value so that it can never happen |
| 165 | * note that only tv_usec is necessary to detect it since a tv_usec > 999999 |
| 166 | * is normally not possible. |
| 167 | * |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 168 | */ |
| 169 | |
Willy Tarreau | fb27867 | 2006-10-15 15:38:50 +0200 | [diff] [blame] | 170 | REGPRM1 static inline struct timeval *tv_eternity(struct timeval *tv) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 171 | { |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame^] | 172 | tv->tv_sec = tv->tv_usec = TV_ETERNITY; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 173 | return tv; |
| 174 | } |
| 175 | |
| 176 | /* |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame^] | 177 | * sets a struct timeval to 0 |
| 178 | * |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 179 | */ |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame^] | 180 | REGPRM1 static inline struct timeval *tv_zero(struct timeval *tv) { |
| 181 | tv->tv_sec = tv->tv_usec = 0; |
| 182 | return tv; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | /* |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame^] | 186 | * returns non null if tv is [eternity], otherwise 0. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 187 | */ |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame^] | 188 | #define tv_iseternity(tv) ((tv)->tv_usec == TV_ETERNITY) |
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 | * returns non null if tv is [0], otherwise 0. |
| 192 | */ |
| 193 | #define tv_iszero(tv) (((tv)->tv_sec | (tv)->tv_usec) == 0) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 194 | |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame^] | 195 | /* |
| 196 | * compares <tv1> and <tv2> : returns 1 if <tv1> is before <tv2>, otherwise 0. |
| 197 | * This should be very fast because it's used in schedulers. |
| 198 | * It has been optimized to return 1 (so call it in a loop which continues |
| 199 | * as long as tv1<=tv2) |
| 200 | */ |
| 201 | |
| 202 | #define tv_isbefore(tv1, tv2) \ |
| 203 | (unlikely((unsigned)(tv1)->tv_sec < (unsigned)(tv2)->tv_sec) ? 1 : \ |
| 204 | (unlikely((unsigned)(tv1)->tv_sec > (unsigned)(tv2)->tv_sec) ? 0 : \ |
| 205 | unlikely((unsigned)(tv1)->tv_usec < (unsigned)(tv2)->tv_usec))) |
| 206 | |
| 207 | /* |
| 208 | * returns the first event between <tv1> and <tv2> into <tvmin>. |
| 209 | * a zero tv is ignored. <tvmin> is returned. If <tvmin> is known |
| 210 | * to be the same as <tv1> or <tv2>, it is recommended to use |
| 211 | * tv_bound instead. |
| 212 | */ |
| 213 | #define tv_min(tvmin, tv1, tv2) ({ \ |
| 214 | if (tv_isbefore(tv1, tv2)) { \ |
| 215 | *tvmin = *tv1; \ |
| 216 | } \ |
| 217 | else { \ |
| 218 | *tvmin = *tv2; \ |
| 219 | } \ |
| 220 | tvmin; \ |
| 221 | }) |
| 222 | |
| 223 | /* |
| 224 | * returns the first event between <tv1> and <tv2> into <tvmin>. |
| 225 | * a zero tv is ignored. <tvmin> is returned. This function has been |
| 226 | * optimized to be called as tv_min(a,a,b) or tv_min(b,a,b). |
| 227 | */ |
| 228 | #define tv_bound(tv1, tv2) ({ \ |
| 229 | if (tv_isbefore(tv2, tv1)) \ |
| 230 | *tv1 = *tv2; \ |
| 231 | tv1; \ |
| 232 | }) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 233 | |
| 234 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 235 | #endif /* _COMMON_TIME_H */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 236 | |
| 237 | /* |
| 238 | * Local variables: |
| 239 | * c-indent-level: 8 |
| 240 | * c-basic-offset: 8 |
| 241 | * End: |
| 242 | */ |