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 | |
Willy Tarreau | ed72d82 | 2018-10-17 19:01:24 +0200 | [diff] [blame] | 13 | #include <unistd.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 14 | #include <sys/time.h> |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 15 | |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 16 | #include <haproxy/api.h> |
Willy Tarreau | 92b4f13 | 2020-06-01 11:05:15 +0200 | [diff] [blame] | 17 | #include <haproxy/time.h> |
Willy Tarreau | 6064b34 | 2021-03-23 08:45:42 +0100 | [diff] [blame] | 18 | #include <haproxy/ticks.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 19 | #include <haproxy/tools.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 20 | |
Willy Tarreau | c690ca4 | 2023-05-17 09:02:21 +0200 | [diff] [blame] | 21 | struct timeval start_date; /* the process's start date */ |
| 22 | struct timeval ready_date; /* date when the process was considered ready */ |
| 23 | |
Christopher Faulet | 9a65571 | 2017-05-11 11:00:15 +0200 | [diff] [blame] | 24 | THREAD_LOCAL unsigned int now_ms; /* internal date in milliseconds (may wrap) */ |
| 25 | THREAD_LOCAL unsigned int samp_time; /* total elapsed time over current sample */ |
| 26 | THREAD_LOCAL unsigned int idle_time; /* total idle time over current sample */ |
Christopher Faulet | 9a65571 | 2017-05-11 11:00:15 +0200 | [diff] [blame] | 27 | THREAD_LOCAL struct timeval now; /* internal date is a monotonic function of real clock */ |
| 28 | THREAD_LOCAL struct timeval date; /* the real current date */ |
Christopher Faulet | 9a65571 | 2017-05-11 11:00:15 +0200 | [diff] [blame] | 29 | THREAD_LOCAL struct timeval before_poll; /* system date before calling poll() */ |
| 30 | THREAD_LOCAL struct timeval after_poll; /* system date after leaving poll() */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 31 | |
Willy Tarreau | 4498271 | 2021-04-11 18:53:58 +0200 | [diff] [blame] | 32 | static unsigned long long now_offset; /* global offset between system time and global time */ |
| 33 | volatile unsigned long long global_now; /* common monotonic date between all threads (32:32) */ |
| 34 | volatile unsigned int global_now_ms; /* common monotonic date in milliseconds (may wrap) */ |
Willy Tarreau | 9fefc51 | 2017-11-23 14:52:28 +0100 | [diff] [blame] | 35 | |
Willy Tarreau | 93acfa2 | 2019-09-26 08:00:23 +0200 | [diff] [blame] | 36 | static THREAD_LOCAL unsigned int iso_time_sec; /* last iso time value for this thread */ |
Emeric Brun | b39a375 | 2020-07-02 17:22:17 +0200 | [diff] [blame] | 37 | static THREAD_LOCAL char iso_time_str[34]; /* ISO time representation of gettimeofday() */ |
Willy Tarreau | 93acfa2 | 2019-09-26 08:00:23 +0200 | [diff] [blame] | 38 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 39 | /* |
| 40 | * adds <ms> ms to <from>, set the result to <tv> and returns a pointer <tv> |
| 41 | */ |
Willy Tarreau | 03e7853 | 2020-02-25 07:38:05 +0100 | [diff] [blame] | 42 | 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] | 43 | { |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 44 | tv->tv_usec = from->tv_usec + (ms % 1000) * 1000; |
| 45 | tv->tv_sec = from->tv_sec + (ms / 1000); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 46 | while (tv->tv_usec >= 1000000) { |
| 47 | tv->tv_usec -= 1000000; |
| 48 | tv->tv_sec++; |
| 49 | } |
| 50 | return tv; |
| 51 | } |
| 52 | |
| 53 | /* |
| 54 | * 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] | 55 | * 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] | 56 | */ |
Willy Tarreau | 03e7853 | 2020-02-25 07:38:05 +0100 | [diff] [blame] | 57 | int _tv_ms_cmp(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_cmp(tv1, tv2); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 63 | * 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] | 64 | * assuming that TV_ETERNITY is greater than everything. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 65 | */ |
Willy Tarreau | 03e7853 | 2020-02-25 07:38:05 +0100 | [diff] [blame] | 66 | int _tv_ms_cmp2(const struct timeval *tv1, const struct timeval *tv2) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 67 | { |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 68 | return __tv_ms_cmp2(tv1, tv2); |
Willy Tarreau | 8d7d149 | 2007-04-29 10:50:43 +0200 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /* |
| 72 | * compares <tv1> and <tv2> modulo 1 ms: returns 1 if tv1 <= tv2, 0 if tv1 > tv2, |
| 73 | * assuming that TV_ETERNITY is greater than everything. Returns 0 if tv1 is |
| 74 | * TV_ETERNITY, and always assumes that tv2 != TV_ETERNITY. Designed to replace |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 75 | * occurrences of (tv_ms_cmp2(tv,now) <= 0). |
Willy Tarreau | 8d7d149 | 2007-04-29 10:50:43 +0200 | [diff] [blame] | 76 | */ |
Willy Tarreau | 03e7853 | 2020-02-25 07:38:05 +0100 | [diff] [blame] | 77 | int _tv_ms_le2(const struct timeval *tv1, const struct timeval *tv2) |
Willy Tarreau | 8d7d149 | 2007-04-29 10:50:43 +0200 | [diff] [blame] | 78 | { |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 79 | return __tv_ms_le2(tv1, tv2); |
| 80 | } |
Willy Tarreau | 8d7d149 | 2007-04-29 10:50:43 +0200 | [diff] [blame] | 81 | |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 82 | /* |
| 83 | * returns the remaining time between tv1=now and event=tv2 |
| 84 | * if tv2 is passed, 0 is returned. |
| 85 | * Must not be used when either argument is eternity. |
| 86 | */ |
Willy Tarreau | 03e7853 | 2020-02-25 07:38:05 +0100 | [diff] [blame] | 87 | unsigned long _tv_ms_remain(const struct timeval *tv1, const struct timeval *tv2) |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 88 | { |
| 89 | return __tv_ms_remain(tv1, tv2); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | /* |
| 93 | * returns the remaining time between tv1=now and event=tv2 |
| 94 | * if tv2 is passed, 0 is returned. |
| 95 | * Returns TIME_ETERNITY if tv2 is eternity. |
| 96 | */ |
Willy Tarreau | 03e7853 | 2020-02-25 07:38:05 +0100 | [diff] [blame] | 97 | 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] | 98 | { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 99 | if (tv_iseternity(tv2)) |
| 100 | return TIME_ETERNITY; |
| 101 | |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 102 | return __tv_ms_remain(tv1, tv2); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 103 | } |
| 104 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 105 | /* |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 106 | * 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] | 107 | * Must not be used when either argument is eternity. |
| 108 | */ |
Willy Tarreau | 03e7853 | 2020-02-25 07:38:05 +0100 | [diff] [blame] | 109 | 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] | 110 | { |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 111 | return __tv_ms_elapsed(tv1, tv2); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /* |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 115 | * adds <inc> to <from>, set the result to <tv> and returns a pointer <tv> |
| 116 | */ |
Willy Tarreau | 03e7853 | 2020-02-25 07:38:05 +0100 | [diff] [blame] | 117 | struct timeval *_tv_add(struct timeval *tv, const struct timeval *from, const struct timeval *inc) |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 118 | { |
| 119 | return __tv_add(tv, from, inc); |
| 120 | } |
| 121 | |
| 122 | /* |
Willy Tarreau | 0481c20 | 2007-05-13 16:03:27 +0200 | [diff] [blame] | 123 | * If <inc> is set, then add it to <from> and set the result to <tv>, then |
| 124 | * return 1, otherwise return 0. It is meant to be used in if conditions. |
| 125 | */ |
Willy Tarreau | 03e7853 | 2020-02-25 07:38:05 +0100 | [diff] [blame] | 126 | int _tv_add_ifset(struct timeval *tv, const struct timeval *from, const struct timeval *inc) |
Willy Tarreau | 0481c20 | 2007-05-13 16:03:27 +0200 | [diff] [blame] | 127 | { |
| 128 | return __tv_add_ifset(tv, from, inc); |
| 129 | } |
| 130 | |
| 131 | /* |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 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. |
| 134 | */ |
Willy Tarreau | 03e7853 | 2020-02-25 07:38:05 +0100 | [diff] [blame] | 135 | struct timeval *_tv_remain(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv) |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 136 | { |
| 137 | return __tv_remain(tv1, tv2, tv); |
| 138 | } |
| 139 | |
| 140 | /* |
| 141 | * Computes the remaining time between tv1=now and event=tv2. if tv2 is passed, |
| 142 | * 0 is returned. The result is stored into tv. Returns ETERNITY if tv2 is |
| 143 | * eternity. |
| 144 | */ |
Willy Tarreau | 03e7853 | 2020-02-25 07:38:05 +0100 | [diff] [blame] | 145 | struct timeval *_tv_remain2(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv) |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 146 | { |
| 147 | return __tv_remain2(tv1, tv2, tv); |
| 148 | } |
| 149 | |
Willy Tarreau | 0481c20 | 2007-05-13 16:03:27 +0200 | [diff] [blame] | 150 | /* tv_isle: compares <tv1> and <tv2> : returns 1 if tv1 <= tv2, otherwise 0 */ |
Willy Tarreau | 03e7853 | 2020-02-25 07:38:05 +0100 | [diff] [blame] | 151 | int _tv_isle(const struct timeval *tv1, const struct timeval *tv2) |
Willy Tarreau | 0481c20 | 2007-05-13 16:03:27 +0200 | [diff] [blame] | 152 | { |
| 153 | return __tv_isle(tv1, tv2); |
| 154 | } |
| 155 | |
| 156 | /* tv_isgt: compares <tv1> and <tv2> : returns 1 if tv1 > tv2, otherwise 0 */ |
Willy Tarreau | 03e7853 | 2020-02-25 07:38:05 +0100 | [diff] [blame] | 157 | int _tv_isgt(const struct timeval *tv1, const struct timeval *tv2) |
Willy Tarreau | 0481c20 | 2007-05-13 16:03:27 +0200 | [diff] [blame] | 158 | { |
| 159 | return __tv_isgt(tv1, tv2); |
| 160 | } |
| 161 | |
Willy Tarreau | 7649aac | 2017-11-23 11:52:55 +0100 | [diff] [blame] | 162 | /* tv_update_date: sets <date> to system time, and sets <now> to something as |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 163 | * close as possible to real time, following a monotonic function. The main |
| 164 | * principle consists in detecting backwards and forwards time jumps and adjust |
| 165 | * an offset to correct them. This function should be called once after each |
| 166 | * poll, and never farther apart than MAX_DELAY_MS*2. The poll's timeout should |
| 167 | * be passed in <max_wait>, and the return value in <interrupted> (a non-zero |
Willy Tarreau | c4c80fb | 2021-04-11 15:00:34 +0200 | [diff] [blame] | 168 | * value means that we have not expired the timeout). |
| 169 | * |
| 170 | * tv_init_process_date() must have been called once first, and |
| 171 | * tv_init_thread_date() must also have been called once for each thread. |
Christopher Faulet | 99aad92 | 2017-10-31 09:03:51 +0100 | [diff] [blame] | 172 | * |
Willy Tarreau | 4498271 | 2021-04-11 18:53:58 +0200 | [diff] [blame] | 173 | * An offset is used to adjust the current time (date), to figure a monotonic |
| 174 | * local time (now). The offset is not critical, as it is only updated after a |
| 175 | * clock jump is detected. From this point all threads will apply it to their |
| 176 | * locally measured time, and will then agree around a common monotonic |
| 177 | * global_now value that serves to further refine their local time. As it is |
| 178 | * not possible to atomically update a timeval, both global_now and the |
| 179 | * now_offset values are instead stored as 64-bit integers made of two 32 bit |
| 180 | * values for the tv_sec and tv_usec parts. The offset is made of two signed |
| 181 | * ints so that the clock can be adjusted in the two directions. |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 182 | */ |
Willy Tarreau | 03e7853 | 2020-02-25 07:38:05 +0100 | [diff] [blame] | 183 | void tv_update_date(int max_wait, int interrupted) |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 184 | { |
Willy Tarreau | 4498271 | 2021-04-11 18:53:58 +0200 | [diff] [blame] | 185 | struct timeval min_deadline, max_deadline, tmp_now; |
Willy Tarreau | 7e4a557 | 2021-04-11 15:34:25 +0200 | [diff] [blame] | 186 | unsigned int old_now_ms; |
Willy Tarreau | 9fefc51 | 2017-11-23 14:52:28 +0100 | [diff] [blame] | 187 | unsigned long long old_now; |
| 188 | unsigned long long new_now; |
Willy Tarreau | 481795d | 2021-04-23 15:17:27 +0200 | [diff] [blame] | 189 | ullong ofs, ofs_new; |
Willy Tarreau | 4498271 | 2021-04-11 18:53:58 +0200 | [diff] [blame] | 190 | uint sec_ofs, usec_ofs; |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 191 | |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 192 | gettimeofday(&date, NULL); |
Willy Tarreau | 7649aac | 2017-11-23 11:52:55 +0100 | [diff] [blame] | 193 | |
Willy Tarreau | 9fefc51 | 2017-11-23 14:52:28 +0100 | [diff] [blame] | 194 | /* compute the minimum and maximum local date we may have reached based |
Willy Tarreau | 4498271 | 2021-04-11 18:53:58 +0200 | [diff] [blame] | 195 | * on our past date and the associated timeout. There are three possible |
| 196 | * extremities: |
| 197 | * - the new date cannot be older than before_poll |
| 198 | * - if not interrupted, the new date cannot be older than |
| 199 | * before_poll+max_wait |
| 200 | * - in any case the new date cannot be newer than |
| 201 | * before_poll+max_wait+some margin (100ms used here). |
| 202 | * In case of violation, we'll ignore the current date and instead |
| 203 | * restart from the last date we knew. |
Willy Tarreau | 9fefc51 | 2017-11-23 14:52:28 +0100 | [diff] [blame] | 204 | */ |
Willy Tarreau | 4498271 | 2021-04-11 18:53:58 +0200 | [diff] [blame] | 205 | _tv_ms_add(&min_deadline, &before_poll, max_wait); |
| 206 | _tv_ms_add(&max_deadline, &before_poll, max_wait + 100); |
Willy Tarreau | 7649aac | 2017-11-23 11:52:55 +0100 | [diff] [blame] | 207 | |
Willy Tarreau | 481795d | 2021-04-23 15:17:27 +0200 | [diff] [blame] | 208 | ofs = HA_ATOMIC_LOAD(&now_offset); |
| 209 | |
Willy Tarreau | 4498271 | 2021-04-11 18:53:58 +0200 | [diff] [blame] | 210 | if (unlikely(__tv_islt(&date, &before_poll) || // big jump backwards |
| 211 | (!interrupted && __tv_islt(&date, &min_deadline)) || // small jump backwards |
| 212 | __tv_islt(&max_deadline, &date))) { // big jump forwards |
| 213 | if (!interrupted) |
| 214 | _tv_ms_add(&now, &now, max_wait); |
| 215 | } else { |
| 216 | /* The date is still within expectations. Let's apply the |
| 217 | * now_offset to the system date. Note: ofs if made of two |
| 218 | * independent signed ints. |
Willy Tarreau | 9fefc51 | 2017-11-23 14:52:28 +0100 | [diff] [blame] | 219 | */ |
Willy Tarreau | 4498271 | 2021-04-11 18:53:58 +0200 | [diff] [blame] | 220 | now.tv_sec = date.tv_sec + (int)(ofs >> 32); // note: may be positive or negative |
| 221 | now.tv_usec = date.tv_usec + (int)ofs; // note: may be positive or negative |
| 222 | if ((int)now.tv_usec < 0) { |
| 223 | now.tv_usec += 1000000; |
| 224 | now.tv_sec -= 1; |
| 225 | } else if (now.tv_usec >= 1000000) { |
| 226 | now.tv_usec -= 1000000; |
| 227 | now.tv_sec += 1; |
| 228 | } |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 229 | } |
| 230 | |
Willy Tarreau | 9fefc51 | 2017-11-23 14:52:28 +0100 | [diff] [blame] | 231 | /* now that we have bounded the local time, let's check if it's |
| 232 | * realistic regarding the global date, which only moves forward, |
| 233 | * otherwise catch up. |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 234 | */ |
Willy Tarreau | 9fefc51 | 2017-11-23 14:52:28 +0100 | [diff] [blame] | 235 | old_now = global_now; |
Willy Tarreau | 7e4a557 | 2021-04-11 15:34:25 +0200 | [diff] [blame] | 236 | old_now_ms = global_now_ms; |
Willy Tarreau | 7559058 | 2009-03-05 14:54:50 +0100 | [diff] [blame] | 237 | |
Willy Tarreau | 9fefc51 | 2017-11-23 14:52:28 +0100 | [diff] [blame] | 238 | do { |
| 239 | tmp_now.tv_sec = (unsigned int)(old_now >> 32); |
| 240 | tmp_now.tv_usec = old_now & 0xFFFFFFFFU; |
Willy Tarreau | 7649aac | 2017-11-23 11:52:55 +0100 | [diff] [blame] | 241 | |
Willy Tarreau | 70cb302 | 2021-04-11 15:17:48 +0200 | [diff] [blame] | 242 | if (__tv_islt(&now, &tmp_now)) |
| 243 | now = tmp_now; |
Willy Tarreau | 9fefc51 | 2017-11-23 14:52:28 +0100 | [diff] [blame] | 244 | |
Willy Tarreau | 70cb302 | 2021-04-11 15:17:48 +0200 | [diff] [blame] | 245 | /* now <now> is expected to be the most accurate date, |
Willy Tarreau | 9fefc51 | 2017-11-23 14:52:28 +0100 | [diff] [blame] | 246 | * equal to <global_now> or newer. |
| 247 | */ |
Willy Tarreau | 70cb302 | 2021-04-11 15:17:48 +0200 | [diff] [blame] | 248 | new_now = ((ullong)now.tv_sec << 32) + (uint)now.tv_usec; |
Willy Tarreau | 1f9e11e | 2021-04-23 16:03:21 +0200 | [diff] [blame] | 249 | now_ms = __tv_to_ms(&now); |
Willy Tarreau | 9fefc51 | 2017-11-23 14:52:28 +0100 | [diff] [blame] | 250 | |
Willy Tarreau | 7e4a557 | 2021-04-11 15:34:25 +0200 | [diff] [blame] | 251 | /* let's try to update the global <now> (both in timeval |
| 252 | * and ms forms) or loop again. |
| 253 | */ |
Willy Tarreau | 4d01f3d | 2021-04-23 15:36:47 +0200 | [diff] [blame] | 254 | } while (((new_now != old_now && !_HA_ATOMIC_CAS(&global_now, &old_now, new_now)) || |
| 255 | (now_ms != old_now_ms && !_HA_ATOMIC_CAS(&global_now_ms, &old_now_ms, now_ms))) && |
| 256 | __ha_cpu_relax()); |
Willy Tarreau | 9fefc51 | 2017-11-23 14:52:28 +0100 | [diff] [blame] | 257 | |
Willy Tarreau | 4498271 | 2021-04-11 18:53:58 +0200 | [diff] [blame] | 258 | /* <now> and <now_ms> are now updated to the last value of global_now |
| 259 | * and global_now_ms, which were also monotonically updated. We can |
| 260 | * compute the latest offset, we don't care who writes it last, the |
| 261 | * variations will not break the monotonic property. |
Willy Tarreau | 9fefc51 | 2017-11-23 14:52:28 +0100 | [diff] [blame] | 262 | */ |
Willy Tarreau | 7649aac | 2017-11-23 11:52:55 +0100 | [diff] [blame] | 263 | |
Willy Tarreau | 4498271 | 2021-04-11 18:53:58 +0200 | [diff] [blame] | 264 | sec_ofs = now.tv_sec - date.tv_sec; |
| 265 | usec_ofs = now.tv_usec - date.tv_usec; |
| 266 | if ((int)usec_ofs < 0) { |
| 267 | usec_ofs += 1000000; |
| 268 | sec_ofs -= 1; |
| 269 | } |
Willy Tarreau | 481795d | 2021-04-23 15:17:27 +0200 | [diff] [blame] | 270 | ofs_new = ((ullong)sec_ofs << 32) + usec_ofs; |
| 271 | if (ofs_new != ofs) |
| 272 | HA_ATOMIC_STORE(&now_offset, ofs_new); |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 273 | } |
| 274 | |
Willy Tarreau | c4c80fb | 2021-04-11 15:00:34 +0200 | [diff] [blame] | 275 | /* must be called once at boot to initialize some global variables */ |
| 276 | void tv_init_process_date() |
| 277 | { |
Willy Tarreau | 4498271 | 2021-04-11 18:53:58 +0200 | [diff] [blame] | 278 | now_offset = 0; |
Willy Tarreau | c4c80fb | 2021-04-11 15:00:34 +0200 | [diff] [blame] | 279 | gettimeofday(&date, NULL); |
| 280 | now = after_poll = before_poll = date; |
| 281 | global_now = ((ullong)date.tv_sec << 32) + (uint)date.tv_usec; |
| 282 | global_now_ms = now.tv_sec * 1000 + now.tv_usec / 1000; |
| 283 | samp_time = idle_time = 0; |
| 284 | ti->idle_pct = 100; |
| 285 | tv_update_date(0, 1); |
| 286 | } |
| 287 | |
| 288 | /* must be called once per thread to initialize their thread-local variables. |
| 289 | * Note that other threads might also be initializing and running in parallel. |
| 290 | */ |
| 291 | void tv_init_thread_date() |
| 292 | { |
| 293 | ullong old_now; |
| 294 | |
| 295 | gettimeofday(&date, NULL); |
| 296 | after_poll = before_poll = date; |
| 297 | |
| 298 | old_now = _HA_ATOMIC_LOAD(&global_now); |
| 299 | now.tv_sec = old_now >> 32; |
| 300 | now.tv_usec = (uint)old_now; |
Willy Tarreau | c4c80fb | 2021-04-11 15:00:34 +0200 | [diff] [blame] | 301 | samp_time = idle_time = 0; |
| 302 | ti->idle_pct = 100; |
Aurelien DARRAGON | 069a6c8 | 2022-11-10 11:47:47 +0100 | [diff] [blame] | 303 | ti->prev_cpu_time = now_cpu_time(); |
Willy Tarreau | c4c80fb | 2021-04-11 15:00:34 +0200 | [diff] [blame] | 304 | tv_update_date(0, 1); |
| 305 | } |
| 306 | |
Willy Tarreau | 93acfa2 | 2019-09-26 08:00:23 +0200 | [diff] [blame] | 307 | /* returns the current date as returned by gettimeofday() in ISO+microsecond |
| 308 | * format. It uses a thread-local static variable that the reader can consume |
| 309 | * for as long as it wants until next call. Thus, do not call it from a signal |
| 310 | * handler. If <pad> is non-0, a trailing space will be added. It will always |
Emeric Brun | b39a375 | 2020-07-02 17:22:17 +0200 | [diff] [blame] | 311 | * return exactly 32 or 33 characters (depending on padding) and will always be |
| 312 | * zero-terminated, thus it will always fit into a 34 bytes buffer. |
| 313 | * This also always include the local timezone (in +/-HH:mm format) . |
Willy Tarreau | 93acfa2 | 2019-09-26 08:00:23 +0200 | [diff] [blame] | 314 | */ |
| 315 | char *timeofday_as_iso_us(int pad) |
| 316 | { |
| 317 | struct timeval new_date; |
| 318 | struct tm tm; |
Emeric Brun | b39a375 | 2020-07-02 17:22:17 +0200 | [diff] [blame] | 319 | const char *offset; |
| 320 | char c; |
Willy Tarreau | 93acfa2 | 2019-09-26 08:00:23 +0200 | [diff] [blame] | 321 | gettimeofday(&new_date, NULL); |
| 322 | if (new_date.tv_sec != iso_time_sec || !new_date.tv_sec) { |
| 323 | get_localtime(new_date.tv_sec, &tm); |
Emeric Brun | b39a375 | 2020-07-02 17:22:17 +0200 | [diff] [blame] | 324 | offset = get_gmt_offset(new_date.tv_sec, &tm); |
| 325 | if (unlikely(strftime(iso_time_str, sizeof(iso_time_str), "%Y-%m-%dT%H:%M:%S.000000+00:00", &tm) != 32)) |
| 326 | strcpy(iso_time_str, "YYYY-mm-ddTHH:MM:SS.000000-00:00"); // make the failure visible but respect format. |
| 327 | iso_time_str[26] = offset[0]; |
| 328 | iso_time_str[27] = offset[1]; |
| 329 | iso_time_str[28] = offset[2]; |
| 330 | iso_time_str[30] = offset[3]; |
| 331 | iso_time_str[31] = offset[4]; |
Willy Tarreau | 93acfa2 | 2019-09-26 08:00:23 +0200 | [diff] [blame] | 332 | iso_time_sec = new_date.tv_sec; |
| 333 | } |
Emeric Brun | b39a375 | 2020-07-02 17:22:17 +0200 | [diff] [blame] | 334 | /* utoa_pad adds a trailing 0 so we save the char for restore */ |
| 335 | c = iso_time_str[26]; |
Willy Tarreau | 93acfa2 | 2019-09-26 08:00:23 +0200 | [diff] [blame] | 336 | utoa_pad(new_date.tv_usec, iso_time_str + 20, 7); |
Emeric Brun | b39a375 | 2020-07-02 17:22:17 +0200 | [diff] [blame] | 337 | iso_time_str[26] = c; |
Willy Tarreau | 93acfa2 | 2019-09-26 08:00:23 +0200 | [diff] [blame] | 338 | if (pad) { |
Emeric Brun | b39a375 | 2020-07-02 17:22:17 +0200 | [diff] [blame] | 339 | iso_time_str[32] = ' '; |
| 340 | iso_time_str[33] = 0; |
Willy Tarreau | 93acfa2 | 2019-09-26 08:00:23 +0200 | [diff] [blame] | 341 | } |
| 342 | return iso_time_str; |
| 343 | } |
| 344 | |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 345 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 346 | * Local variables: |
| 347 | * c-indent-level: 8 |
| 348 | * c-basic-offset: 8 |
| 349 | * End: |
| 350 | */ |