Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Time calculation functions. |
| 3 | * |
Willy Tarreau | 45a1251 | 2011-09-10 16:56:42 +0200 | [diff] [blame] | 4 | * Copyright 2000-2011 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <sys/time.h> |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 14 | |
| 15 | #include <common/config.h> |
Willy Tarreau | 5e8f066 | 2007-02-12 00:59:08 +0100 | [diff] [blame] | 16 | #include <common/standard.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 17 | #include <common/time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 18 | |
Christopher Faulet | 9a65571 | 2017-05-11 11:00:15 +0200 | [diff] [blame] | 19 | THREAD_LOCAL unsigned int ms_left_scaled; /* milliseconds left for current second (0..2^32-1) */ |
| 20 | THREAD_LOCAL unsigned int now_ms; /* internal date in milliseconds (may wrap) */ |
| 21 | THREAD_LOCAL unsigned int samp_time; /* total elapsed time over current sample */ |
| 22 | THREAD_LOCAL unsigned int idle_time; /* total idle time over current sample */ |
| 23 | THREAD_LOCAL unsigned int idle_pct; /* idle to total ratio over last sample (percent) */ |
| 24 | THREAD_LOCAL struct timeval now; /* internal date is a monotonic function of real clock */ |
| 25 | THREAD_LOCAL struct timeval date; /* the real current date */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 26 | struct timeval start_date; /* the process's start date */ |
Christopher Faulet | 9a65571 | 2017-05-11 11:00:15 +0200 | [diff] [blame] | 27 | THREAD_LOCAL struct timeval before_poll; /* system date before calling poll() */ |
| 28 | THREAD_LOCAL struct timeval after_poll; /* system date after leaving poll() */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 29 | |
| 30 | /* |
| 31 | * adds <ms> ms to <from>, set the result to <tv> and returns a pointer <tv> |
| 32 | */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 33 | 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] | 34 | { |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 35 | tv->tv_usec = from->tv_usec + (ms % 1000) * 1000; |
| 36 | tv->tv_sec = from->tv_sec + (ms / 1000); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 37 | while (tv->tv_usec >= 1000000) { |
| 38 | tv->tv_usec -= 1000000; |
| 39 | tv->tv_sec++; |
| 40 | } |
| 41 | return tv; |
| 42 | } |
| 43 | |
| 44 | /* |
| 45 | * 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] | 46 | * 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] | 47 | */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 48 | 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] | 49 | { |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 50 | return __tv_ms_cmp(tv1, tv2); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 54 | * compares <tv1> and <tv2> modulo 1 ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2, |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 55 | * assuming that TV_ETERNITY is greater than everything. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 56 | */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 57 | REGPRM2 int _tv_ms_cmp2(const struct timeval *tv1, const struct timeval *tv2) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 58 | { |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 59 | return __tv_ms_cmp2(tv1, tv2); |
Willy Tarreau | 8d7d149 | 2007-04-29 10:50:43 +0200 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | /* |
| 63 | * compares <tv1> and <tv2> modulo 1 ms: returns 1 if tv1 <= tv2, 0 if tv1 > tv2, |
| 64 | * assuming that TV_ETERNITY is greater than everything. Returns 0 if tv1 is |
| 65 | * TV_ETERNITY, and always assumes that tv2 != TV_ETERNITY. Designed to replace |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 66 | * occurrences of (tv_ms_cmp2(tv,now) <= 0). |
Willy Tarreau | 8d7d149 | 2007-04-29 10:50:43 +0200 | [diff] [blame] | 67 | */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 68 | REGPRM2 int _tv_ms_le2(const struct timeval *tv1, const struct timeval *tv2) |
Willy Tarreau | 8d7d149 | 2007-04-29 10:50:43 +0200 | [diff] [blame] | 69 | { |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 70 | return __tv_ms_le2(tv1, tv2); |
| 71 | } |
Willy Tarreau | 8d7d149 | 2007-04-29 10:50:43 +0200 | [diff] [blame] | 72 | |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 73 | /* |
| 74 | * returns the remaining time between tv1=now and event=tv2 |
| 75 | * if tv2 is passed, 0 is returned. |
| 76 | * Must not be used when either argument is eternity. |
| 77 | */ |
| 78 | REGPRM2 unsigned long _tv_ms_remain(const struct timeval *tv1, const struct timeval *tv2) |
| 79 | { |
| 80 | return __tv_ms_remain(tv1, tv2); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /* |
| 84 | * returns the remaining time between tv1=now and event=tv2 |
| 85 | * if tv2 is passed, 0 is returned. |
| 86 | * Returns TIME_ETERNITY if tv2 is eternity. |
| 87 | */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 88 | REGPRM2 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] | 89 | { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 90 | if (tv_iseternity(tv2)) |
| 91 | return TIME_ETERNITY; |
| 92 | |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 93 | return __tv_ms_remain(tv1, tv2); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 94 | } |
| 95 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 96 | /* |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 97 | * 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] | 98 | * Must not be used when either argument is eternity. |
| 99 | */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 100 | REGPRM2 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] | 101 | { |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 102 | return __tv_ms_elapsed(tv1, tv2); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /* |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 106 | * adds <inc> to <from>, set the result to <tv> and returns a pointer <tv> |
| 107 | */ |
| 108 | REGPRM3 struct timeval *_tv_add(struct timeval *tv, const struct timeval *from, const struct timeval *inc) |
| 109 | { |
| 110 | return __tv_add(tv, from, inc); |
| 111 | } |
| 112 | |
| 113 | /* |
Willy Tarreau | 0481c20 | 2007-05-13 16:03:27 +0200 | [diff] [blame] | 114 | * If <inc> is set, then add it to <from> and set the result to <tv>, then |
| 115 | * return 1, otherwise return 0. It is meant to be used in if conditions. |
| 116 | */ |
| 117 | REGPRM3 int _tv_add_ifset(struct timeval *tv, const struct timeval *from, const struct timeval *inc) |
| 118 | { |
| 119 | return __tv_add_ifset(tv, from, inc); |
| 120 | } |
| 121 | |
| 122 | /* |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 123 | * Computes the remaining time between tv1=now and event=tv2. if tv2 is passed, |
| 124 | * 0 is returned. The result is stored into tv. |
| 125 | */ |
| 126 | REGPRM3 struct timeval *_tv_remain(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv) |
| 127 | { |
| 128 | return __tv_remain(tv1, tv2, tv); |
| 129 | } |
| 130 | |
| 131 | /* |
| 132 | * Computes the remaining time between tv1=now and event=tv2. if tv2 is passed, |
| 133 | * 0 is returned. The result is stored into tv. Returns ETERNITY if tv2 is |
| 134 | * eternity. |
| 135 | */ |
| 136 | REGPRM3 struct timeval *_tv_remain2(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv) |
| 137 | { |
| 138 | return __tv_remain2(tv1, tv2, tv); |
| 139 | } |
| 140 | |
Willy Tarreau | 0481c20 | 2007-05-13 16:03:27 +0200 | [diff] [blame] | 141 | /* tv_isle: compares <tv1> and <tv2> : returns 1 if tv1 <= tv2, otherwise 0 */ |
| 142 | REGPRM2 int _tv_isle(const struct timeval *tv1, const struct timeval *tv2) |
| 143 | { |
| 144 | return __tv_isle(tv1, tv2); |
| 145 | } |
| 146 | |
| 147 | /* tv_isgt: compares <tv1> and <tv2> : returns 1 if tv1 > tv2, otherwise 0 */ |
| 148 | REGPRM2 int _tv_isgt(const struct timeval *tv1, const struct timeval *tv2) |
| 149 | { |
| 150 | return __tv_isgt(tv1, tv2); |
| 151 | } |
| 152 | |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 153 | /* tv_udpate_date: sets <date> to system time, and sets <now> to something as |
| 154 | * close as possible to real time, following a monotonic function. The main |
| 155 | * principle consists in detecting backwards and forwards time jumps and adjust |
| 156 | * an offset to correct them. This function should be called once after each |
| 157 | * poll, and never farther apart than MAX_DELAY_MS*2. The poll's timeout should |
| 158 | * be passed in <max_wait>, and the return value in <interrupted> (a non-zero |
| 159 | * value means that we have not expired the timeout). Calling it with (-1,*) |
| 160 | * sets both <date> and <now> to current date, and calling it with (0,1) simply |
| 161 | * updates the values. |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 162 | */ |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 163 | REGPRM2 void tv_update_date(int max_wait, int interrupted) |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 164 | { |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 165 | static struct timeval tv_offset; /* warning: signed offset! */ |
| 166 | struct timeval adjusted, deadline; |
Willy Tarreau | 351b3a1 | 2017-03-29 15:24:33 +0200 | [diff] [blame] | 167 | unsigned int curr_sec_ms; /* millisecond of current second (0..999) */ |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 168 | |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 169 | gettimeofday(&date, NULL); |
| 170 | if (unlikely(max_wait < 0)) { |
| 171 | tv_zero(&tv_offset); |
Willy Tarreau | 7559058 | 2009-03-05 14:54:50 +0100 | [diff] [blame] | 172 | adjusted = date; |
Willy Tarreau | 45a1251 | 2011-09-10 16:56:42 +0200 | [diff] [blame] | 173 | after_poll = date; |
| 174 | samp_time = idle_time = 0; |
| 175 | idle_pct = 100; |
Willy Tarreau | e6313a3 | 2008-06-29 13:47:25 +0200 | [diff] [blame] | 176 | goto to_ms; |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 177 | } |
| 178 | __tv_add(&adjusted, &date, &tv_offset); |
| 179 | if (unlikely(__tv_islt(&adjusted, &now))) { |
| 180 | goto fixup; /* jump in the past */ |
| 181 | } |
| 182 | |
| 183 | /* OK we did not jump backwards, let's see if we have jumped too far |
| 184 | * forwards. The poll value was in <max_wait>, we accept that plus |
| 185 | * MAX_DELAY_MS to cover additional time. |
| 186 | */ |
| 187 | _tv_ms_add(&deadline, &now, max_wait + MAX_DELAY_MS); |
Willy Tarreau | 7559058 | 2009-03-05 14:54:50 +0100 | [diff] [blame] | 188 | if (likely(__tv_islt(&adjusted, &deadline))) |
| 189 | goto to_ms; /* OK time is within expected range */ |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 190 | fixup: |
| 191 | /* Large jump. If the poll was interrupted, we consider that the date |
| 192 | * has not changed (immediate wake-up), otherwise we add the poll |
| 193 | * time-out to the previous date. The new offset is recomputed. |
| 194 | */ |
Willy Tarreau | 7559058 | 2009-03-05 14:54:50 +0100 | [diff] [blame] | 195 | _tv_ms_add(&adjusted, &now, interrupted ? 0 : max_wait); |
| 196 | |
| 197 | tv_offset.tv_sec = adjusted.tv_sec - date.tv_sec; |
| 198 | tv_offset.tv_usec = adjusted.tv_usec - date.tv_usec; |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 199 | if (tv_offset.tv_usec < 0) { |
| 200 | tv_offset.tv_usec += 1000000; |
| 201 | tv_offset.tv_sec--; |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 202 | } |
Willy Tarreau | e6313a3 | 2008-06-29 13:47:25 +0200 | [diff] [blame] | 203 | to_ms: |
Willy Tarreau | 7559058 | 2009-03-05 14:54:50 +0100 | [diff] [blame] | 204 | now = adjusted; |
| 205 | curr_sec_ms = now.tv_usec / 1000; /* ms of current second */ |
Willy Tarreau | eab777c | 2012-12-29 21:50:07 +0100 | [diff] [blame] | 206 | |
| 207 | /* For frequency counters, we'll need to know the ratio of the previous |
| 208 | * value to add to current value depending on the current millisecond. |
| 209 | * The principle is that during the first millisecond, we use 999/1000 |
| 210 | * of the past value and that during the last millisecond we use 0/1000 |
| 211 | * of the past value. In summary, we only use the past value during the |
| 212 | * first 999 ms of a second, and the last ms is used to complete the |
| 213 | * current measure. The value is scaled to (2^32-1) so that a simple |
| 214 | * multiply followed by a shift gives us the final value. |
| 215 | */ |
| 216 | ms_left_scaled = (999U - curr_sec_ms) * 4294967U; |
Willy Tarreau | 7559058 | 2009-03-05 14:54:50 +0100 | [diff] [blame] | 217 | now_ms = now.tv_sec * 1000 + curr_sec_ms; |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 218 | return; |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 219 | } |
| 220 | |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 221 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 222 | * Local variables: |
| 223 | * c-indent-level: 8 |
| 224 | * c-basic-offset: 8 |
| 225 | * End: |
| 226 | */ |