blob: 5d86ad53ed04199927c1166f2db5433075d890f2 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
Willy Tarreau45a12512011-09-10 16:56:42 +02002 * include/common/time.h
3 * Time calculation functions and macros.
4 *
5 * Copyright (C) 2000-2011 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 */
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
Willy Tarreau2dd0d472006-06-29 17:53:05 +020022#ifndef _COMMON_TIME_H
23#define _COMMON_TIME_H
Willy Tarreaubaaee002006-06-26 02:48:02 +020024
25#include <stdlib.h>
26#include <sys/time.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020027#include <common/config.h>
Willy Tarreau42aae5c2007-04-29 17:43:56 +020028#include <common/standard.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020029
Willy Tarreaua6a6a932007-04-28 22:40:08 +020030/* eternity when exprimed in timeval */
31#ifndef TV_ETERNITY
32#define TV_ETERNITY (~0UL)
33#endif
34
35/* eternity when exprimed in ms */
36#ifndef TV_ETERNITY_MS
37#define TV_ETERNITY_MS (-1)
38#endif
39
40#define TIME_ETERNITY (TV_ETERNITY_MS)
41
Willy Tarreaub0b37bc2008-06-23 14:00:57 +020042/* we want to be able to detect time jumps. Fix the maximum wait time to a low
43 * value so that we know the time has changed if we wait longer.
44 */
45#define MAX_DELAY_MS 1000
46
Willy Tarreaubaaee002006-06-26 02:48:02 +020047
48/* returns the lowest delay amongst <old> and <new>, and respects TIME_ETERNITY */
49#define MINTIME(old, new) (((new)<0)?(old):(((old)<0||(new)<(old))?(new):(old)))
50#define SETNOW(a) (*a=now)
51
Willy Tarreau75590582009-03-05 14:54:50 +010052extern unsigned int curr_sec_ms; /* millisecond of current second (0..999) */
Willy Tarreaueab777c2012-12-29 21:50:07 +010053extern unsigned int ms_left_scaled; /* milliseconds left for current second (0..2^32-1) */
Willy Tarreau75590582009-03-05 14:54:50 +010054extern unsigned int curr_sec_ms_scaled; /* millisecond of current second (0..2^32-1) */
Willy Tarreaue6313a32008-06-29 13:47:25 +020055extern unsigned int now_ms; /* internal date in milliseconds (may wrap) */
Willy Tarreau45a12512011-09-10 16:56:42 +020056extern unsigned int samp_time; /* total elapsed time over current sample */
57extern unsigned int idle_time; /* total idle time over current sample */
58extern unsigned int idle_pct; /* idle to total ratio over last sample (percent) */
Willy Tarreaub7f694f2008-06-22 17:18:02 +020059extern struct timeval now; /* internal date is a monotonic function of real clock */
60extern struct timeval date; /* the real current date */
Willy Tarreaubaaee002006-06-26 02:48:02 +020061extern struct timeval start_date; /* the process's start date */
Willy Tarreau45a12512011-09-10 16:56:42 +020062extern struct timeval before_poll; /* system date before calling poll() */
63extern struct timeval after_poll; /* system date after leaving poll() */
Willy Tarreaubaaee002006-06-26 02:48:02 +020064
65
Willy Tarreau42aae5c2007-04-29 17:43:56 +020066/**** exported functions *************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +020067/*
68 * adds <ms> ms to <from>, set the result to <tv> and returns a pointer <tv>
69 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +020070REGPRM3 struct timeval *tv_ms_add(struct timeval *tv, const struct timeval *from, int ms);
Willy Tarreaubaaee002006-06-26 02:48:02 +020071
72/*
73 * compares <tv1> and <tv2> modulo 1ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2
Willy Tarreau42aae5c2007-04-29 17:43:56 +020074 * Must not be used when either argument is eternity. Use tv_ms_cmp2() for that.
Willy Tarreaubaaee002006-06-26 02:48:02 +020075 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +020076REGPRM2 int tv_ms_cmp(const struct timeval *tv1, const struct timeval *tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +020077
78/*
Willy Tarreau8d7d1492007-04-29 10:50:43 +020079 * compares <tv1> and <tv2> modulo 1 ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2,
80 * assuming that TV_ETERNITY is greater than everything.
Willy Tarreaubaaee002006-06-26 02:48:02 +020081 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +020082REGPRM2 int tv_ms_cmp2(const struct timeval *tv1, const struct timeval *tv2);
Willy Tarreau5e8f0662007-02-12 00:59:08 +010083
Willy Tarreau42aae5c2007-04-29 17:43:56 +020084/**** general purpose functions and macros *******************************/
85
86
87/* tv_now: sets <tv> to the current time */
88REGPRM1 static inline struct timeval *tv_now(struct timeval *tv)
89{
90 gettimeofday(tv, NULL);
91 return tv;
92}
Willy Tarreaubaaee002006-06-26 02:48:02 +020093
Willy Tarreaub0b37bc2008-06-23 14:00:57 +020094/* tv_udpate_date: sets <date> to system time, and sets <now> to something as
95 * close as possible to real time, following a monotonic function. The main
96 * principle consists in detecting backwards and forwards time jumps and adjust
97 * an offset to correct them. This function should be called only once after
98 * each poll. The poll's timeout should be passed in <max_wait>, and the return
99 * value in <interrupted> (a non-zero value means that we have not expired the
100 * timeout).
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200101 */
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200102REGPRM2 void tv_update_date(int max_wait, int interrupted);
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200103
Willy Tarreaubaaee002006-06-26 02:48:02 +0200104/*
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200105 * sets a struct timeval to its highest value so that it can never happen
106 * note that only tv_usec is necessary to detect it since a tv_usec > 999999
107 * is normally not possible.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200108 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200109REGPRM1 static inline struct timeval *tv_eternity(struct timeval *tv)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200110{
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200111 tv->tv_sec = tv->tv_usec = TV_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200112 return tv;
113}
114
115/*
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200116 * sets a struct timeval to 0
117 *
Willy Tarreaubaaee002006-06-26 02:48:02 +0200118 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200119REGPRM1 static inline struct timeval *tv_zero(struct timeval *tv) {
120 tv->tv_sec = tv->tv_usec = 0;
121 return tv;
122}
123
124/*
125 * returns non null if tv is [eternity], otherwise 0.
126 */
127#define tv_iseternity(tv) ((tv)->tv_usec == TV_ETERNITY)
128
129/*
Willy Tarreau49fa3a12007-05-12 22:29:44 +0200130 * returns 0 if tv is [eternity], otherwise non-zero.
131 */
132#define tv_isset(tv) ((tv)->tv_usec != TV_ETERNITY)
133
134/*
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200135 * returns non null if tv is [0], otherwise 0.
136 */
137#define tv_iszero(tv) (((tv)->tv_sec | (tv)->tv_usec) == 0)
138
Willy Tarreau49fa3a12007-05-12 22:29:44 +0200139/*
140 * Converts a struct timeval to a number of milliseconds.
141 */
142REGPRM1 static inline unsigned long __tv_to_ms(const struct timeval *tv)
143{
144 unsigned long ret;
145
146 ret = tv->tv_sec * 1000;
147 ret += tv->tv_usec / 1000;
148 return ret;
149}
150
151/*
152 * Converts a struct timeval to a number of milliseconds.
153 */
154REGPRM2 static inline struct timeval * __tv_from_ms(struct timeval *tv, unsigned long ms)
155{
156 tv->tv_sec = ms / 1000;
157 tv->tv_usec = (ms % 1000) * 1000;
158 return tv;
159}
160
Willy Tarreau776cd872009-03-05 00:34:01 +0100161/* Return a number of 1024Hz ticks between 0 and 1023 for input number of
162 * usecs between 0 and 999999. This function has been optimized to remove
163 * any divide and multiply, as it is completely optimized away by the compiler
164 * on CPUs which don't have a fast multiply. Its avg error rate is 305 ppm,
165 * which is almost twice as low as a direct usec to ms conversion. This version
166 * also has the benefit of returning 1024 for 1000000.
167 */
168REGPRM1 static inline unsigned int __usec_to_1024th(unsigned int usec)
169{
170 return (usec * 1073 + 742516) >> 20;
171}
172
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200173
Willy Tarreau49fa3a12007-05-12 22:29:44 +0200174/**** comparison functions and macros ***********************************/
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200175
176
177/* tv_cmp: compares <tv1> and <tv2> : returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2. */
178REGPRM2 static inline int __tv_cmp(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200179{
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200180 if ((unsigned)tv1->tv_sec < (unsigned)tv2->tv_sec)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200181 return -1;
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200182 else if ((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200183 return 1;
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200184 else if ((unsigned)tv1->tv_usec < (unsigned)tv2->tv_usec)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200185 return -1;
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200186 else if ((unsigned)tv1->tv_usec > (unsigned)tv2->tv_usec)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200187 return 1;
188 else
189 return 0;
Willy Tarreau5e8f0662007-02-12 00:59:08 +0100190}
191
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200192/* tv_iseq: compares <tv1> and <tv2> : returns 1 if tv1 == tv2, otherwise 0 */
Willy Tarreau0481c202007-05-13 16:03:27 +0200193#define tv_iseq __tv_iseq
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200194REGPRM2 static inline int __tv_iseq(const struct timeval *tv1, const struct timeval *tv2)
195{
Willy Tarreau0481c202007-05-13 16:03:27 +0200196 return ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) &&
197 ((unsigned)tv1->tv_usec == (unsigned)tv2->tv_usec);
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200198}
199
200/* tv_isgt: compares <tv1> and <tv2> : returns 1 if tv1 > tv2, otherwise 0 */
Willy Tarreau0481c202007-05-13 16:03:27 +0200201#define tv_isgt _tv_isgt
202REGPRM2 int _tv_isgt(const struct timeval *tv1, const struct timeval *tv2);
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200203REGPRM2 static inline int __tv_isgt(const struct timeval *tv1, const struct timeval *tv2)
204{
Willy Tarreau0481c202007-05-13 16:03:27 +0200205 return
206 ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) ?
207 ((unsigned)tv1->tv_usec > (unsigned)tv2->tv_usec) :
208 ((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec);
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200209}
210
211/* tv_isge: compares <tv1> and <tv2> : returns 1 if tv1 >= tv2, otherwise 0 */
Willy Tarreau0481c202007-05-13 16:03:27 +0200212#define tv_isge __tv_isge
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200213REGPRM2 static inline int __tv_isge(const struct timeval *tv1, const struct timeval *tv2)
214{
Willy Tarreau0481c202007-05-13 16:03:27 +0200215 return
216 ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) ?
217 ((unsigned)tv1->tv_usec >= (unsigned)tv2->tv_usec) :
218 ((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec);
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200219}
220
221/* tv_islt: compares <tv1> and <tv2> : returns 1 if tv1 < tv2, otherwise 0 */
Willy Tarreau0481c202007-05-13 16:03:27 +0200222#define tv_islt __tv_islt
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200223REGPRM2 static inline int __tv_islt(const struct timeval *tv1, const struct timeval *tv2)
224{
Willy Tarreau0481c202007-05-13 16:03:27 +0200225 return
226 ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) ?
227 ((unsigned)tv1->tv_usec < (unsigned)tv2->tv_usec) :
228 ((unsigned)tv1->tv_sec < (unsigned)tv2->tv_sec);
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200229}
230
231/* tv_isle: compares <tv1> and <tv2> : returns 1 if tv1 <= tv2, otherwise 0 */
Willy Tarreau0481c202007-05-13 16:03:27 +0200232#define tv_isle _tv_isle
233REGPRM2 int _tv_isle(const struct timeval *tv1, const struct timeval *tv2);
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200234REGPRM2 static inline int __tv_isle(const struct timeval *tv1, const struct timeval *tv2)
235{
Willy Tarreau0481c202007-05-13 16:03:27 +0200236 return
237 ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) ?
238 ((unsigned)tv1->tv_usec <= (unsigned)tv2->tv_usec) :
239 ((unsigned)tv1->tv_sec < (unsigned)tv2->tv_sec);
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200240}
241
Willy Tarreau5e8f0662007-02-12 00:59:08 +0100242/*
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200243 * compares <tv1> and <tv2> modulo 1ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2
244 * Must not be used when either argument is eternity. Use tv_ms_cmp2() for that.
Willy Tarreau5e8f0662007-02-12 00:59:08 +0100245 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200246#define tv_ms_cmp _tv_ms_cmp
247REGPRM2 int _tv_ms_cmp(const struct timeval *tv1, const struct timeval *tv2);
248REGPRM2 static inline int __tv_ms_cmp(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreau5e8f0662007-02-12 00:59:08 +0100249{
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200250 if ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) {
251 if ((unsigned)tv2->tv_usec >= (unsigned)tv1->tv_usec + 1000)
252 return -1;
253 else if ((unsigned)tv1->tv_usec >= (unsigned)tv2->tv_usec + 1000)
254 return 1;
255 else
256 return 0;
257 }
258 else if (((unsigned)tv2->tv_sec > (unsigned)tv1->tv_sec + 1) ||
259 (((unsigned)tv2->tv_sec == (unsigned)tv1->tv_sec + 1) &&
260 ((unsigned)tv2->tv_usec + 1000000 >= (unsigned)tv1->tv_usec + 1000)))
261 return -1;
262 else if (((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec + 1) ||
263 (((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec + 1) &&
264 ((unsigned)tv1->tv_usec + 1000000 >= (unsigned)tv2->tv_usec + 1000)))
Willy Tarreau5e8f0662007-02-12 00:59:08 +0100265 return 1;
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200266 else
Willy Tarreau5e8f0662007-02-12 00:59:08 +0100267 return 0;
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200268}
269
270/*
271 * compares <tv1> and <tv2> modulo 1 ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2,
272 * assuming that TV_ETERNITY is greater than everything.
273 */
274#define tv_ms_cmp2 _tv_ms_cmp2
275REGPRM2 int _tv_ms_cmp2(const struct timeval *tv1, const struct timeval *tv2);
276REGPRM2 static inline int __tv_ms_cmp2(const struct timeval *tv1, const struct timeval *tv2)
277{
278 if (tv_iseternity(tv1))
279 if (tv_iseternity(tv2))
280 return 0; /* same */
281 else
282 return 1; /* tv1 later than tv2 */
283 else if (tv_iseternity(tv2))
284 return -1; /* tv2 later than tv1 */
285 return tv_ms_cmp(tv1, tv2);
286}
287
288/*
289 * compares <tv1> and <tv2> modulo 1 ms: returns 1 if tv1 <= tv2, 0 if tv1 > tv2,
290 * assuming that TV_ETERNITY is greater than everything. Returns 0 if tv1 is
291 * TV_ETERNITY, and always assumes that tv2 != TV_ETERNITY. Designed to replace
292 * occurrences of (tv_ms_cmp2(tv,now) <= 0).
293 */
294#define tv_ms_le2 _tv_ms_le2
295REGPRM2 int _tv_ms_le2(const struct timeval *tv1, const struct timeval *tv2);
296REGPRM2 static inline int __tv_ms_le2(const struct timeval *tv1, const struct timeval *tv2)
297{
298 if (likely((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec + 1))
299 return 0;
300
301 if (likely((unsigned)tv1->tv_sec < (unsigned)tv2->tv_sec))
302 return 1;
303
304 if (likely((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec)) {
305 if ((unsigned)tv2->tv_usec >= (unsigned)tv1->tv_usec + 1000)
306 return 1;
307 else
308 return 0;
309 }
310
311 if (unlikely(((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec + 1) &&
312 ((unsigned)tv1->tv_usec + 1000000 >= (unsigned)tv2->tv_usec + 1000)))
313 return 0;
314 else
Willy Tarreau5e8f0662007-02-12 00:59:08 +0100315 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200316}
317
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200318
319/**** operators **********************************************************/
320
321
Willy Tarreaubaaee002006-06-26 02:48:02 +0200322/*
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200323 * Returns the time in ms elapsed between tv1 and tv2, assuming that tv1<=tv2.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200324 * Must not be used when either argument is eternity.
325 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200326#define tv_ms_elapsed __tv_ms_elapsed
327REGPRM2 unsigned long _tv_ms_elapsed(const struct timeval *tv1, const struct timeval *tv2);
328REGPRM2 static inline unsigned long __tv_ms_elapsed(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200329{
330 unsigned long ret;
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200331
332 ret = ((signed long)(tv2->tv_sec - tv1->tv_sec)) * 1000;
333 ret += ((signed long)(tv2->tv_usec - tv1->tv_usec)) / 1000;
334 return ret;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200335}
336
337/*
338 * returns the remaining time between tv1=now and event=tv2
339 * if tv2 is passed, 0 is returned.
340 * Must not be used when either argument is eternity.
341 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200342
343#define tv_ms_remain __tv_ms_remain
344REGPRM2 unsigned long _tv_ms_remain(const struct timeval *tv1, const struct timeval *tv2);
345REGPRM2 static inline unsigned long __tv_ms_remain(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200346{
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200347 if (tv_ms_cmp(tv1, tv2) >= 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200348 return 0; /* event elapsed */
349
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200350 return __tv_ms_elapsed(tv1, tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200351}
352
Willy Tarreaubaaee002006-06-26 02:48:02 +0200353/*
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200354 * returns the remaining time between tv1=now and event=tv2
355 * if tv2 is passed, 0 is returned.
356 * Returns TIME_ETERNITY if tv2 is eternity.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200357 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200358#define tv_ms_remain2 _tv_ms_remain2
359REGPRM2 unsigned long _tv_ms_remain2(const struct timeval *tv1, const struct timeval *tv2);
360REGPRM2 static inline unsigned long __tv_ms_remain2(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200361{
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200362 if (tv_iseternity(tv2))
363 return TIME_ETERNITY;
364
365 return tv_ms_remain(tv1, tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200366}
367
Willy Tarreau49fa3a12007-05-12 22:29:44 +0200368/*
369 * adds <inc> to <from>, set the result to <tv> and returns a pointer <tv>
370 */
Willy Tarreau0481c202007-05-13 16:03:27 +0200371#define tv_add _tv_add
Willy Tarreau49fa3a12007-05-12 22:29:44 +0200372REGPRM3 struct timeval *_tv_add(struct timeval *tv, const struct timeval *from, const struct timeval *inc);
373REGPRM3 static inline struct timeval *__tv_add(struct timeval *tv, const struct timeval *from, const struct timeval *inc)
374{
375 tv->tv_usec = from->tv_usec + inc->tv_usec;
376 tv->tv_sec = from->tv_sec + inc->tv_sec;
377 if (tv->tv_usec >= 1000000) {
378 tv->tv_usec -= 1000000;
379 tv->tv_sec++;
380 }
381 return tv;
382}
383
384
385/*
Willy Tarreau0481c202007-05-13 16:03:27 +0200386 * If <inc> is set, then add it to <from> and set the result to <tv>, then
387 * return 1, otherwise return 0. It is meant to be used in if conditions.
388 */
389#define tv_add_ifset _tv_add_ifset
390REGPRM3 int _tv_add_ifset(struct timeval *tv, const struct timeval *from, const struct timeval *inc);
391REGPRM3 static inline int __tv_add_ifset(struct timeval *tv, const struct timeval *from, const struct timeval *inc)
392{
393 if (tv_iseternity(inc))
394 return 0;
395 tv->tv_usec = from->tv_usec + inc->tv_usec;
396 tv->tv_sec = from->tv_sec + inc->tv_sec;
397 if (tv->tv_usec >= 1000000) {
398 tv->tv_usec -= 1000000;
399 tv->tv_sec++;
400 }
401 return 1;
402}
403
404/*
Willy Tarreau49fa3a12007-05-12 22:29:44 +0200405 * adds <inc> to <tv> and returns a pointer <tv>
406 */
407REGPRM2 static inline struct timeval *__tv_add2(struct timeval *tv, const struct timeval *inc)
408{
409 tv->tv_usec += inc->tv_usec;
410 tv->tv_sec += inc->tv_sec;
411 if (tv->tv_usec >= 1000000) {
412 tv->tv_usec -= 1000000;
413 tv->tv_sec++;
414 }
415 return tv;
416}
417
418
419/*
420 * Computes the remaining time between tv1=now and event=tv2. if tv2 is passed,
421 * 0 is returned. The result is stored into tv.
422 */
423#define tv_remain _tv_remain
424REGPRM3 struct timeval *_tv_remain(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv);
425REGPRM3 static inline struct timeval *__tv_remain(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv)
426{
427 tv->tv_usec = tv2->tv_usec - tv1->tv_usec;
428 tv->tv_sec = tv2->tv_sec - tv1->tv_sec;
429 if ((signed)tv->tv_sec > 0) {
430 if ((signed)tv->tv_usec < 0) {
431 tv->tv_usec += 1000000;
432 tv->tv_sec--;
433 }
434 } else if (tv->tv_sec == 0) {
435 if ((signed)tv->tv_usec < 0)
436 tv->tv_usec = 0;
437 } else {
438 tv->tv_sec = 0;
439 tv->tv_usec = 0;
440 }
441 return tv;
442}
443
444
445/*
446 * Computes the remaining time between tv1=now and event=tv2. if tv2 is passed,
447 * 0 is returned. The result is stored into tv. Returns ETERNITY if tv2 is
448 * eternity.
449 */
450#define tv_remain2 _tv_remain2
451REGPRM3 struct timeval *_tv_remain2(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv);
452REGPRM3 static inline struct timeval *__tv_remain2(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv)
453{
454 if (tv_iseternity(tv2))
455 return tv_eternity(tv);
456 return __tv_remain(tv1, tv2, tv);
457}
458
459
Willy Tarreaubaaee002006-06-26 02:48:02 +0200460/*
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200461 * adds <ms> ms to <from>, set the result to <tv> and returns a pointer <tv>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200462 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200463#define tv_ms_add _tv_ms_add
464REGPRM3 struct timeval *_tv_ms_add(struct timeval *tv, const struct timeval *from, int ms);
465REGPRM3 static inline struct timeval *__tv_ms_add(struct timeval *tv, const struct timeval *from, int ms)
466{
467 tv->tv_usec = from->tv_usec + (ms % 1000) * 1000;
468 tv->tv_sec = from->tv_sec + (ms / 1000);
469 while (tv->tv_usec >= 1000000) {
470 tv->tv_usec -= 1000000;
471 tv->tv_sec++;
472 }
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200473 return tv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200474}
475
Willy Tarreaubaaee002006-06-26 02:48:02 +0200476
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200477/*
478 * compares <tv1> and <tv2> : returns 1 if <tv1> is before <tv2>, otherwise 0.
479 * This should be very fast because it's used in schedulers.
480 * It has been optimized to return 1 (so call it in a loop which continues
481 * as long as tv1<=tv2)
482 */
483
484#define tv_isbefore(tv1, tv2) \
485 (unlikely((unsigned)(tv1)->tv_sec < (unsigned)(tv2)->tv_sec) ? 1 : \
486 (unlikely((unsigned)(tv1)->tv_sec > (unsigned)(tv2)->tv_sec) ? 0 : \
487 unlikely((unsigned)(tv1)->tv_usec < (unsigned)(tv2)->tv_usec)))
488
489/*
490 * returns the first event between <tv1> and <tv2> into <tvmin>.
491 * a zero tv is ignored. <tvmin> is returned. If <tvmin> is known
492 * to be the same as <tv1> or <tv2>, it is recommended to use
493 * tv_bound instead.
494 */
495#define tv_min(tvmin, tv1, tv2) ({ \
496 if (tv_isbefore(tv1, tv2)) { \
497 *tvmin = *tv1; \
498 } \
499 else { \
500 *tvmin = *tv2; \
501 } \
502 tvmin; \
503})
504
505/*
506 * returns the first event between <tv1> and <tv2> into <tvmin>.
507 * a zero tv is ignored. <tvmin> is returned. This function has been
508 * optimized to be called as tv_min(a,a,b) or tv_min(b,a,b).
509 */
510#define tv_bound(tv1, tv2) ({ \
511 if (tv_isbefore(tv2, tv1)) \
512 *tv1 = *tv2; \
513 tv1; \
514})
Willy Tarreaubaaee002006-06-26 02:48:02 +0200515
Willy Tarreau45a12512011-09-10 16:56:42 +0200516/* Update the idle time value twice a second, to be called after
517 * tv_update_date() when called after poll(). It relies on <before_poll> to be
518 * updated to the system time before calling poll().
519 */
520static inline void measure_idle()
521{
522 /* Let's compute the idle to work ratio. We worked between after_poll
523 * and before_poll, and slept between before_poll and date. The idle_pct
524 * is updated at most twice every second. Note that the current second
525 * rarely changes so we avoid a multiply when not needed.
526 */
527 int delta;
528
529 if ((delta = date.tv_sec - before_poll.tv_sec))
530 delta *= 1000000;
531 idle_time += delta + (date.tv_usec - before_poll.tv_usec);
532
533 if ((delta = date.tv_sec - after_poll.tv_sec))
534 delta *= 1000000;
535 samp_time += delta + (date.tv_usec - after_poll.tv_usec);
536
537 after_poll.tv_sec = date.tv_sec; after_poll.tv_usec = date.tv_usec;
538 if (samp_time < 500000)
539 return;
540
541 idle_pct = (100 * idle_time + samp_time / 2) / samp_time;
542 idle_time = samp_time = 0;
543}
544
Willy Tarreau2dd0d472006-06-29 17:53:05 +0200545#endif /* _COMMON_TIME_H */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200546
547/*
548 * Local variables:
549 * c-indent-level: 8
550 * c-basic-offset: 8
551 * End:
552 */