blob: cb1e40b5aebf7a7857db2c06c38d31e707242e97 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
Willy Tarreau2dd0d472006-06-29 17:53:05 +02002 include/common/time.h
Willy Tarreaubaaee002006-06-26 02:48:02 +02003 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 Tarreau2dd0d472006-06-29 17:53:05 +020022#ifndef _COMMON_TIME_H
23#define _COMMON_TIME_H
Willy Tarreaubaaee002006-06-26 02:48:02 +020024
25#include <stdlib.h>
26#include <sys/time.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020027#include <common/config.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020028
Willy Tarreaua6a6a932007-04-28 22:40:08 +020029/* eternity when exprimed in timeval */
30#ifndef TV_ETERNITY
31#define TV_ETERNITY (~0UL)
32#endif
33
34/* eternity when exprimed in ms */
35#ifndef TV_ETERNITY_MS
36#define TV_ETERNITY_MS (-1)
37#endif
38
39#define TIME_ETERNITY (TV_ETERNITY_MS)
40
Willy Tarreaubaaee002006-06-26 02:48:02 +020041
42/* returns the lowest delay amongst <old> and <new>, and respects TIME_ETERNITY */
43#define MINTIME(old, new) (((new)<0)?(old):(((old)<0||(new)<(old))?(new):(old)))
44#define SETNOW(a) (*a=now)
45
46extern struct timeval now; /* the current date at any moment */
47extern struct timeval start_date; /* the process's start date */
48
49
50/*
51 * adds <ms> ms to <from>, set the result to <tv> and returns a pointer <tv>
52 */
Willy Tarreaufb278672006-10-15 15:38:50 +020053REGPRM3 struct timeval *tv_delayfrom(struct timeval *tv, const struct timeval *from, int ms);
Willy Tarreaubaaee002006-06-26 02:48:02 +020054
55/*
56 * compares <tv1> and <tv2> modulo 1ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2
57 * Must not be used when either argument is eternity. Use tv_cmp2_ms() for that.
58 */
Willy Tarreaufb278672006-10-15 15:38:50 +020059REGPRM2 int tv_cmp_ms(const struct timeval *tv1, const struct timeval *tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +020060
61/*
Willy Tarreau5e8f0662007-02-12 00:59:08 +010062 * compares <tv1> and <tv2> : returns 0 if tv1 < tv2, 1 if tv1 >= tv2,
63 * considering that 0 is the eternity.
64 */
65REGPRM2 int tv_cmp_ge2(const struct timeval *tv1, const struct timeval *tv2);
66
67/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020068 * compares <tv1> and <tv2> : returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2,
69 * considering that 0 is the eternity.
70 */
Willy Tarreaufb278672006-10-15 15:38:50 +020071REGPRM2 int tv_cmp2(const struct timeval *tv1, const struct timeval *tv2);
Willy Tarreau5e8f0662007-02-12 00:59:08 +010072
Willy Tarreaubaaee002006-06-26 02:48:02 +020073/*
74 * compares <tv1> and <tv2> modulo 1 ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2,
75 * considering that 0 is the eternity.
76 */
Willy Tarreaufb278672006-10-15 15:38:50 +020077REGPRM2 int tv_cmp2_ms(const struct timeval *tv1, const struct timeval *tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +020078
79/*
80 * returns the remaining time between tv1=now and event=tv2
81 * if tv2 is passed, 0 is returned.
82 * Returns TIME_ETERNITY if tv2 is eternity.
83 */
Willy Tarreaufb278672006-10-15 15:38:50 +020084REGPRM2 unsigned long tv_remain2(const struct timeval *tv1, const struct timeval *tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +020085
Willy Tarreaubaaee002006-06-26 02:48:02 +020086/* sets <tv> to the current time */
Willy Tarreaufb278672006-10-15 15:38:50 +020087REGPRM1 static inline struct timeval *tv_now(struct timeval *tv)
Willy Tarreaubaaee002006-06-26 02:48:02 +020088{
89 if (tv)
90 gettimeofday(tv, NULL);
91 return tv;
92}
93
94/*
95 * compares <tv1> and <tv2> : returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2
96 * Must not be used when either argument is eternity. Use tv_cmp2() for that.
97 */
Willy Tarreaufb278672006-10-15 15:38:50 +020098REGPRM2 static inline int tv_cmp(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +020099{
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200100 if ((unsigned)tv1->tv_sec < (unsigned)tv2->tv_sec)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200101 return -1;
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200102 else if ((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200103 return 1;
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200104 else if ((unsigned)tv1->tv_usec < (unsigned)tv2->tv_usec)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200105 return -1;
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200106 else if ((unsigned)tv1->tv_usec > (unsigned)tv2->tv_usec)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200107 return 1;
108 else
109 return 0;
Willy Tarreau5e8f0662007-02-12 00:59:08 +0100110}
111
112/*
113 * compares <tv1> and <tv2> : returns 0 if tv1 < tv2, 1 if tv1 >= tv2
114 */
115REGPRM2 static inline int tv_cmp_ge(const struct timeval *tv1, const struct timeval *tv2)
116{
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200117 if ((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec)
Willy Tarreau5e8f0662007-02-12 00:59:08 +0100118 return 1;
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200119 if ((unsigned)tv1->tv_sec < (unsigned)tv2->tv_sec)
Willy Tarreau5e8f0662007-02-12 00:59:08 +0100120 return 0;
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200121 if ((unsigned)tv1->tv_usec >= (unsigned)tv2->tv_usec)
Willy Tarreau5e8f0662007-02-12 00:59:08 +0100122 return 1;
123 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200124}
125
126/*
127 * returns the difference, in ms, between tv1 and tv2
128 * Must not be used when either argument is eternity.
129 */
Willy Tarreaufb278672006-10-15 15:38:50 +0200130REGPRM2 static inline unsigned long tv_diff(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200131{
132 unsigned long ret;
133
134 ret = (tv2->tv_sec - tv1->tv_sec) * 1000;
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200135 if ((unsigned)tv2->tv_usec > (unsigned)tv1->tv_usec)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200136 ret += (tv2->tv_usec - tv1->tv_usec) / 1000;
137 else
138 ret -= (tv1->tv_usec - tv2->tv_usec) / 1000;
139 return (unsigned long) ret;
140}
141
142/*
143 * returns the remaining time between tv1=now and event=tv2
144 * if tv2 is passed, 0 is returned.
145 * Must not be used when either argument is eternity.
146 */
Willy Tarreaufb278672006-10-15 15:38:50 +0200147REGPRM2 static inline unsigned long tv_remain(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200148{
149 unsigned long ret;
150
151 if (tv_cmp_ms(tv1, tv2) >= 0)
152 return 0; /* event elapsed */
153
154 ret = (tv2->tv_sec - tv1->tv_sec) * 1000;
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200155 if ((unsigned)tv2->tv_usec > (unsigned)tv1->tv_usec)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200156 ret += (tv2->tv_usec - tv1->tv_usec) / 1000;
157 else
158 ret -= (tv1->tv_usec - tv2->tv_usec) / 1000;
159 return (unsigned long) ret;
160}
161
162
163/*
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200164 * sets a struct timeval to its highest value so that it can never happen
165 * note that only tv_usec is necessary to detect it since a tv_usec > 999999
166 * is normally not possible.
167 *
Willy Tarreaubaaee002006-06-26 02:48:02 +0200168 */
169
Willy Tarreaufb278672006-10-15 15:38:50 +0200170REGPRM1 static inline struct timeval *tv_eternity(struct timeval *tv)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200171{
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200172 tv->tv_sec = tv->tv_usec = TV_ETERNITY;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200173 return tv;
174}
175
176/*
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200177 * sets a struct timeval to 0
178 *
Willy Tarreaubaaee002006-06-26 02:48:02 +0200179 */
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200180REGPRM1 static inline struct timeval *tv_zero(struct timeval *tv) {
181 tv->tv_sec = tv->tv_usec = 0;
182 return tv;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200183}
184
185/*
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200186 * returns non null if tv is [eternity], otherwise 0.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200187 */
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200188#define tv_iseternity(tv) ((tv)->tv_usec == TV_ETERNITY)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200189
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200190/*
191 * returns non null if tv is [0], otherwise 0.
192 */
193#define tv_iszero(tv) (((tv)->tv_sec | (tv)->tv_usec) == 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200194
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200195/*
196 * compares <tv1> and <tv2> : returns 1 if <tv1> is before <tv2>, otherwise 0.
197 * This should be very fast because it's used in schedulers.
198 * It has been optimized to return 1 (so call it in a loop which continues
199 * as long as tv1<=tv2)
200 */
201
202#define tv_isbefore(tv1, tv2) \
203 (unlikely((unsigned)(tv1)->tv_sec < (unsigned)(tv2)->tv_sec) ? 1 : \
204 (unlikely((unsigned)(tv1)->tv_sec > (unsigned)(tv2)->tv_sec) ? 0 : \
205 unlikely((unsigned)(tv1)->tv_usec < (unsigned)(tv2)->tv_usec)))
206
207/*
208 * returns the first event between <tv1> and <tv2> into <tvmin>.
209 * a zero tv is ignored. <tvmin> is returned. If <tvmin> is known
210 * to be the same as <tv1> or <tv2>, it is recommended to use
211 * tv_bound instead.
212 */
213#define tv_min(tvmin, tv1, tv2) ({ \
214 if (tv_isbefore(tv1, tv2)) { \
215 *tvmin = *tv1; \
216 } \
217 else { \
218 *tvmin = *tv2; \
219 } \
220 tvmin; \
221})
222
223/*
224 * returns the first event between <tv1> and <tv2> into <tvmin>.
225 * a zero tv is ignored. <tvmin> is returned. This function has been
226 * optimized to be called as tv_min(a,a,b) or tv_min(b,a,b).
227 */
228#define tv_bound(tv1, tv2) ({ \
229 if (tv_isbefore(tv2, tv1)) \
230 *tv1 = *tv2; \
231 tv1; \
232})
Willy Tarreaubaaee002006-06-26 02:48:02 +0200233
234
Willy Tarreau2dd0d472006-06-29 17:53:05 +0200235#endif /* _COMMON_TIME_H */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200236
237/*
238 * Local variables:
239 * c-indent-level: 8
240 * c-basic-offset: 8
241 * End:
242 */