blob: 2633b1c5eea070de270649c0e3df2eca2ebfce10 [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 Tarreau81036f22019-05-20 19:24:50 +020030#include <common/hathreads.h>
Willy Tarreau42aae5c2007-04-29 17:43:56 +020031#include <common/standard.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020032
Willy Tarreaua6a6a932007-04-28 22:40:08 +020033/* eternity when exprimed in timeval */
34#ifndef TV_ETERNITY
35#define TV_ETERNITY (~0UL)
36#endif
37
38/* eternity when exprimed in ms */
39#ifndef TV_ETERNITY_MS
40#define TV_ETERNITY_MS (-1)
41#endif
42
43#define TIME_ETERNITY (TV_ETERNITY_MS)
44
Willy Tarreaub0b37bc2008-06-23 14:00:57 +020045/* we want to be able to detect time jumps. Fix the maximum wait time to a low
46 * value so that we know the time has changed if we wait longer.
47 */
48#define MAX_DELAY_MS 1000
49
Willy Tarreaubaaee002006-06-26 02:48:02 +020050
51/* returns the lowest delay amongst <old> and <new>, and respects TIME_ETERNITY */
52#define MINTIME(old, new) (((new)<0)?(old):(((old)<0||(new)<(old))?(new):(old)))
53#define SETNOW(a) (*a=now)
54
Christopher Faulet9a655712017-05-11 11:00:15 +020055extern THREAD_LOCAL unsigned int curr_sec_ms; /* millisecond of current second (0..999) */
56extern THREAD_LOCAL unsigned int ms_left_scaled; /* milliseconds left for current second (0..2^32-1) */
57extern THREAD_LOCAL unsigned int curr_sec_ms_scaled; /* millisecond of current second (0..2^32-1) */
58extern THREAD_LOCAL unsigned int now_ms; /* internal date in milliseconds (may wrap) */
59extern THREAD_LOCAL unsigned int samp_time; /* total elapsed time over current sample */
60extern THREAD_LOCAL unsigned int idle_time; /* total idle time over current sample */
Christopher Faulet9a655712017-05-11 11:00:15 +020061extern 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 Tarreaubaaee002006-06-26 02:48:02 +020066
67
Willy Tarreau42aae5c2007-04-29 17:43:56 +020068/**** exported functions *************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +020069/*
70 * adds <ms> ms to <from>, set the result to <tv> and returns a pointer <tv>
71 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +020072REGPRM3 struct timeval *tv_ms_add(struct timeval *tv, const struct timeval *from, int ms);
Willy Tarreaubaaee002006-06-26 02:48:02 +020073
74/*
75 * 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 +020076 * Must not be used when either argument is eternity. Use tv_ms_cmp2() for that.
Willy Tarreaubaaee002006-06-26 02:48:02 +020077 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +020078REGPRM2 int tv_ms_cmp(const struct timeval *tv1, const struct timeval *tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +020079
80/*
Willy Tarreau8d7d1492007-04-29 10:50:43 +020081 * compares <tv1> and <tv2> modulo 1 ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2,
82 * assuming that TV_ETERNITY is greater than everything.
Willy Tarreaubaaee002006-06-26 02:48:02 +020083 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +020084REGPRM2 int tv_ms_cmp2(const struct timeval *tv1, const struct timeval *tv2);
Willy Tarreau5e8f0662007-02-12 00:59:08 +010085
Willy Tarreau42aae5c2007-04-29 17:43:56 +020086/**** general purpose functions and macros *******************************/
87
88
89/* tv_now: sets <tv> to the current time */
90REGPRM1 static inline struct timeval *tv_now(struct timeval *tv)
91{
92 gettimeofday(tv, NULL);
93 return tv;
94}
Willy Tarreaubaaee002006-06-26 02:48:02 +020095
Willy Tarreaub0b37bc2008-06-23 14:00:57 +020096/* tv_udpate_date: sets <date> to system time, and sets <now> to something as
97 * close as possible to real time, following a monotonic function. The main
98 * principle consists in detecting backwards and forwards time jumps and adjust
99 * an offset to correct them. This function should be called only once after
100 * each poll. The poll's timeout should be passed in <max_wait>, and the return
101 * value in <interrupted> (a non-zero value means that we have not expired the
102 * timeout).
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200103 */
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200104REGPRM2 void tv_update_date(int max_wait, int interrupted);
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200105
Willy Tarreaubaaee002006-06-26 02:48:02 +0200106/*
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200107 * sets a struct timeval to its highest value so that it can never happen
108 * note that only tv_usec is necessary to detect it since a tv_usec > 999999
109 * is normally not possible.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200110 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200111REGPRM1 static inline struct timeval *tv_eternity(struct timeval *tv)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200112{
Willy Tarreau5f3f15f2013-12-13 09:22:23 +0100113 tv->tv_sec = (typeof(tv->tv_sec))TV_ETERNITY;
114 tv->tv_usec = (typeof(tv->tv_usec))TV_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200115 return tv;
116}
117
118/*
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200119 * sets a struct timeval to 0
120 *
Willy Tarreaubaaee002006-06-26 02:48:02 +0200121 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200122REGPRM1 static inline struct timeval *tv_zero(struct timeval *tv) {
123 tv->tv_sec = tv->tv_usec = 0;
124 return tv;
125}
126
127/*
128 * returns non null if tv is [eternity], otherwise 0.
129 */
Willy Tarreau5f3f15f2013-12-13 09:22:23 +0100130#define tv_iseternity(tv) ((tv)->tv_usec == (typeof((tv)->tv_usec))TV_ETERNITY)
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200131
132/*
Willy Tarreau49fa3a12007-05-12 22:29:44 +0200133 * returns 0 if tv is [eternity], otherwise non-zero.
134 */
Willy Tarreau5f3f15f2013-12-13 09:22:23 +0100135#define tv_isset(tv) ((tv)->tv_usec != (typeof((tv)->tv_usec))TV_ETERNITY)
Willy Tarreau49fa3a12007-05-12 22:29:44 +0200136
137/*
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200138 * returns non null if tv is [0], otherwise 0.
139 */
140#define tv_iszero(tv) (((tv)->tv_sec | (tv)->tv_usec) == 0)
141
Willy Tarreau49fa3a12007-05-12 22:29:44 +0200142/*
143 * Converts a struct timeval to a number of milliseconds.
144 */
145REGPRM1 static inline unsigned long __tv_to_ms(const struct timeval *tv)
146{
147 unsigned long ret;
148
149 ret = tv->tv_sec * 1000;
150 ret += tv->tv_usec / 1000;
151 return ret;
152}
153
154/*
155 * Converts a struct timeval to a number of milliseconds.
156 */
157REGPRM2 static inline struct timeval * __tv_from_ms(struct timeval *tv, unsigned long ms)
158{
159 tv->tv_sec = ms / 1000;
160 tv->tv_usec = (ms % 1000) * 1000;
161 return tv;
162}
163
Willy Tarreau776cd872009-03-05 00:34:01 +0100164/* Return a number of 1024Hz ticks between 0 and 1023 for input number of
165 * usecs between 0 and 999999. This function has been optimized to remove
166 * any divide and multiply, as it is completely optimized away by the compiler
167 * on CPUs which don't have a fast multiply. Its avg error rate is 305 ppm,
168 * which is almost twice as low as a direct usec to ms conversion. This version
169 * also has the benefit of returning 1024 for 1000000.
170 */
171REGPRM1 static inline unsigned int __usec_to_1024th(unsigned int usec)
172{
173 return (usec * 1073 + 742516) >> 20;
174}
175
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200176
Willy Tarreau49fa3a12007-05-12 22:29:44 +0200177/**** comparison functions and macros ***********************************/
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200178
179
180/* tv_cmp: compares <tv1> and <tv2> : returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2. */
181REGPRM2 static inline int __tv_cmp(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200182{
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200183 if ((unsigned)tv1->tv_sec < (unsigned)tv2->tv_sec)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200184 return -1;
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200185 else 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_usec < (unsigned)tv2->tv_usec)
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;
191 else
192 return 0;
Willy Tarreau5e8f0662007-02-12 00:59:08 +0100193}
194
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200195/* tv_iseq: compares <tv1> and <tv2> : returns 1 if tv1 == tv2, otherwise 0 */
Willy Tarreau0481c202007-05-13 16:03:27 +0200196#define tv_iseq __tv_iseq
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200197REGPRM2 static inline int __tv_iseq(const struct timeval *tv1, const struct timeval *tv2)
198{
Willy Tarreau0481c202007-05-13 16:03:27 +0200199 return ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) &&
200 ((unsigned)tv1->tv_usec == (unsigned)tv2->tv_usec);
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200201}
202
203/* tv_isgt: compares <tv1> and <tv2> : returns 1 if tv1 > tv2, otherwise 0 */
Willy Tarreau0481c202007-05-13 16:03:27 +0200204#define tv_isgt _tv_isgt
205REGPRM2 int _tv_isgt(const struct timeval *tv1, const struct timeval *tv2);
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200206REGPRM2 static inline int __tv_isgt(const struct timeval *tv1, const struct timeval *tv2)
207{
Willy Tarreau0481c202007-05-13 16:03:27 +0200208 return
209 ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) ?
210 ((unsigned)tv1->tv_usec > (unsigned)tv2->tv_usec) :
211 ((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec);
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200212}
213
214/* tv_isge: compares <tv1> and <tv2> : returns 1 if tv1 >= tv2, otherwise 0 */
Willy Tarreau0481c202007-05-13 16:03:27 +0200215#define tv_isge __tv_isge
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200216REGPRM2 static inline int __tv_isge(const struct timeval *tv1, const struct timeval *tv2)
217{
Willy Tarreau0481c202007-05-13 16:03:27 +0200218 return
219 ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) ?
220 ((unsigned)tv1->tv_usec >= (unsigned)tv2->tv_usec) :
221 ((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec);
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200222}
223
224/* tv_islt: compares <tv1> and <tv2> : returns 1 if tv1 < tv2, otherwise 0 */
Willy Tarreau0481c202007-05-13 16:03:27 +0200225#define tv_islt __tv_islt
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200226REGPRM2 static inline int __tv_islt(const struct timeval *tv1, const struct timeval *tv2)
227{
Willy Tarreau0481c202007-05-13 16:03:27 +0200228 return
229 ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) ?
230 ((unsigned)tv1->tv_usec < (unsigned)tv2->tv_usec) :
231 ((unsigned)tv1->tv_sec < (unsigned)tv2->tv_sec);
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200232}
233
234/* tv_isle: compares <tv1> and <tv2> : returns 1 if tv1 <= tv2, otherwise 0 */
Willy Tarreau0481c202007-05-13 16:03:27 +0200235#define tv_isle _tv_isle
236REGPRM2 int _tv_isle(const struct timeval *tv1, const struct timeval *tv2);
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200237REGPRM2 static inline int __tv_isle(const struct timeval *tv1, const struct timeval *tv2)
238{
Willy Tarreau0481c202007-05-13 16:03:27 +0200239 return
240 ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) ?
241 ((unsigned)tv1->tv_usec <= (unsigned)tv2->tv_usec) :
242 ((unsigned)tv1->tv_sec < (unsigned)tv2->tv_sec);
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200243}
244
Willy Tarreau5e8f0662007-02-12 00:59:08 +0100245/*
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200246 * compares <tv1> and <tv2> modulo 1ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2
247 * Must not be used when either argument is eternity. Use tv_ms_cmp2() for that.
Willy Tarreau5e8f0662007-02-12 00:59:08 +0100248 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200249#define tv_ms_cmp _tv_ms_cmp
250REGPRM2 int _tv_ms_cmp(const struct timeval *tv1, const struct timeval *tv2);
251REGPRM2 static inline int __tv_ms_cmp(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreau5e8f0662007-02-12 00:59:08 +0100252{
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200253 if ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) {
254 if ((unsigned)tv2->tv_usec >= (unsigned)tv1->tv_usec + 1000)
255 return -1;
256 else if ((unsigned)tv1->tv_usec >= (unsigned)tv2->tv_usec + 1000)
257 return 1;
258 else
259 return 0;
260 }
261 else if (((unsigned)tv2->tv_sec > (unsigned)tv1->tv_sec + 1) ||
262 (((unsigned)tv2->tv_sec == (unsigned)tv1->tv_sec + 1) &&
263 ((unsigned)tv2->tv_usec + 1000000 >= (unsigned)tv1->tv_usec + 1000)))
264 return -1;
265 else if (((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec + 1) ||
266 (((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec + 1) &&
267 ((unsigned)tv1->tv_usec + 1000000 >= (unsigned)tv2->tv_usec + 1000)))
Willy Tarreau5e8f0662007-02-12 00:59:08 +0100268 return 1;
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200269 else
Willy Tarreau5e8f0662007-02-12 00:59:08 +0100270 return 0;
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200271}
272
273/*
274 * compares <tv1> and <tv2> modulo 1 ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2,
275 * assuming that TV_ETERNITY is greater than everything.
276 */
277#define tv_ms_cmp2 _tv_ms_cmp2
278REGPRM2 int _tv_ms_cmp2(const struct timeval *tv1, const struct timeval *tv2);
279REGPRM2 static inline int __tv_ms_cmp2(const struct timeval *tv1, const struct timeval *tv2)
280{
281 if (tv_iseternity(tv1))
282 if (tv_iseternity(tv2))
283 return 0; /* same */
284 else
285 return 1; /* tv1 later than tv2 */
286 else if (tv_iseternity(tv2))
287 return -1; /* tv2 later than tv1 */
288 return tv_ms_cmp(tv1, tv2);
289}
290
291/*
292 * compares <tv1> and <tv2> modulo 1 ms: returns 1 if tv1 <= tv2, 0 if tv1 > tv2,
293 * assuming that TV_ETERNITY is greater than everything. Returns 0 if tv1 is
294 * TV_ETERNITY, and always assumes that tv2 != TV_ETERNITY. Designed to replace
295 * occurrences of (tv_ms_cmp2(tv,now) <= 0).
296 */
297#define tv_ms_le2 _tv_ms_le2
298REGPRM2 int _tv_ms_le2(const struct timeval *tv1, const struct timeval *tv2);
299REGPRM2 static inline int __tv_ms_le2(const struct timeval *tv1, const struct timeval *tv2)
300{
301 if (likely((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec + 1))
302 return 0;
303
304 if (likely((unsigned)tv1->tv_sec < (unsigned)tv2->tv_sec))
305 return 1;
306
307 if (likely((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec)) {
308 if ((unsigned)tv2->tv_usec >= (unsigned)tv1->tv_usec + 1000)
309 return 1;
310 else
311 return 0;
312 }
313
314 if (unlikely(((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec + 1) &&
315 ((unsigned)tv1->tv_usec + 1000000 >= (unsigned)tv2->tv_usec + 1000)))
316 return 0;
317 else
Willy Tarreau5e8f0662007-02-12 00:59:08 +0100318 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200319}
320
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200321
322/**** operators **********************************************************/
323
324
Willy Tarreaubaaee002006-06-26 02:48:02 +0200325/*
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200326 * Returns the time in ms elapsed between tv1 and tv2, assuming that tv1<=tv2.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200327 * Must not be used when either argument is eternity.
328 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200329#define tv_ms_elapsed __tv_ms_elapsed
330REGPRM2 unsigned long _tv_ms_elapsed(const struct timeval *tv1, const struct timeval *tv2);
331REGPRM2 static inline unsigned long __tv_ms_elapsed(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200332{
333 unsigned long ret;
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200334
335 ret = ((signed long)(tv2->tv_sec - tv1->tv_sec)) * 1000;
336 ret += ((signed long)(tv2->tv_usec - tv1->tv_usec)) / 1000;
337 return ret;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200338}
339
340/*
341 * returns the remaining time between tv1=now and event=tv2
342 * if tv2 is passed, 0 is returned.
343 * Must not be used when either argument is eternity.
344 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200345
346#define tv_ms_remain __tv_ms_remain
347REGPRM2 unsigned long _tv_ms_remain(const struct timeval *tv1, const struct timeval *tv2);
348REGPRM2 static inline unsigned long __tv_ms_remain(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200349{
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200350 if (tv_ms_cmp(tv1, tv2) >= 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200351 return 0; /* event elapsed */
352
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200353 return __tv_ms_elapsed(tv1, tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200354}
355
Willy Tarreaubaaee002006-06-26 02:48:02 +0200356/*
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200357 * returns the remaining time between tv1=now and event=tv2
358 * if tv2 is passed, 0 is returned.
359 * Returns TIME_ETERNITY if tv2 is eternity.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200360 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200361#define tv_ms_remain2 _tv_ms_remain2
362REGPRM2 unsigned long _tv_ms_remain2(const struct timeval *tv1, const struct timeval *tv2);
363REGPRM2 static inline unsigned long __tv_ms_remain2(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200364{
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200365 if (tv_iseternity(tv2))
366 return TIME_ETERNITY;
367
368 return tv_ms_remain(tv1, tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200369}
370
Willy Tarreau49fa3a12007-05-12 22:29:44 +0200371/*
372 * adds <inc> to <from>, set the result to <tv> and returns a pointer <tv>
373 */
Willy Tarreau0481c202007-05-13 16:03:27 +0200374#define tv_add _tv_add
Willy Tarreau49fa3a12007-05-12 22:29:44 +0200375REGPRM3 struct timeval *_tv_add(struct timeval *tv, const struct timeval *from, const struct timeval *inc);
376REGPRM3 static inline struct timeval *__tv_add(struct timeval *tv, const struct timeval *from, const struct timeval *inc)
377{
378 tv->tv_usec = from->tv_usec + inc->tv_usec;
379 tv->tv_sec = from->tv_sec + inc->tv_sec;
380 if (tv->tv_usec >= 1000000) {
381 tv->tv_usec -= 1000000;
382 tv->tv_sec++;
383 }
384 return tv;
385}
386
387
388/*
Willy Tarreau0481c202007-05-13 16:03:27 +0200389 * If <inc> is set, then add it to <from> and set the result to <tv>, then
390 * return 1, otherwise return 0. It is meant to be used in if conditions.
391 */
392#define tv_add_ifset _tv_add_ifset
393REGPRM3 int _tv_add_ifset(struct timeval *tv, const struct timeval *from, const struct timeval *inc);
394REGPRM3 static inline int __tv_add_ifset(struct timeval *tv, const struct timeval *from, const struct timeval *inc)
395{
396 if (tv_iseternity(inc))
397 return 0;
398 tv->tv_usec = from->tv_usec + inc->tv_usec;
399 tv->tv_sec = from->tv_sec + inc->tv_sec;
400 if (tv->tv_usec >= 1000000) {
401 tv->tv_usec -= 1000000;
402 tv->tv_sec++;
403 }
404 return 1;
405}
406
407/*
Willy Tarreau49fa3a12007-05-12 22:29:44 +0200408 * adds <inc> to <tv> and returns a pointer <tv>
409 */
410REGPRM2 static inline struct timeval *__tv_add2(struct timeval *tv, const struct timeval *inc)
411{
412 tv->tv_usec += inc->tv_usec;
413 tv->tv_sec += inc->tv_sec;
414 if (tv->tv_usec >= 1000000) {
415 tv->tv_usec -= 1000000;
416 tv->tv_sec++;
417 }
418 return tv;
419}
420
421
422/*
423 * Computes the remaining time between tv1=now and event=tv2. if tv2 is passed,
424 * 0 is returned. The result is stored into tv.
425 */
426#define tv_remain _tv_remain
427REGPRM3 struct timeval *_tv_remain(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv);
428REGPRM3 static inline struct timeval *__tv_remain(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv)
429{
430 tv->tv_usec = tv2->tv_usec - tv1->tv_usec;
431 tv->tv_sec = tv2->tv_sec - tv1->tv_sec;
432 if ((signed)tv->tv_sec > 0) {
433 if ((signed)tv->tv_usec < 0) {
434 tv->tv_usec += 1000000;
435 tv->tv_sec--;
436 }
437 } else if (tv->tv_sec == 0) {
438 if ((signed)tv->tv_usec < 0)
439 tv->tv_usec = 0;
440 } else {
441 tv->tv_sec = 0;
442 tv->tv_usec = 0;
443 }
444 return tv;
445}
446
447
448/*
449 * Computes the remaining time between tv1=now and event=tv2. if tv2 is passed,
450 * 0 is returned. The result is stored into tv. Returns ETERNITY if tv2 is
451 * eternity.
452 */
453#define tv_remain2 _tv_remain2
454REGPRM3 struct timeval *_tv_remain2(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv);
455REGPRM3 static inline struct timeval *__tv_remain2(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv)
456{
457 if (tv_iseternity(tv2))
458 return tv_eternity(tv);
459 return __tv_remain(tv1, tv2, tv);
460}
461
462
Willy Tarreaubaaee002006-06-26 02:48:02 +0200463/*
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200464 * adds <ms> ms to <from>, set the result to <tv> and returns a pointer <tv>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200465 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200466#define tv_ms_add _tv_ms_add
467REGPRM3 struct timeval *_tv_ms_add(struct timeval *tv, const struct timeval *from, int ms);
468REGPRM3 static inline struct timeval *__tv_ms_add(struct timeval *tv, const struct timeval *from, int ms)
469{
470 tv->tv_usec = from->tv_usec + (ms % 1000) * 1000;
471 tv->tv_sec = from->tv_sec + (ms / 1000);
472 while (tv->tv_usec >= 1000000) {
473 tv->tv_usec -= 1000000;
474 tv->tv_sec++;
475 }
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200476 return tv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200477}
478
Willy Tarreaubaaee002006-06-26 02:48:02 +0200479
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200480/*
481 * compares <tv1> and <tv2> : returns 1 if <tv1> is before <tv2>, otherwise 0.
482 * This should be very fast because it's used in schedulers.
483 * It has been optimized to return 1 (so call it in a loop which continues
484 * as long as tv1<=tv2)
485 */
486
487#define tv_isbefore(tv1, tv2) \
488 (unlikely((unsigned)(tv1)->tv_sec < (unsigned)(tv2)->tv_sec) ? 1 : \
489 (unlikely((unsigned)(tv1)->tv_sec > (unsigned)(tv2)->tv_sec) ? 0 : \
490 unlikely((unsigned)(tv1)->tv_usec < (unsigned)(tv2)->tv_usec)))
491
492/*
493 * returns the first event between <tv1> and <tv2> into <tvmin>.
494 * a zero tv is ignored. <tvmin> is returned. If <tvmin> is known
495 * to be the same as <tv1> or <tv2>, it is recommended to use
496 * tv_bound instead.
497 */
498#define tv_min(tvmin, tv1, tv2) ({ \
499 if (tv_isbefore(tv1, tv2)) { \
500 *tvmin = *tv1; \
501 } \
502 else { \
503 *tvmin = *tv2; \
504 } \
505 tvmin; \
506})
507
508/*
509 * returns the first event between <tv1> and <tv2> into <tvmin>.
510 * a zero tv is ignored. <tvmin> is returned. This function has been
511 * optimized to be called as tv_min(a,a,b) or tv_min(b,a,b).
512 */
513#define tv_bound(tv1, tv2) ({ \
514 if (tv_isbefore(tv2, tv1)) \
515 *tv1 = *tv2; \
516 tv1; \
517})
Willy Tarreaubaaee002006-06-26 02:48:02 +0200518
Willy Tarreau5ceeb152018-10-17 18:59:53 +0200519/* returns the system's monotonic time in nanoseconds if supported, otherwise zero */
520static inline uint64_t now_mono_time()
521{
Willy Tarreauf6178242019-05-21 19:46:58 +0200522#if (_POSIX_TIMERS > 0) && defined(_POSIX_MONOTONIC_CLOCK)
Willy Tarreau5ceeb152018-10-17 18:59:53 +0200523 struct timespec ts;
524 clock_gettime(CLOCK_MONOTONIC, &ts);
525 return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
526#else
527 return 0;
528#endif
529}
530
531/* returns the current thread's cumulated CPU time in nanoseconds if supported, otherwise zero */
532static inline uint64_t now_cpu_time()
533{
Willy Tarreauf6178242019-05-21 19:46:58 +0200534#if (_POSIX_TIMERS > 0) && defined(_POSIX_THREAD_CPUTIME)
Willy Tarreau5ceeb152018-10-17 18:59:53 +0200535 struct timespec ts;
536 clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts);
537 return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
538#else
539 return 0;
540#endif
541}
542
Willy Tarreau219b8292019-05-20 20:28:34 +0200543/* returns another thread's cumulated CPU time in nanoseconds if supported, otherwise zero */
544static inline uint64_t now_cpu_time_thread(const struct thread_info *thr)
545{
Willy Tarreauf6178242019-05-21 19:46:58 +0200546#if (_POSIX_TIMERS > 0) && defined(_POSIX_THREAD_CPUTIME)
Willy Tarreau219b8292019-05-20 20:28:34 +0200547 struct timespec ts;
548 clock_gettime(thr->clock_id, &ts);
549 return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
550#else
551 return 0;
552#endif
553}
554
Willy Tarreau45a12512011-09-10 16:56:42 +0200555/* Update the idle time value twice a second, to be called after
556 * tv_update_date() when called after poll(). It relies on <before_poll> to be
557 * updated to the system time before calling poll().
558 */
559static inline void measure_idle()
560{
561 /* Let's compute the idle to work ratio. We worked between after_poll
562 * and before_poll, and slept between before_poll and date. The idle_pct
563 * is updated at most twice every second. Note that the current second
564 * rarely changes so we avoid a multiply when not needed.
565 */
566 int delta;
567
568 if ((delta = date.tv_sec - before_poll.tv_sec))
569 delta *= 1000000;
570 idle_time += delta + (date.tv_usec - before_poll.tv_usec);
571
572 if ((delta = date.tv_sec - after_poll.tv_sec))
573 delta *= 1000000;
574 samp_time += delta + (date.tv_usec - after_poll.tv_usec);
575
576 after_poll.tv_sec = date.tv_sec; after_poll.tv_usec = date.tv_usec;
577 if (samp_time < 500000)
578 return;
579
Willy Tarreau81036f22019-05-20 19:24:50 +0200580 ti->idle_pct = (100 * idle_time + samp_time / 2) / samp_time;
Willy Tarreau45a12512011-09-10 16:56:42 +0200581 idle_time = samp_time = 0;
582}
583
Willy Tarreau7e9c4ae2018-10-17 14:31:19 +0200584/* Collect date and time information before calling poll(). This will be used
585 * to count the run time of the past loop and the sleep time of the next poll.
586 */
587static inline void tv_entering_poll()
588{
589 gettimeofday(&before_poll, NULL);
590}
591
592/* Collect date and time information after leaving poll(). <timeout> must be
593 * set to the maximum sleep time passed to poll (in milliseconds), and
594 * <interrupted> must be zero if the poller reached the timeout or non-zero
595 * otherwise, which generally is provided by the poller's return value.
596 */
597static inline void tv_leaving_poll(int timeout, int interrupted)
598{
Willy Tarreau7e9c4ae2018-10-17 14:31:19 +0200599 measure_idle();
Willy Tarreau81036f22019-05-20 19:24:50 +0200600 ti->prev_cpu_time = now_cpu_time();
601 ti->prev_mono_time = now_mono_time();
Willy Tarreau7e9c4ae2018-10-17 14:31:19 +0200602}
603
Willy Tarreau2dd0d472006-06-29 17:53:05 +0200604#endif /* _COMMON_TIME_H */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200605
606/*
607 * Local variables:
608 * c-indent-level: 8
609 * c-basic-offset: 8
610 * End:
611 */