blob: 14ca58959d7243c9604bad96742e15f86e3d9ddc [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
Krzysztof Oledzki85130942007-10-22 16:21:10 +020030#define SEC 1
31#define MINUTE (60 * SEC)
32#define HOUR (60 * MINUTE)
33#define DAY (24 * HOUR)
34
Willy Tarreaua6a6a932007-04-28 22:40:08 +020035/* 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 Tarreaub0b37bc2008-06-23 14:00:57 +020047/* 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 Tarreaubaaee002006-06-26 02:48:02 +020052
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 Tarreau75590582009-03-05 14:54:50 +010057extern unsigned int curr_sec_ms; /* millisecond of current second (0..999) */
58extern unsigned int curr_sec_ms_scaled; /* millisecond of current second (0..2^32-1) */
Willy Tarreaue6313a32008-06-29 13:47:25 +020059extern unsigned int now_ms; /* internal date in milliseconds (may wrap) */
Willy Tarreau45a12512011-09-10 16:56:42 +020060extern unsigned int samp_time; /* total elapsed time over current sample */
61extern unsigned int idle_time; /* total idle time over current sample */
62extern unsigned int idle_pct; /* idle to total ratio over last sample (percent) */
Willy Tarreaub7f694f2008-06-22 17:18:02 +020063extern struct timeval now; /* internal date is a monotonic function of real clock */
64extern struct timeval date; /* the real current date */
Willy Tarreaubaaee002006-06-26 02:48:02 +020065extern struct timeval start_date; /* the process's start date */
Willy Tarreau45a12512011-09-10 16:56:42 +020066extern struct timeval before_poll; /* system date before calling poll() */
67extern struct timeval after_poll; /* system date after leaving poll() */
Willy Tarreaubaaee002006-06-26 02:48:02 +020068
69
Willy Tarreau42aae5c2007-04-29 17:43:56 +020070/**** exported functions *************************************************/
71
72
Willy Tarreaubaaee002006-06-26 02:48:02 +020073/*
74 * adds <ms> ms to <from>, set the result to <tv> and returns a pointer <tv>
75 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +020076REGPRM3 struct timeval *tv_ms_add(struct timeval *tv, const struct timeval *from, int ms);
Willy Tarreaubaaee002006-06-26 02:48:02 +020077
78/*
79 * 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 +020080 * Must not be used when either argument is eternity. Use tv_ms_cmp2() for that.
Willy Tarreaubaaee002006-06-26 02:48:02 +020081 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +020082REGPRM2 int tv_ms_cmp(const struct timeval *tv1, const struct timeval *tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +020083
84/*
Willy Tarreau8d7d1492007-04-29 10:50:43 +020085 * compares <tv1> and <tv2> modulo 1 ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2,
86 * assuming that TV_ETERNITY is greater than everything.
Willy Tarreaubaaee002006-06-26 02:48:02 +020087 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +020088REGPRM2 int tv_ms_cmp2(const struct timeval *tv1, const struct timeval *tv2);
Willy Tarreau5e8f0662007-02-12 00:59:08 +010089
Willy Tarreau42aae5c2007-04-29 17:43:56 +020090/**** general purpose functions and macros *******************************/
91
92
93/* tv_now: sets <tv> to the current time */
94REGPRM1 static inline struct timeval *tv_now(struct timeval *tv)
95{
96 gettimeofday(tv, NULL);
97 return tv;
98}
Willy Tarreaubaaee002006-06-26 02:48:02 +020099
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200100/* tv_udpate_date: sets <date> to system time, and sets <now> to something as
101 * close as possible to real time, following a monotonic function. The main
102 * principle consists in detecting backwards and forwards time jumps and adjust
103 * an offset to correct them. This function should be called only once after
104 * each poll. The poll's timeout should be passed in <max_wait>, and the return
105 * value in <interrupted> (a non-zero value means that we have not expired the
106 * timeout).
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200107 */
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200108REGPRM2 void tv_update_date(int max_wait, int interrupted);
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200109
Willy Tarreaubaaee002006-06-26 02:48:02 +0200110/*
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200111 * sets a struct timeval to its highest value so that it can never happen
112 * note that only tv_usec is necessary to detect it since a tv_usec > 999999
113 * is normally not possible.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200114 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200115REGPRM1 static inline struct timeval *tv_eternity(struct timeval *tv)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200116{
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200117 tv->tv_sec = tv->tv_usec = TV_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200118 return tv;
119}
120
121/*
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200122 * sets a struct timeval to 0
123 *
Willy Tarreaubaaee002006-06-26 02:48:02 +0200124 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200125REGPRM1 static inline struct timeval *tv_zero(struct timeval *tv) {
126 tv->tv_sec = tv->tv_usec = 0;
127 return tv;
128}
129
130/*
131 * returns non null if tv is [eternity], otherwise 0.
132 */
133#define tv_iseternity(tv) ((tv)->tv_usec == TV_ETERNITY)
134
135/*
Willy Tarreau49fa3a12007-05-12 22:29:44 +0200136 * returns 0 if tv is [eternity], otherwise non-zero.
137 */
138#define tv_isset(tv) ((tv)->tv_usec != TV_ETERNITY)
139
140/*
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200141 * returns non null if tv is [0], otherwise 0.
142 */
143#define tv_iszero(tv) (((tv)->tv_sec | (tv)->tv_usec) == 0)
144
Willy Tarreau49fa3a12007-05-12 22:29:44 +0200145/*
146 * Converts a struct timeval to a number of milliseconds.
147 */
148REGPRM1 static inline unsigned long __tv_to_ms(const struct timeval *tv)
149{
150 unsigned long ret;
151
152 ret = tv->tv_sec * 1000;
153 ret += tv->tv_usec / 1000;
154 return ret;
155}
156
157/*
158 * Converts a struct timeval to a number of milliseconds.
159 */
160REGPRM2 static inline struct timeval * __tv_from_ms(struct timeval *tv, unsigned long ms)
161{
162 tv->tv_sec = ms / 1000;
163 tv->tv_usec = (ms % 1000) * 1000;
164 return tv;
165}
166
Willy Tarreau776cd872009-03-05 00:34:01 +0100167/* Return a number of 1024Hz ticks between 0 and 1023 for input number of
168 * usecs between 0 and 999999. This function has been optimized to remove
169 * any divide and multiply, as it is completely optimized away by the compiler
170 * on CPUs which don't have a fast multiply. Its avg error rate is 305 ppm,
171 * which is almost twice as low as a direct usec to ms conversion. This version
172 * also has the benefit of returning 1024 for 1000000.
173 */
174REGPRM1 static inline unsigned int __usec_to_1024th(unsigned int usec)
175{
176 return (usec * 1073 + 742516) >> 20;
177}
178
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200179
Willy Tarreau49fa3a12007-05-12 22:29:44 +0200180/**** comparison functions and macros ***********************************/
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200181
182
183/* tv_cmp: compares <tv1> and <tv2> : returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2. */
184REGPRM2 static inline int __tv_cmp(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200185{
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200186 if ((unsigned)tv1->tv_sec < (unsigned)tv2->tv_sec)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200187 return -1;
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200188 else if ((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200189 return 1;
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200190 else if ((unsigned)tv1->tv_usec < (unsigned)tv2->tv_usec)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200191 return -1;
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200192 else if ((unsigned)tv1->tv_usec > (unsigned)tv2->tv_usec)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200193 return 1;
194 else
195 return 0;
Willy Tarreau5e8f0662007-02-12 00:59:08 +0100196}
197
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200198/* tv_iseq: compares <tv1> and <tv2> : returns 1 if tv1 == tv2, otherwise 0 */
Willy Tarreau0481c202007-05-13 16:03:27 +0200199#define tv_iseq __tv_iseq
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200200REGPRM2 static inline int __tv_iseq(const struct timeval *tv1, const struct timeval *tv2)
201{
Willy Tarreau0481c202007-05-13 16:03:27 +0200202 return ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) &&
203 ((unsigned)tv1->tv_usec == (unsigned)tv2->tv_usec);
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200204}
205
206/* tv_isgt: compares <tv1> and <tv2> : returns 1 if tv1 > tv2, otherwise 0 */
Willy Tarreau0481c202007-05-13 16:03:27 +0200207#define tv_isgt _tv_isgt
208REGPRM2 int _tv_isgt(const struct timeval *tv1, const struct timeval *tv2);
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200209REGPRM2 static inline int __tv_isgt(const struct timeval *tv1, const struct timeval *tv2)
210{
Willy Tarreau0481c202007-05-13 16:03:27 +0200211 return
212 ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) ?
213 ((unsigned)tv1->tv_usec > (unsigned)tv2->tv_usec) :
214 ((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec);
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200215}
216
217/* tv_isge: compares <tv1> and <tv2> : returns 1 if tv1 >= tv2, otherwise 0 */
Willy Tarreau0481c202007-05-13 16:03:27 +0200218#define tv_isge __tv_isge
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200219REGPRM2 static inline int __tv_isge(const struct timeval *tv1, const struct timeval *tv2)
220{
Willy Tarreau0481c202007-05-13 16:03:27 +0200221 return
222 ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) ?
223 ((unsigned)tv1->tv_usec >= (unsigned)tv2->tv_usec) :
224 ((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec);
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200225}
226
227/* tv_islt: compares <tv1> and <tv2> : returns 1 if tv1 < tv2, otherwise 0 */
Willy Tarreau0481c202007-05-13 16:03:27 +0200228#define tv_islt __tv_islt
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200229REGPRM2 static inline int __tv_islt(const struct timeval *tv1, const struct timeval *tv2)
230{
Willy Tarreau0481c202007-05-13 16:03:27 +0200231 return
232 ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) ?
233 ((unsigned)tv1->tv_usec < (unsigned)tv2->tv_usec) :
234 ((unsigned)tv1->tv_sec < (unsigned)tv2->tv_sec);
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200235}
236
237/* tv_isle: compares <tv1> and <tv2> : returns 1 if tv1 <= tv2, otherwise 0 */
Willy Tarreau0481c202007-05-13 16:03:27 +0200238#define tv_isle _tv_isle
239REGPRM2 int _tv_isle(const struct timeval *tv1, const struct timeval *tv2);
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200240REGPRM2 static inline int __tv_isle(const struct timeval *tv1, const struct timeval *tv2)
241{
Willy Tarreau0481c202007-05-13 16:03:27 +0200242 return
243 ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) ?
244 ((unsigned)tv1->tv_usec <= (unsigned)tv2->tv_usec) :
245 ((unsigned)tv1->tv_sec < (unsigned)tv2->tv_sec);
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200246}
247
Willy Tarreau5e8f0662007-02-12 00:59:08 +0100248/*
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200249 * compares <tv1> and <tv2> modulo 1ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2
250 * Must not be used when either argument is eternity. Use tv_ms_cmp2() for that.
Willy Tarreau5e8f0662007-02-12 00:59:08 +0100251 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200252#define tv_ms_cmp _tv_ms_cmp
253REGPRM2 int _tv_ms_cmp(const struct timeval *tv1, const struct timeval *tv2);
254REGPRM2 static inline int __tv_ms_cmp(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreau5e8f0662007-02-12 00:59:08 +0100255{
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200256 if ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) {
257 if ((unsigned)tv2->tv_usec >= (unsigned)tv1->tv_usec + 1000)
258 return -1;
259 else if ((unsigned)tv1->tv_usec >= (unsigned)tv2->tv_usec + 1000)
260 return 1;
261 else
262 return 0;
263 }
264 else if (((unsigned)tv2->tv_sec > (unsigned)tv1->tv_sec + 1) ||
265 (((unsigned)tv2->tv_sec == (unsigned)tv1->tv_sec + 1) &&
266 ((unsigned)tv2->tv_usec + 1000000 >= (unsigned)tv1->tv_usec + 1000)))
267 return -1;
268 else if (((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec + 1) ||
269 (((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec + 1) &&
270 ((unsigned)tv1->tv_usec + 1000000 >= (unsigned)tv2->tv_usec + 1000)))
Willy Tarreau5e8f0662007-02-12 00:59:08 +0100271 return 1;
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200272 else
Willy Tarreau5e8f0662007-02-12 00:59:08 +0100273 return 0;
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200274}
275
276/*
277 * compares <tv1> and <tv2> modulo 1 ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2,
278 * assuming that TV_ETERNITY is greater than everything.
279 */
280#define tv_ms_cmp2 _tv_ms_cmp2
281REGPRM2 int _tv_ms_cmp2(const struct timeval *tv1, const struct timeval *tv2);
282REGPRM2 static inline int __tv_ms_cmp2(const struct timeval *tv1, const struct timeval *tv2)
283{
284 if (tv_iseternity(tv1))
285 if (tv_iseternity(tv2))
286 return 0; /* same */
287 else
288 return 1; /* tv1 later than tv2 */
289 else if (tv_iseternity(tv2))
290 return -1; /* tv2 later than tv1 */
291 return tv_ms_cmp(tv1, tv2);
292}
293
294/*
295 * compares <tv1> and <tv2> modulo 1 ms: returns 1 if tv1 <= tv2, 0 if tv1 > tv2,
296 * assuming that TV_ETERNITY is greater than everything. Returns 0 if tv1 is
297 * TV_ETERNITY, and always assumes that tv2 != TV_ETERNITY. Designed to replace
298 * occurrences of (tv_ms_cmp2(tv,now) <= 0).
299 */
300#define tv_ms_le2 _tv_ms_le2
301REGPRM2 int _tv_ms_le2(const struct timeval *tv1, const struct timeval *tv2);
302REGPRM2 static inline int __tv_ms_le2(const struct timeval *tv1, const struct timeval *tv2)
303{
304 if (likely((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec + 1))
305 return 0;
306
307 if (likely((unsigned)tv1->tv_sec < (unsigned)tv2->tv_sec))
308 return 1;
309
310 if (likely((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec)) {
311 if ((unsigned)tv2->tv_usec >= (unsigned)tv1->tv_usec + 1000)
312 return 1;
313 else
314 return 0;
315 }
316
317 if (unlikely(((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec + 1) &&
318 ((unsigned)tv1->tv_usec + 1000000 >= (unsigned)tv2->tv_usec + 1000)))
319 return 0;
320 else
Willy Tarreau5e8f0662007-02-12 00:59:08 +0100321 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200322}
323
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200324
325/**** operators **********************************************************/
326
327
Willy Tarreaubaaee002006-06-26 02:48:02 +0200328/*
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200329 * Returns the time in ms elapsed between tv1 and tv2, assuming that tv1<=tv2.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200330 * Must not be used when either argument is eternity.
331 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200332#define tv_ms_elapsed __tv_ms_elapsed
333REGPRM2 unsigned long _tv_ms_elapsed(const struct timeval *tv1, const struct timeval *tv2);
334REGPRM2 static inline unsigned long __tv_ms_elapsed(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200335{
336 unsigned long ret;
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200337
338 ret = ((signed long)(tv2->tv_sec - tv1->tv_sec)) * 1000;
339 ret += ((signed long)(tv2->tv_usec - tv1->tv_usec)) / 1000;
340 return ret;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200341}
342
343/*
344 * returns the remaining time between tv1=now and event=tv2
345 * if tv2 is passed, 0 is returned.
346 * Must not be used when either argument is eternity.
347 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200348
349#define tv_ms_remain __tv_ms_remain
350REGPRM2 unsigned long _tv_ms_remain(const struct timeval *tv1, const struct timeval *tv2);
351REGPRM2 static inline unsigned long __tv_ms_remain(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200352{
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200353 if (tv_ms_cmp(tv1, tv2) >= 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200354 return 0; /* event elapsed */
355
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200356 return __tv_ms_elapsed(tv1, tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200357}
358
Willy Tarreaubaaee002006-06-26 02:48:02 +0200359/*
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200360 * returns the remaining time between tv1=now and event=tv2
361 * if tv2 is passed, 0 is returned.
362 * Returns TIME_ETERNITY if tv2 is eternity.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200363 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200364#define tv_ms_remain2 _tv_ms_remain2
365REGPRM2 unsigned long _tv_ms_remain2(const struct timeval *tv1, const struct timeval *tv2);
366REGPRM2 static inline unsigned long __tv_ms_remain2(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200367{
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200368 if (tv_iseternity(tv2))
369 return TIME_ETERNITY;
370
371 return tv_ms_remain(tv1, tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200372}
373
Willy Tarreau49fa3a12007-05-12 22:29:44 +0200374/*
375 * adds <inc> to <from>, set the result to <tv> and returns a pointer <tv>
376 */
Willy Tarreau0481c202007-05-13 16:03:27 +0200377#define tv_add _tv_add
Willy Tarreau49fa3a12007-05-12 22:29:44 +0200378REGPRM3 struct timeval *_tv_add(struct timeval *tv, const struct timeval *from, const struct timeval *inc);
379REGPRM3 static inline struct timeval *__tv_add(struct timeval *tv, const struct timeval *from, const struct timeval *inc)
380{
381 tv->tv_usec = from->tv_usec + inc->tv_usec;
382 tv->tv_sec = from->tv_sec + inc->tv_sec;
383 if (tv->tv_usec >= 1000000) {
384 tv->tv_usec -= 1000000;
385 tv->tv_sec++;
386 }
387 return tv;
388}
389
390
391/*
Willy Tarreau0481c202007-05-13 16:03:27 +0200392 * If <inc> is set, then add it to <from> and set the result to <tv>, then
393 * return 1, otherwise return 0. It is meant to be used in if conditions.
394 */
395#define tv_add_ifset _tv_add_ifset
396REGPRM3 int _tv_add_ifset(struct timeval *tv, const struct timeval *from, const struct timeval *inc);
397REGPRM3 static inline int __tv_add_ifset(struct timeval *tv, const struct timeval *from, const struct timeval *inc)
398{
399 if (tv_iseternity(inc))
400 return 0;
401 tv->tv_usec = from->tv_usec + inc->tv_usec;
402 tv->tv_sec = from->tv_sec + inc->tv_sec;
403 if (tv->tv_usec >= 1000000) {
404 tv->tv_usec -= 1000000;
405 tv->tv_sec++;
406 }
407 return 1;
408}
409
410/*
Willy Tarreau49fa3a12007-05-12 22:29:44 +0200411 * adds <inc> to <tv> and returns a pointer <tv>
412 */
413REGPRM2 static inline struct timeval *__tv_add2(struct timeval *tv, const struct timeval *inc)
414{
415 tv->tv_usec += inc->tv_usec;
416 tv->tv_sec += inc->tv_sec;
417 if (tv->tv_usec >= 1000000) {
418 tv->tv_usec -= 1000000;
419 tv->tv_sec++;
420 }
421 return tv;
422}
423
424
425/*
426 * Computes the remaining time between tv1=now and event=tv2. if tv2 is passed,
427 * 0 is returned. The result is stored into tv.
428 */
429#define tv_remain _tv_remain
430REGPRM3 struct timeval *_tv_remain(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv);
431REGPRM3 static inline struct timeval *__tv_remain(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv)
432{
433 tv->tv_usec = tv2->tv_usec - tv1->tv_usec;
434 tv->tv_sec = tv2->tv_sec - tv1->tv_sec;
435 if ((signed)tv->tv_sec > 0) {
436 if ((signed)tv->tv_usec < 0) {
437 tv->tv_usec += 1000000;
438 tv->tv_sec--;
439 }
440 } else if (tv->tv_sec == 0) {
441 if ((signed)tv->tv_usec < 0)
442 tv->tv_usec = 0;
443 } else {
444 tv->tv_sec = 0;
445 tv->tv_usec = 0;
446 }
447 return tv;
448}
449
450
451/*
452 * Computes the remaining time between tv1=now and event=tv2. if tv2 is passed,
453 * 0 is returned. The result is stored into tv. Returns ETERNITY if tv2 is
454 * eternity.
455 */
456#define tv_remain2 _tv_remain2
457REGPRM3 struct timeval *_tv_remain2(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv);
458REGPRM3 static inline struct timeval *__tv_remain2(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv)
459{
460 if (tv_iseternity(tv2))
461 return tv_eternity(tv);
462 return __tv_remain(tv1, tv2, tv);
463}
464
465
Willy Tarreaubaaee002006-06-26 02:48:02 +0200466/*
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200467 * adds <ms> ms to <from>, set the result to <tv> and returns a pointer <tv>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200468 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200469#define tv_ms_add _tv_ms_add
470REGPRM3 struct timeval *_tv_ms_add(struct timeval *tv, const struct timeval *from, int ms);
471REGPRM3 static inline struct timeval *__tv_ms_add(struct timeval *tv, const struct timeval *from, int ms)
472{
473 tv->tv_usec = from->tv_usec + (ms % 1000) * 1000;
474 tv->tv_sec = from->tv_sec + (ms / 1000);
475 while (tv->tv_usec >= 1000000) {
476 tv->tv_usec -= 1000000;
477 tv->tv_sec++;
478 }
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200479 return tv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200480}
481
Willy Tarreaubaaee002006-06-26 02:48:02 +0200482
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200483/*
484 * compares <tv1> and <tv2> : returns 1 if <tv1> is before <tv2>, otherwise 0.
485 * This should be very fast because it's used in schedulers.
486 * It has been optimized to return 1 (so call it in a loop which continues
487 * as long as tv1<=tv2)
488 */
489
490#define tv_isbefore(tv1, tv2) \
491 (unlikely((unsigned)(tv1)->tv_sec < (unsigned)(tv2)->tv_sec) ? 1 : \
492 (unlikely((unsigned)(tv1)->tv_sec > (unsigned)(tv2)->tv_sec) ? 0 : \
493 unlikely((unsigned)(tv1)->tv_usec < (unsigned)(tv2)->tv_usec)))
494
495/*
496 * returns the first event between <tv1> and <tv2> into <tvmin>.
497 * a zero tv is ignored. <tvmin> is returned. If <tvmin> is known
498 * to be the same as <tv1> or <tv2>, it is recommended to use
499 * tv_bound instead.
500 */
501#define tv_min(tvmin, tv1, tv2) ({ \
502 if (tv_isbefore(tv1, tv2)) { \
503 *tvmin = *tv1; \
504 } \
505 else { \
506 *tvmin = *tv2; \
507 } \
508 tvmin; \
509})
510
511/*
512 * returns the first event between <tv1> and <tv2> into <tvmin>.
513 * a zero tv is ignored. <tvmin> is returned. This function has been
514 * optimized to be called as tv_min(a,a,b) or tv_min(b,a,b).
515 */
516#define tv_bound(tv1, tv2) ({ \
517 if (tv_isbefore(tv2, tv1)) \
518 *tv1 = *tv2; \
519 tv1; \
520})
Willy Tarreaubaaee002006-06-26 02:48:02 +0200521
Willy Tarreauc8f24f82007-11-30 18:38:35 +0100522char *human_time(int t, short hz_div);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200523
Willy Tarreau45a12512011-09-10 16:56:42 +0200524/* Update the idle time value twice a second, to be called after
525 * tv_update_date() when called after poll(). It relies on <before_poll> to be
526 * updated to the system time before calling poll().
527 */
528static inline void measure_idle()
529{
530 /* Let's compute the idle to work ratio. We worked between after_poll
531 * and before_poll, and slept between before_poll and date. The idle_pct
532 * is updated at most twice every second. Note that the current second
533 * rarely changes so we avoid a multiply when not needed.
534 */
535 int delta;
536
537 if ((delta = date.tv_sec - before_poll.tv_sec))
538 delta *= 1000000;
539 idle_time += delta + (date.tv_usec - before_poll.tv_usec);
540
541 if ((delta = date.tv_sec - after_poll.tv_sec))
542 delta *= 1000000;
543 samp_time += delta + (date.tv_usec - after_poll.tv_usec);
544
545 after_poll.tv_sec = date.tv_sec; after_poll.tv_usec = date.tv_usec;
546 if (samp_time < 500000)
547 return;
548
549 idle_pct = (100 * idle_time + samp_time / 2) / samp_time;
550 idle_time = samp_time = 0;
551}
552
Willy Tarreau2dd0d472006-06-29 17:53:05 +0200553#endif /* _COMMON_TIME_H */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200554
555/*
556 * Local variables:
557 * c-indent-level: 8
558 * c-basic-offset: 8
559 * End:
560 */