blob: a1664b491f64ab679eca0612c1a1d21d735a9175 [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 Tarreau5ceeb152018-10-17 18:59:53 +020025#include <stdint.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 Tarreaued72d822018-10-17 19:01:24 +020088/* Updates the current thread's statistics about stolen CPU time. The unit for
89 * <stolen> is half-milliseconds.
90 */
91REGPRM1 void report_stolen_time(uint64_t stolen);
92
Willy Tarreau42aae5c2007-04-29 17:43:56 +020093/**** general purpose functions and macros *******************************/
94
95
96/* tv_now: sets <tv> to the current time */
97REGPRM1 static inline struct timeval *tv_now(struct timeval *tv)
98{
99 gettimeofday(tv, NULL);
100 return tv;
101}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200102
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200103/* tv_udpate_date: sets <date> to system time, and sets <now> to something as
104 * close as possible to real time, following a monotonic function. The main
105 * principle consists in detecting backwards and forwards time jumps and adjust
106 * an offset to correct them. This function should be called only once after
107 * each poll. The poll's timeout should be passed in <max_wait>, and the return
108 * value in <interrupted> (a non-zero value means that we have not expired the
109 * timeout).
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200110 */
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200111REGPRM2 void tv_update_date(int max_wait, int interrupted);
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200112
Willy Tarreaubaaee002006-06-26 02:48:02 +0200113/*
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200114 * sets a struct timeval to its highest value so that it can never happen
115 * note that only tv_usec is necessary to detect it since a tv_usec > 999999
116 * is normally not possible.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200117 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200118REGPRM1 static inline struct timeval *tv_eternity(struct timeval *tv)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200119{
Willy Tarreau5f3f15f2013-12-13 09:22:23 +0100120 tv->tv_sec = (typeof(tv->tv_sec))TV_ETERNITY;
121 tv->tv_usec = (typeof(tv->tv_usec))TV_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200122 return tv;
123}
124
125/*
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200126 * sets a struct timeval to 0
127 *
Willy Tarreaubaaee002006-06-26 02:48:02 +0200128 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200129REGPRM1 static inline struct timeval *tv_zero(struct timeval *tv) {
130 tv->tv_sec = tv->tv_usec = 0;
131 return tv;
132}
133
134/*
135 * returns non null if tv is [eternity], otherwise 0.
136 */
Willy Tarreau5f3f15f2013-12-13 09:22:23 +0100137#define tv_iseternity(tv) ((tv)->tv_usec == (typeof((tv)->tv_usec))TV_ETERNITY)
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200138
139/*
Willy Tarreau49fa3a12007-05-12 22:29:44 +0200140 * returns 0 if tv is [eternity], otherwise non-zero.
141 */
Willy Tarreau5f3f15f2013-12-13 09:22:23 +0100142#define tv_isset(tv) ((tv)->tv_usec != (typeof((tv)->tv_usec))TV_ETERNITY)
Willy Tarreau49fa3a12007-05-12 22:29:44 +0200143
144/*
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200145 * returns non null if tv is [0], otherwise 0.
146 */
147#define tv_iszero(tv) (((tv)->tv_sec | (tv)->tv_usec) == 0)
148
Willy Tarreau49fa3a12007-05-12 22:29:44 +0200149/*
150 * Converts a struct timeval to a number of milliseconds.
151 */
152REGPRM1 static inline unsigned long __tv_to_ms(const struct timeval *tv)
153{
154 unsigned long ret;
155
156 ret = tv->tv_sec * 1000;
157 ret += tv->tv_usec / 1000;
158 return ret;
159}
160
161/*
162 * Converts a struct timeval to a number of milliseconds.
163 */
164REGPRM2 static inline struct timeval * __tv_from_ms(struct timeval *tv, unsigned long ms)
165{
166 tv->tv_sec = ms / 1000;
167 tv->tv_usec = (ms % 1000) * 1000;
168 return tv;
169}
170
Willy Tarreau776cd872009-03-05 00:34:01 +0100171/* Return a number of 1024Hz ticks between 0 and 1023 for input number of
172 * usecs between 0 and 999999. This function has been optimized to remove
173 * any divide and multiply, as it is completely optimized away by the compiler
174 * on CPUs which don't have a fast multiply. Its avg error rate is 305 ppm,
175 * which is almost twice as low as a direct usec to ms conversion. This version
176 * also has the benefit of returning 1024 for 1000000.
177 */
178REGPRM1 static inline unsigned int __usec_to_1024th(unsigned int usec)
179{
180 return (usec * 1073 + 742516) >> 20;
181}
182
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200183
Willy Tarreau49fa3a12007-05-12 22:29:44 +0200184/**** comparison functions and macros ***********************************/
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200185
186
187/* tv_cmp: compares <tv1> and <tv2> : returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2. */
188REGPRM2 static inline int __tv_cmp(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200189{
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200190 if ((unsigned)tv1->tv_sec < (unsigned)tv2->tv_sec)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200191 return -1;
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200192 else if ((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200193 return 1;
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200194 else if ((unsigned)tv1->tv_usec < (unsigned)tv2->tv_usec)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200195 return -1;
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200196 else if ((unsigned)tv1->tv_usec > (unsigned)tv2->tv_usec)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200197 return 1;
198 else
199 return 0;
Willy Tarreau5e8f0662007-02-12 00:59:08 +0100200}
201
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200202/* tv_iseq: compares <tv1> and <tv2> : returns 1 if tv1 == tv2, otherwise 0 */
Willy Tarreau0481c202007-05-13 16:03:27 +0200203#define tv_iseq __tv_iseq
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200204REGPRM2 static inline int __tv_iseq(const struct timeval *tv1, const struct timeval *tv2)
205{
Willy Tarreau0481c202007-05-13 16:03:27 +0200206 return ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) &&
207 ((unsigned)tv1->tv_usec == (unsigned)tv2->tv_usec);
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200208}
209
210/* tv_isgt: compares <tv1> and <tv2> : returns 1 if tv1 > tv2, otherwise 0 */
Willy Tarreau0481c202007-05-13 16:03:27 +0200211#define tv_isgt _tv_isgt
212REGPRM2 int _tv_isgt(const struct timeval *tv1, const struct timeval *tv2);
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200213REGPRM2 static inline int __tv_isgt(const struct timeval *tv1, const struct timeval *tv2)
214{
Willy Tarreau0481c202007-05-13 16:03:27 +0200215 return
216 ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) ?
217 ((unsigned)tv1->tv_usec > (unsigned)tv2->tv_usec) :
218 ((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec);
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200219}
220
221/* tv_isge: compares <tv1> and <tv2> : returns 1 if tv1 >= tv2, otherwise 0 */
Willy Tarreau0481c202007-05-13 16:03:27 +0200222#define tv_isge __tv_isge
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200223REGPRM2 static inline int __tv_isge(const struct timeval *tv1, const struct timeval *tv2)
224{
Willy Tarreau0481c202007-05-13 16:03:27 +0200225 return
226 ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) ?
227 ((unsigned)tv1->tv_usec >= (unsigned)tv2->tv_usec) :
228 ((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec);
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200229}
230
231/* tv_islt: compares <tv1> and <tv2> : returns 1 if tv1 < tv2, otherwise 0 */
Willy Tarreau0481c202007-05-13 16:03:27 +0200232#define tv_islt __tv_islt
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200233REGPRM2 static inline int __tv_islt(const struct timeval *tv1, const struct timeval *tv2)
234{
Willy Tarreau0481c202007-05-13 16:03:27 +0200235 return
236 ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) ?
237 ((unsigned)tv1->tv_usec < (unsigned)tv2->tv_usec) :
238 ((unsigned)tv1->tv_sec < (unsigned)tv2->tv_sec);
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200239}
240
241/* tv_isle: compares <tv1> and <tv2> : returns 1 if tv1 <= tv2, otherwise 0 */
Willy Tarreau0481c202007-05-13 16:03:27 +0200242#define tv_isle _tv_isle
243REGPRM2 int _tv_isle(const struct timeval *tv1, const struct timeval *tv2);
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200244REGPRM2 static inline int __tv_isle(const struct timeval *tv1, const struct timeval *tv2)
245{
Willy Tarreau0481c202007-05-13 16:03:27 +0200246 return
247 ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) ?
248 ((unsigned)tv1->tv_usec <= (unsigned)tv2->tv_usec) :
249 ((unsigned)tv1->tv_sec < (unsigned)tv2->tv_sec);
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200250}
251
Willy Tarreau5e8f0662007-02-12 00:59:08 +0100252/*
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200253 * compares <tv1> and <tv2> modulo 1ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2
254 * Must not be used when either argument is eternity. Use tv_ms_cmp2() for that.
Willy Tarreau5e8f0662007-02-12 00:59:08 +0100255 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200256#define tv_ms_cmp _tv_ms_cmp
257REGPRM2 int _tv_ms_cmp(const struct timeval *tv1, const struct timeval *tv2);
258REGPRM2 static inline int __tv_ms_cmp(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreau5e8f0662007-02-12 00:59:08 +0100259{
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200260 if ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) {
261 if ((unsigned)tv2->tv_usec >= (unsigned)tv1->tv_usec + 1000)
262 return -1;
263 else if ((unsigned)tv1->tv_usec >= (unsigned)tv2->tv_usec + 1000)
264 return 1;
265 else
266 return 0;
267 }
268 else if (((unsigned)tv2->tv_sec > (unsigned)tv1->tv_sec + 1) ||
269 (((unsigned)tv2->tv_sec == (unsigned)tv1->tv_sec + 1) &&
270 ((unsigned)tv2->tv_usec + 1000000 >= (unsigned)tv1->tv_usec + 1000)))
271 return -1;
272 else if (((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec + 1) ||
273 (((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec + 1) &&
274 ((unsigned)tv1->tv_usec + 1000000 >= (unsigned)tv2->tv_usec + 1000)))
Willy Tarreau5e8f0662007-02-12 00:59:08 +0100275 return 1;
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200276 else
Willy Tarreau5e8f0662007-02-12 00:59:08 +0100277 return 0;
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200278}
279
280/*
281 * compares <tv1> and <tv2> modulo 1 ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2,
282 * assuming that TV_ETERNITY is greater than everything.
283 */
284#define tv_ms_cmp2 _tv_ms_cmp2
285REGPRM2 int _tv_ms_cmp2(const struct timeval *tv1, const struct timeval *tv2);
286REGPRM2 static inline int __tv_ms_cmp2(const struct timeval *tv1, const struct timeval *tv2)
287{
288 if (tv_iseternity(tv1))
289 if (tv_iseternity(tv2))
290 return 0; /* same */
291 else
292 return 1; /* tv1 later than tv2 */
293 else if (tv_iseternity(tv2))
294 return -1; /* tv2 later than tv1 */
295 return tv_ms_cmp(tv1, tv2);
296}
297
298/*
299 * compares <tv1> and <tv2> modulo 1 ms: returns 1 if tv1 <= tv2, 0 if tv1 > tv2,
300 * assuming that TV_ETERNITY is greater than everything. Returns 0 if tv1 is
301 * TV_ETERNITY, and always assumes that tv2 != TV_ETERNITY. Designed to replace
302 * occurrences of (tv_ms_cmp2(tv,now) <= 0).
303 */
304#define tv_ms_le2 _tv_ms_le2
305REGPRM2 int _tv_ms_le2(const struct timeval *tv1, const struct timeval *tv2);
306REGPRM2 static inline int __tv_ms_le2(const struct timeval *tv1, const struct timeval *tv2)
307{
308 if (likely((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec + 1))
309 return 0;
310
311 if (likely((unsigned)tv1->tv_sec < (unsigned)tv2->tv_sec))
312 return 1;
313
314 if (likely((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec)) {
315 if ((unsigned)tv2->tv_usec >= (unsigned)tv1->tv_usec + 1000)
316 return 1;
317 else
318 return 0;
319 }
320
321 if (unlikely(((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec + 1) &&
322 ((unsigned)tv1->tv_usec + 1000000 >= (unsigned)tv2->tv_usec + 1000)))
323 return 0;
324 else
Willy Tarreau5e8f0662007-02-12 00:59:08 +0100325 return 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200326}
327
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200328
329/**** operators **********************************************************/
330
331
Willy Tarreaubaaee002006-06-26 02:48:02 +0200332/*
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200333 * Returns the time in ms elapsed between tv1 and tv2, assuming that tv1<=tv2.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200334 * Must not be used when either argument is eternity.
335 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200336#define tv_ms_elapsed __tv_ms_elapsed
337REGPRM2 unsigned long _tv_ms_elapsed(const struct timeval *tv1, const struct timeval *tv2);
338REGPRM2 static inline unsigned long __tv_ms_elapsed(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200339{
340 unsigned long ret;
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200341
342 ret = ((signed long)(tv2->tv_sec - tv1->tv_sec)) * 1000;
343 ret += ((signed long)(tv2->tv_usec - tv1->tv_usec)) / 1000;
344 return ret;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200345}
346
347/*
348 * returns the remaining time between tv1=now and event=tv2
349 * if tv2 is passed, 0 is returned.
350 * Must not be used when either argument is eternity.
351 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200352
353#define tv_ms_remain __tv_ms_remain
354REGPRM2 unsigned long _tv_ms_remain(const struct timeval *tv1, const struct timeval *tv2);
355REGPRM2 static inline unsigned long __tv_ms_remain(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200356{
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200357 if (tv_ms_cmp(tv1, tv2) >= 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200358 return 0; /* event elapsed */
359
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200360 return __tv_ms_elapsed(tv1, tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200361}
362
Willy Tarreaubaaee002006-06-26 02:48:02 +0200363/*
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200364 * returns the remaining time between tv1=now and event=tv2
365 * if tv2 is passed, 0 is returned.
366 * Returns TIME_ETERNITY if tv2 is eternity.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200367 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200368#define tv_ms_remain2 _tv_ms_remain2
369REGPRM2 unsigned long _tv_ms_remain2(const struct timeval *tv1, const struct timeval *tv2);
370REGPRM2 static inline unsigned long __tv_ms_remain2(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200371{
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200372 if (tv_iseternity(tv2))
373 return TIME_ETERNITY;
374
375 return tv_ms_remain(tv1, tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200376}
377
Willy Tarreau49fa3a12007-05-12 22:29:44 +0200378/*
379 * adds <inc> to <from>, set the result to <tv> and returns a pointer <tv>
380 */
Willy Tarreau0481c202007-05-13 16:03:27 +0200381#define tv_add _tv_add
Willy Tarreau49fa3a12007-05-12 22:29:44 +0200382REGPRM3 struct timeval *_tv_add(struct timeval *tv, const struct timeval *from, const struct timeval *inc);
383REGPRM3 static inline struct timeval *__tv_add(struct timeval *tv, const struct timeval *from, const struct timeval *inc)
384{
385 tv->tv_usec = from->tv_usec + inc->tv_usec;
386 tv->tv_sec = from->tv_sec + inc->tv_sec;
387 if (tv->tv_usec >= 1000000) {
388 tv->tv_usec -= 1000000;
389 tv->tv_sec++;
390 }
391 return tv;
392}
393
394
395/*
Willy Tarreau0481c202007-05-13 16:03:27 +0200396 * If <inc> is set, then add it to <from> and set the result to <tv>, then
397 * return 1, otherwise return 0. It is meant to be used in if conditions.
398 */
399#define tv_add_ifset _tv_add_ifset
400REGPRM3 int _tv_add_ifset(struct timeval *tv, const struct timeval *from, const struct timeval *inc);
401REGPRM3 static inline int __tv_add_ifset(struct timeval *tv, const struct timeval *from, const struct timeval *inc)
402{
403 if (tv_iseternity(inc))
404 return 0;
405 tv->tv_usec = from->tv_usec + inc->tv_usec;
406 tv->tv_sec = from->tv_sec + inc->tv_sec;
407 if (tv->tv_usec >= 1000000) {
408 tv->tv_usec -= 1000000;
409 tv->tv_sec++;
410 }
411 return 1;
412}
413
414/*
Willy Tarreau49fa3a12007-05-12 22:29:44 +0200415 * adds <inc> to <tv> and returns a pointer <tv>
416 */
417REGPRM2 static inline struct timeval *__tv_add2(struct timeval *tv, const struct timeval *inc)
418{
419 tv->tv_usec += inc->tv_usec;
420 tv->tv_sec += inc->tv_sec;
421 if (tv->tv_usec >= 1000000) {
422 tv->tv_usec -= 1000000;
423 tv->tv_sec++;
424 }
425 return tv;
426}
427
428
429/*
430 * Computes the remaining time between tv1=now and event=tv2. if tv2 is passed,
431 * 0 is returned. The result is stored into tv.
432 */
433#define tv_remain _tv_remain
434REGPRM3 struct timeval *_tv_remain(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv);
435REGPRM3 static inline struct timeval *__tv_remain(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv)
436{
437 tv->tv_usec = tv2->tv_usec - tv1->tv_usec;
438 tv->tv_sec = tv2->tv_sec - tv1->tv_sec;
439 if ((signed)tv->tv_sec > 0) {
440 if ((signed)tv->tv_usec < 0) {
441 tv->tv_usec += 1000000;
442 tv->tv_sec--;
443 }
444 } else if (tv->tv_sec == 0) {
445 if ((signed)tv->tv_usec < 0)
446 tv->tv_usec = 0;
447 } else {
448 tv->tv_sec = 0;
449 tv->tv_usec = 0;
450 }
451 return tv;
452}
453
454
455/*
456 * Computes the remaining time between tv1=now and event=tv2. if tv2 is passed,
457 * 0 is returned. The result is stored into tv. Returns ETERNITY if tv2 is
458 * eternity.
459 */
460#define tv_remain2 _tv_remain2
461REGPRM3 struct timeval *_tv_remain2(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv);
462REGPRM3 static inline struct timeval *__tv_remain2(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv)
463{
464 if (tv_iseternity(tv2))
465 return tv_eternity(tv);
466 return __tv_remain(tv1, tv2, tv);
467}
468
469
Willy Tarreaubaaee002006-06-26 02:48:02 +0200470/*
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200471 * adds <ms> ms to <from>, set the result to <tv> and returns a pointer <tv>
Willy Tarreaubaaee002006-06-26 02:48:02 +0200472 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200473#define tv_ms_add _tv_ms_add
474REGPRM3 struct timeval *_tv_ms_add(struct timeval *tv, const struct timeval *from, int ms);
475REGPRM3 static inline struct timeval *__tv_ms_add(struct timeval *tv, const struct timeval *from, int ms)
476{
477 tv->tv_usec = from->tv_usec + (ms % 1000) * 1000;
478 tv->tv_sec = from->tv_sec + (ms / 1000);
479 while (tv->tv_usec >= 1000000) {
480 tv->tv_usec -= 1000000;
481 tv->tv_sec++;
482 }
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200483 return tv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200484}
485
Willy Tarreaubaaee002006-06-26 02:48:02 +0200486
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200487/*
488 * compares <tv1> and <tv2> : returns 1 if <tv1> is before <tv2>, otherwise 0.
489 * This should be very fast because it's used in schedulers.
490 * It has been optimized to return 1 (so call it in a loop which continues
491 * as long as tv1<=tv2)
492 */
493
494#define tv_isbefore(tv1, tv2) \
495 (unlikely((unsigned)(tv1)->tv_sec < (unsigned)(tv2)->tv_sec) ? 1 : \
496 (unlikely((unsigned)(tv1)->tv_sec > (unsigned)(tv2)->tv_sec) ? 0 : \
497 unlikely((unsigned)(tv1)->tv_usec < (unsigned)(tv2)->tv_usec)))
498
499/*
500 * returns the first event between <tv1> and <tv2> into <tvmin>.
501 * a zero tv is ignored. <tvmin> is returned. If <tvmin> is known
502 * to be the same as <tv1> or <tv2>, it is recommended to use
503 * tv_bound instead.
504 */
505#define tv_min(tvmin, tv1, tv2) ({ \
506 if (tv_isbefore(tv1, tv2)) { \
507 *tvmin = *tv1; \
508 } \
509 else { \
510 *tvmin = *tv2; \
511 } \
512 tvmin; \
513})
514
515/*
516 * returns the first event between <tv1> and <tv2> into <tvmin>.
517 * a zero tv is ignored. <tvmin> is returned. This function has been
518 * optimized to be called as tv_min(a,a,b) or tv_min(b,a,b).
519 */
520#define tv_bound(tv1, tv2) ({ \
521 if (tv_isbefore(tv2, tv1)) \
522 *tv1 = *tv2; \
523 tv1; \
524})
Willy Tarreaubaaee002006-06-26 02:48:02 +0200525
Willy Tarreau5ceeb152018-10-17 18:59:53 +0200526/* returns the system's monotonic time in nanoseconds if supported, otherwise zero */
527static inline uint64_t now_mono_time()
528{
529#if defined(_POSIX_TIMERS) && defined(_POSIX_MONOTONIC_CLOCK)
530 struct timespec ts;
531 clock_gettime(CLOCK_MONOTONIC, &ts);
532 return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
533#else
534 return 0;
535#endif
536}
537
538/* returns the current thread's cumulated CPU time in nanoseconds if supported, otherwise zero */
539static inline uint64_t now_cpu_time()
540{
541#if defined(_POSIX_TIMERS) && defined(_POSIX_THREAD_CPUTIME)
542 struct timespec ts;
543 clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts);
544 return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
545#else
546 return 0;
547#endif
548}
549
Willy Tarreau45a12512011-09-10 16:56:42 +0200550/* Update the idle time value twice a second, to be called after
551 * tv_update_date() when called after poll(). It relies on <before_poll> to be
552 * updated to the system time before calling poll().
553 */
554static inline void measure_idle()
555{
556 /* Let's compute the idle to work ratio. We worked between after_poll
557 * and before_poll, and slept between before_poll and date. The idle_pct
558 * is updated at most twice every second. Note that the current second
559 * rarely changes so we avoid a multiply when not needed.
560 */
561 int delta;
562
563 if ((delta = date.tv_sec - before_poll.tv_sec))
564 delta *= 1000000;
565 idle_time += delta + (date.tv_usec - before_poll.tv_usec);
566
567 if ((delta = date.tv_sec - after_poll.tv_sec))
568 delta *= 1000000;
569 samp_time += delta + (date.tv_usec - after_poll.tv_usec);
570
571 after_poll.tv_sec = date.tv_sec; after_poll.tv_usec = date.tv_usec;
572 if (samp_time < 500000)
573 return;
574
575 idle_pct = (100 * idle_time + samp_time / 2) / samp_time;
576 idle_time = samp_time = 0;
577}
578
Willy Tarreau7e9c4ae2018-10-17 14:31:19 +0200579/* Collect date and time information before calling poll(). This will be used
580 * to count the run time of the past loop and the sleep time of the next poll.
581 */
582static inline void tv_entering_poll()
583{
Willy Tarreaued72d822018-10-17 19:01:24 +0200584 uint64_t new_mono_time;
585 uint64_t new_cpu_time;
586 int64_t stolen;
587
588 new_cpu_time = now_cpu_time();
589 new_mono_time = now_mono_time();
590
591 if (prev_cpu_time && prev_mono_time) {
592 new_cpu_time -= prev_cpu_time;
593 new_mono_time -= prev_mono_time;
594 stolen = new_mono_time - new_cpu_time;
595 if (stolen >= 500000) {
596 stolen /= 500000;
597 /* more than half a millisecond difference might
598 * indicate an undesired preemption.
599 */
600 report_stolen_time(stolen);
601 }
602 }
603
Willy Tarreau7e9c4ae2018-10-17 14:31:19 +0200604 gettimeofday(&before_poll, NULL);
605}
606
607/* Collect date and time information after leaving poll(). <timeout> must be
608 * set to the maximum sleep time passed to poll (in milliseconds), and
609 * <interrupted> must be zero if the poller reached the timeout or non-zero
610 * otherwise, which generally is provided by the poller's return value.
611 */
612static inline void tv_leaving_poll(int timeout, int interrupted)
613{
614 tv_update_date(timeout, interrupted);
615 measure_idle();
Willy Tarreaued72d822018-10-17 19:01:24 +0200616 prev_cpu_time = now_cpu_time();
617 prev_mono_time = now_mono_time();
Willy Tarreau7e9c4ae2018-10-17 14:31:19 +0200618}
619
Willy Tarreau2dd0d472006-06-29 17:53:05 +0200620#endif /* _COMMON_TIME_H */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200621
622/*
623 * Local variables:
624 * c-indent-level: 8
625 * c-basic-offset: 8
626 * End:
627 */