Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 2 | include/common/time.h |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3 | Time calculation functions and macros. |
| 4 | |
| 5 | Copyright (C) 2000-2006 Willy Tarreau - w@1wt.eu |
| 6 | |
| 7 | This library is free software; you can redistribute it and/or |
| 8 | modify it under the terms of the GNU Lesser General Public |
| 9 | License as published by the Free Software Foundation, version 2.1 |
| 10 | exclusively. |
| 11 | |
| 12 | This library is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | Lesser General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU Lesser General Public |
| 18 | License along with this library; if not, write to the Free Software |
| 19 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 22 | #ifndef _COMMON_TIME_H |
| 23 | #define _COMMON_TIME_H |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 24 | |
| 25 | #include <stdlib.h> |
| 26 | #include <sys/time.h> |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 27 | #include <common/config.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 28 | |
| 29 | #define TIME_ETERNITY -1 |
| 30 | |
| 31 | /* returns the lowest delay amongst <old> and <new>, and respects TIME_ETERNITY */ |
| 32 | #define MINTIME(old, new) (((new)<0)?(old):(((old)<0||(new)<(old))?(new):(old))) |
| 33 | #define SETNOW(a) (*a=now) |
| 34 | |
| 35 | extern struct timeval now; /* the current date at any moment */ |
| 36 | extern struct timeval start_date; /* the process's start date */ |
| 37 | |
| 38 | |
| 39 | /* |
| 40 | * adds <ms> ms to <from>, set the result to <tv> and returns a pointer <tv> |
| 41 | */ |
Willy Tarreau | fb27867 | 2006-10-15 15:38:50 +0200 | [diff] [blame] | 42 | 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] | 43 | |
| 44 | /* |
| 45 | * compares <tv1> and <tv2> modulo 1ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2 |
| 46 | * Must not be used when either argument is eternity. Use tv_cmp2_ms() for that. |
| 47 | */ |
Willy Tarreau | fb27867 | 2006-10-15 15:38:50 +0200 | [diff] [blame] | 48 | 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] | 49 | |
| 50 | /* |
Willy Tarreau | 5e8f066 | 2007-02-12 00:59:08 +0100 | [diff] [blame] | 51 | * compares <tv1> and <tv2> : returns 0 if tv1 < tv2, 1 if tv1 >= tv2, |
| 52 | * considering that 0 is the eternity. |
| 53 | */ |
| 54 | REGPRM2 int tv_cmp_ge2(const struct timeval *tv1, const struct timeval *tv2); |
| 55 | |
| 56 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 57 | * compares <tv1> and <tv2> : returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2, |
| 58 | * considering that 0 is the eternity. |
| 59 | */ |
Willy Tarreau | fb27867 | 2006-10-15 15:38:50 +0200 | [diff] [blame] | 60 | REGPRM2 int tv_cmp2(const struct timeval *tv1, const struct timeval *tv2); |
Willy Tarreau | 5e8f066 | 2007-02-12 00:59:08 +0100 | [diff] [blame] | 61 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 62 | /* |
| 63 | * compares <tv1> and <tv2> modulo 1 ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2, |
| 64 | * considering that 0 is the eternity. |
| 65 | */ |
Willy Tarreau | fb27867 | 2006-10-15 15:38:50 +0200 | [diff] [blame] | 66 | 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] | 67 | |
| 68 | /* |
| 69 | * returns the remaining time between tv1=now and event=tv2 |
| 70 | * if tv2 is passed, 0 is returned. |
| 71 | * Returns TIME_ETERNITY if tv2 is eternity. |
| 72 | */ |
Willy Tarreau | fb27867 | 2006-10-15 15:38:50 +0200 | [diff] [blame] | 73 | 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] | 74 | |
| 75 | |
| 76 | /* sets <tv> to the current time */ |
Willy Tarreau | fb27867 | 2006-10-15 15:38:50 +0200 | [diff] [blame] | 77 | REGPRM1 static inline struct timeval *tv_now(struct timeval *tv) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 78 | { |
| 79 | if (tv) |
| 80 | gettimeofday(tv, NULL); |
| 81 | return tv; |
| 82 | } |
| 83 | |
| 84 | /* |
| 85 | * compares <tv1> and <tv2> : returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2 |
| 86 | * Must not be used when either argument is eternity. Use tv_cmp2() for that. |
| 87 | */ |
Willy Tarreau | fb27867 | 2006-10-15 15:38:50 +0200 | [diff] [blame] | 88 | REGPRM2 static inline int tv_cmp(const struct timeval *tv1, const struct timeval *tv2) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 89 | { |
| 90 | if (tv1->tv_sec < tv2->tv_sec) |
| 91 | return -1; |
| 92 | else if (tv1->tv_sec > tv2->tv_sec) |
| 93 | return 1; |
| 94 | else if (tv1->tv_usec < tv2->tv_usec) |
| 95 | return -1; |
| 96 | else if (tv1->tv_usec > tv2->tv_usec) |
| 97 | return 1; |
| 98 | else |
| 99 | return 0; |
Willy Tarreau | 5e8f066 | 2007-02-12 00:59:08 +0100 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | /* |
| 103 | * compares <tv1> and <tv2> : returns 0 if tv1 < tv2, 1 if tv1 >= tv2 |
| 104 | */ |
| 105 | REGPRM2 static inline int tv_cmp_ge(const struct timeval *tv1, const struct timeval *tv2) |
| 106 | { |
| 107 | if (tv1->tv_sec > tv2->tv_sec) |
| 108 | return 1; |
| 109 | if (tv1->tv_sec < tv2->tv_sec) |
| 110 | return 0; |
| 111 | if (tv1->tv_usec >= tv2->tv_usec) |
| 112 | return 1; |
| 113 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | /* |
| 117 | * returns the difference, in ms, between tv1 and tv2 |
| 118 | * Must not be used when either argument is eternity. |
| 119 | */ |
Willy Tarreau | fb27867 | 2006-10-15 15:38:50 +0200 | [diff] [blame] | 120 | REGPRM2 static inline unsigned long tv_diff(const struct timeval *tv1, const struct timeval *tv2) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 121 | { |
| 122 | unsigned long ret; |
| 123 | |
| 124 | ret = (tv2->tv_sec - tv1->tv_sec) * 1000; |
| 125 | if (tv2->tv_usec > tv1->tv_usec) |
| 126 | ret += (tv2->tv_usec - tv1->tv_usec) / 1000; |
| 127 | else |
| 128 | ret -= (tv1->tv_usec - tv2->tv_usec) / 1000; |
| 129 | return (unsigned long) ret; |
| 130 | } |
| 131 | |
| 132 | /* |
| 133 | * returns the remaining time between tv1=now and event=tv2 |
| 134 | * if tv2 is passed, 0 is returned. |
| 135 | * Must not be used when either argument is eternity. |
| 136 | */ |
Willy Tarreau | fb27867 | 2006-10-15 15:38:50 +0200 | [diff] [blame] | 137 | REGPRM2 static inline unsigned long tv_remain(const struct timeval *tv1, const struct timeval *tv2) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 138 | { |
| 139 | unsigned long ret; |
| 140 | |
| 141 | if (tv_cmp_ms(tv1, tv2) >= 0) |
| 142 | return 0; /* event elapsed */ |
| 143 | |
| 144 | ret = (tv2->tv_sec - tv1->tv_sec) * 1000; |
| 145 | if (tv2->tv_usec > tv1->tv_usec) |
| 146 | ret += (tv2->tv_usec - tv1->tv_usec) / 1000; |
| 147 | else |
| 148 | ret -= (tv1->tv_usec - tv2->tv_usec) / 1000; |
| 149 | return (unsigned long) ret; |
| 150 | } |
| 151 | |
| 152 | |
| 153 | /* |
| 154 | * zeroes a struct timeval |
| 155 | */ |
| 156 | |
Willy Tarreau | fb27867 | 2006-10-15 15:38:50 +0200 | [diff] [blame] | 157 | REGPRM1 static inline struct timeval *tv_eternity(struct timeval *tv) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 158 | { |
| 159 | tv->tv_sec = tv->tv_usec = 0; |
| 160 | return tv; |
| 161 | } |
| 162 | |
| 163 | /* |
| 164 | * returns 1 if tv is null, else 0 |
| 165 | */ |
Willy Tarreau | fb27867 | 2006-10-15 15:38:50 +0200 | [diff] [blame] | 166 | REGPRM1 static inline int tv_iseternity(const struct timeval *tv) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 167 | { |
Willy Tarreau | 5e8f066 | 2007-02-12 00:59:08 +0100 | [diff] [blame] | 168 | if ((tv->tv_sec | tv->tv_usec) == 0) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 169 | return 1; |
| 170 | else |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | /* |
| 175 | * returns the first event between tv1 and tv2 into tvmin. |
| 176 | * a zero tv is ignored. tvmin is returned. |
| 177 | */ |
Willy Tarreau | fb27867 | 2006-10-15 15:38:50 +0200 | [diff] [blame] | 178 | REGPRM3 static inline const struct timeval *tv_min(struct timeval *tvmin, |
Willy Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 179 | const struct timeval *tv1, |
| 180 | const struct timeval *tv2) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 181 | { |
| 182 | |
| 183 | if (tv_cmp2(tv1, tv2) <= 0) |
| 184 | *tvmin = *tv1; |
| 185 | else |
| 186 | *tvmin = *tv2; |
| 187 | |
| 188 | return tvmin; |
| 189 | } |
| 190 | |
| 191 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 192 | #endif /* _COMMON_TIME_H */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 193 | |
| 194 | /* |
| 195 | * Local variables: |
| 196 | * c-indent-level: 8 |
| 197 | * c-basic-offset: 8 |
| 198 | * End: |
| 199 | */ |