Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Time calculation functions. |
| 3 | * |
Willy Tarreau | 7559058 | 2009-03-05 14:54:50 +0100 | [diff] [blame] | 4 | * Copyright 2000-2009 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 | |
| 13 | #include <sys/time.h> |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 14 | |
| 15 | #include <common/config.h> |
Willy Tarreau | 5e8f066 | 2007-02-12 00:59:08 +0100 | [diff] [blame] | 16 | #include <common/standard.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 17 | #include <common/time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 18 | |
Willy Tarreau | 7559058 | 2009-03-05 14:54:50 +0100 | [diff] [blame] | 19 | unsigned int curr_sec_ms; /* millisecond of current second (0..999) */ |
| 20 | unsigned int curr_sec_ms_scaled; /* millisecond of current second (0..2^32-1) */ |
Willy Tarreau | e6313a3 | 2008-06-29 13:47:25 +0200 | [diff] [blame] | 21 | unsigned int now_ms; /* internal date in milliseconds (may wrap) */ |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 22 | struct timeval now; /* internal date is a monotonic function of real clock */ |
| 23 | struct timeval date; /* the real current date */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 24 | struct timeval start_date; /* the process's start date */ |
| 25 | |
| 26 | /* |
| 27 | * adds <ms> ms to <from>, set the result to <tv> and returns a pointer <tv> |
| 28 | */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 29 | REGPRM3 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] | 30 | { |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 31 | tv->tv_usec = from->tv_usec + (ms % 1000) * 1000; |
| 32 | tv->tv_sec = from->tv_sec + (ms / 1000); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 33 | while (tv->tv_usec >= 1000000) { |
| 34 | tv->tv_usec -= 1000000; |
| 35 | tv->tv_sec++; |
| 36 | } |
| 37 | return tv; |
| 38 | } |
| 39 | |
| 40 | /* |
| 41 | * 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] | 42 | * 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] | 43 | */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 44 | REGPRM2 int _tv_ms_cmp(const struct timeval *tv1, const struct timeval *tv2) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 45 | { |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 46 | return __tv_ms_cmp(tv1, tv2); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 50 | * 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] | 51 | * assuming that TV_ETERNITY is greater than everything. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 52 | */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 53 | REGPRM2 int _tv_ms_cmp2(const struct timeval *tv1, const struct timeval *tv2) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 54 | { |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 55 | return __tv_ms_cmp2(tv1, tv2); |
Willy Tarreau | 8d7d149 | 2007-04-29 10:50:43 +0200 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /* |
| 59 | * compares <tv1> and <tv2> modulo 1 ms: returns 1 if tv1 <= tv2, 0 if tv1 > tv2, |
| 60 | * assuming that TV_ETERNITY is greater than everything. Returns 0 if tv1 is |
| 61 | * TV_ETERNITY, and always assumes that tv2 != TV_ETERNITY. Designed to replace |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 62 | * occurrences of (tv_ms_cmp2(tv,now) <= 0). |
Willy Tarreau | 8d7d149 | 2007-04-29 10:50:43 +0200 | [diff] [blame] | 63 | */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 64 | REGPRM2 int _tv_ms_le2(const struct timeval *tv1, const struct timeval *tv2) |
Willy Tarreau | 8d7d149 | 2007-04-29 10:50:43 +0200 | [diff] [blame] | 65 | { |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 66 | return __tv_ms_le2(tv1, tv2); |
| 67 | } |
Willy Tarreau | 8d7d149 | 2007-04-29 10:50:43 +0200 | [diff] [blame] | 68 | |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 69 | /* |
| 70 | * returns the remaining time between tv1=now and event=tv2 |
| 71 | * if tv2 is passed, 0 is returned. |
| 72 | * Must not be used when either argument is eternity. |
| 73 | */ |
| 74 | REGPRM2 unsigned long _tv_ms_remain(const struct timeval *tv1, const struct timeval *tv2) |
| 75 | { |
| 76 | return __tv_ms_remain(tv1, tv2); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | /* |
| 80 | * returns the remaining time between tv1=now and event=tv2 |
| 81 | * if tv2 is passed, 0 is returned. |
| 82 | * Returns TIME_ETERNITY if tv2 is eternity. |
| 83 | */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 84 | REGPRM2 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] | 85 | { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 86 | if (tv_iseternity(tv2)) |
| 87 | return TIME_ETERNITY; |
| 88 | |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 89 | return __tv_ms_remain(tv1, tv2); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 90 | } |
| 91 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 92 | /* |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 93 | * 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] | 94 | * Must not be used when either argument is eternity. |
| 95 | */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 96 | REGPRM2 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] | 97 | { |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 98 | return __tv_ms_elapsed(tv1, tv2); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | /* |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 102 | * adds <inc> to <from>, set the result to <tv> and returns a pointer <tv> |
| 103 | */ |
| 104 | REGPRM3 struct timeval *_tv_add(struct timeval *tv, const struct timeval *from, const struct timeval *inc) |
| 105 | { |
| 106 | return __tv_add(tv, from, inc); |
| 107 | } |
| 108 | |
| 109 | /* |
Willy Tarreau | 0481c20 | 2007-05-13 16:03:27 +0200 | [diff] [blame] | 110 | * If <inc> is set, then add it to <from> and set the result to <tv>, then |
| 111 | * return 1, otherwise return 0. It is meant to be used in if conditions. |
| 112 | */ |
| 113 | REGPRM3 int _tv_add_ifset(struct timeval *tv, const struct timeval *from, const struct timeval *inc) |
| 114 | { |
| 115 | return __tv_add_ifset(tv, from, inc); |
| 116 | } |
| 117 | |
| 118 | /* |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 119 | * Computes the remaining time between tv1=now and event=tv2. if tv2 is passed, |
| 120 | * 0 is returned. The result is stored into tv. |
| 121 | */ |
| 122 | REGPRM3 struct timeval *_tv_remain(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv) |
| 123 | { |
| 124 | return __tv_remain(tv1, tv2, tv); |
| 125 | } |
| 126 | |
| 127 | /* |
| 128 | * Computes the remaining time between tv1=now and event=tv2. if tv2 is passed, |
| 129 | * 0 is returned. The result is stored into tv. Returns ETERNITY if tv2 is |
| 130 | * eternity. |
| 131 | */ |
| 132 | REGPRM3 struct timeval *_tv_remain2(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv) |
| 133 | { |
| 134 | return __tv_remain2(tv1, tv2, tv); |
| 135 | } |
| 136 | |
Willy Tarreau | 0481c20 | 2007-05-13 16:03:27 +0200 | [diff] [blame] | 137 | /* tv_isle: compares <tv1> and <tv2> : returns 1 if tv1 <= tv2, otherwise 0 */ |
| 138 | REGPRM2 int _tv_isle(const struct timeval *tv1, const struct timeval *tv2) |
| 139 | { |
| 140 | return __tv_isle(tv1, tv2); |
| 141 | } |
| 142 | |
| 143 | /* tv_isgt: compares <tv1> and <tv2> : returns 1 if tv1 > tv2, otherwise 0 */ |
| 144 | REGPRM2 int _tv_isgt(const struct timeval *tv1, const struct timeval *tv2) |
| 145 | { |
| 146 | return __tv_isgt(tv1, tv2); |
| 147 | } |
| 148 | |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 149 | /* tv_udpate_date: sets <date> to system time, and sets <now> to something as |
| 150 | * close as possible to real time, following a monotonic function. The main |
| 151 | * principle consists in detecting backwards and forwards time jumps and adjust |
| 152 | * an offset to correct them. This function should be called once after each |
| 153 | * poll, and never farther apart than MAX_DELAY_MS*2. The poll's timeout should |
| 154 | * be passed in <max_wait>, and the return value in <interrupted> (a non-zero |
| 155 | * value means that we have not expired the timeout). Calling it with (-1,*) |
| 156 | * sets both <date> and <now> to current date, and calling it with (0,1) simply |
| 157 | * updates the values. |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 158 | */ |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 159 | REGPRM2 void tv_update_date(int max_wait, int interrupted) |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 160 | { |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 161 | static struct timeval tv_offset; /* warning: signed offset! */ |
| 162 | struct timeval adjusted, deadline; |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 163 | |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 164 | gettimeofday(&date, NULL); |
| 165 | if (unlikely(max_wait < 0)) { |
| 166 | tv_zero(&tv_offset); |
Willy Tarreau | 7559058 | 2009-03-05 14:54:50 +0100 | [diff] [blame] | 167 | adjusted = date; |
Willy Tarreau | e6313a3 | 2008-06-29 13:47:25 +0200 | [diff] [blame] | 168 | goto to_ms; |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 169 | } |
| 170 | __tv_add(&adjusted, &date, &tv_offset); |
| 171 | if (unlikely(__tv_islt(&adjusted, &now))) { |
| 172 | goto fixup; /* jump in the past */ |
| 173 | } |
| 174 | |
| 175 | /* OK we did not jump backwards, let's see if we have jumped too far |
| 176 | * forwards. The poll value was in <max_wait>, we accept that plus |
| 177 | * MAX_DELAY_MS to cover additional time. |
| 178 | */ |
| 179 | _tv_ms_add(&deadline, &now, max_wait + MAX_DELAY_MS); |
Willy Tarreau | 7559058 | 2009-03-05 14:54:50 +0100 | [diff] [blame] | 180 | if (likely(__tv_islt(&adjusted, &deadline))) |
| 181 | goto to_ms; /* OK time is within expected range */ |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 182 | fixup: |
| 183 | /* Large jump. If the poll was interrupted, we consider that the date |
| 184 | * has not changed (immediate wake-up), otherwise we add the poll |
| 185 | * time-out to the previous date. The new offset is recomputed. |
| 186 | */ |
Willy Tarreau | 7559058 | 2009-03-05 14:54:50 +0100 | [diff] [blame] | 187 | _tv_ms_add(&adjusted, &now, interrupted ? 0 : max_wait); |
| 188 | |
| 189 | tv_offset.tv_sec = adjusted.tv_sec - date.tv_sec; |
| 190 | tv_offset.tv_usec = adjusted.tv_usec - date.tv_usec; |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 191 | if (tv_offset.tv_usec < 0) { |
| 192 | tv_offset.tv_usec += 1000000; |
| 193 | tv_offset.tv_sec--; |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 194 | } |
Willy Tarreau | e6313a3 | 2008-06-29 13:47:25 +0200 | [diff] [blame] | 195 | to_ms: |
Willy Tarreau | 7559058 | 2009-03-05 14:54:50 +0100 | [diff] [blame] | 196 | now = adjusted; |
| 197 | curr_sec_ms = now.tv_usec / 1000; /* ms of current second */ |
| 198 | curr_sec_ms_scaled = curr_sec_ms * 4294971; /* ms * 2^32 / 1000 */ |
| 199 | now_ms = now.tv_sec * 1000 + curr_sec_ms; |
Willy Tarreau | b0b37bc | 2008-06-23 14:00:57 +0200 | [diff] [blame] | 200 | return; |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 201 | } |
| 202 | |
Willy Tarreau | c8f24f8 | 2007-11-30 18:38:35 +0100 | [diff] [blame] | 203 | char *human_time(int t, short hz_div) { |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 204 | static char rv[sizeof("24855d23h")+1]; // longest of "23h59m" and "59m59s" |
| 205 | char *p = rv; |
| 206 | int cnt=2; // print two numbers |
| 207 | |
Willy Tarreau | c8f24f8 | 2007-11-30 18:38:35 +0100 | [diff] [blame] | 208 | if (unlikely(t < 0 || hz_div <= 0)) { |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 209 | sprintf(p, "?"); |
| 210 | return rv; |
| 211 | } |
| 212 | |
Willy Tarreau | c8f24f8 | 2007-11-30 18:38:35 +0100 | [diff] [blame] | 213 | if (unlikely(hz_div > 1)) |
| 214 | t /= hz_div; |
Krzysztof Oledzki | 8513094 | 2007-10-22 16:21:10 +0200 | [diff] [blame] | 215 | |
| 216 | if (t >= DAY) { |
| 217 | p += sprintf(p, "%dd", t / DAY); |
| 218 | cnt--; |
| 219 | } |
| 220 | |
| 221 | if (cnt && t % DAY / HOUR) { |
| 222 | p += sprintf(p, "%dh", t % DAY / HOUR); |
| 223 | cnt--; |
| 224 | } |
| 225 | |
| 226 | if (cnt && t % HOUR / MINUTE) { |
| 227 | p += sprintf(p, "%dm", t % HOUR / MINUTE); |
| 228 | cnt--; |
| 229 | } |
| 230 | |
| 231 | if ((cnt && t % MINUTE) || !t) // also display '0s' |
| 232 | p += sprintf(p, "%ds", t % MINUTE / SEC); |
| 233 | |
| 234 | return rv; |
| 235 | } |
| 236 | |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 237 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 238 | * Local variables: |
| 239 | * c-indent-level: 8 |
| 240 | * c-basic-offset: 8 |
| 241 | * End: |
| 242 | */ |