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 | 5e8f066 | 2007-02-12 00:59:08 +0100 | [diff] [blame] | 65 | * compares <tv1> and <tv2> : returns 0 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 | 5e8f066 | 2007-02-12 00:59:08 +0100 | [diff] [blame] | 67 | */ |
| 68 | REGPRM2 int tv_cmp_ge2(const struct timeval *tv1, const struct timeval *tv2) |
| 69 | { |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame^] | 70 | if ((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec) |
Willy Tarreau | 5e8f066 | 2007-02-12 00:59:08 +0100 | [diff] [blame] | 71 | return 1; |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame^] | 72 | if ((unsigned)tv1->tv_sec < (unsigned)tv2->tv_sec) |
Willy Tarreau | 5e8f066 | 2007-02-12 00:59:08 +0100 | [diff] [blame] | 73 | return 0; |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame^] | 74 | if ((unsigned)tv1->tv_usec >= (unsigned)tv2->tv_usec) |
Willy Tarreau | 5e8f066 | 2007-02-12 00:59:08 +0100 | [diff] [blame] | 75 | return 1; |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 80 | * compares <tv1> and <tv2> : returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2, |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame^] | 81 | * assuming that TV_ETERNITY is greater than everything. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 82 | */ |
Willy Tarreau | fb27867 | 2006-10-15 15:38:50 +0200 | [diff] [blame] | 83 | REGPRM2 int tv_cmp2(const struct timeval *tv1, const struct timeval *tv2) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 84 | { |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame^] | 85 | if ((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 86 | return 1; |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame^] | 87 | else if ((unsigned)tv1->tv_sec < (unsigned)tv2->tv_sec) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 88 | return -1; |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame^] | 89 | else if ((unsigned)tv1->tv_usec > (unsigned)tv2->tv_usec) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 90 | return 1; |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame^] | 91 | else if ((unsigned)tv1->tv_usec < (unsigned)tv2->tv_usec) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 92 | return -1; |
| 93 | else |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | /* |
| 98 | * 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^] | 99 | * assuming that TV_ETERNITY is greater than everything. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 100 | */ |
Willy Tarreau | fb27867 | 2006-10-15 15:38:50 +0200 | [diff] [blame] | 101 | 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] | 102 | { |
| 103 | if (tv_iseternity(tv1)) |
| 104 | if (tv_iseternity(tv2)) |
| 105 | return 0; /* same */ |
| 106 | else |
| 107 | return 1; /* tv1 later than tv2 */ |
| 108 | else if (tv_iseternity(tv2)) |
| 109 | return -1; /* tv2 later than tv1 */ |
| 110 | |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame^] | 111 | if ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) { |
| 112 | if ((unsigned)tv1->tv_usec >= (unsigned)tv2->tv_usec + 1000) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 113 | return 1; |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame^] | 114 | else if ((unsigned)tv2->tv_usec >= (unsigned)tv1->tv_usec + 1000) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 115 | return -1; |
| 116 | else |
| 117 | return 0; |
| 118 | } |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame^] | 119 | else if (((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec + 1) || |
| 120 | (((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec + 1) && |
| 121 | ((unsigned)tv1->tv_usec + 1000000 >= (unsigned)tv2->tv_usec + 1000))) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 122 | return 1; |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame^] | 123 | else if (((unsigned)tv2->tv_sec > (unsigned)tv1->tv_sec + 1) || |
| 124 | (((unsigned)tv2->tv_sec == (unsigned)tv1->tv_sec + 1) && |
| 125 | ((unsigned)tv2->tv_usec + 1000000 >= (unsigned)tv1->tv_usec + 1000))) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 126 | return -1; |
| 127 | else |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | /* |
| 132 | * returns the remaining time between tv1=now and event=tv2 |
| 133 | * if tv2 is passed, 0 is returned. |
| 134 | * Returns TIME_ETERNITY if tv2 is eternity. |
| 135 | */ |
Willy Tarreau | fb27867 | 2006-10-15 15:38:50 +0200 | [diff] [blame] | 136 | 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] | 137 | { |
| 138 | unsigned long ret; |
| 139 | |
| 140 | if (tv_iseternity(tv2)) |
| 141 | return TIME_ETERNITY; |
| 142 | |
| 143 | if (tv_cmp_ms(tv1, tv2) >= 0) |
| 144 | return 0; /* event elapsed */ |
| 145 | |
| 146 | ret = (tv2->tv_sec - tv1->tv_sec) * 1000; |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame^] | 147 | if ((unsigned)tv2->tv_usec > (unsigned)tv1->tv_usec) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 148 | ret += (tv2->tv_usec - tv1->tv_usec) / 1000; |
| 149 | else |
| 150 | ret -= (tv1->tv_usec - tv2->tv_usec) / 1000; |
| 151 | return (unsigned long) ret; |
| 152 | } |
| 153 | |
| 154 | |
| 155 | /* |
| 156 | * returns the absolute difference, in ms, between tv1 and tv2 |
| 157 | * Must not be used when either argument is eternity. |
| 158 | */ |
Willy Tarreau | fb27867 | 2006-10-15 15:38:50 +0200 | [diff] [blame] | 159 | 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] | 160 | { |
| 161 | int cmp; |
| 162 | unsigned long ret; |
| 163 | |
| 164 | |
| 165 | cmp = tv_cmp(tv1, tv2); |
| 166 | if (!cmp) |
| 167 | return 0; /* same dates, null diff */ |
| 168 | else if (cmp < 0) { |
Willy Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 169 | const struct timeval *tmp = tv1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 170 | tv1 = tv2; |
| 171 | tv2 = tmp; |
| 172 | } |
| 173 | ret = (tv1->tv_sec - tv2->tv_sec) * 1000; |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame^] | 174 | if ((unsigned)tv1->tv_usec > (unsigned)tv2->tv_usec) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 175 | ret += (tv1->tv_usec - tv2->tv_usec) / 1000; |
| 176 | else |
| 177 | ret -= (tv2->tv_usec - tv1->tv_usec) / 1000; |
| 178 | return (unsigned long) ret; |
| 179 | } |
| 180 | |
| 181 | /* |
| 182 | * Local variables: |
| 183 | * c-indent-level: 8 |
| 184 | * c-basic-offset: 8 |
| 185 | * End: |
| 186 | */ |