blob: d6863d42c89d44a0cbb39e50b223d3dc5131d1f5 [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>
Willy Tarreau5ceeb152018-10-17 18:59:53 +020026#include <unistd.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020027#include <sys/time.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020028#include <haproxy/api.h>
Willy Tarreau3f567e42020-05-28 15:29:19 +020029#include <haproxy/thread.h>
Willy Tarreau42aae5c2007-04-29 17:43:56 +020030#include <common/standard.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020031
Willy Tarreaua6a6a932007-04-28 22:40:08 +020032/* eternity when exprimed in timeval */
33#ifndef TV_ETERNITY
34#define TV_ETERNITY (~0UL)
35#endif
36
37/* eternity when exprimed in ms */
38#ifndef TV_ETERNITY_MS
39#define TV_ETERNITY_MS (-1)
40#endif
41
42#define TIME_ETERNITY (TV_ETERNITY_MS)
43
Willy Tarreaub0b37bc2008-06-23 14:00:57 +020044/* we want to be able to detect time jumps. Fix the maximum wait time to a low
45 * value so that we know the time has changed if we wait longer.
46 */
47#define MAX_DELAY_MS 1000
48
Willy Tarreaubaaee002006-06-26 02:48:02 +020049
50/* returns the lowest delay amongst <old> and <new>, and respects TIME_ETERNITY */
51#define MINTIME(old, new) (((new)<0)?(old):(((old)<0||(new)<(old))?(new):(old)))
52#define SETNOW(a) (*a=now)
53
Christopher Faulet9a655712017-05-11 11:00:15 +020054extern THREAD_LOCAL unsigned int curr_sec_ms; /* millisecond of current second (0..999) */
55extern THREAD_LOCAL unsigned int ms_left_scaled; /* milliseconds left for current second (0..2^32-1) */
56extern THREAD_LOCAL unsigned int curr_sec_ms_scaled; /* millisecond of current second (0..2^32-1) */
57extern THREAD_LOCAL unsigned int now_ms; /* internal date in milliseconds (may wrap) */
58extern THREAD_LOCAL unsigned int samp_time; /* total elapsed time over current sample */
59extern THREAD_LOCAL unsigned int idle_time; /* total idle time over current sample */
Christopher Faulet9a655712017-05-11 11:00:15 +020060extern THREAD_LOCAL struct timeval now; /* internal date is a monotonic function of real clock */
61extern THREAD_LOCAL struct timeval date; /* the real current date */
Willy Tarreaubaaee002006-06-26 02:48:02 +020062extern struct timeval start_date; /* the process's start date */
Christopher Faulet9a655712017-05-11 11:00:15 +020063extern THREAD_LOCAL struct timeval before_poll; /* system date before calling poll() */
64extern THREAD_LOCAL struct timeval after_poll; /* system date after leaving poll() */
Willy Tarreaubaaee002006-06-26 02:48:02 +020065
66
Willy Tarreau42aae5c2007-04-29 17:43:56 +020067/**** exported functions *************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +020068/*
69 * adds <ms> ms to <from>, set the result to <tv> and returns a pointer <tv>
70 */
Willy Tarreau03e78532020-02-25 07:38:05 +010071struct timeval *tv_ms_add(struct timeval *tv, const struct timeval *from, int ms);
Willy Tarreaubaaee002006-06-26 02:48:02 +020072
73/*
74 * 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 +020075 * Must not be used when either argument is eternity. Use tv_ms_cmp2() for that.
Willy Tarreaubaaee002006-06-26 02:48:02 +020076 */
Willy Tarreau03e78532020-02-25 07:38:05 +010077int tv_ms_cmp(const struct timeval *tv1, const struct timeval *tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +020078
79/*
Willy Tarreau8d7d1492007-04-29 10:50:43 +020080 * compares <tv1> and <tv2> modulo 1 ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2,
81 * assuming that TV_ETERNITY is greater than everything.
Willy Tarreaubaaee002006-06-26 02:48:02 +020082 */
Willy Tarreau03e78532020-02-25 07:38:05 +010083int tv_ms_cmp2(const struct timeval *tv1, const struct timeval *tv2);
Willy Tarreau5e8f0662007-02-12 00:59:08 +010084
Willy Tarreau42aae5c2007-04-29 17:43:56 +020085/**** general purpose functions and macros *******************************/
86
87
88/* tv_now: sets <tv> to the current time */
Willy Tarreau03e78532020-02-25 07:38:05 +010089static inline struct timeval *tv_now(struct timeval *tv)
Willy Tarreau42aae5c2007-04-29 17:43:56 +020090{
91 gettimeofday(tv, NULL);
92 return tv;
93}
Willy Tarreaubaaee002006-06-26 02:48:02 +020094
Willy Tarreaub0b37bc2008-06-23 14:00:57 +020095/* tv_udpate_date: sets <date> to system time, and sets <now> to something as
96 * close as possible to real time, following a monotonic function. The main
97 * principle consists in detecting backwards and forwards time jumps and adjust
98 * an offset to correct them. This function should be called only once after
99 * each poll. The poll's timeout should be passed in <max_wait>, and the return
100 * value in <interrupted> (a non-zero value means that we have not expired the
101 * timeout).
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200102 */
Willy Tarreau03e78532020-02-25 07:38:05 +0100103void tv_update_date(int max_wait, int interrupted);
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200104
Willy Tarreau93acfa22019-09-26 08:00:23 +0200105char *timeofday_as_iso_us(int pad);
106
Willy Tarreaubaaee002006-06-26 02:48:02 +0200107/*
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200108 * sets a struct timeval to its highest value so that it can never happen
109 * note that only tv_usec is necessary to detect it since a tv_usec > 999999
110 * is normally not possible.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200111 */
Willy Tarreau03e78532020-02-25 07:38:05 +0100112static inline struct timeval *tv_eternity(struct timeval *tv)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200113{
Willy Tarreau5f3f15f2013-12-13 09:22:23 +0100114 tv->tv_sec = (typeof(tv->tv_sec))TV_ETERNITY;
115 tv->tv_usec = (typeof(tv->tv_usec))TV_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200116 return tv;
117}
118
119/*
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200120 * sets a struct timeval to 0
121 *
Willy Tarreaubaaee002006-06-26 02:48:02 +0200122 */
Willy Tarreau03e78532020-02-25 07:38:05 +0100123static inline struct timeval *tv_zero(struct timeval *tv) {
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200124 tv->tv_sec = tv->tv_usec = 0;
125 return tv;
126}
127
128/*
129 * returns non null if tv is [eternity], otherwise 0.
130 */
Willy Tarreau5f3f15f2013-12-13 09:22:23 +0100131#define tv_iseternity(tv) ((tv)->tv_usec == (typeof((tv)->tv_usec))TV_ETERNITY)
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200132
133/*
Willy Tarreau49fa3a12007-05-12 22:29:44 +0200134 * returns 0 if tv is [eternity], otherwise non-zero.
135 */
Willy Tarreau5f3f15f2013-12-13 09:22:23 +0100136#define tv_isset(tv) ((tv)->tv_usec != (typeof((tv)->tv_usec))TV_ETERNITY)
Willy Tarreau49fa3a12007-05-12 22:29:44 +0200137
138/*
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200139 * returns non null if tv is [0], otherwise 0.
140 */
141#define tv_iszero(tv) (((tv)->tv_sec | (tv)->tv_usec) == 0)
142
Willy Tarreau49fa3a12007-05-12 22:29:44 +0200143/*
144 * Converts a struct timeval to a number of milliseconds.
145 */
Willy Tarreau03e78532020-02-25 07:38:05 +0100146static inline unsigned long __tv_to_ms(const struct timeval *tv)
Willy Tarreau49fa3a12007-05-12 22:29:44 +0200147{
148 unsigned long ret;
149
150 ret = tv->tv_sec * 1000;
151 ret += tv->tv_usec / 1000;
152 return ret;
153}
154
155/*
156 * Converts a struct timeval to a number of milliseconds.
157 */
Willy Tarreau03e78532020-02-25 07:38:05 +0100158static inline struct timeval * __tv_from_ms(struct timeval *tv, unsigned long ms)
Willy Tarreau49fa3a12007-05-12 22:29:44 +0200159{
160 tv->tv_sec = ms / 1000;
161 tv->tv_usec = (ms % 1000) * 1000;
162 return tv;
163}
164
Willy Tarreau776cd872009-03-05 00:34:01 +0100165/* Return a number of 1024Hz ticks between 0 and 1023 for input number of
166 * usecs between 0 and 999999. This function has been optimized to remove
167 * any divide and multiply, as it is completely optimized away by the compiler
168 * on CPUs which don't have a fast multiply. Its avg error rate is 305 ppm,
169 * which is almost twice as low as a direct usec to ms conversion. This version
170 * also has the benefit of returning 1024 for 1000000.
171 */
Willy Tarreau03e78532020-02-25 07:38:05 +0100172static inline unsigned int __usec_to_1024th(unsigned int usec)
Willy Tarreau776cd872009-03-05 00:34:01 +0100173{
174 return (usec * 1073 + 742516) >> 20;
175}
176
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200177
Willy Tarreau49fa3a12007-05-12 22:29:44 +0200178/**** comparison functions and macros ***********************************/
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200179
180
181/* tv_cmp: compares <tv1> and <tv2> : returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2. */
Willy Tarreau03e78532020-02-25 07:38:05 +0100182static inline int __tv_cmp(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200183{
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200184 if ((unsigned)tv1->tv_sec < (unsigned)tv2->tv_sec)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200185 return -1;
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200186 else 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_usec < (unsigned)tv2->tv_usec)
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;
192 else
193 return 0;
Willy Tarreau5e8f0662007-02-12 00:59:08 +0100194}
195
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200196/* tv_iseq: compares <tv1> and <tv2> : returns 1 if tv1 == tv2, otherwise 0 */
Willy Tarreau0481c202007-05-13 16:03:27 +0200197#define tv_iseq __tv_iseq
Willy Tarreau03e78532020-02-25 07:38:05 +0100198static inline int __tv_iseq(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200199{
Willy Tarreau0481c202007-05-13 16:03:27 +0200200 return ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) &&
201 ((unsigned)tv1->tv_usec == (unsigned)tv2->tv_usec);
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200202}
203
204/* tv_isgt: compares <tv1> and <tv2> : returns 1 if tv1 > tv2, otherwise 0 */
Willy Tarreau0481c202007-05-13 16:03:27 +0200205#define tv_isgt _tv_isgt
Willy Tarreau03e78532020-02-25 07:38:05 +0100206int _tv_isgt(const struct timeval *tv1, const struct timeval *tv2);
207static inline int __tv_isgt(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200208{
Willy Tarreau0481c202007-05-13 16:03:27 +0200209 return
210 ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) ?
211 ((unsigned)tv1->tv_usec > (unsigned)tv2->tv_usec) :
212 ((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec);
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200213}
214
215/* tv_isge: compares <tv1> and <tv2> : returns 1 if tv1 >= tv2, otherwise 0 */
Willy Tarreau0481c202007-05-13 16:03:27 +0200216#define tv_isge __tv_isge
Willy Tarreau03e78532020-02-25 07:38:05 +0100217static inline int __tv_isge(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200218{
Willy Tarreau0481c202007-05-13 16:03:27 +0200219 return
220 ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) ?
221 ((unsigned)tv1->tv_usec >= (unsigned)tv2->tv_usec) :
222 ((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec);
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200223}
224
225/* tv_islt: compares <tv1> and <tv2> : returns 1 if tv1 < tv2, otherwise 0 */
Willy Tarreau0481c202007-05-13 16:03:27 +0200226#define tv_islt __tv_islt
Willy Tarreau03e78532020-02-25 07:38:05 +0100227static inline int __tv_islt(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200228{
Willy Tarreau0481c202007-05-13 16:03:27 +0200229 return
230 ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) ?
231 ((unsigned)tv1->tv_usec < (unsigned)tv2->tv_usec) :
232 ((unsigned)tv1->tv_sec < (unsigned)tv2->tv_sec);
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200233}
234
235/* tv_isle: compares <tv1> and <tv2> : returns 1 if tv1 <= tv2, otherwise 0 */
Willy Tarreau0481c202007-05-13 16:03:27 +0200236#define tv_isle _tv_isle
Willy Tarreau03e78532020-02-25 07:38:05 +0100237int _tv_isle(const struct timeval *tv1, const struct timeval *tv2);
238static inline int __tv_isle(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200239{
Willy Tarreau0481c202007-05-13 16:03:27 +0200240 return
241 ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) ?
242 ((unsigned)tv1->tv_usec <= (unsigned)tv2->tv_usec) :
243 ((unsigned)tv1->tv_sec < (unsigned)tv2->tv_sec);
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200244}
245
Willy Tarreau5e8f0662007-02-12 00:59:08 +0100246/*
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200247 * compares <tv1> and <tv2> modulo 1ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2
248 * Must not be used when either argument is eternity. Use tv_ms_cmp2() for that.
Willy Tarreau5e8f0662007-02-12 00:59:08 +0100249 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200250#define tv_ms_cmp _tv_ms_cmp
Willy Tarreau03e78532020-02-25 07:38:05 +0100251int _tv_ms_cmp(const struct timeval *tv1, const struct timeval *tv2);
252static inline int __tv_ms_cmp(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreau5e8f0662007-02-12 00:59:08 +0100253{
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200254 if ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) {
255 if ((unsigned)tv2->tv_usec >= (unsigned)tv1->tv_usec + 1000)
256 return -1;
257 else if ((unsigned)tv1->tv_usec >= (unsigned)tv2->tv_usec + 1000)
258 return 1;
259 else
260 return 0;
261 }
262 else if (((unsigned)tv2->tv_sec > (unsigned)tv1->tv_sec + 1) ||
263 (((unsigned)tv2->tv_sec == (unsigned)tv1->tv_sec + 1) &&
264 ((unsigned)tv2->tv_usec + 1000000 >= (unsigned)tv1->tv_usec + 1000)))
265 return -1;
266 else if (((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec + 1) ||
267 (((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec + 1) &&
268 ((unsigned)tv1->tv_usec + 1000000 >= (unsigned)tv2->tv_usec + 1000)))
Willy Tarreau5e8f0662007-02-12 00:59:08 +0100269 return 1;
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200270 else
Willy Tarreau5e8f0662007-02-12 00:59:08 +0100271 return 0;
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200272}
273
274/*
275 * compares <tv1> and <tv2> modulo 1 ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2,
276 * assuming that TV_ETERNITY is greater than everything.
277 */
278#define tv_ms_cmp2 _tv_ms_cmp2
Willy Tarreau03e78532020-02-25 07:38:05 +0100279int _tv_ms_cmp2(const struct timeval *tv1, const struct timeval *tv2);
280static inline int __tv_ms_cmp2(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200281{
282 if (tv_iseternity(tv1))
283 if (tv_iseternity(tv2))
284 return 0; /* same */
285 else
286 return 1; /* tv1 later than tv2 */
287 else if (tv_iseternity(tv2))
288 return -1; /* tv2 later than tv1 */
289 return tv_ms_cmp(tv1, tv2);
290}
291
292/*
293 * compares <tv1> and <tv2> modulo 1 ms: returns 1 if tv1 <= tv2, 0 if tv1 > tv2,
294 * assuming that TV_ETERNITY is greater than everything. Returns 0 if tv1 is
295 * TV_ETERNITY, and always assumes that tv2 != TV_ETERNITY. Designed to replace
296 * occurrences of (tv_ms_cmp2(tv,now) <= 0).
297 */
298#define tv_ms_le2 _tv_ms_le2
Willy Tarreau03e78532020-02-25 07:38:05 +0100299int _tv_ms_le2(const struct timeval *tv1, const struct timeval *tv2);
300static inline int __tv_ms_le2(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200301{
302 if (likely((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec + 1))
303 return 0;
304
305 if (likely((unsigned)tv1->tv_sec < (unsigned)tv2->tv_sec))
306 return 1;
307
308 if (likely((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec)) {
309 if ((unsigned)tv2->tv_usec >= (unsigned)tv1->tv_usec + 1000)
310 return 1;
311 else
312 return 0;
313 }
314
315 if (unlikely(((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec + 1) &&
316 ((unsigned)tv1->tv_usec + 1000000 >= (unsigned)tv2->tv_usec + 1000)))
317 return 0;
318 else
Willy Tarreau5e8f0662007-02-12 00:59:08 +0100319 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200320}
321
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200322
323/**** operators **********************************************************/
324
325
Willy Tarreaubaaee002006-06-26 02:48:02 +0200326/*
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200327 * Returns the time in ms elapsed between tv1 and tv2, assuming that tv1<=tv2.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200328 * Must not be used when either argument is eternity.
329 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200330#define tv_ms_elapsed __tv_ms_elapsed
Willy Tarreau03e78532020-02-25 07:38:05 +0100331unsigned long _tv_ms_elapsed(const struct timeval *tv1, const struct timeval *tv2);
332static inline unsigned long __tv_ms_elapsed(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200333{
334 unsigned long ret;
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200335
336 ret = ((signed long)(tv2->tv_sec - tv1->tv_sec)) * 1000;
337 ret += ((signed long)(tv2->tv_usec - tv1->tv_usec)) / 1000;
338 return ret;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200339}
340
341/*
342 * returns the remaining time between tv1=now and event=tv2
343 * if tv2 is passed, 0 is returned.
344 * Must not be used when either argument is eternity.
345 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200346
347#define tv_ms_remain __tv_ms_remain
Willy Tarreau03e78532020-02-25 07:38:05 +0100348unsigned long _tv_ms_remain(const struct timeval *tv1, const struct timeval *tv2);
349static inline unsigned long __tv_ms_remain(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200350{
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200351 if (tv_ms_cmp(tv1, tv2) >= 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200352 return 0; /* event elapsed */
353
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200354 return __tv_ms_elapsed(tv1, tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200355}
356
Willy Tarreaubaaee002006-06-26 02:48:02 +0200357/*
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200358 * returns the remaining time between tv1=now and event=tv2
359 * if tv2 is passed, 0 is returned.
360 * Returns TIME_ETERNITY if tv2 is eternity.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200361 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200362#define tv_ms_remain2 _tv_ms_remain2
Willy Tarreau03e78532020-02-25 07:38:05 +0100363unsigned long _tv_ms_remain2(const struct timeval *tv1, const struct timeval *tv2);
364static inline unsigned long __tv_ms_remain2(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200365{
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200366 if (tv_iseternity(tv2))
367 return TIME_ETERNITY;
368
369 return tv_ms_remain(tv1, tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200370}
371
Willy Tarreau49fa3a12007-05-12 22:29:44 +0200372/*
373 * adds <inc> to <from>, set the result to <tv> and returns a pointer <tv>
374 */
Willy Tarreau0481c202007-05-13 16:03:27 +0200375#define tv_add _tv_add
Willy Tarreau03e78532020-02-25 07:38:05 +0100376struct timeval *_tv_add(struct timeval *tv, const struct timeval *from, const struct timeval *inc);
377static inline struct timeval *__tv_add(struct timeval *tv, const struct timeval *from, const struct timeval *inc)
Willy Tarreau49fa3a12007-05-12 22:29:44 +0200378{
379 tv->tv_usec = from->tv_usec + inc->tv_usec;
380 tv->tv_sec = from->tv_sec + inc->tv_sec;
381 if (tv->tv_usec >= 1000000) {
382 tv->tv_usec -= 1000000;
383 tv->tv_sec++;
384 }
385 return tv;
386}
387
388
389/*
Willy Tarreau0481c202007-05-13 16:03:27 +0200390 * If <inc> is set, then add it to <from> and set the result to <tv>, then
391 * return 1, otherwise return 0. It is meant to be used in if conditions.
392 */
393#define tv_add_ifset _tv_add_ifset
Willy Tarreau03e78532020-02-25 07:38:05 +0100394int _tv_add_ifset(struct timeval *tv, const struct timeval *from, const struct timeval *inc);
395static inline int __tv_add_ifset(struct timeval *tv, const struct timeval *from, const struct timeval *inc)
Willy Tarreau0481c202007-05-13 16:03:27 +0200396{
397 if (tv_iseternity(inc))
398 return 0;
399 tv->tv_usec = from->tv_usec + inc->tv_usec;
400 tv->tv_sec = from->tv_sec + inc->tv_sec;
401 if (tv->tv_usec >= 1000000) {
402 tv->tv_usec -= 1000000;
403 tv->tv_sec++;
404 }
405 return 1;
406}
407
408/*
Willy Tarreau49fa3a12007-05-12 22:29:44 +0200409 * adds <inc> to <tv> and returns a pointer <tv>
410 */
Willy Tarreau03e78532020-02-25 07:38:05 +0100411static inline struct timeval *__tv_add2(struct timeval *tv, const struct timeval *inc)
Willy Tarreau49fa3a12007-05-12 22:29:44 +0200412{
413 tv->tv_usec += inc->tv_usec;
414 tv->tv_sec += inc->tv_sec;
415 if (tv->tv_usec >= 1000000) {
416 tv->tv_usec -= 1000000;
417 tv->tv_sec++;
418 }
419 return tv;
420}
421
422
423/*
424 * Computes the remaining time between tv1=now and event=tv2. if tv2 is passed,
425 * 0 is returned. The result is stored into tv.
426 */
427#define tv_remain _tv_remain
Willy Tarreau03e78532020-02-25 07:38:05 +0100428struct timeval *_tv_remain(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv);
429static inline struct timeval *__tv_remain(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv)
Willy Tarreau49fa3a12007-05-12 22:29:44 +0200430{
431 tv->tv_usec = tv2->tv_usec - tv1->tv_usec;
432 tv->tv_sec = tv2->tv_sec - tv1->tv_sec;
433 if ((signed)tv->tv_sec > 0) {
434 if ((signed)tv->tv_usec < 0) {
435 tv->tv_usec += 1000000;
436 tv->tv_sec--;
437 }
438 } else if (tv->tv_sec == 0) {
439 if ((signed)tv->tv_usec < 0)
440 tv->tv_usec = 0;
441 } else {
442 tv->tv_sec = 0;
443 tv->tv_usec = 0;
444 }
445 return tv;
446}
447
448
449/*
450 * Computes the remaining time between tv1=now and event=tv2. if tv2 is passed,
451 * 0 is returned. The result is stored into tv. Returns ETERNITY if tv2 is
452 * eternity.
453 */
454#define tv_remain2 _tv_remain2
Willy Tarreau03e78532020-02-25 07:38:05 +0100455struct timeval *_tv_remain2(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv);
456static inline struct timeval *__tv_remain2(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv)
Willy Tarreau49fa3a12007-05-12 22:29:44 +0200457{
458 if (tv_iseternity(tv2))
459 return tv_eternity(tv);
460 return __tv_remain(tv1, tv2, tv);
461}
462
463
Willy Tarreaubaaee002006-06-26 02:48:02 +0200464/*
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200465 * adds <ms> ms to <from>, set the result to <tv> and returns a pointer <tv>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200466 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200467#define tv_ms_add _tv_ms_add
Willy Tarreau03e78532020-02-25 07:38:05 +0100468struct timeval *_tv_ms_add(struct timeval *tv, const struct timeval *from, int ms);
469static inline struct timeval *__tv_ms_add(struct timeval *tv, const struct timeval *from, int ms)
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200470{
471 tv->tv_usec = from->tv_usec + (ms % 1000) * 1000;
472 tv->tv_sec = from->tv_sec + (ms / 1000);
473 while (tv->tv_usec >= 1000000) {
474 tv->tv_usec -= 1000000;
475 tv->tv_sec++;
476 }
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200477 return tv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200478}
479
Willy Tarreaubaaee002006-06-26 02:48:02 +0200480
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200481/*
482 * compares <tv1> and <tv2> : returns 1 if <tv1> is before <tv2>, otherwise 0.
483 * This should be very fast because it's used in schedulers.
484 * It has been optimized to return 1 (so call it in a loop which continues
485 * as long as tv1<=tv2)
486 */
487
488#define tv_isbefore(tv1, tv2) \
489 (unlikely((unsigned)(tv1)->tv_sec < (unsigned)(tv2)->tv_sec) ? 1 : \
490 (unlikely((unsigned)(tv1)->tv_sec > (unsigned)(tv2)->tv_sec) ? 0 : \
491 unlikely((unsigned)(tv1)->tv_usec < (unsigned)(tv2)->tv_usec)))
492
493/*
494 * returns the first event between <tv1> and <tv2> into <tvmin>.
495 * a zero tv is ignored. <tvmin> is returned. If <tvmin> is known
496 * to be the same as <tv1> or <tv2>, it is recommended to use
497 * tv_bound instead.
498 */
499#define tv_min(tvmin, tv1, tv2) ({ \
500 if (tv_isbefore(tv1, tv2)) { \
501 *tvmin = *tv1; \
502 } \
503 else { \
504 *tvmin = *tv2; \
505 } \
506 tvmin; \
507})
508
509/*
510 * returns the first event between <tv1> and <tv2> into <tvmin>.
511 * a zero tv is ignored. <tvmin> is returned. This function has been
512 * optimized to be called as tv_min(a,a,b) or tv_min(b,a,b).
513 */
514#define tv_bound(tv1, tv2) ({ \
515 if (tv_isbefore(tv2, tv1)) \
516 *tv1 = *tv2; \
517 tv1; \
518})
Willy Tarreaubaaee002006-06-26 02:48:02 +0200519
Willy Tarreau5ceeb152018-10-17 18:59:53 +0200520/* returns the system's monotonic time in nanoseconds if supported, otherwise zero */
521static inline uint64_t now_mono_time()
522{
Willy Tarreauf6178242019-05-21 19:46:58 +0200523#if (_POSIX_TIMERS > 0) && defined(_POSIX_MONOTONIC_CLOCK)
Willy Tarreau5ceeb152018-10-17 18:59:53 +0200524 struct timespec ts;
525 clock_gettime(CLOCK_MONOTONIC, &ts);
526 return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
527#else
528 return 0;
529#endif
530}
531
532/* returns the current thread's cumulated CPU time in nanoseconds if supported, otherwise zero */
533static inline uint64_t now_cpu_time()
534{
Willy Tarreauf6178242019-05-21 19:46:58 +0200535#if (_POSIX_TIMERS > 0) && defined(_POSIX_THREAD_CPUTIME)
Willy Tarreau5ceeb152018-10-17 18:59:53 +0200536 struct timespec ts;
537 clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts);
538 return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
539#else
540 return 0;
541#endif
542}
543
Willy Tarreau219b8292019-05-20 20:28:34 +0200544/* returns another thread's cumulated CPU time in nanoseconds if supported, otherwise zero */
545static inline uint64_t now_cpu_time_thread(const struct thread_info *thr)
546{
Willy Tarreauf6178242019-05-21 19:46:58 +0200547#if (_POSIX_TIMERS > 0) && defined(_POSIX_THREAD_CPUTIME)
Willy Tarreau219b8292019-05-20 20:28:34 +0200548 struct timespec ts;
549 clock_gettime(thr->clock_id, &ts);
550 return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
551#else
552 return 0;
553#endif
554}
555
Willy Tarreau45a12512011-09-10 16:56:42 +0200556/* Update the idle time value twice a second, to be called after
557 * tv_update_date() when called after poll(). It relies on <before_poll> to be
558 * updated to the system time before calling poll().
559 */
560static inline void measure_idle()
561{
562 /* Let's compute the idle to work ratio. We worked between after_poll
563 * and before_poll, and slept between before_poll and date. The idle_pct
564 * is updated at most twice every second. Note that the current second
565 * rarely changes so we avoid a multiply when not needed.
566 */
567 int delta;
568
569 if ((delta = date.tv_sec - before_poll.tv_sec))
570 delta *= 1000000;
571 idle_time += delta + (date.tv_usec - before_poll.tv_usec);
572
573 if ((delta = date.tv_sec - after_poll.tv_sec))
574 delta *= 1000000;
575 samp_time += delta + (date.tv_usec - after_poll.tv_usec);
576
577 after_poll.tv_sec = date.tv_sec; after_poll.tv_usec = date.tv_usec;
578 if (samp_time < 500000)
579 return;
580
Willy Tarreau81036f22019-05-20 19:24:50 +0200581 ti->idle_pct = (100 * idle_time + samp_time / 2) / samp_time;
Willy Tarreau45a12512011-09-10 16:56:42 +0200582 idle_time = samp_time = 0;
583}
584
Willy Tarreau7e9c4ae2018-10-17 14:31:19 +0200585/* Collect date and time information before calling poll(). This will be used
586 * to count the run time of the past loop and the sleep time of the next poll.
587 */
588static inline void tv_entering_poll()
589{
590 gettimeofday(&before_poll, NULL);
591}
592
593/* Collect date and time information after leaving poll(). <timeout> must be
594 * set to the maximum sleep time passed to poll (in milliseconds), and
595 * <interrupted> must be zero if the poller reached the timeout or non-zero
596 * otherwise, which generally is provided by the poller's return value.
597 */
598static inline void tv_leaving_poll(int timeout, int interrupted)
599{
Willy Tarreau7e9c4ae2018-10-17 14:31:19 +0200600 measure_idle();
Willy Tarreau81036f22019-05-20 19:24:50 +0200601 ti->prev_cpu_time = now_cpu_time();
602 ti->prev_mono_time = now_mono_time();
Willy Tarreau7e9c4ae2018-10-17 14:31:19 +0200603}
604
Willy Tarreau2dd0d472006-06-29 17:53:05 +0200605#endif /* _COMMON_TIME_H */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200606
607/*
608 * Local variables:
609 * c-indent-level: 8
610 * c-basic-offset: 8
611 * End:
612 */