blob: 42bc9579acdf1e056d5ac35420d8d498660ac534 [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
Willy Tarreaua1bd1fa2019-03-29 17:26:33 +010025#include <inttypes.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020026#include <stdlib.h>
Willy Tarreau5ceeb152018-10-17 18:59:53 +020027#include <unistd.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020028#include <sys/time.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020029#include <common/config.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 */
60extern THREAD_LOCAL unsigned int idle_pct; /* idle to total ratio over last sample (percent) */
61extern THREAD_LOCAL struct timeval now; /* internal date is a monotonic function of real clock */
62extern THREAD_LOCAL struct timeval date; /* the real current date */
Willy Tarreaubaaee002006-06-26 02:48:02 +020063extern struct timeval start_date; /* the process's start date */
Christopher Faulet9a655712017-05-11 11:00:15 +020064extern THREAD_LOCAL struct timeval before_poll; /* system date before calling poll() */
65extern THREAD_LOCAL struct timeval after_poll; /* system date after leaving poll() */
Willy Tarreaued72d822018-10-17 19:01:24 +020066extern THREAD_LOCAL uint64_t prev_cpu_time; /* previous per thread CPU time */
67extern THREAD_LOCAL uint64_t prev_mono_time; /* previous system wide monotonic time */
Willy Tarreaubaaee002006-06-26 02:48:02 +020068
69
Willy Tarreau42aae5c2007-04-29 17:43:56 +020070/**** exported functions *************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +020071/*
72 * adds <ms> ms to <from>, set the result to <tv> and returns a pointer <tv>
73 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +020074REGPRM3 struct timeval *tv_ms_add(struct timeval *tv, const struct timeval *from, int ms);
Willy Tarreaubaaee002006-06-26 02:48:02 +020075
76/*
77 * 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 +020078 * Must not be used when either argument is eternity. Use tv_ms_cmp2() for that.
Willy Tarreaubaaee002006-06-26 02:48:02 +020079 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +020080REGPRM2 int tv_ms_cmp(const struct timeval *tv1, const struct timeval *tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +020081
82/*
Willy Tarreau8d7d1492007-04-29 10:50:43 +020083 * compares <tv1> and <tv2> modulo 1 ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2,
84 * assuming that TV_ETERNITY is greater than everything.
Willy Tarreaubaaee002006-06-26 02:48:02 +020085 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +020086REGPRM2 int tv_ms_cmp2(const struct timeval *tv1, const struct timeval *tv2);
Willy Tarreau5e8f0662007-02-12 00:59:08 +010087
Willy Tarreau42aae5c2007-04-29 17:43:56 +020088/**** general purpose functions and macros *******************************/
89
90
91/* tv_now: sets <tv> to the current time */
92REGPRM1 static inline struct timeval *tv_now(struct timeval *tv)
93{
94 gettimeofday(tv, NULL);
95 return tv;
96}
Willy Tarreaubaaee002006-06-26 02:48:02 +020097
Willy Tarreaub0b37bc2008-06-23 14:00:57 +020098/* tv_udpate_date: sets <date> to system time, and sets <now> to something as
99 * close as possible to real time, following a monotonic function. The main
100 * principle consists in detecting backwards and forwards time jumps and adjust
101 * an offset to correct them. This function should be called only once after
102 * each poll. The poll's timeout should be passed in <max_wait>, and the return
103 * value in <interrupted> (a non-zero value means that we have not expired the
104 * timeout).
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200105 */
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200106REGPRM2 void tv_update_date(int max_wait, int interrupted);
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200107
Willy Tarreaubaaee002006-06-26 02:48:02 +0200108/*
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200109 * sets a struct timeval to its highest value so that it can never happen
110 * note that only tv_usec is necessary to detect it since a tv_usec > 999999
111 * is normally not possible.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200112 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200113REGPRM1 static inline struct timeval *tv_eternity(struct timeval *tv)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200114{
Willy Tarreau5f3f15f2013-12-13 09:22:23 +0100115 tv->tv_sec = (typeof(tv->tv_sec))TV_ETERNITY;
116 tv->tv_usec = (typeof(tv->tv_usec))TV_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200117 return tv;
118}
119
120/*
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200121 * sets a struct timeval to 0
122 *
Willy Tarreaubaaee002006-06-26 02:48:02 +0200123 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200124REGPRM1 static inline struct timeval *tv_zero(struct timeval *tv) {
125 tv->tv_sec = tv->tv_usec = 0;
126 return tv;
127}
128
129/*
130 * returns non null if tv is [eternity], otherwise 0.
131 */
Willy Tarreau5f3f15f2013-12-13 09:22:23 +0100132#define tv_iseternity(tv) ((tv)->tv_usec == (typeof((tv)->tv_usec))TV_ETERNITY)
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200133
134/*
Willy Tarreau49fa3a12007-05-12 22:29:44 +0200135 * returns 0 if tv is [eternity], otherwise non-zero.
136 */
Willy Tarreau5f3f15f2013-12-13 09:22:23 +0100137#define tv_isset(tv) ((tv)->tv_usec != (typeof((tv)->tv_usec))TV_ETERNITY)
Willy Tarreau49fa3a12007-05-12 22:29:44 +0200138
139/*
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200140 * returns non null if tv is [0], otherwise 0.
141 */
142#define tv_iszero(tv) (((tv)->tv_sec | (tv)->tv_usec) == 0)
143
Willy Tarreau49fa3a12007-05-12 22:29:44 +0200144/*
145 * Converts a struct timeval to a number of milliseconds.
146 */
147REGPRM1 static inline unsigned long __tv_to_ms(const struct timeval *tv)
148{
149 unsigned long ret;
150
151 ret = tv->tv_sec * 1000;
152 ret += tv->tv_usec / 1000;
153 return ret;
154}
155
156/*
157 * Converts a struct timeval to a number of milliseconds.
158 */
159REGPRM2 static inline struct timeval * __tv_from_ms(struct timeval *tv, unsigned long ms)
160{
161 tv->tv_sec = ms / 1000;
162 tv->tv_usec = (ms % 1000) * 1000;
163 return tv;
164}
165
Willy Tarreau776cd872009-03-05 00:34:01 +0100166/* Return a number of 1024Hz ticks between 0 and 1023 for input number of
167 * usecs between 0 and 999999. This function has been optimized to remove
168 * any divide and multiply, as it is completely optimized away by the compiler
169 * on CPUs which don't have a fast multiply. Its avg error rate is 305 ppm,
170 * which is almost twice as low as a direct usec to ms conversion. This version
171 * also has the benefit of returning 1024 for 1000000.
172 */
173REGPRM1 static inline unsigned int __usec_to_1024th(unsigned int usec)
174{
175 return (usec * 1073 + 742516) >> 20;
176}
177
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200178
Willy Tarreau49fa3a12007-05-12 22:29:44 +0200179/**** comparison functions and macros ***********************************/
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200180
181
182/* tv_cmp: compares <tv1> and <tv2> : returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2. */
183REGPRM2 static inline int __tv_cmp(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200184{
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200185 if ((unsigned)tv1->tv_sec < (unsigned)tv2->tv_sec)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200186 return -1;
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200187 else if ((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200188 return 1;
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200189 else if ((unsigned)tv1->tv_usec < (unsigned)tv2->tv_usec)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200190 return -1;
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200191 else if ((unsigned)tv1->tv_usec > (unsigned)tv2->tv_usec)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200192 return 1;
193 else
194 return 0;
Willy Tarreau5e8f0662007-02-12 00:59:08 +0100195}
196
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200197/* tv_iseq: compares <tv1> and <tv2> : returns 1 if tv1 == tv2, otherwise 0 */
Willy Tarreau0481c202007-05-13 16:03:27 +0200198#define tv_iseq __tv_iseq
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200199REGPRM2 static inline int __tv_iseq(const struct timeval *tv1, const struct timeval *tv2)
200{
Willy Tarreau0481c202007-05-13 16:03:27 +0200201 return ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) &&
202 ((unsigned)tv1->tv_usec == (unsigned)tv2->tv_usec);
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200203}
204
205/* tv_isgt: compares <tv1> and <tv2> : returns 1 if tv1 > tv2, otherwise 0 */
Willy Tarreau0481c202007-05-13 16:03:27 +0200206#define tv_isgt _tv_isgt
207REGPRM2 int _tv_isgt(const struct timeval *tv1, const struct timeval *tv2);
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200208REGPRM2 static inline int __tv_isgt(const struct timeval *tv1, const struct timeval *tv2)
209{
Willy Tarreau0481c202007-05-13 16:03:27 +0200210 return
211 ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) ?
212 ((unsigned)tv1->tv_usec > (unsigned)tv2->tv_usec) :
213 ((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec);
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200214}
215
216/* tv_isge: compares <tv1> and <tv2> : returns 1 if tv1 >= tv2, otherwise 0 */
Willy Tarreau0481c202007-05-13 16:03:27 +0200217#define tv_isge __tv_isge
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200218REGPRM2 static inline int __tv_isge(const struct timeval *tv1, const struct timeval *tv2)
219{
Willy Tarreau0481c202007-05-13 16:03:27 +0200220 return
221 ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) ?
222 ((unsigned)tv1->tv_usec >= (unsigned)tv2->tv_usec) :
223 ((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec);
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200224}
225
226/* tv_islt: compares <tv1> and <tv2> : returns 1 if tv1 < tv2, otherwise 0 */
Willy Tarreau0481c202007-05-13 16:03:27 +0200227#define tv_islt __tv_islt
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200228REGPRM2 static inline int __tv_islt(const struct timeval *tv1, const struct timeval *tv2)
229{
Willy Tarreau0481c202007-05-13 16:03:27 +0200230 return
231 ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) ?
232 ((unsigned)tv1->tv_usec < (unsigned)tv2->tv_usec) :
233 ((unsigned)tv1->tv_sec < (unsigned)tv2->tv_sec);
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200234}
235
236/* tv_isle: compares <tv1> and <tv2> : returns 1 if tv1 <= tv2, otherwise 0 */
Willy Tarreau0481c202007-05-13 16:03:27 +0200237#define tv_isle _tv_isle
238REGPRM2 int _tv_isle(const struct timeval *tv1, const struct timeval *tv2);
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200239REGPRM2 static inline int __tv_isle(const struct timeval *tv1, const struct timeval *tv2)
240{
Willy Tarreau0481c202007-05-13 16:03:27 +0200241 return
242 ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) ?
243 ((unsigned)tv1->tv_usec <= (unsigned)tv2->tv_usec) :
244 ((unsigned)tv1->tv_sec < (unsigned)tv2->tv_sec);
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200245}
246
Willy Tarreau5e8f0662007-02-12 00:59:08 +0100247/*
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200248 * compares <tv1> and <tv2> modulo 1ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2
249 * Must not be used when either argument is eternity. Use tv_ms_cmp2() for that.
Willy Tarreau5e8f0662007-02-12 00:59:08 +0100250 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200251#define tv_ms_cmp _tv_ms_cmp
252REGPRM2 int _tv_ms_cmp(const struct timeval *tv1, const struct timeval *tv2);
253REGPRM2 static inline int __tv_ms_cmp(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreau5e8f0662007-02-12 00:59:08 +0100254{
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200255 if ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) {
256 if ((unsigned)tv2->tv_usec >= (unsigned)tv1->tv_usec + 1000)
257 return -1;
258 else if ((unsigned)tv1->tv_usec >= (unsigned)tv2->tv_usec + 1000)
259 return 1;
260 else
261 return 0;
262 }
263 else if (((unsigned)tv2->tv_sec > (unsigned)tv1->tv_sec + 1) ||
264 (((unsigned)tv2->tv_sec == (unsigned)tv1->tv_sec + 1) &&
265 ((unsigned)tv2->tv_usec + 1000000 >= (unsigned)tv1->tv_usec + 1000)))
266 return -1;
267 else if (((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec + 1) ||
268 (((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec + 1) &&
269 ((unsigned)tv1->tv_usec + 1000000 >= (unsigned)tv2->tv_usec + 1000)))
Willy Tarreau5e8f0662007-02-12 00:59:08 +0100270 return 1;
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200271 else
Willy Tarreau5e8f0662007-02-12 00:59:08 +0100272 return 0;
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200273}
274
275/*
276 * compares <tv1> and <tv2> modulo 1 ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2,
277 * assuming that TV_ETERNITY is greater than everything.
278 */
279#define tv_ms_cmp2 _tv_ms_cmp2
280REGPRM2 int _tv_ms_cmp2(const struct timeval *tv1, const struct timeval *tv2);
281REGPRM2 static inline int __tv_ms_cmp2(const struct timeval *tv1, const struct timeval *tv2)
282{
283 if (tv_iseternity(tv1))
284 if (tv_iseternity(tv2))
285 return 0; /* same */
286 else
287 return 1; /* tv1 later than tv2 */
288 else if (tv_iseternity(tv2))
289 return -1; /* tv2 later than tv1 */
290 return tv_ms_cmp(tv1, tv2);
291}
292
293/*
294 * compares <tv1> and <tv2> modulo 1 ms: returns 1 if tv1 <= tv2, 0 if tv1 > tv2,
295 * assuming that TV_ETERNITY is greater than everything. Returns 0 if tv1 is
296 * TV_ETERNITY, and always assumes that tv2 != TV_ETERNITY. Designed to replace
297 * occurrences of (tv_ms_cmp2(tv,now) <= 0).
298 */
299#define tv_ms_le2 _tv_ms_le2
300REGPRM2 int _tv_ms_le2(const struct timeval *tv1, const struct timeval *tv2);
301REGPRM2 static inline int __tv_ms_le2(const struct timeval *tv1, const struct timeval *tv2)
302{
303 if (likely((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec + 1))
304 return 0;
305
306 if (likely((unsigned)tv1->tv_sec < (unsigned)tv2->tv_sec))
307 return 1;
308
309 if (likely((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec)) {
310 if ((unsigned)tv2->tv_usec >= (unsigned)tv1->tv_usec + 1000)
311 return 1;
312 else
313 return 0;
314 }
315
316 if (unlikely(((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec + 1) &&
317 ((unsigned)tv1->tv_usec + 1000000 >= (unsigned)tv2->tv_usec + 1000)))
318 return 0;
319 else
Willy Tarreau5e8f0662007-02-12 00:59:08 +0100320 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200321}
322
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200323
324/**** operators **********************************************************/
325
326
Willy Tarreaubaaee002006-06-26 02:48:02 +0200327/*
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200328 * Returns the time in ms elapsed between tv1 and tv2, assuming that tv1<=tv2.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200329 * Must not be used when either argument is eternity.
330 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200331#define tv_ms_elapsed __tv_ms_elapsed
332REGPRM2 unsigned long _tv_ms_elapsed(const struct timeval *tv1, const struct timeval *tv2);
333REGPRM2 static inline unsigned long __tv_ms_elapsed(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200334{
335 unsigned long ret;
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200336
337 ret = ((signed long)(tv2->tv_sec - tv1->tv_sec)) * 1000;
338 ret += ((signed long)(tv2->tv_usec - tv1->tv_usec)) / 1000;
339 return ret;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200340}
341
342/*
343 * returns the remaining time between tv1=now and event=tv2
344 * if tv2 is passed, 0 is returned.
345 * Must not be used when either argument is eternity.
346 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200347
348#define tv_ms_remain __tv_ms_remain
349REGPRM2 unsigned long _tv_ms_remain(const struct timeval *tv1, const struct timeval *tv2);
350REGPRM2 static inline unsigned long __tv_ms_remain(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200351{
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200352 if (tv_ms_cmp(tv1, tv2) >= 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200353 return 0; /* event elapsed */
354
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200355 return __tv_ms_elapsed(tv1, tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200356}
357
Willy Tarreaubaaee002006-06-26 02:48:02 +0200358/*
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200359 * returns the remaining time between tv1=now and event=tv2
360 * if tv2 is passed, 0 is returned.
361 * Returns TIME_ETERNITY if tv2 is eternity.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200362 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200363#define tv_ms_remain2 _tv_ms_remain2
364REGPRM2 unsigned long _tv_ms_remain2(const struct timeval *tv1, const struct timeval *tv2);
365REGPRM2 static inline unsigned long __tv_ms_remain2(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200366{
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200367 if (tv_iseternity(tv2))
368 return TIME_ETERNITY;
369
370 return tv_ms_remain(tv1, tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200371}
372
Willy Tarreau49fa3a12007-05-12 22:29:44 +0200373/*
374 * adds <inc> to <from>, set the result to <tv> and returns a pointer <tv>
375 */
Willy Tarreau0481c202007-05-13 16:03:27 +0200376#define tv_add _tv_add
Willy Tarreau49fa3a12007-05-12 22:29:44 +0200377REGPRM3 struct timeval *_tv_add(struct timeval *tv, const struct timeval *from, const struct timeval *inc);
378REGPRM3 static inline struct timeval *__tv_add(struct timeval *tv, const struct timeval *from, const struct timeval *inc)
379{
380 tv->tv_usec = from->tv_usec + inc->tv_usec;
381 tv->tv_sec = from->tv_sec + inc->tv_sec;
382 if (tv->tv_usec >= 1000000) {
383 tv->tv_usec -= 1000000;
384 tv->tv_sec++;
385 }
386 return tv;
387}
388
389
390/*
Willy Tarreau0481c202007-05-13 16:03:27 +0200391 * If <inc> is set, then add it to <from> and set the result to <tv>, then
392 * return 1, otherwise return 0. It is meant to be used in if conditions.
393 */
394#define tv_add_ifset _tv_add_ifset
395REGPRM3 int _tv_add_ifset(struct timeval *tv, const struct timeval *from, const struct timeval *inc);
396REGPRM3 static inline int __tv_add_ifset(struct timeval *tv, const struct timeval *from, const struct timeval *inc)
397{
398 if (tv_iseternity(inc))
399 return 0;
400 tv->tv_usec = from->tv_usec + inc->tv_usec;
401 tv->tv_sec = from->tv_sec + inc->tv_sec;
402 if (tv->tv_usec >= 1000000) {
403 tv->tv_usec -= 1000000;
404 tv->tv_sec++;
405 }
406 return 1;
407}
408
409/*
Willy Tarreau49fa3a12007-05-12 22:29:44 +0200410 * adds <inc> to <tv> and returns a pointer <tv>
411 */
412REGPRM2 static inline struct timeval *__tv_add2(struct timeval *tv, const struct timeval *inc)
413{
414 tv->tv_usec += inc->tv_usec;
415 tv->tv_sec += inc->tv_sec;
416 if (tv->tv_usec >= 1000000) {
417 tv->tv_usec -= 1000000;
418 tv->tv_sec++;
419 }
420 return tv;
421}
422
423
424/*
425 * Computes the remaining time between tv1=now and event=tv2. if tv2 is passed,
426 * 0 is returned. The result is stored into tv.
427 */
428#define tv_remain _tv_remain
429REGPRM3 struct timeval *_tv_remain(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv);
430REGPRM3 static inline struct timeval *__tv_remain(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv)
431{
432 tv->tv_usec = tv2->tv_usec - tv1->tv_usec;
433 tv->tv_sec = tv2->tv_sec - tv1->tv_sec;
434 if ((signed)tv->tv_sec > 0) {
435 if ((signed)tv->tv_usec < 0) {
436 tv->tv_usec += 1000000;
437 tv->tv_sec--;
438 }
439 } else if (tv->tv_sec == 0) {
440 if ((signed)tv->tv_usec < 0)
441 tv->tv_usec = 0;
442 } else {
443 tv->tv_sec = 0;
444 tv->tv_usec = 0;
445 }
446 return tv;
447}
448
449
450/*
451 * Computes the remaining time between tv1=now and event=tv2. if tv2 is passed,
452 * 0 is returned. The result is stored into tv. Returns ETERNITY if tv2 is
453 * eternity.
454 */
455#define tv_remain2 _tv_remain2
456REGPRM3 struct timeval *_tv_remain2(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv);
457REGPRM3 static inline struct timeval *__tv_remain2(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv)
458{
459 if (tv_iseternity(tv2))
460 return tv_eternity(tv);
461 return __tv_remain(tv1, tv2, tv);
462}
463
464
Willy Tarreaubaaee002006-06-26 02:48:02 +0200465/*
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200466 * adds <ms> ms to <from>, set the result to <tv> and returns a pointer <tv>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200467 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200468#define tv_ms_add _tv_ms_add
469REGPRM3 struct timeval *_tv_ms_add(struct timeval *tv, const struct timeval *from, int ms);
470REGPRM3 static inline struct timeval *__tv_ms_add(struct timeval *tv, const struct timeval *from, int ms)
471{
472 tv->tv_usec = from->tv_usec + (ms % 1000) * 1000;
473 tv->tv_sec = from->tv_sec + (ms / 1000);
474 while (tv->tv_usec >= 1000000) {
475 tv->tv_usec -= 1000000;
476 tv->tv_sec++;
477 }
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200478 return tv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200479}
480
Willy Tarreaubaaee002006-06-26 02:48:02 +0200481
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200482/*
483 * compares <tv1> and <tv2> : returns 1 if <tv1> is before <tv2>, otherwise 0.
484 * This should be very fast because it's used in schedulers.
485 * It has been optimized to return 1 (so call it in a loop which continues
486 * as long as tv1<=tv2)
487 */
488
489#define tv_isbefore(tv1, tv2) \
490 (unlikely((unsigned)(tv1)->tv_sec < (unsigned)(tv2)->tv_sec) ? 1 : \
491 (unlikely((unsigned)(tv1)->tv_sec > (unsigned)(tv2)->tv_sec) ? 0 : \
492 unlikely((unsigned)(tv1)->tv_usec < (unsigned)(tv2)->tv_usec)))
493
494/*
495 * returns the first event between <tv1> and <tv2> into <tvmin>.
496 * a zero tv is ignored. <tvmin> is returned. If <tvmin> is known
497 * to be the same as <tv1> or <tv2>, it is recommended to use
498 * tv_bound instead.
499 */
500#define tv_min(tvmin, tv1, tv2) ({ \
501 if (tv_isbefore(tv1, tv2)) { \
502 *tvmin = *tv1; \
503 } \
504 else { \
505 *tvmin = *tv2; \
506 } \
507 tvmin; \
508})
509
510/*
511 * returns the first event between <tv1> and <tv2> into <tvmin>.
512 * a zero tv is ignored. <tvmin> is returned. This function has been
513 * optimized to be called as tv_min(a,a,b) or tv_min(b,a,b).
514 */
515#define tv_bound(tv1, tv2) ({ \
516 if (tv_isbefore(tv2, tv1)) \
517 *tv1 = *tv2; \
518 tv1; \
519})
Willy Tarreaubaaee002006-06-26 02:48:02 +0200520
Willy Tarreau5ceeb152018-10-17 18:59:53 +0200521/* returns the system's monotonic time in nanoseconds if supported, otherwise zero */
522static inline uint64_t now_mono_time()
523{
524#if defined(_POSIX_TIMERS) && defined(_POSIX_MONOTONIC_CLOCK)
525 struct timespec ts;
526 clock_gettime(CLOCK_MONOTONIC, &ts);
527 return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
528#else
529 return 0;
530#endif
531}
532
533/* returns the current thread's cumulated CPU time in nanoseconds if supported, otherwise zero */
534static inline uint64_t now_cpu_time()
535{
536#if defined(_POSIX_TIMERS) && defined(_POSIX_THREAD_CPUTIME)
537 struct timespec ts;
538 clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts);
539 return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
540#else
541 return 0;
542#endif
543}
544
Willy Tarreau45a12512011-09-10 16:56:42 +0200545/* Update the idle time value twice a second, to be called after
546 * tv_update_date() when called after poll(). It relies on <before_poll> to be
547 * updated to the system time before calling poll().
548 */
549static inline void measure_idle()
550{
551 /* Let's compute the idle to work ratio. We worked between after_poll
552 * and before_poll, and slept between before_poll and date. The idle_pct
553 * is updated at most twice every second. Note that the current second
554 * rarely changes so we avoid a multiply when not needed.
555 */
556 int delta;
557
558 if ((delta = date.tv_sec - before_poll.tv_sec))
559 delta *= 1000000;
560 idle_time += delta + (date.tv_usec - before_poll.tv_usec);
561
562 if ((delta = date.tv_sec - after_poll.tv_sec))
563 delta *= 1000000;
564 samp_time += delta + (date.tv_usec - after_poll.tv_usec);
565
566 after_poll.tv_sec = date.tv_sec; after_poll.tv_usec = date.tv_usec;
567 if (samp_time < 500000)
568 return;
569
570 idle_pct = (100 * idle_time + samp_time / 2) / samp_time;
571 idle_time = samp_time = 0;
572}
573
Willy Tarreau7e9c4ae2018-10-17 14:31:19 +0200574/* Collect date and time information before calling poll(). This will be used
575 * to count the run time of the past loop and the sleep time of the next poll.
576 */
577static inline void tv_entering_poll()
578{
579 gettimeofday(&before_poll, NULL);
580}
581
582/* Collect date and time information after leaving poll(). <timeout> must be
583 * set to the maximum sleep time passed to poll (in milliseconds), and
584 * <interrupted> must be zero if the poller reached the timeout or non-zero
585 * otherwise, which generally is provided by the poller's return value.
586 */
587static inline void tv_leaving_poll(int timeout, int interrupted)
588{
Willy Tarreau7e9c4ae2018-10-17 14:31:19 +0200589 measure_idle();
Willy Tarreaued72d822018-10-17 19:01:24 +0200590 prev_cpu_time = now_cpu_time();
591 prev_mono_time = now_mono_time();
Willy Tarreau7e9c4ae2018-10-17 14:31:19 +0200592}
593
Willy Tarreau2dd0d472006-06-29 17:53:05 +0200594#endif /* _COMMON_TIME_H */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200595
596/*
597 * Local variables:
598 * c-indent-level: 8
599 * c-basic-offset: 8
600 * End:
601 */