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