blob: 5318dcdf2665608ab14ea78fc7eb24faf57cad53 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Time calculation functions.
3 *
Willy Tarreaub7f694f2008-06-22 17:18:02 +02004 * Copyright 2000-2008 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
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 Tarreaue3ba5f02006-06-29 18:54:54 +020014
15#include <common/config.h>
Willy Tarreau5e8f0662007-02-12 00:59:08 +010016#include <common/standard.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020017#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020018
Willy Tarreaue6313a32008-06-29 13:47:25 +020019unsigned int now_ms; /* internal date in milliseconds (may wrap) */
Willy Tarreaub7f694f2008-06-22 17:18:02 +020020struct timeval now; /* internal date is a monotonic function of real clock */
21struct timeval date; /* the real current date */
Willy Tarreaubaaee002006-06-26 02:48:02 +020022struct timeval start_date; /* the process's start date */
23
24/*
25 * adds <ms> ms to <from>, set the result to <tv> and returns a pointer <tv>
26 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +020027REGPRM3 struct timeval *_tv_ms_add(struct timeval *tv, const struct timeval *from, int ms)
Willy Tarreaubaaee002006-06-26 02:48:02 +020028{
Willy Tarreau42aae5c2007-04-29 17:43:56 +020029 tv->tv_usec = from->tv_usec + (ms % 1000) * 1000;
30 tv->tv_sec = from->tv_sec + (ms / 1000);
Willy Tarreaubaaee002006-06-26 02:48:02 +020031 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
Willy Tarreau42aae5c2007-04-29 17:43:56 +020040 * Must not be used when either argument is eternity. Use tv_ms_cmp2() for that.
Willy Tarreaubaaee002006-06-26 02:48:02 +020041 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +020042REGPRM2 int _tv_ms_cmp(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +020043{
Willy Tarreau42aae5c2007-04-29 17:43:56 +020044 return __tv_ms_cmp(tv1, tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +020045}
46
47/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020048 * compares <tv1> and <tv2> modulo 1 ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2,
Willy Tarreaua6a6a932007-04-28 22:40:08 +020049 * assuming that TV_ETERNITY is greater than everything.
Willy Tarreaubaaee002006-06-26 02:48:02 +020050 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +020051REGPRM2 int _tv_ms_cmp2(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +020052{
Willy Tarreau42aae5c2007-04-29 17:43:56 +020053 return __tv_ms_cmp2(tv1, tv2);
Willy Tarreau8d7d1492007-04-29 10:50:43 +020054}
55
56/*
57 * compares <tv1> and <tv2> modulo 1 ms: returns 1 if tv1 <= tv2, 0 if tv1 > tv2,
58 * assuming that TV_ETERNITY is greater than everything. Returns 0 if tv1 is
59 * TV_ETERNITY, and always assumes that tv2 != TV_ETERNITY. Designed to replace
Willy Tarreau42aae5c2007-04-29 17:43:56 +020060 * occurrences of (tv_ms_cmp2(tv,now) <= 0).
Willy Tarreau8d7d1492007-04-29 10:50:43 +020061 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +020062REGPRM2 int _tv_ms_le2(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreau8d7d1492007-04-29 10:50:43 +020063{
Willy Tarreau42aae5c2007-04-29 17:43:56 +020064 return __tv_ms_le2(tv1, tv2);
65}
Willy Tarreau8d7d1492007-04-29 10:50:43 +020066
Willy Tarreau42aae5c2007-04-29 17:43:56 +020067/*
68 * returns the remaining time between tv1=now and event=tv2
69 * if tv2 is passed, 0 is returned.
70 * Must not be used when either argument is eternity.
71 */
72REGPRM2 unsigned long _tv_ms_remain(const struct timeval *tv1, const struct timeval *tv2)
73{
74 return __tv_ms_remain(tv1, tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +020075}
76
77/*
78 * returns the remaining time between tv1=now and event=tv2
79 * if tv2 is passed, 0 is returned.
80 * Returns TIME_ETERNITY if tv2 is eternity.
81 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +020082REGPRM2 unsigned long _tv_ms_remain2(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +020083{
Willy Tarreaubaaee002006-06-26 02:48:02 +020084 if (tv_iseternity(tv2))
85 return TIME_ETERNITY;
86
Willy Tarreau42aae5c2007-04-29 17:43:56 +020087 return __tv_ms_remain(tv1, tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +020088}
89
Willy Tarreaubaaee002006-06-26 02:48:02 +020090/*
Willy Tarreau42aae5c2007-04-29 17:43:56 +020091 * Returns the time in ms elapsed between tv1 and tv2, assuming that tv1<=tv2.
Willy Tarreaubaaee002006-06-26 02:48:02 +020092 * Must not be used when either argument is eternity.
93 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +020094REGPRM2 unsigned long _tv_ms_elapsed(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +020095{
Willy Tarreau42aae5c2007-04-29 17:43:56 +020096 return __tv_ms_elapsed(tv1, tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +020097}
98
99/*
Willy Tarreaud825eef2007-05-12 22:35:00 +0200100 * adds <inc> to <from>, set the result to <tv> and returns a pointer <tv>
101 */
102REGPRM3 struct timeval *_tv_add(struct timeval *tv, const struct timeval *from, const struct timeval *inc)
103{
104 return __tv_add(tv, from, inc);
105}
106
107/*
Willy Tarreau0481c202007-05-13 16:03:27 +0200108 * If <inc> is set, then add it to <from> and set the result to <tv>, then
109 * return 1, otherwise return 0. It is meant to be used in if conditions.
110 */
111REGPRM3 int _tv_add_ifset(struct timeval *tv, const struct timeval *from, const struct timeval *inc)
112{
113 return __tv_add_ifset(tv, from, inc);
114}
115
116/*
Willy Tarreaud825eef2007-05-12 22:35:00 +0200117 * Computes the remaining time between tv1=now and event=tv2. if tv2 is passed,
118 * 0 is returned. The result is stored into tv.
119 */
120REGPRM3 struct timeval *_tv_remain(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv)
121{
122 return __tv_remain(tv1, tv2, tv);
123}
124
125/*
126 * Computes the remaining time between tv1=now and event=tv2. if tv2 is passed,
127 * 0 is returned. The result is stored into tv. Returns ETERNITY if tv2 is
128 * eternity.
129 */
130REGPRM3 struct timeval *_tv_remain2(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv)
131{
132 return __tv_remain2(tv1, tv2, tv);
133}
134
Willy Tarreau0481c202007-05-13 16:03:27 +0200135/* tv_isle: compares <tv1> and <tv2> : returns 1 if tv1 <= tv2, otherwise 0 */
136REGPRM2 int _tv_isle(const struct timeval *tv1, const struct timeval *tv2)
137{
138 return __tv_isle(tv1, tv2);
139}
140
141/* tv_isgt: compares <tv1> and <tv2> : returns 1 if tv1 > tv2, otherwise 0 */
142REGPRM2 int _tv_isgt(const struct timeval *tv1, const struct timeval *tv2)
143{
144 return __tv_isgt(tv1, tv2);
145}
146
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200147/* tv_udpate_date: sets <date> to system time, and sets <now> to something as
148 * close as possible to real time, following a monotonic function. The main
149 * principle consists in detecting backwards and forwards time jumps and adjust
150 * an offset to correct them. This function should be called once after each
151 * poll, and never farther apart than MAX_DELAY_MS*2. The poll's timeout should
152 * be passed in <max_wait>, and the return value in <interrupted> (a non-zero
153 * value means that we have not expired the timeout). Calling it with (-1,*)
154 * sets both <date> and <now> to current date, and calling it with (0,1) simply
155 * updates the values.
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200156 */
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200157REGPRM2 void tv_update_date(int max_wait, int interrupted)
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200158{
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200159 static struct timeval tv_offset; /* warning: signed offset! */
160 struct timeval adjusted, deadline;
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200161
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200162 gettimeofday(&date, NULL);
163 if (unlikely(max_wait < 0)) {
164 tv_zero(&tv_offset);
165 now = date;
Willy Tarreaue6313a32008-06-29 13:47:25 +0200166 goto to_ms;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200167 }
168 __tv_add(&adjusted, &date, &tv_offset);
169 if (unlikely(__tv_islt(&adjusted, &now))) {
170 goto fixup; /* jump in the past */
171 }
172
173 /* OK we did not jump backwards, let's see if we have jumped too far
174 * forwards. The poll value was in <max_wait>, we accept that plus
175 * MAX_DELAY_MS to cover additional time.
176 */
177 _tv_ms_add(&deadline, &now, max_wait + MAX_DELAY_MS);
178 if (unlikely(__tv_isge(&adjusted, &deadline))) {
179 goto fixup; /* jump in the future */
180 }
181 now = adjusted;
Willy Tarreaue6313a32008-06-29 13:47:25 +0200182 goto to_ms;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200183 fixup:
184 /* Large jump. If the poll was interrupted, we consider that the date
185 * has not changed (immediate wake-up), otherwise we add the poll
186 * time-out to the previous date. The new offset is recomputed.
187 */
188 if (!interrupted)
189 _tv_ms_add(&now, &now, max_wait);
190 tv_offset.tv_sec = now.tv_sec - date.tv_sec;
191 tv_offset.tv_usec = now.tv_usec - date.tv_usec;
192 if (tv_offset.tv_usec < 0) {
193 tv_offset.tv_usec += 1000000;
194 tv_offset.tv_sec--;
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200195 }
Willy Tarreaue6313a32008-06-29 13:47:25 +0200196 to_ms:
197 now_ms = now.tv_sec * 1000 + now.tv_usec / 1000;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200198 return;
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200199}
200
Willy Tarreauc8f24f82007-11-30 18:38:35 +0100201char *human_time(int t, short hz_div) {
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200202 static char rv[sizeof("24855d23h")+1]; // longest of "23h59m" and "59m59s"
203 char *p = rv;
204 int cnt=2; // print two numbers
205
Willy Tarreauc8f24f82007-11-30 18:38:35 +0100206 if (unlikely(t < 0 || hz_div <= 0)) {
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200207 sprintf(p, "?");
208 return rv;
209 }
210
Willy Tarreauc8f24f82007-11-30 18:38:35 +0100211 if (unlikely(hz_div > 1))
212 t /= hz_div;
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200213
214 if (t >= DAY) {
215 p += sprintf(p, "%dd", t / DAY);
216 cnt--;
217 }
218
219 if (cnt && t % DAY / HOUR) {
220 p += sprintf(p, "%dh", t % DAY / HOUR);
221 cnt--;
222 }
223
224 if (cnt && t % HOUR / MINUTE) {
225 p += sprintf(p, "%dm", t % HOUR / MINUTE);
226 cnt--;
227 }
228
229 if ((cnt && t % MINUTE) || !t) // also display '0s'
230 p += sprintf(p, "%ds", t % MINUTE / SEC);
231
232 return rv;
233}
234
Willy Tarreaud825eef2007-05-12 22:35:00 +0200235/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200236 * Local variables:
237 * c-indent-level: 8
238 * c-basic-offset: 8
239 * End:
240 */