blob: 0855c8cc030d04edc4ea347eec25a96ef6b4cfc1 [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) */
22THREAD_LOCAL unsigned int samp_time; /* total elapsed time over current sample */
23THREAD_LOCAL unsigned int idle_time; /* total idle time over current sample */
Christopher Faulet9a655712017-05-11 11:00:15 +020024THREAD_LOCAL struct timeval now; /* internal date is a monotonic function of real clock */
25THREAD_LOCAL struct timeval date; /* the real current date */
Willy Tarreaubaaee002006-06-26 02:48:02 +020026struct timeval start_date; /* the process's start date */
Christopher Faulet9a655712017-05-11 11:00:15 +020027THREAD_LOCAL struct timeval before_poll; /* system date before calling poll() */
28THREAD_LOCAL struct timeval after_poll; /* system date after leaving poll() */
Willy Tarreaubaaee002006-06-26 02:48:02 +020029
Willy Tarreau9fefc512017-11-23 14:52:28 +010030static THREAD_LOCAL struct timeval tv_offset; /* per-thread time ofsset relative to global time */
Willy Tarreau650f3742021-03-17 18:52:18 +010031volatile unsigned long long global_now; /* common date between all threads (32:32) */
Willy Tarreau6064b342021-03-23 08:45:42 +010032volatile unsigned int global_now_ms; /* common date in milliseconds (may wrap) */
Willy Tarreau9fefc512017-11-23 14:52:28 +010033
Willy Tarreau93acfa22019-09-26 08:00:23 +020034static THREAD_LOCAL unsigned int iso_time_sec; /* last iso time value for this thread */
Emeric Brunb39a3752020-07-02 17:22:17 +020035static THREAD_LOCAL char iso_time_str[34]; /* ISO time representation of gettimeofday() */
Willy Tarreau93acfa22019-09-26 08:00:23 +020036
Willy Tarreaubaaee002006-06-26 02:48:02 +020037/*
38 * adds <ms> ms to <from>, set the result to <tv> and returns a pointer <tv>
39 */
Willy Tarreau03e78532020-02-25 07:38:05 +010040struct timeval *_tv_ms_add(struct timeval *tv, const struct timeval *from, int ms)
Willy Tarreaubaaee002006-06-26 02:48:02 +020041{
Willy Tarreau42aae5c2007-04-29 17:43:56 +020042 tv->tv_usec = from->tv_usec + (ms % 1000) * 1000;
43 tv->tv_sec = from->tv_sec + (ms / 1000);
Willy Tarreaubaaee002006-06-26 02:48:02 +020044 while (tv->tv_usec >= 1000000) {
45 tv->tv_usec -= 1000000;
46 tv->tv_sec++;
47 }
48 return tv;
49}
50
51/*
52 * 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 +020053 * Must not be used when either argument is eternity. Use tv_ms_cmp2() for that.
Willy Tarreaubaaee002006-06-26 02:48:02 +020054 */
Willy Tarreau03e78532020-02-25 07:38:05 +010055int _tv_ms_cmp(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +020056{
Willy Tarreau42aae5c2007-04-29 17:43:56 +020057 return __tv_ms_cmp(tv1, tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +020058}
59
60/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020061 * 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 +020062 * assuming that TV_ETERNITY is greater than everything.
Willy Tarreaubaaee002006-06-26 02:48:02 +020063 */
Willy Tarreau03e78532020-02-25 07:38:05 +010064int _tv_ms_cmp2(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +020065{
Willy Tarreau42aae5c2007-04-29 17:43:56 +020066 return __tv_ms_cmp2(tv1, tv2);
Willy Tarreau8d7d1492007-04-29 10:50:43 +020067}
68
69/*
70 * compares <tv1> and <tv2> modulo 1 ms: returns 1 if tv1 <= tv2, 0 if tv1 > tv2,
71 * assuming that TV_ETERNITY is greater than everything. Returns 0 if tv1 is
72 * TV_ETERNITY, and always assumes that tv2 != TV_ETERNITY. Designed to replace
Willy Tarreau42aae5c2007-04-29 17:43:56 +020073 * occurrences of (tv_ms_cmp2(tv,now) <= 0).
Willy Tarreau8d7d1492007-04-29 10:50:43 +020074 */
Willy Tarreau03e78532020-02-25 07:38:05 +010075int _tv_ms_le2(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreau8d7d1492007-04-29 10:50:43 +020076{
Willy Tarreau42aae5c2007-04-29 17:43:56 +020077 return __tv_ms_le2(tv1, tv2);
78}
Willy Tarreau8d7d1492007-04-29 10:50:43 +020079
Willy Tarreau42aae5c2007-04-29 17:43:56 +020080/*
81 * returns the remaining time between tv1=now and event=tv2
82 * if tv2 is passed, 0 is returned.
83 * Must not be used when either argument is eternity.
84 */
Willy Tarreau03e78532020-02-25 07:38:05 +010085unsigned long _tv_ms_remain(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreau42aae5c2007-04-29 17:43:56 +020086{
87 return __tv_ms_remain(tv1, tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +020088}
89
90/*
91 * returns the remaining time between tv1=now and event=tv2
92 * if tv2 is passed, 0 is returned.
93 * Returns TIME_ETERNITY if tv2 is eternity.
94 */
Willy Tarreau03e78532020-02-25 07:38:05 +010095unsigned long _tv_ms_remain2(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +020096{
Willy Tarreaubaaee002006-06-26 02:48:02 +020097 if (tv_iseternity(tv2))
98 return TIME_ETERNITY;
99
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200100 return __tv_ms_remain(tv1, tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200101}
102
Willy Tarreaubaaee002006-06-26 02:48:02 +0200103/*
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200104 * Returns the time in ms elapsed between tv1 and tv2, assuming that tv1<=tv2.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200105 * Must not be used when either argument is eternity.
106 */
Willy Tarreau03e78532020-02-25 07:38:05 +0100107unsigned long _tv_ms_elapsed(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200108{
Willy Tarreau42aae5c2007-04-29 17:43:56 +0200109 return __tv_ms_elapsed(tv1, tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200110}
111
112/*
Willy Tarreaud825eef2007-05-12 22:35:00 +0200113 * adds <inc> to <from>, set the result to <tv> and returns a pointer <tv>
114 */
Willy Tarreau03e78532020-02-25 07:38:05 +0100115struct timeval *_tv_add(struct timeval *tv, const struct timeval *from, const struct timeval *inc)
Willy Tarreaud825eef2007-05-12 22:35:00 +0200116{
117 return __tv_add(tv, from, inc);
118}
119
120/*
Willy Tarreau0481c202007-05-13 16:03:27 +0200121 * If <inc> is set, then add it to <from> and set the result to <tv>, then
122 * return 1, otherwise return 0. It is meant to be used in if conditions.
123 */
Willy Tarreau03e78532020-02-25 07:38:05 +0100124int _tv_add_ifset(struct timeval *tv, const struct timeval *from, const struct timeval *inc)
Willy Tarreau0481c202007-05-13 16:03:27 +0200125{
126 return __tv_add_ifset(tv, from, inc);
127}
128
129/*
Willy Tarreaud825eef2007-05-12 22:35:00 +0200130 * Computes the remaining time between tv1=now and event=tv2. if tv2 is passed,
131 * 0 is returned. The result is stored into tv.
132 */
Willy Tarreau03e78532020-02-25 07:38:05 +0100133struct timeval *_tv_remain(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv)
Willy Tarreaud825eef2007-05-12 22:35:00 +0200134{
135 return __tv_remain(tv1, tv2, tv);
136}
137
138/*
139 * Computes the remaining time between tv1=now and event=tv2. if tv2 is passed,
140 * 0 is returned. The result is stored into tv. Returns ETERNITY if tv2 is
141 * eternity.
142 */
Willy Tarreau03e78532020-02-25 07:38:05 +0100143struct timeval *_tv_remain2(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv)
Willy Tarreaud825eef2007-05-12 22:35:00 +0200144{
145 return __tv_remain2(tv1, tv2, tv);
146}
147
Willy Tarreau0481c202007-05-13 16:03:27 +0200148/* tv_isle: compares <tv1> and <tv2> : returns 1 if tv1 <= tv2, otherwise 0 */
Willy Tarreau03e78532020-02-25 07:38:05 +0100149int _tv_isle(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreau0481c202007-05-13 16:03:27 +0200150{
151 return __tv_isle(tv1, tv2);
152}
153
154/* tv_isgt: compares <tv1> and <tv2> : returns 1 if tv1 > tv2, otherwise 0 */
Willy Tarreau03e78532020-02-25 07:38:05 +0100155int _tv_isgt(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreau0481c202007-05-13 16:03:27 +0200156{
157 return __tv_isgt(tv1, tv2);
158}
159
Willy Tarreau7649aac2017-11-23 11:52:55 +0100160/* tv_update_date: sets <date> to system time, and sets <now> to something as
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200161 * close as possible to real time, following a monotonic function. The main
162 * principle consists in detecting backwards and forwards time jumps and adjust
163 * an offset to correct them. This function should be called once after each
164 * poll, and never farther apart than MAX_DELAY_MS*2. The poll's timeout should
165 * be passed in <max_wait>, and the return value in <interrupted> (a non-zero
Willy Tarreauc4c80fb2021-04-11 15:00:34 +0200166 * value means that we have not expired the timeout).
167 *
168 * tv_init_process_date() must have been called once first, and
169 * tv_init_thread_date() must also have been called once for each thread.
Christopher Faulet99aad922017-10-31 09:03:51 +0100170 *
Willy Tarreau7649aac2017-11-23 11:52:55 +0100171 * An offset is used to adjust the current time (date), to have a monotonic time
Christopher Faulet99aad922017-10-31 09:03:51 +0100172 * (now). It must be global and thread-safe. But a timeval cannot be atomically
Willy Tarreau7649aac2017-11-23 11:52:55 +0100173 * updated. So instead, we store it in a 64-bits integer (offset) whose 32 MSB
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500174 * contain the signed seconds adjustment and the 32 LSB contain the unsigned
Willy Tarreau7649aac2017-11-23 11:52:55 +0100175 * microsecond adjustment. We cannot use a timeval for this since it's never
176 * clearly specified whether a timeval may hold negative values or not.
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200177 */
Willy Tarreau03e78532020-02-25 07:38:05 +0100178void tv_update_date(int max_wait, int interrupted)
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200179{
Willy Tarreaua3315442018-02-05 20:11:38 +0100180 struct timeval adjusted, deadline, tmp_now, tmp_adj;
Willy Tarreau6064b342021-03-23 08:45:42 +0100181 unsigned int old_now_ms, new_now_ms;
Willy Tarreau9fefc512017-11-23 14:52:28 +0100182 unsigned long long old_now;
183 unsigned long long new_now;
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200184
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200185 gettimeofday(&date, NULL);
Willy Tarreau9fefc512017-11-23 14:52:28 +0100186 __tv_add(&adjusted, &date, &tv_offset);
Willy Tarreau7649aac2017-11-23 11:52:55 +0100187
Willy Tarreau9fefc512017-11-23 14:52:28 +0100188 /* compute the minimum and maximum local date we may have reached based
189 * on our past date and the associated timeout.
190 */
191 _tv_ms_add(&deadline, &now, max_wait + MAX_DELAY_MS);
Willy Tarreau7649aac2017-11-23 11:52:55 +0100192
Willy Tarreau9fefc512017-11-23 14:52:28 +0100193 if (unlikely(__tv_islt(&adjusted, &now) || __tv_islt(&deadline, &adjusted))) {
194 /* Large jump. If the poll was interrupted, we consider that the
195 * date has not changed (immediate wake-up), otherwise we add
196 * the poll time-out to the previous date. The new offset is
197 * recomputed.
198 */
199 _tv_ms_add(&adjusted, &now, interrupted ? 0 : max_wait);
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200200 }
201
Willy Tarreau9fefc512017-11-23 14:52:28 +0100202 /* now that we have bounded the local time, let's check if it's
203 * realistic regarding the global date, which only moves forward,
204 * otherwise catch up.
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200205 */
Willy Tarreau9fefc512017-11-23 14:52:28 +0100206 old_now = global_now;
Willy Tarreau75590582009-03-05 14:54:50 +0100207
Willy Tarreau9fefc512017-11-23 14:52:28 +0100208 do {
209 tmp_now.tv_sec = (unsigned int)(old_now >> 32);
210 tmp_now.tv_usec = old_now & 0xFFFFFFFFU;
Willy Tarreaua3315442018-02-05 20:11:38 +0100211 tmp_adj = adjusted;
Willy Tarreau7649aac2017-11-23 11:52:55 +0100212
Willy Tarreaua3315442018-02-05 20:11:38 +0100213 if (__tv_islt(&tmp_adj, &tmp_now))
214 tmp_adj = tmp_now;
Willy Tarreau9fefc512017-11-23 14:52:28 +0100215
216 /* now <adjusted> is expected to be the most accurate date,
217 * equal to <global_now> or newer.
218 */
Willy Tarreaua3315442018-02-05 20:11:38 +0100219 new_now = (((unsigned long long)tmp_adj.tv_sec) << 32) + (unsigned int)tmp_adj.tv_usec;
Willy Tarreau9fefc512017-11-23 14:52:28 +0100220
221 /* let's try to update the global <now> or loop again */
Olivier Houchardcab0f0b2019-03-08 18:55:31 +0100222 } while (!_HA_ATOMIC_CAS(&global_now, &old_now, new_now));
Willy Tarreau9fefc512017-11-23 14:52:28 +0100223
Willy Tarreaua3315442018-02-05 20:11:38 +0100224 adjusted = tmp_adj;
225
Willy Tarreau9fefc512017-11-23 14:52:28 +0100226 /* the new global date when we looked was old_now, and the new one is
227 * new_now == adjusted. We can recompute our local offset.
228 */
229 tv_offset.tv_sec = adjusted.tv_sec - date.tv_sec;
230 tv_offset.tv_usec = adjusted.tv_usec - date.tv_usec;
231 if (tv_offset.tv_usec < 0) {
232 tv_offset.tv_usec += 1000000;
233 tv_offset.tv_sec--;
234 }
Willy Tarreau7649aac2017-11-23 11:52:55 +0100235
Willy Tarreau75590582009-03-05 14:54:50 +0100236 now = adjusted;
Willy Tarreau61c72c32021-04-11 01:40:13 +0200237 now_ms = now.tv_sec * 1000 + now.tv_usec / 1000;
Willy Tarreau6064b342021-03-23 08:45:42 +0100238
239 /* update the global current millisecond */
240 old_now_ms = global_now_ms;
241 do {
242 new_now_ms = old_now_ms;
Willy Tarreauc4c80fb2021-04-11 15:00:34 +0200243 if (tick_is_lt(new_now_ms, now_ms))
Willy Tarreau6064b342021-03-23 08:45:42 +0100244 new_now_ms = now_ms;
245 } while (!_HA_ATOMIC_CAS(&global_now_ms, &old_now_ms, new_now_ms));
246
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200247 return;
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200248}
249
Willy Tarreauc4c80fb2021-04-11 15:00:34 +0200250/* must be called once at boot to initialize some global variables */
251void tv_init_process_date()
252{
253 tv_zero(&tv_offset);
254 gettimeofday(&date, NULL);
255 now = after_poll = before_poll = date;
256 global_now = ((ullong)date.tv_sec << 32) + (uint)date.tv_usec;
257 global_now_ms = now.tv_sec * 1000 + now.tv_usec / 1000;
258 samp_time = idle_time = 0;
259 ti->idle_pct = 100;
260 tv_update_date(0, 1);
261}
262
263/* must be called once per thread to initialize their thread-local variables.
264 * Note that other threads might also be initializing and running in parallel.
265 */
266void tv_init_thread_date()
267{
268 ullong old_now;
269
270 gettimeofday(&date, NULL);
271 after_poll = before_poll = date;
272
273 old_now = _HA_ATOMIC_LOAD(&global_now);
274 now.tv_sec = old_now >> 32;
275 now.tv_usec = (uint)old_now;
276
277 tv_offset.tv_sec = now.tv_sec - date.tv_sec;
278 tv_offset.tv_usec = now.tv_usec - date.tv_usec;
279 if (tv_offset.tv_usec < 0) {
280 tv_offset.tv_usec += 1000000;
281 tv_offset.tv_sec--;
282 }
283
284 samp_time = idle_time = 0;
285 ti->idle_pct = 100;
286 tv_update_date(0, 1);
287}
288
Willy Tarreau93acfa22019-09-26 08:00:23 +0200289/* returns the current date as returned by gettimeofday() in ISO+microsecond
290 * format. It uses a thread-local static variable that the reader can consume
291 * for as long as it wants until next call. Thus, do not call it from a signal
292 * handler. If <pad> is non-0, a trailing space will be added. It will always
Emeric Brunb39a3752020-07-02 17:22:17 +0200293 * return exactly 32 or 33 characters (depending on padding) and will always be
294 * zero-terminated, thus it will always fit into a 34 bytes buffer.
295 * This also always include the local timezone (in +/-HH:mm format) .
Willy Tarreau93acfa22019-09-26 08:00:23 +0200296 */
297char *timeofday_as_iso_us(int pad)
298{
299 struct timeval new_date;
300 struct tm tm;
Emeric Brunb39a3752020-07-02 17:22:17 +0200301 const char *offset;
302 char c;
Willy Tarreau93acfa22019-09-26 08:00:23 +0200303 gettimeofday(&new_date, NULL);
304 if (new_date.tv_sec != iso_time_sec || !new_date.tv_sec) {
305 get_localtime(new_date.tv_sec, &tm);
Emeric Brunb39a3752020-07-02 17:22:17 +0200306 offset = get_gmt_offset(new_date.tv_sec, &tm);
307 if (unlikely(strftime(iso_time_str, sizeof(iso_time_str), "%Y-%m-%dT%H:%M:%S.000000+00:00", &tm) != 32))
308 strcpy(iso_time_str, "YYYY-mm-ddTHH:MM:SS.000000-00:00"); // make the failure visible but respect format.
309 iso_time_str[26] = offset[0];
310 iso_time_str[27] = offset[1];
311 iso_time_str[28] = offset[2];
312 iso_time_str[30] = offset[3];
313 iso_time_str[31] = offset[4];
Willy Tarreau93acfa22019-09-26 08:00:23 +0200314 iso_time_sec = new_date.tv_sec;
315 }
Emeric Brunb39a3752020-07-02 17:22:17 +0200316 /* utoa_pad adds a trailing 0 so we save the char for restore */
317 c = iso_time_str[26];
Willy Tarreau93acfa22019-09-26 08:00:23 +0200318 utoa_pad(new_date.tv_usec, iso_time_str + 20, 7);
Emeric Brunb39a3752020-07-02 17:22:17 +0200319 iso_time_str[26] = c;
Willy Tarreau93acfa22019-09-26 08:00:23 +0200320 if (pad) {
Emeric Brunb39a3752020-07-02 17:22:17 +0200321 iso_time_str[32] = ' ';
322 iso_time_str[33] = 0;
Willy Tarreau93acfa22019-09-26 08:00:23 +0200323 }
324 return iso_time_str;
325}
326
Willy Tarreaud825eef2007-05-12 22:35:00 +0200327/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200328 * Local variables:
329 * c-indent-level: 8
330 * c-basic-offset: 8
331 * End:
332 */