Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 1 | /* |
| 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 Tarreau | 6cb0c39 | 2021-10-08 14:48:30 +0200 | [diff] [blame] | 14 | #include <signal.h> |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 15 | #include <time.h> |
| 16 | |
Willy Tarreau | 44c58da | 2021-10-08 12:27:54 +0200 | [diff] [blame] | 17 | #ifdef USE_THREAD |
| 18 | #include <pthread.h> |
| 19 | #endif |
| 20 | |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 21 | #include <haproxy/api.h> |
Willy Tarreau | f9d5e10 | 2021-10-08 10:43:59 +0200 | [diff] [blame] | 22 | #include <haproxy/activity.h> |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 23 | #include <haproxy/clock.h> |
Willy Tarreau | 6cb0c39 | 2021-10-08 14:48:30 +0200 | [diff] [blame] | 24 | #include <haproxy/signal-t.h> |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 25 | #include <haproxy/time.h> |
| 26 | #include <haproxy/tinfo-t.h> |
| 27 | #include <haproxy/tools.h> |
| 28 | |
| 29 | struct timeval start_date; /* the process's start date in wall-clock time */ |
Willy Tarreau | c05d30e | 2023-04-28 14:50:29 +0200 | [diff] [blame] | 30 | ullong start_time_ns; /* the process's start date in internal monotonic time (ns) */ |
Willy Tarreau | 69530f5 | 2023-04-28 09:16:15 +0200 | [diff] [blame] | 31 | volatile ullong global_now_ns; /* common monotonic date between all threads, in ns (wraps every 585 yr) */ |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 32 | volatile uint global_now_ms; /* common monotonic date in milliseconds (may wrap) */ |
| 33 | |
Willy Tarreau | 69530f5 | 2023-04-28 09:16:15 +0200 | [diff] [blame] | 34 | THREAD_ALIGNED(64) static llong now_offset; /* global offset between system time and global time in ns */ |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 35 | |
Willy Tarreau | 69530f5 | 2023-04-28 09:16:15 +0200 | [diff] [blame] | 36 | THREAD_LOCAL ullong now_ns; /* internal monotonic date derived from real clock, in ns (wraps every 585 yr) */ |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 37 | THREAD_LOCAL uint now_ms; /* internal monotonic date in milliseconds (may wrap) */ |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 38 | THREAD_LOCAL struct timeval date; /* the real current date (wall-clock time) */ |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 39 | |
Willy Tarreau | 2c6a998 | 2021-10-08 11:38:30 +0200 | [diff] [blame] | 40 | static THREAD_LOCAL struct timeval before_poll; /* system date before calling poll() */ |
| 41 | static THREAD_LOCAL struct timeval after_poll; /* system date after leaving poll() */ |
Willy Tarreau | f9d5e10 | 2021-10-08 10:43:59 +0200 | [diff] [blame] | 42 | static THREAD_LOCAL unsigned int samp_time; /* total elapsed time over current sample */ |
| 43 | static THREAD_LOCAL unsigned int idle_time; /* total idle time over current sample */ |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 44 | static THREAD_LOCAL unsigned int iso_time_sec; /* last iso time value for this thread */ |
| 45 | static THREAD_LOCAL char iso_time_str[34]; /* ISO time representation of gettimeofday() */ |
| 46 | |
Willy Tarreau | 2169498 | 2021-10-08 15:09:17 +0200 | [diff] [blame] | 47 | #if defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0) && defined(_POSIX_THREAD_CPUTIME) |
| 48 | static clockid_t per_thread_clock_id[MAX_THREADS]; |
| 49 | #endif |
| 50 | |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 51 | /* returns the system's monotonic time in nanoseconds if supported, otherwise zero */ |
| 52 | uint64_t now_mono_time(void) |
| 53 | { |
| 54 | uint64_t ret = 0; |
Willy Tarreau | 6cb0c39 | 2021-10-08 14:48:30 +0200 | [diff] [blame] | 55 | #if defined(_POSIX_TIMERS) && defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0) && defined(_POSIX_MONOTONIC_CLOCK) |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 56 | struct timespec ts; |
| 57 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 58 | ret = ts.tv_sec * 1000000000ULL + ts.tv_nsec; |
| 59 | #endif |
| 60 | return ret; |
| 61 | } |
| 62 | |
Aurelien DARRAGON | 07cbd8e | 2022-11-25 08:56:46 +0100 | [diff] [blame] | 63 | /* Returns the system's monotonic time in nanoseconds. |
| 64 | * Uses the coarse clock source if supported (for fast but |
| 65 | * less precise queries with limited resource usage). |
| 66 | * Fallback to now_mono_time() if coarse source is not supported, |
| 67 | * which may itself return 0 if not supported either. |
| 68 | */ |
| 69 | uint64_t now_mono_time_fast(void) |
| 70 | { |
| 71 | #if defined(CLOCK_MONOTONIC_COARSE) |
| 72 | struct timespec ts; |
| 73 | |
| 74 | clock_gettime(CLOCK_MONOTONIC_COARSE, &ts); |
| 75 | return (ts.tv_sec * 1000000000ULL + ts.tv_nsec); |
| 76 | #else |
| 77 | /* fallback to regular mono time, |
| 78 | * returns 0 if not supported |
| 79 | */ |
| 80 | return now_mono_time(); |
| 81 | #endif |
| 82 | } |
| 83 | |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 84 | /* returns the current thread's cumulated CPU time in nanoseconds if supported, otherwise zero */ |
| 85 | uint64_t now_cpu_time(void) |
| 86 | { |
| 87 | uint64_t ret = 0; |
| 88 | #if defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0) && defined(_POSIX_THREAD_CPUTIME) |
| 89 | struct timespec ts; |
| 90 | clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts); |
| 91 | ret = ts.tv_sec * 1000000000ULL + ts.tv_nsec; |
| 92 | #endif |
| 93 | return ret; |
| 94 | } |
| 95 | |
Aurelien DARRAGON | df188f1 | 2023-04-04 17:21:40 +0200 | [diff] [blame] | 96 | /* Returns the current thread's cumulated CPU time in nanoseconds. |
| 97 | * |
| 98 | * thread_local timer is cached so that call is less precise but also less |
| 99 | * expensive if heavily used. |
| 100 | * We use the mono time as a cache expiration hint since now_cpu_time() is |
| 101 | * known to be much more expensive than now_mono_time_fast() on systems |
| 102 | * supporting the COARSE clock source. |
| 103 | * |
| 104 | * Returns 0 if either now_mono_time_fast() or now_cpu_time() are not |
| 105 | * supported. |
| 106 | */ |
| 107 | uint64_t now_cpu_time_fast(void) |
| 108 | { |
| 109 | static THREAD_LOCAL uint64_t mono_cache = 0; |
| 110 | static THREAD_LOCAL uint64_t cpu_cache = 0; |
| 111 | uint64_t mono_cur; |
| 112 | |
| 113 | mono_cur = now_mono_time_fast(); |
| 114 | if (unlikely(mono_cur != mono_cache)) { |
| 115 | /* global mono clock was updated: local cache is outdated */ |
| 116 | cpu_cache = now_cpu_time(); |
| 117 | mono_cache = mono_cur; |
| 118 | } |
| 119 | return cpu_cache; |
| 120 | } |
| 121 | |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 122 | /* returns another thread's cumulated CPU time in nanoseconds if supported, otherwise zero */ |
Willy Tarreau | 2169498 | 2021-10-08 15:09:17 +0200 | [diff] [blame] | 123 | uint64_t now_cpu_time_thread(int thr) |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 124 | { |
| 125 | uint64_t ret = 0; |
| 126 | #if defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0) && defined(_POSIX_THREAD_CPUTIME) |
| 127 | struct timespec ts; |
Willy Tarreau | 2169498 | 2021-10-08 15:09:17 +0200 | [diff] [blame] | 128 | clock_gettime(per_thread_clock_id[thr], &ts); |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 129 | ret = ts.tv_sec * 1000000000ULL + ts.tv_nsec; |
| 130 | #endif |
| 131 | return ret; |
| 132 | } |
| 133 | |
Willy Tarreau | 44c58da | 2021-10-08 12:27:54 +0200 | [diff] [blame] | 134 | /* set the clock source for the local thread */ |
| 135 | void clock_set_local_source(void) |
| 136 | { |
| 137 | #if defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0) && defined(_POSIX_THREAD_CPUTIME) |
| 138 | #ifdef USE_THREAD |
Willy Tarreau | 2169498 | 2021-10-08 15:09:17 +0200 | [diff] [blame] | 139 | pthread_getcpuclockid(pthread_self(), &per_thread_clock_id[tid]); |
Willy Tarreau | 44c58da | 2021-10-08 12:27:54 +0200 | [diff] [blame] | 140 | #else |
Willy Tarreau | 2169498 | 2021-10-08 15:09:17 +0200 | [diff] [blame] | 141 | per_thread_clock_id[tid] = CLOCK_THREAD_CPUTIME_ID; |
Willy Tarreau | 44c58da | 2021-10-08 12:27:54 +0200 | [diff] [blame] | 142 | #endif |
| 143 | #endif |
| 144 | } |
| 145 | |
Willy Tarreau | 6cb0c39 | 2021-10-08 14:48:30 +0200 | [diff] [blame] | 146 | /* registers a timer <tmr> of type timer_t delivering signal <sig> with value |
| 147 | * <val>. It tries on the current thread's clock ID first and falls back to |
| 148 | * CLOCK_REALTIME. Returns non-zero on success, 1 on failure. |
| 149 | */ |
| 150 | int clock_setup_signal_timer(void *tmr, int sig, int val) |
| 151 | { |
| 152 | int ret = 0; |
| 153 | |
| 154 | #if defined(USE_RT) && (_POSIX_TIMERS > 0) && defined(_POSIX_THREAD_CPUTIME) |
| 155 | struct sigevent sev = { }; |
| 156 | timer_t *timer = tmr; |
| 157 | sigset_t set; |
| 158 | |
| 159 | /* unblock the WDTSIG signal we intend to use */ |
| 160 | sigemptyset(&set); |
| 161 | sigaddset(&set, WDTSIG); |
| 162 | ha_sigmask(SIG_UNBLOCK, &set, NULL); |
| 163 | |
| 164 | /* this timer will signal WDTSIG when it fires, with tid in the si_int |
| 165 | * field (important since any thread will receive the signal). |
| 166 | */ |
| 167 | sev.sigev_notify = SIGEV_SIGNAL; |
| 168 | sev.sigev_signo = sig; |
| 169 | sev.sigev_value.sival_int = val; |
Willy Tarreau | 2169498 | 2021-10-08 15:09:17 +0200 | [diff] [blame] | 170 | if (timer_create(per_thread_clock_id[tid], &sev, timer) != -1 || |
Willy Tarreau | 6cb0c39 | 2021-10-08 14:48:30 +0200 | [diff] [blame] | 171 | timer_create(CLOCK_REALTIME, &sev, timer) != -1) |
| 172 | ret = 1; |
| 173 | #endif |
| 174 | return ret; |
| 175 | } |
| 176 | |
Willy Tarreau | 69530f5 | 2023-04-28 09:16:15 +0200 | [diff] [blame] | 177 | /* clock_update_date: sets <date> to system time, and sets <now_ns> to something |
| 178 | * as close as possible to real time, following a monotonic function. The main |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 179 | * principle consists in detecting backwards and forwards time jumps and adjust |
| 180 | * an offset to correct them. This function should be called once after each |
| 181 | * poll, and never farther apart than MAX_DELAY_MS*2. The poll's timeout should |
| 182 | * be passed in <max_wait>, and the return value in <interrupted> (a non-zero |
| 183 | * value means that we have not expired the timeout). |
| 184 | * |
| 185 | * clock_init_process_date() must have been called once first, and |
| 186 | * clock_init_thread_date() must also have been called once for each thread. |
| 187 | * |
| 188 | * An offset is used to adjust the current time (date), to figure a monotonic |
Willy Tarreau | 69530f5 | 2023-04-28 09:16:15 +0200 | [diff] [blame] | 189 | * local time (now_ns). The offset is not critical, as it is only updated after |
| 190 | * a clock jump is detected. From this point all threads will apply it to their |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 191 | * locally measured time, and will then agree around a common monotonic |
Willy Tarreau | 69530f5 | 2023-04-28 09:16:15 +0200 | [diff] [blame] | 192 | * global_now_ns value that serves to further refine their local time. Both |
| 193 | * now_ns and global_now_ns are 64-bit integers counting nanoseconds since a |
| 194 | * vague reference (it starts roughly 20s before the next wrap-around of the |
| 195 | * millisecond counter after boot). The offset is also an integral number of |
| 196 | * nanoseconds, but it's signed so that the clock can be adjusted in the two |
| 197 | * directions. |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 198 | */ |
Willy Tarreau | a700420 | 2022-09-21 07:37:27 +0200 | [diff] [blame] | 199 | void clock_update_local_date(int max_wait, int interrupted) |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 200 | { |
Willy Tarreau | a700420 | 2022-09-21 07:37:27 +0200 | [diff] [blame] | 201 | struct timeval min_deadline, max_deadline; |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 202 | |
| 203 | gettimeofday(&date, NULL); |
| 204 | |
| 205 | /* compute the minimum and maximum local date we may have reached based |
| 206 | * on our past date and the associated timeout. There are three possible |
| 207 | * extremities: |
| 208 | * - the new date cannot be older than before_poll |
| 209 | * - if not interrupted, the new date cannot be older than |
| 210 | * before_poll+max_wait |
| 211 | * - in any case the new date cannot be newer than |
| 212 | * before_poll+max_wait+some margin (100ms used here). |
| 213 | * In case of violation, we'll ignore the current date and instead |
| 214 | * restart from the last date we knew. |
| 215 | */ |
| 216 | _tv_ms_add(&min_deadline, &before_poll, max_wait); |
| 217 | _tv_ms_add(&max_deadline, &before_poll, max_wait + 100); |
| 218 | |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 219 | if (unlikely(__tv_islt(&date, &before_poll) || // big jump backwards |
| 220 | (!interrupted && __tv_islt(&date, &min_deadline)) || // small jump backwards |
| 221 | __tv_islt(&max_deadline, &date))) { // big jump forwards |
| 222 | if (!interrupted) |
Willy Tarreau | 69530f5 | 2023-04-28 09:16:15 +0200 | [diff] [blame] | 223 | now_ns += ms_to_ns(max_wait); |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 224 | } else { |
| 225 | /* The date is still within expectations. Let's apply the |
| 226 | * now_offset to the system date. Note: ofs if made of two |
| 227 | * independent signed ints. |
| 228 | */ |
Willy Tarreau | 69530f5 | 2023-04-28 09:16:15 +0200 | [diff] [blame] | 229 | now_ns = tv_to_ns(&date) + HA_ATOMIC_LOAD(&now_offset); |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 230 | } |
Willy Tarreau | 69530f5 | 2023-04-28 09:16:15 +0200 | [diff] [blame] | 231 | now_ms = ns_to_ms(now_ns); |
Willy Tarreau | a700420 | 2022-09-21 07:37:27 +0200 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | void clock_update_global_date() |
| 235 | { |
Willy Tarreau | 69530f5 | 2023-04-28 09:16:15 +0200 | [diff] [blame] | 236 | ullong old_now_ns; |
Willy Tarreau | a700420 | 2022-09-21 07:37:27 +0200 | [diff] [blame] | 237 | uint old_now_ms; |
Willy Tarreau | a700420 | 2022-09-21 07:37:27 +0200 | [diff] [blame] | 238 | |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 239 | /* now that we have bounded the local time, let's check if it's |
| 240 | * realistic regarding the global date, which only moves forward, |
| 241 | * otherwise catch up. |
| 242 | */ |
Willy Tarreau | 69530f5 | 2023-04-28 09:16:15 +0200 | [diff] [blame] | 243 | old_now_ns = _HA_ATOMIC_LOAD(&global_now_ns); |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 244 | old_now_ms = global_now_ms; |
| 245 | |
| 246 | do { |
Willy Tarreau | 69530f5 | 2023-04-28 09:16:15 +0200 | [diff] [blame] | 247 | if (now_ns < old_now_ns) |
| 248 | now_ns = old_now_ns; |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 249 | |
Willy Tarreau | 69530f5 | 2023-04-28 09:16:15 +0200 | [diff] [blame] | 250 | /* now <now_ns> is expected to be the most accurate date, |
| 251 | * equal to <global_now_ns> or newer. Updating the global |
Willy Tarreau | 4eaf85f | 2022-09-21 08:21:45 +0200 | [diff] [blame] | 252 | * date too often causes extreme contention and is not |
| 253 | * needed: it's only used to help threads run at the |
| 254 | * same date in case of local drift, and the global date, |
| 255 | * which changes, is only used by freq counters (a choice |
| 256 | * which is debatable by the way since it changes under us). |
| 257 | * Tests have seen that the contention can be reduced from |
| 258 | * 37% in this function to almost 0% when keeping clocks |
| 259 | * synchronized no better than 32 microseconds, so that's |
| 260 | * what we're doing here. |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 261 | */ |
Willy Tarreau | 69530f5 | 2023-04-28 09:16:15 +0200 | [diff] [blame] | 262 | now_ms = ns_to_ms(now_ns); |
Willy Tarreau | 4eaf85f | 2022-09-21 08:21:45 +0200 | [diff] [blame] | 263 | |
Willy Tarreau | 69530f5 | 2023-04-28 09:16:15 +0200 | [diff] [blame] | 264 | if (!((now_ns ^ old_now_ns) & ~0x7FFFULL)) |
Willy Tarreau | 4eaf85f | 2022-09-21 08:21:45 +0200 | [diff] [blame] | 265 | return; |
| 266 | |
Willy Tarreau | 69530f5 | 2023-04-28 09:16:15 +0200 | [diff] [blame] | 267 | /* let's try to update the global_now_ns (both in nanoseconds |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 268 | * and ms forms) or loop again. |
| 269 | */ |
Willy Tarreau | 69530f5 | 2023-04-28 09:16:15 +0200 | [diff] [blame] | 270 | } while ((!_HA_ATOMIC_CAS(&global_now_ns, &old_now_ns, now_ns) || |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 271 | (now_ms != old_now_ms && !_HA_ATOMIC_CAS(&global_now_ms, &old_now_ms, now_ms))) && |
| 272 | __ha_cpu_relax()); |
| 273 | |
Willy Tarreau | 69530f5 | 2023-04-28 09:16:15 +0200 | [diff] [blame] | 274 | /* <now_ns> and <now_ms> are now updated to the last value of |
| 275 | * global_now_ns and global_now_ms, which were also monotonically |
| 276 | * updated. We can compute the latest offset, we don't care who writes |
| 277 | * it last, the variations will not break the monotonic property. |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 278 | */ |
Willy Tarreau | 69530f5 | 2023-04-28 09:16:15 +0200 | [diff] [blame] | 279 | HA_ATOMIC_STORE(&now_offset, now_ns - tv_to_ns(&date)); |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | /* must be called once at boot to initialize some global variables */ |
| 283 | void clock_init_process_date(void) |
| 284 | { |
| 285 | now_offset = 0; |
| 286 | gettimeofday(&date, NULL); |
Willy Tarreau | 69530f5 | 2023-04-28 09:16:15 +0200 | [diff] [blame] | 287 | after_poll = before_poll = date; |
| 288 | now_ns = global_now_ns = tv_to_ns(&date); |
| 289 | global_now_ms = ns_to_ms(now_ns); |
Willy Tarreau | 28360dc | 2023-02-07 14:44:44 +0100 | [diff] [blame] | 290 | |
| 291 | /* force time to wrap 20s after boot: we first compute the time offset |
| 292 | * that once applied to the wall-clock date will make the local time |
| 293 | * wrap in 5 seconds. This offset is applied to the process-wide time, |
| 294 | * and will be used to recompute the local time, both of which will |
| 295 | * match and continue from this shifted date. |
| 296 | */ |
Willy Tarreau | 69530f5 | 2023-04-28 09:16:15 +0200 | [diff] [blame] | 297 | now_offset = sec_to_ns((uint)((uint)(-global_now_ms) / 1000U - BOOT_TIME_WRAP_SEC)); |
| 298 | global_now_ns += now_offset; |
| 299 | now_ns = global_now_ns; |
| 300 | now_ms = global_now_ms = ns_to_ms(now_ns); |
Willy Tarreau | 28360dc | 2023-02-07 14:44:44 +0100 | [diff] [blame] | 301 | |
Willy Tarreau | 45c38e2 | 2021-09-30 18:28:49 +0200 | [diff] [blame] | 302 | th_ctx->idle_pct = 100; |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 303 | clock_update_date(0, 1); |
| 304 | } |
| 305 | |
| 306 | /* must be called once per thread to initialize their thread-local variables. |
| 307 | * Note that other threads might also be initializing and running in parallel. |
| 308 | */ |
| 309 | void clock_init_thread_date(void) |
| 310 | { |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 311 | gettimeofday(&date, NULL); |
| 312 | after_poll = before_poll = date; |
| 313 | |
Willy Tarreau | 69530f5 | 2023-04-28 09:16:15 +0200 | [diff] [blame] | 314 | now_ns = _HA_ATOMIC_LOAD(&global_now_ns); |
Willy Tarreau | 45c38e2 | 2021-09-30 18:28:49 +0200 | [diff] [blame] | 315 | th_ctx->idle_pct = 100; |
Aurelien DARRAGON | 16d6c0c | 2022-11-10 11:47:47 +0100 | [diff] [blame] | 316 | th_ctx->prev_cpu_time = now_cpu_time(); |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 317 | clock_update_date(0, 1); |
| 318 | } |
| 319 | |
Willy Tarreau | f9d5e10 | 2021-10-08 10:43:59 +0200 | [diff] [blame] | 320 | /* report the average CPU idle percentage over all running threads, between 0 and 100 */ |
| 321 | uint clock_report_idle(void) |
| 322 | { |
| 323 | uint total = 0; |
| 324 | uint rthr = 0; |
| 325 | uint thr; |
| 326 | |
| 327 | for (thr = 0; thr < MAX_THREADS; thr++) { |
Willy Tarreau | 1e7f0d6 | 2022-06-27 16:22:22 +0200 | [diff] [blame] | 328 | if (!ha_thread_info[thr].tg || |
| 329 | !(ha_thread_info[thr].tg->threads_enabled & ha_thread_info[thr].ltid_bit)) |
Willy Tarreau | f9d5e10 | 2021-10-08 10:43:59 +0200 | [diff] [blame] | 330 | continue; |
Willy Tarreau | 45c38e2 | 2021-09-30 18:28:49 +0200 | [diff] [blame] | 331 | total += HA_ATOMIC_LOAD(&ha_thread_ctx[thr].idle_pct); |
Willy Tarreau | f9d5e10 | 2021-10-08 10:43:59 +0200 | [diff] [blame] | 332 | rthr++; |
| 333 | } |
| 334 | return rthr ? total / rthr : 0; |
| 335 | } |
| 336 | |
| 337 | /* Update the idle time value twice a second, to be called after |
| 338 | * clock_update_date() when called after poll(), and currently called only by |
| 339 | * clock_leaving_poll() below. It relies on <before_poll> to be updated to |
| 340 | * the system time before calling poll(). |
| 341 | */ |
| 342 | static inline void clock_measure_idle(void) |
| 343 | { |
| 344 | /* Let's compute the idle to work ratio. We worked between after_poll |
| 345 | * and before_poll, and slept between before_poll and date. The idle_pct |
| 346 | * is updated at most twice every second. Note that the current second |
| 347 | * rarely changes so we avoid a multiply when not needed. |
| 348 | */ |
| 349 | int delta; |
| 350 | |
| 351 | if ((delta = date.tv_sec - before_poll.tv_sec)) |
| 352 | delta *= 1000000; |
| 353 | idle_time += delta + (date.tv_usec - before_poll.tv_usec); |
| 354 | |
| 355 | if ((delta = date.tv_sec - after_poll.tv_sec)) |
| 356 | delta *= 1000000; |
| 357 | samp_time += delta + (date.tv_usec - after_poll.tv_usec); |
| 358 | |
| 359 | after_poll.tv_sec = date.tv_sec; after_poll.tv_usec = date.tv_usec; |
| 360 | if (samp_time < 500000) |
| 361 | return; |
| 362 | |
Willy Tarreau | 45c38e2 | 2021-09-30 18:28:49 +0200 | [diff] [blame] | 363 | HA_ATOMIC_STORE(&th_ctx->idle_pct, (100ULL * idle_time + samp_time / 2) / samp_time); |
Willy Tarreau | f9d5e10 | 2021-10-08 10:43:59 +0200 | [diff] [blame] | 364 | idle_time = samp_time = 0; |
| 365 | } |
| 366 | |
| 367 | /* Collect date and time information after leaving poll(). <timeout> must be |
| 368 | * set to the maximum sleep time passed to poll (in milliseconds), and |
| 369 | * <interrupted> must be zero if the poller reached the timeout or non-zero |
| 370 | * otherwise, which generally is provided by the poller's return value. |
| 371 | */ |
| 372 | void clock_leaving_poll(int timeout, int interrupted) |
| 373 | { |
| 374 | clock_measure_idle(); |
Willy Tarreau | 45c38e2 | 2021-09-30 18:28:49 +0200 | [diff] [blame] | 375 | th_ctx->prev_cpu_time = now_cpu_time(); |
| 376 | th_ctx->prev_mono_time = now_mono_time(); |
Willy Tarreau | f9d5e10 | 2021-10-08 10:43:59 +0200 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | /* Collect date and time information before calling poll(). This will be used |
| 380 | * to count the run time of the past loop and the sleep time of the next poll. |
Ilya Shipitsin | 4a689da | 2022-10-29 09:34:32 +0500 | [diff] [blame] | 381 | * It also compares the elapsed and cpu times during the activity period to |
Willy Tarreau | f9d5e10 | 2021-10-08 10:43:59 +0200 | [diff] [blame] | 382 | * estimate the amount of stolen time, which is reported if higher than half |
| 383 | * a millisecond. |
| 384 | */ |
| 385 | void clock_entering_poll(void) |
| 386 | { |
| 387 | uint64_t new_mono_time; |
| 388 | uint64_t new_cpu_time; |
Willy Tarreau | 20adfde | 2021-10-08 11:34:46 +0200 | [diff] [blame] | 389 | uint32_t run_time; |
Willy Tarreau | f9d5e10 | 2021-10-08 10:43:59 +0200 | [diff] [blame] | 390 | int64_t stolen; |
| 391 | |
| 392 | gettimeofday(&before_poll, NULL); |
| 393 | |
Willy Tarreau | 20adfde | 2021-10-08 11:34:46 +0200 | [diff] [blame] | 394 | run_time = (before_poll.tv_sec - after_poll.tv_sec) * 1000000U + (before_poll.tv_usec - after_poll.tv_usec); |
| 395 | |
Willy Tarreau | f9d5e10 | 2021-10-08 10:43:59 +0200 | [diff] [blame] | 396 | new_cpu_time = now_cpu_time(); |
| 397 | new_mono_time = now_mono_time(); |
| 398 | |
Willy Tarreau | 45c38e2 | 2021-09-30 18:28:49 +0200 | [diff] [blame] | 399 | if (th_ctx->prev_cpu_time && th_ctx->prev_mono_time) { |
| 400 | new_cpu_time -= th_ctx->prev_cpu_time; |
| 401 | new_mono_time -= th_ctx->prev_mono_time; |
Willy Tarreau | f9d5e10 | 2021-10-08 10:43:59 +0200 | [diff] [blame] | 402 | stolen = new_mono_time - new_cpu_time; |
| 403 | if (unlikely(stolen >= 500000)) { |
| 404 | stolen /= 500000; |
| 405 | /* more than half a millisecond difference might |
| 406 | * indicate an undesired preemption. |
| 407 | */ |
| 408 | report_stolen_time(stolen); |
| 409 | } |
| 410 | } |
Willy Tarreau | 20adfde | 2021-10-08 11:34:46 +0200 | [diff] [blame] | 411 | |
| 412 | /* update the average runtime */ |
| 413 | activity_count_runtime(run_time); |
Willy Tarreau | f9d5e10 | 2021-10-08 10:43:59 +0200 | [diff] [blame] | 414 | } |
| 415 | |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 416 | /* returns the current date as returned by gettimeofday() in ISO+microsecond |
| 417 | * format. It uses a thread-local static variable that the reader can consume |
| 418 | * for as long as it wants until next call. Thus, do not call it from a signal |
| 419 | * handler. If <pad> is non-0, a trailing space will be added. It will always |
| 420 | * return exactly 32 or 33 characters (depending on padding) and will always be |
| 421 | * zero-terminated, thus it will always fit into a 34 bytes buffer. |
| 422 | * This also always include the local timezone (in +/-HH:mm format) . |
| 423 | */ |
| 424 | char *timeofday_as_iso_us(int pad) |
| 425 | { |
| 426 | struct timeval new_date; |
| 427 | struct tm tm; |
| 428 | const char *offset; |
| 429 | char c; |
| 430 | |
| 431 | gettimeofday(&new_date, NULL); |
| 432 | if (new_date.tv_sec != iso_time_sec || !new_date.tv_sec) { |
| 433 | get_localtime(new_date.tv_sec, &tm); |
| 434 | offset = get_gmt_offset(new_date.tv_sec, &tm); |
| 435 | if (unlikely(strftime(iso_time_str, sizeof(iso_time_str), "%Y-%m-%dT%H:%M:%S.000000+00:00", &tm) != 32)) |
Willy Tarreau | fc458ec | 2023-04-07 18:11:39 +0200 | [diff] [blame] | 436 | strlcpy2(iso_time_str, "YYYY-mm-ddTHH:MM:SS.000000-00:00", sizeof(iso_time_str)); // make the failure visible but respect format. |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 437 | iso_time_str[26] = offset[0]; |
| 438 | iso_time_str[27] = offset[1]; |
| 439 | iso_time_str[28] = offset[2]; |
| 440 | iso_time_str[30] = offset[3]; |
| 441 | iso_time_str[31] = offset[4]; |
| 442 | iso_time_sec = new_date.tv_sec; |
| 443 | } |
| 444 | |
| 445 | /* utoa_pad adds a trailing 0 so we save the char for restore */ |
| 446 | c = iso_time_str[26]; |
| 447 | utoa_pad(new_date.tv_usec, iso_time_str + 20, 7); |
| 448 | iso_time_str[26] = c; |
| 449 | if (pad) { |
| 450 | iso_time_str[32] = ' '; |
| 451 | iso_time_str[33] = 0; |
| 452 | } |
| 453 | return iso_time_str; |
| 454 | } |