blob: ec2133c8b9e39733e2ad69db7368280242a90ec3 [file] [log] [blame]
Willy Tarreau55542642021-10-08 09:33:24 +02001/*
2 * General time-keeping code and variables
3 *
4 * Copyright 2000-2021 Willy Tarreau <w@1wt.eu>
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 Tarreau6cb0c392021-10-08 14:48:30 +020014#include <signal.h>
Willy Tarreau55542642021-10-08 09:33:24 +020015#include <time.h>
16
Willy Tarreau44c58da2021-10-08 12:27:54 +020017#ifdef USE_THREAD
18#include <pthread.h>
19#endif
20
Willy Tarreau55542642021-10-08 09:33:24 +020021#include <haproxy/api.h>
Willy Tarreauf9d5e102021-10-08 10:43:59 +020022#include <haproxy/activity.h>
Willy Tarreau55542642021-10-08 09:33:24 +020023#include <haproxy/clock.h>
Willy Tarreau6cb0c392021-10-08 14:48:30 +020024#include <haproxy/signal-t.h>
Willy Tarreau55542642021-10-08 09:33:24 +020025#include <haproxy/time.h>
26#include <haproxy/tinfo-t.h>
27#include <haproxy/tools.h>
28
29struct timeval start_date; /* the process's start date in wall-clock time */
Willy Tarreauda4aa692023-05-17 09:02:21 +020030struct timeval ready_date; /* date when the process was considered ready */
Willy Tarreauc05d30e2023-04-28 14:50:29 +020031ullong start_time_ns; /* the process's start date in internal monotonic time (ns) */
Willy Tarreau69530f52023-04-28 09:16:15 +020032volatile ullong global_now_ns; /* common monotonic date between all threads, in ns (wraps every 585 yr) */
Willy Tarreau55542642021-10-08 09:33:24 +020033volatile uint global_now_ms; /* common monotonic date in milliseconds (may wrap) */
34
Willy Tarreau69530f52023-04-28 09:16:15 +020035THREAD_ALIGNED(64) static llong now_offset; /* global offset between system time and global time in ns */
Willy Tarreau55542642021-10-08 09:33:24 +020036
Willy Tarreau69530f52023-04-28 09:16:15 +020037THREAD_LOCAL ullong now_ns; /* internal monotonic date derived from real clock, in ns (wraps every 585 yr) */
Willy Tarreau55542642021-10-08 09:33:24 +020038THREAD_LOCAL uint now_ms; /* internal monotonic date in milliseconds (may wrap) */
Willy Tarreau55542642021-10-08 09:33:24 +020039THREAD_LOCAL struct timeval date; /* the real current date (wall-clock time) */
Willy Tarreau55542642021-10-08 09:33:24 +020040
Willy Tarreau2c6a9982021-10-08 11:38:30 +020041static THREAD_LOCAL struct timeval before_poll; /* system date before calling poll() */
42static THREAD_LOCAL struct timeval after_poll; /* system date after leaving poll() */
Willy Tarreauf9d5e102021-10-08 10:43:59 +020043static THREAD_LOCAL unsigned int samp_time; /* total elapsed time over current sample */
44static THREAD_LOCAL unsigned int idle_time; /* total idle time over current sample */
Willy Tarreau55542642021-10-08 09:33:24 +020045static THREAD_LOCAL unsigned int iso_time_sec; /* last iso time value for this thread */
46static THREAD_LOCAL char iso_time_str[34]; /* ISO time representation of gettimeofday() */
47
Willy Tarreau21694982021-10-08 15:09:17 +020048#if defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0) && defined(_POSIX_THREAD_CPUTIME)
49static clockid_t per_thread_clock_id[MAX_THREADS];
50#endif
51
Willy Tarreau55542642021-10-08 09:33:24 +020052/* returns the system's monotonic time in nanoseconds if supported, otherwise zero */
53uint64_t now_mono_time(void)
54{
55 uint64_t ret = 0;
Willy Tarreau6cb0c392021-10-08 14:48:30 +020056#if defined(_POSIX_TIMERS) && defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0) && defined(_POSIX_MONOTONIC_CLOCK)
Willy Tarreau55542642021-10-08 09:33:24 +020057 struct timespec ts;
58 clock_gettime(CLOCK_MONOTONIC, &ts);
59 ret = ts.tv_sec * 1000000000ULL + ts.tv_nsec;
60#endif
61 return ret;
62}
63
Aurelien DARRAGON07cbd8e2022-11-25 08:56:46 +010064/* Returns the system's monotonic time in nanoseconds.
65 * Uses the coarse clock source if supported (for fast but
66 * less precise queries with limited resource usage).
67 * Fallback to now_mono_time() if coarse source is not supported,
68 * which may itself return 0 if not supported either.
69 */
70uint64_t now_mono_time_fast(void)
71{
72#if defined(CLOCK_MONOTONIC_COARSE)
73 struct timespec ts;
74
75 clock_gettime(CLOCK_MONOTONIC_COARSE, &ts);
76 return (ts.tv_sec * 1000000000ULL + ts.tv_nsec);
77#else
78 /* fallback to regular mono time,
79 * returns 0 if not supported
80 */
81 return now_mono_time();
82#endif
83}
84
Willy Tarreau55542642021-10-08 09:33:24 +020085/* returns the current thread's cumulated CPU time in nanoseconds if supported, otherwise zero */
86uint64_t now_cpu_time(void)
87{
88 uint64_t ret = 0;
89#if defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0) && defined(_POSIX_THREAD_CPUTIME)
90 struct timespec ts;
91 clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts);
92 ret = ts.tv_sec * 1000000000ULL + ts.tv_nsec;
93#endif
94 return ret;
95}
96
Aurelien DARRAGONdf188f12023-04-04 17:21:40 +020097/* Returns the current thread's cumulated CPU time in nanoseconds.
98 *
99 * thread_local timer is cached so that call is less precise but also less
100 * expensive if heavily used.
101 * We use the mono time as a cache expiration hint since now_cpu_time() is
102 * known to be much more expensive than now_mono_time_fast() on systems
103 * supporting the COARSE clock source.
104 *
105 * Returns 0 if either now_mono_time_fast() or now_cpu_time() are not
106 * supported.
107 */
108uint64_t now_cpu_time_fast(void)
109{
110 static THREAD_LOCAL uint64_t mono_cache = 0;
111 static THREAD_LOCAL uint64_t cpu_cache = 0;
112 uint64_t mono_cur;
113
114 mono_cur = now_mono_time_fast();
115 if (unlikely(mono_cur != mono_cache)) {
116 /* global mono clock was updated: local cache is outdated */
117 cpu_cache = now_cpu_time();
118 mono_cache = mono_cur;
119 }
120 return cpu_cache;
121}
122
Willy Tarreau55542642021-10-08 09:33:24 +0200123/* returns another thread's cumulated CPU time in nanoseconds if supported, otherwise zero */
Willy Tarreau21694982021-10-08 15:09:17 +0200124uint64_t now_cpu_time_thread(int thr)
Willy Tarreau55542642021-10-08 09:33:24 +0200125{
126 uint64_t ret = 0;
127#if defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0) && defined(_POSIX_THREAD_CPUTIME)
128 struct timespec ts;
Willy Tarreau21694982021-10-08 15:09:17 +0200129 clock_gettime(per_thread_clock_id[thr], &ts);
Willy Tarreau55542642021-10-08 09:33:24 +0200130 ret = ts.tv_sec * 1000000000ULL + ts.tv_nsec;
131#endif
132 return ret;
133}
134
Willy Tarreau44c58da2021-10-08 12:27:54 +0200135/* set the clock source for the local thread */
136void clock_set_local_source(void)
137{
138#if defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0) && defined(_POSIX_THREAD_CPUTIME)
139#ifdef USE_THREAD
Willy Tarreau21694982021-10-08 15:09:17 +0200140 pthread_getcpuclockid(pthread_self(), &per_thread_clock_id[tid]);
Willy Tarreau44c58da2021-10-08 12:27:54 +0200141#else
Willy Tarreau21694982021-10-08 15:09:17 +0200142 per_thread_clock_id[tid] = CLOCK_THREAD_CPUTIME_ID;
Willy Tarreau44c58da2021-10-08 12:27:54 +0200143#endif
144#endif
145}
146
Willy Tarreau6cb0c392021-10-08 14:48:30 +0200147/* registers a timer <tmr> of type timer_t delivering signal <sig> with value
148 * <val>. It tries on the current thread's clock ID first and falls back to
149 * CLOCK_REALTIME. Returns non-zero on success, 1 on failure.
150 */
151int clock_setup_signal_timer(void *tmr, int sig, int val)
152{
153 int ret = 0;
154
155#if defined(USE_RT) && (_POSIX_TIMERS > 0) && defined(_POSIX_THREAD_CPUTIME)
156 struct sigevent sev = { };
157 timer_t *timer = tmr;
158 sigset_t set;
159
160 /* unblock the WDTSIG signal we intend to use */
161 sigemptyset(&set);
162 sigaddset(&set, WDTSIG);
163 ha_sigmask(SIG_UNBLOCK, &set, NULL);
164
165 /* this timer will signal WDTSIG when it fires, with tid in the si_int
166 * field (important since any thread will receive the signal).
167 */
168 sev.sigev_notify = SIGEV_SIGNAL;
169 sev.sigev_signo = sig;
170 sev.sigev_value.sival_int = val;
Willy Tarreau21694982021-10-08 15:09:17 +0200171 if (timer_create(per_thread_clock_id[tid], &sev, timer) != -1 ||
Willy Tarreau6cb0c392021-10-08 14:48:30 +0200172 timer_create(CLOCK_REALTIME, &sev, timer) != -1)
173 ret = 1;
174#endif
175 return ret;
176}
177
Willy Tarreau69530f52023-04-28 09:16:15 +0200178/* clock_update_date: sets <date> to system time, and sets <now_ns> to something
179 * as close as possible to real time, following a monotonic function. The main
Willy Tarreau55542642021-10-08 09:33:24 +0200180 * principle consists in detecting backwards and forwards time jumps and adjust
181 * an offset to correct them. This function should be called once after each
182 * poll, and never farther apart than MAX_DELAY_MS*2. The poll's timeout should
183 * be passed in <max_wait>, and the return value in <interrupted> (a non-zero
184 * value means that we have not expired the timeout).
185 *
186 * clock_init_process_date() must have been called once first, and
187 * clock_init_thread_date() must also have been called once for each thread.
188 *
189 * An offset is used to adjust the current time (date), to figure a monotonic
Willy Tarreau69530f52023-04-28 09:16:15 +0200190 * local time (now_ns). The offset is not critical, as it is only updated after
191 * a clock jump is detected. From this point all threads will apply it to their
Willy Tarreau55542642021-10-08 09:33:24 +0200192 * locally measured time, and will then agree around a common monotonic
Willy Tarreau69530f52023-04-28 09:16:15 +0200193 * global_now_ns value that serves to further refine their local time. Both
194 * now_ns and global_now_ns are 64-bit integers counting nanoseconds since a
195 * vague reference (it starts roughly 20s before the next wrap-around of the
196 * millisecond counter after boot). The offset is also an integral number of
197 * nanoseconds, but it's signed so that the clock can be adjusted in the two
198 * directions.
Willy Tarreau55542642021-10-08 09:33:24 +0200199 */
Willy Tarreaua7004202022-09-21 07:37:27 +0200200void clock_update_local_date(int max_wait, int interrupted)
Willy Tarreau55542642021-10-08 09:33:24 +0200201{
Willy Tarreaua7004202022-09-21 07:37:27 +0200202 struct timeval min_deadline, max_deadline;
Willy Tarreau55542642021-10-08 09:33:24 +0200203
204 gettimeofday(&date, NULL);
205
206 /* compute the minimum and maximum local date we may have reached based
207 * on our past date and the associated timeout. There are three possible
208 * extremities:
209 * - the new date cannot be older than before_poll
210 * - if not interrupted, the new date cannot be older than
211 * before_poll+max_wait
212 * - in any case the new date cannot be newer than
213 * before_poll+max_wait+some margin (100ms used here).
214 * In case of violation, we'll ignore the current date and instead
215 * restart from the last date we knew.
216 */
217 _tv_ms_add(&min_deadline, &before_poll, max_wait);
218 _tv_ms_add(&max_deadline, &before_poll, max_wait + 100);
219
Willy Tarreau55542642021-10-08 09:33:24 +0200220 if (unlikely(__tv_islt(&date, &before_poll) || // big jump backwards
221 (!interrupted && __tv_islt(&date, &min_deadline)) || // small jump backwards
222 __tv_islt(&max_deadline, &date))) { // big jump forwards
223 if (!interrupted)
Willy Tarreau69530f52023-04-28 09:16:15 +0200224 now_ns += ms_to_ns(max_wait);
Willy Tarreau55542642021-10-08 09:33:24 +0200225 } else {
226 /* The date is still within expectations. Let's apply the
227 * now_offset to the system date. Note: ofs if made of two
228 * independent signed ints.
229 */
Willy Tarreau69530f52023-04-28 09:16:15 +0200230 now_ns = tv_to_ns(&date) + HA_ATOMIC_LOAD(&now_offset);
Willy Tarreau55542642021-10-08 09:33:24 +0200231 }
Willy Tarreau69530f52023-04-28 09:16:15 +0200232 now_ms = ns_to_ms(now_ns);
Willy Tarreaua7004202022-09-21 07:37:27 +0200233}
234
235void clock_update_global_date()
236{
Willy Tarreau69530f52023-04-28 09:16:15 +0200237 ullong old_now_ns;
Willy Tarreaua7004202022-09-21 07:37:27 +0200238 uint old_now_ms;
Willy Tarreaua7004202022-09-21 07:37:27 +0200239
Willy Tarreau55542642021-10-08 09:33:24 +0200240 /* now that we have bounded the local time, let's check if it's
241 * realistic regarding the global date, which only moves forward,
242 * otherwise catch up.
243 */
Willy Tarreau69530f52023-04-28 09:16:15 +0200244 old_now_ns = _HA_ATOMIC_LOAD(&global_now_ns);
Willy Tarreau55542642021-10-08 09:33:24 +0200245 old_now_ms = global_now_ms;
246
247 do {
Willy Tarreau69530f52023-04-28 09:16:15 +0200248 if (now_ns < old_now_ns)
249 now_ns = old_now_ns;
Willy Tarreau55542642021-10-08 09:33:24 +0200250
Willy Tarreau69530f52023-04-28 09:16:15 +0200251 /* now <now_ns> is expected to be the most accurate date,
252 * equal to <global_now_ns> or newer. Updating the global
Willy Tarreau4eaf85f2022-09-21 08:21:45 +0200253 * date too often causes extreme contention and is not
254 * needed: it's only used to help threads run at the
255 * same date in case of local drift, and the global date,
256 * which changes, is only used by freq counters (a choice
257 * which is debatable by the way since it changes under us).
258 * Tests have seen that the contention can be reduced from
259 * 37% in this function to almost 0% when keeping clocks
260 * synchronized no better than 32 microseconds, so that's
261 * what we're doing here.
Willy Tarreau55542642021-10-08 09:33:24 +0200262 */
Willy Tarreau69530f52023-04-28 09:16:15 +0200263 now_ms = ns_to_ms(now_ns);
Willy Tarreau4eaf85f2022-09-21 08:21:45 +0200264
Willy Tarreau69530f52023-04-28 09:16:15 +0200265 if (!((now_ns ^ old_now_ns) & ~0x7FFFULL))
Willy Tarreau4eaf85f2022-09-21 08:21:45 +0200266 return;
267
Willy Tarreau69530f52023-04-28 09:16:15 +0200268 /* let's try to update the global_now_ns (both in nanoseconds
Willy Tarreau55542642021-10-08 09:33:24 +0200269 * and ms forms) or loop again.
270 */
Willy Tarreau69530f52023-04-28 09:16:15 +0200271 } while ((!_HA_ATOMIC_CAS(&global_now_ns, &old_now_ns, now_ns) ||
Willy Tarreau55542642021-10-08 09:33:24 +0200272 (now_ms != old_now_ms && !_HA_ATOMIC_CAS(&global_now_ms, &old_now_ms, now_ms))) &&
273 __ha_cpu_relax());
274
Willy Tarreau69530f52023-04-28 09:16:15 +0200275 /* <now_ns> and <now_ms> are now updated to the last value of
276 * global_now_ns and global_now_ms, which were also monotonically
277 * updated. We can compute the latest offset, we don't care who writes
278 * it last, the variations will not break the monotonic property.
Willy Tarreau55542642021-10-08 09:33:24 +0200279 */
Willy Tarreau69530f52023-04-28 09:16:15 +0200280 HA_ATOMIC_STORE(&now_offset, now_ns - tv_to_ns(&date));
Willy Tarreau55542642021-10-08 09:33:24 +0200281}
282
283/* must be called once at boot to initialize some global variables */
284void clock_init_process_date(void)
285{
286 now_offset = 0;
287 gettimeofday(&date, NULL);
Willy Tarreau69530f52023-04-28 09:16:15 +0200288 after_poll = before_poll = date;
289 now_ns = global_now_ns = tv_to_ns(&date);
290 global_now_ms = ns_to_ms(now_ns);
Willy Tarreau28360dc2023-02-07 14:44:44 +0100291
292 /* force time to wrap 20s after boot: we first compute the time offset
293 * that once applied to the wall-clock date will make the local time
294 * wrap in 5 seconds. This offset is applied to the process-wide time,
295 * and will be used to recompute the local time, both of which will
296 * match and continue from this shifted date.
297 */
Willy Tarreau69530f52023-04-28 09:16:15 +0200298 now_offset = sec_to_ns((uint)((uint)(-global_now_ms) / 1000U - BOOT_TIME_WRAP_SEC));
299 global_now_ns += now_offset;
300 now_ns = global_now_ns;
301 now_ms = global_now_ms = ns_to_ms(now_ns);
Willy Tarreau28360dc2023-02-07 14:44:44 +0100302
Willy Tarreau45c38e22021-09-30 18:28:49 +0200303 th_ctx->idle_pct = 100;
Willy Tarreau55542642021-10-08 09:33:24 +0200304 clock_update_date(0, 1);
305}
306
Willy Tarreau53454902023-05-16 19:01:55 +0200307void clock_adjust_now_offset(void)
308{
309 HA_ATOMIC_STORE(&now_offset, now_ns - tv_to_ns(&date));
310}
311
Willy Tarreau55542642021-10-08 09:33:24 +0200312/* must be called once per thread to initialize their thread-local variables.
313 * Note that other threads might also be initializing and running in parallel.
314 */
315void clock_init_thread_date(void)
316{
Willy Tarreau55542642021-10-08 09:33:24 +0200317 gettimeofday(&date, NULL);
318 after_poll = before_poll = date;
319
Willy Tarreau69530f52023-04-28 09:16:15 +0200320 now_ns = _HA_ATOMIC_LOAD(&global_now_ns);
Willy Tarreau45c38e22021-09-30 18:28:49 +0200321 th_ctx->idle_pct = 100;
Aurelien DARRAGON16d6c0c2022-11-10 11:47:47 +0100322 th_ctx->prev_cpu_time = now_cpu_time();
Willy Tarreau55542642021-10-08 09:33:24 +0200323 clock_update_date(0, 1);
324}
325
Willy Tarreauf9d5e102021-10-08 10:43:59 +0200326/* report the average CPU idle percentage over all running threads, between 0 and 100 */
327uint clock_report_idle(void)
328{
329 uint total = 0;
330 uint rthr = 0;
331 uint thr;
332
333 for (thr = 0; thr < MAX_THREADS; thr++) {
Willy Tarreau1e7f0d62022-06-27 16:22:22 +0200334 if (!ha_thread_info[thr].tg ||
335 !(ha_thread_info[thr].tg->threads_enabled & ha_thread_info[thr].ltid_bit))
Willy Tarreauf9d5e102021-10-08 10:43:59 +0200336 continue;
Willy Tarreau45c38e22021-09-30 18:28:49 +0200337 total += HA_ATOMIC_LOAD(&ha_thread_ctx[thr].idle_pct);
Willy Tarreauf9d5e102021-10-08 10:43:59 +0200338 rthr++;
339 }
340 return rthr ? total / rthr : 0;
341}
342
343/* Update the idle time value twice a second, to be called after
344 * clock_update_date() when called after poll(), and currently called only by
345 * clock_leaving_poll() below. It relies on <before_poll> to be updated to
346 * the system time before calling poll().
347 */
348static inline void clock_measure_idle(void)
349{
350 /* Let's compute the idle to work ratio. We worked between after_poll
351 * and before_poll, and slept between before_poll and date. The idle_pct
352 * is updated at most twice every second. Note that the current second
353 * rarely changes so we avoid a multiply when not needed.
354 */
355 int delta;
356
357 if ((delta = date.tv_sec - before_poll.tv_sec))
358 delta *= 1000000;
359 idle_time += delta + (date.tv_usec - before_poll.tv_usec);
360
361 if ((delta = date.tv_sec - after_poll.tv_sec))
362 delta *= 1000000;
363 samp_time += delta + (date.tv_usec - after_poll.tv_usec);
364
365 after_poll.tv_sec = date.tv_sec; after_poll.tv_usec = date.tv_usec;
366 if (samp_time < 500000)
367 return;
368
Willy Tarreau45c38e22021-09-30 18:28:49 +0200369 HA_ATOMIC_STORE(&th_ctx->idle_pct, (100ULL * idle_time + samp_time / 2) / samp_time);
Willy Tarreauf9d5e102021-10-08 10:43:59 +0200370 idle_time = samp_time = 0;
371}
372
373/* Collect date and time information after leaving poll(). <timeout> must be
374 * set to the maximum sleep time passed to poll (in milliseconds), and
375 * <interrupted> must be zero if the poller reached the timeout or non-zero
376 * otherwise, which generally is provided by the poller's return value.
377 */
378void clock_leaving_poll(int timeout, int interrupted)
379{
380 clock_measure_idle();
Willy Tarreau45c38e22021-09-30 18:28:49 +0200381 th_ctx->prev_cpu_time = now_cpu_time();
382 th_ctx->prev_mono_time = now_mono_time();
Willy Tarreauf9d5e102021-10-08 10:43:59 +0200383}
384
385/* Collect date and time information before calling poll(). This will be used
386 * to count the run time of the past loop and the sleep time of the next poll.
Ilya Shipitsin4a689da2022-10-29 09:34:32 +0500387 * It also compares the elapsed and cpu times during the activity period to
Willy Tarreauf9d5e102021-10-08 10:43:59 +0200388 * estimate the amount of stolen time, which is reported if higher than half
389 * a millisecond.
390 */
391void clock_entering_poll(void)
392{
393 uint64_t new_mono_time;
394 uint64_t new_cpu_time;
Willy Tarreau20adfde2021-10-08 11:34:46 +0200395 uint32_t run_time;
Willy Tarreauf9d5e102021-10-08 10:43:59 +0200396 int64_t stolen;
397
398 gettimeofday(&before_poll, NULL);
399
Willy Tarreau20adfde2021-10-08 11:34:46 +0200400 run_time = (before_poll.tv_sec - after_poll.tv_sec) * 1000000U + (before_poll.tv_usec - after_poll.tv_usec);
401
Willy Tarreauf9d5e102021-10-08 10:43:59 +0200402 new_cpu_time = now_cpu_time();
403 new_mono_time = now_mono_time();
404
Willy Tarreau45c38e22021-09-30 18:28:49 +0200405 if (th_ctx->prev_cpu_time && th_ctx->prev_mono_time) {
406 new_cpu_time -= th_ctx->prev_cpu_time;
407 new_mono_time -= th_ctx->prev_mono_time;
Willy Tarreauf9d5e102021-10-08 10:43:59 +0200408 stolen = new_mono_time - new_cpu_time;
409 if (unlikely(stolen >= 500000)) {
410 stolen /= 500000;
411 /* more than half a millisecond difference might
412 * indicate an undesired preemption.
413 */
414 report_stolen_time(stolen);
415 }
416 }
Willy Tarreau20adfde2021-10-08 11:34:46 +0200417
418 /* update the average runtime */
419 activity_count_runtime(run_time);
Willy Tarreauf9d5e102021-10-08 10:43:59 +0200420}
421
Willy Tarreau55542642021-10-08 09:33:24 +0200422/* returns the current date as returned by gettimeofday() in ISO+microsecond
423 * format. It uses a thread-local static variable that the reader can consume
424 * for as long as it wants until next call. Thus, do not call it from a signal
425 * handler. If <pad> is non-0, a trailing space will be added. It will always
426 * return exactly 32 or 33 characters (depending on padding) and will always be
427 * zero-terminated, thus it will always fit into a 34 bytes buffer.
428 * This also always include the local timezone (in +/-HH:mm format) .
429 */
430char *timeofday_as_iso_us(int pad)
431{
432 struct timeval new_date;
433 struct tm tm;
434 const char *offset;
435 char c;
436
437 gettimeofday(&new_date, NULL);
438 if (new_date.tv_sec != iso_time_sec || !new_date.tv_sec) {
439 get_localtime(new_date.tv_sec, &tm);
440 offset = get_gmt_offset(new_date.tv_sec, &tm);
441 if (unlikely(strftime(iso_time_str, sizeof(iso_time_str), "%Y-%m-%dT%H:%M:%S.000000+00:00", &tm) != 32))
Willy Tarreaufc458ec2023-04-07 18:11:39 +0200442 strlcpy2(iso_time_str, "YYYY-mm-ddTHH:MM:SS.000000-00:00", sizeof(iso_time_str)); // make the failure visible but respect format.
Willy Tarreau55542642021-10-08 09:33:24 +0200443 iso_time_str[26] = offset[0];
444 iso_time_str[27] = offset[1];
445 iso_time_str[28] = offset[2];
446 iso_time_str[30] = offset[3];
447 iso_time_str[31] = offset[4];
448 iso_time_sec = new_date.tv_sec;
449 }
450
451 /* utoa_pad adds a trailing 0 so we save the char for restore */
452 c = iso_time_str[26];
453 utoa_pad(new_date.tv_usec, iso_time_str + 20, 7);
454 iso_time_str[26] = c;
455 if (pad) {
456 iso_time_str[32] = ' ';
457 iso_time_str[33] = 0;
458 }
459 return iso_time_str;
460}