blob: 99aed11f843174cf3b39cd96689d18624fbc5db0 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Time calculation functions.
3 *
Willy Tarreau45a12512011-09-10 16:56:42 +02004 * Copyright 2000-2011 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
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 Tarreaued72d822018-10-17 19:01:24 +020013#include <unistd.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020014#include <sys/time.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020015
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020016#include <haproxy/api.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020017#include <haproxy/time.h>
Willy Tarreau6064b342021-03-23 08:45:42 +010018#include <haproxy/ticks.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020019#include <haproxy/tools.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020020
Christopher Faulet9a655712017-05-11 11:00:15 +020021THREAD_LOCAL unsigned int now_ms; /* internal date in milliseconds (may wrap) */
Christopher Faulet9a655712017-05-11 11:00:15 +020022THREAD_LOCAL struct timeval now; /* internal date is a monotonic function of real clock */
23THREAD_LOCAL struct timeval date; /* the real current date */
Willy Tarreaubaaee002006-06-26 02:48:02 +020024struct timeval start_date; /* the process's start date */
Christopher Faulet9a655712017-05-11 11:00:15 +020025THREAD_LOCAL struct timeval before_poll; /* system date before calling poll() */
26THREAD_LOCAL struct timeval after_poll; /* system date after leaving poll() */
Willy Tarreaubaaee002006-06-26 02:48:02 +020027
Willy Tarreau44982712021-04-11 18:53:58 +020028static unsigned long long now_offset; /* global offset between system time and global time */
29volatile unsigned long long global_now; /* common monotonic date between all threads (32:32) */
30volatile unsigned int global_now_ms; /* common monotonic date in milliseconds (may wrap) */
Willy Tarreau9fefc512017-11-23 14:52:28 +010031
Willy Tarreau93acfa22019-09-26 08:00:23 +020032static THREAD_LOCAL unsigned int iso_time_sec; /* last iso time value for this thread */
Emeric Brunb39a3752020-07-02 17:22:17 +020033static THREAD_LOCAL char iso_time_str[34]; /* ISO time representation of gettimeofday() */
Willy Tarreau93acfa22019-09-26 08:00:23 +020034
Willy Tarreaubaaee002006-06-26 02:48:02 +020035/*
36 * adds <ms> ms to <from>, set the result to <tv> and returns a pointer <tv>
37 */
Willy Tarreau03e78532020-02-25 07:38:05 +010038struct timeval *_tv_ms_add(struct timeval *tv, const struct timeval *from, int ms)
Willy Tarreaubaaee002006-06-26 02:48:02 +020039{
Willy Tarreau42aae5c2007-04-29 17:43:56 +020040 tv->tv_usec = from->tv_usec + (ms % 1000) * 1000;
41 tv->tv_sec = from->tv_sec + (ms / 1000);
Willy Tarreaubaaee002006-06-26 02:48:02 +020042 while (tv->tv_usec >= 1000000) {
43 tv->tv_usec -= 1000000;
44 tv->tv_sec++;
45 }
46 return tv;
47}
48
49/*
50 * compares <tv1> and <tv2> modulo 1ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2
Willy Tarreau42aae5c2007-04-29 17:43:56 +020051 * Must not be used when either argument is eternity. Use tv_ms_cmp2() for that.
Willy Tarreaubaaee002006-06-26 02:48:02 +020052 */
Willy Tarreau03e78532020-02-25 07:38:05 +010053int _tv_ms_cmp(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +020054{
Willy Tarreau42aae5c2007-04-29 17:43:56 +020055 return __tv_ms_cmp(tv1, tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +020056}
57
58/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020059 * compares <tv1> and <tv2> modulo 1 ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2,
Willy Tarreaua6a6a932007-04-28 22:40:08 +020060 * assuming that TV_ETERNITY is greater than everything.
Willy Tarreaubaaee002006-06-26 02:48:02 +020061 */
Willy Tarreau03e78532020-02-25 07:38:05 +010062int _tv_ms_cmp2(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +020063{
Willy Tarreau42aae5c2007-04-29 17:43:56 +020064 return __tv_ms_cmp2(tv1, tv2);
Willy Tarreau8d7d1492007-04-29 10:50:43 +020065}
66
67/*
68 * compares <tv1> and <tv2> modulo 1 ms: returns 1 if tv1 <= tv2, 0 if tv1 > tv2,
69 * assuming that TV_ETERNITY is greater than everything. Returns 0 if tv1 is
70 * TV_ETERNITY, and always assumes that tv2 != TV_ETERNITY. Designed to replace
Willy Tarreau42aae5c2007-04-29 17:43:56 +020071 * occurrences of (tv_ms_cmp2(tv,now) <= 0).
Willy Tarreau8d7d1492007-04-29 10:50:43 +020072 */
Willy Tarreau03e78532020-02-25 07:38:05 +010073int _tv_ms_le2(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreau8d7d1492007-04-29 10:50:43 +020074{
Willy Tarreau42aae5c2007-04-29 17:43:56 +020075 return __tv_ms_le2(tv1, tv2);
76}
Willy Tarreau8d7d1492007-04-29 10:50:43 +020077
Willy Tarreau42aae5c2007-04-29 17:43:56 +020078/*
79 * returns the remaining time between tv1=now and event=tv2
80 * if tv2 is passed, 0 is returned.
81 * Must not be used when either argument is eternity.
82 */
Willy Tarreau03e78532020-02-25 07:38:05 +010083unsigned long _tv_ms_remain(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreau42aae5c2007-04-29 17:43:56 +020084{
85 return __tv_ms_remain(tv1, tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +020086}
87
88/*
89 * returns the remaining time between tv1=now and event=tv2
90 * if tv2 is passed, 0 is returned.
91 * Returns TIME_ETERNITY if tv2 is eternity.
92 */
Willy Tarreau03e78532020-02-25 07:38:05 +010093unsigned long _tv_ms_remain2(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +020094{
Willy Tarreaubaaee002006-06-26 02:48:02 +020095 if (tv_iseternity(tv2))
96 return TIME_ETERNITY;
97
Willy Tarreau42aae5c2007-04-29 17:43:56 +020098 return __tv_ms_remain(tv1, tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +020099}
100
Willy Tarreaubaaee002006-06-26 02:48:02 +0200101/*
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200102 * Returns the time in ms elapsed between tv1 and tv2, assuming that tv1<=tv2.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200103 * Must not be used when either argument is eternity.
104 */
Willy Tarreau03e78532020-02-25 07:38:05 +0100105unsigned long _tv_ms_elapsed(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200106{
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200107 return __tv_ms_elapsed(tv1, tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200108}
109
110/*
Willy Tarreaud825eef2007-05-12 22:35:00 +0200111 * adds <inc> to <from>, set the result to <tv> and returns a pointer <tv>
112 */
Willy Tarreau03e78532020-02-25 07:38:05 +0100113struct timeval *_tv_add(struct timeval *tv, const struct timeval *from, const struct timeval *inc)
Willy Tarreaud825eef2007-05-12 22:35:00 +0200114{
115 return __tv_add(tv, from, inc);
116}
117
118/*
Willy Tarreau0481c202007-05-13 16:03:27 +0200119 * If <inc> is set, then add it to <from> and set the result to <tv>, then
120 * return 1, otherwise return 0. It is meant to be used in if conditions.
121 */
Willy Tarreau03e78532020-02-25 07:38:05 +0100122int _tv_add_ifset(struct timeval *tv, const struct timeval *from, const struct timeval *inc)
Willy Tarreau0481c202007-05-13 16:03:27 +0200123{
124 return __tv_add_ifset(tv, from, inc);
125}
126
127/*
Willy Tarreaud825eef2007-05-12 22:35:00 +0200128 * Computes the remaining time between tv1=now and event=tv2. if tv2 is passed,
129 * 0 is returned. The result is stored into tv.
130 */
Willy Tarreau03e78532020-02-25 07:38:05 +0100131struct timeval *_tv_remain(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv)
Willy Tarreaud825eef2007-05-12 22:35:00 +0200132{
133 return __tv_remain(tv1, tv2, tv);
134}
135
136/*
137 * Computes the remaining time between tv1=now and event=tv2. if tv2 is passed,
138 * 0 is returned. The result is stored into tv. Returns ETERNITY if tv2 is
139 * eternity.
140 */
Willy Tarreau03e78532020-02-25 07:38:05 +0100141struct timeval *_tv_remain2(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv)
Willy Tarreaud825eef2007-05-12 22:35:00 +0200142{
143 return __tv_remain2(tv1, tv2, tv);
144}
145
Willy Tarreau0481c202007-05-13 16:03:27 +0200146/* tv_isle: compares <tv1> and <tv2> : returns 1 if tv1 <= tv2, otherwise 0 */
Willy Tarreau03e78532020-02-25 07:38:05 +0100147int _tv_isle(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreau0481c202007-05-13 16:03:27 +0200148{
149 return __tv_isle(tv1, tv2);
150}
151
152/* tv_isgt: compares <tv1> and <tv2> : returns 1 if tv1 > tv2, otherwise 0 */
Willy Tarreau03e78532020-02-25 07:38:05 +0100153int _tv_isgt(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreau0481c202007-05-13 16:03:27 +0200154{
155 return __tv_isgt(tv1, tv2);
156}
157
Willy Tarreau7649aac2017-11-23 11:52:55 +0100158/* tv_update_date: sets <date> to system time, and sets <now> to something as
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200159 * close as possible to real time, following a monotonic function. The main
160 * principle consists in detecting backwards and forwards time jumps and adjust
161 * an offset to correct them. This function should be called once after each
162 * poll, and never farther apart than MAX_DELAY_MS*2. The poll's timeout should
163 * be passed in <max_wait>, and the return value in <interrupted> (a non-zero
Willy Tarreauc4c80fb2021-04-11 15:00:34 +0200164 * value means that we have not expired the timeout).
165 *
166 * tv_init_process_date() must have been called once first, and
167 * tv_init_thread_date() must also have been called once for each thread.
Christopher Faulet99aad922017-10-31 09:03:51 +0100168 *
Willy Tarreau44982712021-04-11 18:53:58 +0200169 * An offset is used to adjust the current time (date), to figure a monotonic
170 * local time (now). The offset is not critical, as it is only updated after a
171 * clock jump is detected. From this point all threads will apply it to their
172 * locally measured time, and will then agree around a common monotonic
173 * global_now value that serves to further refine their local time. As it is
174 * not possible to atomically update a timeval, both global_now and the
175 * now_offset values are instead stored as 64-bit integers made of two 32 bit
176 * values for the tv_sec and tv_usec parts. The offset is made of two signed
177 * ints so that the clock can be adjusted in the two directions.
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200178 */
Willy Tarreau03e78532020-02-25 07:38:05 +0100179void tv_update_date(int max_wait, int interrupted)
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200180{
Willy Tarreau44982712021-04-11 18:53:58 +0200181 struct timeval min_deadline, max_deadline, tmp_now;
Willy Tarreau7e4a5572021-04-11 15:34:25 +0200182 unsigned int old_now_ms;
Willy Tarreau9fefc512017-11-23 14:52:28 +0100183 unsigned long long old_now;
184 unsigned long long new_now;
Willy Tarreau481795d2021-04-23 15:17:27 +0200185 ullong ofs, ofs_new;
Willy Tarreau44982712021-04-11 18:53:58 +0200186 uint sec_ofs, usec_ofs;
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200187
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200188 gettimeofday(&date, NULL);
Willy Tarreau7649aac2017-11-23 11:52:55 +0100189
Willy Tarreau9fefc512017-11-23 14:52:28 +0100190 /* compute the minimum and maximum local date we may have reached based
Willy Tarreau44982712021-04-11 18:53:58 +0200191 * on our past date and the associated timeout. There are three possible
192 * extremities:
193 * - the new date cannot be older than before_poll
194 * - if not interrupted, the new date cannot be older than
195 * before_poll+max_wait
196 * - in any case the new date cannot be newer than
197 * before_poll+max_wait+some margin (100ms used here).
198 * In case of violation, we'll ignore the current date and instead
199 * restart from the last date we knew.
Willy Tarreau9fefc512017-11-23 14:52:28 +0100200 */
Willy Tarreau44982712021-04-11 18:53:58 +0200201 _tv_ms_add(&min_deadline, &before_poll, max_wait);
202 _tv_ms_add(&max_deadline, &before_poll, max_wait + 100);
Willy Tarreau7649aac2017-11-23 11:52:55 +0100203
Willy Tarreau481795d2021-04-23 15:17:27 +0200204 ofs = HA_ATOMIC_LOAD(&now_offset);
205
Willy Tarreau44982712021-04-11 18:53:58 +0200206 if (unlikely(__tv_islt(&date, &before_poll) || // big jump backwards
207 (!interrupted && __tv_islt(&date, &min_deadline)) || // small jump backwards
208 __tv_islt(&max_deadline, &date))) { // big jump forwards
209 if (!interrupted)
210 _tv_ms_add(&now, &now, max_wait);
211 } else {
212 /* The date is still within expectations. Let's apply the
213 * now_offset to the system date. Note: ofs if made of two
214 * independent signed ints.
Willy Tarreau9fefc512017-11-23 14:52:28 +0100215 */
Willy Tarreau44982712021-04-11 18:53:58 +0200216 now.tv_sec = date.tv_sec + (int)(ofs >> 32); // note: may be positive or negative
217 now.tv_usec = date.tv_usec + (int)ofs; // note: may be positive or negative
218 if ((int)now.tv_usec < 0) {
219 now.tv_usec += 1000000;
220 now.tv_sec -= 1;
221 } else if (now.tv_usec >= 1000000) {
222 now.tv_usec -= 1000000;
223 now.tv_sec += 1;
224 }
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200225 }
226
Willy Tarreau9fefc512017-11-23 14:52:28 +0100227 /* now that we have bounded the local time, let's check if it's
228 * realistic regarding the global date, which only moves forward,
229 * otherwise catch up.
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200230 */
Willy Tarreau9fefc512017-11-23 14:52:28 +0100231 old_now = global_now;
Willy Tarreau7e4a5572021-04-11 15:34:25 +0200232 old_now_ms = global_now_ms;
Willy Tarreau75590582009-03-05 14:54:50 +0100233
Willy Tarreau9fefc512017-11-23 14:52:28 +0100234 do {
235 tmp_now.tv_sec = (unsigned int)(old_now >> 32);
236 tmp_now.tv_usec = old_now & 0xFFFFFFFFU;
Willy Tarreau7649aac2017-11-23 11:52:55 +0100237
Willy Tarreau70cb3022021-04-11 15:17:48 +0200238 if (__tv_islt(&now, &tmp_now))
239 now = tmp_now;
Willy Tarreau9fefc512017-11-23 14:52:28 +0100240
Willy Tarreau70cb3022021-04-11 15:17:48 +0200241 /* now <now> is expected to be the most accurate date,
Willy Tarreau9fefc512017-11-23 14:52:28 +0100242 * equal to <global_now> or newer.
243 */
Willy Tarreau70cb3022021-04-11 15:17:48 +0200244 new_now = ((ullong)now.tv_sec << 32) + (uint)now.tv_usec;
Willy Tarreau1f9e11e2021-04-23 16:03:21 +0200245 now_ms = __tv_to_ms(&now);
Willy Tarreau9fefc512017-11-23 14:52:28 +0100246
Willy Tarreau7e4a5572021-04-11 15:34:25 +0200247 /* let's try to update the global <now> (both in timeval
248 * and ms forms) or loop again.
249 */
Willy Tarreau4d01f3d2021-04-23 15:36:47 +0200250 } while (((new_now != old_now && !_HA_ATOMIC_CAS(&global_now, &old_now, new_now)) ||
251 (now_ms != old_now_ms && !_HA_ATOMIC_CAS(&global_now_ms, &old_now_ms, now_ms))) &&
252 __ha_cpu_relax());
Willy Tarreau9fefc512017-11-23 14:52:28 +0100253
Willy Tarreau44982712021-04-11 18:53:58 +0200254 /* <now> and <now_ms> are now updated to the last value of global_now
255 * and global_now_ms, which were also monotonically updated. We can
256 * compute the latest offset, we don't care who writes it last, the
257 * variations will not break the monotonic property.
Willy Tarreau9fefc512017-11-23 14:52:28 +0100258 */
Willy Tarreau7649aac2017-11-23 11:52:55 +0100259
Willy Tarreau44982712021-04-11 18:53:58 +0200260 sec_ofs = now.tv_sec - date.tv_sec;
261 usec_ofs = now.tv_usec - date.tv_usec;
262 if ((int)usec_ofs < 0) {
263 usec_ofs += 1000000;
264 sec_ofs -= 1;
265 }
Willy Tarreau481795d2021-04-23 15:17:27 +0200266 ofs_new = ((ullong)sec_ofs << 32) + usec_ofs;
267 if (ofs_new != ofs)
268 HA_ATOMIC_STORE(&now_offset, ofs_new);
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200269}
270
Willy Tarreauc4c80fb2021-04-11 15:00:34 +0200271/* must be called once at boot to initialize some global variables */
272void tv_init_process_date()
273{
Willy Tarreau44982712021-04-11 18:53:58 +0200274 now_offset = 0;
Willy Tarreauc4c80fb2021-04-11 15:00:34 +0200275 gettimeofday(&date, NULL);
276 now = after_poll = before_poll = date;
277 global_now = ((ullong)date.tv_sec << 32) + (uint)date.tv_usec;
278 global_now_ms = now.tv_sec * 1000 + now.tv_usec / 1000;
Willy Tarreauc4c80fb2021-04-11 15:00:34 +0200279 ti->idle_pct = 100;
280 tv_update_date(0, 1);
281}
282
283/* must be called once per thread to initialize their thread-local variables.
284 * Note that other threads might also be initializing and running in parallel.
285 */
286void tv_init_thread_date()
287{
288 ullong old_now;
289
290 gettimeofday(&date, NULL);
291 after_poll = before_poll = date;
292
293 old_now = _HA_ATOMIC_LOAD(&global_now);
294 now.tv_sec = old_now >> 32;
295 now.tv_usec = (uint)old_now;
Willy Tarreauc4c80fb2021-04-11 15:00:34 +0200296 ti->idle_pct = 100;
297 tv_update_date(0, 1);
298}
299
Willy Tarreau93acfa22019-09-26 08:00:23 +0200300/* returns the current date as returned by gettimeofday() in ISO+microsecond
301 * format. It uses a thread-local static variable that the reader can consume
302 * for as long as it wants until next call. Thus, do not call it from a signal
303 * handler. If <pad> is non-0, a trailing space will be added. It will always
Emeric Brunb39a3752020-07-02 17:22:17 +0200304 * return exactly 32 or 33 characters (depending on padding) and will always be
305 * zero-terminated, thus it will always fit into a 34 bytes buffer.
306 * This also always include the local timezone (in +/-HH:mm format) .
Willy Tarreau93acfa22019-09-26 08:00:23 +0200307 */
308char *timeofday_as_iso_us(int pad)
309{
310 struct timeval new_date;
311 struct tm tm;
Emeric Brunb39a3752020-07-02 17:22:17 +0200312 const char *offset;
313 char c;
Willy Tarreau93acfa22019-09-26 08:00:23 +0200314 gettimeofday(&new_date, NULL);
315 if (new_date.tv_sec != iso_time_sec || !new_date.tv_sec) {
316 get_localtime(new_date.tv_sec, &tm);
Emeric Brunb39a3752020-07-02 17:22:17 +0200317 offset = get_gmt_offset(new_date.tv_sec, &tm);
318 if (unlikely(strftime(iso_time_str, sizeof(iso_time_str), "%Y-%m-%dT%H:%M:%S.000000+00:00", &tm) != 32))
319 strcpy(iso_time_str, "YYYY-mm-ddTHH:MM:SS.000000-00:00"); // make the failure visible but respect format.
320 iso_time_str[26] = offset[0];
321 iso_time_str[27] = offset[1];
322 iso_time_str[28] = offset[2];
323 iso_time_str[30] = offset[3];
324 iso_time_str[31] = offset[4];
Willy Tarreau93acfa22019-09-26 08:00:23 +0200325 iso_time_sec = new_date.tv_sec;
326 }
Emeric Brunb39a3752020-07-02 17:22:17 +0200327 /* utoa_pad adds a trailing 0 so we save the char for restore */
328 c = iso_time_str[26];
Willy Tarreau93acfa22019-09-26 08:00:23 +0200329 utoa_pad(new_date.tv_usec, iso_time_str + 20, 7);
Emeric Brunb39a3752020-07-02 17:22:17 +0200330 iso_time_str[26] = c;
Willy Tarreau93acfa22019-09-26 08:00:23 +0200331 if (pad) {
Emeric Brunb39a3752020-07-02 17:22:17 +0200332 iso_time_str[32] = ' ';
333 iso_time_str[33] = 0;
Willy Tarreau93acfa22019-09-26 08:00:23 +0200334 }
335 return iso_time_str;
336}
337
Willy Tarreaud825eef2007-05-12 22:35:00 +0200338/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200339 * Local variables:
340 * c-indent-level: 8
341 * c-basic-offset: 8
342 * End:
343 */