Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
Willy Tarreau | 45a1251 | 2011-09-10 16:56:42 +0200 | [diff] [blame] | 2 | * 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 Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 21 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 22 | #ifndef _COMMON_TIME_H |
| 23 | #define _COMMON_TIME_H |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 24 | |
Willy Tarreau | a1bd1fa | 2019-03-29 17:26:33 +0100 | [diff] [blame] | 25 | #include <inttypes.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 26 | #include <stdlib.h> |
Willy Tarreau | 5ceeb15 | 2018-10-17 18:59:53 +0200 | [diff] [blame] | 27 | #include <unistd.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 28 | #include <sys/time.h> |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 29 | #include <common/config.h> |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 30 | #include <common/standard.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 31 | |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 32 | /* 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 Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 44 | /* 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 Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 49 | |
| 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 Faulet | 9a65571 | 2017-05-11 11:00:15 +0200 | [diff] [blame] | 54 | extern THREAD_LOCAL unsigned int curr_sec_ms; /* millisecond of current second (0..999) */ |
| 55 | extern THREAD_LOCAL unsigned int ms_left_scaled; /* milliseconds left for current second (0..2^32-1) */ |
| 56 | extern THREAD_LOCAL unsigned int curr_sec_ms_scaled; /* millisecond of current second (0..2^32-1) */ |
| 57 | extern THREAD_LOCAL unsigned int now_ms; /* internal date in milliseconds (may wrap) */ |
| 58 | extern THREAD_LOCAL unsigned int samp_time; /* total elapsed time over current sample */ |
| 59 | extern THREAD_LOCAL unsigned int idle_time; /* total idle time over current sample */ |
| 60 | extern THREAD_LOCAL unsigned int idle_pct; /* idle to total ratio over last sample (percent) */ |
| 61 | extern THREAD_LOCAL struct timeval now; /* internal date is a monotonic function of real clock */ |
| 62 | extern THREAD_LOCAL struct timeval date; /* the real current date */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 63 | extern struct timeval start_date; /* the process's start date */ |
Christopher Faulet | 9a65571 | 2017-05-11 11:00:15 +0200 | [diff] [blame] | 64 | extern THREAD_LOCAL struct timeval before_poll; /* system date before calling poll() */ |
| 65 | extern THREAD_LOCAL struct timeval after_poll; /* system date after leaving poll() */ |
Willy Tarreau | ed72d82 | 2018-10-17 19:01:24 +0200 | [diff] [blame] | 66 | extern THREAD_LOCAL uint64_t prev_cpu_time; /* previous per thread CPU time */ |
| 67 | extern THREAD_LOCAL uint64_t prev_mono_time; /* previous system wide monotonic time */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 68 | |
| 69 | |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 70 | /**** exported functions *************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 71 | /* |
| 72 | * adds <ms> ms to <from>, set the result to <tv> and returns a pointer <tv> |
| 73 | */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 74 | REGPRM3 struct timeval *tv_ms_add(struct timeval *tv, const struct timeval *from, int ms); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 75 | |
| 76 | /* |
| 77 | * compares <tv1> and <tv2> modulo 1ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2 |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 78 | * Must not be used when either argument is eternity. Use tv_ms_cmp2() for that. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 79 | */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 80 | REGPRM2 int tv_ms_cmp(const struct timeval *tv1, const struct timeval *tv2); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 81 | |
| 82 | /* |
Willy Tarreau | 8d7d149 | 2007-04-29 10:50:43 +0200 | [diff] [blame] | 83 | * 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 Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 85 | */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 86 | REGPRM2 int tv_ms_cmp2(const struct timeval *tv1, const struct timeval *tv2); |
Willy Tarreau | 5e8f066 | 2007-02-12 00:59:08 +0100 | [diff] [blame] | 87 | |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 88 | /**** general purpose functions and macros *******************************/ |
| 89 | |
| 90 | |
| 91 | /* tv_now: sets <tv> to the current time */ |
| 92 | REGPRM1 static inline struct timeval *tv_now(struct timeval *tv) |
| 93 | { |
| 94 | gettimeofday(tv, NULL); |
| 95 | return tv; |
| 96 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 97 | |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 98 | /* 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 Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 105 | */ |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 106 | REGPRM2 void tv_update_date(int max_wait, int interrupted); |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 107 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 108 | /* |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 109 | * 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 Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 112 | */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 113 | REGPRM1 static inline struct timeval *tv_eternity(struct timeval *tv) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 114 | { |
Willy Tarreau | 5f3f15f | 2013-12-13 09:22:23 +0100 | [diff] [blame] | 115 | tv->tv_sec = (typeof(tv->tv_sec))TV_ETERNITY; |
| 116 | tv->tv_usec = (typeof(tv->tv_usec))TV_ETERNITY; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 117 | return tv; |
| 118 | } |
| 119 | |
| 120 | /* |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 121 | * sets a struct timeval to 0 |
| 122 | * |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 123 | */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 124 | REGPRM1 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 Tarreau | 5f3f15f | 2013-12-13 09:22:23 +0100 | [diff] [blame] | 132 | #define tv_iseternity(tv) ((tv)->tv_usec == (typeof((tv)->tv_usec))TV_ETERNITY) |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 133 | |
| 134 | /* |
Willy Tarreau | 49fa3a1 | 2007-05-12 22:29:44 +0200 | [diff] [blame] | 135 | * returns 0 if tv is [eternity], otherwise non-zero. |
| 136 | */ |
Willy Tarreau | 5f3f15f | 2013-12-13 09:22:23 +0100 | [diff] [blame] | 137 | #define tv_isset(tv) ((tv)->tv_usec != (typeof((tv)->tv_usec))TV_ETERNITY) |
Willy Tarreau | 49fa3a1 | 2007-05-12 22:29:44 +0200 | [diff] [blame] | 138 | |
| 139 | /* |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 140 | * returns non null if tv is [0], otherwise 0. |
| 141 | */ |
| 142 | #define tv_iszero(tv) (((tv)->tv_sec | (tv)->tv_usec) == 0) |
| 143 | |
Willy Tarreau | 49fa3a1 | 2007-05-12 22:29:44 +0200 | [diff] [blame] | 144 | /* |
| 145 | * Converts a struct timeval to a number of milliseconds. |
| 146 | */ |
| 147 | REGPRM1 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 | */ |
| 159 | REGPRM2 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 Tarreau | 776cd87 | 2009-03-05 00:34:01 +0100 | [diff] [blame] | 166 | /* 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 | */ |
| 173 | REGPRM1 static inline unsigned int __usec_to_1024th(unsigned int usec) |
| 174 | { |
| 175 | return (usec * 1073 + 742516) >> 20; |
| 176 | } |
| 177 | |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 178 | |
Willy Tarreau | 49fa3a1 | 2007-05-12 22:29:44 +0200 | [diff] [blame] | 179 | /**** comparison functions and macros ***********************************/ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 180 | |
| 181 | |
| 182 | /* tv_cmp: compares <tv1> and <tv2> : returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2. */ |
| 183 | REGPRM2 static inline int __tv_cmp(const struct timeval *tv1, const struct timeval *tv2) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 184 | { |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 185 | if ((unsigned)tv1->tv_sec < (unsigned)tv2->tv_sec) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 186 | return -1; |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 187 | else if ((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 188 | return 1; |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 189 | else if ((unsigned)tv1->tv_usec < (unsigned)tv2->tv_usec) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 190 | return -1; |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 191 | else if ((unsigned)tv1->tv_usec > (unsigned)tv2->tv_usec) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 192 | return 1; |
| 193 | else |
| 194 | return 0; |
Willy Tarreau | 5e8f066 | 2007-02-12 00:59:08 +0100 | [diff] [blame] | 195 | } |
| 196 | |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 197 | /* tv_iseq: compares <tv1> and <tv2> : returns 1 if tv1 == tv2, otherwise 0 */ |
Willy Tarreau | 0481c20 | 2007-05-13 16:03:27 +0200 | [diff] [blame] | 198 | #define tv_iseq __tv_iseq |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 199 | REGPRM2 static inline int __tv_iseq(const struct timeval *tv1, const struct timeval *tv2) |
| 200 | { |
Willy Tarreau | 0481c20 | 2007-05-13 16:03:27 +0200 | [diff] [blame] | 201 | return ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) && |
| 202 | ((unsigned)tv1->tv_usec == (unsigned)tv2->tv_usec); |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | /* tv_isgt: compares <tv1> and <tv2> : returns 1 if tv1 > tv2, otherwise 0 */ |
Willy Tarreau | 0481c20 | 2007-05-13 16:03:27 +0200 | [diff] [blame] | 206 | #define tv_isgt _tv_isgt |
| 207 | REGPRM2 int _tv_isgt(const struct timeval *tv1, const struct timeval *tv2); |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 208 | REGPRM2 static inline int __tv_isgt(const struct timeval *tv1, const struct timeval *tv2) |
| 209 | { |
Willy Tarreau | 0481c20 | 2007-05-13 16:03:27 +0200 | [diff] [blame] | 210 | 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 Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | /* tv_isge: compares <tv1> and <tv2> : returns 1 if tv1 >= tv2, otherwise 0 */ |
Willy Tarreau | 0481c20 | 2007-05-13 16:03:27 +0200 | [diff] [blame] | 217 | #define tv_isge __tv_isge |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 218 | REGPRM2 static inline int __tv_isge(const struct timeval *tv1, const struct timeval *tv2) |
| 219 | { |
Willy Tarreau | 0481c20 | 2007-05-13 16:03:27 +0200 | [diff] [blame] | 220 | 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 Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | /* tv_islt: compares <tv1> and <tv2> : returns 1 if tv1 < tv2, otherwise 0 */ |
Willy Tarreau | 0481c20 | 2007-05-13 16:03:27 +0200 | [diff] [blame] | 227 | #define tv_islt __tv_islt |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 228 | REGPRM2 static inline int __tv_islt(const struct timeval *tv1, const struct timeval *tv2) |
| 229 | { |
Willy Tarreau | 0481c20 | 2007-05-13 16:03:27 +0200 | [diff] [blame] | 230 | 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 Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | /* tv_isle: compares <tv1> and <tv2> : returns 1 if tv1 <= tv2, otherwise 0 */ |
Willy Tarreau | 0481c20 | 2007-05-13 16:03:27 +0200 | [diff] [blame] | 237 | #define tv_isle _tv_isle |
| 238 | REGPRM2 int _tv_isle(const struct timeval *tv1, const struct timeval *tv2); |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 239 | REGPRM2 static inline int __tv_isle(const struct timeval *tv1, const struct timeval *tv2) |
| 240 | { |
Willy Tarreau | 0481c20 | 2007-05-13 16:03:27 +0200 | [diff] [blame] | 241 | 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 Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 245 | } |
| 246 | |
Willy Tarreau | 5e8f066 | 2007-02-12 00:59:08 +0100 | [diff] [blame] | 247 | /* |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 248 | * 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 Tarreau | 5e8f066 | 2007-02-12 00:59:08 +0100 | [diff] [blame] | 250 | */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 251 | #define tv_ms_cmp _tv_ms_cmp |
| 252 | REGPRM2 int _tv_ms_cmp(const struct timeval *tv1, const struct timeval *tv2); |
| 253 | REGPRM2 static inline int __tv_ms_cmp(const struct timeval *tv1, const struct timeval *tv2) |
Willy Tarreau | 5e8f066 | 2007-02-12 00:59:08 +0100 | [diff] [blame] | 254 | { |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 255 | 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 Tarreau | 5e8f066 | 2007-02-12 00:59:08 +0100 | [diff] [blame] | 270 | return 1; |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 271 | else |
Willy Tarreau | 5e8f066 | 2007-02-12 00:59:08 +0100 | [diff] [blame] | 272 | return 0; |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 273 | } |
| 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 |
| 280 | REGPRM2 int _tv_ms_cmp2(const struct timeval *tv1, const struct timeval *tv2); |
| 281 | REGPRM2 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 |
| 300 | REGPRM2 int _tv_ms_le2(const struct timeval *tv1, const struct timeval *tv2); |
| 301 | REGPRM2 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 Tarreau | 5e8f066 | 2007-02-12 00:59:08 +0100 | [diff] [blame] | 320 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 321 | } |
| 322 | |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 323 | |
| 324 | /**** operators **********************************************************/ |
| 325 | |
| 326 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 327 | /* |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 328 | * Returns the time in ms elapsed between tv1 and tv2, assuming that tv1<=tv2. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 329 | * Must not be used when either argument is eternity. |
| 330 | */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 331 | #define tv_ms_elapsed __tv_ms_elapsed |
| 332 | REGPRM2 unsigned long _tv_ms_elapsed(const struct timeval *tv1, const struct timeval *tv2); |
| 333 | REGPRM2 static inline unsigned long __tv_ms_elapsed(const struct timeval *tv1, const struct timeval *tv2) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 334 | { |
| 335 | unsigned long ret; |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 336 | |
| 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 Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 340 | } |
| 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 Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 347 | |
| 348 | #define tv_ms_remain __tv_ms_remain |
| 349 | REGPRM2 unsigned long _tv_ms_remain(const struct timeval *tv1, const struct timeval *tv2); |
| 350 | REGPRM2 static inline unsigned long __tv_ms_remain(const struct timeval *tv1, const struct timeval *tv2) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 351 | { |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 352 | if (tv_ms_cmp(tv1, tv2) >= 0) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 353 | return 0; /* event elapsed */ |
| 354 | |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 355 | return __tv_ms_elapsed(tv1, tv2); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 356 | } |
| 357 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 358 | /* |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 359 | * 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 Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 362 | */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 363 | #define tv_ms_remain2 _tv_ms_remain2 |
| 364 | REGPRM2 unsigned long _tv_ms_remain2(const struct timeval *tv1, const struct timeval *tv2); |
| 365 | REGPRM2 static inline unsigned long __tv_ms_remain2(const struct timeval *tv1, const struct timeval *tv2) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 366 | { |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 367 | if (tv_iseternity(tv2)) |
| 368 | return TIME_ETERNITY; |
| 369 | |
| 370 | return tv_ms_remain(tv1, tv2); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 371 | } |
| 372 | |
Willy Tarreau | 49fa3a1 | 2007-05-12 22:29:44 +0200 | [diff] [blame] | 373 | /* |
| 374 | * adds <inc> to <from>, set the result to <tv> and returns a pointer <tv> |
| 375 | */ |
Willy Tarreau | 0481c20 | 2007-05-13 16:03:27 +0200 | [diff] [blame] | 376 | #define tv_add _tv_add |
Willy Tarreau | 49fa3a1 | 2007-05-12 22:29:44 +0200 | [diff] [blame] | 377 | REGPRM3 struct timeval *_tv_add(struct timeval *tv, const struct timeval *from, const struct timeval *inc); |
| 378 | REGPRM3 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 Tarreau | 0481c20 | 2007-05-13 16:03:27 +0200 | [diff] [blame] | 391 | * 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 |
| 395 | REGPRM3 int _tv_add_ifset(struct timeval *tv, const struct timeval *from, const struct timeval *inc); |
| 396 | REGPRM3 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 Tarreau | 49fa3a1 | 2007-05-12 22:29:44 +0200 | [diff] [blame] | 410 | * adds <inc> to <tv> and returns a pointer <tv> |
| 411 | */ |
| 412 | REGPRM2 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 |
| 429 | REGPRM3 struct timeval *_tv_remain(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv); |
| 430 | REGPRM3 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 |
| 456 | REGPRM3 struct timeval *_tv_remain2(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv); |
| 457 | REGPRM3 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 Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 465 | /* |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 466 | * adds <ms> ms to <from>, set the result to <tv> and returns a pointer <tv> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 467 | */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 468 | #define tv_ms_add _tv_ms_add |
| 469 | REGPRM3 struct timeval *_tv_ms_add(struct timeval *tv, const struct timeval *from, int ms); |
| 470 | REGPRM3 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 Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 478 | return tv; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 479 | } |
| 480 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 481 | |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 482 | /* |
| 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 Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 520 | |
Willy Tarreau | 5ceeb15 | 2018-10-17 18:59:53 +0200 | [diff] [blame] | 521 | /* returns the system's monotonic time in nanoseconds if supported, otherwise zero */ |
| 522 | static 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 */ |
| 534 | static 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 Tarreau | 45a1251 | 2011-09-10 16:56:42 +0200 | [diff] [blame] | 545 | /* 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 | */ |
| 549 | static 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 Tarreau | 7e9c4ae | 2018-10-17 14:31:19 +0200 | [diff] [blame] | 574 | /* 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 | */ |
| 577 | static 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 | */ |
| 587 | static inline void tv_leaving_poll(int timeout, int interrupted) |
| 588 | { |
Willy Tarreau | 7e9c4ae | 2018-10-17 14:31:19 +0200 | [diff] [blame] | 589 | measure_idle(); |
Willy Tarreau | ed72d82 | 2018-10-17 19:01:24 +0200 | [diff] [blame] | 590 | prev_cpu_time = now_cpu_time(); |
| 591 | prev_mono_time = now_mono_time(); |
Willy Tarreau | 7e9c4ae | 2018-10-17 14:31:19 +0200 | [diff] [blame] | 592 | } |
| 593 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 594 | #endif /* _COMMON_TIME_H */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 595 | |
| 596 | /* |
| 597 | * Local variables: |
| 598 | * c-indent-level: 8 |
| 599 | * c-basic-offset: 8 |
| 600 | * End: |
| 601 | */ |