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