blob: f637f6c78a9eb2756d236c1dccbe70196809ae7f [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 Tarreaub7f694f2008-06-22 17:18:02 +020019struct timeval now; /* internal date is a monotonic function of real clock */
20struct timeval date; /* the real current date */
Willy Tarreaubaaee002006-06-26 02:48:02 +020021struct timeval start_date; /* the process's start date */
22
23/*
24 * adds <ms> ms to <from>, set the result to <tv> and returns a pointer <tv>
25 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +020026REGPRM3 struct timeval *_tv_ms_add(struct timeval *tv, const struct timeval *from, int ms)
Willy Tarreaubaaee002006-06-26 02:48:02 +020027{
Willy Tarreau42aae5c2007-04-29 17:43:56 +020028 tv->tv_usec = from->tv_usec + (ms % 1000) * 1000;
29 tv->tv_sec = from->tv_sec + (ms / 1000);
Willy Tarreaubaaee002006-06-26 02:48:02 +020030 while (tv->tv_usec >= 1000000) {
31 tv->tv_usec -= 1000000;
32 tv->tv_sec++;
33 }
34 return tv;
35}
36
37/*
38 * 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 +020039 * Must not be used when either argument is eternity. Use tv_ms_cmp2() for that.
Willy Tarreaubaaee002006-06-26 02:48:02 +020040 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +020041REGPRM2 int _tv_ms_cmp(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +020042{
Willy Tarreau42aae5c2007-04-29 17:43:56 +020043 return __tv_ms_cmp(tv1, tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +020044}
45
46/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020047 * 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 +020048 * assuming that TV_ETERNITY is greater than everything.
Willy Tarreaubaaee002006-06-26 02:48:02 +020049 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +020050REGPRM2 int _tv_ms_cmp2(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +020051{
Willy Tarreau42aae5c2007-04-29 17:43:56 +020052 return __tv_ms_cmp2(tv1, tv2);
Willy Tarreau8d7d1492007-04-29 10:50:43 +020053}
54
55/*
56 * compares <tv1> and <tv2> modulo 1 ms: returns 1 if tv1 <= tv2, 0 if tv1 > tv2,
57 * assuming that TV_ETERNITY is greater than everything. Returns 0 if tv1 is
58 * TV_ETERNITY, and always assumes that tv2 != TV_ETERNITY. Designed to replace
Willy Tarreau42aae5c2007-04-29 17:43:56 +020059 * occurrences of (tv_ms_cmp2(tv,now) <= 0).
Willy Tarreau8d7d1492007-04-29 10:50:43 +020060 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +020061REGPRM2 int _tv_ms_le2(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreau8d7d1492007-04-29 10:50:43 +020062{
Willy Tarreau42aae5c2007-04-29 17:43:56 +020063 return __tv_ms_le2(tv1, tv2);
64}
Willy Tarreau8d7d1492007-04-29 10:50:43 +020065
Willy Tarreau42aae5c2007-04-29 17:43:56 +020066/*
67 * returns the remaining time between tv1=now and event=tv2
68 * if tv2 is passed, 0 is returned.
69 * Must not be used when either argument is eternity.
70 */
71REGPRM2 unsigned long _tv_ms_remain(const struct timeval *tv1, const struct timeval *tv2)
72{
73 return __tv_ms_remain(tv1, tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +020074}
75
76/*
77 * returns the remaining time between tv1=now and event=tv2
78 * if tv2 is passed, 0 is returned.
79 * Returns TIME_ETERNITY if tv2 is eternity.
80 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +020081REGPRM2 unsigned long _tv_ms_remain2(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +020082{
Willy Tarreaubaaee002006-06-26 02:48:02 +020083 if (tv_iseternity(tv2))
84 return TIME_ETERNITY;
85
Willy Tarreau42aae5c2007-04-29 17:43:56 +020086 return __tv_ms_remain(tv1, tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +020087}
88
Willy Tarreaubaaee002006-06-26 02:48:02 +020089/*
Willy Tarreau42aae5c2007-04-29 17:43:56 +020090 * Returns the time in ms elapsed between tv1 and tv2, assuming that tv1<=tv2.
Willy Tarreaubaaee002006-06-26 02:48:02 +020091 * Must not be used when either argument is eternity.
92 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +020093REGPRM2 unsigned long _tv_ms_elapsed(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +020094{
Willy Tarreau42aae5c2007-04-29 17:43:56 +020095 return __tv_ms_elapsed(tv1, tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +020096}
97
98/*
Willy Tarreaud825eef2007-05-12 22:35:00 +020099 * adds <inc> to <from>, set the result to <tv> and returns a pointer <tv>
100 */
101REGPRM3 struct timeval *_tv_add(struct timeval *tv, const struct timeval *from, const struct timeval *inc)
102{
103 return __tv_add(tv, from, inc);
104}
105
106/*
Willy Tarreau0481c202007-05-13 16:03:27 +0200107 * If <inc> is set, then add it to <from> and set the result to <tv>, then
108 * return 1, otherwise return 0. It is meant to be used in if conditions.
109 */
110REGPRM3 int _tv_add_ifset(struct timeval *tv, const struct timeval *from, const struct timeval *inc)
111{
112 return __tv_add_ifset(tv, from, inc);
113}
114
115/*
Willy Tarreaud825eef2007-05-12 22:35:00 +0200116 * Computes the remaining time between tv1=now and event=tv2. if tv2 is passed,
117 * 0 is returned. The result is stored into tv.
118 */
119REGPRM3 struct timeval *_tv_remain(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv)
120{
121 return __tv_remain(tv1, tv2, tv);
122}
123
124/*
125 * Computes the remaining time between tv1=now and event=tv2. if tv2 is passed,
126 * 0 is returned. The result is stored into tv. Returns ETERNITY if tv2 is
127 * eternity.
128 */
129REGPRM3 struct timeval *_tv_remain2(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv)
130{
131 return __tv_remain2(tv1, tv2, tv);
132}
133
Willy Tarreau0481c202007-05-13 16:03:27 +0200134/* tv_isle: compares <tv1> and <tv2> : returns 1 if tv1 <= tv2, otherwise 0 */
135REGPRM2 int _tv_isle(const struct timeval *tv1, const struct timeval *tv2)
136{
137 return __tv_isle(tv1, tv2);
138}
139
140/* tv_isgt: compares <tv1> and <tv2> : returns 1 if tv1 > tv2, otherwise 0 */
141REGPRM2 int _tv_isgt(const struct timeval *tv1, const struct timeval *tv2)
142{
143 return __tv_isgt(tv1, tv2);
144}
145
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200146/* tv_udpate_date: sets <date> to system time, and sets <now> to something as
147 * close as possible to real time, following a monotonic function. The main
148 * principle consists in detecting backwards and forwards time jumps and adjust
149 * an offset to correct them. This function should be called once after each
150 * poll, and never farther apart than MAX_DELAY_MS*2. The poll's timeout should
151 * be passed in <max_wait>, and the return value in <interrupted> (a non-zero
152 * value means that we have not expired the timeout). Calling it with (-1,*)
153 * sets both <date> and <now> to current date, and calling it with (0,1) simply
154 * updates the values.
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200155 */
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200156REGPRM2 void tv_update_date(int max_wait, int interrupted)
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200157{
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200158 static struct timeval tv_offset; /* warning: signed offset! */
159 struct timeval adjusted, deadline;
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200160
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200161 gettimeofday(&date, NULL);
162 if (unlikely(max_wait < 0)) {
163 tv_zero(&tv_offset);
164 now = date;
165 return;
166 }
167 __tv_add(&adjusted, &date, &tv_offset);
168 if (unlikely(__tv_islt(&adjusted, &now))) {
169 goto fixup; /* jump in the past */
170 }
171
172 /* OK we did not jump backwards, let's see if we have jumped too far
173 * forwards. The poll value was in <max_wait>, we accept that plus
174 * MAX_DELAY_MS to cover additional time.
175 */
176 _tv_ms_add(&deadline, &now, max_wait + MAX_DELAY_MS);
177 if (unlikely(__tv_isge(&adjusted, &deadline))) {
178 goto fixup; /* jump in the future */
179 }
180 now = adjusted;
181 return;
182 fixup:
183 /* Large jump. If the poll was interrupted, we consider that the date
184 * has not changed (immediate wake-up), otherwise we add the poll
185 * time-out to the previous date. The new offset is recomputed.
186 */
187 if (!interrupted)
188 _tv_ms_add(&now, &now, max_wait);
189 tv_offset.tv_sec = now.tv_sec - date.tv_sec;
190 tv_offset.tv_usec = now.tv_usec - date.tv_usec;
191 if (tv_offset.tv_usec < 0) {
192 tv_offset.tv_usec += 1000000;
193 tv_offset.tv_sec--;
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200194 }
Willy Tarreaub0b37bc2008-06-23 14:00:57 +0200195 return;
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200196}
197
Willy Tarreauc8f24f82007-11-30 18:38:35 +0100198char *human_time(int t, short hz_div) {
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200199 static char rv[sizeof("24855d23h")+1]; // longest of "23h59m" and "59m59s"
200 char *p = rv;
201 int cnt=2; // print two numbers
202
Willy Tarreauc8f24f82007-11-30 18:38:35 +0100203 if (unlikely(t < 0 || hz_div <= 0)) {
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200204 sprintf(p, "?");
205 return rv;
206 }
207
Willy Tarreauc8f24f82007-11-30 18:38:35 +0100208 if (unlikely(hz_div > 1))
209 t /= hz_div;
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200210
211 if (t >= DAY) {
212 p += sprintf(p, "%dd", t / DAY);
213 cnt--;
214 }
215
216 if (cnt && t % DAY / HOUR) {
217 p += sprintf(p, "%dh", t % DAY / HOUR);
218 cnt--;
219 }
220
221 if (cnt && t % HOUR / MINUTE) {
222 p += sprintf(p, "%dm", t % HOUR / MINUTE);
223 cnt--;
224 }
225
226 if ((cnt && t % MINUTE) || !t) // also display '0s'
227 p += sprintf(p, "%ds", t % MINUTE / SEC);
228
229 return rv;
230}
231
Willy Tarreaud825eef2007-05-12 22:35:00 +0200232/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200233 * Local variables:
234 * c-indent-level: 8
235 * c-basic-offset: 8
236 * End:
237 */