Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Time calculation functions. |
| 3 | * |
Willy Tarreau | 45a1251 | 2011-09-10 16:56:42 +0200 | [diff] [blame] | 4 | * Copyright 2000-2011 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 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 | |
Willy Tarreau | ed72d82 | 2018-10-17 19:01:24 +0200 | [diff] [blame] | 13 | #include <unistd.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 14 | #include <sys/time.h> |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 15 | |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 16 | #include <haproxy/api.h> |
Willy Tarreau | 92b4f13 | 2020-06-01 11:05:15 +0200 | [diff] [blame] | 17 | #include <haproxy/time.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 18 | #include <haproxy/tools.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 19 | |
Christopher Faulet | 9a65571 | 2017-05-11 11:00:15 +0200 | [diff] [blame] | 20 | THREAD_LOCAL unsigned int ms_left_scaled; /* milliseconds left for current second (0..2^32-1) */ |
| 21 | THREAD_LOCAL unsigned int now_ms; /* internal date in milliseconds (may wrap) */ |
| 22 | THREAD_LOCAL unsigned int samp_time; /* total elapsed time over current sample */ |
| 23 | THREAD_LOCAL unsigned int idle_time; /* total idle time over current sample */ |
Christopher Faulet | 9a65571 | 2017-05-11 11:00:15 +0200 | [diff] [blame] | 24 | THREAD_LOCAL struct timeval now; /* internal date is a monotonic function of real clock */ |
| 25 | THREAD_LOCAL struct timeval date; /* the real current date */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 26 | struct timeval start_date; /* the process's start date */ |
Christopher Faulet | 9a65571 | 2017-05-11 11:00:15 +0200 | [diff] [blame] | 27 | THREAD_LOCAL struct timeval before_poll; /* system date before calling poll() */ |
| 28 | THREAD_LOCAL struct timeval after_poll; /* system date after leaving poll() */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 29 | |
Willy Tarreau | 9fefc51 | 2017-11-23 14:52:28 +0100 | [diff] [blame] | 30 | static THREAD_LOCAL struct timeval tv_offset; /* per-thread time ofsset relative to global time */ |
Willy Tarreau | 650f374 | 2021-03-17 18:52:18 +0100 | [diff] [blame^] | 31 | volatile unsigned long long global_now; /* common date between all threads (32:32) */ |
Willy Tarreau | 9fefc51 | 2017-11-23 14:52:28 +0100 | [diff] [blame] | 32 | |
Willy Tarreau | 93acfa2 | 2019-09-26 08:00:23 +0200 | [diff] [blame] | 33 | static THREAD_LOCAL unsigned int iso_time_sec; /* last iso time value for this thread */ |
Emeric Brun | b39a375 | 2020-07-02 17:22:17 +0200 | [diff] [blame] | 34 | static THREAD_LOCAL char iso_time_str[34]; /* ISO time representation of gettimeofday() */ |
Willy Tarreau | 93acfa2 | 2019-09-26 08:00:23 +0200 | [diff] [blame] | 35 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 36 | /* |
| 37 | * adds <ms> ms to <from>, set the result to <tv> and returns a pointer <tv> |
| 38 | */ |
Willy Tarreau | 03e7853 | 2020-02-25 07:38:05 +0100 | [diff] [blame] | 39 | struct timeval *_tv_ms_add(struct timeval *tv, const struct timeval *from, int ms) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 40 | { |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 41 | tv->tv_usec = from->tv_usec + (ms % 1000) * 1000; |
| 42 | tv->tv_sec = from->tv_sec + (ms / 1000); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 43 | while (tv->tv_usec >= 1000000) { |
| 44 | tv->tv_usec -= 1000000; |
| 45 | tv->tv_sec++; |
| 46 | } |
| 47 | return tv; |
| 48 | } |
| 49 | |
| 50 | /* |
| 51 | * compares <tv1> and <tv2> modulo 1ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2 |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 52 | * Must not be used when either argument is eternity. Use tv_ms_cmp2() for that. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 53 | */ |
Willy Tarreau | 03e7853 | 2020-02-25 07:38:05 +0100 | [diff] [blame] | 54 | int _tv_ms_cmp(const struct timeval *tv1, const struct timeval *tv2) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 55 | { |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 56 | return __tv_ms_cmp(tv1, tv2); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 60 | * compares <tv1> and <tv2> modulo 1 ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2, |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 61 | * assuming that TV_ETERNITY is greater than everything. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 62 | */ |
Willy Tarreau | 03e7853 | 2020-02-25 07:38:05 +0100 | [diff] [blame] | 63 | int _tv_ms_cmp2(const struct timeval *tv1, const struct timeval *tv2) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 64 | { |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 65 | return __tv_ms_cmp2(tv1, tv2); |
Willy Tarreau | 8d7d149 | 2007-04-29 10:50:43 +0200 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /* |
| 69 | * compares <tv1> and <tv2> modulo 1 ms: returns 1 if tv1 <= tv2, 0 if tv1 > tv2, |
| 70 | * assuming that TV_ETERNITY is greater than everything. Returns 0 if tv1 is |
| 71 | * TV_ETERNITY, and always assumes that tv2 != TV_ETERNITY. Designed to replace |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 72 | * occurrences of (tv_ms_cmp2(tv,now) <= 0). |
Willy Tarreau | 8d7d149 | 2007-04-29 10:50:43 +0200 | [diff] [blame] | 73 | */ |
Willy Tarreau | 03e7853 | 2020-02-25 07:38:05 +0100 | [diff] [blame] | 74 | int _tv_ms_le2(const struct timeval *tv1, const struct timeval *tv2) |
Willy Tarreau | 8d7d149 | 2007-04-29 10:50:43 +0200 | [diff] [blame] | 75 | { |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 76 | return __tv_ms_le2(tv1, tv2); |
| 77 | } |
Willy Tarreau | 8d7d149 | 2007-04-29 10:50:43 +0200 | [diff] [blame] | 78 | |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 79 | /* |
| 80 | * returns the remaining time between tv1=now and event=tv2 |
| 81 | * if tv2 is passed, 0 is returned. |
| 82 | * Must not be used when either argument is eternity. |
| 83 | */ |
Willy Tarreau | 03e7853 | 2020-02-25 07:38:05 +0100 | [diff] [blame] | 84 | unsigned long _tv_ms_remain(const struct timeval *tv1, const struct timeval *tv2) |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 85 | { |
| 86 | return __tv_ms_remain(tv1, tv2); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /* |
| 90 | * returns the remaining time between tv1=now and event=tv2 |
| 91 | * if tv2 is passed, 0 is returned. |
| 92 | * Returns TIME_ETERNITY if tv2 is eternity. |
| 93 | */ |
Willy Tarreau | 03e7853 | 2020-02-25 07:38:05 +0100 | [diff] [blame] | 94 | unsigned long _tv_ms_remain2(const struct timeval *tv1, const struct timeval *tv2) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 95 | { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 96 | if (tv_iseternity(tv2)) |
| 97 | return TIME_ETERNITY; |
| 98 | |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 99 | return __tv_ms_remain(tv1, tv2); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 100 | } |
| 101 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 102 | /* |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 103 | * Returns the time in ms elapsed between tv1 and tv2, assuming that tv1<=tv2. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 104 | * Must not be used when either argument is eternity. |
| 105 | */ |
Willy Tarreau | 03e7853 | 2020-02-25 07:38:05 +0100 | [diff] [blame] | 106 | unsigned long _tv_ms_elapsed(const struct timeval *tv1, const struct timeval *tv2) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 107 | { |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 108 | return __tv_ms_elapsed(tv1, tv2); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | /* |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 112 | * adds <inc> to <from>, set the result to <tv> and returns a pointer <tv> |
| 113 | */ |
Willy Tarreau | 03e7853 | 2020-02-25 07:38:05 +0100 | [diff] [blame] | 114 | struct timeval *_tv_add(struct timeval *tv, const struct timeval *from, const struct timeval *inc) |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 115 | { |
| 116 | return __tv_add(tv, from, inc); |
| 117 | } |
| 118 | |
| 119 | /* |
Willy Tarreau | 0481c20 | 2007-05-13 16:03:27 +0200 | [diff] [blame] | 120 | * If <inc> is set, then add it to <from> and set the result to <tv>, then |
| 121 | * return 1, otherwise return 0. It is meant to be used in if conditions. |
| 122 | */ |
Willy Tarreau | 03e7853 | 2020-02-25 07:38:05 +0100 | [diff] [blame] | 123 | int _tv_add_ifset(struct timeval *tv, const struct timeval *from, const struct timeval *inc) |
Willy Tarreau | 0481c20 | 2007-05-13 16:03:27 +0200 | [diff] [blame] | 124 | { |
| 125 | return __tv_add_ifset(tv, from, inc); |
| 126 | } |
| 127 | |
| 128 | /* |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 129 | * Computes the remaining time between tv1=now and event=tv2. if tv2 is passed, |
| 130 | * 0 is returned. The result is stored into tv. |
| 131 | */ |
Willy Tarreau | 03e7853 | 2020-02-25 07:38:05 +0100 | [diff] [blame] | 132 | struct timeval *_tv_remain(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv) |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 133 | { |
| 134 | return __tv_remain(tv1, tv2, tv); |
| 135 | } |
| 136 | |
| 137 | /* |
| 138 | * Computes the remaining time between tv1=now and event=tv2. if tv2 is passed, |
| 139 | * 0 is returned. The result is stored into tv. Returns ETERNITY if tv2 is |
| 140 | * eternity. |
| 141 | */ |
Willy Tarreau | 03e7853 | 2020-02-25 07:38:05 +0100 | [diff] [blame] | 142 | struct timeval *_tv_remain2(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv) |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 143 | { |
| 144 | return __tv_remain2(tv1, tv2, tv); |
| 145 | } |
| 146 | |
Willy Tarreau | 0481c20 | 2007-05-13 16:03:27 +0200 | [diff] [blame] | 147 | /* tv_isle: compares <tv1> and <tv2> : returns 1 if tv1 <= tv2, otherwise 0 */ |
Willy Tarreau | 03e7853 | 2020-02-25 07:38:05 +0100 | [diff] [blame] | 148 | int _tv_isle(const struct timeval *tv1, const struct timeval *tv2) |
Willy Tarreau | 0481c20 | 2007-05-13 16:03:27 +0200 | [diff] [blame] | 149 | { |
| 150 | return __tv_isle(tv1, tv2); |
| 151 | } |
| 152 | |
| 153 | /* tv_isgt: compares <tv1> and <tv2> : returns 1 if tv1 > tv2, otherwise 0 */ |
Willy Tarreau | 03e7853 | 2020-02-25 07:38:05 +0100 | [diff] [blame] | 154 | int _tv_isgt(const struct timeval *tv1, const struct timeval *tv2) |
Willy Tarreau | 0481c20 | 2007-05-13 16:03:27 +0200 | [diff] [blame] | 155 | { |
| 156 | return __tv_isgt(tv1, tv2); |
| 157 | } |
| 158 | |
Willy Tarreau | 7649aac | 2017-11-23 11:52:55 +0100 | [diff] [blame] | 159 | /* tv_update_date: sets <date> to system time, and sets <now> to something as |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 160 | * close as possible to real time, following a monotonic function. The main |
| 161 | * principle consists in detecting backwards and forwards time jumps and adjust |
| 162 | * an offset to correct them. This function should be called once after each |
| 163 | * poll, and never farther apart than MAX_DELAY_MS*2. The poll's timeout should |
| 164 | * be passed in <max_wait>, and the return value in <interrupted> (a non-zero |
| 165 | * value means that we have not expired the timeout). Calling it with (-1,*) |
| 166 | * sets both <date> and <now> to current date, and calling it with (0,1) simply |
| 167 | * updates the values. |
Christopher Faulet | 99aad92 | 2017-10-31 09:03:51 +0100 | [diff] [blame] | 168 | * |
Willy Tarreau | 7649aac | 2017-11-23 11:52:55 +0100 | [diff] [blame] | 169 | * An offset is used to adjust the current time (date), to have a monotonic time |
Christopher Faulet | 99aad92 | 2017-10-31 09:03:51 +0100 | [diff] [blame] | 170 | * (now). It must be global and thread-safe. But a timeval cannot be atomically |
Willy Tarreau | 7649aac | 2017-11-23 11:52:55 +0100 | [diff] [blame] | 171 | * updated. So instead, we store it in a 64-bits integer (offset) whose 32 MSB |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 172 | * contain the signed seconds adjustment and the 32 LSB contain the unsigned |
Willy Tarreau | 7649aac | 2017-11-23 11:52:55 +0100 | [diff] [blame] | 173 | * microsecond adjustment. We cannot use a timeval for this since it's never |
| 174 | * clearly specified whether a timeval may hold negative values or not. |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 175 | */ |
Willy Tarreau | 03e7853 | 2020-02-25 07:38:05 +0100 | [diff] [blame] | 176 | void tv_update_date(int max_wait, int interrupted) |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 177 | { |
Willy Tarreau | a331544 | 2018-02-05 20:11:38 +0100 | [diff] [blame] | 178 | struct timeval adjusted, deadline, tmp_now, tmp_adj; |
Willy Tarreau | 351b3a1 | 2017-03-29 15:24:33 +0200 | [diff] [blame] | 179 | unsigned int curr_sec_ms; /* millisecond of current second (0..999) */ |
Willy Tarreau | 9fefc51 | 2017-11-23 14:52:28 +0100 | [diff] [blame] | 180 | unsigned long long old_now; |
| 181 | unsigned long long new_now; |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 182 | |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 183 | gettimeofday(&date, NULL); |
| 184 | if (unlikely(max_wait < 0)) { |
Willy Tarreau | 9fefc51 | 2017-11-23 14:52:28 +0100 | [diff] [blame] | 185 | tv_zero(&tv_offset); |
Willy Tarreau | 7559058 | 2009-03-05 14:54:50 +0100 | [diff] [blame] | 186 | adjusted = date; |
Willy Tarreau | 45a1251 | 2011-09-10 16:56:42 +0200 | [diff] [blame] | 187 | after_poll = date; |
| 188 | samp_time = idle_time = 0; |
Willy Tarreau | 81036f2 | 2019-05-20 19:24:50 +0200 | [diff] [blame] | 189 | ti->idle_pct = 100; |
Willy Tarreau | 43091ed | 2019-06-06 16:50:39 +0200 | [diff] [blame] | 190 | old_now = global_now; |
| 191 | if (!old_now) { // never set |
| 192 | new_now = (((unsigned long long)adjusted.tv_sec) << 32) + (unsigned int)adjusted.tv_usec; |
| 193 | _HA_ATOMIC_CAS(&global_now, &old_now, new_now); |
| 194 | } |
Willy Tarreau | e6313a3 | 2008-06-29 13:47:25 +0200 | [diff] [blame] | 195 | goto to_ms; |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 196 | } |
Willy Tarreau | 7649aac | 2017-11-23 11:52:55 +0100 | [diff] [blame] | 197 | |
Willy Tarreau | 9fefc51 | 2017-11-23 14:52:28 +0100 | [diff] [blame] | 198 | __tv_add(&adjusted, &date, &tv_offset); |
Willy Tarreau | 7649aac | 2017-11-23 11:52:55 +0100 | [diff] [blame] | 199 | |
Willy Tarreau | 9fefc51 | 2017-11-23 14:52:28 +0100 | [diff] [blame] | 200 | /* compute the minimum and maximum local date we may have reached based |
| 201 | * on our past date and the associated timeout. |
| 202 | */ |
| 203 | _tv_ms_add(&deadline, &now, max_wait + MAX_DELAY_MS); |
Willy Tarreau | 7649aac | 2017-11-23 11:52:55 +0100 | [diff] [blame] | 204 | |
Willy Tarreau | 9fefc51 | 2017-11-23 14:52:28 +0100 | [diff] [blame] | 205 | if (unlikely(__tv_islt(&adjusted, &now) || __tv_islt(&deadline, &adjusted))) { |
| 206 | /* Large jump. If the poll was interrupted, we consider that the |
| 207 | * date has not changed (immediate wake-up), otherwise we add |
| 208 | * the poll time-out to the previous date. The new offset is |
| 209 | * recomputed. |
| 210 | */ |
| 211 | _tv_ms_add(&adjusted, &now, interrupted ? 0 : max_wait); |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 212 | } |
| 213 | |
Willy Tarreau | 9fefc51 | 2017-11-23 14:52:28 +0100 | [diff] [blame] | 214 | /* now that we have bounded the local time, let's check if it's |
| 215 | * realistic regarding the global date, which only moves forward, |
| 216 | * otherwise catch up. |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 217 | */ |
Willy Tarreau | 9fefc51 | 2017-11-23 14:52:28 +0100 | [diff] [blame] | 218 | old_now = global_now; |
Willy Tarreau | 7559058 | 2009-03-05 14:54:50 +0100 | [diff] [blame] | 219 | |
Willy Tarreau | 9fefc51 | 2017-11-23 14:52:28 +0100 | [diff] [blame] | 220 | do { |
| 221 | tmp_now.tv_sec = (unsigned int)(old_now >> 32); |
| 222 | tmp_now.tv_usec = old_now & 0xFFFFFFFFU; |
Willy Tarreau | a331544 | 2018-02-05 20:11:38 +0100 | [diff] [blame] | 223 | tmp_adj = adjusted; |
Willy Tarreau | 7649aac | 2017-11-23 11:52:55 +0100 | [diff] [blame] | 224 | |
Willy Tarreau | a331544 | 2018-02-05 20:11:38 +0100 | [diff] [blame] | 225 | if (__tv_islt(&tmp_adj, &tmp_now)) |
| 226 | tmp_adj = tmp_now; |
Willy Tarreau | 9fefc51 | 2017-11-23 14:52:28 +0100 | [diff] [blame] | 227 | |
| 228 | /* now <adjusted> is expected to be the most accurate date, |
| 229 | * equal to <global_now> or newer. |
| 230 | */ |
Willy Tarreau | a331544 | 2018-02-05 20:11:38 +0100 | [diff] [blame] | 231 | new_now = (((unsigned long long)tmp_adj.tv_sec) << 32) + (unsigned int)tmp_adj.tv_usec; |
Willy Tarreau | 9fefc51 | 2017-11-23 14:52:28 +0100 | [diff] [blame] | 232 | |
| 233 | /* let's try to update the global <now> or loop again */ |
Olivier Houchard | cab0f0b | 2019-03-08 18:55:31 +0100 | [diff] [blame] | 234 | } while (!_HA_ATOMIC_CAS(&global_now, &old_now, new_now)); |
Willy Tarreau | 9fefc51 | 2017-11-23 14:52:28 +0100 | [diff] [blame] | 235 | |
Willy Tarreau | a331544 | 2018-02-05 20:11:38 +0100 | [diff] [blame] | 236 | adjusted = tmp_adj; |
| 237 | |
Willy Tarreau | 9fefc51 | 2017-11-23 14:52:28 +0100 | [diff] [blame] | 238 | /* the new global date when we looked was old_now, and the new one is |
| 239 | * new_now == adjusted. We can recompute our local offset. |
| 240 | */ |
| 241 | tv_offset.tv_sec = adjusted.tv_sec - date.tv_sec; |
| 242 | tv_offset.tv_usec = adjusted.tv_usec - date.tv_usec; |
| 243 | if (tv_offset.tv_usec < 0) { |
| 244 | tv_offset.tv_usec += 1000000; |
| 245 | tv_offset.tv_sec--; |
| 246 | } |
Willy Tarreau | 7649aac | 2017-11-23 11:52:55 +0100 | [diff] [blame] | 247 | |
Willy Tarreau | e6313a3 | 2008-06-29 13:47:25 +0200 | [diff] [blame] | 248 | to_ms: |
Willy Tarreau | 7559058 | 2009-03-05 14:54:50 +0100 | [diff] [blame] | 249 | now = adjusted; |
| 250 | curr_sec_ms = now.tv_usec / 1000; /* ms of current second */ |
Willy Tarreau | eab777c | 2012-12-29 21:50:07 +0100 | [diff] [blame] | 251 | |
| 252 | /* For frequency counters, we'll need to know the ratio of the previous |
| 253 | * value to add to current value depending on the current millisecond. |
| 254 | * The principle is that during the first millisecond, we use 999/1000 |
| 255 | * of the past value and that during the last millisecond we use 0/1000 |
| 256 | * of the past value. In summary, we only use the past value during the |
| 257 | * first 999 ms of a second, and the last ms is used to complete the |
| 258 | * current measure. The value is scaled to (2^32-1) so that a simple |
| 259 | * multiply followed by a shift gives us the final value. |
| 260 | */ |
| 261 | ms_left_scaled = (999U - curr_sec_ms) * 4294967U; |
Willy Tarreau | 7559058 | 2009-03-05 14:54:50 +0100 | [diff] [blame] | 262 | now_ms = now.tv_sec * 1000 + curr_sec_ms; |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 263 | return; |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 264 | } |
| 265 | |
Willy Tarreau | 93acfa2 | 2019-09-26 08:00:23 +0200 | [diff] [blame] | 266 | /* returns the current date as returned by gettimeofday() in ISO+microsecond |
| 267 | * format. It uses a thread-local static variable that the reader can consume |
| 268 | * for as long as it wants until next call. Thus, do not call it from a signal |
| 269 | * handler. If <pad> is non-0, a trailing space will be added. It will always |
Emeric Brun | b39a375 | 2020-07-02 17:22:17 +0200 | [diff] [blame] | 270 | * return exactly 32 or 33 characters (depending on padding) and will always be |
| 271 | * zero-terminated, thus it will always fit into a 34 bytes buffer. |
| 272 | * This also always include the local timezone (in +/-HH:mm format) . |
Willy Tarreau | 93acfa2 | 2019-09-26 08:00:23 +0200 | [diff] [blame] | 273 | */ |
| 274 | char *timeofday_as_iso_us(int pad) |
| 275 | { |
| 276 | struct timeval new_date; |
| 277 | struct tm tm; |
Emeric Brun | b39a375 | 2020-07-02 17:22:17 +0200 | [diff] [blame] | 278 | const char *offset; |
| 279 | char c; |
Willy Tarreau | 93acfa2 | 2019-09-26 08:00:23 +0200 | [diff] [blame] | 280 | gettimeofday(&new_date, NULL); |
| 281 | if (new_date.tv_sec != iso_time_sec || !new_date.tv_sec) { |
| 282 | get_localtime(new_date.tv_sec, &tm); |
Emeric Brun | b39a375 | 2020-07-02 17:22:17 +0200 | [diff] [blame] | 283 | offset = get_gmt_offset(new_date.tv_sec, &tm); |
| 284 | if (unlikely(strftime(iso_time_str, sizeof(iso_time_str), "%Y-%m-%dT%H:%M:%S.000000+00:00", &tm) != 32)) |
| 285 | strcpy(iso_time_str, "YYYY-mm-ddTHH:MM:SS.000000-00:00"); // make the failure visible but respect format. |
| 286 | iso_time_str[26] = offset[0]; |
| 287 | iso_time_str[27] = offset[1]; |
| 288 | iso_time_str[28] = offset[2]; |
| 289 | iso_time_str[30] = offset[3]; |
| 290 | iso_time_str[31] = offset[4]; |
Willy Tarreau | 93acfa2 | 2019-09-26 08:00:23 +0200 | [diff] [blame] | 291 | iso_time_sec = new_date.tv_sec; |
| 292 | } |
Emeric Brun | b39a375 | 2020-07-02 17:22:17 +0200 | [diff] [blame] | 293 | /* utoa_pad adds a trailing 0 so we save the char for restore */ |
| 294 | c = iso_time_str[26]; |
Willy Tarreau | 93acfa2 | 2019-09-26 08:00:23 +0200 | [diff] [blame] | 295 | utoa_pad(new_date.tv_usec, iso_time_str + 20, 7); |
Emeric Brun | b39a375 | 2020-07-02 17:22:17 +0200 | [diff] [blame] | 296 | iso_time_str[26] = c; |
Willy Tarreau | 93acfa2 | 2019-09-26 08:00:23 +0200 | [diff] [blame] | 297 | if (pad) { |
Emeric Brun | b39a375 | 2020-07-02 17:22:17 +0200 | [diff] [blame] | 298 | iso_time_str[32] = ' '; |
| 299 | iso_time_str[33] = 0; |
Willy Tarreau | 93acfa2 | 2019-09-26 08:00:23 +0200 | [diff] [blame] | 300 | } |
| 301 | return iso_time_str; |
| 302 | } |
| 303 | |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 304 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 305 | * Local variables: |
| 306 | * c-indent-level: 8 |
| 307 | * c-basic-offset: 8 |
| 308 | * End: |
| 309 | */ |