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 | 5ceeb15 | 2018-10-17 18:59:53 +0200 | [diff] [blame] | 25 | #include <stdint.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 | ed72d82 | 2018-10-17 19:01:24 +0200 | [diff] [blame] | 88 | /* Updates the current thread's statistics about stolen CPU time. The unit for |
| 89 | * <stolen> is half-milliseconds. |
| 90 | */ |
| 91 | REGPRM1 void report_stolen_time(uint64_t stolen); |
| 92 | |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 93 | /**** general purpose functions and macros *******************************/ |
| 94 | |
| 95 | |
| 96 | /* tv_now: sets <tv> to the current time */ |
| 97 | REGPRM1 static inline struct timeval *tv_now(struct timeval *tv) |
| 98 | { |
| 99 | gettimeofday(tv, NULL); |
| 100 | return tv; |
| 101 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 102 | |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 103 | /* 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 Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 110 | */ |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 111 | REGPRM2 void tv_update_date(int max_wait, int interrupted); |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 112 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 113 | /* |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 114 | * 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 Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 117 | */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 118 | REGPRM1 static inline struct timeval *tv_eternity(struct timeval *tv) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 119 | { |
Willy Tarreau | 5f3f15f | 2013-12-13 09:22:23 +0100 | [diff] [blame] | 120 | tv->tv_sec = (typeof(tv->tv_sec))TV_ETERNITY; |
| 121 | tv->tv_usec = (typeof(tv->tv_usec))TV_ETERNITY; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 122 | return tv; |
| 123 | } |
| 124 | |
| 125 | /* |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 126 | * sets a struct timeval to 0 |
| 127 | * |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 128 | */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 129 | REGPRM1 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 Tarreau | 5f3f15f | 2013-12-13 09:22:23 +0100 | [diff] [blame] | 137 | #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] | 138 | |
| 139 | /* |
Willy Tarreau | 49fa3a1 | 2007-05-12 22:29:44 +0200 | [diff] [blame] | 140 | * returns 0 if tv is [eternity], otherwise non-zero. |
| 141 | */ |
Willy Tarreau | 5f3f15f | 2013-12-13 09:22:23 +0100 | [diff] [blame] | 142 | #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] | 143 | |
| 144 | /* |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 145 | * returns non null if tv is [0], otherwise 0. |
| 146 | */ |
| 147 | #define tv_iszero(tv) (((tv)->tv_sec | (tv)->tv_usec) == 0) |
| 148 | |
Willy Tarreau | 49fa3a1 | 2007-05-12 22:29:44 +0200 | [diff] [blame] | 149 | /* |
| 150 | * Converts a struct timeval to a number of milliseconds. |
| 151 | */ |
| 152 | REGPRM1 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 | */ |
| 164 | REGPRM2 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 Tarreau | 776cd87 | 2009-03-05 00:34:01 +0100 | [diff] [blame] | 171 | /* 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 | */ |
| 178 | REGPRM1 static inline unsigned int __usec_to_1024th(unsigned int usec) |
| 179 | { |
| 180 | return (usec * 1073 + 742516) >> 20; |
| 181 | } |
| 182 | |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 183 | |
Willy Tarreau | 49fa3a1 | 2007-05-12 22:29:44 +0200 | [diff] [blame] | 184 | /**** comparison functions and macros ***********************************/ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 185 | |
| 186 | |
| 187 | /* tv_cmp: compares <tv1> and <tv2> : returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2. */ |
| 188 | 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] | 189 | { |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 190 | if ((unsigned)tv1->tv_sec < (unsigned)tv2->tv_sec) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 191 | return -1; |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 192 | else if ((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 193 | return 1; |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 194 | else if ((unsigned)tv1->tv_usec < (unsigned)tv2->tv_usec) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 195 | return -1; |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 196 | else if ((unsigned)tv1->tv_usec > (unsigned)tv2->tv_usec) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 197 | return 1; |
| 198 | else |
| 199 | return 0; |
Willy Tarreau | 5e8f066 | 2007-02-12 00:59:08 +0100 | [diff] [blame] | 200 | } |
| 201 | |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 202 | /* 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] | 203 | #define tv_iseq __tv_iseq |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 204 | REGPRM2 static inline int __tv_iseq(const struct timeval *tv1, const struct timeval *tv2) |
| 205 | { |
Willy Tarreau | 0481c20 | 2007-05-13 16:03:27 +0200 | [diff] [blame] | 206 | return ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) && |
| 207 | ((unsigned)tv1->tv_usec == (unsigned)tv2->tv_usec); |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | /* 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] | 211 | #define tv_isgt _tv_isgt |
| 212 | REGPRM2 int _tv_isgt(const struct timeval *tv1, const struct timeval *tv2); |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 213 | REGPRM2 static inline int __tv_isgt(const struct timeval *tv1, const struct timeval *tv2) |
| 214 | { |
Willy Tarreau | 0481c20 | 2007-05-13 16:03:27 +0200 | [diff] [blame] | 215 | 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 Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | /* 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] | 222 | #define tv_isge __tv_isge |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 223 | REGPRM2 static inline int __tv_isge(const struct timeval *tv1, const struct timeval *tv2) |
| 224 | { |
Willy Tarreau | 0481c20 | 2007-05-13 16:03:27 +0200 | [diff] [blame] | 225 | 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 Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | /* 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] | 232 | #define tv_islt __tv_islt |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 233 | REGPRM2 static inline int __tv_islt(const struct timeval *tv1, const struct timeval *tv2) |
| 234 | { |
Willy Tarreau | 0481c20 | 2007-05-13 16:03:27 +0200 | [diff] [blame] | 235 | 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 Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | /* 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] | 242 | #define tv_isle _tv_isle |
| 243 | REGPRM2 int _tv_isle(const struct timeval *tv1, const struct timeval *tv2); |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 244 | REGPRM2 static inline int __tv_isle(const struct timeval *tv1, const struct timeval *tv2) |
| 245 | { |
Willy Tarreau | 0481c20 | 2007-05-13 16:03:27 +0200 | [diff] [blame] | 246 | 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 Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 250 | } |
| 251 | |
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 | * 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 Tarreau | 5e8f066 | 2007-02-12 00:59:08 +0100 | [diff] [blame] | 255 | */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 256 | #define tv_ms_cmp _tv_ms_cmp |
| 257 | REGPRM2 int _tv_ms_cmp(const struct timeval *tv1, const struct timeval *tv2); |
| 258 | 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] | 259 | { |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 260 | 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 Tarreau | 5e8f066 | 2007-02-12 00:59:08 +0100 | [diff] [blame] | 275 | return 1; |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 276 | else |
Willy Tarreau | 5e8f066 | 2007-02-12 00:59:08 +0100 | [diff] [blame] | 277 | return 0; |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 278 | } |
| 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 |
| 285 | REGPRM2 int _tv_ms_cmp2(const struct timeval *tv1, const struct timeval *tv2); |
| 286 | REGPRM2 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 |
| 305 | REGPRM2 int _tv_ms_le2(const struct timeval *tv1, const struct timeval *tv2); |
| 306 | REGPRM2 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 Tarreau | 5e8f066 | 2007-02-12 00:59:08 +0100 | [diff] [blame] | 325 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 326 | } |
| 327 | |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 328 | |
| 329 | /**** operators **********************************************************/ |
| 330 | |
| 331 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 332 | /* |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 333 | * 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] | 334 | * Must not be used when either argument is eternity. |
| 335 | */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 336 | #define tv_ms_elapsed __tv_ms_elapsed |
| 337 | REGPRM2 unsigned long _tv_ms_elapsed(const struct timeval *tv1, const struct timeval *tv2); |
| 338 | 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] | 339 | { |
| 340 | unsigned long ret; |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 341 | |
| 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 Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 345 | } |
| 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 Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 352 | |
| 353 | #define tv_ms_remain __tv_ms_remain |
| 354 | REGPRM2 unsigned long _tv_ms_remain(const struct timeval *tv1, const struct timeval *tv2); |
| 355 | 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] | 356 | { |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 357 | if (tv_ms_cmp(tv1, tv2) >= 0) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 358 | return 0; /* event elapsed */ |
| 359 | |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 360 | return __tv_ms_elapsed(tv1, tv2); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 361 | } |
| 362 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 363 | /* |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 364 | * 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 Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 367 | */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 368 | #define tv_ms_remain2 _tv_ms_remain2 |
| 369 | REGPRM2 unsigned long _tv_ms_remain2(const struct timeval *tv1, const struct timeval *tv2); |
| 370 | 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] | 371 | { |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 372 | if (tv_iseternity(tv2)) |
| 373 | return TIME_ETERNITY; |
| 374 | |
| 375 | return tv_ms_remain(tv1, tv2); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 376 | } |
| 377 | |
Willy Tarreau | 49fa3a1 | 2007-05-12 22:29:44 +0200 | [diff] [blame] | 378 | /* |
| 379 | * adds <inc> to <from>, set the result to <tv> and returns a pointer <tv> |
| 380 | */ |
Willy Tarreau | 0481c20 | 2007-05-13 16:03:27 +0200 | [diff] [blame] | 381 | #define tv_add _tv_add |
Willy Tarreau | 49fa3a1 | 2007-05-12 22:29:44 +0200 | [diff] [blame] | 382 | REGPRM3 struct timeval *_tv_add(struct timeval *tv, const struct timeval *from, const struct timeval *inc); |
| 383 | REGPRM3 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 Tarreau | 0481c20 | 2007-05-13 16:03:27 +0200 | [diff] [blame] | 396 | * 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 |
| 400 | REGPRM3 int _tv_add_ifset(struct timeval *tv, const struct timeval *from, const struct timeval *inc); |
| 401 | REGPRM3 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 Tarreau | 49fa3a1 | 2007-05-12 22:29:44 +0200 | [diff] [blame] | 415 | * adds <inc> to <tv> and returns a pointer <tv> |
| 416 | */ |
| 417 | REGPRM2 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 |
| 434 | REGPRM3 struct timeval *_tv_remain(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv); |
| 435 | REGPRM3 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 |
| 461 | REGPRM3 struct timeval *_tv_remain2(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv); |
| 462 | REGPRM3 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 Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 470 | /* |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 471 | * 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] | 472 | */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 473 | #define tv_ms_add _tv_ms_add |
| 474 | REGPRM3 struct timeval *_tv_ms_add(struct timeval *tv, const struct timeval *from, int ms); |
| 475 | REGPRM3 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 Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 483 | return tv; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 484 | } |
| 485 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 486 | |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 487 | /* |
| 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 Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 525 | |
Willy Tarreau | 5ceeb15 | 2018-10-17 18:59:53 +0200 | [diff] [blame] | 526 | /* returns the system's monotonic time in nanoseconds if supported, otherwise zero */ |
| 527 | static 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 */ |
| 539 | static 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 Tarreau | 45a1251 | 2011-09-10 16:56:42 +0200 | [diff] [blame] | 550 | /* 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 | */ |
| 554 | static 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 Tarreau | 7e9c4ae | 2018-10-17 14:31:19 +0200 | [diff] [blame] | 579 | /* 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 | */ |
| 582 | static inline void tv_entering_poll() |
| 583 | { |
Willy Tarreau | ed72d82 | 2018-10-17 19:01:24 +0200 | [diff] [blame] | 584 | 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 Tarreau | 7e9c4ae | 2018-10-17 14:31:19 +0200 | [diff] [blame] | 604 | 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 | */ |
| 612 | static inline void tv_leaving_poll(int timeout, int interrupted) |
| 613 | { |
| 614 | tv_update_date(timeout, interrupted); |
| 615 | measure_idle(); |
Willy Tarreau | ed72d82 | 2018-10-17 19:01:24 +0200 | [diff] [blame] | 616 | prev_cpu_time = now_cpu_time(); |
| 617 | prev_mono_time = now_mono_time(); |
Willy Tarreau | 7e9c4ae | 2018-10-17 14:31:19 +0200 | [diff] [blame] | 618 | } |
| 619 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 620 | #endif /* _COMMON_TIME_H */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 621 | |
| 622 | /* |
| 623 | * Local variables: |
| 624 | * c-indent-level: 8 |
| 625 | * c-basic-offset: 8 |
| 626 | * End: |
| 627 | */ |