Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Time calculation functions. |
| 3 | * |
| 4 | * Copyright 2000-2006 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 | 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 | |
| 19 | struct timeval now; /* the current date at any moment */ |
| 20 | struct timeval start_date; /* the process's start date */ |
| 21 | |
| 22 | /* |
| 23 | * adds <ms> ms to <from>, set the result to <tv> and returns a pointer <tv> |
| 24 | */ |
Willy Tarreau | fb27867 | 2006-10-15 15:38:50 +0200 | [diff] [blame] | 25 | REGPRM3 struct timeval *tv_delayfrom(struct timeval *tv, const struct timeval *from, int ms) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 26 | { |
| 27 | if (!tv || !from) |
| 28 | return NULL; |
| 29 | tv->tv_usec = from->tv_usec + (ms%1000)*1000; |
| 30 | tv->tv_sec = from->tv_sec + (ms/1000); |
| 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 |
| 40 | * Must not be used when either argument is eternity. Use tv_cmp2_ms() for that. |
| 41 | */ |
Willy Tarreau | fb27867 | 2006-10-15 15:38:50 +0200 | [diff] [blame] | 42 | REGPRM2 int tv_cmp_ms(const struct timeval *tv1, const struct timeval *tv2) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 43 | { |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 44 | if ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) { |
| 45 | if ((unsigned)tv2->tv_usec >= (unsigned)tv1->tv_usec + 1000) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 46 | return -1; |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 47 | else if ((unsigned)tv1->tv_usec >= (unsigned)tv2->tv_usec + 1000) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 48 | return 1; |
| 49 | else |
| 50 | return 0; |
| 51 | } |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 52 | else if (((unsigned)tv2->tv_sec > (unsigned)tv1->tv_sec + 1) || |
| 53 | (((unsigned)tv2->tv_sec == (unsigned)tv1->tv_sec + 1) && |
| 54 | ((unsigned)tv2->tv_usec + 1000000 >= (unsigned)tv1->tv_usec + 1000))) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 55 | return -1; |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 56 | else if (((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec + 1) || |
| 57 | (((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec + 1) && |
| 58 | ((unsigned)tv1->tv_usec + 1000000 >= (unsigned)tv2->tv_usec + 1000))) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 59 | return 1; |
| 60 | else |
| 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 65 | * 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] | 66 | * assuming that TV_ETERNITY is greater than everything. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 67 | */ |
Willy Tarreau | fb27867 | 2006-10-15 15:38:50 +0200 | [diff] [blame] | 68 | REGPRM2 int tv_cmp2_ms(const struct timeval *tv1, const struct timeval *tv2) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 69 | { |
| 70 | if (tv_iseternity(tv1)) |
| 71 | if (tv_iseternity(tv2)) |
| 72 | return 0; /* same */ |
| 73 | else |
| 74 | return 1; /* tv1 later than tv2 */ |
| 75 | else if (tv_iseternity(tv2)) |
| 76 | return -1; /* tv2 later than tv1 */ |
| 77 | |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 78 | if ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) { |
| 79 | if ((unsigned)tv1->tv_usec >= (unsigned)tv2->tv_usec + 1000) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 80 | return 1; |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 81 | else if ((unsigned)tv2->tv_usec >= (unsigned)tv1->tv_usec + 1000) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 82 | return -1; |
| 83 | else |
| 84 | return 0; |
| 85 | } |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 86 | else if (((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec + 1) || |
| 87 | (((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec + 1) && |
| 88 | ((unsigned)tv1->tv_usec + 1000000 >= (unsigned)tv2->tv_usec + 1000))) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 89 | return 1; |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 90 | else if (((unsigned)tv2->tv_sec > (unsigned)tv1->tv_sec + 1) || |
| 91 | (((unsigned)tv2->tv_sec == (unsigned)tv1->tv_sec + 1) && |
| 92 | ((unsigned)tv2->tv_usec + 1000000 >= (unsigned)tv1->tv_usec + 1000))) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 93 | return -1; |
| 94 | else |
| 95 | return 0; |
Willy Tarreau | 8d7d149 | 2007-04-29 10:50:43 +0200 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | /* |
| 99 | * compares <tv1> and <tv2> modulo 1 ms: returns 1 if tv1 <= tv2, 0 if tv1 > tv2, |
| 100 | * assuming that TV_ETERNITY is greater than everything. Returns 0 if tv1 is |
| 101 | * TV_ETERNITY, and always assumes that tv2 != TV_ETERNITY. Designed to replace |
| 102 | * occurrences of (tv_cmp2_ms(tv,now) <= 0). |
| 103 | */ |
| 104 | REGPRM2 int tv_cmp2_le(const struct timeval *tv1, const struct timeval *tv2) |
| 105 | { |
| 106 | if (likely((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec + 1)) |
| 107 | return 0; |
| 108 | |
| 109 | if (likely((unsigned)tv1->tv_sec < (unsigned)tv2->tv_sec)) |
| 110 | return 1; |
| 111 | |
| 112 | if (likely((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec)) { |
| 113 | if ((unsigned)tv2->tv_usec >= (unsigned)tv1->tv_usec + 1000) |
| 114 | return 1; |
| 115 | else |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | if (unlikely(((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec + 1) && |
| 120 | ((unsigned)tv1->tv_usec + 1000000 >= (unsigned)tv2->tv_usec + 1000))) |
| 121 | return 0; |
| 122 | else |
| 123 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | /* |
| 127 | * returns the remaining time between tv1=now and event=tv2 |
| 128 | * if tv2 is passed, 0 is returned. |
| 129 | * Returns TIME_ETERNITY if tv2 is eternity. |
| 130 | */ |
Willy Tarreau | fb27867 | 2006-10-15 15:38:50 +0200 | [diff] [blame] | 131 | REGPRM2 unsigned long tv_remain2(const struct timeval *tv1, const struct timeval *tv2) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 132 | { |
| 133 | unsigned long ret; |
| 134 | |
| 135 | if (tv_iseternity(tv2)) |
| 136 | return TIME_ETERNITY; |
| 137 | |
| 138 | if (tv_cmp_ms(tv1, tv2) >= 0) |
| 139 | return 0; /* event elapsed */ |
| 140 | |
| 141 | ret = (tv2->tv_sec - tv1->tv_sec) * 1000; |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 142 | if ((unsigned)tv2->tv_usec > (unsigned)tv1->tv_usec) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 143 | ret += (tv2->tv_usec - tv1->tv_usec) / 1000; |
| 144 | else |
| 145 | ret -= (tv1->tv_usec - tv2->tv_usec) / 1000; |
| 146 | return (unsigned long) ret; |
| 147 | } |
| 148 | |
| 149 | |
| 150 | /* |
| 151 | * returns the absolute difference, in ms, between tv1 and tv2 |
| 152 | * Must not be used when either argument is eternity. |
| 153 | */ |
Willy Tarreau | fb27867 | 2006-10-15 15:38:50 +0200 | [diff] [blame] | 154 | REGPRM2 unsigned long tv_delta(const struct timeval *tv1, const struct timeval *tv2) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 155 | { |
| 156 | int cmp; |
| 157 | unsigned long ret; |
| 158 | |
| 159 | |
| 160 | cmp = tv_cmp(tv1, tv2); |
| 161 | if (!cmp) |
| 162 | return 0; /* same dates, null diff */ |
| 163 | else if (cmp < 0) { |
Willy Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 164 | const struct timeval *tmp = tv1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 165 | tv1 = tv2; |
| 166 | tv2 = tmp; |
| 167 | } |
| 168 | ret = (tv1->tv_sec - tv2->tv_sec) * 1000; |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 169 | if ((unsigned)tv1->tv_usec > (unsigned)tv2->tv_usec) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 170 | ret += (tv1->tv_usec - tv2->tv_usec) / 1000; |
| 171 | else |
| 172 | ret -= (tv2->tv_usec - tv1->tv_usec) / 1000; |
| 173 | return (unsigned long) ret; |
| 174 | } |
| 175 | |
| 176 | /* |
| 177 | * Local variables: |
| 178 | * c-indent-level: 8 |
| 179 | * c-basic-offset: 8 |
| 180 | * End: |
| 181 | */ |