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 | 81036f2 | 2019-05-20 19:24:50 +0200 | [diff] [blame] | 30 | #include <common/hathreads.h> |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 31 | #include <common/standard.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 32 | |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 33 | /* 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 Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 45 | /* 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 Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 50 | |
| 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 Faulet | 9a65571 | 2017-05-11 11:00:15 +0200 | [diff] [blame] | 55 | extern THREAD_LOCAL unsigned int curr_sec_ms; /* millisecond of current second (0..999) */ |
| 56 | extern THREAD_LOCAL unsigned int ms_left_scaled; /* milliseconds left for current second (0..2^32-1) */ |
| 57 | extern THREAD_LOCAL unsigned int curr_sec_ms_scaled; /* millisecond of current second (0..2^32-1) */ |
| 58 | extern THREAD_LOCAL unsigned int now_ms; /* internal date in milliseconds (may wrap) */ |
| 59 | extern THREAD_LOCAL unsigned int samp_time; /* total elapsed time over current sample */ |
| 60 | extern THREAD_LOCAL unsigned int idle_time; /* total idle time over current sample */ |
Christopher Faulet | 9a65571 | 2017-05-11 11:00:15 +0200 | [diff] [blame] | 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 | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 66 | |
| 67 | |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 68 | /**** exported functions *************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 69 | /* |
| 70 | * adds <ms> ms to <from>, set the result to <tv> and returns a pointer <tv> |
| 71 | */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 72 | 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] | 73 | |
| 74 | /* |
| 75 | * 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] | 76 | * 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] | 77 | */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 78 | 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] | 79 | |
| 80 | /* |
Willy Tarreau | 8d7d149 | 2007-04-29 10:50:43 +0200 | [diff] [blame] | 81 | * 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 Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 83 | */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 84 | 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] | 85 | |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 86 | /**** general purpose functions and macros *******************************/ |
| 87 | |
| 88 | |
| 89 | /* tv_now: sets <tv> to the current time */ |
| 90 | REGPRM1 static inline struct timeval *tv_now(struct timeval *tv) |
| 91 | { |
| 92 | gettimeofday(tv, NULL); |
| 93 | return tv; |
| 94 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 95 | |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 96 | /* 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 Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 103 | */ |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 104 | REGPRM2 void tv_update_date(int max_wait, int interrupted); |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 105 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 106 | /* |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 107 | * 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 Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 110 | */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 111 | REGPRM1 static inline struct timeval *tv_eternity(struct timeval *tv) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 112 | { |
Willy Tarreau | 5f3f15f | 2013-12-13 09:22:23 +0100 | [diff] [blame] | 113 | tv->tv_sec = (typeof(tv->tv_sec))TV_ETERNITY; |
| 114 | tv->tv_usec = (typeof(tv->tv_usec))TV_ETERNITY; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 115 | return tv; |
| 116 | } |
| 117 | |
| 118 | /* |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 119 | * sets a struct timeval to 0 |
| 120 | * |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 121 | */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 122 | REGPRM1 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 Tarreau | 5f3f15f | 2013-12-13 09:22:23 +0100 | [diff] [blame] | 130 | #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] | 131 | |
| 132 | /* |
Willy Tarreau | 49fa3a1 | 2007-05-12 22:29:44 +0200 | [diff] [blame] | 133 | * returns 0 if tv is [eternity], otherwise non-zero. |
| 134 | */ |
Willy Tarreau | 5f3f15f | 2013-12-13 09:22:23 +0100 | [diff] [blame] | 135 | #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] | 136 | |
| 137 | /* |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 138 | * returns non null if tv is [0], otherwise 0. |
| 139 | */ |
| 140 | #define tv_iszero(tv) (((tv)->tv_sec | (tv)->tv_usec) == 0) |
| 141 | |
Willy Tarreau | 49fa3a1 | 2007-05-12 22:29:44 +0200 | [diff] [blame] | 142 | /* |
| 143 | * Converts a struct timeval to a number of milliseconds. |
| 144 | */ |
| 145 | REGPRM1 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 | */ |
| 157 | REGPRM2 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 Tarreau | 776cd87 | 2009-03-05 00:34:01 +0100 | [diff] [blame] | 164 | /* 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 | */ |
| 171 | REGPRM1 static inline unsigned int __usec_to_1024th(unsigned int usec) |
| 172 | { |
| 173 | return (usec * 1073 + 742516) >> 20; |
| 174 | } |
| 175 | |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 176 | |
Willy Tarreau | 49fa3a1 | 2007-05-12 22:29:44 +0200 | [diff] [blame] | 177 | /**** comparison functions and macros ***********************************/ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 178 | |
| 179 | |
| 180 | /* tv_cmp: compares <tv1> and <tv2> : returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2. */ |
| 181 | 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] | 182 | { |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 183 | if ((unsigned)tv1->tv_sec < (unsigned)tv2->tv_sec) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 184 | return -1; |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 185 | else 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_usec < (unsigned)tv2->tv_usec) |
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; |
| 191 | else |
| 192 | return 0; |
Willy Tarreau | 5e8f066 | 2007-02-12 00:59:08 +0100 | [diff] [blame] | 193 | } |
| 194 | |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 195 | /* 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] | 196 | #define tv_iseq __tv_iseq |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 197 | REGPRM2 static inline int __tv_iseq(const struct timeval *tv1, const struct timeval *tv2) |
| 198 | { |
Willy Tarreau | 0481c20 | 2007-05-13 16:03:27 +0200 | [diff] [blame] | 199 | return ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) && |
| 200 | ((unsigned)tv1->tv_usec == (unsigned)tv2->tv_usec); |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | /* 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] | 204 | #define tv_isgt _tv_isgt |
| 205 | REGPRM2 int _tv_isgt(const struct timeval *tv1, const struct timeval *tv2); |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 206 | REGPRM2 static inline int __tv_isgt(const struct timeval *tv1, const struct timeval *tv2) |
| 207 | { |
Willy Tarreau | 0481c20 | 2007-05-13 16:03:27 +0200 | [diff] [blame] | 208 | 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 Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | /* 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] | 215 | #define tv_isge __tv_isge |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 216 | REGPRM2 static inline int __tv_isge(const struct timeval *tv1, const struct timeval *tv2) |
| 217 | { |
Willy Tarreau | 0481c20 | 2007-05-13 16:03:27 +0200 | [diff] [blame] | 218 | 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 Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | /* 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] | 225 | #define tv_islt __tv_islt |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 226 | REGPRM2 static inline int __tv_islt(const struct timeval *tv1, const struct timeval *tv2) |
| 227 | { |
Willy Tarreau | 0481c20 | 2007-05-13 16:03:27 +0200 | [diff] [blame] | 228 | 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 Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | /* 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] | 235 | #define tv_isle _tv_isle |
| 236 | REGPRM2 int _tv_isle(const struct timeval *tv1, const struct timeval *tv2); |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 237 | REGPRM2 static inline int __tv_isle(const struct timeval *tv1, const struct timeval *tv2) |
| 238 | { |
Willy Tarreau | 0481c20 | 2007-05-13 16:03:27 +0200 | [diff] [blame] | 239 | 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 Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 243 | } |
| 244 | |
Willy Tarreau | 5e8f066 | 2007-02-12 00:59:08 +0100 | [diff] [blame] | 245 | /* |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 246 | * 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 Tarreau | 5e8f066 | 2007-02-12 00:59:08 +0100 | [diff] [blame] | 248 | */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 249 | #define tv_ms_cmp _tv_ms_cmp |
| 250 | REGPRM2 int _tv_ms_cmp(const struct timeval *tv1, const struct timeval *tv2); |
| 251 | 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] | 252 | { |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 253 | 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 Tarreau | 5e8f066 | 2007-02-12 00:59:08 +0100 | [diff] [blame] | 268 | return 1; |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 269 | else |
Willy Tarreau | 5e8f066 | 2007-02-12 00:59:08 +0100 | [diff] [blame] | 270 | return 0; |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 271 | } |
| 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 |
| 278 | REGPRM2 int _tv_ms_cmp2(const struct timeval *tv1, const struct timeval *tv2); |
| 279 | REGPRM2 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 |
| 298 | REGPRM2 int _tv_ms_le2(const struct timeval *tv1, const struct timeval *tv2); |
| 299 | REGPRM2 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 Tarreau | 5e8f066 | 2007-02-12 00:59:08 +0100 | [diff] [blame] | 318 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 319 | } |
| 320 | |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 321 | |
| 322 | /**** operators **********************************************************/ |
| 323 | |
| 324 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 325 | /* |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 326 | * 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] | 327 | * Must not be used when either argument is eternity. |
| 328 | */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 329 | #define tv_ms_elapsed __tv_ms_elapsed |
| 330 | REGPRM2 unsigned long _tv_ms_elapsed(const struct timeval *tv1, const struct timeval *tv2); |
| 331 | 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] | 332 | { |
| 333 | unsigned long ret; |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 334 | |
| 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 Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 338 | } |
| 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 Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 345 | |
| 346 | #define tv_ms_remain __tv_ms_remain |
| 347 | REGPRM2 unsigned long _tv_ms_remain(const struct timeval *tv1, const struct timeval *tv2); |
| 348 | 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] | 349 | { |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 350 | if (tv_ms_cmp(tv1, tv2) >= 0) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 351 | return 0; /* event elapsed */ |
| 352 | |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 353 | return __tv_ms_elapsed(tv1, tv2); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 354 | } |
| 355 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 356 | /* |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 357 | * 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 Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 360 | */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 361 | #define tv_ms_remain2 _tv_ms_remain2 |
| 362 | REGPRM2 unsigned long _tv_ms_remain2(const struct timeval *tv1, const struct timeval *tv2); |
| 363 | 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] | 364 | { |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 365 | if (tv_iseternity(tv2)) |
| 366 | return TIME_ETERNITY; |
| 367 | |
| 368 | return tv_ms_remain(tv1, tv2); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 369 | } |
| 370 | |
Willy Tarreau | 49fa3a1 | 2007-05-12 22:29:44 +0200 | [diff] [blame] | 371 | /* |
| 372 | * adds <inc> to <from>, set the result to <tv> and returns a pointer <tv> |
| 373 | */ |
Willy Tarreau | 0481c20 | 2007-05-13 16:03:27 +0200 | [diff] [blame] | 374 | #define tv_add _tv_add |
Willy Tarreau | 49fa3a1 | 2007-05-12 22:29:44 +0200 | [diff] [blame] | 375 | REGPRM3 struct timeval *_tv_add(struct timeval *tv, const struct timeval *from, const struct timeval *inc); |
| 376 | REGPRM3 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 Tarreau | 0481c20 | 2007-05-13 16:03:27 +0200 | [diff] [blame] | 389 | * 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 |
| 393 | REGPRM3 int _tv_add_ifset(struct timeval *tv, const struct timeval *from, const struct timeval *inc); |
| 394 | REGPRM3 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 Tarreau | 49fa3a1 | 2007-05-12 22:29:44 +0200 | [diff] [blame] | 408 | * adds <inc> to <tv> and returns a pointer <tv> |
| 409 | */ |
| 410 | REGPRM2 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 |
| 427 | REGPRM3 struct timeval *_tv_remain(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv); |
| 428 | REGPRM3 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 |
| 454 | REGPRM3 struct timeval *_tv_remain2(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv); |
| 455 | REGPRM3 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 Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 463 | /* |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 464 | * 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] | 465 | */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 466 | #define tv_ms_add _tv_ms_add |
| 467 | REGPRM3 struct timeval *_tv_ms_add(struct timeval *tv, const struct timeval *from, int ms); |
| 468 | REGPRM3 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 Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 476 | return tv; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 477 | } |
| 478 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 479 | |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 480 | /* |
| 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 Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 518 | |
Willy Tarreau | 5ceeb15 | 2018-10-17 18:59:53 +0200 | [diff] [blame] | 519 | /* returns the system's monotonic time in nanoseconds if supported, otherwise zero */ |
| 520 | static inline uint64_t now_mono_time() |
| 521 | { |
Willy Tarreau | f617824 | 2019-05-21 19:46:58 +0200 | [diff] [blame] | 522 | #if (_POSIX_TIMERS > 0) && defined(_POSIX_MONOTONIC_CLOCK) |
Willy Tarreau | 5ceeb15 | 2018-10-17 18:59:53 +0200 | [diff] [blame] | 523 | 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 */ |
| 532 | static inline uint64_t now_cpu_time() |
| 533 | { |
Willy Tarreau | f617824 | 2019-05-21 19:46:58 +0200 | [diff] [blame] | 534 | #if (_POSIX_TIMERS > 0) && defined(_POSIX_THREAD_CPUTIME) |
Willy Tarreau | 5ceeb15 | 2018-10-17 18:59:53 +0200 | [diff] [blame] | 535 | 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 Tarreau | 219b829 | 2019-05-20 20:28:34 +0200 | [diff] [blame] | 543 | /* returns another thread's cumulated CPU time in nanoseconds if supported, otherwise zero */ |
| 544 | static inline uint64_t now_cpu_time_thread(const struct thread_info *thr) |
| 545 | { |
Willy Tarreau | f617824 | 2019-05-21 19:46:58 +0200 | [diff] [blame] | 546 | #if (_POSIX_TIMERS > 0) && defined(_POSIX_THREAD_CPUTIME) |
Willy Tarreau | 219b829 | 2019-05-20 20:28:34 +0200 | [diff] [blame] | 547 | 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 Tarreau | 45a1251 | 2011-09-10 16:56:42 +0200 | [diff] [blame] | 555 | /* 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 | */ |
| 559 | static 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 Tarreau | 81036f2 | 2019-05-20 19:24:50 +0200 | [diff] [blame] | 580 | ti->idle_pct = (100 * idle_time + samp_time / 2) / samp_time; |
Willy Tarreau | 45a1251 | 2011-09-10 16:56:42 +0200 | [diff] [blame] | 581 | idle_time = samp_time = 0; |
| 582 | } |
| 583 | |
Willy Tarreau | 7e9c4ae | 2018-10-17 14:31:19 +0200 | [diff] [blame] | 584 | /* 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 | */ |
| 587 | static 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 | */ |
| 597 | static inline void tv_leaving_poll(int timeout, int interrupted) |
| 598 | { |
Willy Tarreau | 7e9c4ae | 2018-10-17 14:31:19 +0200 | [diff] [blame] | 599 | measure_idle(); |
Willy Tarreau | 81036f2 | 2019-05-20 19:24:50 +0200 | [diff] [blame] | 600 | ti->prev_cpu_time = now_cpu_time(); |
| 601 | ti->prev_mono_time = now_mono_time(); |
Willy Tarreau | 7e9c4ae | 2018-10-17 14:31:19 +0200 | [diff] [blame] | 602 | } |
| 603 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 604 | #endif /* _COMMON_TIME_H */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 605 | |
| 606 | /* |
| 607 | * Local variables: |
| 608 | * c-indent-level: 8 |
| 609 | * c-basic-offset: 8 |
| 610 | * End: |
| 611 | */ |