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